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