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