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