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