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