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