2 * net/drivers/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/if_vlan.h>
22 #include <linux/if_arp.h>
23 #include <linux/socket.h>
24 #include <linux/etherdevice.h>
25 #include <linux/rtnetlink.h>
26 #include <net/rtnetlink.h>
27 #include <net/genetlink.h>
28 #include <net/netlink.h>
29 #include <linux/if_team.h>
31 #define DRV_NAME "team"
38 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
40 static struct team_port *team_port_get_rcu(const struct net_device *dev)
42 struct team_port *port = rcu_dereference(dev->rx_handler_data);
44 return team_port_exists(dev) ? port : NULL;
47 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
49 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
51 return team_port_exists(dev) ? port : NULL;
55 * Since the ability to change mac address for open port device is tested in
56 * team_port_add, this function can be called without control of return value
58 static int __set_port_mac(struct net_device *port_dev,
59 const unsigned char *dev_addr)
63 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
64 addr.sa_family = ARPHRD_ETHER;
65 return dev_set_mac_address(port_dev, &addr);
68 int team_port_set_orig_mac(struct team_port *port)
70 return __set_port_mac(port->dev, port->orig.dev_addr);
73 int team_port_set_team_mac(struct team_port *port)
75 return __set_port_mac(port->dev, port->team->dev->dev_addr);
77 EXPORT_SYMBOL(team_port_set_team_mac);
84 struct team_option *__team_find_option(struct team *team, const char *opt_name)
86 struct team_option *option;
88 list_for_each_entry(option, &team->option_list, list) {
89 if (strcmp(option->name, opt_name) == 0)
95 int team_options_register(struct team *team,
96 const struct team_option *option,
100 struct team_option **dst_opts;
103 dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
107 for (i = 0; i < option_count; i++, option++) {
108 if (__team_find_option(team, option->name)) {
112 dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
119 for (i = 0; i < option_count; i++)
120 list_add_tail(&dst_opts[i]->list, &team->option_list);
126 for (i = 0; i < option_count; i++)
133 EXPORT_SYMBOL(team_options_register);
135 static void __team_options_change_check(struct team *team,
136 struct team_option *changed_option);
138 static void __team_options_unregister(struct team *team,
139 const struct team_option *option,
144 for (i = 0; i < option_count; i++, option++) {
145 struct team_option *del_opt;
147 del_opt = __team_find_option(team, option->name);
149 list_del(&del_opt->list);
155 void team_options_unregister(struct team *team,
156 const struct team_option *option,
159 __team_options_unregister(team, option, option_count);
160 __team_options_change_check(team, NULL);
162 EXPORT_SYMBOL(team_options_unregister);
164 static int team_option_get(struct team *team, struct team_option *option,
167 return option->getter(team, arg);
170 static int team_option_set(struct team *team, struct team_option *option,
175 err = option->setter(team, arg);
179 __team_options_change_check(team, option);
187 static LIST_HEAD(mode_list);
188 static DEFINE_SPINLOCK(mode_list_lock);
190 static struct team_mode *__find_mode(const char *kind)
192 struct team_mode *mode;
194 list_for_each_entry(mode, &mode_list, list) {
195 if (strcmp(mode->kind, kind) == 0)
201 static bool is_good_mode_name(const char *name)
203 while (*name != '\0') {
204 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
211 int team_mode_register(struct team_mode *mode)
215 if (!is_good_mode_name(mode->kind) ||
216 mode->priv_size > TEAM_MODE_PRIV_SIZE)
218 spin_lock(&mode_list_lock);
219 if (__find_mode(mode->kind)) {
223 list_add_tail(&mode->list, &mode_list);
225 spin_unlock(&mode_list_lock);
228 EXPORT_SYMBOL(team_mode_register);
230 int team_mode_unregister(struct team_mode *mode)
232 spin_lock(&mode_list_lock);
233 list_del_init(&mode->list);
234 spin_unlock(&mode_list_lock);
237 EXPORT_SYMBOL(team_mode_unregister);
239 static struct team_mode *team_mode_get(const char *kind)
241 struct team_mode *mode;
243 spin_lock(&mode_list_lock);
244 mode = __find_mode(kind);
246 spin_unlock(&mode_list_lock);
247 request_module("team-mode-%s", kind);
248 spin_lock(&mode_list_lock);
249 mode = __find_mode(kind);
252 if (!try_module_get(mode->owner))
255 spin_unlock(&mode_list_lock);
259 static void team_mode_put(const struct team_mode *mode)
261 module_put(mode->owner);
264 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
266 dev_kfree_skb_any(skb);
270 rx_handler_result_t team_dummy_receive(struct team *team,
271 struct team_port *port,
274 return RX_HANDLER_ANOTHER;
277 static void team_adjust_ops(struct team *team)
280 * To avoid checks in rx/tx skb paths, ensure here that non-null and
281 * correct ops are always set.
284 if (list_empty(&team->port_list) ||
285 !team->mode || !team->mode->ops->transmit)
286 team->ops.transmit = team_dummy_transmit;
288 team->ops.transmit = team->mode->ops->transmit;
290 if (list_empty(&team->port_list) ||
291 !team->mode || !team->mode->ops->receive)
292 team->ops.receive = team_dummy_receive;
294 team->ops.receive = team->mode->ops->receive;
298 * We can benefit from the fact that it's ensured no port is present
299 * at the time of mode change. Therefore no packets are in fly so there's no
300 * need to set mode operations in any special way.
302 static int __team_change_mode(struct team *team,
303 const struct team_mode *new_mode)
305 /* Check if mode was previously set and do cleanup if so */
307 void (*exit_op)(struct team *team) = team->ops.exit;
309 /* Clear ops area so no callback is called any longer */
310 memset(&team->ops, 0, sizeof(struct team_mode_ops));
311 team_adjust_ops(team);
315 team_mode_put(team->mode);
317 /* zero private data area */
318 memset(&team->mode_priv, 0,
319 sizeof(struct team) - offsetof(struct team, mode_priv));
325 if (new_mode->ops->init) {
328 err = new_mode->ops->init(team);
333 team->mode = new_mode;
334 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
335 team_adjust_ops(team);
340 static int team_change_mode(struct team *team, const char *kind)
342 struct team_mode *new_mode;
343 struct net_device *dev = team->dev;
346 if (!list_empty(&team->port_list)) {
347 netdev_err(dev, "No ports can be present during mode change\n");
351 if (team->mode && strcmp(team->mode->kind, kind) == 0) {
352 netdev_err(dev, "Unable to change to the same mode the team is in\n");
356 new_mode = team_mode_get(kind);
358 netdev_err(dev, "Mode \"%s\" not found\n", kind);
362 err = __team_change_mode(team, new_mode);
364 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
365 team_mode_put(new_mode);
369 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
374 /************************
375 * Rx path frame handler
376 ************************/
378 /* note: already called with rcu_read_lock */
379 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
381 struct sk_buff *skb = *pskb;
382 struct team_port *port;
384 rx_handler_result_t res;
386 skb = skb_share_check(skb, GFP_ATOMIC);
388 return RX_HANDLER_CONSUMED;
392 port = team_port_get_rcu(skb->dev);
395 res = team->ops.receive(team, port, skb);
396 if (res == RX_HANDLER_ANOTHER) {
397 struct team_pcpu_stats *pcpu_stats;
399 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
400 u64_stats_update_begin(&pcpu_stats->syncp);
401 pcpu_stats->rx_packets++;
402 pcpu_stats->rx_bytes += skb->len;
403 if (skb->pkt_type == PACKET_MULTICAST)
404 pcpu_stats->rx_multicast++;
405 u64_stats_update_end(&pcpu_stats->syncp);
407 skb->dev = team->dev;
409 this_cpu_inc(team->pcpu_stats->rx_dropped);
420 static bool team_port_find(const struct team *team,
421 const struct team_port *port)
423 struct team_port *cur;
425 list_for_each_entry(cur, &team->port_list, list)
432 * Add/delete port to the team port list. Write guarded by rtnl_lock.
433 * Takes care of correct port->index setup (might be racy).
435 static void team_port_list_add_port(struct team *team,
436 struct team_port *port)
438 port->index = team->port_count++;
439 hlist_add_head_rcu(&port->hlist,
440 team_port_index_hash(team, port->index));
441 list_add_tail_rcu(&port->list, &team->port_list);
444 static void __reconstruct_port_hlist(struct team *team, int rm_index)
447 struct team_port *port;
449 for (i = rm_index + 1; i < team->port_count; i++) {
450 port = team_get_port_by_index(team, i);
451 hlist_del_rcu(&port->hlist);
453 hlist_add_head_rcu(&port->hlist,
454 team_port_index_hash(team, port->index));
458 static void team_port_list_del_port(struct team *team,
459 struct team_port *port)
461 int rm_index = port->index;
463 hlist_del_rcu(&port->hlist);
464 list_del_rcu(&port->list);
465 __reconstruct_port_hlist(team, rm_index);
469 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
470 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
471 NETIF_F_HIGHDMA | NETIF_F_LRO)
473 static void __team_compute_features(struct team *team)
475 struct team_port *port;
476 u32 vlan_features = TEAM_VLAN_FEATURES;
477 unsigned short max_hard_header_len = ETH_HLEN;
479 list_for_each_entry(port, &team->port_list, list) {
480 vlan_features = netdev_increment_features(vlan_features,
481 port->dev->vlan_features,
484 if (port->dev->hard_header_len > max_hard_header_len)
485 max_hard_header_len = port->dev->hard_header_len;
488 team->dev->vlan_features = vlan_features;
489 team->dev->hard_header_len = max_hard_header_len;
491 netdev_change_features(team->dev);
494 static void team_compute_features(struct team *team)
496 mutex_lock(&team->lock);
497 __team_compute_features(team);
498 mutex_unlock(&team->lock);
501 static int team_port_enter(struct team *team, struct team_port *port)
506 port->dev->priv_flags |= IFF_TEAM_PORT;
507 if (team->ops.port_enter) {
508 err = team->ops.port_enter(team, port);
510 netdev_err(team->dev, "Device %s failed to enter team mode\n",
519 port->dev->priv_flags &= ~IFF_TEAM_PORT;
525 static void team_port_leave(struct team *team, struct team_port *port)
527 if (team->ops.port_leave)
528 team->ops.port_leave(team, port);
529 port->dev->priv_flags &= ~IFF_TEAM_PORT;
533 static void __team_port_change_check(struct team_port *port, bool linkup);
535 static int team_port_add(struct team *team, struct net_device *port_dev)
537 struct net_device *dev = team->dev;
538 struct team_port *port;
539 char *portname = port_dev->name;
542 if (port_dev->flags & IFF_LOOPBACK ||
543 port_dev->type != ARPHRD_ETHER) {
544 netdev_err(dev, "Device %s is of an unsupported type\n",
549 if (team_port_exists(port_dev)) {
550 netdev_err(dev, "Device %s is already a port "
551 "of a team device\n", portname);
555 if (port_dev->flags & IFF_UP) {
556 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
561 port = kzalloc(sizeof(struct team_port), GFP_KERNEL);
565 port->dev = port_dev;
568 port->orig.mtu = port_dev->mtu;
569 err = dev_set_mtu(port_dev, dev->mtu);
571 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
575 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
577 err = team_port_enter(team, port);
579 netdev_err(dev, "Device %s failed to enter team mode\n",
584 err = dev_open(port_dev);
586 netdev_dbg(dev, "Device %s opening failed\n",
591 err = vlan_vids_add_by_dev(port_dev, dev);
593 netdev_err(dev, "Failed to add vlan ids to device %s\n",
598 err = netdev_set_master(port_dev, dev);
600 netdev_err(dev, "Device %s failed to set master\n", portname);
604 err = netdev_rx_handler_register(port_dev, team_handle_frame,
607 netdev_err(dev, "Device %s failed to register rx_handler\n",
609 goto err_handler_register;
612 team_port_list_add_port(team, port);
613 team_adjust_ops(team);
614 __team_compute_features(team);
615 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
617 netdev_info(dev, "Port device %s added\n", portname);
621 err_handler_register:
622 netdev_set_master(port_dev, NULL);
625 vlan_vids_del_by_dev(port_dev, dev);
631 team_port_leave(team, port);
632 team_port_set_orig_mac(port);
635 dev_set_mtu(port_dev, port->orig.mtu);
643 static int team_port_del(struct team *team, struct net_device *port_dev)
645 struct net_device *dev = team->dev;
646 struct team_port *port;
647 char *portname = port_dev->name;
649 port = team_port_get_rtnl(port_dev);
650 if (!port || !team_port_find(team, port)) {
651 netdev_err(dev, "Device %s does not act as a port of this team\n",
656 __team_port_change_check(port, false);
657 team_port_list_del_port(team, port);
658 team_adjust_ops(team);
659 netdev_rx_handler_unregister(port_dev);
660 netdev_set_master(port_dev, NULL);
661 vlan_vids_del_by_dev(port_dev, dev);
663 team_port_leave(team, port);
664 team_port_set_orig_mac(port);
665 dev_set_mtu(port_dev, port->orig.mtu);
668 netdev_info(dev, "Port device %s removed\n", portname);
669 __team_compute_features(team);
679 static const char team_no_mode_kind[] = "*NOMODE*";
681 static int team_mode_option_get(struct team *team, void *arg)
683 const char **str = arg;
685 *str = team->mode ? team->mode->kind : team_no_mode_kind;
689 static int team_mode_option_set(struct team *team, void *arg)
691 const char **str = arg;
693 return team_change_mode(team, *str);
696 static const struct team_option team_options[] = {
699 .type = TEAM_OPTION_TYPE_STRING,
700 .getter = team_mode_option_get,
701 .setter = team_mode_option_set,
705 static int team_init(struct net_device *dev)
707 struct team *team = netdev_priv(dev);
712 mutex_init(&team->lock);
714 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
715 if (!team->pcpu_stats)
718 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
719 INIT_HLIST_HEAD(&team->port_hlist[i]);
720 INIT_LIST_HEAD(&team->port_list);
722 team_adjust_ops(team);
724 INIT_LIST_HEAD(&team->option_list);
725 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
727 goto err_options_register;
728 netif_carrier_off(dev);
732 err_options_register:
733 free_percpu(team->pcpu_stats);
738 static void team_uninit(struct net_device *dev)
740 struct team *team = netdev_priv(dev);
741 struct team_port *port;
742 struct team_port *tmp;
744 mutex_lock(&team->lock);
745 list_for_each_entry_safe(port, tmp, &team->port_list, list)
746 team_port_del(team, port->dev);
748 __team_change_mode(team, NULL); /* cleanup */
749 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
750 mutex_unlock(&team->lock);
753 static void team_destructor(struct net_device *dev)
755 struct team *team = netdev_priv(dev);
757 free_percpu(team->pcpu_stats);
761 static int team_open(struct net_device *dev)
763 netif_carrier_on(dev);
767 static int team_close(struct net_device *dev)
769 netif_carrier_off(dev);
774 * note: already called with rcu_read_lock
776 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
778 struct team *team = netdev_priv(dev);
779 bool tx_success = false;
780 unsigned int len = skb->len;
782 tx_success = team->ops.transmit(team, skb);
784 struct team_pcpu_stats *pcpu_stats;
786 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
787 u64_stats_update_begin(&pcpu_stats->syncp);
788 pcpu_stats->tx_packets++;
789 pcpu_stats->tx_bytes += len;
790 u64_stats_update_end(&pcpu_stats->syncp);
792 this_cpu_inc(team->pcpu_stats->tx_dropped);
798 static void team_change_rx_flags(struct net_device *dev, int change)
800 struct team *team = netdev_priv(dev);
801 struct team_port *port;
805 list_for_each_entry_rcu(port, &team->port_list, list) {
806 if (change & IFF_PROMISC) {
807 inc = dev->flags & IFF_PROMISC ? 1 : -1;
808 dev_set_promiscuity(port->dev, inc);
810 if (change & IFF_ALLMULTI) {
811 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
812 dev_set_allmulti(port->dev, inc);
818 static void team_set_rx_mode(struct net_device *dev)
820 struct team *team = netdev_priv(dev);
821 struct team_port *port;
824 list_for_each_entry_rcu(port, &team->port_list, list) {
825 dev_uc_sync(port->dev, dev);
826 dev_mc_sync(port->dev, dev);
831 static int team_set_mac_address(struct net_device *dev, void *p)
833 struct team *team = netdev_priv(dev);
834 struct team_port *port;
835 struct sockaddr *addr = p;
837 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
839 list_for_each_entry_rcu(port, &team->port_list, list)
840 if (team->ops.port_change_mac)
841 team->ops.port_change_mac(team, port);
846 static int team_change_mtu(struct net_device *dev, int new_mtu)
848 struct team *team = netdev_priv(dev);
849 struct team_port *port;
853 * Alhough this is reader, it's guarded by team lock. It's not possible
854 * to traverse list in reverse under rcu_read_lock
856 mutex_lock(&team->lock);
857 list_for_each_entry(port, &team->port_list, list) {
858 err = dev_set_mtu(port->dev, new_mtu);
860 netdev_err(dev, "Device %s failed to change mtu",
865 mutex_unlock(&team->lock);
872 list_for_each_entry_continue_reverse(port, &team->port_list, list)
873 dev_set_mtu(port->dev, dev->mtu);
874 mutex_unlock(&team->lock);
879 static struct rtnl_link_stats64 *
880 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
882 struct team *team = netdev_priv(dev);
883 struct team_pcpu_stats *p;
884 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
885 u32 rx_dropped = 0, tx_dropped = 0;
889 for_each_possible_cpu(i) {
890 p = per_cpu_ptr(team->pcpu_stats, i);
892 start = u64_stats_fetch_begin_bh(&p->syncp);
893 rx_packets = p->rx_packets;
894 rx_bytes = p->rx_bytes;
895 rx_multicast = p->rx_multicast;
896 tx_packets = p->tx_packets;
897 tx_bytes = p->tx_bytes;
898 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
900 stats->rx_packets += rx_packets;
901 stats->rx_bytes += rx_bytes;
902 stats->multicast += rx_multicast;
903 stats->tx_packets += tx_packets;
904 stats->tx_bytes += tx_bytes;
906 * rx_dropped & tx_dropped are u32, updated
907 * without syncp protection.
909 rx_dropped += p->rx_dropped;
910 tx_dropped += p->tx_dropped;
912 stats->rx_dropped = rx_dropped;
913 stats->tx_dropped = tx_dropped;
917 static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
919 struct team *team = netdev_priv(dev);
920 struct team_port *port;
924 * Alhough this is reader, it's guarded by team lock. It's not possible
925 * to traverse list in reverse under rcu_read_lock
927 mutex_lock(&team->lock);
928 list_for_each_entry(port, &team->port_list, list) {
929 err = vlan_vid_add(port->dev, vid);
933 mutex_unlock(&team->lock);
938 list_for_each_entry_continue_reverse(port, &team->port_list, list)
939 vlan_vid_del(port->dev, vid);
940 mutex_unlock(&team->lock);
945 static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
947 struct team *team = netdev_priv(dev);
948 struct team_port *port;
951 list_for_each_entry_rcu(port, &team->port_list, list)
952 vlan_vid_del(port->dev, vid);
958 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
960 struct team *team = netdev_priv(dev);
963 mutex_lock(&team->lock);
964 err = team_port_add(team, port_dev);
965 mutex_unlock(&team->lock);
969 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
971 struct team *team = netdev_priv(dev);
974 mutex_lock(&team->lock);
975 err = team_port_del(team, port_dev);
976 mutex_unlock(&team->lock);
980 static netdev_features_t team_fix_features(struct net_device *dev,
981 netdev_features_t features)
983 struct team_port *port;
984 struct team *team = netdev_priv(dev);
985 netdev_features_t mask;
988 features &= ~NETIF_F_ONE_FOR_ALL;
989 features |= NETIF_F_ALL_FOR_ALL;
992 list_for_each_entry_rcu(port, &team->port_list, list) {
993 features = netdev_increment_features(features,
1001 static const struct net_device_ops team_netdev_ops = {
1002 .ndo_init = team_init,
1003 .ndo_uninit = team_uninit,
1004 .ndo_open = team_open,
1005 .ndo_stop = team_close,
1006 .ndo_start_xmit = team_xmit,
1007 .ndo_change_rx_flags = team_change_rx_flags,
1008 .ndo_set_rx_mode = team_set_rx_mode,
1009 .ndo_set_mac_address = team_set_mac_address,
1010 .ndo_change_mtu = team_change_mtu,
1011 .ndo_get_stats64 = team_get_stats64,
1012 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
1013 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
1014 .ndo_add_slave = team_add_slave,
1015 .ndo_del_slave = team_del_slave,
1016 .ndo_fix_features = team_fix_features,
1020 /***********************
1021 * rt netlink interface
1022 ***********************/
1024 static void team_setup(struct net_device *dev)
1028 dev->netdev_ops = &team_netdev_ops;
1029 dev->destructor = team_destructor;
1030 dev->tx_queue_len = 0;
1031 dev->flags |= IFF_MULTICAST;
1032 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1035 * Indicate we support unicast address filtering. That way core won't
1036 * bring us to promisc mode in case a unicast addr is added.
1037 * Let this up to underlay drivers.
1039 dev->priv_flags |= IFF_UNICAST_FLT;
1041 dev->features |= NETIF_F_LLTX;
1042 dev->features |= NETIF_F_GRO;
1043 dev->hw_features = NETIF_F_HW_VLAN_TX |
1044 NETIF_F_HW_VLAN_RX |
1045 NETIF_F_HW_VLAN_FILTER;
1047 dev->features |= dev->hw_features;
1050 static int team_newlink(struct net *src_net, struct net_device *dev,
1051 struct nlattr *tb[], struct nlattr *data[])
1055 if (tb[IFLA_ADDRESS] == NULL)
1056 random_ether_addr(dev->dev_addr);
1058 err = register_netdevice(dev);
1065 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1067 if (tb[IFLA_ADDRESS]) {
1068 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1070 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1071 return -EADDRNOTAVAIL;
1076 static struct rtnl_link_ops team_link_ops __read_mostly = {
1078 .priv_size = sizeof(struct team),
1079 .setup = team_setup,
1080 .newlink = team_newlink,
1081 .validate = team_validate,
1085 /***********************************
1086 * Generic netlink custom interface
1087 ***********************************/
1089 static struct genl_family team_nl_family = {
1090 .id = GENL_ID_GENERATE,
1091 .name = TEAM_GENL_NAME,
1092 .version = TEAM_GENL_VERSION,
1093 .maxattr = TEAM_ATTR_MAX,
1097 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1098 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1099 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1100 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1101 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1104 static const struct nla_policy
1105 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1106 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1107 [TEAM_ATTR_OPTION_NAME] = {
1109 .len = TEAM_STRING_MAX_LEN,
1111 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1112 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
1113 [TEAM_ATTR_OPTION_DATA] = {
1115 .len = TEAM_STRING_MAX_LEN,
1119 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1121 struct sk_buff *msg;
1125 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1129 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1130 &team_nl_family, 0, TEAM_CMD_NOOP);
1136 genlmsg_end(msg, hdr);
1138 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1147 * Netlink cmd functions should be locked by following two functions.
1148 * Since dev gets held here, that ensures dev won't disappear in between.
1150 static struct team *team_nl_team_get(struct genl_info *info)
1152 struct net *net = genl_info_net(info);
1154 struct net_device *dev;
1157 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1160 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
1161 dev = dev_get_by_index(net, ifindex);
1162 if (!dev || dev->netdev_ops != &team_netdev_ops) {
1168 team = netdev_priv(dev);
1169 mutex_lock(&team->lock);
1173 static void team_nl_team_put(struct team *team)
1175 mutex_unlock(&team->lock);
1179 static int team_nl_send_generic(struct genl_info *info, struct team *team,
1180 int (*fill_func)(struct sk_buff *skb,
1181 struct genl_info *info,
1182 int flags, struct team *team))
1184 struct sk_buff *skb;
1187 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1191 err = fill_func(skb, info, NLM_F_ACK, team);
1195 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1203 static int team_nl_fill_options_get_changed(struct sk_buff *skb,
1204 u32 pid, u32 seq, int flags,
1206 struct team_option *changed_option)
1208 struct nlattr *option_list;
1210 struct team_option *option;
1212 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1213 TEAM_CMD_OPTIONS_GET);
1215 return PTR_ERR(hdr);
1217 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
1218 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1222 list_for_each_entry(option, &team->option_list, list) {
1223 struct nlattr *option_item;
1226 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1228 goto nla_put_failure;
1229 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name);
1230 if (option == changed_option)
1231 NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED);
1232 switch (option->type) {
1233 case TEAM_OPTION_TYPE_U32:
1234 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32);
1235 team_option_get(team, option, &arg);
1236 NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg);
1238 case TEAM_OPTION_TYPE_STRING:
1239 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING);
1240 team_option_get(team, option, &arg);
1241 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA,
1247 nla_nest_end(skb, option_item);
1250 nla_nest_end(skb, option_list);
1251 return genlmsg_end(skb, hdr);
1254 genlmsg_cancel(skb, hdr);
1258 static int team_nl_fill_options_get(struct sk_buff *skb,
1259 struct genl_info *info, int flags,
1262 return team_nl_fill_options_get_changed(skb, info->snd_pid,
1263 info->snd_seq, NLM_F_ACK,
1267 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1272 team = team_nl_team_get(info);
1276 err = team_nl_send_generic(info, team, team_nl_fill_options_get);
1278 team_nl_team_put(team);
1283 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1288 struct nlattr *nl_option;
1290 team = team_nl_team_get(info);
1295 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1300 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
1301 struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1];
1302 enum team_option_type opt_type;
1303 struct team_option *option;
1305 bool opt_found = false;
1307 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1311 err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX,
1312 nl_option, team_nl_option_policy);
1315 if (!mode_attrs[TEAM_ATTR_OPTION_NAME] ||
1316 !mode_attrs[TEAM_ATTR_OPTION_TYPE] ||
1317 !mode_attrs[TEAM_ATTR_OPTION_DATA]) {
1321 switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) {
1323 opt_type = TEAM_OPTION_TYPE_U32;
1326 opt_type = TEAM_OPTION_TYPE_STRING;
1332 opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]);
1333 list_for_each_entry(option, &team->option_list, list) {
1335 struct nlattr *opt_data_attr;
1337 if (option->type != opt_type ||
1338 strcmp(option->name, opt_name))
1341 opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA];
1343 case TEAM_OPTION_TYPE_U32:
1344 arg = nla_get_u32(opt_data_attr);
1346 case TEAM_OPTION_TYPE_STRING:
1347 arg = (long) nla_data(opt_data_attr);
1352 err = team_option_set(team, option, &arg);
1363 team_nl_team_put(team);
1368 static int team_nl_fill_port_list_get_changed(struct sk_buff *skb,
1369 u32 pid, u32 seq, int flags,
1371 struct team_port *changed_port)
1373 struct nlattr *port_list;
1375 struct team_port *port;
1377 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1378 TEAM_CMD_PORT_LIST_GET);
1380 return PTR_ERR(hdr);
1382 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
1383 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1387 list_for_each_entry(port, &team->port_list, list) {
1388 struct nlattr *port_item;
1390 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1392 goto nla_put_failure;
1393 NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex);
1394 if (port == changed_port)
1395 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED);
1397 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP);
1398 NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed);
1399 NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex);
1400 nla_nest_end(skb, port_item);
1403 nla_nest_end(skb, port_list);
1404 return genlmsg_end(skb, hdr);
1407 genlmsg_cancel(skb, hdr);
1411 static int team_nl_fill_port_list_get(struct sk_buff *skb,
1412 struct genl_info *info, int flags,
1415 return team_nl_fill_port_list_get_changed(skb, info->snd_pid,
1416 info->snd_seq, NLM_F_ACK,
1420 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1421 struct genl_info *info)
1426 team = team_nl_team_get(info);
1430 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get);
1432 team_nl_team_put(team);
1437 static struct genl_ops team_nl_ops[] = {
1439 .cmd = TEAM_CMD_NOOP,
1440 .doit = team_nl_cmd_noop,
1441 .policy = team_nl_policy,
1444 .cmd = TEAM_CMD_OPTIONS_SET,
1445 .doit = team_nl_cmd_options_set,
1446 .policy = team_nl_policy,
1447 .flags = GENL_ADMIN_PERM,
1450 .cmd = TEAM_CMD_OPTIONS_GET,
1451 .doit = team_nl_cmd_options_get,
1452 .policy = team_nl_policy,
1453 .flags = GENL_ADMIN_PERM,
1456 .cmd = TEAM_CMD_PORT_LIST_GET,
1457 .doit = team_nl_cmd_port_list_get,
1458 .policy = team_nl_policy,
1459 .flags = GENL_ADMIN_PERM,
1463 static struct genl_multicast_group team_change_event_mcgrp = {
1464 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1467 static int team_nl_send_event_options_get(struct team *team,
1468 struct team_option *changed_option)
1470 struct sk_buff *skb;
1472 struct net *net = dev_net(team->dev);
1474 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1478 err = team_nl_fill_options_get_changed(skb, 0, 0, 0, team,
1483 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1492 static int team_nl_send_event_port_list_get(struct team_port *port)
1494 struct sk_buff *skb;
1496 struct net *net = dev_net(port->team->dev);
1498 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1502 err = team_nl_fill_port_list_get_changed(skb, 0, 0, 0,
1507 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1516 static int team_nl_init(void)
1520 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1521 ARRAY_SIZE(team_nl_ops));
1525 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1527 goto err_change_event_grp_reg;
1531 err_change_event_grp_reg:
1532 genl_unregister_family(&team_nl_family);
1537 static void team_nl_fini(void)
1539 genl_unregister_family(&team_nl_family);
1547 static void __team_options_change_check(struct team *team,
1548 struct team_option *changed_option)
1552 err = team_nl_send_event_options_get(team, changed_option);
1554 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1557 /* rtnl lock is held */
1558 static void __team_port_change_check(struct team_port *port, bool linkup)
1562 if (port->linkup == linkup)
1565 port->linkup = linkup;
1567 struct ethtool_cmd ecmd;
1569 err = __ethtool_get_settings(port->dev, &ecmd);
1571 port->speed = ethtool_cmd_speed(&ecmd);
1572 port->duplex = ecmd.duplex;
1580 err = team_nl_send_event_port_list_get(port);
1582 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
1587 static void team_port_change_check(struct team_port *port, bool linkup)
1589 struct team *team = port->team;
1591 mutex_lock(&team->lock);
1592 __team_port_change_check(port, linkup);
1593 mutex_unlock(&team->lock);
1596 /************************************
1597 * Net device notifier event handler
1598 ************************************/
1600 static int team_device_event(struct notifier_block *unused,
1601 unsigned long event, void *ptr)
1603 struct net_device *dev = (struct net_device *) ptr;
1604 struct team_port *port;
1606 port = team_port_get_rtnl(dev);
1612 if (netif_carrier_ok(dev))
1613 team_port_change_check(port, true);
1615 team_port_change_check(port, false);
1617 if (netif_running(port->dev))
1618 team_port_change_check(port,
1619 !!netif_carrier_ok(port->dev));
1621 case NETDEV_UNREGISTER:
1622 team_del_slave(port->team->dev, dev);
1624 case NETDEV_FEAT_CHANGE:
1625 team_compute_features(port->team);
1627 case NETDEV_CHANGEMTU:
1628 /* Forbid to change mtu of underlaying device */
1630 case NETDEV_PRE_TYPE_CHANGE:
1631 /* Forbid to change type of underlaying device */
1637 static struct notifier_block team_notifier_block __read_mostly = {
1638 .notifier_call = team_device_event,
1642 /***********************
1643 * Module init and exit
1644 ***********************/
1646 static int __init team_module_init(void)
1650 register_netdevice_notifier(&team_notifier_block);
1652 err = rtnl_link_register(&team_link_ops);
1656 err = team_nl_init();
1663 rtnl_link_unregister(&team_link_ops);
1666 unregister_netdevice_notifier(&team_notifier_block);
1671 static void __exit team_module_exit(void)
1674 rtnl_link_unregister(&team_link_ops);
1675 unregister_netdevice_notifier(&team_notifier_block);
1678 module_init(team_module_init);
1679 module_exit(team_module_exit);
1681 MODULE_LICENSE("GPL v2");
1682 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
1683 MODULE_DESCRIPTION("Ethernet team device driver");
1684 MODULE_ALIAS_RTNL_LINK(DRV_NAME);