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 <linux/if_team.h>
33 #define DRV_NAME "team"
40 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
42 static struct team_port *team_port_get_rcu(const struct net_device *dev)
44 struct team_port *port = rcu_dereference(dev->rx_handler_data);
46 return team_port_exists(dev) ? port : NULL;
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 int team_port_set_team_dev_addr(struct team_port *port)
77 return __set_port_dev_addr(port->dev, port->team->dev->dev_addr);
79 EXPORT_SYMBOL(team_port_set_team_dev_addr);
81 static void team_refresh_port_linkup(struct team_port *port)
83 port->linkup = port->user.linkup_enabled ? port->user.linkup :
92 struct team_option_inst { /* One for each option instance */
93 struct list_head list;
94 struct list_head tmp_list;
95 struct team_option *option;
96 struct team_option_inst_info info;
101 static struct team_option *__team_find_option(struct team *team,
102 const char *opt_name)
104 struct team_option *option;
106 list_for_each_entry(option, &team->option_list, list) {
107 if (strcmp(option->name, opt_name) == 0)
113 static void __team_option_inst_del(struct team_option_inst *opt_inst)
115 list_del(&opt_inst->list);
119 static void __team_option_inst_del_option(struct team *team,
120 struct team_option *option)
122 struct team_option_inst *opt_inst, *tmp;
124 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
125 if (opt_inst->option == option)
126 __team_option_inst_del(opt_inst);
130 static int __team_option_inst_add(struct team *team, struct team_option *option,
131 struct team_port *port)
133 struct team_option_inst *opt_inst;
134 unsigned int array_size;
138 array_size = option->array_size;
140 array_size = 1; /* No array but still need one instance */
142 for (i = 0; i < array_size; i++) {
143 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
146 opt_inst->option = option;
147 opt_inst->info.port = port;
148 opt_inst->info.array_index = i;
149 opt_inst->changed = true;
150 opt_inst->removed = false;
151 list_add_tail(&opt_inst->list, &team->option_inst_list);
153 err = option->init(team, &opt_inst->info);
162 static int __team_option_inst_add_option(struct team *team,
163 struct team_option *option)
165 struct team_port *port;
168 if (!option->per_port) {
169 err = __team_option_inst_add(team, option, NULL);
171 goto inst_del_option;
174 list_for_each_entry(port, &team->port_list, list) {
175 err = __team_option_inst_add(team, option, port);
177 goto inst_del_option;
182 __team_option_inst_del_option(team, option);
186 static void __team_option_inst_mark_removed_option(struct team *team,
187 struct team_option *option)
189 struct team_option_inst *opt_inst;
191 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
192 if (opt_inst->option == option) {
193 opt_inst->changed = true;
194 opt_inst->removed = true;
199 static void __team_option_inst_del_port(struct team *team,
200 struct team_port *port)
202 struct team_option_inst *opt_inst, *tmp;
204 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
205 if (opt_inst->option->per_port &&
206 opt_inst->info.port == port)
207 __team_option_inst_del(opt_inst);
211 static int __team_option_inst_add_port(struct team *team,
212 struct team_port *port)
214 struct team_option *option;
217 list_for_each_entry(option, &team->option_list, list) {
218 if (!option->per_port)
220 err = __team_option_inst_add(team, option, port);
227 __team_option_inst_del_port(team, port);
231 static void __team_option_inst_mark_removed_port(struct team *team,
232 struct team_port *port)
234 struct team_option_inst *opt_inst;
236 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
237 if (opt_inst->info.port == port) {
238 opt_inst->changed = true;
239 opt_inst->removed = true;
244 static int __team_options_register(struct team *team,
245 const struct team_option *option,
249 struct team_option **dst_opts;
252 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
256 for (i = 0; i < option_count; i++, option++) {
257 if (__team_find_option(team, option->name)) {
261 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
268 for (i = 0; i < option_count; i++) {
269 err = __team_option_inst_add_option(team, dst_opts[i]);
272 list_add_tail(&dst_opts[i]->list, &team->option_list);
279 for (i--; i >= 0; i--)
280 __team_option_inst_del_option(team, dst_opts[i]);
282 i = option_count - 1;
284 for (i--; i >= 0; i--)
291 static void __team_options_mark_removed(struct team *team,
292 const struct team_option *option,
297 for (i = 0; i < option_count; i++, option++) {
298 struct team_option *del_opt;
300 del_opt = __team_find_option(team, option->name);
302 __team_option_inst_mark_removed_option(team, del_opt);
306 static void __team_options_unregister(struct team *team,
307 const struct team_option *option,
312 for (i = 0; i < option_count; i++, option++) {
313 struct team_option *del_opt;
315 del_opt = __team_find_option(team, option->name);
317 __team_option_inst_del_option(team, del_opt);
318 list_del(&del_opt->list);
324 static void __team_options_change_check(struct team *team);
326 int team_options_register(struct team *team,
327 const struct team_option *option,
332 err = __team_options_register(team, option, option_count);
335 __team_options_change_check(team);
338 EXPORT_SYMBOL(team_options_register);
340 void team_options_unregister(struct team *team,
341 const struct team_option *option,
344 __team_options_mark_removed(team, option, option_count);
345 __team_options_change_check(team);
346 __team_options_unregister(team, option, option_count);
348 EXPORT_SYMBOL(team_options_unregister);
350 static int team_option_get(struct team *team,
351 struct team_option_inst *opt_inst,
352 struct team_gsetter_ctx *ctx)
354 if (!opt_inst->option->getter)
356 return opt_inst->option->getter(team, ctx);
359 static int team_option_set(struct team *team,
360 struct team_option_inst *opt_inst,
361 struct team_gsetter_ctx *ctx)
363 if (!opt_inst->option->setter)
365 return opt_inst->option->setter(team, ctx);
368 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
370 struct team_option_inst *opt_inst;
372 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
373 opt_inst->changed = true;
375 EXPORT_SYMBOL(team_option_inst_set_change);
377 void team_options_change_check(struct team *team)
379 __team_options_change_check(team);
381 EXPORT_SYMBOL(team_options_change_check);
388 static LIST_HEAD(mode_list);
389 static DEFINE_SPINLOCK(mode_list_lock);
391 struct team_mode_item {
392 struct list_head list;
393 const struct team_mode *mode;
396 static struct team_mode_item *__find_mode(const char *kind)
398 struct team_mode_item *mitem;
400 list_for_each_entry(mitem, &mode_list, list) {
401 if (strcmp(mitem->mode->kind, kind) == 0)
407 static bool is_good_mode_name(const char *name)
409 while (*name != '\0') {
410 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
417 int team_mode_register(const struct team_mode *mode)
420 struct team_mode_item *mitem;
422 if (!is_good_mode_name(mode->kind) ||
423 mode->priv_size > TEAM_MODE_PRIV_SIZE)
426 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
430 spin_lock(&mode_list_lock);
431 if (__find_mode(mode->kind)) {
437 list_add_tail(&mitem->list, &mode_list);
439 spin_unlock(&mode_list_lock);
442 EXPORT_SYMBOL(team_mode_register);
444 void team_mode_unregister(const struct team_mode *mode)
446 struct team_mode_item *mitem;
448 spin_lock(&mode_list_lock);
449 mitem = __find_mode(mode->kind);
451 list_del_init(&mitem->list);
454 spin_unlock(&mode_list_lock);
456 EXPORT_SYMBOL(team_mode_unregister);
458 static const struct team_mode *team_mode_get(const char *kind)
460 struct team_mode_item *mitem;
461 const struct team_mode *mode = NULL;
463 spin_lock(&mode_list_lock);
464 mitem = __find_mode(kind);
466 spin_unlock(&mode_list_lock);
467 request_module("team-mode-%s", kind);
468 spin_lock(&mode_list_lock);
469 mitem = __find_mode(kind);
473 if (!try_module_get(mode->owner))
477 spin_unlock(&mode_list_lock);
481 static void team_mode_put(const struct team_mode *mode)
483 module_put(mode->owner);
486 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
488 dev_kfree_skb_any(skb);
492 rx_handler_result_t team_dummy_receive(struct team *team,
493 struct team_port *port,
496 return RX_HANDLER_ANOTHER;
499 static const struct team_mode __team_no_mode = {
503 static bool team_is_mode_set(struct team *team)
505 return team->mode != &__team_no_mode;
508 static void team_set_no_mode(struct team *team)
510 team->mode = &__team_no_mode;
513 static void __team_adjust_ops(struct team *team, int en_port_count)
516 * To avoid checks in rx/tx skb paths, ensure here that non-null and
517 * correct ops are always set.
520 if (!en_port_count || !team_is_mode_set(team) ||
521 !team->mode->ops->transmit)
522 team->ops.transmit = team_dummy_transmit;
524 team->ops.transmit = team->mode->ops->transmit;
526 if (!en_port_count || !team_is_mode_set(team) ||
527 !team->mode->ops->receive)
528 team->ops.receive = team_dummy_receive;
530 team->ops.receive = team->mode->ops->receive;
533 static void team_adjust_ops(struct team *team)
535 __team_adjust_ops(team, team->en_port_count);
539 * We can benefit from the fact that it's ensured no port is present
540 * at the time of mode change. Therefore no packets are in fly so there's no
541 * need to set mode operations in any special way.
543 static int __team_change_mode(struct team *team,
544 const struct team_mode *new_mode)
546 /* Check if mode was previously set and do cleanup if so */
547 if (team_is_mode_set(team)) {
548 void (*exit_op)(struct team *team) = team->ops.exit;
550 /* Clear ops area so no callback is called any longer */
551 memset(&team->ops, 0, sizeof(struct team_mode_ops));
552 team_adjust_ops(team);
556 team_mode_put(team->mode);
557 team_set_no_mode(team);
558 /* zero private data area */
559 memset(&team->mode_priv, 0,
560 sizeof(struct team) - offsetof(struct team, mode_priv));
566 if (new_mode->ops->init) {
569 err = new_mode->ops->init(team);
574 team->mode = new_mode;
575 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
576 team_adjust_ops(team);
581 static int team_change_mode(struct team *team, const char *kind)
583 const struct team_mode *new_mode;
584 struct net_device *dev = team->dev;
587 if (!list_empty(&team->port_list)) {
588 netdev_err(dev, "No ports can be present during mode change\n");
592 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
593 netdev_err(dev, "Unable to change to the same mode the team is in\n");
597 new_mode = team_mode_get(kind);
599 netdev_err(dev, "Mode \"%s\" not found\n", kind);
603 err = __team_change_mode(team, new_mode);
605 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
606 team_mode_put(new_mode);
610 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
615 /************************
616 * Rx path frame handler
617 ************************/
619 /* note: already called with rcu_read_lock */
620 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
622 struct sk_buff *skb = *pskb;
623 struct team_port *port;
625 rx_handler_result_t res;
627 skb = skb_share_check(skb, GFP_ATOMIC);
629 return RX_HANDLER_CONSUMED;
633 port = team_port_get_rcu(skb->dev);
635 if (!team_port_enabled(port)) {
636 /* allow exact match delivery for disabled ports */
637 res = RX_HANDLER_EXACT;
639 res = team->ops.receive(team, port, skb);
641 if (res == RX_HANDLER_ANOTHER) {
642 struct team_pcpu_stats *pcpu_stats;
644 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
645 u64_stats_update_begin(&pcpu_stats->syncp);
646 pcpu_stats->rx_packets++;
647 pcpu_stats->rx_bytes += skb->len;
648 if (skb->pkt_type == PACKET_MULTICAST)
649 pcpu_stats->rx_multicast++;
650 u64_stats_update_end(&pcpu_stats->syncp);
652 skb->dev = team->dev;
654 this_cpu_inc(team->pcpu_stats->rx_dropped);
661 /*************************************
662 * Multiqueue Tx port select override
663 *************************************/
665 static int team_queue_override_init(struct team *team)
667 struct list_head *listarr;
668 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
673 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
676 team->qom_lists = listarr;
677 for (i = 0; i < queue_cnt; i++)
678 INIT_LIST_HEAD(listarr++);
682 static void team_queue_override_fini(struct team *team)
684 kfree(team->qom_lists);
687 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
689 return &team->qom_lists[queue_id - 1];
693 * note: already called with rcu_read_lock
695 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
697 struct list_head *qom_list;
698 struct team_port *port;
700 if (!team->queue_override_enabled || !skb->queue_mapping)
702 qom_list = __team_get_qom_list(team, skb->queue_mapping);
703 list_for_each_entry_rcu(port, qom_list, qom_list) {
704 if (!team_dev_queue_xmit(team, port, skb))
710 static void __team_queue_override_port_del(struct team *team,
711 struct team_port *port)
713 list_del_rcu(&port->qom_list);
715 INIT_LIST_HEAD(&port->qom_list);
718 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
719 struct team_port *cur)
721 if (port->priority < cur->priority)
723 if (port->priority > cur->priority)
725 if (port->index < cur->index)
730 static void __team_queue_override_port_add(struct team *team,
731 struct team_port *port)
733 struct team_port *cur;
734 struct list_head *qom_list;
735 struct list_head *node;
737 if (!port->queue_id || !team_port_enabled(port))
740 qom_list = __team_get_qom_list(team, port->queue_id);
742 list_for_each_entry(cur, qom_list, qom_list) {
743 if (team_queue_override_port_has_gt_prio_than(port, cur))
745 node = &cur->qom_list;
747 list_add_tail_rcu(&port->qom_list, node);
750 static void __team_queue_override_enabled_check(struct team *team)
752 struct team_port *port;
753 bool enabled = false;
755 list_for_each_entry(port, &team->port_list, list) {
756 if (!list_empty(&port->qom_list)) {
761 if (enabled == team->queue_override_enabled)
763 netdev_dbg(team->dev, "%s queue override\n",
764 enabled ? "Enabling" : "Disabling");
765 team->queue_override_enabled = enabled;
768 static void team_queue_override_port_refresh(struct team *team,
769 struct team_port *port)
771 __team_queue_override_port_del(team, port);
772 __team_queue_override_port_add(team, port);
773 __team_queue_override_enabled_check(team);
781 static bool team_port_find(const struct team *team,
782 const struct team_port *port)
784 struct team_port *cur;
786 list_for_each_entry(cur, &team->port_list, list)
793 * Enable/disable port by adding to enabled port hashlist and setting
794 * port->index (Might be racy so reader could see incorrect ifindex when
795 * processing a flying packet, but that is not a problem). Write guarded
798 static void team_port_enable(struct team *team,
799 struct team_port *port)
801 if (team_port_enabled(port))
803 port->index = team->en_port_count++;
804 hlist_add_head_rcu(&port->hlist,
805 team_port_index_hash(team, port->index));
806 team_adjust_ops(team);
807 team_queue_override_port_refresh(team, port);
808 if (team->ops.port_enabled)
809 team->ops.port_enabled(team, port);
812 static void __reconstruct_port_hlist(struct team *team, int rm_index)
815 struct team_port *port;
817 for (i = rm_index + 1; i < team->en_port_count; i++) {
818 port = team_get_port_by_index(team, i);
819 hlist_del_rcu(&port->hlist);
821 hlist_add_head_rcu(&port->hlist,
822 team_port_index_hash(team, port->index));
826 static void team_port_disable(struct team *team,
827 struct team_port *port)
829 if (!team_port_enabled(port))
831 if (team->ops.port_disabled)
832 team->ops.port_disabled(team, port);
833 hlist_del_rcu(&port->hlist);
834 __reconstruct_port_hlist(team, port->index);
836 team_queue_override_port_refresh(team, port);
837 __team_adjust_ops(team, team->en_port_count - 1);
839 * Wait until readers see adjusted ops. This ensures that
840 * readers never see team->en_port_count == 0
843 team->en_port_count--;
846 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
847 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
848 NETIF_F_HIGHDMA | NETIF_F_LRO)
850 static void __team_compute_features(struct team *team)
852 struct team_port *port;
853 u32 vlan_features = TEAM_VLAN_FEATURES;
854 unsigned short max_hard_header_len = ETH_HLEN;
855 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
857 list_for_each_entry(port, &team->port_list, list) {
858 vlan_features = netdev_increment_features(vlan_features,
859 port->dev->vlan_features,
862 dst_release_flag &= port->dev->priv_flags;
863 if (port->dev->hard_header_len > max_hard_header_len)
864 max_hard_header_len = port->dev->hard_header_len;
867 team->dev->vlan_features = vlan_features;
868 team->dev->hard_header_len = max_hard_header_len;
870 flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
871 team->dev->priv_flags = flags | dst_release_flag;
873 netdev_change_features(team->dev);
876 static void team_compute_features(struct team *team)
878 mutex_lock(&team->lock);
879 __team_compute_features(team);
880 mutex_unlock(&team->lock);
883 static int team_port_enter(struct team *team, struct team_port *port)
888 port->dev->priv_flags |= IFF_TEAM_PORT;
889 if (team->ops.port_enter) {
890 err = team->ops.port_enter(team, port);
892 netdev_err(team->dev, "Device %s failed to enter team mode\n",
901 port->dev->priv_flags &= ~IFF_TEAM_PORT;
907 static void team_port_leave(struct team *team, struct team_port *port)
909 if (team->ops.port_leave)
910 team->ops.port_leave(team, port);
911 port->dev->priv_flags &= ~IFF_TEAM_PORT;
915 #ifdef CONFIG_NET_POLL_CONTROLLER
916 static int team_port_enable_netpoll(struct team *team, struct team_port *port,
922 np = kzalloc(sizeof(*np), gfp);
926 err = __netpoll_setup(np, port->dev, gfp);
935 static void team_port_disable_netpoll(struct team_port *port)
937 struct netpoll *np = port->np;
943 /* Wait for transmitting packets to finish before freeing. */
944 synchronize_rcu_bh();
945 __netpoll_cleanup(np);
949 static struct netpoll_info *team_netpoll_info(struct team *team)
951 return team->dev->npinfo;
955 static int team_port_enable_netpoll(struct team *team, struct team_port *port,
960 static void team_port_disable_netpoll(struct team_port *port)
963 static struct netpoll_info *team_netpoll_info(struct team *team)
969 static void __team_port_change_port_added(struct team_port *port, bool linkup);
970 static int team_dev_type_check_change(struct net_device *dev,
971 struct net_device *port_dev);
973 static int team_port_add(struct team *team, struct net_device *port_dev)
975 struct net_device *dev = team->dev;
976 struct team_port *port;
977 char *portname = port_dev->name;
980 if (port_dev->flags & IFF_LOOPBACK) {
981 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
986 if (team_port_exists(port_dev)) {
987 netdev_err(dev, "Device %s is already a port "
988 "of a team device\n", portname);
992 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
993 vlan_uses_dev(dev)) {
994 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
999 err = team_dev_type_check_change(dev, port_dev);
1003 if (port_dev->flags & IFF_UP) {
1004 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1009 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1014 port->dev = port_dev;
1016 INIT_LIST_HEAD(&port->qom_list);
1018 port->orig.mtu = port_dev->mtu;
1019 err = dev_set_mtu(port_dev, dev->mtu);
1021 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1025 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1027 err = team_port_enter(team, port);
1029 netdev_err(dev, "Device %s failed to enter team mode\n",
1031 goto err_port_enter;
1034 err = dev_open(port_dev);
1036 netdev_dbg(dev, "Device %s opening failed\n",
1041 err = vlan_vids_add_by_dev(port_dev, dev);
1043 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1048 if (team_netpoll_info(team)) {
1049 err = team_port_enable_netpoll(team, port, GFP_KERNEL);
1051 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1053 goto err_enable_netpoll;
1057 err = netdev_set_master(port_dev, dev);
1059 netdev_err(dev, "Device %s failed to set master\n", portname);
1060 goto err_set_master;
1063 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1066 netdev_err(dev, "Device %s failed to register rx_handler\n",
1068 goto err_handler_register;
1071 err = __team_option_inst_add_port(team, port);
1073 netdev_err(dev, "Device %s failed to add per-port options\n",
1075 goto err_option_port_add;
1079 team_port_enable(team, port);
1080 list_add_tail_rcu(&port->list, &team->port_list);
1081 __team_compute_features(team);
1082 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1083 __team_options_change_check(team);
1085 netdev_info(dev, "Port device %s added\n", portname);
1089 err_option_port_add:
1090 netdev_rx_handler_unregister(port_dev);
1092 err_handler_register:
1093 netdev_set_master(port_dev, NULL);
1096 team_port_disable_netpoll(port);
1099 vlan_vids_del_by_dev(port_dev, dev);
1102 dev_close(port_dev);
1105 team_port_leave(team, port);
1106 team_port_set_orig_dev_addr(port);
1109 dev_set_mtu(port_dev, port->orig.mtu);
1117 static void __team_port_change_port_removed(struct team_port *port);
1119 static int team_port_del(struct team *team, struct net_device *port_dev)
1121 struct net_device *dev = team->dev;
1122 struct team_port *port;
1123 char *portname = port_dev->name;
1125 port = team_port_get_rtnl(port_dev);
1126 if (!port || !team_port_find(team, port)) {
1127 netdev_err(dev, "Device %s does not act as a port of this team\n",
1132 __team_option_inst_mark_removed_port(team, port);
1133 __team_options_change_check(team);
1134 __team_option_inst_del_port(team, port);
1135 __team_port_change_port_removed(port);
1136 team_port_disable(team, port);
1137 list_del_rcu(&port->list);
1138 netdev_rx_handler_unregister(port_dev);
1139 netdev_set_master(port_dev, NULL);
1140 team_port_disable_netpoll(port);
1141 vlan_vids_del_by_dev(port_dev, dev);
1142 dev_close(port_dev);
1143 team_port_leave(team, port);
1144 team_port_set_orig_dev_addr(port);
1145 dev_set_mtu(port_dev, port->orig.mtu);
1148 netdev_info(dev, "Port device %s removed\n", portname);
1149 __team_compute_features(team);
1159 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1161 ctx->data.str_val = team->mode->kind;
1165 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1167 return team_change_mode(team, ctx->data.str_val);
1170 static int team_port_en_option_get(struct team *team,
1171 struct team_gsetter_ctx *ctx)
1173 struct team_port *port = ctx->info->port;
1175 ctx->data.bool_val = team_port_enabled(port);
1179 static int team_port_en_option_set(struct team *team,
1180 struct team_gsetter_ctx *ctx)
1182 struct team_port *port = ctx->info->port;
1184 if (ctx->data.bool_val)
1185 team_port_enable(team, port);
1187 team_port_disable(team, port);
1191 static int team_user_linkup_option_get(struct team *team,
1192 struct team_gsetter_ctx *ctx)
1194 struct team_port *port = ctx->info->port;
1196 ctx->data.bool_val = port->user.linkup;
1200 static int team_user_linkup_option_set(struct team *team,
1201 struct team_gsetter_ctx *ctx)
1203 struct team_port *port = ctx->info->port;
1205 port->user.linkup = ctx->data.bool_val;
1206 team_refresh_port_linkup(port);
1210 static int team_user_linkup_en_option_get(struct team *team,
1211 struct team_gsetter_ctx *ctx)
1213 struct team_port *port = ctx->info->port;
1215 ctx->data.bool_val = port->user.linkup_enabled;
1219 static int team_user_linkup_en_option_set(struct team *team,
1220 struct team_gsetter_ctx *ctx)
1222 struct team_port *port = ctx->info->port;
1224 port->user.linkup_enabled = ctx->data.bool_val;
1225 team_refresh_port_linkup(port);
1229 static int team_priority_option_get(struct team *team,
1230 struct team_gsetter_ctx *ctx)
1232 struct team_port *port = ctx->info->port;
1234 ctx->data.s32_val = port->priority;
1238 static int team_priority_option_set(struct team *team,
1239 struct team_gsetter_ctx *ctx)
1241 struct team_port *port = ctx->info->port;
1243 port->priority = ctx->data.s32_val;
1244 team_queue_override_port_refresh(team, port);
1248 static int team_queue_id_option_get(struct team *team,
1249 struct team_gsetter_ctx *ctx)
1251 struct team_port *port = ctx->info->port;
1253 ctx->data.u32_val = port->queue_id;
1257 static int team_queue_id_option_set(struct team *team,
1258 struct team_gsetter_ctx *ctx)
1260 struct team_port *port = ctx->info->port;
1262 if (port->queue_id == ctx->data.u32_val)
1264 if (ctx->data.u32_val >= team->dev->real_num_tx_queues)
1266 port->queue_id = ctx->data.u32_val;
1267 team_queue_override_port_refresh(team, port);
1272 static const struct team_option team_options[] = {
1275 .type = TEAM_OPTION_TYPE_STRING,
1276 .getter = team_mode_option_get,
1277 .setter = team_mode_option_set,
1281 .type = TEAM_OPTION_TYPE_BOOL,
1283 .getter = team_port_en_option_get,
1284 .setter = team_port_en_option_set,
1287 .name = "user_linkup",
1288 .type = TEAM_OPTION_TYPE_BOOL,
1290 .getter = team_user_linkup_option_get,
1291 .setter = team_user_linkup_option_set,
1294 .name = "user_linkup_enabled",
1295 .type = TEAM_OPTION_TYPE_BOOL,
1297 .getter = team_user_linkup_en_option_get,
1298 .setter = team_user_linkup_en_option_set,
1302 .type = TEAM_OPTION_TYPE_S32,
1304 .getter = team_priority_option_get,
1305 .setter = team_priority_option_set,
1309 .type = TEAM_OPTION_TYPE_U32,
1311 .getter = team_queue_id_option_get,
1312 .setter = team_queue_id_option_set,
1316 static struct lock_class_key team_netdev_xmit_lock_key;
1317 static struct lock_class_key team_netdev_addr_lock_key;
1319 static void team_set_lockdep_class_one(struct net_device *dev,
1320 struct netdev_queue *txq,
1323 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1326 static void team_set_lockdep_class(struct net_device *dev)
1328 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1329 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1332 static int team_init(struct net_device *dev)
1334 struct team *team = netdev_priv(dev);
1339 mutex_init(&team->lock);
1340 team_set_no_mode(team);
1342 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1343 if (!team->pcpu_stats)
1346 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1347 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1348 INIT_LIST_HEAD(&team->port_list);
1349 err = team_queue_override_init(team);
1351 goto err_team_queue_override_init;
1353 team_adjust_ops(team);
1355 INIT_LIST_HEAD(&team->option_list);
1356 INIT_LIST_HEAD(&team->option_inst_list);
1357 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1359 goto err_options_register;
1360 netif_carrier_off(dev);
1362 team_set_lockdep_class(dev);
1366 err_options_register:
1367 team_queue_override_fini(team);
1368 err_team_queue_override_init:
1369 free_percpu(team->pcpu_stats);
1374 static void team_uninit(struct net_device *dev)
1376 struct team *team = netdev_priv(dev);
1377 struct team_port *port;
1378 struct team_port *tmp;
1380 mutex_lock(&team->lock);
1381 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1382 team_port_del(team, port->dev);
1384 __team_change_mode(team, NULL); /* cleanup */
1385 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1386 team_queue_override_fini(team);
1387 mutex_unlock(&team->lock);
1390 static void team_destructor(struct net_device *dev)
1392 struct team *team = netdev_priv(dev);
1394 free_percpu(team->pcpu_stats);
1398 static int team_open(struct net_device *dev)
1400 netif_carrier_on(dev);
1404 static int team_close(struct net_device *dev)
1406 netif_carrier_off(dev);
1411 * note: already called with rcu_read_lock
1413 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1415 struct team *team = netdev_priv(dev);
1417 unsigned int len = skb->len;
1419 tx_success = team_queue_override_transmit(team, skb);
1421 tx_success = team->ops.transmit(team, skb);
1423 struct team_pcpu_stats *pcpu_stats;
1425 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1426 u64_stats_update_begin(&pcpu_stats->syncp);
1427 pcpu_stats->tx_packets++;
1428 pcpu_stats->tx_bytes += len;
1429 u64_stats_update_end(&pcpu_stats->syncp);
1431 this_cpu_inc(team->pcpu_stats->tx_dropped);
1434 return NETDEV_TX_OK;
1437 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1440 * This helper function exists to help dev_pick_tx get the correct
1441 * destination queue. Using a helper function skips a call to
1442 * skb_tx_hash and will put the skbs in the queue we expect on their
1443 * way down to the team driver.
1445 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1448 * Save the original txq to restore before passing to the driver
1450 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1452 if (unlikely(txq >= dev->real_num_tx_queues)) {
1454 txq -= dev->real_num_tx_queues;
1455 } while (txq >= dev->real_num_tx_queues);
1460 static void team_change_rx_flags(struct net_device *dev, int change)
1462 struct team *team = netdev_priv(dev);
1463 struct team_port *port;
1467 list_for_each_entry_rcu(port, &team->port_list, list) {
1468 if (change & IFF_PROMISC) {
1469 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1470 dev_set_promiscuity(port->dev, inc);
1472 if (change & IFF_ALLMULTI) {
1473 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1474 dev_set_allmulti(port->dev, inc);
1480 static void team_set_rx_mode(struct net_device *dev)
1482 struct team *team = netdev_priv(dev);
1483 struct team_port *port;
1486 list_for_each_entry_rcu(port, &team->port_list, list) {
1487 dev_uc_sync(port->dev, dev);
1488 dev_mc_sync(port->dev, dev);
1493 static int team_set_mac_address(struct net_device *dev, void *p)
1495 struct sockaddr *addr = p;
1496 struct team *team = netdev_priv(dev);
1497 struct team_port *port;
1499 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1500 return -EADDRNOTAVAIL;
1501 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1502 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
1504 list_for_each_entry_rcu(port, &team->port_list, list)
1505 if (team->ops.port_change_dev_addr)
1506 team->ops.port_change_dev_addr(team, port);
1511 static int team_change_mtu(struct net_device *dev, int new_mtu)
1513 struct team *team = netdev_priv(dev);
1514 struct team_port *port;
1518 * Alhough this is reader, it's guarded by team lock. It's not possible
1519 * to traverse list in reverse under rcu_read_lock
1521 mutex_lock(&team->lock);
1522 list_for_each_entry(port, &team->port_list, list) {
1523 err = dev_set_mtu(port->dev, new_mtu);
1525 netdev_err(dev, "Device %s failed to change mtu",
1530 mutex_unlock(&team->lock);
1537 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1538 dev_set_mtu(port->dev, dev->mtu);
1539 mutex_unlock(&team->lock);
1544 static struct rtnl_link_stats64 *
1545 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1547 struct team *team = netdev_priv(dev);
1548 struct team_pcpu_stats *p;
1549 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1550 u32 rx_dropped = 0, tx_dropped = 0;
1554 for_each_possible_cpu(i) {
1555 p = per_cpu_ptr(team->pcpu_stats, i);
1557 start = u64_stats_fetch_begin_bh(&p->syncp);
1558 rx_packets = p->rx_packets;
1559 rx_bytes = p->rx_bytes;
1560 rx_multicast = p->rx_multicast;
1561 tx_packets = p->tx_packets;
1562 tx_bytes = p->tx_bytes;
1563 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1565 stats->rx_packets += rx_packets;
1566 stats->rx_bytes += rx_bytes;
1567 stats->multicast += rx_multicast;
1568 stats->tx_packets += tx_packets;
1569 stats->tx_bytes += tx_bytes;
1571 * rx_dropped & tx_dropped are u32, updated
1572 * without syncp protection.
1574 rx_dropped += p->rx_dropped;
1575 tx_dropped += p->tx_dropped;
1577 stats->rx_dropped = rx_dropped;
1578 stats->tx_dropped = tx_dropped;
1582 static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
1584 struct team *team = netdev_priv(dev);
1585 struct team_port *port;
1589 * Alhough this is reader, it's guarded by team lock. It's not possible
1590 * to traverse list in reverse under rcu_read_lock
1592 mutex_lock(&team->lock);
1593 list_for_each_entry(port, &team->port_list, list) {
1594 err = vlan_vid_add(port->dev, vid);
1598 mutex_unlock(&team->lock);
1603 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1604 vlan_vid_del(port->dev, vid);
1605 mutex_unlock(&team->lock);
1610 static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1612 struct team *team = netdev_priv(dev);
1613 struct team_port *port;
1616 list_for_each_entry_rcu(port, &team->port_list, list)
1617 vlan_vid_del(port->dev, vid);
1623 #ifdef CONFIG_NET_POLL_CONTROLLER
1624 static void team_poll_controller(struct net_device *dev)
1628 static void __team_netpoll_cleanup(struct team *team)
1630 struct team_port *port;
1632 list_for_each_entry(port, &team->port_list, list)
1633 team_port_disable_netpoll(port);
1636 static void team_netpoll_cleanup(struct net_device *dev)
1638 struct team *team = netdev_priv(dev);
1640 mutex_lock(&team->lock);
1641 __team_netpoll_cleanup(team);
1642 mutex_unlock(&team->lock);
1645 static int team_netpoll_setup(struct net_device *dev,
1646 struct netpoll_info *npifo, gfp_t gfp)
1648 struct team *team = netdev_priv(dev);
1649 struct team_port *port;
1652 mutex_lock(&team->lock);
1653 list_for_each_entry(port, &team->port_list, list) {
1654 err = team_port_enable_netpoll(team, port, gfp);
1656 __team_netpoll_cleanup(team);
1660 mutex_unlock(&team->lock);
1665 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1667 struct team *team = netdev_priv(dev);
1670 mutex_lock(&team->lock);
1671 err = team_port_add(team, port_dev);
1672 mutex_unlock(&team->lock);
1676 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1678 struct team *team = netdev_priv(dev);
1681 mutex_lock(&team->lock);
1682 err = team_port_del(team, port_dev);
1683 mutex_unlock(&team->lock);
1687 static netdev_features_t team_fix_features(struct net_device *dev,
1688 netdev_features_t features)
1690 struct team_port *port;
1691 struct team *team = netdev_priv(dev);
1692 netdev_features_t mask;
1695 features &= ~NETIF_F_ONE_FOR_ALL;
1696 features |= NETIF_F_ALL_FOR_ALL;
1699 list_for_each_entry_rcu(port, &team->port_list, list) {
1700 features = netdev_increment_features(features,
1701 port->dev->features,
1708 static const struct net_device_ops team_netdev_ops = {
1709 .ndo_init = team_init,
1710 .ndo_uninit = team_uninit,
1711 .ndo_open = team_open,
1712 .ndo_stop = team_close,
1713 .ndo_start_xmit = team_xmit,
1714 .ndo_select_queue = team_select_queue,
1715 .ndo_change_rx_flags = team_change_rx_flags,
1716 .ndo_set_rx_mode = team_set_rx_mode,
1717 .ndo_set_mac_address = team_set_mac_address,
1718 .ndo_change_mtu = team_change_mtu,
1719 .ndo_get_stats64 = team_get_stats64,
1720 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1721 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1722 #ifdef CONFIG_NET_POLL_CONTROLLER
1723 .ndo_poll_controller = team_poll_controller,
1724 .ndo_netpoll_setup = team_netpoll_setup,
1725 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1727 .ndo_add_slave = team_add_slave,
1728 .ndo_del_slave = team_del_slave,
1729 .ndo_fix_features = team_fix_features,
1733 /***********************
1734 * rt netlink interface
1735 ***********************/
1737 static void team_setup_by_port(struct net_device *dev,
1738 struct net_device *port_dev)
1740 dev->header_ops = port_dev->header_ops;
1741 dev->type = port_dev->type;
1742 dev->hard_header_len = port_dev->hard_header_len;
1743 dev->addr_len = port_dev->addr_len;
1744 dev->mtu = port_dev->mtu;
1745 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
1746 memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
1747 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
1750 static int team_dev_type_check_change(struct net_device *dev,
1751 struct net_device *port_dev)
1753 struct team *team = netdev_priv(dev);
1754 char *portname = port_dev->name;
1757 if (dev->type == port_dev->type)
1759 if (!list_empty(&team->port_list)) {
1760 netdev_err(dev, "Device %s is of different type\n", portname);
1763 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
1764 err = notifier_to_errno(err);
1766 netdev_err(dev, "Refused to change device type\n");
1771 team_setup_by_port(dev, port_dev);
1772 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1776 static void team_setup(struct net_device *dev)
1780 dev->netdev_ops = &team_netdev_ops;
1781 dev->destructor = team_destructor;
1782 dev->tx_queue_len = 0;
1783 dev->flags |= IFF_MULTICAST;
1784 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1787 * Indicate we support unicast address filtering. That way core won't
1788 * bring us to promisc mode in case a unicast addr is added.
1789 * Let this up to underlay drivers.
1791 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
1793 dev->features |= NETIF_F_LLTX;
1794 dev->features |= NETIF_F_GRO;
1795 dev->hw_features = NETIF_F_HW_VLAN_TX |
1796 NETIF_F_HW_VLAN_RX |
1797 NETIF_F_HW_VLAN_FILTER;
1799 dev->features |= dev->hw_features;
1802 static int team_newlink(struct net *src_net, struct net_device *dev,
1803 struct nlattr *tb[], struct nlattr *data[])
1807 if (tb[IFLA_ADDRESS] == NULL)
1808 eth_hw_addr_random(dev);
1810 err = register_netdevice(dev);
1817 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1819 if (tb[IFLA_ADDRESS]) {
1820 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1822 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1823 return -EADDRNOTAVAIL;
1828 static unsigned int team_get_num_tx_queues(void)
1830 return TEAM_DEFAULT_NUM_TX_QUEUES;
1833 static unsigned int team_get_num_rx_queues(void)
1835 return TEAM_DEFAULT_NUM_RX_QUEUES;
1838 static struct rtnl_link_ops team_link_ops __read_mostly = {
1840 .priv_size = sizeof(struct team),
1841 .setup = team_setup,
1842 .newlink = team_newlink,
1843 .validate = team_validate,
1844 .get_num_tx_queues = team_get_num_tx_queues,
1845 .get_num_rx_queues = team_get_num_rx_queues,
1849 /***********************************
1850 * Generic netlink custom interface
1851 ***********************************/
1853 static struct genl_family team_nl_family = {
1854 .id = GENL_ID_GENERATE,
1855 .name = TEAM_GENL_NAME,
1856 .version = TEAM_GENL_VERSION,
1857 .maxattr = TEAM_ATTR_MAX,
1861 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1862 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1863 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1864 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1865 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1868 static const struct nla_policy
1869 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1870 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1871 [TEAM_ATTR_OPTION_NAME] = {
1873 .len = TEAM_STRING_MAX_LEN,
1875 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1876 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
1877 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
1880 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1882 struct sk_buff *msg;
1886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1890 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1891 &team_nl_family, 0, TEAM_CMD_NOOP);
1897 genlmsg_end(msg, hdr);
1899 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
1908 * Netlink cmd functions should be locked by following two functions.
1909 * Since dev gets held here, that ensures dev won't disappear in between.
1911 static struct team *team_nl_team_get(struct genl_info *info)
1913 struct net *net = genl_info_net(info);
1915 struct net_device *dev;
1918 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1921 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
1922 dev = dev_get_by_index(net, ifindex);
1923 if (!dev || dev->netdev_ops != &team_netdev_ops) {
1929 team = netdev_priv(dev);
1930 mutex_lock(&team->lock);
1934 static void team_nl_team_put(struct team *team)
1936 mutex_unlock(&team->lock);
1940 static int team_nl_send_generic(struct genl_info *info, struct team *team,
1941 int (*fill_func)(struct sk_buff *skb,
1942 struct genl_info *info,
1943 int flags, struct team *team))
1945 struct sk_buff *skb;
1948 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1952 err = fill_func(skb, info, NLM_F_ACK, team);
1956 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
1964 typedef int team_nl_send_func_t(struct sk_buff *skb,
1965 struct team *team, u32 portid);
1967 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
1969 return genlmsg_unicast(dev_net(team->dev), skb, portid);
1972 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
1973 struct team_option_inst *opt_inst)
1975 struct nlattr *option_item;
1976 struct team_option *option = opt_inst->option;
1977 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
1978 struct team_gsetter_ctx ctx;
1981 ctx.info = opt_inst_info;
1982 err = team_option_get(team, opt_inst, &ctx);
1986 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1990 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
1992 if (opt_inst_info->port &&
1993 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
1994 opt_inst_info->port->dev->ifindex))
1996 if (opt_inst->option->array_size &&
1997 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
1998 opt_inst_info->array_index))
2001 switch (option->type) {
2002 case TEAM_OPTION_TYPE_U32:
2003 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2005 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2008 case TEAM_OPTION_TYPE_STRING:
2009 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2011 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2015 case TEAM_OPTION_TYPE_BINARY:
2016 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2018 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2019 ctx.data.bin_val.ptr))
2022 case TEAM_OPTION_TYPE_BOOL:
2023 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2025 if (ctx.data.bool_val &&
2026 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2029 case TEAM_OPTION_TYPE_S32:
2030 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2032 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2038 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2040 if (opt_inst->changed) {
2041 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2043 opt_inst->changed = false;
2045 nla_nest_end(skb, option_item);
2049 nla_nest_cancel(skb, option_item);
2053 static int __send_and_alloc_skb(struct sk_buff **pskb,
2054 struct team *team, u32 portid,
2055 team_nl_send_func_t *send_func)
2060 err = send_func(*pskb, team, portid);
2064 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2070 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2071 int flags, team_nl_send_func_t *send_func,
2072 struct list_head *sel_opt_inst_list)
2074 struct nlattr *option_list;
2075 struct nlmsghdr *nlh;
2077 struct team_option_inst *opt_inst;
2079 struct sk_buff *skb = NULL;
2083 opt_inst = list_first_entry(sel_opt_inst_list,
2084 struct team_option_inst, tmp_list);
2087 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2091 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2092 TEAM_CMD_OPTIONS_GET);
2096 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2097 goto nla_put_failure;
2098 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2100 goto nla_put_failure;
2104 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2105 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2107 if (err == -EMSGSIZE) {
2118 nla_nest_end(skb, option_list);
2119 genlmsg_end(skb, hdr);
2124 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2126 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2132 return send_func(skb, team, portid);
2137 genlmsg_cancel(skb, hdr);
2142 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2145 struct team_option_inst *opt_inst;
2147 LIST_HEAD(sel_opt_inst_list);
2149 team = team_nl_team_get(info);
2153 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2154 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2155 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2156 NLM_F_ACK, team_nl_send_unicast,
2157 &sel_opt_inst_list);
2159 team_nl_team_put(team);
2164 static int team_nl_send_event_options_get(struct team *team,
2165 struct list_head *sel_opt_inst_list);
2167 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2172 struct nlattr *nl_option;
2173 LIST_HEAD(opt_inst_list);
2175 team = team_nl_team_get(info);
2180 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2185 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2186 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2187 struct nlattr *attr;
2188 struct nlattr *attr_data;
2189 enum team_option_type opt_type;
2190 int opt_port_ifindex = 0; /* != 0 for per-port options */
2191 u32 opt_array_index = 0;
2192 bool opt_is_array = false;
2193 struct team_option_inst *opt_inst;
2195 bool opt_found = false;
2197 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2201 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2202 nl_option, team_nl_option_policy);
2205 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2206 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2210 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2212 opt_type = TEAM_OPTION_TYPE_U32;
2215 opt_type = TEAM_OPTION_TYPE_STRING;
2218 opt_type = TEAM_OPTION_TYPE_BINARY;
2221 opt_type = TEAM_OPTION_TYPE_BOOL;
2224 opt_type = TEAM_OPTION_TYPE_S32;
2230 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2231 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2236 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2237 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2239 opt_port_ifindex = nla_get_u32(attr);
2241 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2243 opt_is_array = true;
2244 opt_array_index = nla_get_u32(attr);
2247 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2248 struct team_option *option = opt_inst->option;
2249 struct team_gsetter_ctx ctx;
2250 struct team_option_inst_info *opt_inst_info;
2253 opt_inst_info = &opt_inst->info;
2254 tmp_ifindex = opt_inst_info->port ?
2255 opt_inst_info->port->dev->ifindex : 0;
2256 if (option->type != opt_type ||
2257 strcmp(option->name, opt_name) ||
2258 tmp_ifindex != opt_port_ifindex ||
2259 (option->array_size && !opt_is_array) ||
2260 opt_inst_info->array_index != opt_array_index)
2263 ctx.info = opt_inst_info;
2265 case TEAM_OPTION_TYPE_U32:
2266 ctx.data.u32_val = nla_get_u32(attr_data);
2268 case TEAM_OPTION_TYPE_STRING:
2269 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2273 ctx.data.str_val = nla_data(attr_data);
2275 case TEAM_OPTION_TYPE_BINARY:
2276 ctx.data.bin_val.len = nla_len(attr_data);
2277 ctx.data.bin_val.ptr = nla_data(attr_data);
2279 case TEAM_OPTION_TYPE_BOOL:
2280 ctx.data.bool_val = attr_data ? true : false;
2282 case TEAM_OPTION_TYPE_S32:
2283 ctx.data.s32_val = nla_get_s32(attr_data);
2288 err = team_option_set(team, opt_inst, &ctx);
2291 opt_inst->changed = true;
2292 list_add(&opt_inst->tmp_list, &opt_inst_list);
2300 err = team_nl_send_event_options_get(team, &opt_inst_list);
2303 team_nl_team_put(team);
2308 static int team_nl_fill_port_list_get(struct sk_buff *skb,
2309 u32 portid, u32 seq, int flags,
2313 struct nlattr *port_list;
2315 struct team_port *port;
2317 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags,
2318 TEAM_CMD_PORT_LIST_GET);
2322 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2323 goto nla_put_failure;
2324 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2326 goto nla_put_failure;
2328 list_for_each_entry(port, &team->port_list, list) {
2329 struct nlattr *port_item;
2331 /* Include only changed ports if fill all mode is not on */
2332 if (!fillall && !port->changed)
2334 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2336 goto nla_put_failure;
2337 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2338 goto nla_put_failure;
2339 if (port->changed) {
2340 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2341 goto nla_put_failure;
2342 port->changed = false;
2344 if ((port->removed &&
2345 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2346 (port->state.linkup &&
2347 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2348 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2349 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2350 goto nla_put_failure;
2351 nla_nest_end(skb, port_item);
2354 nla_nest_end(skb, port_list);
2355 return genlmsg_end(skb, hdr);
2358 genlmsg_cancel(skb, hdr);
2362 static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
2363 struct genl_info *info, int flags,
2366 return team_nl_fill_port_list_get(skb, info->snd_portid,
2367 info->snd_seq, NLM_F_ACK,
2371 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2372 struct genl_info *info)
2377 team = team_nl_team_get(info);
2381 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
2383 team_nl_team_put(team);
2388 static struct genl_ops team_nl_ops[] = {
2390 .cmd = TEAM_CMD_NOOP,
2391 .doit = team_nl_cmd_noop,
2392 .policy = team_nl_policy,
2395 .cmd = TEAM_CMD_OPTIONS_SET,
2396 .doit = team_nl_cmd_options_set,
2397 .policy = team_nl_policy,
2398 .flags = GENL_ADMIN_PERM,
2401 .cmd = TEAM_CMD_OPTIONS_GET,
2402 .doit = team_nl_cmd_options_get,
2403 .policy = team_nl_policy,
2404 .flags = GENL_ADMIN_PERM,
2407 .cmd = TEAM_CMD_PORT_LIST_GET,
2408 .doit = team_nl_cmd_port_list_get,
2409 .policy = team_nl_policy,
2410 .flags = GENL_ADMIN_PERM,
2414 static struct genl_multicast_group team_change_event_mcgrp = {
2415 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2418 static int team_nl_send_multicast(struct sk_buff *skb,
2419 struct team *team, u32 portid)
2421 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2422 team_change_event_mcgrp.id, GFP_KERNEL);
2425 static int team_nl_send_event_options_get(struct team *team,
2426 struct list_head *sel_opt_inst_list)
2428 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2432 static int team_nl_send_event_port_list_get(struct team *team)
2434 struct sk_buff *skb;
2436 struct net *net = dev_net(team->dev);
2438 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2442 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
2446 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
2455 static int team_nl_init(void)
2459 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2460 ARRAY_SIZE(team_nl_ops));
2464 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2466 goto err_change_event_grp_reg;
2470 err_change_event_grp_reg:
2471 genl_unregister_family(&team_nl_family);
2476 static void team_nl_fini(void)
2478 genl_unregister_family(&team_nl_family);
2486 static void __team_options_change_check(struct team *team)
2489 struct team_option_inst *opt_inst;
2490 LIST_HEAD(sel_opt_inst_list);
2492 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2493 if (opt_inst->changed)
2494 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2496 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2497 if (err && err != -ESRCH)
2498 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2502 /* rtnl lock is held */
2504 static void __team_port_change_send(struct team_port *port, bool linkup)
2508 port->changed = true;
2509 port->state.linkup = linkup;
2510 team_refresh_port_linkup(port);
2512 struct ethtool_cmd ecmd;
2514 err = __ethtool_get_settings(port->dev, &ecmd);
2516 port->state.speed = ethtool_cmd_speed(&ecmd);
2517 port->state.duplex = ecmd.duplex;
2521 port->state.speed = 0;
2522 port->state.duplex = 0;
2525 err = team_nl_send_event_port_list_get(port->team);
2526 if (err && err != -ESRCH)
2527 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2528 port->dev->name, err);
2532 static void __team_port_change_check(struct team_port *port, bool linkup)
2534 if (port->state.linkup != linkup)
2535 __team_port_change_send(port, linkup);
2538 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2540 __team_port_change_send(port, linkup);
2543 static void __team_port_change_port_removed(struct team_port *port)
2545 port->removed = true;
2546 __team_port_change_send(port, false);
2549 static void team_port_change_check(struct team_port *port, bool linkup)
2551 struct team *team = port->team;
2553 mutex_lock(&team->lock);
2554 __team_port_change_check(port, linkup);
2555 mutex_unlock(&team->lock);
2559 /************************************
2560 * Net device notifier event handler
2561 ************************************/
2563 static int team_device_event(struct notifier_block *unused,
2564 unsigned long event, void *ptr)
2566 struct net_device *dev = (struct net_device *) ptr;
2567 struct team_port *port;
2569 port = team_port_get_rtnl(dev);
2575 if (netif_carrier_ok(dev))
2576 team_port_change_check(port, true);
2578 team_port_change_check(port, false);
2580 if (netif_running(port->dev))
2581 team_port_change_check(port,
2582 !!netif_carrier_ok(port->dev));
2584 case NETDEV_UNREGISTER:
2585 team_del_slave(port->team->dev, dev);
2587 case NETDEV_FEAT_CHANGE:
2588 team_compute_features(port->team);
2590 case NETDEV_CHANGEMTU:
2591 /* Forbid to change mtu of underlaying device */
2593 case NETDEV_PRE_TYPE_CHANGE:
2594 /* Forbid to change type of underlaying device */
2600 static struct notifier_block team_notifier_block __read_mostly = {
2601 .notifier_call = team_device_event,
2605 /***********************
2606 * Module init and exit
2607 ***********************/
2609 static int __init team_module_init(void)
2613 register_netdevice_notifier(&team_notifier_block);
2615 err = rtnl_link_register(&team_link_ops);
2619 err = team_nl_init();
2626 rtnl_link_unregister(&team_link_ops);
2629 unregister_netdevice_notifier(&team_notifier_block);
2634 static void __exit team_module_exit(void)
2637 rtnl_link_unregister(&team_link_ops);
2638 unregister_netdevice_notifier(&team_notifier_block);
2641 module_init(team_module_init);
2642 module_exit(team_module_exit);
2644 MODULE_LICENSE("GPL v2");
2645 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2646 MODULE_DESCRIPTION("Ethernet team device driver");
2647 MODULE_ALIAS_RTNL_LINK(DRV_NAME);