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