xref: /openbmc/linux/net/openvswitch/datapath.c (revision a31edb20)
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 
1659 	/* Allocate table. */
1660 	err = ovs_flow_tbl_init(&dp->table);
1661 	if (err)
1662 		goto err_destroy_dp;
1663 
1664 	err = ovs_dp_stats_init(dp);
1665 	if (err)
1666 		goto err_destroy_table;
1667 
1668 	err = ovs_dp_vport_init(dp);
1669 	if (err)
1670 		goto err_destroy_stats;
1671 
1672 	err = ovs_meters_init(dp);
1673 	if (err)
1674 		goto err_destroy_ports;
1675 
1676 	/* Set up our datapath device. */
1677 	parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1678 	parms.type = OVS_VPORT_TYPE_INTERNAL;
1679 	parms.options = NULL;
1680 	parms.dp = dp;
1681 	parms.port_no = OVSP_LOCAL;
1682 	parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1683 
1684 	err = ovs_dp_change(dp, a);
1685 	if (err)
1686 		goto err_destroy_meters;
1687 
1688 	/* So far only local changes have been made, now need the lock. */
1689 	ovs_lock();
1690 
1691 	vport = new_vport(&parms);
1692 	if (IS_ERR(vport)) {
1693 		err = PTR_ERR(vport);
1694 		if (err == -EBUSY)
1695 			err = -EEXIST;
1696 
1697 		if (err == -EEXIST) {
1698 			/* An outdated user space instance that does not understand
1699 			 * the concept of user_features has attempted to create a new
1700 			 * datapath and is likely to reuse it. Drop all user features.
1701 			 */
1702 			if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1703 				ovs_dp_reset_user_features(skb, info);
1704 		}
1705 
1706 		ovs_unlock();
1707 		goto err_destroy_meters;
1708 	}
1709 
1710 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1711 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1712 	BUG_ON(err < 0);
1713 
1714 	ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1715 	list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1716 
1717 	ovs_unlock();
1718 
1719 	ovs_notify(&dp_datapath_genl_family, reply, info);
1720 	return 0;
1721 
1722 err_destroy_meters:
1723 	ovs_meters_exit(dp);
1724 err_destroy_ports:
1725 	kfree(dp->ports);
1726 err_destroy_stats:
1727 	free_percpu(dp->stats_percpu);
1728 err_destroy_table:
1729 	ovs_flow_tbl_destroy(&dp->table);
1730 err_destroy_dp:
1731 	kfree(dp);
1732 err_destroy_reply:
1733 	kfree_skb(reply);
1734 err:
1735 	return err;
1736 }
1737 
1738 /* Called with ovs_mutex. */
1739 static void __dp_destroy(struct datapath *dp)
1740 {
1741 	int i;
1742 
1743 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1744 		struct vport *vport;
1745 		struct hlist_node *n;
1746 
1747 		hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1748 			if (vport->port_no != OVSP_LOCAL)
1749 				ovs_dp_detach_port(vport);
1750 	}
1751 
1752 	list_del_rcu(&dp->list_node);
1753 
1754 	/* OVSP_LOCAL is datapath internal port. We need to make sure that
1755 	 * all ports in datapath are destroyed first before freeing datapath.
1756 	 */
1757 	ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1758 
1759 	/* RCU destroy the flow table */
1760 	call_rcu(&dp->rcu, destroy_dp_rcu);
1761 }
1762 
1763 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1764 {
1765 	struct sk_buff *reply;
1766 	struct datapath *dp;
1767 	int err;
1768 
1769 	reply = ovs_dp_cmd_alloc_info();
1770 	if (!reply)
1771 		return -ENOMEM;
1772 
1773 	ovs_lock();
1774 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1775 	err = PTR_ERR(dp);
1776 	if (IS_ERR(dp))
1777 		goto err_unlock_free;
1778 
1779 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1780 				   info->snd_seq, 0, OVS_DP_CMD_DEL);
1781 	BUG_ON(err < 0);
1782 
1783 	__dp_destroy(dp);
1784 	ovs_unlock();
1785 
1786 	ovs_notify(&dp_datapath_genl_family, reply, info);
1787 
1788 	return 0;
1789 
1790 err_unlock_free:
1791 	ovs_unlock();
1792 	kfree_skb(reply);
1793 	return err;
1794 }
1795 
1796 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1797 {
1798 	struct sk_buff *reply;
1799 	struct datapath *dp;
1800 	int err;
1801 
1802 	reply = ovs_dp_cmd_alloc_info();
1803 	if (!reply)
1804 		return -ENOMEM;
1805 
1806 	ovs_lock();
1807 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1808 	err = PTR_ERR(dp);
1809 	if (IS_ERR(dp))
1810 		goto err_unlock_free;
1811 
1812 	err = ovs_dp_change(dp, info->attrs);
1813 	if (err)
1814 		goto err_unlock_free;
1815 
1816 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1817 				   info->snd_seq, 0, OVS_DP_CMD_SET);
1818 	BUG_ON(err < 0);
1819 
1820 	ovs_unlock();
1821 	ovs_notify(&dp_datapath_genl_family, reply, info);
1822 
1823 	return 0;
1824 
1825 err_unlock_free:
1826 	ovs_unlock();
1827 	kfree_skb(reply);
1828 	return err;
1829 }
1830 
1831 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1832 {
1833 	struct sk_buff *reply;
1834 	struct datapath *dp;
1835 	int err;
1836 
1837 	reply = ovs_dp_cmd_alloc_info();
1838 	if (!reply)
1839 		return -ENOMEM;
1840 
1841 	ovs_lock();
1842 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1843 	if (IS_ERR(dp)) {
1844 		err = PTR_ERR(dp);
1845 		goto err_unlock_free;
1846 	}
1847 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1848 				   info->snd_seq, 0, OVS_DP_CMD_GET);
1849 	BUG_ON(err < 0);
1850 	ovs_unlock();
1851 
1852 	return genlmsg_reply(reply, info);
1853 
1854 err_unlock_free:
1855 	ovs_unlock();
1856 	kfree_skb(reply);
1857 	return err;
1858 }
1859 
1860 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1861 {
1862 	struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1863 	struct datapath *dp;
1864 	int skip = cb->args[0];
1865 	int i = 0;
1866 
1867 	ovs_lock();
1868 	list_for_each_entry(dp, &ovs_net->dps, list_node) {
1869 		if (i >= skip &&
1870 		    ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1871 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1872 					 OVS_DP_CMD_GET) < 0)
1873 			break;
1874 		i++;
1875 	}
1876 	ovs_unlock();
1877 
1878 	cb->args[0] = i;
1879 
1880 	return skb->len;
1881 }
1882 
1883 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1884 	[OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1885 	[OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1886 	[OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1887 };
1888 
1889 static const struct genl_ops dp_datapath_genl_ops[] = {
1890 	{ .cmd = OVS_DP_CMD_NEW,
1891 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1892 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1893 	  .doit = ovs_dp_cmd_new
1894 	},
1895 	{ .cmd = OVS_DP_CMD_DEL,
1896 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1897 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1898 	  .doit = ovs_dp_cmd_del
1899 	},
1900 	{ .cmd = OVS_DP_CMD_GET,
1901 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1902 	  .flags = 0,		    /* OK for unprivileged users. */
1903 	  .doit = ovs_dp_cmd_get,
1904 	  .dumpit = ovs_dp_cmd_dump
1905 	},
1906 	{ .cmd = OVS_DP_CMD_SET,
1907 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1908 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1909 	  .doit = ovs_dp_cmd_set,
1910 	},
1911 };
1912 
1913 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1914 	.hdrsize = sizeof(struct ovs_header),
1915 	.name = OVS_DATAPATH_FAMILY,
1916 	.version = OVS_DATAPATH_VERSION,
1917 	.maxattr = OVS_DP_ATTR_MAX,
1918 	.policy = datapath_policy,
1919 	.netnsok = true,
1920 	.parallel_ops = true,
1921 	.ops = dp_datapath_genl_ops,
1922 	.n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1923 	.mcgrps = &ovs_dp_datapath_multicast_group,
1924 	.n_mcgrps = 1,
1925 	.module = THIS_MODULE,
1926 };
1927 
1928 /* Called with ovs_mutex or RCU read lock. */
1929 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1930 				   struct net *net, u32 portid, u32 seq,
1931 				   u32 flags, u8 cmd, gfp_t gfp)
1932 {
1933 	struct ovs_header *ovs_header;
1934 	struct ovs_vport_stats vport_stats;
1935 	int err;
1936 
1937 	ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1938 				 flags, cmd);
1939 	if (!ovs_header)
1940 		return -EMSGSIZE;
1941 
1942 	ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1943 
1944 	if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1945 	    nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1946 	    nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1947 			   ovs_vport_name(vport)) ||
1948 	    nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1949 		goto nla_put_failure;
1950 
1951 	if (!net_eq(net, dev_net(vport->dev))) {
1952 		int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
1953 
1954 		if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1955 			goto nla_put_failure;
1956 	}
1957 
1958 	ovs_vport_get_stats(vport, &vport_stats);
1959 	if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1960 			  sizeof(struct ovs_vport_stats), &vport_stats,
1961 			  OVS_VPORT_ATTR_PAD))
1962 		goto nla_put_failure;
1963 
1964 	if (ovs_vport_get_upcall_portids(vport, skb))
1965 		goto nla_put_failure;
1966 
1967 	err = ovs_vport_get_options(vport, skb);
1968 	if (err == -EMSGSIZE)
1969 		goto error;
1970 
1971 	genlmsg_end(skb, ovs_header);
1972 	return 0;
1973 
1974 nla_put_failure:
1975 	err = -EMSGSIZE;
1976 error:
1977 	genlmsg_cancel(skb, ovs_header);
1978 	return err;
1979 }
1980 
1981 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1982 {
1983 	return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1984 }
1985 
1986 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1987 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1988 					 u32 portid, u32 seq, u8 cmd)
1989 {
1990 	struct sk_buff *skb;
1991 	int retval;
1992 
1993 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1994 	if (!skb)
1995 		return ERR_PTR(-ENOMEM);
1996 
1997 	retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
1998 					 GFP_KERNEL);
1999 	BUG_ON(retval < 0);
2000 
2001 	return skb;
2002 }
2003 
2004 /* Called with ovs_mutex or RCU read lock. */
2005 static struct vport *lookup_vport(struct net *net,
2006 				  const struct ovs_header *ovs_header,
2007 				  struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2008 {
2009 	struct datapath *dp;
2010 	struct vport *vport;
2011 
2012 	if (a[OVS_VPORT_ATTR_IFINDEX])
2013 		return ERR_PTR(-EOPNOTSUPP);
2014 	if (a[OVS_VPORT_ATTR_NAME]) {
2015 		vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2016 		if (!vport)
2017 			return ERR_PTR(-ENODEV);
2018 		if (ovs_header->dp_ifindex &&
2019 		    ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2020 			return ERR_PTR(-ENODEV);
2021 		return vport;
2022 	} else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2023 		u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2024 
2025 		if (port_no >= DP_MAX_PORTS)
2026 			return ERR_PTR(-EFBIG);
2027 
2028 		dp = get_dp(net, ovs_header->dp_ifindex);
2029 		if (!dp)
2030 			return ERR_PTR(-ENODEV);
2031 
2032 		vport = ovs_vport_ovsl_rcu(dp, port_no);
2033 		if (!vport)
2034 			return ERR_PTR(-ENODEV);
2035 		return vport;
2036 	} else
2037 		return ERR_PTR(-EINVAL);
2038 
2039 }
2040 
2041 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2042 {
2043 	unsigned int dev_headroom, max_headroom = 0;
2044 	struct net_device *dev;
2045 	struct vport *vport;
2046 	int i;
2047 
2048 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2049 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2050 					lockdep_ovsl_is_held()) {
2051 			dev = vport->dev;
2052 			dev_headroom = netdev_get_fwd_headroom(dev);
2053 			if (dev_headroom > max_headroom)
2054 				max_headroom = dev_headroom;
2055 		}
2056 	}
2057 
2058 	return max_headroom;
2059 }
2060 
2061 /* Called with ovs_mutex */
2062 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2063 {
2064 	struct vport *vport;
2065 	int i;
2066 
2067 	dp->max_headroom = new_headroom;
2068 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
2069 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node,
2070 					lockdep_ovsl_is_held())
2071 			netdev_set_rx_headroom(vport->dev, new_headroom);
2072 }
2073 
2074 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2075 {
2076 	struct nlattr **a = info->attrs;
2077 	struct ovs_header *ovs_header = info->userhdr;
2078 	struct vport_parms parms;
2079 	struct sk_buff *reply;
2080 	struct vport *vport;
2081 	struct datapath *dp;
2082 	unsigned int new_headroom;
2083 	u32 port_no;
2084 	int err;
2085 
2086 	if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2087 	    !a[OVS_VPORT_ATTR_UPCALL_PID])
2088 		return -EINVAL;
2089 	if (a[OVS_VPORT_ATTR_IFINDEX])
2090 		return -EOPNOTSUPP;
2091 
2092 	port_no = a[OVS_VPORT_ATTR_PORT_NO]
2093 		? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2094 	if (port_no >= DP_MAX_PORTS)
2095 		return -EFBIG;
2096 
2097 	reply = ovs_vport_cmd_alloc_info();
2098 	if (!reply)
2099 		return -ENOMEM;
2100 
2101 	ovs_lock();
2102 restart:
2103 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2104 	err = -ENODEV;
2105 	if (!dp)
2106 		goto exit_unlock_free;
2107 
2108 	if (port_no) {
2109 		vport = ovs_vport_ovsl(dp, port_no);
2110 		err = -EBUSY;
2111 		if (vport)
2112 			goto exit_unlock_free;
2113 	} else {
2114 		for (port_no = 1; ; port_no++) {
2115 			if (port_no >= DP_MAX_PORTS) {
2116 				err = -EFBIG;
2117 				goto exit_unlock_free;
2118 			}
2119 			vport = ovs_vport_ovsl(dp, port_no);
2120 			if (!vport)
2121 				break;
2122 		}
2123 	}
2124 
2125 	parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2126 	parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2127 	parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2128 	parms.dp = dp;
2129 	parms.port_no = port_no;
2130 	parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2131 
2132 	vport = new_vport(&parms);
2133 	err = PTR_ERR(vport);
2134 	if (IS_ERR(vport)) {
2135 		if (err == -EAGAIN)
2136 			goto restart;
2137 		goto exit_unlock_free;
2138 	}
2139 
2140 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2141 				      info->snd_portid, info->snd_seq, 0,
2142 				      OVS_VPORT_CMD_NEW, GFP_KERNEL);
2143 
2144 	new_headroom = netdev_get_fwd_headroom(vport->dev);
2145 
2146 	if (new_headroom > dp->max_headroom)
2147 		ovs_update_headroom(dp, new_headroom);
2148 	else
2149 		netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2150 
2151 	BUG_ON(err < 0);
2152 	ovs_unlock();
2153 
2154 	ovs_notify(&dp_vport_genl_family, reply, info);
2155 	return 0;
2156 
2157 exit_unlock_free:
2158 	ovs_unlock();
2159 	kfree_skb(reply);
2160 	return err;
2161 }
2162 
2163 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2164 {
2165 	struct nlattr **a = info->attrs;
2166 	struct sk_buff *reply;
2167 	struct vport *vport;
2168 	int err;
2169 
2170 	reply = ovs_vport_cmd_alloc_info();
2171 	if (!reply)
2172 		return -ENOMEM;
2173 
2174 	ovs_lock();
2175 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2176 	err = PTR_ERR(vport);
2177 	if (IS_ERR(vport))
2178 		goto exit_unlock_free;
2179 
2180 	if (a[OVS_VPORT_ATTR_TYPE] &&
2181 	    nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2182 		err = -EINVAL;
2183 		goto exit_unlock_free;
2184 	}
2185 
2186 	if (a[OVS_VPORT_ATTR_OPTIONS]) {
2187 		err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2188 		if (err)
2189 			goto exit_unlock_free;
2190 	}
2191 
2192 
2193 	if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2194 		struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2195 
2196 		err = ovs_vport_set_upcall_portids(vport, ids);
2197 		if (err)
2198 			goto exit_unlock_free;
2199 	}
2200 
2201 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2202 				      info->snd_portid, info->snd_seq, 0,
2203 				      OVS_VPORT_CMD_SET, GFP_KERNEL);
2204 	BUG_ON(err < 0);
2205 
2206 	ovs_unlock();
2207 	ovs_notify(&dp_vport_genl_family, reply, info);
2208 	return 0;
2209 
2210 exit_unlock_free:
2211 	ovs_unlock();
2212 	kfree_skb(reply);
2213 	return err;
2214 }
2215 
2216 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2217 {
2218 	bool update_headroom = false;
2219 	struct nlattr **a = info->attrs;
2220 	struct sk_buff *reply;
2221 	struct datapath *dp;
2222 	struct vport *vport;
2223 	unsigned int new_headroom;
2224 	int err;
2225 
2226 	reply = ovs_vport_cmd_alloc_info();
2227 	if (!reply)
2228 		return -ENOMEM;
2229 
2230 	ovs_lock();
2231 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2232 	err = PTR_ERR(vport);
2233 	if (IS_ERR(vport))
2234 		goto exit_unlock_free;
2235 
2236 	if (vport->port_no == OVSP_LOCAL) {
2237 		err = -EINVAL;
2238 		goto exit_unlock_free;
2239 	}
2240 
2241 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2242 				      info->snd_portid, info->snd_seq, 0,
2243 				      OVS_VPORT_CMD_DEL, GFP_KERNEL);
2244 	BUG_ON(err < 0);
2245 
2246 	/* the vport deletion may trigger dp headroom update */
2247 	dp = vport->dp;
2248 	if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2249 		update_headroom = true;
2250 
2251 	netdev_reset_rx_headroom(vport->dev);
2252 	ovs_dp_detach_port(vport);
2253 
2254 	if (update_headroom) {
2255 		new_headroom = ovs_get_max_headroom(dp);
2256 
2257 		if (new_headroom < dp->max_headroom)
2258 			ovs_update_headroom(dp, new_headroom);
2259 	}
2260 	ovs_unlock();
2261 
2262 	ovs_notify(&dp_vport_genl_family, reply, info);
2263 	return 0;
2264 
2265 exit_unlock_free:
2266 	ovs_unlock();
2267 	kfree_skb(reply);
2268 	return err;
2269 }
2270 
2271 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2272 {
2273 	struct nlattr **a = info->attrs;
2274 	struct ovs_header *ovs_header = info->userhdr;
2275 	struct sk_buff *reply;
2276 	struct vport *vport;
2277 	int err;
2278 
2279 	reply = ovs_vport_cmd_alloc_info();
2280 	if (!reply)
2281 		return -ENOMEM;
2282 
2283 	rcu_read_lock();
2284 	vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2285 	err = PTR_ERR(vport);
2286 	if (IS_ERR(vport))
2287 		goto exit_unlock_free;
2288 	err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2289 				      info->snd_portid, info->snd_seq, 0,
2290 				      OVS_VPORT_CMD_GET, GFP_ATOMIC);
2291 	BUG_ON(err < 0);
2292 	rcu_read_unlock();
2293 
2294 	return genlmsg_reply(reply, info);
2295 
2296 exit_unlock_free:
2297 	rcu_read_unlock();
2298 	kfree_skb(reply);
2299 	return err;
2300 }
2301 
2302 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2303 {
2304 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2305 	struct datapath *dp;
2306 	int bucket = cb->args[0], skip = cb->args[1];
2307 	int i, j = 0;
2308 
2309 	rcu_read_lock();
2310 	dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2311 	if (!dp) {
2312 		rcu_read_unlock();
2313 		return -ENODEV;
2314 	}
2315 	for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2316 		struct vport *vport;
2317 
2318 		j = 0;
2319 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2320 			if (j >= skip &&
2321 			    ovs_vport_cmd_fill_info(vport, skb,
2322 						    sock_net(skb->sk),
2323 						    NETLINK_CB(cb->skb).portid,
2324 						    cb->nlh->nlmsg_seq,
2325 						    NLM_F_MULTI,
2326 						    OVS_VPORT_CMD_GET,
2327 						    GFP_ATOMIC) < 0)
2328 				goto out;
2329 
2330 			j++;
2331 		}
2332 		skip = 0;
2333 	}
2334 out:
2335 	rcu_read_unlock();
2336 
2337 	cb->args[0] = i;
2338 	cb->args[1] = j;
2339 
2340 	return skb->len;
2341 }
2342 
2343 static void ovs_dp_masks_rebalance(struct work_struct *work)
2344 {
2345 	struct ovs_net *ovs_net = container_of(work, struct ovs_net,
2346 					       masks_rebalance.work);
2347 	struct datapath *dp;
2348 
2349 	ovs_lock();
2350 
2351 	list_for_each_entry(dp, &ovs_net->dps, list_node)
2352 		ovs_flow_masks_rebalance(&dp->table);
2353 
2354 	ovs_unlock();
2355 
2356 	schedule_delayed_work(&ovs_net->masks_rebalance,
2357 			      msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
2358 }
2359 
2360 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2361 	[OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2362 	[OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2363 	[OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2364 	[OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2365 	[OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2366 	[OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2367 	[OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2368 	[OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2369 };
2370 
2371 static const struct genl_ops dp_vport_genl_ops[] = {
2372 	{ .cmd = OVS_VPORT_CMD_NEW,
2373 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2374 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2375 	  .doit = ovs_vport_cmd_new
2376 	},
2377 	{ .cmd = OVS_VPORT_CMD_DEL,
2378 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2379 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2380 	  .doit = ovs_vport_cmd_del
2381 	},
2382 	{ .cmd = OVS_VPORT_CMD_GET,
2383 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2384 	  .flags = 0,		    /* OK for unprivileged users. */
2385 	  .doit = ovs_vport_cmd_get,
2386 	  .dumpit = ovs_vport_cmd_dump
2387 	},
2388 	{ .cmd = OVS_VPORT_CMD_SET,
2389 	  .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2390 	  .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2391 	  .doit = ovs_vport_cmd_set,
2392 	},
2393 };
2394 
2395 struct genl_family dp_vport_genl_family __ro_after_init = {
2396 	.hdrsize = sizeof(struct ovs_header),
2397 	.name = OVS_VPORT_FAMILY,
2398 	.version = OVS_VPORT_VERSION,
2399 	.maxattr = OVS_VPORT_ATTR_MAX,
2400 	.policy = vport_policy,
2401 	.netnsok = true,
2402 	.parallel_ops = true,
2403 	.ops = dp_vport_genl_ops,
2404 	.n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2405 	.mcgrps = &ovs_dp_vport_multicast_group,
2406 	.n_mcgrps = 1,
2407 	.module = THIS_MODULE,
2408 };
2409 
2410 static struct genl_family * const dp_genl_families[] = {
2411 	&dp_datapath_genl_family,
2412 	&dp_vport_genl_family,
2413 	&dp_flow_genl_family,
2414 	&dp_packet_genl_family,
2415 	&dp_meter_genl_family,
2416 #if	IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2417 	&dp_ct_limit_genl_family,
2418 #endif
2419 };
2420 
2421 static void dp_unregister_genl(int n_families)
2422 {
2423 	int i;
2424 
2425 	for (i = 0; i < n_families; i++)
2426 		genl_unregister_family(dp_genl_families[i]);
2427 }
2428 
2429 static int __init dp_register_genl(void)
2430 {
2431 	int err;
2432 	int i;
2433 
2434 	for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2435 
2436 		err = genl_register_family(dp_genl_families[i]);
2437 		if (err)
2438 			goto error;
2439 	}
2440 
2441 	return 0;
2442 
2443 error:
2444 	dp_unregister_genl(i);
2445 	return err;
2446 }
2447 
2448 static int __net_init ovs_init_net(struct net *net)
2449 {
2450 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2451 
2452 	INIT_LIST_HEAD(&ovs_net->dps);
2453 	INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2454 	INIT_DELAYED_WORK(&ovs_net->masks_rebalance, ovs_dp_masks_rebalance);
2455 	schedule_delayed_work(&ovs_net->masks_rebalance,
2456 			      msecs_to_jiffies(DP_MASKS_REBALANCE_INTERVAL));
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_delayed_work_sync(&ovs_net->masks_rebalance);
2512 	cancel_work_sync(&ovs_net->dp_notify_work);
2513 }
2514 
2515 static struct pernet_operations ovs_net_ops = {
2516 	.init = ovs_init_net,
2517 	.exit = ovs_exit_net,
2518 	.id   = &ovs_net_id,
2519 	.size = sizeof(struct ovs_net),
2520 };
2521 
2522 static int __init dp_init(void)
2523 {
2524 	int err;
2525 
2526 	BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof_field(struct sk_buff, cb));
2527 
2528 	pr_info("Open vSwitch switching datapath\n");
2529 
2530 	err = action_fifos_init();
2531 	if (err)
2532 		goto error;
2533 
2534 	err = ovs_internal_dev_rtnl_link_register();
2535 	if (err)
2536 		goto error_action_fifos_exit;
2537 
2538 	err = ovs_flow_init();
2539 	if (err)
2540 		goto error_unreg_rtnl_link;
2541 
2542 	err = ovs_vport_init();
2543 	if (err)
2544 		goto error_flow_exit;
2545 
2546 	err = register_pernet_device(&ovs_net_ops);
2547 	if (err)
2548 		goto error_vport_exit;
2549 
2550 	err = register_netdevice_notifier(&ovs_dp_device_notifier);
2551 	if (err)
2552 		goto error_netns_exit;
2553 
2554 	err = ovs_netdev_init();
2555 	if (err)
2556 		goto error_unreg_notifier;
2557 
2558 	err = dp_register_genl();
2559 	if (err < 0)
2560 		goto error_unreg_netdev;
2561 
2562 	return 0;
2563 
2564 error_unreg_netdev:
2565 	ovs_netdev_exit();
2566 error_unreg_notifier:
2567 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2568 error_netns_exit:
2569 	unregister_pernet_device(&ovs_net_ops);
2570 error_vport_exit:
2571 	ovs_vport_exit();
2572 error_flow_exit:
2573 	ovs_flow_exit();
2574 error_unreg_rtnl_link:
2575 	ovs_internal_dev_rtnl_link_unregister();
2576 error_action_fifos_exit:
2577 	action_fifos_exit();
2578 error:
2579 	return err;
2580 }
2581 
2582 static void dp_cleanup(void)
2583 {
2584 	dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2585 	ovs_netdev_exit();
2586 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2587 	unregister_pernet_device(&ovs_net_ops);
2588 	rcu_barrier();
2589 	ovs_vport_exit();
2590 	ovs_flow_exit();
2591 	ovs_internal_dev_rtnl_link_unregister();
2592 	action_fifos_exit();
2593 }
2594 
2595 module_init(dp_init);
2596 module_exit(dp_cleanup);
2597 
2598 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2599 MODULE_LICENSE("GPL");
2600 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2601 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2602 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2603 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2604 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2605 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);
2606