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