xref: /openbmc/linux/net/openvswitch/datapath.c (revision 25cd9ba0abc0749e5cb78e6493c6f6b3311ec6c5)
1 /*
2  * Copyright (c) 2007-2014 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18 
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/openvswitch.h>
48 #include <linux/rculist.h>
49 #include <linux/dmi.h>
50 #include <net/genetlink.h>
51 #include <net/net_namespace.h>
52 #include <net/netns/generic.h>
53 
54 #include "datapath.h"
55 #include "flow.h"
56 #include "flow_table.h"
57 #include "flow_netlink.h"
58 #include "vport-internal_dev.h"
59 #include "vport-netdev.h"
60 
61 int ovs_net_id __read_mostly;
62 EXPORT_SYMBOL(ovs_net_id);
63 
64 static struct genl_family dp_packet_genl_family;
65 static struct genl_family dp_flow_genl_family;
66 static struct genl_family dp_datapath_genl_family;
67 
68 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
69 	.name = OVS_FLOW_MCGROUP,
70 };
71 
72 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
73 	.name = OVS_DATAPATH_MCGROUP,
74 };
75 
76 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
77 	.name = OVS_VPORT_MCGROUP,
78 };
79 
80 /* Check if need to build a reply message.
81  * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
82 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
83 			    unsigned int group)
84 {
85 	return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
86 	       genl_has_listeners(family, genl_info_net(info)->genl_sock,
87 				  group);
88 }
89 
90 static void ovs_notify(struct genl_family *family,
91 		       struct sk_buff *skb, struct genl_info *info)
92 {
93 	genl_notify(family, skb, genl_info_net(info), info->snd_portid,
94 		    0, info->nlhdr, GFP_KERNEL);
95 }
96 
97 /**
98  * DOC: Locking:
99  *
100  * All writes e.g. Writes to device state (add/remove datapath, port, set
101  * operations on vports, etc.), Writes to other state (flow table
102  * modifications, set miscellaneous datapath parameters, etc.) are protected
103  * by ovs_lock.
104  *
105  * Reads are protected by RCU.
106  *
107  * There are a few special cases (mostly stats) that have their own
108  * synchronization but they nest under all of above and don't interact with
109  * each other.
110  *
111  * The RTNL lock nests inside ovs_mutex.
112  */
113 
114 static DEFINE_MUTEX(ovs_mutex);
115 
116 void ovs_lock(void)
117 {
118 	mutex_lock(&ovs_mutex);
119 }
120 
121 void ovs_unlock(void)
122 {
123 	mutex_unlock(&ovs_mutex);
124 }
125 
126 #ifdef CONFIG_LOCKDEP
127 int lockdep_ovsl_is_held(void)
128 {
129 	if (debug_locks)
130 		return lockdep_is_held(&ovs_mutex);
131 	else
132 		return 1;
133 }
134 EXPORT_SYMBOL(lockdep_ovsl_is_held);
135 #endif
136 
137 static struct vport *new_vport(const struct vport_parms *);
138 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139 			     const struct dp_upcall_info *);
140 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
141 				  const struct dp_upcall_info *);
142 
143 /* Must be called with rcu_read_lock or ovs_mutex. */
144 static struct datapath *get_dp(struct net *net, int dp_ifindex)
145 {
146 	struct datapath *dp = NULL;
147 	struct net_device *dev;
148 
149 	rcu_read_lock();
150 	dev = dev_get_by_index_rcu(net, dp_ifindex);
151 	if (dev) {
152 		struct vport *vport = ovs_internal_dev_get_vport(dev);
153 		if (vport)
154 			dp = vport->dp;
155 	}
156 	rcu_read_unlock();
157 
158 	return dp;
159 }
160 
161 /* Must be called with rcu_read_lock or ovs_mutex. */
162 const char *ovs_dp_name(const struct datapath *dp)
163 {
164 	struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
165 	return vport->ops->get_name(vport);
166 }
167 
168 static int get_dpifindex(struct datapath *dp)
169 {
170 	struct vport *local;
171 	int ifindex;
172 
173 	rcu_read_lock();
174 
175 	local = ovs_vport_rcu(dp, OVSP_LOCAL);
176 	if (local)
177 		ifindex = netdev_vport_priv(local)->dev->ifindex;
178 	else
179 		ifindex = 0;
180 
181 	rcu_read_unlock();
182 
183 	return ifindex;
184 }
185 
186 static void destroy_dp_rcu(struct rcu_head *rcu)
187 {
188 	struct datapath *dp = container_of(rcu, struct datapath, rcu);
189 
190 	free_percpu(dp->stats_percpu);
191 	release_net(ovs_dp_get_net(dp));
192 	kfree(dp->ports);
193 	kfree(dp);
194 }
195 
196 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
197 					    u16 port_no)
198 {
199 	return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
200 }
201 
202 /* Called with ovs_mutex or RCU read lock. */
203 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
204 {
205 	struct vport *vport;
206 	struct hlist_head *head;
207 
208 	head = vport_hash_bucket(dp, port_no);
209 	hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
210 		if (vport->port_no == port_no)
211 			return vport;
212 	}
213 	return NULL;
214 }
215 
216 /* Called with ovs_mutex. */
217 static struct vport *new_vport(const struct vport_parms *parms)
218 {
219 	struct vport *vport;
220 
221 	vport = ovs_vport_add(parms);
222 	if (!IS_ERR(vport)) {
223 		struct datapath *dp = parms->dp;
224 		struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
225 
226 		hlist_add_head_rcu(&vport->dp_hash_node, head);
227 	}
228 	return vport;
229 }
230 
231 void ovs_dp_detach_port(struct vport *p)
232 {
233 	ASSERT_OVSL();
234 
235 	/* First drop references to device. */
236 	hlist_del_rcu(&p->dp_hash_node);
237 
238 	/* Then destroy it. */
239 	ovs_vport_del(p);
240 }
241 
242 /* Must be called with rcu_read_lock. */
243 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
244 {
245 	const struct vport *p = OVS_CB(skb)->input_vport;
246 	struct datapath *dp = p->dp;
247 	struct sw_flow *flow;
248 	struct dp_stats_percpu *stats;
249 	u64 *stats_counter;
250 	u32 n_mask_hit;
251 
252 	stats = this_cpu_ptr(dp->stats_percpu);
253 
254 	/* Look up flow. */
255 	flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
256 	if (unlikely(!flow)) {
257 		struct dp_upcall_info upcall;
258 		int error;
259 
260 		upcall.cmd = OVS_PACKET_CMD_MISS;
261 		upcall.key = key;
262 		upcall.userdata = NULL;
263 		upcall.portid = ovs_vport_find_upcall_portid(p, skb);
264 		error = ovs_dp_upcall(dp, skb, &upcall);
265 		if (unlikely(error))
266 			kfree_skb(skb);
267 		else
268 			consume_skb(skb);
269 		stats_counter = &stats->n_missed;
270 		goto out;
271 	}
272 
273 	OVS_CB(skb)->flow = flow;
274 
275 	ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
276 	ovs_execute_actions(dp, skb, key);
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 	u64_stats_update_end(&stats->syncp);
285 }
286 
287 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
288 		  const struct dp_upcall_info *upcall_info)
289 {
290 	struct dp_stats_percpu *stats;
291 	int err;
292 
293 	if (upcall_info->portid == 0) {
294 		err = -ENOTCONN;
295 		goto err;
296 	}
297 
298 	if (!skb_is_gso(skb))
299 		err = queue_userspace_packet(dp, skb, upcall_info);
300 	else
301 		err = queue_gso_packets(dp, skb, upcall_info);
302 	if (err)
303 		goto err;
304 
305 	return 0;
306 
307 err:
308 	stats = this_cpu_ptr(dp->stats_percpu);
309 
310 	u64_stats_update_begin(&stats->syncp);
311 	stats->n_lost++;
312 	u64_stats_update_end(&stats->syncp);
313 
314 	return err;
315 }
316 
317 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
318 			     const struct dp_upcall_info *upcall_info)
319 {
320 	unsigned short gso_type = skb_shinfo(skb)->gso_type;
321 	struct dp_upcall_info later_info;
322 	struct sw_flow_key later_key;
323 	struct sk_buff *segs, *nskb;
324 	int err;
325 
326 	segs = __skb_gso_segment(skb, NETIF_F_SG, false);
327 	if (IS_ERR(segs))
328 		return PTR_ERR(segs);
329 	if (segs == NULL)
330 		return -EINVAL;
331 
332 	/* Queue all of the segments. */
333 	skb = segs;
334 	do {
335 		err = queue_userspace_packet(dp, skb, upcall_info);
336 		if (err)
337 			break;
338 
339 		if (skb == segs && gso_type & SKB_GSO_UDP) {
340 			/* The initial flow key extracted by ovs_flow_extract()
341 			 * in this case is for a first fragment, so we need to
342 			 * properly mark later fragments.
343 			 */
344 			later_key = *upcall_info->key;
345 			later_key.ip.frag = OVS_FRAG_TYPE_LATER;
346 
347 			later_info = *upcall_info;
348 			later_info.key = &later_key;
349 			upcall_info = &later_info;
350 		}
351 	} while ((skb = skb->next));
352 
353 	/* Free all of the segments. */
354 	skb = segs;
355 	do {
356 		nskb = skb->next;
357 		if (err)
358 			kfree_skb(skb);
359 		else
360 			consume_skb(skb);
361 	} while ((skb = nskb));
362 	return err;
363 }
364 
365 static size_t key_attr_size(void)
366 {
367 	return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
368 		+ nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
369 		  + nla_total_size(8)   /* OVS_TUNNEL_KEY_ATTR_ID */
370 		  + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
371 		  + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
372 		  + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TOS */
373 		  + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TTL */
374 		  + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
375 		  + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_CSUM */
376 		  + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_OAM */
377 		  + nla_total_size(256)   /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
378 		+ nla_total_size(4)   /* OVS_KEY_ATTR_IN_PORT */
379 		+ nla_total_size(4)   /* OVS_KEY_ATTR_SKB_MARK */
380 		+ nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
381 		+ nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
382 		+ nla_total_size(4)   /* OVS_KEY_ATTR_8021Q */
383 		+ nla_total_size(0)   /* OVS_KEY_ATTR_ENCAP */
384 		+ nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
385 		+ nla_total_size(40)  /* OVS_KEY_ATTR_IPV6 */
386 		+ nla_total_size(2)   /* OVS_KEY_ATTR_ICMPV6 */
387 		+ nla_total_size(28); /* OVS_KEY_ATTR_ND */
388 }
389 
390 static size_t upcall_msg_size(const struct nlattr *userdata,
391 			      unsigned int hdrlen)
392 {
393 	size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
394 		+ nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
395 		+ nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
396 
397 	/* OVS_PACKET_ATTR_USERDATA */
398 	if (userdata)
399 		size += NLA_ALIGN(userdata->nla_len);
400 
401 	return size;
402 }
403 
404 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
405 				  const struct dp_upcall_info *upcall_info)
406 {
407 	struct ovs_header *upcall;
408 	struct sk_buff *nskb = NULL;
409 	struct sk_buff *user_skb = NULL; /* to be queued to userspace */
410 	struct nlattr *nla;
411 	struct genl_info info = {
412 		.dst_sk = ovs_dp_get_net(dp)->genl_sock,
413 		.snd_portid = upcall_info->portid,
414 	};
415 	size_t len;
416 	unsigned int hlen;
417 	int err, dp_ifindex;
418 
419 	dp_ifindex = get_dpifindex(dp);
420 	if (!dp_ifindex)
421 		return -ENODEV;
422 
423 	if (vlan_tx_tag_present(skb)) {
424 		nskb = skb_clone(skb, GFP_ATOMIC);
425 		if (!nskb)
426 			return -ENOMEM;
427 
428 		nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
429 		if (!nskb)
430 			return -ENOMEM;
431 
432 		nskb->vlan_tci = 0;
433 		skb = nskb;
434 	}
435 
436 	if (nla_attr_size(skb->len) > USHRT_MAX) {
437 		err = -EFBIG;
438 		goto out;
439 	}
440 
441 	/* Complete checksum if needed */
442 	if (skb->ip_summed == CHECKSUM_PARTIAL &&
443 	    (err = skb_checksum_help(skb)))
444 		goto out;
445 
446 	/* Older versions of OVS user space enforce alignment of the last
447 	 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 	 * padding logic. Only perform zerocopy if padding is not required.
449 	 */
450 	if (dp->user_features & OVS_DP_F_UNALIGNED)
451 		hlen = skb_zerocopy_headlen(skb);
452 	else
453 		hlen = skb->len;
454 
455 	len = upcall_msg_size(upcall_info->userdata, hlen);
456 	user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
457 	if (!user_skb) {
458 		err = -ENOMEM;
459 		goto out;
460 	}
461 
462 	upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
463 			     0, upcall_info->cmd);
464 	upcall->dp_ifindex = dp_ifindex;
465 
466 	nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
467 	err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
468 	BUG_ON(err);
469 	nla_nest_end(user_skb, nla);
470 
471 	if (upcall_info->userdata)
472 		__nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
473 			  nla_len(upcall_info->userdata),
474 			  nla_data(upcall_info->userdata));
475 
476 	/* Only reserve room for attribute header, packet data is added
477 	 * in skb_zerocopy() */
478 	if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
479 		err = -ENOBUFS;
480 		goto out;
481 	}
482 	nla->nla_len = nla_attr_size(skb->len);
483 
484 	err = skb_zerocopy(user_skb, skb, skb->len, hlen);
485 	if (err)
486 		goto out;
487 
488 	/* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
489 	if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
490 		size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
491 
492 		if (plen > 0)
493 			memset(skb_put(user_skb, plen), 0, plen);
494 	}
495 
496 	((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
497 
498 	err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
499 	user_skb = NULL;
500 out:
501 	if (err)
502 		skb_tx_error(skb);
503 	kfree_skb(user_skb);
504 	kfree_skb(nskb);
505 	return err;
506 }
507 
508 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
509 {
510 	struct ovs_header *ovs_header = info->userhdr;
511 	struct nlattr **a = info->attrs;
512 	struct sw_flow_actions *acts;
513 	struct sk_buff *packet;
514 	struct sw_flow *flow;
515 	struct datapath *dp;
516 	struct ethhdr *eth;
517 	struct vport *input_vport;
518 	int len;
519 	int err;
520 
521 	err = -EINVAL;
522 	if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
523 	    !a[OVS_PACKET_ATTR_ACTIONS])
524 		goto err;
525 
526 	len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
527 	packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
528 	err = -ENOMEM;
529 	if (!packet)
530 		goto err;
531 	skb_reserve(packet, NET_IP_ALIGN);
532 
533 	nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
534 
535 	skb_reset_mac_header(packet);
536 	eth = eth_hdr(packet);
537 
538 	/* Normally, setting the skb 'protocol' field would be handled by a
539 	 * call to eth_type_trans(), but it assumes there's a sending
540 	 * device, which we may not have. */
541 	if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
542 		packet->protocol = eth->h_proto;
543 	else
544 		packet->protocol = htons(ETH_P_802_2);
545 
546 	/* Build an sw_flow for sending this packet. */
547 	flow = ovs_flow_alloc();
548 	err = PTR_ERR(flow);
549 	if (IS_ERR(flow))
550 		goto err_kfree_skb;
551 
552 	err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
553 					     &flow->key);
554 	if (err)
555 		goto err_flow_free;
556 
557 	acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
558 	err = PTR_ERR(acts);
559 	if (IS_ERR(acts))
560 		goto err_flow_free;
561 
562 	err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
563 				   &flow->key, &acts);
564 	if (err)
565 		goto err_flow_free;
566 
567 	rcu_assign_pointer(flow->sf_acts, acts);
568 
569 	OVS_CB(packet)->egress_tun_info = NULL;
570 	OVS_CB(packet)->flow = flow;
571 	packet->priority = flow->key.phy.priority;
572 	packet->mark = flow->key.phy.skb_mark;
573 
574 	rcu_read_lock();
575 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
576 	err = -ENODEV;
577 	if (!dp)
578 		goto err_unlock;
579 
580 	input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
581 	if (!input_vport)
582 		input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
583 
584 	if (!input_vport)
585 		goto err_unlock;
586 
587 	OVS_CB(packet)->input_vport = input_vport;
588 
589 	local_bh_disable();
590 	err = ovs_execute_actions(dp, packet, &flow->key);
591 	local_bh_enable();
592 	rcu_read_unlock();
593 
594 	ovs_flow_free(flow, false);
595 	return err;
596 
597 err_unlock:
598 	rcu_read_unlock();
599 err_flow_free:
600 	ovs_flow_free(flow, false);
601 err_kfree_skb:
602 	kfree_skb(packet);
603 err:
604 	return err;
605 }
606 
607 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
608 	[OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
609 	[OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
610 	[OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
611 };
612 
613 static const struct genl_ops dp_packet_genl_ops[] = {
614 	{ .cmd = OVS_PACKET_CMD_EXECUTE,
615 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
616 	  .policy = packet_policy,
617 	  .doit = ovs_packet_cmd_execute
618 	}
619 };
620 
621 static struct genl_family dp_packet_genl_family = {
622 	.id = GENL_ID_GENERATE,
623 	.hdrsize = sizeof(struct ovs_header),
624 	.name = OVS_PACKET_FAMILY,
625 	.version = OVS_PACKET_VERSION,
626 	.maxattr = OVS_PACKET_ATTR_MAX,
627 	.netnsok = true,
628 	.parallel_ops = true,
629 	.ops = dp_packet_genl_ops,
630 	.n_ops = ARRAY_SIZE(dp_packet_genl_ops),
631 };
632 
633 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
634 			 struct ovs_dp_megaflow_stats *mega_stats)
635 {
636 	int i;
637 
638 	memset(mega_stats, 0, sizeof(*mega_stats));
639 
640 	stats->n_flows = ovs_flow_tbl_count(&dp->table);
641 	mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
642 
643 	stats->n_hit = stats->n_missed = stats->n_lost = 0;
644 
645 	for_each_possible_cpu(i) {
646 		const struct dp_stats_percpu *percpu_stats;
647 		struct dp_stats_percpu local_stats;
648 		unsigned int start;
649 
650 		percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
651 
652 		do {
653 			start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
654 			local_stats = *percpu_stats;
655 		} while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
656 
657 		stats->n_hit += local_stats.n_hit;
658 		stats->n_missed += local_stats.n_missed;
659 		stats->n_lost += local_stats.n_lost;
660 		mega_stats->n_mask_hit += local_stats.n_mask_hit;
661 	}
662 }
663 
664 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
665 {
666 	return NLMSG_ALIGN(sizeof(struct ovs_header))
667 		+ nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
668 		+ nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
669 		+ nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
670 		+ nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
671 		+ nla_total_size(8) /* OVS_FLOW_ATTR_USED */
672 		+ nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
673 }
674 
675 /* Called with ovs_mutex or RCU read lock. */
676 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
677 				  struct sk_buff *skb, u32 portid,
678 				  u32 seq, u32 flags, u8 cmd)
679 {
680 	const int skb_orig_len = skb->len;
681 	struct nlattr *start;
682 	struct ovs_flow_stats stats;
683 	__be16 tcp_flags;
684 	unsigned long used;
685 	struct ovs_header *ovs_header;
686 	struct nlattr *nla;
687 	int err;
688 
689 	ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
690 	if (!ovs_header)
691 		return -EMSGSIZE;
692 
693 	ovs_header->dp_ifindex = dp_ifindex;
694 
695 	/* Fill flow key. */
696 	nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
697 	if (!nla)
698 		goto nla_put_failure;
699 
700 	err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
701 	if (err)
702 		goto error;
703 	nla_nest_end(skb, nla);
704 
705 	nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
706 	if (!nla)
707 		goto nla_put_failure;
708 
709 	err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
710 	if (err)
711 		goto error;
712 
713 	nla_nest_end(skb, nla);
714 
715 	ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
716 
717 	if (used &&
718 	    nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
719 		goto nla_put_failure;
720 
721 	if (stats.n_packets &&
722 	    nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
723 		goto nla_put_failure;
724 
725 	if ((u8)ntohs(tcp_flags) &&
726 	     nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
727 		goto nla_put_failure;
728 
729 	/* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
730 	 * this is the first flow to be dumped into 'skb'.  This is unusual for
731 	 * Netlink but individual action lists can be longer than
732 	 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
733 	 * The userspace caller can always fetch the actions separately if it
734 	 * really wants them.  (Most userspace callers in fact don't care.)
735 	 *
736 	 * This can only fail for dump operations because the skb is always
737 	 * properly sized for single flows.
738 	 */
739 	start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
740 	if (start) {
741 		const struct sw_flow_actions *sf_acts;
742 
743 		sf_acts = rcu_dereference_ovsl(flow->sf_acts);
744 		err = ovs_nla_put_actions(sf_acts->actions,
745 					  sf_acts->actions_len, skb);
746 
747 		if (!err)
748 			nla_nest_end(skb, start);
749 		else {
750 			if (skb_orig_len)
751 				goto error;
752 
753 			nla_nest_cancel(skb, start);
754 		}
755 	} else if (skb_orig_len)
756 		goto nla_put_failure;
757 
758 	return genlmsg_end(skb, ovs_header);
759 
760 nla_put_failure:
761 	err = -EMSGSIZE;
762 error:
763 	genlmsg_cancel(skb, ovs_header);
764 	return err;
765 }
766 
767 /* May not be called with RCU read lock. */
768 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
769 					       struct genl_info *info,
770 					       bool always)
771 {
772 	struct sk_buff *skb;
773 
774 	if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
775 		return NULL;
776 
777 	skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
778 	if (!skb)
779 		return ERR_PTR(-ENOMEM);
780 
781 	return skb;
782 }
783 
784 /* Called with ovs_mutex. */
785 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
786 					       int dp_ifindex,
787 					       struct genl_info *info, u8 cmd,
788 					       bool always)
789 {
790 	struct sk_buff *skb;
791 	int retval;
792 
793 	skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
794 				      always);
795 	if (IS_ERR_OR_NULL(skb))
796 		return skb;
797 
798 	retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
799 					info->snd_portid, info->snd_seq, 0,
800 					cmd);
801 	BUG_ON(retval < 0);
802 	return skb;
803 }
804 
805 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
806 {
807 	struct nlattr **a = info->attrs;
808 	struct ovs_header *ovs_header = info->userhdr;
809 	struct sw_flow *flow, *new_flow;
810 	struct sw_flow_mask mask;
811 	struct sk_buff *reply;
812 	struct datapath *dp;
813 	struct sw_flow_actions *acts;
814 	struct sw_flow_match match;
815 	int error;
816 
817 	/* Must have key and actions. */
818 	error = -EINVAL;
819 	if (!a[OVS_FLOW_ATTR_KEY])
820 		goto error;
821 	if (!a[OVS_FLOW_ATTR_ACTIONS])
822 		goto error;
823 
824 	/* Most of the time we need to allocate a new flow, do it before
825 	 * locking.
826 	 */
827 	new_flow = ovs_flow_alloc();
828 	if (IS_ERR(new_flow)) {
829 		error = PTR_ERR(new_flow);
830 		goto error;
831 	}
832 
833 	/* Extract key. */
834 	ovs_match_init(&match, &new_flow->unmasked_key, &mask);
835 	error = ovs_nla_get_match(&match,
836 				  a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
837 	if (error)
838 		goto err_kfree_flow;
839 
840 	ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
841 
842 	/* Validate actions. */
843 	acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
844 	error = PTR_ERR(acts);
845 	if (IS_ERR(acts))
846 		goto err_kfree_flow;
847 
848 	error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
849 				     &acts);
850 	if (error) {
851 		OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
852 		goto err_kfree_acts;
853 	}
854 
855 	reply = ovs_flow_cmd_alloc_info(acts, info, false);
856 	if (IS_ERR(reply)) {
857 		error = PTR_ERR(reply);
858 		goto err_kfree_acts;
859 	}
860 
861 	ovs_lock();
862 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
863 	if (unlikely(!dp)) {
864 		error = -ENODEV;
865 		goto err_unlock_ovs;
866 	}
867 	/* Check if this is a duplicate flow */
868 	flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
869 	if (likely(!flow)) {
870 		rcu_assign_pointer(new_flow->sf_acts, acts);
871 
872 		/* Put flow in bucket. */
873 		error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
874 		if (unlikely(error)) {
875 			acts = NULL;
876 			goto err_unlock_ovs;
877 		}
878 
879 		if (unlikely(reply)) {
880 			error = ovs_flow_cmd_fill_info(new_flow,
881 						       ovs_header->dp_ifindex,
882 						       reply, info->snd_portid,
883 						       info->snd_seq, 0,
884 						       OVS_FLOW_CMD_NEW);
885 			BUG_ON(error < 0);
886 		}
887 		ovs_unlock();
888 	} else {
889 		struct sw_flow_actions *old_acts;
890 
891 		/* Bail out if we're not allowed to modify an existing flow.
892 		 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
893 		 * because Generic Netlink treats the latter as a dump
894 		 * request.  We also accept NLM_F_EXCL in case that bug ever
895 		 * gets fixed.
896 		 */
897 		if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
898 							 | NLM_F_EXCL))) {
899 			error = -EEXIST;
900 			goto err_unlock_ovs;
901 		}
902 		/* The unmasked key has to be the same for flow updates. */
903 		if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
904 			flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
905 			if (!flow) {
906 				error = -ENOENT;
907 				goto err_unlock_ovs;
908 			}
909 		}
910 		/* Update actions. */
911 		old_acts = ovsl_dereference(flow->sf_acts);
912 		rcu_assign_pointer(flow->sf_acts, acts);
913 
914 		if (unlikely(reply)) {
915 			error = ovs_flow_cmd_fill_info(flow,
916 						       ovs_header->dp_ifindex,
917 						       reply, info->snd_portid,
918 						       info->snd_seq, 0,
919 						       OVS_FLOW_CMD_NEW);
920 			BUG_ON(error < 0);
921 		}
922 		ovs_unlock();
923 
924 		ovs_nla_free_flow_actions(old_acts);
925 		ovs_flow_free(new_flow, false);
926 	}
927 
928 	if (reply)
929 		ovs_notify(&dp_flow_genl_family, reply, info);
930 	return 0;
931 
932 err_unlock_ovs:
933 	ovs_unlock();
934 	kfree_skb(reply);
935 err_kfree_acts:
936 	kfree(acts);
937 err_kfree_flow:
938 	ovs_flow_free(new_flow, false);
939 error:
940 	return error;
941 }
942 
943 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
944 						const struct sw_flow_key *key,
945 						const struct sw_flow_mask *mask)
946 {
947 	struct sw_flow_actions *acts;
948 	struct sw_flow_key masked_key;
949 	int error;
950 
951 	acts = ovs_nla_alloc_flow_actions(nla_len(a));
952 	if (IS_ERR(acts))
953 		return acts;
954 
955 	ovs_flow_mask_key(&masked_key, key, mask);
956 	error = ovs_nla_copy_actions(a, &masked_key, &acts);
957 	if (error) {
958 		OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
959 		kfree(acts);
960 		return ERR_PTR(error);
961 	}
962 
963 	return acts;
964 }
965 
966 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
967 {
968 	struct nlattr **a = info->attrs;
969 	struct ovs_header *ovs_header = info->userhdr;
970 	struct sw_flow_key key;
971 	struct sw_flow *flow;
972 	struct sw_flow_mask mask;
973 	struct sk_buff *reply = NULL;
974 	struct datapath *dp;
975 	struct sw_flow_actions *old_acts = NULL, *acts = NULL;
976 	struct sw_flow_match match;
977 	int error;
978 
979 	/* Extract key. */
980 	error = -EINVAL;
981 	if (!a[OVS_FLOW_ATTR_KEY])
982 		goto error;
983 
984 	ovs_match_init(&match, &key, &mask);
985 	error = ovs_nla_get_match(&match,
986 				  a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
987 	if (error)
988 		goto error;
989 
990 	/* Validate actions. */
991 	if (a[OVS_FLOW_ATTR_ACTIONS]) {
992 		acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
993 		if (IS_ERR(acts)) {
994 			error = PTR_ERR(acts);
995 			goto error;
996 		}
997 	}
998 
999 	/* Can allocate before locking if have acts. */
1000 	if (acts) {
1001 		reply = ovs_flow_cmd_alloc_info(acts, info, false);
1002 		if (IS_ERR(reply)) {
1003 			error = PTR_ERR(reply);
1004 			goto err_kfree_acts;
1005 		}
1006 	}
1007 
1008 	ovs_lock();
1009 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1010 	if (unlikely(!dp)) {
1011 		error = -ENODEV;
1012 		goto err_unlock_ovs;
1013 	}
1014 	/* Check that the flow exists. */
1015 	flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1016 	if (unlikely(!flow)) {
1017 		error = -ENOENT;
1018 		goto err_unlock_ovs;
1019 	}
1020 
1021 	/* Update actions, if present. */
1022 	if (likely(acts)) {
1023 		old_acts = ovsl_dereference(flow->sf_acts);
1024 		rcu_assign_pointer(flow->sf_acts, acts);
1025 
1026 		if (unlikely(reply)) {
1027 			error = ovs_flow_cmd_fill_info(flow,
1028 						       ovs_header->dp_ifindex,
1029 						       reply, info->snd_portid,
1030 						       info->snd_seq, 0,
1031 						       OVS_FLOW_CMD_NEW);
1032 			BUG_ON(error < 0);
1033 		}
1034 	} else {
1035 		/* Could not alloc without acts before locking. */
1036 		reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1037 						info, OVS_FLOW_CMD_NEW, false);
1038 		if (unlikely(IS_ERR(reply))) {
1039 			error = PTR_ERR(reply);
1040 			goto err_unlock_ovs;
1041 		}
1042 	}
1043 
1044 	/* Clear stats. */
1045 	if (a[OVS_FLOW_ATTR_CLEAR])
1046 		ovs_flow_stats_clear(flow);
1047 	ovs_unlock();
1048 
1049 	if (reply)
1050 		ovs_notify(&dp_flow_genl_family, reply, info);
1051 	if (old_acts)
1052 		ovs_nla_free_flow_actions(old_acts);
1053 
1054 	return 0;
1055 
1056 err_unlock_ovs:
1057 	ovs_unlock();
1058 	kfree_skb(reply);
1059 err_kfree_acts:
1060 	kfree(acts);
1061 error:
1062 	return error;
1063 }
1064 
1065 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1066 {
1067 	struct nlattr **a = info->attrs;
1068 	struct ovs_header *ovs_header = info->userhdr;
1069 	struct sw_flow_key key;
1070 	struct sk_buff *reply;
1071 	struct sw_flow *flow;
1072 	struct datapath *dp;
1073 	struct sw_flow_match match;
1074 	int err;
1075 
1076 	if (!a[OVS_FLOW_ATTR_KEY]) {
1077 		OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1078 		return -EINVAL;
1079 	}
1080 
1081 	ovs_match_init(&match, &key, NULL);
1082 	err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1083 	if (err)
1084 		return err;
1085 
1086 	ovs_lock();
1087 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1088 	if (!dp) {
1089 		err = -ENODEV;
1090 		goto unlock;
1091 	}
1092 
1093 	flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1094 	if (!flow) {
1095 		err = -ENOENT;
1096 		goto unlock;
1097 	}
1098 
1099 	reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1100 					OVS_FLOW_CMD_NEW, true);
1101 	if (IS_ERR(reply)) {
1102 		err = PTR_ERR(reply);
1103 		goto unlock;
1104 	}
1105 
1106 	ovs_unlock();
1107 	return genlmsg_reply(reply, info);
1108 unlock:
1109 	ovs_unlock();
1110 	return err;
1111 }
1112 
1113 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1114 {
1115 	struct nlattr **a = info->attrs;
1116 	struct ovs_header *ovs_header = info->userhdr;
1117 	struct sw_flow_key key;
1118 	struct sk_buff *reply;
1119 	struct sw_flow *flow;
1120 	struct datapath *dp;
1121 	struct sw_flow_match match;
1122 	int err;
1123 
1124 	if (likely(a[OVS_FLOW_ATTR_KEY])) {
1125 		ovs_match_init(&match, &key, NULL);
1126 		err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1127 		if (unlikely(err))
1128 			return err;
1129 	}
1130 
1131 	ovs_lock();
1132 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1133 	if (unlikely(!dp)) {
1134 		err = -ENODEV;
1135 		goto unlock;
1136 	}
1137 
1138 	if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1139 		err = ovs_flow_tbl_flush(&dp->table);
1140 		goto unlock;
1141 	}
1142 
1143 	flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1144 	if (unlikely(!flow)) {
1145 		err = -ENOENT;
1146 		goto unlock;
1147 	}
1148 
1149 	ovs_flow_tbl_remove(&dp->table, flow);
1150 	ovs_unlock();
1151 
1152 	reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1153 					info, false);
1154 	if (likely(reply)) {
1155 		if (likely(!IS_ERR(reply))) {
1156 			rcu_read_lock();	/*To keep RCU checker happy. */
1157 			err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1158 						     reply, info->snd_portid,
1159 						     info->snd_seq, 0,
1160 						     OVS_FLOW_CMD_DEL);
1161 			rcu_read_unlock();
1162 			BUG_ON(err < 0);
1163 
1164 			ovs_notify(&dp_flow_genl_family, reply, info);
1165 		} else {
1166 			netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1167 		}
1168 	}
1169 
1170 	ovs_flow_free(flow, true);
1171 	return 0;
1172 unlock:
1173 	ovs_unlock();
1174 	return err;
1175 }
1176 
1177 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1178 {
1179 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1180 	struct table_instance *ti;
1181 	struct datapath *dp;
1182 
1183 	rcu_read_lock();
1184 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1185 	if (!dp) {
1186 		rcu_read_unlock();
1187 		return -ENODEV;
1188 	}
1189 
1190 	ti = rcu_dereference(dp->table.ti);
1191 	for (;;) {
1192 		struct sw_flow *flow;
1193 		u32 bucket, obj;
1194 
1195 		bucket = cb->args[0];
1196 		obj = cb->args[1];
1197 		flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1198 		if (!flow)
1199 			break;
1200 
1201 		if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1202 					   NETLINK_CB(cb->skb).portid,
1203 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
1204 					   OVS_FLOW_CMD_NEW) < 0)
1205 			break;
1206 
1207 		cb->args[0] = bucket;
1208 		cb->args[1] = obj;
1209 	}
1210 	rcu_read_unlock();
1211 	return skb->len;
1212 }
1213 
1214 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1215 	[OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1216 	[OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1217 	[OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1218 };
1219 
1220 static const struct genl_ops dp_flow_genl_ops[] = {
1221 	{ .cmd = OVS_FLOW_CMD_NEW,
1222 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1223 	  .policy = flow_policy,
1224 	  .doit = ovs_flow_cmd_new
1225 	},
1226 	{ .cmd = OVS_FLOW_CMD_DEL,
1227 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1228 	  .policy = flow_policy,
1229 	  .doit = ovs_flow_cmd_del
1230 	},
1231 	{ .cmd = OVS_FLOW_CMD_GET,
1232 	  .flags = 0,		    /* OK for unprivileged users. */
1233 	  .policy = flow_policy,
1234 	  .doit = ovs_flow_cmd_get,
1235 	  .dumpit = ovs_flow_cmd_dump
1236 	},
1237 	{ .cmd = OVS_FLOW_CMD_SET,
1238 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1239 	  .policy = flow_policy,
1240 	  .doit = ovs_flow_cmd_set,
1241 	},
1242 };
1243 
1244 static struct genl_family dp_flow_genl_family = {
1245 	.id = GENL_ID_GENERATE,
1246 	.hdrsize = sizeof(struct ovs_header),
1247 	.name = OVS_FLOW_FAMILY,
1248 	.version = OVS_FLOW_VERSION,
1249 	.maxattr = OVS_FLOW_ATTR_MAX,
1250 	.netnsok = true,
1251 	.parallel_ops = true,
1252 	.ops = dp_flow_genl_ops,
1253 	.n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1254 	.mcgrps = &ovs_dp_flow_multicast_group,
1255 	.n_mcgrps = 1,
1256 };
1257 
1258 static size_t ovs_dp_cmd_msg_size(void)
1259 {
1260 	size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1261 
1262 	msgsize += nla_total_size(IFNAMSIZ);
1263 	msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1264 	msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1265 	msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1266 
1267 	return msgsize;
1268 }
1269 
1270 /* Called with ovs_mutex or RCU read lock. */
1271 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1272 				u32 portid, u32 seq, u32 flags, u8 cmd)
1273 {
1274 	struct ovs_header *ovs_header;
1275 	struct ovs_dp_stats dp_stats;
1276 	struct ovs_dp_megaflow_stats dp_megaflow_stats;
1277 	int err;
1278 
1279 	ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1280 				   flags, cmd);
1281 	if (!ovs_header)
1282 		goto error;
1283 
1284 	ovs_header->dp_ifindex = get_dpifindex(dp);
1285 
1286 	err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1287 	if (err)
1288 		goto nla_put_failure;
1289 
1290 	get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1291 	if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1292 			&dp_stats))
1293 		goto nla_put_failure;
1294 
1295 	if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1296 			sizeof(struct ovs_dp_megaflow_stats),
1297 			&dp_megaflow_stats))
1298 		goto nla_put_failure;
1299 
1300 	if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1301 		goto nla_put_failure;
1302 
1303 	return genlmsg_end(skb, ovs_header);
1304 
1305 nla_put_failure:
1306 	genlmsg_cancel(skb, ovs_header);
1307 error:
1308 	return -EMSGSIZE;
1309 }
1310 
1311 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1312 {
1313 	return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1314 }
1315 
1316 /* Called with rcu_read_lock or ovs_mutex. */
1317 static struct datapath *lookup_datapath(struct net *net,
1318 					struct ovs_header *ovs_header,
1319 					struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1320 {
1321 	struct datapath *dp;
1322 
1323 	if (!a[OVS_DP_ATTR_NAME])
1324 		dp = get_dp(net, ovs_header->dp_ifindex);
1325 	else {
1326 		struct vport *vport;
1327 
1328 		vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1329 		dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1330 	}
1331 	return dp ? dp : ERR_PTR(-ENODEV);
1332 }
1333 
1334 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1335 {
1336 	struct datapath *dp;
1337 
1338 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1339 	if (IS_ERR(dp))
1340 		return;
1341 
1342 	WARN(dp->user_features, "Dropping previously announced user features\n");
1343 	dp->user_features = 0;
1344 }
1345 
1346 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1347 {
1348 	if (a[OVS_DP_ATTR_USER_FEATURES])
1349 		dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1350 }
1351 
1352 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1353 {
1354 	struct nlattr **a = info->attrs;
1355 	struct vport_parms parms;
1356 	struct sk_buff *reply;
1357 	struct datapath *dp;
1358 	struct vport *vport;
1359 	struct ovs_net *ovs_net;
1360 	int err, i;
1361 
1362 	err = -EINVAL;
1363 	if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1364 		goto err;
1365 
1366 	reply = ovs_dp_cmd_alloc_info(info);
1367 	if (!reply)
1368 		return -ENOMEM;
1369 
1370 	err = -ENOMEM;
1371 	dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1372 	if (dp == NULL)
1373 		goto err_free_reply;
1374 
1375 	ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1376 
1377 	/* Allocate table. */
1378 	err = ovs_flow_tbl_init(&dp->table);
1379 	if (err)
1380 		goto err_free_dp;
1381 
1382 	dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1383 	if (!dp->stats_percpu) {
1384 		err = -ENOMEM;
1385 		goto err_destroy_table;
1386 	}
1387 
1388 	dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1389 			    GFP_KERNEL);
1390 	if (!dp->ports) {
1391 		err = -ENOMEM;
1392 		goto err_destroy_percpu;
1393 	}
1394 
1395 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1396 		INIT_HLIST_HEAD(&dp->ports[i]);
1397 
1398 	/* Set up our datapath device. */
1399 	parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1400 	parms.type = OVS_VPORT_TYPE_INTERNAL;
1401 	parms.options = NULL;
1402 	parms.dp = dp;
1403 	parms.port_no = OVSP_LOCAL;
1404 	parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1405 
1406 	ovs_dp_change(dp, a);
1407 
1408 	/* So far only local changes have been made, now need the lock. */
1409 	ovs_lock();
1410 
1411 	vport = new_vport(&parms);
1412 	if (IS_ERR(vport)) {
1413 		err = PTR_ERR(vport);
1414 		if (err == -EBUSY)
1415 			err = -EEXIST;
1416 
1417 		if (err == -EEXIST) {
1418 			/* An outdated user space instance that does not understand
1419 			 * the concept of user_features has attempted to create a new
1420 			 * datapath and is likely to reuse it. Drop all user features.
1421 			 */
1422 			if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1423 				ovs_dp_reset_user_features(skb, info);
1424 		}
1425 
1426 		goto err_destroy_ports_array;
1427 	}
1428 
1429 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1430 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1431 	BUG_ON(err < 0);
1432 
1433 	ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1434 	list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1435 
1436 	ovs_unlock();
1437 
1438 	ovs_notify(&dp_datapath_genl_family, reply, info);
1439 	return 0;
1440 
1441 err_destroy_ports_array:
1442 	ovs_unlock();
1443 	kfree(dp->ports);
1444 err_destroy_percpu:
1445 	free_percpu(dp->stats_percpu);
1446 err_destroy_table:
1447 	ovs_flow_tbl_destroy(&dp->table, false);
1448 err_free_dp:
1449 	release_net(ovs_dp_get_net(dp));
1450 	kfree(dp);
1451 err_free_reply:
1452 	kfree_skb(reply);
1453 err:
1454 	return err;
1455 }
1456 
1457 /* Called with ovs_mutex. */
1458 static void __dp_destroy(struct datapath *dp)
1459 {
1460 	int i;
1461 
1462 	for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1463 		struct vport *vport;
1464 		struct hlist_node *n;
1465 
1466 		hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1467 			if (vport->port_no != OVSP_LOCAL)
1468 				ovs_dp_detach_port(vport);
1469 	}
1470 
1471 	list_del_rcu(&dp->list_node);
1472 
1473 	/* OVSP_LOCAL is datapath internal port. We need to make sure that
1474 	 * all ports in datapath are destroyed first before freeing datapath.
1475 	 */
1476 	ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1477 
1478 	/* RCU destroy the flow table */
1479 	ovs_flow_tbl_destroy(&dp->table, true);
1480 
1481 	call_rcu(&dp->rcu, destroy_dp_rcu);
1482 }
1483 
1484 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1485 {
1486 	struct sk_buff *reply;
1487 	struct datapath *dp;
1488 	int err;
1489 
1490 	reply = ovs_dp_cmd_alloc_info(info);
1491 	if (!reply)
1492 		return -ENOMEM;
1493 
1494 	ovs_lock();
1495 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1496 	err = PTR_ERR(dp);
1497 	if (IS_ERR(dp))
1498 		goto err_unlock_free;
1499 
1500 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1501 				   info->snd_seq, 0, OVS_DP_CMD_DEL);
1502 	BUG_ON(err < 0);
1503 
1504 	__dp_destroy(dp);
1505 	ovs_unlock();
1506 
1507 	ovs_notify(&dp_datapath_genl_family, reply, info);
1508 
1509 	return 0;
1510 
1511 err_unlock_free:
1512 	ovs_unlock();
1513 	kfree_skb(reply);
1514 	return err;
1515 }
1516 
1517 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1518 {
1519 	struct sk_buff *reply;
1520 	struct datapath *dp;
1521 	int err;
1522 
1523 	reply = ovs_dp_cmd_alloc_info(info);
1524 	if (!reply)
1525 		return -ENOMEM;
1526 
1527 	ovs_lock();
1528 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1529 	err = PTR_ERR(dp);
1530 	if (IS_ERR(dp))
1531 		goto err_unlock_free;
1532 
1533 	ovs_dp_change(dp, info->attrs);
1534 
1535 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1536 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1537 	BUG_ON(err < 0);
1538 
1539 	ovs_unlock();
1540 	ovs_notify(&dp_datapath_genl_family, reply, info);
1541 
1542 	return 0;
1543 
1544 err_unlock_free:
1545 	ovs_unlock();
1546 	kfree_skb(reply);
1547 	return err;
1548 }
1549 
1550 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1551 {
1552 	struct sk_buff *reply;
1553 	struct datapath *dp;
1554 	int err;
1555 
1556 	reply = ovs_dp_cmd_alloc_info(info);
1557 	if (!reply)
1558 		return -ENOMEM;
1559 
1560 	rcu_read_lock();
1561 	dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1562 	if (IS_ERR(dp)) {
1563 		err = PTR_ERR(dp);
1564 		goto err_unlock_free;
1565 	}
1566 	err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1567 				   info->snd_seq, 0, OVS_DP_CMD_NEW);
1568 	BUG_ON(err < 0);
1569 	rcu_read_unlock();
1570 
1571 	return genlmsg_reply(reply, info);
1572 
1573 err_unlock_free:
1574 	rcu_read_unlock();
1575 	kfree_skb(reply);
1576 	return err;
1577 }
1578 
1579 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1580 {
1581 	struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1582 	struct datapath *dp;
1583 	int skip = cb->args[0];
1584 	int i = 0;
1585 
1586 	rcu_read_lock();
1587 	list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1588 		if (i >= skip &&
1589 		    ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1590 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1591 					 OVS_DP_CMD_NEW) < 0)
1592 			break;
1593 		i++;
1594 	}
1595 	rcu_read_unlock();
1596 
1597 	cb->args[0] = i;
1598 
1599 	return skb->len;
1600 }
1601 
1602 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1603 	[OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1604 	[OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1605 	[OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1606 };
1607 
1608 static const struct genl_ops dp_datapath_genl_ops[] = {
1609 	{ .cmd = OVS_DP_CMD_NEW,
1610 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1611 	  .policy = datapath_policy,
1612 	  .doit = ovs_dp_cmd_new
1613 	},
1614 	{ .cmd = OVS_DP_CMD_DEL,
1615 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1616 	  .policy = datapath_policy,
1617 	  .doit = ovs_dp_cmd_del
1618 	},
1619 	{ .cmd = OVS_DP_CMD_GET,
1620 	  .flags = 0,		    /* OK for unprivileged users. */
1621 	  .policy = datapath_policy,
1622 	  .doit = ovs_dp_cmd_get,
1623 	  .dumpit = ovs_dp_cmd_dump
1624 	},
1625 	{ .cmd = OVS_DP_CMD_SET,
1626 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1627 	  .policy = datapath_policy,
1628 	  .doit = ovs_dp_cmd_set,
1629 	},
1630 };
1631 
1632 static struct genl_family dp_datapath_genl_family = {
1633 	.id = GENL_ID_GENERATE,
1634 	.hdrsize = sizeof(struct ovs_header),
1635 	.name = OVS_DATAPATH_FAMILY,
1636 	.version = OVS_DATAPATH_VERSION,
1637 	.maxattr = OVS_DP_ATTR_MAX,
1638 	.netnsok = true,
1639 	.parallel_ops = true,
1640 	.ops = dp_datapath_genl_ops,
1641 	.n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1642 	.mcgrps = &ovs_dp_datapath_multicast_group,
1643 	.n_mcgrps = 1,
1644 };
1645 
1646 /* Called with ovs_mutex or RCU read lock. */
1647 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1648 				   u32 portid, u32 seq, u32 flags, u8 cmd)
1649 {
1650 	struct ovs_header *ovs_header;
1651 	struct ovs_vport_stats vport_stats;
1652 	int err;
1653 
1654 	ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1655 				 flags, cmd);
1656 	if (!ovs_header)
1657 		return -EMSGSIZE;
1658 
1659 	ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1660 
1661 	if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1662 	    nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1663 	    nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1664 			   vport->ops->get_name(vport)))
1665 		goto nla_put_failure;
1666 
1667 	ovs_vport_get_stats(vport, &vport_stats);
1668 	if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1669 		    &vport_stats))
1670 		goto nla_put_failure;
1671 
1672 	if (ovs_vport_get_upcall_portids(vport, skb))
1673 		goto nla_put_failure;
1674 
1675 	err = ovs_vport_get_options(vport, skb);
1676 	if (err == -EMSGSIZE)
1677 		goto error;
1678 
1679 	return genlmsg_end(skb, ovs_header);
1680 
1681 nla_put_failure:
1682 	err = -EMSGSIZE;
1683 error:
1684 	genlmsg_cancel(skb, ovs_header);
1685 	return err;
1686 }
1687 
1688 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1689 {
1690 	return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1691 }
1692 
1693 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1694 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1695 					 u32 seq, u8 cmd)
1696 {
1697 	struct sk_buff *skb;
1698 	int retval;
1699 
1700 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1701 	if (!skb)
1702 		return ERR_PTR(-ENOMEM);
1703 
1704 	retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1705 	BUG_ON(retval < 0);
1706 
1707 	return skb;
1708 }
1709 
1710 /* Called with ovs_mutex or RCU read lock. */
1711 static struct vport *lookup_vport(struct net *net,
1712 				  struct ovs_header *ovs_header,
1713 				  struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1714 {
1715 	struct datapath *dp;
1716 	struct vport *vport;
1717 
1718 	if (a[OVS_VPORT_ATTR_NAME]) {
1719 		vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1720 		if (!vport)
1721 			return ERR_PTR(-ENODEV);
1722 		if (ovs_header->dp_ifindex &&
1723 		    ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1724 			return ERR_PTR(-ENODEV);
1725 		return vport;
1726 	} else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1727 		u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1728 
1729 		if (port_no >= DP_MAX_PORTS)
1730 			return ERR_PTR(-EFBIG);
1731 
1732 		dp = get_dp(net, ovs_header->dp_ifindex);
1733 		if (!dp)
1734 			return ERR_PTR(-ENODEV);
1735 
1736 		vport = ovs_vport_ovsl_rcu(dp, port_no);
1737 		if (!vport)
1738 			return ERR_PTR(-ENODEV);
1739 		return vport;
1740 	} else
1741 		return ERR_PTR(-EINVAL);
1742 }
1743 
1744 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1745 {
1746 	struct nlattr **a = info->attrs;
1747 	struct ovs_header *ovs_header = info->userhdr;
1748 	struct vport_parms parms;
1749 	struct sk_buff *reply;
1750 	struct vport *vport;
1751 	struct datapath *dp;
1752 	u32 port_no;
1753 	int err;
1754 
1755 	if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1756 	    !a[OVS_VPORT_ATTR_UPCALL_PID])
1757 		return -EINVAL;
1758 
1759 	port_no = a[OVS_VPORT_ATTR_PORT_NO]
1760 		? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1761 	if (port_no >= DP_MAX_PORTS)
1762 		return -EFBIG;
1763 
1764 	reply = ovs_vport_cmd_alloc_info();
1765 	if (!reply)
1766 		return -ENOMEM;
1767 
1768 	ovs_lock();
1769 restart:
1770 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1771 	err = -ENODEV;
1772 	if (!dp)
1773 		goto exit_unlock_free;
1774 
1775 	if (port_no) {
1776 		vport = ovs_vport_ovsl(dp, port_no);
1777 		err = -EBUSY;
1778 		if (vport)
1779 			goto exit_unlock_free;
1780 	} else {
1781 		for (port_no = 1; ; port_no++) {
1782 			if (port_no >= DP_MAX_PORTS) {
1783 				err = -EFBIG;
1784 				goto exit_unlock_free;
1785 			}
1786 			vport = ovs_vport_ovsl(dp, port_no);
1787 			if (!vport)
1788 				break;
1789 		}
1790 	}
1791 
1792 	parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1793 	parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1794 	parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1795 	parms.dp = dp;
1796 	parms.port_no = port_no;
1797 	parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1798 
1799 	vport = new_vport(&parms);
1800 	err = PTR_ERR(vport);
1801 	if (IS_ERR(vport)) {
1802 		if (err == -EAGAIN)
1803 			goto restart;
1804 		goto exit_unlock_free;
1805 	}
1806 
1807 	err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1808 				      info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1809 	BUG_ON(err < 0);
1810 	ovs_unlock();
1811 
1812 	ovs_notify(&dp_vport_genl_family, reply, info);
1813 	return 0;
1814 
1815 exit_unlock_free:
1816 	ovs_unlock();
1817 	kfree_skb(reply);
1818 	return err;
1819 }
1820 
1821 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1822 {
1823 	struct nlattr **a = info->attrs;
1824 	struct sk_buff *reply;
1825 	struct vport *vport;
1826 	int err;
1827 
1828 	reply = ovs_vport_cmd_alloc_info();
1829 	if (!reply)
1830 		return -ENOMEM;
1831 
1832 	ovs_lock();
1833 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1834 	err = PTR_ERR(vport);
1835 	if (IS_ERR(vport))
1836 		goto exit_unlock_free;
1837 
1838 	if (a[OVS_VPORT_ATTR_TYPE] &&
1839 	    nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1840 		err = -EINVAL;
1841 		goto exit_unlock_free;
1842 	}
1843 
1844 	if (a[OVS_VPORT_ATTR_OPTIONS]) {
1845 		err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1846 		if (err)
1847 			goto exit_unlock_free;
1848 	}
1849 
1850 
1851 	if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1852 		struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1853 
1854 		err = ovs_vport_set_upcall_portids(vport, ids);
1855 		if (err)
1856 			goto exit_unlock_free;
1857 	}
1858 
1859 	err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1860 				      info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1861 	BUG_ON(err < 0);
1862 
1863 	ovs_unlock();
1864 	ovs_notify(&dp_vport_genl_family, reply, info);
1865 	return 0;
1866 
1867 exit_unlock_free:
1868 	ovs_unlock();
1869 	kfree_skb(reply);
1870 	return err;
1871 }
1872 
1873 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1874 {
1875 	struct nlattr **a = info->attrs;
1876 	struct sk_buff *reply;
1877 	struct vport *vport;
1878 	int err;
1879 
1880 	reply = ovs_vport_cmd_alloc_info();
1881 	if (!reply)
1882 		return -ENOMEM;
1883 
1884 	ovs_lock();
1885 	vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1886 	err = PTR_ERR(vport);
1887 	if (IS_ERR(vport))
1888 		goto exit_unlock_free;
1889 
1890 	if (vport->port_no == OVSP_LOCAL) {
1891 		err = -EINVAL;
1892 		goto exit_unlock_free;
1893 	}
1894 
1895 	err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1896 				      info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1897 	BUG_ON(err < 0);
1898 	ovs_dp_detach_port(vport);
1899 	ovs_unlock();
1900 
1901 	ovs_notify(&dp_vport_genl_family, reply, info);
1902 	return 0;
1903 
1904 exit_unlock_free:
1905 	ovs_unlock();
1906 	kfree_skb(reply);
1907 	return err;
1908 }
1909 
1910 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1911 {
1912 	struct nlattr **a = info->attrs;
1913 	struct ovs_header *ovs_header = info->userhdr;
1914 	struct sk_buff *reply;
1915 	struct vport *vport;
1916 	int err;
1917 
1918 	reply = ovs_vport_cmd_alloc_info();
1919 	if (!reply)
1920 		return -ENOMEM;
1921 
1922 	rcu_read_lock();
1923 	vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1924 	err = PTR_ERR(vport);
1925 	if (IS_ERR(vport))
1926 		goto exit_unlock_free;
1927 	err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1928 				      info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1929 	BUG_ON(err < 0);
1930 	rcu_read_unlock();
1931 
1932 	return genlmsg_reply(reply, info);
1933 
1934 exit_unlock_free:
1935 	rcu_read_unlock();
1936 	kfree_skb(reply);
1937 	return err;
1938 }
1939 
1940 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1941 {
1942 	struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1943 	struct datapath *dp;
1944 	int bucket = cb->args[0], skip = cb->args[1];
1945 	int i, j = 0;
1946 
1947 	rcu_read_lock();
1948 	dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1949 	if (!dp) {
1950 		rcu_read_unlock();
1951 		return -ENODEV;
1952 	}
1953 	for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1954 		struct vport *vport;
1955 
1956 		j = 0;
1957 		hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1958 			if (j >= skip &&
1959 			    ovs_vport_cmd_fill_info(vport, skb,
1960 						    NETLINK_CB(cb->skb).portid,
1961 						    cb->nlh->nlmsg_seq,
1962 						    NLM_F_MULTI,
1963 						    OVS_VPORT_CMD_NEW) < 0)
1964 				goto out;
1965 
1966 			j++;
1967 		}
1968 		skip = 0;
1969 	}
1970 out:
1971 	rcu_read_unlock();
1972 
1973 	cb->args[0] = i;
1974 	cb->args[1] = j;
1975 
1976 	return skb->len;
1977 }
1978 
1979 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1980 	[OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1981 	[OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1982 	[OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1983 	[OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1984 	[OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1985 	[OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1986 };
1987 
1988 static const struct genl_ops dp_vport_genl_ops[] = {
1989 	{ .cmd = OVS_VPORT_CMD_NEW,
1990 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1991 	  .policy = vport_policy,
1992 	  .doit = ovs_vport_cmd_new
1993 	},
1994 	{ .cmd = OVS_VPORT_CMD_DEL,
1995 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1996 	  .policy = vport_policy,
1997 	  .doit = ovs_vport_cmd_del
1998 	},
1999 	{ .cmd = OVS_VPORT_CMD_GET,
2000 	  .flags = 0,		    /* OK for unprivileged users. */
2001 	  .policy = vport_policy,
2002 	  .doit = ovs_vport_cmd_get,
2003 	  .dumpit = ovs_vport_cmd_dump
2004 	},
2005 	{ .cmd = OVS_VPORT_CMD_SET,
2006 	  .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2007 	  .policy = vport_policy,
2008 	  .doit = ovs_vport_cmd_set,
2009 	},
2010 };
2011 
2012 struct genl_family dp_vport_genl_family = {
2013 	.id = GENL_ID_GENERATE,
2014 	.hdrsize = sizeof(struct ovs_header),
2015 	.name = OVS_VPORT_FAMILY,
2016 	.version = OVS_VPORT_VERSION,
2017 	.maxattr = OVS_VPORT_ATTR_MAX,
2018 	.netnsok = true,
2019 	.parallel_ops = true,
2020 	.ops = dp_vport_genl_ops,
2021 	.n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2022 	.mcgrps = &ovs_dp_vport_multicast_group,
2023 	.n_mcgrps = 1,
2024 };
2025 
2026 static struct genl_family * const dp_genl_families[] = {
2027 	&dp_datapath_genl_family,
2028 	&dp_vport_genl_family,
2029 	&dp_flow_genl_family,
2030 	&dp_packet_genl_family,
2031 };
2032 
2033 static void dp_unregister_genl(int n_families)
2034 {
2035 	int i;
2036 
2037 	for (i = 0; i < n_families; i++)
2038 		genl_unregister_family(dp_genl_families[i]);
2039 }
2040 
2041 static int dp_register_genl(void)
2042 {
2043 	int err;
2044 	int i;
2045 
2046 	for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2047 
2048 		err = genl_register_family(dp_genl_families[i]);
2049 		if (err)
2050 			goto error;
2051 	}
2052 
2053 	return 0;
2054 
2055 error:
2056 	dp_unregister_genl(i);
2057 	return err;
2058 }
2059 
2060 static int __net_init ovs_init_net(struct net *net)
2061 {
2062 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2063 
2064 	INIT_LIST_HEAD(&ovs_net->dps);
2065 	INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2066 	return 0;
2067 }
2068 
2069 static void __net_exit ovs_exit_net(struct net *net)
2070 {
2071 	struct datapath *dp, *dp_next;
2072 	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2073 
2074 	ovs_lock();
2075 	list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2076 		__dp_destroy(dp);
2077 	ovs_unlock();
2078 
2079 	cancel_work_sync(&ovs_net->dp_notify_work);
2080 }
2081 
2082 static struct pernet_operations ovs_net_ops = {
2083 	.init = ovs_init_net,
2084 	.exit = ovs_exit_net,
2085 	.id   = &ovs_net_id,
2086 	.size = sizeof(struct ovs_net),
2087 };
2088 
2089 static int __init dp_init(void)
2090 {
2091 	int err;
2092 
2093 	BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2094 
2095 	pr_info("Open vSwitch switching datapath\n");
2096 
2097 	err = action_fifos_init();
2098 	if (err)
2099 		goto error;
2100 
2101 	err = ovs_internal_dev_rtnl_link_register();
2102 	if (err)
2103 		goto error_action_fifos_exit;
2104 
2105 	err = ovs_flow_init();
2106 	if (err)
2107 		goto error_unreg_rtnl_link;
2108 
2109 	err = ovs_vport_init();
2110 	if (err)
2111 		goto error_flow_exit;
2112 
2113 	err = register_pernet_device(&ovs_net_ops);
2114 	if (err)
2115 		goto error_vport_exit;
2116 
2117 	err = register_netdevice_notifier(&ovs_dp_device_notifier);
2118 	if (err)
2119 		goto error_netns_exit;
2120 
2121 	err = ovs_netdev_init();
2122 	if (err)
2123 		goto error_unreg_notifier;
2124 
2125 	err = dp_register_genl();
2126 	if (err < 0)
2127 		goto error_unreg_netdev;
2128 
2129 	return 0;
2130 
2131 error_unreg_netdev:
2132 	ovs_netdev_exit();
2133 error_unreg_notifier:
2134 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2135 error_netns_exit:
2136 	unregister_pernet_device(&ovs_net_ops);
2137 error_vport_exit:
2138 	ovs_vport_exit();
2139 error_flow_exit:
2140 	ovs_flow_exit();
2141 error_unreg_rtnl_link:
2142 	ovs_internal_dev_rtnl_link_unregister();
2143 error_action_fifos_exit:
2144 	action_fifos_exit();
2145 error:
2146 	return err;
2147 }
2148 
2149 static void dp_cleanup(void)
2150 {
2151 	dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2152 	ovs_netdev_exit();
2153 	unregister_netdevice_notifier(&ovs_dp_device_notifier);
2154 	unregister_pernet_device(&ovs_net_ops);
2155 	rcu_barrier();
2156 	ovs_vport_exit();
2157 	ovs_flow_exit();
2158 	ovs_internal_dev_rtnl_link_unregister();
2159 	action_fifos_exit();
2160 }
2161 
2162 module_init(dp_init);
2163 module_exit(dp_cleanup);
2164 
2165 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2166 MODULE_LICENSE("GPL");
2167