xref: /openbmc/linux/net/openvswitch/datapath.c (revision b03afaa8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2014 Nicira, Inc.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_vlan.h>
12 #include <linux/in.h>
13 #include <linux/ip.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>
40 
41 #include "datapath.h"
42 #include "flow.h"
43 #include "flow_table.h"
44 #include "flow_netlink.h"
45 #include "meter.h"
46 #include "vport-internal_dev.h"
47 #include "vport-netdev.h"
48 
49 unsigned int ovs_net_id __read_mostly;
50 
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;
54 
55 static const struct nla_policy flow_policy[];
56 
57 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
58 	.name = OVS_FLOW_MCGROUP,
59 };
60 
61 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
62 	.name = OVS_DATAPATH_MCGROUP,
63 };
64 
65 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
66 	.name = OVS_VPORT_MCGROUP,
67 };
68 
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,
72 			    unsigned int group)
73 {
74 	return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
75 	       genl_has_listeners(family, genl_info_net(info), group);
76 }
77 
78 static void ovs_notify(struct genl_family *family,
79 		       struct sk_buff *skb, struct genl_info *info)
80 {
81 	genl_notify(family, skb, info, 0, GFP_KERNEL);
82 }
83 
84 /**
85  * DOC: Locking:
86  *
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
90  * by ovs_lock.
91  *
92  * Reads are protected by RCU.
93  *
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
96  * each other.
97  *
98  * The RTNL lock nests inside ovs_mutex.
99  */
100 
101 static DEFINE_MUTEX(ovs_mutex);
102 
103 void ovs_lock(void)
104 {
105 	mutex_lock(&ovs_mutex);
106 }
107 
108 void ovs_unlock(void)
109 {
110 	mutex_unlock(&ovs_mutex);
111 }
112 
113 #ifdef CONFIG_LOCKDEP
114 int lockdep_ovsl_is_held(void)
115 {
116 	if (debug_locks)
117 		return lockdep_is_held(&ovs_mutex);
118 	else
119 		return 1;
120 }
121 #endif
122 
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 *,
127 			     uint32_t cutlen);
128 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
129 				  const struct sw_flow_key *,
130 				  const struct dp_upcall_info *,
131 				  uint32_t cutlen);
132 
133 static void ovs_dp_masks_rebalance(struct work_struct *work);
134 
135 /* Must be called with rcu_read_lock or ovs_mutex. */
136 const char *ovs_dp_name(const struct datapath *dp)
137 {
138 	struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
139 	return ovs_vport_name(vport);
140 }
141 
142 static int get_dpifindex(const struct datapath *dp)
143 {
144 	struct vport *local;
145 	int ifindex;
146 
147 	rcu_read_lock();
148 
149 	local = ovs_vport_rcu(dp, OVSP_LOCAL);
150 	if (local)
151 		ifindex = local->dev->ifindex;
152 	else
153 		ifindex = 0;
154 
155 	rcu_read_unlock();
156 
157 	return ifindex;
158 }
159 
160 static void destroy_dp_rcu(struct rcu_head *rcu)
161 {
162 	struct datapath *dp = container_of(rcu, struct datapath, rcu);
163 
164 	ovs_flow_tbl_destroy(&dp->table);
165 	free_percpu(dp->stats_percpu);
166 	kfree(dp->ports);
167 	ovs_meters_exit(dp);
168 	kfree(dp);
169 }
170 
171 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
172 					    u16 port_no)
173 {
174 	return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
175 }
176 
177 /* Called with ovs_mutex or RCU read lock. */
178 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
179 {
180 	struct vport *vport;
181 	struct hlist_head *head;
182 
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)
187 			return vport;
188 	}
189 	return NULL;
190 }
191 
192 /* Called with ovs_mutex. */
193 static struct vport *new_vport(const struct vport_parms *parms)
194 {
195 	struct vport *vport;
196 
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);
201 
202 		hlist_add_head_rcu(&vport->dp_hash_node, head);
203 	}
204 	return vport;
205 }
206 
207 void ovs_dp_detach_port(struct vport *p)
208 {
209 	ASSERT_OVSL();
210 
211 	/* First drop references to device. */
212 	hlist_del_rcu(&p->dp_hash_node);
213 
214 	/* Then destroy it. */
215 	ovs_vport_del(p);
216 }
217 
218 /* Must be called with rcu_read_lock. */
219 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
220 {
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;
226 	u64 *stats_counter;
227 	u32 n_mask_hit;
228 	int error;
229 
230 	stats = this_cpu_ptr(dp->stats_percpu);
231 
232 	/* Look up flow. */
233 	flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
234 					 &n_mask_hit);
235 	if (unlikely(!flow)) {
236 		struct dp_upcall_info upcall;
237 
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);
243 		if (unlikely(error))
244 			kfree_skb(skb);
245 		else
246 			consume_skb(skb);
247 		stats_counter = &stats->n_missed;
248 		goto out;
249 	}
250 
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);
254 	if (unlikely(error))
255 		net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
256 							ovs_dp_name(dp), error);
257 
258 	stats_counter = &stats->n_hit;
259 
260 out:
261 	/* Update datapath statistics. */
262 	u64_stats_update_begin(&stats->syncp);
263 	(*stats_counter)++;
264 	stats->n_mask_hit += n_mask_hit;
265 	u64_stats_update_end(&stats->syncp);
266 }
267 
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,
271 		  uint32_t cutlen)
272 {
273 	struct dp_stats_percpu *stats;
274 	int err;
275 
276 	if (upcall_info->portid == 0) {
277 		err = -ENOTCONN;
278 		goto err;
279 	}
280 
281 	if (!skb_is_gso(skb))
282 		err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
283 	else
284 		err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
285 	if (err)
286 		goto err;
287 
288 	return 0;
289 
290 err:
291 	stats = this_cpu_ptr(dp->stats_percpu);
292 
293 	u64_stats_update_begin(&stats->syncp);
294 	stats->n_lost++;
295 	u64_stats_update_end(&stats->syncp);
296 
297 	return err;
298 }
299 
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,
303 				 uint32_t cutlen)
304 {
305 	unsigned int gso_type = skb_shinfo(skb)->gso_type;
306 	struct sw_flow_key later_key;
307 	struct sk_buff *segs, *nskb;
308 	int err;
309 
310 	BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_GSO_CB_OFFSET);
311 	segs = __skb_gso_segment(skb, NETIF_F_SG, false);
312 	if (IS_ERR(segs))
313 		return PTR_ERR(segs);
314 	if (segs == NULL)
315 		return -EINVAL;
316 
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.
321 		 */
322 		later_key = *key;
323 		later_key.ip.frag = OVS_FRAG_TYPE_LATER;
324 	}
325 
326 	/* Queue all of the segments. */
327 	skb_list_walk_safe(segs, skb, nskb) {
328 		if (gso_type & SKB_GSO_UDP && skb != segs)
329 			key = &later_key;
330 
331 		err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
332 		if (err)
333 			break;
334 
335 	}
336 
337 	/* Free all of the segments. */
338 	skb_list_walk_safe(segs, skb, nskb) {
339 		if (err)
340 			kfree_skb(skb);
341 		else
342 			consume_skb(skb);
343 	}
344 	return err;
345 }
346 
347 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
348 			      unsigned int hdrlen, int actions_attrlen)
349 {
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 */
355 
356 	/* OVS_PACKET_ATTR_USERDATA */
357 	if (upcall_info->userdata)
358 		size += NLA_ALIGN(upcall_info->userdata->nla_len);
359 
360 	/* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
361 	if (upcall_info->egress_tun_info)
362 		size += nla_total_size(ovs_tun_key_attr_size());
363 
364 	/* OVS_PACKET_ATTR_ACTIONS */
365 	if (upcall_info->actions_len)
366 		size += nla_total_size(actions_attrlen);
367 
368 	/* OVS_PACKET_ATTR_MRU */
369 	if (upcall_info->mru)
370 		size += nla_total_size(sizeof(upcall_info->mru));
371 
372 	return size;
373 }
374 
375 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
376 {
377 	if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
378 		size_t plen = NLA_ALIGN(skb->len) - skb->len;
379 
380 		if (plen > 0)
381 			skb_put_zero(skb, plen);
382 	}
383 }
384 
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,
388 				  uint32_t cutlen)
389 {
390 	struct ovs_header *upcall;
391 	struct sk_buff *nskb = NULL;
392 	struct sk_buff *user_skb = NULL; /* to be queued to userspace */
393 	struct nlattr *nla;
394 	size_t len;
395 	unsigned int hlen;
396 	int err, dp_ifindex;
397 	u64 hash;
398 
399 	dp_ifindex = get_dpifindex(dp);
400 	if (!dp_ifindex)
401 		return -ENODEV;
402 
403 	if (skb_vlan_tag_present(skb)) {
404 		nskb = skb_clone(skb, GFP_ATOMIC);
405 		if (!nskb)
406 			return -ENOMEM;
407 
408 		nskb = __vlan_hwaccel_push_inside(nskb);
409 		if (!nskb)
410 			return -ENOMEM;
411 
412 		skb = nskb;
413 	}
414 
415 	if (nla_attr_size(skb->len) > USHRT_MAX) {
416 		err = -EFBIG;
417 		goto out;
418 	}
419 
420 	/* Complete checksum if needed */
421 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
422 	    (err = skb_csum_hwoffload_help(skb, 0)))
423 		goto out;
424 
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.
428 	 */
429 	if (dp->user_features & OVS_DP_F_UNALIGNED)
430 		hlen = skb_zerocopy_headlen(skb);
431 	else
432 		hlen = skb->len;
433 
434 	len = upcall_msg_size(upcall_info, hlen - cutlen,
435 			      OVS_CB(skb)->acts_origlen);
436 	user_skb = genlmsg_new(len, GFP_ATOMIC);
437 	if (!user_skb) {
438 		err = -ENOMEM;
439 		goto out;
440 	}
441 
442 	upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
443 			     0, upcall_info->cmd);
444 	if (!upcall) {
445 		err = -EINVAL;
446 		goto out;
447 	}
448 	upcall->dp_ifindex = dp_ifindex;
449 
450 	err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
451 	if (err)
452 		goto out;
453 
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));
458 
459 	if (upcall_info->egress_tun_info) {
460 		nla = nla_nest_start_noflag(user_skb,
461 					    OVS_PACKET_ATTR_EGRESS_TUN_KEY);
462 		if (!nla) {
463 			err = -EMSGSIZE;
464 			goto out;
465 		}
466 		err = ovs_nla_put_tunnel_info(user_skb,
467 					      upcall_info->egress_tun_info);
468 		if (err)
469 			goto out;
470 
471 		nla_nest_end(user_skb, nla);
472 	}
473 
474 	if (upcall_info->actions_len) {
475 		nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
476 		if (!nla) {
477 			err = -EMSGSIZE;
478 			goto out;
479 		}
480 		err = ovs_nla_put_actions(upcall_info->actions,
481 					  upcall_info->actions_len,
482 					  user_skb);
483 		if (!err)
484 			nla_nest_end(user_skb, nla);
485 		else
486 			nla_nest_cancel(user_skb, nla);
487 	}
488 
489 	/* Add OVS_PACKET_ATTR_MRU */
490 	if (upcall_info->mru &&
491 	    nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU, upcall_info->mru)) {
492 		err = -ENOBUFS;
493 		goto out;
494 	}
495 
496 	/* Add OVS_PACKET_ATTR_LEN when packet is truncated */
497 	if (cutlen > 0 &&
498 	    nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN, skb->len)) {
499 		err = -ENOBUFS;
500 		goto out;
501 	}
502 
503 	/* Add OVS_PACKET_ATTR_HASH */
504 	hash = skb_get_hash_raw(skb);
505 	if (skb->sw_hash)
506 		hash |= OVS_PACKET_HASH_SW_BIT;
507 
508 	if (skb->l4_hash)
509 		hash |= OVS_PACKET_HASH_L4_BIT;
510 
511 	if (nla_put(user_skb, OVS_PACKET_ATTR_HASH, sizeof (u64), &hash)) {
512 		err = -ENOBUFS;
513 		goto out;
514 	}
515 
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))) {
519 		err = -ENOBUFS;
520 		goto out;
521 	}
522 	nla->nla_len = nla_attr_size(skb->len - cutlen);
523 
524 	err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
525 	if (err)
526 		goto out;
527 
528 	/* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
529 	pad_packet(dp, user_skb);
530 
531 	((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
532 
533 	err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
534 	user_skb = NULL;
535 out:
536 	if (err)
537 		skb_tx_error(skb);
538 	kfree_skb(user_skb);
539 	kfree_skb(nskb);
540 	return err;
541 }
542 
543 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
544 {
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;
552 	struct datapath *dp;
553 	struct vport *input_vport;
554 	u16 mru = 0;
555 	u64 hash;
556 	int len;
557 	int err;
558 	bool log = !a[OVS_PACKET_ATTR_PROBE];
559 
560 	err = -EINVAL;
561 	if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
562 	    !a[OVS_PACKET_ATTR_ACTIONS])
563 		goto err;
564 
565 	len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
566 	packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
567 	err = -ENOMEM;
568 	if (!packet)
569 		goto err;
570 	skb_reserve(packet, NET_IP_ALIGN);
571 
572 	nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
573 
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;
578 	}
579 	OVS_CB(packet)->mru = mru;
580 
581 	if (a[OVS_PACKET_ATTR_HASH]) {
582 		hash = nla_get_u64(a[OVS_PACKET_ATTR_HASH]);
583 
584 		__skb_set_hash(packet, hash & 0xFFFFFFFFULL,
585 			       !!(hash & OVS_PACKET_HASH_SW_BIT),
586 			       !!(hash & OVS_PACKET_HASH_L4_BIT));
587 	}
588 
589 	/* Build an sw_flow for sending this packet. */
590 	flow = ovs_flow_alloc();
591 	err = PTR_ERR(flow);
592 	if (IS_ERR(flow))
593 		goto err_kfree_skb;
594 
595 	err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
596 					     packet, &flow->key, log);
597 	if (err)
598 		goto err_flow_free;
599 
600 	err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
601 				   &flow->key, &acts, log);
602 	if (err)
603 		goto err_flow_free;
604 
605 	rcu_assign_pointer(flow->sf_acts, acts);
606 	packet->priority = flow->key.phy.priority;
607 	packet->mark = flow->key.phy.skb_mark;
608 
609 	rcu_read_lock();
610 	dp = get_dp_rcu(net, ovs_header->dp_ifindex);
611 	err = -ENODEV;
612 	if (!dp)
613 		goto err_unlock;
614 
615 	input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
616 	if (!input_vport)
617 		input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
618 
619 	if (!input_vport)
620 		goto err_unlock;
621 
622 	packet->dev = input_vport->dev;
623 	OVS_CB(packet)->input_vport = input_vport;
624 	sf_acts = rcu_dereference(flow->sf_acts);
625 
626 	local_bh_disable();
627 	err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
628 	local_bh_enable();
629 	rcu_read_unlock();
630 
631 	ovs_flow_free(flow, false);
632 	return err;
633 
634 err_unlock:
635 	rcu_read_unlock();
636 err_flow_free:
637 	ovs_flow_free(flow, false);
638 err_kfree_skb:
639 	kfree_skb(packet);
640 err:
641 	return err;
642 }
643 
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 },
651 };
652 
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
658 	}
659 };
660 
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,
667 	.netnsok = true,
668 	.parallel_ops = true,
669 	.ops = dp_packet_genl_ops,
670 	.n_ops = ARRAY_SIZE(dp_packet_genl_ops),
671 	.module = THIS_MODULE,
672 };
673 
674 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
675 			 struct ovs_dp_megaflow_stats *mega_stats)
676 {
677 	int i;
678 
679 	memset(mega_stats, 0, sizeof(*mega_stats));
680 
681 	stats->n_flows = ovs_flow_tbl_count(&dp->table);
682 	mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
683 
684 	stats->n_hit = stats->n_missed = stats->n_lost = 0;
685 
686 	for_each_possible_cpu(i) {
687 		const struct dp_stats_percpu *percpu_stats;
688 		struct dp_stats_percpu local_stats;
689 		unsigned int start;
690 
691 		percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
692 
693 		do {
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));
697 
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;
702 	}
703 }
704 
705 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
706 {
707 	return ovs_identifier_is_ufid(sfid) &&
708 	       !(ufid_flags & OVS_UFID_F_OMIT_KEY);
709 }
710 
711 static bool should_fill_mask(uint32_t ufid_flags)
712 {
713 	return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
714 }
715 
716 static bool should_fill_actions(uint32_t ufid_flags)
717 {
718 	return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
719 }
720 
721 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
722 				    const struct sw_flow_id *sfid,
723 				    uint32_t ufid_flags)
724 {
725 	size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
726 
727 	/* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
728 	 * see ovs_nla_put_identifier()
729 	 */
730 	if (sfid && ovs_identifier_is_ufid(sfid))
731 		len += nla_total_size(sfid->ufid_len);
732 	else
733 		len += nla_total_size(ovs_key_attr_size());
734 
735 	/* OVS_FLOW_ATTR_KEY */
736 	if (!sfid || should_fill_key(sfid, ufid_flags))
737 		len += nla_total_size(ovs_key_attr_size());
738 
739 	/* OVS_FLOW_ATTR_MASK */
740 	if (should_fill_mask(ufid_flags))
741 		len += nla_total_size(ovs_key_attr_size());
742 
743 	/* OVS_FLOW_ATTR_ACTIONS */
744 	if (should_fill_actions(ufid_flags))
745 		len += nla_total_size(acts->orig_len);
746 
747 	return 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 */
751 }
752 
753 /* Called with ovs_mutex or RCU read lock. */
754 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
755 				   struct sk_buff *skb)
756 {
757 	struct ovs_flow_stats stats;
758 	__be16 tcp_flags;
759 	unsigned long used;
760 
761 	ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
762 
763 	if (used &&
764 	    nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
765 			      OVS_FLOW_ATTR_PAD))
766 		return -EMSGSIZE;
767 
768 	if (stats.n_packets &&
769 	    nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
770 			  sizeof(struct ovs_flow_stats), &stats,
771 			  OVS_FLOW_ATTR_PAD))
772 		return -EMSGSIZE;
773 
774 	if ((u8)ntohs(tcp_flags) &&
775 	     nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
776 		return -EMSGSIZE;
777 
778 	return 0;
779 }
780 
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)
784 {
785 	struct nlattr *start;
786 	int err;
787 
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.)
794 	 *
795 	 * This can only fail for dump operations because the skb is always
796 	 * properly sized for single flows.
797 	 */
798 	start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
799 	if (start) {
800 		const struct sw_flow_actions *sf_acts;
801 
802 		sf_acts = rcu_dereference_ovsl(flow->sf_acts);
803 		err = ovs_nla_put_actions(sf_acts->actions,
804 					  sf_acts->actions_len, skb);
805 
806 		if (!err)
807 			nla_nest_end(skb, start);
808 		else {
809 			if (skb_orig_len)
810 				return err;
811 
812 			nla_nest_cancel(skb, start);
813 		}
814 	} else if (skb_orig_len) {
815 		return -EMSGSIZE;
816 	}
817 
818 	return 0;
819 }
820 
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)
825 {
826 	const int skb_orig_len = skb->len;
827 	struct ovs_header *ovs_header;
828 	int err;
829 
830 	ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
831 				 flags, cmd);
832 	if (!ovs_header)
833 		return -EMSGSIZE;
834 
835 	ovs_header->dp_ifindex = dp_ifindex;
836 
837 	err = ovs_nla_put_identifier(flow, skb);
838 	if (err)
839 		goto error;
840 
841 	if (should_fill_key(&flow->id, ufid_flags)) {
842 		err = ovs_nla_put_masked_key(flow, skb);
843 		if (err)
844 			goto error;
845 	}
846 
847 	if (should_fill_mask(ufid_flags)) {
848 		err = ovs_nla_put_mask(flow, skb);
849 		if (err)
850 			goto error;
851 	}
852 
853 	err = ovs_flow_cmd_fill_stats(flow, skb);
854 	if (err)
855 		goto error;
856 
857 	if (should_fill_actions(ufid_flags)) {
858 		err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
859 		if (err)
860 			goto error;
861 	}
862 
863 	genlmsg_end(skb, ovs_header);
864 	return 0;
865 
866 error:
867 	genlmsg_cancel(skb, ovs_header);
868 	return err;
869 }
870 
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,
875 					       bool always,
876 					       uint32_t ufid_flags)
877 {
878 	struct sk_buff *skb;
879 	size_t len;
880 
881 	if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
882 		return NULL;
883 
884 	len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
885 	skb = genlmsg_new(len, GFP_KERNEL);
886 	if (!skb)
887 		return ERR_PTR(-ENOMEM);
888 
889 	return skb;
890 }
891 
892 /* Called with ovs_mutex. */
893 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
894 					       int dp_ifindex,
895 					       struct genl_info *info, u8 cmd,
896 					       bool always, u32 ufid_flags)
897 {
898 	struct sk_buff *skb;
899 	int retval;
900 
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))
904 		return skb;
905 
906 	retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
907 					info->snd_portid, info->snd_seq, 0,
908 					cmd, ufid_flags);
909 	if (WARN_ON_ONCE(retval < 0)) {
910 		kfree_skb(skb);
911 		skb = ERR_PTR(retval);
912 	}
913 	return skb;
914 }
915 
916 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
917 {
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;
924 	struct datapath *dp;
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]);
928 	int error;
929 	bool log = !a[OVS_FLOW_ATTR_PROBE];
930 
931 	/* Must have key and actions. */
932 	error = -EINVAL;
933 	if (!a[OVS_FLOW_ATTR_KEY]) {
934 		OVS_NLERR(log, "Flow key attr not present in new flow.");
935 		goto error;
936 	}
937 	if (!a[OVS_FLOW_ATTR_ACTIONS]) {
938 		OVS_NLERR(log, "Flow actions attr not present in new flow.");
939 		goto error;
940 	}
941 
942 	/* Most of the time we need to allocate a new flow, do it before
943 	 * locking.
944 	 */
945 	new_flow = ovs_flow_alloc();
946 	if (IS_ERR(new_flow)) {
947 		error = PTR_ERR(new_flow);
948 		goto error;
949 	}
950 
951 	/* Extract key. */
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);
955 	if (error)
956 		goto err_kfree_flow;
957 
958 	/* Extract flow identifier. */
959 	error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
960 				       &new_flow->key, log);
961 	if (error)
962 		goto err_kfree_flow;
963 
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;
967 
968 	ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
969 
970 	/* Validate actions. */
971 	error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
972 				     &new_flow->key, &acts, log);
973 	if (error) {
974 		OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
975 		goto err_kfree_flow;
976 	}
977 
978 	reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
979 					ufid_flags);
980 	if (IS_ERR(reply)) {
981 		error = PTR_ERR(reply);
982 		goto err_kfree_acts;
983 	}
984 
985 	ovs_lock();
986 	dp = get_dp(net, ovs_header->dp_ifindex);
987 	if (unlikely(!dp)) {
988 		error = -ENODEV;
989 		goto err_unlock_ovs;
990 	}
991 
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);
995 	if (!flow)
996 		flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
997 	if (likely(!flow)) {
998 		rcu_assign_pointer(new_flow->sf_acts, acts);
999 
1000 		/* Put flow in bucket. */
1001 		error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1002 		if (unlikely(error)) {
1003 			acts = NULL;
1004 			goto err_unlock_ovs;
1005 		}
1006 
1007 		if (unlikely(reply)) {
1008 			error = ovs_flow_cmd_fill_info(new_flow,
1009 						       ovs_header->dp_ifindex,
1010 						       reply, info->snd_portid,
1011 						       info->snd_seq, 0,
1012 						       OVS_FLOW_CMD_NEW,
1013 						       ufid_flags);
1014 			BUG_ON(error < 0);
1015 		}
1016 		ovs_unlock();
1017 	} else {
1018 		struct sw_flow_actions *old_acts;
1019 
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
1024 		 * gets fixed.
1025 		 */
1026 		if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1027 							 | NLM_F_EXCL))) {
1028 			error = -EEXIST;
1029 			goto err_unlock_ovs;
1030 		}
1031 		/* The flow identifier has to be the same for flow updates.
1032 		 * Look for any overlapping flow.
1033 		 */
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,
1037 								 &match);
1038 			else /* UFID matches but key is different */
1039 				flow = NULL;
1040 			if (!flow) {
1041 				error = -ENOENT;
1042 				goto err_unlock_ovs;
1043 			}
1044 		}
1045 		/* Update actions. */
1046 		old_acts = ovsl_dereference(flow->sf_acts);
1047 		rcu_assign_pointer(flow->sf_acts, acts);
1048 
1049 		if (unlikely(reply)) {
1050 			error = ovs_flow_cmd_fill_info(flow,
1051 						       ovs_header->dp_ifindex,
1052 						       reply, info->snd_portid,
1053 						       info->snd_seq, 0,
1054 						       OVS_FLOW_CMD_NEW,
1055 						       ufid_flags);
1056 			BUG_ON(error < 0);
1057 		}
1058 		ovs_unlock();
1059 
1060 		ovs_nla_free_flow_actions_rcu(old_acts);
1061 		ovs_flow_free(new_flow, false);
1062 	}
1063 
1064 	if (reply)
1065 		ovs_notify(&dp_flow_genl_family, reply, info);
1066 	return 0;
1067 
1068 err_unlock_ovs:
1069 	ovs_unlock();
1070 	kfree_skb(reply);
1071 err_kfree_acts:
1072 	ovs_nla_free_flow_actions(acts);
1073 err_kfree_flow:
1074 	ovs_flow_free(new_flow, false);
1075 error:
1076 	return error;
1077 }
1078 
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,
1084 						bool log)
1085 {
1086 	struct sw_flow_actions *acts;
1087 	struct sw_flow_key masked_key;
1088 	int error;
1089 
1090 	ovs_flow_mask_key(&masked_key, key, true, mask);
1091 	error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1092 	if (error) {
1093 		OVS_NLERR(log,
1094 			  "Actions may not be safe on all matching packets");
1095 		return ERR_PTR(error);
1096 	}
1097 
1098 	return acts;
1099 }
1100 
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
1104  * stack space.
1105  *
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
1112  * to mask.
1113  * */
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,
1118 			      struct nlattr **a,
1119 			      struct sw_flow_actions **acts,
1120 			      bool log)
1121 {
1122 	struct sw_flow_mask mask;
1123 	int error = 0;
1124 
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);
1129 		if (error)
1130 			goto error;
1131 	}
1132 
1133 	if (a[OVS_FLOW_ATTR_ACTIONS]) {
1134 		if (!a[OVS_FLOW_ATTR_KEY]) {
1135 			OVS_NLERR(log,
1136 				  "Flow key attribute not present in set flow.");
1137 			error = -EINVAL;
1138 			goto error;
1139 		}
1140 
1141 		*acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1142 					 &mask, log);
1143 		if (IS_ERR(*acts)) {
1144 			error = PTR_ERR(*acts);
1145 			goto error;
1146 		}
1147 	}
1148 
1149 	/* On success, error is 0. */
1150 error:
1151 	match->mask = NULL;
1152 	return error;
1153 }
1154 
1155 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1156 {
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]);
1168 	int error = 0;
1169 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1170 	bool ufid_present;
1171 
1172 	ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1173 	if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1174 		OVS_NLERR(log,
1175 			  "Flow set message rejected, Key attribute missing.");
1176 		return -EINVAL;
1177 	}
1178 
1179 	error = ovs_nla_init_match_and_action(net, &match, &key, a,
1180 					      &acts, log);
1181 	if (error)
1182 		goto error;
1183 
1184 	if (acts) {
1185 		/* Can allocate before locking if have acts. */
1186 		reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1187 						ufid_flags);
1188 		if (IS_ERR(reply)) {
1189 			error = PTR_ERR(reply);
1190 			goto err_kfree_acts;
1191 		}
1192 	}
1193 
1194 	ovs_lock();
1195 	dp = get_dp(net, ovs_header->dp_ifindex);
1196 	if (unlikely(!dp)) {
1197 		error = -ENODEV;
1198 		goto err_unlock_ovs;
1199 	}
1200 	/* Check that the flow exists. */
1201 	if (ufid_present)
1202 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1203 	else
1204 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1205 	if (unlikely(!flow)) {
1206 		error = -ENOENT;
1207 		goto err_unlock_ovs;
1208 	}
1209 
1210 	/* Update actions, if present. */
1211 	if (likely(acts)) {
1212 		old_acts = ovsl_dereference(flow->sf_acts);
1213 		rcu_assign_pointer(flow->sf_acts, acts);
1214 
1215 		if (unlikely(reply)) {
1216 			error = ovs_flow_cmd_fill_info(flow,
1217 						       ovs_header->dp_ifindex,
1218 						       reply, info->snd_portid,
1219 						       info->snd_seq, 0,
1220 						       OVS_FLOW_CMD_SET,
1221 						       ufid_flags);
1222 			BUG_ON(error < 0);
1223 		}
1224 	} else {
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,
1228 						ufid_flags);
1229 
1230 		if (IS_ERR(reply)) {
1231 			error = PTR_ERR(reply);
1232 			goto err_unlock_ovs;
1233 		}
1234 	}
1235 
1236 	/* Clear stats. */
1237 	if (a[OVS_FLOW_ATTR_CLEAR])
1238 		ovs_flow_stats_clear(flow);
1239 	ovs_unlock();
1240 
1241 	if (reply)
1242 		ovs_notify(&dp_flow_genl_family, reply, info);
1243 	if (old_acts)
1244 		ovs_nla_free_flow_actions_rcu(old_acts);
1245 
1246 	return 0;
1247 
1248 err_unlock_ovs:
1249 	ovs_unlock();
1250 	kfree_skb(reply);
1251 err_kfree_acts:
1252 	ovs_nla_free_flow_actions(acts);
1253 error:
1254 	return error;
1255 }
1256 
1257 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1258 {
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]);
1269 	int err = 0;
1270 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1271 	bool ufid_present;
1272 
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,
1277 					log);
1278 	} else if (!ufid_present) {
1279 		OVS_NLERR(log,
1280 			  "Flow get message rejected, Key attribute missing.");
1281 		err = -EINVAL;
1282 	}
1283 	if (err)
1284 		return err;
1285 
1286 	ovs_lock();
1287 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1288 	if (!dp) {
1289 		err = -ENODEV;
1290 		goto unlock;
1291 	}
1292 
1293 	if (ufid_present)
1294 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1295 	else
1296 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1297 	if (!flow) {
1298 		err = -ENOENT;
1299 		goto unlock;
1300 	}
1301 
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);
1306 		goto unlock;
1307 	}
1308 
1309 	ovs_unlock();
1310 	return genlmsg_reply(reply, info);
1311 unlock:
1312 	ovs_unlock();
1313 	return err;
1314 }
1315 
1316 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1317 {
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]);
1328 	int err;
1329 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1330 	bool ufid_present;
1331 
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],
1336 					NULL, log);
1337 		if (unlikely(err))
1338 			return err;
1339 	}
1340 
1341 	ovs_lock();
1342 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1343 	if (unlikely(!dp)) {
1344 		err = -ENODEV;
1345 		goto unlock;
1346 	}
1347 
1348 	if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1349 		err = ovs_flow_tbl_flush(&dp->table);
1350 		goto unlock;
1351 	}
1352 
1353 	if (ufid_present)
1354 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1355 	else
1356 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1357 	if (unlikely(!flow)) {
1358 		err = -ENOENT;
1359 		goto unlock;
1360 	}
1361 
1362 	ovs_flow_tbl_remove(&dp->table, flow);
1363 	ovs_unlock();
1364 
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,
1372 						     info->snd_seq, 0,
1373 						     OVS_FLOW_CMD_DEL,
1374 						     ufid_flags);
1375 			rcu_read_unlock();
1376 			if (WARN_ON_ONCE(err < 0)) {
1377 				kfree_skb(reply);
1378 				goto out_free;
1379 			}
1380 
1381 			ovs_notify(&dp_flow_genl_family, reply, info);
1382 		} else {
1383 			netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1384 		}
1385 	}
1386 
1387 out_free:
1388 	ovs_flow_free(flow, true);
1389 	return 0;
1390 unlock:
1391 	ovs_unlock();
1392 	return err;
1393 }
1394 
1395 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1396 {
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;
1401 	u32 ufid_flags;
1402 	int err;
1403 
1404 	err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1405 				       OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1406 	if (err)
1407 		return err;
1408 	ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1409 
1410 	rcu_read_lock();
1411 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1412 	if (!dp) {
1413 		rcu_read_unlock();
1414 		return -ENODEV;
1415 	}
1416 
1417 	ti = rcu_dereference(dp->table.ti);
1418 	for (;;) {
1419 		struct sw_flow *flow;
1420 		u32 bucket, obj;
1421 
1422 		bucket = cb->args[0];
1423 		obj = cb->args[1];
1424 		flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1425 		if (!flow)
1426 			break;
1427 
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)
1432 			break;
1433 
1434 		cb->args[0] = bucket;
1435 		cb->args[1] = obj;
1436 	}
1437 	rcu_read_unlock();
1438 	return skb->len;
1439 }
1440 
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 },
1449 };
1450 
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
1456 	},
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
1461 	},
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
1467 	},
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,
1472 	},
1473 };
1474 
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,
1481 	.netnsok = true,
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,
1486 	.n_mcgrps = 1,
1487 	.module = THIS_MODULE,
1488 };
1489 
1490 static size_t ovs_dp_cmd_msg_size(void)
1491 {
1492 	size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1493 
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 */
1498 
1499 	return msgsize;
1500 }
1501 
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)
1505 {
1506 	struct ovs_header *ovs_header;
1507 	struct ovs_dp_stats dp_stats;
1508 	struct ovs_dp_megaflow_stats dp_megaflow_stats;
1509 	int err;
1510 
1511 	ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1512 				   flags, cmd);
1513 	if (!ovs_header)
1514 		goto error;
1515 
1516 	ovs_header->dp_ifindex = get_dpifindex(dp);
1517 
1518 	err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1519 	if (err)
1520 		goto nla_put_failure;
1521 
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;
1526 
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;
1531 
1532 	if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1533 		goto nla_put_failure;
1534 
1535 	genlmsg_end(skb, ovs_header);
1536 	return 0;
1537 
1538 nla_put_failure:
1539 	genlmsg_cancel(skb, ovs_header);
1540 error:
1541 	return -EMSGSIZE;
1542 }
1543 
1544 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1545 {
1546 	return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1547 }
1548 
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])
1553 {
1554 	struct datapath *dp;
1555 
1556 	if (!a[OVS_DP_ATTR_NAME])
1557 		dp = get_dp(net, ovs_header->dp_ifindex);
1558 	else {
1559 		struct vport *vport;
1560 
1561 		vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1562 		dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1563 	}
1564 	return dp ? dp : ERR_PTR(-ENODEV);
1565 }
1566 
1567 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1568 {
1569 	struct datapath *dp;
1570 
1571 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1572 	if (IS_ERR(dp))
1573 		return;
1574 
1575 	WARN(dp->user_features, "Dropping previously announced user features\n");
1576 	dp->user_features = 0;
1577 }
1578 
1579 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1580 
1581 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1582 {
1583 	u32 user_features = 0;
1584 
1585 	if (a[OVS_DP_ATTR_USER_FEATURES]) {
1586 		user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1587 
1588 		if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1589 				      OVS_DP_F_UNALIGNED |
1590 				      OVS_DP_F_TC_RECIRC_SHARING))
1591 			return -EOPNOTSUPP;
1592 
1593 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1594 		if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1595 			return -EOPNOTSUPP;
1596 #endif
1597 	}
1598 
1599 	dp->user_features = user_features;
1600 
1601 	if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1602 		static_branch_enable(&tc_recirc_sharing_support);
1603 	else
1604 		static_branch_disable(&tc_recirc_sharing_support);
1605 
1606 	return 0;
1607 }
1608 
1609 static int ovs_dp_stats_init(struct datapath *dp)
1610 {
1611 	dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1612 	if (!dp->stats_percpu)
1613 		return -ENOMEM;
1614 
1615 	return 0;
1616 }
1617 
1618 static int ovs_dp_vport_init(struct datapath *dp)
1619 {
1620 	int i;
1621 
1622 	dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1623 				  sizeof(struct hlist_head),
1624 				  GFP_KERNEL);
1625 	if (!dp->ports)
1626 		return -ENOMEM;
1627 
1628 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1629 		INIT_HLIST_HEAD(&dp->ports[i]);
1630 
1631 	return 0;
1632 }
1633 
1634 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1635 {
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;
1642 	int err;
1643 
1644 	err = -EINVAL;
1645 	if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1646 		goto err;
1647 
1648 	reply = ovs_dp_cmd_alloc_info();
1649 	if (!reply)
1650 		return -ENOMEM;
1651 
1652 	err = -ENOMEM;
1653 	dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1654 	if (dp == NULL)
1655 		goto err_destroy_reply;
1656 
1657 	ovs_dp_set_net(dp, sock_net(skb->sk));
1658 	INIT_DELAYED_WORK(&dp->masks_rebalance, ovs_dp_masks_rebalance);
1659 
1660 	/* Allocate table. */
1661 	err = ovs_flow_tbl_init(&dp->table);
1662 	if (err)
1663 		goto err_destroy_dp;
1664 
1665 	err = ovs_dp_stats_init(dp);
1666 	if (err)
1667 		goto err_destroy_table;
1668 
1669 	err = ovs_dp_vport_init(dp);
1670 	if (err)
1671 		goto err_destroy_stats;
1672 
1673 	err = ovs_meters_init(dp);
1674 	if (err)
1675 		goto err_destroy_ports;
1676 
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;
1681 	parms.dp = dp;
1682 	parms.port_no = OVSP_LOCAL;
1683 	parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1684 
1685 	err = ovs_dp_change(dp, a);
1686 	if (err)
1687 		goto err_destroy_meters;
1688 
1689 	/* So far only local changes have been made, now need the lock. */
1690 	ovs_lock();
1691 
1692 	vport = new_vport(&parms);
1693 	if (IS_ERR(vport)) {
1694 		err = PTR_ERR(vport);
1695 		if (err == -EBUSY)
1696 			err = -EEXIST;
1697 
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.
1702 			 */
1703 			if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1704 				ovs_dp_reset_user_features(skb, info);
1705 		}
1706 
1707 		ovs_unlock();
1708 		goto err_destroy_meters;
1709 	}
1710 
1711 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1712 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1713 	BUG_ON(err < 0);
1714 
1715 	ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1716 	list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1717 
1718 	schedule_delayed_work(&dp->masks_rebalance,
1719 			      msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
1720 
1721 	ovs_unlock();
1722 
1723 	ovs_notify(&dp_datapath_genl_family, reply, info);
1724 	return 0;
1725 
1726 err_destroy_meters:
1727 	ovs_meters_exit(dp);
1728 err_destroy_ports:
1729 	kfree(dp->ports);
1730 err_destroy_stats:
1731 	free_percpu(dp->stats_percpu);
1732 err_destroy_table:
1733 	ovs_flow_tbl_destroy(&dp->table);
1734 err_destroy_dp:
1735 	kfree(dp);
1736 err_destroy_reply:
1737 	kfree_skb(reply);
1738 err:
1739 	return err;
1740 }
1741 
1742 /* Called with ovs_mutex. */
1743 static void __dp_destroy(struct datapath *dp)
1744 {
1745 	int i;
1746 
1747 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1748 		struct vport *vport;
1749 		struct hlist_node *n;
1750 
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);
1754 	}
1755 
1756 	list_del_rcu(&dp->list_node);
1757 
1758 	/* OVSP_LOCAL is datapath internal port. We need to make sure that
1759 	 * all ports in datapath are destroyed first before freeing datapath.
1760 	 */
1761 	ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1762 
1763 	/* RCU destroy the flow table */
1764 	call_rcu(&dp->rcu, destroy_dp_rcu);
1765 
1766 	/* Cancel remaining work. */
1767 	cancel_delayed_work_sync(&dp->masks_rebalance);
1768 }
1769 
1770 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1771 {
1772 	struct sk_buff *reply;
1773 	struct datapath *dp;
1774 	int err;
1775 
1776 	reply = ovs_dp_cmd_alloc_info();
1777 	if (!reply)
1778 		return -ENOMEM;
1779 
1780 	ovs_lock();
1781 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1782 	err = PTR_ERR(dp);
1783 	if (IS_ERR(dp))
1784 		goto err_unlock_free;
1785 
1786 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1787 				   info->snd_seq, 0, OVS_DP_CMD_DEL);
1788 	BUG_ON(err < 0);
1789 
1790 	__dp_destroy(dp);
1791 	ovs_unlock();
1792 
1793 	ovs_notify(&dp_datapath_genl_family, reply, info);
1794 
1795 	return 0;
1796 
1797 err_unlock_free:
1798 	ovs_unlock();
1799 	kfree_skb(reply);
1800 	return err;
1801 }
1802 
1803 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1804 {
1805 	struct sk_buff *reply;
1806 	struct datapath *dp;
1807 	int err;
1808 
1809 	reply = ovs_dp_cmd_alloc_info();
1810 	if (!reply)
1811 		return -ENOMEM;
1812 
1813 	ovs_lock();
1814 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1815 	err = PTR_ERR(dp);
1816 	if (IS_ERR(dp))
1817 		goto err_unlock_free;
1818 
1819 	err = ovs_dp_change(dp, info->attrs);
1820 	if (err)
1821 		goto err_unlock_free;
1822 
1823 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1824 				   info->snd_seq, 0, OVS_DP_CMD_SET);
1825 	BUG_ON(err < 0);
1826 
1827 	ovs_unlock();
1828 	ovs_notify(&dp_datapath_genl_family, reply, info);
1829 
1830 	return 0;
1831 
1832 err_unlock_free:
1833 	ovs_unlock();
1834 	kfree_skb(reply);
1835 	return err;
1836 }
1837 
1838 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1839 {
1840 	struct sk_buff *reply;
1841 	struct datapath *dp;
1842 	int err;
1843 
1844 	reply = ovs_dp_cmd_alloc_info();
1845 	if (!reply)
1846 		return -ENOMEM;
1847 
1848 	ovs_lock();
1849 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1850 	if (IS_ERR(dp)) {
1851 		err = PTR_ERR(dp);
1852 		goto err_unlock_free;
1853 	}
1854 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1855 				   info->snd_seq, 0, OVS_DP_CMD_GET);
1856 	BUG_ON(err < 0);
1857 	ovs_unlock();
1858 
1859 	return genlmsg_reply(reply, info);
1860 
1861 err_unlock_free:
1862 	ovs_unlock();
1863 	kfree_skb(reply);
1864 	return err;
1865 }
1866 
1867 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1868 {
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];
1872 	int i = 0;
1873 
1874 	ovs_lock();
1875 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
1876 		if (i >= skip &&
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)
1880 			break;
1881 		i++;
1882 	}
1883 	ovs_unlock();
1884 
1885 	cb->args[0] = i;
1886 
1887 	return skb->len;
1888 }
1889 
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 },
1894 };
1895 
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
1901 	},
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
1906 	},
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
1912 	},
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,
1917 	},
1918 };
1919 
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,
1926 	.netnsok = true,
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,
1931 	.n_mcgrps = 1,
1932 	.module = THIS_MODULE,
1933 };
1934 
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)
1939 {
1940 	struct ovs_header *ovs_header;
1941 	struct ovs_vport_stats vport_stats;
1942 	int err;
1943 
1944 	ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1945 				 flags, cmd);
1946 	if (!ovs_header)
1947 		return -EMSGSIZE;
1948 
1949 	ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1950 
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;
1957 
1958 	if (!net_eq(net, dev_net(vport->dev))) {
1959 		int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
1960 
1961 		if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1962 			goto nla_put_failure;
1963 	}
1964 
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;
1970 
1971 	if (ovs_vport_get_upcall_portids(vport, skb))
1972 		goto nla_put_failure;
1973 
1974 	err = ovs_vport_get_options(vport, skb);
1975 	if (err == -EMSGSIZE)
1976 		goto error;
1977 
1978 	genlmsg_end(skb, ovs_header);
1979 	return 0;
1980 
1981 nla_put_failure:
1982 	err = -EMSGSIZE;
1983 error:
1984 	genlmsg_cancel(skb, ovs_header);
1985 	return err;
1986 }
1987 
1988 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1989 {
1990 	return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1991 }
1992 
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)
1996 {
1997 	struct sk_buff *skb;
1998 	int retval;
1999 
2000 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2001 	if (!skb)
2002 		return ERR_PTR(-ENOMEM);
2003 
2004 	retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
2005 					 GFP_KERNEL);
2006 	BUG_ON(retval < 0);
2007 
2008 	return skb;
2009 }
2010 
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])
2015 {
2016 	struct datapath *dp;
2017 	struct vport *vport;
2018 
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]));
2023 		if (!vport)
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);
2028 		return vport;
2029 	} else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2030 		u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2031 
2032 		if (port_no >= DP_MAX_PORTS)
2033 			return ERR_PTR(-EFBIG);
2034 
2035 		dp = get_dp(net, ovs_header->dp_ifindex);
2036 		if (!dp)
2037 			return ERR_PTR(-ENODEV);
2038 
2039 		vport = ovs_vport_ovsl_rcu(dp, port_no);
2040 		if (!vport)
2041 			return ERR_PTR(-ENODEV);
2042 		return vport;
2043 	} else
2044 		return ERR_PTR(-EINVAL);
2045 
2046 }
2047 
2048 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2049 {
2050 	unsigned int dev_headroom, max_headroom = 0;
2051 	struct net_device *dev;
2052 	struct vport *vport;
2053 	int i;
2054 
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()) {
2058 			dev = vport->dev;
2059 			dev_headroom = netdev_get_fwd_headroom(dev);
2060 			if (dev_headroom > max_headroom)
2061 				max_headroom = dev_headroom;
2062 		}
2063 	}
2064 
2065 	return max_headroom;
2066 }
2067 
2068 /* Called with ovs_mutex */
2069 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2070 {
2071 	struct vport *vport;
2072 	int i;
2073 
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);
2079 }
2080 
2081 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2082 {
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;
2090 	u32 port_no;
2091 	int err;
2092 
2093 	if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2094 	    !a[OVS_VPORT_ATTR_UPCALL_PID])
2095 		return -EINVAL;
2096 	if (a[OVS_VPORT_ATTR_IFINDEX])
2097 		return -EOPNOTSUPP;
2098 
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)
2102 		return -EFBIG;
2103 
2104 	reply = ovs_vport_cmd_alloc_info();
2105 	if (!reply)
2106 		return -ENOMEM;
2107 
2108 	ovs_lock();
2109 restart:
2110 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2111 	err = -ENODEV;
2112 	if (!dp)
2113 		goto exit_unlock_free;
2114 
2115 	if (port_no) {
2116 		vport = ovs_vport_ovsl(dp, port_no);
2117 		err = -EBUSY;
2118 		if (vport)
2119 			goto exit_unlock_free;
2120 	} else {
2121 		for (port_no = 1; ; port_no++) {
2122 			if (port_no >= DP_MAX_PORTS) {
2123 				err = -EFBIG;
2124 				goto exit_unlock_free;
2125 			}
2126 			vport = ovs_vport_ovsl(dp, port_no);
2127 			if (!vport)
2128 				break;
2129 		}
2130 	}
2131 
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];
2135 	parms.dp = dp;
2136 	parms.port_no = port_no;
2137 	parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2138 
2139 	vport = new_vport(&parms);
2140 	err = PTR_ERR(vport);
2141 	if (IS_ERR(vport)) {
2142 		if (err == -EAGAIN)
2143 			goto restart;
2144 		goto exit_unlock_free;
2145 	}
2146 
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);
2150 
2151 	new_headroom = netdev_get_fwd_headroom(vport->dev);
2152 
2153 	if (new_headroom > dp->max_headroom)
2154 		ovs_update_headroom(dp, new_headroom);
2155 	else
2156 		netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2157 
2158 	BUG_ON(err < 0);
2159 	ovs_unlock();
2160 
2161 	ovs_notify(&dp_vport_genl_family, reply, info);
2162 	return 0;
2163 
2164 exit_unlock_free:
2165 	ovs_unlock();
2166 	kfree_skb(reply);
2167 	return err;
2168 }
2169 
2170 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2171 {
2172 	struct nlattr **a = info->attrs;
2173 	struct sk_buff *reply;
2174 	struct vport *vport;
2175 	int err;
2176 
2177 	reply = ovs_vport_cmd_alloc_info();
2178 	if (!reply)
2179 		return -ENOMEM;
2180 
2181 	ovs_lock();
2182 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2183 	err = PTR_ERR(vport);
2184 	if (IS_ERR(vport))
2185 		goto exit_unlock_free;
2186 
2187 	if (a[OVS_VPORT_ATTR_TYPE] &&
2188 	    nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2189 		err = -EINVAL;
2190 		goto exit_unlock_free;
2191 	}
2192 
2193 	if (a[OVS_VPORT_ATTR_OPTIONS]) {
2194 		err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2195 		if (err)
2196 			goto exit_unlock_free;
2197 	}
2198 
2199 
2200 	if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2201 		struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2202 
2203 		err = ovs_vport_set_upcall_portids(vport, ids);
2204 		if (err)
2205 			goto exit_unlock_free;
2206 	}
2207 
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);
2211 	BUG_ON(err < 0);
2212 
2213 	ovs_unlock();
2214 	ovs_notify(&dp_vport_genl_family, reply, info);
2215 	return 0;
2216 
2217 exit_unlock_free:
2218 	ovs_unlock();
2219 	kfree_skb(reply);
2220 	return err;
2221 }
2222 
2223 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2224 {
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;
2231 	int err;
2232 
2233 	reply = ovs_vport_cmd_alloc_info();
2234 	if (!reply)
2235 		return -ENOMEM;
2236 
2237 	ovs_lock();
2238 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2239 	err = PTR_ERR(vport);
2240 	if (IS_ERR(vport))
2241 		goto exit_unlock_free;
2242 
2243 	if (vport->port_no == OVSP_LOCAL) {
2244 		err = -EINVAL;
2245 		goto exit_unlock_free;
2246 	}
2247 
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);
2251 	BUG_ON(err < 0);
2252 
2253 	/* the vport deletion may trigger dp headroom update */
2254 	dp = vport->dp;
2255 	if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2256 		update_headroom = true;
2257 
2258 	netdev_reset_rx_headroom(vport->dev);
2259 	ovs_dp_detach_port(vport);
2260 
2261 	if (update_headroom) {
2262 		new_headroom = ovs_get_max_headroom(dp);
2263 
2264 		if (new_headroom < dp->max_headroom)
2265 			ovs_update_headroom(dp, new_headroom);
2266 	}
2267 	ovs_unlock();
2268 
2269 	ovs_notify(&dp_vport_genl_family, reply, info);
2270 	return 0;
2271 
2272 exit_unlock_free:
2273 	ovs_unlock();
2274 	kfree_skb(reply);
2275 	return err;
2276 }
2277 
2278 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2279 {
2280 	struct nlattr **a = info->attrs;
2281 	struct ovs_header *ovs_header = info->userhdr;
2282 	struct sk_buff *reply;
2283 	struct vport *vport;
2284 	int err;
2285 
2286 	reply = ovs_vport_cmd_alloc_info();
2287 	if (!reply)
2288 		return -ENOMEM;
2289 
2290 	rcu_read_lock();
2291 	vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2292 	err = PTR_ERR(vport);
2293 	if (IS_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);
2298 	BUG_ON(err < 0);
2299 	rcu_read_unlock();
2300 
2301 	return genlmsg_reply(reply, info);
2302 
2303 exit_unlock_free:
2304 	rcu_read_unlock();
2305 	kfree_skb(reply);
2306 	return err;
2307 }
2308 
2309 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2310 {
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];
2314 	int i, j = 0;
2315 
2316 	rcu_read_lock();
2317 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2318 	if (!dp) {
2319 		rcu_read_unlock();
2320 		return -ENODEV;
2321 	}
2322 	for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2323 		struct vport *vport;
2324 
2325 		j = 0;
2326 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2327 			if (j >= skip &&
2328 			    ovs_vport_cmd_fill_info(vport, skb,
2329 						    sock_net(skb->sk),
2330 						    NETLINK_CB(cb->skb).portid,
2331 						    cb->nlh->nlmsg_seq,
2332 						    NLM_F_MULTI,
2333 						    OVS_VPORT_CMD_GET,
2334 						    GFP_ATOMIC) < 0)
2335 				goto out;
2336 
2337 			j++;
2338 		}
2339 		skip = 0;
2340 	}
2341 out:
2342 	rcu_read_unlock();
2343 
2344 	cb->args[0] = i;
2345 	cb->args[1] = j;
2346 
2347 	return skb->len;
2348 }
2349 
2350 static void ovs_dp_masks_rebalance(struct work_struct *work)
2351 {
2352 	struct datapath *dp = container_of(work, struct datapath,
2353 					   masks_rebalance.work);
2354 
2355 	ovs_lock();
2356 	ovs_flow_masks_rebalance(&dp->table);
2357 	ovs_unlock();
2358 
2359 	schedule_delayed_work(&dp->masks_rebalance,
2360 			      msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2361 }
2362 
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 },
2372 };
2373 
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
2379 	},
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
2384 	},
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
2390 	},
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,
2395 	},
2396 };
2397 
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,
2404 	.netnsok = true,
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,
2409 	.n_mcgrps = 1,
2410 	.module = THIS_MODULE,
2411 };
2412 
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,
2421 #endif
2422 };
2423 
2424 static void dp_unregister_genl(int n_families)
2425 {
2426 	int i;
2427 
2428 	for (i = 0; i < n_families; i++)
2429 		genl_unregister_family(dp_genl_families[i]);
2430 }
2431 
2432 static int __init dp_register_genl(void)
2433 {
2434 	int err;
2435 	int i;
2436 
2437 	for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2438 
2439 		err = genl_register_family(dp_genl_families[i]);
2440 		if (err)
2441 			goto error;
2442 	}
2443 
2444 	return 0;
2445 
2446 error:
2447 	dp_unregister_genl(i);
2448 	return err;
2449 }
2450 
2451 static int __net_init ovs_init_net(struct net *net)
2452 {
2453 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2454 
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);
2458 }
2459 
2460 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2461 					    struct list_head *head)
2462 {
2463 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2464 	struct datapath *dp;
2465 
2466 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
2467 		int i;
2468 
2469 		for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2470 			struct vport *vport;
2471 
2472 			hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2473 				if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2474 					continue;
2475 
2476 				if (dev_net(vport->dev) == dnet)
2477 					list_add(&vport->detach_list, head);
2478 			}
2479 		}
2480 	}
2481 }
2482 
2483 static void __net_exit ovs_exit_net(struct net *dnet)
2484 {
2485 	struct datapath *dp, *dp_next;
2486 	struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2487 	struct vport *vport, *vport_next;
2488 	struct net *net;
2489 	LIST_HEAD(head);
2490 
2491 	ovs_lock();
2492 
2493 	ovs_ct_exit(dnet);
2494 
2495 	list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2496 		__dp_destroy(dp);
2497 
2498 	down_read(&net_rwsem);
2499 	for_each_net(net)
2500 		list_vports_from_net(net, dnet, &head);
2501 	up_read(&net_rwsem);
2502 
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);
2507 	}
2508 
2509 	ovs_unlock();
2510 
2511 	cancel_work_sync(&ovs_net->dp_notify_work);
2512 }
2513 
2514 static struct pernet_operations ovs_net_ops = {
2515 	.init = ovs_init_net,
2516 	.exit = ovs_exit_net,
2517 	.id   = &ovs_net_id,
2518 	.size = sizeof(struct ovs_net),
2519 };
2520 
2521 static int __init dp_init(void)
2522 {
2523 	int err;
2524 
2525 	BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof_field(struct sk_buff, cb));
2526 
2527 	pr_info("Open vSwitch switching datapath\n");
2528 
2529 	err = action_fifos_init();
2530 	if (err)
2531 		goto error;
2532 
2533 	err = ovs_internal_dev_rtnl_link_register();
2534 	if (err)
2535 		goto error_action_fifos_exit;
2536 
2537 	err = ovs_flow_init();
2538 	if (err)
2539 		goto error_unreg_rtnl_link;
2540 
2541 	err = ovs_vport_init();
2542 	if (err)
2543 		goto error_flow_exit;
2544 
2545 	err = register_pernet_device(&ovs_net_ops);
2546 	if (err)
2547 		goto error_vport_exit;
2548 
2549 	err = register_netdevice_notifier(&ovs_dp_device_notifier);
2550 	if (err)
2551 		goto error_netns_exit;
2552 
2553 	err = ovs_netdev_init();
2554 	if (err)
2555 		goto error_unreg_notifier;
2556 
2557 	err = dp_register_genl();
2558 	if (err < 0)
2559 		goto error_unreg_netdev;
2560 
2561 	return 0;
2562 
2563 error_unreg_netdev:
2564 	ovs_netdev_exit();
2565 error_unreg_notifier:
2566 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2567 error_netns_exit:
2568 	unregister_pernet_device(&ovs_net_ops);
2569 error_vport_exit:
2570 	ovs_vport_exit();
2571 error_flow_exit:
2572 	ovs_flow_exit();
2573 error_unreg_rtnl_link:
2574 	ovs_internal_dev_rtnl_link_unregister();
2575 error_action_fifos_exit:
2576 	action_fifos_exit();
2577 error:
2578 	return err;
2579 }
2580 
2581 static void dp_cleanup(void)
2582 {
2583 	dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2584 	ovs_netdev_exit();
2585 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2586 	unregister_pernet_device(&ovs_net_ops);
2587 	rcu_barrier();
2588 	ovs_vport_exit();
2589 	ovs_flow_exit();
2590 	ovs_internal_dev_rtnl_link_unregister();
2591 	action_fifos_exit();
2592 }
2593 
2594 module_init(dp_init);
2595 module_exit(dp_cleanup);
2596 
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);
2605