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 if (vport
->port_no
== port_no
)
189 /* Called with ovs_mutex. */
190 static struct vport
*new_vport(const struct vport_parms
*parms
)
194 vport
= ovs_vport_add(parms
);
195 if (!IS_ERR(vport
)) {
196 struct datapath
*dp
= parms
->dp
;
197 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
199 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
204 void ovs_dp_detach_port(struct vport
*p
)
208 /* First drop references to device. */
209 hlist_del_rcu(&p
->dp_hash_node
);
211 /* Then destroy it. */
215 /* Must be called with rcu_read_lock. */
216 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
218 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
219 struct datapath
*dp
= p
->dp
;
220 struct sw_flow
*flow
;
221 struct sw_flow_actions
*sf_acts
;
222 struct dp_stats_percpu
*stats
;
227 stats
= this_cpu_ptr(dp
->stats_percpu
);
230 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, &n_mask_hit
);
231 if (unlikely(!flow
)) {
232 struct dp_upcall_info upcall
;
234 memset(&upcall
, 0, sizeof(upcall
));
235 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
236 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
237 upcall
.mru
= OVS_CB(skb
)->mru
;
238 error
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, 0);
243 stats_counter
= &stats
->n_missed
;
247 ovs_flow_stats_update(flow
, key
->tp
.flags
, skb
);
248 sf_acts
= rcu_dereference(flow
->sf_acts
);
249 error
= ovs_execute_actions(dp
, skb
, sf_acts
, key
);
251 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
252 ovs_dp_name(dp
), error
);
254 stats_counter
= &stats
->n_hit
;
257 /* Update datapath statistics. */
258 u64_stats_update_begin(&stats
->syncp
);
260 stats
->n_mask_hit
+= n_mask_hit
;
261 u64_stats_update_end(&stats
->syncp
);
264 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
265 const struct sw_flow_key
*key
,
266 const struct dp_upcall_info
*upcall_info
,
269 struct dp_stats_percpu
*stats
;
272 if (upcall_info
->portid
== 0) {
277 if (!skb_is_gso(skb
))
278 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
280 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
, cutlen
);
287 stats
= this_cpu_ptr(dp
->stats_percpu
);
289 u64_stats_update_begin(&stats
->syncp
);
291 u64_stats_update_end(&stats
->syncp
);
296 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
297 const struct sw_flow_key
*key
,
298 const struct dp_upcall_info
*upcall_info
,
301 unsigned int gso_type
= skb_shinfo(skb
)->gso_type
;
302 struct sw_flow_key later_key
;
303 struct sk_buff
*segs
, *nskb
;
306 BUILD_BUG_ON(sizeof(*OVS_CB(skb
)) > SKB_SGO_CB_OFFSET
);
307 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
309 return PTR_ERR(segs
);
313 if (gso_type
& SKB_GSO_UDP
) {
314 /* The initial flow key extracted by ovs_flow_key_extract()
315 * in this case is for a first fragment, so we need to
316 * properly mark later fragments.
319 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
322 /* Queue all of the segments. */
325 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
328 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
332 } while ((skb
= skb
->next
));
334 /* Free all of the segments. */
342 } while ((skb
= nskb
));
346 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
347 unsigned int hdrlen
, int actions_attrlen
)
349 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
350 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
351 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
352 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
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 */
396 dp_ifindex
= get_dpifindex(dp
);
400 if (skb_vlan_tag_present(skb
)) {
401 nskb
= skb_clone(skb
, GFP_ATOMIC
);
405 nskb
= __vlan_hwaccel_push_inside(nskb
);
412 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
417 /* Complete checksum if needed */
418 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
419 (err
= skb_csum_hwoffload_help(skb
, 0)))
422 /* Older versions of OVS user space enforce alignment of the last
423 * Netlink attribute to NLA_ALIGNTO which would require extensive
424 * padding logic. Only perform zerocopy if padding is not required.
426 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
427 hlen
= skb_zerocopy_headlen(skb
);
431 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
432 OVS_CB(skb
)->acts_origlen
);
433 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
439 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
440 0, upcall_info
->cmd
);
445 upcall
->dp_ifindex
= dp_ifindex
;
447 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
451 if (upcall_info
->userdata
)
452 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
453 nla_len(upcall_info
->userdata
),
454 nla_data(upcall_info
->userdata
));
456 if (upcall_info
->egress_tun_info
) {
457 nla
= nla_nest_start_noflag(user_skb
,
458 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
463 err
= ovs_nla_put_tunnel_info(user_skb
,
464 upcall_info
->egress_tun_info
);
468 nla_nest_end(user_skb
, nla
);
471 if (upcall_info
->actions_len
) {
472 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
477 err
= ovs_nla_put_actions(upcall_info
->actions
,
478 upcall_info
->actions_len
,
481 nla_nest_end(user_skb
, nla
);
483 nla_nest_cancel(user_skb
, nla
);
486 /* Add OVS_PACKET_ATTR_MRU */
487 if (upcall_info
->mru
) {
488 if (nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
,
493 pad_packet(dp
, user_skb
);
496 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
498 if (nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
,
503 pad_packet(dp
, user_skb
);
506 /* Only reserve room for attribute header, packet data is added
507 * in skb_zerocopy() */
508 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
512 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
514 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
518 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
519 pad_packet(dp
, user_skb
);
521 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
523 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
533 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
535 struct ovs_header
*ovs_header
= info
->userhdr
;
536 struct net
*net
= sock_net(skb
->sk
);
537 struct nlattr
**a
= info
->attrs
;
538 struct sw_flow_actions
*acts
;
539 struct sk_buff
*packet
;
540 struct sw_flow
*flow
;
541 struct sw_flow_actions
*sf_acts
;
543 struct vport
*input_vport
;
547 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
550 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
551 !a
[OVS_PACKET_ATTR_ACTIONS
])
554 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
555 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
559 skb_reserve(packet
, NET_IP_ALIGN
);
561 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
563 /* Set packet's mru */
564 if (a
[OVS_PACKET_ATTR_MRU
]) {
565 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
566 packet
->ignore_df
= 1;
568 OVS_CB(packet
)->mru
= mru
;
570 /* Build an sw_flow for sending this packet. */
571 flow
= ovs_flow_alloc();
576 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
577 packet
, &flow
->key
, log
);
581 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
582 &flow
->key
, &acts
, log
);
586 rcu_assign_pointer(flow
->sf_acts
, acts
);
587 packet
->priority
= flow
->key
.phy
.priority
;
588 packet
->mark
= flow
->key
.phy
.skb_mark
;
591 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
596 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
598 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
603 packet
->dev
= input_vport
->dev
;
604 OVS_CB(packet
)->input_vport
= input_vport
;
605 sf_acts
= rcu_dereference(flow
->sf_acts
);
608 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
612 ovs_flow_free(flow
, false);
618 ovs_flow_free(flow
, false);
625 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
626 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
627 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
628 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
629 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
630 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
633 static const struct genl_ops dp_packet_genl_ops
[] = {
634 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
635 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
636 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
637 .doit
= ovs_packet_cmd_execute
641 static struct genl_family dp_packet_genl_family __ro_after_init
= {
642 .hdrsize
= sizeof(struct ovs_header
),
643 .name
= OVS_PACKET_FAMILY
,
644 .version
= OVS_PACKET_VERSION
,
645 .maxattr
= OVS_PACKET_ATTR_MAX
,
646 .policy
= packet_policy
,
648 .parallel_ops
= true,
649 .ops
= dp_packet_genl_ops
,
650 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
651 .module
= THIS_MODULE
,
654 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
655 struct ovs_dp_megaflow_stats
*mega_stats
)
659 memset(mega_stats
, 0, sizeof(*mega_stats
));
661 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
662 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
664 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
666 for_each_possible_cpu(i
) {
667 const struct dp_stats_percpu
*percpu_stats
;
668 struct dp_stats_percpu local_stats
;
671 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
674 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
675 local_stats
= *percpu_stats
;
676 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
678 stats
->n_hit
+= local_stats
.n_hit
;
679 stats
->n_missed
+= local_stats
.n_missed
;
680 stats
->n_lost
+= local_stats
.n_lost
;
681 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
685 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
687 return ovs_identifier_is_ufid(sfid
) &&
688 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
691 static bool should_fill_mask(uint32_t ufid_flags
)
693 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
696 static bool should_fill_actions(uint32_t ufid_flags
)
698 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
701 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
702 const struct sw_flow_id
*sfid
,
705 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
707 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
708 * see ovs_nla_put_identifier()
710 if (sfid
&& ovs_identifier_is_ufid(sfid
))
711 len
+= nla_total_size(sfid
->ufid_len
);
713 len
+= nla_total_size(ovs_key_attr_size());
715 /* OVS_FLOW_ATTR_KEY */
716 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
717 len
+= nla_total_size(ovs_key_attr_size());
719 /* OVS_FLOW_ATTR_MASK */
720 if (should_fill_mask(ufid_flags
))
721 len
+= nla_total_size(ovs_key_attr_size());
723 /* OVS_FLOW_ATTR_ACTIONS */
724 if (should_fill_actions(ufid_flags
))
725 len
+= nla_total_size(acts
->orig_len
);
728 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
729 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
730 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
733 /* Called with ovs_mutex or RCU read lock. */
734 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
737 struct ovs_flow_stats stats
;
741 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
744 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
748 if (stats
.n_packets
&&
749 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
750 sizeof(struct ovs_flow_stats
), &stats
,
754 if ((u8
)ntohs(tcp_flags
) &&
755 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
761 /* Called with ovs_mutex or RCU read lock. */
762 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
763 struct sk_buff
*skb
, int skb_orig_len
)
765 struct nlattr
*start
;
768 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
769 * this is the first flow to be dumped into 'skb'. This is unusual for
770 * Netlink but individual action lists can be longer than
771 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
772 * The userspace caller can always fetch the actions separately if it
773 * really wants them. (Most userspace callers in fact don't care.)
775 * This can only fail for dump operations because the skb is always
776 * properly sized for single flows.
778 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
780 const struct sw_flow_actions
*sf_acts
;
782 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
783 err
= ovs_nla_put_actions(sf_acts
->actions
,
784 sf_acts
->actions_len
, skb
);
787 nla_nest_end(skb
, start
);
792 nla_nest_cancel(skb
, start
);
794 } else if (skb_orig_len
) {
801 /* Called with ovs_mutex or RCU read lock. */
802 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
803 struct sk_buff
*skb
, u32 portid
,
804 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
806 const int skb_orig_len
= skb
->len
;
807 struct ovs_header
*ovs_header
;
810 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
815 ovs_header
->dp_ifindex
= dp_ifindex
;
817 err
= ovs_nla_put_identifier(flow
, skb
);
821 if (should_fill_key(&flow
->id
, ufid_flags
)) {
822 err
= ovs_nla_put_masked_key(flow
, skb
);
827 if (should_fill_mask(ufid_flags
)) {
828 err
= ovs_nla_put_mask(flow
, skb
);
833 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
837 if (should_fill_actions(ufid_flags
)) {
838 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
843 genlmsg_end(skb
, ovs_header
);
847 genlmsg_cancel(skb
, ovs_header
);
851 /* May not be called with RCU read lock. */
852 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
853 const struct sw_flow_id
*sfid
,
854 struct genl_info
*info
,
861 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
, 0))
864 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
865 skb
= genlmsg_new(len
, GFP_KERNEL
);
867 return ERR_PTR(-ENOMEM
);
872 /* Called with ovs_mutex. */
873 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
875 struct genl_info
*info
, u8 cmd
,
876 bool always
, u32 ufid_flags
)
881 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
882 &flow
->id
, info
, always
, ufid_flags
);
883 if (IS_ERR_OR_NULL(skb
))
886 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
887 info
->snd_portid
, info
->snd_seq
, 0,
889 if (WARN_ON_ONCE(retval
< 0)) {
891 skb
= ERR_PTR(retval
);
896 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
898 struct net
*net
= sock_net(skb
->sk
);
899 struct nlattr
**a
= info
->attrs
;
900 struct ovs_header
*ovs_header
= info
->userhdr
;
901 struct sw_flow
*flow
= NULL
, *new_flow
;
902 struct sw_flow_mask mask
;
903 struct sk_buff
*reply
;
905 struct sw_flow_actions
*acts
;
906 struct sw_flow_match match
;
907 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
909 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
911 /* Must have key and actions. */
913 if (!a
[OVS_FLOW_ATTR_KEY
]) {
914 OVS_NLERR(log
, "Flow key attr not present in new flow.");
917 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
918 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
922 /* Most of the time we need to allocate a new flow, do it before
925 new_flow
= ovs_flow_alloc();
926 if (IS_ERR(new_flow
)) {
927 error
= PTR_ERR(new_flow
);
932 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
933 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
934 a
[OVS_FLOW_ATTR_MASK
], log
);
938 /* Extract flow identifier. */
939 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
940 &new_flow
->key
, log
);
944 /* unmasked key is needed to match when ufid is not used. */
945 if (ovs_identifier_is_key(&new_flow
->id
))
946 match
.key
= new_flow
->id
.unmasked_key
;
948 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
950 /* Validate actions. */
951 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
952 &new_flow
->key
, &acts
, log
);
954 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
958 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
961 error
= PTR_ERR(reply
);
966 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
972 /* Check if this is a duplicate flow */
973 if (ovs_identifier_is_ufid(&new_flow
->id
))
974 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
976 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
978 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
980 /* Put flow in bucket. */
981 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
982 if (unlikely(error
)) {
987 if (unlikely(reply
)) {
988 error
= ovs_flow_cmd_fill_info(new_flow
,
989 ovs_header
->dp_ifindex
,
990 reply
, info
->snd_portid
,
998 struct sw_flow_actions
*old_acts
;
1000 /* Bail out if we're not allowed to modify an existing flow.
1001 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1002 * because Generic Netlink treats the latter as a dump
1003 * request. We also accept NLM_F_EXCL in case that bug ever
1006 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1009 goto err_unlock_ovs
;
1011 /* The flow identifier has to be the same for flow updates.
1012 * Look for any overlapping flow.
1014 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1015 if (ovs_identifier_is_key(&flow
->id
))
1016 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1018 else /* UFID matches but key is different */
1022 goto err_unlock_ovs
;
1025 /* Update actions. */
1026 old_acts
= ovsl_dereference(flow
->sf_acts
);
1027 rcu_assign_pointer(flow
->sf_acts
, acts
);
1029 if (unlikely(reply
)) {
1030 error
= ovs_flow_cmd_fill_info(flow
,
1031 ovs_header
->dp_ifindex
,
1032 reply
, info
->snd_portid
,
1040 ovs_nla_free_flow_actions_rcu(old_acts
);
1041 ovs_flow_free(new_flow
, false);
1045 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1052 ovs_nla_free_flow_actions(acts
);
1054 ovs_flow_free(new_flow
, false);
1059 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1060 static noinline_for_stack
struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1061 const struct nlattr
*a
,
1062 const struct sw_flow_key
*key
,
1063 const struct sw_flow_mask
*mask
,
1066 struct sw_flow_actions
*acts
;
1067 struct sw_flow_key masked_key
;
1070 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1071 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1074 "Actions may not be safe on all matching packets");
1075 return ERR_PTR(error
);
1081 /* Factor out match-init and action-copy to avoid
1082 * "Wframe-larger-than=1024" warning. Because mask is only
1083 * used to get actions, we new a function to save some
1086 * If there are not key and action attrs, we return 0
1087 * directly. In the case, the caller will also not use the
1088 * match as before. If there is action attr, we try to get
1089 * actions and save them to *acts. Before returning from
1090 * the function, we reset the match->mask pointer. Because
1091 * we should not to return match object with dangling reference
1094 static noinline_for_stack
int
1095 ovs_nla_init_match_and_action(struct net
*net
,
1096 struct sw_flow_match
*match
,
1097 struct sw_flow_key
*key
,
1099 struct sw_flow_actions
**acts
,
1102 struct sw_flow_mask mask
;
1105 if (a
[OVS_FLOW_ATTR_KEY
]) {
1106 ovs_match_init(match
, key
, true, &mask
);
1107 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1108 a
[OVS_FLOW_ATTR_MASK
], log
);
1113 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1114 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1116 "Flow key attribute not present in set flow.");
1121 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1123 if (IS_ERR(*acts
)) {
1124 error
= PTR_ERR(*acts
);
1129 /* On success, error is 0. */
1135 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1137 struct net
*net
= sock_net(skb
->sk
);
1138 struct nlattr
**a
= info
->attrs
;
1139 struct ovs_header
*ovs_header
= info
->userhdr
;
1140 struct sw_flow_key key
;
1141 struct sw_flow
*flow
;
1142 struct sk_buff
*reply
= NULL
;
1143 struct datapath
*dp
;
1144 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1145 struct sw_flow_match match
;
1146 struct sw_flow_id sfid
;
1147 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1149 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1152 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1153 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1155 "Flow set message rejected, Key attribute missing.");
1159 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1165 /* Can allocate before locking if have acts. */
1166 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1168 if (IS_ERR(reply
)) {
1169 error
= PTR_ERR(reply
);
1170 goto err_kfree_acts
;
1175 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1176 if (unlikely(!dp
)) {
1178 goto err_unlock_ovs
;
1180 /* Check that the flow exists. */
1182 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1184 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1185 if (unlikely(!flow
)) {
1187 goto err_unlock_ovs
;
1190 /* Update actions, if present. */
1192 old_acts
= ovsl_dereference(flow
->sf_acts
);
1193 rcu_assign_pointer(flow
->sf_acts
, acts
);
1195 if (unlikely(reply
)) {
1196 error
= ovs_flow_cmd_fill_info(flow
,
1197 ovs_header
->dp_ifindex
,
1198 reply
, info
->snd_portid
,
1205 /* Could not alloc without acts before locking. */
1206 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1207 info
, OVS_FLOW_CMD_SET
, false,
1210 if (IS_ERR(reply
)) {
1211 error
= PTR_ERR(reply
);
1212 goto err_unlock_ovs
;
1217 if (a
[OVS_FLOW_ATTR_CLEAR
])
1218 ovs_flow_stats_clear(flow
);
1222 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1224 ovs_nla_free_flow_actions_rcu(old_acts
);
1232 ovs_nla_free_flow_actions(acts
);
1237 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1239 struct nlattr
**a
= info
->attrs
;
1240 struct ovs_header
*ovs_header
= info
->userhdr
;
1241 struct net
*net
= sock_net(skb
->sk
);
1242 struct sw_flow_key key
;
1243 struct sk_buff
*reply
;
1244 struct sw_flow
*flow
;
1245 struct datapath
*dp
;
1246 struct sw_flow_match match
;
1247 struct sw_flow_id ufid
;
1248 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1250 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1253 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1254 if (a
[OVS_FLOW_ATTR_KEY
]) {
1255 ovs_match_init(&match
, &key
, true, NULL
);
1256 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1258 } else if (!ufid_present
) {
1260 "Flow get message rejected, Key attribute missing.");
1267 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1274 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1276 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1282 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1283 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1284 if (IS_ERR(reply
)) {
1285 err
= PTR_ERR(reply
);
1290 return genlmsg_reply(reply
, info
);
1296 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1298 struct nlattr
**a
= info
->attrs
;
1299 struct ovs_header
*ovs_header
= info
->userhdr
;
1300 struct net
*net
= sock_net(skb
->sk
);
1301 struct sw_flow_key key
;
1302 struct sk_buff
*reply
;
1303 struct sw_flow
*flow
= NULL
;
1304 struct datapath
*dp
;
1305 struct sw_flow_match match
;
1306 struct sw_flow_id ufid
;
1307 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1309 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1312 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1313 if (a
[OVS_FLOW_ATTR_KEY
]) {
1314 ovs_match_init(&match
, &key
, true, NULL
);
1315 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1322 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1323 if (unlikely(!dp
)) {
1328 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1329 err
= ovs_flow_tbl_flush(&dp
->table
);
1334 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1336 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1337 if (unlikely(!flow
)) {
1342 ovs_flow_tbl_remove(&dp
->table
, flow
);
1345 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1346 &flow
->id
, info
, false, ufid_flags
);
1347 if (likely(reply
)) {
1348 if (!IS_ERR(reply
)) {
1349 rcu_read_lock(); /*To keep RCU checker happy. */
1350 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1351 reply
, info
->snd_portid
,
1356 if (WARN_ON_ONCE(err
< 0)) {
1361 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1363 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1368 ovs_flow_free(flow
, true);
1375 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1377 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1378 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1379 struct table_instance
*ti
;
1380 struct datapath
*dp
;
1384 err
= genlmsg_parse_deprecated(cb
->nlh
, &dp_flow_genl_family
, a
,
1385 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1388 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1391 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1397 ti
= rcu_dereference(dp
->table
.ti
);
1399 struct sw_flow
*flow
;
1402 bucket
= cb
->args
[0];
1404 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1408 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1409 NETLINK_CB(cb
->skb
).portid
,
1410 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1411 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1414 cb
->args
[0] = bucket
;
1421 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1422 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1423 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1424 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1425 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1426 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1427 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1428 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1431 static const struct genl_ops dp_flow_genl_ops
[] = {
1432 { .cmd
= OVS_FLOW_CMD_NEW
,
1433 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1434 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1435 .doit
= ovs_flow_cmd_new
1437 { .cmd
= OVS_FLOW_CMD_DEL
,
1438 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1439 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1440 .doit
= ovs_flow_cmd_del
1442 { .cmd
= OVS_FLOW_CMD_GET
,
1443 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1444 .flags
= 0, /* OK for unprivileged users. */
1445 .doit
= ovs_flow_cmd_get
,
1446 .dumpit
= ovs_flow_cmd_dump
1448 { .cmd
= OVS_FLOW_CMD_SET
,
1449 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1450 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1451 .doit
= ovs_flow_cmd_set
,
1455 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1456 .hdrsize
= sizeof(struct ovs_header
),
1457 .name
= OVS_FLOW_FAMILY
,
1458 .version
= OVS_FLOW_VERSION
,
1459 .maxattr
= OVS_FLOW_ATTR_MAX
,
1460 .policy
= flow_policy
,
1462 .parallel_ops
= true,
1463 .ops
= dp_flow_genl_ops
,
1464 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1465 .mcgrps
= &ovs_dp_flow_multicast_group
,
1467 .module
= THIS_MODULE
,
1470 static size_t ovs_dp_cmd_msg_size(void)
1472 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1474 msgsize
+= nla_total_size(IFNAMSIZ
);
1475 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1476 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1477 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1482 /* Called with ovs_mutex. */
1483 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1484 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1486 struct ovs_header
*ovs_header
;
1487 struct ovs_dp_stats dp_stats
;
1488 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1491 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1496 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1498 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1500 goto nla_put_failure
;
1502 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1503 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1504 &dp_stats
, OVS_DP_ATTR_PAD
))
1505 goto nla_put_failure
;
1507 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1508 sizeof(struct ovs_dp_megaflow_stats
),
1509 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1510 goto nla_put_failure
;
1512 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1513 goto nla_put_failure
;
1515 genlmsg_end(skb
, ovs_header
);
1519 genlmsg_cancel(skb
, ovs_header
);
1524 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1526 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1529 /* Called with rcu_read_lock or ovs_mutex. */
1530 static struct datapath
*lookup_datapath(struct net
*net
,
1531 const struct ovs_header
*ovs_header
,
1532 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1534 struct datapath
*dp
;
1536 if (!a
[OVS_DP_ATTR_NAME
])
1537 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1539 struct vport
*vport
;
1541 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1542 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1544 return dp
? dp
: ERR_PTR(-ENODEV
);
1547 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1549 struct datapath
*dp
;
1551 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1555 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1556 dp
->user_features
= 0;
1559 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support
);
1561 static int ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1563 u32 user_features
= 0;
1565 if (a
[OVS_DP_ATTR_USER_FEATURES
]) {
1566 user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1568 if (user_features
& ~(OVS_DP_F_VPORT_PIDS
|
1569 OVS_DP_F_UNALIGNED
|
1570 OVS_DP_F_TC_RECIRC_SHARING
))
1573 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1574 if (user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1579 dp
->user_features
= user_features
;
1581 if (dp
->user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1582 static_branch_enable(&tc_recirc_sharing_support
);
1584 static_branch_disable(&tc_recirc_sharing_support
);
1589 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1591 struct nlattr
**a
= info
->attrs
;
1592 struct vport_parms parms
;
1593 struct sk_buff
*reply
;
1594 struct datapath
*dp
;
1595 struct vport
*vport
;
1596 struct ovs_net
*ovs_net
;
1600 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1603 reply
= ovs_dp_cmd_alloc_info();
1608 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1610 goto err_free_reply
;
1612 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1614 /* Allocate table. */
1615 err
= ovs_flow_tbl_init(&dp
->table
);
1619 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1620 if (!dp
->stats_percpu
) {
1622 goto err_destroy_table
;
1625 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1626 sizeof(struct hlist_head
),
1630 goto err_destroy_percpu
;
1633 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1634 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1636 err
= ovs_meters_init(dp
);
1638 goto err_destroy_ports_array
;
1640 /* Set up our datapath device. */
1641 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1642 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1643 parms
.options
= NULL
;
1645 parms
.port_no
= OVSP_LOCAL
;
1646 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1648 err
= ovs_dp_change(dp
, a
);
1650 goto err_destroy_meters
;
1652 /* So far only local changes have been made, now need the lock. */
1655 vport
= new_vport(&parms
);
1656 if (IS_ERR(vport
)) {
1657 err
= PTR_ERR(vport
);
1661 if (err
== -EEXIST
) {
1662 /* An outdated user space instance that does not understand
1663 * the concept of user_features has attempted to create a new
1664 * datapath and is likely to reuse it. Drop all user features.
1666 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1667 ovs_dp_reset_user_features(skb
, info
);
1671 goto err_destroy_meters
;
1674 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1675 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1678 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1679 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1683 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1687 ovs_meters_exit(dp
);
1688 err_destroy_ports_array
:
1691 free_percpu(dp
->stats_percpu
);
1693 ovs_flow_tbl_destroy(&dp
->table
);
1702 /* Called with ovs_mutex. */
1703 static void __dp_destroy(struct datapath
*dp
)
1707 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1708 struct vport
*vport
;
1709 struct hlist_node
*n
;
1711 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1712 if (vport
->port_no
!= OVSP_LOCAL
)
1713 ovs_dp_detach_port(vport
);
1716 list_del_rcu(&dp
->list_node
);
1718 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1719 * all ports in datapath are destroyed first before freeing datapath.
1721 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1723 /* RCU destroy the flow table */
1724 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1727 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1729 struct sk_buff
*reply
;
1730 struct datapath
*dp
;
1733 reply
= ovs_dp_cmd_alloc_info();
1738 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1741 goto err_unlock_free
;
1743 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1744 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1750 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1760 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1762 struct sk_buff
*reply
;
1763 struct datapath
*dp
;
1766 reply
= ovs_dp_cmd_alloc_info();
1771 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1774 goto err_unlock_free
;
1776 err
= ovs_dp_change(dp
, info
->attrs
);
1778 goto err_unlock_free
;
1780 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1781 info
->snd_seq
, 0, OVS_DP_CMD_SET
);
1785 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1795 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1797 struct sk_buff
*reply
;
1798 struct datapath
*dp
;
1801 reply
= ovs_dp_cmd_alloc_info();
1806 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1809 goto err_unlock_free
;
1811 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1812 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1816 return genlmsg_reply(reply
, info
);
1824 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1826 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1827 struct datapath
*dp
;
1828 int skip
= cb
->args
[0];
1832 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1834 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1835 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1836 OVS_DP_CMD_GET
) < 0)
1847 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1848 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1849 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1850 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1853 static const struct genl_ops dp_datapath_genl_ops
[] = {
1854 { .cmd
= OVS_DP_CMD_NEW
,
1855 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1856 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1857 .doit
= ovs_dp_cmd_new
1859 { .cmd
= OVS_DP_CMD_DEL
,
1860 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1861 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1862 .doit
= ovs_dp_cmd_del
1864 { .cmd
= OVS_DP_CMD_GET
,
1865 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1866 .flags
= 0, /* OK for unprivileged users. */
1867 .doit
= ovs_dp_cmd_get
,
1868 .dumpit
= ovs_dp_cmd_dump
1870 { .cmd
= OVS_DP_CMD_SET
,
1871 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1872 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1873 .doit
= ovs_dp_cmd_set
,
1877 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
1878 .hdrsize
= sizeof(struct ovs_header
),
1879 .name
= OVS_DATAPATH_FAMILY
,
1880 .version
= OVS_DATAPATH_VERSION
,
1881 .maxattr
= OVS_DP_ATTR_MAX
,
1882 .policy
= datapath_policy
,
1884 .parallel_ops
= true,
1885 .ops
= dp_datapath_genl_ops
,
1886 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1887 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1889 .module
= THIS_MODULE
,
1892 /* Called with ovs_mutex or RCU read lock. */
1893 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1894 struct net
*net
, u32 portid
, u32 seq
,
1895 u32 flags
, u8 cmd
, gfp_t gfp
)
1897 struct ovs_header
*ovs_header
;
1898 struct ovs_vport_stats vport_stats
;
1901 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1906 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1908 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1909 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1910 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1911 ovs_vport_name(vport
)) ||
1912 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
1913 goto nla_put_failure
;
1915 if (!net_eq(net
, dev_net(vport
->dev
))) {
1916 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
), gfp
);
1918 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
1919 goto nla_put_failure
;
1922 ovs_vport_get_stats(vport
, &vport_stats
);
1923 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
1924 sizeof(struct ovs_vport_stats
), &vport_stats
,
1925 OVS_VPORT_ATTR_PAD
))
1926 goto nla_put_failure
;
1928 if (ovs_vport_get_upcall_portids(vport
, skb
))
1929 goto nla_put_failure
;
1931 err
= ovs_vport_get_options(vport
, skb
);
1932 if (err
== -EMSGSIZE
)
1935 genlmsg_end(skb
, ovs_header
);
1941 genlmsg_cancel(skb
, ovs_header
);
1945 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1947 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1950 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1951 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
1952 u32 portid
, u32 seq
, u8 cmd
)
1954 struct sk_buff
*skb
;
1957 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1959 return ERR_PTR(-ENOMEM
);
1961 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
,
1968 /* Called with ovs_mutex or RCU read lock. */
1969 static struct vport
*lookup_vport(struct net
*net
,
1970 const struct ovs_header
*ovs_header
,
1971 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1973 struct datapath
*dp
;
1974 struct vport
*vport
;
1976 if (a
[OVS_VPORT_ATTR_IFINDEX
])
1977 return ERR_PTR(-EOPNOTSUPP
);
1978 if (a
[OVS_VPORT_ATTR_NAME
]) {
1979 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1981 return ERR_PTR(-ENODEV
);
1982 if (ovs_header
->dp_ifindex
&&
1983 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1984 return ERR_PTR(-ENODEV
);
1986 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1987 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1989 if (port_no
>= DP_MAX_PORTS
)
1990 return ERR_PTR(-EFBIG
);
1992 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1994 return ERR_PTR(-ENODEV
);
1996 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1998 return ERR_PTR(-ENODEV
);
2001 return ERR_PTR(-EINVAL
);
2005 static unsigned int ovs_get_max_headroom(struct datapath
*dp
)
2007 unsigned int dev_headroom
, max_headroom
= 0;
2008 struct net_device
*dev
;
2009 struct vport
*vport
;
2012 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2013 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2015 dev_headroom
= netdev_get_fwd_headroom(dev
);
2016 if (dev_headroom
> max_headroom
)
2017 max_headroom
= dev_headroom
;
2021 return max_headroom
;
2024 /* Called with ovs_mutex */
2025 static void ovs_update_headroom(struct datapath
*dp
, unsigned int new_headroom
)
2027 struct vport
*vport
;
2030 dp
->max_headroom
= new_headroom
;
2031 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
2032 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
)
2033 netdev_set_rx_headroom(vport
->dev
, new_headroom
);
2036 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2038 struct nlattr
**a
= info
->attrs
;
2039 struct ovs_header
*ovs_header
= info
->userhdr
;
2040 struct vport_parms parms
;
2041 struct sk_buff
*reply
;
2042 struct vport
*vport
;
2043 struct datapath
*dp
;
2044 unsigned int new_headroom
;
2048 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2049 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2051 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2054 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2055 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2056 if (port_no
>= DP_MAX_PORTS
)
2059 reply
= ovs_vport_cmd_alloc_info();
2065 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2068 goto exit_unlock_free
;
2071 vport
= ovs_vport_ovsl(dp
, port_no
);
2074 goto exit_unlock_free
;
2076 for (port_no
= 1; ; port_no
++) {
2077 if (port_no
>= DP_MAX_PORTS
) {
2079 goto exit_unlock_free
;
2081 vport
= ovs_vport_ovsl(dp
, port_no
);
2087 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2088 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2089 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2091 parms
.port_no
= port_no
;
2092 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2094 vport
= new_vport(&parms
);
2095 err
= PTR_ERR(vport
);
2096 if (IS_ERR(vport
)) {
2099 goto exit_unlock_free
;
2102 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2103 info
->snd_portid
, info
->snd_seq
, 0,
2104 OVS_VPORT_CMD_NEW
, GFP_KERNEL
);
2106 new_headroom
= netdev_get_fwd_headroom(vport
->dev
);
2108 if (new_headroom
> dp
->max_headroom
)
2109 ovs_update_headroom(dp
, new_headroom
);
2111 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2116 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2125 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2127 struct nlattr
**a
= info
->attrs
;
2128 struct sk_buff
*reply
;
2129 struct vport
*vport
;
2132 reply
= ovs_vport_cmd_alloc_info();
2137 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2138 err
= PTR_ERR(vport
);
2140 goto exit_unlock_free
;
2142 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2143 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2145 goto exit_unlock_free
;
2148 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2149 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2151 goto exit_unlock_free
;
2155 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2156 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2158 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2160 goto exit_unlock_free
;
2163 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2164 info
->snd_portid
, info
->snd_seq
, 0,
2165 OVS_VPORT_CMD_SET
, GFP_KERNEL
);
2169 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2178 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2180 bool update_headroom
= false;
2181 struct nlattr
**a
= info
->attrs
;
2182 struct sk_buff
*reply
;
2183 struct datapath
*dp
;
2184 struct vport
*vport
;
2185 unsigned int new_headroom
;
2188 reply
= ovs_vport_cmd_alloc_info();
2193 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2194 err
= PTR_ERR(vport
);
2196 goto exit_unlock_free
;
2198 if (vport
->port_no
== OVSP_LOCAL
) {
2200 goto exit_unlock_free
;
2203 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2204 info
->snd_portid
, info
->snd_seq
, 0,
2205 OVS_VPORT_CMD_DEL
, GFP_KERNEL
);
2208 /* the vport deletion may trigger dp headroom update */
2210 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2211 update_headroom
= true;
2213 netdev_reset_rx_headroom(vport
->dev
);
2214 ovs_dp_detach_port(vport
);
2216 if (update_headroom
) {
2217 new_headroom
= ovs_get_max_headroom(dp
);
2219 if (new_headroom
< dp
->max_headroom
)
2220 ovs_update_headroom(dp
, new_headroom
);
2224 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2233 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2235 struct nlattr
**a
= info
->attrs
;
2236 struct ovs_header
*ovs_header
= info
->userhdr
;
2237 struct sk_buff
*reply
;
2238 struct vport
*vport
;
2241 reply
= ovs_vport_cmd_alloc_info();
2246 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2247 err
= PTR_ERR(vport
);
2249 goto exit_unlock_free
;
2250 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2251 info
->snd_portid
, info
->snd_seq
, 0,
2252 OVS_VPORT_CMD_GET
, GFP_ATOMIC
);
2256 return genlmsg_reply(reply
, info
);
2264 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2266 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2267 struct datapath
*dp
;
2268 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2272 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2277 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2278 struct vport
*vport
;
2281 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2283 ovs_vport_cmd_fill_info(vport
, skb
,
2285 NETLINK_CB(cb
->skb
).portid
,
2305 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2306 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2307 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2308 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2309 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2310 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2311 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2312 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2313 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2316 static const struct genl_ops dp_vport_genl_ops
[] = {
2317 { .cmd
= OVS_VPORT_CMD_NEW
,
2318 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2319 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2320 .doit
= ovs_vport_cmd_new
2322 { .cmd
= OVS_VPORT_CMD_DEL
,
2323 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2324 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2325 .doit
= ovs_vport_cmd_del
2327 { .cmd
= OVS_VPORT_CMD_GET
,
2328 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2329 .flags
= 0, /* OK for unprivileged users. */
2330 .doit
= ovs_vport_cmd_get
,
2331 .dumpit
= ovs_vport_cmd_dump
2333 { .cmd
= OVS_VPORT_CMD_SET
,
2334 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2335 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2336 .doit
= ovs_vport_cmd_set
,
2340 struct genl_family dp_vport_genl_family __ro_after_init
= {
2341 .hdrsize
= sizeof(struct ovs_header
),
2342 .name
= OVS_VPORT_FAMILY
,
2343 .version
= OVS_VPORT_VERSION
,
2344 .maxattr
= OVS_VPORT_ATTR_MAX
,
2345 .policy
= vport_policy
,
2347 .parallel_ops
= true,
2348 .ops
= dp_vport_genl_ops
,
2349 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2350 .mcgrps
= &ovs_dp_vport_multicast_group
,
2352 .module
= THIS_MODULE
,
2355 static struct genl_family
* const dp_genl_families
[] = {
2356 &dp_datapath_genl_family
,
2357 &dp_vport_genl_family
,
2358 &dp_flow_genl_family
,
2359 &dp_packet_genl_family
,
2360 &dp_meter_genl_family
,
2361 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2362 &dp_ct_limit_genl_family
,
2366 static void dp_unregister_genl(int n_families
)
2370 for (i
= 0; i
< n_families
; i
++)
2371 genl_unregister_family(dp_genl_families
[i
]);
2374 static int __init
dp_register_genl(void)
2379 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2381 err
= genl_register_family(dp_genl_families
[i
]);
2389 dp_unregister_genl(i
);
2393 static int __net_init
ovs_init_net(struct net
*net
)
2395 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2397 INIT_LIST_HEAD(&ovs_net
->dps
);
2398 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2399 return ovs_ct_init(net
);
2402 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2403 struct list_head
*head
)
2405 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2406 struct datapath
*dp
;
2408 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2411 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2412 struct vport
*vport
;
2414 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2415 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2418 if (dev_net(vport
->dev
) == dnet
)
2419 list_add(&vport
->detach_list
, head
);
2425 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2427 struct datapath
*dp
, *dp_next
;
2428 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2429 struct vport
*vport
, *vport_next
;
2437 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2440 down_read(&net_rwsem
);
2442 list_vports_from_net(net
, dnet
, &head
);
2443 up_read(&net_rwsem
);
2445 /* Detach all vports from given namespace. */
2446 list_for_each_entry_safe(vport
, vport_next
, &head
, detach_list
) {
2447 list_del(&vport
->detach_list
);
2448 ovs_dp_detach_port(vport
);
2453 cancel_work_sync(&ovs_net
->dp_notify_work
);
2456 static struct pernet_operations ovs_net_ops
= {
2457 .init
= ovs_init_net
,
2458 .exit
= ovs_exit_net
,
2460 .size
= sizeof(struct ovs_net
),
2463 static int __init
dp_init(void)
2467 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2469 pr_info("Open vSwitch switching datapath\n");
2471 err
= action_fifos_init();
2475 err
= ovs_internal_dev_rtnl_link_register();
2477 goto error_action_fifos_exit
;
2479 err
= ovs_flow_init();
2481 goto error_unreg_rtnl_link
;
2483 err
= ovs_vport_init();
2485 goto error_flow_exit
;
2487 err
= register_pernet_device(&ovs_net_ops
);
2489 goto error_vport_exit
;
2491 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2493 goto error_netns_exit
;
2495 err
= ovs_netdev_init();
2497 goto error_unreg_notifier
;
2499 err
= dp_register_genl();
2501 goto error_unreg_netdev
;
2507 error_unreg_notifier
:
2508 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2510 unregister_pernet_device(&ovs_net_ops
);
2515 error_unreg_rtnl_link
:
2516 ovs_internal_dev_rtnl_link_unregister();
2517 error_action_fifos_exit
:
2518 action_fifos_exit();
2523 static void dp_cleanup(void)
2525 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2527 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2528 unregister_pernet_device(&ovs_net_ops
);
2532 ovs_internal_dev_rtnl_link_unregister();
2533 action_fifos_exit();
2536 module_init(dp_init
);
2537 module_exit(dp_cleanup
);
2539 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2540 MODULE_LICENSE("GPL");
2541 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY
);
2542 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY
);
2543 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY
);
2544 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY
);
2545 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY
);
2546 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY
);