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