blob: 6bc27d9a86700822cf9d53411e161380901e42e5 [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,
91 AD_LINK_SPEED_10000MBPS
92};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
dingtianhong815117a2014-01-02 09:12:54 +080094/* compare MAC addresses */
95#define MAC_ADDRESS_EQUAL(A, B) \
96 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Bandan Das128ea6c2010-10-16 20:19:58 +000098static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static u16 ad_ticks_per_sec;
100static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
101
Holger Eitzenbergere4ac4322008-12-26 13:40:48 -0800102static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
103
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100104/* ================= main 802.3ad protocol functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int ad_lacpdu_send(struct port *port);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700106static int ad_marker_send(struct port *port, struct bond_marker *marker);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700107static void ad_mux_machine(struct port *port, bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
109static void ad_tx_machine(struct port *port);
110static void ad_periodic_machine(struct port *port);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700111static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
112static void ad_agg_selection_logic(struct aggregator *aggregator,
113 bool *update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void ad_clear_agg(struct aggregator *aggregator);
115static void ad_initialize_agg(struct aggregator *aggregator);
116static void ad_initialize_port(struct port *port, int lacp_fast);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700117static void ad_enable_collecting_distributing(struct port *port,
118 bool *update_slave_arr);
119static void ad_disable_collecting_distributing(struct port *port,
120 bool *update_slave_arr);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100121static void ad_marker_info_received(struct bond_marker *marker_info,
122 struct port *port);
123static void ad_marker_response_received(struct bond_marker *marker,
124 struct port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100127/* ================= api to bonding and kernel code ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/**
130 * __get_bond_by_port - get the port's bonding struct
131 * @port: the port we're looking at
132 *
133 * Return @port's bonding struct, or %NULL if it can't be found.
134 */
135static inline struct bonding *__get_bond_by_port(struct port *port)
136{
Bandan Das7bfc4752010-10-16 20:19:59 +0000137 if (port->slave == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return bond_get_bond_by_slave(port->slave);
141}
142
143/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * __get_first_agg - get the first aggregator in the bond
145 * @bond: the bond we're looking at
146 *
147 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
148 * found.
Veaceslav Falico768b9542014-01-10 11:59:44 +0100149 * The caller must hold RCU or RTNL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
151static inline struct aggregator *__get_first_agg(struct port *port)
152{
153 struct bonding *bond = __get_bond_by_port(port);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200154 struct slave *first_slave;
Veaceslav Falico768b9542014-01-10 11:59:44 +0100155 struct aggregator *agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
dingtianhongbe79bd02013-12-13 10:20:12 +0800157 /* If there's no bond for this port, or bond has no slaves */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200158 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100160
dingtianhongbe79bd02013-12-13 10:20:12 +0800161 rcu_read_lock();
162 first_slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +0800163 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
dingtianhongbe79bd02013-12-13 10:20:12 +0800164 rcu_read_unlock();
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100165
Veaceslav Falico768b9542014-01-10 11:59:44 +0100166 return agg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100169/**
170 * __agg_has_partner - see if we have a partner
171 * @agg: the agregator we're looking at
Jay Vosburghfd989c82008-11-04 17:51:16 -0800172 *
173 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100174 * address for the partner). Return 0 if not.
Jay Vosburghfd989c82008-11-04 17:51:16 -0800175 */
176static inline int __agg_has_partner(struct aggregator *agg)
177{
178 return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
182 * __disable_port - disable the port's slave
183 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
185static inline void __disable_port(struct port *port)
186{
dingtianhong5e5b0662014-02-26 11:05:22 +0800187 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/**
191 * __enable_port - enable the port's slave, if it's up
192 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
194static inline void __enable_port(struct port *port)
195{
196 struct slave *slave = port->slave;
197
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200198 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
dingtianhong5e5b0662014-02-26 11:05:22 +0800199 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
203 * __port_is_enabled - check if the port's slave is in active state
204 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206static inline int __port_is_enabled(struct port *port)
207{
Jiri Pirkoe30bc062011-03-12 03:14:37 +0000208 return bond_is_active_slave(port->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/**
212 * __get_agg_selection_mode - get the aggregator selection mode
213 * @port: the port we're looking at
214 *
Jay Vosburghfd989c82008-11-04 17:51:16 -0800215 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
217static inline u32 __get_agg_selection_mode(struct port *port)
218{
219 struct bonding *bond = __get_bond_by_port(port);
220
Bandan Das7bfc4752010-10-16 20:19:59 +0000221 if (bond == NULL)
Jay Vosburghfd989c82008-11-04 17:51:16 -0800222 return BOND_AD_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Peter Pan(潘卫平)1a14fbc2011-06-08 21:19:03 +0000224 return bond->params.ad_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/**
228 * __check_agg_selection_timer - check if the selection timer has expired
229 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231static inline int __check_agg_selection_timer(struct port *port)
232{
233 struct bonding *bond = __get_bond_by_port(port);
234
Bandan Das7bfc4752010-10-16 20:19:59 +0000235 if (bond == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
239}
240
241/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * __get_link_speed - get a port's speed
243 * @port: the port we're looking at
244 *
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800245 * Return @port's speed in 802.3ad enum format. i.e. one of:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * 0,
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800247 * %AD_LINK_SPEED_10MBPS,
248 * %AD_LINK_SPEED_100MBPS,
249 * %AD_LINK_SPEED_1000MBPS,
250 * %AD_LINK_SPEED_10000MBPS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252static u16 __get_link_speed(struct port *port)
253{
254 struct slave *slave = port->slave;
255 u16 speed;
256
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100257 /* this if covers only a special case: when the configuration starts
258 * with link down, it sets the speed to 0.
259 * This is done in spite of the fact that the e100 driver reports 0
260 * to be compatible with MVT in the future.
261 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000262 if (slave->link != BOND_LINK_UP)
Bandan Das128ea6c2010-10-16 20:19:58 +0000263 speed = 0;
Bandan Das7bfc4752010-10-16 20:19:59 +0000264 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 switch (slave->speed) {
266 case SPEED_10:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800267 speed = AD_LINK_SPEED_10MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269
270 case SPEED_100:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800271 speed = AD_LINK_SPEED_100MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273
274 case SPEED_1000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800275 speed = AD_LINK_SPEED_1000MBPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700278 case SPEED_10000:
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800279 speed = AD_LINK_SPEED_10000MBPS;
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700280 break;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100283 /* unknown speed value from ethtool. shouldn't happen */
284 speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 }
287 }
288
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200289 netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
290 port->actor_port_number, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return speed;
292}
293
294/**
295 * __get_duplex - get a port's duplex
296 * @port: the port we're looking at
297 *
298 * Return @port's duplex in 802.3ad bitmask format. i.e.:
299 * 0x01 if in full duplex
300 * 0x00 otherwise
301 */
302static u8 __get_duplex(struct port *port)
303{
304 struct slave *slave = port->slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u8 retval;
306
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100307 /* handling a special case: when the configuration starts with
308 * link down, it sets the duplex to 0.
309 */
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200310 if (slave->link != BOND_LINK_UP) {
Bandan Das128ea6c2010-10-16 20:19:58 +0000311 retval = 0x0;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200312 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 switch (slave->duplex) {
314 case DUPLEX_FULL:
Bandan Das128ea6c2010-10-16 20:19:58 +0000315 retval = 0x1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200316 netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
317 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 case DUPLEX_HALF:
320 default:
Bandan Das128ea6c2010-10-16 20:19:58 +0000321 retval = 0x0;
Veaceslav Falicod4471f52014-07-15 19:36:00 +0200322 netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
323 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 }
326 }
327 return retval;
328}
329
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100330/* Conversions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/**
333 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
334 * @timer_type: which timer to operate
335 * @par: timer parameter. see below
336 *
337 * If @timer_type is %current_while_timer, @par indicates long/short timer.
338 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100339 * %SLOW_PERIODIC_TIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
342{
Bandan Das128ea6c2010-10-16 20:19:58 +0000343 u16 retval = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 switch (timer_type) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100346 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */
Bandan Das7bfc4752010-10-16 20:19:59 +0000347 if (par)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100348 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
Bandan Das7bfc4752010-10-16 20:19:59 +0000349 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100350 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100352 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
354 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100355 case AD_PERIODIC_TIMER: /* for periodic machine */
356 retval = (par*ad_ticks_per_sec); /* long timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100358 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
360 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100361 case AD_WAIT_WHILE_TIMER: /* for selection machine */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
363 break;
364 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return retval;
367}
368
369
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100370/* ================= ad_rx_machine helper functions ================== */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/**
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000373 * __choose_matched - update a port's matched variable from a received lacpdu
374 * @lacpdu: the lacpdu we've received
375 * @port: the port we're looking at
376 *
377 * Update the value of the matched variable, using parameter values from a
378 * newly received lacpdu. Parameter values for the partner carried in the
379 * received PDU are compared with the corresponding operational parameter
380 * values for the actor. Matched is set to TRUE if all of these parameters
381 * match and the PDU parameter partner_state.aggregation has the same value as
382 * actor_oper_port_state.aggregation and lacp will actively maintain the link
383 * in the aggregation. Matched is also set to TRUE if the value of
384 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
385 * an individual link and lacp will actively maintain the link. Otherwise,
386 * matched is set to FALSE. LACP is considered to be actively maintaining the
387 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
388 * the actor's actor_oper_port_state.lacp_activity and the PDU's
389 * partner_state.lacp_activity variables are TRUE.
390 *
391 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
392 * used here to implement the language from 802.3ad 43.4.9 that requires
393 * recordPDU to "match" the LACPDU parameters to the stored values.
394 */
395static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
396{
dingtianhong815117a2014-01-02 09:12:54 +0800397 /* check if all parameters are alike
398 * or this is individual link(aggregation == FALSE)
399 * then update the state machine Matched variable.
400 */
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000401 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
402 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
dingtianhong815117a2014-01-02 09:12:54 +0800403 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000404 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
405 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
406 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000407 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
408 ) {
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000409 port->sm_vars |= AD_PORT_MATCHED;
410 } else {
411 port->sm_vars &= ~AD_PORT_MATCHED;
412 }
413}
414
415/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * __record_pdu - record parameters from a received lacpdu
417 * @lacpdu: the lacpdu we've received
418 * @port: the port we're looking at
419 *
420 * Record the parameter values for the Actor carried in a received lacpdu as
421 * the current partner operational parameter values and sets
422 * actor_oper_port_state.defaulted to FALSE.
423 */
424static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
425{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (lacpdu && port) {
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800427 struct port_params *partner = &port->partner_oper;
428
Jay Vosburgh2d6682d2009-11-13 13:13:01 +0000429 __choose_matched(lacpdu, port);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100430 /* record the new parameter values for the partner
431 * operational
432 */
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800433 partner->port_number = ntohs(lacpdu->actor_port);
434 partner->port_priority = ntohs(lacpdu->actor_port_priority);
435 partner->system = lacpdu->actor_system;
436 partner->system_priority = ntohs(lacpdu->actor_system_priority);
437 partner->key = ntohs(lacpdu->actor_key);
438 partner->port_state = lacpdu->actor_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100440 /* set actor_oper_port_state.defaulted to FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
442
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100443 /* set the partner sync. to on if the partner is sync,
444 * and the port is matched
445 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000446 if ((port->sm_vars & AD_PORT_MATCHED)
447 && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION))
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800448 partner->port_state |= AD_STATE_SYNCHRONIZATION;
Bandan Das7bfc4752010-10-16 20:19:59 +0000449 else
Holger Eitzenbergerb99d6ba2008-12-17 19:08:14 -0800450 partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452}
453
454/**
455 * __record_default - record default parameters
456 * @port: the port we're looking at
457 *
458 * This function records the default parameter values for the partner carried
459 * in the Partner Admin parameters as the current partner operational parameter
460 * values and sets actor_oper_port_state.defaulted to TRUE.
461 */
462static void __record_default(struct port *port)
463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (port) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100465 /* record the partner admin parameters */
Holger Eitzenberger5eefd1a2008-12-17 19:08:46 -0800466 memcpy(&port->partner_oper, &port->partner_admin,
467 sizeof(struct port_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100469 /* set actor_oper_port_state.defaulted to true */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 port->actor_oper_port_state |= AD_STATE_DEFAULTED;
471 }
472}
473
474/**
475 * __update_selected - update a port's Selected variable from a received lacpdu
476 * @lacpdu: the lacpdu we've received
477 * @port: the port we're looking at
478 *
479 * Update the value of the selected variable, using parameter values from a
480 * newly received lacpdu. The parameter values for the Actor carried in the
481 * received PDU are compared with the corresponding operational parameter
482 * values for the ports partner. If one or more of the comparisons shows that
483 * the value(s) received in the PDU differ from the current operational values,
484 * then selected is set to FALSE and actor_oper_port_state.synchronization is
485 * set to out_of_sync. Otherwise, selected remains unchanged.
486 */
487static void __update_selected(struct lacpdu *lacpdu, struct port *port)
488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (lacpdu && port) {
Holger Eitzenbergerce6a49a2008-12-17 19:13:07 -0800490 const struct port_params *partner = &port->partner_oper;
491
dingtianhong815117a2014-01-02 09:12:54 +0800492 /* check if any parameter is different then
493 * update the state machine selected variable.
494 */
Joe Perches8e95a202009-12-03 07:58:21 +0000495 if (ntohs(lacpdu->actor_port) != partner->port_number ||
496 ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800497 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000498 ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
499 ntohs(lacpdu->actor_key) != partner->key ||
500 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 port->sm_vars &= ~AD_PORT_SELECTED;
502 }
503 }
504}
505
506/**
507 * __update_default_selected - update a port's Selected variable from Partner
508 * @port: the port we're looking at
509 *
510 * This function updates the value of the selected variable, using the partner
511 * administrative parameter values. The administrative values are compared with
512 * the corresponding operational parameter values for the partner. If one or
513 * more of the comparisons shows that the administrative value(s) differ from
514 * the current operational values, then Selected is set to FALSE and
515 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
516 * Selected remains unchanged.
517 */
518static void __update_default_selected(struct port *port)
519{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (port) {
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800521 const struct port_params *admin = &port->partner_admin;
522 const struct port_params *oper = &port->partner_oper;
523
dingtianhong815117a2014-01-02 09:12:54 +0800524 /* check if any parameter is different then
525 * update the state machine selected variable.
526 */
Joe Perches8e95a202009-12-03 07:58:21 +0000527 if (admin->port_number != oper->port_number ||
528 admin->port_priority != oper->port_priority ||
dingtianhong815117a2014-01-02 09:12:54 +0800529 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000530 admin->system_priority != oper->system_priority ||
531 admin->key != oper->key ||
532 (admin->port_state & AD_STATE_AGGREGATION)
Holger Eitzenberger3c520652008-12-17 19:13:27 -0800533 != (oper->port_state & AD_STATE_AGGREGATION)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 port->sm_vars &= ~AD_PORT_SELECTED;
535 }
536 }
537}
538
539/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * __update_ntt - update a port's ntt variable from a received lacpdu
541 * @lacpdu: the lacpdu we've received
542 * @port: the port we're looking at
543 *
544 * Updates the value of the ntt variable, using parameter values from a newly
545 * received lacpdu. The parameter values for the partner carried in the
546 * received PDU are compared with the corresponding operational parameter
547 * values for the Actor. If one or more of the comparisons shows that the
548 * value(s) received in the PDU differ from the current operational values,
549 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
550 */
551static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
552{
dingtianhong815117a2014-01-02 09:12:54 +0800553 /* validate lacpdu and port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (lacpdu && port) {
dingtianhong815117a2014-01-02 09:12:54 +0800555 /* check if any parameter is different then
556 * update the port->ntt.
557 */
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700558 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
559 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
dingtianhong815117a2014-01-02 09:12:54 +0800560 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
Jay Vosburgh89cc76f2006-09-22 21:55:32 -0700561 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
562 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
564 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
565 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
566 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
567 ) {
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800568 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 }
571}
572
573/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * __agg_ports_are_ready - check if all ports in an aggregator are ready
575 * @aggregator: the aggregator we're looking at
576 *
577 */
578static int __agg_ports_are_ready(struct aggregator *aggregator)
579{
580 struct port *port;
581 int retval = 1;
582
583 if (aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100584 /* scan all ports in this aggregator to verfy if they are
585 * all ready.
586 */
Bandan Das128ea6c2010-10-16 20:19:58 +0000587 for (port = aggregator->lag_ports;
588 port;
589 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!(port->sm_vars & AD_PORT_READY_N)) {
591 retval = 0;
592 break;
593 }
594 }
595 }
596
597 return retval;
598}
599
600/**
601 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
602 * @aggregator: the aggregator we're looking at
603 * @val: Should the ports' ready bit be set on or off
604 *
605 */
606static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
607{
608 struct port *port;
609
Bandan Das128ea6c2010-10-16 20:19:58 +0000610 for (port = aggregator->lag_ports; port;
611 port = port->next_port_in_aggregator) {
Bandan Das7bfc4752010-10-16 20:19:59 +0000612 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 port->sm_vars |= AD_PORT_READY;
Bandan Das7bfc4752010-10-16 20:19:59 +0000614 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 port->sm_vars &= ~AD_PORT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617}
618
619/**
620 * __get_agg_bandwidth - get the total bandwidth of an aggregator
621 * @aggregator: the aggregator we're looking at
622 *
623 */
624static u32 __get_agg_bandwidth(struct aggregator *aggregator)
625{
Bandan Das128ea6c2010-10-16 20:19:58 +0000626 u32 bandwidth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (aggregator->num_of_ports) {
David Decotigny65cce192011-04-13 15:22:30 +0000629 switch (__get_link_speed(aggregator->lag_ports)) {
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800630 case AD_LINK_SPEED_1MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 bandwidth = aggregator->num_of_ports;
632 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800633 case AD_LINK_SPEED_10MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 bandwidth = aggregator->num_of_ports * 10;
635 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800636 case AD_LINK_SPEED_100MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 bandwidth = aggregator->num_of_ports * 100;
638 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800639 case AD_LINK_SPEED_1000MBPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 bandwidth = aggregator->num_of_ports * 1000;
641 break;
Jianhua Xiecb8dda92014-11-19 16:48:58 +0800642 case AD_LINK_SPEED_10000MBPS:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700643 bandwidth = aggregator->num_of_ports * 10000;
644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 default:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100646 bandwidth = 0; /* to silence the compiler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 }
649 return bandwidth;
650}
651
652/**
653 * __get_active_agg - get the current active aggregator
654 * @aggregator: the aggregator we're looking at
Veaceslav Falico49b76242014-01-10 11:59:45 +0100655 *
656 * Caller must hold RCU lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658static struct aggregator *__get_active_agg(struct aggregator *aggregator)
659{
Veaceslav Falico19177e72013-09-27 16:12:00 +0200660 struct bonding *bond = aggregator->slave->bond;
661 struct list_head *iter;
662 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
dingtianhongbe79bd02013-12-13 10:20:12 +0800664 bond_for_each_slave_rcu(bond, slave, iter)
dingtianhong3fdddd82014-05-12 15:08:43 +0800665 if (SLAVE_AD_INFO(slave)->aggregator.is_active)
666 return &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Veaceslav Falico19177e72013-09-27 16:12:00 +0200668 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/**
672 * __update_lacpdu_from_port - update a port's lacpdu fields
673 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675static inline void __update_lacpdu_from_port(struct port *port)
676{
677 struct lacpdu *lacpdu = &port->lacpdu;
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800678 const struct port_params *partner = &port->partner_oper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100680 /* update current actual Actor parameters
681 * lacpdu->subtype initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * lacpdu->version_number initialized
683 * lacpdu->tlv_type_actor_info initialized
684 * lacpdu->actor_information_length initialized
685 */
686
Al Virod3bb52b2007-08-22 20:06:58 -0400687 lacpdu->actor_system_priority = htons(port->actor_system_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 lacpdu->actor_system = port->actor_system;
Al Virod3bb52b2007-08-22 20:06:58 -0400689 lacpdu->actor_key = htons(port->actor_oper_port_key);
690 lacpdu->actor_port_priority = htons(port->actor_port_priority);
691 lacpdu->actor_port = htons(port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 lacpdu->actor_state = port->actor_oper_port_state;
693
694 /* lacpdu->reserved_3_1 initialized
695 * lacpdu->tlv_type_partner_info initialized
696 * lacpdu->partner_information_length initialized
697 */
698
Holger Eitzenberger3b5b35d2008-12-17 19:13:53 -0800699 lacpdu->partner_system_priority = htons(partner->system_priority);
700 lacpdu->partner_system = partner->system;
701 lacpdu->partner_key = htons(partner->key);
702 lacpdu->partner_port_priority = htons(partner->port_priority);
703 lacpdu->partner_port = htons(partner->port_number);
704 lacpdu->partner_state = partner->port_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* lacpdu->reserved_3_2 initialized
707 * lacpdu->tlv_type_collector_info initialized
708 * lacpdu->collector_information_length initialized
709 * collector_max_delay initialized
710 * reserved_12[12] initialized
711 * tlv_type_terminator initialized
712 * terminator_length initialized
713 * reserved_50[50] initialized
714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100717/* ================= main 802.3ad protocol code ========================= */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719/**
720 * ad_lacpdu_send - send out a lacpdu packet on a given port
721 * @port: the port we're looking at
722 *
723 * Returns: 0 on success
724 * < 0 on error
725 */
726static int ad_lacpdu_send(struct port *port)
727{
728 struct slave *slave = port->slave;
729 struct sk_buff *skb;
730 struct lacpdu_header *lacpdu_header;
731 int length = sizeof(struct lacpdu_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 skb = dev_alloc_skb(length);
Bandan Das7bfc4752010-10-16 20:19:59 +0000734 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700738 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700739 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 skb->protocol = PKT_TYPE_LACPDU;
741 skb->priority = TC_PRIO_CONTROL;
742
743 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
744
Joe Perchesada0f862014-02-15 16:02:17 -0800745 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400746 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100747 * because we use it to identify loopback lacpdus in receive.
748 */
Joe Perchesada0f862014-02-15 16:02:17 -0800749 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800750 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100752 lacpdu_header->lacpdu = port->lacpdu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 dev_queue_xmit(skb);
755
756 return 0;
757}
758
759/**
760 * ad_marker_send - send marker information/response on a given port
761 * @port: the port we're looking at
762 * @marker: marker data to send
763 *
764 * Returns: 0 on success
765 * < 0 on error
766 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700767static int ad_marker_send(struct port *port, struct bond_marker *marker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct slave *slave = port->slave;
770 struct sk_buff *skb;
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700771 struct bond_marker_header *marker_header;
772 int length = sizeof(struct bond_marker_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 skb = dev_alloc_skb(length + 16);
Bandan Das7bfc4752010-10-16 20:19:59 +0000775 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 skb_reserve(skb, 16);
779
780 skb->dev = slave->dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700781 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700782 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 skb->protocol = PKT_TYPE_LACPDU;
784
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -0700785 marker_header = (struct bond_marker_header *)skb_put(skb, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Joe Perchesada0f862014-02-15 16:02:17 -0800787 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400788 /* Note: source address is set to be the member's PERMANENT address,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100789 * because we use it to identify loopback MARKERs in receive.
790 */
Joe Perchesada0f862014-02-15 16:02:17 -0800791 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
Holger Eitzenbergere7271492008-12-26 13:41:53 -0800792 marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100794 marker_header->marker = *marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 dev_queue_xmit(skb);
797
798 return 0;
799}
800
801/**
802 * ad_mux_machine - handle a port's mux state machine
803 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -0700804 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
Mahesh Bandewaree637712014-10-04 17:45:01 -0700806static void ad_mux_machine(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 mux_states_t last_state;
809
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100810 /* keep current State Machine state to compare later if it was
811 * changed
812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 last_state = port->sm_mux_state;
814
815 if (port->sm_vars & AD_PORT_BEGIN) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100816 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 } else {
818 switch (port->sm_mux_state) {
819 case AD_MUX_DETACHED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000820 if ((port->sm_vars & AD_PORT_SELECTED)
821 || (port->sm_vars & AD_PORT_STANDBY))
822 /* if SELECTED or STANDBY */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100823 port->sm_mux_state = AD_MUX_WAITING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 break;
825 case AD_MUX_WAITING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100826 /* if SELECTED == FALSE return to DETACH state */
827 if (!(port->sm_vars & AD_PORT_SELECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100829 /* in order to withhold the Selection Logic to
830 * check all ports READY_N value every callback
831 * cycle to update ready variable, we check
832 * READY_N and update READY here
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100835 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 }
838
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100839 /* check if the wait_while_timer expired */
Bandan Das7bfc4752010-10-16 20:19:59 +0000840 if (port->sm_mux_timer_counter
841 && !(--port->sm_mux_timer_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 port->sm_vars |= AD_PORT_READY_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100844 /* in order to withhold the selection logic to check
845 * all ports READY_N value every callback cycle to
846 * update ready variable, we check READY_N and update
847 * READY here
848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
850
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100851 /* if the wait_while_timer expired, and the port is
852 * in READY state, move to ATTACHED state
853 */
Bandan Das7bfc4752010-10-16 20:19:59 +0000854 if ((port->sm_vars & AD_PORT_READY)
855 && !port->sm_mux_timer_counter)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100856 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 break;
858 case AD_MUX_ATTACHED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100859 /* check also if agg_select_timer expired (so the
860 * edable port will take place only after this timer)
861 */
862 if ((port->sm_vars & AD_PORT_SELECTED) &&
863 (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
864 !__check_agg_selection_timer(port)) {
865 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;
866 } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
867 (port->sm_vars & AD_PORT_STANDBY)) {
868 /* if UNSELECTED or STANDBY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 port->sm_vars &= ~AD_PORT_READY_N;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100870 /* in order to withhold the selection logic to
871 * check all ports READY_N value every callback
872 * cycle to update ready variable, we check
873 * READY_N and update READY here
874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100876 port->sm_mux_state = AD_MUX_DETACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 break;
879 case AD_MUX_COLLECTING_DISTRIBUTING:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100880 if (!(port->sm_vars & AD_PORT_SELECTED) ||
881 (port->sm_vars & AD_PORT_STANDBY) ||
882 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION)) {
883 port->sm_mux_state = AD_MUX_ATTACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100885 /* if port state hasn't changed make
886 * sure that a collecting distributing
887 * port in an active aggregator is enabled
888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (port->aggregator &&
890 port->aggregator->is_active &&
891 !__port_is_enabled(port)) {
892
893 __enable_port(port);
894 }
895 }
896 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100897 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 }
900 }
901
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100902 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (port->sm_mux_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800904 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
905 port->actor_port_number, last_state,
906 port->sm_mux_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 switch (port->sm_mux_state) {
908 case AD_MUX_DETACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700910 ad_disable_collecting_distributing(port,
911 update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
913 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800914 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916 case AD_MUX_WAITING:
917 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
918 break;
919 case AD_MUX_ATTACHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
921 port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
922 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700923 ad_disable_collecting_distributing(port,
924 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800925 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
927 case AD_MUX_COLLECTING_DISTRIBUTING:
928 port->actor_oper_port_state |= AD_STATE_COLLECTING;
929 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
Mahesh Bandewaree637712014-10-04 17:45:01 -0700930 ad_enable_collecting_distributing(port,
931 update_slave_arr);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -0800932 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100934 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936 }
937 }
938}
939
940/**
941 * ad_rx_machine - handle a port's rx State Machine
942 * @lacpdu: the lacpdu we've received
943 * @port: the port we're looking at
944 *
945 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
946 * CURRENT. If timer expired set the state machine in the proper state.
947 * In other cases, this function checks if we need to switch to other state.
948 */
949static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
950{
951 rx_states_t last_state;
952
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100953 /* keep current State Machine state to compare later if it was
954 * changed
955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 last_state = port->sm_rx_state;
957
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100958 /* check if state machine should change state */
959
960 /* first, check if port was reinitialized */
Bandan Das7bfc4752010-10-16 20:19:59 +0000961 if (port->sm_vars & AD_PORT_BEGIN)
Bandan Das7bfc4752010-10-16 20:19:59 +0000962 port->sm_rx_state = AD_RX_INITIALIZE;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100963 /* check if port is not enabled */
Bandan Das7bfc4752010-10-16 20:19:59 +0000964 else if (!(port->sm_vars & AD_PORT_BEGIN)
965 && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED))
Bandan Das7bfc4752010-10-16 20:19:59 +0000966 port->sm_rx_state = AD_RX_PORT_DISABLED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100967 /* check if new lacpdu arrived */
968 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
969 (port->sm_rx_state == AD_RX_DEFAULTED) ||
970 (port->sm_rx_state == AD_RX_CURRENT))) {
971 port->sm_rx_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 port->sm_rx_state = AD_RX_CURRENT;
973 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100974 /* if timer is on, and if it is expired */
975 if (port->sm_rx_timer_counter &&
976 !(--port->sm_rx_timer_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 switch (port->sm_rx_state) {
978 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100979 port->sm_rx_state = AD_RX_DEFAULTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981 case AD_RX_CURRENT:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100982 port->sm_rx_state = AD_RX_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100984 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986 }
987 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100988 /* if no lacpdu arrived and no timer is on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 switch (port->sm_rx_state) {
990 case AD_RX_PORT_DISABLED:
Bandan Das7bfc4752010-10-16 20:19:59 +0000991 if (port->sm_vars & AD_PORT_MOVED)
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100992 port->sm_rx_state = AD_RX_INITIALIZE;
Bandan Das7bfc4752010-10-16 20:19:59 +0000993 else if (port->is_enabled
994 && (port->sm_vars
995 & AD_PORT_LACP_ENABLED))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +0100996 port->sm_rx_state = AD_RX_EXPIRED;
Bandan Das7bfc4752010-10-16 20:19:59 +0000997 else if (port->is_enabled
998 && ((port->sm_vars
999 & AD_PORT_LACP_ENABLED) == 0))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001000 port->sm_rx_state = AD_RX_LACP_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001002 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004
1005 }
1006 }
1007 }
1008
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001009 /* check if the State machine was changed or new lacpdu arrived */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if ((port->sm_rx_state != last_state) || (lacpdu)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001011 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1012 port->actor_port_number, last_state,
1013 port->sm_rx_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 switch (port->sm_rx_state) {
1015 case AD_RX_INITIALIZE:
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001016 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
Bandan Das7bfc4752010-10-16 20:19:59 +00001018 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 port->sm_vars |= AD_PORT_LACP_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 port->sm_vars &= ~AD_PORT_SELECTED;
1021 __record_default(port);
1022 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1023 port->sm_vars &= ~AD_PORT_MOVED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001024 port->sm_rx_state = AD_RX_PORT_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001026 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 case AD_RX_PORT_DISABLED:
1028 port->sm_vars &= ~AD_PORT_MATCHED;
1029 break;
1030 case AD_RX_LACP_DISABLED:
1031 port->sm_vars &= ~AD_PORT_SELECTED;
1032 __record_default(port);
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001033 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 port->sm_vars |= AD_PORT_MATCHED;
1035 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1036 break;
1037 case AD_RX_EXPIRED:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001038 /* Reset of the Synchronization flag (Standard 43.4.12)
1039 * This reset cause to disable this port in the
1040 * COLLECTING_DISTRIBUTING state of the mux machine in
1041 * case of EXPIRED even if LINK_DOWN didn't arrive for
1042 * the port.
1043 */
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001044 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 port->sm_vars &= ~AD_PORT_MATCHED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001046 port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1048 port->actor_oper_port_state |= AD_STATE_EXPIRED;
1049 break;
1050 case AD_RX_DEFAULTED:
1051 __update_default_selected(port);
1052 __record_default(port);
1053 port->sm_vars |= AD_PORT_MATCHED;
1054 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1055 break;
1056 case AD_RX_CURRENT:
dingtianhong815117a2014-01-02 09:12:54 +08001057 /* detect loopback situation */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001058 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1059 &(port->actor_system))) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001060 netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
Joe Perches90194262014-02-15 16:01:45 -08001061 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001062 port->slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return;
1064 }
1065 __update_selected(lacpdu, port);
1066 __update_ntt(lacpdu, port);
1067 __record_pdu(lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1069 port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001071 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073 }
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077/**
1078 * ad_tx_machine - handle a port's tx state machine
1079 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
1081static void ad_tx_machine(struct port *port)
1082{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001083 /* check if tx timer expired, to verify that we do not send more than
1084 * 3 packets per second
1085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001087 /* check if there is something to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1089 __update_lacpdu_from_port(port);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (ad_lacpdu_send(port) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001092 pr_debug("Sent LACPDU on port %d\n",
1093 port->actor_port_number);
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001094
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001095 /* mark ntt as false, so it will not be sent
1096 * again until demanded
1097 */
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001098 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001101 /* restart tx timer(to verify that we will not exceed
1102 * AD_MAX_TX_IN_SECOND
1103 */
1104 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
1108/**
1109 * ad_periodic_machine - handle a port's periodic state machine
1110 * @port: the port we're looking at
1111 *
1112 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1113 */
1114static void ad_periodic_machine(struct port *port)
1115{
1116 periodic_states_t last_state;
1117
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001118 /* keep current state machine state to compare later if it was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 last_state = port->sm_periodic_state;
1120
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001121 /* check if port was reinitialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001123 (!(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 -07001124 ) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001125 port->sm_periodic_state = AD_NO_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001127 /* check if state machine should change state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 else if (port->sm_periodic_timer_counter) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001129 /* check if periodic state machine expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!(--port->sm_periodic_timer_counter)) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001131 /* if expired then do tx */
1132 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 } else {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001134 /* If not expired, check if there is some new timeout
1135 * parameter from the partner state
1136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 switch (port->sm_periodic_state) {
1138 case AD_FAST_PERIODIC:
Bandan Das7bfc4752010-10-16 20:19:59 +00001139 if (!(port->partner_oper.port_state
1140 & AD_STATE_LACP_TIMEOUT))
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001141 port->sm_periodic_state = AD_SLOW_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
1143 case AD_SLOW_PERIODIC:
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001144 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 port->sm_periodic_timer_counter = 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001146 port->sm_periodic_state = AD_PERIODIC_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001149 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 }
1152 }
1153 } else {
1154 switch (port->sm_periodic_state) {
1155 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001156 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 case AD_PERIODIC_TX:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001159 if (!(port->partner_oper.port_state &
1160 AD_STATE_LACP_TIMEOUT))
1161 port->sm_periodic_state = AD_SLOW_PERIODIC;
Bandan Das7bfc4752010-10-16 20:19:59 +00001162 else
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001163 port->sm_periodic_state = AD_FAST_PERIODIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001165 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167 }
1168 }
1169
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001170 /* check if the state machine was changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (port->sm_periodic_state != last_state) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001172 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1173 port->actor_port_number, last_state,
1174 port->sm_periodic_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 switch (port->sm_periodic_state) {
1176 case AD_NO_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001177 port->sm_periodic_timer_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179 case AD_FAST_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001180 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1181 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 -07001182 break;
1183 case AD_SLOW_PERIODIC:
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001184 /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1185 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 -07001186 break;
1187 case AD_PERIODIC_TX:
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001188 port->ntt = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001190 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
1192 }
1193 }
1194}
1195
1196/**
1197 * ad_port_selection_logic - select aggregation groups
1198 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001199 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 *
1201 * Select aggregation groups, and assign each port for it's aggregetor. The
1202 * selection logic is called in the inititalization (after all the handshkes),
1203 * and after every lacpdu receive (if selected is off).
1204 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001205static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1208 struct port *last_port = NULL, *curr_port;
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001209 struct list_head *iter;
1210 struct bonding *bond;
1211 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int found = 0;
1213
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001214 /* if the port is already Selected, do nothing */
Bandan Das7bfc4752010-10-16 20:19:59 +00001215 if (port->sm_vars & AD_PORT_SELECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001218 bond = __get_bond_by_port(port);
1219
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001220 /* if the port is connected to other aggregator, detach it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (port->aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001222 /* detach the port from its former aggregator */
Bandan Das128ea6c2010-10-16 20:19:58 +00001223 temp_aggregator = port->aggregator;
1224 for (curr_port = temp_aggregator->lag_ports; curr_port;
1225 last_port = curr_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001226 curr_port = curr_port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (curr_port == port) {
1228 temp_aggregator->num_of_ports--;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001229 /* if it is the first port attached to the
1230 * aggregator
1231 */
1232 if (!last_port) {
Bandan Das128ea6c2010-10-16 20:19:58 +00001233 temp_aggregator->lag_ports =
1234 port->next_port_in_aggregator;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001235 } else {
1236 /* not the first port attached to the
1237 * aggregator
1238 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001239 last_port->next_port_in_aggregator =
1240 port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001243 /* clear the port's relations to this
1244 * aggregator
1245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 port->aggregator = NULL;
Bandan Das128ea6c2010-10-16 20:19:58 +00001247 port->next_port_in_aggregator = NULL;
1248 port->actor_port_aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001250 netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1251 port->actor_port_number,
1252 temp_aggregator->aggregator_identifier);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001253 /* if the aggregator is empty, clear its
1254 * parameters, and set it ready to be attached
1255 */
Bandan Das7bfc4752010-10-16 20:19:59 +00001256 if (!temp_aggregator->lag_ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ad_clear_agg(temp_aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
1259 }
1260 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001261 if (!curr_port) {
1262 /* meaning: the port was related to an aggregator
1263 * but was not on the aggregator port list
1264 */
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001265 net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1266 port->slave->bond->dev->name,
1267 port->actor_port_number,
1268 port->slave->dev->name,
1269 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001272 /* search on all aggregators for a suitable aggregator for this port */
Veaceslav Falico3e36bb72013-09-27 16:11:59 +02001273 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001274 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001276 /* keep a free aggregator for later use(if needed) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (!aggregator->lag_ports) {
Bandan Das7bfc4752010-10-16 20:19:59 +00001278 if (!free_aggregator)
Bandan Das128ea6c2010-10-16 20:19:58 +00001279 free_aggregator = aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 continue;
1281 }
dingtianhong815117a2014-01-02 09:12:54 +08001282 /* check if current aggregator suits us */
1283 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1284 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001285 (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1286 (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 ) &&
dingtianhong815117a2014-01-02 09:12:54 +08001288 ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1289 !aggregator->is_individual) /* but is not individual OR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 )
1291 ) {
dingtianhong815117a2014-01-02 09:12:54 +08001292 /* attach to the founded aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 port->aggregator = aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001294 port->actor_port_aggregator_identifier =
1295 port->aggregator->aggregator_identifier;
1296 port->next_port_in_aggregator = aggregator->lag_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 port->aggregator->num_of_ports++;
Bandan Das128ea6c2010-10-16 20:19:58 +00001298 aggregator->lag_ports = port;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001299 netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1300 port->actor_port_number,
1301 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001303 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 port->sm_vars |= AD_PORT_SELECTED;
1305 found = 1;
1306 break;
1307 }
1308 }
1309
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001310 /* the port couldn't find an aggregator - attach it to a new
1311 * aggregator
1312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!found) {
1314 if (free_aggregator) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001315 /* assign port a new aggregator */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 port->aggregator = free_aggregator;
Bandan Das128ea6c2010-10-16 20:19:58 +00001317 port->actor_port_aggregator_identifier =
1318 port->aggregator->aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001320 /* update the new aggregator's parameters
1321 * if port was responsed from the end-user
1322 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001323 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
Bandan Das7bfc4752010-10-16 20:19:59 +00001324 /* if port is full duplex */
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001325 port->aggregator->is_individual = false;
Bandan Das7bfc4752010-10-16 20:19:59 +00001326 else
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001327 port->aggregator->is_individual = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
1330 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
Bandan Das128ea6c2010-10-16 20:19:58 +00001331 port->aggregator->partner_system =
1332 port->partner_oper.system;
1333 port->aggregator->partner_system_priority =
1334 port->partner_oper.system_priority;
Holger Eitzenberger1055c9a2008-12-17 19:07:38 -08001335 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 port->aggregator->receive_state = 1;
1337 port->aggregator->transmit_state = 1;
1338 port->aggregator->lag_ports = port;
1339 port->aggregator->num_of_ports++;
1340
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001341 /* mark this port as selected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 port->sm_vars |= AD_PORT_SELECTED;
1343
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001344 netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1345 port->actor_port_number,
1346 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001348 netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 port->actor_port_number, port->slave->dev->name);
1350 }
1351 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001352 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1353 * in all aggregator's ports, else set ready=FALSE in all
1354 * aggregator's ports
1355 */
1356 __set_agg_ports_ready(port->aggregator,
1357 __agg_ports_are_ready(port->aggregator));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Jay Vosburghfd989c82008-11-04 17:51:16 -08001359 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001360 ad_agg_selection_logic(aggregator, update_slave_arr);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001361}
1362
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001363/* Decide if "agg" is a better choice for the new active aggregator that
Jay Vosburghfd989c82008-11-04 17:51:16 -08001364 * the current best, according to the ad_select policy.
1365 */
1366static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1367 struct aggregator *curr)
1368{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001369 /* 0. If no best, select current.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001370 *
1371 * 1. If the current agg is not individual, and the best is
1372 * individual, select current.
1373 *
1374 * 2. If current agg is individual and the best is not, keep best.
1375 *
1376 * 3. Therefore, current and best are both individual or both not
1377 * individual, so:
1378 *
1379 * 3a. If current agg partner replied, and best agg partner did not,
1380 * select current.
1381 *
1382 * 3b. If current agg partner did not reply and best agg partner
1383 * did reply, keep best.
1384 *
1385 * 4. Therefore, current and best both have partner replies or
1386 * both do not, so perform selection policy:
1387 *
1388 * BOND_AD_COUNT: Select by count of ports. If count is equal,
1389 * select by bandwidth.
1390 *
1391 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1392 */
1393 if (!best)
1394 return curr;
1395
1396 if (!curr->is_individual && best->is_individual)
1397 return curr;
1398
1399 if (curr->is_individual && !best->is_individual)
1400 return best;
1401
1402 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1403 return curr;
1404
1405 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1406 return best;
1407
1408 switch (__get_agg_selection_mode(curr->lag_ports)) {
1409 case BOND_AD_COUNT:
1410 if (curr->num_of_ports > best->num_of_ports)
1411 return curr;
1412
1413 if (curr->num_of_ports < best->num_of_ports)
1414 return best;
1415
1416 /*FALLTHROUGH*/
1417 case BOND_AD_STABLE:
1418 case BOND_AD_BANDWIDTH:
1419 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1420 return curr;
1421
1422 break;
1423
1424 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001425 net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1426 curr->slave->bond->dev->name,
1427 __get_agg_selection_mode(curr->lag_ports));
Jay Vosburghfd989c82008-11-04 17:51:16 -08001428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001430
1431 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001434static int agg_device_up(const struct aggregator *agg)
1435{
Jiri Bohac2430af82011-04-19 02:09:55 +00001436 struct port *port = agg->lag_ports;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001437
Jiri Bohac2430af82011-04-19 02:09:55 +00001438 if (!port)
1439 return 0;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001440
1441 return netif_running(port->slave->dev) &&
1442 netif_carrier_ok(port->slave->dev);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445/**
1446 * ad_agg_selection_logic - select an aggregation group for a team
1447 * @aggregator: the aggregator we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001448 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 *
1450 * It is assumed that only one aggregator may be selected for a team.
Jay Vosburghfd989c82008-11-04 17:51:16 -08001451 *
1452 * The logic of this function is to select the aggregator according to
1453 * the ad_select policy:
1454 *
1455 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1456 * it, and to reselect the active aggregator only if the previous
1457 * aggregator has no more ports related to it.
1458 *
1459 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1460 * bandwidth, and reselect whenever a link state change takes place or the
1461 * set of slaves in the bond changes.
1462 *
1463 * BOND_AD_COUNT: select the aggregator with largest number of ports
1464 * (slaves), and reselect whenever a link state change takes place or the
1465 * set of slaves in the bond changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
1467 * FIXME: this function MUST be called with the first agg in the bond, or
1468 * __get_active_agg() won't work correctly. This function should be better
1469 * called with the bond itself, and retrieve the first agg from it.
1470 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001471static void ad_agg_selection_logic(struct aggregator *agg,
1472 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Jay Vosburghfd989c82008-11-04 17:51:16 -08001474 struct aggregator *best, *active, *origin;
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001475 struct bonding *bond = agg->slave->bond;
1476 struct list_head *iter;
1477 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Veaceslav Falico49b76242014-01-10 11:59:45 +01001480 rcu_read_lock();
Jay Vosburghfd989c82008-11-04 17:51:16 -08001481 origin = agg;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001482 active = __get_active_agg(agg);
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001483 best = (active && agg_device_up(active)) ? active : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
dingtianhongbe79bd02013-12-13 10:20:12 +08001485 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001486 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001487
Jay Vosburghfd989c82008-11-04 17:51:16 -08001488 agg->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Stephen Hemminger4cd6fe12009-05-15 08:44:32 +00001490 if (agg->num_of_ports && agg_device_up(agg))
Jay Vosburghfd989c82008-11-04 17:51:16 -08001491 best = ad_agg_selection_test(best, agg);
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Jay Vosburghfd989c82008-11-04 17:51:16 -08001494 if (best &&
1495 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001496 /* For the STABLE policy, don't replace the old active
Jay Vosburghfd989c82008-11-04 17:51:16 -08001497 * aggregator if it's still active (it has an answering
1498 * partner) or if both the best and active don't have an
1499 * answering partner.
1500 */
1501 if (active && active->lag_ports &&
1502 active->lag_ports->is_enabled &&
1503 (__agg_has_partner(active) ||
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001504 (!__agg_has_partner(active) &&
1505 !__agg_has_partner(best)))) {
Jay Vosburghfd989c82008-11-04 17:51:16 -08001506 if (!(!active->actor_oper_aggregator_key &&
1507 best->actor_oper_aggregator_key)) {
1508 best = NULL;
1509 active->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511 }
1512 }
1513
Jay Vosburghfd989c82008-11-04 17:51:16 -08001514 if (best && (best == active)) {
1515 best = NULL;
1516 active->is_active = 1;
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
dingtianhongbe79bd02013-12-13 10:20:12 +08001519 /* if there is new best aggregator, activate it */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001520 if (best) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001521 netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1522 best->aggregator_identifier, best->num_of_ports,
1523 best->actor_oper_aggregator_key,
1524 best->partner_oper_aggregator_key,
1525 best->is_individual, best->is_active);
1526 netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1527 best->lag_ports, best->slave,
1528 best->slave ? best->slave->dev->name : "NULL");
Jay Vosburghfd989c82008-11-04 17:51:16 -08001529
dingtianhongbe79bd02013-12-13 10:20:12 +08001530 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001531 agg = &(SLAVE_AD_INFO(slave)->aggregator);
Jay Vosburghfd989c82008-11-04 17:51:16 -08001532
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001533 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1534 agg->aggregator_identifier, agg->num_of_ports,
1535 agg->actor_oper_aggregator_key,
1536 agg->partner_oper_aggregator_key,
1537 agg->is_individual, agg->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
dingtianhongbe79bd02013-12-13 10:20:12 +08001540 /* check if any partner replys */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001541 if (best->is_individual) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001542 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1543 best->slave ?
1544 best->slave->bond->dev->name : "NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
Jay Vosburghfd989c82008-11-04 17:51:16 -08001547 best->is_active = 1;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001548 netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1549 best->aggregator_identifier);
1550 netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1551 best->aggregator_identifier, best->num_of_ports,
1552 best->actor_oper_aggregator_key,
1553 best->partner_oper_aggregator_key,
1554 best->is_individual, best->is_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001556 /* disable the ports that were related to the former
1557 * active_aggregator
1558 */
Jay Vosburghfd989c82008-11-04 17:51:16 -08001559 if (active) {
1560 for (port = active->lag_ports; port;
1561 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 __disable_port(port);
1563 }
1564 }
Mahesh Bandewaree637712014-10-04 17:45:01 -07001565 /* Slave array needs update. */
1566 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001569 /* if the selected aggregator is of join individuals
Jay Vosburghfd989c82008-11-04 17:51:16 -08001570 * (partner_system is NULL), enable their ports
1571 */
1572 active = __get_active_agg(origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Jay Vosburghfd989c82008-11-04 17:51:16 -08001574 if (active) {
1575 if (!__agg_has_partner(active)) {
1576 for (port = active->lag_ports; port;
1577 port = port->next_port_in_aggregator) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 __enable_port(port);
1579 }
1580 }
1581 }
Jay Vosburghfd989c82008-11-04 17:51:16 -08001582
dingtianhongbe79bd02013-12-13 10:20:12 +08001583 rcu_read_unlock();
1584
Veaceslav Falicobef1fcc2013-09-27 16:12:01 +02001585 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588/**
1589 * ad_clear_agg - clear a given aggregator's parameters
1590 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 */
1592static void ad_clear_agg(struct aggregator *aggregator)
1593{
1594 if (aggregator) {
Holger Eitzenberger1624db72008-12-26 13:27:21 -08001595 aggregator->is_individual = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 aggregator->actor_admin_aggregator_key = 0;
1597 aggregator->actor_oper_aggregator_key = 0;
1598 aggregator->partner_system = null_mac_addr;
1599 aggregator->partner_system_priority = 0;
1600 aggregator->partner_oper_aggregator_key = 0;
1601 aggregator->receive_state = 0;
1602 aggregator->transmit_state = 0;
1603 aggregator->lag_ports = NULL;
1604 aggregator->is_active = 0;
1605 aggregator->num_of_ports = 0;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001606 pr_debug("LAG %d was cleared\n",
1607 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609}
1610
1611/**
1612 * ad_initialize_agg - initialize a given aggregator's parameters
1613 * @aggregator: the aggregator we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615static void ad_initialize_agg(struct aggregator *aggregator)
1616{
1617 if (aggregator) {
1618 ad_clear_agg(aggregator);
1619
1620 aggregator->aggregator_mac_address = null_mac_addr;
1621 aggregator->aggregator_identifier = 0;
1622 aggregator->slave = NULL;
1623 }
1624}
1625
1626/**
1627 * ad_initialize_port - initialize a given port's parameters
1628 * @aggregator: the aggregator we're looking at
1629 * @lacp_fast: boolean. whether fast periodic should be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
1631static void ad_initialize_port(struct port *port, int lacp_fast)
1632{
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001633 static const struct port_params tmpl = {
1634 .system_priority = 0xffff,
1635 .key = 1,
1636 .port_number = 1,
1637 .port_priority = 0xff,
1638 .port_state = 1,
1639 };
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001640 static const struct lacpdu lacpdu = {
1641 .subtype = 0x01,
1642 .version_number = 0x01,
1643 .tlv_type_actor_info = 0x01,
1644 .actor_information_length = 0x14,
1645 .tlv_type_partner_info = 0x02,
1646 .partner_information_length = 0x14,
1647 .tlv_type_collector_info = 0x03,
1648 .collector_information_length = 0x10,
1649 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1650 };
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (port) {
1653 port->actor_port_number = 1;
1654 port->actor_port_priority = 0xff;
1655 port->actor_system = null_mac_addr;
1656 port->actor_system_priority = 0xffff;
1657 port->actor_port_aggregator_identifier = 0;
Holger Eitzenbergerd238d452008-12-26 11:18:15 -08001658 port->ntt = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 port->actor_admin_port_key = 1;
1660 port->actor_oper_port_key = 1;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001661 port->actor_admin_port_state = AD_STATE_AGGREGATION |
1662 AD_STATE_LACP_ACTIVITY;
1663 port->actor_oper_port_state = AD_STATE_AGGREGATION |
1664 AD_STATE_LACP_ACTIVITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Bandan Das7bfc4752010-10-16 20:19:59 +00001666 if (lacp_fast)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Holger Eitzenbergerc7e703d2008-12-17 19:12:07 -08001669 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1670 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1671
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08001672 port->is_enabled = true;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001673 /* private parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 port->sm_vars = 0x3;
1675 port->sm_rx_state = 0;
1676 port->sm_rx_timer_counter = 0;
1677 port->sm_periodic_state = 0;
1678 port->sm_periodic_timer_counter = 0;
1679 port->sm_mux_state = 0;
1680 port->sm_mux_timer_counter = 0;
1681 port->sm_tx_state = 0;
1682 port->sm_tx_timer_counter = 0;
1683 port->slave = NULL;
1684 port->aggregator = NULL;
1685 port->next_port_in_aggregator = NULL;
1686 port->transaction_id = 0;
1687
Holger Eitzenberger7addeef2008-12-26 13:28:33 -08001688 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690}
1691
1692/**
1693 * ad_enable_collecting_distributing - enable a port's transmit/receive
1694 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001695 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 *
1697 * Enable @port if it's in an active aggregator
1698 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001699static void ad_enable_collecting_distributing(struct port *port,
1700 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 if (port->aggregator->is_active) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001703 pr_debug("Enabling port %d(LAG %d)\n",
1704 port->actor_port_number,
1705 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 __enable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001707 /* Slave array needs update */
1708 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710}
1711
1712/**
1713 * ad_disable_collecting_distributing - disable a port's transmit/receive
1714 * @port: the port we're looking at
Mahesh Bandewaree637712014-10-04 17:45:01 -07001715 * @update_slave_arr: Does slave array need update?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07001717static void ad_disable_collecting_distributing(struct port *port,
1718 bool *update_slave_arr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001720 if (port->aggregator &&
1721 !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1722 &(null_mac_addr))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001723 pr_debug("Disabling port %d(LAG %d)\n",
1724 port->actor_port_number,
1725 port->aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 __disable_port(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07001727 /* Slave array needs an update */
1728 *update_slave_arr = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/**
1733 * ad_marker_info_received - handle receive of a Marker information frame
1734 * @marker_info: Marker info received
1735 * @port: the port we're looking at
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001737static void ad_marker_info_received(struct bond_marker *marker_info,
1738 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001740 struct bond_marker marker;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001742 /* copy the received marker data to the response marker */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001743 memcpy(&marker, marker_info, sizeof(struct bond_marker));
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001744 /* change the marker subtype to marker response */
Bandan Das128ea6c2010-10-16 20:19:58 +00001745 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001747 /* send the marker response */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (ad_marker_send(port, &marker) >= 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001749 pr_debug("Sent Marker Response on port %d\n",
1750 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752}
1753
1754/**
1755 * ad_marker_response_received - handle receive of a marker response frame
1756 * @marker: marker PDU received
1757 * @port: the port we're looking at
1758 *
1759 * This function does nothing since we decided not to implement send and handle
1760 * response for marker PDU's, in this stage, but only to respond to marker
1761 * information.
1762 */
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07001763static void ad_marker_response_received(struct bond_marker *marker,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001764 struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001766 marker = NULL;
1767 port = NULL;
1768 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001771/* ========= AD exported functions to the main bonding code ========= */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001773/* Check aggregators status in team every T seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774#define AD_AGGREGATOR_SELECTION_TIMER 8
1775
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001776/**
1777 * bond_3ad_initiate_agg_selection - initate aggregator selection
1778 * @bond: bonding struct
Jay Vosburghfd989c82008-11-04 17:51:16 -08001779 *
1780 * Set the aggregation selection timer, to initiate an agg selection in
1781 * the very near future. Called during first initialization, and during
1782 * any down to up transitions of the bond.
1783 */
1784void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1785{
1786 BOND_AD_INFO(bond).agg_select_timer = timeout;
Jay Vosburghfd989c82008-11-04 17:51:16 -08001787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/**
1790 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1791 * @bond: bonding struct to work on
1792 * @tick_resolution: tick duration (millisecond resolution)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 *
1794 * Can be called only after the mac address of the bond is set.
1795 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001796void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001797{
dingtianhong815117a2014-01-02 09:12:54 +08001798 /* check that the bond is not initialized yet */
1799 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001800 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001802 BOND_AD_INFO(bond).aggregator_identifier = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
1805 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
1806
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001807 /* initialize how many times this module is called in one
1808 * second (should be about every 100ms)
1809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 ad_ticks_per_sec = tick_resolution;
1811
Jay Vosburghfd989c82008-11-04 17:51:16 -08001812 bond_3ad_initiate_agg_selection(bond,
1813 AD_AGGREGATOR_SELECTION_TIMER *
1814 ad_ticks_per_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816}
1817
1818/**
1819 * bond_3ad_bind_slave - initialize a slave's port
1820 * @slave: slave struct to work on
1821 *
1822 * Returns: 0 on success
1823 * < 0 on error
1824 */
dingtianhong359632e2014-01-02 09:13:12 +08001825void bond_3ad_bind_slave(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct bonding *bond = bond_get_bond_by_slave(slave);
1828 struct port *port;
1829 struct aggregator *aggregator;
1830
dingtianhong359632e2014-01-02 09:13:12 +08001831 /* check that the slave has not been initialized yet. */
dingtianhong3fdddd82014-05-12 15:08:43 +08001832 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
dingtianhong359632e2014-01-02 09:13:12 +08001834 /* port initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001835 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Peter Pan(潘卫平)bf0239a2011-06-13 04:30:10 +00001837 ad_initialize_port(port, bond->params.lacp_fast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 port->slave = slave;
dingtianhong3fdddd82014-05-12 15:08:43 +08001840 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
dingtianhong359632e2014-01-02 09:13:12 +08001841 /* key is determined according to the link speed, duplex and user key(which
1842 * is yet not supported)
dingtianhong359632e2014-01-02 09:13:12 +08001843 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001844 port->actor_admin_port_key = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 port->actor_admin_port_key |= __get_duplex(port);
1846 port->actor_admin_port_key |= (__get_link_speed(port) << 1);
1847 port->actor_oper_port_key = port->actor_admin_port_key;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001848 /* if the port is not full duplex, then the port should be not
1849 * lacp Enabled
1850 */
Jianhua Xiecb8dda92014-11-19 16:48:58 +08001851 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
dingtianhong359632e2014-01-02 09:13:12 +08001853 /* actor system is the bond's system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001855 /* tx timer(to verify that no more than MAX_TX_IN_SECOND
1856 * lacpdu's are sent in one second)
1857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1859 port->aggregator = NULL;
1860 port->next_port_in_aggregator = NULL;
1861
1862 __disable_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
dingtianhong359632e2014-01-02 09:13:12 +08001864 /* aggregator initialization */
dingtianhong3fdddd82014-05-12 15:08:43 +08001865 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 ad_initialize_agg(aggregator);
1868
1869 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
Jiri Bohac163c8ff2014-02-14 18:13:50 +01001870 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 aggregator->slave = slave;
1872 aggregator->is_active = 0;
1873 aggregator->num_of_ports = 0;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877/**
1878 * bond_3ad_unbind_slave - deinitialize a slave's port
1879 * @slave: slave struct to work on
1880 *
1881 * Search for the aggregator that is related to this port, remove the
1882 * aggregator and assign another aggregator for other port related to it
1883 * (if any), and remove the port.
1884 */
1885void bond_3ad_unbind_slave(struct slave *slave)
1886{
1887 struct port *port, *prev_port, *temp_port;
1888 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1889 int select_new_active_agg = 0;
Veaceslav Falico0b0882642013-09-27 16:12:02 +02001890 struct bonding *bond = slave->bond;
1891 struct slave *slave_iter;
1892 struct list_head *iter;
Mahesh Bandewaree637712014-10-04 17:45:01 -07001893 bool dummy_slave_update; /* Ignore this value as caller updates array */
Jasper Spaansa361c832009-10-23 04:09:24 +00001894
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001895 /* Sync against bond_3ad_state_machine_handler() */
1896 spin_lock_bh(&bond->mode_lock);
dingtianhong3fdddd82014-05-12 15:08:43 +08001897 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1898 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001900 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001902 netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
1903 slave->dev->name);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02001904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001907 netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
1908 aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /* Tell the partner that this port is not suitable for aggregation */
1911 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
1912 __update_lacpdu_from_port(port);
1913 ad_lacpdu_send(port);
1914
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001915 /* check if this aggregator is occupied */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (aggregator->lag_ports) {
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001917 /* check if there are other ports related to this aggregator
1918 * except the port related to this slave(thats ensure us that
1919 * there is a reason to search for new aggregator, and that we
1920 * will find one
1921 */
1922 if ((aggregator->lag_ports != port) ||
1923 (aggregator->lag_ports->next_port_in_aggregator)) {
1924 /* find new aggregator for the related port(s) */
Veaceslav Falico0b0882642013-09-27 16:12:02 +02001925 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001926 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001927 /* if the new aggregator is empty, or it is
1928 * connected to our port only
1929 */
1930 if (!new_aggregator->lag_ports ||
1931 ((new_aggregator->lag_ports == port) &&
1932 !new_aggregator->lag_ports->next_port_in_aggregator))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Veaceslav Falico0b0882642013-09-27 16:12:02 +02001935 if (!slave_iter)
1936 new_aggregator = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001937
1938 /* if new aggregator found, copy the aggregator's
1939 * parameters and connect the related lag_ports to the
1940 * new aggregator
1941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 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 +02001943 netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
1944 aggregator->aggregator_identifier,
1945 new_aggregator->aggregator_identifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001947 if ((new_aggregator->lag_ports == port) &&
1948 new_aggregator->is_active) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001949 netdev_info(bond->dev, "Removing an active aggregator\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 select_new_active_agg = 1;
1951 }
1952
1953 new_aggregator->is_individual = aggregator->is_individual;
1954 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
1955 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
1956 new_aggregator->partner_system = aggregator->partner_system;
1957 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
1958 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
1959 new_aggregator->receive_state = aggregator->receive_state;
1960 new_aggregator->transmit_state = aggregator->transmit_state;
1961 new_aggregator->lag_ports = aggregator->lag_ports;
1962 new_aggregator->is_active = aggregator->is_active;
1963 new_aggregator->num_of_ports = aggregator->num_of_ports;
1964
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001965 /* update the information that is written on
1966 * the ports about the aggregator
1967 */
Bandan Das128ea6c2010-10-16 20:19:58 +00001968 for (temp_port = aggregator->lag_ports; temp_port;
1969 temp_port = temp_port->next_port_in_aggregator) {
1970 temp_port->aggregator = new_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
1972 }
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 ad_clear_agg(aggregator);
Jasper Spaansa361c832009-10-23 04:09:24 +00001975
Bandan Das7bfc4752010-10-16 20:19:59 +00001976 if (select_new_active_agg)
Mahesh Bandewaree637712014-10-04 17:45:01 -07001977 ad_agg_selection_logic(__get_first_agg(port),
1978 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 } else {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001980 netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001982 } else {
1983 /* in case that the only port related to this
1984 * aggregator is the one we want to remove
1985 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 select_new_active_agg = aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 ad_clear_agg(aggregator);
1988 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001989 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01001990 /* select new active aggregator */
Veaceslav Falico74684492013-09-27 15:10:58 +02001991 temp_aggregator = __get_first_agg(port);
1992 if (temp_aggregator)
Mahesh Bandewaree637712014-10-04 17:45:01 -07001993 ad_agg_selection_logic(temp_aggregator,
1994 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 }
1997 }
1998
Veaceslav Falicod4471f52014-07-15 19:36:00 +02001999 netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002000
2001 /* find the aggregator that this port is connected to */
Veaceslav Falico0b0882642013-09-27 16:12:02 +02002002 bond_for_each_slave(bond, slave_iter, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002003 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 prev_port = NULL;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002005 /* search the port in the aggregator's related ports */
Bandan Das128ea6c2010-10-16 20:19:58 +00002006 for (temp_port = temp_aggregator->lag_ports; temp_port;
2007 prev_port = temp_port,
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002008 temp_port = temp_port->next_port_in_aggregator) {
2009 if (temp_port == port) {
2010 /* the aggregator found - detach the port from
2011 * this aggregator
2012 */
Bandan Das7bfc4752010-10-16 20:19:59 +00002013 if (prev_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
Bandan Das7bfc4752010-10-16 20:19:59 +00002015 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 temp_aggregator->num_of_ports--;
Bandan Das128ea6c2010-10-16 20:19:58 +00002018 if (temp_aggregator->num_of_ports == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 select_new_active_agg = temp_aggregator->is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 ad_clear_agg(temp_aggregator);
2021 if (select_new_active_agg) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002022 netdev_info(bond->dev, "Removing an active aggregator\n");
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002023 /* select new active aggregator */
Mahesh Bandewaree637712014-10-04 17:45:01 -07002024 ad_agg_selection_logic(__get_first_agg(port),
2025 &dummy_slave_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 }
2028 break;
2029 }
2030 }
2031 }
Bandan Das128ea6c2010-10-16 20:19:58 +00002032 port->slave = NULL;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002033
2034out:
2035 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
2038/**
2039 * bond_3ad_state_machine_handler - handle state machines timeout
2040 * @bond: bonding struct to work on
2041 *
2042 * The state machine handling concept in this module is to check every tick
2043 * which state machine should operate any function. The execution order is
2044 * round robin, so when we have an interaction between state machines, the
2045 * reply of one to each other might be delayed until next tick.
2046 *
2047 * This function also complete the initialization when the agg_select_timer
2048 * times out, and it selects an aggregator for the ports that are yet not
2049 * related to any aggregator, and selects the active aggregator for a bond.
2050 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002051void bond_3ad_state_machine_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002053 struct bonding *bond = container_of(work, struct bonding,
2054 ad_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct aggregator *aggregator;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002056 struct list_head *iter;
2057 struct slave *slave;
2058 struct port *port;
dingtianhong5e5b0662014-02-26 11:05:22 +08002059 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Mahesh Bandewaree637712014-10-04 17:45:01 -07002060 bool update_slave_arr = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002062 /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2063 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2064 * concurrently due to incoming LACPDU as well.
2065 */
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002066 spin_lock_bh(&bond->mode_lock);
dingtianhongbe79bd02013-12-13 10:20:12 +08002067 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04002068
dingtianhongbe79bd02013-12-13 10:20:12 +08002069 /* check if there are any slaves */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002070 if (!bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
dingtianhongbe79bd02013-12-13 10:20:12 +08002073 /* check if agg_select_timer timer after initialize is timed out */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002074 if (BOND_AD_INFO(bond).agg_select_timer &&
2075 !(--BOND_AD_INFO(bond).agg_select_timer)) {
dingtianhongbe79bd02013-12-13 10:20:12 +08002076 slave = bond_first_slave_rcu(bond);
dingtianhong3fdddd82014-05-12 15:08:43 +08002077 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002078
dingtianhongbe79bd02013-12-13 10:20:12 +08002079 /* select the active aggregator for the bond */
Veaceslav Falicofe9323d2013-09-27 16:11:58 +02002080 if (port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002082 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2083 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 goto re_arm;
2085 }
2086
2087 aggregator = __get_first_agg(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002088 ad_agg_selection_logic(aggregator, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002090 bond_3ad_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
dingtianhongbe79bd02013-12-13 10:20:12 +08002093 /* for each port run the state machines */
2094 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002095 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002097 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
Veaceslav Falico86a2b9c2014-03-16 17:55:03 +01002098 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 goto re_arm;
2100 }
2101
2102 ad_rx_machine(NULL, port);
2103 ad_periodic_machine(port);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002104 ad_port_selection_logic(port, &update_slave_arr);
2105 ad_mux_machine(port, &update_slave_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 ad_tx_machine(port);
2107
dingtianhongbe79bd02013-12-13 10:20:12 +08002108 /* turn off the BEGIN bit, since we already handled it */
Bandan Das7bfc4752010-10-16 20:19:59 +00002109 if (port->sm_vars & AD_PORT_BEGIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 port->sm_vars &= ~AD_PORT_BEGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
2113re_arm:
dingtianhong5e5b0662014-02-26 11:05:22 +08002114 bond_for_each_slave_rcu(bond, slave, iter) {
2115 if (slave->should_notify) {
2116 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2117 break;
2118 }
2119 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002120 rcu_read_unlock();
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02002121 spin_unlock_bh(&bond->mode_lock);
dingtianhong5e5b0662014-02-26 11:05:22 +08002122
Mahesh Bandewaree637712014-10-04 17:45:01 -07002123 if (update_slave_arr)
2124 bond_slave_arr_work_rearm(bond, 0);
2125
dingtianhong5e5b0662014-02-26 11:05:22 +08002126 if (should_notify_rtnl && rtnl_trylock()) {
dingtianhongb0929912014-02-26 11:05:23 +08002127 bond_slave_state_notify(bond);
dingtianhong5e5b0662014-02-26 11:05:22 +08002128 rtnl_unlock();
2129 }
dingtianhongbe79bd02013-12-13 10:20:12 +08002130 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/**
2134 * bond_3ad_rx_indication - handle a received frame
2135 * @lacpdu: received lacpdu
2136 * @slave: slave struct to work on
2137 * @length: length of the data received
2138 *
2139 * It is assumed that frames that were sent on this NIC don't returned as new
2140 * received frames (loopback). Since only the payload is given to this
2141 * function, it check for loopback.
2142 */
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002143static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2144 u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 struct port *port;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002147 int ret = RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (length >= sizeof(struct lacpdu)) {
2150
dingtianhong3fdddd82014-05-12 15:08:43 +08002151 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002154 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2155 slave->dev->name, slave->bond->dev->name);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158
2159 switch (lacpdu->subtype) {
2160 case AD_TYPE_LACPDU:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002161 ret = RX_HANDLER_CONSUMED;
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002162 netdev_dbg(slave->bond->dev, "Received LACPDU on port %d\n",
2163 port->actor_port_number);
Nils Carlson16d79d72011-03-03 22:09:11 +00002164 /* Protect against concurrent state machines */
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002165 spin_lock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 ad_rx_machine(lacpdu, port);
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002167 spin_unlock(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169
2170 case AD_TYPE_MARKER:
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002171 ret = RX_HANDLER_CONSUMED;
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002172 /* No need to convert fields to Little Endian since we
2173 * don't use the marker's fields.
2174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002176 switch (((struct bond_marker *)lacpdu)->tlv_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 case AD_MARKER_INFORMATION_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002178 netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2179 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002180 ad_marker_info_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 break;
2182
2183 case AD_MARKER_RESPONSE_SUBTYPE:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002184 netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2185 port->actor_port_number);
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -07002186 ad_marker_response_received((struct bond_marker *)lacpdu, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 break;
2188
2189 default:
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002190 netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2191 port->actor_port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 }
2194 }
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198/**
2199 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
2200 * @slave: slave struct to work on
2201 *
2202 * Handle reselection of aggregator (if needed) for this port.
2203 */
2204void bond_3ad_adapter_speed_changed(struct slave *slave)
2205{
2206 struct port *port;
2207
dingtianhong3fdddd82014-05-12 15:08:43 +08002208 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
dingtianhong71a06c52013-12-13 17:29:19 +08002210 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002212 netdev_warn(slave->bond->dev, "speed changed for uninitialized port on %s\n",
2213 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 return;
2215 }
2216
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002217 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong71a06c52013-12-13 17:29:19 +08002218
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002219 port->actor_admin_port_key &= ~AD_SPEED_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002220 port->actor_oper_port_key = port->actor_admin_port_key |=
2221 (__get_link_speed(port) << 1);
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002222 netdev_dbg(slave->bond->dev, "Port %d changed speed\n", port->actor_port_number);
dingtianhong71a06c52013-12-13 17:29:19 +08002223 /* there is no need to reselect a new aggregator, just signal the
2224 * state machines to reinitialize
2225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 port->sm_vars |= AD_PORT_BEGIN;
dingtianhong71a06c52013-12-13 17:29:19 +08002227
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002228 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/**
2232 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
2233 * @slave: slave struct to work on
2234 *
2235 * Handle reselection of aggregator (if needed) for this port.
2236 */
2237void bond_3ad_adapter_duplex_changed(struct slave *slave)
2238{
2239 struct port *port;
2240
dingtianhong3fdddd82014-05-12 15:08:43 +08002241 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
dingtianhongbca44a72013-12-13 17:29:24 +08002243 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002245 netdev_warn(slave->bond->dev, "duplex changed for uninitialized port on %s\n",
2246 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return;
2248 }
2249
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002250 spin_lock_bh(&slave->bond->mode_lock);
dingtianhongbca44a72013-12-13 17:29:24 +08002251
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002252 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002253 port->actor_oper_port_key = port->actor_admin_port_key |=
2254 __get_duplex(port);
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002255 netdev_dbg(slave->bond->dev, "Port %d changed duplex\n", port->actor_port_number);
dingtianhongbca44a72013-12-13 17:29:24 +08002256 /* there is no need to reselect a new aggregator, just signal the
2257 * state machines to reinitialize
2258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 port->sm_vars |= AD_PORT_BEGIN;
dingtianhongbca44a72013-12-13 17:29:24 +08002260
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002261 spin_unlock_bh(&slave->bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264/**
2265 * bond_3ad_handle_link_change - handle a slave's link status change indication
2266 * @slave: slave struct to work on
2267 * @status: whether the link is now up or down
2268 *
2269 * Handle reselection of aggregator (if needed) for this port.
2270 */
2271void bond_3ad_handle_link_change(struct slave *slave, char link)
2272{
2273 struct port *port;
2274
dingtianhong3fdddd82014-05-12 15:08:43 +08002275 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
dingtianhong108db732013-12-13 17:29:29 +08002277 /* if slave is null, the whole port is not initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (!port->slave) {
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002279 netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2280 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return;
2282 }
2283
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002284 spin_lock_bh(&slave->bond->mode_lock);
dingtianhong108db732013-12-13 17:29:29 +08002285 /* on link down we are zeroing duplex and speed since
2286 * some of the adaptors(ce1000.lan) report full duplex/speed
2287 * instead of N/A(duplex) / 0(speed).
2288 *
2289 * on link up we are forcing recheck on the duplex and speed since
2290 * some of he adaptors(ce1000.lan) report.
2291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (link == BOND_LINK_UP) {
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002293 port->is_enabled = true;
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002294 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002295 port->actor_oper_port_key = port->actor_admin_port_key |=
2296 __get_duplex(port);
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002297 port->actor_admin_port_key &= ~AD_SPEED_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002298 port->actor_oper_port_key = port->actor_admin_port_key |=
2299 (__get_link_speed(port) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 } else {
2301 /* link has failed */
Holger Eitzenbergerf48127b2008-12-26 13:26:54 -08002302 port->is_enabled = false;
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002303 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_MASKS;
Bandan Das128ea6c2010-10-16 20:19:58 +00002304 port->actor_oper_port_key = (port->actor_admin_port_key &=
Jianhua Xiecb8dda92014-11-19 16:48:58 +08002305 ~AD_SPEED_KEY_MASKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Veaceslav Falicod4471f52014-07-15 19:36:00 +02002307 netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2308 port->actor_port_number,
2309 link == BOND_LINK_UP ? "UP" : "DOWN");
dingtianhong108db732013-12-13 17:29:29 +08002310 /* there is no need to reselect a new aggregator, just signal the
2311 * state machines to reinitialize
2312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 port->sm_vars |= AD_PORT_BEGIN;
dingtianhong108db732013-12-13 17:29:29 +08002314
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002315 spin_unlock_bh(&slave->bond->mode_lock);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002316
2317 /* RTNL is held and mode_lock is released so it's safe
2318 * to update slave_array here.
2319 */
2320 bond_update_slave_arr(slave->bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002323/**
2324 * bond_3ad_set_carrier - set link state for bonding master
2325 * @bond - bonding structure
2326 *
2327 * if we have an active aggregator, we're up, if not, we're down.
2328 * Presumes that we cannot have an active aggregator if there are
2329 * no slaves with link up.
Jay Vosburghff59c452006-03-27 13:27:43 -08002330 *
Jay Vosburgh031ae4d2007-06-13 22:11:34 -07002331 * This behavior complies with IEEE 802.3 section 43.3.9.
2332 *
Jay Vosburghff59c452006-03-27 13:27:43 -08002333 * Called by bond_set_carrier(). Return zero if carrier state does not
2334 * change, nonzero if it does.
2335 */
2336int bond_3ad_set_carrier(struct bonding *bond)
2337{
stephen hemminger655f8912011-06-22 09:54:39 +00002338 struct aggregator *active;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002339 struct slave *first_slave;
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002340 int ret = 1;
stephen hemminger655f8912011-06-22 09:54:39 +00002341
dingtianhongbe79bd02013-12-13 10:20:12 +08002342 rcu_read_lock();
2343 first_slave = bond_first_slave_rcu(bond);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002344 if (!first_slave) {
2345 ret = 0;
2346 goto out;
2347 }
dingtianhong3fdddd82014-05-12 15:08:43 +08002348 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
stephen hemminger655f8912011-06-22 09:54:39 +00002349 if (active) {
2350 /* are enough slaves available to consider link up? */
2351 if (active->num_of_ports < bond->params.min_links) {
2352 if (netif_carrier_ok(bond->dev)) {
2353 netif_carrier_off(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002354 goto out;
stephen hemminger655f8912011-06-22 09:54:39 +00002355 }
2356 } else if (!netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002357 netif_carrier_on(bond->dev);
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002358 goto out;
Jay Vosburghff59c452006-03-27 13:27:43 -08002359 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002360 } else if (netif_carrier_ok(bond->dev)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002361 netif_carrier_off(bond->dev);
Jay Vosburghff59c452006-03-27 13:27:43 -08002362 }
Veaceslav Falicoc1bc9642014-01-10 11:59:43 +01002363out:
2364 rcu_read_unlock();
2365 return ret;
Jay Vosburghff59c452006-03-27 13:27:43 -08002366}
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368/**
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002369 * __bond_3ad_get_active_agg_info - get information of the active aggregator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 * @bond: bonding struct to work on
2371 * @ad_info: ad_info struct to fill with the bond's info
2372 *
2373 * Returns: 0 on success
2374 * < 0 on error
2375 */
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002376int __bond_3ad_get_active_agg_info(struct bonding *bond,
2377 struct ad_info *ad_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
2379 struct aggregator *aggregator = NULL;
Veaceslav Falico3c4c88a2013-09-27 16:11:57 +02002380 struct list_head *iter;
2381 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 struct port *port;
2383
dingtianhong47e91f562013-10-15 16:28:35 +08002384 bond_for_each_slave_rcu(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002385 port = &(SLAVE_AD_INFO(slave)->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (port->aggregator && port->aggregator->is_active) {
2387 aggregator = port->aggregator;
2388 break;
2389 }
2390 }
2391
Joe Perches21f374c2014-02-18 09:42:47 -08002392 if (!aggregator)
2393 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Joe Perches21f374c2014-02-18 09:42:47 -08002395 ad_info->aggregator_id = aggregator->aggregator_identifier;
2396 ad_info->ports = aggregator->num_of_ports;
2397 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2398 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2399 ether_addr_copy(ad_info->partner_system,
2400 aggregator->partner_system.mac_addr_value);
2401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002404int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2405{
2406 int ret;
2407
dingtianhong47e91f562013-10-15 16:28:35 +08002408 rcu_read_lock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002409 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
dingtianhong47e91f562013-10-15 16:28:35 +08002410 rcu_read_unlock();
nikolay@redhat.com318debd2013-05-18 01:18:31 +00002411
2412 return ret;
2413}
2414
Eric Dumazetde063b72012-06-11 19:23:07 +00002415int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2416 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Eric Dumazetde063b72012-06-11 19:23:07 +00002418 struct lacpdu *lacpdu, _lacpdu;
2419
Jiri Pirko3aba8912011-04-19 03:48:16 +00002420 if (skb->protocol != PKT_TYPE_LACPDU)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002421 return RX_HANDLER_ANOTHER;
Neil Hormanb3053252011-01-20 09:02:31 +00002422
Eric Dumazetde063b72012-06-11 19:23:07 +00002423 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2424 if (!lacpdu)
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002425 return RX_HANDLER_ANOTHER;
Andy Gospodarekab128112010-09-10 11:43:20 +00002426
Nikolay Aleksandrov86e74982014-09-11 22:49:22 +02002427 return bond_3ad_rx_indication(lacpdu, slave, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002429
Veaceslav Falico3bf2d282014-01-08 16:46:46 +01002430/**
2431 * bond_3ad_update_lacp_rate - change the lacp rate
2432 * @bond - bonding struct
2433 *
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002434 * When modify lacp_rate parameter via sysfs,
2435 * update actor_oper_port_state of each port.
2436 *
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002437 * Hold bond->mode_lock,
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002438 * so we can modify port->actor_oper_port_state,
2439 * no matter bond is up or down.
2440 */
2441void bond_3ad_update_lacp_rate(struct bonding *bond)
2442{
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002443 struct port *port = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002444 struct list_head *iter;
nikolay@redhat.comc5093162013-09-02 13:51:40 +02002445 struct slave *slave;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002446 int lacp_fast;
2447
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002448 lacp_fast = bond->params.lacp_fast;
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002449 spin_lock_bh(&bond->mode_lock);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002450 bond_for_each_slave(bond, slave, iter) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002451 port = &(SLAVE_AD_INFO(slave)->port);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002452 if (lacp_fast)
2453 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2454 else
2455 port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002456 }
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002457 spin_unlock_bh(&bond->mode_lock);
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +00002458}