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