blob: 8b64ed4722cb021efd4aed8180bee4321614d219 [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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * __get_first_agg - get the first aggregator in the bond
140 * @bond: the bond we're looking at
141 *
142 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
143 * found.
144 */
145static inline struct aggregator *__get_first_agg(struct port *port)
146{
147 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200148 struct slave *first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 // If there's no bond for this port, or bond has no slaves
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200151 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return NULL;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200153 first_slave = bond_first_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200155 return first_slave ? &(SLAVE_AD_INFO(first_slave).aggregator) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/**
159 * __get_next_agg - get the next aggregator in the bond
160 * @aggregator: the aggregator we're looking at
161 *
162 * Return the aggregator of the slave that is next in line of @aggregator's
163 * slave in the bond, or %NULL if it can't be found.
164 */
165static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
166{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200167 struct slave *slave = aggregator->slave, *slave_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct bonding *bond = bond_get_bond_by_slave(slave);
169
170 // If there's no bond for this aggregator, or this is the last slave
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200171 if (bond == NULL)
172 return NULL;
173 slave_next = bond_next_slave(bond, slave);
174 if (!slave_next || bond_is_first_slave(bond, slave_next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200177 return &(SLAVE_AD_INFO(slave_next).aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Jay Vosburghfd989c82008-11-04 17:51:16 -0800180/*
181 * __agg_has_partner
182 *
183 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
184 * address for the partner). Return 0 if not.
185 */
186static inline int __agg_has_partner(struct aggregator *agg)
187{
188 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/**
192 * __disable_port - disable the port's slave
193 * @port: the port we're looking at
194 *
195 */
196static inline void __disable_port(struct port *port)
197{
198 bond_set_slave_inactive_flags(port->slave);
199}
200
201/**
202 * __enable_port - enable the port's slave, if it's up
203 * @port: the port we're looking at
204 *
205 */
206static inline void __enable_port(struct port *port)
207{
208 struct slave *slave = port->slave;
209
Bandan Das7bfc4752010-10-16 20:19:59 +0000210 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 bond_set_slave_active_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/**
215 * __port_is_enabled - check if the port's slave is in active state
216 * @port: the port we're looking at
217 *
218 */
219static inline int __port_is_enabled(struct port *port)
220{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000221 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/**
225 * __get_agg_selection_mode - get the aggregator selection mode
226 * @port: the port we're looking at
227 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800228 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
230static inline u32 __get_agg_selection_mode(struct port *port)
231{
232 struct bonding *bond = __get_bond_by_port(port);
233
Bandan Das7bfc4752010-10-16 20:19:59 +0000234 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800235 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000237 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/**
241 * __check_agg_selection_timer - check if the selection timer has expired
242 * @port: the port we're looking at
243 *
244 */
245static inline int __check_agg_selection_timer(struct port *port)
246{
247 struct bonding *bond = __get_bond_by_port(port);
248
Bandan Das7bfc4752010-10-16 20:19:59 +0000249 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
253}
254
255/**
Nils Carlson9ac35242011-03-03 22:09:12 +0000256 * __get_state_machine_lock - lock the port's state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * @port: the port we're looking at
258 *
259 */
Nils Carlson9ac35242011-03-03 22:09:12 +0000260static inline void __get_state_machine_lock(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Nils Carlson9ac35242011-03-03 22:09:12 +0000262 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/**
Nils Carlson9ac35242011-03-03 22:09:12 +0000266 * __release_state_machine_lock - unlock the port's state machines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * @port: the port we're looking at
268 *
269 */
Nils Carlson9ac35242011-03-03 22:09:12 +0000270static inline void __release_state_machine_lock(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Nils Carlson9ac35242011-03-03 22:09:12 +0000272 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).state_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/**
276 * __get_link_speed - get a port's speed
277 * @port: the port we're looking at
278 *
279 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
280 * 0,
281 * %AD_LINK_SPEED_BITMASK_10MBPS,
282 * %AD_LINK_SPEED_BITMASK_100MBPS,
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700283 * %AD_LINK_SPEED_BITMASK_1000MBPS,
284 * %AD_LINK_SPEED_BITMASK_10000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286static u16 __get_link_speed(struct port *port)
287{
288 struct slave *slave = port->slave;
289 u16 speed;
290
291 /* this if covers only a special case: when the configuration starts with
292 * link down, it sets the speed to 0.
293 * This is done in spite of the fact that the e100 driver reports 0 to be
294 * compatible with MVT in the future.*/
Bandan Das7bfc4752010-10-16 20:19:59 +0000295 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000296 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000297 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 switch (slave->speed) {
299 case SPEED_10:
300 speed = AD_LINK_SPEED_BITMASK_10MBPS;
301 break;
302
303 case SPEED_100:
304 speed = AD_LINK_SPEED_BITMASK_100MBPS;
305 break;
306
307 case SPEED_1000:
308 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
309 break;
310
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700311 case SPEED_10000:
312 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
313 break;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 default:
316 speed = 0; // unknown speed value from ethtool. shouldn't happen
317 break;
318 }
319 }
320
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800321 pr_debug("Port %d Received link speed %d update from adapter\n",
322 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return speed;
324}
325
326/**
327 * __get_duplex - get a port's duplex
328 * @port: the port we're looking at
329 *
330 * Return @port's duplex in 802.3ad bitmask format. i.e.:
331 * 0x01 if in full duplex
332 * 0x00 otherwise
333 */
334static u8 __get_duplex(struct port *port)
335{
336 struct slave *slave = port->slave;
337
338 u8 retval;
339
340 // handling a special case: when the configuration starts with
341 // link down, it sets the duplex to 0.
Bandan Das7bfc4752010-10-16 20:19:59 +0000342 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000343 retval = 0x0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000344 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 switch (slave->duplex) {
346 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000347 retval = 0x1;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800348 pr_debug("Port %d Received status full duplex update from adapter\n",
349 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 case DUPLEX_HALF:
352 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000353 retval = 0x0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800354 pr_debug("Port %d Received status NOT full duplex update from adapter\n",
355 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
358 }
359 return retval;
360}
361
362/**
Nils Carlson9ac35242011-03-03 22:09:12 +0000363 * __initialize_port_locks - initialize a port's STATE machine spinlock
nikolay@redhat.come0809db2013-02-18 07:59:03 +0000364 * @port: the slave of the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 */
nikolay@redhat.come0809db2013-02-18 07:59:03 +0000367static inline void __initialize_port_locks(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 // make sure it isn't called twice
nikolay@redhat.come0809db2013-02-18 07:59:03 +0000370 spin_lock_init(&(SLAVE_AD_INFO(slave).state_machine_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373//conversions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375/**
376 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
377 * @timer_type: which timer to operate
378 * @par: timer parameter. see below
379 *
380 * If @timer_type is %current_while_timer, @par indicates long/short timer.
381 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
382 * %SLOW_PERIODIC_TIME.
383 */
384static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
385{
Bandan Das128ea6c2010-10-16 20:19:58 +0000386 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 switch (timer_type) {
389 case AD_CURRENT_WHILE_TIMER: // for rx machine usage
Bandan Das7bfc4752010-10-16 20:19:59 +0000390 if (par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
Bandan Das7bfc4752010-10-16 20:19:59 +0000392 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
395 case AD_ACTOR_CHURN_TIMER: // for local churn machine
396 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
397 break;
398 case AD_PERIODIC_TIMER: // for periodic machine
399 retval = (par*ad_ticks_per_sec); // long timeout
400 break;
401 case AD_PARTNER_CHURN_TIMER: // for remote churn machine
402 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
403 break;
404 case AD_WAIT_WHILE_TIMER: // for selection machine
405 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
406 break;
407 }
408 return retval;
409}
410
411
412/////////////////////////////////////////////////////////////////////////////////
413// ================= ad_rx_machine helper functions ==================
414/////////////////////////////////////////////////////////////////////////////////
415
416/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000417 * __choose_matched - update a port's matched variable from a received lacpdu
418 * @lacpdu: the lacpdu we've received
419 * @port: the port we're looking at
420 *
421 * Update the value of the matched variable, using parameter values from a
422 * newly received lacpdu. Parameter values for the partner carried in the
423 * received PDU are compared with the corresponding operational parameter
424 * values for the actor. Matched is set to TRUE if all of these parameters
425 * match and the PDU parameter partner_state.aggregation has the same value as
426 * actor_oper_port_state.aggregation and lacp will actively maintain the link
427 * in the aggregation. Matched is also set to TRUE if the value of
428 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
429 * an individual link and lacp will actively maintain the link. Otherwise,
430 * matched is set to FALSE. LACP is considered to be actively maintaining the
431 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
432 * the actor's actor_oper_port_state.lacp_activity and the PDU's
433 * partner_state.lacp_activity variables are TRUE.
434 *
435 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
436 * used here to implement the language from 802.3ad 43.4.9 that requires
437 * recordPDU to "match" the LACPDU parameters to the stored values.
438 */
439static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
440{
441 // check if all parameters are alike
442 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
443 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
444 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
445 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
446 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
447 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
448 // or this is individual link(aggregation == FALSE)
449 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
450 ) {
451 // update the state machine Matched variable
452 port->sm_vars |= AD_PORT_MATCHED;
453 } else {
454 port->sm_vars &= ~AD_PORT_MATCHED;
455 }
456}
457
458/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * __record_pdu - record parameters from a received lacpdu
460 * @lacpdu: the lacpdu we've received
461 * @port: the port we're looking at
462 *
463 * Record the parameter values for the Actor carried in a received lacpdu as
464 * the current partner operational parameter values and sets
465 * actor_oper_port_state.defaulted to FALSE.
466 */
467static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800470 struct port_params *partner = &port->partner_oper;
471
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000472 __choose_matched(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 // record the new parameter values for the partner operational
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800474 partner->port_number = ntohs(lacpdu->actor_port);
475 partner->port_priority = ntohs(lacpdu->actor_port_priority);
476 partner->system = lacpdu->actor_system;
477 partner->system_priority = ntohs(lacpdu->actor_system_priority);
478 partner->key = ntohs(lacpdu->actor_key);
479 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 // set actor_oper_port_state.defaulted to FALSE
482 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
483
484 // set the partner sync. to on if the partner is sync. and the port is matched
Bandan Das7bfc4752010-10-16 20:19:59 +0000485 if ((port->sm_vars & AD_PORT_MATCHED)
486 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800487 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Bandan Das7bfc4752010-10-16 20:19:59 +0000488 else
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800489 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491}
492
493/**
494 * __record_default - record default parameters
495 * @port: the port we're looking at
496 *
497 * This function records the default parameter values for the partner carried
498 * in the Partner Admin parameters as the current partner operational parameter
499 * values and sets actor_oper_port_state.defaulted to TRUE.
500 */
501static void __record_default(struct port *port)
502{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (port) {
504 // record the partner admin parameters
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800505 memcpy(&port->partner_oper, &port->partner_admin,
506 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 // set actor_oper_port_state.defaulted to true
509 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
510 }
511}
512
513/**
514 * __update_selected - update a port's Selected variable from a received lacpdu
515 * @lacpdu: the lacpdu we've received
516 * @port: the port we're looking at
517 *
518 * Update the value of the selected variable, using parameter values from a
519 * newly received lacpdu. The parameter values for the Actor carried in the
520 * received PDU are compared with the corresponding operational parameter
521 * values for the ports partner. If one or more of the comparisons shows that
522 * the value(s) received in the PDU differ from the current operational values,
523 * then selected is set to FALSE and actor_oper_port_state.synchronization is
524 * set to out_of_sync. Otherwise, selected remains unchanged.
525 */
526static void __update_selected(struct lacpdu *lacpdu, struct port *port)
527{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800529 const struct port_params *partner = &port->partner_oper;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 // check if any parameter is different
Joe Perches8e95a202009-12-03 07:58:21 +0000532 if (ntohs(lacpdu->actor_port) != partner->port_number ||
533 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
534 MAC_ADDRESS_COMPARE(&lacpdu->actor_system, &partner->system) ||
535 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
536 ntohs(lacpdu->actor_key) != partner->key ||
537 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 // update the state machine Selected variable
539 port->sm_vars &= ~AD_PORT_SELECTED;
540 }
541 }
542}
543
544/**
545 * __update_default_selected - update a port's Selected variable from Partner
546 * @port: the port we're looking at
547 *
548 * This function updates the value of the selected variable, using the partner
549 * administrative parameter values. The administrative values are compared with
550 * the corresponding operational parameter values for the partner. If one or
551 * more of the comparisons shows that the administrative value(s) differ from
552 * the current operational values, then Selected is set to FALSE and
553 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
554 * Selected remains unchanged.
555 */
556static void __update_default_selected(struct port *port)
557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800559 const struct port_params *admin = &port->partner_admin;
560 const struct port_params *oper = &port->partner_oper;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 // check if any parameter is different
Joe Perches8e95a202009-12-03 07:58:21 +0000563 if (admin->port_number != oper->port_number ||
564 admin->port_priority != oper->port_priority ||
565 MAC_ADDRESS_COMPARE(&admin->system, &oper->system) ||
566 admin->system_priority != oper->system_priority ||
567 admin->key != oper->key ||
568 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800569 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 // update the state machine Selected variable
571 port->sm_vars &= ~AD_PORT_SELECTED;
572 }
573 }
574}
575
576/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * __update_ntt - update a port's ntt variable from a received lacpdu
578 * @lacpdu: the lacpdu we've received
579 * @port: the port we're looking at
580 *
581 * Updates the value of the ntt variable, using parameter values from a newly
582 * received lacpdu. The parameter values for the partner carried in the
583 * received PDU are compared with the corresponding operational parameter
584 * values for the Actor. If one or more of the comparisons shows that the
585 * value(s) received in the PDU differ from the current operational values,
586 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
587 */
588static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
589{
590 // validate lacpdu and port
591 if (lacpdu && port) {
592 // check if any parameter is different
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700593 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
594 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700596 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
597 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
599 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
600 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
601 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
602 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800603
604 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 }
607}
608
609/**
610 * __attach_bond_to_agg
611 * @port: the port we're looking at
612 *
613 * Handle the attaching of the port's control parser/multiplexer and the
614 * aggregator. This function does nothing since the parser/multiplexer of the
615 * receive and the parser/multiplexer of the aggregator are already combined.
616 */
617static void __attach_bond_to_agg(struct port *port)
618{
Bandan Das128ea6c2010-10-16 20:19:58 +0000619 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 // This function does nothing since the parser/multiplexer of the receive
621 // and the parser/multiplexer of the aggregator are already combined
622}
623
624/**
625 * __detach_bond_from_agg
626 * @port: the port we're looking at
627 *
628 * Handle the detaching of the port's control parser/multiplexer from the
629 * aggregator. This function does nothing since the parser/multiplexer of the
630 * receive and the parser/multiplexer of the aggregator are already combined.
631 */
632static void __detach_bond_from_agg(struct port *port)
633{
Bandan Das128ea6c2010-10-16 20:19:58 +0000634 port = NULL; /* just to satisfy the compiler */
Jesper Juhlb9d6d2d2012-02-05 13:18:57 +0000635 // This function does nothing since the parser/multiplexer of the receive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 // and the parser/multiplexer of the aggregator are already combined
637}
638
639/**
640 * __agg_ports_are_ready - check if all ports in an aggregator are ready
641 * @aggregator: the aggregator we're looking at
642 *
643 */
644static int __agg_ports_are_ready(struct aggregator *aggregator)
645{
646 struct port *port;
647 int retval = 1;
648
649 if (aggregator) {
650 // scan all ports in this aggregator to verfy if they are all ready
Bandan Das128ea6c2010-10-16 20:19:58 +0000651 for (port = aggregator->lag_ports;
652 port;
653 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!(port->sm_vars & AD_PORT_READY_N)) {
655 retval = 0;
656 break;
657 }
658 }
659 }
660
661 return retval;
662}
663
664/**
665 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
666 * @aggregator: the aggregator we're looking at
667 * @val: Should the ports' ready bit be set on or off
668 *
669 */
670static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
671{
672 struct port *port;
673
Bandan Das128ea6c2010-10-16 20:19:58 +0000674 for (port = aggregator->lag_ports; port;
675 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000676 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000678 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681}
682
683/**
684 * __get_agg_bandwidth - get the total bandwidth of an aggregator
685 * @aggregator: the aggregator we're looking at
686 *
687 */
688static u32 __get_agg_bandwidth(struct aggregator *aggregator)
689{
Bandan Das128ea6c2010-10-16 20:19:58 +0000690 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (aggregator->num_of_ports) {
David Decotigny65cce192011-04-13 15:22:30 +0000693 switch (__get_link_speed(aggregator->lag_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case AD_LINK_SPEED_BITMASK_1MBPS:
695 bandwidth = aggregator->num_of_ports;
696 break;
697 case AD_LINK_SPEED_BITMASK_10MBPS:
698 bandwidth = aggregator->num_of_ports * 10;
699 break;
700 case AD_LINK_SPEED_BITMASK_100MBPS:
701 bandwidth = aggregator->num_of_ports * 100;
702 break;
703 case AD_LINK_SPEED_BITMASK_1000MBPS:
704 bandwidth = aggregator->num_of_ports * 1000;
705 break;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700706 case AD_LINK_SPEED_BITMASK_10000MBPS:
707 bandwidth = aggregator->num_of_ports * 10000;
708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000710 bandwidth = 0; /*to silence the compiler ....*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 }
713 return bandwidth;
714}
715
716/**
717 * __get_active_agg - get the current active aggregator
718 * @aggregator: the aggregator we're looking at
719 *
720 */
721static struct aggregator *__get_active_agg(struct aggregator *aggregator)
722{
723 struct aggregator *retval = NULL;
724
725 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
726 if (aggregator->is_active) {
727 retval = aggregator;
728 break;
729 }
730 }
731
732 return retval;
733}
734
735/**
736 * __update_lacpdu_from_port - update a port's lacpdu fields
737 * @port: the port we're looking at
738 *
739 */
740static inline void __update_lacpdu_from_port(struct port *port)
741{
742 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800743 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /* update current actual Actor parameters */
746 /* lacpdu->subtype initialized
747 * lacpdu->version_number initialized
748 * lacpdu->tlv_type_actor_info initialized
749 * lacpdu->actor_information_length initialized
750 */
751
Al Virod3bb52b2007-08-22 20:06:58 -0400752 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400754 lacpdu->actor_key = htons(port->actor_oper_port_key);
755 lacpdu->actor_port_priority = htons(port->actor_port_priority);
756 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 lacpdu->actor_state = port->actor_oper_port_state;
758
759 /* lacpdu->reserved_3_1 initialized
760 * lacpdu->tlv_type_partner_info initialized
761 * lacpdu->partner_information_length initialized
762 */
763
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800764 lacpdu->partner_system_priority = htons(partner->system_priority);
765 lacpdu->partner_system = partner->system;
766 lacpdu->partner_key = htons(partner->key);
767 lacpdu->partner_port_priority = htons(partner->port_priority);
768 lacpdu->partner_port = htons(partner->port_number);
769 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* lacpdu->reserved_3_2 initialized
772 * lacpdu->tlv_type_collector_info initialized
773 * lacpdu->collector_information_length initialized
774 * collector_max_delay initialized
775 * reserved_12[12] initialized
776 * tlv_type_terminator initialized
777 * terminator_length initialized
778 * reserved_50[50] initialized
779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782//////////////////////////////////////////////////////////////////////////////////////
783// ================= main 802.3ad protocol code ======================================
784//////////////////////////////////////////////////////////////////////////////////////
785
786/**
787 * ad_lacpdu_send - send out a lacpdu packet on a given port
788 * @port: the port we're looking at
789 *
790 * Returns: 0 on success
791 * < 0 on error
792 */
793static int ad_lacpdu_send(struct port *port)
794{
795 struct slave *slave = port->slave;
796 struct sk_buff *skb;
797 struct lacpdu_header *lacpdu_header;
798 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000801 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700805 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700806 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 skb->protocol = PKT_TYPE_LACPDU;
808 skb->priority = TC_PRIO_CONTROL;
809
810 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
811
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800812 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400813 /* Note: source address is set to be the member's PERMANENT address,
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800814 because we use it to identify loopback lacpdus in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800815 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
816 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 lacpdu_header->lacpdu = port->lacpdu; // struct copy
819
820 dev_queue_xmit(skb);
821
822 return 0;
823}
824
825/**
826 * ad_marker_send - send marker information/response on a given port
827 * @port: the port we're looking at
828 * @marker: marker data to send
829 *
830 * Returns: 0 on success
831 * < 0 on error
832 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700833static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct slave *slave = port->slave;
836 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700837 struct bond_marker_header *marker_header;
838 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000841 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 skb_reserve(skb, 16);
845
846 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700847 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700848 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 skb->protocol = PKT_TYPE_LACPDU;
850
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700851 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800853 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400854 /* Note: source address is set to be the member's PERMANENT address,
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800855 because we use it to identify loopback MARKERs in receive. */
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800856 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN);
857 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 marker_header->marker = *marker; // struct copy
860
861 dev_queue_xmit(skb);
862
863 return 0;
864}
865
866/**
867 * ad_mux_machine - handle a port's mux state machine
868 * @port: the port we're looking at
869 *
870 */
871static void ad_mux_machine(struct port *port)
872{
873 mux_states_t last_state;
874
875 // keep current State Machine state to compare later if it was changed
876 last_state = port->sm_mux_state;
877
878 if (port->sm_vars & AD_PORT_BEGIN) {
879 port->sm_mux_state = AD_MUX_DETACHED; // next state
880 } else {
881 switch (port->sm_mux_state) {
882 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000883 if ((port->sm_vars & AD_PORT_SELECTED)
884 || (port->sm_vars & AD_PORT_STANDBY))
885 /* if SELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 port->sm_mux_state = AD_MUX_WAITING; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
888 case AD_MUX_WAITING:
889 // if SELECTED == FALSE return to DETACH state
890 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
891 port->sm_vars &= ~AD_PORT_READY_N;
892 // in order to withhold the Selection Logic to check all ports READY_N value
893 // every callback cycle to update ready variable, we check READY_N and update READY here
894 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
895 port->sm_mux_state = AD_MUX_DETACHED; // next state
896 break;
897 }
898
899 // check if the wait_while_timer expired
Bandan Das7bfc4752010-10-16 20:19:59 +0000900 if (port->sm_mux_timer_counter
901 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 // in order to withhold the selection logic to check all ports READY_N value
905 // every callback cycle to update ready variable, we check READY_N and update READY here
906 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
907
908 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
Bandan Das7bfc4752010-10-16 20:19:59 +0000909 if ((port->sm_vars & AD_PORT_READY)
910 && !port->sm_mux_timer_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 port->sm_mux_state = AD_MUX_ATTACHED; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
913 case AD_MUX_ATTACHED:
914 // 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 -0800915 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 -0700916 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
917 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
918 port->sm_vars &= ~AD_PORT_READY_N;
919 // in order to withhold the selection logic to check all ports READY_N value
920 // every callback cycle to update ready variable, we check READY_N and update READY here
921 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
922 port->sm_mux_state = AD_MUX_DETACHED;// next state
923 }
924 break;
925 case AD_MUX_COLLECTING_DISTRIBUTING:
926 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -0800927 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 ) {
929 port->sm_mux_state = AD_MUX_ATTACHED;// next state
930
931 } else {
932 // if port state hasn't changed make
933 // sure that a collecting distributing
934 // port in an active aggregator is enabled
935 if (port->aggregator &&
936 port->aggregator->is_active &&
937 !__port_is_enabled(port)) {
938
939 __enable_port(port);
940 }
941 }
942 break;
943 default: //to silence the compiler
944 break;
945 }
946 }
947
948 // check if the state machine was changed
949 if (port->sm_mux_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800950 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
951 port->actor_port_number, last_state,
952 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 switch (port->sm_mux_state) {
954 case AD_MUX_DETACHED:
955 __detach_bond_from_agg(port);
956 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
957 ad_disable_collecting_distributing(port);
958 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
959 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800960 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
962 case AD_MUX_WAITING:
963 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
964 break;
965 case AD_MUX_ATTACHED:
966 __attach_bond_to_agg(port);
967 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
968 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
969 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
970 ad_disable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800971 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973 case AD_MUX_COLLECTING_DISTRIBUTING:
974 port->actor_oper_port_state |= AD_STATE_COLLECTING;
975 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
976 ad_enable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800977 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
979 default: //to silence the compiler
980 break;
981 }
982 }
983}
984
985/**
986 * ad_rx_machine - handle a port's rx State Machine
987 * @lacpdu: the lacpdu we've received
988 * @port: the port we're looking at
989 *
990 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
991 * CURRENT. If timer expired set the state machine in the proper state.
992 * In other cases, this function checks if we need to switch to other state.
993 */
994static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
995{
996 rx_states_t last_state;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 // keep current State Machine state to compare later if it was changed
999 last_state = port->sm_rx_state;
1000
1001 // check if state machine should change state
1002 // first, check if port was reinitialized
Bandan Das7bfc4752010-10-16 20:19:59 +00001003 if (port->sm_vars & AD_PORT_BEGIN)
1004 /* next state */
1005 port->sm_rx_state = AD_RX_INITIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 // check if port is not enabled
Bandan Das7bfc4752010-10-16 20:19:59 +00001007 else if (!(port->sm_vars & AD_PORT_BEGIN)
1008 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
1009 /* next state */
1010 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 // check if new lacpdu arrived
1012 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
1013 port->sm_rx_timer_counter = 0; // zero timer
1014 port->sm_rx_state = AD_RX_CURRENT;
1015 } else {
1016 // if timer is on, and if it is expired
1017 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
1018 switch (port->sm_rx_state) {
1019 case AD_RX_EXPIRED:
1020 port->sm_rx_state = AD_RX_DEFAULTED; // next state
1021 break;
1022 case AD_RX_CURRENT:
1023 port->sm_rx_state = AD_RX_EXPIRED; // next state
1024 break;
1025 default: //to silence the compiler
1026 break;
1027 }
1028 } else {
1029 // if no lacpdu arrived and no timer is on
1030 switch (port->sm_rx_state) {
1031 case AD_RX_PORT_DISABLED:
Bandan Das7bfc4752010-10-16 20:19:59 +00001032 if (port->sm_vars & AD_PORT_MOVED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 port->sm_rx_state = AD_RX_INITIALIZE; // next state
Bandan Das7bfc4752010-10-16 20:19:59 +00001034 else if (port->is_enabled
1035 && (port->sm_vars
1036 & AD_PORT_LACP_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 port->sm_rx_state = AD_RX_EXPIRED; // next state
Bandan Das7bfc4752010-10-16 20:19:59 +00001038 else if (port->is_enabled
1039 && ((port->sm_vars
1040 & AD_PORT_LACP_ENABLED) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 break;
1043 default: //to silence the compiler
1044 break;
1045
1046 }
1047 }
1048 }
1049
1050 // check if the State machine was changed or new lacpdu arrived
1051 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001052 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1053 port->actor_port_number, last_state,
1054 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 switch (port->sm_rx_state) {
1056 case AD_RX_INITIALIZE:
Bandan Das7bfc4752010-10-16 20:19:59 +00001057 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001059 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 port->sm_vars &= ~AD_PORT_SELECTED;
1062 __record_default(port);
1063 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1064 port->sm_vars &= ~AD_PORT_MOVED;
1065 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
1066
1067 /*- Fall Through -*/
1068
1069 case AD_RX_PORT_DISABLED:
1070 port->sm_vars &= ~AD_PORT_MATCHED;
1071 break;
1072 case AD_RX_LACP_DISABLED:
1073 port->sm_vars &= ~AD_PORT_SELECTED;
1074 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001075 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 port->sm_vars |= AD_PORT_MATCHED;
1077 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1078 break;
1079 case AD_RX_EXPIRED:
1080 //Reset of the Synchronization flag. (Standard 43.4.12)
1081 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
1082 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001083 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 port->sm_vars &= ~AD_PORT_MATCHED;
Julia Lawall8e321c42009-07-11 10:03:55 +00001085 port->partner_oper.port_state |=
1086 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1088 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1089 break;
1090 case AD_RX_DEFAULTED:
1091 __update_default_selected(port);
1092 __record_default(port);
1093 port->sm_vars |= AD_PORT_MATCHED;
1094 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1095 break;
1096 case AD_RX_CURRENT:
1097 // detect loopback situation
1098 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
1099 // INFO_RECEIVED_LOOPBACK_FRAMES
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001100 pr_err("%s: An illegal loopback occurred on adapter (%s).\n"
1101 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001102 port->slave->bond->dev->name, port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
1105 __update_selected(lacpdu, port);
1106 __update_ntt(lacpdu, port);
1107 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1109 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111 default: //to silence the compiler
1112 break;
1113 }
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/**
1118 * ad_tx_machine - handle a port's tx state machine
1119 * @port: the port we're looking at
1120 *
1121 */
1122static void ad_tx_machine(struct port *port)
1123{
1124 // check if tx timer expired, to verify that we do not send more than 3 packets per second
1125 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1126 // check if there is something to send
1127 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1128 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001131 pr_debug("Sent LACPDU on port %d\n",
1132 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001133
1134 /* mark ntt as false, so it will not be sent again until
1135 demanded */
1136 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
Bandan Das128ea6c2010-10-16 20:19:58 +00001140 port->sm_tx_timer_counter =
1141 ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143}
1144
1145/**
1146 * ad_periodic_machine - handle a port's periodic state machine
1147 * @port: the port we're looking at
1148 *
1149 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1150 */
1151static void ad_periodic_machine(struct port *port)
1152{
1153 periodic_states_t last_state;
1154
1155 // keep current state machine state to compare later if it was changed
1156 last_state = port->sm_periodic_state;
1157
1158 // check if port was reinitialized
1159 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001160 (!(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 -07001161 ) {
1162 port->sm_periodic_state = AD_NO_PERIODIC; // next state
1163 }
1164 // check if state machine should change state
1165 else if (port->sm_periodic_timer_counter) {
1166 // check if periodic state machine expired
1167 if (!(--port->sm_periodic_timer_counter)) {
1168 // if expired then do tx
1169 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1170 } else {
1171 // If not expired, check if there is some new timeout parameter from the partner state
1172 switch (port->sm_periodic_state) {
1173 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001174 if (!(port->partner_oper.port_state
1175 & AD_STATE_LACP_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
1178 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001179 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 // stop current timer
1181 port->sm_periodic_timer_counter = 0;
1182 port->sm_periodic_state = AD_PERIODIC_TX; // next state
1183 }
1184 break;
1185 default: //to silence the compiler
1186 break;
1187 }
1188 }
1189 } else {
1190 switch (port->sm_periodic_state) {
1191 case AD_NO_PERIODIC:
1192 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
1193 break;
1194 case AD_PERIODIC_TX:
Bandan Das7bfc4752010-10-16 20:19:59 +00001195 if (!(port->partner_oper.port_state
1196 & AD_STATE_LACP_TIMEOUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
Bandan Das7bfc4752010-10-16 20:19:59 +00001198 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 port->sm_periodic_state = AD_FAST_PERIODIC; // next state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201 default: //to silence the compiler
1202 break;
1203 }
1204 }
1205
1206 // check if the state machine was changed
1207 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001208 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1209 port->actor_port_number, last_state,
1210 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 switch (port->sm_periodic_state) {
1212 case AD_NO_PERIODIC:
1213 port->sm_periodic_timer_counter = 0; // zero timer
1214 break;
1215 case AD_FAST_PERIODIC:
1216 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
1217 break;
1218 case AD_SLOW_PERIODIC:
1219 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
1220 break;
1221 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001222 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
1224 default: //to silence the compiler
1225 break;
1226 }
1227 }
1228}
1229
1230/**
1231 * ad_port_selection_logic - select aggregation groups
1232 * @port: the port we're looking at
1233 *
1234 * Select aggregation groups, and assign each port for it's aggregetor. The
1235 * selection logic is called in the inititalization (after all the handshkes),
1236 * and after every lacpdu receive (if selected is off).
1237 */
1238static void ad_port_selection_logic(struct port *port)
1239{
1240 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1241 struct port *last_port = NULL, *curr_port;
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001242 struct list_head *iter;
1243 struct bonding *bond;
1244 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int found = 0;
1246
1247 // if the port is already Selected, do nothing
Bandan Das7bfc4752010-10-16 20:19:59 +00001248 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001251 bond = __get_bond_by_port(port);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 // if the port is connected to other aggregator, detach it
1254 if (port->aggregator) {
1255 // detach the port from its former aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001256 temp_aggregator = port->aggregator;
1257 for (curr_port = temp_aggregator->lag_ports; curr_port;
1258 last_port = curr_port,
1259 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (curr_port == port) {
1261 temp_aggregator->num_of_ports--;
1262 if (!last_port) {// if it is the first port attached to the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001263 temp_aggregator->lag_ports =
1264 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 } else {// not the first port attached to the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00001266 last_port->next_port_in_aggregator =
1267 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
1270 // clear the port's relations to this aggregator
1271 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001272 port->next_port_in_aggregator = NULL;
1273 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001275 pr_debug("Port %d left LAG %d\n",
1276 port->actor_port_number,
1277 temp_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 // if the aggregator is empty, clear its parameters, and set it ready to be attached
Bandan Das7bfc4752010-10-16 20:19:59 +00001279 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 }
1283 }
1284 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 -08001285 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 +00001286 port->slave->bond->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001287 port->actor_port_number,
1288 port->slave->dev->name,
1289 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 }
1292 // search on all aggregators for a suitable aggregator for this port
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001293 bond_for_each_slave(bond, slave, iter) {
1294 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 // keep a free aggregator for later use(if needed)
1297 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001298 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001299 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 continue;
1301 }
1302 // check if current aggregator suits us
1303 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001304 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1305 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1306 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001308 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 !aggregator->is_individual) // but is not individual OR
1310 )
1311 ) {
1312 // attach to the founded aggregator
1313 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001314 port->actor_port_aggregator_identifier =
1315 port->aggregator->aggregator_identifier;
1316 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001318 aggregator->lag_ports = port;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001319 pr_debug("Port %d joined LAG %d(existing LAG)\n",
1320 port->actor_port_number,
1321 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 // mark this port as selected
1324 port->sm_vars |= AD_PORT_SELECTED;
1325 found = 1;
1326 break;
1327 }
1328 }
1329
1330 // the port couldn't find an aggregator - attach it to a new aggregator
1331 if (!found) {
1332 if (free_aggregator) {
1333 // assign port a new aggregator
1334 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001335 port->actor_port_aggregator_identifier =
1336 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 // update the new aggregator's parameters
1339 // if port was responsed from the end-user
Bandan Das7bfc4752010-10-16 20:19:59 +00001340 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)
1341 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001342 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001343 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001344 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1347 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001348 port->aggregator->partner_system =
1349 port->partner_oper.system;
1350 port->aggregator->partner_system_priority =
1351 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001352 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 port->aggregator->receive_state = 1;
1354 port->aggregator->transmit_state = 1;
1355 port->aggregator->lag_ports = port;
1356 port->aggregator->num_of_ports++;
1357
1358 // mark this port as selected
1359 port->sm_vars |= AD_PORT_SELECTED;
1360
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001361 pr_debug("Port %d joined LAG %d(new LAG)\n",
1362 port->actor_port_number,
1363 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001365 pr_err("%s: Port %d (on %s) did not find a suitable aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001366 port->slave->bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 port->actor_port_number, port->slave->dev->name);
1368 }
1369 }
1370 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
1371 // else set ready=FALSE in all aggregator's ports
1372 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1373
Jay Vosburghfd989c82008-11-04 17:51:16 -08001374 aggregator = __get_first_agg(port);
1375 ad_agg_selection_logic(aggregator);
1376}
1377
1378/*
1379 * Decide if "agg" is a better choice for the new active aggregator that
1380 * the current best, according to the ad_select policy.
1381 */
1382static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1383 struct aggregator *curr)
1384{
1385 /*
1386 * 0. If no best, select current.
1387 *
1388 * 1. If the current agg is not individual, and the best is
1389 * individual, select current.
1390 *
1391 * 2. If current agg is individual and the best is not, keep best.
1392 *
1393 * 3. Therefore, current and best are both individual or both not
1394 * individual, so:
1395 *
1396 * 3a. If current agg partner replied, and best agg partner did not,
1397 * select current.
1398 *
1399 * 3b. If current agg partner did not reply and best agg partner
1400 * did reply, keep best.
1401 *
1402 * 4. Therefore, current and best both have partner replies or
1403 * both do not, so perform selection policy:
1404 *
1405 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1406 * select by bandwidth.
1407 *
1408 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1409 */
1410 if (!best)
1411 return curr;
1412
1413 if (!curr->is_individual && best->is_individual)
1414 return curr;
1415
1416 if (curr->is_individual && !best->is_individual)
1417 return best;
1418
1419 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1420 return curr;
1421
1422 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1423 return best;
1424
1425 switch (__get_agg_selection_mode(curr->lag_ports)) {
1426 case BOND_AD_COUNT:
1427 if (curr->num_of_ports > best->num_of_ports)
1428 return curr;
1429
1430 if (curr->num_of_ports < best->num_of_ports)
1431 return best;
1432
1433 /*FALLTHROUGH*/
1434 case BOND_AD_STABLE:
1435 case BOND_AD_BANDWIDTH:
1436 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1437 return curr;
1438
1439 break;
1440
1441 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001442 pr_warning("%s: Impossible agg select mode %d\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001443 curr->slave->bond->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001444 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001447
1448 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001451static int agg_device_up(const struct aggregator *agg)
1452{
Jiri Bohac2430af82011-04-19 02:09:55 +00001453 struct port *port = agg->lag_ports;
1454 if (!port)
1455 return 0;
1456 return (netif_running(port->slave->dev) &&
1457 netif_carrier_ok(port->slave->dev));
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/**
1461 * ad_agg_selection_logic - select an aggregation group for a team
1462 * @aggregator: the aggregator we're looking at
1463 *
1464 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001465 *
1466 * The logic of this function is to select the aggregator according to
1467 * the ad_select policy:
1468 *
1469 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1470 * it, and to reselect the active aggregator only if the previous
1471 * aggregator has no more ports related to it.
1472 *
1473 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1474 * bandwidth, and reselect whenever a link state change takes place or the
1475 * set of slaves in the bond changes.
1476 *
1477 * BOND_AD_COUNT: select the aggregator with largest number of ports
1478 * (slaves), and reselect whenever a link state change takes place or the
1479 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 *
1481 * FIXME: this function MUST be called with the first agg in the bond, or
1482 * __get_active_agg() won't work correctly. This function should be better
1483 * called with the bond itself, and retrieve the first agg from it.
1484 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001485static void ad_agg_selection_logic(struct aggregator *agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001487 struct aggregator *best, *active, *origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Jay Vosburghfd989c82008-11-04 17:51:16 -08001490 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001491 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001492 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 do {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001495 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001497 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001498 best = ad_agg_selection_test(best, agg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jay Vosburghfd989c82008-11-04 17:51:16 -08001500 } while ((agg = __get_next_agg(agg)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Jay Vosburghfd989c82008-11-04 17:51:16 -08001502 if (best &&
1503 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1504 /*
1505 * For the STABLE policy, don't replace the old active
1506 * aggregator if it's still active (it has an answering
1507 * partner) or if both the best and active don't have an
1508 * answering partner.
1509 */
1510 if (active && active->lag_ports &&
1511 active->lag_ports->is_enabled &&
1512 (__agg_has_partner(active) ||
1513 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1514 if (!(!active->actor_oper_aggregator_key &&
1515 best->actor_oper_aggregator_key)) {
1516 best = NULL;
1517 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 }
1520 }
1521
Jay Vosburghfd989c82008-11-04 17:51:16 -08001522 if (best && (best == active)) {
1523 best = NULL;
1524 active->is_active = 1;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Jay Vosburghfd989c82008-11-04 17:51:16 -08001527 // if there is new best aggregator, activate it
1528 if (best) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001529 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 -08001530 best->aggregator_identifier, best->num_of_ports,
1531 best->actor_oper_aggregator_key,
1532 best->partner_oper_aggregator_key,
1533 best->is_individual, best->is_active);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001534 pr_debug("best ports %p slave %p %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001535 best->lag_ports, best->slave,
1536 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001537
1538 for (agg = __get_first_agg(best->lag_ports); agg;
1539 agg = __get_next_agg(agg)) {
1540
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001541 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001542 agg->aggregator_identifier, agg->num_of_ports,
1543 agg->actor_oper_aggregator_key,
1544 agg->partner_oper_aggregator_key,
1545 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547
1548 // check if any partner replys
Jay Vosburghfd989c82008-11-04 17:51:16 -08001549 if (best->is_individual) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001550 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 +00001551 best->slave ? best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
Jay Vosburghfd989c82008-11-04 17:51:16 -08001554 best->is_active = 1;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001555 pr_debug("LAG %d chosen as the active LAG\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001556 best->aggregator_identifier);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001557 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001558 best->aggregator_identifier, best->num_of_ports,
1559 best->actor_oper_aggregator_key,
1560 best->partner_oper_aggregator_key,
1561 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 // disable the ports that were related to the former active_aggregator
Jay Vosburghfd989c82008-11-04 17:51:16 -08001564 if (active) {
1565 for (port = active->lag_ports; port;
1566 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 __disable_port(port);
1568 }
1569 }
1570 }
1571
Jay Vosburghfd989c82008-11-04 17:51:16 -08001572 /*
1573 * if the selected aggregator is of join individuals
1574 * (partner_system is NULL), enable their ports
1575 */
1576 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jay Vosburghfd989c82008-11-04 17:51:16 -08001578 if (active) {
1579 if (!__agg_has_partner(active)) {
1580 for (port = active->lag_ports; port;
1581 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 __enable_port(port);
1583 }
1584 }
1585 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001586
1587 if (origin->slave) {
1588 struct bonding *bond;
1589
1590 bond = bond_get_bond_by_slave(origin->slave);
1591 if (bond)
1592 bond_3ad_set_carrier(bond);
1593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596/**
1597 * ad_clear_agg - clear a given aggregator's parameters
1598 * @aggregator: the aggregator we're looking at
1599 *
1600 */
1601static void ad_clear_agg(struct aggregator *aggregator)
1602{
1603 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001604 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 aggregator->actor_admin_aggregator_key = 0;
1606 aggregator->actor_oper_aggregator_key = 0;
1607 aggregator->partner_system = null_mac_addr;
1608 aggregator->partner_system_priority = 0;
1609 aggregator->partner_oper_aggregator_key = 0;
1610 aggregator->receive_state = 0;
1611 aggregator->transmit_state = 0;
1612 aggregator->lag_ports = NULL;
1613 aggregator->is_active = 0;
1614 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001615 pr_debug("LAG %d was cleared\n",
1616 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618}
1619
1620/**
1621 * ad_initialize_agg - initialize a given aggregator's parameters
1622 * @aggregator: the aggregator we're looking at
1623 *
1624 */
1625static void ad_initialize_agg(struct aggregator *aggregator)
1626{
1627 if (aggregator) {
1628 ad_clear_agg(aggregator);
1629
1630 aggregator->aggregator_mac_address = null_mac_addr;
1631 aggregator->aggregator_identifier = 0;
1632 aggregator->slave = NULL;
1633 }
1634}
1635
1636/**
1637 * ad_initialize_port - initialize a given port's parameters
1638 * @aggregator: the aggregator we're looking at
1639 * @lacp_fast: boolean. whether fast periodic should be used
1640 *
1641 */
1642static void ad_initialize_port(struct port *port, int lacp_fast)
1643{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001644 static const struct port_params tmpl = {
1645 .system_priority = 0xffff,
1646 .key = 1,
1647 .port_number = 1,
1648 .port_priority = 0xff,
1649 .port_state = 1,
1650 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001651 static const struct lacpdu lacpdu = {
1652 .subtype = 0x01,
1653 .version_number = 0x01,
1654 .tlv_type_actor_info = 0x01,
1655 .actor_information_length = 0x14,
1656 .tlv_type_partner_info = 0x02,
1657 .partner_information_length = 0x14,
1658 .tlv_type_collector_info = 0x03,
1659 .collector_information_length = 0x10,
1660 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1661 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (port) {
1664 port->actor_port_number = 1;
1665 port->actor_port_priority = 0xff;
1666 port->actor_system = null_mac_addr;
1667 port->actor_system_priority = 0xffff;
1668 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001669 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 port->actor_admin_port_key = 1;
1671 port->actor_oper_port_key = 1;
1672 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1673 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
1674
Bandan Das7bfc4752010-10-16 20:19:59 +00001675 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001678 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1679 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1680
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001681 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 // ****** private parameters ******
1683 port->sm_vars = 0x3;
1684 port->sm_rx_state = 0;
1685 port->sm_rx_timer_counter = 0;
1686 port->sm_periodic_state = 0;
1687 port->sm_periodic_timer_counter = 0;
1688 port->sm_mux_state = 0;
1689 port->sm_mux_timer_counter = 0;
1690 port->sm_tx_state = 0;
1691 port->sm_tx_timer_counter = 0;
1692 port->slave = NULL;
1693 port->aggregator = NULL;
1694 port->next_port_in_aggregator = NULL;
1695 port->transaction_id = 0;
1696
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001697 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699}
1700
1701/**
1702 * ad_enable_collecting_distributing - enable a port's transmit/receive
1703 * @port: the port we're looking at
1704 *
1705 * Enable @port if it's in an active aggregator
1706 */
1707static void ad_enable_collecting_distributing(struct port *port)
1708{
1709 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001710 pr_debug("Enabling port %d(LAG %d)\n",
1711 port->actor_port_number,
1712 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 __enable_port(port);
1714 }
1715}
1716
1717/**
1718 * ad_disable_collecting_distributing - disable a port's transmit/receive
1719 * @port: the port we're looking at
1720 *
1721 */
1722static void ad_disable_collecting_distributing(struct port *port)
1723{
1724 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001725 pr_debug("Disabling port %d(LAG %d)\n",
1726 port->actor_port_number,
1727 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 __disable_port(port);
1729 }
1730}
1731
1732#if 0
1733/**
1734 * ad_marker_info_send - send a marker information frame
1735 * @port: the port we're looking at
1736 *
1737 * This function does nothing since we decided not to implement send and handle
1738 * response for marker PDU's, in this stage, but only to respond to marker
1739 * information.
1740 */
1741static void ad_marker_info_send(struct port *port)
1742{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001743 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 u16 index;
1745
1746 // fill the marker PDU with the appropriate values
1747 marker.subtype = 0x02;
1748 marker.version_number = 0x01;
1749 marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
1750 marker.marker_length = 0x16;
1751 // convert requester_port to Big Endian
1752 marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
1753 marker.requester_system = port->actor_system;
1754 // convert requester_port(u32) to Big Endian
Bandan Das128ea6c2010-10-16 20:19:58 +00001755 marker.requester_transaction_id =
1756 (((++port->transaction_id & 0xFF) << 24)
1757 | ((port->transaction_id & 0xFF00) << 8)
1758 | ((port->transaction_id & 0xFF0000) >> 8)
1759 | ((port->transaction_id & 0xFF000000) >> 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 marker.pad = 0;
1761 marker.tlv_type_terminator = 0x00;
1762 marker.terminator_length = 0x00;
Bandan Das128ea6c2010-10-16 20:19:58 +00001763 for (index = 0; index < 90; index++)
1764 marker.reserved_90[index] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 // send the marker information
1767 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001768 pr_debug("Sent Marker Information on port %d\n",
1769 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771}
1772#endif
1773
1774/**
1775 * ad_marker_info_received - handle receive of a Marker information frame
1776 * @marker_info: Marker info received
1777 * @port: the port we're looking at
1778 *
1779 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001780static void ad_marker_info_received(struct bond_marker *marker_info,
1781 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001783 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 // copy the received marker data to the response marker
1786 //marker = *marker_info;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001787 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 // change the marker subtype to marker response
Bandan Das128ea6c2010-10-16 20:19:58 +00001789 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 // send the marker response
1791
1792 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001793 pr_debug("Sent Marker Response on port %d\n",
1794 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796}
1797
1798/**
1799 * ad_marker_response_received - handle receive of a marker response frame
1800 * @marker: marker PDU received
1801 * @port: the port we're looking at
1802 *
1803 * This function does nothing since we decided not to implement send and handle
1804 * response for marker PDU's, in this stage, but only to respond to marker
1805 * information.
1806 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001807static void ad_marker_response_received(struct bond_marker *marker,
1808 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Bandan Das128ea6c2010-10-16 20:19:58 +00001810 marker = NULL; /* just to satisfy the compiler */
1811 port = NULL; /* just to satisfy the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815//////////////////////////////////////////////////////////////////////////////////////
1816// ================= AD exported functions to the main bonding code ==================
1817//////////////////////////////////////////////////////////////////////////////////////
1818
1819// Check aggregators status in team every T seconds
1820#define AD_AGGREGATOR_SELECTION_TIMER 8
1821
Jay Vosburghfd989c82008-11-04 17:51:16 -08001822/*
1823 * bond_3ad_initiate_agg_selection(struct bonding *bond)
1824 *
1825 * Set the aggregation selection timer, to initiate an agg selection in
1826 * the very near future. Called during first initialization, and during
1827 * any down to up transitions of the bond.
1828 */
1829void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1830{
1831 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001832}
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834static u16 aggregator_identifier;
1835
1836/**
1837 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1838 * @bond: bonding struct to work on
1839 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 *
1841 * Can be called only after the mac address of the bond is set.
1842 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001843void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 // check that the bond is not initialized yet
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001846 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1847 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 aggregator_identifier = 0;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1852 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1853
1854 // initialize how many times this module is called in one second(should be about every 100ms)
1855 ad_ticks_per_sec = tick_resolution;
1856
Jay Vosburghfd989c82008-11-04 17:51:16 -08001857 bond_3ad_initiate_agg_selection(bond,
1858 AD_AGGREGATOR_SELECTION_TIMER *
1859 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861}
1862
1863/**
1864 * bond_3ad_bind_slave - initialize a slave's port
1865 * @slave: slave struct to work on
1866 *
1867 * Returns: 0 on success
1868 * < 0 on error
1869 */
1870int bond_3ad_bind_slave(struct slave *slave)
1871{
1872 struct bonding *bond = bond_get_bond_by_slave(slave);
1873 struct port *port;
1874 struct aggregator *aggregator;
1875
1876 if (bond == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001877 pr_err("%s: The slave %s is not attached to its bond\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001878 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return -1;
1880 }
1881
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001882 //check that the slave has not been initialized yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (SLAVE_AD_INFO(slave).port.slave != slave) {
1884
1885 // port initialization
1886 port = &(SLAVE_AD_INFO(slave).port);
1887
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001888 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
nikolay@redhat.come0809db2013-02-18 07:59:03 +00001890 __initialize_port_locks(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 port->slave = slave;
1892 port->actor_port_number = SLAVE_AD_INFO(slave).id;
1893 // key is determined according to the link speed, duplex and user key(which is yet not supported)
1894 // ------------------------------------------------------------
1895 // Port key : | User key | Speed |Duplex|
1896 // ------------------------------------------------------------
1897 // 16 6 1 0
1898 port->actor_admin_port_key = 0; // initialize this parameter
1899 port->actor_admin_port_key |= __get_duplex(port);
1900 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1901 port->actor_oper_port_key = port->actor_admin_port_key;
1902 // if the port is not full duplex, then the port should be not lacp Enabled
Bandan Das7bfc4752010-10-16 20:19:59 +00001903 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 // actor system is the bond's system
1906 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
1907 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
1908 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1909 port->aggregator = NULL;
1910 port->next_port_in_aggregator = NULL;
1911
1912 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 // aggregator initialization
1915 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1916
1917 ad_initialize_agg(aggregator);
1918
1919 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
1920 aggregator->aggregator_identifier = (++aggregator_identifier);
1921 aggregator->slave = slave;
1922 aggregator->is_active = 0;
1923 aggregator->num_of_ports = 0;
1924 }
1925
1926 return 0;
1927}
1928
1929/**
1930 * bond_3ad_unbind_slave - deinitialize a slave's port
1931 * @slave: slave struct to work on
1932 *
1933 * Search for the aggregator that is related to this port, remove the
1934 * aggregator and assign another aggregator for other port related to it
1935 * (if any), and remove the port.
1936 */
1937void bond_3ad_unbind_slave(struct slave *slave)
1938{
1939 struct port *port, *prev_port, *temp_port;
1940 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1941 int select_new_active_agg = 0;
Jasper Spaansa361c832009-10-23 04:09:24 +00001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 // find the aggregator related to this slave
1944 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1945
1946 // find the port related to this slave
1947 port = &(SLAVE_AD_INFO(slave).port);
1948
1949 // if slave is null, the whole port is not initialized
1950 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001951 pr_warning("Warning: %s: Trying to unbind an uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001952 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return;
1954 }
1955
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001956 pr_debug("Unbinding Link Aggregation Group %d\n",
1957 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /* Tell the partner that this port is not suitable for aggregation */
1960 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1961 __update_lacpdu_from_port(port);
1962 ad_lacpdu_send(port);
1963
1964 // check if this aggregator is occupied
1965 if (aggregator->lag_ports) {
1966 // check if there are other ports related to this aggregator except
1967 // the port related to this slave(thats ensure us that there is a
1968 // reason to search for new aggregator, and that we will find one
1969 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
1970 // find new aggregator for the related port(s)
1971 new_aggregator = __get_first_agg(port);
1972 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001973 // if the new aggregator is empty, or it is connected to our port only
Bandan Das7bfc4752010-10-16 20:19:59 +00001974 if (!new_aggregator->lag_ports
1975 || ((new_aggregator->lag_ports == port)
1976 && !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979 // if new aggregator found, copy the aggregator's parameters
1980 // and connect the related lag_ports to the new aggregator
1981 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 -08001982 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n",
1983 aggregator->aggregator_identifier,
1984 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001987 pr_info("%s: Removing an active aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001988 aggregator->slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 // select new active aggregator
1990 select_new_active_agg = 1;
1991 }
1992
1993 new_aggregator->is_individual = aggregator->is_individual;
1994 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
1995 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
1996 new_aggregator->partner_system = aggregator->partner_system;
1997 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
1998 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
1999 new_aggregator->receive_state = aggregator->receive_state;
2000 new_aggregator->transmit_state = aggregator->transmit_state;
2001 new_aggregator->lag_ports = aggregator->lag_ports;
2002 new_aggregator->is_active = aggregator->is_active;
2003 new_aggregator->num_of_ports = aggregator->num_of_ports;
2004
2005 // update the information that is written on the ports about the aggregator
Bandan Das128ea6c2010-10-16 20:19:58 +00002006 for (temp_port = aggregator->lag_ports; temp_port;
2007 temp_port = temp_port->next_port_in_aggregator) {
2008 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2010 }
2011
2012 // clear the aggregator
2013 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00002014
Bandan Das7bfc4752010-10-16 20:19:59 +00002015 if (select_new_active_agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 ad_agg_selection_logic(__get_first_agg(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002018 pr_warning("%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002019 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 } else { // in case that the only port related to this aggregator is the one we want to remove
2022 select_new_active_agg = aggregator->is_active;
2023 // clear the aggregator
2024 ad_clear_agg(aggregator);
2025 if (select_new_active_agg) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002026 pr_info("%s: Removing an active aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002027 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 // select new active aggregator
Veaceslav Falico74684492013-09-27 15:10:58 +02002029 temp_aggregator = __get_first_agg(port);
2030 if (temp_aggregator)
2031 ad_agg_selection_logic(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 }
2034 }
2035
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002036 pr_debug("Unbinding port %d\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 // find the aggregator that this port is connected to
2038 temp_aggregator = __get_first_agg(port);
2039 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2040 prev_port = NULL;
2041 // search the port in the aggregator's related ports
Bandan Das128ea6c2010-10-16 20:19:58 +00002042 for (temp_port = temp_aggregator->lag_ports; temp_port;
2043 prev_port = temp_port,
2044 temp_port = temp_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (temp_port == port) { // the aggregator found - detach the port from this aggregator
Bandan Das7bfc4752010-10-16 20:19:59 +00002046 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00002048 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00002051 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 select_new_active_agg = temp_aggregator->is_active;
2053 // clear the aggregator
2054 ad_clear_agg(temp_aggregator);
2055 if (select_new_active_agg) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002056 pr_info("%s: Removing an active aggregator\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002057 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 // select new active aggregator
2059 ad_agg_selection_logic(__get_first_agg(port));
2060 }
2061 }
2062 break;
2063 }
2064 }
2065 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002066 port->slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069/**
2070 * bond_3ad_state_machine_handler - handle state machines timeout
2071 * @bond: bonding struct to work on
2072 *
2073 * The state machine handling concept in this module is to check every tick
2074 * which state machine should operate any function. The execution order is
2075 * round robin, so when we have an interaction between state machines, the
2076 * reply of one to each other might be delayed until next tick.
2077 *
2078 * This function also complete the initialization when the agg_select_timer
2079 * times out, and it selects an aggregator for the ports that are yet not
2080 * related to any aggregator, and selects the active aggregator for a bond.
2081 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002082void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002084 struct bonding *bond = container_of(work, struct bonding,
2085 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002087 struct list_head *iter;
2088 struct slave *slave;
2089 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 read_lock(&bond->lock);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 //check if there are any slaves
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002094 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 // check if agg_select_timer timer after initialize is timed out
2098 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002099 slave = bond_first_slave(bond);
2100 port = slave ? &(SLAVE_AD_INFO(slave).port) : NULL;
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 // select the active aggregator for the bond
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002103 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002105 pr_warning("%s: Warning: bond's first port is uninitialized\n",
2106 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto re_arm;
2108 }
2109
2110 aggregator = __get_first_agg(port);
2111 ad_agg_selection_logic(aggregator);
2112 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002113 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
2116 // for each port run the state machines
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002117 bond_for_each_slave(bond, slave, iter) {
2118 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002120 pr_warning("%s: Warning: Found an uninitialized port\n",
2121 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto re_arm;
2123 }
2124
Nils Carlson16d79d72011-03-03 22:09:11 +00002125 /* Lock around state machines to protect data accessed
2126 * by all (e.g., port->sm_vars). ad_rx_machine may run
2127 * concurrently due to incoming LACPDU.
2128 */
Nils Carlson9ac35242011-03-03 22:09:12 +00002129 __get_state_machine_lock(port);
Nils Carlson16d79d72011-03-03 22:09:11 +00002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 ad_rx_machine(NULL, port);
2132 ad_periodic_machine(port);
2133 ad_port_selection_logic(port);
2134 ad_mux_machine(port);
2135 ad_tx_machine(port);
2136
2137 // turn off the BEGIN bit, since we already handled it
Bandan Das7bfc4752010-10-16 20:19:59 +00002138 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 port->sm_vars &= ~AD_PORT_BEGIN;
Nils Carlson16d79d72011-03-03 22:09:11 +00002140
Nils Carlson9ac35242011-03-03 22:09:12 +00002141 __release_state_machine_lock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143
2144re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002145 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 read_unlock(&bond->lock);
2148}
2149
2150/**
2151 * bond_3ad_rx_indication - handle a received frame
2152 * @lacpdu: received lacpdu
2153 * @slave: slave struct to work on
2154 * @length: length of the data received
2155 *
2156 * It is assumed that frames that were sent on this NIC don't returned as new
2157 * received frames (loopback). Since only the payload is given to this
2158 * function, it check for loopback.
2159 */
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002160static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002163 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if (length >= sizeof(struct lacpdu)) {
2166
2167 port = &(SLAVE_AD_INFO(slave).port);
2168
2169 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002170 pr_warning("%s: Warning: port of slave %s is uninitialized\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002171 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174
2175 switch (lacpdu->subtype) {
2176 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002177 ret = RX_HANDLER_CONSUMED;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002178 pr_debug("Received LACPDU on port %d\n",
2179 port->actor_port_number);
Nils Carlson16d79d72011-03-03 22:09:11 +00002180 /* Protect against concurrent state machines */
Nils Carlson9ac35242011-03-03 22:09:12 +00002181 __get_state_machine_lock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 ad_rx_machine(lacpdu, port);
Nils Carlson9ac35242011-03-03 22:09:12 +00002183 __release_state_machine_lock(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 break;
2185
2186 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002187 ret = RX_HANDLER_CONSUMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 // No need to convert fields to Little Endian since we don't use the marker's fields.
2189
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002190 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 case AD_MARKER_INFORMATION_SUBTYPE:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002192 pr_debug("Received Marker Information on port %d\n",
2193 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002194 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 break;
2196
2197 case AD_MARKER_RESPONSE_SUBTYPE:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002198 pr_debug("Received Marker Response on port %d\n",
2199 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002200 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 break;
2202
2203 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002204 pr_debug("Received an unknown Marker subtype on slot %d\n",
2205 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207 }
2208 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002209 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/**
2213 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2214 * @slave: slave struct to work on
2215 *
2216 * Handle reselection of aggregator (if needed) for this port.
2217 */
2218void bond_3ad_adapter_speed_changed(struct slave *slave)
2219{
2220 struct port *port;
2221
2222 port = &(SLAVE_AD_INFO(slave).port);
2223
2224 // if slave is null, the whole port is not initialized
2225 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002226 pr_warning("Warning: %s: speed changed for uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002227 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return;
2229 }
2230
2231 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002232 port->actor_oper_port_key = port->actor_admin_port_key |=
2233 (__get_link_speed(port) << 1);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002234 pr_debug("Port %d changed speed\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 // there is no need to reselect a new aggregator, just signal the
2236 // state machines to reinitialize
2237 port->sm_vars |= AD_PORT_BEGIN;
2238}
2239
2240/**
2241 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2242 * @slave: slave struct to work on
2243 *
2244 * Handle reselection of aggregator (if needed) for this port.
2245 */
2246void bond_3ad_adapter_duplex_changed(struct slave *slave)
2247{
2248 struct port *port;
2249
Bandan Das128ea6c2010-10-16 20:19:58 +00002250 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 // if slave is null, the whole port is not initialized
2253 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002254 pr_warning("%s: Warning: duplex changed for uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002255 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return;
2257 }
2258
2259 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002260 port->actor_oper_port_key = port->actor_admin_port_key |=
2261 __get_duplex(port);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002262 pr_debug("Port %d changed duplex\n", port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 // there is no need to reselect a new aggregator, just signal the
2264 // state machines to reinitialize
2265 port->sm_vars |= AD_PORT_BEGIN;
2266}
2267
2268/**
2269 * bond_3ad_handle_link_change - handle a slave's link status change indication
2270 * @slave: slave struct to work on
2271 * @status: whether the link is now up or down
2272 *
2273 * Handle reselection of aggregator (if needed) for this port.
2274 */
2275void bond_3ad_handle_link_change(struct slave *slave, char link)
2276{
2277 struct port *port;
2278
2279 port = &(SLAVE_AD_INFO(slave).port);
2280
2281 // if slave is null, the whole port is not initialized
2282 if (!port->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002283 pr_warning("Warning: %s: link status changed for uninitialized port on %s\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002284 slave->bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return;
2286 }
2287
2288 // 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)
2289 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
2290 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002291 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002293 port->actor_oper_port_key = port->actor_admin_port_key |=
2294 __get_duplex(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002296 port->actor_oper_port_key = port->actor_admin_port_key |=
2297 (__get_link_speed(port) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 } else {
2299 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002300 port->is_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002302 port->actor_oper_port_key = (port->actor_admin_port_key &=
2303 ~AD_SPEED_KEY_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
2306 // there is no need to reselect a new aggregator, just signal the
2307 // state machines to reinitialize
2308 port->sm_vars |= AD_PORT_BEGIN;
2309}
2310
Jay Vosburghff59c452006-03-27 13:27:43 -08002311/*
Jasper Spaansa361c832009-10-23 04:09:24 +00002312 * set link state for bonding master: if we have an active
Jay Vosburghff59c452006-03-27 13:27:43 -08002313 * aggregator, we're up, if not, we're down. Presumes that we cannot
2314 * have an active aggregator if there are no slaves with link up.
2315 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002316 * This behavior complies with IEEE 802.3 section 43.3.9.
2317 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002318 * Called by bond_set_carrier(). Return zero if carrier state does not
2319 * change, nonzero if it does.
2320 */
2321int bond_3ad_set_carrier(struct bonding *bond)
2322{
stephen hemminger655f8912011-06-22 09:54:39 +00002323 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002324 struct slave *first_slave;
stephen hemminger655f8912011-06-22 09:54:39 +00002325
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002326 first_slave = bond_first_slave(bond);
2327 if (!first_slave)
2328 return 0;
2329 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave).aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002330 if (active) {
2331 /* are enough slaves available to consider link up? */
2332 if (active->num_of_ports < bond->params.min_links) {
2333 if (netif_carrier_ok(bond->dev)) {
2334 netif_carrier_off(bond->dev);
2335 return 1;
2336 }
2337 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002338 netif_carrier_on(bond->dev);
2339 return 1;
2340 }
2341 return 0;
2342 }
2343
2344 if (netif_carrier_ok(bond->dev)) {
2345 netif_carrier_off(bond->dev);
2346 return 1;
2347 }
2348 return 0;
2349}
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002352 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 * @bond: bonding struct to work on
2354 * @ad_info: ad_info struct to fill with the bond's info
2355 *
2356 * Returns: 0 on success
2357 * < 0 on error
2358 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002359int __bond_3ad_get_active_agg_info(struct bonding *bond,
2360 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002363 struct list_head *iter;
2364 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct port *port;
2366
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002367 bond_for_each_slave(bond, slave, iter) {
2368 port = &(SLAVE_AD_INFO(slave).port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (port->aggregator && port->aggregator->is_active) {
2370 aggregator = port->aggregator;
2371 break;
2372 }
2373 }
2374
2375 if (aggregator) {
2376 ad_info->aggregator_id = aggregator->aggregator_identifier;
2377 ad_info->ports = aggregator->num_of_ports;
2378 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2379 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2380 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
2381 return 0;
2382 }
2383
2384 return -1;
2385}
2386
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002387/* Wrapper used to hold bond->lock so no slave manipulation can occur */
2388int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2389{
2390 int ret;
2391
2392 read_lock(&bond->lock);
2393 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2394 read_unlock(&bond->lock);
2395
2396 return ret;
2397}
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2400{
Wang Chen454d7c92008-11-12 23:37:49 -08002401 struct bonding *bond = netdev_priv(dev);
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002402 struct slave *slave, *first_ok_slave;
2403 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct ad_info ad_info;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002405 struct list_head *iter;
2406 int slaves_in_agg;
2407 int slave_agg_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int res = 1;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002409 int agg_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002411 read_lock(&bond->lock);
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002412 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
2413 pr_debug("%s: Error: __bond_3ad_get_active_agg_info failed\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002414 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 goto out;
2416 }
2417
2418 slaves_in_agg = ad_info.ports;
2419 agg_id = ad_info.aggregator_id;
2420
2421 if (slaves_in_agg == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002422 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto out;
2424 }
2425
Jasper Spaansa361c832009-10-23 04:09:24 +00002426 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg);
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002427 first_ok_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002429 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002430 agg = SLAVE_AD_INFO(slave).port.aggregator;
2431 if (!agg || agg->aggregator_identifier != agg_id)
2432 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002434 if (slave_agg_no >= 0) {
2435 if (!first_ok_slave && SLAVE_IS_OK(slave))
2436 first_ok_slave = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 slave_agg_no--;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002438 continue;
2439 }
2440
2441 if (SLAVE_IS_OK(slave)) {
2442 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 }
2446
2447 if (slave_agg_no >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002448 pr_err("%s: Error: Couldn't find a slave to tx on for aggregator ID %d\n",
2449 dev->name, agg_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto out;
2451 }
2452
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002453 /* we couldn't find any suitable slave after the agg_no, so use the
2454 * first suitable found, if found. */
2455 if (first_ok_slave)
2456 res = bond_dev_queue_xmit(bond, skb, first_ok_slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458out:
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002459 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (res) {
2461 /* no suitable interface, frame not sent */
Eric Dumazet04502432012-06-13 05:30:07 +00002462 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Michał Mirosław0693e882011-05-07 01:48:02 +00002464
Patrick McHardyec634fe2009-07-05 19:23:38 -07002465 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Eric Dumazetde063b72012-06-11 19:23:07 +00002468int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2469 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002471 int ret = RX_HANDLER_ANOTHER;
Eric Dumazetde063b72012-06-11 19:23:07 +00002472 struct lacpdu *lacpdu, _lacpdu;
2473
Jiri Pirko3aba8912011-04-19 03:48:16 +00002474 if (skb->protocol != PKT_TYPE_LACPDU)
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002475 return ret;
Neil Hormanb3053252011-01-20 09:02:31 +00002476
Eric Dumazetde063b72012-06-11 19:23:07 +00002477 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2478 if (!lacpdu)
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002479 return ret;
Andy Gospodarekab128112010-09-10 11:43:20 +00002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 read_lock(&bond->lock);
Eric Dumazetde063b72012-06-11 19:23:07 +00002482 ret = bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 read_unlock(&bond->lock);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002484 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002486
2487/*
2488 * When modify lacp_rate parameter via sysfs,
2489 * update actor_oper_port_state of each port.
2490 *
2491 * Hold slave->state_machine_lock,
2492 * so we can modify port->actor_oper_port_state,
2493 * no matter bond is up or down.
2494 */
2495void bond_3ad_update_lacp_rate(struct bonding *bond)
2496{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002497 struct port *port = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002498 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002499 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002500 int lacp_fast;
2501
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002502 lacp_fast = bond->params.lacp_fast;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002503 bond_for_each_slave(bond, slave, iter) {
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002504 port = &(SLAVE_AD_INFO(slave).port);
2505 __get_state_machine_lock(port);
2506 if (lacp_fast)
2507 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2508 else
2509 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
2510 __release_state_machine_lock(port);
2511 }
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002512}