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