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
65 #define AD_PORT_BEGIN 0x1
66 #define AD_PORT_LACP_ENABLED 0x2
67 #define AD_PORT_ACTOR_CHURN 0x4
68 #define AD_PORT_PARTNER_CHURN 0x8
69 #define AD_PORT_READY 0x10
70 #define AD_PORT_READY_N 0x20
71 #define AD_PORT_MATCHED 0x40
72 #define AD_PORT_STANDBY 0x80
73 #define AD_PORT_SELECTED 0x100
74 #define AD_PORT_MOVED 0x200
76 /* Port Key definitions
77 * key is determined according to the link speed, duplex and
78 * user key (which is yet not supported)
79 * --------------------------------------------------------------
80 * Port key : | User key | Speed | Duplex |
81 * --------------------------------------------------------------
84 #define AD_DUPLEX_KEY_BITS 0x1
85 #define AD_SPEED_KEY_BITS 0x3E
86 #define AD_USER_KEY_BITS 0xFFC0
88 #define AD_LINK_SPEED_BITMASK_1MBPS 0x1
89 #define AD_LINK_SPEED_BITMASK_10MBPS 0x2
90 #define AD_LINK_SPEED_BITMASK_100MBPS 0x4
91 #define AD_LINK_SPEED_BITMASK_1000MBPS 0x8
92 #define AD_LINK_SPEED_BITMASK_10000MBPS 0x10
94 /* compare MAC addresses */
95 #define MAC_ADDRESS_EQUAL(A, B) \
96 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
98 static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
99 static u16 ad_ticks_per_sec;
100 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
102 static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
104 /* ================= main 802.3ad protocol functions ================== */
105 static int ad_lacpdu_send(struct port *port);
106 static int ad_marker_send(struct port *port, struct bond_marker *marker);
107 static void ad_mux_machine(struct port *port);
108 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
109 static void ad_tx_machine(struct port *port);
110 static void ad_periodic_machine(struct port *port);
111 static void ad_port_selection_logic(struct port *port);
112 static void ad_agg_selection_logic(struct aggregator *aggregator);
113 static void ad_clear_agg(struct aggregator *aggregator);
114 static void ad_initialize_agg(struct aggregator *aggregator);
115 static void ad_initialize_port(struct port *port, int lacp_fast);
116 static void ad_enable_collecting_distributing(struct port *port);
117 static void ad_disable_collecting_distributing(struct port *port);
118 static void ad_marker_info_received(struct bond_marker *marker_info,
120 static void ad_marker_response_received(struct bond_marker *marker,
124 /* ================= api to bonding and kernel code ================== */
127 * __get_bond_by_port - get the port's bonding struct
128 * @port: the port we're looking at
130 * Return @port's bonding struct, or %NULL if it can't be found.
132 static inline struct bonding *__get_bond_by_port(struct port *port)
134 if (port->slave == NULL)
137 return bond_get_bond_by_slave(port->slave);
141 * __get_first_agg - get the first aggregator in the bond
142 * @bond: the bond we're looking at
144 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
147 static inline struct aggregator *__get_first_agg(struct port *port)
149 struct bonding *bond = __get_bond_by_port(port);
150 struct slave *first_slave;
152 /* If there's no bond for this port, or bond has no slaves */
157 first_slave = bond_first_slave_rcu(bond);
160 return first_slave ? &(SLAVE_AD_INFO(first_slave).aggregator) : NULL;
164 * __agg_has_partner - see if we have a partner
165 * @agg: the agregator we're looking at
167 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
168 * address for the partner). Return 0 if not.
170 static inline int __agg_has_partner(struct aggregator *agg)
172 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
176 * __disable_port - disable the port's slave
177 * @port: the port we're looking at
179 static inline void __disable_port(struct port *port)
181 bond_set_slave_inactive_flags(port->slave);
185 * __enable_port - enable the port's slave, if it's up
186 * @port: the port we're looking at
188 static inline void __enable_port(struct port *port)
190 struct slave *slave = port->slave;
192 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev))
193 bond_set_slave_active_flags(slave);
197 * __port_is_enabled - check if the port's slave is in active state
198 * @port: the port we're looking at
200 static inline int __port_is_enabled(struct port *port)
202 return bond_is_active_slave(port->slave);
206 * __get_agg_selection_mode - get the aggregator selection mode
207 * @port: the port we're looking at
209 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
211 static inline u32 __get_agg_selection_mode(struct port *port)
213 struct bonding *bond = __get_bond_by_port(port);
216 return BOND_AD_STABLE;
218 return bond->params.ad_select;
222 * __check_agg_selection_timer - check if the selection timer has expired
223 * @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
239 static inline void __get_state_machine_lock(struct port *port)
241 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
245 * __release_state_machine_lock - unlock the port's state machines
246 * @port: the port we're looking at
248 static inline void __release_state_machine_lock(struct port *port)
250 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
254 * __get_link_speed - get a port's speed
255 * @port: the port we're looking at
257 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
259 * %AD_LINK_SPEED_BITMASK_10MBPS,
260 * %AD_LINK_SPEED_BITMASK_100MBPS,
261 * %AD_LINK_SPEED_BITMASK_1000MBPS,
262 * %AD_LINK_SPEED_BITMASK_10000MBPS
264 static u16 __get_link_speed(struct port *port)
266 struct slave *slave = port->slave;
269 /* this if covers only a special case: when the configuration starts
270 * with link down, it sets the speed to 0.
271 * This is done in spite of the fact that the e100 driver reports 0
272 * to be compatible with MVT in the future.
274 if (slave->link != BOND_LINK_UP)
277 switch (slave->speed) {
279 speed = AD_LINK_SPEED_BITMASK_10MBPS;
283 speed = AD_LINK_SPEED_BITMASK_100MBPS;
287 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
291 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
295 /* 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.
323 if (slave->link != BOND_LINK_UP)
326 switch (slave->duplex) {
329 pr_debug("Port %d Received status full duplex update from adapter\n",
330 port->actor_port_number);
335 pr_debug("Port %d Received status NOT full duplex update from adapter\n",
336 port->actor_port_number);
344 * __initialize_port_locks - initialize a port's STATE machine spinlock
345 * @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);
373 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
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);
393 /* ================= ad_rx_machine helper functions ================== */
396 * __choose_matched - update a port's matched variable from a received lacpdu
397 * @lacpdu: the lacpdu we've received
398 * @port: the port we're looking at
400 * Update the value of the matched variable, using parameter values from a
401 * newly received lacpdu. Parameter values for the partner carried in the
402 * received PDU are compared with the corresponding operational parameter
403 * values for the actor. Matched is set to TRUE if all of these parameters
404 * match and the PDU parameter partner_state.aggregation has the same value as
405 * actor_oper_port_state.aggregation and lacp will actively maintain the link
406 * in the aggregation. Matched is also set to TRUE if the value of
407 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
408 * an individual link and lacp will actively maintain the link. Otherwise,
409 * matched is set to FALSE. LACP is considered to be actively maintaining the
410 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
411 * the actor's actor_oper_port_state.lacp_activity and the PDU's
412 * partner_state.lacp_activity variables are TRUE.
414 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
415 * used here to implement the language from 802.3ad 43.4.9 that requires
416 * recordPDU to "match" the LACPDU parameters to the stored values.
418 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
420 /* check if all parameters are alike
421 * or this is individual link(aggregation == FALSE)
422 * then update the state machine Matched variable.
424 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
425 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
426 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
427 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
428 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
429 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
430 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
432 port->sm_vars |= AD_PORT_MATCHED;
434 port->sm_vars &= ~AD_PORT_MATCHED;
439 * __record_pdu - record parameters from a received lacpdu
440 * @lacpdu: the lacpdu we've received
441 * @port: the port we're looking at
443 * Record the parameter values for the Actor carried in a received lacpdu as
444 * the current partner operational parameter values and sets
445 * actor_oper_port_state.defaulted to FALSE.
447 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
449 if (lacpdu && port) {
450 struct port_params *partner = &port->partner_oper;
452 __choose_matched(lacpdu, port);
453 /* record the new parameter values for the partner
456 partner->port_number = ntohs(lacpdu->actor_port);
457 partner->port_priority = ntohs(lacpdu->actor_port_priority);
458 partner->system = lacpdu->actor_system;
459 partner->system_priority = ntohs(lacpdu->actor_system_priority);
460 partner->key = ntohs(lacpdu->actor_key);
461 partner->port_state = lacpdu->actor_state;
463 /* set actor_oper_port_state.defaulted to FALSE */
464 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
466 /* set the partner sync. to on if the partner is sync,
467 * and the port is matched
469 if ((port->sm_vars & AD_PORT_MATCHED)
470 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
471 partner->port_state |= AD_STATE_SYNCHRONIZATION;
473 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
478 * __record_default - record default parameters
479 * @port: the port we're looking at
481 * This function records the default parameter values for the partner carried
482 * in the Partner Admin parameters as the current partner operational parameter
483 * values and sets actor_oper_port_state.defaulted to TRUE.
485 static void __record_default(struct port *port)
488 /* record the partner admin parameters */
489 memcpy(&port->partner_oper, &port->partner_admin,
490 sizeof(struct port_params));
492 /* set actor_oper_port_state.defaulted to true */
493 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
498 * __update_selected - update a port's Selected variable from a received lacpdu
499 * @lacpdu: the lacpdu we've received
500 * @port: the port we're looking at
502 * Update the value of the selected variable, using parameter values from a
503 * newly received lacpdu. The parameter values for the Actor carried in the
504 * received PDU are compared with the corresponding operational parameter
505 * values for the ports partner. If one or more of the comparisons shows that
506 * the value(s) received in the PDU differ from the current operational values,
507 * then selected is set to FALSE and actor_oper_port_state.synchronization is
508 * set to out_of_sync. Otherwise, selected remains unchanged.
510 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
512 if (lacpdu && port) {
513 const struct port_params *partner = &port->partner_oper;
515 /* check if any parameter is different then
516 * update the state machine selected variable.
518 if (ntohs(lacpdu->actor_port) != partner->port_number ||
519 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
520 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
521 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
522 ntohs(lacpdu->actor_key) != partner->key ||
523 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
524 port->sm_vars &= ~AD_PORT_SELECTED;
530 * __update_default_selected - update a port's Selected variable from Partner
531 * @port: the port we're looking at
533 * This function updates the value of the selected variable, using the partner
534 * administrative parameter values. The administrative values are compared with
535 * the corresponding operational parameter values for the partner. If one or
536 * more of the comparisons shows that the administrative value(s) differ from
537 * the current operational values, then Selected is set to FALSE and
538 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
539 * Selected remains unchanged.
541 static void __update_default_selected(struct port *port)
544 const struct port_params *admin = &port->partner_admin;
545 const struct port_params *oper = &port->partner_oper;
547 /* check if any parameter is different then
548 * update the state machine selected variable.
550 if (admin->port_number != oper->port_number ||
551 admin->port_priority != oper->port_priority ||
552 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
553 admin->system_priority != oper->system_priority ||
554 admin->key != oper->key ||
555 (admin->port_state & AD_STATE_AGGREGATION)
556 != (oper->port_state & AD_STATE_AGGREGATION)) {
557 port->sm_vars &= ~AD_PORT_SELECTED;
563 * __update_ntt - update a port's ntt variable from a received lacpdu
564 * @lacpdu: the lacpdu we've received
565 * @port: the port we're looking at
567 * Updates the value of the ntt variable, using parameter values from a newly
568 * received lacpdu. The parameter values for the partner carried in the
569 * received PDU are compared with the corresponding operational parameter
570 * values for the Actor. If one or more of the comparisons shows that the
571 * value(s) received in the PDU differ from the current operational values,
572 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
574 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
576 /* validate lacpdu and port */
577 if (lacpdu && port) {
578 /* check if any parameter is different then
579 * update the port->ntt.
581 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
582 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
583 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
584 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
585 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
586 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
587 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
588 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
589 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
597 * __agg_ports_are_ready - check if all ports in an aggregator are ready
598 * @aggregator: the aggregator we're looking at
601 static int __agg_ports_are_ready(struct aggregator *aggregator)
607 /* scan all ports in this aggregator to verfy if they are
610 for (port = aggregator->lag_ports;
612 port = port->next_port_in_aggregator) {
613 if (!(port->sm_vars & AD_PORT_READY_N)) {
624 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
625 * @aggregator: the aggregator we're looking at
626 * @val: Should the ports' ready bit be set on or off
629 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
633 for (port = aggregator->lag_ports; port;
634 port = port->next_port_in_aggregator) {
636 port->sm_vars |= AD_PORT_READY;
638 port->sm_vars &= ~AD_PORT_READY;
643 * __get_agg_bandwidth - get the total bandwidth of an aggregator
644 * @aggregator: the aggregator we're looking at
647 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
651 if (aggregator->num_of_ports) {
652 switch (__get_link_speed(aggregator->lag_ports)) {
653 case AD_LINK_SPEED_BITMASK_1MBPS:
654 bandwidth = aggregator->num_of_ports;
656 case AD_LINK_SPEED_BITMASK_10MBPS:
657 bandwidth = aggregator->num_of_ports * 10;
659 case AD_LINK_SPEED_BITMASK_100MBPS:
660 bandwidth = aggregator->num_of_ports * 100;
662 case AD_LINK_SPEED_BITMASK_1000MBPS:
663 bandwidth = aggregator->num_of_ports * 1000;
665 case AD_LINK_SPEED_BITMASK_10000MBPS:
666 bandwidth = aggregator->num_of_ports * 10000;
669 bandwidth = 0; /* to silence the compiler */
676 * __get_active_agg - get the current active aggregator
677 * @aggregator: the aggregator we're looking at
679 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
681 struct bonding *bond = aggregator->slave->bond;
682 struct list_head *iter;
686 bond_for_each_slave_rcu(bond, slave, iter)
687 if (SLAVE_AD_INFO(slave).aggregator.is_active) {
689 return &(SLAVE_AD_INFO(slave).aggregator);
697 * __update_lacpdu_from_port - update a port's lacpdu fields
698 * @port: the port we're looking at
700 static inline void __update_lacpdu_from_port(struct port *port)
702 struct lacpdu *lacpdu = &port->lacpdu;
703 const struct port_params *partner = &port->partner_oper;
705 /* update current actual Actor parameters
706 * lacpdu->subtype initialized
707 * lacpdu->version_number initialized
708 * lacpdu->tlv_type_actor_info initialized
709 * lacpdu->actor_information_length initialized
712 lacpdu->actor_system_priority = htons(port->actor_system_priority);
713 lacpdu->actor_system = port->actor_system;
714 lacpdu->actor_key = htons(port->actor_oper_port_key);
715 lacpdu->actor_port_priority = htons(port->actor_port_priority);
716 lacpdu->actor_port = htons(port->actor_port_number);
717 lacpdu->actor_state = port->actor_oper_port_state;
719 /* lacpdu->reserved_3_1 initialized
720 * lacpdu->tlv_type_partner_info initialized
721 * lacpdu->partner_information_length initialized
724 lacpdu->partner_system_priority = htons(partner->system_priority);
725 lacpdu->partner_system = partner->system;
726 lacpdu->partner_key = htons(partner->key);
727 lacpdu->partner_port_priority = htons(partner->port_priority);
728 lacpdu->partner_port = htons(partner->port_number);
729 lacpdu->partner_state = partner->port_state;
731 /* lacpdu->reserved_3_2 initialized
732 * lacpdu->tlv_type_collector_info initialized
733 * lacpdu->collector_information_length initialized
734 * collector_max_delay initialized
735 * reserved_12[12] initialized
736 * tlv_type_terminator initialized
737 * terminator_length initialized
738 * reserved_50[50] initialized
742 /* ================= main 802.3ad protocol code ========================= */
745 * ad_lacpdu_send - send out a lacpdu packet on a given port
746 * @port: the port we're looking at
748 * Returns: 0 on success
751 static int ad_lacpdu_send(struct port *port)
753 struct slave *slave = port->slave;
755 struct lacpdu_header *lacpdu_header;
756 int length = sizeof(struct lacpdu_header);
758 skb = dev_alloc_skb(length);
762 skb->dev = slave->dev;
763 skb_reset_mac_header(skb);
764 skb->network_header = skb->mac_header + ETH_HLEN;
765 skb->protocol = PKT_TYPE_LACPDU;
766 skb->priority = TC_PRIO_CONTROL;
768 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
770 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
771 /* Note: source address is set to be the member's PERMANENT address,
772 * because we use it to identify loopback lacpdus in receive.
774 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
775 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
777 lacpdu_header->lacpdu = port->lacpdu;
785 * ad_marker_send - send marker information/response on a given port
786 * @port: the port we're looking at
787 * @marker: marker data to send
789 * Returns: 0 on success
792 static int ad_marker_send(struct port *port, struct bond_marker *marker)
794 struct slave *slave = port->slave;
796 struct bond_marker_header *marker_header;
797 int length = sizeof(struct bond_marker_header);
799 skb = dev_alloc_skb(length + 16);
803 skb_reserve(skb, 16);
805 skb->dev = slave->dev;
806 skb_reset_mac_header(skb);
807 skb->network_header = skb->mac_header + ETH_HLEN;
808 skb->protocol = PKT_TYPE_LACPDU;
810 marker_header = (struct bond_marker_header *)skb_put(skb, length);
812 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
813 /* Note: source address is set to be the member's PERMANENT address,
814 * because we use it to identify loopback MARKERs in receive.
816 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
817 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
819 marker_header->marker = *marker;
827 * ad_mux_machine - handle a port's mux state machine
828 * @port: the port we're looking at
830 static void ad_mux_machine(struct port *port)
832 mux_states_t last_state;
834 /* keep current State Machine state to compare later if it was
837 last_state = port->sm_mux_state;
839 if (port->sm_vars & AD_PORT_BEGIN) {
840 port->sm_mux_state = AD_MUX_DETACHED;
842 switch (port->sm_mux_state) {
843 case AD_MUX_DETACHED:
844 if ((port->sm_vars & AD_PORT_SELECTED)
845 || (port->sm_vars & AD_PORT_STANDBY))
846 /* if SELECTED or STANDBY */
847 port->sm_mux_state = AD_MUX_WAITING;
850 /* if SELECTED == FALSE return to DETACH state */
851 if (!(port->sm_vars & AD_PORT_SELECTED)) {
852 port->sm_vars &= ~AD_PORT_READY_N;
853 /* in order to withhold the Selection Logic to
854 * check all ports READY_N value every callback
855 * cycle to update ready variable, we check
856 * READY_N and update READY here
858 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
859 port->sm_mux_state = AD_MUX_DETACHED;
863 /* check if the wait_while_timer expired */
864 if (port->sm_mux_timer_counter
865 && !(--port->sm_mux_timer_counter))
866 port->sm_vars |= AD_PORT_READY_N;
868 /* in order to withhold the selection logic to check
869 * all ports READY_N value every callback cycle to
870 * update ready variable, we check READY_N and update
873 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
875 /* if the wait_while_timer expired, and the port is
876 * in READY state, move to ATTACHED state
878 if ((port->sm_vars & AD_PORT_READY)
879 && !port->sm_mux_timer_counter)
880 port->sm_mux_state = AD_MUX_ATTACHED;
882 case AD_MUX_ATTACHED:
883 /* check also if agg_select_timer expired (so the
884 * edable port will take place only after this timer)
886 if ((port->sm_vars & AD_PORT_SELECTED) &&
887 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
888 !__check_agg_selection_timer(port)) {
889 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;
890 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
891 (port->sm_vars & AD_PORT_STANDBY)) {
892 /* if UNSELECTED or STANDBY */
893 port->sm_vars &= ~AD_PORT_READY_N;
894 /* in order to withhold the selection logic to
895 * check all ports READY_N value every callback
896 * cycle to update ready variable, we check
897 * READY_N and update READY here
899 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
900 port->sm_mux_state = AD_MUX_DETACHED;
903 case AD_MUX_COLLECTING_DISTRIBUTING:
904 if (!(port->sm_vars & AD_PORT_SELECTED) ||
905 (port->sm_vars & AD_PORT_STANDBY) ||
906 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)) {
907 port->sm_mux_state = AD_MUX_ATTACHED;
909 /* if port state hasn't changed make
910 * sure that a collecting distributing
911 * port in an active aggregator is enabled
913 if (port->aggregator &&
914 port->aggregator->is_active &&
915 !__port_is_enabled(port)) {
926 /* check if the state machine was changed */
927 if (port->sm_mux_state != last_state) {
928 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
929 port->actor_port_number, last_state,
931 switch (port->sm_mux_state) {
932 case AD_MUX_DETACHED:
933 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
934 ad_disable_collecting_distributing(port);
935 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
936 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
940 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
942 case AD_MUX_ATTACHED:
943 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
944 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
945 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
946 ad_disable_collecting_distributing(port);
949 case AD_MUX_COLLECTING_DISTRIBUTING:
950 port->actor_oper_port_state |= AD_STATE_COLLECTING;
951 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
952 ad_enable_collecting_distributing(port);
962 * ad_rx_machine - handle a port's rx State Machine
963 * @lacpdu: the lacpdu we've received
964 * @port: the port we're looking at
966 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
967 * CURRENT. If timer expired set the state machine in the proper state.
968 * In other cases, this function checks if we need to switch to other state.
970 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
972 rx_states_t last_state;
974 /* keep current State Machine state to compare later if it was
977 last_state = port->sm_rx_state;
979 /* check if state machine should change state */
981 /* first, check if port was reinitialized */
982 if (port->sm_vars & AD_PORT_BEGIN)
983 port->sm_rx_state = AD_RX_INITIALIZE;
984 /* check if port is not enabled */
985 else if (!(port->sm_vars & AD_PORT_BEGIN)
986 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
987 port->sm_rx_state = AD_RX_PORT_DISABLED;
988 /* check if new lacpdu arrived */
989 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
990 (port->sm_rx_state == AD_RX_DEFAULTED) ||
991 (port->sm_rx_state == AD_RX_CURRENT))) {
992 port->sm_rx_timer_counter = 0;
993 port->sm_rx_state = AD_RX_CURRENT;
995 /* if timer is on, and if it is expired */
996 if (port->sm_rx_timer_counter &&
997 !(--port->sm_rx_timer_counter)) {
998 switch (port->sm_rx_state) {
1000 port->sm_rx_state = AD_RX_DEFAULTED;
1003 port->sm_rx_state = AD_RX_EXPIRED;
1009 /* if no lacpdu arrived and no timer is on */
1010 switch (port->sm_rx_state) {
1011 case AD_RX_PORT_DISABLED:
1012 if (port->sm_vars & AD_PORT_MOVED)
1013 port->sm_rx_state = AD_RX_INITIALIZE;
1014 else if (port->is_enabled
1016 & AD_PORT_LACP_ENABLED))
1017 port->sm_rx_state = AD_RX_EXPIRED;
1018 else if (port->is_enabled
1020 & AD_PORT_LACP_ENABLED) == 0))
1021 port->sm_rx_state = AD_RX_LACP_DISABLED;
1030 /* check if the State machine was changed or new lacpdu arrived */
1031 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1032 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1033 port->actor_port_number, last_state,
1035 switch (port->sm_rx_state) {
1036 case AD_RX_INITIALIZE:
1037 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
1038 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1040 port->sm_vars |= AD_PORT_LACP_ENABLED;
1041 port->sm_vars &= ~AD_PORT_SELECTED;
1042 __record_default(port);
1043 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1044 port->sm_vars &= ~AD_PORT_MOVED;
1045 port->sm_rx_state = AD_RX_PORT_DISABLED;
1048 case AD_RX_PORT_DISABLED:
1049 port->sm_vars &= ~AD_PORT_MATCHED;
1051 case AD_RX_LACP_DISABLED:
1052 port->sm_vars &= ~AD_PORT_SELECTED;
1053 __record_default(port);
1054 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1055 port->sm_vars |= AD_PORT_MATCHED;
1056 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1059 /* Reset of the Synchronization flag (Standard 43.4.12)
1060 * This reset cause to disable this port in the
1061 * COLLECTING_DISTRIBUTING state of the mux machine in
1062 * case of EXPIRED even if LINK_DOWN didn't arrive for
1065 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1066 port->sm_vars &= ~AD_PORT_MATCHED;
1067 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
1068 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1069 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1071 case AD_RX_DEFAULTED:
1072 __update_default_selected(port);
1073 __record_default(port);
1074 port->sm_vars |= AD_PORT_MATCHED;
1075 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1078 /* detect loopback situation */
1079 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1080 &(port->actor_system))) {
1081 pr_err("%s: An illegal loopback occurred on adapter (%s).\nCheck the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1082 port->slave->bond->dev->name,
1083 port->slave->dev->name);
1086 __update_selected(lacpdu, port);
1087 __update_ntt(lacpdu, port);
1088 __record_pdu(lacpdu, port);
1089 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1090 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1099 * ad_tx_machine - handle a port's tx state machine
1100 * @port: the port we're looking at
1102 static void ad_tx_machine(struct port *port)
1104 /* check if tx timer expired, to verify that we do not send more than
1105 * 3 packets per second
1107 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1108 /* check if there is something to send */
1109 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1110 __update_lacpdu_from_port(port);
1112 if (ad_lacpdu_send(port) >= 0) {
1113 pr_debug("Sent LACPDU on port %d\n",
1114 port->actor_port_number);
1116 /* mark ntt as false, so it will not be sent
1117 * again until demanded
1122 /* restart tx timer(to verify that we will not exceed
1123 * AD_MAX_TX_IN_SECOND
1125 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1130 * ad_periodic_machine - handle a port's periodic state machine
1131 * @port: the port we're looking at
1133 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1135 static void ad_periodic_machine(struct port *port)
1137 periodic_states_t last_state;
1139 /* keep current state machine state to compare later if it was changed */
1140 last_state = port->sm_periodic_state;
1142 /* check if port was reinitialized */
1143 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1144 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1146 port->sm_periodic_state = AD_NO_PERIODIC;
1148 /* check if state machine should change state */
1149 else if (port->sm_periodic_timer_counter) {
1150 /* check if periodic state machine expired */
1151 if (!(--port->sm_periodic_timer_counter)) {
1152 /* if expired then do tx */
1153 port->sm_periodic_state = AD_PERIODIC_TX;
1155 /* If not expired, check if there is some new timeout
1156 * 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;
1164 case AD_SLOW_PERIODIC:
1165 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1166 port->sm_periodic_timer_counter = 0;
1167 port->sm_periodic_state = AD_PERIODIC_TX;
1175 switch (port->sm_periodic_state) {
1176 case AD_NO_PERIODIC:
1177 port->sm_periodic_state = AD_FAST_PERIODIC;
1179 case AD_PERIODIC_TX:
1180 if (!(port->partner_oper.port_state &
1181 AD_STATE_LACP_TIMEOUT))
1182 port->sm_periodic_state = AD_SLOW_PERIODIC;
1184 port->sm_periodic_state = AD_FAST_PERIODIC;
1191 /* check if the state machine was changed */
1192 if (port->sm_periodic_state != last_state) {
1193 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1194 port->actor_port_number, last_state,
1195 port->sm_periodic_state);
1196 switch (port->sm_periodic_state) {
1197 case AD_NO_PERIODIC:
1198 port->sm_periodic_timer_counter = 0;
1200 case AD_FAST_PERIODIC:
1201 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1202 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1204 case AD_SLOW_PERIODIC:
1205 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1206 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1208 case AD_PERIODIC_TX:
1218 * ad_port_selection_logic - select aggregation groups
1219 * @port: the port we're looking at
1221 * Select aggregation groups, and assign each port for it's aggregetor. The
1222 * selection logic is called in the inititalization (after all the handshkes),
1223 * and after every lacpdu receive (if selected is off).
1225 static void ad_port_selection_logic(struct port *port)
1227 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1228 struct port *last_port = NULL, *curr_port;
1229 struct list_head *iter;
1230 struct bonding *bond;
1231 struct slave *slave;
1234 /* if the port is already Selected, do nothing */
1235 if (port->sm_vars & AD_PORT_SELECTED)
1238 bond = __get_bond_by_port(port);
1240 /* if the port is connected to other aggregator, detach it */
1241 if (port->aggregator) {
1242 /* detach the port from its former aggregator */
1243 temp_aggregator = port->aggregator;
1244 for (curr_port = temp_aggregator->lag_ports; curr_port;
1245 last_port = curr_port,
1246 curr_port = curr_port->next_port_in_aggregator) {
1247 if (curr_port == port) {
1248 temp_aggregator->num_of_ports--;
1249 /* if it is the first port attached to the
1253 temp_aggregator->lag_ports =
1254 port->next_port_in_aggregator;
1256 /* not the first port attached to the
1259 last_port->next_port_in_aggregator =
1260 port->next_port_in_aggregator;
1263 /* clear the port's relations to this
1266 port->aggregator = NULL;
1267 port->next_port_in_aggregator = NULL;
1268 port->actor_port_aggregator_identifier = 0;
1270 pr_debug("Port %d left LAG %d\n",
1271 port->actor_port_number,
1272 temp_aggregator->aggregator_identifier);
1273 /* if the aggregator is empty, clear its
1274 * parameters, and set it ready to be attached
1276 if (!temp_aggregator->lag_ports)
1277 ad_clear_agg(temp_aggregator);
1282 /* meaning: the port was related to an aggregator
1283 * but was not on the aggregator port list
1285 pr_warn("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1286 port->slave->bond->dev->name,
1287 port->actor_port_number,
1288 port->slave->dev->name,
1289 port->aggregator->aggregator_identifier);
1292 /* search on all aggregators for a suitable aggregator for this port */
1293 bond_for_each_slave(bond, slave, iter) {
1294 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1296 /* keep a free aggregator for later use(if needed) */
1297 if (!aggregator->lag_ports) {
1298 if (!free_aggregator)
1299 free_aggregator = aggregator;
1302 /* check if current aggregator suits us */
1303 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1304 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1305 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1306 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1308 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1309 !aggregator->is_individual) /* but is not individual OR */
1312 /* attach to the founded aggregator */
1313 port->aggregator = aggregator;
1314 port->actor_port_aggregator_identifier =
1315 port->aggregator->aggregator_identifier;
1316 port->next_port_in_aggregator = aggregator->lag_ports;
1317 port->aggregator->num_of_ports++;
1318 aggregator->lag_ports = port;
1319 pr_debug("Port %d joined LAG %d(existing LAG)\n",
1320 port->actor_port_number,
1321 port->aggregator->aggregator_identifier);
1323 /* mark this port as selected */
1324 port->sm_vars |= AD_PORT_SELECTED;
1330 /* the port couldn't find an aggregator - attach it to a new
1334 if (free_aggregator) {
1335 /* assign port a new aggregator */
1336 port->aggregator = free_aggregator;
1337 port->actor_port_aggregator_identifier =
1338 port->aggregator->aggregator_identifier;
1340 /* update the new aggregator's parameters
1341 * if port was responsed from the end-user
1343 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)
1344 /* if port is full duplex */
1345 port->aggregator->is_individual = false;
1347 port->aggregator->is_individual = true;
1349 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1350 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
1351 port->aggregator->partner_system =
1352 port->partner_oper.system;
1353 port->aggregator->partner_system_priority =
1354 port->partner_oper.system_priority;
1355 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1356 port->aggregator->receive_state = 1;
1357 port->aggregator->transmit_state = 1;
1358 port->aggregator->lag_ports = port;
1359 port->aggregator->num_of_ports++;
1361 /* mark this port as selected */
1362 port->sm_vars |= AD_PORT_SELECTED;
1364 pr_debug("Port %d joined LAG %d(new LAG)\n",
1365 port->actor_port_number,
1366 port->aggregator->aggregator_identifier);
1368 pr_err("%s: Port %d (on %s) did not find a suitable aggregator\n",
1369 port->slave->bond->dev->name,
1370 port->actor_port_number, port->slave->dev->name);
1373 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1374 * in all aggregator's ports, else set ready=FALSE in all
1375 * aggregator's ports
1377 __set_agg_ports_ready(port->aggregator,
1378 __agg_ports_are_ready(port->aggregator));
1380 aggregator = __get_first_agg(port);
1381 ad_agg_selection_logic(aggregator);
1384 /* Decide if "agg" is a better choice for the new active aggregator that
1385 * the current best, according to the ad_select policy.
1387 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1388 struct aggregator *curr)
1390 /* 0. If no best, select current.
1392 * 1. If the current agg is not individual, and the best is
1393 * individual, select current.
1395 * 2. If current agg is individual and the best is not, keep best.
1397 * 3. Therefore, current and best are both individual or both not
1400 * 3a. If current agg partner replied, and best agg partner did not,
1403 * 3b. If current agg partner did not reply and best agg partner
1404 * did reply, keep best.
1406 * 4. Therefore, current and best both have partner replies or
1407 * both do not, so perform selection policy:
1409 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1410 * select by bandwidth.
1412 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1417 if (!curr->is_individual && best->is_individual)
1420 if (curr->is_individual && !best->is_individual)
1423 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1426 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1429 switch (__get_agg_selection_mode(curr->lag_ports)) {
1431 if (curr->num_of_ports > best->num_of_ports)
1434 if (curr->num_of_ports < best->num_of_ports)
1438 case BOND_AD_STABLE:
1439 case BOND_AD_BANDWIDTH:
1440 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1446 pr_warn("%s: Impossible agg select mode %d\n",
1447 curr->slave->bond->dev->name,
1448 __get_agg_selection_mode(curr->lag_ports));
1455 static int agg_device_up(const struct aggregator *agg)
1457 struct port *port = agg->lag_ports;
1462 return netif_running(port->slave->dev) &&
1463 netif_carrier_ok(port->slave->dev);
1467 * ad_agg_selection_logic - select an aggregation group for a team
1468 * @aggregator: the aggregator we're looking at
1470 * It is assumed that only one aggregator may be selected for a team.
1472 * The logic of this function is to select the aggregator according to
1473 * the ad_select policy:
1475 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1476 * it, and to reselect the active aggregator only if the previous
1477 * aggregator has no more ports related to it.
1479 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1480 * bandwidth, and reselect whenever a link state change takes place or the
1481 * set of slaves in the bond changes.
1483 * BOND_AD_COUNT: select the aggregator with largest number of ports
1484 * (slaves), and reselect whenever a link state change takes place or the
1485 * set of slaves in the bond changes.
1487 * FIXME: this function MUST be called with the first agg in the bond, or
1488 * __get_active_agg() won't work correctly. This function should be better
1489 * called with the bond itself, and retrieve the first agg from it.
1491 static void ad_agg_selection_logic(struct aggregator *agg)
1493 struct aggregator *best, *active, *origin;
1494 struct bonding *bond = agg->slave->bond;
1495 struct list_head *iter;
1496 struct slave *slave;
1500 active = __get_active_agg(agg);
1501 best = (active && agg_device_up(active)) ? active : NULL;
1504 bond_for_each_slave_rcu(bond, slave, iter) {
1505 agg = &(SLAVE_AD_INFO(slave).aggregator);
1509 if (agg->num_of_ports && agg_device_up(agg))
1510 best = ad_agg_selection_test(best, agg);
1514 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1515 /* For the STABLE policy, don't replace the old active
1516 * aggregator if it's still active (it has an answering
1517 * partner) or if both the best and active don't have an
1518 * answering partner.
1520 if (active && active->lag_ports &&
1521 active->lag_ports->is_enabled &&
1522 (__agg_has_partner(active) ||
1523 (!__agg_has_partner(active) &&
1524 !__agg_has_partner(best)))) {
1525 if (!(!active->actor_oper_aggregator_key &&
1526 best->actor_oper_aggregator_key)) {
1528 active->is_active = 1;
1533 if (best && (best == active)) {
1535 active->is_active = 1;
1538 /* if there is new best aggregator, activate it */
1540 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1541 best->aggregator_identifier, best->num_of_ports,
1542 best->actor_oper_aggregator_key,
1543 best->partner_oper_aggregator_key,
1544 best->is_individual, best->is_active);
1545 pr_debug("best ports %p slave %p %s\n",
1546 best->lag_ports, best->slave,
1547 best->slave ? best->slave->dev->name : "NULL");
1549 bond_for_each_slave_rcu(bond, slave, iter) {
1550 agg = &(SLAVE_AD_INFO(slave).aggregator);
1552 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1553 agg->aggregator_identifier, agg->num_of_ports,
1554 agg->actor_oper_aggregator_key,
1555 agg->partner_oper_aggregator_key,
1556 agg->is_individual, agg->is_active);
1559 /* check if any partner replys */
1560 if (best->is_individual) {
1561 pr_warn("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1563 best->slave->bond->dev->name : "NULL");
1566 best->is_active = 1;
1567 pr_debug("LAG %d chosen as the active LAG\n",
1568 best->aggregator_identifier);
1569 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1570 best->aggregator_identifier, best->num_of_ports,
1571 best->actor_oper_aggregator_key,
1572 best->partner_oper_aggregator_key,
1573 best->is_individual, best->is_active);
1575 /* disable the ports that were related to the former
1579 for (port = active->lag_ports; port;
1580 port = port->next_port_in_aggregator) {
1581 __disable_port(port);
1586 /* if the selected aggregator is of join individuals
1587 * (partner_system is NULL), enable their ports
1589 active = __get_active_agg(origin);
1592 if (!__agg_has_partner(active)) {
1593 for (port = active->lag_ports; port;
1594 port = port->next_port_in_aggregator) {
1595 __enable_port(port);
1602 bond_3ad_set_carrier(bond);
1606 * ad_clear_agg - clear a given aggregator's parameters
1607 * @aggregator: the aggregator we're looking at
1609 static void ad_clear_agg(struct aggregator *aggregator)
1612 aggregator->is_individual = false;
1613 aggregator->actor_admin_aggregator_key = 0;
1614 aggregator->actor_oper_aggregator_key = 0;
1615 aggregator->partner_system = null_mac_addr;
1616 aggregator->partner_system_priority = 0;
1617 aggregator->partner_oper_aggregator_key = 0;
1618 aggregator->receive_state = 0;
1619 aggregator->transmit_state = 0;
1620 aggregator->lag_ports = NULL;
1621 aggregator->is_active = 0;
1622 aggregator->num_of_ports = 0;
1623 pr_debug("LAG %d was cleared\n",
1624 aggregator->aggregator_identifier);
1629 * ad_initialize_agg - initialize a given aggregator's parameters
1630 * @aggregator: the aggregator we're looking at
1632 static void ad_initialize_agg(struct aggregator *aggregator)
1635 ad_clear_agg(aggregator);
1637 aggregator->aggregator_mac_address = null_mac_addr;
1638 aggregator->aggregator_identifier = 0;
1639 aggregator->slave = NULL;
1644 * ad_initialize_port - initialize a given port's parameters
1645 * @aggregator: the aggregator we're looking at
1646 * @lacp_fast: boolean. whether fast periodic should be used
1648 static void ad_initialize_port(struct port *port, int lacp_fast)
1650 static const struct port_params tmpl = {
1651 .system_priority = 0xffff,
1654 .port_priority = 0xff,
1657 static const struct lacpdu lacpdu = {
1659 .version_number = 0x01,
1660 .tlv_type_actor_info = 0x01,
1661 .actor_information_length = 0x14,
1662 .tlv_type_partner_info = 0x02,
1663 .partner_information_length = 0x14,
1664 .tlv_type_collector_info = 0x03,
1665 .collector_information_length = 0x10,
1666 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1670 port->actor_port_number = 1;
1671 port->actor_port_priority = 0xff;
1672 port->actor_system = null_mac_addr;
1673 port->actor_system_priority = 0xffff;
1674 port->actor_port_aggregator_identifier = 0;
1676 port->actor_admin_port_key = 1;
1677 port->actor_oper_port_key = 1;
1678 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1679 AD_STATE_LACP_ACTIVITY;
1680 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1681 AD_STATE_LACP_ACTIVITY;
1684 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1686 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1687 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1689 port->is_enabled = true;
1690 /* private parameters */
1691 port->sm_vars = 0x3;
1692 port->sm_rx_state = 0;
1693 port->sm_rx_timer_counter = 0;
1694 port->sm_periodic_state = 0;
1695 port->sm_periodic_timer_counter = 0;
1696 port->sm_mux_state = 0;
1697 port->sm_mux_timer_counter = 0;
1698 port->sm_tx_state = 0;
1699 port->sm_tx_timer_counter = 0;
1701 port->aggregator = NULL;
1702 port->next_port_in_aggregator = NULL;
1703 port->transaction_id = 0;
1705 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1710 * ad_enable_collecting_distributing - enable a port's transmit/receive
1711 * @port: the port we're looking at
1713 * Enable @port if it's in an active aggregator
1715 static void ad_enable_collecting_distributing(struct port *port)
1717 if (port->aggregator->is_active) {
1718 pr_debug("Enabling port %d(LAG %d)\n",
1719 port->actor_port_number,
1720 port->aggregator->aggregator_identifier);
1721 __enable_port(port);
1726 * ad_disable_collecting_distributing - disable a port's transmit/receive
1727 * @port: the port we're looking at
1729 static void ad_disable_collecting_distributing(struct port *port)
1731 if (port->aggregator &&
1732 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1733 &(null_mac_addr))) {
1734 pr_debug("Disabling port %d(LAG %d)\n",
1735 port->actor_port_number,
1736 port->aggregator->aggregator_identifier);
1737 __disable_port(port);
1742 * ad_marker_info_received - handle receive of a Marker information frame
1743 * @marker_info: Marker info received
1744 * @port: the port we're looking at
1746 static void ad_marker_info_received(struct bond_marker *marker_info,
1749 struct bond_marker marker;
1751 /* copy the received marker data to the response marker */
1752 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1753 /* change the marker subtype to marker response */
1754 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1756 /* send the marker response */
1757 if (ad_marker_send(port, &marker) >= 0) {
1758 pr_debug("Sent Marker Response on port %d\n",
1759 port->actor_port_number);
1764 * ad_marker_response_received - handle receive of a marker response frame
1765 * @marker: marker PDU received
1766 * @port: the port we're looking at
1768 * This function does nothing since we decided not to implement send and handle
1769 * response for marker PDU's, in this stage, but only to respond to marker
1772 static void ad_marker_response_received(struct bond_marker *marker,
1777 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1780 /* ========= AD exported functions to the main bonding code ========= */
1782 /* Check aggregators status in team every T seconds */
1783 #define AD_AGGREGATOR_SELECTION_TIMER 8
1786 * bond_3ad_initiate_agg_selection - initate aggregator selection
1787 * @bond: bonding struct
1789 * Set the aggregation selection timer, to initiate an agg selection in
1790 * the very near future. Called during first initialization, and during
1791 * any down to up transitions of the bond.
1793 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1795 BOND_AD_INFO(bond).agg_select_timer = timeout;
1798 static u16 aggregator_identifier;
1801 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1802 * @bond: bonding struct to work on
1803 * @tick_resolution: tick duration (millisecond resolution)
1805 * Can be called only after the mac address of the bond is set.
1807 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
1809 /* check that the bond is not initialized yet */
1810 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1811 bond->dev->dev_addr)) {
1813 aggregator_identifier = 0;
1815 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1816 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1818 /* initialize how many times this module is called in one
1819 * second (should be about every 100ms)
1821 ad_ticks_per_sec = tick_resolution;
1823 bond_3ad_initiate_agg_selection(bond,
1824 AD_AGGREGATOR_SELECTION_TIMER *
1830 * bond_3ad_bind_slave - initialize a slave's port
1831 * @slave: slave struct to work on
1833 * Returns: 0 on success
1836 void bond_3ad_bind_slave(struct slave *slave)
1838 struct bonding *bond = bond_get_bond_by_slave(slave);
1840 struct aggregator *aggregator;
1842 /* check that the slave has not been initialized yet. */
1843 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1845 /* port initialization */
1846 port = &(SLAVE_AD_INFO(slave).port);
1848 ad_initialize_port(port, bond->params.lacp_fast);
1850 __initialize_port_locks(slave);
1851 port->slave = slave;
1852 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1853 /* key is determined according to the link speed, duplex and user key(which
1854 * is yet not supported)
1856 port->actor_admin_port_key = 0;
1857 port->actor_admin_port_key |= __get_duplex(port);
1858 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1859 port->actor_oper_port_key = port->actor_admin_port_key;
1860 /* if the port is not full duplex, then the port should be not
1863 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
1864 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1865 /* actor system is the bond's system */
1866 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1867 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
1868 * lacpdu's are sent in one second)
1870 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1871 port->aggregator = NULL;
1872 port->next_port_in_aggregator = NULL;
1874 __disable_port(port);
1876 /* aggregator initialization */
1877 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1879 ad_initialize_agg(aggregator);
1881 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1882 aggregator->aggregator_identifier = (++aggregator_identifier);
1883 aggregator->slave = slave;
1884 aggregator->is_active = 0;
1885 aggregator->num_of_ports = 0;
1890 * bond_3ad_unbind_slave - deinitialize a slave's port
1891 * @slave: slave struct to work on
1893 * Search for the aggregator that is related to this port, remove the
1894 * aggregator and assign another aggregator for other port related to it
1895 * (if any), and remove the port.
1897 void bond_3ad_unbind_slave(struct slave *slave)
1899 struct port *port, *prev_port, *temp_port;
1900 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1901 int select_new_active_agg = 0;
1902 struct bonding *bond = slave->bond;
1903 struct slave *slave_iter;
1904 struct list_head *iter;
1906 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1907 port = &(SLAVE_AD_INFO(slave).port);
1909 /* if slave is null, the whole port is not initialized */
1911 pr_warn("Warning: %s: Trying to unbind an uninitialized port on %s\n",
1912 slave->bond->dev->name, slave->dev->name);
1916 pr_debug("Unbinding Link Aggregation Group %d\n",
1917 aggregator->aggregator_identifier);
1919 /* Tell the partner that this port is not suitable for aggregation */
1920 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1921 __update_lacpdu_from_port(port);
1922 ad_lacpdu_send(port);
1924 /* check if this aggregator is occupied */
1925 if (aggregator->lag_ports) {
1926 /* check if there are other ports related to this aggregator
1927 * except the port related to this slave(thats ensure us that
1928 * there is a reason to search for new aggregator, and that we
1931 if ((aggregator->lag_ports != port) ||
1932 (aggregator->lag_ports->next_port_in_aggregator)) {
1933 /* find new aggregator for the related port(s) */
1934 bond_for_each_slave(bond, slave_iter, iter) {
1935 new_aggregator = &(SLAVE_AD_INFO(slave_iter).aggregator);
1936 /* if the new aggregator is empty, or it is
1937 * connected to our port only
1939 if (!new_aggregator->lag_ports ||
1940 ((new_aggregator->lag_ports == port) &&
1941 !new_aggregator->lag_ports->next_port_in_aggregator))
1945 new_aggregator = NULL;
1947 /* if new aggregator found, copy the aggregator's
1948 * parameters and connect the related lag_ports to the
1951 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
1952 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n",
1953 aggregator->aggregator_identifier,
1954 new_aggregator->aggregator_identifier);
1956 if ((new_aggregator->lag_ports == port) &&
1957 new_aggregator->is_active) {
1958 pr_info("%s: Removing an active aggregator\n",
1959 aggregator->slave->bond->dev->name);
1960 select_new_active_agg = 1;
1963 new_aggregator->is_individual = aggregator->is_individual;
1964 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
1965 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
1966 new_aggregator->partner_system = aggregator->partner_system;
1967 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
1968 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
1969 new_aggregator->receive_state = aggregator->receive_state;
1970 new_aggregator->transmit_state = aggregator->transmit_state;
1971 new_aggregator->lag_ports = aggregator->lag_ports;
1972 new_aggregator->is_active = aggregator->is_active;
1973 new_aggregator->num_of_ports = aggregator->num_of_ports;
1975 /* update the information that is written on
1976 * the ports about the aggregator
1978 for (temp_port = aggregator->lag_ports; temp_port;
1979 temp_port = temp_port->next_port_in_aggregator) {
1980 temp_port->aggregator = new_aggregator;
1981 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
1984 ad_clear_agg(aggregator);
1986 if (select_new_active_agg)
1987 ad_agg_selection_logic(__get_first_agg(port));
1989 pr_warn("%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n",
1990 slave->bond->dev->name);
1993 /* in case that the only port related to this
1994 * aggregator is the one we want to remove
1996 select_new_active_agg = aggregator->is_active;
1997 ad_clear_agg(aggregator);
1998 if (select_new_active_agg) {
1999 pr_info("%s: Removing an active aggregator\n",
2000 slave->bond->dev->name);
2001 /* select new active aggregator */
2002 temp_aggregator = __get_first_agg(port);
2003 if (temp_aggregator)
2004 ad_agg_selection_logic(temp_aggregator);
2009 pr_debug("Unbinding port %d\n", port->actor_port_number);
2011 /* find the aggregator that this port is connected to */
2012 bond_for_each_slave(bond, slave_iter, iter) {
2013 temp_aggregator = &(SLAVE_AD_INFO(slave_iter).aggregator);
2015 /* search the port in the aggregator's related ports */
2016 for (temp_port = temp_aggregator->lag_ports; temp_port;
2017 prev_port = temp_port,
2018 temp_port = temp_port->next_port_in_aggregator) {
2019 if (temp_port == port) {
2020 /* the aggregator found - detach the port from
2024 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2026 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2027 temp_aggregator->num_of_ports--;
2028 if (temp_aggregator->num_of_ports == 0) {
2029 select_new_active_agg = temp_aggregator->is_active;
2030 ad_clear_agg(temp_aggregator);
2031 if (select_new_active_agg) {
2032 pr_info("%s: Removing an active aggregator\n",
2033 slave->bond->dev->name);
2034 /* select new active aggregator */
2035 ad_agg_selection_logic(__get_first_agg(port));
2046 * bond_3ad_state_machine_handler - handle state machines timeout
2047 * @bond: bonding struct to work on
2049 * The state machine handling concept in this module is to check every tick
2050 * which state machine should operate any function. The execution order is
2051 * round robin, so when we have an interaction between state machines, the
2052 * reply of one to each other might be delayed until next tick.
2054 * This function also complete the initialization when the agg_select_timer
2055 * times out, and it selects an aggregator for the ports that are yet not
2056 * related to any aggregator, and selects the active aggregator for a bond.
2058 void bond_3ad_state_machine_handler(struct work_struct *work)
2060 struct bonding *bond = container_of(work, struct bonding,
2062 struct aggregator *aggregator;
2063 struct list_head *iter;
2064 struct slave *slave;
2067 read_lock(&bond->lock);
2070 /* check if there are any slaves */
2071 if (!bond_has_slaves(bond))
2074 /* check if agg_select_timer timer after initialize is timed out */
2075 if (BOND_AD_INFO(bond).agg_select_timer &&
2076 !(--BOND_AD_INFO(bond).agg_select_timer)) {
2077 slave = bond_first_slave_rcu(bond);
2078 port = slave ? &(SLAVE_AD_INFO(slave).port) : NULL;
2080 /* select the active aggregator for the bond */
2083 pr_warn("%s: Warning: bond's first port is uninitialized\n",
2088 aggregator = __get_first_agg(port);
2089 ad_agg_selection_logic(aggregator);
2091 bond_3ad_set_carrier(bond);
2094 /* for each port run the state machines */
2095 bond_for_each_slave_rcu(bond, slave, iter) {
2096 port = &(SLAVE_AD_INFO(slave).port);
2098 pr_warn("%s: Warning: Found an uninitialized port\n",
2103 /* Lock around state machines to protect data accessed
2104 * by all (e.g., port->sm_vars). ad_rx_machine may run
2105 * concurrently due to incoming LACPDU.
2107 __get_state_machine_lock(port);
2109 ad_rx_machine(NULL, port);
2110 ad_periodic_machine(port);
2111 ad_port_selection_logic(port);
2112 ad_mux_machine(port);
2113 ad_tx_machine(port);
2115 /* turn off the BEGIN bit, since we already handled it */
2116 if (port->sm_vars & AD_PORT_BEGIN)
2117 port->sm_vars &= ~AD_PORT_BEGIN;
2119 __release_state_machine_lock(port);
2124 read_unlock(&bond->lock);
2125 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2129 * bond_3ad_rx_indication - handle a received frame
2130 * @lacpdu: received lacpdu
2131 * @slave: slave struct to work on
2132 * @length: length of the data received
2134 * It is assumed that frames that were sent on this NIC don't returned as new
2135 * received frames (loopback). Since only the payload is given to this
2136 * function, it check for loopback.
2138 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2142 int ret = RX_HANDLER_ANOTHER;
2144 if (length >= sizeof(struct lacpdu)) {
2146 port = &(SLAVE_AD_INFO(slave).port);
2149 pr_warn("%s: Warning: port of slave %s is uninitialized\n",
2150 slave->dev->name, slave->bond->dev->name);
2154 switch (lacpdu->subtype) {
2155 case AD_TYPE_LACPDU:
2156 ret = RX_HANDLER_CONSUMED;
2157 pr_debug("Received LACPDU on port %d\n",
2158 port->actor_port_number);
2159 /* Protect against concurrent state machines */
2160 __get_state_machine_lock(port);
2161 ad_rx_machine(lacpdu, port);
2162 __release_state_machine_lock(port);
2165 case AD_TYPE_MARKER:
2166 ret = RX_HANDLER_CONSUMED;
2167 /* No need to convert fields to Little Endian since we
2168 * don't use the marker's fields.
2171 switch (((struct bond_marker *)lacpdu)->tlv_type) {
2172 case AD_MARKER_INFORMATION_SUBTYPE:
2173 pr_debug("Received Marker Information on port %d\n",
2174 port->actor_port_number);
2175 ad_marker_info_received((struct bond_marker *)lacpdu, port);
2178 case AD_MARKER_RESPONSE_SUBTYPE:
2179 pr_debug("Received Marker Response on port %d\n",
2180 port->actor_port_number);
2181 ad_marker_response_received((struct bond_marker *)lacpdu, port);
2185 pr_debug("Received an unknown Marker subtype on slot %d\n",
2186 port->actor_port_number);
2194 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2195 * @slave: slave struct to work on
2197 * Handle reselection of aggregator (if needed) for this port.
2199 void bond_3ad_adapter_speed_changed(struct slave *slave)
2203 port = &(SLAVE_AD_INFO(slave).port);
2205 /* if slave is null, the whole port is not initialized */
2207 pr_warn("Warning: %s: speed changed for uninitialized port on %s\n",
2208 slave->bond->dev->name, slave->dev->name);
2212 __get_state_machine_lock(port);
2214 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2215 port->actor_oper_port_key = port->actor_admin_port_key |=
2216 (__get_link_speed(port) << 1);
2217 pr_debug("Port %d changed speed\n", port->actor_port_number);
2218 /* there is no need to reselect a new aggregator, just signal the
2219 * state machines to reinitialize
2221 port->sm_vars |= AD_PORT_BEGIN;
2223 __release_state_machine_lock(port);
2227 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2228 * @slave: slave struct to work on
2230 * Handle reselection of aggregator (if needed) for this port.
2232 void bond_3ad_adapter_duplex_changed(struct slave *slave)
2236 port = &(SLAVE_AD_INFO(slave).port);
2238 /* if slave is null, the whole port is not initialized */
2240 pr_warn("%s: Warning: duplex changed for uninitialized port on %s\n",
2241 slave->bond->dev->name, slave->dev->name);
2245 __get_state_machine_lock(port);
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
2254 port->sm_vars |= AD_PORT_BEGIN;
2256 __release_state_machine_lock(port);
2260 * bond_3ad_handle_link_change - handle a slave's link status change indication
2261 * @slave: slave struct to work on
2262 * @status: whether the link is now up or down
2264 * Handle reselection of aggregator (if needed) for this port.
2266 void bond_3ad_handle_link_change(struct slave *slave, char link)
2270 port = &(SLAVE_AD_INFO(slave).port);
2272 /* if slave is null, the whole port is not initialized */
2274 pr_warn("Warning: %s: link status changed for uninitialized port on %s\n",
2275 slave->bond->dev->name, slave->dev->name);
2279 __get_state_machine_lock(port);
2280 /* on link down we are zeroing duplex and speed since
2281 * some of the adaptors(ce1000.lan) report full duplex/speed
2282 * instead of N/A(duplex) / 0(speed).
2284 * on link up we are forcing recheck on the duplex and speed since
2285 * some of he adaptors(ce1000.lan) report.
2287 if (link == BOND_LINK_UP) {
2288 port->is_enabled = true;
2289 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2290 port->actor_oper_port_key = port->actor_admin_port_key |=
2292 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2293 port->actor_oper_port_key = port->actor_admin_port_key |=
2294 (__get_link_speed(port) << 1);
2296 /* link has failed */
2297 port->is_enabled = false;
2298 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2299 port->actor_oper_port_key = (port->actor_admin_port_key &=
2300 ~AD_SPEED_KEY_BITS);
2302 pr_debug("Port %d changed link status to %s",
2303 port->actor_port_number,
2304 (link == BOND_LINK_UP) ? "UP" : "DOWN");
2305 /* there is no need to reselect a new aggregator, just signal the
2306 * state machines to reinitialize
2308 port->sm_vars |= AD_PORT_BEGIN;
2310 __release_state_machine_lock(port);
2314 * bond_3ad_set_carrier - set link state for bonding master
2315 * @bond - bonding structure
2317 * if we have an active aggregator, we're up, if not, we're down.
2318 * Presumes that we cannot have an active aggregator if there are
2319 * no slaves with link up.
2321 * This behavior complies with IEEE 802.3 section 43.3.9.
2323 * Called by bond_set_carrier(). Return zero if carrier state does not
2324 * change, nonzero if it does.
2326 int bond_3ad_set_carrier(struct bonding *bond)
2328 struct aggregator *active;
2329 struct slave *first_slave;
2332 first_slave = bond_first_slave_rcu(bond);
2336 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave).aggregator));
2338 /* are enough slaves available to consider link up? */
2339 if (active->num_of_ports < bond->params.min_links) {
2340 if (netif_carrier_ok(bond->dev)) {
2341 netif_carrier_off(bond->dev);
2344 } else if (!netif_carrier_ok(bond->dev)) {
2345 netif_carrier_on(bond->dev);
2351 if (netif_carrier_ok(bond->dev)) {
2352 netif_carrier_off(bond->dev);
2359 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2360 * @bond: bonding struct to work on
2361 * @ad_info: ad_info struct to fill with the bond's info
2363 * Returns: 0 on success
2366 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2367 struct ad_info *ad_info)
2369 struct aggregator *aggregator = NULL;
2370 struct list_head *iter;
2371 struct slave *slave;
2374 bond_for_each_slave_rcu(bond, slave, iter) {
2375 port = &(SLAVE_AD_INFO(slave).port);
2376 if (port->aggregator && port->aggregator->is_active) {
2377 aggregator = port->aggregator;
2383 ad_info->aggregator_id = aggregator->aggregator_identifier;
2384 ad_info->ports = aggregator->num_of_ports;
2385 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2386 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2387 memcpy(ad_info->partner_system,
2388 aggregator->partner_system.mac_addr_value, ETH_ALEN);
2395 /* Wrapper used to hold bond->lock so no slave manipulation can occur */
2396 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2401 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2407 int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2409 struct bonding *bond = netdev_priv(dev);
2410 struct slave *slave, *first_ok_slave;
2411 struct aggregator *agg;
2412 struct ad_info ad_info;
2413 struct list_head *iter;
2418 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
2419 pr_debug("%s: Error: __bond_3ad_get_active_agg_info failed\n",
2424 slaves_in_agg = ad_info.ports;
2425 agg_id = ad_info.aggregator_id;
2427 if (slaves_in_agg == 0) {
2428 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
2432 slave_agg_no = bond_xmit_hash(bond, skb, slaves_in_agg);
2433 first_ok_slave = NULL;
2435 bond_for_each_slave_rcu(bond, slave, iter) {
2436 agg = SLAVE_AD_INFO(slave).port.aggregator;
2437 if (!agg || agg->aggregator_identifier != agg_id)
2440 if (slave_agg_no >= 0) {
2441 if (!first_ok_slave && SLAVE_IS_OK(slave))
2442 first_ok_slave = slave;
2447 if (SLAVE_IS_OK(slave)) {
2448 bond_dev_queue_xmit(bond, skb, slave->dev);
2453 if (slave_agg_no >= 0) {
2454 pr_err("%s: Error: Couldn't find a slave to tx on for aggregator ID %d\n",
2459 /* we couldn't find any suitable slave after the agg_no, so use the
2460 * first suitable found, if found.
2463 bond_dev_queue_xmit(bond, skb, first_ok_slave->dev);
2468 return NETDEV_TX_OK;
2470 /* no suitable interface, frame not sent */
2475 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2476 struct slave *slave)
2478 int ret = RX_HANDLER_ANOTHER;
2479 struct lacpdu *lacpdu, _lacpdu;
2481 if (skb->protocol != PKT_TYPE_LACPDU)
2484 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2488 read_lock(&bond->lock);
2489 ret = bond_3ad_rx_indication(lacpdu, slave, skb->len);
2490 read_unlock(&bond->lock);
2495 * bond_3ad_update_lacp_rate - change the lacp rate
2496 * @bond - bonding struct
2498 * When modify lacp_rate parameter via sysfs,
2499 * update actor_oper_port_state of each port.
2501 * Hold slave->state_machine_lock,
2502 * so we can modify port->actor_oper_port_state,
2503 * no matter bond is up or down.
2505 void bond_3ad_update_lacp_rate(struct bonding *bond)
2507 struct port *port = NULL;
2508 struct list_head *iter;
2509 struct slave *slave;
2512 lacp_fast = bond->params.lacp_fast;
2513 bond_for_each_slave(bond, slave, iter) {
2514 port = &(SLAVE_AD_INFO(slave).port);
2515 __get_state_machine_lock(port);
2517 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2519 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
2520 __release_state_machine_lock(port);