1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
4  */
5 
6 #include <linux/skbuff.h>
7 #include <linux/if_ether.h>
8 #include <linux/netdevice.h>
9 #include <linux/spinlock.h>
10 #include <linux/ethtool.h>
11 #include <linux/etherdevice.h>
12 #include <linux/if_bonding.h>
13 #include <linux/pkt_sched.h>
14 #include <net/net_namespace.h>
15 #include <net/bonding.h>
16 #include <net/bond_3ad.h>
17 #include <net/netlink.h>
18 
19 /* General definitions */
20 #define AD_SHORT_TIMEOUT           1
21 #define AD_LONG_TIMEOUT            0
22 #define AD_STANDBY                 0x2
23 #define AD_MAX_TX_IN_SECOND        3
24 #define AD_COLLECTOR_MAX_DELAY     0
25 
26 /* Timer definitions (43.4.4 in the 802.3ad standard) */
27 #define AD_FAST_PERIODIC_TIME      1
28 #define AD_SLOW_PERIODIC_TIME      30
29 #define AD_SHORT_TIMEOUT_TIME      (3*AD_FAST_PERIODIC_TIME)
30 #define AD_LONG_TIMEOUT_TIME       (3*AD_SLOW_PERIODIC_TIME)
31 #define AD_CHURN_DETECTION_TIME    60
32 #define AD_AGGREGATE_WAIT_TIME     2
33 
34 /* Port Variables definitions used by the State Machines (43.4.7 in the
35  * 802.3ad standard)
36  */
37 #define AD_PORT_BEGIN           0x1
38 #define AD_PORT_LACP_ENABLED    0x2
39 #define AD_PORT_ACTOR_CHURN     0x4
40 #define AD_PORT_PARTNER_CHURN   0x8
41 #define AD_PORT_READY           0x10
42 #define AD_PORT_READY_N         0x20
43 #define AD_PORT_MATCHED         0x40
44 #define AD_PORT_STANDBY         0x80
45 #define AD_PORT_SELECTED        0x100
46 #define AD_PORT_MOVED           0x200
47 #define AD_PORT_CHURNED         (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
48 
49 /* Port Key definitions
50  * key is determined according to the link speed, duplex and
51  * user key (which is yet not supported)
52  *           --------------------------------------------------------------
53  * Port key  | User key (10 bits)           | Speed (5 bits)      | Duplex|
54  *           --------------------------------------------------------------
55  *           |15                           6|5                   1|0
56  */
57 #define  AD_DUPLEX_KEY_MASKS    0x1
58 #define  AD_SPEED_KEY_MASKS     0x3E
59 #define  AD_USER_KEY_MASKS      0xFFC0
60 
61 enum ad_link_speed_type {
62 	AD_LINK_SPEED_1MBPS = 1,
63 	AD_LINK_SPEED_10MBPS,
64 	AD_LINK_SPEED_100MBPS,
65 	AD_LINK_SPEED_1000MBPS,
66 	AD_LINK_SPEED_2500MBPS,
67 	AD_LINK_SPEED_5000MBPS,
68 	AD_LINK_SPEED_10000MBPS,
69 	AD_LINK_SPEED_14000MBPS,
70 	AD_LINK_SPEED_20000MBPS,
71 	AD_LINK_SPEED_25000MBPS,
72 	AD_LINK_SPEED_40000MBPS,
73 	AD_LINK_SPEED_50000MBPS,
74 	AD_LINK_SPEED_56000MBPS,
75 	AD_LINK_SPEED_100000MBPS,
76 	AD_LINK_SPEED_200000MBPS,
77 	AD_LINK_SPEED_400000MBPS,
78 };
79 
80 /* compare MAC addresses */
81 #define MAC_ADDRESS_EQUAL(A, B)	\
82 	ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
83 
84 static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
85 	0, 0, 0, 0, 0, 0
86 };
87 
88 static const u16 ad_ticks_per_sec = 1000 / AD_TIMER_INTERVAL;
89 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
90 
91 const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned = {
92 	0x01, 0x80, 0xC2, 0x00, 0x00, 0x02
93 };
94 
95 /* ================= main 802.3ad protocol functions ================== */
96 static int ad_lacpdu_send(struct port *port);
97 static int ad_marker_send(struct port *port, struct bond_marker *marker);
98 static void ad_mux_machine(struct port *port, bool *update_slave_arr);
99 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
100 static void ad_tx_machine(struct port *port);
101 static void ad_periodic_machine(struct port *port, struct bond_params *bond_params);
102 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
103 static void ad_agg_selection_logic(struct aggregator *aggregator,
104 				   bool *update_slave_arr);
105 static void ad_clear_agg(struct aggregator *aggregator);
106 static void ad_initialize_agg(struct aggregator *aggregator);
107 static void ad_initialize_port(struct port *port, int lacp_fast);
108 static void ad_enable_collecting_distributing(struct port *port,
109 					      bool *update_slave_arr);
110 static void ad_disable_collecting_distributing(struct port *port,
111 					       bool *update_slave_arr);
112 static void ad_marker_info_received(struct bond_marker *marker_info,
113 				    struct port *port);
114 static void ad_marker_response_received(struct bond_marker *marker,
115 					struct port *port);
116 static void ad_update_actor_keys(struct port *port, bool reset);
117 
118 
119 /* ================= api to bonding and kernel code ================== */
120 
121 /**
122  * __get_bond_by_port - get the port's bonding struct
123  * @port: the port we're looking at
124  *
125  * Return @port's bonding struct, or %NULL if it can't be found.
126  */
__get_bond_by_port(struct port * port)127 static inline struct bonding *__get_bond_by_port(struct port *port)
128 {
129 	if (port->slave == NULL)
130 		return NULL;
131 
132 	return bond_get_bond_by_slave(port->slave);
133 }
134 
135 /**
136  * __get_first_agg - get the first aggregator in the bond
137  * @port: the port we're looking at
138  *
139  * Return the aggregator of the first slave in @bond, or %NULL if it can't be
140  * found.
141  * The caller must hold RCU or RTNL lock.
142  */
__get_first_agg(struct port * port)143 static inline struct aggregator *__get_first_agg(struct port *port)
144 {
145 	struct bonding *bond = __get_bond_by_port(port);
146 	struct slave *first_slave;
147 	struct aggregator *agg;
148 
149 	/* If there's no bond for this port, or bond has no slaves */
150 	if (bond == NULL)
151 		return NULL;
152 
153 	rcu_read_lock();
154 	first_slave = bond_first_slave_rcu(bond);
155 	agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
156 	rcu_read_unlock();
157 
158 	return agg;
159 }
160 
161 /**
162  * __agg_has_partner - see if we have a partner
163  * @agg: the agregator we're looking at
164  *
165  * Return nonzero if aggregator has a partner (denoted by a non-zero ether
166  * address for the partner). Return 0 if not.
167  */
__agg_has_partner(struct aggregator * agg)168 static inline int __agg_has_partner(struct aggregator *agg)
169 {
170 	return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
171 }
172 
173 /**
174  * __disable_port - disable the port's slave
175  * @port: the port we're looking at
176  */
__disable_port(struct port * port)177 static inline void __disable_port(struct port *port)
178 {
179 	bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
180 }
181 
182 /**
183  * __enable_port - enable the port's slave, if it's up
184  * @port: the port we're looking at
185  */
__enable_port(struct port * port)186 static inline void __enable_port(struct port *port)
187 {
188 	struct slave *slave = port->slave;
189 
190 	if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
191 		bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
192 }
193 
194 /**
195  * __port_is_enabled - check if the port's slave is in active state
196  * @port: the port we're looking at
197  */
__port_is_enabled(struct port * port)198 static inline int __port_is_enabled(struct port *port)
199 {
200 	return bond_is_active_slave(port->slave);
201 }
202 
203 /**
204  * __get_agg_selection_mode - get the aggregator selection mode
205  * @port: the port we're looking at
206  *
207  * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
208  */
__get_agg_selection_mode(struct port * port)209 static inline u32 __get_agg_selection_mode(struct port *port)
210 {
211 	struct bonding *bond = __get_bond_by_port(port);
212 
213 	if (bond == NULL)
214 		return BOND_AD_STABLE;
215 
216 	return bond->params.ad_select;
217 }
218 
219 /**
220  * __check_agg_selection_timer - check if the selection timer has expired
221  * @port: the port we're looking at
222  */
__check_agg_selection_timer(struct port * port)223 static inline int __check_agg_selection_timer(struct port *port)
224 {
225 	struct bonding *bond = __get_bond_by_port(port);
226 
227 	if (bond == NULL)
228 		return 0;
229 
230 	return atomic_read(&BOND_AD_INFO(bond).agg_select_timer) ? 1 : 0;
231 }
232 
233 /**
234  * __get_link_speed - get a port's speed
235  * @port: the port we're looking at
236  *
237  * Return @port's speed in 802.3ad enum format. i.e. one of:
238  *     0,
239  *     %AD_LINK_SPEED_10MBPS,
240  *     %AD_LINK_SPEED_100MBPS,
241  *     %AD_LINK_SPEED_1000MBPS,
242  *     %AD_LINK_SPEED_2500MBPS,
243  *     %AD_LINK_SPEED_5000MBPS,
244  *     %AD_LINK_SPEED_10000MBPS
245  *     %AD_LINK_SPEED_14000MBPS,
246  *     %AD_LINK_SPEED_20000MBPS
247  *     %AD_LINK_SPEED_25000MBPS
248  *     %AD_LINK_SPEED_40000MBPS
249  *     %AD_LINK_SPEED_50000MBPS
250  *     %AD_LINK_SPEED_56000MBPS
251  *     %AD_LINK_SPEED_100000MBPS
252  *     %AD_LINK_SPEED_200000MBPS
253  *     %AD_LINK_SPEED_400000MBPS
254  */
__get_link_speed(struct port * port)255 static u16 __get_link_speed(struct port *port)
256 {
257 	struct slave *slave = port->slave;
258 	u16 speed;
259 
260 	/* this if covers only a special case: when the configuration starts
261 	 * with link down, it sets the speed to 0.
262 	 * This is done in spite of the fact that the e100 driver reports 0
263 	 * to be compatible with MVT in the future.
264 	 */
265 	if (slave->link != BOND_LINK_UP)
266 		speed = 0;
267 	else {
268 		switch (slave->speed) {
269 		case SPEED_10:
270 			speed = AD_LINK_SPEED_10MBPS;
271 			break;
272 
273 		case SPEED_100:
274 			speed = AD_LINK_SPEED_100MBPS;
275 			break;
276 
277 		case SPEED_1000:
278 			speed = AD_LINK_SPEED_1000MBPS;
279 			break;
280 
281 		case SPEED_2500:
282 			speed = AD_LINK_SPEED_2500MBPS;
283 			break;
284 
285 		case SPEED_5000:
286 			speed = AD_LINK_SPEED_5000MBPS;
287 			break;
288 
289 		case SPEED_10000:
290 			speed = AD_LINK_SPEED_10000MBPS;
291 			break;
292 
293 		case SPEED_14000:
294 			speed = AD_LINK_SPEED_14000MBPS;
295 			break;
296 
297 		case SPEED_20000:
298 			speed = AD_LINK_SPEED_20000MBPS;
299 			break;
300 
301 		case SPEED_25000:
302 			speed = AD_LINK_SPEED_25000MBPS;
303 			break;
304 
305 		case SPEED_40000:
306 			speed = AD_LINK_SPEED_40000MBPS;
307 			break;
308 
309 		case SPEED_50000:
310 			speed = AD_LINK_SPEED_50000MBPS;
311 			break;
312 
313 		case SPEED_56000:
314 			speed = AD_LINK_SPEED_56000MBPS;
315 			break;
316 
317 		case SPEED_100000:
318 			speed = AD_LINK_SPEED_100000MBPS;
319 			break;
320 
321 		case SPEED_200000:
322 			speed = AD_LINK_SPEED_200000MBPS;
323 			break;
324 
325 		case SPEED_400000:
326 			speed = AD_LINK_SPEED_400000MBPS;
327 			break;
328 
329 		default:
330 			/* unknown speed value from ethtool. shouldn't happen */
331 			if (slave->speed != SPEED_UNKNOWN)
332 				pr_err_once("%s: (slave %s): unknown ethtool speed (%d) for port %d (set it to 0)\n",
333 					    slave->bond->dev->name,
334 					    slave->dev->name, slave->speed,
335 					    port->actor_port_number);
336 			speed = 0;
337 			break;
338 		}
339 	}
340 
341 	slave_dbg(slave->bond->dev, slave->dev, "Port %d Received link speed %d update from adapter\n",
342 		  port->actor_port_number, speed);
343 	return speed;
344 }
345 
346 /**
347  * __get_duplex - get a port's duplex
348  * @port: the port we're looking at
349  *
350  * Return @port's duplex in 802.3ad bitmask format. i.e.:
351  *     0x01 if in full duplex
352  *     0x00 otherwise
353  */
__get_duplex(struct port * port)354 static u8 __get_duplex(struct port *port)
355 {
356 	struct slave *slave = port->slave;
357 	u8 retval = 0x0;
358 
359 	/* handling a special case: when the configuration starts with
360 	 * link down, it sets the duplex to 0.
361 	 */
362 	if (slave->link == BOND_LINK_UP) {
363 		switch (slave->duplex) {
364 		case DUPLEX_FULL:
365 			retval = 0x1;
366 			slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status full duplex update from adapter\n",
367 				  port->actor_port_number);
368 			break;
369 		case DUPLEX_HALF:
370 		default:
371 			retval = 0x0;
372 			slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status NOT full duplex update from adapter\n",
373 				  port->actor_port_number);
374 			break;
375 		}
376 	}
377 	return retval;
378 }
379 
__ad_actor_update_port(struct port * port)380 static void __ad_actor_update_port(struct port *port)
381 {
382 	const struct bonding *bond = bond_get_bond_by_slave(port->slave);
383 
384 	port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
385 	port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority;
386 }
387 
388 /* Conversions */
389 
390 /**
391  * __ad_timer_to_ticks - convert a given timer type to AD module ticks
392  * @timer_type:	which timer to operate
393  * @par: timer parameter. see below
394  *
395  * If @timer_type is %current_while_timer, @par indicates long/short timer.
396  * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
397  *						     %SLOW_PERIODIC_TIME.
398  */
__ad_timer_to_ticks(u16 timer_type,u16 par)399 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
400 {
401 	u16 retval = 0; /* to silence the compiler */
402 
403 	switch (timer_type) {
404 	case AD_CURRENT_WHILE_TIMER:	/* for rx machine usage */
405 		if (par)
406 			retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
407 		else
408 			retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
409 		break;
410 	case AD_ACTOR_CHURN_TIMER:	/* for local churn machine */
411 		retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
412 		break;
413 	case AD_PERIODIC_TIMER:		/* for periodic machine */
414 		retval = (par*ad_ticks_per_sec); /* long timeout */
415 		break;
416 	case AD_PARTNER_CHURN_TIMER:	/* for remote churn machine */
417 		retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
418 		break;
419 	case AD_WAIT_WHILE_TIMER:	/* for selection machine */
420 		retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
421 		break;
422 	}
423 
424 	return retval;
425 }
426 
427 
428 /* ================= ad_rx_machine helper functions ================== */
429 
430 /**
431  * __choose_matched - update a port's matched variable from a received lacpdu
432  * @lacpdu: the lacpdu we've received
433  * @port: the port we're looking at
434  *
435  * Update the value of the matched variable, using parameter values from a
436  * newly received lacpdu. Parameter values for the partner carried in the
437  * received PDU are compared with the corresponding operational parameter
438  * values for the actor. Matched is set to TRUE if all of these parameters
439  * match and the PDU parameter partner_state.aggregation has the same value as
440  * actor_oper_port_state.aggregation and lacp will actively maintain the link
441  * in the aggregation. Matched is also set to TRUE if the value of
442  * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
443  * an individual link and lacp will actively maintain the link. Otherwise,
444  * matched is set to FALSE. LACP is considered to be actively maintaining the
445  * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
446  * the actor's actor_oper_port_state.lacp_activity and the PDU's
447  * partner_state.lacp_activity variables are TRUE.
448  *
449  * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
450  * used here to implement the language from 802.3ad 43.4.9 that requires
451  * recordPDU to "match" the LACPDU parameters to the stored values.
452  */
__choose_matched(struct lacpdu * lacpdu,struct port * port)453 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
454 {
455 	/* check if all parameters are alike
456 	 * or this is individual link(aggregation == FALSE)
457 	 * then update the state machine Matched variable.
458 	 */
459 	if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
460 	     (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
461 	     MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
462 	     (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
463 	     (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
464 	     ((lacpdu->partner_state & LACP_STATE_AGGREGATION) == (port->actor_oper_port_state & LACP_STATE_AGGREGATION))) ||
465 	    ((lacpdu->actor_state & LACP_STATE_AGGREGATION) == 0)
466 		) {
467 		port->sm_vars |= AD_PORT_MATCHED;
468 	} else {
469 		port->sm_vars &= ~AD_PORT_MATCHED;
470 	}
471 }
472 
473 /**
474  * __record_pdu - record parameters from a received lacpdu
475  * @lacpdu: the lacpdu we've received
476  * @port: the port we're looking at
477  *
478  * Record the parameter values for the Actor carried in a received lacpdu as
479  * the current partner operational parameter values and sets
480  * actor_oper_port_state.defaulted to FALSE.
481  */
__record_pdu(struct lacpdu * lacpdu,struct port * port)482 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
483 {
484 	if (lacpdu && port) {
485 		struct port_params *partner = &port->partner_oper;
486 
487 		__choose_matched(lacpdu, port);
488 		/* record the new parameter values for the partner
489 		 * operational
490 		 */
491 		partner->port_number = ntohs(lacpdu->actor_port);
492 		partner->port_priority = ntohs(lacpdu->actor_port_priority);
493 		partner->system = lacpdu->actor_system;
494 		partner->system_priority = ntohs(lacpdu->actor_system_priority);
495 		partner->key = ntohs(lacpdu->actor_key);
496 		partner->port_state = lacpdu->actor_state;
497 
498 		/* set actor_oper_port_state.defaulted to FALSE */
499 		port->actor_oper_port_state &= ~LACP_STATE_DEFAULTED;
500 
501 		/* set the partner sync. to on if the partner is sync,
502 		 * and the port is matched
503 		 */
504 		if ((port->sm_vars & AD_PORT_MATCHED) &&
505 		    (lacpdu->actor_state & LACP_STATE_SYNCHRONIZATION)) {
506 			partner->port_state |= LACP_STATE_SYNCHRONIZATION;
507 			slave_dbg(port->slave->bond->dev, port->slave->dev,
508 				  "partner sync=1\n");
509 		} else {
510 			partner->port_state &= ~LACP_STATE_SYNCHRONIZATION;
511 			slave_dbg(port->slave->bond->dev, port->slave->dev,
512 				  "partner sync=0\n");
513 		}
514 	}
515 }
516 
517 /**
518  * __record_default - record default parameters
519  * @port: the port we're looking at
520  *
521  * This function records the default parameter values for the partner carried
522  * in the Partner Admin parameters as the current partner operational parameter
523  * values and sets actor_oper_port_state.defaulted to TRUE.
524  */
__record_default(struct port * port)525 static void __record_default(struct port *port)
526 {
527 	if (port) {
528 		/* record the partner admin parameters */
529 		memcpy(&port->partner_oper, &port->partner_admin,
530 		       sizeof(struct port_params));
531 
532 		/* set actor_oper_port_state.defaulted to true */
533 		port->actor_oper_port_state |= LACP_STATE_DEFAULTED;
534 	}
535 }
536 
537 /**
538  * __update_selected - update a port's Selected variable from a received lacpdu
539  * @lacpdu: the lacpdu we've received
540  * @port: the port we're looking at
541  *
542  * Update the value of the selected variable, using parameter values from a
543  * newly received lacpdu. The parameter values for the Actor carried in the
544  * received PDU are compared with the corresponding operational parameter
545  * values for the ports partner. If one or more of the comparisons shows that
546  * the value(s) received in the PDU differ from the current operational values,
547  * then selected is set to FALSE and actor_oper_port_state.synchronization is
548  * set to out_of_sync. Otherwise, selected remains unchanged.
549  */
__update_selected(struct lacpdu * lacpdu,struct port * port)550 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
551 {
552 	if (lacpdu && port) {
553 		const struct port_params *partner = &port->partner_oper;
554 
555 		/* check if any parameter is different then
556 		 * update the state machine selected variable.
557 		 */
558 		if (ntohs(lacpdu->actor_port) != partner->port_number ||
559 		    ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
560 		    !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
561 		    ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
562 		    ntohs(lacpdu->actor_key) != partner->key ||
563 		    (lacpdu->actor_state & LACP_STATE_AGGREGATION) != (partner->port_state & LACP_STATE_AGGREGATION)) {
564 			port->sm_vars &= ~AD_PORT_SELECTED;
565 		}
566 	}
567 }
568 
569 /**
570  * __update_default_selected - update a port's Selected variable from Partner
571  * @port: the port we're looking at
572  *
573  * This function updates the value of the selected variable, using the partner
574  * administrative parameter values. The administrative values are compared with
575  * the corresponding operational parameter values for the partner. If one or
576  * more of the comparisons shows that the administrative value(s) differ from
577  * the current operational values, then Selected is set to FALSE and
578  * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
579  * Selected remains unchanged.
580  */
__update_default_selected(struct port * port)581 static void __update_default_selected(struct port *port)
582 {
583 	if (port) {
584 		const struct port_params *admin = &port->partner_admin;
585 		const struct port_params *oper = &port->partner_oper;
586 
587 		/* check if any parameter is different then
588 		 * update the state machine selected variable.
589 		 */
590 		if (admin->port_number != oper->port_number ||
591 		    admin->port_priority != oper->port_priority ||
592 		    !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
593 		    admin->system_priority != oper->system_priority ||
594 		    admin->key != oper->key ||
595 		    (admin->port_state & LACP_STATE_AGGREGATION)
596 			!= (oper->port_state & LACP_STATE_AGGREGATION)) {
597 			port->sm_vars &= ~AD_PORT_SELECTED;
598 		}
599 	}
600 }
601 
602 /**
603  * __update_ntt - update a port's ntt variable from a received lacpdu
604  * @lacpdu: the lacpdu we've received
605  * @port: the port we're looking at
606  *
607  * Updates the value of the ntt variable, using parameter values from a newly
608  * received lacpdu. The parameter values for the partner carried in the
609  * received PDU are compared with the corresponding operational parameter
610  * values for the Actor. If one or more of the comparisons shows that the
611  * value(s) received in the PDU differ from the current operational values,
612  * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
613  */
__update_ntt(struct lacpdu * lacpdu,struct port * port)614 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
615 {
616 	/* validate lacpdu and port */
617 	if (lacpdu && port) {
618 		/* check if any parameter is different then
619 		 * update the port->ntt.
620 		 */
621 		if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
622 		    (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
623 		    !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
624 		    (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
625 		    (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
626 		    ((lacpdu->partner_state & LACP_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY)) ||
627 		    ((lacpdu->partner_state & LACP_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT)) ||
628 		    ((lacpdu->partner_state & LACP_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) ||
629 		    ((lacpdu->partner_state & LACP_STATE_AGGREGATION) != (port->actor_oper_port_state & LACP_STATE_AGGREGATION))
630 		   ) {
631 			port->ntt = true;
632 		}
633 	}
634 }
635 
636 /**
637  * __agg_ports_are_ready - check if all ports in an aggregator are ready
638  * @aggregator: the aggregator we're looking at
639  *
640  */
__agg_ports_are_ready(struct aggregator * aggregator)641 static int __agg_ports_are_ready(struct aggregator *aggregator)
642 {
643 	struct port *port;
644 	int retval = 1;
645 
646 	if (aggregator) {
647 		/* scan all ports in this aggregator to verfy if they are
648 		 * all ready.
649 		 */
650 		for (port = aggregator->lag_ports;
651 		     port;
652 		     port = port->next_port_in_aggregator) {
653 			if (!(port->sm_vars & AD_PORT_READY_N)) {
654 				retval = 0;
655 				break;
656 			}
657 		}
658 	}
659 
660 	return retval;
661 }
662 
663 /**
664  * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
665  * @aggregator: the aggregator we're looking at
666  * @val: Should the ports' ready bit be set on or off
667  *
668  */
__set_agg_ports_ready(struct aggregator * aggregator,int val)669 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
670 {
671 	struct port *port;
672 
673 	for (port = aggregator->lag_ports; port;
674 	     port = port->next_port_in_aggregator) {
675 		if (val)
676 			port->sm_vars |= AD_PORT_READY;
677 		else
678 			port->sm_vars &= ~AD_PORT_READY;
679 	}
680 }
681 
__agg_active_ports(struct aggregator * agg)682 static int __agg_active_ports(struct aggregator *agg)
683 {
684 	struct port *port;
685 	int active = 0;
686 
687 	for (port = agg->lag_ports; port;
688 	     port = port->next_port_in_aggregator) {
689 		if (port->is_enabled)
690 			active++;
691 	}
692 
693 	return active;
694 }
695 
696 /**
697  * __get_agg_bandwidth - get the total bandwidth of an aggregator
698  * @aggregator: the aggregator we're looking at
699  *
700  */
__get_agg_bandwidth(struct aggregator * aggregator)701 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
702 {
703 	int nports = __agg_active_ports(aggregator);
704 	u32 bandwidth = 0;
705 
706 	if (nports) {
707 		switch (__get_link_speed(aggregator->lag_ports)) {
708 		case AD_LINK_SPEED_1MBPS:
709 			bandwidth = nports;
710 			break;
711 		case AD_LINK_SPEED_10MBPS:
712 			bandwidth = nports * 10;
713 			break;
714 		case AD_LINK_SPEED_100MBPS:
715 			bandwidth = nports * 100;
716 			break;
717 		case AD_LINK_SPEED_1000MBPS:
718 			bandwidth = nports * 1000;
719 			break;
720 		case AD_LINK_SPEED_2500MBPS:
721 			bandwidth = nports * 2500;
722 			break;
723 		case AD_LINK_SPEED_5000MBPS:
724 			bandwidth = nports * 5000;
725 			break;
726 		case AD_LINK_SPEED_10000MBPS:
727 			bandwidth = nports * 10000;
728 			break;
729 		case AD_LINK_SPEED_14000MBPS:
730 			bandwidth = nports * 14000;
731 			break;
732 		case AD_LINK_SPEED_20000MBPS:
733 			bandwidth = nports * 20000;
734 			break;
735 		case AD_LINK_SPEED_25000MBPS:
736 			bandwidth = nports * 25000;
737 			break;
738 		case AD_LINK_SPEED_40000MBPS:
739 			bandwidth = nports * 40000;
740 			break;
741 		case AD_LINK_SPEED_50000MBPS:
742 			bandwidth = nports * 50000;
743 			break;
744 		case AD_LINK_SPEED_56000MBPS:
745 			bandwidth = nports * 56000;
746 			break;
747 		case AD_LINK_SPEED_100000MBPS:
748 			bandwidth = nports * 100000;
749 			break;
750 		case AD_LINK_SPEED_200000MBPS:
751 			bandwidth = nports * 200000;
752 			break;
753 		case AD_LINK_SPEED_400000MBPS:
754 			bandwidth = nports * 400000;
755 			break;
756 		default:
757 			bandwidth = 0; /* to silence the compiler */
758 		}
759 	}
760 	return bandwidth;
761 }
762 
763 /**
764  * __get_active_agg - get the current active aggregator
765  * @aggregator: the aggregator we're looking at
766  *
767  * Caller must hold RCU lock.
768  */
__get_active_agg(struct aggregator * aggregator)769 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
770 {
771 	struct bonding *bond = aggregator->slave->bond;
772 	struct list_head *iter;
773 	struct slave *slave;
774 
775 	bond_for_each_slave_rcu(bond, slave, iter)
776 		if (SLAVE_AD_INFO(slave)->aggregator.is_active)
777 			return &(SLAVE_AD_INFO(slave)->aggregator);
778 
779 	return NULL;
780 }
781 
782 /**
783  * __update_lacpdu_from_port - update a port's lacpdu fields
784  * @port: the port we're looking at
785  */
__update_lacpdu_from_port(struct port * port)786 static inline void __update_lacpdu_from_port(struct port *port)
787 {
788 	struct lacpdu *lacpdu = &port->lacpdu;
789 	const struct port_params *partner = &port->partner_oper;
790 
791 	/* update current actual Actor parameters
792 	 * lacpdu->subtype                   initialized
793 	 * lacpdu->version_number            initialized
794 	 * lacpdu->tlv_type_actor_info       initialized
795 	 * lacpdu->actor_information_length  initialized
796 	 */
797 
798 	lacpdu->actor_system_priority = htons(port->actor_system_priority);
799 	lacpdu->actor_system = port->actor_system;
800 	lacpdu->actor_key = htons(port->actor_oper_port_key);
801 	lacpdu->actor_port_priority = htons(port->actor_port_priority);
802 	lacpdu->actor_port = htons(port->actor_port_number);
803 	lacpdu->actor_state = port->actor_oper_port_state;
804 	slave_dbg(port->slave->bond->dev, port->slave->dev,
805 		  "update lacpdu: actor port state %x\n",
806 		  port->actor_oper_port_state);
807 
808 	/* lacpdu->reserved_3_1              initialized
809 	 * lacpdu->tlv_type_partner_info     initialized
810 	 * lacpdu->partner_information_length initialized
811 	 */
812 
813 	lacpdu->partner_system_priority = htons(partner->system_priority);
814 	lacpdu->partner_system = partner->system;
815 	lacpdu->partner_key = htons(partner->key);
816 	lacpdu->partner_port_priority = htons(partner->port_priority);
817 	lacpdu->partner_port = htons(partner->port_number);
818 	lacpdu->partner_state = partner->port_state;
819 
820 	/* lacpdu->reserved_3_2              initialized
821 	 * lacpdu->tlv_type_collector_info   initialized
822 	 * lacpdu->collector_information_length initialized
823 	 * collector_max_delay                initialized
824 	 * reserved_12[12]                   initialized
825 	 * tlv_type_terminator               initialized
826 	 * terminator_length                 initialized
827 	 * reserved_50[50]                   initialized
828 	 */
829 }
830 
831 /* ================= main 802.3ad protocol code ========================= */
832 
833 /**
834  * ad_lacpdu_send - send out a lacpdu packet on a given port
835  * @port: the port we're looking at
836  *
837  * Returns:   0 on success
838  *          < 0 on error
839  */
ad_lacpdu_send(struct port * port)840 static int ad_lacpdu_send(struct port *port)
841 {
842 	struct slave *slave = port->slave;
843 	struct sk_buff *skb;
844 	struct lacpdu_header *lacpdu_header;
845 	int length = sizeof(struct lacpdu_header);
846 
847 	skb = dev_alloc_skb(length);
848 	if (!skb)
849 		return -ENOMEM;
850 
851 	atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_tx);
852 	atomic64_inc(&BOND_AD_INFO(slave->bond).stats.lacpdu_tx);
853 
854 	skb->dev = slave->dev;
855 	skb_reset_mac_header(skb);
856 	skb->network_header = skb->mac_header + ETH_HLEN;
857 	skb->protocol = PKT_TYPE_LACPDU;
858 	skb->priority = TC_PRIO_CONTROL;
859 
860 	lacpdu_header = skb_put(skb, length);
861 
862 	ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
863 	/* Note: source address is set to be the member's PERMANENT address,
864 	 * because we use it to identify loopback lacpdus in receive.
865 	 */
866 	ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
867 	lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
868 
869 	lacpdu_header->lacpdu = port->lacpdu;
870 
871 	dev_queue_xmit(skb);
872 
873 	return 0;
874 }
875 
876 /**
877  * ad_marker_send - send marker information/response on a given port
878  * @port: the port we're looking at
879  * @marker: marker data to send
880  *
881  * Returns:   0 on success
882  *          < 0 on error
883  */
ad_marker_send(struct port * port,struct bond_marker * marker)884 static int ad_marker_send(struct port *port, struct bond_marker *marker)
885 {
886 	struct slave *slave = port->slave;
887 	struct sk_buff *skb;
888 	struct bond_marker_header *marker_header;
889 	int length = sizeof(struct bond_marker_header);
890 
891 	skb = dev_alloc_skb(length + 16);
892 	if (!skb)
893 		return -ENOMEM;
894 
895 	switch (marker->tlv_type) {
896 	case AD_MARKER_INFORMATION_SUBTYPE:
897 		atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_tx);
898 		atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_tx);
899 		break;
900 	case AD_MARKER_RESPONSE_SUBTYPE:
901 		atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_resp_tx);
902 		atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_resp_tx);
903 		break;
904 	}
905 
906 	skb_reserve(skb, 16);
907 
908 	skb->dev = slave->dev;
909 	skb_reset_mac_header(skb);
910 	skb->network_header = skb->mac_header + ETH_HLEN;
911 	skb->protocol = PKT_TYPE_LACPDU;
912 
913 	marker_header = skb_put(skb, length);
914 
915 	ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
916 	/* Note: source address is set to be the member's PERMANENT address,
917 	 * because we use it to identify loopback MARKERs in receive.
918 	 */
919 	ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
920 	marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
921 
922 	marker_header->marker = *marker;
923 
924 	dev_queue_xmit(skb);
925 
926 	return 0;
927 }
928 
929 /**
930  * ad_mux_machine - handle a port's mux state machine
931  * @port: the port we're looking at
932  * @update_slave_arr: Does slave array need update?
933  */
ad_mux_machine(struct port * port,bool * update_slave_arr)934 static void ad_mux_machine(struct port *port, bool *update_slave_arr)
935 {
936 	mux_states_t last_state;
937 
938 	/* keep current State Machine state to compare later if it was
939 	 * changed
940 	 */
941 	last_state = port->sm_mux_state;
942 
943 	if (port->sm_vars & AD_PORT_BEGIN) {
944 		port->sm_mux_state = AD_MUX_DETACHED;
945 	} else {
946 		switch (port->sm_mux_state) {
947 		case AD_MUX_DETACHED:
948 			if ((port->sm_vars & AD_PORT_SELECTED)
949 			    || (port->sm_vars & AD_PORT_STANDBY))
950 				/* if SELECTED or STANDBY */
951 				port->sm_mux_state = AD_MUX_WAITING;
952 			break;
953 		case AD_MUX_WAITING:
954 			/* if SELECTED == FALSE return to DETACH state */
955 			if (!(port->sm_vars & AD_PORT_SELECTED)) {
956 				port->sm_vars &= ~AD_PORT_READY_N;
957 				/* in order to withhold the Selection Logic to
958 				 * check all ports READY_N value every callback
959 				 * cycle to update ready variable, we check
960 				 * READY_N and update READY here
961 				 */
962 				__set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
963 				port->sm_mux_state = AD_MUX_DETACHED;
964 				break;
965 			}
966 
967 			/* check if the wait_while_timer expired */
968 			if (port->sm_mux_timer_counter
969 			    && !(--port->sm_mux_timer_counter))
970 				port->sm_vars |= AD_PORT_READY_N;
971 
972 			/* in order to withhold the selection logic to check
973 			 * all ports READY_N value every callback cycle to
974 			 * update ready variable, we check READY_N and update
975 			 * READY here
976 			 */
977 			__set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
978 
979 			/* if the wait_while_timer expired, and the port is
980 			 * in READY state, move to ATTACHED state
981 			 */
982 			if ((port->sm_vars & AD_PORT_READY)
983 			    && !port->sm_mux_timer_counter)
984 				port->sm_mux_state = AD_MUX_ATTACHED;
985 			break;
986 		case AD_MUX_ATTACHED:
987 			/* check also if agg_select_timer expired (so the
988 			 * edable port will take place only after this timer)
989 			 */
990 			if ((port->sm_vars & AD_PORT_SELECTED) &&
991 			    (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) &&
992 			    !__check_agg_selection_timer(port)) {
993 				if (port->aggregator->is_active)
994 					port->sm_mux_state =
995 					    AD_MUX_COLLECTING_DISTRIBUTING;
996 			} else if (!(port->sm_vars & AD_PORT_SELECTED) ||
997 				   (port->sm_vars & AD_PORT_STANDBY)) {
998 				/* if UNSELECTED or STANDBY */
999 				port->sm_vars &= ~AD_PORT_READY_N;
1000 				/* in order to withhold the selection logic to
1001 				 * check all ports READY_N value every callback
1002 				 * cycle to update ready variable, we check
1003 				 * READY_N and update READY here
1004 				 */
1005 				__set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1006 				port->sm_mux_state = AD_MUX_DETACHED;
1007 			} else if (port->aggregator->is_active) {
1008 				port->actor_oper_port_state |=
1009 				    LACP_STATE_SYNCHRONIZATION;
1010 			}
1011 			break;
1012 		case AD_MUX_COLLECTING_DISTRIBUTING:
1013 			if (!(port->sm_vars & AD_PORT_SELECTED) ||
1014 			    (port->sm_vars & AD_PORT_STANDBY) ||
1015 			    !(port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) ||
1016 			    !(port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) {
1017 				port->sm_mux_state = AD_MUX_ATTACHED;
1018 			} else {
1019 				/* if port state hasn't changed make
1020 				 * sure that a collecting distributing
1021 				 * port in an active aggregator is enabled
1022 				 */
1023 				if (port->aggregator &&
1024 				    port->aggregator->is_active &&
1025 				    !__port_is_enabled(port)) {
1026 					__enable_port(port);
1027 					*update_slave_arr = true;
1028 				}
1029 			}
1030 			break;
1031 		default:
1032 			break;
1033 		}
1034 	}
1035 
1036 	/* check if the state machine was changed */
1037 	if (port->sm_mux_state != last_state) {
1038 		slave_dbg(port->slave->bond->dev, port->slave->dev,
1039 			  "Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
1040 			  port->actor_port_number,
1041 			  last_state,
1042 			  port->sm_mux_state);
1043 		switch (port->sm_mux_state) {
1044 		case AD_MUX_DETACHED:
1045 			port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1046 			ad_disable_collecting_distributing(port,
1047 							   update_slave_arr);
1048 			port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
1049 			port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
1050 			port->ntt = true;
1051 			break;
1052 		case AD_MUX_WAITING:
1053 			port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1054 			break;
1055 		case AD_MUX_ATTACHED:
1056 			if (port->aggregator->is_active)
1057 				port->actor_oper_port_state |=
1058 				    LACP_STATE_SYNCHRONIZATION;
1059 			else
1060 				port->actor_oper_port_state &=
1061 				    ~LACP_STATE_SYNCHRONIZATION;
1062 			port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
1063 			port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
1064 			ad_disable_collecting_distributing(port,
1065 							   update_slave_arr);
1066 			port->ntt = true;
1067 			break;
1068 		case AD_MUX_COLLECTING_DISTRIBUTING:
1069 			port->actor_oper_port_state |= LACP_STATE_COLLECTING;
1070 			port->actor_oper_port_state |= LACP_STATE_DISTRIBUTING;
1071 			port->actor_oper_port_state |= LACP_STATE_SYNCHRONIZATION;
1072 			ad_enable_collecting_distributing(port,
1073 							  update_slave_arr);
1074 			port->ntt = true;
1075 			break;
1076 		default:
1077 			break;
1078 		}
1079 	}
1080 }
1081 
1082 /**
1083  * ad_rx_machine - handle a port's rx State Machine
1084  * @lacpdu: the lacpdu we've received
1085  * @port: the port we're looking at
1086  *
1087  * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1088  * CURRENT. If timer expired set the state machine in the proper state.
1089  * In other cases, this function checks if we need to switch to other state.
1090  */
ad_rx_machine(struct lacpdu * lacpdu,struct port * port)1091 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1092 {
1093 	rx_states_t last_state;
1094 
1095 	/* keep current State Machine state to compare later if it was
1096 	 * changed
1097 	 */
1098 	last_state = port->sm_rx_state;
1099 
1100 	if (lacpdu) {
1101 		atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx);
1102 		atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.lacpdu_rx);
1103 	}
1104 	/* check if state machine should change state */
1105 
1106 	/* first, check if port was reinitialized */
1107 	if (port->sm_vars & AD_PORT_BEGIN) {
1108 		port->sm_rx_state = AD_RX_INITIALIZE;
1109 		port->sm_vars |= AD_PORT_CHURNED;
1110 	/* check if port is not enabled */
1111 	} else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled)
1112 		port->sm_rx_state = AD_RX_PORT_DISABLED;
1113 	/* check if new lacpdu arrived */
1114 	else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1115 		 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1116 		 (port->sm_rx_state == AD_RX_CURRENT))) {
1117 		if (port->sm_rx_state != AD_RX_CURRENT)
1118 			port->sm_vars |= AD_PORT_CHURNED;
1119 		port->sm_rx_timer_counter = 0;
1120 		port->sm_rx_state = AD_RX_CURRENT;
1121 	} else {
1122 		/* if timer is on, and if it is expired */
1123 		if (port->sm_rx_timer_counter &&
1124 		    !(--port->sm_rx_timer_counter)) {
1125 			switch (port->sm_rx_state) {
1126 			case AD_RX_EXPIRED:
1127 				port->sm_rx_state = AD_RX_DEFAULTED;
1128 				break;
1129 			case AD_RX_CURRENT:
1130 				port->sm_rx_state = AD_RX_EXPIRED;
1131 				break;
1132 			default:
1133 				break;
1134 			}
1135 		} else {
1136 			/* if no lacpdu arrived and no timer is on */
1137 			switch (port->sm_rx_state) {
1138 			case AD_RX_PORT_DISABLED:
1139 				if (port->is_enabled &&
1140 				    (port->sm_vars & AD_PORT_LACP_ENABLED))
1141 					port->sm_rx_state = AD_RX_EXPIRED;
1142 				else if (port->is_enabled
1143 					 && ((port->sm_vars
1144 					      & AD_PORT_LACP_ENABLED) == 0))
1145 					port->sm_rx_state = AD_RX_LACP_DISABLED;
1146 				break;
1147 			default:
1148 				break;
1149 
1150 			}
1151 		}
1152 	}
1153 
1154 	/* check if the State machine was changed or new lacpdu arrived */
1155 	if ((port->sm_rx_state != last_state) || (lacpdu)) {
1156 		slave_dbg(port->slave->bond->dev, port->slave->dev,
1157 			  "Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1158 			  port->actor_port_number,
1159 			  last_state,
1160 			  port->sm_rx_state);
1161 		switch (port->sm_rx_state) {
1162 		case AD_RX_INITIALIZE:
1163 			if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
1164 				port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1165 			else
1166 				port->sm_vars |= AD_PORT_LACP_ENABLED;
1167 			port->sm_vars &= ~AD_PORT_SELECTED;
1168 			__record_default(port);
1169 			port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1170 			port->sm_rx_state = AD_RX_PORT_DISABLED;
1171 
1172 			fallthrough;
1173 		case AD_RX_PORT_DISABLED:
1174 			port->sm_vars &= ~AD_PORT_MATCHED;
1175 			break;
1176 		case AD_RX_LACP_DISABLED:
1177 			port->sm_vars &= ~AD_PORT_SELECTED;
1178 			__record_default(port);
1179 			port->partner_oper.port_state &= ~LACP_STATE_AGGREGATION;
1180 			port->sm_vars |= AD_PORT_MATCHED;
1181 			port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1182 			break;
1183 		case AD_RX_EXPIRED:
1184 			/* Reset of the Synchronization flag (Standard 43.4.12)
1185 			 * This reset cause to disable this port in the
1186 			 * COLLECTING_DISTRIBUTING state of the mux machine in
1187 			 * case of EXPIRED even if LINK_DOWN didn't arrive for
1188 			 * the port.
1189 			 */
1190 			port->partner_oper.port_state &= ~LACP_STATE_SYNCHRONIZATION;
1191 			port->sm_vars &= ~AD_PORT_MATCHED;
1192 			port->partner_oper.port_state |= LACP_STATE_LACP_TIMEOUT;
1193 			port->partner_oper.port_state |= LACP_STATE_LACP_ACTIVITY;
1194 			port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1195 			port->actor_oper_port_state |= LACP_STATE_EXPIRED;
1196 			port->sm_vars |= AD_PORT_CHURNED;
1197 			break;
1198 		case AD_RX_DEFAULTED:
1199 			__update_default_selected(port);
1200 			__record_default(port);
1201 			port->sm_vars |= AD_PORT_MATCHED;
1202 			port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1203 			break;
1204 		case AD_RX_CURRENT:
1205 			/* detect loopback situation */
1206 			if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1207 					      &(port->actor_system))) {
1208 				slave_err(port->slave->bond->dev, port->slave->dev, "An illegal loopback occurred on slave\n"
1209 					  "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n");
1210 				return;
1211 			}
1212 			__update_selected(lacpdu, port);
1213 			__update_ntt(lacpdu, port);
1214 			__record_pdu(lacpdu, port);
1215 			port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT));
1216 			port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1217 			break;
1218 		default:
1219 			break;
1220 		}
1221 	}
1222 }
1223 
1224 /**
1225  * ad_churn_machine - handle port churn's state machine
1226  * @port: the port we're looking at
1227  *
1228  */
ad_churn_machine(struct port * port)1229 static void ad_churn_machine(struct port *port)
1230 {
1231 	if (port->sm_vars & AD_PORT_CHURNED) {
1232 		port->sm_vars &= ~AD_PORT_CHURNED;
1233 		port->sm_churn_actor_state = AD_CHURN_MONITOR;
1234 		port->sm_churn_partner_state = AD_CHURN_MONITOR;
1235 		port->sm_churn_actor_timer_counter =
1236 			__ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1237 		port->sm_churn_partner_timer_counter =
1238 			 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1239 		return;
1240 	}
1241 	if (port->sm_churn_actor_timer_counter &&
1242 	    !(--port->sm_churn_actor_timer_counter) &&
1243 	    port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1244 		if (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION) {
1245 			port->sm_churn_actor_state = AD_NO_CHURN;
1246 		} else {
1247 			port->churn_actor_count++;
1248 			port->sm_churn_actor_state = AD_CHURN;
1249 		}
1250 	}
1251 	if (port->sm_churn_partner_timer_counter &&
1252 	    !(--port->sm_churn_partner_timer_counter) &&
1253 	    port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1254 		if (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) {
1255 			port->sm_churn_partner_state = AD_NO_CHURN;
1256 		} else {
1257 			port->churn_partner_count++;
1258 			port->sm_churn_partner_state = AD_CHURN;
1259 		}
1260 	}
1261 }
1262 
1263 /**
1264  * ad_tx_machine - handle a port's tx state machine
1265  * @port: the port we're looking at
1266  */
ad_tx_machine(struct port * port)1267 static void ad_tx_machine(struct port *port)
1268 {
1269 	/* check if tx timer expired, to verify that we do not send more than
1270 	 * 3 packets per second
1271 	 */
1272 	if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1273 		/* check if there is something to send */
1274 		if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1275 			__update_lacpdu_from_port(port);
1276 
1277 			if (ad_lacpdu_send(port) >= 0) {
1278 				slave_dbg(port->slave->bond->dev,
1279 					  port->slave->dev,
1280 					  "Sent LACPDU on port %d\n",
1281 					  port->actor_port_number);
1282 
1283 				/* mark ntt as false, so it will not be sent
1284 				 * again until demanded
1285 				 */
1286 				port->ntt = false;
1287 			}
1288 		}
1289 		/* restart tx timer(to verify that we will not exceed
1290 		 * AD_MAX_TX_IN_SECOND
1291 		 */
1292 		port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1293 	}
1294 }
1295 
1296 /**
1297  * ad_periodic_machine - handle a port's periodic state machine
1298  * @port: the port we're looking at
1299  * @bond_params: bond parameters we will use
1300  *
1301  * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1302  */
ad_periodic_machine(struct port * port,struct bond_params * bond_params)1303 static void ad_periodic_machine(struct port *port, struct bond_params *bond_params)
1304 {
1305 	periodic_states_t last_state;
1306 
1307 	/* keep current state machine state to compare later if it was changed */
1308 	last_state = port->sm_periodic_state;
1309 
1310 	/* check if port was reinitialized */
1311 	if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1312 	    (!(port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & LACP_STATE_LACP_ACTIVITY)) ||
1313 	    !bond_params->lacp_active) {
1314 		port->sm_periodic_state = AD_NO_PERIODIC;
1315 	}
1316 	/* check if state machine should change state */
1317 	else if (port->sm_periodic_timer_counter) {
1318 		/* check if periodic state machine expired */
1319 		if (!(--port->sm_periodic_timer_counter)) {
1320 			/* if expired then do tx */
1321 			port->sm_periodic_state = AD_PERIODIC_TX;
1322 		} else {
1323 			/* If not expired, check if there is some new timeout
1324 			 * parameter from the partner state
1325 			 */
1326 			switch (port->sm_periodic_state) {
1327 			case AD_FAST_PERIODIC:
1328 				if (!(port->partner_oper.port_state
1329 				      & LACP_STATE_LACP_TIMEOUT))
1330 					port->sm_periodic_state = AD_SLOW_PERIODIC;
1331 				break;
1332 			case AD_SLOW_PERIODIC:
1333 				if ((port->partner_oper.port_state & LACP_STATE_LACP_TIMEOUT)) {
1334 					port->sm_periodic_timer_counter = 0;
1335 					port->sm_periodic_state = AD_PERIODIC_TX;
1336 				}
1337 				break;
1338 			default:
1339 				break;
1340 			}
1341 		}
1342 	} else {
1343 		switch (port->sm_periodic_state) {
1344 		case AD_NO_PERIODIC:
1345 			port->sm_periodic_state = AD_FAST_PERIODIC;
1346 			break;
1347 		case AD_PERIODIC_TX:
1348 			if (!(port->partner_oper.port_state &
1349 			    LACP_STATE_LACP_TIMEOUT))
1350 				port->sm_periodic_state = AD_SLOW_PERIODIC;
1351 			else
1352 				port->sm_periodic_state = AD_FAST_PERIODIC;
1353 			break;
1354 		default:
1355 			break;
1356 		}
1357 	}
1358 
1359 	/* check if the state machine was changed */
1360 	if (port->sm_periodic_state != last_state) {
1361 		slave_dbg(port->slave->bond->dev, port->slave->dev,
1362 			  "Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1363 			  port->actor_port_number, last_state,
1364 			  port->sm_periodic_state);
1365 		switch (port->sm_periodic_state) {
1366 		case AD_NO_PERIODIC:
1367 			port->sm_periodic_timer_counter = 0;
1368 			break;
1369 		case AD_FAST_PERIODIC:
1370 			/* decrement 1 tick we lost in the PERIODIC_TX cycle */
1371 			port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1372 			break;
1373 		case AD_SLOW_PERIODIC:
1374 			/* decrement 1 tick we lost in the PERIODIC_TX cycle */
1375 			port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1376 			break;
1377 		case AD_PERIODIC_TX:
1378 			port->ntt = true;
1379 			break;
1380 		default:
1381 			break;
1382 		}
1383 	}
1384 }
1385 
1386 /**
1387  * ad_port_selection_logic - select aggregation groups
1388  * @port: the port we're looking at
1389  * @update_slave_arr: Does slave array need update?
1390  *
1391  * Select aggregation groups, and assign each port for it's aggregetor. The
1392  * selection logic is called in the inititalization (after all the handshkes),
1393  * and after every lacpdu receive (if selected is off).
1394  */
ad_port_selection_logic(struct port * port,bool * update_slave_arr)1395 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
1396 {
1397 	struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1398 	struct port *last_port = NULL, *curr_port;
1399 	struct list_head *iter;
1400 	struct bonding *bond;
1401 	struct slave *slave;
1402 	int found = 0;
1403 
1404 	/* if the port is already Selected, do nothing */
1405 	if (port->sm_vars & AD_PORT_SELECTED)
1406 		return;
1407 
1408 	bond = __get_bond_by_port(port);
1409 
1410 	/* if the port is connected to other aggregator, detach it */
1411 	if (port->aggregator) {
1412 		/* detach the port from its former aggregator */
1413 		temp_aggregator = port->aggregator;
1414 		for (curr_port = temp_aggregator->lag_ports; curr_port;
1415 		     last_port = curr_port,
1416 		     curr_port = curr_port->next_port_in_aggregator) {
1417 			if (curr_port == port) {
1418 				temp_aggregator->num_of_ports--;
1419 				/* if it is the first port attached to the
1420 				 * aggregator
1421 				 */
1422 				if (!last_port) {
1423 					temp_aggregator->lag_ports =
1424 						port->next_port_in_aggregator;
1425 				} else {
1426 					/* not the first port attached to the
1427 					 * aggregator
1428 					 */
1429 					last_port->next_port_in_aggregator =
1430 						port->next_port_in_aggregator;
1431 				}
1432 
1433 				/* clear the port's relations to this
1434 				 * aggregator
1435 				 */
1436 				port->aggregator = NULL;
1437 				port->next_port_in_aggregator = NULL;
1438 				port->actor_port_aggregator_identifier = 0;
1439 
1440 				slave_dbg(bond->dev, port->slave->dev, "Port %d left LAG %d\n",
1441 					  port->actor_port_number,
1442 					  temp_aggregator->aggregator_identifier);
1443 				/* if the aggregator is empty, clear its
1444 				 * parameters, and set it ready to be attached
1445 				 */
1446 				if (!temp_aggregator->lag_ports)
1447 					ad_clear_agg(temp_aggregator);
1448 				break;
1449 			}
1450 		}
1451 		if (!curr_port) {
1452 			/* meaning: the port was related to an aggregator
1453 			 * but was not on the aggregator port list
1454 			 */
1455 			net_warn_ratelimited("%s: (slave %s): Warning: Port %d was related to aggregator %d but was not on its port list\n",
1456 					     port->slave->bond->dev->name,
1457 					     port->slave->dev->name,
1458 					     port->actor_port_number,
1459 					     port->aggregator->aggregator_identifier);
1460 		}
1461 	}
1462 	/* search on all aggregators for a suitable aggregator for this port */
1463 	bond_for_each_slave(bond, slave, iter) {
1464 		aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1465 
1466 		/* keep a free aggregator for later use(if needed) */
1467 		if (!aggregator->lag_ports) {
1468 			if (!free_aggregator)
1469 				free_aggregator = aggregator;
1470 			continue;
1471 		}
1472 		/* check if current aggregator suits us */
1473 		if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1474 		     MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1475 		     (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1476 		     (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1477 		    ) &&
1478 		    ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1479 		      !aggregator->is_individual)  /* but is not individual OR */
1480 		    )
1481 		   ) {
1482 			/* attach to the founded aggregator */
1483 			port->aggregator = aggregator;
1484 			port->actor_port_aggregator_identifier =
1485 				port->aggregator->aggregator_identifier;
1486 			port->next_port_in_aggregator = aggregator->lag_ports;
1487 			port->aggregator->num_of_ports++;
1488 			aggregator->lag_ports = port;
1489 			slave_dbg(bond->dev, slave->dev, "Port %d joined LAG %d (existing LAG)\n",
1490 				  port->actor_port_number,
1491 				  port->aggregator->aggregator_identifier);
1492 
1493 			/* mark this port as selected */
1494 			port->sm_vars |= AD_PORT_SELECTED;
1495 			found = 1;
1496 			break;
1497 		}
1498 	}
1499 
1500 	/* the port couldn't find an aggregator - attach it to a new
1501 	 * aggregator
1502 	 */
1503 	if (!found) {
1504 		if (free_aggregator) {
1505 			/* assign port a new aggregator */
1506 			port->aggregator = free_aggregator;
1507 			port->actor_port_aggregator_identifier =
1508 				port->aggregator->aggregator_identifier;
1509 
1510 			/* update the new aggregator's parameters
1511 			 * if port was responsed from the end-user
1512 			 */
1513 			if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
1514 				/* if port is full duplex */
1515 				port->aggregator->is_individual = false;
1516 			else
1517 				port->aggregator->is_individual = true;
1518 
1519 			port->aggregator->actor_admin_aggregator_key =
1520 				port->actor_admin_port_key;
1521 			port->aggregator->actor_oper_aggregator_key =
1522 				port->actor_oper_port_key;
1523 			port->aggregator->partner_system =
1524 				port->partner_oper.system;
1525 			port->aggregator->partner_system_priority =
1526 				port->partner_oper.system_priority;
1527 			port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1528 			port->aggregator->receive_state = 1;
1529 			port->aggregator->transmit_state = 1;
1530 			port->aggregator->lag_ports = port;
1531 			port->aggregator->num_of_ports++;
1532 
1533 			/* mark this port as selected */
1534 			port->sm_vars |= AD_PORT_SELECTED;
1535 
1536 			slave_dbg(bond->dev, port->slave->dev, "Port %d joined LAG %d (new LAG)\n",
1537 				  port->actor_port_number,
1538 				  port->aggregator->aggregator_identifier);
1539 		} else {
1540 			slave_err(bond->dev, port->slave->dev,
1541 				  "Port %d did not find a suitable aggregator\n",
1542 				  port->actor_port_number);
1543 			return;
1544 		}
1545 	}
1546 	/* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1547 	 * in all aggregator's ports, else set ready=FALSE in all
1548 	 * aggregator's ports
1549 	 */
1550 	__set_agg_ports_ready(port->aggregator,
1551 			      __agg_ports_are_ready(port->aggregator));
1552 
1553 	aggregator = __get_first_agg(port);
1554 	ad_agg_selection_logic(aggregator, update_slave_arr);
1555 
1556 	if (!port->aggregator->is_active)
1557 		port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1558 }
1559 
1560 /* Decide if "agg" is a better choice for the new active aggregator that
1561  * the current best, according to the ad_select policy.
1562  */
ad_agg_selection_test(struct aggregator * best,struct aggregator * curr)1563 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1564 						struct aggregator *curr)
1565 {
1566 	/* 0. If no best, select current.
1567 	 *
1568 	 * 1. If the current agg is not individual, and the best is
1569 	 *    individual, select current.
1570 	 *
1571 	 * 2. If current agg is individual and the best is not, keep best.
1572 	 *
1573 	 * 3. Therefore, current and best are both individual or both not
1574 	 *    individual, so:
1575 	 *
1576 	 * 3a. If current agg partner replied, and best agg partner did not,
1577 	 *     select current.
1578 	 *
1579 	 * 3b. If current agg partner did not reply and best agg partner
1580 	 *     did reply, keep best.
1581 	 *
1582 	 * 4.  Therefore, current and best both have partner replies or
1583 	 *     both do not, so perform selection policy:
1584 	 *
1585 	 * BOND_AD_COUNT: Select by count of ports.  If count is equal,
1586 	 *     select by bandwidth.
1587 	 *
1588 	 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1589 	 */
1590 	if (!best)
1591 		return curr;
1592 
1593 	if (!curr->is_individual && best->is_individual)
1594 		return curr;
1595 
1596 	if (curr->is_individual && !best->is_individual)
1597 		return best;
1598 
1599 	if (__agg_has_partner(curr) && !__agg_has_partner(best))
1600 		return curr;
1601 
1602 	if (!__agg_has_partner(curr) && __agg_has_partner(best))
1603 		return best;
1604 
1605 	switch (__get_agg_selection_mode(curr->lag_ports)) {
1606 	case BOND_AD_COUNT:
1607 		if (__agg_active_ports(curr) > __agg_active_ports(best))
1608 			return curr;
1609 
1610 		if (__agg_active_ports(curr) < __agg_active_ports(best))
1611 			return best;
1612 
1613 		fallthrough;
1614 	case BOND_AD_STABLE:
1615 	case BOND_AD_BANDWIDTH:
1616 		if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1617 			return curr;
1618 
1619 		break;
1620 
1621 	default:
1622 		net_warn_ratelimited("%s: (slave %s): Impossible agg select mode %d\n",
1623 				     curr->slave->bond->dev->name,
1624 				     curr->slave->dev->name,
1625 				     __get_agg_selection_mode(curr->lag_ports));
1626 		break;
1627 	}
1628 
1629 	return best;
1630 }
1631 
agg_device_up(const struct aggregator * agg)1632 static int agg_device_up(const struct aggregator *agg)
1633 {
1634 	struct port *port = agg->lag_ports;
1635 
1636 	if (!port)
1637 		return 0;
1638 
1639 	for (port = agg->lag_ports; port;
1640 	     port = port->next_port_in_aggregator) {
1641 		if (netif_running(port->slave->dev) &&
1642 		    netif_carrier_ok(port->slave->dev))
1643 			return 1;
1644 	}
1645 
1646 	return 0;
1647 }
1648 
1649 /**
1650  * ad_agg_selection_logic - select an aggregation group for a team
1651  * @agg: the aggregator we're looking at
1652  * @update_slave_arr: Does slave array need update?
1653  *
1654  * It is assumed that only one aggregator may be selected for a team.
1655  *
1656  * The logic of this function is to select the aggregator according to
1657  * the ad_select policy:
1658  *
1659  * BOND_AD_STABLE: select the aggregator with the most ports attached to
1660  * it, and to reselect the active aggregator only if the previous
1661  * aggregator has no more ports related to it.
1662  *
1663  * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1664  * bandwidth, and reselect whenever a link state change takes place or the
1665  * set of slaves in the bond changes.
1666  *
1667  * BOND_AD_COUNT: select the aggregator with largest number of ports
1668  * (slaves), and reselect whenever a link state change takes place or the
1669  * set of slaves in the bond changes.
1670  *
1671  * FIXME: this function MUST be called with the first agg in the bond, or
1672  * __get_active_agg() won't work correctly. This function should be better
1673  * called with the bond itself, and retrieve the first agg from it.
1674  */
ad_agg_selection_logic(struct aggregator * agg,bool * update_slave_arr)1675 static void ad_agg_selection_logic(struct aggregator *agg,
1676 				   bool *update_slave_arr)
1677 {
1678 	struct aggregator *best, *active, *origin;
1679 	struct bonding *bond = agg->slave->bond;
1680 	struct list_head *iter;
1681 	struct slave *slave;
1682 	struct port *port;
1683 
1684 	rcu_read_lock();
1685 	origin = agg;
1686 	active = __get_active_agg(agg);
1687 	best = (active && agg_device_up(active)) ? active : NULL;
1688 
1689 	bond_for_each_slave_rcu(bond, slave, iter) {
1690 		agg = &(SLAVE_AD_INFO(slave)->aggregator);
1691 
1692 		agg->is_active = 0;
1693 
1694 		if (__agg_active_ports(agg) && agg_device_up(agg))
1695 			best = ad_agg_selection_test(best, agg);
1696 	}
1697 
1698 	if (best &&
1699 	    __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1700 		/* For the STABLE policy, don't replace the old active
1701 		 * aggregator if it's still active (it has an answering
1702 		 * partner) or if both the best and active don't have an
1703 		 * answering partner.
1704 		 */
1705 		if (active && active->lag_ports &&
1706 		    __agg_active_ports(active) &&
1707 		    (__agg_has_partner(active) ||
1708 		     (!__agg_has_partner(active) &&
1709 		     !__agg_has_partner(best)))) {
1710 			if (!(!active->actor_oper_aggregator_key &&
1711 			      best->actor_oper_aggregator_key)) {
1712 				best = NULL;
1713 				active->is_active = 1;
1714 			}
1715 		}
1716 	}
1717 
1718 	if (best && (best == active)) {
1719 		best = NULL;
1720 		active->is_active = 1;
1721 	}
1722 
1723 	/* if there is new best aggregator, activate it */
1724 	if (best) {
1725 		netdev_dbg(bond->dev, "(slave %s): best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1726 			   best->slave ? best->slave->dev->name : "NULL",
1727 			   best->aggregator_identifier, best->num_of_ports,
1728 			   best->actor_oper_aggregator_key,
1729 			   best->partner_oper_aggregator_key,
1730 			   best->is_individual, best->is_active);
1731 		netdev_dbg(bond->dev, "(slave %s): best ports %p slave %p\n",
1732 			   best->slave ? best->slave->dev->name : "NULL",
1733 			   best->lag_ports, best->slave);
1734 
1735 		bond_for_each_slave_rcu(bond, slave, iter) {
1736 			agg = &(SLAVE_AD_INFO(slave)->aggregator);
1737 
1738 			slave_dbg(bond->dev, slave->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1739 				  agg->aggregator_identifier, agg->num_of_ports,
1740 				  agg->actor_oper_aggregator_key,
1741 				  agg->partner_oper_aggregator_key,
1742 				  agg->is_individual, agg->is_active);
1743 		}
1744 
1745 		/* check if any partner replies */
1746 		if (best->is_individual)
1747 			net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1748 					     bond->dev->name);
1749 
1750 		best->is_active = 1;
1751 		netdev_dbg(bond->dev, "(slave %s): LAG %d chosen as the active LAG\n",
1752 			   best->slave ? best->slave->dev->name : "NULL",
1753 			   best->aggregator_identifier);
1754 		netdev_dbg(bond->dev, "(slave %s): Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1755 			   best->slave ? best->slave->dev->name : "NULL",
1756 			   best->aggregator_identifier, best->num_of_ports,
1757 			   best->actor_oper_aggregator_key,
1758 			   best->partner_oper_aggregator_key,
1759 			   best->is_individual, best->is_active);
1760 
1761 		/* disable the ports that were related to the former
1762 		 * active_aggregator
1763 		 */
1764 		if (active) {
1765 			for (port = active->lag_ports; port;
1766 			     port = port->next_port_in_aggregator) {
1767 				__disable_port(port);
1768 			}
1769 		}
1770 		/* Slave array needs update. */
1771 		*update_slave_arr = true;
1772 	}
1773 
1774 	/* if the selected aggregator is of join individuals
1775 	 * (partner_system is NULL), enable their ports
1776 	 */
1777 	active = __get_active_agg(origin);
1778 
1779 	if (active) {
1780 		if (!__agg_has_partner(active)) {
1781 			for (port = active->lag_ports; port;
1782 			     port = port->next_port_in_aggregator) {
1783 				__enable_port(port);
1784 			}
1785 			*update_slave_arr = true;
1786 		}
1787 	}
1788 
1789 	rcu_read_unlock();
1790 
1791 	bond_3ad_set_carrier(bond);
1792 }
1793 
1794 /**
1795  * ad_clear_agg - clear a given aggregator's parameters
1796  * @aggregator: the aggregator we're looking at
1797  */
ad_clear_agg(struct aggregator * aggregator)1798 static void ad_clear_agg(struct aggregator *aggregator)
1799 {
1800 	if (aggregator) {
1801 		aggregator->is_individual = false;
1802 		aggregator->actor_admin_aggregator_key = 0;
1803 		aggregator->actor_oper_aggregator_key = 0;
1804 		eth_zero_addr(aggregator->partner_system.mac_addr_value);
1805 		aggregator->partner_system_priority = 0;
1806 		aggregator->partner_oper_aggregator_key = 0;
1807 		aggregator->receive_state = 0;
1808 		aggregator->transmit_state = 0;
1809 		aggregator->lag_ports = NULL;
1810 		aggregator->is_active = 0;
1811 		aggregator->num_of_ports = 0;
1812 		pr_debug("%s: LAG %d was cleared\n",
1813 			 aggregator->slave ?
1814 			 aggregator->slave->dev->name : "NULL",
1815 			 aggregator->aggregator_identifier);
1816 	}
1817 }
1818 
1819 /**
1820  * ad_initialize_agg - initialize a given aggregator's parameters
1821  * @aggregator: the aggregator we're looking at
1822  */
ad_initialize_agg(struct aggregator * aggregator)1823 static void ad_initialize_agg(struct aggregator *aggregator)
1824 {
1825 	if (aggregator) {
1826 		ad_clear_agg(aggregator);
1827 
1828 		eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1829 		aggregator->aggregator_identifier = 0;
1830 		aggregator->slave = NULL;
1831 	}
1832 }
1833 
1834 /**
1835  * ad_initialize_port - initialize a given port's parameters
1836  * @port: the port we're looking at
1837  * @lacp_fast: boolean. whether fast periodic should be used
1838  */
ad_initialize_port(struct port * port,int lacp_fast)1839 static void ad_initialize_port(struct port *port, int lacp_fast)
1840 {
1841 	static const struct port_params tmpl = {
1842 		.system_priority = 0xffff,
1843 		.key             = 1,
1844 		.port_number     = 1,
1845 		.port_priority   = 0xff,
1846 		.port_state      = 1,
1847 	};
1848 	static const struct lacpdu lacpdu = {
1849 		.subtype		= 0x01,
1850 		.version_number = 0x01,
1851 		.tlv_type_actor_info = 0x01,
1852 		.actor_information_length = 0x14,
1853 		.tlv_type_partner_info = 0x02,
1854 		.partner_information_length = 0x14,
1855 		.tlv_type_collector_info = 0x03,
1856 		.collector_information_length = 0x10,
1857 		.collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1858 	};
1859 
1860 	if (port) {
1861 		port->actor_port_priority = 0xff;
1862 		port->actor_port_aggregator_identifier = 0;
1863 		port->ntt = false;
1864 		port->actor_admin_port_state = LACP_STATE_AGGREGATION |
1865 					       LACP_STATE_LACP_ACTIVITY;
1866 		port->actor_oper_port_state  = LACP_STATE_AGGREGATION |
1867 					       LACP_STATE_LACP_ACTIVITY;
1868 
1869 		if (lacp_fast)
1870 			port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
1871 
1872 		memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1873 		memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1874 
1875 		port->is_enabled = true;
1876 		/* private parameters */
1877 		port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1878 		port->sm_rx_state = 0;
1879 		port->sm_rx_timer_counter = 0;
1880 		port->sm_periodic_state = 0;
1881 		port->sm_periodic_timer_counter = 0;
1882 		port->sm_mux_state = 0;
1883 		port->sm_mux_timer_counter = 0;
1884 		port->sm_tx_state = 0;
1885 		port->aggregator = NULL;
1886 		port->next_port_in_aggregator = NULL;
1887 		port->transaction_id = 0;
1888 
1889 		port->sm_churn_actor_timer_counter = 0;
1890 		port->sm_churn_actor_state = 0;
1891 		port->churn_actor_count = 0;
1892 		port->sm_churn_partner_timer_counter = 0;
1893 		port->sm_churn_partner_state = 0;
1894 		port->churn_partner_count = 0;
1895 
1896 		memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1897 	}
1898 }
1899 
1900 /**
1901  * ad_enable_collecting_distributing - enable a port's transmit/receive
1902  * @port: the port we're looking at
1903  * @update_slave_arr: Does slave array need update?
1904  *
1905  * Enable @port if it's in an active aggregator
1906  */
ad_enable_collecting_distributing(struct port * port,bool * update_slave_arr)1907 static void ad_enable_collecting_distributing(struct port *port,
1908 					      bool *update_slave_arr)
1909 {
1910 	if (port->aggregator->is_active) {
1911 		slave_dbg(port->slave->bond->dev, port->slave->dev,
1912 			  "Enabling port %d (LAG %d)\n",
1913 			  port->actor_port_number,
1914 			  port->aggregator->aggregator_identifier);
1915 		__enable_port(port);
1916 		/* Slave array needs update */
1917 		*update_slave_arr = true;
1918 	}
1919 }
1920 
1921 /**
1922  * ad_disable_collecting_distributing - disable a port's transmit/receive
1923  * @port: the port we're looking at
1924  * @update_slave_arr: Does slave array need update?
1925  */
ad_disable_collecting_distributing(struct port * port,bool * update_slave_arr)1926 static void ad_disable_collecting_distributing(struct port *port,
1927 					       bool *update_slave_arr)
1928 {
1929 	if (port->aggregator &&
1930 	    !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1931 			       &(null_mac_addr))) {
1932 		slave_dbg(port->slave->bond->dev, port->slave->dev,
1933 			  "Disabling port %d (LAG %d)\n",
1934 			  port->actor_port_number,
1935 			  port->aggregator->aggregator_identifier);
1936 		__disable_port(port);
1937 		/* Slave array needs an update */
1938 		*update_slave_arr = true;
1939 	}
1940 }
1941 
1942 /**
1943  * ad_marker_info_received - handle receive of a Marker information frame
1944  * @marker_info: Marker info received
1945  * @port: the port we're looking at
1946  */
ad_marker_info_received(struct bond_marker * marker_info,struct port * port)1947 static void ad_marker_info_received(struct bond_marker *marker_info,
1948 				    struct port *port)
1949 {
1950 	struct bond_marker marker;
1951 
1952 	atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx);
1953 	atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_rx);
1954 
1955 	/* copy the received marker data to the response marker */
1956 	memcpy(&marker, marker_info, sizeof(struct bond_marker));
1957 	/* change the marker subtype to marker response */
1958 	marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1959 
1960 	/* send the marker response */
1961 	if (ad_marker_send(port, &marker) >= 0)
1962 		slave_dbg(port->slave->bond->dev, port->slave->dev,
1963 			  "Sent Marker Response on port %d\n",
1964 			  port->actor_port_number);
1965 }
1966 
1967 /**
1968  * ad_marker_response_received - handle receive of a marker response frame
1969  * @marker: marker PDU received
1970  * @port: the port we're looking at
1971  *
1972  * This function does nothing since we decided not to implement send and handle
1973  * response for marker PDU's, in this stage, but only to respond to marker
1974  * information.
1975  */
ad_marker_response_received(struct bond_marker * marker,struct port * port)1976 static void ad_marker_response_received(struct bond_marker *marker,
1977 					struct port *port)
1978 {
1979 	atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_resp_rx);
1980 	atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_resp_rx);
1981 
1982 	/* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1983 }
1984 
1985 /* ========= AD exported functions to the main bonding code ========= */
1986 
1987 /* Check aggregators status in team every T seconds */
1988 #define AD_AGGREGATOR_SELECTION_TIMER  8
1989 
1990 /**
1991  * bond_3ad_initiate_agg_selection - initate aggregator selection
1992  * @bond: bonding struct
1993  * @timeout: timeout value to set
1994  *
1995  * Set the aggregation selection timer, to initiate an agg selection in
1996  * the very near future.  Called during first initialization, and during
1997  * any down to up transitions of the bond.
1998  */
bond_3ad_initiate_agg_selection(struct bonding * bond,int timeout)1999 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
2000 {
2001 	atomic_set(&BOND_AD_INFO(bond).agg_select_timer, timeout);
2002 }
2003 
2004 /**
2005  * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
2006  * @bond: bonding struct to work on
2007  *
2008  * Can be called only after the mac address of the bond is set.
2009  */
bond_3ad_initialize(struct bonding * bond)2010 void bond_3ad_initialize(struct bonding *bond)
2011 {
2012 	BOND_AD_INFO(bond).aggregator_identifier = 0;
2013 	BOND_AD_INFO(bond).system.sys_priority =
2014 		bond->params.ad_actor_sys_prio;
2015 	if (is_zero_ether_addr(bond->params.ad_actor_system))
2016 		BOND_AD_INFO(bond).system.sys_mac_addr =
2017 		    *((struct mac_addr *)bond->dev->dev_addr);
2018 	else
2019 		BOND_AD_INFO(bond).system.sys_mac_addr =
2020 		    *((struct mac_addr *)bond->params.ad_actor_system);
2021 
2022 	bond_3ad_initiate_agg_selection(bond,
2023 					AD_AGGREGATOR_SELECTION_TIMER *
2024 					ad_ticks_per_sec);
2025 }
2026 
2027 /**
2028  * bond_3ad_bind_slave - initialize a slave's port
2029  * @slave: slave struct to work on
2030  *
2031  * Returns:   0 on success
2032  *          < 0 on error
2033  */
bond_3ad_bind_slave(struct slave * slave)2034 void bond_3ad_bind_slave(struct slave *slave)
2035 {
2036 	struct bonding *bond = bond_get_bond_by_slave(slave);
2037 	struct port *port;
2038 	struct aggregator *aggregator;
2039 
2040 	/* check that the slave has not been initialized yet. */
2041 	if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2042 
2043 		/* port initialization */
2044 		port = &(SLAVE_AD_INFO(slave)->port);
2045 
2046 		ad_initialize_port(port, bond->params.lacp_fast);
2047 
2048 		port->slave = slave;
2049 		port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2050 		/* key is determined according to the link speed, duplex and
2051 		 * user key
2052 		 */
2053 		port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2054 		ad_update_actor_keys(port, false);
2055 		/* actor system is the bond's system */
2056 		__ad_actor_update_port(port);
2057 		/* tx timer(to verify that no more than MAX_TX_IN_SECOND
2058 		 * lacpdu's are sent in one second)
2059 		 */
2060 		port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2061 
2062 		__disable_port(port);
2063 
2064 		/* aggregator initialization */
2065 		aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2066 
2067 		ad_initialize_agg(aggregator);
2068 
2069 		aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2070 		aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2071 		aggregator->slave = slave;
2072 		aggregator->is_active = 0;
2073 		aggregator->num_of_ports = 0;
2074 	}
2075 }
2076 
2077 /**
2078  * bond_3ad_unbind_slave - deinitialize a slave's port
2079  * @slave: slave struct to work on
2080  *
2081  * Search for the aggregator that is related to this port, remove the
2082  * aggregator and assign another aggregator for other port related to it
2083  * (if any), and remove the port.
2084  */
bond_3ad_unbind_slave(struct slave * slave)2085 void bond_3ad_unbind_slave(struct slave *slave)
2086 {
2087 	struct port *port, *prev_port, *temp_port;
2088 	struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2089 	int select_new_active_agg = 0;
2090 	struct bonding *bond = slave->bond;
2091 	struct slave *slave_iter;
2092 	struct list_head *iter;
2093 	bool dummy_slave_update; /* Ignore this value as caller updates array */
2094 
2095 	/* Sync against bond_3ad_state_machine_handler() */
2096 	spin_lock_bh(&bond->mode_lock);
2097 	aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2098 	port = &(SLAVE_AD_INFO(slave)->port);
2099 
2100 	/* if slave is null, the whole port is not initialized */
2101 	if (!port->slave) {
2102 		slave_warn(bond->dev, slave->dev, "Trying to unbind an uninitialized port\n");
2103 		goto out;
2104 	}
2105 
2106 	slave_dbg(bond->dev, slave->dev, "Unbinding Link Aggregation Group %d\n",
2107 		  aggregator->aggregator_identifier);
2108 
2109 	/* Tell the partner that this port is not suitable for aggregation */
2110 	port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
2111 	port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
2112 	port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
2113 	port->actor_oper_port_state &= ~LACP_STATE_AGGREGATION;
2114 	__update_lacpdu_from_port(port);
2115 	ad_lacpdu_send(port);
2116 
2117 	/* check if this aggregator is occupied */
2118 	if (aggregator->lag_ports) {
2119 		/* check if there are other ports related to this aggregator
2120 		 * except the port related to this slave(thats ensure us that
2121 		 * there is a reason to search for new aggregator, and that we
2122 		 * will find one
2123 		 */
2124 		if ((aggregator->lag_ports != port) ||
2125 		    (aggregator->lag_ports->next_port_in_aggregator)) {
2126 			/* find new aggregator for the related port(s) */
2127 			bond_for_each_slave(bond, slave_iter, iter) {
2128 				new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2129 				/* if the new aggregator is empty, or it is
2130 				 * connected to our port only
2131 				 */
2132 				if (!new_aggregator->lag_ports ||
2133 				    ((new_aggregator->lag_ports == port) &&
2134 				     !new_aggregator->lag_ports->next_port_in_aggregator))
2135 					break;
2136 			}
2137 			if (!slave_iter)
2138 				new_aggregator = NULL;
2139 
2140 			/* if new aggregator found, copy the aggregator's
2141 			 * parameters and connect the related lag_ports to the
2142 			 * new aggregator
2143 			 */
2144 			if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2145 				slave_dbg(bond->dev, slave->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2146 					  aggregator->aggregator_identifier,
2147 					  new_aggregator->aggregator_identifier);
2148 
2149 				if ((new_aggregator->lag_ports == port) &&
2150 				    new_aggregator->is_active) {
2151 					slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2152 					select_new_active_agg = 1;
2153 				}
2154 
2155 				new_aggregator->is_individual = aggregator->is_individual;
2156 				new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2157 				new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2158 				new_aggregator->partner_system = aggregator->partner_system;
2159 				new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2160 				new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2161 				new_aggregator->receive_state = aggregator->receive_state;
2162 				new_aggregator->transmit_state = aggregator->transmit_state;
2163 				new_aggregator->lag_ports = aggregator->lag_ports;
2164 				new_aggregator->is_active = aggregator->is_active;
2165 				new_aggregator->num_of_ports = aggregator->num_of_ports;
2166 
2167 				/* update the information that is written on
2168 				 * the ports about the aggregator
2169 				 */
2170 				for (temp_port = aggregator->lag_ports; temp_port;
2171 				     temp_port = temp_port->next_port_in_aggregator) {
2172 					temp_port->aggregator = new_aggregator;
2173 					temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2174 				}
2175 
2176 				ad_clear_agg(aggregator);
2177 
2178 				if (select_new_active_agg)
2179 					ad_agg_selection_logic(__get_first_agg(port),
2180 							       &dummy_slave_update);
2181 			} else {
2182 				slave_warn(bond->dev, slave->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2183 			}
2184 		} else {
2185 			/* in case that the only port related to this
2186 			 * aggregator is the one we want to remove
2187 			 */
2188 			select_new_active_agg = aggregator->is_active;
2189 			ad_clear_agg(aggregator);
2190 			if (select_new_active_agg) {
2191 				slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2192 				/* select new active aggregator */
2193 				temp_aggregator = __get_first_agg(port);
2194 				if (temp_aggregator)
2195 					ad_agg_selection_logic(temp_aggregator,
2196 							       &dummy_slave_update);
2197 			}
2198 		}
2199 	}
2200 
2201 	slave_dbg(bond->dev, slave->dev, "Unbinding port %d\n", port->actor_port_number);
2202 
2203 	/* find the aggregator that this port is connected to */
2204 	bond_for_each_slave(bond, slave_iter, iter) {
2205 		temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2206 		prev_port = NULL;
2207 		/* search the port in the aggregator's related ports */
2208 		for (temp_port = temp_aggregator->lag_ports; temp_port;
2209 		     prev_port = temp_port,
2210 		     temp_port = temp_port->next_port_in_aggregator) {
2211 			if (temp_port == port) {
2212 				/* the aggregator found - detach the port from
2213 				 * this aggregator
2214 				 */
2215 				if (prev_port)
2216 					prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2217 				else
2218 					temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2219 				temp_aggregator->num_of_ports--;
2220 				if (__agg_active_ports(temp_aggregator) == 0) {
2221 					select_new_active_agg = temp_aggregator->is_active;
2222 					if (temp_aggregator->num_of_ports == 0)
2223 						ad_clear_agg(temp_aggregator);
2224 					if (select_new_active_agg) {
2225 						slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2226 						/* select new active aggregator */
2227 						ad_agg_selection_logic(__get_first_agg(port),
2228 							               &dummy_slave_update);
2229 					}
2230 				}
2231 				break;
2232 			}
2233 		}
2234 	}
2235 	port->slave = NULL;
2236 
2237 out:
2238 	spin_unlock_bh(&bond->mode_lock);
2239 }
2240 
2241 /**
2242  * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2243  * @bond: bonding struct to work on
2244  *
2245  * If an ad_actor setting gets changed we need to update the individual port
2246  * settings so the bond device will use the new values when it gets upped.
2247  */
bond_3ad_update_ad_actor_settings(struct bonding * bond)2248 void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2249 {
2250 	struct list_head *iter;
2251 	struct slave *slave;
2252 
2253 	ASSERT_RTNL();
2254 
2255 	BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2256 	if (is_zero_ether_addr(bond->params.ad_actor_system))
2257 		BOND_AD_INFO(bond).system.sys_mac_addr =
2258 		    *((struct mac_addr *)bond->dev->dev_addr);
2259 	else
2260 		BOND_AD_INFO(bond).system.sys_mac_addr =
2261 		    *((struct mac_addr *)bond->params.ad_actor_system);
2262 
2263 	spin_lock_bh(&bond->mode_lock);
2264 	bond_for_each_slave(bond, slave, iter) {
2265 		struct port *port = &(SLAVE_AD_INFO(slave))->port;
2266 
2267 		__ad_actor_update_port(port);
2268 		port->ntt = true;
2269 	}
2270 	spin_unlock_bh(&bond->mode_lock);
2271 }
2272 
2273 /**
2274  * bond_agg_timer_advance - advance agg_select_timer
2275  * @bond:  bonding structure
2276  *
2277  * Return true when agg_select_timer reaches 0.
2278  */
bond_agg_timer_advance(struct bonding * bond)2279 static bool bond_agg_timer_advance(struct bonding *bond)
2280 {
2281 	int val, nval;
2282 
2283 	while (1) {
2284 		val = atomic_read(&BOND_AD_INFO(bond).agg_select_timer);
2285 		if (!val)
2286 			return false;
2287 		nval = val - 1;
2288 		if (atomic_cmpxchg(&BOND_AD_INFO(bond).agg_select_timer,
2289 				   val, nval) == val)
2290 			break;
2291 	}
2292 	return nval == 0;
2293 }
2294 
2295 /**
2296  * bond_3ad_state_machine_handler - handle state machines timeout
2297  * @work: work context to fetch bonding struct to work on from
2298  *
2299  * The state machine handling concept in this module is to check every tick
2300  * which state machine should operate any function. The execution order is
2301  * round robin, so when we have an interaction between state machines, the
2302  * reply of one to each other might be delayed until next tick.
2303  *
2304  * This function also complete the initialization when the agg_select_timer
2305  * times out, and it selects an aggregator for the ports that are yet not
2306  * related to any aggregator, and selects the active aggregator for a bond.
2307  */
bond_3ad_state_machine_handler(struct work_struct * work)2308 void bond_3ad_state_machine_handler(struct work_struct *work)
2309 {
2310 	struct bonding *bond = container_of(work, struct bonding,
2311 					    ad_work.work);
2312 	struct aggregator *aggregator;
2313 	struct list_head *iter;
2314 	struct slave *slave;
2315 	struct port *port;
2316 	bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2317 	bool update_slave_arr = false;
2318 
2319 	/* Lock to protect data accessed by all (e.g., port->sm_vars) and
2320 	 * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2321 	 * concurrently due to incoming LACPDU as well.
2322 	 */
2323 	spin_lock_bh(&bond->mode_lock);
2324 	rcu_read_lock();
2325 
2326 	/* check if there are any slaves */
2327 	if (!bond_has_slaves(bond))
2328 		goto re_arm;
2329 
2330 	if (bond_agg_timer_advance(bond)) {
2331 		slave = bond_first_slave_rcu(bond);
2332 		port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2333 
2334 		/* select the active aggregator for the bond */
2335 		if (port) {
2336 			if (!port->slave) {
2337 				net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2338 						     bond->dev->name);
2339 				goto re_arm;
2340 			}
2341 
2342 			aggregator = __get_first_agg(port);
2343 			ad_agg_selection_logic(aggregator, &update_slave_arr);
2344 		}
2345 		bond_3ad_set_carrier(bond);
2346 	}
2347 
2348 	/* for each port run the state machines */
2349 	bond_for_each_slave_rcu(bond, slave, iter) {
2350 		port = &(SLAVE_AD_INFO(slave)->port);
2351 		if (!port->slave) {
2352 			net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2353 					    bond->dev->name);
2354 			goto re_arm;
2355 		}
2356 
2357 		ad_rx_machine(NULL, port);
2358 		ad_periodic_machine(port, &bond->params);
2359 		ad_port_selection_logic(port, &update_slave_arr);
2360 		ad_mux_machine(port, &update_slave_arr);
2361 		ad_tx_machine(port);
2362 		ad_churn_machine(port);
2363 
2364 		/* turn off the BEGIN bit, since we already handled it */
2365 		if (port->sm_vars & AD_PORT_BEGIN)
2366 			port->sm_vars &= ~AD_PORT_BEGIN;
2367 	}
2368 
2369 re_arm:
2370 	bond_for_each_slave_rcu(bond, slave, iter) {
2371 		if (slave->should_notify) {
2372 			should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2373 			break;
2374 		}
2375 	}
2376 	rcu_read_unlock();
2377 	spin_unlock_bh(&bond->mode_lock);
2378 
2379 	if (update_slave_arr)
2380 		bond_slave_arr_work_rearm(bond, 0);
2381 
2382 	if (should_notify_rtnl && rtnl_trylock()) {
2383 		bond_slave_state_notify(bond);
2384 		rtnl_unlock();
2385 	}
2386 	queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2387 }
2388 
2389 /**
2390  * bond_3ad_rx_indication - handle a received frame
2391  * @lacpdu: received lacpdu
2392  * @slave: slave struct to work on
2393  *
2394  * It is assumed that frames that were sent on this NIC don't returned as new
2395  * received frames (loopback). Since only the payload is given to this
2396  * function, it check for loopback.
2397  */
bond_3ad_rx_indication(struct lacpdu * lacpdu,struct slave * slave)2398 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave)
2399 {
2400 	struct bonding *bond = slave->bond;
2401 	int ret = RX_HANDLER_ANOTHER;
2402 	struct bond_marker *marker;
2403 	struct port *port;
2404 	atomic64_t *stat;
2405 
2406 	port = &(SLAVE_AD_INFO(slave)->port);
2407 	if (!port->slave) {
2408 		net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2409 				     slave->dev->name, slave->bond->dev->name);
2410 		return ret;
2411 	}
2412 
2413 	switch (lacpdu->subtype) {
2414 	case AD_TYPE_LACPDU:
2415 		ret = RX_HANDLER_CONSUMED;
2416 		slave_dbg(slave->bond->dev, slave->dev,
2417 			  "Received LACPDU on port %d\n",
2418 			  port->actor_port_number);
2419 		/* Protect against concurrent state machines */
2420 		spin_lock(&slave->bond->mode_lock);
2421 		ad_rx_machine(lacpdu, port);
2422 		spin_unlock(&slave->bond->mode_lock);
2423 		break;
2424 	case AD_TYPE_MARKER:
2425 		ret = RX_HANDLER_CONSUMED;
2426 		/* No need to convert fields to Little Endian since we
2427 		 * don't use the marker's fields.
2428 		 */
2429 		marker = (struct bond_marker *)lacpdu;
2430 		switch (marker->tlv_type) {
2431 		case AD_MARKER_INFORMATION_SUBTYPE:
2432 			slave_dbg(slave->bond->dev, slave->dev, "Received Marker Information on port %d\n",
2433 				  port->actor_port_number);
2434 			ad_marker_info_received(marker, port);
2435 			break;
2436 		case AD_MARKER_RESPONSE_SUBTYPE:
2437 			slave_dbg(slave->bond->dev, slave->dev, "Received Marker Response on port %d\n",
2438 				  port->actor_port_number);
2439 			ad_marker_response_received(marker, port);
2440 			break;
2441 		default:
2442 			slave_dbg(slave->bond->dev, slave->dev, "Received an unknown Marker subtype on port %d\n",
2443 				  port->actor_port_number);
2444 			stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx;
2445 			atomic64_inc(stat);
2446 			stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx;
2447 			atomic64_inc(stat);
2448 		}
2449 		break;
2450 	default:
2451 		atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx);
2452 		atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx);
2453 	}
2454 
2455 	return ret;
2456 }
2457 
2458 /**
2459  * ad_update_actor_keys - Update the oper / admin keys for a port based on
2460  * its current speed and duplex settings.
2461  *
2462  * @port: the port we'are looking at
2463  * @reset: Boolean to just reset the speed and the duplex part of the key
2464  *
2465  * The logic to change the oper / admin keys is:
2466  * (a) A full duplex port can participate in LACP with partner.
2467  * (b) When the speed is changed, LACP need to be reinitiated.
2468  */
ad_update_actor_keys(struct port * port,bool reset)2469 static void ad_update_actor_keys(struct port *port, bool reset)
2470 {
2471 	u8 duplex = 0;
2472 	u16 ospeed = 0, speed = 0;
2473 	u16 old_oper_key = port->actor_oper_port_key;
2474 
2475 	port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2476 	if (!reset) {
2477 		speed = __get_link_speed(port);
2478 		ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2479 		duplex = __get_duplex(port);
2480 		port->actor_admin_port_key |= (speed << 1) | duplex;
2481 	}
2482 	port->actor_oper_port_key = port->actor_admin_port_key;
2483 
2484 	if (old_oper_key != port->actor_oper_port_key) {
2485 		/* Only 'duplex' port participates in LACP */
2486 		if (duplex)
2487 			port->sm_vars |= AD_PORT_LACP_ENABLED;
2488 		else
2489 			port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2490 
2491 		if (!reset) {
2492 			if (!speed) {
2493 				slave_err(port->slave->bond->dev,
2494 					  port->slave->dev,
2495 					  "speed changed to 0 on port %d\n",
2496 					  port->actor_port_number);
2497 			} else if (duplex && ospeed != speed) {
2498 				/* Speed change restarts LACP state-machine */
2499 				port->sm_vars |= AD_PORT_BEGIN;
2500 			}
2501 		}
2502 	}
2503 }
2504 
2505 /**
2506  * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2507  * change indication
2508  *
2509  * @slave: slave struct to work on
2510  *
2511  * Handle reselection of aggregator (if needed) for this port.
2512  */
bond_3ad_adapter_speed_duplex_changed(struct slave * slave)2513 void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2514 {
2515 	struct port *port;
2516 
2517 	port = &(SLAVE_AD_INFO(slave)->port);
2518 
2519 	/* if slave is null, the whole port is not initialized */
2520 	if (!port->slave) {
2521 		slave_warn(slave->bond->dev, slave->dev,
2522 			   "speed/duplex changed for uninitialized port\n");
2523 		return;
2524 	}
2525 
2526 	spin_lock_bh(&slave->bond->mode_lock);
2527 	ad_update_actor_keys(port, false);
2528 	spin_unlock_bh(&slave->bond->mode_lock);
2529 	slave_dbg(slave->bond->dev, slave->dev, "Port %d changed speed/duplex\n",
2530 		  port->actor_port_number);
2531 }
2532 
2533 /**
2534  * bond_3ad_handle_link_change - handle a slave's link status change indication
2535  * @slave: slave struct to work on
2536  * @link: whether the link is now up or down
2537  *
2538  * Handle reselection of aggregator (if needed) for this port.
2539  */
bond_3ad_handle_link_change(struct slave * slave,char link)2540 void bond_3ad_handle_link_change(struct slave *slave, char link)
2541 {
2542 	struct aggregator *agg;
2543 	struct port *port;
2544 	bool dummy;
2545 
2546 	port = &(SLAVE_AD_INFO(slave)->port);
2547 
2548 	/* if slave is null, the whole port is not initialized */
2549 	if (!port->slave) {
2550 		slave_warn(slave->bond->dev, slave->dev, "link status changed for uninitialized port\n");
2551 		return;
2552 	}
2553 
2554 	spin_lock_bh(&slave->bond->mode_lock);
2555 	/* on link down we are zeroing duplex and speed since
2556 	 * some of the adaptors(ce1000.lan) report full duplex/speed
2557 	 * instead of N/A(duplex) / 0(speed).
2558 	 *
2559 	 * on link up we are forcing recheck on the duplex and speed since
2560 	 * some of he adaptors(ce1000.lan) report.
2561 	 */
2562 	if (link == BOND_LINK_UP) {
2563 		port->is_enabled = true;
2564 		ad_update_actor_keys(port, false);
2565 	} else {
2566 		/* link has failed */
2567 		port->is_enabled = false;
2568 		ad_update_actor_keys(port, true);
2569 	}
2570 	agg = __get_first_agg(port);
2571 	ad_agg_selection_logic(agg, &dummy);
2572 
2573 	spin_unlock_bh(&slave->bond->mode_lock);
2574 
2575 	slave_dbg(slave->bond->dev, slave->dev, "Port %d changed link status to %s\n",
2576 		  port->actor_port_number,
2577 		  link == BOND_LINK_UP ? "UP" : "DOWN");
2578 
2579 	/* RTNL is held and mode_lock is released so it's safe
2580 	 * to update slave_array here.
2581 	 */
2582 	bond_update_slave_arr(slave->bond, NULL);
2583 }
2584 
2585 /**
2586  * bond_3ad_set_carrier - set link state for bonding master
2587  * @bond: bonding structure
2588  *
2589  * if we have an active aggregator, we're up, if not, we're down.
2590  * Presumes that we cannot have an active aggregator if there are
2591  * no slaves with link up.
2592  *
2593  * This behavior complies with IEEE 802.3 section 43.3.9.
2594  *
2595  * Called by bond_set_carrier(). Return zero if carrier state does not
2596  * change, nonzero if it does.
2597  */
bond_3ad_set_carrier(struct bonding * bond)2598 int bond_3ad_set_carrier(struct bonding *bond)
2599 {
2600 	struct aggregator *active;
2601 	struct slave *first_slave;
2602 	int ret = 1;
2603 
2604 	rcu_read_lock();
2605 	first_slave = bond_first_slave_rcu(bond);
2606 	if (!first_slave) {
2607 		ret = 0;
2608 		goto out;
2609 	}
2610 	active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2611 	if (active) {
2612 		/* are enough slaves available to consider link up? */
2613 		if (__agg_active_ports(active) < bond->params.min_links) {
2614 			if (netif_carrier_ok(bond->dev)) {
2615 				netif_carrier_off(bond->dev);
2616 				goto out;
2617 			}
2618 		} else if (!netif_carrier_ok(bond->dev)) {
2619 			netif_carrier_on(bond->dev);
2620 			goto out;
2621 		}
2622 	} else if (netif_carrier_ok(bond->dev)) {
2623 		netif_carrier_off(bond->dev);
2624 	}
2625 out:
2626 	rcu_read_unlock();
2627 	return ret;
2628 }
2629 
2630 /**
2631  * __bond_3ad_get_active_agg_info - get information of the active aggregator
2632  * @bond: bonding struct to work on
2633  * @ad_info: ad_info struct to fill with the bond's info
2634  *
2635  * Returns:   0 on success
2636  *          < 0 on error
2637  */
__bond_3ad_get_active_agg_info(struct bonding * bond,struct ad_info * ad_info)2638 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2639 				   struct ad_info *ad_info)
2640 {
2641 	struct aggregator *aggregator = NULL;
2642 	struct list_head *iter;
2643 	struct slave *slave;
2644 	struct port *port;
2645 
2646 	bond_for_each_slave_rcu(bond, slave, iter) {
2647 		port = &(SLAVE_AD_INFO(slave)->port);
2648 		if (port->aggregator && port->aggregator->is_active) {
2649 			aggregator = port->aggregator;
2650 			break;
2651 		}
2652 	}
2653 
2654 	if (!aggregator)
2655 		return -1;
2656 
2657 	ad_info->aggregator_id = aggregator->aggregator_identifier;
2658 	ad_info->ports = __agg_active_ports(aggregator);
2659 	ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2660 	ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2661 	ether_addr_copy(ad_info->partner_system,
2662 			aggregator->partner_system.mac_addr_value);
2663 	return 0;
2664 }
2665 
bond_3ad_get_active_agg_info(struct bonding * bond,struct ad_info * ad_info)2666 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2667 {
2668 	int ret;
2669 
2670 	rcu_read_lock();
2671 	ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2672 	rcu_read_unlock();
2673 
2674 	return ret;
2675 }
2676 
bond_3ad_lacpdu_recv(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)2677 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2678 			 struct slave *slave)
2679 {
2680 	struct lacpdu *lacpdu, _lacpdu;
2681 
2682 	if (skb->protocol != PKT_TYPE_LACPDU)
2683 		return RX_HANDLER_ANOTHER;
2684 
2685 	if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2686 		return RX_HANDLER_ANOTHER;
2687 
2688 	lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2689 	if (!lacpdu) {
2690 		atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx);
2691 		atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx);
2692 		return RX_HANDLER_ANOTHER;
2693 	}
2694 
2695 	return bond_3ad_rx_indication(lacpdu, slave);
2696 }
2697 
2698 /**
2699  * bond_3ad_update_lacp_rate - change the lacp rate
2700  * @bond: bonding struct
2701  *
2702  * When modify lacp_rate parameter via sysfs,
2703  * update actor_oper_port_state of each port.
2704  *
2705  * Hold bond->mode_lock,
2706  * so we can modify port->actor_oper_port_state,
2707  * no matter bond is up or down.
2708  */
bond_3ad_update_lacp_rate(struct bonding * bond)2709 void bond_3ad_update_lacp_rate(struct bonding *bond)
2710 {
2711 	struct port *port = NULL;
2712 	struct list_head *iter;
2713 	struct slave *slave;
2714 	int lacp_fast;
2715 
2716 	lacp_fast = bond->params.lacp_fast;
2717 	spin_lock_bh(&bond->mode_lock);
2718 	bond_for_each_slave(bond, slave, iter) {
2719 		port = &(SLAVE_AD_INFO(slave)->port);
2720 		if (lacp_fast)
2721 			port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
2722 		else
2723 			port->actor_oper_port_state &= ~LACP_STATE_LACP_TIMEOUT;
2724 	}
2725 	spin_unlock_bh(&bond->mode_lock);
2726 }
2727 
bond_3ad_stats_size(void)2728 size_t bond_3ad_stats_size(void)
2729 {
2730 	return nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_RX */
2731 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_TX */
2732 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_UNKNOWN_RX */
2733 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_ILLEGAL_RX */
2734 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RX */
2735 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_TX */
2736 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_RX */
2737 	       nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_TX */
2738 	       nla_total_size_64bit(sizeof(u64)); /* BOND_3AD_STAT_MARKER_UNKNOWN_RX */
2739 }
2740 
bond_3ad_stats_fill(struct sk_buff * skb,struct bond_3ad_stats * stats)2741 int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats)
2742 {
2743 	u64 val;
2744 
2745 	val = atomic64_read(&stats->lacpdu_rx);
2746 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val,
2747 			      BOND_3AD_STAT_PAD))
2748 		return -EMSGSIZE;
2749 	val = atomic64_read(&stats->lacpdu_tx);
2750 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val,
2751 			      BOND_3AD_STAT_PAD))
2752 		return -EMSGSIZE;
2753 	val = atomic64_read(&stats->lacpdu_unknown_rx);
2754 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val,
2755 			      BOND_3AD_STAT_PAD))
2756 		return -EMSGSIZE;
2757 	val = atomic64_read(&stats->lacpdu_illegal_rx);
2758 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val,
2759 			      BOND_3AD_STAT_PAD))
2760 		return -EMSGSIZE;
2761 
2762 	val = atomic64_read(&stats->marker_rx);
2763 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val,
2764 			      BOND_3AD_STAT_PAD))
2765 		return -EMSGSIZE;
2766 	val = atomic64_read(&stats->marker_tx);
2767 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val,
2768 			      BOND_3AD_STAT_PAD))
2769 		return -EMSGSIZE;
2770 	val = atomic64_read(&stats->marker_resp_rx);
2771 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val,
2772 			      BOND_3AD_STAT_PAD))
2773 		return -EMSGSIZE;
2774 	val = atomic64_read(&stats->marker_resp_tx);
2775 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val,
2776 			      BOND_3AD_STAT_PAD))
2777 		return -EMSGSIZE;
2778 	val = atomic64_read(&stats->marker_unknown_rx);
2779 	if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val,
2780 			      BOND_3AD_STAT_PAD))
2781 		return -EMSGSIZE;
2782 
2783 	return 0;
2784 }
2785