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