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