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_arp.h>
22 #include <linux/socket.h>
23 #include <linux/etherdevice.h>
24 #include <linux/rtnetlink.h>
25 #include <net/rtnetlink.h>
26 #include <net/genetlink.h>
27 #include <net/netlink.h>
28 #include <linux/if_team.h>
30 #define DRV_NAME "team"
37 #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
39 static struct team_port *team_port_get_rcu(const struct net_device *dev)
41 struct team_port *port = rcu_dereference(dev->rx_handler_data);
43 return team_port_exists(dev) ? port : NULL;
46 static struct team_port *team_port_get_rtnl(const struct net_device *dev)
48 struct team_port *port = rtnl_dereference(dev->rx_handler_data);
50 return team_port_exists(dev) ? port : NULL;
54 * Since the ability to change mac address for open port device is tested in
55 * team_port_add, this function can be called without control of return value
57 static int __set_port_mac(struct net_device *port_dev,
58 const unsigned char *dev_addr)
62 memcpy(addr.sa_data, dev_addr, ETH_ALEN);
63 addr.sa_family = ARPHRD_ETHER;
64 return dev_set_mac_address(port_dev, &addr);
67 int team_port_set_orig_mac(struct team_port *port)
69 return __set_port_mac(port->dev, port->orig.dev_addr);
72 int team_port_set_team_mac(struct team_port *port)
74 return __set_port_mac(port->dev, port->team->dev->dev_addr);
76 EXPORT_SYMBOL(team_port_set_team_mac);
83 struct team_option *__team_find_option(struct team *team, const char *opt_name)
85 struct team_option *option;
87 list_for_each_entry(option, &team->option_list, list) {
88 if (strcmp(option->name, opt_name) == 0)
94 int team_options_register(struct team *team,
95 const struct team_option *option,
99 struct team_option *dst_opts[option_count];
102 memset(dst_opts, 0, sizeof(dst_opts));
103 for (i = 0; i < option_count; i++, option++) {
104 struct team_option *dst_opt;
106 if (__team_find_option(team, option->name)) {
110 dst_opt = kmalloc(sizeof(*option), GFP_KERNEL);
115 memcpy(dst_opt, option, sizeof(*option));
116 dst_opts[i] = dst_opt;
119 for (i = 0; i < option_count; i++)
120 list_add_tail(&dst_opts[i]->list, &team->option_list);
125 for (i = 0; i < option_count; i++)
131 EXPORT_SYMBOL(team_options_register);
133 static void __team_options_change_check(struct team *team,
134 struct team_option *changed_option);
136 static void __team_options_unregister(struct team *team,
137 const struct team_option *option,
142 for (i = 0; i < option_count; i++, option++) {
143 struct team_option *del_opt;
145 del_opt = __team_find_option(team, option->name);
147 list_del(&del_opt->list);
153 void team_options_unregister(struct team *team,
154 const struct team_option *option,
157 __team_options_unregister(team, option, option_count);
158 __team_options_change_check(team, NULL);
160 EXPORT_SYMBOL(team_options_unregister);
162 static int team_option_get(struct team *team, struct team_option *option,
165 return option->getter(team, arg);
168 static int team_option_set(struct team *team, struct team_option *option,
173 err = option->setter(team, arg);
177 __team_options_change_check(team, option);
185 static LIST_HEAD(mode_list);
186 static DEFINE_SPINLOCK(mode_list_lock);
188 static struct team_mode *__find_mode(const char *kind)
190 struct team_mode *mode;
192 list_for_each_entry(mode, &mode_list, list) {
193 if (strcmp(mode->kind, kind) == 0)
199 static bool is_good_mode_name(const char *name)
201 while (*name != '\0') {
202 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
209 int team_mode_register(struct team_mode *mode)
213 if (!is_good_mode_name(mode->kind) ||
214 mode->priv_size > TEAM_MODE_PRIV_SIZE)
216 spin_lock(&mode_list_lock);
217 if (__find_mode(mode->kind)) {
221 list_add_tail(&mode->list, &mode_list);
223 spin_unlock(&mode_list_lock);
226 EXPORT_SYMBOL(team_mode_register);
228 int team_mode_unregister(struct team_mode *mode)
230 spin_lock(&mode_list_lock);
231 list_del_init(&mode->list);
232 spin_unlock(&mode_list_lock);
235 EXPORT_SYMBOL(team_mode_unregister);
237 static struct team_mode *team_mode_get(const char *kind)
239 struct team_mode *mode;
241 spin_lock(&mode_list_lock);
242 mode = __find_mode(kind);
244 spin_unlock(&mode_list_lock);
245 request_module("team-mode-%s", kind);
246 spin_lock(&mode_list_lock);
247 mode = __find_mode(kind);
250 if (!try_module_get(mode->owner))
253 spin_unlock(&mode_list_lock);
257 static void team_mode_put(const struct team_mode *mode)
259 module_put(mode->owner);
262 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
264 dev_kfree_skb_any(skb);
268 rx_handler_result_t team_dummy_receive(struct team *team,
269 struct team_port *port,
272 return RX_HANDLER_ANOTHER;
275 static void team_adjust_ops(struct team *team)
278 * To avoid checks in rx/tx skb paths, ensure here that non-null and
279 * correct ops are always set.
282 if (list_empty(&team->port_list) ||
283 !team->mode || !team->mode->ops->transmit)
284 team->ops.transmit = team_dummy_transmit;
286 team->ops.transmit = team->mode->ops->transmit;
288 if (list_empty(&team->port_list) ||
289 !team->mode || !team->mode->ops->receive)
290 team->ops.receive = team_dummy_receive;
292 team->ops.receive = team->mode->ops->receive;
296 * We can benefit from the fact that it's ensured no port is present
297 * at the time of mode change. Therefore no packets are in fly so there's no
298 * need to set mode operations in any special way.
300 static int __team_change_mode(struct team *team,
301 const struct team_mode *new_mode)
303 /* Check if mode was previously set and do cleanup if so */
305 void (*exit_op)(struct team *team) = team->ops.exit;
307 /* Clear ops area so no callback is called any longer */
308 memset(&team->ops, 0, sizeof(struct team_mode_ops));
309 team_adjust_ops(team);
313 team_mode_put(team->mode);
315 /* zero private data area */
316 memset(&team->mode_priv, 0,
317 sizeof(struct team) - offsetof(struct team, mode_priv));
323 if (new_mode->ops->init) {
326 err = new_mode->ops->init(team);
331 team->mode = new_mode;
332 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
333 team_adjust_ops(team);
338 static int team_change_mode(struct team *team, const char *kind)
340 struct team_mode *new_mode;
341 struct net_device *dev = team->dev;
344 if (!list_empty(&team->port_list)) {
345 netdev_err(dev, "No ports can be present during mode change\n");
349 if (team->mode && strcmp(team->mode->kind, kind) == 0) {
350 netdev_err(dev, "Unable to change to the same mode the team is in\n");
354 new_mode = team_mode_get(kind);
356 netdev_err(dev, "Mode \"%s\" not found\n", kind);
360 err = __team_change_mode(team, new_mode);
362 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
363 team_mode_put(new_mode);
367 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
372 /************************
373 * Rx path frame handler
374 ************************/
376 /* note: already called with rcu_read_lock */
377 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
379 struct sk_buff *skb = *pskb;
380 struct team_port *port;
382 rx_handler_result_t res;
384 skb = skb_share_check(skb, GFP_ATOMIC);
386 return RX_HANDLER_CONSUMED;
390 port = team_port_get_rcu(skb->dev);
393 res = team->ops.receive(team, port, skb);
394 if (res == RX_HANDLER_ANOTHER) {
395 struct team_pcpu_stats *pcpu_stats;
397 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
398 u64_stats_update_begin(&pcpu_stats->syncp);
399 pcpu_stats->rx_packets++;
400 pcpu_stats->rx_bytes += skb->len;
401 if (skb->pkt_type == PACKET_MULTICAST)
402 pcpu_stats->rx_multicast++;
403 u64_stats_update_end(&pcpu_stats->syncp);
405 skb->dev = team->dev;
407 this_cpu_inc(team->pcpu_stats->rx_dropped);
418 static bool team_port_find(const struct team *team,
419 const struct team_port *port)
421 struct team_port *cur;
423 list_for_each_entry(cur, &team->port_list, list)
430 * Add/delete port to the team port list. Write guarded by rtnl_lock.
431 * Takes care of correct port->index setup (might be racy).
433 static void team_port_list_add_port(struct team *team,
434 struct team_port *port)
436 port->index = team->port_count++;
437 hlist_add_head_rcu(&port->hlist,
438 team_port_index_hash(team, port->index));
439 list_add_tail_rcu(&port->list, &team->port_list);
442 static void __reconstruct_port_hlist(struct team *team, int rm_index)
445 struct team_port *port;
447 for (i = rm_index + 1; i < team->port_count; i++) {
448 port = team_get_port_by_index(team, i);
449 hlist_del_rcu(&port->hlist);
451 hlist_add_head_rcu(&port->hlist,
452 team_port_index_hash(team, port->index));
456 static void team_port_list_del_port(struct team *team,
457 struct team_port *port)
459 int rm_index = port->index;
461 hlist_del_rcu(&port->hlist);
462 list_del_rcu(&port->list);
463 __reconstruct_port_hlist(team, rm_index);
467 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
468 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
469 NETIF_F_HIGHDMA | NETIF_F_LRO)
471 static void __team_compute_features(struct team *team)
473 struct team_port *port;
474 u32 vlan_features = TEAM_VLAN_FEATURES;
475 unsigned short max_hard_header_len = ETH_HLEN;
477 list_for_each_entry(port, &team->port_list, list) {
478 vlan_features = netdev_increment_features(vlan_features,
479 port->dev->vlan_features,
482 if (port->dev->hard_header_len > max_hard_header_len)
483 max_hard_header_len = port->dev->hard_header_len;
486 team->dev->vlan_features = vlan_features;
487 team->dev->hard_header_len = max_hard_header_len;
489 netdev_change_features(team->dev);
492 static void team_compute_features(struct team *team)
494 mutex_lock(&team->lock);
495 __team_compute_features(team);
496 mutex_unlock(&team->lock);
499 static int team_port_enter(struct team *team, struct team_port *port)
504 port->dev->priv_flags |= IFF_TEAM_PORT;
505 if (team->ops.port_enter) {
506 err = team->ops.port_enter(team, port);
508 netdev_err(team->dev, "Device %s failed to enter team mode\n",
517 port->dev->priv_flags &= ~IFF_TEAM_PORT;
523 static void team_port_leave(struct team *team, struct team_port *port)
525 if (team->ops.port_leave)
526 team->ops.port_leave(team, port);
527 port->dev->priv_flags &= ~IFF_TEAM_PORT;
531 static void __team_port_change_check(struct team_port *port, bool linkup);
533 static int team_port_add(struct team *team, struct net_device *port_dev)
535 struct net_device *dev = team->dev;
536 struct team_port *port;
537 char *portname = port_dev->name;
540 if (port_dev->flags & IFF_LOOPBACK ||
541 port_dev->type != ARPHRD_ETHER) {
542 netdev_err(dev, "Device %s is of an unsupported type\n",
547 if (team_port_exists(port_dev)) {
548 netdev_err(dev, "Device %s is already a port "
549 "of a team device\n", portname);
553 if (port_dev->flags & IFF_UP) {
554 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
559 port = kzalloc(sizeof(struct team_port), GFP_KERNEL);
563 port->dev = port_dev;
566 port->orig.mtu = port_dev->mtu;
567 err = dev_set_mtu(port_dev, dev->mtu);
569 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
573 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
575 err = team_port_enter(team, port);
577 netdev_err(dev, "Device %s failed to enter team mode\n",
582 err = dev_open(port_dev);
584 netdev_dbg(dev, "Device %s opening failed\n",
589 err = netdev_set_master(port_dev, dev);
591 netdev_err(dev, "Device %s failed to set master\n", portname);
595 err = netdev_rx_handler_register(port_dev, team_handle_frame,
598 netdev_err(dev, "Device %s failed to register rx_handler\n",
600 goto err_handler_register;
603 team_port_list_add_port(team, port);
604 team_adjust_ops(team);
605 __team_compute_features(team);
606 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
608 netdev_info(dev, "Port device %s added\n", portname);
612 err_handler_register:
613 netdev_set_master(port_dev, NULL);
619 team_port_leave(team, port);
620 team_port_set_orig_mac(port);
623 dev_set_mtu(port_dev, port->orig.mtu);
631 static int team_port_del(struct team *team, struct net_device *port_dev)
633 struct net_device *dev = team->dev;
634 struct team_port *port;
635 char *portname = port_dev->name;
637 port = team_port_get_rtnl(port_dev);
638 if (!port || !team_port_find(team, port)) {
639 netdev_err(dev, "Device %s does not act as a port of this team\n",
644 __team_port_change_check(port, false);
645 team_port_list_del_port(team, port);
646 team_adjust_ops(team);
647 netdev_rx_handler_unregister(port_dev);
648 netdev_set_master(port_dev, NULL);
650 team_port_leave(team, port);
651 team_port_set_orig_mac(port);
652 dev_set_mtu(port_dev, port->orig.mtu);
655 netdev_info(dev, "Port device %s removed\n", portname);
656 __team_compute_features(team);
666 static const char team_no_mode_kind[] = "*NOMODE*";
668 static int team_mode_option_get(struct team *team, void *arg)
670 const char **str = arg;
672 *str = team->mode ? team->mode->kind : team_no_mode_kind;
676 static int team_mode_option_set(struct team *team, void *arg)
678 const char **str = arg;
680 return team_change_mode(team, *str);
683 static const struct team_option team_options[] = {
686 .type = TEAM_OPTION_TYPE_STRING,
687 .getter = team_mode_option_get,
688 .setter = team_mode_option_set,
692 static int team_init(struct net_device *dev)
694 struct team *team = netdev_priv(dev);
699 mutex_init(&team->lock);
701 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
702 if (!team->pcpu_stats)
705 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
706 INIT_HLIST_HEAD(&team->port_hlist[i]);
707 INIT_LIST_HEAD(&team->port_list);
709 team_adjust_ops(team);
711 INIT_LIST_HEAD(&team->option_list);
712 err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
714 goto err_options_register;
715 netif_carrier_off(dev);
719 err_options_register:
720 free_percpu(team->pcpu_stats);
725 static void team_uninit(struct net_device *dev)
727 struct team *team = netdev_priv(dev);
728 struct team_port *port;
729 struct team_port *tmp;
731 mutex_lock(&team->lock);
732 list_for_each_entry_safe(port, tmp, &team->port_list, list)
733 team_port_del(team, port->dev);
735 __team_change_mode(team, NULL); /* cleanup */
736 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
737 mutex_unlock(&team->lock);
740 static void team_destructor(struct net_device *dev)
742 struct team *team = netdev_priv(dev);
744 free_percpu(team->pcpu_stats);
748 static int team_open(struct net_device *dev)
750 netif_carrier_on(dev);
754 static int team_close(struct net_device *dev)
756 netif_carrier_off(dev);
761 * note: already called with rcu_read_lock
763 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
765 struct team *team = netdev_priv(dev);
766 bool tx_success = false;
767 unsigned int len = skb->len;
769 tx_success = team->ops.transmit(team, skb);
771 struct team_pcpu_stats *pcpu_stats;
773 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
774 u64_stats_update_begin(&pcpu_stats->syncp);
775 pcpu_stats->tx_packets++;
776 pcpu_stats->tx_bytes += len;
777 u64_stats_update_end(&pcpu_stats->syncp);
779 this_cpu_inc(team->pcpu_stats->tx_dropped);
785 static void team_change_rx_flags(struct net_device *dev, int change)
787 struct team *team = netdev_priv(dev);
788 struct team_port *port;
792 list_for_each_entry_rcu(port, &team->port_list, list) {
793 if (change & IFF_PROMISC) {
794 inc = dev->flags & IFF_PROMISC ? 1 : -1;
795 dev_set_promiscuity(port->dev, inc);
797 if (change & IFF_ALLMULTI) {
798 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
799 dev_set_allmulti(port->dev, inc);
805 static void team_set_rx_mode(struct net_device *dev)
807 struct team *team = netdev_priv(dev);
808 struct team_port *port;
811 list_for_each_entry_rcu(port, &team->port_list, list) {
812 dev_uc_sync(port->dev, dev);
813 dev_mc_sync(port->dev, dev);
818 static int team_set_mac_address(struct net_device *dev, void *p)
820 struct team *team = netdev_priv(dev);
821 struct team_port *port;
822 struct sockaddr *addr = p;
824 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
826 list_for_each_entry_rcu(port, &team->port_list, list)
827 if (team->ops.port_change_mac)
828 team->ops.port_change_mac(team, port);
833 static int team_change_mtu(struct net_device *dev, int new_mtu)
835 struct team *team = netdev_priv(dev);
836 struct team_port *port;
840 * Alhough this is reader, it's guarded by team lock. It's not possible
841 * to traverse list in reverse under rcu_read_lock
843 mutex_lock(&team->lock);
844 list_for_each_entry(port, &team->port_list, list) {
845 err = dev_set_mtu(port->dev, new_mtu);
847 netdev_err(dev, "Device %s failed to change mtu",
852 mutex_unlock(&team->lock);
859 list_for_each_entry_continue_reverse(port, &team->port_list, list)
860 dev_set_mtu(port->dev, dev->mtu);
861 mutex_unlock(&team->lock);
866 static struct rtnl_link_stats64 *
867 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
869 struct team *team = netdev_priv(dev);
870 struct team_pcpu_stats *p;
871 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
872 u32 rx_dropped = 0, tx_dropped = 0;
876 for_each_possible_cpu(i) {
877 p = per_cpu_ptr(team->pcpu_stats, i);
879 start = u64_stats_fetch_begin_bh(&p->syncp);
880 rx_packets = p->rx_packets;
881 rx_bytes = p->rx_bytes;
882 rx_multicast = p->rx_multicast;
883 tx_packets = p->tx_packets;
884 tx_bytes = p->tx_bytes;
885 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
887 stats->rx_packets += rx_packets;
888 stats->rx_bytes += rx_bytes;
889 stats->multicast += rx_multicast;
890 stats->tx_packets += tx_packets;
891 stats->tx_bytes += tx_bytes;
893 * rx_dropped & tx_dropped are u32, updated
894 * without syncp protection.
896 rx_dropped += p->rx_dropped;
897 tx_dropped += p->tx_dropped;
899 stats->rx_dropped = rx_dropped;
900 stats->tx_dropped = tx_dropped;
904 static void team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
906 struct team *team = netdev_priv(dev);
907 struct team_port *port;
910 list_for_each_entry_rcu(port, &team->port_list, list) {
911 const struct net_device_ops *ops = port->dev->netdev_ops;
913 if (ops->ndo_vlan_rx_add_vid)
914 ops->ndo_vlan_rx_add_vid(port->dev, vid);
919 static void team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
921 struct team *team = netdev_priv(dev);
922 struct team_port *port;
925 list_for_each_entry_rcu(port, &team->port_list, list) {
926 const struct net_device_ops *ops = port->dev->netdev_ops;
928 if (ops->ndo_vlan_rx_kill_vid)
929 ops->ndo_vlan_rx_kill_vid(port->dev, vid);
934 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
936 struct team *team = netdev_priv(dev);
939 mutex_lock(&team->lock);
940 err = team_port_add(team, port_dev);
941 mutex_unlock(&team->lock);
945 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
947 struct team *team = netdev_priv(dev);
950 mutex_lock(&team->lock);
951 err = team_port_del(team, port_dev);
952 mutex_unlock(&team->lock);
956 static netdev_features_t team_fix_features(struct net_device *dev,
957 netdev_features_t features)
959 struct team_port *port;
960 struct team *team = netdev_priv(dev);
961 netdev_features_t mask;
964 features &= ~NETIF_F_ONE_FOR_ALL;
965 features |= NETIF_F_ALL_FOR_ALL;
968 list_for_each_entry_rcu(port, &team->port_list, list) {
969 features = netdev_increment_features(features,
977 static const struct net_device_ops team_netdev_ops = {
978 .ndo_init = team_init,
979 .ndo_uninit = team_uninit,
980 .ndo_open = team_open,
981 .ndo_stop = team_close,
982 .ndo_start_xmit = team_xmit,
983 .ndo_change_rx_flags = team_change_rx_flags,
984 .ndo_set_rx_mode = team_set_rx_mode,
985 .ndo_set_mac_address = team_set_mac_address,
986 .ndo_change_mtu = team_change_mtu,
987 .ndo_get_stats64 = team_get_stats64,
988 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
989 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
990 .ndo_add_slave = team_add_slave,
991 .ndo_del_slave = team_del_slave,
992 .ndo_fix_features = team_fix_features,
996 /***********************
997 * rt netlink interface
998 ***********************/
1000 static void team_setup(struct net_device *dev)
1004 dev->netdev_ops = &team_netdev_ops;
1005 dev->destructor = team_destructor;
1006 dev->tx_queue_len = 0;
1007 dev->flags |= IFF_MULTICAST;
1008 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
1011 * Indicate we support unicast address filtering. That way core won't
1012 * bring us to promisc mode in case a unicast addr is added.
1013 * Let this up to underlay drivers.
1015 dev->priv_flags |= IFF_UNICAST_FLT;
1017 dev->features |= NETIF_F_LLTX;
1018 dev->features |= NETIF_F_GRO;
1019 dev->hw_features = NETIF_F_HW_VLAN_TX |
1020 NETIF_F_HW_VLAN_RX |
1021 NETIF_F_HW_VLAN_FILTER;
1023 dev->features |= dev->hw_features;
1026 static int team_newlink(struct net *src_net, struct net_device *dev,
1027 struct nlattr *tb[], struct nlattr *data[])
1031 if (tb[IFLA_ADDRESS] == NULL)
1032 random_ether_addr(dev->dev_addr);
1034 err = register_netdevice(dev);
1041 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
1043 if (tb[IFLA_ADDRESS]) {
1044 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1046 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1047 return -EADDRNOTAVAIL;
1052 static struct rtnl_link_ops team_link_ops __read_mostly = {
1054 .priv_size = sizeof(struct team),
1055 .setup = team_setup,
1056 .newlink = team_newlink,
1057 .validate = team_validate,
1061 /***********************************
1062 * Generic netlink custom interface
1063 ***********************************/
1065 static struct genl_family team_nl_family = {
1066 .id = GENL_ID_GENERATE,
1067 .name = TEAM_GENL_NAME,
1068 .version = TEAM_GENL_VERSION,
1069 .maxattr = TEAM_ATTR_MAX,
1073 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
1074 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
1075 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
1076 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
1077 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1080 static const struct nla_policy
1081 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1082 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1083 [TEAM_ATTR_OPTION_NAME] = {
1085 .len = TEAM_STRING_MAX_LEN,
1087 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1088 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
1089 [TEAM_ATTR_OPTION_DATA] = {
1091 .len = TEAM_STRING_MAX_LEN,
1095 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1097 struct sk_buff *msg;
1101 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1105 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1106 &team_nl_family, 0, TEAM_CMD_NOOP);
1112 genlmsg_end(msg, hdr);
1114 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1123 * Netlink cmd functions should be locked by following two functions.
1124 * Since dev gets held here, that ensures dev won't disappear in between.
1126 static struct team *team_nl_team_get(struct genl_info *info)
1128 struct net *net = genl_info_net(info);
1130 struct net_device *dev;
1133 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1136 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
1137 dev = dev_get_by_index(net, ifindex);
1138 if (!dev || dev->netdev_ops != &team_netdev_ops) {
1144 team = netdev_priv(dev);
1145 mutex_lock(&team->lock);
1149 static void team_nl_team_put(struct team *team)
1151 mutex_unlock(&team->lock);
1155 static int team_nl_send_generic(struct genl_info *info, struct team *team,
1156 int (*fill_func)(struct sk_buff *skb,
1157 struct genl_info *info,
1158 int flags, struct team *team))
1160 struct sk_buff *skb;
1163 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1167 err = fill_func(skb, info, NLM_F_ACK, team);
1171 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1179 static int team_nl_fill_options_get_changed(struct sk_buff *skb,
1180 u32 pid, u32 seq, int flags,
1182 struct team_option *changed_option)
1184 struct nlattr *option_list;
1186 struct team_option *option;
1188 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1189 TEAM_CMD_OPTIONS_GET);
1191 return PTR_ERR(hdr);
1193 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
1194 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1198 list_for_each_entry(option, &team->option_list, list) {
1199 struct nlattr *option_item;
1202 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1204 goto nla_put_failure;
1205 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name);
1206 if (option == changed_option)
1207 NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED);
1208 switch (option->type) {
1209 case TEAM_OPTION_TYPE_U32:
1210 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32);
1211 team_option_get(team, option, &arg);
1212 NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg);
1214 case TEAM_OPTION_TYPE_STRING:
1215 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING);
1216 team_option_get(team, option, &arg);
1217 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA,
1223 nla_nest_end(skb, option_item);
1226 nla_nest_end(skb, option_list);
1227 return genlmsg_end(skb, hdr);
1230 genlmsg_cancel(skb, hdr);
1234 static int team_nl_fill_options_get(struct sk_buff *skb,
1235 struct genl_info *info, int flags,
1238 return team_nl_fill_options_get_changed(skb, info->snd_pid,
1239 info->snd_seq, NLM_F_ACK,
1243 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1248 team = team_nl_team_get(info);
1252 err = team_nl_send_generic(info, team, team_nl_fill_options_get);
1254 team_nl_team_put(team);
1259 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1264 struct nlattr *nl_option;
1266 team = team_nl_team_get(info);
1271 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1276 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
1277 struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1];
1278 enum team_option_type opt_type;
1279 struct team_option *option;
1281 bool opt_found = false;
1283 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1287 err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX,
1288 nl_option, team_nl_option_policy);
1291 if (!mode_attrs[TEAM_ATTR_OPTION_NAME] ||
1292 !mode_attrs[TEAM_ATTR_OPTION_TYPE] ||
1293 !mode_attrs[TEAM_ATTR_OPTION_DATA]) {
1297 switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) {
1299 opt_type = TEAM_OPTION_TYPE_U32;
1302 opt_type = TEAM_OPTION_TYPE_STRING;
1308 opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]);
1309 list_for_each_entry(option, &team->option_list, list) {
1311 struct nlattr *opt_data_attr;
1313 if (option->type != opt_type ||
1314 strcmp(option->name, opt_name))
1317 opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA];
1319 case TEAM_OPTION_TYPE_U32:
1320 arg = nla_get_u32(opt_data_attr);
1322 case TEAM_OPTION_TYPE_STRING:
1323 arg = (long) nla_data(opt_data_attr);
1328 err = team_option_set(team, option, &arg);
1339 team_nl_team_put(team);
1344 static int team_nl_fill_port_list_get_changed(struct sk_buff *skb,
1345 u32 pid, u32 seq, int flags,
1347 struct team_port *changed_port)
1349 struct nlattr *port_list;
1351 struct team_port *port;
1353 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1354 TEAM_CMD_PORT_LIST_GET);
1356 return PTR_ERR(hdr);
1358 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
1359 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1363 list_for_each_entry(port, &team->port_list, list) {
1364 struct nlattr *port_item;
1366 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1368 goto nla_put_failure;
1369 NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex);
1370 if (port == changed_port)
1371 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED);
1373 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP);
1374 NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed);
1375 NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex);
1376 nla_nest_end(skb, port_item);
1379 nla_nest_end(skb, port_list);
1380 return genlmsg_end(skb, hdr);
1383 genlmsg_cancel(skb, hdr);
1387 static int team_nl_fill_port_list_get(struct sk_buff *skb,
1388 struct genl_info *info, int flags,
1391 return team_nl_fill_port_list_get_changed(skb, info->snd_pid,
1392 info->snd_seq, NLM_F_ACK,
1396 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1397 struct genl_info *info)
1402 team = team_nl_team_get(info);
1406 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get);
1408 team_nl_team_put(team);
1413 static struct genl_ops team_nl_ops[] = {
1415 .cmd = TEAM_CMD_NOOP,
1416 .doit = team_nl_cmd_noop,
1417 .policy = team_nl_policy,
1420 .cmd = TEAM_CMD_OPTIONS_SET,
1421 .doit = team_nl_cmd_options_set,
1422 .policy = team_nl_policy,
1423 .flags = GENL_ADMIN_PERM,
1426 .cmd = TEAM_CMD_OPTIONS_GET,
1427 .doit = team_nl_cmd_options_get,
1428 .policy = team_nl_policy,
1429 .flags = GENL_ADMIN_PERM,
1432 .cmd = TEAM_CMD_PORT_LIST_GET,
1433 .doit = team_nl_cmd_port_list_get,
1434 .policy = team_nl_policy,
1435 .flags = GENL_ADMIN_PERM,
1439 static struct genl_multicast_group team_change_event_mcgrp = {
1440 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1443 static int team_nl_send_event_options_get(struct team *team,
1444 struct team_option *changed_option)
1446 struct sk_buff *skb;
1448 struct net *net = dev_net(team->dev);
1450 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1454 err = team_nl_fill_options_get_changed(skb, 0, 0, 0, team,
1459 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1468 static int team_nl_send_event_port_list_get(struct team_port *port)
1470 struct sk_buff *skb;
1472 struct net *net = dev_net(port->team->dev);
1474 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1478 err = team_nl_fill_port_list_get_changed(skb, 0, 0, 0,
1483 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1492 static int team_nl_init(void)
1496 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1497 ARRAY_SIZE(team_nl_ops));
1501 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1503 goto err_change_event_grp_reg;
1507 err_change_event_grp_reg:
1508 genl_unregister_family(&team_nl_family);
1513 static void team_nl_fini(void)
1515 genl_unregister_family(&team_nl_family);
1523 static void __team_options_change_check(struct team *team,
1524 struct team_option *changed_option)
1528 err = team_nl_send_event_options_get(team, changed_option);
1530 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1533 /* rtnl lock is held */
1534 static void __team_port_change_check(struct team_port *port, bool linkup)
1538 if (port->linkup == linkup)
1541 port->linkup = linkup;
1543 struct ethtool_cmd ecmd;
1545 err = __ethtool_get_settings(port->dev, &ecmd);
1547 port->speed = ethtool_cmd_speed(&ecmd);
1548 port->duplex = ecmd.duplex;
1556 err = team_nl_send_event_port_list_get(port);
1558 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
1563 static void team_port_change_check(struct team_port *port, bool linkup)
1565 struct team *team = port->team;
1567 mutex_lock(&team->lock);
1568 __team_port_change_check(port, linkup);
1569 mutex_unlock(&team->lock);
1572 /************************************
1573 * Net device notifier event handler
1574 ************************************/
1576 static int team_device_event(struct notifier_block *unused,
1577 unsigned long event, void *ptr)
1579 struct net_device *dev = (struct net_device *) ptr;
1580 struct team_port *port;
1582 port = team_port_get_rtnl(dev);
1588 if (netif_carrier_ok(dev))
1589 team_port_change_check(port, true);
1591 team_port_change_check(port, false);
1593 if (netif_running(port->dev))
1594 team_port_change_check(port,
1595 !!netif_carrier_ok(port->dev));
1597 case NETDEV_UNREGISTER:
1598 team_del_slave(port->team->dev, dev);
1600 case NETDEV_FEAT_CHANGE:
1601 team_compute_features(port->team);
1603 case NETDEV_CHANGEMTU:
1604 /* Forbid to change mtu of underlaying device */
1606 case NETDEV_PRE_TYPE_CHANGE:
1607 /* Forbid to change type of underlaying device */
1613 static struct notifier_block team_notifier_block __read_mostly = {
1614 .notifier_call = team_device_event,
1618 /***********************
1619 * Module init and exit
1620 ***********************/
1622 static int __init team_module_init(void)
1626 register_netdevice_notifier(&team_notifier_block);
1628 err = rtnl_link_register(&team_link_ops);
1632 err = team_nl_init();
1639 rtnl_link_unregister(&team_link_ops);
1642 unregister_netdevice_notifier(&team_notifier_block);
1647 static void __exit team_module_exit(void)
1650 rtnl_link_unregister(&team_link_ops);
1651 unregister_netdevice_notifier(&team_notifier_block);
1654 module_init(team_module_init);
1655 module_exit(team_module_exit);
1657 MODULE_LICENSE("GPL v2");
1658 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
1659 MODULE_DESCRIPTION("Ethernet team device driver");
1660 MODULE_ALIAS_RTNL_LINK(DRV_NAME);