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