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,bool * tx_success)823 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb,
824 bool *tx_success)
825 {
826 struct list_head *qom_list;
827 struct team_port *port;
828
829 if (!team->queue_override_enabled || !skb->queue_mapping)
830 return false;
831 qom_list = __team_get_qom_list(team, skb->queue_mapping);
832 port = list_first_or_null_rcu(qom_list, struct team_port, qom_list);
833 if (!port)
834 return false;
835 *tx_success = !team_dev_queue_xmit(team, port, skb);
836 return true;
837 }
838
__team_queue_override_port_del(struct team * team,struct team_port * port)839 static void __team_queue_override_port_del(struct team *team,
840 struct team_port *port)
841 {
842 if (!port->queue_id)
843 return;
844 list_del_rcu(&port->qom_list);
845 }
846
team_queue_override_port_has_gt_prio_than(struct team_port * port,struct team_port * cur)847 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
848 struct team_port *cur)
849 {
850 if (port->priority < cur->priority)
851 return true;
852 if (port->priority > cur->priority)
853 return false;
854 if (port->tx_index < cur->tx_index)
855 return true;
856 return false;
857 }
858
__team_queue_override_port_add(struct team * team,struct team_port * port)859 static void __team_queue_override_port_add(struct team *team,
860 struct team_port *port)
861 {
862 struct team_port *cur;
863 struct list_head *qom_list;
864 struct list_head *node;
865
866 if (!port->queue_id)
867 return;
868 qom_list = __team_get_qom_list(team, port->queue_id);
869 node = qom_list;
870 list_for_each_entry(cur, qom_list, qom_list) {
871 if (team_queue_override_port_has_gt_prio_than(port, cur))
872 break;
873 node = &cur->qom_list;
874 }
875 list_add_tail_rcu(&port->qom_list, node);
876 }
877
__team_queue_override_enabled_check(struct team * team)878 static void __team_queue_override_enabled_check(struct team *team)
879 {
880 struct team_port *port;
881 bool enabled = false;
882
883 list_for_each_entry(port, &team->port_list, list) {
884 if (port->queue_id) {
885 enabled = true;
886 break;
887 }
888 }
889 if (enabled == team->queue_override_enabled)
890 return;
891 netdev_dbg(netdev_from_priv(team), "%s queue override\n",
892 enabled ? "Enabling" : "Disabling");
893 team->queue_override_enabled = enabled;
894 }
895
team_queue_override_port_prio_changed(struct team * team,struct team_port * port)896 static void team_queue_override_port_prio_changed(struct team *team,
897 struct team_port *port)
898 {
899 if (!port->queue_id || !team_port_tx_enabled(port))
900 return;
901 __team_queue_override_port_del(team, port);
902 __team_queue_override_port_add(team, port);
903 __team_queue_override_enabled_check(team);
904 }
905
team_queue_override_port_change_queue_id(struct team * team,struct team_port * port,u16 new_queue_id)906 static void team_queue_override_port_change_queue_id(struct team *team,
907 struct team_port *port,
908 u16 new_queue_id)
909 {
910 if (team_port_tx_enabled(port)) {
911 __team_queue_override_port_del(team, port);
912 port->queue_id = new_queue_id;
913 __team_queue_override_port_add(team, port);
914 __team_queue_override_enabled_check(team);
915 } else {
916 port->queue_id = new_queue_id;
917 }
918 }
919
team_queue_override_port_add(struct team * team,struct team_port * port)920 static void team_queue_override_port_add(struct team *team,
921 struct team_port *port)
922 {
923 __team_queue_override_port_add(team, port);
924 __team_queue_override_enabled_check(team);
925 }
926
team_queue_override_port_del(struct team * team,struct team_port * port)927 static void team_queue_override_port_del(struct team *team,
928 struct team_port *port)
929 {
930 __team_queue_override_port_del(team, port);
931 __team_queue_override_enabled_check(team);
932 }
933
934
935 /****************
936 * Port handling
937 ****************/
938
team_port_find(const struct team * team,const struct team_port * port)939 static bool team_port_find(const struct team *team,
940 const struct team_port *port)
941 {
942 struct team_port *cur;
943
944 list_for_each_entry(cur, &team->port_list, list)
945 if (cur == port)
946 return true;
947 return false;
948 }
949
__team_port_enable_rx(struct team * team,struct team_port * port)950 static void __team_port_enable_rx(struct team *team,
951 struct team_port *port)
952 {
953 team->rx_en_port_count++;
954 WRITE_ONCE(port->rx_enabled, true);
955 }
956
__team_port_disable_rx(struct team * team,struct team_port * port)957 static void __team_port_disable_rx(struct team *team,
958 struct team_port *port)
959 {
960 team->rx_en_port_count--;
961 WRITE_ONCE(port->rx_enabled, false);
962 }
963
team_port_enable_rx(struct team * team,struct team_port * port)964 static void team_port_enable_rx(struct team *team,
965 struct team_port *port)
966 {
967 if (team_port_rx_enabled(port))
968 return;
969
970 __team_port_enable_rx(team, port);
971 team_adjust_ops(team);
972 team_notify_peers(team);
973 team_mcast_rejoin(team);
974 }
975
team_port_disable_rx(struct team * team,struct team_port * port)976 static void team_port_disable_rx(struct team *team,
977 struct team_port *port)
978 {
979 if (!team_port_rx_enabled(port))
980 return;
981
982 __team_port_disable_rx(team, port);
983 team_adjust_ops(team);
984 }
985
986 /*
987 * Enable just TX on the port by adding to tx-enabled port hashlist and
988 * setting port->tx_index (Might be racy so reader could see incorrect
989 * ifindex when processing a flying packet, but that is not a problem).
990 * Write guarded by RTNL.
991 */
__team_port_enable_tx(struct team * team,struct team_port * port)992 static void __team_port_enable_tx(struct team *team,
993 struct team_port *port)
994 {
995 WRITE_ONCE(port->tx_index, team->tx_en_port_count);
996 WRITE_ONCE(team->tx_en_port_count, team->tx_en_port_count + 1);
997 hlist_add_head_rcu(&port->tx_hlist,
998 team_tx_port_index_hash(team, port->tx_index));
999 }
1000
team_port_enable_tx(struct team * team,struct team_port * port)1001 static void team_port_enable_tx(struct team *team,
1002 struct team_port *port)
1003 {
1004 if (team_port_tx_enabled(port))
1005 return;
1006
1007 __team_port_enable_tx(team, port);
1008 team_adjust_ops(team);
1009 team_queue_override_port_add(team, port);
1010
1011 /* Don't rejoin multicast, since this port might not be receiving. */
1012 team_notify_peers(team);
1013 team_lower_state_changed(port);
1014 }
1015
__reconstruct_port_hlist(struct team * team,int rm_index)1016 static void __reconstruct_port_hlist(struct team *team, int rm_index)
1017 {
1018 struct hlist_head *tx_port_index_hash;
1019 struct team_port *port;
1020 int i;
1021
1022 for (i = rm_index + 1; i < team->tx_en_port_count; i++) {
1023 port = team_get_port_by_tx_index(team, i);
1024 hlist_del_rcu(&port->tx_hlist);
1025 WRITE_ONCE(port->tx_index, port->tx_index - 1);
1026 tx_port_index_hash = team_tx_port_index_hash(team,
1027 port->tx_index);
1028 hlist_add_head_rcu(&port->tx_hlist, tx_port_index_hash);
1029 }
1030 }
1031
__team_port_disable_tx(struct team * team,struct team_port * port)1032 static void __team_port_disable_tx(struct team *team,
1033 struct team_port *port)
1034 {
1035 if (team->ops.port_tx_disabled)
1036 team->ops.port_tx_disabled(team, port);
1037
1038 hlist_del_rcu(&port->tx_hlist);
1039 __reconstruct_port_hlist(team, port->tx_index);
1040
1041 WRITE_ONCE(port->tx_index, -1);
1042 WRITE_ONCE(team->tx_en_port_count, team->tx_en_port_count - 1);
1043 }
1044
team_port_disable_tx(struct team * team,struct team_port * port)1045 static void team_port_disable_tx(struct team *team,
1046 struct team_port *port)
1047 {
1048 if (!team_port_tx_enabled(port))
1049 return;
1050
1051 __team_port_disable_tx(team, port);
1052
1053 team_queue_override_port_del(team, port);
1054 team_adjust_ops(team);
1055 team_lower_state_changed(port);
1056 }
1057
1058 /*
1059 * Enable TX AND RX on the port.
1060 */
team_port_enable(struct team * team,struct team_port * port)1061 static void team_port_enable(struct team *team,
1062 struct team_port *port)
1063 {
1064 bool rx_was_enabled;
1065 bool tx_was_enabled;
1066
1067 if (team_port_enabled(port))
1068 return;
1069
1070 rx_was_enabled = team_port_rx_enabled(port);
1071 tx_was_enabled = team_port_tx_enabled(port);
1072
1073 if (!rx_was_enabled)
1074 __team_port_enable_rx(team, port);
1075 if (!tx_was_enabled)
1076 __team_port_enable_tx(team, port);
1077
1078 team_adjust_ops(team);
1079 if (!tx_was_enabled)
1080 team_queue_override_port_add(team, port);
1081 team_notify_peers(team);
1082 if (!rx_was_enabled)
1083 team_mcast_rejoin(team);
1084 if (!tx_was_enabled)
1085 team_lower_state_changed(port);
1086 }
1087
team_port_disable(struct team * team,struct team_port * port)1088 static void team_port_disable(struct team *team,
1089 struct team_port *port)
1090 {
1091 bool rx_was_enabled = team_port_rx_enabled(port);
1092 bool tx_was_enabled = team_port_tx_enabled(port);
1093
1094 if (!tx_was_enabled && !rx_was_enabled)
1095 return;
1096
1097 if (tx_was_enabled) {
1098 __team_port_disable_tx(team, port);
1099 team_queue_override_port_del(team, port);
1100 }
1101 if (rx_was_enabled)
1102 __team_port_disable_rx(team, port);
1103
1104 team_adjust_ops(team);
1105
1106 if (tx_was_enabled)
1107 team_lower_state_changed(port);
1108 }
1109
team_port_enter(struct team * team,struct team_port * port)1110 static int team_port_enter(struct team *team, struct team_port *port)
1111 {
1112 int err = 0;
1113
1114 dev_hold(netdev_from_priv(team));
1115 if (team->ops.port_enter) {
1116 err = team->ops.port_enter(team, port);
1117 if (err) {
1118 netdev_err(netdev_from_priv(team),
1119 "Device %s failed to enter team mode\n",
1120 port->dev->name);
1121 goto err_port_enter;
1122 }
1123 }
1124
1125 return 0;
1126
1127 err_port_enter:
1128 dev_put(netdev_from_priv(team));
1129
1130 return err;
1131 }
1132
team_port_leave(struct team * team,struct team_port * port)1133 static void team_port_leave(struct team *team, struct team_port *port)
1134 {
1135 if (team->ops.port_leave)
1136 team->ops.port_leave(team, port);
1137 dev_put(netdev_from_priv(team));
1138 }
1139
1140 #ifdef CONFIG_NET_POLL_CONTROLLER
__team_port_enable_netpoll(struct team_port * port)1141 static int __team_port_enable_netpoll(struct team_port *port)
1142 {
1143 struct netpoll *np;
1144 int err;
1145
1146 np = kzalloc_obj(*np);
1147 if (!np)
1148 return -ENOMEM;
1149
1150 err = __netpoll_setup(np, port->dev);
1151 if (err) {
1152 kfree(np);
1153 return err;
1154 }
1155 port->np = np;
1156 return err;
1157 }
1158
team_port_enable_netpoll(struct team_port * port)1159 static int team_port_enable_netpoll(struct team_port *port)
1160 {
1161 if (!netdev_from_priv(port->team)->npinfo)
1162 return 0;
1163
1164 return __team_port_enable_netpoll(port);
1165 }
1166
team_port_disable_netpoll(struct team_port * port)1167 static void team_port_disable_netpoll(struct team_port *port)
1168 {
1169 struct netpoll *np = port->np;
1170
1171 if (!np)
1172 return;
1173 port->np = NULL;
1174
1175 __netpoll_free(np);
1176 }
1177 #else
team_port_enable_netpoll(struct team_port * port)1178 static int team_port_enable_netpoll(struct team_port *port)
1179 {
1180 return 0;
1181 }
team_port_disable_netpoll(struct team_port * port)1182 static void team_port_disable_netpoll(struct team_port *port)
1183 {
1184 }
1185 #endif
1186
team_upper_dev_link(struct team * team,struct team_port * port,struct netlink_ext_ack * extack)1187 static int team_upper_dev_link(struct team *team, struct team_port *port,
1188 struct netlink_ext_ack *extack)
1189 {
1190 struct netdev_lag_upper_info lag_upper_info;
1191 int err;
1192
1193 lag_upper_info.tx_type = team->mode->lag_tx_type;
1194 lag_upper_info.hash_type = NETDEV_LAG_HASH_UNKNOWN;
1195 err = netdev_master_upper_dev_link(port->dev, netdev_from_priv(team),
1196 NULL, &lag_upper_info, extack);
1197 if (err)
1198 return err;
1199 port->dev->priv_flags |= IFF_TEAM_PORT;
1200 return 0;
1201 }
1202
team_upper_dev_unlink(struct team * team,struct team_port * port)1203 static void team_upper_dev_unlink(struct team *team, struct team_port *port)
1204 {
1205 netdev_upper_dev_unlink(port->dev, netdev_from_priv(team));
1206 port->dev->priv_flags &= ~IFF_TEAM_PORT;
1207 }
1208
1209 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1210 static int team_dev_type_check_change(struct net_device *dev,
1211 struct net_device *port_dev);
1212
team_port_add(struct team * team,struct net_device * port_dev,struct netlink_ext_ack * extack)1213 static int team_port_add(struct team *team, struct net_device *port_dev,
1214 struct netlink_ext_ack *extack)
1215 {
1216 struct net_device *dev = netdev_from_priv(team);
1217 struct team_port *port;
1218 char *portname = port_dev->name;
1219 int err;
1220
1221 if (port_dev->flags & IFF_LOOPBACK) {
1222 NL_SET_ERR_MSG(extack, "Loopback device can't be added as a team port");
1223 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1224 portname);
1225 return -EINVAL;
1226 }
1227
1228 if (netif_is_team_port(port_dev)) {
1229 NL_SET_ERR_MSG(extack, "Device is already a port of a team device");
1230 netdev_err(dev, "Device %s is already a port "
1231 "of a team device\n", portname);
1232 return -EBUSY;
1233 }
1234
1235 if (dev == port_dev) {
1236 NL_SET_ERR_MSG(extack, "Cannot enslave team device to itself");
1237 netdev_err(dev, "Cannot enslave team device to itself\n");
1238 return -EINVAL;
1239 }
1240
1241 if (netdev_has_upper_dev(dev, port_dev)) {
1242 NL_SET_ERR_MSG(extack, "Device is already an upper device of the team interface");
1243 netdev_err(dev, "Device %s is already an upper device of the team interface\n",
1244 portname);
1245 return -EBUSY;
1246 }
1247
1248 if (netdev_has_upper_dev(port_dev, dev)) {
1249 NL_SET_ERR_MSG(extack, "Device is already a lower device of the team interface");
1250 netdev_err(dev, "Device %s is already a lower device of the team interface\n",
1251 portname);
1252 return -EBUSY;
1253 }
1254
1255 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1256 vlan_uses_dev(dev)) {
1257 NL_SET_ERR_MSG(extack, "Device is VLAN challenged and team device has VLAN set up");
1258 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1259 portname);
1260 return -EPERM;
1261 }
1262
1263 if (port_dev->flags & IFF_UP) {
1264 NL_SET_ERR_MSG(extack, "Device is up. Set it down before adding it as a team port");
1265 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1266 portname);
1267 return -EBUSY;
1268 }
1269
1270 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1271 GFP_KERNEL);
1272 if (!port)
1273 return -ENOMEM;
1274
1275 port->dev = port_dev;
1276 port->team = team;
1277 INIT_LIST_HEAD(&port->qom_list);
1278
1279 port->orig.mtu = port_dev->mtu;
1280 /*
1281 * MTU assignment will be handled in team_dev_type_check_change
1282 * if dev and port_dev are of different types
1283 */
1284 if (dev->type == port_dev->type) {
1285 err = dev_set_mtu(port_dev, dev->mtu);
1286 if (err) {
1287 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1288 goto err_set_mtu;
1289 }
1290 }
1291
1292 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1293
1294 err = team_port_enter(team, port);
1295 if (err) {
1296 netdev_err(dev, "Device %s failed to enter team mode\n",
1297 portname);
1298 goto err_port_enter;
1299 }
1300
1301 err = dev_open(port_dev, extack);
1302 if (err) {
1303 netdev_dbg(dev, "Device %s opening failed\n",
1304 portname);
1305 goto err_dev_open;
1306 }
1307
1308 err = vlan_vids_add_by_dev(port_dev, dev);
1309 if (err) {
1310 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1311 portname);
1312 goto err_vids_add;
1313 }
1314
1315 err = team_port_enable_netpoll(port);
1316 if (err) {
1317 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1318 portname);
1319 goto err_enable_netpoll;
1320 }
1321
1322 if (!(dev->features & NETIF_F_LRO))
1323 dev_disable_lro(port_dev);
1324
1325 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1326 port);
1327 if (err) {
1328 netdev_err(dev, "Device %s failed to register rx_handler\n",
1329 portname);
1330 goto err_handler_register;
1331 }
1332
1333 err = team_upper_dev_link(team, port, extack);
1334 if (err) {
1335 netdev_err(dev, "Device %s failed to set upper link\n",
1336 portname);
1337 goto err_set_upper_link;
1338 }
1339
1340 err = __team_option_inst_add_port(team, port);
1341 if (err) {
1342 netdev_err(dev, "Device %s failed to add per-port options\n",
1343 portname);
1344 goto err_option_port_add;
1345 }
1346
1347 /* set promiscuity level to new slave */
1348 if (dev->flags & IFF_PROMISC) {
1349 err = dev_set_promiscuity(port_dev, 1);
1350 if (err)
1351 goto err_set_slave_promisc;
1352 }
1353
1354 /* set allmulti level to new slave */
1355 if (dev->flags & IFF_ALLMULTI) {
1356 err = dev_set_allmulti(port_dev, 1);
1357 if (err) {
1358 if (dev->flags & IFF_PROMISC)
1359 dev_set_promiscuity(port_dev, -1);
1360 goto err_set_slave_allmulti;
1361 }
1362 }
1363
1364 err = team_dev_type_check_change(dev, port_dev);
1365 if (err)
1366 goto err_set_dev_type;
1367
1368 if (dev->flags & IFF_UP) {
1369 netif_addr_lock_bh(dev);
1370 dev_uc_sync_multiple(port_dev, dev);
1371 dev_mc_sync_multiple(port_dev, dev);
1372 netif_addr_unlock_bh(dev);
1373 }
1374
1375 WRITE_ONCE(port->tx_index, -1);
1376 list_add_tail_rcu(&port->list, &team->port_list);
1377 team_port_enable(team, port);
1378 netdev_compute_master_upper_features(dev, true);
1379 netdev_lock_ops(port_dev);
1380 __team_port_change_port_added(port, !!netif_oper_up(port_dev));
1381 netdev_unlock_ops(port_dev);
1382 __team_options_change_check(team);
1383
1384 netdev_info(dev, "Port device %s added\n", portname);
1385
1386 return 0;
1387
1388 err_set_dev_type:
1389 err_set_slave_allmulti:
1390 err_set_slave_promisc:
1391 __team_option_inst_del_port(team, port);
1392
1393 err_option_port_add:
1394 team_upper_dev_unlink(team, port);
1395
1396 err_set_upper_link:
1397 netdev_rx_handler_unregister(port_dev);
1398
1399 err_handler_register:
1400 team_port_disable_netpoll(port);
1401
1402 err_enable_netpoll:
1403 vlan_vids_del_by_dev(port_dev, dev);
1404
1405 err_vids_add:
1406 dev_close(port_dev);
1407
1408 err_dev_open:
1409 team_port_leave(team, port);
1410 team_port_set_orig_dev_addr(port);
1411
1412 err_port_enter:
1413 dev_set_mtu(port_dev, port->orig.mtu);
1414
1415 err_set_mtu:
1416 kfree(port);
1417
1418 return err;
1419 }
1420
1421 static void __team_port_change_port_removed(struct team_port *port);
1422
team_port_del(struct team * team,struct net_device * port_dev,bool unregister)1423 static int team_port_del(struct team *team, struct net_device *port_dev, bool unregister)
1424 {
1425 struct net_device *dev = netdev_from_priv(team);
1426 struct team_port *port;
1427 char *portname = port_dev->name;
1428
1429 port = team_port_get_rtnl(port_dev);
1430 if (!port || !team_port_find(team, port)) {
1431 netdev_err(dev, "Device %s does not act as a port of this team\n",
1432 portname);
1433 return -ENOENT;
1434 }
1435
1436 team_port_disable(team, port);
1437 list_del_rcu(&port->list);
1438
1439 if (dev->flags & IFF_PROMISC)
1440 dev_set_promiscuity(port_dev, -1);
1441 if (dev->flags & IFF_ALLMULTI)
1442 dev_set_allmulti(port_dev, -1);
1443
1444 team_upper_dev_unlink(team, port);
1445 netdev_rx_handler_unregister(port_dev);
1446 team_port_disable_netpoll(port);
1447 vlan_vids_del_by_dev(port_dev, dev);
1448 if (dev->flags & IFF_UP) {
1449 dev_uc_unsync(port_dev, dev);
1450 dev_mc_unsync(port_dev, dev);
1451 }
1452 dev_close(port_dev);
1453 team_port_leave(team, port);
1454
1455 __team_option_inst_mark_removed_port(team, port);
1456 __team_options_change_check(team);
1457 __team_option_inst_del_port(team, port);
1458 __team_port_change_port_removed(port);
1459
1460 team_port_set_orig_dev_addr(port);
1461 if (unregister) {
1462 netdev_lock_ops(port_dev);
1463 __netif_set_mtu(port_dev, port->orig.mtu);
1464 netdev_unlock_ops(port_dev);
1465 } else {
1466 dev_set_mtu(port_dev, port->orig.mtu);
1467 }
1468 kfree_rcu(port, rcu);
1469 netdev_info(dev, "Port device %s removed\n", portname);
1470 netdev_compute_master_upper_features(dev, true);
1471
1472 return 0;
1473 }
1474
1475
1476 /*****************
1477 * Net device ops
1478 *****************/
1479
team_mode_option_get(struct team * team,struct team_gsetter_ctx * ctx)1480 static void team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1481 {
1482 ctx->data.str_val = team->mode->kind;
1483 }
1484
team_mode_option_set(struct team * team,struct team_gsetter_ctx * ctx)1485 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1486 {
1487 return team_change_mode(team, ctx->data.str_val);
1488 }
1489
team_notify_peers_count_get(struct team * team,struct team_gsetter_ctx * ctx)1490 static void team_notify_peers_count_get(struct team *team,
1491 struct team_gsetter_ctx *ctx)
1492 {
1493 ctx->data.u32_val = team->notify_peers.count;
1494 }
1495
team_notify_peers_count_set(struct team * team,struct team_gsetter_ctx * ctx)1496 static int team_notify_peers_count_set(struct team *team,
1497 struct team_gsetter_ctx *ctx)
1498 {
1499 team->notify_peers.count = ctx->data.u32_val;
1500 return 0;
1501 }
1502
team_notify_peers_interval_get(struct team * team,struct team_gsetter_ctx * ctx)1503 static void team_notify_peers_interval_get(struct team *team,
1504 struct team_gsetter_ctx *ctx)
1505 {
1506 ctx->data.u32_val = team->notify_peers.interval;
1507 }
1508
team_notify_peers_interval_set(struct team * team,struct team_gsetter_ctx * ctx)1509 static int team_notify_peers_interval_set(struct team *team,
1510 struct team_gsetter_ctx *ctx)
1511 {
1512 team->notify_peers.interval = ctx->data.u32_val;
1513 return 0;
1514 }
1515
team_mcast_rejoin_count_get(struct team * team,struct team_gsetter_ctx * ctx)1516 static void team_mcast_rejoin_count_get(struct team *team,
1517 struct team_gsetter_ctx *ctx)
1518 {
1519 ctx->data.u32_val = team->mcast_rejoin.count;
1520 }
1521
team_mcast_rejoin_count_set(struct team * team,struct team_gsetter_ctx * ctx)1522 static int team_mcast_rejoin_count_set(struct team *team,
1523 struct team_gsetter_ctx *ctx)
1524 {
1525 team->mcast_rejoin.count = ctx->data.u32_val;
1526 return 0;
1527 }
1528
team_mcast_rejoin_interval_get(struct team * team,struct team_gsetter_ctx * ctx)1529 static void team_mcast_rejoin_interval_get(struct team *team,
1530 struct team_gsetter_ctx *ctx)
1531 {
1532 ctx->data.u32_val = team->mcast_rejoin.interval;
1533 }
1534
team_mcast_rejoin_interval_set(struct team * team,struct team_gsetter_ctx * ctx)1535 static int team_mcast_rejoin_interval_set(struct team *team,
1536 struct team_gsetter_ctx *ctx)
1537 {
1538 team->mcast_rejoin.interval = ctx->data.u32_val;
1539 return 0;
1540 }
1541
team_port_en_option_get(struct team * team,struct team_gsetter_ctx * ctx)1542 static void team_port_en_option_get(struct team *team,
1543 struct team_gsetter_ctx *ctx)
1544 {
1545 struct team_port *port = ctx->info->port;
1546
1547 ctx->data.bool_val = team_port_enabled(port);
1548 }
1549
team_port_en_option_set(struct team * team,struct team_gsetter_ctx * ctx)1550 static int team_port_en_option_set(struct team *team,
1551 struct team_gsetter_ctx *ctx)
1552 {
1553 struct team_port *port = ctx->info->port;
1554
1555 if (ctx->data.bool_val)
1556 team_port_enable(team, port);
1557 else
1558 team_port_disable(team, port);
1559 return 0;
1560 }
1561
team_port_rx_en_option_get(struct team * team,struct team_gsetter_ctx * ctx)1562 static void team_port_rx_en_option_get(struct team *team,
1563 struct team_gsetter_ctx *ctx)
1564 {
1565 struct team_port *port = ctx->info->port;
1566
1567 ctx->data.bool_val = team_port_rx_enabled(port);
1568 }
1569
team_port_rx_en_option_set(struct team * team,struct team_gsetter_ctx * ctx)1570 static int team_port_rx_en_option_set(struct team *team,
1571 struct team_gsetter_ctx *ctx)
1572 {
1573 struct team_port *port = ctx->info->port;
1574
1575 if (ctx->data.bool_val)
1576 team_port_enable_rx(team, port);
1577 else
1578 team_port_disable_rx(team, port);
1579 return 0;
1580 }
1581
team_port_tx_en_option_get(struct team * team,struct team_gsetter_ctx * ctx)1582 static void team_port_tx_en_option_get(struct team *team,
1583 struct team_gsetter_ctx *ctx)
1584 {
1585 struct team_port *port = ctx->info->port;
1586
1587 ctx->data.bool_val = team_port_tx_enabled(port);
1588 }
1589
team_port_tx_en_option_set(struct team * team,struct team_gsetter_ctx * ctx)1590 static int team_port_tx_en_option_set(struct team *team,
1591 struct team_gsetter_ctx *ctx)
1592 {
1593 struct team_port *port = ctx->info->port;
1594
1595 if (ctx->data.bool_val)
1596 team_port_enable_tx(team, port);
1597 else
1598 team_port_disable_tx(team, port);
1599 return 0;
1600 }
1601
team_user_linkup_option_get(struct team * team,struct team_gsetter_ctx * ctx)1602 static void team_user_linkup_option_get(struct team *team,
1603 struct team_gsetter_ctx *ctx)
1604 {
1605 struct team_port *port = ctx->info->port;
1606
1607 ctx->data.bool_val = port->user.linkup;
1608 }
1609
1610 static void __team_carrier_check(struct team *team);
1611
team_user_linkup_option_set(struct team * team,struct team_gsetter_ctx * ctx)1612 static int team_user_linkup_option_set(struct team *team,
1613 struct team_gsetter_ctx *ctx)
1614 {
1615 struct team_port *port = ctx->info->port;
1616
1617 port->user.linkup = ctx->data.bool_val;
1618 team_refresh_port_linkup(port);
1619 __team_carrier_check(port->team);
1620 return 0;
1621 }
1622
team_user_linkup_en_option_get(struct team * team,struct team_gsetter_ctx * ctx)1623 static void team_user_linkup_en_option_get(struct team *team,
1624 struct team_gsetter_ctx *ctx)
1625 {
1626 struct team_port *port = ctx->info->port;
1627
1628 ctx->data.bool_val = port->user.linkup_enabled;
1629 }
1630
team_user_linkup_en_option_set(struct team * team,struct team_gsetter_ctx * ctx)1631 static int team_user_linkup_en_option_set(struct team *team,
1632 struct team_gsetter_ctx *ctx)
1633 {
1634 struct team_port *port = ctx->info->port;
1635
1636 port->user.linkup_enabled = ctx->data.bool_val;
1637 team_refresh_port_linkup(port);
1638 __team_carrier_check(port->team);
1639 return 0;
1640 }
1641
team_priority_option_get(struct team * team,struct team_gsetter_ctx * ctx)1642 static void team_priority_option_get(struct team *team,
1643 struct team_gsetter_ctx *ctx)
1644 {
1645 struct team_port *port = ctx->info->port;
1646
1647 ctx->data.s32_val = port->priority;
1648 }
1649
team_priority_option_set(struct team * team,struct team_gsetter_ctx * ctx)1650 static int team_priority_option_set(struct team *team,
1651 struct team_gsetter_ctx *ctx)
1652 {
1653 struct team_port *port = ctx->info->port;
1654 s32 priority = ctx->data.s32_val;
1655
1656 if (port->priority == priority)
1657 return 0;
1658 port->priority = priority;
1659 team_queue_override_port_prio_changed(team, port);
1660 return 0;
1661 }
1662
team_queue_id_option_get(struct team * team,struct team_gsetter_ctx * ctx)1663 static void team_queue_id_option_get(struct team *team,
1664 struct team_gsetter_ctx *ctx)
1665 {
1666 struct team_port *port = ctx->info->port;
1667
1668 ctx->data.u32_val = port->queue_id;
1669 }
1670
team_queue_id_option_set(struct team * team,struct team_gsetter_ctx * ctx)1671 static int team_queue_id_option_set(struct team *team,
1672 struct team_gsetter_ctx *ctx)
1673 {
1674 struct team_port *port = ctx->info->port;
1675 u16 new_queue_id = ctx->data.u32_val;
1676
1677 if (port->queue_id == new_queue_id)
1678 return 0;
1679 if (new_queue_id >= netdev_from_priv(team)->real_num_tx_queues)
1680 return -EINVAL;
1681 team_queue_override_port_change_queue_id(team, port, new_queue_id);
1682 return 0;
1683 }
1684
1685 static const struct team_option team_options[] = {
1686 {
1687 .name = "mode",
1688 .type = TEAM_OPTION_TYPE_STRING,
1689 .getter = team_mode_option_get,
1690 .setter = team_mode_option_set,
1691 },
1692 {
1693 .name = "notify_peers_count",
1694 .type = TEAM_OPTION_TYPE_U32,
1695 .getter = team_notify_peers_count_get,
1696 .setter = team_notify_peers_count_set,
1697 },
1698 {
1699 .name = "notify_peers_interval",
1700 .type = TEAM_OPTION_TYPE_U32,
1701 .getter = team_notify_peers_interval_get,
1702 .setter = team_notify_peers_interval_set,
1703 },
1704 {
1705 .name = "mcast_rejoin_count",
1706 .type = TEAM_OPTION_TYPE_U32,
1707 .getter = team_mcast_rejoin_count_get,
1708 .setter = team_mcast_rejoin_count_set,
1709 },
1710 {
1711 .name = "mcast_rejoin_interval",
1712 .type = TEAM_OPTION_TYPE_U32,
1713 .getter = team_mcast_rejoin_interval_get,
1714 .setter = team_mcast_rejoin_interval_set,
1715 },
1716 {
1717 .name = "enabled",
1718 .type = TEAM_OPTION_TYPE_BOOL,
1719 .per_port = true,
1720 .getter = team_port_en_option_get,
1721 .setter = team_port_en_option_set,
1722 },
1723 {
1724 .name = "rx_enabled",
1725 .type = TEAM_OPTION_TYPE_BOOL,
1726 .per_port = true,
1727 .getter = team_port_rx_en_option_get,
1728 .setter = team_port_rx_en_option_set,
1729 },
1730 {
1731 .name = "tx_enabled",
1732 .type = TEAM_OPTION_TYPE_BOOL,
1733 .per_port = true,
1734 .getter = team_port_tx_en_option_get,
1735 .setter = team_port_tx_en_option_set,
1736 },
1737 {
1738 .name = "user_linkup",
1739 .type = TEAM_OPTION_TYPE_BOOL,
1740 .per_port = true,
1741 .getter = team_user_linkup_option_get,
1742 .setter = team_user_linkup_option_set,
1743 },
1744 {
1745 .name = "user_linkup_enabled",
1746 .type = TEAM_OPTION_TYPE_BOOL,
1747 .per_port = true,
1748 .getter = team_user_linkup_en_option_get,
1749 .setter = team_user_linkup_en_option_set,
1750 },
1751 {
1752 .name = "priority",
1753 .type = TEAM_OPTION_TYPE_S32,
1754 .per_port = true,
1755 .getter = team_priority_option_get,
1756 .setter = team_priority_option_set,
1757 },
1758 {
1759 .name = "queue_id",
1760 .type = TEAM_OPTION_TYPE_U32,
1761 .per_port = true,
1762 .getter = team_queue_id_option_get,
1763 .setter = team_queue_id_option_set,
1764 },
1765 };
1766
1767
team_init(struct net_device * dev)1768 static int team_init(struct net_device *dev)
1769 {
1770 struct team *team = netdev_priv(dev);
1771 int i;
1772 int err;
1773
1774 team_set_no_mode(team);
1775 team->notifier_ctx = false;
1776
1777 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1778 if (!team->pcpu_stats)
1779 return -ENOMEM;
1780
1781 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1782 INIT_HLIST_HEAD(&team->tx_en_port_hlist[i]);
1783 INIT_LIST_HEAD(&team->port_list);
1784 err = team_queue_override_init(team);
1785 if (err)
1786 goto err_team_queue_override_init;
1787
1788 team_adjust_ops(team);
1789
1790 INIT_LIST_HEAD(&team->option_list);
1791 INIT_LIST_HEAD(&team->option_inst_list);
1792
1793 team_notify_peers_init(team);
1794 team_mcast_rejoin_init(team);
1795
1796 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1797 if (err)
1798 goto err_options_register;
1799 netif_carrier_off(dev);
1800
1801 netdev_lockdep_set_classes(dev);
1802
1803 return 0;
1804
1805 err_options_register:
1806 team_mcast_rejoin_fini(team);
1807 team_notify_peers_fini(team);
1808 team_queue_override_fini(team);
1809 err_team_queue_override_init:
1810 free_percpu(team->pcpu_stats);
1811
1812 return err;
1813 }
1814
team_uninit(struct net_device * dev)1815 static void team_uninit(struct net_device *dev)
1816 {
1817 struct team *team = netdev_priv(dev);
1818 struct team_port *port;
1819 struct team_port *tmp;
1820
1821 ASSERT_RTNL();
1822
1823 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1824 team_port_del(team, port->dev, false);
1825
1826 __team_change_mode(team, NULL); /* cleanup */
1827 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1828 team_mcast_rejoin_fini(team);
1829 team_notify_peers_fini(team);
1830 team_queue_override_fini(team);
1831 netdev_change_features(dev);
1832 }
1833
team_destructor(struct net_device * dev)1834 static void team_destructor(struct net_device *dev)
1835 {
1836 struct team *team = netdev_priv(dev);
1837
1838 free_percpu(team->pcpu_stats);
1839 }
1840
team_open(struct net_device * dev)1841 static int team_open(struct net_device *dev)
1842 {
1843 return 0;
1844 }
1845
team_close(struct net_device * dev)1846 static int team_close(struct net_device *dev)
1847 {
1848 struct team *team = netdev_priv(dev);
1849 struct team_port *port;
1850
1851 list_for_each_entry(port, &team->port_list, list) {
1852 dev_uc_unsync(port->dev, dev);
1853 dev_mc_unsync(port->dev, dev);
1854 }
1855
1856 return 0;
1857 }
1858
1859 /*
1860 * note: already called with rcu_read_lock
1861 */
team_xmit(struct sk_buff * skb,struct net_device * dev)1862 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1863 {
1864 struct team *team = netdev_priv(dev);
1865 bool tx_success;
1866 unsigned int len = skb->len;
1867
1868 if (!team_queue_override_transmit(team, skb, &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