2 * drivers/net/team/team.c - Network team device driver
3 * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
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.
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>
35 #define DRV_NAME "team"
42 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
44 static struct team_port *team_port_get_rcu(const struct net_device *dev)
46 return rcu_dereference(dev->rx_handler_data);
49 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
51 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
53 return team_port_exists(dev) ? port : NULL;
57 * Since the ability to change device address for open port device is tested in
58 * team_port_add, this function can be called without control of return value
60 static int __set_port_dev_addr(struct net_device *port_dev,
61 const unsigned char *dev_addr)
65 memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
66 addr.sa_family = port_dev->type;
67 return dev_set_mac_address(port_dev, &addr);
70 static int team_port_set_orig_dev_addr(struct team_port *port)
72 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
75 static int team_port_set_team_dev_addr(struct team *team,
76 struct team_port *port)
78 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
81 int team_modeop_port_enter(struct team *team, struct team_port *port)
83 return team_port_set_team_dev_addr(team, port);
85 EXPORT_SYMBOL(team_modeop_port_enter);
87 void team_modeop_port_change_dev_addr(struct team *team,
88 struct team_port *port)
90 team_port_set_team_dev_addr(team, port);
92 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
94 static void team_refresh_port_linkup(struct team_port *port)
96 port->linkup = port->user.linkup_enabled ? port->user.linkup :
105 struct team_option_inst { /* One for each option instance */
106 struct list_head list;
107 struct list_head tmp_list;
108 struct team_option *option;
109 struct team_option_inst_info info;
114 static struct team_option *__team_find_option(struct team *team,
115 const char *opt_name)
117 struct team_option *option;
119 list_for_each_entry(option, &team->option_list, list) {
120 if (strcmp(option->name, opt_name) == 0)
126 static void __team_option_inst_del(struct team_option_inst *opt_inst)
128 list_del(&opt_inst->list);
132 static void __team_option_inst_del_option(struct team *team,
133 struct team_option *option)
135 struct team_option_inst *opt_inst, *tmp;
137 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
138 if (opt_inst->option == option)
139 __team_option_inst_del(opt_inst);
143 static int __team_option_inst_add(struct team *team, struct team_option *option,
144 struct team_port *port)
146 struct team_option_inst *opt_inst;
147 unsigned int array_size;
151 array_size = option->array_size;
153 array_size = 1; /* No array but still need one instance */
155 for (i = 0; i < array_size; i++) {
156 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
159 opt_inst->option = option;
160 opt_inst->info.port = port;
161 opt_inst->info.array_index = i;
162 opt_inst->changed = true;
163 opt_inst->removed = false;
164 list_add_tail(&opt_inst->list, &team->option_inst_list);
166 err = option->init(team, &opt_inst->info);
175 static int __team_option_inst_add_option(struct team *team,
176 struct team_option *option)
180 if (!option->per_port) {
181 err = __team_option_inst_add(team, option, NULL);
183 goto inst_del_option;
188 __team_option_inst_del_option(team, option);
192 static void __team_option_inst_mark_removed_option(struct team *team,
193 struct team_option *option)
195 struct team_option_inst *opt_inst;
197 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
198 if (opt_inst->option == option) {
199 opt_inst->changed = true;
200 opt_inst->removed = true;
205 static void __team_option_inst_del_port(struct team *team,
206 struct team_port *port)
208 struct team_option_inst *opt_inst, *tmp;
210 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
211 if (opt_inst->option->per_port &&
212 opt_inst->info.port == port)
213 __team_option_inst_del(opt_inst);
217 static int __team_option_inst_add_port(struct team *team,
218 struct team_port *port)
220 struct team_option *option;
223 list_for_each_entry(option, &team->option_list, list) {
224 if (!option->per_port)
226 err = __team_option_inst_add(team, option, port);
233 __team_option_inst_del_port(team, port);
237 static void __team_option_inst_mark_removed_port(struct team *team,
238 struct team_port *port)
240 struct team_option_inst *opt_inst;
242 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
243 if (opt_inst->info.port == port) {
244 opt_inst->changed = true;
245 opt_inst->removed = true;
250 static int __team_options_register(struct team *team,
251 const struct team_option *option,
255 struct team_option **dst_opts;
258 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
262 for (i = 0; i < option_count; i++, option++) {
263 if (__team_find_option(team, option->name)) {
267 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
274 for (i = 0; i < option_count; i++) {
275 err = __team_option_inst_add_option(team, dst_opts[i]);
278 list_add_tail(&dst_opts[i]->list, &team->option_list);
285 for (i--; i >= 0; i--)
286 __team_option_inst_del_option(team, dst_opts[i]);
288 i = option_count - 1;
290 for (i--; i >= 0; i--)
297 static void __team_options_mark_removed(struct team *team,
298 const struct team_option *option,
303 for (i = 0; i < option_count; i++, option++) {
304 struct team_option *del_opt;
306 del_opt = __team_find_option(team, option->name);
308 __team_option_inst_mark_removed_option(team, del_opt);
312 static void __team_options_unregister(struct team *team,
313 const struct team_option *option,
318 for (i = 0; i < option_count; i++, option++) {
319 struct team_option *del_opt;
321 del_opt = __team_find_option(team, option->name);
323 __team_option_inst_del_option(team, del_opt);
324 list_del(&del_opt->list);
330 static void __team_options_change_check(struct team *team);
332 int team_options_register(struct team *team,
333 const struct team_option *option,
338 err = __team_options_register(team, option, option_count);
341 __team_options_change_check(team);
344 EXPORT_SYMBOL(team_options_register);
346 void team_options_unregister(struct team *team,
347 const struct team_option *option,
350 __team_options_mark_removed(team, option, option_count);
351 __team_options_change_check(team);
352 __team_options_unregister(team, option, option_count);
354 EXPORT_SYMBOL(team_options_unregister);
356 static int team_option_get(struct team *team,
357 struct team_option_inst *opt_inst,
358 struct team_gsetter_ctx *ctx)
360 if (!opt_inst->option->getter)
362 return opt_inst->option->getter(team, ctx);
365 static int team_option_set(struct team *team,
366 struct team_option_inst *opt_inst,
367 struct team_gsetter_ctx *ctx)
369 if (!opt_inst->option->setter)
371 return opt_inst->option->setter(team, ctx);
374 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
376 struct team_option_inst *opt_inst;
378 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
379 opt_inst->changed = true;
381 EXPORT_SYMBOL(team_option_inst_set_change);
383 void team_options_change_check(struct team *team)
385 __team_options_change_check(team);
387 EXPORT_SYMBOL(team_options_change_check);
394 static LIST_HEAD(mode_list);
395 static DEFINE_SPINLOCK(mode_list_lock);
397 struct team_mode_item {
398 struct list_head list;
399 const struct team_mode *mode;
402 static struct team_mode_item *__find_mode(const char *kind)
404 struct team_mode_item *mitem;
406 list_for_each_entry(mitem, &mode_list, list) {
407 if (strcmp(mitem->mode->kind, kind) == 0)
413 static bool is_good_mode_name(const char *name)
415 while (*name != '\0') {
416 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
423 int team_mode_register(const struct team_mode *mode)
426 struct team_mode_item *mitem;
428 if (!is_good_mode_name(mode->kind) ||
429 mode->priv_size > TEAM_MODE_PRIV_SIZE)
432 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
436 spin_lock(&mode_list_lock);
437 if (__find_mode(mode->kind)) {
443 list_add_tail(&mitem->list, &mode_list);
445 spin_unlock(&mode_list_lock);
448 EXPORT_SYMBOL(team_mode_register);
450 void team_mode_unregister(const struct team_mode *mode)
452 struct team_mode_item *mitem;
454 spin_lock(&mode_list_lock);
455 mitem = __find_mode(mode->kind);
457 list_del_init(&mitem->list);
460 spin_unlock(&mode_list_lock);
462 EXPORT_SYMBOL(team_mode_unregister);
464 static const struct team_mode *team_mode_get(const char *kind)
466 struct team_mode_item *mitem;
467 const struct team_mode *mode = NULL;
469 spin_lock(&mode_list_lock);
470 mitem = __find_mode(kind);
472 spin_unlock(&mode_list_lock);
473 request_module("team-mode-%s", kind);
474 spin_lock(&mode_list_lock);
475 mitem = __find_mode(kind);
479 if (!try_module_get(mode->owner))
483 spin_unlock(&mode_list_lock);
487 static void team_mode_put(const struct team_mode *mode)
489 module_put(mode->owner);
492 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
494 dev_kfree_skb_any(skb);
498 static rx_handler_result_t team_dummy_receive(struct team *team,
499 struct team_port *port,
502 return RX_HANDLER_ANOTHER;
505 static const struct team_mode __team_no_mode = {
509 static bool team_is_mode_set(struct team *team)
511 return team->mode != &__team_no_mode;
514 static void team_set_no_mode(struct team *team)
516 team->user_carrier_enabled = false;
517 team->mode = &__team_no_mode;
520 static void team_adjust_ops(struct team *team)
523 * To avoid checks in rx/tx skb paths, ensure here that non-null and
524 * correct ops are always set.
527 if (!team->en_port_count || !team_is_mode_set(team) ||
528 !team->mode->ops->transmit)
529 team->ops.transmit = team_dummy_transmit;
531 team->ops.transmit = team->mode->ops->transmit;
533 if (!team->en_port_count || !team_is_mode_set(team) ||
534 !team->mode->ops->receive)
535 team->ops.receive = team_dummy_receive;
537 team->ops.receive = team->mode->ops->receive;
541 * We can benefit from the fact that it's ensured no port is present
542 * at the time of mode change. Therefore no packets are in fly so there's no
543 * need to set mode operations in any special way.
545 static int __team_change_mode(struct team *team,
546 const struct team_mode *new_mode)
548 /* Check if mode was previously set and do cleanup if so */
549 if (team_is_mode_set(team)) {
550 void (*exit_op)(struct team *team) = team->ops.exit;
552 /* Clear ops area so no callback is called any longer */
553 memset(&team->ops, 0, sizeof(struct team_mode_ops));
554 team_adjust_ops(team);
558 team_mode_put(team->mode);
559 team_set_no_mode(team);
560 /* zero private data area */
561 memset(&team->mode_priv, 0,
562 sizeof(struct team) - offsetof(struct team, mode_priv));
568 if (new_mode->ops->init) {
571 err = new_mode->ops->init(team);
576 team->mode = new_mode;
577 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
578 team_adjust_ops(team);
583 static int team_change_mode(struct team *team, const char *kind)
585 const struct team_mode *new_mode;
586 struct net_device *dev = team->dev;
589 if (!list_empty(&team->port_list)) {
590 netdev_err(dev, "No ports can be present during mode change\n");
594 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
595 netdev_err(dev, "Unable to change to the same mode the team is in\n");
599 new_mode = team_mode_get(kind);
601 netdev_err(dev, "Mode \"%s\" not found\n", kind);
605 err = __team_change_mode(team, new_mode);
607 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
608 team_mode_put(new_mode);
612 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
617 /*********************
619 *********************/
621 static void team_notify_peers_work(struct work_struct *work)
626 team = container_of(work, struct team, notify_peers.dw.work);
628 if (!rtnl_trylock()) {
629 schedule_delayed_work(&team->notify_peers.dw, 0);
632 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
637 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
640 schedule_delayed_work(&team->notify_peers.dw,
641 msecs_to_jiffies(team->notify_peers.interval));
644 static void team_notify_peers(struct team *team)
646 if (!team->notify_peers.count || !netif_running(team->dev))
648 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
649 schedule_delayed_work(&team->notify_peers.dw, 0);
652 static void team_notify_peers_init(struct team *team)
654 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
657 static void team_notify_peers_fini(struct team *team)
659 cancel_delayed_work_sync(&team->notify_peers.dw);
663 /*******************************
664 * Send multicast group rejoins
665 *******************************/
667 static void team_mcast_rejoin_work(struct work_struct *work)
672 team = container_of(work, struct team, mcast_rejoin.dw.work);
674 if (!rtnl_trylock()) {
675 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
678 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
683 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
686 schedule_delayed_work(&team->mcast_rejoin.dw,
687 msecs_to_jiffies(team->mcast_rejoin.interval));
690 static void team_mcast_rejoin(struct team *team)
692 if (!team->mcast_rejoin.count || !netif_running(team->dev))
694 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
695 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
698 static void team_mcast_rejoin_init(struct team *team)
700 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
703 static void team_mcast_rejoin_fini(struct team *team)
705 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
709 /************************
710 * Rx path frame handler
711 ************************/
713 /* note: already called with rcu_read_lock */
714 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
716 struct sk_buff *skb = *pskb;
717 struct team_port *port;
719 rx_handler_result_t res;
721 skb = skb_share_check(skb, GFP_ATOMIC);
723 return RX_HANDLER_CONSUMED;
727 port = team_port_get_rcu(skb->dev);
729 if (!team_port_enabled(port)) {
730 /* allow exact match delivery for disabled ports */
731 res = RX_HANDLER_EXACT;
733 res = team->ops.receive(team, port, skb);
735 if (res == RX_HANDLER_ANOTHER) {
736 struct team_pcpu_stats *pcpu_stats;
738 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
739 u64_stats_update_begin(&pcpu_stats->syncp);
740 pcpu_stats->rx_packets++;
741 pcpu_stats->rx_bytes += skb->len;
742 if (skb->pkt_type == PACKET_MULTICAST)
743 pcpu_stats->rx_multicast++;
744 u64_stats_update_end(&pcpu_stats->syncp);
746 skb->dev = team->dev;
748 this_cpu_inc(team->pcpu_stats->rx_dropped);
755 /*************************************
756 * Multiqueue Tx port select override
757 *************************************/
759 static int team_queue_override_init(struct team *team)
761 struct list_head *listarr;
762 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
767 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
770 team->qom_lists = listarr;
771 for (i = 0; i < queue_cnt; i++)
772 INIT_LIST_HEAD(listarr++);
776 static void team_queue_override_fini(struct team *team)
778 kfree(team->qom_lists);
781 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
783 return &team->qom_lists[queue_id - 1];
787 * note: already called with rcu_read_lock
789 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
791 struct list_head *qom_list;
792 struct team_port *port;
794 if (!team->queue_override_enabled || !skb->queue_mapping)
796 qom_list = __team_get_qom_list(team, skb->queue_mapping);
797 list_for_each_entry_rcu(port, qom_list, qom_list) {
798 if (!team_dev_queue_xmit(team, port, skb))
804 static void __team_queue_override_port_del(struct team *team,
805 struct team_port *port)
809 list_del_rcu(&port->qom_list);
812 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
813 struct team_port *cur)
815 if (port->priority < cur->priority)
817 if (port->priority > cur->priority)
819 if (port->index < cur->index)
824 static void __team_queue_override_port_add(struct team *team,
825 struct team_port *port)
827 struct team_port *cur;
828 struct list_head *qom_list;
829 struct list_head *node;
833 qom_list = __team_get_qom_list(team, port->queue_id);
835 list_for_each_entry(cur, qom_list, qom_list) {
836 if (team_queue_override_port_has_gt_prio_than(port, cur))
838 node = &cur->qom_list;
840 list_add_tail_rcu(&port->qom_list, node);
843 static void __team_queue_override_enabled_check(struct team *team)
845 struct team_port *port;
846 bool enabled = false;
848 list_for_each_entry(port, &team->port_list, list) {
849 if (port->queue_id) {
854 if (enabled == team->queue_override_enabled)
856 netdev_dbg(team->dev, "%s queue override\n",
857 enabled ? "Enabling" : "Disabling");
858 team->queue_override_enabled = enabled;
861 static void team_queue_override_port_prio_changed(struct team *team,
862 struct team_port *port)
864 if (!port->queue_id || team_port_enabled(port))
866 __team_queue_override_port_del(team, port);
867 __team_queue_override_port_add(team, port);
868 __team_queue_override_enabled_check(team);
871 static void team_queue_override_port_change_queue_id(struct team *team,
872 struct team_port *port,
875 if (team_port_enabled(port)) {
876 __team_queue_override_port_del(team, port);
877 port->queue_id = new_queue_id;
878 __team_queue_override_port_add(team, port);
879 __team_queue_override_enabled_check(team);
881 port->queue_id = new_queue_id;
885 static void team_queue_override_port_add(struct team *team,
886 struct team_port *port)
888 __team_queue_override_port_add(team, port);
889 __team_queue_override_enabled_check(team);
892 static void team_queue_override_port_del(struct team *team,
893 struct team_port *port)
895 __team_queue_override_port_del(team, port);
896 __team_queue_override_enabled_check(team);
904 static bool team_port_find(const struct team *team,
905 const struct team_port *port)
907 struct team_port *cur;
909 list_for_each_entry(cur, &team->port_list, list)
916 * Enable/disable port by adding to enabled port hashlist and setting
917 * port->index (Might be racy so reader could see incorrect ifindex when
918 * processing a flying packet, but that is not a problem). Write guarded
921 static void team_port_enable(struct team *team,
922 struct team_port *port)
924 if (team_port_enabled(port))
926 port->index = team->en_port_count++;
927 hlist_add_head_rcu(&port->hlist,
928 team_port_index_hash(team, port->index));
929 team_adjust_ops(team);
930 team_queue_override_port_add(team, port);
931 if (team->ops.port_enabled)
932 team->ops.port_enabled(team, port);
933 team_notify_peers(team);
934 team_mcast_rejoin(team);
937 static void __reconstruct_port_hlist(struct team *team, int rm_index)
940 struct team_port *port;
942 for (i = rm_index + 1; i < team->en_port_count; i++) {
943 port = team_get_port_by_index(team, i);
944 hlist_del_rcu(&port->hlist);
946 hlist_add_head_rcu(&port->hlist,
947 team_port_index_hash(team, port->index));
951 static void team_port_disable(struct team *team,
952 struct team_port *port)
954 if (!team_port_enabled(port))
956 if (team->ops.port_disabled)
957 team->ops.port_disabled(team, port);
958 hlist_del_rcu(&port->hlist);
959 __reconstruct_port_hlist(team, port->index);
961 team->en_port_count--;
962 team_queue_override_port_del(team, port);
963 team_adjust_ops(team);
964 team_notify_peers(team);
965 team_mcast_rejoin(team);
968 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
969 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
970 NETIF_F_HIGHDMA | NETIF_F_LRO)
972 static void __team_compute_features(struct team *team)
974 struct team_port *port;
975 u32 vlan_features = TEAM_VLAN_FEATURES & NETIF_F_ALL_FOR_ALL;
976 unsigned short max_hard_header_len = ETH_HLEN;
977 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
978 IFF_XMIT_DST_RELEASE_PERM;
980 list_for_each_entry(port, &team->port_list, list) {
981 vlan_features = netdev_increment_features(vlan_features,
982 port->dev->vlan_features,
985 dst_release_flag &= port->dev->priv_flags;
986 if (port->dev->hard_header_len > max_hard_header_len)
987 max_hard_header_len = port->dev->hard_header_len;
990 team->dev->vlan_features = vlan_features;
991 team->dev->hard_header_len = max_hard_header_len;
993 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
994 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
995 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
997 netdev_change_features(team->dev);
1000 static void team_compute_features(struct team *team)
1002 mutex_lock(&team->lock);
1003 __team_compute_features(team);
1004 mutex_unlock(&team->lock);
1007 static int team_port_enter(struct team *team, struct team_port *port)
1011 dev_hold(team->dev);
1012 if (team->ops.port_enter) {
1013 err = team->ops.port_enter(team, port);
1015 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1017 goto err_port_enter;
1029 static void team_port_leave(struct team *team, struct team_port *port)
1031 if (team->ops.port_leave)
1032 team->ops.port_leave(team, port);
1036 #ifdef CONFIG_NET_POLL_CONTROLLER
1037 static int team_port_enable_netpoll(struct team *team, struct team_port *port)
1042 if (!team->dev->npinfo)
1045 np = kzalloc(sizeof(*np), GFP_KERNEL);
1049 err = __netpoll_setup(np, port->dev);
1058 static void team_port_disable_netpoll(struct team_port *port)
1060 struct netpoll *np = port->np;
1066 /* Wait for transmitting packets to finish before freeing. */
1067 synchronize_rcu_bh();
1068 __netpoll_cleanup(np);
1072 static int team_port_enable_netpoll(struct team *team, struct team_port *port)
1076 static void team_port_disable_netpoll(struct team_port *port)
1081 static int team_upper_dev_link(struct team *team, struct team_port *port)
1083 struct netdev_lag_upper_info lag_upper_info;
1086 lag_upper_info.tx_type = team->mode->lag_tx_type;
1087 err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
1091 port->dev->priv_flags |= IFF_TEAM_PORT;
1095 static void team_upper_dev_unlink(struct team *team, struct team_port *port)
1097 netdev_upper_dev_unlink(port->dev, team->dev);
1098 port->dev->priv_flags &= ~IFF_TEAM_PORT;
1101 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1102 static int team_dev_type_check_change(struct net_device *dev,
1103 struct net_device *port_dev);
1105 static int team_port_add(struct team *team, struct net_device *port_dev)
1107 struct net_device *dev = team->dev;
1108 struct team_port *port;
1109 char *portname = port_dev->name;
1112 if (port_dev->flags & IFF_LOOPBACK) {
1113 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1118 if (team_port_exists(port_dev)) {
1119 netdev_err(dev, "Device %s is already a port "
1120 "of a team device\n", portname);
1124 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1125 vlan_uses_dev(dev)) {
1126 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1131 err = team_dev_type_check_change(dev, port_dev);
1135 if (port_dev->flags & IFF_UP) {
1136 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1141 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1146 port->dev = port_dev;
1148 INIT_LIST_HEAD(&port->qom_list);
1150 port->orig.mtu = port_dev->mtu;
1151 err = dev_set_mtu(port_dev, dev->mtu);
1153 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1157 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1159 err = team_port_enter(team, port);
1161 netdev_err(dev, "Device %s failed to enter team mode\n",
1163 goto err_port_enter;
1166 err = dev_open(port_dev);
1168 netdev_dbg(dev, "Device %s opening failed\n",
1173 err = vlan_vids_add_by_dev(port_dev, dev);
1175 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1180 err = team_port_enable_netpoll(team, port);
1182 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1184 goto err_enable_netpoll;
1187 if (!(dev->features & NETIF_F_LRO))
1188 dev_disable_lro(port_dev);
1190 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1193 netdev_err(dev, "Device %s failed to register rx_handler\n",
1195 goto err_handler_register;
1198 err = team_upper_dev_link(team, port);
1200 netdev_err(dev, "Device %s failed to set upper link\n",
1202 goto err_set_upper_link;
1205 err = __team_option_inst_add_port(team, port);
1207 netdev_err(dev, "Device %s failed to add per-port options\n",
1209 goto err_option_port_add;
1213 list_add_tail_rcu(&port->list, &team->port_list);
1214 team_port_enable(team, port);
1215 __team_compute_features(team);
1216 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1217 __team_options_change_check(team);
1219 netdev_info(dev, "Port device %s added\n", portname);
1223 err_option_port_add:
1224 team_upper_dev_unlink(team, port);
1227 netdev_rx_handler_unregister(port_dev);
1229 err_handler_register:
1230 team_port_disable_netpoll(port);
1233 vlan_vids_del_by_dev(port_dev, dev);
1236 dev_close(port_dev);
1239 team_port_leave(team, port);
1240 team_port_set_orig_dev_addr(port);
1243 dev_set_mtu(port_dev, port->orig.mtu);
1251 static void __team_port_change_port_removed(struct team_port *port);
1253 static int team_port_del(struct team *team, struct net_device *port_dev)
1255 struct net_device *dev = team->dev;
1256 struct team_port *port;
1257 char *portname = port_dev->name;
1259 port = team_port_get_rtnl(port_dev);
1260 if (!port || !team_port_find(team, port)) {
1261 netdev_err(dev, "Device %s does not act as a port of this team\n",
1266 team_port_disable(team, port);
1267 list_del_rcu(&port->list);
1268 team_upper_dev_unlink(team, port);
1269 netdev_rx_handler_unregister(port_dev);
1270 team_port_disable_netpoll(port);
1271 vlan_vids_del_by_dev(port_dev, dev);
1272 dev_uc_unsync(port_dev, dev);
1273 dev_mc_unsync(port_dev, dev);
1274 dev_close(port_dev);
1275 team_port_leave(team, port);
1277 __team_option_inst_mark_removed_port(team, port);
1278 __team_options_change_check(team);
1279 __team_option_inst_del_port(team, port);
1280 __team_port_change_port_removed(port);
1282 team_port_set_orig_dev_addr(port);
1283 dev_set_mtu(port_dev, port->orig.mtu);
1284 kfree_rcu(port, rcu);
1285 netdev_info(dev, "Port device %s removed\n", portname);
1286 __team_compute_features(team);
1296 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1298 ctx->data.str_val = team->mode->kind;
1302 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1304 return team_change_mode(team, ctx->data.str_val);
1307 static int team_notify_peers_count_get(struct team *team,
1308 struct team_gsetter_ctx *ctx)
1310 ctx->data.u32_val = team->notify_peers.count;
1314 static int team_notify_peers_count_set(struct team *team,
1315 struct team_gsetter_ctx *ctx)
1317 team->notify_peers.count = ctx->data.u32_val;
1321 static int team_notify_peers_interval_get(struct team *team,
1322 struct team_gsetter_ctx *ctx)
1324 ctx->data.u32_val = team->notify_peers.interval;
1328 static int team_notify_peers_interval_set(struct team *team,
1329 struct team_gsetter_ctx *ctx)
1331 team->notify_peers.interval = ctx->data.u32_val;
1335 static int team_mcast_rejoin_count_get(struct team *team,
1336 struct team_gsetter_ctx *ctx)
1338 ctx->data.u32_val = team->mcast_rejoin.count;
1342 static int team_mcast_rejoin_count_set(struct team *team,
1343 struct team_gsetter_ctx *ctx)
1345 team->mcast_rejoin.count = ctx->data.u32_val;
1349 static int team_mcast_rejoin_interval_get(struct team *team,
1350 struct team_gsetter_ctx *ctx)
1352 ctx->data.u32_val = team->mcast_rejoin.interval;
1356 static int team_mcast_rejoin_interval_set(struct team *team,
1357 struct team_gsetter_ctx *ctx)
1359 team->mcast_rejoin.interval = ctx->data.u32_val;
1363 static int team_port_en_option_get(struct team *team,
1364 struct team_gsetter_ctx *ctx)
1366 struct team_port *port = ctx->info->port;
1368 ctx->data.bool_val = team_port_enabled(port);
1372 static int team_port_en_option_set(struct team *team,
1373 struct team_gsetter_ctx *ctx)
1375 struct team_port *port = ctx->info->port;
1377 if (ctx->data.bool_val)
1378 team_port_enable(team, port);
1380 team_port_disable(team, port);
1384 static int team_user_linkup_option_get(struct team *team,
1385 struct team_gsetter_ctx *ctx)
1387 struct team_port *port = ctx->info->port;
1389 ctx->data.bool_val = port->user.linkup;
1393 static void __team_carrier_check(struct team *team);
1395 static int team_user_linkup_option_set(struct team *team,
1396 struct team_gsetter_ctx *ctx)
1398 struct team_port *port = ctx->info->port;
1400 port->user.linkup = ctx->data.bool_val;
1401 team_refresh_port_linkup(port);
1402 __team_carrier_check(port->team);
1406 static int team_user_linkup_en_option_get(struct team *team,
1407 struct team_gsetter_ctx *ctx)
1409 struct team_port *port = ctx->info->port;
1411 ctx->data.bool_val = port->user.linkup_enabled;
1415 static int team_user_linkup_en_option_set(struct team *team,
1416 struct team_gsetter_ctx *ctx)
1418 struct team_port *port = ctx->info->port;
1420 port->user.linkup_enabled = ctx->data.bool_val;
1421 team_refresh_port_linkup(port);
1422 __team_carrier_check(port->team);
1426 static int team_priority_option_get(struct team *team,
1427 struct team_gsetter_ctx *ctx)
1429 struct team_port *port = ctx->info->port;
1431 ctx->data.s32_val = port->priority;
1435 static int team_priority_option_set(struct team *team,
1436 struct team_gsetter_ctx *ctx)
1438 struct team_port *port = ctx->info->port;
1439 s32 priority = ctx->data.s32_val;
1441 if (port->priority == priority)
1443 port->priority = priority;
1444 team_queue_override_port_prio_changed(team, port);
1448 static int team_queue_id_option_get(struct team *team,
1449 struct team_gsetter_ctx *ctx)
1451 struct team_port *port = ctx->info->port;
1453 ctx->data.u32_val = port->queue_id;
1457 static int team_queue_id_option_set(struct team *team,
1458 struct team_gsetter_ctx *ctx)
1460 struct team_port *port = ctx->info->port;
1461 u16 new_queue_id = ctx->data.u32_val;
1463 if (port->queue_id == new_queue_id)
1465 if (new_queue_id >= team->dev->real_num_tx_queues)
1467 team_queue_override_port_change_queue_id(team, port, new_queue_id);
1471 static const struct team_option team_options[] = {
1474 .type = TEAM_OPTION_TYPE_STRING,
1475 .getter = team_mode_option_get,
1476 .setter = team_mode_option_set,
1479 .name = "notify_peers_count",
1480 .type = TEAM_OPTION_TYPE_U32,
1481 .getter = team_notify_peers_count_get,
1482 .setter = team_notify_peers_count_set,
1485 .name = "notify_peers_interval",
1486 .type = TEAM_OPTION_TYPE_U32,
1487 .getter = team_notify_peers_interval_get,
1488 .setter = team_notify_peers_interval_set,
1491 .name = "mcast_rejoin_count",
1492 .type = TEAM_OPTION_TYPE_U32,
1493 .getter = team_mcast_rejoin_count_get,
1494 .setter = team_mcast_rejoin_count_set,
1497 .name = "mcast_rejoin_interval",
1498 .type = TEAM_OPTION_TYPE_U32,
1499 .getter = team_mcast_rejoin_interval_get,
1500 .setter = team_mcast_rejoin_interval_set,
1504 .type = TEAM_OPTION_TYPE_BOOL,
1506 .getter = team_port_en_option_get,
1507 .setter = team_port_en_option_set,
1510 .name = "user_linkup",
1511 .type = TEAM_OPTION_TYPE_BOOL,
1513 .getter = team_user_linkup_option_get,
1514 .setter = team_user_linkup_option_set,
1517 .name = "user_linkup_enabled",
1518 .type = TEAM_OPTION_TYPE_BOOL,
1520 .getter = team_user_linkup_en_option_get,
1521 .setter = team_user_linkup_en_option_set,
1525 .type = TEAM_OPTION_TYPE_S32,
1527 .getter = team_priority_option_get,
1528 .setter = team_priority_option_set,
1532 .type = TEAM_OPTION_TYPE_U32,
1534 .getter = team_queue_id_option_get,
1535 .setter = team_queue_id_option_set,
1539 static struct lock_class_key team_netdev_xmit_lock_key;
1540 static struct lock_class_key team_netdev_addr_lock_key;
1541 static struct lock_class_key team_tx_busylock_key;
1543 static void team_set_lockdep_class_one(struct net_device *dev,
1544 struct netdev_queue *txq,
1547 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1550 static void team_set_lockdep_class(struct net_device *dev)
1552 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1553 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1554 dev->qdisc_tx_busylock = &team_tx_busylock_key;
1557 static int team_init(struct net_device *dev)
1559 struct team *team = netdev_priv(dev);
1564 mutex_init(&team->lock);
1565 team_set_no_mode(team);
1567 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1568 if (!team->pcpu_stats)
1571 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1572 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1573 INIT_LIST_HEAD(&team->port_list);
1574 err = team_queue_override_init(team);
1576 goto err_team_queue_override_init;
1578 team_adjust_ops(team);
1580 INIT_LIST_HEAD(&team->option_list);
1581 INIT_LIST_HEAD(&team->option_inst_list);
1583 team_notify_peers_init(team);
1584 team_mcast_rejoin_init(team);
1586 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1588 goto err_options_register;
1589 netif_carrier_off(dev);
1591 team_set_lockdep_class(dev);
1595 err_options_register:
1596 team_mcast_rejoin_fini(team);
1597 team_notify_peers_fini(team);
1598 team_queue_override_fini(team);
1599 err_team_queue_override_init:
1600 free_percpu(team->pcpu_stats);
1605 static void team_uninit(struct net_device *dev)
1607 struct team *team = netdev_priv(dev);
1608 struct team_port *port;
1609 struct team_port *tmp;
1611 mutex_lock(&team->lock);
1612 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1613 team_port_del(team, port->dev);
1615 __team_change_mode(team, NULL); /* cleanup */
1616 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1617 team_mcast_rejoin_fini(team);
1618 team_notify_peers_fini(team);
1619 team_queue_override_fini(team);
1620 mutex_unlock(&team->lock);
1623 static void team_destructor(struct net_device *dev)
1625 struct team *team = netdev_priv(dev);
1627 free_percpu(team->pcpu_stats);
1631 static int team_open(struct net_device *dev)
1636 static int team_close(struct net_device *dev)
1642 * note: already called with rcu_read_lock
1644 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1646 struct team *team = netdev_priv(dev);
1648 unsigned int len = skb->len;
1650 tx_success = team_queue_override_transmit(team, skb);
1652 tx_success = team->ops.transmit(team, skb);
1654 struct team_pcpu_stats *pcpu_stats;
1656 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1657 u64_stats_update_begin(&pcpu_stats->syncp);
1658 pcpu_stats->tx_packets++;
1659 pcpu_stats->tx_bytes += len;
1660 u64_stats_update_end(&pcpu_stats->syncp);
1662 this_cpu_inc(team->pcpu_stats->tx_dropped);
1665 return NETDEV_TX_OK;
1668 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1669 void *accel_priv, select_queue_fallback_t fallback)
1672 * This helper function exists to help dev_pick_tx get the correct
1673 * destination queue. Using a helper function skips a call to
1674 * skb_tx_hash and will put the skbs in the queue we expect on their
1675 * way down to the team driver.
1677 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1680 * Save the original txq to restore before passing to the driver
1682 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1684 if (unlikely(txq >= dev->real_num_tx_queues)) {
1686 txq -= dev->real_num_tx_queues;
1687 } while (txq >= dev->real_num_tx_queues);
1692 static void team_change_rx_flags(struct net_device *dev, int change)
1694 struct team *team = netdev_priv(dev);
1695 struct team_port *port;
1699 list_for_each_entry_rcu(port, &team->port_list, list) {
1700 if (change & IFF_PROMISC) {
1701 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1702 dev_set_promiscuity(port->dev, inc);
1704 if (change & IFF_ALLMULTI) {
1705 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1706 dev_set_allmulti(port->dev, inc);
1712 static void team_set_rx_mode(struct net_device *dev)
1714 struct team *team = netdev_priv(dev);
1715 struct team_port *port;
1718 list_for_each_entry_rcu(port, &team->port_list, list) {
1719 dev_uc_sync_multiple(port->dev, dev);
1720 dev_mc_sync_multiple(port->dev, dev);
1725 static int team_set_mac_address(struct net_device *dev, void *p)
1727 struct sockaddr *addr = p;
1728 struct team *team = netdev_priv(dev);
1729 struct team_port *port;
1731 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1732 return -EADDRNOTAVAIL;
1733 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1734 mutex_lock(&team->lock);
1735 list_for_each_entry(port, &team->port_list, list)
1736 if (team->ops.port_change_dev_addr)
1737 team->ops.port_change_dev_addr(team, port);
1738 mutex_unlock(&team->lock);
1742 static int team_change_mtu(struct net_device *dev, int new_mtu)
1744 struct team *team = netdev_priv(dev);
1745 struct team_port *port;
1749 * Alhough this is reader, it's guarded by team lock. It's not possible
1750 * to traverse list in reverse under rcu_read_lock
1752 mutex_lock(&team->lock);
1753 team->port_mtu_change_allowed = true;
1754 list_for_each_entry(port, &team->port_list, list) {
1755 err = dev_set_mtu(port->dev, new_mtu);
1757 netdev_err(dev, "Device %s failed to change mtu",
1762 team->port_mtu_change_allowed = false;
1763 mutex_unlock(&team->lock);
1770 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1771 dev_set_mtu(port->dev, dev->mtu);
1772 team->port_mtu_change_allowed = false;
1773 mutex_unlock(&team->lock);
1778 static struct rtnl_link_stats64 *
1779 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1781 struct team *team = netdev_priv(dev);
1782 struct team_pcpu_stats *p;
1783 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1784 u32 rx_dropped = 0, tx_dropped = 0;
1788 for_each_possible_cpu(i) {
1789 p = per_cpu_ptr(team->pcpu_stats, i);
1791 start = u64_stats_fetch_begin_irq(&p->syncp);
1792 rx_packets = p->rx_packets;
1793 rx_bytes = p->rx_bytes;
1794 rx_multicast = p->rx_multicast;
1795 tx_packets = p->tx_packets;
1796 tx_bytes = p->tx_bytes;
1797 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1799 stats->rx_packets += rx_packets;
1800 stats->rx_bytes += rx_bytes;
1801 stats->multicast += rx_multicast;
1802 stats->tx_packets += tx_packets;
1803 stats->tx_bytes += tx_bytes;
1805 * rx_dropped & tx_dropped are u32, updated
1806 * without syncp protection.
1808 rx_dropped += p->rx_dropped;
1809 tx_dropped += p->tx_dropped;
1811 stats->rx_dropped = rx_dropped;
1812 stats->tx_dropped = tx_dropped;
1816 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1818 struct team *team = netdev_priv(dev);
1819 struct team_port *port;
1823 * Alhough this is reader, it's guarded by team lock. It's not possible
1824 * to traverse list in reverse under rcu_read_lock
1826 mutex_lock(&team->lock);
1827 list_for_each_entry(port, &team->port_list, list) {
1828 err = vlan_vid_add(port->dev, proto, vid);
1832 mutex_unlock(&team->lock);
1837 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1838 vlan_vid_del(port->dev, proto, vid);
1839 mutex_unlock(&team->lock);
1844 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1846 struct team *team = netdev_priv(dev);
1847 struct team_port *port;
1850 list_for_each_entry_rcu(port, &team->port_list, list)
1851 vlan_vid_del(port->dev, proto, vid);
1857 #ifdef CONFIG_NET_POLL_CONTROLLER
1858 static void team_poll_controller(struct net_device *dev)
1862 static void __team_netpoll_cleanup(struct team *team)
1864 struct team_port *port;
1866 list_for_each_entry(port, &team->port_list, list)
1867 team_port_disable_netpoll(port);
1870 static void team_netpoll_cleanup(struct net_device *dev)
1872 struct team *team = netdev_priv(dev);
1874 mutex_lock(&team->lock);
1875 __team_netpoll_cleanup(team);
1876 mutex_unlock(&team->lock);
1879 static int team_netpoll_setup(struct net_device *dev,
1880 struct netpoll_info *npifo)
1882 struct team *team = netdev_priv(dev);
1883 struct team_port *port;
1886 mutex_lock(&team->lock);
1887 list_for_each_entry(port, &team->port_list, list) {
1888 err = team_port_enable_netpoll(team, port);
1890 __team_netpoll_cleanup(team);
1894 mutex_unlock(&team->lock);
1899 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1901 struct team *team = netdev_priv(dev);
1904 mutex_lock(&team->lock);
1905 err = team_port_add(team, port_dev);
1906 mutex_unlock(&team->lock);
1910 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1912 struct team *team = netdev_priv(dev);
1915 mutex_lock(&team->lock);
1916 err = team_port_del(team, port_dev);
1917 mutex_unlock(&team->lock);
1921 static netdev_features_t team_fix_features(struct net_device *dev,
1922 netdev_features_t features)
1924 struct team_port *port;
1925 struct team *team = netdev_priv(dev);
1926 netdev_features_t mask;
1929 features &= ~NETIF_F_ONE_FOR_ALL;
1930 features |= NETIF_F_ALL_FOR_ALL;
1933 list_for_each_entry_rcu(port, &team->port_list, list) {
1934 features = netdev_increment_features(features,
1935 port->dev->features,
1940 features = netdev_add_tso_features(features, mask);
1945 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1947 struct team *team = netdev_priv(dev);
1949 team->user_carrier_enabled = true;
1952 netif_carrier_on(dev);
1954 netif_carrier_off(dev);
1958 static const struct net_device_ops team_netdev_ops = {
1959 .ndo_init = team_init,
1960 .ndo_uninit = team_uninit,
1961 .ndo_open = team_open,
1962 .ndo_stop = team_close,
1963 .ndo_start_xmit = team_xmit,
1964 .ndo_select_queue = team_select_queue,
1965 .ndo_change_rx_flags = team_change_rx_flags,
1966 .ndo_set_rx_mode = team_set_rx_mode,
1967 .ndo_set_mac_address = team_set_mac_address,
1968 .ndo_change_mtu = team_change_mtu,
1969 .ndo_get_stats64 = team_get_stats64,
1970 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1971 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1972 #ifdef CONFIG_NET_POLL_CONTROLLER
1973 .ndo_poll_controller = team_poll_controller,
1974 .ndo_netpoll_setup = team_netpoll_setup,
1975 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1977 .ndo_add_slave = team_add_slave,
1978 .ndo_del_slave = team_del_slave,
1979 .ndo_fix_features = team_fix_features,
1980 .ndo_change_carrier = team_change_carrier,
1981 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1982 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1983 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
1984 .ndo_fdb_add = switchdev_port_fdb_add,
1985 .ndo_fdb_del = switchdev_port_fdb_del,
1986 .ndo_fdb_dump = switchdev_port_fdb_dump,
1987 .ndo_features_check = passthru_features_check,
1990 /***********************
1992 ***********************/
1994 static void team_ethtool_get_drvinfo(struct net_device *dev,
1995 struct ethtool_drvinfo *drvinfo)
1997 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1998 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2001 static const struct ethtool_ops team_ethtool_ops = {
2002 .get_drvinfo = team_ethtool_get_drvinfo,
2003 .get_link = ethtool_op_get_link,
2006 /***********************
2007 * rt netlink interface
2008 ***********************/
2010 static void team_setup_by_port(struct net_device *dev,
2011 struct net_device *port_dev)
2013 dev->header_ops = port_dev->header_ops;
2014 dev->type = port_dev->type;
2015 dev->hard_header_len = port_dev->hard_header_len;
2016 dev->addr_len = port_dev->addr_len;
2017 dev->mtu = port_dev->mtu;
2018 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2019 eth_hw_addr_inherit(dev, port_dev);
2022 static int team_dev_type_check_change(struct net_device *dev,
2023 struct net_device *port_dev)
2025 struct team *team = netdev_priv(dev);
2026 char *portname = port_dev->name;
2029 if (dev->type == port_dev->type)
2031 if (!list_empty(&team->port_list)) {
2032 netdev_err(dev, "Device %s is of different type\n", portname);
2035 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2036 err = notifier_to_errno(err);
2038 netdev_err(dev, "Refused to change device type\n");
2043 team_setup_by_port(dev, port_dev);
2044 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2048 static void team_setup(struct net_device *dev)
2052 dev->netdev_ops = &team_netdev_ops;
2053 dev->ethtool_ops = &team_ethtool_ops;
2054 dev->destructor = team_destructor;
2055 dev->flags |= IFF_MULTICAST;
2056 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2057 dev->priv_flags |= IFF_NO_QUEUE;
2058 dev->priv_flags |= IFF_TEAM;
2061 * Indicate we support unicast address filtering. That way core won't
2062 * bring us to promisc mode in case a unicast addr is added.
2063 * Let this up to underlay drivers.
2065 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2067 dev->features |= NETIF_F_LLTX;
2068 dev->features |= NETIF_F_GRO;
2070 /* Don't allow team devices to change network namespaces. */
2071 dev->features |= NETIF_F_NETNS_LOCAL;
2073 dev->hw_features = TEAM_VLAN_FEATURES |
2074 NETIF_F_HW_VLAN_CTAG_TX |
2075 NETIF_F_HW_VLAN_CTAG_RX |
2076 NETIF_F_HW_VLAN_CTAG_FILTER;
2078 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
2079 dev->features |= dev->hw_features;
2082 static int team_newlink(struct net *src_net, struct net_device *dev,
2083 struct nlattr *tb[], struct nlattr *data[])
2085 if (tb[IFLA_ADDRESS] == NULL)
2086 eth_hw_addr_random(dev);
2088 return register_netdevice(dev);
2091 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
2093 if (tb[IFLA_ADDRESS]) {
2094 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2096 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2097 return -EADDRNOTAVAIL;
2102 static unsigned int team_get_num_tx_queues(void)
2104 return TEAM_DEFAULT_NUM_TX_QUEUES;
2107 static unsigned int team_get_num_rx_queues(void)
2109 return TEAM_DEFAULT_NUM_RX_QUEUES;
2112 static struct rtnl_link_ops team_link_ops __read_mostly = {
2114 .priv_size = sizeof(struct team),
2115 .setup = team_setup,
2116 .newlink = team_newlink,
2117 .validate = team_validate,
2118 .get_num_tx_queues = team_get_num_tx_queues,
2119 .get_num_rx_queues = team_get_num_rx_queues,
2123 /***********************************
2124 * Generic netlink custom interface
2125 ***********************************/
2127 static struct genl_family team_nl_family = {
2128 .id = GENL_ID_GENERATE,
2129 .name = TEAM_GENL_NAME,
2130 .version = TEAM_GENL_VERSION,
2131 .maxattr = TEAM_ATTR_MAX,
2135 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2136 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2137 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2138 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2139 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2142 static const struct nla_policy
2143 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2144 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2145 [TEAM_ATTR_OPTION_NAME] = {
2147 .len = TEAM_STRING_MAX_LEN,
2149 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2150 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2151 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
2154 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2156 struct sk_buff *msg;
2160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2164 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2165 &team_nl_family, 0, TEAM_CMD_NOOP);
2171 genlmsg_end(msg, hdr);
2173 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2182 * Netlink cmd functions should be locked by following two functions.
2183 * Since dev gets held here, that ensures dev won't disappear in between.
2185 static struct team *team_nl_team_get(struct genl_info *info)
2187 struct net *net = genl_info_net(info);
2189 struct net_device *dev;
2192 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2195 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2196 dev = dev_get_by_index(net, ifindex);
2197 if (!dev || dev->netdev_ops != &team_netdev_ops) {
2203 team = netdev_priv(dev);
2204 mutex_lock(&team->lock);
2208 static void team_nl_team_put(struct team *team)
2210 mutex_unlock(&team->lock);
2214 typedef int team_nl_send_func_t(struct sk_buff *skb,
2215 struct team *team, u32 portid);
2217 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2219 return genlmsg_unicast(dev_net(team->dev), skb, portid);
2222 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2223 struct team_option_inst *opt_inst)
2225 struct nlattr *option_item;
2226 struct team_option *option = opt_inst->option;
2227 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2228 struct team_gsetter_ctx ctx;
2231 ctx.info = opt_inst_info;
2232 err = team_option_get(team, opt_inst, &ctx);
2236 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2240 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2242 if (opt_inst_info->port &&
2243 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2244 opt_inst_info->port->dev->ifindex))
2246 if (opt_inst->option->array_size &&
2247 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2248 opt_inst_info->array_index))
2251 switch (option->type) {
2252 case TEAM_OPTION_TYPE_U32:
2253 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2255 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2258 case TEAM_OPTION_TYPE_STRING:
2259 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2261 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2265 case TEAM_OPTION_TYPE_BINARY:
2266 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2268 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2269 ctx.data.bin_val.ptr))
2272 case TEAM_OPTION_TYPE_BOOL:
2273 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2275 if (ctx.data.bool_val &&
2276 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2279 case TEAM_OPTION_TYPE_S32:
2280 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2282 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2288 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2290 if (opt_inst->changed) {
2291 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2293 opt_inst->changed = false;
2295 nla_nest_end(skb, option_item);
2299 nla_nest_cancel(skb, option_item);
2303 static int __send_and_alloc_skb(struct sk_buff **pskb,
2304 struct team *team, u32 portid,
2305 team_nl_send_func_t *send_func)
2310 err = send_func(*pskb, team, portid);
2314 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2320 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2321 int flags, team_nl_send_func_t *send_func,
2322 struct list_head *sel_opt_inst_list)
2324 struct nlattr *option_list;
2325 struct nlmsghdr *nlh;
2327 struct team_option_inst *opt_inst;
2329 struct sk_buff *skb = NULL;
2333 opt_inst = list_first_entry(sel_opt_inst_list,
2334 struct team_option_inst, tmp_list);
2337 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2341 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2342 TEAM_CMD_OPTIONS_GET);
2346 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2347 goto nla_put_failure;
2348 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2350 goto nla_put_failure;
2354 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2355 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2357 if (err == -EMSGSIZE) {
2368 nla_nest_end(skb, option_list);
2369 genlmsg_end(skb, hdr);
2374 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2376 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2382 return send_func(skb, team, portid);
2387 genlmsg_cancel(skb, hdr);
2392 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2395 struct team_option_inst *opt_inst;
2397 LIST_HEAD(sel_opt_inst_list);
2399 team = team_nl_team_get(info);
2403 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2404 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2405 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2406 NLM_F_ACK, team_nl_send_unicast,
2407 &sel_opt_inst_list);
2409 team_nl_team_put(team);
2414 static int team_nl_send_event_options_get(struct team *team,
2415 struct list_head *sel_opt_inst_list);
2417 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2422 struct nlattr *nl_option;
2423 LIST_HEAD(opt_inst_list);
2425 team = team_nl_team_get(info);
2430 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2435 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2436 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2437 struct nlattr *attr;
2438 struct nlattr *attr_data;
2439 enum team_option_type opt_type;
2440 int opt_port_ifindex = 0; /* != 0 for per-port options */
2441 u32 opt_array_index = 0;
2442 bool opt_is_array = false;
2443 struct team_option_inst *opt_inst;
2445 bool opt_found = false;
2447 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2451 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2452 nl_option, team_nl_option_policy);
2455 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2456 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2460 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2462 opt_type = TEAM_OPTION_TYPE_U32;
2465 opt_type = TEAM_OPTION_TYPE_STRING;
2468 opt_type = TEAM_OPTION_TYPE_BINARY;
2471 opt_type = TEAM_OPTION_TYPE_BOOL;
2474 opt_type = TEAM_OPTION_TYPE_S32;
2480 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2481 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2486 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2487 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2489 opt_port_ifindex = nla_get_u32(attr);
2491 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2493 opt_is_array = true;
2494 opt_array_index = nla_get_u32(attr);
2497 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2498 struct team_option *option = opt_inst->option;
2499 struct team_gsetter_ctx ctx;
2500 struct team_option_inst_info *opt_inst_info;
2503 opt_inst_info = &opt_inst->info;
2504 tmp_ifindex = opt_inst_info->port ?
2505 opt_inst_info->port->dev->ifindex : 0;
2506 if (option->type != opt_type ||
2507 strcmp(option->name, opt_name) ||
2508 tmp_ifindex != opt_port_ifindex ||
2509 (option->array_size && !opt_is_array) ||
2510 opt_inst_info->array_index != opt_array_index)
2513 ctx.info = opt_inst_info;
2515 case TEAM_OPTION_TYPE_U32:
2516 ctx.data.u32_val = nla_get_u32(attr_data);
2518 case TEAM_OPTION_TYPE_STRING:
2519 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2523 ctx.data.str_val = nla_data(attr_data);
2525 case TEAM_OPTION_TYPE_BINARY:
2526 ctx.data.bin_val.len = nla_len(attr_data);
2527 ctx.data.bin_val.ptr = nla_data(attr_data);
2529 case TEAM_OPTION_TYPE_BOOL:
2530 ctx.data.bool_val = attr_data ? true : false;
2532 case TEAM_OPTION_TYPE_S32:
2533 ctx.data.s32_val = nla_get_s32(attr_data);
2538 err = team_option_set(team, opt_inst, &ctx);
2541 opt_inst->changed = true;
2542 list_add(&opt_inst->tmp_list, &opt_inst_list);
2550 err = team_nl_send_event_options_get(team, &opt_inst_list);
2553 team_nl_team_put(team);
2558 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2559 struct team_port *port)
2561 struct nlattr *port_item;
2563 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2566 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2568 if (port->changed) {
2569 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2571 port->changed = false;
2573 if ((port->removed &&
2574 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2575 (port->state.linkup &&
2576 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2577 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2578 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2580 nla_nest_end(skb, port_item);
2584 nla_nest_cancel(skb, port_item);
2588 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2589 int flags, team_nl_send_func_t *send_func,
2590 struct team_port *one_port)
2592 struct nlattr *port_list;
2593 struct nlmsghdr *nlh;
2595 struct team_port *port;
2597 struct sk_buff *skb = NULL;
2601 port = list_first_entry_or_null(&team->port_list,
2602 struct team_port, list);
2605 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2609 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2610 TEAM_CMD_PORT_LIST_GET);
2614 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2615 goto nla_put_failure;
2616 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2618 goto nla_put_failure;
2623 /* If one port is selected, called wants to send port list containing
2624 * only this port. Otherwise go through all listed ports and send all
2627 err = team_nl_fill_one_port_get(skb, one_port);
2631 list_for_each_entry_from(port, &team->port_list, list) {
2632 err = team_nl_fill_one_port_get(skb, port);
2634 if (err == -EMSGSIZE) {
2646 nla_nest_end(skb, port_list);
2647 genlmsg_end(skb, hdr);
2652 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2654 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2660 return send_func(skb, team, portid);
2665 genlmsg_cancel(skb, hdr);
2670 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2671 struct genl_info *info)
2676 team = team_nl_team_get(info);
2680 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2681 NLM_F_ACK, team_nl_send_unicast, NULL);
2683 team_nl_team_put(team);
2688 static const struct genl_ops team_nl_ops[] = {
2690 .cmd = TEAM_CMD_NOOP,
2691 .doit = team_nl_cmd_noop,
2692 .policy = team_nl_policy,
2695 .cmd = TEAM_CMD_OPTIONS_SET,
2696 .doit = team_nl_cmd_options_set,
2697 .policy = team_nl_policy,
2698 .flags = GENL_ADMIN_PERM,
2701 .cmd = TEAM_CMD_OPTIONS_GET,
2702 .doit = team_nl_cmd_options_get,
2703 .policy = team_nl_policy,
2704 .flags = GENL_ADMIN_PERM,
2707 .cmd = TEAM_CMD_PORT_LIST_GET,
2708 .doit = team_nl_cmd_port_list_get,
2709 .policy = team_nl_policy,
2710 .flags = GENL_ADMIN_PERM,
2714 static const struct genl_multicast_group team_nl_mcgrps[] = {
2715 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2718 static int team_nl_send_multicast(struct sk_buff *skb,
2719 struct team *team, u32 portid)
2721 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2722 skb, 0, 0, GFP_KERNEL);
2725 static int team_nl_send_event_options_get(struct team *team,
2726 struct list_head *sel_opt_inst_list)
2728 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2732 static int team_nl_send_event_port_get(struct team *team,
2733 struct team_port *port)
2735 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2739 static int team_nl_init(void)
2741 return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
2745 static void team_nl_fini(void)
2747 genl_unregister_family(&team_nl_family);
2755 static void __team_options_change_check(struct team *team)
2758 struct team_option_inst *opt_inst;
2759 LIST_HEAD(sel_opt_inst_list);
2761 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2762 if (opt_inst->changed)
2763 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2765 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2766 if (err && err != -ESRCH)
2767 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2771 /* rtnl lock is held */
2773 static void __team_port_change_send(struct team_port *port, bool linkup)
2777 port->changed = true;
2778 port->state.linkup = linkup;
2779 team_refresh_port_linkup(port);
2781 struct ethtool_cmd ecmd;
2783 err = __ethtool_get_settings(port->dev, &ecmd);
2785 port->state.speed = ethtool_cmd_speed(&ecmd);
2786 port->state.duplex = ecmd.duplex;
2790 port->state.speed = 0;
2791 port->state.duplex = 0;
2794 err = team_nl_send_event_port_get(port->team, port);
2795 if (err && err != -ESRCH)
2796 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2797 port->dev->name, err);
2801 static void __team_carrier_check(struct team *team)
2803 struct team_port *port;
2806 if (team->user_carrier_enabled)
2809 team_linkup = false;
2810 list_for_each_entry(port, &team->port_list, list) {
2818 netif_carrier_on(team->dev);
2820 netif_carrier_off(team->dev);
2823 static void __team_port_change_check(struct team_port *port, bool linkup)
2825 if (port->state.linkup != linkup)
2826 __team_port_change_send(port, linkup);
2827 __team_carrier_check(port->team);
2830 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2832 __team_port_change_send(port, linkup);
2833 __team_carrier_check(port->team);
2836 static void __team_port_change_port_removed(struct team_port *port)
2838 port->removed = true;
2839 __team_port_change_send(port, false);
2840 __team_carrier_check(port->team);
2843 static void team_port_change_check(struct team_port *port, bool linkup)
2845 struct team *team = port->team;
2847 mutex_lock(&team->lock);
2848 __team_port_change_check(port, linkup);
2849 mutex_unlock(&team->lock);
2853 /************************************
2854 * Net device notifier event handler
2855 ************************************/
2857 static int team_device_event(struct notifier_block *unused,
2858 unsigned long event, void *ptr)
2860 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2861 struct team_port *port;
2863 port = team_port_get_rtnl(dev);
2869 if (netif_carrier_ok(dev))
2870 team_port_change_check(port, true);
2873 team_port_change_check(port, false);
2876 if (netif_running(port->dev))
2877 team_port_change_check(port,
2878 !!netif_carrier_ok(port->dev));
2880 case NETDEV_UNREGISTER:
2881 team_del_slave(port->team->dev, dev);
2883 case NETDEV_FEAT_CHANGE:
2884 team_compute_features(port->team);
2886 case NETDEV_PRECHANGEMTU:
2887 /* Forbid to change mtu of underlaying device */
2888 if (!port->team->port_mtu_change_allowed)
2891 case NETDEV_PRE_TYPE_CHANGE:
2892 /* Forbid to change type of underlaying device */
2894 case NETDEV_RESEND_IGMP:
2895 /* Propagate to master device */
2896 call_netdevice_notifiers(event, port->team->dev);
2902 static struct notifier_block team_notifier_block __read_mostly = {
2903 .notifier_call = team_device_event,
2907 /***********************
2908 * Module init and exit
2909 ***********************/
2911 static int __init team_module_init(void)
2915 register_netdevice_notifier(&team_notifier_block);
2917 err = rtnl_link_register(&team_link_ops);
2921 err = team_nl_init();
2928 rtnl_link_unregister(&team_link_ops);
2931 unregister_netdevice_notifier(&team_notifier_block);
2936 static void __exit team_module_exit(void)
2939 rtnl_link_unregister(&team_link_ops);
2940 unregister_netdevice_notifier(&team_notifier_block);
2943 module_init(team_module_init);
2944 module_exit(team_module_exit);
2946 MODULE_LICENSE("GPL v2");
2947 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2948 MODULE_DESCRIPTION("Ethernet team device driver");
2949 MODULE_ALIAS_RTNL_LINK(DRV_NAME);