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 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
43 static struct team_port *team_port_get_rcu(const struct net_device *dev)
45 struct team_port *port = rcu_dereference(dev->rx_handler_data);
47 return team_port_exists(dev) ? port : NULL;
50 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
52 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
54 return team_port_exists(dev) ? port : NULL;
58 * Since the ability to change device address for open port device is tested in
59 * team_port_add, this function can be called without control of return value
61 static int __set_port_dev_addr(struct net_device *port_dev,
62 const unsigned char *dev_addr)
66 memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
67 addr.sa_family = port_dev->type;
68 return dev_set_mac_address(port_dev, &addr);
71 static int team_port_set_orig_dev_addr(struct team_port *port)
73 return __set_port_dev_addr(port->dev, port->orig.dev_addr);
76 int team_port_set_team_dev_addr(struct team_port *port)
78 return __set_port_dev_addr(port->dev, port->team->dev->dev_addr);
80 EXPORT_SYMBOL(team_port_set_team_dev_addr);
82 static void team_refresh_port_linkup(struct team_port *port)
84 port->linkup = port->user.linkup_enabled ? port->user.linkup :
93 struct team_option_inst { /* One for each option instance */
94 struct list_head list;
95 struct list_head tmp_list;
96 struct team_option *option;
97 struct team_option_inst_info info;
102 static struct team_option *__team_find_option(struct team *team,
103 const char *opt_name)
105 struct team_option *option;
107 list_for_each_entry(option, &team->option_list, list) {
108 if (strcmp(option->name, opt_name) == 0)
114 static void __team_option_inst_del(struct team_option_inst *opt_inst)
116 list_del(&opt_inst->list);
120 static void __team_option_inst_del_option(struct team *team,
121 struct team_option *option)
123 struct team_option_inst *opt_inst, *tmp;
125 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
126 if (opt_inst->option == option)
127 __team_option_inst_del(opt_inst);
131 static int __team_option_inst_add(struct team *team, struct team_option *option,
132 struct team_port *port)
134 struct team_option_inst *opt_inst;
135 unsigned int array_size;
139 array_size = option->array_size;
141 array_size = 1; /* No array but still need one instance */
143 for (i = 0; i < array_size; i++) {
144 opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
147 opt_inst->option = option;
148 opt_inst->info.port = port;
149 opt_inst->info.array_index = i;
150 opt_inst->changed = true;
151 opt_inst->removed = false;
152 list_add_tail(&opt_inst->list, &team->option_inst_list);
154 err = option->init(team, &opt_inst->info);
163 static int __team_option_inst_add_option(struct team *team,
164 struct team_option *option)
166 struct team_port *port;
169 if (!option->per_port) {
170 err = __team_option_inst_add(team, option, NULL);
172 goto inst_del_option;
175 list_for_each_entry(port, &team->port_list, list) {
176 err = __team_option_inst_add(team, option, port);
178 goto inst_del_option;
183 __team_option_inst_del_option(team, option);
187 static void __team_option_inst_mark_removed_option(struct team *team,
188 struct team_option *option)
190 struct team_option_inst *opt_inst;
192 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
193 if (opt_inst->option == option) {
194 opt_inst->changed = true;
195 opt_inst->removed = true;
200 static void __team_option_inst_del_port(struct team *team,
201 struct team_port *port)
203 struct team_option_inst *opt_inst, *tmp;
205 list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
206 if (opt_inst->option->per_port &&
207 opt_inst->info.port == port)
208 __team_option_inst_del(opt_inst);
212 static int __team_option_inst_add_port(struct team *team,
213 struct team_port *port)
215 struct team_option *option;
218 list_for_each_entry(option, &team->option_list, list) {
219 if (!option->per_port)
221 err = __team_option_inst_add(team, option, port);
228 __team_option_inst_del_port(team, port);
232 static void __team_option_inst_mark_removed_port(struct team *team,
233 struct team_port *port)
235 struct team_option_inst *opt_inst;
237 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
238 if (opt_inst->info.port == port) {
239 opt_inst->changed = true;
240 opt_inst->removed = true;
245 static int __team_options_register(struct team *team,
246 const struct team_option *option,
250 struct team_option **dst_opts;
253 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
257 for (i = 0; i < option_count; i++, option++) {
258 if (__team_find_option(team, option->name)) {
262 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
269 for (i = 0; i < option_count; i++) {
270 err = __team_option_inst_add_option(team, dst_opts[i]);
273 list_add_tail(&dst_opts[i]->list, &team->option_list);
280 for (i--; i >= 0; i--)
281 __team_option_inst_del_option(team, dst_opts[i]);
283 i = option_count - 1;
285 for (i--; i >= 0; i--)
292 static void __team_options_mark_removed(struct team *team,
293 const struct team_option *option,
298 for (i = 0; i < option_count; i++, option++) {
299 struct team_option *del_opt;
301 del_opt = __team_find_option(team, option->name);
303 __team_option_inst_mark_removed_option(team, del_opt);
307 static void __team_options_unregister(struct team *team,
308 const struct team_option *option,
313 for (i = 0; i < option_count; i++, option++) {
314 struct team_option *del_opt;
316 del_opt = __team_find_option(team, option->name);
318 __team_option_inst_del_option(team, del_opt);
319 list_del(&del_opt->list);
325 static void __team_options_change_check(struct team *team);
327 int team_options_register(struct team *team,
328 const struct team_option *option,
333 err = __team_options_register(team, option, option_count);
336 __team_options_change_check(team);
339 EXPORT_SYMBOL(team_options_register);
341 void team_options_unregister(struct team *team,
342 const struct team_option *option,
345 __team_options_mark_removed(team, option, option_count);
346 __team_options_change_check(team);
347 __team_options_unregister(team, option, option_count);
349 EXPORT_SYMBOL(team_options_unregister);
351 static int team_option_get(struct team *team,
352 struct team_option_inst *opt_inst,
353 struct team_gsetter_ctx *ctx)
355 if (!opt_inst->option->getter)
357 return opt_inst->option->getter(team, ctx);
360 static int team_option_set(struct team *team,
361 struct team_option_inst *opt_inst,
362 struct team_gsetter_ctx *ctx)
364 if (!opt_inst->option->setter)
366 return opt_inst->option->setter(team, ctx);
369 void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
371 struct team_option_inst *opt_inst;
373 opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
374 opt_inst->changed = true;
376 EXPORT_SYMBOL(team_option_inst_set_change);
378 void team_options_change_check(struct team *team)
380 __team_options_change_check(team);
382 EXPORT_SYMBOL(team_options_change_check);
389 static LIST_HEAD(mode_list);
390 static DEFINE_SPINLOCK(mode_list_lock);
392 struct team_mode_item {
393 struct list_head list;
394 const struct team_mode *mode;
397 static struct team_mode_item *__find_mode(const char *kind)
399 struct team_mode_item *mitem;
401 list_for_each_entry(mitem, &mode_list, list) {
402 if (strcmp(mitem->mode->kind, kind) == 0)
408 static bool is_good_mode_name(const char *name)
410 while (*name != '\0') {
411 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
418 int team_mode_register(const struct team_mode *mode)
421 struct team_mode_item *mitem;
423 if (!is_good_mode_name(mode->kind) ||
424 mode->priv_size > TEAM_MODE_PRIV_SIZE)
427 mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
431 spin_lock(&mode_list_lock);
432 if (__find_mode(mode->kind)) {
438 list_add_tail(&mitem->list, &mode_list);
440 spin_unlock(&mode_list_lock);
443 EXPORT_SYMBOL(team_mode_register);
445 void team_mode_unregister(const struct team_mode *mode)
447 struct team_mode_item *mitem;
449 spin_lock(&mode_list_lock);
450 mitem = __find_mode(mode->kind);
452 list_del_init(&mitem->list);
455 spin_unlock(&mode_list_lock);
457 EXPORT_SYMBOL(team_mode_unregister);
459 static const struct team_mode *team_mode_get(const char *kind)
461 struct team_mode_item *mitem;
462 const struct team_mode *mode = NULL;
464 spin_lock(&mode_list_lock);
465 mitem = __find_mode(kind);
467 spin_unlock(&mode_list_lock);
468 request_module("team-mode-%s", kind);
469 spin_lock(&mode_list_lock);
470 mitem = __find_mode(kind);
474 if (!try_module_get(mode->owner))
478 spin_unlock(&mode_list_lock);
482 static void team_mode_put(const struct team_mode *mode)
484 module_put(mode->owner);
487 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
489 dev_kfree_skb_any(skb);
493 rx_handler_result_t team_dummy_receive(struct team *team,
494 struct team_port *port,
497 return RX_HANDLER_ANOTHER;
500 static const struct team_mode __team_no_mode = {
504 static bool team_is_mode_set(struct team *team)
506 return team->mode != &__team_no_mode;
509 static void team_set_no_mode(struct team *team)
511 team->mode = &__team_no_mode;
514 static void __team_adjust_ops(struct team *team, int en_port_count)
517 * To avoid checks in rx/tx skb paths, ensure here that non-null and
518 * correct ops are always set.
521 if (!en_port_count || !team_is_mode_set(team) ||
522 !team->mode->ops->transmit)
523 team->ops.transmit = team_dummy_transmit;
525 team->ops.transmit = team->mode->ops->transmit;
527 if (!en_port_count || !team_is_mode_set(team) ||
528 !team->mode->ops->receive)
529 team->ops.receive = team_dummy_receive;
531 team->ops.receive = team->mode->ops->receive;
534 static void team_adjust_ops(struct team *team)
536 __team_adjust_ops(team, team->en_port_count);
540 * We can benefit from the fact that it's ensured no port is present
541 * at the time of mode change. Therefore no packets are in fly so there's no
542 * need to set mode operations in any special way.
544 static int __team_change_mode(struct team *team,
545 const struct team_mode *new_mode)
547 /* Check if mode was previously set and do cleanup if so */
548 if (team_is_mode_set(team)) {
549 void (*exit_op)(struct team *team) = team->ops.exit;
551 /* Clear ops area so no callback is called any longer */
552 memset(&team->ops, 0, sizeof(struct team_mode_ops));
553 team_adjust_ops(team);
557 team_mode_put(team->mode);
558 team_set_no_mode(team);
559 /* zero private data area */
560 memset(&team->mode_priv, 0,
561 sizeof(struct team) - offsetof(struct team, mode_priv));
567 if (new_mode->ops->init) {
570 err = new_mode->ops->init(team);
575 team->mode = new_mode;
576 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
577 team_adjust_ops(team);
582 static int team_change_mode(struct team *team, const char *kind)
584 const struct team_mode *new_mode;
585 struct net_device *dev = team->dev;
588 if (!list_empty(&team->port_list)) {
589 netdev_err(dev, "No ports can be present during mode change\n");
593 if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
594 netdev_err(dev, "Unable to change to the same mode the team is in\n");
598 new_mode = team_mode_get(kind);
600 netdev_err(dev, "Mode \"%s\" not found\n", kind);
604 err = __team_change_mode(team, new_mode);
606 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
607 team_mode_put(new_mode);
611 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
616 /************************
617 * Rx path frame handler
618 ************************/
620 /* note: already called with rcu_read_lock */
621 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
623 struct sk_buff *skb = *pskb;
624 struct team_port *port;
626 rx_handler_result_t res;
628 skb = skb_share_check(skb, GFP_ATOMIC);
630 return RX_HANDLER_CONSUMED;
634 port = team_port_get_rcu(skb->dev);
636 if (!team_port_enabled(port)) {
637 /* allow exact match delivery for disabled ports */
638 res = RX_HANDLER_EXACT;
640 res = team->ops.receive(team, port, skb);
642 if (res == RX_HANDLER_ANOTHER) {
643 struct team_pcpu_stats *pcpu_stats;
645 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
646 u64_stats_update_begin(&pcpu_stats->syncp);
647 pcpu_stats->rx_packets++;
648 pcpu_stats->rx_bytes += skb->len;
649 if (skb->pkt_type == PACKET_MULTICAST)
650 pcpu_stats->rx_multicast++;
651 u64_stats_update_end(&pcpu_stats->syncp);
653 skb->dev = team->dev;
655 this_cpu_inc(team->pcpu_stats->rx_dropped);
662 /*************************************
663 * Multiqueue Tx port select override
664 *************************************/
666 static int team_queue_override_init(struct team *team)
668 struct list_head *listarr;
669 unsigned int queue_cnt = team->dev->num_tx_queues - 1;
674 listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
677 team->qom_lists = listarr;
678 for (i = 0; i < queue_cnt; i++)
679 INIT_LIST_HEAD(listarr++);
683 static void team_queue_override_fini(struct team *team)
685 kfree(team->qom_lists);
688 static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
690 return &team->qom_lists[queue_id - 1];
694 * note: already called with rcu_read_lock
696 static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
698 struct list_head *qom_list;
699 struct team_port *port;
701 if (!team->queue_override_enabled || !skb->queue_mapping)
703 qom_list = __team_get_qom_list(team, skb->queue_mapping);
704 list_for_each_entry_rcu(port, qom_list, qom_list) {
705 if (!team_dev_queue_xmit(team, port, skb))
711 static void __team_queue_override_port_del(struct team *team,
712 struct team_port *port)
714 list_del_rcu(&port->qom_list);
716 INIT_LIST_HEAD(&port->qom_list);
719 static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
720 struct team_port *cur)
722 if (port->priority < cur->priority)
724 if (port->priority > cur->priority)
726 if (port->index < cur->index)
731 static void __team_queue_override_port_add(struct team *team,
732 struct team_port *port)
734 struct team_port *cur;
735 struct list_head *qom_list;
736 struct list_head *node;
738 if (!port->queue_id || !team_port_enabled(port))
741 qom_list = __team_get_qom_list(team, port->queue_id);
743 list_for_each_entry(cur, qom_list, qom_list) {
744 if (team_queue_override_port_has_gt_prio_than(port, cur))
746 node = &cur->qom_list;
748 list_add_tail_rcu(&port->qom_list, node);
751 static void __team_queue_override_enabled_check(struct team *team)
753 struct team_port *port;
754 bool enabled = false;
756 list_for_each_entry(port, &team->port_list, list) {
757 if (!list_empty(&port->qom_list)) {
762 if (enabled == team->queue_override_enabled)
764 netdev_dbg(team->dev, "%s queue override\n",
765 enabled ? "Enabling" : "Disabling");
766 team->queue_override_enabled = enabled;
769 static void team_queue_override_port_refresh(struct team *team,
770 struct team_port *port)
772 __team_queue_override_port_del(team, port);
773 __team_queue_override_port_add(team, port);
774 __team_queue_override_enabled_check(team);
782 static bool team_port_find(const struct team *team,
783 const struct team_port *port)
785 struct team_port *cur;
787 list_for_each_entry(cur, &team->port_list, list)
794 * Enable/disable port by adding to enabled port hashlist and setting
795 * port->index (Might be racy so reader could see incorrect ifindex when
796 * processing a flying packet, but that is not a problem). Write guarded
799 static void team_port_enable(struct team *team,
800 struct team_port *port)
802 if (team_port_enabled(port))
804 port->index = team->en_port_count++;
805 hlist_add_head_rcu(&port->hlist,
806 team_port_index_hash(team, port->index));
807 team_adjust_ops(team);
808 team_queue_override_port_refresh(team, port);
809 if (team->ops.port_enabled)
810 team->ops.port_enabled(team, port);
813 static void __reconstruct_port_hlist(struct team *team, int rm_index)
816 struct team_port *port;
818 for (i = rm_index + 1; i < team->en_port_count; i++) {
819 port = team_get_port_by_index(team, i);
820 hlist_del_rcu(&port->hlist);
822 hlist_add_head_rcu(&port->hlist,
823 team_port_index_hash(team, port->index));
827 static void team_port_disable(struct team *team,
828 struct team_port *port)
830 if (!team_port_enabled(port))
832 if (team->ops.port_disabled)
833 team->ops.port_disabled(team, port);
834 hlist_del_rcu(&port->hlist);
835 __reconstruct_port_hlist(team, port->index);
837 team_queue_override_port_refresh(team, port);
838 __team_adjust_ops(team, team->en_port_count - 1);
840 * Wait until readers see adjusted ops. This ensures that
841 * readers never see team->en_port_count == 0
844 team->en_port_count--;
847 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
848 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
849 NETIF_F_HIGHDMA | NETIF_F_LRO)
851 static void __team_compute_features(struct team *team)
853 struct team_port *port;
854 u32 vlan_features = TEAM_VLAN_FEATURES;
855 unsigned short max_hard_header_len = ETH_HLEN;
856 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
858 list_for_each_entry(port, &team->port_list, list) {
859 vlan_features = netdev_increment_features(vlan_features,
860 port->dev->vlan_features,
863 dst_release_flag &= port->dev->priv_flags;
864 if (port->dev->hard_header_len > max_hard_header_len)
865 max_hard_header_len = port->dev->hard_header_len;
868 team->dev->vlan_features = vlan_features;
869 team->dev->hard_header_len = max_hard_header_len;
871 flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
872 team->dev->priv_flags = flags | dst_release_flag;
874 netdev_change_features(team->dev);
877 static void team_compute_features(struct team *team)
879 mutex_lock(&team->lock);
880 __team_compute_features(team);
881 mutex_unlock(&team->lock);
884 static int team_port_enter(struct team *team, struct team_port *port)
889 port->dev->priv_flags |= IFF_TEAM_PORT;
890 if (team->ops.port_enter) {
891 err = team->ops.port_enter(team, port);
893 netdev_err(team->dev, "Device %s failed to enter team mode\n",
902 port->dev->priv_flags &= ~IFF_TEAM_PORT;
908 static void team_port_leave(struct team *team, struct team_port *port)
910 if (team->ops.port_leave)
911 team->ops.port_leave(team, port);
912 port->dev->priv_flags &= ~IFF_TEAM_PORT;
916 #ifdef CONFIG_NET_POLL_CONTROLLER
917 static int team_port_enable_netpoll(struct team *team, struct team_port *port,
923 np = kzalloc(sizeof(*np), gfp);
927 err = __netpoll_setup(np, port->dev, gfp);
936 static void team_port_disable_netpoll(struct team_port *port)
938 struct netpoll *np = port->np;
944 /* Wait for transmitting packets to finish before freeing. */
945 synchronize_rcu_bh();
946 __netpoll_cleanup(np);
950 static struct netpoll_info *team_netpoll_info(struct team *team)
952 return team->dev->npinfo;
956 static int team_port_enable_netpoll(struct team *team, struct team_port *port,
961 static void team_port_disable_netpoll(struct team_port *port)
964 static struct netpoll_info *team_netpoll_info(struct team *team)
970 static void __team_port_change_port_added(struct team_port *port, bool linkup);
971 static int team_dev_type_check_change(struct net_device *dev,
972 struct net_device *port_dev);
974 static int team_port_add(struct team *team, struct net_device *port_dev)
976 struct net_device *dev = team->dev;
977 struct team_port *port;
978 char *portname = port_dev->name;
981 if (port_dev->flags & IFF_LOOPBACK) {
982 netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
987 if (team_port_exists(port_dev)) {
988 netdev_err(dev, "Device %s is already a port "
989 "of a team device\n", portname);
993 if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
994 vlan_uses_dev(dev)) {
995 netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
1000 err = team_dev_type_check_change(dev, port_dev);
1004 if (port_dev->flags & IFF_UP) {
1005 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
1010 port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
1015 port->dev = port_dev;
1017 INIT_LIST_HEAD(&port->qom_list);
1019 port->orig.mtu = port_dev->mtu;
1020 err = dev_set_mtu(port_dev, dev->mtu);
1022 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
1026 memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
1028 err = team_port_enter(team, port);
1030 netdev_err(dev, "Device %s failed to enter team mode\n",
1032 goto err_port_enter;
1035 err = dev_open(port_dev);
1037 netdev_dbg(dev, "Device %s opening failed\n",
1042 err = vlan_vids_add_by_dev(port_dev, dev);
1044 netdev_err(dev, "Failed to add vlan ids to device %s\n",
1049 if (team_netpoll_info(team)) {
1050 err = team_port_enable_netpoll(team, port, GFP_KERNEL);
1052 netdev_err(dev, "Failed to enable netpoll on device %s\n",
1054 goto err_enable_netpoll;
1058 err = netdev_set_master(port_dev, dev);
1060 netdev_err(dev, "Device %s failed to set master\n", portname);
1061 goto err_set_master;
1064 err = netdev_rx_handler_register(port_dev, team_handle_frame,
1067 netdev_err(dev, "Device %s failed to register rx_handler\n",
1069 goto err_handler_register;
1072 err = __team_option_inst_add_port(team, port);
1074 netdev_err(dev, "Device %s failed to add per-port options\n",
1076 goto err_option_port_add;
1080 team_port_enable(team, port);
1081 list_add_tail_rcu(&port->list, &team->port_list);
1082 __team_compute_features(team);
1083 __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
1084 __team_options_change_check(team);
1086 netdev_info(dev, "Port device %s added\n", portname);
1090 err_option_port_add:
1091 netdev_rx_handler_unregister(port_dev);
1093 err_handler_register:
1094 netdev_set_master(port_dev, NULL);
1097 team_port_disable_netpoll(port);
1100 vlan_vids_del_by_dev(port_dev, dev);
1103 dev_close(port_dev);
1106 team_port_leave(team, port);
1107 team_port_set_orig_dev_addr(port);
1110 dev_set_mtu(port_dev, port->orig.mtu);
1118 static void __team_port_change_port_removed(struct team_port *port);
1120 static int team_port_del(struct team *team, struct net_device *port_dev)
1122 struct net_device *dev = team->dev;
1123 struct team_port *port;
1124 char *portname = port_dev->name;
1126 port = team_port_get_rtnl(port_dev);
1127 if (!port || !team_port_find(team, port)) {
1128 netdev_err(dev, "Device %s does not act as a port of this team\n",
1133 __team_option_inst_mark_removed_port(team, port);
1134 __team_options_change_check(team);
1135 __team_option_inst_del_port(team, port);
1136 __team_port_change_port_removed(port);
1137 team_port_disable(team, port);
1138 list_del_rcu(&port->list);
1139 netdev_rx_handler_unregister(port_dev);
1140 netdev_set_master(port_dev, NULL);
1141 team_port_disable_netpoll(port);
1142 vlan_vids_del_by_dev(port_dev, dev);
1143 dev_close(port_dev);
1144 team_port_leave(team, port);
1145 team_port_set_orig_dev_addr(port);
1146 dev_set_mtu(port_dev, port->orig.mtu);
1149 netdev_info(dev, "Port device %s removed\n", portname);
1150 __team_compute_features(team);
1160 static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
1162 ctx->data.str_val = team->mode->kind;
1166 static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
1168 return team_change_mode(team, ctx->data.str_val);
1171 static int team_port_en_option_get(struct team *team,
1172 struct team_gsetter_ctx *ctx)
1174 struct team_port *port = ctx->info->port;
1176 ctx->data.bool_val = team_port_enabled(port);
1180 static int team_port_en_option_set(struct team *team,
1181 struct team_gsetter_ctx *ctx)
1183 struct team_port *port = ctx->info->port;
1185 if (ctx->data.bool_val)
1186 team_port_enable(team, port);
1188 team_port_disable(team, port);
1192 static int team_user_linkup_option_get(struct team *team,
1193 struct team_gsetter_ctx *ctx)
1195 struct team_port *port = ctx->info->port;
1197 ctx->data.bool_val = port->user.linkup;
1201 static int team_user_linkup_option_set(struct team *team,
1202 struct team_gsetter_ctx *ctx)
1204 struct team_port *port = ctx->info->port;
1206 port->user.linkup = ctx->data.bool_val;
1207 team_refresh_port_linkup(port);
1211 static int team_user_linkup_en_option_get(struct team *team,
1212 struct team_gsetter_ctx *ctx)
1214 struct team_port *port = ctx->info->port;
1216 ctx->data.bool_val = port->user.linkup_enabled;
1220 static int team_user_linkup_en_option_set(struct team *team,
1221 struct team_gsetter_ctx *ctx)
1223 struct team_port *port = ctx->info->port;
1225 port->user.linkup_enabled = ctx->data.bool_val;
1226 team_refresh_port_linkup(port);
1230 static int team_priority_option_get(struct team *team,
1231 struct team_gsetter_ctx *ctx)
1233 struct team_port *port = ctx->info->port;
1235 ctx->data.s32_val = port->priority;
1239 static int team_priority_option_set(struct team *team,
1240 struct team_gsetter_ctx *ctx)
1242 struct team_port *port = ctx->info->port;
1244 port->priority = ctx->data.s32_val;
1245 team_queue_override_port_refresh(team, port);
1249 static int team_queue_id_option_get(struct team *team,
1250 struct team_gsetter_ctx *ctx)
1252 struct team_port *port = ctx->info->port;
1254 ctx->data.u32_val = port->queue_id;
1258 static int team_queue_id_option_set(struct team *team,
1259 struct team_gsetter_ctx *ctx)
1261 struct team_port *port = ctx->info->port;
1263 if (port->queue_id == ctx->data.u32_val)
1265 if (ctx->data.u32_val >= team->dev->real_num_tx_queues)
1267 port->queue_id = ctx->data.u32_val;
1268 team_queue_override_port_refresh(team, port);
1273 static const struct team_option team_options[] = {
1276 .type = TEAM_OPTION_TYPE_STRING,
1277 .getter = team_mode_option_get,
1278 .setter = team_mode_option_set,
1282 .type = TEAM_OPTION_TYPE_BOOL,
1284 .getter = team_port_en_option_get,
1285 .setter = team_port_en_option_set,
1288 .name = "user_linkup",
1289 .type = TEAM_OPTION_TYPE_BOOL,
1291 .getter = team_user_linkup_option_get,
1292 .setter = team_user_linkup_option_set,
1295 .name = "user_linkup_enabled",
1296 .type = TEAM_OPTION_TYPE_BOOL,
1298 .getter = team_user_linkup_en_option_get,
1299 .setter = team_user_linkup_en_option_set,
1303 .type = TEAM_OPTION_TYPE_S32,
1305 .getter = team_priority_option_get,
1306 .setter = team_priority_option_set,
1310 .type = TEAM_OPTION_TYPE_U32,
1312 .getter = team_queue_id_option_get,
1313 .setter = team_queue_id_option_set,
1317 static struct lock_class_key team_netdev_xmit_lock_key;
1318 static struct lock_class_key team_netdev_addr_lock_key;
1319 static struct lock_class_key team_tx_busylock_key;
1321 static void team_set_lockdep_class_one(struct net_device *dev,
1322 struct netdev_queue *txq,
1325 lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
1328 static void team_set_lockdep_class(struct net_device *dev)
1330 lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
1331 netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
1332 dev->qdisc_tx_busylock = &team_tx_busylock_key;
1335 static int team_init(struct net_device *dev)
1337 struct team *team = netdev_priv(dev);
1342 mutex_init(&team->lock);
1343 team_set_no_mode(team);
1345 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
1346 if (!team->pcpu_stats)
1349 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
1350 INIT_HLIST_HEAD(&team->en_port_hlist[i]);
1351 INIT_LIST_HEAD(&team->port_list);
1352 err = team_queue_override_init(team);
1354 goto err_team_queue_override_init;
1356 team_adjust_ops(team);
1358 INIT_LIST_HEAD(&team->option_list);
1359 INIT_LIST_HEAD(&team->option_inst_list);
1360 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
1362 goto err_options_register;
1363 netif_carrier_off(dev);
1365 team_set_lockdep_class(dev);
1369 err_options_register:
1370 team_queue_override_fini(team);
1371 err_team_queue_override_init:
1372 free_percpu(team->pcpu_stats);
1377 static void team_uninit(struct net_device *dev)
1379 struct team *team = netdev_priv(dev);
1380 struct team_port *port;
1381 struct team_port *tmp;
1383 mutex_lock(&team->lock);
1384 list_for_each_entry_safe(port, tmp, &team->port_list, list)
1385 team_port_del(team, port->dev);
1387 __team_change_mode(team, NULL); /* cleanup */
1388 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
1389 team_queue_override_fini(team);
1390 mutex_unlock(&team->lock);
1393 static void team_destructor(struct net_device *dev)
1395 struct team *team = netdev_priv(dev);
1397 free_percpu(team->pcpu_stats);
1401 static int team_open(struct net_device *dev)
1403 netif_carrier_on(dev);
1407 static int team_close(struct net_device *dev)
1409 netif_carrier_off(dev);
1414 * note: already called with rcu_read_lock
1416 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
1418 struct team *team = netdev_priv(dev);
1420 unsigned int len = skb->len;
1422 tx_success = team_queue_override_transmit(team, skb);
1424 tx_success = team->ops.transmit(team, skb);
1426 struct team_pcpu_stats *pcpu_stats;
1428 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
1429 u64_stats_update_begin(&pcpu_stats->syncp);
1430 pcpu_stats->tx_packets++;
1431 pcpu_stats->tx_bytes += len;
1432 u64_stats_update_end(&pcpu_stats->syncp);
1434 this_cpu_inc(team->pcpu_stats->tx_dropped);
1437 return NETDEV_TX_OK;
1440 static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
1443 * This helper function exists to help dev_pick_tx get the correct
1444 * destination queue. Using a helper function skips a call to
1445 * skb_tx_hash and will put the skbs in the queue we expect on their
1446 * way down to the team driver.
1448 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
1451 * Save the original txq to restore before passing to the driver
1453 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
1455 if (unlikely(txq >= dev->real_num_tx_queues)) {
1457 txq -= dev->real_num_tx_queues;
1458 } while (txq >= dev->real_num_tx_queues);
1463 static void team_change_rx_flags(struct net_device *dev, int change)
1465 struct team *team = netdev_priv(dev);
1466 struct team_port *port;
1470 list_for_each_entry_rcu(port, &team->port_list, list) {
1471 if (change & IFF_PROMISC) {
1472 inc = dev->flags & IFF_PROMISC ? 1 : -1;
1473 dev_set_promiscuity(port->dev, inc);
1475 if (change & IFF_ALLMULTI) {
1476 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
1477 dev_set_allmulti(port->dev, inc);
1483 static void team_set_rx_mode(struct net_device *dev)
1485 struct team *team = netdev_priv(dev);
1486 struct team_port *port;
1489 list_for_each_entry_rcu(port, &team->port_list, list) {
1490 dev_uc_sync(port->dev, dev);
1491 dev_mc_sync(port->dev, dev);
1496 static int team_set_mac_address(struct net_device *dev, void *p)
1498 struct sockaddr *addr = p;
1499 struct team *team = netdev_priv(dev);
1500 struct team_port *port;
1502 if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
1503 return -EADDRNOTAVAIL;
1504 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1505 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
1507 list_for_each_entry_rcu(port, &team->port_list, list)
1508 if (team->ops.port_change_dev_addr)
1509 team->ops.port_change_dev_addr(team, port);
1514 static int team_change_mtu(struct net_device *dev, int new_mtu)
1516 struct team *team = netdev_priv(dev);
1517 struct team_port *port;
1521 * Alhough this is reader, it's guarded by team lock. It's not possible
1522 * to traverse list in reverse under rcu_read_lock
1524 mutex_lock(&team->lock);
1525 list_for_each_entry(port, &team->port_list, list) {
1526 err = dev_set_mtu(port->dev, new_mtu);
1528 netdev_err(dev, "Device %s failed to change mtu",
1533 mutex_unlock(&team->lock);
1540 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1541 dev_set_mtu(port->dev, dev->mtu);
1542 mutex_unlock(&team->lock);
1547 static struct rtnl_link_stats64 *
1548 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1550 struct team *team = netdev_priv(dev);
1551 struct team_pcpu_stats *p;
1552 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
1553 u32 rx_dropped = 0, tx_dropped = 0;
1557 for_each_possible_cpu(i) {
1558 p = per_cpu_ptr(team->pcpu_stats, i);
1560 start = u64_stats_fetch_begin_bh(&p->syncp);
1561 rx_packets = p->rx_packets;
1562 rx_bytes = p->rx_bytes;
1563 rx_multicast = p->rx_multicast;
1564 tx_packets = p->tx_packets;
1565 tx_bytes = p->tx_bytes;
1566 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
1568 stats->rx_packets += rx_packets;
1569 stats->rx_bytes += rx_bytes;
1570 stats->multicast += rx_multicast;
1571 stats->tx_packets += tx_packets;
1572 stats->tx_bytes += tx_bytes;
1574 * rx_dropped & tx_dropped are u32, updated
1575 * without syncp protection.
1577 rx_dropped += p->rx_dropped;
1578 tx_dropped += p->tx_dropped;
1580 stats->rx_dropped = rx_dropped;
1581 stats->tx_dropped = tx_dropped;
1585 static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
1587 struct team *team = netdev_priv(dev);
1588 struct team_port *port;
1592 * Alhough this is reader, it's guarded by team lock. It's not possible
1593 * to traverse list in reverse under rcu_read_lock
1595 mutex_lock(&team->lock);
1596 list_for_each_entry(port, &team->port_list, list) {
1597 err = vlan_vid_add(port->dev, vid);
1601 mutex_unlock(&team->lock);
1606 list_for_each_entry_continue_reverse(port, &team->port_list, list)
1607 vlan_vid_del(port->dev, vid);
1608 mutex_unlock(&team->lock);
1613 static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
1615 struct team *team = netdev_priv(dev);
1616 struct team_port *port;
1619 list_for_each_entry_rcu(port, &team->port_list, list)
1620 vlan_vid_del(port->dev, vid);
1626 #ifdef CONFIG_NET_POLL_CONTROLLER
1627 static void team_poll_controller(struct net_device *dev)
1631 static void __team_netpoll_cleanup(struct team *team)
1633 struct team_port *port;
1635 list_for_each_entry(port, &team->port_list, list)
1636 team_port_disable_netpoll(port);
1639 static void team_netpoll_cleanup(struct net_device *dev)
1641 struct team *team = netdev_priv(dev);
1643 mutex_lock(&team->lock);
1644 __team_netpoll_cleanup(team);
1645 mutex_unlock(&team->lock);
1648 static int team_netpoll_setup(struct net_device *dev,
1649 struct netpoll_info *npifo, gfp_t gfp)
1651 struct team *team = netdev_priv(dev);
1652 struct team_port *port;
1655 mutex_lock(&team->lock);
1656 list_for_each_entry(port, &team->port_list, list) {
1657 err = team_port_enable_netpoll(team, port, gfp);
1659 __team_netpoll_cleanup(team);
1663 mutex_unlock(&team->lock);
1668 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
1670 struct team *team = netdev_priv(dev);
1673 mutex_lock(&team->lock);
1674 err = team_port_add(team, port_dev);
1675 mutex_unlock(&team->lock);
1679 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
1681 struct team *team = netdev_priv(dev);
1684 mutex_lock(&team->lock);
1685 err = team_port_del(team, port_dev);
1686 mutex_unlock(&team->lock);
1690 static netdev_features_t team_fix_features(struct net_device *dev,
1691 netdev_features_t features)
1693 struct team_port *port;
1694 struct team *team = netdev_priv(dev);
1695 netdev_features_t mask;
1698 features &= ~NETIF_F_ONE_FOR_ALL;
1699 features |= NETIF_F_ALL_FOR_ALL;
1702 list_for_each_entry_rcu(port, &team->port_list, list) {
1703 features = netdev_increment_features(features,
1704 port->dev->features,
1711 static int team_change_carrier(struct net_device *dev, bool new_carrier)
1714 netif_carrier_on(dev);
1716 netif_carrier_off(dev);
1720 static const struct net_device_ops team_netdev_ops = {
1721 .ndo_init = team_init,
1722 .ndo_uninit = team_uninit,
1723 .ndo_open = team_open,
1724 .ndo_stop = team_close,
1725 .ndo_start_xmit = team_xmit,
1726 .ndo_select_queue = team_select_queue,
1727 .ndo_change_rx_flags = team_change_rx_flags,
1728 .ndo_set_rx_mode = team_set_rx_mode,
1729 .ndo_set_mac_address = team_set_mac_address,
1730 .ndo_change_mtu = team_change_mtu,
1731 .ndo_get_stats64 = team_get_stats64,
1732 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1733 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1734 #ifdef CONFIG_NET_POLL_CONTROLLER
1735 .ndo_poll_controller = team_poll_controller,
1736 .ndo_netpoll_setup = team_netpoll_setup,
1737 .ndo_netpoll_cleanup = team_netpoll_cleanup,
1739 .ndo_add_slave = team_add_slave,
1740 .ndo_del_slave = team_del_slave,
1741 .ndo_fix_features = team_fix_features,
1742 .ndo_change_carrier = team_change_carrier,
1745 /***********************
1747 ***********************/
1749 static void team_ethtool_get_drvinfo(struct net_device *dev,
1750 struct ethtool_drvinfo *drvinfo)
1752 strncpy(drvinfo->driver, DRV_NAME, 32);
1753 strncpy(drvinfo->version, UTS_RELEASE, 32);
1756 static const struct ethtool_ops team_ethtool_ops = {
1757 .get_drvinfo = team_ethtool_get_drvinfo,
1758 .get_link = ethtool_op_get_link,
1761 /***********************
1762 * rt netlink interface
1763 ***********************/
1765 static void team_setup_by_port(struct net_device *dev,
1766 struct net_device *port_dev)
1768 dev->header_ops = port_dev->header_ops;
1769 dev->type = port_dev->type;
1770 dev->hard_header_len = port_dev->hard_header_len;
1771 dev->addr_len = port_dev->addr_len;
1772 dev->mtu = port_dev->mtu;
1773 memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
1774 memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
1775 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
1778 static int team_dev_type_check_change(struct net_device *dev,
1779 struct net_device *port_dev)
1781 struct team *team = netdev_priv(dev);
1782 char *portname = port_dev->name;
1785 if (dev->type == port_dev->type)
1787 if (!list_empty(&team->port_list)) {
1788 netdev_err(dev, "Device %s is of different type\n", portname);
1791 err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
1792 err = notifier_to_errno(err);
1794 netdev_err(dev, "Refused to change device type\n");
1799 team_setup_by_port(dev, port_dev);
1800 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1804 static void team_setup(struct net_device *dev)
1808 dev->netdev_ops = &team_netdev_ops;
1809 dev->ethtool_ops = &team_ethtool_ops;
1810 dev->destructor = team_destructor;
1811 dev->tx_queue_len = 0;
1812 dev->flags |= IFF_MULTICAST;
1813 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1816 * Indicate we support unicast address filtering. That way core won't
1817 * bring us to promisc mode in case a unicast addr is added.
1818 * Let this up to underlay drivers.
1820 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
1822 dev->features |= NETIF_F_LLTX;
1823 dev->features |= NETIF_F_GRO;
1824 dev->hw_features = TEAM_VLAN_FEATURES |
1825 NETIF_F_HW_VLAN_TX |
1826 NETIF_F_HW_VLAN_RX |
1827 NETIF_F_HW_VLAN_FILTER;
1829 dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
1830 dev->features |= dev->hw_features;
1833 static int team_newlink(struct net *src_net, struct net_device *dev,
1834 struct nlattr *tb[], struct nlattr *data[])
1838 if (tb[IFLA_ADDRESS] == NULL)
1839 eth_hw_addr_random(dev);
1841 err = register_netdevice(dev);
1848 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1850 if (tb[IFLA_ADDRESS]) {
1851 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1853 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1854 return -EADDRNOTAVAIL;
1859 static unsigned int team_get_num_tx_queues(void)
1861 return TEAM_DEFAULT_NUM_TX_QUEUES;
1864 static unsigned int team_get_num_rx_queues(void)
1866 return TEAM_DEFAULT_NUM_RX_QUEUES;
1869 static struct rtnl_link_ops team_link_ops __read_mostly = {
1871 .priv_size = sizeof(struct team),
1872 .setup = team_setup,
1873 .newlink = team_newlink,
1874 .validate = team_validate,
1875 .get_num_tx_queues = team_get_num_tx_queues,
1876 .get_num_rx_queues = team_get_num_rx_queues,
1880 /***********************************
1881 * Generic netlink custom interface
1882 ***********************************/
1884 static struct genl_family team_nl_family = {
1885 .id = GENL_ID_GENERATE,
1886 .name = TEAM_GENL_NAME,
1887 .version = TEAM_GENL_VERSION,
1888 .maxattr = TEAM_ATTR_MAX,
1892 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1893 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1894 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1895 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1896 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1899 static const struct nla_policy
1900 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1901 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1902 [TEAM_ATTR_OPTION_NAME] = {
1904 .len = TEAM_STRING_MAX_LEN,
1906 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1907 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
1908 [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
1911 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1913 struct sk_buff *msg;
1917 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1921 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1922 &team_nl_family, 0, TEAM_CMD_NOOP);
1928 genlmsg_end(msg, hdr);
1930 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
1939 * Netlink cmd functions should be locked by following two functions.
1940 * Since dev gets held here, that ensures dev won't disappear in between.
1942 static struct team *team_nl_team_get(struct genl_info *info)
1944 struct net *net = genl_info_net(info);
1946 struct net_device *dev;
1949 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1952 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
1953 dev = dev_get_by_index(net, ifindex);
1954 if (!dev || dev->netdev_ops != &team_netdev_ops) {
1960 team = netdev_priv(dev);
1961 mutex_lock(&team->lock);
1965 static void team_nl_team_put(struct team *team)
1967 mutex_unlock(&team->lock);
1971 static int team_nl_send_generic(struct genl_info *info, struct team *team,
1972 int (*fill_func)(struct sk_buff *skb,
1973 struct genl_info *info,
1974 int flags, struct team *team))
1976 struct sk_buff *skb;
1979 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1983 err = fill_func(skb, info, NLM_F_ACK, team);
1987 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_portid);
1995 typedef int team_nl_send_func_t(struct sk_buff *skb,
1996 struct team *team, u32 portid);
1998 static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
2000 return genlmsg_unicast(dev_net(team->dev), skb, portid);
2003 static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
2004 struct team_option_inst *opt_inst)
2006 struct nlattr *option_item;
2007 struct team_option *option = opt_inst->option;
2008 struct team_option_inst_info *opt_inst_info = &opt_inst->info;
2009 struct team_gsetter_ctx ctx;
2012 ctx.info = opt_inst_info;
2013 err = team_option_get(team, opt_inst, &ctx);
2017 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
2021 if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
2023 if (opt_inst_info->port &&
2024 nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
2025 opt_inst_info->port->dev->ifindex))
2027 if (opt_inst->option->array_size &&
2028 nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
2029 opt_inst_info->array_index))
2032 switch (option->type) {
2033 case TEAM_OPTION_TYPE_U32:
2034 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
2036 if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
2039 case TEAM_OPTION_TYPE_STRING:
2040 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
2042 if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
2046 case TEAM_OPTION_TYPE_BINARY:
2047 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
2049 if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
2050 ctx.data.bin_val.ptr))
2053 case TEAM_OPTION_TYPE_BOOL:
2054 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
2056 if (ctx.data.bool_val &&
2057 nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
2060 case TEAM_OPTION_TYPE_S32:
2061 if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
2063 if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
2069 if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
2071 if (opt_inst->changed) {
2072 if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
2074 opt_inst->changed = false;
2076 nla_nest_end(skb, option_item);
2080 nla_nest_cancel(skb, option_item);
2084 static int __send_and_alloc_skb(struct sk_buff **pskb,
2085 struct team *team, u32 portid,
2086 team_nl_send_func_t *send_func)
2091 err = send_func(*pskb, team, portid);
2095 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2101 static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
2102 int flags, team_nl_send_func_t *send_func,
2103 struct list_head *sel_opt_inst_list)
2105 struct nlattr *option_list;
2106 struct nlmsghdr *nlh;
2108 struct team_option_inst *opt_inst;
2110 struct sk_buff *skb = NULL;
2114 opt_inst = list_first_entry(sel_opt_inst_list,
2115 struct team_option_inst, tmp_list);
2118 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2122 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
2123 TEAM_CMD_OPTIONS_GET);
2127 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2128 goto nla_put_failure;
2129 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
2131 goto nla_put_failure;
2135 list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
2136 err = team_nl_fill_one_option_get(skb, team, opt_inst);
2138 if (err == -EMSGSIZE) {
2149 nla_nest_end(skb, option_list);
2150 genlmsg_end(skb, hdr);
2155 nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
2157 err = __send_and_alloc_skb(&skb, team, portid, send_func);
2163 return send_func(skb, team, portid);
2168 genlmsg_cancel(skb, hdr);
2173 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
2176 struct team_option_inst *opt_inst;
2178 LIST_HEAD(sel_opt_inst_list);
2180 team = team_nl_team_get(info);
2184 list_for_each_entry(opt_inst, &team->option_inst_list, list)
2185 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2186 err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
2187 NLM_F_ACK, team_nl_send_unicast,
2188 &sel_opt_inst_list);
2190 team_nl_team_put(team);
2195 static int team_nl_send_event_options_get(struct team *team,
2196 struct list_head *sel_opt_inst_list);
2198 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
2203 struct nlattr *nl_option;
2204 LIST_HEAD(opt_inst_list);
2206 team = team_nl_team_get(info);
2211 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
2216 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
2217 struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
2218 struct nlattr *attr;
2219 struct nlattr *attr_data;
2220 enum team_option_type opt_type;
2221 int opt_port_ifindex = 0; /* != 0 for per-port options */
2222 u32 opt_array_index = 0;
2223 bool opt_is_array = false;
2224 struct team_option_inst *opt_inst;
2226 bool opt_found = false;
2228 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
2232 err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
2233 nl_option, team_nl_option_policy);
2236 if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
2237 !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
2241 switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
2243 opt_type = TEAM_OPTION_TYPE_U32;
2246 opt_type = TEAM_OPTION_TYPE_STRING;
2249 opt_type = TEAM_OPTION_TYPE_BINARY;
2252 opt_type = TEAM_OPTION_TYPE_BOOL;
2255 opt_type = TEAM_OPTION_TYPE_S32;
2261 attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
2262 if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
2267 opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
2268 attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
2270 opt_port_ifindex = nla_get_u32(attr);
2272 attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
2274 opt_is_array = true;
2275 opt_array_index = nla_get_u32(attr);
2278 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2279 struct team_option *option = opt_inst->option;
2280 struct team_gsetter_ctx ctx;
2281 struct team_option_inst_info *opt_inst_info;
2284 opt_inst_info = &opt_inst->info;
2285 tmp_ifindex = opt_inst_info->port ?
2286 opt_inst_info->port->dev->ifindex : 0;
2287 if (option->type != opt_type ||
2288 strcmp(option->name, opt_name) ||
2289 tmp_ifindex != opt_port_ifindex ||
2290 (option->array_size && !opt_is_array) ||
2291 opt_inst_info->array_index != opt_array_index)
2294 ctx.info = opt_inst_info;
2296 case TEAM_OPTION_TYPE_U32:
2297 ctx.data.u32_val = nla_get_u32(attr_data);
2299 case TEAM_OPTION_TYPE_STRING:
2300 if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
2304 ctx.data.str_val = nla_data(attr_data);
2306 case TEAM_OPTION_TYPE_BINARY:
2307 ctx.data.bin_val.len = nla_len(attr_data);
2308 ctx.data.bin_val.ptr = nla_data(attr_data);
2310 case TEAM_OPTION_TYPE_BOOL:
2311 ctx.data.bool_val = attr_data ? true : false;
2313 case TEAM_OPTION_TYPE_S32:
2314 ctx.data.s32_val = nla_get_s32(attr_data);
2319 err = team_option_set(team, opt_inst, &ctx);
2322 opt_inst->changed = true;
2323 list_add(&opt_inst->tmp_list, &opt_inst_list);
2331 err = team_nl_send_event_options_get(team, &opt_inst_list);
2334 team_nl_team_put(team);
2339 static int team_nl_fill_port_list_get(struct sk_buff *skb,
2340 u32 portid, u32 seq, int flags,
2344 struct nlattr *port_list;
2346 struct team_port *port;
2348 hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags,
2349 TEAM_CMD_PORT_LIST_GET);
2353 if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
2354 goto nla_put_failure;
2355 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
2357 goto nla_put_failure;
2359 list_for_each_entry(port, &team->port_list, list) {
2360 struct nlattr *port_item;
2362 /* Include only changed ports if fill all mode is not on */
2363 if (!fillall && !port->changed)
2365 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
2367 goto nla_put_failure;
2368 if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
2369 goto nla_put_failure;
2370 if (port->changed) {
2371 if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
2372 goto nla_put_failure;
2373 port->changed = false;
2375 if ((port->removed &&
2376 nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
2377 (port->state.linkup &&
2378 nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
2379 nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
2380 nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
2381 goto nla_put_failure;
2382 nla_nest_end(skb, port_item);
2385 nla_nest_end(skb, port_list);
2386 return genlmsg_end(skb, hdr);
2389 genlmsg_cancel(skb, hdr);
2393 static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
2394 struct genl_info *info, int flags,
2397 return team_nl_fill_port_list_get(skb, info->snd_portid,
2398 info->snd_seq, NLM_F_ACK,
2402 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
2403 struct genl_info *info)
2408 team = team_nl_team_get(info);
2412 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
2414 team_nl_team_put(team);
2419 static struct genl_ops team_nl_ops[] = {
2421 .cmd = TEAM_CMD_NOOP,
2422 .doit = team_nl_cmd_noop,
2423 .policy = team_nl_policy,
2426 .cmd = TEAM_CMD_OPTIONS_SET,
2427 .doit = team_nl_cmd_options_set,
2428 .policy = team_nl_policy,
2429 .flags = GENL_ADMIN_PERM,
2432 .cmd = TEAM_CMD_OPTIONS_GET,
2433 .doit = team_nl_cmd_options_get,
2434 .policy = team_nl_policy,
2435 .flags = GENL_ADMIN_PERM,
2438 .cmd = TEAM_CMD_PORT_LIST_GET,
2439 .doit = team_nl_cmd_port_list_get,
2440 .policy = team_nl_policy,
2441 .flags = GENL_ADMIN_PERM,
2445 static struct genl_multicast_group team_change_event_mcgrp = {
2446 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
2449 static int team_nl_send_multicast(struct sk_buff *skb,
2450 struct team *team, u32 portid)
2452 return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
2453 team_change_event_mcgrp.id, GFP_KERNEL);
2456 static int team_nl_send_event_options_get(struct team *team,
2457 struct list_head *sel_opt_inst_list)
2459 return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
2463 static int team_nl_send_event_port_list_get(struct team *team)
2465 struct sk_buff *skb;
2467 struct net *net = dev_net(team->dev);
2469 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2473 err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
2477 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
2486 static int team_nl_init(void)
2490 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
2491 ARRAY_SIZE(team_nl_ops));
2495 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
2497 goto err_change_event_grp_reg;
2501 err_change_event_grp_reg:
2502 genl_unregister_family(&team_nl_family);
2507 static void team_nl_fini(void)
2509 genl_unregister_family(&team_nl_family);
2517 static void __team_options_change_check(struct team *team)
2520 struct team_option_inst *opt_inst;
2521 LIST_HEAD(sel_opt_inst_list);
2523 list_for_each_entry(opt_inst, &team->option_inst_list, list) {
2524 if (opt_inst->changed)
2525 list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
2527 err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
2528 if (err && err != -ESRCH)
2529 netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
2533 /* rtnl lock is held */
2535 static void __team_port_change_send(struct team_port *port, bool linkup)
2539 port->changed = true;
2540 port->state.linkup = linkup;
2541 team_refresh_port_linkup(port);
2543 struct ethtool_cmd ecmd;
2545 err = __ethtool_get_settings(port->dev, &ecmd);
2547 port->state.speed = ethtool_cmd_speed(&ecmd);
2548 port->state.duplex = ecmd.duplex;
2552 port->state.speed = 0;
2553 port->state.duplex = 0;
2556 err = team_nl_send_event_port_list_get(port->team);
2557 if (err && err != -ESRCH)
2558 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
2559 port->dev->name, err);
2563 static void __team_port_change_check(struct team_port *port, bool linkup)
2565 if (port->state.linkup != linkup)
2566 __team_port_change_send(port, linkup);
2569 static void __team_port_change_port_added(struct team_port *port, bool linkup)
2571 __team_port_change_send(port, linkup);
2574 static void __team_port_change_port_removed(struct team_port *port)
2576 port->removed = true;
2577 __team_port_change_send(port, false);
2580 static void team_port_change_check(struct team_port *port, bool linkup)
2582 struct team *team = port->team;
2584 mutex_lock(&team->lock);
2585 __team_port_change_check(port, linkup);
2586 mutex_unlock(&team->lock);
2590 /************************************
2591 * Net device notifier event handler
2592 ************************************/
2594 static int team_device_event(struct notifier_block *unused,
2595 unsigned long event, void *ptr)
2597 struct net_device *dev = (struct net_device *) ptr;
2598 struct team_port *port;
2600 port = team_port_get_rtnl(dev);
2606 if (netif_carrier_ok(dev))
2607 team_port_change_check(port, true);
2609 team_port_change_check(port, false);
2611 if (netif_running(port->dev))
2612 team_port_change_check(port,
2613 !!netif_carrier_ok(port->dev));
2615 case NETDEV_UNREGISTER:
2616 team_del_slave(port->team->dev, dev);
2618 case NETDEV_FEAT_CHANGE:
2619 team_compute_features(port->team);
2621 case NETDEV_CHANGEMTU:
2622 /* Forbid to change mtu of underlaying device */
2624 case NETDEV_PRE_TYPE_CHANGE:
2625 /* Forbid to change type of underlaying device */
2631 static struct notifier_block team_notifier_block __read_mostly = {
2632 .notifier_call = team_device_event,
2636 /***********************
2637 * Module init and exit
2638 ***********************/
2640 static int __init team_module_init(void)
2644 register_netdevice_notifier(&team_notifier_block);
2646 err = rtnl_link_register(&team_link_ops);
2650 err = team_nl_init();
2657 rtnl_link_unregister(&team_link_ops);
2660 unregister_netdevice_notifier(&team_notifier_block);
2665 static void __exit team_module_exit(void)
2668 rtnl_link_unregister(&team_link_ops);
2669 unregister_netdevice_notifier(&team_notifier_block);
2672 module_init(team_module_init);
2673 module_exit(team_module_exit);
2675 MODULE_LICENSE("GPL v2");
2676 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2677 MODULE_DESCRIPTION("Ethernet team device driver");
2678 MODULE_ALIAS_RTNL_LINK(DRV_NAME);