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> | |
bd2d0837 | 21 | #include <linux/netpoll.h> |
87002b03 | 22 | #include <linux/if_vlan.h> |
3d249d4c JP |
23 | #include <linux/if_arp.h> |
24 | #include <linux/socket.h> | |
25 | #include <linux/etherdevice.h> | |
26 | #include <linux/rtnetlink.h> | |
27 | #include <net/rtnetlink.h> | |
28 | #include <net/genetlink.h> | |
29 | #include <net/netlink.h> | |
6c85f2bd | 30 | #include <net/sch_generic.h> |
3d249d4c JP |
31 | #include <linux/if_team.h> |
32 | ||
33 | #define DRV_NAME "team" | |
34 | ||
35 | ||
36 | /********** | |
37 | * Helpers | |
38 | **********/ | |
39 | ||
40 | #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT) | |
41 | ||
42 | static struct team_port *team_port_get_rcu(const struct net_device *dev) | |
43 | { | |
44 | struct team_port *port = rcu_dereference(dev->rx_handler_data); | |
45 | ||
46 | return team_port_exists(dev) ? port : NULL; | |
47 | } | |
48 | ||
49 | static struct team_port *team_port_get_rtnl(const struct net_device *dev) | |
50 | { | |
51 | struct team_port *port = rtnl_dereference(dev->rx_handler_data); | |
52 | ||
53 | return team_port_exists(dev) ? port : NULL; | |
54 | } | |
55 | ||
56 | /* | |
1d76efe1 | 57 | * Since the ability to change device address for open port device is tested in |
3d249d4c JP |
58 | * team_port_add, this function can be called without control of return value |
59 | */ | |
1d76efe1 JP |
60 | static int __set_port_dev_addr(struct net_device *port_dev, |
61 | const unsigned char *dev_addr) | |
3d249d4c JP |
62 | { |
63 | struct sockaddr addr; | |
64 | ||
1d76efe1 JP |
65 | memcpy(addr.sa_data, dev_addr, port_dev->addr_len); |
66 | addr.sa_family = port_dev->type; | |
3d249d4c JP |
67 | return dev_set_mac_address(port_dev, &addr); |
68 | } | |
69 | ||
1d76efe1 | 70 | static int team_port_set_orig_dev_addr(struct team_port *port) |
3d249d4c | 71 | { |
1d76efe1 | 72 | return __set_port_dev_addr(port->dev, port->orig.dev_addr); |
3d249d4c JP |
73 | } |
74 | ||
1d76efe1 | 75 | int team_port_set_team_dev_addr(struct team_port *port) |
3d249d4c | 76 | { |
1d76efe1 | 77 | return __set_port_dev_addr(port->dev, port->team->dev->dev_addr); |
3d249d4c | 78 | } |
1d76efe1 | 79 | EXPORT_SYMBOL(team_port_set_team_dev_addr); |
3d249d4c | 80 | |
71472ec1 JP |
81 | static void team_refresh_port_linkup(struct team_port *port) |
82 | { | |
83 | port->linkup = port->user.linkup_enabled ? port->user.linkup : | |
84 | port->state.linkup; | |
85 | } | |
3d249d4c | 86 | |
0f1aad2b | 87 | |
3d249d4c JP |
88 | /******************* |
89 | * Options handling | |
90 | *******************/ | |
91 | ||
80f7c668 JP |
92 | struct team_option_inst { /* One for each option instance */ |
93 | struct list_head list; | |
01048d9a | 94 | struct list_head tmp_list; |
80f7c668 | 95 | struct team_option *option; |
85d59a87 | 96 | struct team_option_inst_info info; |
80f7c668 JP |
97 | bool changed; |
98 | bool removed; | |
99 | }; | |
100 | ||
101 | static struct team_option *__team_find_option(struct team *team, | |
102 | const char *opt_name) | |
358b8382 JP |
103 | { |
104 | struct team_option *option; | |
105 | ||
106 | list_for_each_entry(option, &team->option_list, list) { | |
107 | if (strcmp(option->name, opt_name) == 0) | |
108 | return option; | |
109 | } | |
110 | return NULL; | |
111 | } | |
112 | ||
80f7c668 JP |
113 | static void __team_option_inst_del(struct team_option_inst *opt_inst) |
114 | { | |
115 | list_del(&opt_inst->list); | |
116 | kfree(opt_inst); | |
117 | } | |
118 | ||
119 | static void __team_option_inst_del_option(struct team *team, | |
120 | struct team_option *option) | |
121 | { | |
122 | struct team_option_inst *opt_inst, *tmp; | |
123 | ||
124 | list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) { | |
125 | if (opt_inst->option == option) | |
126 | __team_option_inst_del(opt_inst); | |
127 | } | |
128 | } | |
129 | ||
b1303326 JP |
130 | static int __team_option_inst_add(struct team *team, struct team_option *option, |
131 | struct team_port *port) | |
132 | { | |
133 | struct team_option_inst *opt_inst; | |
134 | unsigned int array_size; | |
135 | unsigned int i; | |
85d59a87 | 136 | int err; |
b1303326 JP |
137 | |
138 | array_size = option->array_size; | |
139 | if (!array_size) | |
140 | array_size = 1; /* No array but still need one instance */ | |
141 | ||
142 | for (i = 0; i < array_size; i++) { | |
143 | opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL); | |
144 | if (!opt_inst) | |
145 | return -ENOMEM; | |
146 | opt_inst->option = option; | |
85d59a87 JP |
147 | opt_inst->info.port = port; |
148 | opt_inst->info.array_index = i; | |
b1303326 JP |
149 | opt_inst->changed = true; |
150 | opt_inst->removed = false; | |
151 | list_add_tail(&opt_inst->list, &team->option_inst_list); | |
85d59a87 JP |
152 | if (option->init) { |
153 | err = option->init(team, &opt_inst->info); | |
154 | if (err) | |
155 | return err; | |
156 | } | |
157 | ||
b1303326 JP |
158 | } |
159 | return 0; | |
160 | } | |
161 | ||
80f7c668 JP |
162 | static int __team_option_inst_add_option(struct team *team, |
163 | struct team_option *option) | |
164 | { | |
165 | struct team_port *port; | |
166 | int err; | |
167 | ||
b1303326 | 168 | if (!option->per_port) { |
f88725ff | 169 | err = __team_option_inst_add(team, option, NULL); |
b1303326 JP |
170 | if (err) |
171 | goto inst_del_option; | |
172 | } | |
80f7c668 JP |
173 | |
174 | list_for_each_entry(port, &team->port_list, list) { | |
175 | err = __team_option_inst_add(team, option, port); | |
176 | if (err) | |
177 | goto inst_del_option; | |
178 | } | |
179 | return 0; | |
180 | ||
181 | inst_del_option: | |
182 | __team_option_inst_del_option(team, option); | |
183 | return err; | |
184 | } | |
185 | ||
186 | static void __team_option_inst_mark_removed_option(struct team *team, | |
187 | struct team_option *option) | |
188 | { | |
189 | struct team_option_inst *opt_inst; | |
190 | ||
191 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { | |
192 | if (opt_inst->option == option) { | |
193 | opt_inst->changed = true; | |
194 | opt_inst->removed = true; | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | static void __team_option_inst_del_port(struct team *team, | |
200 | struct team_port *port) | |
201 | { | |
202 | struct team_option_inst *opt_inst, *tmp; | |
203 | ||
204 | list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) { | |
205 | if (opt_inst->option->per_port && | |
85d59a87 | 206 | opt_inst->info.port == port) |
80f7c668 JP |
207 | __team_option_inst_del(opt_inst); |
208 | } | |
209 | } | |
210 | ||
211 | static int __team_option_inst_add_port(struct team *team, | |
212 | struct team_port *port) | |
213 | { | |
214 | struct team_option *option; | |
215 | int err; | |
216 | ||
217 | list_for_each_entry(option, &team->option_list, list) { | |
218 | if (!option->per_port) | |
219 | continue; | |
220 | err = __team_option_inst_add(team, option, port); | |
221 | if (err) | |
222 | goto inst_del_port; | |
223 | } | |
224 | return 0; | |
225 | ||
226 | inst_del_port: | |
227 | __team_option_inst_del_port(team, port); | |
228 | return err; | |
229 | } | |
230 | ||
231 | static void __team_option_inst_mark_removed_port(struct team *team, | |
232 | struct team_port *port) | |
233 | { | |
234 | struct team_option_inst *opt_inst; | |
235 | ||
236 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { | |
85d59a87 | 237 | if (opt_inst->info.port == port) { |
80f7c668 JP |
238 | opt_inst->changed = true; |
239 | opt_inst->removed = true; | |
240 | } | |
241 | } | |
242 | } | |
243 | ||
244 | static int __team_options_register(struct team *team, | |
245 | const struct team_option *option, | |
246 | size_t option_count) | |
3d249d4c JP |
247 | { |
248 | int i; | |
2bba19ff | 249 | struct team_option **dst_opts; |
358b8382 | 250 | int err; |
3d249d4c | 251 | |
2bba19ff JP |
252 | dst_opts = kzalloc(sizeof(struct team_option *) * option_count, |
253 | GFP_KERNEL); | |
254 | if (!dst_opts) | |
255 | return -ENOMEM; | |
358b8382 | 256 | for (i = 0; i < option_count; i++, option++) { |
358b8382 JP |
257 | if (__team_find_option(team, option->name)) { |
258 | err = -EEXIST; | |
80f7c668 | 259 | goto alloc_rollback; |
358b8382 | 260 | } |
f8a15af0 JP |
261 | dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL); |
262 | if (!dst_opts[i]) { | |
358b8382 | 263 | err = -ENOMEM; |
80f7c668 | 264 | goto alloc_rollback; |
358b8382 | 265 | } |
358b8382 JP |
266 | } |
267 | ||
b82b9183 | 268 | for (i = 0; i < option_count; i++) { |
80f7c668 JP |
269 | err = __team_option_inst_add_option(team, dst_opts[i]); |
270 | if (err) | |
271 | goto inst_rollback; | |
358b8382 | 272 | list_add_tail(&dst_opts[i]->list, &team->option_list); |
b82b9183 | 273 | } |
358b8382 | 274 | |
2bba19ff | 275 | kfree(dst_opts); |
358b8382 JP |
276 | return 0; |
277 | ||
80f7c668 JP |
278 | inst_rollback: |
279 | for (i--; i >= 0; i--) | |
280 | __team_option_inst_del_option(team, dst_opts[i]); | |
281 | ||
282 | i = option_count - 1; | |
283 | alloc_rollback: | |
284 | for (i--; i >= 0; i--) | |
358b8382 JP |
285 | kfree(dst_opts[i]); |
286 | ||
2bba19ff | 287 | kfree(dst_opts); |
358b8382 | 288 | return err; |
3d249d4c | 289 | } |
358b8382 | 290 | |
b82b9183 JP |
291 | static void __team_options_mark_removed(struct team *team, |
292 | const struct team_option *option, | |
293 | size_t option_count) | |
294 | { | |
295 | int i; | |
296 | ||
297 | for (i = 0; i < option_count; i++, option++) { | |
298 | struct team_option *del_opt; | |
3d249d4c | 299 | |
b82b9183 | 300 | del_opt = __team_find_option(team, option->name); |
80f7c668 JP |
301 | if (del_opt) |
302 | __team_option_inst_mark_removed_option(team, del_opt); | |
b82b9183 JP |
303 | } |
304 | } | |
3d249d4c JP |
305 | |
306 | static void __team_options_unregister(struct team *team, | |
358b8382 | 307 | const struct team_option *option, |
3d249d4c JP |
308 | size_t option_count) |
309 | { | |
310 | int i; | |
311 | ||
358b8382 JP |
312 | for (i = 0; i < option_count; i++, option++) { |
313 | struct team_option *del_opt; | |
314 | ||
315 | del_opt = __team_find_option(team, option->name); | |
316 | if (del_opt) { | |
80f7c668 | 317 | __team_option_inst_del_option(team, del_opt); |
358b8382 JP |
318 | list_del(&del_opt->list); |
319 | kfree(del_opt); | |
320 | } | |
321 | } | |
3d249d4c JP |
322 | } |
323 | ||
b82b9183 JP |
324 | static void __team_options_change_check(struct team *team); |
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 JP |
350 | static int team_option_get(struct team *team, |
351 | struct team_option_inst *opt_inst, | |
352 | struct team_gsetter_ctx *ctx) | |
353 | { | |
f82b959d JP |
354 | if (!opt_inst->option->getter) |
355 | return -EOPNOTSUPP; | |
80f7c668 JP |
356 | return opt_inst->option->getter(team, ctx); |
357 | } | |
358 | ||
359 | static int team_option_set(struct team *team, | |
360 | struct team_option_inst *opt_inst, | |
361 | struct team_gsetter_ctx *ctx) | |
3d249d4c | 362 | { |
f82b959d JP |
363 | if (!opt_inst->option->setter) |
364 | return -EOPNOTSUPP; | |
2fcdb2c9 | 365 | return opt_inst->option->setter(team, ctx); |
3d249d4c JP |
366 | } |
367 | ||
0f1aad2b JP |
368 | void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info) |
369 | { | |
370 | struct team_option_inst *opt_inst; | |
371 | ||
372 | opt_inst = container_of(opt_inst_info, struct team_option_inst, info); | |
373 | opt_inst->changed = true; | |
374 | } | |
375 | EXPORT_SYMBOL(team_option_inst_set_change); | |
376 | ||
377 | void team_options_change_check(struct team *team) | |
378 | { | |
379 | __team_options_change_check(team); | |
380 | } | |
381 | EXPORT_SYMBOL(team_options_change_check); | |
382 | ||
383 | ||
3d249d4c JP |
384 | /**************** |
385 | * Mode handling | |
386 | ****************/ | |
387 | ||
388 | static LIST_HEAD(mode_list); | |
389 | static DEFINE_SPINLOCK(mode_list_lock); | |
390 | ||
0402788a JP |
391 | struct team_mode_item { |
392 | struct list_head list; | |
393 | const struct team_mode *mode; | |
394 | }; | |
395 | ||
396 | static struct team_mode_item *__find_mode(const char *kind) | |
3d249d4c | 397 | { |
0402788a | 398 | struct team_mode_item *mitem; |
3d249d4c | 399 | |
0402788a JP |
400 | list_for_each_entry(mitem, &mode_list, list) { |
401 | if (strcmp(mitem->mode->kind, kind) == 0) | |
402 | return mitem; | |
3d249d4c JP |
403 | } |
404 | return NULL; | |
405 | } | |
406 | ||
407 | static bool is_good_mode_name(const char *name) | |
408 | { | |
409 | while (*name != '\0') { | |
410 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
411 | return false; | |
412 | name++; | |
413 | } | |
414 | return true; | |
415 | } | |
416 | ||
0402788a | 417 | int team_mode_register(const struct team_mode *mode) |
3d249d4c JP |
418 | { |
419 | int err = 0; | |
0402788a | 420 | struct team_mode_item *mitem; |
3d249d4c JP |
421 | |
422 | if (!is_good_mode_name(mode->kind) || | |
423 | mode->priv_size > TEAM_MODE_PRIV_SIZE) | |
424 | return -EINVAL; | |
0402788a JP |
425 | |
426 | mitem = kmalloc(sizeof(*mitem), GFP_KERNEL); | |
427 | if (!mitem) | |
428 | return -ENOMEM; | |
429 | ||
3d249d4c JP |
430 | spin_lock(&mode_list_lock); |
431 | if (__find_mode(mode->kind)) { | |
432 | err = -EEXIST; | |
0402788a | 433 | kfree(mitem); |
3d249d4c JP |
434 | goto unlock; |
435 | } | |
0402788a JP |
436 | mitem->mode = mode; |
437 | list_add_tail(&mitem->list, &mode_list); | |
3d249d4c JP |
438 | unlock: |
439 | spin_unlock(&mode_list_lock); | |
440 | return err; | |
441 | } | |
442 | EXPORT_SYMBOL(team_mode_register); | |
443 | ||
0402788a | 444 | void team_mode_unregister(const struct team_mode *mode) |
3d249d4c | 445 | { |
0402788a JP |
446 | struct team_mode_item *mitem; |
447 | ||
3d249d4c | 448 | spin_lock(&mode_list_lock); |
0402788a JP |
449 | mitem = __find_mode(mode->kind); |
450 | if (mitem) { | |
451 | list_del_init(&mitem->list); | |
452 | kfree(mitem); | |
453 | } | |
3d249d4c | 454 | spin_unlock(&mode_list_lock); |
3d249d4c JP |
455 | } |
456 | EXPORT_SYMBOL(team_mode_unregister); | |
457 | ||
0402788a | 458 | static const struct team_mode *team_mode_get(const char *kind) |
3d249d4c | 459 | { |
0402788a JP |
460 | struct team_mode_item *mitem; |
461 | const struct team_mode *mode = NULL; | |
3d249d4c JP |
462 | |
463 | spin_lock(&mode_list_lock); | |
0402788a JP |
464 | mitem = __find_mode(kind); |
465 | if (!mitem) { | |
3d249d4c JP |
466 | spin_unlock(&mode_list_lock); |
467 | request_module("team-mode-%s", kind); | |
468 | spin_lock(&mode_list_lock); | |
0402788a | 469 | mitem = __find_mode(kind); |
3d249d4c | 470 | } |
0402788a JP |
471 | if (mitem) { |
472 | mode = mitem->mode; | |
3d249d4c JP |
473 | if (!try_module_get(mode->owner)) |
474 | mode = NULL; | |
0402788a | 475 | } |
3d249d4c JP |
476 | |
477 | spin_unlock(&mode_list_lock); | |
478 | return mode; | |
479 | } | |
480 | ||
481 | static void team_mode_put(const struct team_mode *mode) | |
482 | { | |
483 | module_put(mode->owner); | |
484 | } | |
485 | ||
486 | static bool team_dummy_transmit(struct team *team, struct sk_buff *skb) | |
487 | { | |
488 | dev_kfree_skb_any(skb); | |
489 | return false; | |
490 | } | |
491 | ||
492 | rx_handler_result_t team_dummy_receive(struct team *team, | |
493 | struct team_port *port, | |
494 | struct sk_buff *skb) | |
495 | { | |
496 | return RX_HANDLER_ANOTHER; | |
497 | } | |
498 | ||
d299cd51 JP |
499 | static const struct team_mode __team_no_mode = { |
500 | .kind = "*NOMODE*", | |
501 | }; | |
502 | ||
503 | static bool team_is_mode_set(struct team *team) | |
504 | { | |
505 | return team->mode != &__team_no_mode; | |
506 | } | |
507 | ||
508 | static void team_set_no_mode(struct team *team) | |
509 | { | |
510 | team->mode = &__team_no_mode; | |
511 | } | |
512 | ||
122bb046 | 513 | static void __team_adjust_ops(struct team *team, int en_port_count) |
3d249d4c JP |
514 | { |
515 | /* | |
516 | * To avoid checks in rx/tx skb paths, ensure here that non-null and | |
517 | * correct ops are always set. | |
518 | */ | |
519 | ||
122bb046 JP |
520 | if (!en_port_count || !team_is_mode_set(team) || |
521 | !team->mode->ops->transmit) | |
3d249d4c JP |
522 | team->ops.transmit = team_dummy_transmit; |
523 | else | |
524 | team->ops.transmit = team->mode->ops->transmit; | |
525 | ||
122bb046 JP |
526 | if (!en_port_count || !team_is_mode_set(team) || |
527 | !team->mode->ops->receive) | |
3d249d4c JP |
528 | team->ops.receive = team_dummy_receive; |
529 | else | |
530 | team->ops.receive = team->mode->ops->receive; | |
531 | } | |
532 | ||
122bb046 JP |
533 | static void team_adjust_ops(struct team *team) |
534 | { | |
535 | __team_adjust_ops(team, team->en_port_count); | |
536 | } | |
537 | ||
3d249d4c JP |
538 | /* |
539 | * We can benefit from the fact that it's ensured no port is present | |
540 | * at the time of mode change. Therefore no packets are in fly so there's no | |
541 | * need to set mode operations in any special way. | |
542 | */ | |
543 | static int __team_change_mode(struct team *team, | |
544 | const struct team_mode *new_mode) | |
545 | { | |
546 | /* Check if mode was previously set and do cleanup if so */ | |
d299cd51 | 547 | if (team_is_mode_set(team)) { |
3d249d4c JP |
548 | void (*exit_op)(struct team *team) = team->ops.exit; |
549 | ||
550 | /* Clear ops area so no callback is called any longer */ | |
551 | memset(&team->ops, 0, sizeof(struct team_mode_ops)); | |
552 | team_adjust_ops(team); | |
553 | ||
554 | if (exit_op) | |
555 | exit_op(team); | |
556 | team_mode_put(team->mode); | |
d299cd51 | 557 | team_set_no_mode(team); |
3d249d4c JP |
558 | /* zero private data area */ |
559 | memset(&team->mode_priv, 0, | |
560 | sizeof(struct team) - offsetof(struct team, mode_priv)); | |
561 | } | |
562 | ||
563 | if (!new_mode) | |
564 | return 0; | |
565 | ||
566 | if (new_mode->ops->init) { | |
567 | int err; | |
568 | ||
569 | err = new_mode->ops->init(team); | |
570 | if (err) | |
571 | return err; | |
572 | } | |
573 | ||
574 | team->mode = new_mode; | |
575 | memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops)); | |
576 | team_adjust_ops(team); | |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
581 | static int team_change_mode(struct team *team, const char *kind) | |
582 | { | |
0402788a | 583 | const struct team_mode *new_mode; |
3d249d4c JP |
584 | struct net_device *dev = team->dev; |
585 | int err; | |
586 | ||
587 | if (!list_empty(&team->port_list)) { | |
588 | netdev_err(dev, "No ports can be present during mode change\n"); | |
589 | return -EBUSY; | |
590 | } | |
591 | ||
d299cd51 | 592 | if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) { |
3d249d4c JP |
593 | netdev_err(dev, "Unable to change to the same mode the team is in\n"); |
594 | return -EINVAL; | |
595 | } | |
596 | ||
597 | new_mode = team_mode_get(kind); | |
598 | if (!new_mode) { | |
599 | netdev_err(dev, "Mode \"%s\" not found\n", kind); | |
600 | return -EINVAL; | |
601 | } | |
602 | ||
603 | err = __team_change_mode(team, new_mode); | |
604 | if (err) { | |
605 | netdev_err(dev, "Failed to change to mode \"%s\"\n", kind); | |
606 | team_mode_put(new_mode); | |
607 | return err; | |
608 | } | |
609 | ||
610 | netdev_info(dev, "Mode changed to \"%s\"\n", kind); | |
611 | return 0; | |
612 | } | |
613 | ||
614 | ||
615 | /************************ | |
616 | * Rx path frame handler | |
617 | ************************/ | |
618 | ||
619 | /* note: already called with rcu_read_lock */ | |
620 | static rx_handler_result_t team_handle_frame(struct sk_buff **pskb) | |
621 | { | |
622 | struct sk_buff *skb = *pskb; | |
623 | struct team_port *port; | |
624 | struct team *team; | |
625 | rx_handler_result_t res; | |
626 | ||
627 | skb = skb_share_check(skb, GFP_ATOMIC); | |
628 | if (!skb) | |
629 | return RX_HANDLER_CONSUMED; | |
630 | ||
631 | *pskb = skb; | |
632 | ||
633 | port = team_port_get_rcu(skb->dev); | |
634 | team = port->team; | |
19a0b58e JP |
635 | if (!team_port_enabled(port)) { |
636 | /* allow exact match delivery for disabled ports */ | |
637 | res = RX_HANDLER_EXACT; | |
638 | } else { | |
639 | res = team->ops.receive(team, port, skb); | |
640 | } | |
3d249d4c JP |
641 | if (res == RX_HANDLER_ANOTHER) { |
642 | struct team_pcpu_stats *pcpu_stats; | |
643 | ||
644 | pcpu_stats = this_cpu_ptr(team->pcpu_stats); | |
645 | u64_stats_update_begin(&pcpu_stats->syncp); | |
646 | pcpu_stats->rx_packets++; | |
647 | pcpu_stats->rx_bytes += skb->len; | |
648 | if (skb->pkt_type == PACKET_MULTICAST) | |
649 | pcpu_stats->rx_multicast++; | |
650 | u64_stats_update_end(&pcpu_stats->syncp); | |
651 | ||
652 | skb->dev = team->dev; | |
653 | } else { | |
654 | this_cpu_inc(team->pcpu_stats->rx_dropped); | |
655 | } | |
656 | ||
657 | return res; | |
658 | } | |
659 | ||
660 | ||
8ff5105a JP |
661 | /************************************* |
662 | * Multiqueue Tx port select override | |
663 | *************************************/ | |
664 | ||
665 | static int team_queue_override_init(struct team *team) | |
666 | { | |
667 | struct list_head *listarr; | |
668 | unsigned int queue_cnt = team->dev->num_tx_queues - 1; | |
669 | unsigned int i; | |
670 | ||
671 | if (!queue_cnt) | |
672 | return 0; | |
673 | listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL); | |
674 | if (!listarr) | |
675 | return -ENOMEM; | |
676 | team->qom_lists = listarr; | |
677 | for (i = 0; i < queue_cnt; i++) | |
678 | INIT_LIST_HEAD(listarr++); | |
679 | return 0; | |
680 | } | |
681 | ||
682 | static void team_queue_override_fini(struct team *team) | |
683 | { | |
684 | kfree(team->qom_lists); | |
685 | } | |
686 | ||
687 | static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id) | |
688 | { | |
689 | return &team->qom_lists[queue_id - 1]; | |
690 | } | |
691 | ||
692 | /* | |
693 | * note: already called with rcu_read_lock | |
694 | */ | |
695 | static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb) | |
696 | { | |
697 | struct list_head *qom_list; | |
698 | struct team_port *port; | |
699 | ||
700 | if (!team->queue_override_enabled || !skb->queue_mapping) | |
701 | return false; | |
702 | qom_list = __team_get_qom_list(team, skb->queue_mapping); | |
703 | list_for_each_entry_rcu(port, qom_list, qom_list) { | |
704 | if (!team_dev_queue_xmit(team, port, skb)) | |
705 | return true; | |
706 | } | |
707 | return false; | |
708 | } | |
709 | ||
710 | static void __team_queue_override_port_del(struct team *team, | |
711 | struct team_port *port) | |
712 | { | |
713 | list_del_rcu(&port->qom_list); | |
714 | synchronize_rcu(); | |
715 | INIT_LIST_HEAD(&port->qom_list); | |
716 | } | |
717 | ||
718 | static bool team_queue_override_port_has_gt_prio_than(struct team_port *port, | |
719 | struct team_port *cur) | |
720 | { | |
721 | if (port->priority < cur->priority) | |
722 | return true; | |
723 | if (port->priority > cur->priority) | |
724 | return false; | |
725 | if (port->index < cur->index) | |
726 | return true; | |
727 | return false; | |
728 | } | |
729 | ||
730 | static void __team_queue_override_port_add(struct team *team, | |
731 | struct team_port *port) | |
732 | { | |
733 | struct team_port *cur; | |
734 | struct list_head *qom_list; | |
735 | struct list_head *node; | |
736 | ||
737 | if (!port->queue_id || !team_port_enabled(port)) | |
738 | return; | |
739 | ||
740 | qom_list = __team_get_qom_list(team, port->queue_id); | |
741 | node = qom_list; | |
742 | list_for_each_entry(cur, qom_list, qom_list) { | |
743 | if (team_queue_override_port_has_gt_prio_than(port, cur)) | |
744 | break; | |
745 | node = &cur->qom_list; | |
746 | } | |
747 | list_add_tail_rcu(&port->qom_list, node); | |
748 | } | |
749 | ||
750 | static void __team_queue_override_enabled_check(struct team *team) | |
751 | { | |
752 | struct team_port *port; | |
753 | bool enabled = false; | |
754 | ||
755 | list_for_each_entry(port, &team->port_list, list) { | |
756 | if (!list_empty(&port->qom_list)) { | |
757 | enabled = true; | |
758 | break; | |
759 | } | |
760 | } | |
761 | if (enabled == team->queue_override_enabled) | |
762 | return; | |
763 | netdev_dbg(team->dev, "%s queue override\n", | |
764 | enabled ? "Enabling" : "Disabling"); | |
765 | team->queue_override_enabled = enabled; | |
766 | } | |
767 | ||
768 | static void team_queue_override_port_refresh(struct team *team, | |
769 | struct team_port *port) | |
770 | { | |
771 | __team_queue_override_port_del(team, port); | |
772 | __team_queue_override_port_add(team, port); | |
773 | __team_queue_override_enabled_check(team); | |
774 | } | |
775 | ||
776 | ||
3d249d4c JP |
777 | /**************** |
778 | * Port handling | |
779 | ****************/ | |
780 | ||
781 | static bool team_port_find(const struct team *team, | |
782 | const struct team_port *port) | |
783 | { | |
784 | struct team_port *cur; | |
785 | ||
786 | list_for_each_entry(cur, &team->port_list, list) | |
787 | if (cur == port) | |
788 | return true; | |
789 | return false; | |
790 | } | |
791 | ||
792 | /* | |
19a0b58e JP |
793 | * Enable/disable port by adding to enabled port hashlist and setting |
794 | * port->index (Might be racy so reader could see incorrect ifindex when | |
795 | * processing a flying packet, but that is not a problem). Write guarded | |
796 | * by team->lock. | |
3d249d4c | 797 | */ |
19a0b58e JP |
798 | static void team_port_enable(struct team *team, |
799 | struct team_port *port) | |
3d249d4c | 800 | { |
19a0b58e JP |
801 | if (team_port_enabled(port)) |
802 | return; | |
803 | port->index = team->en_port_count++; | |
3d249d4c JP |
804 | hlist_add_head_rcu(&port->hlist, |
805 | team_port_index_hash(team, port->index)); | |
122bb046 | 806 | team_adjust_ops(team); |
8ff5105a | 807 | team_queue_override_port_refresh(team, port); |
4bccfd17 JP |
808 | if (team->ops.port_enabled) |
809 | team->ops.port_enabled(team, port); | |
3d249d4c JP |
810 | } |
811 | ||
812 | static void __reconstruct_port_hlist(struct team *team, int rm_index) | |
813 | { | |
814 | int i; | |
815 | struct team_port *port; | |
816 | ||
19a0b58e | 817 | for (i = rm_index + 1; i < team->en_port_count; i++) { |
3d249d4c JP |
818 | port = team_get_port_by_index(team, i); |
819 | hlist_del_rcu(&port->hlist); | |
820 | port->index--; | |
821 | hlist_add_head_rcu(&port->hlist, | |
822 | team_port_index_hash(team, port->index)); | |
823 | } | |
824 | } | |
825 | ||
19a0b58e JP |
826 | static void team_port_disable(struct team *team, |
827 | struct team_port *port) | |
3d249d4c | 828 | { |
19a0b58e JP |
829 | if (!team_port_enabled(port)) |
830 | return; | |
4bccfd17 JP |
831 | if (team->ops.port_disabled) |
832 | team->ops.port_disabled(team, port); | |
3d249d4c | 833 | hlist_del_rcu(&port->hlist); |
122bb046 | 834 | __reconstruct_port_hlist(team, port->index); |
19a0b58e | 835 | port->index = -1; |
8ff5105a | 836 | team_queue_override_port_refresh(team, port); |
122bb046 JP |
837 | __team_adjust_ops(team, team->en_port_count - 1); |
838 | /* | |
839 | * Wait until readers see adjusted ops. This ensures that | |
840 | * readers never see team->en_port_count == 0 | |
841 | */ | |
842 | synchronize_rcu(); | |
843 | team->en_port_count--; | |
3d249d4c JP |
844 | } |
845 | ||
846 | #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \ | |
847 | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \ | |
848 | NETIF_F_HIGHDMA | NETIF_F_LRO) | |
849 | ||
850 | static void __team_compute_features(struct team *team) | |
851 | { | |
852 | struct team_port *port; | |
853 | u32 vlan_features = TEAM_VLAN_FEATURES; | |
854 | unsigned short max_hard_header_len = ETH_HLEN; | |
dc905951 | 855 | unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE; |
3d249d4c JP |
856 | |
857 | list_for_each_entry(port, &team->port_list, list) { | |
858 | vlan_features = netdev_increment_features(vlan_features, | |
859 | port->dev->vlan_features, | |
860 | TEAM_VLAN_FEATURES); | |
861 | ||
dc905951 | 862 | dst_release_flag &= port->dev->priv_flags; |
3d249d4c JP |
863 | if (port->dev->hard_header_len > max_hard_header_len) |
864 | max_hard_header_len = port->dev->hard_header_len; | |
865 | } | |
866 | ||
867 | team->dev->vlan_features = vlan_features; | |
868 | team->dev->hard_header_len = max_hard_header_len; | |
869 | ||
dc905951 JP |
870 | flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE; |
871 | team->dev->priv_flags = flags | dst_release_flag; | |
872 | ||
3d249d4c JP |
873 | netdev_change_features(team->dev); |
874 | } | |
875 | ||
876 | static void team_compute_features(struct team *team) | |
877 | { | |
61dc3461 | 878 | mutex_lock(&team->lock); |
3d249d4c | 879 | __team_compute_features(team); |
61dc3461 | 880 | mutex_unlock(&team->lock); |
3d249d4c JP |
881 | } |
882 | ||
883 | static int team_port_enter(struct team *team, struct team_port *port) | |
884 | { | |
885 | int err = 0; | |
886 | ||
887 | dev_hold(team->dev); | |
888 | port->dev->priv_flags |= IFF_TEAM_PORT; | |
889 | if (team->ops.port_enter) { | |
890 | err = team->ops.port_enter(team, port); | |
891 | if (err) { | |
892 | netdev_err(team->dev, "Device %s failed to enter team mode\n", | |
893 | port->dev->name); | |
894 | goto err_port_enter; | |
895 | } | |
896 | } | |
897 | ||
898 | return 0; | |
899 | ||
900 | err_port_enter: | |
901 | port->dev->priv_flags &= ~IFF_TEAM_PORT; | |
902 | dev_put(team->dev); | |
903 | ||
904 | return err; | |
905 | } | |
906 | ||
907 | static void team_port_leave(struct team *team, struct team_port *port) | |
908 | { | |
909 | if (team->ops.port_leave) | |
910 | team->ops.port_leave(team, port); | |
911 | port->dev->priv_flags &= ~IFF_TEAM_PORT; | |
912 | dev_put(team->dev); | |
913 | } | |
914 | ||
bd2d0837 | 915 | #ifdef CONFIG_NET_POLL_CONTROLLER |
47be03a2 AW |
916 | static int team_port_enable_netpoll(struct team *team, struct team_port *port, |
917 | gfp_t gfp) | |
bd2d0837 JP |
918 | { |
919 | struct netpoll *np; | |
920 | int err; | |
921 | ||
47be03a2 | 922 | np = kzalloc(sizeof(*np), gfp); |
bd2d0837 JP |
923 | if (!np) |
924 | return -ENOMEM; | |
925 | ||
47be03a2 | 926 | err = __netpoll_setup(np, port->dev, gfp); |
bd2d0837 JP |
927 | if (err) { |
928 | kfree(np); | |
929 | return err; | |
930 | } | |
931 | port->np = np; | |
932 | return err; | |
933 | } | |
934 | ||
935 | static void team_port_disable_netpoll(struct team_port *port) | |
936 | { | |
937 | struct netpoll *np = port->np; | |
938 | ||
939 | if (!np) | |
940 | return; | |
941 | port->np = NULL; | |
942 | ||
943 | /* Wait for transmitting packets to finish before freeing. */ | |
944 | synchronize_rcu_bh(); | |
945 | __netpoll_cleanup(np); | |
946 | kfree(np); | |
947 | } | |
948 | ||
949 | static struct netpoll_info *team_netpoll_info(struct team *team) | |
950 | { | |
951 | return team->dev->npinfo; | |
952 | } | |
953 | ||
954 | #else | |
47be03a2 AW |
955 | static int team_port_enable_netpoll(struct team *team, struct team_port *port, |
956 | gfp_t gfp) | |
bd2d0837 JP |
957 | { |
958 | return 0; | |
959 | } | |
960 | static void team_port_disable_netpoll(struct team_port *port) | |
961 | { | |
962 | } | |
963 | static struct netpoll_info *team_netpoll_info(struct team *team) | |
964 | { | |
965 | return NULL; | |
966 | } | |
967 | #endif | |
968 | ||
3d249d4c | 969 | static void __team_port_change_check(struct team_port *port, bool linkup); |
1d76efe1 JP |
970 | static int team_dev_type_check_change(struct net_device *dev, |
971 | struct net_device *port_dev); | |
3d249d4c JP |
972 | |
973 | static int team_port_add(struct team *team, struct net_device *port_dev) | |
974 | { | |
975 | struct net_device *dev = team->dev; | |
976 | struct team_port *port; | |
977 | char *portname = port_dev->name; | |
978 | int err; | |
979 | ||
1d76efe1 JP |
980 | if (port_dev->flags & IFF_LOOPBACK) { |
981 | netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n", | |
3d249d4c JP |
982 | portname); |
983 | return -EINVAL; | |
984 | } | |
985 | ||
986 | if (team_port_exists(port_dev)) { | |
987 | netdev_err(dev, "Device %s is already a port " | |
988 | "of a team device\n", portname); | |
989 | return -EBUSY; | |
990 | } | |
991 | ||
2c33bb37 JP |
992 | if (port_dev->features & NETIF_F_VLAN_CHALLENGED && |
993 | vlan_uses_dev(dev)) { | |
994 | netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n", | |
995 | portname); | |
996 | return -EPERM; | |
997 | } | |
998 | ||
1d76efe1 JP |
999 | err = team_dev_type_check_change(dev, port_dev); |
1000 | if (err) | |
1001 | return err; | |
1002 | ||
3d249d4c JP |
1003 | if (port_dev->flags & IFF_UP) { |
1004 | netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n", | |
1005 | portname); | |
1006 | return -EBUSY; | |
1007 | } | |
1008 | ||
5149ee58 JP |
1009 | port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size, |
1010 | GFP_KERNEL); | |
3d249d4c JP |
1011 | if (!port) |
1012 | return -ENOMEM; | |
1013 | ||
1014 | port->dev = port_dev; | |
1015 | port->team = team; | |
8ff5105a | 1016 | INIT_LIST_HEAD(&port->qom_list); |
3d249d4c JP |
1017 | |
1018 | port->orig.mtu = port_dev->mtu; | |
1019 | err = dev_set_mtu(port_dev, dev->mtu); | |
1020 | if (err) { | |
1021 | netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err); | |
1022 | goto err_set_mtu; | |
1023 | } | |
1024 | ||
1d76efe1 | 1025 | memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len); |
3d249d4c JP |
1026 | |
1027 | err = team_port_enter(team, port); | |
1028 | if (err) { | |
1029 | netdev_err(dev, "Device %s failed to enter team mode\n", | |
1030 | portname); | |
1031 | goto err_port_enter; | |
1032 | } | |
1033 | ||
1034 | err = dev_open(port_dev); | |
1035 | if (err) { | |
1036 | netdev_dbg(dev, "Device %s opening failed\n", | |
1037 | portname); | |
1038 | goto err_dev_open; | |
1039 | } | |
1040 | ||
57459185 JP |
1041 | err = vlan_vids_add_by_dev(port_dev, dev); |
1042 | if (err) { | |
1043 | netdev_err(dev, "Failed to add vlan ids to device %s\n", | |
1044 | portname); | |
1045 | goto err_vids_add; | |
1046 | } | |
1047 | ||
bd2d0837 | 1048 | if (team_netpoll_info(team)) { |
47be03a2 | 1049 | err = team_port_enable_netpoll(team, port, GFP_KERNEL); |
bd2d0837 JP |
1050 | if (err) { |
1051 | netdev_err(dev, "Failed to enable netpoll on device %s\n", | |
1052 | portname); | |
1053 | goto err_enable_netpoll; | |
1054 | } | |
1055 | } | |
1056 | ||
3d249d4c JP |
1057 | err = netdev_set_master(port_dev, dev); |
1058 | if (err) { | |
1059 | netdev_err(dev, "Device %s failed to set master\n", portname); | |
1060 | goto err_set_master; | |
1061 | } | |
1062 | ||
1063 | err = netdev_rx_handler_register(port_dev, team_handle_frame, | |
1064 | port); | |
1065 | if (err) { | |
1066 | netdev_err(dev, "Device %s failed to register rx_handler\n", | |
1067 | portname); | |
1068 | goto err_handler_register; | |
1069 | } | |
1070 | ||
35b384bd | 1071 | err = __team_option_inst_add_port(team, port); |
80f7c668 JP |
1072 | if (err) { |
1073 | netdev_err(dev, "Device %s failed to add per-port options\n", | |
1074 | portname); | |
1075 | goto err_option_port_add; | |
1076 | } | |
1077 | ||
19a0b58e JP |
1078 | port->index = -1; |
1079 | team_port_enable(team, port); | |
1080 | list_add_tail_rcu(&port->list, &team->port_list); | |
3d249d4c JP |
1081 | __team_compute_features(team); |
1082 | __team_port_change_check(port, !!netif_carrier_ok(port_dev)); | |
35b384bd | 1083 | __team_options_change_check(team); |
3d249d4c JP |
1084 | |
1085 | netdev_info(dev, "Port device %s added\n", portname); | |
1086 | ||
1087 | return 0; | |
1088 | ||
80f7c668 JP |
1089 | err_option_port_add: |
1090 | netdev_rx_handler_unregister(port_dev); | |
1091 | ||
3d249d4c JP |
1092 | err_handler_register: |
1093 | netdev_set_master(port_dev, NULL); | |
1094 | ||
1095 | err_set_master: | |
bd2d0837 JP |
1096 | team_port_disable_netpoll(port); |
1097 | ||
1098 | err_enable_netpoll: | |
57459185 JP |
1099 | vlan_vids_del_by_dev(port_dev, dev); |
1100 | ||
1101 | err_vids_add: | |
3d249d4c JP |
1102 | dev_close(port_dev); |
1103 | ||
1104 | err_dev_open: | |
1105 | team_port_leave(team, port); | |
1d76efe1 | 1106 | team_port_set_orig_dev_addr(port); |
3d249d4c JP |
1107 | |
1108 | err_port_enter: | |
1109 | dev_set_mtu(port_dev, port->orig.mtu); | |
1110 | ||
1111 | err_set_mtu: | |
1112 | kfree(port); | |
1113 | ||
1114 | return err; | |
1115 | } | |
1116 | ||
1117 | static int team_port_del(struct team *team, struct net_device *port_dev) | |
1118 | { | |
1119 | struct net_device *dev = team->dev; | |
1120 | struct team_port *port; | |
1121 | char *portname = port_dev->name; | |
1122 | ||
1123 | port = team_port_get_rtnl(port_dev); | |
1124 | if (!port || !team_port_find(team, port)) { | |
1125 | netdev_err(dev, "Device %s does not act as a port of this team\n", | |
1126 | portname); | |
1127 | return -ENOENT; | |
1128 | } | |
1129 | ||
35b384bd JP |
1130 | __team_option_inst_mark_removed_port(team, port); |
1131 | __team_options_change_check(team); | |
1132 | __team_option_inst_del_port(team, port); | |
b82b9183 | 1133 | port->removed = true; |
3d249d4c | 1134 | __team_port_change_check(port, false); |
19a0b58e JP |
1135 | team_port_disable(team, port); |
1136 | list_del_rcu(&port->list); | |
3d249d4c JP |
1137 | netdev_rx_handler_unregister(port_dev); |
1138 | netdev_set_master(port_dev, NULL); | |
bd2d0837 | 1139 | team_port_disable_netpoll(port); |
57459185 | 1140 | vlan_vids_del_by_dev(port_dev, dev); |
3d249d4c JP |
1141 | dev_close(port_dev); |
1142 | team_port_leave(team, port); | |
1d76efe1 | 1143 | team_port_set_orig_dev_addr(port); |
3d249d4c JP |
1144 | dev_set_mtu(port_dev, port->orig.mtu); |
1145 | synchronize_rcu(); | |
1146 | kfree(port); | |
1147 | netdev_info(dev, "Port device %s removed\n", portname); | |
1148 | __team_compute_features(team); | |
1149 | ||
1150 | return 0; | |
1151 | } | |
1152 | ||
1153 | ||
1154 | /***************** | |
1155 | * Net device ops | |
1156 | *****************/ | |
1157 | ||
80f7c668 | 1158 | static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx) |
3d249d4c | 1159 | { |
d299cd51 | 1160 | ctx->data.str_val = team->mode->kind; |
3d249d4c JP |
1161 | return 0; |
1162 | } | |
1163 | ||
80f7c668 | 1164 | static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx) |
3d249d4c | 1165 | { |
80f7c668 | 1166 | return team_change_mode(team, ctx->data.str_val); |
3d249d4c JP |
1167 | } |
1168 | ||
acd69962 JP |
1169 | static int team_port_en_option_get(struct team *team, |
1170 | struct team_gsetter_ctx *ctx) | |
1171 | { | |
85d59a87 JP |
1172 | struct team_port *port = ctx->info->port; |
1173 | ||
1174 | ctx->data.bool_val = team_port_enabled(port); | |
acd69962 JP |
1175 | return 0; |
1176 | } | |
1177 | ||
1178 | static int team_port_en_option_set(struct team *team, | |
1179 | struct team_gsetter_ctx *ctx) | |
1180 | { | |
85d59a87 JP |
1181 | struct team_port *port = ctx->info->port; |
1182 | ||
acd69962 | 1183 | if (ctx->data.bool_val) |
85d59a87 | 1184 | team_port_enable(team, port); |
acd69962 | 1185 | else |
85d59a87 | 1186 | team_port_disable(team, port); |
acd69962 JP |
1187 | return 0; |
1188 | } | |
1189 | ||
71472ec1 JP |
1190 | static int team_user_linkup_option_get(struct team *team, |
1191 | struct team_gsetter_ctx *ctx) | |
1192 | { | |
85d59a87 JP |
1193 | struct team_port *port = ctx->info->port; |
1194 | ||
1195 | ctx->data.bool_val = port->user.linkup; | |
71472ec1 JP |
1196 | return 0; |
1197 | } | |
1198 | ||
1199 | static int team_user_linkup_option_set(struct team *team, | |
1200 | struct team_gsetter_ctx *ctx) | |
1201 | { | |
85d59a87 JP |
1202 | struct team_port *port = ctx->info->port; |
1203 | ||
1204 | port->user.linkup = ctx->data.bool_val; | |
1205 | team_refresh_port_linkup(port); | |
71472ec1 JP |
1206 | return 0; |
1207 | } | |
1208 | ||
1209 | static int team_user_linkup_en_option_get(struct team *team, | |
1210 | struct team_gsetter_ctx *ctx) | |
1211 | { | |
85d59a87 | 1212 | struct team_port *port = ctx->info->port; |
71472ec1 JP |
1213 | |
1214 | ctx->data.bool_val = port->user.linkup_enabled; | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | static int team_user_linkup_en_option_set(struct team *team, | |
1219 | struct team_gsetter_ctx *ctx) | |
1220 | { | |
85d59a87 | 1221 | struct team_port *port = ctx->info->port; |
71472ec1 JP |
1222 | |
1223 | port->user.linkup_enabled = ctx->data.bool_val; | |
85d59a87 | 1224 | team_refresh_port_linkup(port); |
71472ec1 JP |
1225 | return 0; |
1226 | } | |
1227 | ||
a86fc6b7 JP |
1228 | static int team_priority_option_get(struct team *team, |
1229 | struct team_gsetter_ctx *ctx) | |
1230 | { | |
1231 | struct team_port *port = ctx->info->port; | |
1232 | ||
1233 | ctx->data.s32_val = port->priority; | |
1234 | return 0; | |
1235 | } | |
1236 | ||
1237 | static int team_priority_option_set(struct team *team, | |
1238 | struct team_gsetter_ctx *ctx) | |
1239 | { | |
1240 | struct team_port *port = ctx->info->port; | |
1241 | ||
1242 | port->priority = ctx->data.s32_val; | |
8ff5105a JP |
1243 | team_queue_override_port_refresh(team, port); |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | static int team_queue_id_option_get(struct team *team, | |
1248 | struct team_gsetter_ctx *ctx) | |
1249 | { | |
1250 | struct team_port *port = ctx->info->port; | |
1251 | ||
1252 | ctx->data.u32_val = port->queue_id; | |
a86fc6b7 JP |
1253 | return 0; |
1254 | } | |
1255 | ||
8ff5105a JP |
1256 | static int team_queue_id_option_set(struct team *team, |
1257 | struct team_gsetter_ctx *ctx) | |
1258 | { | |
1259 | struct team_port *port = ctx->info->port; | |
1260 | ||
1261 | if (port->queue_id == ctx->data.u32_val) | |
1262 | return 0; | |
1263 | if (ctx->data.u32_val >= team->dev->real_num_tx_queues) | |
1264 | return -EINVAL; | |
1265 | port->queue_id = ctx->data.u32_val; | |
1266 | team_queue_override_port_refresh(team, port); | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | ||
358b8382 | 1271 | static const struct team_option team_options[] = { |
3d249d4c JP |
1272 | { |
1273 | .name = "mode", | |
1274 | .type = TEAM_OPTION_TYPE_STRING, | |
1275 | .getter = team_mode_option_get, | |
1276 | .setter = team_mode_option_set, | |
1277 | }, | |
acd69962 JP |
1278 | { |
1279 | .name = "enabled", | |
1280 | .type = TEAM_OPTION_TYPE_BOOL, | |
1281 | .per_port = true, | |
1282 | .getter = team_port_en_option_get, | |
1283 | .setter = team_port_en_option_set, | |
1284 | }, | |
71472ec1 JP |
1285 | { |
1286 | .name = "user_linkup", | |
1287 | .type = TEAM_OPTION_TYPE_BOOL, | |
1288 | .per_port = true, | |
1289 | .getter = team_user_linkup_option_get, | |
1290 | .setter = team_user_linkup_option_set, | |
1291 | }, | |
1292 | { | |
1293 | .name = "user_linkup_enabled", | |
1294 | .type = TEAM_OPTION_TYPE_BOOL, | |
1295 | .per_port = true, | |
1296 | .getter = team_user_linkup_en_option_get, | |
1297 | .setter = team_user_linkup_en_option_set, | |
1298 | }, | |
a86fc6b7 JP |
1299 | { |
1300 | .name = "priority", | |
1301 | .type = TEAM_OPTION_TYPE_S32, | |
1302 | .per_port = true, | |
1303 | .getter = team_priority_option_get, | |
1304 | .setter = team_priority_option_set, | |
1305 | }, | |
8ff5105a JP |
1306 | { |
1307 | .name = "queue_id", | |
1308 | .type = TEAM_OPTION_TYPE_U32, | |
1309 | .per_port = true, | |
1310 | .getter = team_queue_id_option_get, | |
1311 | .setter = team_queue_id_option_set, | |
1312 | }, | |
3d249d4c JP |
1313 | }; |
1314 | ||
6c85f2bd JP |
1315 | static struct lock_class_key team_netdev_xmit_lock_key; |
1316 | static struct lock_class_key team_netdev_addr_lock_key; | |
1317 | ||
1318 | static void team_set_lockdep_class_one(struct net_device *dev, | |
1319 | struct netdev_queue *txq, | |
1320 | void *unused) | |
1321 | { | |
1322 | lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key); | |
1323 | } | |
1324 | ||
1325 | static void team_set_lockdep_class(struct net_device *dev) | |
1326 | { | |
1327 | lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key); | |
1328 | netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL); | |
1329 | } | |
1330 | ||
3d249d4c JP |
1331 | static int team_init(struct net_device *dev) |
1332 | { | |
1333 | struct team *team = netdev_priv(dev); | |
1334 | int i; | |
358b8382 | 1335 | int err; |
3d249d4c JP |
1336 | |
1337 | team->dev = dev; | |
61dc3461 | 1338 | mutex_init(&team->lock); |
d299cd51 | 1339 | team_set_no_mode(team); |
3d249d4c JP |
1340 | |
1341 | team->pcpu_stats = alloc_percpu(struct team_pcpu_stats); | |
1342 | if (!team->pcpu_stats) | |
1343 | return -ENOMEM; | |
1344 | ||
1345 | for (i = 0; i < TEAM_PORT_HASHENTRIES; i++) | |
19a0b58e | 1346 | INIT_HLIST_HEAD(&team->en_port_hlist[i]); |
3d249d4c | 1347 | INIT_LIST_HEAD(&team->port_list); |
8ff5105a JP |
1348 | err = team_queue_override_init(team); |
1349 | if (err) | |
1350 | goto err_team_queue_override_init; | |
3d249d4c JP |
1351 | |
1352 | team_adjust_ops(team); | |
1353 | ||
1354 | INIT_LIST_HEAD(&team->option_list); | |
80f7c668 | 1355 | INIT_LIST_HEAD(&team->option_inst_list); |
358b8382 JP |
1356 | err = team_options_register(team, team_options, ARRAY_SIZE(team_options)); |
1357 | if (err) | |
1358 | goto err_options_register; | |
3d249d4c JP |
1359 | netif_carrier_off(dev); |
1360 | ||
6c85f2bd JP |
1361 | team_set_lockdep_class(dev); |
1362 | ||
3d249d4c | 1363 | return 0; |
358b8382 JP |
1364 | |
1365 | err_options_register: | |
8ff5105a JP |
1366 | team_queue_override_fini(team); |
1367 | err_team_queue_override_init: | |
358b8382 JP |
1368 | free_percpu(team->pcpu_stats); |
1369 | ||
1370 | return err; | |
3d249d4c JP |
1371 | } |
1372 | ||
1373 | static void team_uninit(struct net_device *dev) | |
1374 | { | |
1375 | struct team *team = netdev_priv(dev); | |
1376 | struct team_port *port; | |
1377 | struct team_port *tmp; | |
1378 | ||
61dc3461 | 1379 | mutex_lock(&team->lock); |
3d249d4c JP |
1380 | list_for_each_entry_safe(port, tmp, &team->port_list, list) |
1381 | team_port_del(team, port->dev); | |
1382 | ||
1383 | __team_change_mode(team, NULL); /* cleanup */ | |
1384 | __team_options_unregister(team, team_options, ARRAY_SIZE(team_options)); | |
8ff5105a | 1385 | team_queue_override_fini(team); |
61dc3461 | 1386 | mutex_unlock(&team->lock); |
3d249d4c JP |
1387 | } |
1388 | ||
1389 | static void team_destructor(struct net_device *dev) | |
1390 | { | |
1391 | struct team *team = netdev_priv(dev); | |
1392 | ||
1393 | free_percpu(team->pcpu_stats); | |
1394 | free_netdev(dev); | |
1395 | } | |
1396 | ||
1397 | static int team_open(struct net_device *dev) | |
1398 | { | |
1399 | netif_carrier_on(dev); | |
1400 | return 0; | |
1401 | } | |
1402 | ||
1403 | static int team_close(struct net_device *dev) | |
1404 | { | |
1405 | netif_carrier_off(dev); | |
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | /* | |
1410 | * note: already called with rcu_read_lock | |
1411 | */ | |
1412 | static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev) | |
1413 | { | |
1414 | struct team *team = netdev_priv(dev); | |
8ff5105a | 1415 | bool tx_success; |
3d249d4c JP |
1416 | unsigned int len = skb->len; |
1417 | ||
8ff5105a JP |
1418 | tx_success = team_queue_override_transmit(team, skb); |
1419 | if (!tx_success) | |
1420 | tx_success = team->ops.transmit(team, skb); | |
3d249d4c JP |
1421 | if (tx_success) { |
1422 | struct team_pcpu_stats *pcpu_stats; | |
1423 | ||
1424 | pcpu_stats = this_cpu_ptr(team->pcpu_stats); | |
1425 | u64_stats_update_begin(&pcpu_stats->syncp); | |
1426 | pcpu_stats->tx_packets++; | |
1427 | pcpu_stats->tx_bytes += len; | |
1428 | u64_stats_update_end(&pcpu_stats->syncp); | |
1429 | } else { | |
1430 | this_cpu_inc(team->pcpu_stats->tx_dropped); | |
1431 | } | |
1432 | ||
1433 | return NETDEV_TX_OK; | |
1434 | } | |
1435 | ||
6c85f2bd JP |
1436 | static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb) |
1437 | { | |
1438 | /* | |
1439 | * This helper function exists to help dev_pick_tx get the correct | |
1440 | * destination queue. Using a helper function skips a call to | |
1441 | * skb_tx_hash and will put the skbs in the queue we expect on their | |
1442 | * way down to the team driver. | |
1443 | */ | |
1444 | u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; | |
1445 | ||
1446 | /* | |
1447 | * Save the original txq to restore before passing to the driver | |
1448 | */ | |
1449 | qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; | |
1450 | ||
1451 | if (unlikely(txq >= dev->real_num_tx_queues)) { | |
1452 | do { | |
1453 | txq -= dev->real_num_tx_queues; | |
1454 | } while (txq >= dev->real_num_tx_queues); | |
1455 | } | |
1456 | return txq; | |
1457 | } | |
1458 | ||
3d249d4c JP |
1459 | static void team_change_rx_flags(struct net_device *dev, int change) |
1460 | { | |
1461 | struct team *team = netdev_priv(dev); | |
1462 | struct team_port *port; | |
1463 | int inc; | |
1464 | ||
1465 | rcu_read_lock(); | |
1466 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
1467 | if (change & IFF_PROMISC) { | |
1468 | inc = dev->flags & IFF_PROMISC ? 1 : -1; | |
1469 | dev_set_promiscuity(port->dev, inc); | |
1470 | } | |
1471 | if (change & IFF_ALLMULTI) { | |
1472 | inc = dev->flags & IFF_ALLMULTI ? 1 : -1; | |
1473 | dev_set_allmulti(port->dev, inc); | |
1474 | } | |
1475 | } | |
1476 | rcu_read_unlock(); | |
1477 | } | |
1478 | ||
1479 | static void team_set_rx_mode(struct net_device *dev) | |
1480 | { | |
1481 | struct team *team = netdev_priv(dev); | |
1482 | struct team_port *port; | |
1483 | ||
1484 | rcu_read_lock(); | |
1485 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
1486 | dev_uc_sync(port->dev, dev); | |
1487 | dev_mc_sync(port->dev, dev); | |
1488 | } | |
1489 | rcu_read_unlock(); | |
1490 | } | |
1491 | ||
1492 | static int team_set_mac_address(struct net_device *dev, void *p) | |
1493 | { | |
1d76efe1 | 1494 | struct sockaddr *addr = p; |
3d249d4c JP |
1495 | struct team *team = netdev_priv(dev); |
1496 | struct team_port *port; | |
3d249d4c | 1497 | |
1d76efe1 JP |
1498 | if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data)) |
1499 | return -EADDRNOTAVAIL; | |
1500 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); | |
1501 | dev->addr_assign_type &= ~NET_ADDR_RANDOM; | |
3d249d4c JP |
1502 | rcu_read_lock(); |
1503 | list_for_each_entry_rcu(port, &team->port_list, list) | |
1d76efe1 JP |
1504 | if (team->ops.port_change_dev_addr) |
1505 | team->ops.port_change_dev_addr(team, port); | |
3d249d4c JP |
1506 | rcu_read_unlock(); |
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | static int team_change_mtu(struct net_device *dev, int new_mtu) | |
1511 | { | |
1512 | struct team *team = netdev_priv(dev); | |
1513 | struct team_port *port; | |
1514 | int err; | |
1515 | ||
1516 | /* | |
1517 | * Alhough this is reader, it's guarded by team lock. It's not possible | |
1518 | * to traverse list in reverse under rcu_read_lock | |
1519 | */ | |
61dc3461 | 1520 | mutex_lock(&team->lock); |
3d249d4c JP |
1521 | list_for_each_entry(port, &team->port_list, list) { |
1522 | err = dev_set_mtu(port->dev, new_mtu); | |
1523 | if (err) { | |
1524 | netdev_err(dev, "Device %s failed to change mtu", | |
1525 | port->dev->name); | |
1526 | goto unwind; | |
1527 | } | |
1528 | } | |
61dc3461 | 1529 | mutex_unlock(&team->lock); |
3d249d4c JP |
1530 | |
1531 | dev->mtu = new_mtu; | |
1532 | ||
1533 | return 0; | |
1534 | ||
1535 | unwind: | |
1536 | list_for_each_entry_continue_reverse(port, &team->port_list, list) | |
1537 | dev_set_mtu(port->dev, dev->mtu); | |
61dc3461 | 1538 | mutex_unlock(&team->lock); |
3d249d4c JP |
1539 | |
1540 | return err; | |
1541 | } | |
1542 | ||
1543 | static struct rtnl_link_stats64 * | |
1544 | team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) | |
1545 | { | |
1546 | struct team *team = netdev_priv(dev); | |
1547 | struct team_pcpu_stats *p; | |
1548 | u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes; | |
1549 | u32 rx_dropped = 0, tx_dropped = 0; | |
1550 | unsigned int start; | |
1551 | int i; | |
1552 | ||
1553 | for_each_possible_cpu(i) { | |
1554 | p = per_cpu_ptr(team->pcpu_stats, i); | |
1555 | do { | |
1556 | start = u64_stats_fetch_begin_bh(&p->syncp); | |
1557 | rx_packets = p->rx_packets; | |
1558 | rx_bytes = p->rx_bytes; | |
1559 | rx_multicast = p->rx_multicast; | |
1560 | tx_packets = p->tx_packets; | |
1561 | tx_bytes = p->tx_bytes; | |
1562 | } while (u64_stats_fetch_retry_bh(&p->syncp, start)); | |
1563 | ||
1564 | stats->rx_packets += rx_packets; | |
1565 | stats->rx_bytes += rx_bytes; | |
1566 | stats->multicast += rx_multicast; | |
1567 | stats->tx_packets += tx_packets; | |
1568 | stats->tx_bytes += tx_bytes; | |
1569 | /* | |
1570 | * rx_dropped & tx_dropped are u32, updated | |
1571 | * without syncp protection. | |
1572 | */ | |
1573 | rx_dropped += p->rx_dropped; | |
1574 | tx_dropped += p->tx_dropped; | |
1575 | } | |
1576 | stats->rx_dropped = rx_dropped; | |
1577 | stats->tx_dropped = tx_dropped; | |
1578 | return stats; | |
1579 | } | |
1580 | ||
8e586137 | 1581 | static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid) |
3d249d4c JP |
1582 | { |
1583 | struct team *team = netdev_priv(dev); | |
1584 | struct team_port *port; | |
87002b03 | 1585 | int err; |
3d249d4c | 1586 | |
87002b03 JP |
1587 | /* |
1588 | * Alhough this is reader, it's guarded by team lock. It's not possible | |
1589 | * to traverse list in reverse under rcu_read_lock | |
1590 | */ | |
1591 | mutex_lock(&team->lock); | |
1592 | list_for_each_entry(port, &team->port_list, list) { | |
1593 | err = vlan_vid_add(port->dev, vid); | |
1594 | if (err) | |
1595 | goto unwind; | |
3d249d4c | 1596 | } |
87002b03 | 1597 | mutex_unlock(&team->lock); |
8e586137 JP |
1598 | |
1599 | return 0; | |
87002b03 JP |
1600 | |
1601 | unwind: | |
1602 | list_for_each_entry_continue_reverse(port, &team->port_list, list) | |
1603 | vlan_vid_del(port->dev, vid); | |
1604 | mutex_unlock(&team->lock); | |
1605 | ||
1606 | return err; | |
3d249d4c JP |
1607 | } |
1608 | ||
8e586137 | 1609 | static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid) |
3d249d4c JP |
1610 | { |
1611 | struct team *team = netdev_priv(dev); | |
1612 | struct team_port *port; | |
1613 | ||
1614 | rcu_read_lock(); | |
87002b03 JP |
1615 | list_for_each_entry_rcu(port, &team->port_list, list) |
1616 | vlan_vid_del(port->dev, vid); | |
3d249d4c | 1617 | rcu_read_unlock(); |
8e586137 JP |
1618 | |
1619 | return 0; | |
3d249d4c JP |
1620 | } |
1621 | ||
bd2d0837 JP |
1622 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1623 | static void team_poll_controller(struct net_device *dev) | |
1624 | { | |
1625 | } | |
1626 | ||
1627 | static void __team_netpoll_cleanup(struct team *team) | |
1628 | { | |
1629 | struct team_port *port; | |
1630 | ||
1631 | list_for_each_entry(port, &team->port_list, list) | |
1632 | team_port_disable_netpoll(port); | |
1633 | } | |
1634 | ||
1635 | static void team_netpoll_cleanup(struct net_device *dev) | |
1636 | { | |
1637 | struct team *team = netdev_priv(dev); | |
1638 | ||
1639 | mutex_lock(&team->lock); | |
1640 | __team_netpoll_cleanup(team); | |
1641 | mutex_unlock(&team->lock); | |
1642 | } | |
1643 | ||
1644 | static int team_netpoll_setup(struct net_device *dev, | |
47be03a2 | 1645 | struct netpoll_info *npifo, gfp_t gfp) |
bd2d0837 JP |
1646 | { |
1647 | struct team *team = netdev_priv(dev); | |
1648 | struct team_port *port; | |
3324d024 | 1649 | int err = 0; |
bd2d0837 JP |
1650 | |
1651 | mutex_lock(&team->lock); | |
1652 | list_for_each_entry(port, &team->port_list, list) { | |
47be03a2 | 1653 | err = team_port_enable_netpoll(team, port, gfp); |
bd2d0837 JP |
1654 | if (err) { |
1655 | __team_netpoll_cleanup(team); | |
1656 | break; | |
1657 | } | |
1658 | } | |
1659 | mutex_unlock(&team->lock); | |
1660 | return err; | |
1661 | } | |
1662 | #endif | |
1663 | ||
3d249d4c JP |
1664 | static int team_add_slave(struct net_device *dev, struct net_device *port_dev) |
1665 | { | |
1666 | struct team *team = netdev_priv(dev); | |
1667 | int err; | |
1668 | ||
61dc3461 | 1669 | mutex_lock(&team->lock); |
3d249d4c | 1670 | err = team_port_add(team, port_dev); |
61dc3461 | 1671 | mutex_unlock(&team->lock); |
3d249d4c JP |
1672 | return err; |
1673 | } | |
1674 | ||
1675 | static int team_del_slave(struct net_device *dev, struct net_device *port_dev) | |
1676 | { | |
1677 | struct team *team = netdev_priv(dev); | |
1678 | int err; | |
1679 | ||
61dc3461 | 1680 | mutex_lock(&team->lock); |
3d249d4c | 1681 | err = team_port_del(team, port_dev); |
61dc3461 | 1682 | mutex_unlock(&team->lock); |
3d249d4c JP |
1683 | return err; |
1684 | } | |
1685 | ||
234a8fd4 JP |
1686 | static netdev_features_t team_fix_features(struct net_device *dev, |
1687 | netdev_features_t features) | |
1688 | { | |
1689 | struct team_port *port; | |
1690 | struct team *team = netdev_priv(dev); | |
1691 | netdev_features_t mask; | |
1692 | ||
1693 | mask = features; | |
1694 | features &= ~NETIF_F_ONE_FOR_ALL; | |
1695 | features |= NETIF_F_ALL_FOR_ALL; | |
1696 | ||
1697 | rcu_read_lock(); | |
1698 | list_for_each_entry_rcu(port, &team->port_list, list) { | |
1699 | features = netdev_increment_features(features, | |
1700 | port->dev->features, | |
1701 | mask); | |
1702 | } | |
1703 | rcu_read_unlock(); | |
1704 | return features; | |
1705 | } | |
1706 | ||
3d249d4c JP |
1707 | static const struct net_device_ops team_netdev_ops = { |
1708 | .ndo_init = team_init, | |
1709 | .ndo_uninit = team_uninit, | |
1710 | .ndo_open = team_open, | |
1711 | .ndo_stop = team_close, | |
1712 | .ndo_start_xmit = team_xmit, | |
6c85f2bd | 1713 | .ndo_select_queue = team_select_queue, |
3d249d4c JP |
1714 | .ndo_change_rx_flags = team_change_rx_flags, |
1715 | .ndo_set_rx_mode = team_set_rx_mode, | |
1716 | .ndo_set_mac_address = team_set_mac_address, | |
1717 | .ndo_change_mtu = team_change_mtu, | |
1718 | .ndo_get_stats64 = team_get_stats64, | |
1719 | .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid, | |
1720 | .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid, | |
bd2d0837 JP |
1721 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1722 | .ndo_poll_controller = team_poll_controller, | |
1723 | .ndo_netpoll_setup = team_netpoll_setup, | |
1724 | .ndo_netpoll_cleanup = team_netpoll_cleanup, | |
1725 | #endif | |
3d249d4c JP |
1726 | .ndo_add_slave = team_add_slave, |
1727 | .ndo_del_slave = team_del_slave, | |
234a8fd4 | 1728 | .ndo_fix_features = team_fix_features, |
3d249d4c JP |
1729 | }; |
1730 | ||
1731 | ||
1732 | /*********************** | |
1733 | * rt netlink interface | |
1734 | ***********************/ | |
1735 | ||
1d76efe1 JP |
1736 | static void team_setup_by_port(struct net_device *dev, |
1737 | struct net_device *port_dev) | |
1738 | { | |
1739 | dev->header_ops = port_dev->header_ops; | |
1740 | dev->type = port_dev->type; | |
1741 | dev->hard_header_len = port_dev->hard_header_len; | |
1742 | dev->addr_len = port_dev->addr_len; | |
1743 | dev->mtu = port_dev->mtu; | |
1744 | memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len); | |
1745 | memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len); | |
1746 | dev->addr_assign_type &= ~NET_ADDR_RANDOM; | |
1747 | } | |
1748 | ||
1749 | static int team_dev_type_check_change(struct net_device *dev, | |
1750 | struct net_device *port_dev) | |
1751 | { | |
1752 | struct team *team = netdev_priv(dev); | |
1753 | char *portname = port_dev->name; | |
1754 | int err; | |
1755 | ||
1756 | if (dev->type == port_dev->type) | |
1757 | return 0; | |
1758 | if (!list_empty(&team->port_list)) { | |
1759 | netdev_err(dev, "Device %s is of different type\n", portname); | |
1760 | return -EBUSY; | |
1761 | } | |
1762 | err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev); | |
1763 | err = notifier_to_errno(err); | |
1764 | if (err) { | |
1765 | netdev_err(dev, "Refused to change device type\n"); | |
1766 | return err; | |
1767 | } | |
1768 | dev_uc_flush(dev); | |
1769 | dev_mc_flush(dev); | |
1770 | team_setup_by_port(dev, port_dev); | |
1771 | call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev); | |
1772 | return 0; | |
1773 | } | |
1774 | ||
3d249d4c JP |
1775 | static void team_setup(struct net_device *dev) |
1776 | { | |
1777 | ether_setup(dev); | |
1778 | ||
1779 | dev->netdev_ops = &team_netdev_ops; | |
1780 | dev->destructor = team_destructor; | |
1781 | dev->tx_queue_len = 0; | |
1782 | dev->flags |= IFF_MULTICAST; | |
1783 | dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); | |
1784 | ||
1785 | /* | |
1786 | * Indicate we support unicast address filtering. That way core won't | |
1787 | * bring us to promisc mode in case a unicast addr is added. | |
1788 | * Let this up to underlay drivers. | |
1789 | */ | |
5a1d1c8c | 1790 | dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE; |
3d249d4c JP |
1791 | |
1792 | dev->features |= NETIF_F_LLTX; | |
1793 | dev->features |= NETIF_F_GRO; | |
1794 | dev->hw_features = NETIF_F_HW_VLAN_TX | | |
1795 | NETIF_F_HW_VLAN_RX | | |
1796 | NETIF_F_HW_VLAN_FILTER; | |
1797 | ||
1798 | dev->features |= dev->hw_features; | |
1799 | } | |
1800 | ||
1801 | static int team_newlink(struct net *src_net, struct net_device *dev, | |
1802 | struct nlattr *tb[], struct nlattr *data[]) | |
1803 | { | |
1804 | int err; | |
1805 | ||
1806 | if (tb[IFLA_ADDRESS] == NULL) | |
7ce5d222 | 1807 | eth_hw_addr_random(dev); |
3d249d4c JP |
1808 | |
1809 | err = register_netdevice(dev); | |
1810 | if (err) | |
1811 | return err; | |
1812 | ||
1813 | return 0; | |
1814 | } | |
1815 | ||
1816 | static int team_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1817 | { | |
1818 | if (tb[IFLA_ADDRESS]) { | |
1819 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1820 | return -EINVAL; | |
1821 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1822 | return -EADDRNOTAVAIL; | |
1823 | } | |
1824 | return 0; | |
1825 | } | |
1826 | ||
6c85f2bd JP |
1827 | static unsigned int team_get_num_tx_queues(void) |
1828 | { | |
1829 | return TEAM_DEFAULT_NUM_TX_QUEUES; | |
1830 | } | |
1831 | ||
1832 | static unsigned int team_get_num_rx_queues(void) | |
1833 | { | |
1834 | return TEAM_DEFAULT_NUM_RX_QUEUES; | |
1835 | } | |
1836 | ||
3d249d4c | 1837 | static struct rtnl_link_ops team_link_ops __read_mostly = { |
6c85f2bd JP |
1838 | .kind = DRV_NAME, |
1839 | .priv_size = sizeof(struct team), | |
1840 | .setup = team_setup, | |
1841 | .newlink = team_newlink, | |
1842 | .validate = team_validate, | |
1843 | .get_num_tx_queues = team_get_num_tx_queues, | |
1844 | .get_num_rx_queues = team_get_num_rx_queues, | |
3d249d4c JP |
1845 | }; |
1846 | ||
1847 | ||
1848 | /*********************************** | |
1849 | * Generic netlink custom interface | |
1850 | ***********************************/ | |
1851 | ||
1852 | static struct genl_family team_nl_family = { | |
1853 | .id = GENL_ID_GENERATE, | |
1854 | .name = TEAM_GENL_NAME, | |
1855 | .version = TEAM_GENL_VERSION, | |
1856 | .maxattr = TEAM_ATTR_MAX, | |
1857 | .netnsok = true, | |
1858 | }; | |
1859 | ||
1860 | static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = { | |
1861 | [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, }, | |
1862 | [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 }, | |
1863 | [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED }, | |
1864 | [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED }, | |
1865 | }; | |
1866 | ||
1867 | static const struct nla_policy | |
1868 | team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = { | |
1869 | [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, }, | |
1870 | [TEAM_ATTR_OPTION_NAME] = { | |
1871 | .type = NLA_STRING, | |
1872 | .len = TEAM_STRING_MAX_LEN, | |
1873 | }, | |
1874 | [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG }, | |
1875 | [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 }, | |
2615598f | 1876 | [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY }, |
3d249d4c JP |
1877 | }; |
1878 | ||
1879 | static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) | |
1880 | { | |
1881 | struct sk_buff *msg; | |
1882 | void *hdr; | |
1883 | int err; | |
1884 | ||
58050fce | 1885 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
3d249d4c JP |
1886 | if (!msg) |
1887 | return -ENOMEM; | |
1888 | ||
1889 | hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, | |
1890 | &team_nl_family, 0, TEAM_CMD_NOOP); | |
1891 | if (IS_ERR(hdr)) { | |
1892 | err = PTR_ERR(hdr); | |
1893 | goto err_msg_put; | |
1894 | } | |
1895 | ||
1896 | genlmsg_end(msg, hdr); | |
1897 | ||
1898 | return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid); | |
1899 | ||
1900 | err_msg_put: | |
1901 | nlmsg_free(msg); | |
1902 | ||
1903 | return err; | |
1904 | } | |
1905 | ||
1906 | /* | |
1907 | * Netlink cmd functions should be locked by following two functions. | |
8c0713a5 | 1908 | * Since dev gets held here, that ensures dev won't disappear in between. |
3d249d4c JP |
1909 | */ |
1910 | static struct team *team_nl_team_get(struct genl_info *info) | |
1911 | { | |
1912 | struct net *net = genl_info_net(info); | |
1913 | int ifindex; | |
1914 | struct net_device *dev; | |
1915 | struct team *team; | |
1916 | ||
1917 | if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX]) | |
1918 | return NULL; | |
1919 | ||
1920 | ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]); | |
8c0713a5 | 1921 | dev = dev_get_by_index(net, ifindex); |
3d249d4c | 1922 | if (!dev || dev->netdev_ops != &team_netdev_ops) { |
8c0713a5 JP |
1923 | if (dev) |
1924 | dev_put(dev); | |
3d249d4c JP |
1925 | return NULL; |
1926 | } | |
1927 | ||
1928 | team = netdev_priv(dev); | |
61dc3461 | 1929 | mutex_lock(&team->lock); |
3d249d4c JP |
1930 | return team; |
1931 | } | |
1932 | ||
1933 | static void team_nl_team_put(struct team *team) | |
1934 | { | |
61dc3461 | 1935 | mutex_unlock(&team->lock); |
8c0713a5 | 1936 | dev_put(team->dev); |
3d249d4c JP |
1937 | } |
1938 | ||
1939 | static int team_nl_send_generic(struct genl_info *info, struct team *team, | |
1940 | int (*fill_func)(struct sk_buff *skb, | |
1941 | struct genl_info *info, | |
1942 | int flags, struct team *team)) | |
1943 | { | |
1944 | struct sk_buff *skb; | |
1945 | int err; | |
1946 | ||
58050fce | 1947 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
3d249d4c JP |
1948 | if (!skb) |
1949 | return -ENOMEM; | |
1950 | ||
1951 | err = fill_func(skb, info, NLM_F_ACK, team); | |
1952 | if (err < 0) | |
1953 | goto err_fill; | |
1954 | ||
1955 | err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid); | |
1956 | return err; | |
1957 | ||
1958 | err_fill: | |
1959 | nlmsg_free(skb); | |
1960 | return err; | |
1961 | } | |
1962 | ||
9b00cf2d JP |
1963 | typedef int team_nl_send_func_t(struct sk_buff *skb, |
1964 | struct team *team, u32 pid); | |
1965 | ||
1966 | static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid) | |
1967 | { | |
1968 | return genlmsg_unicast(dev_net(team->dev), skb, pid); | |
1969 | } | |
1970 | ||
01048d9a JP |
1971 | static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team, |
1972 | struct team_option_inst *opt_inst) | |
1973 | { | |
1974 | struct nlattr *option_item; | |
1975 | struct team_option *option = opt_inst->option; | |
9b00cf2d | 1976 | struct team_option_inst_info *opt_inst_info = &opt_inst->info; |
01048d9a JP |
1977 | struct team_gsetter_ctx ctx; |
1978 | int err; | |
1979 | ||
9b00cf2d JP |
1980 | ctx.info = opt_inst_info; |
1981 | err = team_option_get(team, opt_inst, &ctx); | |
1982 | if (err) | |
1983 | return err; | |
1984 | ||
01048d9a JP |
1985 | option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION); |
1986 | if (!option_item) | |
9b00cf2d | 1987 | return -EMSGSIZE; |
01048d9a | 1988 | |
9b00cf2d JP |
1989 | if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name)) |
1990 | goto nest_cancel; | |
01048d9a JP |
1991 | if (opt_inst_info->port && |
1992 | nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX, | |
1993 | opt_inst_info->port->dev->ifindex)) | |
9b00cf2d | 1994 | goto nest_cancel; |
01048d9a JP |
1995 | if (opt_inst->option->array_size && |
1996 | nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX, | |
1997 | opt_inst_info->array_index)) | |
9b00cf2d | 1998 | goto nest_cancel; |
01048d9a JP |
1999 | |
2000 | switch (option->type) { | |
2001 | case TEAM_OPTION_TYPE_U32: | |
2002 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32)) | |
9b00cf2d | 2003 | goto nest_cancel; |
01048d9a | 2004 | if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val)) |
9b00cf2d | 2005 | goto nest_cancel; |
01048d9a JP |
2006 | break; |
2007 | case TEAM_OPTION_TYPE_STRING: | |
2008 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING)) | |
9b00cf2d | 2009 | goto nest_cancel; |
01048d9a JP |
2010 | if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA, |
2011 | ctx.data.str_val)) | |
9b00cf2d | 2012 | goto nest_cancel; |
01048d9a JP |
2013 | break; |
2014 | case TEAM_OPTION_TYPE_BINARY: | |
2015 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY)) | |
9b00cf2d | 2016 | goto nest_cancel; |
01048d9a JP |
2017 | if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len, |
2018 | ctx.data.bin_val.ptr)) | |
9b00cf2d | 2019 | goto nest_cancel; |
01048d9a JP |
2020 | break; |
2021 | case TEAM_OPTION_TYPE_BOOL: | |
2022 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG)) | |
9b00cf2d | 2023 | goto nest_cancel; |
01048d9a JP |
2024 | if (ctx.data.bool_val && |
2025 | nla_put_flag(skb, TEAM_ATTR_OPTION_DATA)) | |
9b00cf2d | 2026 | goto nest_cancel; |
01048d9a | 2027 | break; |
69821638 JP |
2028 | case TEAM_OPTION_TYPE_S32: |
2029 | if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32)) | |
2030 | goto nest_cancel; | |
2031 | if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val)) | |
2032 | goto nest_cancel; | |
2033 | break; | |
01048d9a JP |
2034 | default: |
2035 | BUG(); | |
2036 | } | |
9b00cf2d JP |
2037 | if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED)) |
2038 | goto nest_cancel; | |
2039 | if (opt_inst->changed) { | |
2040 | if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED)) | |
2041 | goto nest_cancel; | |
2042 | opt_inst->changed = false; | |
2043 | } | |
01048d9a JP |
2044 | nla_nest_end(skb, option_item); |
2045 | return 0; | |
2046 | ||
9b00cf2d JP |
2047 | nest_cancel: |
2048 | nla_nest_cancel(skb, option_item); | |
2049 | return -EMSGSIZE; | |
2050 | } | |
2051 | ||
2052 | static int __send_and_alloc_skb(struct sk_buff **pskb, | |
2053 | struct team *team, u32 pid, | |
2054 | team_nl_send_func_t *send_func) | |
2055 | { | |
2056 | int err; | |
2057 | ||
2058 | if (*pskb) { | |
2059 | err = send_func(*pskb, team, pid); | |
2060 | if (err) | |
2061 | return err; | |
2062 | } | |
58050fce | 2063 | *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); |
9b00cf2d JP |
2064 | if (!*pskb) |
2065 | return -ENOMEM; | |
2066 | return 0; | |
01048d9a JP |
2067 | } |
2068 | ||
9b00cf2d JP |
2069 | static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq, |
2070 | int flags, team_nl_send_func_t *send_func, | |
01048d9a | 2071 | struct list_head *sel_opt_inst_list) |
3d249d4c JP |
2072 | { |
2073 | struct nlattr *option_list; | |
9b00cf2d | 2074 | struct nlmsghdr *nlh; |
3d249d4c | 2075 | void *hdr; |
80f7c668 JP |
2076 | struct team_option_inst *opt_inst; |
2077 | int err; | |
9b00cf2d JP |
2078 | struct sk_buff *skb = NULL; |
2079 | bool incomplete; | |
2080 | int i; | |
3d249d4c | 2081 | |
9b00cf2d JP |
2082 | opt_inst = list_first_entry(sel_opt_inst_list, |
2083 | struct team_option_inst, tmp_list); | |
2084 | ||
2085 | start_again: | |
2086 | err = __send_and_alloc_skb(&skb, team, pid, send_func); | |
2087 | if (err) | |
2088 | return err; | |
2089 | ||
2090 | hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI, | |
3d249d4c JP |
2091 | TEAM_CMD_OPTIONS_GET); |
2092 | if (IS_ERR(hdr)) | |
2093 | return PTR_ERR(hdr); | |
2094 | ||
86ebb02d DM |
2095 | if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex)) |
2096 | goto nla_put_failure; | |
3d249d4c JP |
2097 | option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION); |
2098 | if (!option_list) | |
75db986a | 2099 | goto nla_put_failure; |
3d249d4c | 2100 | |
9b00cf2d JP |
2101 | i = 0; |
2102 | incomplete = false; | |
2103 | list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) { | |
01048d9a | 2104 | err = team_nl_fill_one_option_get(skb, team, opt_inst); |
9b00cf2d JP |
2105 | if (err) { |
2106 | if (err == -EMSGSIZE) { | |
2107 | if (!i) | |
2108 | goto errout; | |
2109 | incomplete = true; | |
2110 | break; | |
2111 | } | |
01048d9a | 2112 | goto errout; |
9b00cf2d JP |
2113 | } |
2114 | i++; | |
3d249d4c JP |
2115 | } |
2116 | ||
2117 | nla_nest_end(skb, option_list); | |
9b00cf2d JP |
2118 | genlmsg_end(skb, hdr); |
2119 | if (incomplete) | |
2120 | goto start_again; | |
2121 | ||
2122 | send_done: | |
2123 | nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI); | |
2124 | if (!nlh) { | |
2125 | err = __send_and_alloc_skb(&skb, team, pid, send_func); | |
2126 | if (err) | |
2127 | goto errout; | |
2128 | goto send_done; | |
2129 | } | |
2130 | ||
2131 | return send_func(skb, team, pid); | |
3d249d4c JP |
2132 | |
2133 | nla_put_failure: | |
80f7c668 JP |
2134 | err = -EMSGSIZE; |
2135 | errout: | |
3d249d4c | 2136 | genlmsg_cancel(skb, hdr); |
9b00cf2d | 2137 | nlmsg_free(skb); |
80f7c668 | 2138 | return err; |
3d249d4c JP |
2139 | } |
2140 | ||
3d249d4c JP |
2141 | static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info) |
2142 | { | |
2143 | struct team *team; | |
9b00cf2d | 2144 | struct team_option_inst *opt_inst; |
3d249d4c | 2145 | int err; |
9b00cf2d | 2146 | LIST_HEAD(sel_opt_inst_list); |
3d249d4c JP |
2147 | |
2148 | team = team_nl_team_get(info); | |
2149 | if (!team) | |
2150 | return -EINVAL; | |
2151 | ||
9b00cf2d JP |
2152 | list_for_each_entry(opt_inst, &team->option_inst_list, list) |
2153 | list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list); | |
2154 | err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq, | |
2155 | NLM_F_ACK, team_nl_send_unicast, | |
2156 | &sel_opt_inst_list); | |
3d249d4c JP |
2157 | |
2158 | team_nl_team_put(team); | |
2159 | ||
2160 | return err; | |
2161 | } | |
2162 | ||
2fcdb2c9 JP |
2163 | static int team_nl_send_event_options_get(struct team *team, |
2164 | struct list_head *sel_opt_inst_list); | |
2165 | ||
3d249d4c JP |
2166 | static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info) |
2167 | { | |
2168 | struct team *team; | |
2169 | int err = 0; | |
2170 | int i; | |
2171 | struct nlattr *nl_option; | |
2fcdb2c9 | 2172 | LIST_HEAD(opt_inst_list); |
3d249d4c JP |
2173 | |
2174 | team = team_nl_team_get(info); | |
2175 | if (!team) | |
2176 | return -EINVAL; | |
2177 | ||
2178 | err = -EINVAL; | |
2179 | if (!info->attrs[TEAM_ATTR_LIST_OPTION]) { | |
2180 | err = -EINVAL; | |
2181 | goto team_put; | |
2182 | } | |
2183 | ||
2184 | nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) { | |
80f7c668 | 2185 | struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1]; |
b1303326 | 2186 | struct nlattr *attr; |
14f066ba | 2187 | struct nlattr *attr_data; |
3d249d4c | 2188 | enum team_option_type opt_type; |
80f7c668 | 2189 | int opt_port_ifindex = 0; /* != 0 for per-port options */ |
b1303326 JP |
2190 | u32 opt_array_index = 0; |
2191 | bool opt_is_array = false; | |
80f7c668 | 2192 | struct team_option_inst *opt_inst; |
3d249d4c JP |
2193 | char *opt_name; |
2194 | bool opt_found = false; | |
2195 | ||
2196 | if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) { | |
2197 | err = -EINVAL; | |
2198 | goto team_put; | |
2199 | } | |
80f7c668 | 2200 | err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX, |
3d249d4c JP |
2201 | nl_option, team_nl_option_policy); |
2202 | if (err) | |
2203 | goto team_put; | |
80f7c668 | 2204 | if (!opt_attrs[TEAM_ATTR_OPTION_NAME] || |
14f066ba | 2205 | !opt_attrs[TEAM_ATTR_OPTION_TYPE]) { |
3d249d4c JP |
2206 | err = -EINVAL; |
2207 | goto team_put; | |
2208 | } | |
80f7c668 | 2209 | switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) { |
3d249d4c JP |
2210 | case NLA_U32: |
2211 | opt_type = TEAM_OPTION_TYPE_U32; | |
2212 | break; | |
2213 | case NLA_STRING: | |
2214 | opt_type = TEAM_OPTION_TYPE_STRING; | |
2215 | break; | |
2615598f JP |
2216 | case NLA_BINARY: |
2217 | opt_type = TEAM_OPTION_TYPE_BINARY; | |
2218 | break; | |
14f066ba JP |
2219 | case NLA_FLAG: |
2220 | opt_type = TEAM_OPTION_TYPE_BOOL; | |
2221 | break; | |
69821638 JP |
2222 | case NLA_S32: |
2223 | opt_type = TEAM_OPTION_TYPE_S32; | |
2224 | break; | |
3d249d4c JP |
2225 | default: |
2226 | goto team_put; | |
2227 | } | |
2228 | ||
14f066ba JP |
2229 | attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA]; |
2230 | if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) { | |
2231 | err = -EINVAL; | |
2232 | goto team_put; | |
2233 | } | |
2234 | ||
80f7c668 | 2235 | opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]); |
b1303326 JP |
2236 | attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX]; |
2237 | if (attr) | |
2238 | opt_port_ifindex = nla_get_u32(attr); | |
2239 | ||
2240 | attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX]; | |
2241 | if (attr) { | |
2242 | opt_is_array = true; | |
2243 | opt_array_index = nla_get_u32(attr); | |
2244 | } | |
80f7c668 JP |
2245 | |
2246 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { | |
2247 | struct team_option *option = opt_inst->option; | |
80f7c668 | 2248 | struct team_gsetter_ctx ctx; |
85d59a87 | 2249 | struct team_option_inst_info *opt_inst_info; |
80f7c668 | 2250 | int tmp_ifindex; |
3d249d4c | 2251 | |
85d59a87 JP |
2252 | opt_inst_info = &opt_inst->info; |
2253 | tmp_ifindex = opt_inst_info->port ? | |
2254 | opt_inst_info->port->dev->ifindex : 0; | |
3d249d4c | 2255 | if (option->type != opt_type || |
80f7c668 | 2256 | strcmp(option->name, opt_name) || |
b1303326 JP |
2257 | tmp_ifindex != opt_port_ifindex || |
2258 | (option->array_size && !opt_is_array) || | |
85d59a87 | 2259 | opt_inst_info->array_index != opt_array_index) |
3d249d4c JP |
2260 | continue; |
2261 | opt_found = true; | |
85d59a87 | 2262 | ctx.info = opt_inst_info; |
3d249d4c JP |
2263 | switch (opt_type) { |
2264 | case TEAM_OPTION_TYPE_U32: | |
14f066ba | 2265 | ctx.data.u32_val = nla_get_u32(attr_data); |
3d249d4c JP |
2266 | break; |
2267 | case TEAM_OPTION_TYPE_STRING: | |
14f066ba | 2268 | if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) { |
2615598f JP |
2269 | err = -EINVAL; |
2270 | goto team_put; | |
2271 | } | |
14f066ba | 2272 | ctx.data.str_val = nla_data(attr_data); |
3d249d4c | 2273 | break; |
2615598f | 2274 | case TEAM_OPTION_TYPE_BINARY: |
14f066ba JP |
2275 | ctx.data.bin_val.len = nla_len(attr_data); |
2276 | ctx.data.bin_val.ptr = nla_data(attr_data); | |
2277 | break; | |
2278 | case TEAM_OPTION_TYPE_BOOL: | |
2279 | ctx.data.bool_val = attr_data ? true : false; | |
2615598f | 2280 | break; |
69821638 JP |
2281 | case TEAM_OPTION_TYPE_S32: |
2282 | ctx.data.s32_val = nla_get_s32(attr_data); | |
2283 | break; | |
3d249d4c JP |
2284 | default: |
2285 | BUG(); | |
2286 | } | |
80f7c668 | 2287 | err = team_option_set(team, opt_inst, &ctx); |
3d249d4c JP |
2288 | if (err) |
2289 | goto team_put; | |
2fcdb2c9 JP |
2290 | opt_inst->changed = true; |
2291 | list_add(&opt_inst->tmp_list, &opt_inst_list); | |
3d249d4c JP |
2292 | } |
2293 | if (!opt_found) { | |
2294 | err = -ENOENT; | |
2295 | goto team_put; | |
2296 | } | |
2297 | } | |
2298 | ||
2fcdb2c9 JP |
2299 | err = team_nl_send_event_options_get(team, &opt_inst_list); |
2300 | ||
3d249d4c JP |
2301 | team_put: |
2302 | team_nl_team_put(team); | |
2303 | ||
2304 | return err; | |
2305 | } | |
2306 | ||
b82b9183 JP |
2307 | static int team_nl_fill_port_list_get(struct sk_buff *skb, |
2308 | u32 pid, u32 seq, int flags, | |
2309 | struct team *team, | |
2310 | bool fillall) | |
3d249d4c JP |
2311 | { |
2312 | struct nlattr *port_list; | |
2313 | void *hdr; | |
2314 | struct team_port *port; | |
2315 | ||
2316 | hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags, | |
2317 | TEAM_CMD_PORT_LIST_GET); | |
2318 | if (IS_ERR(hdr)) | |
2319 | return PTR_ERR(hdr); | |
2320 | ||
86ebb02d DM |
2321 | if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex)) |
2322 | goto nla_put_failure; | |
3d249d4c JP |
2323 | port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT); |
2324 | if (!port_list) | |
3221c646 | 2325 | goto nla_put_failure; |
3d249d4c JP |
2326 | |
2327 | list_for_each_entry(port, &team->port_list, list) { | |
2328 | struct nlattr *port_item; | |
2329 | ||
b82b9183 JP |
2330 | /* Include only changed ports if fill all mode is not on */ |
2331 | if (!fillall && !port->changed) | |
2332 | continue; | |
3d249d4c JP |
2333 | port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT); |
2334 | if (!port_item) | |
2335 | goto nla_put_failure; | |
86ebb02d DM |
2336 | if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex)) |
2337 | goto nla_put_failure; | |
b82b9183 | 2338 | if (port->changed) { |
86ebb02d DM |
2339 | if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED)) |
2340 | goto nla_put_failure; | |
b82b9183 JP |
2341 | port->changed = false; |
2342 | } | |
86ebb02d DM |
2343 | if ((port->removed && |
2344 | nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) || | |
71472ec1 | 2345 | (port->state.linkup && |
86ebb02d | 2346 | nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) || |
71472ec1 JP |
2347 | nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) || |
2348 | nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex)) | |
86ebb02d | 2349 | goto nla_put_failure; |
3d249d4c JP |
2350 | nla_nest_end(skb, port_item); |
2351 | } | |
2352 | ||
2353 | nla_nest_end(skb, port_list); | |
2354 | return genlmsg_end(skb, hdr); | |
2355 | ||
2356 | nla_put_failure: | |
2357 | genlmsg_cancel(skb, hdr); | |
2358 | return -EMSGSIZE; | |
2359 | } | |
2360 | ||
b82b9183 JP |
2361 | static int team_nl_fill_port_list_get_all(struct sk_buff *skb, |
2362 | struct genl_info *info, int flags, | |
2363 | struct team *team) | |
3d249d4c | 2364 | { |
b82b9183 JP |
2365 | return team_nl_fill_port_list_get(skb, info->snd_pid, |
2366 | info->snd_seq, NLM_F_ACK, | |
2367 | team, true); | |
3d249d4c JP |
2368 | } |
2369 | ||
2370 | static int team_nl_cmd_port_list_get(struct sk_buff *skb, | |
2371 | struct genl_info *info) | |
2372 | { | |
2373 | struct team *team; | |
2374 | int err; | |
2375 | ||
2376 | team = team_nl_team_get(info); | |
2377 | if (!team) | |
2378 | return -EINVAL; | |
2379 | ||
b82b9183 | 2380 | err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all); |
3d249d4c JP |
2381 | |
2382 | team_nl_team_put(team); | |
2383 | ||
2384 | return err; | |
2385 | } | |
2386 | ||
2387 | static struct genl_ops team_nl_ops[] = { | |
2388 | { | |
2389 | .cmd = TEAM_CMD_NOOP, | |
2390 | .doit = team_nl_cmd_noop, | |
2391 | .policy = team_nl_policy, | |
2392 | }, | |
2393 | { | |
2394 | .cmd = TEAM_CMD_OPTIONS_SET, | |
2395 | .doit = team_nl_cmd_options_set, | |
2396 | .policy = team_nl_policy, | |
2397 | .flags = GENL_ADMIN_PERM, | |
2398 | }, | |
2399 | { | |
2400 | .cmd = TEAM_CMD_OPTIONS_GET, | |
2401 | .doit = team_nl_cmd_options_get, | |
2402 | .policy = team_nl_policy, | |
2403 | .flags = GENL_ADMIN_PERM, | |
2404 | }, | |
2405 | { | |
2406 | .cmd = TEAM_CMD_PORT_LIST_GET, | |
2407 | .doit = team_nl_cmd_port_list_get, | |
2408 | .policy = team_nl_policy, | |
2409 | .flags = GENL_ADMIN_PERM, | |
2410 | }, | |
2411 | }; | |
2412 | ||
2413 | static struct genl_multicast_group team_change_event_mcgrp = { | |
2414 | .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, | |
2415 | }; | |
2416 | ||
9b00cf2d JP |
2417 | static int team_nl_send_multicast(struct sk_buff *skb, |
2418 | struct team *team, u32 pid) | |
2419 | { | |
2420 | return genlmsg_multicast_netns(dev_net(team->dev), skb, 0, | |
2421 | team_change_event_mcgrp.id, GFP_KERNEL); | |
2422 | } | |
2423 | ||
01048d9a JP |
2424 | static int team_nl_send_event_options_get(struct team *team, |
2425 | struct list_head *sel_opt_inst_list) | |
3d249d4c | 2426 | { |
9b00cf2d JP |
2427 | return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast, |
2428 | sel_opt_inst_list); | |
3d249d4c JP |
2429 | } |
2430 | ||
b82b9183 | 2431 | static int team_nl_send_event_port_list_get(struct team *team) |
3d249d4c JP |
2432 | { |
2433 | struct sk_buff *skb; | |
2434 | int err; | |
b82b9183 | 2435 | struct net *net = dev_net(team->dev); |
3d249d4c | 2436 | |
58050fce | 2437 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
3d249d4c JP |
2438 | if (!skb) |
2439 | return -ENOMEM; | |
2440 | ||
b82b9183 | 2441 | err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false); |
3d249d4c JP |
2442 | if (err < 0) |
2443 | goto err_fill; | |
2444 | ||
2445 | err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id, | |
2446 | GFP_KERNEL); | |
2447 | return err; | |
2448 | ||
2449 | err_fill: | |
2450 | nlmsg_free(skb); | |
2451 | return err; | |
2452 | } | |
2453 | ||
2454 | static int team_nl_init(void) | |
2455 | { | |
2456 | int err; | |
2457 | ||
2458 | err = genl_register_family_with_ops(&team_nl_family, team_nl_ops, | |
2459 | ARRAY_SIZE(team_nl_ops)); | |
2460 | if (err) | |
2461 | return err; | |
2462 | ||
2463 | err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp); | |
2464 | if (err) | |
2465 | goto err_change_event_grp_reg; | |
2466 | ||
2467 | return 0; | |
2468 | ||
2469 | err_change_event_grp_reg: | |
2470 | genl_unregister_family(&team_nl_family); | |
2471 | ||
2472 | return err; | |
2473 | } | |
2474 | ||
2475 | static void team_nl_fini(void) | |
2476 | { | |
2477 | genl_unregister_family(&team_nl_family); | |
2478 | } | |
2479 | ||
2480 | ||
2481 | /****************** | |
2482 | * Change checkers | |
2483 | ******************/ | |
2484 | ||
b82b9183 | 2485 | static void __team_options_change_check(struct team *team) |
3d249d4c JP |
2486 | { |
2487 | int err; | |
01048d9a JP |
2488 | struct team_option_inst *opt_inst; |
2489 | LIST_HEAD(sel_opt_inst_list); | |
3d249d4c | 2490 | |
01048d9a JP |
2491 | list_for_each_entry(opt_inst, &team->option_inst_list, list) { |
2492 | if (opt_inst->changed) | |
2493 | list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list); | |
2494 | } | |
2495 | err = team_nl_send_event_options_get(team, &sel_opt_inst_list); | |
0c7517e9 | 2496 | if (err && err != -ESRCH) |
9b00cf2d JP |
2497 | netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n", |
2498 | err); | |
3d249d4c JP |
2499 | } |
2500 | ||
2501 | /* rtnl lock is held */ | |
2502 | static void __team_port_change_check(struct team_port *port, bool linkup) | |
2503 | { | |
2504 | int err; | |
2505 | ||
71472ec1 | 2506 | if (!port->removed && port->state.linkup == linkup) |
3d249d4c JP |
2507 | return; |
2508 | ||
b82b9183 | 2509 | port->changed = true; |
71472ec1 JP |
2510 | port->state.linkup = linkup; |
2511 | team_refresh_port_linkup(port); | |
3d249d4c JP |
2512 | if (linkup) { |
2513 | struct ethtool_cmd ecmd; | |
2514 | ||
2515 | err = __ethtool_get_settings(port->dev, &ecmd); | |
2516 | if (!err) { | |
71472ec1 JP |
2517 | port->state.speed = ethtool_cmd_speed(&ecmd); |
2518 | port->state.duplex = ecmd.duplex; | |
3d249d4c JP |
2519 | goto send_event; |
2520 | } | |
2521 | } | |
71472ec1 JP |
2522 | port->state.speed = 0; |
2523 | port->state.duplex = 0; | |
3d249d4c JP |
2524 | |
2525 | send_event: | |
b82b9183 | 2526 | err = team_nl_send_event_port_list_get(port->team); |
0c7517e9 JP |
2527 | if (err && err != -ESRCH) |
2528 | netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n", | |
2529 | port->dev->name, err); | |
3d249d4c JP |
2530 | |
2531 | } | |
2532 | ||
2533 | static void team_port_change_check(struct team_port *port, bool linkup) | |
2534 | { | |
2535 | struct team *team = port->team; | |
2536 | ||
61dc3461 | 2537 | mutex_lock(&team->lock); |
3d249d4c | 2538 | __team_port_change_check(port, linkup); |
61dc3461 | 2539 | mutex_unlock(&team->lock); |
3d249d4c JP |
2540 | } |
2541 | ||
0f1aad2b | 2542 | |
3d249d4c JP |
2543 | /************************************ |
2544 | * Net device notifier event handler | |
2545 | ************************************/ | |
2546 | ||
2547 | static int team_device_event(struct notifier_block *unused, | |
2548 | unsigned long event, void *ptr) | |
2549 | { | |
2550 | struct net_device *dev = (struct net_device *) ptr; | |
2551 | struct team_port *port; | |
2552 | ||
2553 | port = team_port_get_rtnl(dev); | |
2554 | if (!port) | |
2555 | return NOTIFY_DONE; | |
2556 | ||
2557 | switch (event) { | |
2558 | case NETDEV_UP: | |
2559 | if (netif_carrier_ok(dev)) | |
2560 | team_port_change_check(port, true); | |
2561 | case NETDEV_DOWN: | |
2562 | team_port_change_check(port, false); | |
2563 | case NETDEV_CHANGE: | |
2564 | if (netif_running(port->dev)) | |
2565 | team_port_change_check(port, | |
2566 | !!netif_carrier_ok(port->dev)); | |
2567 | break; | |
2568 | case NETDEV_UNREGISTER: | |
2569 | team_del_slave(port->team->dev, dev); | |
2570 | break; | |
2571 | case NETDEV_FEAT_CHANGE: | |
2572 | team_compute_features(port->team); | |
2573 | break; | |
2574 | case NETDEV_CHANGEMTU: | |
2575 | /* Forbid to change mtu of underlaying device */ | |
2576 | return NOTIFY_BAD; | |
2577 | case NETDEV_PRE_TYPE_CHANGE: | |
2578 | /* Forbid to change type of underlaying device */ | |
2579 | return NOTIFY_BAD; | |
2580 | } | |
2581 | return NOTIFY_DONE; | |
2582 | } | |
2583 | ||
2584 | static struct notifier_block team_notifier_block __read_mostly = { | |
2585 | .notifier_call = team_device_event, | |
2586 | }; | |
2587 | ||
2588 | ||
2589 | /*********************** | |
2590 | * Module init and exit | |
2591 | ***********************/ | |
2592 | ||
2593 | static int __init team_module_init(void) | |
2594 | { | |
2595 | int err; | |
2596 | ||
2597 | register_netdevice_notifier(&team_notifier_block); | |
2598 | ||
2599 | err = rtnl_link_register(&team_link_ops); | |
2600 | if (err) | |
2601 | goto err_rtnl_reg; | |
2602 | ||
2603 | err = team_nl_init(); | |
2604 | if (err) | |
2605 | goto err_nl_init; | |
2606 | ||
2607 | return 0; | |
2608 | ||
2609 | err_nl_init: | |
2610 | rtnl_link_unregister(&team_link_ops); | |
2611 | ||
2612 | err_rtnl_reg: | |
2613 | unregister_netdevice_notifier(&team_notifier_block); | |
2614 | ||
2615 | return err; | |
2616 | } | |
2617 | ||
2618 | static void __exit team_module_exit(void) | |
2619 | { | |
2620 | team_nl_fini(); | |
2621 | rtnl_link_unregister(&team_link_ops); | |
2622 | unregister_netdevice_notifier(&team_notifier_block); | |
2623 | } | |
2624 | ||
2625 | module_init(team_module_init); | |
2626 | module_exit(team_module_exit); | |
2627 | ||
2628 | MODULE_LICENSE("GPL v2"); | |
2629 | MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>"); | |
2630 | MODULE_DESCRIPTION("Ethernet team device driver"); | |
2631 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |