datapath: Always notify in initial namespace for port deletions.
[openvswitch] / 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 rcu_read_lock or RTNL lock. */
116 const char *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         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 = 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 dp_detach_port(struct vport *p)
264 {
265         ASSERT_RTNL();
266
267         if (p->port_no != OVSP_LOCAL)
268                 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         vport_del(p);
277 }
278
279 /* Must be called with rcu_read_lock. */
280 void 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 = 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 = 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                         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         flow_used(OVS_CB(skb)->flow, skb);
322         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 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 flow_extract() in
399                          * 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         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 = flow_tbl_alloc(TBL_MIN_BUCKETS);
498         if (!new_table)
499                 return -ENOMEM;
500
501         rcu_assign_pointer(dp->table, new_table);
502
503         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 = flow_alloc();
747         err = PTR_ERR(flow);
748         if (IS_ERR(flow))
749                 goto err_kfree_skb;
750
751         err = flow_extract(packet, -1, &flow->key, &key_len);
752         if (err)
753                 goto err_flow_put;
754
755         err = 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 = flow_hash(&flow->key, key_len);
767
768         acts = 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 = execute_actions(dp, packet);
785         local_bh_enable();
786         rcu_read_unlock();
787
788         flow_put(flow);
789         return err;
790
791 err_unlock:
792         rcu_read_unlock();
793 err_flow_put:
794         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 = 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 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 = 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, 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 = 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 = 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 (flow_tbl_need_to_expand(table)) {
1018                         struct flow_table *new_table;
1019
1020                         new_table = flow_tbl_expand(table);
1021                         if (!IS_ERR(new_table)) {
1022                                 rcu_assign_pointer(dp->table, new_table);
1023                                 flow_tbl_deferred_destroy(table);
1024                                 table = genl_dereference(dp->table);
1025                         }
1026                 }
1027
1028                 /* Allocate flow. */
1029                 flow = 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 = 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 = flow_hash(&key, key_len);
1046                 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 = 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                         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                            dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1100         else
1101                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1102                                 dp_flow_multicast_group.id, PTR_ERR(reply));
1103         return 0;
1104
1105 error_free_flow:
1106         flow_put(flow);
1107 error:
1108         return error;
1109 }
1110
1111 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1112 {
1113         struct nlattr **a = info->attrs;
1114         struct ovs_header *ovs_header = info->userhdr;
1115         struct sw_flow_key key;
1116         struct sk_buff *reply;
1117         struct sw_flow *flow;
1118         struct datapath *dp;
1119         struct flow_table *table;
1120         int err;
1121         int key_len;
1122
1123         if (!a[OVS_FLOW_ATTR_KEY])
1124                 return -EINVAL;
1125         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1126         if (err)
1127                 return err;
1128
1129         dp = get_dp(ovs_header->dp_ifindex);
1130         if (!dp)
1131                 return -ENODEV;
1132
1133         table = genl_dereference(dp->table);
1134         flow = flow_tbl_lookup(table, &key, key_len);
1135         if (!flow)
1136                 return -ENOENT;
1137
1138         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1139                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1140         if (IS_ERR(reply))
1141                 return PTR_ERR(reply);
1142
1143         return genlmsg_reply(reply, info);
1144 }
1145
1146 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1147 {
1148         struct nlattr **a = info->attrs;
1149         struct ovs_header *ovs_header = info->userhdr;
1150         struct sw_flow_key key;
1151         struct sk_buff *reply;
1152         struct sw_flow *flow;
1153         struct datapath *dp;
1154         struct flow_table *table;
1155         int err;
1156         int key_len;
1157
1158         if (!a[OVS_FLOW_ATTR_KEY])
1159                 return flush_flows(ovs_header->dp_ifindex);
1160         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1161         if (err)
1162                 return err;
1163
1164         dp = get_dp(ovs_header->dp_ifindex);
1165         if (!dp)
1166                 return -ENODEV;
1167
1168         table = genl_dereference(dp->table);
1169         flow = flow_tbl_lookup(table, &key, key_len);
1170         if (!flow)
1171                 return -ENOENT;
1172
1173         reply = ovs_flow_cmd_alloc_info(flow);
1174         if (!reply)
1175                 return -ENOMEM;
1176
1177         flow_tbl_remove(table, flow);
1178
1179         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1180                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1181         BUG_ON(err < 0);
1182
1183         flow_deferred_free(flow);
1184
1185         genl_notify(reply, genl_info_net(info), info->snd_pid,
1186                     dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1187         return 0;
1188 }
1189
1190 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1191 {
1192         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1193         struct datapath *dp;
1194         struct flow_table *table;
1195
1196         dp = get_dp(ovs_header->dp_ifindex);
1197         if (!dp)
1198                 return -ENODEV;
1199
1200         table = genl_dereference(dp->table);
1201
1202         for (;;) {
1203                 struct sw_flow *flow;
1204                 u32 bucket, obj;
1205
1206                 bucket = cb->args[0];
1207                 obj = cb->args[1];
1208                 flow = flow_tbl_next(table, &bucket, &obj);
1209                 if (!flow)
1210                         break;
1211
1212                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1213                                            NETLINK_CB(cb->skb).pid,
1214                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1215                                            OVS_FLOW_CMD_NEW) < 0)
1216                         break;
1217
1218                 cb->args[0] = bucket;
1219                 cb->args[1] = obj;
1220         }
1221         return skb->len;
1222 }
1223
1224 static struct genl_ops dp_flow_genl_ops[] = {
1225         { .cmd = OVS_FLOW_CMD_NEW,
1226           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1227           .policy = flow_policy,
1228           .doit = ovs_flow_cmd_new_or_set
1229         },
1230         { .cmd = OVS_FLOW_CMD_DEL,
1231           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1232           .policy = flow_policy,
1233           .doit = ovs_flow_cmd_del
1234         },
1235         { .cmd = OVS_FLOW_CMD_GET,
1236           .flags = 0,               /* OK for unprivileged users. */
1237           .policy = flow_policy,
1238           .doit = ovs_flow_cmd_get,
1239           .dumpit = ovs_flow_cmd_dump
1240         },
1241         { .cmd = OVS_FLOW_CMD_SET,
1242           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1243           .policy = flow_policy,
1244           .doit = ovs_flow_cmd_new_or_set,
1245         },
1246 };
1247
1248 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1249 #ifdef HAVE_NLA_NUL_STRING
1250         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1251 #endif
1252         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1253 };
1254
1255 static struct genl_family dp_datapath_genl_family = {
1256         .id = GENL_ID_GENERATE,
1257         .hdrsize = sizeof(struct ovs_header),
1258         .name = OVS_DATAPATH_FAMILY,
1259         .version = OVS_DATAPATH_VERSION,
1260         .maxattr = OVS_DP_ATTR_MAX
1261 };
1262
1263 static struct genl_multicast_group dp_datapath_multicast_group = {
1264         .name = OVS_DATAPATH_MCGROUP
1265 };
1266
1267 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1268                                 u32 pid, u32 seq, u32 flags, u8 cmd)
1269 {
1270         struct ovs_header *ovs_header;
1271         struct ovs_dp_stats dp_stats;
1272         int err;
1273
1274         ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1275                                    flags, cmd);
1276         if (!ovs_header)
1277                 goto error;
1278
1279         ovs_header->dp_ifindex = get_dpifindex(dp);
1280
1281         rcu_read_lock();
1282         err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1283         rcu_read_unlock();
1284         if (err)
1285                 goto nla_put_failure;
1286
1287         get_dp_stats(dp, &dp_stats);
1288         NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
1289
1290         return genlmsg_end(skb, ovs_header);
1291
1292 nla_put_failure:
1293         genlmsg_cancel(skb, ovs_header);
1294 error:
1295         return -EMSGSIZE;
1296 }
1297
1298 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1299                                              u32 seq, u8 cmd)
1300 {
1301         struct sk_buff *skb;
1302         int retval;
1303
1304         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1305         if (!skb)
1306                 return ERR_PTR(-ENOMEM);
1307
1308         retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1309         if (retval < 0) {
1310                 kfree_skb(skb);
1311                 return ERR_PTR(retval);
1312         }
1313         return skb;
1314 }
1315
1316 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1317 {
1318         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1319 }
1320
1321 /* Called with genl_mutex and optionally with RTNL lock also. */
1322 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1323                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1324 {
1325         struct datapath *dp;
1326
1327         if (!a[OVS_DP_ATTR_NAME])
1328                 dp = get_dp(ovs_header->dp_ifindex);
1329         else {
1330                 struct vport *vport;
1331
1332                 rcu_read_lock();
1333                 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1334                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1335                 rcu_read_unlock();
1336         }
1337         return dp ? dp : ERR_PTR(-ENODEV);
1338 }
1339
1340 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1341 {
1342         struct nlattr **a = info->attrs;
1343         struct vport_parms parms;
1344         struct sk_buff *reply;
1345         struct datapath *dp;
1346         struct vport *vport;
1347         int err;
1348
1349         err = -EINVAL;
1350         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1351                 goto err;
1352
1353         err = ovs_dp_cmd_validate(a);
1354         if (err)
1355                 goto err;
1356
1357         rtnl_lock();
1358         err = -ENODEV;
1359         if (!try_module_get(THIS_MODULE))
1360                 goto err_unlock_rtnl;
1361
1362         err = -ENOMEM;
1363         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1364         if (dp == NULL)
1365                 goto err_put_module;
1366         INIT_LIST_HEAD(&dp->port_list);
1367
1368         /* Initialize kobject for bridge.  This will be added as
1369          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1370         dp->ifobj.kset = NULL;
1371         kobject_init(&dp->ifobj, &dp_ktype);
1372
1373         /* Allocate table. */
1374         err = -ENOMEM;
1375         rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1376         if (!dp->table)
1377                 goto err_free_dp;
1378
1379         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1380         if (!dp->stats_percpu) {
1381                 err = -ENOMEM;
1382                 goto err_destroy_table;
1383         }
1384
1385         /* Set up our datapath device. */
1386         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1387         parms.type = OVS_VPORT_TYPE_INTERNAL;
1388         parms.options = NULL;
1389         parms.dp = dp;
1390         parms.port_no = OVSP_LOCAL;
1391         parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1392
1393         vport = new_vport(&parms);
1394         if (IS_ERR(vport)) {
1395                 err = PTR_ERR(vport);
1396                 if (err == -EBUSY)
1397                         err = -EEXIST;
1398
1399                 goto err_destroy_percpu;
1400         }
1401
1402         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1403                                       info->snd_seq, OVS_DP_CMD_NEW);
1404         err = PTR_ERR(reply);
1405         if (IS_ERR(reply))
1406                 goto err_destroy_local_port;
1407
1408         list_add_tail(&dp->list_node, &dps);
1409         dp_sysfs_add_dp(dp);
1410
1411         rtnl_unlock();
1412
1413         genl_notify(reply, genl_info_net(info), info->snd_pid,
1414                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1415         return 0;
1416
1417 err_destroy_local_port:
1418         dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1419 err_destroy_percpu:
1420         free_percpu(dp->stats_percpu);
1421 err_destroy_table:
1422         flow_tbl_destroy(genl_dereference(dp->table));
1423 err_free_dp:
1424         kfree(dp);
1425 err_put_module:
1426         module_put(THIS_MODULE);
1427 err_unlock_rtnl:
1428         rtnl_unlock();
1429 err:
1430         return err;
1431 }
1432
1433 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1434 {
1435         struct vport *vport, *next_vport;
1436         struct sk_buff *reply;
1437         struct datapath *dp;
1438         int err;
1439
1440         err = ovs_dp_cmd_validate(info->attrs);
1441         if (err)
1442                 goto exit;
1443
1444         rtnl_lock();
1445         dp = lookup_datapath(info->userhdr, info->attrs);
1446         err = PTR_ERR(dp);
1447         if (IS_ERR(dp))
1448                 goto exit_unlock;
1449
1450         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1451                                       info->snd_seq, OVS_DP_CMD_DEL);
1452         err = PTR_ERR(reply);
1453         if (IS_ERR(reply))
1454                 goto exit_unlock;
1455
1456         list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1457                 if (vport->port_no != OVSP_LOCAL)
1458                         dp_detach_port(vport);
1459
1460         dp_sysfs_del_dp(dp);
1461         list_del(&dp->list_node);
1462         dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1463
1464         /* rtnl_unlock() will wait until all the references to devices that
1465          * are pending unregistration have been dropped.  We do it here to
1466          * ensure that any internal devices (which contain DP pointers) are
1467          * fully destroyed before freeing the datapath.
1468          */
1469         rtnl_unlock();
1470
1471         call_rcu(&dp->rcu, destroy_dp_rcu);
1472         module_put(THIS_MODULE);
1473
1474         genl_notify(reply, genl_info_net(info), info->snd_pid,
1475                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1476
1477         return 0;
1478
1479 exit_unlock:
1480         rtnl_unlock();
1481 exit:
1482         return err;
1483 }
1484
1485 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1486 {
1487         struct sk_buff *reply;
1488         struct datapath *dp;
1489         int err;
1490
1491         err = ovs_dp_cmd_validate(info->attrs);
1492         if (err)
1493                 return err;
1494
1495         dp = lookup_datapath(info->userhdr, info->attrs);
1496         if (IS_ERR(dp))
1497                 return PTR_ERR(dp);
1498
1499         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1500                                       info->snd_seq, OVS_DP_CMD_NEW);
1501         if (IS_ERR(reply)) {
1502                 err = PTR_ERR(reply);
1503                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1504                                 dp_datapath_multicast_group.id, err);
1505                 return 0;
1506         }
1507
1508         genl_notify(reply, genl_info_net(info), info->snd_pid,
1509                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1510         return 0;
1511 }
1512
1513 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1514 {
1515         struct sk_buff *reply;
1516         struct datapath *dp;
1517         int err;
1518
1519         err = ovs_dp_cmd_validate(info->attrs);
1520         if (err)
1521                 return err;
1522
1523         dp = lookup_datapath(info->userhdr, info->attrs);
1524         if (IS_ERR(dp))
1525                 return PTR_ERR(dp);
1526
1527         reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1528                                       info->snd_seq, OVS_DP_CMD_NEW);
1529         if (IS_ERR(reply))
1530                 return PTR_ERR(reply);
1531
1532         return genlmsg_reply(reply, info);
1533 }
1534
1535 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1536 {
1537         struct datapath *dp;
1538         int skip = cb->args[0];
1539         int i = 0;
1540
1541         list_for_each_entry(dp, &dps, list_node) {
1542                 if (i < skip)
1543                         continue;
1544                 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1545                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1546                                          OVS_DP_CMD_NEW) < 0)
1547                         break;
1548                 i++;
1549         }
1550
1551         cb->args[0] = i;
1552
1553         return skb->len;
1554 }
1555
1556 static struct genl_ops dp_datapath_genl_ops[] = {
1557         { .cmd = OVS_DP_CMD_NEW,
1558           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1559           .policy = datapath_policy,
1560           .doit = ovs_dp_cmd_new
1561         },
1562         { .cmd = OVS_DP_CMD_DEL,
1563           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1564           .policy = datapath_policy,
1565           .doit = ovs_dp_cmd_del
1566         },
1567         { .cmd = OVS_DP_CMD_GET,
1568           .flags = 0,               /* OK for unprivileged users. */
1569           .policy = datapath_policy,
1570           .doit = ovs_dp_cmd_get,
1571           .dumpit = ovs_dp_cmd_dump
1572         },
1573         { .cmd = OVS_DP_CMD_SET,
1574           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1575           .policy = datapath_policy,
1576           .doit = ovs_dp_cmd_set,
1577         },
1578 };
1579
1580 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1581 #ifdef HAVE_NLA_NUL_STRING
1582         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1583         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1584         [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1585 #else
1586         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1587         [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1588 #endif
1589         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1590         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1591         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1592         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1593 };
1594
1595 static struct genl_family dp_vport_genl_family = {
1596         .id = GENL_ID_GENERATE,
1597         .hdrsize = sizeof(struct ovs_header),
1598         .name = OVS_VPORT_FAMILY,
1599         .version = OVS_VPORT_VERSION,
1600         .maxattr = OVS_VPORT_ATTR_MAX
1601 };
1602
1603 struct genl_multicast_group dp_vport_multicast_group = {
1604         .name = OVS_VPORT_MCGROUP
1605 };
1606
1607 /* Called with RTNL lock or RCU read lock. */
1608 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1609                                    u32 pid, u32 seq, u32 flags, u8 cmd)
1610 {
1611         struct ovs_header *ovs_header;
1612         struct ovs_vport_stats vport_stats;
1613         int err;
1614
1615         ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1616                                  flags, cmd);
1617         if (!ovs_header)
1618                 return -EMSGSIZE;
1619
1620         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1621
1622         NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1623         NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1624         NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1625         NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1626
1627         vport_get_stats(vport, &vport_stats);
1628         NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1629                 &vport_stats);
1630
1631         NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1632                 vport->ops->get_addr(vport));
1633
1634         err = vport_get_options(vport, skb);
1635         if (err == -EMSGSIZE)
1636                 goto error;
1637
1638         return genlmsg_end(skb, ovs_header);
1639
1640 nla_put_failure:
1641         err = -EMSGSIZE;
1642 error:
1643         genlmsg_cancel(skb, ovs_header);
1644         return err;
1645 }
1646
1647 /* Called with RTNL lock or RCU read lock. */
1648 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1649                                          u32 seq, u8 cmd)
1650 {
1651         struct sk_buff *skb;
1652         int retval;
1653
1654         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1655         if (!skb)
1656                 return ERR_PTR(-ENOMEM);
1657
1658         retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1659         if (retval < 0) {
1660                 kfree_skb(skb);
1661                 return ERR_PTR(retval);
1662         }
1663         return skb;
1664 }
1665
1666 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1667 {
1668         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1669 }
1670
1671 /* Called with RTNL lock or RCU read lock. */
1672 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1673                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1674 {
1675         struct datapath *dp;
1676         struct vport *vport;
1677
1678         if (a[OVS_VPORT_ATTR_NAME]) {
1679                 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1680                 if (!vport)
1681                         return ERR_PTR(-ENODEV);
1682                 return vport;
1683         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1684                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1685
1686                 if (port_no >= DP_MAX_PORTS)
1687                         return ERR_PTR(-EFBIG);
1688
1689                 dp = get_dp(ovs_header->dp_ifindex);
1690                 if (!dp)
1691                         return ERR_PTR(-ENODEV);
1692
1693                 vport = rcu_dereference_rtnl(dp->ports[port_no]);
1694                 if (!vport)
1695                         return ERR_PTR(-ENOENT);
1696                 return vport;
1697         } else
1698                 return ERR_PTR(-EINVAL);
1699 }
1700
1701 /* Called with RTNL lock. */
1702 static int change_vport(struct vport *vport,
1703                         struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1704 {
1705         int err = 0;
1706
1707         if (a[OVS_VPORT_ATTR_STATS])
1708                 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1709
1710         if (a[OVS_VPORT_ATTR_ADDRESS])
1711                 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1712
1713         return err;
1714 }
1715
1716 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1717 {
1718         struct nlattr **a = info->attrs;
1719         struct ovs_header *ovs_header = info->userhdr;
1720         struct vport_parms parms;
1721         struct sk_buff *reply;
1722         struct vport *vport;
1723         struct datapath *dp;
1724         u32 port_no;
1725         int err;
1726
1727         err = -EINVAL;
1728         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1729             !a[OVS_VPORT_ATTR_UPCALL_PID])
1730                 goto exit;
1731
1732         err = ovs_vport_cmd_validate(a);
1733         if (err)
1734                 goto exit;
1735
1736         rtnl_lock();
1737         dp = get_dp(ovs_header->dp_ifindex);
1738         err = -ENODEV;
1739         if (!dp)
1740                 goto exit_unlock;
1741
1742         if (a[OVS_VPORT_ATTR_PORT_NO]) {
1743                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1744
1745                 err = -EFBIG;
1746                 if (port_no >= DP_MAX_PORTS)
1747                         goto exit_unlock;
1748
1749                 vport = rtnl_dereference(dp->ports[port_no]);
1750                 err = -EBUSY;
1751                 if (vport)
1752                         goto exit_unlock;
1753         } else {
1754                 for (port_no = 1; ; port_no++) {
1755                         if (port_no >= DP_MAX_PORTS) {
1756                                 err = -EFBIG;
1757                                 goto exit_unlock;
1758                         }
1759                         vport = rtnl_dereference(dp->ports[port_no]);
1760                         if (!vport)
1761                                 break;
1762                 }
1763         }
1764
1765         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1766         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1767         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1768         parms.dp = dp;
1769         parms.port_no = port_no;
1770         parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1771
1772         vport = new_vport(&parms);
1773         err = PTR_ERR(vport);
1774         if (IS_ERR(vport))
1775                 goto exit_unlock;
1776
1777         dp_sysfs_add_if(vport);
1778
1779         err = change_vport(vport, a);
1780         if (!err) {
1781                 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1782                                                  info->snd_seq,
1783                                                  OVS_VPORT_CMD_NEW);
1784                 if (IS_ERR(reply))
1785                         err = PTR_ERR(reply);
1786         }
1787         if (err) {
1788                 dp_detach_port(vport);
1789                 goto exit_unlock;
1790         }
1791         genl_notify(reply, genl_info_net(info), info->snd_pid,
1792                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1793
1794
1795 exit_unlock:
1796         rtnl_unlock();
1797 exit:
1798         return err;
1799 }
1800
1801 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1802 {
1803         struct nlattr **a = info->attrs;
1804         struct sk_buff *reply;
1805         struct vport *vport;
1806         int err;
1807
1808         err = ovs_vport_cmd_validate(a);
1809         if (err)
1810                 goto exit;
1811
1812         rtnl_lock();
1813         vport = lookup_vport(info->userhdr, a);
1814         err = PTR_ERR(vport);
1815         if (IS_ERR(vport))
1816                 goto exit_unlock;
1817
1818         err = 0;
1819         if (a[OVS_VPORT_ATTR_TYPE] &&
1820             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1821                 err = -EINVAL;
1822
1823         if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1824                 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1825         if (!err)
1826                 err = change_vport(vport, a);
1827         if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1828                 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1829
1830         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1831                                          OVS_VPORT_CMD_NEW);
1832         if (IS_ERR(reply)) {
1833                 err = PTR_ERR(reply);
1834                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1835                                 dp_vport_multicast_group.id, err);
1836                 return 0;
1837         }
1838
1839         genl_notify(reply, genl_info_net(info), info->snd_pid,
1840                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1841
1842 exit_unlock:
1843         rtnl_unlock();
1844 exit:
1845         return err;
1846 }
1847
1848 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1849 {
1850         struct nlattr **a = info->attrs;
1851         struct sk_buff *reply;
1852         struct vport *vport;
1853         int err;
1854
1855         err = ovs_vport_cmd_validate(a);
1856         if (err)
1857                 goto exit;
1858
1859         rtnl_lock();
1860         vport = lookup_vport(info->userhdr, a);
1861         err = PTR_ERR(vport);
1862         if (IS_ERR(vport))
1863                 goto exit_unlock;
1864
1865         if (vport->port_no == OVSP_LOCAL) {
1866                 err = -EINVAL;
1867                 goto exit_unlock;
1868         }
1869
1870         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1871                                          OVS_VPORT_CMD_DEL);
1872         err = PTR_ERR(reply);
1873         if (IS_ERR(reply))
1874                 goto exit_unlock;
1875
1876         dp_detach_port(vport);
1877
1878         genl_notify(reply, genl_info_net(info), info->snd_pid,
1879                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1880
1881 exit_unlock:
1882         rtnl_unlock();
1883 exit:
1884         return err;
1885 }
1886
1887 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1888 {
1889         struct nlattr **a = info->attrs;
1890         struct ovs_header *ovs_header = info->userhdr;
1891         struct sk_buff *reply;
1892         struct vport *vport;
1893         int err;
1894
1895         err = ovs_vport_cmd_validate(a);
1896         if (err)
1897                 goto exit;
1898
1899         rcu_read_lock();
1900         vport = lookup_vport(ovs_header, a);
1901         err = PTR_ERR(vport);
1902         if (IS_ERR(vport))
1903                 goto exit_unlock;
1904
1905         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1906                                          OVS_VPORT_CMD_NEW);
1907         err = PTR_ERR(reply);
1908         if (IS_ERR(reply))
1909                 goto exit_unlock;
1910
1911         rcu_read_unlock();
1912
1913         return genlmsg_reply(reply, info);
1914
1915 exit_unlock:
1916         rcu_read_unlock();
1917 exit:
1918         return err;
1919 }
1920
1921 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1922 {
1923         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1924         struct datapath *dp;
1925         u32 port_no;
1926         int retval;
1927
1928         dp = get_dp(ovs_header->dp_ifindex);
1929         if (!dp)
1930                 return -ENODEV;
1931
1932         rcu_read_lock();
1933         for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1934                 struct vport *vport;
1935
1936                 vport = rcu_dereference(dp->ports[port_no]);
1937                 if (!vport)
1938                         continue;
1939
1940                 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1941                                             cb->nlh->nlmsg_seq, NLM_F_MULTI,
1942                                             OVS_VPORT_CMD_NEW) < 0)
1943                         break;
1944         }
1945         rcu_read_unlock();
1946
1947         cb->args[0] = port_no;
1948         retval = skb->len;
1949
1950         return retval;
1951 }
1952
1953 static struct genl_ops dp_vport_genl_ops[] = {
1954         { .cmd = OVS_VPORT_CMD_NEW,
1955           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1956           .policy = vport_policy,
1957           .doit = ovs_vport_cmd_new
1958         },
1959         { .cmd = OVS_VPORT_CMD_DEL,
1960           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961           .policy = vport_policy,
1962           .doit = ovs_vport_cmd_del
1963         },
1964         { .cmd = OVS_VPORT_CMD_GET,
1965           .flags = 0,               /* OK for unprivileged users. */
1966           .policy = vport_policy,
1967           .doit = ovs_vport_cmd_get,
1968           .dumpit = ovs_vport_cmd_dump
1969         },
1970         { .cmd = OVS_VPORT_CMD_SET,
1971           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1972           .policy = vport_policy,
1973           .doit = ovs_vport_cmd_set,
1974         },
1975 };
1976
1977 struct genl_family_and_ops {
1978         struct genl_family *family;
1979         struct genl_ops *ops;
1980         int n_ops;
1981         struct genl_multicast_group *group;
1982 };
1983
1984 static const struct genl_family_and_ops dp_genl_families[] = {
1985         { &dp_datapath_genl_family,
1986           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1987           &dp_datapath_multicast_group },
1988         { &dp_vport_genl_family,
1989           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1990           &dp_vport_multicast_group },
1991         { &dp_flow_genl_family,
1992           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1993           &dp_flow_multicast_group },
1994         { &dp_packet_genl_family,
1995           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1996           NULL },
1997 };
1998
1999 static void dp_unregister_genl(int n_families)
2000 {
2001         int i;
2002
2003         for (i = 0; i < n_families; i++)
2004                 genl_unregister_family(dp_genl_families[i].family);
2005 }
2006
2007 static int dp_register_genl(void)
2008 {
2009         int n_registered;
2010         int err;
2011         int i;
2012
2013         n_registered = 0;
2014         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2015                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2016
2017                 err = genl_register_family_with_ops(f->family, f->ops,
2018                                                     f->n_ops);
2019                 if (err)
2020                         goto error;
2021                 n_registered++;
2022
2023                 if (f->group) {
2024                         err = genl_register_mc_group(f->family, f->group);
2025                         if (err)
2026                                 goto error;
2027                 }
2028         }
2029
2030         return 0;
2031
2032 error:
2033         dp_unregister_genl(n_registered);
2034         return err;
2035 }
2036
2037 static int __init dp_init(void)
2038 {
2039         struct sk_buff *dummy_skb;
2040         int err;
2041
2042         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2043
2044         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2045                 VERSION BUILDNR);
2046
2047         err = tnl_init();
2048         if (err)
2049                 goto error;
2050
2051         err = flow_init();
2052         if (err)
2053                 goto error_tnl_exit;
2054
2055         err = vport_init();
2056         if (err)
2057                 goto error_flow_exit;
2058
2059         err = register_netdevice_notifier(&dp_device_notifier);
2060         if (err)
2061                 goto error_vport_exit;
2062
2063         err = dp_register_genl();
2064         if (err < 0)
2065                 goto error_unreg_notifier;
2066
2067         return 0;
2068
2069 error_unreg_notifier:
2070         unregister_netdevice_notifier(&dp_device_notifier);
2071 error_vport_exit:
2072         vport_exit();
2073 error_flow_exit:
2074         flow_exit();
2075 error_tnl_exit:
2076         tnl_exit();
2077 error:
2078         return err;
2079 }
2080
2081 static void dp_cleanup(void)
2082 {
2083         rcu_barrier();
2084         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2085         unregister_netdevice_notifier(&dp_device_notifier);
2086         vport_exit();
2087         flow_exit();
2088         tnl_exit();
2089 }
2090
2091 module_init(dp_init);
2092 module_exit(dp_cleanup);
2093
2094 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2095 MODULE_LICENSE("GPL");