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 void team_options_register(struct team *team, struct team_option *option,
88 for (i = 0; i < option_count; i++, option++)
89 list_add_tail(&option->list, &team->option_list);
91 EXPORT_SYMBOL(team_options_register);
93 static void __team_options_change_check(struct team *team,
94 struct team_option *changed_option);
96 static void __team_options_unregister(struct team *team,
97 struct team_option *option,
102 for (i = 0; i < option_count; i++, option++)
103 list_del(&option->list);
106 void team_options_unregister(struct team *team, struct team_option *option,
109 __team_options_unregister(team, option, option_count);
110 __team_options_change_check(team, NULL);
112 EXPORT_SYMBOL(team_options_unregister);
114 static int team_option_get(struct team *team, struct team_option *option,
117 return option->getter(team, arg);
120 static int team_option_set(struct team *team, struct team_option *option,
125 err = option->setter(team, arg);
129 __team_options_change_check(team, option);
137 static LIST_HEAD(mode_list);
138 static DEFINE_SPINLOCK(mode_list_lock);
140 static struct team_mode *__find_mode(const char *kind)
142 struct team_mode *mode;
144 list_for_each_entry(mode, &mode_list, list) {
145 if (strcmp(mode->kind, kind) == 0)
151 static bool is_good_mode_name(const char *name)
153 while (*name != '\0') {
154 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
161 int team_mode_register(struct team_mode *mode)
165 if (!is_good_mode_name(mode->kind) ||
166 mode->priv_size > TEAM_MODE_PRIV_SIZE)
168 spin_lock(&mode_list_lock);
169 if (__find_mode(mode->kind)) {
173 list_add_tail(&mode->list, &mode_list);
175 spin_unlock(&mode_list_lock);
178 EXPORT_SYMBOL(team_mode_register);
180 int team_mode_unregister(struct team_mode *mode)
182 spin_lock(&mode_list_lock);
183 list_del_init(&mode->list);
184 spin_unlock(&mode_list_lock);
187 EXPORT_SYMBOL(team_mode_unregister);
189 static struct team_mode *team_mode_get(const char *kind)
191 struct team_mode *mode;
193 spin_lock(&mode_list_lock);
194 mode = __find_mode(kind);
196 spin_unlock(&mode_list_lock);
197 request_module("team-mode-%s", kind);
198 spin_lock(&mode_list_lock);
199 mode = __find_mode(kind);
202 if (!try_module_get(mode->owner))
205 spin_unlock(&mode_list_lock);
209 static void team_mode_put(const struct team_mode *mode)
211 module_put(mode->owner);
214 static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
216 dev_kfree_skb_any(skb);
220 rx_handler_result_t team_dummy_receive(struct team *team,
221 struct team_port *port,
224 return RX_HANDLER_ANOTHER;
227 static void team_adjust_ops(struct team *team)
230 * To avoid checks in rx/tx skb paths, ensure here that non-null and
231 * correct ops are always set.
234 if (list_empty(&team->port_list) ||
235 !team->mode || !team->mode->ops->transmit)
236 team->ops.transmit = team_dummy_transmit;
238 team->ops.transmit = team->mode->ops->transmit;
240 if (list_empty(&team->port_list) ||
241 !team->mode || !team->mode->ops->receive)
242 team->ops.receive = team_dummy_receive;
244 team->ops.receive = team->mode->ops->receive;
248 * We can benefit from the fact that it's ensured no port is present
249 * at the time of mode change. Therefore no packets are in fly so there's no
250 * need to set mode operations in any special way.
252 static int __team_change_mode(struct team *team,
253 const struct team_mode *new_mode)
255 /* Check if mode was previously set and do cleanup if so */
257 void (*exit_op)(struct team *team) = team->ops.exit;
259 /* Clear ops area so no callback is called any longer */
260 memset(&team->ops, 0, sizeof(struct team_mode_ops));
261 team_adjust_ops(team);
265 team_mode_put(team->mode);
267 /* zero private data area */
268 memset(&team->mode_priv, 0,
269 sizeof(struct team) - offsetof(struct team, mode_priv));
275 if (new_mode->ops->init) {
278 err = new_mode->ops->init(team);
283 team->mode = new_mode;
284 memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
285 team_adjust_ops(team);
290 static int team_change_mode(struct team *team, const char *kind)
292 struct team_mode *new_mode;
293 struct net_device *dev = team->dev;
296 if (!list_empty(&team->port_list)) {
297 netdev_err(dev, "No ports can be present during mode change\n");
301 if (team->mode && strcmp(team->mode->kind, kind) == 0) {
302 netdev_err(dev, "Unable to change to the same mode the team is in\n");
306 new_mode = team_mode_get(kind);
308 netdev_err(dev, "Mode \"%s\" not found\n", kind);
312 err = __team_change_mode(team, new_mode);
314 netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
315 team_mode_put(new_mode);
319 netdev_info(dev, "Mode changed to \"%s\"\n", kind);
324 /************************
325 * Rx path frame handler
326 ************************/
328 /* note: already called with rcu_read_lock */
329 static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
331 struct sk_buff *skb = *pskb;
332 struct team_port *port;
334 rx_handler_result_t res;
336 skb = skb_share_check(skb, GFP_ATOMIC);
338 return RX_HANDLER_CONSUMED;
342 port = team_port_get_rcu(skb->dev);
345 res = team->ops.receive(team, port, skb);
346 if (res == RX_HANDLER_ANOTHER) {
347 struct team_pcpu_stats *pcpu_stats;
349 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
350 u64_stats_update_begin(&pcpu_stats->syncp);
351 pcpu_stats->rx_packets++;
352 pcpu_stats->rx_bytes += skb->len;
353 if (skb->pkt_type == PACKET_MULTICAST)
354 pcpu_stats->rx_multicast++;
355 u64_stats_update_end(&pcpu_stats->syncp);
357 skb->dev = team->dev;
359 this_cpu_inc(team->pcpu_stats->rx_dropped);
370 static bool team_port_find(const struct team *team,
371 const struct team_port *port)
373 struct team_port *cur;
375 list_for_each_entry(cur, &team->port_list, list)
382 * Add/delete port to the team port list. Write guarded by rtnl_lock.
383 * Takes care of correct port->index setup (might be racy).
385 static void team_port_list_add_port(struct team *team,
386 struct team_port *port)
388 port->index = team->port_count++;
389 hlist_add_head_rcu(&port->hlist,
390 team_port_index_hash(team, port->index));
391 list_add_tail_rcu(&port->list, &team->port_list);
394 static void __reconstruct_port_hlist(struct team *team, int rm_index)
397 struct team_port *port;
399 for (i = rm_index + 1; i < team->port_count; i++) {
400 port = team_get_port_by_index(team, i);
401 hlist_del_rcu(&port->hlist);
403 hlist_add_head_rcu(&port->hlist,
404 team_port_index_hash(team, port->index));
408 static void team_port_list_del_port(struct team *team,
409 struct team_port *port)
411 int rm_index = port->index;
413 hlist_del_rcu(&port->hlist);
414 list_del_rcu(&port->list);
415 __reconstruct_port_hlist(team, rm_index);
419 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
420 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
421 NETIF_F_HIGHDMA | NETIF_F_LRO)
423 static void __team_compute_features(struct team *team)
425 struct team_port *port;
426 u32 vlan_features = TEAM_VLAN_FEATURES;
427 unsigned short max_hard_header_len = ETH_HLEN;
429 list_for_each_entry(port, &team->port_list, list) {
430 vlan_features = netdev_increment_features(vlan_features,
431 port->dev->vlan_features,
434 if (port->dev->hard_header_len > max_hard_header_len)
435 max_hard_header_len = port->dev->hard_header_len;
438 team->dev->vlan_features = vlan_features;
439 team->dev->hard_header_len = max_hard_header_len;
441 netdev_change_features(team->dev);
444 static void team_compute_features(struct team *team)
446 spin_lock(&team->lock);
447 __team_compute_features(team);
448 spin_unlock(&team->lock);
451 static int team_port_enter(struct team *team, struct team_port *port)
456 port->dev->priv_flags |= IFF_TEAM_PORT;
457 if (team->ops.port_enter) {
458 err = team->ops.port_enter(team, port);
460 netdev_err(team->dev, "Device %s failed to enter team mode\n",
469 port->dev->priv_flags &= ~IFF_TEAM_PORT;
475 static void team_port_leave(struct team *team, struct team_port *port)
477 if (team->ops.port_leave)
478 team->ops.port_leave(team, port);
479 port->dev->priv_flags &= ~IFF_TEAM_PORT;
483 static void __team_port_change_check(struct team_port *port, bool linkup);
485 static int team_port_add(struct team *team, struct net_device *port_dev)
487 struct net_device *dev = team->dev;
488 struct team_port *port;
489 char *portname = port_dev->name;
492 if (port_dev->flags & IFF_LOOPBACK ||
493 port_dev->type != ARPHRD_ETHER) {
494 netdev_err(dev, "Device %s is of an unsupported type\n",
499 if (team_port_exists(port_dev)) {
500 netdev_err(dev, "Device %s is already a port "
501 "of a team device\n", portname);
505 if (port_dev->flags & IFF_UP) {
506 netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
511 port = kzalloc(sizeof(struct team_port), GFP_KERNEL);
515 port->dev = port_dev;
518 port->orig.mtu = port_dev->mtu;
519 err = dev_set_mtu(port_dev, dev->mtu);
521 netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
525 memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
527 err = team_port_enter(team, port);
529 netdev_err(dev, "Device %s failed to enter team mode\n",
534 err = dev_open(port_dev);
536 netdev_dbg(dev, "Device %s opening failed\n",
541 err = netdev_set_master(port_dev, dev);
543 netdev_err(dev, "Device %s failed to set master\n", portname);
547 err = netdev_rx_handler_register(port_dev, team_handle_frame,
550 netdev_err(dev, "Device %s failed to register rx_handler\n",
552 goto err_handler_register;
555 team_port_list_add_port(team, port);
556 team_adjust_ops(team);
557 __team_compute_features(team);
558 __team_port_change_check(port, !!netif_carrier_ok(port_dev));
560 netdev_info(dev, "Port device %s added\n", portname);
564 err_handler_register:
565 netdev_set_master(port_dev, NULL);
571 team_port_leave(team, port);
572 team_port_set_orig_mac(port);
575 dev_set_mtu(port_dev, port->orig.mtu);
583 static int team_port_del(struct team *team, struct net_device *port_dev)
585 struct net_device *dev = team->dev;
586 struct team_port *port;
587 char *portname = port_dev->name;
589 port = team_port_get_rtnl(port_dev);
590 if (!port || !team_port_find(team, port)) {
591 netdev_err(dev, "Device %s does not act as a port of this team\n",
596 __team_port_change_check(port, false);
597 team_port_list_del_port(team, port);
598 team_adjust_ops(team);
599 netdev_rx_handler_unregister(port_dev);
600 netdev_set_master(port_dev, NULL);
602 team_port_leave(team, port);
603 team_port_set_orig_mac(port);
604 dev_set_mtu(port_dev, port->orig.mtu);
607 netdev_info(dev, "Port device %s removed\n", portname);
608 __team_compute_features(team);
618 static const char team_no_mode_kind[] = "*NOMODE*";
620 static int team_mode_option_get(struct team *team, void *arg)
622 const char **str = arg;
624 *str = team->mode ? team->mode->kind : team_no_mode_kind;
628 static int team_mode_option_set(struct team *team, void *arg)
630 const char **str = arg;
632 return team_change_mode(team, *str);
635 static struct team_option team_options[] = {
638 .type = TEAM_OPTION_TYPE_STRING,
639 .getter = team_mode_option_get,
640 .setter = team_mode_option_set,
644 static int team_init(struct net_device *dev)
646 struct team *team = netdev_priv(dev);
650 spin_lock_init(&team->lock);
652 team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
653 if (!team->pcpu_stats)
656 for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
657 INIT_HLIST_HEAD(&team->port_hlist[i]);
658 INIT_LIST_HEAD(&team->port_list);
660 team_adjust_ops(team);
662 INIT_LIST_HEAD(&team->option_list);
663 team_options_register(team, team_options, ARRAY_SIZE(team_options));
664 netif_carrier_off(dev);
669 static void team_uninit(struct net_device *dev)
671 struct team *team = netdev_priv(dev);
672 struct team_port *port;
673 struct team_port *tmp;
675 spin_lock(&team->lock);
676 list_for_each_entry_safe(port, tmp, &team->port_list, list)
677 team_port_del(team, port->dev);
679 __team_change_mode(team, NULL); /* cleanup */
680 __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
681 spin_unlock(&team->lock);
684 static void team_destructor(struct net_device *dev)
686 struct team *team = netdev_priv(dev);
688 free_percpu(team->pcpu_stats);
692 static int team_open(struct net_device *dev)
694 netif_carrier_on(dev);
698 static int team_close(struct net_device *dev)
700 netif_carrier_off(dev);
705 * note: already called with rcu_read_lock
707 static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
709 struct team *team = netdev_priv(dev);
710 bool tx_success = false;
711 unsigned int len = skb->len;
713 tx_success = team->ops.transmit(team, skb);
715 struct team_pcpu_stats *pcpu_stats;
717 pcpu_stats = this_cpu_ptr(team->pcpu_stats);
718 u64_stats_update_begin(&pcpu_stats->syncp);
719 pcpu_stats->tx_packets++;
720 pcpu_stats->tx_bytes += len;
721 u64_stats_update_end(&pcpu_stats->syncp);
723 this_cpu_inc(team->pcpu_stats->tx_dropped);
729 static void team_change_rx_flags(struct net_device *dev, int change)
731 struct team *team = netdev_priv(dev);
732 struct team_port *port;
736 list_for_each_entry_rcu(port, &team->port_list, list) {
737 if (change & IFF_PROMISC) {
738 inc = dev->flags & IFF_PROMISC ? 1 : -1;
739 dev_set_promiscuity(port->dev, inc);
741 if (change & IFF_ALLMULTI) {
742 inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
743 dev_set_allmulti(port->dev, inc);
749 static void team_set_rx_mode(struct net_device *dev)
751 struct team *team = netdev_priv(dev);
752 struct team_port *port;
755 list_for_each_entry_rcu(port, &team->port_list, list) {
756 dev_uc_sync(port->dev, dev);
757 dev_mc_sync(port->dev, dev);
762 static int team_set_mac_address(struct net_device *dev, void *p)
764 struct team *team = netdev_priv(dev);
765 struct team_port *port;
766 struct sockaddr *addr = p;
768 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
770 list_for_each_entry_rcu(port, &team->port_list, list)
771 if (team->ops.port_change_mac)
772 team->ops.port_change_mac(team, port);
777 static int team_change_mtu(struct net_device *dev, int new_mtu)
779 struct team *team = netdev_priv(dev);
780 struct team_port *port;
784 * Alhough this is reader, it's guarded by team lock. It's not possible
785 * to traverse list in reverse under rcu_read_lock
787 spin_lock(&team->lock);
788 list_for_each_entry(port, &team->port_list, list) {
789 err = dev_set_mtu(port->dev, new_mtu);
791 netdev_err(dev, "Device %s failed to change mtu",
796 spin_unlock(&team->lock);
803 list_for_each_entry_continue_reverse(port, &team->port_list, list)
804 dev_set_mtu(port->dev, dev->mtu);
805 spin_unlock(&team->lock);
810 static struct rtnl_link_stats64 *
811 team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
813 struct team *team = netdev_priv(dev);
814 struct team_pcpu_stats *p;
815 u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
816 u32 rx_dropped = 0, tx_dropped = 0;
820 for_each_possible_cpu(i) {
821 p = per_cpu_ptr(team->pcpu_stats, i);
823 start = u64_stats_fetch_begin_bh(&p->syncp);
824 rx_packets = p->rx_packets;
825 rx_bytes = p->rx_bytes;
826 rx_multicast = p->rx_multicast;
827 tx_packets = p->tx_packets;
828 tx_bytes = p->tx_bytes;
829 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
831 stats->rx_packets += rx_packets;
832 stats->rx_bytes += rx_bytes;
833 stats->multicast += rx_multicast;
834 stats->tx_packets += tx_packets;
835 stats->tx_bytes += tx_bytes;
837 * rx_dropped & tx_dropped are u32, updated
838 * without syncp protection.
840 rx_dropped += p->rx_dropped;
841 tx_dropped += p->tx_dropped;
843 stats->rx_dropped = rx_dropped;
844 stats->tx_dropped = tx_dropped;
848 static void team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
850 struct team *team = netdev_priv(dev);
851 struct team_port *port;
854 list_for_each_entry_rcu(port, &team->port_list, list) {
855 const struct net_device_ops *ops = port->dev->netdev_ops;
857 if (ops->ndo_vlan_rx_add_vid)
858 ops->ndo_vlan_rx_add_vid(port->dev, vid);
863 static void team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
865 struct team *team = netdev_priv(dev);
866 struct team_port *port;
869 list_for_each_entry_rcu(port, &team->port_list, list) {
870 const struct net_device_ops *ops = port->dev->netdev_ops;
872 if (ops->ndo_vlan_rx_kill_vid)
873 ops->ndo_vlan_rx_kill_vid(port->dev, vid);
878 static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
880 struct team *team = netdev_priv(dev);
883 spin_lock(&team->lock);
884 err = team_port_add(team, port_dev);
885 spin_unlock(&team->lock);
889 static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
891 struct team *team = netdev_priv(dev);
894 spin_lock(&team->lock);
895 err = team_port_del(team, port_dev);
896 spin_unlock(&team->lock);
900 static const struct net_device_ops team_netdev_ops = {
901 .ndo_init = team_init,
902 .ndo_uninit = team_uninit,
903 .ndo_open = team_open,
904 .ndo_stop = team_close,
905 .ndo_start_xmit = team_xmit,
906 .ndo_change_rx_flags = team_change_rx_flags,
907 .ndo_set_rx_mode = team_set_rx_mode,
908 .ndo_set_mac_address = team_set_mac_address,
909 .ndo_change_mtu = team_change_mtu,
910 .ndo_get_stats64 = team_get_stats64,
911 .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
912 .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
913 .ndo_add_slave = team_add_slave,
914 .ndo_del_slave = team_del_slave,
918 /***********************
919 * rt netlink interface
920 ***********************/
922 static void team_setup(struct net_device *dev)
926 dev->netdev_ops = &team_netdev_ops;
927 dev->destructor = team_destructor;
928 dev->tx_queue_len = 0;
929 dev->flags |= IFF_MULTICAST;
930 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
933 * Indicate we support unicast address filtering. That way core won't
934 * bring us to promisc mode in case a unicast addr is added.
935 * Let this up to underlay drivers.
937 dev->priv_flags |= IFF_UNICAST_FLT;
939 dev->features |= NETIF_F_LLTX;
940 dev->features |= NETIF_F_GRO;
941 dev->hw_features = NETIF_F_HW_VLAN_TX |
943 NETIF_F_HW_VLAN_FILTER;
945 dev->features |= dev->hw_features;
948 static int team_newlink(struct net *src_net, struct net_device *dev,
949 struct nlattr *tb[], struct nlattr *data[])
953 if (tb[IFLA_ADDRESS] == NULL)
954 random_ether_addr(dev->dev_addr);
956 err = register_netdevice(dev);
963 static int team_validate(struct nlattr *tb[], struct nlattr *data[])
965 if (tb[IFLA_ADDRESS]) {
966 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
968 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
969 return -EADDRNOTAVAIL;
974 static struct rtnl_link_ops team_link_ops __read_mostly = {
976 .priv_size = sizeof(struct team),
978 .newlink = team_newlink,
979 .validate = team_validate,
983 /***********************************
984 * Generic netlink custom interface
985 ***********************************/
987 static struct genl_family team_nl_family = {
988 .id = GENL_ID_GENERATE,
989 .name = TEAM_GENL_NAME,
990 .version = TEAM_GENL_VERSION,
991 .maxattr = TEAM_ATTR_MAX,
995 static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
996 [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
997 [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
998 [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
999 [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
1002 static const struct nla_policy
1003 team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
1004 [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
1005 [TEAM_ATTR_OPTION_NAME] = {
1007 .len = TEAM_STRING_MAX_LEN,
1009 [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
1010 [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
1011 [TEAM_ATTR_OPTION_DATA] = {
1013 .len = TEAM_STRING_MAX_LEN,
1017 static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
1019 struct sk_buff *msg;
1023 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1027 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
1028 &team_nl_family, 0, TEAM_CMD_NOOP);
1034 genlmsg_end(msg, hdr);
1036 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
1045 * Netlink cmd functions should be locked by following two functions.
1046 * To ensure team_uninit would not be called in between, hold rcu_read_lock
1049 static struct team *team_nl_team_get(struct genl_info *info)
1051 struct net *net = genl_info_net(info);
1053 struct net_device *dev;
1056 if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
1059 ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
1061 dev = dev_get_by_index_rcu(net, ifindex);
1062 if (!dev || dev->netdev_ops != &team_netdev_ops) {
1067 team = netdev_priv(dev);
1068 spin_lock(&team->lock);
1072 static void team_nl_team_put(struct team *team)
1074 spin_unlock(&team->lock);
1078 static int team_nl_send_generic(struct genl_info *info, struct team *team,
1079 int (*fill_func)(struct sk_buff *skb,
1080 struct genl_info *info,
1081 int flags, struct team *team))
1083 struct sk_buff *skb;
1086 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1090 err = fill_func(skb, info, NLM_F_ACK, team);
1094 err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
1102 static int team_nl_fill_options_get_changed(struct sk_buff *skb,
1103 u32 pid, u32 seq, int flags,
1105 struct team_option *changed_option)
1107 struct nlattr *option_list;
1109 struct team_option *option;
1111 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1112 TEAM_CMD_OPTIONS_GET);
1114 return PTR_ERR(hdr);
1116 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
1117 option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
1121 list_for_each_entry(option, &team->option_list, list) {
1122 struct nlattr *option_item;
1125 option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
1127 goto nla_put_failure;
1128 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name);
1129 if (option == changed_option)
1130 NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED);
1131 switch (option->type) {
1132 case TEAM_OPTION_TYPE_U32:
1133 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32);
1134 team_option_get(team, option, &arg);
1135 NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg);
1137 case TEAM_OPTION_TYPE_STRING:
1138 NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING);
1139 team_option_get(team, option, &arg);
1140 NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA,
1146 nla_nest_end(skb, option_item);
1149 nla_nest_end(skb, option_list);
1150 return genlmsg_end(skb, hdr);
1153 genlmsg_cancel(skb, hdr);
1157 static int team_nl_fill_options_get(struct sk_buff *skb,
1158 struct genl_info *info, int flags,
1161 return team_nl_fill_options_get_changed(skb, info->snd_pid,
1162 info->snd_seq, NLM_F_ACK,
1166 static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
1171 team = team_nl_team_get(info);
1175 err = team_nl_send_generic(info, team, team_nl_fill_options_get);
1177 team_nl_team_put(team);
1182 static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
1187 struct nlattr *nl_option;
1189 team = team_nl_team_get(info);
1194 if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
1199 nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
1200 struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1];
1201 enum team_option_type opt_type;
1202 struct team_option *option;
1204 bool opt_found = false;
1206 if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
1210 err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX,
1211 nl_option, team_nl_option_policy);
1214 if (!mode_attrs[TEAM_ATTR_OPTION_NAME] ||
1215 !mode_attrs[TEAM_ATTR_OPTION_TYPE] ||
1216 !mode_attrs[TEAM_ATTR_OPTION_DATA]) {
1220 switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) {
1222 opt_type = TEAM_OPTION_TYPE_U32;
1225 opt_type = TEAM_OPTION_TYPE_STRING;
1231 opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]);
1232 list_for_each_entry(option, &team->option_list, list) {
1234 struct nlattr *opt_data_attr;
1236 if (option->type != opt_type ||
1237 strcmp(option->name, opt_name))
1240 opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA];
1242 case TEAM_OPTION_TYPE_U32:
1243 arg = nla_get_u32(opt_data_attr);
1245 case TEAM_OPTION_TYPE_STRING:
1246 arg = (long) nla_data(opt_data_attr);
1251 err = team_option_set(team, option, &arg);
1262 team_nl_team_put(team);
1267 static int team_nl_fill_port_list_get_changed(struct sk_buff *skb,
1268 u32 pid, u32 seq, int flags,
1270 struct team_port *changed_port)
1272 struct nlattr *port_list;
1274 struct team_port *port;
1276 hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
1277 TEAM_CMD_PORT_LIST_GET);
1279 return PTR_ERR(hdr);
1281 NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
1282 port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
1286 list_for_each_entry(port, &team->port_list, list) {
1287 struct nlattr *port_item;
1289 port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
1291 goto nla_put_failure;
1292 NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex);
1293 if (port == changed_port)
1294 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED);
1296 NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP);
1297 NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed);
1298 NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex);
1299 nla_nest_end(skb, port_item);
1302 nla_nest_end(skb, port_list);
1303 return genlmsg_end(skb, hdr);
1306 genlmsg_cancel(skb, hdr);
1310 static int team_nl_fill_port_list_get(struct sk_buff *skb,
1311 struct genl_info *info, int flags,
1314 return team_nl_fill_port_list_get_changed(skb, info->snd_pid,
1315 info->snd_seq, NLM_F_ACK,
1319 static int team_nl_cmd_port_list_get(struct sk_buff *skb,
1320 struct genl_info *info)
1325 team = team_nl_team_get(info);
1329 err = team_nl_send_generic(info, team, team_nl_fill_port_list_get);
1331 team_nl_team_put(team);
1336 static struct genl_ops team_nl_ops[] = {
1338 .cmd = TEAM_CMD_NOOP,
1339 .doit = team_nl_cmd_noop,
1340 .policy = team_nl_policy,
1343 .cmd = TEAM_CMD_OPTIONS_SET,
1344 .doit = team_nl_cmd_options_set,
1345 .policy = team_nl_policy,
1346 .flags = GENL_ADMIN_PERM,
1349 .cmd = TEAM_CMD_OPTIONS_GET,
1350 .doit = team_nl_cmd_options_get,
1351 .policy = team_nl_policy,
1352 .flags = GENL_ADMIN_PERM,
1355 .cmd = TEAM_CMD_PORT_LIST_GET,
1356 .doit = team_nl_cmd_port_list_get,
1357 .policy = team_nl_policy,
1358 .flags = GENL_ADMIN_PERM,
1362 static struct genl_multicast_group team_change_event_mcgrp = {
1363 .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
1366 static int team_nl_send_event_options_get(struct team *team,
1367 struct team_option *changed_option)
1369 struct sk_buff *skb;
1371 struct net *net = dev_net(team->dev);
1373 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1377 err = team_nl_fill_options_get_changed(skb, 0, 0, 0, team,
1382 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1391 static int team_nl_send_event_port_list_get(struct team_port *port)
1393 struct sk_buff *skb;
1395 struct net *net = dev_net(port->team->dev);
1397 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1401 err = team_nl_fill_port_list_get_changed(skb, 0, 0, 0,
1406 err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
1415 static int team_nl_init(void)
1419 err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
1420 ARRAY_SIZE(team_nl_ops));
1424 err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
1426 goto err_change_event_grp_reg;
1430 err_change_event_grp_reg:
1431 genl_unregister_family(&team_nl_family);
1436 static void team_nl_fini(void)
1438 genl_unregister_family(&team_nl_family);
1446 static void __team_options_change_check(struct team *team,
1447 struct team_option *changed_option)
1451 err = team_nl_send_event_options_get(team, changed_option);
1453 netdev_warn(team->dev, "Failed to send options change via netlink\n");
1456 /* rtnl lock is held */
1457 static void __team_port_change_check(struct team_port *port, bool linkup)
1461 if (port->linkup == linkup)
1464 port->linkup = linkup;
1466 struct ethtool_cmd ecmd;
1468 err = __ethtool_get_settings(port->dev, &ecmd);
1470 port->speed = ethtool_cmd_speed(&ecmd);
1471 port->duplex = ecmd.duplex;
1479 err = team_nl_send_event_port_list_get(port);
1481 netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
1486 static void team_port_change_check(struct team_port *port, bool linkup)
1488 struct team *team = port->team;
1490 spin_lock(&team->lock);
1491 __team_port_change_check(port, linkup);
1492 spin_unlock(&team->lock);
1495 /************************************
1496 * Net device notifier event handler
1497 ************************************/
1499 static int team_device_event(struct notifier_block *unused,
1500 unsigned long event, void *ptr)
1502 struct net_device *dev = (struct net_device *) ptr;
1503 struct team_port *port;
1505 port = team_port_get_rtnl(dev);
1511 if (netif_carrier_ok(dev))
1512 team_port_change_check(port, true);
1514 team_port_change_check(port, false);
1516 if (netif_running(port->dev))
1517 team_port_change_check(port,
1518 !!netif_carrier_ok(port->dev));
1520 case NETDEV_UNREGISTER:
1521 team_del_slave(port->team->dev, dev);
1523 case NETDEV_FEAT_CHANGE:
1524 team_compute_features(port->team);
1526 case NETDEV_CHANGEMTU:
1527 /* Forbid to change mtu of underlaying device */
1529 case NETDEV_PRE_TYPE_CHANGE:
1530 /* Forbid to change type of underlaying device */
1536 static struct notifier_block team_notifier_block __read_mostly = {
1537 .notifier_call = team_device_event,
1541 /***********************
1542 * Module init and exit
1543 ***********************/
1545 static int __init team_module_init(void)
1549 register_netdevice_notifier(&team_notifier_block);
1551 err = rtnl_link_register(&team_link_ops);
1555 err = team_nl_init();
1562 rtnl_link_unregister(&team_link_ops);
1565 unregister_netdevice_notifier(&team_notifier_block);
1570 static void __exit team_module_exit(void)
1573 rtnl_link_unregister(&team_link_ops);
1574 unregister_netdevice_notifier(&team_notifier_block);
1577 module_init(team_module_init);
1578 module_exit(team_module_exit);
1580 MODULE_LICENSE("GPL v2");
1581 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
1582 MODULE_DESCRIPTION("Ethernet team device driver");
1583 MODULE_ALIAS_RTNL_LINK(DRV_NAME);