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