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