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