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