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