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