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_10000MBPS
,
94 AD_LINK_SPEED_20000MBPS
,
95 AD_LINK_SPEED_25000MBPS
,
96 AD_LINK_SPEED_40000MBPS
,
97 AD_LINK_SPEED_56000MBPS
,
98 AD_LINK_SPEED_100000MBPS
,
101 /* compare MAC addresses */
102 #define MAC_ADDRESS_EQUAL(A, B) \
103 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
105 static const u8 null_mac_addr
[ETH_ALEN
+ 2] __long_aligned
= {
108 static u16 ad_ticks_per_sec
;
109 static const int ad_delta_in_ticks
= (AD_TIMER_INTERVAL
* HZ
) / 1000;
111 static const u8 lacpdu_mcast_addr
[ETH_ALEN
+ 2] __long_aligned
=
112 MULTICAST_LACPDU_ADDR
;
114 /* ================= main 802.3ad protocol functions ================== */
115 static int ad_lacpdu_send(struct port
*port
);
116 static int ad_marker_send(struct port
*port
, struct bond_marker
*marker
);
117 static void ad_mux_machine(struct port
*port
, bool *update_slave_arr
);
118 static void ad_rx_machine(struct lacpdu
*lacpdu
, struct port
*port
);
119 static void ad_tx_machine(struct port
*port
);
120 static void ad_periodic_machine(struct port
*port
);
121 static void ad_port_selection_logic(struct port
*port
, bool *update_slave_arr
);
122 static void ad_agg_selection_logic(struct aggregator
*aggregator
,
123 bool *update_slave_arr
);
124 static void ad_clear_agg(struct aggregator
*aggregator
);
125 static void ad_initialize_agg(struct aggregator
*aggregator
);
126 static void ad_initialize_port(struct port
*port
, int lacp_fast
);
127 static void ad_enable_collecting_distributing(struct port
*port
,
128 bool *update_slave_arr
);
129 static void ad_disable_collecting_distributing(struct port
*port
,
130 bool *update_slave_arr
);
131 static void ad_marker_info_received(struct bond_marker
*marker_info
,
133 static void ad_marker_response_received(struct bond_marker
*marker
,
135 static void ad_update_actor_keys(struct port
*port
, bool reset
);
138 /* ================= api to bonding and kernel code ================== */
141 * __get_bond_by_port - get the port's bonding struct
142 * @port: the port we're looking at
144 * Return @port's bonding struct, or %NULL if it can't be found.
146 static inline struct bonding
*__get_bond_by_port(struct port
*port
)
148 if (port
->slave
== NULL
)
151 return bond_get_bond_by_slave(port
->slave
);
155 * __get_first_agg - get the first aggregator in the bond
156 * @bond: the bond we're looking at
158 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
160 * The caller must hold RCU or RTNL lock.
162 static inline struct aggregator
*__get_first_agg(struct port
*port
)
164 struct bonding
*bond
= __get_bond_by_port(port
);
165 struct slave
*first_slave
;
166 struct aggregator
*agg
;
168 /* If there's no bond for this port, or bond has no slaves */
173 first_slave
= bond_first_slave_rcu(bond
);
174 agg
= first_slave
? &(SLAVE_AD_INFO(first_slave
)->aggregator
) : NULL
;
181 * __agg_has_partner - see if we have a partner
182 * @agg: the agregator we're looking at
184 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
185 * address for the partner). Return 0 if not.
187 static inline int __agg_has_partner(struct aggregator
*agg
)
189 return !is_zero_ether_addr(agg
->partner_system
.mac_addr_value
);
193 * __disable_port - disable the port's slave
194 * @port: the port we're looking at
196 static inline void __disable_port(struct port
*port
)
198 bond_set_slave_inactive_flags(port
->slave
, BOND_SLAVE_NOTIFY_LATER
);
202 * __enable_port - enable the port's slave, if it's up
203 * @port: the port we're looking at
205 static inline void __enable_port(struct port
*port
)
207 struct slave
*slave
= port
->slave
;
209 if ((slave
->link
== BOND_LINK_UP
) && bond_slave_is_up(slave
))
210 bond_set_slave_active_flags(slave
, BOND_SLAVE_NOTIFY_LATER
);
214 * __port_is_enabled - check if the port's slave is in active state
215 * @port: the port we're looking at
217 static inline int __port_is_enabled(struct port
*port
)
219 return bond_is_active_slave(port
->slave
);
223 * __get_agg_selection_mode - get the aggregator selection mode
224 * @port: the port we're looking at
226 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
228 static inline u32
__get_agg_selection_mode(struct port
*port
)
230 struct bonding
*bond
= __get_bond_by_port(port
);
233 return BOND_AD_STABLE
;
235 return bond
->params
.ad_select
;
239 * __check_agg_selection_timer - check if the selection timer has expired
240 * @port: the port we're looking at
242 static inline int __check_agg_selection_timer(struct port
*port
)
244 struct bonding
*bond
= __get_bond_by_port(port
);
249 return BOND_AD_INFO(bond
).agg_select_timer
? 1 : 0;
253 * __get_link_speed - get a port's speed
254 * @port: the port we're looking at
256 * Return @port's speed in 802.3ad enum format. i.e. one of:
258 * %AD_LINK_SPEED_10MBPS,
259 * %AD_LINK_SPEED_100MBPS,
260 * %AD_LINK_SPEED_1000MBPS,
261 * %AD_LINK_SPEED_2500MBPS,
262 * %AD_LINK_SPEED_10000MBPS
263 * %AD_LINK_SPEED_20000MBPS
264 * %AD_LINK_SPEED_25000MBPS
265 * %AD_LINK_SPEED_40000MBPS
266 * %AD_LINK_SPEED_56000MBPS
267 * %AD_LINK_SPEED_100000MBPS
269 static u16
__get_link_speed(struct port
*port
)
271 struct slave
*slave
= port
->slave
;
274 /* this if covers only a special case: when the configuration starts
275 * with link down, it sets the speed to 0.
276 * This is done in spite of the fact that the e100 driver reports 0
277 * to be compatible with MVT in the future.
279 if (slave
->link
!= BOND_LINK_UP
)
282 switch (slave
->speed
) {
284 speed
= AD_LINK_SPEED_10MBPS
;
288 speed
= AD_LINK_SPEED_100MBPS
;
292 speed
= AD_LINK_SPEED_1000MBPS
;
296 speed
= AD_LINK_SPEED_2500MBPS
;
300 speed
= AD_LINK_SPEED_10000MBPS
;
304 speed
= AD_LINK_SPEED_20000MBPS
;
308 speed
= AD_LINK_SPEED_25000MBPS
;
312 speed
= AD_LINK_SPEED_40000MBPS
;
316 speed
= AD_LINK_SPEED_56000MBPS
;
320 speed
= AD_LINK_SPEED_100000MBPS
;
324 /* unknown speed value from ethtool. shouldn't happen */
330 netdev_dbg(slave
->bond
->dev
, "Port %d Received link speed %d update from adapter\n",
331 port
->actor_port_number
, speed
);
336 * __get_duplex - get a port's duplex
337 * @port: the port we're looking at
339 * Return @port's duplex in 802.3ad bitmask format. i.e.:
340 * 0x01 if in full duplex
343 static u8
__get_duplex(struct port
*port
)
345 struct slave
*slave
= port
->slave
;
348 /* handling a special case: when the configuration starts with
349 * link down, it sets the duplex to 0.
351 if (slave
->link
== BOND_LINK_UP
) {
352 switch (slave
->duplex
) {
355 netdev_dbg(slave
->bond
->dev
, "Port %d Received status full duplex update from adapter\n",
356 port
->actor_port_number
);
361 netdev_dbg(slave
->bond
->dev
, "Port %d Received status NOT full duplex update from adapter\n",
362 port
->actor_port_number
);
369 static void __ad_actor_update_port(struct port
*port
)
371 const struct bonding
*bond
= bond_get_bond_by_slave(port
->slave
);
373 port
->actor_system
= BOND_AD_INFO(bond
).system
.sys_mac_addr
;
374 port
->actor_system_priority
= BOND_AD_INFO(bond
).system
.sys_priority
;
380 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
381 * @timer_type: which timer to operate
382 * @par: timer parameter. see below
384 * If @timer_type is %current_while_timer, @par indicates long/short timer.
385 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
386 * %SLOW_PERIODIC_TIME.
388 static u16
__ad_timer_to_ticks(u16 timer_type
, u16 par
)
390 u16 retval
= 0; /* to silence the compiler */
392 switch (timer_type
) {
393 case AD_CURRENT_WHILE_TIMER
: /* for rx machine usage */
395 retval
= (AD_SHORT_TIMEOUT_TIME
*ad_ticks_per_sec
);
397 retval
= (AD_LONG_TIMEOUT_TIME
*ad_ticks_per_sec
);
399 case AD_ACTOR_CHURN_TIMER
: /* for local churn machine */
400 retval
= (AD_CHURN_DETECTION_TIME
*ad_ticks_per_sec
);
402 case AD_PERIODIC_TIMER
: /* for periodic machine */
403 retval
= (par
*ad_ticks_per_sec
); /* long timeout */
405 case AD_PARTNER_CHURN_TIMER
: /* for remote churn machine */
406 retval
= (AD_CHURN_DETECTION_TIME
*ad_ticks_per_sec
);
408 case AD_WAIT_WHILE_TIMER
: /* for selection machine */
409 retval
= (AD_AGGREGATE_WAIT_TIME
*ad_ticks_per_sec
);
417 /* ================= ad_rx_machine helper functions ================== */
420 * __choose_matched - update a port's matched variable from a received lacpdu
421 * @lacpdu: the lacpdu we've received
422 * @port: the port we're looking at
424 * Update the value of the matched variable, using parameter values from a
425 * newly received lacpdu. Parameter values for the partner carried in the
426 * received PDU are compared with the corresponding operational parameter
427 * values for the actor. Matched is set to TRUE if all of these parameters
428 * match and the PDU parameter partner_state.aggregation has the same value as
429 * actor_oper_port_state.aggregation and lacp will actively maintain the link
430 * in the aggregation. Matched is also set to TRUE if the value of
431 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
432 * an individual link and lacp will actively maintain the link. Otherwise,
433 * matched is set to FALSE. LACP is considered to be actively maintaining the
434 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
435 * the actor's actor_oper_port_state.lacp_activity and the PDU's
436 * partner_state.lacp_activity variables are TRUE.
438 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
439 * used here to implement the language from 802.3ad 43.4.9 that requires
440 * recordPDU to "match" the LACPDU parameters to the stored values.
442 static void __choose_matched(struct lacpdu
*lacpdu
, struct port
*port
)
444 /* check if all parameters are alike
445 * or this is individual link(aggregation == FALSE)
446 * then update the state machine Matched variable.
448 if (((ntohs(lacpdu
->partner_port
) == port
->actor_port_number
) &&
449 (ntohs(lacpdu
->partner_port_priority
) == port
->actor_port_priority
) &&
450 MAC_ADDRESS_EQUAL(&(lacpdu
->partner_system
), &(port
->actor_system
)) &&
451 (ntohs(lacpdu
->partner_system_priority
) == port
->actor_system_priority
) &&
452 (ntohs(lacpdu
->partner_key
) == port
->actor_oper_port_key
) &&
453 ((lacpdu
->partner_state
& AD_STATE_AGGREGATION
) == (port
->actor_oper_port_state
& AD_STATE_AGGREGATION
))) ||
454 ((lacpdu
->actor_state
& AD_STATE_AGGREGATION
) == 0)
456 port
->sm_vars
|= AD_PORT_MATCHED
;
458 port
->sm_vars
&= ~AD_PORT_MATCHED
;
463 * __record_pdu - record parameters from a received lacpdu
464 * @lacpdu: the lacpdu we've received
465 * @port: the port we're looking at
467 * Record the parameter values for the Actor carried in a received lacpdu as
468 * the current partner operational parameter values and sets
469 * actor_oper_port_state.defaulted to FALSE.
471 static void __record_pdu(struct lacpdu
*lacpdu
, struct port
*port
)
473 if (lacpdu
&& port
) {
474 struct port_params
*partner
= &port
->partner_oper
;
476 __choose_matched(lacpdu
, port
);
477 /* record the new parameter values for the partner
480 partner
->port_number
= ntohs(lacpdu
->actor_port
);
481 partner
->port_priority
= ntohs(lacpdu
->actor_port_priority
);
482 partner
->system
= lacpdu
->actor_system
;
483 partner
->system_priority
= ntohs(lacpdu
->actor_system_priority
);
484 partner
->key
= ntohs(lacpdu
->actor_key
);
485 partner
->port_state
= lacpdu
->actor_state
;
487 /* set actor_oper_port_state.defaulted to FALSE */
488 port
->actor_oper_port_state
&= ~AD_STATE_DEFAULTED
;
490 /* set the partner sync. to on if the partner is sync,
491 * and the port is matched
493 if ((port
->sm_vars
& AD_PORT_MATCHED
) &&
494 (lacpdu
->actor_state
& AD_STATE_SYNCHRONIZATION
)) {
495 partner
->port_state
|= AD_STATE_SYNCHRONIZATION
;
496 pr_debug("%s partner sync=1\n", port
->slave
->dev
->name
);
498 partner
->port_state
&= ~AD_STATE_SYNCHRONIZATION
;
499 pr_debug("%s partner sync=0\n", port
->slave
->dev
->name
);
505 * __record_default - record default parameters
506 * @port: the port we're looking at
508 * This function records the default parameter values for the partner carried
509 * in the Partner Admin parameters as the current partner operational parameter
510 * values and sets actor_oper_port_state.defaulted to TRUE.
512 static void __record_default(struct port
*port
)
515 /* record the partner admin parameters */
516 memcpy(&port
->partner_oper
, &port
->partner_admin
,
517 sizeof(struct port_params
));
519 /* set actor_oper_port_state.defaulted to true */
520 port
->actor_oper_port_state
|= AD_STATE_DEFAULTED
;
525 * __update_selected - update a port's Selected variable from a received lacpdu
526 * @lacpdu: the lacpdu we've received
527 * @port: the port we're looking at
529 * Update the value of the selected variable, using parameter values from a
530 * newly received lacpdu. The parameter values for the Actor carried in the
531 * received PDU are compared with the corresponding operational parameter
532 * values for the ports partner. If one or more of the comparisons shows that
533 * the value(s) received in the PDU differ from the current operational values,
534 * then selected is set to FALSE and actor_oper_port_state.synchronization is
535 * set to out_of_sync. Otherwise, selected remains unchanged.
537 static void __update_selected(struct lacpdu
*lacpdu
, struct port
*port
)
539 if (lacpdu
&& port
) {
540 const struct port_params
*partner
= &port
->partner_oper
;
542 /* check if any parameter is different then
543 * update the state machine selected variable.
545 if (ntohs(lacpdu
->actor_port
) != partner
->port_number
||
546 ntohs(lacpdu
->actor_port_priority
) != partner
->port_priority
||
547 !MAC_ADDRESS_EQUAL(&lacpdu
->actor_system
, &partner
->system
) ||
548 ntohs(lacpdu
->actor_system_priority
) != partner
->system_priority
||
549 ntohs(lacpdu
->actor_key
) != partner
->key
||
550 (lacpdu
->actor_state
& AD_STATE_AGGREGATION
) != (partner
->port_state
& AD_STATE_AGGREGATION
)) {
551 port
->sm_vars
&= ~AD_PORT_SELECTED
;
557 * __update_default_selected - update a port's Selected variable from Partner
558 * @port: the port we're looking at
560 * This function updates the value of the selected variable, using the partner
561 * administrative parameter values. The administrative values are compared with
562 * the corresponding operational parameter values for the partner. If one or
563 * more of the comparisons shows that the administrative value(s) differ from
564 * the current operational values, then Selected is set to FALSE and
565 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
566 * Selected remains unchanged.
568 static void __update_default_selected(struct port
*port
)
571 const struct port_params
*admin
= &port
->partner_admin
;
572 const struct port_params
*oper
= &port
->partner_oper
;
574 /* check if any parameter is different then
575 * update the state machine selected variable.
577 if (admin
->port_number
!= oper
->port_number
||
578 admin
->port_priority
!= oper
->port_priority
||
579 !MAC_ADDRESS_EQUAL(&admin
->system
, &oper
->system
) ||
580 admin
->system_priority
!= oper
->system_priority
||
581 admin
->key
!= oper
->key
||
582 (admin
->port_state
& AD_STATE_AGGREGATION
)
583 != (oper
->port_state
& AD_STATE_AGGREGATION
)) {
584 port
->sm_vars
&= ~AD_PORT_SELECTED
;
590 * __update_ntt - update a port's ntt variable from a received lacpdu
591 * @lacpdu: the lacpdu we've received
592 * @port: the port we're looking at
594 * Updates the value of the ntt variable, using parameter values from a newly
595 * received lacpdu. The parameter values for the partner carried in the
596 * received PDU are compared with the corresponding operational parameter
597 * values for the Actor. If one or more of the comparisons shows that the
598 * value(s) received in the PDU differ from the current operational values,
599 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
601 static void __update_ntt(struct lacpdu
*lacpdu
, struct port
*port
)
603 /* validate lacpdu and port */
604 if (lacpdu
&& port
) {
605 /* check if any parameter is different then
606 * update the port->ntt.
608 if ((ntohs(lacpdu
->partner_port
) != port
->actor_port_number
) ||
609 (ntohs(lacpdu
->partner_port_priority
) != port
->actor_port_priority
) ||
610 !MAC_ADDRESS_EQUAL(&(lacpdu
->partner_system
), &(port
->actor_system
)) ||
611 (ntohs(lacpdu
->partner_system_priority
) != port
->actor_system_priority
) ||
612 (ntohs(lacpdu
->partner_key
) != port
->actor_oper_port_key
) ||
613 ((lacpdu
->partner_state
& AD_STATE_LACP_ACTIVITY
) != (port
->actor_oper_port_state
& AD_STATE_LACP_ACTIVITY
)) ||
614 ((lacpdu
->partner_state
& AD_STATE_LACP_TIMEOUT
) != (port
->actor_oper_port_state
& AD_STATE_LACP_TIMEOUT
)) ||
615 ((lacpdu
->partner_state
& AD_STATE_SYNCHRONIZATION
) != (port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
)) ||
616 ((lacpdu
->partner_state
& AD_STATE_AGGREGATION
) != (port
->actor_oper_port_state
& AD_STATE_AGGREGATION
))
624 * __agg_ports_are_ready - check if all ports in an aggregator are ready
625 * @aggregator: the aggregator we're looking at
628 static int __agg_ports_are_ready(struct aggregator
*aggregator
)
634 /* scan all ports in this aggregator to verfy if they are
637 for (port
= aggregator
->lag_ports
;
639 port
= port
->next_port_in_aggregator
) {
640 if (!(port
->sm_vars
& AD_PORT_READY_N
)) {
651 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
652 * @aggregator: the aggregator we're looking at
653 * @val: Should the ports' ready bit be set on or off
656 static void __set_agg_ports_ready(struct aggregator
*aggregator
, int val
)
660 for (port
= aggregator
->lag_ports
; port
;
661 port
= port
->next_port_in_aggregator
) {
663 port
->sm_vars
|= AD_PORT_READY
;
665 port
->sm_vars
&= ~AD_PORT_READY
;
669 static int __agg_active_ports(struct aggregator
*agg
)
674 for (port
= agg
->lag_ports
; port
;
675 port
= port
->next_port_in_aggregator
) {
676 if (port
->is_enabled
)
684 * __get_agg_bandwidth - get the total bandwidth of an aggregator
685 * @aggregator: the aggregator we're looking at
688 static u32
__get_agg_bandwidth(struct aggregator
*aggregator
)
690 int nports
= __agg_active_ports(aggregator
);
694 switch (__get_link_speed(aggregator
->lag_ports
)) {
695 case AD_LINK_SPEED_1MBPS
:
698 case AD_LINK_SPEED_10MBPS
:
699 bandwidth
= nports
* 10;
701 case AD_LINK_SPEED_100MBPS
:
702 bandwidth
= nports
* 100;
704 case AD_LINK_SPEED_1000MBPS
:
705 bandwidth
= nports
* 1000;
707 case AD_LINK_SPEED_2500MBPS
:
708 bandwidth
= nports
* 2500;
710 case AD_LINK_SPEED_10000MBPS
:
711 bandwidth
= nports
* 10000;
713 case AD_LINK_SPEED_20000MBPS
:
714 bandwidth
= nports
* 20000;
716 case AD_LINK_SPEED_25000MBPS
:
717 bandwidth
= nports
* 25000;
719 case AD_LINK_SPEED_40000MBPS
:
720 bandwidth
= nports
* 40000;
722 case AD_LINK_SPEED_56000MBPS
:
723 bandwidth
= nports
* 56000;
725 case AD_LINK_SPEED_100000MBPS
:
726 bandwidth
= nports
* 100000;
729 bandwidth
= 0; /* to silence the compiler */
736 * __get_active_agg - get the current active aggregator
737 * @aggregator: the aggregator we're looking at
739 * Caller must hold RCU lock.
741 static struct aggregator
*__get_active_agg(struct aggregator
*aggregator
)
743 struct bonding
*bond
= aggregator
->slave
->bond
;
744 struct list_head
*iter
;
747 bond_for_each_slave_rcu(bond
, slave
, iter
)
748 if (SLAVE_AD_INFO(slave
)->aggregator
.is_active
)
749 return &(SLAVE_AD_INFO(slave
)->aggregator
);
755 * __update_lacpdu_from_port - update a port's lacpdu fields
756 * @port: the port we're looking at
758 static inline void __update_lacpdu_from_port(struct port
*port
)
760 struct lacpdu
*lacpdu
= &port
->lacpdu
;
761 const struct port_params
*partner
= &port
->partner_oper
;
763 /* update current actual Actor parameters
764 * lacpdu->subtype initialized
765 * lacpdu->version_number initialized
766 * lacpdu->tlv_type_actor_info initialized
767 * lacpdu->actor_information_length initialized
770 lacpdu
->actor_system_priority
= htons(port
->actor_system_priority
);
771 lacpdu
->actor_system
= port
->actor_system
;
772 lacpdu
->actor_key
= htons(port
->actor_oper_port_key
);
773 lacpdu
->actor_port_priority
= htons(port
->actor_port_priority
);
774 lacpdu
->actor_port
= htons(port
->actor_port_number
);
775 lacpdu
->actor_state
= port
->actor_oper_port_state
;
776 pr_debug("update lacpdu: %s, actor port state %x\n",
777 port
->slave
->dev
->name
, port
->actor_oper_port_state
);
779 /* lacpdu->reserved_3_1 initialized
780 * lacpdu->tlv_type_partner_info initialized
781 * lacpdu->partner_information_length initialized
784 lacpdu
->partner_system_priority
= htons(partner
->system_priority
);
785 lacpdu
->partner_system
= partner
->system
;
786 lacpdu
->partner_key
= htons(partner
->key
);
787 lacpdu
->partner_port_priority
= htons(partner
->port_priority
);
788 lacpdu
->partner_port
= htons(partner
->port_number
);
789 lacpdu
->partner_state
= partner
->port_state
;
791 /* lacpdu->reserved_3_2 initialized
792 * lacpdu->tlv_type_collector_info initialized
793 * lacpdu->collector_information_length initialized
794 * collector_max_delay initialized
795 * reserved_12[12] initialized
796 * tlv_type_terminator initialized
797 * terminator_length initialized
798 * reserved_50[50] initialized
802 /* ================= main 802.3ad protocol code ========================= */
805 * ad_lacpdu_send - send out a lacpdu packet on a given port
806 * @port: the port we're looking at
808 * Returns: 0 on success
811 static int ad_lacpdu_send(struct port
*port
)
813 struct slave
*slave
= port
->slave
;
815 struct lacpdu_header
*lacpdu_header
;
816 int length
= sizeof(struct lacpdu_header
);
818 skb
= dev_alloc_skb(length
);
822 skb
->dev
= slave
->dev
;
823 skb_reset_mac_header(skb
);
824 skb
->network_header
= skb
->mac_header
+ ETH_HLEN
;
825 skb
->protocol
= PKT_TYPE_LACPDU
;
826 skb
->priority
= TC_PRIO_CONTROL
;
828 lacpdu_header
= (struct lacpdu_header
*)skb_put(skb
, length
);
830 ether_addr_copy(lacpdu_header
->hdr
.h_dest
, lacpdu_mcast_addr
);
831 /* Note: source address is set to be the member's PERMANENT address,
832 * because we use it to identify loopback lacpdus in receive.
834 ether_addr_copy(lacpdu_header
->hdr
.h_source
, slave
->perm_hwaddr
);
835 lacpdu_header
->hdr
.h_proto
= PKT_TYPE_LACPDU
;
837 lacpdu_header
->lacpdu
= port
->lacpdu
;
845 * ad_marker_send - send marker information/response on a given port
846 * @port: the port we're looking at
847 * @marker: marker data to send
849 * Returns: 0 on success
852 static int ad_marker_send(struct port
*port
, struct bond_marker
*marker
)
854 struct slave
*slave
= port
->slave
;
856 struct bond_marker_header
*marker_header
;
857 int length
= sizeof(struct bond_marker_header
);
859 skb
= dev_alloc_skb(length
+ 16);
863 skb_reserve(skb
, 16);
865 skb
->dev
= slave
->dev
;
866 skb_reset_mac_header(skb
);
867 skb
->network_header
= skb
->mac_header
+ ETH_HLEN
;
868 skb
->protocol
= PKT_TYPE_LACPDU
;
870 marker_header
= (struct bond_marker_header
*)skb_put(skb
, length
);
872 ether_addr_copy(marker_header
->hdr
.h_dest
, lacpdu_mcast_addr
);
873 /* Note: source address is set to be the member's PERMANENT address,
874 * because we use it to identify loopback MARKERs in receive.
876 ether_addr_copy(marker_header
->hdr
.h_source
, slave
->perm_hwaddr
);
877 marker_header
->hdr
.h_proto
= PKT_TYPE_LACPDU
;
879 marker_header
->marker
= *marker
;
887 * ad_mux_machine - handle a port's mux state machine
888 * @port: the port we're looking at
889 * @update_slave_arr: Does slave array need update?
891 static void ad_mux_machine(struct port
*port
, bool *update_slave_arr
)
893 mux_states_t last_state
;
895 /* keep current State Machine state to compare later if it was
898 last_state
= port
->sm_mux_state
;
900 if (port
->sm_vars
& AD_PORT_BEGIN
) {
901 port
->sm_mux_state
= AD_MUX_DETACHED
;
903 switch (port
->sm_mux_state
) {
904 case AD_MUX_DETACHED
:
905 if ((port
->sm_vars
& AD_PORT_SELECTED
)
906 || (port
->sm_vars
& AD_PORT_STANDBY
))
907 /* if SELECTED or STANDBY */
908 port
->sm_mux_state
= AD_MUX_WAITING
;
911 /* if SELECTED == FALSE return to DETACH state */
912 if (!(port
->sm_vars
& AD_PORT_SELECTED
)) {
913 port
->sm_vars
&= ~AD_PORT_READY_N
;
914 /* in order to withhold the Selection Logic to
915 * check all ports READY_N value every callback
916 * cycle to update ready variable, we check
917 * READY_N and update READY here
919 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
920 port
->sm_mux_state
= AD_MUX_DETACHED
;
924 /* check if the wait_while_timer expired */
925 if (port
->sm_mux_timer_counter
926 && !(--port
->sm_mux_timer_counter
))
927 port
->sm_vars
|= AD_PORT_READY_N
;
929 /* in order to withhold the selection logic to check
930 * all ports READY_N value every callback cycle to
931 * update ready variable, we check READY_N and update
934 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
936 /* if the wait_while_timer expired, and the port is
937 * in READY state, move to ATTACHED state
939 if ((port
->sm_vars
& AD_PORT_READY
)
940 && !port
->sm_mux_timer_counter
)
941 port
->sm_mux_state
= AD_MUX_ATTACHED
;
943 case AD_MUX_ATTACHED
:
944 /* check also if agg_select_timer expired (so the
945 * edable port will take place only after this timer)
947 if ((port
->sm_vars
& AD_PORT_SELECTED
) &&
948 (port
->partner_oper
.port_state
& AD_STATE_SYNCHRONIZATION
) &&
949 !__check_agg_selection_timer(port
)) {
950 if (port
->aggregator
->is_active
)
952 AD_MUX_COLLECTING_DISTRIBUTING
;
953 } else if (!(port
->sm_vars
& AD_PORT_SELECTED
) ||
954 (port
->sm_vars
& AD_PORT_STANDBY
)) {
955 /* if UNSELECTED or STANDBY */
956 port
->sm_vars
&= ~AD_PORT_READY_N
;
957 /* in order to withhold the selection logic to
958 * check all ports READY_N value every callback
959 * cycle to update ready variable, we check
960 * READY_N and update READY here
962 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
963 port
->sm_mux_state
= AD_MUX_DETACHED
;
964 } else if (port
->aggregator
->is_active
) {
965 port
->actor_oper_port_state
|=
966 AD_STATE_SYNCHRONIZATION
;
969 case AD_MUX_COLLECTING_DISTRIBUTING
:
970 if (!(port
->sm_vars
& AD_PORT_SELECTED
) ||
971 (port
->sm_vars
& AD_PORT_STANDBY
) ||
972 !(port
->partner_oper
.port_state
& AD_STATE_SYNCHRONIZATION
) ||
973 !(port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
)) {
974 port
->sm_mux_state
= AD_MUX_ATTACHED
;
976 /* if port state hasn't changed make
977 * sure that a collecting distributing
978 * port in an active aggregator is enabled
980 if (port
->aggregator
&&
981 port
->aggregator
->is_active
&&
982 !__port_is_enabled(port
)) {
993 /* check if the state machine was changed */
994 if (port
->sm_mux_state
!= last_state
) {
995 pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
996 port
->actor_port_number
,
997 port
->slave
->dev
->name
,
1000 switch (port
->sm_mux_state
) {
1001 case AD_MUX_DETACHED
:
1002 port
->actor_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1003 ad_disable_collecting_distributing(port
,
1005 port
->actor_oper_port_state
&= ~AD_STATE_COLLECTING
;
1006 port
->actor_oper_port_state
&= ~AD_STATE_DISTRIBUTING
;
1009 case AD_MUX_WAITING
:
1010 port
->sm_mux_timer_counter
= __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER
, 0);
1012 case AD_MUX_ATTACHED
:
1013 if (port
->aggregator
->is_active
)
1014 port
->actor_oper_port_state
|=
1015 AD_STATE_SYNCHRONIZATION
;
1017 port
->actor_oper_port_state
&=
1018 ~AD_STATE_SYNCHRONIZATION
;
1019 port
->actor_oper_port_state
&= ~AD_STATE_COLLECTING
;
1020 port
->actor_oper_port_state
&= ~AD_STATE_DISTRIBUTING
;
1021 ad_disable_collecting_distributing(port
,
1025 case AD_MUX_COLLECTING_DISTRIBUTING
:
1026 port
->actor_oper_port_state
|= AD_STATE_COLLECTING
;
1027 port
->actor_oper_port_state
|= AD_STATE_DISTRIBUTING
;
1028 port
->actor_oper_port_state
|= AD_STATE_SYNCHRONIZATION
;
1029 ad_enable_collecting_distributing(port
,
1040 * ad_rx_machine - handle a port's rx State Machine
1041 * @lacpdu: the lacpdu we've received
1042 * @port: the port we're looking at
1044 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1045 * CURRENT. If timer expired set the state machine in the proper state.
1046 * In other cases, this function checks if we need to switch to other state.
1048 static void ad_rx_machine(struct lacpdu
*lacpdu
, struct port
*port
)
1050 rx_states_t last_state
;
1052 /* keep current State Machine state to compare later if it was
1055 last_state
= port
->sm_rx_state
;
1057 /* check if state machine should change state */
1059 /* first, check if port was reinitialized */
1060 if (port
->sm_vars
& AD_PORT_BEGIN
) {
1061 port
->sm_rx_state
= AD_RX_INITIALIZE
;
1062 port
->sm_vars
|= AD_PORT_CHURNED
;
1063 /* check if port is not enabled */
1064 } else if (!(port
->sm_vars
& AD_PORT_BEGIN
)
1065 && !port
->is_enabled
&& !(port
->sm_vars
& AD_PORT_MOVED
))
1066 port
->sm_rx_state
= AD_RX_PORT_DISABLED
;
1067 /* check if new lacpdu arrived */
1068 else if (lacpdu
&& ((port
->sm_rx_state
== AD_RX_EXPIRED
) ||
1069 (port
->sm_rx_state
== AD_RX_DEFAULTED
) ||
1070 (port
->sm_rx_state
== AD_RX_CURRENT
))) {
1071 if (port
->sm_rx_state
!= AD_RX_CURRENT
)
1072 port
->sm_vars
|= AD_PORT_CHURNED
;
1073 port
->sm_rx_timer_counter
= 0;
1074 port
->sm_rx_state
= AD_RX_CURRENT
;
1076 /* if timer is on, and if it is expired */
1077 if (port
->sm_rx_timer_counter
&&
1078 !(--port
->sm_rx_timer_counter
)) {
1079 switch (port
->sm_rx_state
) {
1081 port
->sm_rx_state
= AD_RX_DEFAULTED
;
1084 port
->sm_rx_state
= AD_RX_EXPIRED
;
1090 /* if no lacpdu arrived and no timer is on */
1091 switch (port
->sm_rx_state
) {
1092 case AD_RX_PORT_DISABLED
:
1093 if (port
->sm_vars
& AD_PORT_MOVED
)
1094 port
->sm_rx_state
= AD_RX_INITIALIZE
;
1095 else if (port
->is_enabled
1097 & AD_PORT_LACP_ENABLED
))
1098 port
->sm_rx_state
= AD_RX_EXPIRED
;
1099 else if (port
->is_enabled
1101 & AD_PORT_LACP_ENABLED
) == 0))
1102 port
->sm_rx_state
= AD_RX_LACP_DISABLED
;
1111 /* check if the State machine was changed or new lacpdu arrived */
1112 if ((port
->sm_rx_state
!= last_state
) || (lacpdu
)) {
1113 pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1114 port
->actor_port_number
,
1115 port
->slave
->dev
->name
,
1118 switch (port
->sm_rx_state
) {
1119 case AD_RX_INITIALIZE
:
1120 if (!(port
->actor_oper_port_key
& AD_DUPLEX_KEY_MASKS
))
1121 port
->sm_vars
&= ~AD_PORT_LACP_ENABLED
;
1123 port
->sm_vars
|= AD_PORT_LACP_ENABLED
;
1124 port
->sm_vars
&= ~AD_PORT_SELECTED
;
1125 __record_default(port
);
1126 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1127 port
->sm_vars
&= ~AD_PORT_MOVED
;
1128 port
->sm_rx_state
= AD_RX_PORT_DISABLED
;
1131 case AD_RX_PORT_DISABLED
:
1132 port
->sm_vars
&= ~AD_PORT_MATCHED
;
1134 case AD_RX_LACP_DISABLED
:
1135 port
->sm_vars
&= ~AD_PORT_SELECTED
;
1136 __record_default(port
);
1137 port
->partner_oper
.port_state
&= ~AD_STATE_AGGREGATION
;
1138 port
->sm_vars
|= AD_PORT_MATCHED
;
1139 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1142 /* Reset of the Synchronization flag (Standard 43.4.12)
1143 * This reset cause to disable this port in the
1144 * COLLECTING_DISTRIBUTING state of the mux machine in
1145 * case of EXPIRED even if LINK_DOWN didn't arrive for
1148 port
->partner_oper
.port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1149 port
->sm_vars
&= ~AD_PORT_MATCHED
;
1150 port
->partner_oper
.port_state
|= AD_STATE_LACP_TIMEOUT
;
1151 port
->partner_oper
.port_state
|= AD_STATE_LACP_ACTIVITY
;
1152 port
->sm_rx_timer_counter
= __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER
, (u16
)(AD_SHORT_TIMEOUT
));
1153 port
->actor_oper_port_state
|= AD_STATE_EXPIRED
;
1154 port
->sm_vars
|= AD_PORT_CHURNED
;
1156 case AD_RX_DEFAULTED
:
1157 __update_default_selected(port
);
1158 __record_default(port
);
1159 port
->sm_vars
|= AD_PORT_MATCHED
;
1160 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1163 /* detect loopback situation */
1164 if (MAC_ADDRESS_EQUAL(&(lacpdu
->actor_system
),
1165 &(port
->actor_system
))) {
1166 netdev_err(port
->slave
->bond
->dev
, "An illegal loopback occurred on adapter (%s)\n"
1167 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1168 port
->slave
->dev
->name
);
1171 __update_selected(lacpdu
, port
);
1172 __update_ntt(lacpdu
, port
);
1173 __record_pdu(lacpdu
, port
);
1174 port
->sm_rx_timer_counter
= __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER
, (u16
)(port
->actor_oper_port_state
& AD_STATE_LACP_TIMEOUT
));
1175 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1184 * ad_churn_machine - handle port churn's state machine
1185 * @port: the port we're looking at
1188 static void ad_churn_machine(struct port
*port
)
1190 if (port
->sm_vars
& AD_PORT_CHURNED
) {
1191 port
->sm_vars
&= ~AD_PORT_CHURNED
;
1192 port
->sm_churn_actor_state
= AD_CHURN_MONITOR
;
1193 port
->sm_churn_partner_state
= AD_CHURN_MONITOR
;
1194 port
->sm_churn_actor_timer_counter
=
1195 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER
, 0);
1196 port
->sm_churn_partner_timer_counter
=
1197 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER
, 0);
1200 if (port
->sm_churn_actor_timer_counter
&&
1201 !(--port
->sm_churn_actor_timer_counter
) &&
1202 port
->sm_churn_actor_state
== AD_CHURN_MONITOR
) {
1203 if (port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
) {
1204 port
->sm_churn_actor_state
= AD_NO_CHURN
;
1206 port
->churn_actor_count
++;
1207 port
->sm_churn_actor_state
= AD_CHURN
;
1210 if (port
->sm_churn_partner_timer_counter
&&
1211 !(--port
->sm_churn_partner_timer_counter
) &&
1212 port
->sm_churn_partner_state
== AD_CHURN_MONITOR
) {
1213 if (port
->partner_oper
.port_state
& AD_STATE_SYNCHRONIZATION
) {
1214 port
->sm_churn_partner_state
= AD_NO_CHURN
;
1216 port
->churn_partner_count
++;
1217 port
->sm_churn_partner_state
= AD_CHURN
;
1223 * ad_tx_machine - handle a port's tx state machine
1224 * @port: the port we're looking at
1226 static void ad_tx_machine(struct port
*port
)
1228 /* check if tx timer expired, to verify that we do not send more than
1229 * 3 packets per second
1231 if (port
->sm_tx_timer_counter
&& !(--port
->sm_tx_timer_counter
)) {
1232 /* check if there is something to send */
1233 if (port
->ntt
&& (port
->sm_vars
& AD_PORT_LACP_ENABLED
)) {
1234 __update_lacpdu_from_port(port
);
1236 if (ad_lacpdu_send(port
) >= 0) {
1237 pr_debug("Sent LACPDU on port %d\n",
1238 port
->actor_port_number
);
1240 /* mark ntt as false, so it will not be sent
1241 * again until demanded
1246 /* restart tx timer(to verify that we will not exceed
1247 * AD_MAX_TX_IN_SECOND
1249 port
->sm_tx_timer_counter
= ad_ticks_per_sec
/AD_MAX_TX_IN_SECOND
;
1254 * ad_periodic_machine - handle a port's periodic state machine
1255 * @port: the port we're looking at
1257 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1259 static void ad_periodic_machine(struct port
*port
)
1261 periodic_states_t last_state
;
1263 /* keep current state machine state to compare later if it was changed */
1264 last_state
= port
->sm_periodic_state
;
1266 /* check if port was reinitialized */
1267 if (((port
->sm_vars
& AD_PORT_BEGIN
) || !(port
->sm_vars
& AD_PORT_LACP_ENABLED
) || !port
->is_enabled
) ||
1268 (!(port
->actor_oper_port_state
& AD_STATE_LACP_ACTIVITY
) && !(port
->partner_oper
.port_state
& AD_STATE_LACP_ACTIVITY
))
1270 port
->sm_periodic_state
= AD_NO_PERIODIC
;
1272 /* check if state machine should change state */
1273 else if (port
->sm_periodic_timer_counter
) {
1274 /* check if periodic state machine expired */
1275 if (!(--port
->sm_periodic_timer_counter
)) {
1276 /* if expired then do tx */
1277 port
->sm_periodic_state
= AD_PERIODIC_TX
;
1279 /* If not expired, check if there is some new timeout
1280 * parameter from the partner state
1282 switch (port
->sm_periodic_state
) {
1283 case AD_FAST_PERIODIC
:
1284 if (!(port
->partner_oper
.port_state
1285 & AD_STATE_LACP_TIMEOUT
))
1286 port
->sm_periodic_state
= AD_SLOW_PERIODIC
;
1288 case AD_SLOW_PERIODIC
:
1289 if ((port
->partner_oper
.port_state
& AD_STATE_LACP_TIMEOUT
)) {
1290 port
->sm_periodic_timer_counter
= 0;
1291 port
->sm_periodic_state
= AD_PERIODIC_TX
;
1299 switch (port
->sm_periodic_state
) {
1300 case AD_NO_PERIODIC
:
1301 port
->sm_periodic_state
= AD_FAST_PERIODIC
;
1303 case AD_PERIODIC_TX
:
1304 if (!(port
->partner_oper
.port_state
&
1305 AD_STATE_LACP_TIMEOUT
))
1306 port
->sm_periodic_state
= AD_SLOW_PERIODIC
;
1308 port
->sm_periodic_state
= AD_FAST_PERIODIC
;
1315 /* check if the state machine was changed */
1316 if (port
->sm_periodic_state
!= last_state
) {
1317 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1318 port
->actor_port_number
, last_state
,
1319 port
->sm_periodic_state
);
1320 switch (port
->sm_periodic_state
) {
1321 case AD_NO_PERIODIC
:
1322 port
->sm_periodic_timer_counter
= 0;
1324 case AD_FAST_PERIODIC
:
1325 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1326 port
->sm_periodic_timer_counter
= __ad_timer_to_ticks(AD_PERIODIC_TIMER
, (u16
)(AD_FAST_PERIODIC_TIME
))-1;
1328 case AD_SLOW_PERIODIC
:
1329 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1330 port
->sm_periodic_timer_counter
= __ad_timer_to_ticks(AD_PERIODIC_TIMER
, (u16
)(AD_SLOW_PERIODIC_TIME
))-1;
1332 case AD_PERIODIC_TX
:
1342 * ad_port_selection_logic - select aggregation groups
1343 * @port: the port we're looking at
1344 * @update_slave_arr: Does slave array need update?
1346 * Select aggregation groups, and assign each port for it's aggregetor. The
1347 * selection logic is called in the inititalization (after all the handshkes),
1348 * and after every lacpdu receive (if selected is off).
1350 static void ad_port_selection_logic(struct port
*port
, bool *update_slave_arr
)
1352 struct aggregator
*aggregator
, *free_aggregator
= NULL
, *temp_aggregator
;
1353 struct port
*last_port
= NULL
, *curr_port
;
1354 struct list_head
*iter
;
1355 struct bonding
*bond
;
1356 struct slave
*slave
;
1359 /* if the port is already Selected, do nothing */
1360 if (port
->sm_vars
& AD_PORT_SELECTED
)
1363 bond
= __get_bond_by_port(port
);
1365 /* if the port is connected to other aggregator, detach it */
1366 if (port
->aggregator
) {
1367 /* detach the port from its former aggregator */
1368 temp_aggregator
= port
->aggregator
;
1369 for (curr_port
= temp_aggregator
->lag_ports
; curr_port
;
1370 last_port
= curr_port
,
1371 curr_port
= curr_port
->next_port_in_aggregator
) {
1372 if (curr_port
== port
) {
1373 temp_aggregator
->num_of_ports
--;
1374 /* if it is the first port attached to the
1378 temp_aggregator
->lag_ports
=
1379 port
->next_port_in_aggregator
;
1381 /* not the first port attached to the
1384 last_port
->next_port_in_aggregator
=
1385 port
->next_port_in_aggregator
;
1388 /* clear the port's relations to this
1391 port
->aggregator
= NULL
;
1392 port
->next_port_in_aggregator
= NULL
;
1393 port
->actor_port_aggregator_identifier
= 0;
1395 netdev_dbg(bond
->dev
, "Port %d left LAG %d\n",
1396 port
->actor_port_number
,
1397 temp_aggregator
->aggregator_identifier
);
1398 /* if the aggregator is empty, clear its
1399 * parameters, and set it ready to be attached
1401 if (!temp_aggregator
->lag_ports
)
1402 ad_clear_agg(temp_aggregator
);
1407 /* meaning: the port was related to an aggregator
1408 * but was not on the aggregator port list
1410 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1411 port
->slave
->bond
->dev
->name
,
1412 port
->actor_port_number
,
1413 port
->slave
->dev
->name
,
1414 port
->aggregator
->aggregator_identifier
);
1417 /* search on all aggregators for a suitable aggregator for this port */
1418 bond_for_each_slave(bond
, slave
, iter
) {
1419 aggregator
= &(SLAVE_AD_INFO(slave
)->aggregator
);
1421 /* keep a free aggregator for later use(if needed) */
1422 if (!aggregator
->lag_ports
) {
1423 if (!free_aggregator
)
1424 free_aggregator
= aggregator
;
1427 /* check if current aggregator suits us */
1428 if (((aggregator
->actor_oper_aggregator_key
== port
->actor_oper_port_key
) && /* if all parameters match AND */
1429 MAC_ADDRESS_EQUAL(&(aggregator
->partner_system
), &(port
->partner_oper
.system
)) &&
1430 (aggregator
->partner_system_priority
== port
->partner_oper
.system_priority
) &&
1431 (aggregator
->partner_oper_aggregator_key
== port
->partner_oper
.key
)
1433 ((!MAC_ADDRESS_EQUAL(&(port
->partner_oper
.system
), &(null_mac_addr
)) && /* partner answers */
1434 !aggregator
->is_individual
) /* but is not individual OR */
1437 /* attach to the founded aggregator */
1438 port
->aggregator
= aggregator
;
1439 port
->actor_port_aggregator_identifier
=
1440 port
->aggregator
->aggregator_identifier
;
1441 port
->next_port_in_aggregator
= aggregator
->lag_ports
;
1442 port
->aggregator
->num_of_ports
++;
1443 aggregator
->lag_ports
= port
;
1444 netdev_dbg(bond
->dev
, "Port %d joined LAG %d(existing LAG)\n",
1445 port
->actor_port_number
,
1446 port
->aggregator
->aggregator_identifier
);
1448 /* mark this port as selected */
1449 port
->sm_vars
|= AD_PORT_SELECTED
;
1455 /* the port couldn't find an aggregator - attach it to a new
1459 if (free_aggregator
) {
1460 /* assign port a new aggregator */
1461 port
->aggregator
= free_aggregator
;
1462 port
->actor_port_aggregator_identifier
=
1463 port
->aggregator
->aggregator_identifier
;
1465 /* update the new aggregator's parameters
1466 * if port was responsed from the end-user
1468 if (port
->actor_oper_port_key
& AD_DUPLEX_KEY_MASKS
)
1469 /* if port is full duplex */
1470 port
->aggregator
->is_individual
= false;
1472 port
->aggregator
->is_individual
= true;
1474 port
->aggregator
->actor_admin_aggregator_key
=
1475 port
->actor_admin_port_key
;
1476 port
->aggregator
->actor_oper_aggregator_key
=
1477 port
->actor_oper_port_key
;
1478 port
->aggregator
->partner_system
=
1479 port
->partner_oper
.system
;
1480 port
->aggregator
->partner_system_priority
=
1481 port
->partner_oper
.system_priority
;
1482 port
->aggregator
->partner_oper_aggregator_key
= port
->partner_oper
.key
;
1483 port
->aggregator
->receive_state
= 1;
1484 port
->aggregator
->transmit_state
= 1;
1485 port
->aggregator
->lag_ports
= port
;
1486 port
->aggregator
->num_of_ports
++;
1488 /* mark this port as selected */
1489 port
->sm_vars
|= AD_PORT_SELECTED
;
1491 netdev_dbg(bond
->dev
, "Port %d joined LAG %d(new LAG)\n",
1492 port
->actor_port_number
,
1493 port
->aggregator
->aggregator_identifier
);
1495 netdev_err(bond
->dev
, "Port %d (on %s) did not find a suitable aggregator\n",
1496 port
->actor_port_number
, port
->slave
->dev
->name
);
1499 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1500 * in all aggregator's ports, else set ready=FALSE in all
1501 * aggregator's ports
1503 __set_agg_ports_ready(port
->aggregator
,
1504 __agg_ports_are_ready(port
->aggregator
));
1506 aggregator
= __get_first_agg(port
);
1507 ad_agg_selection_logic(aggregator
, update_slave_arr
);
1509 if (!port
->aggregator
->is_active
)
1510 port
->actor_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1513 /* Decide if "agg" is a better choice for the new active aggregator that
1514 * the current best, according to the ad_select policy.
1516 static struct aggregator
*ad_agg_selection_test(struct aggregator
*best
,
1517 struct aggregator
*curr
)
1519 /* 0. If no best, select current.
1521 * 1. If the current agg is not individual, and the best is
1522 * individual, select current.
1524 * 2. If current agg is individual and the best is not, keep best.
1526 * 3. Therefore, current and best are both individual or both not
1529 * 3a. If current agg partner replied, and best agg partner did not,
1532 * 3b. If current agg partner did not reply and best agg partner
1533 * did reply, keep best.
1535 * 4. Therefore, current and best both have partner replies or
1536 * both do not, so perform selection policy:
1538 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1539 * select by bandwidth.
1541 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1546 if (!curr
->is_individual
&& best
->is_individual
)
1549 if (curr
->is_individual
&& !best
->is_individual
)
1552 if (__agg_has_partner(curr
) && !__agg_has_partner(best
))
1555 if (!__agg_has_partner(curr
) && __agg_has_partner(best
))
1558 switch (__get_agg_selection_mode(curr
->lag_ports
)) {
1560 if (__agg_active_ports(curr
) > __agg_active_ports(best
))
1563 if (__agg_active_ports(curr
) < __agg_active_ports(best
))
1567 case BOND_AD_STABLE
:
1568 case BOND_AD_BANDWIDTH
:
1569 if (__get_agg_bandwidth(curr
) > __get_agg_bandwidth(best
))
1575 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1576 curr
->slave
->bond
->dev
->name
,
1577 __get_agg_selection_mode(curr
->lag_ports
));
1584 static int agg_device_up(const struct aggregator
*agg
)
1586 struct port
*port
= agg
->lag_ports
;
1591 for (port
= agg
->lag_ports
; port
;
1592 port
= port
->next_port_in_aggregator
) {
1593 if (netif_running(port
->slave
->dev
) &&
1594 netif_carrier_ok(port
->slave
->dev
))
1602 * ad_agg_selection_logic - select an aggregation group for a team
1603 * @aggregator: the aggregator we're looking at
1604 * @update_slave_arr: Does slave array need update?
1606 * It is assumed that only one aggregator may be selected for a team.
1608 * The logic of this function is to select the aggregator according to
1609 * the ad_select policy:
1611 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1612 * it, and to reselect the active aggregator only if the previous
1613 * aggregator has no more ports related to it.
1615 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1616 * bandwidth, and reselect whenever a link state change takes place or the
1617 * set of slaves in the bond changes.
1619 * BOND_AD_COUNT: select the aggregator with largest number of ports
1620 * (slaves), and reselect whenever a link state change takes place or the
1621 * set of slaves in the bond changes.
1623 * FIXME: this function MUST be called with the first agg in the bond, or
1624 * __get_active_agg() won't work correctly. This function should be better
1625 * called with the bond itself, and retrieve the first agg from it.
1627 static void ad_agg_selection_logic(struct aggregator
*agg
,
1628 bool *update_slave_arr
)
1630 struct aggregator
*best
, *active
, *origin
;
1631 struct bonding
*bond
= agg
->slave
->bond
;
1632 struct list_head
*iter
;
1633 struct slave
*slave
;
1638 active
= __get_active_agg(agg
);
1639 best
= (active
&& agg_device_up(active
)) ? active
: NULL
;
1641 bond_for_each_slave_rcu(bond
, slave
, iter
) {
1642 agg
= &(SLAVE_AD_INFO(slave
)->aggregator
);
1646 if (__agg_active_ports(agg
) && agg_device_up(agg
))
1647 best
= ad_agg_selection_test(best
, agg
);
1651 __get_agg_selection_mode(best
->lag_ports
) == BOND_AD_STABLE
) {
1652 /* For the STABLE policy, don't replace the old active
1653 * aggregator if it's still active (it has an answering
1654 * partner) or if both the best and active don't have an
1655 * answering partner.
1657 if (active
&& active
->lag_ports
&&
1658 __agg_active_ports(active
) &&
1659 (__agg_has_partner(active
) ||
1660 (!__agg_has_partner(active
) &&
1661 !__agg_has_partner(best
)))) {
1662 if (!(!active
->actor_oper_aggregator_key
&&
1663 best
->actor_oper_aggregator_key
)) {
1665 active
->is_active
= 1;
1670 if (best
&& (best
== active
)) {
1672 active
->is_active
= 1;
1675 /* if there is new best aggregator, activate it */
1677 netdev_dbg(bond
->dev
, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1678 best
->aggregator_identifier
, best
->num_of_ports
,
1679 best
->actor_oper_aggregator_key
,
1680 best
->partner_oper_aggregator_key
,
1681 best
->is_individual
, best
->is_active
);
1682 netdev_dbg(bond
->dev
, "best ports %p slave %p %s\n",
1683 best
->lag_ports
, best
->slave
,
1684 best
->slave
? best
->slave
->dev
->name
: "NULL");
1686 bond_for_each_slave_rcu(bond
, slave
, iter
) {
1687 agg
= &(SLAVE_AD_INFO(slave
)->aggregator
);
1689 netdev_dbg(bond
->dev
, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1690 agg
->aggregator_identifier
, agg
->num_of_ports
,
1691 agg
->actor_oper_aggregator_key
,
1692 agg
->partner_oper_aggregator_key
,
1693 agg
->is_individual
, agg
->is_active
);
1696 /* check if any partner replys */
1697 if (best
->is_individual
) {
1698 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1700 best
->slave
->bond
->dev
->name
: "NULL");
1703 best
->is_active
= 1;
1704 netdev_dbg(bond
->dev
, "LAG %d chosen as the active LAG\n",
1705 best
->aggregator_identifier
);
1706 netdev_dbg(bond
->dev
, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1707 best
->aggregator_identifier
, best
->num_of_ports
,
1708 best
->actor_oper_aggregator_key
,
1709 best
->partner_oper_aggregator_key
,
1710 best
->is_individual
, best
->is_active
);
1712 /* disable the ports that were related to the former
1716 for (port
= active
->lag_ports
; port
;
1717 port
= port
->next_port_in_aggregator
) {
1718 __disable_port(port
);
1721 /* Slave array needs update. */
1722 *update_slave_arr
= true;
1725 /* if the selected aggregator is of join individuals
1726 * (partner_system is NULL), enable their ports
1728 active
= __get_active_agg(origin
);
1731 if (!__agg_has_partner(active
)) {
1732 for (port
= active
->lag_ports
; port
;
1733 port
= port
->next_port_in_aggregator
) {
1734 __enable_port(port
);
1741 bond_3ad_set_carrier(bond
);
1745 * ad_clear_agg - clear a given aggregator's parameters
1746 * @aggregator: the aggregator we're looking at
1748 static void ad_clear_agg(struct aggregator
*aggregator
)
1751 aggregator
->is_individual
= false;
1752 aggregator
->actor_admin_aggregator_key
= 0;
1753 aggregator
->actor_oper_aggregator_key
= 0;
1754 eth_zero_addr(aggregator
->partner_system
.mac_addr_value
);
1755 aggregator
->partner_system_priority
= 0;
1756 aggregator
->partner_oper_aggregator_key
= 0;
1757 aggregator
->receive_state
= 0;
1758 aggregator
->transmit_state
= 0;
1759 aggregator
->lag_ports
= NULL
;
1760 aggregator
->is_active
= 0;
1761 aggregator
->num_of_ports
= 0;
1762 pr_debug("LAG %d was cleared\n",
1763 aggregator
->aggregator_identifier
);
1768 * ad_initialize_agg - initialize a given aggregator's parameters
1769 * @aggregator: the aggregator we're looking at
1771 static void ad_initialize_agg(struct aggregator
*aggregator
)
1774 ad_clear_agg(aggregator
);
1776 eth_zero_addr(aggregator
->aggregator_mac_address
.mac_addr_value
);
1777 aggregator
->aggregator_identifier
= 0;
1778 aggregator
->slave
= NULL
;
1783 * ad_initialize_port - initialize a given port's parameters
1784 * @aggregator: the aggregator we're looking at
1785 * @lacp_fast: boolean. whether fast periodic should be used
1787 static void ad_initialize_port(struct port
*port
, int lacp_fast
)
1789 static const struct port_params tmpl
= {
1790 .system_priority
= 0xffff,
1793 .port_priority
= 0xff,
1796 static const struct lacpdu lacpdu
= {
1798 .version_number
= 0x01,
1799 .tlv_type_actor_info
= 0x01,
1800 .actor_information_length
= 0x14,
1801 .tlv_type_partner_info
= 0x02,
1802 .partner_information_length
= 0x14,
1803 .tlv_type_collector_info
= 0x03,
1804 .collector_information_length
= 0x10,
1805 .collector_max_delay
= htons(AD_COLLECTOR_MAX_DELAY
),
1809 port
->actor_port_priority
= 0xff;
1810 port
->actor_port_aggregator_identifier
= 0;
1812 port
->actor_admin_port_state
= AD_STATE_AGGREGATION
|
1813 AD_STATE_LACP_ACTIVITY
;
1814 port
->actor_oper_port_state
= AD_STATE_AGGREGATION
|
1815 AD_STATE_LACP_ACTIVITY
;
1818 port
->actor_oper_port_state
|= AD_STATE_LACP_TIMEOUT
;
1820 memcpy(&port
->partner_admin
, &tmpl
, sizeof(tmpl
));
1821 memcpy(&port
->partner_oper
, &tmpl
, sizeof(tmpl
));
1823 port
->is_enabled
= true;
1824 /* private parameters */
1825 port
->sm_vars
= AD_PORT_BEGIN
| AD_PORT_LACP_ENABLED
;
1826 port
->sm_rx_state
= 0;
1827 port
->sm_rx_timer_counter
= 0;
1828 port
->sm_periodic_state
= 0;
1829 port
->sm_periodic_timer_counter
= 0;
1830 port
->sm_mux_state
= 0;
1831 port
->sm_mux_timer_counter
= 0;
1832 port
->sm_tx_state
= 0;
1833 port
->aggregator
= NULL
;
1834 port
->next_port_in_aggregator
= NULL
;
1835 port
->transaction_id
= 0;
1837 port
->sm_churn_actor_timer_counter
= 0;
1838 port
->sm_churn_actor_state
= 0;
1839 port
->churn_actor_count
= 0;
1840 port
->sm_churn_partner_timer_counter
= 0;
1841 port
->sm_churn_partner_state
= 0;
1842 port
->churn_partner_count
= 0;
1844 memcpy(&port
->lacpdu
, &lacpdu
, sizeof(lacpdu
));
1849 * ad_enable_collecting_distributing - enable a port's transmit/receive
1850 * @port: the port we're looking at
1851 * @update_slave_arr: Does slave array need update?
1853 * Enable @port if it's in an active aggregator
1855 static void ad_enable_collecting_distributing(struct port
*port
,
1856 bool *update_slave_arr
)
1858 if (port
->aggregator
->is_active
) {
1859 pr_debug("Enabling port %d(LAG %d)\n",
1860 port
->actor_port_number
,
1861 port
->aggregator
->aggregator_identifier
);
1862 __enable_port(port
);
1863 /* Slave array needs update */
1864 *update_slave_arr
= true;
1869 * ad_disable_collecting_distributing - disable a port's transmit/receive
1870 * @port: the port we're looking at
1871 * @update_slave_arr: Does slave array need update?
1873 static void ad_disable_collecting_distributing(struct port
*port
,
1874 bool *update_slave_arr
)
1876 if (port
->aggregator
&&
1877 !MAC_ADDRESS_EQUAL(&(port
->aggregator
->partner_system
),
1878 &(null_mac_addr
))) {
1879 pr_debug("Disabling port %d(LAG %d)\n",
1880 port
->actor_port_number
,
1881 port
->aggregator
->aggregator_identifier
);
1882 __disable_port(port
);
1883 /* Slave array needs an update */
1884 *update_slave_arr
= true;
1889 * ad_marker_info_received - handle receive of a Marker information frame
1890 * @marker_info: Marker info received
1891 * @port: the port we're looking at
1893 static void ad_marker_info_received(struct bond_marker
*marker_info
,
1896 struct bond_marker marker
;
1898 /* copy the received marker data to the response marker */
1899 memcpy(&marker
, marker_info
, sizeof(struct bond_marker
));
1900 /* change the marker subtype to marker response */
1901 marker
.tlv_type
= AD_MARKER_RESPONSE_SUBTYPE
;
1903 /* send the marker response */
1904 if (ad_marker_send(port
, &marker
) >= 0) {
1905 pr_debug("Sent Marker Response on port %d\n",
1906 port
->actor_port_number
);
1911 * ad_marker_response_received - handle receive of a marker response frame
1912 * @marker: marker PDU received
1913 * @port: the port we're looking at
1915 * This function does nothing since we decided not to implement send and handle
1916 * response for marker PDU's, in this stage, but only to respond to marker
1919 static void ad_marker_response_received(struct bond_marker
*marker
,
1922 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1925 /* ========= AD exported functions to the main bonding code ========= */
1927 /* Check aggregators status in team every T seconds */
1928 #define AD_AGGREGATOR_SELECTION_TIMER 8
1931 * bond_3ad_initiate_agg_selection - initate aggregator selection
1932 * @bond: bonding struct
1934 * Set the aggregation selection timer, to initiate an agg selection in
1935 * the very near future. Called during first initialization, and during
1936 * any down to up transitions of the bond.
1938 void bond_3ad_initiate_agg_selection(struct bonding
*bond
, int timeout
)
1940 BOND_AD_INFO(bond
).agg_select_timer
= timeout
;
1944 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1945 * @bond: bonding struct to work on
1946 * @tick_resolution: tick duration (millisecond resolution)
1948 * Can be called only after the mac address of the bond is set.
1950 void bond_3ad_initialize(struct bonding
*bond
, u16 tick_resolution
)
1952 /* check that the bond is not initialized yet */
1953 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond
).system
.sys_mac_addr
),
1954 bond
->dev
->dev_addr
)) {
1956 BOND_AD_INFO(bond
).aggregator_identifier
= 0;
1958 BOND_AD_INFO(bond
).system
.sys_priority
=
1959 bond
->params
.ad_actor_sys_prio
;
1960 if (is_zero_ether_addr(bond
->params
.ad_actor_system
))
1961 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
1962 *((struct mac_addr
*)bond
->dev
->dev_addr
);
1964 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
1965 *((struct mac_addr
*)bond
->params
.ad_actor_system
);
1967 /* initialize how many times this module is called in one
1968 * second (should be about every 100ms)
1970 ad_ticks_per_sec
= tick_resolution
;
1972 bond_3ad_initiate_agg_selection(bond
,
1973 AD_AGGREGATOR_SELECTION_TIMER
*
1979 * bond_3ad_bind_slave - initialize a slave's port
1980 * @slave: slave struct to work on
1982 * Returns: 0 on success
1985 void bond_3ad_bind_slave(struct slave
*slave
)
1987 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
1989 struct aggregator
*aggregator
;
1991 /* check that the slave has not been initialized yet. */
1992 if (SLAVE_AD_INFO(slave
)->port
.slave
!= slave
) {
1994 /* port initialization */
1995 port
= &(SLAVE_AD_INFO(slave
)->port
);
1997 ad_initialize_port(port
, bond
->params
.lacp_fast
);
1999 port
->slave
= slave
;
2000 port
->actor_port_number
= SLAVE_AD_INFO(slave
)->id
;
2001 /* key is determined according to the link speed, duplex and
2004 port
->actor_admin_port_key
= bond
->params
.ad_user_port_key
<< 6;
2005 ad_update_actor_keys(port
, false);
2006 /* actor system is the bond's system */
2007 __ad_actor_update_port(port
);
2008 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2009 * lacpdu's are sent in one second)
2011 port
->sm_tx_timer_counter
= ad_ticks_per_sec
/AD_MAX_TX_IN_SECOND
;
2013 __disable_port(port
);
2015 /* aggregator initialization */
2016 aggregator
= &(SLAVE_AD_INFO(slave
)->aggregator
);
2018 ad_initialize_agg(aggregator
);
2020 aggregator
->aggregator_mac_address
= *((struct mac_addr
*)bond
->dev
->dev_addr
);
2021 aggregator
->aggregator_identifier
= ++BOND_AD_INFO(bond
).aggregator_identifier
;
2022 aggregator
->slave
= slave
;
2023 aggregator
->is_active
= 0;
2024 aggregator
->num_of_ports
= 0;
2029 * bond_3ad_unbind_slave - deinitialize a slave's port
2030 * @slave: slave struct to work on
2032 * Search for the aggregator that is related to this port, remove the
2033 * aggregator and assign another aggregator for other port related to it
2034 * (if any), and remove the port.
2036 void bond_3ad_unbind_slave(struct slave
*slave
)
2038 struct port
*port
, *prev_port
, *temp_port
;
2039 struct aggregator
*aggregator
, *new_aggregator
, *temp_aggregator
;
2040 int select_new_active_agg
= 0;
2041 struct bonding
*bond
= slave
->bond
;
2042 struct slave
*slave_iter
;
2043 struct list_head
*iter
;
2044 bool dummy_slave_update
; /* Ignore this value as caller updates array */
2046 /* Sync against bond_3ad_state_machine_handler() */
2047 spin_lock_bh(&bond
->mode_lock
);
2048 aggregator
= &(SLAVE_AD_INFO(slave
)->aggregator
);
2049 port
= &(SLAVE_AD_INFO(slave
)->port
);
2051 /* if slave is null, the whole port is not initialized */
2053 netdev_warn(bond
->dev
, "Trying to unbind an uninitialized port on %s\n",
2058 netdev_dbg(bond
->dev
, "Unbinding Link Aggregation Group %d\n",
2059 aggregator
->aggregator_identifier
);
2061 /* Tell the partner that this port is not suitable for aggregation */
2062 port
->actor_oper_port_state
&= ~AD_STATE_AGGREGATION
;
2063 __update_lacpdu_from_port(port
);
2064 ad_lacpdu_send(port
);
2066 /* check if this aggregator is occupied */
2067 if (aggregator
->lag_ports
) {
2068 /* check if there are other ports related to this aggregator
2069 * except the port related to this slave(thats ensure us that
2070 * there is a reason to search for new aggregator, and that we
2073 if ((aggregator
->lag_ports
!= port
) ||
2074 (aggregator
->lag_ports
->next_port_in_aggregator
)) {
2075 /* find new aggregator for the related port(s) */
2076 bond_for_each_slave(bond
, slave_iter
, iter
) {
2077 new_aggregator
= &(SLAVE_AD_INFO(slave_iter
)->aggregator
);
2078 /* if the new aggregator is empty, or it is
2079 * connected to our port only
2081 if (!new_aggregator
->lag_ports
||
2082 ((new_aggregator
->lag_ports
== port
) &&
2083 !new_aggregator
->lag_ports
->next_port_in_aggregator
))
2087 new_aggregator
= NULL
;
2089 /* if new aggregator found, copy the aggregator's
2090 * parameters and connect the related lag_ports to the
2093 if ((new_aggregator
) && ((!new_aggregator
->lag_ports
) || ((new_aggregator
->lag_ports
== port
) && !new_aggregator
->lag_ports
->next_port_in_aggregator
))) {
2094 netdev_dbg(bond
->dev
, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2095 aggregator
->aggregator_identifier
,
2096 new_aggregator
->aggregator_identifier
);
2098 if ((new_aggregator
->lag_ports
== port
) &&
2099 new_aggregator
->is_active
) {
2100 netdev_info(bond
->dev
, "Removing an active aggregator\n");
2101 select_new_active_agg
= 1;
2104 new_aggregator
->is_individual
= aggregator
->is_individual
;
2105 new_aggregator
->actor_admin_aggregator_key
= aggregator
->actor_admin_aggregator_key
;
2106 new_aggregator
->actor_oper_aggregator_key
= aggregator
->actor_oper_aggregator_key
;
2107 new_aggregator
->partner_system
= aggregator
->partner_system
;
2108 new_aggregator
->partner_system_priority
= aggregator
->partner_system_priority
;
2109 new_aggregator
->partner_oper_aggregator_key
= aggregator
->partner_oper_aggregator_key
;
2110 new_aggregator
->receive_state
= aggregator
->receive_state
;
2111 new_aggregator
->transmit_state
= aggregator
->transmit_state
;
2112 new_aggregator
->lag_ports
= aggregator
->lag_ports
;
2113 new_aggregator
->is_active
= aggregator
->is_active
;
2114 new_aggregator
->num_of_ports
= aggregator
->num_of_ports
;
2116 /* update the information that is written on
2117 * the ports about the aggregator
2119 for (temp_port
= aggregator
->lag_ports
; temp_port
;
2120 temp_port
= temp_port
->next_port_in_aggregator
) {
2121 temp_port
->aggregator
= new_aggregator
;
2122 temp_port
->actor_port_aggregator_identifier
= new_aggregator
->aggregator_identifier
;
2125 ad_clear_agg(aggregator
);
2127 if (select_new_active_agg
)
2128 ad_agg_selection_logic(__get_first_agg(port
),
2129 &dummy_slave_update
);
2131 netdev_warn(bond
->dev
, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2134 /* in case that the only port related to this
2135 * aggregator is the one we want to remove
2137 select_new_active_agg
= aggregator
->is_active
;
2138 ad_clear_agg(aggregator
);
2139 if (select_new_active_agg
) {
2140 netdev_info(bond
->dev
, "Removing an active aggregator\n");
2141 /* select new active aggregator */
2142 temp_aggregator
= __get_first_agg(port
);
2143 if (temp_aggregator
)
2144 ad_agg_selection_logic(temp_aggregator
,
2145 &dummy_slave_update
);
2150 netdev_dbg(bond
->dev
, "Unbinding port %d\n", port
->actor_port_number
);
2152 /* find the aggregator that this port is connected to */
2153 bond_for_each_slave(bond
, slave_iter
, iter
) {
2154 temp_aggregator
= &(SLAVE_AD_INFO(slave_iter
)->aggregator
);
2156 /* search the port in the aggregator's related ports */
2157 for (temp_port
= temp_aggregator
->lag_ports
; temp_port
;
2158 prev_port
= temp_port
,
2159 temp_port
= temp_port
->next_port_in_aggregator
) {
2160 if (temp_port
== port
) {
2161 /* the aggregator found - detach the port from
2165 prev_port
->next_port_in_aggregator
= temp_port
->next_port_in_aggregator
;
2167 temp_aggregator
->lag_ports
= temp_port
->next_port_in_aggregator
;
2168 temp_aggregator
->num_of_ports
--;
2169 if (__agg_active_ports(temp_aggregator
) == 0) {
2170 select_new_active_agg
= temp_aggregator
->is_active
;
2171 ad_clear_agg(temp_aggregator
);
2172 if (select_new_active_agg
) {
2173 netdev_info(bond
->dev
, "Removing an active aggregator\n");
2174 /* select new active aggregator */
2175 ad_agg_selection_logic(__get_first_agg(port
),
2176 &dummy_slave_update
);
2186 spin_unlock_bh(&bond
->mode_lock
);
2190 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2191 * @bond: bonding struct to work on
2193 * If an ad_actor setting gets changed we need to update the individual port
2194 * settings so the bond device will use the new values when it gets upped.
2196 void bond_3ad_update_ad_actor_settings(struct bonding
*bond
)
2198 struct list_head
*iter
;
2199 struct slave
*slave
;
2203 BOND_AD_INFO(bond
).system
.sys_priority
= bond
->params
.ad_actor_sys_prio
;
2204 if (is_zero_ether_addr(bond
->params
.ad_actor_system
))
2205 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
2206 *((struct mac_addr
*)bond
->dev
->dev_addr
);
2208 BOND_AD_INFO(bond
).system
.sys_mac_addr
=
2209 *((struct mac_addr
*)bond
->params
.ad_actor_system
);
2211 spin_lock_bh(&bond
->mode_lock
);
2212 bond_for_each_slave(bond
, slave
, iter
) {
2213 struct port
*port
= &(SLAVE_AD_INFO(slave
))->port
;
2215 __ad_actor_update_port(port
);
2218 spin_unlock_bh(&bond
->mode_lock
);
2222 * bond_3ad_state_machine_handler - handle state machines timeout
2223 * @bond: bonding struct to work on
2225 * The state machine handling concept in this module is to check every tick
2226 * which state machine should operate any function. The execution order is
2227 * round robin, so when we have an interaction between state machines, the
2228 * reply of one to each other might be delayed until next tick.
2230 * This function also complete the initialization when the agg_select_timer
2231 * times out, and it selects an aggregator for the ports that are yet not
2232 * related to any aggregator, and selects the active aggregator for a bond.
2234 void bond_3ad_state_machine_handler(struct work_struct
*work
)
2236 struct bonding
*bond
= container_of(work
, struct bonding
,
2238 struct aggregator
*aggregator
;
2239 struct list_head
*iter
;
2240 struct slave
*slave
;
2242 bool should_notify_rtnl
= BOND_SLAVE_NOTIFY_LATER
;
2243 bool update_slave_arr
= false;
2245 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2246 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2247 * concurrently due to incoming LACPDU as well.
2249 spin_lock_bh(&bond
->mode_lock
);
2252 /* check if there are any slaves */
2253 if (!bond_has_slaves(bond
))
2256 /* check if agg_select_timer timer after initialize is timed out */
2257 if (BOND_AD_INFO(bond
).agg_select_timer
&&
2258 !(--BOND_AD_INFO(bond
).agg_select_timer
)) {
2259 slave
= bond_first_slave_rcu(bond
);
2260 port
= slave
? &(SLAVE_AD_INFO(slave
)->port
) : NULL
;
2262 /* select the active aggregator for the bond */
2265 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2270 aggregator
= __get_first_agg(port
);
2271 ad_agg_selection_logic(aggregator
, &update_slave_arr
);
2273 bond_3ad_set_carrier(bond
);
2276 /* for each port run the state machines */
2277 bond_for_each_slave_rcu(bond
, slave
, iter
) {
2278 port
= &(SLAVE_AD_INFO(slave
)->port
);
2280 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2285 ad_rx_machine(NULL
, port
);
2286 ad_periodic_machine(port
);
2287 ad_port_selection_logic(port
, &update_slave_arr
);
2288 ad_mux_machine(port
, &update_slave_arr
);
2289 ad_tx_machine(port
);
2290 ad_churn_machine(port
);
2292 /* turn off the BEGIN bit, since we already handled it */
2293 if (port
->sm_vars
& AD_PORT_BEGIN
)
2294 port
->sm_vars
&= ~AD_PORT_BEGIN
;
2298 bond_for_each_slave_rcu(bond
, slave
, iter
) {
2299 if (slave
->should_notify
) {
2300 should_notify_rtnl
= BOND_SLAVE_NOTIFY_NOW
;
2305 spin_unlock_bh(&bond
->mode_lock
);
2307 if (update_slave_arr
)
2308 bond_slave_arr_work_rearm(bond
, 0);
2310 if (should_notify_rtnl
&& rtnl_trylock()) {
2311 bond_slave_state_notify(bond
);
2314 queue_delayed_work(bond
->wq
, &bond
->ad_work
, ad_delta_in_ticks
);
2318 * bond_3ad_rx_indication - handle a received frame
2319 * @lacpdu: received lacpdu
2320 * @slave: slave struct to work on
2321 * @length: length of the data received
2323 * It is assumed that frames that were sent on this NIC don't returned as new
2324 * received frames (loopback). Since only the payload is given to this
2325 * function, it check for loopback.
2327 static int bond_3ad_rx_indication(struct lacpdu
*lacpdu
, struct slave
*slave
,
2331 int ret
= RX_HANDLER_ANOTHER
;
2333 if (length
>= sizeof(struct lacpdu
)) {
2335 port
= &(SLAVE_AD_INFO(slave
)->port
);
2338 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2339 slave
->dev
->name
, slave
->bond
->dev
->name
);
2343 switch (lacpdu
->subtype
) {
2344 case AD_TYPE_LACPDU
:
2345 ret
= RX_HANDLER_CONSUMED
;
2346 netdev_dbg(slave
->bond
->dev
,
2347 "Received LACPDU on port %d slave %s\n",
2348 port
->actor_port_number
,
2350 /* Protect against concurrent state machines */
2351 spin_lock(&slave
->bond
->mode_lock
);
2352 ad_rx_machine(lacpdu
, port
);
2353 spin_unlock(&slave
->bond
->mode_lock
);
2356 case AD_TYPE_MARKER
:
2357 ret
= RX_HANDLER_CONSUMED
;
2358 /* No need to convert fields to Little Endian since we
2359 * don't use the marker's fields.
2362 switch (((struct bond_marker
*)lacpdu
)->tlv_type
) {
2363 case AD_MARKER_INFORMATION_SUBTYPE
:
2364 netdev_dbg(slave
->bond
->dev
, "Received Marker Information on port %d\n",
2365 port
->actor_port_number
);
2366 ad_marker_info_received((struct bond_marker
*)lacpdu
, port
);
2369 case AD_MARKER_RESPONSE_SUBTYPE
:
2370 netdev_dbg(slave
->bond
->dev
, "Received Marker Response on port %d\n",
2371 port
->actor_port_number
);
2372 ad_marker_response_received((struct bond_marker
*)lacpdu
, port
);
2376 netdev_dbg(slave
->bond
->dev
, "Received an unknown Marker subtype on slot %d\n",
2377 port
->actor_port_number
);
2385 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2386 * its current speed and duplex settings.
2388 * @port: the port we'are looking at
2389 * @reset: Boolean to just reset the speed and the duplex part of the key
2391 * The logic to change the oper / admin keys is:
2392 * (a) A full duplex port can participate in LACP with partner.
2393 * (b) When the speed is changed, LACP need to be reinitiated.
2395 static void ad_update_actor_keys(struct port
*port
, bool reset
)
2398 u16 ospeed
= 0, speed
= 0;
2399 u16 old_oper_key
= port
->actor_oper_port_key
;
2401 port
->actor_admin_port_key
&= ~(AD_SPEED_KEY_MASKS
|AD_DUPLEX_KEY_MASKS
);
2403 speed
= __get_link_speed(port
);
2404 ospeed
= (old_oper_key
& AD_SPEED_KEY_MASKS
) >> 1;
2405 duplex
= __get_duplex(port
);
2406 port
->actor_admin_port_key
|= (speed
<< 1) | duplex
;
2408 port
->actor_oper_port_key
= port
->actor_admin_port_key
;
2410 if (old_oper_key
!= port
->actor_oper_port_key
) {
2411 /* Only 'duplex' port participates in LACP */
2413 port
->sm_vars
|= AD_PORT_LACP_ENABLED
;
2415 port
->sm_vars
&= ~AD_PORT_LACP_ENABLED
;
2419 netdev_err(port
->slave
->dev
,
2420 "speed changed to 0 for port %s",
2421 port
->slave
->dev
->name
);
2422 } else if (duplex
&& ospeed
!= speed
) {
2423 /* Speed change restarts LACP state-machine */
2424 port
->sm_vars
|= AD_PORT_BEGIN
;
2431 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2434 * @slave: slave struct to work on
2436 * Handle reselection of aggregator (if needed) for this port.
2438 void bond_3ad_adapter_speed_duplex_changed(struct slave
*slave
)
2442 port
= &(SLAVE_AD_INFO(slave
)->port
);
2444 /* if slave is null, the whole port is not initialized */
2446 netdev_warn(slave
->bond
->dev
,
2447 "speed/duplex changed for uninitialized port %s\n",
2452 spin_lock_bh(&slave
->bond
->mode_lock
);
2453 ad_update_actor_keys(port
, false);
2454 netdev_dbg(slave
->bond
->dev
, "Port %d slave %s changed speed/duplex\n",
2455 port
->actor_port_number
, slave
->dev
->name
);
2456 spin_unlock_bh(&slave
->bond
->mode_lock
);
2460 * bond_3ad_handle_link_change - handle a slave's link status change indication
2461 * @slave: slave struct to work on
2462 * @status: whether the link is now up or down
2464 * Handle reselection of aggregator (if needed) for this port.
2466 void bond_3ad_handle_link_change(struct slave
*slave
, char link
)
2468 struct aggregator
*agg
;
2472 port
= &(SLAVE_AD_INFO(slave
)->port
);
2474 /* if slave is null, the whole port is not initialized */
2476 netdev_warn(slave
->bond
->dev
, "link status changed for uninitialized port on %s\n",
2481 spin_lock_bh(&slave
->bond
->mode_lock
);
2482 /* on link down we are zeroing duplex and speed since
2483 * some of the adaptors(ce1000.lan) report full duplex/speed
2484 * instead of N/A(duplex) / 0(speed).
2486 * on link up we are forcing recheck on the duplex and speed since
2487 * some of he adaptors(ce1000.lan) report.
2489 if (link
== BOND_LINK_UP
) {
2490 port
->is_enabled
= true;
2491 ad_update_actor_keys(port
, false);
2493 /* link has failed */
2494 port
->is_enabled
= false;
2495 ad_update_actor_keys(port
, true);
2497 agg
= __get_first_agg(port
);
2498 ad_agg_selection_logic(agg
, &dummy
);
2500 netdev_dbg(slave
->bond
->dev
, "Port %d changed link status to %s\n",
2501 port
->actor_port_number
,
2502 link
== BOND_LINK_UP
? "UP" : "DOWN");
2504 spin_unlock_bh(&slave
->bond
->mode_lock
);
2506 /* RTNL is held and mode_lock is released so it's safe
2507 * to update slave_array here.
2509 bond_update_slave_arr(slave
->bond
, NULL
);
2513 * bond_3ad_set_carrier - set link state for bonding master
2514 * @bond - bonding structure
2516 * if we have an active aggregator, we're up, if not, we're down.
2517 * Presumes that we cannot have an active aggregator if there are
2518 * no slaves with link up.
2520 * This behavior complies with IEEE 802.3 section 43.3.9.
2522 * Called by bond_set_carrier(). Return zero if carrier state does not
2523 * change, nonzero if it does.
2525 int bond_3ad_set_carrier(struct bonding
*bond
)
2527 struct aggregator
*active
;
2528 struct slave
*first_slave
;
2532 first_slave
= bond_first_slave_rcu(bond
);
2537 active
= __get_active_agg(&(SLAVE_AD_INFO(first_slave
)->aggregator
));
2539 /* are enough slaves available to consider link up? */
2540 if (__agg_active_ports(active
) < bond
->params
.min_links
) {
2541 if (netif_carrier_ok(bond
->dev
)) {
2542 netif_carrier_off(bond
->dev
);
2545 } else if (!netif_carrier_ok(bond
->dev
)) {
2546 netif_carrier_on(bond
->dev
);
2549 } else if (netif_carrier_ok(bond
->dev
)) {
2550 netif_carrier_off(bond
->dev
);
2558 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2559 * @bond: bonding struct to work on
2560 * @ad_info: ad_info struct to fill with the bond's info
2562 * Returns: 0 on success
2565 int __bond_3ad_get_active_agg_info(struct bonding
*bond
,
2566 struct ad_info
*ad_info
)
2568 struct aggregator
*aggregator
= NULL
;
2569 struct list_head
*iter
;
2570 struct slave
*slave
;
2573 bond_for_each_slave_rcu(bond
, slave
, iter
) {
2574 port
= &(SLAVE_AD_INFO(slave
)->port
);
2575 if (port
->aggregator
&& port
->aggregator
->is_active
) {
2576 aggregator
= port
->aggregator
;
2584 ad_info
->aggregator_id
= aggregator
->aggregator_identifier
;
2585 ad_info
->ports
= aggregator
->num_of_ports
;
2586 ad_info
->actor_key
= aggregator
->actor_oper_aggregator_key
;
2587 ad_info
->partner_key
= aggregator
->partner_oper_aggregator_key
;
2588 ether_addr_copy(ad_info
->partner_system
,
2589 aggregator
->partner_system
.mac_addr_value
);
2593 int bond_3ad_get_active_agg_info(struct bonding
*bond
, struct ad_info
*ad_info
)
2598 ret
= __bond_3ad_get_active_agg_info(bond
, ad_info
);
2604 int bond_3ad_lacpdu_recv(const struct sk_buff
*skb
, struct bonding
*bond
,
2605 struct slave
*slave
)
2607 struct lacpdu
*lacpdu
, _lacpdu
;
2609 if (skb
->protocol
!= PKT_TYPE_LACPDU
)
2610 return RX_HANDLER_ANOTHER
;
2612 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb
)->h_dest
, lacpdu_mcast_addr
))
2613 return RX_HANDLER_ANOTHER
;
2615 lacpdu
= skb_header_pointer(skb
, 0, sizeof(_lacpdu
), &_lacpdu
);
2617 return RX_HANDLER_ANOTHER
;
2619 return bond_3ad_rx_indication(lacpdu
, slave
, skb
->len
);
2623 * bond_3ad_update_lacp_rate - change the lacp rate
2624 * @bond - bonding struct
2626 * When modify lacp_rate parameter via sysfs,
2627 * update actor_oper_port_state of each port.
2629 * Hold bond->mode_lock,
2630 * so we can modify port->actor_oper_port_state,
2631 * no matter bond is up or down.
2633 void bond_3ad_update_lacp_rate(struct bonding
*bond
)
2635 struct port
*port
= NULL
;
2636 struct list_head
*iter
;
2637 struct slave
*slave
;
2640 lacp_fast
= bond
->params
.lacp_fast
;
2641 spin_lock_bh(&bond
->mode_lock
);
2642 bond_for_each_slave(bond
, slave
, iter
) {
2643 port
= &(SLAVE_AD_INFO(slave
)->port
);
2645 port
->actor_oper_port_state
|= AD_STATE_LACP_TIMEOUT
;
2647 port
->actor_oper_port_state
&= ~AD_STATE_LACP_TIMEOUT
;
2649 spin_unlock_bh(&bond
->mode_lock
);