2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #include <linux/skbuff.h>
24 #include <linux/if_ether.h>
25 #include <linux/netdevice.h>
26 #include <linux/spinlock.h>
27 #include <linux/ethtool.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_bonding.h>
30 #include <linux/pkt_sched.h>
31 #include <net/net_namespace.h>
32 #include <net/bonding.h>
33 #include <net/bond_3ad.h>
34 #include <net/netlink.h>
36 /* General definitions */
37 #define AD_SHORT_TIMEOUT 1
38 #define AD_LONG_TIMEOUT 0
39 #define AD_STANDBY 0x2
40 #define AD_MAX_TX_IN_SECOND 3
41 #define AD_COLLECTOR_MAX_DELAY 0
43 /* Timer definitions (43.4.4 in the 802.3ad standard) */
44 #define AD_FAST_PERIODIC_TIME 1
45 #define AD_SLOW_PERIODIC_TIME 30
46 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
47 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
48 #define AD_CHURN_DETECTION_TIME 60
49 #define AD_AGGREGATE_WAIT_TIME 2
51 /* Port state definitions (43.4.2.2 in the 802.3ad standard) */
52 #define AD_STATE_LACP_ACTIVITY 0x1
53 #define AD_STATE_LACP_TIMEOUT 0x2
54 #define AD_STATE_AGGREGATION 0x4
55 #define AD_STATE_SYNCHRONIZATION 0x8
56 #define AD_STATE_COLLECTING 0x10
57 #define AD_STATE_DISTRIBUTING 0x20
58 #define AD_STATE_DEFAULTED 0x40
59 #define AD_STATE_EXPIRED 0x80
61 /* Port Variables definitions used by the State Machines (43.4.7 in the
64 #define AD_PORT_BEGIN 0x1
65 #define AD_PORT_LACP_ENABLED 0x2
66 #define AD_PORT_ACTOR_CHURN 0x4
67 #define AD_PORT_PARTNER_CHURN 0x8
68 #define AD_PORT_READY 0x10
69 #define AD_PORT_READY_N 0x20
70 #define AD_PORT_MATCHED 0x40
71 #define AD_PORT_STANDBY 0x80
72 #define AD_PORT_SELECTED 0x100
73 #define AD_PORT_MOVED 0x200
74 #define AD_PORT_CHURNED (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
76 /* Port Key definitions
77 * key is determined according to the link speed, duplex and
78 * user key (which is yet not supported)
79 * --------------------------------------------------------------
80 * Port key | User key (10 bits) | Speed (5 bits) | Duplex|
81 * --------------------------------------------------------------
84 #define AD_DUPLEX_KEY_MASKS 0x1
85 #define AD_SPEED_KEY_MASKS 0x3E
86 #define AD_USER_KEY_MASKS 0xFFC0
88 enum ad_link_speed_type {
89 AD_LINK_SPEED_1MBPS = 1,
91 AD_LINK_SPEED_100MBPS,
92 AD_LINK_SPEED_1000MBPS,
93 AD_LINK_SPEED_2500MBPS,
94 AD_LINK_SPEED_5000MBPS,
95 AD_LINK_SPEED_10000MBPS,
96 AD_LINK_SPEED_14000MBPS,
97 AD_LINK_SPEED_20000MBPS,
98 AD_LINK_SPEED_25000MBPS,
99 AD_LINK_SPEED_40000MBPS,
100 AD_LINK_SPEED_50000MBPS,
101 AD_LINK_SPEED_56000MBPS,
102 AD_LINK_SPEED_100000MBPS,
105 /* compare MAC addresses */
106 #define MAC_ADDRESS_EQUAL(A, B) \
107 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
109 static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
112 static u16 ad_ticks_per_sec;
113 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
115 static const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned =
116 MULTICAST_LACPDU_ADDR;
118 /* ================= main 802.3ad protocol functions ================== */
119 static int ad_lacpdu_send(struct port *port);
120 static int ad_marker_send(struct port *port, struct bond_marker *marker);
121 static void ad_mux_machine(struct port *port, bool *update_slave_arr);
122 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
123 static void ad_tx_machine(struct port *port);
124 static void ad_periodic_machine(struct port *port);
125 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
126 static void ad_agg_selection_logic(struct aggregator *aggregator,
127 bool *update_slave_arr);
128 static void ad_clear_agg(struct aggregator *aggregator);
129 static void ad_initialize_agg(struct aggregator *aggregator);
130 static void ad_initialize_port(struct port *port, int lacp_fast);
131 static void ad_enable_collecting_distributing(struct port *port,
132 bool *update_slave_arr);
133 static void ad_disable_collecting_distributing(struct port *port,
134 bool *update_slave_arr);
135 static void ad_marker_info_received(struct bond_marker *marker_info,
137 static void ad_marker_response_received(struct bond_marker *marker,
139 static void ad_update_actor_keys(struct port *port, bool reset);
142 /* ================= api to bonding and kernel code ================== */
145 * __get_bond_by_port - get the port's bonding struct
146 * @port: the port we're looking at
148 * Return @port's bonding struct, or %NULL if it can't be found.
150 static inline struct bonding *__get_bond_by_port(struct port *port)
152 if (port->slave == NULL)
155 return bond_get_bond_by_slave(port->slave);
159 * __get_first_agg - get the first aggregator in the bond
160 * @bond: the bond we're looking at
162 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
164 * The caller must hold RCU or RTNL lock.
166 static inline struct aggregator *__get_first_agg(struct port *port)
168 struct bonding *bond = __get_bond_by_port(port);
169 struct slave *first_slave;
170 struct aggregator *agg;
172 /* If there's no bond for this port, or bond has no slaves */
177 first_slave = bond_first_slave_rcu(bond);
178 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
185 * __agg_has_partner - see if we have a partner
186 * @agg: the agregator we're looking at
188 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
189 * address for the partner). Return 0 if not.
191 static inline int __agg_has_partner(struct aggregator *agg)
193 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
197 * __disable_port - disable the port's slave
198 * @port: the port we're looking at
200 static inline void __disable_port(struct port *port)
202 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
206 * __enable_port - enable the port's slave, if it's up
207 * @port: the port we're looking at
209 static inline void __enable_port(struct port *port)
211 struct slave *slave = port->slave;
213 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
214 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
218 * __port_is_enabled - check if the port's slave is in active state
219 * @port: the port we're looking at
221 static inline int __port_is_enabled(struct port *port)
223 return bond_is_active_slave(port->slave);
227 * __get_agg_selection_mode - get the aggregator selection mode
228 * @port: the port we're looking at
230 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
232 static inline u32 __get_agg_selection_mode(struct port *port)
234 struct bonding *bond = __get_bond_by_port(port);
237 return BOND_AD_STABLE;
239 return bond->params.ad_select;
243 * __check_agg_selection_timer - check if the selection timer has expired
244 * @port: the port we're looking at
246 static inline int __check_agg_selection_timer(struct port *port)
248 struct bonding *bond = __get_bond_by_port(port);
253 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
257 * __get_link_speed - get a port's speed
258 * @port: the port we're looking at
260 * Return @port's speed in 802.3ad enum format. i.e. one of:
262 * %AD_LINK_SPEED_10MBPS,
263 * %AD_LINK_SPEED_100MBPS,
264 * %AD_LINK_SPEED_1000MBPS,
265 * %AD_LINK_SPEED_2500MBPS,
266 * %AD_LINK_SPEED_5000MBPS,
267 * %AD_LINK_SPEED_10000MBPS
268 * %AD_LINK_SPEED_14000MBPS,
269 * %AD_LINK_SPEED_20000MBPS
270 * %AD_LINK_SPEED_25000MBPS
271 * %AD_LINK_SPEED_40000MBPS
272 * %AD_LINK_SPEED_50000MBPS
273 * %AD_LINK_SPEED_56000MBPS
274 * %AD_LINK_SPEED_100000MBPS
276 static u16 __get_link_speed(struct port *port)
278 struct slave *slave = port->slave;
281 /* this if covers only a special case: when the configuration starts
282 * with link down, it sets the speed to 0.
283 * This is done in spite of the fact that the e100 driver reports 0
284 * to be compatible with MVT in the future.
286 if (slave->link != BOND_LINK_UP)
289 switch (slave->speed) {
291 speed = AD_LINK_SPEED_10MBPS;
295 speed = AD_LINK_SPEED_100MBPS;
299 speed = AD_LINK_SPEED_1000MBPS;
303 speed = AD_LINK_SPEED_2500MBPS;
307 speed = AD_LINK_SPEED_5000MBPS;
311 speed = AD_LINK_SPEED_10000MBPS;
315 speed = AD_LINK_SPEED_14000MBPS;
319 speed = AD_LINK_SPEED_20000MBPS;
323 speed = AD_LINK_SPEED_25000MBPS;
327 speed = AD_LINK_SPEED_40000MBPS;
331 speed = AD_LINK_SPEED_50000MBPS;
335 speed = AD_LINK_SPEED_56000MBPS;
339 speed = AD_LINK_SPEED_100000MBPS;
343 /* unknown speed value from ethtool. shouldn't happen */
344 if (slave->speed != SPEED_UNKNOWN)
345 pr_warn_once("%s: unknown ethtool speed (%d) for port %d (set it to 0)\n",
346 slave->bond->dev->name,
348 port->actor_port_number);
354 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
355 port->actor_port_number, speed);
360 * __get_duplex - get a port's duplex
361 * @port: the port we're looking at
363 * Return @port's duplex in 802.3ad bitmask format. i.e.:
364 * 0x01 if in full duplex
367 static u8 __get_duplex(struct port *port)
369 struct slave *slave = port->slave;
372 /* handling a special case: when the configuration starts with
373 * link down, it sets the duplex to 0.
375 if (slave->link == BOND_LINK_UP) {
376 switch (slave->duplex) {
379 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
380 port->actor_port_number);
385 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
386 port->actor_port_number);
393 static void __ad_actor_update_port(struct port *port)
395 const struct bonding *bond = bond_get_bond_by_slave(port->slave);
397 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
398 port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority;
404 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
405 * @timer_type: which timer to operate
406 * @par: timer parameter. see below
408 * If @timer_type is %current_while_timer, @par indicates long/short timer.
409 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
410 * %SLOW_PERIODIC_TIME.
412 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
414 u16 retval = 0; /* to silence the compiler */
416 switch (timer_type) {
417 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
419 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
421 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
423 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
424 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
426 case AD_PERIODIC_TIMER: /* for periodic machine */
427 retval = (par*ad_ticks_per_sec); /* long timeout */
429 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
430 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
432 case AD_WAIT_WHILE_TIMER: /* for selection machine */
433 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
441 /* ================= ad_rx_machine helper functions ================== */
444 * __choose_matched - update a port's matched variable from a received lacpdu
445 * @lacpdu: the lacpdu we've received
446 * @port: the port we're looking at
448 * Update the value of the matched variable, using parameter values from a
449 * newly received lacpdu. Parameter values for the partner carried in the
450 * received PDU are compared with the corresponding operational parameter
451 * values for the actor. Matched is set to TRUE if all of these parameters
452 * match and the PDU parameter partner_state.aggregation has the same value as
453 * actor_oper_port_state.aggregation and lacp will actively maintain the link
454 * in the aggregation. Matched is also set to TRUE if the value of
455 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
456 * an individual link and lacp will actively maintain the link. Otherwise,
457 * matched is set to FALSE. LACP is considered to be actively maintaining the
458 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
459 * the actor's actor_oper_port_state.lacp_activity and the PDU's
460 * partner_state.lacp_activity variables are TRUE.
462 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
463 * used here to implement the language from 802.3ad 43.4.9 that requires
464 * recordPDU to "match" the LACPDU parameters to the stored values.
466 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
468 /* check if all parameters are alike
469 * or this is individual link(aggregation == FALSE)
470 * then update the state machine Matched variable.
472 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
473 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
474 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
475 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
476 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
477 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
478 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
480 port->sm_vars |= AD_PORT_MATCHED;
482 port->sm_vars &= ~AD_PORT_MATCHED;
487 * __record_pdu - record parameters from a received lacpdu
488 * @lacpdu: the lacpdu we've received
489 * @port: the port we're looking at
491 * Record the parameter values for the Actor carried in a received lacpdu as
492 * the current partner operational parameter values and sets
493 * actor_oper_port_state.defaulted to FALSE.
495 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
497 if (lacpdu && port) {
498 struct port_params *partner = &port->partner_oper;
500 __choose_matched(lacpdu, port);
501 /* record the new parameter values for the partner
504 partner->port_number = ntohs(lacpdu->actor_port);
505 partner->port_priority = ntohs(lacpdu->actor_port_priority);
506 partner->system = lacpdu->actor_system;
507 partner->system_priority = ntohs(lacpdu->actor_system_priority);
508 partner->key = ntohs(lacpdu->actor_key);
509 partner->port_state = lacpdu->actor_state;
511 /* set actor_oper_port_state.defaulted to FALSE */
512 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
514 /* set the partner sync. to on if the partner is sync,
515 * and the port is matched
517 if ((port->sm_vars & AD_PORT_MATCHED) &&
518 (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
519 partner->port_state |= AD_STATE_SYNCHRONIZATION;
520 pr_debug("%s partner sync=1\n", port->slave->dev->name);
522 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
523 pr_debug("%s partner sync=0\n", port->slave->dev->name);
529 * __record_default - record default parameters
530 * @port: the port we're looking at
532 * This function records the default parameter values for the partner carried
533 * in the Partner Admin parameters as the current partner operational parameter
534 * values and sets actor_oper_port_state.defaulted to TRUE.
536 static void __record_default(struct port *port)
539 /* record the partner admin parameters */
540 memcpy(&port->partner_oper, &port->partner_admin,
541 sizeof(struct port_params));
543 /* set actor_oper_port_state.defaulted to true */
544 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
549 * __update_selected - update a port's Selected variable from a received lacpdu
550 * @lacpdu: the lacpdu we've received
551 * @port: the port we're looking at
553 * Update the value of the selected variable, using parameter values from a
554 * newly received lacpdu. The parameter values for the Actor carried in the
555 * received PDU are compared with the corresponding operational parameter
556 * values for the ports partner. If one or more of the comparisons shows that
557 * the value(s) received in the PDU differ from the current operational values,
558 * then selected is set to FALSE and actor_oper_port_state.synchronization is
559 * set to out_of_sync. Otherwise, selected remains unchanged.
561 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
563 if (lacpdu && port) {
564 const struct port_params *partner = &port->partner_oper;
566 /* check if any parameter is different then
567 * update the state machine selected variable.
569 if (ntohs(lacpdu->actor_port) != partner->port_number ||
570 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
571 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
572 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
573 ntohs(lacpdu->actor_key) != partner->key ||
574 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
575 port->sm_vars &= ~AD_PORT_SELECTED;
581 * __update_default_selected - update a port's Selected variable from Partner
582 * @port: the port we're looking at
584 * This function updates the value of the selected variable, using the partner
585 * administrative parameter values. The administrative values are compared with
586 * the corresponding operational parameter values for the partner. If one or
587 * more of the comparisons shows that the administrative value(s) differ from
588 * the current operational values, then Selected is set to FALSE and
589 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
590 * Selected remains unchanged.
592 static void __update_default_selected(struct port *port)
595 const struct port_params *admin = &port->partner_admin;
596 const struct port_params *oper = &port->partner_oper;
598 /* check if any parameter is different then
599 * update the state machine selected variable.
601 if (admin->port_number != oper->port_number ||
602 admin->port_priority != oper->port_priority ||
603 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
604 admin->system_priority != oper->system_priority ||
605 admin->key != oper->key ||
606 (admin->port_state & AD_STATE_AGGREGATION)
607 != (oper->port_state & AD_STATE_AGGREGATION)) {
608 port->sm_vars &= ~AD_PORT_SELECTED;
614 * __update_ntt - update a port's ntt variable from a received lacpdu
615 * @lacpdu: the lacpdu we've received
616 * @port: the port we're looking at
618 * Updates the value of the ntt variable, using parameter values from a newly
619 * received lacpdu. The parameter values for the partner carried in the
620 * received PDU are compared with the corresponding operational parameter
621 * values for the Actor. If one or more of the comparisons shows that the
622 * value(s) received in the PDU differ from the current operational values,
623 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
625 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
627 /* validate lacpdu and port */
628 if (lacpdu && port) {
629 /* check if any parameter is different then
630 * update the port->ntt.
632 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
633 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
634 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
635 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
636 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
637 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
638 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
639 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
640 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
648 * __agg_ports_are_ready - check if all ports in an aggregator are ready
649 * @aggregator: the aggregator we're looking at
652 static int __agg_ports_are_ready(struct aggregator *aggregator)
658 /* scan all ports in this aggregator to verfy if they are
661 for (port = aggregator->lag_ports;
663 port = port->next_port_in_aggregator) {
664 if (!(port->sm_vars & AD_PORT_READY_N)) {
675 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
676 * @aggregator: the aggregator we're looking at
677 * @val: Should the ports' ready bit be set on or off
680 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
684 for (port = aggregator->lag_ports; port;
685 port = port->next_port_in_aggregator) {
687 port->sm_vars |= AD_PORT_READY;
689 port->sm_vars &= ~AD_PORT_READY;
693 static int __agg_active_ports(struct aggregator *agg)
698 for (port = agg->lag_ports; port;
699 port = port->next_port_in_aggregator) {
700 if (port->is_enabled)
708 * __get_agg_bandwidth - get the total bandwidth of an aggregator
709 * @aggregator: the aggregator we're looking at
712 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
714 int nports = __agg_active_ports(aggregator);
718 switch (__get_link_speed(aggregator->lag_ports)) {
719 case AD_LINK_SPEED_1MBPS:
722 case AD_LINK_SPEED_10MBPS:
723 bandwidth = nports * 10;
725 case AD_LINK_SPEED_100MBPS:
726 bandwidth = nports * 100;
728 case AD_LINK_SPEED_1000MBPS:
729 bandwidth = nports * 1000;
731 case AD_LINK_SPEED_2500MBPS:
732 bandwidth = nports * 2500;
734 case AD_LINK_SPEED_5000MBPS:
735 bandwidth = nports * 5000;
737 case AD_LINK_SPEED_10000MBPS:
738 bandwidth = nports * 10000;
740 case AD_LINK_SPEED_14000MBPS:
741 bandwidth = nports * 14000;
743 case AD_LINK_SPEED_20000MBPS:
744 bandwidth = nports * 20000;
746 case AD_LINK_SPEED_25000MBPS:
747 bandwidth = nports * 25000;
749 case AD_LINK_SPEED_40000MBPS:
750 bandwidth = nports * 40000;
752 case AD_LINK_SPEED_50000MBPS:
753 bandwidth = nports * 50000;
755 case AD_LINK_SPEED_56000MBPS:
756 bandwidth = nports * 56000;
758 case AD_LINK_SPEED_100000MBPS:
759 bandwidth = nports * 100000;
762 bandwidth = 0; /* to silence the compiler */
769 * __get_active_agg - get the current active aggregator
770 * @aggregator: the aggregator we're looking at
772 * Caller must hold RCU lock.
774 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
776 struct bonding *bond = aggregator->slave->bond;
777 struct list_head *iter;
780 bond_for_each_slave_rcu(bond, slave, iter)
781 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
782 return &(SLAVE_AD_INFO(slave)->aggregator);
788 * __update_lacpdu_from_port - update a port's lacpdu fields
789 * @port: the port we're looking at
791 static inline void __update_lacpdu_from_port(struct port *port)
793 struct lacpdu *lacpdu = &port->lacpdu;
794 const struct port_params *partner = &port->partner_oper;
796 /* update current actual Actor parameters
797 * lacpdu->subtype initialized
798 * lacpdu->version_number initialized
799 * lacpdu->tlv_type_actor_info initialized
800 * lacpdu->actor_information_length initialized
803 lacpdu->actor_system_priority = htons(port->actor_system_priority);
804 lacpdu->actor_system = port->actor_system;
805 lacpdu->actor_key = htons(port->actor_oper_port_key);
806 lacpdu->actor_port_priority = htons(port->actor_port_priority);
807 lacpdu->actor_port = htons(port->actor_port_number);
808 lacpdu->actor_state = port->actor_oper_port_state;
809 pr_debug("update lacpdu: %s, actor port state %x\n",
810 port->slave->dev->name, port->actor_oper_port_state);
812 /* lacpdu->reserved_3_1 initialized
813 * lacpdu->tlv_type_partner_info initialized
814 * lacpdu->partner_information_length initialized
817 lacpdu->partner_system_priority = htons(partner->system_priority);
818 lacpdu->partner_system = partner->system;
819 lacpdu->partner_key = htons(partner->key);
820 lacpdu->partner_port_priority = htons(partner->port_priority);
821 lacpdu->partner_port = htons(partner->port_number);
822 lacpdu->partner_state = partner->port_state;
824 /* lacpdu->reserved_3_2 initialized
825 * lacpdu->tlv_type_collector_info initialized
826 * lacpdu->collector_information_length initialized
827 * collector_max_delay initialized
828 * reserved_12[12] initialized
829 * tlv_type_terminator initialized
830 * terminator_length initialized
831 * reserved_50[50] initialized
835 /* ================= main 802.3ad protocol code ========================= */
838 * ad_lacpdu_send - send out a lacpdu packet on a given port
839 * @port: the port we're looking at
841 * Returns: 0 on success
844 static int ad_lacpdu_send(struct port *port)
846 struct slave *slave = port->slave;
848 struct lacpdu_header *lacpdu_header;
849 int length = sizeof(struct lacpdu_header);
851 skb = dev_alloc_skb(length);
855 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_tx);
856 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.lacpdu_tx);
858 skb->dev = slave->dev;
859 skb_reset_mac_header(skb);
860 skb->network_header = skb->mac_header + ETH_HLEN;
861 skb->protocol = PKT_TYPE_LACPDU;
862 skb->priority = TC_PRIO_CONTROL;
864 lacpdu_header = skb_put(skb, length);
866 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
867 /* Note: source address is set to be the member's PERMANENT address,
868 * because we use it to identify loopback lacpdus in receive.
870 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
871 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
873 lacpdu_header->lacpdu = port->lacpdu;
881 * ad_marker_send - send marker information/response on a given port
882 * @port: the port we're looking at
883 * @marker: marker data to send
885 * Returns: 0 on success
888 static int ad_marker_send(struct port *port, struct bond_marker *marker)
890 struct slave *slave = port->slave;
892 struct bond_marker_header *marker_header;
893 int length = sizeof(struct bond_marker_header);
895 skb = dev_alloc_skb(length + 16);
899 switch (marker->tlv_type) {
900 case AD_MARKER_INFORMATION_SUBTYPE:
901 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_tx);
902 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_tx);
904 case AD_MARKER_RESPONSE_SUBTYPE:
905 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_resp_tx);
906 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_resp_tx);
910 skb_reserve(skb, 16);
912 skb->dev = slave->dev;
913 skb_reset_mac_header(skb);
914 skb->network_header = skb->mac_header + ETH_HLEN;
915 skb->protocol = PKT_TYPE_LACPDU;
917 marker_header = skb_put(skb, length);
919 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
920 /* Note: source address is set to be the member's PERMANENT address,
921 * because we use it to identify loopback MARKERs in receive.
923 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
924 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
926 marker_header->marker = *marker;
934 * ad_mux_machine - handle a port's mux state machine
935 * @port: the port we're looking at
936 * @update_slave_arr: Does slave array need update?
938 static void ad_mux_machine(struct port *port, bool *update_slave_arr)
940 mux_states_t last_state;
942 /* keep current State Machine state to compare later if it was
945 last_state = port->sm_mux_state;
947 if (port->sm_vars & AD_PORT_BEGIN) {
948 port->sm_mux_state = AD_MUX_DETACHED;
950 switch (port->sm_mux_state) {
951 case AD_MUX_DETACHED:
952 if ((port->sm_vars & AD_PORT_SELECTED)
953 || (port->sm_vars & AD_PORT_STANDBY))
954 /* if SELECTED or STANDBY */
955 port->sm_mux_state = AD_MUX_WAITING;
958 /* if SELECTED == FALSE return to DETACH state */
959 if (!(port->sm_vars & AD_PORT_SELECTED)) {
960 port->sm_vars &= ~AD_PORT_READY_N;
961 /* in order to withhold the Selection Logic to
962 * check all ports READY_N value every callback
963 * cycle to update ready variable, we check
964 * READY_N and update READY here
966 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
967 port->sm_mux_state = AD_MUX_DETACHED;
971 /* check if the wait_while_timer expired */
972 if (port->sm_mux_timer_counter
973 && !(--port->sm_mux_timer_counter))
974 port->sm_vars |= AD_PORT_READY_N;
976 /* in order to withhold the selection logic to check
977 * all ports READY_N value every callback cycle to
978 * update ready variable, we check READY_N and update
981 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
983 /* if the wait_while_timer expired, and the port is
984 * in READY state, move to ATTACHED state
986 if ((port->sm_vars & AD_PORT_READY)
987 && !port->sm_mux_timer_counter)
988 port->sm_mux_state = AD_MUX_ATTACHED;
990 case AD_MUX_ATTACHED:
991 /* check also if agg_select_timer expired (so the
992 * edable port will take place only after this timer)
994 if ((port->sm_vars & AD_PORT_SELECTED) &&
995 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
996 !__check_agg_selection_timer(port)) {
997 if (port->aggregator->is_active)
999 AD_MUX_COLLECTING_DISTRIBUTING;
1000 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
1001 (port->sm_vars & AD_PORT_STANDBY)) {
1002 /* if UNSELECTED or STANDBY */
1003 port->sm_vars &= ~AD_PORT_READY_N;
1004 /* in order to withhold the selection logic to
1005 * check all ports READY_N value every callback
1006 * cycle to update ready variable, we check
1007 * READY_N and update READY here
1009 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1010 port->sm_mux_state = AD_MUX_DETACHED;
1011 } else if (port->aggregator->is_active) {
1012 port->actor_oper_port_state |=
1013 AD_STATE_SYNCHRONIZATION;
1016 case AD_MUX_COLLECTING_DISTRIBUTING:
1017 if (!(port->sm_vars & AD_PORT_SELECTED) ||
1018 (port->sm_vars & AD_PORT_STANDBY) ||
1019 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) ||
1020 !(port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) {
1021 port->sm_mux_state = AD_MUX_ATTACHED;
1023 /* if port state hasn't changed make
1024 * sure that a collecting distributing
1025 * port in an active aggregator is enabled
1027 if (port->aggregator &&
1028 port->aggregator->is_active &&
1029 !__port_is_enabled(port)) {
1031 __enable_port(port);
1040 /* check if the state machine was changed */
1041 if (port->sm_mux_state != last_state) {
1042 pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1043 port->actor_port_number,
1044 port->slave->dev->name,
1046 port->sm_mux_state);
1047 switch (port->sm_mux_state) {
1048 case AD_MUX_DETACHED:
1049 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
1050 ad_disable_collecting_distributing(port,
1052 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1053 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1056 case AD_MUX_WAITING:
1057 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1059 case AD_MUX_ATTACHED:
1060 if (port->aggregator->is_active)
1061 port->actor_oper_port_state |=
1062 AD_STATE_SYNCHRONIZATION;
1064 port->actor_oper_port_state &=
1065 ~AD_STATE_SYNCHRONIZATION;
1066 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1067 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1068 ad_disable_collecting_distributing(port,
1072 case AD_MUX_COLLECTING_DISTRIBUTING:
1073 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1074 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
1075 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
1076 ad_enable_collecting_distributing(port,
1087 * ad_rx_machine - handle a port's rx State Machine
1088 * @lacpdu: the lacpdu we've received
1089 * @port: the port we're looking at
1091 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1092 * CURRENT. If timer expired set the state machine in the proper state.
1093 * In other cases, this function checks if we need to switch to other state.
1095 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1097 rx_states_t last_state;
1099 /* keep current State Machine state to compare later if it was
1102 last_state = port->sm_rx_state;
1105 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx);
1106 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.lacpdu_rx);
1108 /* check if state machine should change state */
1110 /* first, check if port was reinitialized */
1111 if (port->sm_vars & AD_PORT_BEGIN) {
1112 port->sm_rx_state = AD_RX_INITIALIZE;
1113 port->sm_vars |= AD_PORT_CHURNED;
1114 /* check if port is not enabled */
1115 } else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled)
1116 port->sm_rx_state = AD_RX_PORT_DISABLED;
1117 /* check if new lacpdu arrived */
1118 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1119 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1120 (port->sm_rx_state == AD_RX_CURRENT))) {
1121 if (port->sm_rx_state != AD_RX_CURRENT)
1122 port->sm_vars |= AD_PORT_CHURNED;
1123 port->sm_rx_timer_counter = 0;
1124 port->sm_rx_state = AD_RX_CURRENT;
1126 /* if timer is on, and if it is expired */
1127 if (port->sm_rx_timer_counter &&
1128 !(--port->sm_rx_timer_counter)) {
1129 switch (port->sm_rx_state) {
1131 port->sm_rx_state = AD_RX_DEFAULTED;
1134 port->sm_rx_state = AD_RX_EXPIRED;
1140 /* if no lacpdu arrived and no timer is on */
1141 switch (port->sm_rx_state) {
1142 case AD_RX_PORT_DISABLED:
1143 if (port->is_enabled &&
1144 (port->sm_vars & AD_PORT_LACP_ENABLED))
1145 port->sm_rx_state = AD_RX_EXPIRED;
1146 else if (port->is_enabled
1148 & AD_PORT_LACP_ENABLED) == 0))
1149 port->sm_rx_state = AD_RX_LACP_DISABLED;
1158 /* check if the State machine was changed or new lacpdu arrived */
1159 if ((port->sm_rx_state != last_state) || (lacpdu)) {
1160 pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1161 port->actor_port_number,
1162 port->slave->dev->name,
1165 switch (port->sm_rx_state) {
1166 case AD_RX_INITIALIZE:
1167 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
1168 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1170 port->sm_vars |= AD_PORT_LACP_ENABLED;
1171 port->sm_vars &= ~AD_PORT_SELECTED;
1172 __record_default(port);
1173 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1174 port->sm_rx_state = AD_RX_PORT_DISABLED;
1177 case AD_RX_PORT_DISABLED:
1178 port->sm_vars &= ~AD_PORT_MATCHED;
1180 case AD_RX_LACP_DISABLED:
1181 port->sm_vars &= ~AD_PORT_SELECTED;
1182 __record_default(port);
1183 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1184 port->sm_vars |= AD_PORT_MATCHED;
1185 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1188 /* Reset of the Synchronization flag (Standard 43.4.12)
1189 * This reset cause to disable this port in the
1190 * COLLECTING_DISTRIBUTING state of the mux machine in
1191 * case of EXPIRED even if LINK_DOWN didn't arrive for
1194 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1195 port->sm_vars &= ~AD_PORT_MATCHED;
1196 port->partner_oper.port_state |= AD_STATE_LACP_TIMEOUT;
1197 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
1198 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1199 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1200 port->sm_vars |= AD_PORT_CHURNED;
1202 case AD_RX_DEFAULTED:
1203 __update_default_selected(port);
1204 __record_default(port);
1205 port->sm_vars |= AD_PORT_MATCHED;
1206 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1209 /* detect loopback situation */
1210 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1211 &(port->actor_system))) {
1212 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
1213 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1214 port->slave->dev->name);
1217 __update_selected(lacpdu, port);
1218 __update_ntt(lacpdu, port);
1219 __record_pdu(lacpdu, port);
1220 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1221 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1230 * ad_churn_machine - handle port churn's state machine
1231 * @port: the port we're looking at
1234 static void ad_churn_machine(struct port *port)
1236 if (port->sm_vars & AD_PORT_CHURNED) {
1237 port->sm_vars &= ~AD_PORT_CHURNED;
1238 port->sm_churn_actor_state = AD_CHURN_MONITOR;
1239 port->sm_churn_partner_state = AD_CHURN_MONITOR;
1240 port->sm_churn_actor_timer_counter =
1241 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1242 port->sm_churn_partner_timer_counter =
1243 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1246 if (port->sm_churn_actor_timer_counter &&
1247 !(--port->sm_churn_actor_timer_counter) &&
1248 port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1249 if (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION) {
1250 port->sm_churn_actor_state = AD_NO_CHURN;
1252 port->churn_actor_count++;
1253 port->sm_churn_actor_state = AD_CHURN;
1256 if (port->sm_churn_partner_timer_counter &&
1257 !(--port->sm_churn_partner_timer_counter) &&
1258 port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1259 if (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) {
1260 port->sm_churn_partner_state = AD_NO_CHURN;
1262 port->churn_partner_count++;
1263 port->sm_churn_partner_state = AD_CHURN;
1269 * ad_tx_machine - handle a port's tx state machine
1270 * @port: the port we're looking at
1272 static void ad_tx_machine(struct port *port)
1274 /* check if tx timer expired, to verify that we do not send more than
1275 * 3 packets per second
1277 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1278 /* check if there is something to send */
1279 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1280 __update_lacpdu_from_port(port);
1282 if (ad_lacpdu_send(port) >= 0) {
1283 pr_debug("Sent LACPDU on port %d\n",
1284 port->actor_port_number);
1286 /* mark ntt as false, so it will not be sent
1287 * again until demanded
1292 /* restart tx timer(to verify that we will not exceed
1293 * AD_MAX_TX_IN_SECOND
1295 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1300 * ad_periodic_machine - handle a port's periodic state machine
1301 * @port: the port we're looking at
1303 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1305 static void ad_periodic_machine(struct port *port)
1307 periodic_states_t last_state;
1309 /* keep current state machine state to compare later if it was changed */
1310 last_state = port->sm_periodic_state;
1312 /* check if port was reinitialized */
1313 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1314 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1316 port->sm_periodic_state = AD_NO_PERIODIC;
1318 /* check if state machine should change state */
1319 else if (port->sm_periodic_timer_counter) {
1320 /* check if periodic state machine expired */
1321 if (!(--port->sm_periodic_timer_counter)) {
1322 /* if expired then do tx */
1323 port->sm_periodic_state = AD_PERIODIC_TX;
1325 /* If not expired, check if there is some new timeout
1326 * parameter from the partner state
1328 switch (port->sm_periodic_state) {
1329 case AD_FAST_PERIODIC:
1330 if (!(port->partner_oper.port_state
1331 & AD_STATE_LACP_TIMEOUT))
1332 port->sm_periodic_state = AD_SLOW_PERIODIC;
1334 case AD_SLOW_PERIODIC:
1335 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1336 port->sm_periodic_timer_counter = 0;
1337 port->sm_periodic_state = AD_PERIODIC_TX;
1345 switch (port->sm_periodic_state) {
1346 case AD_NO_PERIODIC:
1347 port->sm_periodic_state = AD_FAST_PERIODIC;
1349 case AD_PERIODIC_TX:
1350 if (!(port->partner_oper.port_state &
1351 AD_STATE_LACP_TIMEOUT))
1352 port->sm_periodic_state = AD_SLOW_PERIODIC;
1354 port->sm_periodic_state = AD_FAST_PERIODIC;
1361 /* check if the state machine was changed */
1362 if (port->sm_periodic_state != last_state) {
1363 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1364 port->actor_port_number, last_state,
1365 port->sm_periodic_state);
1366 switch (port->sm_periodic_state) {
1367 case AD_NO_PERIODIC:
1368 port->sm_periodic_timer_counter = 0;
1370 case AD_FAST_PERIODIC:
1371 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1372 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1374 case AD_SLOW_PERIODIC:
1375 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1376 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1378 case AD_PERIODIC_TX:
1388 * ad_port_selection_logic - select aggregation groups
1389 * @port: the port we're looking at
1390 * @update_slave_arr: Does slave array need update?
1392 * Select aggregation groups, and assign each port for it's aggregetor. The
1393 * selection logic is called in the inititalization (after all the handshkes),
1394 * and after every lacpdu receive (if selected is off).
1396 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
1398 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1399 struct port *last_port = NULL, *curr_port;
1400 struct list_head *iter;
1401 struct bonding *bond;
1402 struct slave *slave;
1405 /* if the port is already Selected, do nothing */
1406 if (port->sm_vars & AD_PORT_SELECTED)
1409 bond = __get_bond_by_port(port);
1411 /* if the port is connected to other aggregator, detach it */
1412 if (port->aggregator) {
1413 /* detach the port from its former aggregator */
1414 temp_aggregator = port->aggregator;
1415 for (curr_port = temp_aggregator->lag_ports; curr_port;
1416 last_port = curr_port,
1417 curr_port = curr_port->next_port_in_aggregator) {
1418 if (curr_port == port) {
1419 temp_aggregator->num_of_ports--;
1420 /* if it is the first port attached to the
1424 temp_aggregator->lag_ports =
1425 port->next_port_in_aggregator;
1427 /* not the first port attached to the
1430 last_port->next_port_in_aggregator =
1431 port->next_port_in_aggregator;
1434 /* clear the port's relations to this
1437 port->aggregator = NULL;
1438 port->next_port_in_aggregator = NULL;
1439 port->actor_port_aggregator_identifier = 0;
1441 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1442 port->actor_port_number,
1443 temp_aggregator->aggregator_identifier);
1444 /* if the aggregator is empty, clear its
1445 * parameters, and set it ready to be attached
1447 if (!temp_aggregator->lag_ports)
1448 ad_clear_agg(temp_aggregator);
1453 /* meaning: the port was related to an aggregator
1454 * but was not on the aggregator port list
1456 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1457 port->slave->bond->dev->name,
1458 port->actor_port_number,
1459 port->slave->dev->name,
1460 port->aggregator->aggregator_identifier);
1463 /* search on all aggregators for a suitable aggregator for this port */
1464 bond_for_each_slave(bond, slave, iter) {
1465 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1467 /* keep a free aggregator for later use(if needed) */
1468 if (!aggregator->lag_ports) {
1469 if (!free_aggregator)
1470 free_aggregator = aggregator;
1473 /* check if current aggregator suits us */
1474 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1475 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1476 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1477 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1479 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1480 !aggregator->is_individual) /* but is not individual OR */
1483 /* attach to the founded aggregator */
1484 port->aggregator = aggregator;
1485 port->actor_port_aggregator_identifier =
1486 port->aggregator->aggregator_identifier;
1487 port->next_port_in_aggregator = aggregator->lag_ports;
1488 port->aggregator->num_of_ports++;
1489 aggregator->lag_ports = port;
1490 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1491 port->actor_port_number,
1492 port->aggregator->aggregator_identifier);
1494 /* mark this port as selected */
1495 port->sm_vars |= AD_PORT_SELECTED;
1501 /* the port couldn't find an aggregator - attach it to a new
1505 if (free_aggregator) {
1506 /* assign port a new aggregator */
1507 port->aggregator = free_aggregator;
1508 port->actor_port_aggregator_identifier =
1509 port->aggregator->aggregator_identifier;
1511 /* update the new aggregator's parameters
1512 * if port was responsed from the end-user
1514 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
1515 /* if port is full duplex */
1516 port->aggregator->is_individual = false;
1518 port->aggregator->is_individual = true;
1520 port->aggregator->actor_admin_aggregator_key =
1521 port->actor_admin_port_key;
1522 port->aggregator->actor_oper_aggregator_key =
1523 port->actor_oper_port_key;
1524 port->aggregator->partner_system =
1525 port->partner_oper.system;
1526 port->aggregator->partner_system_priority =
1527 port->partner_oper.system_priority;
1528 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1529 port->aggregator->receive_state = 1;
1530 port->aggregator->transmit_state = 1;
1531 port->aggregator->lag_ports = port;
1532 port->aggregator->num_of_ports++;
1534 /* mark this port as selected */
1535 port->sm_vars |= AD_PORT_SELECTED;
1537 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1538 port->actor_port_number,
1539 port->aggregator->aggregator_identifier);
1541 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
1542 port->actor_port_number, port->slave->dev->name);
1545 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1546 * in all aggregator's ports, else set ready=FALSE in all
1547 * aggregator's ports
1549 __set_agg_ports_ready(port->aggregator,
1550 __agg_ports_are_ready(port->aggregator));
1552 aggregator = __get_first_agg(port);
1553 ad_agg_selection_logic(aggregator, update_slave_arr);
1555 if (!port->aggregator->is_active)
1556 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
1559 /* Decide if "agg" is a better choice for the new active aggregator that
1560 * the current best, according to the ad_select policy.
1562 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1563 struct aggregator *curr)
1565 /* 0. If no best, select current.
1567 * 1. If the current agg is not individual, and the best is
1568 * individual, select current.
1570 * 2. If current agg is individual and the best is not, keep best.
1572 * 3. Therefore, current and best are both individual or both not
1575 * 3a. If current agg partner replied, and best agg partner did not,
1578 * 3b. If current agg partner did not reply and best agg partner
1579 * did reply, keep best.
1581 * 4. Therefore, current and best both have partner replies or
1582 * both do not, so perform selection policy:
1584 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1585 * select by bandwidth.
1587 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1592 if (!curr->is_individual && best->is_individual)
1595 if (curr->is_individual && !best->is_individual)
1598 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1601 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1604 switch (__get_agg_selection_mode(curr->lag_ports)) {
1606 if (__agg_active_ports(curr) > __agg_active_ports(best))
1609 if (__agg_active_ports(curr) < __agg_active_ports(best))
1613 case BOND_AD_STABLE:
1614 case BOND_AD_BANDWIDTH:
1615 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1621 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1622 curr->slave->bond->dev->name,
1623 __get_agg_selection_mode(curr->lag_ports));
1630 static int agg_device_up(const struct aggregator *agg)
1632 struct port *port = agg->lag_ports;
1637 for (port = agg->lag_ports; port;
1638 port = port->next_port_in_aggregator) {
1639 if (netif_running(port->slave->dev) &&
1640 netif_carrier_ok(port->slave->dev))
1648 * ad_agg_selection_logic - select an aggregation group for a team
1649 * @aggregator: the aggregator we're looking at
1650 * @update_slave_arr: Does slave array need update?
1652 * It is assumed that only one aggregator may be selected for a team.
1654 * The logic of this function is to select the aggregator according to
1655 * the ad_select policy:
1657 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1658 * it, and to reselect the active aggregator only if the previous
1659 * aggregator has no more ports related to it.
1661 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1662 * bandwidth, and reselect whenever a link state change takes place or the
1663 * set of slaves in the bond changes.
1665 * BOND_AD_COUNT: select the aggregator with largest number of ports
1666 * (slaves), and reselect whenever a link state change takes place or the
1667 * set of slaves in the bond changes.
1669 * FIXME: this function MUST be called with the first agg in the bond, or
1670 * __get_active_agg() won't work correctly. This function should be better
1671 * called with the bond itself, and retrieve the first agg from it.
1673 static void ad_agg_selection_logic(struct aggregator *agg,
1674 bool *update_slave_arr)
1676 struct aggregator *best, *active, *origin;
1677 struct bonding *bond = agg->slave->bond;
1678 struct list_head *iter;
1679 struct slave *slave;
1684 active = __get_active_agg(agg);
1685 best = (active && agg_device_up(active)) ? active : NULL;
1687 bond_for_each_slave_rcu(bond, slave, iter) {
1688 agg = &(SLAVE_AD_INFO(slave)->aggregator);
1692 if (__agg_active_ports(agg) && agg_device_up(agg))
1693 best = ad_agg_selection_test(best, agg);
1697 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1698 /* For the STABLE policy, don't replace the old active
1699 * aggregator if it's still active (it has an answering
1700 * partner) or if both the best and active don't have an
1701 * answering partner.
1703 if (active && active->lag_ports &&
1704 __agg_active_ports(active) &&
1705 (__agg_has_partner(active) ||
1706 (!__agg_has_partner(active) &&
1707 !__agg_has_partner(best)))) {
1708 if (!(!active->actor_oper_aggregator_key &&
1709 best->actor_oper_aggregator_key)) {
1711 active->is_active = 1;
1716 if (best && (best == active)) {
1718 active->is_active = 1;
1721 /* if there is new best aggregator, activate it */
1723 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1724 best->aggregator_identifier, best->num_of_ports,
1725 best->actor_oper_aggregator_key,
1726 best->partner_oper_aggregator_key,
1727 best->is_individual, best->is_active);
1728 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1729 best->lag_ports, best->slave,
1730 best->slave ? best->slave->dev->name : "NULL");
1732 bond_for_each_slave_rcu(bond, slave, iter) {
1733 agg = &(SLAVE_AD_INFO(slave)->aggregator);
1735 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1736 agg->aggregator_identifier, agg->num_of_ports,
1737 agg->actor_oper_aggregator_key,
1738 agg->partner_oper_aggregator_key,
1739 agg->is_individual, agg->is_active);
1742 /* check if any partner replys */
1743 if (best->is_individual) {
1744 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1746 best->slave->bond->dev->name : "NULL");
1749 best->is_active = 1;
1750 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1751 best->aggregator_identifier);
1752 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1753 best->aggregator_identifier, best->num_of_ports,
1754 best->actor_oper_aggregator_key,
1755 best->partner_oper_aggregator_key,
1756 best->is_individual, best->is_active);
1758 /* disable the ports that were related to the former
1762 for (port = active->lag_ports; port;
1763 port = port->next_port_in_aggregator) {
1764 __disable_port(port);
1767 /* Slave array needs update. */
1768 *update_slave_arr = true;
1771 /* if the selected aggregator is of join individuals
1772 * (partner_system is NULL), enable their ports
1774 active = __get_active_agg(origin);
1777 if (!__agg_has_partner(active)) {
1778 for (port = active->lag_ports; port;
1779 port = port->next_port_in_aggregator) {
1780 __enable_port(port);
1787 bond_3ad_set_carrier(bond);
1791 * ad_clear_agg - clear a given aggregator's parameters
1792 * @aggregator: the aggregator we're looking at
1794 static void ad_clear_agg(struct aggregator *aggregator)
1797 aggregator->is_individual = false;
1798 aggregator->actor_admin_aggregator_key = 0;
1799 aggregator->actor_oper_aggregator_key = 0;
1800 eth_zero_addr(aggregator->partner_system.mac_addr_value);
1801 aggregator->partner_system_priority = 0;
1802 aggregator->partner_oper_aggregator_key = 0;
1803 aggregator->receive_state = 0;
1804 aggregator->transmit_state = 0;
1805 aggregator->lag_ports = NULL;
1806 aggregator->is_active = 0;
1807 aggregator->num_of_ports = 0;
1808 pr_debug("LAG %d was cleared\n",
1809 aggregator->aggregator_identifier);
1814 * ad_initialize_agg - initialize a given aggregator's parameters
1815 * @aggregator: the aggregator we're looking at
1817 static void ad_initialize_agg(struct aggregator *aggregator)
1820 ad_clear_agg(aggregator);
1822 eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1823 aggregator->aggregator_identifier = 0;
1824 aggregator->slave = NULL;
1829 * ad_initialize_port - initialize a given port's parameters
1830 * @aggregator: the aggregator we're looking at
1831 * @lacp_fast: boolean. whether fast periodic should be used
1833 static void ad_initialize_port(struct port *port, int lacp_fast)
1835 static const struct port_params tmpl = {
1836 .system_priority = 0xffff,
1839 .port_priority = 0xff,
1842 static const struct lacpdu lacpdu = {
1844 .version_number = 0x01,
1845 .tlv_type_actor_info = 0x01,
1846 .actor_information_length = 0x14,
1847 .tlv_type_partner_info = 0x02,
1848 .partner_information_length = 0x14,
1849 .tlv_type_collector_info = 0x03,
1850 .collector_information_length = 0x10,
1851 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1855 port->actor_port_priority = 0xff;
1856 port->actor_port_aggregator_identifier = 0;
1858 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1859 AD_STATE_LACP_ACTIVITY;
1860 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1861 AD_STATE_LACP_ACTIVITY;
1864 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1866 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1867 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1869 port->is_enabled = true;
1870 /* private parameters */
1871 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1872 port->sm_rx_state = 0;
1873 port->sm_rx_timer_counter = 0;
1874 port->sm_periodic_state = 0;
1875 port->sm_periodic_timer_counter = 0;
1876 port->sm_mux_state = 0;
1877 port->sm_mux_timer_counter = 0;
1878 port->sm_tx_state = 0;
1879 port->aggregator = NULL;
1880 port->next_port_in_aggregator = NULL;
1881 port->transaction_id = 0;
1883 port->sm_churn_actor_timer_counter = 0;
1884 port->sm_churn_actor_state = 0;
1885 port->churn_actor_count = 0;
1886 port->sm_churn_partner_timer_counter = 0;
1887 port->sm_churn_partner_state = 0;
1888 port->churn_partner_count = 0;
1890 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1895 * ad_enable_collecting_distributing - enable a port's transmit/receive
1896 * @port: the port we're looking at
1897 * @update_slave_arr: Does slave array need update?
1899 * Enable @port if it's in an active aggregator
1901 static void ad_enable_collecting_distributing(struct port *port,
1902 bool *update_slave_arr)
1904 if (port->aggregator->is_active) {
1905 pr_debug("Enabling port %d(LAG %d)\n",
1906 port->actor_port_number,
1907 port->aggregator->aggregator_identifier);
1908 __enable_port(port);
1909 /* Slave array needs update */
1910 *update_slave_arr = true;
1915 * ad_disable_collecting_distributing - disable a port's transmit/receive
1916 * @port: the port we're looking at
1917 * @update_slave_arr: Does slave array need update?
1919 static void ad_disable_collecting_distributing(struct port *port,
1920 bool *update_slave_arr)
1922 if (port->aggregator &&
1923 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1924 &(null_mac_addr))) {
1925 pr_debug("Disabling port %d(LAG %d)\n",
1926 port->actor_port_number,
1927 port->aggregator->aggregator_identifier);
1928 __disable_port(port);
1929 /* Slave array needs an update */
1930 *update_slave_arr = true;
1935 * ad_marker_info_received - handle receive of a Marker information frame
1936 * @marker_info: Marker info received
1937 * @port: the port we're looking at
1939 static void ad_marker_info_received(struct bond_marker *marker_info,
1942 struct bond_marker marker;
1944 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx);
1945 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_rx);
1947 /* copy the received marker data to the response marker */
1948 memcpy(&marker, marker_info, sizeof(struct bond_marker));
1949 /* change the marker subtype to marker response */
1950 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1952 /* send the marker response */
1953 if (ad_marker_send(port, &marker) >= 0) {
1954 pr_debug("Sent Marker Response on port %d\n",
1955 port->actor_port_number);
1960 * ad_marker_response_received - handle receive of a marker response frame
1961 * @marker: marker PDU received
1962 * @port: the port we're looking at
1964 * This function does nothing since we decided not to implement send and handle
1965 * response for marker PDU's, in this stage, but only to respond to marker
1968 static void ad_marker_response_received(struct bond_marker *marker,
1971 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_resp_rx);
1972 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_resp_rx);
1974 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1977 /* ========= AD exported functions to the main bonding code ========= */
1979 /* Check aggregators status in team every T seconds */
1980 #define AD_AGGREGATOR_SELECTION_TIMER 8
1983 * bond_3ad_initiate_agg_selection - initate aggregator selection
1984 * @bond: bonding struct
1986 * Set the aggregation selection timer, to initiate an agg selection in
1987 * the very near future. Called during first initialization, and during
1988 * any down to up transitions of the bond.
1990 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1992 BOND_AD_INFO(bond).agg_select_timer = timeout;
1996 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1997 * @bond: bonding struct to work on
1998 * @tick_resolution: tick duration (millisecond resolution)
2000 * Can be called only after the mac address of the bond is set.
2002 void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
2004 /* check that the bond is not initialized yet */
2005 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
2006 bond->dev->dev_addr)) {
2008 BOND_AD_INFO(bond).aggregator_identifier = 0;
2010 BOND_AD_INFO(bond).system.sys_priority =
2011 bond->params.ad_actor_sys_prio;
2012 if (is_zero_ether_addr(bond->params.ad_actor_system))
2013 BOND_AD_INFO(bond).system.sys_mac_addr =
2014 *((struct mac_addr *)bond->dev->dev_addr);
2016 BOND_AD_INFO(bond).system.sys_mac_addr =
2017 *((struct mac_addr *)bond->params.ad_actor_system);
2019 /* initialize how many times this module is called in one
2020 * second (should be about every 100ms)
2022 ad_ticks_per_sec = tick_resolution;
2024 bond_3ad_initiate_agg_selection(bond,
2025 AD_AGGREGATOR_SELECTION_TIMER *
2031 * bond_3ad_bind_slave - initialize a slave's port
2032 * @slave: slave struct to work on
2034 * Returns: 0 on success
2037 void bond_3ad_bind_slave(struct slave *slave)
2039 struct bonding *bond = bond_get_bond_by_slave(slave);
2041 struct aggregator *aggregator;
2043 /* check that the slave has not been initialized yet. */
2044 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2046 /* port initialization */
2047 port = &(SLAVE_AD_INFO(slave)->port);
2049 ad_initialize_port(port, bond->params.lacp_fast);
2051 port->slave = slave;
2052 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2053 /* key is determined according to the link speed, duplex and
2056 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2057 ad_update_actor_keys(port, false);
2058 /* actor system is the bond's system */
2059 __ad_actor_update_port(port);
2060 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2061 * lacpdu's are sent in one second)
2063 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2065 __disable_port(port);
2067 /* aggregator initialization */
2068 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2070 ad_initialize_agg(aggregator);
2072 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2073 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2074 aggregator->slave = slave;
2075 aggregator->is_active = 0;
2076 aggregator->num_of_ports = 0;
2081 * bond_3ad_unbind_slave - deinitialize a slave's port
2082 * @slave: slave struct to work on
2084 * Search for the aggregator that is related to this port, remove the
2085 * aggregator and assign another aggregator for other port related to it
2086 * (if any), and remove the port.
2088 void bond_3ad_unbind_slave(struct slave *slave)
2090 struct port *port, *prev_port, *temp_port;
2091 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2092 int select_new_active_agg = 0;
2093 struct bonding *bond = slave->bond;
2094 struct slave *slave_iter;
2095 struct list_head *iter;
2096 bool dummy_slave_update; /* Ignore this value as caller updates array */
2098 /* Sync against bond_3ad_state_machine_handler() */
2099 spin_lock_bh(&bond->mode_lock);
2100 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2101 port = &(SLAVE_AD_INFO(slave)->port);
2103 /* if slave is null, the whole port is not initialized */
2105 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
2110 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
2111 aggregator->aggregator_identifier);
2113 /* Tell the partner that this port is not suitable for aggregation */
2114 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
2115 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
2116 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
2117 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2118 __update_lacpdu_from_port(port);
2119 ad_lacpdu_send(port);
2121 /* check if this aggregator is occupied */
2122 if (aggregator->lag_ports) {
2123 /* check if there are other ports related to this aggregator
2124 * except the port related to this slave(thats ensure us that
2125 * there is a reason to search for new aggregator, and that we
2128 if ((aggregator->lag_ports != port) ||
2129 (aggregator->lag_ports->next_port_in_aggregator)) {
2130 /* find new aggregator for the related port(s) */
2131 bond_for_each_slave(bond, slave_iter, iter) {
2132 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2133 /* if the new aggregator is empty, or it is
2134 * connected to our port only
2136 if (!new_aggregator->lag_ports ||
2137 ((new_aggregator->lag_ports == port) &&
2138 !new_aggregator->lag_ports->next_port_in_aggregator))
2142 new_aggregator = NULL;
2144 /* if new aggregator found, copy the aggregator's
2145 * parameters and connect the related lag_ports to the
2148 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2149 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2150 aggregator->aggregator_identifier,
2151 new_aggregator->aggregator_identifier);
2153 if ((new_aggregator->lag_ports == port) &&
2154 new_aggregator->is_active) {
2155 netdev_info(bond->dev, "Removing an active aggregator\n");
2156 select_new_active_agg = 1;
2159 new_aggregator->is_individual = aggregator->is_individual;
2160 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2161 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2162 new_aggregator->partner_system = aggregator->partner_system;
2163 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2164 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2165 new_aggregator->receive_state = aggregator->receive_state;
2166 new_aggregator->transmit_state = aggregator->transmit_state;
2167 new_aggregator->lag_ports = aggregator->lag_ports;
2168 new_aggregator->is_active = aggregator->is_active;
2169 new_aggregator->num_of_ports = aggregator->num_of_ports;
2171 /* update the information that is written on
2172 * the ports about the aggregator
2174 for (temp_port = aggregator->lag_ports; temp_port;
2175 temp_port = temp_port->next_port_in_aggregator) {
2176 temp_port->aggregator = new_aggregator;
2177 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2180 ad_clear_agg(aggregator);
2182 if (select_new_active_agg)
2183 ad_agg_selection_logic(__get_first_agg(port),
2184 &dummy_slave_update);
2186 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2189 /* in case that the only port related to this
2190 * aggregator is the one we want to remove
2192 select_new_active_agg = aggregator->is_active;
2193 ad_clear_agg(aggregator);
2194 if (select_new_active_agg) {
2195 netdev_info(bond->dev, "Removing an active aggregator\n");
2196 /* select new active aggregator */
2197 temp_aggregator = __get_first_agg(port);
2198 if (temp_aggregator)
2199 ad_agg_selection_logic(temp_aggregator,
2200 &dummy_slave_update);
2205 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
2207 /* find the aggregator that this port is connected to */
2208 bond_for_each_slave(bond, slave_iter, iter) {
2209 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2211 /* search the port in the aggregator's related ports */
2212 for (temp_port = temp_aggregator->lag_ports; temp_port;
2213 prev_port = temp_port,
2214 temp_port = temp_port->next_port_in_aggregator) {
2215 if (temp_port == port) {
2216 /* the aggregator found - detach the port from
2220 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2222 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2223 temp_aggregator->num_of_ports--;
2224 if (__agg_active_ports(temp_aggregator) == 0) {
2225 select_new_active_agg = temp_aggregator->is_active;
2226 ad_clear_agg(temp_aggregator);
2227 if (select_new_active_agg) {
2228 netdev_info(bond->dev, "Removing an active aggregator\n");
2229 /* select new active aggregator */
2230 ad_agg_selection_logic(__get_first_agg(port),
2231 &dummy_slave_update);
2241 spin_unlock_bh(&bond->mode_lock);
2245 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2246 * @bond: bonding struct to work on
2248 * If an ad_actor setting gets changed we need to update the individual port
2249 * settings so the bond device will use the new values when it gets upped.
2251 void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2253 struct list_head *iter;
2254 struct slave *slave;
2258 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2259 if (is_zero_ether_addr(bond->params.ad_actor_system))
2260 BOND_AD_INFO(bond).system.sys_mac_addr =
2261 *((struct mac_addr *)bond->dev->dev_addr);
2263 BOND_AD_INFO(bond).system.sys_mac_addr =
2264 *((struct mac_addr *)bond->params.ad_actor_system);
2266 spin_lock_bh(&bond->mode_lock);
2267 bond_for_each_slave(bond, slave, iter) {
2268 struct port *port = &(SLAVE_AD_INFO(slave))->port;
2270 __ad_actor_update_port(port);
2273 spin_unlock_bh(&bond->mode_lock);
2277 * bond_3ad_state_machine_handler - handle state machines timeout
2278 * @bond: bonding struct to work on
2280 * The state machine handling concept in this module is to check every tick
2281 * which state machine should operate any function. The execution order is
2282 * round robin, so when we have an interaction between state machines, the
2283 * reply of one to each other might be delayed until next tick.
2285 * This function also complete the initialization when the agg_select_timer
2286 * times out, and it selects an aggregator for the ports that are yet not
2287 * related to any aggregator, and selects the active aggregator for a bond.
2289 void bond_3ad_state_machine_handler(struct work_struct *work)
2291 struct bonding *bond = container_of(work, struct bonding,
2293 struct aggregator *aggregator;
2294 struct list_head *iter;
2295 struct slave *slave;
2297 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2298 bool update_slave_arr = false;
2300 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2301 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2302 * concurrently due to incoming LACPDU as well.
2304 spin_lock_bh(&bond->mode_lock);
2307 /* check if there are any slaves */
2308 if (!bond_has_slaves(bond))
2311 /* check if agg_select_timer timer after initialize is timed out */
2312 if (BOND_AD_INFO(bond).agg_select_timer &&
2313 !(--BOND_AD_INFO(bond).agg_select_timer)) {
2314 slave = bond_first_slave_rcu(bond);
2315 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2317 /* select the active aggregator for the bond */
2320 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2325 aggregator = __get_first_agg(port);
2326 ad_agg_selection_logic(aggregator, &update_slave_arr);
2328 bond_3ad_set_carrier(bond);
2331 /* for each port run the state machines */
2332 bond_for_each_slave_rcu(bond, slave, iter) {
2333 port = &(SLAVE_AD_INFO(slave)->port);
2335 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2340 ad_rx_machine(NULL, port);
2341 ad_periodic_machine(port);
2342 ad_port_selection_logic(port, &update_slave_arr);
2343 ad_mux_machine(port, &update_slave_arr);
2344 ad_tx_machine(port);
2345 ad_churn_machine(port);
2347 /* turn off the BEGIN bit, since we already handled it */
2348 if (port->sm_vars & AD_PORT_BEGIN)
2349 port->sm_vars &= ~AD_PORT_BEGIN;
2353 bond_for_each_slave_rcu(bond, slave, iter) {
2354 if (slave->should_notify) {
2355 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2360 spin_unlock_bh(&bond->mode_lock);
2362 if (update_slave_arr)
2363 bond_slave_arr_work_rearm(bond, 0);
2365 if (should_notify_rtnl && rtnl_trylock()) {
2366 bond_slave_state_notify(bond);
2369 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2373 * bond_3ad_rx_indication - handle a received frame
2374 * @lacpdu: received lacpdu
2375 * @slave: slave struct to work on
2377 * It is assumed that frames that were sent on this NIC don't returned as new
2378 * received frames (loopback). Since only the payload is given to this
2379 * function, it check for loopback.
2381 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave)
2383 struct bonding *bond = slave->bond;
2384 int ret = RX_HANDLER_ANOTHER;
2385 struct bond_marker *marker;
2389 port = &(SLAVE_AD_INFO(slave)->port);
2391 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2392 slave->dev->name, slave->bond->dev->name);
2396 switch (lacpdu->subtype) {
2397 case AD_TYPE_LACPDU:
2398 ret = RX_HANDLER_CONSUMED;
2399 netdev_dbg(slave->bond->dev,
2400 "Received LACPDU on port %d slave %s\n",
2401 port->actor_port_number, slave->dev->name);
2402 /* Protect against concurrent state machines */
2403 spin_lock(&slave->bond->mode_lock);
2404 ad_rx_machine(lacpdu, port);
2405 spin_unlock(&slave->bond->mode_lock);
2407 case AD_TYPE_MARKER:
2408 ret = RX_HANDLER_CONSUMED;
2409 /* No need to convert fields to Little Endian since we
2410 * don't use the marker's fields.
2412 marker = (struct bond_marker *)lacpdu;
2413 switch (marker->tlv_type) {
2414 case AD_MARKER_INFORMATION_SUBTYPE:
2415 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2416 port->actor_port_number);
2417 ad_marker_info_received(marker, port);
2419 case AD_MARKER_RESPONSE_SUBTYPE:
2420 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2421 port->actor_port_number);
2422 ad_marker_response_received(marker, port);
2425 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2426 port->actor_port_number);
2427 stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx;
2429 stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx;
2434 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx);
2435 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx);
2442 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2443 * its current speed and duplex settings.
2445 * @port: the port we'are looking at
2446 * @reset: Boolean to just reset the speed and the duplex part of the key
2448 * The logic to change the oper / admin keys is:
2449 * (a) A full duplex port can participate in LACP with partner.
2450 * (b) When the speed is changed, LACP need to be reinitiated.
2452 static void ad_update_actor_keys(struct port *port, bool reset)
2455 u16 ospeed = 0, speed = 0;
2456 u16 old_oper_key = port->actor_oper_port_key;
2458 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2460 speed = __get_link_speed(port);
2461 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2462 duplex = __get_duplex(port);
2463 port->actor_admin_port_key |= (speed << 1) | duplex;
2465 port->actor_oper_port_key = port->actor_admin_port_key;
2467 if (old_oper_key != port->actor_oper_port_key) {
2468 /* Only 'duplex' port participates in LACP */
2470 port->sm_vars |= AD_PORT_LACP_ENABLED;
2472 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2476 netdev_err(port->slave->dev,
2477 "speed changed to 0 for port %s",
2478 port->slave->dev->name);
2479 } else if (duplex && ospeed != speed) {
2480 /* Speed change restarts LACP state-machine */
2481 port->sm_vars |= AD_PORT_BEGIN;
2488 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2491 * @slave: slave struct to work on
2493 * Handle reselection of aggregator (if needed) for this port.
2495 void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2499 port = &(SLAVE_AD_INFO(slave)->port);
2501 /* if slave is null, the whole port is not initialized */
2503 netdev_warn(slave->bond->dev,
2504 "speed/duplex changed for uninitialized port %s\n",
2509 spin_lock_bh(&slave->bond->mode_lock);
2510 ad_update_actor_keys(port, false);
2511 spin_unlock_bh(&slave->bond->mode_lock);
2512 netdev_dbg(slave->bond->dev, "Port %d slave %s changed speed/duplex\n",
2513 port->actor_port_number, slave->dev->name);
2517 * bond_3ad_handle_link_change - handle a slave's link status change indication
2518 * @slave: slave struct to work on
2519 * @status: whether the link is now up or down
2521 * Handle reselection of aggregator (if needed) for this port.
2523 void bond_3ad_handle_link_change(struct slave *slave, char link)
2525 struct aggregator *agg;
2529 port = &(SLAVE_AD_INFO(slave)->port);
2531 /* if slave is null, the whole port is not initialized */
2533 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2538 spin_lock_bh(&slave->bond->mode_lock);
2539 /* on link down we are zeroing duplex and speed since
2540 * some of the adaptors(ce1000.lan) report full duplex/speed
2541 * instead of N/A(duplex) / 0(speed).
2543 * on link up we are forcing recheck on the duplex and speed since
2544 * some of he adaptors(ce1000.lan) report.
2546 if (link == BOND_LINK_UP) {
2547 port->is_enabled = true;
2548 ad_update_actor_keys(port, false);
2550 /* link has failed */
2551 port->is_enabled = false;
2552 ad_update_actor_keys(port, true);
2554 agg = __get_first_agg(port);
2555 ad_agg_selection_logic(agg, &dummy);
2557 spin_unlock_bh(&slave->bond->mode_lock);
2559 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2560 port->actor_port_number,
2561 link == BOND_LINK_UP ? "UP" : "DOWN");
2563 /* RTNL is held and mode_lock is released so it's safe
2564 * to update slave_array here.
2566 bond_update_slave_arr(slave->bond, NULL);
2570 * bond_3ad_set_carrier - set link state for bonding master
2571 * @bond - bonding structure
2573 * if we have an active aggregator, we're up, if not, we're down.
2574 * Presumes that we cannot have an active aggregator if there are
2575 * no slaves with link up.
2577 * This behavior complies with IEEE 802.3 section 43.3.9.
2579 * Called by bond_set_carrier(). Return zero if carrier state does not
2580 * change, nonzero if it does.
2582 int bond_3ad_set_carrier(struct bonding *bond)
2584 struct aggregator *active;
2585 struct slave *first_slave;
2589 first_slave = bond_first_slave_rcu(bond);
2594 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2596 /* are enough slaves available to consider link up? */
2597 if (__agg_active_ports(active) < bond->params.min_links) {
2598 if (netif_carrier_ok(bond->dev)) {
2599 netif_carrier_off(bond->dev);
2602 } else if (!netif_carrier_ok(bond->dev)) {
2603 netif_carrier_on(bond->dev);
2606 } else if (netif_carrier_ok(bond->dev)) {
2607 netif_carrier_off(bond->dev);
2615 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2616 * @bond: bonding struct to work on
2617 * @ad_info: ad_info struct to fill with the bond's info
2619 * Returns: 0 on success
2622 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2623 struct ad_info *ad_info)
2625 struct aggregator *aggregator = NULL;
2626 struct list_head *iter;
2627 struct slave *slave;
2630 bond_for_each_slave_rcu(bond, slave, iter) {
2631 port = &(SLAVE_AD_INFO(slave)->port);
2632 if (port->aggregator && port->aggregator->is_active) {
2633 aggregator = port->aggregator;
2641 ad_info->aggregator_id = aggregator->aggregator_identifier;
2642 ad_info->ports = __agg_active_ports(aggregator);
2643 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2644 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2645 ether_addr_copy(ad_info->partner_system,
2646 aggregator->partner_system.mac_addr_value);
2650 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2655 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2661 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2662 struct slave *slave)
2664 struct lacpdu *lacpdu, _lacpdu;
2666 if (skb->protocol != PKT_TYPE_LACPDU)
2667 return RX_HANDLER_ANOTHER;
2669 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2670 return RX_HANDLER_ANOTHER;
2672 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2674 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx);
2675 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx);
2676 return RX_HANDLER_ANOTHER;
2679 return bond_3ad_rx_indication(lacpdu, slave);
2683 * bond_3ad_update_lacp_rate - change the lacp rate
2684 * @bond - bonding struct
2686 * When modify lacp_rate parameter via sysfs,
2687 * update actor_oper_port_state of each port.
2689 * Hold bond->mode_lock,
2690 * so we can modify port->actor_oper_port_state,
2691 * no matter bond is up or down.
2693 void bond_3ad_update_lacp_rate(struct bonding *bond)
2695 struct port *port = NULL;
2696 struct list_head *iter;
2697 struct slave *slave;
2700 lacp_fast = bond->params.lacp_fast;
2701 spin_lock_bh(&bond->mode_lock);
2702 bond_for_each_slave(bond, slave, iter) {
2703 port = &(SLAVE_AD_INFO(slave)->port);
2705 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2707 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
2709 spin_unlock_bh(&bond->mode_lock);
2712 size_t bond_3ad_stats_size(void)
2714 return nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_RX */
2715 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_TX */
2716 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_UNKNOWN_RX */
2717 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_ILLEGAL_RX */
2718 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RX */
2719 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_TX */
2720 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_RX */
2721 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_TX */
2722 nla_total_size_64bit(sizeof(u64)); /* BOND_3AD_STAT_MARKER_UNKNOWN_RX */
2725 int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats)
2729 val = atomic64_read(&stats->lacpdu_rx);
2730 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val,
2733 val = atomic64_read(&stats->lacpdu_tx);
2734 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val,
2737 val = atomic64_read(&stats->lacpdu_unknown_rx);
2738 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val,
2741 val = atomic64_read(&stats->lacpdu_illegal_rx);
2742 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val,
2746 val = atomic64_read(&stats->marker_rx);
2747 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val,
2750 val = atomic64_read(&stats->marker_tx);
2751 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val,
2754 val = atomic64_read(&stats->marker_resp_rx);
2755 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val,
2758 val = atomic64_read(&stats->marker_resp_tx);
2759 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val,
2762 val = atomic64_read(&stats->marker_unknown_rx);
2763 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val,