xref: /linux/net/openvswitch/datapath.c (revision 2c63221cd9e5c0dad0424029aeb1c40faada8330)
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 /* Must be called with rcu_read_lock or ovs_mutex. */
134 const char *ovs_dp_name(const struct datapath *dp)
135 {
136 	struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
137 	return ovs_vport_name(vport);
138 }
139 
140 static int get_dpifindex(const struct datapath *dp)
141 {
142 	struct vport *local;
143 	int ifindex;
144 
145 	rcu_read_lock();
146 
147 	local = ovs_vport_rcu(dp, OVSP_LOCAL);
148 	if (local)
149 		ifindex = local->dev->ifindex;
150 	else
151 		ifindex = 0;
152 
153 	rcu_read_unlock();
154 
155 	return ifindex;
156 }
157 
158 static void destroy_dp_rcu(struct rcu_head *rcu)
159 {
160 	struct datapath *dp = container_of(rcu, struct datapath, rcu);
161 
162 	ovs_flow_tbl_destroy(&dp->table);
163 	free_percpu(dp->stats_percpu);
164 	kfree(dp->ports);
165 	ovs_meters_exit(dp);
166 	kfree(dp);
167 }
168 
169 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
170 					    u16 port_no)
171 {
172 	return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
173 }
174 
175 /* Called with ovs_mutex or RCU read lock. */
176 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
177 {
178 	struct vport *vport;
179 	struct hlist_head *head;
180 
181 	head = vport_hash_bucket(dp, port_no);
182 	hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
183 		if (vport->port_no == port_no)
184 			return vport;
185 	}
186 	return NULL;
187 }
188 
189 /* Called with ovs_mutex. */
190 static struct vport *new_vport(const struct vport_parms *parms)
191 {
192 	struct vport *vport;
193 
194 	vport = ovs_vport_add(parms);
195 	if (!IS_ERR(vport)) {
196 		struct datapath *dp = parms->dp;
197 		struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
198 
199 		hlist_add_head_rcu(&vport->dp_hash_node, head);
200 	}
201 	return vport;
202 }
203 
204 void ovs_dp_detach_port(struct vport *p)
205 {
206 	ASSERT_OVSL();
207 
208 	/* First drop references to device. */
209 	hlist_del_rcu(&p->dp_hash_node);
210 
211 	/* Then destroy it. */
212 	ovs_vport_del(p);
213 }
214 
215 /* Must be called with rcu_read_lock. */
216 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
217 {
218 	const struct vport *p = OVS_CB(skb)->input_vport;
219 	struct datapath *dp = p->dp;
220 	struct sw_flow *flow;
221 	struct sw_flow_actions *sf_acts;
222 	struct dp_stats_percpu *stats;
223 	u64 *stats_counter;
224 	u32 n_mask_hit;
225 	int error;
226 
227 	stats = this_cpu_ptr(dp->stats_percpu);
228 
229 	/* Look up flow. */
230 	flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
231 					 &n_mask_hit);
232 	if (unlikely(!flow)) {
233 		struct dp_upcall_info upcall;
234 
235 		memset(&upcall, 0, sizeof(upcall));
236 		upcall.cmd = OVS_PACKET_CMD_MISS;
237 		upcall.portid = ovs_vport_find_upcall_portid(p, skb);
238 		upcall.mru = OVS_CB(skb)->mru;
239 		error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
240 		if (unlikely(error))
241 			kfree_skb(skb);
242 		else
243 			consume_skb(skb);
244 		stats_counter = &stats->n_missed;
245 		goto out;
246 	}
247 
248 	ovs_flow_stats_update(flow, key->tp.flags, skb);
249 	sf_acts = rcu_dereference(flow->sf_acts);
250 	error = ovs_execute_actions(dp, skb, sf_acts, key);
251 	if (unlikely(error))
252 		net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
253 							ovs_dp_name(dp), error);
254 
255 	stats_counter = &stats->n_hit;
256 
257 out:
258 	/* Update datapath statistics. */
259 	u64_stats_update_begin(&stats->syncp);
260 	(*stats_counter)++;
261 	stats->n_mask_hit += n_mask_hit;
262 	u64_stats_update_end(&stats->syncp);
263 }
264 
265 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
266 		  const struct sw_flow_key *key,
267 		  const struct dp_upcall_info *upcall_info,
268 		  uint32_t cutlen)
269 {
270 	struct dp_stats_percpu *stats;
271 	int err;
272 
273 	if (upcall_info->portid == 0) {
274 		err = -ENOTCONN;
275 		goto err;
276 	}
277 
278 	if (!skb_is_gso(skb))
279 		err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
280 	else
281 		err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
282 	if (err)
283 		goto err;
284 
285 	return 0;
286 
287 err:
288 	stats = this_cpu_ptr(dp->stats_percpu);
289 
290 	u64_stats_update_begin(&stats->syncp);
291 	stats->n_lost++;
292 	u64_stats_update_end(&stats->syncp);
293 
294 	return err;
295 }
296 
297 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
298 			     const struct sw_flow_key *key,
299 			     const struct dp_upcall_info *upcall_info,
300 				 uint32_t cutlen)
301 {
302 	unsigned int gso_type = skb_shinfo(skb)->gso_type;
303 	struct sw_flow_key later_key;
304 	struct sk_buff *segs, *nskb;
305 	int err;
306 
307 	BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
308 	segs = __skb_gso_segment(skb, NETIF_F_SG, false);
309 	if (IS_ERR(segs))
310 		return PTR_ERR(segs);
311 	if (segs == NULL)
312 		return -EINVAL;
313 
314 	if (gso_type & SKB_GSO_UDP) {
315 		/* The initial flow key extracted by ovs_flow_key_extract()
316 		 * in this case is for a first fragment, so we need to
317 		 * properly mark later fragments.
318 		 */
319 		later_key = *key;
320 		later_key.ip.frag = OVS_FRAG_TYPE_LATER;
321 	}
322 
323 	/* Queue all of the segments. */
324 	skb = segs;
325 	do {
326 		if (gso_type & SKB_GSO_UDP && skb != segs)
327 			key = &later_key;
328 
329 		err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
330 		if (err)
331 			break;
332 
333 	} while ((skb = skb->next));
334 
335 	/* Free all of the segments. */
336 	skb = segs;
337 	do {
338 		nskb = skb->next;
339 		if (err)
340 			kfree_skb(skb);
341 		else
342 			consume_skb(skb);
343 	} while ((skb = nskb));
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 
355 	/* OVS_PACKET_ATTR_USERDATA */
356 	if (upcall_info->userdata)
357 		size += NLA_ALIGN(upcall_info->userdata->nla_len);
358 
359 	/* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
360 	if (upcall_info->egress_tun_info)
361 		size += nla_total_size(ovs_tun_key_attr_size());
362 
363 	/* OVS_PACKET_ATTR_ACTIONS */
364 	if (upcall_info->actions_len)
365 		size += nla_total_size(actions_attrlen);
366 
367 	/* OVS_PACKET_ATTR_MRU */
368 	if (upcall_info->mru)
369 		size += nla_total_size(sizeof(upcall_info->mru));
370 
371 	return size;
372 }
373 
374 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
375 {
376 	if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
377 		size_t plen = NLA_ALIGN(skb->len) - skb->len;
378 
379 		if (plen > 0)
380 			skb_put_zero(skb, plen);
381 	}
382 }
383 
384 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
385 				  const struct sw_flow_key *key,
386 				  const struct dp_upcall_info *upcall_info,
387 				  uint32_t cutlen)
388 {
389 	struct ovs_header *upcall;
390 	struct sk_buff *nskb = NULL;
391 	struct sk_buff *user_skb = NULL; /* to be queued to userspace */
392 	struct nlattr *nla;
393 	size_t len;
394 	unsigned int hlen;
395 	int err, dp_ifindex;
396 
397 	dp_ifindex = get_dpifindex(dp);
398 	if (!dp_ifindex)
399 		return -ENODEV;
400 
401 	if (skb_vlan_tag_present(skb)) {
402 		nskb = skb_clone(skb, GFP_ATOMIC);
403 		if (!nskb)
404 			return -ENOMEM;
405 
406 		nskb = __vlan_hwaccel_push_inside(nskb);
407 		if (!nskb)
408 			return -ENOMEM;
409 
410 		skb = nskb;
411 	}
412 
413 	if (nla_attr_size(skb->len) > USHRT_MAX) {
414 		err = -EFBIG;
415 		goto out;
416 	}
417 
418 	/* Complete checksum if needed */
419 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
420 	    (err = skb_csum_hwoffload_help(skb, 0)))
421 		goto out;
422 
423 	/* Older versions of OVS user space enforce alignment of the last
424 	 * Netlink attribute to NLA_ALIGNTO which would require extensive
425 	 * padding logic. Only perform zerocopy if padding is not required.
426 	 */
427 	if (dp->user_features & OVS_DP_F_UNALIGNED)
428 		hlen = skb_zerocopy_headlen(skb);
429 	else
430 		hlen = skb->len;
431 
432 	len = upcall_msg_size(upcall_info, hlen - cutlen,
433 			      OVS_CB(skb)->acts_origlen);
434 	user_skb = genlmsg_new(len, GFP_ATOMIC);
435 	if (!user_skb) {
436 		err = -ENOMEM;
437 		goto out;
438 	}
439 
440 	upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
441 			     0, upcall_info->cmd);
442 	if (!upcall) {
443 		err = -EINVAL;
444 		goto out;
445 	}
446 	upcall->dp_ifindex = dp_ifindex;
447 
448 	err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
449 	if (err)
450 		goto out;
451 
452 	if (upcall_info->userdata)
453 		__nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
454 			  nla_len(upcall_info->userdata),
455 			  nla_data(upcall_info->userdata));
456 
457 	if (upcall_info->egress_tun_info) {
458 		nla = nla_nest_start_noflag(user_skb,
459 					    OVS_PACKET_ATTR_EGRESS_TUN_KEY);
460 		if (!nla) {
461 			err = -EMSGSIZE;
462 			goto out;
463 		}
464 		err = ovs_nla_put_tunnel_info(user_skb,
465 					      upcall_info->egress_tun_info);
466 		if (err)
467 			goto out;
468 
469 		nla_nest_end(user_skb, nla);
470 	}
471 
472 	if (upcall_info->actions_len) {
473 		nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
474 		if (!nla) {
475 			err = -EMSGSIZE;
476 			goto out;
477 		}
478 		err = ovs_nla_put_actions(upcall_info->actions,
479 					  upcall_info->actions_len,
480 					  user_skb);
481 		if (!err)
482 			nla_nest_end(user_skb, nla);
483 		else
484 			nla_nest_cancel(user_skb, nla);
485 	}
486 
487 	/* Add OVS_PACKET_ATTR_MRU */
488 	if (upcall_info->mru) {
489 		if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
490 				upcall_info->mru)) {
491 			err = -ENOBUFS;
492 			goto out;
493 		}
494 		pad_packet(dp, user_skb);
495 	}
496 
497 	/* Add OVS_PACKET_ATTR_LEN when packet is truncated */
498 	if (cutlen > 0) {
499 		if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
500 				skb->len)) {
501 			err = -ENOBUFS;
502 			goto out;
503 		}
504 		pad_packet(dp, user_skb);
505 	}
506 
507 	/* Only reserve room for attribute header, packet data is added
508 	 * in skb_zerocopy() */
509 	if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
510 		err = -ENOBUFS;
511 		goto out;
512 	}
513 	nla->nla_len = nla_attr_size(skb->len - cutlen);
514 
515 	err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
516 	if (err)
517 		goto out;
518 
519 	/* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
520 	pad_packet(dp, user_skb);
521 
522 	((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
523 
524 	err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
525 	user_skb = NULL;
526 out:
527 	if (err)
528 		skb_tx_error(skb);
529 	kfree_skb(user_skb);
530 	kfree_skb(nskb);
531 	return err;
532 }
533 
534 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
535 {
536 	struct ovs_header *ovs_header = info->userhdr;
537 	struct net *net = sock_net(skb->sk);
538 	struct nlattr **a = info->attrs;
539 	struct sw_flow_actions *acts;
540 	struct sk_buff *packet;
541 	struct sw_flow *flow;
542 	struct sw_flow_actions *sf_acts;
543 	struct datapath *dp;
544 	struct vport *input_vport;
545 	u16 mru = 0;
546 	int len;
547 	int err;
548 	bool log = !a[OVS_PACKET_ATTR_PROBE];
549 
550 	err = -EINVAL;
551 	if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
552 	    !a[OVS_PACKET_ATTR_ACTIONS])
553 		goto err;
554 
555 	len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
556 	packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
557 	err = -ENOMEM;
558 	if (!packet)
559 		goto err;
560 	skb_reserve(packet, NET_IP_ALIGN);
561 
562 	nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
563 
564 	/* Set packet's mru */
565 	if (a[OVS_PACKET_ATTR_MRU]) {
566 		mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
567 		packet->ignore_df = 1;
568 	}
569 	OVS_CB(packet)->mru = mru;
570 
571 	/* Build an sw_flow for sending this packet. */
572 	flow = ovs_flow_alloc();
573 	err = PTR_ERR(flow);
574 	if (IS_ERR(flow))
575 		goto err_kfree_skb;
576 
577 	err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
578 					     packet, &flow->key, log);
579 	if (err)
580 		goto err_flow_free;
581 
582 	err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
583 				   &flow->key, &acts, log);
584 	if (err)
585 		goto err_flow_free;
586 
587 	rcu_assign_pointer(flow->sf_acts, acts);
588 	packet->priority = flow->key.phy.priority;
589 	packet->mark = flow->key.phy.skb_mark;
590 
591 	rcu_read_lock();
592 	dp = get_dp_rcu(net, ovs_header->dp_ifindex);
593 	err = -ENODEV;
594 	if (!dp)
595 		goto err_unlock;
596 
597 	input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
598 	if (!input_vport)
599 		input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
600 
601 	if (!input_vport)
602 		goto err_unlock;
603 
604 	packet->dev = input_vport->dev;
605 	OVS_CB(packet)->input_vport = input_vport;
606 	sf_acts = rcu_dereference(flow->sf_acts);
607 
608 	local_bh_disable();
609 	err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
610 	local_bh_enable();
611 	rcu_read_unlock();
612 
613 	ovs_flow_free(flow, false);
614 	return err;
615 
616 err_unlock:
617 	rcu_read_unlock();
618 err_flow_free:
619 	ovs_flow_free(flow, false);
620 err_kfree_skb:
621 	kfree_skb(packet);
622 err:
623 	return err;
624 }
625 
626 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
627 	[OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
628 	[OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
629 	[OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
630 	[OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
631 	[OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
632 };
633 
634 static const struct genl_ops dp_packet_genl_ops[] = {
635 	{ .cmd = OVS_PACKET_CMD_EXECUTE,
636 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
637 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
638 	  .doit = ovs_packet_cmd_execute
639 	}
640 };
641 
642 static struct genl_family dp_packet_genl_family __ro_after_init = {
643 	.hdrsize = sizeof(struct ovs_header),
644 	.name = OVS_PACKET_FAMILY,
645 	.version = OVS_PACKET_VERSION,
646 	.maxattr = OVS_PACKET_ATTR_MAX,
647 	.policy = packet_policy,
648 	.netnsok = true,
649 	.parallel_ops = true,
650 	.ops = dp_packet_genl_ops,
651 	.n_ops = ARRAY_SIZE(dp_packet_genl_ops),
652 	.module = THIS_MODULE,
653 };
654 
655 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
656 			 struct ovs_dp_megaflow_stats *mega_stats)
657 {
658 	int i;
659 
660 	memset(mega_stats, 0, sizeof(*mega_stats));
661 
662 	stats->n_flows = ovs_flow_tbl_count(&dp->table);
663 	mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
664 
665 	stats->n_hit = stats->n_missed = stats->n_lost = 0;
666 
667 	for_each_possible_cpu(i) {
668 		const struct dp_stats_percpu *percpu_stats;
669 		struct dp_stats_percpu local_stats;
670 		unsigned int start;
671 
672 		percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
673 
674 		do {
675 			start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
676 			local_stats = *percpu_stats;
677 		} while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
678 
679 		stats->n_hit += local_stats.n_hit;
680 		stats->n_missed += local_stats.n_missed;
681 		stats->n_lost += local_stats.n_lost;
682 		mega_stats->n_mask_hit += local_stats.n_mask_hit;
683 	}
684 }
685 
686 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
687 {
688 	return ovs_identifier_is_ufid(sfid) &&
689 	       !(ufid_flags & OVS_UFID_F_OMIT_KEY);
690 }
691 
692 static bool should_fill_mask(uint32_t ufid_flags)
693 {
694 	return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
695 }
696 
697 static bool should_fill_actions(uint32_t ufid_flags)
698 {
699 	return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
700 }
701 
702 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
703 				    const struct sw_flow_id *sfid,
704 				    uint32_t ufid_flags)
705 {
706 	size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
707 
708 	/* OVS_FLOW_ATTR_UFID */
709 	if (sfid && ovs_identifier_is_ufid(sfid))
710 		len += nla_total_size(sfid->ufid_len);
711 
712 	/* OVS_FLOW_ATTR_KEY */
713 	if (!sfid || should_fill_key(sfid, ufid_flags))
714 		len += nla_total_size(ovs_key_attr_size());
715 
716 	/* OVS_FLOW_ATTR_MASK */
717 	if (should_fill_mask(ufid_flags))
718 		len += nla_total_size(ovs_key_attr_size());
719 
720 	/* OVS_FLOW_ATTR_ACTIONS */
721 	if (should_fill_actions(ufid_flags))
722 		len += nla_total_size(acts->orig_len);
723 
724 	return len
725 		+ nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
726 		+ nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
727 		+ nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
728 }
729 
730 /* Called with ovs_mutex or RCU read lock. */
731 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
732 				   struct sk_buff *skb)
733 {
734 	struct ovs_flow_stats stats;
735 	__be16 tcp_flags;
736 	unsigned long used;
737 
738 	ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
739 
740 	if (used &&
741 	    nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
742 			      OVS_FLOW_ATTR_PAD))
743 		return -EMSGSIZE;
744 
745 	if (stats.n_packets &&
746 	    nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
747 			  sizeof(struct ovs_flow_stats), &stats,
748 			  OVS_FLOW_ATTR_PAD))
749 		return -EMSGSIZE;
750 
751 	if ((u8)ntohs(tcp_flags) &&
752 	     nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
753 		return -EMSGSIZE;
754 
755 	return 0;
756 }
757 
758 /* Called with ovs_mutex or RCU read lock. */
759 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
760 				     struct sk_buff *skb, int skb_orig_len)
761 {
762 	struct nlattr *start;
763 	int err;
764 
765 	/* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
766 	 * this is the first flow to be dumped into 'skb'.  This is unusual for
767 	 * Netlink but individual action lists can be longer than
768 	 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
769 	 * The userspace caller can always fetch the actions separately if it
770 	 * really wants them.  (Most userspace callers in fact don't care.)
771 	 *
772 	 * This can only fail for dump operations because the skb is always
773 	 * properly sized for single flows.
774 	 */
775 	start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
776 	if (start) {
777 		const struct sw_flow_actions *sf_acts;
778 
779 		sf_acts = rcu_dereference_ovsl(flow->sf_acts);
780 		err = ovs_nla_put_actions(sf_acts->actions,
781 					  sf_acts->actions_len, skb);
782 
783 		if (!err)
784 			nla_nest_end(skb, start);
785 		else {
786 			if (skb_orig_len)
787 				return err;
788 
789 			nla_nest_cancel(skb, start);
790 		}
791 	} else if (skb_orig_len) {
792 		return -EMSGSIZE;
793 	}
794 
795 	return 0;
796 }
797 
798 /* Called with ovs_mutex or RCU read lock. */
799 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
800 				  struct sk_buff *skb, u32 portid,
801 				  u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
802 {
803 	const int skb_orig_len = skb->len;
804 	struct ovs_header *ovs_header;
805 	int err;
806 
807 	ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
808 				 flags, cmd);
809 	if (!ovs_header)
810 		return -EMSGSIZE;
811 
812 	ovs_header->dp_ifindex = dp_ifindex;
813 
814 	err = ovs_nla_put_identifier(flow, skb);
815 	if (err)
816 		goto error;
817 
818 	if (should_fill_key(&flow->id, ufid_flags)) {
819 		err = ovs_nla_put_masked_key(flow, skb);
820 		if (err)
821 			goto error;
822 	}
823 
824 	if (should_fill_mask(ufid_flags)) {
825 		err = ovs_nla_put_mask(flow, skb);
826 		if (err)
827 			goto error;
828 	}
829 
830 	err = ovs_flow_cmd_fill_stats(flow, skb);
831 	if (err)
832 		goto error;
833 
834 	if (should_fill_actions(ufid_flags)) {
835 		err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
836 		if (err)
837 			goto error;
838 	}
839 
840 	genlmsg_end(skb, ovs_header);
841 	return 0;
842 
843 error:
844 	genlmsg_cancel(skb, ovs_header);
845 	return err;
846 }
847 
848 /* May not be called with RCU read lock. */
849 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
850 					       const struct sw_flow_id *sfid,
851 					       struct genl_info *info,
852 					       bool always,
853 					       uint32_t ufid_flags)
854 {
855 	struct sk_buff *skb;
856 	size_t len;
857 
858 	if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
859 		return NULL;
860 
861 	len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
862 	skb = genlmsg_new(len, GFP_KERNEL);
863 	if (!skb)
864 		return ERR_PTR(-ENOMEM);
865 
866 	return skb;
867 }
868 
869 /* Called with ovs_mutex. */
870 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
871 					       int dp_ifindex,
872 					       struct genl_info *info, u8 cmd,
873 					       bool always, u32 ufid_flags)
874 {
875 	struct sk_buff *skb;
876 	int retval;
877 
878 	skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
879 				      &flow->id, info, always, ufid_flags);
880 	if (IS_ERR_OR_NULL(skb))
881 		return skb;
882 
883 	retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
884 					info->snd_portid, info->snd_seq, 0,
885 					cmd, ufid_flags);
886 	BUG_ON(retval < 0);
887 	return skb;
888 }
889 
890 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
891 {
892 	struct net *net = sock_net(skb->sk);
893 	struct nlattr **a = info->attrs;
894 	struct ovs_header *ovs_header = info->userhdr;
895 	struct sw_flow *flow = NULL, *new_flow;
896 	struct sw_flow_mask mask;
897 	struct sk_buff *reply;
898 	struct datapath *dp;
899 	struct sw_flow_actions *acts;
900 	struct sw_flow_match match;
901 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
902 	int error;
903 	bool log = !a[OVS_FLOW_ATTR_PROBE];
904 
905 	/* Must have key and actions. */
906 	error = -EINVAL;
907 	if (!a[OVS_FLOW_ATTR_KEY]) {
908 		OVS_NLERR(log, "Flow key attr not present in new flow.");
909 		goto error;
910 	}
911 	if (!a[OVS_FLOW_ATTR_ACTIONS]) {
912 		OVS_NLERR(log, "Flow actions attr not present in new flow.");
913 		goto error;
914 	}
915 
916 	/* Most of the time we need to allocate a new flow, do it before
917 	 * locking.
918 	 */
919 	new_flow = ovs_flow_alloc();
920 	if (IS_ERR(new_flow)) {
921 		error = PTR_ERR(new_flow);
922 		goto error;
923 	}
924 
925 	/* Extract key. */
926 	ovs_match_init(&match, &new_flow->key, false, &mask);
927 	error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
928 				  a[OVS_FLOW_ATTR_MASK], log);
929 	if (error)
930 		goto err_kfree_flow;
931 
932 	/* Extract flow identifier. */
933 	error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
934 				       &new_flow->key, log);
935 	if (error)
936 		goto err_kfree_flow;
937 
938 	/* unmasked key is needed to match when ufid is not used. */
939 	if (ovs_identifier_is_key(&new_flow->id))
940 		match.key = new_flow->id.unmasked_key;
941 
942 	ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
943 
944 	/* Validate actions. */
945 	error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
946 				     &new_flow->key, &acts, log);
947 	if (error) {
948 		OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
949 		goto err_kfree_flow;
950 	}
951 
952 	reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
953 					ufid_flags);
954 	if (IS_ERR(reply)) {
955 		error = PTR_ERR(reply);
956 		goto err_kfree_acts;
957 	}
958 
959 	ovs_lock();
960 	dp = get_dp(net, ovs_header->dp_ifindex);
961 	if (unlikely(!dp)) {
962 		error = -ENODEV;
963 		goto err_unlock_ovs;
964 	}
965 
966 	/* Check if this is a duplicate flow */
967 	if (ovs_identifier_is_ufid(&new_flow->id))
968 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
969 	if (!flow)
970 		flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
971 	if (likely(!flow)) {
972 		rcu_assign_pointer(new_flow->sf_acts, acts);
973 
974 		/* Put flow in bucket. */
975 		error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
976 		if (unlikely(error)) {
977 			acts = NULL;
978 			goto err_unlock_ovs;
979 		}
980 
981 		if (unlikely(reply)) {
982 			error = ovs_flow_cmd_fill_info(new_flow,
983 						       ovs_header->dp_ifindex,
984 						       reply, info->snd_portid,
985 						       info->snd_seq, 0,
986 						       OVS_FLOW_CMD_NEW,
987 						       ufid_flags);
988 			BUG_ON(error < 0);
989 		}
990 		ovs_unlock();
991 	} else {
992 		struct sw_flow_actions *old_acts;
993 
994 		/* Bail out if we're not allowed to modify an existing flow.
995 		 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
996 		 * because Generic Netlink treats the latter as a dump
997 		 * request.  We also accept NLM_F_EXCL in case that bug ever
998 		 * gets fixed.
999 		 */
1000 		if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1001 							 | NLM_F_EXCL))) {
1002 			error = -EEXIST;
1003 			goto err_unlock_ovs;
1004 		}
1005 		/* The flow identifier has to be the same for flow updates.
1006 		 * Look for any overlapping flow.
1007 		 */
1008 		if (unlikely(!ovs_flow_cmp(flow, &match))) {
1009 			if (ovs_identifier_is_key(&flow->id))
1010 				flow = ovs_flow_tbl_lookup_exact(&dp->table,
1011 								 &match);
1012 			else /* UFID matches but key is different */
1013 				flow = NULL;
1014 			if (!flow) {
1015 				error = -ENOENT;
1016 				goto err_unlock_ovs;
1017 			}
1018 		}
1019 		/* Update actions. */
1020 		old_acts = ovsl_dereference(flow->sf_acts);
1021 		rcu_assign_pointer(flow->sf_acts, acts);
1022 
1023 		if (unlikely(reply)) {
1024 			error = ovs_flow_cmd_fill_info(flow,
1025 						       ovs_header->dp_ifindex,
1026 						       reply, info->snd_portid,
1027 						       info->snd_seq, 0,
1028 						       OVS_FLOW_CMD_NEW,
1029 						       ufid_flags);
1030 			BUG_ON(error < 0);
1031 		}
1032 		ovs_unlock();
1033 
1034 		ovs_nla_free_flow_actions_rcu(old_acts);
1035 		ovs_flow_free(new_flow, false);
1036 	}
1037 
1038 	if (reply)
1039 		ovs_notify(&dp_flow_genl_family, reply, info);
1040 	return 0;
1041 
1042 err_unlock_ovs:
1043 	ovs_unlock();
1044 	kfree_skb(reply);
1045 err_kfree_acts:
1046 	ovs_nla_free_flow_actions(acts);
1047 err_kfree_flow:
1048 	ovs_flow_free(new_flow, false);
1049 error:
1050 	return error;
1051 }
1052 
1053 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1054 static noinline_for_stack struct sw_flow_actions *get_flow_actions(struct net *net,
1055 						const struct nlattr *a,
1056 						const struct sw_flow_key *key,
1057 						const struct sw_flow_mask *mask,
1058 						bool log)
1059 {
1060 	struct sw_flow_actions *acts;
1061 	struct sw_flow_key masked_key;
1062 	int error;
1063 
1064 	ovs_flow_mask_key(&masked_key, key, true, mask);
1065 	error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1066 	if (error) {
1067 		OVS_NLERR(log,
1068 			  "Actions may not be safe on all matching packets");
1069 		return ERR_PTR(error);
1070 	}
1071 
1072 	return acts;
1073 }
1074 
1075 /* Factor out match-init and action-copy to avoid
1076  * "Wframe-larger-than=1024" warning. Because mask is only
1077  * used to get actions, we new a function to save some
1078  * stack space.
1079  *
1080  * If there are not key and action attrs, we return 0
1081  * directly. In the case, the caller will also not use the
1082  * match as before. If there is action attr, we try to get
1083  * actions and save them to *acts. Before returning from
1084  * the function, we reset the match->mask pointer. Because
1085  * we should not to return match object with dangling reference
1086  * to mask.
1087  * */
1088 static noinline_for_stack int
1089 ovs_nla_init_match_and_action(struct net *net,
1090 			      struct sw_flow_match *match,
1091 			      struct sw_flow_key *key,
1092 			      struct nlattr **a,
1093 			      struct sw_flow_actions **acts,
1094 			      bool log)
1095 {
1096 	struct sw_flow_mask mask;
1097 	int error = 0;
1098 
1099 	if (a[OVS_FLOW_ATTR_KEY]) {
1100 		ovs_match_init(match, key, true, &mask);
1101 		error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1102 					  a[OVS_FLOW_ATTR_MASK], log);
1103 		if (error)
1104 			goto error;
1105 	}
1106 
1107 	if (a[OVS_FLOW_ATTR_ACTIONS]) {
1108 		if (!a[OVS_FLOW_ATTR_KEY]) {
1109 			OVS_NLERR(log,
1110 				  "Flow key attribute not present in set flow.");
1111 			error = -EINVAL;
1112 			goto error;
1113 		}
1114 
1115 		*acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1116 					 &mask, log);
1117 		if (IS_ERR(*acts)) {
1118 			error = PTR_ERR(*acts);
1119 			goto error;
1120 		}
1121 	}
1122 
1123 	/* On success, error is 0. */
1124 error:
1125 	match->mask = NULL;
1126 	return error;
1127 }
1128 
1129 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1130 {
1131 	struct net *net = sock_net(skb->sk);
1132 	struct nlattr **a = info->attrs;
1133 	struct ovs_header *ovs_header = info->userhdr;
1134 	struct sw_flow_key key;
1135 	struct sw_flow *flow;
1136 	struct sk_buff *reply = NULL;
1137 	struct datapath *dp;
1138 	struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1139 	struct sw_flow_match match;
1140 	struct sw_flow_id sfid;
1141 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1142 	int error = 0;
1143 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1144 	bool ufid_present;
1145 
1146 	ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1147 	if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1148 		OVS_NLERR(log,
1149 			  "Flow set message rejected, Key attribute missing.");
1150 		return -EINVAL;
1151 	}
1152 
1153 	error = ovs_nla_init_match_and_action(net, &match, &key, a,
1154 					      &acts, log);
1155 	if (error)
1156 		goto error;
1157 
1158 	if (acts) {
1159 		/* Can allocate before locking if have acts. */
1160 		reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1161 						ufid_flags);
1162 		if (IS_ERR(reply)) {
1163 			error = PTR_ERR(reply);
1164 			goto err_kfree_acts;
1165 		}
1166 	}
1167 
1168 	ovs_lock();
1169 	dp = get_dp(net, ovs_header->dp_ifindex);
1170 	if (unlikely(!dp)) {
1171 		error = -ENODEV;
1172 		goto err_unlock_ovs;
1173 	}
1174 	/* Check that the flow exists. */
1175 	if (ufid_present)
1176 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1177 	else
1178 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1179 	if (unlikely(!flow)) {
1180 		error = -ENOENT;
1181 		goto err_unlock_ovs;
1182 	}
1183 
1184 	/* Update actions, if present. */
1185 	if (likely(acts)) {
1186 		old_acts = ovsl_dereference(flow->sf_acts);
1187 		rcu_assign_pointer(flow->sf_acts, acts);
1188 
1189 		if (unlikely(reply)) {
1190 			error = ovs_flow_cmd_fill_info(flow,
1191 						       ovs_header->dp_ifindex,
1192 						       reply, info->snd_portid,
1193 						       info->snd_seq, 0,
1194 						       OVS_FLOW_CMD_SET,
1195 						       ufid_flags);
1196 			BUG_ON(error < 0);
1197 		}
1198 	} else {
1199 		/* Could not alloc without acts before locking. */
1200 		reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1201 						info, OVS_FLOW_CMD_SET, false,
1202 						ufid_flags);
1203 
1204 		if (IS_ERR(reply)) {
1205 			error = PTR_ERR(reply);
1206 			goto err_unlock_ovs;
1207 		}
1208 	}
1209 
1210 	/* Clear stats. */
1211 	if (a[OVS_FLOW_ATTR_CLEAR])
1212 		ovs_flow_stats_clear(flow);
1213 	ovs_unlock();
1214 
1215 	if (reply)
1216 		ovs_notify(&dp_flow_genl_family, reply, info);
1217 	if (old_acts)
1218 		ovs_nla_free_flow_actions_rcu(old_acts);
1219 
1220 	return 0;
1221 
1222 err_unlock_ovs:
1223 	ovs_unlock();
1224 	kfree_skb(reply);
1225 err_kfree_acts:
1226 	ovs_nla_free_flow_actions(acts);
1227 error:
1228 	return error;
1229 }
1230 
1231 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1232 {
1233 	struct nlattr **a = info->attrs;
1234 	struct ovs_header *ovs_header = info->userhdr;
1235 	struct net *net = sock_net(skb->sk);
1236 	struct sw_flow_key key;
1237 	struct sk_buff *reply;
1238 	struct sw_flow *flow;
1239 	struct datapath *dp;
1240 	struct sw_flow_match match;
1241 	struct sw_flow_id ufid;
1242 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1243 	int err = 0;
1244 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1245 	bool ufid_present;
1246 
1247 	ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1248 	if (a[OVS_FLOW_ATTR_KEY]) {
1249 		ovs_match_init(&match, &key, true, NULL);
1250 		err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1251 					log);
1252 	} else if (!ufid_present) {
1253 		OVS_NLERR(log,
1254 			  "Flow get message rejected, Key attribute missing.");
1255 		err = -EINVAL;
1256 	}
1257 	if (err)
1258 		return err;
1259 
1260 	ovs_lock();
1261 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1262 	if (!dp) {
1263 		err = -ENODEV;
1264 		goto unlock;
1265 	}
1266 
1267 	if (ufid_present)
1268 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1269 	else
1270 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1271 	if (!flow) {
1272 		err = -ENOENT;
1273 		goto unlock;
1274 	}
1275 
1276 	reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1277 					OVS_FLOW_CMD_GET, true, ufid_flags);
1278 	if (IS_ERR(reply)) {
1279 		err = PTR_ERR(reply);
1280 		goto unlock;
1281 	}
1282 
1283 	ovs_unlock();
1284 	return genlmsg_reply(reply, info);
1285 unlock:
1286 	ovs_unlock();
1287 	return err;
1288 }
1289 
1290 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1291 {
1292 	struct nlattr **a = info->attrs;
1293 	struct ovs_header *ovs_header = info->userhdr;
1294 	struct net *net = sock_net(skb->sk);
1295 	struct sw_flow_key key;
1296 	struct sk_buff *reply;
1297 	struct sw_flow *flow = NULL;
1298 	struct datapath *dp;
1299 	struct sw_flow_match match;
1300 	struct sw_flow_id ufid;
1301 	u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1302 	int err;
1303 	bool log = !a[OVS_FLOW_ATTR_PROBE];
1304 	bool ufid_present;
1305 
1306 	ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1307 	if (a[OVS_FLOW_ATTR_KEY]) {
1308 		ovs_match_init(&match, &key, true, NULL);
1309 		err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1310 					NULL, log);
1311 		if (unlikely(err))
1312 			return err;
1313 	}
1314 
1315 	ovs_lock();
1316 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1317 	if (unlikely(!dp)) {
1318 		err = -ENODEV;
1319 		goto unlock;
1320 	}
1321 
1322 	if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1323 		err = ovs_flow_tbl_flush(&dp->table);
1324 		goto unlock;
1325 	}
1326 
1327 	if (ufid_present)
1328 		flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1329 	else
1330 		flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1331 	if (unlikely(!flow)) {
1332 		err = -ENOENT;
1333 		goto unlock;
1334 	}
1335 
1336 	ovs_flow_tbl_remove(&dp->table, flow);
1337 	ovs_unlock();
1338 
1339 	reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1340 					&flow->id, info, false, ufid_flags);
1341 	if (likely(reply)) {
1342 		if (!IS_ERR(reply)) {
1343 			rcu_read_lock();	/*To keep RCU checker happy. */
1344 			err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1345 						     reply, info->snd_portid,
1346 						     info->snd_seq, 0,
1347 						     OVS_FLOW_CMD_DEL,
1348 						     ufid_flags);
1349 			rcu_read_unlock();
1350 			BUG_ON(err < 0);
1351 
1352 			ovs_notify(&dp_flow_genl_family, reply, info);
1353 		} else {
1354 			netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1355 		}
1356 	}
1357 
1358 	ovs_flow_free(flow, true);
1359 	return 0;
1360 unlock:
1361 	ovs_unlock();
1362 	return err;
1363 }
1364 
1365 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1366 {
1367 	struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1368 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1369 	struct table_instance *ti;
1370 	struct datapath *dp;
1371 	u32 ufid_flags;
1372 	int err;
1373 
1374 	err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1375 				       OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1376 	if (err)
1377 		return err;
1378 	ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1379 
1380 	rcu_read_lock();
1381 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1382 	if (!dp) {
1383 		rcu_read_unlock();
1384 		return -ENODEV;
1385 	}
1386 
1387 	ti = rcu_dereference(dp->table.ti);
1388 	for (;;) {
1389 		struct sw_flow *flow;
1390 		u32 bucket, obj;
1391 
1392 		bucket = cb->args[0];
1393 		obj = cb->args[1];
1394 		flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1395 		if (!flow)
1396 			break;
1397 
1398 		if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1399 					   NETLINK_CB(cb->skb).portid,
1400 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
1401 					   OVS_FLOW_CMD_GET, ufid_flags) < 0)
1402 			break;
1403 
1404 		cb->args[0] = bucket;
1405 		cb->args[1] = obj;
1406 	}
1407 	rcu_read_unlock();
1408 	return skb->len;
1409 }
1410 
1411 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1412 	[OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1413 	[OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1414 	[OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1415 	[OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1416 	[OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1417 	[OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1418 	[OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1419 };
1420 
1421 static const struct genl_ops dp_flow_genl_ops[] = {
1422 	{ .cmd = OVS_FLOW_CMD_NEW,
1423 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1424 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1425 	  .doit = ovs_flow_cmd_new
1426 	},
1427 	{ .cmd = OVS_FLOW_CMD_DEL,
1428 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1429 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1430 	  .doit = ovs_flow_cmd_del
1431 	},
1432 	{ .cmd = OVS_FLOW_CMD_GET,
1433 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1434 	  .flags = 0,		    /* OK for unprivileged users. */
1435 	  .doit = ovs_flow_cmd_get,
1436 	  .dumpit = ovs_flow_cmd_dump
1437 	},
1438 	{ .cmd = OVS_FLOW_CMD_SET,
1439 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1440 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1441 	  .doit = ovs_flow_cmd_set,
1442 	},
1443 };
1444 
1445 static struct genl_family dp_flow_genl_family __ro_after_init = {
1446 	.hdrsize = sizeof(struct ovs_header),
1447 	.name = OVS_FLOW_FAMILY,
1448 	.version = OVS_FLOW_VERSION,
1449 	.maxattr = OVS_FLOW_ATTR_MAX,
1450 	.policy = flow_policy,
1451 	.netnsok = true,
1452 	.parallel_ops = true,
1453 	.ops = dp_flow_genl_ops,
1454 	.n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1455 	.mcgrps = &ovs_dp_flow_multicast_group,
1456 	.n_mcgrps = 1,
1457 	.module = THIS_MODULE,
1458 };
1459 
1460 static size_t ovs_dp_cmd_msg_size(void)
1461 {
1462 	size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1463 
1464 	msgsize += nla_total_size(IFNAMSIZ);
1465 	msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1466 	msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1467 	msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1468 
1469 	return msgsize;
1470 }
1471 
1472 /* Called with ovs_mutex. */
1473 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1474 				u32 portid, u32 seq, u32 flags, u8 cmd)
1475 {
1476 	struct ovs_header *ovs_header;
1477 	struct ovs_dp_stats dp_stats;
1478 	struct ovs_dp_megaflow_stats dp_megaflow_stats;
1479 	int err;
1480 
1481 	ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1482 				   flags, cmd);
1483 	if (!ovs_header)
1484 		goto error;
1485 
1486 	ovs_header->dp_ifindex = get_dpifindex(dp);
1487 
1488 	err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1489 	if (err)
1490 		goto nla_put_failure;
1491 
1492 	get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1493 	if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1494 			  &dp_stats, OVS_DP_ATTR_PAD))
1495 		goto nla_put_failure;
1496 
1497 	if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1498 			  sizeof(struct ovs_dp_megaflow_stats),
1499 			  &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1500 		goto nla_put_failure;
1501 
1502 	if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1503 		goto nla_put_failure;
1504 
1505 	genlmsg_end(skb, ovs_header);
1506 	return 0;
1507 
1508 nla_put_failure:
1509 	genlmsg_cancel(skb, ovs_header);
1510 error:
1511 	return -EMSGSIZE;
1512 }
1513 
1514 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1515 {
1516 	return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1517 }
1518 
1519 /* Called with rcu_read_lock or ovs_mutex. */
1520 static struct datapath *lookup_datapath(struct net *net,
1521 					const struct ovs_header *ovs_header,
1522 					struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1523 {
1524 	struct datapath *dp;
1525 
1526 	if (!a[OVS_DP_ATTR_NAME])
1527 		dp = get_dp(net, ovs_header->dp_ifindex);
1528 	else {
1529 		struct vport *vport;
1530 
1531 		vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1532 		dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1533 	}
1534 	return dp ? dp : ERR_PTR(-ENODEV);
1535 }
1536 
1537 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1538 {
1539 	struct datapath *dp;
1540 
1541 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1542 	if (IS_ERR(dp))
1543 		return;
1544 
1545 	WARN(dp->user_features, "Dropping previously announced user features\n");
1546 	dp->user_features = 0;
1547 }
1548 
1549 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1550 
1551 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1552 {
1553 	u32 user_features = 0;
1554 
1555 	if (a[OVS_DP_ATTR_USER_FEATURES]) {
1556 		user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1557 
1558 		if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1559 				      OVS_DP_F_UNALIGNED |
1560 				      OVS_DP_F_TC_RECIRC_SHARING))
1561 			return -EOPNOTSUPP;
1562 
1563 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1564 		if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1565 			return -EOPNOTSUPP;
1566 #endif
1567 	}
1568 
1569 	dp->user_features = user_features;
1570 
1571 	if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1572 		static_branch_enable(&tc_recirc_sharing_support);
1573 	else
1574 		static_branch_disable(&tc_recirc_sharing_support);
1575 
1576 	return 0;
1577 }
1578 
1579 static int ovs_dp_stats_init(struct datapath *dp)
1580 {
1581 	dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1582 	if (!dp->stats_percpu)
1583 		return -ENOMEM;
1584 
1585 	return 0;
1586 }
1587 
1588 static int ovs_dp_vport_init(struct datapath *dp)
1589 {
1590 	int i;
1591 
1592 	dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1593 				  sizeof(struct hlist_head),
1594 				  GFP_KERNEL);
1595 	if (!dp->ports)
1596 		return -ENOMEM;
1597 
1598 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1599 		INIT_HLIST_HEAD(&dp->ports[i]);
1600 
1601 	return 0;
1602 }
1603 
1604 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1605 {
1606 	struct nlattr **a = info->attrs;
1607 	struct vport_parms parms;
1608 	struct sk_buff *reply;
1609 	struct datapath *dp;
1610 	struct vport *vport;
1611 	struct ovs_net *ovs_net;
1612 	int err;
1613 
1614 	err = -EINVAL;
1615 	if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1616 		goto err;
1617 
1618 	reply = ovs_dp_cmd_alloc_info();
1619 	if (!reply)
1620 		return -ENOMEM;
1621 
1622 	err = -ENOMEM;
1623 	dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1624 	if (dp == NULL)
1625 		goto err_destroy_reply;
1626 
1627 	ovs_dp_set_net(dp, sock_net(skb->sk));
1628 
1629 	/* Allocate table. */
1630 	err = ovs_flow_tbl_init(&dp->table);
1631 	if (err)
1632 		goto err_destroy_dp;
1633 
1634 	err = ovs_dp_stats_init(dp);
1635 	if (err)
1636 		goto err_destroy_table;
1637 
1638 	err = ovs_dp_vport_init(dp);
1639 	if (err)
1640 		goto err_destroy_stats;
1641 
1642 	err = ovs_meters_init(dp);
1643 	if (err)
1644 		goto err_destroy_ports;
1645 
1646 	/* Set up our datapath device. */
1647 	parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1648 	parms.type = OVS_VPORT_TYPE_INTERNAL;
1649 	parms.options = NULL;
1650 	parms.dp = dp;
1651 	parms.port_no = OVSP_LOCAL;
1652 	parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1653 
1654 	err = ovs_dp_change(dp, a);
1655 	if (err)
1656 		goto err_destroy_meters;
1657 
1658 	/* So far only local changes have been made, now need the lock. */
1659 	ovs_lock();
1660 
1661 	vport = new_vport(&parms);
1662 	if (IS_ERR(vport)) {
1663 		err = PTR_ERR(vport);
1664 		if (err == -EBUSY)
1665 			err = -EEXIST;
1666 
1667 		if (err == -EEXIST) {
1668 			/* An outdated user space instance that does not understand
1669 			 * the concept of user_features has attempted to create a new
1670 			 * datapath and is likely to reuse it. Drop all user features.
1671 			 */
1672 			if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1673 				ovs_dp_reset_user_features(skb, info);
1674 		}
1675 
1676 		ovs_unlock();
1677 		goto err_destroy_meters;
1678 	}
1679 
1680 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1681 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1682 	BUG_ON(err < 0);
1683 
1684 	ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1685 	list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1686 
1687 	ovs_unlock();
1688 
1689 	ovs_notify(&dp_datapath_genl_family, reply, info);
1690 	return 0;
1691 
1692 err_destroy_meters:
1693 	ovs_meters_exit(dp);
1694 err_destroy_ports:
1695 	kfree(dp->ports);
1696 err_destroy_stats:
1697 	free_percpu(dp->stats_percpu);
1698 err_destroy_table:
1699 	ovs_flow_tbl_destroy(&dp->table);
1700 err_destroy_dp:
1701 	kfree(dp);
1702 err_destroy_reply:
1703 	kfree_skb(reply);
1704 err:
1705 	return err;
1706 }
1707 
1708 /* Called with ovs_mutex. */
1709 static void __dp_destroy(struct datapath *dp)
1710 {
1711 	int i;
1712 
1713 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1714 		struct vport *vport;
1715 		struct hlist_node *n;
1716 
1717 		hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1718 			if (vport->port_no != OVSP_LOCAL)
1719 				ovs_dp_detach_port(vport);
1720 	}
1721 
1722 	list_del_rcu(&dp->list_node);
1723 
1724 	/* OVSP_LOCAL is datapath internal port. We need to make sure that
1725 	 * all ports in datapath are destroyed first before freeing datapath.
1726 	 */
1727 	ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1728 
1729 	/* RCU destroy the flow table */
1730 	call_rcu(&dp->rcu, destroy_dp_rcu);
1731 }
1732 
1733 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1734 {
1735 	struct sk_buff *reply;
1736 	struct datapath *dp;
1737 	int err;
1738 
1739 	reply = ovs_dp_cmd_alloc_info();
1740 	if (!reply)
1741 		return -ENOMEM;
1742 
1743 	ovs_lock();
1744 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1745 	err = PTR_ERR(dp);
1746 	if (IS_ERR(dp))
1747 		goto err_unlock_free;
1748 
1749 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1750 				   info->snd_seq, 0, OVS_DP_CMD_DEL);
1751 	BUG_ON(err < 0);
1752 
1753 	__dp_destroy(dp);
1754 	ovs_unlock();
1755 
1756 	ovs_notify(&dp_datapath_genl_family, reply, info);
1757 
1758 	return 0;
1759 
1760 err_unlock_free:
1761 	ovs_unlock();
1762 	kfree_skb(reply);
1763 	return err;
1764 }
1765 
1766 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1767 {
1768 	struct sk_buff *reply;
1769 	struct datapath *dp;
1770 	int err;
1771 
1772 	reply = ovs_dp_cmd_alloc_info();
1773 	if (!reply)
1774 		return -ENOMEM;
1775 
1776 	ovs_lock();
1777 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1778 	err = PTR_ERR(dp);
1779 	if (IS_ERR(dp))
1780 		goto err_unlock_free;
1781 
1782 	err = ovs_dp_change(dp, info->attrs);
1783 	if (err)
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_SET);
1788 	BUG_ON(err < 0);
1789 
1790 	ovs_unlock();
1791 	ovs_notify(&dp_datapath_genl_family, reply, info);
1792 
1793 	return 0;
1794 
1795 err_unlock_free:
1796 	ovs_unlock();
1797 	kfree_skb(reply);
1798 	return err;
1799 }
1800 
1801 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1802 {
1803 	struct sk_buff *reply;
1804 	struct datapath *dp;
1805 	int err;
1806 
1807 	reply = ovs_dp_cmd_alloc_info();
1808 	if (!reply)
1809 		return -ENOMEM;
1810 
1811 	ovs_lock();
1812 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1813 	if (IS_ERR(dp)) {
1814 		err = PTR_ERR(dp);
1815 		goto err_unlock_free;
1816 	}
1817 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1818 				   info->snd_seq, 0, OVS_DP_CMD_GET);
1819 	BUG_ON(err < 0);
1820 	ovs_unlock();
1821 
1822 	return genlmsg_reply(reply, info);
1823 
1824 err_unlock_free:
1825 	ovs_unlock();
1826 	kfree_skb(reply);
1827 	return err;
1828 }
1829 
1830 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1831 {
1832 	struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1833 	struct datapath *dp;
1834 	int skip = cb->args[0];
1835 	int i = 0;
1836 
1837 	ovs_lock();
1838 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
1839 		if (i >= skip &&
1840 		    ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1841 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1842 					 OVS_DP_CMD_GET) < 0)
1843 			break;
1844 		i++;
1845 	}
1846 	ovs_unlock();
1847 
1848 	cb->args[0] = i;
1849 
1850 	return skb->len;
1851 }
1852 
1853 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1854 	[OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1855 	[OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1856 	[OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1857 };
1858 
1859 static const struct genl_ops dp_datapath_genl_ops[] = {
1860 	{ .cmd = OVS_DP_CMD_NEW,
1861 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1862 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1863 	  .doit = ovs_dp_cmd_new
1864 	},
1865 	{ .cmd = OVS_DP_CMD_DEL,
1866 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1867 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1868 	  .doit = ovs_dp_cmd_del
1869 	},
1870 	{ .cmd = OVS_DP_CMD_GET,
1871 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1872 	  .flags = 0,		    /* OK for unprivileged users. */
1873 	  .doit = ovs_dp_cmd_get,
1874 	  .dumpit = ovs_dp_cmd_dump
1875 	},
1876 	{ .cmd = OVS_DP_CMD_SET,
1877 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1878 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1879 	  .doit = ovs_dp_cmd_set,
1880 	},
1881 };
1882 
1883 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1884 	.hdrsize = sizeof(struct ovs_header),
1885 	.name = OVS_DATAPATH_FAMILY,
1886 	.version = OVS_DATAPATH_VERSION,
1887 	.maxattr = OVS_DP_ATTR_MAX,
1888 	.policy = datapath_policy,
1889 	.netnsok = true,
1890 	.parallel_ops = true,
1891 	.ops = dp_datapath_genl_ops,
1892 	.n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1893 	.mcgrps = &ovs_dp_datapath_multicast_group,
1894 	.n_mcgrps = 1,
1895 	.module = THIS_MODULE,
1896 };
1897 
1898 /* Called with ovs_mutex or RCU read lock. */
1899 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1900 				   struct net *net, u32 portid, u32 seq,
1901 				   u32 flags, u8 cmd, gfp_t gfp)
1902 {
1903 	struct ovs_header *ovs_header;
1904 	struct ovs_vport_stats vport_stats;
1905 	int err;
1906 
1907 	ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1908 				 flags, cmd);
1909 	if (!ovs_header)
1910 		return -EMSGSIZE;
1911 
1912 	ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1913 
1914 	if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1915 	    nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1916 	    nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1917 			   ovs_vport_name(vport)) ||
1918 	    nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1919 		goto nla_put_failure;
1920 
1921 	if (!net_eq(net, dev_net(vport->dev))) {
1922 		int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
1923 
1924 		if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1925 			goto nla_put_failure;
1926 	}
1927 
1928 	ovs_vport_get_stats(vport, &vport_stats);
1929 	if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1930 			  sizeof(struct ovs_vport_stats), &vport_stats,
1931 			  OVS_VPORT_ATTR_PAD))
1932 		goto nla_put_failure;
1933 
1934 	if (ovs_vport_get_upcall_portids(vport, skb))
1935 		goto nla_put_failure;
1936 
1937 	err = ovs_vport_get_options(vport, skb);
1938 	if (err == -EMSGSIZE)
1939 		goto error;
1940 
1941 	genlmsg_end(skb, ovs_header);
1942 	return 0;
1943 
1944 nla_put_failure:
1945 	err = -EMSGSIZE;
1946 error:
1947 	genlmsg_cancel(skb, ovs_header);
1948 	return err;
1949 }
1950 
1951 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1952 {
1953 	return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1954 }
1955 
1956 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1957 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1958 					 u32 portid, u32 seq, u8 cmd)
1959 {
1960 	struct sk_buff *skb;
1961 	int retval;
1962 
1963 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1964 	if (!skb)
1965 		return ERR_PTR(-ENOMEM);
1966 
1967 	retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
1968 					 GFP_KERNEL);
1969 	BUG_ON(retval < 0);
1970 
1971 	return skb;
1972 }
1973 
1974 /* Called with ovs_mutex or RCU read lock. */
1975 static struct vport *lookup_vport(struct net *net,
1976 				  const struct ovs_header *ovs_header,
1977 				  struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1978 {
1979 	struct datapath *dp;
1980 	struct vport *vport;
1981 
1982 	if (a[OVS_VPORT_ATTR_IFINDEX])
1983 		return ERR_PTR(-EOPNOTSUPP);
1984 	if (a[OVS_VPORT_ATTR_NAME]) {
1985 		vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1986 		if (!vport)
1987 			return ERR_PTR(-ENODEV);
1988 		if (ovs_header->dp_ifindex &&
1989 		    ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1990 			return ERR_PTR(-ENODEV);
1991 		return vport;
1992 	} else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1993 		u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1994 
1995 		if (port_no >= DP_MAX_PORTS)
1996 			return ERR_PTR(-EFBIG);
1997 
1998 		dp = get_dp(net, ovs_header->dp_ifindex);
1999 		if (!dp)
2000 			return ERR_PTR(-ENODEV);
2001 
2002 		vport = ovs_vport_ovsl_rcu(dp, port_no);
2003 		if (!vport)
2004 			return ERR_PTR(-ENODEV);
2005 		return vport;
2006 	} else
2007 		return ERR_PTR(-EINVAL);
2008 
2009 }
2010 
2011 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2012 {
2013 	unsigned int dev_headroom, max_headroom = 0;
2014 	struct net_device *dev;
2015 	struct vport *vport;
2016 	int i;
2017 
2018 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2019 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2020 			dev = vport->dev;
2021 			dev_headroom = netdev_get_fwd_headroom(dev);
2022 			if (dev_headroom > max_headroom)
2023 				max_headroom = dev_headroom;
2024 		}
2025 	}
2026 
2027 	return max_headroom;
2028 }
2029 
2030 /* Called with ovs_mutex */
2031 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2032 {
2033 	struct vport *vport;
2034 	int i;
2035 
2036 	dp->max_headroom = new_headroom;
2037 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
2038 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
2039 			netdev_set_rx_headroom(vport->dev, new_headroom);
2040 }
2041 
2042 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2043 {
2044 	struct nlattr **a = info->attrs;
2045 	struct ovs_header *ovs_header = info->userhdr;
2046 	struct vport_parms parms;
2047 	struct sk_buff *reply;
2048 	struct vport *vport;
2049 	struct datapath *dp;
2050 	unsigned int new_headroom;
2051 	u32 port_no;
2052 	int err;
2053 
2054 	if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2055 	    !a[OVS_VPORT_ATTR_UPCALL_PID])
2056 		return -EINVAL;
2057 	if (a[OVS_VPORT_ATTR_IFINDEX])
2058 		return -EOPNOTSUPP;
2059 
2060 	port_no = a[OVS_VPORT_ATTR_PORT_NO]
2061 		? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2062 	if (port_no >= DP_MAX_PORTS)
2063 		return -EFBIG;
2064 
2065 	reply = ovs_vport_cmd_alloc_info();
2066 	if (!reply)
2067 		return -ENOMEM;
2068 
2069 	ovs_lock();
2070 restart:
2071 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2072 	err = -ENODEV;
2073 	if (!dp)
2074 		goto exit_unlock_free;
2075 
2076 	if (port_no) {
2077 		vport = ovs_vport_ovsl(dp, port_no);
2078 		err = -EBUSY;
2079 		if (vport)
2080 			goto exit_unlock_free;
2081 	} else {
2082 		for (port_no = 1; ; port_no++) {
2083 			if (port_no >= DP_MAX_PORTS) {
2084 				err = -EFBIG;
2085 				goto exit_unlock_free;
2086 			}
2087 			vport = ovs_vport_ovsl(dp, port_no);
2088 			if (!vport)
2089 				break;
2090 		}
2091 	}
2092 
2093 	parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2094 	parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2095 	parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2096 	parms.dp = dp;
2097 	parms.port_no = port_no;
2098 	parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2099 
2100 	vport = new_vport(&parms);
2101 	err = PTR_ERR(vport);
2102 	if (IS_ERR(vport)) {
2103 		if (err == -EAGAIN)
2104 			goto restart;
2105 		goto exit_unlock_free;
2106 	}
2107 
2108 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2109 				      info->snd_portid, info->snd_seq, 0,
2110 				      OVS_VPORT_CMD_NEW, GFP_KERNEL);
2111 
2112 	new_headroom = netdev_get_fwd_headroom(vport->dev);
2113 
2114 	if (new_headroom > dp->max_headroom)
2115 		ovs_update_headroom(dp, new_headroom);
2116 	else
2117 		netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2118 
2119 	BUG_ON(err < 0);
2120 	ovs_unlock();
2121 
2122 	ovs_notify(&dp_vport_genl_family, reply, info);
2123 	return 0;
2124 
2125 exit_unlock_free:
2126 	ovs_unlock();
2127 	kfree_skb(reply);
2128 	return err;
2129 }
2130 
2131 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2132 {
2133 	struct nlattr **a = info->attrs;
2134 	struct sk_buff *reply;
2135 	struct vport *vport;
2136 	int err;
2137 
2138 	reply = ovs_vport_cmd_alloc_info();
2139 	if (!reply)
2140 		return -ENOMEM;
2141 
2142 	ovs_lock();
2143 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2144 	err = PTR_ERR(vport);
2145 	if (IS_ERR(vport))
2146 		goto exit_unlock_free;
2147 
2148 	if (a[OVS_VPORT_ATTR_TYPE] &&
2149 	    nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2150 		err = -EINVAL;
2151 		goto exit_unlock_free;
2152 	}
2153 
2154 	if (a[OVS_VPORT_ATTR_OPTIONS]) {
2155 		err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2156 		if (err)
2157 			goto exit_unlock_free;
2158 	}
2159 
2160 
2161 	if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2162 		struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2163 
2164 		err = ovs_vport_set_upcall_portids(vport, ids);
2165 		if (err)
2166 			goto exit_unlock_free;
2167 	}
2168 
2169 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2170 				      info->snd_portid, info->snd_seq, 0,
2171 				      OVS_VPORT_CMD_SET, GFP_KERNEL);
2172 	BUG_ON(err < 0);
2173 
2174 	ovs_unlock();
2175 	ovs_notify(&dp_vport_genl_family, reply, info);
2176 	return 0;
2177 
2178 exit_unlock_free:
2179 	ovs_unlock();
2180 	kfree_skb(reply);
2181 	return err;
2182 }
2183 
2184 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2185 {
2186 	bool update_headroom = false;
2187 	struct nlattr **a = info->attrs;
2188 	struct sk_buff *reply;
2189 	struct datapath *dp;
2190 	struct vport *vport;
2191 	unsigned int new_headroom;
2192 	int err;
2193 
2194 	reply = ovs_vport_cmd_alloc_info();
2195 	if (!reply)
2196 		return -ENOMEM;
2197 
2198 	ovs_lock();
2199 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2200 	err = PTR_ERR(vport);
2201 	if (IS_ERR(vport))
2202 		goto exit_unlock_free;
2203 
2204 	if (vport->port_no == OVSP_LOCAL) {
2205 		err = -EINVAL;
2206 		goto exit_unlock_free;
2207 	}
2208 
2209 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2210 				      info->snd_portid, info->snd_seq, 0,
2211 				      OVS_VPORT_CMD_DEL, GFP_KERNEL);
2212 	BUG_ON(err < 0);
2213 
2214 	/* the vport deletion may trigger dp headroom update */
2215 	dp = vport->dp;
2216 	if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2217 		update_headroom = true;
2218 
2219 	netdev_reset_rx_headroom(vport->dev);
2220 	ovs_dp_detach_port(vport);
2221 
2222 	if (update_headroom) {
2223 		new_headroom = ovs_get_max_headroom(dp);
2224 
2225 		if (new_headroom < dp->max_headroom)
2226 			ovs_update_headroom(dp, new_headroom);
2227 	}
2228 	ovs_unlock();
2229 
2230 	ovs_notify(&dp_vport_genl_family, reply, info);
2231 	return 0;
2232 
2233 exit_unlock_free:
2234 	ovs_unlock();
2235 	kfree_skb(reply);
2236 	return err;
2237 }
2238 
2239 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2240 {
2241 	struct nlattr **a = info->attrs;
2242 	struct ovs_header *ovs_header = info->userhdr;
2243 	struct sk_buff *reply;
2244 	struct vport *vport;
2245 	int err;
2246 
2247 	reply = ovs_vport_cmd_alloc_info();
2248 	if (!reply)
2249 		return -ENOMEM;
2250 
2251 	rcu_read_lock();
2252 	vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2253 	err = PTR_ERR(vport);
2254 	if (IS_ERR(vport))
2255 		goto exit_unlock_free;
2256 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2257 				      info->snd_portid, info->snd_seq, 0,
2258 				      OVS_VPORT_CMD_GET, GFP_ATOMIC);
2259 	BUG_ON(err < 0);
2260 	rcu_read_unlock();
2261 
2262 	return genlmsg_reply(reply, info);
2263 
2264 exit_unlock_free:
2265 	rcu_read_unlock();
2266 	kfree_skb(reply);
2267 	return err;
2268 }
2269 
2270 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2271 {
2272 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2273 	struct datapath *dp;
2274 	int bucket = cb->args[0], skip = cb->args[1];
2275 	int i, j = 0;
2276 
2277 	rcu_read_lock();
2278 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2279 	if (!dp) {
2280 		rcu_read_unlock();
2281 		return -ENODEV;
2282 	}
2283 	for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2284 		struct vport *vport;
2285 
2286 		j = 0;
2287 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2288 			if (j >= skip &&
2289 			    ovs_vport_cmd_fill_info(vport, skb,
2290 						    sock_net(skb->sk),
2291 						    NETLINK_CB(cb->skb).portid,
2292 						    cb->nlh->nlmsg_seq,
2293 						    NLM_F_MULTI,
2294 						    OVS_VPORT_CMD_GET,
2295 						    GFP_ATOMIC) < 0)
2296 				goto out;
2297 
2298 			j++;
2299 		}
2300 		skip = 0;
2301 	}
2302 out:
2303 	rcu_read_unlock();
2304 
2305 	cb->args[0] = i;
2306 	cb->args[1] = j;
2307 
2308 	return skb->len;
2309 }
2310 
2311 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2312 	[OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2313 	[OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2314 	[OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2315 	[OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2316 	[OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2317 	[OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2318 	[OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2319 	[OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2320 };
2321 
2322 static const struct genl_ops dp_vport_genl_ops[] = {
2323 	{ .cmd = OVS_VPORT_CMD_NEW,
2324 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2325 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2326 	  .doit = ovs_vport_cmd_new
2327 	},
2328 	{ .cmd = OVS_VPORT_CMD_DEL,
2329 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2330 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2331 	  .doit = ovs_vport_cmd_del
2332 	},
2333 	{ .cmd = OVS_VPORT_CMD_GET,
2334 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2335 	  .flags = 0,		    /* OK for unprivileged users. */
2336 	  .doit = ovs_vport_cmd_get,
2337 	  .dumpit = ovs_vport_cmd_dump
2338 	},
2339 	{ .cmd = OVS_VPORT_CMD_SET,
2340 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2341 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2342 	  .doit = ovs_vport_cmd_set,
2343 	},
2344 };
2345 
2346 struct genl_family dp_vport_genl_family __ro_after_init = {
2347 	.hdrsize = sizeof(struct ovs_header),
2348 	.name = OVS_VPORT_FAMILY,
2349 	.version = OVS_VPORT_VERSION,
2350 	.maxattr = OVS_VPORT_ATTR_MAX,
2351 	.policy = vport_policy,
2352 	.netnsok = true,
2353 	.parallel_ops = true,
2354 	.ops = dp_vport_genl_ops,
2355 	.n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2356 	.mcgrps = &ovs_dp_vport_multicast_group,
2357 	.n_mcgrps = 1,
2358 	.module = THIS_MODULE,
2359 };
2360 
2361 static struct genl_family * const dp_genl_families[] = {
2362 	&dp_datapath_genl_family,
2363 	&dp_vport_genl_family,
2364 	&dp_flow_genl_family,
2365 	&dp_packet_genl_family,
2366 	&dp_meter_genl_family,
2367 #if	IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2368 	&dp_ct_limit_genl_family,
2369 #endif
2370 };
2371 
2372 static void dp_unregister_genl(int n_families)
2373 {
2374 	int i;
2375 
2376 	for (i = 0; i < n_families; i++)
2377 		genl_unregister_family(dp_genl_families[i]);
2378 }
2379 
2380 static int __init dp_register_genl(void)
2381 {
2382 	int err;
2383 	int i;
2384 
2385 	for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2386 
2387 		err = genl_register_family(dp_genl_families[i]);
2388 		if (err)
2389 			goto error;
2390 	}
2391 
2392 	return 0;
2393 
2394 error:
2395 	dp_unregister_genl(i);
2396 	return err;
2397 }
2398 
2399 static int __net_init ovs_init_net(struct net *net)
2400 {
2401 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2402 
2403 	INIT_LIST_HEAD(&ovs_net->dps);
2404 	INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2405 	return ovs_ct_init(net);
2406 }
2407 
2408 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2409 					    struct list_head *head)
2410 {
2411 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2412 	struct datapath *dp;
2413 
2414 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
2415 		int i;
2416 
2417 		for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2418 			struct vport *vport;
2419 
2420 			hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2421 				if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2422 					continue;
2423 
2424 				if (dev_net(vport->dev) == dnet)
2425 					list_add(&vport->detach_list, head);
2426 			}
2427 		}
2428 	}
2429 }
2430 
2431 static void __net_exit ovs_exit_net(struct net *dnet)
2432 {
2433 	struct datapath *dp, *dp_next;
2434 	struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2435 	struct vport *vport, *vport_next;
2436 	struct net *net;
2437 	LIST_HEAD(head);
2438 
2439 	ovs_ct_exit(dnet);
2440 	ovs_lock();
2441 	list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2442 		__dp_destroy(dp);
2443 
2444 	down_read(&net_rwsem);
2445 	for_each_net(net)
2446 		list_vports_from_net(net, dnet, &head);
2447 	up_read(&net_rwsem);
2448 
2449 	/* Detach all vports from given namespace. */
2450 	list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2451 		list_del(&vport->detach_list);
2452 		ovs_dp_detach_port(vport);
2453 	}
2454 
2455 	ovs_unlock();
2456 
2457 	cancel_work_sync(&ovs_net->dp_notify_work);
2458 }
2459 
2460 static struct pernet_operations ovs_net_ops = {
2461 	.init = ovs_init_net,
2462 	.exit = ovs_exit_net,
2463 	.id   = &ovs_net_id,
2464 	.size = sizeof(struct ovs_net),
2465 };
2466 
2467 static int __init dp_init(void)
2468 {
2469 	int err;
2470 
2471 	BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2472 
2473 	pr_info("Open vSwitch switching datapath\n");
2474 
2475 	err = action_fifos_init();
2476 	if (err)
2477 		goto error;
2478 
2479 	err = ovs_internal_dev_rtnl_link_register();
2480 	if (err)
2481 		goto error_action_fifos_exit;
2482 
2483 	err = ovs_flow_init();
2484 	if (err)
2485 		goto error_unreg_rtnl_link;
2486 
2487 	err = ovs_vport_init();
2488 	if (err)
2489 		goto error_flow_exit;
2490 
2491 	err = register_pernet_device(&ovs_net_ops);
2492 	if (err)
2493 		goto error_vport_exit;
2494 
2495 	err = register_netdevice_notifier(&ovs_dp_device_notifier);
2496 	if (err)
2497 		goto error_netns_exit;
2498 
2499 	err = ovs_netdev_init();
2500 	if (err)
2501 		goto error_unreg_notifier;
2502 
2503 	err = dp_register_genl();
2504 	if (err < 0)
2505 		goto error_unreg_netdev;
2506 
2507 	return 0;
2508 
2509 error_unreg_netdev:
2510 	ovs_netdev_exit();
2511 error_unreg_notifier:
2512 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2513 error_netns_exit:
2514 	unregister_pernet_device(&ovs_net_ops);
2515 error_vport_exit:
2516 	ovs_vport_exit();
2517 error_flow_exit:
2518 	ovs_flow_exit();
2519 error_unreg_rtnl_link:
2520 	ovs_internal_dev_rtnl_link_unregister();
2521 error_action_fifos_exit:
2522 	action_fifos_exit();
2523 error:
2524 	return err;
2525 }
2526 
2527 static void dp_cleanup(void)
2528 {
2529 	dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2530 	ovs_netdev_exit();
2531 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2532 	unregister_pernet_device(&ovs_net_ops);
2533 	rcu_barrier();
2534 	ovs_vport_exit();
2535 	ovs_flow_exit();
2536 	ovs_internal_dev_rtnl_link_unregister();
2537 	action_fifos_exit();
2538 }
2539 
2540 module_init(dp_init);
2541 module_exit(dp_cleanup);
2542 
2543 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2544 MODULE_LICENSE("GPL");
2545 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2546 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2547 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2548 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2549 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2550 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);
2551