2 * drivers/net/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 static 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
);
79 static void team_refresh_port_linkup(struct team_port
*port
)
81 port
->linkup
= port
->user
.linkup_enabled
? port
->user
.linkup
:
89 struct team_option_inst
{ /* One for each option instance */
90 struct list_head list
;
91 struct team_option
*option
;
92 struct team_option_inst_info info
;
97 static struct team_option
*__team_find_option(struct team
*team
,
100 struct team_option
*option
;
102 list_for_each_entry(option
, &team
->option_list
, list
) {
103 if (strcmp(option
->name
, opt_name
) == 0)
109 static void __team_option_inst_del(struct team_option_inst
*opt_inst
)
111 list_del(&opt_inst
->list
);
115 static void __team_option_inst_del_option(struct team
*team
,
116 struct team_option
*option
)
118 struct team_option_inst
*opt_inst
, *tmp
;
120 list_for_each_entry_safe(opt_inst
, tmp
, &team
->option_inst_list
, list
) {
121 if (opt_inst
->option
== option
)
122 __team_option_inst_del(opt_inst
);
126 static int __team_option_inst_add(struct team
*team
, struct team_option
*option
,
127 struct team_port
*port
)
129 struct team_option_inst
*opt_inst
;
130 unsigned int array_size
;
134 array_size
= option
->array_size
;
136 array_size
= 1; /* No array but still need one instance */
138 for (i
= 0; i
< array_size
; i
++) {
139 opt_inst
= kmalloc(sizeof(*opt_inst
), GFP_KERNEL
);
142 opt_inst
->option
= option
;
143 opt_inst
->info
.port
= port
;
144 opt_inst
->info
.array_index
= i
;
145 opt_inst
->changed
= true;
146 opt_inst
->removed
= false;
147 list_add_tail(&opt_inst
->list
, &team
->option_inst_list
);
149 err
= option
->init(team
, &opt_inst
->info
);
158 static int __team_option_inst_add_option(struct team
*team
,
159 struct team_option
*option
)
161 struct team_port
*port
;
164 if (!option
->per_port
) {
165 err
= __team_option_inst_add(team
, option
, 0);
167 goto inst_del_option
;
170 list_for_each_entry(port
, &team
->port_list
, list
) {
171 err
= __team_option_inst_add(team
, option
, port
);
173 goto inst_del_option
;
178 __team_option_inst_del_option(team
, option
);
182 static void __team_option_inst_mark_removed_option(struct team
*team
,
183 struct team_option
*option
)
185 struct team_option_inst
*opt_inst
;
187 list_for_each_entry(opt_inst
, &team
->option_inst_list
, list
) {
188 if (opt_inst
->option
== option
) {
189 opt_inst
->changed
= true;
190 opt_inst
->removed
= true;
195 static void __team_option_inst_del_port(struct team
*team
,
196 struct team_port
*port
)
198 struct team_option_inst
*opt_inst
, *tmp
;
200 list_for_each_entry_safe(opt_inst
, tmp
, &team
->option_inst_list
, list
) {
201 if (opt_inst
->option
->per_port
&&
202 opt_inst
->info
.port
== port
)
203 __team_option_inst_del(opt_inst
);
207 static int __team_option_inst_add_port(struct team
*team
,
208 struct team_port
*port
)
210 struct team_option
*option
;
213 list_for_each_entry(option
, &team
->option_list
, list
) {
214 if (!option
->per_port
)
216 err
= __team_option_inst_add(team
, option
, port
);
223 __team_option_inst_del_port(team
, port
);
227 static void __team_option_inst_mark_removed_port(struct team
*team
,
228 struct team_port
*port
)
230 struct team_option_inst
*opt_inst
;
232 list_for_each_entry(opt_inst
, &team
->option_inst_list
, list
) {
233 if (opt_inst
->info
.port
== port
) {
234 opt_inst
->changed
= true;
235 opt_inst
->removed
= true;
240 static int __team_options_register(struct team
*team
,
241 const struct team_option
*option
,
245 struct team_option
**dst_opts
;
248 dst_opts
= kzalloc(sizeof(struct team_option
*) * option_count
,
252 for (i
= 0; i
< option_count
; i
++, option
++) {
253 if (__team_find_option(team
, option
->name
)) {
257 dst_opts
[i
] = kmemdup(option
, sizeof(*option
), GFP_KERNEL
);
264 for (i
= 0; i
< option_count
; i
++) {
265 err
= __team_option_inst_add_option(team
, dst_opts
[i
]);
268 list_add_tail(&dst_opts
[i
]->list
, &team
->option_list
);
275 for (i
--; i
>= 0; i
--)
276 __team_option_inst_del_option(team
, dst_opts
[i
]);
278 i
= option_count
- 1;
280 for (i
--; i
>= 0; i
--)
287 static void __team_options_mark_removed(struct team
*team
,
288 const struct team_option
*option
,
293 for (i
= 0; i
< option_count
; i
++, option
++) {
294 struct team_option
*del_opt
;
296 del_opt
= __team_find_option(team
, option
->name
);
298 __team_option_inst_mark_removed_option(team
, del_opt
);
302 static void __team_options_unregister(struct team
*team
,
303 const struct team_option
*option
,
308 for (i
= 0; i
< option_count
; i
++, option
++) {
309 struct team_option
*del_opt
;
311 del_opt
= __team_find_option(team
, option
->name
);
313 __team_option_inst_del_option(team
, del_opt
);
314 list_del(&del_opt
->list
);
320 static void __team_options_change_check(struct team
*team
);
322 int team_options_register(struct team
*team
,
323 const struct team_option
*option
,
328 err
= __team_options_register(team
, option
, option_count
);
331 __team_options_change_check(team
);
334 EXPORT_SYMBOL(team_options_register
);
336 void team_options_unregister(struct team
*team
,
337 const struct team_option
*option
,
340 __team_options_mark_removed(team
, option
, option_count
);
341 __team_options_change_check(team
);
342 __team_options_unregister(team
, option
, option_count
);
344 EXPORT_SYMBOL(team_options_unregister
);
346 static int team_option_port_add(struct team
*team
, struct team_port
*port
)
350 err
= __team_option_inst_add_port(team
, port
);
353 __team_options_change_check(team
);
357 static void team_option_port_del(struct team
*team
, struct team_port
*port
)
359 __team_option_inst_mark_removed_port(team
, port
);
360 __team_options_change_check(team
);
361 __team_option_inst_del_port(team
, port
);
364 static int team_option_get(struct team
*team
,
365 struct team_option_inst
*opt_inst
,
366 struct team_gsetter_ctx
*ctx
)
368 if (!opt_inst
->option
->getter
)
370 return opt_inst
->option
->getter(team
, ctx
);
373 static int team_option_set(struct team
*team
,
374 struct team_option_inst
*opt_inst
,
375 struct team_gsetter_ctx
*ctx
)
379 if (!opt_inst
->option
->setter
)
381 err
= opt_inst
->option
->setter(team
, ctx
);
385 opt_inst
->changed
= true;
386 __team_options_change_check(team
);
394 static LIST_HEAD(mode_list
);
395 static DEFINE_SPINLOCK(mode_list_lock
);
397 struct team_mode_item
{
398 struct list_head list
;
399 const struct team_mode
*mode
;
402 static struct team_mode_item
*__find_mode(const char *kind
)
404 struct team_mode_item
*mitem
;
406 list_for_each_entry(mitem
, &mode_list
, list
) {
407 if (strcmp(mitem
->mode
->kind
, kind
) == 0)
413 static bool is_good_mode_name(const char *name
)
415 while (*name
!= '\0') {
416 if (!isalpha(*name
) && !isdigit(*name
) && *name
!= '_')
423 int team_mode_register(const struct team_mode
*mode
)
426 struct team_mode_item
*mitem
;
428 if (!is_good_mode_name(mode
->kind
) ||
429 mode
->priv_size
> TEAM_MODE_PRIV_SIZE
)
432 mitem
= kmalloc(sizeof(*mitem
), GFP_KERNEL
);
436 spin_lock(&mode_list_lock
);
437 if (__find_mode(mode
->kind
)) {
443 list_add_tail(&mitem
->list
, &mode_list
);
445 spin_unlock(&mode_list_lock
);
448 EXPORT_SYMBOL(team_mode_register
);
450 void team_mode_unregister(const struct team_mode
*mode
)
452 struct team_mode_item
*mitem
;
454 spin_lock(&mode_list_lock
);
455 mitem
= __find_mode(mode
->kind
);
457 list_del_init(&mitem
->list
);
460 spin_unlock(&mode_list_lock
);
462 EXPORT_SYMBOL(team_mode_unregister
);
464 static const struct team_mode
*team_mode_get(const char *kind
)
466 struct team_mode_item
*mitem
;
467 const struct team_mode
*mode
= NULL
;
469 spin_lock(&mode_list_lock
);
470 mitem
= __find_mode(kind
);
472 spin_unlock(&mode_list_lock
);
473 request_module("team-mode-%s", kind
);
474 spin_lock(&mode_list_lock
);
475 mitem
= __find_mode(kind
);
479 if (!try_module_get(mode
->owner
))
483 spin_unlock(&mode_list_lock
);
487 static void team_mode_put(const struct team_mode
*mode
)
489 module_put(mode
->owner
);
492 static bool team_dummy_transmit(struct team
*team
, struct sk_buff
*skb
)
494 dev_kfree_skb_any(skb
);
498 rx_handler_result_t
team_dummy_receive(struct team
*team
,
499 struct team_port
*port
,
502 return RX_HANDLER_ANOTHER
;
505 static const struct team_mode __team_no_mode
= {
509 static bool team_is_mode_set(struct team
*team
)
511 return team
->mode
!= &__team_no_mode
;
514 static void team_set_no_mode(struct team
*team
)
516 team
->mode
= &__team_no_mode
;
519 static void team_adjust_ops(struct team
*team
)
522 * To avoid checks in rx/tx skb paths, ensure here that non-null and
523 * correct ops are always set.
526 if (list_empty(&team
->port_list
) ||
527 !team_is_mode_set(team
) || !team
->mode
->ops
->transmit
)
528 team
->ops
.transmit
= team_dummy_transmit
;
530 team
->ops
.transmit
= team
->mode
->ops
->transmit
;
532 if (list_empty(&team
->port_list
) ||
533 !team_is_mode_set(team
) || !team
->mode
->ops
->receive
)
534 team
->ops
.receive
= team_dummy_receive
;
536 team
->ops
.receive
= team
->mode
->ops
->receive
;
540 * We can benefit from the fact that it's ensured no port is present
541 * at the time of mode change. Therefore no packets are in fly so there's no
542 * need to set mode operations in any special way.
544 static int __team_change_mode(struct team
*team
,
545 const struct team_mode
*new_mode
)
547 /* Check if mode was previously set and do cleanup if so */
548 if (team_is_mode_set(team
)) {
549 void (*exit_op
)(struct team
*team
) = team
->ops
.exit
;
551 /* Clear ops area so no callback is called any longer */
552 memset(&team
->ops
, 0, sizeof(struct team_mode_ops
));
553 team_adjust_ops(team
);
557 team_mode_put(team
->mode
);
558 team_set_no_mode(team
);
559 /* zero private data area */
560 memset(&team
->mode_priv
, 0,
561 sizeof(struct team
) - offsetof(struct team
, mode_priv
));
567 if (new_mode
->ops
->init
) {
570 err
= new_mode
->ops
->init(team
);
575 team
->mode
= new_mode
;
576 memcpy(&team
->ops
, new_mode
->ops
, sizeof(struct team_mode_ops
));
577 team_adjust_ops(team
);
582 static int team_change_mode(struct team
*team
, const char *kind
)
584 const struct team_mode
*new_mode
;
585 struct net_device
*dev
= team
->dev
;
588 if (!list_empty(&team
->port_list
)) {
589 netdev_err(dev
, "No ports can be present during mode change\n");
593 if (team_is_mode_set(team
) && strcmp(team
->mode
->kind
, kind
) == 0) {
594 netdev_err(dev
, "Unable to change to the same mode the team is in\n");
598 new_mode
= team_mode_get(kind
);
600 netdev_err(dev
, "Mode \"%s\" not found\n", kind
);
604 err
= __team_change_mode(team
, new_mode
);
606 netdev_err(dev
, "Failed to change to mode \"%s\"\n", kind
);
607 team_mode_put(new_mode
);
611 netdev_info(dev
, "Mode changed to \"%s\"\n", kind
);
616 /************************
617 * Rx path frame handler
618 ************************/
620 static bool team_port_enabled(struct team_port
*port
);
622 /* note: already called with rcu_read_lock */
623 static rx_handler_result_t
team_handle_frame(struct sk_buff
**pskb
)
625 struct sk_buff
*skb
= *pskb
;
626 struct team_port
*port
;
628 rx_handler_result_t res
;
630 skb
= skb_share_check(skb
, GFP_ATOMIC
);
632 return RX_HANDLER_CONSUMED
;
636 port
= team_port_get_rcu(skb
->dev
);
638 if (!team_port_enabled(port
)) {
639 /* allow exact match delivery for disabled ports */
640 res
= RX_HANDLER_EXACT
;
642 res
= team
->ops
.receive(team
, port
, skb
);
644 if (res
== RX_HANDLER_ANOTHER
) {
645 struct team_pcpu_stats
*pcpu_stats
;
647 pcpu_stats
= this_cpu_ptr(team
->pcpu_stats
);
648 u64_stats_update_begin(&pcpu_stats
->syncp
);
649 pcpu_stats
->rx_packets
++;
650 pcpu_stats
->rx_bytes
+= skb
->len
;
651 if (skb
->pkt_type
== PACKET_MULTICAST
)
652 pcpu_stats
->rx_multicast
++;
653 u64_stats_update_end(&pcpu_stats
->syncp
);
655 skb
->dev
= team
->dev
;
657 this_cpu_inc(team
->pcpu_stats
->rx_dropped
);
668 static bool team_port_find(const struct team
*team
,
669 const struct team_port
*port
)
671 struct team_port
*cur
;
673 list_for_each_entry(cur
, &team
->port_list
, list
)
679 static bool team_port_enabled(struct team_port
*port
)
681 return port
->index
!= -1;
685 * Enable/disable port by adding to enabled port hashlist and setting
686 * port->index (Might be racy so reader could see incorrect ifindex when
687 * processing a flying packet, but that is not a problem). Write guarded
690 static void team_port_enable(struct team
*team
,
691 struct team_port
*port
)
693 if (team_port_enabled(port
))
695 port
->index
= team
->en_port_count
++;
696 hlist_add_head_rcu(&port
->hlist
,
697 team_port_index_hash(team
, port
->index
));
700 static void __reconstruct_port_hlist(struct team
*team
, int rm_index
)
703 struct team_port
*port
;
705 for (i
= rm_index
+ 1; i
< team
->en_port_count
; i
++) {
706 port
= team_get_port_by_index(team
, i
);
707 hlist_del_rcu(&port
->hlist
);
709 hlist_add_head_rcu(&port
->hlist
,
710 team_port_index_hash(team
, port
->index
));
714 static void team_port_disable(struct team
*team
,
715 struct team_port
*port
)
717 int rm_index
= port
->index
;
719 if (!team_port_enabled(port
))
721 hlist_del_rcu(&port
->hlist
);
722 __reconstruct_port_hlist(team
, rm_index
);
723 team
->en_port_count
--;
727 #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
728 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
729 NETIF_F_HIGHDMA | NETIF_F_LRO)
731 static void __team_compute_features(struct team
*team
)
733 struct team_port
*port
;
734 u32 vlan_features
= TEAM_VLAN_FEATURES
;
735 unsigned short max_hard_header_len
= ETH_HLEN
;
737 list_for_each_entry(port
, &team
->port_list
, list
) {
738 vlan_features
= netdev_increment_features(vlan_features
,
739 port
->dev
->vlan_features
,
742 if (port
->dev
->hard_header_len
> max_hard_header_len
)
743 max_hard_header_len
= port
->dev
->hard_header_len
;
746 team
->dev
->vlan_features
= vlan_features
;
747 team
->dev
->hard_header_len
= max_hard_header_len
;
749 netdev_change_features(team
->dev
);
752 static void team_compute_features(struct team
*team
)
754 mutex_lock(&team
->lock
);
755 __team_compute_features(team
);
756 mutex_unlock(&team
->lock
);
759 static int team_port_enter(struct team
*team
, struct team_port
*port
)
764 port
->dev
->priv_flags
|= IFF_TEAM_PORT
;
765 if (team
->ops
.port_enter
) {
766 err
= team
->ops
.port_enter(team
, port
);
768 netdev_err(team
->dev
, "Device %s failed to enter team mode\n",
777 port
->dev
->priv_flags
&= ~IFF_TEAM_PORT
;
783 static void team_port_leave(struct team
*team
, struct team_port
*port
)
785 if (team
->ops
.port_leave
)
786 team
->ops
.port_leave(team
, port
);
787 port
->dev
->priv_flags
&= ~IFF_TEAM_PORT
;
791 static void __team_port_change_check(struct team_port
*port
, bool linkup
);
793 static int team_port_add(struct team
*team
, struct net_device
*port_dev
)
795 struct net_device
*dev
= team
->dev
;
796 struct team_port
*port
;
797 char *portname
= port_dev
->name
;
800 if (port_dev
->flags
& IFF_LOOPBACK
||
801 port_dev
->type
!= ARPHRD_ETHER
) {
802 netdev_err(dev
, "Device %s is of an unsupported type\n",
807 if (team_port_exists(port_dev
)) {
808 netdev_err(dev
, "Device %s is already a port "
809 "of a team device\n", portname
);
813 if (port_dev
->flags
& IFF_UP
) {
814 netdev_err(dev
, "Device %s is up. Set it down before adding it as a team port\n",
819 port
= kzalloc(sizeof(struct team_port
) + team
->mode
->port_priv_size
,
824 port
->dev
= port_dev
;
827 port
->orig
.mtu
= port_dev
->mtu
;
828 err
= dev_set_mtu(port_dev
, dev
->mtu
);
830 netdev_dbg(dev
, "Error %d calling dev_set_mtu\n", err
);
834 memcpy(port
->orig
.dev_addr
, port_dev
->dev_addr
, ETH_ALEN
);
836 err
= team_port_enter(team
, port
);
838 netdev_err(dev
, "Device %s failed to enter team mode\n",
843 err
= dev_open(port_dev
);
845 netdev_dbg(dev
, "Device %s opening failed\n",
850 err
= vlan_vids_add_by_dev(port_dev
, dev
);
852 netdev_err(dev
, "Failed to add vlan ids to device %s\n",
857 err
= netdev_set_master(port_dev
, dev
);
859 netdev_err(dev
, "Device %s failed to set master\n", portname
);
863 err
= netdev_rx_handler_register(port_dev
, team_handle_frame
,
866 netdev_err(dev
, "Device %s failed to register rx_handler\n",
868 goto err_handler_register
;
871 err
= team_option_port_add(team
, port
);
873 netdev_err(dev
, "Device %s failed to add per-port options\n",
875 goto err_option_port_add
;
879 team_port_enable(team
, port
);
880 list_add_tail_rcu(&port
->list
, &team
->port_list
);
881 team_adjust_ops(team
);
882 __team_compute_features(team
);
883 __team_port_change_check(port
, !!netif_carrier_ok(port_dev
));
885 netdev_info(dev
, "Port device %s added\n", portname
);
890 netdev_rx_handler_unregister(port_dev
);
892 err_handler_register
:
893 netdev_set_master(port_dev
, NULL
);
896 vlan_vids_del_by_dev(port_dev
, dev
);
902 team_port_leave(team
, port
);
903 team_port_set_orig_mac(port
);
906 dev_set_mtu(port_dev
, port
->orig
.mtu
);
914 static int team_port_del(struct team
*team
, struct net_device
*port_dev
)
916 struct net_device
*dev
= team
->dev
;
917 struct team_port
*port
;
918 char *portname
= port_dev
->name
;
920 port
= team_port_get_rtnl(port_dev
);
921 if (!port
|| !team_port_find(team
, port
)) {
922 netdev_err(dev
, "Device %s does not act as a port of this team\n",
927 port
->removed
= true;
928 __team_port_change_check(port
, false);
929 team_port_disable(team
, port
);
930 list_del_rcu(&port
->list
);
931 team_adjust_ops(team
);
932 team_option_port_del(team
, port
);
933 netdev_rx_handler_unregister(port_dev
);
934 netdev_set_master(port_dev
, NULL
);
935 vlan_vids_del_by_dev(port_dev
, dev
);
937 team_port_leave(team
, port
);
938 team_port_set_orig_mac(port
);
939 dev_set_mtu(port_dev
, port
->orig
.mtu
);
942 netdev_info(dev
, "Port device %s removed\n", portname
);
943 __team_compute_features(team
);
953 static int team_mode_option_get(struct team
*team
, struct team_gsetter_ctx
*ctx
)
955 ctx
->data
.str_val
= team
->mode
->kind
;
959 static int team_mode_option_set(struct team
*team
, struct team_gsetter_ctx
*ctx
)
961 return team_change_mode(team
, ctx
->data
.str_val
);
964 static int team_port_en_option_get(struct team
*team
,
965 struct team_gsetter_ctx
*ctx
)
967 struct team_port
*port
= ctx
->info
->port
;
969 ctx
->data
.bool_val
= team_port_enabled(port
);
973 static int team_port_en_option_set(struct team
*team
,
974 struct team_gsetter_ctx
*ctx
)
976 struct team_port
*port
= ctx
->info
->port
;
978 if (ctx
->data
.bool_val
)
979 team_port_enable(team
, port
);
981 team_port_disable(team
, port
);
985 static int team_user_linkup_option_get(struct team
*team
,
986 struct team_gsetter_ctx
*ctx
)
988 struct team_port
*port
= ctx
->info
->port
;
990 ctx
->data
.bool_val
= port
->user
.linkup
;
994 static int team_user_linkup_option_set(struct team
*team
,
995 struct team_gsetter_ctx
*ctx
)
997 struct team_port
*port
= ctx
->info
->port
;
999 port
->user
.linkup
= ctx
->data
.bool_val
;
1000 team_refresh_port_linkup(port
);
1004 static int team_user_linkup_en_option_get(struct team
*team
,
1005 struct team_gsetter_ctx
*ctx
)
1007 struct team_port
*port
= ctx
->info
->port
;
1009 ctx
->data
.bool_val
= port
->user
.linkup_enabled
;
1013 static int team_user_linkup_en_option_set(struct team
*team
,
1014 struct team_gsetter_ctx
*ctx
)
1016 struct team_port
*port
= ctx
->info
->port
;
1018 port
->user
.linkup_enabled
= ctx
->data
.bool_val
;
1019 team_refresh_port_linkup(port
);
1023 static const struct team_option team_options
[] = {
1026 .type
= TEAM_OPTION_TYPE_STRING
,
1027 .getter
= team_mode_option_get
,
1028 .setter
= team_mode_option_set
,
1032 .type
= TEAM_OPTION_TYPE_BOOL
,
1034 .getter
= team_port_en_option_get
,
1035 .setter
= team_port_en_option_set
,
1038 .name
= "user_linkup",
1039 .type
= TEAM_OPTION_TYPE_BOOL
,
1041 .getter
= team_user_linkup_option_get
,
1042 .setter
= team_user_linkup_option_set
,
1045 .name
= "user_linkup_enabled",
1046 .type
= TEAM_OPTION_TYPE_BOOL
,
1048 .getter
= team_user_linkup_en_option_get
,
1049 .setter
= team_user_linkup_en_option_set
,
1053 static int team_init(struct net_device
*dev
)
1055 struct team
*team
= netdev_priv(dev
);
1060 mutex_init(&team
->lock
);
1061 team_set_no_mode(team
);
1063 team
->pcpu_stats
= alloc_percpu(struct team_pcpu_stats
);
1064 if (!team
->pcpu_stats
)
1067 for (i
= 0; i
< TEAM_PORT_HASHENTRIES
; i
++)
1068 INIT_HLIST_HEAD(&team
->en_port_hlist
[i
]);
1069 INIT_LIST_HEAD(&team
->port_list
);
1071 team_adjust_ops(team
);
1073 INIT_LIST_HEAD(&team
->option_list
);
1074 INIT_LIST_HEAD(&team
->option_inst_list
);
1075 err
= team_options_register(team
, team_options
, ARRAY_SIZE(team_options
));
1077 goto err_options_register
;
1078 netif_carrier_off(dev
);
1082 err_options_register
:
1083 free_percpu(team
->pcpu_stats
);
1088 static void team_uninit(struct net_device
*dev
)
1090 struct team
*team
= netdev_priv(dev
);
1091 struct team_port
*port
;
1092 struct team_port
*tmp
;
1094 mutex_lock(&team
->lock
);
1095 list_for_each_entry_safe(port
, tmp
, &team
->port_list
, list
)
1096 team_port_del(team
, port
->dev
);
1098 __team_change_mode(team
, NULL
); /* cleanup */
1099 __team_options_unregister(team
, team_options
, ARRAY_SIZE(team_options
));
1100 mutex_unlock(&team
->lock
);
1103 static void team_destructor(struct net_device
*dev
)
1105 struct team
*team
= netdev_priv(dev
);
1107 free_percpu(team
->pcpu_stats
);
1111 static int team_open(struct net_device
*dev
)
1113 netif_carrier_on(dev
);
1117 static int team_close(struct net_device
*dev
)
1119 netif_carrier_off(dev
);
1124 * note: already called with rcu_read_lock
1126 static netdev_tx_t
team_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1128 struct team
*team
= netdev_priv(dev
);
1129 bool tx_success
= false;
1130 unsigned int len
= skb
->len
;
1132 tx_success
= team
->ops
.transmit(team
, skb
);
1134 struct team_pcpu_stats
*pcpu_stats
;
1136 pcpu_stats
= this_cpu_ptr(team
->pcpu_stats
);
1137 u64_stats_update_begin(&pcpu_stats
->syncp
);
1138 pcpu_stats
->tx_packets
++;
1139 pcpu_stats
->tx_bytes
+= len
;
1140 u64_stats_update_end(&pcpu_stats
->syncp
);
1142 this_cpu_inc(team
->pcpu_stats
->tx_dropped
);
1145 return NETDEV_TX_OK
;
1148 static void team_change_rx_flags(struct net_device
*dev
, int change
)
1150 struct team
*team
= netdev_priv(dev
);
1151 struct team_port
*port
;
1155 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
1156 if (change
& IFF_PROMISC
) {
1157 inc
= dev
->flags
& IFF_PROMISC
? 1 : -1;
1158 dev_set_promiscuity(port
->dev
, inc
);
1160 if (change
& IFF_ALLMULTI
) {
1161 inc
= dev
->flags
& IFF_ALLMULTI
? 1 : -1;
1162 dev_set_allmulti(port
->dev
, inc
);
1168 static void team_set_rx_mode(struct net_device
*dev
)
1170 struct team
*team
= netdev_priv(dev
);
1171 struct team_port
*port
;
1174 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
1175 dev_uc_sync(port
->dev
, dev
);
1176 dev_mc_sync(port
->dev
, dev
);
1181 static int team_set_mac_address(struct net_device
*dev
, void *p
)
1183 struct team
*team
= netdev_priv(dev
);
1184 struct team_port
*port
;
1185 struct sockaddr
*addr
= p
;
1187 dev
->addr_assign_type
&= ~NET_ADDR_RANDOM
;
1188 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
1190 list_for_each_entry_rcu(port
, &team
->port_list
, list
)
1191 if (team
->ops
.port_change_mac
)
1192 team
->ops
.port_change_mac(team
, port
);
1197 static int team_change_mtu(struct net_device
*dev
, int new_mtu
)
1199 struct team
*team
= netdev_priv(dev
);
1200 struct team_port
*port
;
1204 * Alhough this is reader, it's guarded by team lock. It's not possible
1205 * to traverse list in reverse under rcu_read_lock
1207 mutex_lock(&team
->lock
);
1208 list_for_each_entry(port
, &team
->port_list
, list
) {
1209 err
= dev_set_mtu(port
->dev
, new_mtu
);
1211 netdev_err(dev
, "Device %s failed to change mtu",
1216 mutex_unlock(&team
->lock
);
1223 list_for_each_entry_continue_reverse(port
, &team
->port_list
, list
)
1224 dev_set_mtu(port
->dev
, dev
->mtu
);
1225 mutex_unlock(&team
->lock
);
1230 static struct rtnl_link_stats64
*
1231 team_get_stats64(struct net_device
*dev
, struct rtnl_link_stats64
*stats
)
1233 struct team
*team
= netdev_priv(dev
);
1234 struct team_pcpu_stats
*p
;
1235 u64 rx_packets
, rx_bytes
, rx_multicast
, tx_packets
, tx_bytes
;
1236 u32 rx_dropped
= 0, tx_dropped
= 0;
1240 for_each_possible_cpu(i
) {
1241 p
= per_cpu_ptr(team
->pcpu_stats
, i
);
1243 start
= u64_stats_fetch_begin_bh(&p
->syncp
);
1244 rx_packets
= p
->rx_packets
;
1245 rx_bytes
= p
->rx_bytes
;
1246 rx_multicast
= p
->rx_multicast
;
1247 tx_packets
= p
->tx_packets
;
1248 tx_bytes
= p
->tx_bytes
;
1249 } while (u64_stats_fetch_retry_bh(&p
->syncp
, start
));
1251 stats
->rx_packets
+= rx_packets
;
1252 stats
->rx_bytes
+= rx_bytes
;
1253 stats
->multicast
+= rx_multicast
;
1254 stats
->tx_packets
+= tx_packets
;
1255 stats
->tx_bytes
+= tx_bytes
;
1257 * rx_dropped & tx_dropped are u32, updated
1258 * without syncp protection.
1260 rx_dropped
+= p
->rx_dropped
;
1261 tx_dropped
+= p
->tx_dropped
;
1263 stats
->rx_dropped
= rx_dropped
;
1264 stats
->tx_dropped
= tx_dropped
;
1268 static int team_vlan_rx_add_vid(struct net_device
*dev
, uint16_t vid
)
1270 struct team
*team
= netdev_priv(dev
);
1271 struct team_port
*port
;
1275 * Alhough this is reader, it's guarded by team lock. It's not possible
1276 * to traverse list in reverse under rcu_read_lock
1278 mutex_lock(&team
->lock
);
1279 list_for_each_entry(port
, &team
->port_list
, list
) {
1280 err
= vlan_vid_add(port
->dev
, vid
);
1284 mutex_unlock(&team
->lock
);
1289 list_for_each_entry_continue_reverse(port
, &team
->port_list
, list
)
1290 vlan_vid_del(port
->dev
, vid
);
1291 mutex_unlock(&team
->lock
);
1296 static int team_vlan_rx_kill_vid(struct net_device
*dev
, uint16_t vid
)
1298 struct team
*team
= netdev_priv(dev
);
1299 struct team_port
*port
;
1302 list_for_each_entry_rcu(port
, &team
->port_list
, list
)
1303 vlan_vid_del(port
->dev
, vid
);
1309 static int team_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
1311 struct team
*team
= netdev_priv(dev
);
1314 mutex_lock(&team
->lock
);
1315 err
= team_port_add(team
, port_dev
);
1316 mutex_unlock(&team
->lock
);
1320 static int team_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
1322 struct team
*team
= netdev_priv(dev
);
1325 mutex_lock(&team
->lock
);
1326 err
= team_port_del(team
, port_dev
);
1327 mutex_unlock(&team
->lock
);
1331 static netdev_features_t
team_fix_features(struct net_device
*dev
,
1332 netdev_features_t features
)
1334 struct team_port
*port
;
1335 struct team
*team
= netdev_priv(dev
);
1336 netdev_features_t mask
;
1339 features
&= ~NETIF_F_ONE_FOR_ALL
;
1340 features
|= NETIF_F_ALL_FOR_ALL
;
1343 list_for_each_entry_rcu(port
, &team
->port_list
, list
) {
1344 features
= netdev_increment_features(features
,
1345 port
->dev
->features
,
1352 static const struct net_device_ops team_netdev_ops
= {
1353 .ndo_init
= team_init
,
1354 .ndo_uninit
= team_uninit
,
1355 .ndo_open
= team_open
,
1356 .ndo_stop
= team_close
,
1357 .ndo_start_xmit
= team_xmit
,
1358 .ndo_change_rx_flags
= team_change_rx_flags
,
1359 .ndo_set_rx_mode
= team_set_rx_mode
,
1360 .ndo_set_mac_address
= team_set_mac_address
,
1361 .ndo_change_mtu
= team_change_mtu
,
1362 .ndo_get_stats64
= team_get_stats64
,
1363 .ndo_vlan_rx_add_vid
= team_vlan_rx_add_vid
,
1364 .ndo_vlan_rx_kill_vid
= team_vlan_rx_kill_vid
,
1365 .ndo_add_slave
= team_add_slave
,
1366 .ndo_del_slave
= team_del_slave
,
1367 .ndo_fix_features
= team_fix_features
,
1371 /***********************
1372 * rt netlink interface
1373 ***********************/
1375 static void team_setup(struct net_device
*dev
)
1379 dev
->netdev_ops
= &team_netdev_ops
;
1380 dev
->destructor
= team_destructor
;
1381 dev
->tx_queue_len
= 0;
1382 dev
->flags
|= IFF_MULTICAST
;
1383 dev
->priv_flags
&= ~(IFF_XMIT_DST_RELEASE
| IFF_TX_SKB_SHARING
);
1386 * Indicate we support unicast address filtering. That way core won't
1387 * bring us to promisc mode in case a unicast addr is added.
1388 * Let this up to underlay drivers.
1390 dev
->priv_flags
|= IFF_UNICAST_FLT
;
1392 dev
->features
|= NETIF_F_LLTX
;
1393 dev
->features
|= NETIF_F_GRO
;
1394 dev
->hw_features
= NETIF_F_HW_VLAN_TX
|
1395 NETIF_F_HW_VLAN_RX
|
1396 NETIF_F_HW_VLAN_FILTER
;
1398 dev
->features
|= dev
->hw_features
;
1401 static int team_newlink(struct net
*src_net
, struct net_device
*dev
,
1402 struct nlattr
*tb
[], struct nlattr
*data
[])
1406 if (tb
[IFLA_ADDRESS
] == NULL
)
1407 eth_hw_addr_random(dev
);
1409 err
= register_netdevice(dev
);
1416 static int team_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1418 if (tb
[IFLA_ADDRESS
]) {
1419 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1421 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1422 return -EADDRNOTAVAIL
;
1427 static struct rtnl_link_ops team_link_ops __read_mostly
= {
1429 .priv_size
= sizeof(struct team
),
1430 .setup
= team_setup
,
1431 .newlink
= team_newlink
,
1432 .validate
= team_validate
,
1436 /***********************************
1437 * Generic netlink custom interface
1438 ***********************************/
1440 static struct genl_family team_nl_family
= {
1441 .id
= GENL_ID_GENERATE
,
1442 .name
= TEAM_GENL_NAME
,
1443 .version
= TEAM_GENL_VERSION
,
1444 .maxattr
= TEAM_ATTR_MAX
,
1448 static const struct nla_policy team_nl_policy
[TEAM_ATTR_MAX
+ 1] = {
1449 [TEAM_ATTR_UNSPEC
] = { .type
= NLA_UNSPEC
, },
1450 [TEAM_ATTR_TEAM_IFINDEX
] = { .type
= NLA_U32
},
1451 [TEAM_ATTR_LIST_OPTION
] = { .type
= NLA_NESTED
},
1452 [TEAM_ATTR_LIST_PORT
] = { .type
= NLA_NESTED
},
1455 static const struct nla_policy
1456 team_nl_option_policy
[TEAM_ATTR_OPTION_MAX
+ 1] = {
1457 [TEAM_ATTR_OPTION_UNSPEC
] = { .type
= NLA_UNSPEC
, },
1458 [TEAM_ATTR_OPTION_NAME
] = {
1460 .len
= TEAM_STRING_MAX_LEN
,
1462 [TEAM_ATTR_OPTION_CHANGED
] = { .type
= NLA_FLAG
},
1463 [TEAM_ATTR_OPTION_TYPE
] = { .type
= NLA_U8
},
1464 [TEAM_ATTR_OPTION_DATA
] = { .type
= NLA_BINARY
},
1467 static int team_nl_cmd_noop(struct sk_buff
*skb
, struct genl_info
*info
)
1469 struct sk_buff
*msg
;
1473 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1477 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
,
1478 &team_nl_family
, 0, TEAM_CMD_NOOP
);
1484 genlmsg_end(msg
, hdr
);
1486 return genlmsg_unicast(genl_info_net(info
), msg
, info
->snd_pid
);
1495 * Netlink cmd functions should be locked by following two functions.
1496 * Since dev gets held here, that ensures dev won't disappear in between.
1498 static struct team
*team_nl_team_get(struct genl_info
*info
)
1500 struct net
*net
= genl_info_net(info
);
1502 struct net_device
*dev
;
1505 if (!info
->attrs
[TEAM_ATTR_TEAM_IFINDEX
])
1508 ifindex
= nla_get_u32(info
->attrs
[TEAM_ATTR_TEAM_IFINDEX
]);
1509 dev
= dev_get_by_index(net
, ifindex
);
1510 if (!dev
|| dev
->netdev_ops
!= &team_netdev_ops
) {
1516 team
= netdev_priv(dev
);
1517 mutex_lock(&team
->lock
);
1521 static void team_nl_team_put(struct team
*team
)
1523 mutex_unlock(&team
->lock
);
1527 static int team_nl_send_generic(struct genl_info
*info
, struct team
*team
,
1528 int (*fill_func
)(struct sk_buff
*skb
,
1529 struct genl_info
*info
,
1530 int flags
, struct team
*team
))
1532 struct sk_buff
*skb
;
1535 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1539 err
= fill_func(skb
, info
, NLM_F_ACK
, team
);
1543 err
= genlmsg_unicast(genl_info_net(info
), skb
, info
->snd_pid
);
1551 static int team_nl_fill_options_get(struct sk_buff
*skb
,
1552 u32 pid
, u32 seq
, int flags
,
1553 struct team
*team
, bool fillall
)
1555 struct nlattr
*option_list
;
1557 struct team_option_inst
*opt_inst
;
1560 hdr
= genlmsg_put(skb
, pid
, seq
, &team_nl_family
, flags
,
1561 TEAM_CMD_OPTIONS_GET
);
1563 return PTR_ERR(hdr
);
1565 if (nla_put_u32(skb
, TEAM_ATTR_TEAM_IFINDEX
, team
->dev
->ifindex
))
1566 goto nla_put_failure
;
1567 option_list
= nla_nest_start(skb
, TEAM_ATTR_LIST_OPTION
);
1571 list_for_each_entry(opt_inst
, &team
->option_inst_list
, list
) {
1572 struct nlattr
*option_item
;
1573 struct team_option
*option
= opt_inst
->option
;
1574 struct team_option_inst_info
*opt_inst_info
;
1575 struct team_gsetter_ctx ctx
;
1577 /* Include only changed options if fill all mode is not on */
1578 if (!fillall
&& !opt_inst
->changed
)
1580 option_item
= nla_nest_start(skb
, TEAM_ATTR_ITEM_OPTION
);
1582 goto nla_put_failure
;
1583 if (nla_put_string(skb
, TEAM_ATTR_OPTION_NAME
, option
->name
))
1584 goto nla_put_failure
;
1585 if (opt_inst
->changed
) {
1586 if (nla_put_flag(skb
, TEAM_ATTR_OPTION_CHANGED
))
1587 goto nla_put_failure
;
1588 opt_inst
->changed
= false;
1590 if (opt_inst
->removed
&&
1591 nla_put_flag(skb
, TEAM_ATTR_OPTION_REMOVED
))
1592 goto nla_put_failure
;
1594 opt_inst_info
= &opt_inst
->info
;
1595 if (opt_inst_info
->port
&&
1596 nla_put_u32(skb
, TEAM_ATTR_OPTION_PORT_IFINDEX
,
1597 opt_inst_info
->port
->dev
->ifindex
))
1598 goto nla_put_failure
;
1599 if (opt_inst
->option
->array_size
&&
1600 nla_put_u32(skb
, TEAM_ATTR_OPTION_ARRAY_INDEX
,
1601 opt_inst_info
->array_index
))
1602 goto nla_put_failure
;
1603 ctx
.info
= opt_inst_info
;
1605 switch (option
->type
) {
1606 case TEAM_OPTION_TYPE_U32
:
1607 if (nla_put_u8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_U32
))
1608 goto nla_put_failure
;
1609 err
= team_option_get(team
, opt_inst
, &ctx
);
1612 if (nla_put_u32(skb
, TEAM_ATTR_OPTION_DATA
,
1614 goto nla_put_failure
;
1616 case TEAM_OPTION_TYPE_STRING
:
1617 if (nla_put_u8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_STRING
))
1618 goto nla_put_failure
;
1619 err
= team_option_get(team
, opt_inst
, &ctx
);
1622 if (nla_put_string(skb
, TEAM_ATTR_OPTION_DATA
,
1624 goto nla_put_failure
;
1626 case TEAM_OPTION_TYPE_BINARY
:
1627 if (nla_put_u8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_BINARY
))
1628 goto nla_put_failure
;
1629 err
= team_option_get(team
, opt_inst
, &ctx
);
1632 if (nla_put(skb
, TEAM_ATTR_OPTION_DATA
,
1633 ctx
.data
.bin_val
.len
, ctx
.data
.bin_val
.ptr
))
1634 goto nla_put_failure
;
1636 case TEAM_OPTION_TYPE_BOOL
:
1637 if (nla_put_u8(skb
, TEAM_ATTR_OPTION_TYPE
, NLA_FLAG
))
1638 goto nla_put_failure
;
1639 err
= team_option_get(team
, opt_inst
, &ctx
);
1642 if (ctx
.data
.bool_val
&&
1643 nla_put_flag(skb
, TEAM_ATTR_OPTION_DATA
))
1644 goto nla_put_failure
;
1649 nla_nest_end(skb
, option_item
);
1652 nla_nest_end(skb
, option_list
);
1653 return genlmsg_end(skb
, hdr
);
1658 genlmsg_cancel(skb
, hdr
);
1662 static int team_nl_fill_options_get_all(struct sk_buff
*skb
,
1663 struct genl_info
*info
, int flags
,
1666 return team_nl_fill_options_get(skb
, info
->snd_pid
,
1667 info
->snd_seq
, NLM_F_ACK
,
1671 static int team_nl_cmd_options_get(struct sk_buff
*skb
, struct genl_info
*info
)
1676 team
= team_nl_team_get(info
);
1680 err
= team_nl_send_generic(info
, team
, team_nl_fill_options_get_all
);
1682 team_nl_team_put(team
);
1687 static int team_nl_cmd_options_set(struct sk_buff
*skb
, struct genl_info
*info
)
1692 struct nlattr
*nl_option
;
1694 team
= team_nl_team_get(info
);
1699 if (!info
->attrs
[TEAM_ATTR_LIST_OPTION
]) {
1704 nla_for_each_nested(nl_option
, info
->attrs
[TEAM_ATTR_LIST_OPTION
], i
) {
1705 struct nlattr
*opt_attrs
[TEAM_ATTR_OPTION_MAX
+ 1];
1706 struct nlattr
*attr
;
1707 struct nlattr
*attr_data
;
1708 enum team_option_type opt_type
;
1709 int opt_port_ifindex
= 0; /* != 0 for per-port options */
1710 u32 opt_array_index
= 0;
1711 bool opt_is_array
= false;
1712 struct team_option_inst
*opt_inst
;
1714 bool opt_found
= false;
1716 if (nla_type(nl_option
) != TEAM_ATTR_ITEM_OPTION
) {
1720 err
= nla_parse_nested(opt_attrs
, TEAM_ATTR_OPTION_MAX
,
1721 nl_option
, team_nl_option_policy
);
1724 if (!opt_attrs
[TEAM_ATTR_OPTION_NAME
] ||
1725 !opt_attrs
[TEAM_ATTR_OPTION_TYPE
]) {
1729 switch (nla_get_u8(opt_attrs
[TEAM_ATTR_OPTION_TYPE
])) {
1731 opt_type
= TEAM_OPTION_TYPE_U32
;
1734 opt_type
= TEAM_OPTION_TYPE_STRING
;
1737 opt_type
= TEAM_OPTION_TYPE_BINARY
;
1740 opt_type
= TEAM_OPTION_TYPE_BOOL
;
1746 attr_data
= opt_attrs
[TEAM_ATTR_OPTION_DATA
];
1747 if (opt_type
!= TEAM_OPTION_TYPE_BOOL
&& !attr_data
) {
1752 opt_name
= nla_data(opt_attrs
[TEAM_ATTR_OPTION_NAME
]);
1753 attr
= opt_attrs
[TEAM_ATTR_OPTION_PORT_IFINDEX
];
1755 opt_port_ifindex
= nla_get_u32(attr
);
1757 attr
= opt_attrs
[TEAM_ATTR_OPTION_ARRAY_INDEX
];
1759 opt_is_array
= true;
1760 opt_array_index
= nla_get_u32(attr
);
1763 list_for_each_entry(opt_inst
, &team
->option_inst_list
, list
) {
1764 struct team_option
*option
= opt_inst
->option
;
1765 struct team_gsetter_ctx ctx
;
1766 struct team_option_inst_info
*opt_inst_info
;
1769 opt_inst_info
= &opt_inst
->info
;
1770 tmp_ifindex
= opt_inst_info
->port
?
1771 opt_inst_info
->port
->dev
->ifindex
: 0;
1772 if (option
->type
!= opt_type
||
1773 strcmp(option
->name
, opt_name
) ||
1774 tmp_ifindex
!= opt_port_ifindex
||
1775 (option
->array_size
&& !opt_is_array
) ||
1776 opt_inst_info
->array_index
!= opt_array_index
)
1779 ctx
.info
= opt_inst_info
;
1781 case TEAM_OPTION_TYPE_U32
:
1782 ctx
.data
.u32_val
= nla_get_u32(attr_data
);
1784 case TEAM_OPTION_TYPE_STRING
:
1785 if (nla_len(attr_data
) > TEAM_STRING_MAX_LEN
) {
1789 ctx
.data
.str_val
= nla_data(attr_data
);
1791 case TEAM_OPTION_TYPE_BINARY
:
1792 ctx
.data
.bin_val
.len
= nla_len(attr_data
);
1793 ctx
.data
.bin_val
.ptr
= nla_data(attr_data
);
1795 case TEAM_OPTION_TYPE_BOOL
:
1796 ctx
.data
.bool_val
= attr_data
? true : false;
1801 err
= team_option_set(team
, opt_inst
, &ctx
);
1812 team_nl_team_put(team
);
1817 static int team_nl_fill_port_list_get(struct sk_buff
*skb
,
1818 u32 pid
, u32 seq
, int flags
,
1822 struct nlattr
*port_list
;
1824 struct team_port
*port
;
1826 hdr
= genlmsg_put(skb
, pid
, seq
, &team_nl_family
, flags
,
1827 TEAM_CMD_PORT_LIST_GET
);
1829 return PTR_ERR(hdr
);
1831 if (nla_put_u32(skb
, TEAM_ATTR_TEAM_IFINDEX
, team
->dev
->ifindex
))
1832 goto nla_put_failure
;
1833 port_list
= nla_nest_start(skb
, TEAM_ATTR_LIST_PORT
);
1837 list_for_each_entry(port
, &team
->port_list
, list
) {
1838 struct nlattr
*port_item
;
1840 /* Include only changed ports if fill all mode is not on */
1841 if (!fillall
&& !port
->changed
)
1843 port_item
= nla_nest_start(skb
, TEAM_ATTR_ITEM_PORT
);
1845 goto nla_put_failure
;
1846 if (nla_put_u32(skb
, TEAM_ATTR_PORT_IFINDEX
, port
->dev
->ifindex
))
1847 goto nla_put_failure
;
1848 if (port
->changed
) {
1849 if (nla_put_flag(skb
, TEAM_ATTR_PORT_CHANGED
))
1850 goto nla_put_failure
;
1851 port
->changed
= false;
1853 if ((port
->removed
&&
1854 nla_put_flag(skb
, TEAM_ATTR_PORT_REMOVED
)) ||
1855 (port
->state
.linkup
&&
1856 nla_put_flag(skb
, TEAM_ATTR_PORT_LINKUP
)) ||
1857 nla_put_u32(skb
, TEAM_ATTR_PORT_SPEED
, port
->state
.speed
) ||
1858 nla_put_u8(skb
, TEAM_ATTR_PORT_DUPLEX
, port
->state
.duplex
))
1859 goto nla_put_failure
;
1860 nla_nest_end(skb
, port_item
);
1863 nla_nest_end(skb
, port_list
);
1864 return genlmsg_end(skb
, hdr
);
1867 genlmsg_cancel(skb
, hdr
);
1871 static int team_nl_fill_port_list_get_all(struct sk_buff
*skb
,
1872 struct genl_info
*info
, int flags
,
1875 return team_nl_fill_port_list_get(skb
, info
->snd_pid
,
1876 info
->snd_seq
, NLM_F_ACK
,
1880 static int team_nl_cmd_port_list_get(struct sk_buff
*skb
,
1881 struct genl_info
*info
)
1886 team
= team_nl_team_get(info
);
1890 err
= team_nl_send_generic(info
, team
, team_nl_fill_port_list_get_all
);
1892 team_nl_team_put(team
);
1897 static struct genl_ops team_nl_ops
[] = {
1899 .cmd
= TEAM_CMD_NOOP
,
1900 .doit
= team_nl_cmd_noop
,
1901 .policy
= team_nl_policy
,
1904 .cmd
= TEAM_CMD_OPTIONS_SET
,
1905 .doit
= team_nl_cmd_options_set
,
1906 .policy
= team_nl_policy
,
1907 .flags
= GENL_ADMIN_PERM
,
1910 .cmd
= TEAM_CMD_OPTIONS_GET
,
1911 .doit
= team_nl_cmd_options_get
,
1912 .policy
= team_nl_policy
,
1913 .flags
= GENL_ADMIN_PERM
,
1916 .cmd
= TEAM_CMD_PORT_LIST_GET
,
1917 .doit
= team_nl_cmd_port_list_get
,
1918 .policy
= team_nl_policy
,
1919 .flags
= GENL_ADMIN_PERM
,
1923 static struct genl_multicast_group team_change_event_mcgrp
= {
1924 .name
= TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME
,
1927 static int team_nl_send_event_options_get(struct team
*team
)
1929 struct sk_buff
*skb
;
1931 struct net
*net
= dev_net(team
->dev
);
1933 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1937 err
= team_nl_fill_options_get(skb
, 0, 0, 0, team
, false);
1941 err
= genlmsg_multicast_netns(net
, skb
, 0, team_change_event_mcgrp
.id
,
1950 static int team_nl_send_event_port_list_get(struct team
*team
)
1952 struct sk_buff
*skb
;
1954 struct net
*net
= dev_net(team
->dev
);
1956 skb
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1960 err
= team_nl_fill_port_list_get(skb
, 0, 0, 0, team
, false);
1964 err
= genlmsg_multicast_netns(net
, skb
, 0, team_change_event_mcgrp
.id
,
1973 static int team_nl_init(void)
1977 err
= genl_register_family_with_ops(&team_nl_family
, team_nl_ops
,
1978 ARRAY_SIZE(team_nl_ops
));
1982 err
= genl_register_mc_group(&team_nl_family
, &team_change_event_mcgrp
);
1984 goto err_change_event_grp_reg
;
1988 err_change_event_grp_reg
:
1989 genl_unregister_family(&team_nl_family
);
1994 static void team_nl_fini(void)
1996 genl_unregister_family(&team_nl_family
);
2004 static void __team_options_change_check(struct team
*team
)
2008 err
= team_nl_send_event_options_get(team
);
2010 netdev_warn(team
->dev
, "Failed to send options change via netlink\n");
2013 /* rtnl lock is held */
2014 static void __team_port_change_check(struct team_port
*port
, bool linkup
)
2018 if (!port
->removed
&& port
->state
.linkup
== linkup
)
2021 port
->changed
= true;
2022 port
->state
.linkup
= linkup
;
2023 team_refresh_port_linkup(port
);
2025 struct ethtool_cmd ecmd
;
2027 err
= __ethtool_get_settings(port
->dev
, &ecmd
);
2029 port
->state
.speed
= ethtool_cmd_speed(&ecmd
);
2030 port
->state
.duplex
= ecmd
.duplex
;
2034 port
->state
.speed
= 0;
2035 port
->state
.duplex
= 0;
2038 err
= team_nl_send_event_port_list_get(port
->team
);
2040 netdev_warn(port
->team
->dev
, "Failed to send port change of device %s via netlink\n",
2045 static void team_port_change_check(struct team_port
*port
, bool linkup
)
2047 struct team
*team
= port
->team
;
2049 mutex_lock(&team
->lock
);
2050 __team_port_change_check(port
, linkup
);
2051 mutex_unlock(&team
->lock
);
2054 /************************************
2055 * Net device notifier event handler
2056 ************************************/
2058 static int team_device_event(struct notifier_block
*unused
,
2059 unsigned long event
, void *ptr
)
2061 struct net_device
*dev
= (struct net_device
*) ptr
;
2062 struct team_port
*port
;
2064 port
= team_port_get_rtnl(dev
);
2070 if (netif_carrier_ok(dev
))
2071 team_port_change_check(port
, true);
2073 team_port_change_check(port
, false);
2075 if (netif_running(port
->dev
))
2076 team_port_change_check(port
,
2077 !!netif_carrier_ok(port
->dev
));
2079 case NETDEV_UNREGISTER
:
2080 team_del_slave(port
->team
->dev
, dev
);
2082 case NETDEV_FEAT_CHANGE
:
2083 team_compute_features(port
->team
);
2085 case NETDEV_CHANGEMTU
:
2086 /* Forbid to change mtu of underlaying device */
2088 case NETDEV_PRE_TYPE_CHANGE
:
2089 /* Forbid to change type of underlaying device */
2095 static struct notifier_block team_notifier_block __read_mostly
= {
2096 .notifier_call
= team_device_event
,
2100 /***********************
2101 * Module init and exit
2102 ***********************/
2104 static int __init
team_module_init(void)
2108 register_netdevice_notifier(&team_notifier_block
);
2110 err
= rtnl_link_register(&team_link_ops
);
2114 err
= team_nl_init();
2121 rtnl_link_unregister(&team_link_ops
);
2124 unregister_netdevice_notifier(&team_notifier_block
);
2129 static void __exit
team_module_exit(void)
2132 rtnl_link_unregister(&team_link_ops
);
2133 unregister_netdevice_notifier(&team_notifier_block
);
2136 module_init(team_module_init
);
2137 module_exit(team_module_exit
);
2139 MODULE_LICENSE("GPL v2");
2140 MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
2141 MODULE_DESCRIPTION("Ethernet team device driver");
2142 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);