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