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
;
102 dst_opts
= kzalloc(sizeof(struct team_option
*) * option_count
,
106 for (i
= 0; i
< option_count
; i
++, option
++) {
107 if (__team_find_option(team
, option
->name
)) {
111 dst_opts
[i
] = kmemdup(option
, sizeof(*option
), GFP_KERNEL
);
118 for (i
= 0; i
< option_count
; i
++)
119 list_add_tail(&dst_opts
[i
]->list
, &team
->option_list
);
125 for (i
= 0; i
< option_count
; i
++)
132 EXPORT_SYMBOL(team_options_register
);
134 static void __team_options_change_check(struct team
*team
,
135 struct team_option
*changed_option
);
137 static void __team_options_unregister(struct team
*team
,
138 const struct team_option
*option
,
143 for (i
= 0; i
< option_count
; i
++, option
++) {
144 struct team_option
*del_opt
;
146 del_opt
= __team_find_option(team
, option
->name
);
148 list_del(&del_opt
->list
);
154 void team_options_unregister(struct team
*team
,
155 const struct team_option
*option
,
158 __team_options_unregister(team
, option
, option_count
);
159 __team_options_change_check(team
, NULL
);
161 EXPORT_SYMBOL(team_options_unregister
);
163 static int team_option_get(struct team
*team
, struct team_option
*option
,
166 return option
->getter(team
, arg
);
169 static int team_option_set(struct team
*team
, struct team_option
*option
,
174 err
= option
->setter(team
, arg
);
178 __team_options_change_check(team
, option
);
186 static LIST_HEAD(mode_list
);
187 static DEFINE_SPINLOCK(mode_list_lock
);
189 static struct team_mode
*__find_mode(const char *kind
)
191 struct team_mode
*mode
;
193 list_for_each_entry(mode
, &mode_list
, list
) {
194 if (strcmp(mode
->kind
, kind
) == 0)
200 static bool is_good_mode_name(const char *name
)
202 while (*name
!= '\0') {
203 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
210 int team_mode_register(struct team_mode
*mode
)
214 if (!is_good_mode_name(mode
->kind
) ||
215 mode
->priv_size
> TEAM_MODE_PRIV_SIZE
)
217 spin_lock(&mode_list_lock
);
218 if (__find_mode(mode
->kind
)) {
222 list_add_tail(&mode
->list
, &mode_list
);
224 spin_unlock(&mode_list_lock
);
227 EXPORT_SYMBOL(team_mode_register
);
229 int team_mode_unregister(struct team_mode
*mode
)
231 spin_lock(&mode_list_lock
);
232 list_del_init(&mode
->list
);
233 spin_unlock(&mode_list_lock
);
236 EXPORT_SYMBOL(team_mode_unregister
);
238 static struct team_mode
*team_mode_get(const char *kind
)
240 struct team_mode
*mode
;
242 spin_lock(&mode_list_lock
);
243 mode
= __find_mode(kind
);
245 spin_unlock(&mode_list_lock
);
246 request_module("team-mode-%s", kind
);
247 spin_lock(&mode_list_lock
);
248 mode
= __find_mode(kind
);
251 if (!try_module_get(mode
->owner
))
254 spin_unlock(&mode_list_lock
);
258 static void team_mode_put(const struct team_mode
*mode
)
260 module_put(mode
->owner
);
263 static bool team_dummy_transmit(struct team
*team
, struct sk_buff
*skb
)
265 dev_kfree_skb_any(skb
);
269 rx_handler_result_t
team_dummy_receive(struct team
*team
,
270 struct team_port
*port
,
273 return RX_HANDLER_ANOTHER
;
276 static void team_adjust_ops(struct team
*team
)
279 * To avoid checks in rx/tx skb paths, ensure here that non-null and
280 * correct ops are always set.
283 if (list_empty(&team
->port_list
) ||
284 !team
->mode
|| !team
->mode
->ops
->transmit
)
285 team
->ops
.transmit
= team_dummy_transmit
;
287 team
->ops
.transmit
= team
->mode
->ops
->transmit
;
289 if (list_empty(&team
->port_list
) ||
290 !team
->mode
|| !team
->mode
->ops
->receive
)
291 team
->ops
.receive
= team_dummy_receive
;
293 team
->ops
.receive
= team
->mode
->ops
->receive
;
297 * We can benefit from the fact that it's ensured no port is present
298 * at the time of mode change. Therefore no packets are in fly so there's no
299 * need to set mode operations in any special way.
301 static int __team_change_mode(struct team
*team
,
302 const struct team_mode
*new_mode
)
304 /* Check if mode was previously set and do cleanup if so */
306 void (*exit_op
)(struct team
*team
) = team
->ops
.exit
;
308 /* Clear ops area so no callback is called any longer */
309 memset(&team
->ops
, 0, sizeof(struct team_mode_ops
));
310 team_adjust_ops(team
);
314 team_mode_put(team
->mode
);
316 /* zero private data area */
317 memset(&team
->mode_priv
, 0,
318 sizeof(struct team
) - offsetof(struct team
, mode_priv
));
324 if (new_mode
->ops
->init
) {
327 err
= new_mode
->ops
->init(team
);
332 team
->mode
= new_mode
;
333 memcpy(&team
->ops
, new_mode
->ops
, sizeof(struct team_mode_ops
));
334 team_adjust_ops(team
);
339 static int team_change_mode(struct team
*team
, const char *kind
)
341 struct team_mode
*new_mode
;
342 struct net_device
*dev
= team
->dev
;
345 if (!list_empty(&team
->port_list
)) {
346 netdev_err(dev
, "No ports can be present during mode change\n");
350 if (team
->mode
&& strcmp(team
->mode
->kind
, kind
) == 0) {
351 netdev_err(dev
, "Unable to change to the same mode the team is in\n");
355 new_mode
= team_mode_get(kind
);
357 netdev_err(dev
, "Mode \"%s\" not found\n", kind
);
361 err
= __team_change_mode(team
, new_mode
);
363 netdev_err(dev
, "Failed to change to mode \"%s\"\n", kind
);
364 team_mode_put(new_mode
);
368 netdev_info(dev
, "Mode changed to \"%s\"\n", kind
);
373 /************************
374 * Rx path frame handler
375 ************************/
377 /* note: already called with rcu_read_lock */
378 static rx_handler_result_t
team_handle_frame(struct sk_buff
**pskb
)
380 struct sk_buff
*skb
= *pskb
;
381 struct team_port
*port
;
383 rx_handler_result_t res
;
385 skb
= skb_share_check(skb
, GFP_ATOMIC
);
387 return RX_HANDLER_CONSUMED
;
391 port
= team_port_get_rcu(skb
->dev
);
394 res
= team
->ops
.receive(team
, port
, skb
);
395 if (res
== RX_HANDLER_ANOTHER
) {
396 struct team_pcpu_stats
*pcpu_stats
;
398 pcpu_stats
= this_cpu_ptr(team
->pcpu_stats
);
399 u64_stats_update_begin(&pcpu_stats
->syncp
);
400 pcpu_stats
->rx_packets
++;
401 pcpu_stats
->rx_bytes
+= skb
->len
;
402 if (skb
->pkt_type
== PACKET_MULTICAST
)
403 pcpu_stats
->rx_multicast
++;
404 u64_stats_update_end(&pcpu_stats
->syncp
);
406 skb
->dev
= team
->dev
;
408 this_cpu_inc(team
->pcpu_stats
->rx_dropped
);
419 static bool team_port_find(const struct team
*team
,
420 const struct team_port
*port
)
422 struct team_port
*cur
;
424 list_for_each_entry(cur
, &team
->port_list
, list
)
431 * Add/delete port to the team port list. Write guarded by rtnl_lock.
432 * Takes care of correct port->index setup (might be racy).
434 static void team_port_list_add_port(struct team
*team
,
435 struct team_port
*port
)
437 port
->index
= team
->port_count
++;
438 hlist_add_head_rcu(&port
->hlist
,
439 team_port_index_hash(team
, port
->index
));
440 list_add_tail_rcu(&port
->list
, &team
->port_list
);
443 static void __reconstruct_port_hlist(struct team
*team
, int rm_index
)
446 struct team_port
*port
;
448 for (i
= rm_index
+ 1; i
< team
->port_count
; i
++) {
449 port
= team_get_port_by_index(team
, i
);
450 hlist_del_rcu(&port
->hlist
);
452 hlist_add_head_rcu(&port
->hlist
,
453 team_port_index_hash(team
, port
->index
));
457 static void team_port_list_del_port(struct team
*team
,
458 struct team_port
*port
)
460 int rm_index
= port
->index
;
462 hlist_del_rcu(&port
->hlist
);
463 list_del_rcu(&port
->list
);
464 __reconstruct_port_hlist(team
, rm_index
);
468 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
469 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
470 NETIF_F_HIGHDMA | NETIF_F_LRO)
472 static void __team_compute_features(struct team
*team
)
474 struct team_port
*port
;
475 u32 vlan_features
= TEAM_VLAN_FEATURES
;
476 unsigned short max_hard_header_len
= ETH_HLEN
;
478 list_for_each_entry(port
, &team
->port_list
, list
) {
479 vlan_features
= netdev_increment_features(vlan_features
,
480 port
->dev
->vlan_features
,
483 if (port
->dev
->hard_header_len
> max_hard_header_len
)
484 max_hard_header_len
= port
->dev
->hard_header_len
;
487 team
->dev
->vlan_features
= vlan_features
;
488 team
->dev
->hard_header_len
= max_hard_header_len
;
490 netdev_change_features(team
->dev
);
493 static void team_compute_features(struct team
*team
)
495 mutex_lock(&team
->lock
);
496 __team_compute_features(team
);
497 mutex_unlock(&team
->lock
);
500 static int team_port_enter(struct team
*team
, struct team_port
*port
)
505 port
->dev
->priv_flags
|= IFF_TEAM_PORT
;
506 if (team
->ops
.port_enter
) {
507 err
= team
->ops
.port_enter(team
, port
);
509 netdev_err(team
->dev
, "Device %s failed to enter team mode\n",
518 port
->dev
->priv_flags
&= ~IFF_TEAM_PORT
;
524 static void team_port_leave(struct team
*team
, struct team_port
*port
)
526 if (team
->ops
.port_leave
)
527 team
->ops
.port_leave(team
, port
);
528 port
->dev
->priv_flags
&= ~IFF_TEAM_PORT
;
532 static void __team_port_change_check(struct team_port
*port
, bool linkup
);
534 static int team_port_add(struct team
*team
, struct net_device
*port_dev
)
536 struct net_device
*dev
= team
->dev
;
537 struct team_port
*port
;
538 char *portname
= port_dev
->name
;
541 if (port_dev
->flags
& IFF_LOOPBACK
||
542 port_dev
->type
!= ARPHRD_ETHER
) {
543 netdev_err(dev
, "Device %s is of an unsupported type\n",
548 if (team_port_exists(port_dev
)) {
549 netdev_err(dev
, "Device %s is already a port "
550 "of a team device\n", portname
);
554 if (port_dev
->flags
& IFF_UP
) {
555 netdev_err(dev
, "Device %s is up. Set it down before adding it as a team port\n",
560 port
= kzalloc(sizeof(struct team_port
), GFP_KERNEL
);
564 port
->dev
= port_dev
;
567 port
->orig
.mtu
= port_dev
->mtu
;
568 err
= dev_set_mtu(port_dev
, dev
->mtu
);
570 netdev_dbg(dev
, "Error %d calling dev_set_mtu\n", err
);
574 memcpy(port
->orig
.dev_addr
, port_dev
->dev_addr
, ETH_ALEN
);
576 err
= team_port_enter(team
, port
);
578 netdev_err(dev
, "Device %s failed to enter team mode\n",
583 err
= dev_open(port_dev
);
585 netdev_dbg(dev
, "Device %s opening failed\n",
590 err
= netdev_set_master(port_dev
, dev
);
592 netdev_err(dev
, "Device %s failed to set master\n", portname
);
596 err
= netdev_rx_handler_register(port_dev
, team_handle_frame
,
599 netdev_err(dev
, "Device %s failed to register rx_handler\n",
601 goto err_handler_register
;
604 team_port_list_add_port(team
, port
);
605 team_adjust_ops(team
);
606 __team_compute_features(team
);
607 __team_port_change_check(port
, !!netif_carrier_ok(port_dev
));
609 netdev_info(dev
, "Port device %s added\n", portname
);
613 err_handler_register
:
614 netdev_set_master(port_dev
, NULL
);
620 team_port_leave(team
, port
);
621 team_port_set_orig_mac(port
);
624 dev_set_mtu(port_dev
, port
->orig
.mtu
);
632 static int team_port_del(struct team
*team
, struct net_device
*port_dev
)
634 struct net_device
*dev
= team
->dev
;
635 struct team_port
*port
;
636 char *portname
= port_dev
->name
;
638 port
= team_port_get_rtnl(port_dev
);
639 if (!port
|| !team_port_find(team
, port
)) {
640 netdev_err(dev
, "Device %s does not act as a port of this team\n",
645 __team_port_change_check(port
, false);
646 team_port_list_del_port(team
, port
);
647 team_adjust_ops(team
);
648 netdev_rx_handler_unregister(port_dev
);
649 netdev_set_master(port_dev
, NULL
);
651 team_port_leave(team
, port
);
652 team_port_set_orig_mac(port
);
653 dev_set_mtu(port_dev
, port
->orig
.mtu
);
656 netdev_info(dev
, "Port device %s removed\n", portname
);
657 __team_compute_features(team
);
667 static const char team_no_mode_kind
[] = "*NOMODE*";
669 static int team_mode_option_get(struct team
*team
, void *arg
)
671 const char **str
= arg
;
673 *str
= team
->mode
? team
->mode
->kind
: team_no_mode_kind
;
677 static int team_mode_option_set(struct team
*team
, void *arg
)
679 const char **str
= arg
;
681 return team_change_mode(team
, *str
);
684 static const struct team_option team_options
[] = {
687 .type
= TEAM_OPTION_TYPE_STRING
,
688 .getter
= team_mode_option_get
,
689 .setter
= team_mode_option_set
,
693 static int team_init(struct net_device
*dev
)
695 struct team
*team
= netdev_priv(dev
);
700 mutex_init(&team
->lock
);
702 team
->pcpu_stats
= alloc_percpu(struct team_pcpu_stats
);
703 if (!team
->pcpu_stats
)
706 for (i
= 0; i
< TEAM_PORT_HASHENTRIES
; i
++)
707 INIT_HLIST_HEAD(&team
->port_hlist
[i
]);
708 INIT_LIST_HEAD(&team
->port_list
);
710 team_adjust_ops(team
);
712 INIT_LIST_HEAD(&team
->option_list
);
713 err
= team_options_register(team
, team_options
, ARRAY_SIZE(team_options
));
715 goto err_options_register
;
716 netif_carrier_off(dev
);
720 err_options_register
:
721 free_percpu(team
->pcpu_stats
);
726 static void team_uninit(struct net_device
*dev
)
728 struct team
*team
= netdev_priv(dev
);
729 struct team_port
*port
;
730 struct team_port
*tmp
;
732 mutex_lock(&team
->lock
);
733 list_for_each_entry_safe(port
, tmp
, &team
->port_list
, list
)
734 team_port_del(team
, port
->dev
);
736 __team_change_mode(team
, NULL
); /* cleanup */
737 __team_options_unregister(team
, team_options
, ARRAY_SIZE(team_options
));
738 mutex_unlock(&team
->lock
);
741 static void team_destructor(struct net_device
*dev
)
743 struct team
*team
= netdev_priv(dev
);
745 free_percpu(team
->pcpu_stats
);
749 static int team_open(struct net_device
*dev
)
751 netif_carrier_on(dev
);
755 static int team_close(struct net_device
*dev
)
757 netif_carrier_off(dev
);
762 * note: already called with rcu_read_lock
764 static netdev_tx_t
team_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
766 struct team
*team
= netdev_priv(dev
);
767 bool tx_success
= false;
768 unsigned int len
= skb
->len
;
770 tx_success
= team
->ops
.transmit(team
, skb
);
772 struct team_pcpu_stats
*pcpu_stats
;
774 pcpu_stats
= this_cpu_ptr(team
->pcpu_stats
);
775 u64_stats_update_begin(&pcpu_stats
->syncp
);
776 pcpu_stats
->tx_packets
++;
777 pcpu_stats
->tx_bytes
+= len
;
778 u64_stats_update_end(&pcpu_stats
->syncp
);
780 this_cpu_inc(team
->pcpu_stats
->tx_dropped
);
786 static void team_change_rx_flags(struct net_device
*dev
, int change
)
788 struct team
*team
= netdev_priv(dev
);
789 struct team_port
*port
;
793 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
794 if (change
& IFF_PROMISC
) {
795 inc
= dev
->flags
& IFF_PROMISC
? 1 : -1;
796 dev_set_promiscuity(port
->dev
, inc
);
798 if (change
& IFF_ALLMULTI
) {
799 inc
= dev
->flags
& IFF_ALLMULTI
? 1 : -1;
800 dev_set_allmulti(port
->dev
, inc
);
806 static void team_set_rx_mode(struct net_device
*dev
)
808 struct team
*team
= netdev_priv(dev
);
809 struct team_port
*port
;
812 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
813 dev_uc_sync(port
->dev
, dev
);
814 dev_mc_sync(port
->dev
, dev
);
819 static int team_set_mac_address(struct net_device
*dev
, void *p
)
821 struct team
*team
= netdev_priv(dev
);
822 struct team_port
*port
;
823 struct sockaddr
*addr
= p
;
825 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
827 list_for_each_entry_rcu(port
, &team
->port_list
, list
)
828 if (team
->ops
.port_change_mac
)
829 team
->ops
.port_change_mac(team
, port
);
834 static int team_change_mtu(struct net_device
*dev
, int new_mtu
)
836 struct team
*team
= netdev_priv(dev
);
837 struct team_port
*port
;
841 * Alhough this is reader, it's guarded by team lock. It's not possible
842 * to traverse list in reverse under rcu_read_lock
844 mutex_lock(&team
->lock
);
845 list_for_each_entry(port
, &team
->port_list
, list
) {
846 err
= dev_set_mtu(port
->dev
, new_mtu
);
848 netdev_err(dev
, "Device %s failed to change mtu",
853 mutex_unlock(&team
->lock
);
860 list_for_each_entry_continue_reverse(port
, &team
->port_list
, list
)
861 dev_set_mtu(port
->dev
, dev
->mtu
);
862 mutex_unlock(&team
->lock
);
867 static struct rtnl_link_stats64
*
868 team_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
870 struct team
*team
= netdev_priv(dev
);
871 struct team_pcpu_stats
*p
;
872 u64 rx_packets
, rx_bytes
, rx_multicast
, tx_packets
, tx_bytes
;
873 u32 rx_dropped
= 0, tx_dropped
= 0;
877 for_each_possible_cpu(i
) {
878 p
= per_cpu_ptr(team
->pcpu_stats
, i
);
880 start
= u64_stats_fetch_begin_bh(&p
->syncp
);
881 rx_packets
= p
->rx_packets
;
882 rx_bytes
= p
->rx_bytes
;
883 rx_multicast
= p
->rx_multicast
;
884 tx_packets
= p
->tx_packets
;
885 tx_bytes
= p
->tx_bytes
;
886 } while (u64_stats_fetch_retry_bh(&p
->syncp
, start
));
888 stats
->rx_packets
+= rx_packets
;
889 stats
->rx_bytes
+= rx_bytes
;
890 stats
->multicast
+= rx_multicast
;
891 stats
->tx_packets
+= tx_packets
;
892 stats
->tx_bytes
+= tx_bytes
;
894 * rx_dropped & tx_dropped are u32, updated
895 * without syncp protection.
897 rx_dropped
+= p
->rx_dropped
;
898 tx_dropped
+= p
->tx_dropped
;
900 stats
->rx_dropped
= rx_dropped
;
901 stats
->tx_dropped
= tx_dropped
;
905 static void team_vlan_rx_add_vid(struct net_device
*dev
, uint16_t vid
)
907 struct team
*team
= netdev_priv(dev
);
908 struct team_port
*port
;
911 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
912 const struct net_device_ops
*ops
= port
->dev
->netdev_ops
;
914 if (ops
->ndo_vlan_rx_add_vid
)
915 ops
->ndo_vlan_rx_add_vid(port
->dev
, vid
);
920 static void team_vlan_rx_kill_vid(struct net_device
*dev
, uint16_t vid
)
922 struct team
*team
= netdev_priv(dev
);
923 struct team_port
*port
;
926 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
927 const struct net_device_ops
*ops
= port
->dev
->netdev_ops
;
929 if (ops
->ndo_vlan_rx_kill_vid
)
930 ops
->ndo_vlan_rx_kill_vid(port
->dev
, vid
);
935 static int team_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
937 struct team
*team
= netdev_priv(dev
);
940 mutex_lock(&team
->lock
);
941 err
= team_port_add(team
, port_dev
);
942 mutex_unlock(&team
->lock
);
946 static int team_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
948 struct team
*team
= netdev_priv(dev
);
951 mutex_lock(&team
->lock
);
952 err
= team_port_del(team
, port_dev
);
953 mutex_unlock(&team
->lock
);
957 static netdev_features_t
team_fix_features(struct net_device
*dev
,
958 netdev_features_t features
)
960 struct team_port
*port
;
961 struct team
*team
= netdev_priv(dev
);
962 netdev_features_t mask
;
965 features
&= ~NETIF_F_ONE_FOR_ALL
;
966 features
|= NETIF_F_ALL_FOR_ALL
;
969 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
970 features
= netdev_increment_features(features
,
978 static const struct net_device_ops team_netdev_ops
= {
979 .ndo_init
= team_init
,
980 .ndo_uninit
= team_uninit
,
981 .ndo_open
= team_open
,
982 .ndo_stop
= team_close
,
983 .ndo_start_xmit
= team_xmit
,
984 .ndo_change_rx_flags
= team_change_rx_flags
,
985 .ndo_set_rx_mode
= team_set_rx_mode
,
986 .ndo_set_mac_address
= team_set_mac_address
,
987 .ndo_change_mtu
= team_change_mtu
,
988 .ndo_get_stats64
= team_get_stats64
,
989 .ndo_vlan_rx_add_vid
= team_vlan_rx_add_vid
,
990 .ndo_vlan_rx_kill_vid
= team_vlan_rx_kill_vid
,
991 .ndo_add_slave
= team_add_slave
,
992 .ndo_del_slave
= team_del_slave
,
993 .ndo_fix_features
= team_fix_features
,
997 /***********************
998 * rt netlink interface
999 ***********************/
1001 static void team_setup(struct net_device
*dev
)
1005 dev
->netdev_ops
= &team_netdev_ops
;
1006 dev
->destructor
= team_destructor
;
1007 dev
->tx_queue_len
= 0;
1008 dev
->flags
|= IFF_MULTICAST
;
1009 dev
->priv_flags
&= ~(IFF_XMIT_DST_RELEASE
| IFF_TX_SKB_SHARING
);
1012 * Indicate we support unicast address filtering. That way core won't
1013 * bring us to promisc mode in case a unicast addr is added.
1014 * Let this up to underlay drivers.
1016 dev
->priv_flags
|= IFF_UNICAST_FLT
;
1018 dev
->features
|= NETIF_F_LLTX
;
1019 dev
->features
|= NETIF_F_GRO
;
1020 dev
->hw_features
= NETIF_F_HW_VLAN_TX
|
1021 NETIF_F_HW_VLAN_RX
|
1022 NETIF_F_HW_VLAN_FILTER
;
1024 dev
->features
|= dev
->hw_features
;
1027 static int team_newlink(struct net
*src_net
, struct net_device
*dev
,
1028 struct nlattr
*tb
[], struct nlattr
*data
[])
1032 if (tb
[IFLA_ADDRESS
] == NULL
)
1033 random_ether_addr(dev
->dev_addr
);
1035 err
= register_netdevice(dev
);
1042 static int team_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1044 if (tb
[IFLA_ADDRESS
]) {
1045 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1047 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1048 return -EADDRNOTAVAIL
;
1053 static struct rtnl_link_ops team_link_ops __read_mostly
= {
1055 .priv_size
= sizeof(struct team
),
1056 .setup
= team_setup
,
1057 .newlink
= team_newlink
,
1058 .validate
= team_validate
,
1062 /***********************************
1063 * Generic netlink custom interface
1064 ***********************************/
1066 static struct genl_family team_nl_family
= {
1067 .id
= GENL_ID_GENERATE
,
1068 .name
= TEAM_GENL_NAME
,
1069 .version
= TEAM_GENL_VERSION
,
1070 .maxattr
= TEAM_ATTR_MAX
,
1074 static const struct nla_policy team_nl_policy
[TEAM_ATTR_MAX
+ 1] = {
1075 [TEAM_ATTR_UNSPEC
] = { .type
= NLA_UNSPEC
, },
1076 [TEAM_ATTR_TEAM_IFINDEX
] = { .type
= NLA_U32
},
1077 [TEAM_ATTR_LIST_OPTION
] = { .type
= NLA_NESTED
},
1078 [TEAM_ATTR_LIST_PORT
] = { .type
= NLA_NESTED
},
1081 static const struct nla_policy
1082 team_nl_option_policy
[TEAM_ATTR_OPTION_MAX
+ 1] = {
1083 [TEAM_ATTR_OPTION_UNSPEC
] = { .type
= NLA_UNSPEC
, },
1084 [TEAM_ATTR_OPTION_NAME
] = {
1086 .len
= TEAM_STRING_MAX_LEN
,
1088 [TEAM_ATTR_OPTION_CHANGED
] = { .type
= NLA_FLAG
},
1089 [TEAM_ATTR_OPTION_TYPE
] = { .type
= NLA_U8
},
1090 [TEAM_ATTR_OPTION_DATA
] = {
1092 .len
= TEAM_STRING_MAX_LEN
,
1096 static int team_nl_cmd_noop(struct sk_buff
*skb
, struct genl_info
*info
)
1098 struct sk_buff
*msg
;
1102 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1106 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
,
1107 &team_nl_family
, 0, TEAM_CMD_NOOP
);
1113 genlmsg_end(msg
, hdr
);
1115 return genlmsg_unicast(genl_info_net(info
), msg
, info
->snd_pid
);
1124 * Netlink cmd functions should be locked by following two functions.
1125 * Since dev gets held here, that ensures dev won't disappear in between.
1127 static struct team
*team_nl_team_get(struct genl_info
*info
)
1129 struct net
*net
= genl_info_net(info
);
1131 struct net_device
*dev
;
1134 if (!info
->attrs
[TEAM_ATTR_TEAM_IFINDEX
])
1137 ifindex
= nla_get_u32(info
->attrs
[TEAM_ATTR_TEAM_IFINDEX
]);
1138 dev
= dev_get_by_index(net
, ifindex
);
1139 if (!dev
|| dev
->netdev_ops
!= &team_netdev_ops
) {
1145 team
= netdev_priv(dev
);
1146 mutex_lock(&team
->lock
);
1150 static void team_nl_team_put(struct team
*team
)
1152 mutex_unlock(&team
->lock
);
1156 static int team_nl_send_generic(struct genl_info
*info
, struct team
*team
,
1157 int (*fill_func
)(struct sk_buff
*skb
,
1158 struct genl_info
*info
,
1159 int flags
, struct team
*team
))
1161 struct sk_buff
*skb
;
1164 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1168 err
= fill_func(skb
, info
, NLM_F_ACK
, team
);
1172 err
= genlmsg_unicast(genl_info_net(info
), skb
, info
->snd_pid
);
1180 static int team_nl_fill_options_get_changed(struct sk_buff
*skb
,
1181 u32 pid
, u32 seq
, int flags
,
1183 struct team_option
*changed_option
)
1185 struct nlattr
*option_list
;
1187 struct team_option
*option
;
1189 hdr
= genlmsg_put(skb
, pid
, seq
, &team_nl_family
, flags
,
1190 TEAM_CMD_OPTIONS_GET
);
1192 return PTR_ERR(hdr
);
1194 NLA_PUT_U32(skb
, TEAM_ATTR_TEAM_IFINDEX
, team
->dev
->ifindex
);
1195 option_list
= nla_nest_start(skb
, TEAM_ATTR_LIST_OPTION
);
1199 list_for_each_entry(option
, &team
->option_list
, list
) {
1200 struct nlattr
*option_item
;
1203 option_item
= nla_nest_start(skb
, TEAM_ATTR_ITEM_OPTION
);
1205 goto nla_put_failure
;
1206 NLA_PUT_STRING(skb
, TEAM_ATTR_OPTION_NAME
, option
->name
);
1207 if (option
== changed_option
)
1208 NLA_PUT_FLAG(skb
, TEAM_ATTR_OPTION_CHANGED
);
1209 switch (option
->type
) {
1210 case TEAM_OPTION_TYPE_U32
:
1211 NLA_PUT_U8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_U32
);
1212 team_option_get(team
, option
, &arg
);
1213 NLA_PUT_U32(skb
, TEAM_ATTR_OPTION_DATA
, arg
);
1215 case TEAM_OPTION_TYPE_STRING
:
1216 NLA_PUT_U8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_STRING
);
1217 team_option_get(team
, option
, &arg
);
1218 NLA_PUT_STRING(skb
, TEAM_ATTR_OPTION_DATA
,
1224 nla_nest_end(skb
, option_item
);
1227 nla_nest_end(skb
, option_list
);
1228 return genlmsg_end(skb
, hdr
);
1231 genlmsg_cancel(skb
, hdr
);
1235 static int team_nl_fill_options_get(struct sk_buff
*skb
,
1236 struct genl_info
*info
, int flags
,
1239 return team_nl_fill_options_get_changed(skb
, info
->snd_pid
,
1240 info
->snd_seq
, NLM_F_ACK
,
1244 static int team_nl_cmd_options_get(struct sk_buff
*skb
, struct genl_info
*info
)
1249 team
= team_nl_team_get(info
);
1253 err
= team_nl_send_generic(info
, team
, team_nl_fill_options_get
);
1255 team_nl_team_put(team
);
1260 static int team_nl_cmd_options_set(struct sk_buff
*skb
, struct genl_info
*info
)
1265 struct nlattr
*nl_option
;
1267 team
= team_nl_team_get(info
);
1272 if (!info
->attrs
[TEAM_ATTR_LIST_OPTION
]) {
1277 nla_for_each_nested(nl_option
, info
->attrs
[TEAM_ATTR_LIST_OPTION
], i
) {
1278 struct nlattr
*mode_attrs
[TEAM_ATTR_OPTION_MAX
+ 1];
1279 enum team_option_type opt_type
;
1280 struct team_option
*option
;
1282 bool opt_found
= false;
1284 if (nla_type(nl_option
) != TEAM_ATTR_ITEM_OPTION
) {
1288 err
= nla_parse_nested(mode_attrs
, TEAM_ATTR_OPTION_MAX
,
1289 nl_option
, team_nl_option_policy
);
1292 if (!mode_attrs
[TEAM_ATTR_OPTION_NAME
] ||
1293 !mode_attrs
[TEAM_ATTR_OPTION_TYPE
] ||
1294 !mode_attrs
[TEAM_ATTR_OPTION_DATA
]) {
1298 switch (nla_get_u8(mode_attrs
[TEAM_ATTR_OPTION_TYPE
])) {
1300 opt_type
= TEAM_OPTION_TYPE_U32
;
1303 opt_type
= TEAM_OPTION_TYPE_STRING
;
1309 opt_name
= nla_data(mode_attrs
[TEAM_ATTR_OPTION_NAME
]);
1310 list_for_each_entry(option
, &team
->option_list
, list
) {
1312 struct nlattr
*opt_data_attr
;
1314 if (option
->type
!= opt_type
||
1315 strcmp(option
->name
, opt_name
))
1318 opt_data_attr
= mode_attrs
[TEAM_ATTR_OPTION_DATA
];
1320 case TEAM_OPTION_TYPE_U32
:
1321 arg
= nla_get_u32(opt_data_attr
);
1323 case TEAM_OPTION_TYPE_STRING
:
1324 arg
= (long) nla_data(opt_data_attr
);
1329 err
= team_option_set(team
, option
, &arg
);
1340 team_nl_team_put(team
);
1345 static int team_nl_fill_port_list_get_changed(struct sk_buff
*skb
,
1346 u32 pid
, u32 seq
, int flags
,
1348 struct team_port
*changed_port
)
1350 struct nlattr
*port_list
;
1352 struct team_port
*port
;
1354 hdr
= genlmsg_put(skb
, pid
, seq
, &team_nl_family
, flags
,
1355 TEAM_CMD_PORT_LIST_GET
);
1357 return PTR_ERR(hdr
);
1359 NLA_PUT_U32(skb
, TEAM_ATTR_TEAM_IFINDEX
, team
->dev
->ifindex
);
1360 port_list
= nla_nest_start(skb
, TEAM_ATTR_LIST_PORT
);
1364 list_for_each_entry(port
, &team
->port_list
, list
) {
1365 struct nlattr
*port_item
;
1367 port_item
= nla_nest_start(skb
, TEAM_ATTR_ITEM_PORT
);
1369 goto nla_put_failure
;
1370 NLA_PUT_U32(skb
, TEAM_ATTR_PORT_IFINDEX
, port
->dev
->ifindex
);
1371 if (port
== changed_port
)
1372 NLA_PUT_FLAG(skb
, TEAM_ATTR_PORT_CHANGED
);
1374 NLA_PUT_FLAG(skb
, TEAM_ATTR_PORT_LINKUP
);
1375 NLA_PUT_U32(skb
, TEAM_ATTR_PORT_SPEED
, port
->speed
);
1376 NLA_PUT_U8(skb
, TEAM_ATTR_PORT_DUPLEX
, port
->duplex
);
1377 nla_nest_end(skb
, port_item
);
1380 nla_nest_end(skb
, port_list
);
1381 return genlmsg_end(skb
, hdr
);
1384 genlmsg_cancel(skb
, hdr
);
1388 static int team_nl_fill_port_list_get(struct sk_buff
*skb
,
1389 struct genl_info
*info
, int flags
,
1392 return team_nl_fill_port_list_get_changed(skb
, info
->snd_pid
,
1393 info
->snd_seq
, NLM_F_ACK
,
1397 static int team_nl_cmd_port_list_get(struct sk_buff
*skb
,
1398 struct genl_info
*info
)
1403 team
= team_nl_team_get(info
);
1407 err
= team_nl_send_generic(info
, team
, team_nl_fill_port_list_get
);
1409 team_nl_team_put(team
);
1414 static struct genl_ops team_nl_ops
[] = {
1416 .cmd
= TEAM_CMD_NOOP
,
1417 .doit
= team_nl_cmd_noop
,
1418 .policy
= team_nl_policy
,
1421 .cmd
= TEAM_CMD_OPTIONS_SET
,
1422 .doit
= team_nl_cmd_options_set
,
1423 .policy
= team_nl_policy
,
1424 .flags
= GENL_ADMIN_PERM
,
1427 .cmd
= TEAM_CMD_OPTIONS_GET
,
1428 .doit
= team_nl_cmd_options_get
,
1429 .policy
= team_nl_policy
,
1430 .flags
= GENL_ADMIN_PERM
,
1433 .cmd
= TEAM_CMD_PORT_LIST_GET
,
1434 .doit
= team_nl_cmd_port_list_get
,
1435 .policy
= team_nl_policy
,
1436 .flags
= GENL_ADMIN_PERM
,
1440 static struct genl_multicast_group team_change_event_mcgrp
= {
1441 .name
= TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME
,
1444 static int team_nl_send_event_options_get(struct team
*team
,
1445 struct team_option
*changed_option
)
1447 struct sk_buff
*skb
;
1449 struct net
*net
= dev_net(team
->dev
);
1451 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1455 err
= team_nl_fill_options_get_changed(skb
, 0, 0, 0, team
,
1460 err
= genlmsg_multicast_netns(net
, skb
, 0, team_change_event_mcgrp
.id
,
1469 static int team_nl_send_event_port_list_get(struct team_port
*port
)
1471 struct sk_buff
*skb
;
1473 struct net
*net
= dev_net(port
->team
->dev
);
1475 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1479 err
= team_nl_fill_port_list_get_changed(skb
, 0, 0, 0,
1484 err
= genlmsg_multicast_netns(net
, skb
, 0, team_change_event_mcgrp
.id
,
1493 static int team_nl_init(void)
1497 err
= genl_register_family_with_ops(&team_nl_family
, team_nl_ops
,
1498 ARRAY_SIZE(team_nl_ops
));
1502 err
= genl_register_mc_group(&team_nl_family
, &team_change_event_mcgrp
);
1504 goto err_change_event_grp_reg
;
1508 err_change_event_grp_reg
:
1509 genl_unregister_family(&team_nl_family
);
1514 static void team_nl_fini(void)
1516 genl_unregister_family(&team_nl_family
);
1524 static void __team_options_change_check(struct team
*team
,
1525 struct team_option
*changed_option
)
1529 err
= team_nl_send_event_options_get(team
, changed_option
);
1531 netdev_warn(team
->dev
, "Failed to send options change via netlink\n");
1534 /* rtnl lock is held */
1535 static void __team_port_change_check(struct team_port
*port
, bool linkup
)
1539 if (port
->linkup
== linkup
)
1542 port
->linkup
= linkup
;
1544 struct ethtool_cmd ecmd
;
1546 err
= __ethtool_get_settings(port
->dev
, &ecmd
);
1548 port
->speed
= ethtool_cmd_speed(&ecmd
);
1549 port
->duplex
= ecmd
.duplex
;
1557 err
= team_nl_send_event_port_list_get(port
);
1559 netdev_warn(port
->team
->dev
, "Failed to send port change of device %s via netlink\n",
1564 static void team_port_change_check(struct team_port
*port
, bool linkup
)
1566 struct team
*team
= port
->team
;
1568 mutex_lock(&team
->lock
);
1569 __team_port_change_check(port
, linkup
);
1570 mutex_unlock(&team
->lock
);
1573 /************************************
1574 * Net device notifier event handler
1575 ************************************/
1577 static int team_device_event(struct notifier_block
*unused
,
1578 unsigned long event
, void *ptr
)
1580 struct net_device
*dev
= (struct net_device
*) ptr
;
1581 struct team_port
*port
;
1583 port
= team_port_get_rtnl(dev
);
1589 if (netif_carrier_ok(dev
))
1590 team_port_change_check(port
, true);
1592 team_port_change_check(port
, false);
1594 if (netif_running(port
->dev
))
1595 team_port_change_check(port
,
1596 !!netif_carrier_ok(port
->dev
));
1598 case NETDEV_UNREGISTER
:
1599 team_del_slave(port
->team
->dev
, dev
);
1601 case NETDEV_FEAT_CHANGE
:
1602 team_compute_features(port
->team
);
1604 case NETDEV_CHANGEMTU
:
1605 /* Forbid to change mtu of underlaying device */
1607 case NETDEV_PRE_TYPE_CHANGE
:
1608 /* Forbid to change type of underlaying device */
1614 static struct notifier_block team_notifier_block __read_mostly
= {
1615 .notifier_call
= team_device_event
,
1619 /***********************
1620 * Module init and exit
1621 ***********************/
1623 static int __init
team_module_init(void)
1627 register_netdevice_notifier(&team_notifier_block
);
1629 err
= rtnl_link_register(&team_link_ops
);
1633 err
= team_nl_init();
1640 rtnl_link_unregister(&team_link_ops
);
1643 unregister_netdevice_notifier(&team_notifier_block
);
1648 static void __exit
team_module_exit(void)
1651 rtnl_link_unregister(&team_link_ops
);
1652 unregister_netdevice_notifier(&team_notifier_block
);
1655 module_init(team_module_init
);
1656 module_exit(team_module_exit
);
1658 MODULE_LICENSE("GPL v2");
1659 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
1660 MODULE_DESCRIPTION("Ethernet team device driver");
1661 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);