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>
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
88 #define AD_LINK_SPEED_BITMASK_10000MBPS 0x10
91 // compare MAC addresses
92 #define MAC_ADDRESS_COMPARE(A, B) memcmp(A, B, ETH_ALEN)
94 static struct mac_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
95 static u16 ad_ticks_per_sec;
96 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
98 static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
100 // ================= main 802.3ad protocol functions ==================
101 static int ad_lacpdu_send(struct port *port);
102 static int ad_marker_send(struct port *port, struct bond_marker *marker);
103 static void ad_mux_machine(struct port *port);
104 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
105 static void ad_tx_machine(struct port *port);
106 static void ad_periodic_machine(struct port *port);
107 static void ad_port_selection_logic(struct port *port);
108 static void ad_agg_selection_logic(struct aggregator *aggregator);
109 static void ad_clear_agg(struct aggregator *aggregator);
110 static void ad_initialize_agg(struct aggregator *aggregator);
111 static void ad_initialize_port(struct port *port, int lacp_fast);
112 static void ad_enable_collecting_distributing(struct port *port);
113 static void ad_disable_collecting_distributing(struct port *port);
114 static void ad_marker_info_received(struct bond_marker *marker_info, struct port *port);
115 static void ad_marker_response_received(struct bond_marker *marker, struct port *port);
118 /////////////////////////////////////////////////////////////////////////////////
119 // ================= api to bonding and kernel code ==================
120 /////////////////////////////////////////////////////////////////////////////////
123 * __get_bond_by_port - get the port's bonding struct
124 * @port: the port we're looking at
126 * Return @port's bonding struct, or %NULL if it can't be found.
128 static inline struct bonding *__get_bond_by_port(struct port *port)
130 if (port->slave == NULL) {
134 return bond_get_bond_by_slave(port->slave);
138 * __get_first_port - get the first port in the bond
139 * @bond: the bond we're looking at
141 * Return the port of the first slave in @bond, or %NULL if it can't be found.
143 static inline struct port *__get_first_port(struct bonding *bond)
145 if (bond->slave_cnt == 0) {
149 return &(SLAVE_AD_INFO(bond->first_slave).port);
153 * __get_next_port - get the next port in the bond
154 * @port: the port we're looking at
156 * Return the port of the slave that is next in line of @port's slave in the
157 * bond, or %NULL if it can't be found.
159 static inline struct port *__get_next_port(struct port *port)
161 struct bonding *bond = __get_bond_by_port(port);
162 struct slave *slave = port->slave;
164 // If there's no bond for this port, or this is the last slave
165 if ((bond == NULL) || (slave->next == bond->first_slave)) {
169 return &(SLAVE_AD_INFO(slave->next).port);
173 * __get_first_agg - get the first aggregator in the bond
174 * @bond: the bond we're looking at
176 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
179 static inline struct aggregator *__get_first_agg(struct port *port)
181 struct bonding *bond = __get_bond_by_port(port);
183 // If there's no bond for this port, or bond has no slaves
184 if ((bond == NULL) || (bond->slave_cnt == 0)) {
188 return &(SLAVE_AD_INFO(bond->first_slave).aggregator);
192 * __get_next_agg - get the next aggregator in the bond
193 * @aggregator: the aggregator we're looking at
195 * Return the aggregator of the slave that is next in line of @aggregator's
196 * slave in the bond, or %NULL if it can't be found.
198 static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
200 struct slave *slave = aggregator->slave;
201 struct bonding *bond = bond_get_bond_by_slave(slave);
203 // If there's no bond for this aggregator, or this is the last slave
204 if ((bond == NULL) || (slave->next == bond->first_slave)) {
208 return &(SLAVE_AD_INFO(slave->next).aggregator);
214 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
215 * address for the partner). Return 0 if not.
217 static inline int __agg_has_partner(struct aggregator *agg)
219 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
223 * __disable_port - disable the port's slave
224 * @port: the port we're looking at
227 static inline void __disable_port(struct port *port)
229 bond_set_slave_inactive_flags(port->slave);
233 * __enable_port - enable the port's slave, if it's up
234 * @port: the port we're looking at
237 static inline void __enable_port(struct port *port)
239 struct slave *slave = port->slave;
241 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev)) {
242 bond_set_slave_active_flags(slave);
247 * __port_is_enabled - check if the port's slave is in active state
248 * @port: the port we're looking at
251 static inline int __port_is_enabled(struct port *port)
253 return(port->slave->state == BOND_STATE_ACTIVE);
257 * __get_agg_selection_mode - get the aggregator selection mode
258 * @port: the port we're looking at
260 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
262 static inline u32 __get_agg_selection_mode(struct port *port)
264 struct bonding *bond = __get_bond_by_port(port);
267 return BOND_AD_STABLE;
270 return BOND_AD_INFO(bond).agg_select_mode;
274 * __check_agg_selection_timer - check if the selection timer has expired
275 * @port: the port we're looking at
278 static inline int __check_agg_selection_timer(struct port *port)
280 struct bonding *bond = __get_bond_by_port(port);
286 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
290 * __get_rx_machine_lock - lock the port's RX machine
291 * @port: the port we're looking at
294 static inline void __get_rx_machine_lock(struct port *port)
296 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
300 * __release_rx_machine_lock - unlock the port's RX machine
301 * @port: the port we're looking at
304 static inline void __release_rx_machine_lock(struct port *port)
306 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
310 * __get_link_speed - get a port's speed
311 * @port: the port we're looking at
313 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
315 * %AD_LINK_SPEED_BITMASK_10MBPS,
316 * %AD_LINK_SPEED_BITMASK_100MBPS,
317 * %AD_LINK_SPEED_BITMASK_1000MBPS,
318 * %AD_LINK_SPEED_BITMASK_10000MBPS
320 static u16 __get_link_speed(struct port *port)
322 struct slave *slave = port->slave;
325 /* this if covers only a special case: when the configuration starts with
326 * link down, it sets the speed to 0.
327 * This is done in spite of the fact that the e100 driver reports 0 to be
328 * compatible with MVT in the future.*/
329 if (slave->link != BOND_LINK_UP) {
332 switch (slave->speed) {
334 speed = AD_LINK_SPEED_BITMASK_10MBPS;
338 speed = AD_LINK_SPEED_BITMASK_100MBPS;
342 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
346 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
350 speed = 0; // unknown speed value from ethtool. shouldn't happen
355 pr_debug("Port %d Received link speed %d update from adapter\n", port->actor_port_number, speed);
360 * __get_duplex - get a port's duplex
361 * @port: the port we're looking at
363 * Return @port's duplex in 802.3ad bitmask format. i.e.:
364 * 0x01 if in full duplex
367 static u8 __get_duplex(struct port *port)
369 struct slave *slave = port->slave;
373 // handling a special case: when the configuration starts with
374 // link down, it sets the duplex to 0.
375 if (slave->link != BOND_LINK_UP) {
378 switch (slave->duplex) {
381 pr_debug("Port %d Received status full duplex update from adapter\n", port->actor_port_number);
386 pr_debug("Port %d Received status NOT full duplex update from adapter\n", port->actor_port_number);
394 * __initialize_port_locks - initialize a port's RX machine spinlock
395 * @port: the port we're looking at
398 static inline void __initialize_port_locks(struct port *port)
400 // make sure it isn't called twice
401 spin_lock_init(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
407 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
408 * @timer_type: which timer to operate
409 * @par: timer parameter. see below
411 * If @timer_type is %current_while_timer, @par indicates long/short timer.
412 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
413 * %SLOW_PERIODIC_TIME.
415 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
417 u16 retval=0; //to silence the compiler
419 switch (timer_type) {
420 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
421 if (par) { // for short or long timeout
422 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
424 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
427 case AD_ACTOR_CHURN_TIMER: // for local churn machine
428 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
430 case AD_PERIODIC_TIMER: // for periodic machine
431 retval = (par*ad_ticks_per_sec); // long timeout
433 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
434 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
436 case AD_WAIT_WHILE_TIMER: // for selection machine
437 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
444 /////////////////////////////////////////////////////////////////////////////////
445 // ================= ad_rx_machine helper functions ==================
446 /////////////////////////////////////////////////////////////////////////////////
449 * __record_pdu - record parameters from a received lacpdu
450 * @lacpdu: the lacpdu we've received
451 * @port: the port we're looking at
453 * Record the parameter values for the Actor carried in a received lacpdu as
454 * the current partner operational parameter values and sets
455 * actor_oper_port_state.defaulted to FALSE.
457 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
459 if (lacpdu && port) {
460 struct port_params *partner = &port->partner_oper;
462 // record the new parameter values for the partner operational
463 partner->port_number = ntohs(lacpdu->actor_port);
464 partner->port_priority = ntohs(lacpdu->actor_port_priority);
465 partner->system = lacpdu->actor_system;
466 partner->system_priority = ntohs(lacpdu->actor_system_priority);
467 partner->key = ntohs(lacpdu->actor_key);
468 partner->port_state = lacpdu->actor_state;
470 // set actor_oper_port_state.defaulted to FALSE
471 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
473 // set the partner sync. to on if the partner is sync. and the port is matched
474 if ((port->sm_vars & AD_PORT_MATCHED) && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
475 partner->port_state |= AD_STATE_SYNCHRONIZATION;
477 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
483 * __record_default - record default parameters
484 * @port: the port we're looking at
486 * This function records the default parameter values for the partner carried
487 * in the Partner Admin parameters as the current partner operational parameter
488 * values and sets actor_oper_port_state.defaulted to TRUE.
490 static void __record_default(struct port *port)
493 // record the partner admin parameters
494 memcpy(&port->partner_oper, &port->partner_admin,
495 sizeof(struct port_params));
497 // set actor_oper_port_state.defaulted to true
498 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
503 * __update_selected - update a port's Selected variable from a received lacpdu
504 * @lacpdu: the lacpdu we've received
505 * @port: the port we're looking at
507 * Update the value of the selected variable, using parameter values from a
508 * newly received lacpdu. The parameter values for the Actor carried in the
509 * received PDU are compared with the corresponding operational parameter
510 * values for the ports partner. If one or more of the comparisons shows that
511 * the value(s) received in the PDU differ from the current operational values,
512 * then selected is set to FALSE and actor_oper_port_state.synchronization is
513 * set to out_of_sync. Otherwise, selected remains unchanged.
515 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
517 if (lacpdu && port) {
518 const struct port_params *partner = &port->partner_oper;
520 // check if any parameter is different
521 if (ntohs(lacpdu->actor_port) != partner->port_number
522 || ntohs(lacpdu->actor_port_priority) != partner->port_priority
523 || MAC_ADDRESS_COMPARE(&lacpdu->actor_system, &partner->system)
524 || ntohs(lacpdu->actor_system_priority) != partner->system_priority
525 || ntohs(lacpdu->actor_key) != partner->key
526 || (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
527 // update the state machine Selected variable
528 port->sm_vars &= ~AD_PORT_SELECTED;
534 * __update_default_selected - update a port's Selected variable from Partner
535 * @port: the port we're looking at
537 * This function updates the value of the selected variable, using the partner
538 * administrative parameter values. The administrative values are compared with
539 * the corresponding operational parameter values for the partner. If one or
540 * more of the comparisons shows that the administrative value(s) differ from
541 * the current operational values, then Selected is set to FALSE and
542 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
543 * Selected remains unchanged.
545 static void __update_default_selected(struct port *port)
548 const struct port_params *admin = &port->partner_admin;
549 const struct port_params *oper = &port->partner_oper;
551 // check if any parameter is different
552 if (admin->port_number != oper->port_number
553 || admin->port_priority != oper->port_priority
554 || MAC_ADDRESS_COMPARE(&admin->system, &oper->system)
555 || admin->system_priority != oper->system_priority
556 || admin->key != oper->key
557 || (admin->port_state & AD_STATE_AGGREGATION)
558 != (oper->port_state & AD_STATE_AGGREGATION)) {
559 // update the state machine Selected variable
560 port->sm_vars &= ~AD_PORT_SELECTED;
566 * __choose_matched - update a port's matched variable from a received lacpdu
567 * @lacpdu: the lacpdu we've received
568 * @port: the port we're looking at
570 * Update the value of the matched variable, using parameter values from a
571 * newly received lacpdu. Parameter values for the partner carried in the
572 * received PDU are compared with the corresponding operational parameter
573 * values for the actor. Matched is set to TRUE if all of these parameters
574 * match and the PDU parameter partner_state.aggregation has the same value as
575 * actor_oper_port_state.aggregation and lacp will actively maintain the link
576 * in the aggregation. Matched is also set to TRUE if the value of
577 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
578 * an individual link and lacp will actively maintain the link. Otherwise,
579 * matched is set to FALSE. LACP is considered to be actively maintaining the
580 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
581 * the actor's actor_oper_port_state.lacp_activity and the PDU's
582 * partner_state.lacp_activity variables are TRUE.
584 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
586 // validate lacpdu and port
587 if (lacpdu && port) {
588 // check if all parameters are alike
589 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
590 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
591 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
592 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
593 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
594 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
595 // or this is individual link(aggregation == FALSE)
596 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
598 // update the state machine Matched variable
599 port->sm_vars |= AD_PORT_MATCHED;
601 port->sm_vars &= ~AD_PORT_MATCHED;
607 * __update_ntt - update a port's ntt variable from a received lacpdu
608 * @lacpdu: the lacpdu we've received
609 * @port: the port we're looking at
611 * Updates the value of the ntt variable, using parameter values from a newly
612 * received lacpdu. The parameter values for the partner carried in the
613 * received PDU are compared with the corresponding operational parameter
614 * values for the Actor. If one or more of the comparisons shows that the
615 * value(s) received in the PDU differ from the current operational values,
616 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
618 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
620 // validate lacpdu and port
621 if (lacpdu && port) {
622 // check if any parameter is different
623 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
624 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
625 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
626 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
627 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
628 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
629 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
630 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
631 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
640 * __attach_bond_to_agg
641 * @port: the port we're looking at
643 * Handle the attaching of the port's control parser/multiplexer and the
644 * aggregator. This function does nothing since the parser/multiplexer of the
645 * receive and the parser/multiplexer of the aggregator are already combined.
647 static void __attach_bond_to_agg(struct port *port)
649 port=NULL; // just to satisfy the compiler
650 // This function does nothing since the parser/multiplexer of the receive
651 // and the parser/multiplexer of the aggregator are already combined
655 * __detach_bond_from_agg
656 * @port: the port we're looking at
658 * Handle the detaching of the port's control parser/multiplexer from the
659 * aggregator. This function does nothing since the parser/multiplexer of the
660 * receive and the parser/multiplexer of the aggregator are already combined.
662 static void __detach_bond_from_agg(struct port *port)
664 port=NULL; // just to satisfy the compiler
665 // This function does nothing sience the parser/multiplexer of the receive
666 // and the parser/multiplexer of the aggregator are already combined
670 * __agg_ports_are_ready - check if all ports in an aggregator are ready
671 * @aggregator: the aggregator we're looking at
674 static int __agg_ports_are_ready(struct aggregator *aggregator)
680 // scan all ports in this aggregator to verfy if they are all ready
681 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
682 if (!(port->sm_vars & AD_PORT_READY_N)) {
693 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
694 * @aggregator: the aggregator we're looking at
695 * @val: Should the ports' ready bit be set on or off
698 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
702 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
704 port->sm_vars |= AD_PORT_READY;
706 port->sm_vars &= ~AD_PORT_READY;
712 * __get_agg_bandwidth - get the total bandwidth of an aggregator
713 * @aggregator: the aggregator we're looking at
716 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
721 if (aggregator->num_of_ports) {
722 basic_speed = __get_link_speed(aggregator->lag_ports);
723 switch (basic_speed) {
724 case AD_LINK_SPEED_BITMASK_1MBPS:
725 bandwidth = aggregator->num_of_ports;
727 case AD_LINK_SPEED_BITMASK_10MBPS:
728 bandwidth = aggregator->num_of_ports * 10;
730 case AD_LINK_SPEED_BITMASK_100MBPS:
731 bandwidth = aggregator->num_of_ports * 100;
733 case AD_LINK_SPEED_BITMASK_1000MBPS:
734 bandwidth = aggregator->num_of_ports * 1000;
736 case AD_LINK_SPEED_BITMASK_10000MBPS:
737 bandwidth = aggregator->num_of_ports * 10000;
740 bandwidth=0; // to silent the compilor ....
747 * __get_active_agg - get the current active aggregator
748 * @aggregator: the aggregator we're looking at
751 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
753 struct aggregator *retval = NULL;
755 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
756 if (aggregator->is_active) {
766 * __update_lacpdu_from_port - update a port's lacpdu fields
767 * @port: the port we're looking at
770 static inline void __update_lacpdu_from_port(struct port *port)
772 struct lacpdu *lacpdu = &port->lacpdu;
773 const struct port_params *partner = &port->partner_oper;
775 /* update current actual Actor parameters */
776 /* lacpdu->subtype initialized
777 * lacpdu->version_number initialized
778 * lacpdu->tlv_type_actor_info initialized
779 * lacpdu->actor_information_length initialized
782 lacpdu->actor_system_priority = htons(port->actor_system_priority);
783 lacpdu->actor_system = port->actor_system;
784 lacpdu->actor_key = htons(port->actor_oper_port_key);
785 lacpdu->actor_port_priority = htons(port->actor_port_priority);
786 lacpdu->actor_port = htons(port->actor_port_number);
787 lacpdu->actor_state = port->actor_oper_port_state;
789 /* lacpdu->reserved_3_1 initialized
790 * lacpdu->tlv_type_partner_info initialized
791 * lacpdu->partner_information_length initialized
794 lacpdu->partner_system_priority = htons(partner->system_priority);
795 lacpdu->partner_system = partner->system;
796 lacpdu->partner_key = htons(partner->key);
797 lacpdu->partner_port_priority = htons(partner->port_priority);
798 lacpdu->partner_port = htons(partner->port_number);
799 lacpdu->partner_state = partner->port_state;
801 /* lacpdu->reserved_3_2 initialized
802 * lacpdu->tlv_type_collector_info initialized
803 * lacpdu->collector_information_length initialized
804 * collector_max_delay initialized
805 * reserved_12[12] initialized
806 * tlv_type_terminator initialized
807 * terminator_length initialized
808 * reserved_50[50] initialized
812 //////////////////////////////////////////////////////////////////////////////////////
813 // ================= main 802.3ad protocol code ======================================
814 //////////////////////////////////////////////////////////////////////////////////////
817 * ad_lacpdu_send - send out a lacpdu packet on a given port
818 * @port: the port we're looking at
820 * Returns: 0 on success
823 static int ad_lacpdu_send(struct port *port)
825 struct slave *slave = port->slave;
827 struct lacpdu_header *lacpdu_header;
828 int length = sizeof(struct lacpdu_header);
830 skb = dev_alloc_skb(length);
835 skb->dev = slave->dev;
836 skb_reset_mac_header(skb);
837 skb->network_header = skb->mac_header + ETH_HLEN;
838 skb->protocol = PKT_TYPE_LACPDU;
839 skb->priority = TC_PRIO_CONTROL;
841 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
843 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
844 /* Note: source addres is set to be the member's PERMANENT address,
845 because we use it to identify loopback lacpdus in receive. */
846 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
847 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
849 lacpdu_header->lacpdu = port->lacpdu; // struct copy
857 * ad_marker_send - send marker information/response on a given port
858 * @port: the port we're looking at
859 * @marker: marker data to send
861 * Returns: 0 on success
864 static int ad_marker_send(struct port *port, struct bond_marker *marker)
866 struct slave *slave = port->slave;
868 struct bond_marker_header *marker_header;
869 int length = sizeof(struct bond_marker_header);
871 skb = dev_alloc_skb(length + 16);
876 skb_reserve(skb, 16);
878 skb->dev = slave->dev;
879 skb_reset_mac_header(skb);
880 skb->network_header = skb->mac_header + ETH_HLEN;
881 skb->protocol = PKT_TYPE_LACPDU;
883 marker_header = (struct bond_marker_header *)skb_put(skb, length);
885 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
886 /* Note: source addres is set to be the member's PERMANENT address,
887 because we use it to identify loopback MARKERs in receive. */
888 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
889 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
891 marker_header->marker = *marker; // struct copy
899 * ad_mux_machine - handle a port's mux state machine
900 * @port: the port we're looking at
903 static void ad_mux_machine(struct port *port)
905 mux_states_t last_state;
907 // keep current State Machine state to compare later if it was changed
908 last_state = port->sm_mux_state;
910 if (port->sm_vars & AD_PORT_BEGIN) {
911 port->sm_mux_state = AD_MUX_DETACHED; // next state
913 switch (port->sm_mux_state) {
914 case AD_MUX_DETACHED:
915 if ((port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if SELECTED or STANDBY
916 port->sm_mux_state = AD_MUX_WAITING; // next state
920 // if SELECTED == FALSE return to DETACH state
921 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
922 port->sm_vars &= ~AD_PORT_READY_N;
923 // in order to withhold the Selection Logic to check all ports READY_N value
924 // every callback cycle to update ready variable, we check READY_N and update READY here
925 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
926 port->sm_mux_state = AD_MUX_DETACHED; // next state
930 // check if the wait_while_timer expired
931 if (port->sm_mux_timer_counter && !(--port->sm_mux_timer_counter)) {
932 port->sm_vars |= AD_PORT_READY_N;
935 // in order to withhold the selection logic to check all ports READY_N value
936 // every callback cycle to update ready variable, we check READY_N and update READY here
937 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
939 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
940 if ((port->sm_vars & AD_PORT_READY) && !port->sm_mux_timer_counter) {
941 port->sm_mux_state = AD_MUX_ATTACHED; // next state
944 case AD_MUX_ATTACHED:
945 // check also if agg_select_timer expired(so the edable port will take place only after this timer)
946 if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) {
947 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
948 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
949 port->sm_vars &= ~AD_PORT_READY_N;
950 // in order to withhold the selection logic to check all ports READY_N value
951 // every callback cycle to update ready variable, we check READY_N and update READY here
952 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
953 port->sm_mux_state = AD_MUX_DETACHED;// next state
956 case AD_MUX_COLLECTING_DISTRIBUTING:
957 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
958 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
960 port->sm_mux_state = AD_MUX_ATTACHED;// next state
963 // if port state hasn't changed make
964 // sure that a collecting distributing
965 // port in an active aggregator is enabled
966 if (port->aggregator &&
967 port->aggregator->is_active &&
968 !__port_is_enabled(port)) {
974 default: //to silence the compiler
979 // check if the state machine was changed
980 if (port->sm_mux_state != last_state) {
981 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_mux_state);
982 switch (port->sm_mux_state) {
983 case AD_MUX_DETACHED:
984 __detach_bond_from_agg(port);
985 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
986 ad_disable_collecting_distributing(port);
987 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
988 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
992 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
994 case AD_MUX_ATTACHED:
995 __attach_bond_to_agg(port);
996 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
997 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
998 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
999 ad_disable_collecting_distributing(port);
1002 case AD_MUX_COLLECTING_DISTRIBUTING:
1003 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1004 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
1005 ad_enable_collecting_distributing(port);
1008 default: //to silence the compiler
1015 * ad_rx_machine - handle a port's rx State Machine
1016 * @lacpdu: the lacpdu we've received
1017 * @port: the port we're looking at
1019 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1020 * CURRENT. If timer expired set the state machine in the proper state.
1021 * In other cases, this function checks if we need to switch to other state.
1023 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1025 rx_states_t last_state;
1027 // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback)
1028 __get_rx_machine_lock(port);
1030 // keep current State Machine state to compare later if it was changed
1031 last_state = port->sm_rx_state;
1033 // check if state machine should change state
1034 // first, check if port was reinitialized
1035 if (port->sm_vars & AD_PORT_BEGIN) {
1036 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1038 // check if port is not enabled
1039 else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED)) {
1040 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1042 // check if new lacpdu arrived
1043 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1044 port->sm_rx_timer_counter = 0; // zero timer
1045 port->sm_rx_state = AD_RX_CURRENT;
1047 // if timer is on, and if it is expired
1048 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1049 switch (port->sm_rx_state) {
1051 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1054 port->sm_rx_state = AD_RX_EXPIRED; // next state
1056 default: //to silence the compiler
1060 // if no lacpdu arrived and no timer is on
1061 switch (port->sm_rx_state) {
1062 case AD_RX_PORT_DISABLED:
1063 if (port->sm_vars & AD_PORT_MOVED) {
1064 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1065 } else if (port->is_enabled && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1066 port->sm_rx_state = AD_RX_EXPIRED; // next state
1067 } else if (port->is_enabled && ((port->sm_vars & AD_PORT_LACP_ENABLED) == 0)) {
1068 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
1071 default: //to silence the compiler
1078 // check if the State machine was changed or new lacpdu arrived
1079 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1080 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_rx_state);
1081 switch (port->sm_rx_state) {
1082 case AD_RX_INITIALIZE:
1083 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1084 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1086 port->sm_vars |= AD_PORT_LACP_ENABLED;
1088 port->sm_vars &= ~AD_PORT_SELECTED;
1089 __record_default(port);
1090 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1091 port->sm_vars &= ~AD_PORT_MOVED;
1092 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1094 /*- Fall Through -*/
1096 case AD_RX_PORT_DISABLED:
1097 port->sm_vars &= ~AD_PORT_MATCHED;
1099 case AD_RX_LACP_DISABLED:
1100 port->sm_vars &= ~AD_PORT_SELECTED;
1101 __record_default(port);
1102 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1103 port->sm_vars |= AD_PORT_MATCHED;
1104 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1107 //Reset of the Synchronization flag. (Standard 43.4.12)
1108 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1109 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
1110 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1111 port->sm_vars &= ~AD_PORT_MATCHED;
1112 port->partner_oper.port_state |= AD_SHORT_TIMEOUT;
1113 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1114 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1116 case AD_RX_DEFAULTED:
1117 __update_default_selected(port);
1118 __record_default(port);
1119 port->sm_vars |= AD_PORT_MATCHED;
1120 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1123 // detect loopback situation
1124 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1125 // INFO_RECEIVED_LOOPBACK_FRAMES
1126 printk(KERN_ERR DRV_NAME ": %s: An illegal loopback occurred on "
1127 "adapter (%s). Check the configuration to verify that all "
1128 "Adapters are connected to 802.3ad compliant switch ports\n",
1129 port->slave->dev->master->name, port->slave->dev->name);
1130 __release_rx_machine_lock(port);
1133 __update_selected(lacpdu, port);
1134 __update_ntt(lacpdu, port);
1135 __record_pdu(lacpdu, port);
1136 __choose_matched(lacpdu, port);
1137 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1138 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1139 // verify that if the aggregator is enabled, the port is enabled too.
1140 //(because if the link goes down for a short time, the 802.3ad will not
1141 // catch it, and the port will continue to be disabled)
1142 if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) {
1143 __enable_port(port);
1146 default: //to silence the compiler
1150 __release_rx_machine_lock(port);
1154 * ad_tx_machine - handle a port's tx state machine
1155 * @port: the port we're looking at
1158 static void ad_tx_machine(struct port *port)
1160 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1161 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1162 // check if there is something to send
1163 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1164 __update_lacpdu_from_port(port);
1166 if (ad_lacpdu_send(port) >= 0) {
1167 pr_debug("Sent LACPDU on port %d\n", port->actor_port_number);
1169 /* mark ntt as false, so it will not be sent again until
1174 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
1175 port->sm_tx_timer_counter=ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1180 * ad_periodic_machine - handle a port's periodic state machine
1181 * @port: the port we're looking at
1183 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1185 static void ad_periodic_machine(struct port *port)
1187 periodic_states_t last_state;
1189 // keep current state machine state to compare later if it was changed
1190 last_state = port->sm_periodic_state;
1192 // check if port was reinitialized
1193 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1194 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1196 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1198 // check if state machine should change state
1199 else if (port->sm_periodic_timer_counter) {
1200 // check if periodic state machine expired
1201 if (!(--port->sm_periodic_timer_counter)) {
1202 // if expired then do tx
1203 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1205 // If not expired, check if there is some new timeout parameter from the partner state
1206 switch (port->sm_periodic_state) {
1207 case AD_FAST_PERIODIC:
1208 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1209 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1212 case AD_SLOW_PERIODIC:
1213 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1214 // stop current timer
1215 port->sm_periodic_timer_counter = 0;
1216 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1219 default: //to silence the compiler
1224 switch (port->sm_periodic_state) {
1225 case AD_NO_PERIODIC:
1226 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1228 case AD_PERIODIC_TX:
1229 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1230 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1232 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1235 default: //to silence the compiler
1240 // check if the state machine was changed
1241 if (port->sm_periodic_state != last_state) {
1242 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_periodic_state);
1243 switch (port->sm_periodic_state) {
1244 case AD_NO_PERIODIC:
1245 port->sm_periodic_timer_counter = 0; // zero timer
1247 case AD_FAST_PERIODIC:
1248 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
1250 case AD_SLOW_PERIODIC:
1251 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
1253 case AD_PERIODIC_TX:
1256 default: //to silence the compiler
1263 * ad_port_selection_logic - select aggregation groups
1264 * @port: the port we're looking at
1266 * Select aggregation groups, and assign each port for it's aggregetor. The
1267 * selection logic is called in the inititalization (after all the handshkes),
1268 * and after every lacpdu receive (if selected is off).
1270 static void ad_port_selection_logic(struct port *port)
1272 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1273 struct port *last_port = NULL, *curr_port;
1276 // if the port is already Selected, do nothing
1277 if (port->sm_vars & AD_PORT_SELECTED) {
1281 // if the port is connected to other aggregator, detach it
1282 if (port->aggregator) {
1283 // detach the port from its former aggregator
1284 temp_aggregator=port->aggregator;
1285 for (curr_port=temp_aggregator->lag_ports; curr_port; last_port=curr_port, curr_port=curr_port->next_port_in_aggregator) {
1286 if (curr_port == port) {
1287 temp_aggregator->num_of_ports--;
1288 if (!last_port) {// if it is the first port attached to the aggregator
1289 temp_aggregator->lag_ports=port->next_port_in_aggregator;
1290 } else {// not the first port attached to the aggregator
1291 last_port->next_port_in_aggregator=port->next_port_in_aggregator;
1294 // clear the port's relations to this aggregator
1295 port->aggregator = NULL;
1296 port->next_port_in_aggregator=NULL;
1297 port->actor_port_aggregator_identifier=0;
1299 pr_debug("Port %d left LAG %d\n", port->actor_port_number, temp_aggregator->aggregator_identifier);
1300 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1301 if (!temp_aggregator->lag_ports) {
1302 ad_clear_agg(temp_aggregator);
1307 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
1308 printk(KERN_WARNING DRV_NAME ": %s: Warning: Port %d (on %s) was "
1309 "related to aggregator %d but was not on its port list\n",
1310 port->slave->dev->master->name,
1311 port->actor_port_number, port->slave->dev->name,
1312 port->aggregator->aggregator_identifier);
1315 // search on all aggregators for a suitable aggregator for this port
1316 for (aggregator = __get_first_agg(port); aggregator;
1317 aggregator = __get_next_agg(aggregator)) {
1319 // keep a free aggregator for later use(if needed)
1320 if (!aggregator->lag_ports) {
1321 if (!free_aggregator) {
1322 free_aggregator=aggregator;
1326 // check if current aggregator suits us
1327 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
1328 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1329 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1330 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1332 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
1333 !aggregator->is_individual) // but is not individual OR
1336 // attach to the founded aggregator
1337 port->aggregator = aggregator;
1338 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1339 port->next_port_in_aggregator=aggregator->lag_ports;
1340 port->aggregator->num_of_ports++;
1341 aggregator->lag_ports=port;
1342 pr_debug("Port %d joined LAG %d(existing LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1344 // mark this port as selected
1345 port->sm_vars |= AD_PORT_SELECTED;
1351 // the port couldn't find an aggregator - attach it to a new aggregator
1353 if (free_aggregator) {
1354 // assign port a new aggregator
1355 port->aggregator = free_aggregator;
1356 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1358 // update the new aggregator's parameters
1359 // if port was responsed from the end-user
1360 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
1361 port->aggregator->is_individual = false;
1363 port->aggregator->is_individual = true;
1366 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1367 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
1368 port->aggregator->partner_system=port->partner_oper.system;
1369 port->aggregator->partner_system_priority = port->partner_oper.system_priority;
1370 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1371 port->aggregator->receive_state = 1;
1372 port->aggregator->transmit_state = 1;
1373 port->aggregator->lag_ports = port;
1374 port->aggregator->num_of_ports++;
1376 // mark this port as selected
1377 port->sm_vars |= AD_PORT_SELECTED;
1379 pr_debug("Port %d joined LAG %d(new LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1381 printk(KERN_ERR DRV_NAME ": %s: Port %d (on %s) did not find a suitable aggregator\n",
1382 port->slave->dev->master->name,
1383 port->actor_port_number, port->slave->dev->name);
1386 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1387 // else set ready=FALSE in all aggregator's ports
1388 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1390 aggregator = __get_first_agg(port);
1391 ad_agg_selection_logic(aggregator);
1395 * Decide if "agg" is a better choice for the new active aggregator that
1396 * the current best, according to the ad_select policy.
1398 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1399 struct aggregator *curr)
1402 * 0. If no best, select current.
1404 * 1. If the current agg is not individual, and the best is
1405 * individual, select current.
1407 * 2. If current agg is individual and the best is not, keep best.
1409 * 3. Therefore, current and best are both individual or both not
1412 * 3a. If current agg partner replied, and best agg partner did not,
1415 * 3b. If current agg partner did not reply and best agg partner
1416 * did reply, keep best.
1418 * 4. Therefore, current and best both have partner replies or
1419 * both do not, so perform selection policy:
1421 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1422 * select by bandwidth.
1424 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1429 if (!curr->is_individual && best->is_individual)
1432 if (curr->is_individual && !best->is_individual)
1435 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1438 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1441 switch (__get_agg_selection_mode(curr->lag_ports)) {
1443 if (curr->num_of_ports > best->num_of_ports)
1446 if (curr->num_of_ports < best->num_of_ports)
1450 case BOND_AD_STABLE:
1451 case BOND_AD_BANDWIDTH:
1452 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1458 printk(KERN_WARNING DRV_NAME
1459 ": %s: Impossible agg select mode %d\n",
1460 curr->slave->dev->master->name,
1461 __get_agg_selection_mode(curr->lag_ports));
1468 static int agg_device_up(const struct aggregator *agg)
1470 return (netif_running(agg->slave->dev) &&
1471 netif_carrier_ok(agg->slave->dev));
1475 * ad_agg_selection_logic - select an aggregation group for a team
1476 * @aggregator: the aggregator we're looking at
1478 * It is assumed that only one aggregator may be selected for a team.
1480 * The logic of this function is to select the aggregator according to
1481 * the ad_select policy:
1483 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1484 * it, and to reselect the active aggregator only if the previous
1485 * aggregator has no more ports related to it.
1487 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1488 * bandwidth, and reselect whenever a link state change takes place or the
1489 * set of slaves in the bond changes.
1491 * BOND_AD_COUNT: select the aggregator with largest number of ports
1492 * (slaves), and reselect whenever a link state change takes place or the
1493 * set of slaves in the bond changes.
1495 * FIXME: this function MUST be called with the first agg in the bond, or
1496 * __get_active_agg() won't work correctly. This function should be better
1497 * called with the bond itself, and retrieve the first agg from it.
1499 static void ad_agg_selection_logic(struct aggregator *agg)
1501 struct aggregator *best, *active, *origin;
1505 active = __get_active_agg(agg);
1506 best = (active && agg_device_up(active)) ? active : NULL;
1511 if (agg->num_of_ports && agg_device_up(agg))
1512 best = ad_agg_selection_test(best, agg);
1514 } while ((agg = __get_next_agg(agg)));
1517 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1519 * For the STABLE policy, don't replace the old active
1520 * aggregator if it's still active (it has an answering
1521 * partner) or if both the best and active don't have an
1522 * answering partner.
1524 if (active && active->lag_ports &&
1525 active->lag_ports->is_enabled &&
1526 (__agg_has_partner(active) ||
1527 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1528 if (!(!active->actor_oper_aggregator_key &&
1529 best->actor_oper_aggregator_key)) {
1531 active->is_active = 1;
1536 if (best && (best == active)) {
1538 active->is_active = 1;
1541 // if there is new best aggregator, activate it
1543 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1544 best->aggregator_identifier, best->num_of_ports,
1545 best->actor_oper_aggregator_key,
1546 best->partner_oper_aggregator_key,
1547 best->is_individual, best->is_active);
1548 pr_debug("best ports %p slave %p %s\n",
1549 best->lag_ports, best->slave,
1550 best->slave ? best->slave->dev->name : "NULL");
1552 for (agg = __get_first_agg(best->lag_ports); agg;
1553 agg = __get_next_agg(agg)) {
1555 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1556 agg->aggregator_identifier, agg->num_of_ports,
1557 agg->actor_oper_aggregator_key,
1558 agg->partner_oper_aggregator_key,
1559 agg->is_individual, agg->is_active);
1562 // check if any partner replys
1563 if (best->is_individual) {
1564 printk(KERN_WARNING DRV_NAME ": %s: Warning: No 802.3ad"
1565 " response from the link partner for any"
1566 " adapters in the bond\n",
1567 best->slave->dev->master->name);
1570 best->is_active = 1;
1571 pr_debug("LAG %d chosen as the active LAG\n",
1572 best->aggregator_identifier);
1573 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1574 best->aggregator_identifier, best->num_of_ports,
1575 best->actor_oper_aggregator_key,
1576 best->partner_oper_aggregator_key,
1577 best->is_individual, best->is_active);
1579 // disable the ports that were related to the former active_aggregator
1581 for (port = active->lag_ports; port;
1582 port = port->next_port_in_aggregator) {
1583 __disable_port(port);
1589 * if the selected aggregator is of join individuals
1590 * (partner_system is NULL), enable their ports
1592 active = __get_active_agg(origin);
1595 if (!__agg_has_partner(active)) {
1596 for (port = active->lag_ports; port;
1597 port = port->next_port_in_aggregator) {
1598 __enable_port(port);
1603 if (origin->slave) {
1604 struct bonding *bond;
1606 bond = bond_get_bond_by_slave(origin->slave);
1608 bond_3ad_set_carrier(bond);
1613 * ad_clear_agg - clear a given aggregator's parameters
1614 * @aggregator: the aggregator we're looking at
1617 static void ad_clear_agg(struct aggregator *aggregator)
1620 aggregator->is_individual = false;
1621 aggregator->actor_admin_aggregator_key = 0;
1622 aggregator->actor_oper_aggregator_key = 0;
1623 aggregator->partner_system = null_mac_addr;
1624 aggregator->partner_system_priority = 0;
1625 aggregator->partner_oper_aggregator_key = 0;
1626 aggregator->receive_state = 0;
1627 aggregator->transmit_state = 0;
1628 aggregator->lag_ports = NULL;
1629 aggregator->is_active = 0;
1630 aggregator->num_of_ports = 0;
1631 pr_debug("LAG %d was cleared\n", aggregator->aggregator_identifier);
1636 * ad_initialize_agg - initialize a given aggregator's parameters
1637 * @aggregator: the aggregator we're looking at
1640 static void ad_initialize_agg(struct aggregator *aggregator)
1643 ad_clear_agg(aggregator);
1645 aggregator->aggregator_mac_address = null_mac_addr;
1646 aggregator->aggregator_identifier = 0;
1647 aggregator->slave = NULL;
1652 * ad_initialize_port - initialize a given port's parameters
1653 * @aggregator: the aggregator we're looking at
1654 * @lacp_fast: boolean. whether fast periodic should be used
1657 static void ad_initialize_port(struct port *port, int lacp_fast)
1659 static const struct port_params tmpl = {
1660 .system_priority = 0xffff,
1663 .port_priority = 0xff,
1666 static const struct lacpdu lacpdu = {
1668 .version_number = 0x01,
1669 .tlv_type_actor_info = 0x01,
1670 .actor_information_length = 0x14,
1671 .tlv_type_partner_info = 0x02,
1672 .partner_information_length = 0x14,
1673 .tlv_type_collector_info = 0x03,
1674 .collector_information_length = 0x10,
1675 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1679 port->actor_port_number = 1;
1680 port->actor_port_priority = 0xff;
1681 port->actor_system = null_mac_addr;
1682 port->actor_system_priority = 0xffff;
1683 port->actor_port_aggregator_identifier = 0;
1685 port->actor_admin_port_key = 1;
1686 port->actor_oper_port_key = 1;
1687 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1688 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1691 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1694 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1695 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1697 port->is_enabled = true;
1698 // ****** private parameters ******
1699 port->sm_vars = 0x3;
1700 port->sm_rx_state = 0;
1701 port->sm_rx_timer_counter = 0;
1702 port->sm_periodic_state = 0;
1703 port->sm_periodic_timer_counter = 0;
1704 port->sm_mux_state = 0;
1705 port->sm_mux_timer_counter = 0;
1706 port->sm_tx_state = 0;
1707 port->sm_tx_timer_counter = 0;
1709 port->aggregator = NULL;
1710 port->next_port_in_aggregator = NULL;
1711 port->transaction_id = 0;
1713 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1718 * ad_enable_collecting_distributing - enable a port's transmit/receive
1719 * @port: the port we're looking at
1721 * Enable @port if it's in an active aggregator
1723 static void ad_enable_collecting_distributing(struct port *port)
1725 if (port->aggregator->is_active) {
1726 pr_debug("Enabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1727 __enable_port(port);
1732 * ad_disable_collecting_distributing - disable a port's transmit/receive
1733 * @port: the port we're looking at
1736 static void ad_disable_collecting_distributing(struct port *port)
1738 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
1739 pr_debug("Disabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1740 __disable_port(port);
1746 * ad_marker_info_send - send a marker information frame
1747 * @port: the port we're looking at
1749 * This function does nothing since we decided not to implement send and handle
1750 * response for marker PDU's, in this stage, but only to respond to marker
1753 static void ad_marker_info_send(struct port *port)
1755 struct bond_marker marker;
1758 // fill the marker PDU with the appropriate values
1759 marker.subtype = 0x02;
1760 marker.version_number = 0x01;
1761 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1762 marker.marker_length = 0x16;
1763 // convert requester_port to Big Endian
1764 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1765 marker.requester_system = port->actor_system;
1766 // convert requester_port(u32) to Big Endian
1767 marker.requester_transaction_id = (((++port->transaction_id & 0xFF) << 24) |((port->transaction_id & 0xFF00) << 8) |((port->transaction_id & 0xFF0000) >> 8) |((port->transaction_id & 0xFF000000) >> 24));
1769 marker.tlv_type_terminator = 0x00;
1770 marker.terminator_length = 0x00;
1771 for (index=0; index<90; index++) {
1772 marker.reserved_90[index]=0;
1775 // send the marker information
1776 if (ad_marker_send(port, &marker) >= 0) {
1777 pr_debug("Sent Marker Information on port %d\n", port->actor_port_number);
1783 * ad_marker_info_received - handle receive of a Marker information frame
1784 * @marker_info: Marker info received
1785 * @port: the port we're looking at
1788 static void ad_marker_info_received(struct bond_marker *marker_info,
1791 struct bond_marker marker;
1793 // copy the received marker data to the response marker
1794 //marker = *marker_info;
1795 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1796 // change the marker subtype to marker response
1797 marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE;
1798 // send the marker response
1800 if (ad_marker_send(port, &marker) >= 0) {
1801 pr_debug("Sent Marker Response on port %d\n", port->actor_port_number);
1806 * ad_marker_response_received - handle receive of a marker response frame
1807 * @marker: marker PDU received
1808 * @port: the port we're looking at
1810 * This function does nothing since we decided not to implement send and handle
1811 * response for marker PDU's, in this stage, but only to respond to marker
1814 static void ad_marker_response_received(struct bond_marker *marker,
1817 marker=NULL; // just to satisfy the compiler
1818 port=NULL; // just to satisfy the compiler
1819 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1822 //////////////////////////////////////////////////////////////////////////////////////
1823 // ================= AD exported functions to the main bonding code ==================
1824 //////////////////////////////////////////////////////////////////////////////////////
1826 // Check aggregators status in team every T seconds
1827 #define AD_AGGREGATOR_SELECTION_TIMER 8
1830 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1832 * Set the aggregation selection timer, to initiate an agg selection in
1833 * the very near future. Called during first initialization, and during
1834 * any down to up transitions of the bond.
1836 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1838 BOND_AD_INFO(bond).agg_select_timer = timeout;
1839 BOND_AD_INFO(bond).agg_select_mode = bond->params.ad_select;
1842 static u16 aggregator_identifier;
1845 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1846 * @bond: bonding struct to work on
1847 * @tick_resolution: tick duration (millisecond resolution)
1848 * @lacp_fast: boolean. whether fast periodic should be used
1850 * Can be called only after the mac address of the bond is set.
1852 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
1854 // check that the bond is not initialized yet
1855 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr), &(bond->dev->dev_addr))) {
1857 aggregator_identifier = 0;
1859 BOND_AD_INFO(bond).lacp_fast = lacp_fast;
1860 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1861 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1863 // initialize how many times this module is called in one second(should be about every 100ms)
1864 ad_ticks_per_sec = tick_resolution;
1866 bond_3ad_initiate_agg_selection(bond,
1867 AD_AGGREGATOR_SELECTION_TIMER *
1873 * bond_3ad_bind_slave - initialize a slave's port
1874 * @slave: slave struct to work on
1876 * Returns: 0 on success
1879 int bond_3ad_bind_slave(struct slave *slave)
1881 struct bonding *bond = bond_get_bond_by_slave(slave);
1883 struct aggregator *aggregator;
1886 printk(KERN_ERR DRV_NAME ": %s: The slave %s is not attached to its bond\n",
1887 slave->dev->master->name, slave->dev->name);
1891 //check that the slave has not been intialized yet.
1892 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1894 // port initialization
1895 port = &(SLAVE_AD_INFO(slave).port);
1897 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
1899 port->slave = slave;
1900 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1901 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1902 // ------------------------------------------------------------
1903 // Port key : | User key | Speed |Duplex|
1904 // ------------------------------------------------------------
1906 port->actor_admin_port_key = 0; // initialize this parameter
1907 port->actor_admin_port_key |= __get_duplex(port);
1908 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1909 port->actor_oper_port_key = port->actor_admin_port_key;
1910 // if the port is not full duplex, then the port should be not lacp Enabled
1911 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1912 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1914 // actor system is the bond's system
1915 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1916 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1917 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1918 port->aggregator = NULL;
1919 port->next_port_in_aggregator = NULL;
1921 __disable_port(port);
1922 __initialize_port_locks(port);
1925 // aggregator initialization
1926 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1928 ad_initialize_agg(aggregator);
1930 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1931 aggregator->aggregator_identifier = (++aggregator_identifier);
1932 aggregator->slave = slave;
1933 aggregator->is_active = 0;
1934 aggregator->num_of_ports = 0;
1941 * bond_3ad_unbind_slave - deinitialize a slave's port
1942 * @slave: slave struct to work on
1944 * Search for the aggregator that is related to this port, remove the
1945 * aggregator and assign another aggregator for other port related to it
1946 * (if any), and remove the port.
1948 void bond_3ad_unbind_slave(struct slave *slave)
1950 struct port *port, *prev_port, *temp_port;
1951 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1952 int select_new_active_agg = 0;
1954 // find the aggregator related to this slave
1955 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1957 // find the port related to this slave
1958 port = &(SLAVE_AD_INFO(slave).port);
1960 // if slave is null, the whole port is not initialized
1962 printk(KERN_WARNING DRV_NAME ": Warning: %s: Trying to "
1963 "unbind an uninitialized port on %s\n",
1964 slave->dev->master->name, slave->dev->name);
1968 pr_debug("Unbinding Link Aggregation Group %d\n", aggregator->aggregator_identifier);
1970 /* Tell the partner that this port is not suitable for aggregation */
1971 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1972 __update_lacpdu_from_port(port);
1973 ad_lacpdu_send(port);
1975 // check if this aggregator is occupied
1976 if (aggregator->lag_ports) {
1977 // check if there are other ports related to this aggregator except
1978 // the port related to this slave(thats ensure us that there is a
1979 // reason to search for new aggregator, and that we will find one
1980 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
1981 // find new aggregator for the related port(s)
1982 new_aggregator = __get_first_agg(port);
1983 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
1984 // if the new aggregator is empty, or it connected to to our port only
1985 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
1989 // if new aggregator found, copy the aggregator's parameters
1990 // and connect the related lag_ports to the new aggregator
1991 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
1992 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator->aggregator_identifier, new_aggregator->aggregator_identifier);
1994 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
1995 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
1996 aggregator->slave->dev->master->name);
1997 // select new active aggregator
1998 select_new_active_agg = 1;
2001 new_aggregator->is_individual = aggregator->is_individual;
2002 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2003 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2004 new_aggregator->partner_system = aggregator->partner_system;
2005 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2006 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2007 new_aggregator->receive_state = aggregator->receive_state;
2008 new_aggregator->transmit_state = aggregator->transmit_state;
2009 new_aggregator->lag_ports = aggregator->lag_ports;
2010 new_aggregator->is_active = aggregator->is_active;
2011 new_aggregator->num_of_ports = aggregator->num_of_ports;
2013 // update the information that is written on the ports about the aggregator
2014 for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) {
2015 temp_port->aggregator=new_aggregator;
2016 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2019 // clear the aggregator
2020 ad_clear_agg(aggregator);
2022 if (select_new_active_agg) {
2023 ad_agg_selection_logic(__get_first_agg(port));
2026 printk(KERN_WARNING DRV_NAME ": %s: Warning: unbinding aggregator, "
2027 "and could not find a new aggregator for its ports\n",
2028 slave->dev->master->name);
2030 } else { // in case that the only port related to this aggregator is the one we want to remove
2031 select_new_active_agg = aggregator->is_active;
2032 // clear the aggregator
2033 ad_clear_agg(aggregator);
2034 if (select_new_active_agg) {
2035 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2036 slave->dev->master->name);
2037 // select new active aggregator
2038 ad_agg_selection_logic(__get_first_agg(port));
2043 pr_debug("Unbinding port %d\n", port->actor_port_number);
2044 // find the aggregator that this port is connected to
2045 temp_aggregator = __get_first_agg(port);
2046 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2048 // search the port in the aggregator's related ports
2049 for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) {
2050 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2052 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2054 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2056 temp_aggregator->num_of_ports--;
2057 if (temp_aggregator->num_of_ports==0) {
2058 select_new_active_agg = temp_aggregator->is_active;
2059 // clear the aggregator
2060 ad_clear_agg(temp_aggregator);
2061 if (select_new_active_agg) {
2062 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2063 slave->dev->master->name);
2064 // select new active aggregator
2065 ad_agg_selection_logic(__get_first_agg(port));
2076 * bond_3ad_state_machine_handler - handle state machines timeout
2077 * @bond: bonding struct to work on
2079 * The state machine handling concept in this module is to check every tick
2080 * which state machine should operate any function. The execution order is
2081 * round robin, so when we have an interaction between state machines, the
2082 * reply of one to each other might be delayed until next tick.
2084 * This function also complete the initialization when the agg_select_timer
2085 * times out, and it selects an aggregator for the ports that are yet not
2086 * related to any aggregator, and selects the active aggregator for a bond.
2088 void bond_3ad_state_machine_handler(struct work_struct *work)
2090 struct bonding *bond = container_of(work, struct bonding,
2093 struct aggregator *aggregator;
2095 read_lock(&bond->lock);
2097 if (bond->kill_timers) {
2101 //check if there are any slaves
2102 if (bond->slave_cnt == 0) {
2106 // check if agg_select_timer timer after initialize is timed out
2107 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2108 // select the active aggregator for the bond
2109 if ((port = __get_first_port(bond))) {
2111 printk(KERN_WARNING DRV_NAME ": %s: Warning: bond's first port is "
2112 "uninitialized\n", bond->dev->name);
2116 aggregator = __get_first_agg(port);
2117 ad_agg_selection_logic(aggregator);
2119 bond_3ad_set_carrier(bond);
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 queue_delayed_work(bond->wq, &bond->ad_work, 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 pr_debug("Received LACPDU on port %d\n", port->actor_port_number);
2175 ad_rx_machine(lacpdu, port);
2178 case AD_TYPE_MARKER:
2179 // No need to convert fields to Little Endian since we don't use the marker's fields.
2181 switch (((struct bond_marker *)lacpdu)->tlv_type) {
2182 case AD_MARKER_INFORMATION_SUBTYPE:
2183 pr_debug("Received Marker Information on port %d\n", port->actor_port_number);
2184 ad_marker_info_received((struct bond_marker *)lacpdu, port);
2187 case AD_MARKER_RESPONSE_SUBTYPE:
2188 pr_debug("Received Marker Response on port %d\n", port->actor_port_number);
2189 ad_marker_response_received((struct bond_marker *)lacpdu, port);
2193 pr_debug("Received an unknown Marker subtype on slot %d\n", port->actor_port_number);
2200 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2201 * @slave: slave struct to work on
2203 * Handle reselection of aggregator (if needed) for this port.
2205 void bond_3ad_adapter_speed_changed(struct slave *slave)
2209 port = &(SLAVE_AD_INFO(slave).port);
2211 // if slave is null, the whole port is not initialized
2213 printk(KERN_WARNING DRV_NAME ": Warning: %s: speed "
2214 "changed for uninitialized port on %s\n",
2215 slave->dev->master->name, slave->dev->name);
2219 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2220 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2221 pr_debug("Port %d changed speed\n", port->actor_port_number);
2222 // there is no need to reselect a new aggregator, just signal the
2223 // state machines to reinitialize
2224 port->sm_vars |= AD_PORT_BEGIN;
2228 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2229 * @slave: slave struct to work on
2231 * Handle reselection of aggregator (if needed) for this port.
2233 void bond_3ad_adapter_duplex_changed(struct slave *slave)
2237 port=&(SLAVE_AD_INFO(slave).port);
2239 // if slave is null, the whole port is not initialized
2241 printk(KERN_WARNING DRV_NAME ": %s: Warning: duplex changed "
2242 "for uninitialized port on %s\n",
2243 slave->dev->master->name, slave->dev->name);
2247 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2248 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2249 pr_debug("Port %d changed duplex\n", port->actor_port_number);
2250 // there is no need to reselect a new aggregator, just signal the
2251 // state machines to reinitialize
2252 port->sm_vars |= AD_PORT_BEGIN;
2256 * bond_3ad_handle_link_change - handle a slave's link status change indication
2257 * @slave: slave struct to work on
2258 * @status: whether the link is now up or down
2260 * Handle reselection of aggregator (if needed) for this port.
2262 void bond_3ad_handle_link_change(struct slave *slave, char link)
2266 port = &(SLAVE_AD_INFO(slave).port);
2268 // if slave is null, the whole port is not initialized
2270 printk(KERN_WARNING DRV_NAME ": Warning: %s: link status changed for "
2271 "uninitialized port on %s\n",
2272 slave->dev->master->name, slave->dev->name);
2276 // 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)
2277 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2278 if (link == BOND_LINK_UP) {
2279 port->is_enabled = true;
2280 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2281 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2282 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2283 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2285 /* link has failed */
2286 port->is_enabled = false;
2287 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2288 port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS);
2290 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2291 // there is no need to reselect a new aggregator, just signal the
2292 // state machines to reinitialize
2293 port->sm_vars |= AD_PORT_BEGIN;
2297 * set link state for bonding master: if we have an active
2298 * aggregator, we're up, if not, we're down. Presumes that we cannot
2299 * have an active aggregator if there are no slaves with link up.
2301 * This behavior complies with IEEE 802.3 section 43.3.9.
2303 * Called by bond_set_carrier(). Return zero if carrier state does not
2304 * change, nonzero if it does.
2306 int bond_3ad_set_carrier(struct bonding *bond)
2308 if (__get_active_agg(&(SLAVE_AD_INFO(bond->first_slave).aggregator))) {
2309 if (!netif_carrier_ok(bond->dev)) {
2310 netif_carrier_on(bond->dev);
2316 if (netif_carrier_ok(bond->dev)) {
2317 netif_carrier_off(bond->dev);
2324 * bond_3ad_get_active_agg_info - get information of the active aggregator
2325 * @bond: bonding struct to work on
2326 * @ad_info: ad_info struct to fill with the bond's info
2328 * Returns: 0 on success
2331 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2333 struct aggregator *aggregator = NULL;
2336 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2337 if (port->aggregator && port->aggregator->is_active) {
2338 aggregator = port->aggregator;
2344 ad_info->aggregator_id = aggregator->aggregator_identifier;
2345 ad_info->ports = aggregator->num_of_ports;
2346 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2347 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2348 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2355 int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2357 struct slave *slave, *start_at;
2358 struct bonding *bond = netdev_priv(dev);
2363 struct ad_info ad_info;
2366 /* make sure that the slaves list will
2367 * not change during tx
2369 read_lock(&bond->lock);
2371 if (!BOND_IS_OK(bond)) {
2375 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
2376 printk(KERN_DEBUG DRV_NAME ": %s: Error: "
2377 "bond_3ad_get_active_agg_info failed\n", dev->name);
2381 slaves_in_agg = ad_info.ports;
2382 agg_id = ad_info.aggregator_id;
2384 if (slaves_in_agg == 0) {
2385 /*the aggregator is empty*/
2386 printk(KERN_DEBUG DRV_NAME ": %s: Error: active "
2387 "aggregator is empty\n",
2392 slave_agg_no = bond->xmit_hash_policy(skb, dev, slaves_in_agg);
2394 bond_for_each_slave(bond, slave, i) {
2395 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2397 if (agg && (agg->aggregator_identifier == agg_id)) {
2399 if (slave_agg_no < 0) {
2405 if (slave_agg_no >= 0) {
2406 printk(KERN_ERR DRV_NAME ": %s: Error: Couldn't find a slave to tx on "
2407 "for aggregator ID %d\n", dev->name, agg_id);
2413 bond_for_each_slave_from(bond, slave, i, start_at) {
2414 int slave_agg_id = 0;
2415 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2418 slave_agg_id = agg->aggregator_identifier;
2421 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2422 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2429 /* no suitable interface, frame not sent */
2432 read_unlock(&bond->lock);
2436 int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype, struct net_device *orig_dev)
2438 struct bonding *bond = netdev_priv(dev);
2439 struct slave *slave = NULL;
2440 int ret = NET_RX_DROP;
2442 if (dev_net(dev) != &init_net)
2445 if (!(dev->flags & IFF_MASTER))
2448 read_lock(&bond->lock);
2449 slave = bond_get_slave_by_dev((struct bonding *)netdev_priv(dev),
2454 bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
2456 ret = NET_RX_SUCCESS;
2459 read_unlock(&bond->lock);