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