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