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 "openvswitch_trace.h"
47 #include "vport-internal_dev.h"
48 #include "vport-netdev.h"
50 unsigned int ovs_net_id __read_mostly
;
52 static struct genl_family dp_packet_genl_family
;
53 static struct genl_family dp_flow_genl_family
;
54 static struct genl_family dp_datapath_genl_family
;
56 static const struct nla_policy flow_policy
[];
58 static const struct genl_multicast_group ovs_dp_flow_multicast_group
= {
59 .name
= OVS_FLOW_MCGROUP
,
62 static const struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
63 .name
= OVS_DATAPATH_MCGROUP
,
66 static const struct genl_multicast_group ovs_dp_vport_multicast_group
= {
67 .name
= OVS_VPORT_MCGROUP
,
70 /* Check if need to build a reply message.
71 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
72 static bool ovs_must_notify(struct genl_family
*family
, struct genl_info
*info
,
75 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
76 genl_has_listeners(family
, genl_info_net(info
), group
);
79 static void ovs_notify(struct genl_family
*family
,
80 struct sk_buff
*skb
, struct genl_info
*info
)
82 genl_notify(family
, skb
, info
, 0, GFP_KERNEL
);
88 * All writes e.g. Writes to device state (add/remove datapath, port, set
89 * operations on vports, etc.), Writes to other state (flow table
90 * modifications, set miscellaneous datapath parameters, etc.) are protected
93 * Reads are protected by RCU.
95 * There are a few special cases (mostly stats) that have their own
96 * synchronization but they nest under all of above and don't interact with
99 * The RTNL lock nests inside ovs_mutex.
102 static DEFINE_MUTEX(ovs_mutex
);
106 mutex_lock(&ovs_mutex
);
109 void ovs_unlock(void)
111 mutex_unlock(&ovs_mutex
);
114 #ifdef CONFIG_LOCKDEP
115 int lockdep_ovsl_is_held(void)
118 return lockdep_is_held(&ovs_mutex
);
124 static struct vport
*new_vport(const struct vport_parms
*);
125 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
126 const struct sw_flow_key
*,
127 const struct dp_upcall_info
*,
129 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
130 const struct sw_flow_key
*,
131 const struct dp_upcall_info
*,
134 static void ovs_dp_masks_rebalance(struct work_struct
*work
);
136 static int ovs_dp_set_upcall_portids(struct datapath
*, const struct nlattr
*);
138 /* Must be called with rcu_read_lock or ovs_mutex. */
139 const char *ovs_dp_name(const struct datapath
*dp
)
141 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
142 return ovs_vport_name(vport
);
145 static int get_dpifindex(const struct datapath
*dp
)
152 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
154 ifindex
= local
->dev
->ifindex
;
163 static void destroy_dp_rcu(struct rcu_head
*rcu
)
165 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
167 ovs_flow_tbl_destroy(&dp
->table
);
168 free_percpu(dp
->stats_percpu
);
171 kfree(rcu_dereference_raw(dp
->upcall_portids
));
175 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
178 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
181 /* Called with ovs_mutex or RCU read lock. */
182 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
185 struct hlist_head
*head
;
187 head
= vport_hash_bucket(dp
, port_no
);
188 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
,
189 lockdep_ovsl_is_held()) {
190 if (vport
->port_no
== port_no
)
196 /* Called with ovs_mutex. */
197 static struct vport
*new_vport(const struct vport_parms
*parms
)
201 vport
= ovs_vport_add(parms
);
202 if (!IS_ERR(vport
)) {
203 struct datapath
*dp
= parms
->dp
;
204 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
206 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
211 void ovs_dp_detach_port(struct vport
*p
)
215 /* First drop references to device. */
216 hlist_del_rcu(&p
->dp_hash_node
);
218 /* Then destroy it. */
222 /* Must be called with rcu_read_lock. */
223 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
225 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
226 struct datapath
*dp
= p
->dp
;
227 struct sw_flow
*flow
;
228 struct sw_flow_actions
*sf_acts
;
229 struct dp_stats_percpu
*stats
;
235 stats
= this_cpu_ptr(dp
->stats_percpu
);
238 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, skb_get_hash(skb
),
239 &n_mask_hit
, &n_cache_hit
);
240 if (unlikely(!flow
)) {
241 struct dp_upcall_info upcall
;
243 memset(&upcall
, 0, sizeof(upcall
));
244 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
246 if (dp
->user_features
& OVS_DP_F_DISPATCH_UPCALL_PER_CPU
)
248 ovs_dp_get_upcall_portid(dp
, smp_processor_id());
250 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
252 upcall
.mru
= OVS_CB(skb
)->mru
;
253 error
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, 0);
258 stats_counter
= &stats
->n_missed
;
262 ovs_flow_stats_update(flow
, key
->tp
.flags
, skb
);
263 sf_acts
= rcu_dereference(flow
->sf_acts
);
264 error
= ovs_execute_actions(dp
, skb
, sf_acts
, key
);
266 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
267 ovs_dp_name(dp
), error
);
269 stats_counter
= &stats
->n_hit
;
272 /* Update datapath statistics. */
273 u64_stats_update_begin(&stats
->syncp
);
275 stats
->n_mask_hit
+= n_mask_hit
;
276 stats
->n_cache_hit
+= n_cache_hit
;
277 u64_stats_update_end(&stats
->syncp
);
280 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
281 const struct sw_flow_key
*key
,
282 const struct dp_upcall_info
*upcall_info
,
285 struct dp_stats_percpu
*stats
;
288 if (trace_ovs_dp_upcall_enabled())
289 trace_ovs_dp_upcall(dp
, skb
, key
, upcall_info
);
291 if (upcall_info
->portid
== 0) {
296 if (!skb_is_gso(skb
))
297 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
299 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
, cutlen
);
306 stats
= this_cpu_ptr(dp
->stats_percpu
);
308 u64_stats_update_begin(&stats
->syncp
);
310 u64_stats_update_end(&stats
->syncp
);
315 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
316 const struct sw_flow_key
*key
,
317 const struct dp_upcall_info
*upcall_info
,
320 unsigned int gso_type
= skb_shinfo(skb
)->gso_type
;
321 struct sw_flow_key later_key
;
322 struct sk_buff
*segs
, *nskb
;
325 BUILD_BUG_ON(sizeof(*OVS_CB(skb
)) > SKB_GSO_CB_OFFSET
);
326 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
328 return PTR_ERR(segs
);
332 if (gso_type
& SKB_GSO_UDP
) {
333 /* The initial flow key extracted by ovs_flow_key_extract()
334 * in this case is for a first fragment, so we need to
335 * properly mark later fragments.
338 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
341 /* Queue all of the segments. */
342 skb_list_walk_safe(segs
, skb
, nskb
) {
343 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
346 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
352 /* Free all of the segments. */
353 skb_list_walk_safe(segs
, skb
, nskb
) {
362 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
363 unsigned int hdrlen
, int actions_attrlen
)
365 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
366 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
367 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
368 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
369 + nla_total_size(sizeof(u64
)); /* OVS_PACKET_ATTR_HASH */
371 /* OVS_PACKET_ATTR_USERDATA */
372 if (upcall_info
->userdata
)
373 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
375 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
376 if (upcall_info
->egress_tun_info
)
377 size
+= nla_total_size(ovs_tun_key_attr_size());
379 /* OVS_PACKET_ATTR_ACTIONS */
380 if (upcall_info
->actions_len
)
381 size
+= nla_total_size(actions_attrlen
);
383 /* OVS_PACKET_ATTR_MRU */
384 if (upcall_info
->mru
)
385 size
+= nla_total_size(sizeof(upcall_info
->mru
));
390 static void pad_packet(struct datapath
*dp
, struct sk_buff
*skb
)
392 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
393 size_t plen
= NLA_ALIGN(skb
->len
) - skb
->len
;
396 skb_put_zero(skb
, plen
);
400 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
401 const struct sw_flow_key
*key
,
402 const struct dp_upcall_info
*upcall_info
,
405 struct ovs_header
*upcall
;
406 struct sk_buff
*nskb
= NULL
;
407 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
414 dp_ifindex
= get_dpifindex(dp
);
418 if (skb_vlan_tag_present(skb
)) {
419 nskb
= skb_clone(skb
, GFP_ATOMIC
);
423 nskb
= __vlan_hwaccel_push_inside(nskb
);
430 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
435 /* Complete checksum if needed */
436 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
437 (err
= skb_csum_hwoffload_help(skb
, 0)))
440 /* Older versions of OVS user space enforce alignment of the last
441 * Netlink attribute to NLA_ALIGNTO which would require extensive
442 * padding logic. Only perform zerocopy if padding is not required.
444 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
445 hlen
= skb_zerocopy_headlen(skb
);
449 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
450 OVS_CB(skb
)->acts_origlen
);
451 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
457 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
458 0, upcall_info
->cmd
);
463 upcall
->dp_ifindex
= dp_ifindex
;
465 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
469 if (upcall_info
->userdata
)
470 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
471 nla_len(upcall_info
->userdata
),
472 nla_data(upcall_info
->userdata
));
474 if (upcall_info
->egress_tun_info
) {
475 nla
= nla_nest_start_noflag(user_skb
,
476 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
481 err
= ovs_nla_put_tunnel_info(user_skb
,
482 upcall_info
->egress_tun_info
);
486 nla_nest_end(user_skb
, nla
);
489 if (upcall_info
->actions_len
) {
490 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
495 err
= ovs_nla_put_actions(upcall_info
->actions
,
496 upcall_info
->actions_len
,
499 nla_nest_end(user_skb
, nla
);
501 nla_nest_cancel(user_skb
, nla
);
504 /* Add OVS_PACKET_ATTR_MRU */
505 if (upcall_info
->mru
&&
506 nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
, upcall_info
->mru
)) {
511 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
513 nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
, skb
->len
)) {
518 /* Add OVS_PACKET_ATTR_HASH */
519 hash
= skb_get_hash_raw(skb
);
521 hash
|= OVS_PACKET_HASH_SW_BIT
;
524 hash
|= OVS_PACKET_HASH_L4_BIT
;
526 if (nla_put(user_skb
, OVS_PACKET_ATTR_HASH
, sizeof (u64
), &hash
)) {
531 /* Only reserve room for attribute header, packet data is added
532 * in skb_zerocopy() */
533 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
537 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
539 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
543 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
544 pad_packet(dp
, user_skb
);
546 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
548 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
558 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
560 struct ovs_header
*ovs_header
= info
->userhdr
;
561 struct net
*net
= sock_net(skb
->sk
);
562 struct nlattr
**a
= info
->attrs
;
563 struct sw_flow_actions
*acts
;
564 struct sk_buff
*packet
;
565 struct sw_flow
*flow
;
566 struct sw_flow_actions
*sf_acts
;
568 struct vport
*input_vport
;
573 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
576 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
577 !a
[OVS_PACKET_ATTR_ACTIONS
])
580 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
581 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
585 skb_reserve(packet
, NET_IP_ALIGN
);
587 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
589 /* Set packet's mru */
590 if (a
[OVS_PACKET_ATTR_MRU
]) {
591 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
592 packet
->ignore_df
= 1;
594 OVS_CB(packet
)->mru
= mru
;
596 if (a
[OVS_PACKET_ATTR_HASH
]) {
597 hash
= nla_get_u64(a
[OVS_PACKET_ATTR_HASH
]);
599 __skb_set_hash(packet
, hash
& 0xFFFFFFFFULL
,
600 !!(hash
& OVS_PACKET_HASH_SW_BIT
),
601 !!(hash
& OVS_PACKET_HASH_L4_BIT
));
604 /* Build an sw_flow for sending this packet. */
605 flow
= ovs_flow_alloc();
610 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
611 packet
, &flow
->key
, log
);
615 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
616 &flow
->key
, &acts
, log
);
620 rcu_assign_pointer(flow
->sf_acts
, acts
);
621 packet
->priority
= flow
->key
.phy
.priority
;
622 packet
->mark
= flow
->key
.phy
.skb_mark
;
625 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
630 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
632 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
637 packet
->dev
= input_vport
->dev
;
638 OVS_CB(packet
)->input_vport
= input_vport
;
639 sf_acts
= rcu_dereference(flow
->sf_acts
);
642 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
646 ovs_flow_free(flow
, false);
652 ovs_flow_free(flow
, false);
659 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
660 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
661 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
662 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
663 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
664 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
665 [OVS_PACKET_ATTR_HASH
] = { .type
= NLA_U64
},
668 static const struct genl_small_ops dp_packet_genl_ops
[] = {
669 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
670 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
671 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
672 .doit
= ovs_packet_cmd_execute
676 static struct genl_family dp_packet_genl_family __ro_after_init
= {
677 .hdrsize
= sizeof(struct ovs_header
),
678 .name
= OVS_PACKET_FAMILY
,
679 .version
= OVS_PACKET_VERSION
,
680 .maxattr
= OVS_PACKET_ATTR_MAX
,
681 .policy
= packet_policy
,
683 .parallel_ops
= true,
684 .small_ops
= dp_packet_genl_ops
,
685 .n_small_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
686 .module
= THIS_MODULE
,
689 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
690 struct ovs_dp_megaflow_stats
*mega_stats
)
694 memset(mega_stats
, 0, sizeof(*mega_stats
));
696 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
697 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
699 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
701 for_each_possible_cpu(i
) {
702 const struct dp_stats_percpu
*percpu_stats
;
703 struct dp_stats_percpu local_stats
;
706 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
709 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
710 local_stats
= *percpu_stats
;
711 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
713 stats
->n_hit
+= local_stats
.n_hit
;
714 stats
->n_missed
+= local_stats
.n_missed
;
715 stats
->n_lost
+= local_stats
.n_lost
;
716 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
717 mega_stats
->n_cache_hit
+= local_stats
.n_cache_hit
;
721 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
723 return ovs_identifier_is_ufid(sfid
) &&
724 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
727 static bool should_fill_mask(uint32_t ufid_flags
)
729 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
732 static bool should_fill_actions(uint32_t ufid_flags
)
734 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
737 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
738 const struct sw_flow_id
*sfid
,
741 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
743 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
744 * see ovs_nla_put_identifier()
746 if (sfid
&& ovs_identifier_is_ufid(sfid
))
747 len
+= nla_total_size(sfid
->ufid_len
);
749 len
+= nla_total_size(ovs_key_attr_size());
751 /* OVS_FLOW_ATTR_KEY */
752 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
753 len
+= nla_total_size(ovs_key_attr_size());
755 /* OVS_FLOW_ATTR_MASK */
756 if (should_fill_mask(ufid_flags
))
757 len
+= nla_total_size(ovs_key_attr_size());
759 /* OVS_FLOW_ATTR_ACTIONS */
760 if (should_fill_actions(ufid_flags
))
761 len
+= nla_total_size(acts
->orig_len
);
764 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
765 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
766 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
769 /* Called with ovs_mutex or RCU read lock. */
770 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
773 struct ovs_flow_stats stats
;
777 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
780 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
784 if (stats
.n_packets
&&
785 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
786 sizeof(struct ovs_flow_stats
), &stats
,
790 if ((u8
)ntohs(tcp_flags
) &&
791 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
797 /* Called with ovs_mutex or RCU read lock. */
798 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
799 struct sk_buff
*skb
, int skb_orig_len
)
801 struct nlattr
*start
;
804 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
805 * this is the first flow to be dumped into 'skb'. This is unusual for
806 * Netlink but individual action lists can be longer than
807 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
808 * The userspace caller can always fetch the actions separately if it
809 * really wants them. (Most userspace callers in fact don't care.)
811 * This can only fail for dump operations because the skb is always
812 * properly sized for single flows.
814 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
816 const struct sw_flow_actions
*sf_acts
;
818 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
819 err
= ovs_nla_put_actions(sf_acts
->actions
,
820 sf_acts
->actions_len
, skb
);
823 nla_nest_end(skb
, start
);
828 nla_nest_cancel(skb
, start
);
830 } else if (skb_orig_len
) {
837 /* Called with ovs_mutex or RCU read lock. */
838 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
839 struct sk_buff
*skb
, u32 portid
,
840 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
842 const int skb_orig_len
= skb
->len
;
843 struct ovs_header
*ovs_header
;
846 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
851 ovs_header
->dp_ifindex
= dp_ifindex
;
853 err
= ovs_nla_put_identifier(flow
, skb
);
857 if (should_fill_key(&flow
->id
, ufid_flags
)) {
858 err
= ovs_nla_put_masked_key(flow
, skb
);
863 if (should_fill_mask(ufid_flags
)) {
864 err
= ovs_nla_put_mask(flow
, skb
);
869 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
873 if (should_fill_actions(ufid_flags
)) {
874 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
879 genlmsg_end(skb
, ovs_header
);
883 genlmsg_cancel(skb
, ovs_header
);
887 /* May not be called with RCU read lock. */
888 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
889 const struct sw_flow_id
*sfid
,
890 struct genl_info
*info
,
897 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
, 0))
900 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
901 skb
= genlmsg_new(len
, GFP_KERNEL
);
903 return ERR_PTR(-ENOMEM
);
908 /* Called with ovs_mutex. */
909 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
911 struct genl_info
*info
, u8 cmd
,
912 bool always
, u32 ufid_flags
)
917 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
918 &flow
->id
, info
, always
, ufid_flags
);
919 if (IS_ERR_OR_NULL(skb
))
922 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
923 info
->snd_portid
, info
->snd_seq
, 0,
925 if (WARN_ON_ONCE(retval
< 0)) {
927 skb
= ERR_PTR(retval
);
932 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
934 struct net
*net
= sock_net(skb
->sk
);
935 struct nlattr
**a
= info
->attrs
;
936 struct ovs_header
*ovs_header
= info
->userhdr
;
937 struct sw_flow
*flow
= NULL
, *new_flow
;
938 struct sw_flow_mask mask
;
939 struct sk_buff
*reply
;
941 struct sw_flow_actions
*acts
;
942 struct sw_flow_match match
;
943 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
945 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
947 /* Must have key and actions. */
949 if (!a
[OVS_FLOW_ATTR_KEY
]) {
950 OVS_NLERR(log
, "Flow key attr not present in new flow.");
953 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
954 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
958 /* Most of the time we need to allocate a new flow, do it before
961 new_flow
= ovs_flow_alloc();
962 if (IS_ERR(new_flow
)) {
963 error
= PTR_ERR(new_flow
);
968 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
969 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
970 a
[OVS_FLOW_ATTR_MASK
], log
);
974 /* Extract flow identifier. */
975 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
976 &new_flow
->key
, log
);
980 /* unmasked key is needed to match when ufid is not used. */
981 if (ovs_identifier_is_key(&new_flow
->id
))
982 match
.key
= new_flow
->id
.unmasked_key
;
984 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
986 /* Validate actions. */
987 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
988 &new_flow
->key
, &acts
, log
);
990 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
994 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
997 error
= PTR_ERR(reply
);
1002 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1003 if (unlikely(!dp
)) {
1005 goto err_unlock_ovs
;
1008 /* Check if this is a duplicate flow */
1009 if (ovs_identifier_is_ufid(&new_flow
->id
))
1010 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
1012 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
1013 if (likely(!flow
)) {
1014 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
1016 /* Put flow in bucket. */
1017 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
1018 if (unlikely(error
)) {
1020 goto err_unlock_ovs
;
1023 if (unlikely(reply
)) {
1024 error
= ovs_flow_cmd_fill_info(new_flow
,
1025 ovs_header
->dp_ifindex
,
1026 reply
, info
->snd_portid
,
1034 struct sw_flow_actions
*old_acts
;
1036 /* Bail out if we're not allowed to modify an existing flow.
1037 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1038 * because Generic Netlink treats the latter as a dump
1039 * request. We also accept NLM_F_EXCL in case that bug ever
1042 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1045 goto err_unlock_ovs
;
1047 /* The flow identifier has to be the same for flow updates.
1048 * Look for any overlapping flow.
1050 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1051 if (ovs_identifier_is_key(&flow
->id
))
1052 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1054 else /* UFID matches but key is different */
1058 goto err_unlock_ovs
;
1061 /* Update actions. */
1062 old_acts
= ovsl_dereference(flow
->sf_acts
);
1063 rcu_assign_pointer(flow
->sf_acts
, acts
);
1065 if (unlikely(reply
)) {
1066 error
= ovs_flow_cmd_fill_info(flow
,
1067 ovs_header
->dp_ifindex
,
1068 reply
, info
->snd_portid
,
1076 ovs_nla_free_flow_actions_rcu(old_acts
);
1077 ovs_flow_free(new_flow
, false);
1081 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1088 ovs_nla_free_flow_actions(acts
);
1090 ovs_flow_free(new_flow
, false);
1095 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1096 static noinline_for_stack
1097 struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1098 const struct nlattr
*a
,
1099 const struct sw_flow_key
*key
,
1100 const struct sw_flow_mask
*mask
,
1103 struct sw_flow_actions
*acts
;
1104 struct sw_flow_key masked_key
;
1107 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1108 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1111 "Actions may not be safe on all matching packets");
1112 return ERR_PTR(error
);
1118 /* Factor out match-init and action-copy to avoid
1119 * "Wframe-larger-than=1024" warning. Because mask is only
1120 * used to get actions, we new a function to save some
1123 * If there are not key and action attrs, we return 0
1124 * directly. In the case, the caller will also not use the
1125 * match as before. If there is action attr, we try to get
1126 * actions and save them to *acts. Before returning from
1127 * the function, we reset the match->mask pointer. Because
1128 * we should not to return match object with dangling reference
1131 static noinline_for_stack
int
1132 ovs_nla_init_match_and_action(struct net
*net
,
1133 struct sw_flow_match
*match
,
1134 struct sw_flow_key
*key
,
1136 struct sw_flow_actions
**acts
,
1139 struct sw_flow_mask mask
;
1142 if (a
[OVS_FLOW_ATTR_KEY
]) {
1143 ovs_match_init(match
, key
, true, &mask
);
1144 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1145 a
[OVS_FLOW_ATTR_MASK
], log
);
1150 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1151 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1153 "Flow key attribute not present in set flow.");
1158 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1160 if (IS_ERR(*acts
)) {
1161 error
= PTR_ERR(*acts
);
1166 /* On success, error is 0. */
1172 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1174 struct net
*net
= sock_net(skb
->sk
);
1175 struct nlattr
**a
= info
->attrs
;
1176 struct ovs_header
*ovs_header
= info
->userhdr
;
1177 struct sw_flow_key key
;
1178 struct sw_flow
*flow
;
1179 struct sk_buff
*reply
= NULL
;
1180 struct datapath
*dp
;
1181 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1182 struct sw_flow_match match
;
1183 struct sw_flow_id sfid
;
1184 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1186 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1189 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1190 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1192 "Flow set message rejected, Key attribute missing.");
1196 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1202 /* Can allocate before locking if have acts. */
1203 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1205 if (IS_ERR(reply
)) {
1206 error
= PTR_ERR(reply
);
1207 goto err_kfree_acts
;
1212 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1213 if (unlikely(!dp
)) {
1215 goto err_unlock_ovs
;
1217 /* Check that the flow exists. */
1219 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1221 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1222 if (unlikely(!flow
)) {
1224 goto err_unlock_ovs
;
1227 /* Update actions, if present. */
1229 old_acts
= ovsl_dereference(flow
->sf_acts
);
1230 rcu_assign_pointer(flow
->sf_acts
, acts
);
1232 if (unlikely(reply
)) {
1233 error
= ovs_flow_cmd_fill_info(flow
,
1234 ovs_header
->dp_ifindex
,
1235 reply
, info
->snd_portid
,
1242 /* Could not alloc without acts before locking. */
1243 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1244 info
, OVS_FLOW_CMD_SET
, false,
1247 if (IS_ERR(reply
)) {
1248 error
= PTR_ERR(reply
);
1249 goto err_unlock_ovs
;
1254 if (a
[OVS_FLOW_ATTR_CLEAR
])
1255 ovs_flow_stats_clear(flow
);
1259 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1261 ovs_nla_free_flow_actions_rcu(old_acts
);
1269 ovs_nla_free_flow_actions(acts
);
1274 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1276 struct nlattr
**a
= info
->attrs
;
1277 struct ovs_header
*ovs_header
= info
->userhdr
;
1278 struct net
*net
= sock_net(skb
->sk
);
1279 struct sw_flow_key key
;
1280 struct sk_buff
*reply
;
1281 struct sw_flow
*flow
;
1282 struct datapath
*dp
;
1283 struct sw_flow_match match
;
1284 struct sw_flow_id ufid
;
1285 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1287 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1290 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1291 if (a
[OVS_FLOW_ATTR_KEY
]) {
1292 ovs_match_init(&match
, &key
, true, NULL
);
1293 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1295 } else if (!ufid_present
) {
1297 "Flow get message rejected, Key attribute missing.");
1304 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1311 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1313 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1319 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1320 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1321 if (IS_ERR(reply
)) {
1322 err
= PTR_ERR(reply
);
1327 return genlmsg_reply(reply
, info
);
1333 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1335 struct nlattr
**a
= info
->attrs
;
1336 struct ovs_header
*ovs_header
= info
->userhdr
;
1337 struct net
*net
= sock_net(skb
->sk
);
1338 struct sw_flow_key key
;
1339 struct sk_buff
*reply
;
1340 struct sw_flow
*flow
= NULL
;
1341 struct datapath
*dp
;
1342 struct sw_flow_match match
;
1343 struct sw_flow_id ufid
;
1344 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1346 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1349 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1350 if (a
[OVS_FLOW_ATTR_KEY
]) {
1351 ovs_match_init(&match
, &key
, true, NULL
);
1352 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1359 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1360 if (unlikely(!dp
)) {
1365 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1366 err
= ovs_flow_tbl_flush(&dp
->table
);
1371 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1373 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1374 if (unlikely(!flow
)) {
1379 ovs_flow_tbl_remove(&dp
->table
, flow
);
1382 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1383 &flow
->id
, info
, false, ufid_flags
);
1384 if (likely(reply
)) {
1385 if (!IS_ERR(reply
)) {
1386 rcu_read_lock(); /*To keep RCU checker happy. */
1387 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1388 reply
, info
->snd_portid
,
1393 if (WARN_ON_ONCE(err
< 0)) {
1398 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1400 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0,
1406 ovs_flow_free(flow
, true);
1413 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1415 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1416 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1417 struct table_instance
*ti
;
1418 struct datapath
*dp
;
1422 err
= genlmsg_parse_deprecated(cb
->nlh
, &dp_flow_genl_family
, a
,
1423 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1426 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1429 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1435 ti
= rcu_dereference(dp
->table
.ti
);
1437 struct sw_flow
*flow
;
1440 bucket
= cb
->args
[0];
1442 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1446 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1447 NETLINK_CB(cb
->skb
).portid
,
1448 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1449 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1452 cb
->args
[0] = bucket
;
1459 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1460 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1461 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1462 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1463 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1464 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1465 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1466 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1469 static const struct genl_small_ops dp_flow_genl_ops
[] = {
1470 { .cmd
= OVS_FLOW_CMD_NEW
,
1471 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1472 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1473 .doit
= ovs_flow_cmd_new
1475 { .cmd
= OVS_FLOW_CMD_DEL
,
1476 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1477 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1478 .doit
= ovs_flow_cmd_del
1480 { .cmd
= OVS_FLOW_CMD_GET
,
1481 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1482 .flags
= 0, /* OK for unprivileged users. */
1483 .doit
= ovs_flow_cmd_get
,
1484 .dumpit
= ovs_flow_cmd_dump
1486 { .cmd
= OVS_FLOW_CMD_SET
,
1487 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1488 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1489 .doit
= ovs_flow_cmd_set
,
1493 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1494 .hdrsize
= sizeof(struct ovs_header
),
1495 .name
= OVS_FLOW_FAMILY
,
1496 .version
= OVS_FLOW_VERSION
,
1497 .maxattr
= OVS_FLOW_ATTR_MAX
,
1498 .policy
= flow_policy
,
1500 .parallel_ops
= true,
1501 .small_ops
= dp_flow_genl_ops
,
1502 .n_small_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1503 .mcgrps
= &ovs_dp_flow_multicast_group
,
1505 .module
= THIS_MODULE
,
1508 static size_t ovs_dp_cmd_msg_size(void)
1510 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1512 msgsize
+= nla_total_size(IFNAMSIZ
);
1513 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1514 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1515 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1516 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_MASKS_CACHE_SIZE */
1521 /* Called with ovs_mutex. */
1522 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1523 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1525 struct ovs_header
*ovs_header
;
1526 struct ovs_dp_stats dp_stats
;
1527 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1530 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1535 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1537 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1539 goto nla_put_failure
;
1541 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1542 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1543 &dp_stats
, OVS_DP_ATTR_PAD
))
1544 goto nla_put_failure
;
1546 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1547 sizeof(struct ovs_dp_megaflow_stats
),
1548 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1549 goto nla_put_failure
;
1551 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1552 goto nla_put_failure
;
1554 if (nla_put_u32(skb
, OVS_DP_ATTR_MASKS_CACHE_SIZE
,
1555 ovs_flow_tbl_masks_cache_size(&dp
->table
)))
1556 goto nla_put_failure
;
1558 genlmsg_end(skb
, ovs_header
);
1562 genlmsg_cancel(skb
, ovs_header
);
1567 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1569 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1572 /* Called with rcu_read_lock or ovs_mutex. */
1573 static struct datapath
*lookup_datapath(struct net
*net
,
1574 const struct ovs_header
*ovs_header
,
1575 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1577 struct datapath
*dp
;
1579 if (!a
[OVS_DP_ATTR_NAME
])
1580 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1582 struct vport
*vport
;
1584 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1585 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1587 return dp
? dp
: ERR_PTR(-ENODEV
);
1590 static void ovs_dp_reset_user_features(struct sk_buff
*skb
,
1591 struct genl_info
*info
)
1593 struct datapath
*dp
;
1595 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
,
1600 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1601 dp
->user_features
= 0;
1604 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support
);
1606 static int ovs_dp_set_upcall_portids(struct datapath
*dp
,
1607 const struct nlattr
*ids
)
1609 struct dp_nlsk_pids
*old
, *dp_nlsk_pids
;
1611 if (!nla_len(ids
) || nla_len(ids
) % sizeof(u32
))
1614 old
= ovsl_dereference(dp
->upcall_portids
);
1616 dp_nlsk_pids
= kmalloc(sizeof(*dp_nlsk_pids
) + nla_len(ids
),
1621 dp_nlsk_pids
->n_pids
= nla_len(ids
) / sizeof(u32
);
1622 nla_memcpy(dp_nlsk_pids
->pids
, ids
, nla_len(ids
));
1624 rcu_assign_pointer(dp
->upcall_portids
, dp_nlsk_pids
);
1626 kfree_rcu(old
, rcu
);
1631 u32
ovs_dp_get_upcall_portid(const struct datapath
*dp
, uint32_t cpu_id
)
1633 struct dp_nlsk_pids
*dp_nlsk_pids
;
1635 dp_nlsk_pids
= rcu_dereference(dp
->upcall_portids
);
1638 if (cpu_id
< dp_nlsk_pids
->n_pids
) {
1639 return dp_nlsk_pids
->pids
[cpu_id
];
1640 } else if (dp_nlsk_pids
->n_pids
> 0 &&
1641 cpu_id
>= dp_nlsk_pids
->n_pids
) {
1642 /* If the number of netlink PIDs is mismatched with
1643 * the number of CPUs as seen by the kernel, log this
1644 * and send the upcall to an arbitrary socket (0) in
1645 * order to not drop packets
1647 pr_info_ratelimited("cpu_id mismatch with handler threads");
1648 return dp_nlsk_pids
->pids
[cpu_id
%
1649 dp_nlsk_pids
->n_pids
];
1658 static int ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1660 u32 user_features
= 0;
1663 if (a
[OVS_DP_ATTR_USER_FEATURES
]) {
1664 user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1666 if (user_features
& ~(OVS_DP_F_VPORT_PIDS
|
1667 OVS_DP_F_UNALIGNED
|
1668 OVS_DP_F_TC_RECIRC_SHARING
|
1669 OVS_DP_F_DISPATCH_UPCALL_PER_CPU
))
1672 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1673 if (user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1678 if (a
[OVS_DP_ATTR_MASKS_CACHE_SIZE
]) {
1682 cache_size
= nla_get_u32(a
[OVS_DP_ATTR_MASKS_CACHE_SIZE
]);
1683 err
= ovs_flow_tbl_masks_cache_resize(&dp
->table
, cache_size
);
1688 dp
->user_features
= user_features
;
1690 if (dp
->user_features
& OVS_DP_F_DISPATCH_UPCALL_PER_CPU
&&
1691 a
[OVS_DP_ATTR_PER_CPU_PIDS
]) {
1692 /* Upcall Netlink Port IDs have been updated */
1693 err
= ovs_dp_set_upcall_portids(dp
,
1694 a
[OVS_DP_ATTR_PER_CPU_PIDS
]);
1699 if (dp
->user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1700 static_branch_enable(&tc_recirc_sharing_support
);
1702 static_branch_disable(&tc_recirc_sharing_support
);
1707 static int ovs_dp_stats_init(struct datapath
*dp
)
1709 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1710 if (!dp
->stats_percpu
)
1716 static int ovs_dp_vport_init(struct datapath
*dp
)
1720 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1721 sizeof(struct hlist_head
),
1726 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1727 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1732 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1734 struct nlattr
**a
= info
->attrs
;
1735 struct vport_parms parms
;
1736 struct sk_buff
*reply
;
1737 struct datapath
*dp
;
1738 struct vport
*vport
;
1739 struct ovs_net
*ovs_net
;
1743 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1746 reply
= ovs_dp_cmd_alloc_info();
1751 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1753 goto err_destroy_reply
;
1755 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1757 /* Allocate table. */
1758 err
= ovs_flow_tbl_init(&dp
->table
);
1760 goto err_destroy_dp
;
1762 err
= ovs_dp_stats_init(dp
);
1764 goto err_destroy_table
;
1766 err
= ovs_dp_vport_init(dp
);
1768 goto err_destroy_stats
;
1770 err
= ovs_meters_init(dp
);
1772 goto err_destroy_ports
;
1774 /* Set up our datapath device. */
1775 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1776 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1777 parms
.options
= NULL
;
1779 parms
.port_no
= OVSP_LOCAL
;
1780 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1782 /* So far only local changes have been made, now need the lock. */
1785 err
= ovs_dp_change(dp
, a
);
1787 goto err_unlock_and_destroy_meters
;
1789 vport
= new_vport(&parms
);
1790 if (IS_ERR(vport
)) {
1791 err
= PTR_ERR(vport
);
1795 if (err
== -EEXIST
) {
1796 /* An outdated user space instance that does not understand
1797 * the concept of user_features has attempted to create a new
1798 * datapath and is likely to reuse it. Drop all user features.
1800 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1801 ovs_dp_reset_user_features(skb
, info
);
1804 goto err_unlock_and_destroy_meters
;
1807 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1808 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1811 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1812 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1816 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1819 err_unlock_and_destroy_meters
:
1821 ovs_meters_exit(dp
);
1825 free_percpu(dp
->stats_percpu
);
1827 ovs_flow_tbl_destroy(&dp
->table
);
1836 /* Called with ovs_mutex. */
1837 static void __dp_destroy(struct datapath
*dp
)
1839 struct flow_table
*table
= &dp
->table
;
1842 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1843 struct vport
*vport
;
1844 struct hlist_node
*n
;
1846 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1847 if (vport
->port_no
!= OVSP_LOCAL
)
1848 ovs_dp_detach_port(vport
);
1851 list_del_rcu(&dp
->list_node
);
1853 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1854 * all ports in datapath are destroyed first before freeing datapath.
1856 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1858 /* Flush sw_flow in the tables. RCU cb only releases resource
1859 * such as dp, ports and tables. That may avoid some issues
1860 * such as RCU usage warning.
1862 table_instance_flow_flush(table
, ovsl_dereference(table
->ti
),
1863 ovsl_dereference(table
->ufid_ti
));
1865 /* RCU destroy the ports, meters and flow tables. */
1866 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1869 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1871 struct sk_buff
*reply
;
1872 struct datapath
*dp
;
1875 reply
= ovs_dp_cmd_alloc_info();
1880 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1883 goto err_unlock_free
;
1885 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1886 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1892 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1902 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1904 struct sk_buff
*reply
;
1905 struct datapath
*dp
;
1908 reply
= ovs_dp_cmd_alloc_info();
1913 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1916 goto err_unlock_free
;
1918 err
= ovs_dp_change(dp
, info
->attrs
);
1920 goto err_unlock_free
;
1922 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1923 info
->snd_seq
, 0, OVS_DP_CMD_SET
);
1927 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1937 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1939 struct sk_buff
*reply
;
1940 struct datapath
*dp
;
1943 reply
= ovs_dp_cmd_alloc_info();
1948 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1951 goto err_unlock_free
;
1953 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1954 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1958 return genlmsg_reply(reply
, info
);
1966 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1968 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1969 struct datapath
*dp
;
1970 int skip
= cb
->args
[0];
1974 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1976 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1977 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1978 OVS_DP_CMD_GET
) < 0)
1989 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1990 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1991 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1992 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1993 [OVS_DP_ATTR_MASKS_CACHE_SIZE
] = NLA_POLICY_RANGE(NLA_U32
, 0,
1994 PCPU_MIN_UNIT_SIZE
/ sizeof(struct mask_cache_entry
)),
1997 static const struct genl_small_ops dp_datapath_genl_ops
[] = {
1998 { .cmd
= OVS_DP_CMD_NEW
,
1999 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2000 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2001 .doit
= ovs_dp_cmd_new
2003 { .cmd
= OVS_DP_CMD_DEL
,
2004 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2005 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2006 .doit
= ovs_dp_cmd_del
2008 { .cmd
= OVS_DP_CMD_GET
,
2009 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2010 .flags
= 0, /* OK for unprivileged users. */
2011 .doit
= ovs_dp_cmd_get
,
2012 .dumpit
= ovs_dp_cmd_dump
2014 { .cmd
= OVS_DP_CMD_SET
,
2015 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2016 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2017 .doit
= ovs_dp_cmd_set
,
2021 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
2022 .hdrsize
= sizeof(struct ovs_header
),
2023 .name
= OVS_DATAPATH_FAMILY
,
2024 .version
= OVS_DATAPATH_VERSION
,
2025 .maxattr
= OVS_DP_ATTR_MAX
,
2026 .policy
= datapath_policy
,
2028 .parallel_ops
= true,
2029 .small_ops
= dp_datapath_genl_ops
,
2030 .n_small_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
2031 .mcgrps
= &ovs_dp_datapath_multicast_group
,
2033 .module
= THIS_MODULE
,
2036 /* Called with ovs_mutex or RCU read lock. */
2037 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
2038 struct net
*net
, u32 portid
, u32 seq
,
2039 u32 flags
, u8 cmd
, gfp_t gfp
)
2041 struct ovs_header
*ovs_header
;
2042 struct ovs_vport_stats vport_stats
;
2045 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
2050 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
2052 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
2053 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
2054 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
2055 ovs_vport_name(vport
)) ||
2056 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
2057 goto nla_put_failure
;
2059 if (!net_eq(net
, dev_net(vport
->dev
))) {
2060 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
), gfp
);
2062 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
2063 goto nla_put_failure
;
2066 ovs_vport_get_stats(vport
, &vport_stats
);
2067 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
2068 sizeof(struct ovs_vport_stats
), &vport_stats
,
2069 OVS_VPORT_ATTR_PAD
))
2070 goto nla_put_failure
;
2072 if (ovs_vport_get_upcall_portids(vport
, skb
))
2073 goto nla_put_failure
;
2075 err
= ovs_vport_get_options(vport
, skb
);
2076 if (err
== -EMSGSIZE
)
2079 genlmsg_end(skb
, ovs_header
);
2085 genlmsg_cancel(skb
, ovs_header
);
2089 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
2091 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2094 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
2095 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
2096 u32 portid
, u32 seq
, u8 cmd
)
2098 struct sk_buff
*skb
;
2101 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2103 return ERR_PTR(-ENOMEM
);
2105 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
,
2112 /* Called with ovs_mutex or RCU read lock. */
2113 static struct vport
*lookup_vport(struct net
*net
,
2114 const struct ovs_header
*ovs_header
,
2115 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
2117 struct datapath
*dp
;
2118 struct vport
*vport
;
2120 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2121 return ERR_PTR(-EOPNOTSUPP
);
2122 if (a
[OVS_VPORT_ATTR_NAME
]) {
2123 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
2125 return ERR_PTR(-ENODEV
);
2126 if (ovs_header
->dp_ifindex
&&
2127 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
2128 return ERR_PTR(-ENODEV
);
2130 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
2131 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
2133 if (port_no
>= DP_MAX_PORTS
)
2134 return ERR_PTR(-EFBIG
);
2136 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
2138 return ERR_PTR(-ENODEV
);
2140 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
2142 return ERR_PTR(-ENODEV
);
2145 return ERR_PTR(-EINVAL
);
2149 static unsigned int ovs_get_max_headroom(struct datapath
*dp
)
2151 unsigned int dev_headroom
, max_headroom
= 0;
2152 struct net_device
*dev
;
2153 struct vport
*vport
;
2156 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2157 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
,
2158 lockdep_ovsl_is_held()) {
2160 dev_headroom
= netdev_get_fwd_headroom(dev
);
2161 if (dev_headroom
> max_headroom
)
2162 max_headroom
= dev_headroom
;
2166 return max_headroom
;
2169 /* Called with ovs_mutex */
2170 static void ovs_update_headroom(struct datapath
*dp
, unsigned int new_headroom
)
2172 struct vport
*vport
;
2175 dp
->max_headroom
= new_headroom
;
2176 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2177 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
,
2178 lockdep_ovsl_is_held())
2179 netdev_set_rx_headroom(vport
->dev
, new_headroom
);
2183 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2185 struct nlattr
**a
= info
->attrs
;
2186 struct ovs_header
*ovs_header
= info
->userhdr
;
2187 struct vport_parms parms
;
2188 struct sk_buff
*reply
;
2189 struct vport
*vport
;
2190 struct datapath
*dp
;
2191 unsigned int new_headroom
;
2195 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2196 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2198 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2201 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2202 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2203 if (port_no
>= DP_MAX_PORTS
)
2206 reply
= ovs_vport_cmd_alloc_info();
2212 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2215 goto exit_unlock_free
;
2218 vport
= ovs_vport_ovsl(dp
, port_no
);
2221 goto exit_unlock_free
;
2223 for (port_no
= 1; ; port_no
++) {
2224 if (port_no
>= DP_MAX_PORTS
) {
2226 goto exit_unlock_free
;
2228 vport
= ovs_vport_ovsl(dp
, port_no
);
2234 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2235 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2236 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2238 parms
.port_no
= port_no
;
2239 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2241 vport
= new_vport(&parms
);
2242 err
= PTR_ERR(vport
);
2243 if (IS_ERR(vport
)) {
2246 goto exit_unlock_free
;
2249 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2250 info
->snd_portid
, info
->snd_seq
, 0,
2251 OVS_VPORT_CMD_NEW
, GFP_KERNEL
);
2253 new_headroom
= netdev_get_fwd_headroom(vport
->dev
);
2255 if (new_headroom
> dp
->max_headroom
)
2256 ovs_update_headroom(dp
, new_headroom
);
2258 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2263 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2272 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2274 struct nlattr
**a
= info
->attrs
;
2275 struct sk_buff
*reply
;
2276 struct vport
*vport
;
2279 reply
= ovs_vport_cmd_alloc_info();
2284 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2285 err
= PTR_ERR(vport
);
2287 goto exit_unlock_free
;
2289 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2290 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2292 goto exit_unlock_free
;
2295 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2296 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2298 goto exit_unlock_free
;
2302 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2303 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2305 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2307 goto exit_unlock_free
;
2310 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2311 info
->snd_portid
, info
->snd_seq
, 0,
2312 OVS_VPORT_CMD_SET
, GFP_KERNEL
);
2316 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2325 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2327 bool update_headroom
= false;
2328 struct nlattr
**a
= info
->attrs
;
2329 struct sk_buff
*reply
;
2330 struct datapath
*dp
;
2331 struct vport
*vport
;
2332 unsigned int new_headroom
;
2335 reply
= ovs_vport_cmd_alloc_info();
2340 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2341 err
= PTR_ERR(vport
);
2343 goto exit_unlock_free
;
2345 if (vport
->port_no
== OVSP_LOCAL
) {
2347 goto exit_unlock_free
;
2350 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2351 info
->snd_portid
, info
->snd_seq
, 0,
2352 OVS_VPORT_CMD_DEL
, GFP_KERNEL
);
2355 /* the vport deletion may trigger dp headroom update */
2357 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2358 update_headroom
= true;
2360 netdev_reset_rx_headroom(vport
->dev
);
2361 ovs_dp_detach_port(vport
);
2363 if (update_headroom
) {
2364 new_headroom
= ovs_get_max_headroom(dp
);
2366 if (new_headroom
< dp
->max_headroom
)
2367 ovs_update_headroom(dp
, new_headroom
);
2371 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2380 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2382 struct nlattr
**a
= info
->attrs
;
2383 struct ovs_header
*ovs_header
= info
->userhdr
;
2384 struct sk_buff
*reply
;
2385 struct vport
*vport
;
2388 reply
= ovs_vport_cmd_alloc_info();
2393 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2394 err
= PTR_ERR(vport
);
2396 goto exit_unlock_free
;
2397 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2398 info
->snd_portid
, info
->snd_seq
, 0,
2399 OVS_VPORT_CMD_GET
, GFP_ATOMIC
);
2403 return genlmsg_reply(reply
, info
);
2411 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2413 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2414 struct datapath
*dp
;
2415 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2419 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2424 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2425 struct vport
*vport
;
2428 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2430 ovs_vport_cmd_fill_info(vport
, skb
,
2432 NETLINK_CB(cb
->skb
).portid
,
2452 static void ovs_dp_masks_rebalance(struct work_struct
*work
)
2454 struct ovs_net
*ovs_net
= container_of(work
, struct ovs_net
,
2455 masks_rebalance
.work
);
2456 struct datapath
*dp
;
2460 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
)
2461 ovs_flow_masks_rebalance(&dp
->table
);
2465 schedule_delayed_work(&ovs_net
->masks_rebalance
,
2466 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL
));
2469 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2470 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2471 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2472 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2473 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2474 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2475 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2476 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2477 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2480 static const struct genl_small_ops dp_vport_genl_ops
[] = {
2481 { .cmd
= OVS_VPORT_CMD_NEW
,
2482 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2483 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2484 .doit
= ovs_vport_cmd_new
2486 { .cmd
= OVS_VPORT_CMD_DEL
,
2487 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2488 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2489 .doit
= ovs_vport_cmd_del
2491 { .cmd
= OVS_VPORT_CMD_GET
,
2492 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2493 .flags
= 0, /* OK for unprivileged users. */
2494 .doit
= ovs_vport_cmd_get
,
2495 .dumpit
= ovs_vport_cmd_dump
2497 { .cmd
= OVS_VPORT_CMD_SET
,
2498 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2499 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2500 .doit
= ovs_vport_cmd_set
,
2504 struct genl_family dp_vport_genl_family __ro_after_init
= {
2505 .hdrsize
= sizeof(struct ovs_header
),
2506 .name
= OVS_VPORT_FAMILY
,
2507 .version
= OVS_VPORT_VERSION
,
2508 .maxattr
= OVS_VPORT_ATTR_MAX
,
2509 .policy
= vport_policy
,
2511 .parallel_ops
= true,
2512 .small_ops
= dp_vport_genl_ops
,
2513 .n_small_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2514 .mcgrps
= &ovs_dp_vport_multicast_group
,
2516 .module
= THIS_MODULE
,
2519 static struct genl_family
* const dp_genl_families
[] = {
2520 &dp_datapath_genl_family
,
2521 &dp_vport_genl_family
,
2522 &dp_flow_genl_family
,
2523 &dp_packet_genl_family
,
2524 &dp_meter_genl_family
,
2525 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2526 &dp_ct_limit_genl_family
,
2530 static void dp_unregister_genl(int n_families
)
2534 for (i
= 0; i
< n_families
; i
++)
2535 genl_unregister_family(dp_genl_families
[i
]);
2538 static int __init
dp_register_genl(void)
2543 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2545 err
= genl_register_family(dp_genl_families
[i
]);
2553 dp_unregister_genl(i
);
2557 static int __net_init
ovs_init_net(struct net
*net
)
2559 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2562 INIT_LIST_HEAD(&ovs_net
->dps
);
2563 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2564 INIT_DELAYED_WORK(&ovs_net
->masks_rebalance
, ovs_dp_masks_rebalance
);
2566 err
= ovs_ct_init(net
);
2570 schedule_delayed_work(&ovs_net
->masks_rebalance
,
2571 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL
));
2575 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2576 struct list_head
*head
)
2578 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2579 struct datapath
*dp
;
2581 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2584 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2585 struct vport
*vport
;
2587 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2588 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2591 if (dev_net(vport
->dev
) == dnet
)
2592 list_add(&vport
->detach_list
, head
);
2598 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2600 struct datapath
*dp
, *dp_next
;
2601 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2602 struct vport
*vport
, *vport_next
;
2610 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2613 down_read(&net_rwsem
);
2615 list_vports_from_net(net
, dnet
, &head
);
2616 up_read(&net_rwsem
);
2618 /* Detach all vports from given namespace. */
2619 list_for_each_entry_safe(vport
, vport_next
, &head
, detach_list
) {
2620 list_del(&vport
->detach_list
);
2621 ovs_dp_detach_port(vport
);
2626 cancel_delayed_work_sync(&ovs_net
->masks_rebalance
);
2627 cancel_work_sync(&ovs_net
->dp_notify_work
);
2630 static struct pernet_operations ovs_net_ops
= {
2631 .init
= ovs_init_net
,
2632 .exit
= ovs_exit_net
,
2634 .size
= sizeof(struct ovs_net
),
2637 static int __init
dp_init(void)
2641 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) >
2642 sizeof_field(struct sk_buff
, cb
));
2644 pr_info("Open vSwitch switching datapath\n");
2646 err
= action_fifos_init();
2650 err
= ovs_internal_dev_rtnl_link_register();
2652 goto error_action_fifos_exit
;
2654 err
= ovs_flow_init();
2656 goto error_unreg_rtnl_link
;
2658 err
= ovs_vport_init();
2660 goto error_flow_exit
;
2662 err
= register_pernet_device(&ovs_net_ops
);
2664 goto error_vport_exit
;
2666 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2668 goto error_netns_exit
;
2670 err
= ovs_netdev_init();
2672 goto error_unreg_notifier
;
2674 err
= dp_register_genl();
2676 goto error_unreg_netdev
;
2682 error_unreg_notifier
:
2683 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2685 unregister_pernet_device(&ovs_net_ops
);
2690 error_unreg_rtnl_link
:
2691 ovs_internal_dev_rtnl_link_unregister();
2692 error_action_fifos_exit
:
2693 action_fifos_exit();
2698 static void dp_cleanup(void)
2700 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2702 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2703 unregister_pernet_device(&ovs_net_ops
);
2707 ovs_internal_dev_rtnl_link_unregister();
2708 action_fifos_exit();
2711 module_init(dp_init
);
2712 module_exit(dp_cleanup
);
2714 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2715 MODULE_LICENSE("GPL");
2716 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY
);
2717 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY
);
2718 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY
);
2719 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY
);
2720 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY
);
2721 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY
);