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