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 //#define BONDING_DEBUG 1
25 #include <linux/skbuff.h>
26 #include <linux/if_ether.h>
27 #include <linux/netdevice.h>
28 #include <linux/spinlock.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_bonding.h>
31 #include <linux/pkt_sched.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 802.3ad standard)
61 #define AD_PORT_BEGIN 0x1
62 #define AD_PORT_LACP_ENABLED 0x2
63 #define AD_PORT_ACTOR_CHURN 0x4
64 #define AD_PORT_PARTNER_CHURN 0x8
65 #define AD_PORT_READY 0x10
66 #define AD_PORT_READY_N 0x20
67 #define AD_PORT_MATCHED 0x40
68 #define AD_PORT_STANDBY 0x80
69 #define AD_PORT_SELECTED 0x100
70 #define AD_PORT_MOVED 0x200
72 // Port Key definitions
73 // key is determined according to the link speed, duplex and
74 // user key(which is yet not supported)
75 // ------------------------------------------------------------
76 // Port key : | User key | Speed |Duplex|
77 // ------------------------------------------------------------
79 #define AD_DUPLEX_KEY_BITS 0x1
80 #define AD_SPEED_KEY_BITS 0x3E
81 #define AD_USER_KEY_BITS 0xFFC0
84 #define AD_LINK_SPEED_BITMASK_1MBPS 0x1
85 #define AD_LINK_SPEED_BITMASK_10MBPS 0x2
86 #define AD_LINK_SPEED_BITMASK_100MBPS 0x4
87 #define AD_LINK_SPEED_BITMASK_1000MBPS 0x8
90 // compare MAC addresses
91 #define MAC_ADDRESS_COMPARE(A, B) memcmp(A, B, ETH_ALEN)
93 static struct mac_addr null_mac_addr
= {{0, 0, 0, 0, 0, 0}};
94 static u16 ad_ticks_per_sec
;
95 static const int ad_delta_in_ticks
= (AD_TIMER_INTERVAL
* HZ
) / 1000;
97 // ================= 3AD api to bonding and kernel code ==================
98 static u16
__get_link_speed(struct port
*port
);
99 static u8
__get_duplex(struct port
*port
);
100 static inline void __initialize_port_locks(struct port
*port
);
102 static void __ntohs_lacpdu(struct lacpdu
*lacpdu
);
103 static u16
__ad_timer_to_ticks(u16 timer_type
, u16 Par
);
106 // ================= ad code helper functions ==================
107 //needed by ad_rx_machine(...)
108 static void __record_pdu(struct lacpdu
*lacpdu
, struct port
*port
);
109 static void __record_default(struct port
*port
);
110 static void __update_selected(struct lacpdu
*lacpdu
, struct port
*port
);
111 static void __update_default_selected(struct port
*port
);
112 static void __choose_matched(struct lacpdu
*lacpdu
, struct port
*port
);
113 static void __update_ntt(struct lacpdu
*lacpdu
, struct port
*port
);
115 //needed for ad_mux_machine(..)
116 static void __attach_bond_to_agg(struct port
*port
);
117 static void __detach_bond_from_agg(struct port
*port
);
118 static int __agg_ports_are_ready(struct aggregator
*aggregator
);
119 static void __set_agg_ports_ready(struct aggregator
*aggregator
, int val
);
121 //needed for ad_agg_selection_logic(...)
122 static u32
__get_agg_bandwidth(struct aggregator
*aggregator
);
123 static struct aggregator
*__get_active_agg(struct aggregator
*aggregator
);
126 // ================= main 802.3ad protocol functions ==================
127 static int ad_lacpdu_send(struct port
*port
);
128 static int ad_marker_send(struct port
*port
, struct marker
*marker
);
129 static void ad_mux_machine(struct port
*port
);
130 static void ad_rx_machine(struct lacpdu
*lacpdu
, struct port
*port
);
131 static void ad_tx_machine(struct port
*port
);
132 static void ad_periodic_machine(struct port
*port
);
133 static void ad_port_selection_logic(struct port
*port
);
134 static void ad_agg_selection_logic(struct aggregator
*aggregator
);
135 static void ad_clear_agg(struct aggregator
*aggregator
);
136 static void ad_initialize_agg(struct aggregator
*aggregator
);
137 static void ad_initialize_port(struct port
*port
, int lacp_fast
);
138 static void ad_initialize_lacpdu(struct lacpdu
*Lacpdu
);
139 static void ad_enable_collecting_distributing(struct port
*port
);
140 static void ad_disable_collecting_distributing(struct port
*port
);
141 static void ad_marker_info_received(struct marker
*marker_info
, struct port
*port
);
142 static void ad_marker_response_received(struct marker
*marker
, struct port
*port
);
145 /////////////////////////////////////////////////////////////////////////////////
146 // ================= api to bonding and kernel code ==================
147 /////////////////////////////////////////////////////////////////////////////////
150 * __get_bond_by_port - get the port's bonding struct
151 * @port: the port we're looking at
153 * Return @port's bonding struct, or %NULL if it can't be found.
155 static inline struct bonding
*__get_bond_by_port(struct port
*port
)
157 if (port
->slave
== NULL
) {
161 return bond_get_bond_by_slave(port
->slave
);
165 * __get_first_port - get the first port in the bond
166 * @bond: the bond we're looking at
168 * Return the port of the first slave in @bond, or %NULL if it can't be found.
170 static inline struct port
*__get_first_port(struct bonding
*bond
)
172 if (bond
->slave_cnt
== 0) {
176 return &(SLAVE_AD_INFO(bond
->first_slave
).port
);
180 * __get_next_port - get the next port in the bond
181 * @port: the port we're looking at
183 * Return the port of the slave that is next in line of @port's slave in the
184 * bond, or %NULL if it can't be found.
186 static inline struct port
*__get_next_port(struct port
*port
)
188 struct bonding
*bond
= __get_bond_by_port(port
);
189 struct slave
*slave
= port
->slave
;
191 // If there's no bond for this port, or this is the last slave
192 if ((bond
== NULL
) || (slave
->next
== bond
->first_slave
)) {
196 return &(SLAVE_AD_INFO(slave
->next
).port
);
200 * __get_first_agg - get the first aggregator in the bond
201 * @bond: the bond we're looking at
203 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
206 static inline struct aggregator
*__get_first_agg(struct port
*port
)
208 struct bonding
*bond
= __get_bond_by_port(port
);
210 // If there's no bond for this port, or bond has no slaves
211 if ((bond
== NULL
) || (bond
->slave_cnt
== 0)) {
215 return &(SLAVE_AD_INFO(bond
->first_slave
).aggregator
);
219 * __get_next_agg - get the next aggregator in the bond
220 * @aggregator: the aggregator we're looking at
222 * Return the aggregator of the slave that is next in line of @aggregator's
223 * slave in the bond, or %NULL if it can't be found.
225 static inline struct aggregator
*__get_next_agg(struct aggregator
*aggregator
)
227 struct slave
*slave
= aggregator
->slave
;
228 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
230 // If there's no bond for this aggregator, or this is the last slave
231 if ((bond
== NULL
) || (slave
->next
== bond
->first_slave
)) {
235 return &(SLAVE_AD_INFO(slave
->next
).aggregator
);
239 * __disable_port - disable the port's slave
240 * @port: the port we're looking at
243 static inline void __disable_port(struct port
*port
)
245 bond_set_slave_inactive_flags(port
->slave
);
249 * __enable_port - enable the port's slave, if it's up
250 * @port: the port we're looking at
253 static inline void __enable_port(struct port
*port
)
255 struct slave
*slave
= port
->slave
;
257 if ((slave
->link
== BOND_LINK_UP
) && IS_UP(slave
->dev
)) {
258 bond_set_slave_active_flags(slave
);
263 * __port_is_enabled - check if the port's slave is in active state
264 * @port: the port we're looking at
267 static inline int __port_is_enabled(struct port
*port
)
269 return(port
->slave
->state
== BOND_STATE_ACTIVE
);
273 * __get_agg_selection_mode - get the aggregator selection mode
274 * @port: the port we're looking at
276 * Get the aggregator selection mode. Can be %BANDWIDTH or %COUNT.
278 static inline u32
__get_agg_selection_mode(struct port
*port
)
280 struct bonding
*bond
= __get_bond_by_port(port
);
286 return BOND_AD_INFO(bond
).agg_select_mode
;
290 * __check_agg_selection_timer - check if the selection timer has expired
291 * @port: the port we're looking at
294 static inline int __check_agg_selection_timer(struct port
*port
)
296 struct bonding
*bond
= __get_bond_by_port(port
);
302 return BOND_AD_INFO(bond
).agg_select_timer
? 1 : 0;
306 * __get_rx_machine_lock - lock the port's RX machine
307 * @port: the port we're looking at
310 static inline void __get_rx_machine_lock(struct port
*port
)
312 spin_lock(&(SLAVE_AD_INFO(port
->slave
).rx_machine_lock
));
316 * __release_rx_machine_lock - unlock the port's RX machine
317 * @port: the port we're looking at
320 static inline void __release_rx_machine_lock(struct port
*port
)
322 spin_unlock(&(SLAVE_AD_INFO(port
->slave
).rx_machine_lock
));
326 * __get_link_speed - get a port's speed
327 * @port: the port we're looking at
329 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
331 * %AD_LINK_SPEED_BITMASK_10MBPS,
332 * %AD_LINK_SPEED_BITMASK_100MBPS,
333 * %AD_LINK_SPEED_BITMASK_1000MBPS
335 static u16
__get_link_speed(struct port
*port
)
337 struct slave
*slave
= port
->slave
;
340 /* this if covers only a special case: when the configuration starts with
341 * link down, it sets the speed to 0.
342 * This is done in spite of the fact that the e100 driver reports 0 to be
343 * compatible with MVT in the future.*/
344 if (slave
->link
!= BOND_LINK_UP
) {
347 switch (slave
->speed
) {
349 speed
= AD_LINK_SPEED_BITMASK_10MBPS
;
353 speed
= AD_LINK_SPEED_BITMASK_100MBPS
;
357 speed
= AD_LINK_SPEED_BITMASK_1000MBPS
;
361 speed
= 0; // unknown speed value from ethtool. shouldn't happen
366 dprintk("Port %d Received link speed %d update from adapter\n", port
->actor_port_number
, speed
);
371 * __get_duplex - get a port's duplex
372 * @port: the port we're looking at
374 * Return @port's duplex in 802.3ad bitmask format. i.e.:
375 * 0x01 if in full duplex
378 static u8
__get_duplex(struct port
*port
)
380 struct slave
*slave
= port
->slave
;
384 // handling a special case: when the configuration starts with
385 // link down, it sets the duplex to 0.
386 if (slave
->link
!= BOND_LINK_UP
) {
389 switch (slave
->duplex
) {
392 dprintk("Port %d Received status full duplex update from adapter\n", port
->actor_port_number
);
397 dprintk("Port %d Received status NOT full duplex update from adapter\n", port
->actor_port_number
);
405 * __initialize_port_locks - initialize a port's RX machine spinlock
406 * @port: the port we're looking at
409 static inline void __initialize_port_locks(struct port
*port
)
411 // make sure it isn't called twice
412 spin_lock_init(&(SLAVE_AD_INFO(port
->slave
).rx_machine_lock
));
417 * __ntohs_lacpdu - convert the contents of a LACPDU to host byte order
418 * @lacpdu: the speicifed lacpdu
420 * For each multi-byte field in the lacpdu, convert its content
422 static void __ntohs_lacpdu(struct lacpdu
*lacpdu
)
425 lacpdu
->actor_system_priority
= ntohs(lacpdu
->actor_system_priority
);
426 lacpdu
->actor_key
= ntohs(lacpdu
->actor_key
);
427 lacpdu
->actor_port_priority
= ntohs(lacpdu
->actor_port_priority
);
428 lacpdu
->actor_port
= ntohs(lacpdu
->actor_port
);
429 lacpdu
->partner_system_priority
= ntohs(lacpdu
->partner_system_priority
);
430 lacpdu
->partner_key
= ntohs(lacpdu
->partner_key
);
431 lacpdu
->partner_port_priority
= ntohs(lacpdu
->partner_port_priority
);
432 lacpdu
->partner_port
= ntohs(lacpdu
->partner_port
);
433 lacpdu
->collector_max_delay
= ntohs(lacpdu
->collector_max_delay
);
438 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
439 * @timer_type: which timer to operate
440 * @par: timer parameter. see below
442 * If @timer_type is %current_while_timer, @par indicates long/short timer.
443 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
444 * %SLOW_PERIODIC_TIME.
446 static u16
__ad_timer_to_ticks(u16 timer_type
, u16 par
)
448 u16 retval
=0; //to silence the compiler
450 switch (timer_type
) {
451 case AD_CURRENT_WHILE_TIMER
: // for rx machine usage
452 if (par
) { // for short or long timeout
453 retval
= (AD_SHORT_TIMEOUT_TIME
*ad_ticks_per_sec
); // short timeout
455 retval
= (AD_LONG_TIMEOUT_TIME
*ad_ticks_per_sec
); // long timeout
458 case AD_ACTOR_CHURN_TIMER
: // for local churn machine
459 retval
= (AD_CHURN_DETECTION_TIME
*ad_ticks_per_sec
);
461 case AD_PERIODIC_TIMER
: // for periodic machine
462 retval
= (par
*ad_ticks_per_sec
); // long timeout
464 case AD_PARTNER_CHURN_TIMER
: // for remote churn machine
465 retval
= (AD_CHURN_DETECTION_TIME
*ad_ticks_per_sec
);
467 case AD_WAIT_WHILE_TIMER
: // for selection machine
468 retval
= (AD_AGGREGATE_WAIT_TIME
*ad_ticks_per_sec
);
475 /////////////////////////////////////////////////////////////////////////////////
476 // ================= ad_rx_machine helper functions ==================
477 /////////////////////////////////////////////////////////////////////////////////
480 * __record_pdu - record parameters from a received lacpdu
481 * @lacpdu: the lacpdu we've received
482 * @port: the port we're looking at
484 * Record the parameter values for the Actor carried in a received lacpdu as
485 * the current partner operational parameter values and sets
486 * actor_oper_port_state.defaulted to FALSE.
488 static void __record_pdu(struct lacpdu
*lacpdu
, struct port
*port
)
490 // validate lacpdu and port
491 if (lacpdu
&& port
) {
492 // record the new parameter values for the partner operational
493 port
->partner_oper_port_number
= lacpdu
->actor_port
;
494 port
->partner_oper_port_priority
= lacpdu
->actor_port_priority
;
495 port
->partner_oper_system
= lacpdu
->actor_system
;
496 port
->partner_oper_system_priority
= lacpdu
->actor_system_priority
;
497 port
->partner_oper_key
= lacpdu
->actor_key
;
498 // zero partener's lase states
499 port
->partner_oper_port_state
= 0;
500 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_LACP_ACTIVITY
);
501 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_LACP_TIMEOUT
);
502 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_AGGREGATION
);
503 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_SYNCHRONIZATION
);
504 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_COLLECTING
);
505 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_DISTRIBUTING
);
506 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_DEFAULTED
);
507 port
->partner_oper_port_state
|= (lacpdu
->actor_state
& AD_STATE_EXPIRED
);
509 // set actor_oper_port_state.defaulted to FALSE
510 port
->actor_oper_port_state
&= ~AD_STATE_DEFAULTED
;
512 // set the partner sync. to on if the partner is sync. and the port is matched
513 if ((port
->sm_vars
& AD_PORT_MATCHED
) && (lacpdu
->actor_state
& AD_STATE_SYNCHRONIZATION
)) {
514 port
->partner_oper_port_state
|= AD_STATE_SYNCHRONIZATION
;
516 port
->partner_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
522 * __record_default - record default parameters
523 * @port: the port we're looking at
525 * This function records the default parameter values for the partner carried
526 * in the Partner Admin parameters as the current partner operational parameter
527 * values and sets actor_oper_port_state.defaulted to TRUE.
529 static void __record_default(struct port
*port
)
533 // record the partner admin parameters
534 port
->partner_oper_port_number
= port
->partner_admin_port_number
;
535 port
->partner_oper_port_priority
= port
->partner_admin_port_priority
;
536 port
->partner_oper_system
= port
->partner_admin_system
;
537 port
->partner_oper_system_priority
= port
->partner_admin_system_priority
;
538 port
->partner_oper_key
= port
->partner_admin_key
;
539 port
->partner_oper_port_state
= port
->partner_admin_port_state
;
541 // set actor_oper_port_state.defaulted to true
542 port
->actor_oper_port_state
|= AD_STATE_DEFAULTED
;
547 * __update_selected - update a port's Selected variable from a received lacpdu
548 * @lacpdu: the lacpdu we've received
549 * @port: the port we're looking at
551 * Update the value of the selected variable, using parameter values from a
552 * newly received lacpdu. The parameter values for the Actor carried in the
553 * received PDU are compared with the corresponding operational parameter
554 * values for the ports partner. If one or more of the comparisons shows that
555 * the value(s) received in the PDU differ from the current operational values,
556 * then selected is set to FALSE and actor_oper_port_state.synchronization is
557 * set to out_of_sync. Otherwise, selected remains unchanged.
559 static void __update_selected(struct lacpdu
*lacpdu
, struct port
*port
)
561 // validate lacpdu and port
562 if (lacpdu
&& port
) {
563 // check if any parameter is different
564 if ((lacpdu
->actor_port
!= port
->partner_oper_port_number
) ||
565 (lacpdu
->actor_port_priority
!= port
->partner_oper_port_priority
) ||
566 MAC_ADDRESS_COMPARE(&(lacpdu
->actor_system
), &(port
->partner_oper_system
)) ||
567 (lacpdu
->actor_system_priority
!= port
->partner_oper_system_priority
) ||
568 (lacpdu
->actor_key
!= port
->partner_oper_key
) ||
569 ((lacpdu
->actor_state
& AD_STATE_AGGREGATION
) != (port
->partner_oper_port_state
& AD_STATE_AGGREGATION
))
571 // update the state machine Selected variable
572 port
->sm_vars
&= ~AD_PORT_SELECTED
;
578 * __update_default_selected - update a port's Selected variable from Partner
579 * @port: the port we're looking at
581 * This function updates the value of the selected variable, using the partner
582 * administrative parameter values. The administrative values are compared with
583 * the corresponding operational parameter values for the partner. If one or
584 * more of the comparisons shows that the administrative value(s) differ from
585 * the current operational values, then Selected is set to FALSE and
586 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
587 * Selected remains unchanged.
589 static void __update_default_selected(struct port
*port
)
593 // check if any parameter is different
594 if ((port
->partner_admin_port_number
!= port
->partner_oper_port_number
) ||
595 (port
->partner_admin_port_priority
!= port
->partner_oper_port_priority
) ||
596 MAC_ADDRESS_COMPARE(&(port
->partner_admin_system
), &(port
->partner_oper_system
)) ||
597 (port
->partner_admin_system_priority
!= port
->partner_oper_system_priority
) ||
598 (port
->partner_admin_key
!= port
->partner_oper_key
) ||
599 ((port
->partner_admin_port_state
& AD_STATE_AGGREGATION
) != (port
->partner_oper_port_state
& AD_STATE_AGGREGATION
))
601 // update the state machine Selected variable
602 port
->sm_vars
&= ~AD_PORT_SELECTED
;
608 * __choose_matched - update a port's matched variable from a received lacpdu
609 * @lacpdu: the lacpdu we've received
610 * @port: the port we're looking at
612 * Update the value of the matched variable, using parameter values from a
613 * newly received lacpdu. Parameter values for the partner carried in the
614 * received PDU are compared with the corresponding operational parameter
615 * values for the actor. Matched is set to TRUE if all of these parameters
616 * match and the PDU parameter partner_state.aggregation has the same value as
617 * actor_oper_port_state.aggregation and lacp will actively maintain the link
618 * in the aggregation. Matched is also set to TRUE if the value of
619 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
620 * an individual link and lacp will actively maintain the link. Otherwise,
621 * matched is set to FALSE. LACP is considered to be actively maintaining the
622 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
623 * the actor's actor_oper_port_state.lacp_activity and the PDU's
624 * partner_state.lacp_activity variables are TRUE.
626 static void __choose_matched(struct lacpdu
*lacpdu
, struct port
*port
)
628 // validate lacpdu and port
629 if (lacpdu
&& port
) {
630 // check if all parameters are alike
631 if (((lacpdu
->partner_port
== port
->actor_port_number
) &&
632 (lacpdu
->partner_port_priority
== port
->actor_port_priority
) &&
633 !MAC_ADDRESS_COMPARE(&(lacpdu
->partner_system
), &(port
->actor_system
)) &&
634 (lacpdu
->partner_system_priority
== port
->actor_system_priority
) &&
635 (lacpdu
->partner_key
== port
->actor_oper_port_key
) &&
636 ((lacpdu
->partner_state
& AD_STATE_AGGREGATION
) == (port
->actor_oper_port_state
& AD_STATE_AGGREGATION
))) ||
637 // or this is individual link(aggregation == FALSE)
638 ((lacpdu
->actor_state
& AD_STATE_AGGREGATION
) == 0)
640 // update the state machine Matched variable
641 port
->sm_vars
|= AD_PORT_MATCHED
;
643 port
->sm_vars
&= ~AD_PORT_MATCHED
;
649 * __update_ntt - update a port's ntt variable from a received lacpdu
650 * @lacpdu: the lacpdu we've received
651 * @port: the port we're looking at
653 * Updates the value of the ntt variable, using parameter values from a newly
654 * received lacpdu. The parameter values for the partner carried in the
655 * received PDU are compared with the corresponding operational parameter
656 * values for the Actor. If one or more of the comparisons shows that the
657 * value(s) received in the PDU differ from the current operational values,
658 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
660 static void __update_ntt(struct lacpdu
*lacpdu
, struct port
*port
)
662 // validate lacpdu and port
663 if (lacpdu
&& port
) {
664 // check if any parameter is different
665 if ((lacpdu
->partner_port
!= port
->actor_port_number
) ||
666 (lacpdu
->partner_port_priority
!= port
->actor_port_priority
) ||
667 MAC_ADDRESS_COMPARE(&(lacpdu
->partner_system
), &(port
->actor_system
)) ||
668 (lacpdu
->partner_system_priority
!= port
->actor_system_priority
) ||
669 (lacpdu
->partner_key
!= port
->actor_oper_port_key
) ||
670 ((lacpdu
->partner_state
& AD_STATE_LACP_ACTIVITY
) != (port
->actor_oper_port_state
& AD_STATE_LACP_ACTIVITY
)) ||
671 ((lacpdu
->partner_state
& AD_STATE_LACP_TIMEOUT
) != (port
->actor_oper_port_state
& AD_STATE_LACP_TIMEOUT
)) ||
672 ((lacpdu
->partner_state
& AD_STATE_SYNCHRONIZATION
) != (port
->actor_oper_port_state
& AD_STATE_SYNCHRONIZATION
)) ||
673 ((lacpdu
->partner_state
& AD_STATE_AGGREGATION
) != (port
->actor_oper_port_state
& AD_STATE_AGGREGATION
))
675 // set ntt to be TRUE
682 * __attach_bond_to_agg
683 * @port: the port we're looking at
685 * Handle the attaching of the port's control parser/multiplexer and the
686 * aggregator. This function does nothing since the parser/multiplexer of the
687 * receive and the parser/multiplexer of the aggregator are already combined.
689 static void __attach_bond_to_agg(struct port
*port
)
691 port
=NULL
; // just to satisfy the compiler
692 // This function does nothing since the parser/multiplexer of the receive
693 // and the parser/multiplexer of the aggregator are already combined
697 * __detach_bond_from_agg
698 * @port: the port we're looking at
700 * Handle the detaching of the port's control parser/multiplexer from the
701 * aggregator. This function does nothing since the parser/multiplexer of the
702 * receive and the parser/multiplexer of the aggregator are already combined.
704 static void __detach_bond_from_agg(struct port
*port
)
706 port
=NULL
; // just to satisfy the compiler
707 // This function does nothing sience the parser/multiplexer of the receive
708 // and the parser/multiplexer of the aggregator are already combined
712 * __agg_ports_are_ready - check if all ports in an aggregator are ready
713 * @aggregator: the aggregator we're looking at
716 static int __agg_ports_are_ready(struct aggregator
*aggregator
)
722 // scan all ports in this aggregator to verfy if they are all ready
723 for (port
=aggregator
->lag_ports
; port
; port
=port
->next_port_in_aggregator
) {
724 if (!(port
->sm_vars
& AD_PORT_READY_N
)) {
735 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
736 * @aggregator: the aggregator we're looking at
737 * @val: Should the ports' ready bit be set on or off
740 static void __set_agg_ports_ready(struct aggregator
*aggregator
, int val
)
744 for (port
=aggregator
->lag_ports
; port
; port
=port
->next_port_in_aggregator
) {
746 port
->sm_vars
|= AD_PORT_READY
;
748 port
->sm_vars
&= ~AD_PORT_READY
;
754 * __get_agg_bandwidth - get the total bandwidth of an aggregator
755 * @aggregator: the aggregator we're looking at
758 static u32
__get_agg_bandwidth(struct aggregator
*aggregator
)
763 if (aggregator
->num_of_ports
) {
764 basic_speed
= __get_link_speed(aggregator
->lag_ports
);
765 switch (basic_speed
) {
766 case AD_LINK_SPEED_BITMASK_1MBPS
:
767 bandwidth
= aggregator
->num_of_ports
;
769 case AD_LINK_SPEED_BITMASK_10MBPS
:
770 bandwidth
= aggregator
->num_of_ports
* 10;
772 case AD_LINK_SPEED_BITMASK_100MBPS
:
773 bandwidth
= aggregator
->num_of_ports
* 100;
775 case AD_LINK_SPEED_BITMASK_1000MBPS
:
776 bandwidth
= aggregator
->num_of_ports
* 1000;
779 bandwidth
=0; // to silent the compilor ....
786 * __get_active_agg - get the current active aggregator
787 * @aggregator: the aggregator we're looking at
790 static struct aggregator
*__get_active_agg(struct aggregator
*aggregator
)
792 struct aggregator
*retval
= NULL
;
794 for (; aggregator
; aggregator
= __get_next_agg(aggregator
)) {
795 if (aggregator
->is_active
) {
805 * __update_lacpdu_from_port - update a port's lacpdu fields
806 * @port: the port we're looking at
809 static inline void __update_lacpdu_from_port(struct port
*port
)
811 struct lacpdu
*lacpdu
= &port
->lacpdu
;
813 /* update current actual Actor parameters */
814 /* lacpdu->subtype initialized
815 * lacpdu->version_number initialized
816 * lacpdu->tlv_type_actor_info initialized
817 * lacpdu->actor_information_length initialized
820 lacpdu
->actor_system_priority
= port
->actor_system_priority
;
821 lacpdu
->actor_system
= port
->actor_system
;
822 lacpdu
->actor_key
= port
->actor_oper_port_key
;
823 lacpdu
->actor_port_priority
= port
->actor_port_priority
;
824 lacpdu
->actor_port
= port
->actor_port_number
;
825 lacpdu
->actor_state
= port
->actor_oper_port_state
;
827 /* lacpdu->reserved_3_1 initialized
828 * lacpdu->tlv_type_partner_info initialized
829 * lacpdu->partner_information_length initialized
832 lacpdu
->partner_system_priority
= port
->partner_oper_system_priority
;
833 lacpdu
->partner_system
= port
->partner_oper_system
;
834 lacpdu
->partner_key
= port
->partner_oper_key
;
835 lacpdu
->partner_port_priority
= port
->partner_oper_port_priority
;
836 lacpdu
->partner_port
= port
->partner_oper_port_number
;
837 lacpdu
->partner_state
= port
->partner_oper_port_state
;
839 /* lacpdu->reserved_3_2 initialized
840 * lacpdu->tlv_type_collector_info initialized
841 * lacpdu->collector_information_length initialized
842 * collector_max_delay initialized
843 * reserved_12[12] initialized
844 * tlv_type_terminator initialized
845 * terminator_length initialized
846 * reserved_50[50] initialized
849 /* Convert all non u8 parameters to Big Endian for transmit */
850 __ntohs_lacpdu(lacpdu
);
853 //////////////////////////////////////////////////////////////////////////////////////
854 // ================= main 802.3ad protocol code ======================================
855 //////////////////////////////////////////////////////////////////////////////////////
858 * ad_lacpdu_send - send out a lacpdu packet on a given port
859 * @port: the port we're looking at
861 * Returns: 0 on success
864 static int ad_lacpdu_send(struct port
*port
)
866 struct slave
*slave
= port
->slave
;
868 struct lacpdu_header
*lacpdu_header
;
869 int length
= sizeof(struct lacpdu_header
);
870 struct mac_addr lacpdu_multicast_address
= AD_MULTICAST_LACPDU_ADDR
;
872 skb
= dev_alloc_skb(length
);
877 skb
->dev
= slave
->dev
;
878 skb
->mac
.raw
= skb
->data
;
879 skb
->nh
.raw
= skb
->data
+ ETH_HLEN
;
880 skb
->protocol
= PKT_TYPE_LACPDU
;
881 skb
->priority
= TC_PRIO_CONTROL
;
883 lacpdu_header
= (struct lacpdu_header
*)skb_put(skb
, length
);
885 lacpdu_header
->ad_header
.destination_address
= lacpdu_multicast_address
;
886 /* Note: source addres is set to be the member's PERMANENT address, because we use it
887 to identify loopback lacpdus in receive. */
888 lacpdu_header
->ad_header
.source_address
= *((struct mac_addr
*)(slave
->perm_hwaddr
));
889 lacpdu_header
->ad_header
.length_type
= PKT_TYPE_LACPDU
;
891 lacpdu_header
->lacpdu
= port
->lacpdu
; // struct copy
899 * ad_marker_send - send marker information/response on a given port
900 * @port: the port we're looking at
901 * @marker: marker data to send
903 * Returns: 0 on success
906 static int ad_marker_send(struct port
*port
, struct marker
*marker
)
908 struct slave
*slave
= port
->slave
;
910 struct marker_header
*marker_header
;
911 int length
= sizeof(struct marker_header
);
912 struct mac_addr lacpdu_multicast_address
= AD_MULTICAST_LACPDU_ADDR
;
914 skb
= dev_alloc_skb(length
+ 16);
919 skb_reserve(skb
, 16);
921 skb
->dev
= slave
->dev
;
922 skb
->mac
.raw
= skb
->data
;
923 skb
->nh
.raw
= skb
->data
+ ETH_HLEN
;
924 skb
->protocol
= PKT_TYPE_LACPDU
;
926 marker_header
= (struct marker_header
*)skb_put(skb
, length
);
928 marker_header
->ad_header
.destination_address
= lacpdu_multicast_address
;
929 /* Note: source addres is set to be the member's PERMANENT address, because we use it
930 to identify loopback MARKERs in receive. */
931 marker_header
->ad_header
.source_address
= *((struct mac_addr
*)(slave
->perm_hwaddr
));
932 marker_header
->ad_header
.length_type
= PKT_TYPE_LACPDU
;
934 marker_header
->marker
= *marker
; // struct copy
942 * ad_mux_machine - handle a port's mux state machine
943 * @port: the port we're looking at
946 static void ad_mux_machine(struct port
*port
)
948 mux_states_t last_state
;
950 // keep current State Machine state to compare later if it was changed
951 last_state
= port
->sm_mux_state
;
953 if (port
->sm_vars
& AD_PORT_BEGIN
) {
954 port
->sm_mux_state
= AD_MUX_DETACHED
; // next state
956 switch (port
->sm_mux_state
) {
957 case AD_MUX_DETACHED
:
958 if ((port
->sm_vars
& AD_PORT_SELECTED
) || (port
->sm_vars
& AD_PORT_STANDBY
)) { // if SELECTED or STANDBY
959 port
->sm_mux_state
= AD_MUX_WAITING
; // next state
963 // if SELECTED == FALSE return to DETACH state
964 if (!(port
->sm_vars
& AD_PORT_SELECTED
)) { // if UNSELECTED
965 port
->sm_vars
&= ~AD_PORT_READY_N
;
966 // in order to withhold the Selection Logic to check all ports READY_N value
967 // every callback cycle to update ready variable, we check READY_N and update READY here
968 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
969 port
->sm_mux_state
= AD_MUX_DETACHED
; // next state
973 // check if the wait_while_timer expired
974 if (port
->sm_mux_timer_counter
&& !(--port
->sm_mux_timer_counter
)) {
975 port
->sm_vars
|= AD_PORT_READY_N
;
978 // in order to withhold the selection logic to check all ports READY_N value
979 // every callback cycle to update ready variable, we check READY_N and update READY here
980 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
982 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
983 if ((port
->sm_vars
& AD_PORT_READY
) && !port
->sm_mux_timer_counter
) {
984 port
->sm_mux_state
= AD_MUX_ATTACHED
; // next state
987 case AD_MUX_ATTACHED
:
988 // check also if agg_select_timer expired(so the edable port will take place only after this timer)
989 if ((port
->sm_vars
& AD_PORT_SELECTED
) && (port
->partner_oper_port_state
& AD_STATE_SYNCHRONIZATION
) && !__check_agg_selection_timer(port
)) {
990 port
->sm_mux_state
= AD_MUX_COLLECTING_DISTRIBUTING
;// next state
991 } else if (!(port
->sm_vars
& AD_PORT_SELECTED
) || (port
->sm_vars
& AD_PORT_STANDBY
)) { // if UNSELECTED or STANDBY
992 port
->sm_vars
&= ~AD_PORT_READY_N
;
993 // in order to withhold the selection logic to check all ports READY_N value
994 // every callback cycle to update ready variable, we check READY_N and update READY here
995 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
996 port
->sm_mux_state
= AD_MUX_DETACHED
;// next state
999 case AD_MUX_COLLECTING_DISTRIBUTING
:
1000 if (!(port
->sm_vars
& AD_PORT_SELECTED
) || (port
->sm_vars
& AD_PORT_STANDBY
) ||
1001 !(port
->partner_oper_port_state
& AD_STATE_SYNCHRONIZATION
)
1003 port
->sm_mux_state
= AD_MUX_ATTACHED
;// next state
1006 // if port state hasn't changed make
1007 // sure that a collecting distributing
1008 // port in an active aggregator is enabled
1009 if (port
->aggregator
&&
1010 port
->aggregator
->is_active
&&
1011 !__port_is_enabled(port
)) {
1013 __enable_port(port
);
1017 default: //to silence the compiler
1022 // check if the state machine was changed
1023 if (port
->sm_mux_state
!= last_state
) {
1024 dprintk("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", port
->actor_port_number
, last_state
, port
->sm_mux_state
);
1025 switch (port
->sm_mux_state
) {
1026 case AD_MUX_DETACHED
:
1027 __detach_bond_from_agg(port
);
1028 port
->actor_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1029 ad_disable_collecting_distributing(port
);
1030 port
->actor_oper_port_state
&= ~AD_STATE_COLLECTING
;
1031 port
->actor_oper_port_state
&= ~AD_STATE_DISTRIBUTING
;
1034 case AD_MUX_WAITING
:
1035 port
->sm_mux_timer_counter
= __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER
, 0);
1037 case AD_MUX_ATTACHED
:
1038 __attach_bond_to_agg(port
);
1039 port
->actor_oper_port_state
|= AD_STATE_SYNCHRONIZATION
;
1040 port
->actor_oper_port_state
&= ~AD_STATE_COLLECTING
;
1041 port
->actor_oper_port_state
&= ~AD_STATE_DISTRIBUTING
;
1042 ad_disable_collecting_distributing(port
);
1045 case AD_MUX_COLLECTING_DISTRIBUTING
:
1046 port
->actor_oper_port_state
|= AD_STATE_COLLECTING
;
1047 port
->actor_oper_port_state
|= AD_STATE_DISTRIBUTING
;
1048 ad_enable_collecting_distributing(port
);
1051 default: //to silence the compiler
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 // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback)
1071 __get_rx_machine_lock(port
);
1073 // keep current State Machine state to compare later if it was changed
1074 last_state
= port
->sm_rx_state
;
1076 // 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
; // next state
1081 // check if port is not enabled
1082 else if (!(port
->sm_vars
& AD_PORT_BEGIN
) && !port
->is_enabled
&& !(port
->sm_vars
& AD_PORT_MOVED
)) {
1083 port
->sm_rx_state
= AD_RX_PORT_DISABLED
; // next state
1085 // check if new lacpdu arrived
1086 else if (lacpdu
&& ((port
->sm_rx_state
== AD_RX_EXPIRED
) || (port
->sm_rx_state
== AD_RX_DEFAULTED
) || (port
->sm_rx_state
== AD_RX_CURRENT
))) {
1087 port
->sm_rx_timer_counter
= 0; // zero timer
1088 port
->sm_rx_state
= AD_RX_CURRENT
;
1090 // if timer is on, and if it is expired
1091 if (port
->sm_rx_timer_counter
&& !(--port
->sm_rx_timer_counter
)) {
1092 switch (port
->sm_rx_state
) {
1094 port
->sm_rx_state
= AD_RX_DEFAULTED
; // next state
1097 port
->sm_rx_state
= AD_RX_EXPIRED
; // next state
1099 default: //to silence the compiler
1103 // if no lacpdu arrived and no timer is on
1104 switch (port
->sm_rx_state
) {
1105 case AD_RX_PORT_DISABLED
:
1106 if (port
->sm_vars
& AD_PORT_MOVED
) {
1107 port
->sm_rx_state
= AD_RX_INITIALIZE
; // next state
1108 } else if (port
->is_enabled
&& (port
->sm_vars
& AD_PORT_LACP_ENABLED
)) {
1109 port
->sm_rx_state
= AD_RX_EXPIRED
; // next state
1110 } else if (port
->is_enabled
&& ((port
->sm_vars
& AD_PORT_LACP_ENABLED
) == 0)) {
1111 port
->sm_rx_state
= AD_RX_LACP_DISABLED
; // next state
1114 default: //to silence the compiler
1121 // check if the State machine was changed or new lacpdu arrived
1122 if ((port
->sm_rx_state
!= last_state
) || (lacpdu
)) {
1123 dprintk("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", port
->actor_port_number
, last_state
, port
->sm_rx_state
);
1124 switch (port
->sm_rx_state
) {
1125 case AD_RX_INITIALIZE
:
1126 if (!(port
->actor_oper_port_key
& AD_DUPLEX_KEY_BITS
)) {
1127 port
->sm_vars
&= ~AD_PORT_LACP_ENABLED
;
1129 port
->sm_vars
|= AD_PORT_LACP_ENABLED
;
1131 port
->sm_vars
&= ~AD_PORT_SELECTED
;
1132 __record_default(port
);
1133 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1134 port
->sm_vars
&= ~AD_PORT_MOVED
;
1135 port
->sm_rx_state
= AD_RX_PORT_DISABLED
; // next state
1137 /*- Fall Through -*/
1139 case AD_RX_PORT_DISABLED
:
1140 port
->sm_vars
&= ~AD_PORT_MATCHED
;
1142 case AD_RX_LACP_DISABLED
:
1143 port
->sm_vars
&= ~AD_PORT_SELECTED
;
1144 __record_default(port
);
1145 port
->partner_oper_port_state
&= ~AD_STATE_AGGREGATION
;
1146 port
->sm_vars
|= AD_PORT_MATCHED
;
1147 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1150 //Reset of the Synchronization flag. (Standard 43.4.12)
1151 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1152 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
1153 port
->partner_oper_port_state
&= ~AD_STATE_SYNCHRONIZATION
;
1154 port
->sm_vars
&= ~AD_PORT_MATCHED
;
1155 port
->partner_oper_port_state
|= AD_SHORT_TIMEOUT
;
1156 port
->sm_rx_timer_counter
= __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER
, (u16
)(AD_SHORT_TIMEOUT
));
1157 port
->actor_oper_port_state
|= AD_STATE_EXPIRED
;
1159 case AD_RX_DEFAULTED
:
1160 __update_default_selected(port
);
1161 __record_default(port
);
1162 port
->sm_vars
|= AD_PORT_MATCHED
;
1163 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1166 // detect loopback situation
1167 if (!MAC_ADDRESS_COMPARE(&(lacpdu
->actor_system
), &(port
->actor_system
))) {
1168 // INFO_RECEIVED_LOOPBACK_FRAMES
1169 printk(KERN_ERR DRV_NAME
": %s: An illegal loopback occurred on "
1170 "adapter (%s). Check the configuration to verify that all "
1171 "Adapters are connected to 802.3ad compliant switch ports\n",
1172 port
->slave
->dev
->master
->name
, port
->slave
->dev
->name
);
1173 __release_rx_machine_lock(port
);
1176 __update_selected(lacpdu
, port
);
1177 __update_ntt(lacpdu
, port
);
1178 __record_pdu(lacpdu
, port
);
1179 __choose_matched(lacpdu
, port
);
1180 port
->sm_rx_timer_counter
= __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER
, (u16
)(port
->actor_oper_port_state
& AD_STATE_LACP_TIMEOUT
));
1181 port
->actor_oper_port_state
&= ~AD_STATE_EXPIRED
;
1182 // verify that if the aggregator is enabled, the port is enabled too.
1183 //(because if the link goes down for a short time, the 802.3ad will not
1184 // catch it, and the port will continue to be disabled)
1185 if (port
->aggregator
&& port
->aggregator
->is_active
&& !__port_is_enabled(port
)) {
1186 __enable_port(port
);
1189 default: //to silence the compiler
1193 __release_rx_machine_lock(port
);
1197 * ad_tx_machine - handle a port's tx state machine
1198 * @port: the port we're looking at
1201 static void ad_tx_machine(struct port
*port
)
1203 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1204 if (port
->sm_tx_timer_counter
&& !(--port
->sm_tx_timer_counter
)) {
1205 // check if there is something to send
1206 if (port
->ntt
&& (port
->sm_vars
& AD_PORT_LACP_ENABLED
)) {
1207 __update_lacpdu_from_port(port
);
1209 if (ad_lacpdu_send(port
) >= 0) {
1210 dprintk("Sent LACPDU on port %d\n", port
->actor_port_number
);
1211 // mark ntt as false, so it will not be sent again until demanded
1215 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
1216 port
->sm_tx_timer_counter
=ad_ticks_per_sec
/AD_MAX_TX_IN_SECOND
;
1221 * ad_periodic_machine - handle a port's periodic state machine
1222 * @port: the port we're looking at
1224 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1226 static void ad_periodic_machine(struct port
*port
)
1228 periodic_states_t last_state
;
1230 // keep current state machine state to compare later if it was changed
1231 last_state
= port
->sm_periodic_state
;
1233 // check if port was reinitialized
1234 if (((port
->sm_vars
& AD_PORT_BEGIN
) || !(port
->sm_vars
& AD_PORT_LACP_ENABLED
) || !port
->is_enabled
) ||
1235 (!(port
->actor_oper_port_state
& AD_STATE_LACP_ACTIVITY
) && !(port
->partner_oper_port_state
& AD_STATE_LACP_ACTIVITY
))
1237 port
->sm_periodic_state
= AD_NO_PERIODIC
; // next state
1239 // check if state machine should change state
1240 else if (port
->sm_periodic_timer_counter
) {
1241 // check if periodic state machine expired
1242 if (!(--port
->sm_periodic_timer_counter
)) {
1243 // if expired then do tx
1244 port
->sm_periodic_state
= AD_PERIODIC_TX
; // next state
1246 // If not expired, check if there is some new timeout parameter from the partner state
1247 switch (port
->sm_periodic_state
) {
1248 case AD_FAST_PERIODIC
:
1249 if (!(port
->partner_oper_port_state
& AD_STATE_LACP_TIMEOUT
)) {
1250 port
->sm_periodic_state
= AD_SLOW_PERIODIC
; // next state
1253 case AD_SLOW_PERIODIC
:
1254 if ((port
->partner_oper_port_state
& AD_STATE_LACP_TIMEOUT
)) {
1255 // stop current timer
1256 port
->sm_periodic_timer_counter
= 0;
1257 port
->sm_periodic_state
= AD_PERIODIC_TX
; // next state
1260 default: //to silence the compiler
1265 switch (port
->sm_periodic_state
) {
1266 case AD_NO_PERIODIC
:
1267 port
->sm_periodic_state
= AD_FAST_PERIODIC
; // next state
1269 case AD_PERIODIC_TX
:
1270 if (!(port
->partner_oper_port_state
& AD_STATE_LACP_TIMEOUT
)) {
1271 port
->sm_periodic_state
= AD_SLOW_PERIODIC
; // next state
1273 port
->sm_periodic_state
= AD_FAST_PERIODIC
; // next state
1276 default: //to silence the compiler
1281 // check if the state machine was changed
1282 if (port
->sm_periodic_state
!= last_state
) {
1283 dprintk("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", port
->actor_port_number
, last_state
, port
->sm_periodic_state
);
1284 switch (port
->sm_periodic_state
) {
1285 case AD_NO_PERIODIC
:
1286 port
->sm_periodic_timer_counter
= 0; // zero timer
1288 case AD_FAST_PERIODIC
:
1289 port
->sm_periodic_timer_counter
= __ad_timer_to_ticks(AD_PERIODIC_TIMER
, (u16
)(AD_FAST_PERIODIC_TIME
))-1; // decrement 1 tick we lost in the PERIODIC_TX cycle
1291 case AD_SLOW_PERIODIC
:
1292 port
->sm_periodic_timer_counter
= __ad_timer_to_ticks(AD_PERIODIC_TIMER
, (u16
)(AD_SLOW_PERIODIC_TIME
))-1; // decrement 1 tick we lost in the PERIODIC_TX cycle
1294 case AD_PERIODIC_TX
:
1297 default: //to silence the compiler
1304 * ad_port_selection_logic - select aggregation groups
1305 * @port: the port we're looking at
1307 * Select aggregation groups, and assign each port for it's aggregetor. The
1308 * selection logic is called in the inititalization (after all the handshkes),
1309 * and after every lacpdu receive (if selected is off).
1311 static void ad_port_selection_logic(struct port
*port
)
1313 struct aggregator
*aggregator
, *free_aggregator
= NULL
, *temp_aggregator
;
1314 struct port
*last_port
= NULL
, *curr_port
;
1317 // if the port is already Selected, do nothing
1318 if (port
->sm_vars
& AD_PORT_SELECTED
) {
1322 // if the port is connected to other aggregator, detach it
1323 if (port
->aggregator
) {
1324 // detach the port from its former aggregator
1325 temp_aggregator
=port
->aggregator
;
1326 for (curr_port
=temp_aggregator
->lag_ports
; curr_port
; last_port
=curr_port
, curr_port
=curr_port
->next_port_in_aggregator
) {
1327 if (curr_port
== port
) {
1328 temp_aggregator
->num_of_ports
--;
1329 if (!last_port
) {// if it is the first port attached to the aggregator
1330 temp_aggregator
->lag_ports
=port
->next_port_in_aggregator
;
1331 } else {// not the first port attached to the aggregator
1332 last_port
->next_port_in_aggregator
=port
->next_port_in_aggregator
;
1335 // clear the port's relations to this aggregator
1336 port
->aggregator
= NULL
;
1337 port
->next_port_in_aggregator
=NULL
;
1338 port
->actor_port_aggregator_identifier
=0;
1340 dprintk("Port %d left LAG %d\n", port
->actor_port_number
, temp_aggregator
->aggregator_identifier
);
1341 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1342 if (!temp_aggregator
->lag_ports
) {
1343 ad_clear_agg(temp_aggregator
);
1348 if (!curr_port
) { // meaning: the port was related to an aggregator but was not on the aggregator port list
1349 printk(KERN_WARNING DRV_NAME
": %s: Warning: Port %d (on %s) was "
1350 "related to aggregator %d but was not on its port list\n",
1351 port
->slave
->dev
->master
->name
,
1352 port
->actor_port_number
, port
->slave
->dev
->name
,
1353 port
->aggregator
->aggregator_identifier
);
1356 // search on all aggregators for a suitable aggregator for this port
1357 for (aggregator
= __get_first_agg(port
); aggregator
;
1358 aggregator
= __get_next_agg(aggregator
)) {
1360 // keep a free aggregator for later use(if needed)
1361 if (!aggregator
->lag_ports
) {
1362 if (!free_aggregator
) {
1363 free_aggregator
=aggregator
;
1367 // check if current aggregator suits us
1368 if (((aggregator
->actor_oper_aggregator_key
== port
->actor_oper_port_key
) && // if all parameters match AND
1369 !MAC_ADDRESS_COMPARE(&(aggregator
->partner_system
), &(port
->partner_oper_system
)) &&
1370 (aggregator
->partner_system_priority
== port
->partner_oper_system_priority
) &&
1371 (aggregator
->partner_oper_aggregator_key
== port
->partner_oper_key
)
1373 ((MAC_ADDRESS_COMPARE(&(port
->partner_oper_system
), &(null_mac_addr
)) && // partner answers
1374 !aggregator
->is_individual
) // but is not individual OR
1377 // attach to the founded aggregator
1378 port
->aggregator
= aggregator
;
1379 port
->actor_port_aggregator_identifier
=port
->aggregator
->aggregator_identifier
;
1380 port
->next_port_in_aggregator
=aggregator
->lag_ports
;
1381 port
->aggregator
->num_of_ports
++;
1382 aggregator
->lag_ports
=port
;
1383 dprintk("Port %d joined LAG %d(existing LAG)\n", port
->actor_port_number
, port
->aggregator
->aggregator_identifier
);
1385 // mark this port as selected
1386 port
->sm_vars
|= AD_PORT_SELECTED
;
1392 // the port couldn't find an aggregator - attach it to a new aggregator
1394 if (free_aggregator
) {
1395 // assign port a new aggregator
1396 port
->aggregator
= free_aggregator
;
1397 port
->actor_port_aggregator_identifier
=port
->aggregator
->aggregator_identifier
;
1399 // update the new aggregator's parameters
1400 // if port was responsed from the end-user
1401 if (port
->actor_oper_port_key
& AD_DUPLEX_KEY_BITS
) {// if port is full duplex
1402 port
->aggregator
->is_individual
= 0;
1404 port
->aggregator
->is_individual
= 1;
1407 port
->aggregator
->actor_admin_aggregator_key
= port
->actor_admin_port_key
;
1408 port
->aggregator
->actor_oper_aggregator_key
= port
->actor_oper_port_key
;
1409 port
->aggregator
->partner_system
=port
->partner_oper_system
;
1410 port
->aggregator
->partner_system_priority
= port
->partner_oper_system_priority
;
1411 port
->aggregator
->partner_oper_aggregator_key
= port
->partner_oper_key
;
1412 port
->aggregator
->receive_state
= 1;
1413 port
->aggregator
->transmit_state
= 1;
1414 port
->aggregator
->lag_ports
= port
;
1415 port
->aggregator
->num_of_ports
++;
1417 // mark this port as selected
1418 port
->sm_vars
|= AD_PORT_SELECTED
;
1420 dprintk("Port %d joined LAG %d(new LAG)\n", port
->actor_port_number
, port
->aggregator
->aggregator_identifier
);
1422 printk(KERN_ERR DRV_NAME
": %s: Port %d (on %s) did not find a suitable aggregator\n",
1423 port
->slave
->dev
->master
->name
,
1424 port
->actor_port_number
, port
->slave
->dev
->name
);
1427 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1428 // else set ready=FALSE in all aggregator's ports
1429 __set_agg_ports_ready(port
->aggregator
, __agg_ports_are_ready(port
->aggregator
));
1431 if (!__check_agg_selection_timer(port
) && (aggregator
= __get_first_agg(port
))) {
1432 ad_agg_selection_logic(aggregator
);
1437 * ad_agg_selection_logic - select an aggregation group for a team
1438 * @aggregator: the aggregator we're looking at
1440 * It is assumed that only one aggregator may be selected for a team.
1441 * The logic of this function is to select (at first time) the aggregator with
1442 * the most ports attached to it, and to reselect the active aggregator only if
1443 * the previous aggregator has no more ports related to it.
1445 * FIXME: this function MUST be called with the first agg in the bond, or
1446 * __get_active_agg() won't work correctly. This function should be better
1447 * called with the bond itself, and retrieve the first agg from it.
1449 static void ad_agg_selection_logic(struct aggregator
*aggregator
)
1451 struct aggregator
*best_aggregator
= NULL
, *active_aggregator
= NULL
;
1452 struct aggregator
*last_active_aggregator
= NULL
, *origin_aggregator
;
1456 origin_aggregator
= aggregator
;
1458 //get current active aggregator
1459 last_active_aggregator
= __get_active_agg(aggregator
);
1461 // search for the aggregator with the most ports attached to it.
1463 // count how many candidate lag's we have
1464 if (aggregator
->lag_ports
) {
1467 if (aggregator
->is_active
&& !aggregator
->is_individual
&& // if current aggregator is the active aggregator
1468 MAC_ADDRESS_COMPARE(&(aggregator
->partner_system
), &(null_mac_addr
))) { // and partner answers to 802.3ad PDUs
1469 if (aggregator
->num_of_ports
) { // if any ports attached to the current aggregator
1470 best_aggregator
=NULL
; // disregard the best aggregator that was chosen by now
1471 break; // stop the selection of other aggregator if there are any ports attached to this active aggregator
1472 } else { // no ports attached to this active aggregator
1473 aggregator
->is_active
= 0; // mark this aggregator as not active anymore
1476 if (aggregator
->num_of_ports
) { // if any ports attached
1477 if (best_aggregator
) { // if there is a candidte aggregator
1478 //The reasons for choosing new best aggregator:
1479 // 1. if current agg is NOT individual and the best agg chosen so far is individual OR
1480 // current and best aggs are both individual or both not individual, AND
1481 // 2a. current agg partner reply but best agg partner do not reply OR
1482 // 2b. current agg partner reply OR current agg partner do not reply AND best agg partner also do not reply AND
1483 // current has more ports/bandwidth, or same amount of ports but current has faster ports, THEN
1484 // current agg become best agg so far
1486 //if current agg is NOT individual and the best agg chosen so far is individual change best_aggregator
1487 if (!aggregator
->is_individual
&& best_aggregator
->is_individual
) {
1488 best_aggregator
=aggregator
;
1490 // current and best aggs are both individual or both not individual
1491 else if ((aggregator
->is_individual
&& best_aggregator
->is_individual
) ||
1492 (!aggregator
->is_individual
&& !best_aggregator
->is_individual
)) {
1493 // current and best aggs are both individual or both not individual AND
1494 // current agg partner reply but best agg partner do not reply
1495 if ((MAC_ADDRESS_COMPARE(&(aggregator
->partner_system
), &(null_mac_addr
)) &&
1496 !MAC_ADDRESS_COMPARE(&(best_aggregator
->partner_system
), &(null_mac_addr
)))) {
1497 best_aggregator
=aggregator
;
1499 // current agg partner reply OR current agg partner do not reply AND best agg partner also do not reply
1500 else if (! (!MAC_ADDRESS_COMPARE(&(aggregator
->partner_system
), &(null_mac_addr
)) &&
1501 MAC_ADDRESS_COMPARE(&(best_aggregator
->partner_system
), &(null_mac_addr
)))) {
1502 if ((__get_agg_selection_mode(aggregator
->lag_ports
) == AD_BANDWIDTH
)&&
1503 (__get_agg_bandwidth(aggregator
) > __get_agg_bandwidth(best_aggregator
))) {
1504 best_aggregator
=aggregator
;
1505 } else if (__get_agg_selection_mode(aggregator
->lag_ports
) == AD_COUNT
) {
1506 if (((aggregator
->num_of_ports
> best_aggregator
->num_of_ports
) &&
1507 (aggregator
->actor_oper_aggregator_key
& AD_SPEED_KEY_BITS
))||
1508 ((aggregator
->num_of_ports
== best_aggregator
->num_of_ports
) &&
1509 ((u16
)(aggregator
->actor_oper_aggregator_key
& AD_SPEED_KEY_BITS
) >
1510 (u16
)(best_aggregator
->actor_oper_aggregator_key
& AD_SPEED_KEY_BITS
)))) {
1511 best_aggregator
=aggregator
;
1517 best_aggregator
=aggregator
;
1520 aggregator
->is_active
= 0; // mark all aggregators as not active anymore
1521 } while ((aggregator
= __get_next_agg(aggregator
)));
1523 // if we have new aggregator selected, don't replace the old aggregator if it has an answering partner,
1524 // or if both old aggregator and new aggregator don't have answering partner
1525 if (best_aggregator
) {
1526 if (last_active_aggregator
&& last_active_aggregator
->lag_ports
&& last_active_aggregator
->lag_ports
->is_enabled
&&
1527 (MAC_ADDRESS_COMPARE(&(last_active_aggregator
->partner_system
), &(null_mac_addr
)) || // partner answers OR
1528 (!MAC_ADDRESS_COMPARE(&(last_active_aggregator
->partner_system
), &(null_mac_addr
)) && // both old and new
1529 !MAC_ADDRESS_COMPARE(&(best_aggregator
->partner_system
), &(null_mac_addr
)))) // partner do not answer
1531 // if new aggregator has link, and old aggregator does not, replace old aggregator.(do nothing)
1532 // -> don't replace otherwise.
1533 if (!(!last_active_aggregator
->actor_oper_aggregator_key
&& best_aggregator
->actor_oper_aggregator_key
)) {
1534 best_aggregator
=NULL
;
1535 last_active_aggregator
->is_active
= 1; // don't replace good old aggregator
1541 // if there is new best aggregator, activate it
1542 if (best_aggregator
) {
1543 for (aggregator
= __get_first_agg(best_aggregator
->lag_ports
);
1545 aggregator
= __get_next_agg(aggregator
)) {
1547 dprintk("Agg=%d; Ports=%d; a key=%d; p key=%d; Indiv=%d; Active=%d\n",
1548 aggregator
->aggregator_identifier
, aggregator
->num_of_ports
,
1549 aggregator
->actor_oper_aggregator_key
, aggregator
->partner_oper_aggregator_key
,
1550 aggregator
->is_individual
, aggregator
->is_active
);
1553 // check if any partner replys
1554 if (best_aggregator
->is_individual
) {
1555 printk(KERN_WARNING DRV_NAME
": %s: Warning: No 802.3ad response from "
1556 "the link partner for any adapters in the bond\n",
1557 best_aggregator
->slave
->dev
->master
->name
);
1560 // check if there are more than one aggregator
1561 if (num_of_aggs
> 1) {
1562 dprintk("Warning: More than one Link Aggregation Group was "
1563 "found in the bond. Only one group will function in the bond\n");
1566 best_aggregator
->is_active
= 1;
1567 dprintk("LAG %d choosed as the active LAG\n", best_aggregator
->aggregator_identifier
);
1568 dprintk("Agg=%d; Ports=%d; a key=%d; p key=%d; Indiv=%d; Active=%d\n",
1569 best_aggregator
->aggregator_identifier
, best_aggregator
->num_of_ports
,
1570 best_aggregator
->actor_oper_aggregator_key
, best_aggregator
->partner_oper_aggregator_key
,
1571 best_aggregator
->is_individual
, best_aggregator
->is_active
);
1573 // disable the ports that were related to the former active_aggregator
1574 if (last_active_aggregator
) {
1575 for (port
=last_active_aggregator
->lag_ports
; port
; port
=port
->next_port_in_aggregator
) {
1576 __disable_port(port
);
1581 // if the selected aggregator is of join individuals(partner_system is NULL), enable their ports
1582 active_aggregator
= __get_active_agg(origin_aggregator
);
1584 if (active_aggregator
) {
1585 if (!MAC_ADDRESS_COMPARE(&(active_aggregator
->partner_system
), &(null_mac_addr
))) {
1586 for (port
=active_aggregator
->lag_ports
; port
; port
=port
->next_port_in_aggregator
) {
1587 __enable_port(port
);
1594 * ad_clear_agg - clear a given aggregator's parameters
1595 * @aggregator: the aggregator we're looking at
1598 static void ad_clear_agg(struct aggregator
*aggregator
)
1601 aggregator
->is_individual
= 0;
1602 aggregator
->actor_admin_aggregator_key
= 0;
1603 aggregator
->actor_oper_aggregator_key
= 0;
1604 aggregator
->partner_system
= null_mac_addr
;
1605 aggregator
->partner_system_priority
= 0;
1606 aggregator
->partner_oper_aggregator_key
= 0;
1607 aggregator
->receive_state
= 0;
1608 aggregator
->transmit_state
= 0;
1609 aggregator
->lag_ports
= NULL
;
1610 aggregator
->is_active
= 0;
1611 aggregator
->num_of_ports
= 0;
1612 dprintk("LAG %d was cleared\n", aggregator
->aggregator_identifier
);
1617 * ad_initialize_agg - initialize a given aggregator's parameters
1618 * @aggregator: the aggregator we're looking at
1621 static void ad_initialize_agg(struct aggregator
*aggregator
)
1624 ad_clear_agg(aggregator
);
1626 aggregator
->aggregator_mac_address
= null_mac_addr
;
1627 aggregator
->aggregator_identifier
= 0;
1628 aggregator
->slave
= NULL
;
1633 * ad_initialize_port - initialize a given port's parameters
1634 * @aggregator: the aggregator we're looking at
1635 * @lacp_fast: boolean. whether fast periodic should be used
1638 static void ad_initialize_port(struct port
*port
, int lacp_fast
)
1641 port
->actor_port_number
= 1;
1642 port
->actor_port_priority
= 0xff;
1643 port
->actor_system
= null_mac_addr
;
1644 port
->actor_system_priority
= 0xffff;
1645 port
->actor_port_aggregator_identifier
= 0;
1647 port
->actor_admin_port_key
= 1;
1648 port
->actor_oper_port_key
= 1;
1649 port
->actor_admin_port_state
= AD_STATE_AGGREGATION
| AD_STATE_LACP_ACTIVITY
;
1650 port
->actor_oper_port_state
= AD_STATE_AGGREGATION
| AD_STATE_LACP_ACTIVITY
;
1653 port
->actor_oper_port_state
|= AD_STATE_LACP_TIMEOUT
;
1656 port
->partner_admin_system
= null_mac_addr
;
1657 port
->partner_oper_system
= null_mac_addr
;
1658 port
->partner_admin_system_priority
= 0xffff;
1659 port
->partner_oper_system_priority
= 0xffff;
1660 port
->partner_admin_key
= 1;
1661 port
->partner_oper_key
= 1;
1662 port
->partner_admin_port_number
= 1;
1663 port
->partner_oper_port_number
= 1;
1664 port
->partner_admin_port_priority
= 0xff;
1665 port
->partner_oper_port_priority
= 0xff;
1666 port
->partner_admin_port_state
= 1;
1667 port
->partner_oper_port_state
= 1;
1668 port
->is_enabled
= 1;
1669 // ****** private parameters ******
1670 port
->sm_vars
= 0x3;
1671 port
->sm_rx_state
= 0;
1672 port
->sm_rx_timer_counter
= 0;
1673 port
->sm_periodic_state
= 0;
1674 port
->sm_periodic_timer_counter
= 0;
1675 port
->sm_mux_state
= 0;
1676 port
->sm_mux_timer_counter
= 0;
1677 port
->sm_tx_state
= 0;
1678 port
->sm_tx_timer_counter
= 0;
1680 port
->aggregator
= NULL
;
1681 port
->next_port_in_aggregator
= NULL
;
1682 port
->transaction_id
= 0;
1684 ad_initialize_lacpdu(&(port
->lacpdu
));
1689 * ad_enable_collecting_distributing - enable a port's transmit/receive
1690 * @port: the port we're looking at
1692 * Enable @port if it's in an active aggregator
1694 static void ad_enable_collecting_distributing(struct port
*port
)
1696 if (port
->aggregator
->is_active
) {
1697 dprintk("Enabling port %d(LAG %d)\n", port
->actor_port_number
, port
->aggregator
->aggregator_identifier
);
1698 __enable_port(port
);
1703 * ad_disable_collecting_distributing - disable a port's transmit/receive
1704 * @port: the port we're looking at
1707 static void ad_disable_collecting_distributing(struct port
*port
)
1709 if (port
->aggregator
&& MAC_ADDRESS_COMPARE(&(port
->aggregator
->partner_system
), &(null_mac_addr
))) {
1710 dprintk("Disabling port %d(LAG %d)\n", port
->actor_port_number
, port
->aggregator
->aggregator_identifier
);
1711 __disable_port(port
);
1717 * ad_marker_info_send - send a marker information frame
1718 * @port: the port we're looking at
1720 * This function does nothing since we decided not to implement send and handle
1721 * response for marker PDU's, in this stage, but only to respond to marker
1724 static void ad_marker_info_send(struct port
*port
)
1726 struct marker marker
;
1729 // fill the marker PDU with the appropriate values
1730 marker
.subtype
= 0x02;
1731 marker
.version_number
= 0x01;
1732 marker
.tlv_type
= AD_MARKER_INFORMATION_SUBTYPE
;
1733 marker
.marker_length
= 0x16;
1734 // convert requester_port to Big Endian
1735 marker
.requester_port
= (((port
->actor_port_number
& 0xFF) << 8) |((u16
)(port
->actor_port_number
& 0xFF00) >> 8));
1736 marker
.requester_system
= port
->actor_system
;
1737 // convert requester_port(u32) to Big Endian
1738 marker
.requester_transaction_id
= (((++port
->transaction_id
& 0xFF) << 24) |((port
->transaction_id
& 0xFF00) << 8) |((port
->transaction_id
& 0xFF0000) >> 8) |((port
->transaction_id
& 0xFF000000) >> 24));
1740 marker
.tlv_type_terminator
= 0x00;
1741 marker
.terminator_length
= 0x00;
1742 for (index
=0; index
<90; index
++) {
1743 marker
.reserved_90
[index
]=0;
1746 // send the marker information
1747 if (ad_marker_send(port
, &marker
) >= 0) {
1748 dprintk("Sent Marker Information on port %d\n", port
->actor_port_number
);
1754 * ad_marker_info_received - handle receive of a Marker information frame
1755 * @marker_info: Marker info received
1756 * @port: the port we're looking at
1759 static void ad_marker_info_received(struct marker
*marker_info
,struct port
*port
)
1761 struct marker marker
;
1763 // copy the received marker data to the response marker
1764 //marker = *marker_info;
1765 memcpy(&marker
, marker_info
, sizeof(struct marker
));
1766 // change the marker subtype to marker response
1767 marker
.tlv_type
=AD_MARKER_RESPONSE_SUBTYPE
;
1768 // send the marker response
1770 if (ad_marker_send(port
, &marker
) >= 0) {
1771 dprintk("Sent Marker Response on port %d\n", port
->actor_port_number
);
1776 * ad_marker_response_received - handle receive of a marker response frame
1777 * @marker: marker PDU received
1778 * @port: the port we're looking at
1780 * This function does nothing since we decided not to implement send and handle
1781 * response for marker PDU's, in this stage, but only to respond to marker
1784 static void ad_marker_response_received(struct marker
*marker
, struct port
*port
)
1786 marker
=NULL
; // just to satisfy the compiler
1787 port
=NULL
; // just to satisfy the compiler
1788 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1792 * ad_initialize_lacpdu - initialize a given lacpdu structure
1793 * @lacpdu: lacpdu structure to initialize
1796 static void ad_initialize_lacpdu(struct lacpdu
*lacpdu
)
1800 // initialize lacpdu data
1801 lacpdu
->subtype
= 0x01;
1802 lacpdu
->version_number
= 0x01;
1803 lacpdu
->tlv_type_actor_info
= 0x01;
1804 lacpdu
->actor_information_length
= 0x14;
1805 // lacpdu->actor_system_priority updated on send
1806 // lacpdu->actor_system updated on send
1807 // lacpdu->actor_key updated on send
1808 // lacpdu->actor_port_priority updated on send
1809 // lacpdu->actor_port updated on send
1810 // lacpdu->actor_state updated on send
1811 lacpdu
->tlv_type_partner_info
= 0x02;
1812 lacpdu
->partner_information_length
= 0x14;
1813 for (index
=0; index
<=2; index
++) {
1814 lacpdu
->reserved_3_1
[index
]=0;
1816 // lacpdu->partner_system_priority updated on send
1817 // lacpdu->partner_system updated on send
1818 // lacpdu->partner_key updated on send
1819 // lacpdu->partner_port_priority updated on send
1820 // lacpdu->partner_port updated on send
1821 // lacpdu->partner_state updated on send
1822 for (index
=0; index
<=2; index
++) {
1823 lacpdu
->reserved_3_2
[index
]=0;
1825 lacpdu
->tlv_type_collector_info
= 0x03;
1826 lacpdu
->collector_information_length
= 0x10;
1827 lacpdu
->collector_max_delay
= AD_COLLECTOR_MAX_DELAY
;
1828 for (index
=0; index
<=11; index
++) {
1829 lacpdu
->reserved_12
[index
]=0;
1831 lacpdu
->tlv_type_terminator
= 0x00;
1832 lacpdu
->terminator_length
= 0;
1833 for (index
=0; index
<=49; index
++) {
1834 lacpdu
->reserved_50
[index
]=0;
1838 //////////////////////////////////////////////////////////////////////////////////////
1839 // ================= AD exported functions to the main bonding code ==================
1840 //////////////////////////////////////////////////////////////////////////////////////
1842 // Check aggregators status in team every T seconds
1843 #define AD_AGGREGATOR_SELECTION_TIMER 8
1845 static u16 aggregator_identifier
;
1848 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1849 * @bond: bonding struct to work on
1850 * @tick_resolution: tick duration (millisecond resolution)
1851 * @lacp_fast: boolean. whether fast periodic should be used
1853 * Can be called only after the mac address of the bond is set.
1855 void bond_3ad_initialize(struct bonding
*bond
, u16 tick_resolution
, int lacp_fast
)
1857 // check that the bond is not initialized yet
1858 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond
).system
.sys_mac_addr
), &(bond
->dev
->dev_addr
))) {
1860 aggregator_identifier
= 0;
1862 BOND_AD_INFO(bond
).lacp_fast
= lacp_fast
;
1863 BOND_AD_INFO(bond
).system
.sys_priority
= 0xFFFF;
1864 BOND_AD_INFO(bond
).system
.sys_mac_addr
= *((struct mac_addr
*)bond
->dev
->dev_addr
);
1866 // initialize how many times this module is called in one second(should be about every 100ms)
1867 ad_ticks_per_sec
= tick_resolution
;
1869 // initialize the aggregator selection timer(to activate an aggregation selection after initialize)
1870 BOND_AD_INFO(bond
).agg_select_timer
= (AD_AGGREGATOR_SELECTION_TIMER
* ad_ticks_per_sec
);
1871 BOND_AD_INFO(bond
).agg_select_mode
= AD_BANDWIDTH
;
1876 * bond_3ad_bind_slave - initialize a slave's port
1877 * @slave: slave struct to work on
1879 * Returns: 0 on success
1882 int bond_3ad_bind_slave(struct slave
*slave
)
1884 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
1886 struct aggregator
*aggregator
;
1889 printk(KERN_ERR DRV_NAME
": %s: The slave %s is not attached to its bond\n",
1890 slave
->dev
->master
->name
, slave
->dev
->name
);
1894 //check that the slave has not been intialized yet.
1895 if (SLAVE_AD_INFO(slave
).port
.slave
!= slave
) {
1897 // port initialization
1898 port
= &(SLAVE_AD_INFO(slave
).port
);
1900 ad_initialize_port(port
, BOND_AD_INFO(bond
).lacp_fast
);
1902 port
->slave
= slave
;
1903 port
->actor_port_number
= SLAVE_AD_INFO(slave
).id
;
1904 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1905 // ------------------------------------------------------------
1906 // Port key : | User key | Speed |Duplex|
1907 // ------------------------------------------------------------
1909 port
->actor_admin_port_key
= 0; // initialize this parameter
1910 port
->actor_admin_port_key
|= __get_duplex(port
);
1911 port
->actor_admin_port_key
|= (__get_link_speed(port
) << 1);
1912 port
->actor_oper_port_key
= port
->actor_admin_port_key
;
1913 // if the port is not full duplex, then the port should be not lacp Enabled
1914 if (!(port
->actor_oper_port_key
& AD_DUPLEX_KEY_BITS
)) {
1915 port
->sm_vars
&= ~AD_PORT_LACP_ENABLED
;
1917 // actor system is the bond's system
1918 port
->actor_system
= BOND_AD_INFO(bond
).system
.sys_mac_addr
;
1919 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1920 port
->sm_tx_timer_counter
= ad_ticks_per_sec
/AD_MAX_TX_IN_SECOND
;
1921 port
->aggregator
= NULL
;
1922 port
->next_port_in_aggregator
= NULL
;
1924 __disable_port(port
);
1925 __initialize_port_locks(port
);
1928 // aggregator initialization
1929 aggregator
= &(SLAVE_AD_INFO(slave
).aggregator
);
1931 ad_initialize_agg(aggregator
);
1933 aggregator
->aggregator_mac_address
= *((struct mac_addr
*)bond
->dev
->dev_addr
);
1934 aggregator
->aggregator_identifier
= (++aggregator_identifier
);
1935 aggregator
->slave
= slave
;
1936 aggregator
->is_active
= 0;
1937 aggregator
->num_of_ports
= 0;
1944 * bond_3ad_unbind_slave - deinitialize a slave's port
1945 * @slave: slave struct to work on
1947 * Search for the aggregator that is related to this port, remove the
1948 * aggregator and assign another aggregator for other port related to it
1949 * (if any), and remove the port.
1951 void bond_3ad_unbind_slave(struct slave
*slave
)
1953 struct port
*port
, *prev_port
, *temp_port
;
1954 struct aggregator
*aggregator
, *new_aggregator
, *temp_aggregator
;
1955 int select_new_active_agg
= 0;
1957 // find the aggregator related to this slave
1958 aggregator
= &(SLAVE_AD_INFO(slave
).aggregator
);
1960 // find the port related to this slave
1961 port
= &(SLAVE_AD_INFO(slave
).port
);
1963 // if slave is null, the whole port is not initialized
1965 printk(KERN_WARNING DRV_NAME
": Warning: %s: Trying to "
1966 "unbind an uninitialized port on %s\n",
1967 slave
->dev
->master
->name
, slave
->dev
->name
);
1971 dprintk("Unbinding Link Aggregation Group %d\n", aggregator
->aggregator_identifier
);
1973 /* Tell the partner that this port is not suitable for aggregation */
1974 port
->actor_oper_port_state
&= ~AD_STATE_AGGREGATION
;
1975 __update_lacpdu_from_port(port
);
1976 ad_lacpdu_send(port
);
1978 // check if this aggregator is occupied
1979 if (aggregator
->lag_ports
) {
1980 // check if there are other ports related to this aggregator except
1981 // the port related to this slave(thats ensure us that there is a
1982 // reason to search for new aggregator, and that we will find one
1983 if ((aggregator
->lag_ports
!= port
) || (aggregator
->lag_ports
->next_port_in_aggregator
)) {
1984 // find new aggregator for the related port(s)
1985 new_aggregator
= __get_first_agg(port
);
1986 for (; new_aggregator
; new_aggregator
= __get_next_agg(new_aggregator
)) {
1987 // if the new aggregator is empty, or it connected to to our port only
1988 if (!new_aggregator
->lag_ports
|| ((new_aggregator
->lag_ports
== port
) && !new_aggregator
->lag_ports
->next_port_in_aggregator
)) {
1992 // if new aggregator found, copy the aggregator's parameters
1993 // and connect the related lag_ports to the new aggregator
1994 if ((new_aggregator
) && ((!new_aggregator
->lag_ports
) || ((new_aggregator
->lag_ports
== port
) && !new_aggregator
->lag_ports
->next_port_in_aggregator
))) {
1995 dprintk("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator
->aggregator_identifier
, new_aggregator
->aggregator_identifier
);
1997 if ((new_aggregator
->lag_ports
== port
) && new_aggregator
->is_active
) {
1998 printk(KERN_INFO DRV_NAME
": %s: Removing an active aggregator\n",
1999 aggregator
->slave
->dev
->master
->name
);
2000 // select new active aggregator
2001 select_new_active_agg
= 1;
2004 new_aggregator
->is_individual
= aggregator
->is_individual
;
2005 new_aggregator
->actor_admin_aggregator_key
= aggregator
->actor_admin_aggregator_key
;
2006 new_aggregator
->actor_oper_aggregator_key
= aggregator
->actor_oper_aggregator_key
;
2007 new_aggregator
->partner_system
= aggregator
->partner_system
;
2008 new_aggregator
->partner_system_priority
= aggregator
->partner_system_priority
;
2009 new_aggregator
->partner_oper_aggregator_key
= aggregator
->partner_oper_aggregator_key
;
2010 new_aggregator
->receive_state
= aggregator
->receive_state
;
2011 new_aggregator
->transmit_state
= aggregator
->transmit_state
;
2012 new_aggregator
->lag_ports
= aggregator
->lag_ports
;
2013 new_aggregator
->is_active
= aggregator
->is_active
;
2014 new_aggregator
->num_of_ports
= aggregator
->num_of_ports
;
2016 // update the information that is written on the ports about the aggregator
2017 for (temp_port
=aggregator
->lag_ports
; temp_port
; temp_port
=temp_port
->next_port_in_aggregator
) {
2018 temp_port
->aggregator
=new_aggregator
;
2019 temp_port
->actor_port_aggregator_identifier
= new_aggregator
->aggregator_identifier
;
2022 // clear the aggregator
2023 ad_clear_agg(aggregator
);
2025 if (select_new_active_agg
) {
2026 ad_agg_selection_logic(__get_first_agg(port
));
2029 printk(KERN_WARNING DRV_NAME
": %s: Warning: unbinding aggregator, "
2030 "and could not find a new aggregator for its ports\n",
2031 slave
->dev
->master
->name
);
2033 } else { // in case that the only port related to this aggregator is the one we want to remove
2034 select_new_active_agg
= aggregator
->is_active
;
2035 // clear the aggregator
2036 ad_clear_agg(aggregator
);
2037 if (select_new_active_agg
) {
2038 printk(KERN_INFO DRV_NAME
": %s: Removing an active aggregator\n",
2039 slave
->dev
->master
->name
);
2040 // select new active aggregator
2041 ad_agg_selection_logic(__get_first_agg(port
));
2046 dprintk("Unbinding port %d\n", port
->actor_port_number
);
2047 // find the aggregator that this port is connected to
2048 temp_aggregator
= __get_first_agg(port
);
2049 for (; temp_aggregator
; temp_aggregator
= __get_next_agg(temp_aggregator
)) {
2051 // search the port in the aggregator's related ports
2052 for (temp_port
=temp_aggregator
->lag_ports
; temp_port
; prev_port
=temp_port
, temp_port
=temp_port
->next_port_in_aggregator
) {
2053 if (temp_port
== port
) { // the aggregator found - detach the port from this aggregator
2055 prev_port
->next_port_in_aggregator
= temp_port
->next_port_in_aggregator
;
2057 temp_aggregator
->lag_ports
= temp_port
->next_port_in_aggregator
;
2059 temp_aggregator
->num_of_ports
--;
2060 if (temp_aggregator
->num_of_ports
==0) {
2061 select_new_active_agg
= temp_aggregator
->is_active
;
2062 // clear the aggregator
2063 ad_clear_agg(temp_aggregator
);
2064 if (select_new_active_agg
) {
2065 printk(KERN_INFO DRV_NAME
": %s: Removing an active aggregator\n",
2066 slave
->dev
->master
->name
);
2067 // select new active aggregator
2068 ad_agg_selection_logic(__get_first_agg(port
));
2079 * bond_3ad_state_machine_handler - handle state machines timeout
2080 * @bond: bonding struct to work on
2082 * The state machine handling concept in this module is to check every tick
2083 * which state machine should operate any function. The execution order is
2084 * round robin, so when we have an interaction between state machines, the
2085 * reply of one to each other might be delayed until next tick.
2087 * This function also complete the initialization when the agg_select_timer
2088 * times out, and it selects an aggregator for the ports that are yet not
2089 * related to any aggregator, and selects the active aggregator for a bond.
2091 void bond_3ad_state_machine_handler(struct bonding
*bond
)
2094 struct aggregator
*aggregator
;
2096 read_lock(&bond
->lock
);
2098 if (bond
->kill_timers
) {
2102 //check if there are any slaves
2103 if (bond
->slave_cnt
== 0) {
2107 // check if agg_select_timer timer after initialize is timed out
2108 if (BOND_AD_INFO(bond
).agg_select_timer
&& !(--BOND_AD_INFO(bond
).agg_select_timer
)) {
2109 // select the active aggregator for the bond
2110 if ((port
= __get_first_port(bond
))) {
2112 printk(KERN_WARNING DRV_NAME
": %s: Warning: bond's first port is "
2113 "uninitialized\n", bond
->dev
->name
);
2117 aggregator
= __get_first_agg(port
);
2118 ad_agg_selection_logic(aggregator
);
2122 // for each port run the state machines
2123 for (port
= __get_first_port(bond
); port
; port
= __get_next_port(port
)) {
2125 printk(KERN_WARNING DRV_NAME
": %s: Warning: Found an uninitialized "
2126 "port\n", bond
->dev
->name
);
2130 ad_rx_machine(NULL
, port
);
2131 ad_periodic_machine(port
);
2132 ad_port_selection_logic(port
);
2133 ad_mux_machine(port
);
2134 ad_tx_machine(port
);
2136 // turn off the BEGIN bit, since we already handled it
2137 if (port
->sm_vars
& AD_PORT_BEGIN
) {
2138 port
->sm_vars
&= ~AD_PORT_BEGIN
;
2143 mod_timer(&(BOND_AD_INFO(bond
).ad_timer
), jiffies
+ ad_delta_in_ticks
);
2145 read_unlock(&bond
->lock
);
2149 * bond_3ad_rx_indication - handle a received frame
2150 * @lacpdu: received lacpdu
2151 * @slave: slave struct to work on
2152 * @length: length of the data received
2154 * It is assumed that frames that were sent on this NIC don't returned as new
2155 * received frames (loopback). Since only the payload is given to this
2156 * function, it check for loopback.
2158 static void bond_3ad_rx_indication(struct lacpdu
*lacpdu
, struct slave
*slave
, u16 length
)
2162 if (length
>= sizeof(struct lacpdu
)) {
2164 port
= &(SLAVE_AD_INFO(slave
).port
);
2167 printk(KERN_WARNING DRV_NAME
": %s: Warning: port of slave %s is "
2168 "uninitialized\n", slave
->dev
->name
, slave
->dev
->master
->name
);
2172 switch (lacpdu
->subtype
) {
2173 case AD_TYPE_LACPDU
:
2174 __ntohs_lacpdu(lacpdu
);
2175 dprintk("Received LACPDU on port %d\n", port
->actor_port_number
);
2176 ad_rx_machine(lacpdu
, port
);
2179 case AD_TYPE_MARKER
:
2180 // No need to convert fields to Little Endian since we don't use the marker's fields.
2182 switch (((struct marker
*)lacpdu
)->tlv_type
) {
2183 case AD_MARKER_INFORMATION_SUBTYPE
:
2184 dprintk("Received Marker Information on port %d\n", port
->actor_port_number
);
2185 ad_marker_info_received((struct marker
*)lacpdu
, port
);
2188 case AD_MARKER_RESPONSE_SUBTYPE
:
2189 dprintk("Received Marker Response on port %d\n", port
->actor_port_number
);
2190 ad_marker_response_received((struct marker
*)lacpdu
, port
);
2194 dprintk("Received an unknown Marker subtype on slot %d\n", port
->actor_port_number
);
2201 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2202 * @slave: slave struct to work on
2204 * Handle reselection of aggregator (if needed) for this port.
2206 void bond_3ad_adapter_speed_changed(struct slave
*slave
)
2210 port
= &(SLAVE_AD_INFO(slave
).port
);
2212 // if slave is null, the whole port is not initialized
2214 printk(KERN_WARNING DRV_NAME
": Warning: %s: speed "
2215 "changed for uninitialized port on %s\n",
2216 slave
->dev
->master
->name
, slave
->dev
->name
);
2220 port
->actor_admin_port_key
&= ~AD_SPEED_KEY_BITS
;
2221 port
->actor_oper_port_key
=port
->actor_admin_port_key
|= (__get_link_speed(port
) << 1);
2222 dprintk("Port %d changed speed\n", port
->actor_port_number
);
2223 // there is no need to reselect a new aggregator, just signal the
2224 // state machines to reinitialize
2225 port
->sm_vars
|= AD_PORT_BEGIN
;
2229 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2230 * @slave: slave struct to work on
2232 * Handle reselection of aggregator (if needed) for this port.
2234 void bond_3ad_adapter_duplex_changed(struct slave
*slave
)
2238 port
=&(SLAVE_AD_INFO(slave
).port
);
2240 // if slave is null, the whole port is not initialized
2242 printk(KERN_WARNING DRV_NAME
": %s: Warning: duplex changed "
2243 "for uninitialized port on %s\n",
2244 slave
->dev
->master
->name
, slave
->dev
->name
);
2248 port
->actor_admin_port_key
&= ~AD_DUPLEX_KEY_BITS
;
2249 port
->actor_oper_port_key
=port
->actor_admin_port_key
|= __get_duplex(port
);
2250 dprintk("Port %d changed duplex\n", port
->actor_port_number
);
2251 // there is no need to reselect a new aggregator, just signal the
2252 // state machines to reinitialize
2253 port
->sm_vars
|= AD_PORT_BEGIN
;
2257 * bond_3ad_handle_link_change - handle a slave's link status change indication
2258 * @slave: slave struct to work on
2259 * @status: whether the link is now up or down
2261 * Handle reselection of aggregator (if needed) for this port.
2263 void bond_3ad_handle_link_change(struct slave
*slave
, char link
)
2267 port
= &(SLAVE_AD_INFO(slave
).port
);
2269 // if slave is null, the whole port is not initialized
2271 printk(KERN_WARNING DRV_NAME
": Warning: %s: link status changed for "
2272 "uninitialized port on %s\n",
2273 slave
->dev
->master
->name
, slave
->dev
->name
);
2277 // on link down we are zeroing duplex and speed since some of the adaptors(ce1000.lan) report full duplex/speed instead of N/A(duplex) / 0(speed)
2278 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2279 if (link
== BOND_LINK_UP
) {
2280 port
->is_enabled
= 1;
2281 port
->actor_admin_port_key
&= ~AD_DUPLEX_KEY_BITS
;
2282 port
->actor_oper_port_key
=port
->actor_admin_port_key
|= __get_duplex(port
);
2283 port
->actor_admin_port_key
&= ~AD_SPEED_KEY_BITS
;
2284 port
->actor_oper_port_key
=port
->actor_admin_port_key
|= (__get_link_speed(port
) << 1);
2286 /* link has failed */
2287 port
->is_enabled
= 0;
2288 port
->actor_admin_port_key
&= ~AD_DUPLEX_KEY_BITS
;
2289 port
->actor_oper_port_key
= (port
->actor_admin_port_key
&= ~AD_SPEED_KEY_BITS
);
2291 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2292 // there is no need to reselect a new aggregator, just signal the
2293 // state machines to reinitialize
2294 port
->sm_vars
|= AD_PORT_BEGIN
;
2298 * bond_3ad_get_active_agg_info - get information of the active aggregator
2299 * @bond: bonding struct to work on
2300 * @ad_info: ad_info struct to fill with the bond's info
2302 * Returns: 0 on success
2305 int bond_3ad_get_active_agg_info(struct bonding
*bond
, struct ad_info
*ad_info
)
2307 struct aggregator
*aggregator
= NULL
;
2310 for (port
= __get_first_port(bond
); port
; port
= __get_next_port(port
)) {
2311 if (port
->aggregator
&& port
->aggregator
->is_active
) {
2312 aggregator
= port
->aggregator
;
2318 ad_info
->aggregator_id
= aggregator
->aggregator_identifier
;
2319 ad_info
->ports
= aggregator
->num_of_ports
;
2320 ad_info
->actor_key
= aggregator
->actor_oper_aggregator_key
;
2321 ad_info
->partner_key
= aggregator
->partner_oper_aggregator_key
;
2322 memcpy(ad_info
->partner_system
, aggregator
->partner_system
.mac_addr_value
, ETH_ALEN
);
2329 int bond_3ad_xmit_xor(struct sk_buff
*skb
, struct net_device
*dev
)
2331 struct slave
*slave
, *start_at
;
2332 struct bonding
*bond
= dev
->priv
;
2337 struct ad_info ad_info
;
2340 /* make sure that the slaves list will
2341 * not change during tx
2343 read_lock(&bond
->lock
);
2345 if (!BOND_IS_OK(bond
)) {
2349 if (bond_3ad_get_active_agg_info(bond
, &ad_info
)) {
2350 printk(KERN_DEBUG DRV_NAME
": %s: Error: "
2351 "bond_3ad_get_active_agg_info failed\n", dev
->name
);
2355 slaves_in_agg
= ad_info
.ports
;
2356 agg_id
= ad_info
.aggregator_id
;
2358 if (slaves_in_agg
== 0) {
2359 /*the aggregator is empty*/
2360 printk(KERN_DEBUG DRV_NAME
": %s: Error: active "
2361 "aggregator is empty\n",
2366 slave_agg_no
= bond
->xmit_hash_policy(skb
, dev
, slaves_in_agg
);
2368 bond_for_each_slave(bond
, slave
, i
) {
2369 struct aggregator
*agg
= SLAVE_AD_INFO(slave
).port
.aggregator
;
2371 if (agg
&& (agg
->aggregator_identifier
== agg_id
)) {
2373 if (slave_agg_no
< 0) {
2379 if (slave_agg_no
>= 0) {
2380 printk(KERN_ERR DRV_NAME
": %s: Error: Couldn't find a slave to tx on "
2381 "for aggregator ID %d\n", dev
->name
, agg_id
);
2387 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
2388 int slave_agg_id
= 0;
2389 struct aggregator
*agg
= SLAVE_AD_INFO(slave
).port
.aggregator
;
2392 slave_agg_id
= agg
->aggregator_identifier
;
2395 if (SLAVE_IS_OK(slave
) && agg
&& (slave_agg_id
== agg_id
)) {
2396 res
= bond_dev_queue_xmit(bond
, skb
, slave
->dev
);
2403 /* no suitable interface, frame not sent */
2406 read_unlock(&bond
->lock
);
2410 int bond_3ad_lacpdu_recv(struct sk_buff
*skb
, struct net_device
*dev
, struct packet_type
* ptype
, struct net_device
*orig_dev
)
2412 struct bonding
*bond
= dev
->priv
;
2413 struct slave
*slave
= NULL
;
2414 int ret
= NET_RX_DROP
;
2416 if (!(dev
->flags
& IFF_MASTER
))
2419 read_lock(&bond
->lock
);
2420 slave
= bond_get_slave_by_dev((struct bonding
*)dev
->priv
, orig_dev
);
2424 bond_3ad_rx_indication((struct lacpdu
*) skb
->data
, slave
, skb
->len
);
2426 ret
= NET_RX_SUCCESS
;
2429 read_unlock(&bond
->lock
);