]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/bonding/bond_3ad.c
bonding: fix 802.3ad support for 5G and 50G speeds
[mirror_ubuntu-zesty-kernel.git] / drivers / net / bonding / bond_3ad.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
3 *
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)
7 * any later version.
8 *
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
12 * more details.
13 *
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.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
1da177e4
LT
21 */
22
1da177e4
LT
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>
fd989c83 28#include <linux/etherdevice.h>
1da177e4
LT
29#include <linux/if_bonding.h>
30#include <linux/pkt_sched.h>
e730c155 31#include <net/net_namespace.h>
1ef8019b
DM
32#include <net/bonding.h>
33#include <net/bond_3ad.h>
1da177e4 34
3bf2d28a 35/* General definitions */
1da177e4
LT
36#define AD_SHORT_TIMEOUT 1
37#define AD_LONG_TIMEOUT 0
38#define AD_STANDBY 0x2
39#define AD_MAX_TX_IN_SECOND 3
40#define AD_COLLECTOR_MAX_DELAY 0
41
3bf2d28a 42/* Timer definitions (43.4.4 in the 802.3ad standard) */
1da177e4
LT
43#define AD_FAST_PERIODIC_TIME 1
44#define AD_SLOW_PERIODIC_TIME 30
45#define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
46#define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
47#define AD_CHURN_DETECTION_TIME 60
48#define AD_AGGREGATE_WAIT_TIME 2
49
3bf2d28a 50/* Port state definitions (43.4.2.2 in the 802.3ad standard) */
1da177e4
LT
51#define AD_STATE_LACP_ACTIVITY 0x1
52#define AD_STATE_LACP_TIMEOUT 0x2
53#define AD_STATE_AGGREGATION 0x4
54#define AD_STATE_SYNCHRONIZATION 0x8
55#define AD_STATE_COLLECTING 0x10
56#define AD_STATE_DISTRIBUTING 0x20
57#define AD_STATE_DEFAULTED 0x40
58#define AD_STATE_EXPIRED 0x80
59
3bf2d28a
VF
60/* Port Variables definitions used by the State Machines (43.4.7 in the
61 * 802.3ad standard)
62 */
1da177e4
LT
63#define AD_PORT_BEGIN 0x1
64#define AD_PORT_LACP_ENABLED 0x2
65#define AD_PORT_ACTOR_CHURN 0x4
66#define AD_PORT_PARTNER_CHURN 0x8
67#define AD_PORT_READY 0x10
68#define AD_PORT_READY_N 0x20
69#define AD_PORT_MATCHED 0x40
70#define AD_PORT_STANDBY 0x80
71#define AD_PORT_SELECTED 0x100
72#define AD_PORT_MOVED 0x200
ef015d72 73#define AD_PORT_CHURNED (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
1da177e4 74
3bf2d28a
VF
75/* Port Key definitions
76 * key is determined according to the link speed, duplex and
77 * user key (which is yet not supported)
d22a5fc0
MB
78 * --------------------------------------------------------------
79 * Port key | User key (10 bits) | Speed (5 bits) | Duplex|
80 * --------------------------------------------------------------
81 * |15 6|5 1|0
3bf2d28a 82 */
cb8dda90
JX
83#define AD_DUPLEX_KEY_MASKS 0x1
84#define AD_SPEED_KEY_MASKS 0x3E
85#define AD_USER_KEY_MASKS 0xFFC0
86
87enum ad_link_speed_type {
88 AD_LINK_SPEED_1MBPS = 1,
89 AD_LINK_SPEED_10MBPS,
90 AD_LINK_SPEED_100MBPS,
91 AD_LINK_SPEED_1000MBPS,
424c3232 92 AD_LINK_SPEED_2500MBPS,
65da0a91 93 AD_LINK_SPEED_5000MBPS,
424c3232
JX
94 AD_LINK_SPEED_10000MBPS,
95 AD_LINK_SPEED_20000MBPS,
f9dc3524 96 AD_LINK_SPEED_25000MBPS,
424c3232 97 AD_LINK_SPEED_40000MBPS,
65da0a91 98 AD_LINK_SPEED_50000MBPS,
3952af4d
JP
99 AD_LINK_SPEED_56000MBPS,
100 AD_LINK_SPEED_100000MBPS,
cb8dda90 101};
1da177e4 102
815117ad 103/* compare MAC addresses */
104#define MAC_ADDRESS_EQUAL(A, B) \
105 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
1da177e4 106
f87fda00
ED
107static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
108 0, 0, 0, 0, 0, 0
109};
1da177e4
LT
110static u16 ad_ticks_per_sec;
111static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
112
f87fda00
ED
113static const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned =
114 MULTICAST_LACPDU_ADDR;
e4ac4320 115
3bf2d28a 116/* ================= main 802.3ad protocol functions ================== */
1da177e4 117static int ad_lacpdu_send(struct port *port);
1c3f0b8e 118static int ad_marker_send(struct port *port, struct bond_marker *marker);
ee637714 119static void ad_mux_machine(struct port *port, bool *update_slave_arr);
1da177e4
LT
120static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
121static void ad_tx_machine(struct port *port);
122static void ad_periodic_machine(struct port *port);
ee637714
MB
123static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
124static void ad_agg_selection_logic(struct aggregator *aggregator,
125 bool *update_slave_arr);
1da177e4
LT
126static void ad_clear_agg(struct aggregator *aggregator);
127static void ad_initialize_agg(struct aggregator *aggregator);
128static void ad_initialize_port(struct port *port, int lacp_fast);
ee637714
MB
129static void ad_enable_collecting_distributing(struct port *port,
130 bool *update_slave_arr);
131static void ad_disable_collecting_distributing(struct port *port,
132 bool *update_slave_arr);
3bf2d28a
VF
133static void ad_marker_info_received(struct bond_marker *marker_info,
134 struct port *port);
135static void ad_marker_response_received(struct bond_marker *marker,
136 struct port *port);
7bb11dc9 137static void ad_update_actor_keys(struct port *port, bool reset);
1da177e4
LT
138
139
3bf2d28a 140/* ================= api to bonding and kernel code ================== */
1da177e4
LT
141
142/**
143 * __get_bond_by_port - get the port's bonding struct
144 * @port: the port we're looking at
145 *
146 * Return @port's bonding struct, or %NULL if it can't be found.
147 */
148static inline struct bonding *__get_bond_by_port(struct port *port)
149{
7bfc4753 150 if (port->slave == NULL)
1da177e4 151 return NULL;
1da177e4
LT
152
153 return bond_get_bond_by_slave(port->slave);
154}
155
1da177e4
LT
156/**
157 * __get_first_agg - get the first aggregator in the bond
158 * @bond: the bond we're looking at
159 *
160 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
161 * found.
768b9549 162 * The caller must hold RCU or RTNL lock.
1da177e4
LT
163 */
164static inline struct aggregator *__get_first_agg(struct port *port)
165{
166 struct bonding *bond = __get_bond_by_port(port);
dec1e90e 167 struct slave *first_slave;
768b9549 168 struct aggregator *agg;
1da177e4 169
be79bd04 170 /* If there's no bond for this port, or bond has no slaves */
dec1e90e 171 if (bond == NULL)
1da177e4 172 return NULL;
3bf2d28a 173
be79bd04 174 rcu_read_lock();
175 first_slave = bond_first_slave_rcu(bond);
3fdddd85 176 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
be79bd04 177 rcu_read_unlock();
3bf2d28a 178
768b9549 179 return agg;
1da177e4
LT
180}
181
3bf2d28a
VF
182/**
183 * __agg_has_partner - see if we have a partner
184 * @agg: the agregator we're looking at
fd989c83
JV
185 *
186 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
3bf2d28a 187 * address for the partner). Return 0 if not.
fd989c83
JV
188 */
189static inline int __agg_has_partner(struct aggregator *agg)
190{
191 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
192}
193
1da177e4
LT
194/**
195 * __disable_port - disable the port's slave
196 * @port: the port we're looking at
1da177e4
LT
197 */
198static inline void __disable_port(struct port *port)
199{
5e5b0665 200 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
1da177e4
LT
201}
202
203/**
204 * __enable_port - enable the port's slave, if it's up
205 * @port: the port we're looking at
1da177e4
LT
206 */
207static inline void __enable_port(struct port *port)
208{
209 struct slave *slave = port->slave;
210
b6adc610 211 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
5e5b0665 212 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
1da177e4
LT
213}
214
215/**
216 * __port_is_enabled - check if the port's slave is in active state
217 * @port: the port we're looking at
1da177e4
LT
218 */
219static inline int __port_is_enabled(struct port *port)
220{
e30bc066 221 return bond_is_active_slave(port->slave);
1da177e4
LT
222}
223
224/**
225 * __get_agg_selection_mode - get the aggregator selection mode
226 * @port: the port we're looking at
227 *
fd989c83 228 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
1da177e4
LT
229 */
230static inline u32 __get_agg_selection_mode(struct port *port)
231{
232 struct bonding *bond = __get_bond_by_port(port);
233
7bfc4753 234 if (bond == NULL)
fd989c83 235 return BOND_AD_STABLE;
1da177e4 236
1a14fbcb 237 return bond->params.ad_select;
1da177e4
LT
238}
239
240/**
241 * __check_agg_selection_timer - check if the selection timer has expired
242 * @port: the port we're looking at
1da177e4
LT
243 */
244static inline int __check_agg_selection_timer(struct port *port)
245{
246 struct bonding *bond = __get_bond_by_port(port);
247
7bfc4753 248 if (bond == NULL)
1da177e4 249 return 0;
1da177e4
LT
250
251 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
252}
253
1da177e4
LT
254/**
255 * __get_link_speed - get a port's speed
256 * @port: the port we're looking at
257 *
cb8dda90 258 * Return @port's speed in 802.3ad enum format. i.e. one of:
1da177e4 259 * 0,
cb8dda90
JX
260 * %AD_LINK_SPEED_10MBPS,
261 * %AD_LINK_SPEED_100MBPS,
262 * %AD_LINK_SPEED_1000MBPS,
424c3232 263 * %AD_LINK_SPEED_2500MBPS,
65da0a91 264 * %AD_LINK_SPEED_5000MBPS,
cb8dda90 265 * %AD_LINK_SPEED_10000MBPS
424c3232 266 * %AD_LINK_SPEED_20000MBPS
f9dc3524 267 * %AD_LINK_SPEED_25000MBPS
424c3232 268 * %AD_LINK_SPEED_40000MBPS
65da0a91 269 * %AD_LINK_SPEED_50000MBPS
424c3232 270 * %AD_LINK_SPEED_56000MBPS
3952af4d 271 * %AD_LINK_SPEED_100000MBPS
1da177e4
LT
272 */
273static u16 __get_link_speed(struct port *port)
274{
275 struct slave *slave = port->slave;
276 u16 speed;
277
3bf2d28a
VF
278 /* this if covers only a special case: when the configuration starts
279 * with link down, it sets the speed to 0.
280 * This is done in spite of the fact that the e100 driver reports 0
281 * to be compatible with MVT in the future.
282 */
7bfc4753 283 if (slave->link != BOND_LINK_UP)
128ea6c3 284 speed = 0;
7bfc4753 285 else {
1da177e4
LT
286 switch (slave->speed) {
287 case SPEED_10:
cb8dda90 288 speed = AD_LINK_SPEED_10MBPS;
1da177e4
LT
289 break;
290
291 case SPEED_100:
cb8dda90 292 speed = AD_LINK_SPEED_100MBPS;
1da177e4
LT
293 break;
294
295 case SPEED_1000:
cb8dda90 296 speed = AD_LINK_SPEED_1000MBPS;
1da177e4
LT
297 break;
298
424c3232
JX
299 case SPEED_2500:
300 speed = AD_LINK_SPEED_2500MBPS;
301 break;
302
65da0a91
TC
303 case SPEED_5000:
304 speed = AD_LINK_SPEED_5000MBPS;
305 break;
306
94dbffd5 307 case SPEED_10000:
cb8dda90 308 speed = AD_LINK_SPEED_10000MBPS;
94dbffd5
JV
309 break;
310
424c3232
JX
311 case SPEED_20000:
312 speed = AD_LINK_SPEED_20000MBPS;
313 break;
314
f9dc3524
JW
315 case SPEED_25000:
316 speed = AD_LINK_SPEED_25000MBPS;
317 break;
318
424c3232
JX
319 case SPEED_40000:
320 speed = AD_LINK_SPEED_40000MBPS;
321 break;
322
65da0a91
TC
323 case SPEED_50000:
324 speed = AD_LINK_SPEED_50000MBPS;
325 break;
326
424c3232
JX
327 case SPEED_56000:
328 speed = AD_LINK_SPEED_56000MBPS;
329 break;
330
3952af4d
JP
331 case SPEED_100000:
332 speed = AD_LINK_SPEED_100000MBPS;
333 break;
334
1da177e4 335 default:
3bf2d28a
VF
336 /* unknown speed value from ethtool. shouldn't happen */
337 speed = 0;
1da177e4
LT
338 break;
339 }
340 }
341
d4471f5e
VF
342 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
343 port->actor_port_number, speed);
1da177e4
LT
344 return speed;
345}
346
347/**
348 * __get_duplex - get a port's duplex
349 * @port: the port we're looking at
350 *
351 * Return @port's duplex in 802.3ad bitmask format. i.e.:
352 * 0x01 if in full duplex
353 * 0x00 otherwise
354 */
355static u8 __get_duplex(struct port *port)
356{
357 struct slave *slave = port->slave;
b25c2e7d 358 u8 retval = 0x0;
1da177e4 359
3bf2d28a
VF
360 /* handling a special case: when the configuration starts with
361 * link down, it sets the duplex to 0.
362 */
b25c2e7d 363 if (slave->link == BOND_LINK_UP) {
1da177e4
LT
364 switch (slave->duplex) {
365 case DUPLEX_FULL:
128ea6c3 366 retval = 0x1;
d4471f5e
VF
367 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
368 port->actor_port_number);
1da177e4
LT
369 break;
370 case DUPLEX_HALF:
371 default:
128ea6c3 372 retval = 0x0;
d4471f5e
VF
373 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
374 port->actor_port_number);
1da177e4
LT
375 break;
376 }
377 }
378 return retval;
379}
380
5ee14e6d
NA
381static void __ad_actor_update_port(struct port *port)
382{
383 const struct bonding *bond = bond_get_bond_by_slave(port->slave);
384
385 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
386 port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority;
387}
388
3bf2d28a 389/* Conversions */
1da177e4
LT
390
391/**
392 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
393 * @timer_type: which timer to operate
394 * @par: timer parameter. see below
395 *
396 * If @timer_type is %current_while_timer, @par indicates long/short timer.
397 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
3bf2d28a 398 * %SLOW_PERIODIC_TIME.
1da177e4
LT
399 */
400static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
401{
128ea6c3 402 u16 retval = 0; /* to silence the compiler */
1da177e4
LT
403
404 switch (timer_type) {
3bf2d28a 405 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
7bfc4753 406 if (par)
3bf2d28a 407 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
7bfc4753 408 else
3bf2d28a 409 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
1da177e4 410 break;
3bf2d28a 411 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
1da177e4
LT
412 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
413 break;
3bf2d28a
VF
414 case AD_PERIODIC_TIMER: /* for periodic machine */
415 retval = (par*ad_ticks_per_sec); /* long timeout */
1da177e4 416 break;
3bf2d28a 417 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
1da177e4
LT
418 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
419 break;
3bf2d28a 420 case AD_WAIT_WHILE_TIMER: /* for selection machine */
1da177e4
LT
421 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
422 break;
423 }
3bf2d28a 424
1da177e4
LT
425 return retval;
426}
427
428
3bf2d28a 429/* ================= ad_rx_machine helper functions ================== */
1da177e4 430
2d6682db
JV
431/**
432 * __choose_matched - update a port's matched variable from a received lacpdu
433 * @lacpdu: the lacpdu we've received
434 * @port: the port we're looking at
435 *
436 * Update the value of the matched variable, using parameter values from a
437 * newly received lacpdu. Parameter values for the partner carried in the
438 * received PDU are compared with the corresponding operational parameter
439 * values for the actor. Matched is set to TRUE if all of these parameters
440 * match and the PDU parameter partner_state.aggregation has the same value as
441 * actor_oper_port_state.aggregation and lacp will actively maintain the link
442 * in the aggregation. Matched is also set to TRUE if the value of
443 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
444 * an individual link and lacp will actively maintain the link. Otherwise,
445 * matched is set to FALSE. LACP is considered to be actively maintaining the
446 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
447 * the actor's actor_oper_port_state.lacp_activity and the PDU's
448 * partner_state.lacp_activity variables are TRUE.
449 *
450 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
451 * used here to implement the language from 802.3ad 43.4.9 that requires
452 * recordPDU to "match" the LACPDU parameters to the stored values.
453 */
454static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
455{
815117ad 456 /* check if all parameters are alike
457 * or this is individual link(aggregation == FALSE)
458 * then update the state machine Matched variable.
459 */
2d6682db
JV
460 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
461 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
815117ad 462 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
2d6682db
JV
463 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
464 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
465 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
2d6682db
JV
466 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
467 ) {
2d6682db
JV
468 port->sm_vars |= AD_PORT_MATCHED;
469 } else {
470 port->sm_vars &= ~AD_PORT_MATCHED;
471 }
472}
473
1da177e4
LT
474/**
475 * __record_pdu - record parameters from a received lacpdu
476 * @lacpdu: the lacpdu we've received
477 * @port: the port we're looking at
478 *
479 * Record the parameter values for the Actor carried in a received lacpdu as
480 * the current partner operational parameter values and sets
481 * actor_oper_port_state.defaulted to FALSE.
482 */
483static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
484{
1da177e4 485 if (lacpdu && port) {
b99d6ba9
HE
486 struct port_params *partner = &port->partner_oper;
487
2d6682db 488 __choose_matched(lacpdu, port);
3bf2d28a
VF
489 /* record the new parameter values for the partner
490 * operational
491 */
b99d6ba9
HE
492 partner->port_number = ntohs(lacpdu->actor_port);
493 partner->port_priority = ntohs(lacpdu->actor_port_priority);
494 partner->system = lacpdu->actor_system;
495 partner->system_priority = ntohs(lacpdu->actor_system_priority);
496 partner->key = ntohs(lacpdu->actor_key);
497 partner->port_state = lacpdu->actor_state;
1da177e4 498
3bf2d28a 499 /* set actor_oper_port_state.defaulted to FALSE */
1da177e4
LT
500 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
501
3bf2d28a
VF
502 /* set the partner sync. to on if the partner is sync,
503 * and the port is matched
504 */
63b46242
WK
505 if ((port->sm_vars & AD_PORT_MATCHED) &&
506 (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
b99d6ba9 507 partner->port_state |= AD_STATE_SYNCHRONIZATION;
63b46242
WK
508 pr_debug("%s partner sync=1\n", port->slave->dev->name);
509 } else {
b99d6ba9 510 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
63b46242
WK
511 pr_debug("%s partner sync=0\n", port->slave->dev->name);
512 }
1da177e4
LT
513 }
514}
515
516/**
517 * __record_default - record default parameters
518 * @port: the port we're looking at
519 *
520 * This function records the default parameter values for the partner carried
521 * in the Partner Admin parameters as the current partner operational parameter
522 * values and sets actor_oper_port_state.defaulted to TRUE.
523 */
524static void __record_default(struct port *port)
525{
1da177e4 526 if (port) {
3bf2d28a 527 /* record the partner admin parameters */
5eefd1ad
HE
528 memcpy(&port->partner_oper, &port->partner_admin,
529 sizeof(struct port_params));
1da177e4 530
3bf2d28a 531 /* set actor_oper_port_state.defaulted to true */
1da177e4
LT
532 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
533 }
534}
535
536/**
537 * __update_selected - update a port's Selected variable from a received lacpdu
538 * @lacpdu: the lacpdu we've received
539 * @port: the port we're looking at
540 *
541 * Update the value of the selected variable, using parameter values from a
542 * newly received lacpdu. The parameter values for the Actor carried in the
543 * received PDU are compared with the corresponding operational parameter
544 * values for the ports partner. If one or more of the comparisons shows that
545 * the value(s) received in the PDU differ from the current operational values,
546 * then selected is set to FALSE and actor_oper_port_state.synchronization is
547 * set to out_of_sync. Otherwise, selected remains unchanged.
548 */
549static void __update_selected(struct lacpdu *lacpdu, struct port *port)
550{
1da177e4 551 if (lacpdu && port) {
ce6a49ad
HE
552 const struct port_params *partner = &port->partner_oper;
553
815117ad 554 /* check if any parameter is different then
555 * update the state machine selected variable.
556 */
8e95a202
JP
557 if (ntohs(lacpdu->actor_port) != partner->port_number ||
558 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
815117ad 559 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
8e95a202
JP
560 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
561 ntohs(lacpdu->actor_key) != partner->key ||
562 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
1da177e4
LT
563 port->sm_vars &= ~AD_PORT_SELECTED;
564 }
565 }
566}
567
568/**
569 * __update_default_selected - update a port's Selected variable from Partner
570 * @port: the port we're looking at
571 *
572 * This function updates the value of the selected variable, using the partner
573 * administrative parameter values. The administrative values are compared with
574 * the corresponding operational parameter values for the partner. If one or
575 * more of the comparisons shows that the administrative value(s) differ from
576 * the current operational values, then Selected is set to FALSE and
577 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
578 * Selected remains unchanged.
579 */
580static void __update_default_selected(struct port *port)
581{
1da177e4 582 if (port) {
3c52065f
HE
583 const struct port_params *admin = &port->partner_admin;
584 const struct port_params *oper = &port->partner_oper;
585
815117ad 586 /* check if any parameter is different then
587 * update the state machine selected variable.
588 */
8e95a202
JP
589 if (admin->port_number != oper->port_number ||
590 admin->port_priority != oper->port_priority ||
815117ad 591 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
8e95a202
JP
592 admin->system_priority != oper->system_priority ||
593 admin->key != oper->key ||
594 (admin->port_state & AD_STATE_AGGREGATION)
3c52065f 595 != (oper->port_state & AD_STATE_AGGREGATION)) {
1da177e4
LT
596 port->sm_vars &= ~AD_PORT_SELECTED;
597 }
598 }
599}
600
1da177e4
LT
601/**
602 * __update_ntt - update a port's ntt variable from a received lacpdu
603 * @lacpdu: the lacpdu we've received
604 * @port: the port we're looking at
605 *
606 * Updates the value of the ntt variable, using parameter values from a newly
607 * received lacpdu. The parameter values for the partner carried in the
608 * received PDU are compared with the corresponding operational parameter
609 * values for the Actor. If one or more of the comparisons shows that the
610 * value(s) received in the PDU differ from the current operational values,
611 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
612 */
613static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
614{
815117ad 615 /* validate lacpdu and port */
1da177e4 616 if (lacpdu && port) {
815117ad 617 /* check if any parameter is different then
618 * update the port->ntt.
619 */
89cc76f9
JV
620 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
621 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
815117ad 622 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
89cc76f9
JV
623 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
624 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
1da177e4
LT
625 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
626 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
627 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
628 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
629 ) {
d238d458 630 port->ntt = true;
1da177e4
LT
631 }
632 }
633}
634
1da177e4
LT
635/**
636 * __agg_ports_are_ready - check if all ports in an aggregator are ready
637 * @aggregator: the aggregator we're looking at
638 *
639 */
640static int __agg_ports_are_ready(struct aggregator *aggregator)
641{
642 struct port *port;
643 int retval = 1;
644
645 if (aggregator) {
3bf2d28a
VF
646 /* scan all ports in this aggregator to verfy if they are
647 * all ready.
648 */
128ea6c3
BD
649 for (port = aggregator->lag_ports;
650 port;
651 port = port->next_port_in_aggregator) {
1da177e4
LT
652 if (!(port->sm_vars & AD_PORT_READY_N)) {
653 retval = 0;
654 break;
655 }
656 }
657 }
658
659 return retval;
660}
661
662/**
663 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
664 * @aggregator: the aggregator we're looking at
665 * @val: Should the ports' ready bit be set on or off
666 *
667 */
668static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
669{
670 struct port *port;
671
128ea6c3
BD
672 for (port = aggregator->lag_ports; port;
673 port = port->next_port_in_aggregator) {
7bfc4753 674 if (val)
1da177e4 675 port->sm_vars |= AD_PORT_READY;
7bfc4753 676 else
1da177e4 677 port->sm_vars &= ~AD_PORT_READY;
1da177e4
LT
678 }
679}
680
0622cab0
JV
681static int __agg_active_ports(struct aggregator *agg)
682{
683 struct port *port;
684 int active = 0;
685
686 for (port = agg->lag_ports; port;
687 port = port->next_port_in_aggregator) {
688 if (port->is_enabled)
689 active++;
690 }
691
692 return active;
693}
694
1da177e4
LT
695/**
696 * __get_agg_bandwidth - get the total bandwidth of an aggregator
697 * @aggregator: the aggregator we're looking at
698 *
699 */
700static u32 __get_agg_bandwidth(struct aggregator *aggregator)
701{
0622cab0 702 int nports = __agg_active_ports(aggregator);
128ea6c3 703 u32 bandwidth = 0;
1da177e4 704
0622cab0 705 if (nports) {
65cce19c 706 switch (__get_link_speed(aggregator->lag_ports)) {
cb8dda90 707 case AD_LINK_SPEED_1MBPS:
0622cab0 708 bandwidth = nports;
1da177e4 709 break;
cb8dda90 710 case AD_LINK_SPEED_10MBPS:
0622cab0 711 bandwidth = nports * 10;
1da177e4 712 break;
cb8dda90 713 case AD_LINK_SPEED_100MBPS:
0622cab0 714 bandwidth = nports * 100;
1da177e4 715 break;
cb8dda90 716 case AD_LINK_SPEED_1000MBPS:
0622cab0 717 bandwidth = nports * 1000;
1da177e4 718 break;
424c3232 719 case AD_LINK_SPEED_2500MBPS:
0622cab0 720 bandwidth = nports * 2500;
424c3232 721 break;
65da0a91
TC
722 case AD_LINK_SPEED_5000MBPS:
723 bandwidth = nports * 5000;
724 break;
cb8dda90 725 case AD_LINK_SPEED_10000MBPS:
0622cab0 726 bandwidth = nports * 10000;
94dbffd5 727 break;
424c3232 728 case AD_LINK_SPEED_20000MBPS:
0622cab0 729 bandwidth = nports * 20000;
424c3232 730 break;
f9dc3524
JW
731 case AD_LINK_SPEED_25000MBPS:
732 bandwidth = nports * 25000;
733 break;
424c3232 734 case AD_LINK_SPEED_40000MBPS:
0622cab0 735 bandwidth = nports * 40000;
424c3232 736 break;
65da0a91
TC
737 case AD_LINK_SPEED_50000MBPS:
738 bandwidth = nports * 50000;
739 break;
424c3232 740 case AD_LINK_SPEED_56000MBPS:
0622cab0 741 bandwidth = nports * 56000;
424c3232 742 break;
3952af4d 743 case AD_LINK_SPEED_100000MBPS:
0622cab0 744 bandwidth = nports * 100000;
3952af4d 745 break;
1da177e4 746 default:
3bf2d28a 747 bandwidth = 0; /* to silence the compiler */
1da177e4
LT
748 }
749 }
750 return bandwidth;
751}
752
753/**
754 * __get_active_agg - get the current active aggregator
755 * @aggregator: the aggregator we're looking at
49b7624e
VF
756 *
757 * Caller must hold RCU lock.
1da177e4
LT
758 */
759static struct aggregator *__get_active_agg(struct aggregator *aggregator)
760{
19177e7d
VF
761 struct bonding *bond = aggregator->slave->bond;
762 struct list_head *iter;
763 struct slave *slave;
1da177e4 764
be79bd04 765 bond_for_each_slave_rcu(bond, slave, iter)
3fdddd85 766 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
767 return &(SLAVE_AD_INFO(slave)->aggregator);
1da177e4 768
19177e7d 769 return NULL;
1da177e4
LT
770}
771
772/**
773 * __update_lacpdu_from_port - update a port's lacpdu fields
774 * @port: the port we're looking at
1da177e4
LT
775 */
776static inline void __update_lacpdu_from_port(struct port *port)
777{
778 struct lacpdu *lacpdu = &port->lacpdu;
3b5b35d0 779 const struct port_params *partner = &port->partner_oper;
1da177e4 780
3bf2d28a
VF
781 /* update current actual Actor parameters
782 * lacpdu->subtype initialized
1da177e4
LT
783 * lacpdu->version_number initialized
784 * lacpdu->tlv_type_actor_info initialized
785 * lacpdu->actor_information_length initialized
786 */
787
d3bb52b0 788 lacpdu->actor_system_priority = htons(port->actor_system_priority);
1da177e4 789 lacpdu->actor_system = port->actor_system;
d3bb52b0
AV
790 lacpdu->actor_key = htons(port->actor_oper_port_key);
791 lacpdu->actor_port_priority = htons(port->actor_port_priority);
792 lacpdu->actor_port = htons(port->actor_port_number);
1da177e4 793 lacpdu->actor_state = port->actor_oper_port_state;
63b46242
WK
794 pr_debug("update lacpdu: %s, actor port state %x\n",
795 port->slave->dev->name, port->actor_oper_port_state);
1da177e4
LT
796
797 /* lacpdu->reserved_3_1 initialized
798 * lacpdu->tlv_type_partner_info initialized
799 * lacpdu->partner_information_length initialized
800 */
801
3b5b35d0
HE
802 lacpdu->partner_system_priority = htons(partner->system_priority);
803 lacpdu->partner_system = partner->system;
804 lacpdu->partner_key = htons(partner->key);
805 lacpdu->partner_port_priority = htons(partner->port_priority);
806 lacpdu->partner_port = htons(partner->port_number);
807 lacpdu->partner_state = partner->port_state;
1da177e4
LT
808
809 /* lacpdu->reserved_3_2 initialized
810 * lacpdu->tlv_type_collector_info initialized
811 * lacpdu->collector_information_length initialized
812 * collector_max_delay initialized
813 * reserved_12[12] initialized
814 * tlv_type_terminator initialized
815 * terminator_length initialized
816 * reserved_50[50] initialized
817 */
1da177e4
LT
818}
819
3bf2d28a 820/* ================= main 802.3ad protocol code ========================= */
1da177e4
LT
821
822/**
823 * ad_lacpdu_send - send out a lacpdu packet on a given port
824 * @port: the port we're looking at
825 *
826 * Returns: 0 on success
827 * < 0 on error
828 */
829static int ad_lacpdu_send(struct port *port)
830{
831 struct slave *slave = port->slave;
832 struct sk_buff *skb;
833 struct lacpdu_header *lacpdu_header;
834 int length = sizeof(struct lacpdu_header);
1da177e4
LT
835
836 skb = dev_alloc_skb(length);
7bfc4753 837 if (!skb)
1da177e4 838 return -ENOMEM;
1da177e4
LT
839
840 skb->dev = slave->dev;
459a98ed 841 skb_reset_mac_header(skb);
b0e380b1 842 skb->network_header = skb->mac_header + ETH_HLEN;
1da177e4
LT
843 skb->protocol = PKT_TYPE_LACPDU;
844 skb->priority = TC_PRIO_CONTROL;
845
846 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
847
ada0f863 848 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
b595076a 849 /* Note: source address is set to be the member's PERMANENT address,
3bf2d28a
VF
850 * because we use it to identify loopback lacpdus in receive.
851 */
ada0f863 852 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
e727149e 853 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
1da177e4 854
3bf2d28a 855 lacpdu_header->lacpdu = port->lacpdu;
1da177e4
LT
856
857 dev_queue_xmit(skb);
858
859 return 0;
860}
861
862/**
863 * ad_marker_send - send marker information/response on a given port
864 * @port: the port we're looking at
865 * @marker: marker data to send
866 *
867 * Returns: 0 on success
868 * < 0 on error
869 */
1c3f0b8e 870static int ad_marker_send(struct port *port, struct bond_marker *marker)
1da177e4
LT
871{
872 struct slave *slave = port->slave;
873 struct sk_buff *skb;
1c3f0b8e
MD
874 struct bond_marker_header *marker_header;
875 int length = sizeof(struct bond_marker_header);
1da177e4
LT
876
877 skb = dev_alloc_skb(length + 16);
7bfc4753 878 if (!skb)
1da177e4 879 return -ENOMEM;
1da177e4
LT
880
881 skb_reserve(skb, 16);
882
883 skb->dev = slave->dev;
459a98ed 884 skb_reset_mac_header(skb);
b0e380b1 885 skb->network_header = skb->mac_header + ETH_HLEN;
1da177e4
LT
886 skb->protocol = PKT_TYPE_LACPDU;
887
1c3f0b8e 888 marker_header = (struct bond_marker_header *)skb_put(skb, length);
1da177e4 889
ada0f863 890 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
b595076a 891 /* Note: source address is set to be the member's PERMANENT address,
3bf2d28a
VF
892 * because we use it to identify loopback MARKERs in receive.
893 */
ada0f863 894 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
e727149e 895 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
1da177e4 896
3bf2d28a 897 marker_header->marker = *marker;
1da177e4
LT
898
899 dev_queue_xmit(skb);
900
901 return 0;
902}
903
904/**
905 * ad_mux_machine - handle a port's mux state machine
906 * @port: the port we're looking at
ee637714 907 * @update_slave_arr: Does slave array need update?
1da177e4 908 */
ee637714 909static void ad_mux_machine(struct port *port, bool *update_slave_arr)
1da177e4
LT
910{
911 mux_states_t last_state;
912
3bf2d28a
VF
913 /* keep current State Machine state to compare later if it was
914 * changed
915 */
1da177e4
LT
916 last_state = port->sm_mux_state;
917
918 if (port->sm_vars & AD_PORT_BEGIN) {
3bf2d28a 919 port->sm_mux_state = AD_MUX_DETACHED;
1da177e4
LT
920 } else {
921 switch (port->sm_mux_state) {
922 case AD_MUX_DETACHED:
7bfc4753
BD
923 if ((port->sm_vars & AD_PORT_SELECTED)
924 || (port->sm_vars & AD_PORT_STANDBY))
925 /* if SELECTED or STANDBY */
3bf2d28a 926 port->sm_mux_state = AD_MUX_WAITING;
1da177e4
LT
927 break;
928 case AD_MUX_WAITING:
3bf2d28a
VF
929 /* if SELECTED == FALSE return to DETACH state */
930 if (!(port->sm_vars & AD_PORT_SELECTED)) {
1da177e4 931 port->sm_vars &= ~AD_PORT_READY_N;
3bf2d28a
VF
932 /* in order to withhold the Selection Logic to
933 * check all ports READY_N value every callback
934 * cycle to update ready variable, we check
935 * READY_N and update READY here
936 */
1da177e4 937 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
3bf2d28a 938 port->sm_mux_state = AD_MUX_DETACHED;
1da177e4
LT
939 break;
940 }
941
3bf2d28a 942 /* check if the wait_while_timer expired */
7bfc4753
BD
943 if (port->sm_mux_timer_counter
944 && !(--port->sm_mux_timer_counter))
1da177e4 945 port->sm_vars |= AD_PORT_READY_N;
1da177e4 946
3bf2d28a
VF
947 /* in order to withhold the selection logic to check
948 * all ports READY_N value every callback cycle to
949 * update ready variable, we check READY_N and update
950 * READY here
951 */
1da177e4
LT
952 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
953
3bf2d28a
VF
954 /* if the wait_while_timer expired, and the port is
955 * in READY state, move to ATTACHED state
956 */
7bfc4753
BD
957 if ((port->sm_vars & AD_PORT_READY)
958 && !port->sm_mux_timer_counter)
3bf2d28a 959 port->sm_mux_state = AD_MUX_ATTACHED;
1da177e4
LT
960 break;
961 case AD_MUX_ATTACHED:
3bf2d28a
VF
962 /* check also if agg_select_timer expired (so the
963 * edable port will take place only after this timer)
964 */
965 if ((port->sm_vars & AD_PORT_SELECTED) &&
966 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
967 !__check_agg_selection_timer(port)) {
63b46242
WK
968 if (port->aggregator->is_active)
969 port->sm_mux_state =
970 AD_MUX_COLLECTING_DISTRIBUTING;
3bf2d28a
VF
971 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
972 (port->sm_vars & AD_PORT_STANDBY)) {
973 /* if UNSELECTED or STANDBY */
1da177e4 974 port->sm_vars &= ~AD_PORT_READY_N;
3bf2d28a
VF
975 /* in order to withhold the selection logic to
976 * check all ports READY_N value every callback
977 * cycle to update ready variable, we check
978 * READY_N and update READY here
979 */
1da177e4 980 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
3bf2d28a 981 port->sm_mux_state = AD_MUX_DETACHED;
63b46242
WK
982 } else if (port->aggregator->is_active) {
983 port->actor_oper_port_state |=
984 AD_STATE_SYNCHRONIZATION;
1da177e4
LT
985 }
986 break;
987 case AD_MUX_COLLECTING_DISTRIBUTING:
3bf2d28a
VF
988 if (!(port->sm_vars & AD_PORT_SELECTED) ||
989 (port->sm_vars & AD_PORT_STANDBY) ||
63b46242
WK
990 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) ||
991 !(port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) {
3bf2d28a 992 port->sm_mux_state = AD_MUX_ATTACHED;
1da177e4 993 } else {
3bf2d28a
VF
994 /* if port state hasn't changed make
995 * sure that a collecting distributing
996 * port in an active aggregator is enabled
997 */
1da177e4
LT
998 if (port->aggregator &&
999 port->aggregator->is_active &&
1000 !__port_is_enabled(port)) {
1001
1002 __enable_port(port);
1003 }
1004 }
1005 break;
3bf2d28a 1006 default:
1da177e4
LT
1007 break;
1008 }
1009 }
1010
3bf2d28a 1011 /* check if the state machine was changed */
1da177e4 1012 if (port->sm_mux_state != last_state) {
63b46242
WK
1013 pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1014 port->actor_port_number,
1015 port->slave->dev->name,
1016 last_state,
a4aee5c8 1017 port->sm_mux_state);
1da177e4
LT
1018 switch (port->sm_mux_state) {
1019 case AD_MUX_DETACHED:
1da177e4 1020 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
ee637714
MB
1021 ad_disable_collecting_distributing(port,
1022 update_slave_arr);
1da177e4
LT
1023 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1024 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
d238d458 1025 port->ntt = true;
1da177e4
LT
1026 break;
1027 case AD_MUX_WAITING:
1028 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1029 break;
1030 case AD_MUX_ATTACHED:
63b46242
WK
1031 if (port->aggregator->is_active)
1032 port->actor_oper_port_state |=
1033 AD_STATE_SYNCHRONIZATION;
1034 else
1035 port->actor_oper_port_state &=
1036 ~AD_STATE_SYNCHRONIZATION;
1da177e4
LT
1037 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1038 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
ee637714
MB
1039 ad_disable_collecting_distributing(port,
1040 update_slave_arr);
d238d458 1041 port->ntt = true;
1da177e4
LT
1042 break;
1043 case AD_MUX_COLLECTING_DISTRIBUTING:
1044 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1045 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
63b46242 1046 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
ee637714
MB
1047 ad_enable_collecting_distributing(port,
1048 update_slave_arr);
d238d458 1049 port->ntt = true;
1da177e4 1050 break;
3bf2d28a 1051 default:
1da177e4
LT
1052 break;
1053 }
1054 }
1055}
1056
1057/**
1058 * ad_rx_machine - handle a port's rx State Machine
1059 * @lacpdu: the lacpdu we've received
1060 * @port: the port we're looking at
1061 *
1062 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1063 * CURRENT. If timer expired set the state machine in the proper state.
1064 * In other cases, this function checks if we need to switch to other state.
1065 */
1066static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1067{
1068 rx_states_t last_state;
1069
3bf2d28a
VF
1070 /* keep current State Machine state to compare later if it was
1071 * changed
1072 */
1da177e4
LT
1073 last_state = port->sm_rx_state;
1074
3bf2d28a
VF
1075 /* check if state machine should change state */
1076
1077 /* first, check if port was reinitialized */
14c9551a 1078 if (port->sm_vars & AD_PORT_BEGIN) {
7bfc4753 1079 port->sm_rx_state = AD_RX_INITIALIZE;
ef015d72 1080 port->sm_vars |= AD_PORT_CHURNED;
3bf2d28a 1081 /* check if port is not enabled */
14c9551a 1082 } else if (!(port->sm_vars & AD_PORT_BEGIN)
7bfc4753 1083 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
7bfc4753 1084 port->sm_rx_state = AD_RX_PORT_DISABLED;
3bf2d28a
VF
1085 /* check if new lacpdu arrived */
1086 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1087 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1088 (port->sm_rx_state == AD_RX_CURRENT))) {
14c9551a 1089 if (port->sm_rx_state != AD_RX_CURRENT)
ef015d72 1090 port->sm_vars |= AD_PORT_CHURNED;
3bf2d28a 1091 port->sm_rx_timer_counter = 0;
1da177e4
LT
1092 port->sm_rx_state = AD_RX_CURRENT;
1093 } else {
3bf2d28a
VF
1094 /* if timer is on, and if it is expired */
1095 if (port->sm_rx_timer_counter &&
1096 !(--port->sm_rx_timer_counter)) {
1da177e4
LT
1097 switch (port->sm_rx_state) {
1098 case AD_RX_EXPIRED:
3bf2d28a 1099 port->sm_rx_state = AD_RX_DEFAULTED;
1da177e4
LT
1100 break;
1101 case AD_RX_CURRENT:
3bf2d28a 1102 port->sm_rx_state = AD_RX_EXPIRED;
1da177e4 1103 break;
3bf2d28a 1104 default:
1da177e4
LT
1105 break;
1106 }
1107 } else {
3bf2d28a 1108 /* if no lacpdu arrived and no timer is on */
1da177e4
LT
1109 switch (port->sm_rx_state) {
1110 case AD_RX_PORT_DISABLED:
7bfc4753 1111 if (port->sm_vars & AD_PORT_MOVED)
3bf2d28a 1112 port->sm_rx_state = AD_RX_INITIALIZE;
7bfc4753
BD
1113 else if (port->is_enabled
1114 && (port->sm_vars
1115 & AD_PORT_LACP_ENABLED))
3bf2d28a 1116 port->sm_rx_state = AD_RX_EXPIRED;
7bfc4753
BD
1117 else if (port->is_enabled
1118 && ((port->sm_vars
1119 & AD_PORT_LACP_ENABLED) == 0))
3bf2d28a 1120 port->sm_rx_state = AD_RX_LACP_DISABLED;
1da177e4 1121 break;
3bf2d28a 1122 default:
1da177e4
LT
1123 break;
1124
1125 }
1126 }
1127 }
1128
3bf2d28a 1129 /* check if the State machine was changed or new lacpdu arrived */
1da177e4 1130 if ((port->sm_rx_state != last_state) || (lacpdu)) {
63b46242
WK
1131 pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1132 port->actor_port_number,
1133 port->slave->dev->name,
1134 last_state,
a4aee5c8 1135 port->sm_rx_state);
1da177e4
LT
1136 switch (port->sm_rx_state) {
1137 case AD_RX_INITIALIZE:
cb8dda90 1138 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
1da177e4 1139 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
7bfc4753 1140 else
1da177e4 1141 port->sm_vars |= AD_PORT_LACP_ENABLED;
1da177e4
LT
1142 port->sm_vars &= ~AD_PORT_SELECTED;
1143 __record_default(port);
1144 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1145 port->sm_vars &= ~AD_PORT_MOVED;
3bf2d28a 1146 port->sm_rx_state = AD_RX_PORT_DISABLED;
1da177e4 1147
3bf2d28a 1148 /* Fall Through */
1da177e4
LT
1149 case AD_RX_PORT_DISABLED:
1150 port->sm_vars &= ~AD_PORT_MATCHED;
1151 break;
1152 case AD_RX_LACP_DISABLED:
1153 port->sm_vars &= ~AD_PORT_SELECTED;
1154 __record_default(port);
1055c9ab 1155 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1da177e4
LT
1156 port->sm_vars |= AD_PORT_MATCHED;
1157 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1158 break;
1159 case AD_RX_EXPIRED:
3bf2d28a
VF
1160 /* Reset of the Synchronization flag (Standard 43.4.12)
1161 * This reset cause to disable this port in the
1162 * COLLECTING_DISTRIBUTING state of the mux machine in
1163 * case of EXPIRED even if LINK_DOWN didn't arrive for
1164 * the port.
1165 */
1055c9ab 1166 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1da177e4 1167 port->sm_vars &= ~AD_PORT_MATCHED;
14c9551a 1168 port->partner_oper.port_state |= AD_STATE_LACP_TIMEOUT;
3bf2d28a 1169 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
1da177e4
LT
1170 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1171 port->actor_oper_port_state |= AD_STATE_EXPIRED;
ef015d72 1172 port->sm_vars |= AD_PORT_CHURNED;
1da177e4
LT
1173 break;
1174 case AD_RX_DEFAULTED:
1175 __update_default_selected(port);
1176 __record_default(port);
1177 port->sm_vars |= AD_PORT_MATCHED;
1178 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1179 break;
1180 case AD_RX_CURRENT:
815117ad 1181 /* detect loopback situation */
3bf2d28a
VF
1182 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1183 &(port->actor_system))) {
d4471f5e 1184 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
90194264 1185 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
3bf2d28a 1186 port->slave->dev->name);
1da177e4
LT
1187 return;
1188 }
1189 __update_selected(lacpdu, port);
1190 __update_ntt(lacpdu, port);
1191 __record_pdu(lacpdu, port);
1da177e4
LT
1192 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1193 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1da177e4 1194 break;
3bf2d28a 1195 default:
1da177e4
LT
1196 break;
1197 }
1198 }
1da177e4
LT
1199}
1200
14c9551a
MB
1201/**
1202 * ad_churn_machine - handle port churn's state machine
1203 * @port: the port we're looking at
1204 *
1205 */
1206static void ad_churn_machine(struct port *port)
1207{
ef015d72
MB
1208 if (port->sm_vars & AD_PORT_CHURNED) {
1209 port->sm_vars &= ~AD_PORT_CHURNED;
14c9551a
MB
1210 port->sm_churn_actor_state = AD_CHURN_MONITOR;
1211 port->sm_churn_partner_state = AD_CHURN_MONITOR;
1212 port->sm_churn_actor_timer_counter =
1213 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1214 port->sm_churn_partner_timer_counter =
1215 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1216 return;
1217 }
1218 if (port->sm_churn_actor_timer_counter &&
1219 !(--port->sm_churn_actor_timer_counter) &&
1220 port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1221 if (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION) {
1222 port->sm_churn_actor_state = AD_NO_CHURN;
1223 } else {
1224 port->churn_actor_count++;
1225 port->sm_churn_actor_state = AD_CHURN;
1226 }
1227 }
1228 if (port->sm_churn_partner_timer_counter &&
1229 !(--port->sm_churn_partner_timer_counter) &&
1230 port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1231 if (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) {
1232 port->sm_churn_partner_state = AD_NO_CHURN;
1233 } else {
1234 port->churn_partner_count++;
1235 port->sm_churn_partner_state = AD_CHURN;
1236 }
1237 }
1238}
1239
1da177e4
LT
1240/**
1241 * ad_tx_machine - handle a port's tx state machine
1242 * @port: the port we're looking at
1da177e4
LT
1243 */
1244static void ad_tx_machine(struct port *port)
1245{
3bf2d28a
VF
1246 /* check if tx timer expired, to verify that we do not send more than
1247 * 3 packets per second
1248 */
1da177e4 1249 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
3bf2d28a 1250 /* check if there is something to send */
1da177e4
LT
1251 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1252 __update_lacpdu_from_port(port);
d238d458 1253
1da177e4 1254 if (ad_lacpdu_send(port) >= 0) {
a4aee5c8
JP
1255 pr_debug("Sent LACPDU on port %d\n",
1256 port->actor_port_number);
d238d458 1257
3bf2d28a
VF
1258 /* mark ntt as false, so it will not be sent
1259 * again until demanded
1260 */
d238d458 1261 port->ntt = false;
1da177e4
LT
1262 }
1263 }
3bf2d28a
VF
1264 /* restart tx timer(to verify that we will not exceed
1265 * AD_MAX_TX_IN_SECOND
1266 */
1267 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1da177e4
LT
1268 }
1269}
1270
1271/**
1272 * ad_periodic_machine - handle a port's periodic state machine
1273 * @port: the port we're looking at
1274 *
1275 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1276 */
1277static void ad_periodic_machine(struct port *port)
1278{
1279 periodic_states_t last_state;
1280
3bf2d28a 1281 /* keep current state machine state to compare later if it was changed */
1da177e4
LT
1282 last_state = port->sm_periodic_state;
1283
3bf2d28a 1284 /* check if port was reinitialized */
1da177e4 1285 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1055c9ab 1286 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1da177e4 1287 ) {
3bf2d28a 1288 port->sm_periodic_state = AD_NO_PERIODIC;
1da177e4 1289 }
3bf2d28a 1290 /* check if state machine should change state */
1da177e4 1291 else if (port->sm_periodic_timer_counter) {
3bf2d28a 1292 /* check if periodic state machine expired */
1da177e4 1293 if (!(--port->sm_periodic_timer_counter)) {
3bf2d28a
VF
1294 /* if expired then do tx */
1295 port->sm_periodic_state = AD_PERIODIC_TX;
1da177e4 1296 } else {
3bf2d28a
VF
1297 /* If not expired, check if there is some new timeout
1298 * parameter from the partner state
1299 */
1da177e4
LT
1300 switch (port->sm_periodic_state) {
1301 case AD_FAST_PERIODIC:
7bfc4753
BD
1302 if (!(port->partner_oper.port_state
1303 & AD_STATE_LACP_TIMEOUT))
3bf2d28a 1304 port->sm_periodic_state = AD_SLOW_PERIODIC;
1da177e4
LT
1305 break;
1306 case AD_SLOW_PERIODIC:
1055c9ab 1307 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1da177e4 1308 port->sm_periodic_timer_counter = 0;
3bf2d28a 1309 port->sm_periodic_state = AD_PERIODIC_TX;
1da177e4
LT
1310 }
1311 break;
3bf2d28a 1312 default:
1da177e4
LT
1313 break;
1314 }
1315 }
1316 } else {
1317 switch (port->sm_periodic_state) {
1318 case AD_NO_PERIODIC:
3bf2d28a 1319 port->sm_periodic_state = AD_FAST_PERIODIC;
1da177e4
LT
1320 break;
1321 case AD_PERIODIC_TX:
3bf2d28a
VF
1322 if (!(port->partner_oper.port_state &
1323 AD_STATE_LACP_TIMEOUT))
1324 port->sm_periodic_state = AD_SLOW_PERIODIC;
7bfc4753 1325 else
3bf2d28a 1326 port->sm_periodic_state = AD_FAST_PERIODIC;
1da177e4 1327 break;
3bf2d28a 1328 default:
1da177e4
LT
1329 break;
1330 }
1331 }
1332
3bf2d28a 1333 /* check if the state machine was changed */
1da177e4 1334 if (port->sm_periodic_state != last_state) {
a4aee5c8
JP
1335 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1336 port->actor_port_number, last_state,
1337 port->sm_periodic_state);
1da177e4
LT
1338 switch (port->sm_periodic_state) {
1339 case AD_NO_PERIODIC:
3bf2d28a 1340 port->sm_periodic_timer_counter = 0;
1da177e4
LT
1341 break;
1342 case AD_FAST_PERIODIC:
3bf2d28a
VF
1343 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1344 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1da177e4
LT
1345 break;
1346 case AD_SLOW_PERIODIC:
3bf2d28a
VF
1347 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1348 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1da177e4
LT
1349 break;
1350 case AD_PERIODIC_TX:
d238d458 1351 port->ntt = true;
1da177e4 1352 break;
3bf2d28a 1353 default:
1da177e4
LT
1354 break;
1355 }
1356 }
1357}
1358
1359/**
1360 * ad_port_selection_logic - select aggregation groups
1361 * @port: the port we're looking at
ee637714 1362 * @update_slave_arr: Does slave array need update?
1da177e4
LT
1363 *
1364 * Select aggregation groups, and assign each port for it's aggregetor. The
1365 * selection logic is called in the inititalization (after all the handshkes),
1366 * and after every lacpdu receive (if selected is off).
1367 */
ee637714 1368static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
1da177e4
LT
1369{
1370 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1371 struct port *last_port = NULL, *curr_port;
3e36bb75
VF
1372 struct list_head *iter;
1373 struct bonding *bond;
1374 struct slave *slave;
1da177e4
LT
1375 int found = 0;
1376
3bf2d28a 1377 /* if the port is already Selected, do nothing */
7bfc4753 1378 if (port->sm_vars & AD_PORT_SELECTED)
1da177e4 1379 return;
1da177e4 1380
3e36bb75
VF
1381 bond = __get_bond_by_port(port);
1382
3bf2d28a 1383 /* if the port is connected to other aggregator, detach it */
1da177e4 1384 if (port->aggregator) {
3bf2d28a 1385 /* detach the port from its former aggregator */
128ea6c3
BD
1386 temp_aggregator = port->aggregator;
1387 for (curr_port = temp_aggregator->lag_ports; curr_port;
1388 last_port = curr_port,
3bf2d28a 1389 curr_port = curr_port->next_port_in_aggregator) {
1da177e4
LT
1390 if (curr_port == port) {
1391 temp_aggregator->num_of_ports--;
3bf2d28a
VF
1392 /* if it is the first port attached to the
1393 * aggregator
1394 */
1395 if (!last_port) {
128ea6c3
BD
1396 temp_aggregator->lag_ports =
1397 port->next_port_in_aggregator;
3bf2d28a
VF
1398 } else {
1399 /* not the first port attached to the
1400 * aggregator
1401 */
128ea6c3
BD
1402 last_port->next_port_in_aggregator =
1403 port->next_port_in_aggregator;
1da177e4
LT
1404 }
1405
3bf2d28a
VF
1406 /* clear the port's relations to this
1407 * aggregator
1408 */
1da177e4 1409 port->aggregator = NULL;
128ea6c3
BD
1410 port->next_port_in_aggregator = NULL;
1411 port->actor_port_aggregator_identifier = 0;
1da177e4 1412
d4471f5e
VF
1413 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1414 port->actor_port_number,
1415 temp_aggregator->aggregator_identifier);
3bf2d28a
VF
1416 /* if the aggregator is empty, clear its
1417 * parameters, and set it ready to be attached
1418 */
7bfc4753 1419 if (!temp_aggregator->lag_ports)
1da177e4 1420 ad_clear_agg(temp_aggregator);
1da177e4
LT
1421 break;
1422 }
1423 }
3bf2d28a
VF
1424 if (!curr_port) {
1425 /* meaning: the port was related to an aggregator
1426 * but was not on the aggregator port list
1427 */
d4471f5e
VF
1428 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1429 port->slave->bond->dev->name,
1430 port->actor_port_number,
1431 port->slave->dev->name,
1432 port->aggregator->aggregator_identifier);
1da177e4
LT
1433 }
1434 }
3bf2d28a 1435 /* search on all aggregators for a suitable aggregator for this port */
3e36bb75 1436 bond_for_each_slave(bond, slave, iter) {
3fdddd85 1437 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1da177e4 1438
3bf2d28a 1439 /* keep a free aggregator for later use(if needed) */
1da177e4 1440 if (!aggregator->lag_ports) {
7bfc4753 1441 if (!free_aggregator)
128ea6c3 1442 free_aggregator = aggregator;
1da177e4
LT
1443 continue;
1444 }
815117ad 1445 /* check if current aggregator suits us */
1446 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1447 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1055c9ab
HE
1448 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1449 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1da177e4 1450 ) &&
815117ad 1451 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1452 !aggregator->is_individual) /* but is not individual OR */
1da177e4
LT
1453 )
1454 ) {
815117ad 1455 /* attach to the founded aggregator */
1da177e4 1456 port->aggregator = aggregator;
128ea6c3
BD
1457 port->actor_port_aggregator_identifier =
1458 port->aggregator->aggregator_identifier;
1459 port->next_port_in_aggregator = aggregator->lag_ports;
1da177e4 1460 port->aggregator->num_of_ports++;
128ea6c3 1461 aggregator->lag_ports = port;
d4471f5e
VF
1462 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1463 port->actor_port_number,
1464 port->aggregator->aggregator_identifier);
1da177e4 1465
3bf2d28a 1466 /* mark this port as selected */
1da177e4
LT
1467 port->sm_vars |= AD_PORT_SELECTED;
1468 found = 1;
1469 break;
1470 }
1471 }
1472
3bf2d28a
VF
1473 /* the port couldn't find an aggregator - attach it to a new
1474 * aggregator
1475 */
1da177e4
LT
1476 if (!found) {
1477 if (free_aggregator) {
3bf2d28a 1478 /* assign port a new aggregator */
1da177e4 1479 port->aggregator = free_aggregator;
128ea6c3
BD
1480 port->actor_port_aggregator_identifier =
1481 port->aggregator->aggregator_identifier;
1da177e4 1482
3bf2d28a
VF
1483 /* update the new aggregator's parameters
1484 * if port was responsed from the end-user
1485 */
cb8dda90 1486 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
7bfc4753 1487 /* if port is full duplex */
1624db7b 1488 port->aggregator->is_individual = false;
7bfc4753 1489 else
1624db7b 1490 port->aggregator->is_individual = true;
1da177e4 1491
c3cd9ee1
MB
1492 port->aggregator->actor_admin_aggregator_key =
1493 port->actor_admin_port_key;
1494 port->aggregator->actor_oper_aggregator_key =
1495 port->actor_oper_port_key;
128ea6c3
BD
1496 port->aggregator->partner_system =
1497 port->partner_oper.system;
1498 port->aggregator->partner_system_priority =
1499 port->partner_oper.system_priority;
1055c9ab 1500 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1da177e4
LT
1501 port->aggregator->receive_state = 1;
1502 port->aggregator->transmit_state = 1;
1503 port->aggregator->lag_ports = port;
1504 port->aggregator->num_of_ports++;
1505
3bf2d28a 1506 /* mark this port as selected */
1da177e4
LT
1507 port->sm_vars |= AD_PORT_SELECTED;
1508
d4471f5e
VF
1509 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1510 port->actor_port_number,
1511 port->aggregator->aggregator_identifier);
1da177e4 1512 } else {
d4471f5e 1513 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
1da177e4
LT
1514 port->actor_port_number, port->slave->dev->name);
1515 }
1516 }
3bf2d28a
VF
1517 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1518 * in all aggregator's ports, else set ready=FALSE in all
1519 * aggregator's ports
1520 */
1521 __set_agg_ports_ready(port->aggregator,
1522 __agg_ports_are_ready(port->aggregator));
1da177e4 1523
fd989c83 1524 aggregator = __get_first_agg(port);
ee637714 1525 ad_agg_selection_logic(aggregator, update_slave_arr);
63b46242
WK
1526
1527 if (!port->aggregator->is_active)
1528 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
fd989c83
JV
1529}
1530
3bf2d28a 1531/* Decide if "agg" is a better choice for the new active aggregator that
fd989c83
JV
1532 * the current best, according to the ad_select policy.
1533 */
1534static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1535 struct aggregator *curr)
1536{
3bf2d28a 1537 /* 0. If no best, select current.
fd989c83
JV
1538 *
1539 * 1. If the current agg is not individual, and the best is
1540 * individual, select current.
1541 *
1542 * 2. If current agg is individual and the best is not, keep best.
1543 *
1544 * 3. Therefore, current and best are both individual or both not
1545 * individual, so:
1546 *
1547 * 3a. If current agg partner replied, and best agg partner did not,
1548 * select current.
1549 *
1550 * 3b. If current agg partner did not reply and best agg partner
1551 * did reply, keep best.
1552 *
1553 * 4. Therefore, current and best both have partner replies or
1554 * both do not, so perform selection policy:
1555 *
1556 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1557 * select by bandwidth.
1558 *
1559 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1560 */
1561 if (!best)
1562 return curr;
1563
1564 if (!curr->is_individual && best->is_individual)
1565 return curr;
1566
1567 if (curr->is_individual && !best->is_individual)
1568 return best;
1569
1570 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1571 return curr;
1572
1573 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1574 return best;
1575
1576 switch (__get_agg_selection_mode(curr->lag_ports)) {
1577 case BOND_AD_COUNT:
0622cab0 1578 if (__agg_active_ports(curr) > __agg_active_ports(best))
fd989c83
JV
1579 return curr;
1580
0622cab0 1581 if (__agg_active_ports(curr) < __agg_active_ports(best))
fd989c83
JV
1582 return best;
1583
1584 /*FALLTHROUGH*/
1585 case BOND_AD_STABLE:
1586 case BOND_AD_BANDWIDTH:
1587 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1588 return curr;
1589
1590 break;
1591
1592 default:
d4471f5e
VF
1593 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1594 curr->slave->bond->dev->name,
1595 __get_agg_selection_mode(curr->lag_ports));
fd989c83 1596 break;
1da177e4 1597 }
fd989c83
JV
1598
1599 return best;
1da177e4
LT
1600}
1601
4cd6fe1c
SH
1602static int agg_device_up(const struct aggregator *agg)
1603{
2430af8b 1604 struct port *port = agg->lag_ports;
3bf2d28a 1605
2430af8b
JB
1606 if (!port)
1607 return 0;
3bf2d28a 1608
0622cab0
JV
1609 for (port = agg->lag_ports; port;
1610 port = port->next_port_in_aggregator) {
1611 if (netif_running(port->slave->dev) &&
1612 netif_carrier_ok(port->slave->dev))
1613 return 1;
1614 }
1615
1616 return 0;
4cd6fe1c
SH
1617}
1618
1da177e4
LT
1619/**
1620 * ad_agg_selection_logic - select an aggregation group for a team
1621 * @aggregator: the aggregator we're looking at
ee637714 1622 * @update_slave_arr: Does slave array need update?
1da177e4
LT
1623 *
1624 * It is assumed that only one aggregator may be selected for a team.
fd989c83
JV
1625 *
1626 * The logic of this function is to select the aggregator according to
1627 * the ad_select policy:
1628 *
1629 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1630 * it, and to reselect the active aggregator only if the previous
1631 * aggregator has no more ports related to it.
1632 *
1633 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1634 * bandwidth, and reselect whenever a link state change takes place or the
1635 * set of slaves in the bond changes.
1636 *
1637 * BOND_AD_COUNT: select the aggregator with largest number of ports
1638 * (slaves), and reselect whenever a link state change takes place or the
1639 * set of slaves in the bond changes.
1da177e4
LT
1640 *
1641 * FIXME: this function MUST be called with the first agg in the bond, or
1642 * __get_active_agg() won't work correctly. This function should be better
1643 * called with the bond itself, and retrieve the first agg from it.
1644 */
ee637714
MB
1645static void ad_agg_selection_logic(struct aggregator *agg,
1646 bool *update_slave_arr)
1da177e4 1647{
fd989c83 1648 struct aggregator *best, *active, *origin;
bef1fcce
VF
1649 struct bonding *bond = agg->slave->bond;
1650 struct list_head *iter;
1651 struct slave *slave;
1da177e4 1652 struct port *port;
1da177e4 1653
49b7624e 1654 rcu_read_lock();
fd989c83 1655 origin = agg;
fd989c83 1656 active = __get_active_agg(agg);
4cd6fe1c 1657 best = (active && agg_device_up(active)) ? active : NULL;
1da177e4 1658
be79bd04 1659 bond_for_each_slave_rcu(bond, slave, iter) {
3fdddd85 1660 agg = &(SLAVE_AD_INFO(slave)->aggregator);
bef1fcce 1661
fd989c83
JV
1662 agg->is_active = 0;
1663
0622cab0 1664 if (__agg_active_ports(agg) && agg_device_up(agg))
fd989c83 1665 best = ad_agg_selection_test(best, agg);
bef1fcce 1666 }
fd989c83
JV
1667
1668 if (best &&
1669 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
3bf2d28a 1670 /* For the STABLE policy, don't replace the old active
fd989c83
JV
1671 * aggregator if it's still active (it has an answering
1672 * partner) or if both the best and active don't have an
1673 * answering partner.
1674 */
1675 if (active && active->lag_ports &&
0622cab0 1676 __agg_active_ports(active) &&
fd989c83 1677 (__agg_has_partner(active) ||
3bf2d28a
VF
1678 (!__agg_has_partner(active) &&
1679 !__agg_has_partner(best)))) {
fd989c83
JV
1680 if (!(!active->actor_oper_aggregator_key &&
1681 best->actor_oper_aggregator_key)) {
1682 best = NULL;
1683 active->is_active = 1;
1da177e4
LT
1684 }
1685 }
fd989c83 1686 }
1da177e4 1687
fd989c83
JV
1688 if (best && (best == active)) {
1689 best = NULL;
1690 active->is_active = 1;
1da177e4
LT
1691 }
1692
be79bd04 1693 /* if there is new best aggregator, activate it */
fd989c83 1694 if (best) {
d4471f5e
VF
1695 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1696 best->aggregator_identifier, best->num_of_ports,
1697 best->actor_oper_aggregator_key,
1698 best->partner_oper_aggregator_key,
1699 best->is_individual, best->is_active);
1700 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1701 best->lag_ports, best->slave,
1702 best->slave ? best->slave->dev->name : "NULL");
fd989c83 1703
be79bd04 1704 bond_for_each_slave_rcu(bond, slave, iter) {
3fdddd85 1705 agg = &(SLAVE_AD_INFO(slave)->aggregator);
fd989c83 1706
d4471f5e
VF
1707 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1708 agg->aggregator_identifier, agg->num_of_ports,
1709 agg->actor_oper_aggregator_key,
1710 agg->partner_oper_aggregator_key,
1711 agg->is_individual, agg->is_active);
1da177e4
LT
1712 }
1713
be79bd04 1714 /* check if any partner replys */
fd989c83 1715 if (best->is_individual) {
d4471f5e
VF
1716 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1717 best->slave ?
1718 best->slave->bond->dev->name : "NULL");
1da177e4
LT
1719 }
1720
fd989c83 1721 best->is_active = 1;
d4471f5e
VF
1722 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1723 best->aggregator_identifier);
1724 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1725 best->aggregator_identifier, best->num_of_ports,
1726 best->actor_oper_aggregator_key,
1727 best->partner_oper_aggregator_key,
1728 best->is_individual, best->is_active);
1da177e4 1729
3bf2d28a
VF
1730 /* disable the ports that were related to the former
1731 * active_aggregator
1732 */
fd989c83
JV
1733 if (active) {
1734 for (port = active->lag_ports; port;
1735 port = port->next_port_in_aggregator) {
1da177e4
LT
1736 __disable_port(port);
1737 }
1738 }
ee637714
MB
1739 /* Slave array needs update. */
1740 *update_slave_arr = true;
1da177e4
LT
1741 }
1742
3bf2d28a 1743 /* if the selected aggregator is of join individuals
fd989c83
JV
1744 * (partner_system is NULL), enable their ports
1745 */
1746 active = __get_active_agg(origin);
1da177e4 1747
fd989c83
JV
1748 if (active) {
1749 if (!__agg_has_partner(active)) {
1750 for (port = active->lag_ports; port;
1751 port = port->next_port_in_aggregator) {
1da177e4
LT
1752 __enable_port(port);
1753 }
1754 }
1755 }
fd989c83 1756
be79bd04 1757 rcu_read_unlock();
1758
bef1fcce 1759 bond_3ad_set_carrier(bond);
1da177e4
LT
1760}
1761
1762/**
1763 * ad_clear_agg - clear a given aggregator's parameters
1764 * @aggregator: the aggregator we're looking at
1da177e4
LT
1765 */
1766static void ad_clear_agg(struct aggregator *aggregator)
1767{
1768 if (aggregator) {
1624db7b 1769 aggregator->is_individual = false;
1da177e4
LT
1770 aggregator->actor_admin_aggregator_key = 0;
1771 aggregator->actor_oper_aggregator_key = 0;
f87fda00 1772 eth_zero_addr(aggregator->partner_system.mac_addr_value);
1da177e4
LT
1773 aggregator->partner_system_priority = 0;
1774 aggregator->partner_oper_aggregator_key = 0;
1775 aggregator->receive_state = 0;
1776 aggregator->transmit_state = 0;
1777 aggregator->lag_ports = NULL;
1778 aggregator->is_active = 0;
1779 aggregator->num_of_ports = 0;
a4aee5c8
JP
1780 pr_debug("LAG %d was cleared\n",
1781 aggregator->aggregator_identifier);
1da177e4
LT
1782 }
1783}
1784
1785/**
1786 * ad_initialize_agg - initialize a given aggregator's parameters
1787 * @aggregator: the aggregator we're looking at
1da177e4
LT
1788 */
1789static void ad_initialize_agg(struct aggregator *aggregator)
1790{
1791 if (aggregator) {
1792 ad_clear_agg(aggregator);
1793
f87fda00 1794 eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1da177e4
LT
1795 aggregator->aggregator_identifier = 0;
1796 aggregator->slave = NULL;
1797 }
1798}
1799
1800/**
1801 * ad_initialize_port - initialize a given port's parameters
1802 * @aggregator: the aggregator we're looking at
1803 * @lacp_fast: boolean. whether fast periodic should be used
1da177e4
LT
1804 */
1805static void ad_initialize_port(struct port *port, int lacp_fast)
1806{
c7e703d0
HE
1807 static const struct port_params tmpl = {
1808 .system_priority = 0xffff,
1809 .key = 1,
1810 .port_number = 1,
1811 .port_priority = 0xff,
1812 .port_state = 1,
1813 };
7addeef6
HE
1814 static const struct lacpdu lacpdu = {
1815 .subtype = 0x01,
1816 .version_number = 0x01,
1817 .tlv_type_actor_info = 0x01,
1818 .actor_information_length = 0x14,
1819 .tlv_type_partner_info = 0x02,
1820 .partner_information_length = 0x14,
1821 .tlv_type_collector_info = 0x03,
1822 .collector_information_length = 0x10,
1823 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1824 };
c7e703d0 1825
1da177e4 1826 if (port) {
1da177e4 1827 port->actor_port_priority = 0xff;
1da177e4 1828 port->actor_port_aggregator_identifier = 0;
d238d458 1829 port->ntt = false;
3bf2d28a
VF
1830 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1831 AD_STATE_LACP_ACTIVITY;
1832 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1833 AD_STATE_LACP_ACTIVITY;
1da177e4 1834
7bfc4753 1835 if (lacp_fast)
1da177e4 1836 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1da177e4 1837
c7e703d0
HE
1838 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1839 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1840
f48127b6 1841 port->is_enabled = true;
3bf2d28a 1842 /* private parameters */
19a12049 1843 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1da177e4
LT
1844 port->sm_rx_state = 0;
1845 port->sm_rx_timer_counter = 0;
1846 port->sm_periodic_state = 0;
1847 port->sm_periodic_timer_counter = 0;
1848 port->sm_mux_state = 0;
1849 port->sm_mux_timer_counter = 0;
1850 port->sm_tx_state = 0;
1da177e4
LT
1851 port->aggregator = NULL;
1852 port->next_port_in_aggregator = NULL;
1853 port->transaction_id = 0;
1854
14c9551a
MB
1855 port->sm_churn_actor_timer_counter = 0;
1856 port->sm_churn_actor_state = 0;
1857 port->churn_actor_count = 0;
1858 port->sm_churn_partner_timer_counter = 0;
1859 port->sm_churn_partner_state = 0;
1860 port->churn_partner_count = 0;
1861
7addeef6 1862 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1da177e4
LT
1863 }
1864}
1865
1866/**
1867 * ad_enable_collecting_distributing - enable a port's transmit/receive
1868 * @port: the port we're looking at
ee637714 1869 * @update_slave_arr: Does slave array need update?
1da177e4
LT
1870 *
1871 * Enable @port if it's in an active aggregator
1872 */
ee637714
MB
1873static void ad_enable_collecting_distributing(struct port *port,
1874 bool *update_slave_arr)
1da177e4
LT
1875{
1876 if (port->aggregator->is_active) {
a4aee5c8
JP
1877 pr_debug("Enabling port %d(LAG %d)\n",
1878 port->actor_port_number,
1879 port->aggregator->aggregator_identifier);
1da177e4 1880 __enable_port(port);
ee637714
MB
1881 /* Slave array needs update */
1882 *update_slave_arr = true;
1da177e4
LT
1883 }
1884}
1885
1886/**
1887 * ad_disable_collecting_distributing - disable a port's transmit/receive
1888 * @port: the port we're looking at
ee637714 1889 * @update_slave_arr: Does slave array need update?
1da177e4 1890 */
ee637714
MB
1891static void ad_disable_collecting_distributing(struct port *port,
1892 bool *update_slave_arr)
1da177e4 1893{
3bf2d28a
VF
1894 if (port->aggregator &&
1895 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1896 &(null_mac_addr))) {
a4aee5c8
JP
1897 pr_debug("Disabling port %d(LAG %d)\n",
1898 port->actor_port_number,
1899 port->aggregator->aggregator_identifier);
1da177e4 1900 __disable_port(port);
ee637714
MB
1901 /* Slave array needs an update */
1902 *update_slave_arr = true;
1da177e4
LT
1903 }
1904}
1905
1da177e4
LT
1906/**
1907 * ad_marker_info_received - handle receive of a Marker information frame
1908 * @marker_info: Marker info received
1909 * @port: the port we're looking at
1da177e4 1910 */
1c3f0b8e
MD
1911static void ad_marker_info_received(struct bond_marker *marker_info,
1912 struct port *port)
1da177e4 1913{
1c3f0b8e 1914 struct bond_marker marker;
1da177e4 1915
3bf2d28a 1916 /* copy the received marker data to the response marker */
1c3f0b8e 1917 memcpy(&marker, marker_info, sizeof(struct bond_marker));
3bf2d28a 1918 /* change the marker subtype to marker response */
128ea6c3 1919 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1da177e4 1920
3bf2d28a 1921 /* send the marker response */
1da177e4 1922 if (ad_marker_send(port, &marker) >= 0) {
a4aee5c8
JP
1923 pr_debug("Sent Marker Response on port %d\n",
1924 port->actor_port_number);
1da177e4
LT
1925 }
1926}
1927
1928/**
1929 * ad_marker_response_received - handle receive of a marker response frame
1930 * @marker: marker PDU received
1931 * @port: the port we're looking at
1932 *
1933 * This function does nothing since we decided not to implement send and handle
1934 * response for marker PDU's, in this stage, but only to respond to marker
1935 * information.
1936 */
1c3f0b8e 1937static void ad_marker_response_received(struct bond_marker *marker,
3bf2d28a 1938 struct port *port)
1da177e4 1939{
3bf2d28a 1940 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1da177e4
LT
1941}
1942
3bf2d28a 1943/* ========= AD exported functions to the main bonding code ========= */
1da177e4 1944
3bf2d28a 1945/* Check aggregators status in team every T seconds */
1da177e4
LT
1946#define AD_AGGREGATOR_SELECTION_TIMER 8
1947
3bf2d28a
VF
1948/**
1949 * bond_3ad_initiate_agg_selection - initate aggregator selection
1950 * @bond: bonding struct
fd989c83
JV
1951 *
1952 * Set the aggregation selection timer, to initiate an agg selection in
1953 * the very near future. Called during first initialization, and during
1954 * any down to up transitions of the bond.
1955 */
1956void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1957{
1958 BOND_AD_INFO(bond).agg_select_timer = timeout;
fd989c83
JV
1959}
1960
1da177e4
LT
1961/**
1962 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1963 * @bond: bonding struct to work on
1964 * @tick_resolution: tick duration (millisecond resolution)
1da177e4
LT
1965 *
1966 * Can be called only after the mac address of the bond is set.
1967 */
56d00c67 1968void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
3a6d54c5 1969{
815117ad 1970 /* check that the bond is not initialized yet */
1971 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
3a6d54c5 1972 bond->dev->dev_addr)) {
1da177e4 1973
163c8ff3 1974 BOND_AD_INFO(bond).aggregator_identifier = 0;
1da177e4 1975
6791e466
MB
1976 BOND_AD_INFO(bond).system.sys_priority =
1977 bond->params.ad_actor_sys_prio;
74514957
MB
1978 if (is_zero_ether_addr(bond->params.ad_actor_system))
1979 BOND_AD_INFO(bond).system.sys_mac_addr =
1980 *((struct mac_addr *)bond->dev->dev_addr);
1981 else
1982 BOND_AD_INFO(bond).system.sys_mac_addr =
1983 *((struct mac_addr *)bond->params.ad_actor_system);
1da177e4 1984
3bf2d28a
VF
1985 /* initialize how many times this module is called in one
1986 * second (should be about every 100ms)
1987 */
1da177e4
LT
1988 ad_ticks_per_sec = tick_resolution;
1989
fd989c83
JV
1990 bond_3ad_initiate_agg_selection(bond,
1991 AD_AGGREGATOR_SELECTION_TIMER *
1992 ad_ticks_per_sec);
1da177e4
LT
1993 }
1994}
1995
1996/**
1997 * bond_3ad_bind_slave - initialize a slave's port
1998 * @slave: slave struct to work on
1999 *
2000 * Returns: 0 on success
2001 * < 0 on error
2002 */
359632e5 2003void bond_3ad_bind_slave(struct slave *slave)
1da177e4
LT
2004{
2005 struct bonding *bond = bond_get_bond_by_slave(slave);
2006 struct port *port;
2007 struct aggregator *aggregator;
2008
359632e5 2009 /* check that the slave has not been initialized yet. */
3fdddd85 2010 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
1da177e4 2011
359632e5 2012 /* port initialization */
3fdddd85 2013 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4 2014
bf0239a9 2015 ad_initialize_port(port, bond->params.lacp_fast);
1da177e4
LT
2016
2017 port->slave = slave;
3fdddd85 2018 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
d22a5fc0
MB
2019 /* key is determined according to the link speed, duplex and
2020 * user key
359632e5 2021 */
d22a5fc0 2022 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
7bb11dc9 2023 ad_update_actor_keys(port, false);
359632e5 2024 /* actor system is the bond's system */
5ee14e6d 2025 __ad_actor_update_port(port);
3bf2d28a
VF
2026 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2027 * lacpdu's are sent in one second)
2028 */
1da177e4 2029 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1da177e4
LT
2030
2031 __disable_port(port);
1da177e4 2032
359632e5 2033 /* aggregator initialization */
3fdddd85 2034 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1da177e4
LT
2035
2036 ad_initialize_agg(aggregator);
2037
2038 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
163c8ff3 2039 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
1da177e4
LT
2040 aggregator->slave = slave;
2041 aggregator->is_active = 0;
2042 aggregator->num_of_ports = 0;
2043 }
1da177e4
LT
2044}
2045
2046/**
2047 * bond_3ad_unbind_slave - deinitialize a slave's port
2048 * @slave: slave struct to work on
2049 *
2050 * Search for the aggregator that is related to this port, remove the
2051 * aggregator and assign another aggregator for other port related to it
2052 * (if any), and remove the port.
2053 */
2054void bond_3ad_unbind_slave(struct slave *slave)
2055{
2056 struct port *port, *prev_port, *temp_port;
2057 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2058 int select_new_active_agg = 0;
0b088264
VF
2059 struct bonding *bond = slave->bond;
2060 struct slave *slave_iter;
2061 struct list_head *iter;
ee637714 2062 bool dummy_slave_update; /* Ignore this value as caller updates array */
a361c83c 2063
e470259f
NA
2064 /* Sync against bond_3ad_state_machine_handler() */
2065 spin_lock_bh(&bond->mode_lock);
3fdddd85 2066 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2067 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4 2068
3bf2d28a 2069 /* if slave is null, the whole port is not initialized */
1da177e4 2070 if (!port->slave) {
d4471f5e
VF
2071 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
2072 slave->dev->name);
e470259f 2073 goto out;
1da177e4
LT
2074 }
2075
d4471f5e
VF
2076 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
2077 aggregator->aggregator_identifier);
1da177e4
LT
2078
2079 /* Tell the partner that this port is not suitable for aggregation */
2080 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2081 __update_lacpdu_from_port(port);
2082 ad_lacpdu_send(port);
2083
3bf2d28a 2084 /* check if this aggregator is occupied */
1da177e4 2085 if (aggregator->lag_ports) {
3bf2d28a
VF
2086 /* check if there are other ports related to this aggregator
2087 * except the port related to this slave(thats ensure us that
2088 * there is a reason to search for new aggregator, and that we
2089 * will find one
2090 */
2091 if ((aggregator->lag_ports != port) ||
2092 (aggregator->lag_ports->next_port_in_aggregator)) {
2093 /* find new aggregator for the related port(s) */
0b088264 2094 bond_for_each_slave(bond, slave_iter, iter) {
3fdddd85 2095 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
3bf2d28a
VF
2096 /* if the new aggregator is empty, or it is
2097 * connected to our port only
2098 */
2099 if (!new_aggregator->lag_ports ||
2100 ((new_aggregator->lag_ports == port) &&
2101 !new_aggregator->lag_ports->next_port_in_aggregator))
1da177e4 2102 break;
1da177e4 2103 }
0b088264
VF
2104 if (!slave_iter)
2105 new_aggregator = NULL;
3bf2d28a
VF
2106
2107 /* if new aggregator found, copy the aggregator's
2108 * parameters and connect the related lag_ports to the
2109 * new aggregator
2110 */
1da177e4 2111 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
d4471f5e
VF
2112 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2113 aggregator->aggregator_identifier,
2114 new_aggregator->aggregator_identifier);
1da177e4 2115
3bf2d28a
VF
2116 if ((new_aggregator->lag_ports == port) &&
2117 new_aggregator->is_active) {
d4471f5e 2118 netdev_info(bond->dev, "Removing an active aggregator\n");
1da177e4
LT
2119 select_new_active_agg = 1;
2120 }
2121
2122 new_aggregator->is_individual = aggregator->is_individual;
2123 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2124 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2125 new_aggregator->partner_system = aggregator->partner_system;
2126 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2127 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2128 new_aggregator->receive_state = aggregator->receive_state;
2129 new_aggregator->transmit_state = aggregator->transmit_state;
2130 new_aggregator->lag_ports = aggregator->lag_ports;
2131 new_aggregator->is_active = aggregator->is_active;
2132 new_aggregator->num_of_ports = aggregator->num_of_ports;
2133
3bf2d28a
VF
2134 /* update the information that is written on
2135 * the ports about the aggregator
2136 */
128ea6c3
BD
2137 for (temp_port = aggregator->lag_ports; temp_port;
2138 temp_port = temp_port->next_port_in_aggregator) {
2139 temp_port->aggregator = new_aggregator;
1da177e4
LT
2140 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2141 }
2142
1da177e4 2143 ad_clear_agg(aggregator);
a361c83c 2144
7bfc4753 2145 if (select_new_active_agg)
ee637714
MB
2146 ad_agg_selection_logic(__get_first_agg(port),
2147 &dummy_slave_update);
1da177e4 2148 } else {
d4471f5e 2149 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
1da177e4 2150 }
3bf2d28a
VF
2151 } else {
2152 /* in case that the only port related to this
2153 * aggregator is the one we want to remove
2154 */
1da177e4 2155 select_new_active_agg = aggregator->is_active;
1da177e4
LT
2156 ad_clear_agg(aggregator);
2157 if (select_new_active_agg) {
d4471f5e 2158 netdev_info(bond->dev, "Removing an active aggregator\n");
3bf2d28a 2159 /* select new active aggregator */
74684493
VF
2160 temp_aggregator = __get_first_agg(port);
2161 if (temp_aggregator)
ee637714
MB
2162 ad_agg_selection_logic(temp_aggregator,
2163 &dummy_slave_update);
1da177e4
LT
2164 }
2165 }
2166 }
2167
d4471f5e 2168 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
3bf2d28a
VF
2169
2170 /* find the aggregator that this port is connected to */
0b088264 2171 bond_for_each_slave(bond, slave_iter, iter) {
3fdddd85 2172 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
1da177e4 2173 prev_port = NULL;
3bf2d28a 2174 /* search the port in the aggregator's related ports */
128ea6c3
BD
2175 for (temp_port = temp_aggregator->lag_ports; temp_port;
2176 prev_port = temp_port,
3bf2d28a
VF
2177 temp_port = temp_port->next_port_in_aggregator) {
2178 if (temp_port == port) {
2179 /* the aggregator found - detach the port from
2180 * this aggregator
2181 */
7bfc4753 2182 if (prev_port)
1da177e4 2183 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
7bfc4753 2184 else
1da177e4 2185 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
1da177e4 2186 temp_aggregator->num_of_ports--;
0622cab0 2187 if (__agg_active_ports(temp_aggregator) == 0) {
1da177e4 2188 select_new_active_agg = temp_aggregator->is_active;
1da177e4
LT
2189 ad_clear_agg(temp_aggregator);
2190 if (select_new_active_agg) {
d4471f5e 2191 netdev_info(bond->dev, "Removing an active aggregator\n");
3bf2d28a 2192 /* select new active aggregator */
ee637714
MB
2193 ad_agg_selection_logic(__get_first_agg(port),
2194 &dummy_slave_update);
1da177e4
LT
2195 }
2196 }
2197 break;
2198 }
2199 }
2200 }
128ea6c3 2201 port->slave = NULL;
e470259f
NA
2202
2203out:
2204 spin_unlock_bh(&bond->mode_lock);
1da177e4
LT
2205}
2206
5ee14e6d
NA
2207/**
2208 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2209 * @bond: bonding struct to work on
2210 *
2211 * If an ad_actor setting gets changed we need to update the individual port
2212 * settings so the bond device will use the new values when it gets upped.
2213 */
2214void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2215{
2216 struct list_head *iter;
2217 struct slave *slave;
2218
2219 ASSERT_RTNL();
2220
2221 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2222 if (is_zero_ether_addr(bond->params.ad_actor_system))
2223 BOND_AD_INFO(bond).system.sys_mac_addr =
2224 *((struct mac_addr *)bond->dev->dev_addr);
2225 else
2226 BOND_AD_INFO(bond).system.sys_mac_addr =
2227 *((struct mac_addr *)bond->params.ad_actor_system);
2228
2229 spin_lock_bh(&bond->mode_lock);
7f20cd25
NA
2230 bond_for_each_slave(bond, slave, iter) {
2231 struct port *port = &(SLAVE_AD_INFO(slave))->port;
2232
2233 __ad_actor_update_port(port);
2234 port->ntt = true;
2235 }
5ee14e6d
NA
2236 spin_unlock_bh(&bond->mode_lock);
2237}
2238
1da177e4
LT
2239/**
2240 * bond_3ad_state_machine_handler - handle state machines timeout
2241 * @bond: bonding struct to work on
2242 *
2243 * The state machine handling concept in this module is to check every tick
2244 * which state machine should operate any function. The execution order is
2245 * round robin, so when we have an interaction between state machines, the
2246 * reply of one to each other might be delayed until next tick.
2247 *
2248 * This function also complete the initialization when the agg_select_timer
2249 * times out, and it selects an aggregator for the ports that are yet not
2250 * related to any aggregator, and selects the active aggregator for a bond.
2251 */
1b76b316 2252void bond_3ad_state_machine_handler(struct work_struct *work)
1da177e4 2253{
1b76b316
JV
2254 struct bonding *bond = container_of(work, struct bonding,
2255 ad_work.work);
1da177e4 2256 struct aggregator *aggregator;
3c4c88a1
VF
2257 struct list_head *iter;
2258 struct slave *slave;
2259 struct port *port;
5e5b0665 2260 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
ee637714 2261 bool update_slave_arr = false;
1da177e4 2262
e470259f
NA
2263 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2264 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2265 * concurrently due to incoming LACPDU as well.
2266 */
b7435628 2267 spin_lock_bh(&bond->mode_lock);
be79bd04 2268 rcu_read_lock();
1f2cd845 2269
be79bd04 2270 /* check if there are any slaves */
0965a1f3 2271 if (!bond_has_slaves(bond))
1da177e4 2272 goto re_arm;
1da177e4 2273
be79bd04 2274 /* check if agg_select_timer timer after initialize is timed out */
3bf2d28a
VF
2275 if (BOND_AD_INFO(bond).agg_select_timer &&
2276 !(--BOND_AD_INFO(bond).agg_select_timer)) {
be79bd04 2277 slave = bond_first_slave_rcu(bond);
3fdddd85 2278 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
fe9323da 2279
be79bd04 2280 /* select the active aggregator for the bond */
fe9323da 2281 if (port) {
1da177e4 2282 if (!port->slave) {
d4471f5e
VF
2283 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2284 bond->dev->name);
1da177e4
LT
2285 goto re_arm;
2286 }
2287
2288 aggregator = __get_first_agg(port);
ee637714 2289 ad_agg_selection_logic(aggregator, &update_slave_arr);
1da177e4 2290 }
f0c76d61 2291 bond_3ad_set_carrier(bond);
1da177e4
LT
2292 }
2293
be79bd04 2294 /* for each port run the state machines */
2295 bond_for_each_slave_rcu(bond, slave, iter) {
3fdddd85 2296 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4 2297 if (!port->slave) {
d4471f5e 2298 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
86a2b9cf 2299 bond->dev->name);
1da177e4
LT
2300 goto re_arm;
2301 }
2302
2303 ad_rx_machine(NULL, port);
2304 ad_periodic_machine(port);
ee637714
MB
2305 ad_port_selection_logic(port, &update_slave_arr);
2306 ad_mux_machine(port, &update_slave_arr);
1da177e4 2307 ad_tx_machine(port);
14c9551a 2308 ad_churn_machine(port);
1da177e4 2309
be79bd04 2310 /* turn off the BEGIN bit, since we already handled it */
7bfc4753 2311 if (port->sm_vars & AD_PORT_BEGIN)
1da177e4 2312 port->sm_vars &= ~AD_PORT_BEGIN;
1da177e4
LT
2313 }
2314
2315re_arm:
5e5b0665 2316 bond_for_each_slave_rcu(bond, slave, iter) {
2317 if (slave->should_notify) {
2318 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2319 break;
2320 }
2321 }
be79bd04 2322 rcu_read_unlock();
b7435628 2323 spin_unlock_bh(&bond->mode_lock);
5e5b0665 2324
ee637714
MB
2325 if (update_slave_arr)
2326 bond_slave_arr_work_rearm(bond, 0);
2327
5e5b0665 2328 if (should_notify_rtnl && rtnl_trylock()) {
b0929915 2329 bond_slave_state_notify(bond);
5e5b0665 2330 rtnl_unlock();
2331 }
be79bd04 2332 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
1da177e4
LT
2333}
2334
2335/**
2336 * bond_3ad_rx_indication - handle a received frame
2337 * @lacpdu: received lacpdu
2338 * @slave: slave struct to work on
2339 * @length: length of the data received
2340 *
2341 * It is assumed that frames that were sent on this NIC don't returned as new
2342 * received frames (loopback). Since only the payload is given to this
2343 * function, it check for loopback.
2344 */
3bf2d28a
VF
2345static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2346 u16 length)
1da177e4
LT
2347{
2348 struct port *port;
13a8e0c8 2349 int ret = RX_HANDLER_ANOTHER;
1da177e4
LT
2350
2351 if (length >= sizeof(struct lacpdu)) {
2352
3fdddd85 2353 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4
LT
2354
2355 if (!port->slave) {
d4471f5e
VF
2356 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2357 slave->dev->name, slave->bond->dev->name);
13a8e0c8 2358 return ret;
1da177e4
LT
2359 }
2360
2361 switch (lacpdu->subtype) {
2362 case AD_TYPE_LACPDU:
13a8e0c8 2363 ret = RX_HANDLER_CONSUMED;
2f637324
SA
2364 netdev_dbg(slave->bond->dev,
2365 "Received LACPDU on port %d slave %s\n",
2366 port->actor_port_number,
2367 slave->dev->name);
16d79d7d 2368 /* Protect against concurrent state machines */
e470259f 2369 spin_lock(&slave->bond->mode_lock);
1da177e4 2370 ad_rx_machine(lacpdu, port);
e470259f 2371 spin_unlock(&slave->bond->mode_lock);
1da177e4
LT
2372 break;
2373
2374 case AD_TYPE_MARKER:
13a8e0c8 2375 ret = RX_HANDLER_CONSUMED;
3bf2d28a
VF
2376 /* No need to convert fields to Little Endian since we
2377 * don't use the marker's fields.
2378 */
1da177e4 2379
1c3f0b8e 2380 switch (((struct bond_marker *)lacpdu)->tlv_type) {
1da177e4 2381 case AD_MARKER_INFORMATION_SUBTYPE:
d4471f5e
VF
2382 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2383 port->actor_port_number);
1c3f0b8e 2384 ad_marker_info_received((struct bond_marker *)lacpdu, port);
1da177e4
LT
2385 break;
2386
2387 case AD_MARKER_RESPONSE_SUBTYPE:
d4471f5e
VF
2388 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2389 port->actor_port_number);
1c3f0b8e 2390 ad_marker_response_received((struct bond_marker *)lacpdu, port);
1da177e4
LT
2391 break;
2392
2393 default:
d4471f5e
VF
2394 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2395 port->actor_port_number);
1da177e4
LT
2396 }
2397 }
2398 }
13a8e0c8 2399 return ret;
1da177e4
LT
2400}
2401
7bb11dc9
MB
2402/**
2403 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2404 * its current speed and duplex settings.
2405 *
2406 * @port: the port we'are looking at
2407 * @reset: Boolean to just reset the speed and the duplex part of the key
2408 *
2409 * The logic to change the oper / admin keys is:
2410 * (a) A full duplex port can participate in LACP with partner.
2411 * (b) When the speed is changed, LACP need to be reinitiated.
2412 */
2413static void ad_update_actor_keys(struct port *port, bool reset)
2414{
2415 u8 duplex = 0;
2416 u16 ospeed = 0, speed = 0;
2417 u16 old_oper_key = port->actor_oper_port_key;
2418
2419 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2420 if (!reset) {
2421 speed = __get_link_speed(port);
2422 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2423 duplex = __get_duplex(port);
2424 port->actor_admin_port_key |= (speed << 1) | duplex;
2425 }
2426 port->actor_oper_port_key = port->actor_admin_port_key;
2427
2428 if (old_oper_key != port->actor_oper_port_key) {
2429 /* Only 'duplex' port participates in LACP */
2430 if (duplex)
2431 port->sm_vars |= AD_PORT_LACP_ENABLED;
2432 else
2433 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2434
2435 if (!reset) {
2436 if (!speed) {
2437 netdev_err(port->slave->dev,
2438 "speed changed to 0 for port %s",
2439 port->slave->dev->name);
2440 } else if (duplex && ospeed != speed) {
2441 /* Speed change restarts LACP state-machine */
2442 port->sm_vars |= AD_PORT_BEGIN;
2443 }
2444 }
2445 }
2446}
2447
1da177e4 2448/**
52bc6716
MB
2449 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2450 * change indication
1da177e4 2451 *
1da177e4
LT
2452 * @slave: slave struct to work on
2453 *
2454 * Handle reselection of aggregator (if needed) for this port.
2455 */
52bc6716 2456void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
1da177e4
LT
2457{
2458 struct port *port;
2459
3fdddd85 2460 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4 2461
bca44a73 2462 /* if slave is null, the whole port is not initialized */
1da177e4 2463 if (!port->slave) {
52bc6716
MB
2464 netdev_warn(slave->bond->dev,
2465 "speed/duplex changed for uninitialized port %s\n",
d4471f5e 2466 slave->dev->name);
1da177e4
LT
2467 return;
2468 }
2469
e470259f 2470 spin_lock_bh(&slave->bond->mode_lock);
7bb11dc9 2471 ad_update_actor_keys(port, false);
52bc6716 2472 netdev_dbg(slave->bond->dev, "Port %d slave %s changed speed/duplex\n",
2f637324 2473 port->actor_port_number, slave->dev->name);
e470259f 2474 spin_unlock_bh(&slave->bond->mode_lock);
1da177e4
LT
2475}
2476
2477/**
2478 * bond_3ad_handle_link_change - handle a slave's link status change indication
2479 * @slave: slave struct to work on
2480 * @status: whether the link is now up or down
2481 *
2482 * Handle reselection of aggregator (if needed) for this port.
2483 */
2484void bond_3ad_handle_link_change(struct slave *slave, char link)
2485{
0622cab0 2486 struct aggregator *agg;
1da177e4 2487 struct port *port;
0622cab0 2488 bool dummy;
1da177e4 2489
3fdddd85 2490 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4 2491
108db736 2492 /* if slave is null, the whole port is not initialized */
1da177e4 2493 if (!port->slave) {
d4471f5e
VF
2494 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2495 slave->dev->name);
1da177e4
LT
2496 return;
2497 }
2498
e470259f 2499 spin_lock_bh(&slave->bond->mode_lock);
108db736 2500 /* on link down we are zeroing duplex and speed since
2501 * some of the adaptors(ce1000.lan) report full duplex/speed
2502 * instead of N/A(duplex) / 0(speed).
2503 *
2504 * on link up we are forcing recheck on the duplex and speed since
2505 * some of he adaptors(ce1000.lan) report.
2506 */
1da177e4 2507 if (link == BOND_LINK_UP) {
f48127b6 2508 port->is_enabled = true;
7bb11dc9 2509 ad_update_actor_keys(port, false);
1da177e4
LT
2510 } else {
2511 /* link has failed */
f48127b6 2512 port->is_enabled = false;
7bb11dc9 2513 ad_update_actor_keys(port, true);
1da177e4 2514 }
0622cab0
JV
2515 agg = __get_first_agg(port);
2516 ad_agg_selection_logic(agg, &dummy);
2517
d4471f5e
VF
2518 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2519 port->actor_port_number,
2520 link == BOND_LINK_UP ? "UP" : "DOWN");
108db736 2521
e470259f 2522 spin_unlock_bh(&slave->bond->mode_lock);
ee637714
MB
2523
2524 /* RTNL is held and mode_lock is released so it's safe
2525 * to update slave_array here.
2526 */
2527 bond_update_slave_arr(slave->bond, NULL);
1da177e4
LT
2528}
2529
3bf2d28a
VF
2530/**
2531 * bond_3ad_set_carrier - set link state for bonding master
2532 * @bond - bonding structure
2533 *
2534 * if we have an active aggregator, we're up, if not, we're down.
2535 * Presumes that we cannot have an active aggregator if there are
2536 * no slaves with link up.
ff59c456 2537 *
031ae4de
JV
2538 * This behavior complies with IEEE 802.3 section 43.3.9.
2539 *
ff59c456
JV
2540 * Called by bond_set_carrier(). Return zero if carrier state does not
2541 * change, nonzero if it does.
2542 */
2543int bond_3ad_set_carrier(struct bonding *bond)
2544{
655f8919 2545 struct aggregator *active;
dec1e90e 2546 struct slave *first_slave;
c1bc9644 2547 int ret = 1;
655f8919 2548
be79bd04 2549 rcu_read_lock();
2550 first_slave = bond_first_slave_rcu(bond);
c1bc9644
VF
2551 if (!first_slave) {
2552 ret = 0;
2553 goto out;
2554 }
3fdddd85 2555 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
655f8919 2556 if (active) {
2557 /* are enough slaves available to consider link up? */
0622cab0 2558 if (__agg_active_ports(active) < bond->params.min_links) {
655f8919 2559 if (netif_carrier_ok(bond->dev)) {
2560 netif_carrier_off(bond->dev);
c1bc9644 2561 goto out;
655f8919 2562 }
2563 } else if (!netif_carrier_ok(bond->dev)) {
ff59c456 2564 netif_carrier_on(bond->dev);
c1bc9644 2565 goto out;
ff59c456 2566 }
c1bc9644 2567 } else if (netif_carrier_ok(bond->dev)) {
ff59c456 2568 netif_carrier_off(bond->dev);
ff59c456 2569 }
c1bc9644
VF
2570out:
2571 rcu_read_unlock();
2572 return ret;
ff59c456
JV
2573}
2574
1da177e4 2575/**
318debd8 2576 * __bond_3ad_get_active_agg_info - get information of the active aggregator
1da177e4
LT
2577 * @bond: bonding struct to work on
2578 * @ad_info: ad_info struct to fill with the bond's info
2579 *
2580 * Returns: 0 on success
2581 * < 0 on error
2582 */
318debd8 2583int __bond_3ad_get_active_agg_info(struct bonding *bond,
2584 struct ad_info *ad_info)
1da177e4
LT
2585{
2586 struct aggregator *aggregator = NULL;
3c4c88a1
VF
2587 struct list_head *iter;
2588 struct slave *slave;
1da177e4
LT
2589 struct port *port;
2590
47e91f56 2591 bond_for_each_slave_rcu(bond, slave, iter) {
3fdddd85 2592 port = &(SLAVE_AD_INFO(slave)->port);
1da177e4
LT
2593 if (port->aggregator && port->aggregator->is_active) {
2594 aggregator = port->aggregator;
2595 break;
2596 }
2597 }
2598
21f374c6
JP
2599 if (!aggregator)
2600 return -1;
2601
2602 ad_info->aggregator_id = aggregator->aggregator_identifier;
2603 ad_info->ports = aggregator->num_of_ports;
2604 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2605 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2606 ether_addr_copy(ad_info->partner_system,
2607 aggregator->partner_system.mac_addr_value);
2608 return 0;
1da177e4
LT
2609}
2610
318debd8 2611int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2612{
2613 int ret;
2614
47e91f56 2615 rcu_read_lock();
318debd8 2616 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
47e91f56 2617 rcu_read_unlock();
318debd8 2618
2619 return ret;
2620}
2621
de063b70
ED
2622int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2623 struct slave *slave)
1da177e4 2624{
de063b70
ED
2625 struct lacpdu *lacpdu, _lacpdu;
2626
3aba891d 2627 if (skb->protocol != PKT_TYPE_LACPDU)
86e74986 2628 return RX_HANDLER_ANOTHER;
b3053251 2629
bb54e589
MB
2630 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2631 return RX_HANDLER_ANOTHER;
2632
de063b70
ED
2633 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2634 if (!lacpdu)
86e74986 2635 return RX_HANDLER_ANOTHER;
ab12811c 2636
86e74986 2637 return bond_3ad_rx_indication(lacpdu, slave, skb->len);
1da177e4 2638}
ba824a8b 2639
3bf2d28a
VF
2640/**
2641 * bond_3ad_update_lacp_rate - change the lacp rate
2642 * @bond - bonding struct
2643 *
ba824a8b
PP
2644 * When modify lacp_rate parameter via sysfs,
2645 * update actor_oper_port_state of each port.
2646 *
e470259f 2647 * Hold bond->mode_lock,
ba824a8b
PP
2648 * so we can modify port->actor_oper_port_state,
2649 * no matter bond is up or down.
2650 */
2651void bond_3ad_update_lacp_rate(struct bonding *bond)
2652{
ba824a8b 2653 struct port *port = NULL;
9caff1e7 2654 struct list_head *iter;
c509316b 2655 struct slave *slave;
ba824a8b
PP
2656 int lacp_fast;
2657
ba824a8b 2658 lacp_fast = bond->params.lacp_fast;
e470259f 2659 spin_lock_bh(&bond->mode_lock);
9caff1e7 2660 bond_for_each_slave(bond, slave, iter) {
3fdddd85 2661 port = &(SLAVE_AD_INFO(slave)->port);
ba824a8b
PP
2662 if (lacp_fast)
2663 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2664 else
2665 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
ba824a8b 2666 }
e470259f 2667 spin_unlock_bh(&bond->mode_lock);
ba824a8b 2668}