blob: 7e9e522fd4767a5e4ccdb2076202f8af20f22a16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
24#include <linux/if_ether.h>
25#include <linux/netdevice.h>
26#include <linux/spinlock.h>
27#include <linux/ethtool.h>
Jay Vosburghfd989c82008-11-04 17:51:16 -080028#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/if_bonding.h>
30#include <linux/pkt_sched.h>
Eric W. Biedermane730c152007-09-17 11:53:39 -070031#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "bonding.h"
33#include "bond_3ad.h"
34
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010035/* General definitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define AD_SHORT_TIMEOUT 1
37#define AD_LONG_TIMEOUT 0
38#define AD_STANDBY 0x2
39#define AD_MAX_TX_IN_SECOND 3
40#define AD_COLLECTOR_MAX_DELAY 0
41
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010042/* Timer definitions (43.4.4 in the 802.3ad standard) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define AD_FAST_PERIODIC_TIME 1
44#define AD_SLOW_PERIODIC_TIME 30
45#define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
46#define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
47#define AD_CHURN_DETECTION_TIME 60
48#define AD_AGGREGATE_WAIT_TIME 2
49
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010050/* Port state definitions (43.4.2.2 in the 802.3ad standard) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define AD_STATE_LACP_ACTIVITY 0x1
52#define AD_STATE_LACP_TIMEOUT 0x2
53#define AD_STATE_AGGREGATION 0x4
54#define AD_STATE_SYNCHRONIZATION 0x8
55#define AD_STATE_COLLECTING 0x10
56#define AD_STATE_DISTRIBUTING 0x20
57#define AD_STATE_DEFAULTED 0x40
58#define AD_STATE_EXPIRED 0x80
59
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010060/* Port Variables definitions used by the State Machines (43.4.7 in the
61 * 802.3ad standard)
62 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#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
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010074/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define AD_DUPLEX_KEY_BITS 0x1
83#define AD_SPEED_KEY_BITS 0x3E
84#define AD_USER_KEY_BITS 0xFFC0
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#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
dingtianhong815117a2014-01-02 09:12:54 +080092/* compare MAC addresses */
93#define MAC_ADDRESS_EQUAL(A, B) \
94 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100102/* ================= main 802.3ad protocol functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static 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);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100116static void ad_marker_info_received(struct bond_marker *marker_info,
117 struct port *port);
118static void ad_marker_response_received(struct bond_marker *marker,
119 struct port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100122/* ================= api to bonding and kernel code ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
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.
Veaceslav Falico768b9542014-01-10 11:59:44 +0100144 * The caller must hold RCU or RTNL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
146static inline struct aggregator *__get_first_agg(struct port *port)
147{
148 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200149 struct slave *first_slave;
Veaceslav Falico768b9542014-01-10 11:59:44 +0100150 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
dingtianhongbe79bd02013-12-13 10:20:12 +0800152 /* If there's no bond for this port, or bond has no slaves */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200153 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100155
dingtianhongbe79bd02013-12-13 10:20:12 +0800156 rcu_read_lock();
157 first_slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +0800158 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
dingtianhongbe79bd02013-12-13 10:20:12 +0800159 rcu_read_unlock();
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100160
Veaceslav Falico768b9542014-01-10 11:59:44 +0100161 return agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100164/**
165 * __agg_has_partner - see if we have a partner
166 * @agg: the agregator we're looking at
Jay Vosburghfd989c82008-11-04 17:51:16 -0800167 *
168 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100169 * address for the partner). Return 0 if not.
Jay Vosburghfd989c82008-11-04 17:51:16 -0800170 */
171static inline int __agg_has_partner(struct aggregator *agg)
172{
173 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/**
177 * __disable_port - disable the port's slave
178 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180static inline void __disable_port(struct port *port)
181{
dingtianhong5e5b0662014-02-26 11:05:22 +0800182 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/**
186 * __enable_port - enable the port's slave, if it's up
187 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189static inline void __enable_port(struct port *port)
190{
191 struct slave *slave = port->slave;
192
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200193 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
dingtianhong5e5b0662014-02-26 11:05:22 +0800194 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/**
198 * __port_is_enabled - check if the port's slave is in active state
199 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201static inline int __port_is_enabled(struct port *port)
202{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000203 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/**
207 * __get_agg_selection_mode - get the aggregator selection mode
208 * @port: the port we're looking at
209 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800210 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
212static inline u32 __get_agg_selection_mode(struct port *port)
213{
214 struct bonding *bond = __get_bond_by_port(port);
215
Bandan Das7bfc4752010-10-16 20:19:59 +0000216 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800217 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000219 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/**
223 * __check_agg_selection_timer - check if the selection timer has expired
224 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
226static inline int __check_agg_selection_timer(struct port *port)
227{
228 struct bonding *bond = __get_bond_by_port(port);
229
Bandan Das7bfc4752010-10-16 20:19:59 +0000230 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
234}
235
236/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * __get_link_speed - get a port's speed
238 * @port: the port we're looking at
239 *
240 * Return @port's speed in 802.3ad bitmask format. i.e. one of:
241 * 0,
242 * %AD_LINK_SPEED_BITMASK_10MBPS,
243 * %AD_LINK_SPEED_BITMASK_100MBPS,
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700244 * %AD_LINK_SPEED_BITMASK_1000MBPS,
245 * %AD_LINK_SPEED_BITMASK_10000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
247static u16 __get_link_speed(struct port *port)
248{
249 struct slave *slave = port->slave;
250 u16 speed;
251
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100252 /* this if covers only a special case: when the configuration starts
253 * with link down, it sets the speed to 0.
254 * This is done in spite of the fact that the e100 driver reports 0
255 * to be compatible with MVT in the future.
256 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000257 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000258 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000259 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 switch (slave->speed) {
261 case SPEED_10:
262 speed = AD_LINK_SPEED_BITMASK_10MBPS;
263 break;
264
265 case SPEED_100:
266 speed = AD_LINK_SPEED_BITMASK_100MBPS;
267 break;
268
269 case SPEED_1000:
270 speed = AD_LINK_SPEED_BITMASK_1000MBPS;
271 break;
272
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700273 case SPEED_10000:
274 speed = AD_LINK_SPEED_BITMASK_10000MBPS;
275 break;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100278 /* unknown speed value from ethtool. shouldn't happen */
279 speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 }
282 }
283
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200284 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
285 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return speed;
287}
288
289/**
290 * __get_duplex - get a port's duplex
291 * @port: the port we're looking at
292 *
293 * Return @port's duplex in 802.3ad bitmask format. i.e.:
294 * 0x01 if in full duplex
295 * 0x00 otherwise
296 */
297static u8 __get_duplex(struct port *port)
298{
299 struct slave *slave = port->slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 u8 retval;
301
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100302 /* handling a special case: when the configuration starts with
303 * link down, it sets the duplex to 0.
304 */
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200305 if (slave->link != BOND_LINK_UP) {
Bandan Das128ea6c2010-10-16 20:19:58 +0000306 retval = 0x0;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200307 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 switch (slave->duplex) {
309 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000310 retval = 0x1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200311 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
312 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314 case DUPLEX_HALF:
315 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000316 retval = 0x0;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200317 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
318 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320 }
321 }
322 return retval;
323}
324
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100325/* Conversions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327/**
328 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
329 * @timer_type: which timer to operate
330 * @par: timer parameter. see below
331 *
332 * If @timer_type is %current_while_timer, @par indicates long/short timer.
333 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100334 * %SLOW_PERIODIC_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
337{
Bandan Das128ea6c2010-10-16 20:19:58 +0000338 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 switch (timer_type) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100341 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
Bandan Das7bfc4752010-10-16 20:19:59 +0000342 if (par)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100343 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
Bandan Das7bfc4752010-10-16 20:19:59 +0000344 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100345 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100347 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
349 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100350 case AD_PERIODIC_TIMER: /* for periodic machine */
351 retval = (par*ad_ticks_per_sec); /* long timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100353 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
355 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100356 case AD_WAIT_WHILE_TIMER: /* for selection machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
358 break;
359 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return retval;
362}
363
364
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100365/* ================= ad_rx_machine helper functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000368 * __choose_matched - update a port's matched variable from a received lacpdu
369 * @lacpdu: the lacpdu we've received
370 * @port: the port we're looking at
371 *
372 * Update the value of the matched variable, using parameter values from a
373 * newly received lacpdu. Parameter values for the partner carried in the
374 * received PDU are compared with the corresponding operational parameter
375 * values for the actor. Matched is set to TRUE if all of these parameters
376 * match and the PDU parameter partner_state.aggregation has the same value as
377 * actor_oper_port_state.aggregation and lacp will actively maintain the link
378 * in the aggregation. Matched is also set to TRUE if the value of
379 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
380 * an individual link and lacp will actively maintain the link. Otherwise,
381 * matched is set to FALSE. LACP is considered to be actively maintaining the
382 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
383 * the actor's actor_oper_port_state.lacp_activity and the PDU's
384 * partner_state.lacp_activity variables are TRUE.
385 *
386 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
387 * used here to implement the language from 802.3ad 43.4.9 that requires
388 * recordPDU to "match" the LACPDU parameters to the stored values.
389 */
390static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
391{
dingtianhong815117a2014-01-02 09:12:54 +0800392 /* check if all parameters are alike
393 * or this is individual link(aggregation == FALSE)
394 * then update the state machine Matched variable.
395 */
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000396 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
397 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
dingtianhong815117a2014-01-02 09:12:54 +0800398 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000399 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
400 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
401 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000402 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
403 ) {
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000404 port->sm_vars |= AD_PORT_MATCHED;
405 } else {
406 port->sm_vars &= ~AD_PORT_MATCHED;
407 }
408}
409
410/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * __record_pdu - record parameters from a received lacpdu
412 * @lacpdu: the lacpdu we've received
413 * @port: the port we're looking at
414 *
415 * Record the parameter values for the Actor carried in a received lacpdu as
416 * the current partner operational parameter values and sets
417 * actor_oper_port_state.defaulted to FALSE.
418 */
419static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800422 struct port_params *partner = &port->partner_oper;
423
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000424 __choose_matched(lacpdu, port);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100425 /* record the new parameter values for the partner
426 * operational
427 */
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800428 partner->port_number = ntohs(lacpdu->actor_port);
429 partner->port_priority = ntohs(lacpdu->actor_port_priority);
430 partner->system = lacpdu->actor_system;
431 partner->system_priority = ntohs(lacpdu->actor_system_priority);
432 partner->key = ntohs(lacpdu->actor_key);
433 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100435 /* set actor_oper_port_state.defaulted to FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
437
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100438 /* set the partner sync. to on if the partner is sync,
439 * and the port is matched
440 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000441 if ((port->sm_vars & AD_PORT_MATCHED)
442 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800443 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Bandan Das7bfc4752010-10-16 20:19:59 +0000444 else
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800445 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447}
448
449/**
450 * __record_default - record default parameters
451 * @port: the port we're looking at
452 *
453 * This function records the default parameter values for the partner carried
454 * in the Partner Admin parameters as the current partner operational parameter
455 * values and sets actor_oper_port_state.defaulted to TRUE.
456 */
457static void __record_default(struct port *port)
458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (port) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100460 /* record the partner admin parameters */
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800461 memcpy(&port->partner_oper, &port->partner_admin,
462 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100464 /* set actor_oper_port_state.defaulted to true */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
466 }
467}
468
469/**
470 * __update_selected - update a port's Selected variable from a received lacpdu
471 * @lacpdu: the lacpdu we've received
472 * @port: the port we're looking at
473 *
474 * Update the value of the selected variable, using parameter values from a
475 * newly received lacpdu. The parameter values for the Actor carried in the
476 * received PDU are compared with the corresponding operational parameter
477 * values for the ports partner. If one or more of the comparisons shows that
478 * the value(s) received in the PDU differ from the current operational values,
479 * then selected is set to FALSE and actor_oper_port_state.synchronization is
480 * set to out_of_sync. Otherwise, selected remains unchanged.
481 */
482static void __update_selected(struct lacpdu *lacpdu, struct port *port)
483{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800485 const struct port_params *partner = &port->partner_oper;
486
dingtianhong815117a2014-01-02 09:12:54 +0800487 /* check if any parameter is different then
488 * update the state machine selected variable.
489 */
Joe Perches8e95a202009-12-03 07:58:21 +0000490 if (ntohs(lacpdu->actor_port) != partner->port_number ||
491 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800492 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000493 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
494 ntohs(lacpdu->actor_key) != partner->key ||
495 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 port->sm_vars &= ~AD_PORT_SELECTED;
497 }
498 }
499}
500
501/**
502 * __update_default_selected - update a port's Selected variable from Partner
503 * @port: the port we're looking at
504 *
505 * This function updates the value of the selected variable, using the partner
506 * administrative parameter values. The administrative values are compared with
507 * the corresponding operational parameter values for the partner. If one or
508 * more of the comparisons shows that the administrative value(s) differ from
509 * the current operational values, then Selected is set to FALSE and
510 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
511 * Selected remains unchanged.
512 */
513static void __update_default_selected(struct port *port)
514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800516 const struct port_params *admin = &port->partner_admin;
517 const struct port_params *oper = &port->partner_oper;
518
dingtianhong815117a2014-01-02 09:12:54 +0800519 /* check if any parameter is different then
520 * update the state machine selected variable.
521 */
Joe Perches8e95a202009-12-03 07:58:21 +0000522 if (admin->port_number != oper->port_number ||
523 admin->port_priority != oper->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800524 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000525 admin->system_priority != oper->system_priority ||
526 admin->key != oper->key ||
527 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800528 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 port->sm_vars &= ~AD_PORT_SELECTED;
530 }
531 }
532}
533
534/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * __update_ntt - update a port's ntt variable from a received lacpdu
536 * @lacpdu: the lacpdu we've received
537 * @port: the port we're looking at
538 *
539 * Updates the value of the ntt variable, using parameter values from a newly
540 * received lacpdu. The parameter values for the partner carried in the
541 * received PDU are compared with the corresponding operational parameter
542 * values for the Actor. If one or more of the comparisons shows that the
543 * value(s) received in the PDU differ from the current operational values,
544 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
545 */
546static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
547{
dingtianhong815117a2014-01-02 09:12:54 +0800548 /* validate lacpdu and port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (lacpdu && port) {
dingtianhong815117a2014-01-02 09:12:54 +0800550 /* check if any parameter is different then
551 * update the port->ntt.
552 */
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700553 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
554 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
dingtianhong815117a2014-01-02 09:12:54 +0800555 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700556 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
557 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
559 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
560 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
561 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
562 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800563 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 }
566}
567
568/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * __agg_ports_are_ready - check if all ports in an aggregator are ready
570 * @aggregator: the aggregator we're looking at
571 *
572 */
573static int __agg_ports_are_ready(struct aggregator *aggregator)
574{
575 struct port *port;
576 int retval = 1;
577
578 if (aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100579 /* scan all ports in this aggregator to verfy if they are
580 * all ready.
581 */
Bandan Das128ea6c2010-10-16 20:19:58 +0000582 for (port = aggregator->lag_ports;
583 port;
584 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!(port->sm_vars & AD_PORT_READY_N)) {
586 retval = 0;
587 break;
588 }
589 }
590 }
591
592 return retval;
593}
594
595/**
596 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
597 * @aggregator: the aggregator we're looking at
598 * @val: Should the ports' ready bit be set on or off
599 *
600 */
601static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
602{
603 struct port *port;
604
Bandan Das128ea6c2010-10-16 20:19:58 +0000605 for (port = aggregator->lag_ports; port;
606 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000607 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000609 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612}
613
614/**
615 * __get_agg_bandwidth - get the total bandwidth of an aggregator
616 * @aggregator: the aggregator we're looking at
617 *
618 */
619static u32 __get_agg_bandwidth(struct aggregator *aggregator)
620{
Bandan Das128ea6c2010-10-16 20:19:58 +0000621 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 if (aggregator->num_of_ports) {
David Decotigny65cce192011-04-13 15:22:30 +0000624 switch (__get_link_speed(aggregator->lag_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 case AD_LINK_SPEED_BITMASK_1MBPS:
626 bandwidth = aggregator->num_of_ports;
627 break;
628 case AD_LINK_SPEED_BITMASK_10MBPS:
629 bandwidth = aggregator->num_of_ports * 10;
630 break;
631 case AD_LINK_SPEED_BITMASK_100MBPS:
632 bandwidth = aggregator->num_of_ports * 100;
633 break;
634 case AD_LINK_SPEED_BITMASK_1000MBPS:
635 bandwidth = aggregator->num_of_ports * 1000;
636 break;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700637 case AD_LINK_SPEED_BITMASK_10000MBPS:
638 bandwidth = aggregator->num_of_ports * 10000;
639 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100641 bandwidth = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644 return bandwidth;
645}
646
647/**
648 * __get_active_agg - get the current active aggregator
649 * @aggregator: the aggregator we're looking at
Veaceslav Falico49b76242014-01-10 11:59:45 +0100650 *
651 * Caller must hold RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
653static struct aggregator *__get_active_agg(struct aggregator *aggregator)
654{
Veaceslav Falico19177e72013-09-27 16:12:00 +0200655 struct bonding *bond = aggregator->slave->bond;
656 struct list_head *iter;
657 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
dingtianhongbe79bd02013-12-13 10:20:12 +0800659 bond_for_each_slave_rcu(bond, slave, iter)
dingtianhong3fdddd82014-05-12 15:08:43 +0800660 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
661 return &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Veaceslav Falico19177e72013-09-27 16:12:00 +0200663 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/**
667 * __update_lacpdu_from_port - update a port's lacpdu fields
668 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
670static inline void __update_lacpdu_from_port(struct port *port)
671{
672 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800673 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100675 /* update current actual Actor parameters
676 * lacpdu->subtype initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * lacpdu->version_number initialized
678 * lacpdu->tlv_type_actor_info initialized
679 * lacpdu->actor_information_length initialized
680 */
681
Al Virod3bb52b2007-08-22 20:06:58 -0400682 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400684 lacpdu->actor_key = htons(port->actor_oper_port_key);
685 lacpdu->actor_port_priority = htons(port->actor_port_priority);
686 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 lacpdu->actor_state = port->actor_oper_port_state;
688
689 /* lacpdu->reserved_3_1 initialized
690 * lacpdu->tlv_type_partner_info initialized
691 * lacpdu->partner_information_length initialized
692 */
693
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800694 lacpdu->partner_system_priority = htons(partner->system_priority);
695 lacpdu->partner_system = partner->system;
696 lacpdu->partner_key = htons(partner->key);
697 lacpdu->partner_port_priority = htons(partner->port_priority);
698 lacpdu->partner_port = htons(partner->port_number);
699 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* lacpdu->reserved_3_2 initialized
702 * lacpdu->tlv_type_collector_info initialized
703 * lacpdu->collector_information_length initialized
704 * collector_max_delay initialized
705 * reserved_12[12] initialized
706 * tlv_type_terminator initialized
707 * terminator_length initialized
708 * reserved_50[50] initialized
709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100712/* ================= main 802.3ad protocol code ========================= */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714/**
715 * ad_lacpdu_send - send out a lacpdu packet on a given port
716 * @port: the port we're looking at
717 *
718 * Returns: 0 on success
719 * < 0 on error
720 */
721static int ad_lacpdu_send(struct port *port)
722{
723 struct slave *slave = port->slave;
724 struct sk_buff *skb;
725 struct lacpdu_header *lacpdu_header;
726 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000729 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700733 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700734 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 skb->protocol = PKT_TYPE_LACPDU;
736 skb->priority = TC_PRIO_CONTROL;
737
738 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
739
Joe Perchesada0f862014-02-15 16:02:17 -0800740 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400741 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100742 * because we use it to identify loopback lacpdus in receive.
743 */
Joe Perchesada0f862014-02-15 16:02:17 -0800744 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800745 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100747 lacpdu_header->lacpdu = port->lacpdu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 dev_queue_xmit(skb);
750
751 return 0;
752}
753
754/**
755 * ad_marker_send - send marker information/response on a given port
756 * @port: the port we're looking at
757 * @marker: marker data to send
758 *
759 * Returns: 0 on success
760 * < 0 on error
761 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700762static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct slave *slave = port->slave;
765 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700766 struct bond_marker_header *marker_header;
767 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000770 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 skb_reserve(skb, 16);
774
775 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700776 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700777 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 skb->protocol = PKT_TYPE_LACPDU;
779
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700780 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Joe Perchesada0f862014-02-15 16:02:17 -0800782 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400783 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100784 * because we use it to identify loopback MARKERs in receive.
785 */
Joe Perchesada0f862014-02-15 16:02:17 -0800786 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800787 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100789 marker_header->marker = *marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 dev_queue_xmit(skb);
792
793 return 0;
794}
795
796/**
797 * ad_mux_machine - handle a port's mux state machine
798 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
800static void ad_mux_machine(struct port *port)
801{
802 mux_states_t last_state;
803
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100804 /* keep current State Machine state to compare later if it was
805 * changed
806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 last_state = port->sm_mux_state;
808
809 if (port->sm_vars & AD_PORT_BEGIN) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100810 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 } else {
812 switch (port->sm_mux_state) {
813 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000814 if ((port->sm_vars & AD_PORT_SELECTED)
815 || (port->sm_vars & AD_PORT_STANDBY))
816 /* if SELECTED or STANDBY */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100817 port->sm_mux_state = AD_MUX_WAITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819 case AD_MUX_WAITING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100820 /* if SELECTED == FALSE return to DETACH state */
821 if (!(port->sm_vars & AD_PORT_SELECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100823 /* in order to withhold the Selection Logic to
824 * check all ports READY_N value every callback
825 * cycle to update ready variable, we check
826 * READY_N and update READY here
827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100829 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831 }
832
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100833 /* check if the wait_while_timer expired */
Bandan Das7bfc4752010-10-16 20:19:59 +0000834 if (port->sm_mux_timer_counter
835 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100838 /* in order to withhold the selection logic to check
839 * all ports READY_N value every callback cycle to
840 * update ready variable, we check READY_N and update
841 * READY here
842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
844
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100845 /* if the wait_while_timer expired, and the port is
846 * in READY state, move to ATTACHED state
847 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000848 if ((port->sm_vars & AD_PORT_READY)
849 && !port->sm_mux_timer_counter)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100850 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852 case AD_MUX_ATTACHED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100853 /* check also if agg_select_timer expired (so the
854 * edable port will take place only after this timer)
855 */
856 if ((port->sm_vars & AD_PORT_SELECTED) &&
857 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
858 !__check_agg_selection_timer(port)) {
859 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;
860 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
861 (port->sm_vars & AD_PORT_STANDBY)) {
862 /* if UNSELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100864 /* in order to withhold the selection logic to
865 * check all ports READY_N value every callback
866 * cycle to update ready variable, we check
867 * READY_N and update READY here
868 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100870 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 break;
873 case AD_MUX_COLLECTING_DISTRIBUTING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100874 if (!(port->sm_vars & AD_PORT_SELECTED) ||
875 (port->sm_vars & AD_PORT_STANDBY) ||
876 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)) {
877 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100879 /* if port state hasn't changed make
880 * sure that a collecting distributing
881 * port in an active aggregator is enabled
882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (port->aggregator &&
884 port->aggregator->is_active &&
885 !__port_is_enabled(port)) {
886
887 __enable_port(port);
888 }
889 }
890 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100891 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
893 }
894 }
895
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100896 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (port->sm_mux_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800898 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
899 port->actor_port_number, last_state,
900 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 switch (port->sm_mux_state) {
902 case AD_MUX_DETACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
904 ad_disable_collecting_distributing(port);
905 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
906 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800907 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 break;
909 case AD_MUX_WAITING:
910 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
911 break;
912 case AD_MUX_ATTACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
914 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
915 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
916 ad_disable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800917 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
919 case AD_MUX_COLLECTING_DISTRIBUTING:
920 port->actor_oper_port_state |= AD_STATE_COLLECTING;
921 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
922 ad_enable_collecting_distributing(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800923 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100925 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
927 }
928 }
929}
930
931/**
932 * ad_rx_machine - handle a port's rx State Machine
933 * @lacpdu: the lacpdu we've received
934 * @port: the port we're looking at
935 *
936 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
937 * CURRENT. If timer expired set the state machine in the proper state.
938 * In other cases, this function checks if we need to switch to other state.
939 */
940static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
941{
942 rx_states_t last_state;
943
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100944 /* keep current State Machine state to compare later if it was
945 * changed
946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 last_state = port->sm_rx_state;
948
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100949 /* check if state machine should change state */
950
951 /* first, check if port was reinitialized */
Bandan Das7bfc4752010-10-16 20:19:59 +0000952 if (port->sm_vars & AD_PORT_BEGIN)
Bandan Das7bfc4752010-10-16 20:19:59 +0000953 port->sm_rx_state = AD_RX_INITIALIZE;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100954 /* check if port is not enabled */
Bandan Das7bfc4752010-10-16 20:19:59 +0000955 else if (!(port->sm_vars & AD_PORT_BEGIN)
956 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
Bandan Das7bfc4752010-10-16 20:19:59 +0000957 port->sm_rx_state = AD_RX_PORT_DISABLED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100958 /* check if new lacpdu arrived */
959 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
960 (port->sm_rx_state == AD_RX_DEFAULTED) ||
961 (port->sm_rx_state == AD_RX_CURRENT))) {
962 port->sm_rx_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 port->sm_rx_state = AD_RX_CURRENT;
964 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100965 /* if timer is on, and if it is expired */
966 if (port->sm_rx_timer_counter &&
967 !(--port->sm_rx_timer_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 switch (port->sm_rx_state) {
969 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100970 port->sm_rx_state = AD_RX_DEFAULTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 break;
972 case AD_RX_CURRENT:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100973 port->sm_rx_state = AD_RX_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100975 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
977 }
978 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100979 /* if no lacpdu arrived and no timer is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 switch (port->sm_rx_state) {
981 case AD_RX_PORT_DISABLED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000982 if (port->sm_vars & AD_PORT_MOVED)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100983 port->sm_rx_state = AD_RX_INITIALIZE;
Bandan Das7bfc4752010-10-16 20:19:59 +0000984 else if (port->is_enabled
985 && (port->sm_vars
986 & AD_PORT_LACP_ENABLED))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100987 port->sm_rx_state = AD_RX_EXPIRED;
Bandan Das7bfc4752010-10-16 20:19:59 +0000988 else if (port->is_enabled
989 && ((port->sm_vars
990 & AD_PORT_LACP_ENABLED) == 0))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100991 port->sm_rx_state = AD_RX_LACP_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100993 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995
996 }
997 }
998 }
999
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001000 /* check if the State machine was changed or new lacpdu arrived */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001002 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1003 port->actor_port_number, last_state,
1004 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 switch (port->sm_rx_state) {
1006 case AD_RX_INITIALIZE:
Bandan Das7bfc4752010-10-16 20:19:59 +00001007 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001009 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 port->sm_vars &= ~AD_PORT_SELECTED;
1012 __record_default(port);
1013 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1014 port->sm_vars &= ~AD_PORT_MOVED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001015 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001017 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 case AD_RX_PORT_DISABLED:
1019 port->sm_vars &= ~AD_PORT_MATCHED;
1020 break;
1021 case AD_RX_LACP_DISABLED:
1022 port->sm_vars &= ~AD_PORT_SELECTED;
1023 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001024 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 port->sm_vars |= AD_PORT_MATCHED;
1026 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1027 break;
1028 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001029 /* Reset of the Synchronization flag (Standard 43.4.12)
1030 * This reset cause to disable this port in the
1031 * COLLECTING_DISTRIBUTING state of the mux machine in
1032 * case of EXPIRED even if LINK_DOWN didn't arrive for
1033 * the port.
1034 */
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001035 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 port->sm_vars &= ~AD_PORT_MATCHED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001037 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1039 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1040 break;
1041 case AD_RX_DEFAULTED:
1042 __update_default_selected(port);
1043 __record_default(port);
1044 port->sm_vars |= AD_PORT_MATCHED;
1045 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1046 break;
1047 case AD_RX_CURRENT:
dingtianhong815117a2014-01-02 09:12:54 +08001048 /* detect loopback situation */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001049 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1050 &(port->actor_system))) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001051 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
Joe Perches90194262014-02-15 16:01:45 -08001052 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001053 port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return;
1055 }
1056 __update_selected(lacpdu, port);
1057 __update_ntt(lacpdu, port);
1058 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1060 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001062 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
1064 }
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068/**
1069 * ad_tx_machine - handle a port's tx state machine
1070 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072static void ad_tx_machine(struct port *port)
1073{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001074 /* check if tx timer expired, to verify that we do not send more than
1075 * 3 packets per second
1076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001078 /* check if there is something to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1080 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001083 pr_debug("Sent LACPDU on port %d\n",
1084 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001085
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001086 /* mark ntt as false, so it will not be sent
1087 * again until demanded
1088 */
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001089 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001092 /* restart tx timer(to verify that we will not exceed
1093 * AD_MAX_TX_IN_SECOND
1094 */
1095 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097}
1098
1099/**
1100 * ad_periodic_machine - handle a port's periodic state machine
1101 * @port: the port we're looking at
1102 *
1103 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1104 */
1105static void ad_periodic_machine(struct port *port)
1106{
1107 periodic_states_t last_state;
1108
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001109 /* keep current state machine state to compare later if it was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 last_state = port->sm_periodic_state;
1111
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001112 /* check if port was reinitialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001114 (!(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 -07001115 ) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001116 port->sm_periodic_state = AD_NO_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001118 /* check if state machine should change state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 else if (port->sm_periodic_timer_counter) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001120 /* check if periodic state machine expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!(--port->sm_periodic_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001122 /* if expired then do tx */
1123 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001125 /* If not expired, check if there is some new timeout
1126 * parameter from the partner state
1127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 switch (port->sm_periodic_state) {
1129 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001130 if (!(port->partner_oper.port_state
1131 & AD_STATE_LACP_TIMEOUT))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001132 port->sm_periodic_state = AD_SLOW_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
1134 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001135 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 port->sm_periodic_timer_counter = 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001137 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001140 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142 }
1143 }
1144 } else {
1145 switch (port->sm_periodic_state) {
1146 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001147 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case AD_PERIODIC_TX:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001150 if (!(port->partner_oper.port_state &
1151 AD_STATE_LACP_TIMEOUT))
1152 port->sm_periodic_state = AD_SLOW_PERIODIC;
Bandan Das7bfc4752010-10-16 20:19:59 +00001153 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001154 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001156 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 }
1159 }
1160
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001161 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001163 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1164 port->actor_port_number, last_state,
1165 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 switch (port->sm_periodic_state) {
1167 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001168 port->sm_periodic_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 break;
1170 case AD_FAST_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001171 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1172 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 case AD_SLOW_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001175 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1176 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
1178 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001179 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001181 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 }
1184 }
1185}
1186
1187/**
1188 * ad_port_selection_logic - select aggregation groups
1189 * @port: the port we're looking at
1190 *
1191 * Select aggregation groups, and assign each port for it's aggregetor. The
1192 * selection logic is called in the inititalization (after all the handshkes),
1193 * and after every lacpdu receive (if selected is off).
1194 */
1195static void ad_port_selection_logic(struct port *port)
1196{
1197 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1198 struct port *last_port = NULL, *curr_port;
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001199 struct list_head *iter;
1200 struct bonding *bond;
1201 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int found = 0;
1203
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001204 /* if the port is already Selected, do nothing */
Bandan Das7bfc4752010-10-16 20:19:59 +00001205 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001208 bond = __get_bond_by_port(port);
1209
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001210 /* if the port is connected to other aggregator, detach it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (port->aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001212 /* detach the port from its former aggregator */
Bandan Das128ea6c2010-10-16 20:19:58 +00001213 temp_aggregator = port->aggregator;
1214 for (curr_port = temp_aggregator->lag_ports; curr_port;
1215 last_port = curr_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001216 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (curr_port == port) {
1218 temp_aggregator->num_of_ports--;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001219 /* if it is the first port attached to the
1220 * aggregator
1221 */
1222 if (!last_port) {
Bandan Das128ea6c2010-10-16 20:19:58 +00001223 temp_aggregator->lag_ports =
1224 port->next_port_in_aggregator;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001225 } else {
1226 /* not the first port attached to the
1227 * aggregator
1228 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001229 last_port->next_port_in_aggregator =
1230 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001233 /* clear the port's relations to this
1234 * aggregator
1235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001237 port->next_port_in_aggregator = NULL;
1238 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001240 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1241 port->actor_port_number,
1242 temp_aggregator->aggregator_identifier);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001243 /* if the aggregator is empty, clear its
1244 * parameters, and set it ready to be attached
1245 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001246 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
1249 }
1250 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001251 if (!curr_port) {
1252 /* meaning: the port was related to an aggregator
1253 * but was not on the aggregator port list
1254 */
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001255 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1256 port->slave->bond->dev->name,
1257 port->actor_port_number,
1258 port->slave->dev->name,
1259 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001262 /* search on all aggregators for a suitable aggregator for this port */
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001263 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001264 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001266 /* keep a free aggregator for later use(if needed) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001268 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001269 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 continue;
1271 }
dingtianhong815117a2014-01-02 09:12:54 +08001272 /* check if current aggregator suits us */
1273 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1274 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001275 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1276 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 ) &&
dingtianhong815117a2014-01-02 09:12:54 +08001278 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1279 !aggregator->is_individual) /* but is not individual OR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 )
1281 ) {
dingtianhong815117a2014-01-02 09:12:54 +08001282 /* attach to the founded aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001284 port->actor_port_aggregator_identifier =
1285 port->aggregator->aggregator_identifier;
1286 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001288 aggregator->lag_ports = port;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001289 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1290 port->actor_port_number,
1291 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001293 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 port->sm_vars |= AD_PORT_SELECTED;
1295 found = 1;
1296 break;
1297 }
1298 }
1299
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001300 /* the port couldn't find an aggregator - attach it to a new
1301 * aggregator
1302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!found) {
1304 if (free_aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001305 /* assign port a new aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001307 port->actor_port_aggregator_identifier =
1308 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001310 /* update the new aggregator's parameters
1311 * if port was responsed from the end-user
1312 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001313 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)
1314 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001315 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001316 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001317 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1320 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001321 port->aggregator->partner_system =
1322 port->partner_oper.system;
1323 port->aggregator->partner_system_priority =
1324 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001325 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 port->aggregator->receive_state = 1;
1327 port->aggregator->transmit_state = 1;
1328 port->aggregator->lag_ports = port;
1329 port->aggregator->num_of_ports++;
1330
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001331 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 port->sm_vars |= AD_PORT_SELECTED;
1333
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001334 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1335 port->actor_port_number,
1336 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001338 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 port->actor_port_number, port->slave->dev->name);
1340 }
1341 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001342 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1343 * in all aggregator's ports, else set ready=FALSE in all
1344 * aggregator's ports
1345 */
1346 __set_agg_ports_ready(port->aggregator,
1347 __agg_ports_are_ready(port->aggregator));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Jay Vosburghfd989c82008-11-04 17:51:16 -08001349 aggregator = __get_first_agg(port);
1350 ad_agg_selection_logic(aggregator);
1351}
1352
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001353/* Decide if "agg" is a better choice for the new active aggregator that
Jay Vosburghfd989c82008-11-04 17:51:16 -08001354 * the current best, according to the ad_select policy.
1355 */
1356static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1357 struct aggregator *curr)
1358{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001359 /* 0. If no best, select current.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001360 *
1361 * 1. If the current agg is not individual, and the best is
1362 * individual, select current.
1363 *
1364 * 2. If current agg is individual and the best is not, keep best.
1365 *
1366 * 3. Therefore, current and best are both individual or both not
1367 * individual, so:
1368 *
1369 * 3a. If current agg partner replied, and best agg partner did not,
1370 * select current.
1371 *
1372 * 3b. If current agg partner did not reply and best agg partner
1373 * did reply, keep best.
1374 *
1375 * 4. Therefore, current and best both have partner replies or
1376 * both do not, so perform selection policy:
1377 *
1378 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1379 * select by bandwidth.
1380 *
1381 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1382 */
1383 if (!best)
1384 return curr;
1385
1386 if (!curr->is_individual && best->is_individual)
1387 return curr;
1388
1389 if (curr->is_individual && !best->is_individual)
1390 return best;
1391
1392 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1393 return curr;
1394
1395 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1396 return best;
1397
1398 switch (__get_agg_selection_mode(curr->lag_ports)) {
1399 case BOND_AD_COUNT:
1400 if (curr->num_of_ports > best->num_of_ports)
1401 return curr;
1402
1403 if (curr->num_of_ports < best->num_of_ports)
1404 return best;
1405
1406 /*FALLTHROUGH*/
1407 case BOND_AD_STABLE:
1408 case BOND_AD_BANDWIDTH:
1409 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1410 return curr;
1411
1412 break;
1413
1414 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001415 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1416 curr->slave->bond->dev->name,
1417 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001420
1421 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001424static int agg_device_up(const struct aggregator *agg)
1425{
Jiri Bohac2430af82011-04-19 02:09:55 +00001426 struct port *port = agg->lag_ports;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001427
Jiri Bohac2430af82011-04-19 02:09:55 +00001428 if (!port)
1429 return 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001430
1431 return netif_running(port->slave->dev) &&
1432 netif_carrier_ok(port->slave->dev);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/**
1436 * ad_agg_selection_logic - select an aggregation group for a team
1437 * @aggregator: the aggregator we're looking at
1438 *
1439 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001440 *
1441 * The logic of this function is to select the aggregator according to
1442 * the ad_select policy:
1443 *
1444 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1445 * it, and to reselect the active aggregator only if the previous
1446 * aggregator has no more ports related to it.
1447 *
1448 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1449 * bandwidth, and reselect whenever a link state change takes place or the
1450 * set of slaves in the bond changes.
1451 *
1452 * BOND_AD_COUNT: select the aggregator with largest number of ports
1453 * (slaves), and reselect whenever a link state change takes place or the
1454 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 *
1456 * FIXME: this function MUST be called with the first agg in the bond, or
1457 * __get_active_agg() won't work correctly. This function should be better
1458 * called with the bond itself, and retrieve the first agg from it.
1459 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001460static void ad_agg_selection_logic(struct aggregator *agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001462 struct aggregator *best, *active, *origin;
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001463 struct bonding *bond = agg->slave->bond;
1464 struct list_head *iter;
1465 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Veaceslav Falico49b76242014-01-10 11:59:45 +01001468 rcu_read_lock();
Jay Vosburghfd989c82008-11-04 17:51:16 -08001469 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001470 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001471 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
dingtianhongbe79bd02013-12-13 10:20:12 +08001473 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001474 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001475
Jay Vosburghfd989c82008-11-04 17:51:16 -08001476 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001478 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001479 best = ad_agg_selection_test(best, agg);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jay Vosburghfd989c82008-11-04 17:51:16 -08001482 if (best &&
1483 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001484 /* For the STABLE policy, don't replace the old active
Jay Vosburghfd989c82008-11-04 17:51:16 -08001485 * aggregator if it's still active (it has an answering
1486 * partner) or if both the best and active don't have an
1487 * answering partner.
1488 */
1489 if (active && active->lag_ports &&
1490 active->lag_ports->is_enabled &&
1491 (__agg_has_partner(active) ||
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001492 (!__agg_has_partner(active) &&
1493 !__agg_has_partner(best)))) {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001494 if (!(!active->actor_oper_aggregator_key &&
1495 best->actor_oper_aggregator_key)) {
1496 best = NULL;
1497 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 }
1500 }
1501
Jay Vosburghfd989c82008-11-04 17:51:16 -08001502 if (best && (best == active)) {
1503 best = NULL;
1504 active->is_active = 1;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
dingtianhongbe79bd02013-12-13 10:20:12 +08001507 /* if there is new best aggregator, activate it */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001508 if (best) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001509 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1510 best->aggregator_identifier, best->num_of_ports,
1511 best->actor_oper_aggregator_key,
1512 best->partner_oper_aggregator_key,
1513 best->is_individual, best->is_active);
1514 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1515 best->lag_ports, best->slave,
1516 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001517
dingtianhongbe79bd02013-12-13 10:20:12 +08001518 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001519 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001520
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001521 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1522 agg->aggregator_identifier, agg->num_of_ports,
1523 agg->actor_oper_aggregator_key,
1524 agg->partner_oper_aggregator_key,
1525 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
dingtianhongbe79bd02013-12-13 10:20:12 +08001528 /* check if any partner replys */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001529 if (best->is_individual) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001530 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1531 best->slave ?
1532 best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
Jay Vosburghfd989c82008-11-04 17:51:16 -08001535 best->is_active = 1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001536 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1537 best->aggregator_identifier);
1538 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1539 best->aggregator_identifier, best->num_of_ports,
1540 best->actor_oper_aggregator_key,
1541 best->partner_oper_aggregator_key,
1542 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001544 /* disable the ports that were related to the former
1545 * active_aggregator
1546 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001547 if (active) {
1548 for (port = active->lag_ports; port;
1549 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 __disable_port(port);
1551 }
1552 }
1553 }
1554
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001555 /* if the selected aggregator is of join individuals
Jay Vosburghfd989c82008-11-04 17:51:16 -08001556 * (partner_system is NULL), enable their ports
1557 */
1558 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Jay Vosburghfd989c82008-11-04 17:51:16 -08001560 if (active) {
1561 if (!__agg_has_partner(active)) {
1562 for (port = active->lag_ports; port;
1563 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 __enable_port(port);
1565 }
1566 }
1567 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001568
dingtianhongbe79bd02013-12-13 10:20:12 +08001569 rcu_read_unlock();
1570
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001571 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574/**
1575 * ad_clear_agg - clear a given aggregator's parameters
1576 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 */
1578static void ad_clear_agg(struct aggregator *aggregator)
1579{
1580 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001581 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 aggregator->actor_admin_aggregator_key = 0;
1583 aggregator->actor_oper_aggregator_key = 0;
1584 aggregator->partner_system = null_mac_addr;
1585 aggregator->partner_system_priority = 0;
1586 aggregator->partner_oper_aggregator_key = 0;
1587 aggregator->receive_state = 0;
1588 aggregator->transmit_state = 0;
1589 aggregator->lag_ports = NULL;
1590 aggregator->is_active = 0;
1591 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001592 pr_debug("LAG %d was cleared\n",
1593 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595}
1596
1597/**
1598 * ad_initialize_agg - initialize a given aggregator's parameters
1599 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 */
1601static void ad_initialize_agg(struct aggregator *aggregator)
1602{
1603 if (aggregator) {
1604 ad_clear_agg(aggregator);
1605
1606 aggregator->aggregator_mac_address = null_mac_addr;
1607 aggregator->aggregator_identifier = 0;
1608 aggregator->slave = NULL;
1609 }
1610}
1611
1612/**
1613 * ad_initialize_port - initialize a given port's parameters
1614 * @aggregator: the aggregator we're looking at
1615 * @lacp_fast: boolean. whether fast periodic should be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
1617static void ad_initialize_port(struct port *port, int lacp_fast)
1618{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001619 static const struct port_params tmpl = {
1620 .system_priority = 0xffff,
1621 .key = 1,
1622 .port_number = 1,
1623 .port_priority = 0xff,
1624 .port_state = 1,
1625 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001626 static const struct lacpdu lacpdu = {
1627 .subtype = 0x01,
1628 .version_number = 0x01,
1629 .tlv_type_actor_info = 0x01,
1630 .actor_information_length = 0x14,
1631 .tlv_type_partner_info = 0x02,
1632 .partner_information_length = 0x14,
1633 .tlv_type_collector_info = 0x03,
1634 .collector_information_length = 0x10,
1635 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1636 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (port) {
1639 port->actor_port_number = 1;
1640 port->actor_port_priority = 0xff;
1641 port->actor_system = null_mac_addr;
1642 port->actor_system_priority = 0xffff;
1643 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001644 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 port->actor_admin_port_key = 1;
1646 port->actor_oper_port_key = 1;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001647 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1648 AD_STATE_LACP_ACTIVITY;
1649 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1650 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Bandan Das7bfc4752010-10-16 20:19:59 +00001652 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001655 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1656 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1657
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001658 port->is_enabled = true;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001659 /* private parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 port->sm_vars = 0x3;
1661 port->sm_rx_state = 0;
1662 port->sm_rx_timer_counter = 0;
1663 port->sm_periodic_state = 0;
1664 port->sm_periodic_timer_counter = 0;
1665 port->sm_mux_state = 0;
1666 port->sm_mux_timer_counter = 0;
1667 port->sm_tx_state = 0;
1668 port->sm_tx_timer_counter = 0;
1669 port->slave = NULL;
1670 port->aggregator = NULL;
1671 port->next_port_in_aggregator = NULL;
1672 port->transaction_id = 0;
1673
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001674 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676}
1677
1678/**
1679 * ad_enable_collecting_distributing - enable a port's transmit/receive
1680 * @port: the port we're looking at
1681 *
1682 * Enable @port if it's in an active aggregator
1683 */
1684static void ad_enable_collecting_distributing(struct port *port)
1685{
1686 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001687 pr_debug("Enabling port %d(LAG %d)\n",
1688 port->actor_port_number,
1689 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 __enable_port(port);
1691 }
1692}
1693
1694/**
1695 * ad_disable_collecting_distributing - disable a port's transmit/receive
1696 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
1698static void ad_disable_collecting_distributing(struct port *port)
1699{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001700 if (port->aggregator &&
1701 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1702 &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001703 pr_debug("Disabling port %d(LAG %d)\n",
1704 port->actor_port_number,
1705 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 __disable_port(port);
1707 }
1708}
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/**
1711 * ad_marker_info_received - handle receive of a Marker information frame
1712 * @marker_info: Marker info received
1713 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001715static void ad_marker_info_received(struct bond_marker *marker_info,
1716 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001718 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001720 /* copy the received marker data to the response marker */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001721 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001722 /* change the marker subtype to marker response */
Bandan Das128ea6c2010-10-16 20:19:58 +00001723 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001725 /* send the marker response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001727 pr_debug("Sent Marker Response on port %d\n",
1728 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730}
1731
1732/**
1733 * ad_marker_response_received - handle receive of a marker response frame
1734 * @marker: marker PDU received
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 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001741static void ad_marker_response_received(struct bond_marker *marker,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001742 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001744 marker = NULL;
1745 port = NULL;
1746 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001749/* ========= AD exported functions to the main bonding code ========= */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001751/* Check aggregators status in team every T seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752#define AD_AGGREGATOR_SELECTION_TIMER 8
1753
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001754/**
1755 * bond_3ad_initiate_agg_selection - initate aggregator selection
1756 * @bond: bonding struct
Jay Vosburghfd989c82008-11-04 17:51:16 -08001757 *
1758 * Set the aggregation selection timer, to initiate an agg selection in
1759 * the very near future. Called during first initialization, and during
1760 * any down to up transitions of the bond.
1761 */
1762void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1763{
1764 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1769 * @bond: bonding struct to work on
1770 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
1772 * Can be called only after the mac address of the bond is set.
1773 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001774void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001775{
dingtianhong815117a2014-01-02 09:12:54 +08001776 /* check that the bond is not initialized yet */
1777 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001778 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001780 BOND_AD_INFO(bond).aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1783 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1784
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001785 /* initialize how many times this module is called in one
1786 * second (should be about every 100ms)
1787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ad_ticks_per_sec = tick_resolution;
1789
Jay Vosburghfd989c82008-11-04 17:51:16 -08001790 bond_3ad_initiate_agg_selection(bond,
1791 AD_AGGREGATOR_SELECTION_TIMER *
1792 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794}
1795
1796/**
1797 * bond_3ad_bind_slave - initialize a slave's port
1798 * @slave: slave struct to work on
1799 *
1800 * Returns: 0 on success
1801 * < 0 on error
1802 */
dingtianhong359632e2014-01-02 09:13:12 +08001803void bond_3ad_bind_slave(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 struct bonding *bond = bond_get_bond_by_slave(slave);
1806 struct port *port;
1807 struct aggregator *aggregator;
1808
dingtianhong359632e2014-01-02 09:13:12 +08001809 /* check that the slave has not been initialized yet. */
dingtianhong3fdddd82014-05-12 15:08:43 +08001810 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
dingtianhong359632e2014-01-02 09:13:12 +08001812 /* port initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001813 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001815 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 port->slave = slave;
dingtianhong3fdddd82014-05-12 15:08:43 +08001818 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
dingtianhong359632e2014-01-02 09:13:12 +08001819 /* key is determined according to the link speed, duplex and user key(which
1820 * is yet not supported)
dingtianhong359632e2014-01-02 09:13:12 +08001821 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001822 port->actor_admin_port_key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 port->actor_admin_port_key |= __get_duplex(port);
1824 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1825 port->actor_oper_port_key = port->actor_admin_port_key;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001826 /* if the port is not full duplex, then the port should be not
1827 * lacp Enabled
1828 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001829 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
dingtianhong359632e2014-01-02 09:13:12 +08001831 /* actor system is the bond's system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001833 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
1834 * lacpdu's are sent in one second)
1835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1837 port->aggregator = NULL;
1838 port->next_port_in_aggregator = NULL;
1839
1840 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
dingtianhong359632e2014-01-02 09:13:12 +08001842 /* aggregator initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001843 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 ad_initialize_agg(aggregator);
1846
1847 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001848 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 aggregator->slave = slave;
1850 aggregator->is_active = 0;
1851 aggregator->num_of_ports = 0;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
1855/**
1856 * bond_3ad_unbind_slave - deinitialize a slave's port
1857 * @slave: slave struct to work on
1858 *
1859 * Search for the aggregator that is related to this port, remove the
1860 * aggregator and assign another aggregator for other port related to it
1861 * (if any), and remove the port.
1862 */
1863void bond_3ad_unbind_slave(struct slave *slave)
1864{
1865 struct port *port, *prev_port, *temp_port;
1866 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1867 int select_new_active_agg = 0;
Veaceslav Falico0b088262013-09-27 16:12:02 +02001868 struct bonding *bond = slave->bond;
1869 struct slave *slave_iter;
1870 struct list_head *iter;
Jasper Spaansa361c832009-10-23 04:09:24 +00001871
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001872 /* Sync against bond_3ad_state_machine_handler() */
1873 spin_lock_bh(&bond->mode_lock);
dingtianhong3fdddd82014-05-12 15:08:43 +08001874 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1875 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001877 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001879 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
1880 slave->dev->name);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001884 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
1885 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* Tell the partner that this port is not suitable for aggregation */
1888 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1889 __update_lacpdu_from_port(port);
1890 ad_lacpdu_send(port);
1891
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001892 /* check if this aggregator is occupied */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (aggregator->lag_ports) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001894 /* check if there are other ports related to this aggregator
1895 * except the port related to this slave(thats ensure us that
1896 * there is a reason to search for new aggregator, and that we
1897 * will find one
1898 */
1899 if ((aggregator->lag_ports != port) ||
1900 (aggregator->lag_ports->next_port_in_aggregator)) {
1901 /* find new aggregator for the related port(s) */
Veaceslav Falico0b088262013-09-27 16:12:02 +02001902 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001903 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001904 /* if the new aggregator is empty, or it is
1905 * connected to our port only
1906 */
1907 if (!new_aggregator->lag_ports ||
1908 ((new_aggregator->lag_ports == port) &&
1909 !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Veaceslav Falico0b088262013-09-27 16:12:02 +02001912 if (!slave_iter)
1913 new_aggregator = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001914
1915 /* if new aggregator found, copy the aggregator's
1916 * parameters and connect the related lag_ports to the
1917 * new aggregator
1918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001920 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
1921 aggregator->aggregator_identifier,
1922 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001924 if ((new_aggregator->lag_ports == port) &&
1925 new_aggregator->is_active) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001926 netdev_info(bond->dev, "Removing an active aggregator\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 select_new_active_agg = 1;
1928 }
1929
1930 new_aggregator->is_individual = aggregator->is_individual;
1931 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
1932 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
1933 new_aggregator->partner_system = aggregator->partner_system;
1934 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
1935 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
1936 new_aggregator->receive_state = aggregator->receive_state;
1937 new_aggregator->transmit_state = aggregator->transmit_state;
1938 new_aggregator->lag_ports = aggregator->lag_ports;
1939 new_aggregator->is_active = aggregator->is_active;
1940 new_aggregator->num_of_ports = aggregator->num_of_ports;
1941
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001942 /* update the information that is written on
1943 * the ports about the aggregator
1944 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001945 for (temp_port = aggregator->lag_ports; temp_port;
1946 temp_port = temp_port->next_port_in_aggregator) {
1947 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
1949 }
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00001952
Bandan Das7bfc4752010-10-16 20:19:59 +00001953 if (select_new_active_agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 ad_agg_selection_logic(__get_first_agg(port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001956 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001958 } else {
1959 /* in case that the only port related to this
1960 * aggregator is the one we want to remove
1961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 select_new_active_agg = aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ad_clear_agg(aggregator);
1964 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001965 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001966 /* select new active aggregator */
Veaceslav Falico74684492013-09-27 15:10:58 +02001967 temp_aggregator = __get_first_agg(port);
1968 if (temp_aggregator)
1969 ad_agg_selection_logic(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 }
1972 }
1973
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001974 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001975
1976 /* find the aggregator that this port is connected to */
Veaceslav Falico0b088262013-09-27 16:12:02 +02001977 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001978 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 prev_port = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001980 /* search the port in the aggregator's related ports */
Bandan Das128ea6c2010-10-16 20:19:58 +00001981 for (temp_port = temp_aggregator->lag_ports; temp_port;
1982 prev_port = temp_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001983 temp_port = temp_port->next_port_in_aggregator) {
1984 if (temp_port == port) {
1985 /* the aggregator found - detach the port from
1986 * this aggregator
1987 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001988 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00001990 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00001993 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 select_new_active_agg = temp_aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 ad_clear_agg(temp_aggregator);
1996 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001997 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001998 /* select new active aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 ad_agg_selection_logic(__get_first_agg(port));
2000 }
2001 }
2002 break;
2003 }
2004 }
2005 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002006 port->slave = NULL;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002007
2008out:
2009 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012/**
2013 * bond_3ad_state_machine_handler - handle state machines timeout
2014 * @bond: bonding struct to work on
2015 *
2016 * The state machine handling concept in this module is to check every tick
2017 * which state machine should operate any function. The execution order is
2018 * round robin, so when we have an interaction between state machines, the
2019 * reply of one to each other might be delayed until next tick.
2020 *
2021 * This function also complete the initialization when the agg_select_timer
2022 * times out, and it selects an aggregator for the ports that are yet not
2023 * related to any aggregator, and selects the active aggregator for a bond.
2024 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002025void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002027 struct bonding *bond = container_of(work, struct bonding,
2028 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002030 struct list_head *iter;
2031 struct slave *slave;
2032 struct port *port;
dingtianhong5e5b0662014-02-26 11:05:22 +08002033 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002035 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2036 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2037 * concurrently due to incoming LACPDU as well.
2038 */
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002039 spin_lock_bh(&bond->mode_lock);
dingtianhongbe79bd02013-12-13 10:20:12 +08002040 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04002041
dingtianhongbe79bd02013-12-13 10:20:12 +08002042 /* check if there are any slaves */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002043 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
dingtianhongbe79bd02013-12-13 10:20:12 +08002046 /* check if agg_select_timer timer after initialize is timed out */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002047 if (BOND_AD_INFO(bond).agg_select_timer &&
2048 !(--BOND_AD_INFO(bond).agg_select_timer)) {
dingtianhongbe79bd02013-12-13 10:20:12 +08002049 slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +08002050 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002051
dingtianhongbe79bd02013-12-13 10:20:12 +08002052 /* select the active aggregator for the bond */
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002053 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002055 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2056 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 goto re_arm;
2058 }
2059
2060 aggregator = __get_first_agg(port);
2061 ad_agg_selection_logic(aggregator);
2062 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002063 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
dingtianhongbe79bd02013-12-13 10:20:12 +08002066 /* for each port run the state machines */
2067 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002068 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002070 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
Veaceslav Falico86a2b9c2014-03-16 17:55:03 +01002071 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 goto re_arm;
2073 }
2074
2075 ad_rx_machine(NULL, port);
2076 ad_periodic_machine(port);
2077 ad_port_selection_logic(port);
2078 ad_mux_machine(port);
2079 ad_tx_machine(port);
2080
dingtianhongbe79bd02013-12-13 10:20:12 +08002081 /* turn off the BEGIN bit, since we already handled it */
Bandan Das7bfc4752010-10-16 20:19:59 +00002082 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 port->sm_vars &= ~AD_PORT_BEGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085
2086re_arm:
dingtianhong5e5b0662014-02-26 11:05:22 +08002087 bond_for_each_slave_rcu(bond, slave, iter) {
2088 if (slave->should_notify) {
2089 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2090 break;
2091 }
2092 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002093 rcu_read_unlock();
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002094 spin_unlock_bh(&bond->mode_lock);
dingtianhong5e5b0662014-02-26 11:05:22 +08002095
2096 if (should_notify_rtnl && rtnl_trylock()) {
dingtianhongb0929912014-02-26 11:05:23 +08002097 bond_slave_state_notify(bond);
dingtianhong5e5b0662014-02-26 11:05:22 +08002098 rtnl_unlock();
2099 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002100 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
2103/**
2104 * bond_3ad_rx_indication - handle a received frame
2105 * @lacpdu: received lacpdu
2106 * @slave: slave struct to work on
2107 * @length: length of the data received
2108 *
2109 * It is assumed that frames that were sent on this NIC don't returned as new
2110 * received frames (loopback). Since only the payload is given to this
2111 * function, it check for loopback.
2112 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002113static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2114 u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002117 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 if (length >= sizeof(struct lacpdu)) {
2120
dingtianhong3fdddd82014-05-12 15:08:43 +08002121 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002124 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2125 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128
2129 switch (lacpdu->subtype) {
2130 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002131 ret = RX_HANDLER_CONSUMED;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002132 netdev_dbg(slave->bond->dev, "Received LACPDU on port %d\n",
2133 port->actor_port_number);
Nils Carlson16d79d72011-03-03 22:09:11 +00002134 /* Protect against concurrent state machines */
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002135 spin_lock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 ad_rx_machine(lacpdu, port);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002137 spin_unlock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139
2140 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002141 ret = RX_HANDLER_CONSUMED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002142 /* No need to convert fields to Little Endian since we
2143 * don't use the marker's fields.
2144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002146 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 case AD_MARKER_INFORMATION_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002148 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2149 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002150 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 break;
2152
2153 case AD_MARKER_RESPONSE_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002154 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2155 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002156 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 break;
2158
2159 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002160 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2161 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 }
2164 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002165 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
2168/**
2169 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2170 * @slave: slave struct to work on
2171 *
2172 * Handle reselection of aggregator (if needed) for this port.
2173 */
2174void bond_3ad_adapter_speed_changed(struct slave *slave)
2175{
2176 struct port *port;
2177
dingtianhong3fdddd82014-05-12 15:08:43 +08002178 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
dingtianhong71a06c52013-12-13 17:29:19 +08002180 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002182 netdev_warn(slave->bond->dev, "speed changed for uninitialized port on %s\n",
2183 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return;
2185 }
2186
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002187 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong71a06c52013-12-13 17:29:19 +08002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002190 port->actor_oper_port_key = port->actor_admin_port_key |=
2191 (__get_link_speed(port) << 1);
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002192 netdev_dbg(slave->bond->dev, "Port %d changed speed\n", port->actor_port_number);
dingtianhong71a06c52013-12-13 17:29:19 +08002193 /* there is no need to reselect a new aggregator, just signal the
2194 * state machines to reinitialize
2195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 port->sm_vars |= AD_PORT_BEGIN;
dingtianhong71a06c52013-12-13 17:29:19 +08002197
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002198 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
2201/**
2202 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2203 * @slave: slave struct to work on
2204 *
2205 * Handle reselection of aggregator (if needed) for this port.
2206 */
2207void bond_3ad_adapter_duplex_changed(struct slave *slave)
2208{
2209 struct port *port;
2210
dingtianhong3fdddd82014-05-12 15:08:43 +08002211 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
dingtianhongbca44a72013-12-13 17:29:24 +08002213 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002215 netdev_warn(slave->bond->dev, "duplex changed for uninitialized port on %s\n",
2216 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return;
2218 }
2219
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002220 spin_lock_bh(&slave->bond->mode_lock);
dingtianhongbca44a72013-12-13 17:29:24 +08002221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002223 port->actor_oper_port_key = port->actor_admin_port_key |=
2224 __get_duplex(port);
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002225 netdev_dbg(slave->bond->dev, "Port %d changed duplex\n", port->actor_port_number);
dingtianhongbca44a72013-12-13 17:29:24 +08002226 /* there is no need to reselect a new aggregator, just signal the
2227 * state machines to reinitialize
2228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 port->sm_vars |= AD_PORT_BEGIN;
dingtianhongbca44a72013-12-13 17:29:24 +08002230
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002231 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234/**
2235 * bond_3ad_handle_link_change - handle a slave's link status change indication
2236 * @slave: slave struct to work on
2237 * @status: whether the link is now up or down
2238 *
2239 * Handle reselection of aggregator (if needed) for this port.
2240 */
2241void bond_3ad_handle_link_change(struct slave *slave, char link)
2242{
2243 struct port *port;
2244
dingtianhong3fdddd82014-05-12 15:08:43 +08002245 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
dingtianhong108db732013-12-13 17:29:29 +08002247 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002249 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2250 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return;
2252 }
2253
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002254 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong108db732013-12-13 17:29:29 +08002255 /* on link down we are zeroing duplex and speed since
2256 * some of the adaptors(ce1000.lan) report full duplex/speed
2257 * instead of N/A(duplex) / 0(speed).
2258 *
2259 * on link up we are forcing recheck on the duplex and speed since
2260 * some of he adaptors(ce1000.lan) report.
2261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002263 port->is_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002265 port->actor_oper_port_key = port->actor_admin_port_key |=
2266 __get_duplex(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002268 port->actor_oper_port_key = port->actor_admin_port_key |=
2269 (__get_link_speed(port) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 } else {
2271 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002272 port->is_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002274 port->actor_oper_port_key = (port->actor_admin_port_key &=
2275 ~AD_SPEED_KEY_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002277 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2278 port->actor_port_number,
2279 link == BOND_LINK_UP ? "UP" : "DOWN");
dingtianhong108db732013-12-13 17:29:29 +08002280 /* there is no need to reselect a new aggregator, just signal the
2281 * state machines to reinitialize
2282 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 port->sm_vars |= AD_PORT_BEGIN;
dingtianhong108db732013-12-13 17:29:29 +08002284
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002285 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002288/**
2289 * bond_3ad_set_carrier - set link state for bonding master
2290 * @bond - bonding structure
2291 *
2292 * if we have an active aggregator, we're up, if not, we're down.
2293 * Presumes that we cannot have an active aggregator if there are
2294 * no slaves with link up.
Jay Vosburghff59c452006-03-27 13:27:43 -08002295 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002296 * This behavior complies with IEEE 802.3 section 43.3.9.
2297 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002298 * Called by bond_set_carrier(). Return zero if carrier state does not
2299 * change, nonzero if it does.
2300 */
2301int bond_3ad_set_carrier(struct bonding *bond)
2302{
stephen hemminger655f8912011-06-22 09:54:39 +00002303 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002304 struct slave *first_slave;
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002305 int ret = 1;
stephen hemminger655f8912011-06-22 09:54:39 +00002306
dingtianhongbe79bd02013-12-13 10:20:12 +08002307 rcu_read_lock();
2308 first_slave = bond_first_slave_rcu(bond);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002309 if (!first_slave) {
2310 ret = 0;
2311 goto out;
2312 }
dingtianhong3fdddd82014-05-12 15:08:43 +08002313 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002314 if (active) {
2315 /* are enough slaves available to consider link up? */
2316 if (active->num_of_ports < bond->params.min_links) {
2317 if (netif_carrier_ok(bond->dev)) {
2318 netif_carrier_off(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002319 goto out;
stephen hemminger655f8912011-06-22 09:54:39 +00002320 }
2321 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002322 netif_carrier_on(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002323 goto out;
Jay Vosburghff59c452006-03-27 13:27:43 -08002324 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002325 } else if (netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002326 netif_carrier_off(bond->dev);
Jay Vosburghff59c452006-03-27 13:27:43 -08002327 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002328out:
2329 rcu_read_unlock();
2330 return ret;
Jay Vosburghff59c452006-03-27 13:27:43 -08002331}
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002334 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 * @bond: bonding struct to work on
2336 * @ad_info: ad_info struct to fill with the bond's info
2337 *
2338 * Returns: 0 on success
2339 * < 0 on error
2340 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002341int __bond_3ad_get_active_agg_info(struct bonding *bond,
2342 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002345 struct list_head *iter;
2346 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 struct port *port;
2348
dingtianhong47e91f562013-10-15 16:28:35 +08002349 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002350 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (port->aggregator && port->aggregator->is_active) {
2352 aggregator = port->aggregator;
2353 break;
2354 }
2355 }
2356
Joe Perches21f374c2014-02-18 09:42:47 -08002357 if (!aggregator)
2358 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Joe Perches21f374c2014-02-18 09:42:47 -08002360 ad_info->aggregator_id = aggregator->aggregator_identifier;
2361 ad_info->ports = aggregator->num_of_ports;
2362 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2363 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2364 ether_addr_copy(ad_info->partner_system,
2365 aggregator->partner_system.mac_addr_value);
2366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002369int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2370{
2371 int ret;
2372
dingtianhong47e91f562013-10-15 16:28:35 +08002373 rcu_read_lock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002374 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
dingtianhong47e91f562013-10-15 16:28:35 +08002375 rcu_read_unlock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002376
2377 return ret;
2378}
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2381{
Wang Chen454d7c92008-11-12 23:37:49 -08002382 struct bonding *bond = netdev_priv(dev);
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002383 struct slave *slave, *first_ok_slave;
2384 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 struct ad_info ad_info;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002386 struct list_head *iter;
2387 int slaves_in_agg;
2388 int slave_agg_no;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002389 int agg_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002391 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002392 netdev_dbg(dev, "__bond_3ad_get_active_agg_info failed\n");
dingtianhonga742e1f2014-01-02 09:12:59 +08002393 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395
2396 slaves_in_agg = ad_info.ports;
2397 agg_id = ad_info.aggregator_id;
2398
2399 if (slaves_in_agg == 0) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002400 netdev_dbg(dev, "active aggregator is empty\n");
dingtianhonga742e1f2014-01-02 09:12:59 +08002401 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403
Mahesh Bandewaree62e862014-04-22 16:30:15 -07002404 slave_agg_no = bond_xmit_hash(bond, skb) % slaves_in_agg;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002405 first_ok_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
dingtianhong47e91f562013-10-15 16:28:35 +08002407 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002408 agg = SLAVE_AD_INFO(slave)->port.aggregator;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002409 if (!agg || agg->aggregator_identifier != agg_id)
2410 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002412 if (slave_agg_no >= 0) {
Veaceslav Falico8557cd72014-05-15 21:39:59 +02002413 if (!first_ok_slave && bond_slave_can_tx(slave))
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002414 first_ok_slave = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 slave_agg_no--;
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002416 continue;
2417 }
2418
Veaceslav Falico8557cd72014-05-15 21:39:59 +02002419 if (bond_slave_can_tx(slave)) {
dingtianhonga742e1f2014-01-02 09:12:59 +08002420 bond_dev_queue_xmit(bond, skb, slave->dev);
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002421 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423 }
2424
2425 if (slave_agg_no >= 0) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002426 netdev_err(dev, "Couldn't find a slave to tx on for aggregator ID %d\n",
2427 agg_id);
dingtianhonga742e1f2014-01-02 09:12:59 +08002428 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002431 /* we couldn't find any suitable slave after the agg_no, so use the
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002432 * first suitable found, if found.
2433 */
Veaceslav Falicoc33d7882013-09-25 09:20:16 +02002434 if (first_ok_slave)
dingtianhonga742e1f2014-01-02 09:12:59 +08002435 bond_dev_queue_xmit(bond, skb, first_ok_slave->dev);
2436 else
2437 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439out:
Patrick McHardyec634fe2009-07-05 19:23:38 -07002440 return NETDEV_TX_OK;
dingtianhonga742e1f2014-01-02 09:12:59 +08002441err_free:
2442 /* no suitable interface, frame not sent */
Eric W. Biederman2bb77ab2014-03-11 14:16:58 -07002443 dev_kfree_skb_any(skb);
dingtianhonga742e1f2014-01-02 09:12:59 +08002444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Eric Dumazetde063b72012-06-11 19:23:07 +00002447int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2448 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Eric Dumazetde063b72012-06-11 19:23:07 +00002450 struct lacpdu *lacpdu, _lacpdu;
2451
Jiri Pirko3aba8912011-04-19 03:48:16 +00002452 if (skb->protocol != PKT_TYPE_LACPDU)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002453 return RX_HANDLER_ANOTHER;
Neil Hormanb3053252011-01-20 09:02:31 +00002454
Eric Dumazetde063b72012-06-11 19:23:07 +00002455 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2456 if (!lacpdu)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002457 return RX_HANDLER_ANOTHER;
Andy Gospodarekab128112010-09-10 11:43:20 +00002458
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002459 return bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002461
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002462/**
2463 * bond_3ad_update_lacp_rate - change the lacp rate
2464 * @bond - bonding struct
2465 *
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002466 * When modify lacp_rate parameter via sysfs,
2467 * update actor_oper_port_state of each port.
2468 *
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002469 * Hold bond->mode_lock,
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002470 * so we can modify port->actor_oper_port_state,
2471 * no matter bond is up or down.
2472 */
2473void bond_3ad_update_lacp_rate(struct bonding *bond)
2474{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002475 struct port *port = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002476 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002477 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002478 int lacp_fast;
2479
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002480 lacp_fast = bond->params.lacp_fast;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002481 spin_lock_bh(&bond->mode_lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002482 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002483 port = &(SLAVE_AD_INFO(slave)->port);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002484 if (lacp_fast)
2485 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2486 else
2487 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002488 }
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002489 spin_unlock_bh(&bond->mode_lock);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002490}