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