390acc8a4e5f8a029fbf207b58c6eb45593aab3a
[openvswitch] / datapath / datapath.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010 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/workqueue.h>
43 #include <linux/dmi.h>
44 #include <net/inet_ecn.h>
45 #include <linux/compat.h>
46
47 #include "openvswitch/datapath-protocol.h"
48 #include "datapath.h"
49 #include "actions.h"
50 #include "flow.h"
51 #include "odp-compat.h"
52 #include "table.h"
53 #include "vport-internal_dev.h"
54
55 #include "compat.h"
56
57
58 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
59 EXPORT_SYMBOL(dp_ioctl_hook);
60
61 /* Datapaths.  Protected on the read side by rcu_read_lock, on the write side
62  * by dp_mutex.
63  *
64  * dp_mutex nests inside the RTNL lock: if you need both you must take the RTNL
65  * lock first.
66  *
67  * It is safe to access the datapath and dp_port structures with just
68  * dp_mutex.
69  */
70 static struct datapath *dps[ODP_MAX];
71 static DEFINE_MUTEX(dp_mutex);
72
73 /* We limit the number of times that we pass into dp_process_received_packet()
74  * to avoid blowing out the stack in the event that we have a loop. */
75 struct loop_counter {
76         int count;              /* Count. */
77         bool looping;           /* Loop detected? */
78 };
79
80 #define DP_MAX_LOOPS 5
81
82 /* We use a separate counter for each CPU for both interrupt and non-interrupt
83  * context in order to keep the limit deterministic for a given packet. */
84 struct percpu_loop_counters {
85         struct loop_counter counters[2];
86 };
87
88 static DEFINE_PER_CPU(struct percpu_loop_counters, dp_loop_counters);
89
90 static int new_dp_port(struct datapath *, struct odp_port *, int port_no);
91
92 /* Must be called with rcu_read_lock or dp_mutex. */
93 struct datapath *get_dp(int dp_idx)
94 {
95         if (dp_idx < 0 || dp_idx >= ODP_MAX)
96                 return NULL;
97         return rcu_dereference(dps[dp_idx]);
98 }
99 EXPORT_SYMBOL_GPL(get_dp);
100
101 static struct datapath *get_dp_locked(int dp_idx)
102 {
103         struct datapath *dp;
104
105         mutex_lock(&dp_mutex);
106         dp = get_dp(dp_idx);
107         if (dp)
108                 mutex_lock(&dp->mutex);
109         mutex_unlock(&dp_mutex);
110         return dp;
111 }
112
113 /* Must be called with rcu_read_lock or RTNL lock. */
114 const char *dp_name(const struct datapath *dp)
115 {
116         return vport_get_name(dp->ports[ODPP_LOCAL]->vport);
117 }
118
119 static inline size_t br_nlmsg_size(void)
120 {
121         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
122                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
123                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
124                + nla_total_size(4) /* IFLA_MASTER */
125                + nla_total_size(4) /* IFLA_MTU */
126                + nla_total_size(4) /* IFLA_LINK */
127                + nla_total_size(1); /* IFLA_OPERSTATE */
128 }
129
130 static int dp_fill_ifinfo(struct sk_buff *skb,
131                           const struct dp_port *port,
132                           int event, unsigned int flags)
133 {
134         const struct datapath *dp = port->dp;
135         int ifindex = vport_get_ifindex(port->vport);
136         int iflink = vport_get_iflink(port->vport);
137         struct ifinfomsg *hdr;
138         struct nlmsghdr *nlh;
139
140         if (ifindex < 0)
141                 return ifindex;
142
143         if (iflink < 0)
144                 return iflink;
145
146         nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
147         if (nlh == NULL)
148                 return -EMSGSIZE;
149
150         hdr = nlmsg_data(nlh);
151         hdr->ifi_family = AF_BRIDGE;
152         hdr->__ifi_pad = 0;
153         hdr->ifi_type = ARPHRD_ETHER;
154         hdr->ifi_index = ifindex;
155         hdr->ifi_flags = vport_get_flags(port->vport);
156         hdr->ifi_change = 0;
157
158         NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port->vport));
159         NLA_PUT_U32(skb, IFLA_MASTER, vport_get_ifindex(dp->ports[ODPP_LOCAL]->vport));
160         NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port->vport));
161 #ifdef IFLA_OPERSTATE
162         NLA_PUT_U8(skb, IFLA_OPERSTATE,
163                    vport_is_running(port->vport)
164                         ? vport_get_operstate(port->vport)
165                         : IF_OPER_DOWN);
166 #endif
167
168         NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN,
169                                         vport_get_addr(port->vport));
170
171         if (ifindex != iflink)
172                 NLA_PUT_U32(skb, IFLA_LINK,iflink);
173
174         return nlmsg_end(skb, nlh);
175
176 nla_put_failure:
177         nlmsg_cancel(skb, nlh);
178         return -EMSGSIZE;
179 }
180
181 static void dp_ifinfo_notify(int event, struct dp_port *port)
182 {
183         struct sk_buff *skb;
184         int err = -ENOBUFS;
185
186         skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
187         if (skb == NULL)
188                 goto errout;
189
190         err = dp_fill_ifinfo(skb, port, event, 0);
191         if (err < 0) {
192                 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
193                 WARN_ON(err == -EMSGSIZE);
194                 kfree_skb(skb);
195                 goto errout;
196         }
197         rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
198         return;
199 errout:
200         if (err < 0)
201                 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
202 }
203
204 static void release_dp(struct kobject *kobj)
205 {
206         struct datapath *dp = container_of(kobj, struct datapath, ifobj);
207         kfree(dp);
208 }
209
210 static struct kobj_type dp_ktype = {
211         .release = release_dp
212 };
213
214 static int create_dp(int dp_idx, const char __user *devnamep)
215 {
216         struct odp_port internal_dev_port;
217         char devname[IFNAMSIZ];
218         struct datapath *dp;
219         int err;
220         int i;
221
222         if (devnamep) {
223                 int retval = strncpy_from_user(devname, devnamep, IFNAMSIZ);
224                 if (retval < 0) {
225                         err = -EFAULT;
226                         goto err;
227                 } else if (retval >= IFNAMSIZ) {
228                         err = -ENAMETOOLONG;
229                         goto err;
230                 }
231         } else {
232                 snprintf(devname, sizeof devname, "of%d", dp_idx);
233         }
234
235         rtnl_lock();
236         mutex_lock(&dp_mutex);
237         err = -ENODEV;
238         if (!try_module_get(THIS_MODULE))
239                 goto err_unlock;
240
241         /* Exit early if a datapath with that number already exists.
242          * (We don't use -EEXIST because that's ambiguous with 'devname'
243          * conflicting with an existing network device name.) */
244         err = -EBUSY;
245         if (get_dp(dp_idx))
246                 goto err_put_module;
247
248         err = -ENOMEM;
249         dp = kzalloc(sizeof *dp, GFP_KERNEL);
250         if (dp == NULL)
251                 goto err_put_module;
252         INIT_LIST_HEAD(&dp->port_list);
253         mutex_init(&dp->mutex);
254         dp->dp_idx = dp_idx;
255         for (i = 0; i < DP_N_QUEUES; i++)
256                 skb_queue_head_init(&dp->queues[i]);
257         init_waitqueue_head(&dp->waitqueue);
258
259         /* Initialize kobject for bridge.  This will be added as
260          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
261         dp->ifobj.kset = NULL;
262         kobject_init(&dp->ifobj, &dp_ktype);
263
264         /* Allocate table. */
265         err = -ENOMEM;
266         rcu_assign_pointer(dp->table, tbl_create(0));
267         if (!dp->table)
268                 goto err_free_dp;
269
270         /* Set up our datapath device. */
271         BUILD_BUG_ON(sizeof(internal_dev_port.devname) != sizeof(devname));
272         strcpy(internal_dev_port.devname, devname);
273         internal_dev_port.flags = ODP_PORT_INTERNAL;
274         err = new_dp_port(dp, &internal_dev_port, ODPP_LOCAL);
275         if (err) {
276                 if (err == -EBUSY)
277                         err = -EEXIST;
278
279                 goto err_destroy_table;
280         }
281
282         dp->drop_frags = 0;
283         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
284         if (!dp->stats_percpu)
285                 goto err_destroy_local_port;
286
287         rcu_assign_pointer(dps[dp_idx], dp);
288         mutex_unlock(&dp_mutex);
289         rtnl_unlock();
290
291         dp_sysfs_add_dp(dp);
292
293         return 0;
294
295 err_destroy_local_port:
296         dp_detach_port(dp->ports[ODPP_LOCAL], 1);
297 err_destroy_table:
298         tbl_destroy(dp->table, NULL);
299 err_free_dp:
300         kfree(dp);
301 err_put_module:
302         module_put(THIS_MODULE);
303 err_unlock:
304         mutex_unlock(&dp_mutex);
305         rtnl_unlock();
306 err:
307         return err;
308 }
309
310 static void do_destroy_dp(struct datapath *dp)
311 {
312         struct dp_port *p, *n;
313         int i;
314
315         list_for_each_entry_safe (p, n, &dp->port_list, node)
316                 if (p->port_no != ODPP_LOCAL)
317                         dp_detach_port(p, 1);
318
319         dp_sysfs_del_dp(dp);
320
321         rcu_assign_pointer(dps[dp->dp_idx], NULL);
322
323         dp_detach_port(dp->ports[ODPP_LOCAL], 1);
324
325         tbl_destroy(dp->table, flow_free_tbl);
326
327         for (i = 0; i < DP_N_QUEUES; i++)
328                 skb_queue_purge(&dp->queues[i]);
329         for (i = 0; i < DP_MAX_GROUPS; i++)
330                 kfree(dp->groups[i]);
331         free_percpu(dp->stats_percpu);
332         kobject_put(&dp->ifobj);
333         module_put(THIS_MODULE);
334 }
335
336 static int destroy_dp(int dp_idx)
337 {
338         struct datapath *dp;
339         int err;
340
341         rtnl_lock();
342         mutex_lock(&dp_mutex);
343         dp = get_dp(dp_idx);
344         err = -ENODEV;
345         if (!dp)
346                 goto err_unlock;
347
348         do_destroy_dp(dp);
349         err = 0;
350
351 err_unlock:
352         mutex_unlock(&dp_mutex);
353         rtnl_unlock();
354         return err;
355 }
356
357 static void release_dp_port(struct kobject *kobj)
358 {
359         struct dp_port *p = container_of(kobj, struct dp_port, kobj);
360         kfree(p);
361 }
362
363 static struct kobj_type brport_ktype = {
364 #ifdef CONFIG_SYSFS
365         .sysfs_ops = &brport_sysfs_ops,
366 #endif
367         .release = release_dp_port
368 };
369
370 /* Called with RTNL lock and dp_mutex. */
371 static int new_dp_port(struct datapath *dp, struct odp_port *odp_port, int port_no)
372 {
373         struct vport *vport;
374         struct dp_port *p;
375         int err;
376
377         vport = vport_locate(odp_port->devname);
378         if (!vport) {
379                 vport_lock();
380
381                 if (odp_port->flags & ODP_PORT_INTERNAL)
382                         vport = vport_add(odp_port->devname, "internal", NULL);
383                 else
384                         vport = vport_add(odp_port->devname, "netdev", NULL);
385
386                 vport_unlock();
387
388                 if (IS_ERR(vport))
389                         return PTR_ERR(vport);
390         }
391
392         p = kzalloc(sizeof(*p), GFP_KERNEL);
393         if (!p)
394                 return -ENOMEM;
395
396         p->port_no = port_no;
397         p->dp = dp;
398         p->vport = vport;
399         atomic_set(&p->sflow_pool, 0);
400
401         err = vport_attach(vport, p);
402         if (err) {
403                 kfree(p);
404                 return err;
405         }
406
407         rcu_assign_pointer(dp->ports[port_no], p);
408         list_add_rcu(&p->node, &dp->port_list);
409         dp->n_ports++;
410
411         /* Initialize kobject for bridge.  This will be added as
412          * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
413         p->kobj.kset = NULL;
414         kobject_init(&p->kobj, &brport_ktype);
415
416         dp_ifinfo_notify(RTM_NEWLINK, p);
417
418         return 0;
419 }
420
421 static int attach_port(int dp_idx, struct odp_port __user *portp)
422 {
423         struct datapath *dp;
424         struct odp_port port;
425         int port_no;
426         int err;
427
428         err = -EFAULT;
429         if (copy_from_user(&port, portp, sizeof port))
430                 goto out;
431         port.devname[IFNAMSIZ - 1] = '\0';
432
433         rtnl_lock();
434         dp = get_dp_locked(dp_idx);
435         err = -ENODEV;
436         if (!dp)
437                 goto out_unlock_rtnl;
438
439         for (port_no = 1; port_no < DP_MAX_PORTS; port_no++)
440                 if (!dp->ports[port_no])
441                         goto got_port_no;
442         err = -EFBIG;
443         goto out_unlock_dp;
444
445 got_port_no:
446         err = new_dp_port(dp, &port, port_no);
447         if (err)
448                 goto out_unlock_dp;
449
450         set_internal_devs_mtu(dp);
451         dp_sysfs_add_if(dp->ports[port_no]);
452
453         err = put_user(port_no, &portp->port);
454
455 out_unlock_dp:
456         mutex_unlock(&dp->mutex);
457 out_unlock_rtnl:
458         rtnl_unlock();
459 out:
460         return err;
461 }
462
463 int dp_detach_port(struct dp_port *p, int may_delete)
464 {
465         struct vport *vport = p->vport;
466         int err;
467
468         ASSERT_RTNL();
469
470         if (p->port_no != ODPP_LOCAL)
471                 dp_sysfs_del_if(p);
472         dp_ifinfo_notify(RTM_DELLINK, p);
473
474         /* First drop references to device. */
475         p->dp->n_ports--;
476         list_del_rcu(&p->node);
477         rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
478
479         err = vport_detach(vport);
480         if (err)
481                 return err;
482
483         /* Then wait until no one is still using it, and destroy it. */
484         synchronize_rcu();
485
486         if (may_delete) {
487                 const char *port_type = vport_get_type(vport);
488
489                 if (!strcmp(port_type, "netdev") || !strcmp(port_type, "internal")) {
490                         vport_lock();
491                         vport_del(vport);
492                         vport_unlock();
493                 }
494         }
495
496         kobject_put(&p->kobj);
497
498         return 0;
499 }
500
501 static int detach_port(int dp_idx, int port_no)
502 {
503         struct dp_port *p;
504         struct datapath *dp;
505         int err;
506
507         err = -EINVAL;
508         if (port_no < 0 || port_no >= DP_MAX_PORTS || port_no == ODPP_LOCAL)
509                 goto out;
510
511         rtnl_lock();
512         dp = get_dp_locked(dp_idx);
513         err = -ENODEV;
514         if (!dp)
515                 goto out_unlock_rtnl;
516
517         p = dp->ports[port_no];
518         err = -ENOENT;
519         if (!p)
520                 goto out_unlock_dp;
521
522         err = dp_detach_port(p, 1);
523
524 out_unlock_dp:
525         mutex_unlock(&dp->mutex);
526 out_unlock_rtnl:
527         rtnl_unlock();
528 out:
529         return err;
530 }
531
532 static void suppress_loop(struct datapath *dp, struct sw_flow_actions *actions)
533 {
534         if (net_ratelimit())
535                 pr_warn("%s: flow looped %d times, dropping\n",
536                         dp_name(dp), DP_MAX_LOOPS);
537         actions->n_actions = 0;
538 }
539
540 /* Must be called with rcu_read_lock. */
541 void dp_process_received_packet(struct dp_port *p, struct sk_buff *skb)
542 {
543         struct datapath *dp = p->dp;
544         struct dp_stats_percpu *stats;
545         int stats_counter_off;
546         struct sw_flow_actions *acts;
547         struct loop_counter *loop;
548         int error;
549
550         OVS_CB(skb)->dp_port = p;
551
552         if (!OVS_CB(skb)->flow) {
553                 struct odp_flow_key key;
554                 struct tbl_node *flow_node;
555
556                 /* Extract flow from 'skb' into 'key'. */
557                 error = flow_extract(skb, p ? p->port_no : ODPP_NONE, &key);
558                 if (unlikely(error)) {
559                         kfree_skb(skb);
560                         return;
561                 }
562
563                 if (OVS_CB(skb)->is_frag && dp->drop_frags) {
564                         kfree_skb(skb);
565                         stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
566                         goto out;
567                 }
568
569                 /* Look up flow. */
570                 flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
571                                         flow_hash(&key), flow_cmp);
572                 if (unlikely(!flow_node)) {
573                         dp_output_control(dp, skb, _ODPL_MISS_NR, OVS_CB(skb)->tun_id);
574                         stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
575                         goto out;
576                 }
577
578                 OVS_CB(skb)->flow = flow_cast(flow_node);
579         }
580
581         flow_used(OVS_CB(skb)->flow, skb);
582
583         acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
584
585         /* Check whether we've looped too much. */
586         loop = &get_cpu_var(dp_loop_counters).counters[!!in_interrupt()];
587         if (unlikely(++loop->count > DP_MAX_LOOPS))
588                 loop->looping = true;
589         if (unlikely(loop->looping)) {
590                 suppress_loop(dp, acts);
591                 goto out_loop;
592         }
593
594         /* Execute actions. */
595         execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
596                         acts->n_actions, GFP_ATOMIC);
597         stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
598
599         /* Check whether sub-actions looped too much. */
600         if (unlikely(loop->looping))
601                 suppress_loop(dp, acts);
602
603 out_loop:
604         /* Decrement loop counter. */
605         if (!--loop->count)
606                 loop->looping = false;
607         put_cpu_var(dp_loop_counters);
608
609 out:
610         /* Update datapath statistics. */
611         local_bh_disable();
612         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
613
614         write_seqcount_begin(&stats->seqlock);
615         (*(u64 *)((u8 *)stats + stats_counter_off))++;
616         write_seqcount_end(&stats->seqlock);
617
618         local_bh_enable();
619 }
620
621 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
622 /* This code is based on skb_checksum_setup() from Xen's net/dev/core.c.  We
623  * can't call this function directly because it isn't exported in all
624  * versions. */
625 int vswitch_skb_checksum_setup(struct sk_buff *skb)
626 {
627         struct iphdr *iph;
628         unsigned char *th;
629         int err = -EPROTO;
630         __u16 csum_start, csum_offset;
631
632         if (!skb->proto_csum_blank)
633                 return 0;
634
635         if (skb->protocol != htons(ETH_P_IP))
636                 goto out;
637
638         if (!pskb_may_pull(skb, skb_network_header(skb) + sizeof(struct iphdr) - skb->data))
639                 goto out;
640
641         iph = ip_hdr(skb);
642         th = skb_network_header(skb) + 4 * iph->ihl;
643
644         csum_start = th - skb->head;
645         switch (iph->protocol) {
646         case IPPROTO_TCP:
647                 csum_offset = offsetof(struct tcphdr, check);
648                 break;
649         case IPPROTO_UDP:
650                 csum_offset = offsetof(struct udphdr, check);
651                 break;
652         default:
653                 if (net_ratelimit())
654                         pr_err("Attempting to checksum a non-TCP/UDP packet, "
655                                "dropping a protocol %d packet",
656                                iph->protocol);
657                 goto out;
658         }
659
660         if (!pskb_may_pull(skb, th + csum_offset + 2 - skb->data))
661                 goto out;
662
663         skb->ip_summed = CHECKSUM_PARTIAL;
664         skb->proto_csum_blank = 0;
665
666 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
667         skb->csum_start = csum_start;
668         skb->csum_offset = csum_offset;
669 #else
670         skb_set_transport_header(skb, csum_start - skb_headroom(skb));
671         skb->csum = csum_offset;
672 #endif
673
674         err = 0;
675
676 out:
677         return err;
678 }
679 #endif /* CONFIG_XEN && HAVE_PROTO_DATA_VALID */
680
681  /* Types of checksums that we can receive (these all refer to L4 checksums):
682  * 1. CHECKSUM_NONE: Device that did not compute checksum, contains full
683  *      (though not verified) checksum in packet but not in skb->csum.  Packets
684  *      from the bridge local port will also have this type.
685  * 2. CHECKSUM_COMPLETE (CHECKSUM_HW): Good device that computes checksums,
686  *      also the GRE module.  This is the same as CHECKSUM_NONE, except it has
687  *      a valid skb->csum.  Importantly, both contain a full checksum (not
688  *      verified) in the packet itself.  The only difference is that if the
689  *      packet gets to L4 processing on this machine (not in DomU) we won't
690  *      have to recompute the checksum to verify.  Most hardware devices do not
691  *      produce packets with this type, even if they support receive checksum
692  *      offloading (they produce type #5).
693  * 3. CHECKSUM_PARTIAL (CHECKSUM_HW): Packet without full checksum and needs to
694  *      be computed if it is sent off box.  Unfortunately on earlier kernels,
695  *      this case is impossible to distinguish from #2, despite having opposite
696  *      meanings.  Xen adds an extra field on earlier kernels (see #4) in order
697  *      to distinguish the different states.
698  * 4. CHECKSUM_UNNECESSARY (with proto_csum_blank true): This packet was
699  *      generated locally by a Xen DomU and has a partial checksum.  If it is
700  *      handled on this machine (Dom0 or DomU), then the checksum will not be
701  *      computed.  If it goes off box, the checksum in the packet needs to be
702  *      completed.  Calling skb_checksum_setup converts this to CHECKSUM_HW
703  *      (CHECKSUM_PARTIAL) so that the checksum can be completed.  In later
704  *      kernels, this combination is replaced with CHECKSUM_PARTIAL.
705  * 5. CHECKSUM_UNNECESSARY (with proto_csum_blank false): Packet with a correct
706  *      full checksum or using a protocol without a checksum.  skb->csum is
707  *      undefined.  This is common from devices with receive checksum
708  *      offloading.  This is somewhat similar to CHECKSUM_NONE, except that
709  *      nobody will try to verify the checksum with CHECKSUM_UNNECESSARY.
710  *
711  * Note that on earlier kernels, CHECKSUM_COMPLETE and CHECKSUM_PARTIAL are
712  * both defined as CHECKSUM_HW.  Normally the meaning of CHECKSUM_HW is clear
713  * based on whether it is on the transmit or receive path.  After the datapath
714  * it will be intepreted as CHECKSUM_PARTIAL.  If the packet already has a
715  * checksum, we will panic.  Since we can receive packets with checksums, we
716  * assume that all CHECKSUM_HW packets have checksums and map them to
717  * CHECKSUM_NONE, which has a similar meaning (the it is only different if the
718  * packet is processed by the local IP stack, in which case it will need to
719  * be reverified).  If we receive a packet with CHECKSUM_HW that really means
720  * CHECKSUM_PARTIAL, it will be sent with the wrong checksum.  However, there
721  * shouldn't be any devices that do this with bridging. */
722 void compute_ip_summed(struct sk_buff *skb, bool xmit)
723 {
724         /* For our convenience these defines change repeatedly between kernel
725          * versions, so we can't just copy them over... */
726         switch (skb->ip_summed) {
727         case CHECKSUM_NONE:
728                 OVS_CB(skb)->ip_summed = OVS_CSUM_NONE;
729                 break;
730         case CHECKSUM_UNNECESSARY:
731                 OVS_CB(skb)->ip_summed = OVS_CSUM_UNNECESSARY;
732                 break;
733 #ifdef CHECKSUM_HW
734         /* In theory this could be either CHECKSUM_PARTIAL or CHECKSUM_COMPLETE.
735          * However, on the receive side we should only get CHECKSUM_PARTIAL
736          * packets from Xen, which uses some special fields to represent this
737          * (see below).  Since we can only make one type work, pick the one
738          * that actually happens in practice.
739          *
740          * On the transmit side (basically after skb_checksum_setup()
741          * has been run or on internal dev transmit), packets with
742          * CHECKSUM_COMPLETE aren't generated, so assume CHECKSUM_PARTIAL. */
743         case CHECKSUM_HW:
744                 if (!xmit)
745                         OVS_CB(skb)->ip_summed = OVS_CSUM_COMPLETE;
746                 else
747                         OVS_CB(skb)->ip_summed = OVS_CSUM_PARTIAL;
748
749                 break;
750 #else
751         case CHECKSUM_COMPLETE:
752                 OVS_CB(skb)->ip_summed = OVS_CSUM_COMPLETE;
753                 break;
754         case CHECKSUM_PARTIAL:
755                 OVS_CB(skb)->ip_summed = OVS_CSUM_PARTIAL;
756                 break;
757 #endif
758         default:
759                 pr_err("unknown checksum type %d\n", skb->ip_summed);
760                 /* None seems the safest... */
761                 OVS_CB(skb)->ip_summed = OVS_CSUM_NONE;
762         }
763
764 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
765         /* Xen has a special way of representing CHECKSUM_PARTIAL on older
766          * kernels. It should not be set on the transmit path though. */
767         if (skb->proto_csum_blank)
768                 OVS_CB(skb)->ip_summed = OVS_CSUM_PARTIAL;
769
770         WARN_ON_ONCE(skb->proto_csum_blank && xmit);
771 #endif
772 }
773
774 /* This function closely resembles skb_forward_csum() used by the bridge.  It
775  * is slightly different because we are only concerned with bridging and not
776  * other types of forwarding and can get away with slightly more optimal
777  * behavior.*/
778 void forward_ip_summed(struct sk_buff *skb)
779 {
780 #ifdef CHECKSUM_HW
781         if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
782                 skb->ip_summed = CHECKSUM_NONE;
783 #endif
784 }
785
786 /* Append each packet in 'skb' list to 'queue'.  There will be only one packet
787  * unless we broke up a GSO packet. */
788 static int queue_control_packets(struct sk_buff *skb, struct sk_buff_head *queue,
789                                  int queue_no, u32 arg)
790 {
791         struct sk_buff *nskb;
792         int port_no;
793         int err;
794
795         if (OVS_CB(skb)->dp_port)
796                 port_no = OVS_CB(skb)->dp_port->port_no;
797         else
798                 port_no = ODPP_LOCAL;
799
800         do {
801                 struct odp_msg *header;
802
803                 nskb = skb->next;
804                 skb->next = NULL;
805
806                 err = skb_cow(skb, sizeof *header);
807                 if (err)
808                         goto err_kfree_skbs;
809
810                 header = (struct odp_msg*)__skb_push(skb, sizeof *header);
811                 header->type = queue_no;
812                 header->length = skb->len;
813                 header->port = port_no;
814                 header->reserved = 0;
815                 header->arg = arg;
816                 skb_queue_tail(queue, skb);
817
818                 skb = nskb;
819         } while (skb);
820         return 0;
821
822 err_kfree_skbs:
823         kfree_skb(skb);
824         while ((skb = nskb) != NULL) {
825                 nskb = skb->next;
826                 kfree_skb(skb);
827         }
828         return err;
829 }
830
831 int dp_output_control(struct datapath *dp, struct sk_buff *skb, int queue_no,
832                       u32 arg)
833 {
834         struct dp_stats_percpu *stats;
835         struct sk_buff_head *queue;
836         int err;
837
838         WARN_ON_ONCE(skb_shared(skb));
839         BUG_ON(queue_no != _ODPL_MISS_NR && queue_no != _ODPL_ACTION_NR && queue_no != _ODPL_SFLOW_NR);
840         queue = &dp->queues[queue_no];
841         err = -ENOBUFS;
842         if (skb_queue_len(queue) >= DP_MAX_QUEUE_LEN)
843                 goto err_kfree_skb;
844
845         forward_ip_summed(skb);
846
847         err = vswitch_skb_checksum_setup(skb);
848         if (err)
849                 goto err_kfree_skb;
850
851         /* Break apart GSO packets into their component pieces.  Otherwise
852          * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
853         if (skb_is_gso(skb)) {
854                 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
855                 if (nskb) {
856                         kfree_skb(skb);
857                         skb = nskb;
858                         if (unlikely(IS_ERR(skb))) {
859                                 err = PTR_ERR(skb);
860                                 goto err;
861                         }
862                 } else {
863                         /* XXX This case might not be possible.  It's hard to
864                          * tell from the skb_gso_segment() code and comment. */
865                 }
866         }
867
868         err = queue_control_packets(skb, queue, queue_no, arg);
869         wake_up_interruptible(&dp->waitqueue);
870         return err;
871
872 err_kfree_skb:
873         kfree_skb(skb);
874 err:
875         local_bh_disable();
876         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
877
878         write_seqcount_begin(&stats->seqlock);
879         stats->n_lost++;
880         write_seqcount_end(&stats->seqlock);
881
882         local_bh_enable();
883
884         return err;
885 }
886
887 static int flush_flows(struct datapath *dp)
888 {
889         struct tbl *old_table = rcu_dereference(dp->table);
890         struct tbl *new_table;
891
892         new_table = tbl_create(0);
893         if (!new_table)
894                 return -ENOMEM;
895
896         rcu_assign_pointer(dp->table, new_table);
897
898         tbl_deferred_destroy(old_table, flow_free_tbl);
899
900         return 0;
901 }
902
903 static int validate_actions(const struct sw_flow_actions *actions)
904 {
905         unsigned int i;
906
907         for (i = 0; i < actions->n_actions; i++) {
908                 const union odp_action *a = &actions->actions[i];
909                 switch (a->type) {
910                 case ODPAT_OUTPUT:
911                         if (a->output.port >= DP_MAX_PORTS)
912                                 return -EINVAL;
913                         break;
914
915                 case ODPAT_OUTPUT_GROUP:
916                         if (a->output_group.group >= DP_MAX_GROUPS)
917                                 return -EINVAL;
918                         break;
919
920                 case ODPAT_SET_VLAN_VID:
921                         if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK))
922                                 return -EINVAL;
923                         break;
924
925                 case ODPAT_SET_VLAN_PCP:
926                         if (a->vlan_pcp.vlan_pcp
927                             & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT))
928                                 return -EINVAL;
929                         break;
930
931                 case ODPAT_SET_NW_TOS:
932                         if (a->nw_tos.nw_tos & INET_ECN_MASK)
933                                 return -EINVAL;
934                         break;
935
936                 default:
937                         if (a->type >= ODPAT_N_ACTIONS)
938                                 return -EOPNOTSUPP;
939                         break;
940                 }
941         }
942
943         return 0;
944 }
945
946 static struct sw_flow_actions *get_actions(const struct odp_flow *flow)
947 {
948         struct sw_flow_actions *actions;
949         int error;
950
951         actions = flow_actions_alloc(flow->n_actions);
952         error = PTR_ERR(actions);
953         if (IS_ERR(actions))
954                 goto error;
955
956         error = -EFAULT;
957         if (copy_from_user(actions->actions, flow->actions,
958                            flow->n_actions * sizeof(union odp_action)))
959                 goto error_free_actions;
960         error = validate_actions(actions);
961         if (error)
962                 goto error_free_actions;
963
964         return actions;
965
966 error_free_actions:
967         kfree(actions);
968 error:
969         return ERR_PTR(error);
970 }
971
972 static struct timespec get_time_offset(void)
973 {
974         struct timespec now_mono, now_jiffies;
975
976         ktime_get_ts(&now_mono);
977         jiffies_to_timespec(jiffies, &now_jiffies);
978         return timespec_sub(now_mono, now_jiffies);
979 }
980
981 static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats,
982                       struct timespec time_offset)
983 {
984         if (flow->used) {
985                 struct timespec flow_ts, used;
986
987                 jiffies_to_timespec(flow->used, &flow_ts);
988                 set_normalized_timespec(&used, flow_ts.tv_sec + time_offset.tv_sec,
989                                         flow_ts.tv_nsec + time_offset.tv_nsec);
990
991                 stats->used_sec = used.tv_sec;
992                 stats->used_nsec = used.tv_nsec;
993         } else {
994                 stats->used_sec = 0;
995                 stats->used_nsec = 0;
996         }
997
998         stats->n_packets = flow->packet_count;
999         stats->n_bytes = flow->byte_count;
1000         stats->reserved = 0;
1001         stats->tcp_flags = flow->tcp_flags;
1002         stats->error = 0;
1003 }
1004
1005 static void clear_stats(struct sw_flow *flow)
1006 {
1007         flow->used = 0;
1008         flow->tcp_flags = 0;
1009         flow->packet_count = 0;
1010         flow->byte_count = 0;
1011 }
1012
1013 static int expand_table(struct datapath *dp)
1014 {
1015         struct tbl *old_table = rcu_dereference(dp->table);
1016         struct tbl *new_table;
1017
1018         new_table = tbl_expand(old_table);
1019         if (IS_ERR(new_table))
1020                 return PTR_ERR(new_table);
1021
1022         rcu_assign_pointer(dp->table, new_table);
1023         tbl_deferred_destroy(old_table, NULL);
1024
1025         return 0;
1026 }
1027
1028 static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf,
1029                        struct odp_flow_stats *stats)
1030 {
1031         struct tbl_node *flow_node;
1032         struct sw_flow *flow;
1033         struct tbl *table;
1034         int error;
1035
1036         memset(uf->flow.key.reserved, 0, sizeof uf->flow.key.reserved);
1037
1038         table = rcu_dereference(dp->table);
1039         flow_node = tbl_lookup(table, &uf->flow.key, flow_hash(&uf->flow.key), flow_cmp);
1040         if (!flow_node) {
1041                 /* No such flow. */
1042                 struct sw_flow_actions *acts;
1043
1044                 error = -ENOENT;
1045                 if (!(uf->flags & ODPPF_CREATE))
1046                         goto error;
1047
1048                 /* Expand table, if necessary, to make room. */
1049                 if (tbl_count(table) >= tbl_n_buckets(table)) {
1050                         error = expand_table(dp);
1051                         if (error)
1052                                 goto error;
1053                         table = rcu_dereference(dp->table);
1054                 }
1055
1056                 /* Allocate flow. */
1057                 flow = flow_alloc();
1058                 if (IS_ERR(flow)) {
1059                         error = PTR_ERR(flow);
1060                         goto error;
1061                 }
1062                 flow->key = uf->flow.key;
1063                 clear_stats(flow);
1064
1065                 /* Obtain actions. */
1066                 acts = get_actions(&uf->flow);
1067                 error = PTR_ERR(acts);
1068                 if (IS_ERR(acts))
1069                         goto error_free_flow;
1070                 rcu_assign_pointer(flow->sf_acts, acts);
1071
1072                 /* Put flow in bucket. */
1073                 error = tbl_insert(table, &flow->tbl_node, flow_hash(&flow->key));
1074                 if (error)
1075                         goto error_free_flow_acts;
1076
1077                 memset(stats, 0, sizeof(struct odp_flow_stats));
1078         } else {
1079                 /* We found a matching flow. */
1080                 struct sw_flow_actions *old_acts, *new_acts;
1081
1082                 flow = flow_cast(flow_node);
1083
1084                 /* Bail out if we're not allowed to modify an existing flow. */
1085                 error = -EEXIST;
1086                 if (!(uf->flags & ODPPF_MODIFY))
1087                         goto error;
1088
1089                 /* Swap actions. */
1090                 new_acts = get_actions(&uf->flow);
1091                 error = PTR_ERR(new_acts);
1092                 if (IS_ERR(new_acts))
1093                         goto error;
1094                 old_acts = rcu_dereference(flow->sf_acts);
1095                 if (old_acts->n_actions != new_acts->n_actions ||
1096                     memcmp(old_acts->actions, new_acts->actions,
1097                            sizeof(union odp_action) * old_acts->n_actions)) {
1098                         rcu_assign_pointer(flow->sf_acts, new_acts);
1099                         flow_deferred_free_acts(old_acts);
1100                 } else {
1101                         kfree(new_acts);
1102                 }
1103
1104                 /* Fetch stats, then clear them if necessary. */
1105                 spin_lock_bh(&flow->lock);
1106                 get_stats(flow, stats, get_time_offset());
1107                 if (uf->flags & ODPPF_ZERO_STATS)
1108                         clear_stats(flow);
1109                 spin_unlock_bh(&flow->lock);
1110         }
1111
1112         return 0;
1113
1114 error_free_flow_acts:
1115         kfree(flow->sf_acts);
1116 error_free_flow:
1117         flow->sf_acts = NULL;
1118         flow_put(flow);
1119 error:
1120         return error;
1121 }
1122
1123 static int put_flow(struct datapath *dp, struct odp_flow_put __user *ufp)
1124 {
1125         struct odp_flow_stats stats;
1126         struct odp_flow_put uf;
1127         int error;
1128
1129         if (copy_from_user(&uf, ufp, sizeof(struct odp_flow_put)))
1130                 return -EFAULT;
1131
1132         error = do_put_flow(dp, &uf, &stats);
1133         if (error)
1134                 return error;
1135
1136         if (copy_to_user(&ufp->flow.stats, &stats,
1137                          sizeof(struct odp_flow_stats)))
1138                 return -EFAULT;
1139
1140         return 0;
1141 }
1142
1143 static int do_answer_query(struct sw_flow *flow, u32 query_flags,
1144                            struct timespec time_offset,
1145                            struct odp_flow_stats __user *ustats,
1146                            union odp_action __user *actions,
1147                            u32 __user *n_actionsp)
1148 {
1149         struct sw_flow_actions *sf_acts;
1150         struct odp_flow_stats stats;
1151         u32 n_actions;
1152
1153         spin_lock_bh(&flow->lock);
1154         get_stats(flow, &stats, time_offset);
1155         if (query_flags & ODPFF_ZERO_TCP_FLAGS)
1156                 flow->tcp_flags = 0;
1157
1158         spin_unlock_bh(&flow->lock);
1159
1160         if (copy_to_user(ustats, &stats, sizeof(struct odp_flow_stats)) ||
1161             get_user(n_actions, n_actionsp))
1162                 return -EFAULT;
1163
1164         if (!n_actions)
1165                 return 0;
1166
1167         sf_acts = rcu_dereference(flow->sf_acts);
1168         if (put_user(sf_acts->n_actions, n_actionsp) ||
1169             (actions && copy_to_user(actions, sf_acts->actions,
1170                                      sizeof(union odp_action) *
1171                                      min(sf_acts->n_actions, n_actions))))
1172                 return -EFAULT;
1173
1174         return 0;
1175 }
1176
1177 static int answer_query(struct sw_flow *flow, u32 query_flags,
1178                         struct timespec time_offset,
1179                         struct odp_flow __user *ufp)
1180 {
1181         union odp_action *actions;
1182
1183         if (get_user(actions, &ufp->actions))
1184                 return -EFAULT;
1185
1186         return do_answer_query(flow, query_flags, time_offset,
1187                                &ufp->stats, actions, &ufp->n_actions);
1188 }
1189
1190 static struct sw_flow *do_del_flow(struct datapath *dp, struct odp_flow_key *key)
1191 {
1192         struct tbl *table = rcu_dereference(dp->table);
1193         struct tbl_node *flow_node;
1194         int error;
1195
1196         memset(key->reserved, 0, sizeof key->reserved);
1197         flow_node = tbl_lookup(table, key, flow_hash(key), flow_cmp);
1198         if (!flow_node)
1199                 return ERR_PTR(-ENOENT);
1200
1201         error = tbl_remove(table, flow_node);
1202         if (error)
1203                 return ERR_PTR(error);
1204
1205         /* XXX Returned flow_node's statistics might lose a few packets, since
1206          * other CPUs can be using this flow.  We used to synchronize_rcu() to
1207          * make sure that we get completely accurate stats, but that blows our
1208          * performance, badly. */
1209         return flow_cast(flow_node);
1210 }
1211
1212 static int del_flow(struct datapath *dp, struct odp_flow __user *ufp)
1213 {
1214         struct sw_flow *flow;
1215         struct odp_flow uf;
1216         int error;
1217
1218         if (copy_from_user(&uf, ufp, sizeof uf))
1219                 return -EFAULT;
1220
1221         flow = do_del_flow(dp, &uf.key);
1222         if (IS_ERR(flow))
1223                 return PTR_ERR(flow);
1224
1225         error = answer_query(flow, 0, get_time_offset(), ufp);
1226         flow_deferred_free(flow);
1227         return error;
1228 }
1229
1230 static int do_query_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
1231 {
1232         struct tbl *table = rcu_dereference(dp->table);
1233         struct timespec time_offset;
1234         u32 i;
1235
1236         time_offset = get_time_offset();
1237
1238         for (i = 0; i < flowvec->n_flows; i++) {
1239                 struct odp_flow __user *ufp = &flowvec->flows[i];
1240                 struct odp_flow uf;
1241                 struct tbl_node *flow_node;
1242                 int error;
1243
1244                 if (copy_from_user(&uf, ufp, sizeof uf))
1245                         return -EFAULT;
1246                 memset(uf.key.reserved, 0, sizeof uf.key.reserved);
1247
1248                 flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp);
1249                 if (!flow_node)
1250                         error = put_user(ENOENT, &ufp->stats.error);
1251                 else
1252                         error = answer_query(flow_cast(flow_node), uf.flags, time_offset, ufp);
1253                 if (error)
1254                         return -EFAULT;
1255         }
1256         return flowvec->n_flows;
1257 }
1258
1259 struct list_flows_cbdata {
1260         struct odp_flow __user *uflows;
1261         u32 n_flows;
1262         u32 listed_flows;
1263         struct timespec time_offset;
1264 };
1265
1266 static int list_flow(struct tbl_node *node, void *cbdata_)
1267 {
1268         struct sw_flow *flow = flow_cast(node);
1269         struct list_flows_cbdata *cbdata = cbdata_;
1270         struct odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
1271         int error;
1272
1273         if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
1274                 return -EFAULT;
1275         error = answer_query(flow, 0, cbdata->time_offset, ufp);
1276         if (error)
1277                 return error;
1278
1279         if (cbdata->listed_flows >= cbdata->n_flows)
1280                 return cbdata->listed_flows;
1281         return 0;
1282 }
1283
1284 static int do_list_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
1285 {
1286         struct list_flows_cbdata cbdata;
1287         int error;
1288
1289         if (!flowvec->n_flows)
1290                 return 0;
1291
1292         cbdata.uflows = flowvec->flows;
1293         cbdata.n_flows = flowvec->n_flows;
1294         cbdata.listed_flows = 0;
1295         cbdata.time_offset = get_time_offset();
1296
1297         error = tbl_foreach(rcu_dereference(dp->table), list_flow, &cbdata);
1298         return error ? error : cbdata.listed_flows;
1299 }
1300
1301 static int do_flowvec_ioctl(struct datapath *dp, unsigned long argp,
1302                             int (*function)(struct datapath *,
1303                                             const struct odp_flowvec *))
1304 {
1305         struct odp_flowvec __user *uflowvec;
1306         struct odp_flowvec flowvec;
1307         int retval;
1308
1309         uflowvec = (struct odp_flowvec __user *)argp;
1310         if (copy_from_user(&flowvec, uflowvec, sizeof flowvec))
1311                 return -EFAULT;
1312
1313         if (flowvec.n_flows > INT_MAX / sizeof(struct odp_flow))
1314                 return -EINVAL;
1315
1316         retval = function(dp, &flowvec);
1317         return (retval < 0 ? retval
1318                 : retval == flowvec.n_flows ? 0
1319                 : put_user(retval, &uflowvec->n_flows));
1320 }
1321
1322 static int do_execute(struct datapath *dp, const struct odp_execute *execute)
1323 {
1324         struct odp_flow_key key;
1325         struct sk_buff *skb;
1326         struct sw_flow_actions *actions;
1327         struct ethhdr *eth;
1328         int err;
1329
1330         err = -EINVAL;
1331         if (execute->length < ETH_HLEN || execute->length > 65535)
1332                 goto error;
1333
1334         actions = flow_actions_alloc(execute->n_actions);
1335         if (IS_ERR(actions)) {
1336                 err = PTR_ERR(actions);
1337                 goto error;
1338         }
1339
1340         err = -EFAULT;
1341         if (copy_from_user(actions->actions, execute->actions,
1342                            execute->n_actions * sizeof *execute->actions))
1343                 goto error_free_actions;
1344
1345         err = validate_actions(actions);
1346         if (err)
1347                 goto error_free_actions;
1348
1349         err = -ENOMEM;
1350         skb = alloc_skb(execute->length, GFP_KERNEL);
1351         if (!skb)
1352                 goto error_free_actions;
1353
1354         if (execute->in_port < DP_MAX_PORTS)
1355                 OVS_CB(skb)->dp_port = dp->ports[execute->in_port];
1356         else
1357                 OVS_CB(skb)->dp_port = NULL;
1358
1359         err = -EFAULT;
1360         if (copy_from_user(skb_put(skb, execute->length), execute->data,
1361                            execute->length))
1362                 goto error_free_skb;
1363
1364         skb_reset_mac_header(skb);
1365         eth = eth_hdr(skb);
1366
1367         /* Normally, setting the skb 'protocol' field would be handled by a
1368          * call to eth_type_trans(), but it assumes there's a sending
1369          * device, which we may not have. */
1370         if (ntohs(eth->h_proto) >= 1536)
1371                 skb->protocol = eth->h_proto;
1372         else
1373                 skb->protocol = htons(ETH_P_802_2);
1374
1375         err = flow_extract(skb, execute->in_port, &key);
1376         if (err)
1377                 goto error_free_skb;
1378
1379         rcu_read_lock();
1380         err = execute_actions(dp, skb, &key, actions->actions,
1381                               actions->n_actions, GFP_KERNEL);
1382         rcu_read_unlock();
1383
1384         kfree(actions);
1385         return err;
1386
1387 error_free_skb:
1388         kfree_skb(skb);
1389 error_free_actions:
1390         kfree(actions);
1391 error:
1392         return err;
1393 }
1394
1395 static int execute_packet(struct datapath *dp, const struct odp_execute __user *executep)
1396 {
1397         struct odp_execute execute;
1398
1399         if (copy_from_user(&execute, executep, sizeof execute))
1400                 return -EFAULT;
1401
1402         return do_execute(dp, &execute);
1403 }
1404
1405 static int get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp)
1406 {
1407         struct tbl *table = rcu_dereference(dp->table);
1408         struct odp_stats stats;
1409         int i;
1410
1411         stats.n_flows = tbl_count(table);
1412         stats.cur_capacity = tbl_n_buckets(table);
1413         stats.max_capacity = TBL_MAX_BUCKETS;
1414         stats.n_ports = dp->n_ports;
1415         stats.max_ports = DP_MAX_PORTS;
1416         stats.max_groups = DP_MAX_GROUPS;
1417         stats.n_frags = stats.n_hit = stats.n_missed = stats.n_lost = 0;
1418         for_each_possible_cpu(i) {
1419                 const struct dp_stats_percpu *percpu_stats;
1420                 struct dp_stats_percpu local_stats;
1421                 unsigned seqcount;
1422
1423                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
1424
1425                 do {
1426                         seqcount = read_seqcount_begin(&percpu_stats->seqlock);
1427                         local_stats = *percpu_stats;
1428                 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
1429
1430                 stats.n_frags += local_stats.n_frags;
1431                 stats.n_hit += local_stats.n_hit;
1432                 stats.n_missed += local_stats.n_missed;
1433                 stats.n_lost += local_stats.n_lost;
1434         }
1435         stats.max_miss_queue = DP_MAX_QUEUE_LEN;
1436         stats.max_action_queue = DP_MAX_QUEUE_LEN;
1437         return copy_to_user(statsp, &stats, sizeof stats) ? -EFAULT : 0;
1438 }
1439
1440 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports */
1441 int dp_min_mtu(const struct datapath *dp)
1442 {
1443         struct dp_port *p;
1444         int mtu = 0;
1445
1446         ASSERT_RTNL();
1447
1448         list_for_each_entry_rcu (p, &dp->port_list, node) {
1449                 int dev_mtu;
1450
1451                 /* Skip any internal ports, since that's what we're trying to
1452                  * set. */
1453                 if (is_internal_vport(p->vport))
1454                         continue;
1455
1456                 dev_mtu = vport_get_mtu(p->vport);
1457                 if (!mtu || dev_mtu < mtu)
1458                         mtu = dev_mtu;
1459         }
1460
1461         return mtu ? mtu : ETH_DATA_LEN;
1462 }
1463
1464 /* Sets the MTU of all datapath devices to the minimum of the ports.  Must
1465  * be called with RTNL lock. */
1466 void set_internal_devs_mtu(const struct datapath *dp)
1467 {
1468         struct dp_port *p;
1469         int mtu;
1470
1471         ASSERT_RTNL();
1472
1473         mtu = dp_min_mtu(dp);
1474
1475         list_for_each_entry_rcu (p, &dp->port_list, node) {
1476                 if (is_internal_vport(p->vport))
1477                         vport_set_mtu(p->vport, mtu);
1478         }
1479 }
1480
1481 static int put_port(const struct dp_port *p, struct odp_port __user *uop)
1482 {
1483         struct odp_port op;
1484
1485         memset(&op, 0, sizeof op);
1486
1487         rcu_read_lock();
1488         strncpy(op.devname, vport_get_name(p->vport), sizeof op.devname);
1489         rcu_read_unlock();
1490
1491         op.port = p->port_no;
1492         op.flags = is_internal_vport(p->vport) ? ODP_PORT_INTERNAL : 0;
1493
1494         return copy_to_user(uop, &op, sizeof op) ? -EFAULT : 0;
1495 }
1496
1497 static int query_port(struct datapath *dp, struct odp_port __user *uport)
1498 {
1499         struct odp_port port;
1500
1501         if (copy_from_user(&port, uport, sizeof port))
1502                 return -EFAULT;
1503
1504         if (port.devname[0]) {
1505                 struct vport *vport;
1506                 struct dp_port *dp_port;
1507                 int err = 0;
1508
1509                 port.devname[IFNAMSIZ - 1] = '\0';
1510
1511                 vport_lock();
1512                 rcu_read_lock();
1513
1514                 vport = vport_locate(port.devname);
1515                 if (!vport) {
1516                         err = -ENODEV;
1517                         goto error_unlock;
1518                 }
1519
1520                 dp_port = vport_get_dp_port(vport);
1521                 if (!dp_port || dp_port->dp != dp) {
1522                         err = -ENOENT;
1523                         goto error_unlock;
1524                 }
1525
1526                 port.port = dp_port->port_no;
1527
1528 error_unlock:
1529                 rcu_read_unlock();
1530                 vport_unlock();
1531
1532                 if (err)
1533                         return err;
1534         } else {
1535                 if (port.port >= DP_MAX_PORTS)
1536                         return -EINVAL;
1537                 if (!dp->ports[port.port])
1538                         return -ENOENT;
1539         }
1540
1541         return put_port(dp->ports[port.port], uport);
1542 }
1543
1544 static int do_list_ports(struct datapath *dp, struct odp_port __user *uports,
1545                          int n_ports)
1546 {
1547         int idx = 0;
1548         if (n_ports) {
1549                 struct dp_port *p;
1550
1551                 list_for_each_entry_rcu (p, &dp->port_list, node) {
1552                         if (put_port(p, &uports[idx]))
1553                                 return -EFAULT;
1554                         if (idx++ >= n_ports)
1555                                 break;
1556                 }
1557         }
1558         return idx;
1559 }
1560
1561 static int list_ports(struct datapath *dp, struct odp_portvec __user *upv)
1562 {
1563         struct odp_portvec pv;
1564         int retval;
1565
1566         if (copy_from_user(&pv, upv, sizeof pv))
1567                 return -EFAULT;
1568
1569         retval = do_list_ports(dp, pv.ports, pv.n_ports);
1570         if (retval < 0)
1571                 return retval;
1572
1573         return put_user(retval, &upv->n_ports);
1574 }
1575
1576 /* RCU callback for freeing a dp_port_group */
1577 static void free_port_group(struct rcu_head *rcu)
1578 {
1579         struct dp_port_group *g = container_of(rcu, struct dp_port_group, rcu);
1580         kfree(g);
1581 }
1582
1583 static int do_set_port_group(struct datapath *dp, u16 __user *ports,
1584                              int n_ports, int group)
1585 {
1586         struct dp_port_group *new_group, *old_group;
1587         int error;
1588
1589         error = -EINVAL;
1590         if (n_ports > DP_MAX_PORTS || group >= DP_MAX_GROUPS)
1591                 goto error;
1592
1593         error = -ENOMEM;
1594         new_group = kmalloc(sizeof *new_group + sizeof(u16) * n_ports, GFP_KERNEL);
1595         if (!new_group)
1596                 goto error;
1597
1598         new_group->n_ports = n_ports;
1599         error = -EFAULT;
1600         if (copy_from_user(new_group->ports, ports, sizeof(u16) * n_ports))
1601                 goto error_free;
1602
1603         old_group = rcu_dereference(dp->groups[group]);
1604         rcu_assign_pointer(dp->groups[group], new_group);
1605         if (old_group)
1606                 call_rcu(&old_group->rcu, free_port_group);
1607         return 0;
1608
1609 error_free:
1610         kfree(new_group);
1611 error:
1612         return error;
1613 }
1614
1615 static int set_port_group(struct datapath *dp,
1616                           const struct odp_port_group __user *upg)
1617 {
1618         struct odp_port_group pg;
1619
1620         if (copy_from_user(&pg, upg, sizeof pg))
1621                 return -EFAULT;
1622
1623         return do_set_port_group(dp, pg.ports, pg.n_ports, pg.group);
1624 }
1625
1626 static int do_get_port_group(struct datapath *dp,
1627                              u16 __user *ports, int n_ports, int group,
1628                              u16 __user *n_portsp)
1629 {
1630         struct dp_port_group *g;
1631         u16 n_copy;
1632
1633         if (group >= DP_MAX_GROUPS)
1634                 return -EINVAL;
1635
1636         g = dp->groups[group];
1637         n_copy = g ? min_t(int, g->n_ports, n_ports) : 0;
1638         if (n_copy && copy_to_user(ports, g->ports, n_copy * sizeof(u16)))
1639                 return -EFAULT;
1640
1641         if (put_user(g ? g->n_ports : 0, n_portsp))
1642                 return -EFAULT;
1643
1644         return 0;
1645 }
1646
1647 static int get_port_group(struct datapath *dp, struct odp_port_group __user *upg)
1648 {
1649         struct odp_port_group pg;
1650
1651         if (copy_from_user(&pg, upg, sizeof pg))
1652                 return -EFAULT;
1653
1654         return do_get_port_group(dp, pg.ports, pg.n_ports, pg.group, &upg->n_ports);
1655 }
1656
1657 static int get_listen_mask(const struct file *f)
1658 {
1659         return (long)f->private_data;
1660 }
1661
1662 static void set_listen_mask(struct file *f, int listen_mask)
1663 {
1664         f->private_data = (void*)(long)listen_mask;
1665 }
1666
1667 static long openvswitch_ioctl(struct file *f, unsigned int cmd,
1668                            unsigned long argp)
1669 {
1670         int dp_idx = iminor(f->f_dentry->d_inode);
1671         struct datapath *dp;
1672         int drop_frags, listeners, port_no;
1673         unsigned int sflow_probability;
1674         int err;
1675
1676         /* Handle commands with special locking requirements up front. */
1677         switch (cmd) {
1678         case ODP_DP_CREATE:
1679                 err = create_dp(dp_idx, (char __user *)argp);
1680                 goto exit;
1681
1682         case ODP_DP_DESTROY:
1683                 err = destroy_dp(dp_idx);
1684                 goto exit;
1685
1686         case ODP_PORT_ATTACH:
1687                 err = attach_port(dp_idx, (struct odp_port __user *)argp);
1688                 goto exit;
1689
1690         case ODP_PORT_DETACH:
1691                 err = get_user(port_no, (int __user *)argp);
1692                 if (!err)
1693                         err = detach_port(dp_idx, port_no);
1694                 goto exit;
1695
1696         case ODP_VPORT_ADD:
1697                 err = vport_user_add((struct odp_vport_add __user *)argp);
1698                 goto exit;
1699
1700         case ODP_VPORT_MOD:
1701                 err = vport_user_mod((struct odp_vport_mod __user *)argp);
1702                 goto exit;
1703
1704         case ODP_VPORT_DEL:
1705                 err = vport_user_del((char __user *)argp);
1706                 goto exit;
1707
1708         case ODP_VPORT_STATS_GET:
1709                 err = vport_user_stats_get((struct odp_vport_stats_req __user *)argp);
1710                 goto exit;
1711
1712         case ODP_VPORT_STATS_SET:
1713                 err = vport_user_stats_set((struct odp_vport_stats_req __user *)argp);
1714                 goto exit;
1715
1716         case ODP_VPORT_ETHER_GET:
1717                 err = vport_user_ether_get((struct odp_vport_ether __user *)argp);
1718                 goto exit;
1719
1720         case ODP_VPORT_ETHER_SET:
1721                 err = vport_user_ether_set((struct odp_vport_ether __user *)argp);
1722                 goto exit;
1723
1724         case ODP_VPORT_MTU_GET:
1725                 err = vport_user_mtu_get((struct odp_vport_mtu __user *)argp);
1726                 goto exit;
1727
1728         case ODP_VPORT_MTU_SET:
1729                 err = vport_user_mtu_set((struct odp_vport_mtu __user *)argp);
1730                 goto exit;
1731         }
1732
1733         dp = get_dp_locked(dp_idx);
1734         err = -ENODEV;
1735         if (!dp)
1736                 goto exit;
1737
1738         switch (cmd) {
1739         case ODP_DP_STATS:
1740                 err = get_dp_stats(dp, (struct odp_stats __user *)argp);
1741                 break;
1742
1743         case ODP_GET_DROP_FRAGS:
1744                 err = put_user(dp->drop_frags, (int __user *)argp);
1745                 break;
1746
1747         case ODP_SET_DROP_FRAGS:
1748                 err = get_user(drop_frags, (int __user *)argp);
1749                 if (err)
1750                         break;
1751                 err = -EINVAL;
1752                 if (drop_frags != 0 && drop_frags != 1)
1753                         break;
1754                 dp->drop_frags = drop_frags;
1755                 err = 0;
1756                 break;
1757
1758         case ODP_GET_LISTEN_MASK:
1759                 err = put_user(get_listen_mask(f), (int __user *)argp);
1760                 break;
1761
1762         case ODP_SET_LISTEN_MASK:
1763                 err = get_user(listeners, (int __user *)argp);
1764                 if (err)
1765                         break;
1766                 err = -EINVAL;
1767                 if (listeners & ~ODPL_ALL)
1768                         break;
1769                 err = 0;
1770                 set_listen_mask(f, listeners);
1771                 break;
1772
1773         case ODP_GET_SFLOW_PROBABILITY:
1774                 err = put_user(dp->sflow_probability, (unsigned int __user *)argp);
1775                 break;
1776
1777         case ODP_SET_SFLOW_PROBABILITY:
1778                 err = get_user(sflow_probability, (unsigned int __user *)argp);
1779                 if (!err)
1780                         dp->sflow_probability = sflow_probability;
1781                 break;
1782
1783         case ODP_PORT_QUERY:
1784                 err = query_port(dp, (struct odp_port __user *)argp);
1785                 break;
1786
1787         case ODP_PORT_LIST:
1788                 err = list_ports(dp, (struct odp_portvec __user *)argp);
1789                 break;
1790
1791         case ODP_PORT_GROUP_SET:
1792                 err = set_port_group(dp, (struct odp_port_group __user *)argp);
1793                 break;
1794
1795         case ODP_PORT_GROUP_GET:
1796                 err = get_port_group(dp, (struct odp_port_group __user *)argp);
1797                 break;
1798
1799         case ODP_FLOW_FLUSH:
1800                 err = flush_flows(dp);
1801                 break;
1802
1803         case ODP_FLOW_PUT:
1804                 err = put_flow(dp, (struct odp_flow_put __user *)argp);
1805                 break;
1806
1807         case ODP_FLOW_DEL:
1808                 err = del_flow(dp, (struct odp_flow __user *)argp);
1809                 break;
1810
1811         case ODP_FLOW_GET:
1812                 err = do_flowvec_ioctl(dp, argp, do_query_flows);
1813                 break;
1814
1815         case ODP_FLOW_LIST:
1816                 err = do_flowvec_ioctl(dp, argp, do_list_flows);
1817                 break;
1818
1819         case ODP_EXECUTE:
1820                 err = execute_packet(dp, (struct odp_execute __user *)argp);
1821                 break;
1822
1823         default:
1824                 err = -ENOIOCTLCMD;
1825                 break;
1826         }
1827         mutex_unlock(&dp->mutex);
1828 exit:
1829         return err;
1830 }
1831
1832 static int dp_has_packet_of_interest(struct datapath *dp, int listeners)
1833 {
1834         int i;
1835         for (i = 0; i < DP_N_QUEUES; i++) {
1836                 if (listeners & (1 << i) && !skb_queue_empty(&dp->queues[i]))
1837                         return 1;
1838         }
1839         return 0;
1840 }
1841
1842 #ifdef CONFIG_COMPAT
1843 static int compat_list_ports(struct datapath *dp, struct compat_odp_portvec __user *upv)
1844 {
1845         struct compat_odp_portvec pv;
1846         int retval;
1847
1848         if (copy_from_user(&pv, upv, sizeof pv))
1849                 return -EFAULT;
1850
1851         retval = do_list_ports(dp, compat_ptr(pv.ports), pv.n_ports);
1852         if (retval < 0)
1853                 return retval;
1854
1855         return put_user(retval, &upv->n_ports);
1856 }
1857
1858 static int compat_set_port_group(struct datapath *dp, const struct compat_odp_port_group __user *upg)
1859 {
1860         struct compat_odp_port_group pg;
1861
1862         if (copy_from_user(&pg, upg, sizeof pg))
1863                 return -EFAULT;
1864
1865         return do_set_port_group(dp, compat_ptr(pg.ports), pg.n_ports, pg.group);
1866 }
1867
1868 static int compat_get_port_group(struct datapath *dp, struct compat_odp_port_group __user *upg)
1869 {
1870         struct compat_odp_port_group pg;
1871
1872         if (copy_from_user(&pg, upg, sizeof pg))
1873                 return -EFAULT;
1874
1875         return do_get_port_group(dp, compat_ptr(pg.ports), pg.n_ports,
1876                                  pg.group, &upg->n_ports);
1877 }
1878
1879 static int compat_get_flow(struct odp_flow *flow, const struct compat_odp_flow __user *compat)
1880 {
1881         compat_uptr_t actions;
1882
1883         if (!access_ok(VERIFY_READ, compat, sizeof(struct compat_odp_flow)) ||
1884             __copy_from_user(&flow->stats, &compat->stats, sizeof(struct odp_flow_stats)) ||
1885             __copy_from_user(&flow->key, &compat->key, sizeof(struct odp_flow_key)) ||
1886             __get_user(actions, &compat->actions) ||
1887             __get_user(flow->n_actions, &compat->n_actions) ||
1888             __get_user(flow->flags, &compat->flags))
1889                 return -EFAULT;
1890
1891         flow->actions = compat_ptr(actions);
1892         return 0;
1893 }
1894
1895 static int compat_put_flow(struct datapath *dp, struct compat_odp_flow_put __user *ufp)
1896 {
1897         struct odp_flow_stats stats;
1898         struct odp_flow_put fp;
1899         int error;
1900
1901         if (compat_get_flow(&fp.flow, &ufp->flow) ||
1902             get_user(fp.flags, &ufp->flags))
1903                 return -EFAULT;
1904
1905         error = do_put_flow(dp, &fp, &stats);
1906         if (error)
1907                 return error;
1908
1909         if (copy_to_user(&ufp->flow.stats, &stats,
1910                          sizeof(struct odp_flow_stats)))
1911                 return -EFAULT;
1912
1913         return 0;
1914 }
1915
1916 static int compat_answer_query(struct sw_flow *flow, u32 query_flags,
1917                                struct timespec time_offset,
1918                                struct compat_odp_flow __user *ufp)
1919 {
1920         compat_uptr_t actions;
1921
1922         if (get_user(actions, &ufp->actions))
1923                 return -EFAULT;
1924
1925         return do_answer_query(flow, query_flags, time_offset, &ufp->stats,
1926                                compat_ptr(actions), &ufp->n_actions);
1927 }
1928
1929 static int compat_del_flow(struct datapath *dp, struct compat_odp_flow __user *ufp)
1930 {
1931         struct sw_flow *flow;
1932         struct odp_flow uf;
1933         int error;
1934
1935         if (compat_get_flow(&uf, ufp))
1936                 return -EFAULT;
1937
1938         flow = do_del_flow(dp, &uf.key);
1939         if (IS_ERR(flow))
1940                 return PTR_ERR(flow);
1941
1942         error = compat_answer_query(flow, 0, get_time_offset(), ufp);
1943         flow_deferred_free(flow);
1944         return error;
1945 }
1946
1947 static int compat_query_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
1948 {
1949         struct tbl *table = rcu_dereference(dp->table);
1950         struct timespec time_offset;
1951         u32 i;
1952
1953         time_offset = get_time_offset();
1954
1955         for (i = 0; i < n_flows; i++) {
1956                 struct compat_odp_flow __user *ufp = &flows[i];
1957                 struct odp_flow uf;
1958                 struct tbl_node *flow_node;
1959                 int error;
1960
1961                 if (compat_get_flow(&uf, ufp))
1962                         return -EFAULT;
1963                 memset(uf.key.reserved, 0, sizeof uf.key.reserved);
1964
1965                 flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp);
1966                 if (!flow_node)
1967                         error = put_user(ENOENT, &ufp->stats.error);
1968                 else
1969                         error = compat_answer_query(flow_cast(flow_node), uf.flags, time_offset, ufp);
1970                 if (error)
1971                         return -EFAULT;
1972         }
1973         return n_flows;
1974 }
1975
1976 struct compat_list_flows_cbdata {
1977         struct compat_odp_flow __user *uflows;
1978         u32 n_flows;
1979         u32 listed_flows;
1980         struct timespec time_offset;
1981 };
1982
1983 static int compat_list_flow(struct tbl_node *node, void *cbdata_)
1984 {
1985         struct sw_flow *flow = flow_cast(node);
1986         struct compat_list_flows_cbdata *cbdata = cbdata_;
1987         struct compat_odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
1988         int error;
1989
1990         if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
1991                 return -EFAULT;
1992         error = compat_answer_query(flow, 0, cbdata->time_offset, ufp);
1993         if (error)
1994                 return error;
1995
1996         if (cbdata->listed_flows >= cbdata->n_flows)
1997                 return cbdata->listed_flows;
1998         return 0;
1999 }
2000
2001 static int compat_list_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
2002 {
2003         struct compat_list_flows_cbdata cbdata;
2004         int error;
2005
2006         if (!n_flows)
2007                 return 0;
2008
2009         cbdata.uflows = flows;
2010         cbdata.n_flows = n_flows;
2011         cbdata.listed_flows = 0;
2012         cbdata.time_offset = get_time_offset();
2013
2014         error = tbl_foreach(rcu_dereference(dp->table), compat_list_flow, &cbdata);
2015         return error ? error : cbdata.listed_flows;
2016 }
2017
2018 static int compat_flowvec_ioctl(struct datapath *dp, unsigned long argp,
2019                                 int (*function)(struct datapath *,
2020                                                 struct compat_odp_flow *,
2021                                                 u32 n_flows))
2022 {
2023         struct compat_odp_flowvec __user *uflowvec;
2024         struct compat_odp_flow __user *flows;
2025         struct compat_odp_flowvec flowvec;
2026         int retval;
2027
2028         uflowvec = compat_ptr(argp);
2029         if (!access_ok(VERIFY_WRITE, uflowvec, sizeof *uflowvec) ||
2030             copy_from_user(&flowvec, uflowvec, sizeof flowvec))
2031                 return -EFAULT;
2032
2033         if (flowvec.n_flows > INT_MAX / sizeof(struct compat_odp_flow))
2034                 return -EINVAL;
2035
2036         flows = compat_ptr(flowvec.flows);
2037         if (!access_ok(VERIFY_WRITE, flows,
2038                        flowvec.n_flows * sizeof(struct compat_odp_flow)))
2039                 return -EFAULT;
2040
2041         retval = function(dp, flows, flowvec.n_flows);
2042         return (retval < 0 ? retval
2043                 : retval == flowvec.n_flows ? 0
2044                 : put_user(retval, &uflowvec->n_flows));
2045 }
2046
2047 static int compat_execute(struct datapath *dp, const struct compat_odp_execute __user *uexecute)
2048 {
2049         struct odp_execute execute;
2050         compat_uptr_t actions;
2051         compat_uptr_t data;
2052
2053         if (!access_ok(VERIFY_READ, uexecute, sizeof(struct compat_odp_execute)) ||
2054             __get_user(execute.in_port, &uexecute->in_port) ||
2055             __get_user(actions, &uexecute->actions) ||
2056             __get_user(execute.n_actions, &uexecute->n_actions) ||
2057             __get_user(data, &uexecute->data) ||
2058             __get_user(execute.length, &uexecute->length))
2059                 return -EFAULT;
2060
2061         execute.actions = compat_ptr(actions);
2062         execute.data = compat_ptr(data);
2063
2064         return do_execute(dp, &execute);
2065 }
2066
2067 static long openvswitch_compat_ioctl(struct file *f, unsigned int cmd, unsigned long argp)
2068 {
2069         int dp_idx = iminor(f->f_dentry->d_inode);
2070         struct datapath *dp;
2071         int err;
2072
2073         switch (cmd) {
2074         case ODP_DP_DESTROY:
2075         case ODP_FLOW_FLUSH:
2076                 /* Ioctls that don't need any translation at all. */
2077                 return openvswitch_ioctl(f, cmd, argp);
2078
2079         case ODP_DP_CREATE:
2080         case ODP_PORT_ATTACH:
2081         case ODP_PORT_DETACH:
2082         case ODP_VPORT_DEL:
2083         case ODP_VPORT_MTU_SET:
2084         case ODP_VPORT_MTU_GET:
2085         case ODP_VPORT_ETHER_SET:
2086         case ODP_VPORT_ETHER_GET:
2087         case ODP_VPORT_STATS_SET:
2088         case ODP_VPORT_STATS_GET:
2089         case ODP_DP_STATS:
2090         case ODP_GET_DROP_FRAGS:
2091         case ODP_SET_DROP_FRAGS:
2092         case ODP_SET_LISTEN_MASK:
2093         case ODP_GET_LISTEN_MASK:
2094         case ODP_SET_SFLOW_PROBABILITY:
2095         case ODP_GET_SFLOW_PROBABILITY:
2096         case ODP_PORT_QUERY:
2097                 /* Ioctls that just need their pointer argument extended. */
2098                 return openvswitch_ioctl(f, cmd, (unsigned long)compat_ptr(argp));
2099
2100         case ODP_VPORT_ADD32:
2101                 return compat_vport_user_add(compat_ptr(argp));
2102
2103         case ODP_VPORT_MOD32:
2104                 return compat_vport_user_mod(compat_ptr(argp));
2105         }
2106
2107         dp = get_dp_locked(dp_idx);
2108         err = -ENODEV;
2109         if (!dp)
2110                 goto exit;
2111
2112         switch (cmd) {
2113         case ODP_PORT_LIST32:
2114                 err = compat_list_ports(dp, compat_ptr(argp));
2115                 break;
2116
2117         case ODP_PORT_GROUP_SET32:
2118                 err = compat_set_port_group(dp, compat_ptr(argp));
2119                 break;
2120
2121         case ODP_PORT_GROUP_GET32:
2122                 err = compat_get_port_group(dp, compat_ptr(argp));
2123                 break;
2124
2125         case ODP_FLOW_PUT32:
2126                 err = compat_put_flow(dp, compat_ptr(argp));
2127                 break;
2128
2129         case ODP_FLOW_DEL32:
2130                 err = compat_del_flow(dp, compat_ptr(argp));
2131                 break;
2132
2133         case ODP_FLOW_GET32:
2134                 err = compat_flowvec_ioctl(dp, argp, compat_query_flows);
2135                 break;
2136
2137         case ODP_FLOW_LIST32:
2138                 err = compat_flowvec_ioctl(dp, argp, compat_list_flows);
2139                 break;
2140
2141         case ODP_EXECUTE32:
2142                 err = compat_execute(dp, compat_ptr(argp));
2143                 break;
2144
2145         default:
2146                 err = -ENOIOCTLCMD;
2147                 break;
2148         }
2149         mutex_unlock(&dp->mutex);
2150 exit:
2151         return err;
2152 }
2153 #endif
2154
2155 /* Unfortunately this function is not exported so this is a verbatim copy
2156  * from net/core/datagram.c in 2.6.30. */
2157 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
2158                                       u8 __user *to, int len,
2159                                       __wsum *csump)
2160 {
2161         int start = skb_headlen(skb);
2162         int pos = 0;
2163         int i, copy = start - offset;
2164
2165         /* Copy header. */
2166         if (copy > 0) {
2167                 int err = 0;
2168                 if (copy > len)
2169                         copy = len;
2170                 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
2171                                                *csump, &err);
2172                 if (err)
2173                         goto fault;
2174                 if ((len -= copy) == 0)
2175                         return 0;
2176                 offset += copy;
2177                 to += copy;
2178                 pos = copy;
2179         }
2180
2181         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2182                 int end;
2183
2184                 WARN_ON(start > offset + len);
2185
2186                 end = start + skb_shinfo(skb)->frags[i].size;
2187                 if ((copy = end - offset) > 0) {
2188                         __wsum csum2;
2189                         int err = 0;
2190                         u8  *vaddr;
2191                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2192                         struct page *page = frag->page;
2193
2194                         if (copy > len)
2195                                 copy = len;
2196                         vaddr = kmap(page);
2197                         csum2 = csum_and_copy_to_user(vaddr +
2198                                                         frag->page_offset +
2199                                                         offset - start,
2200                                                       to, copy, 0, &err);
2201                         kunmap(page);
2202                         if (err)
2203                                 goto fault;
2204                         *csump = csum_block_add(*csump, csum2, pos);
2205                         if (!(len -= copy))
2206                                 return 0;
2207                         offset += copy;
2208                         to += copy;
2209                         pos += copy;
2210                 }
2211                 start = end;
2212         }
2213
2214         if (skb_shinfo(skb)->frag_list) {
2215                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2216
2217                 for (; list; list=list->next) {
2218                         int end;
2219
2220                         WARN_ON(start > offset + len);
2221
2222                         end = start + list->len;
2223                         if ((copy = end - offset) > 0) {
2224                                 __wsum csum2 = 0;
2225                                 if (copy > len)
2226                                         copy = len;
2227                                 if (skb_copy_and_csum_datagram(list,
2228                                                                offset - start,
2229                                                                to, copy,
2230                                                                &csum2))
2231                                         goto fault;
2232                                 *csump = csum_block_add(*csump, csum2, pos);
2233                                 if ((len -= copy) == 0)
2234                                         return 0;
2235                                 offset += copy;
2236                                 to += copy;
2237                                 pos += copy;
2238                         }
2239                         start = end;
2240                 }
2241         }
2242         if (!len)
2243                 return 0;
2244
2245 fault:
2246         return -EFAULT;
2247 }
2248
2249 ssize_t openvswitch_read(struct file *f, char __user *buf, size_t nbytes,
2250                       loff_t *ppos)
2251 {
2252         /* XXX is there sufficient synchronization here? */
2253         int listeners = get_listen_mask(f);
2254         int dp_idx = iminor(f->f_dentry->d_inode);
2255         struct datapath *dp = get_dp(dp_idx);
2256         struct sk_buff *skb;
2257         size_t copy_bytes, tot_copy_bytes;
2258         int retval;
2259
2260         if (!dp)
2261                 return -ENODEV;
2262
2263         if (nbytes == 0 || !listeners)
2264                 return 0;
2265
2266         for (;;) {
2267                 int i;
2268
2269                 for (i = 0; i < DP_N_QUEUES; i++) {
2270                         if (listeners & (1 << i)) {
2271                                 skb = skb_dequeue(&dp->queues[i]);
2272                                 if (skb)
2273                                         goto success;
2274                         }
2275                 }
2276
2277                 if (f->f_flags & O_NONBLOCK) {
2278                         retval = -EAGAIN;
2279                         goto error;
2280                 }
2281
2282                 wait_event_interruptible(dp->waitqueue,
2283                                          dp_has_packet_of_interest(dp,
2284                                                                    listeners));
2285
2286                 if (signal_pending(current)) {
2287                         retval = -ERESTARTSYS;
2288                         goto error;
2289                 }
2290         }
2291 success:
2292         copy_bytes = tot_copy_bytes = min_t(size_t, skb->len, nbytes);
2293
2294         retval = 0;
2295         if (skb->ip_summed == CHECKSUM_PARTIAL) {
2296                 if (copy_bytes == skb->len) {
2297                         __wsum csum = 0;
2298                         unsigned int csum_start, csum_offset;
2299
2300 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
2301                         csum_start = skb->csum_start - skb_headroom(skb);
2302                         csum_offset = skb->csum_offset;
2303 #else
2304                         csum_start = skb_transport_header(skb) - skb->data;
2305                         csum_offset = skb->csum;
2306 #endif
2307                         BUG_ON(csum_start >= skb_headlen(skb));
2308                         retval = skb_copy_and_csum_datagram(skb, csum_start, buf + csum_start,
2309                                                             copy_bytes - csum_start, &csum);
2310                         if (!retval) {
2311                                 __sum16 __user *csump;
2312
2313                                 copy_bytes = csum_start;
2314                                 csump = (__sum16 __user *)(buf + csum_start + csum_offset);
2315
2316                                 BUG_ON((char *)csump + sizeof(__sum16) > buf + nbytes);
2317                                 put_user(csum_fold(csum), csump);
2318                         }
2319                 } else
2320                         retval = skb_checksum_help(skb);
2321         }
2322
2323         if (!retval) {
2324                 struct iovec __user iov;
2325
2326                 iov.iov_base = buf;
2327                 iov.iov_len = copy_bytes;
2328                 retval = skb_copy_datagram_iovec(skb, 0, &iov, iov.iov_len);
2329         }
2330
2331         if (!retval)
2332                 retval = tot_copy_bytes;
2333
2334         kfree_skb(skb);
2335
2336 error:
2337         return retval;
2338 }
2339
2340 static unsigned int openvswitch_poll(struct file *file, poll_table *wait)
2341 {
2342         /* XXX is there sufficient synchronization here? */
2343         int dp_idx = iminor(file->f_dentry->d_inode);
2344         struct datapath *dp = get_dp(dp_idx);
2345         unsigned int mask;
2346
2347         if (dp) {
2348                 mask = 0;
2349                 poll_wait(file, &dp->waitqueue, wait);
2350                 if (dp_has_packet_of_interest(dp, get_listen_mask(file)))
2351                         mask |= POLLIN | POLLRDNORM;
2352         } else {
2353                 mask = POLLIN | POLLRDNORM | POLLHUP;
2354         }
2355         return mask;
2356 }
2357
2358 struct file_operations openvswitch_fops = {
2359         /* XXX .aio_read = openvswitch_aio_read, */
2360         .read  = openvswitch_read,
2361         .poll  = openvswitch_poll,
2362         .unlocked_ioctl = openvswitch_ioctl,
2363 #ifdef CONFIG_COMPAT
2364         .compat_ioctl = openvswitch_compat_ioctl,
2365 #endif
2366         /* XXX .fasync = openvswitch_fasync, */
2367 };
2368
2369 static int major;
2370
2371 static int __init dp_init(void)
2372 {
2373         struct sk_buff *dummy_skb;
2374         int err;
2375
2376         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2377
2378         printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2379
2380         err = flow_init();
2381         if (err)
2382                 goto error;
2383
2384         err = vport_init();
2385         if (err)
2386                 goto error_flow_exit;
2387
2388         err = register_netdevice_notifier(&dp_device_notifier);
2389         if (err)
2390                 goto error_vport_exit;
2391
2392         major = register_chrdev(0, "openvswitch", &openvswitch_fops);
2393         if (err < 0)
2394                 goto error_unreg_notifier;
2395
2396         return 0;
2397
2398 error_unreg_notifier:
2399         unregister_netdevice_notifier(&dp_device_notifier);
2400 error_vport_exit:
2401         vport_exit();
2402 error_flow_exit:
2403         flow_exit();
2404 error:
2405         return err;
2406 }
2407
2408 static void dp_cleanup(void)
2409 {
2410         rcu_barrier();
2411         unregister_chrdev(major, "openvswitch");
2412         unregister_netdevice_notifier(&dp_device_notifier);
2413         vport_exit();
2414         flow_exit();
2415 }
2416
2417 module_init(dp_init);
2418 module_exit(dp_cleanup);
2419
2420 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2421 MODULE_LICENSE("GPL");