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