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