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