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