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