blob: be799d2a8a8da511f9fe0210ec9d7351445c1820 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Jay Vosburghfd989c82008-11-04 17:51:16 -080028#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/if_bonding.h>
30#include <linux/pkt_sched.h>
Eric W. Biedermane730c152007-09-17 11:53:39 -070031#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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
Jay Vosburgh94dbffd2006-09-22 21:52:15 -070088#define AD_LINK_SPEED_BITMASK_10000MBPS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -070089//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
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -080098static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100// ================= main 802.3ad protocol functions ==================
101static int ad_lacpdu_send(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700102static int ad_marker_send(struct port *port, struct bond_marker *marker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void ad_enable_collecting_distributing(struct port *port);
113static void ad_disable_collecting_distributing(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700114static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
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
Jay Vosburghfd989c82008-11-04 17:51:16 -0800211/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/**
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 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800260 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
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) {
Jay Vosburghfd989c82008-11-04 17:51:16 -0800267 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
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{
Jay Vosburgh2bf86b72008-03-21 22:29:33 -0700296 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
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{
Jay Vosburgh2bf86b72008-03-21 22:29:33 -0700306 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
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,
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700317 * %AD_LINK_SPEED_BITMASK_1000MBPS,
318 * %AD_LINK_SPEED_BITMASK_10000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
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
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700345 case SPEED_10000:
346 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
347 break;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 default:
350 speed = 0; // unknown speed value from ethtool. shouldn't happen
351 break;
352 }
353 }
354
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800355 pr_debug("Port %d Received link speed %d update from adapter\n", port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 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;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800381 pr_debug("Port %d Received status full duplex update from adapter\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383 case DUPLEX_HALF:
384 default:
385 retval=0x0;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800386 pr_debug("Port %d Received status NOT full duplex update from adapter\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800460 struct port_params *partner = &port->partner_oper;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 // record the new parameter values for the partner operational
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800463 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
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)) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800475 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800477 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (port) {
493 // record the partner admin parameters
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800494 memcpy(&port->partner_oper, &port->partner_admin,
495 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800518 const struct port_params *partner = &port->partner_oper;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 // check if any parameter is different
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800521 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 // 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800548 const struct port_params *admin = &port->partner_admin;
549 const struct port_params *oper = &port->partner_oper;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 // check if any parameter is different
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800552 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 // 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
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700589 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
590 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700592 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
593 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 ((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
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700623 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
624 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700626 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
627 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ((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 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800633
634 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
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;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700736 case AD_LINK_SPEED_BITMASK_10000MBPS:
737 bandwidth = aggregator->num_of_ports * 10000;
738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 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;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800773 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
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
Al Virod3bb52b2007-08-22 20:06:58 -0400782 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400784 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 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
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800794 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 skb = dev_alloc_skb(length);
831 if (!skb) {
832 return -ENOMEM;
833 }
834
835 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700836 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700837 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 skb->protocol = PKT_TYPE_LACPDU;
839 skb->priority = TC_PRIO_CONTROL;
840
841 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
842
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800843 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800844 /* Note: source addres is set to be the member's PERMANENT address,
845 because we use it to identify loopback lacpdus in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800846 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
847 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 lacpdu_header->lacpdu = port->lacpdu; // struct copy
850
851 dev_queue_xmit(skb);
852
853 return 0;
854}
855
856/**
857 * ad_marker_send - send marker information/response on a given port
858 * @port: the port we're looking at
859 * @marker: marker data to send
860 *
861 * Returns: 0 on success
862 * < 0 on error
863 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700864static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct slave *slave = port->slave;
867 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700868 struct bond_marker_header *marker_header;
869 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
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;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700879 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700880 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 skb->protocol = PKT_TYPE_LACPDU;
882
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700883 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800885 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800886 /* Note: source addres is set to be the member's PERMANENT address,
887 because we use it to identify loopback MARKERs in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800888 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
889 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
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)
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -0800946 if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 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) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -0800958 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 ) {
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) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800981 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 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;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800989 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 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);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001000 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 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);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001006 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 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)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001080 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 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);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001102 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 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.
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001110 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 port->sm_vars &= ~AD_PORT_MATCHED;
Julia Lawall8e321c42009-07-11 10:03:55 +00001112 port->partner_oper.port_state |=
1113 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1115 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1116 break;
1117 case AD_RX_DEFAULTED:
1118 __update_default_selected(port);
1119 __record_default(port);
1120 port->sm_vars |= AD_PORT_MATCHED;
1121 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1122 break;
1123 case AD_RX_CURRENT:
1124 // detect loopback situation
1125 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1126 // INFO_RECEIVED_LOOPBACK_FRAMES
Mitch Williams4e0952c2005-11-09 10:34:57 -08001127 printk(KERN_ERR DRV_NAME ": %s: An illegal loopback occurred on "
1128 "adapter (%s). Check the configuration to verify that all "
1129 "Adapters are connected to 802.3ad compliant switch ports\n",
1130 port->slave->dev->master->name, port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 __release_rx_machine_lock(port);
1132 return;
1133 }
1134 __update_selected(lacpdu, port);
1135 __update_ntt(lacpdu, port);
1136 __record_pdu(lacpdu, port);
1137 __choose_matched(lacpdu, port);
1138 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1139 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1140 // verify that if the aggregator is enabled, the port is enabled too.
1141 //(because if the link goes down for a short time, the 802.3ad will not
1142 // catch it, and the port will continue to be disabled)
1143 if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) {
1144 __enable_port(port);
1145 }
1146 break;
1147 default: //to silence the compiler
1148 break;
1149 }
1150 }
1151 __release_rx_machine_lock(port);
1152}
1153
1154/**
1155 * ad_tx_machine - handle a port's tx state machine
1156 * @port: the port we're looking at
1157 *
1158 */
1159static void ad_tx_machine(struct port *port)
1160{
1161 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1162 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1163 // check if there is something to send
1164 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1165 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (ad_lacpdu_send(port) >= 0) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001168 pr_debug("Sent LACPDU on port %d\n", port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001169
1170 /* mark ntt as false, so it will not be sent again until
1171 demanded */
1172 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 }
1175 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
1176 port->sm_tx_timer_counter=ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1177 }
1178}
1179
1180/**
1181 * ad_periodic_machine - handle a port's periodic state machine
1182 * @port: the port we're looking at
1183 *
1184 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1185 */
1186static void ad_periodic_machine(struct port *port)
1187{
1188 periodic_states_t last_state;
1189
1190 // keep current state machine state to compare later if it was changed
1191 last_state = port->sm_periodic_state;
1192
1193 // check if port was reinitialized
1194 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001195 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ) {
1197 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1198 }
1199 // check if state machine should change state
1200 else if (port->sm_periodic_timer_counter) {
1201 // check if periodic state machine expired
1202 if (!(--port->sm_periodic_timer_counter)) {
1203 // if expired then do tx
1204 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1205 } else {
1206 // If not expired, check if there is some new timeout parameter from the partner state
1207 switch (port->sm_periodic_state) {
1208 case AD_FAST_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001209 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1211 }
1212 break;
1213 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001214 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 // stop current timer
1216 port->sm_periodic_timer_counter = 0;
1217 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1218 }
1219 break;
1220 default: //to silence the compiler
1221 break;
1222 }
1223 }
1224 } else {
1225 switch (port->sm_periodic_state) {
1226 case AD_NO_PERIODIC:
1227 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1228 break;
1229 case AD_PERIODIC_TX:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001230 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1232 } else {
1233 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1234 }
1235 break;
1236 default: //to silence the compiler
1237 break;
1238 }
1239 }
1240
1241 // check if the state machine was changed
1242 if (port->sm_periodic_state != last_state) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001243 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 switch (port->sm_periodic_state) {
1245 case AD_NO_PERIODIC:
1246 port->sm_periodic_timer_counter = 0; // zero timer
1247 break;
1248 case AD_FAST_PERIODIC:
1249 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
1250 break;
1251 case AD_SLOW_PERIODIC:
1252 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
1253 break;
1254 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001255 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257 default: //to silence the compiler
1258 break;
1259 }
1260 }
1261}
1262
1263/**
1264 * ad_port_selection_logic - select aggregation groups
1265 * @port: the port we're looking at
1266 *
1267 * Select aggregation groups, and assign each port for it's aggregetor. The
1268 * selection logic is called in the inititalization (after all the handshkes),
1269 * and after every lacpdu receive (if selected is off).
1270 */
1271static void ad_port_selection_logic(struct port *port)
1272{
1273 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1274 struct port *last_port = NULL, *curr_port;
1275 int found = 0;
1276
1277 // if the port is already Selected, do nothing
1278 if (port->sm_vars & AD_PORT_SELECTED) {
1279 return;
1280 }
1281
1282 // if the port is connected to other aggregator, detach it
1283 if (port->aggregator) {
1284 // detach the port from its former aggregator
1285 temp_aggregator=port->aggregator;
1286 for (curr_port=temp_aggregator->lag_ports; curr_port; last_port=curr_port, curr_port=curr_port->next_port_in_aggregator) {
1287 if (curr_port == port) {
1288 temp_aggregator->num_of_ports--;
1289 if (!last_port) {// if it is the first port attached to the aggregator
1290 temp_aggregator->lag_ports=port->next_port_in_aggregator;
1291 } else {// not the first port attached to the aggregator
1292 last_port->next_port_in_aggregator=port->next_port_in_aggregator;
1293 }
1294
1295 // clear the port's relations to this aggregator
1296 port->aggregator = NULL;
1297 port->next_port_in_aggregator=NULL;
1298 port->actor_port_aggregator_identifier=0;
1299
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001300 pr_debug("Port %d left LAG %d\n", port->actor_port_number, temp_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1302 if (!temp_aggregator->lag_ports) {
1303 ad_clear_agg(temp_aggregator);
1304 }
1305 break;
1306 }
1307 }
1308 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
Mitch Williams4e0952c2005-11-09 10:34:57 -08001309 printk(KERN_WARNING DRV_NAME ": %s: Warning: Port %d (on %s) was "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 "related to aggregator %d but was not on its port list\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001311 port->slave->dev->master->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 port->actor_port_number, port->slave->dev->name,
1313 port->aggregator->aggregator_identifier);
1314 }
1315 }
1316 // search on all aggregators for a suitable aggregator for this port
1317 for (aggregator = __get_first_agg(port); aggregator;
1318 aggregator = __get_next_agg(aggregator)) {
1319
1320 // keep a free aggregator for later use(if needed)
1321 if (!aggregator->lag_ports) {
1322 if (!free_aggregator) {
1323 free_aggregator=aggregator;
1324 }
1325 continue;
1326 }
1327 // check if current aggregator suits us
1328 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001329 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1330 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1331 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 ) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001333 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 !aggregator->is_individual) // but is not individual OR
1335 )
1336 ) {
1337 // attach to the founded aggregator
1338 port->aggregator = aggregator;
1339 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1340 port->next_port_in_aggregator=aggregator->lag_ports;
1341 port->aggregator->num_of_ports++;
1342 aggregator->lag_ports=port;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001343 pr_debug("Port %d joined LAG %d(existing LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 // mark this port as selected
1346 port->sm_vars |= AD_PORT_SELECTED;
1347 found = 1;
1348 break;
1349 }
1350 }
1351
1352 // the port couldn't find an aggregator - attach it to a new aggregator
1353 if (!found) {
1354 if (free_aggregator) {
1355 // assign port a new aggregator
1356 port->aggregator = free_aggregator;
1357 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
1358
1359 // update the new aggregator's parameters
1360 // if port was responsed from the end-user
1361 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001362 port->aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 } else {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001364 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
1367 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1368 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001369 port->aggregator->partner_system=port->partner_oper.system;
1370 port->aggregator->partner_system_priority = port->partner_oper.system_priority;
1371 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 port->aggregator->receive_state = 1;
1373 port->aggregator->transmit_state = 1;
1374 port->aggregator->lag_ports = port;
1375 port->aggregator->num_of_ports++;
1376
1377 // mark this port as selected
1378 port->sm_vars |= AD_PORT_SELECTED;
1379
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001380 pr_debug("Port %d joined LAG %d(new LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } else {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001382 printk(KERN_ERR DRV_NAME ": %s: Port %d (on %s) did not find a suitable aggregator\n",
1383 port->slave->dev->master->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 port->actor_port_number, port->slave->dev->name);
1385 }
1386 }
1387 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1388 // else set ready=FALSE in all aggregator's ports
1389 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1390
Jay Vosburghfd989c82008-11-04 17:51:16 -08001391 aggregator = __get_first_agg(port);
1392 ad_agg_selection_logic(aggregator);
1393}
1394
1395/*
1396 * Decide if "agg" is a better choice for the new active aggregator that
1397 * the current best, according to the ad_select policy.
1398 */
1399static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1400 struct aggregator *curr)
1401{
1402 /*
1403 * 0. If no best, select current.
1404 *
1405 * 1. If the current agg is not individual, and the best is
1406 * individual, select current.
1407 *
1408 * 2. If current agg is individual and the best is not, keep best.
1409 *
1410 * 3. Therefore, current and best are both individual or both not
1411 * individual, so:
1412 *
1413 * 3a. If current agg partner replied, and best agg partner did not,
1414 * select current.
1415 *
1416 * 3b. If current agg partner did not reply and best agg partner
1417 * did reply, keep best.
1418 *
1419 * 4. Therefore, current and best both have partner replies or
1420 * both do not, so perform selection policy:
1421 *
1422 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1423 * select by bandwidth.
1424 *
1425 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1426 */
1427 if (!best)
1428 return curr;
1429
1430 if (!curr->is_individual && best->is_individual)
1431 return curr;
1432
1433 if (curr->is_individual && !best->is_individual)
1434 return best;
1435
1436 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1437 return curr;
1438
1439 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1440 return best;
1441
1442 switch (__get_agg_selection_mode(curr->lag_ports)) {
1443 case BOND_AD_COUNT:
1444 if (curr->num_of_ports > best->num_of_ports)
1445 return curr;
1446
1447 if (curr->num_of_ports < best->num_of_ports)
1448 return best;
1449
1450 /*FALLTHROUGH*/
1451 case BOND_AD_STABLE:
1452 case BOND_AD_BANDWIDTH:
1453 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1454 return curr;
1455
1456 break;
1457
1458 default:
1459 printk(KERN_WARNING DRV_NAME
1460 ": %s: Impossible agg select mode %d\n",
1461 curr->slave->dev->master->name,
1462 __get_agg_selection_mode(curr->lag_ports));
1463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001465
1466 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001469static int agg_device_up(const struct aggregator *agg)
1470{
1471 return (netif_running(agg->slave->dev) &&
1472 netif_carrier_ok(agg->slave->dev));
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475/**
1476 * ad_agg_selection_logic - select an aggregation group for a team
1477 * @aggregator: the aggregator we're looking at
1478 *
1479 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001480 *
1481 * The logic of this function is to select the aggregator according to
1482 * the ad_select policy:
1483 *
1484 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1485 * it, and to reselect the active aggregator only if the previous
1486 * aggregator has no more ports related to it.
1487 *
1488 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1489 * bandwidth, and reselect whenever a link state change takes place or the
1490 * set of slaves in the bond changes.
1491 *
1492 * BOND_AD_COUNT: select the aggregator with largest number of ports
1493 * (slaves), and reselect whenever a link state change takes place or the
1494 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
1496 * FIXME: this function MUST be called with the first agg in the bond, or
1497 * __get_active_agg() won't work correctly. This function should be better
1498 * called with the bond itself, and retrieve the first agg from it.
1499 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001500static void ad_agg_selection_logic(struct aggregator *agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001502 struct aggregator *best, *active, *origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Jay Vosburghfd989c82008-11-04 17:51:16 -08001505 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001506 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001507 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 do {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001510 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001512 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001513 best = ad_agg_selection_test(best, agg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Jay Vosburghfd989c82008-11-04 17:51:16 -08001515 } while ((agg = __get_next_agg(agg)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Jay Vosburghfd989c82008-11-04 17:51:16 -08001517 if (best &&
1518 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1519 /*
1520 * For the STABLE policy, don't replace the old active
1521 * aggregator if it's still active (it has an answering
1522 * partner) or if both the best and active don't have an
1523 * answering partner.
1524 */
1525 if (active && active->lag_ports &&
1526 active->lag_ports->is_enabled &&
1527 (__agg_has_partner(active) ||
1528 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1529 if (!(!active->actor_oper_aggregator_key &&
1530 best->actor_oper_aggregator_key)) {
1531 best = NULL;
1532 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 }
1535 }
1536
Jay Vosburghfd989c82008-11-04 17:51:16 -08001537 if (best && (best == active)) {
1538 best = NULL;
1539 active->is_active = 1;
1540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jay Vosburghfd989c82008-11-04 17:51:16 -08001542 // if there is new best aggregator, activate it
1543 if (best) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001544 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08001545 best->aggregator_identifier, best->num_of_ports,
1546 best->actor_oper_aggregator_key,
1547 best->partner_oper_aggregator_key,
1548 best->is_individual, best->is_active);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001549 pr_debug("best ports %p slave %p %s\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08001550 best->lag_ports, best->slave,
1551 best->slave ? best->slave->dev->name : "NULL");
1552
1553 for (agg = __get_first_agg(best->lag_ports); agg;
1554 agg = __get_next_agg(agg)) {
1555
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001556 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08001557 agg->aggregator_identifier, agg->num_of_ports,
1558 agg->actor_oper_aggregator_key,
1559 agg->partner_oper_aggregator_key,
1560 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562
1563 // check if any partner replys
Jay Vosburghfd989c82008-11-04 17:51:16 -08001564 if (best->is_individual) {
1565 printk(KERN_WARNING DRV_NAME ": %s: Warning: No 802.3ad"
1566 " response from the link partner for any"
1567 " adapters in the bond\n",
1568 best->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
Jay Vosburghfd989c82008-11-04 17:51:16 -08001571 best->is_active = 1;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001572 pr_debug("LAG %d chosen as the active LAG\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08001573 best->aggregator_identifier);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001574 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08001575 best->aggregator_identifier, best->num_of_ports,
1576 best->actor_oper_aggregator_key,
1577 best->partner_oper_aggregator_key,
1578 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 // disable the ports that were related to the former active_aggregator
Jay Vosburghfd989c82008-11-04 17:51:16 -08001581 if (active) {
1582 for (port = active->lag_ports; port;
1583 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 __disable_port(port);
1585 }
1586 }
1587 }
1588
Jay Vosburghfd989c82008-11-04 17:51:16 -08001589 /*
1590 * if the selected aggregator is of join individuals
1591 * (partner_system is NULL), enable their ports
1592 */
1593 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Jay Vosburghfd989c82008-11-04 17:51:16 -08001595 if (active) {
1596 if (!__agg_has_partner(active)) {
1597 for (port = active->lag_ports; port;
1598 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 __enable_port(port);
1600 }
1601 }
1602 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001603
1604 if (origin->slave) {
1605 struct bonding *bond;
1606
1607 bond = bond_get_bond_by_slave(origin->slave);
1608 if (bond)
1609 bond_3ad_set_carrier(bond);
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/**
1614 * ad_clear_agg - clear a given aggregator's parameters
1615 * @aggregator: the aggregator we're looking at
1616 *
1617 */
1618static void ad_clear_agg(struct aggregator *aggregator)
1619{
1620 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001621 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 aggregator->actor_admin_aggregator_key = 0;
1623 aggregator->actor_oper_aggregator_key = 0;
1624 aggregator->partner_system = null_mac_addr;
1625 aggregator->partner_system_priority = 0;
1626 aggregator->partner_oper_aggregator_key = 0;
1627 aggregator->receive_state = 0;
1628 aggregator->transmit_state = 0;
1629 aggregator->lag_ports = NULL;
1630 aggregator->is_active = 0;
1631 aggregator->num_of_ports = 0;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001632 pr_debug("LAG %d was cleared\n", aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634}
1635
1636/**
1637 * ad_initialize_agg - initialize a given aggregator's parameters
1638 * @aggregator: the aggregator we're looking at
1639 *
1640 */
1641static void ad_initialize_agg(struct aggregator *aggregator)
1642{
1643 if (aggregator) {
1644 ad_clear_agg(aggregator);
1645
1646 aggregator->aggregator_mac_address = null_mac_addr;
1647 aggregator->aggregator_identifier = 0;
1648 aggregator->slave = NULL;
1649 }
1650}
1651
1652/**
1653 * ad_initialize_port - initialize a given port's parameters
1654 * @aggregator: the aggregator we're looking at
1655 * @lacp_fast: boolean. whether fast periodic should be used
1656 *
1657 */
1658static void ad_initialize_port(struct port *port, int lacp_fast)
1659{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001660 static const struct port_params tmpl = {
1661 .system_priority = 0xffff,
1662 .key = 1,
1663 .port_number = 1,
1664 .port_priority = 0xff,
1665 .port_state = 1,
1666 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001667 static const struct lacpdu lacpdu = {
1668 .subtype = 0x01,
1669 .version_number = 0x01,
1670 .tlv_type_actor_info = 0x01,
1671 .actor_information_length = 0x14,
1672 .tlv_type_partner_info = 0x02,
1673 .partner_information_length = 0x14,
1674 .tlv_type_collector_info = 0x03,
1675 .collector_information_length = 0x10,
1676 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1677 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (port) {
1680 port->actor_port_number = 1;
1681 port->actor_port_priority = 0xff;
1682 port->actor_system = null_mac_addr;
1683 port->actor_system_priority = 0xffff;
1684 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001685 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 port->actor_admin_port_key = 1;
1687 port->actor_oper_port_key = 1;
1688 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1689 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1690
1691 if (lacp_fast) {
1692 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1693 }
1694
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001695 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1696 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1697
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001698 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 // ****** private parameters ******
1700 port->sm_vars = 0x3;
1701 port->sm_rx_state = 0;
1702 port->sm_rx_timer_counter = 0;
1703 port->sm_periodic_state = 0;
1704 port->sm_periodic_timer_counter = 0;
1705 port->sm_mux_state = 0;
1706 port->sm_mux_timer_counter = 0;
1707 port->sm_tx_state = 0;
1708 port->sm_tx_timer_counter = 0;
1709 port->slave = NULL;
1710 port->aggregator = NULL;
1711 port->next_port_in_aggregator = NULL;
1712 port->transaction_id = 0;
1713
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001714 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716}
1717
1718/**
1719 * ad_enable_collecting_distributing - enable a port's transmit/receive
1720 * @port: the port we're looking at
1721 *
1722 * Enable @port if it's in an active aggregator
1723 */
1724static void ad_enable_collecting_distributing(struct port *port)
1725{
1726 if (port->aggregator->is_active) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001727 pr_debug("Enabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 __enable_port(port);
1729 }
1730}
1731
1732/**
1733 * ad_disable_collecting_distributing - disable a port's transmit/receive
1734 * @port: the port we're looking at
1735 *
1736 */
1737static void ad_disable_collecting_distributing(struct port *port)
1738{
1739 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001740 pr_debug("Disabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 __disable_port(port);
1742 }
1743}
1744
1745#if 0
1746/**
1747 * ad_marker_info_send - send a marker information frame
1748 * @port: the port we're looking at
1749 *
1750 * This function does nothing since we decided not to implement send and handle
1751 * response for marker PDU's, in this stage, but only to respond to marker
1752 * information.
1753 */
1754static void ad_marker_info_send(struct port *port)
1755{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001756 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 u16 index;
1758
1759 // fill the marker PDU with the appropriate values
1760 marker.subtype = 0x02;
1761 marker.version_number = 0x01;
1762 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1763 marker.marker_length = 0x16;
1764 // convert requester_port to Big Endian
1765 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1766 marker.requester_system = port->actor_system;
1767 // convert requester_port(u32) to Big Endian
1768 marker.requester_transaction_id = (((++port->transaction_id & 0xFF) << 24) |((port->transaction_id & 0xFF00) << 8) |((port->transaction_id & 0xFF0000) >> 8) |((port->transaction_id & 0xFF000000) >> 24));
1769 marker.pad = 0;
1770 marker.tlv_type_terminator = 0x00;
1771 marker.terminator_length = 0x00;
1772 for (index=0; index<90; index++) {
1773 marker.reserved_90[index]=0;
1774 }
1775
1776 // send the marker information
1777 if (ad_marker_send(port, &marker) >= 0) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001778 pr_debug("Sent Marker Information on port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780}
1781#endif
1782
1783/**
1784 * ad_marker_info_received - handle receive of a Marker information frame
1785 * @marker_info: Marker info received
1786 * @port: the port we're looking at
1787 *
1788 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001789static void ad_marker_info_received(struct bond_marker *marker_info,
1790 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001792 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 // copy the received marker data to the response marker
1795 //marker = *marker_info;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001796 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 // change the marker subtype to marker response
1798 marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE;
1799 // send the marker response
1800
1801 if (ad_marker_send(port, &marker) >= 0) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001802 pr_debug("Sent Marker Response on port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804}
1805
1806/**
1807 * ad_marker_response_received - handle receive of a marker response frame
1808 * @marker: marker PDU received
1809 * @port: the port we're looking at
1810 *
1811 * This function does nothing since we decided not to implement send and handle
1812 * response for marker PDU's, in this stage, but only to respond to marker
1813 * information.
1814 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001815static void ad_marker_response_received(struct bond_marker *marker,
1816 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 marker=NULL; // just to satisfy the compiler
1819 port=NULL; // just to satisfy the compiler
1820 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823//////////////////////////////////////////////////////////////////////////////////////
1824// ================= AD exported functions to the main bonding code ==================
1825//////////////////////////////////////////////////////////////////////////////////////
1826
1827// Check aggregators status in team every T seconds
1828#define AD_AGGREGATOR_SELECTION_TIMER 8
1829
Jay Vosburghfd989c82008-11-04 17:51:16 -08001830/*
1831 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1832 *
1833 * Set the aggregation selection timer, to initiate an agg selection in
1834 * the very near future. Called during first initialization, and during
1835 * any down to up transitions of the bond.
1836 */
1837void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1838{
1839 BOND_AD_INFO(bond).agg_select_timer = timeout;
1840 BOND_AD_INFO(bond).agg_select_mode = bond->params.ad_select;
1841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843static u16 aggregator_identifier;
1844
1845/**
1846 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1847 * @bond: bonding struct to work on
1848 * @tick_resolution: tick duration (millisecond resolution)
1849 * @lacp_fast: boolean. whether fast periodic should be used
1850 *
1851 * Can be called only after the mac address of the bond is set.
1852 */
1853void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 // check that the bond is not initialized yet
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001856 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1857 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 aggregator_identifier = 0;
1860
1861 BOND_AD_INFO(bond).lacp_fast = lacp_fast;
1862 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1863 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1864
1865 // initialize how many times this module is called in one second(should be about every 100ms)
1866 ad_ticks_per_sec = tick_resolution;
1867
Jay Vosburghfd989c82008-11-04 17:51:16 -08001868 bond_3ad_initiate_agg_selection(bond,
1869 AD_AGGREGATOR_SELECTION_TIMER *
1870 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872}
1873
1874/**
1875 * bond_3ad_bind_slave - initialize a slave's port
1876 * @slave: slave struct to work on
1877 *
1878 * Returns: 0 on success
1879 * < 0 on error
1880 */
1881int bond_3ad_bind_slave(struct slave *slave)
1882{
1883 struct bonding *bond = bond_get_bond_by_slave(slave);
1884 struct port *port;
1885 struct aggregator *aggregator;
1886
1887 if (bond == NULL) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001888 printk(KERN_ERR DRV_NAME ": %s: The slave %s is not attached to its bond\n",
1889 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return -1;
1891 }
1892
1893 //check that the slave has not been intialized yet.
1894 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1895
1896 // port initialization
1897 port = &(SLAVE_AD_INFO(slave).port);
1898
1899 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
1900
1901 port->slave = slave;
1902 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1903 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1904 // ------------------------------------------------------------
1905 // Port key : | User key | Speed |Duplex|
1906 // ------------------------------------------------------------
1907 // 16 6 1 0
1908 port->actor_admin_port_key = 0; // initialize this parameter
1909 port->actor_admin_port_key |= __get_duplex(port);
1910 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1911 port->actor_oper_port_key = port->actor_admin_port_key;
1912 // if the port is not full duplex, then the port should be not lacp Enabled
1913 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1914 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1915 }
1916 // actor system is the bond's system
1917 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1918 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1919 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1920 port->aggregator = NULL;
1921 port->next_port_in_aggregator = NULL;
1922
1923 __disable_port(port);
1924 __initialize_port_locks(port);
1925
1926
1927 // aggregator initialization
1928 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1929
1930 ad_initialize_agg(aggregator);
1931
1932 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1933 aggregator->aggregator_identifier = (++aggregator_identifier);
1934 aggregator->slave = slave;
1935 aggregator->is_active = 0;
1936 aggregator->num_of_ports = 0;
1937 }
1938
1939 return 0;
1940}
1941
1942/**
1943 * bond_3ad_unbind_slave - deinitialize a slave's port
1944 * @slave: slave struct to work on
1945 *
1946 * Search for the aggregator that is related to this port, remove the
1947 * aggregator and assign another aggregator for other port related to it
1948 * (if any), and remove the port.
1949 */
1950void bond_3ad_unbind_slave(struct slave *slave)
1951{
1952 struct port *port, *prev_port, *temp_port;
1953 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1954 int select_new_active_agg = 0;
1955
1956 // find the aggregator related to this slave
1957 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1958
1959 // find the port related to this slave
1960 port = &(SLAVE_AD_INFO(slave).port);
1961
1962 // if slave is null, the whole port is not initialized
1963 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001964 printk(KERN_WARNING DRV_NAME ": Warning: %s: Trying to "
1965 "unbind an uninitialized port on %s\n",
1966 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return;
1968 }
1969
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001970 pr_debug("Unbinding Link Aggregation Group %d\n", aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /* Tell the partner that this port is not suitable for aggregation */
1973 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1974 __update_lacpdu_from_port(port);
1975 ad_lacpdu_send(port);
1976
1977 // check if this aggregator is occupied
1978 if (aggregator->lag_ports) {
1979 // check if there are other ports related to this aggregator except
1980 // the port related to this slave(thats ensure us that there is a
1981 // reason to search for new aggregator, and that we will find one
1982 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
1983 // find new aggregator for the related port(s)
1984 new_aggregator = __get_first_agg(port);
1985 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
1986 // if the new aggregator is empty, or it connected to to our port only
1987 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
1988 break;
1989 }
1990 }
1991 // if new aggregator found, copy the aggregator's parameters
1992 // and connect the related lag_ports to the new aggregator
1993 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001994 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator->aggregator_identifier, new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001997 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
1998 aggregator->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 // select new active aggregator
2000 select_new_active_agg = 1;
2001 }
2002
2003 new_aggregator->is_individual = aggregator->is_individual;
2004 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2005 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2006 new_aggregator->partner_system = aggregator->partner_system;
2007 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2008 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2009 new_aggregator->receive_state = aggregator->receive_state;
2010 new_aggregator->transmit_state = aggregator->transmit_state;
2011 new_aggregator->lag_ports = aggregator->lag_ports;
2012 new_aggregator->is_active = aggregator->is_active;
2013 new_aggregator->num_of_ports = aggregator->num_of_ports;
2014
2015 // update the information that is written on the ports about the aggregator
2016 for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) {
2017 temp_port->aggregator=new_aggregator;
2018 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2019 }
2020
2021 // clear the aggregator
2022 ad_clear_agg(aggregator);
2023
2024 if (select_new_active_agg) {
2025 ad_agg_selection_logic(__get_first_agg(port));
2026 }
2027 } else {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002028 printk(KERN_WARNING DRV_NAME ": %s: Warning: unbinding aggregator, "
2029 "and could not find a new aggregator for its ports\n",
2030 slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032 } else { // in case that the only port related to this aggregator is the one we want to remove
2033 select_new_active_agg = aggregator->is_active;
2034 // clear the aggregator
2035 ad_clear_agg(aggregator);
2036 if (select_new_active_agg) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002037 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2038 slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 // select new active aggregator
2040 ad_agg_selection_logic(__get_first_agg(port));
2041 }
2042 }
2043 }
2044
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002045 pr_debug("Unbinding port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 // find the aggregator that this port is connected to
2047 temp_aggregator = __get_first_agg(port);
2048 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2049 prev_port = NULL;
2050 // search the port in the aggregator's related ports
2051 for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) {
2052 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2053 if (prev_port) {
2054 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2055 } else {
2056 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2057 }
2058 temp_aggregator->num_of_ports--;
2059 if (temp_aggregator->num_of_ports==0) {
2060 select_new_active_agg = temp_aggregator->is_active;
2061 // clear the aggregator
2062 ad_clear_agg(temp_aggregator);
2063 if (select_new_active_agg) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002064 printk(KERN_INFO DRV_NAME ": %s: Removing an active aggregator\n",
2065 slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 // select new active aggregator
2067 ad_agg_selection_logic(__get_first_agg(port));
2068 }
2069 }
2070 break;
2071 }
2072 }
2073 }
2074 port->slave=NULL;
2075}
2076
2077/**
2078 * bond_3ad_state_machine_handler - handle state machines timeout
2079 * @bond: bonding struct to work on
2080 *
2081 * The state machine handling concept in this module is to check every tick
2082 * which state machine should operate any function. The execution order is
2083 * round robin, so when we have an interaction between state machines, the
2084 * reply of one to each other might be delayed until next tick.
2085 *
2086 * This function also complete the initialization when the agg_select_timer
2087 * times out, and it selects an aggregator for the ports that are yet not
2088 * related to any aggregator, and selects the active aggregator for a bond.
2089 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002090void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002092 struct bonding *bond = container_of(work, struct bonding,
2093 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct port *port;
2095 struct aggregator *aggregator;
2096
2097 read_lock(&bond->lock);
2098
2099 if (bond->kill_timers) {
2100 goto out;
2101 }
2102
2103 //check if there are any slaves
2104 if (bond->slave_cnt == 0) {
2105 goto re_arm;
2106 }
2107
2108 // check if agg_select_timer timer after initialize is timed out
2109 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2110 // select the active aggregator for the bond
2111 if ((port = __get_first_port(bond))) {
2112 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002113 printk(KERN_WARNING DRV_NAME ": %s: Warning: bond's first port is "
2114 "uninitialized\n", bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto re_arm;
2116 }
2117
2118 aggregator = __get_first_agg(port);
2119 ad_agg_selection_logic(aggregator);
2120 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002121 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
2124 // for each port run the state machines
2125 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2126 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002127 printk(KERN_WARNING DRV_NAME ": %s: Warning: Found an uninitialized "
2128 "port\n", bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto re_arm;
2130 }
2131
2132 ad_rx_machine(NULL, port);
2133 ad_periodic_machine(port);
2134 ad_port_selection_logic(port);
2135 ad_mux_machine(port);
2136 ad_tx_machine(port);
2137
2138 // turn off the BEGIN bit, since we already handled it
2139 if (port->sm_vars & AD_PORT_BEGIN) {
2140 port->sm_vars &= ~AD_PORT_BEGIN;
2141 }
2142 }
2143
2144re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002145 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146out:
2147 read_unlock(&bond->lock);
2148}
2149
2150/**
2151 * bond_3ad_rx_indication - handle a received frame
2152 * @lacpdu: received lacpdu
2153 * @slave: slave struct to work on
2154 * @length: length of the data received
2155 *
2156 * It is assumed that frames that were sent on this NIC don't returned as new
2157 * received frames (loopback). Since only the payload is given to this
2158 * function, it check for loopback.
2159 */
2160static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
2161{
2162 struct port *port;
2163
2164 if (length >= sizeof(struct lacpdu)) {
2165
2166 port = &(SLAVE_AD_INFO(slave).port);
2167
2168 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002169 printk(KERN_WARNING DRV_NAME ": %s: Warning: port of slave %s is "
2170 "uninitialized\n", slave->dev->name, slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return;
2172 }
2173
2174 switch (lacpdu->subtype) {
2175 case AD_TYPE_LACPDU:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002176 pr_debug("Received LACPDU on port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 ad_rx_machine(lacpdu, port);
2178 break;
2179
2180 case AD_TYPE_MARKER:
2181 // No need to convert fields to Little Endian since we don't use the marker's fields.
2182
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002183 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 case AD_MARKER_INFORMATION_SUBTYPE:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002185 pr_debug("Received Marker Information on port %d\n", port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002186 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 break;
2188
2189 case AD_MARKER_RESPONSE_SUBTYPE:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002190 pr_debug("Received Marker Response on port %d\n", port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002191 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 break;
2193
2194 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002195 pr_debug("Received an unknown Marker subtype on slot %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197 }
2198 }
2199}
2200
2201/**
2202 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2203 * @slave: slave struct to work on
2204 *
2205 * Handle reselection of aggregator (if needed) for this port.
2206 */
2207void bond_3ad_adapter_speed_changed(struct slave *slave)
2208{
2209 struct port *port;
2210
2211 port = &(SLAVE_AD_INFO(slave).port);
2212
2213 // if slave is null, the whole port is not initialized
2214 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002215 printk(KERN_WARNING DRV_NAME ": Warning: %s: speed "
2216 "changed for uninitialized port on %s\n",
2217 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return;
2219 }
2220
2221 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2222 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002223 pr_debug("Port %d changed speed\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 // there is no need to reselect a new aggregator, just signal the
2225 // state machines to reinitialize
2226 port->sm_vars |= AD_PORT_BEGIN;
2227}
2228
2229/**
2230 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2231 * @slave: slave struct to work on
2232 *
2233 * Handle reselection of aggregator (if needed) for this port.
2234 */
2235void bond_3ad_adapter_duplex_changed(struct slave *slave)
2236{
2237 struct port *port;
2238
2239 port=&(SLAVE_AD_INFO(slave).port);
2240
2241 // if slave is null, the whole port is not initialized
2242 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002243 printk(KERN_WARNING DRV_NAME ": %s: Warning: duplex changed "
2244 "for uninitialized port on %s\n",
2245 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return;
2247 }
2248
2249 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2250 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002251 pr_debug("Port %d changed duplex\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 // there is no need to reselect a new aggregator, just signal the
2253 // state machines to reinitialize
2254 port->sm_vars |= AD_PORT_BEGIN;
2255}
2256
2257/**
2258 * bond_3ad_handle_link_change - handle a slave's link status change indication
2259 * @slave: slave struct to work on
2260 * @status: whether the link is now up or down
2261 *
2262 * Handle reselection of aggregator (if needed) for this port.
2263 */
2264void bond_3ad_handle_link_change(struct slave *slave, char link)
2265{
2266 struct port *port;
2267
2268 port = &(SLAVE_AD_INFO(slave).port);
2269
2270 // if slave is null, the whole port is not initialized
2271 if (!port->slave) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002272 printk(KERN_WARNING DRV_NAME ": Warning: %s: link status changed for "
2273 "uninitialized port on %s\n",
2274 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return;
2276 }
2277
2278 // 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)
2279 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2280 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002281 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2283 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
2284 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
2285 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
2286 } else {
2287 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002288 port->is_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
2290 port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS);
2291 }
2292 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2293 // there is no need to reselect a new aggregator, just signal the
2294 // state machines to reinitialize
2295 port->sm_vars |= AD_PORT_BEGIN;
2296}
2297
Jay Vosburghff59c452006-03-27 13:27:43 -08002298/*
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002299 * set link state for bonding master: if we have an active
Jay Vosburghff59c452006-03-27 13:27:43 -08002300 * aggregator, we're up, if not, we're down. Presumes that we cannot
2301 * have an active aggregator if there are no slaves with link up.
2302 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002303 * This behavior complies with IEEE 802.3 section 43.3.9.
2304 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002305 * Called by bond_set_carrier(). Return zero if carrier state does not
2306 * change, nonzero if it does.
2307 */
2308int bond_3ad_set_carrier(struct bonding *bond)
2309{
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002310 if (__get_active_agg(&(SLAVE_AD_INFO(bond->first_slave).aggregator))) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002311 if (!netif_carrier_ok(bond->dev)) {
2312 netif_carrier_on(bond->dev);
2313 return 1;
2314 }
2315 return 0;
2316 }
2317
2318 if (netif_carrier_ok(bond->dev)) {
2319 netif_carrier_off(bond->dev);
2320 return 1;
2321 }
2322 return 0;
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/**
2326 * bond_3ad_get_active_agg_info - get information of the active aggregator
2327 * @bond: bonding struct to work on
2328 * @ad_info: ad_info struct to fill with the bond's info
2329 *
2330 * Returns: 0 on success
2331 * < 0 on error
2332 */
2333int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2334{
2335 struct aggregator *aggregator = NULL;
2336 struct port *port;
2337
2338 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2339 if (port->aggregator && port->aggregator->is_active) {
2340 aggregator = port->aggregator;
2341 break;
2342 }
2343 }
2344
2345 if (aggregator) {
2346 ad_info->aggregator_id = aggregator->aggregator_identifier;
2347 ad_info->ports = aggregator->num_of_ports;
2348 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2349 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2350 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2351 return 0;
2352 }
2353
2354 return -1;
2355}
2356
2357int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2358{
2359 struct slave *slave, *start_at;
Wang Chen454d7c92008-11-12 23:37:49 -08002360 struct bonding *bond = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 int slave_agg_no;
2362 int slaves_in_agg;
2363 int agg_id;
2364 int i;
2365 struct ad_info ad_info;
2366 int res = 1;
2367
2368 /* make sure that the slaves list will
2369 * not change during tx
2370 */
2371 read_lock(&bond->lock);
2372
2373 if (!BOND_IS_OK(bond)) {
2374 goto out;
2375 }
2376
2377 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002378 printk(KERN_DEBUG DRV_NAME ": %s: Error: "
2379 "bond_3ad_get_active_agg_info failed\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto out;
2381 }
2382
2383 slaves_in_agg = ad_info.ports;
2384 agg_id = ad_info.aggregator_id;
2385
2386 if (slaves_in_agg == 0) {
2387 /*the aggregator is empty*/
Mitch Williams4e0952c2005-11-09 10:34:57 -08002388 printk(KERN_DEBUG DRV_NAME ": %s: Error: active "
2389 "aggregator is empty\n",
2390 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 goto out;
2392 }
2393
Jay Vosburgh169a3e62005-06-26 17:54:11 -04002394 slave_agg_no = bond->xmit_hash_policy(skb, dev, slaves_in_agg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 bond_for_each_slave(bond, slave, i) {
2397 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2398
2399 if (agg && (agg->aggregator_identifier == agg_id)) {
2400 slave_agg_no--;
2401 if (slave_agg_no < 0) {
2402 break;
2403 }
2404 }
2405 }
2406
2407 if (slave_agg_no >= 0) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08002408 printk(KERN_ERR DRV_NAME ": %s: Error: Couldn't find a slave to tx on "
2409 "for aggregator ID %d\n", dev->name, agg_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto out;
2411 }
2412
2413 start_at = slave;
2414
2415 bond_for_each_slave_from(bond, slave, i, start_at) {
2416 int slave_agg_id = 0;
2417 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2418
2419 if (agg) {
2420 slave_agg_id = agg->aggregator_identifier;
2421 }
2422
2423 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2424 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2425 break;
2426 }
2427 }
2428
2429out:
2430 if (res) {
2431 /* no suitable interface, frame not sent */
2432 dev_kfree_skb(skb);
2433 }
2434 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07002435 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002438int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Wang Chen454d7c92008-11-12 23:37:49 -08002440 struct bonding *bond = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct slave *slave = NULL;
2442 int ret = NET_RX_DROP;
2443
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002444 if (dev_net(dev) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -07002445 goto out;
2446
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002447 if (!(dev->flags & IFF_MASTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 read_lock(&bond->lock);
Wang Chen454d7c92008-11-12 23:37:49 -08002451 slave = bond_get_slave_by_dev((struct bonding *)netdev_priv(dev),
2452 orig_dev);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002453 if (!slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
2457
2458 ret = NET_RX_SUCCESS;
2459
2460out_unlock:
2461 read_unlock(&bond->lock);
2462out:
2463 dev_kfree_skb(skb);
2464
2465 return ret;
2466}
2467