bonding: use port_params in __update_lacpdu_from_port
[linux-2.6/btrfs-unstable.git] / drivers / net / bonding / bond_3ad.c
blobb9dd4091ede973fe3f26af2051277fe6538f9270
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 // ================= main 802.3ad protocol functions ==================
99 static int ad_lacpdu_send(struct port *port);
100 static int ad_marker_send(struct port *port, struct bond_marker *marker);
101 static void ad_mux_machine(struct port *port);
102 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
103 static void ad_tx_machine(struct port *port);
104 static void ad_periodic_machine(struct port *port);
105 static void ad_port_selection_logic(struct port *port);
106 static void ad_agg_selection_logic(struct aggregator *aggregator);
107 static void ad_clear_agg(struct aggregator *aggregator);
108 static void ad_initialize_agg(struct aggregator *aggregator);
109 static void ad_initialize_port(struct port *port, int lacp_fast);
110 static void ad_initialize_lacpdu(struct lacpdu *Lacpdu);
111 static void ad_enable_collecting_distributing(struct port *port);
112 static void ad_disable_collecting_distributing(struct port *port);
113 static void ad_marker_info_received(struct bond_marker *marker_info, struct port *port);
114 static void ad_marker_response_received(struct bond_marker *marker, struct port *port);
117 /////////////////////////////////////////////////////////////////////////////////
118 // ================= api to bonding and kernel code ==================
119 /////////////////////////////////////////////////////////////////////////////////
122 * __get_bond_by_port - get the port's bonding struct
123 * @port: the port we're looking at
125 * Return @port's bonding struct, or %NULL if it can't be found.
127 static inline struct bonding *__get_bond_by_port(struct port *port)
129 if (port->slave == NULL) {
130 return NULL;
133 return bond_get_bond_by_slave(port->slave);
137 * __get_first_port - get the first port in the bond
138 * @bond: the bond we're looking at
140 * Return the port of the first slave in @bond, or %NULL if it can't be found.
142 static inline struct port *__get_first_port(struct bonding *bond)
144 if (bond->slave_cnt == 0) {
145 return NULL;
148 return &(SLAVE_AD_INFO(bond->first_slave).port);
152 * __get_next_port - get the next port in the bond
153 * @port: the port we're looking at
155 * Return the port of the slave that is next in line of @port's slave in the
156 * bond, or %NULL if it can't be found.
158 static inline struct port *__get_next_port(struct port *port)
160 struct bonding *bond = __get_bond_by_port(port);
161 struct slave *slave = port->slave;
163 // If there's no bond for this port, or this is the last slave
164 if ((bond == NULL) || (slave->next == bond->first_slave)) {
165 return NULL;
168 return &(SLAVE_AD_INFO(slave->next).port);
172 * __get_first_agg - get the first aggregator in the bond
173 * @bond: the bond we're looking at
175 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
176 * found.
178 static inline struct aggregator *__get_first_agg(struct port *port)
180 struct bonding *bond = __get_bond_by_port(port);
182 // If there's no bond for this port, or bond has no slaves
183 if ((bond == NULL) || (bond->slave_cnt == 0)) {
184 return NULL;
187 return &(SLAVE_AD_INFO(bond->first_slave).aggregator);
191 * __get_next_agg - get the next aggregator in the bond
192 * @aggregator: the aggregator we're looking at
194 * Return the aggregator of the slave that is next in line of @aggregator's
195 * slave in the bond, or %NULL if it can't be found.
197 static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
199 struct slave *slave = aggregator->slave;
200 struct bonding *bond = bond_get_bond_by_slave(slave);
202 // If there's no bond for this aggregator, or this is the last slave
203 if ((bond == NULL) || (slave->next == bond->first_slave)) {
204 return NULL;
207 return &(SLAVE_AD_INFO(slave->next).aggregator);
211 * __agg_has_partner
213 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
214 * address for the partner). Return 0 if not.
216 static inline int __agg_has_partner(struct aggregator *agg)
218 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
222 * __disable_port - disable the port's slave
223 * @port: the port we're looking at
226 static inline void __disable_port(struct port *port)
228 bond_set_slave_inactive_flags(port->slave);
232 * __enable_port - enable the port's slave, if it's up
233 * @port: the port we're looking at
236 static inline void __enable_port(struct port *port)
238 struct slave *slave = port->slave;
240 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev)) {
241 bond_set_slave_active_flags(slave);
246 * __port_is_enabled - check if the port's slave is in active state
247 * @port: the port we're looking at
250 static inline int __port_is_enabled(struct port *port)
252 return(port->slave->state == BOND_STATE_ACTIVE);
256 * __get_agg_selection_mode - get the aggregator selection mode
257 * @port: the port we're looking at
259 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
261 static inline u32 __get_agg_selection_mode(struct port *port)
263 struct bonding *bond = __get_bond_by_port(port);
265 if (bond == NULL) {
266 return BOND_AD_STABLE;
269 return BOND_AD_INFO(bond).agg_select_mode;
273 * __check_agg_selection_timer - check if the selection timer has expired
274 * @port: the port we're looking at
277 static inline int __check_agg_selection_timer(struct port *port)
279 struct bonding *bond = __get_bond_by_port(port);
281 if (bond == NULL) {
282 return 0;
285 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
289 * __get_rx_machine_lock - lock the port's RX machine
290 * @port: the port we're looking at
293 static inline void __get_rx_machine_lock(struct port *port)
295 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
299 * __release_rx_machine_lock - unlock the port's RX machine
300 * @port: the port we're looking at
303 static inline void __release_rx_machine_lock(struct port *port)
305 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
309 * __get_link_speed - get a port's speed
310 * @port: the port we're looking at
312 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
313 * 0,
314 * %AD_LINK_SPEED_BITMASK_10MBPS,
315 * %AD_LINK_SPEED_BITMASK_100MBPS,
316 * %AD_LINK_SPEED_BITMASK_1000MBPS,
317 * %AD_LINK_SPEED_BITMASK_10000MBPS
319 static u16 __get_link_speed(struct port *port)
321 struct slave *slave = port->slave;
322 u16 speed;
324 /* this if covers only a special case: when the configuration starts with
325 * link down, it sets the speed to 0.
326 * This is done in spite of the fact that the e100 driver reports 0 to be
327 * compatible with MVT in the future.*/
328 if (slave->link != BOND_LINK_UP) {
329 speed=0;
330 } else {
331 switch (slave->speed) {
332 case SPEED_10:
333 speed = AD_LINK_SPEED_BITMASK_10MBPS;
334 break;
336 case SPEED_100:
337 speed = AD_LINK_SPEED_BITMASK_100MBPS;
338 break;
340 case SPEED_1000:
341 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
342 break;
344 case SPEED_10000:
345 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
346 break;
348 default:
349 speed = 0; // unknown speed value from ethtool. shouldn't happen
350 break;
354 pr_debug("Port %d Received link speed %d update from adapter\n", port->actor_port_number, speed);
355 return speed;
359 * __get_duplex - get a port's duplex
360 * @port: the port we're looking at
362 * Return @port's duplex in 802.3ad bitmask format. i.e.:
363 * 0x01 if in full duplex
364 * 0x00 otherwise
366 static u8 __get_duplex(struct port *port)
368 struct slave *slave = port->slave;
370 u8 retval;
372 // handling a special case: when the configuration starts with
373 // link down, it sets the duplex to 0.
374 if (slave->link != BOND_LINK_UP) {
375 retval=0x0;
376 } else {
377 switch (slave->duplex) {
378 case DUPLEX_FULL:
379 retval=0x1;
380 pr_debug("Port %d Received status full duplex update from adapter\n", port->actor_port_number);
381 break;
382 case DUPLEX_HALF:
383 default:
384 retval=0x0;
385 pr_debug("Port %d Received status NOT full duplex update from adapter\n", port->actor_port_number);
386 break;
389 return retval;
393 * __initialize_port_locks - initialize a port's RX machine spinlock
394 * @port: the port we're looking at
397 static inline void __initialize_port_locks(struct port *port)
399 // make sure it isn't called twice
400 spin_lock_init(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
403 //conversions
406 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
407 * @timer_type: which timer to operate
408 * @par: timer parameter. see below
410 * If @timer_type is %current_while_timer, @par indicates long/short timer.
411 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
412 * %SLOW_PERIODIC_TIME.
414 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
416 u16 retval=0; //to silence the compiler
418 switch (timer_type) {
419 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
420 if (par) { // for short or long timeout
421 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
422 } else {
423 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
425 break;
426 case AD_ACTOR_CHURN_TIMER: // for local churn machine
427 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
428 break;
429 case AD_PERIODIC_TIMER: // for periodic machine
430 retval = (par*ad_ticks_per_sec); // long timeout
431 break;
432 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
433 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
434 break;
435 case AD_WAIT_WHILE_TIMER: // for selection machine
436 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
437 break;
439 return retval;
443 /////////////////////////////////////////////////////////////////////////////////
444 // ================= ad_rx_machine helper functions ==================
445 /////////////////////////////////////////////////////////////////////////////////
448 * __record_pdu - record parameters from a received lacpdu
449 * @lacpdu: the lacpdu we've received
450 * @port: the port we're looking at
452 * Record the parameter values for the Actor carried in a received lacpdu as
453 * the current partner operational parameter values and sets
454 * actor_oper_port_state.defaulted to FALSE.
456 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
458 if (lacpdu && port) {
459 struct port_params *partner = &port->partner_oper;
461 // record the new parameter values for the partner operational
462 partner->port_number = ntohs(lacpdu->actor_port);
463 partner->port_priority = ntohs(lacpdu->actor_port_priority);
464 partner->system = lacpdu->actor_system;
465 partner->system_priority = ntohs(lacpdu->actor_system_priority);
466 partner->key = ntohs(lacpdu->actor_key);
467 partner->port_state = lacpdu->actor_state;
469 // set actor_oper_port_state.defaulted to FALSE
470 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
472 // set the partner sync. to on if the partner is sync. and the port is matched
473 if ((port->sm_vars & AD_PORT_MATCHED) && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
474 partner->port_state |= AD_STATE_SYNCHRONIZATION;
475 } else {
476 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
482 * __record_default - record default parameters
483 * @port: the port we're looking at
485 * This function records the default parameter values for the partner carried
486 * in the Partner Admin parameters as the current partner operational parameter
487 * values and sets actor_oper_port_state.defaulted to TRUE.
489 static void __record_default(struct port *port)
491 if (port) {
492 // record the partner admin parameters
493 memcpy(&port->partner_oper, &port->partner_admin,
494 sizeof(struct port_params));
496 // set actor_oper_port_state.defaulted to true
497 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
502 * __update_selected - update a port's Selected variable from a received lacpdu
503 * @lacpdu: the lacpdu we've received
504 * @port: the port we're looking at
506 * Update the value of the selected variable, using parameter values from a
507 * newly received lacpdu. The parameter values for the Actor carried in the
508 * received PDU are compared with the corresponding operational parameter
509 * values for the ports partner. If one or more of the comparisons shows that
510 * the value(s) received in the PDU differ from the current operational values,
511 * then selected is set to FALSE and actor_oper_port_state.synchronization is
512 * set to out_of_sync. Otherwise, selected remains unchanged.
514 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
516 if (lacpdu && port) {
517 const struct port_params *partner = &port->partner_oper;
519 // check if any parameter is different
520 if (ntohs(lacpdu->actor_port) != partner->port_number
521 || ntohs(lacpdu->actor_port_priority) != partner->port_priority
522 || MAC_ADDRESS_COMPARE(&lacpdu->actor_system, &partner->system)
523 || ntohs(lacpdu->actor_system_priority) != partner->system_priority
524 || ntohs(lacpdu->actor_key) != partner->key
525 || (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
526 // update the state machine Selected variable
527 port->sm_vars &= ~AD_PORT_SELECTED;
533 * __update_default_selected - update a port's Selected variable from Partner
534 * @port: the port we're looking at
536 * This function updates the value of the selected variable, using the partner
537 * administrative parameter values. The administrative values are compared with
538 * the corresponding operational parameter values for the partner. If one or
539 * more of the comparisons shows that the administrative value(s) differ from
540 * the current operational values, then Selected is set to FALSE and
541 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
542 * Selected remains unchanged.
544 static void __update_default_selected(struct port *port)
546 if (port) {
547 const struct port_params *admin = &port->partner_admin;
548 const struct port_params *oper = &port->partner_oper;
550 // check if any parameter is different
551 if (admin->port_number != oper->port_number
552 || admin->port_priority != oper->port_priority
553 || MAC_ADDRESS_COMPARE(&admin->system, &oper->system)
554 || admin->system_priority != oper->system_priority
555 || admin->key != oper->key
556 || (admin->port_state & AD_STATE_AGGREGATION)
557 != (oper->port_state & AD_STATE_AGGREGATION)) {
558 // update the state machine Selected variable
559 port->sm_vars &= ~AD_PORT_SELECTED;
565 * __choose_matched - update a port's matched variable from a received lacpdu
566 * @lacpdu: the lacpdu we've received
567 * @port: the port we're looking at
569 * Update the value of the matched variable, using parameter values from a
570 * newly received lacpdu. Parameter values for the partner carried in the
571 * received PDU are compared with the corresponding operational parameter
572 * values for the actor. Matched is set to TRUE if all of these parameters
573 * match and the PDU parameter partner_state.aggregation has the same value as
574 * actor_oper_port_state.aggregation and lacp will actively maintain the link
575 * in the aggregation. Matched is also set to TRUE if the value of
576 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
577 * an individual link and lacp will actively maintain the link. Otherwise,
578 * matched is set to FALSE. LACP is considered to be actively maintaining the
579 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
580 * the actor's actor_oper_port_state.lacp_activity and the PDU's
581 * partner_state.lacp_activity variables are TRUE.
583 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
585 // validate lacpdu and port
586 if (lacpdu && port) {
587 // check if all parameters are alike
588 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
589 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
590 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
591 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
592 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
593 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
594 // or this is individual link(aggregation == FALSE)
595 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
597 // update the state machine Matched variable
598 port->sm_vars |= AD_PORT_MATCHED;
599 } else {
600 port->sm_vars &= ~AD_PORT_MATCHED;
606 * __update_ntt - update a port's ntt variable from a received lacpdu
607 * @lacpdu: the lacpdu we've received
608 * @port: the port we're looking at
610 * Updates the value of the ntt variable, using parameter values from a newly
611 * received lacpdu. The parameter values for the partner carried in the
612 * received PDU are compared with the corresponding operational parameter
613 * values for the Actor. If one or more of the comparisons shows that the
614 * value(s) received in the PDU differ from the current operational values,
615 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
617 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
619 // validate lacpdu and port
620 if (lacpdu && port) {
621 // check if any parameter is different
622 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
623 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
624 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
625 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
626 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
627 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
628 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
629 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
630 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
632 // set ntt to be TRUE
633 port->ntt = 1;
639 * __attach_bond_to_agg
640 * @port: the port we're looking at
642 * Handle the attaching of the port's control parser/multiplexer and the
643 * aggregator. This function does nothing since the parser/multiplexer of the
644 * receive and the parser/multiplexer of the aggregator are already combined.
646 static void __attach_bond_to_agg(struct port *port)
648 port=NULL; // just to satisfy the compiler
649 // This function does nothing since the parser/multiplexer of the receive
650 // and the parser/multiplexer of the aggregator are already combined
654 * __detach_bond_from_agg
655 * @port: the port we're looking at
657 * Handle the detaching of the port's control parser/multiplexer from the
658 * aggregator. This function does nothing since the parser/multiplexer of the
659 * receive and the parser/multiplexer of the aggregator are already combined.
661 static void __detach_bond_from_agg(struct port *port)
663 port=NULL; // just to satisfy the compiler
664 // This function does nothing sience the parser/multiplexer of the receive
665 // and the parser/multiplexer of the aggregator are already combined
669 * __agg_ports_are_ready - check if all ports in an aggregator are ready
670 * @aggregator: the aggregator we're looking at
673 static int __agg_ports_are_ready(struct aggregator *aggregator)
675 struct port *port;
676 int retval = 1;
678 if (aggregator) {
679 // scan all ports in this aggregator to verfy if they are all ready
680 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
681 if (!(port->sm_vars & AD_PORT_READY_N)) {
682 retval = 0;
683 break;
688 return retval;
692 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
693 * @aggregator: the aggregator we're looking at
694 * @val: Should the ports' ready bit be set on or off
697 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
699 struct port *port;
701 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
702 if (val) {
703 port->sm_vars |= AD_PORT_READY;
704 } else {
705 port->sm_vars &= ~AD_PORT_READY;
711 * __get_agg_bandwidth - get the total bandwidth of an aggregator
712 * @aggregator: the aggregator we're looking at
715 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
717 u32 bandwidth=0;
718 u32 basic_speed;
720 if (aggregator->num_of_ports) {
721 basic_speed = __get_link_speed(aggregator->lag_ports);
722 switch (basic_speed) {
723 case AD_LINK_SPEED_BITMASK_1MBPS:
724 bandwidth = aggregator->num_of_ports;
725 break;
726 case AD_LINK_SPEED_BITMASK_10MBPS:
727 bandwidth = aggregator->num_of_ports * 10;
728 break;
729 case AD_LINK_SPEED_BITMASK_100MBPS:
730 bandwidth = aggregator->num_of_ports * 100;
731 break;
732 case AD_LINK_SPEED_BITMASK_1000MBPS:
733 bandwidth = aggregator->num_of_ports * 1000;
734 break;
735 case AD_LINK_SPEED_BITMASK_10000MBPS:
736 bandwidth = aggregator->num_of_ports * 10000;
737 break;
738 default:
739 bandwidth=0; // to silent the compilor ....
742 return bandwidth;
746 * __get_active_agg - get the current active aggregator
747 * @aggregator: the aggregator we're looking at
750 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
752 struct aggregator *retval = NULL;
754 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
755 if (aggregator->is_active) {
756 retval = aggregator;
757 break;
761 return retval;
765 * __update_lacpdu_from_port - update a port's lacpdu fields
766 * @port: the port we're looking at
769 static inline void __update_lacpdu_from_port(struct port *port)
771 struct lacpdu *lacpdu = &port->lacpdu;
772 const struct port_params *partner = &port->partner_oper;
774 /* update current actual Actor parameters */
775 /* lacpdu->subtype initialized
776 * lacpdu->version_number initialized
777 * lacpdu->tlv_type_actor_info initialized
778 * lacpdu->actor_information_length initialized
781 lacpdu->actor_system_priority = htons(port->actor_system_priority);
782 lacpdu->actor_system = port->actor_system;
783 lacpdu->actor_key = htons(port->actor_oper_port_key);
784 lacpdu->actor_port_priority = htons(port->actor_port_priority);
785 lacpdu->actor_port = htons(port->actor_port_number);
786 lacpdu->actor_state = port->actor_oper_port_state;
788 /* lacpdu->reserved_3_1 initialized
789 * lacpdu->tlv_type_partner_info initialized
790 * lacpdu->partner_information_length initialized
793 lacpdu->partner_system_priority = htons(partner->system_priority);
794 lacpdu->partner_system = partner->system;
795 lacpdu->partner_key = htons(partner->key);
796 lacpdu->partner_port_priority = htons(partner->port_priority);
797 lacpdu->partner_port = htons(partner->port_number);
798 lacpdu->partner_state = partner->port_state;
800 /* lacpdu->reserved_3_2 initialized
801 * lacpdu->tlv_type_collector_info initialized
802 * lacpdu->collector_information_length initialized
803 * collector_max_delay initialized
804 * reserved_12[12] initialized
805 * tlv_type_terminator initialized
806 * terminator_length initialized
807 * reserved_50[50] initialized
811 //////////////////////////////////////////////////////////////////////////////////////
812 // ================= main 802.3ad protocol code ======================================
813 //////////////////////////////////////////////////////////////////////////////////////
816 * ad_lacpdu_send - send out a lacpdu packet on a given port
817 * @port: the port we're looking at
819 * Returns: 0 on success
820 * < 0 on error
822 static int ad_lacpdu_send(struct port *port)
824 struct slave *slave = port->slave;
825 struct sk_buff *skb;
826 struct lacpdu_header *lacpdu_header;
827 int length = sizeof(struct lacpdu_header);
828 struct mac_addr lacpdu_multicast_address = AD_MULTICAST_LACPDU_ADDR;
830 skb = dev_alloc_skb(length);
831 if (!skb) {
832 return -ENOMEM;
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 lacpdu_header->ad_header.destination_address = lacpdu_multicast_address;
844 /* Note: source addres is set to be the member's PERMANENT address, because we use it
845 to identify loopback lacpdus in receive. */
846 lacpdu_header->ad_header.source_address = *((struct mac_addr *)(slave->perm_hwaddr));
847 lacpdu_header->ad_header.length_type = PKT_TYPE_LACPDU;
849 lacpdu_header->lacpdu = port->lacpdu; // struct copy
851 dev_queue_xmit(skb);
853 return 0;
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
862 * < 0 on error
864 static int ad_marker_send(struct port *port, struct bond_marker *marker)
866 struct slave *slave = port->slave;
867 struct sk_buff *skb;
868 struct bond_marker_header *marker_header;
869 int length = sizeof(struct bond_marker_header);
870 struct mac_addr lacpdu_multicast_address = AD_MULTICAST_LACPDU_ADDR;
872 skb = dev_alloc_skb(length + 16);
873 if (!skb) {
874 return -ENOMEM;
877 skb_reserve(skb, 16);
879 skb->dev = slave->dev;
880 skb_reset_mac_header(skb);
881 skb->network_header = skb->mac_header + ETH_HLEN;
882 skb->protocol = PKT_TYPE_LACPDU;
884 marker_header = (struct bond_marker_header *)skb_put(skb, length);
886 marker_header->ad_header.destination_address = lacpdu_multicast_address;
887 /* Note: source addres is set to be the member's PERMANENT address, because we use it
888 to identify loopback MARKERs in receive. */
889 marker_header->ad_header.source_address = *((struct mac_addr *)(slave->perm_hwaddr));
890 marker_header->ad_header.length_type = PKT_TYPE_LACPDU;
892 marker_header->marker = *marker; // struct copy
894 dev_queue_xmit(skb);
896 return 0;
900 * ad_mux_machine - handle a port's mux state machine
901 * @port: the port we're looking at
904 static void ad_mux_machine(struct port *port)
906 mux_states_t last_state;
908 // keep current State Machine state to compare later if it was changed
909 last_state = port->sm_mux_state;
911 if (port->sm_vars & AD_PORT_BEGIN) {
912 port->sm_mux_state = AD_MUX_DETACHED; // next state
913 } else {
914 switch (port->sm_mux_state) {
915 case AD_MUX_DETACHED:
916 if ((port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if SELECTED or STANDBY
917 port->sm_mux_state = AD_MUX_WAITING; // next state
919 break;
920 case AD_MUX_WAITING:
921 // if SELECTED == FALSE return to DETACH state
922 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
923 port->sm_vars &= ~AD_PORT_READY_N;
924 // in order to withhold the Selection Logic to check all ports READY_N value
925 // every callback cycle to update ready variable, we check READY_N and update READY here
926 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
927 port->sm_mux_state = AD_MUX_DETACHED; // next state
928 break;
931 // check if the wait_while_timer expired
932 if (port->sm_mux_timer_counter && !(--port->sm_mux_timer_counter)) {
933 port->sm_vars |= AD_PORT_READY_N;
936 // in order to withhold the selection logic to check all ports READY_N value
937 // every callback cycle to update ready variable, we check READY_N and update READY here
938 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
940 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
941 if ((port->sm_vars & AD_PORT_READY) && !port->sm_mux_timer_counter) {
942 port->sm_mux_state = AD_MUX_ATTACHED; // next state
944 break;
945 case AD_MUX_ATTACHED:
946 // check also if agg_select_timer expired(so the edable port will take place only after this timer)
947 if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) {
948 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
949 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
950 port->sm_vars &= ~AD_PORT_READY_N;
951 // in order to withhold the selection logic to check all ports READY_N value
952 // every callback cycle to update ready variable, we check READY_N and update READY here
953 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
954 port->sm_mux_state = AD_MUX_DETACHED;// next state
956 break;
957 case AD_MUX_COLLECTING_DISTRIBUTING:
958 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
959 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
961 port->sm_mux_state = AD_MUX_ATTACHED;// next state
963 } else {
964 // if port state hasn't changed make
965 // sure that a collecting distributing
966 // port in an active aggregator is enabled
967 if (port->aggregator &&
968 port->aggregator->is_active &&
969 !__port_is_enabled(port)) {
971 __enable_port(port);
974 break;
975 default: //to silence the compiler
976 break;
980 // check if the state machine was changed
981 if (port->sm_mux_state != last_state) {
982 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_mux_state);
983 switch (port->sm_mux_state) {
984 case AD_MUX_DETACHED:
985 __detach_bond_from_agg(port);
986 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
987 ad_disable_collecting_distributing(port);
988 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
989 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
990 port->ntt = 1;
991 break;
992 case AD_MUX_WAITING:
993 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
994 break;
995 case AD_MUX_ATTACHED:
996 __attach_bond_to_agg(port);
997 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
998 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
999 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1000 ad_disable_collecting_distributing(port);
1001 port->ntt = 1;
1002 break;
1003 case AD_MUX_COLLECTING_DISTRIBUTING:
1004 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1005 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
1006 ad_enable_collecting_distributing(port);
1007 port->ntt = 1;
1008 break;
1009 default: //to silence the compiler
1010 break;
1016 * ad_rx_machine - handle a port's rx State Machine
1017 * @lacpdu: the lacpdu we've received
1018 * @port: the port we're looking at
1020 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1021 * CURRENT. If timer expired set the state machine in the proper state.
1022 * In other cases, this function checks if we need to switch to other state.
1024 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1026 rx_states_t last_state;
1028 // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback)
1029 __get_rx_machine_lock(port);
1031 // keep current State Machine state to compare later if it was changed
1032 last_state = port->sm_rx_state;
1034 // check if state machine should change state
1035 // first, check if port was reinitialized
1036 if (port->sm_vars & AD_PORT_BEGIN) {
1037 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1039 // check if port is not enabled
1040 else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED)) {
1041 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1043 // check if new lacpdu arrived
1044 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1045 port->sm_rx_timer_counter = 0; // zero timer
1046 port->sm_rx_state = AD_RX_CURRENT;
1047 } else {
1048 // if timer is on, and if it is expired
1049 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1050 switch (port->sm_rx_state) {
1051 case AD_RX_EXPIRED:
1052 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1053 break;
1054 case AD_RX_CURRENT:
1055 port->sm_rx_state = AD_RX_EXPIRED; // next state
1056 break;
1057 default: //to silence the compiler
1058 break;
1060 } else {
1061 // if no lacpdu arrived and no timer is on
1062 switch (port->sm_rx_state) {
1063 case AD_RX_PORT_DISABLED:
1064 if (port->sm_vars & AD_PORT_MOVED) {
1065 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1066 } else if (port->is_enabled && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1067 port->sm_rx_state = AD_RX_EXPIRED; // next state
1068 } else if (port->is_enabled && ((port->sm_vars & AD_PORT_LACP_ENABLED) == 0)) {
1069 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
1071 break;
1072 default: //to silence the compiler
1073 break;
1079 // check if the State machine was changed or new lacpdu arrived
1080 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1081 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_rx_state);
1082 switch (port->sm_rx_state) {
1083 case AD_RX_INITIALIZE:
1084 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1085 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1086 } else {
1087 port->sm_vars |= AD_PORT_LACP_ENABLED;
1089 port->sm_vars &= ~AD_PORT_SELECTED;
1090 __record_default(port);
1091 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1092 port->sm_vars &= ~AD_PORT_MOVED;
1093 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1095 /*- Fall Through -*/
1097 case AD_RX_PORT_DISABLED:
1098 port->sm_vars &= ~AD_PORT_MATCHED;
1099 break;
1100 case AD_RX_LACP_DISABLED:
1101 port->sm_vars &= ~AD_PORT_SELECTED;
1102 __record_default(port);
1103 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1104 port->sm_vars |= AD_PORT_MATCHED;
1105 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1106 break;
1107 case AD_RX_EXPIRED:
1108 //Reset of the Synchronization flag. (Standard 43.4.12)
1109 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1110 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
1111 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1112 port->sm_vars &= ~AD_PORT_MATCHED;
1113 port->partner_oper.port_state |= AD_SHORT_TIMEOUT;
1114 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1115 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1116 break;
1117 case AD_RX_DEFAULTED:
1118 __update_default_selected(port);
1119 __record_default(port);
1120 port->sm_vars |= AD_PORT_MATCHED;
1121 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1122 break;
1123 case AD_RX_CURRENT:
1124 // detect loopback situation
1125 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1126 // INFO_RECEIVED_LOOPBACK_FRAMES
1127 printk(KERN_ERR DRV_NAME ": %s: An illegal loopback occurred on "
1128 "adapter (%s). Check the configuration to verify that all "
1129 "Adapters are connected to 802.3ad compliant switch ports\n",
1130 port->slave->dev->master->name, port->slave->dev->name);
1131 __release_rx_machine_lock(port);
1132 return;
1134 __update_selected(lacpdu, port);
1135 __update_ntt(lacpdu, port);
1136 __record_pdu(lacpdu, port);
1137 __choose_matched(lacpdu, port);
1138 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1139 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1140 // verify that if the aggregator is enabled, the port is enabled too.
1141 //(because if the link goes down for a short time, the 802.3ad will not
1142 // catch it, and the port will continue to be disabled)
1143 if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) {
1144 __enable_port(port);
1146 break;
1147 default: //to silence the compiler
1148 break;
1151 __release_rx_machine_lock(port);
1155 * ad_tx_machine - handle a port's tx state machine
1156 * @port: the port we're looking at
1159 static void ad_tx_machine(struct port *port)
1161 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1162 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1163 // check if there is something to send
1164 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1165 __update_lacpdu_from_port(port);
1166 // send the lacpdu
1167 if (ad_lacpdu_send(port) >= 0) {
1168 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 demanded
1170 port->ntt = 0;
1173 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
1174 port->sm_tx_timer_counter=ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1179 * ad_periodic_machine - handle a port's periodic state machine
1180 * @port: the port we're looking at
1182 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1184 static void ad_periodic_machine(struct port *port)
1186 periodic_states_t last_state;
1188 // keep current state machine state to compare later if it was changed
1189 last_state = port->sm_periodic_state;
1191 // check if port was reinitialized
1192 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1193 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1195 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1197 // check if state machine should change state
1198 else if (port->sm_periodic_timer_counter) {
1199 // check if periodic state machine expired
1200 if (!(--port->sm_periodic_timer_counter)) {
1201 // if expired then do tx
1202 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1203 } else {
1204 // If not expired, check if there is some new timeout parameter from the partner state
1205 switch (port->sm_periodic_state) {
1206 case AD_FAST_PERIODIC:
1207 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1208 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1210 break;
1211 case AD_SLOW_PERIODIC:
1212 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1213 // stop current timer
1214 port->sm_periodic_timer_counter = 0;
1215 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1217 break;
1218 default: //to silence the compiler
1219 break;
1222 } else {
1223 switch (port->sm_periodic_state) {
1224 case AD_NO_PERIODIC:
1225 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1226 break;
1227 case AD_PERIODIC_TX:
1228 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1229 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1230 } else {
1231 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1233 break;
1234 default: //to silence the compiler
1235 break;
1239 // check if the state machine was changed
1240 if (port->sm_periodic_state != last_state) {
1241 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_periodic_state);
1242 switch (port->sm_periodic_state) {
1243 case AD_NO_PERIODIC:
1244 port->sm_periodic_timer_counter = 0; // zero timer
1245 break;
1246 case AD_FAST_PERIODIC:
1247 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
1248 break;
1249 case AD_SLOW_PERIODIC:
1250 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
1251 break;
1252 case AD_PERIODIC_TX:
1253 port->ntt = 1;
1254 break;
1255 default: //to silence the compiler
1256 break;
1262 * ad_port_selection_logic - select aggregation groups
1263 * @port: the port we're looking at
1265 * Select aggregation groups, and assign each port for it's aggregetor. The
1266 * selection logic is called in the inititalization (after all the handshkes),
1267 * and after every lacpdu receive (if selected is off).
1269 static void ad_port_selection_logic(struct port *port)
1271 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1272 struct port *last_port = NULL, *curr_port;
1273 int found = 0;
1275 // if the port is already Selected, do nothing
1276 if (port->sm_vars & AD_PORT_SELECTED) {
1277 return;
1280 // if the port is connected to other aggregator, detach it
1281 if (port->aggregator) {
1282 // detach the port from its former aggregator
1283 temp_aggregator=port->aggregator;
1284 for (curr_port=temp_aggregator->lag_ports; curr_port; last_port=curr_port, curr_port=curr_port->next_port_in_aggregator) {
1285 if (curr_port == port) {
1286 temp_aggregator->num_of_ports--;
1287 if (!last_port) {// if it is the first port attached to the aggregator
1288 temp_aggregator->lag_ports=port->next_port_in_aggregator;
1289 } else {// not the first port attached to the aggregator
1290 last_port->next_port_in_aggregator=port->next_port_in_aggregator;
1293 // clear the port's relations to this aggregator
1294 port->aggregator = NULL;
1295 port->next_port_in_aggregator=NULL;
1296 port->actor_port_aggregator_identifier=0;
1298 pr_debug("Port %d left LAG %d\n", port->actor_port_number, temp_aggregator->aggregator_identifier);
1299 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1300 if (!temp_aggregator->lag_ports) {
1301 ad_clear_agg(temp_aggregator);
1303 break;
1306 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
1307 printk(KERN_WARNING DRV_NAME ": %s: Warning: Port %d (on %s) was "
1308 "related to aggregator %d but was not on its port list\n",
1309 port->slave->dev->master->name,
1310 port->actor_port_number, port->slave->dev->name,
1311 port->aggregator->aggregator_identifier);
1314 // search on all aggregators for a suitable aggregator for this port
1315 for (aggregator = __get_first_agg(port); aggregator;
1316 aggregator = __get_next_agg(aggregator)) {
1318 // keep a free aggregator for later use(if needed)
1319 if (!aggregator->lag_ports) {
1320 if (!free_aggregator) {
1321 free_aggregator=aggregator;
1323 continue;
1325 // check if current aggregator suits us
1326 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
1327 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1328 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1329 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1330 ) &&
1331 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
1332 !aggregator->is_individual) // but is not individual OR
1335 // attach to the founded aggregator
1336 port->aggregator = aggregator;
1337 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1338 port->next_port_in_aggregator=aggregator->lag_ports;
1339 port->aggregator->num_of_ports++;
1340 aggregator->lag_ports=port;
1341 pr_debug("Port %d joined LAG %d(existing LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1343 // mark this port as selected
1344 port->sm_vars |= AD_PORT_SELECTED;
1345 found = 1;
1346 break;
1350 // the port couldn't find an aggregator - attach it to a new aggregator
1351 if (!found) {
1352 if (free_aggregator) {
1353 // assign port a new aggregator
1354 port->aggregator = free_aggregator;
1355 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1357 // update the new aggregator's parameters
1358 // if port was responsed from the end-user
1359 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
1360 port->aggregator->is_individual = 0;
1361 } else {
1362 port->aggregator->is_individual = 1;
1365 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1366 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
1367 port->aggregator->partner_system=port->partner_oper.system;
1368 port->aggregator->partner_system_priority = port->partner_oper.system_priority;
1369 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1370 port->aggregator->receive_state = 1;
1371 port->aggregator->transmit_state = 1;
1372 port->aggregator->lag_ports = port;
1373 port->aggregator->num_of_ports++;
1375 // mark this port as selected
1376 port->sm_vars |= AD_PORT_SELECTED;
1378 pr_debug("Port %d joined LAG %d(new LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1379 } else {
1380 printk(KERN_ERR DRV_NAME ": %s: Port %d (on %s) did not find a suitable aggregator\n",
1381 port->slave->dev->master->name,
1382 port->actor_port_number, port->slave->dev->name);
1385 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1386 // else set ready=FALSE in all aggregator's ports
1387 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1389 aggregator = __get_first_agg(port);
1390 ad_agg_selection_logic(aggregator);
1394 * Decide if "agg" is a better choice for the new active aggregator that
1395 * the current best, according to the ad_select policy.
1397 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1398 struct aggregator *curr)
1401 * 0. If no best, select current.
1403 * 1. If the current agg is not individual, and the best is
1404 * individual, select current.
1406 * 2. If current agg is individual and the best is not, keep best.
1408 * 3. Therefore, current and best are both individual or both not
1409 * individual, so:
1411 * 3a. If current agg partner replied, and best agg partner did not,
1412 * select current.
1414 * 3b. If current agg partner did not reply and best agg partner
1415 * did reply, keep best.
1417 * 4. Therefore, current and best both have partner replies or
1418 * both do not, so perform selection policy:
1420 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1421 * select by bandwidth.
1423 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1425 if (!best)
1426 return curr;
1428 if (!curr->is_individual && best->is_individual)
1429 return curr;
1431 if (curr->is_individual && !best->is_individual)
1432 return best;
1434 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1435 return curr;
1437 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1438 return best;
1440 switch (__get_agg_selection_mode(curr->lag_ports)) {
1441 case BOND_AD_COUNT:
1442 if (curr->num_of_ports > best->num_of_ports)
1443 return curr;
1445 if (curr->num_of_ports < best->num_of_ports)
1446 return best;
1448 /*FALLTHROUGH*/
1449 case BOND_AD_STABLE:
1450 case BOND_AD_BANDWIDTH:
1451 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1452 return curr;
1454 break;
1456 default:
1457 printk(KERN_WARNING DRV_NAME
1458 ": %s: Impossible agg select mode %d\n",
1459 curr->slave->dev->master->name,
1460 __get_agg_selection_mode(curr->lag_ports));
1461 break;
1464 return best;
1468 * ad_agg_selection_logic - select an aggregation group for a team
1469 * @aggregator: the aggregator we're looking at
1471 * It is assumed that only one aggregator may be selected for a team.
1473 * The logic of this function is to select the aggregator according to
1474 * the ad_select policy:
1476 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1477 * it, and to reselect the active aggregator only if the previous
1478 * aggregator has no more ports related to it.
1480 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1481 * bandwidth, and reselect whenever a link state change takes place or the
1482 * set of slaves in the bond changes.
1484 * BOND_AD_COUNT: select the aggregator with largest number of ports
1485 * (slaves), and reselect whenever a link state change takes place or the
1486 * set of slaves in the bond changes.
1488 * FIXME: this function MUST be called with the first agg in the bond, or
1489 * __get_active_agg() won't work correctly. This function should be better
1490 * called with the bond itself, and retrieve the first agg from it.
1492 static void ad_agg_selection_logic(struct aggregator *agg)
1494 struct aggregator *best, *active, *origin;
1495 struct port *port;
1497 origin = agg;
1499 active = __get_active_agg(agg);
1500 best = active;
1502 do {
1503 agg->is_active = 0;
1505 if (agg->num_of_ports)
1506 best = ad_agg_selection_test(best, agg);
1508 } while ((agg = __get_next_agg(agg)));
1510 if (best &&
1511 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1513 * For the STABLE policy, don't replace the old active
1514 * aggregator if it's still active (it has an answering
1515 * partner) or if both the best and active don't have an
1516 * answering partner.
1518 if (active && active->lag_ports &&
1519 active->lag_ports->is_enabled &&
1520 (__agg_has_partner(active) ||
1521 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1522 if (!(!active->actor_oper_aggregator_key &&
1523 best->actor_oper_aggregator_key)) {
1524 best = NULL;
1525 active->is_active = 1;
1530 if (best && (best == active)) {
1531 best = NULL;
1532 active->is_active = 1;
1535 // if there is new best aggregator, activate it
1536 if (best) {
1537 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1538 best->aggregator_identifier, best->num_of_ports,
1539 best->actor_oper_aggregator_key,
1540 best->partner_oper_aggregator_key,
1541 best->is_individual, best->is_active);
1542 pr_debug("best ports %p slave %p %s\n",
1543 best->lag_ports, best->slave,
1544 best->slave ? best->slave->dev->name : "NULL");
1546 for (agg = __get_first_agg(best->lag_ports); agg;
1547 agg = __get_next_agg(agg)) {
1549 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1550 agg->aggregator_identifier, agg->num_of_ports,
1551 agg->actor_oper_aggregator_key,
1552 agg->partner_oper_aggregator_key,
1553 agg->is_individual, agg->is_active);
1556 // check if any partner replys
1557 if (best->is_individual) {
1558 printk(KERN_WARNING DRV_NAME ": %s: Warning: No 802.3ad"
1559 " response from the link partner for any"
1560 " adapters in the bond\n",
1561 best->slave->dev->master->name);
1564 best->is_active = 1;
1565 pr_debug("LAG %d chosen as the active LAG\n",
1566 best->aggregator_identifier);
1567 pr_debug("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);
1573 // disable the ports that were related to the former active_aggregator
1574 if (active) {
1575 for (port = active->lag_ports; port;
1576 port = port->next_port_in_aggregator) {
1577 __disable_port(port);
1583 * if the selected aggregator is of join individuals
1584 * (partner_system is NULL), enable their ports
1586 active = __get_active_agg(origin);
1588 if (active) {
1589 if (!__agg_has_partner(active)) {
1590 for (port = active->lag_ports; port;
1591 port = port->next_port_in_aggregator) {
1592 __enable_port(port);
1597 if (origin->slave) {
1598 struct bonding *bond;
1600 bond = bond_get_bond_by_slave(origin->slave);
1601 if (bond)
1602 bond_3ad_set_carrier(bond);
1607 * ad_clear_agg - clear a given aggregator's parameters
1608 * @aggregator: the aggregator we're looking at
1611 static void ad_clear_agg(struct aggregator *aggregator)
1613 if (aggregator) {
1614 aggregator->is_individual = 0;
1615 aggregator->actor_admin_aggregator_key = 0;
1616 aggregator->actor_oper_aggregator_key = 0;
1617 aggregator->partner_system = null_mac_addr;
1618 aggregator->partner_system_priority = 0;
1619 aggregator->partner_oper_aggregator_key = 0;
1620 aggregator->receive_state = 0;
1621 aggregator->transmit_state = 0;
1622 aggregator->lag_ports = NULL;
1623 aggregator->is_active = 0;
1624 aggregator->num_of_ports = 0;
1625 pr_debug("LAG %d was cleared\n", aggregator->aggregator_identifier);
1630 * ad_initialize_agg - initialize a given aggregator's parameters
1631 * @aggregator: the aggregator we're looking at
1634 static void ad_initialize_agg(struct aggregator *aggregator)
1636 if (aggregator) {
1637 ad_clear_agg(aggregator);
1639 aggregator->aggregator_mac_address = null_mac_addr;
1640 aggregator->aggregator_identifier = 0;
1641 aggregator->slave = NULL;
1646 * ad_initialize_port - initialize a given port's parameters
1647 * @aggregator: the aggregator we're looking at
1648 * @lacp_fast: boolean. whether fast periodic should be used
1651 static void ad_initialize_port(struct port *port, int lacp_fast)
1653 static const struct port_params tmpl = {
1654 .system_priority = 0xffff,
1655 .key = 1,
1656 .port_number = 1,
1657 .port_priority = 0xff,
1658 .port_state = 1,
1661 if (port) {
1662 port->actor_port_number = 1;
1663 port->actor_port_priority = 0xff;
1664 port->actor_system = null_mac_addr;
1665 port->actor_system_priority = 0xffff;
1666 port->actor_port_aggregator_identifier = 0;
1667 port->ntt = 0;
1668 port->actor_admin_port_key = 1;
1669 port->actor_oper_port_key = 1;
1670 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1671 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1673 if (lacp_fast) {
1674 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1677 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1678 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1680 port->is_enabled = 1;
1681 // ****** private parameters ******
1682 port->sm_vars = 0x3;
1683 port->sm_rx_state = 0;
1684 port->sm_rx_timer_counter = 0;
1685 port->sm_periodic_state = 0;
1686 port->sm_periodic_timer_counter = 0;
1687 port->sm_mux_state = 0;
1688 port->sm_mux_timer_counter = 0;
1689 port->sm_tx_state = 0;
1690 port->sm_tx_timer_counter = 0;
1691 port->slave = NULL;
1692 port->aggregator = NULL;
1693 port->next_port_in_aggregator = NULL;
1694 port->transaction_id = 0;
1696 ad_initialize_lacpdu(&(port->lacpdu));
1701 * ad_enable_collecting_distributing - enable a port's transmit/receive
1702 * @port: the port we're looking at
1704 * Enable @port if it's in an active aggregator
1706 static void ad_enable_collecting_distributing(struct port *port)
1708 if (port->aggregator->is_active) {
1709 pr_debug("Enabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1710 __enable_port(port);
1715 * ad_disable_collecting_distributing - disable a port's transmit/receive
1716 * @port: the port we're looking at
1719 static void ad_disable_collecting_distributing(struct port *port)
1721 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
1722 pr_debug("Disabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
1723 __disable_port(port);
1727 #if 0
1729 * ad_marker_info_send - send a marker information frame
1730 * @port: the port we're looking at
1732 * This function does nothing since we decided not to implement send and handle
1733 * response for marker PDU's, in this stage, but only to respond to marker
1734 * information.
1736 static void ad_marker_info_send(struct port *port)
1738 struct bond_marker marker;
1739 u16 index;
1741 // fill the marker PDU with the appropriate values
1742 marker.subtype = 0x02;
1743 marker.version_number = 0x01;
1744 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1745 marker.marker_length = 0x16;
1746 // convert requester_port to Big Endian
1747 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1748 marker.requester_system = port->actor_system;
1749 // convert requester_port(u32) to Big Endian
1750 marker.requester_transaction_id = (((++port->transaction_id & 0xFF) << 24) |((port->transaction_id & 0xFF00) << 8) |((port->transaction_id & 0xFF0000) >> 8) |((port->transaction_id & 0xFF000000) >> 24));
1751 marker.pad = 0;
1752 marker.tlv_type_terminator = 0x00;
1753 marker.terminator_length = 0x00;
1754 for (index=0; index<90; index++) {
1755 marker.reserved_90[index]=0;
1758 // send the marker information
1759 if (ad_marker_send(port, &marker) >= 0) {
1760 pr_debug("Sent Marker Information on port %d\n", port->actor_port_number);
1763 #endif
1766 * ad_marker_info_received - handle receive of a Marker information frame
1767 * @marker_info: Marker info received
1768 * @port: the port we're looking at
1771 static void ad_marker_info_received(struct bond_marker *marker_info,
1772 struct port *port)
1774 struct bond_marker marker;
1776 // copy the received marker data to the response marker
1777 //marker = *marker_info;
1778 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1779 // change the marker subtype to marker response
1780 marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE;
1781 // send the marker response
1783 if (ad_marker_send(port, &marker) >= 0) {
1784 pr_debug("Sent Marker Response on port %d\n", port->actor_port_number);
1789 * ad_marker_response_received - handle receive of a marker response frame
1790 * @marker: marker PDU received
1791 * @port: the port we're looking at
1793 * This function does nothing since we decided not to implement send and handle
1794 * response for marker PDU's, in this stage, but only to respond to marker
1795 * information.
1797 static void ad_marker_response_received(struct bond_marker *marker,
1798 struct port *port)
1800 marker=NULL; // just to satisfy the compiler
1801 port=NULL; // just to satisfy the compiler
1802 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1806 * ad_initialize_lacpdu - initialize a given lacpdu structure
1807 * @lacpdu: lacpdu structure to initialize
1810 static void ad_initialize_lacpdu(struct lacpdu *lacpdu)
1812 u16 index;
1814 // initialize lacpdu data
1815 lacpdu->subtype = 0x01;
1816 lacpdu->version_number = 0x01;
1817 lacpdu->tlv_type_actor_info = 0x01;
1818 lacpdu->actor_information_length = 0x14;
1819 // lacpdu->actor_system_priority updated on send
1820 // lacpdu->actor_system updated on send
1821 // lacpdu->actor_key updated on send
1822 // lacpdu->actor_port_priority updated on send
1823 // lacpdu->actor_port updated on send
1824 // lacpdu->actor_state updated on send
1825 lacpdu->tlv_type_partner_info = 0x02;
1826 lacpdu->partner_information_length = 0x14;
1827 for (index=0; index<=2; index++) {
1828 lacpdu->reserved_3_1[index]=0;
1830 // lacpdu->partner_system_priority updated on send
1831 // lacpdu->partner_system updated on send
1832 // lacpdu->partner_key updated on send
1833 // lacpdu->partner_port_priority updated on send
1834 // lacpdu->partner_port updated on send
1835 // lacpdu->partner_state updated on send
1836 for (index=0; index<=2; index++) {
1837 lacpdu->reserved_3_2[index]=0;
1839 lacpdu->tlv_type_collector_info = 0x03;
1840 lacpdu->collector_information_length= 0x10;
1841 lacpdu->collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY);
1842 for (index=0; index<=11; index++) {
1843 lacpdu->reserved_12[index]=0;
1845 lacpdu->tlv_type_terminator = 0x00;
1846 lacpdu->terminator_length = 0;
1847 for (index=0; index<=49; index++) {
1848 lacpdu->reserved_50[index]=0;
1852 //////////////////////////////////////////////////////////////////////////////////////
1853 // ================= AD exported functions to the main bonding code ==================
1854 //////////////////////////////////////////////////////////////////////////////////////
1856 // Check aggregators status in team every T seconds
1857 #define AD_AGGREGATOR_SELECTION_TIMER 8
1860 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1862 * Set the aggregation selection timer, to initiate an agg selection in
1863 * the very near future. Called during first initialization, and during
1864 * any down to up transitions of the bond.
1866 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1868 BOND_AD_INFO(bond).agg_select_timer = timeout;
1869 BOND_AD_INFO(bond).agg_select_mode = bond->params.ad_select;
1872 static u16 aggregator_identifier;
1875 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1876 * @bond: bonding struct to work on
1877 * @tick_resolution: tick duration (millisecond resolution)
1878 * @lacp_fast: boolean. whether fast periodic should be used
1880 * Can be called only after the mac address of the bond is set.
1882 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
1884 // check that the bond is not initialized yet
1885 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr), &(bond->dev->dev_addr))) {
1887 aggregator_identifier = 0;
1889 BOND_AD_INFO(bond).lacp_fast = lacp_fast;
1890 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1891 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1893 // initialize how many times this module is called in one second(should be about every 100ms)
1894 ad_ticks_per_sec = tick_resolution;
1896 bond_3ad_initiate_agg_selection(bond,
1897 AD_AGGREGATOR_SELECTION_TIMER *
1898 ad_ticks_per_sec);
1903 * bond_3ad_bind_slave - initialize a slave's port
1904 * @slave: slave struct to work on
1906 * Returns: 0 on success
1907 * < 0 on error
1909 int bond_3ad_bind_slave(struct slave *slave)
1911 struct bonding *bond = bond_get_bond_by_slave(slave);
1912 struct port *port;
1913 struct aggregator *aggregator;
1915 if (bond == NULL) {
1916 printk(KERN_ERR DRV_NAME ": %s: The slave %s is not attached to its bond\n",
1917 slave->dev->master->name, slave->dev->name);
1918 return -1;
1921 //check that the slave has not been intialized yet.
1922 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1924 // port initialization
1925 port = &(SLAVE_AD_INFO(slave).port);
1927 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
1929 port->slave = slave;
1930 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1931 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1932 // ------------------------------------------------------------
1933 // Port key : | User key | Speed |Duplex|
1934 // ------------------------------------------------------------
1935 // 16 6 1 0
1936 port->actor_admin_port_key = 0; // initialize this parameter
1937 port->actor_admin_port_key |= __get_duplex(port);
1938 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1939 port->actor_oper_port_key = port->actor_admin_port_key;
1940 // if the port is not full duplex, then the port should be not lacp Enabled
1941 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1942 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1944 // actor system is the bond's system
1945 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1946 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1947 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1948 port->aggregator = NULL;
1949 port->next_port_in_aggregator = NULL;
1951 __disable_port(port);
1952 __initialize_port_locks(port);
1955 // aggregator initialization
1956 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1958 ad_initialize_agg(aggregator);
1960 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1961 aggregator->aggregator_identifier = (++aggregator_identifier);
1962 aggregator->slave = slave;
1963 aggregator->is_active = 0;
1964 aggregator->num_of_ports = 0;
1967 return 0;
1971 * bond_3ad_unbind_slave - deinitialize a slave's port
1972 * @slave: slave struct to work on
1974 * Search for the aggregator that is related to this port, remove the
1975 * aggregator and assign another aggregator for other port related to it
1976 * (if any), and remove the port.
1978 void bond_3ad_unbind_slave(struct slave *slave)
1980 struct port *port, *prev_port, *temp_port;
1981 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1982 int select_new_active_agg = 0;
1984 // find the aggregator related to this slave
1985 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1987 // find the port related to this slave
1988 port = &(SLAVE_AD_INFO(slave).port);
1990 // if slave is null, the whole port is not initialized
1991 if (!port->slave) {
1992 printk(KERN_WARNING DRV_NAME ": Warning: %s: Trying to "
1993 "unbind an uninitialized port on %s\n",
1994 slave->dev->master->name, slave->dev->name);
1995 return;
1998 pr_debug("Unbinding Link Aggregation Group %d\n", aggregator->aggregator_identifier);
2000 /* Tell the partner that this port is not suitable for aggregation */
2001 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2002 __update_lacpdu_from_port(port);
2003 ad_lacpdu_send(port);
2005 // check if this aggregator is occupied
2006 if (aggregator->lag_ports) {
2007 // check if there are other ports related to this aggregator except
2008 // the port related to this slave(thats ensure us that there is a
2009 // reason to search for new aggregator, and that we will find one
2010 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
2011 // find new aggregator for the related port(s)
2012 new_aggregator = __get_first_agg(port);
2013 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
2014 // if the new aggregator is empty, or it connected to to our port only
2015 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
2016 break;
2019 // if new aggregator found, copy the aggregator's parameters
2020 // and connect the related lag_ports to the new aggregator
2021 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2022 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator->aggregator_identifier, new_aggregator->aggregator_identifier);
2024 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
2025 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2026 aggregator->slave->dev->master->name);
2027 // select new active aggregator
2028 select_new_active_agg = 1;
2031 new_aggregator->is_individual = aggregator->is_individual;
2032 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2033 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2034 new_aggregator->partner_system = aggregator->partner_system;
2035 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2036 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2037 new_aggregator->receive_state = aggregator->receive_state;
2038 new_aggregator->transmit_state = aggregator->transmit_state;
2039 new_aggregator->lag_ports = aggregator->lag_ports;
2040 new_aggregator->is_active = aggregator->is_active;
2041 new_aggregator->num_of_ports = aggregator->num_of_ports;
2043 // update the information that is written on the ports about the aggregator
2044 for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) {
2045 temp_port->aggregator=new_aggregator;
2046 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2049 // clear the aggregator
2050 ad_clear_agg(aggregator);
2052 if (select_new_active_agg) {
2053 ad_agg_selection_logic(__get_first_agg(port));
2055 } else {
2056 printk(KERN_WARNING DRV_NAME ": %s: Warning: unbinding aggregator, "
2057 "and could not find a new aggregator for its ports\n",
2058 slave->dev->master->name);
2060 } else { // in case that the only port related to this aggregator is the one we want to remove
2061 select_new_active_agg = aggregator->is_active;
2062 // clear the aggregator
2063 ad_clear_agg(aggregator);
2064 if (select_new_active_agg) {
2065 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2066 slave->dev->master->name);
2067 // select new active aggregator
2068 ad_agg_selection_logic(__get_first_agg(port));
2073 pr_debug("Unbinding port %d\n", port->actor_port_number);
2074 // find the aggregator that this port is connected to
2075 temp_aggregator = __get_first_agg(port);
2076 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2077 prev_port = NULL;
2078 // search the port in the aggregator's related ports
2079 for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) {
2080 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2081 if (prev_port) {
2082 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2083 } else {
2084 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2086 temp_aggregator->num_of_ports--;
2087 if (temp_aggregator->num_of_ports==0) {
2088 select_new_active_agg = temp_aggregator->is_active;
2089 // clear the aggregator
2090 ad_clear_agg(temp_aggregator);
2091 if (select_new_active_agg) {
2092 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2093 slave->dev->master->name);
2094 // select new active aggregator
2095 ad_agg_selection_logic(__get_first_agg(port));
2098 break;
2102 port->slave=NULL;
2106 * bond_3ad_state_machine_handler - handle state machines timeout
2107 * @bond: bonding struct to work on
2109 * The state machine handling concept in this module is to check every tick
2110 * which state machine should operate any function. The execution order is
2111 * round robin, so when we have an interaction between state machines, the
2112 * reply of one to each other might be delayed until next tick.
2114 * This function also complete the initialization when the agg_select_timer
2115 * times out, and it selects an aggregator for the ports that are yet not
2116 * related to any aggregator, and selects the active aggregator for a bond.
2118 void bond_3ad_state_machine_handler(struct work_struct *work)
2120 struct bonding *bond = container_of(work, struct bonding,
2121 ad_work.work);
2122 struct port *port;
2123 struct aggregator *aggregator;
2125 read_lock(&bond->lock);
2127 if (bond->kill_timers) {
2128 goto out;
2131 //check if there are any slaves
2132 if (bond->slave_cnt == 0) {
2133 goto re_arm;
2136 // check if agg_select_timer timer after initialize is timed out
2137 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2138 // select the active aggregator for the bond
2139 if ((port = __get_first_port(bond))) {
2140 if (!port->slave) {
2141 printk(KERN_WARNING DRV_NAME ": %s: Warning: bond's first port is "
2142 "uninitialized\n", bond->dev->name);
2143 goto re_arm;
2146 aggregator = __get_first_agg(port);
2147 ad_agg_selection_logic(aggregator);
2149 bond_3ad_set_carrier(bond);
2152 // for each port run the state machines
2153 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2154 if (!port->slave) {
2155 printk(KERN_WARNING DRV_NAME ": %s: Warning: Found an uninitialized "
2156 "port\n", bond->dev->name);
2157 goto re_arm;
2160 ad_rx_machine(NULL, port);
2161 ad_periodic_machine(port);
2162 ad_port_selection_logic(port);
2163 ad_mux_machine(port);
2164 ad_tx_machine(port);
2166 // turn off the BEGIN bit, since we already handled it
2167 if (port->sm_vars & AD_PORT_BEGIN) {
2168 port->sm_vars &= ~AD_PORT_BEGIN;
2172 re_arm:
2173 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2174 out:
2175 read_unlock(&bond->lock);
2179 * bond_3ad_rx_indication - handle a received frame
2180 * @lacpdu: received lacpdu
2181 * @slave: slave struct to work on
2182 * @length: length of the data received
2184 * It is assumed that frames that were sent on this NIC don't returned as new
2185 * received frames (loopback). Since only the payload is given to this
2186 * function, it check for loopback.
2188 static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
2190 struct port *port;
2192 if (length >= sizeof(struct lacpdu)) {
2194 port = &(SLAVE_AD_INFO(slave).port);
2196 if (!port->slave) {
2197 printk(KERN_WARNING DRV_NAME ": %s: Warning: port of slave %s is "
2198 "uninitialized\n", slave->dev->name, slave->dev->master->name);
2199 return;
2202 switch (lacpdu->subtype) {
2203 case AD_TYPE_LACPDU:
2204 pr_debug("Received LACPDU on port %d\n", port->actor_port_number);
2205 ad_rx_machine(lacpdu, port);
2206 break;
2208 case AD_TYPE_MARKER:
2209 // No need to convert fields to Little Endian since we don't use the marker's fields.
2211 switch (((struct bond_marker *)lacpdu)->tlv_type) {
2212 case AD_MARKER_INFORMATION_SUBTYPE:
2213 pr_debug("Received Marker Information on port %d\n", port->actor_port_number);
2214 ad_marker_info_received((struct bond_marker *)lacpdu, port);
2215 break;
2217 case AD_MARKER_RESPONSE_SUBTYPE:
2218 pr_debug("Received Marker Response on port %d\n", port->actor_port_number);
2219 ad_marker_response_received((struct bond_marker *)lacpdu, port);
2220 break;
2222 default:
2223 pr_debug("Received an unknown Marker subtype on slot %d\n", port->actor_port_number);
2230 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2231 * @slave: slave struct to work on
2233 * Handle reselection of aggregator (if needed) for this port.
2235 void bond_3ad_adapter_speed_changed(struct slave *slave)
2237 struct port *port;
2239 port = &(SLAVE_AD_INFO(slave).port);
2241 // if slave is null, the whole port is not initialized
2242 if (!port->slave) {
2243 printk(KERN_WARNING DRV_NAME ": Warning: %s: speed "
2244 "changed for uninitialized port on %s\n",
2245 slave->dev->master->name, slave->dev->name);
2246 return;
2249 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2250 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2251 pr_debug("Port %d changed speed\n", port->actor_port_number);
2252 // there is no need to reselect a new aggregator, just signal the
2253 // state machines to reinitialize
2254 port->sm_vars |= AD_PORT_BEGIN;
2258 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2259 * @slave: slave struct to work on
2261 * Handle reselection of aggregator (if needed) for this port.
2263 void bond_3ad_adapter_duplex_changed(struct slave *slave)
2265 struct port *port;
2267 port=&(SLAVE_AD_INFO(slave).port);
2269 // if slave is null, the whole port is not initialized
2270 if (!port->slave) {
2271 printk(KERN_WARNING DRV_NAME ": %s: Warning: duplex changed "
2272 "for uninitialized port on %s\n",
2273 slave->dev->master->name, slave->dev->name);
2274 return;
2277 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2278 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2279 pr_debug("Port %d changed duplex\n", port->actor_port_number);
2280 // there is no need to reselect a new aggregator, just signal the
2281 // state machines to reinitialize
2282 port->sm_vars |= AD_PORT_BEGIN;
2286 * bond_3ad_handle_link_change - handle a slave's link status change indication
2287 * @slave: slave struct to work on
2288 * @status: whether the link is now up or down
2290 * Handle reselection of aggregator (if needed) for this port.
2292 void bond_3ad_handle_link_change(struct slave *slave, char link)
2294 struct port *port;
2296 port = &(SLAVE_AD_INFO(slave).port);
2298 // if slave is null, the whole port is not initialized
2299 if (!port->slave) {
2300 printk(KERN_WARNING DRV_NAME ": Warning: %s: link status changed for "
2301 "uninitialized port on %s\n",
2302 slave->dev->master->name, slave->dev->name);
2303 return;
2306 // 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)
2307 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2308 if (link == BOND_LINK_UP) {
2309 port->is_enabled = 1;
2310 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2311 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2312 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2313 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2314 } else {
2315 /* link has failed */
2316 port->is_enabled = 0;
2317 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2318 port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS);
2320 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2321 // there is no need to reselect a new aggregator, just signal the
2322 // state machines to reinitialize
2323 port->sm_vars |= AD_PORT_BEGIN;
2327 * set link state for bonding master: if we have an active
2328 * aggregator, we're up, if not, we're down. Presumes that we cannot
2329 * have an active aggregator if there are no slaves with link up.
2331 * This behavior complies with IEEE 802.3 section 43.3.9.
2333 * Called by bond_set_carrier(). Return zero if carrier state does not
2334 * change, nonzero if it does.
2336 int bond_3ad_set_carrier(struct bonding *bond)
2338 if (__get_active_agg(&(SLAVE_AD_INFO(bond->first_slave).aggregator))) {
2339 if (!netif_carrier_ok(bond->dev)) {
2340 netif_carrier_on(bond->dev);
2341 return 1;
2343 return 0;
2346 if (netif_carrier_ok(bond->dev)) {
2347 netif_carrier_off(bond->dev);
2348 return 1;
2350 return 0;
2354 * bond_3ad_get_active_agg_info - get information of the active aggregator
2355 * @bond: bonding struct to work on
2356 * @ad_info: ad_info struct to fill with the bond's info
2358 * Returns: 0 on success
2359 * < 0 on error
2361 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2363 struct aggregator *aggregator = NULL;
2364 struct port *port;
2366 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2367 if (port->aggregator && port->aggregator->is_active) {
2368 aggregator = port->aggregator;
2369 break;
2373 if (aggregator) {
2374 ad_info->aggregator_id = aggregator->aggregator_identifier;
2375 ad_info->ports = aggregator->num_of_ports;
2376 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2377 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2378 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2379 return 0;
2382 return -1;
2385 int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2387 struct slave *slave, *start_at;
2388 struct bonding *bond = netdev_priv(dev);
2389 int slave_agg_no;
2390 int slaves_in_agg;
2391 int agg_id;
2392 int i;
2393 struct ad_info ad_info;
2394 int res = 1;
2396 /* make sure that the slaves list will
2397 * not change during tx
2399 read_lock(&bond->lock);
2401 if (!BOND_IS_OK(bond)) {
2402 goto out;
2405 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
2406 printk(KERN_DEBUG DRV_NAME ": %s: Error: "
2407 "bond_3ad_get_active_agg_info failed\n", dev->name);
2408 goto out;
2411 slaves_in_agg = ad_info.ports;
2412 agg_id = ad_info.aggregator_id;
2414 if (slaves_in_agg == 0) {
2415 /*the aggregator is empty*/
2416 printk(KERN_DEBUG DRV_NAME ": %s: Error: active "
2417 "aggregator is empty\n",
2418 dev->name);
2419 goto out;
2422 slave_agg_no = bond->xmit_hash_policy(skb, dev, slaves_in_agg);
2424 bond_for_each_slave(bond, slave, i) {
2425 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2427 if (agg && (agg->aggregator_identifier == agg_id)) {
2428 slave_agg_no--;
2429 if (slave_agg_no < 0) {
2430 break;
2435 if (slave_agg_no >= 0) {
2436 printk(KERN_ERR DRV_NAME ": %s: Error: Couldn't find a slave to tx on "
2437 "for aggregator ID %d\n", dev->name, agg_id);
2438 goto out;
2441 start_at = slave;
2443 bond_for_each_slave_from(bond, slave, i, start_at) {
2444 int slave_agg_id = 0;
2445 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2447 if (agg) {
2448 slave_agg_id = agg->aggregator_identifier;
2451 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2452 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2453 break;
2457 out:
2458 if (res) {
2459 /* no suitable interface, frame not sent */
2460 dev_kfree_skb(skb);
2462 read_unlock(&bond->lock);
2463 return 0;
2466 int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype, struct net_device *orig_dev)
2468 struct bonding *bond = netdev_priv(dev);
2469 struct slave *slave = NULL;
2470 int ret = NET_RX_DROP;
2472 if (dev_net(dev) != &init_net)
2473 goto out;
2475 if (!(dev->flags & IFF_MASTER))
2476 goto out;
2478 read_lock(&bond->lock);
2479 slave = bond_get_slave_by_dev((struct bonding *)netdev_priv(dev),
2480 orig_dev);
2481 if (!slave)
2482 goto out_unlock;
2484 bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
2486 ret = NET_RX_SUCCESS;
2488 out_unlock:
2489 read_unlock(&bond->lock);
2490 out:
2491 dev_kfree_skb(skb);
2493 return ret;