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