1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
6 #include <linux/skbuff.h>
7 #include <linux/if_ether.h>
8 #include <linux/netdevice.h>
9 #include <linux/spinlock.h>
10 #include <linux/ethtool.h>
11 #include <linux/etherdevice.h>
12 #include <linux/if_bonding.h>
13 #include <linux/pkt_sched.h>
14 #include <net/net_namespace.h>
15 #include <net/bonding.h>
16 #include <net/bond_3ad.h>
17 #include <net/netlink.h>
19 /* General definitions */
20 #define AD_SHORT_TIMEOUT 1
21 #define AD_LONG_TIMEOUT 0
22 #define AD_STANDBY 0x2
23 #define AD_MAX_TX_IN_SECOND 3
24 #define AD_COLLECTOR_MAX_DELAY 0
26 /* Timer definitions (43.4.4 in the 802.3ad standard) */
27 #define AD_FAST_PERIODIC_TIME 1
28 #define AD_SLOW_PERIODIC_TIME 30
29 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
30 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
31 #define AD_CHURN_DETECTION_TIME 60
32 #define AD_AGGREGATE_WAIT_TIME 2
34 /* Port Variables definitions used by the State Machines (43.4.7 in the
37 #define AD_PORT_BEGIN 0x1
38 #define AD_PORT_LACP_ENABLED 0x2
39 #define AD_PORT_ACTOR_CHURN 0x4
40 #define AD_PORT_PARTNER_CHURN 0x8
41 #define AD_PORT_READY 0x10
42 #define AD_PORT_READY_N 0x20
43 #define AD_PORT_MATCHED 0x40
44 #define AD_PORT_STANDBY 0x80
45 #define AD_PORT_SELECTED 0x100
46 #define AD_PORT_MOVED 0x200
47 #define AD_PORT_CHURNED (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
49 /* Port Key definitions
50 * key is determined according to the link speed, duplex and
51 * user key (which is yet not supported)
52 * --------------------------------------------------------------
53 * Port key | User key (10 bits) | Speed (5 bits) | Duplex|
54 * --------------------------------------------------------------
57 #define AD_DUPLEX_KEY_MASKS 0x1
58 #define AD_SPEED_KEY_MASKS 0x3E
59 #define AD_USER_KEY_MASKS 0xFFC0
61 enum ad_link_speed_type {
62 AD_LINK_SPEED_1MBPS = 1,
64 AD_LINK_SPEED_100MBPS,
65 AD_LINK_SPEED_1000MBPS,
66 AD_LINK_SPEED_2500MBPS,
67 AD_LINK_SPEED_5000MBPS,
68 AD_LINK_SPEED_10000MBPS,
69 AD_LINK_SPEED_14000MBPS,
70 AD_LINK_SPEED_20000MBPS,
71 AD_LINK_SPEED_25000MBPS,
72 AD_LINK_SPEED_40000MBPS,
73 AD_LINK_SPEED_50000MBPS,
74 AD_LINK_SPEED_56000MBPS,
75 AD_LINK_SPEED_100000MBPS,
78 /* compare MAC addresses */
79 #define MAC_ADDRESS_EQUAL(A, B) \
80 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
82 static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
85 static u16 ad_ticks_per_sec;
86 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
88 const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned = {
89 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02
92 /* ================= main 802.3ad protocol functions ================== */
93 static int ad_lacpdu_send(struct port *port);
94 static int ad_marker_send(struct port *port, struct bond_marker *marker);
95 static void ad_mux_machine(struct port *port, bool *update_slave_arr);
96 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
97 static void ad_tx_machine(struct port *port);
98 static void ad_periodic_machine(struct port *port);
99 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
100 static void ad_agg_selection_logic(struct aggregator *aggregator,
101 bool *update_slave_arr);
102 static void ad_clear_agg(struct aggregator *aggregator);
103 static void ad_initialize_agg(struct aggregator *aggregator);
104 static void ad_initialize_port(struct port *port, int lacp_fast);
105 static void ad_enable_collecting_distributing(struct port *port,
106 bool *update_slave_arr);
107 static void ad_disable_collecting_distributing(struct port *port,
108 bool *update_slave_arr);
109 static void ad_marker_info_received(struct bond_marker *marker_info,
111 static void ad_marker_response_received(struct bond_marker *marker,
113 static void ad_update_actor_keys(struct port *port, bool reset);
116 /* ================= api to bonding and kernel code ================== */
119 * __get_bond_by_port - get the port's bonding struct
120 * @port: the port we're looking at
122 * Return @port's bonding struct, or %NULL if it can't be found.
124 static inline struct bonding *__get_bond_by_port(struct port *port)
126 if (port->slave == NULL)
129 return bond_get_bond_by_slave(port->slave);
133 * __get_first_agg - get the first aggregator in the bond
134 * @port: the port we're looking at
136 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
138 * The caller must hold RCU or RTNL lock.
140 static inline struct aggregator *__get_first_agg(struct port *port)
142 struct bonding *bond = __get_bond_by_port(port);
143 struct slave *first_slave;
144 struct aggregator *agg;
146 /* If there's no bond for this port, or bond has no slaves */
151 first_slave = bond_first_slave_rcu(bond);
152 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
159 * __agg_has_partner - see if we have a partner
160 * @agg: the agregator we're looking at
162 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
163 * address for the partner). Return 0 if not.
165 static inline int __agg_has_partner(struct aggregator *agg)
167 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
171 * __disable_port - disable the port's slave
172 * @port: the port we're looking at
174 static inline void __disable_port(struct port *port)
176 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
180 * __enable_port - enable the port's slave, if it's up
181 * @port: the port we're looking at
183 static inline void __enable_port(struct port *port)
185 struct slave *slave = port->slave;
187 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
188 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
192 * __port_is_enabled - check if the port's slave is in active state
193 * @port: the port we're looking at
195 static inline int __port_is_enabled(struct port *port)
197 return bond_is_active_slave(port->slave);
201 * __get_agg_selection_mode - get the aggregator selection mode
202 * @port: the port we're looking at
204 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
206 static inline u32 __get_agg_selection_mode(struct port *port)
208 struct bonding *bond = __get_bond_by_port(port);
211 return BOND_AD_STABLE;
213 return bond->params.ad_select;
217 * __check_agg_selection_timer - check if the selection timer has expired
218 * @port: the port we're looking at
220 static inline int __check_agg_selection_timer(struct port *port)
222 struct bonding *bond = __get_bond_by_port(port);
227 return atomic_read(&BOND_AD_INFO(bond).agg_select_timer) ? 1 : 0;
231 * __get_link_speed - get a port's speed
232 * @port: the port we're looking at
234 * Return @port's speed in 802.3ad enum format. i.e. one of:
236 * %AD_LINK_SPEED_10MBPS,
237 * %AD_LINK_SPEED_100MBPS,
238 * %AD_LINK_SPEED_1000MBPS,
239 * %AD_LINK_SPEED_2500MBPS,
240 * %AD_LINK_SPEED_5000MBPS,
241 * %AD_LINK_SPEED_10000MBPS
242 * %AD_LINK_SPEED_14000MBPS,
243 * %AD_LINK_SPEED_20000MBPS
244 * %AD_LINK_SPEED_25000MBPS
245 * %AD_LINK_SPEED_40000MBPS
246 * %AD_LINK_SPEED_50000MBPS
247 * %AD_LINK_SPEED_56000MBPS
248 * %AD_LINK_SPEED_100000MBPS
250 static u16 __get_link_speed(struct port *port)
252 struct slave *slave = port->slave;
255 /* this if covers only a special case: when the configuration starts
256 * with link down, it sets the speed to 0.
257 * This is done in spite of the fact that the e100 driver reports 0
258 * to be compatible with MVT in the future.
260 if (slave->link != BOND_LINK_UP)
263 switch (slave->speed) {
265 speed = AD_LINK_SPEED_10MBPS;
269 speed = AD_LINK_SPEED_100MBPS;
273 speed = AD_LINK_SPEED_1000MBPS;
277 speed = AD_LINK_SPEED_2500MBPS;
281 speed = AD_LINK_SPEED_5000MBPS;
285 speed = AD_LINK_SPEED_10000MBPS;
289 speed = AD_LINK_SPEED_14000MBPS;
293 speed = AD_LINK_SPEED_20000MBPS;
297 speed = AD_LINK_SPEED_25000MBPS;
301 speed = AD_LINK_SPEED_40000MBPS;
305 speed = AD_LINK_SPEED_50000MBPS;
309 speed = AD_LINK_SPEED_56000MBPS;
313 speed = AD_LINK_SPEED_100000MBPS;
317 /* unknown speed value from ethtool. shouldn't happen */
318 if (slave->speed != SPEED_UNKNOWN)
319 pr_warn_once("%s: (slave %s): unknown ethtool speed (%d) for port %d (set it to 0)\n",
320 slave->bond->dev->name,
321 slave->dev->name, slave->speed,
322 port->actor_port_number);
328 slave_dbg(slave->bond->dev, slave->dev, "Port %d Received link speed %d update from adapter\n",
329 port->actor_port_number, speed);
334 * __get_duplex - get a port's duplex
335 * @port: the port we're looking at
337 * Return @port's duplex in 802.3ad bitmask format. i.e.:
338 * 0x01 if in full duplex
341 static u8 __get_duplex(struct port *port)
343 struct slave *slave = port->slave;
346 /* handling a special case: when the configuration starts with
347 * link down, it sets the duplex to 0.
349 if (slave->link == BOND_LINK_UP) {
350 switch (slave->duplex) {
353 slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status full duplex update from adapter\n",
354 port->actor_port_number);
359 slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status NOT full duplex update from adapter\n",
360 port->actor_port_number);
367 static void __ad_actor_update_port(struct port *port)
369 const struct bonding *bond = bond_get_bond_by_slave(port->slave);
371 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
372 port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority;
378 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
379 * @timer_type: which timer to operate
380 * @par: timer parameter. see below
382 * If @timer_type is %current_while_timer, @par indicates long/short timer.
383 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
384 * %SLOW_PERIODIC_TIME.
386 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
388 u16 retval = 0; /* to silence the compiler */
390 switch (timer_type) {
391 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
393 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
395 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
397 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
398 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
400 case AD_PERIODIC_TIMER: /* for periodic machine */
401 retval = (par*ad_ticks_per_sec); /* long timeout */
403 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
404 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
406 case AD_WAIT_WHILE_TIMER: /* for selection machine */
407 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
415 /* ================= ad_rx_machine helper functions ================== */
418 * __choose_matched - update a port's matched variable from a received lacpdu
419 * @lacpdu: the lacpdu we've received
420 * @port: the port we're looking at
422 * Update the value of the matched variable, using parameter values from a
423 * newly received lacpdu. Parameter values for the partner carried in the
424 * received PDU are compared with the corresponding operational parameter
425 * values for the actor. Matched is set to TRUE if all of these parameters
426 * match and the PDU parameter partner_state.aggregation has the same value as
427 * actor_oper_port_state.aggregation and lacp will actively maintain the link
428 * in the aggregation. Matched is also set to TRUE if the value of
429 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
430 * an individual link and lacp will actively maintain the link. Otherwise,
431 * matched is set to FALSE. LACP is considered to be actively maintaining the
432 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
433 * the actor's actor_oper_port_state.lacp_activity and the PDU's
434 * partner_state.lacp_activity variables are TRUE.
436 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
437 * used here to implement the language from 802.3ad 43.4.9 that requires
438 * recordPDU to "match" the LACPDU parameters to the stored values.
440 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
442 /* check if all parameters are alike
443 * or this is individual link(aggregation == FALSE)
444 * then update the state machine Matched variable.
446 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
447 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
448 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
449 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
450 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
451 ((lacpdu->partner_state & LACP_STATE_AGGREGATION) == (port->actor_oper_port_state & LACP_STATE_AGGREGATION))) ||
452 ((lacpdu->actor_state & LACP_STATE_AGGREGATION) == 0)
454 port->sm_vars |= AD_PORT_MATCHED;
456 port->sm_vars &= ~AD_PORT_MATCHED;
461 * __record_pdu - record parameters from a received lacpdu
462 * @lacpdu: the lacpdu we've received
463 * @port: the port we're looking at
465 * Record the parameter values for the Actor carried in a received lacpdu as
466 * the current partner operational parameter values and sets
467 * actor_oper_port_state.defaulted to FALSE.
469 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
471 if (lacpdu && port) {
472 struct port_params *partner = &port->partner_oper;
474 __choose_matched(lacpdu, port);
475 /* record the new parameter values for the partner
478 partner->port_number = ntohs(lacpdu->actor_port);
479 partner->port_priority = ntohs(lacpdu->actor_port_priority);
480 partner->system = lacpdu->actor_system;
481 partner->system_priority = ntohs(lacpdu->actor_system_priority);
482 partner->key = ntohs(lacpdu->actor_key);
483 partner->port_state = lacpdu->actor_state;
485 /* set actor_oper_port_state.defaulted to FALSE */
486 port->actor_oper_port_state &= ~LACP_STATE_DEFAULTED;
488 /* set the partner sync. to on if the partner is sync,
489 * and the port is matched
491 if ((port->sm_vars & AD_PORT_MATCHED) &&
492 (lacpdu->actor_state & LACP_STATE_SYNCHRONIZATION)) {
493 partner->port_state |= LACP_STATE_SYNCHRONIZATION;
494 slave_dbg(port->slave->bond->dev, port->slave->dev,
497 partner->port_state &= ~LACP_STATE_SYNCHRONIZATION;
498 slave_dbg(port->slave->bond->dev, port->slave->dev,
505 * __record_default - record default parameters
506 * @port: the port we're looking at
508 * This function records the default parameter values for the partner carried
509 * in the Partner Admin parameters as the current partner operational parameter
510 * values and sets actor_oper_port_state.defaulted to TRUE.
512 static void __record_default(struct port *port)
515 /* record the partner admin parameters */
516 memcpy(&port->partner_oper, &port->partner_admin,
517 sizeof(struct port_params));
519 /* set actor_oper_port_state.defaulted to true */
520 port->actor_oper_port_state |= LACP_STATE_DEFAULTED;
525 * __update_selected - update a port's Selected variable from a received lacpdu
526 * @lacpdu: the lacpdu we've received
527 * @port: the port we're looking at
529 * Update the value of the selected variable, using parameter values from a
530 * newly received lacpdu. The parameter values for the Actor carried in the
531 * received PDU are compared with the corresponding operational parameter
532 * values for the ports partner. If one or more of the comparisons shows that
533 * the value(s) received in the PDU differ from the current operational values,
534 * then selected is set to FALSE and actor_oper_port_state.synchronization is
535 * set to out_of_sync. Otherwise, selected remains unchanged.
537 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
539 if (lacpdu && port) {
540 const struct port_params *partner = &port->partner_oper;
542 /* check if any parameter is different then
543 * update the state machine selected variable.
545 if (ntohs(lacpdu->actor_port) != partner->port_number ||
546 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
547 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
548 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
549 ntohs(lacpdu->actor_key) != partner->key ||
550 (lacpdu->actor_state & LACP_STATE_AGGREGATION) != (partner->port_state & LACP_STATE_AGGREGATION)) {
551 port->sm_vars &= ~AD_PORT_SELECTED;
557 * __update_default_selected - update a port's Selected variable from Partner
558 * @port: the port we're looking at
560 * This function updates the value of the selected variable, using the partner
561 * administrative parameter values. The administrative values are compared with
562 * the corresponding operational parameter values for the partner. If one or
563 * more of the comparisons shows that the administrative value(s) differ from
564 * the current operational values, then Selected is set to FALSE and
565 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
566 * Selected remains unchanged.
568 static void __update_default_selected(struct port *port)
571 const struct port_params *admin = &port->partner_admin;
572 const struct port_params *oper = &port->partner_oper;
574 /* check if any parameter is different then
575 * update the state machine selected variable.
577 if (admin->port_number != oper->port_number ||
578 admin->port_priority != oper->port_priority ||
579 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
580 admin->system_priority != oper->system_priority ||
581 admin->key != oper->key ||
582 (admin->port_state & LACP_STATE_AGGREGATION)
583 != (oper->port_state & LACP_STATE_AGGREGATION)) {
584 port->sm_vars &= ~AD_PORT_SELECTED;
590 * __update_ntt - update a port's ntt variable from a received lacpdu
591 * @lacpdu: the lacpdu we've received
592 * @port: the port we're looking at
594 * Updates the value of the ntt variable, using parameter values from a newly
595 * received lacpdu. The parameter values for the partner carried in the
596 * received PDU are compared with the corresponding operational parameter
597 * values for the Actor. If one or more of the comparisons shows that the
598 * value(s) received in the PDU differ from the current operational values,
599 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
601 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
603 /* validate lacpdu and port */
604 if (lacpdu && port) {
605 /* check if any parameter is different then
606 * update the port->ntt.
608 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
609 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
610 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
611 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
612 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
613 ((lacpdu->partner_state & LACP_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY)) ||
614 ((lacpdu->partner_state & LACP_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT)) ||
615 ((lacpdu->partner_state & LACP_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) ||
616 ((lacpdu->partner_state & LACP_STATE_AGGREGATION) != (port->actor_oper_port_state & LACP_STATE_AGGREGATION))
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
637 for (port = aggregator->lag_ports;
639 port = port->next_port_in_aggregator) {
640 if (!(port->sm_vars & AD_PORT_READY_N)) {
651 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
652 * @aggregator: the aggregator we're looking at
653 * @val: Should the ports' ready bit be set on or off
656 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
660 for (port = aggregator->lag_ports; port;
661 port = port->next_port_in_aggregator) {
663 port->sm_vars |= AD_PORT_READY;
665 port->sm_vars &= ~AD_PORT_READY;
669 static int __agg_active_ports(struct aggregator *agg)
674 for (port = agg->lag_ports; port;
675 port = port->next_port_in_aggregator) {
676 if (port->is_enabled)
684 * __get_agg_bandwidth - get the total bandwidth of an aggregator
685 * @aggregator: the aggregator we're looking at
688 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
690 int nports = __agg_active_ports(aggregator);
694 switch (__get_link_speed(aggregator->lag_ports)) {
695 case AD_LINK_SPEED_1MBPS:
698 case AD_LINK_SPEED_10MBPS:
699 bandwidth = nports * 10;
701 case AD_LINK_SPEED_100MBPS:
702 bandwidth = nports * 100;
704 case AD_LINK_SPEED_1000MBPS:
705 bandwidth = nports * 1000;
707 case AD_LINK_SPEED_2500MBPS:
708 bandwidth = nports * 2500;
710 case AD_LINK_SPEED_5000MBPS:
711 bandwidth = nports * 5000;
713 case AD_LINK_SPEED_10000MBPS:
714 bandwidth = nports * 10000;
716 case AD_LINK_SPEED_14000MBPS:
717 bandwidth = nports * 14000;
719 case AD_LINK_SPEED_20000MBPS:
720 bandwidth = nports * 20000;
722 case AD_LINK_SPEED_25000MBPS:
723 bandwidth = nports * 25000;
725 case AD_LINK_SPEED_40000MBPS:
726 bandwidth = nports * 40000;
728 case AD_LINK_SPEED_50000MBPS:
729 bandwidth = nports * 50000;
731 case AD_LINK_SPEED_56000MBPS:
732 bandwidth = nports * 56000;
734 case AD_LINK_SPEED_100000MBPS:
735 bandwidth = nports * 100000;
738 bandwidth = 0; /* to silence the compiler */
745 * __get_active_agg - get the current active aggregator
746 * @aggregator: the aggregator we're looking at
748 * Caller must hold RCU lock.
750 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
752 struct bonding *bond = aggregator->slave->bond;
753 struct list_head *iter;
756 bond_for_each_slave_rcu(bond, slave, iter)
757 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
758 return &(SLAVE_AD_INFO(slave)->aggregator);
764 * __update_lacpdu_from_port - update a port's lacpdu fields
765 * @port: the port we're looking at
767 static inline void __update_lacpdu_from_port(struct port *port)
769 struct lacpdu *lacpdu = &port->lacpdu;
770 const struct port_params *partner = &port->partner_oper;
772 /* update current actual Actor parameters
773 * lacpdu->subtype initialized
774 * lacpdu->version_number initialized
775 * lacpdu->tlv_type_actor_info initialized
776 * lacpdu->actor_information_length initialized
779 lacpdu->actor_system_priority = htons(port->actor_system_priority);
780 lacpdu->actor_system = port->actor_system;
781 lacpdu->actor_key = htons(port->actor_oper_port_key);
782 lacpdu->actor_port_priority = htons(port->actor_port_priority);
783 lacpdu->actor_port = htons(port->actor_port_number);
784 lacpdu->actor_state = port->actor_oper_port_state;
785 slave_dbg(port->slave->bond->dev, port->slave->dev,
786 "update lacpdu: actor port state %x\n",
787 port->actor_oper_port_state);
789 /* lacpdu->reserved_3_1 initialized
790 * lacpdu->tlv_type_partner_info initialized
791 * lacpdu->partner_information_length initialized
794 lacpdu->partner_system_priority = htons(partner->system_priority);
795 lacpdu->partner_system = partner->system;
796 lacpdu->partner_key = htons(partner->key);
797 lacpdu->partner_port_priority = htons(partner->port_priority);
798 lacpdu->partner_port = htons(partner->port_number);
799 lacpdu->partner_state = partner->port_state;
801 /* lacpdu->reserved_3_2 initialized
802 * lacpdu->tlv_type_collector_info initialized
803 * lacpdu->collector_information_length initialized
804 * collector_max_delay initialized
805 * reserved_12[12] initialized
806 * tlv_type_terminator initialized
807 * terminator_length initialized
808 * reserved_50[50] initialized
812 /* ================= main 802.3ad protocol code ========================= */
815 * ad_lacpdu_send - send out a lacpdu packet on a given port
816 * @port: the port we're looking at
818 * Returns: 0 on success
821 static int ad_lacpdu_send(struct port *port)
823 struct slave *slave = port->slave;
825 struct lacpdu_header *lacpdu_header;
826 int length = sizeof(struct lacpdu_header);
828 skb = dev_alloc_skb(length);
832 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_tx);
833 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.lacpdu_tx);
835 skb->dev = slave->dev;
836 skb_reset_mac_header(skb);
837 skb->network_header = skb->mac_header + ETH_HLEN;
838 skb->protocol = PKT_TYPE_LACPDU;
839 skb->priority = TC_PRIO_CONTROL;
841 lacpdu_header = skb_put(skb, length);
843 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
844 /* Note: source address is set to be the member's PERMANENT address,
845 * because we use it to identify loopback lacpdus in receive.
847 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
848 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
850 lacpdu_header->lacpdu = port->lacpdu;
858 * ad_marker_send - send marker information/response on a given port
859 * @port: the port we're looking at
860 * @marker: marker data to send
862 * Returns: 0 on success
865 static int ad_marker_send(struct port *port, struct bond_marker *marker)
867 struct slave *slave = port->slave;
869 struct bond_marker_header *marker_header;
870 int length = sizeof(struct bond_marker_header);
872 skb = dev_alloc_skb(length + 16);
876 switch (marker->tlv_type) {
877 case AD_MARKER_INFORMATION_SUBTYPE:
878 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_tx);
879 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_tx);
881 case AD_MARKER_RESPONSE_SUBTYPE:
882 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_resp_tx);
883 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_resp_tx);
887 skb_reserve(skb, 16);
889 skb->dev = slave->dev;
890 skb_reset_mac_header(skb);
891 skb->network_header = skb->mac_header + ETH_HLEN;
892 skb->protocol = PKT_TYPE_LACPDU;
894 marker_header = skb_put(skb, length);
896 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
897 /* Note: source address is set to be the member's PERMANENT address,
898 * because we use it to identify loopback MARKERs in receive.
900 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
901 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
903 marker_header->marker = *marker;
911 * ad_mux_machine - handle a port's mux state machine
912 * @port: the port we're looking at
913 * @update_slave_arr: Does slave array need update?
915 static void ad_mux_machine(struct port *port, bool *update_slave_arr)
917 mux_states_t last_state;
919 /* keep current State Machine state to compare later if it was
922 last_state = port->sm_mux_state;
924 if (port->sm_vars & AD_PORT_BEGIN) {
925 port->sm_mux_state = AD_MUX_DETACHED;
927 switch (port->sm_mux_state) {
928 case AD_MUX_DETACHED:
929 if ((port->sm_vars & AD_PORT_SELECTED)
930 || (port->sm_vars & AD_PORT_STANDBY))
931 /* if SELECTED or STANDBY */
932 port->sm_mux_state = AD_MUX_WAITING;
935 /* if SELECTED == FALSE return to DETACH state */
936 if (!(port->sm_vars & AD_PORT_SELECTED)) {
937 port->sm_vars &= ~AD_PORT_READY_N;
938 /* in order to withhold the Selection Logic to
939 * check all ports READY_N value every callback
940 * cycle to update ready variable, we check
941 * READY_N and update READY here
943 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
944 port->sm_mux_state = AD_MUX_DETACHED;
948 /* check if the wait_while_timer expired */
949 if (port->sm_mux_timer_counter
950 && !(--port->sm_mux_timer_counter))
951 port->sm_vars |= AD_PORT_READY_N;
953 /* in order to withhold the selection logic to check
954 * all ports READY_N value every callback cycle to
955 * update ready variable, we check READY_N and update
958 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
960 /* if the wait_while_timer expired, and the port is
961 * in READY state, move to ATTACHED state
963 if ((port->sm_vars & AD_PORT_READY)
964 && !port->sm_mux_timer_counter)
965 port->sm_mux_state = AD_MUX_ATTACHED;
967 case AD_MUX_ATTACHED:
968 /* check also if agg_select_timer expired (so the
969 * edable port will take place only after this timer)
971 if ((port->sm_vars & AD_PORT_SELECTED) &&
972 (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) &&
973 !__check_agg_selection_timer(port)) {
974 if (port->aggregator->is_active)
976 AD_MUX_COLLECTING_DISTRIBUTING;
977 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
978 (port->sm_vars & AD_PORT_STANDBY)) {
979 /* if UNSELECTED or STANDBY */
980 port->sm_vars &= ~AD_PORT_READY_N;
981 /* in order to withhold the selection logic to
982 * check all ports READY_N value every callback
983 * cycle to update ready variable, we check
984 * READY_N and update READY here
986 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
987 port->sm_mux_state = AD_MUX_DETACHED;
988 } else if (port->aggregator->is_active) {
989 port->actor_oper_port_state |=
990 LACP_STATE_SYNCHRONIZATION;
993 case AD_MUX_COLLECTING_DISTRIBUTING:
994 if (!(port->sm_vars & AD_PORT_SELECTED) ||
995 (port->sm_vars & AD_PORT_STANDBY) ||
996 !(port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) ||
997 !(port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) {
998 port->sm_mux_state = AD_MUX_ATTACHED;
1000 /* if port state hasn't changed make
1001 * sure that a collecting distributing
1002 * port in an active aggregator is enabled
1004 if (port->aggregator &&
1005 port->aggregator->is_active &&
1006 !__port_is_enabled(port)) {
1007 __enable_port(port);
1008 *update_slave_arr = true;
1017 /* check if the state machine was changed */
1018 if (port->sm_mux_state != last_state) {
1019 slave_dbg(port->slave->bond->dev, port->slave->dev,
1020 "Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
1021 port->actor_port_number,
1023 port->sm_mux_state);
1024 switch (port->sm_mux_state) {
1025 case AD_MUX_DETACHED:
1026 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1027 ad_disable_collecting_distributing(port,
1029 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
1030 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
1033 case AD_MUX_WAITING:
1034 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1036 case AD_MUX_ATTACHED:
1037 if (port->aggregator->is_active)
1038 port->actor_oper_port_state |=
1039 LACP_STATE_SYNCHRONIZATION;
1041 port->actor_oper_port_state &=
1042 ~LACP_STATE_SYNCHRONIZATION;
1043 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
1044 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
1045 ad_disable_collecting_distributing(port,
1049 case AD_MUX_COLLECTING_DISTRIBUTING:
1050 port->actor_oper_port_state |= LACP_STATE_COLLECTING;
1051 port->actor_oper_port_state |= LACP_STATE_DISTRIBUTING;
1052 port->actor_oper_port_state |= LACP_STATE_SYNCHRONIZATION;
1053 ad_enable_collecting_distributing(port,
1064 * ad_rx_machine - handle a port's rx State Machine
1065 * @lacpdu: the lacpdu we've received
1066 * @port: the port we're looking at
1068 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1069 * CURRENT. If timer expired set the state machine in the proper state.
1070 * In other cases, this function checks if we need to switch to other state.
1072 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1074 rx_states_t last_state;
1076 /* keep current State Machine state to compare later if it was
1079 last_state = port->sm_rx_state;
1082 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx);
1083 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.lacpdu_rx);
1085 /* check if state machine should change state */
1087 /* first, check if port was reinitialized */
1088 if (port->sm_vars & AD_PORT_BEGIN) {
1089 port->sm_rx_state = AD_RX_INITIALIZE;
1090 port->sm_vars |= AD_PORT_CHURNED;
1091 /* check if port is not enabled */
1092 } else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled)
1093 port->sm_rx_state = AD_RX_PORT_DISABLED;
1094 /* check if new lacpdu arrived */
1095 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1096 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1097 (port->sm_rx_state == AD_RX_CURRENT))) {
1098 if (port->sm_rx_state != AD_RX_CURRENT)
1099 port->sm_vars |= AD_PORT_CHURNED;
1100 port->sm_rx_timer_counter = 0;
1101 port->sm_rx_state = AD_RX_CURRENT;
1103 /* if timer is on, and if it is expired */
1104 if (port->sm_rx_timer_counter &&
1105 !(--port->sm_rx_timer_counter)) {
1106 switch (port->sm_rx_state) {
1108 port->sm_rx_state = AD_RX_DEFAULTED;
1111 port->sm_rx_state = AD_RX_EXPIRED;
1117 /* if no lacpdu arrived and no timer is on */
1118 switch (port->sm_rx_state) {
1119 case AD_RX_PORT_DISABLED:
1120 if (port->is_enabled &&
1121 (port->sm_vars & AD_PORT_LACP_ENABLED))
1122 port->sm_rx_state = AD_RX_EXPIRED;
1123 else if (port->is_enabled
1125 & AD_PORT_LACP_ENABLED) == 0))
1126 port->sm_rx_state = AD_RX_LACP_DISABLED;
1135 /* check if the State machine was changed or new lacpdu arrived */
1136 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1137 slave_dbg(port->slave->bond->dev, port->slave->dev,
1138 "Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1139 port->actor_port_number,
1142 switch (port->sm_rx_state) {
1143 case AD_RX_INITIALIZE:
1144 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
1145 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1147 port->sm_vars |= AD_PORT_LACP_ENABLED;
1148 port->sm_vars &= ~AD_PORT_SELECTED;
1149 __record_default(port);
1150 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1151 port->sm_rx_state = AD_RX_PORT_DISABLED;
1154 case AD_RX_PORT_DISABLED:
1155 port->sm_vars &= ~AD_PORT_MATCHED;
1157 case AD_RX_LACP_DISABLED:
1158 port->sm_vars &= ~AD_PORT_SELECTED;
1159 __record_default(port);
1160 port->partner_oper.port_state &= ~LACP_STATE_AGGREGATION;
1161 port->sm_vars |= AD_PORT_MATCHED;
1162 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1165 /* Reset of the Synchronization flag (Standard 43.4.12)
1166 * This reset cause to disable this port in the
1167 * COLLECTING_DISTRIBUTING state of the mux machine in
1168 * case of EXPIRED even if LINK_DOWN didn't arrive for
1171 port->partner_oper.port_state &= ~LACP_STATE_SYNCHRONIZATION;
1172 port->sm_vars &= ~AD_PORT_MATCHED;
1173 port->partner_oper.port_state |= LACP_STATE_LACP_TIMEOUT;
1174 port->partner_oper.port_state |= LACP_STATE_LACP_ACTIVITY;
1175 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1176 port->actor_oper_port_state |= LACP_STATE_EXPIRED;
1177 port->sm_vars |= AD_PORT_CHURNED;
1179 case AD_RX_DEFAULTED:
1180 __update_default_selected(port);
1181 __record_default(port);
1182 port->sm_vars |= AD_PORT_MATCHED;
1183 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1186 /* detect loopback situation */
1187 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1188 &(port->actor_system))) {
1189 slave_err(port->slave->bond->dev, port->slave->dev, "An illegal loopback occurred on slave\n"
1190 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n");
1193 __update_selected(lacpdu, port);
1194 __update_ntt(lacpdu, port);
1195 __record_pdu(lacpdu, port);
1196 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT));
1197 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1206 * ad_churn_machine - handle port churn's state machine
1207 * @port: the port we're looking at
1210 static void ad_churn_machine(struct port *port)
1212 if (port->sm_vars & AD_PORT_CHURNED) {
1213 port->sm_vars &= ~AD_PORT_CHURNED;
1214 port->sm_churn_actor_state = AD_CHURN_MONITOR;
1215 port->sm_churn_partner_state = AD_CHURN_MONITOR;
1216 port->sm_churn_actor_timer_counter =
1217 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1218 port->sm_churn_partner_timer_counter =
1219 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1222 if (port->sm_churn_actor_timer_counter &&
1223 !(--port->sm_churn_actor_timer_counter) &&
1224 port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1225 if (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION) {
1226 port->sm_churn_actor_state = AD_NO_CHURN;
1228 port->churn_actor_count++;
1229 port->sm_churn_actor_state = AD_CHURN;
1232 if (port->sm_churn_partner_timer_counter &&
1233 !(--port->sm_churn_partner_timer_counter) &&
1234 port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1235 if (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) {
1236 port->sm_churn_partner_state = AD_NO_CHURN;
1238 port->churn_partner_count++;
1239 port->sm_churn_partner_state = AD_CHURN;
1245 * ad_tx_machine - handle a port's tx state machine
1246 * @port: the port we're looking at
1248 static void ad_tx_machine(struct port *port)
1250 /* check if tx timer expired, to verify that we do not send more than
1251 * 3 packets per second
1253 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1254 /* check if there is something to send */
1255 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1256 __update_lacpdu_from_port(port);
1258 if (ad_lacpdu_send(port) >= 0) {
1259 slave_dbg(port->slave->bond->dev,
1261 "Sent LACPDU on port %d\n",
1262 port->actor_port_number);
1264 /* mark ntt as false, so it will not be sent
1265 * again until demanded
1270 /* restart tx timer(to verify that we will not exceed
1271 * AD_MAX_TX_IN_SECOND
1273 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1278 * ad_periodic_machine - handle a port's periodic state machine
1279 * @port: the port we're looking at
1281 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1283 static void ad_periodic_machine(struct port *port)
1285 periodic_states_t last_state;
1287 /* keep current state machine state to compare later if it was changed */
1288 last_state = port->sm_periodic_state;
1290 /* check if port was reinitialized */
1291 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1292 (!(port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & LACP_STATE_LACP_ACTIVITY))
1294 port->sm_periodic_state = AD_NO_PERIODIC;
1296 /* check if state machine should change state */
1297 else if (port->sm_periodic_timer_counter) {
1298 /* check if periodic state machine expired */
1299 if (!(--port->sm_periodic_timer_counter)) {
1300 /* if expired then do tx */
1301 port->sm_periodic_state = AD_PERIODIC_TX;
1303 /* If not expired, check if there is some new timeout
1304 * parameter from the partner state
1306 switch (port->sm_periodic_state) {
1307 case AD_FAST_PERIODIC:
1308 if (!(port->partner_oper.port_state
1309 & LACP_STATE_LACP_TIMEOUT))
1310 port->sm_periodic_state = AD_SLOW_PERIODIC;
1312 case AD_SLOW_PERIODIC:
1313 if ((port->partner_oper.port_state & LACP_STATE_LACP_TIMEOUT)) {
1314 port->sm_periodic_timer_counter = 0;
1315 port->sm_periodic_state = AD_PERIODIC_TX;
1323 switch (port->sm_periodic_state) {
1324 case AD_NO_PERIODIC:
1325 port->sm_periodic_state = AD_FAST_PERIODIC;
1327 case AD_PERIODIC_TX:
1328 if (!(port->partner_oper.port_state &
1329 LACP_STATE_LACP_TIMEOUT))
1330 port->sm_periodic_state = AD_SLOW_PERIODIC;
1332 port->sm_periodic_state = AD_FAST_PERIODIC;
1339 /* check if the state machine was changed */
1340 if (port->sm_periodic_state != last_state) {
1341 slave_dbg(port->slave->bond->dev, port->slave->dev,
1342 "Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1343 port->actor_port_number, last_state,
1344 port->sm_periodic_state);
1345 switch (port->sm_periodic_state) {
1346 case AD_NO_PERIODIC:
1347 port->sm_periodic_timer_counter = 0;
1349 case AD_FAST_PERIODIC:
1350 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1351 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1353 case AD_SLOW_PERIODIC:
1354 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1355 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1357 case AD_PERIODIC_TX:
1367 * ad_port_selection_logic - select aggregation groups
1368 * @port: the port we're looking at
1369 * @update_slave_arr: Does slave array need update?
1371 * Select aggregation groups, and assign each port for it's aggregetor. The
1372 * selection logic is called in the inititalization (after all the handshkes),
1373 * and after every lacpdu receive (if selected is off).
1375 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
1377 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1378 struct port *last_port = NULL, *curr_port;
1379 struct list_head *iter;
1380 struct bonding *bond;
1381 struct slave *slave;
1384 /* if the port is already Selected, do nothing */
1385 if (port->sm_vars & AD_PORT_SELECTED)
1388 bond = __get_bond_by_port(port);
1390 /* if the port is connected to other aggregator, detach it */
1391 if (port->aggregator) {
1392 /* detach the port from its former aggregator */
1393 temp_aggregator = port->aggregator;
1394 for (curr_port = temp_aggregator->lag_ports; curr_port;
1395 last_port = curr_port,
1396 curr_port = curr_port->next_port_in_aggregator) {
1397 if (curr_port == port) {
1398 temp_aggregator->num_of_ports--;
1399 /* if it is the first port attached to the
1403 temp_aggregator->lag_ports =
1404 port->next_port_in_aggregator;
1406 /* not the first port attached to the
1409 last_port->next_port_in_aggregator =
1410 port->next_port_in_aggregator;
1413 /* clear the port's relations to this
1416 port->aggregator = NULL;
1417 port->next_port_in_aggregator = NULL;
1418 port->actor_port_aggregator_identifier = 0;
1420 slave_dbg(bond->dev, port->slave->dev, "Port %d left LAG %d\n",
1421 port->actor_port_number,
1422 temp_aggregator->aggregator_identifier);
1423 /* if the aggregator is empty, clear its
1424 * parameters, and set it ready to be attached
1426 if (!temp_aggregator->lag_ports)
1427 ad_clear_agg(temp_aggregator);
1432 /* meaning: the port was related to an aggregator
1433 * but was not on the aggregator port list
1435 net_warn_ratelimited("%s: (slave %s): Warning: Port %d was related to aggregator %d but was not on its port list\n",
1436 port->slave->bond->dev->name,
1437 port->slave->dev->name,
1438 port->actor_port_number,
1439 port->aggregator->aggregator_identifier);
1442 /* search on all aggregators for a suitable aggregator for this port */
1443 bond_for_each_slave(bond, slave, iter) {
1444 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1446 /* keep a free aggregator for later use(if needed) */
1447 if (!aggregator->lag_ports) {
1448 if (!free_aggregator)
1449 free_aggregator = aggregator;
1452 /* check if current aggregator suits us */
1453 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1454 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1455 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1456 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1458 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1459 !aggregator->is_individual) /* but is not individual OR */
1462 /* attach to the founded aggregator */
1463 port->aggregator = aggregator;
1464 port->actor_port_aggregator_identifier =
1465 port->aggregator->aggregator_identifier;
1466 port->next_port_in_aggregator = aggregator->lag_ports;
1467 port->aggregator->num_of_ports++;
1468 aggregator->lag_ports = port;
1469 slave_dbg(bond->dev, slave->dev, "Port %d joined LAG %d (existing LAG)\n",
1470 port->actor_port_number,
1471 port->aggregator->aggregator_identifier);
1473 /* mark this port as selected */
1474 port->sm_vars |= AD_PORT_SELECTED;
1480 /* the port couldn't find an aggregator - attach it to a new
1484 if (free_aggregator) {
1485 /* assign port a new aggregator */
1486 port->aggregator = free_aggregator;
1487 port->actor_port_aggregator_identifier =
1488 port->aggregator->aggregator_identifier;
1490 /* update the new aggregator's parameters
1491 * if port was responsed from the end-user
1493 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
1494 /* if port is full duplex */
1495 port->aggregator->is_individual = false;
1497 port->aggregator->is_individual = true;
1499 port->aggregator->actor_admin_aggregator_key =
1500 port->actor_admin_port_key;
1501 port->aggregator->actor_oper_aggregator_key =
1502 port->actor_oper_port_key;
1503 port->aggregator->partner_system =
1504 port->partner_oper.system;
1505 port->aggregator->partner_system_priority =
1506 port->partner_oper.system_priority;
1507 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1508 port->aggregator->receive_state = 1;
1509 port->aggregator->transmit_state = 1;
1510 port->aggregator->lag_ports = port;
1511 port->aggregator->num_of_ports++;
1513 /* mark this port as selected */
1514 port->sm_vars |= AD_PORT_SELECTED;
1516 slave_dbg(bond->dev, port->slave->dev, "Port %d joined LAG %d (new LAG)\n",
1517 port->actor_port_number,
1518 port->aggregator->aggregator_identifier);
1520 slave_err(bond->dev, port->slave->dev,
1521 "Port %d did not find a suitable aggregator\n",
1522 port->actor_port_number);
1525 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1526 * in all aggregator's ports, else set ready=FALSE in all
1527 * aggregator's ports
1529 __set_agg_ports_ready(port->aggregator,
1530 __agg_ports_are_ready(port->aggregator));
1532 aggregator = __get_first_agg(port);
1533 ad_agg_selection_logic(aggregator, update_slave_arr);
1535 if (!port->aggregator->is_active)
1536 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1539 /* Decide if "agg" is a better choice for the new active aggregator that
1540 * the current best, according to the ad_select policy.
1542 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1543 struct aggregator *curr)
1545 /* 0. If no best, select current.
1547 * 1. If the current agg is not individual, and the best is
1548 * individual, select current.
1550 * 2. If current agg is individual and the best is not, keep best.
1552 * 3. Therefore, current and best are both individual or both not
1555 * 3a. If current agg partner replied, and best agg partner did not,
1558 * 3b. If current agg partner did not reply and best agg partner
1559 * did reply, keep best.
1561 * 4. Therefore, current and best both have partner replies or
1562 * both do not, so perform selection policy:
1564 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1565 * select by bandwidth.
1567 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1572 if (!curr->is_individual && best->is_individual)
1575 if (curr->is_individual && !best->is_individual)
1578 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1581 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1584 switch (__get_agg_selection_mode(curr->lag_ports)) {
1586 if (__agg_active_ports(curr) > __agg_active_ports(best))
1589 if (__agg_active_ports(curr) < __agg_active_ports(best))
1593 case BOND_AD_STABLE:
1594 case BOND_AD_BANDWIDTH:
1595 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1601 net_warn_ratelimited("%s: (slave %s): Impossible agg select mode %d\n",
1602 curr->slave->bond->dev->name,
1603 curr->slave->dev->name,
1604 __get_agg_selection_mode(curr->lag_ports));
1611 static int agg_device_up(const struct aggregator *agg)
1613 struct port *port = agg->lag_ports;
1618 for (port = agg->lag_ports; port;
1619 port = port->next_port_in_aggregator) {
1620 if (netif_running(port->slave->dev) &&
1621 netif_carrier_ok(port->slave->dev))
1629 * ad_agg_selection_logic - select an aggregation group for a team
1630 * @agg: the aggregator we're looking at
1631 * @update_slave_arr: Does slave array need update?
1633 * It is assumed that only one aggregator may be selected for a team.
1635 * The logic of this function is to select the aggregator according to
1636 * the ad_select policy:
1638 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1639 * it, and to reselect the active aggregator only if the previous
1640 * aggregator has no more ports related to it.
1642 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1643 * bandwidth, and reselect whenever a link state change takes place or the
1644 * set of slaves in the bond changes.
1646 * BOND_AD_COUNT: select the aggregator with largest number of ports
1647 * (slaves), and reselect whenever a link state change takes place or the
1648 * set of slaves in the bond changes.
1650 * FIXME: this function MUST be called with the first agg in the bond, or
1651 * __get_active_agg() won't work correctly. This function should be better
1652 * called with the bond itself, and retrieve the first agg from it.
1654 static void ad_agg_selection_logic(struct aggregator *agg,
1655 bool *update_slave_arr)
1657 struct aggregator *best, *active, *origin;
1658 struct bonding *bond = agg->slave->bond;
1659 struct list_head *iter;
1660 struct slave *slave;
1665 active = __get_active_agg(agg);
1666 best = (active && agg_device_up(active)) ? active : NULL;
1668 bond_for_each_slave_rcu(bond, slave, iter) {
1669 agg = &(SLAVE_AD_INFO(slave)->aggregator);
1673 if (__agg_active_ports(agg) && agg_device_up(agg))
1674 best = ad_agg_selection_test(best, agg);
1678 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1679 /* For the STABLE policy, don't replace the old active
1680 * aggregator if it's still active (it has an answering
1681 * partner) or if both the best and active don't have an
1682 * answering partner.
1684 if (active && active->lag_ports &&
1685 __agg_active_ports(active) &&
1686 (__agg_has_partner(active) ||
1687 (!__agg_has_partner(active) &&
1688 !__agg_has_partner(best)))) {
1689 if (!(!active->actor_oper_aggregator_key &&
1690 best->actor_oper_aggregator_key)) {
1692 active->is_active = 1;
1697 if (best && (best == active)) {
1699 active->is_active = 1;
1702 /* if there is new best aggregator, activate it */
1704 netdev_dbg(bond->dev, "(slave %s): best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1705 best->slave ? best->slave->dev->name : "NULL",
1706 best->aggregator_identifier, best->num_of_ports,
1707 best->actor_oper_aggregator_key,
1708 best->partner_oper_aggregator_key,
1709 best->is_individual, best->is_active);
1710 netdev_dbg(bond->dev, "(slave %s): best ports %p slave %p\n",
1711 best->slave ? best->slave->dev->name : "NULL",
1712 best->lag_ports, best->slave);
1714 bond_for_each_slave_rcu(bond, slave, iter) {
1715 agg = &(SLAVE_AD_INFO(slave)->aggregator);
1717 slave_dbg(bond->dev, slave->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1718 agg->aggregator_identifier, agg->num_of_ports,
1719 agg->actor_oper_aggregator_key,
1720 agg->partner_oper_aggregator_key,
1721 agg->is_individual, agg->is_active);
1724 /* check if any partner replies */
1725 if (best->is_individual)
1726 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1729 best->is_active = 1;
1730 netdev_dbg(bond->dev, "(slave %s): LAG %d chosen as the active LAG\n",
1731 best->slave ? best->slave->dev->name : "NULL",
1732 best->aggregator_identifier);
1733 netdev_dbg(bond->dev, "(slave %s): Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1734 best->slave ? best->slave->dev->name : "NULL",
1735 best->aggregator_identifier, best->num_of_ports,
1736 best->actor_oper_aggregator_key,
1737 best->partner_oper_aggregator_key,
1738 best->is_individual, best->is_active);
1740 /* disable the ports that were related to the former
1744 for (port = active->lag_ports; port;
1745 port = port->next_port_in_aggregator) {
1746 __disable_port(port);
1749 /* Slave array needs update. */
1750 *update_slave_arr = true;
1753 /* if the selected aggregator is of join individuals
1754 * (partner_system is NULL), enable their ports
1756 active = __get_active_agg(origin);
1759 if (!__agg_has_partner(active)) {
1760 for (port = active->lag_ports; port;
1761 port = port->next_port_in_aggregator) {
1762 __enable_port(port);
1764 *update_slave_arr = true;
1770 bond_3ad_set_carrier(bond);
1774 * ad_clear_agg - clear a given aggregator's parameters
1775 * @aggregator: the aggregator we're looking at
1777 static void ad_clear_agg(struct aggregator *aggregator)
1780 aggregator->is_individual = false;
1781 aggregator->actor_admin_aggregator_key = 0;
1782 aggregator->actor_oper_aggregator_key = 0;
1783 eth_zero_addr(aggregator->partner_system.mac_addr_value);
1784 aggregator->partner_system_priority = 0;
1785 aggregator->partner_oper_aggregator_key = 0;
1786 aggregator->receive_state = 0;
1787 aggregator->transmit_state = 0;
1788 aggregator->lag_ports = NULL;
1789 aggregator->is_active = 0;
1790 aggregator->num_of_ports = 0;
1791 pr_debug("%s: LAG %d was cleared\n",
1793 aggregator->slave->dev->name : "NULL",
1794 aggregator->aggregator_identifier);
1799 * ad_initialize_agg - initialize a given aggregator's parameters
1800 * @aggregator: the aggregator we're looking at
1802 static void ad_initialize_agg(struct aggregator *aggregator)
1805 ad_clear_agg(aggregator);
1807 eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1808 aggregator->aggregator_identifier = 0;
1809 aggregator->slave = NULL;
1814 * ad_initialize_port - initialize a given port's parameters
1815 * @port: the port we're looking at
1816 * @lacp_fast: boolean. whether fast periodic should be used
1818 static void ad_initialize_port(struct port *port, int lacp_fast)
1820 static const struct port_params tmpl = {
1821 .system_priority = 0xffff,
1824 .port_priority = 0xff,
1827 static const struct lacpdu lacpdu = {
1829 .version_number = 0x01,
1830 .tlv_type_actor_info = 0x01,
1831 .actor_information_length = 0x14,
1832 .tlv_type_partner_info = 0x02,
1833 .partner_information_length = 0x14,
1834 .tlv_type_collector_info = 0x03,
1835 .collector_information_length = 0x10,
1836 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1840 port->actor_port_priority = 0xff;
1841 port->actor_port_aggregator_identifier = 0;
1843 port->actor_admin_port_state = LACP_STATE_AGGREGATION |
1844 LACP_STATE_LACP_ACTIVITY;
1845 port->actor_oper_port_state = LACP_STATE_AGGREGATION |
1846 LACP_STATE_LACP_ACTIVITY;
1849 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
1851 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1852 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1854 port->is_enabled = true;
1855 /* private parameters */
1856 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1857 port->sm_rx_state = 0;
1858 port->sm_rx_timer_counter = 0;
1859 port->sm_periodic_state = 0;
1860 port->sm_periodic_timer_counter = 0;
1861 port->sm_mux_state = 0;
1862 port->sm_mux_timer_counter = 0;
1863 port->sm_tx_state = 0;
1864 port->aggregator = NULL;
1865 port->next_port_in_aggregator = NULL;
1866 port->transaction_id = 0;
1868 port->sm_churn_actor_timer_counter = 0;
1869 port->sm_churn_actor_state = 0;
1870 port->churn_actor_count = 0;
1871 port->sm_churn_partner_timer_counter = 0;
1872 port->sm_churn_partner_state = 0;
1873 port->churn_partner_count = 0;
1875 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1880 * ad_enable_collecting_distributing - enable a port's transmit/receive
1881 * @port: the port we're looking at
1882 * @update_slave_arr: Does slave array need update?
1884 * Enable @port if it's in an active aggregator
1886 static void ad_enable_collecting_distributing(struct port *port,
1887 bool *update_slave_arr)
1889 if (port->aggregator->is_active) {
1890 slave_dbg(port->slave->bond->dev, port->slave->dev,
1891 "Enabling port %d (LAG %d)\n",
1892 port->actor_port_number,
1893 port->aggregator->aggregator_identifier);
1894 __enable_port(port);
1895 /* Slave array needs update */
1896 *update_slave_arr = true;
1901 * ad_disable_collecting_distributing - disable a port's transmit/receive
1902 * @port: the port we're looking at
1903 * @update_slave_arr: Does slave array need update?
1905 static void ad_disable_collecting_distributing(struct port *port,
1906 bool *update_slave_arr)
1908 if (port->aggregator &&
1909 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1910 &(null_mac_addr))) {
1911 slave_dbg(port->slave->bond->dev, port->slave->dev,
1912 "Disabling port %d (LAG %d)\n",
1913 port->actor_port_number,
1914 port->aggregator->aggregator_identifier);
1915 __disable_port(port);
1916 /* Slave array needs an update */
1917 *update_slave_arr = true;
1922 * ad_marker_info_received - handle receive of a Marker information frame
1923 * @marker_info: Marker info received
1924 * @port: the port we're looking at
1926 static void ad_marker_info_received(struct bond_marker *marker_info,
1929 struct bond_marker marker;
1931 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx);
1932 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_rx);
1934 /* copy the received marker data to the response marker */
1935 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1936 /* change the marker subtype to marker response */
1937 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1939 /* send the marker response */
1940 if (ad_marker_send(port, &marker) >= 0)
1941 slave_dbg(port->slave->bond->dev, port->slave->dev,
1942 "Sent Marker Response on port %d\n",
1943 port->actor_port_number);
1947 * ad_marker_response_received - handle receive of a marker response frame
1948 * @marker: marker PDU received
1949 * @port: the port we're looking at
1951 * This function does nothing since we decided not to implement send and handle
1952 * response for marker PDU's, in this stage, but only to respond to marker
1955 static void ad_marker_response_received(struct bond_marker *marker,
1958 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_resp_rx);
1959 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_resp_rx);
1961 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1964 /* ========= AD exported functions to the main bonding code ========= */
1966 /* Check aggregators status in team every T seconds */
1967 #define AD_AGGREGATOR_SELECTION_TIMER 8
1970 * bond_3ad_initiate_agg_selection - initate aggregator selection
1971 * @bond: bonding struct
1972 * @timeout: timeout value to set
1974 * Set the aggregation selection timer, to initiate an agg selection in
1975 * the very near future. Called during first initialization, and during
1976 * any down to up transitions of the bond.
1978 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1980 atomic_set(&BOND_AD_INFO(bond).agg_select_timer, timeout);
1984 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1985 * @bond: bonding struct to work on
1986 * @tick_resolution: tick duration (millisecond resolution)
1988 * Can be called only after the mac address of the bond is set.
1990 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
1992 BOND_AD_INFO(bond).aggregator_identifier = 0;
1993 BOND_AD_INFO(bond).system.sys_priority =
1994 bond->params.ad_actor_sys_prio;
1995 if (is_zero_ether_addr(bond->params.ad_actor_system))
1996 BOND_AD_INFO(bond).system.sys_mac_addr =
1997 *((struct mac_addr *)bond->dev->dev_addr);
1999 BOND_AD_INFO(bond).system.sys_mac_addr =
2000 *((struct mac_addr *)bond->params.ad_actor_system);
2002 /* initialize how many times this module is called in one
2003 * second (should be about every 100ms)
2005 ad_ticks_per_sec = tick_resolution;
2007 bond_3ad_initiate_agg_selection(bond,
2008 AD_AGGREGATOR_SELECTION_TIMER *
2013 * bond_3ad_bind_slave - initialize a slave's port
2014 * @slave: slave struct to work on
2016 * Returns: 0 on success
2019 void bond_3ad_bind_slave(struct slave *slave)
2021 struct bonding *bond = bond_get_bond_by_slave(slave);
2023 struct aggregator *aggregator;
2025 /* check that the slave has not been initialized yet. */
2026 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2028 /* port initialization */
2029 port = &(SLAVE_AD_INFO(slave)->port);
2031 ad_initialize_port(port, bond->params.lacp_fast);
2033 port->slave = slave;
2034 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2035 /* key is determined according to the link speed, duplex and
2038 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2039 ad_update_actor_keys(port, false);
2040 /* actor system is the bond's system */
2041 __ad_actor_update_port(port);
2042 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2043 * lacpdu's are sent in one second)
2045 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2047 __disable_port(port);
2049 /* aggregator initialization */
2050 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2052 ad_initialize_agg(aggregator);
2054 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2055 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2056 aggregator->slave = slave;
2057 aggregator->is_active = 0;
2058 aggregator->num_of_ports = 0;
2063 * bond_3ad_unbind_slave - deinitialize a slave's port
2064 * @slave: slave struct to work on
2066 * Search for the aggregator that is related to this port, remove the
2067 * aggregator and assign another aggregator for other port related to it
2068 * (if any), and remove the port.
2070 void bond_3ad_unbind_slave(struct slave *slave)
2072 struct port *port, *prev_port, *temp_port;
2073 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2074 int select_new_active_agg = 0;
2075 struct bonding *bond = slave->bond;
2076 struct slave *slave_iter;
2077 struct list_head *iter;
2078 bool dummy_slave_update; /* Ignore this value as caller updates array */
2080 /* Sync against bond_3ad_state_machine_handler() */
2081 spin_lock_bh(&bond->mode_lock);
2082 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2083 port = &(SLAVE_AD_INFO(slave)->port);
2085 /* if slave is null, the whole port is not initialized */
2087 slave_warn(bond->dev, slave->dev, "Trying to unbind an uninitialized port\n");
2091 slave_dbg(bond->dev, slave->dev, "Unbinding Link Aggregation Group %d\n",
2092 aggregator->aggregator_identifier);
2094 /* Tell the partner that this port is not suitable for aggregation */
2095 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
2096 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
2097 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
2098 port->actor_oper_port_state &= ~LACP_STATE_AGGREGATION;
2099 __update_lacpdu_from_port(port);
2100 ad_lacpdu_send(port);
2102 /* check if this aggregator is occupied */
2103 if (aggregator->lag_ports) {
2104 /* check if there are other ports related to this aggregator
2105 * except the port related to this slave(thats ensure us that
2106 * there is a reason to search for new aggregator, and that we
2109 if ((aggregator->lag_ports != port) ||
2110 (aggregator->lag_ports->next_port_in_aggregator)) {
2111 /* find new aggregator for the related port(s) */
2112 bond_for_each_slave(bond, slave_iter, iter) {
2113 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2114 /* if the new aggregator is empty, or it is
2115 * connected to our port only
2117 if (!new_aggregator->lag_ports ||
2118 ((new_aggregator->lag_ports == port) &&
2119 !new_aggregator->lag_ports->next_port_in_aggregator))
2123 new_aggregator = NULL;
2125 /* if new aggregator found, copy the aggregator's
2126 * parameters and connect the related lag_ports to the
2129 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2130 slave_dbg(bond->dev, slave->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2131 aggregator->aggregator_identifier,
2132 new_aggregator->aggregator_identifier);
2134 if ((new_aggregator->lag_ports == port) &&
2135 new_aggregator->is_active) {
2136 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2137 select_new_active_agg = 1;
2140 new_aggregator->is_individual = aggregator->is_individual;
2141 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2142 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2143 new_aggregator->partner_system = aggregator->partner_system;
2144 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2145 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2146 new_aggregator->receive_state = aggregator->receive_state;
2147 new_aggregator->transmit_state = aggregator->transmit_state;
2148 new_aggregator->lag_ports = aggregator->lag_ports;
2149 new_aggregator->is_active = aggregator->is_active;
2150 new_aggregator->num_of_ports = aggregator->num_of_ports;
2152 /* update the information that is written on
2153 * the ports about the aggregator
2155 for (temp_port = aggregator->lag_ports; temp_port;
2156 temp_port = temp_port->next_port_in_aggregator) {
2157 temp_port->aggregator = new_aggregator;
2158 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2161 ad_clear_agg(aggregator);
2163 if (select_new_active_agg)
2164 ad_agg_selection_logic(__get_first_agg(port),
2165 &dummy_slave_update);
2167 slave_warn(bond->dev, slave->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2170 /* in case that the only port related to this
2171 * aggregator is the one we want to remove
2173 select_new_active_agg = aggregator->is_active;
2174 ad_clear_agg(aggregator);
2175 if (select_new_active_agg) {
2176 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2177 /* select new active aggregator */
2178 temp_aggregator = __get_first_agg(port);
2179 if (temp_aggregator)
2180 ad_agg_selection_logic(temp_aggregator,
2181 &dummy_slave_update);
2186 slave_dbg(bond->dev, slave->dev, "Unbinding port %d\n", port->actor_port_number);
2188 /* find the aggregator that this port is connected to */
2189 bond_for_each_slave(bond, slave_iter, iter) {
2190 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2192 /* search the port in the aggregator's related ports */
2193 for (temp_port = temp_aggregator->lag_ports; temp_port;
2194 prev_port = temp_port,
2195 temp_port = temp_port->next_port_in_aggregator) {
2196 if (temp_port == port) {
2197 /* the aggregator found - detach the port from
2201 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2203 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2204 temp_aggregator->num_of_ports--;
2205 if (__agg_active_ports(temp_aggregator) == 0) {
2206 select_new_active_agg = temp_aggregator->is_active;
2207 if (temp_aggregator->num_of_ports == 0)
2208 ad_clear_agg(temp_aggregator);
2209 if (select_new_active_agg) {
2210 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2211 /* select new active aggregator */
2212 ad_agg_selection_logic(__get_first_agg(port),
2213 &dummy_slave_update);
2223 spin_unlock_bh(&bond->mode_lock);
2227 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2228 * @bond: bonding struct to work on
2230 * If an ad_actor setting gets changed we need to update the individual port
2231 * settings so the bond device will use the new values when it gets upped.
2233 void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2235 struct list_head *iter;
2236 struct slave *slave;
2240 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2241 if (is_zero_ether_addr(bond->params.ad_actor_system))
2242 BOND_AD_INFO(bond).system.sys_mac_addr =
2243 *((struct mac_addr *)bond->dev->dev_addr);
2245 BOND_AD_INFO(bond).system.sys_mac_addr =
2246 *((struct mac_addr *)bond->params.ad_actor_system);
2248 spin_lock_bh(&bond->mode_lock);
2249 bond_for_each_slave(bond, slave, iter) {
2250 struct port *port = &(SLAVE_AD_INFO(slave))->port;
2252 __ad_actor_update_port(port);
2255 spin_unlock_bh(&bond->mode_lock);
2259 * bond_agg_timer_advance - advance agg_select_timer
2260 * @bond: bonding structure
2262 * Return true when agg_select_timer reaches 0.
2264 static bool bond_agg_timer_advance(struct bonding *bond)
2269 val = atomic_read(&BOND_AD_INFO(bond).agg_select_timer);
2273 if (atomic_cmpxchg(&BOND_AD_INFO(bond).agg_select_timer,
2281 * bond_3ad_state_machine_handler - handle state machines timeout
2282 * @work: work context to fetch bonding struct to work on from
2284 * The state machine handling concept in this module is to check every tick
2285 * which state machine should operate any function. The execution order is
2286 * round robin, so when we have an interaction between state machines, the
2287 * reply of one to each other might be delayed until next tick.
2289 * This function also complete the initialization when the agg_select_timer
2290 * times out, and it selects an aggregator for the ports that are yet not
2291 * related to any aggregator, and selects the active aggregator for a bond.
2293 void bond_3ad_state_machine_handler(struct work_struct *work)
2295 struct bonding *bond = container_of(work, struct bonding,
2297 struct aggregator *aggregator;
2298 struct list_head *iter;
2299 struct slave *slave;
2301 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2302 bool update_slave_arr = false;
2304 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2305 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2306 * concurrently due to incoming LACPDU as well.
2308 spin_lock_bh(&bond->mode_lock);
2311 /* check if there are any slaves */
2312 if (!bond_has_slaves(bond))
2315 if (bond_agg_timer_advance(bond)) {
2316 slave = bond_first_slave_rcu(bond);
2317 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2319 /* select the active aggregator for the bond */
2322 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2327 aggregator = __get_first_agg(port);
2328 ad_agg_selection_logic(aggregator, &update_slave_arr);
2330 bond_3ad_set_carrier(bond);
2333 /* for each port run the state machines */
2334 bond_for_each_slave_rcu(bond, slave, iter) {
2335 port = &(SLAVE_AD_INFO(slave)->port);
2337 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2342 ad_rx_machine(NULL, port);
2343 ad_periodic_machine(port);
2344 ad_port_selection_logic(port, &update_slave_arr);
2345 ad_mux_machine(port, &update_slave_arr);
2346 ad_tx_machine(port);
2347 ad_churn_machine(port);
2349 /* turn off the BEGIN bit, since we already handled it */
2350 if (port->sm_vars & AD_PORT_BEGIN)
2351 port->sm_vars &= ~AD_PORT_BEGIN;
2355 bond_for_each_slave_rcu(bond, slave, iter) {
2356 if (slave->should_notify) {
2357 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2362 spin_unlock_bh(&bond->mode_lock);
2364 if (update_slave_arr)
2365 bond_slave_arr_work_rearm(bond, 0);
2367 if (should_notify_rtnl && rtnl_trylock()) {
2368 bond_slave_state_notify(bond);
2371 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2375 * bond_3ad_rx_indication - handle a received frame
2376 * @lacpdu: received lacpdu
2377 * @slave: slave struct to work on
2379 * It is assumed that frames that were sent on this NIC don't returned as new
2380 * received frames (loopback). Since only the payload is given to this
2381 * function, it check for loopback.
2383 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave)
2385 struct bonding *bond = slave->bond;
2386 int ret = RX_HANDLER_ANOTHER;
2387 struct bond_marker *marker;
2391 port = &(SLAVE_AD_INFO(slave)->port);
2393 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2394 slave->dev->name, slave->bond->dev->name);
2398 switch (lacpdu->subtype) {
2399 case AD_TYPE_LACPDU:
2400 ret = RX_HANDLER_CONSUMED;
2401 slave_dbg(slave->bond->dev, slave->dev,
2402 "Received LACPDU on port %d\n",
2403 port->actor_port_number);
2404 /* Protect against concurrent state machines */
2405 spin_lock(&slave->bond->mode_lock);
2406 ad_rx_machine(lacpdu, port);
2407 spin_unlock(&slave->bond->mode_lock);
2409 case AD_TYPE_MARKER:
2410 ret = RX_HANDLER_CONSUMED;
2411 /* No need to convert fields to Little Endian since we
2412 * don't use the marker's fields.
2414 marker = (struct bond_marker *)lacpdu;
2415 switch (marker->tlv_type) {
2416 case AD_MARKER_INFORMATION_SUBTYPE:
2417 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Information on port %d\n",
2418 port->actor_port_number);
2419 ad_marker_info_received(marker, port);
2421 case AD_MARKER_RESPONSE_SUBTYPE:
2422 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Response on port %d\n",
2423 port->actor_port_number);
2424 ad_marker_response_received(marker, port);
2427 slave_dbg(slave->bond->dev, slave->dev, "Received an unknown Marker subtype on port %d\n",
2428 port->actor_port_number);
2429 stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx;
2431 stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx;
2436 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx);
2437 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx);
2444 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2445 * its current speed and duplex settings.
2447 * @port: the port we'are looking at
2448 * @reset: Boolean to just reset the speed and the duplex part of the key
2450 * The logic to change the oper / admin keys is:
2451 * (a) A full duplex port can participate in LACP with partner.
2452 * (b) When the speed is changed, LACP need to be reinitiated.
2454 static void ad_update_actor_keys(struct port *port, bool reset)
2457 u16 ospeed = 0, speed = 0;
2458 u16 old_oper_key = port->actor_oper_port_key;
2460 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2462 speed = __get_link_speed(port);
2463 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2464 duplex = __get_duplex(port);
2465 port->actor_admin_port_key |= (speed << 1) | duplex;
2467 port->actor_oper_port_key = port->actor_admin_port_key;
2469 if (old_oper_key != port->actor_oper_port_key) {
2470 /* Only 'duplex' port participates in LACP */
2472 port->sm_vars |= AD_PORT_LACP_ENABLED;
2474 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2478 slave_err(port->slave->bond->dev,
2480 "speed changed to 0 on port %d\n",
2481 port->actor_port_number);
2482 } else if (duplex && ospeed != speed) {
2483 /* Speed change restarts LACP state-machine */
2484 port->sm_vars |= AD_PORT_BEGIN;
2491 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2494 * @slave: slave struct to work on
2496 * Handle reselection of aggregator (if needed) for this port.
2498 void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2502 port = &(SLAVE_AD_INFO(slave)->port);
2504 /* if slave is null, the whole port is not initialized */
2506 slave_warn(slave->bond->dev, slave->dev,
2507 "speed/duplex changed for uninitialized port\n");
2511 spin_lock_bh(&slave->bond->mode_lock);
2512 ad_update_actor_keys(port, false);
2513 spin_unlock_bh(&slave->bond->mode_lock);
2514 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed speed/duplex\n",
2515 port->actor_port_number);
2519 * bond_3ad_handle_link_change - handle a slave's link status change indication
2520 * @slave: slave struct to work on
2521 * @link: whether the link is now up or down
2523 * Handle reselection of aggregator (if needed) for this port.
2525 void bond_3ad_handle_link_change(struct slave *slave, char link)
2527 struct aggregator *agg;
2531 port = &(SLAVE_AD_INFO(slave)->port);
2533 /* if slave is null, the whole port is not initialized */
2535 slave_warn(slave->bond->dev, slave->dev, "link status changed for uninitialized port\n");
2539 spin_lock_bh(&slave->bond->mode_lock);
2540 /* on link down we are zeroing duplex and speed since
2541 * some of the adaptors(ce1000.lan) report full duplex/speed
2542 * instead of N/A(duplex) / 0(speed).
2544 * on link up we are forcing recheck on the duplex and speed since
2545 * some of he adaptors(ce1000.lan) report.
2547 if (link == BOND_LINK_UP) {
2548 port->is_enabled = true;
2549 ad_update_actor_keys(port, false);
2551 /* link has failed */
2552 port->is_enabled = false;
2553 ad_update_actor_keys(port, true);
2555 agg = __get_first_agg(port);
2556 ad_agg_selection_logic(agg, &dummy);
2558 spin_unlock_bh(&slave->bond->mode_lock);
2560 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed link status to %s\n",
2561 port->actor_port_number,
2562 link == BOND_LINK_UP ? "UP" : "DOWN");
2564 /* RTNL is held and mode_lock is released so it's safe
2565 * to update slave_array here.
2567 bond_update_slave_arr(slave->bond, NULL);
2571 * bond_3ad_set_carrier - set link state for bonding master
2572 * @bond: bonding structure
2574 * if we have an active aggregator, we're up, if not, we're down.
2575 * Presumes that we cannot have an active aggregator if there are
2576 * no slaves with link up.
2578 * This behavior complies with IEEE 802.3 section 43.3.9.
2580 * Called by bond_set_carrier(). Return zero if carrier state does not
2581 * change, nonzero if it does.
2583 int bond_3ad_set_carrier(struct bonding *bond)
2585 struct aggregator *active;
2586 struct slave *first_slave;
2590 first_slave = bond_first_slave_rcu(bond);
2595 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2597 /* are enough slaves available to consider link up? */
2598 if (__agg_active_ports(active) < bond->params.min_links) {
2599 if (netif_carrier_ok(bond->dev)) {
2600 netif_carrier_off(bond->dev);
2603 } else if (!netif_carrier_ok(bond->dev)) {
2604 netif_carrier_on(bond->dev);
2607 } else if (netif_carrier_ok(bond->dev)) {
2608 netif_carrier_off(bond->dev);
2616 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2617 * @bond: bonding struct to work on
2618 * @ad_info: ad_info struct to fill with the bond's info
2620 * Returns: 0 on success
2623 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2624 struct ad_info *ad_info)
2626 struct aggregator *aggregator = NULL;
2627 struct list_head *iter;
2628 struct slave *slave;
2631 bond_for_each_slave_rcu(bond, slave, iter) {
2632 port = &(SLAVE_AD_INFO(slave)->port);
2633 if (port->aggregator && port->aggregator->is_active) {
2634 aggregator = port->aggregator;
2642 ad_info->aggregator_id = aggregator->aggregator_identifier;
2643 ad_info->ports = __agg_active_ports(aggregator);
2644 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2645 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2646 ether_addr_copy(ad_info->partner_system,
2647 aggregator->partner_system.mac_addr_value);
2651 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2656 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2662 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2663 struct slave *slave)
2665 struct lacpdu *lacpdu, _lacpdu;
2667 if (skb->protocol != PKT_TYPE_LACPDU)
2668 return RX_HANDLER_ANOTHER;
2670 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2671 return RX_HANDLER_ANOTHER;
2673 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2675 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx);
2676 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx);
2677 return RX_HANDLER_ANOTHER;
2680 return bond_3ad_rx_indication(lacpdu, slave);
2684 * bond_3ad_update_lacp_rate - change the lacp rate
2685 * @bond: bonding struct
2687 * When modify lacp_rate parameter via sysfs,
2688 * update actor_oper_port_state of each port.
2690 * Hold bond->mode_lock,
2691 * so we can modify port->actor_oper_port_state,
2692 * no matter bond is up or down.
2694 void bond_3ad_update_lacp_rate(struct bonding *bond)
2696 struct port *port = NULL;
2697 struct list_head *iter;
2698 struct slave *slave;
2701 lacp_fast = bond->params.lacp_fast;
2702 spin_lock_bh(&bond->mode_lock);
2703 bond_for_each_slave(bond, slave, iter) {
2704 port = &(SLAVE_AD_INFO(slave)->port);
2706 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
2708 port->actor_oper_port_state &= ~LACP_STATE_LACP_TIMEOUT;
2710 spin_unlock_bh(&bond->mode_lock);
2713 size_t bond_3ad_stats_size(void)
2715 return nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_RX */
2716 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_TX */
2717 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_UNKNOWN_RX */
2718 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_ILLEGAL_RX */
2719 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RX */
2720 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_TX */
2721 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_RX */
2722 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_TX */
2723 nla_total_size_64bit(sizeof(u64)); /* BOND_3AD_STAT_MARKER_UNKNOWN_RX */
2726 int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats)
2730 val = atomic64_read(&stats->lacpdu_rx);
2731 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val,
2734 val = atomic64_read(&stats->lacpdu_tx);
2735 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val,
2738 val = atomic64_read(&stats->lacpdu_unknown_rx);
2739 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val,
2742 val = atomic64_read(&stats->lacpdu_illegal_rx);
2743 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val,
2747 val = atomic64_read(&stats->marker_rx);
2748 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val,
2751 val = atomic64_read(&stats->marker_tx);
2752 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val,
2755 val = atomic64_read(&stats->marker_resp_rx);
2756 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val,
2759 val = atomic64_read(&stats->marker_resp_tx);
2760 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val,
2763 val = atomic64_read(&stats->marker_unknown_rx);
2764 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val,