blob: c5fd4259da331b27503644938ab22787e2eea8ae [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,
Jarod Wilson19ddde12017-03-14 11:48:32 -040095 AD_LINK_SPEED_25000MBPS,
Jianhua Xie424c3232014-11-19 16:48:59 +080096 AD_LINK_SPEED_40000MBPS,
Jiri Pirko3952af42015-12-03 12:12:05 +010097 AD_LINK_SPEED_56000MBPS,
98 AD_LINK_SPEED_100000MBPS,
Jianhua Xiecb8dda92014-11-19 16:48:58 +080099};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
dingtianhong815117a2014-01-02 09:12:54 +0800101/* compare MAC addresses */
102#define MAC_ADDRESS_EQUAL(A, B) \
103 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Eric Dumazetf87fda02016-06-30 16:13:41 +0200105static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
106 0, 0, 0, 0, 0, 0
107};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static u16 ad_ticks_per_sec;
109static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
110
Eric Dumazetf87fda02016-06-30 16:13:41 +0200111static const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned =
112 MULTICAST_LACPDU_ADDR;
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800113
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100114/* ================= main 802.3ad protocol functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int ad_lacpdu_send(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700116static int ad_marker_send(struct port *port, struct bond_marker *marker);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700117static void ad_mux_machine(struct port *port, bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
119static void ad_tx_machine(struct port *port);
120static void ad_periodic_machine(struct port *port);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700121static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
122static void ad_agg_selection_logic(struct aggregator *aggregator,
123 bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static void ad_clear_agg(struct aggregator *aggregator);
125static void ad_initialize_agg(struct aggregator *aggregator);
126static void ad_initialize_port(struct port *port, int lacp_fast);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700127static void ad_enable_collecting_distributing(struct port *port,
128 bool *update_slave_arr);
129static void ad_disable_collecting_distributing(struct port *port,
130 bool *update_slave_arr);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100131static void ad_marker_info_received(struct bond_marker *marker_info,
132 struct port *port);
133static void ad_marker_response_received(struct bond_marker *marker,
134 struct port *port);
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -0700135static void ad_update_actor_keys(struct port *port, bool reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100138/* ================= api to bonding and kernel code ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/**
141 * __get_bond_by_port - get the port's bonding struct
142 * @port: the port we're looking at
143 *
144 * Return @port's bonding struct, or %NULL if it can't be found.
145 */
146static inline struct bonding *__get_bond_by_port(struct port *port)
147{
Bandan Das7bfc4752010-10-16 20:19:59 +0000148 if (port->slave == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return bond_get_bond_by_slave(port->slave);
152}
153
154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * __get_first_agg - get the first aggregator in the bond
156 * @bond: the bond we're looking at
157 *
158 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
159 * found.
Veaceslav Falico768b9542014-01-10 11:59:44 +0100160 * The caller must hold RCU or RTNL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
162static inline struct aggregator *__get_first_agg(struct port *port)
163{
164 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200165 struct slave *first_slave;
Veaceslav Falico768b9542014-01-10 11:59:44 +0100166 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
dingtianhongbe79bd02013-12-13 10:20:12 +0800168 /* If there's no bond for this port, or bond has no slaves */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200169 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100171
dingtianhongbe79bd02013-12-13 10:20:12 +0800172 rcu_read_lock();
173 first_slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +0800174 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
dingtianhongbe79bd02013-12-13 10:20:12 +0800175 rcu_read_unlock();
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100176
Veaceslav Falico768b9542014-01-10 11:59:44 +0100177 return agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100180/**
181 * __agg_has_partner - see if we have a partner
182 * @agg: the agregator we're looking at
Jay Vosburghfd989c82008-11-04 17:51:16 -0800183 *
184 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100185 * address for the partner). Return 0 if not.
Jay Vosburghfd989c82008-11-04 17:51:16 -0800186 */
187static inline int __agg_has_partner(struct aggregator *agg)
188{
189 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/**
193 * __disable_port - disable the port's slave
194 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
196static inline void __disable_port(struct port *port)
197{
dingtianhong5e5b0662014-02-26 11:05:22 +0800198 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201/**
202 * __enable_port - enable the port's slave, if it's up
203 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
205static inline void __enable_port(struct port *port)
206{
207 struct slave *slave = port->slave;
208
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200209 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
dingtianhong5e5b0662014-02-26 11:05:22 +0800210 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/**
214 * __port_is_enabled - check if the port's slave is in active state
215 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
217static inline int __port_is_enabled(struct port *port)
218{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000219 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/**
223 * __get_agg_selection_mode - get the aggregator selection mode
224 * @port: the port we're looking at
225 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800226 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228static inline u32 __get_agg_selection_mode(struct port *port)
229{
230 struct bonding *bond = __get_bond_by_port(port);
231
Bandan Das7bfc4752010-10-16 20:19:59 +0000232 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800233 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000235 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/**
239 * __check_agg_selection_timer - check if the selection timer has expired
240 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242static inline int __check_agg_selection_timer(struct port *port)
243{
244 struct bonding *bond = __get_bond_by_port(port);
245
Bandan Das7bfc4752010-10-16 20:19:59 +0000246 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
250}
251
252/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * __get_link_speed - get a port's speed
254 * @port: the port we're looking at
255 *
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800256 * Return @port's speed in 802.3ad enum format. i.e. one of:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * 0,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800258 * %AD_LINK_SPEED_10MBPS,
259 * %AD_LINK_SPEED_100MBPS,
260 * %AD_LINK_SPEED_1000MBPS,
Jianhua Xie424c3232014-11-19 16:48:59 +0800261 * %AD_LINK_SPEED_2500MBPS,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800262 * %AD_LINK_SPEED_10000MBPS
Jianhua Xie424c3232014-11-19 16:48:59 +0800263 * %AD_LINK_SPEED_20000MBPS
Jarod Wilson19ddde12017-03-14 11:48:32 -0400264 * %AD_LINK_SPEED_25000MBPS
Jianhua Xie424c3232014-11-19 16:48:59 +0800265 * %AD_LINK_SPEED_40000MBPS
266 * %AD_LINK_SPEED_56000MBPS
Jiri Pirko3952af42015-12-03 12:12:05 +0100267 * %AD_LINK_SPEED_100000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269static u16 __get_link_speed(struct port *port)
270{
271 struct slave *slave = port->slave;
272 u16 speed;
273
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100274 /* this if covers only a special case: when the configuration starts
275 * with link down, it sets the speed to 0.
276 * This is done in spite of the fact that the e100 driver reports 0
277 * to be compatible with MVT in the future.
278 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000279 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000280 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000281 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 switch (slave->speed) {
283 case SPEED_10:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800284 speed = AD_LINK_SPEED_10MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286
287 case SPEED_100:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800288 speed = AD_LINK_SPEED_100MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
290
291 case SPEED_1000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800292 speed = AD_LINK_SPEED_1000MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294
Jianhua Xie424c3232014-11-19 16:48:59 +0800295 case SPEED_2500:
296 speed = AD_LINK_SPEED_2500MBPS;
297 break;
298
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700299 case SPEED_10000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800300 speed = AD_LINK_SPEED_10000MBPS;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700301 break;
302
Jianhua Xie424c3232014-11-19 16:48:59 +0800303 case SPEED_20000:
304 speed = AD_LINK_SPEED_20000MBPS;
305 break;
306
Jarod Wilson19ddde12017-03-14 11:48:32 -0400307 case SPEED_25000:
308 speed = AD_LINK_SPEED_25000MBPS;
309 break;
310
Jianhua Xie424c3232014-11-19 16:48:59 +0800311 case SPEED_40000:
312 speed = AD_LINK_SPEED_40000MBPS;
313 break;
314
315 case SPEED_56000:
316 speed = AD_LINK_SPEED_56000MBPS;
317 break;
318
Jiri Pirko3952af42015-12-03 12:12:05 +0100319 case SPEED_100000:
320 speed = AD_LINK_SPEED_100000MBPS;
321 break;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100324 /* unknown speed value from ethtool. shouldn't happen */
325 speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327 }
328 }
329
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200330 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
331 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return speed;
333}
334
335/**
336 * __get_duplex - get a port's duplex
337 * @port: the port we're looking at
338 *
339 * Return @port's duplex in 802.3ad bitmask format. i.e.:
340 * 0x01 if in full duplex
341 * 0x00 otherwise
342 */
343static u8 __get_duplex(struct port *port)
344{
345 struct slave *slave = port->slave;
Mahesh Bandewarb25c2e72015-10-31 12:45:00 -0700346 u8 retval = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100348 /* handling a special case: when the configuration starts with
349 * link down, it sets the duplex to 0.
350 */
Mahesh Bandewarb25c2e72015-10-31 12:45:00 -0700351 if (slave->link == BOND_LINK_UP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 switch (slave->duplex) {
353 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000354 retval = 0x1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200355 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
356 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 case DUPLEX_HALF:
359 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000360 retval = 0x0;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200361 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
362 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 }
365 }
366 return retval;
367}
368
Nikolay Aleksandrov5ee14e62016-02-03 13:17:01 +0100369static void __ad_actor_update_port(struct port *port)
370{
371 const struct bonding *bond = bond_get_bond_by_slave(port->slave);
372
373 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
374 port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority;
375}
376
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100377/* Conversions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/**
380 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
381 * @timer_type: which timer to operate
382 * @par: timer parameter. see below
383 *
384 * If @timer_type is %current_while_timer, @par indicates long/short timer.
385 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100386 * %SLOW_PERIODIC_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
389{
Bandan Das128ea6c2010-10-16 20:19:58 +0000390 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 switch (timer_type) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100393 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
Bandan Das7bfc4752010-10-16 20:19:59 +0000394 if (par)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100395 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
Bandan Das7bfc4752010-10-16 20:19:59 +0000396 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100397 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100399 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
401 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100402 case AD_PERIODIC_TIMER: /* for periodic machine */
403 retval = (par*ad_ticks_per_sec); /* long timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100405 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
407 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100408 case AD_WAIT_WHILE_TIMER: /* for selection machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
410 break;
411 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return retval;
414}
415
416
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100417/* ================= ad_rx_machine helper functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000420 * __choose_matched - update a port's matched variable from a received lacpdu
421 * @lacpdu: the lacpdu we've received
422 * @port: the port we're looking at
423 *
424 * Update the value of the matched variable, using parameter values from a
425 * newly received lacpdu. Parameter values for the partner carried in the
426 * received PDU are compared with the corresponding operational parameter
427 * values for the actor. Matched is set to TRUE if all of these parameters
428 * match and the PDU parameter partner_state.aggregation has the same value as
429 * actor_oper_port_state.aggregation and lacp will actively maintain the link
430 * in the aggregation. Matched is also set to TRUE if the value of
431 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
432 * an individual link and lacp will actively maintain the link. Otherwise,
433 * matched is set to FALSE. LACP is considered to be actively maintaining the
434 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
435 * the actor's actor_oper_port_state.lacp_activity and the PDU's
436 * partner_state.lacp_activity variables are TRUE.
437 *
438 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
439 * used here to implement the language from 802.3ad 43.4.9 that requires
440 * recordPDU to "match" the LACPDU parameters to the stored values.
441 */
442static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
443{
dingtianhong815117a2014-01-02 09:12:54 +0800444 /* check if all parameters are alike
445 * or this is individual link(aggregation == FALSE)
446 * then update the state machine Matched variable.
447 */
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000448 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
449 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
dingtianhong815117a2014-01-02 09:12:54 +0800450 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000451 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
452 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
453 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000454 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
455 ) {
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000456 port->sm_vars |= AD_PORT_MATCHED;
457 } else {
458 port->sm_vars &= ~AD_PORT_MATCHED;
459 }
460}
461
462/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * __record_pdu - record parameters from a received lacpdu
464 * @lacpdu: the lacpdu we've received
465 * @port: the port we're looking at
466 *
467 * Record the parameter values for the Actor carried in a received lacpdu as
468 * the current partner operational parameter values and sets
469 * actor_oper_port_state.defaulted to FALSE.
470 */
471static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
472{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800474 struct port_params *partner = &port->partner_oper;
475
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000476 __choose_matched(lacpdu, port);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100477 /* record the new parameter values for the partner
478 * operational
479 */
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800480 partner->port_number = ntohs(lacpdu->actor_port);
481 partner->port_priority = ntohs(lacpdu->actor_port_priority);
482 partner->system = lacpdu->actor_system;
483 partner->system_priority = ntohs(lacpdu->actor_system_priority);
484 partner->key = ntohs(lacpdu->actor_key);
485 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100487 /* set actor_oper_port_state.defaulted to FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
489
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100490 /* set the partner sync. to on if the partner is sync,
491 * and the port is matched
492 */
Wilson Kok63b46242015-01-26 01:16:59 -0500493 if ((port->sm_vars & AD_PORT_MATCHED) &&
494 (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800495 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Wilson Kok63b46242015-01-26 01:16:59 -0500496 pr_debug("%s partner sync=1\n", port->slave->dev->name);
497 } else {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800498 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Wilson Kok63b46242015-01-26 01:16:59 -0500499 pr_debug("%s partner sync=0\n", port->slave->dev->name);
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502}
503
504/**
505 * __record_default - record default parameters
506 * @port: the port we're looking at
507 *
508 * This function records the default parameter values for the partner carried
509 * in the Partner Admin parameters as the current partner operational parameter
510 * values and sets actor_oper_port_state.defaulted to TRUE.
511 */
512static void __record_default(struct port *port)
513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (port) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100515 /* record the partner admin parameters */
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800516 memcpy(&port->partner_oper, &port->partner_admin,
517 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100519 /* set actor_oper_port_state.defaulted to true */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
521 }
522}
523
524/**
525 * __update_selected - update a port's Selected variable from a received lacpdu
526 * @lacpdu: the lacpdu we've received
527 * @port: the port we're looking at
528 *
529 * Update the value of the selected variable, using parameter values from a
530 * newly received lacpdu. The parameter values for the Actor carried in the
531 * received PDU are compared with the corresponding operational parameter
532 * values for the ports partner. If one or more of the comparisons shows that
533 * the value(s) received in the PDU differ from the current operational values,
534 * then selected is set to FALSE and actor_oper_port_state.synchronization is
535 * set to out_of_sync. Otherwise, selected remains unchanged.
536 */
537static void __update_selected(struct lacpdu *lacpdu, struct port *port)
538{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800540 const struct port_params *partner = &port->partner_oper;
541
dingtianhong815117a2014-01-02 09:12:54 +0800542 /* check if any parameter is different then
543 * update the state machine selected variable.
544 */
Joe Perches8e95a202009-12-03 07:58:21 +0000545 if (ntohs(lacpdu->actor_port) != partner->port_number ||
546 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800547 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000548 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
549 ntohs(lacpdu->actor_key) != partner->key ||
550 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 port->sm_vars &= ~AD_PORT_SELECTED;
552 }
553 }
554}
555
556/**
557 * __update_default_selected - update a port's Selected variable from Partner
558 * @port: the port we're looking at
559 *
560 * This function updates the value of the selected variable, using the partner
561 * administrative parameter values. The administrative values are compared with
562 * the corresponding operational parameter values for the partner. If one or
563 * more of the comparisons shows that the administrative value(s) differ from
564 * the current operational values, then Selected is set to FALSE and
565 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
566 * Selected remains unchanged.
567 */
568static void __update_default_selected(struct port *port)
569{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800571 const struct port_params *admin = &port->partner_admin;
572 const struct port_params *oper = &port->partner_oper;
573
dingtianhong815117a2014-01-02 09:12:54 +0800574 /* check if any parameter is different then
575 * update the state machine selected variable.
576 */
Joe Perches8e95a202009-12-03 07:58:21 +0000577 if (admin->port_number != oper->port_number ||
578 admin->port_priority != oper->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800579 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000580 admin->system_priority != oper->system_priority ||
581 admin->key != oper->key ||
582 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800583 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 port->sm_vars &= ~AD_PORT_SELECTED;
585 }
586 }
587}
588
589/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * __update_ntt - update a port's ntt variable from a received lacpdu
591 * @lacpdu: the lacpdu we've received
592 * @port: the port we're looking at
593 *
594 * Updates the value of the ntt variable, using parameter values from a newly
595 * received lacpdu. The parameter values for the partner carried in the
596 * received PDU are compared with the corresponding operational parameter
597 * values for the Actor. If one or more of the comparisons shows that the
598 * value(s) received in the PDU differ from the current operational values,
599 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
600 */
601static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
602{
dingtianhong815117a2014-01-02 09:12:54 +0800603 /* validate lacpdu and port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (lacpdu && port) {
dingtianhong815117a2014-01-02 09:12:54 +0800605 /* check if any parameter is different then
606 * update the port->ntt.
607 */
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700608 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
609 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
dingtianhong815117a2014-01-02 09:12:54 +0800610 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700611 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
612 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
614 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
615 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
616 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
617 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800618 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 }
621}
622
623/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * __agg_ports_are_ready - check if all ports in an aggregator are ready
625 * @aggregator: the aggregator we're looking at
626 *
627 */
628static int __agg_ports_are_ready(struct aggregator *aggregator)
629{
630 struct port *port;
631 int retval = 1;
632
633 if (aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100634 /* scan all ports in this aggregator to verfy if they are
635 * all ready.
636 */
Bandan Das128ea6c2010-10-16 20:19:58 +0000637 for (port = aggregator->lag_ports;
638 port;
639 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!(port->sm_vars & AD_PORT_READY_N)) {
641 retval = 0;
642 break;
643 }
644 }
645 }
646
647 return retval;
648}
649
650/**
651 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
652 * @aggregator: the aggregator we're looking at
653 * @val: Should the ports' ready bit be set on or off
654 *
655 */
656static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
657{
658 struct port *port;
659
Bandan Das128ea6c2010-10-16 20:19:58 +0000660 for (port = aggregator->lag_ports; port;
661 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000662 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000664 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667}
668
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700669static int __agg_active_ports(struct aggregator *agg)
670{
671 struct port *port;
672 int active = 0;
673
674 for (port = agg->lag_ports; port;
675 port = port->next_port_in_aggregator) {
676 if (port->is_enabled)
677 active++;
678 }
679
680 return active;
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/**
684 * __get_agg_bandwidth - get the total bandwidth of an aggregator
685 * @aggregator: the aggregator we're looking at
686 *
687 */
688static u32 __get_agg_bandwidth(struct aggregator *aggregator)
689{
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700690 int nports = __agg_active_ports(aggregator);
Bandan Das128ea6c2010-10-16 20:19:58 +0000691 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700693 if (nports) {
David Decotigny65cce192011-04-13 15:22:30 +0000694 switch (__get_link_speed(aggregator->lag_ports)) {
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800695 case AD_LINK_SPEED_1MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700696 bandwidth = nports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800698 case AD_LINK_SPEED_10MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700699 bandwidth = nports * 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800701 case AD_LINK_SPEED_100MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700702 bandwidth = nports * 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800704 case AD_LINK_SPEED_1000MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700705 bandwidth = nports * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800707 case AD_LINK_SPEED_2500MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700708 bandwidth = nports * 2500;
Jianhua Xie424c3232014-11-19 16:48:59 +0800709 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800710 case AD_LINK_SPEED_10000MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700711 bandwidth = nports * 10000;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700712 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800713 case AD_LINK_SPEED_20000MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700714 bandwidth = nports * 20000;
Jianhua Xie424c3232014-11-19 16:48:59 +0800715 break;
Jarod Wilson19ddde12017-03-14 11:48:32 -0400716 case AD_LINK_SPEED_25000MBPS:
717 bandwidth = nports * 25000;
718 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800719 case AD_LINK_SPEED_40000MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700720 bandwidth = nports * 40000;
Jianhua Xie424c3232014-11-19 16:48:59 +0800721 break;
722 case AD_LINK_SPEED_56000MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700723 bandwidth = nports * 56000;
Jianhua Xie424c3232014-11-19 16:48:59 +0800724 break;
Jiri Pirko3952af42015-12-03 12:12:05 +0100725 case AD_LINK_SPEED_100000MBPS:
Jay Vosburgh0622cab2016-06-23 14:20:51 -0700726 bandwidth = nports * 100000;
Jiri Pirko3952af42015-12-03 12:12:05 +0100727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100729 bandwidth = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732 return bandwidth;
733}
734
735/**
736 * __get_active_agg - get the current active aggregator
737 * @aggregator: the aggregator we're looking at
Veaceslav Falico49b76242014-01-10 11:59:45 +0100738 *
739 * Caller must hold RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
741static struct aggregator *__get_active_agg(struct aggregator *aggregator)
742{
Veaceslav Falico19177e72013-09-27 16:12:00 +0200743 struct bonding *bond = aggregator->slave->bond;
744 struct list_head *iter;
745 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
dingtianhongbe79bd02013-12-13 10:20:12 +0800747 bond_for_each_slave_rcu(bond, slave, iter)
dingtianhong3fdddd82014-05-12 15:08:43 +0800748 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
749 return &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Veaceslav Falico19177e72013-09-27 16:12:00 +0200751 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/**
755 * __update_lacpdu_from_port - update a port's lacpdu fields
756 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758static inline void __update_lacpdu_from_port(struct port *port)
759{
760 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800761 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100763 /* update current actual Actor parameters
764 * lacpdu->subtype initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * lacpdu->version_number initialized
766 * lacpdu->tlv_type_actor_info initialized
767 * lacpdu->actor_information_length initialized
768 */
769
Al Virod3bb52b2007-08-22 20:06:58 -0400770 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400772 lacpdu->actor_key = htons(port->actor_oper_port_key);
773 lacpdu->actor_port_priority = htons(port->actor_port_priority);
774 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 lacpdu->actor_state = port->actor_oper_port_state;
Wilson Kok63b46242015-01-26 01:16:59 -0500776 pr_debug("update lacpdu: %s, actor port state %x\n",
777 port->slave->dev->name, port->actor_oper_port_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* lacpdu->reserved_3_1 initialized
780 * lacpdu->tlv_type_partner_info initialized
781 * lacpdu->partner_information_length initialized
782 */
783
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800784 lacpdu->partner_system_priority = htons(partner->system_priority);
785 lacpdu->partner_system = partner->system;
786 lacpdu->partner_key = htons(partner->key);
787 lacpdu->partner_port_priority = htons(partner->port_priority);
788 lacpdu->partner_port = htons(partner->port_number);
789 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* lacpdu->reserved_3_2 initialized
792 * lacpdu->tlv_type_collector_info initialized
793 * lacpdu->collector_information_length initialized
794 * collector_max_delay initialized
795 * reserved_12[12] initialized
796 * tlv_type_terminator initialized
797 * terminator_length initialized
798 * reserved_50[50] initialized
799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100802/* ================= main 802.3ad protocol code ========================= */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804/**
805 * ad_lacpdu_send - send out a lacpdu packet on a given port
806 * @port: the port we're looking at
807 *
808 * Returns: 0 on success
809 * < 0 on error
810 */
811static int ad_lacpdu_send(struct port *port)
812{
813 struct slave *slave = port->slave;
814 struct sk_buff *skb;
815 struct lacpdu_header *lacpdu_header;
816 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000819 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700823 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700824 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 skb->protocol = PKT_TYPE_LACPDU;
826 skb->priority = TC_PRIO_CONTROL;
827
828 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
829
Joe Perchesada0f862014-02-15 16:02:17 -0800830 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400831 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100832 * because we use it to identify loopback lacpdus in receive.
833 */
Joe Perchesada0f862014-02-15 16:02:17 -0800834 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800835 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100837 lacpdu_header->lacpdu = port->lacpdu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 dev_queue_xmit(skb);
840
841 return 0;
842}
843
844/**
845 * ad_marker_send - send marker information/response on a given port
846 * @port: the port we're looking at
847 * @marker: marker data to send
848 *
849 * Returns: 0 on success
850 * < 0 on error
851 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700852static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 struct slave *slave = port->slave;
855 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700856 struct bond_marker_header *marker_header;
857 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000860 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 skb_reserve(skb, 16);
864
865 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700866 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700867 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 skb->protocol = PKT_TYPE_LACPDU;
869
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700870 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Joe Perchesada0f862014-02-15 16:02:17 -0800872 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400873 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100874 * because we use it to identify loopback MARKERs in receive.
875 */
Joe Perchesada0f862014-02-15 16:02:17 -0800876 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800877 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100879 marker_header->marker = *marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 dev_queue_xmit(skb);
882
883 return 0;
884}
885
886/**
887 * ad_mux_machine - handle a port's mux state machine
888 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -0700889 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Mahesh Bandewaree637712014-10-04 17:45:01 -0700891static void ad_mux_machine(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 mux_states_t last_state;
894
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100895 /* keep current State Machine state to compare later if it was
896 * changed
897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 last_state = port->sm_mux_state;
899
900 if (port->sm_vars & AD_PORT_BEGIN) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100901 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 } else {
903 switch (port->sm_mux_state) {
904 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000905 if ((port->sm_vars & AD_PORT_SELECTED)
906 || (port->sm_vars & AD_PORT_STANDBY))
907 /* if SELECTED or STANDBY */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100908 port->sm_mux_state = AD_MUX_WAITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 break;
910 case AD_MUX_WAITING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100911 /* if SELECTED == FALSE return to DETACH state */
912 if (!(port->sm_vars & AD_PORT_SELECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100914 /* in order to withhold the Selection Logic to
915 * check all ports READY_N value every callback
916 * cycle to update ready variable, we check
917 * READY_N and update READY here
918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100920 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
922 }
923
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100924 /* check if the wait_while_timer expired */
Bandan Das7bfc4752010-10-16 20:19:59 +0000925 if (port->sm_mux_timer_counter
926 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100929 /* in order to withhold the selection logic to check
930 * all ports READY_N value every callback cycle to
931 * update ready variable, we check READY_N and update
932 * READY here
933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
935
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100936 /* if the wait_while_timer expired, and the port is
937 * in READY state, move to ATTACHED state
938 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000939 if ((port->sm_vars & AD_PORT_READY)
940 && !port->sm_mux_timer_counter)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100941 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
943 case AD_MUX_ATTACHED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100944 /* check also if agg_select_timer expired (so the
945 * edable port will take place only after this timer)
946 */
947 if ((port->sm_vars & AD_PORT_SELECTED) &&
948 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
949 !__check_agg_selection_timer(port)) {
Wilson Kok63b46242015-01-26 01:16:59 -0500950 if (port->aggregator->is_active)
951 port->sm_mux_state =
952 AD_MUX_COLLECTING_DISTRIBUTING;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100953 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
954 (port->sm_vars & AD_PORT_STANDBY)) {
955 /* if UNSELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100957 /* in order to withhold the selection logic to
958 * check all ports READY_N value every callback
959 * cycle to update ready variable, we check
960 * READY_N and update READY here
961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100963 port->sm_mux_state = AD_MUX_DETACHED;
Wilson Kok63b46242015-01-26 01:16:59 -0500964 } else if (port->aggregator->is_active) {
965 port->actor_oper_port_state |=
966 AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 break;
969 case AD_MUX_COLLECTING_DISTRIBUTING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100970 if (!(port->sm_vars & AD_PORT_SELECTED) ||
971 (port->sm_vars & AD_PORT_STANDBY) ||
Wilson Kok63b46242015-01-26 01:16:59 -0500972 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) ||
973 !(port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100974 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100976 /* if port state hasn't changed make
977 * sure that a collecting distributing
978 * port in an active aggregator is enabled
979 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (port->aggregator &&
981 port->aggregator->is_active &&
982 !__port_is_enabled(port)) {
983
984 __enable_port(port);
985 }
986 }
987 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100988 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
990 }
991 }
992
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100993 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (port->sm_mux_state != last_state) {
Wilson Kok63b46242015-01-26 01:16:59 -0500995 pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
996 port->actor_port_number,
997 port->slave->dev->name,
998 last_state,
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800999 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 switch (port->sm_mux_state) {
1001 case AD_MUX_DETACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
Mahesh Bandewaree637712014-10-04 17:45:01 -07001003 ad_disable_collecting_distributing(port,
1004 update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1006 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001007 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
1009 case AD_MUX_WAITING:
1010 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1011 break;
1012 case AD_MUX_ATTACHED:
Wilson Kok63b46242015-01-26 01:16:59 -05001013 if (port->aggregator->is_active)
1014 port->actor_oper_port_state |=
1015 AD_STATE_SYNCHRONIZATION;
1016 else
1017 port->actor_oper_port_state &=
1018 ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1020 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Mahesh Bandewaree637712014-10-04 17:45:01 -07001021 ad_disable_collecting_distributing(port,
1022 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001023 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025 case AD_MUX_COLLECTING_DISTRIBUTING:
1026 port->actor_oper_port_state |= AD_STATE_COLLECTING;
1027 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
Wilson Kok63b46242015-01-26 01:16:59 -05001028 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
Mahesh Bandewaree637712014-10-04 17:45:01 -07001029 ad_enable_collecting_distributing(port,
1030 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001031 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001033 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035 }
1036 }
1037}
1038
1039/**
1040 * ad_rx_machine - handle a port's rx State Machine
1041 * @lacpdu: the lacpdu we've received
1042 * @port: the port we're looking at
1043 *
1044 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1045 * CURRENT. If timer expired set the state machine in the proper state.
1046 * In other cases, this function checks if we need to switch to other state.
1047 */
1048static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1049{
1050 rx_states_t last_state;
1051
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001052 /* keep current State Machine state to compare later if it was
1053 * changed
1054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 last_state = port->sm_rx_state;
1056
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001057 /* check if state machine should change state */
1058
1059 /* first, check if port was reinitialized */
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001060 if (port->sm_vars & AD_PORT_BEGIN) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001061 port->sm_rx_state = AD_RX_INITIALIZE;
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001062 port->sm_vars |= AD_PORT_CHURNED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001063 /* check if port is not enabled */
Mahesh Bandewarec891c82017-03-08 10:55:59 -08001064 } else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled)
Bandan Das7bfc4752010-10-16 20:19:59 +00001065 port->sm_rx_state = AD_RX_PORT_DISABLED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001066 /* check if new lacpdu arrived */
1067 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1068 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1069 (port->sm_rx_state == AD_RX_CURRENT))) {
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001070 if (port->sm_rx_state != AD_RX_CURRENT)
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001071 port->sm_vars |= AD_PORT_CHURNED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001072 port->sm_rx_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 port->sm_rx_state = AD_RX_CURRENT;
1074 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001075 /* if timer is on, and if it is expired */
1076 if (port->sm_rx_timer_counter &&
1077 !(--port->sm_rx_timer_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 switch (port->sm_rx_state) {
1079 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001080 port->sm_rx_state = AD_RX_DEFAULTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 break;
1082 case AD_RX_CURRENT:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001083 port->sm_rx_state = AD_RX_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001085 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 break;
1087 }
1088 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001089 /* if no lacpdu arrived and no timer is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 switch (port->sm_rx_state) {
1091 case AD_RX_PORT_DISABLED:
Mahesh Bandewarec891c82017-03-08 10:55:59 -08001092 if (port->is_enabled &&
1093 (port->sm_vars & AD_PORT_LACP_ENABLED))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001094 port->sm_rx_state = AD_RX_EXPIRED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001095 else if (port->is_enabled
1096 && ((port->sm_vars
1097 & AD_PORT_LACP_ENABLED) == 0))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001098 port->sm_rx_state = AD_RX_LACP_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001100 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
1102
1103 }
1104 }
1105 }
1106
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001107 /* check if the State machine was changed or new lacpdu arrived */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Wilson Kok63b46242015-01-26 01:16:59 -05001109 pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1110 port->actor_port_number,
1111 port->slave->dev->name,
1112 last_state,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001113 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 switch (port->sm_rx_state) {
1115 case AD_RX_INITIALIZE:
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001116 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001118 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 port->sm_vars &= ~AD_PORT_SELECTED;
1121 __record_default(port);
1122 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001123 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001125 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 case AD_RX_PORT_DISABLED:
1127 port->sm_vars &= ~AD_PORT_MATCHED;
1128 break;
1129 case AD_RX_LACP_DISABLED:
1130 port->sm_vars &= ~AD_PORT_SELECTED;
1131 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001132 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 port->sm_vars |= AD_PORT_MATCHED;
1134 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1135 break;
1136 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001137 /* Reset of the Synchronization flag (Standard 43.4.12)
1138 * This reset cause to disable this port in the
1139 * COLLECTING_DISTRIBUTING state of the mux machine in
1140 * case of EXPIRED even if LINK_DOWN didn't arrive for
1141 * the port.
1142 */
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001143 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 port->sm_vars &= ~AD_PORT_MATCHED;
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001145 port->partner_oper.port_state |= AD_STATE_LACP_TIMEOUT;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001146 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1148 port->actor_oper_port_state |= AD_STATE_EXPIRED;
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001149 port->sm_vars |= AD_PORT_CHURNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 case AD_RX_DEFAULTED:
1152 __update_default_selected(port);
1153 __record_default(port);
1154 port->sm_vars |= AD_PORT_MATCHED;
1155 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1156 break;
1157 case AD_RX_CURRENT:
dingtianhong815117a2014-01-02 09:12:54 +08001158 /* detect loopback situation */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001159 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1160 &(port->actor_system))) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001161 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
Joe Perches90194262014-02-15 16:01:45 -08001162 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001163 port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return;
1165 }
1166 __update_selected(lacpdu, port);
1167 __update_ntt(lacpdu, port);
1168 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1170 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001172 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 }
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178/**
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001179 * ad_churn_machine - handle port churn's state machine
1180 * @port: the port we're looking at
1181 *
1182 */
1183static void ad_churn_machine(struct port *port)
1184{
Mahesh Bandewaref015d72015-03-30 14:30:40 -07001185 if (port->sm_vars & AD_PORT_CHURNED) {
1186 port->sm_vars &= ~AD_PORT_CHURNED;
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001187 port->sm_churn_actor_state = AD_CHURN_MONITOR;
1188 port->sm_churn_partner_state = AD_CHURN_MONITOR;
1189 port->sm_churn_actor_timer_counter =
1190 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1191 port->sm_churn_partner_timer_counter =
1192 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1193 return;
1194 }
1195 if (port->sm_churn_actor_timer_counter &&
1196 !(--port->sm_churn_actor_timer_counter) &&
1197 port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1198 if (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION) {
1199 port->sm_churn_actor_state = AD_NO_CHURN;
1200 } else {
1201 port->churn_actor_count++;
1202 port->sm_churn_actor_state = AD_CHURN;
1203 }
1204 }
1205 if (port->sm_churn_partner_timer_counter &&
1206 !(--port->sm_churn_partner_timer_counter) &&
1207 port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1208 if (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) {
1209 port->sm_churn_partner_state = AD_NO_CHURN;
1210 } else {
1211 port->churn_partner_count++;
1212 port->sm_churn_partner_state = AD_CHURN;
1213 }
1214 }
1215}
1216
1217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * ad_tx_machine - handle a port's tx state machine
1219 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
1221static void ad_tx_machine(struct port *port)
1222{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001223 /* check if tx timer expired, to verify that we do not send more than
1224 * 3 packets per second
1225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001227 /* check if there is something to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1229 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001232 pr_debug("Sent LACPDU on port %d\n",
1233 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001234
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001235 /* mark ntt as false, so it will not be sent
1236 * again until demanded
1237 */
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001238 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001241 /* restart tx timer(to verify that we will not exceed
1242 * AD_MAX_TX_IN_SECOND
1243 */
1244 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246}
1247
1248/**
1249 * ad_periodic_machine - handle a port's periodic state machine
1250 * @port: the port we're looking at
1251 *
1252 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1253 */
1254static void ad_periodic_machine(struct port *port)
1255{
1256 periodic_states_t last_state;
1257
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001258 /* keep current state machine state to compare later if it was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 last_state = port->sm_periodic_state;
1260
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001261 /* check if port was reinitialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001263 (!(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 -07001264 ) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001265 port->sm_periodic_state = AD_NO_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001267 /* check if state machine should change state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 else if (port->sm_periodic_timer_counter) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001269 /* check if periodic state machine expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (!(--port->sm_periodic_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001271 /* if expired then do tx */
1272 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001274 /* If not expired, check if there is some new timeout
1275 * parameter from the partner state
1276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 switch (port->sm_periodic_state) {
1278 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001279 if (!(port->partner_oper.port_state
1280 & AD_STATE_LACP_TIMEOUT))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001281 port->sm_periodic_state = AD_SLOW_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
1283 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001284 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 port->sm_periodic_timer_counter = 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001286 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001289 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291 }
1292 }
1293 } else {
1294 switch (port->sm_periodic_state) {
1295 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001296 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
1298 case AD_PERIODIC_TX:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001299 if (!(port->partner_oper.port_state &
1300 AD_STATE_LACP_TIMEOUT))
1301 port->sm_periodic_state = AD_SLOW_PERIODIC;
Bandan Das7bfc4752010-10-16 20:19:59 +00001302 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001303 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001305 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
1307 }
1308 }
1309
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001310 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001312 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1313 port->actor_port_number, last_state,
1314 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (port->sm_periodic_state) {
1316 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001317 port->sm_periodic_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 case AD_FAST_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001320 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1321 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 -07001322 break;
1323 case AD_SLOW_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001324 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1325 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 -07001326 break;
1327 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001328 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001330 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332 }
1333 }
1334}
1335
1336/**
1337 * ad_port_selection_logic - select aggregation groups
1338 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001339 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
1341 * Select aggregation groups, and assign each port for it's aggregetor. The
1342 * selection logic is called in the inititalization (after all the handshkes),
1343 * and after every lacpdu receive (if selected is off).
1344 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001345static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1348 struct port *last_port = NULL, *curr_port;
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001349 struct list_head *iter;
1350 struct bonding *bond;
1351 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int found = 0;
1353
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001354 /* if the port is already Selected, do nothing */
Bandan Das7bfc4752010-10-16 20:19:59 +00001355 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001358 bond = __get_bond_by_port(port);
1359
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001360 /* if the port is connected to other aggregator, detach it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (port->aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001362 /* detach the port from its former aggregator */
Bandan Das128ea6c2010-10-16 20:19:58 +00001363 temp_aggregator = port->aggregator;
1364 for (curr_port = temp_aggregator->lag_ports; curr_port;
1365 last_port = curr_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001366 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (curr_port == port) {
1368 temp_aggregator->num_of_ports--;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001369 /* if it is the first port attached to the
1370 * aggregator
1371 */
1372 if (!last_port) {
Bandan Das128ea6c2010-10-16 20:19:58 +00001373 temp_aggregator->lag_ports =
1374 port->next_port_in_aggregator;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001375 } else {
1376 /* not the first port attached to the
1377 * aggregator
1378 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001379 last_port->next_port_in_aggregator =
1380 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001383 /* clear the port's relations to this
1384 * aggregator
1385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001387 port->next_port_in_aggregator = NULL;
1388 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001390 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1391 port->actor_port_number,
1392 temp_aggregator->aggregator_identifier);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001393 /* if the aggregator is empty, clear its
1394 * parameters, and set it ready to be attached
1395 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001396 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 }
1400 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001401 if (!curr_port) {
1402 /* meaning: the port was related to an aggregator
1403 * but was not on the aggregator port list
1404 */
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001405 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1406 port->slave->bond->dev->name,
1407 port->actor_port_number,
1408 port->slave->dev->name,
1409 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001412 /* search on all aggregators for a suitable aggregator for this port */
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001413 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001414 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001416 /* keep a free aggregator for later use(if needed) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001418 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001419 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 continue;
1421 }
dingtianhong815117a2014-01-02 09:12:54 +08001422 /* check if current aggregator suits us */
1423 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1424 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001425 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1426 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ) &&
dingtianhong815117a2014-01-02 09:12:54 +08001428 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1429 !aggregator->is_individual) /* but is not individual OR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 )
1431 ) {
dingtianhong815117a2014-01-02 09:12:54 +08001432 /* attach to the founded aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001434 port->actor_port_aggregator_identifier =
1435 port->aggregator->aggregator_identifier;
1436 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001438 aggregator->lag_ports = port;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001439 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1440 port->actor_port_number,
1441 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001443 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 port->sm_vars |= AD_PORT_SELECTED;
1445 found = 1;
1446 break;
1447 }
1448 }
1449
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001450 /* the port couldn't find an aggregator - attach it to a new
1451 * aggregator
1452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!found) {
1454 if (free_aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001455 /* assign port a new aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001457 port->actor_port_aggregator_identifier =
1458 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001460 /* update the new aggregator's parameters
1461 * if port was responsed from the end-user
1462 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001463 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
Bandan Das7bfc4752010-10-16 20:19:59 +00001464 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001465 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001466 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001467 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Mahesh Bandewarc3cd9ee2015-04-07 16:16:11 -07001469 port->aggregator->actor_admin_aggregator_key =
1470 port->actor_admin_port_key;
1471 port->aggregator->actor_oper_aggregator_key =
1472 port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001473 port->aggregator->partner_system =
1474 port->partner_oper.system;
1475 port->aggregator->partner_system_priority =
1476 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001477 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 port->aggregator->receive_state = 1;
1479 port->aggregator->transmit_state = 1;
1480 port->aggregator->lag_ports = port;
1481 port->aggregator->num_of_ports++;
1482
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001483 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 port->sm_vars |= AD_PORT_SELECTED;
1485
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001486 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1487 port->actor_port_number,
1488 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001490 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 port->actor_port_number, port->slave->dev->name);
1492 }
1493 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001494 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1495 * in all aggregator's ports, else set ready=FALSE in all
1496 * aggregator's ports
1497 */
1498 __set_agg_ports_ready(port->aggregator,
1499 __agg_ports_are_ready(port->aggregator));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Jay Vosburghfd989c82008-11-04 17:51:16 -08001501 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001502 ad_agg_selection_logic(aggregator, update_slave_arr);
Wilson Kok63b46242015-01-26 01:16:59 -05001503
1504 if (!port->aggregator->is_active)
1505 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001506}
1507
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001508/* Decide if "agg" is a better choice for the new active aggregator that
Jay Vosburghfd989c82008-11-04 17:51:16 -08001509 * the current best, according to the ad_select policy.
1510 */
1511static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1512 struct aggregator *curr)
1513{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001514 /* 0. If no best, select current.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001515 *
1516 * 1. If the current agg is not individual, and the best is
1517 * individual, select current.
1518 *
1519 * 2. If current agg is individual and the best is not, keep best.
1520 *
1521 * 3. Therefore, current and best are both individual or both not
1522 * individual, so:
1523 *
1524 * 3a. If current agg partner replied, and best agg partner did not,
1525 * select current.
1526 *
1527 * 3b. If current agg partner did not reply and best agg partner
1528 * did reply, keep best.
1529 *
1530 * 4. Therefore, current and best both have partner replies or
1531 * both do not, so perform selection policy:
1532 *
1533 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1534 * select by bandwidth.
1535 *
1536 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1537 */
1538 if (!best)
1539 return curr;
1540
1541 if (!curr->is_individual && best->is_individual)
1542 return curr;
1543
1544 if (curr->is_individual && !best->is_individual)
1545 return best;
1546
1547 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1548 return curr;
1549
1550 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1551 return best;
1552
1553 switch (__get_agg_selection_mode(curr->lag_ports)) {
1554 case BOND_AD_COUNT:
Jay Vosburgh0622cab2016-06-23 14:20:51 -07001555 if (__agg_active_ports(curr) > __agg_active_ports(best))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001556 return curr;
1557
Jay Vosburgh0622cab2016-06-23 14:20:51 -07001558 if (__agg_active_ports(curr) < __agg_active_ports(best))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001559 return best;
1560
1561 /*FALLTHROUGH*/
1562 case BOND_AD_STABLE:
1563 case BOND_AD_BANDWIDTH:
1564 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1565 return curr;
1566
1567 break;
1568
1569 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001570 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1571 curr->slave->bond->dev->name,
1572 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001575
1576 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001579static int agg_device_up(const struct aggregator *agg)
1580{
Jiri Bohac2430af82011-04-19 02:09:55 +00001581 struct port *port = agg->lag_ports;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001582
Jiri Bohac2430af82011-04-19 02:09:55 +00001583 if (!port)
1584 return 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001585
Jay Vosburgh0622cab2016-06-23 14:20:51 -07001586 for (port = agg->lag_ports; port;
1587 port = port->next_port_in_aggregator) {
1588 if (netif_running(port->slave->dev) &&
1589 netif_carrier_ok(port->slave->dev))
1590 return 1;
1591 }
1592
1593 return 0;
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/**
1597 * ad_agg_selection_logic - select an aggregation group for a team
1598 * @aggregator: the aggregator we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001599 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
1601 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001602 *
1603 * The logic of this function is to select the aggregator according to
1604 * the ad_select policy:
1605 *
1606 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1607 * it, and to reselect the active aggregator only if the previous
1608 * aggregator has no more ports related to it.
1609 *
1610 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1611 * bandwidth, and reselect whenever a link state change takes place or the
1612 * set of slaves in the bond changes.
1613 *
1614 * BOND_AD_COUNT: select the aggregator with largest number of ports
1615 * (slaves), and reselect whenever a link state change takes place or the
1616 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 *
1618 * FIXME: this function MUST be called with the first agg in the bond, or
1619 * __get_active_agg() won't work correctly. This function should be better
1620 * called with the bond itself, and retrieve the first agg from it.
1621 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001622static void ad_agg_selection_logic(struct aggregator *agg,
1623 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001625 struct aggregator *best, *active, *origin;
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001626 struct bonding *bond = agg->slave->bond;
1627 struct list_head *iter;
1628 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Veaceslav Falico49b76242014-01-10 11:59:45 +01001631 rcu_read_lock();
Jay Vosburghfd989c82008-11-04 17:51:16 -08001632 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001633 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001634 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
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);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001638
Jay Vosburghfd989c82008-11-04 17:51:16 -08001639 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jay Vosburgh0622cab2016-06-23 14:20:51 -07001641 if (__agg_active_ports(agg) && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001642 best = ad_agg_selection_test(best, agg);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jay Vosburghfd989c82008-11-04 17:51:16 -08001645 if (best &&
1646 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001647 /* For the STABLE policy, don't replace the old active
Jay Vosburghfd989c82008-11-04 17:51:16 -08001648 * aggregator if it's still active (it has an answering
1649 * partner) or if both the best and active don't have an
1650 * answering partner.
1651 */
1652 if (active && active->lag_ports &&
Jay Vosburgh0622cab2016-06-23 14:20:51 -07001653 __agg_active_ports(active) &&
Jay Vosburghfd989c82008-11-04 17:51:16 -08001654 (__agg_has_partner(active) ||
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001655 (!__agg_has_partner(active) &&
1656 !__agg_has_partner(best)))) {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001657 if (!(!active->actor_oper_aggregator_key &&
1658 best->actor_oper_aggregator_key)) {
1659 best = NULL;
1660 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662 }
1663 }
1664
Jay Vosburghfd989c82008-11-04 17:51:16 -08001665 if (best && (best == active)) {
1666 best = NULL;
1667 active->is_active = 1;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
dingtianhongbe79bd02013-12-13 10:20:12 +08001670 /* if there is new best aggregator, activate it */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001671 if (best) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001672 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1673 best->aggregator_identifier, best->num_of_ports,
1674 best->actor_oper_aggregator_key,
1675 best->partner_oper_aggregator_key,
1676 best->is_individual, best->is_active);
1677 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1678 best->lag_ports, best->slave,
1679 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001680
dingtianhongbe79bd02013-12-13 10:20:12 +08001681 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001682 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001683
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001684 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1685 agg->aggregator_identifier, agg->num_of_ports,
1686 agg->actor_oper_aggregator_key,
1687 agg->partner_oper_aggregator_key,
1688 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
dingtianhongbe79bd02013-12-13 10:20:12 +08001691 /* check if any partner replys */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001692 if (best->is_individual) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001693 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1694 best->slave ?
1695 best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
Jay Vosburghfd989c82008-11-04 17:51:16 -08001698 best->is_active = 1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001699 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1700 best->aggregator_identifier);
1701 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1702 best->aggregator_identifier, best->num_of_ports,
1703 best->actor_oper_aggregator_key,
1704 best->partner_oper_aggregator_key,
1705 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001707 /* disable the ports that were related to the former
1708 * active_aggregator
1709 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001710 if (active) {
1711 for (port = active->lag_ports; port;
1712 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 __disable_port(port);
1714 }
1715 }
Mahesh Bandewaree637712014-10-04 17:45:01 -07001716 /* Slave array needs update. */
1717 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001720 /* if the selected aggregator is of join individuals
Jay Vosburghfd989c82008-11-04 17:51:16 -08001721 * (partner_system is NULL), enable their ports
1722 */
1723 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Jay Vosburghfd989c82008-11-04 17:51:16 -08001725 if (active) {
1726 if (!__agg_has_partner(active)) {
1727 for (port = active->lag_ports; port;
1728 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 __enable_port(port);
1730 }
1731 }
1732 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001733
dingtianhongbe79bd02013-12-13 10:20:12 +08001734 rcu_read_unlock();
1735
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001736 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739/**
1740 * ad_clear_agg - clear a given aggregator's parameters
1741 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
1743static void ad_clear_agg(struct aggregator *aggregator)
1744{
1745 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001746 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 aggregator->actor_admin_aggregator_key = 0;
1748 aggregator->actor_oper_aggregator_key = 0;
Eric Dumazetf87fda02016-06-30 16:13:41 +02001749 eth_zero_addr(aggregator->partner_system.mac_addr_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 aggregator->partner_system_priority = 0;
1751 aggregator->partner_oper_aggregator_key = 0;
1752 aggregator->receive_state = 0;
1753 aggregator->transmit_state = 0;
1754 aggregator->lag_ports = NULL;
1755 aggregator->is_active = 0;
1756 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001757 pr_debug("LAG %d was cleared\n",
1758 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760}
1761
1762/**
1763 * ad_initialize_agg - initialize a given aggregator's parameters
1764 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
1766static void ad_initialize_agg(struct aggregator *aggregator)
1767{
1768 if (aggregator) {
1769 ad_clear_agg(aggregator);
1770
Eric Dumazetf87fda02016-06-30 16:13:41 +02001771 eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 aggregator->aggregator_identifier = 0;
1773 aggregator->slave = NULL;
1774 }
1775}
1776
1777/**
1778 * ad_initialize_port - initialize a given port's parameters
1779 * @aggregator: the aggregator we're looking at
1780 * @lacp_fast: boolean. whether fast periodic should be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
1782static void ad_initialize_port(struct port *port, int lacp_fast)
1783{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001784 static const struct port_params tmpl = {
1785 .system_priority = 0xffff,
1786 .key = 1,
1787 .port_number = 1,
1788 .port_priority = 0xff,
1789 .port_state = 1,
1790 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001791 static const struct lacpdu lacpdu = {
1792 .subtype = 0x01,
1793 .version_number = 0x01,
1794 .tlv_type_actor_info = 0x01,
1795 .actor_information_length = 0x14,
1796 .tlv_type_partner_info = 0x02,
1797 .partner_information_length = 0x14,
1798 .tlv_type_collector_info = 0x03,
1799 .collector_information_length = 0x10,
1800 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1801 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 port->actor_port_priority = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001806 port->ntt = false;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001807 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1808 AD_STATE_LACP_ACTIVITY;
1809 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1810 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Bandan Das7bfc4752010-10-16 20:19:59 +00001812 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001815 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1816 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1817
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001818 port->is_enabled = true;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001819 /* private parameters */
Mahesh Bandewar19a12042015-03-27 22:34:31 -07001820 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 port->sm_rx_state = 0;
1822 port->sm_rx_timer_counter = 0;
1823 port->sm_periodic_state = 0;
1824 port->sm_periodic_timer_counter = 0;
1825 port->sm_mux_state = 0;
1826 port->sm_mux_timer_counter = 0;
1827 port->sm_tx_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 port->aggregator = NULL;
1829 port->next_port_in_aggregator = NULL;
1830 port->transaction_id = 0;
1831
Mahesh Bandewar14c95512015-02-23 17:50:11 -08001832 port->sm_churn_actor_timer_counter = 0;
1833 port->sm_churn_actor_state = 0;
1834 port->churn_actor_count = 0;
1835 port->sm_churn_partner_timer_counter = 0;
1836 port->sm_churn_partner_state = 0;
1837 port->churn_partner_count = 0;
1838
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001839 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841}
1842
1843/**
1844 * ad_enable_collecting_distributing - enable a port's transmit/receive
1845 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001846 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 *
1848 * Enable @port if it's in an active aggregator
1849 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001850static void ad_enable_collecting_distributing(struct port *port,
1851 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001854 pr_debug("Enabling port %d(LAG %d)\n",
1855 port->actor_port_number,
1856 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 __enable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001858 /* Slave array needs update */
1859 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861}
1862
1863/**
1864 * ad_disable_collecting_distributing - disable a port's transmit/receive
1865 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001866 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001868static void ad_disable_collecting_distributing(struct port *port,
1869 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001871 if (port->aggregator &&
1872 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1873 &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001874 pr_debug("Disabling port %d(LAG %d)\n",
1875 port->actor_port_number,
1876 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 __disable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001878 /* Slave array needs an update */
1879 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881}
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883/**
1884 * ad_marker_info_received - handle receive of a Marker information frame
1885 * @marker_info: Marker info received
1886 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001888static void ad_marker_info_received(struct bond_marker *marker_info,
1889 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001891 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001893 /* copy the received marker data to the response marker */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001894 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001895 /* change the marker subtype to marker response */
Bandan Das128ea6c2010-10-16 20:19:58 +00001896 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001898 /* send the marker response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001900 pr_debug("Sent Marker Response on port %d\n",
1901 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903}
1904
1905/**
1906 * ad_marker_response_received - handle receive of a marker response frame
1907 * @marker: marker PDU received
1908 * @port: the port we're looking at
1909 *
1910 * This function does nothing since we decided not to implement send and handle
1911 * response for marker PDU's, in this stage, but only to respond to marker
1912 * information.
1913 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001914static void ad_marker_response_received(struct bond_marker *marker,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001915 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001917 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001920/* ========= AD exported functions to the main bonding code ========= */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001922/* Check aggregators status in team every T seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923#define AD_AGGREGATOR_SELECTION_TIMER 8
1924
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001925/**
1926 * bond_3ad_initiate_agg_selection - initate aggregator selection
1927 * @bond: bonding struct
Jay Vosburghfd989c82008-11-04 17:51:16 -08001928 *
1929 * Set the aggregation selection timer, to initiate an agg selection in
1930 * the very near future. Called during first initialization, and during
1931 * any down to up transitions of the bond.
1932 */
1933void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1934{
1935 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/**
1939 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1940 * @bond: bonding struct to work on
1941 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 *
1943 * Can be called only after the mac address of the bond is set.
1944 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001945void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001946{
dingtianhong815117a2014-01-02 09:12:54 +08001947 /* check that the bond is not initialized yet */
1948 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001949 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001951 BOND_AD_INFO(bond).aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Mahesh Bandewar6791e462015-05-09 00:01:55 -07001953 BOND_AD_INFO(bond).system.sys_priority =
1954 bond->params.ad_actor_sys_prio;
Mahesh Bandewar74514952015-05-09 00:01:56 -07001955 if (is_zero_ether_addr(bond->params.ad_actor_system))
1956 BOND_AD_INFO(bond).system.sys_mac_addr =
1957 *((struct mac_addr *)bond->dev->dev_addr);
1958 else
1959 BOND_AD_INFO(bond).system.sys_mac_addr =
1960 *((struct mac_addr *)bond->params.ad_actor_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001962 /* initialize how many times this module is called in one
1963 * second (should be about every 100ms)
1964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 ad_ticks_per_sec = tick_resolution;
1966
Jay Vosburghfd989c82008-11-04 17:51:16 -08001967 bond_3ad_initiate_agg_selection(bond,
1968 AD_AGGREGATOR_SELECTION_TIMER *
1969 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971}
1972
1973/**
1974 * bond_3ad_bind_slave - initialize a slave's port
1975 * @slave: slave struct to work on
1976 *
1977 * Returns: 0 on success
1978 * < 0 on error
1979 */
dingtianhong359632e2014-01-02 09:13:12 +08001980void bond_3ad_bind_slave(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct bonding *bond = bond_get_bond_by_slave(slave);
1983 struct port *port;
1984 struct aggregator *aggregator;
1985
dingtianhong359632e2014-01-02 09:13:12 +08001986 /* check that the slave has not been initialized yet. */
dingtianhong3fdddd82014-05-12 15:08:43 +08001987 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
dingtianhong359632e2014-01-02 09:13:12 +08001989 /* port initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001990 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001992 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 port->slave = slave;
dingtianhong3fdddd82014-05-12 15:08:43 +08001995 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07001996 /* key is determined according to the link speed, duplex and
1997 * user key
dingtianhong359632e2014-01-02 09:13:12 +08001998 */
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07001999 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002000 ad_update_actor_keys(port, false);
dingtianhong359632e2014-01-02 09:13:12 +08002001 /* actor system is the bond's system */
Nikolay Aleksandrov5ee14e62016-02-03 13:17:01 +01002002 __ad_actor_update_port(port);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002003 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2004 * lacpdu's are sent in one second)
2005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
dingtianhong359632e2014-01-02 09:13:12 +08002010 /* aggregator initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08002011 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 ad_initialize_agg(aggregator);
2014
2015 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
Jiri Bohac163c8ff2014-02-14 18:13:50 +01002016 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 aggregator->slave = slave;
2018 aggregator->is_active = 0;
2019 aggregator->num_of_ports = 0;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023/**
2024 * bond_3ad_unbind_slave - deinitialize a slave's port
2025 * @slave: slave struct to work on
2026 *
2027 * Search for the aggregator that is related to this port, remove the
2028 * aggregator and assign another aggregator for other port related to it
2029 * (if any), and remove the port.
2030 */
2031void bond_3ad_unbind_slave(struct slave *slave)
2032{
2033 struct port *port, *prev_port, *temp_port;
2034 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2035 int select_new_active_agg = 0;
Veaceslav Falico0b0882642013-09-27 16:12:02 +02002036 struct bonding *bond = slave->bond;
2037 struct slave *slave_iter;
2038 struct list_head *iter;
Mahesh Bandewaree637712014-10-04 17:45:01 -07002039 bool dummy_slave_update; /* Ignore this value as caller updates array */
Jasper Spaansa361c832009-10-23 04:09:24 +00002040
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002041 /* Sync against bond_3ad_state_machine_handler() */
2042 spin_lock_bh(&bond->mode_lock);
dingtianhong3fdddd82014-05-12 15:08:43 +08002043 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2044 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002046 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002048 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
2049 slave->dev->name);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002050 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002053 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
2054 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /* Tell the partner that this port is not suitable for aggregation */
2057 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2058 __update_lacpdu_from_port(port);
2059 ad_lacpdu_send(port);
2060
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002061 /* check if this aggregator is occupied */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (aggregator->lag_ports) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002063 /* check if there are other ports related to this aggregator
2064 * except the port related to this slave(thats ensure us that
2065 * there is a reason to search for new aggregator, and that we
2066 * will find one
2067 */
2068 if ((aggregator->lag_ports != port) ||
2069 (aggregator->lag_ports->next_port_in_aggregator)) {
2070 /* find new aggregator for the related port(s) */
Veaceslav Falico0b0882642013-09-27 16:12:02 +02002071 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002072 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002073 /* if the new aggregator is empty, or it is
2074 * connected to our port only
2075 */
2076 if (!new_aggregator->lag_ports ||
2077 ((new_aggregator->lag_ports == port) &&
2078 !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Veaceslav Falico0b0882642013-09-27 16:12:02 +02002081 if (!slave_iter)
2082 new_aggregator = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002083
2084 /* if new aggregator found, copy the aggregator's
2085 * parameters and connect the related lag_ports to the
2086 * new aggregator
2087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 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 +02002089 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2090 aggregator->aggregator_identifier,
2091 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002093 if ((new_aggregator->lag_ports == port) &&
2094 new_aggregator->is_active) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002095 netdev_info(bond->dev, "Removing an active aggregator\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 select_new_active_agg = 1;
2097 }
2098
2099 new_aggregator->is_individual = aggregator->is_individual;
2100 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2101 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2102 new_aggregator->partner_system = aggregator->partner_system;
2103 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2104 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2105 new_aggregator->receive_state = aggregator->receive_state;
2106 new_aggregator->transmit_state = aggregator->transmit_state;
2107 new_aggregator->lag_ports = aggregator->lag_ports;
2108 new_aggregator->is_active = aggregator->is_active;
2109 new_aggregator->num_of_ports = aggregator->num_of_ports;
2110
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002111 /* update the information that is written on
2112 * the ports about the aggregator
2113 */
Bandan Das128ea6c2010-10-16 20:19:58 +00002114 for (temp_port = aggregator->lag_ports; temp_port;
2115 temp_port = temp_port->next_port_in_aggregator) {
2116 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2118 }
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00002121
Bandan Das7bfc4752010-10-16 20:19:59 +00002122 if (select_new_active_agg)
Mahesh Bandewaree637712014-10-04 17:45:01 -07002123 ad_agg_selection_logic(__get_first_agg(port),
2124 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002126 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002128 } else {
2129 /* in case that the only port related to this
2130 * aggregator is the one we want to remove
2131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 select_new_active_agg = aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 ad_clear_agg(aggregator);
2134 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002135 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002136 /* select new active aggregator */
Veaceslav Falico74684492013-09-27 15:10:58 +02002137 temp_aggregator = __get_first_agg(port);
2138 if (temp_aggregator)
Mahesh Bandewaree637712014-10-04 17:45:01 -07002139 ad_agg_selection_logic(temp_aggregator,
2140 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142 }
2143 }
2144
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002145 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002146
2147 /* find the aggregator that this port is connected to */
Veaceslav Falico0b0882642013-09-27 16:12:02 +02002148 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002149 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 prev_port = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002151 /* search the port in the aggregator's related ports */
Bandan Das128ea6c2010-10-16 20:19:58 +00002152 for (temp_port = temp_aggregator->lag_ports; temp_port;
2153 prev_port = temp_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002154 temp_port = temp_port->next_port_in_aggregator) {
2155 if (temp_port == port) {
2156 /* the aggregator found - detach the port from
2157 * this aggregator
2158 */
Bandan Das7bfc4752010-10-16 20:19:59 +00002159 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00002161 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 temp_aggregator->num_of_ports--;
Jay Vosburgh0622cab2016-06-23 14:20:51 -07002164 if (__agg_active_ports(temp_aggregator) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 select_new_active_agg = temp_aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 ad_clear_agg(temp_aggregator);
2167 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002168 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002169 /* select new active aggregator */
Mahesh Bandewaree637712014-10-04 17:45:01 -07002170 ad_agg_selection_logic(__get_first_agg(port),
2171 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173 }
2174 break;
2175 }
2176 }
2177 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002178 port->slave = NULL;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002179
2180out:
2181 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184/**
Nikolay Aleksandrov5ee14e62016-02-03 13:17:01 +01002185 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2186 * @bond: bonding struct to work on
2187 *
2188 * If an ad_actor setting gets changed we need to update the individual port
2189 * settings so the bond device will use the new values when it gets upped.
2190 */
2191void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2192{
2193 struct list_head *iter;
2194 struct slave *slave;
2195
2196 ASSERT_RTNL();
2197
2198 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2199 if (is_zero_ether_addr(bond->params.ad_actor_system))
2200 BOND_AD_INFO(bond).system.sys_mac_addr =
2201 *((struct mac_addr *)bond->dev->dev_addr);
2202 else
2203 BOND_AD_INFO(bond).system.sys_mac_addr =
2204 *((struct mac_addr *)bond->params.ad_actor_system);
2205
2206 spin_lock_bh(&bond->mode_lock);
Nikolay Aleksandrov7f20cd22016-02-04 17:42:28 +01002207 bond_for_each_slave(bond, slave, iter) {
2208 struct port *port = &(SLAVE_AD_INFO(slave))->port;
2209
2210 __ad_actor_update_port(port);
2211 port->ntt = true;
2212 }
Nikolay Aleksandrov5ee14e62016-02-03 13:17:01 +01002213 spin_unlock_bh(&bond->mode_lock);
2214}
2215
2216/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * bond_3ad_state_machine_handler - handle state machines timeout
2218 * @bond: bonding struct to work on
2219 *
2220 * The state machine handling concept in this module is to check every tick
2221 * which state machine should operate any function. The execution order is
2222 * round robin, so when we have an interaction between state machines, the
2223 * reply of one to each other might be delayed until next tick.
2224 *
2225 * This function also complete the initialization when the agg_select_timer
2226 * times out, and it selects an aggregator for the ports that are yet not
2227 * related to any aggregator, and selects the active aggregator for a bond.
2228 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002229void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002231 struct bonding *bond = container_of(work, struct bonding,
2232 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002234 struct list_head *iter;
2235 struct slave *slave;
2236 struct port *port;
dingtianhong5e5b0662014-02-26 11:05:22 +08002237 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Mahesh Bandewaree637712014-10-04 17:45:01 -07002238 bool update_slave_arr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002240 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2241 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2242 * concurrently due to incoming LACPDU as well.
2243 */
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002244 spin_lock_bh(&bond->mode_lock);
dingtianhongbe79bd02013-12-13 10:20:12 +08002245 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04002246
dingtianhongbe79bd02013-12-13 10:20:12 +08002247 /* check if there are any slaves */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002248 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
dingtianhongbe79bd02013-12-13 10:20:12 +08002251 /* check if agg_select_timer timer after initialize is timed out */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002252 if (BOND_AD_INFO(bond).agg_select_timer &&
2253 !(--BOND_AD_INFO(bond).agg_select_timer)) {
dingtianhongbe79bd02013-12-13 10:20:12 +08002254 slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +08002255 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002256
dingtianhongbe79bd02013-12-13 10:20:12 +08002257 /* select the active aggregator for the bond */
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002258 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002260 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2261 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto re_arm;
2263 }
2264
2265 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002266 ad_agg_selection_logic(aggregator, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002268 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270
dingtianhongbe79bd02013-12-13 10:20:12 +08002271 /* for each port run the state machines */
2272 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002273 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002275 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
Veaceslav Falico86a2b9c2014-03-16 17:55:03 +01002276 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 goto re_arm;
2278 }
2279
2280 ad_rx_machine(NULL, port);
2281 ad_periodic_machine(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002282 ad_port_selection_logic(port, &update_slave_arr);
2283 ad_mux_machine(port, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 ad_tx_machine(port);
Mahesh Bandewar14c95512015-02-23 17:50:11 -08002285 ad_churn_machine(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
dingtianhongbe79bd02013-12-13 10:20:12 +08002287 /* turn off the BEGIN bit, since we already handled it */
Bandan Das7bfc4752010-10-16 20:19:59 +00002288 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 port->sm_vars &= ~AD_PORT_BEGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291
2292re_arm:
dingtianhong5e5b0662014-02-26 11:05:22 +08002293 bond_for_each_slave_rcu(bond, slave, iter) {
2294 if (slave->should_notify) {
2295 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2296 break;
2297 }
2298 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002299 rcu_read_unlock();
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002300 spin_unlock_bh(&bond->mode_lock);
dingtianhong5e5b0662014-02-26 11:05:22 +08002301
Mahesh Bandewaree637712014-10-04 17:45:01 -07002302 if (update_slave_arr)
2303 bond_slave_arr_work_rearm(bond, 0);
2304
dingtianhong5e5b0662014-02-26 11:05:22 +08002305 if (should_notify_rtnl && rtnl_trylock()) {
dingtianhongb0929912014-02-26 11:05:23 +08002306 bond_slave_state_notify(bond);
dingtianhong5e5b0662014-02-26 11:05:22 +08002307 rtnl_unlock();
2308 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002309 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312/**
2313 * bond_3ad_rx_indication - handle a received frame
2314 * @lacpdu: received lacpdu
2315 * @slave: slave struct to work on
2316 * @length: length of the data received
2317 *
2318 * It is assumed that frames that were sent on this NIC don't returned as new
2319 * received frames (loopback). Since only the payload is given to this
2320 * function, it check for loopback.
2321 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002322static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2323 u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002326 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (length >= sizeof(struct lacpdu)) {
2329
dingtianhong3fdddd82014-05-12 15:08:43 +08002330 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002333 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2334 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
2338 switch (lacpdu->subtype) {
2339 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002340 ret = RX_HANDLER_CONSUMED;
Satish Ashok2f637322015-01-26 01:17:00 -05002341 netdev_dbg(slave->bond->dev,
2342 "Received LACPDU on port %d slave %s\n",
2343 port->actor_port_number,
2344 slave->dev->name);
Nils Carlson16d79d72011-03-03 22:09:11 +00002345 /* Protect against concurrent state machines */
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002346 spin_lock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 ad_rx_machine(lacpdu, port);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002348 spin_unlock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 break;
2350
2351 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002352 ret = RX_HANDLER_CONSUMED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002353 /* No need to convert fields to Little Endian since we
2354 * don't use the marker's fields.
2355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002357 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 case AD_MARKER_INFORMATION_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002359 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2360 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002361 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
2363
2364 case AD_MARKER_RESPONSE_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002365 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2366 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002367 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 break;
2369
2370 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002371 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2372 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 }
2375 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379/**
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002380 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2381 * its current speed and duplex settings.
2382 *
2383 * @port: the port we'are looking at
2384 * @reset: Boolean to just reset the speed and the duplex part of the key
2385 *
2386 * The logic to change the oper / admin keys is:
2387 * (a) A full duplex port can participate in LACP with partner.
2388 * (b) When the speed is changed, LACP need to be reinitiated.
2389 */
2390static void ad_update_actor_keys(struct port *port, bool reset)
2391{
2392 u8 duplex = 0;
2393 u16 ospeed = 0, speed = 0;
2394 u16 old_oper_key = port->actor_oper_port_key;
2395
2396 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2397 if (!reset) {
2398 speed = __get_link_speed(port);
2399 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2400 duplex = __get_duplex(port);
2401 port->actor_admin_port_key |= (speed << 1) | duplex;
2402 }
2403 port->actor_oper_port_key = port->actor_admin_port_key;
2404
2405 if (old_oper_key != port->actor_oper_port_key) {
2406 /* Only 'duplex' port participates in LACP */
2407 if (duplex)
2408 port->sm_vars |= AD_PORT_LACP_ENABLED;
2409 else
2410 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2411
2412 if (!reset) {
2413 if (!speed) {
2414 netdev_err(port->slave->dev,
2415 "speed changed to 0 for port %s",
2416 port->slave->dev->name);
2417 } else if (duplex && ospeed != speed) {
2418 /* Speed change restarts LACP state-machine */
2419 port->sm_vars |= AD_PORT_BEGIN;
2420 }
2421 }
2422 }
2423}
2424
2425/**
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002426 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2427 * change indication
2428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 * @slave: slave struct to work on
2430 *
2431 * Handle reselection of aggregator (if needed) for this port.
2432 */
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002433void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 struct port *port;
2436
dingtianhong3fdddd82014-05-12 15:08:43 +08002437 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
dingtianhong71a06c52013-12-13 17:29:19 +08002439 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!port->slave) {
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002441 netdev_warn(slave->bond->dev,
2442 "speed/duplex changed for uninitialized port %s\n",
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002443 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return;
2445 }
2446
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002447 spin_lock_bh(&slave->bond->mode_lock);
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002448 ad_update_actor_keys(port, false);
Mahesh Bandeware292dca2017-03-27 11:37:40 -07002449 spin_unlock_bh(&slave->bond->mode_lock);
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07002450 netdev_dbg(slave->bond->dev, "Port %d slave %s changed speed/duplex\n",
Satish Ashok2f637322015-01-26 01:17:00 -05002451 port->actor_port_number, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
2454/**
2455 * bond_3ad_handle_link_change - handle a slave's link status change indication
2456 * @slave: slave struct to work on
2457 * @status: whether the link is now up or down
2458 *
2459 * Handle reselection of aggregator (if needed) for this port.
2460 */
2461void bond_3ad_handle_link_change(struct slave *slave, char link)
2462{
Jay Vosburgh0622cab2016-06-23 14:20:51 -07002463 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct port *port;
Jay Vosburgh0622cab2016-06-23 14:20:51 -07002465 bool dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
dingtianhong3fdddd82014-05-12 15:08:43 +08002467 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
dingtianhong108db732013-12-13 17:29:29 +08002469 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002471 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2472 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return;
2474 }
2475
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002476 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong108db732013-12-13 17:29:29 +08002477 /* on link down we are zeroing duplex and speed since
2478 * some of the adaptors(ce1000.lan) report full duplex/speed
2479 * instead of N/A(duplex) / 0(speed).
2480 *
2481 * on link up we are forcing recheck on the duplex and speed since
2482 * some of he adaptors(ce1000.lan) report.
2483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002485 port->is_enabled = true;
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002486 ad_update_actor_keys(port, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 } else {
2488 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002489 port->is_enabled = false;
Mahesh Bandewar7bb11dc2015-10-31 12:45:06 -07002490 ad_update_actor_keys(port, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Jay Vosburgh0622cab2016-06-23 14:20:51 -07002492 agg = __get_first_agg(port);
2493 ad_agg_selection_logic(agg, &dummy);
2494
Mahesh Bandeware292dca2017-03-27 11:37:40 -07002495 spin_unlock_bh(&slave->bond->mode_lock);
2496
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002497 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2498 port->actor_port_number,
2499 link == BOND_LINK_UP ? "UP" : "DOWN");
dingtianhong108db732013-12-13 17:29:29 +08002500
Mahesh Bandewaree637712014-10-04 17:45:01 -07002501 /* RTNL is held and mode_lock is released so it's safe
2502 * to update slave_array here.
2503 */
2504 bond_update_slave_arr(slave->bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002507/**
2508 * bond_3ad_set_carrier - set link state for bonding master
2509 * @bond - bonding structure
2510 *
2511 * if we have an active aggregator, we're up, if not, we're down.
2512 * Presumes that we cannot have an active aggregator if there are
2513 * no slaves with link up.
Jay Vosburghff59c452006-03-27 13:27:43 -08002514 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002515 * This behavior complies with IEEE 802.3 section 43.3.9.
2516 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002517 * Called by bond_set_carrier(). Return zero if carrier state does not
2518 * change, nonzero if it does.
2519 */
2520int bond_3ad_set_carrier(struct bonding *bond)
2521{
stephen hemminger655f8912011-06-22 09:54:39 +00002522 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002523 struct slave *first_slave;
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002524 int ret = 1;
stephen hemminger655f8912011-06-22 09:54:39 +00002525
dingtianhongbe79bd02013-12-13 10:20:12 +08002526 rcu_read_lock();
2527 first_slave = bond_first_slave_rcu(bond);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002528 if (!first_slave) {
2529 ret = 0;
2530 goto out;
2531 }
dingtianhong3fdddd82014-05-12 15:08:43 +08002532 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002533 if (active) {
2534 /* are enough slaves available to consider link up? */
Jay Vosburgh0622cab2016-06-23 14:20:51 -07002535 if (__agg_active_ports(active) < bond->params.min_links) {
stephen hemminger655f8912011-06-22 09:54:39 +00002536 if (netif_carrier_ok(bond->dev)) {
2537 netif_carrier_off(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002538 goto out;
stephen hemminger655f8912011-06-22 09:54:39 +00002539 }
2540 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002541 netif_carrier_on(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002542 goto out;
Jay Vosburghff59c452006-03-27 13:27:43 -08002543 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002544 } else if (netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002545 netif_carrier_off(bond->dev);
Jay Vosburghff59c452006-03-27 13:27:43 -08002546 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002547out:
2548 rcu_read_unlock();
2549 return ret;
Jay Vosburghff59c452006-03-27 13:27:43 -08002550}
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002553 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 * @bond: bonding struct to work on
2555 * @ad_info: ad_info struct to fill with the bond's info
2556 *
2557 * Returns: 0 on success
2558 * < 0 on error
2559 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002560int __bond_3ad_get_active_agg_info(struct bonding *bond,
2561 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002564 struct list_head *iter;
2565 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 struct port *port;
2567
dingtianhong47e91f562013-10-15 16:28:35 +08002568 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002569 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (port->aggregator && port->aggregator->is_active) {
2571 aggregator = port->aggregator;
2572 break;
2573 }
2574 }
2575
Joe Perches21f374c2014-02-18 09:42:47 -08002576 if (!aggregator)
2577 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Joe Perches21f374c2014-02-18 09:42:47 -08002579 ad_info->aggregator_id = aggregator->aggregator_identifier;
2580 ad_info->ports = aggregator->num_of_ports;
2581 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2582 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2583 ether_addr_copy(ad_info->partner_system,
2584 aggregator->partner_system.mac_addr_value);
2585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002588int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2589{
2590 int ret;
2591
dingtianhong47e91f562013-10-15 16:28:35 +08002592 rcu_read_lock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002593 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
dingtianhong47e91f562013-10-15 16:28:35 +08002594 rcu_read_unlock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002595
2596 return ret;
2597}
2598
Eric Dumazetde063b72012-06-11 19:23:07 +00002599int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2600 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Eric Dumazetde063b72012-06-11 19:23:07 +00002602 struct lacpdu *lacpdu, _lacpdu;
2603
Jiri Pirko3aba8912011-04-19 03:48:16 +00002604 if (skb->protocol != PKT_TYPE_LACPDU)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002605 return RX_HANDLER_ANOTHER;
Neil Hormanb3053252011-01-20 09:02:31 +00002606
Mahesh Bandewarbb54e582015-02-23 17:50:10 -08002607 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2608 return RX_HANDLER_ANOTHER;
2609
Eric Dumazetde063b72012-06-11 19:23:07 +00002610 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2611 if (!lacpdu)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002612 return RX_HANDLER_ANOTHER;
Andy Gospodarekab128112010-09-10 11:43:20 +00002613
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002614 return bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002616
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002617/**
2618 * bond_3ad_update_lacp_rate - change the lacp rate
2619 * @bond - bonding struct
2620 *
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002621 * When modify lacp_rate parameter via sysfs,
2622 * update actor_oper_port_state of each port.
2623 *
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002624 * Hold bond->mode_lock,
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002625 * so we can modify port->actor_oper_port_state,
2626 * no matter bond is up or down.
2627 */
2628void bond_3ad_update_lacp_rate(struct bonding *bond)
2629{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002630 struct port *port = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002631 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002632 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002633 int lacp_fast;
2634
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002635 lacp_fast = bond->params.lacp_fast;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002636 spin_lock_bh(&bond->mode_lock);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002637 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002638 port = &(SLAVE_AD_INFO(slave)->port);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002639 if (lacp_fast)
2640 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2641 else
2642 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002643 }
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002644 spin_unlock_bh(&bond->mode_lock);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002645}