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 dst_opts
[i
]->changed
= true;
121 dst_opts
[i
]->removed
= false;
122 list_add_tail(&dst_opts
[i
]->list
, &team
->option_list
);
129 for (i
= 0; i
< option_count
; i
++)
136 static void __team_options_mark_removed(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 del_opt
->changed
= true;
148 del_opt
->removed
= true;
153 static void __team_options_unregister(struct team
*team
,
154 const struct team_option
*option
,
159 for (i
= 0; i
< option_count
; i
++, option
++) {
160 struct team_option
*del_opt
;
162 del_opt
= __team_find_option(team
, option
->name
);
164 list_del(&del_opt
->list
);
170 static void __team_options_change_check(struct team
*team
);
172 int team_options_register(struct team
*team
,
173 const struct team_option
*option
,
178 err
= __team_options_register(team
, option
, option_count
);
181 __team_options_change_check(team
);
184 EXPORT_SYMBOL(team_options_register
);
186 void team_options_unregister(struct team
*team
,
187 const struct team_option
*option
,
190 __team_options_mark_removed(team
, option
, option_count
);
191 __team_options_change_check(team
);
192 __team_options_unregister(team
, option
, option_count
);
194 EXPORT_SYMBOL(team_options_unregister
);
196 static int team_option_get(struct team
*team
, struct team_option
*option
,
199 return option
->getter(team
, arg
);
202 static int team_option_set(struct team
*team
, struct team_option
*option
,
207 err
= option
->setter(team
, arg
);
211 option
->changed
= true;
212 __team_options_change_check(team
);
220 static LIST_HEAD(mode_list
);
221 static DEFINE_SPINLOCK(mode_list_lock
);
223 static struct team_mode
*__find_mode(const char *kind
)
225 struct team_mode
*mode
;
227 list_for_each_entry(mode
, &mode_list
, list
) {
228 if (strcmp(mode
->kind
, kind
) == 0)
234 static bool is_good_mode_name(const char *name
)
236 while (*name
!= '\0') {
237 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
244 int team_mode_register(struct team_mode
*mode
)
248 if (!is_good_mode_name(mode
->kind
) ||
249 mode
->priv_size
> TEAM_MODE_PRIV_SIZE
)
251 spin_lock(&mode_list_lock
);
252 if (__find_mode(mode
->kind
)) {
256 list_add_tail(&mode
->list
, &mode_list
);
258 spin_unlock(&mode_list_lock
);
261 EXPORT_SYMBOL(team_mode_register
);
263 int team_mode_unregister(struct team_mode
*mode
)
265 spin_lock(&mode_list_lock
);
266 list_del_init(&mode
->list
);
267 spin_unlock(&mode_list_lock
);
270 EXPORT_SYMBOL(team_mode_unregister
);
272 static struct team_mode
*team_mode_get(const char *kind
)
274 struct team_mode
*mode
;
276 spin_lock(&mode_list_lock
);
277 mode
= __find_mode(kind
);
279 spin_unlock(&mode_list_lock
);
280 request_module("team-mode-%s", kind
);
281 spin_lock(&mode_list_lock
);
282 mode
= __find_mode(kind
);
285 if (!try_module_get(mode
->owner
))
288 spin_unlock(&mode_list_lock
);
292 static void team_mode_put(const struct team_mode
*mode
)
294 module_put(mode
->owner
);
297 static bool team_dummy_transmit(struct team
*team
, struct sk_buff
*skb
)
299 dev_kfree_skb_any(skb
);
303 rx_handler_result_t
team_dummy_receive(struct team
*team
,
304 struct team_port
*port
,
307 return RX_HANDLER_ANOTHER
;
310 static void team_adjust_ops(struct team
*team
)
313 * To avoid checks in rx/tx skb paths, ensure here that non-null and
314 * correct ops are always set.
317 if (list_empty(&team
->port_list
) ||
318 !team
->mode
|| !team
->mode
->ops
->transmit
)
319 team
->ops
.transmit
= team_dummy_transmit
;
321 team
->ops
.transmit
= team
->mode
->ops
->transmit
;
323 if (list_empty(&team
->port_list
) ||
324 !team
->mode
|| !team
->mode
->ops
->receive
)
325 team
->ops
.receive
= team_dummy_receive
;
327 team
->ops
.receive
= team
->mode
->ops
->receive
;
331 * We can benefit from the fact that it's ensured no port is present
332 * at the time of mode change. Therefore no packets are in fly so there's no
333 * need to set mode operations in any special way.
335 static int __team_change_mode(struct team
*team
,
336 const struct team_mode
*new_mode
)
338 /* Check if mode was previously set and do cleanup if so */
340 void (*exit_op
)(struct team
*team
) = team
->ops
.exit
;
342 /* Clear ops area so no callback is called any longer */
343 memset(&team
->ops
, 0, sizeof(struct team_mode_ops
));
344 team_adjust_ops(team
);
348 team_mode_put(team
->mode
);
350 /* zero private data area */
351 memset(&team
->mode_priv
, 0,
352 sizeof(struct team
) - offsetof(struct team
, mode_priv
));
358 if (new_mode
->ops
->init
) {
361 err
= new_mode
->ops
->init(team
);
366 team
->mode
= new_mode
;
367 memcpy(&team
->ops
, new_mode
->ops
, sizeof(struct team_mode_ops
));
368 team_adjust_ops(team
);
373 static int team_change_mode(struct team
*team
, const char *kind
)
375 struct team_mode
*new_mode
;
376 struct net_device
*dev
= team
->dev
;
379 if (!list_empty(&team
->port_list
)) {
380 netdev_err(dev
, "No ports can be present during mode change\n");
384 if (team
->mode
&& strcmp(team
->mode
->kind
, kind
) == 0) {
385 netdev_err(dev
, "Unable to change to the same mode the team is in\n");
389 new_mode
= team_mode_get(kind
);
391 netdev_err(dev
, "Mode \"%s\" not found\n", kind
);
395 err
= __team_change_mode(team
, new_mode
);
397 netdev_err(dev
, "Failed to change to mode \"%s\"\n", kind
);
398 team_mode_put(new_mode
);
402 netdev_info(dev
, "Mode changed to \"%s\"\n", kind
);
407 /************************
408 * Rx path frame handler
409 ************************/
411 /* note: already called with rcu_read_lock */
412 static rx_handler_result_t
team_handle_frame(struct sk_buff
**pskb
)
414 struct sk_buff
*skb
= *pskb
;
415 struct team_port
*port
;
417 rx_handler_result_t res
;
419 skb
= skb_share_check(skb
, GFP_ATOMIC
);
421 return RX_HANDLER_CONSUMED
;
425 port
= team_port_get_rcu(skb
->dev
);
428 res
= team
->ops
.receive(team
, port
, skb
);
429 if (res
== RX_HANDLER_ANOTHER
) {
430 struct team_pcpu_stats
*pcpu_stats
;
432 pcpu_stats
= this_cpu_ptr(team
->pcpu_stats
);
433 u64_stats_update_begin(&pcpu_stats
->syncp
);
434 pcpu_stats
->rx_packets
++;
435 pcpu_stats
->rx_bytes
+= skb
->len
;
436 if (skb
->pkt_type
== PACKET_MULTICAST
)
437 pcpu_stats
->rx_multicast
++;
438 u64_stats_update_end(&pcpu_stats
->syncp
);
440 skb
->dev
= team
->dev
;
442 this_cpu_inc(team
->pcpu_stats
->rx_dropped
);
453 static bool team_port_find(const struct team
*team
,
454 const struct team_port
*port
)
456 struct team_port
*cur
;
458 list_for_each_entry(cur
, &team
->port_list
, list
)
465 * Add/delete port to the team port list. Write guarded by rtnl_lock.
466 * Takes care of correct port->index setup (might be racy).
468 static void team_port_list_add_port(struct team
*team
,
469 struct team_port
*port
)
471 port
->index
= team
->port_count
++;
472 hlist_add_head_rcu(&port
->hlist
,
473 team_port_index_hash(team
, port
->index
));
474 list_add_tail_rcu(&port
->list
, &team
->port_list
);
477 static void __reconstruct_port_hlist(struct team
*team
, int rm_index
)
480 struct team_port
*port
;
482 for (i
= rm_index
+ 1; i
< team
->port_count
; i
++) {
483 port
= team_get_port_by_index(team
, i
);
484 hlist_del_rcu(&port
->hlist
);
486 hlist_add_head_rcu(&port
->hlist
,
487 team_port_index_hash(team
, port
->index
));
491 static void team_port_list_del_port(struct team
*team
,
492 struct team_port
*port
)
494 int rm_index
= port
->index
;
496 hlist_del_rcu(&port
->hlist
);
497 list_del_rcu(&port
->list
);
498 __reconstruct_port_hlist(team
, rm_index
);
502 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
503 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
504 NETIF_F_HIGHDMA | NETIF_F_LRO)
506 static void __team_compute_features(struct team
*team
)
508 struct team_port
*port
;
509 u32 vlan_features
= TEAM_VLAN_FEATURES
;
510 unsigned short max_hard_header_len
= ETH_HLEN
;
512 list_for_each_entry(port
, &team
->port_list
, list
) {
513 vlan_features
= netdev_increment_features(vlan_features
,
514 port
->dev
->vlan_features
,
517 if (port
->dev
->hard_header_len
> max_hard_header_len
)
518 max_hard_header_len
= port
->dev
->hard_header_len
;
521 team
->dev
->vlan_features
= vlan_features
;
522 team
->dev
->hard_header_len
= max_hard_header_len
;
524 netdev_change_features(team
->dev
);
527 static void team_compute_features(struct team
*team
)
529 mutex_lock(&team
->lock
);
530 __team_compute_features(team
);
531 mutex_unlock(&team
->lock
);
534 static int team_port_enter(struct team
*team
, struct team_port
*port
)
539 port
->dev
->priv_flags
|= IFF_TEAM_PORT
;
540 if (team
->ops
.port_enter
) {
541 err
= team
->ops
.port_enter(team
, port
);
543 netdev_err(team
->dev
, "Device %s failed to enter team mode\n",
552 port
->dev
->priv_flags
&= ~IFF_TEAM_PORT
;
558 static void team_port_leave(struct team
*team
, struct team_port
*port
)
560 if (team
->ops
.port_leave
)
561 team
->ops
.port_leave(team
, port
);
562 port
->dev
->priv_flags
&= ~IFF_TEAM_PORT
;
566 static void __team_port_change_check(struct team_port
*port
, bool linkup
);
568 static int team_port_add(struct team
*team
, struct net_device
*port_dev
)
570 struct net_device
*dev
= team
->dev
;
571 struct team_port
*port
;
572 char *portname
= port_dev
->name
;
575 if (port_dev
->flags
& IFF_LOOPBACK
||
576 port_dev
->type
!= ARPHRD_ETHER
) {
577 netdev_err(dev
, "Device %s is of an unsupported type\n",
582 if (team_port_exists(port_dev
)) {
583 netdev_err(dev
, "Device %s is already a port "
584 "of a team device\n", portname
);
588 if (port_dev
->flags
& IFF_UP
) {
589 netdev_err(dev
, "Device %s is up. Set it down before adding it as a team port\n",
594 port
= kzalloc(sizeof(struct team_port
), GFP_KERNEL
);
598 port
->dev
= port_dev
;
601 port
->orig
.mtu
= port_dev
->mtu
;
602 err
= dev_set_mtu(port_dev
, dev
->mtu
);
604 netdev_dbg(dev
, "Error %d calling dev_set_mtu\n", err
);
608 memcpy(port
->orig
.dev_addr
, port_dev
->dev_addr
, ETH_ALEN
);
610 err
= team_port_enter(team
, port
);
612 netdev_err(dev
, "Device %s failed to enter team mode\n",
617 err
= dev_open(port_dev
);
619 netdev_dbg(dev
, "Device %s opening failed\n",
624 err
= vlan_vids_add_by_dev(port_dev
, dev
);
626 netdev_err(dev
, "Failed to add vlan ids to device %s\n",
631 err
= netdev_set_master(port_dev
, dev
);
633 netdev_err(dev
, "Device %s failed to set master\n", portname
);
637 err
= netdev_rx_handler_register(port_dev
, team_handle_frame
,
640 netdev_err(dev
, "Device %s failed to register rx_handler\n",
642 goto err_handler_register
;
645 team_port_list_add_port(team
, port
);
646 team_adjust_ops(team
);
647 __team_compute_features(team
);
648 __team_port_change_check(port
, !!netif_carrier_ok(port_dev
));
650 netdev_info(dev
, "Port device %s added\n", portname
);
654 err_handler_register
:
655 netdev_set_master(port_dev
, NULL
);
658 vlan_vids_del_by_dev(port_dev
, dev
);
664 team_port_leave(team
, port
);
665 team_port_set_orig_mac(port
);
668 dev_set_mtu(port_dev
, port
->orig
.mtu
);
676 static int team_port_del(struct team
*team
, struct net_device
*port_dev
)
678 struct net_device
*dev
= team
->dev
;
679 struct team_port
*port
;
680 char *portname
= port_dev
->name
;
682 port
= team_port_get_rtnl(port_dev
);
683 if (!port
|| !team_port_find(team
, port
)) {
684 netdev_err(dev
, "Device %s does not act as a port of this team\n",
689 port
->removed
= true;
690 __team_port_change_check(port
, false);
691 team_port_list_del_port(team
, port
);
692 team_adjust_ops(team
);
693 netdev_rx_handler_unregister(port_dev
);
694 netdev_set_master(port_dev
, NULL
);
695 vlan_vids_del_by_dev(port_dev
, dev
);
697 team_port_leave(team
, port
);
698 team_port_set_orig_mac(port
);
699 dev_set_mtu(port_dev
, port
->orig
.mtu
);
702 netdev_info(dev
, "Port device %s removed\n", portname
);
703 __team_compute_features(team
);
713 static const char team_no_mode_kind
[] = "*NOMODE*";
715 static int team_mode_option_get(struct team
*team
, void *arg
)
717 const char **str
= arg
;
719 *str
= team
->mode
? team
->mode
->kind
: team_no_mode_kind
;
723 static int team_mode_option_set(struct team
*team
, void *arg
)
725 const char **str
= arg
;
727 return team_change_mode(team
, *str
);
730 static const struct team_option team_options
[] = {
733 .type
= TEAM_OPTION_TYPE_STRING
,
734 .getter
= team_mode_option_get
,
735 .setter
= team_mode_option_set
,
739 static int team_init(struct net_device
*dev
)
741 struct team
*team
= netdev_priv(dev
);
746 mutex_init(&team
->lock
);
748 team
->pcpu_stats
= alloc_percpu(struct team_pcpu_stats
);
749 if (!team
->pcpu_stats
)
752 for (i
= 0; i
< TEAM_PORT_HASHENTRIES
; i
++)
753 INIT_HLIST_HEAD(&team
->port_hlist
[i
]);
754 INIT_LIST_HEAD(&team
->port_list
);
756 team_adjust_ops(team
);
758 INIT_LIST_HEAD(&team
->option_list
);
759 err
= team_options_register(team
, team_options
, ARRAY_SIZE(team_options
));
761 goto err_options_register
;
762 netif_carrier_off(dev
);
766 err_options_register
:
767 free_percpu(team
->pcpu_stats
);
772 static void team_uninit(struct net_device
*dev
)
774 struct team
*team
= netdev_priv(dev
);
775 struct team_port
*port
;
776 struct team_port
*tmp
;
778 mutex_lock(&team
->lock
);
779 list_for_each_entry_safe(port
, tmp
, &team
->port_list
, list
)
780 team_port_del(team
, port
->dev
);
782 __team_change_mode(team
, NULL
); /* cleanup */
783 __team_options_unregister(team
, team_options
, ARRAY_SIZE(team_options
));
784 mutex_unlock(&team
->lock
);
787 static void team_destructor(struct net_device
*dev
)
789 struct team
*team
= netdev_priv(dev
);
791 free_percpu(team
->pcpu_stats
);
795 static int team_open(struct net_device
*dev
)
797 netif_carrier_on(dev
);
801 static int team_close(struct net_device
*dev
)
803 netif_carrier_off(dev
);
808 * note: already called with rcu_read_lock
810 static netdev_tx_t
team_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
812 struct team
*team
= netdev_priv(dev
);
813 bool tx_success
= false;
814 unsigned int len
= skb
->len
;
816 tx_success
= team
->ops
.transmit(team
, skb
);
818 struct team_pcpu_stats
*pcpu_stats
;
820 pcpu_stats
= this_cpu_ptr(team
->pcpu_stats
);
821 u64_stats_update_begin(&pcpu_stats
->syncp
);
822 pcpu_stats
->tx_packets
++;
823 pcpu_stats
->tx_bytes
+= len
;
824 u64_stats_update_end(&pcpu_stats
->syncp
);
826 this_cpu_inc(team
->pcpu_stats
->tx_dropped
);
832 static void team_change_rx_flags(struct net_device
*dev
, int change
)
834 struct team
*team
= netdev_priv(dev
);
835 struct team_port
*port
;
839 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
840 if (change
& IFF_PROMISC
) {
841 inc
= dev
->flags
& IFF_PROMISC
? 1 : -1;
842 dev_set_promiscuity(port
->dev
, inc
);
844 if (change
& IFF_ALLMULTI
) {
845 inc
= dev
->flags
& IFF_ALLMULTI
? 1 : -1;
846 dev_set_allmulti(port
->dev
, inc
);
852 static void team_set_rx_mode(struct net_device
*dev
)
854 struct team
*team
= netdev_priv(dev
);
855 struct team_port
*port
;
858 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
859 dev_uc_sync(port
->dev
, dev
);
860 dev_mc_sync(port
->dev
, dev
);
865 static int team_set_mac_address(struct net_device
*dev
, void *p
)
867 struct team
*team
= netdev_priv(dev
);
868 struct team_port
*port
;
869 struct sockaddr
*addr
= p
;
871 dev
->addr_assign_type
&= ~NET_ADDR_RANDOM
;
872 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
874 list_for_each_entry_rcu(port
, &team
->port_list
, list
)
875 if (team
->ops
.port_change_mac
)
876 team
->ops
.port_change_mac(team
, port
);
881 static int team_change_mtu(struct net_device
*dev
, int new_mtu
)
883 struct team
*team
= netdev_priv(dev
);
884 struct team_port
*port
;
888 * Alhough this is reader, it's guarded by team lock. It's not possible
889 * to traverse list in reverse under rcu_read_lock
891 mutex_lock(&team
->lock
);
892 team
->port_mtu_change_allowed
= true;
893 list_for_each_entry(port
, &team
->port_list
, list
) {
894 err
= dev_set_mtu(port
->dev
, new_mtu
);
896 netdev_err(dev
, "Device %s failed to change mtu",
901 team
->port_mtu_change_allowed
= false;
902 mutex_unlock(&team
->lock
);
909 list_for_each_entry_continue_reverse(port
, &team
->port_list
, list
)
910 dev_set_mtu(port
->dev
, dev
->mtu
);
911 team
->port_mtu_change_allowed
= false;
912 mutex_unlock(&team
->lock
);
917 static struct rtnl_link_stats64
*
918 team_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
920 struct team
*team
= netdev_priv(dev
);
921 struct team_pcpu_stats
*p
;
922 u64 rx_packets
, rx_bytes
, rx_multicast
, tx_packets
, tx_bytes
;
923 u32 rx_dropped
= 0, tx_dropped
= 0;
927 for_each_possible_cpu(i
) {
928 p
= per_cpu_ptr(team
->pcpu_stats
, i
);
930 start
= u64_stats_fetch_begin_bh(&p
->syncp
);
931 rx_packets
= p
->rx_packets
;
932 rx_bytes
= p
->rx_bytes
;
933 rx_multicast
= p
->rx_multicast
;
934 tx_packets
= p
->tx_packets
;
935 tx_bytes
= p
->tx_bytes
;
936 } while (u64_stats_fetch_retry_bh(&p
->syncp
, start
));
938 stats
->rx_packets
+= rx_packets
;
939 stats
->rx_bytes
+= rx_bytes
;
940 stats
->multicast
+= rx_multicast
;
941 stats
->tx_packets
+= tx_packets
;
942 stats
->tx_bytes
+= tx_bytes
;
944 * rx_dropped & tx_dropped are u32, updated
945 * without syncp protection.
947 rx_dropped
+= p
->rx_dropped
;
948 tx_dropped
+= p
->tx_dropped
;
950 stats
->rx_dropped
= rx_dropped
;
951 stats
->tx_dropped
= tx_dropped
;
955 static int team_vlan_rx_add_vid(struct net_device
*dev
, uint16_t vid
)
957 struct team
*team
= netdev_priv(dev
);
958 struct team_port
*port
;
962 * Alhough this is reader, it's guarded by team lock. It's not possible
963 * to traverse list in reverse under rcu_read_lock
965 mutex_lock(&team
->lock
);
966 list_for_each_entry(port
, &team
->port_list
, list
) {
967 err
= vlan_vid_add(port
->dev
, vid
);
971 mutex_unlock(&team
->lock
);
976 list_for_each_entry_continue_reverse(port
, &team
->port_list
, list
)
977 vlan_vid_del(port
->dev
, vid
);
978 mutex_unlock(&team
->lock
);
983 static int team_vlan_rx_kill_vid(struct net_device
*dev
, uint16_t vid
)
985 struct team
*team
= netdev_priv(dev
);
986 struct team_port
*port
;
989 list_for_each_entry_rcu(port
, &team
->port_list
, list
)
990 vlan_vid_del(port
->dev
, vid
);
996 static int team_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
998 struct team
*team
= netdev_priv(dev
);
1001 mutex_lock(&team
->lock
);
1002 err
= team_port_add(team
, port_dev
);
1003 mutex_unlock(&team
->lock
);
1007 static int team_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
1009 struct team
*team
= netdev_priv(dev
);
1012 mutex_lock(&team
->lock
);
1013 err
= team_port_del(team
, port_dev
);
1014 mutex_unlock(&team
->lock
);
1018 static netdev_features_t
team_fix_features(struct net_device
*dev
,
1019 netdev_features_t features
)
1021 struct team_port
*port
;
1022 struct team
*team
= netdev_priv(dev
);
1023 netdev_features_t mask
;
1026 features
&= ~NETIF_F_ONE_FOR_ALL
;
1027 features
|= NETIF_F_ALL_FOR_ALL
;
1030 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
1031 features
= netdev_increment_features(features
,
1032 port
->dev
->features
,
1039 static const struct net_device_ops team_netdev_ops
= {
1040 .ndo_init
= team_init
,
1041 .ndo_uninit
= team_uninit
,
1042 .ndo_open
= team_open
,
1043 .ndo_stop
= team_close
,
1044 .ndo_start_xmit
= team_xmit
,
1045 .ndo_change_rx_flags
= team_change_rx_flags
,
1046 .ndo_set_rx_mode
= team_set_rx_mode
,
1047 .ndo_set_mac_address
= team_set_mac_address
,
1048 .ndo_change_mtu
= team_change_mtu
,
1049 .ndo_get_stats64
= team_get_stats64
,
1050 .ndo_vlan_rx_add_vid
= team_vlan_rx_add_vid
,
1051 .ndo_vlan_rx_kill_vid
= team_vlan_rx_kill_vid
,
1052 .ndo_add_slave
= team_add_slave
,
1053 .ndo_del_slave
= team_del_slave
,
1054 .ndo_fix_features
= team_fix_features
,
1058 /***********************
1059 * rt netlink interface
1060 ***********************/
1062 static void team_setup(struct net_device
*dev
)
1066 dev
->netdev_ops
= &team_netdev_ops
;
1067 dev
->destructor
= team_destructor
;
1068 dev
->tx_queue_len
= 0;
1069 dev
->flags
|= IFF_MULTICAST
;
1070 dev
->priv_flags
&= ~(IFF_XMIT_DST_RELEASE
| IFF_TX_SKB_SHARING
);
1073 * Indicate we support unicast address filtering. That way core won't
1074 * bring us to promisc mode in case a unicast addr is added.
1075 * Let this up to underlay drivers.
1077 dev
->priv_flags
|= IFF_UNICAST_FLT
;
1079 dev
->features
|= NETIF_F_LLTX
;
1080 dev
->features
|= NETIF_F_GRO
;
1081 dev
->hw_features
= NETIF_F_HW_VLAN_TX
|
1082 NETIF_F_HW_VLAN_RX
|
1083 NETIF_F_HW_VLAN_FILTER
;
1085 dev
->features
|= dev
->hw_features
;
1088 static int team_newlink(struct net
*src_net
, struct net_device
*dev
,
1089 struct nlattr
*tb
[], struct nlattr
*data
[])
1093 if (tb
[IFLA_ADDRESS
] == NULL
)
1094 eth_hw_addr_random(dev
);
1096 err
= register_netdevice(dev
);
1103 static int team_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1105 if (tb
[IFLA_ADDRESS
]) {
1106 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1108 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1109 return -EADDRNOTAVAIL
;
1114 static struct rtnl_link_ops team_link_ops __read_mostly
= {
1116 .priv_size
= sizeof(struct team
),
1117 .setup
= team_setup
,
1118 .newlink
= team_newlink
,
1119 .validate
= team_validate
,
1123 /***********************************
1124 * Generic netlink custom interface
1125 ***********************************/
1127 static struct genl_family team_nl_family
= {
1128 .id
= GENL_ID_GENERATE
,
1129 .name
= TEAM_GENL_NAME
,
1130 .version
= TEAM_GENL_VERSION
,
1131 .maxattr
= TEAM_ATTR_MAX
,
1135 static const struct nla_policy team_nl_policy
[TEAM_ATTR_MAX
+ 1] = {
1136 [TEAM_ATTR_UNSPEC
] = { .type
= NLA_UNSPEC
, },
1137 [TEAM_ATTR_TEAM_IFINDEX
] = { .type
= NLA_U32
},
1138 [TEAM_ATTR_LIST_OPTION
] = { .type
= NLA_NESTED
},
1139 [TEAM_ATTR_LIST_PORT
] = { .type
= NLA_NESTED
},
1142 static const struct nla_policy
1143 team_nl_option_policy
[TEAM_ATTR_OPTION_MAX
+ 1] = {
1144 [TEAM_ATTR_OPTION_UNSPEC
] = { .type
= NLA_UNSPEC
, },
1145 [TEAM_ATTR_OPTION_NAME
] = {
1147 .len
= TEAM_STRING_MAX_LEN
,
1149 [TEAM_ATTR_OPTION_CHANGED
] = { .type
= NLA_FLAG
},
1150 [TEAM_ATTR_OPTION_TYPE
] = { .type
= NLA_U8
},
1151 [TEAM_ATTR_OPTION_DATA
] = {
1153 .len
= TEAM_STRING_MAX_LEN
,
1157 static int team_nl_cmd_noop(struct sk_buff
*skb
, struct genl_info
*info
)
1159 struct sk_buff
*msg
;
1163 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1167 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
,
1168 &team_nl_family
, 0, TEAM_CMD_NOOP
);
1174 genlmsg_end(msg
, hdr
);
1176 return genlmsg_unicast(genl_info_net(info
), msg
, info
->snd_pid
);
1185 * Netlink cmd functions should be locked by following two functions.
1186 * Since dev gets held here, that ensures dev won't disappear in between.
1188 static struct team
*team_nl_team_get(struct genl_info
*info
)
1190 struct net
*net
= genl_info_net(info
);
1192 struct net_device
*dev
;
1195 if (!info
->attrs
[TEAM_ATTR_TEAM_IFINDEX
])
1198 ifindex
= nla_get_u32(info
->attrs
[TEAM_ATTR_TEAM_IFINDEX
]);
1199 dev
= dev_get_by_index(net
, ifindex
);
1200 if (!dev
|| dev
->netdev_ops
!= &team_netdev_ops
) {
1206 team
= netdev_priv(dev
);
1207 mutex_lock(&team
->lock
);
1211 static void team_nl_team_put(struct team
*team
)
1213 mutex_unlock(&team
->lock
);
1217 static int team_nl_send_generic(struct genl_info
*info
, struct team
*team
,
1218 int (*fill_func
)(struct sk_buff
*skb
,
1219 struct genl_info
*info
,
1220 int flags
, struct team
*team
))
1222 struct sk_buff
*skb
;
1225 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1229 err
= fill_func(skb
, info
, NLM_F_ACK
, team
);
1233 err
= genlmsg_unicast(genl_info_net(info
), skb
, info
->snd_pid
);
1241 static int team_nl_fill_options_get(struct sk_buff
*skb
,
1242 u32 pid
, u32 seq
, int flags
,
1243 struct team
*team
, bool fillall
)
1245 struct nlattr
*option_list
;
1247 struct team_option
*option
;
1249 hdr
= genlmsg_put(skb
, pid
, seq
, &team_nl_family
, flags
,
1250 TEAM_CMD_OPTIONS_GET
);
1252 return PTR_ERR(hdr
);
1254 NLA_PUT_U32(skb
, TEAM_ATTR_TEAM_IFINDEX
, team
->dev
->ifindex
);
1255 option_list
= nla_nest_start(skb
, TEAM_ATTR_LIST_OPTION
);
1259 list_for_each_entry(option
, &team
->option_list
, list
) {
1260 struct nlattr
*option_item
;
1263 /* Include only changed options if fill all mode is not on */
1264 if (!fillall
&& !option
->changed
)
1266 option_item
= nla_nest_start(skb
, TEAM_ATTR_ITEM_OPTION
);
1268 goto nla_put_failure
;
1269 NLA_PUT_STRING(skb
, TEAM_ATTR_OPTION_NAME
, option
->name
);
1270 if (option
->changed
) {
1271 NLA_PUT_FLAG(skb
, TEAM_ATTR_OPTION_CHANGED
);
1272 option
->changed
= false;
1274 if (option
->removed
)
1275 NLA_PUT_FLAG(skb
, TEAM_ATTR_OPTION_REMOVED
);
1276 switch (option
->type
) {
1277 case TEAM_OPTION_TYPE_U32
:
1278 NLA_PUT_U8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_U32
);
1279 team_option_get(team
, option
, &arg
);
1280 NLA_PUT_U32(skb
, TEAM_ATTR_OPTION_DATA
, arg
);
1282 case TEAM_OPTION_TYPE_STRING
:
1283 NLA_PUT_U8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_STRING
);
1284 team_option_get(team
, option
, &arg
);
1285 NLA_PUT_STRING(skb
, TEAM_ATTR_OPTION_DATA
,
1291 nla_nest_end(skb
, option_item
);
1294 nla_nest_end(skb
, option_list
);
1295 return genlmsg_end(skb
, hdr
);
1298 genlmsg_cancel(skb
, hdr
);
1302 static int team_nl_fill_options_get_all(struct sk_buff
*skb
,
1303 struct genl_info
*info
, int flags
,
1306 return team_nl_fill_options_get(skb
, info
->snd_pid
,
1307 info
->snd_seq
, NLM_F_ACK
,
1311 static int team_nl_cmd_options_get(struct sk_buff
*skb
, struct genl_info
*info
)
1316 team
= team_nl_team_get(info
);
1320 err
= team_nl_send_generic(info
, team
, team_nl_fill_options_get_all
);
1322 team_nl_team_put(team
);
1327 static int team_nl_cmd_options_set(struct sk_buff
*skb
, struct genl_info
*info
)
1332 struct nlattr
*nl_option
;
1334 team
= team_nl_team_get(info
);
1339 if (!info
->attrs
[TEAM_ATTR_LIST_OPTION
]) {
1344 nla_for_each_nested(nl_option
, info
->attrs
[TEAM_ATTR_LIST_OPTION
], i
) {
1345 struct nlattr
*mode_attrs
[TEAM_ATTR_OPTION_MAX
+ 1];
1346 enum team_option_type opt_type
;
1347 struct team_option
*option
;
1349 bool opt_found
= false;
1351 if (nla_type(nl_option
) != TEAM_ATTR_ITEM_OPTION
) {
1355 err
= nla_parse_nested(mode_attrs
, TEAM_ATTR_OPTION_MAX
,
1356 nl_option
, team_nl_option_policy
);
1359 if (!mode_attrs
[TEAM_ATTR_OPTION_NAME
] ||
1360 !mode_attrs
[TEAM_ATTR_OPTION_TYPE
] ||
1361 !mode_attrs
[TEAM_ATTR_OPTION_DATA
]) {
1365 switch (nla_get_u8(mode_attrs
[TEAM_ATTR_OPTION_TYPE
])) {
1367 opt_type
= TEAM_OPTION_TYPE_U32
;
1370 opt_type
= TEAM_OPTION_TYPE_STRING
;
1376 opt_name
= nla_data(mode_attrs
[TEAM_ATTR_OPTION_NAME
]);
1377 list_for_each_entry(option
, &team
->option_list
, list
) {
1379 struct nlattr
*opt_data_attr
;
1381 if (option
->type
!= opt_type
||
1382 strcmp(option
->name
, opt_name
))
1385 opt_data_attr
= mode_attrs
[TEAM_ATTR_OPTION_DATA
];
1387 case TEAM_OPTION_TYPE_U32
:
1388 arg
= nla_get_u32(opt_data_attr
);
1390 case TEAM_OPTION_TYPE_STRING
:
1391 arg
= (long) nla_data(opt_data_attr
);
1396 err
= team_option_set(team
, option
, &arg
);
1407 team_nl_team_put(team
);
1412 static int team_nl_fill_port_list_get(struct sk_buff
*skb
,
1413 u32 pid
, u32 seq
, int flags
,
1417 struct nlattr
*port_list
;
1419 struct team_port
*port
;
1421 hdr
= genlmsg_put(skb
, pid
, seq
, &team_nl_family
, flags
,
1422 TEAM_CMD_PORT_LIST_GET
);
1424 return PTR_ERR(hdr
);
1426 NLA_PUT_U32(skb
, TEAM_ATTR_TEAM_IFINDEX
, team
->dev
->ifindex
);
1427 port_list
= nla_nest_start(skb
, TEAM_ATTR_LIST_PORT
);
1431 list_for_each_entry(port
, &team
->port_list
, list
) {
1432 struct nlattr
*port_item
;
1434 /* Include only changed ports if fill all mode is not on */
1435 if (!fillall
&& !port
->changed
)
1437 port_item
= nla_nest_start(skb
, TEAM_ATTR_ITEM_PORT
);
1439 goto nla_put_failure
;
1440 NLA_PUT_U32(skb
, TEAM_ATTR_PORT_IFINDEX
, port
->dev
->ifindex
);
1441 if (port
->changed
) {
1442 NLA_PUT_FLAG(skb
, TEAM_ATTR_PORT_CHANGED
);
1443 port
->changed
= false;
1446 NLA_PUT_FLAG(skb
, TEAM_ATTR_PORT_REMOVED
);
1448 NLA_PUT_FLAG(skb
, TEAM_ATTR_PORT_LINKUP
);
1449 NLA_PUT_U32(skb
, TEAM_ATTR_PORT_SPEED
, port
->speed
);
1450 NLA_PUT_U8(skb
, TEAM_ATTR_PORT_DUPLEX
, port
->duplex
);
1451 nla_nest_end(skb
, port_item
);
1454 nla_nest_end(skb
, port_list
);
1455 return genlmsg_end(skb
, hdr
);
1458 genlmsg_cancel(skb
, hdr
);
1462 static int team_nl_fill_port_list_get_all(struct sk_buff
*skb
,
1463 struct genl_info
*info
, int flags
,
1466 return team_nl_fill_port_list_get(skb
, info
->snd_pid
,
1467 info
->snd_seq
, NLM_F_ACK
,
1471 static int team_nl_cmd_port_list_get(struct sk_buff
*skb
,
1472 struct genl_info
*info
)
1477 team
= team_nl_team_get(info
);
1481 err
= team_nl_send_generic(info
, team
, team_nl_fill_port_list_get_all
);
1483 team_nl_team_put(team
);
1488 static struct genl_ops team_nl_ops
[] = {
1490 .cmd
= TEAM_CMD_NOOP
,
1491 .doit
= team_nl_cmd_noop
,
1492 .policy
= team_nl_policy
,
1495 .cmd
= TEAM_CMD_OPTIONS_SET
,
1496 .doit
= team_nl_cmd_options_set
,
1497 .policy
= team_nl_policy
,
1498 .flags
= GENL_ADMIN_PERM
,
1501 .cmd
= TEAM_CMD_OPTIONS_GET
,
1502 .doit
= team_nl_cmd_options_get
,
1503 .policy
= team_nl_policy
,
1504 .flags
= GENL_ADMIN_PERM
,
1507 .cmd
= TEAM_CMD_PORT_LIST_GET
,
1508 .doit
= team_nl_cmd_port_list_get
,
1509 .policy
= team_nl_policy
,
1510 .flags
= GENL_ADMIN_PERM
,
1514 static struct genl_multicast_group team_change_event_mcgrp
= {
1515 .name
= TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME
,
1518 static int team_nl_send_event_options_get(struct team
*team
)
1520 struct sk_buff
*skb
;
1522 struct net
*net
= dev_net(team
->dev
);
1524 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1528 err
= team_nl_fill_options_get(skb
, 0, 0, 0, team
, false);
1532 err
= genlmsg_multicast_netns(net
, skb
, 0, team_change_event_mcgrp
.id
,
1541 static int team_nl_send_event_port_list_get(struct team
*team
)
1543 struct sk_buff
*skb
;
1545 struct net
*net
= dev_net(team
->dev
);
1547 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1551 err
= team_nl_fill_port_list_get(skb
, 0, 0, 0, team
, false);
1555 err
= genlmsg_multicast_netns(net
, skb
, 0, team_change_event_mcgrp
.id
,
1564 static int team_nl_init(void)
1568 err
= genl_register_family_with_ops(&team_nl_family
, team_nl_ops
,
1569 ARRAY_SIZE(team_nl_ops
));
1573 err
= genl_register_mc_group(&team_nl_family
, &team_change_event_mcgrp
);
1575 goto err_change_event_grp_reg
;
1579 err_change_event_grp_reg
:
1580 genl_unregister_family(&team_nl_family
);
1585 static void team_nl_fini(void)
1587 genl_unregister_family(&team_nl_family
);
1595 static void __team_options_change_check(struct team
*team
)
1599 err
= team_nl_send_event_options_get(team
);
1601 netdev_warn(team
->dev
, "Failed to send options change via netlink\n");
1604 /* rtnl lock is held */
1605 static void __team_port_change_check(struct team_port
*port
, bool linkup
)
1609 if (!port
->removed
&& port
->linkup
== linkup
)
1612 port
->changed
= true;
1613 port
->linkup
= linkup
;
1615 struct ethtool_cmd ecmd
;
1617 err
= __ethtool_get_settings(port
->dev
, &ecmd
);
1619 port
->speed
= ethtool_cmd_speed(&ecmd
);
1620 port
->duplex
= ecmd
.duplex
;
1628 err
= team_nl_send_event_port_list_get(port
->team
);
1630 netdev_warn(port
->team
->dev
, "Failed to send port change of device %s via netlink\n",
1635 static void team_port_change_check(struct team_port
*port
, bool linkup
)
1637 struct team
*team
= port
->team
;
1639 mutex_lock(&team
->lock
);
1640 __team_port_change_check(port
, linkup
);
1641 mutex_unlock(&team
->lock
);
1644 /************************************
1645 * Net device notifier event handler
1646 ************************************/
1648 static int team_device_event(struct notifier_block
*unused
,
1649 unsigned long event
, void *ptr
)
1651 struct net_device
*dev
= (struct net_device
*) ptr
;
1652 struct team_port
*port
;
1654 port
= team_port_get_rtnl(dev
);
1660 if (netif_carrier_ok(dev
))
1661 team_port_change_check(port
, true);
1663 team_port_change_check(port
, false);
1665 if (netif_running(port
->dev
))
1666 team_port_change_check(port
,
1667 !!netif_carrier_ok(port
->dev
));
1669 case NETDEV_UNREGISTER
:
1670 team_del_slave(port
->team
->dev
, dev
);
1672 case NETDEV_FEAT_CHANGE
:
1673 team_compute_features(port
->team
);
1675 case NETDEV_CHANGEMTU
:
1676 /* Forbid to change mtu of underlaying device */
1677 if (!port
->team
->port_mtu_change_allowed
)
1680 case NETDEV_PRE_TYPE_CHANGE
:
1681 /* Forbid to change type of underlaying device */
1687 static struct notifier_block team_notifier_block __read_mostly
= {
1688 .notifier_call
= team_device_event
,
1692 /***********************
1693 * Module init and exit
1694 ***********************/
1696 static int __init
team_module_init(void)
1700 register_netdevice_notifier(&team_notifier_block
);
1702 err
= rtnl_link_register(&team_link_ops
);
1706 err
= team_nl_init();
1713 rtnl_link_unregister(&team_link_ops
);
1716 unregister_netdevice_notifier(&team_notifier_block
);
1721 static void __exit
team_module_exit(void)
1724 rtnl_link_unregister(&team_link_ops
);
1725 unregister_netdevice_notifier(&team_notifier_block
);
1728 module_init(team_module_init
);
1729 module_exit(team_module_exit
);
1731 MODULE_LICENSE("GPL v2");
1732 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
1733 MODULE_DESCRIPTION("Ethernet team device driver");
1734 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);