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