2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/skbuff.h>
26 #include <linux/if_ether.h>
27 #include <linux/netdevice.h>
28 #include <linux/spinlock.h>
29 #include <linux/ethtool.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if_bonding.h>
32 #include <linux/pkt_sched.h>
33 #include <net/net_namespace.h>
37 // General definitions
38 #define AD_SHORT_TIMEOUT 1
39 #define AD_LONG_TIMEOUT 0
40 #define AD_STANDBY 0x2
41 #define AD_MAX_TX_IN_SECOND 3
42 #define AD_COLLECTOR_MAX_DELAY 0
44 // Timer definitions(43.4.4 in the 802.3ad standard)
45 #define AD_FAST_PERIODIC_TIME 1
46 #define AD_SLOW_PERIODIC_TIME 30
47 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
48 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
49 #define AD_CHURN_DETECTION_TIME 60
50 #define AD_AGGREGATE_WAIT_TIME 2
52 // Port state definitions(43.4.2.2 in the 802.3ad standard)
53 #define AD_STATE_LACP_ACTIVITY 0x1
54 #define AD_STATE_LACP_TIMEOUT 0x2
55 #define AD_STATE_AGGREGATION 0x4
56 #define AD_STATE_SYNCHRONIZATION 0x8
57 #define AD_STATE_COLLECTING 0x10
58 #define AD_STATE_DISTRIBUTING 0x20
59 #define AD_STATE_DEFAULTED 0x40
60 #define AD_STATE_EXPIRED 0x80
62 // Port Variables definitions used by the State Machines(43.4.7 in the 802.3ad standard)
63 #define AD_PORT_BEGIN 0x1
64 #define AD_PORT_LACP_ENABLED 0x2
65 #define AD_PORT_ACTOR_CHURN 0x4
66 #define AD_PORT_PARTNER_CHURN 0x8
67 #define AD_PORT_READY 0x10
68 #define AD_PORT_READY_N 0x20
69 #define AD_PORT_MATCHED 0x40
70 #define AD_PORT_STANDBY 0x80
71 #define AD_PORT_SELECTED 0x100
72 #define AD_PORT_MOVED 0x200
74 // Port Key definitions
75 // key is determined according to the link speed, duplex and
76 // user key(which is yet not supported)
77 // ------------------------------------------------------------
78 // Port key : | User key | Speed |Duplex|
79 // ------------------------------------------------------------
81 #define AD_DUPLEX_KEY_BITS 0x1
82 #define AD_SPEED_KEY_BITS 0x3E
83 #define AD_USER_KEY_BITS 0xFFC0
86 #define AD_LINK_SPEED_BITMASK_1MBPS 0x1
87 #define AD_LINK_SPEED_BITMASK_10MBPS 0x2
88 #define AD_LINK_SPEED_BITMASK_100MBPS 0x4
89 #define AD_LINK_SPEED_BITMASK_1000MBPS 0x8
90 #define AD_LINK_SPEED_BITMASK_10000MBPS 0x10
93 /* compare MAC addresses */
94 #define MAC_ADDRESS_EQUAL(A, B) \
95 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
97 static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
98 static u16 ad_ticks_per_sec;
99 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
101 static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
103 // ================= main 802.3ad protocol functions ==================
104 static int ad_lacpdu_send(struct port *port);
105 static int ad_marker_send(struct port *port, struct bond_marker *marker);
106 static void ad_mux_machine(struct port *port);
107 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
108 static void ad_tx_machine(struct port *port);
109 static void ad_periodic_machine(struct port *port);
110 static void ad_port_selection_logic(struct port *port);
111 static void ad_agg_selection_logic(struct aggregator *aggregator);
112 static void ad_clear_agg(struct aggregator *aggregator);
113 static void ad_initialize_agg(struct aggregator *aggregator);
114 static void ad_initialize_port(struct port *port, int lacp_fast);
115 static void ad_enable_collecting_distributing(struct port *port);
116 static void ad_disable_collecting_distributing(struct port *port);
117 static void ad_marker_info_received(struct bond_marker *marker_info, struct port *port);
118 static void ad_marker_response_received(struct bond_marker *marker, struct port *port);
121 /////////////////////////////////////////////////////////////////////////////////
122 // ================= api to bonding and kernel code ==================
123 /////////////////////////////////////////////////////////////////////////////////
126 * __get_bond_by_port - get the port's bonding struct
127 * @port: the port we're looking at
129 * Return @port's bonding struct, or %NULL if it can't be found.
131 static inline struct bonding *__get_bond_by_port(struct port *port)
133 if (port->slave == NULL)
136 return bond_get_bond_by_slave(port->slave);
140 * __get_first_agg - get the first aggregator in the bond
141 * @bond: the bond we're looking at
143 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
146 static inline struct aggregator *__get_first_agg(struct port *port)
148 struct bonding *bond = __get_bond_by_port(port);
149 struct slave *first_slave;
151 /* If there's no bond for this port, or bond has no slaves */
155 first_slave = bond_first_slave_rcu(bond);
157 return first_slave ? &(SLAVE_AD_INFO(first_slave).aggregator) : NULL;
163 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
164 * address for the partner). Return 0 if not.
166 static inline int __agg_has_partner(struct aggregator *agg)
168 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
172 * __disable_port - disable the port's slave
173 * @port: the port we're looking at
176 static inline void __disable_port(struct port *port)
178 bond_set_slave_inactive_flags(port->slave);
182 * __enable_port - enable the port's slave, if it's up
183 * @port: the port we're looking at
186 static inline void __enable_port(struct port *port)
188 struct slave *slave = port->slave;
190 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev))
191 bond_set_slave_active_flags(slave);
195 * __port_is_enabled - check if the port's slave is in active state
196 * @port: the port we're looking at
199 static inline int __port_is_enabled(struct port *port)
201 return bond_is_active_slave(port->slave);
205 * __get_agg_selection_mode - get the aggregator selection mode
206 * @port: the port we're looking at
208 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
210 static inline u32 __get_agg_selection_mode(struct port *port)
212 struct bonding *bond = __get_bond_by_port(port);
215 return BOND_AD_STABLE;
217 return bond->params.ad_select;
221 * __check_agg_selection_timer - check if the selection timer has expired
222 * @port: the port we're looking at
225 static inline int __check_agg_selection_timer(struct port *port)
227 struct bonding *bond = __get_bond_by_port(port);
232 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
236 * __get_state_machine_lock - lock the port's state machines
237 * @port: the port we're looking at
240 static inline void __get_state_machine_lock(struct port *port)
242 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
246 * __release_state_machine_lock - unlock the port's state machines
247 * @port: the port we're looking at
250 static inline void __release_state_machine_lock(struct port *port)
252 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
256 * __get_link_speed - get a port's speed
257 * @port: the port we're looking at
259 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
261 * %AD_LINK_SPEED_BITMASK_10MBPS,
262 * %AD_LINK_SPEED_BITMASK_100MBPS,
263 * %AD_LINK_SPEED_BITMASK_1000MBPS,
264 * %AD_LINK_SPEED_BITMASK_10000MBPS
266 static u16 __get_link_speed(struct port *port)
268 struct slave *slave = port->slave;
271 /* this if covers only a special case: when the configuration starts with
272 * link down, it sets the speed to 0.
273 * This is done in spite of the fact that the e100 driver reports 0 to be
274 * compatible with MVT in the future.*/
275 if (slave->link != BOND_LINK_UP)
278 switch (slave->speed) {
280 speed = AD_LINK_SPEED_BITMASK_10MBPS;
284 speed = AD_LINK_SPEED_BITMASK_100MBPS;
288 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
292 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
296 speed = 0; // unknown speed value from ethtool. shouldn't happen
301 pr_debug("Port %d Received link speed %d update from adapter\n",
302 port->actor_port_number, speed);
307 * __get_duplex - get a port's duplex
308 * @port: the port we're looking at
310 * Return @port's duplex in 802.3ad bitmask format. i.e.:
311 * 0x01 if in full duplex
314 static u8 __get_duplex(struct port *port)
316 struct slave *slave = port->slave;
320 // handling a special case: when the configuration starts with
321 // link down, it sets the duplex to 0.
322 if (slave->link != BOND_LINK_UP)
325 switch (slave->duplex) {
328 pr_debug("Port %d Received status full duplex update from adapter\n",
329 port->actor_port_number);
334 pr_debug("Port %d Received status NOT full duplex update from adapter\n",
335 port->actor_port_number);
343 * __initialize_port_locks - initialize a port's STATE machine spinlock
344 * @port: the slave of the port we're looking at
347 static inline void __initialize_port_locks(struct slave *slave)
349 // make sure it isn't called twice
350 spin_lock_init(&(SLAVE_AD_INFO(slave).state_machine_lock));
356 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
357 * @timer_type: which timer to operate
358 * @par: timer parameter. see below
360 * If @timer_type is %current_while_timer, @par indicates long/short timer.
361 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
362 * %SLOW_PERIODIC_TIME.
364 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
366 u16 retval = 0; /* to silence the compiler */
368 switch (timer_type) {
369 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
371 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
373 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
375 case AD_ACTOR_CHURN_TIMER: // for local churn machine
376 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
378 case AD_PERIODIC_TIMER: // for periodic machine
379 retval = (par*ad_ticks_per_sec); // long timeout
381 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
382 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
384 case AD_WAIT_WHILE_TIMER: // for selection machine
385 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
392 /////////////////////////////////////////////////////////////////////////////////
393 // ================= ad_rx_machine helper functions ==================
394 /////////////////////////////////////////////////////////////////////////////////
397 * __choose_matched - update a port's matched variable from a received lacpdu
398 * @lacpdu: the lacpdu we've received
399 * @port: the port we're looking at
401 * Update the value of the matched variable, using parameter values from a
402 * newly received lacpdu. Parameter values for the partner carried in the
403 * received PDU are compared with the corresponding operational parameter
404 * values for the actor. Matched is set to TRUE if all of these parameters
405 * match and the PDU parameter partner_state.aggregation has the same value as
406 * actor_oper_port_state.aggregation and lacp will actively maintain the link
407 * in the aggregation. Matched is also set to TRUE if the value of
408 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
409 * an individual link and lacp will actively maintain the link. Otherwise,
410 * matched is set to FALSE. LACP is considered to be actively maintaining the
411 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
412 * the actor's actor_oper_port_state.lacp_activity and the PDU's
413 * partner_state.lacp_activity variables are TRUE.
415 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
416 * used here to implement the language from 802.3ad 43.4.9 that requires
417 * recordPDU to "match" the LACPDU parameters to the stored values.
419 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
421 /* check if all parameters are alike
422 * or this is individual link(aggregation == FALSE)
423 * then update the state machine Matched variable.
425 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
426 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
427 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
428 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
429 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
430 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
431 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
433 port->sm_vars |= AD_PORT_MATCHED;
435 port->sm_vars &= ~AD_PORT_MATCHED;
440 * __record_pdu - record parameters from a received lacpdu
441 * @lacpdu: the lacpdu we've received
442 * @port: the port we're looking at
444 * Record the parameter values for the Actor carried in a received lacpdu as
445 * the current partner operational parameter values and sets
446 * actor_oper_port_state.defaulted to FALSE.
448 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
450 if (lacpdu && port) {
451 struct port_params *partner = &port->partner_oper;
453 __choose_matched(lacpdu, port);
454 // record the new parameter values for the partner operational
455 partner->port_number = ntohs(lacpdu->actor_port);
456 partner->port_priority = ntohs(lacpdu->actor_port_priority);
457 partner->system = lacpdu->actor_system;
458 partner->system_priority = ntohs(lacpdu->actor_system_priority);
459 partner->key = ntohs(lacpdu->actor_key);
460 partner->port_state = lacpdu->actor_state;
462 // set actor_oper_port_state.defaulted to FALSE
463 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
465 // set the partner sync. to on if the partner is sync. and the port is matched
466 if ((port->sm_vars & AD_PORT_MATCHED)
467 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
468 partner->port_state |= AD_STATE_SYNCHRONIZATION;
470 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
475 * __record_default - record default parameters
476 * @port: the port we're looking at
478 * This function records the default parameter values for the partner carried
479 * in the Partner Admin parameters as the current partner operational parameter
480 * values and sets actor_oper_port_state.defaulted to TRUE.
482 static void __record_default(struct port *port)
485 // record the partner admin parameters
486 memcpy(&port->partner_oper, &port->partner_admin,
487 sizeof(struct port_params));
489 // set actor_oper_port_state.defaulted to true
490 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
495 * __update_selected - update a port's Selected variable from a received lacpdu
496 * @lacpdu: the lacpdu we've received
497 * @port: the port we're looking at
499 * Update the value of the selected variable, using parameter values from a
500 * newly received lacpdu. The parameter values for the Actor carried in the
501 * received PDU are compared with the corresponding operational parameter
502 * values for the ports partner. If one or more of the comparisons shows that
503 * the value(s) received in the PDU differ from the current operational values,
504 * then selected is set to FALSE and actor_oper_port_state.synchronization is
505 * set to out_of_sync. Otherwise, selected remains unchanged.
507 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
509 if (lacpdu && port) {
510 const struct port_params *partner = &port->partner_oper;
512 /* check if any parameter is different then
513 * update the state machine selected variable.
515 if (ntohs(lacpdu->actor_port) != partner->port_number ||
516 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
517 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
518 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
519 ntohs(lacpdu->actor_key) != partner->key ||
520 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
521 port->sm_vars &= ~AD_PORT_SELECTED;
527 * __update_default_selected - update a port's Selected variable from Partner
528 * @port: the port we're looking at
530 * This function updates the value of the selected variable, using the partner
531 * administrative parameter values. The administrative values are compared with
532 * the corresponding operational parameter values for the partner. If one or
533 * more of the comparisons shows that the administrative value(s) differ from
534 * the current operational values, then Selected is set to FALSE and
535 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
536 * Selected remains unchanged.
538 static void __update_default_selected(struct port *port)
541 const struct port_params *admin = &port->partner_admin;
542 const struct port_params *oper = &port->partner_oper;
544 /* check if any parameter is different then
545 * update the state machine selected variable.
547 if (admin->port_number != oper->port_number ||
548 admin->port_priority != oper->port_priority ||
549 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
550 admin->system_priority != oper->system_priority ||
551 admin->key != oper->key ||
552 (admin->port_state & AD_STATE_AGGREGATION)
553 != (oper->port_state & AD_STATE_AGGREGATION)) {
554 port->sm_vars &= ~AD_PORT_SELECTED;
560 * __update_ntt - update a port's ntt variable from a received lacpdu
561 * @lacpdu: the lacpdu we've received
562 * @port: the port we're looking at
564 * Updates the value of the ntt variable, using parameter values from a newly
565 * received lacpdu. The parameter values for the partner carried in the
566 * received PDU are compared with the corresponding operational parameter
567 * values for the Actor. If one or more of the comparisons shows that the
568 * value(s) received in the PDU differ from the current operational values,
569 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
571 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
573 /* validate lacpdu and port */
574 if (lacpdu && port) {
575 /* check if any parameter is different then
576 * update the port->ntt.
578 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
579 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
580 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
581 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
582 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
583 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
584 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
585 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
586 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
594 * __attach_bond_to_agg
595 * @port: the port we're looking at
597 * Handle the attaching of the port's control parser/multiplexer and the
598 * aggregator. This function does nothing since the parser/multiplexer of the
599 * receive and the parser/multiplexer of the aggregator are already combined.
601 static void __attach_bond_to_agg(struct port *port)
603 port = NULL; /* just to satisfy the compiler */
604 // This function does nothing since the parser/multiplexer of the receive
605 // and the parser/multiplexer of the aggregator are already combined
609 * __detach_bond_from_agg
610 * @port: the port we're looking at
612 * Handle the detaching of the port's control parser/multiplexer from the
613 * aggregator. This function does nothing since the parser/multiplexer of the
614 * receive and the parser/multiplexer of the aggregator are already combined.
616 static void __detach_bond_from_agg(struct port *port)
618 port = NULL; /* just to satisfy the compiler */
619 // This function does nothing since the parser/multiplexer of the receive
620 // and the parser/multiplexer of the aggregator are already combined
624 * __agg_ports_are_ready - check if all ports in an aggregator are ready
625 * @aggregator: the aggregator we're looking at
628 static int __agg_ports_are_ready(struct aggregator *aggregator)
634 // scan all ports in this aggregator to verfy if they are all ready
635 for (port = aggregator->lag_ports;
637 port = port->next_port_in_aggregator) {
638 if (!(port->sm_vars & AD_PORT_READY_N)) {
649 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
650 * @aggregator: the aggregator we're looking at
651 * @val: Should the ports' ready bit be set on or off
654 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
658 for (port = aggregator->lag_ports; port;
659 port = port->next_port_in_aggregator) {
661 port->sm_vars |= AD_PORT_READY;
663 port->sm_vars &= ~AD_PORT_READY;
668 * __get_agg_bandwidth - get the total bandwidth of an aggregator
669 * @aggregator: the aggregator we're looking at
672 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
676 if (aggregator->num_of_ports) {
677 switch (__get_link_speed(aggregator->lag_ports)) {
678 case AD_LINK_SPEED_BITMASK_1MBPS:
679 bandwidth = aggregator->num_of_ports;
681 case AD_LINK_SPEED_BITMASK_10MBPS:
682 bandwidth = aggregator->num_of_ports * 10;
684 case AD_LINK_SPEED_BITMASK_100MBPS:
685 bandwidth = aggregator->num_of_ports * 100;
687 case AD_LINK_SPEED_BITMASK_1000MBPS:
688 bandwidth = aggregator->num_of_ports * 1000;
690 case AD_LINK_SPEED_BITMASK_10000MBPS:
691 bandwidth = aggregator->num_of_ports * 10000;
694 bandwidth = 0; /*to silence the compiler ....*/
701 * __get_active_agg - get the current active aggregator
702 * @aggregator: the aggregator we're looking at
705 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
707 struct bonding *bond = aggregator->slave->bond;
708 struct list_head *iter;
712 bond_for_each_slave_rcu(bond, slave, iter)
713 if (SLAVE_AD_INFO(slave).aggregator.is_active) {
715 return &(SLAVE_AD_INFO(slave).aggregator);
723 * __update_lacpdu_from_port - update a port's lacpdu fields
724 * @port: the port we're looking at
727 static inline void __update_lacpdu_from_port(struct port *port)
729 struct lacpdu *lacpdu = &port->lacpdu;
730 const struct port_params *partner = &port->partner_oper;
732 /* update current actual Actor parameters */
733 /* lacpdu->subtype initialized
734 * lacpdu->version_number initialized
735 * lacpdu->tlv_type_actor_info initialized
736 * lacpdu->actor_information_length initialized
739 lacpdu->actor_system_priority = htons(port->actor_system_priority);
740 lacpdu->actor_system = port->actor_system;
741 lacpdu->actor_key = htons(port->actor_oper_port_key);
742 lacpdu->actor_port_priority = htons(port->actor_port_priority);
743 lacpdu->actor_port = htons(port->actor_port_number);
744 lacpdu->actor_state = port->actor_oper_port_state;
746 /* lacpdu->reserved_3_1 initialized
747 * lacpdu->tlv_type_partner_info initialized
748 * lacpdu->partner_information_length initialized
751 lacpdu->partner_system_priority = htons(partner->system_priority);
752 lacpdu->partner_system = partner->system;
753 lacpdu->partner_key = htons(partner->key);
754 lacpdu->partner_port_priority = htons(partner->port_priority);
755 lacpdu->partner_port = htons(partner->port_number);
756 lacpdu->partner_state = partner->port_state;
758 /* lacpdu->reserved_3_2 initialized
759 * lacpdu->tlv_type_collector_info initialized
760 * lacpdu->collector_information_length initialized
761 * collector_max_delay initialized
762 * reserved_12[12] initialized
763 * tlv_type_terminator initialized
764 * terminator_length initialized
765 * reserved_50[50] initialized
769 //////////////////////////////////////////////////////////////////////////////////////
770 // ================= main 802.3ad protocol code ======================================
771 //////////////////////////////////////////////////////////////////////////////////////
774 * ad_lacpdu_send - send out a lacpdu packet on a given port
775 * @port: the port we're looking at
777 * Returns: 0 on success
780 static int ad_lacpdu_send(struct port *port)
782 struct slave *slave = port->slave;
784 struct lacpdu_header *lacpdu_header;
785 int length = sizeof(struct lacpdu_header);
787 skb = dev_alloc_skb(length);
791 skb->dev = slave->dev;
792 skb_reset_mac_header(skb);
793 skb->network_header = skb->mac_header + ETH_HLEN;
794 skb->protocol = PKT_TYPE_LACPDU;
795 skb->priority = TC_PRIO_CONTROL;
797 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
799 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
800 /* Note: source address is set to be the member's PERMANENT address,
801 because we use it to identify loopback lacpdus in receive. */
802 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
803 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
805 lacpdu_header->lacpdu = port->lacpdu; // struct copy
813 * ad_marker_send - send marker information/response on a given port
814 * @port: the port we're looking at
815 * @marker: marker data to send
817 * Returns: 0 on success
820 static int ad_marker_send(struct port *port, struct bond_marker *marker)
822 struct slave *slave = port->slave;
824 struct bond_marker_header *marker_header;
825 int length = sizeof(struct bond_marker_header);
827 skb = dev_alloc_skb(length + 16);
831 skb_reserve(skb, 16);
833 skb->dev = slave->dev;
834 skb_reset_mac_header(skb);
835 skb->network_header = skb->mac_header + ETH_HLEN;
836 skb->protocol = PKT_TYPE_LACPDU;
838 marker_header = (struct bond_marker_header *)skb_put(skb, length);
840 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
841 /* Note: source address is set to be the member's PERMANENT address,
842 because we use it to identify loopback MARKERs in receive. */
843 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
844 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
846 marker_header->marker = *marker; // struct copy
854 * ad_mux_machine - handle a port's mux state machine
855 * @port: the port we're looking at
858 static void ad_mux_machine(struct port *port)
860 mux_states_t last_state;
862 // keep current State Machine state to compare later if it was changed
863 last_state = port->sm_mux_state;
865 if (port->sm_vars & AD_PORT_BEGIN) {
866 port->sm_mux_state = AD_MUX_DETACHED; // next state
868 switch (port->sm_mux_state) {
869 case AD_MUX_DETACHED:
870 if ((port->sm_vars & AD_PORT_SELECTED)
871 || (port->sm_vars & AD_PORT_STANDBY))
872 /* if SELECTED or STANDBY */
873 port->sm_mux_state = AD_MUX_WAITING; // next state
876 // if SELECTED == FALSE return to DETACH state
877 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
878 port->sm_vars &= ~AD_PORT_READY_N;
879 // in order to withhold the Selection Logic to check all ports READY_N value
880 // every callback cycle to update ready variable, we check READY_N and update READY here
881 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
882 port->sm_mux_state = AD_MUX_DETACHED; // next state
886 // check if the wait_while_timer expired
887 if (port->sm_mux_timer_counter
888 && !(--port->sm_mux_timer_counter))
889 port->sm_vars |= AD_PORT_READY_N;
891 // in order to withhold the selection logic to check all ports READY_N value
892 // every callback cycle to update ready variable, we check READY_N and update READY here
893 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
895 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
896 if ((port->sm_vars & AD_PORT_READY)
897 && !port->sm_mux_timer_counter)
898 port->sm_mux_state = AD_MUX_ATTACHED; // next state
900 case AD_MUX_ATTACHED:
901 // check also if agg_select_timer expired(so the edable port will take place only after this timer)
902 if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) {
903 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
904 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
905 port->sm_vars &= ~AD_PORT_READY_N;
906 // in order to withhold the selection logic to check all ports READY_N value
907 // every callback cycle to update ready variable, we check READY_N and update READY here
908 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
909 port->sm_mux_state = AD_MUX_DETACHED;// next state
912 case AD_MUX_COLLECTING_DISTRIBUTING:
913 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
914 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
916 port->sm_mux_state = AD_MUX_ATTACHED;// next state
919 // if port state hasn't changed make
920 // sure that a collecting distributing
921 // port in an active aggregator is enabled
922 if (port->aggregator &&
923 port->aggregator->is_active &&
924 !__port_is_enabled(port)) {
930 default: //to silence the compiler
935 // check if the state machine was changed
936 if (port->sm_mux_state != last_state) {
937 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
938 port->actor_port_number, last_state,
940 switch (port->sm_mux_state) {
941 case AD_MUX_DETACHED:
942 __detach_bond_from_agg(port);
943 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
944 ad_disable_collecting_distributing(port);
945 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
946 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
950 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
952 case AD_MUX_ATTACHED:
953 __attach_bond_to_agg(port);
954 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
955 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
956 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
957 ad_disable_collecting_distributing(port);
960 case AD_MUX_COLLECTING_DISTRIBUTING:
961 port->actor_oper_port_state |= AD_STATE_COLLECTING;
962 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
963 ad_enable_collecting_distributing(port);
966 default: //to silence the compiler
973 * ad_rx_machine - handle a port's rx State Machine
974 * @lacpdu: the lacpdu we've received
975 * @port: the port we're looking at
977 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
978 * CURRENT. If timer expired set the state machine in the proper state.
979 * In other cases, this function checks if we need to switch to other state.
981 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
983 rx_states_t last_state;
985 // keep current State Machine state to compare later if it was changed
986 last_state = port->sm_rx_state;
988 // check if state machine should change state
989 // first, check if port was reinitialized
990 if (port->sm_vars & AD_PORT_BEGIN)
992 port->sm_rx_state = AD_RX_INITIALIZE;
993 // check if port is not enabled
994 else if (!(port->sm_vars & AD_PORT_BEGIN)
995 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
997 port->sm_rx_state = AD_RX_PORT_DISABLED;
998 // check if new lacpdu arrived
999 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1000 port->sm_rx_timer_counter = 0; // zero timer
1001 port->sm_rx_state = AD_RX_CURRENT;
1003 // if timer is on, and if it is expired
1004 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1005 switch (port->sm_rx_state) {
1007 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1010 port->sm_rx_state = AD_RX_EXPIRED; // next state
1012 default: //to silence the compiler
1016 // if no lacpdu arrived and no timer is on
1017 switch (port->sm_rx_state) {
1018 case AD_RX_PORT_DISABLED:
1019 if (port->sm_vars & AD_PORT_MOVED)
1020 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1021 else if (port->is_enabled
1023 & AD_PORT_LACP_ENABLED))
1024 port->sm_rx_state = AD_RX_EXPIRED; // next state
1025 else if (port->is_enabled
1027 & AD_PORT_LACP_ENABLED) == 0))
1028 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
1030 default: //to silence the compiler
1037 // check if the State machine was changed or new lacpdu arrived
1038 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1039 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1040 port->actor_port_number, last_state,
1042 switch (port->sm_rx_state) {
1043 case AD_RX_INITIALIZE:
1044 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
1045 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1047 port->sm_vars |= AD_PORT_LACP_ENABLED;
1048 port->sm_vars &= ~AD_PORT_SELECTED;
1049 __record_default(port);
1050 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1051 port->sm_vars &= ~AD_PORT_MOVED;
1052 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1054 /*- Fall Through -*/
1056 case AD_RX_PORT_DISABLED:
1057 port->sm_vars &= ~AD_PORT_MATCHED;
1059 case AD_RX_LACP_DISABLED:
1060 port->sm_vars &= ~AD_PORT_SELECTED;
1061 __record_default(port);
1062 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1063 port->sm_vars |= AD_PORT_MATCHED;
1064 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1067 //Reset of the Synchronization flag. (Standard 43.4.12)
1068 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1069 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
1070 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1071 port->sm_vars &= ~AD_PORT_MATCHED;
1072 port->partner_oper.port_state |=
1073 AD_STATE_LACP_ACTIVITY;
1074 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1075 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1077 case AD_RX_DEFAULTED:
1078 __update_default_selected(port);
1079 __record_default(port);
1080 port->sm_vars |= AD_PORT_MATCHED;
1081 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1084 /* detect loopback situation */
1085 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system), &(port->actor_system))) {
1086 pr_err("%s: An illegal loopback occurred on adapter (%s).\n"
1087 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1088 port->slave->bond->dev->name, port->slave->dev->name);
1091 __update_selected(lacpdu, port);
1092 __update_ntt(lacpdu, port);
1093 __record_pdu(lacpdu, port);
1094 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1095 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1097 default: /* to silence the compiler */
1104 * ad_tx_machine - handle a port's tx state machine
1105 * @port: the port we're looking at
1108 static void ad_tx_machine(struct port *port)
1110 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1111 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1112 // check if there is something to send
1113 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1114 __update_lacpdu_from_port(port);
1116 if (ad_lacpdu_send(port) >= 0) {
1117 pr_debug("Sent LACPDU on port %d\n",
1118 port->actor_port_number);
1120 /* mark ntt as false, so it will not be sent again until
1125 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
1126 port->sm_tx_timer_counter =
1127 ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1132 * ad_periodic_machine - handle a port's periodic state machine
1133 * @port: the port we're looking at
1135 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1137 static void ad_periodic_machine(struct port *port)
1139 periodic_states_t last_state;
1141 // keep current state machine state to compare later if it was changed
1142 last_state = port->sm_periodic_state;
1144 // check if port was reinitialized
1145 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1146 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1148 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1150 // check if state machine should change state
1151 else if (port->sm_periodic_timer_counter) {
1152 // check if periodic state machine expired
1153 if (!(--port->sm_periodic_timer_counter)) {
1154 // if expired then do tx
1155 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1157 // If not expired, check if there is some new timeout parameter from the partner state
1158 switch (port->sm_periodic_state) {
1159 case AD_FAST_PERIODIC:
1160 if (!(port->partner_oper.port_state
1161 & AD_STATE_LACP_TIMEOUT))
1162 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1164 case AD_SLOW_PERIODIC:
1165 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1166 // stop current timer
1167 port->sm_periodic_timer_counter = 0;
1168 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1171 default: //to silence the compiler
1176 switch (port->sm_periodic_state) {
1177 case AD_NO_PERIODIC:
1178 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1180 case AD_PERIODIC_TX:
1181 if (!(port->partner_oper.port_state
1182 & AD_STATE_LACP_TIMEOUT))
1183 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1185 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1187 default: //to silence the compiler
1192 // check if the state machine was changed
1193 if (port->sm_periodic_state != last_state) {
1194 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1195 port->actor_port_number, last_state,
1196 port->sm_periodic_state);
1197 switch (port->sm_periodic_state) {
1198 case AD_NO_PERIODIC:
1199 port->sm_periodic_timer_counter = 0; // zero timer
1201 case AD_FAST_PERIODIC:
1202 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
1204 case AD_SLOW_PERIODIC:
1205 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
1207 case AD_PERIODIC_TX:
1210 default: //to silence the compiler
1217 * ad_port_selection_logic - select aggregation groups
1218 * @port: the port we're looking at
1220 * Select aggregation groups, and assign each port for it's aggregetor. The
1221 * selection logic is called in the inititalization (after all the handshkes),
1222 * and after every lacpdu receive (if selected is off).
1224 static void ad_port_selection_logic(struct port *port)
1226 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1227 struct port *last_port = NULL, *curr_port;
1228 struct list_head *iter;
1229 struct bonding *bond;
1230 struct slave *slave;
1233 // if the port is already Selected, do nothing
1234 if (port->sm_vars & AD_PORT_SELECTED)
1237 bond = __get_bond_by_port(port);
1239 // if the port is connected to other aggregator, detach it
1240 if (port->aggregator) {
1241 // detach the port from its former aggregator
1242 temp_aggregator = port->aggregator;
1243 for (curr_port = temp_aggregator->lag_ports; curr_port;
1244 last_port = curr_port,
1245 curr_port = curr_port->next_port_in_aggregator) {
1246 if (curr_port == port) {
1247 temp_aggregator->num_of_ports--;
1248 if (!last_port) {// if it is the first port attached to the aggregator
1249 temp_aggregator->lag_ports =
1250 port->next_port_in_aggregator;
1251 } else {// not the first port attached to the aggregator
1252 last_port->next_port_in_aggregator =
1253 port->next_port_in_aggregator;
1256 // clear the port's relations to this aggregator
1257 port->aggregator = NULL;
1258 port->next_port_in_aggregator = NULL;
1259 port->actor_port_aggregator_identifier = 0;
1261 pr_debug("Port %d left LAG %d\n",
1262 port->actor_port_number,
1263 temp_aggregator->aggregator_identifier);
1264 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1265 if (!temp_aggregator->lag_ports)
1266 ad_clear_agg(temp_aggregator);
1270 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
1271 pr_warning("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1272 port->slave->bond->dev->name,
1273 port->actor_port_number,
1274 port->slave->dev->name,
1275 port->aggregator->aggregator_identifier);
1278 // search on all aggregators for a suitable aggregator for this port
1279 bond_for_each_slave(bond, slave, iter) {
1280 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1282 // keep a free aggregator for later use(if needed)
1283 if (!aggregator->lag_ports) {
1284 if (!free_aggregator)
1285 free_aggregator = aggregator;
1288 /* check if current aggregator suits us */
1289 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1290 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1291 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1292 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1294 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1295 !aggregator->is_individual) /* but is not individual OR */
1298 /* attach to the founded aggregator */
1299 port->aggregator = aggregator;
1300 port->actor_port_aggregator_identifier =
1301 port->aggregator->aggregator_identifier;
1302 port->next_port_in_aggregator = aggregator->lag_ports;
1303 port->aggregator->num_of_ports++;
1304 aggregator->lag_ports = port;
1305 pr_debug("Port %d joined LAG %d(existing LAG)\n",
1306 port->actor_port_number,
1307 port->aggregator->aggregator_identifier);
1309 // mark this port as selected
1310 port->sm_vars |= AD_PORT_SELECTED;
1316 // the port couldn't find an aggregator - attach it to a new aggregator
1318 if (free_aggregator) {
1319 // assign port a new aggregator
1320 port->aggregator = free_aggregator;
1321 port->actor_port_aggregator_identifier =
1322 port->aggregator->aggregator_identifier;
1324 // update the new aggregator's parameters
1325 // if port was responsed from the end-user
1326 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)
1327 /* if port is full duplex */
1328 port->aggregator->is_individual = false;
1330 port->aggregator->is_individual = true;
1332 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1333 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
1334 port->aggregator->partner_system =
1335 port->partner_oper.system;
1336 port->aggregator->partner_system_priority =
1337 port->partner_oper.system_priority;
1338 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1339 port->aggregator->receive_state = 1;
1340 port->aggregator->transmit_state = 1;
1341 port->aggregator->lag_ports = port;
1342 port->aggregator->num_of_ports++;
1344 // mark this port as selected
1345 port->sm_vars |= AD_PORT_SELECTED;
1347 pr_debug("Port %d joined LAG %d(new LAG)\n",
1348 port->actor_port_number,
1349 port->aggregator->aggregator_identifier);
1351 pr_err("%s: Port %d (on %s) did not find a suitable aggregator\n",
1352 port->slave->bond->dev->name,
1353 port->actor_port_number, port->slave->dev->name);
1356 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1357 // else set ready=FALSE in all aggregator's ports
1358 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1360 aggregator = __get_first_agg(port);
1361 ad_agg_selection_logic(aggregator);
1365 * Decide if "agg" is a better choice for the new active aggregator that
1366 * the current best, according to the ad_select policy.
1368 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1369 struct aggregator *curr)
1372 * 0. If no best, select current.
1374 * 1. If the current agg is not individual, and the best is
1375 * individual, select current.
1377 * 2. If current agg is individual and the best is not, keep best.
1379 * 3. Therefore, current and best are both individual or both not
1382 * 3a. If current agg partner replied, and best agg partner did not,
1385 * 3b. If current agg partner did not reply and best agg partner
1386 * did reply, keep best.
1388 * 4. Therefore, current and best both have partner replies or
1389 * both do not, so perform selection policy:
1391 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1392 * select by bandwidth.
1394 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1399 if (!curr->is_individual && best->is_individual)
1402 if (curr->is_individual && !best->is_individual)
1405 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1408 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1411 switch (__get_agg_selection_mode(curr->lag_ports)) {
1413 if (curr->num_of_ports > best->num_of_ports)
1416 if (curr->num_of_ports < best->num_of_ports)
1420 case BOND_AD_STABLE:
1421 case BOND_AD_BANDWIDTH:
1422 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1428 pr_warning("%s: Impossible agg select mode %d\n",
1429 curr->slave->bond->dev->name,
1430 __get_agg_selection_mode(curr->lag_ports));
1437 static int agg_device_up(const struct aggregator *agg)
1439 struct port *port = agg->lag_ports;
1442 return (netif_running(port->slave->dev) &&
1443 netif_carrier_ok(port->slave->dev));
1447 * ad_agg_selection_logic - select an aggregation group for a team
1448 * @aggregator: the aggregator we're looking at
1450 * It is assumed that only one aggregator may be selected for a team.
1452 * The logic of this function is to select the aggregator according to
1453 * the ad_select policy:
1455 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1456 * it, and to reselect the active aggregator only if the previous
1457 * aggregator has no more ports related to it.
1459 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1460 * bandwidth, and reselect whenever a link state change takes place or the
1461 * set of slaves in the bond changes.
1463 * BOND_AD_COUNT: select the aggregator with largest number of ports
1464 * (slaves), and reselect whenever a link state change takes place or the
1465 * set of slaves in the bond changes.
1467 * FIXME: this function MUST be called with the first agg in the bond, or
1468 * __get_active_agg() won't work correctly. This function should be better
1469 * called with the bond itself, and retrieve the first agg from it.
1471 static void ad_agg_selection_logic(struct aggregator *agg)
1473 struct aggregator *best, *active, *origin;
1474 struct bonding *bond = agg->slave->bond;
1475 struct list_head *iter;
1476 struct slave *slave;
1480 active = __get_active_agg(agg);
1481 best = (active && agg_device_up(active)) ? active : NULL;
1484 bond_for_each_slave_rcu(bond, slave, iter) {
1485 agg = &(SLAVE_AD_INFO(slave).aggregator);
1489 if (agg->num_of_ports && agg_device_up(agg))
1490 best = ad_agg_selection_test(best, agg);
1494 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1496 * For the STABLE policy, don't replace the old active
1497 * aggregator if it's still active (it has an answering
1498 * partner) or if both the best and active don't have an
1499 * answering partner.
1501 if (active && active->lag_ports &&
1502 active->lag_ports->is_enabled &&
1503 (__agg_has_partner(active) ||
1504 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1505 if (!(!active->actor_oper_aggregator_key &&
1506 best->actor_oper_aggregator_key)) {
1508 active->is_active = 1;
1513 if (best && (best == active)) {
1515 active->is_active = 1;
1518 /* if there is new best aggregator, activate it */
1520 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1521 best->aggregator_identifier, best->num_of_ports,
1522 best->actor_oper_aggregator_key,
1523 best->partner_oper_aggregator_key,
1524 best->is_individual, best->is_active);
1525 pr_debug("best ports %p slave %p %s\n",
1526 best->lag_ports, best->slave,
1527 best->slave ? best->slave->dev->name : "NULL");
1529 bond_for_each_slave_rcu(bond, slave, iter) {
1530 agg = &(SLAVE_AD_INFO(slave).aggregator);
1532 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1533 agg->aggregator_identifier, agg->num_of_ports,
1534 agg->actor_oper_aggregator_key,
1535 agg->partner_oper_aggregator_key,
1536 agg->is_individual, agg->is_active);
1539 /* check if any partner replys */
1540 if (best->is_individual) {
1541 pr_warning("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1543 best->slave->bond->dev->name : "NULL");
1546 best->is_active = 1;
1547 pr_debug("LAG %d chosen as the active LAG\n",
1548 best->aggregator_identifier);
1549 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1550 best->aggregator_identifier, best->num_of_ports,
1551 best->actor_oper_aggregator_key,
1552 best->partner_oper_aggregator_key,
1553 best->is_individual, best->is_active);
1555 /* disable the ports that were related to the former active_aggregator */
1557 for (port = active->lag_ports; port;
1558 port = port->next_port_in_aggregator) {
1559 __disable_port(port);
1565 * if the selected aggregator is of join individuals
1566 * (partner_system is NULL), enable their ports
1568 active = __get_active_agg(origin);
1571 if (!__agg_has_partner(active)) {
1572 for (port = active->lag_ports; port;
1573 port = port->next_port_in_aggregator) {
1574 __enable_port(port);
1581 bond_3ad_set_carrier(bond);
1585 * ad_clear_agg - clear a given aggregator's parameters
1586 * @aggregator: the aggregator we're looking at
1589 static void ad_clear_agg(struct aggregator *aggregator)
1592 aggregator->is_individual = false;
1593 aggregator->actor_admin_aggregator_key = 0;
1594 aggregator->actor_oper_aggregator_key = 0;
1595 aggregator->partner_system = null_mac_addr;
1596 aggregator->partner_system_priority = 0;
1597 aggregator->partner_oper_aggregator_key = 0;
1598 aggregator->receive_state = 0;
1599 aggregator->transmit_state = 0;
1600 aggregator->lag_ports = NULL;
1601 aggregator->is_active = 0;
1602 aggregator->num_of_ports = 0;
1603 pr_debug("LAG %d was cleared\n",
1604 aggregator->aggregator_identifier);
1609 * ad_initialize_agg - initialize a given aggregator's parameters
1610 * @aggregator: the aggregator we're looking at
1613 static void ad_initialize_agg(struct aggregator *aggregator)
1616 ad_clear_agg(aggregator);
1618 aggregator->aggregator_mac_address = null_mac_addr;
1619 aggregator->aggregator_identifier = 0;
1620 aggregator->slave = NULL;
1625 * ad_initialize_port - initialize a given port's parameters
1626 * @aggregator: the aggregator we're looking at
1627 * @lacp_fast: boolean. whether fast periodic should be used
1630 static void ad_initialize_port(struct port *port, int lacp_fast)
1632 static const struct port_params tmpl = {
1633 .system_priority = 0xffff,
1636 .port_priority = 0xff,
1639 static const struct lacpdu lacpdu = {
1641 .version_number = 0x01,
1642 .tlv_type_actor_info = 0x01,
1643 .actor_information_length = 0x14,
1644 .tlv_type_partner_info = 0x02,
1645 .partner_information_length = 0x14,
1646 .tlv_type_collector_info = 0x03,
1647 .collector_information_length = 0x10,
1648 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1652 port->actor_port_number = 1;
1653 port->actor_port_priority = 0xff;
1654 port->actor_system = null_mac_addr;
1655 port->actor_system_priority = 0xffff;
1656 port->actor_port_aggregator_identifier = 0;
1658 port->actor_admin_port_key = 1;
1659 port->actor_oper_port_key = 1;
1660 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1661 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1664 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1666 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1667 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1669 port->is_enabled = true;
1670 // ****** private parameters ******
1671 port->sm_vars = 0x3;
1672 port->sm_rx_state = 0;
1673 port->sm_rx_timer_counter = 0;
1674 port->sm_periodic_state = 0;
1675 port->sm_periodic_timer_counter = 0;
1676 port->sm_mux_state = 0;
1677 port->sm_mux_timer_counter = 0;
1678 port->sm_tx_state = 0;
1679 port->sm_tx_timer_counter = 0;
1681 port->aggregator = NULL;
1682 port->next_port_in_aggregator = NULL;
1683 port->transaction_id = 0;
1685 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1690 * ad_enable_collecting_distributing - enable a port's transmit/receive
1691 * @port: the port we're looking at
1693 * Enable @port if it's in an active aggregator
1695 static void ad_enable_collecting_distributing(struct port *port)
1697 if (port->aggregator->is_active) {
1698 pr_debug("Enabling port %d(LAG %d)\n",
1699 port->actor_port_number,
1700 port->aggregator->aggregator_identifier);
1701 __enable_port(port);
1706 * ad_disable_collecting_distributing - disable a port's transmit/receive
1707 * @port: the port we're looking at
1710 static void ad_disable_collecting_distributing(struct port *port)
1712 if (port->aggregator && !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system), &(null_mac_addr))) {
1713 pr_debug("Disabling port %d(LAG %d)\n",
1714 port->actor_port_number,
1715 port->aggregator->aggregator_identifier);
1716 __disable_port(port);
1722 * ad_marker_info_send - send a marker information frame
1723 * @port: the port we're looking at
1725 * This function does nothing since we decided not to implement send and handle
1726 * response for marker PDU's, in this stage, but only to respond to marker
1729 static void ad_marker_info_send(struct port *port)
1731 struct bond_marker marker;
1734 // fill the marker PDU with the appropriate values
1735 marker.subtype = 0x02;
1736 marker.version_number = 0x01;
1737 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1738 marker.marker_length = 0x16;
1739 // convert requester_port to Big Endian
1740 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1741 marker.requester_system = port->actor_system;
1742 // convert requester_port(u32) to Big Endian
1743 marker.requester_transaction_id =
1744 (((++port->transaction_id & 0xFF) << 24)
1745 | ((port->transaction_id & 0xFF00) << 8)
1746 | ((port->transaction_id & 0xFF0000) >> 8)
1747 | ((port->transaction_id & 0xFF000000) >> 24));
1749 marker.tlv_type_terminator = 0x00;
1750 marker.terminator_length = 0x00;
1751 for (index = 0; index < 90; index++)
1752 marker.reserved_90[index] = 0;
1754 // send the marker information
1755 if (ad_marker_send(port, &marker) >= 0) {
1756 pr_debug("Sent Marker Information on port %d\n",
1757 port->actor_port_number);
1763 * ad_marker_info_received - handle receive of a Marker information frame
1764 * @marker_info: Marker info received
1765 * @port: the port we're looking at
1768 static void ad_marker_info_received(struct bond_marker *marker_info,
1771 struct bond_marker marker;
1773 // copy the received marker data to the response marker
1774 //marker = *marker_info;
1775 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1776 // change the marker subtype to marker response
1777 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1778 // send the marker response
1780 if (ad_marker_send(port, &marker) >= 0) {
1781 pr_debug("Sent Marker Response on port %d\n",
1782 port->actor_port_number);
1787 * ad_marker_response_received - handle receive of a marker response frame
1788 * @marker: marker PDU received
1789 * @port: the port we're looking at
1791 * This function does nothing since we decided not to implement send and handle
1792 * response for marker PDU's, in this stage, but only to respond to marker
1795 static void ad_marker_response_received(struct bond_marker *marker,
1798 marker = NULL; /* just to satisfy the compiler */
1799 port = NULL; /* just to satisfy the compiler */
1800 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1803 //////////////////////////////////////////////////////////////////////////////////////
1804 // ================= AD exported functions to the main bonding code ==================
1805 //////////////////////////////////////////////////////////////////////////////////////
1807 // Check aggregators status in team every T seconds
1808 #define AD_AGGREGATOR_SELECTION_TIMER 8
1811 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1813 * Set the aggregation selection timer, to initiate an agg selection in
1814 * the very near future. Called during first initialization, and during
1815 * any down to up transitions of the bond.
1817 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1819 BOND_AD_INFO(bond).agg_select_timer = timeout;
1822 static u16 aggregator_identifier;
1825 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1826 * @bond: bonding struct to work on
1827 * @tick_resolution: tick duration (millisecond resolution)
1829 * Can be called only after the mac address of the bond is set.
1831 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
1833 /* check that the bond is not initialized yet */
1834 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1835 bond->dev->dev_addr)) {
1837 aggregator_identifier = 0;
1839 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1840 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1842 // initialize how many times this module is called in one second(should be about every 100ms)
1843 ad_ticks_per_sec = tick_resolution;
1845 bond_3ad_initiate_agg_selection(bond,
1846 AD_AGGREGATOR_SELECTION_TIMER *
1852 * bond_3ad_bind_slave - initialize a slave's port
1853 * @slave: slave struct to work on
1855 * Returns: 0 on success
1858 void bond_3ad_bind_slave(struct slave *slave)
1860 struct bonding *bond = bond_get_bond_by_slave(slave);
1862 struct aggregator *aggregator;
1864 /* check that the slave has not been initialized yet. */
1865 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1867 /* port initialization */
1868 port = &(SLAVE_AD_INFO(slave).port);
1870 ad_initialize_port(port, bond->params.lacp_fast);
1872 __initialize_port_locks(slave);
1873 port->slave = slave;
1874 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1875 /* key is determined according to the link speed, duplex and user key(which
1876 * is yet not supported)
1877 * ------------------------------------------------------------
1878 * Port key : | User key | Speed |Duplex|
1879 * ------------------------------------------------------------
1882 port->actor_admin_port_key = 0; /* initialize this parameter */
1883 port->actor_admin_port_key |= __get_duplex(port);
1884 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1885 port->actor_oper_port_key = port->actor_admin_port_key;
1886 /* if the port is not full duplex, then the port should be not lacp Enabled */
1887 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
1888 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1889 /* actor system is the bond's system */
1890 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1891 /* tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second) */
1892 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1893 port->aggregator = NULL;
1894 port->next_port_in_aggregator = NULL;
1896 __disable_port(port);
1898 /* aggregator initialization */
1899 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1901 ad_initialize_agg(aggregator);
1903 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1904 aggregator->aggregator_identifier = (++aggregator_identifier);
1905 aggregator->slave = slave;
1906 aggregator->is_active = 0;
1907 aggregator->num_of_ports = 0;
1912 * bond_3ad_unbind_slave - deinitialize a slave's port
1913 * @slave: slave struct to work on
1915 * Search for the aggregator that is related to this port, remove the
1916 * aggregator and assign another aggregator for other port related to it
1917 * (if any), and remove the port.
1919 void bond_3ad_unbind_slave(struct slave *slave)
1921 struct port *port, *prev_port, *temp_port;
1922 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1923 int select_new_active_agg = 0;
1924 struct bonding *bond = slave->bond;
1925 struct slave *slave_iter;
1926 struct list_head *iter;
1928 // find the aggregator related to this slave
1929 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1931 // find the port related to this slave
1932 port = &(SLAVE_AD_INFO(slave).port);
1934 // if slave is null, the whole port is not initialized
1936 pr_warning("Warning: %s: Trying to unbind an uninitialized port on %s\n",
1937 slave->bond->dev->name, slave->dev->name);
1941 pr_debug("Unbinding Link Aggregation Group %d\n",
1942 aggregator->aggregator_identifier);
1944 /* Tell the partner that this port is not suitable for aggregation */
1945 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1946 __update_lacpdu_from_port(port);
1947 ad_lacpdu_send(port);
1949 // check if this aggregator is occupied
1950 if (aggregator->lag_ports) {
1951 // check if there are other ports related to this aggregator except
1952 // the port related to this slave(thats ensure us that there is a
1953 // reason to search for new aggregator, and that we will find one
1954 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
1955 // find new aggregator for the related port(s)
1956 bond_for_each_slave(bond, slave_iter, iter) {
1957 new_aggregator = &(SLAVE_AD_INFO(slave_iter).aggregator);
1958 // if the new aggregator is empty, or it is connected to our port only
1959 if (!new_aggregator->lag_ports
1960 || ((new_aggregator->lag_ports == port)
1961 && !new_aggregator->lag_ports->next_port_in_aggregator))
1965 new_aggregator = NULL;
1966 // if new aggregator found, copy the aggregator's parameters
1967 // and connect the related lag_ports to the new aggregator
1968 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
1969 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n",
1970 aggregator->aggregator_identifier,
1971 new_aggregator->aggregator_identifier);
1973 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
1974 pr_info("%s: Removing an active aggregator\n",
1975 aggregator->slave->bond->dev->name);
1976 // select new active aggregator
1977 select_new_active_agg = 1;
1980 new_aggregator->is_individual = aggregator->is_individual;
1981 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
1982 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
1983 new_aggregator->partner_system = aggregator->partner_system;
1984 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
1985 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
1986 new_aggregator->receive_state = aggregator->receive_state;
1987 new_aggregator->transmit_state = aggregator->transmit_state;
1988 new_aggregator->lag_ports = aggregator->lag_ports;
1989 new_aggregator->is_active = aggregator->is_active;
1990 new_aggregator->num_of_ports = aggregator->num_of_ports;
1992 // update the information that is written on the ports about the aggregator
1993 for (temp_port = aggregator->lag_ports; temp_port;
1994 temp_port = temp_port->next_port_in_aggregator) {
1995 temp_port->aggregator = new_aggregator;
1996 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
1999 // clear the aggregator
2000 ad_clear_agg(aggregator);
2002 if (select_new_active_agg)
2003 ad_agg_selection_logic(__get_first_agg(port));
2005 pr_warning("%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n",
2006 slave->bond->dev->name);
2008 } else { // in case that the only port related to this aggregator is the one we want to remove
2009 select_new_active_agg = aggregator->is_active;
2010 // clear the aggregator
2011 ad_clear_agg(aggregator);
2012 if (select_new_active_agg) {
2013 pr_info("%s: Removing an active aggregator\n",
2014 slave->bond->dev->name);
2015 // select new active aggregator
2016 temp_aggregator = __get_first_agg(port);
2017 if (temp_aggregator)
2018 ad_agg_selection_logic(temp_aggregator);
2023 pr_debug("Unbinding port %d\n", port->actor_port_number);
2024 // find the aggregator that this port is connected to
2025 bond_for_each_slave(bond, slave_iter, iter) {
2026 temp_aggregator = &(SLAVE_AD_INFO(slave_iter).aggregator);
2028 // search the port in the aggregator's related ports
2029 for (temp_port = temp_aggregator->lag_ports; temp_port;
2030 prev_port = temp_port,
2031 temp_port = temp_port->next_port_in_aggregator) {
2032 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2034 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2036 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2037 temp_aggregator->num_of_ports--;
2038 if (temp_aggregator->num_of_ports == 0) {
2039 select_new_active_agg = temp_aggregator->is_active;
2040 // clear the aggregator
2041 ad_clear_agg(temp_aggregator);
2042 if (select_new_active_agg) {
2043 pr_info("%s: Removing an active aggregator\n",
2044 slave->bond->dev->name);
2045 // select new active aggregator
2046 ad_agg_selection_logic(__get_first_agg(port));
2057 * bond_3ad_state_machine_handler - handle state machines timeout
2058 * @bond: bonding struct to work on
2060 * The state machine handling concept in this module is to check every tick
2061 * which state machine should operate any function. The execution order is
2062 * round robin, so when we have an interaction between state machines, the
2063 * reply of one to each other might be delayed until next tick.
2065 * This function also complete the initialization when the agg_select_timer
2066 * times out, and it selects an aggregator for the ports that are yet not
2067 * related to any aggregator, and selects the active aggregator for a bond.
2069 void bond_3ad_state_machine_handler(struct work_struct *work)
2071 struct bonding *bond = container_of(work, struct bonding,
2073 struct aggregator *aggregator;
2074 struct list_head *iter;
2075 struct slave *slave;
2078 read_lock(&bond->lock);
2081 /* check if there are any slaves */
2082 if (!bond_has_slaves(bond))
2085 /* check if agg_select_timer timer after initialize is timed out */
2086 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2087 slave = bond_first_slave_rcu(bond);
2088 port = slave ? &(SLAVE_AD_INFO(slave).port) : NULL;
2090 /* select the active aggregator for the bond */
2093 pr_warning("%s: Warning: bond's first port is uninitialized\n",
2098 aggregator = __get_first_agg(port);
2099 ad_agg_selection_logic(aggregator);
2101 bond_3ad_set_carrier(bond);
2104 /* for each port run the state machines */
2105 bond_for_each_slave_rcu(bond, slave, iter) {
2106 port = &(SLAVE_AD_INFO(slave).port);
2108 pr_warning("%s: Warning: Found an uninitialized port\n",
2113 /* Lock around state machines to protect data accessed
2114 * by all (e.g., port->sm_vars). ad_rx_machine may run
2115 * concurrently due to incoming LACPDU.
2117 __get_state_machine_lock(port);
2119 ad_rx_machine(NULL, port);
2120 ad_periodic_machine(port);
2121 ad_port_selection_logic(port);
2122 ad_mux_machine(port);
2123 ad_tx_machine(port);
2125 /* turn off the BEGIN bit, since we already handled it */
2126 if (port->sm_vars & AD_PORT_BEGIN)
2127 port->sm_vars &= ~AD_PORT_BEGIN;
2129 __release_state_machine_lock(port);
2134 read_unlock(&bond->lock);
2135 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2139 * bond_3ad_rx_indication - handle a received frame
2140 * @lacpdu: received lacpdu
2141 * @slave: slave struct to work on
2142 * @length: length of the data received
2144 * It is assumed that frames that were sent on this NIC don't returned as new
2145 * received frames (loopback). Since only the payload is given to this
2146 * function, it check for loopback.
2148 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
2151 int ret = RX_HANDLER_ANOTHER;
2153 if (length >= sizeof(struct lacpdu)) {
2155 port = &(SLAVE_AD_INFO(slave).port);
2158 pr_warning("%s: Warning: port of slave %s is uninitialized\n",
2159 slave->dev->name, slave->bond->dev->name);
2163 switch (lacpdu->subtype) {
2164 case AD_TYPE_LACPDU:
2165 ret = RX_HANDLER_CONSUMED;
2166 pr_debug("Received LACPDU on port %d\n",
2167 port->actor_port_number);
2168 /* Protect against concurrent state machines */
2169 __get_state_machine_lock(port);
2170 ad_rx_machine(lacpdu, port);
2171 __release_state_machine_lock(port);
2174 case AD_TYPE_MARKER:
2175 ret = RX_HANDLER_CONSUMED;
2176 // No need to convert fields to Little Endian since we don't use the marker's fields.
2178 switch (((struct bond_marker *)lacpdu)->tlv_type) {
2179 case AD_MARKER_INFORMATION_SUBTYPE:
2180 pr_debug("Received Marker Information on port %d\n",
2181 port->actor_port_number);
2182 ad_marker_info_received((struct bond_marker *)lacpdu, port);
2185 case AD_MARKER_RESPONSE_SUBTYPE:
2186 pr_debug("Received Marker Response on port %d\n",
2187 port->actor_port_number);
2188 ad_marker_response_received((struct bond_marker *)lacpdu, port);
2192 pr_debug("Received an unknown Marker subtype on slot %d\n",
2193 port->actor_port_number);
2201 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2202 * @slave: slave struct to work on
2204 * Handle reselection of aggregator (if needed) for this port.
2206 void bond_3ad_adapter_speed_changed(struct slave *slave)
2210 port = &(SLAVE_AD_INFO(slave).port);
2212 // if slave is null, the whole port is not initialized
2214 pr_warning("Warning: %s: speed changed for uninitialized port on %s\n",
2215 slave->bond->dev->name, slave->dev->name);
2219 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2220 port->actor_oper_port_key = port->actor_admin_port_key |=
2221 (__get_link_speed(port) << 1);
2222 pr_debug("Port %d changed speed\n", port->actor_port_number);
2223 // there is no need to reselect a new aggregator, just signal the
2224 // state machines to reinitialize
2225 port->sm_vars |= AD_PORT_BEGIN;
2229 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2230 * @slave: slave struct to work on
2232 * Handle reselection of aggregator (if needed) for this port.
2234 void bond_3ad_adapter_duplex_changed(struct slave *slave)
2238 port = &(SLAVE_AD_INFO(slave).port);
2240 // if slave is null, the whole port is not initialized
2242 pr_warning("%s: Warning: duplex changed for uninitialized port on %s\n",
2243 slave->bond->dev->name, slave->dev->name);
2247 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2248 port->actor_oper_port_key = port->actor_admin_port_key |=
2250 pr_debug("Port %d changed duplex\n", port->actor_port_number);
2251 // there is no need to reselect a new aggregator, just signal the
2252 // state machines to reinitialize
2253 port->sm_vars |= AD_PORT_BEGIN;
2257 * bond_3ad_handle_link_change - handle a slave's link status change indication
2258 * @slave: slave struct to work on
2259 * @status: whether the link is now up or down
2261 * Handle reselection of aggregator (if needed) for this port.
2263 void bond_3ad_handle_link_change(struct slave *slave, char link)
2267 port = &(SLAVE_AD_INFO(slave).port);
2269 // if slave is null, the whole port is not initialized
2271 pr_warning("Warning: %s: link status changed for uninitialized port on %s\n",
2272 slave->bond->dev->name, slave->dev->name);
2276 // on link down we are zeroing duplex and speed since some of the adaptors(ce1000.lan) report full duplex/speed instead of N/A(duplex) / 0(speed)
2277 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2278 if (link == BOND_LINK_UP) {
2279 port->is_enabled = true;
2280 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2281 port->actor_oper_port_key = port->actor_admin_port_key |=
2283 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2284 port->actor_oper_port_key = port->actor_admin_port_key |=
2285 (__get_link_speed(port) << 1);
2287 /* link has failed */
2288 port->is_enabled = false;
2289 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2290 port->actor_oper_port_key = (port->actor_admin_port_key &=
2291 ~AD_SPEED_KEY_BITS);
2293 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2294 // there is no need to reselect a new aggregator, just signal the
2295 // state machines to reinitialize
2296 port->sm_vars |= AD_PORT_BEGIN;
2300 * set link state for bonding master: if we have an active
2301 * aggregator, we're up, if not, we're down. Presumes that we cannot
2302 * have an active aggregator if there are no slaves with link up.
2304 * This behavior complies with IEEE 802.3 section 43.3.9.
2306 * Called by bond_set_carrier(). Return zero if carrier state does not
2307 * change, nonzero if it does.
2309 int bond_3ad_set_carrier(struct bonding *bond)
2311 struct aggregator *active;
2312 struct slave *first_slave;
2315 first_slave = bond_first_slave_rcu(bond);
2319 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave).aggregator));
2321 /* are enough slaves available to consider link up? */
2322 if (active->num_of_ports < bond->params.min_links) {
2323 if (netif_carrier_ok(bond->dev)) {
2324 netif_carrier_off(bond->dev);
2327 } else if (!netif_carrier_ok(bond->dev)) {
2328 netif_carrier_on(bond->dev);
2334 if (netif_carrier_ok(bond->dev)) {
2335 netif_carrier_off(bond->dev);
2342 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2343 * @bond: bonding struct to work on
2344 * @ad_info: ad_info struct to fill with the bond's info
2346 * Returns: 0 on success
2349 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2350 struct ad_info *ad_info)
2352 struct aggregator *aggregator = NULL;
2353 struct list_head *iter;
2354 struct slave *slave;
2357 bond_for_each_slave_rcu(bond, slave, iter) {
2358 port = &(SLAVE_AD_INFO(slave).port);
2359 if (port->aggregator && port->aggregator->is_active) {
2360 aggregator = port->aggregator;
2366 ad_info->aggregator_id = aggregator->aggregator_identifier;
2367 ad_info->ports = aggregator->num_of_ports;
2368 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2369 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2370 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2377 /* Wrapper used to hold bond->lock so no slave manipulation can occur */
2378 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2383 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2389 int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2391 struct bonding *bond = netdev_priv(dev);
2392 struct slave *slave, *first_ok_slave;
2393 struct aggregator *agg;
2394 struct ad_info ad_info;
2395 struct list_head *iter;
2400 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
2401 pr_debug("%s: Error: __bond_3ad_get_active_agg_info failed\n",
2406 slaves_in_agg = ad_info.ports;
2407 agg_id = ad_info.aggregator_id;
2409 if (slaves_in_agg == 0) {
2410 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
2414 slave_agg_no = bond_xmit_hash(bond, skb, slaves_in_agg);
2415 first_ok_slave = NULL;
2417 bond_for_each_slave_rcu(bond, slave, iter) {
2418 agg = SLAVE_AD_INFO(slave).port.aggregator;
2419 if (!agg || agg->aggregator_identifier != agg_id)
2422 if (slave_agg_no >= 0) {
2423 if (!first_ok_slave && SLAVE_IS_OK(slave))
2424 first_ok_slave = slave;
2429 if (SLAVE_IS_OK(slave)) {
2430 bond_dev_queue_xmit(bond, skb, slave->dev);
2435 if (slave_agg_no >= 0) {
2436 pr_err("%s: Error: Couldn't find a slave to tx on for aggregator ID %d\n",
2441 /* we couldn't find any suitable slave after the agg_no, so use the
2442 * first suitable found, if found. */
2444 bond_dev_queue_xmit(bond, skb, first_ok_slave->dev);
2449 return NETDEV_TX_OK;
2451 /* no suitable interface, frame not sent */
2456 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2457 struct slave *slave)
2459 int ret = RX_HANDLER_ANOTHER;
2460 struct lacpdu *lacpdu, _lacpdu;
2462 if (skb->protocol != PKT_TYPE_LACPDU)
2465 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2469 read_lock(&bond->lock);
2470 ret = bond_3ad_rx_indication(lacpdu, slave, skb->len);
2471 read_unlock(&bond->lock);
2476 * When modify lacp_rate parameter via sysfs,
2477 * update actor_oper_port_state of each port.
2479 * Hold slave->state_machine_lock,
2480 * so we can modify port->actor_oper_port_state,
2481 * no matter bond is up or down.
2483 void bond_3ad_update_lacp_rate(struct bonding *bond)
2485 struct port *port = NULL;
2486 struct list_head *iter;
2487 struct slave *slave;
2490 lacp_fast = bond->params.lacp_fast;
2491 bond_for_each_slave(bond, slave, iter) {
2492 port = &(SLAVE_AD_INFO(slave).port);
2493 __get_state_machine_lock(port);
2495 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2497 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
2498 __release_state_machine_lock(port);