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