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