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