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