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