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