]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
064af421 | 2 | * Copyright (c) 2007, 2008, 2009 Nicira Networks. |
a14bc59f BP |
3 | * Distributed under the terms of the GNU GPL version 2. |
4 | * | |
5 | * Significant portions of this file may be copied from parts of the Linux | |
6 | * kernel, by Linus Torvalds and others. | |
064af421 BP |
7 | */ |
8 | ||
9 | /* Functions for managing the dp interface/device. */ | |
10 | ||
11 | #include <linux/init.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/fs.h> | |
14 | #include <linux/if_arp.h> | |
15 | #include <linux/if_bridge.h> | |
16 | #include <linux/if_vlan.h> | |
17 | #include <linux/in.h> | |
18 | #include <linux/ip.h> | |
19 | #include <linux/delay.h> | |
20 | #include <linux/time.h> | |
21 | #include <linux/etherdevice.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/kthread.h> | |
24 | #include <linux/llc.h> | |
25 | #include <linux/mutex.h> | |
26 | #include <linux/percpu.h> | |
27 | #include <linux/rcupdate.h> | |
28 | #include <linux/tcp.h> | |
29 | #include <linux/udp.h> | |
30 | #include <linux/version.h> | |
31 | #include <linux/ethtool.h> | |
32 | #include <linux/random.h> | |
33 | #include <linux/wait.h> | |
34 | #include <asm/system.h> | |
35 | #include <asm/div64.h> | |
36 | #include <asm/bug.h> | |
37 | #include <linux/netfilter_bridge.h> | |
38 | #include <linux/netfilter_ipv4.h> | |
39 | #include <linux/inetdevice.h> | |
40 | #include <linux/list.h> | |
41 | #include <linux/rculist.h> | |
42 | #include <linux/workqueue.h> | |
43 | #include <linux/dmi.h> | |
44 | #include <net/llc.h> | |
45 | ||
46 | #include "openvswitch/datapath-protocol.h" | |
47 | #include "datapath.h" | |
48 | #include "actions.h" | |
49 | #include "dp_dev.h" | |
50 | #include "flow.h" | |
51 | ||
52 | #include "compat.h" | |
53 | ||
54 | ||
55 | int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd); | |
56 | EXPORT_SYMBOL(dp_ioctl_hook); | |
57 | ||
58 | int (*dp_add_dp_hook)(struct datapath *dp); | |
59 | EXPORT_SYMBOL(dp_add_dp_hook); | |
60 | ||
61 | int (*dp_del_dp_hook)(struct datapath *dp); | |
62 | EXPORT_SYMBOL(dp_del_dp_hook); | |
63 | ||
64 | int (*dp_add_if_hook)(struct net_bridge_port *p); | |
65 | EXPORT_SYMBOL(dp_add_if_hook); | |
66 | ||
67 | int (*dp_del_if_hook)(struct net_bridge_port *p); | |
68 | EXPORT_SYMBOL(dp_del_if_hook); | |
69 | ||
70 | /* Datapaths. Protected on the read side by rcu_read_lock, on the write side | |
71 | * by dp_mutex. dp_mutex is almost completely redundant with genl_mutex | |
72 | * maintained by the Generic Netlink code, but the timeout path needs mutual | |
73 | * exclusion too. | |
74 | * | |
75 | * dp_mutex nests inside the RTNL lock: if you need both you must take the RTNL | |
76 | * lock first. | |
77 | * | |
78 | * It is safe to access the datapath and net_bridge_port structures with just | |
79 | * dp_mutex. | |
80 | */ | |
81 | static struct datapath *dps[ODP_MAX]; | |
82 | static DEFINE_MUTEX(dp_mutex); | |
83 | ||
84 | /* Number of milliseconds between runs of the maintenance thread. */ | |
85 | #define MAINT_SLEEP_MSECS 1000 | |
86 | ||
87 | static int new_nbp(struct datapath *, struct net_device *, int port_no); | |
88 | ||
89 | /* Must be called with rcu_read_lock or dp_mutex. */ | |
90 | struct datapath *get_dp(int dp_idx) | |
91 | { | |
92 | if (dp_idx < 0 || dp_idx >= ODP_MAX) | |
93 | return NULL; | |
94 | return rcu_dereference(dps[dp_idx]); | |
95 | } | |
96 | EXPORT_SYMBOL_GPL(get_dp); | |
97 | ||
98 | struct datapath *get_dp_locked(int dp_idx) | |
99 | { | |
100 | struct datapath *dp; | |
101 | ||
102 | mutex_lock(&dp_mutex); | |
103 | dp = get_dp(dp_idx); | |
104 | if (dp) | |
105 | mutex_lock(&dp->mutex); | |
106 | mutex_unlock(&dp_mutex); | |
107 | return dp; | |
108 | } | |
109 | ||
110 | static inline size_t br_nlmsg_size(void) | |
111 | { | |
112 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
113 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
114 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
115 | + nla_total_size(4) /* IFLA_MASTER */ | |
116 | + nla_total_size(4) /* IFLA_MTU */ | |
117 | + nla_total_size(4) /* IFLA_LINK */ | |
118 | + nla_total_size(1); /* IFLA_OPERSTATE */ | |
119 | } | |
120 | ||
121 | static int dp_fill_ifinfo(struct sk_buff *skb, | |
122 | const struct net_bridge_port *port, | |
123 | int event, unsigned int flags) | |
124 | { | |
125 | const struct datapath *dp = port->dp; | |
126 | const struct net_device *dev = port->dev; | |
127 | struct ifinfomsg *hdr; | |
128 | struct nlmsghdr *nlh; | |
129 | ||
130 | nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags); | |
131 | if (nlh == NULL) | |
132 | return -EMSGSIZE; | |
133 | ||
134 | hdr = nlmsg_data(nlh); | |
135 | hdr->ifi_family = AF_BRIDGE; | |
136 | hdr->__ifi_pad = 0; | |
137 | hdr->ifi_type = dev->type; | |
138 | hdr->ifi_index = dev->ifindex; | |
139 | hdr->ifi_flags = dev_get_flags(dev); | |
140 | hdr->ifi_change = 0; | |
141 | ||
142 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); | |
143 | NLA_PUT_U32(skb, IFLA_MASTER, dp->ports[ODPP_LOCAL]->dev->ifindex); | |
144 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); | |
145 | #ifdef IFLA_OPERSTATE | |
146 | NLA_PUT_U8(skb, IFLA_OPERSTATE, | |
147 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); | |
148 | #endif | |
149 | ||
150 | if (dev->addr_len) | |
151 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); | |
152 | ||
153 | if (dev->ifindex != dev->iflink) | |
154 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); | |
155 | ||
156 | return nlmsg_end(skb, nlh); | |
157 | ||
158 | nla_put_failure: | |
159 | nlmsg_cancel(skb, nlh); | |
160 | return -EMSGSIZE; | |
161 | } | |
162 | ||
163 | static void dp_ifinfo_notify(int event, struct net_bridge_port *port) | |
164 | { | |
165 | struct net *net = dev_net(port->dev); | |
166 | struct sk_buff *skb; | |
167 | int err = -ENOBUFS; | |
168 | ||
169 | skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL); | |
170 | if (skb == NULL) | |
171 | goto errout; | |
172 | ||
173 | err = dp_fill_ifinfo(skb, port, event, 0); | |
174 | if (err < 0) { | |
175 | /* -EMSGSIZE implies BUG in br_nlmsg_size() */ | |
176 | WARN_ON(err == -EMSGSIZE); | |
177 | kfree_skb(skb); | |
178 | goto errout; | |
179 | } | |
180 | err = rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); | |
181 | errout: | |
182 | if (err < 0) | |
183 | rtnl_set_sk_err(net, RTNLGRP_LINK, err); | |
184 | } | |
185 | ||
186 | static int create_dp(int dp_idx, const char __user *devnamep) | |
187 | { | |
188 | struct net_device *dp_dev; | |
189 | char devname[IFNAMSIZ]; | |
190 | struct datapath *dp; | |
191 | int err; | |
192 | int i; | |
193 | ||
194 | if (devnamep) { | |
195 | err = -EFAULT; | |
196 | if (strncpy_from_user(devname, devnamep, IFNAMSIZ - 1) < 0) | |
197 | goto err; | |
198 | devname[IFNAMSIZ - 1] = '\0'; | |
199 | } else { | |
200 | snprintf(devname, sizeof devname, "of%d", dp_idx); | |
201 | } | |
202 | ||
203 | rtnl_lock(); | |
204 | mutex_lock(&dp_mutex); | |
205 | err = -ENODEV; | |
206 | if (!try_module_get(THIS_MODULE)) | |
207 | goto err_unlock; | |
208 | ||
209 | /* Exit early if a datapath with that number already exists. | |
210 | * (We don't use -EEXIST because that's ambiguous with 'devname' | |
211 | * conflicting with an existing network device name.) */ | |
212 | err = -EBUSY; | |
213 | if (get_dp(dp_idx)) | |
214 | goto err_put_module; | |
215 | ||
216 | err = -ENOMEM; | |
217 | dp = kzalloc(sizeof *dp, GFP_KERNEL); | |
218 | if (dp == NULL) | |
219 | goto err_put_module; | |
828bc1f0 | 220 | INIT_LIST_HEAD(&dp->port_list); |
064af421 BP |
221 | mutex_init(&dp->mutex); |
222 | dp->dp_idx = dp_idx; | |
223 | for (i = 0; i < DP_N_QUEUES; i++) | |
224 | skb_queue_head_init(&dp->queues[i]); | |
225 | init_waitqueue_head(&dp->waitqueue); | |
226 | ||
828bc1f0 BP |
227 | /* Allocate table. */ |
228 | err = -ENOMEM; | |
229 | rcu_assign_pointer(dp->table, dp_table_create(DP_L1_SIZE)); | |
230 | if (!dp->table) | |
231 | goto err_free_dp; | |
232 | ||
064af421 BP |
233 | /* Setup our datapath device */ |
234 | dp_dev = dp_dev_create(dp, devname, ODPP_LOCAL); | |
235 | err = PTR_ERR(dp_dev); | |
236 | if (IS_ERR(dp_dev)) | |
828bc1f0 | 237 | goto err_destroy_table; |
064af421 BP |
238 | |
239 | err = new_nbp(dp, dp_dev, ODPP_LOCAL); | |
828bc1f0 BP |
240 | if (err) { |
241 | dp_dev_destroy(dp_dev); | |
064af421 | 242 | goto err_destroy_table; |
828bc1f0 | 243 | } |
064af421 BP |
244 | |
245 | dp->drop_frags = 0; | |
246 | dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); | |
247 | if (!dp->stats_percpu) | |
248 | goto err_destroy_local_port; | |
249 | ||
250 | rcu_assign_pointer(dps[dp_idx], dp); | |
251 | mutex_unlock(&dp_mutex); | |
252 | rtnl_unlock(); | |
253 | ||
254 | if (dp_add_dp_hook) | |
255 | dp_add_dp_hook(dp); | |
256 | ||
257 | return 0; | |
258 | ||
259 | err_destroy_local_port: | |
72ca14c1 | 260 | dp_del_port(dp->ports[ODPP_LOCAL]); |
064af421 BP |
261 | err_destroy_table: |
262 | dp_table_destroy(dp->table, 0); | |
064af421 BP |
263 | err_free_dp: |
264 | kfree(dp); | |
265 | err_put_module: | |
266 | module_put(THIS_MODULE); | |
267 | err_unlock: | |
268 | mutex_unlock(&dp_mutex); | |
269 | rtnl_unlock(); | |
270 | err: | |
271 | return err; | |
272 | } | |
273 | ||
72ca14c1 | 274 | static void do_destroy_dp(struct datapath *dp) |
064af421 BP |
275 | { |
276 | struct net_bridge_port *p, *n; | |
277 | int i; | |
278 | ||
6fba0d0b BP |
279 | list_for_each_entry_safe (p, n, &dp->port_list, node) |
280 | if (p->port_no != ODPP_LOCAL) | |
72ca14c1 | 281 | dp_del_port(p); |
6fba0d0b | 282 | |
064af421 BP |
283 | if (dp_del_dp_hook) |
284 | dp_del_dp_hook(dp); | |
285 | ||
064af421 | 286 | rcu_assign_pointer(dps[dp->dp_idx], NULL); |
064af421 | 287 | |
72ca14c1 | 288 | dp_del_port(dp->ports[ODPP_LOCAL]); |
6fba0d0b | 289 | |
064af421 | 290 | dp_table_destroy(dp->table, 1); |
6fba0d0b | 291 | |
064af421 BP |
292 | for (i = 0; i < DP_N_QUEUES; i++) |
293 | skb_queue_purge(&dp->queues[i]); | |
294 | for (i = 0; i < DP_MAX_GROUPS; i++) | |
295 | kfree(dp->groups[i]); | |
296 | free_percpu(dp->stats_percpu); | |
297 | kfree(dp); | |
298 | module_put(THIS_MODULE); | |
299 | } | |
300 | ||
301 | static int destroy_dp(int dp_idx) | |
302 | { | |
064af421 | 303 | struct datapath *dp; |
064af421 BP |
304 | int err; |
305 | ||
306 | rtnl_lock(); | |
307 | mutex_lock(&dp_mutex); | |
308 | dp = get_dp(dp_idx); | |
309 | err = -ENODEV; | |
310 | if (!dp) | |
311 | goto err_unlock; | |
312 | ||
72ca14c1 | 313 | do_destroy_dp(dp); |
064af421 BP |
314 | err = 0; |
315 | ||
316 | err_unlock: | |
317 | mutex_unlock(&dp_mutex); | |
318 | rtnl_unlock(); | |
064af421 BP |
319 | return err; |
320 | } | |
321 | ||
322 | /* Called with RTNL lock and dp_mutex. */ | |
323 | static int new_nbp(struct datapath *dp, struct net_device *dev, int port_no) | |
324 | { | |
325 | struct net_bridge_port *p; | |
326 | ||
327 | if (dev->br_port != NULL) | |
328 | return -EBUSY; | |
329 | ||
330 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
331 | if (!p) | |
332 | return -ENOMEM; | |
333 | ||
334 | dev_set_promiscuity(dev, 1); | |
335 | dev_hold(dev); | |
336 | p->port_no = port_no; | |
337 | p->dp = dp; | |
338 | p->dev = dev; | |
339 | if (!is_dp_dev(dev)) | |
340 | rcu_assign_pointer(dev->br_port, p); | |
341 | else { | |
342 | /* It would make sense to assign dev->br_port here too, but | |
343 | * that causes packets received on internal ports to get caught | |
344 | * in dp_frame_hook(). In turn dp_frame_hook() can reject them | |
345 | * back to network stack, but that's a waste of time. */ | |
346 | } | |
347 | rcu_assign_pointer(dp->ports[port_no], p); | |
348 | list_add_rcu(&p->node, &dp->port_list); | |
349 | dp->n_ports++; | |
350 | ||
351 | dp_ifinfo_notify(RTM_NEWLINK, p); | |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
356 | static int add_port(int dp_idx, struct odp_port __user *portp) | |
357 | { | |
358 | struct net_device *dev; | |
359 | struct datapath *dp; | |
360 | struct odp_port port; | |
361 | int port_no; | |
362 | int err; | |
363 | ||
364 | err = -EFAULT; | |
365 | if (copy_from_user(&port, portp, sizeof port)) | |
366 | goto out; | |
367 | port.devname[IFNAMSIZ - 1] = '\0'; | |
368 | port_no = port.port; | |
369 | ||
370 | err = -EINVAL; | |
371 | if (port_no < 0 || port_no >= DP_MAX_PORTS) | |
372 | goto out; | |
373 | ||
374 | rtnl_lock(); | |
375 | dp = get_dp_locked(dp_idx); | |
376 | err = -ENODEV; | |
377 | if (!dp) | |
378 | goto out_unlock_rtnl; | |
379 | ||
380 | err = -EEXIST; | |
381 | if (dp->ports[port_no]) | |
382 | goto out_unlock_dp; | |
383 | ||
384 | if (!(port.flags & ODP_PORT_INTERNAL)) { | |
385 | err = -ENODEV; | |
386 | dev = dev_get_by_name(&init_net, port.devname); | |
387 | if (!dev) | |
388 | goto out_unlock_dp; | |
389 | ||
390 | err = -EINVAL; | |
391 | if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER || | |
392 | is_dp_dev(dev)) | |
393 | goto out_put; | |
394 | } else { | |
395 | dev = dp_dev_create(dp, port.devname, port_no); | |
396 | err = PTR_ERR(dev); | |
397 | if (IS_ERR(dev)) | |
398 | goto out_unlock_dp; | |
399 | dev_hold(dev); | |
400 | } | |
401 | ||
402 | err = new_nbp(dp, dev, port_no); | |
403 | if (err) | |
404 | goto out_put; | |
405 | ||
406 | if (dp_add_if_hook) | |
407 | dp_add_if_hook(dp->ports[port_no]); | |
408 | ||
409 | out_put: | |
410 | dev_put(dev); | |
411 | out_unlock_dp: | |
412 | mutex_unlock(&dp->mutex); | |
413 | out_unlock_rtnl: | |
414 | rtnl_unlock(); | |
415 | out: | |
416 | return err; | |
417 | } | |
418 | ||
72ca14c1 | 419 | int dp_del_port(struct net_bridge_port *p) |
064af421 BP |
420 | { |
421 | ASSERT_RTNL(); | |
422 | ||
423 | #ifdef SUPPORT_SYSFS | |
424 | if (p->port_no != ODPP_LOCAL && dp_del_if_hook) | |
425 | sysfs_remove_link(&p->dp->ifobj, p->dev->name); | |
426 | #endif | |
427 | dp_ifinfo_notify(RTM_DELLINK, p); | |
428 | ||
429 | p->dp->n_ports--; | |
430 | ||
431 | if (is_dp_dev(p->dev)) { | |
432 | /* Make sure that no packets arrive from now on, since | |
433 | * dp_dev_xmit() will try to find itself through | |
434 | * p->dp->ports[], and we're about to set that to null. */ | |
435 | netif_tx_disable(p->dev); | |
436 | } | |
437 | ||
438 | /* First drop references to device. */ | |
439 | dev_set_promiscuity(p->dev, -1); | |
440 | list_del_rcu(&p->node); | |
441 | rcu_assign_pointer(p->dp->ports[p->port_no], NULL); | |
442 | rcu_assign_pointer(p->dev->br_port, NULL); | |
443 | ||
444 | /* Then wait until no one is still using it, and destroy it. */ | |
445 | synchronize_rcu(); | |
446 | ||
447 | if (is_dp_dev(p->dev)) { | |
448 | dp_dev_destroy(p->dev); | |
064af421 BP |
449 | } |
450 | if (p->port_no != ODPP_LOCAL && dp_del_if_hook) { | |
451 | dp_del_if_hook(p); | |
452 | } else { | |
453 | dev_put(p->dev); | |
454 | kfree(p); | |
455 | } | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | static int del_port(int dp_idx, int port_no) | |
461 | { | |
064af421 BP |
462 | struct net_bridge_port *p; |
463 | struct datapath *dp; | |
464 | LIST_HEAD(dp_devs); | |
465 | int err; | |
466 | ||
467 | err = -EINVAL; | |
468 | if (port_no < 0 || port_no >= DP_MAX_PORTS || port_no == ODPP_LOCAL) | |
469 | goto out; | |
470 | ||
471 | rtnl_lock(); | |
472 | dp = get_dp_locked(dp_idx); | |
473 | err = -ENODEV; | |
474 | if (!dp) | |
475 | goto out_unlock_rtnl; | |
476 | ||
477 | p = dp->ports[port_no]; | |
478 | err = -ENOENT; | |
479 | if (!p) | |
480 | goto out_unlock_dp; | |
481 | ||
72ca14c1 | 482 | err = dp_del_port(p); |
064af421 BP |
483 | |
484 | out_unlock_dp: | |
485 | mutex_unlock(&dp->mutex); | |
486 | out_unlock_rtnl: | |
487 | rtnl_unlock(); | |
488 | out: | |
064af421 BP |
489 | return err; |
490 | } | |
491 | ||
492 | /* Must be called with rcu_read_lock. */ | |
493 | static void | |
494 | do_port_input(struct net_bridge_port *p, struct sk_buff *skb) | |
495 | { | |
496 | /* Make our own copy of the packet. Otherwise we will mangle the | |
497 | * packet for anyone who came before us (e.g. tcpdump via AF_PACKET). | |
498 | * (No one comes after us, since we tell handle_bridge() that we took | |
499 | * the packet.) */ | |
500 | skb = skb_share_check(skb, GFP_ATOMIC); | |
501 | if (!skb) | |
502 | return; | |
503 | ||
504 | /* Push the Ethernet header back on. */ | |
505 | skb_push(skb, ETH_HLEN); | |
506 | skb_reset_mac_header(skb); | |
507 | dp_process_received_packet(skb, p); | |
508 | } | |
509 | ||
510 | /* Must be called with rcu_read_lock and with bottom-halves disabled. */ | |
511 | void dp_process_received_packet(struct sk_buff *skb, struct net_bridge_port *p) | |
512 | { | |
513 | struct datapath *dp = p->dp; | |
514 | struct dp_stats_percpu *stats; | |
515 | struct odp_flow_key key; | |
516 | struct sw_flow *flow; | |
517 | ||
518 | WARN_ON_ONCE(skb_shared(skb)); | |
064af421 BP |
519 | |
520 | /* BHs are off so we don't have to use get_cpu()/put_cpu() here. */ | |
521 | stats = percpu_ptr(dp->stats_percpu, smp_processor_id()); | |
522 | ||
523 | if (flow_extract(skb, p ? p->port_no : ODPP_NONE, &key)) { | |
524 | if (dp->drop_frags) { | |
525 | kfree_skb(skb); | |
526 | stats->n_frags++; | |
527 | return; | |
528 | } | |
529 | } | |
530 | ||
531 | flow = dp_table_lookup(rcu_dereference(dp->table), &key); | |
532 | if (flow) { | |
533 | struct sw_flow_actions *acts = rcu_dereference(flow->sf_acts); | |
534 | flow_used(flow, skb); | |
535 | execute_actions(dp, skb, &key, acts->actions, acts->n_actions, | |
536 | GFP_ATOMIC); | |
537 | stats->n_hit++; | |
538 | } else { | |
539 | stats->n_missed++; | |
540 | dp_output_control(dp, skb, _ODPL_MISS_NR, 0); | |
541 | } | |
542 | } | |
543 | ||
544 | /* | |
545 | * Used as br_handle_frame_hook. (Cannot run bridge at the same time, even on | |
546 | * different set of devices!) | |
547 | */ | |
548 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) | |
549 | /* Called with rcu_read_lock and bottom-halves disabled. */ | |
550 | static struct sk_buff *dp_frame_hook(struct net_bridge_port *p, | |
551 | struct sk_buff *skb) | |
552 | { | |
553 | do_port_input(p, skb); | |
554 | return NULL; | |
555 | } | |
556 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) | |
557 | /* Called with rcu_read_lock and bottom-halves disabled. */ | |
558 | static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb) | |
559 | { | |
560 | do_port_input(p, *pskb); | |
561 | return 1; | |
562 | } | |
563 | #else | |
564 | #error | |
565 | #endif | |
566 | ||
567 | #ifdef CONFIG_XEN | |
568 | /* This code is copied verbatim from net/dev/core.c in Xen's | |
569 | * linux-2.6.18-92.1.10.el5.xs5.0.0.394.644. We can't call those functions | |
570 | * directly because they aren't exported. */ | |
571 | static int skb_pull_up_to(struct sk_buff *skb, void *ptr) | |
572 | { | |
573 | if (ptr < (void *)skb->tail) | |
574 | return 1; | |
575 | if (__pskb_pull_tail(skb, | |
576 | ptr - (void *)skb->data - skb_headlen(skb))) { | |
577 | return 1; | |
578 | } else { | |
579 | return 0; | |
580 | } | |
581 | } | |
582 | ||
583 | int skb_checksum_setup(struct sk_buff *skb) | |
584 | { | |
585 | if (skb->proto_csum_blank) { | |
586 | if (skb->protocol != htons(ETH_P_IP)) | |
587 | goto out; | |
588 | if (!skb_pull_up_to(skb, skb->nh.iph + 1)) | |
589 | goto out; | |
590 | skb->h.raw = (unsigned char *)skb->nh.iph + 4*skb->nh.iph->ihl; | |
591 | switch (skb->nh.iph->protocol) { | |
592 | case IPPROTO_TCP: | |
593 | skb->csum = offsetof(struct tcphdr, check); | |
594 | break; | |
595 | case IPPROTO_UDP: | |
596 | skb->csum = offsetof(struct udphdr, check); | |
597 | break; | |
598 | default: | |
599 | if (net_ratelimit()) | |
600 | printk(KERN_ERR "Attempting to checksum a non-" | |
601 | "TCP/UDP packet, dropping a protocol" | |
602 | " %d packet", skb->nh.iph->protocol); | |
603 | goto out; | |
604 | } | |
605 | if (!skb_pull_up_to(skb, skb->h.raw + skb->csum + 2)) | |
606 | goto out; | |
607 | skb->ip_summed = CHECKSUM_HW; | |
608 | skb->proto_csum_blank = 0; | |
609 | } | |
610 | return 0; | |
611 | out: | |
612 | return -EPROTO; | |
613 | } | |
614 | #endif | |
615 | ||
616 | int | |
617 | dp_output_control(struct datapath *dp, struct sk_buff *skb, int queue_no, | |
618 | u32 arg) | |
619 | { | |
620 | struct dp_stats_percpu *stats; | |
621 | struct sk_buff_head *queue; | |
622 | int port_no; | |
623 | int err; | |
624 | ||
625 | WARN_ON_ONCE(skb_shared(skb)); | |
626 | BUG_ON(queue_no != _ODPL_MISS_NR && queue_no != _ODPL_ACTION_NR); | |
627 | ||
628 | queue = &dp->queues[queue_no]; | |
629 | err = -ENOBUFS; | |
630 | if (skb_queue_len(queue) >= DP_MAX_QUEUE_LEN) | |
631 | goto err_kfree_skb; | |
632 | ||
633 | /* If a checksum-deferred packet is forwarded to the controller, | |
634 | * correct the pointers and checksum. This happens on a regular basis | |
635 | * only on Xen (the CHECKSUM_HW case), on which VMs can pass up packets | |
636 | * that do not have their checksum computed. We also implement it for | |
637 | * the non-Xen case, but it is difficult to trigger or test this case | |
638 | * there, hence the WARN_ON_ONCE(). | |
639 | */ | |
640 | err = skb_checksum_setup(skb); | |
641 | if (err) | |
642 | goto err_kfree_skb; | |
643 | #ifndef CHECKSUM_HW | |
644 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
645 | WARN_ON_ONCE(1); | |
646 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) | |
647 | /* Until 2.6.22, the start of the transport header was also the | |
648 | * start of data to be checksummed. Linux 2.6.22 introduced | |
649 | * the csum_start field for this purpose, but we should point | |
650 | * the transport header to it anyway for backward | |
651 | * compatibility, as dev_queue_xmit() does even in 2.6.28. */ | |
652 | skb_set_transport_header(skb, skb->csum_start - | |
653 | skb_headroom(skb)); | |
654 | #endif | |
655 | err = skb_checksum_help(skb); | |
656 | if (err) | |
657 | goto err_kfree_skb; | |
658 | } | |
659 | #else | |
660 | if (skb->ip_summed == CHECKSUM_HW) { | |
661 | err = skb_checksum_help(skb, 0); | |
662 | if (err) | |
663 | goto err_kfree_skb; | |
664 | } | |
665 | #endif | |
666 | ||
667 | /* Break apart GSO packets into their component pieces. Otherwise | |
668 | * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */ | |
669 | if (skb_is_gso(skb)) { | |
670 | struct sk_buff *nskb = skb_gso_segment(skb, 0); | |
671 | if (nskb) { | |
672 | kfree_skb(skb); | |
673 | skb = nskb; | |
674 | if (unlikely(IS_ERR(skb))) { | |
675 | err = PTR_ERR(skb); | |
676 | goto err; | |
677 | } | |
678 | } else { | |
679 | /* XXX This case might not be possible. It's hard to | |
680 | * tell from the skb_gso_segment() code and comment. */ | |
681 | } | |
682 | } | |
683 | ||
684 | /* Figure out port number. */ | |
685 | port_no = ODPP_LOCAL; | |
686 | if (skb->dev) { | |
687 | if (skb->dev->br_port) | |
688 | port_no = skb->dev->br_port->port_no; | |
689 | else if (is_dp_dev(skb->dev)) | |
690 | port_no = dp_dev_priv(skb->dev)->port_no; | |
691 | } | |
692 | ||
693 | /* Append each packet to queue. There will be only one packet unless | |
694 | * we broke up a GSO packet above. */ | |
695 | do { | |
696 | struct odp_msg *header; | |
697 | struct sk_buff *nskb = skb->next; | |
698 | skb->next = NULL; | |
699 | ||
700 | err = skb_cow(skb, sizeof *header); | |
701 | if (err) { | |
702 | while (nskb) { | |
703 | kfree_skb(skb); | |
704 | skb = nskb; | |
705 | nskb = skb->next; | |
706 | } | |
707 | goto err_kfree_skb; | |
708 | } | |
709 | ||
710 | header = (struct odp_msg*)__skb_push(skb, sizeof *header); | |
711 | header->type = queue_no; | |
712 | header->length = skb->len; | |
713 | header->port = port_no; | |
714 | header->reserved = 0; | |
715 | header->arg = arg; | |
716 | skb_queue_tail(queue, skb); | |
717 | ||
718 | skb = nskb; | |
719 | } while (skb); | |
720 | ||
721 | wake_up_interruptible(&dp->waitqueue); | |
722 | return 0; | |
723 | ||
724 | err_kfree_skb: | |
725 | kfree_skb(skb); | |
726 | err: | |
727 | stats = percpu_ptr(dp->stats_percpu, get_cpu()); | |
728 | stats->n_lost++; | |
729 | put_cpu(); | |
730 | ||
731 | return err; | |
732 | } | |
733 | ||
734 | static int flush_flows(struct datapath *dp) | |
735 | { | |
736 | dp->n_flows = 0; | |
737 | return dp_table_flush(dp); | |
738 | } | |
739 | ||
740 | static int validate_actions(const struct sw_flow_actions *actions) | |
741 | { | |
742 | unsigned int i; | |
743 | ||
744 | for (i = 0; i < actions->n_actions; i++) { | |
745 | const union odp_action *a = &actions->actions[i]; | |
746 | switch (a->type) { | |
747 | case ODPAT_OUTPUT: | |
748 | if (a->output.port >= DP_MAX_PORTS) | |
749 | return -EINVAL; | |
750 | break; | |
751 | ||
752 | case ODPAT_OUTPUT_GROUP: | |
753 | if (a->output_group.group >= DP_MAX_GROUPS) | |
754 | return -EINVAL; | |
755 | break; | |
756 | ||
757 | case ODPAT_SET_VLAN_VID: | |
758 | if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) | |
759 | return -EINVAL; | |
760 | break; | |
761 | ||
762 | case ODPAT_SET_VLAN_PCP: | |
763 | if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) | |
764 | return -EINVAL; | |
765 | break; | |
766 | ||
767 | default: | |
768 | if (a->type >= ODPAT_N_ACTIONS) | |
769 | return -EOPNOTSUPP; | |
770 | break; | |
771 | } | |
772 | } | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
777 | static struct sw_flow_actions *get_actions(const struct odp_flow *flow) | |
778 | { | |
779 | struct sw_flow_actions *actions; | |
780 | int error; | |
781 | ||
782 | actions = flow_actions_alloc(flow->n_actions); | |
783 | error = PTR_ERR(actions); | |
784 | if (IS_ERR(actions)) | |
785 | goto error; | |
786 | ||
787 | error = -EFAULT; | |
788 | if (copy_from_user(actions->actions, flow->actions, | |
789 | flow->n_actions * sizeof(union odp_action))) | |
790 | goto error_free_actions; | |
791 | error = validate_actions(actions); | |
792 | if (error) | |
793 | goto error_free_actions; | |
794 | ||
795 | return actions; | |
796 | ||
797 | error_free_actions: | |
798 | kfree(actions); | |
799 | error: | |
800 | return ERR_PTR(error); | |
801 | } | |
802 | ||
803 | static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats) | |
804 | { | |
805 | if (flow->used.tv_sec) { | |
806 | stats->used_sec = flow->used.tv_sec; | |
807 | stats->used_nsec = flow->used.tv_nsec; | |
808 | } else { | |
809 | stats->used_sec = 0; | |
810 | stats->used_nsec = 0; | |
811 | } | |
812 | stats->n_packets = flow->packet_count; | |
813 | stats->n_bytes = flow->byte_count; | |
814 | stats->ip_tos = flow->ip_tos; | |
815 | stats->tcp_flags = flow->tcp_flags; | |
816 | } | |
817 | ||
818 | static void clear_stats(struct sw_flow *flow) | |
819 | { | |
820 | flow->used.tv_sec = flow->used.tv_nsec = 0; | |
821 | flow->tcp_flags = 0; | |
822 | flow->ip_tos = 0; | |
823 | flow->packet_count = 0; | |
824 | flow->byte_count = 0; | |
825 | } | |
826 | ||
827 | static int put_flow(struct datapath *dp, struct odp_flow_put __user *ufp) | |
828 | { | |
829 | struct odp_flow_put uf; | |
830 | struct sw_flow *flow, **bucket; | |
831 | struct dp_table *table; | |
832 | struct odp_flow_stats stats; | |
833 | int error; | |
834 | ||
835 | error = -EFAULT; | |
836 | if (copy_from_user(&uf, ufp, sizeof(struct odp_flow_put))) | |
837 | goto error; | |
838 | uf.flow.key.reserved = 0; | |
839 | ||
840 | retry: | |
841 | table = rcu_dereference(dp->table); | |
842 | bucket = dp_table_lookup_for_insert(table, &uf.flow.key); | |
843 | if (!bucket) { | |
844 | /* No such flow, and the slots where it could go are full. */ | |
845 | error = uf.flags & ODPPF_CREATE ? -EXFULL : -ENOENT; | |
846 | goto error; | |
847 | } else if (!*bucket) { | |
848 | /* No such flow, but we found an available slot for it. */ | |
849 | struct sw_flow_actions *acts; | |
850 | ||
851 | error = -ENOENT; | |
852 | if (!(uf.flags & ODPPF_CREATE)) | |
853 | goto error; | |
854 | ||
855 | /* Expand table, if necessary, to make room. */ | |
856 | if (dp->n_flows * 4 >= table->n_buckets && | |
857 | table->n_buckets < DP_MAX_BUCKETS) { | |
858 | error = dp_table_expand(dp); | |
859 | if (error) | |
860 | goto error; | |
861 | ||
862 | /* The bucket's location has changed. Try again. */ | |
863 | goto retry; | |
864 | } | |
865 | ||
866 | /* Allocate flow. */ | |
867 | error = -ENOMEM; | |
868 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
869 | if (flow == NULL) | |
870 | goto error; | |
871 | flow->key = uf.flow.key; | |
872 | spin_lock_init(&flow->lock); | |
873 | clear_stats(flow); | |
874 | ||
875 | /* Obtain actions. */ | |
876 | acts = get_actions(&uf.flow); | |
877 | error = PTR_ERR(acts); | |
878 | if (IS_ERR(acts)) | |
879 | goto error_free_flow; | |
880 | rcu_assign_pointer(flow->sf_acts, acts); | |
881 | ||
882 | /* Put flow in bucket. */ | |
883 | rcu_assign_pointer(*bucket, flow); | |
884 | dp->n_flows++; | |
885 | memset(&stats, 0, sizeof(struct odp_flow_stats)); | |
886 | } else { | |
887 | /* We found a matching flow. */ | |
888 | struct sw_flow *flow = *rcu_dereference(bucket); | |
889 | struct sw_flow_actions *old_acts, *new_acts; | |
890 | unsigned long int flags; | |
891 | ||
892 | /* Bail out if we're not allowed to modify an existing flow. */ | |
893 | error = -EEXIST; | |
894 | if (!(uf.flags & ODPPF_MODIFY)) | |
895 | goto error; | |
896 | ||
897 | /* Swap actions. */ | |
898 | new_acts = get_actions(&uf.flow); | |
899 | error = PTR_ERR(new_acts); | |
900 | if (IS_ERR(new_acts)) | |
901 | goto error; | |
902 | old_acts = rcu_dereference(flow->sf_acts); | |
903 | if (old_acts->n_actions != new_acts->n_actions || | |
904 | memcmp(old_acts->actions, new_acts->actions, | |
905 | sizeof(union odp_action) * old_acts->n_actions)) { | |
906 | rcu_assign_pointer(flow->sf_acts, new_acts); | |
907 | flow_deferred_free_acts(old_acts); | |
908 | } else { | |
909 | kfree(new_acts); | |
910 | } | |
911 | ||
912 | /* Fetch stats, then clear them if necessary. */ | |
913 | spin_lock_irqsave(&flow->lock, flags); | |
914 | get_stats(flow, &stats); | |
915 | if (uf.flags & ODPPF_ZERO_STATS) | |
916 | clear_stats(flow); | |
917 | spin_unlock_irqrestore(&flow->lock, flags); | |
918 | } | |
919 | ||
920 | /* Copy stats to userspace. */ | |
921 | if (__copy_to_user(&ufp->flow.stats, &stats, | |
922 | sizeof(struct odp_flow_stats))) | |
923 | return -EFAULT; | |
924 | return 0; | |
925 | ||
926 | error_free_flow: | |
927 | kmem_cache_free(flow_cache, flow); | |
928 | error: | |
929 | return error; | |
930 | } | |
931 | ||
932 | static int put_actions(const struct sw_flow *flow, struct odp_flow __user *ufp) | |
933 | { | |
934 | union odp_action __user *actions; | |
935 | struct sw_flow_actions *sf_acts; | |
936 | u32 n_actions; | |
937 | ||
938 | if (__get_user(actions, &ufp->actions) || | |
939 | __get_user(n_actions, &ufp->n_actions)) | |
940 | return -EFAULT; | |
941 | ||
942 | if (!n_actions) | |
943 | return 0; | |
944 | if (ufp->n_actions > INT_MAX / sizeof(union odp_action)) | |
945 | return -EINVAL; | |
946 | ||
947 | sf_acts = rcu_dereference(flow->sf_acts); | |
948 | if (__put_user(sf_acts->n_actions, &ufp->n_actions) || | |
949 | (actions && copy_to_user(actions, sf_acts->actions, | |
950 | sizeof(union odp_action) * | |
951 | min(sf_acts->n_actions, n_actions)))) | |
952 | return -EFAULT; | |
953 | ||
954 | return 0; | |
955 | } | |
956 | ||
957 | static int answer_query(struct sw_flow *flow, struct odp_flow __user *ufp) | |
958 | { | |
959 | struct odp_flow_stats stats; | |
960 | unsigned long int flags; | |
961 | ||
962 | spin_lock_irqsave(&flow->lock, flags); | |
963 | get_stats(flow, &stats); | |
964 | spin_unlock_irqrestore(&flow->lock, flags); | |
965 | ||
966 | if (__copy_to_user(&ufp->stats, &stats, sizeof(struct odp_flow_stats))) | |
967 | return -EFAULT; | |
968 | return put_actions(flow, ufp); | |
969 | } | |
970 | ||
971 | static int del_or_query_flow(struct datapath *dp, | |
972 | struct odp_flow __user *ufp, | |
973 | unsigned int cmd) | |
974 | { | |
975 | struct dp_table *table = rcu_dereference(dp->table); | |
976 | struct odp_flow uf; | |
977 | struct sw_flow *flow; | |
978 | int error; | |
979 | ||
980 | error = -EFAULT; | |
981 | if (copy_from_user(&uf, ufp, sizeof uf)) | |
982 | goto error; | |
983 | uf.key.reserved = 0; | |
984 | ||
985 | flow = dp_table_lookup(table, &uf.key); | |
986 | error = -ENOENT; | |
987 | if (!flow) | |
988 | goto error; | |
989 | ||
990 | if (cmd == ODP_FLOW_DEL) { | |
991 | /* XXX redundant lookup */ | |
992 | error = dp_table_delete(table, flow); | |
993 | if (error) | |
994 | goto error; | |
995 | ||
996 | /* XXX These statistics might lose a few packets, since other | |
997 | * CPUs can be using this flow. We used to synchronize_rcu() | |
998 | * to make sure that we get completely accurate stats, but that | |
999 | * blows our performance, badly. */ | |
1000 | dp->n_flows--; | |
1001 | error = answer_query(flow, ufp); | |
1002 | flow_deferred_free(flow); | |
1003 | } else { | |
1004 | error = answer_query(flow, ufp); | |
1005 | } | |
1006 | ||
1007 | error: | |
1008 | return error; | |
1009 | } | |
1010 | ||
1011 | static int query_multiple_flows(struct datapath *dp, | |
1012 | const struct odp_flowvec *flowvec) | |
1013 | { | |
1014 | struct dp_table *table = rcu_dereference(dp->table); | |
1015 | int i; | |
1016 | for (i = 0; i < flowvec->n_flows; i++) { | |
1017 | struct __user odp_flow *ufp = &flowvec->flows[i]; | |
1018 | struct odp_flow uf; | |
1019 | struct sw_flow *flow; | |
1020 | int error; | |
1021 | ||
1022 | if (__copy_from_user(&uf, ufp, sizeof uf)) | |
1023 | return -EFAULT; | |
1024 | uf.key.reserved = 0; | |
1025 | ||
1026 | flow = dp_table_lookup(table, &uf.key); | |
1027 | if (!flow) | |
1028 | error = __clear_user(&ufp->stats, sizeof ufp->stats); | |
1029 | else | |
1030 | error = answer_query(flow, ufp); | |
1031 | if (error) | |
1032 | return -EFAULT; | |
1033 | } | |
1034 | return flowvec->n_flows; | |
1035 | } | |
1036 | ||
1037 | struct list_flows_cbdata { | |
1038 | struct odp_flow __user *uflows; | |
1039 | int n_flows; | |
1040 | int listed_flows; | |
1041 | }; | |
1042 | ||
1043 | static int list_flow(struct sw_flow *flow, void *cbdata_) | |
1044 | { | |
1045 | struct list_flows_cbdata *cbdata = cbdata_; | |
1046 | struct odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++]; | |
1047 | int error; | |
1048 | ||
1049 | if (__copy_to_user(&ufp->key, &flow->key, sizeof flow->key)) | |
1050 | return -EFAULT; | |
1051 | error = answer_query(flow, ufp); | |
1052 | if (error) | |
1053 | return error; | |
1054 | ||
1055 | if (cbdata->listed_flows >= cbdata->n_flows) | |
1056 | return cbdata->listed_flows; | |
1057 | return 0; | |
1058 | } | |
1059 | ||
1060 | static int list_flows(struct datapath *dp, const struct odp_flowvec *flowvec) | |
1061 | { | |
1062 | struct list_flows_cbdata cbdata; | |
1063 | int error; | |
1064 | ||
1065 | if (!flowvec->n_flows) | |
1066 | return 0; | |
1067 | ||
1068 | cbdata.uflows = flowvec->flows; | |
1069 | cbdata.n_flows = flowvec->n_flows; | |
1070 | cbdata.listed_flows = 0; | |
1071 | error = dp_table_foreach(rcu_dereference(dp->table), | |
1072 | list_flow, &cbdata); | |
1073 | return error ? error : cbdata.listed_flows; | |
1074 | } | |
1075 | ||
1076 | static int do_flowvec_ioctl(struct datapath *dp, unsigned long argp, | |
1077 | int (*function)(struct datapath *, | |
1078 | const struct odp_flowvec *)) | |
1079 | { | |
1080 | struct odp_flowvec __user *uflowvec; | |
1081 | struct odp_flowvec flowvec; | |
1082 | int retval; | |
1083 | ||
1084 | uflowvec = (struct odp_flowvec __user *)argp; | |
1085 | if (!access_ok(VERIFY_WRITE, uflowvec, sizeof *uflowvec) || | |
1086 | copy_from_user(&flowvec, uflowvec, sizeof flowvec)) | |
1087 | return -EFAULT; | |
1088 | ||
1089 | if (flowvec.n_flows > INT_MAX / sizeof(struct odp_flow)) | |
1090 | return -EINVAL; | |
1091 | ||
1092 | if (!access_ok(VERIFY_WRITE, flowvec.flows, | |
1093 | flowvec.n_flows * sizeof(struct odp_flow))) | |
1094 | return -EFAULT; | |
1095 | ||
1096 | retval = function(dp, &flowvec); | |
1097 | return (retval < 0 ? retval | |
1098 | : retval == flowvec.n_flows ? 0 | |
1099 | : __put_user(retval, &uflowvec->n_flows)); | |
1100 | } | |
1101 | ||
1102 | static int do_execute(struct datapath *dp, const struct odp_execute *executep) | |
1103 | { | |
1104 | struct odp_execute execute; | |
1105 | struct odp_flow_key key; | |
1106 | struct sk_buff *skb; | |
1107 | struct sw_flow_actions *actions; | |
1108 | int err; | |
1109 | ||
1110 | err = -EFAULT; | |
1111 | if (copy_from_user(&execute, executep, sizeof execute)) | |
1112 | goto error; | |
1113 | ||
1114 | err = -EINVAL; | |
1115 | if (execute.length < ETH_HLEN || execute.length > 65535) | |
1116 | goto error; | |
1117 | ||
1118 | err = -ENOMEM; | |
1119 | actions = flow_actions_alloc(execute.n_actions); | |
1120 | if (!actions) | |
1121 | goto error; | |
1122 | ||
1123 | err = -EFAULT; | |
1124 | if (copy_from_user(actions->actions, execute.actions, | |
1125 | execute.n_actions * sizeof *execute.actions)) | |
1126 | goto error_free_actions; | |
1127 | ||
1128 | err = validate_actions(actions); | |
1129 | if (err) | |
1130 | goto error_free_actions; | |
1131 | ||
1132 | err = -ENOMEM; | |
1133 | skb = alloc_skb(execute.length, GFP_KERNEL); | |
1134 | if (!skb) | |
1135 | goto error_free_actions; | |
1136 | if (execute.in_port < DP_MAX_PORTS) { | |
1137 | struct net_bridge_port *p = dp->ports[execute.in_port]; | |
1138 | if (p) | |
1139 | skb->dev = p->dev; | |
1140 | } | |
1141 | ||
1142 | err = -EFAULT; | |
1143 | if (copy_from_user(skb_put(skb, execute.length), execute.data, | |
1144 | execute.length)) | |
1145 | goto error_free_skb; | |
1146 | ||
1147 | flow_extract(skb, execute.in_port, &key); | |
1148 | err = execute_actions(dp, skb, &key, actions->actions, | |
1149 | actions->n_actions, GFP_KERNEL); | |
1150 | kfree(actions); | |
1151 | return err; | |
1152 | ||
1153 | error_free_skb: | |
1154 | kfree_skb(skb); | |
1155 | error_free_actions: | |
1156 | kfree(actions); | |
1157 | error: | |
1158 | return err; | |
1159 | } | |
1160 | ||
1161 | static int | |
1162 | get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp) | |
1163 | { | |
1164 | struct odp_stats stats; | |
1165 | int i; | |
1166 | ||
1167 | stats.n_flows = dp->n_flows; | |
1168 | stats.cur_capacity = rcu_dereference(dp->table)->n_buckets * 2; | |
1169 | stats.max_capacity = DP_MAX_BUCKETS * 2; | |
1170 | stats.n_ports = dp->n_ports; | |
1171 | stats.max_ports = DP_MAX_PORTS; | |
1172 | stats.max_groups = DP_MAX_GROUPS; | |
1173 | stats.n_frags = stats.n_hit = stats.n_missed = stats.n_lost = 0; | |
1174 | for_each_possible_cpu(i) { | |
1175 | const struct dp_stats_percpu *s; | |
1176 | s = percpu_ptr(dp->stats_percpu, i); | |
1177 | stats.n_frags += s->n_frags; | |
1178 | stats.n_hit += s->n_hit; | |
1179 | stats.n_missed += s->n_missed; | |
1180 | stats.n_lost += s->n_lost; | |
1181 | } | |
1182 | stats.max_miss_queue = DP_MAX_QUEUE_LEN; | |
1183 | stats.max_action_queue = DP_MAX_QUEUE_LEN; | |
1184 | return copy_to_user(statsp, &stats, sizeof stats) ? -EFAULT : 0; | |
1185 | } | |
1186 | ||
1187 | static int | |
1188 | put_port(const struct net_bridge_port *p, struct odp_port __user *uop) | |
1189 | { | |
1190 | struct odp_port op; | |
1191 | memset(&op, 0, sizeof op); | |
1192 | strncpy(op.devname, p->dev->name, sizeof op.devname); | |
1193 | op.port = p->port_no; | |
1194 | op.flags = is_dp_dev(p->dev) ? ODP_PORT_INTERNAL : 0; | |
1195 | return copy_to_user(uop, &op, sizeof op) ? -EFAULT : 0; | |
1196 | } | |
1197 | ||
1198 | static int | |
1199 | query_port(struct datapath *dp, struct odp_port __user *uport) | |
1200 | { | |
1201 | struct odp_port port; | |
1202 | ||
1203 | if (copy_from_user(&port, uport, sizeof port)) | |
1204 | return -EFAULT; | |
1205 | if (port.devname[0]) { | |
1206 | struct net_bridge_port *p; | |
1207 | struct net_device *dev; | |
1208 | int err; | |
1209 | ||
1210 | port.devname[IFNAMSIZ - 1] = '\0'; | |
1211 | ||
1212 | dev = dev_get_by_name(&init_net, port.devname); | |
1213 | if (!dev) | |
1214 | return -ENODEV; | |
1215 | ||
1216 | p = dev->br_port; | |
1217 | if (!p && is_dp_dev(dev)) { | |
1218 | struct dp_dev *dp_dev = dp_dev_priv(dev); | |
1219 | if (dp_dev->dp == dp) | |
1220 | p = dp->ports[dp_dev->port_no]; | |
1221 | } | |
1222 | err = p && p->dp == dp ? put_port(p, uport) : -ENOENT; | |
1223 | dev_put(dev); | |
1224 | ||
1225 | return err; | |
1226 | } else { | |
1227 | if (port.port >= DP_MAX_PORTS) | |
1228 | return -EINVAL; | |
1229 | if (!dp->ports[port.port]) | |
1230 | return -ENOENT; | |
1231 | return put_port(dp->ports[port.port], uport); | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | static int | |
1236 | list_ports(struct datapath *dp, struct odp_portvec __user *pvp) | |
1237 | { | |
1238 | struct odp_portvec pv; | |
1239 | struct net_bridge_port *p; | |
1240 | int idx; | |
1241 | ||
1242 | if (copy_from_user(&pv, pvp, sizeof pv)) | |
1243 | return -EFAULT; | |
1244 | ||
1245 | idx = 0; | |
1246 | if (pv.n_ports) { | |
1247 | list_for_each_entry_rcu (p, &dp->port_list, node) { | |
1248 | if (put_port(p, &pv.ports[idx])) | |
1249 | return -EFAULT; | |
1250 | if (idx++ >= pv.n_ports) | |
1251 | break; | |
1252 | } | |
1253 | } | |
1254 | return put_user(idx, &pvp->n_ports); | |
1255 | } | |
1256 | ||
1257 | /* RCU callback for freeing a dp_port_group */ | |
1258 | static void free_port_group(struct rcu_head *rcu) | |
1259 | { | |
1260 | struct dp_port_group *g = container_of(rcu, struct dp_port_group, rcu); | |
1261 | kfree(g); | |
1262 | } | |
1263 | ||
1264 | static int | |
1265 | set_port_group(struct datapath *dp, const struct odp_port_group __user *upg) | |
1266 | { | |
1267 | struct odp_port_group pg; | |
1268 | struct dp_port_group *new_group, *old_group; | |
1269 | int error; | |
1270 | ||
1271 | error = -EFAULT; | |
1272 | if (copy_from_user(&pg, upg, sizeof pg)) | |
1273 | goto error; | |
1274 | ||
1275 | error = -EINVAL; | |
1276 | if (pg.n_ports > DP_MAX_PORTS || pg.group >= DP_MAX_GROUPS) | |
1277 | goto error; | |
1278 | ||
1279 | error = -ENOMEM; | |
1280 | new_group = kmalloc(sizeof *new_group + sizeof(u16) * pg.n_ports, | |
1281 | GFP_KERNEL); | |
1282 | if (!new_group) | |
1283 | goto error; | |
1284 | ||
1285 | new_group->n_ports = pg.n_ports; | |
1286 | error = -EFAULT; | |
1287 | if (copy_from_user(new_group->ports, pg.ports, | |
1288 | sizeof(u16) * pg.n_ports)) | |
1289 | goto error_free; | |
1290 | ||
1291 | old_group = rcu_dereference(dp->groups[pg.group]); | |
1292 | rcu_assign_pointer(dp->groups[pg.group], new_group); | |
1293 | if (old_group) | |
1294 | call_rcu(&old_group->rcu, free_port_group); | |
1295 | return 0; | |
1296 | ||
1297 | error_free: | |
1298 | kfree(new_group); | |
1299 | error: | |
1300 | return error; | |
1301 | } | |
1302 | ||
1303 | static int | |
1304 | get_port_group(struct datapath *dp, struct odp_port_group *upg) | |
1305 | { | |
1306 | struct odp_port_group pg; | |
1307 | struct dp_port_group *g; | |
1308 | u16 n_copy; | |
1309 | ||
1310 | if (copy_from_user(&pg, upg, sizeof pg)) | |
1311 | return -EFAULT; | |
1312 | ||
1313 | if (pg.group >= DP_MAX_GROUPS) | |
1314 | return -EINVAL; | |
1315 | ||
1316 | g = dp->groups[pg.group]; | |
1317 | n_copy = g ? min_t(int, g->n_ports, pg.n_ports) : 0; | |
1318 | if (n_copy && copy_to_user(pg.ports, g->ports, n_copy * sizeof(u16))) | |
1319 | return -EFAULT; | |
1320 | ||
1321 | if (put_user(g ? g->n_ports : 0, &upg->n_ports)) | |
1322 | return -EFAULT; | |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | static long openvswitch_ioctl(struct file *f, unsigned int cmd, | |
1328 | unsigned long argp) | |
1329 | { | |
1330 | int dp_idx = iminor(f->f_dentry->d_inode); | |
1331 | struct datapath *dp; | |
1332 | int drop_frags, listeners, port_no; | |
1333 | int err; | |
1334 | ||
1335 | /* Handle commands with special locking requirements up front. */ | |
1336 | switch (cmd) { | |
1337 | case ODP_DP_CREATE: | |
1338 | return create_dp(dp_idx, (char __user *)argp); | |
1339 | ||
1340 | case ODP_DP_DESTROY: | |
1341 | return destroy_dp(dp_idx); | |
1342 | ||
1343 | case ODP_PORT_ADD: | |
1344 | return add_port(dp_idx, (struct odp_port __user *)argp); | |
1345 | ||
1346 | case ODP_PORT_DEL: | |
1347 | err = get_user(port_no, (int __user *)argp); | |
1348 | if (err) | |
1349 | break; | |
1350 | return del_port(dp_idx, port_no); | |
1351 | } | |
1352 | ||
1353 | dp = get_dp_locked(dp_idx); | |
1354 | if (!dp) | |
1355 | return -ENODEV; | |
1356 | ||
1357 | switch (cmd) { | |
1358 | case ODP_DP_STATS: | |
1359 | err = get_dp_stats(dp, (struct odp_stats __user *)argp); | |
1360 | break; | |
1361 | ||
1362 | case ODP_GET_DROP_FRAGS: | |
1363 | err = put_user(dp->drop_frags, (int __user *)argp); | |
1364 | break; | |
1365 | ||
1366 | case ODP_SET_DROP_FRAGS: | |
1367 | err = get_user(drop_frags, (int __user *)argp); | |
1368 | if (err) | |
1369 | break; | |
1370 | err = -EINVAL; | |
1371 | if (drop_frags != 0 && drop_frags != 1) | |
1372 | break; | |
1373 | dp->drop_frags = drop_frags; | |
1374 | err = 0; | |
1375 | break; | |
1376 | ||
1377 | case ODP_GET_LISTEN_MASK: | |
1378 | err = put_user((int)f->private_data, (int __user *)argp); | |
1379 | break; | |
1380 | ||
1381 | case ODP_SET_LISTEN_MASK: | |
1382 | err = get_user(listeners, (int __user *)argp); | |
1383 | if (err) | |
1384 | break; | |
1385 | err = -EINVAL; | |
1386 | if (listeners & ~ODPL_ALL) | |
1387 | break; | |
1388 | err = 0; | |
1389 | f->private_data = (void*)listeners; | |
1390 | break; | |
1391 | ||
1392 | case ODP_PORT_QUERY: | |
1393 | err = query_port(dp, (struct odp_port __user *)argp); | |
1394 | break; | |
1395 | ||
1396 | case ODP_PORT_LIST: | |
1397 | err = list_ports(dp, (struct odp_portvec __user *)argp); | |
1398 | break; | |
1399 | ||
1400 | case ODP_PORT_GROUP_SET: | |
1401 | err = set_port_group(dp, (struct odp_port_group __user *)argp); | |
1402 | break; | |
1403 | ||
1404 | case ODP_PORT_GROUP_GET: | |
1405 | err = get_port_group(dp, (struct odp_port_group __user *)argp); | |
1406 | break; | |
1407 | ||
1408 | case ODP_FLOW_FLUSH: | |
1409 | err = flush_flows(dp); | |
1410 | break; | |
1411 | ||
1412 | case ODP_FLOW_PUT: | |
1413 | err = put_flow(dp, (struct odp_flow_put __user *)argp); | |
1414 | break; | |
1415 | ||
1416 | case ODP_FLOW_DEL: | |
1417 | case ODP_FLOW_GET: | |
1418 | err = del_or_query_flow(dp, (struct odp_flow __user *)argp, | |
1419 | cmd); | |
1420 | break; | |
1421 | ||
1422 | case ODP_FLOW_GET_MULTIPLE: | |
1423 | err = do_flowvec_ioctl(dp, argp, query_multiple_flows); | |
1424 | break; | |
1425 | ||
1426 | case ODP_FLOW_LIST: | |
1427 | err = do_flowvec_ioctl(dp, argp, list_flows); | |
1428 | break; | |
1429 | ||
1430 | case ODP_EXECUTE: | |
1431 | err = do_execute(dp, (struct odp_execute __user *)argp); | |
1432 | break; | |
1433 | ||
1434 | default: | |
1435 | err = -ENOIOCTLCMD; | |
1436 | break; | |
1437 | } | |
1438 | mutex_unlock(&dp->mutex); | |
1439 | return err; | |
1440 | } | |
1441 | ||
1442 | static int dp_has_packet_of_interest(struct datapath *dp, int listeners) | |
1443 | { | |
1444 | int i; | |
1445 | for (i = 0; i < DP_N_QUEUES; i++) { | |
1446 | if (listeners & (1 << i) && !skb_queue_empty(&dp->queues[i])) | |
1447 | return 1; | |
1448 | } | |
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | ssize_t openvswitch_read(struct file *f, char __user *buf, size_t nbytes, | |
1453 | loff_t *ppos) | |
1454 | { | |
6fba0d0b | 1455 | /* XXX is there sufficient synchronization here? */ |
064af421 BP |
1456 | int listeners = (int) f->private_data; |
1457 | int dp_idx = iminor(f->f_dentry->d_inode); | |
1458 | struct datapath *dp = get_dp(dp_idx); | |
1459 | struct sk_buff *skb; | |
1460 | struct iovec __user iov; | |
1461 | size_t copy_bytes; | |
1462 | int retval; | |
1463 | ||
1464 | if (!dp) | |
1465 | return -ENODEV; | |
1466 | ||
1467 | if (nbytes == 0 || !listeners) | |
1468 | return 0; | |
1469 | ||
1470 | for (;;) { | |
1471 | int i; | |
1472 | ||
1473 | for (i = 0; i < DP_N_QUEUES; i++) { | |
1474 | if (listeners & (1 << i)) { | |
1475 | skb = skb_dequeue(&dp->queues[i]); | |
1476 | if (skb) | |
1477 | goto success; | |
1478 | } | |
1479 | } | |
1480 | ||
1481 | if (f->f_flags & O_NONBLOCK) { | |
1482 | retval = -EAGAIN; | |
1483 | goto error; | |
1484 | } | |
1485 | ||
1486 | wait_event_interruptible(dp->waitqueue, | |
1487 | dp_has_packet_of_interest(dp, | |
1488 | listeners)); | |
1489 | ||
1490 | if (signal_pending(current)) { | |
1491 | retval = -ERESTARTSYS; | |
1492 | goto error; | |
1493 | } | |
1494 | } | |
1495 | success: | |
1496 | copy_bytes = min(skb->len, nbytes); | |
1497 | iov.iov_base = buf; | |
1498 | iov.iov_len = copy_bytes; | |
1499 | retval = skb_copy_datagram_iovec(skb, 0, &iov, iov.iov_len); | |
1500 | if (!retval) | |
1501 | retval = copy_bytes; | |
1502 | kfree_skb(skb); | |
1503 | ||
1504 | error: | |
1505 | return retval; | |
1506 | } | |
1507 | ||
1508 | static unsigned int openvswitch_poll(struct file *file, poll_table *wait) | |
1509 | { | |
6fba0d0b | 1510 | /* XXX is there sufficient synchronization here? */ |
064af421 BP |
1511 | int dp_idx = iminor(file->f_dentry->d_inode); |
1512 | struct datapath *dp = get_dp(dp_idx); | |
1513 | unsigned int mask; | |
1514 | ||
1515 | if (dp) { | |
1516 | mask = 0; | |
1517 | poll_wait(file, &dp->waitqueue, wait); | |
1518 | if (dp_has_packet_of_interest(dp, (int)file->private_data)) | |
1519 | mask |= POLLIN | POLLRDNORM; | |
1520 | } else { | |
1521 | mask = POLLIN | POLLRDNORM | POLLHUP; | |
1522 | } | |
1523 | return mask; | |
1524 | } | |
1525 | ||
1526 | struct file_operations openvswitch_fops = { | |
1527 | /* XXX .aio_read = openvswitch_aio_read, */ | |
1528 | .read = openvswitch_read, | |
1529 | .poll = openvswitch_poll, | |
1530 | .unlocked_ioctl = openvswitch_ioctl, | |
1531 | /* XXX .fasync = openvswitch_fasync, */ | |
1532 | }; | |
1533 | ||
1534 | static int major; | |
1535 | static struct llc_sap *dp_stp_sap; | |
1536 | ||
1537 | static int dp_stp_rcv(struct sk_buff *skb, struct net_device *dev, | |
1538 | struct packet_type *pt, struct net_device *orig_dev) | |
1539 | { | |
1540 | /* We don't really care about STP packets, we just listen for them for | |
1541 | * mutual exclusion with the bridge module, so this just discards | |
1542 | * them. */ | |
1543 | kfree_skb(skb); | |
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | static int __init dp_init(void) | |
1548 | { | |
1549 | int err; | |
1550 | ||
1551 | printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR); | |
1552 | ||
1553 | /* Register to receive STP packets because the bridge module also | |
1554 | * attempts to do so. Since there can only be a single listener for a | |
1555 | * given protocol, this provides mutual exclusion against the bridge | |
1556 | * module, preventing both of them from being loaded at the same | |
1557 | * time. */ | |
1558 | dp_stp_sap = llc_sap_open(LLC_SAP_BSPAN, dp_stp_rcv); | |
1559 | if (!dp_stp_sap) { | |
1560 | printk(KERN_ERR "openvswitch: can't register sap for STP (probably the bridge module is loaded)\n"); | |
1561 | return -EADDRINUSE; | |
1562 | } | |
1563 | ||
1564 | err = flow_init(); | |
1565 | if (err) | |
1566 | goto error; | |
1567 | ||
1568 | err = register_netdevice_notifier(&dp_device_notifier); | |
1569 | if (err) | |
1570 | goto error_flow_exit; | |
1571 | ||
1572 | major = register_chrdev(0, "openvswitch", &openvswitch_fops); | |
1573 | if (err < 0) | |
1574 | goto error_unreg_notifier; | |
1575 | ||
1576 | /* Hook into callback used by the bridge to intercept packets. | |
1577 | * Parasites we are. */ | |
1578 | br_handle_frame_hook = dp_frame_hook; | |
1579 | ||
1580 | return 0; | |
1581 | ||
1582 | error_unreg_notifier: | |
1583 | unregister_netdevice_notifier(&dp_device_notifier); | |
1584 | error_flow_exit: | |
1585 | flow_exit(); | |
1586 | error: | |
1587 | return err; | |
1588 | } | |
1589 | ||
1590 | static void dp_cleanup(void) | |
1591 | { | |
1592 | rcu_barrier(); | |
1593 | unregister_chrdev(major, "openvswitch"); | |
1594 | unregister_netdevice_notifier(&dp_device_notifier); | |
1595 | flow_exit(); | |
1596 | br_handle_frame_hook = NULL; | |
1597 | llc_sap_put(dp_stp_sap); | |
1598 | } | |
1599 | ||
1600 | module_init(dp_init); | |
1601 | module_exit(dp_cleanup); | |
1602 | ||
1603 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
1604 | MODULE_LICENSE("GPL"); |