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