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 <generated/utsrelease.h>
32 #include <linux/if_team.h>
34 #define DRV_NAME "team"
41 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
43 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
45 return netif_is_team_port(dev) ? port : NULL;
49 * Since the ability to change device address for open port device is tested in
50 * team_port_add, this function can be called without control of return value
52 static int __set_port_dev_addr(struct net_device *port_dev,
53 const unsigned char *dev_addr)
55 struct sockaddr_storage addr;
57 memcpy(addr.__data, dev_addr, port_dev->addr_len);
58 addr.ss_family = port_dev->type;
59 return dev_set_mac_address(port_dev, (struct sockaddr *)&addr, NULL);
62 static int team_port_set_orig_dev_addr(struct team_port *port)
64 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
67 static int team_port_set_team_dev_addr(struct team *team,
68 struct team_port *port)
70 return __set_port_dev_addr(port->dev, team->dev->dev_addr);
73 int team_modeop_port_enter(struct team *team, struct team_port *port)
75 return team_port_set_team_dev_addr(team, port);
77 EXPORT_SYMBOL(team_modeop_port_enter);
79 void team_modeop_port_change_dev_addr(struct team *team,
80 struct team_port *port)
82 team_port_set_team_dev_addr(team, port);
84 EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
86 static void team_lower_state_changed(struct team_port *port)
88 struct netdev_lag_lower_state_info info;
90 info.link_up = port->linkup;
91 info.tx_enabled = team_port_enabled(port);
92 netdev_lower_state_changed(port->dev, &info);
95 static void team_refresh_port_linkup(struct team_port *port)
97 bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
100 if (port->linkup != new_linkup) {
101 port->linkup = new_linkup;
102 team_lower_state_changed(port);
111 struct team_option_inst { /* One for each option instance */
112 struct list_head list;
113 struct list_head tmp_list;
114 struct team_option *option;
115 struct team_option_inst_info info;
120 static struct team_option *__team_find_option(struct team *team,
121 const char *opt_name)
123 struct team_option *option;
125 list_for_each_entry(option, &team->option_list, list) {
126 if (strcmp(option->name, opt_name) == 0)
132 static void __team_option_inst_del(struct team_option_inst *opt_inst)
134 list_del(&opt_inst->list);
138 static void __team_option_inst_del_option(struct team *team,
139 struct team_option *option)
141 struct team_option_inst *opt_inst, *tmp;
143 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
144 if (opt_inst->option == option)
145 __team_option_inst_del(opt_inst);
149 static int __team_option_inst_add(struct team *team, struct team_option *option,
150 struct team_port *port)
152 struct team_option_inst *opt_inst;
153 unsigned int array_size;
157 array_size = option->array_size;
159 array_size = 1; /* No array but still need one instance */
161 for (i = 0; i < array_size; i++) {
162 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
165 opt_inst->option = option;
166 opt_inst->info.port = port;
167 opt_inst->info.array_index = i;
168 opt_inst->changed = true;
169 opt_inst->removed = false;
170 list_add_tail(&opt_inst->list, &team->option_inst_list);
172 err = option->init(team, &opt_inst->info);
181 static int __team_option_inst_add_option(struct team *team,
182 struct team_option *option)
186 if (!option->per_port) {
187 err = __team_option_inst_add(team, option, NULL);
189 goto inst_del_option;
194 __team_option_inst_del_option(team, option);
198 static void __team_option_inst_mark_removed_option(struct team *team,
199 struct team_option *option)
201 struct team_option_inst *opt_inst;
203 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
204 if (opt_inst->option == option) {
205 opt_inst->changed = true;
206 opt_inst->removed = true;
211 static void __team_option_inst_del_port(struct team *team,
212 struct team_port *port)
214 struct team_option_inst *opt_inst, *tmp;
216 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
217 if (opt_inst->option->per_port &&
218 opt_inst->info.port == port)
219 __team_option_inst_del(opt_inst);
223 static int __team_option_inst_add_port(struct team *team,
224 struct team_port *port)
226 struct team_option *option;
229 list_for_each_entry(option, &team->option_list, list) {
230 if (!option->per_port)
232 err = __team_option_inst_add(team, option, port);
239 __team_option_inst_del_port(team, port);
243 static void __team_option_inst_mark_removed_port(struct team *team,
244 struct team_port *port)
246 struct team_option_inst *opt_inst;
248 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
249 if (opt_inst->info.port == port) {
250 opt_inst->changed = true;
251 opt_inst->removed = true;
256 static int __team_options_register(struct team *team,
257 const struct team_option *option,
261 struct team_option **dst_opts;
264 dst_opts = kcalloc(option_count, sizeof(struct team_option *),
268 for (i = 0; i < option_count; i++, option++) {
269 if (__team_find_option(team, option->name)) {
273 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
280 for (i = 0; i < option_count; i++) {
281 err = __team_option_inst_add_option(team, dst_opts[i]);
284 list_add_tail(&dst_opts[i]->list, &team->option_list);
291 for (i--; i >= 0; i--)
292 __team_option_inst_del_option(team, dst_opts[i]);
294 i = option_count - 1;
296 for (i--; i >= 0; i--)
303 static void __team_options_mark_removed(struct team *team,
304 const struct team_option *option,
309 for (i = 0; i < option_count; i++, option++) {
310 struct team_option *del_opt;
312 del_opt = __team_find_option(team, option->name);
314 __team_option_inst_mark_removed_option(team, del_opt);
318 static void __team_options_unregister(struct team *team,
319 const struct team_option *option,
324 for (i = 0; i < option_count; i++, option++) {
325 struct team_option *del_opt;
327 del_opt = __team_find_option(team, option->name);
329 __team_option_inst_del_option(team, del_opt);
330 list_del(&del_opt->list);
336 static void __team_options_change_check(struct team *team);
338 int team_options_register(struct team *team,
339 const struct team_option *option,
344 err = __team_options_register(team, option, option_count);
347 __team_options_change_check(team);
350 EXPORT_SYMBOL(team_options_register);
352 void team_options_unregister(struct team *team,
353 const struct team_option *option,
356 __team_options_mark_removed(team, option, option_count);
357 __team_options_change_check(team);
358 __team_options_unregister(team, option, option_count);
360 EXPORT_SYMBOL(team_options_unregister);
362 static int team_option_get(struct team *team,
363 struct team_option_inst *opt_inst,
364 struct team_gsetter_ctx *ctx)
366 if (!opt_inst->option->getter)
368 return opt_inst->option->getter(team, ctx);
371 static int team_option_set(struct team *team,
372 struct team_option_inst *opt_inst,
373 struct team_gsetter_ctx *ctx)
375 if (!opt_inst->option->setter)
377 return opt_inst->option->setter(team, ctx);
380 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
382 struct team_option_inst *opt_inst;
384 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
385 opt_inst->changed = true;
387 EXPORT_SYMBOL(team_option_inst_set_change);
389 void team_options_change_check(struct team *team)
391 __team_options_change_check(team);
393 EXPORT_SYMBOL(team_options_change_check);
400 static LIST_HEAD(mode_list);
401 static DEFINE_SPINLOCK(mode_list_lock);
403 struct team_mode_item {
404 struct list_head list;
405 const struct team_mode *mode;
408 static struct team_mode_item *__find_mode(const char *kind)
410 struct team_mode_item *mitem;
412 list_for_each_entry(mitem, &mode_list, list) {
413 if (strcmp(mitem->mode->kind, kind) == 0)
419 static bool is_good_mode_name(const char *name)
421 while (*name != '\0') {
422 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
429 int team_mode_register(const struct team_mode *mode)
432 struct team_mode_item *mitem;
434 if (!is_good_mode_name(mode->kind) ||
435 mode->priv_size > TEAM_MODE_PRIV_SIZE)
438 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
442 spin_lock(&mode_list_lock);
443 if (__find_mode(mode->kind)) {
449 list_add_tail(&mitem->list, &mode_list);
451 spin_unlock(&mode_list_lock);
454 EXPORT_SYMBOL(team_mode_register);
456 void team_mode_unregister(const struct team_mode *mode)
458 struct team_mode_item *mitem;
460 spin_lock(&mode_list_lock);
461 mitem = __find_mode(mode->kind);
463 list_del_init(&mitem->list);
466 spin_unlock(&mode_list_lock);
468 EXPORT_SYMBOL(team_mode_unregister);
470 static const struct team_mode *team_mode_get(const char *kind)
472 struct team_mode_item *mitem;
473 const struct team_mode *mode = NULL;
475 spin_lock(&mode_list_lock);
476 mitem = __find_mode(kind);
478 spin_unlock(&mode_list_lock);
479 request_module("team-mode-%s", kind);
480 spin_lock(&mode_list_lock);
481 mitem = __find_mode(kind);
485 if (!try_module_get(mode->owner))
489 spin_unlock(&mode_list_lock);
493 static void team_mode_put(const struct team_mode *mode)
495 module_put(mode->owner);
498 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
500 dev_kfree_skb_any(skb);
504 static rx_handler_result_t team_dummy_receive(struct team *team,
505 struct team_port *port,
508 return RX_HANDLER_ANOTHER;
511 static const struct team_mode __team_no_mode = {
515 static bool team_is_mode_set(struct team *team)
517 return team->mode != &__team_no_mode;
520 static void team_set_no_mode(struct team *team)
522 team->user_carrier_enabled = false;
523 team->mode = &__team_no_mode;
526 static void team_adjust_ops(struct team *team)
529 * To avoid checks in rx/tx skb paths, ensure here that non-null and
530 * correct ops are always set.
533 if (!team->en_port_count || !team_is_mode_set(team) ||
534 !team->mode->ops->transmit)
535 team->ops.transmit = team_dummy_transmit;
537 team->ops.transmit = team->mode->ops->transmit;
539 if (!team->en_port_count || !team_is_mode_set(team) ||
540 !team->mode->ops->receive)
541 team->ops.receive = team_dummy_receive;
543 team->ops.receive = team->mode->ops->receive;
547 * We can benefit from the fact that it's ensured no port is present
548 * at the time of mode change. Therefore no packets are in fly so there's no
549 * need to set mode operations in any special way.
551 static int __team_change_mode(struct team *team,
552 const struct team_mode *new_mode)
554 /* Check if mode was previously set and do cleanup if so */
555 if (team_is_mode_set(team)) {
556 void (*exit_op)(struct team *team) = team->ops.exit;
558 /* Clear ops area so no callback is called any longer */
559 memset(&team->ops, 0, sizeof(struct team_mode_ops));
560 team_adjust_ops(team);
564 team_mode_put(team->mode);
565 team_set_no_mode(team);
566 /* zero private data area */
567 memset(&team->mode_priv, 0,
568 sizeof(struct team) - offsetof(struct team, mode_priv));
574 if (new_mode->ops->init) {
577 err = new_mode->ops->init(team);
582 team->mode = new_mode;
583 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
584 team_adjust_ops(team);
589 static int team_change_mode(struct team *team, const char *kind)
591 const struct team_mode *new_mode;
592 struct net_device *dev = team->dev;
595 if (!list_empty(&team->port_list)) {
596 netdev_err(dev, "No ports can be present during mode change\n");
600 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
601 netdev_err(dev, "Unable to change to the same mode the team is in\n");
605 new_mode = team_mode_get(kind);
607 netdev_err(dev, "Mode \"%s\" not found\n", kind);
611 err = __team_change_mode(team, new_mode);
613 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
614 team_mode_put(new_mode);
618 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
623 /*********************
625 *********************/
627 static void team_notify_peers_work(struct work_struct *work)
632 team = container_of(work, struct team, notify_peers.dw.work);
634 if (!rtnl_trylock()) {
635 schedule_delayed_work(&team->notify_peers.dw, 0);
638 val = atomic_dec_if_positive(&team->notify_peers.count_pending);
643 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
646 schedule_delayed_work(&team->notify_peers.dw,
647 msecs_to_jiffies(team->notify_peers.interval));
650 static void team_notify_peers(struct team *team)
652 if (!team->notify_peers.count || !netif_running(team->dev))
654 atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
655 schedule_delayed_work(&team->notify_peers.dw, 0);
658 static void team_notify_peers_init(struct team *team)
660 INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
663 static void team_notify_peers_fini(struct team *team)
665 cancel_delayed_work_sync(&team->notify_peers.dw);
669 /*******************************
670 * Send multicast group rejoins
671 *******************************/
673 static void team_mcast_rejoin_work(struct work_struct *work)
678 team = container_of(work, struct team, mcast_rejoin.dw.work);
680 if (!rtnl_trylock()) {
681 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
684 val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
689 call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
692 schedule_delayed_work(&team->mcast_rejoin.dw,
693 msecs_to_jiffies(team->mcast_rejoin.interval));
696 static void team_mcast_rejoin(struct team *team)
698 if (!team->mcast_rejoin.count || !netif_running(team->dev))
700 atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
701 schedule_delayed_work(&team->mcast_rejoin.dw, 0);
704 static void team_mcast_rejoin_init(struct team *team)
706 INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
709 static void team_mcast_rejoin_fini(struct team *team)
711 cancel_delayed_work_sync(&team->mcast_rejoin.dw);
715 /************************
716 * Rx path frame handler
717 ************************/
719 /* note: already called with rcu_read_lock */
720 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
722 struct sk_buff *skb = *pskb;
723 struct team_port *port;
725 rx_handler_result_t res;
727 skb = skb_share_check(skb, GFP_ATOMIC);
729 return RX_HANDLER_CONSUMED;
733 port = team_port_get_rcu(skb->dev);
735 if (!team_port_enabled(port)) {
736 /* allow exact match delivery for disabled ports */
737 res = RX_HANDLER_EXACT;
739 res = team->ops.receive(team, port, skb);
741 if (res == RX_HANDLER_ANOTHER) {
742 struct team_pcpu_stats *pcpu_stats;
744 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
745 u64_stats_update_begin(&pcpu_stats->syncp);
746 pcpu_stats->rx_packets++;
747 pcpu_stats->rx_bytes += skb->len;
748 if (skb->pkt_type == PACKET_MULTICAST)
749 pcpu_stats->rx_multicast++;
750 u64_stats_update_end(&pcpu_stats->syncp);
752 skb->dev = team->dev;
753 } else if (res == RX_HANDLER_EXACT) {
754 this_cpu_inc(team->pcpu_stats->rx_nohandler);
756 this_cpu_inc(team->pcpu_stats->rx_dropped);
763 /*************************************
764 * Multiqueue Tx port select override
765 *************************************/
767 static int team_queue_override_init(struct team *team)
769 struct list_head *listarr;
770 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
775 listarr = kmalloc_array(queue_cnt, sizeof(struct list_head),
779 team->qom_lists = listarr;
780 for (i = 0; i < queue_cnt; i++)
781 INIT_LIST_HEAD(listarr++);
785 static void team_queue_override_fini(struct team *team)
787 kfree(team->qom_lists);
790 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
792 return &team->qom_lists[queue_id - 1];
796 * note: already called with rcu_read_lock
798 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
800 struct list_head *qom_list;
801 struct team_port *port;
803 if (!team->queue_override_enabled || !skb->queue_mapping)
805 qom_list = __team_get_qom_list(team, skb->queue_mapping);
806 list_for_each_entry_rcu(port, qom_list, qom_list) {
807 if (!team_dev_queue_xmit(team, port, skb))
813 static void __team_queue_override_port_del(struct team *team,
814 struct team_port *port)
818 list_del_rcu(&port->qom_list);
821 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
822 struct team_port *cur)
824 if (port->priority < cur->priority)
826 if (port->priority > cur->priority)
828 if (port->index < cur->index)
833 static void __team_queue_override_port_add(struct team *team,
834 struct team_port *port)
836 struct team_port *cur;
837 struct list_head *qom_list;
838 struct list_head *node;
842 qom_list = __team_get_qom_list(team, port->queue_id);
844 list_for_each_entry(cur, qom_list, qom_list) {
845 if (team_queue_override_port_has_gt_prio_than(port, cur))
847 node = &cur->qom_list;
849 list_add_tail_rcu(&port->qom_list, node);
852 static void __team_queue_override_enabled_check(struct team *team)
854 struct team_port *port;
855 bool enabled = false;
857 list_for_each_entry(port, &team->port_list, list) {
858 if (port->queue_id) {
863 if (enabled == team->queue_override_enabled)
865 netdev_dbg(team->dev, "%s queue override\n",
866 enabled ? "Enabling" : "Disabling");
867 team->queue_override_enabled = enabled;
870 static void team_queue_override_port_prio_changed(struct team *team,
871 struct team_port *port)
873 if (!port->queue_id || team_port_enabled(port))
875 __team_queue_override_port_del(team, port);
876 __team_queue_override_port_add(team, port);
877 __team_queue_override_enabled_check(team);
880 static void team_queue_override_port_change_queue_id(struct team *team,
881 struct team_port *port,
884 if (team_port_enabled(port)) {
885 __team_queue_override_port_del(team, port);
886 port->queue_id = new_queue_id;
887 __team_queue_override_port_add(team, port);
888 __team_queue_override_enabled_check(team);
890 port->queue_id = new_queue_id;
894 static void team_queue_override_port_add(struct team *team,
895 struct team_port *port)
897 __team_queue_override_port_add(team, port);
898 __team_queue_override_enabled_check(team);
901 static void team_queue_override_port_del(struct team *team,
902 struct team_port *port)
904 __team_queue_override_port_del(team, port);
905 __team_queue_override_enabled_check(team);
913 static bool team_port_find(const struct team *team,
914 const struct team_port *port)
916 struct team_port *cur;
918 list_for_each_entry(cur, &team->port_list, list)
925 * Enable/disable port by adding to enabled port hashlist and setting
926 * port->index (Might be racy so reader could see incorrect ifindex when
927 * processing a flying packet, but that is not a problem). Write guarded
930 static void team_port_enable(struct team *team,
931 struct team_port *port)
933 if (team_port_enabled(port))
935 port->index = team->en_port_count++;
936 hlist_add_head_rcu(&port->hlist,
937 team_port_index_hash(team, port->index));
938 team_adjust_ops(team);
939 team_queue_override_port_add(team, port);
940 if (team->ops.port_enabled)
941 team->ops.port_enabled(team, port);
942 team_notify_peers(team);
943 team_mcast_rejoin(team);
944 team_lower_state_changed(port);
947 static void __reconstruct_port_hlist(struct team *team, int rm_index)
950 struct team_port *port;
952 for (i = rm_index + 1; i < team->en_port_count; i++) {
953 port = team_get_port_by_index(team, i);
954 hlist_del_rcu(&port->hlist);
956 hlist_add_head_rcu(&port->hlist,
957 team_port_index_hash(team, port->index));
961 static void team_port_disable(struct team *team,
962 struct team_port *port)
964 if (!team_port_enabled(port))
966 if (team->ops.port_disabled)
967 team->ops.port_disabled(team, port);
968 hlist_del_rcu(&port->hlist);
969 __reconstruct_port_hlist(team, port->index);
971 team->en_port_count--;
972 team_queue_override_port_del(team, port);
973 team_adjust_ops(team);
974 team_lower_state_changed(port);
977 #define TEAM_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
978 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
979 NETIF_F_HIGHDMA | NETIF_F_LRO)
981 #define TEAM_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
982 NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
984 static void __team_compute_features(struct team *team)
986 struct team_port *port;
987 netdev_features_t vlan_features = TEAM_VLAN_FEATURES &
989 netdev_features_t enc_features = TEAM_ENC_FEATURES;
990 unsigned short max_hard_header_len = ETH_HLEN;
991 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
992 IFF_XMIT_DST_RELEASE_PERM;
994 list_for_each_entry(port, &team->port_list, list) {
995 vlan_features = netdev_increment_features(vlan_features,
996 port->dev->vlan_features,
999 netdev_increment_features(enc_features,
1000 port->dev->hw_enc_features,
1004 dst_release_flag &= port->dev->priv_flags;
1005 if (port->dev->hard_header_len > max_hard_header_len)
1006 max_hard_header_len = port->dev->hard_header_len;
1009 team->dev->vlan_features = vlan_features;
1010 team->dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
1012 team->dev->hard_header_len = max_hard_header_len;
1014 team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1015 if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1016 team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1019 static void team_compute_features(struct team *team)
1021 mutex_lock(&team->lock);
1022 __team_compute_features(team);
1023 mutex_unlock(&team->lock);
1024 netdev_change_features(team->dev);
1027 static int team_port_enter(struct team *team, struct team_port *port)
1031 dev_hold(team->dev);
1032 if (team->ops.port_enter) {
1033 err = team->ops.port_enter(team, port);
1035 netdev_err(team->dev, "Device %s failed to enter team mode\n",
1037 goto err_port_enter;
1049 static void team_port_leave(struct team *team, struct team_port *port)
1051 if (team->ops.port_leave)
1052 team->ops.port_leave(team, port);
1056 #ifdef CONFIG_NET_POLL_CONTROLLER
1057 static int __team_port_enable_netpoll(struct team_port *port)
1062 np = kzalloc(sizeof(*np), GFP_KERNEL);
1066 err = __netpoll_setup(np, port->dev);
1075 static int team_port_enable_netpoll(struct team_port *port)
1077 if (!port->team->dev->npinfo)
1080 return __team_port_enable_netpoll(port);
1083 static void team_port_disable_netpoll(struct team_port *port)
1085 struct netpoll *np = port->np;
1094 static int team_port_enable_netpoll(struct team_port *port)
1098 static void team_port_disable_netpoll(struct team_port *port)
1103 static int team_upper_dev_link(struct team *team, struct team_port *port,
1104 struct netlink_ext_ack *extack)
1106 struct netdev_lag_upper_info lag_upper_info;
1109 lag_upper_info.tx_type = team->mode->lag_tx_type;
1110 lag_upper_info.hash_type = NETDEV_LAG_HASH_UNKNOWN;
1111 err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
1112 &lag_upper_info, extack);
1115 port->dev->priv_flags |= IFF_TEAM_PORT;
1119 static void team_upper_dev_unlink(struct team *team, struct team_port *port)
1121 netdev_upper_dev_unlink(port->dev, team->dev);
1122 port->dev->priv_flags &= ~IFF_TEAM_PORT;
1125 static void __team_port_change_port_added(struct team_port *port, bool linkup);
1126 static int team_dev_type_check_change(struct net_device *dev,
1127 struct net_device *port_dev);
1129 static int team_port_add(struct team *team, struct net_device *port_dev,
1130 struct netlink_ext_ack *extack)
1132 struct net_device *dev = team->dev;
1133 struct team_port *port;
1134 char *portname = port_dev->name;
1137 if (port_dev->flags & IFF_LOOPBACK) {
1138 NL_SET_ERR_MSG(extack, "Loopback device can't be added as a team port");
1139 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
1144 if (netif_is_team_port(port_dev)) {
1145 NL_SET_ERR_MSG(extack, "Device is already a port of a team device");
1146 netdev_err(dev, "Device %s is already a port "
1147 "of a team device\n", portname);
1151 if (dev == port_dev) {
1152 NL_SET_ERR_MSG(extack, "Cannot enslave team device to itself");
1153 netdev_err(dev, "Cannot enslave team device to itself\n");
1157 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
1158 vlan_uses_dev(dev)) {
1159 NL_SET_ERR_MSG(extack, "Device is VLAN challenged and team device has VLAN set up");
1160 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1165 err = team_dev_type_check_change(dev, port_dev);
1169 if (port_dev->flags & IFF_UP) {
1170 NL_SET_ERR_MSG(extack, "Device is up. Set it down before adding it as a team port");
1171 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1176 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1181 port->dev = port_dev;
1183 INIT_LIST_HEAD(&port->qom_list);
1185 port->orig.mtu = port_dev->mtu;
1186 err = dev_set_mtu(port_dev, dev->mtu);
1188 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1192 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1194 err = team_port_enter(team, port);
1196 netdev_err(dev, "Device %s failed to enter team mode\n",
1198 goto err_port_enter;
1201 err = dev_open(port_dev, extack);
1203 netdev_dbg(dev, "Device %s opening failed\n",
1208 err = vlan_vids_add_by_dev(port_dev, dev);
1210 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1215 err = team_port_enable_netpoll(port);
1217 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1219 goto err_enable_netpoll;
1222 if (!(dev->features & NETIF_F_LRO))
1223 dev_disable_lro(port_dev);
1225 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1228 netdev_err(dev, "Device %s failed to register rx_handler\n",
1230 goto err_handler_register;
1233 err = team_upper_dev_link(team, port, extack);
1235 netdev_err(dev, "Device %s failed to set upper link\n",
1237 goto err_set_upper_link;
1240 err = __team_option_inst_add_port(team, port);
1242 netdev_err(dev, "Device %s failed to add per-port options\n",
1244 goto err_option_port_add;
1247 /* set promiscuity level to new slave */
1248 if (dev->flags & IFF_PROMISC) {
1249 err = dev_set_promiscuity(port_dev, 1);
1251 goto err_set_slave_promisc;
1254 /* set allmulti level to new slave */
1255 if (dev->flags & IFF_ALLMULTI) {
1256 err = dev_set_allmulti(port_dev, 1);
1258 if (dev->flags & IFF_PROMISC)
1259 dev_set_promiscuity(port_dev, -1);
1260 goto err_set_slave_promisc;
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);
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_oper_up(port_dev));
1274 __team_options_change_check(team);
1276 netdev_info(dev, "Port device %s added\n", portname);
1280 err_set_slave_promisc:
1281 __team_option_inst_del_port(team, port);
1283 err_option_port_add:
1284 team_upper_dev_unlink(team, port);
1287 netdev_rx_handler_unregister(port_dev);
1289 err_handler_register:
1290 team_port_disable_netpoll(port);
1293 vlan_vids_del_by_dev(port_dev, dev);
1296 dev_close(port_dev);
1299 team_port_leave(team, port);
1300 team_port_set_orig_dev_addr(port);
1303 dev_set_mtu(port_dev, port->orig.mtu);
1311 static void __team_port_change_port_removed(struct team_port *port);
1313 static int team_port_del(struct team *team, struct net_device *port_dev)
1315 struct net_device *dev = team->dev;
1316 struct team_port *port;
1317 char *portname = port_dev->name;
1319 port = team_port_get_rtnl(port_dev);
1320 if (!port || !team_port_find(team, port)) {
1321 netdev_err(dev, "Device %s does not act as a port of this team\n",
1326 team_port_disable(team, port);
1327 list_del_rcu(&port->list);
1329 if (dev->flags & IFF_PROMISC)
1330 dev_set_promiscuity(port_dev, -1);
1331 if (dev->flags & IFF_ALLMULTI)
1332 dev_set_allmulti(port_dev, -1);
1334 team_upper_dev_unlink(team, port);
1335 netdev_rx_handler_unregister(port_dev);
1336 team_port_disable_netpoll(port);
1337 vlan_vids_del_by_dev(port_dev, dev);
1338 dev_uc_unsync(port_dev, dev);
1339 dev_mc_unsync(port_dev, dev);
1340 dev_close(port_dev);
1341 team_port_leave(team, port);
1343 __team_option_inst_mark_removed_port(team, port);
1344 __team_options_change_check(team);
1345 __team_option_inst_del_port(team, port);
1346 __team_port_change_port_removed(port);
1348 team_port_set_orig_dev_addr(port);
1349 dev_set_mtu(port_dev, port->orig.mtu);
1350 kfree_rcu(port, rcu);
1351 netdev_info(dev, "Port device %s removed\n", portname);
1352 __team_compute_features(team);
1362 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1364 ctx->data.str_val = team->mode->kind;
1368 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1370 return team_change_mode(team, ctx->data.str_val);
1373 static int team_notify_peers_count_get(struct team *team,
1374 struct team_gsetter_ctx *ctx)
1376 ctx->data.u32_val = team->notify_peers.count;
1380 static int team_notify_peers_count_set(struct team *team,
1381 struct team_gsetter_ctx *ctx)
1383 team->notify_peers.count = ctx->data.u32_val;
1387 static int team_notify_peers_interval_get(struct team *team,
1388 struct team_gsetter_ctx *ctx)
1390 ctx->data.u32_val = team->notify_peers.interval;
1394 static int team_notify_peers_interval_set(struct team *team,
1395 struct team_gsetter_ctx *ctx)
1397 team->notify_peers.interval = ctx->data.u32_val;
1401 static int team_mcast_rejoin_count_get(struct team *team,
1402 struct team_gsetter_ctx *ctx)
1404 ctx->data.u32_val = team->mcast_rejoin.count;
1408 static int team_mcast_rejoin_count_set(struct team *team,
1409 struct team_gsetter_ctx *ctx)
1411 team->mcast_rejoin.count = ctx->data.u32_val;
1415 static int team_mcast_rejoin_interval_get(struct team *team,
1416 struct team_gsetter_ctx *ctx)
1418 ctx->data.u32_val = team->mcast_rejoin.interval;
1422 static int team_mcast_rejoin_interval_set(struct team *team,
1423 struct team_gsetter_ctx *ctx)
1425 team->mcast_rejoin.interval = ctx->data.u32_val;
1429 static int team_port_en_option_get(struct team *team,
1430 struct team_gsetter_ctx *ctx)
1432 struct team_port *port = ctx->info->port;
1434 ctx->data.bool_val = team_port_enabled(port);
1438 static int team_port_en_option_set(struct team *team,
1439 struct team_gsetter_ctx *ctx)
1441 struct team_port *port = ctx->info->port;
1443 if (ctx->data.bool_val)
1444 team_port_enable(team, port);
1446 team_port_disable(team, port);
1450 static int team_user_linkup_option_get(struct team *team,
1451 struct team_gsetter_ctx *ctx)
1453 struct team_port *port = ctx->info->port;
1455 ctx->data.bool_val = port->user.linkup;
1459 static void __team_carrier_check(struct team *team);
1461 static int team_user_linkup_option_set(struct team *team,
1462 struct team_gsetter_ctx *ctx)
1464 struct team_port *port = ctx->info->port;
1466 port->user.linkup = ctx->data.bool_val;
1467 team_refresh_port_linkup(port);
1468 __team_carrier_check(port->team);
1472 static int team_user_linkup_en_option_get(struct team *team,
1473 struct team_gsetter_ctx *ctx)
1475 struct team_port *port = ctx->info->port;
1477 ctx->data.bool_val = port->user.linkup_enabled;
1481 static int team_user_linkup_en_option_set(struct team *team,
1482 struct team_gsetter_ctx *ctx)
1484 struct team_port *port = ctx->info->port;
1486 port->user.linkup_enabled = ctx->data.bool_val;
1487 team_refresh_port_linkup(port);
1488 __team_carrier_check(port->team);
1492 static int team_priority_option_get(struct team *team,
1493 struct team_gsetter_ctx *ctx)
1495 struct team_port *port = ctx->info->port;
1497 ctx->data.s32_val = port->priority;
1501 static int team_priority_option_set(struct team *team,
1502 struct team_gsetter_ctx *ctx)
1504 struct team_port *port = ctx->info->port;
1505 s32 priority = ctx->data.s32_val;
1507 if (port->priority == priority)
1509 port->priority = priority;
1510 team_queue_override_port_prio_changed(team, port);
1514 static int team_queue_id_option_get(struct team *team,
1515 struct team_gsetter_ctx *ctx)
1517 struct team_port *port = ctx->info->port;
1519 ctx->data.u32_val = port->queue_id;
1523 static int team_queue_id_option_set(struct team *team,
1524 struct team_gsetter_ctx *ctx)
1526 struct team_port *port = ctx->info->port;
1527 u16 new_queue_id = ctx->data.u32_val;
1529 if (port->queue_id == new_queue_id)
1531 if (new_queue_id >= team->dev->real_num_tx_queues)
1533 team_queue_override_port_change_queue_id(team, port, new_queue_id);
1537 static const struct team_option team_options[] = {
1540 .type = TEAM_OPTION_TYPE_STRING,
1541 .getter = team_mode_option_get,
1542 .setter = team_mode_option_set,
1545 .name = "notify_peers_count",
1546 .type = TEAM_OPTION_TYPE_U32,
1547 .getter = team_notify_peers_count_get,
1548 .setter = team_notify_peers_count_set,
1551 .name = "notify_peers_interval",
1552 .type = TEAM_OPTION_TYPE_U32,
1553 .getter = team_notify_peers_interval_get,
1554 .setter = team_notify_peers_interval_set,
1557 .name = "mcast_rejoin_count",
1558 .type = TEAM_OPTION_TYPE_U32,
1559 .getter = team_mcast_rejoin_count_get,
1560 .setter = team_mcast_rejoin_count_set,
1563 .name = "mcast_rejoin_interval",
1564 .type = TEAM_OPTION_TYPE_U32,
1565 .getter = team_mcast_rejoin_interval_get,
1566 .setter = team_mcast_rejoin_interval_set,
1570 .type = TEAM_OPTION_TYPE_BOOL,
1572 .getter = team_port_en_option_get,
1573 .setter = team_port_en_option_set,
1576 .name = "user_linkup",
1577 .type = TEAM_OPTION_TYPE_BOOL,
1579 .getter = team_user_linkup_option_get,
1580 .setter = team_user_linkup_option_set,
1583 .name = "user_linkup_enabled",
1584 .type = TEAM_OPTION_TYPE_BOOL,
1586 .getter = team_user_linkup_en_option_get,
1587 .setter = team_user_linkup_en_option_set,
1591 .type = TEAM_OPTION_TYPE_S32,
1593 .getter = team_priority_option_get,
1594 .setter = team_priority_option_set,
1598 .type = TEAM_OPTION_TYPE_U32,
1600 .getter = team_queue_id_option_get,
1601 .setter = team_queue_id_option_set,
1606 static int team_init(struct net_device *dev)
1608 struct team *team = netdev_priv(dev);
1613 mutex_init(&team->lock);
1614 team_set_no_mode(team);
1616 team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
1617 if (!team->pcpu_stats)
1620 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1621 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1622 INIT_LIST_HEAD(&team->port_list);
1623 err = team_queue_override_init(team);
1625 goto err_team_queue_override_init;
1627 team_adjust_ops(team);
1629 INIT_LIST_HEAD(&team->option_list);
1630 INIT_LIST_HEAD(&team->option_inst_list);
1632 team_notify_peers_init(team);
1633 team_mcast_rejoin_init(team);
1635 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1637 goto err_options_register;
1638 netif_carrier_off(dev);
1640 netdev_lockdep_set_classes(dev);
1644 err_options_register:
1645 team_mcast_rejoin_fini(team);
1646 team_notify_peers_fini(team);
1647 team_queue_override_fini(team);
1648 err_team_queue_override_init:
1649 free_percpu(team->pcpu_stats);
1654 static void team_uninit(struct net_device *dev)
1656 struct team *team = netdev_priv(dev);
1657 struct team_port *port;
1658 struct team_port *tmp;
1660 mutex_lock(&team->lock);
1661 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1662 team_port_del(team, port->dev);
1664 __team_change_mode(team, NULL); /* cleanup */
1665 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1666 team_mcast_rejoin_fini(team);
1667 team_notify_peers_fini(team);
1668 team_queue_override_fini(team);
1669 mutex_unlock(&team->lock);
1670 netdev_change_features(dev);
1673 static void team_destructor(struct net_device *dev)
1675 struct team *team = netdev_priv(dev);
1677 free_percpu(team->pcpu_stats);
1680 static int team_open(struct net_device *dev)
1685 static int team_close(struct net_device *dev)
1691 * note: already called with rcu_read_lock
1693 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1695 struct team *team = netdev_priv(dev);
1697 unsigned int len = skb->len;
1699 tx_success = team_queue_override_transmit(team, skb);
1701 tx_success = team->ops.transmit(team, skb);
1703 struct team_pcpu_stats *pcpu_stats;
1705 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1706 u64_stats_update_begin(&pcpu_stats->syncp);
1707 pcpu_stats->tx_packets++;
1708 pcpu_stats->tx_bytes += len;
1709 u64_stats_update_end(&pcpu_stats->syncp);
1711 this_cpu_inc(team->pcpu_stats->tx_dropped);
1714 return NETDEV_TX_OK;
1717 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
1718 struct net_device *sb_dev)
1721 * This helper function exists to help dev_pick_tx get the correct
1722 * destination queue. Using a helper function skips a call to
1723 * skb_tx_hash and will put the skbs in the queue we expect on their
1724 * way down to the team driver.
1726 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1729 * Save the original txq to restore before passing to the driver
1731 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1733 if (unlikely(txq >= dev->real_num_tx_queues)) {
1735 txq -= dev->real_num_tx_queues;
1736 } while (txq >= dev->real_num_tx_queues);
1741 static void team_change_rx_flags(struct net_device *dev, int change)
1743 struct team *team = netdev_priv(dev);
1744 struct team_port *port;
1748 list_for_each_entry_rcu(port, &team->port_list, list) {
1749 if (change & IFF_PROMISC) {
1750 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1751 dev_set_promiscuity(port->dev, inc);
1753 if (change & IFF_ALLMULTI) {
1754 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1755 dev_set_allmulti(port->dev, inc);
1761 static void team_set_rx_mode(struct net_device *dev)
1763 struct team *team = netdev_priv(dev);
1764 struct team_port *port;
1767 list_for_each_entry_rcu(port, &team->port_list, list) {
1768 dev_uc_sync_multiple(port->dev, dev);
1769 dev_mc_sync_multiple(port->dev, dev);
1774 static int team_set_mac_address(struct net_device *dev, void *p)
1776 struct sockaddr *addr = p;
1777 struct team *team = netdev_priv(dev);
1778 struct team_port *port;
1780 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1781 return -EADDRNOTAVAIL;
1782 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1783 mutex_lock(&team->lock);
1784 list_for_each_entry(port, &team->port_list, list)
1785 if (team->ops.port_change_dev_addr)
1786 team->ops.port_change_dev_addr(team, port);
1787 mutex_unlock(&team->lock);
1791 static int team_change_mtu(struct net_device *dev, int new_mtu)
1793 struct team *team = netdev_priv(dev);
1794 struct team_port *port;
1798 * Alhough this is reader, it's guarded by team lock. It's not possible
1799 * to traverse list in reverse under rcu_read_lock
1801 mutex_lock(&team->lock);
1802 team->port_mtu_change_allowed = true;
1803 list_for_each_entry(port, &team->port_list, list) {
1804 err = dev_set_mtu(port->dev, new_mtu);
1806 netdev_err(dev, "Device %s failed to change mtu",
1811 team->port_mtu_change_allowed = false;
1812 mutex_unlock(&team->lock);
1819 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1820 dev_set_mtu(port->dev, dev->mtu);
1821 team->port_mtu_change_allowed = false;
1822 mutex_unlock(&team->lock);
1828 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1830 struct team *team = netdev_priv(dev);
1831 struct team_pcpu_stats *p;
1832 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1833 u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
1837 for_each_possible_cpu(i) {
1838 p = per_cpu_ptr(team->pcpu_stats, i);
1840 start = u64_stats_fetch_begin_irq(&p->syncp);
1841 rx_packets = p->rx_packets;
1842 rx_bytes = p->rx_bytes;
1843 rx_multicast = p->rx_multicast;
1844 tx_packets = p->tx_packets;
1845 tx_bytes = p->tx_bytes;
1846 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1848 stats->rx_packets += rx_packets;
1849 stats->rx_bytes += rx_bytes;
1850 stats->multicast += rx_multicast;
1851 stats->tx_packets += tx_packets;
1852 stats->tx_bytes += tx_bytes;
1854 * rx_dropped, tx_dropped & rx_nohandler are u32,
1855 * updated without syncp protection.
1857 rx_dropped += p->rx_dropped;
1858 tx_dropped += p->tx_dropped;
1859 rx_nohandler += p->rx_nohandler;
1861 stats->rx_dropped = rx_dropped;
1862 stats->tx_dropped = tx_dropped;
1863 stats->rx_nohandler = rx_nohandler;
1866 static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1868 struct team *team = netdev_priv(dev);
1869 struct team_port *port;
1873 * Alhough this is reader, it's guarded by team lock. It's not possible
1874 * to traverse list in reverse under rcu_read_lock
1876 mutex_lock(&team->lock);
1877 list_for_each_entry(port, &team->port_list, list) {
1878 err = vlan_vid_add(port->dev, proto, vid);
1882 mutex_unlock(&team->lock);
1887 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1888 vlan_vid_del(port->dev, proto, vid);
1889 mutex_unlock(&team->lock);
1894 static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1896 struct team *team = netdev_priv(dev);
1897 struct team_port *port;
1899 mutex_lock(&team->lock);
1900 list_for_each_entry(port, &team->port_list, list)
1901 vlan_vid_del(port->dev, proto, vid);
1902 mutex_unlock(&team->lock);
1907 #ifdef CONFIG_NET_POLL_CONTROLLER
1908 static void team_poll_controller(struct net_device *dev)
1912 static void __team_netpoll_cleanup(struct team *team)
1914 struct team_port *port;
1916 list_for_each_entry(port, &team->port_list, list)
1917 team_port_disable_netpoll(port);
1920 static void team_netpoll_cleanup(struct net_device *dev)
1922 struct team *team = netdev_priv(dev);
1924 mutex_lock(&team->lock);
1925 __team_netpoll_cleanup(team);
1926 mutex_unlock(&team->lock);
1929 static int team_netpoll_setup(struct net_device *dev,
1930 struct netpoll_info *npifo)
1932 struct team *team = netdev_priv(dev);
1933 struct team_port *port;
1936 mutex_lock(&team->lock);
1937 list_for_each_entry(port, &team->port_list, list) {
1938 err = __team_port_enable_netpoll(port);
1940 __team_netpoll_cleanup(team);
1944 mutex_unlock(&team->lock);
1949 static int team_add_slave(struct net_device *dev, struct net_device *port_dev,
1950 struct netlink_ext_ack *extack)
1952 struct team *team = netdev_priv(dev);
1955 mutex_lock(&team->lock);
1956 err = team_port_add(team, port_dev, extack);
1957 mutex_unlock(&team->lock);
1960 netdev_change_features(dev);
1965 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1967 struct team *team = netdev_priv(dev);
1970 mutex_lock(&team->lock);
1971 err = team_port_del(team, port_dev);
1972 mutex_unlock(&team->lock);
1975 netdev_change_features(dev);
1980 static netdev_features_t team_fix_features(struct net_device *dev,
1981 netdev_features_t features)
1983 struct team_port *port;
1984 struct team *team = netdev_priv(dev);
1985 netdev_features_t mask;
1988 features &= ~NETIF_F_ONE_FOR_ALL;
1989 features |= NETIF_F_ALL_FOR_ALL;
1992 list_for_each_entry_rcu(port, &team->port_list, list) {
1993 features = netdev_increment_features(features,
1994 port->dev->features,
1999 features = netdev_add_tso_features(features, mask);
2004 static int team_change_carrier(struct net_device *dev, bool new_carrier)
2006 struct team *team = netdev_priv(dev);
2008 team->user_carrier_enabled = true;
2011 netif_carrier_on(dev);
2013 netif_carrier_off(dev);
2017 static const struct net_device_ops team_netdev_ops = {
2018 .ndo_init = team_init,
2019 .ndo_uninit = team_uninit,
2020 .ndo_open = team_open,
2021 .ndo_stop = team_close,
2022 .ndo_start_xmit = team_xmit,
2023 .ndo_select_queue = team_select_queue,
2024 .ndo_change_rx_flags = team_change_rx_flags,
2025 .ndo_set_rx_mode = team_set_rx_mode,
2026 .ndo_set_mac_address = team_set_mac_address,
2027 .ndo_change_mtu = team_change_mtu,
2028 .ndo_get_stats64 = team_get_stats64,
2029 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
2030 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
2031 #ifdef CONFIG_NET_POLL_CONTROLLER
2032 .ndo_poll_controller = team_poll_controller,
2033 .ndo_netpoll_setup = team_netpoll_setup,
2034 .ndo_netpoll_cleanup = team_netpoll_cleanup,
2036 .ndo_add_slave = team_add_slave,
2037 .ndo_del_slave = team_del_slave,
2038 .ndo_fix_features = team_fix_features,
2039 .ndo_change_carrier = team_change_carrier,
2040 .ndo_features_check = passthru_features_check,
2043 /***********************
2045 ***********************/
2047 static void team_ethtool_get_drvinfo(struct net_device *dev,
2048 struct ethtool_drvinfo *drvinfo)
2050 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
2051 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2054 static const struct ethtool_ops team_ethtool_ops = {
2055 .get_drvinfo = team_ethtool_get_drvinfo,
2056 .get_link = ethtool_op_get_link,
2059 /***********************
2060 * rt netlink interface
2061 ***********************/
2063 static void team_setup_by_port(struct net_device *dev,
2064 struct net_device *port_dev)
2066 dev->header_ops = port_dev->header_ops;
2067 dev->type = port_dev->type;
2068 dev->hard_header_len = port_dev->hard_header_len;
2069 dev->addr_len = port_dev->addr_len;
2070 dev->mtu = port_dev->mtu;
2071 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
2072 eth_hw_addr_inherit(dev, port_dev);
2075 static int team_dev_type_check_change(struct net_device *dev,
2076 struct net_device *port_dev)
2078 struct team *team = netdev_priv(dev);
2079 char *portname = port_dev->name;
2082 if (dev->type == port_dev->type)
2084 if (!list_empty(&team->port_list)) {
2085 netdev_err(dev, "Device %s is of different type\n", portname);
2088 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
2089 err = notifier_to_errno(err);
2091 netdev_err(dev, "Refused to change device type\n");
2096 team_setup_by_port(dev, port_dev);
2097 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
2101 static void team_setup(struct net_device *dev)
2104 dev->max_mtu = ETH_MAX_MTU;
2106 dev->netdev_ops = &team_netdev_ops;
2107 dev->ethtool_ops = &team_ethtool_ops;
2108 dev->needs_free_netdev = true;
2109 dev->priv_destructor = team_destructor;
2110 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
2111 dev->priv_flags |= IFF_NO_QUEUE;
2112 dev->priv_flags |= IFF_TEAM;
2115 * Indicate we support unicast address filtering. That way core won't
2116 * bring us to promisc mode in case a unicast addr is added.
2117 * Let this up to underlay drivers.
2119 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
2121 dev->features |= NETIF_F_LLTX;
2122 dev->features |= NETIF_F_GRO;
2124 /* Don't allow team devices to change network namespaces. */
2125 dev->features |= NETIF_F_NETNS_LOCAL;
2127 dev->hw_features = TEAM_VLAN_FEATURES |
2128 NETIF_F_HW_VLAN_CTAG_TX |
2129 NETIF_F_HW_VLAN_CTAG_RX |
2130 NETIF_F_HW_VLAN_CTAG_FILTER;
2132 dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
2133 dev->features |= dev->hw_features;
2136 static int team_newlink(struct net *src_net, struct net_device *dev,
2137 struct nlattr *tb[], struct nlattr *data[],
2138 struct netlink_ext_ack *extack)
2140 if (tb[IFLA_ADDRESS] == NULL)
2141 eth_hw_addr_random(dev);
2143 return register_netdevice(dev);
2146 static int team_validate(struct nlattr *tb[], struct nlattr *data[],
2147 struct netlink_ext_ack *extack)
2149 if (tb[IFLA_ADDRESS]) {
2150 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
2152 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
2153 return -EADDRNOTAVAIL;
2158 static unsigned int team_get_num_tx_queues(void)
2160 return TEAM_DEFAULT_NUM_TX_QUEUES;
2163 static unsigned int team_get_num_rx_queues(void)
2165 return TEAM_DEFAULT_NUM_RX_QUEUES;
2168 static struct rtnl_link_ops team_link_ops __read_mostly = {
2170 .priv_size = sizeof(struct team),
2171 .setup = team_setup,
2172 .newlink = team_newlink,
2173 .validate = team_validate,
2174 .get_num_tx_queues = team_get_num_tx_queues,
2175 .get_num_rx_queues = team_get_num_rx_queues,
2179 /***********************************
2180 * Generic netlink custom interface
2181 ***********************************/
2183 static struct genl_family team_nl_family;
2185 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
2186 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
2187 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
2188 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
2189 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
2192 static const struct nla_policy
2193 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
2194 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
2195 [TEAM_ATTR_OPTION_NAME] = {
2197 .len = TEAM_STRING_MAX_LEN,
2199 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
2200 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
2201 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
2204 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
2206 struct sk_buff *msg;
2210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2214 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
2215 &team_nl_family, 0, TEAM_CMD_NOOP);
2221 genlmsg_end(msg, hdr);
2223 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
2232 * Netlink cmd functions should be locked by following two functions.
2233 * Since dev gets held here, that ensures dev won't disappear in between.
2235 static struct team *team_nl_team_get(struct genl_info *info)
2237 struct net *net = genl_info_net(info);
2239 struct net_device *dev;
2242 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
2245 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
2246 dev = dev_get_by_index(net, ifindex);
2247 if (!dev || dev->netdev_ops != &team_netdev_ops) {
2253 team = netdev_priv(dev);
2254 mutex_lock(&team->lock);
2258 static void team_nl_team_put(struct team *team)
2260 mutex_unlock(&team->lock);
2264 typedef int team_nl_send_func_t(struct sk_buff *skb,
2265 struct team *team, u32 portid);
2267 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2269 return genlmsg_unicast(dev_net(team->dev), skb, portid);
2272 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2273 struct team_option_inst *opt_inst)
2275 struct nlattr *option_item;
2276 struct team_option *option = opt_inst->option;
2277 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2278 struct team_gsetter_ctx ctx;
2281 ctx.info = opt_inst_info;
2282 err = team_option_get(team, opt_inst, &ctx);
2286 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2290 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2292 if (opt_inst_info->port &&
2293 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2294 opt_inst_info->port->dev->ifindex))
2296 if (opt_inst->option->array_size &&
2297 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2298 opt_inst_info->array_index))
2301 switch (option->type) {
2302 case TEAM_OPTION_TYPE_U32:
2303 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2305 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2308 case TEAM_OPTION_TYPE_STRING:
2309 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2311 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2315 case TEAM_OPTION_TYPE_BINARY:
2316 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2318 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2319 ctx.data.bin_val.ptr))
2322 case TEAM_OPTION_TYPE_BOOL:
2323 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2325 if (ctx.data.bool_val &&
2326 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2329 case TEAM_OPTION_TYPE_S32:
2330 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2332 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2338 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2340 if (opt_inst->changed) {
2341 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2343 opt_inst->changed = false;
2345 nla_nest_end(skb, option_item);
2349 nla_nest_cancel(skb, option_item);
2353 static int __send_and_alloc_skb(struct sk_buff **pskb,
2354 struct team *team, u32 portid,
2355 team_nl_send_func_t *send_func)
2360 err = send_func(*pskb, team, portid);
2364 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2370 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2371 int flags, team_nl_send_func_t *send_func,
2372 struct list_head *sel_opt_inst_list)
2374 struct nlattr *option_list;
2375 struct nlmsghdr *nlh;
2377 struct team_option_inst *opt_inst;
2379 struct sk_buff *skb = NULL;
2383 opt_inst = list_first_entry(sel_opt_inst_list,
2384 struct team_option_inst, tmp_list);
2387 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2391 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2392 TEAM_CMD_OPTIONS_GET);
2398 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2399 goto nla_put_failure;
2400 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2402 goto nla_put_failure;
2406 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2407 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2409 if (err == -EMSGSIZE) {
2420 nla_nest_end(skb, option_list);
2421 genlmsg_end(skb, hdr);
2426 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2428 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2434 return send_func(skb, team, portid);
2443 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2446 struct team_option_inst *opt_inst;
2448 LIST_HEAD(sel_opt_inst_list);
2450 team = team_nl_team_get(info);
2454 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2455 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2456 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2457 NLM_F_ACK, team_nl_send_unicast,
2458 &sel_opt_inst_list);
2460 team_nl_team_put(team);
2465 static int team_nl_send_event_options_get(struct team *team,
2466 struct list_head *sel_opt_inst_list);
2468 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2473 struct nlattr *nl_option;
2477 team = team_nl_team_get(info);
2484 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2489 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2490 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2491 struct nlattr *attr;
2492 struct nlattr *attr_data;
2493 LIST_HEAD(opt_inst_list);
2494 enum team_option_type opt_type;
2495 int opt_port_ifindex = 0; /* != 0 for per-port options */
2496 u32 opt_array_index = 0;
2497 bool opt_is_array = false;
2498 struct team_option_inst *opt_inst;
2500 bool opt_found = false;
2502 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2506 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2507 nl_option, team_nl_option_policy,
2511 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2512 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2516 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2518 opt_type = TEAM_OPTION_TYPE_U32;
2521 opt_type = TEAM_OPTION_TYPE_STRING;
2524 opt_type = TEAM_OPTION_TYPE_BINARY;
2527 opt_type = TEAM_OPTION_TYPE_BOOL;
2530 opt_type = TEAM_OPTION_TYPE_S32;
2536 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2537 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2542 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2543 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2545 opt_port_ifindex = nla_get_u32(attr);
2547 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2549 opt_is_array = true;
2550 opt_array_index = nla_get_u32(attr);
2553 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2554 struct team_option *option = opt_inst->option;
2555 struct team_gsetter_ctx ctx;
2556 struct team_option_inst_info *opt_inst_info;
2559 opt_inst_info = &opt_inst->info;
2560 tmp_ifindex = opt_inst_info->port ?
2561 opt_inst_info->port->dev->ifindex : 0;
2562 if (option->type != opt_type ||
2563 strcmp(option->name, opt_name) ||
2564 tmp_ifindex != opt_port_ifindex ||
2565 (option->array_size && !opt_is_array) ||
2566 opt_inst_info->array_index != opt_array_index)
2569 ctx.info = opt_inst_info;
2571 case TEAM_OPTION_TYPE_U32:
2572 ctx.data.u32_val = nla_get_u32(attr_data);
2574 case TEAM_OPTION_TYPE_STRING:
2575 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2579 ctx.data.str_val = nla_data(attr_data);
2581 case TEAM_OPTION_TYPE_BINARY:
2582 ctx.data.bin_val.len = nla_len(attr_data);
2583 ctx.data.bin_val.ptr = nla_data(attr_data);
2585 case TEAM_OPTION_TYPE_BOOL:
2586 ctx.data.bool_val = attr_data ? true : false;
2588 case TEAM_OPTION_TYPE_S32:
2589 ctx.data.s32_val = nla_get_s32(attr_data);
2594 err = team_option_set(team, opt_inst, &ctx);
2597 opt_inst->changed = true;
2598 list_add(&opt_inst->tmp_list, &opt_inst_list);
2605 err = team_nl_send_event_options_get(team, &opt_inst_list);
2611 team_nl_team_put(team);
2617 static int team_nl_fill_one_port_get(struct sk_buff *skb,
2618 struct team_port *port)
2620 struct nlattr *port_item;
2622 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2625 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2627 if (port->changed) {
2628 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2630 port->changed = false;
2632 if ((port->removed &&
2633 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2634 (port->state.linkup &&
2635 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2636 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2637 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2639 nla_nest_end(skb, port_item);
2643 nla_nest_cancel(skb, port_item);
2647 static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
2648 int flags, team_nl_send_func_t *send_func,
2649 struct team_port *one_port)
2651 struct nlattr *port_list;
2652 struct nlmsghdr *nlh;
2654 struct team_port *port;
2656 struct sk_buff *skb = NULL;
2660 port = list_first_entry_or_null(&team->port_list,
2661 struct team_port, list);
2664 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2668 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2669 TEAM_CMD_PORT_LIST_GET);
2675 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2676 goto nla_put_failure;
2677 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2679 goto nla_put_failure;
2684 /* If one port is selected, called wants to send port list containing
2685 * only this port. Otherwise go through all listed ports and send all
2688 err = team_nl_fill_one_port_get(skb, one_port);
2692 list_for_each_entry_from(port, &team->port_list, list) {
2693 err = team_nl_fill_one_port_get(skb, port);
2695 if (err == -EMSGSIZE) {
2707 nla_nest_end(skb, port_list);
2708 genlmsg_end(skb, hdr);
2713 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2715 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2721 return send_func(skb, team, portid);
2730 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2731 struct genl_info *info)
2736 team = team_nl_team_get(info);
2740 err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
2741 NLM_F_ACK, team_nl_send_unicast, NULL);
2743 team_nl_team_put(team);
2748 static const struct genl_ops team_nl_ops[] = {
2750 .cmd = TEAM_CMD_NOOP,
2751 .doit = team_nl_cmd_noop,
2754 .cmd = TEAM_CMD_OPTIONS_SET,
2755 .doit = team_nl_cmd_options_set,
2756 .flags = GENL_ADMIN_PERM,
2759 .cmd = TEAM_CMD_OPTIONS_GET,
2760 .doit = team_nl_cmd_options_get,
2761 .flags = GENL_ADMIN_PERM,
2764 .cmd = TEAM_CMD_PORT_LIST_GET,
2765 .doit = team_nl_cmd_port_list_get,
2766 .flags = GENL_ADMIN_PERM,
2770 static const struct genl_multicast_group team_nl_mcgrps[] = {
2771 { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
2774 static struct genl_family team_nl_family __ro_after_init = {
2775 .name = TEAM_GENL_NAME,
2776 .version = TEAM_GENL_VERSION,
2777 .maxattr = TEAM_ATTR_MAX,
2778 .policy = team_nl_policy,
2780 .module = THIS_MODULE,
2782 .n_ops = ARRAY_SIZE(team_nl_ops),
2783 .mcgrps = team_nl_mcgrps,
2784 .n_mcgrps = ARRAY_SIZE(team_nl_mcgrps),
2787 static int team_nl_send_multicast(struct sk_buff *skb,
2788 struct team *team, u32 portid)
2790 return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
2791 skb, 0, 0, GFP_KERNEL);
2794 static int team_nl_send_event_options_get(struct team *team,
2795 struct list_head *sel_opt_inst_list)
2797 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2801 static int team_nl_send_event_port_get(struct team *team,
2802 struct team_port *port)
2804 return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
2808 static int __init team_nl_init(void)
2810 return genl_register_family(&team_nl_family);
2813 static void team_nl_fini(void)
2815 genl_unregister_family(&team_nl_family);
2823 static void __team_options_change_check(struct team *team)
2826 struct team_option_inst *opt_inst;
2827 LIST_HEAD(sel_opt_inst_list);
2829 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2830 if (opt_inst->changed)
2831 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2833 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2834 if (err && err != -ESRCH)
2835 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2839 /* rtnl lock is held */
2841 static void __team_port_change_send(struct team_port *port, bool linkup)
2845 port->changed = true;
2846 port->state.linkup = linkup;
2847 team_refresh_port_linkup(port);
2849 struct ethtool_link_ksettings ecmd;
2851 err = __ethtool_get_link_ksettings(port->dev, &ecmd);
2853 port->state.speed = ecmd.base.speed;
2854 port->state.duplex = ecmd.base.duplex;
2858 port->state.speed = 0;
2859 port->state.duplex = 0;
2862 err = team_nl_send_event_port_get(port->team, port);
2863 if (err && err != -ESRCH)
2864 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2865 port->dev->name, err);
2869 static void __team_carrier_check(struct team *team)
2871 struct team_port *port;
2874 if (team->user_carrier_enabled)
2877 team_linkup = false;
2878 list_for_each_entry(port, &team->port_list, list) {
2886 netif_carrier_on(team->dev);
2888 netif_carrier_off(team->dev);
2891 static void __team_port_change_check(struct team_port *port, bool linkup)
2893 if (port->state.linkup != linkup)
2894 __team_port_change_send(port, linkup);
2895 __team_carrier_check(port->team);
2898 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2900 __team_port_change_send(port, linkup);
2901 __team_carrier_check(port->team);
2904 static void __team_port_change_port_removed(struct team_port *port)
2906 port->removed = true;
2907 __team_port_change_send(port, false);
2908 __team_carrier_check(port->team);
2911 static void team_port_change_check(struct team_port *port, bool linkup)
2913 struct team *team = port->team;
2915 mutex_lock(&team->lock);
2916 __team_port_change_check(port, linkup);
2917 mutex_unlock(&team->lock);
2921 /************************************
2922 * Net device notifier event handler
2923 ************************************/
2925 static int team_device_event(struct notifier_block *unused,
2926 unsigned long event, void *ptr)
2928 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2929 struct team_port *port;
2931 port = team_port_get_rtnl(dev);
2937 if (netif_oper_up(dev))
2938 team_port_change_check(port, true);
2941 team_port_change_check(port, false);
2944 if (netif_running(port->dev))
2945 team_port_change_check(port,
2946 !!netif_oper_up(port->dev));
2948 case NETDEV_UNREGISTER:
2949 team_del_slave(port->team->dev, dev);
2951 case NETDEV_FEAT_CHANGE:
2952 team_compute_features(port->team);
2954 case NETDEV_PRECHANGEMTU:
2955 /* Forbid to change mtu of underlaying device */
2956 if (!port->team->port_mtu_change_allowed)
2959 case NETDEV_PRE_TYPE_CHANGE:
2960 /* Forbid to change type of underlaying device */
2962 case NETDEV_RESEND_IGMP:
2963 /* Propagate to master device */
2964 call_netdevice_notifiers(event, port->team->dev);
2970 static struct notifier_block team_notifier_block __read_mostly = {
2971 .notifier_call = team_device_event,
2975 /***********************
2976 * Module init and exit
2977 ***********************/
2979 static int __init team_module_init(void)
2983 register_netdevice_notifier(&team_notifier_block);
2985 err = rtnl_link_register(&team_link_ops);
2989 err = team_nl_init();
2996 rtnl_link_unregister(&team_link_ops);
2999 unregister_netdevice_notifier(&team_notifier_block);
3004 static void __exit team_module_exit(void)
3007 rtnl_link_unregister(&team_link_ops);
3008 unregister_netdevice_notifier(&team_notifier_block);
3011 module_init(team_module_init);
3012 module_exit(team_module_exit);
3014 MODULE_LICENSE("GPL v2");
3015 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
3016 MODULE_DESCRIPTION("Ethernet team device driver");
3017 MODULE_ALIAS_RTNL_LINK(DRV_NAME);