blob: 6b06b00af359a6347ec3a564344f47fc018376b4 [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
Joe Perchesa4aee5c2009-12-13 20:06:07 -080023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/skbuff.h>
26#include <linux/if_ether.h>
27#include <linux/netdevice.h>
28#include <linux/spinlock.h>
29#include <linux/ethtool.h>
Jay Vosburghfd989c82008-11-04 17:51:16 -080030#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/if_bonding.h>
32#include <linux/pkt_sched.h>
Eric W. Biedermane730c152007-09-17 11:53:39 -070033#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "bonding.h"
35#include "bond_3ad.h"
36
37// General definitions
38#define AD_SHORT_TIMEOUT 1
39#define AD_LONG_TIMEOUT 0
40#define AD_STANDBY 0x2
41#define AD_MAX_TX_IN_SECOND 3
42#define AD_COLLECTOR_MAX_DELAY 0
43
44// Timer definitions(43.4.4 in the 802.3ad standard)
45#define AD_FAST_PERIODIC_TIME 1
46#define AD_SLOW_PERIODIC_TIME 30
47#define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
48#define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
49#define AD_CHURN_DETECTION_TIME 60
50#define AD_AGGREGATE_WAIT_TIME 2
51
52// Port state definitions(43.4.2.2 in the 802.3ad standard)
53#define AD_STATE_LACP_ACTIVITY 0x1
54#define AD_STATE_LACP_TIMEOUT 0x2
55#define AD_STATE_AGGREGATION 0x4
56#define AD_STATE_SYNCHRONIZATION 0x8
57#define AD_STATE_COLLECTING 0x10
58#define AD_STATE_DISTRIBUTING 0x20
59#define AD_STATE_DEFAULTED 0x40
60#define AD_STATE_EXPIRED 0x80
61
62// Port Variables definitions used by the State Machines(43.4.7 in the 802.3ad standard)
63#define AD_PORT_BEGIN 0x1
64#define AD_PORT_LACP_ENABLED 0x2
65#define AD_PORT_ACTOR_CHURN 0x4
66#define AD_PORT_PARTNER_CHURN 0x8
67#define AD_PORT_READY 0x10
68#define AD_PORT_READY_N 0x20
69#define AD_PORT_MATCHED 0x40
70#define AD_PORT_STANDBY 0x80
71#define AD_PORT_SELECTED 0x100
72#define AD_PORT_MOVED 0x200
73
74// Port Key definitions
75// key is determined according to the link speed, duplex and
76// user key(which is yet not supported)
77// ------------------------------------------------------------
78// Port key : | User key | Speed |Duplex|
79// ------------------------------------------------------------
80// 16 6 1 0
81#define AD_DUPLEX_KEY_BITS 0x1
82#define AD_SPEED_KEY_BITS 0x3E
83#define AD_USER_KEY_BITS 0xFFC0
84
85//dalloun
86#define AD_LINK_SPEED_BITMASK_1MBPS 0x1
87#define AD_LINK_SPEED_BITMASK_10MBPS 0x2
88#define AD_LINK_SPEED_BITMASK_100MBPS 0x4
89#define AD_LINK_SPEED_BITMASK_1000MBPS 0x8
Jay Vosburgh94dbffd2006-09-22 21:52:15 -070090#define AD_LINK_SPEED_BITMASK_10000MBPS 0x10
Linus Torvalds1da177e2005-04-16 15:20:36 -070091//endalloun
92
93// compare MAC addresses
94#define MAC_ADDRESS_COMPARE(A, B) memcmp(A, B, ETH_ALEN)
95
Bandan Das128ea6c2010-10-16 20:19:58 +000096static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static u16 ad_ticks_per_sec;
98static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
99
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800100static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102// ================= main 802.3ad protocol functions ==================
103static int ad_lacpdu_send(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700104static int ad_marker_send(struct port *port, struct bond_marker *marker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void ad_mux_machine(struct port *port);
106static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
107static void ad_tx_machine(struct port *port);
108static void ad_periodic_machine(struct port *port);
109static void ad_port_selection_logic(struct port *port);
110static void ad_agg_selection_logic(struct aggregator *aggregator);
111static void ad_clear_agg(struct aggregator *aggregator);
112static void ad_initialize_agg(struct aggregator *aggregator);
113static void ad_initialize_port(struct port *port, int lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void ad_enable_collecting_distributing(struct port *port);
115static void ad_disable_collecting_distributing(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700116static void ad_marker_info_received(struct bond_marker *marker_info, struct port *port);
117static void ad_marker_response_received(struct bond_marker *marker, struct port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119
120/////////////////////////////////////////////////////////////////////////////////
121// ================= api to bonding and kernel code ==================
122/////////////////////////////////////////////////////////////////////////////////
123
124/**
125 * __get_bond_by_port - get the port's bonding struct
126 * @port: the port we're looking at
127 *
128 * Return @port's bonding struct, or %NULL if it can't be found.
129 */
130static inline struct bonding *__get_bond_by_port(struct port *port)
131{
132 if (port->slave == NULL) {
133 return NULL;
134 }
135
136 return bond_get_bond_by_slave(port->slave);
137}
138
139/**
140 * __get_first_port - get the first port in the bond
141 * @bond: the bond we're looking at
142 *
143 * Return the port of the first slave in @bond, or %NULL if it can't be found.
144 */
145static inline struct port *__get_first_port(struct bonding *bond)
146{
147 if (bond->slave_cnt == 0) {
148 return NULL;
149 }
150
151 return &(SLAVE_AD_INFO(bond->first_slave).port);
152}
153
154/**
155 * __get_next_port - get the next port in the bond
156 * @port: the port we're looking at
157 *
158 * Return the port of the slave that is next in line of @port's slave in the
159 * bond, or %NULL if it can't be found.
160 */
161static inline struct port *__get_next_port(struct port *port)
162{
163 struct bonding *bond = __get_bond_by_port(port);
164 struct slave *slave = port->slave;
165
166 // If there's no bond for this port, or this is the last slave
167 if ((bond == NULL) || (slave->next == bond->first_slave)) {
168 return NULL;
169 }
170
171 return &(SLAVE_AD_INFO(slave->next).port);
172}
173
174/**
175 * __get_first_agg - get the first aggregator in the bond
176 * @bond: the bond we're looking at
177 *
178 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
179 * found.
180 */
181static inline struct aggregator *__get_first_agg(struct port *port)
182{
183 struct bonding *bond = __get_bond_by_port(port);
184
185 // If there's no bond for this port, or bond has no slaves
186 if ((bond == NULL) || (bond->slave_cnt == 0)) {
187 return NULL;
188 }
189
190 return &(SLAVE_AD_INFO(bond->first_slave).aggregator);
191}
192
193/**
194 * __get_next_agg - get the next aggregator in the bond
195 * @aggregator: the aggregator we're looking at
196 *
197 * Return the aggregator of the slave that is next in line of @aggregator's
198 * slave in the bond, or %NULL if it can't be found.
199 */
200static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
201{
202 struct slave *slave = aggregator->slave;
203 struct bonding *bond = bond_get_bond_by_slave(slave);
204
205 // If there's no bond for this aggregator, or this is the last slave
206 if ((bond == NULL) || (slave->next == bond->first_slave)) {
207 return NULL;
208 }
209
210 return &(SLAVE_AD_INFO(slave->next).aggregator);
211}
212
Jay Vosburghfd989c82008-11-04 17:51:16 -0800213/*
214 * __agg_has_partner
215 *
216 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
217 * address for the partner). Return 0 if not.
218 */
219static inline int __agg_has_partner(struct aggregator *agg)
220{
221 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/**
225 * __disable_port - disable the port's slave
226 * @port: the port we're looking at
227 *
228 */
229static inline void __disable_port(struct port *port)
230{
231 bond_set_slave_inactive_flags(port->slave);
232}
233
234/**
235 * __enable_port - enable the port's slave, if it's up
236 * @port: the port we're looking at
237 *
238 */
239static inline void __enable_port(struct port *port)
240{
241 struct slave *slave = port->slave;
242
243 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev)) {
244 bond_set_slave_active_flags(slave);
245 }
246}
247
248/**
249 * __port_is_enabled - check if the port's slave is in active state
250 * @port: the port we're looking at
251 *
252 */
253static inline int __port_is_enabled(struct port *port)
254{
Eric Dumazet807540b2010-09-23 05:40:09 +0000255 return port->slave->state == BOND_STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/**
259 * __get_agg_selection_mode - get the aggregator selection mode
260 * @port: the port we're looking at
261 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800262 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
264static inline u32 __get_agg_selection_mode(struct port *port)
265{
266 struct bonding *bond = __get_bond_by_port(port);
267
268 if (bond == NULL) {
Jay Vosburghfd989c82008-11-04 17:51:16 -0800269 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
272 return BOND_AD_INFO(bond).agg_select_mode;
273}
274
275/**
276 * __check_agg_selection_timer - check if the selection timer has expired
277 * @port: the port we're looking at
278 *
279 */
280static inline int __check_agg_selection_timer(struct port *port)
281{
282 struct bonding *bond = __get_bond_by_port(port);
283
284 if (bond == NULL) {
285 return 0;
286 }
287
288 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
289}
290
291/**
292 * __get_rx_machine_lock - lock the port's RX machine
293 * @port: the port we're looking at
294 *
295 */
296static inline void __get_rx_machine_lock(struct port *port)
297{
Jay Vosburgh2bf86b72008-03-21 22:29:33 -0700298 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/**
302 * __release_rx_machine_lock - unlock the port's RX machine
303 * @port: the port we're looking at
304 *
305 */
306static inline void __release_rx_machine_lock(struct port *port)
307{
Jay Vosburgh2bf86b72008-03-21 22:29:33 -0700308 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/**
312 * __get_link_speed - get a port's speed
313 * @port: the port we're looking at
314 *
315 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
316 * 0,
317 * %AD_LINK_SPEED_BITMASK_10MBPS,
318 * %AD_LINK_SPEED_BITMASK_100MBPS,
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700319 * %AD_LINK_SPEED_BITMASK_1000MBPS,
320 * %AD_LINK_SPEED_BITMASK_10000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
322static u16 __get_link_speed(struct port *port)
323{
324 struct slave *slave = port->slave;
325 u16 speed;
326
327 /* this if covers only a special case: when the configuration starts with
328 * link down, it sets the speed to 0.
329 * This is done in spite of the fact that the e100 driver reports 0 to be
330 * compatible with MVT in the future.*/
331 if (slave->link != BOND_LINK_UP) {
Bandan Das128ea6c2010-10-16 20:19:58 +0000332 speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 } else {
334 switch (slave->speed) {
335 case SPEED_10:
336 speed = AD_LINK_SPEED_BITMASK_10MBPS;
337 break;
338
339 case SPEED_100:
340 speed = AD_LINK_SPEED_BITMASK_100MBPS;
341 break;
342
343 case SPEED_1000:
344 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
345 break;
346
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700347 case SPEED_10000:
348 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
349 break;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 default:
352 speed = 0; // unknown speed value from ethtool. shouldn't happen
353 break;
354 }
355 }
356
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800357 pr_debug("Port %d Received link speed %d update from adapter\n",
358 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return speed;
360}
361
362/**
363 * __get_duplex - get a port's duplex
364 * @port: the port we're looking at
365 *
366 * Return @port's duplex in 802.3ad bitmask format. i.e.:
367 * 0x01 if in full duplex
368 * 0x00 otherwise
369 */
370static u8 __get_duplex(struct port *port)
371{
372 struct slave *slave = port->slave;
373
374 u8 retval;
375
376 // handling a special case: when the configuration starts with
377 // link down, it sets the duplex to 0.
378 if (slave->link != BOND_LINK_UP) {
Bandan Das128ea6c2010-10-16 20:19:58 +0000379 retval = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else {
381 switch (slave->duplex) {
382 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000383 retval = 0x1;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800384 pr_debug("Port %d Received status full duplex update from adapter\n",
385 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 case DUPLEX_HALF:
388 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000389 retval = 0x0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800390 pr_debug("Port %d Received status NOT full duplex update from adapter\n",
391 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
393 }
394 }
395 return retval;
396}
397
398/**
399 * __initialize_port_locks - initialize a port's RX machine spinlock
400 * @port: the port we're looking at
401 *
402 */
403static inline void __initialize_port_locks(struct port *port)
404{
405 // make sure it isn't called twice
406 spin_lock_init(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
407}
408
409//conversions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/**
412 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
413 * @timer_type: which timer to operate
414 * @par: timer parameter. see below
415 *
416 * If @timer_type is %current_while_timer, @par indicates long/short timer.
417 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
418 * %SLOW_PERIODIC_TIME.
419 */
420static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
421{
Bandan Das128ea6c2010-10-16 20:19:58 +0000422 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 switch (timer_type) {
425 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
426 if (par) { // for short or long timeout
427 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
428 } else {
429 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
430 }
431 break;
432 case AD_ACTOR_CHURN_TIMER: // for local churn machine
433 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
434 break;
435 case AD_PERIODIC_TIMER: // for periodic machine
436 retval = (par*ad_ticks_per_sec); // long timeout
437 break;
438 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
439 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
440 break;
441 case AD_WAIT_WHILE_TIMER: // for selection machine
442 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
443 break;
444 }
445 return retval;
446}
447
448
449/////////////////////////////////////////////////////////////////////////////////
450// ================= ad_rx_machine helper functions ==================
451/////////////////////////////////////////////////////////////////////////////////
452
453/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000454 * __choose_matched - update a port's matched variable from a received lacpdu
455 * @lacpdu: the lacpdu we've received
456 * @port: the port we're looking at
457 *
458 * Update the value of the matched variable, using parameter values from a
459 * newly received lacpdu. Parameter values for the partner carried in the
460 * received PDU are compared with the corresponding operational parameter
461 * values for the actor. Matched is set to TRUE if all of these parameters
462 * match and the PDU parameter partner_state.aggregation has the same value as
463 * actor_oper_port_state.aggregation and lacp will actively maintain the link
464 * in the aggregation. Matched is also set to TRUE if the value of
465 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
466 * an individual link and lacp will actively maintain the link. Otherwise,
467 * matched is set to FALSE. LACP is considered to be actively maintaining the
468 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
469 * the actor's actor_oper_port_state.lacp_activity and the PDU's
470 * partner_state.lacp_activity variables are TRUE.
471 *
472 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
473 * used here to implement the language from 802.3ad 43.4.9 that requires
474 * recordPDU to "match" the LACPDU parameters to the stored values.
475 */
476static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
477{
478 // check if all parameters are alike
479 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
480 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
481 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
482 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
483 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
484 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
485 // or this is individual link(aggregation == FALSE)
486 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
487 ) {
488 // update the state machine Matched variable
489 port->sm_vars |= AD_PORT_MATCHED;
490 } else {
491 port->sm_vars &= ~AD_PORT_MATCHED;
492 }
493}
494
495/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * __record_pdu - record parameters from a received lacpdu
497 * @lacpdu: the lacpdu we've received
498 * @port: the port we're looking at
499 *
500 * Record the parameter values for the Actor carried in a received lacpdu as
501 * the current partner operational parameter values and sets
502 * actor_oper_port_state.defaulted to FALSE.
503 */
504static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
505{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800507 struct port_params *partner = &port->partner_oper;
508
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000509 __choose_matched(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 // record the new parameter values for the partner operational
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800511 partner->port_number = ntohs(lacpdu->actor_port);
512 partner->port_priority = ntohs(lacpdu->actor_port_priority);
513 partner->system = lacpdu->actor_system;
514 partner->system_priority = ntohs(lacpdu->actor_system_priority);
515 partner->key = ntohs(lacpdu->actor_key);
516 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 // set actor_oper_port_state.defaulted to FALSE
519 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
520
521 // set the partner sync. to on if the partner is sync. and the port is matched
522 if ((port->sm_vars & AD_PORT_MATCHED) && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800523 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800525 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 }
528}
529
530/**
531 * __record_default - record default parameters
532 * @port: the port we're looking at
533 *
534 * This function records the default parameter values for the partner carried
535 * in the Partner Admin parameters as the current partner operational parameter
536 * values and sets actor_oper_port_state.defaulted to TRUE.
537 */
538static void __record_default(struct port *port)
539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (port) {
541 // record the partner admin parameters
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800542 memcpy(&port->partner_oper, &port->partner_admin,
543 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 // set actor_oper_port_state.defaulted to true
546 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
547 }
548}
549
550/**
551 * __update_selected - update a port's Selected variable from a received lacpdu
552 * @lacpdu: the lacpdu we've received
553 * @port: the port we're looking at
554 *
555 * Update the value of the selected variable, using parameter values from a
556 * newly received lacpdu. The parameter values for the Actor carried in the
557 * received PDU are compared with the corresponding operational parameter
558 * values for the ports partner. If one or more of the comparisons shows that
559 * the value(s) received in the PDU differ from the current operational values,
560 * then selected is set to FALSE and actor_oper_port_state.synchronization is
561 * set to out_of_sync. Otherwise, selected remains unchanged.
562 */
563static void __update_selected(struct lacpdu *lacpdu, struct port *port)
564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800566 const struct port_params *partner = &port->partner_oper;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 // check if any parameter is different
Joe Perches8e95a202009-12-03 07:58:21 +0000569 if (ntohs(lacpdu->actor_port) != partner->port_number ||
570 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
571 MAC_ADDRESS_COMPARE(&lacpdu->actor_system, &partner->system) ||
572 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
573 ntohs(lacpdu->actor_key) != partner->key ||
574 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 // update the state machine Selected variable
576 port->sm_vars &= ~AD_PORT_SELECTED;
577 }
578 }
579}
580
581/**
582 * __update_default_selected - update a port's Selected variable from Partner
583 * @port: the port we're looking at
584 *
585 * This function updates the value of the selected variable, using the partner
586 * administrative parameter values. The administrative values are compared with
587 * the corresponding operational parameter values for the partner. If one or
588 * more of the comparisons shows that the administrative value(s) differ from
589 * the current operational values, then Selected is set to FALSE and
590 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
591 * Selected remains unchanged.
592 */
593static void __update_default_selected(struct port *port)
594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800596 const struct port_params *admin = &port->partner_admin;
597 const struct port_params *oper = &port->partner_oper;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 // check if any parameter is different
Joe Perches8e95a202009-12-03 07:58:21 +0000600 if (admin->port_number != oper->port_number ||
601 admin->port_priority != oper->port_priority ||
602 MAC_ADDRESS_COMPARE(&admin->system, &oper->system) ||
603 admin->system_priority != oper->system_priority ||
604 admin->key != oper->key ||
605 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800606 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 // update the state machine Selected variable
608 port->sm_vars &= ~AD_PORT_SELECTED;
609 }
610 }
611}
612
613/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * __update_ntt - update a port's ntt variable from a received lacpdu
615 * @lacpdu: the lacpdu we've received
616 * @port: the port we're looking at
617 *
618 * Updates the value of the ntt variable, using parameter values from a newly
619 * received lacpdu. The parameter values for the partner carried in the
620 * received PDU are compared with the corresponding operational parameter
621 * values for the Actor. If one or more of the comparisons shows that the
622 * value(s) received in the PDU differ from the current operational values,
623 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
624 */
625static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
626{
627 // validate lacpdu and port
628 if (lacpdu && port) {
629 // check if any parameter is different
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700630 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
631 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700633 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
634 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
636 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
637 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
638 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
639 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800640
641 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644}
645
646/**
647 * __attach_bond_to_agg
648 * @port: the port we're looking at
649 *
650 * Handle the attaching of the port's control parser/multiplexer and the
651 * aggregator. This function does nothing since the parser/multiplexer of the
652 * receive and the parser/multiplexer of the aggregator are already combined.
653 */
654static void __attach_bond_to_agg(struct port *port)
655{
Bandan Das128ea6c2010-10-16 20:19:58 +0000656 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 // This function does nothing since the parser/multiplexer of the receive
658 // and the parser/multiplexer of the aggregator are already combined
659}
660
661/**
662 * __detach_bond_from_agg
663 * @port: the port we're looking at
664 *
665 * Handle the detaching of the port's control parser/multiplexer from the
666 * aggregator. This function does nothing since the parser/multiplexer of the
667 * receive and the parser/multiplexer of the aggregator are already combined.
668 */
669static void __detach_bond_from_agg(struct port *port)
670{
Bandan Das128ea6c2010-10-16 20:19:58 +0000671 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 // This function does nothing sience the parser/multiplexer of the receive
673 // and the parser/multiplexer of the aggregator are already combined
674}
675
676/**
677 * __agg_ports_are_ready - check if all ports in an aggregator are ready
678 * @aggregator: the aggregator we're looking at
679 *
680 */
681static int __agg_ports_are_ready(struct aggregator *aggregator)
682{
683 struct port *port;
684 int retval = 1;
685
686 if (aggregator) {
687 // scan all ports in this aggregator to verfy if they are all ready
Bandan Das128ea6c2010-10-16 20:19:58 +0000688 for (port = aggregator->lag_ports;
689 port;
690 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!(port->sm_vars & AD_PORT_READY_N)) {
692 retval = 0;
693 break;
694 }
695 }
696 }
697
698 return retval;
699}
700
701/**
702 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
703 * @aggregator: the aggregator we're looking at
704 * @val: Should the ports' ready bit be set on or off
705 *
706 */
707static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
708{
709 struct port *port;
710
Bandan Das128ea6c2010-10-16 20:19:58 +0000711 for (port = aggregator->lag_ports; port;
712 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (val) {
714 port->sm_vars |= AD_PORT_READY;
715 } else {
716 port->sm_vars &= ~AD_PORT_READY;
717 }
718 }
719}
720
721/**
722 * __get_agg_bandwidth - get the total bandwidth of an aggregator
723 * @aggregator: the aggregator we're looking at
724 *
725 */
726static u32 __get_agg_bandwidth(struct aggregator *aggregator)
727{
Bandan Das128ea6c2010-10-16 20:19:58 +0000728 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 u32 basic_speed;
730
731 if (aggregator->num_of_ports) {
732 basic_speed = __get_link_speed(aggregator->lag_ports);
733 switch (basic_speed) {
734 case AD_LINK_SPEED_BITMASK_1MBPS:
735 bandwidth = aggregator->num_of_ports;
736 break;
737 case AD_LINK_SPEED_BITMASK_10MBPS:
738 bandwidth = aggregator->num_of_ports * 10;
739 break;
740 case AD_LINK_SPEED_BITMASK_100MBPS:
741 bandwidth = aggregator->num_of_ports * 100;
742 break;
743 case AD_LINK_SPEED_BITMASK_1000MBPS:
744 bandwidth = aggregator->num_of_ports * 1000;
745 break;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700746 case AD_LINK_SPEED_BITMASK_10000MBPS:
747 bandwidth = aggregator->num_of_ports * 10000;
748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000750 bandwidth = 0; /*to silence the compiler ....*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 }
753 return bandwidth;
754}
755
756/**
757 * __get_active_agg - get the current active aggregator
758 * @aggregator: the aggregator we're looking at
759 *
760 */
761static struct aggregator *__get_active_agg(struct aggregator *aggregator)
762{
763 struct aggregator *retval = NULL;
764
765 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
766 if (aggregator->is_active) {
767 retval = aggregator;
768 break;
769 }
770 }
771
772 return retval;
773}
774
775/**
776 * __update_lacpdu_from_port - update a port's lacpdu fields
777 * @port: the port we're looking at
778 *
779 */
780static inline void __update_lacpdu_from_port(struct port *port)
781{
782 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800783 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* update current actual Actor parameters */
786 /* lacpdu->subtype initialized
787 * lacpdu->version_number initialized
788 * lacpdu->tlv_type_actor_info initialized
789 * lacpdu->actor_information_length initialized
790 */
791
Al Virod3bb52b2007-08-22 20:06:58 -0400792 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400794 lacpdu->actor_key = htons(port->actor_oper_port_key);
795 lacpdu->actor_port_priority = htons(port->actor_port_priority);
796 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 lacpdu->actor_state = port->actor_oper_port_state;
798
799 /* lacpdu->reserved_3_1 initialized
800 * lacpdu->tlv_type_partner_info initialized
801 * lacpdu->partner_information_length initialized
802 */
803
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800804 lacpdu->partner_system_priority = htons(partner->system_priority);
805 lacpdu->partner_system = partner->system;
806 lacpdu->partner_key = htons(partner->key);
807 lacpdu->partner_port_priority = htons(partner->port_priority);
808 lacpdu->partner_port = htons(partner->port_number);
809 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* lacpdu->reserved_3_2 initialized
812 * lacpdu->tlv_type_collector_info initialized
813 * lacpdu->collector_information_length initialized
814 * collector_max_delay initialized
815 * reserved_12[12] initialized
816 * tlv_type_terminator initialized
817 * terminator_length initialized
818 * reserved_50[50] initialized
819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822//////////////////////////////////////////////////////////////////////////////////////
823// ================= main 802.3ad protocol code ======================================
824//////////////////////////////////////////////////////////////////////////////////////
825
826/**
827 * ad_lacpdu_send - send out a lacpdu packet on a given port
828 * @port: the port we're looking at
829 *
830 * Returns: 0 on success
831 * < 0 on error
832 */
833static int ad_lacpdu_send(struct port *port)
834{
835 struct slave *slave = port->slave;
836 struct sk_buff *skb;
837 struct lacpdu_header *lacpdu_header;
838 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 skb = dev_alloc_skb(length);
841 if (!skb) {
842 return -ENOMEM;
843 }
844
845 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700846 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700847 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 skb->protocol = PKT_TYPE_LACPDU;
849 skb->priority = TC_PRIO_CONTROL;
850
851 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
852
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800853 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800854 /* Note: source addres is set to be the member's PERMANENT address,
855 because we use it to identify loopback lacpdus in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800856 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
857 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 lacpdu_header->lacpdu = port->lacpdu; // struct copy
860
861 dev_queue_xmit(skb);
862
863 return 0;
864}
865
866/**
867 * ad_marker_send - send marker information/response on a given port
868 * @port: the port we're looking at
869 * @marker: marker data to send
870 *
871 * Returns: 0 on success
872 * < 0 on error
873 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700874static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct slave *slave = port->slave;
877 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700878 struct bond_marker_header *marker_header;
879 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 skb = dev_alloc_skb(length + 16);
882 if (!skb) {
883 return -ENOMEM;
884 }
885
886 skb_reserve(skb, 16);
887
888 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700889 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700890 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 skb->protocol = PKT_TYPE_LACPDU;
892
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700893 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800895 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800896 /* Note: source addres is set to be the member's PERMANENT address,
897 because we use it to identify loopback MARKERs in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800898 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
899 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 marker_header->marker = *marker; // struct copy
902
903 dev_queue_xmit(skb);
904
905 return 0;
906}
907
908/**
909 * ad_mux_machine - handle a port's mux state machine
910 * @port: the port we're looking at
911 *
912 */
913static void ad_mux_machine(struct port *port)
914{
915 mux_states_t last_state;
916
917 // keep current State Machine state to compare later if it was changed
918 last_state = port->sm_mux_state;
919
920 if (port->sm_vars & AD_PORT_BEGIN) {
921 port->sm_mux_state = AD_MUX_DETACHED; // next state
922 } else {
923 switch (port->sm_mux_state) {
924 case AD_MUX_DETACHED:
925 if ((port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if SELECTED or STANDBY
926 port->sm_mux_state = AD_MUX_WAITING; // next state
927 }
928 break;
929 case AD_MUX_WAITING:
930 // if SELECTED == FALSE return to DETACH state
931 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
932 port->sm_vars &= ~AD_PORT_READY_N;
933 // in order to withhold the Selection Logic to check all ports READY_N value
934 // every callback cycle to update ready variable, we check READY_N and update READY here
935 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
936 port->sm_mux_state = AD_MUX_DETACHED; // next state
937 break;
938 }
939
940 // check if the wait_while_timer expired
941 if (port->sm_mux_timer_counter && !(--port->sm_mux_timer_counter)) {
942 port->sm_vars |= AD_PORT_READY_N;
943 }
944
945 // in order to withhold the selection logic to check all ports READY_N value
946 // every callback cycle to update ready variable, we check READY_N and update READY here
947 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
948
949 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
950 if ((port->sm_vars & AD_PORT_READY) && !port->sm_mux_timer_counter) {
951 port->sm_mux_state = AD_MUX_ATTACHED; // next state
952 }
953 break;
954 case AD_MUX_ATTACHED:
955 // 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 -0800956 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 -0700957 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
958 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
959 port->sm_vars &= ~AD_PORT_READY_N;
960 // in order to withhold the selection logic to check all ports READY_N value
961 // every callback cycle to update ready variable, we check READY_N and update READY here
962 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
963 port->sm_mux_state = AD_MUX_DETACHED;// next state
964 }
965 break;
966 case AD_MUX_COLLECTING_DISTRIBUTING:
967 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -0800968 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ) {
970 port->sm_mux_state = AD_MUX_ATTACHED;// next state
971
972 } else {
973 // if port state hasn't changed make
974 // sure that a collecting distributing
975 // port in an active aggregator is enabled
976 if (port->aggregator &&
977 port->aggregator->is_active &&
978 !__port_is_enabled(port)) {
979
980 __enable_port(port);
981 }
982 }
983 break;
984 default: //to silence the compiler
985 break;
986 }
987 }
988
989 // check if the state machine was changed
990 if (port->sm_mux_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800991 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
992 port->actor_port_number, last_state,
993 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 switch (port->sm_mux_state) {
995 case AD_MUX_DETACHED:
996 __detach_bond_from_agg(port);
997 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
998 ad_disable_collecting_distributing(port);
999 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1000 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001001 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003 case AD_MUX_WAITING:
1004 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1005 break;
1006 case AD_MUX_ATTACHED:
1007 __attach_bond_to_agg(port);
1008 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
1009 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1010 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1011 ad_disable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001012 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 case AD_MUX_COLLECTING_DISTRIBUTING:
1015 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1016 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
1017 ad_enable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001018 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
1020 default: //to silence the compiler
1021 break;
1022 }
1023 }
1024}
1025
1026/**
1027 * ad_rx_machine - handle a port's rx State Machine
1028 * @lacpdu: the lacpdu we've received
1029 * @port: the port we're looking at
1030 *
1031 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1032 * CURRENT. If timer expired set the state machine in the proper state.
1033 * In other cases, this function checks if we need to switch to other state.
1034 */
1035static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1036{
1037 rx_states_t last_state;
1038
1039 // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback)
1040 __get_rx_machine_lock(port);
1041
1042 // keep current State Machine state to compare later if it was changed
1043 last_state = port->sm_rx_state;
1044
1045 // check if state machine should change state
1046 // first, check if port was reinitialized
1047 if (port->sm_vars & AD_PORT_BEGIN) {
1048 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1049 }
1050 // check if port is not enabled
1051 else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED)) {
1052 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1053 }
1054 // check if new lacpdu arrived
1055 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1056 port->sm_rx_timer_counter = 0; // zero timer
1057 port->sm_rx_state = AD_RX_CURRENT;
1058 } else {
1059 // if timer is on, and if it is expired
1060 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1061 switch (port->sm_rx_state) {
1062 case AD_RX_EXPIRED:
1063 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1064 break;
1065 case AD_RX_CURRENT:
1066 port->sm_rx_state = AD_RX_EXPIRED; // next state
1067 break;
1068 default: //to silence the compiler
1069 break;
1070 }
1071 } else {
1072 // if no lacpdu arrived and no timer is on
1073 switch (port->sm_rx_state) {
1074 case AD_RX_PORT_DISABLED:
1075 if (port->sm_vars & AD_PORT_MOVED) {
1076 port->sm_rx_state = AD_RX_INITIALIZE; // next state
1077 } else if (port->is_enabled && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1078 port->sm_rx_state = AD_RX_EXPIRED; // next state
1079 } else if (port->is_enabled && ((port->sm_vars & AD_PORT_LACP_ENABLED) == 0)) {
1080 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
1081 }
1082 break;
1083 default: //to silence the compiler
1084 break;
1085
1086 }
1087 }
1088 }
1089
1090 // check if the State machine was changed or new lacpdu arrived
1091 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001092 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1093 port->actor_port_number, last_state,
1094 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 switch (port->sm_rx_state) {
1096 case AD_RX_INITIALIZE:
1097 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1098 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1099 } else {
1100 port->sm_vars |= AD_PORT_LACP_ENABLED;
1101 }
1102 port->sm_vars &= ~AD_PORT_SELECTED;
1103 __record_default(port);
1104 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1105 port->sm_vars &= ~AD_PORT_MOVED;
1106 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1107
1108 /*- Fall Through -*/
1109
1110 case AD_RX_PORT_DISABLED:
1111 port->sm_vars &= ~AD_PORT_MATCHED;
1112 break;
1113 case AD_RX_LACP_DISABLED:
1114 port->sm_vars &= ~AD_PORT_SELECTED;
1115 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001116 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 port->sm_vars |= AD_PORT_MATCHED;
1118 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1119 break;
1120 case AD_RX_EXPIRED:
1121 //Reset of the Synchronization flag. (Standard 43.4.12)
1122 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1123 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001124 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 port->sm_vars &= ~AD_PORT_MATCHED;
Julia Lawall8e321c42009-07-11 10:03:55 +00001126 port->partner_oper.port_state |=
1127 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1129 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1130 break;
1131 case AD_RX_DEFAULTED:
1132 __update_default_selected(port);
1133 __record_default(port);
1134 port->sm_vars |= AD_PORT_MATCHED;
1135 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1136 break;
1137 case AD_RX_CURRENT:
1138 // detect loopback situation
1139 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1140 // INFO_RECEIVED_LOOPBACK_FRAMES
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001141 pr_err("%s: An illegal loopback occurred on adapter (%s).\n"
1142 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001143 port->slave->dev->master->name, port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 __release_rx_machine_lock(port);
1145 return;
1146 }
1147 __update_selected(lacpdu, port);
1148 __update_ntt(lacpdu, port);
1149 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1151 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1152 // verify that if the aggregator is enabled, the port is enabled too.
1153 //(because if the link goes down for a short time, the 802.3ad will not
1154 // catch it, and the port will continue to be disabled)
1155 if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) {
1156 __enable_port(port);
1157 }
1158 break;
1159 default: //to silence the compiler
1160 break;
1161 }
1162 }
1163 __release_rx_machine_lock(port);
1164}
1165
1166/**
1167 * ad_tx_machine - handle a port's tx state machine
1168 * @port: the port we're looking at
1169 *
1170 */
1171static void ad_tx_machine(struct port *port)
1172{
1173 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1174 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1175 // check if there is something to send
1176 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1177 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001180 pr_debug("Sent LACPDU on port %d\n",
1181 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001182
1183 /* mark ntt as false, so it will not be sent again until
1184 demanded */
1185 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 }
1188 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
Bandan Das128ea6c2010-10-16 20:19:58 +00001189 port->sm_tx_timer_counter =
1190 ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192}
1193
1194/**
1195 * ad_periodic_machine - handle a port's periodic state machine
1196 * @port: the port we're looking at
1197 *
1198 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1199 */
1200static void ad_periodic_machine(struct port *port)
1201{
1202 periodic_states_t last_state;
1203
1204 // keep current state machine state to compare later if it was changed
1205 last_state = port->sm_periodic_state;
1206
1207 // check if port was reinitialized
1208 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001209 (!(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 -07001210 ) {
1211 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1212 }
1213 // check if state machine should change state
1214 else if (port->sm_periodic_timer_counter) {
1215 // check if periodic state machine expired
1216 if (!(--port->sm_periodic_timer_counter)) {
1217 // if expired then do tx
1218 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1219 } else {
1220 // If not expired, check if there is some new timeout parameter from the partner state
1221 switch (port->sm_periodic_state) {
1222 case AD_FAST_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001223 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1225 }
1226 break;
1227 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001228 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 // stop current timer
1230 port->sm_periodic_timer_counter = 0;
1231 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1232 }
1233 break;
1234 default: //to silence the compiler
1235 break;
1236 }
1237 }
1238 } else {
1239 switch (port->sm_periodic_state) {
1240 case AD_NO_PERIODIC:
1241 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1242 break;
1243 case AD_PERIODIC_TX:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001244 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
1246 } else {
1247 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1248 }
1249 break;
1250 default: //to silence the compiler
1251 break;
1252 }
1253 }
1254
1255 // check if the state machine was changed
1256 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001257 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1258 port->actor_port_number, last_state,
1259 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 switch (port->sm_periodic_state) {
1261 case AD_NO_PERIODIC:
1262 port->sm_periodic_timer_counter = 0; // zero timer
1263 break;
1264 case AD_FAST_PERIODIC:
1265 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
1266 break;
1267 case AD_SLOW_PERIODIC:
1268 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
1269 break;
1270 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001271 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 default: //to silence the compiler
1274 break;
1275 }
1276 }
1277}
1278
1279/**
1280 * ad_port_selection_logic - select aggregation groups
1281 * @port: the port we're looking at
1282 *
1283 * Select aggregation groups, and assign each port for it's aggregetor. The
1284 * selection logic is called in the inititalization (after all the handshkes),
1285 * and after every lacpdu receive (if selected is off).
1286 */
1287static void ad_port_selection_logic(struct port *port)
1288{
1289 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1290 struct port *last_port = NULL, *curr_port;
1291 int found = 0;
1292
1293 // if the port is already Selected, do nothing
1294 if (port->sm_vars & AD_PORT_SELECTED) {
1295 return;
1296 }
1297
1298 // if the port is connected to other aggregator, detach it
1299 if (port->aggregator) {
1300 // detach the port from its former aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001301 temp_aggregator = port->aggregator;
1302 for (curr_port = temp_aggregator->lag_ports; curr_port;
1303 last_port = curr_port,
1304 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (curr_port == port) {
1306 temp_aggregator->num_of_ports--;
1307 if (!last_port) {// if it is the first port attached to the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001308 temp_aggregator->lag_ports =
1309 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 } else {// not the first port attached to the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001311 last_port->next_port_in_aggregator =
1312 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
1315 // clear the port's relations to this aggregator
1316 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001317 port->next_port_in_aggregator = NULL;
1318 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001320 pr_debug("Port %d left LAG %d\n",
1321 port->actor_port_number,
1322 temp_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 // if the aggregator is empty, clear its parameters, and set it ready to be attached
1324 if (!temp_aggregator->lag_ports) {
1325 ad_clear_agg(temp_aggregator);
1326 }
1327 break;
1328 }
1329 }
1330 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001331 pr_warning("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001332 port->slave->dev->master->name,
1333 port->actor_port_number,
1334 port->slave->dev->name,
1335 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 }
1338 // search on all aggregators for a suitable aggregator for this port
1339 for (aggregator = __get_first_agg(port); aggregator;
1340 aggregator = __get_next_agg(aggregator)) {
1341
1342 // keep a free aggregator for later use(if needed)
1343 if (!aggregator->lag_ports) {
1344 if (!free_aggregator) {
Bandan Das128ea6c2010-10-16 20:19:58 +00001345 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 continue;
1348 }
1349 // check if current aggregator suits us
1350 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001351 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1352 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1353 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 ) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001355 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 !aggregator->is_individual) // but is not individual OR
1357 )
1358 ) {
1359 // attach to the founded aggregator
1360 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001361 port->actor_port_aggregator_identifier =
1362 port->aggregator->aggregator_identifier;
1363 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001365 aggregator->lag_ports = port;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001366 pr_debug("Port %d joined LAG %d(existing LAG)\n",
1367 port->actor_port_number,
1368 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 // mark this port as selected
1371 port->sm_vars |= AD_PORT_SELECTED;
1372 found = 1;
1373 break;
1374 }
1375 }
1376
1377 // the port couldn't find an aggregator - attach it to a new aggregator
1378 if (!found) {
1379 if (free_aggregator) {
1380 // assign port a new aggregator
1381 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001382 port->actor_port_aggregator_identifier =
1383 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 // update the new aggregator's parameters
1386 // if port was responsed from the end-user
1387 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001388 port->aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 } else {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001390 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1394 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001395 port->aggregator->partner_system =
1396 port->partner_oper.system;
1397 port->aggregator->partner_system_priority =
1398 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001399 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 port->aggregator->receive_state = 1;
1401 port->aggregator->transmit_state = 1;
1402 port->aggregator->lag_ports = port;
1403 port->aggregator->num_of_ports++;
1404
1405 // mark this port as selected
1406 port->sm_vars |= AD_PORT_SELECTED;
1407
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001408 pr_debug("Port %d joined LAG %d(new LAG)\n",
1409 port->actor_port_number,
1410 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001412 pr_err("%s: Port %d (on %s) did not find a suitable aggregator\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001413 port->slave->dev->master->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 port->actor_port_number, port->slave->dev->name);
1415 }
1416 }
1417 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1418 // else set ready=FALSE in all aggregator's ports
1419 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1420
Jay Vosburghfd989c82008-11-04 17:51:16 -08001421 aggregator = __get_first_agg(port);
1422 ad_agg_selection_logic(aggregator);
1423}
1424
1425/*
1426 * Decide if "agg" is a better choice for the new active aggregator that
1427 * the current best, according to the ad_select policy.
1428 */
1429static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1430 struct aggregator *curr)
1431{
1432 /*
1433 * 0. If no best, select current.
1434 *
1435 * 1. If the current agg is not individual, and the best is
1436 * individual, select current.
1437 *
1438 * 2. If current agg is individual and the best is not, keep best.
1439 *
1440 * 3. Therefore, current and best are both individual or both not
1441 * individual, so:
1442 *
1443 * 3a. If current agg partner replied, and best agg partner did not,
1444 * select current.
1445 *
1446 * 3b. If current agg partner did not reply and best agg partner
1447 * did reply, keep best.
1448 *
1449 * 4. Therefore, current and best both have partner replies or
1450 * both do not, so perform selection policy:
1451 *
1452 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1453 * select by bandwidth.
1454 *
1455 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1456 */
1457 if (!best)
1458 return curr;
1459
1460 if (!curr->is_individual && best->is_individual)
1461 return curr;
1462
1463 if (curr->is_individual && !best->is_individual)
1464 return best;
1465
1466 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1467 return curr;
1468
1469 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1470 return best;
1471
1472 switch (__get_agg_selection_mode(curr->lag_ports)) {
1473 case BOND_AD_COUNT:
1474 if (curr->num_of_ports > best->num_of_ports)
1475 return curr;
1476
1477 if (curr->num_of_ports < best->num_of_ports)
1478 return best;
1479
1480 /*FALLTHROUGH*/
1481 case BOND_AD_STABLE:
1482 case BOND_AD_BANDWIDTH:
1483 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1484 return curr;
1485
1486 break;
1487
1488 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001489 pr_warning("%s: Impossible agg select mode %d\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001490 curr->slave->dev->master->name,
1491 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001494
1495 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001498static int agg_device_up(const struct aggregator *agg)
1499{
1500 return (netif_running(agg->slave->dev) &&
1501 netif_carrier_ok(agg->slave->dev));
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
1505 * ad_agg_selection_logic - select an aggregation group for a team
1506 * @aggregator: the aggregator we're looking at
1507 *
1508 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001509 *
1510 * The logic of this function is to select the aggregator according to
1511 * the ad_select policy:
1512 *
1513 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1514 * it, and to reselect the active aggregator only if the previous
1515 * aggregator has no more ports related to it.
1516 *
1517 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1518 * bandwidth, and reselect whenever a link state change takes place or the
1519 * set of slaves in the bond changes.
1520 *
1521 * BOND_AD_COUNT: select the aggregator with largest number of ports
1522 * (slaves), and reselect whenever a link state change takes place or the
1523 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 *
1525 * FIXME: this function MUST be called with the first agg in the bond, or
1526 * __get_active_agg() won't work correctly. This function should be better
1527 * called with the bond itself, and retrieve the first agg from it.
1528 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001529static void ad_agg_selection_logic(struct aggregator *agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001531 struct aggregator *best, *active, *origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Jay Vosburghfd989c82008-11-04 17:51:16 -08001534 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001535 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001536 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 do {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001539 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001541 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001542 best = ad_agg_selection_test(best, agg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Jay Vosburghfd989c82008-11-04 17:51:16 -08001544 } while ((agg = __get_next_agg(agg)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jay Vosburghfd989c82008-11-04 17:51:16 -08001546 if (best &&
1547 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1548 /*
1549 * For the STABLE policy, don't replace the old active
1550 * aggregator if it's still active (it has an answering
1551 * partner) or if both the best and active don't have an
1552 * answering partner.
1553 */
1554 if (active && active->lag_ports &&
1555 active->lag_ports->is_enabled &&
1556 (__agg_has_partner(active) ||
1557 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1558 if (!(!active->actor_oper_aggregator_key &&
1559 best->actor_oper_aggregator_key)) {
1560 best = NULL;
1561 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 }
1564 }
1565
Jay Vosburghfd989c82008-11-04 17:51:16 -08001566 if (best && (best == active)) {
1567 best = NULL;
1568 active->is_active = 1;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Jay Vosburghfd989c82008-11-04 17:51:16 -08001571 // if there is new best aggregator, activate it
1572 if (best) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001573 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001574 best->aggregator_identifier, best->num_of_ports,
1575 best->actor_oper_aggregator_key,
1576 best->partner_oper_aggregator_key,
1577 best->is_individual, best->is_active);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001578 pr_debug("best ports %p slave %p %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001579 best->lag_ports, best->slave,
1580 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001581
1582 for (agg = __get_first_agg(best->lag_ports); agg;
1583 agg = __get_next_agg(agg)) {
1584
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001585 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001586 agg->aggregator_identifier, agg->num_of_ports,
1587 agg->actor_oper_aggregator_key,
1588 agg->partner_oper_aggregator_key,
1589 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
1592 // check if any partner replys
Jay Vosburghfd989c82008-11-04 17:51:16 -08001593 if (best->is_individual) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001594 pr_warning("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
Dan Carpenterc99a3d22009-12-23 03:27:10 +00001595 best->slave ? best->slave->dev->master->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597
Jay Vosburghfd989c82008-11-04 17:51:16 -08001598 best->is_active = 1;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001599 pr_debug("LAG %d chosen as the active LAG\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001600 best->aggregator_identifier);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001601 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001602 best->aggregator_identifier, best->num_of_ports,
1603 best->actor_oper_aggregator_key,
1604 best->partner_oper_aggregator_key,
1605 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 // disable the ports that were related to the former active_aggregator
Jay Vosburghfd989c82008-11-04 17:51:16 -08001608 if (active) {
1609 for (port = active->lag_ports; port;
1610 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 __disable_port(port);
1612 }
1613 }
1614 }
1615
Jay Vosburghfd989c82008-11-04 17:51:16 -08001616 /*
1617 * if the selected aggregator is of join individuals
1618 * (partner_system is NULL), enable their ports
1619 */
1620 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Jay Vosburghfd989c82008-11-04 17:51:16 -08001622 if (active) {
1623 if (!__agg_has_partner(active)) {
1624 for (port = active->lag_ports; port;
1625 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 __enable_port(port);
1627 }
1628 }
1629 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001630
1631 if (origin->slave) {
1632 struct bonding *bond;
1633
1634 bond = bond_get_bond_by_slave(origin->slave);
1635 if (bond)
1636 bond_3ad_set_carrier(bond);
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640/**
1641 * ad_clear_agg - clear a given aggregator's parameters
1642 * @aggregator: the aggregator we're looking at
1643 *
1644 */
1645static void ad_clear_agg(struct aggregator *aggregator)
1646{
1647 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001648 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 aggregator->actor_admin_aggregator_key = 0;
1650 aggregator->actor_oper_aggregator_key = 0;
1651 aggregator->partner_system = null_mac_addr;
1652 aggregator->partner_system_priority = 0;
1653 aggregator->partner_oper_aggregator_key = 0;
1654 aggregator->receive_state = 0;
1655 aggregator->transmit_state = 0;
1656 aggregator->lag_ports = NULL;
1657 aggregator->is_active = 0;
1658 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001659 pr_debug("LAG %d was cleared\n",
1660 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662}
1663
1664/**
1665 * ad_initialize_agg - initialize a given aggregator's parameters
1666 * @aggregator: the aggregator we're looking at
1667 *
1668 */
1669static void ad_initialize_agg(struct aggregator *aggregator)
1670{
1671 if (aggregator) {
1672 ad_clear_agg(aggregator);
1673
1674 aggregator->aggregator_mac_address = null_mac_addr;
1675 aggregator->aggregator_identifier = 0;
1676 aggregator->slave = NULL;
1677 }
1678}
1679
1680/**
1681 * ad_initialize_port - initialize a given port's parameters
1682 * @aggregator: the aggregator we're looking at
1683 * @lacp_fast: boolean. whether fast periodic should be used
1684 *
1685 */
1686static void ad_initialize_port(struct port *port, int lacp_fast)
1687{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001688 static const struct port_params tmpl = {
1689 .system_priority = 0xffff,
1690 .key = 1,
1691 .port_number = 1,
1692 .port_priority = 0xff,
1693 .port_state = 1,
1694 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001695 static const struct lacpdu lacpdu = {
1696 .subtype = 0x01,
1697 .version_number = 0x01,
1698 .tlv_type_actor_info = 0x01,
1699 .actor_information_length = 0x14,
1700 .tlv_type_partner_info = 0x02,
1701 .partner_information_length = 0x14,
1702 .tlv_type_collector_info = 0x03,
1703 .collector_information_length = 0x10,
1704 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1705 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (port) {
1708 port->actor_port_number = 1;
1709 port->actor_port_priority = 0xff;
1710 port->actor_system = null_mac_addr;
1711 port->actor_system_priority = 0xffff;
1712 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001713 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 port->actor_admin_port_key = 1;
1715 port->actor_oper_port_key = 1;
1716 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1717 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1718
1719 if (lacp_fast) {
1720 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1721 }
1722
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001723 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1724 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1725
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001726 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 // ****** private parameters ******
1728 port->sm_vars = 0x3;
1729 port->sm_rx_state = 0;
1730 port->sm_rx_timer_counter = 0;
1731 port->sm_periodic_state = 0;
1732 port->sm_periodic_timer_counter = 0;
1733 port->sm_mux_state = 0;
1734 port->sm_mux_timer_counter = 0;
1735 port->sm_tx_state = 0;
1736 port->sm_tx_timer_counter = 0;
1737 port->slave = NULL;
1738 port->aggregator = NULL;
1739 port->next_port_in_aggregator = NULL;
1740 port->transaction_id = 0;
1741
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001742 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744}
1745
1746/**
1747 * ad_enable_collecting_distributing - enable a port's transmit/receive
1748 * @port: the port we're looking at
1749 *
1750 * Enable @port if it's in an active aggregator
1751 */
1752static void ad_enable_collecting_distributing(struct port *port)
1753{
1754 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001755 pr_debug("Enabling port %d(LAG %d)\n",
1756 port->actor_port_number,
1757 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 __enable_port(port);
1759 }
1760}
1761
1762/**
1763 * ad_disable_collecting_distributing - disable a port's transmit/receive
1764 * @port: the port we're looking at
1765 *
1766 */
1767static void ad_disable_collecting_distributing(struct port *port)
1768{
1769 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001770 pr_debug("Disabling port %d(LAG %d)\n",
1771 port->actor_port_number,
1772 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 __disable_port(port);
1774 }
1775}
1776
1777#if 0
1778/**
1779 * ad_marker_info_send - send a marker information frame
1780 * @port: the port we're looking at
1781 *
1782 * This function does nothing since we decided not to implement send and handle
1783 * response for marker PDU's, in this stage, but only to respond to marker
1784 * information.
1785 */
1786static void ad_marker_info_send(struct port *port)
1787{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001788 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 u16 index;
1790
1791 // fill the marker PDU with the appropriate values
1792 marker.subtype = 0x02;
1793 marker.version_number = 0x01;
1794 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1795 marker.marker_length = 0x16;
1796 // convert requester_port to Big Endian
1797 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1798 marker.requester_system = port->actor_system;
1799 // convert requester_port(u32) to Big Endian
Bandan Das128ea6c2010-10-16 20:19:58 +00001800 marker.requester_transaction_id =
1801 (((++port->transaction_id & 0xFF) << 24)
1802 | ((port->transaction_id & 0xFF00) << 8)
1803 | ((port->transaction_id & 0xFF0000) >> 8)
1804 | ((port->transaction_id & 0xFF000000) >> 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 marker.pad = 0;
1806 marker.tlv_type_terminator = 0x00;
1807 marker.terminator_length = 0x00;
Bandan Das128ea6c2010-10-16 20:19:58 +00001808 for (index = 0; index < 90; index++)
1809 marker.reserved_90[index] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 // send the marker information
1812 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001813 pr_debug("Sent Marker Information on port %d\n",
1814 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816}
1817#endif
1818
1819/**
1820 * ad_marker_info_received - handle receive of a Marker information frame
1821 * @marker_info: Marker info received
1822 * @port: the port we're looking at
1823 *
1824 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001825static void ad_marker_info_received(struct bond_marker *marker_info,
1826 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001828 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 // copy the received marker data to the response marker
1831 //marker = *marker_info;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001832 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 // change the marker subtype to marker response
Bandan Das128ea6c2010-10-16 20:19:58 +00001834 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 // send the marker response
1836
1837 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001838 pr_debug("Sent Marker Response on port %d\n",
1839 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841}
1842
1843/**
1844 * ad_marker_response_received - handle receive of a marker response frame
1845 * @marker: marker PDU received
1846 * @port: the port we're looking at
1847 *
1848 * This function does nothing since we decided not to implement send and handle
1849 * response for marker PDU's, in this stage, but only to respond to marker
1850 * information.
1851 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001852static void ad_marker_response_received(struct bond_marker *marker,
1853 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Bandan Das128ea6c2010-10-16 20:19:58 +00001855 marker = NULL; /* just to satisfy the compiler */
1856 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1858}
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860//////////////////////////////////////////////////////////////////////////////////////
1861// ================= AD exported functions to the main bonding code ==================
1862//////////////////////////////////////////////////////////////////////////////////////
1863
1864// Check aggregators status in team every T seconds
1865#define AD_AGGREGATOR_SELECTION_TIMER 8
1866
Jay Vosburghfd989c82008-11-04 17:51:16 -08001867/*
1868 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1869 *
1870 * Set the aggregation selection timer, to initiate an agg selection in
1871 * the very near future. Called during first initialization, and during
1872 * any down to up transitions of the bond.
1873 */
1874void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1875{
1876 BOND_AD_INFO(bond).agg_select_timer = timeout;
1877 BOND_AD_INFO(bond).agg_select_mode = bond->params.ad_select;
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static u16 aggregator_identifier;
1881
1882/**
1883 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1884 * @bond: bonding struct to work on
1885 * @tick_resolution: tick duration (millisecond resolution)
1886 * @lacp_fast: boolean. whether fast periodic should be used
1887 *
1888 * Can be called only after the mac address of the bond is set.
1889 */
1890void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 // check that the bond is not initialized yet
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001893 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1894 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 aggregator_identifier = 0;
1897
1898 BOND_AD_INFO(bond).lacp_fast = lacp_fast;
1899 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1900 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1901
1902 // initialize how many times this module is called in one second(should be about every 100ms)
1903 ad_ticks_per_sec = tick_resolution;
1904
Jay Vosburghfd989c82008-11-04 17:51:16 -08001905 bond_3ad_initiate_agg_selection(bond,
1906 AD_AGGREGATOR_SELECTION_TIMER *
1907 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909}
1910
1911/**
1912 * bond_3ad_bind_slave - initialize a slave's port
1913 * @slave: slave struct to work on
1914 *
1915 * Returns: 0 on success
1916 * < 0 on error
1917 */
1918int bond_3ad_bind_slave(struct slave *slave)
1919{
1920 struct bonding *bond = bond_get_bond_by_slave(slave);
1921 struct port *port;
1922 struct aggregator *aggregator;
1923
1924 if (bond == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001925 pr_err("%s: The slave %s is not attached to its bond\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001926 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return -1;
1928 }
1929
1930 //check that the slave has not been intialized yet.
1931 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1932
1933 // port initialization
1934 port = &(SLAVE_AD_INFO(slave).port);
1935
1936 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
1937
1938 port->slave = slave;
1939 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1940 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1941 // ------------------------------------------------------------
1942 // Port key : | User key | Speed |Duplex|
1943 // ------------------------------------------------------------
1944 // 16 6 1 0
1945 port->actor_admin_port_key = 0; // initialize this parameter
1946 port->actor_admin_port_key |= __get_duplex(port);
1947 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1948 port->actor_oper_port_key = port->actor_admin_port_key;
1949 // if the port is not full duplex, then the port should be not lacp Enabled
1950 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
1951 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1952 }
1953 // actor system is the bond's system
1954 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1955 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1956 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1957 port->aggregator = NULL;
1958 port->next_port_in_aggregator = NULL;
1959
1960 __disable_port(port);
1961 __initialize_port_locks(port);
1962
1963
1964 // aggregator initialization
1965 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1966
1967 ad_initialize_agg(aggregator);
1968
1969 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1970 aggregator->aggregator_identifier = (++aggregator_identifier);
1971 aggregator->slave = slave;
1972 aggregator->is_active = 0;
1973 aggregator->num_of_ports = 0;
1974 }
1975
1976 return 0;
1977}
1978
1979/**
1980 * bond_3ad_unbind_slave - deinitialize a slave's port
1981 * @slave: slave struct to work on
1982 *
1983 * Search for the aggregator that is related to this port, remove the
1984 * aggregator and assign another aggregator for other port related to it
1985 * (if any), and remove the port.
1986 */
1987void bond_3ad_unbind_slave(struct slave *slave)
1988{
1989 struct port *port, *prev_port, *temp_port;
1990 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1991 int select_new_active_agg = 0;
Jasper Spaansa361c832009-10-23 04:09:24 +00001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 // find the aggregator related to this slave
1994 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1995
1996 // find the port related to this slave
1997 port = &(SLAVE_AD_INFO(slave).port);
1998
1999 // if slave is null, the whole port is not initialized
2000 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002001 pr_warning("Warning: %s: Trying to unbind an uninitialized port on %s\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002002 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return;
2004 }
2005
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002006 pr_debug("Unbinding Link Aggregation Group %d\n",
2007 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /* Tell the partner that this port is not suitable for aggregation */
2010 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2011 __update_lacpdu_from_port(port);
2012 ad_lacpdu_send(port);
2013
2014 // check if this aggregator is occupied
2015 if (aggregator->lag_ports) {
2016 // check if there are other ports related to this aggregator except
2017 // the port related to this slave(thats ensure us that there is a
2018 // reason to search for new aggregator, and that we will find one
2019 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
2020 // find new aggregator for the related port(s)
2021 new_aggregator = __get_first_agg(port);
2022 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002023 // if the new aggregator is empty, or it is connected to our port only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
2025 break;
2026 }
2027 }
2028 // if new aggregator found, copy the aggregator's parameters
2029 // and connect the related lag_ports to the new aggregator
2030 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002031 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n",
2032 aggregator->aggregator_identifier,
2033 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002036 pr_info("%s: Removing an active aggregator\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002037 aggregator->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 // select new active aggregator
2039 select_new_active_agg = 1;
2040 }
2041
2042 new_aggregator->is_individual = aggregator->is_individual;
2043 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2044 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2045 new_aggregator->partner_system = aggregator->partner_system;
2046 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2047 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2048 new_aggregator->receive_state = aggregator->receive_state;
2049 new_aggregator->transmit_state = aggregator->transmit_state;
2050 new_aggregator->lag_ports = aggregator->lag_ports;
2051 new_aggregator->is_active = aggregator->is_active;
2052 new_aggregator->num_of_ports = aggregator->num_of_ports;
2053
2054 // update the information that is written on the ports about the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00002055 for (temp_port = aggregator->lag_ports; temp_port;
2056 temp_port = temp_port->next_port_in_aggregator) {
2057 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2059 }
2060
2061 // clear the aggregator
2062 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (select_new_active_agg) {
2065 ad_agg_selection_logic(__get_first_agg(port));
2066 }
2067 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002068 pr_warning("%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002069 slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 } else { // in case that the only port related to this aggregator is the one we want to remove
2072 select_new_active_agg = aggregator->is_active;
2073 // clear the aggregator
2074 ad_clear_agg(aggregator);
2075 if (select_new_active_agg) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002076 pr_info("%s: Removing an active aggregator\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002077 slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 // select new active aggregator
2079 ad_agg_selection_logic(__get_first_agg(port));
2080 }
2081 }
2082 }
2083
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002084 pr_debug("Unbinding port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 // find the aggregator that this port is connected to
2086 temp_aggregator = __get_first_agg(port);
2087 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2088 prev_port = NULL;
2089 // search the port in the aggregator's related ports
Bandan Das128ea6c2010-10-16 20:19:58 +00002090 for (temp_port = temp_aggregator->lag_ports; temp_port;
2091 prev_port = temp_port,
2092 temp_port = temp_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
2094 if (prev_port) {
2095 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2096 } else {
2097 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2098 }
2099 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00002100 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 select_new_active_agg = temp_aggregator->is_active;
2102 // clear the aggregator
2103 ad_clear_agg(temp_aggregator);
2104 if (select_new_active_agg) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002105 pr_info("%s: Removing an active aggregator\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002106 slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 // select new active aggregator
2108 ad_agg_selection_logic(__get_first_agg(port));
2109 }
2110 }
2111 break;
2112 }
2113 }
2114 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002115 port->slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/**
2119 * bond_3ad_state_machine_handler - handle state machines timeout
2120 * @bond: bonding struct to work on
2121 *
2122 * The state machine handling concept in this module is to check every tick
2123 * which state machine should operate any function. The execution order is
2124 * round robin, so when we have an interaction between state machines, the
2125 * reply of one to each other might be delayed until next tick.
2126 *
2127 * This function also complete the initialization when the agg_select_timer
2128 * times out, and it selects an aggregator for the ports that are yet not
2129 * related to any aggregator, and selects the active aggregator for a bond.
2130 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002131void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002133 struct bonding *bond = container_of(work, struct bonding,
2134 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct port *port;
2136 struct aggregator *aggregator;
2137
2138 read_lock(&bond->lock);
2139
2140 if (bond->kill_timers) {
2141 goto out;
2142 }
2143
2144 //check if there are any slaves
2145 if (bond->slave_cnt == 0) {
2146 goto re_arm;
2147 }
2148
2149 // check if agg_select_timer timer after initialize is timed out
2150 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2151 // select the active aggregator for the bond
2152 if ((port = __get_first_port(bond))) {
2153 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002154 pr_warning("%s: Warning: bond's first port is uninitialized\n",
2155 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto re_arm;
2157 }
2158
2159 aggregator = __get_first_agg(port);
2160 ad_agg_selection_logic(aggregator);
2161 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002162 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
2165 // for each port run the state machines
2166 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2167 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002168 pr_warning("%s: Warning: Found an uninitialized port\n",
2169 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto re_arm;
2171 }
2172
2173 ad_rx_machine(NULL, port);
2174 ad_periodic_machine(port);
2175 ad_port_selection_logic(port);
2176 ad_mux_machine(port);
2177 ad_tx_machine(port);
2178
2179 // turn off the BEGIN bit, since we already handled it
2180 if (port->sm_vars & AD_PORT_BEGIN) {
2181 port->sm_vars &= ~AD_PORT_BEGIN;
2182 }
2183 }
2184
2185re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002186 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187out:
2188 read_unlock(&bond->lock);
2189}
2190
2191/**
2192 * bond_3ad_rx_indication - handle a received frame
2193 * @lacpdu: received lacpdu
2194 * @slave: slave struct to work on
2195 * @length: length of the data received
2196 *
2197 * It is assumed that frames that were sent on this NIC don't returned as new
2198 * received frames (loopback). Since only the payload is given to this
2199 * function, it check for loopback.
2200 */
2201static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
2202{
2203 struct port *port;
2204
2205 if (length >= sizeof(struct lacpdu)) {
2206
2207 port = &(SLAVE_AD_INFO(slave).port);
2208
2209 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002210 pr_warning("%s: Warning: port of slave %s is uninitialized\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002211 slave->dev->name, slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return;
2213 }
2214
2215 switch (lacpdu->subtype) {
2216 case AD_TYPE_LACPDU:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002217 pr_debug("Received LACPDU on port %d\n",
2218 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 ad_rx_machine(lacpdu, port);
2220 break;
2221
2222 case AD_TYPE_MARKER:
2223 // No need to convert fields to Little Endian since we don't use the marker's fields.
2224
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002225 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 case AD_MARKER_INFORMATION_SUBTYPE:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002227 pr_debug("Received Marker Information on port %d\n",
2228 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002229 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 break;
2231
2232 case AD_MARKER_RESPONSE_SUBTYPE:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002233 pr_debug("Received Marker Response on port %d\n",
2234 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002235 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 break;
2237
2238 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002239 pr_debug("Received an unknown Marker subtype on slot %d\n",
2240 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 }
2243 }
2244}
2245
2246/**
2247 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2248 * @slave: slave struct to work on
2249 *
2250 * Handle reselection of aggregator (if needed) for this port.
2251 */
2252void bond_3ad_adapter_speed_changed(struct slave *slave)
2253{
2254 struct port *port;
2255
2256 port = &(SLAVE_AD_INFO(slave).port);
2257
2258 // if slave is null, the whole port is not initialized
2259 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002260 pr_warning("Warning: %s: speed changed for uninitialized port on %s\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002261 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return;
2263 }
2264
2265 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002266 port->actor_oper_port_key = port->actor_admin_port_key |=
2267 (__get_link_speed(port) << 1);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002268 pr_debug("Port %d changed speed\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 // there is no need to reselect a new aggregator, just signal the
2270 // state machines to reinitialize
2271 port->sm_vars |= AD_PORT_BEGIN;
2272}
2273
2274/**
2275 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2276 * @slave: slave struct to work on
2277 *
2278 * Handle reselection of aggregator (if needed) for this port.
2279 */
2280void bond_3ad_adapter_duplex_changed(struct slave *slave)
2281{
2282 struct port *port;
2283
Bandan Das128ea6c2010-10-16 20:19:58 +00002284 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 // if slave is null, the whole port is not initialized
2287 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002288 pr_warning("%s: Warning: duplex changed for uninitialized port on %s\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002289 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return;
2291 }
2292
2293 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002294 port->actor_oper_port_key = port->actor_admin_port_key |=
2295 __get_duplex(port);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002296 pr_debug("Port %d changed duplex\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 // there is no need to reselect a new aggregator, just signal the
2298 // state machines to reinitialize
2299 port->sm_vars |= AD_PORT_BEGIN;
2300}
2301
2302/**
2303 * bond_3ad_handle_link_change - handle a slave's link status change indication
2304 * @slave: slave struct to work on
2305 * @status: whether the link is now up or down
2306 *
2307 * Handle reselection of aggregator (if needed) for this port.
2308 */
2309void bond_3ad_handle_link_change(struct slave *slave, char link)
2310{
2311 struct port *port;
2312
2313 port = &(SLAVE_AD_INFO(slave).port);
2314
2315 // if slave is null, the whole port is not initialized
2316 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002317 pr_warning("Warning: %s: link status changed for uninitialized port on %s\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00002318 slave->dev->master->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return;
2320 }
2321
2322 // 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)
2323 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2324 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002325 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002327 port->actor_oper_port_key = port->actor_admin_port_key |=
2328 __get_duplex(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002330 port->actor_oper_port_key = port->actor_admin_port_key |=
2331 (__get_link_speed(port) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 } else {
2333 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002334 port->is_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002336 port->actor_oper_port_key = (port->actor_admin_port_key &=
2337 ~AD_SPEED_KEY_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2340 // there is no need to reselect a new aggregator, just signal the
2341 // state machines to reinitialize
2342 port->sm_vars |= AD_PORT_BEGIN;
2343}
2344
Jay Vosburghff59c452006-03-27 13:27:43 -08002345/*
Jasper Spaansa361c832009-10-23 04:09:24 +00002346 * set link state for bonding master: if we have an active
Jay Vosburghff59c452006-03-27 13:27:43 -08002347 * aggregator, we're up, if not, we're down. Presumes that we cannot
2348 * have an active aggregator if there are no slaves with link up.
2349 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002350 * This behavior complies with IEEE 802.3 section 43.3.9.
2351 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002352 * Called by bond_set_carrier(). Return zero if carrier state does not
2353 * change, nonzero if it does.
2354 */
2355int bond_3ad_set_carrier(struct bonding *bond)
2356{
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002357 if (__get_active_agg(&(SLAVE_AD_INFO(bond->first_slave).aggregator))) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002358 if (!netif_carrier_ok(bond->dev)) {
2359 netif_carrier_on(bond->dev);
2360 return 1;
2361 }
2362 return 0;
2363 }
2364
2365 if (netif_carrier_ok(bond->dev)) {
2366 netif_carrier_off(bond->dev);
2367 return 1;
2368 }
2369 return 0;
2370}
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372/**
2373 * bond_3ad_get_active_agg_info - get information of the active aggregator
2374 * @bond: bonding struct to work on
2375 * @ad_info: ad_info struct to fill with the bond's info
2376 *
2377 * Returns: 0 on success
2378 * < 0 on error
2379 */
2380int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2381{
2382 struct aggregator *aggregator = NULL;
2383 struct port *port;
2384
2385 for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
2386 if (port->aggregator && port->aggregator->is_active) {
2387 aggregator = port->aggregator;
2388 break;
2389 }
2390 }
2391
2392 if (aggregator) {
2393 ad_info->aggregator_id = aggregator->aggregator_identifier;
2394 ad_info->ports = aggregator->num_of_ports;
2395 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2396 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2397 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2398 return 0;
2399 }
2400
2401 return -1;
2402}
2403
2404int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2405{
2406 struct slave *slave, *start_at;
Wang Chen454d7c92008-11-12 23:37:49 -08002407 struct bonding *bond = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int slave_agg_no;
2409 int slaves_in_agg;
2410 int agg_id;
2411 int i;
2412 struct ad_info ad_info;
2413 int res = 1;
2414
2415 /* make sure that the slaves list will
2416 * not change during tx
2417 */
2418 read_lock(&bond->lock);
2419
2420 if (!BOND_IS_OK(bond)) {
2421 goto out;
2422 }
2423
2424 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002425 pr_debug("%s: Error: bond_3ad_get_active_agg_info failed\n",
2426 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 goto out;
2428 }
2429
2430 slaves_in_agg = ad_info.ports;
2431 agg_id = ad_info.aggregator_id;
2432
2433 if (slaves_in_agg == 0) {
2434 /*the aggregator is empty*/
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002435 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto out;
2437 }
2438
Jasper Spaansa361c832009-10-23 04:09:24 +00002439 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 bond_for_each_slave(bond, slave, i) {
2442 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2443
2444 if (agg && (agg->aggregator_identifier == agg_id)) {
2445 slave_agg_no--;
2446 if (slave_agg_no < 0) {
2447 break;
2448 }
2449 }
2450 }
2451
2452 if (slave_agg_no >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002453 pr_err("%s: Error: Couldn't find a slave to tx on for aggregator ID %d\n",
2454 dev->name, agg_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto out;
2456 }
2457
2458 start_at = slave;
2459
2460 bond_for_each_slave_from(bond, slave, i, start_at) {
2461 int slave_agg_id = 0;
2462 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2463
2464 if (agg) {
2465 slave_agg_id = agg->aggregator_identifier;
2466 }
2467
2468 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2469 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2470 break;
2471 }
2472 }
2473
2474out:
2475 if (res) {
2476 /* no suitable interface, frame not sent */
2477 dev_kfree_skb(skb);
2478 }
2479 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07002480 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002483int 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 -07002484{
Wang Chen454d7c92008-11-12 23:37:49 -08002485 struct bonding *bond = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 struct slave *slave = NULL;
2487 int ret = NET_RX_DROP;
2488
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002489 if (!(dev->flags & IFF_MASTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Andy Gospodarekab128112010-09-10 11:43:20 +00002492 if (!pskb_may_pull(skb, sizeof(struct lacpdu)))
2493 goto out;
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 read_lock(&bond->lock);
Wang Chen454d7c92008-11-12 23:37:49 -08002496 slave = bond_get_slave_by_dev((struct bonding *)netdev_priv(dev),
2497 orig_dev);
David S. Millerf2ccd8f2005-08-09 19:34:12 -07002498 if (!slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
2502
2503 ret = NET_RX_SUCCESS;
2504
2505out_unlock:
2506 read_unlock(&bond->lock);
2507out:
2508 dev_kfree_skb(skb);
2509
2510 return ret;
2511}