blob: 8baa87df173841ea90e07895dbb0620d4abb0a8d [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
73
Veaceslav Falico3bf2d282014-01-08 16:46:46 +010074/* Port Key definitions
75 * key is determined according to the link speed, duplex and
76 * user key (which is yet not supported)
77 * --------------------------------------------------------------
78 * Port key : | User key | Speed | Duplex |
79 * --------------------------------------------------------------
80 * 16 6 1 0
81 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +080082#define AD_DUPLEX_KEY_MASKS 0x1
83#define AD_SPEED_KEY_MASKS 0x3E
84#define AD_USER_KEY_MASKS 0xFFC0
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jianhua Xiecb8dda92014-11-19 16:48:58 +080086enum ad_link_speed_type {
87 AD_LINK_SPEED_1MBPS = 1,
88 AD_LINK_SPEED_10MBPS,
89 AD_LINK_SPEED_100MBPS,
90 AD_LINK_SPEED_1000MBPS,
Jianhua Xie424c3232014-11-19 16:48:59 +080091 AD_LINK_SPEED_2500MBPS,
92 AD_LINK_SPEED_10000MBPS,
93 AD_LINK_SPEED_20000MBPS,
94 AD_LINK_SPEED_40000MBPS,
95 AD_LINK_SPEED_56000MBPS
Jianhua Xiecb8dda92014-11-19 16:48:58 +080096};
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
dingtianhong815117a2014-01-02 09:12:54 +080098/* compare MAC addresses */
99#define MAC_ADDRESS_EQUAL(A, B) \
100 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Bandan Das128ea6c2010-10-16 20:19:58 +0000102static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static u16 ad_ticks_per_sec;
104static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
105
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800106static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
107
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100108/* ================= main 802.3ad protocol functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int ad_lacpdu_send(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700110static int ad_marker_send(struct port *port, struct bond_marker *marker);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700111static void ad_mux_machine(struct port *port, bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
113static void ad_tx_machine(struct port *port);
114static void ad_periodic_machine(struct port *port);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700115static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
116static void ad_agg_selection_logic(struct aggregator *aggregator,
117 bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void ad_clear_agg(struct aggregator *aggregator);
119static void ad_initialize_agg(struct aggregator *aggregator);
120static void ad_initialize_port(struct port *port, int lacp_fast);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700121static void ad_enable_collecting_distributing(struct port *port,
122 bool *update_slave_arr);
123static void ad_disable_collecting_distributing(struct port *port,
124 bool *update_slave_arr);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100125static void ad_marker_info_received(struct bond_marker *marker_info,
126 struct port *port);
127static void ad_marker_response_received(struct bond_marker *marker,
128 struct port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100131/* ================= api to bonding and kernel code ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/**
134 * __get_bond_by_port - get the port's bonding struct
135 * @port: the port we're looking at
136 *
137 * Return @port's bonding struct, or %NULL if it can't be found.
138 */
139static inline struct bonding *__get_bond_by_port(struct port *port)
140{
Bandan Das7bfc4752010-10-16 20:19:59 +0000141 if (port->slave == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return bond_get_bond_by_slave(port->slave);
145}
146
147/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * __get_first_agg - get the first aggregator in the bond
149 * @bond: the bond we're looking at
150 *
151 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
152 * found.
Veaceslav Falico768b9542014-01-10 11:59:44 +0100153 * The caller must hold RCU or RTNL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
155static inline struct aggregator *__get_first_agg(struct port *port)
156{
157 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200158 struct slave *first_slave;
Veaceslav Falico768b9542014-01-10 11:59:44 +0100159 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
dingtianhongbe79bd02013-12-13 10:20:12 +0800161 /* If there's no bond for this port, or bond has no slaves */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200162 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100164
dingtianhongbe79bd02013-12-13 10:20:12 +0800165 rcu_read_lock();
166 first_slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +0800167 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
dingtianhongbe79bd02013-12-13 10:20:12 +0800168 rcu_read_unlock();
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100169
Veaceslav Falico768b9542014-01-10 11:59:44 +0100170 return agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100173/**
174 * __agg_has_partner - see if we have a partner
175 * @agg: the agregator we're looking at
Jay Vosburghfd989c82008-11-04 17:51:16 -0800176 *
177 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100178 * address for the partner). Return 0 if not.
Jay Vosburghfd989c82008-11-04 17:51:16 -0800179 */
180static inline int __agg_has_partner(struct aggregator *agg)
181{
182 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/**
186 * __disable_port - disable the port's slave
187 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189static inline void __disable_port(struct port *port)
190{
dingtianhong5e5b0662014-02-26 11:05:22 +0800191 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/**
195 * __enable_port - enable the port's slave, if it's up
196 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198static inline void __enable_port(struct port *port)
199{
200 struct slave *slave = port->slave;
201
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200202 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
dingtianhong5e5b0662014-02-26 11:05:22 +0800203 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/**
207 * __port_is_enabled - check if the port's slave is in active state
208 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210static inline int __port_is_enabled(struct port *port)
211{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000212 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215/**
216 * __get_agg_selection_mode - get the aggregator selection mode
217 * @port: the port we're looking at
218 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800219 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221static inline u32 __get_agg_selection_mode(struct port *port)
222{
223 struct bonding *bond = __get_bond_by_port(port);
224
Bandan Das7bfc4752010-10-16 20:19:59 +0000225 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800226 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000228 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/**
232 * __check_agg_selection_timer - check if the selection timer has expired
233 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235static inline int __check_agg_selection_timer(struct port *port)
236{
237 struct bonding *bond = __get_bond_by_port(port);
238
Bandan Das7bfc4752010-10-16 20:19:59 +0000239 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
243}
244
245/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * __get_link_speed - get a port's speed
247 * @port: the port we're looking at
248 *
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800249 * Return @port's speed in 802.3ad enum format. i.e. one of:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * 0,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800251 * %AD_LINK_SPEED_10MBPS,
252 * %AD_LINK_SPEED_100MBPS,
253 * %AD_LINK_SPEED_1000MBPS,
Jianhua Xie424c3232014-11-19 16:48:59 +0800254 * %AD_LINK_SPEED_2500MBPS,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800255 * %AD_LINK_SPEED_10000MBPS
Jianhua Xie424c3232014-11-19 16:48:59 +0800256 * %AD_LINK_SPEED_20000MBPS
257 * %AD_LINK_SPEED_40000MBPS
258 * %AD_LINK_SPEED_56000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
260static u16 __get_link_speed(struct port *port)
261{
262 struct slave *slave = port->slave;
263 u16 speed;
264
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100265 /* this if covers only a special case: when the configuration starts
266 * with link down, it sets the speed to 0.
267 * This is done in spite of the fact that the e100 driver reports 0
268 * to be compatible with MVT in the future.
269 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000270 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000271 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000272 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 switch (slave->speed) {
274 case SPEED_10:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800275 speed = AD_LINK_SPEED_10MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
278 case SPEED_100:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800279 speed = AD_LINK_SPEED_100MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281
282 case SPEED_1000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800283 speed = AD_LINK_SPEED_1000MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285
Jianhua Xie424c3232014-11-19 16:48:59 +0800286 case SPEED_2500:
287 speed = AD_LINK_SPEED_2500MBPS;
288 break;
289
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700290 case SPEED_10000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800291 speed = AD_LINK_SPEED_10000MBPS;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700292 break;
293
Jianhua Xie424c3232014-11-19 16:48:59 +0800294 case SPEED_20000:
295 speed = AD_LINK_SPEED_20000MBPS;
296 break;
297
298 case SPEED_40000:
299 speed = AD_LINK_SPEED_40000MBPS;
300 break;
301
302 case SPEED_56000:
303 speed = AD_LINK_SPEED_56000MBPS;
304 break;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100307 /* unknown speed value from ethtool. shouldn't happen */
308 speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 }
311 }
312
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200313 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
314 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return speed;
316}
317
318/**
319 * __get_duplex - get a port's duplex
320 * @port: the port we're looking at
321 *
322 * Return @port's duplex in 802.3ad bitmask format. i.e.:
323 * 0x01 if in full duplex
324 * 0x00 otherwise
325 */
326static u8 __get_duplex(struct port *port)
327{
328 struct slave *slave = port->slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 u8 retval;
330
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100331 /* handling a special case: when the configuration starts with
332 * link down, it sets the duplex to 0.
333 */
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200334 if (slave->link != BOND_LINK_UP) {
Bandan Das128ea6c2010-10-16 20:19:58 +0000335 retval = 0x0;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200336 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 switch (slave->duplex) {
338 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000339 retval = 0x1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200340 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
341 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343 case DUPLEX_HALF:
344 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000345 retval = 0x0;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200346 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
347 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 }
350 }
351 return retval;
352}
353
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100354/* Conversions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356/**
357 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
358 * @timer_type: which timer to operate
359 * @par: timer parameter. see below
360 *
361 * If @timer_type is %current_while_timer, @par indicates long/short timer.
362 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100363 * %SLOW_PERIODIC_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
366{
Bandan Das128ea6c2010-10-16 20:19:58 +0000367 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 switch (timer_type) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100370 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
Bandan Das7bfc4752010-10-16 20:19:59 +0000371 if (par)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100372 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
Bandan Das7bfc4752010-10-16 20:19:59 +0000373 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100374 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100376 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
378 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100379 case AD_PERIODIC_TIMER: /* for periodic machine */
380 retval = (par*ad_ticks_per_sec); /* long timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100382 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
384 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100385 case AD_WAIT_WHILE_TIMER: /* for selection machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
387 break;
388 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return retval;
391}
392
393
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100394/* ================= ad_rx_machine helper functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000397 * __choose_matched - update a port's matched variable from a received lacpdu
398 * @lacpdu: the lacpdu we've received
399 * @port: the port we're looking at
400 *
401 * Update the value of the matched variable, using parameter values from a
402 * newly received lacpdu. Parameter values for the partner carried in the
403 * received PDU are compared with the corresponding operational parameter
404 * values for the actor. Matched is set to TRUE if all of these parameters
405 * match and the PDU parameter partner_state.aggregation has the same value as
406 * actor_oper_port_state.aggregation and lacp will actively maintain the link
407 * in the aggregation. Matched is also set to TRUE if the value of
408 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
409 * an individual link and lacp will actively maintain the link. Otherwise,
410 * matched is set to FALSE. LACP is considered to be actively maintaining the
411 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
412 * the actor's actor_oper_port_state.lacp_activity and the PDU's
413 * partner_state.lacp_activity variables are TRUE.
414 *
415 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
416 * used here to implement the language from 802.3ad 43.4.9 that requires
417 * recordPDU to "match" the LACPDU parameters to the stored values.
418 */
419static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
420{
dingtianhong815117a2014-01-02 09:12:54 +0800421 /* check if all parameters are alike
422 * or this is individual link(aggregation == FALSE)
423 * then update the state machine Matched variable.
424 */
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000425 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
426 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
dingtianhong815117a2014-01-02 09:12:54 +0800427 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000428 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
429 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
430 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000431 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
432 ) {
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000433 port->sm_vars |= AD_PORT_MATCHED;
434 } else {
435 port->sm_vars &= ~AD_PORT_MATCHED;
436 }
437}
438
439/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * __record_pdu - record parameters from a received lacpdu
441 * @lacpdu: the lacpdu we've received
442 * @port: the port we're looking at
443 *
444 * Record the parameter values for the Actor carried in a received lacpdu as
445 * the current partner operational parameter values and sets
446 * actor_oper_port_state.defaulted to FALSE.
447 */
448static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
449{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800451 struct port_params *partner = &port->partner_oper;
452
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000453 __choose_matched(lacpdu, port);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100454 /* record the new parameter values for the partner
455 * operational
456 */
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800457 partner->port_number = ntohs(lacpdu->actor_port);
458 partner->port_priority = ntohs(lacpdu->actor_port_priority);
459 partner->system = lacpdu->actor_system;
460 partner->system_priority = ntohs(lacpdu->actor_system_priority);
461 partner->key = ntohs(lacpdu->actor_key);
462 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100464 /* set actor_oper_port_state.defaulted to FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
466
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100467 /* set the partner sync. to on if the partner is sync,
468 * and the port is matched
469 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000470 if ((port->sm_vars & AD_PORT_MATCHED)
471 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800472 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Bandan Das7bfc4752010-10-16 20:19:59 +0000473 else
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800474 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476}
477
478/**
479 * __record_default - record default parameters
480 * @port: the port we're looking at
481 *
482 * This function records the default parameter values for the partner carried
483 * in the Partner Admin parameters as the current partner operational parameter
484 * values and sets actor_oper_port_state.defaulted to TRUE.
485 */
486static void __record_default(struct port *port)
487{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (port) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100489 /* record the partner admin parameters */
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800490 memcpy(&port->partner_oper, &port->partner_admin,
491 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100493 /* set actor_oper_port_state.defaulted to true */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
495 }
496}
497
498/**
499 * __update_selected - update a port's Selected variable from a received lacpdu
500 * @lacpdu: the lacpdu we've received
501 * @port: the port we're looking at
502 *
503 * Update the value of the selected variable, using parameter values from a
504 * newly received lacpdu. The parameter values for the Actor carried in the
505 * received PDU are compared with the corresponding operational parameter
506 * values for the ports partner. If one or more of the comparisons shows that
507 * the value(s) received in the PDU differ from the current operational values,
508 * then selected is set to FALSE and actor_oper_port_state.synchronization is
509 * set to out_of_sync. Otherwise, selected remains unchanged.
510 */
511static void __update_selected(struct lacpdu *lacpdu, struct port *port)
512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800514 const struct port_params *partner = &port->partner_oper;
515
dingtianhong815117a2014-01-02 09:12:54 +0800516 /* check if any parameter is different then
517 * update the state machine selected variable.
518 */
Joe Perches8e95a202009-12-03 07:58:21 +0000519 if (ntohs(lacpdu->actor_port) != partner->port_number ||
520 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800521 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000522 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
523 ntohs(lacpdu->actor_key) != partner->key ||
524 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 port->sm_vars &= ~AD_PORT_SELECTED;
526 }
527 }
528}
529
530/**
531 * __update_default_selected - update a port's Selected variable from Partner
532 * @port: the port we're looking at
533 *
534 * This function updates the value of the selected variable, using the partner
535 * administrative parameter values. The administrative values are compared with
536 * the corresponding operational parameter values for the partner. If one or
537 * more of the comparisons shows that the administrative value(s) differ from
538 * the current operational values, then Selected is set to FALSE and
539 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
540 * Selected remains unchanged.
541 */
542static void __update_default_selected(struct port *port)
543{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800545 const struct port_params *admin = &port->partner_admin;
546 const struct port_params *oper = &port->partner_oper;
547
dingtianhong815117a2014-01-02 09:12:54 +0800548 /* check if any parameter is different then
549 * update the state machine selected variable.
550 */
Joe Perches8e95a202009-12-03 07:58:21 +0000551 if (admin->port_number != oper->port_number ||
552 admin->port_priority != oper->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800553 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000554 admin->system_priority != oper->system_priority ||
555 admin->key != oper->key ||
556 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800557 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 port->sm_vars &= ~AD_PORT_SELECTED;
559 }
560 }
561}
562
563/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * __update_ntt - update a port's ntt variable from a received lacpdu
565 * @lacpdu: the lacpdu we've received
566 * @port: the port we're looking at
567 *
568 * Updates the value of the ntt variable, using parameter values from a newly
569 * received lacpdu. The parameter values for the partner carried in the
570 * received PDU are compared with the corresponding operational parameter
571 * values for the Actor. If one or more of the comparisons shows that the
572 * value(s) received in the PDU differ from the current operational values,
573 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
574 */
575static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
576{
dingtianhong815117a2014-01-02 09:12:54 +0800577 /* validate lacpdu and port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (lacpdu && port) {
dingtianhong815117a2014-01-02 09:12:54 +0800579 /* check if any parameter is different then
580 * update the port->ntt.
581 */
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700582 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
583 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
dingtianhong815117a2014-01-02 09:12:54 +0800584 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700585 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
586 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
588 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
589 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
590 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
591 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800592 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 }
595}
596
597/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * __agg_ports_are_ready - check if all ports in an aggregator are ready
599 * @aggregator: the aggregator we're looking at
600 *
601 */
602static int __agg_ports_are_ready(struct aggregator *aggregator)
603{
604 struct port *port;
605 int retval = 1;
606
607 if (aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100608 /* scan all ports in this aggregator to verfy if they are
609 * all ready.
610 */
Bandan Das128ea6c2010-10-16 20:19:58 +0000611 for (port = aggregator->lag_ports;
612 port;
613 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!(port->sm_vars & AD_PORT_READY_N)) {
615 retval = 0;
616 break;
617 }
618 }
619 }
620
621 return retval;
622}
623
624/**
625 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
626 * @aggregator: the aggregator we're looking at
627 * @val: Should the ports' ready bit be set on or off
628 *
629 */
630static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
631{
632 struct port *port;
633
Bandan Das128ea6c2010-10-16 20:19:58 +0000634 for (port = aggregator->lag_ports; port;
635 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000636 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000638 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641}
642
643/**
644 * __get_agg_bandwidth - get the total bandwidth of an aggregator
645 * @aggregator: the aggregator we're looking at
646 *
647 */
648static u32 __get_agg_bandwidth(struct aggregator *aggregator)
649{
Bandan Das128ea6c2010-10-16 20:19:58 +0000650 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (aggregator->num_of_ports) {
David Decotigny65cce192011-04-13 15:22:30 +0000653 switch (__get_link_speed(aggregator->lag_ports)) {
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800654 case AD_LINK_SPEED_1MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 bandwidth = aggregator->num_of_ports;
656 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800657 case AD_LINK_SPEED_10MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 bandwidth = aggregator->num_of_ports * 10;
659 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800660 case AD_LINK_SPEED_100MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 bandwidth = aggregator->num_of_ports * 100;
662 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800663 case AD_LINK_SPEED_1000MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 bandwidth = aggregator->num_of_ports * 1000;
665 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800666 case AD_LINK_SPEED_2500MBPS:
667 bandwidth = aggregator->num_of_ports * 2500;
668 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800669 case AD_LINK_SPEED_10000MBPS:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700670 bandwidth = aggregator->num_of_ports * 10000;
671 break;
Jianhua Xie424c3232014-11-19 16:48:59 +0800672 case AD_LINK_SPEED_20000MBPS:
673 bandwidth = aggregator->num_of_ports * 20000;
674 break;
675 case AD_LINK_SPEED_40000MBPS:
676 bandwidth = aggregator->num_of_ports * 40000;
677 break;
678 case AD_LINK_SPEED_56000MBPS:
679 bandwidth = aggregator->num_of_ports * 56000;
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100682 bandwidth = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 }
685 return bandwidth;
686}
687
688/**
689 * __get_active_agg - get the current active aggregator
690 * @aggregator: the aggregator we're looking at
Veaceslav Falico49b76242014-01-10 11:59:45 +0100691 *
692 * Caller must hold RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
694static struct aggregator *__get_active_agg(struct aggregator *aggregator)
695{
Veaceslav Falico19177e72013-09-27 16:12:00 +0200696 struct bonding *bond = aggregator->slave->bond;
697 struct list_head *iter;
698 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
dingtianhongbe79bd02013-12-13 10:20:12 +0800700 bond_for_each_slave_rcu(bond, slave, iter)
dingtianhong3fdddd82014-05-12 15:08:43 +0800701 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
702 return &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Veaceslav Falico19177e72013-09-27 16:12:00 +0200704 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/**
708 * __update_lacpdu_from_port - update a port's lacpdu fields
709 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
711static inline void __update_lacpdu_from_port(struct port *port)
712{
713 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800714 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100716 /* update current actual Actor parameters
717 * lacpdu->subtype initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * lacpdu->version_number initialized
719 * lacpdu->tlv_type_actor_info initialized
720 * lacpdu->actor_information_length initialized
721 */
722
Al Virod3bb52b2007-08-22 20:06:58 -0400723 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400725 lacpdu->actor_key = htons(port->actor_oper_port_key);
726 lacpdu->actor_port_priority = htons(port->actor_port_priority);
727 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 lacpdu->actor_state = port->actor_oper_port_state;
729
730 /* lacpdu->reserved_3_1 initialized
731 * lacpdu->tlv_type_partner_info initialized
732 * lacpdu->partner_information_length initialized
733 */
734
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800735 lacpdu->partner_system_priority = htons(partner->system_priority);
736 lacpdu->partner_system = partner->system;
737 lacpdu->partner_key = htons(partner->key);
738 lacpdu->partner_port_priority = htons(partner->port_priority);
739 lacpdu->partner_port = htons(partner->port_number);
740 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /* lacpdu->reserved_3_2 initialized
743 * lacpdu->tlv_type_collector_info initialized
744 * lacpdu->collector_information_length initialized
745 * collector_max_delay initialized
746 * reserved_12[12] initialized
747 * tlv_type_terminator initialized
748 * terminator_length initialized
749 * reserved_50[50] initialized
750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100753/* ================= main 802.3ad protocol code ========================= */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755/**
756 * ad_lacpdu_send - send out a lacpdu packet on a given port
757 * @port: the port we're looking at
758 *
759 * Returns: 0 on success
760 * < 0 on error
761 */
762static int ad_lacpdu_send(struct port *port)
763{
764 struct slave *slave = port->slave;
765 struct sk_buff *skb;
766 struct lacpdu_header *lacpdu_header;
767 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000770 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700774 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700775 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 skb->protocol = PKT_TYPE_LACPDU;
777 skb->priority = TC_PRIO_CONTROL;
778
779 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
780
Joe Perchesada0f862014-02-15 16:02:17 -0800781 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400782 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100783 * because we use it to identify loopback lacpdus in receive.
784 */
Joe Perchesada0f862014-02-15 16:02:17 -0800785 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800786 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100788 lacpdu_header->lacpdu = port->lacpdu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 dev_queue_xmit(skb);
791
792 return 0;
793}
794
795/**
796 * ad_marker_send - send marker information/response on a given port
797 * @port: the port we're looking at
798 * @marker: marker data to send
799 *
800 * Returns: 0 on success
801 * < 0 on error
802 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700803static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct slave *slave = port->slave;
806 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700807 struct bond_marker_header *marker_header;
808 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000811 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 skb_reserve(skb, 16);
815
816 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700817 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700818 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 skb->protocol = PKT_TYPE_LACPDU;
820
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700821 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Joe Perchesada0f862014-02-15 16:02:17 -0800823 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400824 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100825 * because we use it to identify loopback MARKERs in receive.
826 */
Joe Perchesada0f862014-02-15 16:02:17 -0800827 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800828 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100830 marker_header->marker = *marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 dev_queue_xmit(skb);
833
834 return 0;
835}
836
837/**
838 * ad_mux_machine - handle a port's mux state machine
839 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -0700840 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
Mahesh Bandewaree637712014-10-04 17:45:01 -0700842static void ad_mux_machine(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 mux_states_t last_state;
845
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100846 /* keep current State Machine state to compare later if it was
847 * changed
848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 last_state = port->sm_mux_state;
850
851 if (port->sm_vars & AD_PORT_BEGIN) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100852 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else {
854 switch (port->sm_mux_state) {
855 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000856 if ((port->sm_vars & AD_PORT_SELECTED)
857 || (port->sm_vars & AD_PORT_STANDBY))
858 /* if SELECTED or STANDBY */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100859 port->sm_mux_state = AD_MUX_WAITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 case AD_MUX_WAITING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100862 /* if SELECTED == FALSE return to DETACH state */
863 if (!(port->sm_vars & AD_PORT_SELECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100865 /* in order to withhold the Selection Logic to
866 * check all ports READY_N value every callback
867 * cycle to update ready variable, we check
868 * READY_N and update READY here
869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100871 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873 }
874
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100875 /* check if the wait_while_timer expired */
Bandan Das7bfc4752010-10-16 20:19:59 +0000876 if (port->sm_mux_timer_counter
877 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100880 /* in order to withhold the selection logic to check
881 * all ports READY_N value every callback cycle to
882 * update ready variable, we check READY_N and update
883 * READY here
884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
886
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100887 /* if the wait_while_timer expired, and the port is
888 * in READY state, move to ATTACHED state
889 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000890 if ((port->sm_vars & AD_PORT_READY)
891 && !port->sm_mux_timer_counter)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100892 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
894 case AD_MUX_ATTACHED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100895 /* check also if agg_select_timer expired (so the
896 * edable port will take place only after this timer)
897 */
898 if ((port->sm_vars & AD_PORT_SELECTED) &&
899 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
900 !__check_agg_selection_timer(port)) {
901 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;
902 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
903 (port->sm_vars & AD_PORT_STANDBY)) {
904 /* if UNSELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100906 /* in order to withhold the selection logic to
907 * check all ports READY_N value every callback
908 * cycle to update ready variable, we check
909 * READY_N and update READY here
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100912 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 break;
915 case AD_MUX_COLLECTING_DISTRIBUTING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100916 if (!(port->sm_vars & AD_PORT_SELECTED) ||
917 (port->sm_vars & AD_PORT_STANDBY) ||
918 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)) {
919 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100921 /* if port state hasn't changed make
922 * sure that a collecting distributing
923 * port in an active aggregator is enabled
924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (port->aggregator &&
926 port->aggregator->is_active &&
927 !__port_is_enabled(port)) {
928
929 __enable_port(port);
930 }
931 }
932 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100933 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935 }
936 }
937
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100938 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (port->sm_mux_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800940 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
941 port->actor_port_number, last_state,
942 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (port->sm_mux_state) {
944 case AD_MUX_DETACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700946 ad_disable_collecting_distributing(port,
947 update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
949 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800950 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
952 case AD_MUX_WAITING:
953 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
954 break;
955 case AD_MUX_ATTACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
957 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
958 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700959 ad_disable_collecting_distributing(port,
960 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800961 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 break;
963 case AD_MUX_COLLECTING_DISTRIBUTING:
964 port->actor_oper_port_state |= AD_STATE_COLLECTING;
965 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700966 ad_enable_collecting_distributing(port,
967 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800968 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100970 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 break;
972 }
973 }
974}
975
976/**
977 * ad_rx_machine - handle a port's rx State Machine
978 * @lacpdu: the lacpdu we've received
979 * @port: the port we're looking at
980 *
981 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
982 * CURRENT. If timer expired set the state machine in the proper state.
983 * In other cases, this function checks if we need to switch to other state.
984 */
985static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
986{
987 rx_states_t last_state;
988
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100989 /* keep current State Machine state to compare later if it was
990 * changed
991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 last_state = port->sm_rx_state;
993
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100994 /* check if state machine should change state */
995
996 /* first, check if port was reinitialized */
Bandan Das7bfc4752010-10-16 20:19:59 +0000997 if (port->sm_vars & AD_PORT_BEGIN)
Bandan Das7bfc4752010-10-16 20:19:59 +0000998 port->sm_rx_state = AD_RX_INITIALIZE;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100999 /* check if port is not enabled */
Bandan Das7bfc4752010-10-16 20:19:59 +00001000 else if (!(port->sm_vars & AD_PORT_BEGIN)
1001 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
Bandan Das7bfc4752010-10-16 20:19:59 +00001002 port->sm_rx_state = AD_RX_PORT_DISABLED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001003 /* check if new lacpdu arrived */
1004 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1005 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1006 (port->sm_rx_state == AD_RX_CURRENT))) {
1007 port->sm_rx_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 port->sm_rx_state = AD_RX_CURRENT;
1009 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001010 /* if timer is on, and if it is expired */
1011 if (port->sm_rx_timer_counter &&
1012 !(--port->sm_rx_timer_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 switch (port->sm_rx_state) {
1014 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001015 port->sm_rx_state = AD_RX_DEFAULTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 case AD_RX_CURRENT:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001018 port->sm_rx_state = AD_RX_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001020 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
1022 }
1023 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001024 /* if no lacpdu arrived and no timer is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 switch (port->sm_rx_state) {
1026 case AD_RX_PORT_DISABLED:
Bandan Das7bfc4752010-10-16 20:19:59 +00001027 if (port->sm_vars & AD_PORT_MOVED)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001028 port->sm_rx_state = AD_RX_INITIALIZE;
Bandan Das7bfc4752010-10-16 20:19:59 +00001029 else if (port->is_enabled
1030 && (port->sm_vars
1031 & AD_PORT_LACP_ENABLED))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001032 port->sm_rx_state = AD_RX_EXPIRED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001033 else if (port->is_enabled
1034 && ((port->sm_vars
1035 & AD_PORT_LACP_ENABLED) == 0))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001036 port->sm_rx_state = AD_RX_LACP_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001038 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040
1041 }
1042 }
1043 }
1044
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001045 /* check if the State machine was changed or new lacpdu arrived */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001047 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1048 port->actor_port_number, last_state,
1049 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 switch (port->sm_rx_state) {
1051 case AD_RX_INITIALIZE:
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001052 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001054 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 port->sm_vars &= ~AD_PORT_SELECTED;
1057 __record_default(port);
1058 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1059 port->sm_vars &= ~AD_PORT_MOVED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001060 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001062 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 case AD_RX_PORT_DISABLED:
1064 port->sm_vars &= ~AD_PORT_MATCHED;
1065 break;
1066 case AD_RX_LACP_DISABLED:
1067 port->sm_vars &= ~AD_PORT_SELECTED;
1068 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001069 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 port->sm_vars |= AD_PORT_MATCHED;
1071 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1072 break;
1073 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001074 /* Reset of the Synchronization flag (Standard 43.4.12)
1075 * This reset cause to disable this port in the
1076 * COLLECTING_DISTRIBUTING state of the mux machine in
1077 * case of EXPIRED even if LINK_DOWN didn't arrive for
1078 * the port.
1079 */
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001080 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 port->sm_vars &= ~AD_PORT_MATCHED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001082 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1084 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1085 break;
1086 case AD_RX_DEFAULTED:
1087 __update_default_selected(port);
1088 __record_default(port);
1089 port->sm_vars |= AD_PORT_MATCHED;
1090 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1091 break;
1092 case AD_RX_CURRENT:
dingtianhong815117a2014-01-02 09:12:54 +08001093 /* detect loopback situation */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001094 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1095 &(port->actor_system))) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001096 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
Joe Perches90194262014-02-15 16:01:45 -08001097 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001098 port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return;
1100 }
1101 __update_selected(lacpdu, port);
1102 __update_ntt(lacpdu, port);
1103 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1105 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001107 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109 }
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113/**
1114 * ad_tx_machine - handle a port's tx state machine
1115 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
1117static void ad_tx_machine(struct port *port)
1118{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001119 /* check if tx timer expired, to verify that we do not send more than
1120 * 3 packets per second
1121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001123 /* check if there is something to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1125 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001128 pr_debug("Sent LACPDU on port %d\n",
1129 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001130
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001131 /* mark ntt as false, so it will not be sent
1132 * again until demanded
1133 */
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001134 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001137 /* restart tx timer(to verify that we will not exceed
1138 * AD_MAX_TX_IN_SECOND
1139 */
1140 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142}
1143
1144/**
1145 * ad_periodic_machine - handle a port's periodic state machine
1146 * @port: the port we're looking at
1147 *
1148 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1149 */
1150static void ad_periodic_machine(struct port *port)
1151{
1152 periodic_states_t last_state;
1153
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001154 /* keep current state machine state to compare later if it was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 last_state = port->sm_periodic_state;
1156
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001157 /* check if port was reinitialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001159 (!(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 -07001160 ) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001161 port->sm_periodic_state = AD_NO_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001163 /* check if state machine should change state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 else if (port->sm_periodic_timer_counter) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001165 /* check if periodic state machine expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!(--port->sm_periodic_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001167 /* if expired then do tx */
1168 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001170 /* If not expired, check if there is some new timeout
1171 * parameter from the partner state
1172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 switch (port->sm_periodic_state) {
1174 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001175 if (!(port->partner_oper.port_state
1176 & AD_STATE_LACP_TIMEOUT))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001177 port->sm_periodic_state = AD_SLOW_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001180 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 port->sm_periodic_timer_counter = 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001182 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001185 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 break;
1187 }
1188 }
1189 } else {
1190 switch (port->sm_periodic_state) {
1191 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001192 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 break;
1194 case AD_PERIODIC_TX:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001195 if (!(port->partner_oper.port_state &
1196 AD_STATE_LACP_TIMEOUT))
1197 port->sm_periodic_state = AD_SLOW_PERIODIC;
Bandan Das7bfc4752010-10-16 20:19:59 +00001198 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001199 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001201 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 break;
1203 }
1204 }
1205
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001206 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001208 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1209 port->actor_port_number, last_state,
1210 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 switch (port->sm_periodic_state) {
1212 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001213 port->sm_periodic_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 break;
1215 case AD_FAST_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001216 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1217 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 -07001218 break;
1219 case AD_SLOW_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001220 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1221 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 -07001222 break;
1223 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001224 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001226 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228 }
1229 }
1230}
1231
1232/**
1233 * ad_port_selection_logic - select aggregation groups
1234 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001235 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 *
1237 * Select aggregation groups, and assign each port for it's aggregetor. The
1238 * selection logic is called in the inititalization (after all the handshkes),
1239 * and after every lacpdu receive (if selected is off).
1240 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001241static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1244 struct port *last_port = NULL, *curr_port;
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001245 struct list_head *iter;
1246 struct bonding *bond;
1247 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int found = 0;
1249
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001250 /* if the port is already Selected, do nothing */
Bandan Das7bfc4752010-10-16 20:19:59 +00001251 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001254 bond = __get_bond_by_port(port);
1255
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001256 /* if the port is connected to other aggregator, detach it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (port->aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001258 /* detach the port from its former aggregator */
Bandan Das128ea6c2010-10-16 20:19:58 +00001259 temp_aggregator = port->aggregator;
1260 for (curr_port = temp_aggregator->lag_ports; curr_port;
1261 last_port = curr_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001262 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (curr_port == port) {
1264 temp_aggregator->num_of_ports--;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001265 /* if it is the first port attached to the
1266 * aggregator
1267 */
1268 if (!last_port) {
Bandan Das128ea6c2010-10-16 20:19:58 +00001269 temp_aggregator->lag_ports =
1270 port->next_port_in_aggregator;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001271 } else {
1272 /* not the first port attached to the
1273 * aggregator
1274 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001275 last_port->next_port_in_aggregator =
1276 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001279 /* clear the port's relations to this
1280 * aggregator
1281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001283 port->next_port_in_aggregator = NULL;
1284 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001286 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1287 port->actor_port_number,
1288 temp_aggregator->aggregator_identifier);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001289 /* if the aggregator is empty, clear its
1290 * parameters, and set it ready to be attached
1291 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001292 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
1295 }
1296 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001297 if (!curr_port) {
1298 /* meaning: the port was related to an aggregator
1299 * but was not on the aggregator port list
1300 */
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001301 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1302 port->slave->bond->dev->name,
1303 port->actor_port_number,
1304 port->slave->dev->name,
1305 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001308 /* search on all aggregators for a suitable aggregator for this port */
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001309 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001310 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001312 /* keep a free aggregator for later use(if needed) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001314 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001315 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 continue;
1317 }
dingtianhong815117a2014-01-02 09:12:54 +08001318 /* check if current aggregator suits us */
1319 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1320 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001321 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1322 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 ) &&
dingtianhong815117a2014-01-02 09:12:54 +08001324 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1325 !aggregator->is_individual) /* but is not individual OR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 )
1327 ) {
dingtianhong815117a2014-01-02 09:12:54 +08001328 /* attach to the founded aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001330 port->actor_port_aggregator_identifier =
1331 port->aggregator->aggregator_identifier;
1332 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001334 aggregator->lag_ports = port;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001335 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1336 port->actor_port_number,
1337 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001339 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 port->sm_vars |= AD_PORT_SELECTED;
1341 found = 1;
1342 break;
1343 }
1344 }
1345
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001346 /* the port couldn't find an aggregator - attach it to a new
1347 * aggregator
1348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (!found) {
1350 if (free_aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001351 /* assign port a new aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001353 port->actor_port_aggregator_identifier =
1354 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001356 /* update the new aggregator's parameters
1357 * if port was responsed from the end-user
1358 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001359 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
Bandan Das7bfc4752010-10-16 20:19:59 +00001360 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001361 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001362 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001363 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1366 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001367 port->aggregator->partner_system =
1368 port->partner_oper.system;
1369 port->aggregator->partner_system_priority =
1370 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001371 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 port->aggregator->receive_state = 1;
1373 port->aggregator->transmit_state = 1;
1374 port->aggregator->lag_ports = port;
1375 port->aggregator->num_of_ports++;
1376
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001377 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 port->sm_vars |= AD_PORT_SELECTED;
1379
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001380 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1381 port->actor_port_number,
1382 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001384 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 port->actor_port_number, port->slave->dev->name);
1386 }
1387 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001388 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1389 * in all aggregator's ports, else set ready=FALSE in all
1390 * aggregator's ports
1391 */
1392 __set_agg_ports_ready(port->aggregator,
1393 __agg_ports_are_ready(port->aggregator));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Jay Vosburghfd989c82008-11-04 17:51:16 -08001395 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001396 ad_agg_selection_logic(aggregator, update_slave_arr);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001397}
1398
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001399/* Decide if "agg" is a better choice for the new active aggregator that
Jay Vosburghfd989c82008-11-04 17:51:16 -08001400 * the current best, according to the ad_select policy.
1401 */
1402static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1403 struct aggregator *curr)
1404{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001405 /* 0. If no best, select current.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001406 *
1407 * 1. If the current agg is not individual, and the best is
1408 * individual, select current.
1409 *
1410 * 2. If current agg is individual and the best is not, keep best.
1411 *
1412 * 3. Therefore, current and best are both individual or both not
1413 * individual, so:
1414 *
1415 * 3a. If current agg partner replied, and best agg partner did not,
1416 * select current.
1417 *
1418 * 3b. If current agg partner did not reply and best agg partner
1419 * did reply, keep best.
1420 *
1421 * 4. Therefore, current and best both have partner replies or
1422 * both do not, so perform selection policy:
1423 *
1424 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1425 * select by bandwidth.
1426 *
1427 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1428 */
1429 if (!best)
1430 return curr;
1431
1432 if (!curr->is_individual && best->is_individual)
1433 return curr;
1434
1435 if (curr->is_individual && !best->is_individual)
1436 return best;
1437
1438 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1439 return curr;
1440
1441 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1442 return best;
1443
1444 switch (__get_agg_selection_mode(curr->lag_ports)) {
1445 case BOND_AD_COUNT:
1446 if (curr->num_of_ports > best->num_of_ports)
1447 return curr;
1448
1449 if (curr->num_of_ports < best->num_of_ports)
1450 return best;
1451
1452 /*FALLTHROUGH*/
1453 case BOND_AD_STABLE:
1454 case BOND_AD_BANDWIDTH:
1455 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1456 return curr;
1457
1458 break;
1459
1460 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001461 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1462 curr->slave->bond->dev->name,
1463 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001466
1467 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001470static int agg_device_up(const struct aggregator *agg)
1471{
Jiri Bohac2430af82011-04-19 02:09:55 +00001472 struct port *port = agg->lag_ports;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001473
Jiri Bohac2430af82011-04-19 02:09:55 +00001474 if (!port)
1475 return 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001476
1477 return netif_running(port->slave->dev) &&
1478 netif_carrier_ok(port->slave->dev);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481/**
1482 * ad_agg_selection_logic - select an aggregation group for a team
1483 * @aggregator: the aggregator we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001484 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
1486 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001487 *
1488 * The logic of this function is to select the aggregator according to
1489 * the ad_select policy:
1490 *
1491 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1492 * it, and to reselect the active aggregator only if the previous
1493 * aggregator has no more ports related to it.
1494 *
1495 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1496 * bandwidth, and reselect whenever a link state change takes place or the
1497 * set of slaves in the bond changes.
1498 *
1499 * BOND_AD_COUNT: select the aggregator with largest number of ports
1500 * (slaves), and reselect whenever a link state change takes place or the
1501 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
1503 * FIXME: this function MUST be called with the first agg in the bond, or
1504 * __get_active_agg() won't work correctly. This function should be better
1505 * called with the bond itself, and retrieve the first agg from it.
1506 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001507static void ad_agg_selection_logic(struct aggregator *agg,
1508 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001510 struct aggregator *best, *active, *origin;
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001511 struct bonding *bond = agg->slave->bond;
1512 struct list_head *iter;
1513 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Veaceslav Falico49b76242014-01-10 11:59:45 +01001516 rcu_read_lock();
Jay Vosburghfd989c82008-11-04 17:51:16 -08001517 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001518 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001519 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
dingtianhongbe79bd02013-12-13 10:20:12 +08001521 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001522 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001523
Jay Vosburghfd989c82008-11-04 17:51:16 -08001524 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001526 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001527 best = ad_agg_selection_test(best, agg);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Jay Vosburghfd989c82008-11-04 17:51:16 -08001530 if (best &&
1531 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001532 /* For the STABLE policy, don't replace the old active
Jay Vosburghfd989c82008-11-04 17:51:16 -08001533 * aggregator if it's still active (it has an answering
1534 * partner) or if both the best and active don't have an
1535 * answering partner.
1536 */
1537 if (active && active->lag_ports &&
1538 active->lag_ports->is_enabled &&
1539 (__agg_has_partner(active) ||
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001540 (!__agg_has_partner(active) &&
1541 !__agg_has_partner(best)))) {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001542 if (!(!active->actor_oper_aggregator_key &&
1543 best->actor_oper_aggregator_key)) {
1544 best = NULL;
1545 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547 }
1548 }
1549
Jay Vosburghfd989c82008-11-04 17:51:16 -08001550 if (best && (best == active)) {
1551 best = NULL;
1552 active->is_active = 1;
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
dingtianhongbe79bd02013-12-13 10:20:12 +08001555 /* if there is new best aggregator, activate it */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001556 if (best) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001557 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1558 best->aggregator_identifier, best->num_of_ports,
1559 best->actor_oper_aggregator_key,
1560 best->partner_oper_aggregator_key,
1561 best->is_individual, best->is_active);
1562 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1563 best->lag_ports, best->slave,
1564 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001565
dingtianhongbe79bd02013-12-13 10:20:12 +08001566 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001567 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001568
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001569 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1570 agg->aggregator_identifier, agg->num_of_ports,
1571 agg->actor_oper_aggregator_key,
1572 agg->partner_oper_aggregator_key,
1573 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
dingtianhongbe79bd02013-12-13 10:20:12 +08001576 /* check if any partner replys */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001577 if (best->is_individual) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001578 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1579 best->slave ?
1580 best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
Jay Vosburghfd989c82008-11-04 17:51:16 -08001583 best->is_active = 1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001584 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1585 best->aggregator_identifier);
1586 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1587 best->aggregator_identifier, best->num_of_ports,
1588 best->actor_oper_aggregator_key,
1589 best->partner_oper_aggregator_key,
1590 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001592 /* disable the ports that were related to the former
1593 * active_aggregator
1594 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001595 if (active) {
1596 for (port = active->lag_ports; port;
1597 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 __disable_port(port);
1599 }
1600 }
Mahesh Bandewaree637712014-10-04 17:45:01 -07001601 /* Slave array needs update. */
1602 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001605 /* if the selected aggregator is of join individuals
Jay Vosburghfd989c82008-11-04 17:51:16 -08001606 * (partner_system is NULL), enable their ports
1607 */
1608 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Jay Vosburghfd989c82008-11-04 17:51:16 -08001610 if (active) {
1611 if (!__agg_has_partner(active)) {
1612 for (port = active->lag_ports; port;
1613 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 __enable_port(port);
1615 }
1616 }
1617 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001618
dingtianhongbe79bd02013-12-13 10:20:12 +08001619 rcu_read_unlock();
1620
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001621 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
1624/**
1625 * ad_clear_agg - clear a given aggregator's parameters
1626 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628static void ad_clear_agg(struct aggregator *aggregator)
1629{
1630 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001631 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 aggregator->actor_admin_aggregator_key = 0;
1633 aggregator->actor_oper_aggregator_key = 0;
1634 aggregator->partner_system = null_mac_addr;
1635 aggregator->partner_system_priority = 0;
1636 aggregator->partner_oper_aggregator_key = 0;
1637 aggregator->receive_state = 0;
1638 aggregator->transmit_state = 0;
1639 aggregator->lag_ports = NULL;
1640 aggregator->is_active = 0;
1641 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001642 pr_debug("LAG %d was cleared\n",
1643 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645}
1646
1647/**
1648 * ad_initialize_agg - initialize a given aggregator's parameters
1649 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651static void ad_initialize_agg(struct aggregator *aggregator)
1652{
1653 if (aggregator) {
1654 ad_clear_agg(aggregator);
1655
1656 aggregator->aggregator_mac_address = null_mac_addr;
1657 aggregator->aggregator_identifier = 0;
1658 aggregator->slave = NULL;
1659 }
1660}
1661
1662/**
1663 * ad_initialize_port - initialize a given port's parameters
1664 * @aggregator: the aggregator we're looking at
1665 * @lacp_fast: boolean. whether fast periodic should be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
1667static void ad_initialize_port(struct port *port, int lacp_fast)
1668{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001669 static const struct port_params tmpl = {
1670 .system_priority = 0xffff,
1671 .key = 1,
1672 .port_number = 1,
1673 .port_priority = 0xff,
1674 .port_state = 1,
1675 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001676 static const struct lacpdu lacpdu = {
1677 .subtype = 0x01,
1678 .version_number = 0x01,
1679 .tlv_type_actor_info = 0x01,
1680 .actor_information_length = 0x14,
1681 .tlv_type_partner_info = 0x02,
1682 .partner_information_length = 0x14,
1683 .tlv_type_collector_info = 0x03,
1684 .collector_information_length = 0x10,
1685 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1686 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (port) {
1689 port->actor_port_number = 1;
1690 port->actor_port_priority = 0xff;
1691 port->actor_system = null_mac_addr;
1692 port->actor_system_priority = 0xffff;
1693 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001694 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 port->actor_admin_port_key = 1;
1696 port->actor_oper_port_key = 1;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001697 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1698 AD_STATE_LACP_ACTIVITY;
1699 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1700 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Bandan Das7bfc4752010-10-16 20:19:59 +00001702 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001705 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1706 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1707
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001708 port->is_enabled = true;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001709 /* private parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 port->sm_vars = 0x3;
1711 port->sm_rx_state = 0;
1712 port->sm_rx_timer_counter = 0;
1713 port->sm_periodic_state = 0;
1714 port->sm_periodic_timer_counter = 0;
1715 port->sm_mux_state = 0;
1716 port->sm_mux_timer_counter = 0;
1717 port->sm_tx_state = 0;
1718 port->sm_tx_timer_counter = 0;
1719 port->slave = NULL;
1720 port->aggregator = NULL;
1721 port->next_port_in_aggregator = NULL;
1722 port->transaction_id = 0;
1723
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001724 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726}
1727
1728/**
1729 * ad_enable_collecting_distributing - enable a port's transmit/receive
1730 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001731 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 *
1733 * Enable @port if it's in an active aggregator
1734 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001735static void ad_enable_collecting_distributing(struct port *port,
1736 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001739 pr_debug("Enabling port %d(LAG %d)\n",
1740 port->actor_port_number,
1741 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 __enable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001743 /* Slave array needs update */
1744 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746}
1747
1748/**
1749 * ad_disable_collecting_distributing - disable a port's transmit/receive
1750 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001751 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001753static void ad_disable_collecting_distributing(struct port *port,
1754 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001756 if (port->aggregator &&
1757 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1758 &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001759 pr_debug("Disabling port %d(LAG %d)\n",
1760 port->actor_port_number,
1761 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 __disable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001763 /* Slave array needs an update */
1764 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/**
1769 * ad_marker_info_received - handle receive of a Marker information frame
1770 * @marker_info: Marker info received
1771 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001773static void ad_marker_info_received(struct bond_marker *marker_info,
1774 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001776 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001778 /* copy the received marker data to the response marker */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001779 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001780 /* change the marker subtype to marker response */
Bandan Das128ea6c2010-10-16 20:19:58 +00001781 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001783 /* send the marker response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001785 pr_debug("Sent Marker Response on port %d\n",
1786 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788}
1789
1790/**
1791 * ad_marker_response_received - handle receive of a marker response frame
1792 * @marker: marker PDU received
1793 * @port: the port we're looking at
1794 *
1795 * This function does nothing since we decided not to implement send and handle
1796 * response for marker PDU's, in this stage, but only to respond to marker
1797 * information.
1798 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001799static void ad_marker_response_received(struct bond_marker *marker,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001800 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001802 marker = NULL;
1803 port = NULL;
1804 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001807/* ========= AD exported functions to the main bonding code ========= */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001809/* Check aggregators status in team every T seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810#define AD_AGGREGATOR_SELECTION_TIMER 8
1811
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001812/**
1813 * bond_3ad_initiate_agg_selection - initate aggregator selection
1814 * @bond: bonding struct
Jay Vosburghfd989c82008-11-04 17:51:16 -08001815 *
1816 * Set the aggregation selection timer, to initiate an agg selection in
1817 * the very near future. Called during first initialization, and during
1818 * any down to up transitions of the bond.
1819 */
1820void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1821{
1822 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/**
1826 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1827 * @bond: bonding struct to work on
1828 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 *
1830 * Can be called only after the mac address of the bond is set.
1831 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001832void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001833{
dingtianhong815117a2014-01-02 09:12:54 +08001834 /* check that the bond is not initialized yet */
1835 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001836 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001838 BOND_AD_INFO(bond).aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1841 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1842
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001843 /* initialize how many times this module is called in one
1844 * second (should be about every 100ms)
1845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 ad_ticks_per_sec = tick_resolution;
1847
Jay Vosburghfd989c82008-11-04 17:51:16 -08001848 bond_3ad_initiate_agg_selection(bond,
1849 AD_AGGREGATOR_SELECTION_TIMER *
1850 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852}
1853
1854/**
1855 * bond_3ad_bind_slave - initialize a slave's port
1856 * @slave: slave struct to work on
1857 *
1858 * Returns: 0 on success
1859 * < 0 on error
1860 */
dingtianhong359632e2014-01-02 09:13:12 +08001861void bond_3ad_bind_slave(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct bonding *bond = bond_get_bond_by_slave(slave);
1864 struct port *port;
1865 struct aggregator *aggregator;
1866
dingtianhong359632e2014-01-02 09:13:12 +08001867 /* check that the slave has not been initialized yet. */
dingtianhong3fdddd82014-05-12 15:08:43 +08001868 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
dingtianhong359632e2014-01-02 09:13:12 +08001870 /* port initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001871 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001873 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 port->slave = slave;
dingtianhong3fdddd82014-05-12 15:08:43 +08001876 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
dingtianhong359632e2014-01-02 09:13:12 +08001877 /* key is determined according to the link speed, duplex and user key(which
1878 * is yet not supported)
dingtianhong359632e2014-01-02 09:13:12 +08001879 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001880 port->actor_admin_port_key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 port->actor_admin_port_key |= __get_duplex(port);
1882 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1883 port->actor_oper_port_key = port->actor_admin_port_key;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001884 /* if the port is not full duplex, then the port should be not
1885 * lacp Enabled
1886 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001887 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
dingtianhong359632e2014-01-02 09:13:12 +08001889 /* actor system is the bond's system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001891 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
1892 * lacpdu's are sent in one second)
1893 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1895 port->aggregator = NULL;
1896 port->next_port_in_aggregator = NULL;
1897
1898 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
dingtianhong359632e2014-01-02 09:13:12 +08001900 /* aggregator initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001901 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 ad_initialize_agg(aggregator);
1904
1905 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001906 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 aggregator->slave = slave;
1908 aggregator->is_active = 0;
1909 aggregator->num_of_ports = 0;
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913/**
1914 * bond_3ad_unbind_slave - deinitialize a slave's port
1915 * @slave: slave struct to work on
1916 *
1917 * Search for the aggregator that is related to this port, remove the
1918 * aggregator and assign another aggregator for other port related to it
1919 * (if any), and remove the port.
1920 */
1921void bond_3ad_unbind_slave(struct slave *slave)
1922{
1923 struct port *port, *prev_port, *temp_port;
1924 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1925 int select_new_active_agg = 0;
Veaceslav Falico0b088262013-09-27 16:12:02 +02001926 struct bonding *bond = slave->bond;
1927 struct slave *slave_iter;
1928 struct list_head *iter;
Mahesh Bandewaree637712014-10-04 17:45:01 -07001929 bool dummy_slave_update; /* Ignore this value as caller updates array */
Jasper Spaansa361c832009-10-23 04:09:24 +00001930
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001931 /* Sync against bond_3ad_state_machine_handler() */
1932 spin_lock_bh(&bond->mode_lock);
dingtianhong3fdddd82014-05-12 15:08:43 +08001933 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1934 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001936 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001938 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
1939 slave->dev->name);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001943 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
1944 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /* Tell the partner that this port is not suitable for aggregation */
1947 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1948 __update_lacpdu_from_port(port);
1949 ad_lacpdu_send(port);
1950
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001951 /* check if this aggregator is occupied */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (aggregator->lag_ports) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001953 /* check if there are other ports related to this aggregator
1954 * except the port related to this slave(thats ensure us that
1955 * there is a reason to search for new aggregator, and that we
1956 * will find one
1957 */
1958 if ((aggregator->lag_ports != port) ||
1959 (aggregator->lag_ports->next_port_in_aggregator)) {
1960 /* find new aggregator for the related port(s) */
Veaceslav Falico0b088262013-09-27 16:12:02 +02001961 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001962 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001963 /* if the new aggregator is empty, or it is
1964 * connected to our port only
1965 */
1966 if (!new_aggregator->lag_ports ||
1967 ((new_aggregator->lag_ports == port) &&
1968 !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Veaceslav Falico0b088262013-09-27 16:12:02 +02001971 if (!slave_iter)
1972 new_aggregator = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001973
1974 /* if new aggregator found, copy the aggregator's
1975 * parameters and connect the related lag_ports to the
1976 * new aggregator
1977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 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 +02001979 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
1980 aggregator->aggregator_identifier,
1981 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001983 if ((new_aggregator->lag_ports == port) &&
1984 new_aggregator->is_active) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001985 netdev_info(bond->dev, "Removing an active aggregator\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 select_new_active_agg = 1;
1987 }
1988
1989 new_aggregator->is_individual = aggregator->is_individual;
1990 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
1991 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
1992 new_aggregator->partner_system = aggregator->partner_system;
1993 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
1994 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
1995 new_aggregator->receive_state = aggregator->receive_state;
1996 new_aggregator->transmit_state = aggregator->transmit_state;
1997 new_aggregator->lag_ports = aggregator->lag_ports;
1998 new_aggregator->is_active = aggregator->is_active;
1999 new_aggregator->num_of_ports = aggregator->num_of_ports;
2000
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002001 /* update the information that is written on
2002 * the ports about the aggregator
2003 */
Bandan Das128ea6c2010-10-16 20:19:58 +00002004 for (temp_port = aggregator->lag_ports; temp_port;
2005 temp_port = temp_port->next_port_in_aggregator) {
2006 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2008 }
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00002011
Bandan Das7bfc4752010-10-16 20:19:59 +00002012 if (select_new_active_agg)
Mahesh Bandewaree637712014-10-04 17:45:01 -07002013 ad_agg_selection_logic(__get_first_agg(port),
2014 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002016 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002018 } else {
2019 /* in case that the only port related to this
2020 * aggregator is the one we want to remove
2021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 select_new_active_agg = aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 ad_clear_agg(aggregator);
2024 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002025 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002026 /* select new active aggregator */
Veaceslav Falico74684492013-09-27 15:10:58 +02002027 temp_aggregator = __get_first_agg(port);
2028 if (temp_aggregator)
Mahesh Bandewaree637712014-10-04 17:45:01 -07002029 ad_agg_selection_logic(temp_aggregator,
2030 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032 }
2033 }
2034
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002035 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002036
2037 /* find the aggregator that this port is connected to */
Veaceslav Falico0b088262013-09-27 16:12:02 +02002038 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002039 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 prev_port = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002041 /* search the port in the aggregator's related ports */
Bandan Das128ea6c2010-10-16 20:19:58 +00002042 for (temp_port = temp_aggregator->lag_ports; temp_port;
2043 prev_port = temp_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002044 temp_port = temp_port->next_port_in_aggregator) {
2045 if (temp_port == port) {
2046 /* the aggregator found - detach the port from
2047 * this aggregator
2048 */
Bandan Das7bfc4752010-10-16 20:19:59 +00002049 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00002051 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00002054 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 select_new_active_agg = temp_aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ad_clear_agg(temp_aggregator);
2057 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002058 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002059 /* select new active aggregator */
Mahesh Bandewaree637712014-10-04 17:45:01 -07002060 ad_agg_selection_logic(__get_first_agg(port),
2061 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 }
2064 break;
2065 }
2066 }
2067 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002068 port->slave = NULL;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002069
2070out:
2071 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074/**
2075 * bond_3ad_state_machine_handler - handle state machines timeout
2076 * @bond: bonding struct to work on
2077 *
2078 * The state machine handling concept in this module is to check every tick
2079 * which state machine should operate any function. The execution order is
2080 * round robin, so when we have an interaction between state machines, the
2081 * reply of one to each other might be delayed until next tick.
2082 *
2083 * This function also complete the initialization when the agg_select_timer
2084 * times out, and it selects an aggregator for the ports that are yet not
2085 * related to any aggregator, and selects the active aggregator for a bond.
2086 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002087void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002089 struct bonding *bond = container_of(work, struct bonding,
2090 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002092 struct list_head *iter;
2093 struct slave *slave;
2094 struct port *port;
dingtianhong5e5b0662014-02-26 11:05:22 +08002095 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Mahesh Bandewaree637712014-10-04 17:45:01 -07002096 bool update_slave_arr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002098 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2099 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2100 * concurrently due to incoming LACPDU as well.
2101 */
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002102 spin_lock_bh(&bond->mode_lock);
dingtianhongbe79bd02013-12-13 10:20:12 +08002103 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04002104
dingtianhongbe79bd02013-12-13 10:20:12 +08002105 /* check if there are any slaves */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002106 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
dingtianhongbe79bd02013-12-13 10:20:12 +08002109 /* check if agg_select_timer timer after initialize is timed out */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002110 if (BOND_AD_INFO(bond).agg_select_timer &&
2111 !(--BOND_AD_INFO(bond).agg_select_timer)) {
dingtianhongbe79bd02013-12-13 10:20:12 +08002112 slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +08002113 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002114
dingtianhongbe79bd02013-12-13 10:20:12 +08002115 /* select the active aggregator for the bond */
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002116 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002118 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2119 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto re_arm;
2121 }
2122
2123 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002124 ad_agg_selection_logic(aggregator, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002126 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128
dingtianhongbe79bd02013-12-13 10:20:12 +08002129 /* for each port run the state machines */
2130 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002131 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002133 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
Veaceslav Falico86a2b9c2014-03-16 17:55:03 +01002134 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 goto re_arm;
2136 }
2137
2138 ad_rx_machine(NULL, port);
2139 ad_periodic_machine(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002140 ad_port_selection_logic(port, &update_slave_arr);
2141 ad_mux_machine(port, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ad_tx_machine(port);
2143
dingtianhongbe79bd02013-12-13 10:20:12 +08002144 /* turn off the BEGIN bit, since we already handled it */
Bandan Das7bfc4752010-10-16 20:19:59 +00002145 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 port->sm_vars &= ~AD_PORT_BEGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
2149re_arm:
dingtianhong5e5b0662014-02-26 11:05:22 +08002150 bond_for_each_slave_rcu(bond, slave, iter) {
2151 if (slave->should_notify) {
2152 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2153 break;
2154 }
2155 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002156 rcu_read_unlock();
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002157 spin_unlock_bh(&bond->mode_lock);
dingtianhong5e5b0662014-02-26 11:05:22 +08002158
Mahesh Bandewaree637712014-10-04 17:45:01 -07002159 if (update_slave_arr)
2160 bond_slave_arr_work_rearm(bond, 0);
2161
dingtianhong5e5b0662014-02-26 11:05:22 +08002162 if (should_notify_rtnl && rtnl_trylock()) {
dingtianhongb0929912014-02-26 11:05:23 +08002163 bond_slave_state_notify(bond);
dingtianhong5e5b0662014-02-26 11:05:22 +08002164 rtnl_unlock();
2165 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002166 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
2169/**
2170 * bond_3ad_rx_indication - handle a received frame
2171 * @lacpdu: received lacpdu
2172 * @slave: slave struct to work on
2173 * @length: length of the data received
2174 *
2175 * It is assumed that frames that were sent on this NIC don't returned as new
2176 * received frames (loopback). Since only the payload is given to this
2177 * function, it check for loopback.
2178 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002179static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2180 u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002183 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 if (length >= sizeof(struct lacpdu)) {
2186
dingtianhong3fdddd82014-05-12 15:08:43 +08002187 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002190 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2191 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194
2195 switch (lacpdu->subtype) {
2196 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002197 ret = RX_HANDLER_CONSUMED;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002198 netdev_dbg(slave->bond->dev, "Received LACPDU on port %d\n",
2199 port->actor_port_number);
Nils Carlson16d79d72011-03-03 22:09:11 +00002200 /* Protect against concurrent state machines */
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002201 spin_lock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 ad_rx_machine(lacpdu, port);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002203 spin_unlock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 break;
2205
2206 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002207 ret = RX_HANDLER_CONSUMED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002208 /* No need to convert fields to Little Endian since we
2209 * don't use the marker's fields.
2210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002212 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 case AD_MARKER_INFORMATION_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002214 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2215 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002216 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 break;
2218
2219 case AD_MARKER_RESPONSE_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002220 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2221 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002222 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224
2225 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002226 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2227 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 }
2230 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234/**
2235 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2236 * @slave: slave struct to work on
2237 *
2238 * Handle reselection of aggregator (if needed) for this port.
2239 */
2240void bond_3ad_adapter_speed_changed(struct slave *slave)
2241{
2242 struct port *port;
2243
dingtianhong3fdddd82014-05-12 15:08:43 +08002244 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
dingtianhong71a06c52013-12-13 17:29:19 +08002246 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002248 netdev_warn(slave->bond->dev, "speed changed for uninitialized port on %s\n",
2249 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return;
2251 }
2252
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002253 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong71a06c52013-12-13 17:29:19 +08002254
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002255 port->actor_admin_port_key &= ~AD_SPEED_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002256 port->actor_oper_port_key = port->actor_admin_port_key |=
2257 (__get_link_speed(port) << 1);
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002258 netdev_dbg(slave->bond->dev, "Port %d changed speed\n", port->actor_port_number);
dingtianhong71a06c52013-12-13 17:29:19 +08002259 /* there is no need to reselect a new aggregator, just signal the
2260 * state machines to reinitialize
2261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 port->sm_vars |= AD_PORT_BEGIN;
dingtianhong71a06c52013-12-13 17:29:19 +08002263
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002264 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267/**
2268 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2269 * @slave: slave struct to work on
2270 *
2271 * Handle reselection of aggregator (if needed) for this port.
2272 */
2273void bond_3ad_adapter_duplex_changed(struct slave *slave)
2274{
2275 struct port *port;
2276
dingtianhong3fdddd82014-05-12 15:08:43 +08002277 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
dingtianhongbca44a72013-12-13 17:29:24 +08002279 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002281 netdev_warn(slave->bond->dev, "duplex changed for uninitialized port on %s\n",
2282 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return;
2284 }
2285
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002286 spin_lock_bh(&slave->bond->mode_lock);
dingtianhongbca44a72013-12-13 17:29:24 +08002287
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002288 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002289 port->actor_oper_port_key = port->actor_admin_port_key |=
2290 __get_duplex(port);
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002291 netdev_dbg(slave->bond->dev, "Port %d changed duplex\n", port->actor_port_number);
dingtianhongbca44a72013-12-13 17:29:24 +08002292 /* there is no need to reselect a new aggregator, just signal the
2293 * state machines to reinitialize
2294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 port->sm_vars |= AD_PORT_BEGIN;
dingtianhongbca44a72013-12-13 17:29:24 +08002296
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002297 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
2300/**
2301 * bond_3ad_handle_link_change - handle a slave's link status change indication
2302 * @slave: slave struct to work on
2303 * @status: whether the link is now up or down
2304 *
2305 * Handle reselection of aggregator (if needed) for this port.
2306 */
2307void bond_3ad_handle_link_change(struct slave *slave, char link)
2308{
2309 struct port *port;
2310
dingtianhong3fdddd82014-05-12 15:08:43 +08002311 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
dingtianhong108db732013-12-13 17:29:29 +08002313 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002315 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2316 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return;
2318 }
2319
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002320 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong108db732013-12-13 17:29:29 +08002321 /* on link down we are zeroing duplex and speed since
2322 * some of the adaptors(ce1000.lan) report full duplex/speed
2323 * instead of N/A(duplex) / 0(speed).
2324 *
2325 * on link up we are forcing recheck on the duplex and speed since
2326 * some of he adaptors(ce1000.lan) report.
2327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002329 port->is_enabled = true;
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002330 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002331 port->actor_oper_port_key = port->actor_admin_port_key |=
2332 __get_duplex(port);
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002333 port->actor_admin_port_key &= ~AD_SPEED_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002334 port->actor_oper_port_key = port->actor_admin_port_key |=
2335 (__get_link_speed(port) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 } else {
2337 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002338 port->is_enabled = false;
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002339 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002340 port->actor_oper_port_key = (port->actor_admin_port_key &=
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002341 ~AD_SPEED_KEY_MASKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002343 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2344 port->actor_port_number,
2345 link == BOND_LINK_UP ? "UP" : "DOWN");
dingtianhong108db732013-12-13 17:29:29 +08002346 /* there is no need to reselect a new aggregator, just signal the
2347 * state machines to reinitialize
2348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 port->sm_vars |= AD_PORT_BEGIN;
dingtianhong108db732013-12-13 17:29:29 +08002350
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002351 spin_unlock_bh(&slave->bond->mode_lock);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002352
2353 /* RTNL is held and mode_lock is released so it's safe
2354 * to update slave_array here.
2355 */
2356 bond_update_slave_arr(slave->bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002359/**
2360 * bond_3ad_set_carrier - set link state for bonding master
2361 * @bond - bonding structure
2362 *
2363 * if we have an active aggregator, we're up, if not, we're down.
2364 * Presumes that we cannot have an active aggregator if there are
2365 * no slaves with link up.
Jay Vosburghff59c452006-03-27 13:27:43 -08002366 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002367 * This behavior complies with IEEE 802.3 section 43.3.9.
2368 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002369 * Called by bond_set_carrier(). Return zero if carrier state does not
2370 * change, nonzero if it does.
2371 */
2372int bond_3ad_set_carrier(struct bonding *bond)
2373{
stephen hemminger655f8912011-06-22 09:54:39 +00002374 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002375 struct slave *first_slave;
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002376 int ret = 1;
stephen hemminger655f8912011-06-22 09:54:39 +00002377
dingtianhongbe79bd02013-12-13 10:20:12 +08002378 rcu_read_lock();
2379 first_slave = bond_first_slave_rcu(bond);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002380 if (!first_slave) {
2381 ret = 0;
2382 goto out;
2383 }
dingtianhong3fdddd82014-05-12 15:08:43 +08002384 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002385 if (active) {
2386 /* are enough slaves available to consider link up? */
2387 if (active->num_of_ports < bond->params.min_links) {
2388 if (netif_carrier_ok(bond->dev)) {
2389 netif_carrier_off(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002390 goto out;
stephen hemminger655f8912011-06-22 09:54:39 +00002391 }
2392 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002393 netif_carrier_on(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002394 goto out;
Jay Vosburghff59c452006-03-27 13:27:43 -08002395 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002396 } else if (netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002397 netif_carrier_off(bond->dev);
Jay Vosburghff59c452006-03-27 13:27:43 -08002398 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002399out:
2400 rcu_read_unlock();
2401 return ret;
Jay Vosburghff59c452006-03-27 13:27:43 -08002402}
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002405 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 * @bond: bonding struct to work on
2407 * @ad_info: ad_info struct to fill with the bond's info
2408 *
2409 * Returns: 0 on success
2410 * < 0 on error
2411 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002412int __bond_3ad_get_active_agg_info(struct bonding *bond,
2413 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002416 struct list_head *iter;
2417 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 struct port *port;
2419
dingtianhong47e91f562013-10-15 16:28:35 +08002420 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002421 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (port->aggregator && port->aggregator->is_active) {
2423 aggregator = port->aggregator;
2424 break;
2425 }
2426 }
2427
Joe Perches21f374c2014-02-18 09:42:47 -08002428 if (!aggregator)
2429 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Joe Perches21f374c2014-02-18 09:42:47 -08002431 ad_info->aggregator_id = aggregator->aggregator_identifier;
2432 ad_info->ports = aggregator->num_of_ports;
2433 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2434 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2435 ether_addr_copy(ad_info->partner_system,
2436 aggregator->partner_system.mac_addr_value);
2437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002440int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2441{
2442 int ret;
2443
dingtianhong47e91f562013-10-15 16:28:35 +08002444 rcu_read_lock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002445 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
dingtianhong47e91f562013-10-15 16:28:35 +08002446 rcu_read_unlock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002447
2448 return ret;
2449}
2450
Eric Dumazetde063b72012-06-11 19:23:07 +00002451int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2452 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Eric Dumazetde063b72012-06-11 19:23:07 +00002454 struct lacpdu *lacpdu, _lacpdu;
2455
Jiri Pirko3aba8912011-04-19 03:48:16 +00002456 if (skb->protocol != PKT_TYPE_LACPDU)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002457 return RX_HANDLER_ANOTHER;
Neil Hormanb3053252011-01-20 09:02:31 +00002458
Eric Dumazetde063b72012-06-11 19:23:07 +00002459 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2460 if (!lacpdu)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002461 return RX_HANDLER_ANOTHER;
Andy Gospodarekab128112010-09-10 11:43:20 +00002462
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002463 return bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002465
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002466/**
2467 * bond_3ad_update_lacp_rate - change the lacp rate
2468 * @bond - bonding struct
2469 *
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002470 * When modify lacp_rate parameter via sysfs,
2471 * update actor_oper_port_state of each port.
2472 *
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002473 * Hold bond->mode_lock,
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002474 * so we can modify port->actor_oper_port_state,
2475 * no matter bond is up or down.
2476 */
2477void bond_3ad_update_lacp_rate(struct bonding *bond)
2478{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002479 struct port *port = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002480 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002481 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002482 int lacp_fast;
2483
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002484 lacp_fast = bond->params.lacp_fast;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002485 spin_lock_bh(&bond->mode_lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02002486 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002487 port = &(SLAVE_AD_INFO(slave)->port);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002488 if (lacp_fast)
2489 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2490 else
2491 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002492 }
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002493 spin_unlock_bh(&bond->mode_lock);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002494}