blob: c1535f8762d159d37805d0db6fd7dba86d49c0c8 [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{
Bandan Das7bfc4752010-10-16 20:19:59 +0000132 if (port->slave == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return bond_get_bond_by_slave(port->slave);
136}
137
138/**
139 * __get_first_port - get the first port in the bond
140 * @bond: the bond we're looking at
141 *
142 * Return the port of the first slave in @bond, or %NULL if it can't be found.
143 */
144static inline struct port *__get_first_port(struct bonding *bond)
145{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200146 struct slave *first_slave = bond_first_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200148 return first_slave ? &(SLAVE_AD_INFO(first_slave).port) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * __get_first_agg - get the first aggregator in the bond
153 * @bond: the bond we're looking at
154 *
155 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
156 * found.
157 */
158static inline struct aggregator *__get_first_agg(struct port *port)
159{
160 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200161 struct slave *first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 // If there's no bond for this port, or bond has no slaves
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200164 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return NULL;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200166 first_slave = bond_first_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200168 return first_slave ? &(SLAVE_AD_INFO(first_slave).aggregator) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/**
172 * __get_next_agg - get the next aggregator in the bond
173 * @aggregator: the aggregator we're looking at
174 *
175 * Return the aggregator of the slave that is next in line of @aggregator's
176 * slave in the bond, or %NULL if it can't be found.
177 */
178static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
179{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200180 struct slave *slave = aggregator->slave, *slave_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct bonding *bond = bond_get_bond_by_slave(slave);
182
183 // If there's no bond for this aggregator, or this is the last slave
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200184 if (bond == NULL)
185 return NULL;
186 slave_next = bond_next_slave(bond, slave);
187 if (!slave_next || bond_is_first_slave(bond, slave_next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200190 return &(SLAVE_AD_INFO(slave_next).aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jay Vosburghfd989c82008-11-04 17:51:16 -0800193/*
194 * __agg_has_partner
195 *
196 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
197 * address for the partner). Return 0 if not.
198 */
199static inline int __agg_has_partner(struct aggregator *agg)
200{
201 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/**
205 * __disable_port - disable the port's slave
206 * @port: the port we're looking at
207 *
208 */
209static inline void __disable_port(struct port *port)
210{
211 bond_set_slave_inactive_flags(port->slave);
212}
213
214/**
215 * __enable_port - enable the port's slave, if it's up
216 * @port: the port we're looking at
217 *
218 */
219static inline void __enable_port(struct port *port)
220{
221 struct slave *slave = port->slave;
222
Bandan Das7bfc4752010-10-16 20:19:59 +0000223 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 bond_set_slave_active_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/**
228 * __port_is_enabled - check if the port's slave is in active state
229 * @port: the port we're looking at
230 *
231 */
232static inline int __port_is_enabled(struct port *port)
233{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000234 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/**
238 * __get_agg_selection_mode - get the aggregator selection mode
239 * @port: the port we're looking at
240 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800241 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243static inline u32 __get_agg_selection_mode(struct port *port)
244{
245 struct bonding *bond = __get_bond_by_port(port);
246
Bandan Das7bfc4752010-10-16 20:19:59 +0000247 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800248 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000250 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
254 * __check_agg_selection_timer - check if the selection timer has expired
255 * @port: the port we're looking at
256 *
257 */
258static inline int __check_agg_selection_timer(struct port *port)
259{
260 struct bonding *bond = __get_bond_by_port(port);
261
Bandan Das7bfc4752010-10-16 20:19:59 +0000262 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
266}
267
268/**
Nils Carlson9ac35242011-03-03 22:09:12 +0000269 * __get_state_machine_lock - lock the port's state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * @port: the port we're looking at
271 *
272 */
Nils Carlson9ac35242011-03-03 22:09:12 +0000273static inline void __get_state_machine_lock(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Nils Carlson9ac35242011-03-03 22:09:12 +0000275 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
Nils Carlson9ac35242011-03-03 22:09:12 +0000279 * __release_state_machine_lock - unlock the port's state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * @port: the port we're looking at
281 *
282 */
Nils Carlson9ac35242011-03-03 22:09:12 +0000283static inline void __release_state_machine_lock(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Nils Carlson9ac35242011-03-03 22:09:12 +0000285 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/**
289 * __get_link_speed - get a port's speed
290 * @port: the port we're looking at
291 *
292 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
293 * 0,
294 * %AD_LINK_SPEED_BITMASK_10MBPS,
295 * %AD_LINK_SPEED_BITMASK_100MBPS,
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700296 * %AD_LINK_SPEED_BITMASK_1000MBPS,
297 * %AD_LINK_SPEED_BITMASK_10000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299static u16 __get_link_speed(struct port *port)
300{
301 struct slave *slave = port->slave;
302 u16 speed;
303
304 /* this if covers only a special case: when the configuration starts with
305 * link down, it sets the speed to 0.
306 * This is done in spite of the fact that the e100 driver reports 0 to be
307 * compatible with MVT in the future.*/
Bandan Das7bfc4752010-10-16 20:19:59 +0000308 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000309 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000310 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 switch (slave->speed) {
312 case SPEED_10:
313 speed = AD_LINK_SPEED_BITMASK_10MBPS;
314 break;
315
316 case SPEED_100:
317 speed = AD_LINK_SPEED_BITMASK_100MBPS;
318 break;
319
320 case SPEED_1000:
321 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
322 break;
323
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700324 case SPEED_10000:
325 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
326 break;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 default:
329 speed = 0; // unknown speed value from ethtool. shouldn't happen
330 break;
331 }
332 }
333
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800334 pr_debug("Port %d Received link speed %d update from adapter\n",
335 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return speed;
337}
338
339/**
340 * __get_duplex - get a port's duplex
341 * @port: the port we're looking at
342 *
343 * Return @port's duplex in 802.3ad bitmask format. i.e.:
344 * 0x01 if in full duplex
345 * 0x00 otherwise
346 */
347static u8 __get_duplex(struct port *port)
348{
349 struct slave *slave = port->slave;
350
351 u8 retval;
352
353 // handling a special case: when the configuration starts with
354 // link down, it sets the duplex to 0.
Bandan Das7bfc4752010-10-16 20:19:59 +0000355 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000356 retval = 0x0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000357 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 switch (slave->duplex) {
359 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000360 retval = 0x1;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800361 pr_debug("Port %d Received status full duplex update from adapter\n",
362 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 case DUPLEX_HALF:
365 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000366 retval = 0x0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800367 pr_debug("Port %d Received status NOT full duplex update from adapter\n",
368 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370 }
371 }
372 return retval;
373}
374
375/**
Nils Carlson9ac35242011-03-03 22:09:12 +0000376 * __initialize_port_locks - initialize a port's STATE machine spinlock
nikolay@redhat.come0809db2013-02-18 07:59:03 +0000377 * @port: the slave of the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 */
nikolay@redhat.come0809db2013-02-18 07:59:03 +0000380static inline void __initialize_port_locks(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 // make sure it isn't called twice
nikolay@redhat.come0809db2013-02-18 07:59:03 +0000383 spin_lock_init(&(SLAVE_AD_INFO(slave).state_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386//conversions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/**
389 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
390 * @timer_type: which timer to operate
391 * @par: timer parameter. see below
392 *
393 * If @timer_type is %current_while_timer, @par indicates long/short timer.
394 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
395 * %SLOW_PERIODIC_TIME.
396 */
397static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
398{
Bandan Das128ea6c2010-10-16 20:19:58 +0000399 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 switch (timer_type) {
402 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
Bandan Das7bfc4752010-10-16 20:19:59 +0000403 if (par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
Bandan Das7bfc4752010-10-16 20:19:59 +0000405 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
408 case AD_ACTOR_CHURN_TIMER: // for local churn machine
409 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
410 break;
411 case AD_PERIODIC_TIMER: // for periodic machine
412 retval = (par*ad_ticks_per_sec); // long timeout
413 break;
414 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
415 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
416 break;
417 case AD_WAIT_WHILE_TIMER: // for selection machine
418 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
419 break;
420 }
421 return retval;
422}
423
424
425/////////////////////////////////////////////////////////////////////////////////
426// ================= ad_rx_machine helper functions ==================
427/////////////////////////////////////////////////////////////////////////////////
428
429/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000430 * __choose_matched - update a port's matched variable from a received lacpdu
431 * @lacpdu: the lacpdu we've received
432 * @port: the port we're looking at
433 *
434 * Update the value of the matched variable, using parameter values from a
435 * newly received lacpdu. Parameter values for the partner carried in the
436 * received PDU are compared with the corresponding operational parameter
437 * values for the actor. Matched is set to TRUE if all of these parameters
438 * match and the PDU parameter partner_state.aggregation has the same value as
439 * actor_oper_port_state.aggregation and lacp will actively maintain the link
440 * in the aggregation. Matched is also set to TRUE if the value of
441 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
442 * an individual link and lacp will actively maintain the link. Otherwise,
443 * matched is set to FALSE. LACP is considered to be actively maintaining the
444 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
445 * the actor's actor_oper_port_state.lacp_activity and the PDU's
446 * partner_state.lacp_activity variables are TRUE.
447 *
448 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
449 * used here to implement the language from 802.3ad 43.4.9 that requires
450 * recordPDU to "match" the LACPDU parameters to the stored values.
451 */
452static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
453{
454 // check if all parameters are alike
455 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
456 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
457 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
458 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
459 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
460 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
461 // or this is individual link(aggregation == FALSE)
462 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
463 ) {
464 // update the state machine Matched variable
465 port->sm_vars |= AD_PORT_MATCHED;
466 } else {
467 port->sm_vars &= ~AD_PORT_MATCHED;
468 }
469}
470
471/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * __record_pdu - record parameters from a received lacpdu
473 * @lacpdu: the lacpdu we've received
474 * @port: the port we're looking at
475 *
476 * Record the parameter values for the Actor carried in a received lacpdu as
477 * the current partner operational parameter values and sets
478 * actor_oper_port_state.defaulted to FALSE.
479 */
480static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
481{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800483 struct port_params *partner = &port->partner_oper;
484
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000485 __choose_matched(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 // record the new parameter values for the partner operational
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800487 partner->port_number = ntohs(lacpdu->actor_port);
488 partner->port_priority = ntohs(lacpdu->actor_port_priority);
489 partner->system = lacpdu->actor_system;
490 partner->system_priority = ntohs(lacpdu->actor_system_priority);
491 partner->key = ntohs(lacpdu->actor_key);
492 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 // set actor_oper_port_state.defaulted to FALSE
495 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
496
497 // set the partner sync. to on if the partner is sync. and the port is matched
Bandan Das7bfc4752010-10-16 20:19:59 +0000498 if ((port->sm_vars & AD_PORT_MATCHED)
499 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800500 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Bandan Das7bfc4752010-10-16 20:19:59 +0000501 else
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800502 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504}
505
506/**
507 * __record_default - record default parameters
508 * @port: the port we're looking at
509 *
510 * This function records the default parameter values for the partner carried
511 * in the Partner Admin parameters as the current partner operational parameter
512 * values and sets actor_oper_port_state.defaulted to TRUE.
513 */
514static void __record_default(struct port *port)
515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (port) {
517 // record the partner admin parameters
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800518 memcpy(&port->partner_oper, &port->partner_admin,
519 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 // set actor_oper_port_state.defaulted to true
522 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
523 }
524}
525
526/**
527 * __update_selected - update a port's Selected variable from a received lacpdu
528 * @lacpdu: the lacpdu we've received
529 * @port: the port we're looking at
530 *
531 * Update the value of the selected variable, using parameter values from a
532 * newly received lacpdu. The parameter values for the Actor carried in the
533 * received PDU are compared with the corresponding operational parameter
534 * values for the ports partner. If one or more of the comparisons shows that
535 * the value(s) received in the PDU differ from the current operational values,
536 * then selected is set to FALSE and actor_oper_port_state.synchronization is
537 * set to out_of_sync. Otherwise, selected remains unchanged.
538 */
539static void __update_selected(struct lacpdu *lacpdu, struct port *port)
540{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800542 const struct port_params *partner = &port->partner_oper;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 // check if any parameter is different
Joe Perches8e95a202009-12-03 07:58:21 +0000545 if (ntohs(lacpdu->actor_port) != partner->port_number ||
546 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
547 MAC_ADDRESS_COMPARE(&lacpdu->actor_system, &partner->system) ||
548 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
549 ntohs(lacpdu->actor_key) != partner->key ||
550 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 // update the state machine Selected variable
552 port->sm_vars &= ~AD_PORT_SELECTED;
553 }
554 }
555}
556
557/**
558 * __update_default_selected - update a port's Selected variable from Partner
559 * @port: the port we're looking at
560 *
561 * This function updates the value of the selected variable, using the partner
562 * administrative parameter values. The administrative values are compared with
563 * the corresponding operational parameter values for the partner. If one or
564 * more of the comparisons shows that the administrative value(s) differ from
565 * the current operational values, then Selected is set to FALSE and
566 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
567 * Selected remains unchanged.
568 */
569static void __update_default_selected(struct port *port)
570{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800572 const struct port_params *admin = &port->partner_admin;
573 const struct port_params *oper = &port->partner_oper;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 // check if any parameter is different
Joe Perches8e95a202009-12-03 07:58:21 +0000576 if (admin->port_number != oper->port_number ||
577 admin->port_priority != oper->port_priority ||
578 MAC_ADDRESS_COMPARE(&admin->system, &oper->system) ||
579 admin->system_priority != oper->system_priority ||
580 admin->key != oper->key ||
581 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800582 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 // update the state machine Selected variable
584 port->sm_vars &= ~AD_PORT_SELECTED;
585 }
586 }
587}
588
589/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * __update_ntt - update a port's ntt variable from a received lacpdu
591 * @lacpdu: the lacpdu we've received
592 * @port: the port we're looking at
593 *
594 * Updates the value of the ntt variable, using parameter values from a newly
595 * received lacpdu. The parameter values for the partner carried in the
596 * received PDU are compared with the corresponding operational parameter
597 * values for the Actor. If one or more of the comparisons shows that the
598 * value(s) received in the PDU differ from the current operational values,
599 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
600 */
601static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
602{
603 // validate lacpdu and port
604 if (lacpdu && port) {
605 // check if any parameter is different
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700606 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
607 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700609 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
610 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
612 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
613 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
614 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
615 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800616
617 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 }
620}
621
622/**
623 * __attach_bond_to_agg
624 * @port: the port we're looking at
625 *
626 * Handle the attaching of the port's control parser/multiplexer and the
627 * aggregator. This function does nothing since the parser/multiplexer of the
628 * receive and the parser/multiplexer of the aggregator are already combined.
629 */
630static void __attach_bond_to_agg(struct port *port)
631{
Bandan Das128ea6c2010-10-16 20:19:58 +0000632 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 // This function does nothing since the parser/multiplexer of the receive
634 // and the parser/multiplexer of the aggregator are already combined
635}
636
637/**
638 * __detach_bond_from_agg
639 * @port: the port we're looking at
640 *
641 * Handle the detaching of the port's control parser/multiplexer from the
642 * aggregator. This function does nothing since the parser/multiplexer of the
643 * receive and the parser/multiplexer of the aggregator are already combined.
644 */
645static void __detach_bond_from_agg(struct port *port)
646{
Bandan Das128ea6c2010-10-16 20:19:58 +0000647 port = NULL; /* just to satisfy the compiler */
Jesper Juhlb9d6d2d2012-02-05 13:18:57 +0000648 // This function does nothing since the parser/multiplexer of the receive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 // and the parser/multiplexer of the aggregator are already combined
650}
651
652/**
653 * __agg_ports_are_ready - check if all ports in an aggregator are ready
654 * @aggregator: the aggregator we're looking at
655 *
656 */
657static int __agg_ports_are_ready(struct aggregator *aggregator)
658{
659 struct port *port;
660 int retval = 1;
661
662 if (aggregator) {
663 // scan all ports in this aggregator to verfy if they are all ready
Bandan Das128ea6c2010-10-16 20:19:58 +0000664 for (port = aggregator->lag_ports;
665 port;
666 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!(port->sm_vars & AD_PORT_READY_N)) {
668 retval = 0;
669 break;
670 }
671 }
672 }
673
674 return retval;
675}
676
677/**
678 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
679 * @aggregator: the aggregator we're looking at
680 * @val: Should the ports' ready bit be set on or off
681 *
682 */
683static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
684{
685 struct port *port;
686
Bandan Das128ea6c2010-10-16 20:19:58 +0000687 for (port = aggregator->lag_ports; port;
688 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000689 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000691 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694}
695
696/**
697 * __get_agg_bandwidth - get the total bandwidth of an aggregator
698 * @aggregator: the aggregator we're looking at
699 *
700 */
701static u32 __get_agg_bandwidth(struct aggregator *aggregator)
702{
Bandan Das128ea6c2010-10-16 20:19:58 +0000703 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (aggregator->num_of_ports) {
David Decotigny65cce192011-04-13 15:22:30 +0000706 switch (__get_link_speed(aggregator->lag_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case AD_LINK_SPEED_BITMASK_1MBPS:
708 bandwidth = aggregator->num_of_ports;
709 break;
710 case AD_LINK_SPEED_BITMASK_10MBPS:
711 bandwidth = aggregator->num_of_ports * 10;
712 break;
713 case AD_LINK_SPEED_BITMASK_100MBPS:
714 bandwidth = aggregator->num_of_ports * 100;
715 break;
716 case AD_LINK_SPEED_BITMASK_1000MBPS:
717 bandwidth = aggregator->num_of_ports * 1000;
718 break;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700719 case AD_LINK_SPEED_BITMASK_10000MBPS:
720 bandwidth = aggregator->num_of_ports * 10000;
721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000723 bandwidth = 0; /*to silence the compiler ....*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 }
726 return bandwidth;
727}
728
729/**
730 * __get_active_agg - get the current active aggregator
731 * @aggregator: the aggregator we're looking at
732 *
733 */
734static struct aggregator *__get_active_agg(struct aggregator *aggregator)
735{
736 struct aggregator *retval = NULL;
737
738 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
739 if (aggregator->is_active) {
740 retval = aggregator;
741 break;
742 }
743 }
744
745 return retval;
746}
747
748/**
749 * __update_lacpdu_from_port - update a port's lacpdu fields
750 * @port: the port we're looking at
751 *
752 */
753static inline void __update_lacpdu_from_port(struct port *port)
754{
755 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800756 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /* update current actual Actor parameters */
759 /* lacpdu->subtype initialized
760 * lacpdu->version_number initialized
761 * lacpdu->tlv_type_actor_info initialized
762 * lacpdu->actor_information_length initialized
763 */
764
Al Virod3bb52b2007-08-22 20:06:58 -0400765 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400767 lacpdu->actor_key = htons(port->actor_oper_port_key);
768 lacpdu->actor_port_priority = htons(port->actor_port_priority);
769 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 lacpdu->actor_state = port->actor_oper_port_state;
771
772 /* lacpdu->reserved_3_1 initialized
773 * lacpdu->tlv_type_partner_info initialized
774 * lacpdu->partner_information_length initialized
775 */
776
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800777 lacpdu->partner_system_priority = htons(partner->system_priority);
778 lacpdu->partner_system = partner->system;
779 lacpdu->partner_key = htons(partner->key);
780 lacpdu->partner_port_priority = htons(partner->port_priority);
781 lacpdu->partner_port = htons(partner->port_number);
782 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* lacpdu->reserved_3_2 initialized
785 * lacpdu->tlv_type_collector_info initialized
786 * lacpdu->collector_information_length initialized
787 * collector_max_delay initialized
788 * reserved_12[12] initialized
789 * tlv_type_terminator initialized
790 * terminator_length initialized
791 * reserved_50[50] initialized
792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795//////////////////////////////////////////////////////////////////////////////////////
796// ================= main 802.3ad protocol code ======================================
797//////////////////////////////////////////////////////////////////////////////////////
798
799/**
800 * ad_lacpdu_send - send out a lacpdu packet on a given port
801 * @port: the port we're looking at
802 *
803 * Returns: 0 on success
804 * < 0 on error
805 */
806static int ad_lacpdu_send(struct port *port)
807{
808 struct slave *slave = port->slave;
809 struct sk_buff *skb;
810 struct lacpdu_header *lacpdu_header;
811 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000814 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700818 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700819 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 skb->protocol = PKT_TYPE_LACPDU;
821 skb->priority = TC_PRIO_CONTROL;
822
823 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
824
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800825 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400826 /* Note: source address is set to be the member's PERMANENT address,
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800827 because we use it to identify loopback lacpdus in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800828 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
829 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 lacpdu_header->lacpdu = port->lacpdu; // struct copy
832
833 dev_queue_xmit(skb);
834
835 return 0;
836}
837
838/**
839 * ad_marker_send - send marker information/response on a given port
840 * @port: the port we're looking at
841 * @marker: marker data to send
842 *
843 * Returns: 0 on success
844 * < 0 on error
845 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700846static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct slave *slave = port->slave;
849 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700850 struct bond_marker_header *marker_header;
851 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000854 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 skb_reserve(skb, 16);
858
859 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700860 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700861 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 skb->protocol = PKT_TYPE_LACPDU;
863
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700864 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800866 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400867 /* Note: source address is set to be the member's PERMANENT address,
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800868 because we use it to identify loopback MARKERs in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800869 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
870 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 marker_header->marker = *marker; // struct copy
873
874 dev_queue_xmit(skb);
875
876 return 0;
877}
878
879/**
880 * ad_mux_machine - handle a port's mux state machine
881 * @port: the port we're looking at
882 *
883 */
884static void ad_mux_machine(struct port *port)
885{
886 mux_states_t last_state;
887
888 // keep current State Machine state to compare later if it was changed
889 last_state = port->sm_mux_state;
890
891 if (port->sm_vars & AD_PORT_BEGIN) {
892 port->sm_mux_state = AD_MUX_DETACHED; // next state
893 } else {
894 switch (port->sm_mux_state) {
895 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000896 if ((port->sm_vars & AD_PORT_SELECTED)
897 || (port->sm_vars & AD_PORT_STANDBY))
898 /* if SELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 port->sm_mux_state = AD_MUX_WAITING; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
901 case AD_MUX_WAITING:
902 // if SELECTED == FALSE return to DETACH state
903 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
904 port->sm_vars &= ~AD_PORT_READY_N;
905 // in order to withhold the Selection Logic to check all ports READY_N value
906 // every callback cycle to update ready variable, we check READY_N and update READY here
907 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
908 port->sm_mux_state = AD_MUX_DETACHED; // next state
909 break;
910 }
911
912 // check if the wait_while_timer expired
Bandan Das7bfc4752010-10-16 20:19:59 +0000913 if (port->sm_mux_timer_counter
914 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 // in order to withhold the selection logic to check all ports READY_N value
918 // every callback cycle to update ready variable, we check READY_N and update READY here
919 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
920
921 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
Bandan Das7bfc4752010-10-16 20:19:59 +0000922 if ((port->sm_vars & AD_PORT_READY)
923 && !port->sm_mux_timer_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 port->sm_mux_state = AD_MUX_ATTACHED; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 break;
926 case AD_MUX_ATTACHED:
927 // 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 -0800928 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 -0700929 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
930 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
931 port->sm_vars &= ~AD_PORT_READY_N;
932 // in order to withhold the selection logic to check all ports READY_N value
933 // every callback cycle to update ready variable, we check READY_N and update READY here
934 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
935 port->sm_mux_state = AD_MUX_DETACHED;// next state
936 }
937 break;
938 case AD_MUX_COLLECTING_DISTRIBUTING:
939 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -0800940 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ) {
942 port->sm_mux_state = AD_MUX_ATTACHED;// next state
943
944 } else {
945 // if port state hasn't changed make
946 // sure that a collecting distributing
947 // port in an active aggregator is enabled
948 if (port->aggregator &&
949 port->aggregator->is_active &&
950 !__port_is_enabled(port)) {
951
952 __enable_port(port);
953 }
954 }
955 break;
956 default: //to silence the compiler
957 break;
958 }
959 }
960
961 // check if the state machine was changed
962 if (port->sm_mux_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800963 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
964 port->actor_port_number, last_state,
965 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 switch (port->sm_mux_state) {
967 case AD_MUX_DETACHED:
968 __detach_bond_from_agg(port);
969 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
970 ad_disable_collecting_distributing(port);
971 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
972 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800973 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
975 case AD_MUX_WAITING:
976 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
977 break;
978 case AD_MUX_ATTACHED:
979 __attach_bond_to_agg(port);
980 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
981 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
982 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
983 ad_disable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800984 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986 case AD_MUX_COLLECTING_DISTRIBUTING:
987 port->actor_oper_port_state |= AD_STATE_COLLECTING;
988 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
989 ad_enable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800990 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 default: //to silence the compiler
993 break;
994 }
995 }
996}
997
998/**
999 * ad_rx_machine - handle a port's rx State Machine
1000 * @lacpdu: the lacpdu we've received
1001 * @port: the port we're looking at
1002 *
1003 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1004 * CURRENT. If timer expired set the state machine in the proper state.
1005 * In other cases, this function checks if we need to switch to other state.
1006 */
1007static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1008{
1009 rx_states_t last_state;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 // keep current State Machine state to compare later if it was changed
1012 last_state = port->sm_rx_state;
1013
1014 // check if state machine should change state
1015 // first, check if port was reinitialized
Bandan Das7bfc4752010-10-16 20:19:59 +00001016 if (port->sm_vars & AD_PORT_BEGIN)
1017 /* next state */
1018 port->sm_rx_state = AD_RX_INITIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 // check if port is not enabled
Bandan Das7bfc4752010-10-16 20:19:59 +00001020 else if (!(port->sm_vars & AD_PORT_BEGIN)
1021 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
1022 /* next state */
1023 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 // check if new lacpdu arrived
1025 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1026 port->sm_rx_timer_counter = 0; // zero timer
1027 port->sm_rx_state = AD_RX_CURRENT;
1028 } else {
1029 // if timer is on, and if it is expired
1030 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1031 switch (port->sm_rx_state) {
1032 case AD_RX_EXPIRED:
1033 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1034 break;
1035 case AD_RX_CURRENT:
1036 port->sm_rx_state = AD_RX_EXPIRED; // next state
1037 break;
1038 default: //to silence the compiler
1039 break;
1040 }
1041 } else {
1042 // if no lacpdu arrived and no timer is on
1043 switch (port->sm_rx_state) {
1044 case AD_RX_PORT_DISABLED:
Bandan Das7bfc4752010-10-16 20:19:59 +00001045 if (port->sm_vars & AD_PORT_MOVED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 port->sm_rx_state = AD_RX_INITIALIZE; // next state
Bandan Das7bfc4752010-10-16 20:19:59 +00001047 else if (port->is_enabled
1048 && (port->sm_vars
1049 & AD_PORT_LACP_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 port->sm_rx_state = AD_RX_EXPIRED; // next state
Bandan Das7bfc4752010-10-16 20:19:59 +00001051 else if (port->is_enabled
1052 && ((port->sm_vars
1053 & AD_PORT_LACP_ENABLED) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 default: //to silence the compiler
1057 break;
1058
1059 }
1060 }
1061 }
1062
1063 // check if the State machine was changed or new lacpdu arrived
1064 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001065 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1066 port->actor_port_number, last_state,
1067 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 switch (port->sm_rx_state) {
1069 case AD_RX_INITIALIZE:
Bandan Das7bfc4752010-10-16 20:19:59 +00001070 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001072 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 port->sm_vars &= ~AD_PORT_SELECTED;
1075 __record_default(port);
1076 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1077 port->sm_vars &= ~AD_PORT_MOVED;
1078 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1079
1080 /*- Fall Through -*/
1081
1082 case AD_RX_PORT_DISABLED:
1083 port->sm_vars &= ~AD_PORT_MATCHED;
1084 break;
1085 case AD_RX_LACP_DISABLED:
1086 port->sm_vars &= ~AD_PORT_SELECTED;
1087 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001088 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 port->sm_vars |= AD_PORT_MATCHED;
1090 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1091 break;
1092 case AD_RX_EXPIRED:
1093 //Reset of the Synchronization flag. (Standard 43.4.12)
1094 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1095 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001096 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 port->sm_vars &= ~AD_PORT_MATCHED;
Julia Lawall8e321c42009-07-11 10:03:55 +00001098 port->partner_oper.port_state |=
1099 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1101 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1102 break;
1103 case AD_RX_DEFAULTED:
1104 __update_default_selected(port);
1105 __record_default(port);
1106 port->sm_vars |= AD_PORT_MATCHED;
1107 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1108 break;
1109 case AD_RX_CURRENT:
1110 // detect loopback situation
1111 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1112 // INFO_RECEIVED_LOOPBACK_FRAMES
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001113 pr_err("%s: An illegal loopback occurred on adapter (%s).\n"
1114 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001115 port->slave->bond->dev->name, port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return;
1117 }
1118 __update_selected(lacpdu, port);
1119 __update_ntt(lacpdu, port);
1120 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1122 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124 default: //to silence the compiler
1125 break;
1126 }
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/**
1131 * ad_tx_machine - handle a port's tx state machine
1132 * @port: the port we're looking at
1133 *
1134 */
1135static void ad_tx_machine(struct port *port)
1136{
1137 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1138 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1139 // check if there is something to send
1140 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1141 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001144 pr_debug("Sent LACPDU on port %d\n",
1145 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001146
1147 /* mark ntt as false, so it will not be sent again until
1148 demanded */
1149 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 }
1152 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
Bandan Das128ea6c2010-10-16 20:19:58 +00001153 port->sm_tx_timer_counter =
1154 ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156}
1157
1158/**
1159 * ad_periodic_machine - handle a port's periodic state machine
1160 * @port: the port we're looking at
1161 *
1162 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1163 */
1164static void ad_periodic_machine(struct port *port)
1165{
1166 periodic_states_t last_state;
1167
1168 // keep current state machine state to compare later if it was changed
1169 last_state = port->sm_periodic_state;
1170
1171 // check if port was reinitialized
1172 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001173 (!(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 -07001174 ) {
1175 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1176 }
1177 // check if state machine should change state
1178 else if (port->sm_periodic_timer_counter) {
1179 // check if periodic state machine expired
1180 if (!(--port->sm_periodic_timer_counter)) {
1181 // if expired then do tx
1182 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1183 } else {
1184 // If not expired, check if there is some new timeout parameter from the partner state
1185 switch (port->sm_periodic_state) {
1186 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001187 if (!(port->partner_oper.port_state
1188 & AD_STATE_LACP_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001192 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 // stop current timer
1194 port->sm_periodic_timer_counter = 0;
1195 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1196 }
1197 break;
1198 default: //to silence the compiler
1199 break;
1200 }
1201 }
1202 } else {
1203 switch (port->sm_periodic_state) {
1204 case AD_NO_PERIODIC:
1205 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1206 break;
1207 case AD_PERIODIC_TX:
Bandan Das7bfc4752010-10-16 20:19:59 +00001208 if (!(port->partner_oper.port_state
1209 & AD_STATE_LACP_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
Bandan Das7bfc4752010-10-16 20:19:59 +00001211 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214 default: //to silence the compiler
1215 break;
1216 }
1217 }
1218
1219 // check if the state machine was changed
1220 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001221 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1222 port->actor_port_number, last_state,
1223 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 switch (port->sm_periodic_state) {
1225 case AD_NO_PERIODIC:
1226 port->sm_periodic_timer_counter = 0; // zero timer
1227 break;
1228 case AD_FAST_PERIODIC:
1229 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
1230 break;
1231 case AD_SLOW_PERIODIC:
1232 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
1233 break;
1234 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001235 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237 default: //to silence the compiler
1238 break;
1239 }
1240 }
1241}
1242
1243/**
1244 * ad_port_selection_logic - select aggregation groups
1245 * @port: the port we're looking at
1246 *
1247 * Select aggregation groups, and assign each port for it's aggregetor. The
1248 * selection logic is called in the inititalization (after all the handshkes),
1249 * and after every lacpdu receive (if selected is off).
1250 */
1251static void ad_port_selection_logic(struct port *port)
1252{
1253 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1254 struct port *last_port = NULL, *curr_port;
1255 int found = 0;
1256
1257 // if the port is already Selected, do nothing
Bandan Das7bfc4752010-10-16 20:19:59 +00001258 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 // if the port is connected to other aggregator, detach it
1262 if (port->aggregator) {
1263 // detach the port from its former aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001264 temp_aggregator = port->aggregator;
1265 for (curr_port = temp_aggregator->lag_ports; curr_port;
1266 last_port = curr_port,
1267 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (curr_port == port) {
1269 temp_aggregator->num_of_ports--;
1270 if (!last_port) {// if it is the first port attached to the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001271 temp_aggregator->lag_ports =
1272 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } else {// not the first port attached to the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001274 last_port->next_port_in_aggregator =
1275 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 // clear the port's relations to this aggregator
1279 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001280 port->next_port_in_aggregator = NULL;
1281 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001283 pr_debug("Port %d left LAG %d\n",
1284 port->actor_port_number,
1285 temp_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 // if the aggregator is empty, clear its parameters, and set it ready to be attached
Bandan Das7bfc4752010-10-16 20:19:59 +00001287 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 }
1291 }
1292 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 -08001293 pr_warning("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001294 port->slave->bond->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001295 port->actor_port_number,
1296 port->slave->dev->name,
1297 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 }
1300 // search on all aggregators for a suitable aggregator for this port
1301 for (aggregator = __get_first_agg(port); aggregator;
1302 aggregator = __get_next_agg(aggregator)) {
1303
1304 // keep a free aggregator for later use(if needed)
1305 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001306 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001307 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 continue;
1309 }
1310 // check if current aggregator suits us
1311 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001312 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1313 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1314 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001316 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 !aggregator->is_individual) // but is not individual OR
1318 )
1319 ) {
1320 // attach to the founded aggregator
1321 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001322 port->actor_port_aggregator_identifier =
1323 port->aggregator->aggregator_identifier;
1324 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001326 aggregator->lag_ports = port;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001327 pr_debug("Port %d joined LAG %d(existing LAG)\n",
1328 port->actor_port_number,
1329 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 // mark this port as selected
1332 port->sm_vars |= AD_PORT_SELECTED;
1333 found = 1;
1334 break;
1335 }
1336 }
1337
1338 // the port couldn't find an aggregator - attach it to a new aggregator
1339 if (!found) {
1340 if (free_aggregator) {
1341 // assign port a new aggregator
1342 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001343 port->actor_port_aggregator_identifier =
1344 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 // update the new aggregator's parameters
1347 // if port was responsed from the end-user
Bandan Das7bfc4752010-10-16 20:19:59 +00001348 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)
1349 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001350 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001351 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001352 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1355 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001356 port->aggregator->partner_system =
1357 port->partner_oper.system;
1358 port->aggregator->partner_system_priority =
1359 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001360 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 port->aggregator->receive_state = 1;
1362 port->aggregator->transmit_state = 1;
1363 port->aggregator->lag_ports = port;
1364 port->aggregator->num_of_ports++;
1365
1366 // mark this port as selected
1367 port->sm_vars |= AD_PORT_SELECTED;
1368
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001369 pr_debug("Port %d joined LAG %d(new LAG)\n",
1370 port->actor_port_number,
1371 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001373 pr_err("%s: Port %d (on %s) did not find a suitable aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001374 port->slave->bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 port->actor_port_number, port->slave->dev->name);
1376 }
1377 }
1378 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1379 // else set ready=FALSE in all aggregator's ports
1380 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1381
Jay Vosburghfd989c82008-11-04 17:51:16 -08001382 aggregator = __get_first_agg(port);
1383 ad_agg_selection_logic(aggregator);
1384}
1385
1386/*
1387 * Decide if "agg" is a better choice for the new active aggregator that
1388 * the current best, according to the ad_select policy.
1389 */
1390static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1391 struct aggregator *curr)
1392{
1393 /*
1394 * 0. If no best, select current.
1395 *
1396 * 1. If the current agg is not individual, and the best is
1397 * individual, select current.
1398 *
1399 * 2. If current agg is individual and the best is not, keep best.
1400 *
1401 * 3. Therefore, current and best are both individual or both not
1402 * individual, so:
1403 *
1404 * 3a. If current agg partner replied, and best agg partner did not,
1405 * select current.
1406 *
1407 * 3b. If current agg partner did not reply and best agg partner
1408 * did reply, keep best.
1409 *
1410 * 4. Therefore, current and best both have partner replies or
1411 * both do not, so perform selection policy:
1412 *
1413 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1414 * select by bandwidth.
1415 *
1416 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1417 */
1418 if (!best)
1419 return curr;
1420
1421 if (!curr->is_individual && best->is_individual)
1422 return curr;
1423
1424 if (curr->is_individual && !best->is_individual)
1425 return best;
1426
1427 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1428 return curr;
1429
1430 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1431 return best;
1432
1433 switch (__get_agg_selection_mode(curr->lag_ports)) {
1434 case BOND_AD_COUNT:
1435 if (curr->num_of_ports > best->num_of_ports)
1436 return curr;
1437
1438 if (curr->num_of_ports < best->num_of_ports)
1439 return best;
1440
1441 /*FALLTHROUGH*/
1442 case BOND_AD_STABLE:
1443 case BOND_AD_BANDWIDTH:
1444 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1445 return curr;
1446
1447 break;
1448
1449 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001450 pr_warning("%s: Impossible agg select mode %d\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001451 curr->slave->bond->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001452 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001455
1456 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001459static int agg_device_up(const struct aggregator *agg)
1460{
Jiri Bohac2430af82011-04-19 02:09:55 +00001461 struct port *port = agg->lag_ports;
1462 if (!port)
1463 return 0;
1464 return (netif_running(port->slave->dev) &&
1465 netif_carrier_ok(port->slave->dev));
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001466}
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468/**
1469 * ad_agg_selection_logic - select an aggregation group for a team
1470 * @aggregator: the aggregator we're looking at
1471 *
1472 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001473 *
1474 * The logic of this function is to select the aggregator according to
1475 * the ad_select policy:
1476 *
1477 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1478 * it, and to reselect the active aggregator only if the previous
1479 * aggregator has no more ports related to it.
1480 *
1481 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1482 * bandwidth, and reselect whenever a link state change takes place or the
1483 * set of slaves in the bond changes.
1484 *
1485 * BOND_AD_COUNT: select the aggregator with largest number of ports
1486 * (slaves), and reselect whenever a link state change takes place or the
1487 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 *
1489 * FIXME: this function MUST be called with the first agg in the bond, or
1490 * __get_active_agg() won't work correctly. This function should be better
1491 * called with the bond itself, and retrieve the first agg from it.
1492 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001493static void ad_agg_selection_logic(struct aggregator *agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001495 struct aggregator *best, *active, *origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Jay Vosburghfd989c82008-11-04 17:51:16 -08001498 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001499 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001500 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 do {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001503 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001505 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001506 best = ad_agg_selection_test(best, agg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Jay Vosburghfd989c82008-11-04 17:51:16 -08001508 } while ((agg = __get_next_agg(agg)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jay Vosburghfd989c82008-11-04 17:51:16 -08001510 if (best &&
1511 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1512 /*
1513 * For the STABLE policy, don't replace the old active
1514 * aggregator if it's still active (it has an answering
1515 * partner) or if both the best and active don't have an
1516 * answering partner.
1517 */
1518 if (active && active->lag_ports &&
1519 active->lag_ports->is_enabled &&
1520 (__agg_has_partner(active) ||
1521 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1522 if (!(!active->actor_oper_aggregator_key &&
1523 best->actor_oper_aggregator_key)) {
1524 best = NULL;
1525 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 }
1528 }
1529
Jay Vosburghfd989c82008-11-04 17:51:16 -08001530 if (best && (best == active)) {
1531 best = NULL;
1532 active->is_active = 1;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Jay Vosburghfd989c82008-11-04 17:51:16 -08001535 // if there is new best aggregator, activate it
1536 if (best) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001537 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 -08001538 best->aggregator_identifier, best->num_of_ports,
1539 best->actor_oper_aggregator_key,
1540 best->partner_oper_aggregator_key,
1541 best->is_individual, best->is_active);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001542 pr_debug("best ports %p slave %p %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001543 best->lag_ports, best->slave,
1544 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001545
1546 for (agg = __get_first_agg(best->lag_ports); agg;
1547 agg = __get_next_agg(agg)) {
1548
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001549 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001550 agg->aggregator_identifier, agg->num_of_ports,
1551 agg->actor_oper_aggregator_key,
1552 agg->partner_oper_aggregator_key,
1553 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
1556 // check if any partner replys
Jay Vosburghfd989c82008-11-04 17:51:16 -08001557 if (best->is_individual) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001558 pr_warning("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001559 best->slave ? best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Jay Vosburghfd989c82008-11-04 17:51:16 -08001562 best->is_active = 1;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001563 pr_debug("LAG %d chosen as the active LAG\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001564 best->aggregator_identifier);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001565 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001566 best->aggregator_identifier, best->num_of_ports,
1567 best->actor_oper_aggregator_key,
1568 best->partner_oper_aggregator_key,
1569 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 // disable the ports that were related to the former active_aggregator
Jay Vosburghfd989c82008-11-04 17:51:16 -08001572 if (active) {
1573 for (port = active->lag_ports; port;
1574 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 __disable_port(port);
1576 }
1577 }
1578 }
1579
Jay Vosburghfd989c82008-11-04 17:51:16 -08001580 /*
1581 * if the selected aggregator is of join individuals
1582 * (partner_system is NULL), enable their ports
1583 */
1584 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Jay Vosburghfd989c82008-11-04 17:51:16 -08001586 if (active) {
1587 if (!__agg_has_partner(active)) {
1588 for (port = active->lag_ports; port;
1589 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 __enable_port(port);
1591 }
1592 }
1593 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001594
1595 if (origin->slave) {
1596 struct bonding *bond;
1597
1598 bond = bond_get_bond_by_slave(origin->slave);
1599 if (bond)
1600 bond_3ad_set_carrier(bond);
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604/**
1605 * ad_clear_agg - clear a given aggregator's parameters
1606 * @aggregator: the aggregator we're looking at
1607 *
1608 */
1609static void ad_clear_agg(struct aggregator *aggregator)
1610{
1611 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001612 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 aggregator->actor_admin_aggregator_key = 0;
1614 aggregator->actor_oper_aggregator_key = 0;
1615 aggregator->partner_system = null_mac_addr;
1616 aggregator->partner_system_priority = 0;
1617 aggregator->partner_oper_aggregator_key = 0;
1618 aggregator->receive_state = 0;
1619 aggregator->transmit_state = 0;
1620 aggregator->lag_ports = NULL;
1621 aggregator->is_active = 0;
1622 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001623 pr_debug("LAG %d was cleared\n",
1624 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626}
1627
1628/**
1629 * ad_initialize_agg - initialize a given aggregator's parameters
1630 * @aggregator: the aggregator we're looking at
1631 *
1632 */
1633static void ad_initialize_agg(struct aggregator *aggregator)
1634{
1635 if (aggregator) {
1636 ad_clear_agg(aggregator);
1637
1638 aggregator->aggregator_mac_address = null_mac_addr;
1639 aggregator->aggregator_identifier = 0;
1640 aggregator->slave = NULL;
1641 }
1642}
1643
1644/**
1645 * ad_initialize_port - initialize a given port's parameters
1646 * @aggregator: the aggregator we're looking at
1647 * @lacp_fast: boolean. whether fast periodic should be used
1648 *
1649 */
1650static void ad_initialize_port(struct port *port, int lacp_fast)
1651{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001652 static const struct port_params tmpl = {
1653 .system_priority = 0xffff,
1654 .key = 1,
1655 .port_number = 1,
1656 .port_priority = 0xff,
1657 .port_state = 1,
1658 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001659 static const struct lacpdu lacpdu = {
1660 .subtype = 0x01,
1661 .version_number = 0x01,
1662 .tlv_type_actor_info = 0x01,
1663 .actor_information_length = 0x14,
1664 .tlv_type_partner_info = 0x02,
1665 .partner_information_length = 0x14,
1666 .tlv_type_collector_info = 0x03,
1667 .collector_information_length = 0x10,
1668 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1669 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (port) {
1672 port->actor_port_number = 1;
1673 port->actor_port_priority = 0xff;
1674 port->actor_system = null_mac_addr;
1675 port->actor_system_priority = 0xffff;
1676 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001677 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 port->actor_admin_port_key = 1;
1679 port->actor_oper_port_key = 1;
1680 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1681 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1682
Bandan Das7bfc4752010-10-16 20:19:59 +00001683 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001686 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1687 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1688
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001689 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 // ****** private parameters ******
1691 port->sm_vars = 0x3;
1692 port->sm_rx_state = 0;
1693 port->sm_rx_timer_counter = 0;
1694 port->sm_periodic_state = 0;
1695 port->sm_periodic_timer_counter = 0;
1696 port->sm_mux_state = 0;
1697 port->sm_mux_timer_counter = 0;
1698 port->sm_tx_state = 0;
1699 port->sm_tx_timer_counter = 0;
1700 port->slave = NULL;
1701 port->aggregator = NULL;
1702 port->next_port_in_aggregator = NULL;
1703 port->transaction_id = 0;
1704
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001705 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707}
1708
1709/**
1710 * ad_enable_collecting_distributing - enable a port's transmit/receive
1711 * @port: the port we're looking at
1712 *
1713 * Enable @port if it's in an active aggregator
1714 */
1715static void ad_enable_collecting_distributing(struct port *port)
1716{
1717 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001718 pr_debug("Enabling port %d(LAG %d)\n",
1719 port->actor_port_number,
1720 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 __enable_port(port);
1722 }
1723}
1724
1725/**
1726 * ad_disable_collecting_distributing - disable a port's transmit/receive
1727 * @port: the port we're looking at
1728 *
1729 */
1730static void ad_disable_collecting_distributing(struct port *port)
1731{
1732 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001733 pr_debug("Disabling port %d(LAG %d)\n",
1734 port->actor_port_number,
1735 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 __disable_port(port);
1737 }
1738}
1739
1740#if 0
1741/**
1742 * ad_marker_info_send - send a marker information frame
1743 * @port: the port we're looking at
1744 *
1745 * This function does nothing since we decided not to implement send and handle
1746 * response for marker PDU's, in this stage, but only to respond to marker
1747 * information.
1748 */
1749static void ad_marker_info_send(struct port *port)
1750{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001751 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 u16 index;
1753
1754 // fill the marker PDU with the appropriate values
1755 marker.subtype = 0x02;
1756 marker.version_number = 0x01;
1757 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1758 marker.marker_length = 0x16;
1759 // convert requester_port to Big Endian
1760 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1761 marker.requester_system = port->actor_system;
1762 // convert requester_port(u32) to Big Endian
Bandan Das128ea6c2010-10-16 20:19:58 +00001763 marker.requester_transaction_id =
1764 (((++port->transaction_id & 0xFF) << 24)
1765 | ((port->transaction_id & 0xFF00) << 8)
1766 | ((port->transaction_id & 0xFF0000) >> 8)
1767 | ((port->transaction_id & 0xFF000000) >> 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 marker.pad = 0;
1769 marker.tlv_type_terminator = 0x00;
1770 marker.terminator_length = 0x00;
Bandan Das128ea6c2010-10-16 20:19:58 +00001771 for (index = 0; index < 90; index++)
1772 marker.reserved_90[index] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 // send the marker information
1775 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001776 pr_debug("Sent Marker Information on port %d\n",
1777 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779}
1780#endif
1781
1782/**
1783 * ad_marker_info_received - handle receive of a Marker information frame
1784 * @marker_info: Marker info received
1785 * @port: the port we're looking at
1786 *
1787 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001788static void ad_marker_info_received(struct bond_marker *marker_info,
1789 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001791 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 // copy the received marker data to the response marker
1794 //marker = *marker_info;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001795 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 // change the marker subtype to marker response
Bandan Das128ea6c2010-10-16 20:19:58 +00001797 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 // send the marker response
1799
1800 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001801 pr_debug("Sent Marker Response on port %d\n",
1802 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804}
1805
1806/**
1807 * ad_marker_response_received - handle receive of a marker response frame
1808 * @marker: marker PDU received
1809 * @port: the port we're looking at
1810 *
1811 * This function does nothing since we decided not to implement send and handle
1812 * response for marker PDU's, in this stage, but only to respond to marker
1813 * information.
1814 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001815static void ad_marker_response_received(struct bond_marker *marker,
1816 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Bandan Das128ea6c2010-10-16 20:19:58 +00001818 marker = NULL; /* just to satisfy the compiler */
1819 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823//////////////////////////////////////////////////////////////////////////////////////
1824// ================= AD exported functions to the main bonding code ==================
1825//////////////////////////////////////////////////////////////////////////////////////
1826
1827// Check aggregators status in team every T seconds
1828#define AD_AGGREGATOR_SELECTION_TIMER 8
1829
Jay Vosburghfd989c82008-11-04 17:51:16 -08001830/*
1831 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1832 *
1833 * Set the aggregation selection timer, to initiate an agg selection in
1834 * the very near future. Called during first initialization, and during
1835 * any down to up transitions of the bond.
1836 */
1837void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1838{
1839 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842static u16 aggregator_identifier;
1843
1844/**
1845 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1846 * @bond: bonding struct to work on
1847 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 *
1849 * Can be called only after the mac address of the bond is set.
1850 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001851void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 // check that the bond is not initialized yet
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001854 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1855 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 aggregator_identifier = 0;
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1860 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1861
1862 // initialize how many times this module is called in one second(should be about every 100ms)
1863 ad_ticks_per_sec = tick_resolution;
1864
Jay Vosburghfd989c82008-11-04 17:51:16 -08001865 bond_3ad_initiate_agg_selection(bond,
1866 AD_AGGREGATOR_SELECTION_TIMER *
1867 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869}
1870
1871/**
1872 * bond_3ad_bind_slave - initialize a slave's port
1873 * @slave: slave struct to work on
1874 *
1875 * Returns: 0 on success
1876 * < 0 on error
1877 */
1878int bond_3ad_bind_slave(struct slave *slave)
1879{
1880 struct bonding *bond = bond_get_bond_by_slave(slave);
1881 struct port *port;
1882 struct aggregator *aggregator;
1883
1884 if (bond == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001885 pr_err("%s: The slave %s is not attached to its bond\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001886 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return -1;
1888 }
1889
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001890 //check that the slave has not been initialized yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1892
1893 // port initialization
1894 port = &(SLAVE_AD_INFO(slave).port);
1895
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001896 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
nikolay@redhat.come0809db2013-02-18 07:59:03 +00001898 __initialize_port_locks(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 port->slave = slave;
1900 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1901 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1902 // ------------------------------------------------------------
1903 // Port key : | User key | Speed |Duplex|
1904 // ------------------------------------------------------------
1905 // 16 6 1 0
1906 port->actor_admin_port_key = 0; // initialize this parameter
1907 port->actor_admin_port_key |= __get_duplex(port);
1908 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1909 port->actor_oper_port_key = port->actor_admin_port_key;
1910 // if the port is not full duplex, then the port should be not lacp Enabled
Bandan Das7bfc4752010-10-16 20:19:59 +00001911 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 // actor system is the bond's system
1914 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1915 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1916 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1917 port->aggregator = NULL;
1918 port->next_port_in_aggregator = NULL;
1919
1920 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 // aggregator initialization
1923 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1924
1925 ad_initialize_agg(aggregator);
1926
1927 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1928 aggregator->aggregator_identifier = (++aggregator_identifier);
1929 aggregator->slave = slave;
1930 aggregator->is_active = 0;
1931 aggregator->num_of_ports = 0;
1932 }
1933
1934 return 0;
1935}
1936
1937/**
1938 * bond_3ad_unbind_slave - deinitialize a slave's port
1939 * @slave: slave struct to work on
1940 *
1941 * Search for the aggregator that is related to this port, remove the
1942 * aggregator and assign another aggregator for other port related to it
1943 * (if any), and remove the port.
1944 */
1945void bond_3ad_unbind_slave(struct slave *slave)
1946{
1947 struct port *port, *prev_port, *temp_port;
1948 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1949 int select_new_active_agg = 0;
Jasper Spaansa361c832009-10-23 04:09:24 +00001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 // find the aggregator related to this slave
1952 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1953
1954 // find the port related to this slave
1955 port = &(SLAVE_AD_INFO(slave).port);
1956
1957 // if slave is null, the whole port is not initialized
1958 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001959 pr_warning("Warning: %s: Trying to unbind an uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001960 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return;
1962 }
1963
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001964 pr_debug("Unbinding Link Aggregation Group %d\n",
1965 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Tell the partner that this port is not suitable for aggregation */
1968 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1969 __update_lacpdu_from_port(port);
1970 ad_lacpdu_send(port);
1971
1972 // check if this aggregator is occupied
1973 if (aggregator->lag_ports) {
1974 // check if there are other ports related to this aggregator except
1975 // the port related to this slave(thats ensure us that there is a
1976 // reason to search for new aggregator, and that we will find one
1977 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
1978 // find new aggregator for the related port(s)
1979 new_aggregator = __get_first_agg(port);
1980 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001981 // if the new aggregator is empty, or it is connected to our port only
Bandan Das7bfc4752010-10-16 20:19:59 +00001982 if (!new_aggregator->lag_ports
1983 || ((new_aggregator->lag_ports == port)
1984 && !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 // if new aggregator found, copy the aggregator's parameters
1988 // and connect the related lag_ports to the new aggregator
1989 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 -08001990 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n",
1991 aggregator->aggregator_identifier,
1992 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001995 pr_info("%s: Removing an active aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001996 aggregator->slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 // select new active aggregator
1998 select_new_active_agg = 1;
1999 }
2000
2001 new_aggregator->is_individual = aggregator->is_individual;
2002 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2003 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2004 new_aggregator->partner_system = aggregator->partner_system;
2005 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2006 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2007 new_aggregator->receive_state = aggregator->receive_state;
2008 new_aggregator->transmit_state = aggregator->transmit_state;
2009 new_aggregator->lag_ports = aggregator->lag_ports;
2010 new_aggregator->is_active = aggregator->is_active;
2011 new_aggregator->num_of_ports = aggregator->num_of_ports;
2012
2013 // update the information that is written on the ports about the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00002014 for (temp_port = aggregator->lag_ports; temp_port;
2015 temp_port = temp_port->next_port_in_aggregator) {
2016 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2018 }
2019
2020 // clear the aggregator
2021 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00002022
Bandan Das7bfc4752010-10-16 20:19:59 +00002023 if (select_new_active_agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 ad_agg_selection_logic(__get_first_agg(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002026 pr_warning("%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002027 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029 } else { // in case that the only port related to this aggregator is the one we want to remove
2030 select_new_active_agg = aggregator->is_active;
2031 // clear the aggregator
2032 ad_clear_agg(aggregator);
2033 if (select_new_active_agg) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002034 pr_info("%s: Removing an active aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002035 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 // select new active aggregator
Veaceslav Falico74684492013-09-27 15:10:58 +02002037 temp_aggregator = __get_first_agg(port);
2038 if (temp_aggregator)
2039 ad_agg_selection_logic(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041 }
2042 }
2043
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002044 pr_debug("Unbinding port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 // find the aggregator that this port is connected to
2046 temp_aggregator = __get_first_agg(port);
2047 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2048 prev_port = NULL;
2049 // search the port in the aggregator's related ports
Bandan Das128ea6c2010-10-16 20:19:58 +00002050 for (temp_port = temp_aggregator->lag_ports; temp_port;
2051 prev_port = temp_port,
2052 temp_port = temp_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
Bandan Das7bfc4752010-10-16 20:19:59 +00002054 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00002056 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00002059 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 select_new_active_agg = temp_aggregator->is_active;
2061 // clear the aggregator
2062 ad_clear_agg(temp_aggregator);
2063 if (select_new_active_agg) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002064 pr_info("%s: Removing an active aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002065 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 // select new active aggregator
2067 ad_agg_selection_logic(__get_first_agg(port));
2068 }
2069 }
2070 break;
2071 }
2072 }
2073 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002074 port->slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077/**
2078 * bond_3ad_state_machine_handler - handle state machines timeout
2079 * @bond: bonding struct to work on
2080 *
2081 * The state machine handling concept in this module is to check every tick
2082 * which state machine should operate any function. The execution order is
2083 * round robin, so when we have an interaction between state machines, the
2084 * reply of one to each other might be delayed until next tick.
2085 *
2086 * This function also complete the initialization when the agg_select_timer
2087 * times out, and it selects an aggregator for the ports that are yet not
2088 * related to any aggregator, and selects the active aggregator for a bond.
2089 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002090void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002092 struct bonding *bond = container_of(work, struct bonding,
2093 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002095 struct list_head *iter;
2096 struct slave *slave;
2097 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 read_lock(&bond->lock);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 //check if there are any slaves
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002102 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 // check if agg_select_timer timer after initialize is timed out
2106 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2107 // select the active aggregator for the bond
2108 if ((port = __get_first_port(bond))) {
2109 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002110 pr_warning("%s: Warning: bond's first port is uninitialized\n",
2111 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 goto re_arm;
2113 }
2114
2115 aggregator = __get_first_agg(port);
2116 ad_agg_selection_logic(aggregator);
2117 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002118 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
2121 // for each port run the state machines
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002122 bond_for_each_slave(bond, slave, iter) {
2123 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002125 pr_warning("%s: Warning: Found an uninitialized port\n",
2126 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto re_arm;
2128 }
2129
Nils Carlson16d79d72011-03-03 22:09:11 +00002130 /* Lock around state machines to protect data accessed
2131 * by all (e.g., port->sm_vars). ad_rx_machine may run
2132 * concurrently due to incoming LACPDU.
2133 */
Nils Carlson9ac35242011-03-03 22:09:12 +00002134 __get_state_machine_lock(port);
Nils Carlson16d79d72011-03-03 22:09:11 +00002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 ad_rx_machine(NULL, port);
2137 ad_periodic_machine(port);
2138 ad_port_selection_logic(port);
2139 ad_mux_machine(port);
2140 ad_tx_machine(port);
2141
2142 // turn off the BEGIN bit, since we already handled it
Bandan Das7bfc4752010-10-16 20:19:59 +00002143 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 port->sm_vars &= ~AD_PORT_BEGIN;
Nils Carlson16d79d72011-03-03 22:09:11 +00002145
Nils Carlson9ac35242011-03-03 22:09:12 +00002146 __release_state_machine_lock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
2149re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002150 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 read_unlock(&bond->lock);
2153}
2154
2155/**
2156 * bond_3ad_rx_indication - handle a received frame
2157 * @lacpdu: received lacpdu
2158 * @slave: slave struct to work on
2159 * @length: length of the data received
2160 *
2161 * It is assumed that frames that were sent on this NIC don't returned as new
2162 * received frames (loopback). Since only the payload is given to this
2163 * function, it check for loopback.
2164 */
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002165static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002168 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 if (length >= sizeof(struct lacpdu)) {
2171
2172 port = &(SLAVE_AD_INFO(slave).port);
2173
2174 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002175 pr_warning("%s: Warning: port of slave %s is uninitialized\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002176 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
2179
2180 switch (lacpdu->subtype) {
2181 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002182 ret = RX_HANDLER_CONSUMED;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002183 pr_debug("Received LACPDU on port %d\n",
2184 port->actor_port_number);
Nils Carlson16d79d72011-03-03 22:09:11 +00002185 /* Protect against concurrent state machines */
Nils Carlson9ac35242011-03-03 22:09:12 +00002186 __get_state_machine_lock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 ad_rx_machine(lacpdu, port);
Nils Carlson9ac35242011-03-03 22:09:12 +00002188 __release_state_machine_lock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 break;
2190
2191 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002192 ret = RX_HANDLER_CONSUMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 // No need to convert fields to Little Endian since we don't use the marker's fields.
2194
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002195 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 case AD_MARKER_INFORMATION_SUBTYPE:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002197 pr_debug("Received Marker Information on port %d\n",
2198 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002199 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 break;
2201
2202 case AD_MARKER_RESPONSE_SUBTYPE:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002203 pr_debug("Received Marker Response on port %d\n",
2204 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002205 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207
2208 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002209 pr_debug("Received an unknown Marker subtype on slot %d\n",
2210 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 }
2213 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217/**
2218 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2219 * @slave: slave struct to work on
2220 *
2221 * Handle reselection of aggregator (if needed) for this port.
2222 */
2223void bond_3ad_adapter_speed_changed(struct slave *slave)
2224{
2225 struct port *port;
2226
2227 port = &(SLAVE_AD_INFO(slave).port);
2228
2229 // if slave is null, the whole port is not initialized
2230 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002231 pr_warning("Warning: %s: speed changed for uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002232 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return;
2234 }
2235
2236 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002237 port->actor_oper_port_key = port->actor_admin_port_key |=
2238 (__get_link_speed(port) << 1);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002239 pr_debug("Port %d changed speed\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 // there is no need to reselect a new aggregator, just signal the
2241 // state machines to reinitialize
2242 port->sm_vars |= AD_PORT_BEGIN;
2243}
2244
2245/**
2246 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2247 * @slave: slave struct to work on
2248 *
2249 * Handle reselection of aggregator (if needed) for this port.
2250 */
2251void bond_3ad_adapter_duplex_changed(struct slave *slave)
2252{
2253 struct port *port;
2254
Bandan Das128ea6c2010-10-16 20:19:58 +00002255 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 // if slave is null, the whole port is not initialized
2258 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002259 pr_warning("%s: Warning: duplex changed for uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002260 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return;
2262 }
2263
2264 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002265 port->actor_oper_port_key = port->actor_admin_port_key |=
2266 __get_duplex(port);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002267 pr_debug("Port %d changed duplex\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 // there is no need to reselect a new aggregator, just signal the
2269 // state machines to reinitialize
2270 port->sm_vars |= AD_PORT_BEGIN;
2271}
2272
2273/**
2274 * bond_3ad_handle_link_change - handle a slave's link status change indication
2275 * @slave: slave struct to work on
2276 * @status: whether the link is now up or down
2277 *
2278 * Handle reselection of aggregator (if needed) for this port.
2279 */
2280void bond_3ad_handle_link_change(struct slave *slave, char link)
2281{
2282 struct port *port;
2283
2284 port = &(SLAVE_AD_INFO(slave).port);
2285
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("Warning: %s: link status changed for uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002289 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return;
2291 }
2292
2293 // 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)
2294 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2295 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002296 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002298 port->actor_oper_port_key = port->actor_admin_port_key |=
2299 __get_duplex(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002301 port->actor_oper_port_key = port->actor_admin_port_key |=
2302 (__get_link_speed(port) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 } else {
2304 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002305 port->is_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002307 port->actor_oper_port_key = (port->actor_admin_port_key &=
2308 ~AD_SPEED_KEY_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2311 // there is no need to reselect a new aggregator, just signal the
2312 // state machines to reinitialize
2313 port->sm_vars |= AD_PORT_BEGIN;
2314}
2315
Jay Vosburghff59c452006-03-27 13:27:43 -08002316/*
Jasper Spaansa361c832009-10-23 04:09:24 +00002317 * set link state for bonding master: if we have an active
Jay Vosburghff59c452006-03-27 13:27:43 -08002318 * aggregator, we're up, if not, we're down. Presumes that we cannot
2319 * have an active aggregator if there are no slaves with link up.
2320 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002321 * This behavior complies with IEEE 802.3 section 43.3.9.
2322 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002323 * Called by bond_set_carrier(). Return zero if carrier state does not
2324 * change, nonzero if it does.
2325 */
2326int bond_3ad_set_carrier(struct bonding *bond)
2327{
stephen hemminger655f8912011-06-22 09:54:39 +00002328 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002329 struct slave *first_slave;
stephen hemminger655f8912011-06-22 09:54:39 +00002330
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002331 first_slave = bond_first_slave(bond);
2332 if (!first_slave)
2333 return 0;
2334 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave).aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002335 if (active) {
2336 /* are enough slaves available to consider link up? */
2337 if (active->num_of_ports < bond->params.min_links) {
2338 if (netif_carrier_ok(bond->dev)) {
2339 netif_carrier_off(bond->dev);
2340 return 1;
2341 }
2342 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002343 netif_carrier_on(bond->dev);
2344 return 1;
2345 }
2346 return 0;
2347 }
2348
2349 if (netif_carrier_ok(bond->dev)) {
2350 netif_carrier_off(bond->dev);
2351 return 1;
2352 }
2353 return 0;
2354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002357 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 * @bond: bonding struct to work on
2359 * @ad_info: ad_info struct to fill with the bond's info
2360 *
2361 * Returns: 0 on success
2362 * < 0 on error
2363 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002364int __bond_3ad_get_active_agg_info(struct bonding *bond,
2365 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002368 struct list_head *iter;
2369 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct port *port;
2371
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002372 bond_for_each_slave(bond, slave, iter) {
2373 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (port->aggregator && port->aggregator->is_active) {
2375 aggregator = port->aggregator;
2376 break;
2377 }
2378 }
2379
2380 if (aggregator) {
2381 ad_info->aggregator_id = aggregator->aggregator_identifier;
2382 ad_info->ports = aggregator->num_of_ports;
2383 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2384 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2385 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2386 return 0;
2387 }
2388
2389 return -1;
2390}
2391
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002392/* Wrapper used to hold bond->lock so no slave manipulation can occur */
2393int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2394{
2395 int ret;
2396
2397 read_lock(&bond->lock);
2398 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2399 read_unlock(&bond->lock);
2400
2401 return ret;
2402}
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2405{
Wang Chen454d7c92008-11-12 23:37:49 -08002406 struct bonding *bond = netdev_priv(dev);
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002407 struct slave *slave, *first_ok_slave;
2408 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct ad_info ad_info;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002410 struct list_head *iter;
2411 int slaves_in_agg;
2412 int slave_agg_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 int res = 1;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002414 int agg_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002416 read_lock(&bond->lock);
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002417 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
2418 pr_debug("%s: Error: __bond_3ad_get_active_agg_info failed\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002419 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 goto out;
2421 }
2422
2423 slaves_in_agg = ad_info.ports;
2424 agg_id = ad_info.aggregator_id;
2425
2426 if (slaves_in_agg == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002427 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto out;
2429 }
2430
Jasper Spaansa361c832009-10-23 04:09:24 +00002431 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg);
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002432 first_ok_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002434 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002435 agg = SLAVE_AD_INFO(slave).port.aggregator;
2436 if (!agg || agg->aggregator_identifier != agg_id)
2437 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002439 if (slave_agg_no >= 0) {
2440 if (!first_ok_slave && SLAVE_IS_OK(slave))
2441 first_ok_slave = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 slave_agg_no--;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002443 continue;
2444 }
2445
2446 if (SLAVE_IS_OK(slave)) {
2447 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
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
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002458 /* we couldn't find any suitable slave after the agg_no, so use the
2459 * first suitable found, if found. */
2460 if (first_ok_slave)
2461 res = bond_dev_queue_xmit(bond, skb, first_ok_slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463out:
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002464 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (res) {
2466 /* no suitable interface, frame not sent */
Eric Dumazet04502432012-06-13 05:30:07 +00002467 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
Michał Mirosław0693e882011-05-07 01:48:02 +00002469
Patrick McHardyec634fe2009-07-05 19:23:38 -07002470 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Eric Dumazetde063b72012-06-11 19:23:07 +00002473int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2474 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002476 int ret = RX_HANDLER_ANOTHER;
Eric Dumazetde063b72012-06-11 19:23:07 +00002477 struct lacpdu *lacpdu, _lacpdu;
2478
Jiri Pirko3aba8912011-04-19 03:48:16 +00002479 if (skb->protocol != PKT_TYPE_LACPDU)
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002480 return ret;
Neil Hormanb3053252011-01-20 09:02:31 +00002481
Eric Dumazetde063b72012-06-11 19:23:07 +00002482 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2483 if (!lacpdu)
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002484 return ret;
Andy Gospodarekab128112010-09-10 11:43:20 +00002485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 read_lock(&bond->lock);
Eric Dumazetde063b72012-06-11 19:23:07 +00002487 ret = bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 read_unlock(&bond->lock);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002489 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002491
2492/*
2493 * When modify lacp_rate parameter via sysfs,
2494 * update actor_oper_port_state of each port.
2495 *
2496 * Hold slave->state_machine_lock,
2497 * so we can modify port->actor_oper_port_state,
2498 * no matter bond is up or down.
2499 */
2500void bond_3ad_update_lacp_rate(struct bonding *bond)
2501{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002502 struct port *port = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002503 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002504 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002505 int lacp_fast;
2506
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002507 lacp_fast = bond->params.lacp_fast;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002508 bond_for_each_slave(bond, slave, iter) {
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002509 port = &(SLAVE_AD_INFO(slave).port);
2510 __get_state_machine_lock(port);
2511 if (lacp_fast)
2512 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2513 else
2514 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
2515 __release_state_machine_lock(port);
2516 }
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002517}