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 fallthrough;
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 fallthrough;
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
1975
1976void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1977{
1978 BOND_AD_INFO(bond).agg_select_timer = timeout;
1979}
1980
1981
1982
1983
1984
1985
1986
1987
1988void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
1989{
1990
1991 if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1992 bond->dev->dev_addr)) {
1993
1994 BOND_AD_INFO(bond).aggregator_identifier = 0;
1995
1996 BOND_AD_INFO(bond).system.sys_priority =
1997 bond->params.ad_actor_sys_prio;
1998 if (is_zero_ether_addr(bond->params.ad_actor_system))
1999 BOND_AD_INFO(bond).system.sys_mac_addr =
2000 *((struct mac_addr *)bond->dev->dev_addr);
2001 else
2002 BOND_AD_INFO(bond).system.sys_mac_addr =
2003 *((struct mac_addr *)bond->params.ad_actor_system);
2004
2005
2006
2007
2008 ad_ticks_per_sec = tick_resolution;
2009
2010 bond_3ad_initiate_agg_selection(bond,
2011 AD_AGGREGATOR_SELECTION_TIMER *
2012 ad_ticks_per_sec);
2013 }
2014}
2015
2016
2017
2018
2019
2020
2021
2022
2023void bond_3ad_bind_slave(struct slave *slave)
2024{
2025 struct bonding *bond = bond_get_bond_by_slave(slave);
2026 struct port *port;
2027 struct aggregator *aggregator;
2028
2029
2030 if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2031
2032
2033 port = &(SLAVE_AD_INFO(slave)->port);
2034
2035 ad_initialize_port(port, bond->params.lacp_fast);
2036
2037 port->slave = slave;
2038 port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2039
2040
2041
2042 port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2043 ad_update_actor_keys(port, false);
2044
2045 __ad_actor_update_port(port);
2046
2047
2048
2049 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2050
2051 __disable_port(port);
2052
2053
2054 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2055
2056 ad_initialize_agg(aggregator);
2057
2058 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2059 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2060 aggregator->slave = slave;
2061 aggregator->is_active = 0;
2062 aggregator->num_of_ports = 0;
2063 }
2064}
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074void bond_3ad_unbind_slave(struct slave *slave)
2075{
2076 struct port *port, *prev_port, *temp_port;
2077 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2078 int select_new_active_agg = 0;
2079 struct bonding *bond = slave->bond;
2080 struct slave *slave_iter;
2081 struct list_head *iter;
2082 bool dummy_slave_update;
2083
2084
2085 spin_lock_bh(&bond->mode_lock);
2086 aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2087 port = &(SLAVE_AD_INFO(slave)->port);
2088
2089
2090 if (!port->slave) {
2091 slave_warn(bond->dev, slave->dev, "Trying to unbind an uninitialized port\n");
2092 goto out;
2093 }
2094
2095 slave_dbg(bond->dev, slave->dev, "Unbinding Link Aggregation Group %d\n",
2096 aggregator->aggregator_identifier);
2097
2098
2099 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
2100 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
2101 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
2102 port->actor_oper_port_state &= ~LACP_STATE_AGGREGATION;
2103 __update_lacpdu_from_port(port);
2104 ad_lacpdu_send(port);
2105
2106
2107 if (aggregator->lag_ports) {
2108
2109
2110
2111
2112
2113 if ((aggregator->lag_ports != port) ||
2114 (aggregator->lag_ports->next_port_in_aggregator)) {
2115
2116 bond_for_each_slave(bond, slave_iter, iter) {
2117 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2118
2119
2120
2121 if (!new_aggregator->lag_ports ||
2122 ((new_aggregator->lag_ports == port) &&
2123 !new_aggregator->lag_ports->next_port_in_aggregator))
2124 break;
2125 }
2126 if (!slave_iter)
2127 new_aggregator = NULL;
2128
2129
2130
2131
2132
2133 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2134 slave_dbg(bond->dev, slave->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2135 aggregator->aggregator_identifier,
2136 new_aggregator->aggregator_identifier);
2137
2138 if ((new_aggregator->lag_ports == port) &&
2139 new_aggregator->is_active) {
2140 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2141 select_new_active_agg = 1;
2142 }
2143
2144 new_aggregator->is_individual = aggregator->is_individual;
2145 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2146 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2147 new_aggregator->partner_system = aggregator->partner_system;
2148 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2149 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2150 new_aggregator->receive_state = aggregator->receive_state;
2151 new_aggregator->transmit_state = aggregator->transmit_state;
2152 new_aggregator->lag_ports = aggregator->lag_ports;
2153 new_aggregator->is_active = aggregator->is_active;
2154 new_aggregator->num_of_ports = aggregator->num_of_ports;
2155
2156
2157
2158
2159 for (temp_port = aggregator->lag_ports; temp_port;
2160 temp_port = temp_port->next_port_in_aggregator) {
2161 temp_port->aggregator = new_aggregator;
2162 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2163 }
2164
2165 ad_clear_agg(aggregator);
2166
2167 if (select_new_active_agg)
2168 ad_agg_selection_logic(__get_first_agg(port),
2169 &dummy_slave_update);
2170 } else {
2171 slave_warn(bond->dev, slave->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2172 }
2173 } else {
2174
2175
2176
2177 select_new_active_agg = aggregator->is_active;
2178 ad_clear_agg(aggregator);
2179 if (select_new_active_agg) {
2180 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2181
2182 temp_aggregator = __get_first_agg(port);
2183 if (temp_aggregator)
2184 ad_agg_selection_logic(temp_aggregator,
2185 &dummy_slave_update);
2186 }
2187 }
2188 }
2189
2190 slave_dbg(bond->dev, slave->dev, "Unbinding port %d\n", port->actor_port_number);
2191
2192
2193 bond_for_each_slave(bond, slave_iter, iter) {
2194 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2195 prev_port = NULL;
2196
2197 for (temp_port = temp_aggregator->lag_ports; temp_port;
2198 prev_port = temp_port,
2199 temp_port = temp_port->next_port_in_aggregator) {
2200 if (temp_port == port) {
2201
2202
2203
2204 if (prev_port)
2205 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2206 else
2207 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2208 temp_aggregator->num_of_ports--;
2209 if (__agg_active_ports(temp_aggregator) == 0) {
2210 select_new_active_agg = temp_aggregator->is_active;
2211 ad_clear_agg(temp_aggregator);
2212 if (select_new_active_agg) {
2213 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2214
2215 ad_agg_selection_logic(__get_first_agg(port),
2216 &dummy_slave_update);
2217 }
2218 }
2219 break;
2220 }
2221 }
2222 }
2223 port->slave = NULL;
2224
2225out:
2226 spin_unlock_bh(&bond->mode_lock);
2227}
2228
2229
2230
2231
2232
2233
2234
2235
2236void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2237{
2238 struct list_head *iter;
2239 struct slave *slave;
2240
2241 ASSERT_RTNL();
2242
2243 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2244 if (is_zero_ether_addr(bond->params.ad_actor_system))
2245 BOND_AD_INFO(bond).system.sys_mac_addr =
2246 *((struct mac_addr *)bond->dev->dev_addr);
2247 else
2248 BOND_AD_INFO(bond).system.sys_mac_addr =
2249 *((struct mac_addr *)bond->params.ad_actor_system);
2250
2251 spin_lock_bh(&bond->mode_lock);
2252 bond_for_each_slave(bond, slave, iter) {
2253 struct port *port = &(SLAVE_AD_INFO(slave))->port;
2254
2255 __ad_actor_update_port(port);
2256 port->ntt = true;
2257 }
2258 spin_unlock_bh(&bond->mode_lock);
2259}
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274void bond_3ad_state_machine_handler(struct work_struct *work)
2275{
2276 struct bonding *bond = container_of(work, struct bonding,
2277 ad_work.work);
2278 struct aggregator *aggregator;
2279 struct list_head *iter;
2280 struct slave *slave;
2281 struct port *port;
2282 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2283 bool update_slave_arr = false;
2284
2285
2286
2287
2288
2289 spin_lock_bh(&bond->mode_lock);
2290 rcu_read_lock();
2291
2292
2293 if (!bond_has_slaves(bond))
2294 goto re_arm;
2295
2296
2297 if (BOND_AD_INFO(bond).agg_select_timer &&
2298 !(--BOND_AD_INFO(bond).agg_select_timer)) {
2299 slave = bond_first_slave_rcu(bond);
2300 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2301
2302
2303 if (port) {
2304 if (!port->slave) {
2305 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2306 bond->dev->name);
2307 goto re_arm;
2308 }
2309
2310 aggregator = __get_first_agg(port);
2311 ad_agg_selection_logic(aggregator, &update_slave_arr);
2312 }
2313 bond_3ad_set_carrier(bond);
2314 }
2315
2316
2317 bond_for_each_slave_rcu(bond, slave, iter) {
2318 port = &(SLAVE_AD_INFO(slave)->port);
2319 if (!port->slave) {
2320 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2321 bond->dev->name);
2322 goto re_arm;
2323 }
2324
2325 ad_rx_machine(NULL, port);
2326 ad_periodic_machine(port);
2327 ad_port_selection_logic(port, &update_slave_arr);
2328 ad_mux_machine(port, &update_slave_arr);
2329 ad_tx_machine(port);
2330 ad_churn_machine(port);
2331
2332
2333 if (port->sm_vars & AD_PORT_BEGIN)
2334 port->sm_vars &= ~AD_PORT_BEGIN;
2335 }
2336
2337re_arm:
2338 bond_for_each_slave_rcu(bond, slave, iter) {
2339 if (slave->should_notify) {
2340 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2341 break;
2342 }
2343 }
2344 rcu_read_unlock();
2345 spin_unlock_bh(&bond->mode_lock);
2346
2347 if (update_slave_arr)
2348 bond_slave_arr_work_rearm(bond, 0);
2349
2350 if (should_notify_rtnl && rtnl_trylock()) {
2351 bond_slave_state_notify(bond);
2352 rtnl_unlock();
2353 }
2354 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2355}
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave)
2367{
2368 struct bonding *bond = slave->bond;
2369 int ret = RX_HANDLER_ANOTHER;
2370 struct bond_marker *marker;
2371 struct port *port;
2372 atomic64_t *stat;
2373
2374 port = &(SLAVE_AD_INFO(slave)->port);
2375 if (!port->slave) {
2376 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2377 slave->dev->name, slave->bond->dev->name);
2378 return ret;
2379 }
2380
2381 switch (lacpdu->subtype) {
2382 case AD_TYPE_LACPDU:
2383 ret = RX_HANDLER_CONSUMED;
2384 slave_dbg(slave->bond->dev, slave->dev,
2385 "Received LACPDU on port %d\n",
2386 port->actor_port_number);
2387
2388 spin_lock(&slave->bond->mode_lock);
2389 ad_rx_machine(lacpdu, port);
2390 spin_unlock(&slave->bond->mode_lock);
2391 break;
2392 case AD_TYPE_MARKER:
2393 ret = RX_HANDLER_CONSUMED;
2394
2395
2396
2397 marker = (struct bond_marker *)lacpdu;
2398 switch (marker->tlv_type) {
2399 case AD_MARKER_INFORMATION_SUBTYPE:
2400 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Information on port %d\n",
2401 port->actor_port_number);
2402 ad_marker_info_received(marker, port);
2403 break;
2404 case AD_MARKER_RESPONSE_SUBTYPE:
2405 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Response on port %d\n",
2406 port->actor_port_number);
2407 ad_marker_response_received(marker, port);
2408 break;
2409 default:
2410 slave_dbg(slave->bond->dev, slave->dev, "Received an unknown Marker subtype on port %d\n",
2411 port->actor_port_number);
2412 stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx;
2413 atomic64_inc(stat);
2414 stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx;
2415 atomic64_inc(stat);
2416 }
2417 break;
2418 default:
2419 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx);
2420 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx);
2421 }
2422
2423 return ret;
2424}
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437static void ad_update_actor_keys(struct port *port, bool reset)
2438{
2439 u8 duplex = 0;
2440 u16 ospeed = 0, speed = 0;
2441 u16 old_oper_key = port->actor_oper_port_key;
2442
2443 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2444 if (!reset) {
2445 speed = __get_link_speed(port);
2446 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2447 duplex = __get_duplex(port);
2448 port->actor_admin_port_key |= (speed << 1) | duplex;
2449 }
2450 port->actor_oper_port_key = port->actor_admin_port_key;
2451
2452 if (old_oper_key != port->actor_oper_port_key) {
2453
2454 if (duplex)
2455 port->sm_vars |= AD_PORT_LACP_ENABLED;
2456 else
2457 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2458
2459 if (!reset) {
2460 if (!speed) {
2461 slave_err(port->slave->bond->dev,
2462 port->slave->dev,
2463 "speed changed to 0 on port %d\n",
2464 port->actor_port_number);
2465 } else if (duplex && ospeed != speed) {
2466
2467 port->sm_vars |= AD_PORT_BEGIN;
2468 }
2469 }
2470 }
2471}
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2482{
2483 struct port *port;
2484
2485 port = &(SLAVE_AD_INFO(slave)->port);
2486
2487
2488 if (!port->slave) {
2489 slave_warn(slave->bond->dev, slave->dev,
2490 "speed/duplex changed for uninitialized port\n");
2491 return;
2492 }
2493
2494 spin_lock_bh(&slave->bond->mode_lock);
2495 ad_update_actor_keys(port, false);
2496 spin_unlock_bh(&slave->bond->mode_lock);
2497 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed speed/duplex\n",
2498 port->actor_port_number);
2499}
2500
2501
2502
2503
2504
2505
2506
2507
2508void bond_3ad_handle_link_change(struct slave *slave, char link)
2509{
2510 struct aggregator *agg;
2511 struct port *port;
2512 bool dummy;
2513
2514 port = &(SLAVE_AD_INFO(slave)->port);
2515
2516
2517 if (!port->slave) {
2518 slave_warn(slave->bond->dev, slave->dev, "link status changed for uninitialized port\n");
2519 return;
2520 }
2521
2522 spin_lock_bh(&slave->bond->mode_lock);
2523
2524
2525
2526
2527
2528
2529
2530 if (link == BOND_LINK_UP) {
2531 port->is_enabled = true;
2532 ad_update_actor_keys(port, false);
2533 } else {
2534
2535 port->is_enabled = false;
2536 ad_update_actor_keys(port, true);
2537 }
2538 agg = __get_first_agg(port);
2539 ad_agg_selection_logic(agg, &dummy);
2540
2541 spin_unlock_bh(&slave->bond->mode_lock);
2542
2543 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed link status to %s\n",
2544 port->actor_port_number,
2545 link == BOND_LINK_UP ? "UP" : "DOWN");
2546
2547
2548
2549
2550 bond_update_slave_arr(slave->bond, NULL);
2551}
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566int bond_3ad_set_carrier(struct bonding *bond)
2567{
2568 struct aggregator *active;
2569 struct slave *first_slave;
2570 int ret = 1;
2571
2572 rcu_read_lock();
2573 first_slave = bond_first_slave_rcu(bond);
2574 if (!first_slave) {
2575 ret = 0;
2576 goto out;
2577 }
2578 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2579 if (active) {
2580
2581 if (__agg_active_ports(active) < bond->params.min_links) {
2582 if (netif_carrier_ok(bond->dev)) {
2583 netif_carrier_off(bond->dev);
2584 goto out;
2585 }
2586 } else if (!netif_carrier_ok(bond->dev)) {
2587 netif_carrier_on(bond->dev);
2588 goto out;
2589 }
2590 } else if (netif_carrier_ok(bond->dev)) {
2591 netif_carrier_off(bond->dev);
2592 }
2593out:
2594 rcu_read_unlock();
2595 return ret;
2596}
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606int __bond_3ad_get_active_agg_info(struct bonding *bond,
2607 struct ad_info *ad_info)
2608{
2609 struct aggregator *aggregator = NULL;
2610 struct list_head *iter;
2611 struct slave *slave;
2612 struct port *port;
2613
2614 bond_for_each_slave_rcu(bond, slave, iter) {
2615 port = &(SLAVE_AD_INFO(slave)->port);
2616 if (port->aggregator && port->aggregator->is_active) {
2617 aggregator = port->aggregator;
2618 break;
2619 }
2620 }
2621
2622 if (!aggregator)
2623 return -1;
2624
2625 ad_info->aggregator_id = aggregator->aggregator_identifier;
2626 ad_info->ports = __agg_active_ports(aggregator);
2627 ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2628 ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2629 ether_addr_copy(ad_info->partner_system,
2630 aggregator->partner_system.mac_addr_value);
2631 return 0;
2632}
2633
2634int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2635{
2636 int ret;
2637
2638 rcu_read_lock();
2639 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2640 rcu_read_unlock();
2641
2642 return ret;
2643}
2644
2645int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2646 struct slave *slave)
2647{
2648 struct lacpdu *lacpdu, _lacpdu;
2649
2650 if (skb->protocol != PKT_TYPE_LACPDU)
2651 return RX_HANDLER_ANOTHER;
2652
2653 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2654 return RX_HANDLER_ANOTHER;
2655
2656 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2657 if (!lacpdu) {
2658 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx);
2659 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx);
2660 return RX_HANDLER_ANOTHER;
2661 }
2662
2663 return bond_3ad_rx_indication(lacpdu, slave);
2664}
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677void bond_3ad_update_lacp_rate(struct bonding *bond)
2678{
2679 struct port *port = NULL;
2680 struct list_head *iter;
2681 struct slave *slave;
2682 int lacp_fast;
2683
2684 lacp_fast = bond->params.lacp_fast;
2685 spin_lock_bh(&bond->mode_lock);
2686 bond_for_each_slave(bond, slave, iter) {
2687 port = &(SLAVE_AD_INFO(slave)->port);
2688 if (lacp_fast)
2689 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
2690 else
2691 port->actor_oper_port_state &= ~LACP_STATE_LACP_TIMEOUT;
2692 }
2693 spin_unlock_bh(&bond->mode_lock);
2694}
2695
2696size_t bond_3ad_stats_size(void)
2697{
2698 return 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 nla_total_size_64bit(sizeof(u64));
2707}
2708
2709int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats)
2710{
2711 u64 val;
2712
2713 val = atomic64_read(&stats->lacpdu_rx);
2714 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val,
2715 BOND_3AD_STAT_PAD))
2716 return -EMSGSIZE;
2717 val = atomic64_read(&stats->lacpdu_tx);
2718 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val,
2719 BOND_3AD_STAT_PAD))
2720 return -EMSGSIZE;
2721 val = atomic64_read(&stats->lacpdu_unknown_rx);
2722 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val,
2723 BOND_3AD_STAT_PAD))
2724 return -EMSGSIZE;
2725 val = atomic64_read(&stats->lacpdu_illegal_rx);
2726 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val,
2727 BOND_3AD_STAT_PAD))
2728 return -EMSGSIZE;
2729
2730 val = atomic64_read(&stats->marker_rx);
2731 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val,
2732 BOND_3AD_STAT_PAD))
2733 return -EMSGSIZE;
2734 val = atomic64_read(&stats->marker_tx);
2735 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val,
2736 BOND_3AD_STAT_PAD))
2737 return -EMSGSIZE;
2738 val = atomic64_read(&stats->marker_resp_rx);
2739 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val,
2740 BOND_3AD_STAT_PAD))
2741 return -EMSGSIZE;
2742 val = atomic64_read(&stats->marker_resp_tx);
2743 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val,
2744 BOND_3AD_STAT_PAD))
2745 return -EMSGSIZE;
2746 val = atomic64_read(&stats->marker_unknown_rx);
2747 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val,
2748 BOND_3AD_STAT_PAD))
2749 return -EMSGSIZE;
2750
2751 return 0;
2752}
2753