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