1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2014 Nicira, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_vlan.h>
14 #include <linux/jhash.h>
15 #include <linux/delay.h>
16 #include <linux/time.h>
17 #include <linux/etherdevice.h>
18 #include <linux/genetlink.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/mutex.h>
22 #include <linux/percpu.h>
23 #include <linux/rcupdate.h>
24 #include <linux/tcp.h>
25 #include <linux/udp.h>
26 #include <linux/ethtool.h>
27 #include <linux/wait.h>
28 #include <asm/div64.h>
29 #include <linux/highmem.h>
30 #include <linux/netfilter_bridge.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/inetdevice.h>
33 #include <linux/list.h>
34 #include <linux/openvswitch.h>
35 #include <linux/rculist.h>
36 #include <linux/dmi.h>
37 #include <net/genetlink.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
43 #include "flow_table.h"
44 #include "flow_netlink.h"
46 #include "vport-internal_dev.h"
47 #include "vport-netdev.h"
49 unsigned int ovs_net_id __read_mostly
;
51 static struct genl_family dp_packet_genl_family
;
52 static struct genl_family dp_flow_genl_family
;
53 static struct genl_family dp_datapath_genl_family
;
55 static const struct nla_policy flow_policy
[];
57 static const struct genl_multicast_group ovs_dp_flow_multicast_group
= {
58 .name
= OVS_FLOW_MCGROUP
,
61 static const struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
62 .name
= OVS_DATAPATH_MCGROUP
,
65 static const struct genl_multicast_group ovs_dp_vport_multicast_group
= {
66 .name
= OVS_VPORT_MCGROUP
,
69 /* Check if need to build a reply message.
70 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
71 static bool ovs_must_notify(struct genl_family
*family
, struct genl_info
*info
,
74 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
75 genl_has_listeners(family
, genl_info_net(info
), group
);
78 static void ovs_notify(struct genl_family
*family
,
79 struct sk_buff
*skb
, struct genl_info
*info
)
81 genl_notify(family
, skb
, info
, 0, GFP_KERNEL
);
87 * All writes e.g. Writes to device state (add/remove datapath, port, set
88 * operations on vports, etc.), Writes to other state (flow table
89 * modifications, set miscellaneous datapath parameters, etc.) are protected
92 * Reads are protected by RCU.
94 * There are a few special cases (mostly stats) that have their own
95 * synchronization but they nest under all of above and don't interact with
98 * The RTNL lock nests inside ovs_mutex.
101 static DEFINE_MUTEX(ovs_mutex
);
105 mutex_lock(&ovs_mutex
);
108 void ovs_unlock(void)
110 mutex_unlock(&ovs_mutex
);
113 #ifdef CONFIG_LOCKDEP
114 int lockdep_ovsl_is_held(void)
117 return lockdep_is_held(&ovs_mutex
);
123 static struct vport
*new_vport(const struct vport_parms
*);
124 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
125 const struct sw_flow_key
*,
126 const struct dp_upcall_info
*,
128 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
129 const struct sw_flow_key
*,
130 const struct dp_upcall_info
*,
133 /* Must be called with rcu_read_lock or ovs_mutex. */
134 const char *ovs_dp_name(const struct datapath
*dp
)
136 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
137 return ovs_vport_name(vport
);
140 static int get_dpifindex(const struct datapath
*dp
)
147 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
149 ifindex
= local
->dev
->ifindex
;
158 static void destroy_dp_rcu(struct rcu_head
*rcu
)
160 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
162 ovs_flow_tbl_destroy(&dp
->table
);
163 free_percpu(dp
->stats_percpu
);
169 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
172 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
175 /* Called with ovs_mutex or RCU read lock. */
176 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
179 struct hlist_head
*head
;
181 head
= vport_hash_bucket(dp
, port_no
);
182 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
,
183 lockdep_ovsl_is_held()) {
184 if (vport
->port_no
== port_no
)
190 /* Called with ovs_mutex. */
191 static struct vport
*new_vport(const struct vport_parms
*parms
)
195 vport
= ovs_vport_add(parms
);
196 if (!IS_ERR(vport
)) {
197 struct datapath
*dp
= parms
->dp
;
198 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
200 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
205 void ovs_dp_detach_port(struct vport
*p
)
209 /* First drop references to device. */
210 hlist_del_rcu(&p
->dp_hash_node
);
212 /* Then destroy it. */
216 /* Must be called with rcu_read_lock. */
217 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
219 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
220 struct datapath
*dp
= p
->dp
;
221 struct sw_flow
*flow
;
222 struct sw_flow_actions
*sf_acts
;
223 struct dp_stats_percpu
*stats
;
228 stats
= this_cpu_ptr(dp
->stats_percpu
);
231 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, skb_get_hash(skb
),
233 if (unlikely(!flow
)) {
234 struct dp_upcall_info upcall
;
236 memset(&upcall
, 0, sizeof(upcall
));
237 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
238 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
239 upcall
.mru
= OVS_CB(skb
)->mru
;
240 error
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, 0);
245 stats_counter
= &stats
->n_missed
;
249 ovs_flow_stats_update(flow
, key
->tp
.flags
, skb
);
250 sf_acts
= rcu_dereference(flow
->sf_acts
);
251 error
= ovs_execute_actions(dp
, skb
, sf_acts
, key
);
253 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
254 ovs_dp_name(dp
), error
);
256 stats_counter
= &stats
->n_hit
;
259 /* Update datapath statistics. */
260 u64_stats_update_begin(&stats
->syncp
);
262 stats
->n_mask_hit
+= n_mask_hit
;
263 u64_stats_update_end(&stats
->syncp
);
266 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
267 const struct sw_flow_key
*key
,
268 const struct dp_upcall_info
*upcall_info
,
271 struct dp_stats_percpu
*stats
;
274 if (upcall_info
->portid
== 0) {
279 if (!skb_is_gso(skb
))
280 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
282 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
, cutlen
);
289 stats
= this_cpu_ptr(dp
->stats_percpu
);
291 u64_stats_update_begin(&stats
->syncp
);
293 u64_stats_update_end(&stats
->syncp
);
298 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
299 const struct sw_flow_key
*key
,
300 const struct dp_upcall_info
*upcall_info
,
303 unsigned int gso_type
= skb_shinfo(skb
)->gso_type
;
304 struct sw_flow_key later_key
;
305 struct sk_buff
*segs
, *nskb
;
308 BUILD_BUG_ON(sizeof(*OVS_CB(skb
)) > SKB_GSO_CB_OFFSET
);
309 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
311 return PTR_ERR(segs
);
315 if (gso_type
& SKB_GSO_UDP
) {
316 /* The initial flow key extracted by ovs_flow_key_extract()
317 * in this case is for a first fragment, so we need to
318 * properly mark later fragments.
321 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
324 /* Queue all of the segments. */
325 skb_list_walk_safe(segs
, skb
, nskb
) {
326 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
329 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
335 /* Free all of the segments. */
336 skb_list_walk_safe(segs
, skb
, nskb
) {
345 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
346 unsigned int hdrlen
, int actions_attrlen
)
348 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
349 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
350 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
351 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
352 + nla_total_size(sizeof(u64
)); /* OVS_PACKET_ATTR_HASH */
354 /* OVS_PACKET_ATTR_USERDATA */
355 if (upcall_info
->userdata
)
356 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
358 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
359 if (upcall_info
->egress_tun_info
)
360 size
+= nla_total_size(ovs_tun_key_attr_size());
362 /* OVS_PACKET_ATTR_ACTIONS */
363 if (upcall_info
->actions_len
)
364 size
+= nla_total_size(actions_attrlen
);
366 /* OVS_PACKET_ATTR_MRU */
367 if (upcall_info
->mru
)
368 size
+= nla_total_size(sizeof(upcall_info
->mru
));
373 static void pad_packet(struct datapath
*dp
, struct sk_buff
*skb
)
375 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
376 size_t plen
= NLA_ALIGN(skb
->len
) - skb
->len
;
379 skb_put_zero(skb
, plen
);
383 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
384 const struct sw_flow_key
*key
,
385 const struct dp_upcall_info
*upcall_info
,
388 struct ovs_header
*upcall
;
389 struct sk_buff
*nskb
= NULL
;
390 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
397 dp_ifindex
= get_dpifindex(dp
);
401 if (skb_vlan_tag_present(skb
)) {
402 nskb
= skb_clone(skb
, GFP_ATOMIC
);
406 nskb
= __vlan_hwaccel_push_inside(nskb
);
413 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
418 /* Complete checksum if needed */
419 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
420 (err
= skb_csum_hwoffload_help(skb
, 0)))
423 /* Older versions of OVS user space enforce alignment of the last
424 * Netlink attribute to NLA_ALIGNTO which would require extensive
425 * padding logic. Only perform zerocopy if padding is not required.
427 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
428 hlen
= skb_zerocopy_headlen(skb
);
432 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
433 OVS_CB(skb
)->acts_origlen
);
434 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
440 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
441 0, upcall_info
->cmd
);
446 upcall
->dp_ifindex
= dp_ifindex
;
448 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
452 if (upcall_info
->userdata
)
453 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
454 nla_len(upcall_info
->userdata
),
455 nla_data(upcall_info
->userdata
));
457 if (upcall_info
->egress_tun_info
) {
458 nla
= nla_nest_start_noflag(user_skb
,
459 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
464 err
= ovs_nla_put_tunnel_info(user_skb
,
465 upcall_info
->egress_tun_info
);
469 nla_nest_end(user_skb
, nla
);
472 if (upcall_info
->actions_len
) {
473 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
478 err
= ovs_nla_put_actions(upcall_info
->actions
,
479 upcall_info
->actions_len
,
482 nla_nest_end(user_skb
, nla
);
484 nla_nest_cancel(user_skb
, nla
);
487 /* Add OVS_PACKET_ATTR_MRU */
488 if (upcall_info
->mru
&&
489 nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
, upcall_info
->mru
)) {
494 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
496 nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
, skb
->len
)) {
501 /* Add OVS_PACKET_ATTR_HASH */
502 hash
= skb_get_hash_raw(skb
);
504 hash
|= OVS_PACKET_HASH_SW_BIT
;
507 hash
|= OVS_PACKET_HASH_L4_BIT
;
509 if (nla_put(user_skb
, OVS_PACKET_ATTR_HASH
, sizeof (u64
), &hash
)) {
514 /* Only reserve room for attribute header, packet data is added
515 * in skb_zerocopy() */
516 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
520 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
522 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
526 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
527 pad_packet(dp
, user_skb
);
529 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
531 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
541 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
543 struct ovs_header
*ovs_header
= info
->userhdr
;
544 struct net
*net
= sock_net(skb
->sk
);
545 struct nlattr
**a
= info
->attrs
;
546 struct sw_flow_actions
*acts
;
547 struct sk_buff
*packet
;
548 struct sw_flow
*flow
;
549 struct sw_flow_actions
*sf_acts
;
551 struct vport
*input_vport
;
556 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
559 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
560 !a
[OVS_PACKET_ATTR_ACTIONS
])
563 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
564 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
568 skb_reserve(packet
, NET_IP_ALIGN
);
570 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
572 /* Set packet's mru */
573 if (a
[OVS_PACKET_ATTR_MRU
]) {
574 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
575 packet
->ignore_df
= 1;
577 OVS_CB(packet
)->mru
= mru
;
579 if (a
[OVS_PACKET_ATTR_HASH
]) {
580 hash
= nla_get_u64(a
[OVS_PACKET_ATTR_HASH
]);
582 __skb_set_hash(packet
, hash
& 0xFFFFFFFFULL
,
583 !!(hash
& OVS_PACKET_HASH_SW_BIT
),
584 !!(hash
& OVS_PACKET_HASH_L4_BIT
));
587 /* Build an sw_flow for sending this packet. */
588 flow
= ovs_flow_alloc();
593 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
594 packet
, &flow
->key
, log
);
598 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
599 &flow
->key
, &acts
, log
);
603 rcu_assign_pointer(flow
->sf_acts
, acts
);
604 packet
->priority
= flow
->key
.phy
.priority
;
605 packet
->mark
= flow
->key
.phy
.skb_mark
;
608 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
613 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
615 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
620 packet
->dev
= input_vport
->dev
;
621 OVS_CB(packet
)->input_vport
= input_vport
;
622 sf_acts
= rcu_dereference(flow
->sf_acts
);
625 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
629 ovs_flow_free(flow
, false);
635 ovs_flow_free(flow
, false);
642 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
643 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
644 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
645 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
646 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
647 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
648 [OVS_PACKET_ATTR_HASH
] = { .type
= NLA_U64
},
651 static const struct genl_ops dp_packet_genl_ops
[] = {
652 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
653 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
654 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
655 .doit
= ovs_packet_cmd_execute
659 static struct genl_family dp_packet_genl_family __ro_after_init
= {
660 .hdrsize
= sizeof(struct ovs_header
),
661 .name
= OVS_PACKET_FAMILY
,
662 .version
= OVS_PACKET_VERSION
,
663 .maxattr
= OVS_PACKET_ATTR_MAX
,
664 .policy
= packet_policy
,
666 .parallel_ops
= true,
667 .ops
= dp_packet_genl_ops
,
668 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
669 .module
= THIS_MODULE
,
672 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
673 struct ovs_dp_megaflow_stats
*mega_stats
)
677 memset(mega_stats
, 0, sizeof(*mega_stats
));
679 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
680 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
682 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
684 for_each_possible_cpu(i
) {
685 const struct dp_stats_percpu
*percpu_stats
;
686 struct dp_stats_percpu local_stats
;
689 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
692 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
693 local_stats
= *percpu_stats
;
694 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
696 stats
->n_hit
+= local_stats
.n_hit
;
697 stats
->n_missed
+= local_stats
.n_missed
;
698 stats
->n_lost
+= local_stats
.n_lost
;
699 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
703 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
705 return ovs_identifier_is_ufid(sfid
) &&
706 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
709 static bool should_fill_mask(uint32_t ufid_flags
)
711 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
714 static bool should_fill_actions(uint32_t ufid_flags
)
716 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
719 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
720 const struct sw_flow_id
*sfid
,
723 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
725 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
726 * see ovs_nla_put_identifier()
728 if (sfid
&& ovs_identifier_is_ufid(sfid
))
729 len
+= nla_total_size(sfid
->ufid_len
);
731 len
+= nla_total_size(ovs_key_attr_size());
733 /* OVS_FLOW_ATTR_KEY */
734 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
735 len
+= nla_total_size(ovs_key_attr_size());
737 /* OVS_FLOW_ATTR_MASK */
738 if (should_fill_mask(ufid_flags
))
739 len
+= nla_total_size(ovs_key_attr_size());
741 /* OVS_FLOW_ATTR_ACTIONS */
742 if (should_fill_actions(ufid_flags
))
743 len
+= nla_total_size(acts
->orig_len
);
746 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
747 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
748 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
751 /* Called with ovs_mutex or RCU read lock. */
752 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
755 struct ovs_flow_stats stats
;
759 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
762 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
766 if (stats
.n_packets
&&
767 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
768 sizeof(struct ovs_flow_stats
), &stats
,
772 if ((u8
)ntohs(tcp_flags
) &&
773 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
779 /* Called with ovs_mutex or RCU read lock. */
780 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
781 struct sk_buff
*skb
, int skb_orig_len
)
783 struct nlattr
*start
;
786 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
787 * this is the first flow to be dumped into 'skb'. This is unusual for
788 * Netlink but individual action lists can be longer than
789 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
790 * The userspace caller can always fetch the actions separately if it
791 * really wants them. (Most userspace callers in fact don't care.)
793 * This can only fail for dump operations because the skb is always
794 * properly sized for single flows.
796 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
798 const struct sw_flow_actions
*sf_acts
;
800 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
801 err
= ovs_nla_put_actions(sf_acts
->actions
,
802 sf_acts
->actions_len
, skb
);
805 nla_nest_end(skb
, start
);
810 nla_nest_cancel(skb
, start
);
812 } else if (skb_orig_len
) {
819 /* Called with ovs_mutex or RCU read lock. */
820 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
821 struct sk_buff
*skb
, u32 portid
,
822 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
824 const int skb_orig_len
= skb
->len
;
825 struct ovs_header
*ovs_header
;
828 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
833 ovs_header
->dp_ifindex
= dp_ifindex
;
835 err
= ovs_nla_put_identifier(flow
, skb
);
839 if (should_fill_key(&flow
->id
, ufid_flags
)) {
840 err
= ovs_nla_put_masked_key(flow
, skb
);
845 if (should_fill_mask(ufid_flags
)) {
846 err
= ovs_nla_put_mask(flow
, skb
);
851 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
855 if (should_fill_actions(ufid_flags
)) {
856 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
861 genlmsg_end(skb
, ovs_header
);
865 genlmsg_cancel(skb
, ovs_header
);
869 /* May not be called with RCU read lock. */
870 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
871 const struct sw_flow_id
*sfid
,
872 struct genl_info
*info
,
879 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
, 0))
882 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
883 skb
= genlmsg_new(len
, GFP_KERNEL
);
885 return ERR_PTR(-ENOMEM
);
890 /* Called with ovs_mutex. */
891 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
893 struct genl_info
*info
, u8 cmd
,
894 bool always
, u32 ufid_flags
)
899 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
900 &flow
->id
, info
, always
, ufid_flags
);
901 if (IS_ERR_OR_NULL(skb
))
904 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
905 info
->snd_portid
, info
->snd_seq
, 0,
907 if (WARN_ON_ONCE(retval
< 0)) {
909 skb
= ERR_PTR(retval
);
914 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
916 struct net
*net
= sock_net(skb
->sk
);
917 struct nlattr
**a
= info
->attrs
;
918 struct ovs_header
*ovs_header
= info
->userhdr
;
919 struct sw_flow
*flow
= NULL
, *new_flow
;
920 struct sw_flow_mask mask
;
921 struct sk_buff
*reply
;
923 struct sw_flow_actions
*acts
;
924 struct sw_flow_match match
;
925 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
927 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
929 /* Must have key and actions. */
931 if (!a
[OVS_FLOW_ATTR_KEY
]) {
932 OVS_NLERR(log
, "Flow key attr not present in new flow.");
935 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
936 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
940 /* Most of the time we need to allocate a new flow, do it before
943 new_flow
= ovs_flow_alloc();
944 if (IS_ERR(new_flow
)) {
945 error
= PTR_ERR(new_flow
);
950 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
951 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
952 a
[OVS_FLOW_ATTR_MASK
], log
);
956 /* Extract flow identifier. */
957 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
958 &new_flow
->key
, log
);
962 /* unmasked key is needed to match when ufid is not used. */
963 if (ovs_identifier_is_key(&new_flow
->id
))
964 match
.key
= new_flow
->id
.unmasked_key
;
966 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
968 /* Validate actions. */
969 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
970 &new_flow
->key
, &acts
, log
);
972 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
976 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
979 error
= PTR_ERR(reply
);
984 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
990 /* Check if this is a duplicate flow */
991 if (ovs_identifier_is_ufid(&new_flow
->id
))
992 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
994 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
996 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
998 /* Put flow in bucket. */
999 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
1000 if (unlikely(error
)) {
1002 goto err_unlock_ovs
;
1005 if (unlikely(reply
)) {
1006 error
= ovs_flow_cmd_fill_info(new_flow
,
1007 ovs_header
->dp_ifindex
,
1008 reply
, info
->snd_portid
,
1016 struct sw_flow_actions
*old_acts
;
1018 /* Bail out if we're not allowed to modify an existing flow.
1019 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1020 * because Generic Netlink treats the latter as a dump
1021 * request. We also accept NLM_F_EXCL in case that bug ever
1024 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1027 goto err_unlock_ovs
;
1029 /* The flow identifier has to be the same for flow updates.
1030 * Look for any overlapping flow.
1032 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1033 if (ovs_identifier_is_key(&flow
->id
))
1034 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1036 else /* UFID matches but key is different */
1040 goto err_unlock_ovs
;
1043 /* Update actions. */
1044 old_acts
= ovsl_dereference(flow
->sf_acts
);
1045 rcu_assign_pointer(flow
->sf_acts
, acts
);
1047 if (unlikely(reply
)) {
1048 error
= ovs_flow_cmd_fill_info(flow
,
1049 ovs_header
->dp_ifindex
,
1050 reply
, info
->snd_portid
,
1058 ovs_nla_free_flow_actions_rcu(old_acts
);
1059 ovs_flow_free(new_flow
, false);
1063 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1070 ovs_nla_free_flow_actions(acts
);
1072 ovs_flow_free(new_flow
, false);
1077 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1078 static noinline_for_stack
struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1079 const struct nlattr
*a
,
1080 const struct sw_flow_key
*key
,
1081 const struct sw_flow_mask
*mask
,
1084 struct sw_flow_actions
*acts
;
1085 struct sw_flow_key masked_key
;
1088 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1089 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1092 "Actions may not be safe on all matching packets");
1093 return ERR_PTR(error
);
1099 /* Factor out match-init and action-copy to avoid
1100 * "Wframe-larger-than=1024" warning. Because mask is only
1101 * used to get actions, we new a function to save some
1104 * If there are not key and action attrs, we return 0
1105 * directly. In the case, the caller will also not use the
1106 * match as before. If there is action attr, we try to get
1107 * actions and save them to *acts. Before returning from
1108 * the function, we reset the match->mask pointer. Because
1109 * we should not to return match object with dangling reference
1112 static noinline_for_stack
int
1113 ovs_nla_init_match_and_action(struct net
*net
,
1114 struct sw_flow_match
*match
,
1115 struct sw_flow_key
*key
,
1117 struct sw_flow_actions
**acts
,
1120 struct sw_flow_mask mask
;
1123 if (a
[OVS_FLOW_ATTR_KEY
]) {
1124 ovs_match_init(match
, key
, true, &mask
);
1125 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1126 a
[OVS_FLOW_ATTR_MASK
], log
);
1131 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1132 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1134 "Flow key attribute not present in set flow.");
1139 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1141 if (IS_ERR(*acts
)) {
1142 error
= PTR_ERR(*acts
);
1147 /* On success, error is 0. */
1153 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1155 struct net
*net
= sock_net(skb
->sk
);
1156 struct nlattr
**a
= info
->attrs
;
1157 struct ovs_header
*ovs_header
= info
->userhdr
;
1158 struct sw_flow_key key
;
1159 struct sw_flow
*flow
;
1160 struct sk_buff
*reply
= NULL
;
1161 struct datapath
*dp
;
1162 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1163 struct sw_flow_match match
;
1164 struct sw_flow_id sfid
;
1165 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1167 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1170 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1171 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1173 "Flow set message rejected, Key attribute missing.");
1177 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1183 /* Can allocate before locking if have acts. */
1184 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1186 if (IS_ERR(reply
)) {
1187 error
= PTR_ERR(reply
);
1188 goto err_kfree_acts
;
1193 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1194 if (unlikely(!dp
)) {
1196 goto err_unlock_ovs
;
1198 /* Check that the flow exists. */
1200 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1202 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1203 if (unlikely(!flow
)) {
1205 goto err_unlock_ovs
;
1208 /* Update actions, if present. */
1210 old_acts
= ovsl_dereference(flow
->sf_acts
);
1211 rcu_assign_pointer(flow
->sf_acts
, acts
);
1213 if (unlikely(reply
)) {
1214 error
= ovs_flow_cmd_fill_info(flow
,
1215 ovs_header
->dp_ifindex
,
1216 reply
, info
->snd_portid
,
1223 /* Could not alloc without acts before locking. */
1224 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1225 info
, OVS_FLOW_CMD_SET
, false,
1228 if (IS_ERR(reply
)) {
1229 error
= PTR_ERR(reply
);
1230 goto err_unlock_ovs
;
1235 if (a
[OVS_FLOW_ATTR_CLEAR
])
1236 ovs_flow_stats_clear(flow
);
1240 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1242 ovs_nla_free_flow_actions_rcu(old_acts
);
1250 ovs_nla_free_flow_actions(acts
);
1255 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1257 struct nlattr
**a
= info
->attrs
;
1258 struct ovs_header
*ovs_header
= info
->userhdr
;
1259 struct net
*net
= sock_net(skb
->sk
);
1260 struct sw_flow_key key
;
1261 struct sk_buff
*reply
;
1262 struct sw_flow
*flow
;
1263 struct datapath
*dp
;
1264 struct sw_flow_match match
;
1265 struct sw_flow_id ufid
;
1266 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1268 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1271 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1272 if (a
[OVS_FLOW_ATTR_KEY
]) {
1273 ovs_match_init(&match
, &key
, true, NULL
);
1274 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1276 } else if (!ufid_present
) {
1278 "Flow get message rejected, Key attribute missing.");
1285 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1292 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1294 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1300 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1301 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1302 if (IS_ERR(reply
)) {
1303 err
= PTR_ERR(reply
);
1308 return genlmsg_reply(reply
, info
);
1314 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1316 struct nlattr
**a
= info
->attrs
;
1317 struct ovs_header
*ovs_header
= info
->userhdr
;
1318 struct net
*net
= sock_net(skb
->sk
);
1319 struct sw_flow_key key
;
1320 struct sk_buff
*reply
;
1321 struct sw_flow
*flow
= NULL
;
1322 struct datapath
*dp
;
1323 struct sw_flow_match match
;
1324 struct sw_flow_id ufid
;
1325 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1327 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1330 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1331 if (a
[OVS_FLOW_ATTR_KEY
]) {
1332 ovs_match_init(&match
, &key
, true, NULL
);
1333 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1340 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1341 if (unlikely(!dp
)) {
1346 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1347 err
= ovs_flow_tbl_flush(&dp
->table
);
1352 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1354 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1355 if (unlikely(!flow
)) {
1360 ovs_flow_tbl_remove(&dp
->table
, flow
);
1363 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1364 &flow
->id
, info
, false, ufid_flags
);
1365 if (likely(reply
)) {
1366 if (!IS_ERR(reply
)) {
1367 rcu_read_lock(); /*To keep RCU checker happy. */
1368 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1369 reply
, info
->snd_portid
,
1374 if (WARN_ON_ONCE(err
< 0)) {
1379 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1381 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1386 ovs_flow_free(flow
, true);
1393 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1395 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1396 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1397 struct table_instance
*ti
;
1398 struct datapath
*dp
;
1402 err
= genlmsg_parse_deprecated(cb
->nlh
, &dp_flow_genl_family
, a
,
1403 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1406 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1409 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1415 ti
= rcu_dereference(dp
->table
.ti
);
1417 struct sw_flow
*flow
;
1420 bucket
= cb
->args
[0];
1422 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1426 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1427 NETLINK_CB(cb
->skb
).portid
,
1428 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1429 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1432 cb
->args
[0] = bucket
;
1439 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1440 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1441 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1442 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1443 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1444 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1445 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1446 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1449 static const struct genl_ops dp_flow_genl_ops
[] = {
1450 { .cmd
= OVS_FLOW_CMD_NEW
,
1451 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1452 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1453 .doit
= ovs_flow_cmd_new
1455 { .cmd
= OVS_FLOW_CMD_DEL
,
1456 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1457 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1458 .doit
= ovs_flow_cmd_del
1460 { .cmd
= OVS_FLOW_CMD_GET
,
1461 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1462 .flags
= 0, /* OK for unprivileged users. */
1463 .doit
= ovs_flow_cmd_get
,
1464 .dumpit
= ovs_flow_cmd_dump
1466 { .cmd
= OVS_FLOW_CMD_SET
,
1467 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1468 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1469 .doit
= ovs_flow_cmd_set
,
1473 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1474 .hdrsize
= sizeof(struct ovs_header
),
1475 .name
= OVS_FLOW_FAMILY
,
1476 .version
= OVS_FLOW_VERSION
,
1477 .maxattr
= OVS_FLOW_ATTR_MAX
,
1478 .policy
= flow_policy
,
1480 .parallel_ops
= true,
1481 .ops
= dp_flow_genl_ops
,
1482 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1483 .mcgrps
= &ovs_dp_flow_multicast_group
,
1485 .module
= THIS_MODULE
,
1488 static size_t ovs_dp_cmd_msg_size(void)
1490 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1492 msgsize
+= nla_total_size(IFNAMSIZ
);
1493 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1494 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1495 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1500 /* Called with ovs_mutex. */
1501 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1502 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1504 struct ovs_header
*ovs_header
;
1505 struct ovs_dp_stats dp_stats
;
1506 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1509 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1514 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1516 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1518 goto nla_put_failure
;
1520 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1521 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1522 &dp_stats
, OVS_DP_ATTR_PAD
))
1523 goto nla_put_failure
;
1525 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1526 sizeof(struct ovs_dp_megaflow_stats
),
1527 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1528 goto nla_put_failure
;
1530 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1531 goto nla_put_failure
;
1533 genlmsg_end(skb
, ovs_header
);
1537 genlmsg_cancel(skb
, ovs_header
);
1542 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1544 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1547 /* Called with rcu_read_lock or ovs_mutex. */
1548 static struct datapath
*lookup_datapath(struct net
*net
,
1549 const struct ovs_header
*ovs_header
,
1550 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1552 struct datapath
*dp
;
1554 if (!a
[OVS_DP_ATTR_NAME
])
1555 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1557 struct vport
*vport
;
1559 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1560 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1562 return dp
? dp
: ERR_PTR(-ENODEV
);
1565 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1567 struct datapath
*dp
;
1569 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1573 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1574 dp
->user_features
= 0;
1577 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support
);
1579 static int ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1581 u32 user_features
= 0;
1583 if (a
[OVS_DP_ATTR_USER_FEATURES
]) {
1584 user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1586 if (user_features
& ~(OVS_DP_F_VPORT_PIDS
|
1587 OVS_DP_F_UNALIGNED
|
1588 OVS_DP_F_TC_RECIRC_SHARING
))
1591 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1592 if (user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1597 dp
->user_features
= user_features
;
1599 if (dp
->user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1600 static_branch_enable(&tc_recirc_sharing_support
);
1602 static_branch_disable(&tc_recirc_sharing_support
);
1607 static int ovs_dp_stats_init(struct datapath
*dp
)
1609 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1610 if (!dp
->stats_percpu
)
1616 static int ovs_dp_vport_init(struct datapath
*dp
)
1620 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1621 sizeof(struct hlist_head
),
1626 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1627 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1632 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1634 struct nlattr
**a
= info
->attrs
;
1635 struct vport_parms parms
;
1636 struct sk_buff
*reply
;
1637 struct datapath
*dp
;
1638 struct vport
*vport
;
1639 struct ovs_net
*ovs_net
;
1643 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1646 reply
= ovs_dp_cmd_alloc_info();
1651 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1653 goto err_destroy_reply
;
1655 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1657 /* Allocate table. */
1658 err
= ovs_flow_tbl_init(&dp
->table
);
1660 goto err_destroy_dp
;
1662 err
= ovs_dp_stats_init(dp
);
1664 goto err_destroy_table
;
1666 err
= ovs_dp_vport_init(dp
);
1668 goto err_destroy_stats
;
1670 err
= ovs_meters_init(dp
);
1672 goto err_destroy_ports
;
1674 /* Set up our datapath device. */
1675 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1676 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1677 parms
.options
= NULL
;
1679 parms
.port_no
= OVSP_LOCAL
;
1680 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1682 err
= ovs_dp_change(dp
, a
);
1684 goto err_destroy_meters
;
1686 /* So far only local changes have been made, now need the lock. */
1689 vport
= new_vport(&parms
);
1690 if (IS_ERR(vport
)) {
1691 err
= PTR_ERR(vport
);
1695 if (err
== -EEXIST
) {
1696 /* An outdated user space instance that does not understand
1697 * the concept of user_features has attempted to create a new
1698 * datapath and is likely to reuse it. Drop all user features.
1700 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1701 ovs_dp_reset_user_features(skb
, info
);
1705 goto err_destroy_meters
;
1708 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1709 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1712 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1713 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1717 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1721 ovs_meters_exit(dp
);
1725 free_percpu(dp
->stats_percpu
);
1727 ovs_flow_tbl_destroy(&dp
->table
);
1736 /* Called with ovs_mutex. */
1737 static void __dp_destroy(struct datapath
*dp
)
1741 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1742 struct vport
*vport
;
1743 struct hlist_node
*n
;
1745 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1746 if (vport
->port_no
!= OVSP_LOCAL
)
1747 ovs_dp_detach_port(vport
);
1750 list_del_rcu(&dp
->list_node
);
1752 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1753 * all ports in datapath are destroyed first before freeing datapath.
1755 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1757 /* RCU destroy the flow table */
1758 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1761 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1763 struct sk_buff
*reply
;
1764 struct datapath
*dp
;
1767 reply
= ovs_dp_cmd_alloc_info();
1772 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1775 goto err_unlock_free
;
1777 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1778 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1784 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1794 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1796 struct sk_buff
*reply
;
1797 struct datapath
*dp
;
1800 reply
= ovs_dp_cmd_alloc_info();
1805 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1808 goto err_unlock_free
;
1810 err
= ovs_dp_change(dp
, info
->attrs
);
1812 goto err_unlock_free
;
1814 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1815 info
->snd_seq
, 0, OVS_DP_CMD_SET
);
1819 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1829 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1831 struct sk_buff
*reply
;
1832 struct datapath
*dp
;
1835 reply
= ovs_dp_cmd_alloc_info();
1840 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1843 goto err_unlock_free
;
1845 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1846 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1850 return genlmsg_reply(reply
, info
);
1858 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1860 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1861 struct datapath
*dp
;
1862 int skip
= cb
->args
[0];
1866 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1868 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1869 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1870 OVS_DP_CMD_GET
) < 0)
1881 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1882 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1883 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1884 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1887 static const struct genl_ops dp_datapath_genl_ops
[] = {
1888 { .cmd
= OVS_DP_CMD_NEW
,
1889 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1890 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1891 .doit
= ovs_dp_cmd_new
1893 { .cmd
= OVS_DP_CMD_DEL
,
1894 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1895 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1896 .doit
= ovs_dp_cmd_del
1898 { .cmd
= OVS_DP_CMD_GET
,
1899 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1900 .flags
= 0, /* OK for unprivileged users. */
1901 .doit
= ovs_dp_cmd_get
,
1902 .dumpit
= ovs_dp_cmd_dump
1904 { .cmd
= OVS_DP_CMD_SET
,
1905 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1906 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1907 .doit
= ovs_dp_cmd_set
,
1911 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
1912 .hdrsize
= sizeof(struct ovs_header
),
1913 .name
= OVS_DATAPATH_FAMILY
,
1914 .version
= OVS_DATAPATH_VERSION
,
1915 .maxattr
= OVS_DP_ATTR_MAX
,
1916 .policy
= datapath_policy
,
1918 .parallel_ops
= true,
1919 .ops
= dp_datapath_genl_ops
,
1920 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1921 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1923 .module
= THIS_MODULE
,
1926 /* Called with ovs_mutex or RCU read lock. */
1927 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1928 struct net
*net
, u32 portid
, u32 seq
,
1929 u32 flags
, u8 cmd
, gfp_t gfp
)
1931 struct ovs_header
*ovs_header
;
1932 struct ovs_vport_stats vport_stats
;
1935 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1940 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1942 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1943 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1944 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1945 ovs_vport_name(vport
)) ||
1946 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
1947 goto nla_put_failure
;
1949 if (!net_eq(net
, dev_net(vport
->dev
))) {
1950 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
), gfp
);
1952 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
1953 goto nla_put_failure
;
1956 ovs_vport_get_stats(vport
, &vport_stats
);
1957 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
1958 sizeof(struct ovs_vport_stats
), &vport_stats
,
1959 OVS_VPORT_ATTR_PAD
))
1960 goto nla_put_failure
;
1962 if (ovs_vport_get_upcall_portids(vport
, skb
))
1963 goto nla_put_failure
;
1965 err
= ovs_vport_get_options(vport
, skb
);
1966 if (err
== -EMSGSIZE
)
1969 genlmsg_end(skb
, ovs_header
);
1975 genlmsg_cancel(skb
, ovs_header
);
1979 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1981 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1984 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1985 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
1986 u32 portid
, u32 seq
, u8 cmd
)
1988 struct sk_buff
*skb
;
1991 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1993 return ERR_PTR(-ENOMEM
);
1995 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
,
2002 /* Called with ovs_mutex or RCU read lock. */
2003 static struct vport
*lookup_vport(struct net
*net
,
2004 const struct ovs_header
*ovs_header
,
2005 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
2007 struct datapath
*dp
;
2008 struct vport
*vport
;
2010 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2011 return ERR_PTR(-EOPNOTSUPP
);
2012 if (a
[OVS_VPORT_ATTR_NAME
]) {
2013 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
2015 return ERR_PTR(-ENODEV
);
2016 if (ovs_header
->dp_ifindex
&&
2017 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
2018 return ERR_PTR(-ENODEV
);
2020 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
2021 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
2023 if (port_no
>= DP_MAX_PORTS
)
2024 return ERR_PTR(-EFBIG
);
2026 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
2028 return ERR_PTR(-ENODEV
);
2030 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
2032 return ERR_PTR(-ENODEV
);
2035 return ERR_PTR(-EINVAL
);
2039 static unsigned int ovs_get_max_headroom(struct datapath
*dp
)
2041 unsigned int dev_headroom
, max_headroom
= 0;
2042 struct net_device
*dev
;
2043 struct vport
*vport
;
2046 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2047 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
,
2048 lockdep_ovsl_is_held()) {
2050 dev_headroom
= netdev_get_fwd_headroom(dev
);
2051 if (dev_headroom
> max_headroom
)
2052 max_headroom
= dev_headroom
;
2056 return max_headroom
;
2059 /* Called with ovs_mutex */
2060 static void ovs_update_headroom(struct datapath
*dp
, unsigned int new_headroom
)
2062 struct vport
*vport
;
2065 dp
->max_headroom
= new_headroom
;
2066 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
2067 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
,
2068 lockdep_ovsl_is_held())
2069 netdev_set_rx_headroom(vport
->dev
, new_headroom
);
2072 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2074 struct nlattr
**a
= info
->attrs
;
2075 struct ovs_header
*ovs_header
= info
->userhdr
;
2076 struct vport_parms parms
;
2077 struct sk_buff
*reply
;
2078 struct vport
*vport
;
2079 struct datapath
*dp
;
2080 unsigned int new_headroom
;
2084 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2085 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2087 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2090 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2091 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2092 if (port_no
>= DP_MAX_PORTS
)
2095 reply
= ovs_vport_cmd_alloc_info();
2101 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2104 goto exit_unlock_free
;
2107 vport
= ovs_vport_ovsl(dp
, port_no
);
2110 goto exit_unlock_free
;
2112 for (port_no
= 1; ; port_no
++) {
2113 if (port_no
>= DP_MAX_PORTS
) {
2115 goto exit_unlock_free
;
2117 vport
= ovs_vport_ovsl(dp
, port_no
);
2123 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2124 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2125 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2127 parms
.port_no
= port_no
;
2128 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2130 vport
= new_vport(&parms
);
2131 err
= PTR_ERR(vport
);
2132 if (IS_ERR(vport
)) {
2135 goto exit_unlock_free
;
2138 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2139 info
->snd_portid
, info
->snd_seq
, 0,
2140 OVS_VPORT_CMD_NEW
, GFP_KERNEL
);
2142 new_headroom
= netdev_get_fwd_headroom(vport
->dev
);
2144 if (new_headroom
> dp
->max_headroom
)
2145 ovs_update_headroom(dp
, new_headroom
);
2147 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2152 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2161 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2163 struct nlattr
**a
= info
->attrs
;
2164 struct sk_buff
*reply
;
2165 struct vport
*vport
;
2168 reply
= ovs_vport_cmd_alloc_info();
2173 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2174 err
= PTR_ERR(vport
);
2176 goto exit_unlock_free
;
2178 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2179 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2181 goto exit_unlock_free
;
2184 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2185 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2187 goto exit_unlock_free
;
2191 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2192 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2194 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2196 goto exit_unlock_free
;
2199 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2200 info
->snd_portid
, info
->snd_seq
, 0,
2201 OVS_VPORT_CMD_SET
, GFP_KERNEL
);
2205 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2214 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2216 bool update_headroom
= false;
2217 struct nlattr
**a
= info
->attrs
;
2218 struct sk_buff
*reply
;
2219 struct datapath
*dp
;
2220 struct vport
*vport
;
2221 unsigned int new_headroom
;
2224 reply
= ovs_vport_cmd_alloc_info();
2229 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2230 err
= PTR_ERR(vport
);
2232 goto exit_unlock_free
;
2234 if (vport
->port_no
== OVSP_LOCAL
) {
2236 goto exit_unlock_free
;
2239 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2240 info
->snd_portid
, info
->snd_seq
, 0,
2241 OVS_VPORT_CMD_DEL
, GFP_KERNEL
);
2244 /* the vport deletion may trigger dp headroom update */
2246 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2247 update_headroom
= true;
2249 netdev_reset_rx_headroom(vport
->dev
);
2250 ovs_dp_detach_port(vport
);
2252 if (update_headroom
) {
2253 new_headroom
= ovs_get_max_headroom(dp
);
2255 if (new_headroom
< dp
->max_headroom
)
2256 ovs_update_headroom(dp
, new_headroom
);
2260 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2269 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2271 struct nlattr
**a
= info
->attrs
;
2272 struct ovs_header
*ovs_header
= info
->userhdr
;
2273 struct sk_buff
*reply
;
2274 struct vport
*vport
;
2277 reply
= ovs_vport_cmd_alloc_info();
2282 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2283 err
= PTR_ERR(vport
);
2285 goto exit_unlock_free
;
2286 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2287 info
->snd_portid
, info
->snd_seq
, 0,
2288 OVS_VPORT_CMD_GET
, GFP_ATOMIC
);
2292 return genlmsg_reply(reply
, info
);
2300 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2302 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2303 struct datapath
*dp
;
2304 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2308 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2313 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2314 struct vport
*vport
;
2317 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2319 ovs_vport_cmd_fill_info(vport
, skb
,
2321 NETLINK_CB(cb
->skb
).portid
,
2341 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2342 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2343 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2344 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2345 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2346 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2347 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2348 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2349 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2352 static const struct genl_ops dp_vport_genl_ops
[] = {
2353 { .cmd
= OVS_VPORT_CMD_NEW
,
2354 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2355 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2356 .doit
= ovs_vport_cmd_new
2358 { .cmd
= OVS_VPORT_CMD_DEL
,
2359 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2360 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2361 .doit
= ovs_vport_cmd_del
2363 { .cmd
= OVS_VPORT_CMD_GET
,
2364 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2365 .flags
= 0, /* OK for unprivileged users. */
2366 .doit
= ovs_vport_cmd_get
,
2367 .dumpit
= ovs_vport_cmd_dump
2369 { .cmd
= OVS_VPORT_CMD_SET
,
2370 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2371 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2372 .doit
= ovs_vport_cmd_set
,
2376 struct genl_family dp_vport_genl_family __ro_after_init
= {
2377 .hdrsize
= sizeof(struct ovs_header
),
2378 .name
= OVS_VPORT_FAMILY
,
2379 .version
= OVS_VPORT_VERSION
,
2380 .maxattr
= OVS_VPORT_ATTR_MAX
,
2381 .policy
= vport_policy
,
2383 .parallel_ops
= true,
2384 .ops
= dp_vport_genl_ops
,
2385 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2386 .mcgrps
= &ovs_dp_vport_multicast_group
,
2388 .module
= THIS_MODULE
,
2391 static struct genl_family
* const dp_genl_families
[] = {
2392 &dp_datapath_genl_family
,
2393 &dp_vport_genl_family
,
2394 &dp_flow_genl_family
,
2395 &dp_packet_genl_family
,
2396 &dp_meter_genl_family
,
2397 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2398 &dp_ct_limit_genl_family
,
2402 static void dp_unregister_genl(int n_families
)
2406 for (i
= 0; i
< n_families
; i
++)
2407 genl_unregister_family(dp_genl_families
[i
]);
2410 static int __init
dp_register_genl(void)
2415 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2417 err
= genl_register_family(dp_genl_families
[i
]);
2425 dp_unregister_genl(i
);
2429 static int __net_init
ovs_init_net(struct net
*net
)
2431 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2433 INIT_LIST_HEAD(&ovs_net
->dps
);
2434 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2435 return ovs_ct_init(net
);
2438 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2439 struct list_head
*head
)
2441 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2442 struct datapath
*dp
;
2444 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2447 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2448 struct vport
*vport
;
2450 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2451 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2454 if (dev_net(vport
->dev
) == dnet
)
2455 list_add(&vport
->detach_list
, head
);
2461 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2463 struct datapath
*dp
, *dp_next
;
2464 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2465 struct vport
*vport
, *vport_next
;
2473 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2476 down_read(&net_rwsem
);
2478 list_vports_from_net(net
, dnet
, &head
);
2479 up_read(&net_rwsem
);
2481 /* Detach all vports from given namespace. */
2482 list_for_each_entry_safe(vport
, vport_next
, &head
, detach_list
) {
2483 list_del(&vport
->detach_list
);
2484 ovs_dp_detach_port(vport
);
2489 cancel_work_sync(&ovs_net
->dp_notify_work
);
2492 static struct pernet_operations ovs_net_ops
= {
2493 .init
= ovs_init_net
,
2494 .exit
= ovs_exit_net
,
2496 .size
= sizeof(struct ovs_net
),
2499 static int __init
dp_init(void)
2503 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > sizeof_field(struct sk_buff
, cb
));
2505 pr_info("Open vSwitch switching datapath\n");
2507 err
= action_fifos_init();
2511 err
= ovs_internal_dev_rtnl_link_register();
2513 goto error_action_fifos_exit
;
2515 err
= ovs_flow_init();
2517 goto error_unreg_rtnl_link
;
2519 err
= ovs_vport_init();
2521 goto error_flow_exit
;
2523 err
= register_pernet_device(&ovs_net_ops
);
2525 goto error_vport_exit
;
2527 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2529 goto error_netns_exit
;
2531 err
= ovs_netdev_init();
2533 goto error_unreg_notifier
;
2535 err
= dp_register_genl();
2537 goto error_unreg_netdev
;
2543 error_unreg_notifier
:
2544 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2546 unregister_pernet_device(&ovs_net_ops
);
2551 error_unreg_rtnl_link
:
2552 ovs_internal_dev_rtnl_link_unregister();
2553 error_action_fifos_exit
:
2554 action_fifos_exit();
2559 static void dp_cleanup(void)
2561 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2563 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2564 unregister_pernet_device(&ovs_net_ops
);
2568 ovs_internal_dev_rtnl_link_unregister();
2569 action_fifos_exit();
2572 module_init(dp_init
);
2573 module_exit(dp_cleanup
);
2575 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2576 MODULE_LICENSE("GPL");
2577 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY
);
2578 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY
);
2579 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY
);
2580 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY
);
2581 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY
);
2582 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY
);