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