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