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