Commit | Line | Data |
---|---|---|
3d249d4c JP |
1 | /* |
2 | * net/drivers/team/team.c - Network team device driver | |
3 | * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/types.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/rcupdate.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/ctype.h> | |
19 | #include <linux/notifier.h> | |
20 | #include <linux/netdevice.h> | |
87002b03 | 21 | #include <linux/if_vlan.h> |
3d249d4c JP |
22 | #include <linux/if_arp.h> |
23 | #include <linux/socket.h> | |
24 | #include <linux/etherdevice.h> | |
25 | #include <linux/rtnetlink.h> | |
26 | #include <net/rtnetlink.h> | |
27 | #include <net/genetlink.h> | |
28 | #include <net/netlink.h> | |
29 | #include <linux/if_team.h> | |
30 | ||
31 | #define DRV_NAME "team" | |
32 | ||
33 | ||
34 | /********** | |
35 | * Helpers | |
36 | **********/ | |
37 | ||
38 | #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT) | |
39 | ||
40 | static struct team_port *team_port_get_rcu(const struct net_device *dev) | |
41 | { | |
42 | struct team_port *port = rcu_dereference(dev->rx_handler_data); | |
43 | ||
44 | return team_port_exists(dev) ? port : NULL; | |
45 | } | |
46 | ||
47 | static struct team_port *team_port_get_rtnl(const struct net_device *dev) | |
48 | { | |
49 | struct team_port *port = rtnl_dereference(dev->rx_handler_data); | |
50 | ||
51 | return team_port_exists(dev) ? port : NULL; | |
52 | } | |
53 | ||
54 | /* | |
55 | * Since the ability to change mac address for open port device is tested in | |
56 | * team_port_add, this function can be called without control of return value | |
57 | */ | |
58 | static int __set_port_mac(struct net_device *port_dev, | |
59 | const unsigned char *dev_addr) | |
60 | { | |
61 | struct sockaddr addr; | |
62 | ||
63 | memcpy(addr.sa_data, dev_addr, ETH_ALEN); | |
64 | addr.sa_family = ARPHRD_ETHER; | |
65 | return dev_set_mac_address(port_dev, &addr); | |
66 | } | |
67 | ||
cade4555 | 68 | static int team_port_set_orig_mac(struct team_port *port) |
3d249d4c JP |
69 | { |
70 | return __set_port_mac(port->dev, port->orig.dev_addr); | |
71 | } | |
72 | ||
73 | int team_port_set_team_mac(struct team_port *port) | |
74 | { | |
75 | return __set_port_mac(port->dev, port->team->dev->dev_addr); | |
76 | } | |
77 | EXPORT_SYMBOL(team_port_set_team_mac); | |
78 | ||
71472ec1 JP |
79 | static void team_refresh_port_linkup(struct team_port *port) |
80 | { | |
81 | port->linkup = port->user.linkup_enabled ? port->user.linkup : | |
82 | port->state.linkup; | |
83 | } | |
3d249d4c JP |
84 | |
85 | /******************* | |
86 | * Options handling | |
87 | *******************/ | |
88 | ||
80f7c668 JP |
89 | struct team_option_inst { /* One for each option instance */ |
90 | struct list_head list; | |
91 | struct team_option *option; | |
92 | struct team_port *port; /* != NULL if per-port */ | |
93 | bool changed; | |
94 | bool removed; | |
95 | }; | |
96 | ||
97 | static struct team_option *__team_find_option(struct team *team, | |
98 | const char *opt_name) | |
358b8382 JP |
99 | { |
100 | struct team_option *option; | |
101 | ||
102 | list_for_each_entry(option, &team->option_list, list) { | |
103 | if (strcmp(option->name, opt_name) == 0) | |
104 | return option; | |
105 | } | |
106 | return NULL; | |
107 | } | |
108 | ||
80f7c668 JP |
109 | static int __team_option_inst_add(struct team *team, struct team_option *option, |
110 | struct team_port *port) | |
111 | { | |
112 | struct team_option_inst *opt_inst; | |
113 | ||
114 | opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL); | |
115 | if (!opt_inst) | |
116 | return -ENOMEM; | |
117 | opt_inst->option = option; | |
118 | opt_inst->port = port; | |
119 | opt_inst->changed = true; | |
120 | opt_inst->removed = false; | |
121 | list_add_tail(&opt_inst->list, &team->option_inst_list); | |
122 | return 0; | |
123 | } | |
124 | ||
125 | static void __team_option_inst_del(struct team_option_inst *opt_inst) | |
126 | { | |
127 | list_del(&opt_inst->list); | |
128 | kfree(opt_inst); | |
129 | } | |
130 | ||
131 | static void __team_option_inst_del_option(struct team *team, | |
132 | struct team_option *option) | |
133 | { | |
134 | struct team_option_inst *opt_inst, *tmp; | |
135 | ||
136 | list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) { | |
137 | if (opt_inst->option == option) | |
138 | __team_option_inst_del(opt_inst); | |
139 | } | |
140 | } | |
141 | ||
142 | static int __team_option_inst_add_option(struct team *team, | |
143 | struct team_option *option) | |
144 | { | |
145 | struct team_port *port; | |
146 | int err; | |
147 | ||
148 | if (!option->per_port) | |
149 | return __team_option_inst_add(team, option, 0); | |
150 | ||
151 | list_for_each_entry(port, &team->port_list, list) { | |
152 | err = __team_option_inst_add(team, option, port); | |
153 | if (err) | |
154 | goto inst_del_option; | |
155 | } | |
156 | return 0; | |
157 | ||
158 | inst_del_option: | |
159 | __team_option_inst_del_option(team, option); | |
160 | return err; | |
161 | } | |
162 | ||
163 | static void __team_option_inst_mark_removed_option(struct team *team, | |
164 | struct team_option *option) | |
165 | { | |
166 | struct team_option_inst *opt_inst; | |
167 | ||
168 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { | |
169 | if (opt_inst->option == option) { | |
170 | opt_inst->changed = true; | |
171 | opt_inst->removed = true; | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | static void __team_option_inst_del_port(struct team *team, | |
177 | struct team_port *port) | |
178 | { | |
179 | struct team_option_inst *opt_inst, *tmp; | |
180 | ||
181 | list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) { | |
182 | if (opt_inst->option->per_port && | |
183 | opt_inst->port == port) | |
184 | __team_option_inst_del(opt_inst); | |
185 | } | |
186 | } | |
187 | ||
188 | static int __team_option_inst_add_port(struct team *team, | |
189 | struct team_port *port) | |
190 | { | |
191 | struct team_option *option; | |
192 | int err; | |
193 | ||
194 | list_for_each_entry(option, &team->option_list, list) { | |
195 | if (!option->per_port) | |
196 | continue; | |
197 | err = __team_option_inst_add(team, option, port); | |
198 | if (err) | |
199 | goto inst_del_port; | |
200 | } | |
201 | return 0; | |
202 | ||
203 | inst_del_port: | |
204 | __team_option_inst_del_port(team, port); | |
205 | return err; | |
206 | } | |
207 | ||
208 | static void __team_option_inst_mark_removed_port(struct team *team, | |
209 | struct team_port *port) | |
210 | { | |
211 | struct team_option_inst *opt_inst; | |
212 | ||
213 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { | |
214 | if (opt_inst->port == port) { | |
215 | opt_inst->changed = true; | |
216 | opt_inst->removed = true; | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | static int __team_options_register(struct team *team, | |
222 | const struct team_option *option, | |
223 | size_t option_count) | |
3d249d4c JP |
224 | { |
225 | int i; | |
2bba19ff | 226 | struct team_option **dst_opts; |
358b8382 | 227 | int err; |
3d249d4c | 228 | |
2bba19ff JP |
229 | dst_opts = kzalloc(sizeof(struct team_option *) * option_count, |
230 | GFP_KERNEL); | |
231 | if (!dst_opts) | |
232 | return -ENOMEM; | |
358b8382 | 233 | for (i = 0; i < option_count; i++, option++) { |
358b8382 JP |
234 | if (__team_find_option(team, option->name)) { |
235 | err = -EEXIST; | |
80f7c668 | 236 | goto alloc_rollback; |
358b8382 | 237 | } |
f8a15af0 JP |
238 | dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL); |
239 | if (!dst_opts[i]) { | |
358b8382 | 240 | err = -ENOMEM; |
80f7c668 | 241 | goto alloc_rollback; |
358b8382 | 242 | } |
358b8382 JP |
243 | } |
244 | ||
b82b9183 | 245 | for (i = 0; i < option_count; i++) { |
80f7c668 JP |
246 | err = __team_option_inst_add_option(team, dst_opts[i]); |
247 | if (err) | |
248 | goto inst_rollback; | |
358b8382 | 249 | list_add_tail(&dst_opts[i]->list, &team->option_list); |
b82b9183 | 250 | } |
358b8382 | 251 | |
2bba19ff | 252 | kfree(dst_opts); |
358b8382 JP |
253 | return 0; |
254 | ||
80f7c668 JP |
255 | inst_rollback: |
256 | for (i--; i >= 0; i--) | |
257 | __team_option_inst_del_option(team, dst_opts[i]); | |
258 | ||
259 | i = option_count - 1; | |
260 | alloc_rollback: | |
261 | for (i--; i >= 0; i--) | |
358b8382 JP |
262 | kfree(dst_opts[i]); |
263 | ||
2bba19ff | 264 | kfree(dst_opts); |
358b8382 | 265 | return err; |
3d249d4c | 266 | } |
358b8382 | 267 | |
b82b9183 JP |
268 | static void __team_options_mark_removed(struct team *team, |
269 | const struct team_option *option, | |
270 | size_t option_count) | |
271 | { | |
272 | int i; | |
273 | ||
274 | for (i = 0; i < option_count; i++, option++) { | |
275 | struct team_option *del_opt; | |
3d249d4c | 276 | |
b82b9183 | 277 | del_opt = __team_find_option(team, option->name); |
80f7c668 JP |
278 | if (del_opt) |
279 | __team_option_inst_mark_removed_option(team, del_opt); | |
b82b9183 JP |
280 | } |
281 | } | |
3d249d4c JP |
282 | |
283 | static void __team_options_unregister(struct team *team, | |
358b8382 | 284 | const struct team_option *option, |
3d249d4c JP |
285 | size_t option_count) |
286 | { | |
287 | int i; | |
288 | ||
358b8382 JP |
289 | for (i = 0; i < option_count; i++, option++) { |
290 | struct team_option *del_opt; | |
291 | ||
292 | del_opt = __team_find_option(team, option->name); | |
293 | if (del_opt) { | |
80f7c668 | 294 | __team_option_inst_del_option(team, del_opt); |
358b8382 JP |
295 | list_del(&del_opt->list); |
296 | kfree(del_opt); | |
297 | } | |
298 | } | |
3d249d4c JP |
299 | } |
300 | ||
b82b9183 JP |
301 | static void __team_options_change_check(struct team *team); |
302 | ||
303 | int team_options_register(struct team *team, | |
304 | const struct team_option *option, | |
305 | size_t option_count) | |
306 | { | |
307 | int err; | |
308 | ||
309 | err = __team_options_register(team, option, option_count); | |
310 | if (err) | |
311 | return err; | |
312 | __team_options_change_check(team); | |
313 | return 0; | |
314 | } | |
315 | EXPORT_SYMBOL(team_options_register); | |
316 | ||
358b8382 JP |
317 | void team_options_unregister(struct team *team, |
318 | const struct team_option *option, | |
3d249d4c JP |
319 | size_t option_count) |
320 | { | |
b82b9183 JP |
321 | __team_options_mark_removed(team, option, option_count); |
322 | __team_options_change_check(team); | |
3d249d4c | 323 | __team_options_unregister(team, option, option_count); |
3d249d4c JP |
324 | } |
325 | EXPORT_SYMBOL(team_options_unregister); | |
326 | ||
80f7c668 | 327 | static int team_option_port_add(struct team *team, struct team_port *port) |
3d249d4c | 328 | { |
80f7c668 JP |
329 | int err; |
330 | ||
331 | err = __team_option_inst_add_port(team, port); | |
332 | if (err) | |
333 | return err; | |
334 | __team_options_change_check(team); | |
335 | return 0; | |
3d249d4c JP |
336 | } |
337 | ||
80f7c668 JP |
338 | static void team_option_port_del(struct team *team, struct team_port *port) |
339 | { | |
340 | __team_option_inst_mark_removed_port(team, port); | |
341 | __team_options_change_check(team); | |
342 | __team_option_inst_del_port(team, port); | |
343 | } | |
344 | ||
345 | static int team_option_get(struct team *team, | |
346 | struct team_option_inst *opt_inst, | |
347 | struct team_gsetter_ctx *ctx) | |
348 | { | |
349 | return opt_inst->option->getter(team, ctx); | |
350 | } | |
351 | ||
352 | static int team_option_set(struct team *team, | |
353 | struct team_option_inst *opt_inst, | |
354 | struct team_gsetter_ctx *ctx) | |
3d249d4c JP |
355 | { |
356 | int err; | |
357 | ||
80f7c668 | 358 | err = opt_inst->option->setter(team, ctx); |
3d249d4c JP |
359 | if (err) |
360 | return err; | |
361 | ||
80f7c668 | 362 | opt_inst->changed = true; |
b82b9183 | 363 | __team_options_change_check(team); |
3d249d4c JP |
364 | return err; |
365 | } | |
366 | ||
367 | /**************** | |
368 | * Mode handling | |
369 | ****************/ | |
370 | ||
371 | static LIST_HEAD(mode_list); | |
372 | static DEFINE_SPINLOCK(mode_list_lock); | |
373 | ||
0402788a JP |
374 | struct team_mode_item { |
375 | struct list_head list; | |
376 | const struct team_mode *mode; | |
377 | }; | |
378 | ||
379 | static struct team_mode_item *__find_mode(const char *kind) | |
3d249d4c | 380 | { |
0402788a | 381 | struct team_mode_item *mitem; |
3d249d4c | 382 | |
0402788a JP |
383 | list_for_each_entry(mitem, &mode_list, list) { |
384 | if (strcmp(mitem->mode->kind, kind) == 0) | |
385 | return mitem; | |
3d249d4c JP |
386 | } |
387 | return NULL; | |
388 | } | |
389 | ||
390 | static bool is_good_mode_name(const char *name) | |
391 | { | |
392 | while (*name != '\0') { | |
393 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
394 | return false; | |
395 | name++; | |
396 | } | |
397 | return true; | |
398 | } | |
399 | ||
0402788a | 400 | int team_mode_register(const struct team_mode *mode) |
3d249d4c JP |
401 | { |
402 | int err = 0; | |
0402788a | 403 | struct team_mode_item *mitem; |
3d249d4c JP |
404 | |
405 | if (!is_good_mode_name(mode->kind) || | |
406 | mode->priv_size > TEAM_MODE_PRIV_SIZE) | |
407 | return -EINVAL; | |
0402788a JP |
408 | |
409 | mitem = kmalloc(sizeof(*mitem), GFP_KERNEL); | |
410 | if (!mitem) | |
411 | return -ENOMEM; | |
412 | ||
3d249d4c JP |
413 | spin_lock(&mode_list_lock); |
414 | if (__find_mode(mode->kind)) { | |
415 | err = -EEXIST; | |
0402788a | 416 | kfree(mitem); |
3d249d4c JP |
417 | goto unlock; |
418 | } | |
0402788a JP |
419 | mitem->mode = mode; |
420 | list_add_tail(&mitem->list, &mode_list); | |
3d249d4c JP |
421 | unlock: |
422 | spin_unlock(&mode_list_lock); | |
423 | return err; | |
424 | } | |
425 | EXPORT_SYMBOL(team_mode_register); | |
426 | ||
0402788a | 427 | void team_mode_unregister(const struct team_mode *mode) |
3d249d4c | 428 | { |
0402788a JP |
429 | struct team_mode_item *mitem; |
430 | ||
3d249d4c | 431 | spin_lock(&mode_list_lock); |
0402788a JP |
432 | mitem = __find_mode(mode->kind); |
433 | if (mitem) { | |
434 | list_del_init(&mitem->list); | |
435 | kfree(mitem); | |
436 | } | |
3d249d4c | 437 | spin_unlock(&mode_list_lock); |
3d249d4c JP |
438 | } |
439 | EXPORT_SYMBOL(team_mode_unregister); | |
440 | ||
0402788a | 441 | static const struct team_mode *team_mode_get(const char *kind) |
3d249d4c | 442 | { |
0402788a JP |
443 | struct team_mode_item *mitem; |
444 | const struct team_mode *mode = NULL; | |
3d249d4c JP |
445 | |
446 | spin_lock(&mode_list_lock); | |
0402788a JP |
447 | mitem = __find_mode(kind); |
448 | if (!mitem) { | |
3d249d4c JP |
449 | spin_unlock(&mode_list_lock); |
450 | request_module("team-mode-%s", kind); | |
451 | spin_lock(&mode_list_lock); | |
0402788a | 452 | mitem = __find_mode(kind); |
3d249d4c | 453 | } |
0402788a JP |
454 | if (mitem) { |
455 | mode = mitem->mode; | |
3d249d4c JP |
456 | if (!try_module_get(mode->owner)) |
457 | mode = NULL; | |
0402788a | 458 | } |
3d249d4c JP |
459 | |
460 | spin_unlock(&mode_list_lock); | |
461 | return mode; | |
462 | } | |
463 | ||
464 | static void team_mode_put(const struct team_mode *mode) | |
465 | { | |
466 | module_put(mode->owner); | |
467 | } | |
468 | ||
469 | static bool team_dummy_transmit(struct team *team, struct sk_buff *skb) | |
470 | { | |
471 | dev_kfree_skb_any(skb); | |
472 | return false; | |
473 | } | |
474 | ||
475 | rx_handler_result_t team_dummy_receive(struct team *team, | |
476 | struct team_port *port, | |
477 | struct sk_buff *skb) | |
478 | { | |
479 | return RX_HANDLER_ANOTHER; | |
480 | } | |
481 | ||
482 | static void team_adjust_ops(struct team *team) | |
483 | { | |
484 | /* | |
485 | * To avoid checks in rx/tx skb paths, ensure here that non-null and | |
486 | * correct ops are always set. | |
487 | */ | |
488 | ||
489 | if (list_empty(&team->port_list) || | |
490 | !team->mode || !team->mode->ops->transmit) | |
491 | team->ops.transmit = team_dummy_transmit; | |
492 | else | |
493 | team->ops.transmit = team->mode->ops->transmit; | |
494 | ||
495 | if (list_empty(&team->port_list) || | |
496 | !team->mode || !team->mode->ops->receive) | |
497 | team->ops.receive = team_dummy_receive; | |
498 | else | |
499 | team->ops.receive = team->mode->ops->receive; | |
500 | } | |
501 | ||
502 | /* | |
503 | * We can benefit from the fact that it's ensured no port is present | |
504 | * at the time of mode change. Therefore no packets are in fly so there's no | |
505 | * need to set mode operations in any special way. | |
506 | */ | |
507 | static int __team_change_mode(struct team *team, | |
508 | const struct team_mode *new_mode) | |
509 | { | |
510 | /* Check if mode was previously set and do cleanup if so */ | |
511 | if (team->mode) { | |
512 | void (*exit_op)(struct team *team) = team->ops.exit; | |
513 | ||
514 | /* Clear ops area so no callback is called any longer */ | |
515 | memset(&team->ops, 0, sizeof(struct team_mode_ops)); | |
516 | team_adjust_ops(team); | |
517 | ||
518 | if (exit_op) | |
519 | exit_op(team); | |
520 | team_mode_put(team->mode); | |
521 | team->mode = NULL; | |
522 | /* zero private data area */ | |
523 | memset(&team->mode_priv, 0, | |
524 | sizeof(struct team) - offsetof(struct team, mode_priv)); | |
525 | } | |
526 | ||
527 | if (!new_mode) | |
528 | return 0; | |
529 | ||
530 | if (new_mode->ops->init) { | |
531 | int err; | |
532 | ||
533 | err = new_mode->ops->init(team); | |
534 | if (err) | |
535 | return err; | |
536 | } | |
537 | ||
538 | team->mode = new_mode; | |
539 | memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops)); | |
540 | team_adjust_ops(team); | |
541 | ||
542 | return 0; | |
543 | } | |
544 | ||
545 | static int team_change_mode(struct team *team, const char *kind) | |
546 | { | |
0402788a | 547 | const struct team_mode *new_mode; |
3d249d4c JP |
548 | struct net_device *dev = team->dev; |
549 | int err; | |
550 | ||
551 | if (!list_empty(&team->port_list)) { | |
552 | netdev_err(dev, "No ports can be present during mode change\n"); | |
553 | return -EBUSY; | |
554 | } | |
555 | ||
556 | if (team->mode && strcmp(team->mode->kind, kind) == 0) { | |
557 | netdev_err(dev, "Unable to change to the same mode the team is in\n"); | |
558 | return -EINVAL; | |
559 | } | |
560 | ||
561 | new_mode = team_mode_get(kind); | |
562 | if (!new_mode) { | |
563 | netdev_err(dev, "Mode \"%s\" not found\n", kind); | |
564 | return -EINVAL; | |
565 | } | |
566 | ||
567 | err = __team_change_mode(team, new_mode); | |
568 | if (err) { | |
569 | netdev_err(dev, "Failed to change to mode \"%s\"\n", kind); | |
570 | team_mode_put(new_mode); | |
571 | return err; | |
572 | } | |
573 | ||
574 | netdev_info(dev, "Mode changed to \"%s\"\n", kind); | |
575 | return 0; | |
576 | } | |
577 | ||
578 | ||
579 | /************************ | |
580 | * Rx path frame handler | |
581 | ************************/ | |
582 | ||
19a0b58e JP |
583 | static bool team_port_enabled(struct team_port *port); |
584 | ||
3d249d4c JP |
585 | /* note: already called with rcu_read_lock */ |
586 | static rx_handler_result_t team_handle_frame(struct sk_buff **pskb) | |
587 | { | |
588 | struct sk_buff *skb = *pskb; | |
589 | struct team_port *port; | |
590 | struct team *team; | |
591 | rx_handler_result_t res; | |
592 | ||
593 | skb = skb_share_check(skb, GFP_ATOMIC); | |
594 | if (!skb) | |
595 | return RX_HANDLER_CONSUMED; | |
596 | ||
597 | *pskb = skb; | |
598 | ||
599 | port = team_port_get_rcu(skb->dev); | |
600 | team = port->team; | |
19a0b58e JP |
601 | if (!team_port_enabled(port)) { |
602 | /* allow exact match delivery for disabled ports */ | |
603 | res = RX_HANDLER_EXACT; | |
604 | } else { | |
605 | res = team->ops.receive(team, port, skb); | |
606 | } | |
3d249d4c JP |
607 | if (res == RX_HANDLER_ANOTHER) { |
608 | struct team_pcpu_stats *pcpu_stats; | |
609 | ||
610 | pcpu_stats = this_cpu_ptr(team->pcpu_stats); | |
611 | u64_stats_update_begin(&pcpu_stats->syncp); | |
612 | pcpu_stats->rx_packets++; | |
613 | pcpu_stats->rx_bytes += skb->len; | |
614 | if (skb->pkt_type == PACKET_MULTICAST) | |
615 | pcpu_stats->rx_multicast++; | |
616 | u64_stats_update_end(&pcpu_stats->syncp); | |
617 | ||
618 | skb->dev = team->dev; | |
619 | } else { | |
620 | this_cpu_inc(team->pcpu_stats->rx_dropped); | |
621 | } | |
622 | ||
623 | return res; | |
624 | } | |
625 | ||
626 | ||
627 | /**************** | |
628 | * Port handling | |
629 | ****************/ | |
630 | ||
631 | static bool team_port_find(const struct team *team, | |
632 | const struct team_port *port) | |
633 | { | |
634 | struct team_port *cur; | |
635 | ||
636 | list_for_each_entry(cur, &team->port_list, list) | |
637 | if (cur == port) | |
638 | return true; | |
639 | return false; | |
640 | } | |
641 | ||
19a0b58e JP |
642 | static bool team_port_enabled(struct team_port *port) |
643 | { | |
644 | return port->index != -1; | |
645 | } | |
646 | ||
3d249d4c | 647 | /* |
19a0b58e JP |
648 | * Enable/disable port by adding to enabled port hashlist and setting |
649 | * port->index (Might be racy so reader could see incorrect ifindex when | |
650 | * processing a flying packet, but that is not a problem). Write guarded | |
651 | * by team->lock. | |
3d249d4c | 652 | */ |
19a0b58e JP |
653 | static void team_port_enable(struct team *team, |
654 | struct team_port *port) | |
3d249d4c | 655 | { |
19a0b58e JP |
656 | if (team_port_enabled(port)) |
657 | return; | |
658 | port->index = team->en_port_count++; | |
3d249d4c JP |
659 | hlist_add_head_rcu(&port->hlist, |
660 | team_port_index_hash(team, port->index)); | |
3d249d4c JP |
661 | } |
662 | ||
663 | static void __reconstruct_port_hlist(struct team *team, int rm_index) | |
664 | { | |
665 | int i; | |
666 | struct team_port *port; | |
667 | ||
19a0b58e | 668 | for (i = rm_index + 1; i < team->en_port_count; i++) { |
3d249d4c JP |
669 | port = team_get_port_by_index(team, i); |
670 | hlist_del_rcu(&port->hlist); | |
671 | port->index--; | |
672 | hlist_add_head_rcu(&port->hlist, | |
673 | team_port_index_hash(team, port->index)); | |
674 | } | |
675 | } | |
676 | ||
19a0b58e JP |
677 | static void team_port_disable(struct team *team, |
678 | struct team_port *port) | |
3d249d4c JP |
679 | { |
680 | int rm_index = port->index; | |
681 | ||
19a0b58e JP |
682 | if (!team_port_enabled(port)) |
683 | return; | |
3d249d4c | 684 | hlist_del_rcu(&port->hlist); |
3d249d4c | 685 | __reconstruct_port_hlist(team, rm_index); |
19a0b58e JP |
686 | team->en_port_count--; |
687 | port->index = -1; | |
3d249d4c JP |
688 | } |
689 | ||
690 | #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \ | |
691 | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \ | |
692 | NETIF_F_HIGHDMA | NETIF_F_LRO) | |
693 | ||
694 | static void __team_compute_features(struct team *team) | |
695 | { | |
696 | struct team_port *port; | |
697 | u32 vlan_features = TEAM_VLAN_FEATURES; | |
698 | unsigned short max_hard_header_len = ETH_HLEN; | |
699 | ||
700 | list_for_each_entry(port, &team->port_list, list) { | |
701 | vlan_features = netdev_increment_features(vlan_features, | |
702 | port->dev->vlan_features, | |
703 | TEAM_VLAN_FEATURES); | |
704 | ||
705 | if (port->dev->hard_header_len > max_hard_header_len) | |
706 | max_hard_header_len = port->dev->hard_header_len; | |
707 | } | |
708 | ||
709 | team->dev->vlan_features = vlan_features; | |
710 | team->dev->hard_header_len = max_hard_header_len; | |
711 | ||
712 | netdev_change_features(team->dev); | |
713 | } | |
714 | ||
715 | static void team_compute_features(struct team *team) | |
716 | { | |
61dc3461 | 717 | mutex_lock(&team->lock); |
3d249d4c | 718 | __team_compute_features(team); |
61dc3461 | 719 | mutex_unlock(&team->lock); |
3d249d4c JP |
720 | } |
721 | ||
722 | static int team_port_enter(struct team *team, struct team_port *port) | |
723 | { | |
724 | int err = 0; | |
725 | ||
726 | dev_hold(team->dev); | |
727 | port->dev->priv_flags |= IFF_TEAM_PORT; | |
728 | if (team->ops.port_enter) { | |
729 | err = team->ops.port_enter(team, port); | |
730 | if (err) { | |
731 | netdev_err(team->dev, "Device %s failed to enter team mode\n", | |
732 | port->dev->name); | |
733 | goto err_port_enter; | |
734 | } | |
735 | } | |
736 | ||
737 | return 0; | |
738 | ||
739 | err_port_enter: | |
740 | port->dev->priv_flags &= ~IFF_TEAM_PORT; | |
741 | dev_put(team->dev); | |
742 | ||
743 | return err; | |
744 | } | |
745 | ||
746 | static void team_port_leave(struct team *team, struct team_port *port) | |
747 | { | |
748 | if (team->ops.port_leave) | |
749 | team->ops.port_leave(team, port); | |
750 | port->dev->priv_flags &= ~IFF_TEAM_PORT; | |
751 | dev_put(team->dev); | |
752 | } | |
753 | ||
754 | static void __team_port_change_check(struct team_port *port, bool linkup); | |
755 | ||
756 | static int team_port_add(struct team *team, struct net_device *port_dev) | |
757 | { | |
758 | struct net_device *dev = team->dev; | |
759 | struct team_port *port; | |
760 | char *portname = port_dev->name; | |
761 | int err; | |
762 | ||
763 | if (port_dev->flags & IFF_LOOPBACK || | |
764 | port_dev->type != ARPHRD_ETHER) { | |
765 | netdev_err(dev, "Device %s is of an unsupported type\n", | |
766 | portname); | |
767 | return -EINVAL; | |
768 | } | |
769 | ||
770 | if (team_port_exists(port_dev)) { | |
771 | netdev_err(dev, "Device %s is already a port " | |
772 | "of a team device\n", portname); | |
773 | return -EBUSY; | |
774 | } | |
775 | ||
776 | if (port_dev->flags & IFF_UP) { | |
777 | netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n", | |
778 | portname); | |
779 | return -EBUSY; | |
780 | } | |
781 | ||
782 | port = kzalloc(sizeof(struct team_port), GFP_KERNEL); | |
783 | if (!port) | |
784 | return -ENOMEM; | |
785 | ||
786 | port->dev = port_dev; | |
787 | port->team = team; | |
788 | ||
789 | port->orig.mtu = port_dev->mtu; | |
790 | err = dev_set_mtu(port_dev, dev->mtu); | |
791 | if (err) { | |
792 | netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err); | |
793 | goto err_set_mtu; | |
794 | } | |
795 | ||
796 | memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN); | |
797 | ||
798 | err = team_port_enter(team, port); | |
799 | if (err) { | |
800 | netdev_err(dev, "Device %s failed to enter team mode\n", | |
801 | portname); | |
802 | goto err_port_enter; | |
803 | } | |
804 | ||
805 | err = dev_open(port_dev); | |
806 | if (err) { | |
807 | netdev_dbg(dev, "Device %s opening failed\n", | |
808 | portname); | |
809 | goto err_dev_open; | |
810 | } | |
811 | ||
57459185 JP |
812 | err = vlan_vids_add_by_dev(port_dev, dev); |
813 | if (err) { | |
814 | netdev_err(dev, "Failed to add vlan ids to device %s\n", | |
815 | portname); | |
816 | goto err_vids_add; | |
817 | } | |
818 | ||
3d249d4c JP |
819 | err = netdev_set_master(port_dev, dev); |
820 | if (err) { | |
821 | netdev_err(dev, "Device %s failed to set master\n", portname); | |
822 | goto err_set_master; | |
823 | } | |
824 | ||
825 | err = netdev_rx_handler_register(port_dev, team_handle_frame, | |
826 | port); | |
827 | if (err) { | |
828 | netdev_err(dev, "Device %s failed to register rx_handler\n", | |
829 | portname); | |
830 | goto err_handler_register; | |
831 | } | |
832 | ||
80f7c668 JP |
833 | err = team_option_port_add(team, port); |
834 | if (err) { | |
835 | netdev_err(dev, "Device %s failed to add per-port options\n", | |
836 | portname); | |
837 | goto err_option_port_add; | |
838 | } | |
839 | ||
19a0b58e JP |
840 | port->index = -1; |
841 | team_port_enable(team, port); | |
842 | list_add_tail_rcu(&port->list, &team->port_list); | |
3d249d4c JP |
843 | team_adjust_ops(team); |
844 | __team_compute_features(team); | |
845 | __team_port_change_check(port, !!netif_carrier_ok(port_dev)); | |
846 | ||
847 | netdev_info(dev, "Port device %s added\n", portname); | |
848 | ||
849 | return 0; | |
850 | ||
80f7c668 JP |
851 | err_option_port_add: |
852 | netdev_rx_handler_unregister(port_dev); | |
853 | ||
3d249d4c JP |
854 | err_handler_register: |
855 | netdev_set_master(port_dev, NULL); | |
856 | ||
857 | err_set_master: | |
57459185 JP |
858 | vlan_vids_del_by_dev(port_dev, dev); |
859 | ||
860 | err_vids_add: | |
3d249d4c JP |
861 | dev_close(port_dev); |
862 | ||
863 | err_dev_open: | |
864 | team_port_leave(team, port); | |
865 | team_port_set_orig_mac(port); | |
866 | ||
867 | err_port_enter: | |
868 | dev_set_mtu(port_dev, port->orig.mtu); | |
869 | ||
870 | err_set_mtu: | |
871 | kfree(port); | |
872 | ||
873 | return err; | |
874 | } | |
875 | ||
876 | static int team_port_del(struct team *team, struct net_device *port_dev) | |
877 | { | |
878 | struct net_device *dev = team->dev; | |
879 | struct team_port *port; | |
880 | char *portname = port_dev->name; | |
881 | ||
882 | port = team_port_get_rtnl(port_dev); | |
883 | if (!port || !team_port_find(team, port)) { | |
884 | netdev_err(dev, "Device %s does not act as a port of this team\n", | |
885 | portname); | |
886 | return -ENOENT; | |
887 | } | |
888 | ||
b82b9183 | 889 | port->removed = true; |
3d249d4c | 890 | __team_port_change_check(port, false); |
19a0b58e JP |
891 | team_port_disable(team, port); |
892 | list_del_rcu(&port->list); | |
3d249d4c | 893 | team_adjust_ops(team); |
80f7c668 | 894 | team_option_port_del(team, port); |
3d249d4c JP |
895 | netdev_rx_handler_unregister(port_dev); |
896 | netdev_set_master(port_dev, NULL); | |
57459185 | 897 | vlan_vids_del_by_dev(port_dev, dev); |
3d249d4c JP |
898 | dev_close(port_dev); |
899 | team_port_leave(team, port); | |
900 | team_port_set_orig_mac(port); | |
901 | dev_set_mtu(port_dev, port->orig.mtu); | |
902 | synchronize_rcu(); | |
903 | kfree(port); | |
904 | netdev_info(dev, "Port device %s removed\n", portname); | |
905 | __team_compute_features(team); | |
906 | ||
907 | return 0; | |
908 | } | |
909 | ||
910 | ||
911 | /***************** | |
912 | * Net device ops | |
913 | *****************/ | |
914 | ||
915 | static const char team_no_mode_kind[] = "*NOMODE*"; | |
916 | ||
80f7c668 | 917 | static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx) |
3d249d4c | 918 | { |
80f7c668 | 919 | ctx->data.str_val = team->mode ? team->mode->kind : team_no_mode_kind; |
3d249d4c JP |
920 | return 0; |
921 | } | |
922 | ||
80f7c668 | 923 | static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx) |
3d249d4c | 924 | { |
80f7c668 | 925 | return team_change_mode(team, ctx->data.str_val); |
3d249d4c JP |
926 | } |
927 | ||
acd69962 JP |
928 | static int team_port_en_option_get(struct team *team, |
929 | struct team_gsetter_ctx *ctx) | |
930 | { | |
931 | ctx->data.bool_val = team_port_enabled(ctx->port); | |
932 | return 0; | |
933 | } | |
934 | ||
935 | static int team_port_en_option_set(struct team *team, | |
936 | struct team_gsetter_ctx *ctx) | |
937 | { | |
938 | if (ctx->data.bool_val) | |
939 | team_port_enable(team, ctx->port); | |
940 | else | |
941 | team_port_disable(team, ctx->port); | |
942 | return 0; | |
943 | } | |
944 | ||
71472ec1 JP |
945 | static int team_user_linkup_option_get(struct team *team, |
946 | struct team_gsetter_ctx *ctx) | |
947 | { | |
948 | ctx->data.bool_val = ctx->port->user.linkup; | |
949 | return 0; | |
950 | } | |
951 | ||
952 | static int team_user_linkup_option_set(struct team *team, | |
953 | struct team_gsetter_ctx *ctx) | |
954 | { | |
955 | ctx->port->user.linkup = ctx->data.bool_val; | |
956 | team_refresh_port_linkup(ctx->port); | |
957 | return 0; | |
958 | } | |
959 | ||
960 | static int team_user_linkup_en_option_get(struct team *team, | |
961 | struct team_gsetter_ctx *ctx) | |
962 | { | |
963 | struct team_port *port = ctx->port; | |
964 | ||
965 | ctx->data.bool_val = port->user.linkup_enabled; | |
966 | return 0; | |
967 | } | |
968 | ||
969 | static int team_user_linkup_en_option_set(struct team *team, | |
970 | struct team_gsetter_ctx *ctx) | |
971 | { | |
972 | struct team_port *port = ctx->port; | |
973 | ||
974 | port->user.linkup_enabled = ctx->data.bool_val; | |
975 | team_refresh_port_linkup(ctx->port); | |
976 | return 0; | |
977 | } | |
978 | ||
358b8382 | 979 | static const struct team_option team_options[] = { |
3d249d4c JP |
980 | { |
981 | .name = "mode", | |
982 | .type = TEAM_OPTION_TYPE_STRING, | |
983 | .getter = team_mode_option_get, | |
984 | .setter = team_mode_option_set, | |
985 | }, | |
acd69962 JP |
986 | { |
987 | .name = "enabled", | |
988 | .type = TEAM_OPTION_TYPE_BOOL, | |
989 | .per_port = true, | |
990 | .getter = team_port_en_option_get, | |
991 | .setter = team_port_en_option_set, | |
992 | }, | |
71472ec1 JP |
993 | { |
994 | .name = "user_linkup", | |
995 | .type = TEAM_OPTION_TYPE_BOOL, | |
996 | .per_port = true, | |
997 | .getter = team_user_linkup_option_get, | |
998 | .setter = team_user_linkup_option_set, | |
999 | }, | |
1000 | { | |
1001 | .name = "user_linkup_enabled", | |
1002 | .type = TEAM_OPTION_TYPE_BOOL, | |
1003 | .per_port = true, | |
1004 | .getter = team_user_linkup_en_option_get, | |
1005 | .setter = team_user_linkup_en_option_set, | |
1006 | }, | |
3d249d4c JP |
1007 | }; |
1008 | ||
1009 | static int team_init(struct net_device *dev) | |
1010 | { | |
1011 | struct team *team = netdev_priv(dev); | |
1012 | int i; | |
358b8382 | 1013 | int err; |
3d249d4c JP |
1014 | |
1015 | team->dev = dev; | |
61dc3461 | 1016 | mutex_init(&team->lock); |
3d249d4c JP |
1017 | |
1018 | team->pcpu_stats = alloc_percpu(struct team_pcpu_stats); | |
1019 | if (!team->pcpu_stats) | |
1020 | return -ENOMEM; | |
1021 | ||
1022 | for (i = 0; i < TEAM_PORT_HASHENTRIES; i++) | |
19a0b58e | 1023 | INIT_HLIST_HEAD(&team->en_port_hlist[i]); |
3d249d4c JP |
1024 | INIT_LIST_HEAD(&team->port_list); |
1025 | ||
1026 | team_adjust_ops(team); | |
1027 | ||
1028 | INIT_LIST_HEAD(&team->option_list); | |
80f7c668 | 1029 | INIT_LIST_HEAD(&team->option_inst_list); |
358b8382 JP |
1030 | err = team_options_register(team, team_options, ARRAY_SIZE(team_options)); |
1031 | if (err) | |
1032 | goto err_options_register; | |
3d249d4c JP |
1033 | netif_carrier_off(dev); |
1034 | ||
1035 | return 0; | |
358b8382 JP |
1036 | |
1037 | err_options_register: | |
1038 | free_percpu(team->pcpu_stats); | |
1039 | ||
1040 | return err; | |
3d249d4c JP |
1041 | } |
1042 | ||
1043 | static void team_uninit(struct net_device *dev) | |
1044 | { | |
1045 | struct team *team = netdev_priv(dev); | |
1046 | struct team_port *port; | |
1047 | struct team_port *tmp; | |
1048 | ||
61dc3461 | 1049 | mutex_lock(&team->lock); |
3d249d4c JP |
1050 | list_for_each_entry_safe(port, tmp, &team->port_list, list) |
1051 | team_port_del(team, port->dev); | |
1052 | ||
1053 | __team_change_mode(team, NULL); /* cleanup */ | |
1054 | __team_options_unregister(team, team_options, ARRAY_SIZE(team_options)); | |
61dc3461 | 1055 | mutex_unlock(&team->lock); |
3d249d4c JP |
1056 | } |
1057 | ||
1058 | static void team_destructor(struct net_device *dev) | |
1059 | { | |
1060 | struct team *team = netdev_priv(dev); | |
1061 | ||
1062 | free_percpu(team->pcpu_stats); | |
1063 | free_netdev(dev); | |
1064 | } | |
1065 | ||
1066 | static int team_open(struct net_device *dev) | |
1067 | { | |
1068 | netif_carrier_on(dev); | |
1069 | return 0; | |
1070 | } | |
1071 | ||
1072 | static int team_close(struct net_device *dev) | |
1073 | { | |
1074 | netif_carrier_off(dev); | |
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * note: already called with rcu_read_lock | |
1080 | */ | |
1081 | static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev) | |
1082 | { | |
1083 | struct team *team = netdev_priv(dev); | |
1084 | bool tx_success = false; | |
1085 | unsigned int len = skb->len; | |
1086 | ||
1087 | tx_success = team->ops.transmit(team, skb); | |
1088 | if (tx_success) { | |
1089 | struct team_pcpu_stats *pcpu_stats; | |
1090 | ||
1091 | pcpu_stats = this_cpu_ptr(team->pcpu_stats); | |
1092 | u64_stats_update_begin(&pcpu_stats->syncp); | |
1093 | pcpu_stats->tx_packets++; | |
1094 | pcpu_stats->tx_bytes += len; | |
1095 | u64_stats_update_end(&pcpu_stats->syncp); | |
1096 | } else { | |
1097 | this_cpu_inc(team->pcpu_stats->tx_dropped); | |
1098 | } | |
1099 | ||
1100 | return NETDEV_TX_OK; | |
1101 | } | |
1102 | ||
1103 | static void team_change_rx_flags(struct net_device *dev, int change) | |
1104 | { | |
1105 | struct team *team = netdev_priv(dev); | |
1106 | struct team_port *port; | |
1107 | int inc; | |
1108 | ||
1109 | rcu_read_lock(); | |
1110 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
1111 | if (change & IFF_PROMISC) { | |
1112 | inc = dev->flags & IFF_PROMISC ? 1 : -1; | |
1113 | dev_set_promiscuity(port->dev, inc); | |
1114 | } | |
1115 | if (change & IFF_ALLMULTI) { | |
1116 | inc = dev->flags & IFF_ALLMULTI ? 1 : -1; | |
1117 | dev_set_allmulti(port->dev, inc); | |
1118 | } | |
1119 | } | |
1120 | rcu_read_unlock(); | |
1121 | } | |
1122 | ||
1123 | static void team_set_rx_mode(struct net_device *dev) | |
1124 | { | |
1125 | struct team *team = netdev_priv(dev); | |
1126 | struct team_port *port; | |
1127 | ||
1128 | rcu_read_lock(); | |
1129 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
1130 | dev_uc_sync(port->dev, dev); | |
1131 | dev_mc_sync(port->dev, dev); | |
1132 | } | |
1133 | rcu_read_unlock(); | |
1134 | } | |
1135 | ||
1136 | static int team_set_mac_address(struct net_device *dev, void *p) | |
1137 | { | |
1138 | struct team *team = netdev_priv(dev); | |
1139 | struct team_port *port; | |
1140 | struct sockaddr *addr = p; | |
1141 | ||
7ce5d222 | 1142 | dev->addr_assign_type &= ~NET_ADDR_RANDOM; |
3d249d4c JP |
1143 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
1144 | rcu_read_lock(); | |
1145 | list_for_each_entry_rcu(port, &team->port_list, list) | |
1146 | if (team->ops.port_change_mac) | |
1147 | team->ops.port_change_mac(team, port); | |
1148 | rcu_read_unlock(); | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static int team_change_mtu(struct net_device *dev, int new_mtu) | |
1153 | { | |
1154 | struct team *team = netdev_priv(dev); | |
1155 | struct team_port *port; | |
1156 | int err; | |
1157 | ||
1158 | /* | |
1159 | * Alhough this is reader, it's guarded by team lock. It's not possible | |
1160 | * to traverse list in reverse under rcu_read_lock | |
1161 | */ | |
61dc3461 | 1162 | mutex_lock(&team->lock); |
3d249d4c JP |
1163 | list_for_each_entry(port, &team->port_list, list) { |
1164 | err = dev_set_mtu(port->dev, new_mtu); | |
1165 | if (err) { | |
1166 | netdev_err(dev, "Device %s failed to change mtu", | |
1167 | port->dev->name); | |
1168 | goto unwind; | |
1169 | } | |
1170 | } | |
61dc3461 | 1171 | mutex_unlock(&team->lock); |
3d249d4c JP |
1172 | |
1173 | dev->mtu = new_mtu; | |
1174 | ||
1175 | return 0; | |
1176 | ||
1177 | unwind: | |
1178 | list_for_each_entry_continue_reverse(port, &team->port_list, list) | |
1179 | dev_set_mtu(port->dev, dev->mtu); | |
61dc3461 | 1180 | mutex_unlock(&team->lock); |
3d249d4c JP |
1181 | |
1182 | return err; | |
1183 | } | |
1184 | ||
1185 | static struct rtnl_link_stats64 * | |
1186 | team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) | |
1187 | { | |
1188 | struct team *team = netdev_priv(dev); | |
1189 | struct team_pcpu_stats *p; | |
1190 | u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; | |
1191 | u32 rx_dropped = 0, tx_dropped = 0; | |
1192 | unsigned int start; | |
1193 | int i; | |
1194 | ||
1195 | for_each_possible_cpu(i) { | |
1196 | p = per_cpu_ptr(team->pcpu_stats, i); | |
1197 | do { | |
1198 | start = u64_stats_fetch_begin_bh(&p->syncp); | |
1199 | rx_packets = p->rx_packets; | |
1200 | rx_bytes = p->rx_bytes; | |
1201 | rx_multicast = p->rx_multicast; | |
1202 | tx_packets = p->tx_packets; | |
1203 | tx_bytes = p->tx_bytes; | |
1204 | } while (u64_stats_fetch_retry_bh(&p->syncp, start)); | |
1205 | ||
1206 | stats->rx_packets += rx_packets; | |
1207 | stats->rx_bytes += rx_bytes; | |
1208 | stats->multicast += rx_multicast; | |
1209 | stats->tx_packets += tx_packets; | |
1210 | stats->tx_bytes += tx_bytes; | |
1211 | /* | |
1212 | * rx_dropped & tx_dropped are u32, updated | |
1213 | * without syncp protection. | |
1214 | */ | |
1215 | rx_dropped += p->rx_dropped; | |
1216 | tx_dropped += p->tx_dropped; | |
1217 | } | |
1218 | stats->rx_dropped = rx_dropped; | |
1219 | stats->tx_dropped = tx_dropped; | |
1220 | return stats; | |
1221 | } | |
1222 | ||
8e586137 | 1223 | static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid) |
3d249d4c JP |
1224 | { |
1225 | struct team *team = netdev_priv(dev); | |
1226 | struct team_port *port; | |
87002b03 | 1227 | int err; |
3d249d4c | 1228 | |
87002b03 JP |
1229 | /* |
1230 | * Alhough this is reader, it's guarded by team lock. It's not possible | |
1231 | * to traverse list in reverse under rcu_read_lock | |
1232 | */ | |
1233 | mutex_lock(&team->lock); | |
1234 | list_for_each_entry(port, &team->port_list, list) { | |
1235 | err = vlan_vid_add(port->dev, vid); | |
1236 | if (err) | |
1237 | goto unwind; | |
3d249d4c | 1238 | } |
87002b03 | 1239 | mutex_unlock(&team->lock); |
8e586137 JP |
1240 | |
1241 | return 0; | |
87002b03 JP |
1242 | |
1243 | unwind: | |
1244 | list_for_each_entry_continue_reverse(port, &team->port_list, list) | |
1245 | vlan_vid_del(port->dev, vid); | |
1246 | mutex_unlock(&team->lock); | |
1247 | ||
1248 | return err; | |
3d249d4c JP |
1249 | } |
1250 | ||
8e586137 | 1251 | static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid) |
3d249d4c JP |
1252 | { |
1253 | struct team *team = netdev_priv(dev); | |
1254 | struct team_port *port; | |
1255 | ||
1256 | rcu_read_lock(); | |
87002b03 JP |
1257 | list_for_each_entry_rcu(port, &team->port_list, list) |
1258 | vlan_vid_del(port->dev, vid); | |
3d249d4c | 1259 | rcu_read_unlock(); |
8e586137 JP |
1260 | |
1261 | return 0; | |
3d249d4c JP |
1262 | } |
1263 | ||
1264 | static int team_add_slave(struct net_device *dev, struct net_device *port_dev) | |
1265 | { | |
1266 | struct team *team = netdev_priv(dev); | |
1267 | int err; | |
1268 | ||
61dc3461 | 1269 | mutex_lock(&team->lock); |
3d249d4c | 1270 | err = team_port_add(team, port_dev); |
61dc3461 | 1271 | mutex_unlock(&team->lock); |
3d249d4c JP |
1272 | return err; |
1273 | } | |
1274 | ||
1275 | static int team_del_slave(struct net_device *dev, struct net_device *port_dev) | |
1276 | { | |
1277 | struct team *team = netdev_priv(dev); | |
1278 | int err; | |
1279 | ||
61dc3461 | 1280 | mutex_lock(&team->lock); |
3d249d4c | 1281 | err = team_port_del(team, port_dev); |
61dc3461 | 1282 | mutex_unlock(&team->lock); |
3d249d4c JP |
1283 | return err; |
1284 | } | |
1285 | ||
234a8fd4 JP |
1286 | static netdev_features_t team_fix_features(struct net_device *dev, |
1287 | netdev_features_t features) | |
1288 | { | |
1289 | struct team_port *port; | |
1290 | struct team *team = netdev_priv(dev); | |
1291 | netdev_features_t mask; | |
1292 | ||
1293 | mask = features; | |
1294 | features &= ~NETIF_F_ONE_FOR_ALL; | |
1295 | features |= NETIF_F_ALL_FOR_ALL; | |
1296 | ||
1297 | rcu_read_lock(); | |
1298 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
1299 | features = netdev_increment_features(features, | |
1300 | port->dev->features, | |
1301 | mask); | |
1302 | } | |
1303 | rcu_read_unlock(); | |
1304 | return features; | |
1305 | } | |
1306 | ||
3d249d4c JP |
1307 | static const struct net_device_ops team_netdev_ops = { |
1308 | .ndo_init = team_init, | |
1309 | .ndo_uninit = team_uninit, | |
1310 | .ndo_open = team_open, | |
1311 | .ndo_stop = team_close, | |
1312 | .ndo_start_xmit = team_xmit, | |
1313 | .ndo_change_rx_flags = team_change_rx_flags, | |
1314 | .ndo_set_rx_mode = team_set_rx_mode, | |
1315 | .ndo_set_mac_address = team_set_mac_address, | |
1316 | .ndo_change_mtu = team_change_mtu, | |
1317 | .ndo_get_stats64 = team_get_stats64, | |
1318 | .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid, | |
1319 | .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid, | |
1320 | .ndo_add_slave = team_add_slave, | |
1321 | .ndo_del_slave = team_del_slave, | |
234a8fd4 | 1322 | .ndo_fix_features = team_fix_features, |
3d249d4c JP |
1323 | }; |
1324 | ||
1325 | ||
1326 | /*********************** | |
1327 | * rt netlink interface | |
1328 | ***********************/ | |
1329 | ||
1330 | static void team_setup(struct net_device *dev) | |
1331 | { | |
1332 | ether_setup(dev); | |
1333 | ||
1334 | dev->netdev_ops = &team_netdev_ops; | |
1335 | dev->destructor = team_destructor; | |
1336 | dev->tx_queue_len = 0; | |
1337 | dev->flags |= IFF_MULTICAST; | |
1338 | dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); | |
1339 | ||
1340 | /* | |
1341 | * Indicate we support unicast address filtering. That way core won't | |
1342 | * bring us to promisc mode in case a unicast addr is added. | |
1343 | * Let this up to underlay drivers. | |
1344 | */ | |
1345 | dev->priv_flags |= IFF_UNICAST_FLT; | |
1346 | ||
1347 | dev->features |= NETIF_F_LLTX; | |
1348 | dev->features |= NETIF_F_GRO; | |
1349 | dev->hw_features = NETIF_F_HW_VLAN_TX | | |
1350 | NETIF_F_HW_VLAN_RX | | |
1351 | NETIF_F_HW_VLAN_FILTER; | |
1352 | ||
1353 | dev->features |= dev->hw_features; | |
1354 | } | |
1355 | ||
1356 | static int team_newlink(struct net *src_net, struct net_device *dev, | |
1357 | struct nlattr *tb[], struct nlattr *data[]) | |
1358 | { | |
1359 | int err; | |
1360 | ||
1361 | if (tb[IFLA_ADDRESS] == NULL) | |
7ce5d222 | 1362 | eth_hw_addr_random(dev); |
3d249d4c JP |
1363 | |
1364 | err = register_netdevice(dev); | |
1365 | if (err) | |
1366 | return err; | |
1367 | ||
1368 | return 0; | |
1369 | } | |
1370 | ||
1371 | static int team_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1372 | { | |
1373 | if (tb[IFLA_ADDRESS]) { | |
1374 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1375 | return -EINVAL; | |
1376 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1377 | return -EADDRNOTAVAIL; | |
1378 | } | |
1379 | return 0; | |
1380 | } | |
1381 | ||
1382 | static struct rtnl_link_ops team_link_ops __read_mostly = { | |
1383 | .kind = DRV_NAME, | |
1384 | .priv_size = sizeof(struct team), | |
1385 | .setup = team_setup, | |
1386 | .newlink = team_newlink, | |
1387 | .validate = team_validate, | |
1388 | }; | |
1389 | ||
1390 | ||
1391 | /*********************************** | |
1392 | * Generic netlink custom interface | |
1393 | ***********************************/ | |
1394 | ||
1395 | static struct genl_family team_nl_family = { | |
1396 | .id = GENL_ID_GENERATE, | |
1397 | .name = TEAM_GENL_NAME, | |
1398 | .version = TEAM_GENL_VERSION, | |
1399 | .maxattr = TEAM_ATTR_MAX, | |
1400 | .netnsok = true, | |
1401 | }; | |
1402 | ||
1403 | static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = { | |
1404 | [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, }, | |
1405 | [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 }, | |
1406 | [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED }, | |
1407 | [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED }, | |
1408 | }; | |
1409 | ||
1410 | static const struct nla_policy | |
1411 | team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = { | |
1412 | [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, }, | |
1413 | [TEAM_ATTR_OPTION_NAME] = { | |
1414 | .type = NLA_STRING, | |
1415 | .len = TEAM_STRING_MAX_LEN, | |
1416 | }, | |
1417 | [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG }, | |
1418 | [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 }, | |
2615598f | 1419 | [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY }, |
3d249d4c JP |
1420 | }; |
1421 | ||
1422 | static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) | |
1423 | { | |
1424 | struct sk_buff *msg; | |
1425 | void *hdr; | |
1426 | int err; | |
1427 | ||
1428 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1429 | if (!msg) | |
1430 | return -ENOMEM; | |
1431 | ||
1432 | hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, | |
1433 | &team_nl_family, 0, TEAM_CMD_NOOP); | |
1434 | if (IS_ERR(hdr)) { | |
1435 | err = PTR_ERR(hdr); | |
1436 | goto err_msg_put; | |
1437 | } | |
1438 | ||
1439 | genlmsg_end(msg, hdr); | |
1440 | ||
1441 | return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); | |
1442 | ||
1443 | err_msg_put: | |
1444 | nlmsg_free(msg); | |
1445 | ||
1446 | return err; | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Netlink cmd functions should be locked by following two functions. | |
8c0713a5 | 1451 | * Since dev gets held here, that ensures dev won't disappear in between. |
3d249d4c JP |
1452 | */ |
1453 | static struct team *team_nl_team_get(struct genl_info *info) | |
1454 | { | |
1455 | struct net *net = genl_info_net(info); | |
1456 | int ifindex; | |
1457 | struct net_device *dev; | |
1458 | struct team *team; | |
1459 | ||
1460 | if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX]) | |
1461 | return NULL; | |
1462 | ||
1463 | ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]); | |
8c0713a5 | 1464 | dev = dev_get_by_index(net, ifindex); |
3d249d4c | 1465 | if (!dev || dev->netdev_ops != &team_netdev_ops) { |
8c0713a5 JP |
1466 | if (dev) |
1467 | dev_put(dev); | |
3d249d4c JP |
1468 | return NULL; |
1469 | } | |
1470 | ||
1471 | team = netdev_priv(dev); | |
61dc3461 | 1472 | mutex_lock(&team->lock); |
3d249d4c JP |
1473 | return team; |
1474 | } | |
1475 | ||
1476 | static void team_nl_team_put(struct team *team) | |
1477 | { | |
61dc3461 | 1478 | mutex_unlock(&team->lock); |
8c0713a5 | 1479 | dev_put(team->dev); |
3d249d4c JP |
1480 | } |
1481 | ||
1482 | static int team_nl_send_generic(struct genl_info *info, struct team *team, | |
1483 | int (*fill_func)(struct sk_buff *skb, | |
1484 | struct genl_info *info, | |
1485 | int flags, struct team *team)) | |
1486 | { | |
1487 | struct sk_buff *skb; | |
1488 | int err; | |
1489 | ||
1490 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1491 | if (!skb) | |
1492 | return -ENOMEM; | |
1493 | ||
1494 | err = fill_func(skb, info, NLM_F_ACK, team); | |
1495 | if (err < 0) | |
1496 | goto err_fill; | |
1497 | ||
1498 | err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); | |
1499 | return err; | |
1500 | ||
1501 | err_fill: | |
1502 | nlmsg_free(skb); | |
1503 | return err; | |
1504 | } | |
1505 | ||
b82b9183 JP |
1506 | static int team_nl_fill_options_get(struct sk_buff *skb, |
1507 | u32 pid, u32 seq, int flags, | |
1508 | struct team *team, bool fillall) | |
3d249d4c JP |
1509 | { |
1510 | struct nlattr *option_list; | |
1511 | void *hdr; | |
80f7c668 JP |
1512 | struct team_option_inst *opt_inst; |
1513 | int err; | |
3d249d4c JP |
1514 | |
1515 | hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, | |
1516 | TEAM_CMD_OPTIONS_GET); | |
1517 | if (IS_ERR(hdr)) | |
1518 | return PTR_ERR(hdr); | |
1519 | ||
86ebb02d DM |
1520 | if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex)) |
1521 | goto nla_put_failure; | |
3d249d4c JP |
1522 | option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION); |
1523 | if (!option_list) | |
1524 | return -EMSGSIZE; | |
1525 | ||
80f7c668 | 1526 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { |
3d249d4c | 1527 | struct nlattr *option_item; |
80f7c668 JP |
1528 | struct team_option *option = opt_inst->option; |
1529 | struct team_gsetter_ctx ctx; | |
3d249d4c | 1530 | |
b82b9183 | 1531 | /* Include only changed options if fill all mode is not on */ |
80f7c668 | 1532 | if (!fillall && !opt_inst->changed) |
b82b9183 | 1533 | continue; |
3d249d4c JP |
1534 | option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION); |
1535 | if (!option_item) | |
1536 | goto nla_put_failure; | |
86ebb02d DM |
1537 | if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name)) |
1538 | goto nla_put_failure; | |
80f7c668 | 1539 | if (opt_inst->changed) { |
86ebb02d DM |
1540 | if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED)) |
1541 | goto nla_put_failure; | |
80f7c668 | 1542 | opt_inst->changed = false; |
b82b9183 | 1543 | } |
80f7c668 | 1544 | if (opt_inst->removed && |
86ebb02d DM |
1545 | nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED)) |
1546 | goto nla_put_failure; | |
80f7c668 JP |
1547 | if (opt_inst->port && |
1548 | nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX, | |
1549 | opt_inst->port->dev->ifindex)) | |
1550 | goto nla_put_failure; | |
1551 | ctx.port = opt_inst->port; | |
3d249d4c JP |
1552 | switch (option->type) { |
1553 | case TEAM_OPTION_TYPE_U32: | |
86ebb02d DM |
1554 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32)) |
1555 | goto nla_put_failure; | |
80f7c668 JP |
1556 | err = team_option_get(team, opt_inst, &ctx); |
1557 | if (err) | |
1558 | goto errout; | |
1559 | if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, | |
1560 | ctx.data.u32_val)) | |
86ebb02d | 1561 | goto nla_put_failure; |
3d249d4c JP |
1562 | break; |
1563 | case TEAM_OPTION_TYPE_STRING: | |
86ebb02d DM |
1564 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING)) |
1565 | goto nla_put_failure; | |
80f7c668 JP |
1566 | err = team_option_get(team, opt_inst, &ctx); |
1567 | if (err) | |
1568 | goto errout; | |
86ebb02d | 1569 | if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA, |
80f7c668 | 1570 | ctx.data.str_val)) |
86ebb02d | 1571 | goto nla_put_failure; |
3d249d4c | 1572 | break; |
2615598f JP |
1573 | case TEAM_OPTION_TYPE_BINARY: |
1574 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY)) | |
1575 | goto nla_put_failure; | |
80f7c668 JP |
1576 | err = team_option_get(team, opt_inst, &ctx); |
1577 | if (err) | |
1578 | goto errout; | |
2615598f | 1579 | if (nla_put(skb, TEAM_ATTR_OPTION_DATA, |
80f7c668 | 1580 | ctx.data.bin_val.len, ctx.data.bin_val.ptr)) |
2615598f JP |
1581 | goto nla_put_failure; |
1582 | break; | |
14f066ba JP |
1583 | case TEAM_OPTION_TYPE_BOOL: |
1584 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG)) | |
1585 | goto nla_put_failure; | |
1586 | err = team_option_get(team, opt_inst, &ctx); | |
1587 | if (err) | |
1588 | goto errout; | |
1589 | if (ctx.data.bool_val && | |
1590 | nla_put_flag(skb, TEAM_ATTR_OPTION_DATA)) | |
1591 | goto nla_put_failure; | |
1592 | break; | |
3d249d4c JP |
1593 | default: |
1594 | BUG(); | |
1595 | } | |
1596 | nla_nest_end(skb, option_item); | |
1597 | } | |
1598 | ||
1599 | nla_nest_end(skb, option_list); | |
1600 | return genlmsg_end(skb, hdr); | |
1601 | ||
1602 | nla_put_failure: | |
80f7c668 JP |
1603 | err = -EMSGSIZE; |
1604 | errout: | |
3d249d4c | 1605 | genlmsg_cancel(skb, hdr); |
80f7c668 | 1606 | return err; |
3d249d4c JP |
1607 | } |
1608 | ||
b82b9183 JP |
1609 | static int team_nl_fill_options_get_all(struct sk_buff *skb, |
1610 | struct genl_info *info, int flags, | |
1611 | struct team *team) | |
3d249d4c | 1612 | { |
b82b9183 JP |
1613 | return team_nl_fill_options_get(skb, info->snd_pid, |
1614 | info->snd_seq, NLM_F_ACK, | |
1615 | team, true); | |
3d249d4c JP |
1616 | } |
1617 | ||
1618 | static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info) | |
1619 | { | |
1620 | struct team *team; | |
1621 | int err; | |
1622 | ||
1623 | team = team_nl_team_get(info); | |
1624 | if (!team) | |
1625 | return -EINVAL; | |
1626 | ||
b82b9183 | 1627 | err = team_nl_send_generic(info, team, team_nl_fill_options_get_all); |
3d249d4c JP |
1628 | |
1629 | team_nl_team_put(team); | |
1630 | ||
1631 | return err; | |
1632 | } | |
1633 | ||
1634 | static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info) | |
1635 | { | |
1636 | struct team *team; | |
1637 | int err = 0; | |
1638 | int i; | |
1639 | struct nlattr *nl_option; | |
1640 | ||
1641 | team = team_nl_team_get(info); | |
1642 | if (!team) | |
1643 | return -EINVAL; | |
1644 | ||
1645 | err = -EINVAL; | |
1646 | if (!info->attrs[TEAM_ATTR_LIST_OPTION]) { | |
1647 | err = -EINVAL; | |
1648 | goto team_put; | |
1649 | } | |
1650 | ||
1651 | nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) { | |
80f7c668 JP |
1652 | struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1]; |
1653 | struct nlattr *attr_port_ifindex; | |
14f066ba | 1654 | struct nlattr *attr_data; |
3d249d4c | 1655 | enum team_option_type opt_type; |
80f7c668 JP |
1656 | int opt_port_ifindex = 0; /* != 0 for per-port options */ |
1657 | struct team_option_inst *opt_inst; | |
3d249d4c JP |
1658 | char *opt_name; |
1659 | bool opt_found = false; | |
1660 | ||
1661 | if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) { | |
1662 | err = -EINVAL; | |
1663 | goto team_put; | |
1664 | } | |
80f7c668 | 1665 | err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX, |
3d249d4c JP |
1666 | nl_option, team_nl_option_policy); |
1667 | if (err) | |
1668 | goto team_put; | |
80f7c668 | 1669 | if (!opt_attrs[TEAM_ATTR_OPTION_NAME] || |
14f066ba | 1670 | !opt_attrs[TEAM_ATTR_OPTION_TYPE]) { |
3d249d4c JP |
1671 | err = -EINVAL; |
1672 | goto team_put; | |
1673 | } | |
80f7c668 | 1674 | switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) { |
3d249d4c JP |
1675 | case NLA_U32: |
1676 | opt_type = TEAM_OPTION_TYPE_U32; | |
1677 | break; | |
1678 | case NLA_STRING: | |
1679 | opt_type = TEAM_OPTION_TYPE_STRING; | |
1680 | break; | |
2615598f JP |
1681 | case NLA_BINARY: |
1682 | opt_type = TEAM_OPTION_TYPE_BINARY; | |
1683 | break; | |
14f066ba JP |
1684 | case NLA_FLAG: |
1685 | opt_type = TEAM_OPTION_TYPE_BOOL; | |
1686 | break; | |
3d249d4c JP |
1687 | default: |
1688 | goto team_put; | |
1689 | } | |
1690 | ||
14f066ba JP |
1691 | attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA]; |
1692 | if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) { | |
1693 | err = -EINVAL; | |
1694 | goto team_put; | |
1695 | } | |
1696 | ||
80f7c668 JP |
1697 | opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]); |
1698 | attr_port_ifindex = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX]; | |
1699 | if (attr_port_ifindex) | |
1700 | opt_port_ifindex = nla_get_u32(attr_port_ifindex); | |
1701 | ||
1702 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { | |
1703 | struct team_option *option = opt_inst->option; | |
80f7c668 | 1704 | struct team_gsetter_ctx ctx; |
80f7c668 | 1705 | int tmp_ifindex; |
3d249d4c | 1706 | |
80f7c668 JP |
1707 | tmp_ifindex = opt_inst->port ? |
1708 | opt_inst->port->dev->ifindex : 0; | |
3d249d4c | 1709 | if (option->type != opt_type || |
80f7c668 JP |
1710 | strcmp(option->name, opt_name) || |
1711 | tmp_ifindex != opt_port_ifindex) | |
3d249d4c JP |
1712 | continue; |
1713 | opt_found = true; | |
80f7c668 | 1714 | ctx.port = opt_inst->port; |
3d249d4c JP |
1715 | switch (opt_type) { |
1716 | case TEAM_OPTION_TYPE_U32: | |
14f066ba | 1717 | ctx.data.u32_val = nla_get_u32(attr_data); |
3d249d4c JP |
1718 | break; |
1719 | case TEAM_OPTION_TYPE_STRING: | |
14f066ba | 1720 | if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) { |
2615598f JP |
1721 | err = -EINVAL; |
1722 | goto team_put; | |
1723 | } | |
14f066ba | 1724 | ctx.data.str_val = nla_data(attr_data); |
3d249d4c | 1725 | break; |
2615598f | 1726 | case TEAM_OPTION_TYPE_BINARY: |
14f066ba JP |
1727 | ctx.data.bin_val.len = nla_len(attr_data); |
1728 | ctx.data.bin_val.ptr = nla_data(attr_data); | |
1729 | break; | |
1730 | case TEAM_OPTION_TYPE_BOOL: | |
1731 | ctx.data.bool_val = attr_data ? true : false; | |
2615598f | 1732 | break; |
3d249d4c JP |
1733 | default: |
1734 | BUG(); | |
1735 | } | |
80f7c668 | 1736 | err = team_option_set(team, opt_inst, &ctx); |
3d249d4c JP |
1737 | if (err) |
1738 | goto team_put; | |
1739 | } | |
1740 | if (!opt_found) { | |
1741 | err = -ENOENT; | |
1742 | goto team_put; | |
1743 | } | |
1744 | } | |
1745 | ||
1746 | team_put: | |
1747 | team_nl_team_put(team); | |
1748 | ||
1749 | return err; | |
1750 | } | |
1751 | ||
b82b9183 JP |
1752 | static int team_nl_fill_port_list_get(struct sk_buff *skb, |
1753 | u32 pid, u32 seq, int flags, | |
1754 | struct team *team, | |
1755 | bool fillall) | |
3d249d4c JP |
1756 | { |
1757 | struct nlattr *port_list; | |
1758 | void *hdr; | |
1759 | struct team_port *port; | |
1760 | ||
1761 | hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, | |
1762 | TEAM_CMD_PORT_LIST_GET); | |
1763 | if (IS_ERR(hdr)) | |
1764 | return PTR_ERR(hdr); | |
1765 | ||
86ebb02d DM |
1766 | if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex)) |
1767 | goto nla_put_failure; | |
3d249d4c JP |
1768 | port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT); |
1769 | if (!port_list) | |
1770 | return -EMSGSIZE; | |
1771 | ||
1772 | list_for_each_entry(port, &team->port_list, list) { | |
1773 | struct nlattr *port_item; | |
1774 | ||
b82b9183 JP |
1775 | /* Include only changed ports if fill all mode is not on */ |
1776 | if (!fillall && !port->changed) | |
1777 | continue; | |
3d249d4c JP |
1778 | port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT); |
1779 | if (!port_item) | |
1780 | goto nla_put_failure; | |
86ebb02d DM |
1781 | if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex)) |
1782 | goto nla_put_failure; | |
b82b9183 | 1783 | if (port->changed) { |
86ebb02d DM |
1784 | if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED)) |
1785 | goto nla_put_failure; | |
b82b9183 JP |
1786 | port->changed = false; |
1787 | } | |
86ebb02d DM |
1788 | if ((port->removed && |
1789 | nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) || | |
71472ec1 | 1790 | (port->state.linkup && |
86ebb02d | 1791 | nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) || |
71472ec1 JP |
1792 | nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) || |
1793 | nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex)) | |
86ebb02d | 1794 | goto nla_put_failure; |
3d249d4c JP |
1795 | nla_nest_end(skb, port_item); |
1796 | } | |
1797 | ||
1798 | nla_nest_end(skb, port_list); | |
1799 | return genlmsg_end(skb, hdr); | |
1800 | ||
1801 | nla_put_failure: | |
1802 | genlmsg_cancel(skb, hdr); | |
1803 | return -EMSGSIZE; | |
1804 | } | |
1805 | ||
b82b9183 JP |
1806 | static int team_nl_fill_port_list_get_all(struct sk_buff *skb, |
1807 | struct genl_info *info, int flags, | |
1808 | struct team *team) | |
3d249d4c | 1809 | { |
b82b9183 JP |
1810 | return team_nl_fill_port_list_get(skb, info->snd_pid, |
1811 | info->snd_seq, NLM_F_ACK, | |
1812 | team, true); | |
3d249d4c JP |
1813 | } |
1814 | ||
1815 | static int team_nl_cmd_port_list_get(struct sk_buff *skb, | |
1816 | struct genl_info *info) | |
1817 | { | |
1818 | struct team *team; | |
1819 | int err; | |
1820 | ||
1821 | team = team_nl_team_get(info); | |
1822 | if (!team) | |
1823 | return -EINVAL; | |
1824 | ||
b82b9183 | 1825 | err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all); |
3d249d4c JP |
1826 | |
1827 | team_nl_team_put(team); | |
1828 | ||
1829 | return err; | |
1830 | } | |
1831 | ||
1832 | static struct genl_ops team_nl_ops[] = { | |
1833 | { | |
1834 | .cmd = TEAM_CMD_NOOP, | |
1835 | .doit = team_nl_cmd_noop, | |
1836 | .policy = team_nl_policy, | |
1837 | }, | |
1838 | { | |
1839 | .cmd = TEAM_CMD_OPTIONS_SET, | |
1840 | .doit = team_nl_cmd_options_set, | |
1841 | .policy = team_nl_policy, | |
1842 | .flags = GENL_ADMIN_PERM, | |
1843 | }, | |
1844 | { | |
1845 | .cmd = TEAM_CMD_OPTIONS_GET, | |
1846 | .doit = team_nl_cmd_options_get, | |
1847 | .policy = team_nl_policy, | |
1848 | .flags = GENL_ADMIN_PERM, | |
1849 | }, | |
1850 | { | |
1851 | .cmd = TEAM_CMD_PORT_LIST_GET, | |
1852 | .doit = team_nl_cmd_port_list_get, | |
1853 | .policy = team_nl_policy, | |
1854 | .flags = GENL_ADMIN_PERM, | |
1855 | }, | |
1856 | }; | |
1857 | ||
1858 | static struct genl_multicast_group team_change_event_mcgrp = { | |
1859 | .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, | |
1860 | }; | |
1861 | ||
b82b9183 | 1862 | static int team_nl_send_event_options_get(struct team *team) |
3d249d4c JP |
1863 | { |
1864 | struct sk_buff *skb; | |
1865 | int err; | |
1866 | struct net *net = dev_net(team->dev); | |
1867 | ||
1868 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1869 | if (!skb) | |
1870 | return -ENOMEM; | |
1871 | ||
b82b9183 | 1872 | err = team_nl_fill_options_get(skb, 0, 0, 0, team, false); |
3d249d4c JP |
1873 | if (err < 0) |
1874 | goto err_fill; | |
1875 | ||
1876 | err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, | |
1877 | GFP_KERNEL); | |
1878 | return err; | |
1879 | ||
1880 | err_fill: | |
1881 | nlmsg_free(skb); | |
1882 | return err; | |
1883 | } | |
1884 | ||
b82b9183 | 1885 | static int team_nl_send_event_port_list_get(struct team *team) |
3d249d4c JP |
1886 | { |
1887 | struct sk_buff *skb; | |
1888 | int err; | |
b82b9183 | 1889 | struct net *net = dev_net(team->dev); |
3d249d4c JP |
1890 | |
1891 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1892 | if (!skb) | |
1893 | return -ENOMEM; | |
1894 | ||
b82b9183 | 1895 | err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false); |
3d249d4c JP |
1896 | if (err < 0) |
1897 | goto err_fill; | |
1898 | ||
1899 | err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, | |
1900 | GFP_KERNEL); | |
1901 | return err; | |
1902 | ||
1903 | err_fill: | |
1904 | nlmsg_free(skb); | |
1905 | return err; | |
1906 | } | |
1907 | ||
1908 | static int team_nl_init(void) | |
1909 | { | |
1910 | int err; | |
1911 | ||
1912 | err = genl_register_family_with_ops(&team_nl_family, team_nl_ops, | |
1913 | ARRAY_SIZE(team_nl_ops)); | |
1914 | if (err) | |
1915 | return err; | |
1916 | ||
1917 | err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp); | |
1918 | if (err) | |
1919 | goto err_change_event_grp_reg; | |
1920 | ||
1921 | return 0; | |
1922 | ||
1923 | err_change_event_grp_reg: | |
1924 | genl_unregister_family(&team_nl_family); | |
1925 | ||
1926 | return err; | |
1927 | } | |
1928 | ||
1929 | static void team_nl_fini(void) | |
1930 | { | |
1931 | genl_unregister_family(&team_nl_family); | |
1932 | } | |
1933 | ||
1934 | ||
1935 | /****************** | |
1936 | * Change checkers | |
1937 | ******************/ | |
1938 | ||
b82b9183 | 1939 | static void __team_options_change_check(struct team *team) |
3d249d4c JP |
1940 | { |
1941 | int err; | |
1942 | ||
b82b9183 | 1943 | err = team_nl_send_event_options_get(team); |
3d249d4c JP |
1944 | if (err) |
1945 | netdev_warn(team->dev, "Failed to send options change via netlink\n"); | |
1946 | } | |
1947 | ||
1948 | /* rtnl lock is held */ | |
1949 | static void __team_port_change_check(struct team_port *port, bool linkup) | |
1950 | { | |
1951 | int err; | |
1952 | ||
71472ec1 | 1953 | if (!port->removed && port->state.linkup == linkup) |
3d249d4c JP |
1954 | return; |
1955 | ||
b82b9183 | 1956 | port->changed = true; |
71472ec1 JP |
1957 | port->state.linkup = linkup; |
1958 | team_refresh_port_linkup(port); | |
3d249d4c JP |
1959 | if (linkup) { |
1960 | struct ethtool_cmd ecmd; | |
1961 | ||
1962 | err = __ethtool_get_settings(port->dev, &ecmd); | |
1963 | if (!err) { | |
71472ec1 JP |
1964 | port->state.speed = ethtool_cmd_speed(&ecmd); |
1965 | port->state.duplex = ecmd.duplex; | |
3d249d4c JP |
1966 | goto send_event; |
1967 | } | |
1968 | } | |
71472ec1 JP |
1969 | port->state.speed = 0; |
1970 | port->state.duplex = 0; | |
3d249d4c JP |
1971 | |
1972 | send_event: | |
b82b9183 | 1973 | err = team_nl_send_event_port_list_get(port->team); |
3d249d4c JP |
1974 | if (err) |
1975 | netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n", | |
1976 | port->dev->name); | |
1977 | ||
1978 | } | |
1979 | ||
1980 | static void team_port_change_check(struct team_port *port, bool linkup) | |
1981 | { | |
1982 | struct team *team = port->team; | |
1983 | ||
61dc3461 | 1984 | mutex_lock(&team->lock); |
3d249d4c | 1985 | __team_port_change_check(port, linkup); |
61dc3461 | 1986 | mutex_unlock(&team->lock); |
3d249d4c JP |
1987 | } |
1988 | ||
1989 | /************************************ | |
1990 | * Net device notifier event handler | |
1991 | ************************************/ | |
1992 | ||
1993 | static int team_device_event(struct notifier_block *unused, | |
1994 | unsigned long event, void *ptr) | |
1995 | { | |
1996 | struct net_device *dev = (struct net_device *) ptr; | |
1997 | struct team_port *port; | |
1998 | ||
1999 | port = team_port_get_rtnl(dev); | |
2000 | if (!port) | |
2001 | return NOTIFY_DONE; | |
2002 | ||
2003 | switch (event) { | |
2004 | case NETDEV_UP: | |
2005 | if (netif_carrier_ok(dev)) | |
2006 | team_port_change_check(port, true); | |
2007 | case NETDEV_DOWN: | |
2008 | team_port_change_check(port, false); | |
2009 | case NETDEV_CHANGE: | |
2010 | if (netif_running(port->dev)) | |
2011 | team_port_change_check(port, | |
2012 | !!netif_carrier_ok(port->dev)); | |
2013 | break; | |
2014 | case NETDEV_UNREGISTER: | |
2015 | team_del_slave(port->team->dev, dev); | |
2016 | break; | |
2017 | case NETDEV_FEAT_CHANGE: | |
2018 | team_compute_features(port->team); | |
2019 | break; | |
2020 | case NETDEV_CHANGEMTU: | |
2021 | /* Forbid to change mtu of underlaying device */ | |
2022 | return NOTIFY_BAD; | |
2023 | case NETDEV_PRE_TYPE_CHANGE: | |
2024 | /* Forbid to change type of underlaying device */ | |
2025 | return NOTIFY_BAD; | |
2026 | } | |
2027 | return NOTIFY_DONE; | |
2028 | } | |
2029 | ||
2030 | static struct notifier_block team_notifier_block __read_mostly = { | |
2031 | .notifier_call = team_device_event, | |
2032 | }; | |
2033 | ||
2034 | ||
2035 | /*********************** | |
2036 | * Module init and exit | |
2037 | ***********************/ | |
2038 | ||
2039 | static int __init team_module_init(void) | |
2040 | { | |
2041 | int err; | |
2042 | ||
2043 | register_netdevice_notifier(&team_notifier_block); | |
2044 | ||
2045 | err = rtnl_link_register(&team_link_ops); | |
2046 | if (err) | |
2047 | goto err_rtnl_reg; | |
2048 | ||
2049 | err = team_nl_init(); | |
2050 | if (err) | |
2051 | goto err_nl_init; | |
2052 | ||
2053 | return 0; | |
2054 | ||
2055 | err_nl_init: | |
2056 | rtnl_link_unregister(&team_link_ops); | |
2057 | ||
2058 | err_rtnl_reg: | |
2059 | unregister_netdevice_notifier(&team_notifier_block); | |
2060 | ||
2061 | return err; | |
2062 | } | |
2063 | ||
2064 | static void __exit team_module_exit(void) | |
2065 | { | |
2066 | team_nl_fini(); | |
2067 | rtnl_link_unregister(&team_link_ops); | |
2068 | unregister_netdevice_notifier(&team_notifier_block); | |
2069 | } | |
2070 | ||
2071 | module_init(team_module_init); | |
2072 | module_exit(team_module_exit); | |
2073 | ||
2074 | MODULE_LICENSE("GPL v2"); | |
2075 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); | |
2076 | MODULE_DESCRIPTION("Ethernet team device driver"); | |
2077 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |