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 static void ovs_dp_masks_rebalance(struct work_struct
*work
);
135 /* Must be called with rcu_read_lock or ovs_mutex. */
136 const char *ovs_dp_name(const struct datapath
*dp
)
138 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
139 return ovs_vport_name(vport
);
142 static int get_dpifindex(const struct datapath
*dp
)
149 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
151 ifindex
= local
->dev
->ifindex
;
160 static void destroy_dp_rcu(struct rcu_head
*rcu
)
162 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
164 ovs_flow_tbl_destroy(&dp
->table
);
165 free_percpu(dp
->stats_percpu
);
171 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
174 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
177 /* Called with ovs_mutex or RCU read lock. */
178 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
181 struct hlist_head
*head
;
183 head
= vport_hash_bucket(dp
, port_no
);
184 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
,
185 lockdep_ovsl_is_held()) {
186 if (vport
->port_no
== port_no
)
192 /* Called with ovs_mutex. */
193 static struct vport
*new_vport(const struct vport_parms
*parms
)
197 vport
= ovs_vport_add(parms
);
198 if (!IS_ERR(vport
)) {
199 struct datapath
*dp
= parms
->dp
;
200 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
202 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
207 void ovs_dp_detach_port(struct vport
*p
)
211 /* First drop references to device. */
212 hlist_del_rcu(&p
->dp_hash_node
);
214 /* Then destroy it. */
218 /* Must be called with rcu_read_lock. */
219 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
221 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
222 struct datapath
*dp
= p
->dp
;
223 struct sw_flow
*flow
;
224 struct sw_flow_actions
*sf_acts
;
225 struct dp_stats_percpu
*stats
;
230 stats
= this_cpu_ptr(dp
->stats_percpu
);
233 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, skb_get_hash(skb
),
235 if (unlikely(!flow
)) {
236 struct dp_upcall_info upcall
;
238 memset(&upcall
, 0, sizeof(upcall
));
239 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
240 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
241 upcall
.mru
= OVS_CB(skb
)->mru
;
242 error
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, 0);
247 stats_counter
= &stats
->n_missed
;
251 ovs_flow_stats_update(flow
, key
->tp
.flags
, skb
);
252 sf_acts
= rcu_dereference(flow
->sf_acts
);
253 error
= ovs_execute_actions(dp
, skb
, sf_acts
, key
);
255 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
256 ovs_dp_name(dp
), error
);
258 stats_counter
= &stats
->n_hit
;
261 /* Update datapath statistics. */
262 u64_stats_update_begin(&stats
->syncp
);
264 stats
->n_mask_hit
+= n_mask_hit
;
265 u64_stats_update_end(&stats
->syncp
);
268 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
269 const struct sw_flow_key
*key
,
270 const struct dp_upcall_info
*upcall_info
,
273 struct dp_stats_percpu
*stats
;
276 if (upcall_info
->portid
== 0) {
281 if (!skb_is_gso(skb
))
282 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
284 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
, cutlen
);
291 stats
= this_cpu_ptr(dp
->stats_percpu
);
293 u64_stats_update_begin(&stats
->syncp
);
295 u64_stats_update_end(&stats
->syncp
);
300 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
301 const struct sw_flow_key
*key
,
302 const struct dp_upcall_info
*upcall_info
,
305 unsigned int gso_type
= skb_shinfo(skb
)->gso_type
;
306 struct sw_flow_key later_key
;
307 struct sk_buff
*segs
, *nskb
;
310 BUILD_BUG_ON(sizeof(*OVS_CB(skb
)) > SKB_GSO_CB_OFFSET
);
311 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
313 return PTR_ERR(segs
);
317 if (gso_type
& SKB_GSO_UDP
) {
318 /* The initial flow key extracted by ovs_flow_key_extract()
319 * in this case is for a first fragment, so we need to
320 * properly mark later fragments.
323 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
326 /* Queue all of the segments. */
327 skb_list_walk_safe(segs
, skb
, nskb
) {
328 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
331 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
, cutlen
);
337 /* Free all of the segments. */
338 skb_list_walk_safe(segs
, skb
, nskb
) {
347 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
348 unsigned int hdrlen
, int actions_attrlen
)
350 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
351 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
352 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
353 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
354 + nla_total_size(sizeof(u64
)); /* OVS_PACKET_ATTR_HASH */
356 /* OVS_PACKET_ATTR_USERDATA */
357 if (upcall_info
->userdata
)
358 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
360 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
361 if (upcall_info
->egress_tun_info
)
362 size
+= nla_total_size(ovs_tun_key_attr_size());
364 /* OVS_PACKET_ATTR_ACTIONS */
365 if (upcall_info
->actions_len
)
366 size
+= nla_total_size(actions_attrlen
);
368 /* OVS_PACKET_ATTR_MRU */
369 if (upcall_info
->mru
)
370 size
+= nla_total_size(sizeof(upcall_info
->mru
));
375 static void pad_packet(struct datapath
*dp
, struct sk_buff
*skb
)
377 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
378 size_t plen
= NLA_ALIGN(skb
->len
) - skb
->len
;
381 skb_put_zero(skb
, plen
);
385 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
386 const struct sw_flow_key
*key
,
387 const struct dp_upcall_info
*upcall_info
,
390 struct ovs_header
*upcall
;
391 struct sk_buff
*nskb
= NULL
;
392 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
399 dp_ifindex
= get_dpifindex(dp
);
403 if (skb_vlan_tag_present(skb
)) {
404 nskb
= skb_clone(skb
, GFP_ATOMIC
);
408 nskb
= __vlan_hwaccel_push_inside(nskb
);
415 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
420 /* Complete checksum if needed */
421 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
422 (err
= skb_csum_hwoffload_help(skb
, 0)))
425 /* Older versions of OVS user space enforce alignment of the last
426 * Netlink attribute to NLA_ALIGNTO which would require extensive
427 * padding logic. Only perform zerocopy if padding is not required.
429 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
430 hlen
= skb_zerocopy_headlen(skb
);
434 len
= upcall_msg_size(upcall_info
, hlen
- cutlen
,
435 OVS_CB(skb
)->acts_origlen
);
436 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
442 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
443 0, upcall_info
->cmd
);
448 upcall
->dp_ifindex
= dp_ifindex
;
450 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
454 if (upcall_info
->userdata
)
455 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
456 nla_len(upcall_info
->userdata
),
457 nla_data(upcall_info
->userdata
));
459 if (upcall_info
->egress_tun_info
) {
460 nla
= nla_nest_start_noflag(user_skb
,
461 OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
466 err
= ovs_nla_put_tunnel_info(user_skb
,
467 upcall_info
->egress_tun_info
);
471 nla_nest_end(user_skb
, nla
);
474 if (upcall_info
->actions_len
) {
475 nla
= nla_nest_start_noflag(user_skb
, OVS_PACKET_ATTR_ACTIONS
);
480 err
= ovs_nla_put_actions(upcall_info
->actions
,
481 upcall_info
->actions_len
,
484 nla_nest_end(user_skb
, nla
);
486 nla_nest_cancel(user_skb
, nla
);
489 /* Add OVS_PACKET_ATTR_MRU */
490 if (upcall_info
->mru
&&
491 nla_put_u16(user_skb
, OVS_PACKET_ATTR_MRU
, upcall_info
->mru
)) {
496 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
498 nla_put_u32(user_skb
, OVS_PACKET_ATTR_LEN
, skb
->len
)) {
503 /* Add OVS_PACKET_ATTR_HASH */
504 hash
= skb_get_hash_raw(skb
);
506 hash
|= OVS_PACKET_HASH_SW_BIT
;
509 hash
|= OVS_PACKET_HASH_L4_BIT
;
511 if (nla_put(user_skb
, OVS_PACKET_ATTR_HASH
, sizeof (u64
), &hash
)) {
516 /* Only reserve room for attribute header, packet data is added
517 * in skb_zerocopy() */
518 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
522 nla
->nla_len
= nla_attr_size(skb
->len
- cutlen
);
524 err
= skb_zerocopy(user_skb
, skb
, skb
->len
- cutlen
, hlen
);
528 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
529 pad_packet(dp
, user_skb
);
531 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
533 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
543 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
545 struct ovs_header
*ovs_header
= info
->userhdr
;
546 struct net
*net
= sock_net(skb
->sk
);
547 struct nlattr
**a
= info
->attrs
;
548 struct sw_flow_actions
*acts
;
549 struct sk_buff
*packet
;
550 struct sw_flow
*flow
;
551 struct sw_flow_actions
*sf_acts
;
553 struct vport
*input_vport
;
558 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
561 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
562 !a
[OVS_PACKET_ATTR_ACTIONS
])
565 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
566 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
570 skb_reserve(packet
, NET_IP_ALIGN
);
572 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
574 /* Set packet's mru */
575 if (a
[OVS_PACKET_ATTR_MRU
]) {
576 mru
= nla_get_u16(a
[OVS_PACKET_ATTR_MRU
]);
577 packet
->ignore_df
= 1;
579 OVS_CB(packet
)->mru
= mru
;
581 if (a
[OVS_PACKET_ATTR_HASH
]) {
582 hash
= nla_get_u64(a
[OVS_PACKET_ATTR_HASH
]);
584 __skb_set_hash(packet
, hash
& 0xFFFFFFFFULL
,
585 !!(hash
& OVS_PACKET_HASH_SW_BIT
),
586 !!(hash
& OVS_PACKET_HASH_L4_BIT
));
589 /* Build an sw_flow for sending this packet. */
590 flow
= ovs_flow_alloc();
595 err
= ovs_flow_key_extract_userspace(net
, a
[OVS_PACKET_ATTR_KEY
],
596 packet
, &flow
->key
, log
);
600 err
= ovs_nla_copy_actions(net
, a
[OVS_PACKET_ATTR_ACTIONS
],
601 &flow
->key
, &acts
, log
);
605 rcu_assign_pointer(flow
->sf_acts
, acts
);
606 packet
->priority
= flow
->key
.phy
.priority
;
607 packet
->mark
= flow
->key
.phy
.skb_mark
;
610 dp
= get_dp_rcu(net
, ovs_header
->dp_ifindex
);
615 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
617 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
622 packet
->dev
= input_vport
->dev
;
623 OVS_CB(packet
)->input_vport
= input_vport
;
624 sf_acts
= rcu_dereference(flow
->sf_acts
);
627 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
631 ovs_flow_free(flow
, false);
637 ovs_flow_free(flow
, false);
644 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
645 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
646 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
647 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
648 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
649 [OVS_PACKET_ATTR_MRU
] = { .type
= NLA_U16
},
650 [OVS_PACKET_ATTR_HASH
] = { .type
= NLA_U64
},
653 static const struct genl_ops dp_packet_genl_ops
[] = {
654 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
655 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
656 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
657 .doit
= ovs_packet_cmd_execute
661 static struct genl_family dp_packet_genl_family __ro_after_init
= {
662 .hdrsize
= sizeof(struct ovs_header
),
663 .name
= OVS_PACKET_FAMILY
,
664 .version
= OVS_PACKET_VERSION
,
665 .maxattr
= OVS_PACKET_ATTR_MAX
,
666 .policy
= packet_policy
,
668 .parallel_ops
= true,
669 .ops
= dp_packet_genl_ops
,
670 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
671 .module
= THIS_MODULE
,
674 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
675 struct ovs_dp_megaflow_stats
*mega_stats
)
679 memset(mega_stats
, 0, sizeof(*mega_stats
));
681 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
682 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
684 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
686 for_each_possible_cpu(i
) {
687 const struct dp_stats_percpu
*percpu_stats
;
688 struct dp_stats_percpu local_stats
;
691 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
694 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
695 local_stats
= *percpu_stats
;
696 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
698 stats
->n_hit
+= local_stats
.n_hit
;
699 stats
->n_missed
+= local_stats
.n_missed
;
700 stats
->n_lost
+= local_stats
.n_lost
;
701 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
705 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
707 return ovs_identifier_is_ufid(sfid
) &&
708 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
711 static bool should_fill_mask(uint32_t ufid_flags
)
713 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
716 static bool should_fill_actions(uint32_t ufid_flags
)
718 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
721 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
722 const struct sw_flow_id
*sfid
,
725 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
727 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
728 * see ovs_nla_put_identifier()
730 if (sfid
&& ovs_identifier_is_ufid(sfid
))
731 len
+= nla_total_size(sfid
->ufid_len
);
733 len
+= nla_total_size(ovs_key_attr_size());
735 /* OVS_FLOW_ATTR_KEY */
736 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
737 len
+= nla_total_size(ovs_key_attr_size());
739 /* OVS_FLOW_ATTR_MASK */
740 if (should_fill_mask(ufid_flags
))
741 len
+= nla_total_size(ovs_key_attr_size());
743 /* OVS_FLOW_ATTR_ACTIONS */
744 if (should_fill_actions(ufid_flags
))
745 len
+= nla_total_size(acts
->orig_len
);
748 + nla_total_size_64bit(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
749 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
750 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
753 /* Called with ovs_mutex or RCU read lock. */
754 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
757 struct ovs_flow_stats stats
;
761 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
764 nla_put_u64_64bit(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
),
768 if (stats
.n_packets
&&
769 nla_put_64bit(skb
, OVS_FLOW_ATTR_STATS
,
770 sizeof(struct ovs_flow_stats
), &stats
,
774 if ((u8
)ntohs(tcp_flags
) &&
775 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
781 /* Called with ovs_mutex or RCU read lock. */
782 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
783 struct sk_buff
*skb
, int skb_orig_len
)
785 struct nlattr
*start
;
788 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
789 * this is the first flow to be dumped into 'skb'. This is unusual for
790 * Netlink but individual action lists can be longer than
791 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
792 * The userspace caller can always fetch the actions separately if it
793 * really wants them. (Most userspace callers in fact don't care.)
795 * This can only fail for dump operations because the skb is always
796 * properly sized for single flows.
798 start
= nla_nest_start_noflag(skb
, OVS_FLOW_ATTR_ACTIONS
);
800 const struct sw_flow_actions
*sf_acts
;
802 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
803 err
= ovs_nla_put_actions(sf_acts
->actions
,
804 sf_acts
->actions_len
, skb
);
807 nla_nest_end(skb
, start
);
812 nla_nest_cancel(skb
, start
);
814 } else if (skb_orig_len
) {
821 /* Called with ovs_mutex or RCU read lock. */
822 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
823 struct sk_buff
*skb
, u32 portid
,
824 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
826 const int skb_orig_len
= skb
->len
;
827 struct ovs_header
*ovs_header
;
830 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
835 ovs_header
->dp_ifindex
= dp_ifindex
;
837 err
= ovs_nla_put_identifier(flow
, skb
);
841 if (should_fill_key(&flow
->id
, ufid_flags
)) {
842 err
= ovs_nla_put_masked_key(flow
, skb
);
847 if (should_fill_mask(ufid_flags
)) {
848 err
= ovs_nla_put_mask(flow
, skb
);
853 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
857 if (should_fill_actions(ufid_flags
)) {
858 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
863 genlmsg_end(skb
, ovs_header
);
867 genlmsg_cancel(skb
, ovs_header
);
871 /* May not be called with RCU read lock. */
872 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
873 const struct sw_flow_id
*sfid
,
874 struct genl_info
*info
,
881 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
, 0))
884 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
885 skb
= genlmsg_new(len
, GFP_KERNEL
);
887 return ERR_PTR(-ENOMEM
);
892 /* Called with ovs_mutex. */
893 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
895 struct genl_info
*info
, u8 cmd
,
896 bool always
, u32 ufid_flags
)
901 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
902 &flow
->id
, info
, always
, ufid_flags
);
903 if (IS_ERR_OR_NULL(skb
))
906 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
907 info
->snd_portid
, info
->snd_seq
, 0,
909 if (WARN_ON_ONCE(retval
< 0)) {
911 skb
= ERR_PTR(retval
);
916 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
918 struct net
*net
= sock_net(skb
->sk
);
919 struct nlattr
**a
= info
->attrs
;
920 struct ovs_header
*ovs_header
= info
->userhdr
;
921 struct sw_flow
*flow
= NULL
, *new_flow
;
922 struct sw_flow_mask mask
;
923 struct sk_buff
*reply
;
925 struct sw_flow_actions
*acts
;
926 struct sw_flow_match match
;
927 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
929 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
931 /* Must have key and actions. */
933 if (!a
[OVS_FLOW_ATTR_KEY
]) {
934 OVS_NLERR(log
, "Flow key attr not present in new flow.");
937 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
938 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
942 /* Most of the time we need to allocate a new flow, do it before
945 new_flow
= ovs_flow_alloc();
946 if (IS_ERR(new_flow
)) {
947 error
= PTR_ERR(new_flow
);
952 ovs_match_init(&match
, &new_flow
->key
, false, &mask
);
953 error
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
954 a
[OVS_FLOW_ATTR_MASK
], log
);
958 /* Extract flow identifier. */
959 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
960 &new_flow
->key
, log
);
964 /* unmasked key is needed to match when ufid is not used. */
965 if (ovs_identifier_is_key(&new_flow
->id
))
966 match
.key
= new_flow
->id
.unmasked_key
;
968 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->key
, true, &mask
);
970 /* Validate actions. */
971 error
= ovs_nla_copy_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
],
972 &new_flow
->key
, &acts
, log
);
974 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
978 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
981 error
= PTR_ERR(reply
);
986 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
992 /* Check if this is a duplicate flow */
993 if (ovs_identifier_is_ufid(&new_flow
->id
))
994 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
996 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->key
);
998 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
1000 /* Put flow in bucket. */
1001 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
1002 if (unlikely(error
)) {
1004 goto err_unlock_ovs
;
1007 if (unlikely(reply
)) {
1008 error
= ovs_flow_cmd_fill_info(new_flow
,
1009 ovs_header
->dp_ifindex
,
1010 reply
, info
->snd_portid
,
1018 struct sw_flow_actions
*old_acts
;
1020 /* Bail out if we're not allowed to modify an existing flow.
1021 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1022 * because Generic Netlink treats the latter as a dump
1023 * request. We also accept NLM_F_EXCL in case that bug ever
1026 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
1029 goto err_unlock_ovs
;
1031 /* The flow identifier has to be the same for flow updates.
1032 * Look for any overlapping flow.
1034 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
1035 if (ovs_identifier_is_key(&flow
->id
))
1036 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
1038 else /* UFID matches but key is different */
1042 goto err_unlock_ovs
;
1045 /* Update actions. */
1046 old_acts
= ovsl_dereference(flow
->sf_acts
);
1047 rcu_assign_pointer(flow
->sf_acts
, acts
);
1049 if (unlikely(reply
)) {
1050 error
= ovs_flow_cmd_fill_info(flow
,
1051 ovs_header
->dp_ifindex
,
1052 reply
, info
->snd_portid
,
1060 ovs_nla_free_flow_actions_rcu(old_acts
);
1061 ovs_flow_free(new_flow
, false);
1065 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1072 ovs_nla_free_flow_actions(acts
);
1074 ovs_flow_free(new_flow
, false);
1079 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1080 static noinline_for_stack
struct sw_flow_actions
*get_flow_actions(struct net
*net
,
1081 const struct nlattr
*a
,
1082 const struct sw_flow_key
*key
,
1083 const struct sw_flow_mask
*mask
,
1086 struct sw_flow_actions
*acts
;
1087 struct sw_flow_key masked_key
;
1090 ovs_flow_mask_key(&masked_key
, key
, true, mask
);
1091 error
= ovs_nla_copy_actions(net
, a
, &masked_key
, &acts
, log
);
1094 "Actions may not be safe on all matching packets");
1095 return ERR_PTR(error
);
1101 /* Factor out match-init and action-copy to avoid
1102 * "Wframe-larger-than=1024" warning. Because mask is only
1103 * used to get actions, we new a function to save some
1106 * If there are not key and action attrs, we return 0
1107 * directly. In the case, the caller will also not use the
1108 * match as before. If there is action attr, we try to get
1109 * actions and save them to *acts. Before returning from
1110 * the function, we reset the match->mask pointer. Because
1111 * we should not to return match object with dangling reference
1114 static noinline_for_stack
int
1115 ovs_nla_init_match_and_action(struct net
*net
,
1116 struct sw_flow_match
*match
,
1117 struct sw_flow_key
*key
,
1119 struct sw_flow_actions
**acts
,
1122 struct sw_flow_mask mask
;
1125 if (a
[OVS_FLOW_ATTR_KEY
]) {
1126 ovs_match_init(match
, key
, true, &mask
);
1127 error
= ovs_nla_get_match(net
, match
, a
[OVS_FLOW_ATTR_KEY
],
1128 a
[OVS_FLOW_ATTR_MASK
], log
);
1133 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1134 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1136 "Flow key attribute not present in set flow.");
1141 *acts
= get_flow_actions(net
, a
[OVS_FLOW_ATTR_ACTIONS
], key
,
1143 if (IS_ERR(*acts
)) {
1144 error
= PTR_ERR(*acts
);
1149 /* On success, error is 0. */
1155 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1157 struct net
*net
= sock_net(skb
->sk
);
1158 struct nlattr
**a
= info
->attrs
;
1159 struct ovs_header
*ovs_header
= info
->userhdr
;
1160 struct sw_flow_key key
;
1161 struct sw_flow
*flow
;
1162 struct sk_buff
*reply
= NULL
;
1163 struct datapath
*dp
;
1164 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1165 struct sw_flow_match match
;
1166 struct sw_flow_id sfid
;
1167 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1169 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1172 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1173 if (!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
) {
1175 "Flow set message rejected, Key attribute missing.");
1179 error
= ovs_nla_init_match_and_action(net
, &match
, &key
, a
,
1185 /* Can allocate before locking if have acts. */
1186 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1188 if (IS_ERR(reply
)) {
1189 error
= PTR_ERR(reply
);
1190 goto err_kfree_acts
;
1195 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1196 if (unlikely(!dp
)) {
1198 goto err_unlock_ovs
;
1200 /* Check that the flow exists. */
1202 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1204 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1205 if (unlikely(!flow
)) {
1207 goto err_unlock_ovs
;
1210 /* Update actions, if present. */
1212 old_acts
= ovsl_dereference(flow
->sf_acts
);
1213 rcu_assign_pointer(flow
->sf_acts
, acts
);
1215 if (unlikely(reply
)) {
1216 error
= ovs_flow_cmd_fill_info(flow
,
1217 ovs_header
->dp_ifindex
,
1218 reply
, info
->snd_portid
,
1225 /* Could not alloc without acts before locking. */
1226 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1227 info
, OVS_FLOW_CMD_SET
, false,
1230 if (IS_ERR(reply
)) {
1231 error
= PTR_ERR(reply
);
1232 goto err_unlock_ovs
;
1237 if (a
[OVS_FLOW_ATTR_CLEAR
])
1238 ovs_flow_stats_clear(flow
);
1242 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1244 ovs_nla_free_flow_actions_rcu(old_acts
);
1252 ovs_nla_free_flow_actions(acts
);
1257 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1259 struct nlattr
**a
= info
->attrs
;
1260 struct ovs_header
*ovs_header
= info
->userhdr
;
1261 struct net
*net
= sock_net(skb
->sk
);
1262 struct sw_flow_key key
;
1263 struct sk_buff
*reply
;
1264 struct sw_flow
*flow
;
1265 struct datapath
*dp
;
1266 struct sw_flow_match match
;
1267 struct sw_flow_id ufid
;
1268 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1270 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1273 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1274 if (a
[OVS_FLOW_ATTR_KEY
]) {
1275 ovs_match_init(&match
, &key
, true, NULL
);
1276 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1278 } else if (!ufid_present
) {
1280 "Flow get message rejected, Key attribute missing.");
1287 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1294 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1296 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1302 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1303 OVS_FLOW_CMD_GET
, true, ufid_flags
);
1304 if (IS_ERR(reply
)) {
1305 err
= PTR_ERR(reply
);
1310 return genlmsg_reply(reply
, info
);
1316 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1318 struct nlattr
**a
= info
->attrs
;
1319 struct ovs_header
*ovs_header
= info
->userhdr
;
1320 struct net
*net
= sock_net(skb
->sk
);
1321 struct sw_flow_key key
;
1322 struct sk_buff
*reply
;
1323 struct sw_flow
*flow
= NULL
;
1324 struct datapath
*dp
;
1325 struct sw_flow_match match
;
1326 struct sw_flow_id ufid
;
1327 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1329 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1332 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1333 if (a
[OVS_FLOW_ATTR_KEY
]) {
1334 ovs_match_init(&match
, &key
, true, NULL
);
1335 err
= ovs_nla_get_match(net
, &match
, a
[OVS_FLOW_ATTR_KEY
],
1342 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1343 if (unlikely(!dp
)) {
1348 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1349 err
= ovs_flow_tbl_flush(&dp
->table
);
1354 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1356 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1357 if (unlikely(!flow
)) {
1362 ovs_flow_tbl_remove(&dp
->table
, flow
);
1365 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1366 &flow
->id
, info
, false, ufid_flags
);
1367 if (likely(reply
)) {
1368 if (!IS_ERR(reply
)) {
1369 rcu_read_lock(); /*To keep RCU checker happy. */
1370 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1371 reply
, info
->snd_portid
,
1376 if (WARN_ON_ONCE(err
< 0)) {
1381 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1383 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1388 ovs_flow_free(flow
, true);
1395 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1397 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1398 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1399 struct table_instance
*ti
;
1400 struct datapath
*dp
;
1404 err
= genlmsg_parse_deprecated(cb
->nlh
, &dp_flow_genl_family
, a
,
1405 OVS_FLOW_ATTR_MAX
, flow_policy
, NULL
);
1408 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1411 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1417 ti
= rcu_dereference(dp
->table
.ti
);
1419 struct sw_flow
*flow
;
1422 bucket
= cb
->args
[0];
1424 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1428 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1429 NETLINK_CB(cb
->skb
).portid
,
1430 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1431 OVS_FLOW_CMD_GET
, ufid_flags
) < 0)
1434 cb
->args
[0] = bucket
;
1441 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1442 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1443 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1444 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1445 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1446 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1447 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1448 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1451 static const struct genl_ops dp_flow_genl_ops
[] = {
1452 { .cmd
= OVS_FLOW_CMD_NEW
,
1453 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1454 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1455 .doit
= ovs_flow_cmd_new
1457 { .cmd
= OVS_FLOW_CMD_DEL
,
1458 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1459 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1460 .doit
= ovs_flow_cmd_del
1462 { .cmd
= OVS_FLOW_CMD_GET
,
1463 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1464 .flags
= 0, /* OK for unprivileged users. */
1465 .doit
= ovs_flow_cmd_get
,
1466 .dumpit
= ovs_flow_cmd_dump
1468 { .cmd
= OVS_FLOW_CMD_SET
,
1469 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1470 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1471 .doit
= ovs_flow_cmd_set
,
1475 static struct genl_family dp_flow_genl_family __ro_after_init
= {
1476 .hdrsize
= sizeof(struct ovs_header
),
1477 .name
= OVS_FLOW_FAMILY
,
1478 .version
= OVS_FLOW_VERSION
,
1479 .maxattr
= OVS_FLOW_ATTR_MAX
,
1480 .policy
= flow_policy
,
1482 .parallel_ops
= true,
1483 .ops
= dp_flow_genl_ops
,
1484 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1485 .mcgrps
= &ovs_dp_flow_multicast_group
,
1487 .module
= THIS_MODULE
,
1490 static size_t ovs_dp_cmd_msg_size(void)
1492 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1494 msgsize
+= nla_total_size(IFNAMSIZ
);
1495 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_stats
));
1496 msgsize
+= nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats
));
1497 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1502 /* Called with ovs_mutex. */
1503 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1504 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1506 struct ovs_header
*ovs_header
;
1507 struct ovs_dp_stats dp_stats
;
1508 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1511 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1516 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1518 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1520 goto nla_put_failure
;
1522 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1523 if (nla_put_64bit(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1524 &dp_stats
, OVS_DP_ATTR_PAD
))
1525 goto nla_put_failure
;
1527 if (nla_put_64bit(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1528 sizeof(struct ovs_dp_megaflow_stats
),
1529 &dp_megaflow_stats
, OVS_DP_ATTR_PAD
))
1530 goto nla_put_failure
;
1532 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1533 goto nla_put_failure
;
1535 genlmsg_end(skb
, ovs_header
);
1539 genlmsg_cancel(skb
, ovs_header
);
1544 static struct sk_buff
*ovs_dp_cmd_alloc_info(void)
1546 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL
);
1549 /* Called with rcu_read_lock or ovs_mutex. */
1550 static struct datapath
*lookup_datapath(struct net
*net
,
1551 const struct ovs_header
*ovs_header
,
1552 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1554 struct datapath
*dp
;
1556 if (!a
[OVS_DP_ATTR_NAME
])
1557 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1559 struct vport
*vport
;
1561 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1562 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1564 return dp
? dp
: ERR_PTR(-ENODEV
);
1567 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1569 struct datapath
*dp
;
1571 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1575 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1576 dp
->user_features
= 0;
1579 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support
);
1581 static int ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1583 u32 user_features
= 0;
1585 if (a
[OVS_DP_ATTR_USER_FEATURES
]) {
1586 user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1588 if (user_features
& ~(OVS_DP_F_VPORT_PIDS
|
1589 OVS_DP_F_UNALIGNED
|
1590 OVS_DP_F_TC_RECIRC_SHARING
))
1593 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1594 if (user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1599 dp
->user_features
= user_features
;
1601 if (dp
->user_features
& OVS_DP_F_TC_RECIRC_SHARING
)
1602 static_branch_enable(&tc_recirc_sharing_support
);
1604 static_branch_disable(&tc_recirc_sharing_support
);
1609 static int ovs_dp_stats_init(struct datapath
*dp
)
1611 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1612 if (!dp
->stats_percpu
)
1618 static int ovs_dp_vport_init(struct datapath
*dp
)
1622 dp
->ports
= kmalloc_array(DP_VPORT_HASH_BUCKETS
,
1623 sizeof(struct hlist_head
),
1628 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1629 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1634 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1636 struct nlattr
**a
= info
->attrs
;
1637 struct vport_parms parms
;
1638 struct sk_buff
*reply
;
1639 struct datapath
*dp
;
1640 struct vport
*vport
;
1641 struct ovs_net
*ovs_net
;
1645 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1648 reply
= ovs_dp_cmd_alloc_info();
1653 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1655 goto err_destroy_reply
;
1657 ovs_dp_set_net(dp
, sock_net(skb
->sk
));
1658 INIT_DELAYED_WORK(&dp
->masks_rebalance
, ovs_dp_masks_rebalance
);
1660 /* Allocate table. */
1661 err
= ovs_flow_tbl_init(&dp
->table
);
1663 goto err_destroy_dp
;
1665 err
= ovs_dp_stats_init(dp
);
1667 goto err_destroy_table
;
1669 err
= ovs_dp_vport_init(dp
);
1671 goto err_destroy_stats
;
1673 err
= ovs_meters_init(dp
);
1675 goto err_destroy_ports
;
1677 /* Set up our datapath device. */
1678 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1679 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1680 parms
.options
= NULL
;
1682 parms
.port_no
= OVSP_LOCAL
;
1683 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1685 err
= ovs_dp_change(dp
, a
);
1687 goto err_destroy_meters
;
1689 /* So far only local changes have been made, now need the lock. */
1692 vport
= new_vport(&parms
);
1693 if (IS_ERR(vport
)) {
1694 err
= PTR_ERR(vport
);
1698 if (err
== -EEXIST
) {
1699 /* An outdated user space instance that does not understand
1700 * the concept of user_features has attempted to create a new
1701 * datapath and is likely to reuse it. Drop all user features.
1703 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1704 ovs_dp_reset_user_features(skb
, info
);
1708 goto err_destroy_meters
;
1711 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1712 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1715 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1716 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1718 schedule_delayed_work(&dp
->masks_rebalance
,
1719 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL
));
1723 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1727 ovs_meters_exit(dp
);
1731 free_percpu(dp
->stats_percpu
);
1733 ovs_flow_tbl_destroy(&dp
->table
);
1742 /* Called with ovs_mutex. */
1743 static void __dp_destroy(struct datapath
*dp
)
1747 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1748 struct vport
*vport
;
1749 struct hlist_node
*n
;
1751 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1752 if (vport
->port_no
!= OVSP_LOCAL
)
1753 ovs_dp_detach_port(vport
);
1756 list_del_rcu(&dp
->list_node
);
1758 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1759 * all ports in datapath are destroyed first before freeing datapath.
1761 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1763 /* RCU destroy the flow table */
1764 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1766 /* Cancel remaining work. */
1767 cancel_delayed_work_sync(&dp
->masks_rebalance
);
1770 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1772 struct sk_buff
*reply
;
1773 struct datapath
*dp
;
1776 reply
= ovs_dp_cmd_alloc_info();
1781 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1784 goto err_unlock_free
;
1786 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1787 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1793 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1803 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1805 struct sk_buff
*reply
;
1806 struct datapath
*dp
;
1809 reply
= ovs_dp_cmd_alloc_info();
1814 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1817 goto err_unlock_free
;
1819 err
= ovs_dp_change(dp
, info
->attrs
);
1821 goto err_unlock_free
;
1823 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1824 info
->snd_seq
, 0, OVS_DP_CMD_SET
);
1828 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1838 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1840 struct sk_buff
*reply
;
1841 struct datapath
*dp
;
1844 reply
= ovs_dp_cmd_alloc_info();
1849 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1852 goto err_unlock_free
;
1854 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1855 info
->snd_seq
, 0, OVS_DP_CMD_GET
);
1859 return genlmsg_reply(reply
, info
);
1867 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1869 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1870 struct datapath
*dp
;
1871 int skip
= cb
->args
[0];
1875 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1877 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1878 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1879 OVS_DP_CMD_GET
) < 0)
1890 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1891 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1892 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1893 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1896 static const struct genl_ops dp_datapath_genl_ops
[] = {
1897 { .cmd
= OVS_DP_CMD_NEW
,
1898 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1899 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1900 .doit
= ovs_dp_cmd_new
1902 { .cmd
= OVS_DP_CMD_DEL
,
1903 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1904 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1905 .doit
= ovs_dp_cmd_del
1907 { .cmd
= OVS_DP_CMD_GET
,
1908 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1909 .flags
= 0, /* OK for unprivileged users. */
1910 .doit
= ovs_dp_cmd_get
,
1911 .dumpit
= ovs_dp_cmd_dump
1913 { .cmd
= OVS_DP_CMD_SET
,
1914 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1915 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1916 .doit
= ovs_dp_cmd_set
,
1920 static struct genl_family dp_datapath_genl_family __ro_after_init
= {
1921 .hdrsize
= sizeof(struct ovs_header
),
1922 .name
= OVS_DATAPATH_FAMILY
,
1923 .version
= OVS_DATAPATH_VERSION
,
1924 .maxattr
= OVS_DP_ATTR_MAX
,
1925 .policy
= datapath_policy
,
1927 .parallel_ops
= true,
1928 .ops
= dp_datapath_genl_ops
,
1929 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1930 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1932 .module
= THIS_MODULE
,
1935 /* Called with ovs_mutex or RCU read lock. */
1936 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1937 struct net
*net
, u32 portid
, u32 seq
,
1938 u32 flags
, u8 cmd
, gfp_t gfp
)
1940 struct ovs_header
*ovs_header
;
1941 struct ovs_vport_stats vport_stats
;
1944 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1949 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1951 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1952 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1953 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1954 ovs_vport_name(vport
)) ||
1955 nla_put_u32(skb
, OVS_VPORT_ATTR_IFINDEX
, vport
->dev
->ifindex
))
1956 goto nla_put_failure
;
1958 if (!net_eq(net
, dev_net(vport
->dev
))) {
1959 int id
= peernet2id_alloc(net
, dev_net(vport
->dev
), gfp
);
1961 if (nla_put_s32(skb
, OVS_VPORT_ATTR_NETNSID
, id
))
1962 goto nla_put_failure
;
1965 ovs_vport_get_stats(vport
, &vport_stats
);
1966 if (nla_put_64bit(skb
, OVS_VPORT_ATTR_STATS
,
1967 sizeof(struct ovs_vport_stats
), &vport_stats
,
1968 OVS_VPORT_ATTR_PAD
))
1969 goto nla_put_failure
;
1971 if (ovs_vport_get_upcall_portids(vport
, skb
))
1972 goto nla_put_failure
;
1974 err
= ovs_vport_get_options(vport
, skb
);
1975 if (err
== -EMSGSIZE
)
1978 genlmsg_end(skb
, ovs_header
);
1984 genlmsg_cancel(skb
, ovs_header
);
1988 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1990 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1993 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1994 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, struct net
*net
,
1995 u32 portid
, u32 seq
, u8 cmd
)
1997 struct sk_buff
*skb
;
2000 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2002 return ERR_PTR(-ENOMEM
);
2004 retval
= ovs_vport_cmd_fill_info(vport
, skb
, net
, portid
, seq
, 0, cmd
,
2011 /* Called with ovs_mutex or RCU read lock. */
2012 static struct vport
*lookup_vport(struct net
*net
,
2013 const struct ovs_header
*ovs_header
,
2014 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
2016 struct datapath
*dp
;
2017 struct vport
*vport
;
2019 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2020 return ERR_PTR(-EOPNOTSUPP
);
2021 if (a
[OVS_VPORT_ATTR_NAME
]) {
2022 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
2024 return ERR_PTR(-ENODEV
);
2025 if (ovs_header
->dp_ifindex
&&
2026 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
2027 return ERR_PTR(-ENODEV
);
2029 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
2030 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
2032 if (port_no
>= DP_MAX_PORTS
)
2033 return ERR_PTR(-EFBIG
);
2035 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
2037 return ERR_PTR(-ENODEV
);
2039 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
2041 return ERR_PTR(-ENODEV
);
2044 return ERR_PTR(-EINVAL
);
2048 static unsigned int ovs_get_max_headroom(struct datapath
*dp
)
2050 unsigned int dev_headroom
, max_headroom
= 0;
2051 struct net_device
*dev
;
2052 struct vport
*vport
;
2055 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2056 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
,
2057 lockdep_ovsl_is_held()) {
2059 dev_headroom
= netdev_get_fwd_headroom(dev
);
2060 if (dev_headroom
> max_headroom
)
2061 max_headroom
= dev_headroom
;
2065 return max_headroom
;
2068 /* Called with ovs_mutex */
2069 static void ovs_update_headroom(struct datapath
*dp
, unsigned int new_headroom
)
2071 struct vport
*vport
;
2074 dp
->max_headroom
= new_headroom
;
2075 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
2076 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
,
2077 lockdep_ovsl_is_held())
2078 netdev_set_rx_headroom(vport
->dev
, new_headroom
);
2081 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
2083 struct nlattr
**a
= info
->attrs
;
2084 struct ovs_header
*ovs_header
= info
->userhdr
;
2085 struct vport_parms parms
;
2086 struct sk_buff
*reply
;
2087 struct vport
*vport
;
2088 struct datapath
*dp
;
2089 unsigned int new_headroom
;
2093 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
2094 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
2096 if (a
[OVS_VPORT_ATTR_IFINDEX
])
2099 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
2100 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
2101 if (port_no
>= DP_MAX_PORTS
)
2104 reply
= ovs_vport_cmd_alloc_info();
2110 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2113 goto exit_unlock_free
;
2116 vport
= ovs_vport_ovsl(dp
, port_no
);
2119 goto exit_unlock_free
;
2121 for (port_no
= 1; ; port_no
++) {
2122 if (port_no
>= DP_MAX_PORTS
) {
2124 goto exit_unlock_free
;
2126 vport
= ovs_vport_ovsl(dp
, port_no
);
2132 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
2133 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
2134 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
2136 parms
.port_no
= port_no
;
2137 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2139 vport
= new_vport(&parms
);
2140 err
= PTR_ERR(vport
);
2141 if (IS_ERR(vport
)) {
2144 goto exit_unlock_free
;
2147 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2148 info
->snd_portid
, info
->snd_seq
, 0,
2149 OVS_VPORT_CMD_NEW
, GFP_KERNEL
);
2151 new_headroom
= netdev_get_fwd_headroom(vport
->dev
);
2153 if (new_headroom
> dp
->max_headroom
)
2154 ovs_update_headroom(dp
, new_headroom
);
2156 netdev_set_rx_headroom(vport
->dev
, dp
->max_headroom
);
2161 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2170 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
2172 struct nlattr
**a
= info
->attrs
;
2173 struct sk_buff
*reply
;
2174 struct vport
*vport
;
2177 reply
= ovs_vport_cmd_alloc_info();
2182 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2183 err
= PTR_ERR(vport
);
2185 goto exit_unlock_free
;
2187 if (a
[OVS_VPORT_ATTR_TYPE
] &&
2188 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
2190 goto exit_unlock_free
;
2193 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
2194 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
2196 goto exit_unlock_free
;
2200 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
2201 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
2203 err
= ovs_vport_set_upcall_portids(vport
, ids
);
2205 goto exit_unlock_free
;
2208 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2209 info
->snd_portid
, info
->snd_seq
, 0,
2210 OVS_VPORT_CMD_SET
, GFP_KERNEL
);
2214 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2223 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2225 bool update_headroom
= false;
2226 struct nlattr
**a
= info
->attrs
;
2227 struct sk_buff
*reply
;
2228 struct datapath
*dp
;
2229 struct vport
*vport
;
2230 unsigned int new_headroom
;
2233 reply
= ovs_vport_cmd_alloc_info();
2238 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2239 err
= PTR_ERR(vport
);
2241 goto exit_unlock_free
;
2243 if (vport
->port_no
== OVSP_LOCAL
) {
2245 goto exit_unlock_free
;
2248 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2249 info
->snd_portid
, info
->snd_seq
, 0,
2250 OVS_VPORT_CMD_DEL
, GFP_KERNEL
);
2253 /* the vport deletion may trigger dp headroom update */
2255 if (netdev_get_fwd_headroom(vport
->dev
) == dp
->max_headroom
)
2256 update_headroom
= true;
2258 netdev_reset_rx_headroom(vport
->dev
);
2259 ovs_dp_detach_port(vport
);
2261 if (update_headroom
) {
2262 new_headroom
= ovs_get_max_headroom(dp
);
2264 if (new_headroom
< dp
->max_headroom
)
2265 ovs_update_headroom(dp
, new_headroom
);
2269 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2278 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2280 struct nlattr
**a
= info
->attrs
;
2281 struct ovs_header
*ovs_header
= info
->userhdr
;
2282 struct sk_buff
*reply
;
2283 struct vport
*vport
;
2286 reply
= ovs_vport_cmd_alloc_info();
2291 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2292 err
= PTR_ERR(vport
);
2294 goto exit_unlock_free
;
2295 err
= ovs_vport_cmd_fill_info(vport
, reply
, genl_info_net(info
),
2296 info
->snd_portid
, info
->snd_seq
, 0,
2297 OVS_VPORT_CMD_GET
, GFP_ATOMIC
);
2301 return genlmsg_reply(reply
, info
);
2309 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2311 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2312 struct datapath
*dp
;
2313 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2317 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2322 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2323 struct vport
*vport
;
2326 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2328 ovs_vport_cmd_fill_info(vport
, skb
,
2330 NETLINK_CB(cb
->skb
).portid
,
2350 static void ovs_dp_masks_rebalance(struct work_struct
*work
)
2352 struct datapath
*dp
= container_of(work
, struct datapath
,
2353 masks_rebalance
.work
);
2356 ovs_flow_masks_rebalance(&dp
->table
);
2359 schedule_delayed_work(&dp
->masks_rebalance
,
2360 msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL
));
2363 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2364 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2365 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2366 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2367 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2368 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_UNSPEC
},
2369 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2370 [OVS_VPORT_ATTR_IFINDEX
] = { .type
= NLA_U32
},
2371 [OVS_VPORT_ATTR_NETNSID
] = { .type
= NLA_S32
},
2374 static const struct genl_ops dp_vport_genl_ops
[] = {
2375 { .cmd
= OVS_VPORT_CMD_NEW
,
2376 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2377 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2378 .doit
= ovs_vport_cmd_new
2380 { .cmd
= OVS_VPORT_CMD_DEL
,
2381 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2382 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2383 .doit
= ovs_vport_cmd_del
2385 { .cmd
= OVS_VPORT_CMD_GET
,
2386 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2387 .flags
= 0, /* OK for unprivileged users. */
2388 .doit
= ovs_vport_cmd_get
,
2389 .dumpit
= ovs_vport_cmd_dump
2391 { .cmd
= OVS_VPORT_CMD_SET
,
2392 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
2393 .flags
= GENL_UNS_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2394 .doit
= ovs_vport_cmd_set
,
2398 struct genl_family dp_vport_genl_family __ro_after_init
= {
2399 .hdrsize
= sizeof(struct ovs_header
),
2400 .name
= OVS_VPORT_FAMILY
,
2401 .version
= OVS_VPORT_VERSION
,
2402 .maxattr
= OVS_VPORT_ATTR_MAX
,
2403 .policy
= vport_policy
,
2405 .parallel_ops
= true,
2406 .ops
= dp_vport_genl_ops
,
2407 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2408 .mcgrps
= &ovs_dp_vport_multicast_group
,
2410 .module
= THIS_MODULE
,
2413 static struct genl_family
* const dp_genl_families
[] = {
2414 &dp_datapath_genl_family
,
2415 &dp_vport_genl_family
,
2416 &dp_flow_genl_family
,
2417 &dp_packet_genl_family
,
2418 &dp_meter_genl_family
,
2419 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2420 &dp_ct_limit_genl_family
,
2424 static void dp_unregister_genl(int n_families
)
2428 for (i
= 0; i
< n_families
; i
++)
2429 genl_unregister_family(dp_genl_families
[i
]);
2432 static int __init
dp_register_genl(void)
2437 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2439 err
= genl_register_family(dp_genl_families
[i
]);
2447 dp_unregister_genl(i
);
2451 static int __net_init
ovs_init_net(struct net
*net
)
2453 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2455 INIT_LIST_HEAD(&ovs_net
->dps
);
2456 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2457 return ovs_ct_init(net
);
2460 static void __net_exit
list_vports_from_net(struct net
*net
, struct net
*dnet
,
2461 struct list_head
*head
)
2463 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2464 struct datapath
*dp
;
2466 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
2469 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2470 struct vport
*vport
;
2472 hlist_for_each_entry(vport
, &dp
->ports
[i
], dp_hash_node
) {
2473 if (vport
->ops
->type
!= OVS_VPORT_TYPE_INTERNAL
)
2476 if (dev_net(vport
->dev
) == dnet
)
2477 list_add(&vport
->detach_list
, head
);
2483 static void __net_exit
ovs_exit_net(struct net
*dnet
)
2485 struct datapath
*dp
, *dp_next
;
2486 struct ovs_net
*ovs_net
= net_generic(dnet
, ovs_net_id
);
2487 struct vport
*vport
, *vport_next
;
2495 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2498 down_read(&net_rwsem
);
2500 list_vports_from_net(net
, dnet
, &head
);
2501 up_read(&net_rwsem
);
2503 /* Detach all vports from given namespace. */
2504 list_for_each_entry_safe(vport
, vport_next
, &head
, detach_list
) {
2505 list_del(&vport
->detach_list
);
2506 ovs_dp_detach_port(vport
);
2511 cancel_work_sync(&ovs_net
->dp_notify_work
);
2514 static struct pernet_operations ovs_net_ops
= {
2515 .init
= ovs_init_net
,
2516 .exit
= ovs_exit_net
,
2518 .size
= sizeof(struct ovs_net
),
2521 static int __init
dp_init(void)
2525 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > sizeof_field(struct sk_buff
, cb
));
2527 pr_info("Open vSwitch switching datapath\n");
2529 err
= action_fifos_init();
2533 err
= ovs_internal_dev_rtnl_link_register();
2535 goto error_action_fifos_exit
;
2537 err
= ovs_flow_init();
2539 goto error_unreg_rtnl_link
;
2541 err
= ovs_vport_init();
2543 goto error_flow_exit
;
2545 err
= register_pernet_device(&ovs_net_ops
);
2547 goto error_vport_exit
;
2549 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2551 goto error_netns_exit
;
2553 err
= ovs_netdev_init();
2555 goto error_unreg_notifier
;
2557 err
= dp_register_genl();
2559 goto error_unreg_netdev
;
2565 error_unreg_notifier
:
2566 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2568 unregister_pernet_device(&ovs_net_ops
);
2573 error_unreg_rtnl_link
:
2574 ovs_internal_dev_rtnl_link_unregister();
2575 error_action_fifos_exit
:
2576 action_fifos_exit();
2581 static void dp_cleanup(void)
2583 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2585 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2586 unregister_pernet_device(&ovs_net_ops
);
2590 ovs_internal_dev_rtnl_link_unregister();
2591 action_fifos_exit();
2594 module_init(dp_init
);
2595 module_exit(dp_cleanup
);
2597 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2598 MODULE_LICENSE("GPL");
2599 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY
);
2600 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY
);
2601 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY
);
2602 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY
);
2603 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY
);
2604 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY
);