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