blob: 940e2ebbdea81ef459cffa1128361816f7d309f1 [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>
David S. Miller1ef80192014-11-10 13:27:49 -050032#include <net/bonding.h>
33#include <net/bond_3ad.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
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
Mahesh Bandewaref015d72015-03-30 14:30:40 -070073#define AD_PORT_CHURNED (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010075/* Port Key definitions
76 * key is determined according to the link speed, duplex and
77 * user key (which is yet not supported)
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -070078 * --------------------------------------------------------------
79 * Port key | User key (10 bits) | Speed (5 bits) | Duplex|
80 * --------------------------------------------------------------
81 * |15 6|5 1|0
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010082 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +080083#define AD_DUPLEX_KEY_MASKS 0x1
84#define AD_SPEED_KEY_MASKS 0x3E
85#define AD_USER_KEY_MASKS 0xFFC0
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jianhua Xiecb8dda92014-11-19 16:48:58 +080087enum ad_link_speed_type {
88 AD_LINK_SPEED_1MBPS = 1,
89 AD_LINK_SPEED_10MBPS,
90 AD_LINK_SPEED_100MBPS,
91 AD_LINK_SPEED_1000MBPS,
Jianhua Xie424c3232014-11-19 16:48:59 +080092 AD_LINK_SPEED_2500MBPS,
93 AD_LINK_SPEED_10000MBPS,
94 AD_LINK_SPEED_20000MBPS,
95 AD_LINK_SPEED_40000MBPS,
96 AD_LINK_SPEED_56000MBPS
Jianhua Xiecb8dda92014-11-19 16:48:58 +080097};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
dingtianhong815117a2014-01-02 09:12:54 +080099/* compare MAC addresses */
100#define MAC_ADDRESS_EQUAL(A, B) \
101 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Bandan Das128ea6c2010-10-16 20:19:58 +0000103static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static u16 ad_ticks_per_sec;
105static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
106
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800107static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
108
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100109/* ================= main 802.3ad protocol functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int ad_lacpdu_send(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700111static int ad_marker_send(struct port *port, struct bond_marker *marker);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700112static void ad_mux_machine(struct port *port, bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
114static void ad_tx_machine(struct port *port);
115static void ad_periodic_machine(struct port *port);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700116static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
117static void ad_agg_selection_logic(struct aggregator *aggregator,
118 bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void ad_clear_agg(struct aggregator *aggregator);
120static void ad_initialize_agg(struct aggregator *aggregator);
121static void ad_initialize_port(struct port *port, int lacp_fast);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700122static void ad_enable_collecting_distributing(struct port *port,
123 bool *update_slave_arr);
124static void ad_disable_collecting_distributing(struct port *port,
125 bool *update_slave_arr);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100126static void ad_marker_info_received(struct bond_marker *marker_info,
127 struct port *port);
128static void ad_marker_response_received(struct bond_marker *marker,
129 struct port *port);
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -0700130static void ad_update_actor_keys(struct port *port, bool reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100133/* ================= api to bonding and kernel code ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/**
136 * __get_bond_by_port - get the port's bonding struct
137 * @port: the port we're looking at
138 *
139 * Return @port's bonding struct, or %NULL if it can't be found.
140 */
141static inline struct bonding *__get_bond_by_port(struct port *port)
142{
Bandan Das7bfc4752010-10-16 20:19:59 +0000143 if (port->slave == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return bond_get_bond_by_slave(port->slave);
147}
148
149/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * __get_first_agg - get the first aggregator in the bond
151 * @bond: the bond we're looking at
152 *
153 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
154 * found.
Veaceslav Falico768b9542014-01-10 11:59:44 +0100155 * The caller must hold RCU or RTNL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157static inline struct aggregator *__get_first_agg(struct port *port)
158{
159 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200160 struct slave *first_slave;
Veaceslav Falico768b9542014-01-10 11:59:44 +0100161 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
dingtianhongbe79bd02013-12-13 10:20:12 +0800163 /* If there's no bond for this port, or bond has no slaves */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200164 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100166
dingtianhongbe79bd02013-12-13 10:20:12 +0800167 rcu_read_lock();
168 first_slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +0800169 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
dingtianhongbe79bd02013-12-13 10:20:12 +0800170 rcu_read_unlock();
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100171
Veaceslav Falico768b9542014-01-10 11:59:44 +0100172 return agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100175/**
176 * __agg_has_partner - see if we have a partner
177 * @agg: the agregator we're looking at
Jay Vosburghfd989c82008-11-04 17:51:16 -0800178 *
179 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100180 * address for the partner). Return 0 if not.
Jay Vosburghfd989c82008-11-04 17:51:16 -0800181 */
182static inline int __agg_has_partner(struct aggregator *agg)
183{
184 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/**
188 * __disable_port - disable the port's slave
189 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191static inline void __disable_port(struct port *port)
192{
dingtianhong5e5b0662014-02-26 11:05:22 +0800193 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
197 * __enable_port - enable the port's slave, if it's up
198 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
200static inline void __enable_port(struct port *port)
201{
202 struct slave *slave = port->slave;
203
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200204 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
dingtianhong5e5b0662014-02-26 11:05:22 +0800205 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/**
209 * __port_is_enabled - check if the port's slave is in active state
210 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
212static inline int __port_is_enabled(struct port *port)
213{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000214 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/**
218 * __get_agg_selection_mode - get the aggregator selection mode
219 * @port: the port we're looking at
220 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800221 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
223static inline u32 __get_agg_selection_mode(struct port *port)
224{
225 struct bonding *bond = __get_bond_by_port(port);
226
Bandan Das7bfc4752010-10-16 20:19:59 +0000227 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800228 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000230 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233/**
234 * __check_agg_selection_timer - check if the selection timer has expired
235 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237static inline int __check_agg_selection_timer(struct port *port)
238{
239 struct bonding *bond = __get_bond_by_port(port);
240
Bandan Das7bfc4752010-10-16 20:19:59 +0000241 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
245}
246
247/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * __get_link_speed - get a port's speed
249 * @port: the port we're looking at
250 *
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800251 * Return @port's speed in 802.3ad enum format. i.e. one of:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * 0,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800253 * %AD_LINK_SPEED_10MBPS,
254 * %AD_LINK_SPEED_100MBPS,
255 * %AD_LINK_SPEED_1000MBPS,
Jianhua Xie424c3232014-11-19 16:48:59 +0800256 * %AD_LINK_SPEED_2500MBPS,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800257 * %AD_LINK_SPEED_10000MBPS
Jianhua Xie424c3232014-11-19 16:48:59 +0800258 * %AD_LINK_SPEED_20000MBPS
259 * %AD_LINK_SPEED_40000MBPS
260 * %AD_LINK_SPEED_56000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262static u16 __get_link_speed(struct port *port)
263{
264 struct slave *slave = port->slave;
265 u16 speed;
266
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100267 /* this if covers only a special case: when the configuration starts
268 * with link down, it sets the speed to 0.
269 * This is done in spite of the fact that the e100 driver reports 0
270 * to be compatible with MVT in the future.
271 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000272 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000273 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000274 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 switch (slave->speed) {
276 case SPEED_10:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800277 speed = AD_LINK_SPEED_10MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279
280 case SPEED_100:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800281 speed = AD_LINK_SPEED_100MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283
284 case SPEED_1000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800285 speed = AD_LINK_SPEED_1000MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287
Jianhua Xie424c3232014-11-19 16:48:59 +0800288 case SPEED_2500:
289 speed = AD_LINK_SPEED_2500MBPS;
290 break;
291
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700292 case SPEED_10000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800293 speed = AD_LINK_SPEED_10000MBPS;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700294 break;
295
Jianhua Xie424c3232014-11-19 16:48:59 +0800296 case SPEED_20000:
297 speed = AD_LINK_SPEED_20000MBPS;
298 break;
299
300 case SPEED_40000:
301 speed = AD_LINK_SPEED_40000MBPS;
302 break;
303
304 case SPEED_56000:
305 speed = AD_LINK_SPEED_56000MBPS;
306 break;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100309 /* unknown speed value from ethtool. shouldn't happen */
310 speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 }
313 }
314
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200315 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
316 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return speed;
318}
319
320/**
321 * __get_duplex - get a port's duplex
322 * @port: the port we're looking at
323 *
324 * Return @port's duplex in 802.3ad bitmask format. i.e.:
325 * 0x01 if in full duplex
326 * 0x00 otherwise
327 */
328static u8 __get_duplex(struct port *port)
329{
330 struct slave *slave = port->slave;
Mahesh Bandewarb25c2e72015-10-31 12:45:00 -0700331 u8 retval = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100333 /* handling a special case: when the configuration starts with
334 * link down, it sets the duplex to 0.
335 */
Mahesh Bandewarb25c2e72015-10-31 12:45:00 -0700336 if (slave->link == BOND_LINK_UP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 switch (slave->duplex) {
338 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000339 retval = 0x1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200340 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
341 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343 case DUPLEX_HALF:
344 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000345 retval = 0x0;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200346 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
347 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 }
350 }
351 return retval;
352}
353
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100354/* Conversions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356/**
357 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
358 * @timer_type: which timer to operate
359 * @par: timer parameter. see below
360 *
361 * If @timer_type is %current_while_timer, @par indicates long/short timer.
362 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100363 * %SLOW_PERIODIC_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
366{
Bandan Das128ea6c2010-10-16 20:19:58 +0000367 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 switch (timer_type) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100370 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
Bandan Das7bfc4752010-10-16 20:19:59 +0000371 if (par)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100372 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
Bandan Das7bfc4752010-10-16 20:19:59 +0000373 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100374 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100376 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
378 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100379 case AD_PERIODIC_TIMER: /* for periodic machine */
380 retval = (par*ad_ticks_per_sec); /* long timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100382 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
384 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100385 case AD_WAIT_WHILE_TIMER: /* for selection machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
387 break;
388 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return retval;
391}
392
393
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100394/* ================= ad_rx_machine helper functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000397 * __choose_matched - update a port's matched variable from a received lacpdu
398 * @lacpdu: the lacpdu we've received
399 * @port: the port we're looking at
400 *
401 * Update the value of the matched variable, using parameter values from a
402 * newly received lacpdu. Parameter values for the partner carried in the
403 * received PDU are compared with the corresponding operational parameter
404 * values for the actor. Matched is set to TRUE if all of these parameters
405 * match and the PDU parameter partner_state.aggregation has the same value as
406 * actor_oper_port_state.aggregation and lacp will actively maintain the link
407 * in the aggregation. Matched is also set to TRUE if the value of
408 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
409 * an individual link and lacp will actively maintain the link. Otherwise,
410 * matched is set to FALSE. LACP is considered to be actively maintaining the
411 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
412 * the actor's actor_oper_port_state.lacp_activity and the PDU's
413 * partner_state.lacp_activity variables are TRUE.
414 *
415 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
416 * used here to implement the language from 802.3ad 43.4.9 that requires
417 * recordPDU to "match" the LACPDU parameters to the stored values.
418 */
419static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
420{
dingtianhong815117a2014-01-02 09:12:54 +0800421 /* check if all parameters are alike
422 * or this is individual link(aggregation == FALSE)
423 * then update the state machine Matched variable.
424 */
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000425 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
426 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
dingtianhong815117a2014-01-02 09:12:54 +0800427 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000428 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
429 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
430 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000431 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
432 ) {
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000433 port->sm_vars |= AD_PORT_MATCHED;
434 } else {
435 port->sm_vars &= ~AD_PORT_MATCHED;
436 }
437}
438
439/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * __record_pdu - record parameters from a received lacpdu
441 * @lacpdu: the lacpdu we've received
442 * @port: the port we're looking at
443 *
444 * Record the parameter values for the Actor carried in a received lacpdu as
445 * the current partner operational parameter values and sets
446 * actor_oper_port_state.defaulted to FALSE.
447 */
448static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
449{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800451 struct port_params *partner = &port->partner_oper;
452
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000453 __choose_matched(lacpdu, port);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100454 /* record the new parameter values for the partner
455 * operational
456 */
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800457 partner->port_number = ntohs(lacpdu->actor_port);
458 partner->port_priority = ntohs(lacpdu->actor_port_priority);
459 partner->system = lacpdu->actor_system;
460 partner->system_priority = ntohs(lacpdu->actor_system_priority);
461 partner->key = ntohs(lacpdu->actor_key);
462 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100464 /* set actor_oper_port_state.defaulted to FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
466
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100467 /* set the partner sync. to on if the partner is sync,
468 * and the port is matched
469 */
Wilson Kok63b46242015-01-26 01:16:59 -0500470 if ((port->sm_vars & AD_PORT_MATCHED) &&
471 (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800472 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Wilson Kok63b46242015-01-26 01:16:59 -0500473 pr_debug("%s partner sync=1\n", port->slave->dev->name);
474 } else {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800475 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Wilson Kok63b46242015-01-26 01:16:59 -0500476 pr_debug("%s partner sync=0\n", port->slave->dev->name);
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479}
480
481/**
482 * __record_default - record default parameters
483 * @port: the port we're looking at
484 *
485 * This function records the default parameter values for the partner carried
486 * in the Partner Admin parameters as the current partner operational parameter
487 * values and sets actor_oper_port_state.defaulted to TRUE.
488 */
489static void __record_default(struct port *port)
490{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (port) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100492 /* record the partner admin parameters */
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800493 memcpy(&port->partner_oper, &port->partner_admin,
494 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100496 /* set actor_oper_port_state.defaulted to true */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
498 }
499}
500
501/**
502 * __update_selected - update a port's Selected variable from a received lacpdu
503 * @lacpdu: the lacpdu we've received
504 * @port: the port we're looking at
505 *
506 * Update the value of the selected variable, using parameter values from a
507 * newly received lacpdu. The parameter values for the Actor carried in the
508 * received PDU are compared with the corresponding operational parameter
509 * values for the ports partner. If one or more of the comparisons shows that
510 * the value(s) received in the PDU differ from the current operational values,
511 * then selected is set to FALSE and actor_oper_port_state.synchronization is
512 * set to out_of_sync. Otherwise, selected remains unchanged.
513 */
514static void __update_selected(struct lacpdu *lacpdu, struct port *port)
515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800517 const struct port_params *partner = &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 (ntohs(lacpdu->actor_port) != partner->port_number ||
523 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800524 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000525 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
526 ntohs(lacpdu->actor_key) != partner->key ||
527 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 port->sm_vars &= ~AD_PORT_SELECTED;
529 }
530 }
531}
532
533/**
534 * __update_default_selected - update a port's Selected variable from Partner
535 * @port: the port we're looking at
536 *
537 * This function updates the value of the selected variable, using the partner
538 * administrative parameter values. The administrative values are compared with
539 * the corresponding operational parameter values for the partner. If one or
540 * more of the comparisons shows that the administrative value(s) differ from
541 * the current operational values, then Selected is set to FALSE and
542 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
543 * Selected remains unchanged.
544 */
545static void __update_default_selected(struct port *port)
546{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800548 const struct port_params *admin = &port->partner_admin;
549 const struct port_params *oper = &port->partner_oper;
550
dingtianhong815117a2014-01-02 09:12:54 +0800551 /* check if any parameter is different then
552 * update the state machine selected variable.
553 */
Joe Perches8e95a202009-12-03 07:58:21 +0000554 if (admin->port_number != oper->port_number ||
555 admin->port_priority != oper->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800556 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000557 admin->system_priority != oper->system_priority ||
558 admin->key != oper->key ||
559 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800560 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 port->sm_vars &= ~AD_PORT_SELECTED;
562 }
563 }
564}
565
566/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * __update_ntt - update a port's ntt variable from a received lacpdu
568 * @lacpdu: the lacpdu we've received
569 * @port: the port we're looking at
570 *
571 * Updates the value of the ntt variable, using parameter values from a newly
572 * received lacpdu. The parameter values for the partner carried in the
573 * received PDU are compared with the corresponding operational parameter
574 * values for the Actor. If one or more of the comparisons shows that the
575 * value(s) received in the PDU differ from the current operational values,
576 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
577 */
578static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
579{
dingtianhong815117a2014-01-02 09:12:54 +0800580 /* validate lacpdu and port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (lacpdu && port) {
dingtianhong815117a2014-01-02 09:12:54 +0800582 /* check if any parameter is different then
583 * update the port->ntt.
584 */
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700585 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
586 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
dingtianhong815117a2014-01-02 09:12:54 +0800587 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700588 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
589 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
591 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
592 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
593 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
594 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800595 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 }
598}
599
600/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * __agg_ports_are_ready - check if all ports in an aggregator are ready
602 * @aggregator: the aggregator we're looking at
603 *
604 */
605static int __agg_ports_are_ready(struct aggregator *aggregator)
606{
607 struct port *port;
608 int retval = 1;
609
610 if (aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100611 /* scan all ports in this aggregator to verfy if they are
612 * all ready.
613 */
Bandan Das128ea6c2010-10-16 20:19:58 +0000614 for (port = aggregator->lag_ports;
615 port;
616 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!(port->sm_vars & AD_PORT_READY_N)) {
618 retval = 0;
619 break;
620 }
621 }
622 }
623
624 return retval;
625}
626
627/**
628 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
629 * @aggregator: the aggregator we're looking at
630 * @val: Should the ports' ready bit be set on or off
631 *
632 */
633static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
634{
635 struct port *port;
636
Bandan Das128ea6c2010-10-16 20:19:58 +0000637 for (port = aggregator->lag_ports; port;
638 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000639 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000641 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644}
645
646/**
647 * __get_agg_bandwidth - get the total bandwidth of an aggregator
648 * @aggregator: the aggregator we're looking at
649 *
650 */
651static u32 __get_agg_bandwidth(struct aggregator *aggregator)
652{
Bandan Das128ea6c2010-10-16 20:19:58 +0000653 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (aggregator->num_of_ports) {
David Decotigny65cce192011-04-13 15:22:30 +0000656 switch (__get_link_speed(aggregator->lag_ports)) {
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800657 case AD_LINK_SPEED_1MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 bandwidth = aggregator->num_of_ports;
659 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800660 case AD_LINK_SPEED_10MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 bandwidth = aggregator->num_of_ports * 10;
662 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800663 case AD_LINK_SPEED_100MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 bandwidth = aggregator->num_of_ports * 100;
665 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800666 case AD_LINK_SPEED_1000MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 bandwidth = aggregator->num_of_ports * 1000;
668 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800669 case AD_LINK_SPEED_2500MBPS:
670 bandwidth = aggregator->num_of_ports * 2500;
671 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800672 case AD_LINK_SPEED_10000MBPS:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700673 bandwidth = aggregator->num_of_ports * 10000;
674 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800675 case AD_LINK_SPEED_20000MBPS:
676 bandwidth = aggregator->num_of_ports * 20000;
677 break;
678 case AD_LINK_SPEED_40000MBPS:
679 bandwidth = aggregator->num_of_ports * 40000;
680 break;
681 case AD_LINK_SPEED_56000MBPS:
682 bandwidth = aggregator->num_of_ports * 56000;
683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100685 bandwidth = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 }
688 return bandwidth;
689}
690
691/**
692 * __get_active_agg - get the current active aggregator
693 * @aggregator: the aggregator we're looking at
Veaceslav Falico49b76242014-01-10 11:59:45 +0100694 *
695 * Caller must hold RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
697static struct aggregator *__get_active_agg(struct aggregator *aggregator)
698{
Veaceslav Falico19177e72013-09-27 16:12:00 +0200699 struct bonding *bond = aggregator->slave->bond;
700 struct list_head *iter;
701 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
dingtianhongbe79bd02013-12-13 10:20:12 +0800703 bond_for_each_slave_rcu(bond, slave, iter)
dingtianhong3fdddd82014-05-12 15:08:43 +0800704 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
705 return &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Veaceslav Falico19177e72013-09-27 16:12:00 +0200707 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/**
711 * __update_lacpdu_from_port - update a port's lacpdu fields
712 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
714static inline void __update_lacpdu_from_port(struct port *port)
715{
716 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800717 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100719 /* update current actual Actor parameters
720 * lacpdu->subtype initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * lacpdu->version_number initialized
722 * lacpdu->tlv_type_actor_info initialized
723 * lacpdu->actor_information_length initialized
724 */
725
Al Virod3bb52b2007-08-22 20:06:58 -0400726 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400728 lacpdu->actor_key = htons(port->actor_oper_port_key);
729 lacpdu->actor_port_priority = htons(port->actor_port_priority);
730 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 lacpdu->actor_state = port->actor_oper_port_state;
Wilson Kok63b46242015-01-26 01:16:59 -0500732 pr_debug("update lacpdu: %s, actor port state %x\n",
733 port->slave->dev->name, port->actor_oper_port_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* lacpdu->reserved_3_1 initialized
736 * lacpdu->tlv_type_partner_info initialized
737 * lacpdu->partner_information_length initialized
738 */
739
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800740 lacpdu->partner_system_priority = htons(partner->system_priority);
741 lacpdu->partner_system = partner->system;
742 lacpdu->partner_key = htons(partner->key);
743 lacpdu->partner_port_priority = htons(partner->port_priority);
744 lacpdu->partner_port = htons(partner->port_number);
745 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* lacpdu->reserved_3_2 initialized
748 * lacpdu->tlv_type_collector_info initialized
749 * lacpdu->collector_information_length initialized
750 * collector_max_delay initialized
751 * reserved_12[12] initialized
752 * tlv_type_terminator initialized
753 * terminator_length initialized
754 * reserved_50[50] initialized
755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100758/* ================= main 802.3ad protocol code ========================= */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760/**
761 * ad_lacpdu_send - send out a lacpdu packet on a given port
762 * @port: the port we're looking at
763 *
764 * Returns: 0 on success
765 * < 0 on error
766 */
767static int ad_lacpdu_send(struct port *port)
768{
769 struct slave *slave = port->slave;
770 struct sk_buff *skb;
771 struct lacpdu_header *lacpdu_header;
772 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000775 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700779 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700780 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 skb->protocol = PKT_TYPE_LACPDU;
782 skb->priority = TC_PRIO_CONTROL;
783
784 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
785
Joe Perchesada0f862014-02-15 16:02:17 -0800786 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400787 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100788 * because we use it to identify loopback lacpdus in receive.
789 */
Joe Perchesada0f862014-02-15 16:02:17 -0800790 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800791 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100793 lacpdu_header->lacpdu = port->lacpdu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 dev_queue_xmit(skb);
796
797 return 0;
798}
799
800/**
801 * ad_marker_send - send marker information/response on a given port
802 * @port: the port we're looking at
803 * @marker: marker data to send
804 *
805 * Returns: 0 on success
806 * < 0 on error
807 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700808static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct slave *slave = port->slave;
811 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700812 struct bond_marker_header *marker_header;
813 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000816 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 skb_reserve(skb, 16);
820
821 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700822 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700823 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 skb->protocol = PKT_TYPE_LACPDU;
825
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700826 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Joe Perchesada0f862014-02-15 16:02:17 -0800828 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400829 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100830 * because we use it to identify loopback MARKERs in receive.
831 */
Joe Perchesada0f862014-02-15 16:02:17 -0800832 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800833 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100835 marker_header->marker = *marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 dev_queue_xmit(skb);
838
839 return 0;
840}
841
842/**
843 * ad_mux_machine - handle a port's mux state machine
844 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -0700845 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Mahesh Bandewaree637712014-10-04 17:45:01 -0700847static void ad_mux_machine(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 mux_states_t last_state;
850
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100851 /* keep current State Machine state to compare later if it was
852 * changed
853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 last_state = port->sm_mux_state;
855
856 if (port->sm_vars & AD_PORT_BEGIN) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100857 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 } else {
859 switch (port->sm_mux_state) {
860 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000861 if ((port->sm_vars & AD_PORT_SELECTED)
862 || (port->sm_vars & AD_PORT_STANDBY))
863 /* if SELECTED or STANDBY */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100864 port->sm_mux_state = AD_MUX_WAITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866 case AD_MUX_WAITING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100867 /* if SELECTED == FALSE return to DETACH state */
868 if (!(port->sm_vars & AD_PORT_SELECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100870 /* in order to withhold the Selection Logic to
871 * check all ports READY_N value every callback
872 * cycle to update ready variable, we check
873 * READY_N and update READY here
874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100876 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 }
879
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100880 /* check if the wait_while_timer expired */
Bandan Das7bfc4752010-10-16 20:19:59 +0000881 if (port->sm_mux_timer_counter
882 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100885 /* in order to withhold the selection logic to check
886 * all ports READY_N value every callback cycle to
887 * update ready variable, we check READY_N and update
888 * READY here
889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
891
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100892 /* if the wait_while_timer expired, and the port is
893 * in READY state, move to ATTACHED state
894 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000895 if ((port->sm_vars & AD_PORT_READY)
896 && !port->sm_mux_timer_counter)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100897 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 case AD_MUX_ATTACHED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100900 /* check also if agg_select_timer expired (so the
901 * edable port will take place only after this timer)
902 */
903 if ((port->sm_vars & AD_PORT_SELECTED) &&
904 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
905 !__check_agg_selection_timer(port)) {
Wilson Kok63b46242015-01-26 01:16:59 -0500906 if (port->aggregator->is_active)
907 port->sm_mux_state =
908 AD_MUX_COLLECTING_DISTRIBUTING;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100909 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
910 (port->sm_vars & AD_PORT_STANDBY)) {
911 /* if UNSELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100913 /* in order to withhold the selection logic to
914 * check all ports READY_N value every callback
915 * cycle to update ready variable, we check
916 * READY_N and update READY here
917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100919 port->sm_mux_state = AD_MUX_DETACHED;
Wilson Kok63b46242015-01-26 01:16:59 -0500920 } else if (port->aggregator->is_active) {
921 port->actor_oper_port_state |=
922 AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 break;
925 case AD_MUX_COLLECTING_DISTRIBUTING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100926 if (!(port->sm_vars & AD_PORT_SELECTED) ||
927 (port->sm_vars & AD_PORT_STANDBY) ||
Wilson Kok63b46242015-01-26 01:16:59 -0500928 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) ||
929 !(port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100930 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100932 /* if port state hasn't changed make
933 * sure that a collecting distributing
934 * port in an active aggregator is enabled
935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (port->aggregator &&
937 port->aggregator->is_active &&
938 !__port_is_enabled(port)) {
939
940 __enable_port(port);
941 }
942 }
943 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100944 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
946 }
947 }
948
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100949 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (port->sm_mux_state != last_state) {
Wilson Kok63b46242015-01-26 01:16:59 -0500951 pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
952 port->actor_port_number,
953 port->slave->dev->name,
954 last_state,
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800955 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 switch (port->sm_mux_state) {
957 case AD_MUX_DETACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700959 ad_disable_collecting_distributing(port,
960 update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
962 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800963 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
965 case AD_MUX_WAITING:
966 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
967 break;
968 case AD_MUX_ATTACHED:
Wilson Kok63b46242015-01-26 01:16:59 -0500969 if (port->aggregator->is_active)
970 port->actor_oper_port_state |=
971 AD_STATE_SYNCHRONIZATION;
972 else
973 port->actor_oper_port_state &=
974 ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
976 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700977 ad_disable_collecting_distributing(port,
978 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800979 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981 case AD_MUX_COLLECTING_DISTRIBUTING:
982 port->actor_oper_port_state |= AD_STATE_COLLECTING;
983 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
Wilson Kok63b46242015-01-26 01:16:59 -0500984 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700985 ad_enable_collecting_distributing(port,
986 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800987 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100989 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 }
992 }
993}
994
995/**
996 * ad_rx_machine - handle a port's rx State Machine
997 * @lacpdu: the lacpdu we've received
998 * @port: the port we're looking at
999 *
1000 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1001 * CURRENT. If timer expired set the state machine in the proper state.
1002 * In other cases, this function checks if we need to switch to other state.
1003 */
1004static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1005{
1006 rx_states_t last_state;
1007
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001008 /* keep current State Machine state to compare later if it was
1009 * changed
1010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 last_state = port->sm_rx_state;
1012
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001013 /* check if state machine should change state */
1014
1015 /* first, check if port was reinitialized */
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001016 if (port->sm_vars & AD_PORT_BEGIN) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001017 port->sm_rx_state = AD_RX_INITIALIZE;
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001018 port->sm_vars |= AD_PORT_CHURNED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001019 /* check if port is not enabled */
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001020 } else if (!(port->sm_vars & AD_PORT_BEGIN)
Bandan Das7bfc4752010-10-16 20:19:59 +00001021 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
Bandan Das7bfc4752010-10-16 20:19:59 +00001022 port->sm_rx_state = AD_RX_PORT_DISABLED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001023 /* check if new lacpdu arrived */
1024 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1025 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1026 (port->sm_rx_state == AD_RX_CURRENT))) {
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001027 if (port->sm_rx_state != AD_RX_CURRENT)
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001028 port->sm_vars |= AD_PORT_CHURNED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001029 port->sm_rx_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 port->sm_rx_state = AD_RX_CURRENT;
1031 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001032 /* if timer is on, and if it is expired */
1033 if (port->sm_rx_timer_counter &&
1034 !(--port->sm_rx_timer_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 switch (port->sm_rx_state) {
1036 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001037 port->sm_rx_state = AD_RX_DEFAULTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
1039 case AD_RX_CURRENT:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001040 port->sm_rx_state = AD_RX_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001042 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 }
1045 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001046 /* if no lacpdu arrived and no timer is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 switch (port->sm_rx_state) {
1048 case AD_RX_PORT_DISABLED:
Bandan Das7bfc4752010-10-16 20:19:59 +00001049 if (port->sm_vars & AD_PORT_MOVED)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001050 port->sm_rx_state = AD_RX_INITIALIZE;
Bandan Das7bfc4752010-10-16 20:19:59 +00001051 else if (port->is_enabled
1052 && (port->sm_vars
1053 & AD_PORT_LACP_ENABLED))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001054 port->sm_rx_state = AD_RX_EXPIRED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001055 else if (port->is_enabled
1056 && ((port->sm_vars
1057 & AD_PORT_LACP_ENABLED) == 0))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001058 port->sm_rx_state = AD_RX_LACP_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001060 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
1062
1063 }
1064 }
1065 }
1066
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001067 /* check if the State machine was changed or new lacpdu arrived */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Wilson Kok63b46242015-01-26 01:16:59 -05001069 pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1070 port->actor_port_number,
1071 port->slave->dev->name,
1072 last_state,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001073 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 switch (port->sm_rx_state) {
1075 case AD_RX_INITIALIZE:
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001076 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001078 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 port->sm_vars &= ~AD_PORT_SELECTED;
1081 __record_default(port);
1082 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1083 port->sm_vars &= ~AD_PORT_MOVED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001084 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001086 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 case AD_RX_PORT_DISABLED:
1088 port->sm_vars &= ~AD_PORT_MATCHED;
1089 break;
1090 case AD_RX_LACP_DISABLED:
1091 port->sm_vars &= ~AD_PORT_SELECTED;
1092 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001093 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 port->sm_vars |= AD_PORT_MATCHED;
1095 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1096 break;
1097 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001098 /* Reset of the Synchronization flag (Standard 43.4.12)
1099 * This reset cause to disable this port in the
1100 * COLLECTING_DISTRIBUTING state of the mux machine in
1101 * case of EXPIRED even if LINK_DOWN didn't arrive for
1102 * the port.
1103 */
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001104 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 port->sm_vars &= ~AD_PORT_MATCHED;
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001106 port->partner_oper.port_state |= AD_STATE_LACP_TIMEOUT;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001107 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1109 port->actor_oper_port_state |= AD_STATE_EXPIRED;
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001110 port->sm_vars |= AD_PORT_CHURNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112 case AD_RX_DEFAULTED:
1113 __update_default_selected(port);
1114 __record_default(port);
1115 port->sm_vars |= AD_PORT_MATCHED;
1116 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1117 break;
1118 case AD_RX_CURRENT:
dingtianhong815117a2014-01-02 09:12:54 +08001119 /* detect loopback situation */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001120 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1121 &(port->actor_system))) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001122 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
Joe Perches90194262014-02-15 16:01:45 -08001123 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001124 port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return;
1126 }
1127 __update_selected(lacpdu, port);
1128 __update_ntt(lacpdu, port);
1129 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1131 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001133 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 }
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139/**
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001140 * ad_churn_machine - handle port churn's state machine
1141 * @port: the port we're looking at
1142 *
1143 */
1144static void ad_churn_machine(struct port *port)
1145{
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001146 if (port->sm_vars & AD_PORT_CHURNED) {
1147 port->sm_vars &= ~AD_PORT_CHURNED;
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001148 port->sm_churn_actor_state = AD_CHURN_MONITOR;
1149 port->sm_churn_partner_state = AD_CHURN_MONITOR;
1150 port->sm_churn_actor_timer_counter =
1151 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1152 port->sm_churn_partner_timer_counter =
1153 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1154 return;
1155 }
1156 if (port->sm_churn_actor_timer_counter &&
1157 !(--port->sm_churn_actor_timer_counter) &&
1158 port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1159 if (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION) {
1160 port->sm_churn_actor_state = AD_NO_CHURN;
1161 } else {
1162 port->churn_actor_count++;
1163 port->sm_churn_actor_state = AD_CHURN;
1164 }
1165 }
1166 if (port->sm_churn_partner_timer_counter &&
1167 !(--port->sm_churn_partner_timer_counter) &&
1168 port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1169 if (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) {
1170 port->sm_churn_partner_state = AD_NO_CHURN;
1171 } else {
1172 port->churn_partner_count++;
1173 port->sm_churn_partner_state = AD_CHURN;
1174 }
1175 }
1176}
1177
1178/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * ad_tx_machine - handle a port's tx state machine
1180 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182static void ad_tx_machine(struct port *port)
1183{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001184 /* check if tx timer expired, to verify that we do not send more than
1185 * 3 packets per second
1186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001188 /* check if there is something to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1190 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001193 pr_debug("Sent LACPDU on port %d\n",
1194 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001195
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001196 /* mark ntt as false, so it will not be sent
1197 * again until demanded
1198 */
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001199 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001202 /* restart tx timer(to verify that we will not exceed
1203 * AD_MAX_TX_IN_SECOND
1204 */
1205 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207}
1208
1209/**
1210 * ad_periodic_machine - handle a port's periodic state machine
1211 * @port: the port we're looking at
1212 *
1213 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1214 */
1215static void ad_periodic_machine(struct port *port)
1216{
1217 periodic_states_t last_state;
1218
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001219 /* keep current state machine state to compare later if it was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 last_state = port->sm_periodic_state;
1221
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001222 /* check if port was reinitialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001224 (!(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 -07001225 ) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001226 port->sm_periodic_state = AD_NO_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001228 /* check if state machine should change state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 else if (port->sm_periodic_timer_counter) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001230 /* check if periodic state machine expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!(--port->sm_periodic_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001232 /* if expired then do tx */
1233 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001235 /* If not expired, check if there is some new timeout
1236 * parameter from the partner state
1237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 switch (port->sm_periodic_state) {
1239 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001240 if (!(port->partner_oper.port_state
1241 & AD_STATE_LACP_TIMEOUT))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001242 port->sm_periodic_state = AD_SLOW_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
1244 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001245 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 port->sm_periodic_timer_counter = 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001247 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001250 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252 }
1253 }
1254 } else {
1255 switch (port->sm_periodic_state) {
1256 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001257 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
1259 case AD_PERIODIC_TX:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001260 if (!(port->partner_oper.port_state &
1261 AD_STATE_LACP_TIMEOUT))
1262 port->sm_periodic_state = AD_SLOW_PERIODIC;
Bandan Das7bfc4752010-10-16 20:19:59 +00001263 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001264 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001266 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268 }
1269 }
1270
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001271 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001273 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1274 port->actor_port_number, last_state,
1275 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 switch (port->sm_periodic_state) {
1277 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001278 port->sm_periodic_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 case AD_FAST_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001281 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1282 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 -07001283 break;
1284 case AD_SLOW_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001285 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1286 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 -07001287 break;
1288 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001289 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001291 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 break;
1293 }
1294 }
1295}
1296
1297/**
1298 * ad_port_selection_logic - select aggregation groups
1299 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001300 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
1302 * Select aggregation groups, and assign each port for it's aggregetor. The
1303 * selection logic is called in the inititalization (after all the handshkes),
1304 * and after every lacpdu receive (if selected is off).
1305 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001306static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1309 struct port *last_port = NULL, *curr_port;
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001310 struct list_head *iter;
1311 struct bonding *bond;
1312 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 int found = 0;
1314
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001315 /* if the port is already Selected, do nothing */
Bandan Das7bfc4752010-10-16 20:19:59 +00001316 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001319 bond = __get_bond_by_port(port);
1320
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001321 /* if the port is connected to other aggregator, detach it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (port->aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001323 /* detach the port from its former aggregator */
Bandan Das128ea6c2010-10-16 20:19:58 +00001324 temp_aggregator = port->aggregator;
1325 for (curr_port = temp_aggregator->lag_ports; curr_port;
1326 last_port = curr_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001327 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (curr_port == port) {
1329 temp_aggregator->num_of_ports--;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001330 /* if it is the first port attached to the
1331 * aggregator
1332 */
1333 if (!last_port) {
Bandan Das128ea6c2010-10-16 20:19:58 +00001334 temp_aggregator->lag_ports =
1335 port->next_port_in_aggregator;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001336 } else {
1337 /* not the first port attached to the
1338 * aggregator
1339 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001340 last_port->next_port_in_aggregator =
1341 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001344 /* clear the port's relations to this
1345 * aggregator
1346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001348 port->next_port_in_aggregator = NULL;
1349 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001351 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1352 port->actor_port_number,
1353 temp_aggregator->aggregator_identifier);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001354 /* if the aggregator is empty, clear its
1355 * parameters, and set it ready to be attached
1356 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001357 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 break;
1360 }
1361 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001362 if (!curr_port) {
1363 /* meaning: the port was related to an aggregator
1364 * but was not on the aggregator port list
1365 */
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001366 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1367 port->slave->bond->dev->name,
1368 port->actor_port_number,
1369 port->slave->dev->name,
1370 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001373 /* search on all aggregators for a suitable aggregator for this port */
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001374 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001375 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001377 /* keep a free aggregator for later use(if needed) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001379 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001380 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 continue;
1382 }
dingtianhong815117a2014-01-02 09:12:54 +08001383 /* check if current aggregator suits us */
1384 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1385 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001386 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1387 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ) &&
dingtianhong815117a2014-01-02 09:12:54 +08001389 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1390 !aggregator->is_individual) /* but is not individual OR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 )
1392 ) {
dingtianhong815117a2014-01-02 09:12:54 +08001393 /* attach to the founded aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001395 port->actor_port_aggregator_identifier =
1396 port->aggregator->aggregator_identifier;
1397 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001399 aggregator->lag_ports = port;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001400 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1401 port->actor_port_number,
1402 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001404 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 port->sm_vars |= AD_PORT_SELECTED;
1406 found = 1;
1407 break;
1408 }
1409 }
1410
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001411 /* the port couldn't find an aggregator - attach it to a new
1412 * aggregator
1413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!found) {
1415 if (free_aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001416 /* assign port a new aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001418 port->actor_port_aggregator_identifier =
1419 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001421 /* update the new aggregator's parameters
1422 * if port was responsed from the end-user
1423 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001424 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
Bandan Das7bfc4752010-10-16 20:19:59 +00001425 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001426 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001427 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001428 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Mahesh Bandewarc3cd9ee2015-04-07 16:16:11 -07001430 port->aggregator->actor_admin_aggregator_key =
1431 port->actor_admin_port_key;
1432 port->aggregator->actor_oper_aggregator_key =
1433 port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001434 port->aggregator->partner_system =
1435 port->partner_oper.system;
1436 port->aggregator->partner_system_priority =
1437 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001438 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 port->aggregator->receive_state = 1;
1440 port->aggregator->transmit_state = 1;
1441 port->aggregator->lag_ports = port;
1442 port->aggregator->num_of_ports++;
1443
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001444 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 port->sm_vars |= AD_PORT_SELECTED;
1446
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001447 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1448 port->actor_port_number,
1449 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001451 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 port->actor_port_number, port->slave->dev->name);
1453 }
1454 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001455 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1456 * in all aggregator's ports, else set ready=FALSE in all
1457 * aggregator's ports
1458 */
1459 __set_agg_ports_ready(port->aggregator,
1460 __agg_ports_are_ready(port->aggregator));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jay Vosburghfd989c82008-11-04 17:51:16 -08001462 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001463 ad_agg_selection_logic(aggregator, update_slave_arr);
Wilson Kok63b46242015-01-26 01:16:59 -05001464
1465 if (!port->aggregator->is_active)
1466 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001467}
1468
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001469/* Decide if "agg" is a better choice for the new active aggregator that
Jay Vosburghfd989c82008-11-04 17:51:16 -08001470 * the current best, according to the ad_select policy.
1471 */
1472static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1473 struct aggregator *curr)
1474{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001475 /* 0. If no best, select current.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001476 *
1477 * 1. If the current agg is not individual, and the best is
1478 * individual, select current.
1479 *
1480 * 2. If current agg is individual and the best is not, keep best.
1481 *
1482 * 3. Therefore, current and best are both individual or both not
1483 * individual, so:
1484 *
1485 * 3a. If current agg partner replied, and best agg partner did not,
1486 * select current.
1487 *
1488 * 3b. If current agg partner did not reply and best agg partner
1489 * did reply, keep best.
1490 *
1491 * 4. Therefore, current and best both have partner replies or
1492 * both do not, so perform selection policy:
1493 *
1494 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1495 * select by bandwidth.
1496 *
1497 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1498 */
1499 if (!best)
1500 return curr;
1501
1502 if (!curr->is_individual && best->is_individual)
1503 return curr;
1504
1505 if (curr->is_individual && !best->is_individual)
1506 return best;
1507
1508 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1509 return curr;
1510
1511 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1512 return best;
1513
1514 switch (__get_agg_selection_mode(curr->lag_ports)) {
1515 case BOND_AD_COUNT:
1516 if (curr->num_of_ports > best->num_of_ports)
1517 return curr;
1518
1519 if (curr->num_of_ports < best->num_of_ports)
1520 return best;
1521
1522 /*FALLTHROUGH*/
1523 case BOND_AD_STABLE:
1524 case BOND_AD_BANDWIDTH:
1525 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1526 return curr;
1527
1528 break;
1529
1530 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001531 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1532 curr->slave->bond->dev->name,
1533 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001536
1537 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001540static int agg_device_up(const struct aggregator *agg)
1541{
Jiri Bohac2430af82011-04-19 02:09:55 +00001542 struct port *port = agg->lag_ports;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001543
Jiri Bohac2430af82011-04-19 02:09:55 +00001544 if (!port)
1545 return 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001546
1547 return netif_running(port->slave->dev) &&
1548 netif_carrier_ok(port->slave->dev);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/**
1552 * ad_agg_selection_logic - select an aggregation group for a team
1553 * @aggregator: the aggregator we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001554 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 *
1556 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001557 *
1558 * The logic of this function is to select the aggregator according to
1559 * the ad_select policy:
1560 *
1561 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1562 * it, and to reselect the active aggregator only if the previous
1563 * aggregator has no more ports related to it.
1564 *
1565 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1566 * bandwidth, and reselect whenever a link state change takes place or the
1567 * set of slaves in the bond changes.
1568 *
1569 * BOND_AD_COUNT: select the aggregator with largest number of ports
1570 * (slaves), and reselect whenever a link state change takes place or the
1571 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 *
1573 * FIXME: this function MUST be called with the first agg in the bond, or
1574 * __get_active_agg() won't work correctly. This function should be better
1575 * called with the bond itself, and retrieve the first agg from it.
1576 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001577static void ad_agg_selection_logic(struct aggregator *agg,
1578 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001580 struct aggregator *best, *active, *origin;
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001581 struct bonding *bond = agg->slave->bond;
1582 struct list_head *iter;
1583 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Veaceslav Falico49b76242014-01-10 11:59:45 +01001586 rcu_read_lock();
Jay Vosburghfd989c82008-11-04 17:51:16 -08001587 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001588 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001589 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
dingtianhongbe79bd02013-12-13 10:20:12 +08001591 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001592 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001593
Jay Vosburghfd989c82008-11-04 17:51:16 -08001594 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001596 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001597 best = ad_agg_selection_test(best, agg);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Jay Vosburghfd989c82008-11-04 17:51:16 -08001600 if (best &&
1601 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001602 /* For the STABLE policy, don't replace the old active
Jay Vosburghfd989c82008-11-04 17:51:16 -08001603 * aggregator if it's still active (it has an answering
1604 * partner) or if both the best and active don't have an
1605 * answering partner.
1606 */
1607 if (active && active->lag_ports &&
1608 active->lag_ports->is_enabled &&
1609 (__agg_has_partner(active) ||
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001610 (!__agg_has_partner(active) &&
1611 !__agg_has_partner(best)))) {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001612 if (!(!active->actor_oper_aggregator_key &&
1613 best->actor_oper_aggregator_key)) {
1614 best = NULL;
1615 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 }
1618 }
1619
Jay Vosburghfd989c82008-11-04 17:51:16 -08001620 if (best && (best == active)) {
1621 best = NULL;
1622 active->is_active = 1;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
dingtianhongbe79bd02013-12-13 10:20:12 +08001625 /* if there is new best aggregator, activate it */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001626 if (best) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001627 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1628 best->aggregator_identifier, best->num_of_ports,
1629 best->actor_oper_aggregator_key,
1630 best->partner_oper_aggregator_key,
1631 best->is_individual, best->is_active);
1632 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1633 best->lag_ports, best->slave,
1634 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001635
dingtianhongbe79bd02013-12-13 10:20:12 +08001636 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001637 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001638
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001639 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1640 agg->aggregator_identifier, agg->num_of_ports,
1641 agg->actor_oper_aggregator_key,
1642 agg->partner_oper_aggregator_key,
1643 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
dingtianhongbe79bd02013-12-13 10:20:12 +08001646 /* check if any partner replys */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001647 if (best->is_individual) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001648 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1649 best->slave ?
1650 best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Jay Vosburghfd989c82008-11-04 17:51:16 -08001653 best->is_active = 1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001654 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1655 best->aggregator_identifier);
1656 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1657 best->aggregator_identifier, best->num_of_ports,
1658 best->actor_oper_aggregator_key,
1659 best->partner_oper_aggregator_key,
1660 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001662 /* disable the ports that were related to the former
1663 * active_aggregator
1664 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001665 if (active) {
1666 for (port = active->lag_ports; port;
1667 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 __disable_port(port);
1669 }
1670 }
Mahesh Bandewaree637712014-10-04 17:45:01 -07001671 /* Slave array needs update. */
1672 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001675 /* if the selected aggregator is of join individuals
Jay Vosburghfd989c82008-11-04 17:51:16 -08001676 * (partner_system is NULL), enable their ports
1677 */
1678 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Jay Vosburghfd989c82008-11-04 17:51:16 -08001680 if (active) {
1681 if (!__agg_has_partner(active)) {
1682 for (port = active->lag_ports; port;
1683 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 __enable_port(port);
1685 }
1686 }
1687 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001688
dingtianhongbe79bd02013-12-13 10:20:12 +08001689 rcu_read_unlock();
1690
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001691 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694/**
1695 * ad_clear_agg - clear a given aggregator's parameters
1696 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
1698static void ad_clear_agg(struct aggregator *aggregator)
1699{
1700 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001701 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 aggregator->actor_admin_aggregator_key = 0;
1703 aggregator->actor_oper_aggregator_key = 0;
1704 aggregator->partner_system = null_mac_addr;
1705 aggregator->partner_system_priority = 0;
1706 aggregator->partner_oper_aggregator_key = 0;
1707 aggregator->receive_state = 0;
1708 aggregator->transmit_state = 0;
1709 aggregator->lag_ports = NULL;
1710 aggregator->is_active = 0;
1711 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001712 pr_debug("LAG %d was cleared\n",
1713 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715}
1716
1717/**
1718 * ad_initialize_agg - initialize a given aggregator's parameters
1719 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 */
1721static void ad_initialize_agg(struct aggregator *aggregator)
1722{
1723 if (aggregator) {
1724 ad_clear_agg(aggregator);
1725
1726 aggregator->aggregator_mac_address = null_mac_addr;
1727 aggregator->aggregator_identifier = 0;
1728 aggregator->slave = NULL;
1729 }
1730}
1731
1732/**
1733 * ad_initialize_port - initialize a given port's parameters
1734 * @aggregator: the aggregator we're looking at
1735 * @lacp_fast: boolean. whether fast periodic should be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
1737static void ad_initialize_port(struct port *port, int lacp_fast)
1738{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001739 static const struct port_params tmpl = {
1740 .system_priority = 0xffff,
1741 .key = 1,
1742 .port_number = 1,
1743 .port_priority = 0xff,
1744 .port_state = 1,
1745 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001746 static const struct lacpdu lacpdu = {
1747 .subtype = 0x01,
1748 .version_number = 0x01,
1749 .tlv_type_actor_info = 0x01,
1750 .actor_information_length = 0x14,
1751 .tlv_type_partner_info = 0x02,
1752 .partner_information_length = 0x14,
1753 .tlv_type_collector_info = 0x03,
1754 .collector_information_length = 0x10,
1755 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1756 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 port->actor_port_priority = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001761 port->ntt = false;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001762 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1763 AD_STATE_LACP_ACTIVITY;
1764 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1765 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Bandan Das7bfc4752010-10-16 20:19:59 +00001767 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001770 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1771 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1772
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001773 port->is_enabled = true;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001774 /* private parameters */
Mahesh Bandewar19a12042015-03-27 22:34:31 -07001775 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 port->sm_rx_state = 0;
1777 port->sm_rx_timer_counter = 0;
1778 port->sm_periodic_state = 0;
1779 port->sm_periodic_timer_counter = 0;
1780 port->sm_mux_state = 0;
1781 port->sm_mux_timer_counter = 0;
1782 port->sm_tx_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 port->aggregator = NULL;
1784 port->next_port_in_aggregator = NULL;
1785 port->transaction_id = 0;
1786
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001787 port->sm_churn_actor_timer_counter = 0;
1788 port->sm_churn_actor_state = 0;
1789 port->churn_actor_count = 0;
1790 port->sm_churn_partner_timer_counter = 0;
1791 port->sm_churn_partner_state = 0;
1792 port->churn_partner_count = 0;
1793
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001794 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796}
1797
1798/**
1799 * ad_enable_collecting_distributing - enable a port's transmit/receive
1800 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001801 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 *
1803 * Enable @port if it's in an active aggregator
1804 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001805static void ad_enable_collecting_distributing(struct port *port,
1806 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001809 pr_debug("Enabling port %d(LAG %d)\n",
1810 port->actor_port_number,
1811 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 __enable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001813 /* Slave array needs update */
1814 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816}
1817
1818/**
1819 * ad_disable_collecting_distributing - disable a port's transmit/receive
1820 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001821 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001823static void ad_disable_collecting_distributing(struct port *port,
1824 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001826 if (port->aggregator &&
1827 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1828 &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001829 pr_debug("Disabling port %d(LAG %d)\n",
1830 port->actor_port_number,
1831 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 __disable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001833 /* Slave array needs an update */
1834 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836}
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/**
1839 * ad_marker_info_received - handle receive of a Marker information frame
1840 * @marker_info: Marker info received
1841 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001843static void ad_marker_info_received(struct bond_marker *marker_info,
1844 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001846 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001848 /* copy the received marker data to the response marker */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001849 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001850 /* change the marker subtype to marker response */
Bandan Das128ea6c2010-10-16 20:19:58 +00001851 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001853 /* send the marker response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001855 pr_debug("Sent Marker Response on port %d\n",
1856 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858}
1859
1860/**
1861 * ad_marker_response_received - handle receive of a marker response frame
1862 * @marker: marker PDU received
1863 * @port: the port we're looking at
1864 *
1865 * This function does nothing since we decided not to implement send and handle
1866 * response for marker PDU's, in this stage, but only to respond to marker
1867 * information.
1868 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001869static void ad_marker_response_received(struct bond_marker *marker,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001870 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001872 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001875/* ========= AD exported functions to the main bonding code ========= */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001877/* Check aggregators status in team every T seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878#define AD_AGGREGATOR_SELECTION_TIMER 8
1879
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001880/**
1881 * bond_3ad_initiate_agg_selection - initate aggregator selection
1882 * @bond: bonding struct
Jay Vosburghfd989c82008-11-04 17:51:16 -08001883 *
1884 * Set the aggregation selection timer, to initiate an agg selection in
1885 * the very near future. Called during first initialization, and during
1886 * any down to up transitions of the bond.
1887 */
1888void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1889{
1890 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893/**
1894 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1895 * @bond: bonding struct to work on
1896 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *
1898 * Can be called only after the mac address of the bond is set.
1899 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001900void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001901{
dingtianhong815117a2014-01-02 09:12:54 +08001902 /* check that the bond is not initialized yet */
1903 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001904 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001906 BOND_AD_INFO(bond).aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Mahesh Bandewar6791e462015-05-09 00:01:55 -07001908 BOND_AD_INFO(bond).system.sys_priority =
1909 bond->params.ad_actor_sys_prio;
Mahesh Bandewar74514952015-05-09 00:01:56 -07001910 if (is_zero_ether_addr(bond->params.ad_actor_system))
1911 BOND_AD_INFO(bond).system.sys_mac_addr =
1912 *((struct mac_addr *)bond->dev->dev_addr);
1913 else
1914 BOND_AD_INFO(bond).system.sys_mac_addr =
1915 *((struct mac_addr *)bond->params.ad_actor_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001917 /* initialize how many times this module is called in one
1918 * second (should be about every 100ms)
1919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 ad_ticks_per_sec = tick_resolution;
1921
Jay Vosburghfd989c82008-11-04 17:51:16 -08001922 bond_3ad_initiate_agg_selection(bond,
1923 AD_AGGREGATOR_SELECTION_TIMER *
1924 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926}
1927
1928/**
1929 * bond_3ad_bind_slave - initialize a slave's port
1930 * @slave: slave struct to work on
1931 *
1932 * Returns: 0 on success
1933 * < 0 on error
1934 */
dingtianhong359632e2014-01-02 09:13:12 +08001935void bond_3ad_bind_slave(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct bonding *bond = bond_get_bond_by_slave(slave);
1938 struct port *port;
1939 struct aggregator *aggregator;
1940
dingtianhong359632e2014-01-02 09:13:12 +08001941 /* check that the slave has not been initialized yet. */
dingtianhong3fdddd82014-05-12 15:08:43 +08001942 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
dingtianhong359632e2014-01-02 09:13:12 +08001944 /* port initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001945 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001947 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 port->slave = slave;
dingtianhong3fdddd82014-05-12 15:08:43 +08001950 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07001951 /* key is determined according to the link speed, duplex and
1952 * user key
dingtianhong359632e2014-01-02 09:13:12 +08001953 */
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07001954 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07001955 ad_update_actor_keys(port, false);
dingtianhong359632e2014-01-02 09:13:12 +08001956 /* actor system is the bond's system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
Mahesh Bandewar6791e462015-05-09 00:01:55 -07001958 port->actor_system_priority =
1959 BOND_AD_INFO(bond).system.sys_priority;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001960 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
1961 * lacpdu's are sent in one second)
1962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
dingtianhong359632e2014-01-02 09:13:12 +08001967 /* aggregator initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001968 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 ad_initialize_agg(aggregator);
1971
1972 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001973 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 aggregator->slave = slave;
1975 aggregator->is_active = 0;
1976 aggregator->num_of_ports = 0;
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980/**
1981 * bond_3ad_unbind_slave - deinitialize a slave's port
1982 * @slave: slave struct to work on
1983 *
1984 * Search for the aggregator that is related to this port, remove the
1985 * aggregator and assign another aggregator for other port related to it
1986 * (if any), and remove the port.
1987 */
1988void bond_3ad_unbind_slave(struct slave *slave)
1989{
1990 struct port *port, *prev_port, *temp_port;
1991 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1992 int select_new_active_agg = 0;
Veaceslav Falico0b088262013-09-27 16:12:02 +02001993 struct bonding *bond = slave->bond;
1994 struct slave *slave_iter;
1995 struct list_head *iter;
Mahesh Bandewaree637712014-10-04 17:45:01 -07001996 bool dummy_slave_update; /* Ignore this value as caller updates array */
Jasper Spaansa361c832009-10-23 04:09:24 +00001997
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001998 /* Sync against bond_3ad_state_machine_handler() */
1999 spin_lock_bh(&bond->mode_lock);
dingtianhong3fdddd82014-05-12 15:08:43 +08002000 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2001 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002003 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002005 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
2006 slave->dev->name);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002010 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
2011 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* Tell the partner that this port is not suitable for aggregation */
2014 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2015 __update_lacpdu_from_port(port);
2016 ad_lacpdu_send(port);
2017
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002018 /* check if this aggregator is occupied */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (aggregator->lag_ports) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002020 /* check if there are other ports related to this aggregator
2021 * except the port related to this slave(thats ensure us that
2022 * there is a reason to search for new aggregator, and that we
2023 * will find one
2024 */
2025 if ((aggregator->lag_ports != port) ||
2026 (aggregator->lag_ports->next_port_in_aggregator)) {
2027 /* find new aggregator for the related port(s) */
Veaceslav Falico0b088262013-09-27 16:12:02 +02002028 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002029 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002030 /* if the new aggregator is empty, or it is
2031 * connected to our port only
2032 */
2033 if (!new_aggregator->lag_ports ||
2034 ((new_aggregator->lag_ports == port) &&
2035 !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
Veaceslav Falico0b088262013-09-27 16:12:02 +02002038 if (!slave_iter)
2039 new_aggregator = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002040
2041 /* if new aggregator found, copy the aggregator's
2042 * parameters and connect the related lag_ports to the
2043 * new aggregator
2044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 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 +02002046 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2047 aggregator->aggregator_identifier,
2048 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002050 if ((new_aggregator->lag_ports == port) &&
2051 new_aggregator->is_active) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002052 netdev_info(bond->dev, "Removing an active aggregator\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 select_new_active_agg = 1;
2054 }
2055
2056 new_aggregator->is_individual = aggregator->is_individual;
2057 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2058 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2059 new_aggregator->partner_system = aggregator->partner_system;
2060 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2061 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2062 new_aggregator->receive_state = aggregator->receive_state;
2063 new_aggregator->transmit_state = aggregator->transmit_state;
2064 new_aggregator->lag_ports = aggregator->lag_ports;
2065 new_aggregator->is_active = aggregator->is_active;
2066 new_aggregator->num_of_ports = aggregator->num_of_ports;
2067
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002068 /* update the information that is written on
2069 * the ports about the aggregator
2070 */
Bandan Das128ea6c2010-10-16 20:19:58 +00002071 for (temp_port = aggregator->lag_ports; temp_port;
2072 temp_port = temp_port->next_port_in_aggregator) {
2073 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2075 }
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00002078
Bandan Das7bfc4752010-10-16 20:19:59 +00002079 if (select_new_active_agg)
Mahesh Bandewaree637712014-10-04 17:45:01 -07002080 ad_agg_selection_logic(__get_first_agg(port),
2081 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002083 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002085 } else {
2086 /* in case that the only port related to this
2087 * aggregator is the one we want to remove
2088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 select_new_active_agg = aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 ad_clear_agg(aggregator);
2091 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002092 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002093 /* select new active aggregator */
Veaceslav Falico74684492013-09-27 15:10:58 +02002094 temp_aggregator = __get_first_agg(port);
2095 if (temp_aggregator)
Mahesh Bandewaree637712014-10-04 17:45:01 -07002096 ad_agg_selection_logic(temp_aggregator,
2097 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 }
2100 }
2101
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002102 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002103
2104 /* find the aggregator that this port is connected to */
Veaceslav Falico0b088262013-09-27 16:12:02 +02002105 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002106 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 prev_port = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002108 /* search the port in the aggregator's related ports */
Bandan Das128ea6c2010-10-16 20:19:58 +00002109 for (temp_port = temp_aggregator->lag_ports; temp_port;
2110 prev_port = temp_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002111 temp_port = temp_port->next_port_in_aggregator) {
2112 if (temp_port == port) {
2113 /* the aggregator found - detach the port from
2114 * this aggregator
2115 */
Bandan Das7bfc4752010-10-16 20:19:59 +00002116 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00002118 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00002121 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 select_new_active_agg = temp_aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 ad_clear_agg(temp_aggregator);
2124 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002125 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002126 /* select new active aggregator */
Mahesh Bandewaree637712014-10-04 17:45:01 -07002127 ad_agg_selection_logic(__get_first_agg(port),
2128 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 }
2131 break;
2132 }
2133 }
2134 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002135 port->slave = NULL;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002136
2137out:
2138 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141/**
2142 * bond_3ad_state_machine_handler - handle state machines timeout
2143 * @bond: bonding struct to work on
2144 *
2145 * The state machine handling concept in this module is to check every tick
2146 * which state machine should operate any function. The execution order is
2147 * round robin, so when we have an interaction between state machines, the
2148 * reply of one to each other might be delayed until next tick.
2149 *
2150 * This function also complete the initialization when the agg_select_timer
2151 * times out, and it selects an aggregator for the ports that are yet not
2152 * related to any aggregator, and selects the active aggregator for a bond.
2153 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002154void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002156 struct bonding *bond = container_of(work, struct bonding,
2157 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002159 struct list_head *iter;
2160 struct slave *slave;
2161 struct port *port;
dingtianhong5e5b0662014-02-26 11:05:22 +08002162 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Mahesh Bandewaree637712014-10-04 17:45:01 -07002163 bool update_slave_arr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002165 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2166 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2167 * concurrently due to incoming LACPDU as well.
2168 */
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002169 spin_lock_bh(&bond->mode_lock);
dingtianhongbe79bd02013-12-13 10:20:12 +08002170 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04002171
dingtianhongbe79bd02013-12-13 10:20:12 +08002172 /* check if there are any slaves */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002173 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
dingtianhongbe79bd02013-12-13 10:20:12 +08002176 /* check if agg_select_timer timer after initialize is timed out */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002177 if (BOND_AD_INFO(bond).agg_select_timer &&
2178 !(--BOND_AD_INFO(bond).agg_select_timer)) {
dingtianhongbe79bd02013-12-13 10:20:12 +08002179 slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +08002180 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002181
dingtianhongbe79bd02013-12-13 10:20:12 +08002182 /* select the active aggregator for the bond */
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002183 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002185 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2186 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto re_arm;
2188 }
2189
2190 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002191 ad_agg_selection_logic(aggregator, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002193 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195
dingtianhongbe79bd02013-12-13 10:20:12 +08002196 /* for each port run the state machines */
2197 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002198 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002200 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
Veaceslav Falico86a2b9c2014-03-16 17:55:03 +01002201 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto re_arm;
2203 }
2204
2205 ad_rx_machine(NULL, port);
2206 ad_periodic_machine(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002207 ad_port_selection_logic(port, &update_slave_arr);
2208 ad_mux_machine(port, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 ad_tx_machine(port);
Mahesh Bandewar14c95512015-02-23 17:50:11 -08002210 ad_churn_machine(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
dingtianhongbe79bd02013-12-13 10:20:12 +08002212 /* turn off the BEGIN bit, since we already handled it */
Bandan Das7bfc4752010-10-16 20:19:59 +00002213 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 port->sm_vars &= ~AD_PORT_BEGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216
2217re_arm:
dingtianhong5e5b0662014-02-26 11:05:22 +08002218 bond_for_each_slave_rcu(bond, slave, iter) {
2219 if (slave->should_notify) {
2220 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2221 break;
2222 }
2223 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002224 rcu_read_unlock();
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002225 spin_unlock_bh(&bond->mode_lock);
dingtianhong5e5b0662014-02-26 11:05:22 +08002226
Mahesh Bandewaree637712014-10-04 17:45:01 -07002227 if (update_slave_arr)
2228 bond_slave_arr_work_rearm(bond, 0);
2229
dingtianhong5e5b0662014-02-26 11:05:22 +08002230 if (should_notify_rtnl && rtnl_trylock()) {
dingtianhongb0929912014-02-26 11:05:23 +08002231 bond_slave_state_notify(bond);
dingtianhong5e5b0662014-02-26 11:05:22 +08002232 rtnl_unlock();
2233 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002234 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237/**
2238 * bond_3ad_rx_indication - handle a received frame
2239 * @lacpdu: received lacpdu
2240 * @slave: slave struct to work on
2241 * @length: length of the data received
2242 *
2243 * It is assumed that frames that were sent on this NIC don't returned as new
2244 * received frames (loopback). Since only the payload is given to this
2245 * function, it check for loopback.
2246 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002247static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2248 u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002251 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 if (length >= sizeof(struct lacpdu)) {
2254
dingtianhong3fdddd82014-05-12 15:08:43 +08002255 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002258 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2259 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
2263 switch (lacpdu->subtype) {
2264 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002265 ret = RX_HANDLER_CONSUMED;
Satish Ashok2f637322015-01-26 01:17:00 -05002266 netdev_dbg(slave->bond->dev,
2267 "Received LACPDU on port %d slave %s\n",
2268 port->actor_port_number,
2269 slave->dev->name);
Nils Carlson16d79d72011-03-03 22:09:11 +00002270 /* Protect against concurrent state machines */
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002271 spin_lock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 ad_rx_machine(lacpdu, port);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002273 spin_unlock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 break;
2275
2276 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002277 ret = RX_HANDLER_CONSUMED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002278 /* No need to convert fields to Little Endian since we
2279 * don't use the marker's fields.
2280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002282 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 case AD_MARKER_INFORMATION_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002284 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2285 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002286 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
2288
2289 case AD_MARKER_RESPONSE_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002290 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2291 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002292 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 break;
2294
2295 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002296 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2297 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 }
2300 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304/**
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002305 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2306 * its current speed and duplex settings.
2307 *
2308 * @port: the port we'are looking at
2309 * @reset: Boolean to just reset the speed and the duplex part of the key
2310 *
2311 * The logic to change the oper / admin keys is:
2312 * (a) A full duplex port can participate in LACP with partner.
2313 * (b) When the speed is changed, LACP need to be reinitiated.
2314 */
2315static void ad_update_actor_keys(struct port *port, bool reset)
2316{
2317 u8 duplex = 0;
2318 u16 ospeed = 0, speed = 0;
2319 u16 old_oper_key = port->actor_oper_port_key;
2320
2321 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2322 if (!reset) {
2323 speed = __get_link_speed(port);
2324 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2325 duplex = __get_duplex(port);
2326 port->actor_admin_port_key |= (speed << 1) | duplex;
2327 }
2328 port->actor_oper_port_key = port->actor_admin_port_key;
2329
2330 if (old_oper_key != port->actor_oper_port_key) {
2331 /* Only 'duplex' port participates in LACP */
2332 if (duplex)
2333 port->sm_vars |= AD_PORT_LACP_ENABLED;
2334 else
2335 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2336
2337 if (!reset) {
2338 if (!speed) {
2339 netdev_err(port->slave->dev,
2340 "speed changed to 0 for port %s",
2341 port->slave->dev->name);
2342 } else if (duplex && ospeed != speed) {
2343 /* Speed change restarts LACP state-machine */
2344 port->sm_vars |= AD_PORT_BEGIN;
2345 }
2346 }
2347 }
2348}
2349
2350/**
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002351 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2352 * change indication
2353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 * @slave: slave struct to work on
2355 *
2356 * Handle reselection of aggregator (if needed) for this port.
2357 */
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002358void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 struct port *port;
2361
dingtianhong3fdddd82014-05-12 15:08:43 +08002362 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
dingtianhong71a06c52013-12-13 17:29:19 +08002364 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!port->slave) {
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002366 netdev_warn(slave->bond->dev,
2367 "speed/duplex changed for uninitialized port %s\n",
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002368 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return;
2370 }
2371
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002372 spin_lock_bh(&slave->bond->mode_lock);
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002373 ad_update_actor_keys(port, false);
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002374 netdev_dbg(slave->bond->dev, "Port %d slave %s changed speed/duplex\n",
Satish Ashok2f637322015-01-26 01:17:00 -05002375 port->actor_port_number, slave->dev->name);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002376 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379/**
2380 * bond_3ad_handle_link_change - handle a slave's link status change indication
2381 * @slave: slave struct to work on
2382 * @status: whether the link is now up or down
2383 *
2384 * Handle reselection of aggregator (if needed) for this port.
2385 */
2386void bond_3ad_handle_link_change(struct slave *slave, char link)
2387{
2388 struct port *port;
2389
dingtianhong3fdddd82014-05-12 15:08:43 +08002390 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
dingtianhong108db732013-12-13 17:29:29 +08002392 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002394 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2395 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return;
2397 }
2398
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002399 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong108db732013-12-13 17:29:29 +08002400 /* on link down we are zeroing duplex and speed since
2401 * some of the adaptors(ce1000.lan) report full duplex/speed
2402 * instead of N/A(duplex) / 0(speed).
2403 *
2404 * on link up we are forcing recheck on the duplex and speed since
2405 * some of he adaptors(ce1000.lan) report.
2406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002408 port->is_enabled = true;
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002409 ad_update_actor_keys(port, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 } else {
2411 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002412 port->is_enabled = false;
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002413 ad_update_actor_keys(port, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002415 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2416 port->actor_port_number,
2417 link == BOND_LINK_UP ? "UP" : "DOWN");
dingtianhong108db732013-12-13 17:29:29 +08002418
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002419 spin_unlock_bh(&slave->bond->mode_lock);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002420
2421 /* RTNL is held and mode_lock is released so it's safe
2422 * to update slave_array here.
2423 */
2424 bond_update_slave_arr(slave->bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002427/**
2428 * bond_3ad_set_carrier - set link state for bonding master
2429 * @bond - bonding structure
2430 *
2431 * if we have an active aggregator, we're up, if not, we're down.
2432 * Presumes that we cannot have an active aggregator if there are
2433 * no slaves with link up.
Jay Vosburghff59c452006-03-27 13:27:43 -08002434 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002435 * This behavior complies with IEEE 802.3 section 43.3.9.
2436 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002437 * Called by bond_set_carrier(). Return zero if carrier state does not
2438 * change, nonzero if it does.
2439 */
2440int bond_3ad_set_carrier(struct bonding *bond)
2441{
stephen hemminger655f8912011-06-22 09:54:39 +00002442 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002443 struct slave *first_slave;
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002444 int ret = 1;
stephen hemminger655f8912011-06-22 09:54:39 +00002445
dingtianhongbe79bd02013-12-13 10:20:12 +08002446 rcu_read_lock();
2447 first_slave = bond_first_slave_rcu(bond);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002448 if (!first_slave) {
2449 ret = 0;
2450 goto out;
2451 }
dingtianhong3fdddd82014-05-12 15:08:43 +08002452 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002453 if (active) {
2454 /* are enough slaves available to consider link up? */
2455 if (active->num_of_ports < bond->params.min_links) {
2456 if (netif_carrier_ok(bond->dev)) {
2457 netif_carrier_off(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002458 goto out;
stephen hemminger655f8912011-06-22 09:54:39 +00002459 }
2460 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002461 netif_carrier_on(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002462 goto out;
Jay Vosburghff59c452006-03-27 13:27:43 -08002463 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002464 } else if (netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002465 netif_carrier_off(bond->dev);
Jay Vosburghff59c452006-03-27 13:27:43 -08002466 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002467out:
2468 rcu_read_unlock();
2469 return ret;
Jay Vosburghff59c452006-03-27 13:27:43 -08002470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002473 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 * @bond: bonding struct to work on
2475 * @ad_info: ad_info struct to fill with the bond's info
2476 *
2477 * Returns: 0 on success
2478 * < 0 on error
2479 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002480int __bond_3ad_get_active_agg_info(struct bonding *bond,
2481 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002484 struct list_head *iter;
2485 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 struct port *port;
2487
dingtianhong47e91f562013-10-15 16:28:35 +08002488 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002489 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (port->aggregator && port->aggregator->is_active) {
2491 aggregator = port->aggregator;
2492 break;
2493 }
2494 }
2495
Joe Perches21f374c2014-02-18 09:42:47 -08002496 if (!aggregator)
2497 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Joe Perches21f374c2014-02-18 09:42:47 -08002499 ad_info->aggregator_id = aggregator->aggregator_identifier;
2500 ad_info->ports = aggregator->num_of_ports;
2501 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2502 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2503 ether_addr_copy(ad_info->partner_system,
2504 aggregator->partner_system.mac_addr_value);
2505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002508int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2509{
2510 int ret;
2511
dingtianhong47e91f562013-10-15 16:28:35 +08002512 rcu_read_lock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002513 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
dingtianhong47e91f562013-10-15 16:28:35 +08002514 rcu_read_unlock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002515
2516 return ret;
2517}
2518
Eric Dumazetde063b72012-06-11 19:23:07 +00002519int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2520 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Eric Dumazetde063b72012-06-11 19:23:07 +00002522 struct lacpdu *lacpdu, _lacpdu;
2523
Jiri Pirko3aba8912011-04-19 03:48:16 +00002524 if (skb->protocol != PKT_TYPE_LACPDU)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002525 return RX_HANDLER_ANOTHER;
Neil Hormanb3053252011-01-20 09:02:31 +00002526
Mahesh Bandewarbb54e582015-02-23 17:50:10 -08002527 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2528 return RX_HANDLER_ANOTHER;
2529
Eric Dumazetde063b72012-06-11 19:23:07 +00002530 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2531 if (!lacpdu)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002532 return RX_HANDLER_ANOTHER;
Andy Gospodarekab128112010-09-10 11:43:20 +00002533
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002534 return bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002536
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002537/**
2538 * bond_3ad_update_lacp_rate - change the lacp rate
2539 * @bond - bonding struct
2540 *
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002541 * When modify lacp_rate parameter via sysfs,
2542 * update actor_oper_port_state of each port.
2543 *
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002544 * Hold bond->mode_lock,
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002545 * so we can modify port->actor_oper_port_state,
2546 * no matter bond is up or down.
2547 */
2548void bond_3ad_update_lacp_rate(struct bonding *bond)
2549{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002550 struct port *port = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002551 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002552 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002553 int lacp_fast;
2554
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002555 lacp_fast = bond->params.lacp_fast;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002556 spin_lock_bh(&bond->mode_lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002557 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002558 port = &(SLAVE_AD_INFO(slave)->port);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002559 if (lacp_fast)
2560 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2561 else
2562 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002563 }
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002564 spin_unlock_bh(&bond->mode_lock);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002565}