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