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