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