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