bonding: use port_params in __record_pdu()
[linux-2.6/mini2440.git] / drivers / net / bonding / bond_3ad.c
blobb789dcb776269a05e45e7ec7d03b7214c4f9d714
1 /*
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)
7 * any later version.
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
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #include <linux/skbuff.h>
24 #include <linux/if_ether.h>
25 #include <linux/netdevice.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_bonding.h>
30 #include <linux/pkt_sched.h>
31 #include <net/net_namespace.h>
32 #include "bonding.h"
33 #include "bond_3ad.h"
35 // General definitions
36 #define AD_SHORT_TIMEOUT 1
37 #define AD_LONG_TIMEOUT 0
38 #define AD_STANDBY 0x2
39 #define AD_MAX_TX_IN_SECOND 3
40 #define AD_COLLECTOR_MAX_DELAY 0
42 // Timer definitions(43.4.4 in the 802.3ad standard)
43 #define AD_FAST_PERIODIC_TIME 1
44 #define AD_SLOW_PERIODIC_TIME 30
45 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
46 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
47 #define AD_CHURN_DETECTION_TIME 60
48 #define AD_AGGREGATE_WAIT_TIME 2
50 // Port state definitions(43.4.2.2 in the 802.3ad standard)
51 #define AD_STATE_LACP_ACTIVITY 0x1
52 #define AD_STATE_LACP_TIMEOUT 0x2
53 #define AD_STATE_AGGREGATION 0x4
54 #define AD_STATE_SYNCHRONIZATION 0x8
55 #define AD_STATE_COLLECTING 0x10
56 #define AD_STATE_DISTRIBUTING 0x20
57 #define AD_STATE_DEFAULTED 0x40
58 #define AD_STATE_EXPIRED 0x80
60 // Port Variables definitions used by the State Machines(43.4.7 in the 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 // ------------------------------------------------------------
78 // 16 6 1 0
79 #define AD_DUPLEX_KEY_BITS 0x1
80 #define AD_SPEED_KEY_BITS 0x3E
81 #define AD_USER_KEY_BITS 0xFFC0
83 //dalloun
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
89 //endalloun
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 // ================= 3AD api to bonding and kernel code ==================
99 static u16 __get_link_speed(struct port *port);
100 static u8 __get_duplex(struct port *port);
101 static inline void __initialize_port_locks(struct port *port);
102 //conversions
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 bond_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 bond_marker *marker_info, struct port *port);
142 static void ad_marker_response_received(struct bond_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) {
158 return 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) {
173 return NULL;
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)) {
193 return NULL;
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
204 * found.
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)) {
212 return NULL;
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)) {
232 return NULL;
235 return &(SLAVE_AD_INFO(slave->next).aggregator);
239 * __agg_has_partner
241 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
242 * address for the partner). Return 0 if not.
244 static inline int __agg_has_partner(struct aggregator *agg)
246 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
250 * __disable_port - disable the port's slave
251 * @port: the port we're looking at
254 static inline void __disable_port(struct port *port)
256 bond_set_slave_inactive_flags(port->slave);
260 * __enable_port - enable the port's slave, if it's up
261 * @port: the port we're looking at
264 static inline void __enable_port(struct port *port)
266 struct slave *slave = port->slave;
268 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev)) {
269 bond_set_slave_active_flags(slave);
274 * __port_is_enabled - check if the port's slave is in active state
275 * @port: the port we're looking at
278 static inline int __port_is_enabled(struct port *port)
280 return(port->slave->state == BOND_STATE_ACTIVE);
284 * __get_agg_selection_mode - get the aggregator selection mode
285 * @port: the port we're looking at
287 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
289 static inline u32 __get_agg_selection_mode(struct port *port)
291 struct bonding *bond = __get_bond_by_port(port);
293 if (bond == NULL) {
294 return BOND_AD_STABLE;
297 return BOND_AD_INFO(bond).agg_select_mode;
301 * __check_agg_selection_timer - check if the selection timer has expired
302 * @port: the port we're looking at
305 static inline int __check_agg_selection_timer(struct port *port)
307 struct bonding *bond = __get_bond_by_port(port);
309 if (bond == NULL) {
310 return 0;
313 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
317 * __get_rx_machine_lock - lock the port's RX machine
318 * @port: the port we're looking at
321 static inline void __get_rx_machine_lock(struct port *port)
323 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
327 * __release_rx_machine_lock - unlock the port's RX machine
328 * @port: the port we're looking at
331 static inline void __release_rx_machine_lock(struct port *port)
333 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
337 * __get_link_speed - get a port's speed
338 * @port: the port we're looking at
340 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
341 * 0,
342 * %AD_LINK_SPEED_BITMASK_10MBPS,
343 * %AD_LINK_SPEED_BITMASK_100MBPS,
344 * %AD_LINK_SPEED_BITMASK_1000MBPS,
345 * %AD_LINK_SPEED_BITMASK_10000MBPS
347 static u16 __get_link_speed(struct port *port)
349 struct slave *slave = port->slave;
350 u16 speed;
352 /* this if covers only a special case: when the configuration starts with
353 * link down, it sets the speed to 0.
354 * This is done in spite of the fact that the e100 driver reports 0 to be
355 * compatible with MVT in the future.*/
356 if (slave->link != BOND_LINK_UP) {
357 speed=0;
358 } else {
359 switch (slave->speed) {
360 case SPEED_10:
361 speed = AD_LINK_SPEED_BITMASK_10MBPS;
362 break;
364 case SPEED_100:
365 speed = AD_LINK_SPEED_BITMASK_100MBPS;
366 break;
368 case SPEED_1000:
369 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
370 break;
372 case SPEED_10000:
373 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
374 break;
376 default:
377 speed = 0; // unknown speed value from ethtool. shouldn't happen
378 break;
382 pr_debug("Port %d Received link speed %d update from adapter\n", port->actor_port_number, speed);
383 return speed;
387 * __get_duplex - get a port's duplex
388 * @port: the port we're looking at
390 * Return @port's duplex in 802.3ad bitmask format. i.e.:
391 * 0x01 if in full duplex
392 * 0x00 otherwise
394 static u8 __get_duplex(struct port *port)
396 struct slave *slave = port->slave;
398 u8 retval;
400 // handling a special case: when the configuration starts with
401 // link down, it sets the duplex to 0.
402 if (slave->link != BOND_LINK_UP) {
403 retval=0x0;
404 } else {
405 switch (slave->duplex) {
406 case DUPLEX_FULL:
407 retval=0x1;
408 pr_debug("Port %d Received status full duplex update from adapter\n", port->actor_port_number);
409 break;
410 case DUPLEX_HALF:
411 default:
412 retval=0x0;
413 pr_debug("Port %d Received status NOT full duplex update from adapter\n", port->actor_port_number);
414 break;
417 return retval;
421 * __initialize_port_locks - initialize a port's RX machine spinlock
422 * @port: the port we're looking at
425 static inline void __initialize_port_locks(struct port *port)
427 // make sure it isn't called twice
428 spin_lock_init(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
431 //conversions
434 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
435 * @timer_type: which timer to operate
436 * @par: timer parameter. see below
438 * If @timer_type is %current_while_timer, @par indicates long/short timer.
439 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
440 * %SLOW_PERIODIC_TIME.
442 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
444 u16 retval=0; //to silence the compiler
446 switch (timer_type) {
447 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
448 if (par) { // for short or long timeout
449 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
450 } else {
451 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
453 break;
454 case AD_ACTOR_CHURN_TIMER: // for local churn machine
455 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
456 break;
457 case AD_PERIODIC_TIMER: // for periodic machine
458 retval = (par*ad_ticks_per_sec); // long timeout
459 break;
460 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
461 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
462 break;
463 case AD_WAIT_WHILE_TIMER: // for selection machine
464 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
465 break;
467 return retval;
471 /////////////////////////////////////////////////////////////////////////////////
472 // ================= ad_rx_machine helper functions ==================
473 /////////////////////////////////////////////////////////////////////////////////
476 * __record_pdu - record parameters from a received lacpdu
477 * @lacpdu: the lacpdu we've received
478 * @port: the port we're looking at
480 * Record the parameter values for the Actor carried in a received lacpdu as
481 * the current partner operational parameter values and sets
482 * actor_oper_port_state.defaulted to FALSE.
484 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
486 if (lacpdu && port) {
487 struct port_params *partner = &port->partner_oper;
489 // record the new parameter values for the partner operational
490 partner->port_number = ntohs(lacpdu->actor_port);
491 partner->port_priority = ntohs(lacpdu->actor_port_priority);
492 partner->system = lacpdu->actor_system;
493 partner->system_priority = ntohs(lacpdu->actor_system_priority);
494 partner->key = ntohs(lacpdu->actor_key);
495 partner->port_state = lacpdu->actor_state;
497 // set actor_oper_port_state.defaulted to FALSE
498 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
500 // set the partner sync. to on if the partner is sync. and the port is matched
501 if ((port->sm_vars & AD_PORT_MATCHED) && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
502 partner->port_state |= AD_STATE_SYNCHRONIZATION;
503 } else {
504 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
510 * __record_default - record default parameters
511 * @port: the port we're looking at
513 * This function records the default parameter values for the partner carried
514 * in the Partner Admin parameters as the current partner operational parameter
515 * values and sets actor_oper_port_state.defaulted to TRUE.
517 static void __record_default(struct port *port)
519 // validate the port
520 if (port) {
521 // record the partner admin parameters
522 port->partner_oper.port_number = port->partner_admin.port_number;
523 port->partner_oper.port_priority = port->partner_admin.port_priority;
524 port->partner_oper.system = port->partner_admin.system;
525 port->partner_oper.system_priority = port->partner_admin.system_priority;
526 port->partner_oper.key = port->partner_admin.key;
527 port->partner_oper.port_state = port->partner_admin.port_state;
529 // set actor_oper_port_state.defaulted to true
530 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
535 * __update_selected - update a port's Selected variable from a received lacpdu
536 * @lacpdu: the lacpdu we've received
537 * @port: the port we're looking at
539 * Update the value of the selected variable, using parameter values from a
540 * newly received lacpdu. The parameter values for the Actor carried in the
541 * received PDU are compared with the corresponding operational parameter
542 * values for the ports partner. If one or more of the comparisons shows that
543 * the value(s) received in the PDU differ from the current operational values,
544 * then selected is set to FALSE and actor_oper_port_state.synchronization is
545 * set to out_of_sync. Otherwise, selected remains unchanged.
547 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
549 // validate lacpdu and port
550 if (lacpdu && port) {
551 // check if any parameter is different
552 if ((ntohs(lacpdu->actor_port) != port->partner_oper.port_number) ||
553 (ntohs(lacpdu->actor_port_priority) != port->partner_oper.port_priority) ||
554 MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->partner_oper.system)) ||
555 (ntohs(lacpdu->actor_system_priority) != port->partner_oper.system_priority) ||
556 (ntohs(lacpdu->actor_key) != port->partner_oper.key) ||
557 ((lacpdu->actor_state & AD_STATE_AGGREGATION) != (port->partner_oper.port_state & AD_STATE_AGGREGATION))
559 // update the state machine Selected variable
560 port->sm_vars &= ~AD_PORT_SELECTED;
566 * __update_default_selected - update a port's Selected variable from Partner
567 * @port: the port we're looking at
569 * This function updates the value of the selected variable, using the partner
570 * administrative parameter values. The administrative values are compared with
571 * the corresponding operational parameter values for the partner. If one or
572 * more of the comparisons shows that the administrative value(s) differ from
573 * the current operational values, then Selected is set to FALSE and
574 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
575 * Selected remains unchanged.
577 static void __update_default_selected(struct port *port)
579 // validate the port
580 if (port) {
581 // check if any parameter is different
582 if ((port->partner_admin.port_number != port->partner_oper.port_number) ||
583 (port->partner_admin.port_priority != port->partner_oper.port_priority) ||
584 MAC_ADDRESS_COMPARE(&(port->partner_admin.system), &(port->partner_oper.system)) ||
585 (port->partner_admin.system_priority != port->partner_oper.system_priority) ||
586 (port->partner_admin.key != port->partner_oper.key) ||
587 ((port->partner_admin.port_state & AD_STATE_AGGREGATION) != (port->partner_oper.port_state & AD_STATE_AGGREGATION))
589 // update the state machine Selected variable
590 port->sm_vars &= ~AD_PORT_SELECTED;
596 * __choose_matched - update a port's matched variable from a received lacpdu
597 * @lacpdu: the lacpdu we've received
598 * @port: the port we're looking at
600 * Update the value of the matched variable, using parameter values from a
601 * newly received lacpdu. Parameter values for the partner carried in the
602 * received PDU are compared with the corresponding operational parameter
603 * values for the actor. Matched is set to TRUE if all of these parameters
604 * match and the PDU parameter partner_state.aggregation has the same value as
605 * actor_oper_port_state.aggregation and lacp will actively maintain the link
606 * in the aggregation. Matched is also set to TRUE if the value of
607 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
608 * an individual link and lacp will actively maintain the link. Otherwise,
609 * matched is set to FALSE. LACP is considered to be actively maintaining the
610 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
611 * the actor's actor_oper_port_state.lacp_activity and the PDU's
612 * partner_state.lacp_activity variables are TRUE.
614 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
616 // validate lacpdu and port
617 if (lacpdu && port) {
618 // check if all parameters are alike
619 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
620 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
621 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
622 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
623 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
624 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
625 // or this is individual link(aggregation == FALSE)
626 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
628 // update the state machine Matched variable
629 port->sm_vars |= AD_PORT_MATCHED;
630 } else {
631 port->sm_vars &= ~AD_PORT_MATCHED;
637 * __update_ntt - update a port's ntt variable from a received lacpdu
638 * @lacpdu: the lacpdu we've received
639 * @port: the port we're looking at
641 * Updates the value of the ntt variable, using parameter values from a newly
642 * received lacpdu. The parameter values for the partner carried in the
643 * received PDU are compared with the corresponding operational parameter
644 * values for the Actor. If one or more of the comparisons shows that the
645 * value(s) received in the PDU differ from the current operational values,
646 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
648 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
650 // validate lacpdu and port
651 if (lacpdu && port) {
652 // check if any parameter is different
653 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
654 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
655 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
656 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
657 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
658 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
659 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
660 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
661 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
663 // set ntt to be TRUE
664 port->ntt = 1;
670 * __attach_bond_to_agg
671 * @port: the port we're looking at
673 * Handle the attaching of the port's control parser/multiplexer and the
674 * aggregator. This function does nothing since the parser/multiplexer of the
675 * receive and the parser/multiplexer of the aggregator are already combined.
677 static void __attach_bond_to_agg(struct port *port)
679 port=NULL; // just to satisfy the compiler
680 // This function does nothing since the parser/multiplexer of the receive
681 // and the parser/multiplexer of the aggregator are already combined
685 * __detach_bond_from_agg
686 * @port: the port we're looking at
688 * Handle the detaching of the port's control parser/multiplexer from the
689 * aggregator. This function does nothing since the parser/multiplexer of the
690 * receive and the parser/multiplexer of the aggregator are already combined.
692 static void __detach_bond_from_agg(struct port *port)
694 port=NULL; // just to satisfy the compiler
695 // This function does nothing sience the parser/multiplexer of the receive
696 // and the parser/multiplexer of the aggregator are already combined
700 * __agg_ports_are_ready - check if all ports in an aggregator are ready
701 * @aggregator: the aggregator we're looking at
704 static int __agg_ports_are_ready(struct aggregator *aggregator)
706 struct port *port;
707 int retval = 1;
709 if (aggregator) {
710 // scan all ports in this aggregator to verfy if they are all ready
711 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
712 if (!(port->sm_vars & AD_PORT_READY_N)) {
713 retval = 0;
714 break;
719 return retval;
723 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
724 * @aggregator: the aggregator we're looking at
725 * @val: Should the ports' ready bit be set on or off
728 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
730 struct port *port;
732 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
733 if (val) {
734 port->sm_vars |= AD_PORT_READY;
735 } else {
736 port->sm_vars &= ~AD_PORT_READY;
742 * __get_agg_bandwidth - get the total bandwidth of an aggregator
743 * @aggregator: the aggregator we're looking at
746 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
748 u32 bandwidth=0;
749 u32 basic_speed;
751 if (aggregator->num_of_ports) {
752 basic_speed = __get_link_speed(aggregator->lag_ports);
753 switch (basic_speed) {
754 case AD_LINK_SPEED_BITMASK_1MBPS:
755 bandwidth = aggregator->num_of_ports;
756 break;
757 case AD_LINK_SPEED_BITMASK_10MBPS:
758 bandwidth = aggregator->num_of_ports * 10;
759 break;
760 case AD_LINK_SPEED_BITMASK_100MBPS:
761 bandwidth = aggregator->num_of_ports * 100;
762 break;
763 case AD_LINK_SPEED_BITMASK_1000MBPS:
764 bandwidth = aggregator->num_of_ports * 1000;
765 break;
766 case AD_LINK_SPEED_BITMASK_10000MBPS:
767 bandwidth = aggregator->num_of_ports * 10000;
768 break;
769 default:
770 bandwidth=0; // to silent the compilor ....
773 return bandwidth;
777 * __get_active_agg - get the current active aggregator
778 * @aggregator: the aggregator we're looking at
781 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
783 struct aggregator *retval = NULL;
785 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
786 if (aggregator->is_active) {
787 retval = aggregator;
788 break;
792 return retval;
796 * __update_lacpdu_from_port - update a port's lacpdu fields
797 * @port: the port we're looking at
800 static inline void __update_lacpdu_from_port(struct port *port)
802 struct lacpdu *lacpdu = &port->lacpdu;
804 /* update current actual Actor parameters */
805 /* lacpdu->subtype initialized
806 * lacpdu->version_number initialized
807 * lacpdu->tlv_type_actor_info initialized
808 * lacpdu->actor_information_length initialized
811 lacpdu->actor_system_priority = htons(port->actor_system_priority);
812 lacpdu->actor_system = port->actor_system;
813 lacpdu->actor_key = htons(port->actor_oper_port_key);
814 lacpdu->actor_port_priority = htons(port->actor_port_priority);
815 lacpdu->actor_port = htons(port->actor_port_number);
816 lacpdu->actor_state = port->actor_oper_port_state;
818 /* lacpdu->reserved_3_1 initialized
819 * lacpdu->tlv_type_partner_info initialized
820 * lacpdu->partner_information_length initialized
823 lacpdu->partner_system_priority = htons(port->partner_oper.system_priority);
824 lacpdu->partner_system = port->partner_oper.system;
825 lacpdu->partner_key = htons(port->partner_oper.key);
826 lacpdu->partner_port_priority = htons(port->partner_oper.port_priority);
827 lacpdu->partner_port = htons(port->partner_oper.port_number);
828 lacpdu->partner_state = port->partner_oper.port_state;
830 /* lacpdu->reserved_3_2 initialized
831 * lacpdu->tlv_type_collector_info initialized
832 * lacpdu->collector_information_length initialized
833 * collector_max_delay initialized
834 * reserved_12[12] initialized
835 * tlv_type_terminator initialized
836 * terminator_length initialized
837 * reserved_50[50] initialized
841 //////////////////////////////////////////////////////////////////////////////////////
842 // ================= main 802.3ad protocol code ======================================
843 //////////////////////////////////////////////////////////////////////////////////////
846 * ad_lacpdu_send - send out a lacpdu packet on a given port
847 * @port: the port we're looking at
849 * Returns: 0 on success
850 * < 0 on error
852 static int ad_lacpdu_send(struct port *port)
854 struct slave *slave = port->slave;
855 struct sk_buff *skb;
856 struct lacpdu_header *lacpdu_header;
857 int length = sizeof(struct lacpdu_header);
858 struct mac_addr lacpdu_multicast_address = AD_MULTICAST_LACPDU_ADDR;
860 skb = dev_alloc_skb(length);
861 if (!skb) {
862 return -ENOMEM;
865 skb->dev = slave->dev;
866 skb_reset_mac_header(skb);
867 skb->network_header = skb->mac_header + ETH_HLEN;
868 skb->protocol = PKT_TYPE_LACPDU;
869 skb->priority = TC_PRIO_CONTROL;
871 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
873 lacpdu_header->ad_header.destination_address = lacpdu_multicast_address;
874 /* Note: source addres is set to be the member's PERMANENT address, because we use it
875 to identify loopback lacpdus in receive. */
876 lacpdu_header->ad_header.source_address = *((struct mac_addr *)(slave->perm_hwaddr));
877 lacpdu_header->ad_header.length_type = PKT_TYPE_LACPDU;
879 lacpdu_header->lacpdu = port->lacpdu; // struct copy
881 dev_queue_xmit(skb);
883 return 0;
887 * ad_marker_send - send marker information/response on a given port
888 * @port: the port we're looking at
889 * @marker: marker data to send
891 * Returns: 0 on success
892 * < 0 on error
894 static int ad_marker_send(struct port *port, struct bond_marker *marker)
896 struct slave *slave = port->slave;
897 struct sk_buff *skb;
898 struct bond_marker_header *marker_header;
899 int length = sizeof(struct bond_marker_header);
900 struct mac_addr lacpdu_multicast_address = AD_MULTICAST_LACPDU_ADDR;
902 skb = dev_alloc_skb(length + 16);
903 if (!skb) {
904 return -ENOMEM;
907 skb_reserve(skb, 16);
909 skb->dev = slave->dev;
910 skb_reset_mac_header(skb);
911 skb->network_header = skb->mac_header + ETH_HLEN;
912 skb->protocol = PKT_TYPE_LACPDU;
914 marker_header = (struct bond_marker_header *)skb_put(skb, length);
916 marker_header->ad_header.destination_address = lacpdu_multicast_address;
917 /* Note: source addres is set to be the member's PERMANENT address, because we use it
918 to identify loopback MARKERs in receive. */
919 marker_header->ad_header.source_address = *((struct mac_addr *)(slave->perm_hwaddr));
920 marker_header->ad_header.length_type = PKT_TYPE_LACPDU;
922 marker_header->marker = *marker; // struct copy
924 dev_queue_xmit(skb);
926 return 0;
930 * ad_mux_machine - handle a port's mux state machine
931 * @port: the port we're looking at
934 static void ad_mux_machine(struct port *port)
936 mux_states_t last_state;
938 // keep current State Machine state to compare later if it was changed
939 last_state = port->sm_mux_state;
941 if (port->sm_vars & AD_PORT_BEGIN) {
942 port->sm_mux_state = AD_MUX_DETACHED; // next state
943 } else {
944 switch (port->sm_mux_state) {
945 case AD_MUX_DETACHED:
946 if ((port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if SELECTED or STANDBY
947 port->sm_mux_state = AD_MUX_WAITING; // next state
949 break;
950 case AD_MUX_WAITING:
951 // if SELECTED == FALSE return to DETACH state
952 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
953 port->sm_vars &= ~AD_PORT_READY_N;
954 // in order to withhold the Selection Logic to check all ports READY_N value
955 // every callback cycle to update ready variable, we check READY_N and update READY here
956 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
957 port->sm_mux_state = AD_MUX_DETACHED; // next state
958 break;
961 // check if the wait_while_timer expired
962 if (port->sm_mux_timer_counter && !(--port->sm_mux_timer_counter)) {
963 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));
970 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
971 if ((port->sm_vars & AD_PORT_READY) && !port->sm_mux_timer_counter) {
972 port->sm_mux_state = AD_MUX_ATTACHED; // next state
974 break;
975 case AD_MUX_ATTACHED:
976 // check also if agg_select_timer expired(so the edable port will take place only after this timer)
977 if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) {
978 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
979 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
980 port->sm_vars &= ~AD_PORT_READY_N;
981 // in order to withhold the selection logic to check all ports READY_N value
982 // every callback cycle to update ready variable, we check READY_N and update READY here
983 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
984 port->sm_mux_state = AD_MUX_DETACHED;// next state
986 break;
987 case AD_MUX_COLLECTING_DISTRIBUTING:
988 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
989 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
991 port->sm_mux_state = AD_MUX_ATTACHED;// next state
993 } else {
994 // if port state hasn't changed make
995 // sure that a collecting distributing
996 // port in an active aggregator is enabled
997 if (port->aggregator &&
998 port->aggregator->is_active &&
999 !__port_is_enabled(port)) {
1001 __enable_port(port);
1004 break;
1005 default: //to silence the compiler
1006 break;
1010 // check if the state machine was changed
1011 if (port->sm_mux_state != last_state) {
1012 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_mux_state);
1013 switch (port->sm_mux_state) {
1014 case AD_MUX_DETACHED:
1015 __detach_bond_from_agg(port);
1016 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
1017 ad_disable_collecting_distributing(port);
1018 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1019 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1020 port->ntt = 1;
1021 break;
1022 case AD_MUX_WAITING:
1023 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1024 break;
1025 case AD_MUX_ATTACHED:
1026 __attach_bond_to_agg(port);
1027 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
1028 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1029 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1030 ad_disable_collecting_distributing(port);
1031 port->ntt = 1;
1032 break;
1033 case AD_MUX_COLLECTING_DISTRIBUTING:
1034 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1035 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
1036 ad_enable_collecting_distributing(port);
1037 port->ntt = 1;
1038 break;
1039 default: //to silence the compiler
1040 break;
1046 * ad_rx_machine - handle a port's rx State Machine
1047 * @lacpdu: the lacpdu we've received
1048 * @port: the port we're looking at
1050 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1051 * CURRENT. If timer expired set the state machine in the proper state.
1052 * In other cases, this function checks if we need to switch to other state.
1054 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1056 rx_states_t last_state;
1058 // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback)
1059 __get_rx_machine_lock(port);
1061 // keep current State Machine state to compare later if it was changed
1062 last_state = port->sm_rx_state;
1064 // check if state machine should change state
1065 // first, check if port was reinitialized
1066 if (port->sm_vars & AD_PORT_BEGIN) {
1067 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1069 // check if port is not enabled
1070 else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED)) {
1071 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1073 // check if new lacpdu arrived
1074 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1075 port->sm_rx_timer_counter = 0; // zero timer
1076 port->sm_rx_state = AD_RX_CURRENT;
1077 } else {
1078 // if timer is on, and if it is expired
1079 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1080 switch (port->sm_rx_state) {
1081 case AD_RX_EXPIRED:
1082 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1083 break;
1084 case AD_RX_CURRENT:
1085 port->sm_rx_state = AD_RX_EXPIRED; // next state
1086 break;
1087 default: //to silence the compiler
1088 break;
1090 } else {
1091 // if no lacpdu arrived and no timer is on
1092 switch (port->sm_rx_state) {
1093 case AD_RX_PORT_DISABLED:
1094 if (port->sm_vars & AD_PORT_MOVED) {
1095 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1096 } else if (port->is_enabled && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1097 port->sm_rx_state = AD_RX_EXPIRED; // next state
1098 } else if (port->is_enabled && ((port->sm_vars & AD_PORT_LACP_ENABLED) == 0)) {
1099 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
1101 break;
1102 default: //to silence the compiler
1103 break;
1109 // check if the State machine was changed or new lacpdu arrived
1110 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1111 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_rx_state);
1112 switch (port->sm_rx_state) {
1113 case AD_RX_INITIALIZE:
1114 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1115 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1116 } else {
1117 port->sm_vars |= AD_PORT_LACP_ENABLED;
1119 port->sm_vars &= ~AD_PORT_SELECTED;
1120 __record_default(port);
1121 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1122 port->sm_vars &= ~AD_PORT_MOVED;
1123 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1125 /*- Fall Through -*/
1127 case AD_RX_PORT_DISABLED:
1128 port->sm_vars &= ~AD_PORT_MATCHED;
1129 break;
1130 case AD_RX_LACP_DISABLED:
1131 port->sm_vars &= ~AD_PORT_SELECTED;
1132 __record_default(port);
1133 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1134 port->sm_vars |= AD_PORT_MATCHED;
1135 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1136 break;
1137 case AD_RX_EXPIRED:
1138 //Reset of the Synchronization flag. (Standard 43.4.12)
1139 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1140 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
1141 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1142 port->sm_vars &= ~AD_PORT_MATCHED;
1143 port->partner_oper.port_state |= AD_SHORT_TIMEOUT;
1144 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1145 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1146 break;
1147 case AD_RX_DEFAULTED:
1148 __update_default_selected(port);
1149 __record_default(port);
1150 port->sm_vars |= AD_PORT_MATCHED;
1151 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1152 break;
1153 case AD_RX_CURRENT:
1154 // detect loopback situation
1155 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1156 // INFO_RECEIVED_LOOPBACK_FRAMES
1157 printk(KERN_ERR DRV_NAME ": %s: An illegal loopback occurred on "
1158 "adapter (%s). Check the configuration to verify that all "
1159 "Adapters are connected to 802.3ad compliant switch ports\n",
1160 port->slave->dev->master->name, port->slave->dev->name);
1161 __release_rx_machine_lock(port);
1162 return;
1164 __update_selected(lacpdu, port);
1165 __update_ntt(lacpdu, port);
1166 __record_pdu(lacpdu, port);
1167 __choose_matched(lacpdu, port);
1168 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1169 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1170 // verify that if the aggregator is enabled, the port is enabled too.
1171 //(because if the link goes down for a short time, the 802.3ad will not
1172 // catch it, and the port will continue to be disabled)
1173 if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) {
1174 __enable_port(port);
1176 break;
1177 default: //to silence the compiler
1178 break;
1181 __release_rx_machine_lock(port);
1185 * ad_tx_machine - handle a port's tx state machine
1186 * @port: the port we're looking at
1189 static void ad_tx_machine(struct port *port)
1191 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1192 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1193 // check if there is something to send
1194 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1195 __update_lacpdu_from_port(port);
1196 // send the lacpdu
1197 if (ad_lacpdu_send(port) >= 0) {
1198 pr_debug("Sent LACPDU on port %d\n", port->actor_port_number);
1199 // mark ntt as false, so it will not be sent again until demanded
1200 port->ntt = 0;
1203 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
1204 port->sm_tx_timer_counter=ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1209 * ad_periodic_machine - handle a port's periodic state machine
1210 * @port: the port we're looking at
1212 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1214 static void ad_periodic_machine(struct port *port)
1216 periodic_states_t last_state;
1218 // keep current state machine state to compare later if it was changed
1219 last_state = port->sm_periodic_state;
1221 // check if port was reinitialized
1222 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1223 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1225 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1227 // check if state machine should change state
1228 else if (port->sm_periodic_timer_counter) {
1229 // check if periodic state machine expired
1230 if (!(--port->sm_periodic_timer_counter)) {
1231 // if expired then do tx
1232 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1233 } else {
1234 // If not expired, check if there is some new timeout parameter from the partner state
1235 switch (port->sm_periodic_state) {
1236 case AD_FAST_PERIODIC:
1237 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1238 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1240 break;
1241 case AD_SLOW_PERIODIC:
1242 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1243 // stop current timer
1244 port->sm_periodic_timer_counter = 0;
1245 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1247 break;
1248 default: //to silence the compiler
1249 break;
1252 } else {
1253 switch (port->sm_periodic_state) {
1254 case AD_NO_PERIODIC:
1255 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1256 break;
1257 case AD_PERIODIC_TX:
1258 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1259 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1260 } else {
1261 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1263 break;
1264 default: //to silence the compiler
1265 break;
1269 // check if the state machine was changed
1270 if (port->sm_periodic_state != last_state) {
1271 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_periodic_state);
1272 switch (port->sm_periodic_state) {
1273 case AD_NO_PERIODIC:
1274 port->sm_periodic_timer_counter = 0; // zero timer
1275 break;
1276 case AD_FAST_PERIODIC:
1277 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
1278 break;
1279 case AD_SLOW_PERIODIC:
1280 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
1281 break;
1282 case AD_PERIODIC_TX:
1283 port->ntt = 1;
1284 break;
1285 default: //to silence the compiler
1286 break;
1292 * ad_port_selection_logic - select aggregation groups
1293 * @port: the port we're looking at
1295 * Select aggregation groups, and assign each port for it's aggregetor. The
1296 * selection logic is called in the inititalization (after all the handshkes),
1297 * and after every lacpdu receive (if selected is off).
1299 static void ad_port_selection_logic(struct port *port)
1301 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1302 struct port *last_port = NULL, *curr_port;
1303 int found = 0;
1305 // if the port is already Selected, do nothing
1306 if (port->sm_vars & AD_PORT_SELECTED) {
1307 return;
1310 // if the port is connected to other aggregator, detach it
1311 if (port->aggregator) {
1312 // detach the port from its former aggregator
1313 temp_aggregator=port->aggregator;
1314 for (curr_port=temp_aggregator->lag_ports; curr_port; last_port=curr_port, curr_port=curr_port->next_port_in_aggregator) {
1315 if (curr_port == port) {
1316 temp_aggregator->num_of_ports--;
1317 if (!last_port) {// if it is the first port attached to the aggregator
1318 temp_aggregator->lag_ports=port->next_port_in_aggregator;
1319 } else {// not the first port attached to the aggregator
1320 last_port->next_port_in_aggregator=port->next_port_in_aggregator;
1323 // clear the port's relations to this aggregator
1324 port->aggregator = NULL;
1325 port->next_port_in_aggregator=NULL;
1326 port->actor_port_aggregator_identifier=0;
1328 pr_debug("Port %d left LAG %d\n", port->actor_port_number, temp_aggregator->aggregator_identifier);
1329 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1330 if (!temp_aggregator->lag_ports) {
1331 ad_clear_agg(temp_aggregator);
1333 break;
1336 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
1337 printk(KERN_WARNING DRV_NAME ": %s: Warning: Port %d (on %s) was "
1338 "related to aggregator %d but was not on its port list\n",
1339 port->slave->dev->master->name,
1340 port->actor_port_number, port->slave->dev->name,
1341 port->aggregator->aggregator_identifier);
1344 // search on all aggregators for a suitable aggregator for this port
1345 for (aggregator = __get_first_agg(port); aggregator;
1346 aggregator = __get_next_agg(aggregator)) {
1348 // keep a free aggregator for later use(if needed)
1349 if (!aggregator->lag_ports) {
1350 if (!free_aggregator) {
1351 free_aggregator=aggregator;
1353 continue;
1355 // check if current aggregator suits us
1356 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
1357 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1358 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1359 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1360 ) &&
1361 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
1362 !aggregator->is_individual) // but is not individual OR
1365 // attach to the founded aggregator
1366 port->aggregator = aggregator;
1367 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1368 port->next_port_in_aggregator=aggregator->lag_ports;
1369 port->aggregator->num_of_ports++;
1370 aggregator->lag_ports=port;
1371 pr_debug("Port %d joined LAG %d(existing LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1373 // mark this port as selected
1374 port->sm_vars |= AD_PORT_SELECTED;
1375 found = 1;
1376 break;
1380 // the port couldn't find an aggregator - attach it to a new aggregator
1381 if (!found) {
1382 if (free_aggregator) {
1383 // assign port a new aggregator
1384 port->aggregator = free_aggregator;
1385 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1387 // update the new aggregator's parameters
1388 // if port was responsed from the end-user
1389 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
1390 port->aggregator->is_individual = 0;
1391 } else {
1392 port->aggregator->is_individual = 1;
1395 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1396 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
1397 port->aggregator->partner_system=port->partner_oper.system;
1398 port->aggregator->partner_system_priority = port->partner_oper.system_priority;
1399 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1400 port->aggregator->receive_state = 1;
1401 port->aggregator->transmit_state = 1;
1402 port->aggregator->lag_ports = port;
1403 port->aggregator->num_of_ports++;
1405 // mark this port as selected
1406 port->sm_vars |= AD_PORT_SELECTED;
1408 pr_debug("Port %d joined LAG %d(new LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1409 } else {
1410 printk(KERN_ERR DRV_NAME ": %s: Port %d (on %s) did not find a suitable aggregator\n",
1411 port->slave->dev->master->name,
1412 port->actor_port_number, port->slave->dev->name);
1415 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1416 // else set ready=FALSE in all aggregator's ports
1417 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1419 aggregator = __get_first_agg(port);
1420 ad_agg_selection_logic(aggregator);
1424 * Decide if "agg" is a better choice for the new active aggregator that
1425 * the current best, according to the ad_select policy.
1427 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1428 struct aggregator *curr)
1431 * 0. If no best, select current.
1433 * 1. If the current agg is not individual, and the best is
1434 * individual, select current.
1436 * 2. If current agg is individual and the best is not, keep best.
1438 * 3. Therefore, current and best are both individual or both not
1439 * individual, so:
1441 * 3a. If current agg partner replied, and best agg partner did not,
1442 * select current.
1444 * 3b. If current agg partner did not reply and best agg partner
1445 * did reply, keep best.
1447 * 4. Therefore, current and best both have partner replies or
1448 * both do not, so perform selection policy:
1450 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1451 * select by bandwidth.
1453 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1455 if (!best)
1456 return curr;
1458 if (!curr->is_individual && best->is_individual)
1459 return curr;
1461 if (curr->is_individual && !best->is_individual)
1462 return best;
1464 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1465 return curr;
1467 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1468 return best;
1470 switch (__get_agg_selection_mode(curr->lag_ports)) {
1471 case BOND_AD_COUNT:
1472 if (curr->num_of_ports > best->num_of_ports)
1473 return curr;
1475 if (curr->num_of_ports < best->num_of_ports)
1476 return best;
1478 /*FALLTHROUGH*/
1479 case BOND_AD_STABLE:
1480 case BOND_AD_BANDWIDTH:
1481 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1482 return curr;
1484 break;
1486 default:
1487 printk(KERN_WARNING DRV_NAME
1488 ": %s: Impossible agg select mode %d\n",
1489 curr->slave->dev->master->name,
1490 __get_agg_selection_mode(curr->lag_ports));
1491 break;
1494 return best;
1498 * ad_agg_selection_logic - select an aggregation group for a team
1499 * @aggregator: the aggregator we're looking at
1501 * It is assumed that only one aggregator may be selected for a team.
1503 * The logic of this function is to select the aggregator according to
1504 * the ad_select policy:
1506 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1507 * it, and to reselect the active aggregator only if the previous
1508 * aggregator has no more ports related to it.
1510 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1511 * bandwidth, and reselect whenever a link state change takes place or the
1512 * set of slaves in the bond changes.
1514 * BOND_AD_COUNT: select the aggregator with largest number of ports
1515 * (slaves), and reselect whenever a link state change takes place or the
1516 * set of slaves in the bond changes.
1518 * FIXME: this function MUST be called with the first agg in the bond, or
1519 * __get_active_agg() won't work correctly. This function should be better
1520 * called with the bond itself, and retrieve the first agg from it.
1522 static void ad_agg_selection_logic(struct aggregator *agg)
1524 struct aggregator *best, *active, *origin;
1525 struct port *port;
1527 origin = agg;
1529 active = __get_active_agg(agg);
1530 best = active;
1532 do {
1533 agg->is_active = 0;
1535 if (agg->num_of_ports)
1536 best = ad_agg_selection_test(best, agg);
1538 } while ((agg = __get_next_agg(agg)));
1540 if (best &&
1541 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1543 * For the STABLE policy, don't replace the old active
1544 * aggregator if it's still active (it has an answering
1545 * partner) or if both the best and active don't have an
1546 * answering partner.
1548 if (active && active->lag_ports &&
1549 active->lag_ports->is_enabled &&
1550 (__agg_has_partner(active) ||
1551 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1552 if (!(!active->actor_oper_aggregator_key &&
1553 best->actor_oper_aggregator_key)) {
1554 best = NULL;
1555 active->is_active = 1;
1560 if (best && (best == active)) {
1561 best = NULL;
1562 active->is_active = 1;
1565 // if there is new best aggregator, activate it
1566 if (best) {
1567 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1568 best->aggregator_identifier, best->num_of_ports,
1569 best->actor_oper_aggregator_key,
1570 best->partner_oper_aggregator_key,
1571 best->is_individual, best->is_active);
1572 pr_debug("best ports %p slave %p %s\n",
1573 best->lag_ports, best->slave,
1574 best->slave ? best->slave->dev->name : "NULL");
1576 for (agg = __get_first_agg(best->lag_ports); agg;
1577 agg = __get_next_agg(agg)) {
1579 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1580 agg->aggregator_identifier, agg->num_of_ports,
1581 agg->actor_oper_aggregator_key,
1582 agg->partner_oper_aggregator_key,
1583 agg->is_individual, agg->is_active);
1586 // check if any partner replys
1587 if (best->is_individual) {
1588 printk(KERN_WARNING DRV_NAME ": %s: Warning: No 802.3ad"
1589 " response from the link partner for any"
1590 " adapters in the bond\n",
1591 best->slave->dev->master->name);
1594 best->is_active = 1;
1595 pr_debug("LAG %d chosen as the active LAG\n",
1596 best->aggregator_identifier);
1597 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1598 best->aggregator_identifier, best->num_of_ports,
1599 best->actor_oper_aggregator_key,
1600 best->partner_oper_aggregator_key,
1601 best->is_individual, best->is_active);
1603 // disable the ports that were related to the former active_aggregator
1604 if (active) {
1605 for (port = active->lag_ports; port;
1606 port = port->next_port_in_aggregator) {
1607 __disable_port(port);
1613 * if the selected aggregator is of join individuals
1614 * (partner_system is NULL), enable their ports
1616 active = __get_active_agg(origin);
1618 if (active) {
1619 if (!__agg_has_partner(active)) {
1620 for (port = active->lag_ports; port;
1621 port = port->next_port_in_aggregator) {
1622 __enable_port(port);
1627 if (origin->slave) {
1628 struct bonding *bond;
1630 bond = bond_get_bond_by_slave(origin->slave);
1631 if (bond)
1632 bond_3ad_set_carrier(bond);
1637 * ad_clear_agg - clear a given aggregator's parameters
1638 * @aggregator: the aggregator we're looking at
1641 static void ad_clear_agg(struct aggregator *aggregator)
1643 if (aggregator) {
1644 aggregator->is_individual = 0;
1645 aggregator->actor_admin_aggregator_key = 0;
1646 aggregator->actor_oper_aggregator_key = 0;
1647 aggregator->partner_system = null_mac_addr;
1648 aggregator->partner_system_priority = 0;
1649 aggregator->partner_oper_aggregator_key = 0;
1650 aggregator->receive_state = 0;
1651 aggregator->transmit_state = 0;
1652 aggregator->lag_ports = NULL;
1653 aggregator->is_active = 0;
1654 aggregator->num_of_ports = 0;
1655 pr_debug("LAG %d was cleared\n", aggregator->aggregator_identifier);
1660 * ad_initialize_agg - initialize a given aggregator's parameters
1661 * @aggregator: the aggregator we're looking at
1664 static void ad_initialize_agg(struct aggregator *aggregator)
1666 if (aggregator) {
1667 ad_clear_agg(aggregator);
1669 aggregator->aggregator_mac_address = null_mac_addr;
1670 aggregator->aggregator_identifier = 0;
1671 aggregator->slave = NULL;
1676 * ad_initialize_port - initialize a given port's parameters
1677 * @aggregator: the aggregator we're looking at
1678 * @lacp_fast: boolean. whether fast periodic should be used
1681 static void ad_initialize_port(struct port *port, int lacp_fast)
1683 if (port) {
1684 port->actor_port_number = 1;
1685 port->actor_port_priority = 0xff;
1686 port->actor_system = null_mac_addr;
1687 port->actor_system_priority = 0xffff;
1688 port->actor_port_aggregator_identifier = 0;
1689 port->ntt = 0;
1690 port->actor_admin_port_key = 1;
1691 port->actor_oper_port_key = 1;
1692 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1693 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1695 if (lacp_fast) {
1696 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1699 port->partner_admin.system = null_mac_addr;
1700 port->partner_oper.system = null_mac_addr;
1701 port->partner_admin.system_priority = 0xffff;
1702 port->partner_oper.system_priority = 0xffff;
1703 port->partner_admin.key = 1;
1704 port->partner_oper.key = 1;
1705 port->partner_admin.port_number = 1;
1706 port->partner_oper.port_number = 1;
1707 port->partner_admin.port_priority = 0xff;
1708 port->partner_oper.port_priority = 0xff;
1709 port->partner_admin.port_state = 1;
1710 port->partner_oper.port_state = 1;
1711 port->is_enabled = 1;
1712 // ****** private parameters ******
1713 port->sm_vars = 0x3;
1714 port->sm_rx_state = 0;
1715 port->sm_rx_timer_counter = 0;
1716 port->sm_periodic_state = 0;
1717 port->sm_periodic_timer_counter = 0;
1718 port->sm_mux_state = 0;
1719 port->sm_mux_timer_counter = 0;
1720 port->sm_tx_state = 0;
1721 port->sm_tx_timer_counter = 0;
1722 port->slave = NULL;
1723 port->aggregator = NULL;
1724 port->next_port_in_aggregator = NULL;
1725 port->transaction_id = 0;
1727 ad_initialize_lacpdu(&(port->lacpdu));
1732 * ad_enable_collecting_distributing - enable a port's transmit/receive
1733 * @port: the port we're looking at
1735 * Enable @port if it's in an active aggregator
1737 static void ad_enable_collecting_distributing(struct port *port)
1739 if (port->aggregator->is_active) {
1740 pr_debug("Enabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1741 __enable_port(port);
1746 * ad_disable_collecting_distributing - disable a port's transmit/receive
1747 * @port: the port we're looking at
1750 static void ad_disable_collecting_distributing(struct port *port)
1752 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
1753 pr_debug("Disabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1754 __disable_port(port);
1758 #if 0
1760 * ad_marker_info_send - send a marker information frame
1761 * @port: the port we're looking at
1763 * This function does nothing since we decided not to implement send and handle
1764 * response for marker PDU's, in this stage, but only to respond to marker
1765 * information.
1767 static void ad_marker_info_send(struct port *port)
1769 struct bond_marker marker;
1770 u16 index;
1772 // fill the marker PDU with the appropriate values
1773 marker.subtype = 0x02;
1774 marker.version_number = 0x01;
1775 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1776 marker.marker_length = 0x16;
1777 // convert requester_port to Big Endian
1778 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1779 marker.requester_system = port->actor_system;
1780 // convert requester_port(u32) to Big Endian
1781 marker.requester_transaction_id = (((++port->transaction_id & 0xFF) << 24) |((port->transaction_id & 0xFF00) << 8) |((port->transaction_id & 0xFF0000) >> 8) |((port->transaction_id & 0xFF000000) >> 24));
1782 marker.pad = 0;
1783 marker.tlv_type_terminator = 0x00;
1784 marker.terminator_length = 0x00;
1785 for (index=0; index<90; index++) {
1786 marker.reserved_90[index]=0;
1789 // send the marker information
1790 if (ad_marker_send(port, &marker) >= 0) {
1791 pr_debug("Sent Marker Information on port %d\n", port->actor_port_number);
1794 #endif
1797 * ad_marker_info_received - handle receive of a Marker information frame
1798 * @marker_info: Marker info received
1799 * @port: the port we're looking at
1802 static void ad_marker_info_received(struct bond_marker *marker_info,
1803 struct port *port)
1805 struct bond_marker marker;
1807 // copy the received marker data to the response marker
1808 //marker = *marker_info;
1809 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1810 // change the marker subtype to marker response
1811 marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE;
1812 // send the marker response
1814 if (ad_marker_send(port, &marker) >= 0) {
1815 pr_debug("Sent Marker Response on port %d\n", port->actor_port_number);
1820 * ad_marker_response_received - handle receive of a marker response frame
1821 * @marker: marker PDU received
1822 * @port: the port we're looking at
1824 * This function does nothing since we decided not to implement send and handle
1825 * response for marker PDU's, in this stage, but only to respond to marker
1826 * information.
1828 static void ad_marker_response_received(struct bond_marker *marker,
1829 struct port *port)
1831 marker=NULL; // just to satisfy the compiler
1832 port=NULL; // just to satisfy the compiler
1833 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1837 * ad_initialize_lacpdu - initialize a given lacpdu structure
1838 * @lacpdu: lacpdu structure to initialize
1841 static void ad_initialize_lacpdu(struct lacpdu *lacpdu)
1843 u16 index;
1845 // initialize lacpdu data
1846 lacpdu->subtype = 0x01;
1847 lacpdu->version_number = 0x01;
1848 lacpdu->tlv_type_actor_info = 0x01;
1849 lacpdu->actor_information_length = 0x14;
1850 // lacpdu->actor_system_priority updated on send
1851 // lacpdu->actor_system updated on send
1852 // lacpdu->actor_key updated on send
1853 // lacpdu->actor_port_priority updated on send
1854 // lacpdu->actor_port updated on send
1855 // lacpdu->actor_state updated on send
1856 lacpdu->tlv_type_partner_info = 0x02;
1857 lacpdu->partner_information_length = 0x14;
1858 for (index=0; index<=2; index++) {
1859 lacpdu->reserved_3_1[index]=0;
1861 // lacpdu->partner_system_priority updated on send
1862 // lacpdu->partner_system updated on send
1863 // lacpdu->partner_key updated on send
1864 // lacpdu->partner_port_priority updated on send
1865 // lacpdu->partner_port updated on send
1866 // lacpdu->partner_state updated on send
1867 for (index=0; index<=2; index++) {
1868 lacpdu->reserved_3_2[index]=0;
1870 lacpdu->tlv_type_collector_info = 0x03;
1871 lacpdu->collector_information_length= 0x10;
1872 lacpdu->collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY);
1873 for (index=0; index<=11; index++) {
1874 lacpdu->reserved_12[index]=0;
1876 lacpdu->tlv_type_terminator = 0x00;
1877 lacpdu->terminator_length = 0;
1878 for (index=0; index<=49; index++) {
1879 lacpdu->reserved_50[index]=0;
1883 //////////////////////////////////////////////////////////////////////////////////////
1884 // ================= AD exported functions to the main bonding code ==================
1885 //////////////////////////////////////////////////////////////////////////////////////
1887 // Check aggregators status in team every T seconds
1888 #define AD_AGGREGATOR_SELECTION_TIMER 8
1891 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1893 * Set the aggregation selection timer, to initiate an agg selection in
1894 * the very near future. Called during first initialization, and during
1895 * any down to up transitions of the bond.
1897 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1899 BOND_AD_INFO(bond).agg_select_timer = timeout;
1900 BOND_AD_INFO(bond).agg_select_mode = bond->params.ad_select;
1903 static u16 aggregator_identifier;
1906 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1907 * @bond: bonding struct to work on
1908 * @tick_resolution: tick duration (millisecond resolution)
1909 * @lacp_fast: boolean. whether fast periodic should be used
1911 * Can be called only after the mac address of the bond is set.
1913 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
1915 // check that the bond is not initialized yet
1916 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr), &(bond->dev->dev_addr))) {
1918 aggregator_identifier = 0;
1920 BOND_AD_INFO(bond).lacp_fast = lacp_fast;
1921 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1922 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1924 // initialize how many times this module is called in one second(should be about every 100ms)
1925 ad_ticks_per_sec = tick_resolution;
1927 bond_3ad_initiate_agg_selection(bond,
1928 AD_AGGREGATOR_SELECTION_TIMER *
1929 ad_ticks_per_sec);
1934 * bond_3ad_bind_slave - initialize a slave's port
1935 * @slave: slave struct to work on
1937 * Returns: 0 on success
1938 * < 0 on error
1940 int bond_3ad_bind_slave(struct slave *slave)
1942 struct bonding *bond = bond_get_bond_by_slave(slave);
1943 struct port *port;
1944 struct aggregator *aggregator;
1946 if (bond == NULL) {
1947 printk(KERN_ERR DRV_NAME ": %s: The slave %s is not attached to its bond\n",
1948 slave->dev->master->name, slave->dev->name);
1949 return -1;
1952 //check that the slave has not been intialized yet.
1953 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1955 // port initialization
1956 port = &(SLAVE_AD_INFO(slave).port);
1958 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
1960 port->slave = slave;
1961 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1962 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1963 // ------------------------------------------------------------
1964 // Port key : | User key | Speed |Duplex|
1965 // ------------------------------------------------------------
1966 // 16 6 1 0
1967 port->actor_admin_port_key = 0; // initialize this parameter
1968 port->actor_admin_port_key |= __get_duplex(port);
1969 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1970 port->actor_oper_port_key = port->actor_admin_port_key;
1971 // if the port is not full duplex, then the port should be not lacp Enabled
1972 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1973 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1975 // actor system is the bond's system
1976 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1977 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1978 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1979 port->aggregator = NULL;
1980 port->next_port_in_aggregator = NULL;
1982 __disable_port(port);
1983 __initialize_port_locks(port);
1986 // aggregator initialization
1987 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1989 ad_initialize_agg(aggregator);
1991 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1992 aggregator->aggregator_identifier = (++aggregator_identifier);
1993 aggregator->slave = slave;
1994 aggregator->is_active = 0;
1995 aggregator->num_of_ports = 0;
1998 return 0;
2002 * bond_3ad_unbind_slave - deinitialize a slave's port
2003 * @slave: slave struct to work on
2005 * Search for the aggregator that is related to this port, remove the
2006 * aggregator and assign another aggregator for other port related to it
2007 * (if any), and remove the port.
2009 void bond_3ad_unbind_slave(struct slave *slave)
2011 struct port *port, *prev_port, *temp_port;
2012 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2013 int select_new_active_agg = 0;
2015 // find the aggregator related to this slave
2016 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
2018 // find the port related to this slave
2019 port = &(SLAVE_AD_INFO(slave).port);
2021 // if slave is null, the whole port is not initialized
2022 if (!port->slave) {
2023 printk(KERN_WARNING DRV_NAME ": Warning: %s: Trying to "
2024 "unbind an uninitialized port on %s\n",
2025 slave->dev->master->name, slave->dev->name);
2026 return;
2029 pr_debug("Unbinding Link Aggregation Group %d\n", aggregator->aggregator_identifier);
2031 /* Tell the partner that this port is not suitable for aggregation */
2032 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2033 __update_lacpdu_from_port(port);
2034 ad_lacpdu_send(port);
2036 // check if this aggregator is occupied
2037 if (aggregator->lag_ports) {
2038 // check if there are other ports related to this aggregator except
2039 // the port related to this slave(thats ensure us that there is a
2040 // reason to search for new aggregator, and that we will find one
2041 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
2042 // find new aggregator for the related port(s)
2043 new_aggregator = __get_first_agg(port);
2044 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
2045 // if the new aggregator is empty, or it connected to to our port only
2046 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
2047 break;
2050 // if new aggregator found, copy the aggregator's parameters
2051 // and connect the related lag_ports to the new aggregator
2052 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2053 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator->aggregator_identifier, new_aggregator->aggregator_identifier);
2055 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
2056 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2057 aggregator->slave->dev->master->name);
2058 // select new active aggregator
2059 select_new_active_agg = 1;
2062 new_aggregator->is_individual = aggregator->is_individual;
2063 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2064 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2065 new_aggregator->partner_system = aggregator->partner_system;
2066 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2067 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2068 new_aggregator->receive_state = aggregator->receive_state;
2069 new_aggregator->transmit_state = aggregator->transmit_state;
2070 new_aggregator->lag_ports = aggregator->lag_ports;
2071 new_aggregator->is_active = aggregator->is_active;
2072 new_aggregator->num_of_ports = aggregator->num_of_ports;
2074 // update the information that is written on the ports about the aggregator
2075 for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) {
2076 temp_port->aggregator=new_aggregator;
2077 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2080 // clear the aggregator
2081 ad_clear_agg(aggregator);
2083 if (select_new_active_agg) {
2084 ad_agg_selection_logic(__get_first_agg(port));
2086 } else {
2087 printk(KERN_WARNING DRV_NAME ": %s: Warning: unbinding aggregator, "
2088 "and could not find a new aggregator for its ports\n",
2089 slave->dev->master->name);
2091 } else { // in case that the only port related to this aggregator is the one we want to remove
2092 select_new_active_agg = aggregator->is_active;
2093 // clear the aggregator
2094 ad_clear_agg(aggregator);
2095 if (select_new_active_agg) {
2096 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2097 slave->dev->master->name);
2098 // select new active aggregator
2099 ad_agg_selection_logic(__get_first_agg(port));
2104 pr_debug("Unbinding port %d\n", port->actor_port_number);
2105 // find the aggregator that this port is connected to
2106 temp_aggregator = __get_first_agg(port);
2107 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2108 prev_port = NULL;
2109 // search the port in the aggregator's related ports
2110 for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) {
2111 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2112 if (prev_port) {
2113 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2114 } else {
2115 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2117 temp_aggregator->num_of_ports--;
2118 if (temp_aggregator->num_of_ports==0) {
2119 select_new_active_agg = temp_aggregator->is_active;
2120 // clear the aggregator
2121 ad_clear_agg(temp_aggregator);
2122 if (select_new_active_agg) {
2123 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2124 slave->dev->master->name);
2125 // select new active aggregator
2126 ad_agg_selection_logic(__get_first_agg(port));
2129 break;
2133 port->slave=NULL;
2137 * bond_3ad_state_machine_handler - handle state machines timeout
2138 * @bond: bonding struct to work on
2140 * The state machine handling concept in this module is to check every tick
2141 * which state machine should operate any function. The execution order is
2142 * round robin, so when we have an interaction between state machines, the
2143 * reply of one to each other might be delayed until next tick.
2145 * This function also complete the initialization when the agg_select_timer
2146 * times out, and it selects an aggregator for the ports that are yet not
2147 * related to any aggregator, and selects the active aggregator for a bond.
2149 void bond_3ad_state_machine_handler(struct work_struct *work)
2151 struct bonding *bond = container_of(work, struct bonding,
2152 ad_work.work);
2153 struct port *port;
2154 struct aggregator *aggregator;
2156 read_lock(&bond->lock);
2158 if (bond->kill_timers) {
2159 goto out;
2162 //check if there are any slaves
2163 if (bond->slave_cnt == 0) {
2164 goto re_arm;
2167 // check if agg_select_timer timer after initialize is timed out
2168 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2169 // select the active aggregator for the bond
2170 if ((port = __get_first_port(bond))) {
2171 if (!port->slave) {
2172 printk(KERN_WARNING DRV_NAME ": %s: Warning: bond's first port is "
2173 "uninitialized\n", bond->dev->name);
2174 goto re_arm;
2177 aggregator = __get_first_agg(port);
2178 ad_agg_selection_logic(aggregator);
2180 bond_3ad_set_carrier(bond);
2183 // for each port run the state machines
2184 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2185 if (!port->slave) {
2186 printk(KERN_WARNING DRV_NAME ": %s: Warning: Found an uninitialized "
2187 "port\n", bond->dev->name);
2188 goto re_arm;
2191 ad_rx_machine(NULL, port);
2192 ad_periodic_machine(port);
2193 ad_port_selection_logic(port);
2194 ad_mux_machine(port);
2195 ad_tx_machine(port);
2197 // turn off the BEGIN bit, since we already handled it
2198 if (port->sm_vars & AD_PORT_BEGIN) {
2199 port->sm_vars &= ~AD_PORT_BEGIN;
2203 re_arm:
2204 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2205 out:
2206 read_unlock(&bond->lock);
2210 * bond_3ad_rx_indication - handle a received frame
2211 * @lacpdu: received lacpdu
2212 * @slave: slave struct to work on
2213 * @length: length of the data received
2215 * It is assumed that frames that were sent on this NIC don't returned as new
2216 * received frames (loopback). Since only the payload is given to this
2217 * function, it check for loopback.
2219 static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
2221 struct port *port;
2223 if (length >= sizeof(struct lacpdu)) {
2225 port = &(SLAVE_AD_INFO(slave).port);
2227 if (!port->slave) {
2228 printk(KERN_WARNING DRV_NAME ": %s: Warning: port of slave %s is "
2229 "uninitialized\n", slave->dev->name, slave->dev->master->name);
2230 return;
2233 switch (lacpdu->subtype) {
2234 case AD_TYPE_LACPDU:
2235 pr_debug("Received LACPDU on port %d\n", port->actor_port_number);
2236 ad_rx_machine(lacpdu, port);
2237 break;
2239 case AD_TYPE_MARKER:
2240 // No need to convert fields to Little Endian since we don't use the marker's fields.
2242 switch (((struct bond_marker *)lacpdu)->tlv_type) {
2243 case AD_MARKER_INFORMATION_SUBTYPE:
2244 pr_debug("Received Marker Information on port %d\n", port->actor_port_number);
2245 ad_marker_info_received((struct bond_marker *)lacpdu, port);
2246 break;
2248 case AD_MARKER_RESPONSE_SUBTYPE:
2249 pr_debug("Received Marker Response on port %d\n", port->actor_port_number);
2250 ad_marker_response_received((struct bond_marker *)lacpdu, port);
2251 break;
2253 default:
2254 pr_debug("Received an unknown Marker subtype on slot %d\n", port->actor_port_number);
2261 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2262 * @slave: slave struct to work on
2264 * Handle reselection of aggregator (if needed) for this port.
2266 void bond_3ad_adapter_speed_changed(struct slave *slave)
2268 struct port *port;
2270 port = &(SLAVE_AD_INFO(slave).port);
2272 // if slave is null, the whole port is not initialized
2273 if (!port->slave) {
2274 printk(KERN_WARNING DRV_NAME ": Warning: %s: speed "
2275 "changed for uninitialized port on %s\n",
2276 slave->dev->master->name, slave->dev->name);
2277 return;
2280 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2281 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2282 pr_debug("Port %d changed speed\n", port->actor_port_number);
2283 // there is no need to reselect a new aggregator, just signal the
2284 // state machines to reinitialize
2285 port->sm_vars |= AD_PORT_BEGIN;
2289 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2290 * @slave: slave struct to work on
2292 * Handle reselection of aggregator (if needed) for this port.
2294 void bond_3ad_adapter_duplex_changed(struct slave *slave)
2296 struct port *port;
2298 port=&(SLAVE_AD_INFO(slave).port);
2300 // if slave is null, the whole port is not initialized
2301 if (!port->slave) {
2302 printk(KERN_WARNING DRV_NAME ": %s: Warning: duplex changed "
2303 "for uninitialized port on %s\n",
2304 slave->dev->master->name, slave->dev->name);
2305 return;
2308 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2309 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2310 pr_debug("Port %d changed duplex\n", port->actor_port_number);
2311 // there is no need to reselect a new aggregator, just signal the
2312 // state machines to reinitialize
2313 port->sm_vars |= AD_PORT_BEGIN;
2317 * bond_3ad_handle_link_change - handle a slave's link status change indication
2318 * @slave: slave struct to work on
2319 * @status: whether the link is now up or down
2321 * Handle reselection of aggregator (if needed) for this port.
2323 void bond_3ad_handle_link_change(struct slave *slave, char link)
2325 struct port *port;
2327 port = &(SLAVE_AD_INFO(slave).port);
2329 // if slave is null, the whole port is not initialized
2330 if (!port->slave) {
2331 printk(KERN_WARNING DRV_NAME ": Warning: %s: link status changed for "
2332 "uninitialized port on %s\n",
2333 slave->dev->master->name, slave->dev->name);
2334 return;
2337 // 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)
2338 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2339 if (link == BOND_LINK_UP) {
2340 port->is_enabled = 1;
2341 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2342 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2343 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2344 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2345 } else {
2346 /* link has failed */
2347 port->is_enabled = 0;
2348 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2349 port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS);
2351 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2352 // there is no need to reselect a new aggregator, just signal the
2353 // state machines to reinitialize
2354 port->sm_vars |= AD_PORT_BEGIN;
2358 * set link state for bonding master: if we have an active
2359 * aggregator, we're up, if not, we're down. Presumes that we cannot
2360 * have an active aggregator if there are no slaves with link up.
2362 * This behavior complies with IEEE 802.3 section 43.3.9.
2364 * Called by bond_set_carrier(). Return zero if carrier state does not
2365 * change, nonzero if it does.
2367 int bond_3ad_set_carrier(struct bonding *bond)
2369 if (__get_active_agg(&(SLAVE_AD_INFO(bond->first_slave).aggregator))) {
2370 if (!netif_carrier_ok(bond->dev)) {
2371 netif_carrier_on(bond->dev);
2372 return 1;
2374 return 0;
2377 if (netif_carrier_ok(bond->dev)) {
2378 netif_carrier_off(bond->dev);
2379 return 1;
2381 return 0;
2385 * bond_3ad_get_active_agg_info - get information of the active aggregator
2386 * @bond: bonding struct to work on
2387 * @ad_info: ad_info struct to fill with the bond's info
2389 * Returns: 0 on success
2390 * < 0 on error
2392 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2394 struct aggregator *aggregator = NULL;
2395 struct port *port;
2397 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2398 if (port->aggregator && port->aggregator->is_active) {
2399 aggregator = port->aggregator;
2400 break;
2404 if (aggregator) {
2405 ad_info->aggregator_id = aggregator->aggregator_identifier;
2406 ad_info->ports = aggregator->num_of_ports;
2407 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2408 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2409 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2410 return 0;
2413 return -1;
2416 int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2418 struct slave *slave, *start_at;
2419 struct bonding *bond = netdev_priv(dev);
2420 int slave_agg_no;
2421 int slaves_in_agg;
2422 int agg_id;
2423 int i;
2424 struct ad_info ad_info;
2425 int res = 1;
2427 /* make sure that the slaves list will
2428 * not change during tx
2430 read_lock(&bond->lock);
2432 if (!BOND_IS_OK(bond)) {
2433 goto out;
2436 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
2437 printk(KERN_DEBUG DRV_NAME ": %s: Error: "
2438 "bond_3ad_get_active_agg_info failed\n", dev->name);
2439 goto out;
2442 slaves_in_agg = ad_info.ports;
2443 agg_id = ad_info.aggregator_id;
2445 if (slaves_in_agg == 0) {
2446 /*the aggregator is empty*/
2447 printk(KERN_DEBUG DRV_NAME ": %s: Error: active "
2448 "aggregator is empty\n",
2449 dev->name);
2450 goto out;
2453 slave_agg_no = bond->xmit_hash_policy(skb, dev, slaves_in_agg);
2455 bond_for_each_slave(bond, slave, i) {
2456 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2458 if (agg && (agg->aggregator_identifier == agg_id)) {
2459 slave_agg_no--;
2460 if (slave_agg_no < 0) {
2461 break;
2466 if (slave_agg_no >= 0) {
2467 printk(KERN_ERR DRV_NAME ": %s: Error: Couldn't find a slave to tx on "
2468 "for aggregator ID %d\n", dev->name, agg_id);
2469 goto out;
2472 start_at = slave;
2474 bond_for_each_slave_from(bond, slave, i, start_at) {
2475 int slave_agg_id = 0;
2476 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2478 if (agg) {
2479 slave_agg_id = agg->aggregator_identifier;
2482 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2483 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2484 break;
2488 out:
2489 if (res) {
2490 /* no suitable interface, frame not sent */
2491 dev_kfree_skb(skb);
2493 read_unlock(&bond->lock);
2494 return 0;
2497 int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype, struct net_device *orig_dev)
2499 struct bonding *bond = netdev_priv(dev);
2500 struct slave *slave = NULL;
2501 int ret = NET_RX_DROP;
2503 if (dev_net(dev) != &init_net)
2504 goto out;
2506 if (!(dev->flags & IFF_MASTER))
2507 goto out;
2509 read_lock(&bond->lock);
2510 slave = bond_get_slave_by_dev((struct bonding *)netdev_priv(dev),
2511 orig_dev);
2512 if (!slave)
2513 goto out_unlock;
2515 bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
2517 ret = NET_RX_SUCCESS;
2519 out_unlock:
2520 read_unlock(&bond->lock);
2521 out:
2522 dev_kfree_skb(skb);
2524 return ret;