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);
1526 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1527 * in all aggregator's ports, else set ready=FALSE in all
1528 * aggregator's ports
1530 __set_agg_ports_ready(port->aggregator,
1531 __agg_ports_are_ready(port->aggregator));
1533 aggregator = __get_first_agg(port);
1534 ad_agg_selection_logic(aggregator, update_slave_arr);
1536 if (!port->aggregator->is_active)
1537 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1540 /* Decide if "agg" is a better choice for the new active aggregator that
1541 * the current best, according to the ad_select policy.
1543 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1544 struct aggregator *curr)
1546 /* 0. If no best, select current.
1548 * 1. If the current agg is not individual, and the best is
1549 * individual, select current.
1551 * 2. If current agg is individual and the best is not, keep best.
1553 * 3. Therefore, current and best are both individual or both not
1556 * 3a. If current agg partner replied, and best agg partner did not,
1559 * 3b. If current agg partner did not reply and best agg partner
1560 * did reply, keep best.
1562 * 4. Therefore, current and best both have partner replies or
1563 * both do not, so perform selection policy:
1565 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1566 * select by bandwidth.
1568 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1573 if (!curr->is_individual && best->is_individual)
1576 if (curr->is_individual && !best->is_individual)
1579 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1582 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1585 switch (__get_agg_selection_mode(curr->lag_ports)) {
1587 if (__agg_active_ports(curr) > __agg_active_ports(best))
1590 if (__agg_active_ports(curr) < __agg_active_ports(best))
1594 case BOND_AD_STABLE:
1595 case BOND_AD_BANDWIDTH:
1596 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1602 net_warn_ratelimited("%s: (slave %s): Impossible agg select mode %d\n",
1603 curr->slave->bond->dev->name,
1604 curr->slave->dev->name,
1605 __get_agg_selection_mode(curr->lag_ports));
1612 static int agg_device_up(const struct aggregator *agg)
1614 struct port *port = agg->lag_ports;
1619 for (port = agg->lag_ports; port;
1620 port = port->next_port_in_aggregator) {
1621 if (netif_running(port->slave->dev) &&
1622 netif_carrier_ok(port->slave->dev))
1630 * ad_agg_selection_logic - select an aggregation group for a team
1631 * @agg: the aggregator we're looking at
1632 * @update_slave_arr: Does slave array need update?
1634 * It is assumed that only one aggregator may be selected for a team.
1636 * The logic of this function is to select the aggregator according to
1637 * the ad_select policy:
1639 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1640 * it, and to reselect the active aggregator only if the previous
1641 * aggregator has no more ports related to it.
1643 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1644 * bandwidth, and reselect whenever a link state change takes place or the
1645 * set of slaves in the bond changes.
1647 * BOND_AD_COUNT: select the aggregator with largest number of ports
1648 * (slaves), and reselect whenever a link state change takes place or the
1649 * set of slaves in the bond changes.
1651 * FIXME: this function MUST be called with the first agg in the bond, or
1652 * __get_active_agg() won't work correctly. This function should be better
1653 * called with the bond itself, and retrieve the first agg from it.
1655 static void ad_agg_selection_logic(struct aggregator *agg,
1656 bool *update_slave_arr)
1658 struct aggregator *best, *active, *origin;
1659 struct bonding *bond = agg->slave->bond;
1660 struct list_head *iter;
1661 struct slave *slave;
1666 active = __get_active_agg(agg);
1667 best = (active && agg_device_up(active)) ? active : NULL;
1669 bond_for_each_slave_rcu(bond, slave, iter) {
1670 agg = &(SLAVE_AD_INFO(slave)->aggregator);
1674 if (__agg_active_ports(agg) && agg_device_up(agg))
1675 best = ad_agg_selection_test(best, agg);
1679 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1680 /* For the STABLE policy, don't replace the old active
1681 * aggregator if it's still active (it has an answering
1682 * partner) or if both the best and active don't have an
1683 * answering partner.
1685 if (active && active->lag_ports &&
1686 __agg_active_ports(active) &&
1687 (__agg_has_partner(active) ||
1688 (!__agg_has_partner(active) &&
1689 !__agg_has_partner(best)))) {
1690 if (!(!active->actor_oper_aggregator_key &&
1691 best->actor_oper_aggregator_key)) {
1693 active->is_active = 1;
1698 if (best && (best == active)) {
1700 active->is_active = 1;
1703 /* if there is new best aggregator, activate it */
1705 netdev_dbg(bond->dev, "(slave %s): best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1706 best->slave ? best->slave->dev->name : "NULL",
1707 best->aggregator_identifier, best->num_of_ports,
1708 best->actor_oper_aggregator_key,
1709 best->partner_oper_aggregator_key,
1710 best->is_individual, best->is_active);
1711 netdev_dbg(bond->dev, "(slave %s): best ports %p slave %p\n",
1712 best->slave ? best->slave->dev->name : "NULL",
1713 best->lag_ports, best->slave);
1715 bond_for_each_slave_rcu(bond, slave, iter) {
1716 agg = &(SLAVE_AD_INFO(slave)->aggregator);
1718 slave_dbg(bond->dev, slave->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1719 agg->aggregator_identifier, agg->num_of_ports,
1720 agg->actor_oper_aggregator_key,
1721 agg->partner_oper_aggregator_key,
1722 agg->is_individual, agg->is_active);
1725 /* check if any partner replies */
1726 if (best->is_individual)
1727 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1730 best->is_active = 1;
1731 netdev_dbg(bond->dev, "(slave %s): LAG %d chosen as the active LAG\n",
1732 best->slave ? best->slave->dev->name : "NULL",
1733 best->aggregator_identifier);
1734 netdev_dbg(bond->dev, "(slave %s): Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1735 best->slave ? best->slave->dev->name : "NULL",
1736 best->aggregator_identifier, best->num_of_ports,
1737 best->actor_oper_aggregator_key,
1738 best->partner_oper_aggregator_key,
1739 best->is_individual, best->is_active);
1741 /* disable the ports that were related to the former
1745 for (port = active->lag_ports; port;
1746 port = port->next_port_in_aggregator) {
1747 __disable_port(port);
1750 /* Slave array needs update. */
1751 *update_slave_arr = true;
1754 /* if the selected aggregator is of join individuals
1755 * (partner_system is NULL), enable their ports
1757 active = __get_active_agg(origin);
1760 if (!__agg_has_partner(active)) {
1761 for (port = active->lag_ports; port;
1762 port = port->next_port_in_aggregator) {
1763 __enable_port(port);
1765 *update_slave_arr = true;
1771 bond_3ad_set_carrier(bond);
1775 * ad_clear_agg - clear a given aggregator's parameters
1776 * @aggregator: the aggregator we're looking at
1778 static void ad_clear_agg(struct aggregator *aggregator)
1781 aggregator->is_individual = false;
1782 aggregator->actor_admin_aggregator_key = 0;
1783 aggregator->actor_oper_aggregator_key = 0;
1784 eth_zero_addr(aggregator->partner_system.mac_addr_value);
1785 aggregator->partner_system_priority = 0;
1786 aggregator->partner_oper_aggregator_key = 0;
1787 aggregator->receive_state = 0;
1788 aggregator->transmit_state = 0;
1789 aggregator->lag_ports = NULL;
1790 aggregator->is_active = 0;
1791 aggregator->num_of_ports = 0;
1792 pr_debug("%s: LAG %d was cleared\n",
1794 aggregator->slave->dev->name : "NULL",
1795 aggregator->aggregator_identifier);
1800 * ad_initialize_agg - initialize a given aggregator's parameters
1801 * @aggregator: the aggregator we're looking at
1803 static void ad_initialize_agg(struct aggregator *aggregator)
1806 ad_clear_agg(aggregator);
1808 eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1809 aggregator->aggregator_identifier = 0;
1810 aggregator->slave = NULL;
1815 * ad_initialize_port - initialize a given port's parameters
1816 * @port: the port we're looking at
1817 * @lacp_fast: boolean. whether fast periodic should be used
1819 static void ad_initialize_port(struct port *port, int lacp_fast)
1821 static const struct port_params tmpl = {
1822 .system_priority = 0xffff,
1825 .port_priority = 0xff,
1828 static const struct lacpdu lacpdu = {
1830 .version_number = 0x01,
1831 .tlv_type_actor_info = 0x01,
1832 .actor_information_length = 0x14,
1833 .tlv_type_partner_info = 0x02,
1834 .partner_information_length = 0x14,
1835 .tlv_type_collector_info = 0x03,
1836 .collector_information_length = 0x10,
1837 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1841 port->actor_port_priority = 0xff;
1842 port->actor_port_aggregator_identifier = 0;
1844 port->actor_admin_port_state = LACP_STATE_AGGREGATION |
1845 LACP_STATE_LACP_ACTIVITY;
1846 port->actor_oper_port_state = LACP_STATE_AGGREGATION |
1847 LACP_STATE_LACP_ACTIVITY;
1850 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
1852 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1853 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1855 port->is_enabled = true;
1856 /* private parameters */
1857 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1858 port->sm_rx_state = 0;
1859 port->sm_rx_timer_counter = 0;
1860 port->sm_periodic_state = 0;
1861 port->sm_periodic_timer_counter = 0;
1862 port->sm_mux_state = 0;
1863 port->sm_mux_timer_counter = 0;
1864 port->sm_tx_state = 0;
1865 port->aggregator = NULL;
1866 port->next_port_in_aggregator = NULL;
1867 port->transaction_id = 0;
1869 port->sm_churn_actor_timer_counter = 0;
1870 port->sm_churn_actor_state = 0;
1871 port->churn_actor_count = 0;
1872 port->sm_churn_partner_timer_counter = 0;
1873 port->sm_churn_partner_state = 0;
1874 port->churn_partner_count = 0;
1876 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1881 * ad_enable_collecting_distributing - enable a port's transmit/receive
1882 * @port: the port we're looking at
1883 * @update_slave_arr: Does slave array need update?
1885 * Enable @port if it's in an active aggregator
1887 static void ad_enable_collecting_distributing(struct port *port,
1888 bool *update_slave_arr)
1890 if (port->aggregator->is_active) {
1891 slave_dbg(port->slave->bond->dev, port->slave->dev,
1892 "Enabling port %d (LAG %d)\n",
1893 port->actor_port_number,
1894 port->aggregator->aggregator_identifier);
1895 __enable_port(port);
1896 /* Slave array needs update */
1897 *update_slave_arr = true;
1902 * ad_disable_collecting_distributing - disable a port's transmit/receive
1903 * @port: the port we're looking at
1904 * @update_slave_arr: Does slave array need update?
1906 static void ad_disable_collecting_distributing(struct port *port,
1907 bool *update_slave_arr)
1909 if (port->aggregator &&
1910 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1911 &(null_mac_addr))) {
1912 slave_dbg(port->slave->bond->dev, port->slave->dev,
1913 "Disabling port %d (LAG %d)\n",
1914 port->actor_port_number,
1915 port->aggregator->aggregator_identifier);
1916 __disable_port(port);
1917 /* Slave array needs an update */
1918 *update_slave_arr = true;
1923 * ad_marker_info_received - handle receive of a Marker information frame
1924 * @marker_info: Marker info received
1925 * @port: the port we're looking at
1927 static void ad_marker_info_received(struct bond_marker *marker_info,
1930 struct bond_marker marker;
1932 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx);
1933 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_rx);
1935 /* copy the received marker data to the response marker */
1936 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1937 /* change the marker subtype to marker response */
1938 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1940 /* send the marker response */
1941 if (ad_marker_send(port, &marker) >= 0)
1942 slave_dbg(port->slave->bond->dev, port->slave->dev,
1943 "Sent Marker Response on port %d\n",
1944 port->actor_port_number);
1948 * ad_marker_response_received - handle receive of a marker response frame
1949 * @marker: marker PDU received
1950 * @port: the port we're looking at
1952 * This function does nothing since we decided not to implement send and handle
1953 * response for marker PDU's, in this stage, but only to respond to marker
1956 static void ad_marker_response_received(struct bond_marker *marker,
1959 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_resp_rx);
1960 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_resp_rx);
1962 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1965 /* ========= AD exported functions to the main bonding code ========= */
1967 /* Check aggregators status in team every T seconds */
1968 #define AD_AGGREGATOR_SELECTION_TIMER 8
1971 * bond_3ad_initiate_agg_selection - initate aggregator selection
1972 * @bond: bonding struct
1973 * @timeout: timeout value to set
1975 * Set the aggregation selection timer, to initiate an agg selection in
1976 * the very near future. Called during first initialization, and during
1977 * any down to up transitions of the bond.
1979 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1981 atomic_set(&BOND_AD_INFO(bond).agg_select_timer, timeout);
1985 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1986 * @bond: bonding struct to work on
1987 * @tick_resolution: tick duration (millisecond resolution)
1989 * Can be called only after the mac address of the bond is set.
1991 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
1993 BOND_AD_INFO(bond).aggregator_identifier = 0;
1994 BOND_AD_INFO(bond).system.sys_priority =
1995 bond->params.ad_actor_sys_prio;
1996 if (is_zero_ether_addr(bond->params.ad_actor_system))
1997 BOND_AD_INFO(bond).system.sys_mac_addr =
1998 *((struct mac_addr *)bond->dev->dev_addr);
2000 BOND_AD_INFO(bond).system.sys_mac_addr =
2001 *((struct mac_addr *)bond->params.ad_actor_system);
2003 /* initialize how many times this module is called in one
2004 * second (should be about every 100ms)
2006 ad_ticks_per_sec = tick_resolution;
2008 bond_3ad_initiate_agg_selection(bond,
2009 AD_AGGREGATOR_SELECTION_TIMER *
2014 * bond_3ad_bind_slave - initialize a slave's port
2015 * @slave: slave struct to work on
2017 * Returns: 0 on success
2020 void bond_3ad_bind_slave(struct slave *slave)
2022 struct bonding *bond = bond_get_bond_by_slave(slave);
2024 struct aggregator *aggregator;
2026 /* check that the slave has not been initialized yet. */
2027 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2029 /* port initialization */
2030 port = &(SLAVE_AD_INFO(slave)->port);
2032 ad_initialize_port(port, bond->params.lacp_fast);
2034 port->slave = slave;
2035 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2036 /* key is determined according to the link speed, duplex and
2039 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2040 ad_update_actor_keys(port, false);
2041 /* actor system is the bond's system */
2042 __ad_actor_update_port(port);
2043 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2044 * lacpdu's are sent in one second)
2046 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2048 __disable_port(port);
2050 /* aggregator initialization */
2051 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2053 ad_initialize_agg(aggregator);
2055 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2056 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2057 aggregator->slave = slave;
2058 aggregator->is_active = 0;
2059 aggregator->num_of_ports = 0;
2064 * bond_3ad_unbind_slave - deinitialize a slave's port
2065 * @slave: slave struct to work on
2067 * Search for the aggregator that is related to this port, remove the
2068 * aggregator and assign another aggregator for other port related to it
2069 * (if any), and remove the port.
2071 void bond_3ad_unbind_slave(struct slave *slave)
2073 struct port *port, *prev_port, *temp_port;
2074 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2075 int select_new_active_agg = 0;
2076 struct bonding *bond = slave->bond;
2077 struct slave *slave_iter;
2078 struct list_head *iter;
2079 bool dummy_slave_update; /* Ignore this value as caller updates array */
2081 /* Sync against bond_3ad_state_machine_handler() */
2082 spin_lock_bh(&bond->mode_lock);
2083 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2084 port = &(SLAVE_AD_INFO(slave)->port);
2086 /* if slave is null, the whole port is not initialized */
2088 slave_warn(bond->dev, slave->dev, "Trying to unbind an uninitialized port\n");
2092 slave_dbg(bond->dev, slave->dev, "Unbinding Link Aggregation Group %d\n",
2093 aggregator->aggregator_identifier);
2095 /* Tell the partner that this port is not suitable for aggregation */
2096 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
2097 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
2098 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
2099 port->actor_oper_port_state &= ~LACP_STATE_AGGREGATION;
2100 __update_lacpdu_from_port(port);
2101 ad_lacpdu_send(port);
2103 /* check if this aggregator is occupied */
2104 if (aggregator->lag_ports) {
2105 /* check if there are other ports related to this aggregator
2106 * except the port related to this slave(thats ensure us that
2107 * there is a reason to search for new aggregator, and that we
2110 if ((aggregator->lag_ports != port) ||
2111 (aggregator->lag_ports->next_port_in_aggregator)) {
2112 /* find new aggregator for the related port(s) */
2113 bond_for_each_slave(bond, slave_iter, iter) {
2114 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2115 /* if the new aggregator is empty, or it is
2116 * connected to our port only
2118 if (!new_aggregator->lag_ports ||
2119 ((new_aggregator->lag_ports == port) &&
2120 !new_aggregator->lag_ports->next_port_in_aggregator))
2124 new_aggregator = NULL;
2126 /* if new aggregator found, copy the aggregator's
2127 * parameters and connect the related lag_ports to the
2130 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2131 slave_dbg(bond->dev, slave->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2132 aggregator->aggregator_identifier,
2133 new_aggregator->aggregator_identifier);
2135 if ((new_aggregator->lag_ports == port) &&
2136 new_aggregator->is_active) {
2137 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2138 select_new_active_agg = 1;
2141 new_aggregator->is_individual = aggregator->is_individual;
2142 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2143 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2144 new_aggregator->partner_system = aggregator->partner_system;
2145 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2146 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2147 new_aggregator->receive_state = aggregator->receive_state;
2148 new_aggregator->transmit_state = aggregator->transmit_state;
2149 new_aggregator->lag_ports = aggregator->lag_ports;
2150 new_aggregator->is_active = aggregator->is_active;
2151 new_aggregator->num_of_ports = aggregator->num_of_ports;
2153 /* update the information that is written on
2154 * the ports about the aggregator
2156 for (temp_port = aggregator->lag_ports; temp_port;
2157 temp_port = temp_port->next_port_in_aggregator) {
2158 temp_port->aggregator = new_aggregator;
2159 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2162 ad_clear_agg(aggregator);
2164 if (select_new_active_agg)
2165 ad_agg_selection_logic(__get_first_agg(port),
2166 &dummy_slave_update);
2168 slave_warn(bond->dev, slave->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2171 /* in case that the only port related to this
2172 * aggregator is the one we want to remove
2174 select_new_active_agg = aggregator->is_active;
2175 ad_clear_agg(aggregator);
2176 if (select_new_active_agg) {
2177 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2178 /* select new active aggregator */
2179 temp_aggregator = __get_first_agg(port);
2180 if (temp_aggregator)
2181 ad_agg_selection_logic(temp_aggregator,
2182 &dummy_slave_update);
2187 slave_dbg(bond->dev, slave->dev, "Unbinding port %d\n", port->actor_port_number);
2189 /* find the aggregator that this port is connected to */
2190 bond_for_each_slave(bond, slave_iter, iter) {
2191 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2193 /* search the port in the aggregator's related ports */
2194 for (temp_port = temp_aggregator->lag_ports; temp_port;
2195 prev_port = temp_port,
2196 temp_port = temp_port->next_port_in_aggregator) {
2197 if (temp_port == port) {
2198 /* the aggregator found - detach the port from
2202 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2204 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2205 temp_aggregator->num_of_ports--;
2206 if (__agg_active_ports(temp_aggregator) == 0) {
2207 select_new_active_agg = temp_aggregator->is_active;
2208 if (temp_aggregator->num_of_ports == 0)
2209 ad_clear_agg(temp_aggregator);
2210 if (select_new_active_agg) {
2211 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2212 /* select new active aggregator */
2213 ad_agg_selection_logic(__get_first_agg(port),
2214 &dummy_slave_update);
2224 spin_unlock_bh(&bond->mode_lock);
2228 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2229 * @bond: bonding struct to work on
2231 * If an ad_actor setting gets changed we need to update the individual port
2232 * settings so the bond device will use the new values when it gets upped.
2234 void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2236 struct list_head *iter;
2237 struct slave *slave;
2241 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2242 if (is_zero_ether_addr(bond->params.ad_actor_system))
2243 BOND_AD_INFO(bond).system.sys_mac_addr =
2244 *((struct mac_addr *)bond->dev->dev_addr);
2246 BOND_AD_INFO(bond).system.sys_mac_addr =
2247 *((struct mac_addr *)bond->params.ad_actor_system);
2249 spin_lock_bh(&bond->mode_lock);
2250 bond_for_each_slave(bond, slave, iter) {
2251 struct port *port = &(SLAVE_AD_INFO(slave))->port;
2253 __ad_actor_update_port(port);
2256 spin_unlock_bh(&bond->mode_lock);
2260 * bond_agg_timer_advance - advance agg_select_timer
2261 * @bond: bonding structure
2263 * Return true when agg_select_timer reaches 0.
2265 static bool bond_agg_timer_advance(struct bonding *bond)
2270 val = atomic_read(&BOND_AD_INFO(bond).agg_select_timer);
2274 if (atomic_cmpxchg(&BOND_AD_INFO(bond).agg_select_timer,
2282 * bond_3ad_state_machine_handler - handle state machines timeout
2283 * @work: work context to fetch bonding struct to work on from
2285 * The state machine handling concept in this module is to check every tick
2286 * which state machine should operate any function. The execution order is
2287 * round robin, so when we have an interaction between state machines, the
2288 * reply of one to each other might be delayed until next tick.
2290 * This function also complete the initialization when the agg_select_timer
2291 * times out, and it selects an aggregator for the ports that are yet not
2292 * related to any aggregator, and selects the active aggregator for a bond.
2294 void bond_3ad_state_machine_handler(struct work_struct *work)
2296 struct bonding *bond = container_of(work, struct bonding,
2298 struct aggregator *aggregator;
2299 struct list_head *iter;
2300 struct slave *slave;
2302 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2303 bool update_slave_arr = false;
2305 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2306 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2307 * concurrently due to incoming LACPDU as well.
2309 spin_lock_bh(&bond->mode_lock);
2312 /* check if there are any slaves */
2313 if (!bond_has_slaves(bond))
2316 if (bond_agg_timer_advance(bond)) {
2317 slave = bond_first_slave_rcu(bond);
2318 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2320 /* select the active aggregator for the bond */
2323 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2328 aggregator = __get_first_agg(port);
2329 ad_agg_selection_logic(aggregator, &update_slave_arr);
2331 bond_3ad_set_carrier(bond);
2334 /* for each port run the state machines */
2335 bond_for_each_slave_rcu(bond, slave, iter) {
2336 port = &(SLAVE_AD_INFO(slave)->port);
2338 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2343 ad_rx_machine(NULL, port);
2344 ad_periodic_machine(port);
2345 ad_port_selection_logic(port, &update_slave_arr);
2346 ad_mux_machine(port, &update_slave_arr);
2347 ad_tx_machine(port);
2348 ad_churn_machine(port);
2350 /* turn off the BEGIN bit, since we already handled it */
2351 if (port->sm_vars & AD_PORT_BEGIN)
2352 port->sm_vars &= ~AD_PORT_BEGIN;
2356 bond_for_each_slave_rcu(bond, slave, iter) {
2357 if (slave->should_notify) {
2358 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2363 spin_unlock_bh(&bond->mode_lock);
2365 if (update_slave_arr)
2366 bond_slave_arr_work_rearm(bond, 0);
2368 if (should_notify_rtnl && rtnl_trylock()) {
2369 bond_slave_state_notify(bond);
2372 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2376 * bond_3ad_rx_indication - handle a received frame
2377 * @lacpdu: received lacpdu
2378 * @slave: slave struct to work on
2380 * It is assumed that frames that were sent on this NIC don't returned as new
2381 * received frames (loopback). Since only the payload is given to this
2382 * function, it check for loopback.
2384 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave)
2386 struct bonding *bond = slave->bond;
2387 int ret = RX_HANDLER_ANOTHER;
2388 struct bond_marker *marker;
2392 port = &(SLAVE_AD_INFO(slave)->port);
2394 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2395 slave->dev->name, slave->bond->dev->name);
2399 switch (lacpdu->subtype) {
2400 case AD_TYPE_LACPDU:
2401 ret = RX_HANDLER_CONSUMED;
2402 slave_dbg(slave->bond->dev, slave->dev,
2403 "Received LACPDU on port %d\n",
2404 port->actor_port_number);
2405 /* Protect against concurrent state machines */
2406 spin_lock(&slave->bond->mode_lock);
2407 ad_rx_machine(lacpdu, port);
2408 spin_unlock(&slave->bond->mode_lock);
2410 case AD_TYPE_MARKER:
2411 ret = RX_HANDLER_CONSUMED;
2412 /* No need to convert fields to Little Endian since we
2413 * don't use the marker's fields.
2415 marker = (struct bond_marker *)lacpdu;
2416 switch (marker->tlv_type) {
2417 case AD_MARKER_INFORMATION_SUBTYPE:
2418 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Information on port %d\n",
2419 port->actor_port_number);
2420 ad_marker_info_received(marker, port);
2422 case AD_MARKER_RESPONSE_SUBTYPE:
2423 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Response on port %d\n",
2424 port->actor_port_number);
2425 ad_marker_response_received(marker, port);
2428 slave_dbg(slave->bond->dev, slave->dev, "Received an unknown Marker subtype on port %d\n",
2429 port->actor_port_number);
2430 stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx;
2432 stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx;
2437 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx);
2438 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx);
2445 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2446 * its current speed and duplex settings.
2448 * @port: the port we'are looking at
2449 * @reset: Boolean to just reset the speed and the duplex part of the key
2451 * The logic to change the oper / admin keys is:
2452 * (a) A full duplex port can participate in LACP with partner.
2453 * (b) When the speed is changed, LACP need to be reinitiated.
2455 static void ad_update_actor_keys(struct port *port, bool reset)
2458 u16 ospeed = 0, speed = 0;
2459 u16 old_oper_key = port->actor_oper_port_key;
2461 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2463 speed = __get_link_speed(port);
2464 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2465 duplex = __get_duplex(port);
2466 port->actor_admin_port_key |= (speed << 1) | duplex;
2468 port->actor_oper_port_key = port->actor_admin_port_key;
2470 if (old_oper_key != port->actor_oper_port_key) {
2471 /* Only 'duplex' port participates in LACP */
2473 port->sm_vars |= AD_PORT_LACP_ENABLED;
2475 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2479 slave_err(port->slave->bond->dev,
2481 "speed changed to 0 on port %d\n",
2482 port->actor_port_number);
2483 } else if (duplex && ospeed != speed) {
2484 /* Speed change restarts LACP state-machine */
2485 port->sm_vars |= AD_PORT_BEGIN;
2492 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2495 * @slave: slave struct to work on
2497 * Handle reselection of aggregator (if needed) for this port.
2499 void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2503 port = &(SLAVE_AD_INFO(slave)->port);
2505 /* if slave is null, the whole port is not initialized */
2507 slave_warn(slave->bond->dev, slave->dev,
2508 "speed/duplex changed for uninitialized port\n");
2512 spin_lock_bh(&slave->bond->mode_lock);
2513 ad_update_actor_keys(port, false);
2514 spin_unlock_bh(&slave->bond->mode_lock);
2515 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed speed/duplex\n",
2516 port->actor_port_number);
2520 * bond_3ad_handle_link_change - handle a slave's link status change indication
2521 * @slave: slave struct to work on
2522 * @link: whether the link is now up or down
2524 * Handle reselection of aggregator (if needed) for this port.
2526 void bond_3ad_handle_link_change(struct slave *slave, char link)
2528 struct aggregator *agg;
2532 port = &(SLAVE_AD_INFO(slave)->port);
2534 /* if slave is null, the whole port is not initialized */
2536 slave_warn(slave->bond->dev, slave->dev, "link status changed for uninitialized port\n");
2540 spin_lock_bh(&slave->bond->mode_lock);
2541 /* on link down we are zeroing duplex and speed since
2542 * some of the adaptors(ce1000.lan) report full duplex/speed
2543 * instead of N/A(duplex) / 0(speed).
2545 * on link up we are forcing recheck on the duplex and speed since
2546 * some of he adaptors(ce1000.lan) report.
2548 if (link == BOND_LINK_UP) {
2549 port->is_enabled = true;
2550 ad_update_actor_keys(port, false);
2552 /* link has failed */
2553 port->is_enabled = false;
2554 ad_update_actor_keys(port, true);
2556 agg = __get_first_agg(port);
2557 ad_agg_selection_logic(agg, &dummy);
2559 spin_unlock_bh(&slave->bond->mode_lock);
2561 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed link status to %s\n",
2562 port->actor_port_number,
2563 link == BOND_LINK_UP ? "UP" : "DOWN");
2565 /* RTNL is held and mode_lock is released so it's safe
2566 * to update slave_array here.
2568 bond_update_slave_arr(slave->bond, NULL);
2572 * bond_3ad_set_carrier - set link state for bonding master
2573 * @bond: bonding structure
2575 * if we have an active aggregator, we're up, if not, we're down.
2576 * Presumes that we cannot have an active aggregator if there are
2577 * no slaves with link up.
2579 * This behavior complies with IEEE 802.3 section 43.3.9.
2581 * Called by bond_set_carrier(). Return zero if carrier state does not
2582 * change, nonzero if it does.
2584 int bond_3ad_set_carrier(struct bonding *bond)
2586 struct aggregator *active;
2587 struct slave *first_slave;
2591 first_slave = bond_first_slave_rcu(bond);
2596 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2598 /* are enough slaves available to consider link up? */
2599 if (__agg_active_ports(active) < bond->params.min_links) {
2600 if (netif_carrier_ok(bond->dev)) {
2601 netif_carrier_off(bond->dev);
2604 } else if (!netif_carrier_ok(bond->dev)) {
2605 netif_carrier_on(bond->dev);
2608 } else if (netif_carrier_ok(bond->dev)) {
2609 netif_carrier_off(bond->dev);
2617 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2618 * @bond: bonding struct to work on
2619 * @ad_info: ad_info struct to fill with the bond's info
2621 * Returns: 0 on success
2624 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2625 struct ad_info *ad_info)
2627 struct aggregator *aggregator = NULL;
2628 struct list_head *iter;
2629 struct slave *slave;
2632 bond_for_each_slave_rcu(bond, slave, iter) {
2633 port = &(SLAVE_AD_INFO(slave)->port);
2634 if (port->aggregator && port->aggregator->is_active) {
2635 aggregator = port->aggregator;
2643 ad_info->aggregator_id = aggregator->aggregator_identifier;
2644 ad_info->ports = __agg_active_ports(aggregator);
2645 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2646 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2647 ether_addr_copy(ad_info->partner_system,
2648 aggregator->partner_system.mac_addr_value);
2652 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2657 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2663 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2664 struct slave *slave)
2666 struct lacpdu *lacpdu, _lacpdu;
2668 if (skb->protocol != PKT_TYPE_LACPDU)
2669 return RX_HANDLER_ANOTHER;
2671 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2672 return RX_HANDLER_ANOTHER;
2674 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2676 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx);
2677 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx);
2678 return RX_HANDLER_ANOTHER;
2681 return bond_3ad_rx_indication(lacpdu, slave);
2685 * bond_3ad_update_lacp_rate - change the lacp rate
2686 * @bond: bonding struct
2688 * When modify lacp_rate parameter via sysfs,
2689 * update actor_oper_port_state of each port.
2691 * Hold bond->mode_lock,
2692 * so we can modify port->actor_oper_port_state,
2693 * no matter bond is up or down.
2695 void bond_3ad_update_lacp_rate(struct bonding *bond)
2697 struct port *port = NULL;
2698 struct list_head *iter;
2699 struct slave *slave;
2702 lacp_fast = bond->params.lacp_fast;
2703 spin_lock_bh(&bond->mode_lock);
2704 bond_for_each_slave(bond, slave, iter) {
2705 port = &(SLAVE_AD_INFO(slave)->port);
2707 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
2709 port->actor_oper_port_state &= ~LACP_STATE_LACP_TIMEOUT;
2711 spin_unlock_bh(&bond->mode_lock);
2714 size_t bond_3ad_stats_size(void)
2716 return nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_RX */
2717 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_TX */
2718 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_UNKNOWN_RX */
2719 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_ILLEGAL_RX */
2720 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RX */
2721 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_TX */
2722 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_RX */
2723 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_TX */
2724 nla_total_size_64bit(sizeof(u64)); /* BOND_3AD_STAT_MARKER_UNKNOWN_RX */
2727 int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats)
2731 val = atomic64_read(&stats->lacpdu_rx);
2732 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val,
2735 val = atomic64_read(&stats->lacpdu_tx);
2736 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val,
2739 val = atomic64_read(&stats->lacpdu_unknown_rx);
2740 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val,
2743 val = atomic64_read(&stats->lacpdu_illegal_rx);
2744 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val,
2748 val = atomic64_read(&stats->marker_rx);
2749 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val,
2752 val = atomic64_read(&stats->marker_tx);
2753 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val,
2756 val = atomic64_read(&stats->marker_resp_rx);
2757 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val,
2760 val = atomic64_read(&stats->marker_resp_tx);
2761 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val,
2764 val = atomic64_read(&stats->marker_unknown_rx);
2765 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val,