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