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