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