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