2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #include <linux/skbuff.h>
24 #include <linux/if_ether.h>
25 #include <linux/netdevice.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_bonding.h>
30 #include <linux/pkt_sched.h>
31 #include <net/net_namespace.h>
32 #include <net/bonding.h>
33 #include <net/bond_3ad.h>
35 /* General definitions */
36 #define AD_SHORT_TIMEOUT 1
37 #define AD_LONG_TIMEOUT 0
38 #define AD_STANDBY 0x2
39 #define AD_MAX_TX_IN_SECOND 3
40 #define AD_COLLECTOR_MAX_DELAY 0
42 /* Timer definitions (43.4.4 in the 802.3ad standard) */
43 #define AD_FAST_PERIODIC_TIME 1
44 #define AD_SLOW_PERIODIC_TIME 30
45 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
46 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
47 #define AD_CHURN_DETECTION_TIME 60
48 #define AD_AGGREGATE_WAIT_TIME 2
50 /* Port state definitions (43.4.2.2 in the 802.3ad standard) */
51 #define AD_STATE_LACP_ACTIVITY 0x1
52 #define AD_STATE_LACP_TIMEOUT 0x2
53 #define AD_STATE_AGGREGATION 0x4
54 #define AD_STATE_SYNCHRONIZATION 0x8
55 #define AD_STATE_COLLECTING 0x10
56 #define AD_STATE_DISTRIBUTING 0x20
57 #define AD_STATE_DEFAULTED 0x40
58 #define AD_STATE_EXPIRED 0x80
60 /* Port Variables definitions used by the State Machines (43.4.7 in the
63 #define AD_PORT_BEGIN 0x1
64 #define AD_PORT_LACP_ENABLED 0x2
65 #define AD_PORT_ACTOR_CHURN 0x4
66 #define AD_PORT_PARTNER_CHURN 0x8
67 #define AD_PORT_READY 0x10
68 #define AD_PORT_READY_N 0x20
69 #define AD_PORT_MATCHED 0x40
70 #define AD_PORT_STANDBY 0x80
71 #define AD_PORT_SELECTED 0x100
72 #define AD_PORT_MOVED 0x200
73 #define AD_PORT_CHURNED (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
75 /* Port Key definitions
76 * key is determined according to the link speed, duplex and
77 * user key (which is yet not supported)
78 * --------------------------------------------------------------
79 * Port key | User key (10 bits) | Speed (5 bits) | Duplex|
80 * --------------------------------------------------------------
83 #define AD_DUPLEX_KEY_MASKS 0x1
84 #define AD_SPEED_KEY_MASKS 0x3E
85 #define AD_USER_KEY_MASKS 0xFFC0
87 enum ad_link_speed_type
{
88 AD_LINK_SPEED_1MBPS
= 1,
90 AD_LINK_SPEED_100MBPS
,
91 AD_LINK_SPEED_1000MBPS
,
92 AD_LINK_SPEED_2500MBPS
,
93 AD_LINK_SPEED_5000MBPS
,
94 AD_LINK_SPEED_10000MBPS
,
95 AD_LINK_SPEED_20000MBPS
,
96 AD_LINK_SPEED_25000MBPS
,
97 AD_LINK_SPEED_40000MBPS
,
98 AD_LINK_SPEED_50000MBPS
,
99 AD_LINK_SPEED_56000MBPS
,
100 AD_LINK_SPEED_100000MBPS
,
103 /* compare MAC addresses */
104 #define MAC_ADDRESS_EQUAL(A, B) \
105 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
107 static const u8 null_mac_addr
[ETH_ALEN
+ 2] __long_aligned
= {
110 static u16 ad_ticks_per_sec
;
111 static const int ad_delta_in_ticks
= (AD_TIMER_INTERVAL
* HZ
) / 1000;
113 static const u8 lacpdu_mcast_addr
[ETH_ALEN
+ 2] __long_aligned
=
114 MULTICAST_LACPDU_ADDR
;
116 /* ================= main 802.3ad protocol functions ================== */
117 static int ad_lacpdu_send(struct port
*port
);
118 static int ad_marker_send(struct port
*port
, struct bond_marker
*marker
);
119 static void ad_mux_machine(struct port
*port
, bool *update_slave_arr
);
120 static void ad_rx_machine(struct lacpdu
*lacpdu
, struct port
*port
);
121 static void ad_tx_machine(struct port
*port
);
122 static void ad_periodic_machine(struct port
*port
);
123 static void ad_port_selection_logic(struct port
*port
, bool *update_slave_arr
);
124 static void ad_agg_selection_logic(struct aggregator
*aggregator
,
125 bool *update_slave_arr
);
126 static void ad_clear_agg(struct aggregator
*aggregator
);
127 static void ad_initialize_agg(struct aggregator
*aggregator
);
128 static void ad_initialize_port(struct port
*port
, int lacp_fast
);
129 static void ad_enable_collecting_distributing(struct port
*port
,
130 bool *update_slave_arr
);
131 static void ad_disable_collecting_distributing(struct port
*port
,
132 bool *update_slave_arr
);
133 static void ad_marker_info_received(struct bond_marker
*marker_info
,
135 static void ad_marker_response_received(struct bond_marker
*marker
,
137 static void ad_update_actor_keys(struct port
*port
, bool reset
);
140 /* ================= api to bonding and kernel code ================== */
143 * __get_bond_by_port - get the port's bonding struct
144 * @port: the port we're looking at
146 * Return @port's bonding struct, or %NULL if it can't be found.
148 static inline struct bonding
*__get_bond_by_port(struct port
*port
)
150 if (port
->slave
== NULL
)
153 return bond_get_bond_by_slave(port
->slave
);
157 * __get_first_agg - get the first aggregator in the bond
158 * @bond: the bond we're looking at
160 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
162 * The caller must hold RCU or RTNL lock.
164 static inline struct aggregator
*__get_first_agg(struct port
*port
)
166 struct bonding
*bond
= __get_bond_by_port(port
);
167 struct slave
*first_slave
;
168 struct aggregator
*agg
;
170 /* If there's no bond for this port, or bond has no slaves */
175 first_slave
= bond_first_slave_rcu(bond
);
176 agg
= first_slave
? &(SLAVE_AD_INFO(first_slave
)->aggregator
) : NULL
;
183 * __agg_has_partner - see if we have a partner
184 * @agg: the agregator we're looking at
186 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
187 * address for the partner). Return 0 if not.
189 static inline int __agg_has_partner(struct aggregator
*agg
)
191 return !is_zero_ether_addr(agg
->partner_system
.mac_addr_value
);
195 * __disable_port - disable the port's slave
196 * @port: the port we're looking at
198 static inline void __disable_port(struct port
*port
)
200 bond_set_slave_inactive_flags(port
->slave
, BOND_SLAVE_NOTIFY_LATER
);
204 * __enable_port - enable the port's slave, if it's up
205 * @port: the port we're looking at
207 static inline void __enable_port(struct port
*port
)
209 struct slave
*slave
= port
->slave
;
211 if ((slave
->link
== BOND_LINK_UP
) && bond_slave_is_up(slave
))
212 bond_set_slave_active_flags(slave
, BOND_SLAVE_NOTIFY_LATER
);
216 * __port_is_enabled - check if the port's slave is in active state
217 * @port: the port we're looking at
219 static inline int __port_is_enabled(struct port
*port
)
221 return bond_is_active_slave(port
->slave
);
225 * __get_agg_selection_mode - get the aggregator selection mode
226 * @port: the port we're looking at
228 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
230 static inline u32
__get_agg_selection_mode(struct port
*port
)
232 struct bonding
*bond
= __get_bond_by_port(port
);
235 return BOND_AD_STABLE
;
237 return bond
->params
.ad_select
;
241 * __check_agg_selection_timer - check if the selection timer has expired
242 * @port: the port we're looking at
244 static inline int __check_agg_selection_timer(struct port
*port
)
246 struct bonding
*bond
= __get_bond_by_port(port
);
251 return BOND_AD_INFO(bond
).agg_select_timer
? 1 : 0;
255 * __get_link_speed - get a port's speed
256 * @port: the port we're looking at
258 * Return @port's speed in 802.3ad enum format. i.e. one of:
260 * %AD_LINK_SPEED_10MBPS,
261 * %AD_LINK_SPEED_100MBPS,
262 * %AD_LINK_SPEED_1000MBPS,
263 * %AD_LINK_SPEED_2500MBPS,
264 * %AD_LINK_SPEED_5000MBPS,
265 * %AD_LINK_SPEED_10000MBPS
266 * %AD_LINK_SPEED_20000MBPS
267 * %AD_LINK_SPEED_25000MBPS
268 * %AD_LINK_SPEED_40000MBPS
269 * %AD_LINK_SPEED_50000MBPS
270 * %AD_LINK_SPEED_56000MBPS
271 * %AD_LINK_SPEED_100000MBPS
273 static u16
__get_link_speed(struct port
*port
)
275 struct slave
*slave
= port
->slave
;
278 /* this if covers only a special case: when the configuration starts
279 * with link down, it sets the speed to 0.
280 * This is done in spite of the fact that the e100 driver reports 0
281 * to be compatible with MVT in the future.
283 if (slave
->link
!= BOND_LINK_UP
)
286 switch (slave
->speed
) {
288 speed
= AD_LINK_SPEED_10MBPS
;
292 speed
= AD_LINK_SPEED_100MBPS
;
296 speed
= AD_LINK_SPEED_1000MBPS
;
300 speed
= AD_LINK_SPEED_2500MBPS
;
304 speed
= AD_LINK_SPEED_5000MBPS
;
308 speed
= AD_LINK_SPEED_10000MBPS
;
312 speed
= AD_LINK_SPEED_20000MBPS
;
316 speed
= AD_LINK_SPEED_25000MBPS
;
320 speed
= AD_LINK_SPEED_40000MBPS
;
324 speed
= AD_LINK_SPEED_50000MBPS
;
328 speed
= AD_LINK_SPEED_56000MBPS
;
332 speed
= AD_LINK_SPEED_100000MBPS
;
336 /* unknown speed value from ethtool. shouldn't happen */
342 netdev_dbg(slave
->bond
->dev
, "Port %d Received link speed %d update from adapter\n",
343 port
->actor_port_number
, speed
);
348 * __get_duplex - get a port's duplex
349 * @port: the port we're looking at
351 * Return @port's duplex in 802.3ad bitmask format. i.e.:
352 * 0x01 if in full duplex
355 static u8
__get_duplex(struct port
*port
)
357 struct slave
*slave
= port
->slave
;
360 /* handling a special case: when the configuration starts with
361 * link down, it sets the duplex to 0.
363 if (slave
->link
== BOND_LINK_UP
) {
364 switch (slave
->duplex
) {
367 netdev_dbg(slave
->bond
->dev
, "Port %d Received status full duplex update from adapter\n",
368 port
->actor_port_number
);
373 netdev_dbg(slave
->bond
->dev
, "Port %d Received status NOT full duplex update from adapter\n",
374 port
->actor_port_number
);
381 static void __ad_actor_update_port(struct port
*port
)
383 const struct bonding
*bond
= bond_get_bond_by_slave(port
->slave
);
385 port
->actor_system
= BOND_AD_INFO(bond
).system
.sys_mac_addr
;
386 port
->actor_system_priority
= BOND_AD_INFO(bond
).system
.sys_priority
;
392 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
393 * @timer_type: which timer to operate
394 * @par: timer parameter. see below
396 * If @timer_type is %current_while_timer, @par indicates long/short timer.
397 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
398 * %SLOW_PERIODIC_TIME.
400 static u16
__ad_timer_to_ticks(u16 timer_type
, u16 par
)
402 u16 retval
= 0; /* to silence the compiler */
404 switch (timer_type
) {
405 case AD_CURRENT_WHILE_TIMER
: /* for rx machine usage */
407 retval
= (AD_SHORT_TIMEOUT_TIME
*ad_ticks_per_sec
);
409 retval
= (AD_LONG_TIMEOUT_TIME
*ad_ticks_per_sec
);
411 case AD_ACTOR_CHURN_TIMER
: /* for local churn machine */
412 retval
= (AD_CHURN_DETECTION_TIME
*ad_ticks_per_sec
);
414 case AD_PERIODIC_TIMER
: /* for periodic machine */
415 retval
= (par
*ad_ticks_per_sec
); /* long timeout */
417 case AD_PARTNER_CHURN_TIMER
: /* for remote churn machine */
418 retval
= (AD_CHURN_DETECTION_TIME
*ad_ticks_per_sec
);
420 case AD_WAIT_WHILE_TIMER
: /* for selection machine */
421 retval
= (AD_AGGREGATE_WAIT_TIME
*ad_ticks_per_sec
);
429 /* ================= ad_rx_machine helper functions ================== */
432 * __choose_matched - update a port's matched variable from a received lacpdu
433 * @lacpdu: the lacpdu we've received
434 * @port: the port we're looking at
436 * Update the value of the matched variable, using parameter values from a
437 * newly received lacpdu. Parameter values for the partner carried in the
438 * received PDU are compared with the corresponding operational parameter
439 * values for the actor. Matched is set to TRUE if all of these parameters
440 * match and the PDU parameter partner_state.aggregation has the same value as
441 * actor_oper_port_state.aggregation and lacp will actively maintain the link
442 * in the aggregation. Matched is also set to TRUE if the value of
443 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
444 * an individual link and lacp will actively maintain the link. Otherwise,
445 * matched is set to FALSE. LACP is considered to be actively maintaining the
446 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
447 * the actor's actor_oper_port_state.lacp_activity and the PDU's
448 * partner_state.lacp_activity variables are TRUE.
450 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
451 * used here to implement the language from 802.3ad 43.4.9 that requires
452 * recordPDU to "match" the LACPDU parameters to the stored values.
454 static void __choose_matched(struct lacpdu
*lacpdu
, struct port
*port
)
456 /* check if all parameters are alike
457 * or this is individual link(aggregation == FALSE)
458 * then update the state machine Matched variable.
460 if (((ntohs(lacpdu
->partner_port
) == port
->actor_port_number
) &&
461 (ntohs(lacpdu
->partner_port_priority
) == port
->actor_port_priority
) &&
462 MAC_ADDRESS_EQUAL(&(lacpdu
->partner_system
), &(port
->actor_system
)) &&
463 (ntohs(lacpdu
->partner_system_priority
) == port
->actor_system_priority
) &&
464 (ntohs(lacpdu
->partner_key
) == port
->actor_oper_port_key
) &&
465 ((lacpdu
->partner_state
& AD_STATE_AGGREGATION
) == (port
->actor_oper_port_state
& AD_STATE_AGGREGATION
))) ||
466 ((lacpdu
->actor_state
& AD_STATE_AGGREGATION
) == 0)
468 port
->sm_vars
|= AD_PORT_MATCHED
;
470 port
->sm_vars
&= ~AD_PORT_MATCHED
;
475 * __record_pdu - record parameters from a received lacpdu
476 * @lacpdu: the lacpdu we've received
477 * @port: the port we're looking at
479 * Record the parameter values for the Actor carried in a received lacpdu as
480 * the current partner operational parameter values and sets
481 * actor_oper_port_state.defaulted to FALSE.
483 static void __record_pdu(struct lacpdu
*lacpdu
, struct port
*port
)
485 if (lacpdu
&& port
) {
486 struct port_params
*partner
= &port
->partner_oper
;
488 __choose_matched(lacpdu
, port
);
489 /* record the new parameter values for the partner
492 partner
->port_number
= ntohs(lacpdu
->actor_port
);
493 partner
->port_priority
= ntohs(lacpdu
->actor_port_priority
);
494 partner
->system
= lacpdu
->actor_system
;
495 partner
->system_priority
= ntohs(lacpdu
->actor_system_priority
);
496 partner
->key
= ntohs(lacpdu
->actor_key
);
497 partner
->port_state
= lacpdu
->actor_state
;
499 /* set actor_oper_port_state.defaulted to FALSE */
500 port
->actor_oper_port_state
&= ~AD_STATE_DEFAULTED
;
502 /* set the partner sync. to on if the partner is sync,
503 * and the port is matched
505 if ((port
->sm_vars
& AD_PORT_MATCHED
) &&
506 (lacpdu
->actor_state
& AD_STATE_SYNCHRONIZATION
)) {
507 partner
->port_state
|= AD_STATE_SYNCHRONIZATION
;
508 pr_debug("%s partner sync=1\n", port
->slave
->dev
->name
);
510 partner
->port_state
&= ~AD_STATE_SYNCHRONIZATION
;
511 pr_debug("%s partner sync=0\n", port
->slave
->dev
->name
);
517 * __record_default - record default parameters
518 * @port: the port we're looking at
520 * This function records the default parameter values for the partner carried
521 * in the Partner Admin parameters as the current partner operational parameter
522 * values and sets actor_oper_port_state.defaulted to TRUE.
524 static void __record_default(struct port
*port
)
527 /* record the partner admin parameters */
528 memcpy(&port
->partner_oper
, &port
->partner_admin
,
529 sizeof(struct port_params
));
531 /* set actor_oper_port_state.defaulted to true */
532 port
->actor_oper_port_state
|= AD_STATE_DEFAULTED
;
537 * __update_selected - update a port's Selected variable from a received lacpdu
538 * @lacpdu: the lacpdu we've received
539 * @port: the port we're looking at
541 * Update the value of the selected variable, using parameter values from a
542 * newly received lacpdu. The parameter values for the Actor carried in the
543 * received PDU are compared with the corresponding operational parameter
544 * values for the ports partner. If one or more of the comparisons shows that
545 * the value(s) received in the PDU differ from the current operational values,
546 * then selected is set to FALSE and actor_oper_port_state.synchronization is
547 * set to out_of_sync. Otherwise, selected remains unchanged.
549 static void __update_selected(struct lacpdu
*lacpdu
, struct port
*port
)
551 if (lacpdu
&& port
) {
552 const struct port_params
*partner
= &port
->partner_oper
;
554 /* check if any parameter is different then
555 * update the state machine selected variable.
557 if (ntohs(lacpdu
->actor_port
) != partner
->port_number
||
558 ntohs(lacpdu
->actor_port_priority
) != partner
->port_priority
||
559 !MAC_ADDRESS_EQUAL(&lacpdu
->actor_system
, &partner
->system
) ||
560 ntohs(lacpdu
->actor_system_priority
) != partner
->system_priority
||
561 ntohs(lacpdu
->actor_key
) != partner
->key
||
562 (lacpdu
->actor_state
& AD_STATE_AGGREGATION
) != (partner
->port_state
& AD_STATE_AGGREGATION
)) {
563 port
->sm_vars
&= ~AD_PORT_SELECTED
;
569 * __update_default_selected - update a port's Selected variable from Partner
570 * @port: the port we're looking at
572 * This function updates the value of the selected variable, using the partner
573 * administrative parameter values. The administrative values are compared with
574 * the corresponding operational parameter values for the partner. If one or
575 * more of the comparisons shows that the administrative value(s) differ from
576 * the current operational values, then Selected is set to FALSE and
577 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
578 * Selected remains unchanged.
580 static void __update_default_selected(struct port
*port
)
583 const struct port_params
*admin
= &port
->partner_admin
;
584 const struct port_params
*oper
= &port
->partner_oper
;
586 /* check if any parameter is different then
587 * update the state machine selected variable.
589 if (admin
->port_number
!= oper
->port_number
||
590 admin
->port_priority
!= oper
->port_priority
||
591 !MAC_ADDRESS_EQUAL(&admin
->system
, &oper
->system
) ||
592 admin
->system_priority
!= oper
->system_priority
||
593 admin
->key
!= oper
->key
||
594 (admin
->port_state
& AD_STATE_AGGREGATION
)
595 != (oper
->port_state
& AD_STATE_AGGREGATION
)) {
596 port
->sm_vars
&= ~AD_PORT_SELECTED
;
602 * __update_ntt - update a port's ntt variable from a received lacpdu
603 * @lacpdu: the lacpdu we've received
604 * @port: the port we're looking at
606 * Updates the value of the ntt variable, using parameter values from a newly
607 * received lacpdu. The parameter values for the partner carried in the
608 * received PDU are compared with the corresponding operational parameter
609 * values for the Actor. If one or more of the comparisons shows that the
610 * value(s) received in the PDU differ from the current operational values,
611 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
613 static void __update_ntt(struct lacpdu
*lacpdu
, struct port
*port
)
615 /* validate lacpdu and port */
616 if (lacpdu
&& port
) {
617 /* check if any parameter is different then
618 * update the port->ntt.
620 if ((ntohs(lacpdu
->partner_port
) != port
->actor_port_number
) ||
621 (ntohs(lacpdu
->partner_port_priority
) != port
->actor_port_priority
) ||
622 !MAC_ADDRESS_EQUAL(&(lacpdu
->partner_system
), &(port
->actor_system
)) ||
623 (ntohs(lacpdu
->partner_system_priority
) != port
->actor_system_priority
) ||
624 (ntohs(lacpdu
->partner_key
) != port
->actor_oper_port_key
) ||
625 ((lacpdu
->partner_state
& AD_STATE_LACP_ACTIVITY
) != (port
->actor_oper_port_state
& AD_STATE_LACP_ACTIVITY
)) ||
626 ((lacpdu
->partner_state
& AD_STATE_LACP_TIMEOUT
) != (port
->actor_oper_port_state
& AD_STATE_LACP_TIMEOUT
)) ||
627 ((lacpdu
->partner_state
& AD_STATE_SYNCHRONIZATION
) != (port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
)) ||
628 ((lacpdu
->partner_state
& AD_STATE_AGGREGATION
) != (port
->actor_oper_port_state
& AD_STATE_AGGREGATION
))
636 * __agg_ports_are_ready - check if all ports in an aggregator are ready
637 * @aggregator: the aggregator we're looking at
640 static int __agg_ports_are_ready(struct aggregator
*aggregator
)
646 /* scan all ports in this aggregator to verfy if they are
649 for (port
= aggregator
->lag_ports
;
651 port
= port
->next_port_in_aggregator
) {
652 if (!(port
->sm_vars
& AD_PORT_READY_N
)) {
663 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
664 * @aggregator: the aggregator we're looking at
665 * @val: Should the ports' ready bit be set on or off
668 static void __set_agg_ports_ready(struct aggregator
*aggregator
, int val
)
672 for (port
= aggregator
->lag_ports
; port
;
673 port
= port
->next_port_in_aggregator
) {
675 port
->sm_vars
|= AD_PORT_READY
;
677 port
->sm_vars
&= ~AD_PORT_READY
;
681 static int __agg_active_ports(struct aggregator
*agg
)
686 for (port
= agg
->lag_ports
; port
;
687 port
= port
->next_port_in_aggregator
) {
688 if (port
->is_enabled
)
696 * __get_agg_bandwidth - get the total bandwidth of an aggregator
697 * @aggregator: the aggregator we're looking at
700 static u32
__get_agg_bandwidth(struct aggregator
*aggregator
)
702 int nports
= __agg_active_ports(aggregator
);
706 switch (__get_link_speed(aggregator
->lag_ports
)) {
707 case AD_LINK_SPEED_1MBPS
:
710 case AD_LINK_SPEED_10MBPS
:
711 bandwidth
= nports
* 10;
713 case AD_LINK_SPEED_100MBPS
:
714 bandwidth
= nports
* 100;
716 case AD_LINK_SPEED_1000MBPS
:
717 bandwidth
= nports
* 1000;
719 case AD_LINK_SPEED_2500MBPS
:
720 bandwidth
= nports
* 2500;
722 case AD_LINK_SPEED_5000MBPS
:
723 bandwidth
= nports
* 5000;
725 case AD_LINK_SPEED_10000MBPS
:
726 bandwidth
= nports
* 10000;
728 case AD_LINK_SPEED_20000MBPS
:
729 bandwidth
= nports
* 20000;
731 case AD_LINK_SPEED_25000MBPS
:
732 bandwidth
= nports
* 25000;
734 case AD_LINK_SPEED_40000MBPS
:
735 bandwidth
= nports
* 40000;
737 case AD_LINK_SPEED_50000MBPS
:
738 bandwidth
= nports
* 50000;
740 case AD_LINK_SPEED_56000MBPS
:
741 bandwidth
= nports
* 56000;
743 case AD_LINK_SPEED_100000MBPS
:
744 bandwidth
= nports
* 100000;
747 bandwidth
= 0; /* to silence the compiler */
754 * __get_active_agg - get the current active aggregator
755 * @aggregator: the aggregator we're looking at
757 * Caller must hold RCU lock.
759 static struct aggregator
*__get_active_agg(struct aggregator
*aggregator
)
761 struct bonding
*bond
= aggregator
->slave
->bond
;
762 struct list_head
*iter
;
765 bond_for_each_slave_rcu(bond
, slave
, iter
)
766 if (SLAVE_AD_INFO(slave
)->aggregator
.is_active
)
767 return &(SLAVE_AD_INFO(slave
)->aggregator
);
773 * __update_lacpdu_from_port - update a port's lacpdu fields
774 * @port: the port we're looking at
776 static inline void __update_lacpdu_from_port(struct port
*port
)
778 struct lacpdu
*lacpdu
= &port
->lacpdu
;
779 const struct port_params
*partner
= &port
->partner_oper
;
781 /* update current actual Actor parameters
782 * lacpdu->subtype initialized
783 * lacpdu->version_number initialized
784 * lacpdu->tlv_type_actor_info initialized
785 * lacpdu->actor_information_length initialized
788 lacpdu
->actor_system_priority
= htons(port
->actor_system_priority
);
789 lacpdu
->actor_system
= port
->actor_system
;
790 lacpdu
->actor_key
= htons(port
->actor_oper_port_key
);
791 lacpdu
->actor_port_priority
= htons(port
->actor_port_priority
);
792 lacpdu
->actor_port
= htons(port
->actor_port_number
);
793 lacpdu
->actor_state
= port
->actor_oper_port_state
;
794 pr_debug("update lacpdu: %s, actor port state %x\n",
795 port
->slave
->dev
->name
, port
->actor_oper_port_state
);
797 /* lacpdu->reserved_3_1 initialized
798 * lacpdu->tlv_type_partner_info initialized
799 * lacpdu->partner_information_length initialized
802 lacpdu
->partner_system_priority
= htons(partner
->system_priority
);
803 lacpdu
->partner_system
= partner
->system
;
804 lacpdu
->partner_key
= htons(partner
->key
);
805 lacpdu
->partner_port_priority
= htons(partner
->port_priority
);
806 lacpdu
->partner_port
= htons(partner
->port_number
);
807 lacpdu
->partner_state
= partner
->port_state
;
809 /* lacpdu->reserved_3_2 initialized
810 * lacpdu->tlv_type_collector_info initialized
811 * lacpdu->collector_information_length initialized
812 * collector_max_delay initialized
813 * reserved_12[12] initialized
814 * tlv_type_terminator initialized
815 * terminator_length initialized
816 * reserved_50[50] initialized
820 /* ================= main 802.3ad protocol code ========================= */
823 * ad_lacpdu_send - send out a lacpdu packet on a given port
824 * @port: the port we're looking at
826 * Returns: 0 on success
829 static int ad_lacpdu_send(struct port
*port
)
831 struct slave
*slave
= port
->slave
;
833 struct lacpdu_header
*lacpdu_header
;
834 int length
= sizeof(struct lacpdu_header
);
836 skb
= dev_alloc_skb(length
);
840 skb
->dev
= slave
->dev
;
841 skb_reset_mac_header(skb
);
842 skb
->network_header
= skb
->mac_header
+ ETH_HLEN
;
843 skb
->protocol
= PKT_TYPE_LACPDU
;
844 skb
->priority
= TC_PRIO_CONTROL
;
846 lacpdu_header
= (struct lacpdu_header
*)skb_put(skb
, length
);
848 ether_addr_copy(lacpdu_header
->hdr
.h_dest
, lacpdu_mcast_addr
);
849 /* Note: source address is set to be the member's PERMANENT address,
850 * because we use it to identify loopback lacpdus in receive.
852 ether_addr_copy(lacpdu_header
->hdr
.h_source
, slave
->perm_hwaddr
);
853 lacpdu_header
->hdr
.h_proto
= PKT_TYPE_LACPDU
;
855 lacpdu_header
->lacpdu
= port
->lacpdu
;
863 * ad_marker_send - send marker information/response on a given port
864 * @port: the port we're looking at
865 * @marker: marker data to send
867 * Returns: 0 on success
870 static int ad_marker_send(struct port
*port
, struct bond_marker
*marker
)
872 struct slave
*slave
= port
->slave
;
874 struct bond_marker_header
*marker_header
;
875 int length
= sizeof(struct bond_marker_header
);
877 skb
= dev_alloc_skb(length
+ 16);
881 skb_reserve(skb
, 16);
883 skb
->dev
= slave
->dev
;
884 skb_reset_mac_header(skb
);
885 skb
->network_header
= skb
->mac_header
+ ETH_HLEN
;
886 skb
->protocol
= PKT_TYPE_LACPDU
;
888 marker_header
= (struct bond_marker_header
*)skb_put(skb
, length
);
890 ether_addr_copy(marker_header
->hdr
.h_dest
, lacpdu_mcast_addr
);
891 /* Note: source address is set to be the member's PERMANENT address,
892 * because we use it to identify loopback MARKERs in receive.
894 ether_addr_copy(marker_header
->hdr
.h_source
, slave
->perm_hwaddr
);
895 marker_header
->hdr
.h_proto
= PKT_TYPE_LACPDU
;
897 marker_header
->marker
= *marker
;
905 * ad_mux_machine - handle a port's mux state machine
906 * @port: the port we're looking at
907 * @update_slave_arr: Does slave array need update?
909 static void ad_mux_machine(struct port
*port
, bool *update_slave_arr
)
911 mux_states_t last_state
;
913 /* keep current State Machine state to compare later if it was
916 last_state
= port
->sm_mux_state
;
918 if (port
->sm_vars
& AD_PORT_BEGIN
) {
919 port
->sm_mux_state
= AD_MUX_DETACHED
;
921 switch (port
->sm_mux_state
) {
922 case AD_MUX_DETACHED
:
923 if ((port
->sm_vars
& AD_PORT_SELECTED
)
924 || (port
->sm_vars
& AD_PORT_STANDBY
))
925 /* if SELECTED or STANDBY */
926 port
->sm_mux_state
= AD_MUX_WAITING
;
929 /* if SELECTED == FALSE return to DETACH state */
930 if (!(port
->sm_vars
& AD_PORT_SELECTED
)) {
931 port
->sm_vars
&= ~AD_PORT_READY_N
;
932 /* in order to withhold the Selection Logic to
933 * check all ports READY_N value every callback
934 * cycle to update ready variable, we check
935 * READY_N and update READY here
937 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
938 port
->sm_mux_state
= AD_MUX_DETACHED
;
942 /* check if the wait_while_timer expired */
943 if (port
->sm_mux_timer_counter
944 && !(--port
->sm_mux_timer_counter
))
945 port
->sm_vars
|= AD_PORT_READY_N
;
947 /* in order to withhold the selection logic to check
948 * all ports READY_N value every callback cycle to
949 * update ready variable, we check READY_N and update
952 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
954 /* if the wait_while_timer expired, and the port is
955 * in READY state, move to ATTACHED state
957 if ((port
->sm_vars
& AD_PORT_READY
)
958 && !port
->sm_mux_timer_counter
)
959 port
->sm_mux_state
= AD_MUX_ATTACHED
;
961 case AD_MUX_ATTACHED
:
962 /* check also if agg_select_timer expired (so the
963 * edable port will take place only after this timer)
965 if ((port
->sm_vars
& AD_PORT_SELECTED
) &&
966 (port
->partner_oper
.port_state
& AD_STATE_SYNCHRONIZATION
) &&
967 !__check_agg_selection_timer(port
)) {
968 if (port
->aggregator
->is_active
)
970 AD_MUX_COLLECTING_DISTRIBUTING
;
971 } else if (!(port
->sm_vars
& AD_PORT_SELECTED
) ||
972 (port
->sm_vars
& AD_PORT_STANDBY
)) {
973 /* if UNSELECTED or STANDBY */
974 port
->sm_vars
&= ~AD_PORT_READY_N
;
975 /* in order to withhold the selection logic to
976 * check all ports READY_N value every callback
977 * cycle to update ready variable, we check
978 * READY_N and update READY here
980 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
981 port
->sm_mux_state
= AD_MUX_DETACHED
;
982 } else if (port
->aggregator
->is_active
) {
983 port
->actor_oper_port_state
|=
984 AD_STATE_SYNCHRONIZATION
;
987 case AD_MUX_COLLECTING_DISTRIBUTING
:
988 if (!(port
->sm_vars
& AD_PORT_SELECTED
) ||
989 (port
->sm_vars
& AD_PORT_STANDBY
) ||
990 !(port
->partner_oper
.port_state
& AD_STATE_SYNCHRONIZATION
) ||
991 !(port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
)) {
992 port
->sm_mux_state
= AD_MUX_ATTACHED
;
994 /* if port state hasn't changed make
995 * sure that a collecting distributing
996 * port in an active aggregator is enabled
998 if (port
->aggregator
&&
999 port
->aggregator
->is_active
&&
1000 !__port_is_enabled(port
)) {
1002 __enable_port(port
);
1011 /* check if the state machine was changed */
1012 if (port
->sm_mux_state
!= last_state
) {
1013 pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1014 port
->actor_port_number
,
1015 port
->slave
->dev
->name
,
1017 port
->sm_mux_state
);
1018 switch (port
->sm_mux_state
) {
1019 case AD_MUX_DETACHED
:
1020 port
->actor_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1021 ad_disable_collecting_distributing(port
,
1023 port
->actor_oper_port_state
&= ~AD_STATE_COLLECTING
;
1024 port
->actor_oper_port_state
&= ~AD_STATE_DISTRIBUTING
;
1027 case AD_MUX_WAITING
:
1028 port
->sm_mux_timer_counter
= __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER
, 0);
1030 case AD_MUX_ATTACHED
:
1031 if (port
->aggregator
->is_active
)
1032 port
->actor_oper_port_state
|=
1033 AD_STATE_SYNCHRONIZATION
;
1035 port
->actor_oper_port_state
&=
1036 ~AD_STATE_SYNCHRONIZATION
;
1037 port
->actor_oper_port_state
&= ~AD_STATE_COLLECTING
;
1038 port
->actor_oper_port_state
&= ~AD_STATE_DISTRIBUTING
;
1039 ad_disable_collecting_distributing(port
,
1043 case AD_MUX_COLLECTING_DISTRIBUTING
:
1044 port
->actor_oper_port_state
|= AD_STATE_COLLECTING
;
1045 port
->actor_oper_port_state
|= AD_STATE_DISTRIBUTING
;
1046 port
->actor_oper_port_state
|= AD_STATE_SYNCHRONIZATION
;
1047 ad_enable_collecting_distributing(port
,
1058 * ad_rx_machine - handle a port's rx State Machine
1059 * @lacpdu: the lacpdu we've received
1060 * @port: the port we're looking at
1062 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1063 * CURRENT. If timer expired set the state machine in the proper state.
1064 * In other cases, this function checks if we need to switch to other state.
1066 static void ad_rx_machine(struct lacpdu
*lacpdu
, struct port
*port
)
1068 rx_states_t last_state
;
1070 /* keep current State Machine state to compare later if it was
1073 last_state
= port
->sm_rx_state
;
1075 /* check if state machine should change state */
1077 /* first, check if port was reinitialized */
1078 if (port
->sm_vars
& AD_PORT_BEGIN
) {
1079 port
->sm_rx_state
= AD_RX_INITIALIZE
;
1080 port
->sm_vars
|= AD_PORT_CHURNED
;
1081 /* check if port is not enabled */
1082 } else if (!(port
->sm_vars
& AD_PORT_BEGIN
)
1083 && !port
->is_enabled
&& !(port
->sm_vars
& AD_PORT_MOVED
))
1084 port
->sm_rx_state
= AD_RX_PORT_DISABLED
;
1085 /* check if new lacpdu arrived */
1086 else if (lacpdu
&& ((port
->sm_rx_state
== AD_RX_EXPIRED
) ||
1087 (port
->sm_rx_state
== AD_RX_DEFAULTED
) ||
1088 (port
->sm_rx_state
== AD_RX_CURRENT
))) {
1089 if (port
->sm_rx_state
!= AD_RX_CURRENT
)
1090 port
->sm_vars
|= AD_PORT_CHURNED
;
1091 port
->sm_rx_timer_counter
= 0;
1092 port
->sm_rx_state
= AD_RX_CURRENT
;
1094 /* if timer is on, and if it is expired */
1095 if (port
->sm_rx_timer_counter
&&
1096 !(--port
->sm_rx_timer_counter
)) {
1097 switch (port
->sm_rx_state
) {
1099 port
->sm_rx_state
= AD_RX_DEFAULTED
;
1102 port
->sm_rx_state
= AD_RX_EXPIRED
;
1108 /* if no lacpdu arrived and no timer is on */
1109 switch (port
->sm_rx_state
) {
1110 case AD_RX_PORT_DISABLED
:
1111 if (port
->sm_vars
& AD_PORT_MOVED
)
1112 port
->sm_rx_state
= AD_RX_INITIALIZE
;
1113 else if (port
->is_enabled
1115 & AD_PORT_LACP_ENABLED
))
1116 port
->sm_rx_state
= AD_RX_EXPIRED
;
1117 else if (port
->is_enabled
1119 & AD_PORT_LACP_ENABLED
) == 0))
1120 port
->sm_rx_state
= AD_RX_LACP_DISABLED
;
1129 /* check if the State machine was changed or new lacpdu arrived */
1130 if ((port
->sm_rx_state
!= last_state
) || (lacpdu
)) {
1131 pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1132 port
->actor_port_number
,
1133 port
->slave
->dev
->name
,
1136 switch (port
->sm_rx_state
) {
1137 case AD_RX_INITIALIZE
:
1138 if (!(port
->actor_oper_port_key
& AD_DUPLEX_KEY_MASKS
))
1139 port
->sm_vars
&= ~AD_PORT_LACP_ENABLED
;
1141 port
->sm_vars
|= AD_PORT_LACP_ENABLED
;
1142 port
->sm_vars
&= ~AD_PORT_SELECTED
;
1143 __record_default(port
);
1144 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1145 port
->sm_vars
&= ~AD_PORT_MOVED
;
1146 port
->sm_rx_state
= AD_RX_PORT_DISABLED
;
1149 case AD_RX_PORT_DISABLED
:
1150 port
->sm_vars
&= ~AD_PORT_MATCHED
;
1152 case AD_RX_LACP_DISABLED
:
1153 port
->sm_vars
&= ~AD_PORT_SELECTED
;
1154 __record_default(port
);
1155 port
->partner_oper
.port_state
&= ~AD_STATE_AGGREGATION
;
1156 port
->sm_vars
|= AD_PORT_MATCHED
;
1157 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1160 /* Reset of the Synchronization flag (Standard 43.4.12)
1161 * This reset cause to disable this port in the
1162 * COLLECTING_DISTRIBUTING state of the mux machine in
1163 * case of EXPIRED even if LINK_DOWN didn't arrive for
1166 port
->partner_oper
.port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1167 port
->sm_vars
&= ~AD_PORT_MATCHED
;
1168 port
->partner_oper
.port_state
|= AD_STATE_LACP_TIMEOUT
;
1169 port
->partner_oper
.port_state
|= AD_STATE_LACP_ACTIVITY
;
1170 port
->sm_rx_timer_counter
= __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER
, (u16
)(AD_SHORT_TIMEOUT
));
1171 port
->actor_oper_port_state
|= AD_STATE_EXPIRED
;
1172 port
->sm_vars
|= AD_PORT_CHURNED
;
1174 case AD_RX_DEFAULTED
:
1175 __update_default_selected(port
);
1176 __record_default(port
);
1177 port
->sm_vars
|= AD_PORT_MATCHED
;
1178 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1181 /* detect loopback situation */
1182 if (MAC_ADDRESS_EQUAL(&(lacpdu
->actor_system
),
1183 &(port
->actor_system
))) {
1184 netdev_err(port
->slave
->bond
->dev
, "An illegal loopback occurred on adapter (%s)\n"
1185 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1186 port
->slave
->dev
->name
);
1189 __update_selected(lacpdu
, port
);
1190 __update_ntt(lacpdu
, port
);
1191 __record_pdu(lacpdu
, port
);
1192 port
->sm_rx_timer_counter
= __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER
, (u16
)(port
->actor_oper_port_state
& AD_STATE_LACP_TIMEOUT
));
1193 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1202 * ad_churn_machine - handle port churn's state machine
1203 * @port: the port we're looking at
1206 static void ad_churn_machine(struct port
*port
)
1208 if (port
->sm_vars
& AD_PORT_CHURNED
) {
1209 port
->sm_vars
&= ~AD_PORT_CHURNED
;
1210 port
->sm_churn_actor_state
= AD_CHURN_MONITOR
;
1211 port
->sm_churn_partner_state
= AD_CHURN_MONITOR
;
1212 port
->sm_churn_actor_timer_counter
=
1213 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER
, 0);
1214 port
->sm_churn_partner_timer_counter
=
1215 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER
, 0);
1218 if (port
->sm_churn_actor_timer_counter
&&
1219 !(--port
->sm_churn_actor_timer_counter
) &&
1220 port
->sm_churn_actor_state
== AD_CHURN_MONITOR
) {
1221 if (port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
) {
1222 port
->sm_churn_actor_state
= AD_NO_CHURN
;
1224 port
->churn_actor_count
++;
1225 port
->sm_churn_actor_state
= AD_CHURN
;
1228 if (port
->sm_churn_partner_timer_counter
&&
1229 !(--port
->sm_churn_partner_timer_counter
) &&
1230 port
->sm_churn_partner_state
== AD_CHURN_MONITOR
) {
1231 if (port
->partner_oper
.port_state
& AD_STATE_SYNCHRONIZATION
) {
1232 port
->sm_churn_partner_state
= AD_NO_CHURN
;
1234 port
->churn_partner_count
++;
1235 port
->sm_churn_partner_state
= AD_CHURN
;
1241 * ad_tx_machine - handle a port's tx state machine
1242 * @port: the port we're looking at
1244 static void ad_tx_machine(struct port
*port
)
1246 /* check if tx timer expired, to verify that we do not send more than
1247 * 3 packets per second
1249 if (port
->sm_tx_timer_counter
&& !(--port
->sm_tx_timer_counter
)) {
1250 /* check if there is something to send */
1251 if (port
->ntt
&& (port
->sm_vars
& AD_PORT_LACP_ENABLED
)) {
1252 __update_lacpdu_from_port(port
);
1254 if (ad_lacpdu_send(port
) >= 0) {
1255 pr_debug("Sent LACPDU on port %d\n",
1256 port
->actor_port_number
);
1258 /* mark ntt as false, so it will not be sent
1259 * again until demanded
1264 /* restart tx timer(to verify that we will not exceed
1265 * AD_MAX_TX_IN_SECOND
1267 port
->sm_tx_timer_counter
= ad_ticks_per_sec
/AD_MAX_TX_IN_SECOND
;
1272 * ad_periodic_machine - handle a port's periodic state machine
1273 * @port: the port we're looking at
1275 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1277 static void ad_periodic_machine(struct port
*port
)
1279 periodic_states_t last_state
;
1281 /* keep current state machine state to compare later if it was changed */
1282 last_state
= port
->sm_periodic_state
;
1284 /* check if port was reinitialized */
1285 if (((port
->sm_vars
& AD_PORT_BEGIN
) || !(port
->sm_vars
& AD_PORT_LACP_ENABLED
) || !port
->is_enabled
) ||
1286 (!(port
->actor_oper_port_state
& AD_STATE_LACP_ACTIVITY
) && !(port
->partner_oper
.port_state
& AD_STATE_LACP_ACTIVITY
))
1288 port
->sm_periodic_state
= AD_NO_PERIODIC
;
1290 /* check if state machine should change state */
1291 else if (port
->sm_periodic_timer_counter
) {
1292 /* check if periodic state machine expired */
1293 if (!(--port
->sm_periodic_timer_counter
)) {
1294 /* if expired then do tx */
1295 port
->sm_periodic_state
= AD_PERIODIC_TX
;
1297 /* If not expired, check if there is some new timeout
1298 * parameter from the partner state
1300 switch (port
->sm_periodic_state
) {
1301 case AD_FAST_PERIODIC
:
1302 if (!(port
->partner_oper
.port_state
1303 & AD_STATE_LACP_TIMEOUT
))
1304 port
->sm_periodic_state
= AD_SLOW_PERIODIC
;
1306 case AD_SLOW_PERIODIC
:
1307 if ((port
->partner_oper
.port_state
& AD_STATE_LACP_TIMEOUT
)) {
1308 port
->sm_periodic_timer_counter
= 0;
1309 port
->sm_periodic_state
= AD_PERIODIC_TX
;
1317 switch (port
->sm_periodic_state
) {
1318 case AD_NO_PERIODIC
:
1319 port
->sm_periodic_state
= AD_FAST_PERIODIC
;
1321 case AD_PERIODIC_TX
:
1322 if (!(port
->partner_oper
.port_state
&
1323 AD_STATE_LACP_TIMEOUT
))
1324 port
->sm_periodic_state
= AD_SLOW_PERIODIC
;
1326 port
->sm_periodic_state
= AD_FAST_PERIODIC
;
1333 /* check if the state machine was changed */
1334 if (port
->sm_periodic_state
!= last_state
) {
1335 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1336 port
->actor_port_number
, last_state
,
1337 port
->sm_periodic_state
);
1338 switch (port
->sm_periodic_state
) {
1339 case AD_NO_PERIODIC
:
1340 port
->sm_periodic_timer_counter
= 0;
1342 case AD_FAST_PERIODIC
:
1343 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1344 port
->sm_periodic_timer_counter
= __ad_timer_to_ticks(AD_PERIODIC_TIMER
, (u16
)(AD_FAST_PERIODIC_TIME
))-1;
1346 case AD_SLOW_PERIODIC
:
1347 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1348 port
->sm_periodic_timer_counter
= __ad_timer_to_ticks(AD_PERIODIC_TIMER
, (u16
)(AD_SLOW_PERIODIC_TIME
))-1;
1350 case AD_PERIODIC_TX
:
1360 * ad_port_selection_logic - select aggregation groups
1361 * @port: the port we're looking at
1362 * @update_slave_arr: Does slave array need update?
1364 * Select aggregation groups, and assign each port for it's aggregetor. The
1365 * selection logic is called in the inititalization (after all the handshkes),
1366 * and after every lacpdu receive (if selected is off).
1368 static void ad_port_selection_logic(struct port
*port
, bool *update_slave_arr
)
1370 struct aggregator
*aggregator
, *free_aggregator
= NULL
, *temp_aggregator
;
1371 struct port
*last_port
= NULL
, *curr_port
;
1372 struct list_head
*iter
;
1373 struct bonding
*bond
;
1374 struct slave
*slave
;
1377 /* if the port is already Selected, do nothing */
1378 if (port
->sm_vars
& AD_PORT_SELECTED
)
1381 bond
= __get_bond_by_port(port
);
1383 /* if the port is connected to other aggregator, detach it */
1384 if (port
->aggregator
) {
1385 /* detach the port from its former aggregator */
1386 temp_aggregator
= port
->aggregator
;
1387 for (curr_port
= temp_aggregator
->lag_ports
; curr_port
;
1388 last_port
= curr_port
,
1389 curr_port
= curr_port
->next_port_in_aggregator
) {
1390 if (curr_port
== port
) {
1391 temp_aggregator
->num_of_ports
--;
1392 /* if it is the first port attached to the
1396 temp_aggregator
->lag_ports
=
1397 port
->next_port_in_aggregator
;
1399 /* not the first port attached to the
1402 last_port
->next_port_in_aggregator
=
1403 port
->next_port_in_aggregator
;
1406 /* clear the port's relations to this
1409 port
->aggregator
= NULL
;
1410 port
->next_port_in_aggregator
= NULL
;
1411 port
->actor_port_aggregator_identifier
= 0;
1413 netdev_dbg(bond
->dev
, "Port %d left LAG %d\n",
1414 port
->actor_port_number
,
1415 temp_aggregator
->aggregator_identifier
);
1416 /* if the aggregator is empty, clear its
1417 * parameters, and set it ready to be attached
1419 if (!temp_aggregator
->lag_ports
)
1420 ad_clear_agg(temp_aggregator
);
1425 /* meaning: the port was related to an aggregator
1426 * but was not on the aggregator port list
1428 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1429 port
->slave
->bond
->dev
->name
,
1430 port
->actor_port_number
,
1431 port
->slave
->dev
->name
,
1432 port
->aggregator
->aggregator_identifier
);
1435 /* search on all aggregators for a suitable aggregator for this port */
1436 bond_for_each_slave(bond
, slave
, iter
) {
1437 aggregator
= &(SLAVE_AD_INFO(slave
)->aggregator
);
1439 /* keep a free aggregator for later use(if needed) */
1440 if (!aggregator
->lag_ports
) {
1441 if (!free_aggregator
)
1442 free_aggregator
= aggregator
;
1445 /* check if current aggregator suits us */
1446 if (((aggregator
->actor_oper_aggregator_key
== port
->actor_oper_port_key
) && /* if all parameters match AND */
1447 MAC_ADDRESS_EQUAL(&(aggregator
->partner_system
), &(port
->partner_oper
.system
)) &&
1448 (aggregator
->partner_system_priority
== port
->partner_oper
.system_priority
) &&
1449 (aggregator
->partner_oper_aggregator_key
== port
->partner_oper
.key
)
1451 ((!MAC_ADDRESS_EQUAL(&(port
->partner_oper
.system
), &(null_mac_addr
)) && /* partner answers */
1452 !aggregator
->is_individual
) /* but is not individual OR */
1455 /* attach to the founded aggregator */
1456 port
->aggregator
= aggregator
;
1457 port
->actor_port_aggregator_identifier
=
1458 port
->aggregator
->aggregator_identifier
;
1459 port
->next_port_in_aggregator
= aggregator
->lag_ports
;
1460 port
->aggregator
->num_of_ports
++;
1461 aggregator
->lag_ports
= port
;
1462 netdev_dbg(bond
->dev
, "Port %d joined LAG %d(existing LAG)\n",
1463 port
->actor_port_number
,
1464 port
->aggregator
->aggregator_identifier
);
1466 /* mark this port as selected */
1467 port
->sm_vars
|= AD_PORT_SELECTED
;
1473 /* the port couldn't find an aggregator - attach it to a new
1477 if (free_aggregator
) {
1478 /* assign port a new aggregator */
1479 port
->aggregator
= free_aggregator
;
1480 port
->actor_port_aggregator_identifier
=
1481 port
->aggregator
->aggregator_identifier
;
1483 /* update the new aggregator's parameters
1484 * if port was responsed from the end-user
1486 if (port
->actor_oper_port_key
& AD_DUPLEX_KEY_MASKS
)
1487 /* if port is full duplex */
1488 port
->aggregator
->is_individual
= false;
1490 port
->aggregator
->is_individual
= true;
1492 port
->aggregator
->actor_admin_aggregator_key
=
1493 port
->actor_admin_port_key
;
1494 port
->aggregator
->actor_oper_aggregator_key
=
1495 port
->actor_oper_port_key
;
1496 port
->aggregator
->partner_system
=
1497 port
->partner_oper
.system
;
1498 port
->aggregator
->partner_system_priority
=
1499 port
->partner_oper
.system_priority
;
1500 port
->aggregator
->partner_oper_aggregator_key
= port
->partner_oper
.key
;
1501 port
->aggregator
->receive_state
= 1;
1502 port
->aggregator
->transmit_state
= 1;
1503 port
->aggregator
->lag_ports
= port
;
1504 port
->aggregator
->num_of_ports
++;
1506 /* mark this port as selected */
1507 port
->sm_vars
|= AD_PORT_SELECTED
;
1509 netdev_dbg(bond
->dev
, "Port %d joined LAG %d(new LAG)\n",
1510 port
->actor_port_number
,
1511 port
->aggregator
->aggregator_identifier
);
1513 netdev_err(bond
->dev
, "Port %d (on %s) did not find a suitable aggregator\n",
1514 port
->actor_port_number
, port
->slave
->dev
->name
);
1517 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1518 * in all aggregator's ports, else set ready=FALSE in all
1519 * aggregator's ports
1521 __set_agg_ports_ready(port
->aggregator
,
1522 __agg_ports_are_ready(port
->aggregator
));
1524 aggregator
= __get_first_agg(port
);
1525 ad_agg_selection_logic(aggregator
, update_slave_arr
);
1527 if (!port
->aggregator
->is_active
)
1528 port
->actor_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1531 /* Decide if "agg" is a better choice for the new active aggregator that
1532 * the current best, according to the ad_select policy.
1534 static struct aggregator
*ad_agg_selection_test(struct aggregator
*best
,
1535 struct aggregator
*curr
)
1537 /* 0. If no best, select current.
1539 * 1. If the current agg is not individual, and the best is
1540 * individual, select current.
1542 * 2. If current agg is individual and the best is not, keep best.
1544 * 3. Therefore, current and best are both individual or both not
1547 * 3a. If current agg partner replied, and best agg partner did not,
1550 * 3b. If current agg partner did not reply and best agg partner
1551 * did reply, keep best.
1553 * 4. Therefore, current and best both have partner replies or
1554 * both do not, so perform selection policy:
1556 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1557 * select by bandwidth.
1559 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1564 if (!curr
->is_individual
&& best
->is_individual
)
1567 if (curr
->is_individual
&& !best
->is_individual
)
1570 if (__agg_has_partner(curr
) && !__agg_has_partner(best
))
1573 if (!__agg_has_partner(curr
) && __agg_has_partner(best
))
1576 switch (__get_agg_selection_mode(curr
->lag_ports
)) {
1578 if (__agg_active_ports(curr
) > __agg_active_ports(best
))
1581 if (__agg_active_ports(curr
) < __agg_active_ports(best
))
1585 case BOND_AD_STABLE
:
1586 case BOND_AD_BANDWIDTH
:
1587 if (__get_agg_bandwidth(curr
) > __get_agg_bandwidth(best
))
1593 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1594 curr
->slave
->bond
->dev
->name
,
1595 __get_agg_selection_mode(curr
->lag_ports
));
1602 static int agg_device_up(const struct aggregator
*agg
)
1604 struct port
*port
= agg
->lag_ports
;
1609 for (port
= agg
->lag_ports
; port
;
1610 port
= port
->next_port_in_aggregator
) {
1611 if (netif_running(port
->slave
->dev
) &&
1612 netif_carrier_ok(port
->slave
->dev
))
1620 * ad_agg_selection_logic - select an aggregation group for a team
1621 * @aggregator: the aggregator we're looking at
1622 * @update_slave_arr: Does slave array need update?
1624 * It is assumed that only one aggregator may be selected for a team.
1626 * The logic of this function is to select the aggregator according to
1627 * the ad_select policy:
1629 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1630 * it, and to reselect the active aggregator only if the previous
1631 * aggregator has no more ports related to it.
1633 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1634 * bandwidth, and reselect whenever a link state change takes place or the
1635 * set of slaves in the bond changes.
1637 * BOND_AD_COUNT: select the aggregator with largest number of ports
1638 * (slaves), and reselect whenever a link state change takes place or the
1639 * set of slaves in the bond changes.
1641 * FIXME: this function MUST be called with the first agg in the bond, or
1642 * __get_active_agg() won't work correctly. This function should be better
1643 * called with the bond itself, and retrieve the first agg from it.
1645 static void ad_agg_selection_logic(struct aggregator
*agg
,
1646 bool *update_slave_arr
)
1648 struct aggregator
*best
, *active
, *origin
;
1649 struct bonding
*bond
= agg
->slave
->bond
;
1650 struct list_head
*iter
;
1651 struct slave
*slave
;
1656 active
= __get_active_agg(agg
);
1657 best
= (active
&& agg_device_up(active
)) ? active
: NULL
;
1659 bond_for_each_slave_rcu(bond
, slave
, iter
) {
1660 agg
= &(SLAVE_AD_INFO(slave
)->aggregator
);
1664 if (__agg_active_ports(agg
) && agg_device_up(agg
))
1665 best
= ad_agg_selection_test(best
, agg
);
1669 __get_agg_selection_mode(best
->lag_ports
) == BOND_AD_STABLE
) {
1670 /* For the STABLE policy, don't replace the old active
1671 * aggregator if it's still active (it has an answering
1672 * partner) or if both the best and active don't have an
1673 * answering partner.
1675 if (active
&& active
->lag_ports
&&
1676 __agg_active_ports(active
) &&
1677 (__agg_has_partner(active
) ||
1678 (!__agg_has_partner(active
) &&
1679 !__agg_has_partner(best
)))) {
1680 if (!(!active
->actor_oper_aggregator_key
&&
1681 best
->actor_oper_aggregator_key
)) {
1683 active
->is_active
= 1;
1688 if (best
&& (best
== active
)) {
1690 active
->is_active
= 1;
1693 /* if there is new best aggregator, activate it */
1695 netdev_dbg(bond
->dev
, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1696 best
->aggregator_identifier
, best
->num_of_ports
,
1697 best
->actor_oper_aggregator_key
,
1698 best
->partner_oper_aggregator_key
,
1699 best
->is_individual
, best
->is_active
);
1700 netdev_dbg(bond
->dev
, "best ports %p slave %p %s\n",
1701 best
->lag_ports
, best
->slave
,
1702 best
->slave
? best
->slave
->dev
->name
: "NULL");
1704 bond_for_each_slave_rcu(bond
, slave
, iter
) {
1705 agg
= &(SLAVE_AD_INFO(slave
)->aggregator
);
1707 netdev_dbg(bond
->dev
, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1708 agg
->aggregator_identifier
, agg
->num_of_ports
,
1709 agg
->actor_oper_aggregator_key
,
1710 agg
->partner_oper_aggregator_key
,
1711 agg
->is_individual
, agg
->is_active
);
1714 /* check if any partner replys */
1715 if (best
->is_individual
) {
1716 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1718 best
->slave
->bond
->dev
->name
: "NULL");
1721 best
->is_active
= 1;
1722 netdev_dbg(bond
->dev
, "LAG %d chosen as the active LAG\n",
1723 best
->aggregator_identifier
);
1724 netdev_dbg(bond
->dev
, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1725 best
->aggregator_identifier
, best
->num_of_ports
,
1726 best
->actor_oper_aggregator_key
,
1727 best
->partner_oper_aggregator_key
,
1728 best
->is_individual
, best
->is_active
);
1730 /* disable the ports that were related to the former
1734 for (port
= active
->lag_ports
; port
;
1735 port
= port
->next_port_in_aggregator
) {
1736 __disable_port(port
);
1739 /* Slave array needs update. */
1740 *update_slave_arr
= true;
1743 /* if the selected aggregator is of join individuals
1744 * (partner_system is NULL), enable their ports
1746 active
= __get_active_agg(origin
);
1749 if (!__agg_has_partner(active
)) {
1750 for (port
= active
->lag_ports
; port
;
1751 port
= port
->next_port_in_aggregator
) {
1752 __enable_port(port
);
1759 bond_3ad_set_carrier(bond
);
1763 * ad_clear_agg - clear a given aggregator's parameters
1764 * @aggregator: the aggregator we're looking at
1766 static void ad_clear_agg(struct aggregator
*aggregator
)
1769 aggregator
->is_individual
= false;
1770 aggregator
->actor_admin_aggregator_key
= 0;
1771 aggregator
->actor_oper_aggregator_key
= 0;
1772 eth_zero_addr(aggregator
->partner_system
.mac_addr_value
);
1773 aggregator
->partner_system_priority
= 0;
1774 aggregator
->partner_oper_aggregator_key
= 0;
1775 aggregator
->receive_state
= 0;
1776 aggregator
->transmit_state
= 0;
1777 aggregator
->lag_ports
= NULL
;
1778 aggregator
->is_active
= 0;
1779 aggregator
->num_of_ports
= 0;
1780 pr_debug("LAG %d was cleared\n",
1781 aggregator
->aggregator_identifier
);
1786 * ad_initialize_agg - initialize a given aggregator's parameters
1787 * @aggregator: the aggregator we're looking at
1789 static void ad_initialize_agg(struct aggregator
*aggregator
)
1792 ad_clear_agg(aggregator
);
1794 eth_zero_addr(aggregator
->aggregator_mac_address
.mac_addr_value
);
1795 aggregator
->aggregator_identifier
= 0;
1796 aggregator
->slave
= NULL
;
1801 * ad_initialize_port - initialize a given port's parameters
1802 * @aggregator: the aggregator we're looking at
1803 * @lacp_fast: boolean. whether fast periodic should be used
1805 static void ad_initialize_port(struct port
*port
, int lacp_fast
)
1807 static const struct port_params tmpl
= {
1808 .system_priority
= 0xffff,
1811 .port_priority
= 0xff,
1814 static const struct lacpdu lacpdu
= {
1816 .version_number
= 0x01,
1817 .tlv_type_actor_info
= 0x01,
1818 .actor_information_length
= 0x14,
1819 .tlv_type_partner_info
= 0x02,
1820 .partner_information_length
= 0x14,
1821 .tlv_type_collector_info
= 0x03,
1822 .collector_information_length
= 0x10,
1823 .collector_max_delay
= htons(AD_COLLECTOR_MAX_DELAY
),
1827 port
->actor_port_priority
= 0xff;
1828 port
->actor_port_aggregator_identifier
= 0;
1830 port
->actor_admin_port_state
= AD_STATE_AGGREGATION
|
1831 AD_STATE_LACP_ACTIVITY
;
1832 port
->actor_oper_port_state
= AD_STATE_AGGREGATION
|
1833 AD_STATE_LACP_ACTIVITY
;
1836 port
->actor_oper_port_state
|= AD_STATE_LACP_TIMEOUT
;
1838 memcpy(&port
->partner_admin
, &tmpl
, sizeof(tmpl
));
1839 memcpy(&port
->partner_oper
, &tmpl
, sizeof(tmpl
));
1841 port
->is_enabled
= true;
1842 /* private parameters */
1843 port
->sm_vars
= AD_PORT_BEGIN
| AD_PORT_LACP_ENABLED
;
1844 port
->sm_rx_state
= 0;
1845 port
->sm_rx_timer_counter
= 0;
1846 port
->sm_periodic_state
= 0;
1847 port
->sm_periodic_timer_counter
= 0;
1848 port
->sm_mux_state
= 0;
1849 port
->sm_mux_timer_counter
= 0;
1850 port
->sm_tx_state
= 0;
1851 port
->aggregator
= NULL
;
1852 port
->next_port_in_aggregator
= NULL
;
1853 port
->transaction_id
= 0;
1855 port
->sm_churn_actor_timer_counter
= 0;
1856 port
->sm_churn_actor_state
= 0;
1857 port
->churn_actor_count
= 0;
1858 port
->sm_churn_partner_timer_counter
= 0;
1859 port
->sm_churn_partner_state
= 0;
1860 port
->churn_partner_count
= 0;
1862 memcpy(&port
->lacpdu
, &lacpdu
, sizeof(lacpdu
));
1867 * ad_enable_collecting_distributing - enable a port's transmit/receive
1868 * @port: the port we're looking at
1869 * @update_slave_arr: Does slave array need update?
1871 * Enable @port if it's in an active aggregator
1873 static void ad_enable_collecting_distributing(struct port
*port
,
1874 bool *update_slave_arr
)
1876 if (port
->aggregator
->is_active
) {
1877 pr_debug("Enabling port %d(LAG %d)\n",
1878 port
->actor_port_number
,
1879 port
->aggregator
->aggregator_identifier
);
1880 __enable_port(port
);
1881 /* Slave array needs update */
1882 *update_slave_arr
= true;
1887 * ad_disable_collecting_distributing - disable a port's transmit/receive
1888 * @port: the port we're looking at
1889 * @update_slave_arr: Does slave array need update?
1891 static void ad_disable_collecting_distributing(struct port
*port
,
1892 bool *update_slave_arr
)
1894 if (port
->aggregator
&&
1895 !MAC_ADDRESS_EQUAL(&(port
->aggregator
->partner_system
),
1896 &(null_mac_addr
))) {
1897 pr_debug("Disabling port %d(LAG %d)\n",
1898 port
->actor_port_number
,
1899 port
->aggregator
->aggregator_identifier
);
1900 __disable_port(port
);
1901 /* Slave array needs an update */
1902 *update_slave_arr
= true;
1907 * ad_marker_info_received - handle receive of a Marker information frame
1908 * @marker_info: Marker info received
1909 * @port: the port we're looking at
1911 static void ad_marker_info_received(struct bond_marker
*marker_info
,
1914 struct bond_marker marker
;
1916 /* copy the received marker data to the response marker */
1917 memcpy(&marker
, marker_info
, sizeof(struct bond_marker
));
1918 /* change the marker subtype to marker response */
1919 marker
.tlv_type
= AD_MARKER_RESPONSE_SUBTYPE
;
1921 /* send the marker response */
1922 if (ad_marker_send(port
, &marker
) >= 0) {
1923 pr_debug("Sent Marker Response on port %d\n",
1924 port
->actor_port_number
);
1929 * ad_marker_response_received - handle receive of a marker response frame
1930 * @marker: marker PDU received
1931 * @port: the port we're looking at
1933 * This function does nothing since we decided not to implement send and handle
1934 * response for marker PDU's, in this stage, but only to respond to marker
1937 static void ad_marker_response_received(struct bond_marker
*marker
,
1940 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1943 /* ========= AD exported functions to the main bonding code ========= */
1945 /* Check aggregators status in team every T seconds */
1946 #define AD_AGGREGATOR_SELECTION_TIMER 8
1949 * bond_3ad_initiate_agg_selection - initate aggregator selection
1950 * @bond: bonding struct
1952 * Set the aggregation selection timer, to initiate an agg selection in
1953 * the very near future. Called during first initialization, and during
1954 * any down to up transitions of the bond.
1956 void bond_3ad_initiate_agg_selection(struct bonding
*bond
, int timeout
)
1958 BOND_AD_INFO(bond
).agg_select_timer
= timeout
;
1962 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1963 * @bond: bonding struct to work on
1964 * @tick_resolution: tick duration (millisecond resolution)
1966 * Can be called only after the mac address of the bond is set.
1968 void bond_3ad_initialize(struct bonding
*bond
, u16 tick_resolution
)
1970 /* check that the bond is not initialized yet */
1971 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond
).system
.sys_mac_addr
),
1972 bond
->dev
->dev_addr
)) {
1974 BOND_AD_INFO(bond
).aggregator_identifier
= 0;
1976 BOND_AD_INFO(bond
).system
.sys_priority
=
1977 bond
->params
.ad_actor_sys_prio
;
1978 if (is_zero_ether_addr(bond
->params
.ad_actor_system
))
1979 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
1980 *((struct mac_addr
*)bond
->dev
->dev_addr
);
1982 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
1983 *((struct mac_addr
*)bond
->params
.ad_actor_system
);
1985 /* initialize how many times this module is called in one
1986 * second (should be about every 100ms)
1988 ad_ticks_per_sec
= tick_resolution
;
1990 bond_3ad_initiate_agg_selection(bond
,
1991 AD_AGGREGATOR_SELECTION_TIMER
*
1997 * bond_3ad_bind_slave - initialize a slave's port
1998 * @slave: slave struct to work on
2000 * Returns: 0 on success
2003 void bond_3ad_bind_slave(struct slave
*slave
)
2005 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
2007 struct aggregator
*aggregator
;
2009 /* check that the slave has not been initialized yet. */
2010 if (SLAVE_AD_INFO(slave
)->port
.slave
!= slave
) {
2012 /* port initialization */
2013 port
= &(SLAVE_AD_INFO(slave
)->port
);
2015 ad_initialize_port(port
, bond
->params
.lacp_fast
);
2017 port
->slave
= slave
;
2018 port
->actor_port_number
= SLAVE_AD_INFO(slave
)->id
;
2019 /* key is determined according to the link speed, duplex and
2022 port
->actor_admin_port_key
= bond
->params
.ad_user_port_key
<< 6;
2023 ad_update_actor_keys(port
, false);
2024 /* actor system is the bond's system */
2025 __ad_actor_update_port(port
);
2026 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2027 * lacpdu's are sent in one second)
2029 port
->sm_tx_timer_counter
= ad_ticks_per_sec
/AD_MAX_TX_IN_SECOND
;
2031 __disable_port(port
);
2033 /* aggregator initialization */
2034 aggregator
= &(SLAVE_AD_INFO(slave
)->aggregator
);
2036 ad_initialize_agg(aggregator
);
2038 aggregator
->aggregator_mac_address
= *((struct mac_addr
*)bond
->dev
->dev_addr
);
2039 aggregator
->aggregator_identifier
= ++BOND_AD_INFO(bond
).aggregator_identifier
;
2040 aggregator
->slave
= slave
;
2041 aggregator
->is_active
= 0;
2042 aggregator
->num_of_ports
= 0;
2047 * bond_3ad_unbind_slave - deinitialize a slave's port
2048 * @slave: slave struct to work on
2050 * Search for the aggregator that is related to this port, remove the
2051 * aggregator and assign another aggregator for other port related to it
2052 * (if any), and remove the port.
2054 void bond_3ad_unbind_slave(struct slave
*slave
)
2056 struct port
*port
, *prev_port
, *temp_port
;
2057 struct aggregator
*aggregator
, *new_aggregator
, *temp_aggregator
;
2058 int select_new_active_agg
= 0;
2059 struct bonding
*bond
= slave
->bond
;
2060 struct slave
*slave_iter
;
2061 struct list_head
*iter
;
2062 bool dummy_slave_update
; /* Ignore this value as caller updates array */
2064 /* Sync against bond_3ad_state_machine_handler() */
2065 spin_lock_bh(&bond
->mode_lock
);
2066 aggregator
= &(SLAVE_AD_INFO(slave
)->aggregator
);
2067 port
= &(SLAVE_AD_INFO(slave
)->port
);
2069 /* if slave is null, the whole port is not initialized */
2071 netdev_warn(bond
->dev
, "Trying to unbind an uninitialized port on %s\n",
2076 netdev_dbg(bond
->dev
, "Unbinding Link Aggregation Group %d\n",
2077 aggregator
->aggregator_identifier
);
2079 /* Tell the partner that this port is not suitable for aggregation */
2080 port
->actor_oper_port_state
&= ~AD_STATE_AGGREGATION
;
2081 __update_lacpdu_from_port(port
);
2082 ad_lacpdu_send(port
);
2084 /* check if this aggregator is occupied */
2085 if (aggregator
->lag_ports
) {
2086 /* check if there are other ports related to this aggregator
2087 * except the port related to this slave(thats ensure us that
2088 * there is a reason to search for new aggregator, and that we
2091 if ((aggregator
->lag_ports
!= port
) ||
2092 (aggregator
->lag_ports
->next_port_in_aggregator
)) {
2093 /* find new aggregator for the related port(s) */
2094 bond_for_each_slave(bond
, slave_iter
, iter
) {
2095 new_aggregator
= &(SLAVE_AD_INFO(slave_iter
)->aggregator
);
2096 /* if the new aggregator is empty, or it is
2097 * connected to our port only
2099 if (!new_aggregator
->lag_ports
||
2100 ((new_aggregator
->lag_ports
== port
) &&
2101 !new_aggregator
->lag_ports
->next_port_in_aggregator
))
2105 new_aggregator
= NULL
;
2107 /* if new aggregator found, copy the aggregator's
2108 * parameters and connect the related lag_ports to the
2111 if ((new_aggregator
) && ((!new_aggregator
->lag_ports
) || ((new_aggregator
->lag_ports
== port
) && !new_aggregator
->lag_ports
->next_port_in_aggregator
))) {
2112 netdev_dbg(bond
->dev
, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2113 aggregator
->aggregator_identifier
,
2114 new_aggregator
->aggregator_identifier
);
2116 if ((new_aggregator
->lag_ports
== port
) &&
2117 new_aggregator
->is_active
) {
2118 netdev_info(bond
->dev
, "Removing an active aggregator\n");
2119 select_new_active_agg
= 1;
2122 new_aggregator
->is_individual
= aggregator
->is_individual
;
2123 new_aggregator
->actor_admin_aggregator_key
= aggregator
->actor_admin_aggregator_key
;
2124 new_aggregator
->actor_oper_aggregator_key
= aggregator
->actor_oper_aggregator_key
;
2125 new_aggregator
->partner_system
= aggregator
->partner_system
;
2126 new_aggregator
->partner_system_priority
= aggregator
->partner_system_priority
;
2127 new_aggregator
->partner_oper_aggregator_key
= aggregator
->partner_oper_aggregator_key
;
2128 new_aggregator
->receive_state
= aggregator
->receive_state
;
2129 new_aggregator
->transmit_state
= aggregator
->transmit_state
;
2130 new_aggregator
->lag_ports
= aggregator
->lag_ports
;
2131 new_aggregator
->is_active
= aggregator
->is_active
;
2132 new_aggregator
->num_of_ports
= aggregator
->num_of_ports
;
2134 /* update the information that is written on
2135 * the ports about the aggregator
2137 for (temp_port
= aggregator
->lag_ports
; temp_port
;
2138 temp_port
= temp_port
->next_port_in_aggregator
) {
2139 temp_port
->aggregator
= new_aggregator
;
2140 temp_port
->actor_port_aggregator_identifier
= new_aggregator
->aggregator_identifier
;
2143 ad_clear_agg(aggregator
);
2145 if (select_new_active_agg
)
2146 ad_agg_selection_logic(__get_first_agg(port
),
2147 &dummy_slave_update
);
2149 netdev_warn(bond
->dev
, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2152 /* in case that the only port related to this
2153 * aggregator is the one we want to remove
2155 select_new_active_agg
= aggregator
->is_active
;
2156 ad_clear_agg(aggregator
);
2157 if (select_new_active_agg
) {
2158 netdev_info(bond
->dev
, "Removing an active aggregator\n");
2159 /* select new active aggregator */
2160 temp_aggregator
= __get_first_agg(port
);
2161 if (temp_aggregator
)
2162 ad_agg_selection_logic(temp_aggregator
,
2163 &dummy_slave_update
);
2168 netdev_dbg(bond
->dev
, "Unbinding port %d\n", port
->actor_port_number
);
2170 /* find the aggregator that this port is connected to */
2171 bond_for_each_slave(bond
, slave_iter
, iter
) {
2172 temp_aggregator
= &(SLAVE_AD_INFO(slave_iter
)->aggregator
);
2174 /* search the port in the aggregator's related ports */
2175 for (temp_port
= temp_aggregator
->lag_ports
; temp_port
;
2176 prev_port
= temp_port
,
2177 temp_port
= temp_port
->next_port_in_aggregator
) {
2178 if (temp_port
== port
) {
2179 /* the aggregator found - detach the port from
2183 prev_port
->next_port_in_aggregator
= temp_port
->next_port_in_aggregator
;
2185 temp_aggregator
->lag_ports
= temp_port
->next_port_in_aggregator
;
2186 temp_aggregator
->num_of_ports
--;
2187 if (__agg_active_ports(temp_aggregator
) == 0) {
2188 select_new_active_agg
= temp_aggregator
->is_active
;
2189 ad_clear_agg(temp_aggregator
);
2190 if (select_new_active_agg
) {
2191 netdev_info(bond
->dev
, "Removing an active aggregator\n");
2192 /* select new active aggregator */
2193 ad_agg_selection_logic(__get_first_agg(port
),
2194 &dummy_slave_update
);
2204 spin_unlock_bh(&bond
->mode_lock
);
2208 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2209 * @bond: bonding struct to work on
2211 * If an ad_actor setting gets changed we need to update the individual port
2212 * settings so the bond device will use the new values when it gets upped.
2214 void bond_3ad_update_ad_actor_settings(struct bonding
*bond
)
2216 struct list_head
*iter
;
2217 struct slave
*slave
;
2221 BOND_AD_INFO(bond
).system
.sys_priority
= bond
->params
.ad_actor_sys_prio
;
2222 if (is_zero_ether_addr(bond
->params
.ad_actor_system
))
2223 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
2224 *((struct mac_addr
*)bond
->dev
->dev_addr
);
2226 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
2227 *((struct mac_addr
*)bond
->params
.ad_actor_system
);
2229 spin_lock_bh(&bond
->mode_lock
);
2230 bond_for_each_slave(bond
, slave
, iter
) {
2231 struct port
*port
= &(SLAVE_AD_INFO(slave
))->port
;
2233 __ad_actor_update_port(port
);
2236 spin_unlock_bh(&bond
->mode_lock
);
2240 * bond_3ad_state_machine_handler - handle state machines timeout
2241 * @bond: bonding struct to work on
2243 * The state machine handling concept in this module is to check every tick
2244 * which state machine should operate any function. The execution order is
2245 * round robin, so when we have an interaction between state machines, the
2246 * reply of one to each other might be delayed until next tick.
2248 * This function also complete the initialization when the agg_select_timer
2249 * times out, and it selects an aggregator for the ports that are yet not
2250 * related to any aggregator, and selects the active aggregator for a bond.
2252 void bond_3ad_state_machine_handler(struct work_struct
*work
)
2254 struct bonding
*bond
= container_of(work
, struct bonding
,
2256 struct aggregator
*aggregator
;
2257 struct list_head
*iter
;
2258 struct slave
*slave
;
2260 bool should_notify_rtnl
= BOND_SLAVE_NOTIFY_LATER
;
2261 bool update_slave_arr
= false;
2263 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2264 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2265 * concurrently due to incoming LACPDU as well.
2267 spin_lock_bh(&bond
->mode_lock
);
2270 /* check if there are any slaves */
2271 if (!bond_has_slaves(bond
))
2274 /* check if agg_select_timer timer after initialize is timed out */
2275 if (BOND_AD_INFO(bond
).agg_select_timer
&&
2276 !(--BOND_AD_INFO(bond
).agg_select_timer
)) {
2277 slave
= bond_first_slave_rcu(bond
);
2278 port
= slave
? &(SLAVE_AD_INFO(slave
)->port
) : NULL
;
2280 /* select the active aggregator for the bond */
2283 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2288 aggregator
= __get_first_agg(port
);
2289 ad_agg_selection_logic(aggregator
, &update_slave_arr
);
2291 bond_3ad_set_carrier(bond
);
2294 /* for each port run the state machines */
2295 bond_for_each_slave_rcu(bond
, slave
, iter
) {
2296 port
= &(SLAVE_AD_INFO(slave
)->port
);
2298 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2303 ad_rx_machine(NULL
, port
);
2304 ad_periodic_machine(port
);
2305 ad_port_selection_logic(port
, &update_slave_arr
);
2306 ad_mux_machine(port
, &update_slave_arr
);
2307 ad_tx_machine(port
);
2308 ad_churn_machine(port
);
2310 /* turn off the BEGIN bit, since we already handled it */
2311 if (port
->sm_vars
& AD_PORT_BEGIN
)
2312 port
->sm_vars
&= ~AD_PORT_BEGIN
;
2316 bond_for_each_slave_rcu(bond
, slave
, iter
) {
2317 if (slave
->should_notify
) {
2318 should_notify_rtnl
= BOND_SLAVE_NOTIFY_NOW
;
2323 spin_unlock_bh(&bond
->mode_lock
);
2325 if (update_slave_arr
)
2326 bond_slave_arr_work_rearm(bond
, 0);
2328 if (should_notify_rtnl
&& rtnl_trylock()) {
2329 bond_slave_state_notify(bond
);
2332 queue_delayed_work(bond
->wq
, &bond
->ad_work
, ad_delta_in_ticks
);
2336 * bond_3ad_rx_indication - handle a received frame
2337 * @lacpdu: received lacpdu
2338 * @slave: slave struct to work on
2339 * @length: length of the data received
2341 * It is assumed that frames that were sent on this NIC don't returned as new
2342 * received frames (loopback). Since only the payload is given to this
2343 * function, it check for loopback.
2345 static int bond_3ad_rx_indication(struct lacpdu
*lacpdu
, struct slave
*slave
,
2349 int ret
= RX_HANDLER_ANOTHER
;
2351 if (length
>= sizeof(struct lacpdu
)) {
2353 port
= &(SLAVE_AD_INFO(slave
)->port
);
2356 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2357 slave
->dev
->name
, slave
->bond
->dev
->name
);
2361 switch (lacpdu
->subtype
) {
2362 case AD_TYPE_LACPDU
:
2363 ret
= RX_HANDLER_CONSUMED
;
2364 netdev_dbg(slave
->bond
->dev
,
2365 "Received LACPDU on port %d slave %s\n",
2366 port
->actor_port_number
,
2368 /* Protect against concurrent state machines */
2369 spin_lock(&slave
->bond
->mode_lock
);
2370 ad_rx_machine(lacpdu
, port
);
2371 spin_unlock(&slave
->bond
->mode_lock
);
2374 case AD_TYPE_MARKER
:
2375 ret
= RX_HANDLER_CONSUMED
;
2376 /* No need to convert fields to Little Endian since we
2377 * don't use the marker's fields.
2380 switch (((struct bond_marker
*)lacpdu
)->tlv_type
) {
2381 case AD_MARKER_INFORMATION_SUBTYPE
:
2382 netdev_dbg(slave
->bond
->dev
, "Received Marker Information on port %d\n",
2383 port
->actor_port_number
);
2384 ad_marker_info_received((struct bond_marker
*)lacpdu
, port
);
2387 case AD_MARKER_RESPONSE_SUBTYPE
:
2388 netdev_dbg(slave
->bond
->dev
, "Received Marker Response on port %d\n",
2389 port
->actor_port_number
);
2390 ad_marker_response_received((struct bond_marker
*)lacpdu
, port
);
2394 netdev_dbg(slave
->bond
->dev
, "Received an unknown Marker subtype on slot %d\n",
2395 port
->actor_port_number
);
2403 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2404 * its current speed and duplex settings.
2406 * @port: the port we'are looking at
2407 * @reset: Boolean to just reset the speed and the duplex part of the key
2409 * The logic to change the oper / admin keys is:
2410 * (a) A full duplex port can participate in LACP with partner.
2411 * (b) When the speed is changed, LACP need to be reinitiated.
2413 static void ad_update_actor_keys(struct port
*port
, bool reset
)
2416 u16 ospeed
= 0, speed
= 0;
2417 u16 old_oper_key
= port
->actor_oper_port_key
;
2419 port
->actor_admin_port_key
&= ~(AD_SPEED_KEY_MASKS
|AD_DUPLEX_KEY_MASKS
);
2421 speed
= __get_link_speed(port
);
2422 ospeed
= (old_oper_key
& AD_SPEED_KEY_MASKS
) >> 1;
2423 duplex
= __get_duplex(port
);
2424 port
->actor_admin_port_key
|= (speed
<< 1) | duplex
;
2426 port
->actor_oper_port_key
= port
->actor_admin_port_key
;
2428 if (old_oper_key
!= port
->actor_oper_port_key
) {
2429 /* Only 'duplex' port participates in LACP */
2431 port
->sm_vars
|= AD_PORT_LACP_ENABLED
;
2433 port
->sm_vars
&= ~AD_PORT_LACP_ENABLED
;
2437 netdev_err(port
->slave
->dev
,
2438 "speed changed to 0 for port %s",
2439 port
->slave
->dev
->name
);
2440 } else if (duplex
&& ospeed
!= speed
) {
2441 /* Speed change restarts LACP state-machine */
2442 port
->sm_vars
|= AD_PORT_BEGIN
;
2449 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2452 * @slave: slave struct to work on
2454 * Handle reselection of aggregator (if needed) for this port.
2456 void bond_3ad_adapter_speed_duplex_changed(struct slave
*slave
)
2460 port
= &(SLAVE_AD_INFO(slave
)->port
);
2462 /* if slave is null, the whole port is not initialized */
2464 netdev_warn(slave
->bond
->dev
,
2465 "speed/duplex changed for uninitialized port %s\n",
2470 spin_lock_bh(&slave
->bond
->mode_lock
);
2471 ad_update_actor_keys(port
, false);
2472 netdev_dbg(slave
->bond
->dev
, "Port %d slave %s changed speed/duplex\n",
2473 port
->actor_port_number
, slave
->dev
->name
);
2474 spin_unlock_bh(&slave
->bond
->mode_lock
);
2478 * bond_3ad_handle_link_change - handle a slave's link status change indication
2479 * @slave: slave struct to work on
2480 * @status: whether the link is now up or down
2482 * Handle reselection of aggregator (if needed) for this port.
2484 void bond_3ad_handle_link_change(struct slave
*slave
, char link
)
2486 struct aggregator
*agg
;
2490 port
= &(SLAVE_AD_INFO(slave
)->port
);
2492 /* if slave is null, the whole port is not initialized */
2494 netdev_warn(slave
->bond
->dev
, "link status changed for uninitialized port on %s\n",
2499 spin_lock_bh(&slave
->bond
->mode_lock
);
2500 /* on link down we are zeroing duplex and speed since
2501 * some of the adaptors(ce1000.lan) report full duplex/speed
2502 * instead of N/A(duplex) / 0(speed).
2504 * on link up we are forcing recheck on the duplex and speed since
2505 * some of he adaptors(ce1000.lan) report.
2507 if (link
== BOND_LINK_UP
) {
2508 port
->is_enabled
= true;
2509 ad_update_actor_keys(port
, false);
2511 /* link has failed */
2512 port
->is_enabled
= false;
2513 ad_update_actor_keys(port
, true);
2515 agg
= __get_first_agg(port
);
2516 ad_agg_selection_logic(agg
, &dummy
);
2518 netdev_dbg(slave
->bond
->dev
, "Port %d changed link status to %s\n",
2519 port
->actor_port_number
,
2520 link
== BOND_LINK_UP
? "UP" : "DOWN");
2522 spin_unlock_bh(&slave
->bond
->mode_lock
);
2524 /* RTNL is held and mode_lock is released so it's safe
2525 * to update slave_array here.
2527 bond_update_slave_arr(slave
->bond
, NULL
);
2531 * bond_3ad_set_carrier - set link state for bonding master
2532 * @bond - bonding structure
2534 * if we have an active aggregator, we're up, if not, we're down.
2535 * Presumes that we cannot have an active aggregator if there are
2536 * no slaves with link up.
2538 * This behavior complies with IEEE 802.3 section 43.3.9.
2540 * Called by bond_set_carrier(). Return zero if carrier state does not
2541 * change, nonzero if it does.
2543 int bond_3ad_set_carrier(struct bonding
*bond
)
2545 struct aggregator
*active
;
2546 struct slave
*first_slave
;
2550 first_slave
= bond_first_slave_rcu(bond
);
2555 active
= __get_active_agg(&(SLAVE_AD_INFO(first_slave
)->aggregator
));
2557 /* are enough slaves available to consider link up? */
2558 if (__agg_active_ports(active
) < bond
->params
.min_links
) {
2559 if (netif_carrier_ok(bond
->dev
)) {
2560 netif_carrier_off(bond
->dev
);
2563 } else if (!netif_carrier_ok(bond
->dev
)) {
2564 netif_carrier_on(bond
->dev
);
2567 } else if (netif_carrier_ok(bond
->dev
)) {
2568 netif_carrier_off(bond
->dev
);
2576 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2577 * @bond: bonding struct to work on
2578 * @ad_info: ad_info struct to fill with the bond's info
2580 * Returns: 0 on success
2583 int __bond_3ad_get_active_agg_info(struct bonding
*bond
,
2584 struct ad_info
*ad_info
)
2586 struct aggregator
*aggregator
= NULL
;
2587 struct list_head
*iter
;
2588 struct slave
*slave
;
2591 bond_for_each_slave_rcu(bond
, slave
, iter
) {
2592 port
= &(SLAVE_AD_INFO(slave
)->port
);
2593 if (port
->aggregator
&& port
->aggregator
->is_active
) {
2594 aggregator
= port
->aggregator
;
2602 ad_info
->aggregator_id
= aggregator
->aggregator_identifier
;
2603 ad_info
->ports
= aggregator
->num_of_ports
;
2604 ad_info
->actor_key
= aggregator
->actor_oper_aggregator_key
;
2605 ad_info
->partner_key
= aggregator
->partner_oper_aggregator_key
;
2606 ether_addr_copy(ad_info
->partner_system
,
2607 aggregator
->partner_system
.mac_addr_value
);
2611 int bond_3ad_get_active_agg_info(struct bonding
*bond
, struct ad_info
*ad_info
)
2616 ret
= __bond_3ad_get_active_agg_info(bond
, ad_info
);
2622 int bond_3ad_lacpdu_recv(const struct sk_buff
*skb
, struct bonding
*bond
,
2623 struct slave
*slave
)
2625 struct lacpdu
*lacpdu
, _lacpdu
;
2627 if (skb
->protocol
!= PKT_TYPE_LACPDU
)
2628 return RX_HANDLER_ANOTHER
;
2630 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb
)->h_dest
, lacpdu_mcast_addr
))
2631 return RX_HANDLER_ANOTHER
;
2633 lacpdu
= skb_header_pointer(skb
, 0, sizeof(_lacpdu
), &_lacpdu
);
2635 return RX_HANDLER_ANOTHER
;
2637 return bond_3ad_rx_indication(lacpdu
, slave
, skb
->len
);
2641 * bond_3ad_update_lacp_rate - change the lacp rate
2642 * @bond - bonding struct
2644 * When modify lacp_rate parameter via sysfs,
2645 * update actor_oper_port_state of each port.
2647 * Hold bond->mode_lock,
2648 * so we can modify port->actor_oper_port_state,
2649 * no matter bond is up or down.
2651 void bond_3ad_update_lacp_rate(struct bonding
*bond
)
2653 struct port
*port
= NULL
;
2654 struct list_head
*iter
;
2655 struct slave
*slave
;
2658 lacp_fast
= bond
->params
.lacp_fast
;
2659 spin_lock_bh(&bond
->mode_lock
);
2660 bond_for_each_slave(bond
, slave
, iter
) {
2661 port
= &(SLAVE_AD_INFO(slave
)->port
);
2663 port
->actor_oper_port_state
|= AD_STATE_LACP_TIMEOUT
;
2665 port
->actor_oper_port_state
&= ~AD_STATE_LACP_TIMEOUT
;
2667 spin_unlock_bh(&bond
->mode_lock
);