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