datapath: Add genl_dereference() and use it.
[sliver-openvswitch.git] / datapath / datapath.c
1 /*
2  * Copyright (c) 2007-2011 Nicira Networks.
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/system.h>
43 #include <asm/div64.h>
44 #include <linux/highmem.h>
45 #include <linux/netfilter_bridge.h>
46 #include <linux/netfilter_ipv4.h>
47 #include <linux/inetdevice.h>
48 #include <linux/list.h>
49 #include <linux/openvswitch.h>
50 #include <linux/rculist.h>
51 #include <linux/dmi.h>
52 #include <net/genetlink.h>
53
54 #include "checksum.h"
55 #include "datapath.h"
56 #include "flow.h"
57 #include "vlan.h"
58 #include "tunnel.h"
59 #include "vport-internal_dev.h"
60
61 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
62     LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
63 #error Kernels before 2.6.18 or after 3.2 are not supported by this version of Open vSwitch.
64 #endif
65
66 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
67 EXPORT_SYMBOL(dp_ioctl_hook);
68
69 /**
70  * DOC: Locking:
71  *
72  * Writes to device state (add/remove datapath, port, set operations on vports,
73  * etc.) are protected by RTNL.
74  *
75  * Writes to other state (flow table modifications, set miscellaneous datapath
76  * parameters, etc.) are protected by genl_mutex.  The RTNL lock nests inside
77  * genl_mutex.
78  *
79  * Reads are protected by RCU.
80  *
81  * There are a few special cases (mostly stats) that have their own
82  * synchronization but they nest under all of above and don't interact with
83  * each other.
84  */
85
86 /* Global list of datapaths to enable dumping them all out.
87  * Protected by genl_mutex.
88  */
89 static LIST_HEAD(dps);
90
91 static struct vport *new_vport(const struct vport_parms *);
92 static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
93                              const struct dp_upcall_info *);
94 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
95                                   const struct dp_upcall_info *);
96
97 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
98 static struct datapath *get_dp(int dp_ifindex)
99 {
100         struct datapath *dp = NULL;
101         struct net_device *dev;
102
103         rcu_read_lock();
104         dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
105         if (dev) {
106                 struct vport *vport = internal_dev_get_vport(dev);
107                 if (vport)
108                         dp = vport->dp;
109         }
110         rcu_read_unlock();
111
112         return dp;
113 }
114
115 /* Must be called with rcu_read_lock or RTNL lock. */
116 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
117 {
118         return rcu_dereference_rtnl(dp->ports[port_no]);
119 }
120
121 /* Must be called with rcu_read_lock or RTNL lock. */
122 const char *dp_name(const struct datapath *dp)
123 {
124         struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
125         return vport->ops->get_name(vport);
126 }
127
128 static int get_dpifindex(struct datapath *dp)
129 {
130         struct vport *local;
131         int ifindex;
132
133         rcu_read_lock();
134
135         local = get_vport_protected(dp, OVSP_LOCAL);
136         if (local)
137                 ifindex = local->ops->get_ifindex(local);
138         else
139                 ifindex = 0;
140
141         rcu_read_unlock();
142
143         return ifindex;
144 }
145
146 static size_t br_nlmsg_size(void)
147 {
148         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
149                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
150                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
151                + nla_total_size(4) /* IFLA_MASTER */
152                + nla_total_size(4) /* IFLA_MTU */
153                + nla_total_size(1); /* IFLA_OPERSTATE */
154 }
155
156 /* Caller must hold RTNL lock. */
157 static int dp_fill_ifinfo(struct sk_buff *skb,
158                           const struct vport *port,
159                           int event, unsigned int flags)
160 {
161         struct datapath *dp = port->dp;
162         struct ifinfomsg *hdr;
163         struct nlmsghdr *nlh;
164
165         if (!port->ops->get_ifindex)
166                 return -ENODEV;
167
168         nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
169         if (nlh == NULL)
170                 return -EMSGSIZE;
171
172         hdr = nlmsg_data(nlh);
173         hdr->ifi_family = AF_BRIDGE;
174         hdr->__ifi_pad = 0;
175         hdr->ifi_type = ARPHRD_ETHER;
176         hdr->ifi_index = port->ops->get_ifindex(port);
177         hdr->ifi_flags = port->ops->get_dev_flags(port);
178         hdr->ifi_change = 0;
179
180         NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
181         NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
182         NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
183 #ifdef IFLA_OPERSTATE
184         NLA_PUT_U8(skb, IFLA_OPERSTATE,
185                    port->ops->is_running(port)
186                         ? port->ops->get_operstate(port)
187                         : IF_OPER_DOWN);
188 #endif
189
190         NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
191
192         return nlmsg_end(skb, nlh);
193
194 nla_put_failure:
195         nlmsg_cancel(skb, nlh);
196         return -EMSGSIZE;
197 }
198
199 /* Caller must hold RTNL lock. */
200 static void dp_ifinfo_notify(int event, struct vport *port)
201 {
202         struct sk_buff *skb;
203         int err;
204
205         skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
206         if (!skb) {
207                 err = -ENOBUFS;
208                 goto err;
209         }
210
211         err = dp_fill_ifinfo(skb, port, event, 0);
212         if (err < 0) {
213                 if (err == -ENODEV) {
214                         goto out;
215                 } else {
216                         /* -EMSGSIZE implies BUG in br_nlmsg_size() */
217                         WARN_ON(err == -EMSGSIZE);
218                         goto err;
219                 }
220         }
221
222         rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
223
224         return;
225 err:
226         rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
227 out:
228         kfree_skb(skb);
229 }
230
231 static void release_dp(struct kobject *kobj)
232 {
233         struct datapath *dp = container_of(kobj, struct datapath, ifobj);
234         kfree(dp);
235 }
236
237 static struct kobj_type dp_ktype = {
238         .release = release_dp
239 };
240
241 static void destroy_dp_rcu(struct rcu_head *rcu)
242 {
243         struct datapath *dp = container_of(rcu, struct datapath, rcu);
244
245         flow_tbl_destroy((__force struct flow_table *)dp->table);
246         free_percpu(dp->stats_percpu);
247         kobject_put(&dp->ifobj);
248 }
249
250 /* Called with RTNL lock and genl_lock. */
251 static struct vport *new_vport(const struct vport_parms *parms)
252 {
253         struct vport *vport;
254
255         vport = vport_add(parms);
256         if (!IS_ERR(vport)) {
257                 struct datapath *dp = parms->dp;
258
259                 rcu_assign_pointer(dp->ports[parms->port_no], vport);
260                 list_add(&vport->node, &dp->port_list);
261
262                 dp_ifinfo_notify(RTM_NEWLINK, vport);
263         }
264
265         return vport;
266 }
267
268 /* Called with RTNL lock. */
269 void dp_detach_port(struct vport *p)
270 {
271         ASSERT_RTNL();
272
273         if (p->port_no != OVSP_LOCAL)
274                 dp_sysfs_del_if(p);
275         dp_ifinfo_notify(RTM_DELLINK, p);
276
277         /* First drop references to device. */
278         list_del(&p->node);
279         rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
280
281         /* Then destroy it. */
282         vport_del(p);
283 }
284
285 /* Must be called with rcu_read_lock. */
286 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
287 {
288         struct datapath *dp = p->dp;
289         struct sw_flow *flow;
290         struct dp_stats_percpu *stats;
291         u64 *stats_counter;
292         int error;
293
294         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
295
296         if (!OVS_CB(skb)->flow) {
297                 struct sw_flow_key key;
298                 int key_len;
299
300                 /* Extract flow from 'skb' into 'key'. */
301                 error = flow_extract(skb, p->port_no, &key, &key_len);
302                 if (unlikely(error)) {
303                         kfree_skb(skb);
304                         return;
305                 }
306
307                 /* Look up flow. */
308                 flow = flow_tbl_lookup(rcu_dereference(dp->table),
309                                        &key, key_len);
310                 if (unlikely(!flow)) {
311                         struct dp_upcall_info upcall;
312
313                         upcall.cmd = OVS_PACKET_CMD_MISS;
314                         upcall.key = &key;
315                         upcall.userdata = NULL;
316                         upcall.pid = p->upcall_pid;
317                         dp_upcall(dp, skb, &upcall);
318                         consume_skb(skb);
319                         stats_counter = &stats->n_missed;
320                         goto out;
321                 }
322
323                 OVS_CB(skb)->flow = flow;
324         }
325
326         stats_counter = &stats->n_hit;
327         flow_used(OVS_CB(skb)->flow, skb);
328         execute_actions(dp, skb);
329
330 out:
331         /* Update datapath statistics. */
332         u64_stats_update_begin(&stats->sync);
333         (*stats_counter)++;
334         u64_stats_update_end(&stats->sync);
335 }
336
337 static struct genl_family dp_packet_genl_family = {
338         .id = GENL_ID_GENERATE,
339         .hdrsize = sizeof(struct ovs_header),
340         .name = OVS_PACKET_FAMILY,
341         .version = OVS_PACKET_VERSION,
342         .maxattr = OVS_PACKET_ATTR_MAX
343 };
344
345 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
346               const struct dp_upcall_info *upcall_info)
347 {
348         struct dp_stats_percpu *stats;
349         int dp_ifindex;
350         int err;
351
352         if (upcall_info->pid == 0) {
353                 err = -ENOTCONN;
354                 goto err;
355         }
356
357         dp_ifindex = get_dpifindex(dp);
358         if (!dp_ifindex) {
359                 err = -ENODEV;
360                 goto err;
361         }
362
363         forward_ip_summed(skb, true);
364
365         if (!skb_is_gso(skb))
366                 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
367         else
368                 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
369         if (err)
370                 goto err;
371
372         return 0;
373
374 err:
375         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
376
377         u64_stats_update_begin(&stats->sync);
378         stats->n_lost++;
379         u64_stats_update_end(&stats->sync);
380
381         return err;
382 }
383
384 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
385                              const struct dp_upcall_info *upcall_info)
386 {
387         struct dp_upcall_info later_info;
388         struct sw_flow_key later_key;
389         struct sk_buff *segs, *nskb;
390         int err;
391
392         segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
393         if (IS_ERR(skb))
394                 return PTR_ERR(skb);
395
396         /* Queue all of the segments. */
397         skb = segs;
398         do {
399                 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
400                 if (err)
401                         break;
402
403                 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
404                         /* The initial flow key extracted by flow_extract() in
405                          * this case is for a first fragment, so we need to
406                          * properly mark later fragments.
407                          */
408                         later_key = *upcall_info->key;
409                         later_key.ip.frag = OVS_FRAG_TYPE_LATER;
410
411                         later_info = *upcall_info;
412                         later_info.key = &later_key;
413                         upcall_info = &later_info;
414                 }
415         } while ((skb = skb->next));
416
417         /* Free all of the segments. */
418         skb = segs;
419         do {
420                 nskb = skb->next;
421                 if (err)
422                         kfree_skb(skb);
423                 else
424                         consume_skb(skb);
425         } while ((skb = nskb));
426         return err;
427 }
428
429 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
430                                   const struct dp_upcall_info *upcall_info)
431 {
432         struct ovs_header *upcall;
433         struct sk_buff *nskb = NULL;
434         struct sk_buff *user_skb; /* to be queued to userspace */
435         struct nlattr *nla;
436         unsigned int len;
437         int err;
438
439         if (vlan_tx_tag_present(skb)) {
440                 nskb = skb_clone(skb, GFP_ATOMIC);
441                 if (!nskb)
442                         return -ENOMEM;
443                 
444                 err = vlan_deaccel_tag(nskb);
445                 if (err)
446                         return err;
447
448                 skb = nskb;
449         }
450
451         if (nla_attr_size(skb->len) > USHRT_MAX) {
452                 err = -EFBIG;
453                 goto out;
454         }
455
456         len = sizeof(struct ovs_header);
457         len += nla_total_size(skb->len);
458         len += nla_total_size(FLOW_BUFSIZE);
459         if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
460                 len += nla_total_size(8);
461
462         user_skb = genlmsg_new(len, GFP_ATOMIC);
463         if (!user_skb) {
464                 err = -ENOMEM;
465                 goto out;
466         }
467
468         upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
469                              0, upcall_info->cmd);
470         upcall->dp_ifindex = dp_ifindex;
471
472         nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
473         flow_to_nlattrs(upcall_info->key, user_skb);
474         nla_nest_end(user_skb, nla);
475
476         if (upcall_info->userdata)
477                 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
478                             nla_get_u64(upcall_info->userdata));
479
480         nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
481
482         skb_copy_and_csum_dev(skb, nla_data(nla));
483
484         err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
485
486 out:
487         kfree_skb(nskb);
488         return err;
489 }
490
491 /* Called with genl_mutex. */
492 static int flush_flows(int dp_ifindex)
493 {
494         struct flow_table *old_table;
495         struct flow_table *new_table;
496         struct datapath *dp;
497
498         dp = get_dp(dp_ifindex);
499         if (!dp)
500                 return -ENODEV;
501
502         old_table = genl_dereference(dp->table);
503         new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
504         if (!new_table)
505                 return -ENOMEM;
506
507         rcu_assign_pointer(dp->table, new_table);
508
509         flow_tbl_deferred_destroy(old_table);
510         return 0;
511 }
512
513 static int validate_actions(const struct nlattr *attr,
514                                 const struct sw_flow_key *key, int depth);
515
516 static int validate_sample(const struct nlattr *attr,
517                                 const struct sw_flow_key *key, int depth)
518 {
519         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
520         const struct nlattr *probability, *actions;
521         const struct nlattr *a;
522         int rem;
523
524         memset(attrs, 0, sizeof(attrs));
525         nla_for_each_nested(a, attr, rem) {
526                 int type = nla_type(a);
527                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
528                         return -EINVAL;
529                 attrs[type] = a;
530         }
531         if (rem)
532                 return -EINVAL;
533
534         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
535         if (!probability || nla_len(probability) != sizeof(u32))
536                 return -EINVAL;
537
538         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
539         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
540                 return -EINVAL;
541         return validate_actions(actions, key, depth + 1);
542 }
543
544 static int validate_set(const struct nlattr *a,
545                         const struct sw_flow_key *flow_key)
546 {
547         const struct nlattr *ovs_key = nla_data(a);
548         int key_type = nla_type(ovs_key);
549
550         /* There can be only one key in a action */
551         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
552                 return -EINVAL;
553
554         if (key_type > OVS_KEY_ATTR_MAX ||
555             nla_len(ovs_key) != ovs_key_lens[key_type])
556                 return -EINVAL;
557
558         switch (key_type) {
559         const struct ovs_key_ipv4 *ipv4_key;
560
561         case OVS_KEY_ATTR_PRIORITY:
562         case OVS_KEY_ATTR_TUN_ID:
563         case OVS_KEY_ATTR_ETHERNET:
564                 break;
565
566         case OVS_KEY_ATTR_IPV4:
567                 if (flow_key->eth.type != htons(ETH_P_IP))
568                         return -EINVAL;
569
570                 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
571                         return -EINVAL;
572
573                 ipv4_key = nla_data(ovs_key);
574                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
575                         return -EINVAL;
576
577                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
578                         return -EINVAL;
579
580                 break;
581
582         case OVS_KEY_ATTR_TCP:
583                 if (flow_key->ip.proto != IPPROTO_TCP)
584                         return -EINVAL;
585
586                 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
587                         return -EINVAL;
588
589                 break;
590
591         case OVS_KEY_ATTR_UDP:
592                 if (flow_key->ip.proto != IPPROTO_UDP)
593                         return -EINVAL;
594
595                 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
596                         return -EINVAL;
597                 break;
598
599         default:
600                 return -EINVAL;
601         }
602
603         return 0;
604 }
605
606 static int validate_userspace(const struct nlattr *attr)
607 {
608         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
609                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
610                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
611         };
612         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
613         int error;
614
615         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
616                                  attr, userspace_policy);
617         if (error)
618                 return error;
619
620         if (!a[OVS_USERSPACE_ATTR_PID] ||
621             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
622                 return -EINVAL;
623
624         return 0;
625 }
626
627 static int validate_actions(const struct nlattr *attr,
628                                 const struct sw_flow_key *key,  int depth)
629 {
630         const struct nlattr *a;
631         int rem, err;
632
633         if (depth >= SAMPLE_ACTION_DEPTH)
634                 return -EOVERFLOW;
635
636         nla_for_each_nested(a, attr, rem) {
637                 /* Expected argument lengths, (u32)-1 for variable length. */
638                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
639                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
640                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
641                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
642                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
643                         [OVS_ACTION_ATTR_SET] = (u32)-1,
644                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
645                 };
646                 const struct ovs_action_push_vlan *vlan;
647                 int type = nla_type(a);
648
649                 if (type > OVS_ACTION_ATTR_MAX ||
650                     (action_lens[type] != nla_len(a) &&
651                      action_lens[type] != (u32)-1))
652                         return -EINVAL;
653
654                 switch (type) {
655                 case OVS_ACTION_ATTR_UNSPEC:
656                         return -EINVAL;
657
658                 case OVS_ACTION_ATTR_USERSPACE:
659                         err = validate_userspace(a);
660                         if (err)
661                                 return err;
662                         break;
663
664                 case OVS_ACTION_ATTR_OUTPUT:
665                         if (nla_get_u32(a) >= DP_MAX_PORTS)
666                                 return -EINVAL;
667                         break;
668
669
670                 case OVS_ACTION_ATTR_POP_VLAN:
671                         break;
672
673                 case OVS_ACTION_ATTR_PUSH_VLAN:
674                         vlan = nla_data(a);
675                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
676                                 return -EINVAL;
677                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
678                                 return -EINVAL;
679                         break;
680
681                 case OVS_ACTION_ATTR_SET:
682                         err = validate_set(a, key);
683                         if (err)
684                                 return err;
685                         break;
686
687                 case OVS_ACTION_ATTR_SAMPLE:
688                         err = validate_sample(a, key, depth);
689                         if (err)
690                                 return err;
691                         break;
692
693                 default:
694                         return -EINVAL;
695                 }
696         }
697
698         if (rem > 0)
699                 return -EINVAL;
700
701         return 0;
702 }
703
704 static void clear_stats(struct sw_flow *flow)
705 {
706         flow->used = 0;
707         flow->tcp_flags = 0;
708         flow->packet_count = 0;
709         flow->byte_count = 0;
710 }
711
712 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
713 {
714         struct ovs_header *ovs_header = info->userhdr;
715         struct nlattr **a = info->attrs;
716         struct sw_flow_actions *acts;
717         struct sk_buff *packet;
718         struct sw_flow *flow;
719         struct datapath *dp;
720         struct ethhdr *eth;
721         int len;
722         int err;
723         int key_len;
724
725         err = -EINVAL;
726         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
727             !a[OVS_PACKET_ATTR_ACTIONS] ||
728             nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
729                 goto err;
730
731         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
732         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
733         err = -ENOMEM;
734         if (!packet)
735                 goto err;
736         skb_reserve(packet, NET_IP_ALIGN);
737
738         memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
739
740         skb_reset_mac_header(packet);
741         eth = eth_hdr(packet);
742
743         /* Normally, setting the skb 'protocol' field would be handled by a
744          * call to eth_type_trans(), but it assumes there's a sending
745          * device, which we may not have. */
746         if (ntohs(eth->h_proto) >= 1536)
747                 packet->protocol = eth->h_proto;
748         else
749                 packet->protocol = htons(ETH_P_802_2);
750
751         /* Build an sw_flow for sending this packet. */
752         flow = flow_alloc();
753         err = PTR_ERR(flow);
754         if (IS_ERR(flow))
755                 goto err_kfree_skb;
756
757         err = flow_extract(packet, -1, &flow->key, &key_len);
758         if (err)
759                 goto err_flow_put;
760
761         err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
762                                          &flow->key.phy.in_port,
763                                          &flow->key.phy.tun_id,
764                                          a[OVS_PACKET_ATTR_KEY]);
765         if (err)
766                 goto err_flow_put;
767
768         err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
769         if (err)
770                 goto err_flow_put;
771
772         flow->hash = flow_hash(&flow->key, key_len);
773
774         acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
775         err = PTR_ERR(acts);
776         if (IS_ERR(acts))
777                 goto err_flow_put;
778         rcu_assign_pointer(flow->sf_acts, acts);
779
780         OVS_CB(packet)->flow = flow;
781         packet->priority = flow->key.phy.priority;
782
783         rcu_read_lock();
784         dp = get_dp(ovs_header->dp_ifindex);
785         err = -ENODEV;
786         if (!dp)
787                 goto err_unlock;
788
789         local_bh_disable();
790         err = execute_actions(dp, packet);
791         local_bh_enable();
792         rcu_read_unlock();
793
794         flow_put(flow);
795         return err;
796
797 err_unlock:
798         rcu_read_unlock();
799 err_flow_put:
800         flow_put(flow);
801 err_kfree_skb:
802         kfree_skb(packet);
803 err:
804         return err;
805 }
806
807 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
808         [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
809         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
810         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
811 };
812
813 static struct genl_ops dp_packet_genl_ops[] = {
814         { .cmd = OVS_PACKET_CMD_EXECUTE,
815           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
816           .policy = packet_policy,
817           .doit = ovs_packet_cmd_execute
818         }
819 };
820
821 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
822 {
823         int i;
824         struct flow_table *table = genl_dereference(dp->table);
825
826         stats->n_flows = flow_tbl_count(table);
827
828         stats->n_hit = stats->n_missed = stats->n_lost = 0;
829         for_each_possible_cpu(i) {
830                 const struct dp_stats_percpu *percpu_stats;
831                 struct dp_stats_percpu local_stats;
832                 unsigned int start;
833
834                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
835
836                 do {
837                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
838                         local_stats = *percpu_stats;
839                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
840
841                 stats->n_hit += local_stats.n_hit;
842                 stats->n_missed += local_stats.n_missed;
843                 stats->n_lost += local_stats.n_lost;
844         }
845 }
846
847 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
848         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
849         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
850         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
851 };
852
853 static struct genl_family dp_flow_genl_family = {
854         .id = GENL_ID_GENERATE,
855         .hdrsize = sizeof(struct ovs_header),
856         .name = OVS_FLOW_FAMILY,
857         .version = OVS_FLOW_VERSION,
858         .maxattr = OVS_FLOW_ATTR_MAX
859 };
860
861 static struct genl_multicast_group dp_flow_multicast_group = {
862         .name = OVS_FLOW_MCGROUP
863 };
864
865 /* Called with genl_lock. */
866 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
867                                   struct sk_buff *skb, u32 pid,
868                                   u32 seq, u32 flags, u8 cmd)
869 {
870         const int skb_orig_len = skb->len;
871         const struct sw_flow_actions *sf_acts;
872         struct ovs_flow_stats stats;
873         struct ovs_header *ovs_header;
874         struct nlattr *nla;
875         unsigned long used;
876         u8 tcp_flags;
877         int err;
878
879         sf_acts = rcu_dereference_protected(flow->sf_acts,
880                                             lockdep_genl_is_held());
881
882         ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
883         if (!ovs_header)
884                 return -EMSGSIZE;
885
886         ovs_header->dp_ifindex = get_dpifindex(dp);
887
888         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
889         if (!nla)
890                 goto nla_put_failure;
891         err = flow_to_nlattrs(&flow->key, skb);
892         if (err)
893                 goto error;
894         nla_nest_end(skb, nla);
895
896         spin_lock_bh(&flow->lock);
897         used = flow->used;
898         stats.n_packets = flow->packet_count;
899         stats.n_bytes = flow->byte_count;
900         tcp_flags = flow->tcp_flags;
901         spin_unlock_bh(&flow->lock);
902
903         if (used)
904                 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
905
906         if (stats.n_packets)
907                 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
908                         sizeof(struct ovs_flow_stats), &stats);
909
910         if (tcp_flags)
911                 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
912
913         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
914          * this is the first flow to be dumped into 'skb'.  This is unusual for
915          * Netlink but individual action lists can be longer than
916          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
917          * The userspace caller can always fetch the actions separately if it
918          * really wants them.  (Most userspace callers in fact don't care.)
919          *
920          * This can only fail for dump operations because the skb is always
921          * properly sized for single flows.
922          */
923         err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
924                       sf_acts->actions);
925         if (err < 0 && skb_orig_len)
926                 goto error;
927
928         return genlmsg_end(skb, ovs_header);
929
930 nla_put_failure:
931         err = -EMSGSIZE;
932 error:
933         genlmsg_cancel(skb, ovs_header);
934         return err;
935 }
936
937 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
938 {
939         const struct sw_flow_actions *sf_acts;
940         int len;
941
942         sf_acts = rcu_dereference_protected(flow->sf_acts,
943                                             lockdep_genl_is_held());
944
945         /* OVS_FLOW_ATTR_KEY */
946         len = nla_total_size(FLOW_BUFSIZE);
947         /* OVS_FLOW_ATTR_ACTIONS */
948         len += nla_total_size(sf_acts->actions_len);
949         /* OVS_FLOW_ATTR_STATS */
950         len += nla_total_size(sizeof(struct ovs_flow_stats));
951         /* OVS_FLOW_ATTR_TCP_FLAGS */
952         len += nla_total_size(1);
953         /* OVS_FLOW_ATTR_USED */
954         len += nla_total_size(8);
955
956         len += NLMSG_ALIGN(sizeof(struct ovs_header));
957
958         return genlmsg_new(len, GFP_KERNEL);
959 }
960
961 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
962                                                struct datapath *dp,
963                                                u32 pid, u32 seq, u8 cmd)
964 {
965         struct sk_buff *skb;
966         int retval;
967
968         skb = ovs_flow_cmd_alloc_info(flow);
969         if (!skb)
970                 return ERR_PTR(-ENOMEM);
971
972         retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
973         BUG_ON(retval < 0);
974         return skb;
975 }
976
977 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
978 {
979         struct nlattr **a = info->attrs;
980         struct ovs_header *ovs_header = info->userhdr;
981         struct sw_flow_key key;
982         struct sw_flow *flow;
983         struct sk_buff *reply;
984         struct datapath *dp;
985         struct flow_table *table;
986         int error;
987         int key_len;
988
989         /* Extract key. */
990         error = -EINVAL;
991         if (!a[OVS_FLOW_ATTR_KEY])
992                 goto error;
993         error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
994         if (error)
995                 goto error;
996
997         /* Validate actions. */
998         if (a[OVS_FLOW_ATTR_ACTIONS]) {
999                 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key,  0);
1000                 if (error)
1001                         goto error;
1002         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1003                 error = -EINVAL;
1004                 goto error;
1005         }
1006
1007         dp = get_dp(ovs_header->dp_ifindex);
1008         error = -ENODEV;
1009         if (!dp)
1010                 goto error;
1011
1012         table = genl_dereference(dp->table);
1013         flow = flow_tbl_lookup(table, &key, key_len);
1014         if (!flow) {
1015                 struct sw_flow_actions *acts;
1016
1017                 /* Bail out if we're not allowed to create a new flow. */
1018                 error = -ENOENT;
1019                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1020                         goto error;
1021
1022                 /* Expand table, if necessary, to make room. */
1023                 if (flow_tbl_need_to_expand(table)) {
1024                         struct flow_table *new_table;
1025
1026                         new_table = flow_tbl_expand(table);
1027                         if (!IS_ERR(new_table)) {
1028                                 rcu_assign_pointer(dp->table, new_table);
1029                                 flow_tbl_deferred_destroy(table);
1030                                 table = genl_dereference(dp->table);
1031                         }
1032                 }
1033
1034                 /* Allocate flow. */
1035                 flow = flow_alloc();
1036                 if (IS_ERR(flow)) {
1037                         error = PTR_ERR(flow);
1038                         goto error;
1039                 }
1040                 flow->key = key;
1041                 clear_stats(flow);
1042
1043                 /* Obtain actions. */
1044                 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1045                 error = PTR_ERR(acts);
1046                 if (IS_ERR(acts))
1047                         goto error_free_flow;
1048                 rcu_assign_pointer(flow->sf_acts, acts);
1049
1050                 /* Put flow in bucket. */
1051                 flow->hash = flow_hash(&key, key_len);
1052                 flow_tbl_insert(table, flow);
1053
1054                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1055                                                 info->snd_seq,
1056                                                 OVS_FLOW_CMD_NEW);
1057         } else {
1058                 /* We found a matching flow. */
1059                 struct sw_flow_actions *old_acts;
1060                 struct nlattr *acts_attrs;
1061
1062                 /* Bail out if we're not allowed to modify an existing flow.
1063                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1064                  * because Generic Netlink treats the latter as a dump
1065                  * request.  We also accept NLM_F_EXCL in case that bug ever
1066                  * gets fixed.
1067                  */
1068                 error = -EEXIST;
1069                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1070                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1071                         goto error;
1072
1073                 /* Update actions. */
1074                 old_acts = rcu_dereference_protected(flow->sf_acts,
1075                                                      lockdep_genl_is_held());
1076                 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1077                 if (acts_attrs &&
1078                    (old_acts->actions_len != nla_len(acts_attrs) ||
1079                    memcmp(old_acts->actions, nla_data(acts_attrs),
1080                           old_acts->actions_len))) {
1081                         struct sw_flow_actions *new_acts;
1082
1083                         new_acts = flow_actions_alloc(acts_attrs);
1084                         error = PTR_ERR(new_acts);
1085                         if (IS_ERR(new_acts))
1086                                 goto error;
1087
1088                         rcu_assign_pointer(flow->sf_acts, new_acts);
1089                         flow_deferred_free_acts(old_acts);
1090                 }
1091
1092                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1093                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1094
1095                 /* Clear stats. */
1096                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1097                         spin_lock_bh(&flow->lock);
1098                         clear_stats(flow);
1099                         spin_unlock_bh(&flow->lock);
1100                 }
1101         }
1102
1103         if (!IS_ERR(reply))
1104                 genl_notify(reply, genl_info_net(info), info->snd_pid,
1105                            dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1106         else
1107                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1108                                 dp_flow_multicast_group.id, PTR_ERR(reply));
1109         return 0;
1110
1111 error_free_flow:
1112         flow_put(flow);
1113 error:
1114         return error;
1115 }
1116
1117 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1118 {
1119         struct nlattr **a = info->attrs;
1120         struct ovs_header *ovs_header = info->userhdr;
1121         struct sw_flow_key key;
1122         struct sk_buff *reply;
1123         struct sw_flow *flow;
1124         struct datapath *dp;
1125         struct flow_table *table;
1126         int err;
1127         int key_len;
1128
1129         if (!a[OVS_FLOW_ATTR_KEY])
1130                 return -EINVAL;
1131         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1132         if (err)
1133                 return err;
1134
1135         dp = get_dp(ovs_header->dp_ifindex);
1136         if (!dp)
1137                 return -ENODEV;
1138
1139         table = genl_dereference(dp->table);
1140         flow = flow_tbl_lookup(table, &key, key_len);
1141         if (!flow)
1142                 return -ENOENT;
1143
1144         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1145                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1146         if (IS_ERR(reply))
1147                 return PTR_ERR(reply);
1148
1149         return genlmsg_reply(reply, info);
1150 }
1151
1152 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1153 {
1154         struct nlattr **a = info->attrs;
1155         struct ovs_header *ovs_header = info->userhdr;
1156         struct sw_flow_key key;
1157         struct sk_buff *reply;
1158         struct sw_flow *flow;
1159         struct datapath *dp;
1160         struct flow_table *table;
1161         int err;
1162         int key_len;
1163
1164         if (!a[OVS_FLOW_ATTR_KEY])
1165                 return flush_flows(ovs_header->dp_ifindex);
1166         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1167         if (err)
1168                 return err;
1169
1170         dp = get_dp(ovs_header->dp_ifindex);
1171         if (!dp)
1172                 return -ENODEV;
1173
1174         table = genl_dereference(dp->table);
1175         flow = flow_tbl_lookup(table, &key, key_len);
1176         if (!flow)
1177                 return -ENOENT;
1178
1179         reply = ovs_flow_cmd_alloc_info(flow);
1180         if (!reply)
1181                 return -ENOMEM;
1182
1183         flow_tbl_remove(table, flow);
1184
1185         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1186                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1187         BUG_ON(err < 0);
1188
1189         flow_deferred_free(flow);
1190
1191         genl_notify(reply, genl_info_net(info), info->snd_pid,
1192                     dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1193         return 0;
1194 }
1195
1196 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1197 {
1198         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1199         struct datapath *dp;
1200         struct flow_table *table;
1201
1202         dp = get_dp(ovs_header->dp_ifindex);
1203         if (!dp)
1204                 return -ENODEV;
1205
1206         table = genl_dereference(dp->table);
1207
1208         for (;;) {
1209                 struct sw_flow *flow;
1210                 u32 bucket, obj;
1211
1212                 bucket = cb->args[0];
1213                 obj = cb->args[1];
1214                 flow = flow_tbl_next(table, &bucket, &obj);
1215                 if (!flow)
1216                         break;
1217
1218                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1219                                            NETLINK_CB(cb->skb).pid,
1220                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1221                                            OVS_FLOW_CMD_NEW) < 0)
1222                         break;
1223
1224                 cb->args[0] = bucket;
1225                 cb->args[1] = obj;
1226         }
1227         return skb->len;
1228 }
1229
1230 static struct genl_ops dp_flow_genl_ops[] = {
1231         { .cmd = OVS_FLOW_CMD_NEW,
1232           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1233           .policy = flow_policy,
1234           .doit = ovs_flow_cmd_new_or_set
1235         },
1236         { .cmd = OVS_FLOW_CMD_DEL,
1237           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1238           .policy = flow_policy,
1239           .doit = ovs_flow_cmd_del
1240         },
1241         { .cmd = OVS_FLOW_CMD_GET,
1242           .flags = 0,               /* OK for unprivileged users. */
1243           .policy = flow_policy,
1244           .doit = ovs_flow_cmd_get,
1245           .dumpit = ovs_flow_cmd_dump
1246         },
1247         { .cmd = OVS_FLOW_CMD_SET,
1248           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1249           .policy = flow_policy,
1250           .doit = ovs_flow_cmd_new_or_set,
1251         },
1252 };
1253
1254 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1255 #ifdef HAVE_NLA_NUL_STRING
1256         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1257 #endif
1258         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1259 };
1260
1261 static struct genl_family dp_datapath_genl_family = {
1262         .id = GENL_ID_GENERATE,
1263         .hdrsize = sizeof(struct ovs_header),
1264         .name = OVS_DATAPATH_FAMILY,
1265         .version = OVS_DATAPATH_VERSION,
1266         .maxattr = OVS_DP_ATTR_MAX
1267 };
1268
1269 static struct genl_multicast_group dp_datapath_multicast_group = {
1270         .name = OVS_DATAPATH_MCGROUP
1271 };
1272
1273 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1274                                 u32 pid, u32 seq, u32 flags, u8 cmd)
1275 {
1276         struct ovs_header *ovs_header;
1277         struct ovs_dp_stats dp_stats;
1278         int err;
1279
1280         ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1281                                    flags, cmd);
1282         if (!ovs_header)
1283                 goto error;
1284
1285         ovs_header->dp_ifindex = get_dpifindex(dp);
1286
1287         rcu_read_lock();
1288         err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1289         rcu_read_unlock();
1290         if (err)
1291                 goto nla_put_failure;
1292
1293         get_dp_stats(dp, &dp_stats);
1294         NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
1295
1296         return genlmsg_end(skb, ovs_header);
1297
1298 nla_put_failure:
1299         genlmsg_cancel(skb, ovs_header);
1300 error:
1301         return -EMSGSIZE;
1302 }
1303
1304 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1305                                              u32 seq, u8 cmd)
1306 {
1307         struct sk_buff *skb;
1308         int retval;
1309
1310         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1311         if (!skb)
1312                 return ERR_PTR(-ENOMEM);
1313
1314         retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1315         if (retval < 0) {
1316                 kfree_skb(skb);
1317                 return ERR_PTR(retval);
1318         }
1319         return skb;
1320 }
1321
1322 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1323 {
1324         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1325 }
1326
1327 /* Called with genl_mutex and optionally with RTNL lock also. */
1328 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1329                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1330 {
1331         struct datapath *dp;
1332
1333         if (!a[OVS_DP_ATTR_NAME])
1334                 dp = get_dp(ovs_header->dp_ifindex);
1335         else {
1336                 struct vport *vport;
1337
1338                 rcu_read_lock();
1339                 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1340                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1341                 rcu_read_unlock();
1342         }
1343         return dp ? dp : ERR_PTR(-ENODEV);
1344 }
1345
1346 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1347 {
1348         struct nlattr **a = info->attrs;
1349         struct vport_parms parms;
1350         struct sk_buff *reply;
1351         struct datapath *dp;
1352         struct vport *vport;
1353         int err;
1354
1355         err = -EINVAL;
1356         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1357                 goto err;
1358
1359         err = ovs_dp_cmd_validate(a);
1360         if (err)
1361                 goto err;
1362
1363         rtnl_lock();
1364         err = -ENODEV;
1365         if (!try_module_get(THIS_MODULE))
1366                 goto err_unlock_rtnl;
1367
1368         err = -ENOMEM;
1369         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1370         if (dp == NULL)
1371                 goto err_put_module;
1372         INIT_LIST_HEAD(&dp->port_list);
1373
1374         /* Initialize kobject for bridge.  This will be added as
1375          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1376         dp->ifobj.kset = NULL;
1377         kobject_init(&dp->ifobj, &dp_ktype);
1378
1379         /* Allocate table. */
1380         err = -ENOMEM;
1381         rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1382         if (!dp->table)
1383                 goto err_free_dp;
1384
1385         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1386         if (!dp->stats_percpu) {
1387                 err = -ENOMEM;
1388                 goto err_destroy_table;
1389         }
1390
1391         /* Set up our datapath device. */
1392         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1393         parms.type = OVS_VPORT_TYPE_INTERNAL;
1394         parms.options = NULL;
1395         parms.dp = dp;
1396         parms.port_no = OVSP_LOCAL;
1397         parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1398
1399         vport = new_vport(&parms);
1400         if (IS_ERR(vport)) {
1401                 err = PTR_ERR(vport);
1402                 if (err == -EBUSY)
1403                         err = -EEXIST;
1404
1405                 goto err_destroy_percpu;
1406         }
1407
1408         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1409                                       info->snd_seq, OVS_DP_CMD_NEW);
1410         err = PTR_ERR(reply);
1411         if (IS_ERR(reply))
1412                 goto err_destroy_local_port;
1413
1414         list_add_tail(&dp->list_node, &dps);
1415         dp_sysfs_add_dp(dp);
1416
1417         rtnl_unlock();
1418
1419         genl_notify(reply, genl_info_net(info), info->snd_pid,
1420                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1421         return 0;
1422
1423 err_destroy_local_port:
1424         dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1425 err_destroy_percpu:
1426         free_percpu(dp->stats_percpu);
1427 err_destroy_table:
1428         flow_tbl_destroy(genl_dereference(dp->table));
1429 err_free_dp:
1430         kfree(dp);
1431 err_put_module:
1432         module_put(THIS_MODULE);
1433 err_unlock_rtnl:
1434         rtnl_unlock();
1435 err:
1436         return err;
1437 }
1438
1439 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1440 {
1441         struct vport *vport, *next_vport;
1442         struct sk_buff *reply;
1443         struct datapath *dp;
1444         int err;
1445
1446         err = ovs_dp_cmd_validate(info->attrs);
1447         if (err)
1448                 goto exit;
1449
1450         rtnl_lock();
1451         dp = lookup_datapath(info->userhdr, info->attrs);
1452         err = PTR_ERR(dp);
1453         if (IS_ERR(dp))
1454                 goto exit_unlock;
1455
1456         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1457                                       info->snd_seq, OVS_DP_CMD_DEL);
1458         err = PTR_ERR(reply);
1459         if (IS_ERR(reply))
1460                 goto exit_unlock;
1461
1462         list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1463                 if (vport->port_no != OVSP_LOCAL)
1464                         dp_detach_port(vport);
1465
1466         dp_sysfs_del_dp(dp);
1467         list_del(&dp->list_node);
1468         dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1469
1470         /* rtnl_unlock() will wait until all the references to devices that
1471          * are pending unregistration have been dropped.  We do it here to
1472          * ensure that any internal devices (which contain DP pointers) are
1473          * fully destroyed before freeing the datapath.
1474          */
1475         rtnl_unlock();
1476
1477         call_rcu(&dp->rcu, destroy_dp_rcu);
1478         module_put(THIS_MODULE);
1479
1480         genl_notify(reply, genl_info_net(info), info->snd_pid,
1481                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1482
1483         return 0;
1484
1485 exit_unlock:
1486         rtnl_unlock();
1487 exit:
1488         return err;
1489 }
1490
1491 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1492 {
1493         struct sk_buff *reply;
1494         struct datapath *dp;
1495         int err;
1496
1497         err = ovs_dp_cmd_validate(info->attrs);
1498         if (err)
1499                 return err;
1500
1501         dp = lookup_datapath(info->userhdr, info->attrs);
1502         if (IS_ERR(dp))
1503                 return PTR_ERR(dp);
1504
1505         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1506                                       info->snd_seq, OVS_DP_CMD_NEW);
1507         if (IS_ERR(reply)) {
1508                 err = PTR_ERR(reply);
1509                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1510                                 dp_datapath_multicast_group.id, err);
1511                 return 0;
1512         }
1513
1514         genl_notify(reply, genl_info_net(info), info->snd_pid,
1515                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1516         return 0;
1517 }
1518
1519 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1520 {
1521         struct sk_buff *reply;
1522         struct datapath *dp;
1523         int err;
1524
1525         err = ovs_dp_cmd_validate(info->attrs);
1526         if (err)
1527                 return err;
1528
1529         dp = lookup_datapath(info->userhdr, info->attrs);
1530         if (IS_ERR(dp))
1531                 return PTR_ERR(dp);
1532
1533         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1534                                       info->snd_seq, OVS_DP_CMD_NEW);
1535         if (IS_ERR(reply))
1536                 return PTR_ERR(reply);
1537
1538         return genlmsg_reply(reply, info);
1539 }
1540
1541 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1542 {
1543         struct datapath *dp;
1544         int skip = cb->args[0];
1545         int i = 0;
1546
1547         list_for_each_entry(dp, &dps, list_node) {
1548                 if (i < skip)
1549                         continue;
1550                 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1551                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1552                                          OVS_DP_CMD_NEW) < 0)
1553                         break;
1554                 i++;
1555         }
1556
1557         cb->args[0] = i;
1558
1559         return skb->len;
1560 }
1561
1562 static struct genl_ops dp_datapath_genl_ops[] = {
1563         { .cmd = OVS_DP_CMD_NEW,
1564           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1565           .policy = datapath_policy,
1566           .doit = ovs_dp_cmd_new
1567         },
1568         { .cmd = OVS_DP_CMD_DEL,
1569           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1570           .policy = datapath_policy,
1571           .doit = ovs_dp_cmd_del
1572         },
1573         { .cmd = OVS_DP_CMD_GET,
1574           .flags = 0,               /* OK for unprivileged users. */
1575           .policy = datapath_policy,
1576           .doit = ovs_dp_cmd_get,
1577           .dumpit = ovs_dp_cmd_dump
1578         },
1579         { .cmd = OVS_DP_CMD_SET,
1580           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1581           .policy = datapath_policy,
1582           .doit = ovs_dp_cmd_set,
1583         },
1584 };
1585
1586 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1587 #ifdef HAVE_NLA_NUL_STRING
1588         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1589         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1590         [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1591 #else
1592         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1593         [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1594 #endif
1595         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1596         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1597         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1598         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1599 };
1600
1601 static struct genl_family dp_vport_genl_family = {
1602         .id = GENL_ID_GENERATE,
1603         .hdrsize = sizeof(struct ovs_header),
1604         .name = OVS_VPORT_FAMILY,
1605         .version = OVS_VPORT_VERSION,
1606         .maxattr = OVS_VPORT_ATTR_MAX
1607 };
1608
1609 struct genl_multicast_group dp_vport_multicast_group = {
1610         .name = OVS_VPORT_MCGROUP
1611 };
1612
1613 /* Called with RTNL lock or RCU read lock. */
1614 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1615                                    u32 pid, u32 seq, u32 flags, u8 cmd)
1616 {
1617         struct ovs_header *ovs_header;
1618         struct ovs_vport_stats vport_stats;
1619         int err;
1620
1621         ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1622                                  flags, cmd);
1623         if (!ovs_header)
1624                 return -EMSGSIZE;
1625
1626         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1627
1628         NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1629         NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1630         NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1631         NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1632
1633         vport_get_stats(vport, &vport_stats);
1634         NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1635                 &vport_stats);
1636
1637         NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1638                 vport->ops->get_addr(vport));
1639
1640         err = vport_get_options(vport, skb);
1641         if (err == -EMSGSIZE)
1642                 goto error;
1643
1644         return genlmsg_end(skb, ovs_header);
1645
1646 nla_put_failure:
1647         err = -EMSGSIZE;
1648 error:
1649         genlmsg_cancel(skb, ovs_header);
1650         return err;
1651 }
1652
1653 /* Called with RTNL lock or RCU read lock. */
1654 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1655                                          u32 seq, u8 cmd)
1656 {
1657         struct sk_buff *skb;
1658         int retval;
1659
1660         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1661         if (!skb)
1662                 return ERR_PTR(-ENOMEM);
1663
1664         retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1665         if (retval < 0) {
1666                 kfree_skb(skb);
1667                 return ERR_PTR(retval);
1668         }
1669         return skb;
1670 }
1671
1672 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1673 {
1674         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1675 }
1676
1677 /* Called with RTNL lock or RCU read lock. */
1678 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1679                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1680 {
1681         struct datapath *dp;
1682         struct vport *vport;
1683
1684         if (a[OVS_VPORT_ATTR_NAME]) {
1685                 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1686                 if (!vport)
1687                         return ERR_PTR(-ENODEV);
1688                 return vport;
1689         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1690                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1691
1692                 if (port_no >= DP_MAX_PORTS)
1693                         return ERR_PTR(-EFBIG);
1694
1695                 dp = get_dp(ovs_header->dp_ifindex);
1696                 if (!dp)
1697                         return ERR_PTR(-ENODEV);
1698
1699                 vport = get_vport_protected(dp, port_no);
1700                 if (!vport)
1701                         return ERR_PTR(-ENOENT);
1702                 return vport;
1703         } else
1704                 return ERR_PTR(-EINVAL);
1705 }
1706
1707 /* Called with RTNL lock. */
1708 static int change_vport(struct vport *vport,
1709                         struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1710 {
1711         int err = 0;
1712
1713         if (a[OVS_VPORT_ATTR_STATS])
1714                 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1715
1716         if (a[OVS_VPORT_ATTR_ADDRESS])
1717                 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1718
1719         return err;
1720 }
1721
1722 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1723 {
1724         struct nlattr **a = info->attrs;
1725         struct ovs_header *ovs_header = info->userhdr;
1726         struct vport_parms parms;
1727         struct sk_buff *reply;
1728         struct vport *vport;
1729         struct datapath *dp;
1730         u32 port_no;
1731         int err;
1732
1733         err = -EINVAL;
1734         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1735             !a[OVS_VPORT_ATTR_UPCALL_PID])
1736                 goto exit;
1737
1738         err = ovs_vport_cmd_validate(a);
1739         if (err)
1740                 goto exit;
1741
1742         rtnl_lock();
1743         dp = get_dp(ovs_header->dp_ifindex);
1744         err = -ENODEV;
1745         if (!dp)
1746                 goto exit_unlock;
1747
1748         if (a[OVS_VPORT_ATTR_PORT_NO]) {
1749                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1750
1751                 err = -EFBIG;
1752                 if (port_no >= DP_MAX_PORTS)
1753                         goto exit_unlock;
1754
1755                 vport = get_vport_protected(dp, port_no);
1756                 err = -EBUSY;
1757                 if (vport)
1758                         goto exit_unlock;
1759         } else {
1760                 for (port_no = 1; ; port_no++) {
1761                         if (port_no >= DP_MAX_PORTS) {
1762                                 err = -EFBIG;
1763                                 goto exit_unlock;
1764                         }
1765                         vport = get_vport_protected(dp, port_no);
1766                         if (!vport)
1767                                 break;
1768                 }
1769         }
1770
1771         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1772         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1773         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1774         parms.dp = dp;
1775         parms.port_no = port_no;
1776         parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1777
1778         vport = new_vport(&parms);
1779         err = PTR_ERR(vport);
1780         if (IS_ERR(vport))
1781                 goto exit_unlock;
1782
1783         dp_sysfs_add_if(vport);
1784
1785         err = change_vport(vport, a);
1786         if (!err) {
1787                 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1788                                                  info->snd_seq,
1789                                                  OVS_VPORT_CMD_NEW);
1790                 if (IS_ERR(reply))
1791                         err = PTR_ERR(reply);
1792         }
1793         if (err) {
1794                 dp_detach_port(vport);
1795                 goto exit_unlock;
1796         }
1797         genl_notify(reply, genl_info_net(info), info->snd_pid,
1798                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1799
1800
1801 exit_unlock:
1802         rtnl_unlock();
1803 exit:
1804         return err;
1805 }
1806
1807 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1808 {
1809         struct nlattr **a = info->attrs;
1810         struct sk_buff *reply;
1811         struct vport *vport;
1812         int err;
1813
1814         err = ovs_vport_cmd_validate(a);
1815         if (err)
1816                 goto exit;
1817
1818         rtnl_lock();
1819         vport = lookup_vport(info->userhdr, a);
1820         err = PTR_ERR(vport);
1821         if (IS_ERR(vport))
1822                 goto exit_unlock;
1823
1824         err = 0;
1825         if (a[OVS_VPORT_ATTR_TYPE] &&
1826             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1827                 err = -EINVAL;
1828
1829         if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1830                 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1831         if (!err)
1832                 err = change_vport(vport, a);
1833         if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1834                 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1835
1836         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1837                                          OVS_VPORT_CMD_NEW);
1838         if (IS_ERR(reply)) {
1839                 err = PTR_ERR(reply);
1840                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1841                                 dp_vport_multicast_group.id, err);
1842                 return 0;
1843         }
1844
1845         genl_notify(reply, genl_info_net(info), info->snd_pid,
1846                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1847
1848 exit_unlock:
1849         rtnl_unlock();
1850 exit:
1851         return err;
1852 }
1853
1854 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1855 {
1856         struct nlattr **a = info->attrs;
1857         struct sk_buff *reply;
1858         struct vport *vport;
1859         int err;
1860
1861         err = ovs_vport_cmd_validate(a);
1862         if (err)
1863                 goto exit;
1864
1865         rtnl_lock();
1866         vport = lookup_vport(info->userhdr, a);
1867         err = PTR_ERR(vport);
1868         if (IS_ERR(vport))
1869                 goto exit_unlock;
1870
1871         if (vport->port_no == OVSP_LOCAL) {
1872                 err = -EINVAL;
1873                 goto exit_unlock;
1874         }
1875
1876         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1877                                          OVS_VPORT_CMD_DEL);
1878         err = PTR_ERR(reply);
1879         if (IS_ERR(reply))
1880                 goto exit_unlock;
1881
1882         dp_detach_port(vport);
1883
1884         genl_notify(reply, genl_info_net(info), info->snd_pid,
1885                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1886
1887 exit_unlock:
1888         rtnl_unlock();
1889 exit:
1890         return err;
1891 }
1892
1893 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1894 {
1895         struct nlattr **a = info->attrs;
1896         struct ovs_header *ovs_header = info->userhdr;
1897         struct sk_buff *reply;
1898         struct vport *vport;
1899         int err;
1900
1901         err = ovs_vport_cmd_validate(a);
1902         if (err)
1903                 goto exit;
1904
1905         rcu_read_lock();
1906         vport = lookup_vport(ovs_header, a);
1907         err = PTR_ERR(vport);
1908         if (IS_ERR(vport))
1909                 goto exit_unlock;
1910
1911         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1912                                          OVS_VPORT_CMD_NEW);
1913         err = PTR_ERR(reply);
1914         if (IS_ERR(reply))
1915                 goto exit_unlock;
1916
1917         rcu_read_unlock();
1918
1919         return genlmsg_reply(reply, info);
1920
1921 exit_unlock:
1922         rcu_read_unlock();
1923 exit:
1924         return err;
1925 }
1926
1927 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1928 {
1929         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1930         struct datapath *dp;
1931         u32 port_no;
1932         int retval;
1933
1934         dp = get_dp(ovs_header->dp_ifindex);
1935         if (!dp)
1936                 return -ENODEV;
1937
1938         rcu_read_lock();
1939         for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1940                 struct vport *vport;
1941
1942                 vport = get_vport_protected(dp, port_no);
1943                 if (!vport)
1944                         continue;
1945
1946                 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1947                                             cb->nlh->nlmsg_seq, NLM_F_MULTI,
1948                                             OVS_VPORT_CMD_NEW) < 0)
1949                         break;
1950         }
1951         rcu_read_unlock();
1952
1953         cb->args[0] = port_no;
1954         retval = skb->len;
1955
1956         return retval;
1957 }
1958
1959 static struct genl_ops dp_vport_genl_ops[] = {
1960         { .cmd = OVS_VPORT_CMD_NEW,
1961           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1962           .policy = vport_policy,
1963           .doit = ovs_vport_cmd_new
1964         },
1965         { .cmd = OVS_VPORT_CMD_DEL,
1966           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1967           .policy = vport_policy,
1968           .doit = ovs_vport_cmd_del
1969         },
1970         { .cmd = OVS_VPORT_CMD_GET,
1971           .flags = 0,               /* OK for unprivileged users. */
1972           .policy = vport_policy,
1973           .doit = ovs_vport_cmd_get,
1974           .dumpit = ovs_vport_cmd_dump
1975         },
1976         { .cmd = OVS_VPORT_CMD_SET,
1977           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1978           .policy = vport_policy,
1979           .doit = ovs_vport_cmd_set,
1980         },
1981 };
1982
1983 struct genl_family_and_ops {
1984         struct genl_family *family;
1985         struct genl_ops *ops;
1986         int n_ops;
1987         struct genl_multicast_group *group;
1988 };
1989
1990 static const struct genl_family_and_ops dp_genl_families[] = {
1991         { &dp_datapath_genl_family,
1992           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1993           &dp_datapath_multicast_group },
1994         { &dp_vport_genl_family,
1995           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1996           &dp_vport_multicast_group },
1997         { &dp_flow_genl_family,
1998           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1999           &dp_flow_multicast_group },
2000         { &dp_packet_genl_family,
2001           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2002           NULL },
2003 };
2004
2005 static void dp_unregister_genl(int n_families)
2006 {
2007         int i;
2008
2009         for (i = 0; i < n_families; i++)
2010                 genl_unregister_family(dp_genl_families[i].family);
2011 }
2012
2013 static int dp_register_genl(void)
2014 {
2015         int n_registered;
2016         int err;
2017         int i;
2018
2019         n_registered = 0;
2020         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2021                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2022
2023                 err = genl_register_family_with_ops(f->family, f->ops,
2024                                                     f->n_ops);
2025                 if (err)
2026                         goto error;
2027                 n_registered++;
2028
2029                 if (f->group) {
2030                         err = genl_register_mc_group(f->family, f->group);
2031                         if (err)
2032                                 goto error;
2033                 }
2034         }
2035
2036         return 0;
2037
2038 error:
2039         dp_unregister_genl(n_registered);
2040         return err;
2041 }
2042
2043 static int __init dp_init(void)
2044 {
2045         struct sk_buff *dummy_skb;
2046         int err;
2047
2048         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2049
2050         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2051                 VERSION BUILDNR);
2052
2053         err = tnl_init();
2054         if (err)
2055                 goto error;
2056
2057         err = flow_init();
2058         if (err)
2059                 goto error_tnl_exit;
2060
2061         err = vport_init();
2062         if (err)
2063                 goto error_flow_exit;
2064
2065         err = register_netdevice_notifier(&dp_device_notifier);
2066         if (err)
2067                 goto error_vport_exit;
2068
2069         err = dp_register_genl();
2070         if (err < 0)
2071                 goto error_unreg_notifier;
2072
2073         return 0;
2074
2075 error_unreg_notifier:
2076         unregister_netdevice_notifier(&dp_device_notifier);
2077 error_vport_exit:
2078         vport_exit();
2079 error_flow_exit:
2080         flow_exit();
2081 error_tnl_exit:
2082         tnl_exit();
2083 error:
2084         return err;
2085 }
2086
2087 static void dp_cleanup(void)
2088 {
2089         rcu_barrier();
2090         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2091         unregister_netdevice_notifier(&dp_device_notifier);
2092         vport_exit();
2093         flow_exit();
2094         tnl_exit();
2095 }
2096
2097 module_init(dp_init);
2098 module_exit(dp_cleanup);
2099
2100 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2101 MODULE_LICENSE("GPL");