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