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