-
Notifications
You must be signed in to change notification settings - Fork 59
/
teamd_runner_lacp.c
1959 lines (1706 loc) · 50.9 KB
/
teamd_runner_lacp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* teamd_runner_lacp.c - Teamd 802.3ad LACP runner implementation
* Copyright (C) 2012-2015 Jiri Pirko <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <team.h>
#include <private/misc.h>
#include <net/ethernet.h>
#include "teamd.h"
#include "teamd_config.h"
#include "teamd_state.h"
#include "teamd_workq.h"
/*
* Packet format for LACPDU described in
* IEEE Std 802.3ad-2000 (43.4.2.2)
*/
#define INFO_STATE_LACP_ACTIVITY 0x01
#define INFO_STATE_LACP_TIMEOUT 0x02
#define INFO_STATE_AGGREGATION 0x04
#define INFO_STATE_SYNCHRONIZATION 0x08
#define INFO_STATE_COLLECTING 0x10
#define INFO_STATE_DISTRIBUTING 0x20
#define INFO_STATE_DEFAULTED 0x40
#define INFO_STATE_EXPIRED 0x80
struct lacpdu_info {
uint16_t system_priority;
uint8_t system[ETH_ALEN]; /* ID */
uint16_t key;
uint16_t port_priority;
uint16_t port; /* ID */
uint8_t state;
} __attribute__((__packed__));
struct lacpdu {
struct ether_header hdr;
uint8_t subtype;
uint8_t version_number;
uint8_t actor_tlv_type;
uint8_t actor_info_len;
struct lacpdu_info actor;
uint8_t __reserved1[3];
uint8_t partner_tlv_type;
uint8_t partner_info_len;
struct lacpdu_info partner;
uint8_t __reserved2[3];
uint8_t collector_tlv_type;
uint8_t collector_info_len;
uint16_t collector_max_delay;
uint8_t __reserved3[12];
uint8_t terminator_tlv_type;
uint8_t terminator_info_len;
uint8_t __reserved4[50];
} __attribute__((__packed__));
static void lacpdu_init(struct lacpdu *lacpdu)
{
memset(lacpdu, 0, sizeof(*lacpdu));
lacpdu->subtype = 0x01;
lacpdu->version_number = 0x01;
lacpdu->actor_tlv_type = 0x01;
lacpdu->actor_info_len = 0x14;
lacpdu->partner_tlv_type = 0x02;
lacpdu->partner_info_len = 0x14;
lacpdu->collector_tlv_type = 0x03;
lacpdu->collector_info_len = 0x10;
}
static bool lacpdu_check(struct lacpdu *lacpdu)
{
/*
* According to Annex 43B, section 4, aside from LACP, the Slow
* Protocol linktype is also to be used by other protocols, like
* 0x02 for LAMP, 0x03 for OAM. So for none LACP protocols, just
* silence ignore.
*/
if (lacpdu->subtype != 0x01)
return false;
/*
* According to 43.4.12 version_number, tlv_type and reserved fields
* should not be checked.
*/
if (lacpdu->actor_info_len != 0x14 ||
lacpdu->partner_info_len != 0x14 ||
lacpdu->collector_info_len != 0x10 ||
lacpdu->terminator_info_len != 0x00) {
teamd_log_warn("malformed LACP PDU came.");
return false;
}
return true;
}
enum lacp_agg_select_policy {
LACP_AGG_SELECT_LACP_PRIO = 0,
LACP_AGG_SELECT_LACP_PRIO_STABLE = 1,
LACP_AGG_SELECT_BANDWIDTH = 2,
LACP_AGG_SELECT_COUNT = 3,
LACP_AGG_SELECT_PORT_CONFIG = 4,
};
static const char *lacp_agg_select_policy_names_list[] = {
"lacp_prio", "lacp_prio_stable", "bandwidth", "count", "port_config",
};
#define LACP_AGG_SELECT_POLICY_NAMES_LIST_SIZE \
ARRAY_SIZE(lacp_agg_select_policy_names_list)
struct lacp_port;
struct lacp {
struct teamd_context *ctx;
struct lacp_port *selected_agg_lead; /* leading port of selected aggregator */
bool carrier_up;
struct {
bool active;
#define LACP_CFG_DFLT_ACTIVE true
uint16_t sys_prio;
#define LACP_CFG_DFLT_SYS_PRIO 0xffff
bool fast_rate;
#define LACP_CFG_DFLT_FAST_RATE false
int min_ports;
#define LACP_CFG_DFLT_MIN_PORTS 1
enum lacp_agg_select_policy agg_select_policy;
#define LACP_CFG_DFLT_AGG_SELECT_POLICY LACP_AGG_SELECT_LACP_PRIO
} cfg;
struct teamd_balancer *tb;
};
enum lacp_port_state {
PORT_STATE_DISABLED = 0,
PORT_STATE_CURRENT = 1,
PORT_STATE_EXPIRED = 2,
PORT_STATE_DEFAULTED = 3,
};
static const char *lacp_port_state_name[] = {
"disabled",
"current",
"expired",
"defaulted",
};
struct lacp_port {
struct teamd_context *ctx;
struct teamd_port *tdport;
struct lacp *lacp;
int sock;
struct lacpdu_info actor;
struct lacpdu_info partner;
struct lacpdu_info __partner_last; /* last state before update */
bool periodic_on;
struct lacp_port *agg_lead; /* leading port of aggregator.
* NULL in case this port is not selected */
enum lacp_port_state state;
struct {
uint32_t speed;
uint8_t duplex;
bool up;
} __link_last;
struct {
uint16_t lacp_prio;
#define LACP_PORT_CFG_DFLT_LACP_PRIO 0xff
uint16_t lacp_key;
#define LACP_PORT_CFG_DFLT_LACP_KEY 0
bool sticky;
#define LACP_PORT_CFG_DFLT_STICKY false
} cfg;
};
static struct lacp_port *lacp_port_get(struct lacp *lacp,
struct teamd_port *tdport)
{
/*
* When calling this after teamd_event_watch_register() which is in
* lacp_init() it is ensured that this will always return valid priv
* pointer for an existing port.
*/
return teamd_get_first_port_priv_by_creator(tdport, lacp);
}
static uint32_t lacp_agg_id(struct lacp_port *agg_lead)
{
return agg_lead ? agg_lead->tdport->ifindex : 0;
}
static bool lacp_agg_selected(struct lacp_port *agg_lead)
{
return agg_lead ? agg_lead == agg_lead->lacp->selected_agg_lead : false;
}
static uint32_t lacp_port_agg_id(struct lacp_port *lacp_port)
{
return lacp_agg_id(lacp_port->agg_lead);
}
static uint32_t lacp_port_agg_selected(struct lacp_port *lacp_port)
{
return lacp_agg_selected(lacp_port->agg_lead);
}
static bool lacp_port_is_agg_lead(struct lacp_port *lacp_port)
{
return lacp_port->agg_lead == lacp_port;
}
static const char *lacp_get_agg_select_policy_name(struct lacp *lacp)
{
return lacp_agg_select_policy_names_list[lacp->cfg.agg_select_policy];
}
static int lacp_assign_agg_select_policy(struct lacp *lacp,
const char *agg_select_policy_name)
{
int i = LACP_CFG_DFLT_AGG_SELECT_POLICY;
if (!agg_select_policy_name)
goto found;
for (i = 0; i < LACP_AGG_SELECT_POLICY_NAMES_LIST_SIZE; i++)
if (!strcmp(lacp_agg_select_policy_names_list[i],
agg_select_policy_name))
goto found;
return -ENOENT;
found:
lacp->cfg.agg_select_policy = i;
return 0;
}
static int lacp_load_config(struct teamd_context *ctx, struct lacp *lacp)
{
int err;
int tmp;
const char *agg_select_policy_name;
err = teamd_config_bool_get(ctx, &lacp->cfg.active, "$.runner.active");
if (err)
lacp->cfg.active = LACP_CFG_DFLT_ACTIVE;
teamd_log_dbg(ctx, "Using active \"%d\".", lacp->cfg.active);
err = teamd_config_int_get(ctx, &tmp, "$.runner.sys_prio");
if (err) {
lacp->cfg.sys_prio = LACP_CFG_DFLT_SYS_PRIO;
} else if (tmp < 0 || tmp > USHRT_MAX) {
teamd_log_err("\"sys_prio\" value is out of its limits.");
return -EINVAL;
} else {
lacp->cfg.sys_prio = tmp;
}
teamd_log_dbg(ctx, "Using sys_prio \"%d\".", lacp->cfg.sys_prio);
err = teamd_config_bool_get(ctx, &lacp->cfg.fast_rate, "$.runner.fast_rate");
if (err)
lacp->cfg.fast_rate = LACP_CFG_DFLT_FAST_RATE;
teamd_log_dbg(ctx, "Using fast_rate \"%d\".", lacp->cfg.fast_rate);
err = teamd_config_int_get(ctx, &tmp, "$.runner.min_ports");
if (err) {
lacp->cfg.min_ports = LACP_CFG_DFLT_MIN_PORTS;
} else if (tmp < 1 || tmp > UCHAR_MAX) {
teamd_log_err("\"min_ports\" value is out of its limits.");
return -EINVAL;
} else {
lacp->cfg.min_ports = tmp;
}
teamd_log_dbg(ctx, "Using min_ports \"%d\".", lacp->cfg.min_ports);
err = teamd_config_string_get(ctx, &agg_select_policy_name, "$.runner.agg_select_policy");
if (err)
agg_select_policy_name = NULL;
err = lacp_assign_agg_select_policy(lacp, agg_select_policy_name);
if (err) {
teamd_log_err("Unknown \"agg_select_policy\" named \"%s\" passed.",
agg_select_policy_name);
return err;
}
teamd_log_dbg(ctx, "Using agg_select_policy \"%s\".",
lacp_get_agg_select_policy_name(lacp));
return 0;
}
static bool lacp_port_loopback_free(struct lacp_port *lacp_port)
{
if (!memcmp(lacp_port->actor.system,
lacp_port->partner.system, ETH_ALEN)) {
teamd_log_warn("%s: Port seems to be loopbacked to the same "
"team device.", lacp_port->tdport->ifname);
return false;
}
return true;
}
static bool lacp_port_selectable_state(struct lacp_port *lacp_port)
{
if (lacp_port->state == PORT_STATE_CURRENT)
return true;
return false;
}
static bool lacp_port_unselectable_state(struct lacp_port *lacp_port)
{
if (lacp_port->state == PORT_STATE_CURRENT ||
lacp_port->state == PORT_STATE_EXPIRED)
return false;
return true;
}
static bool lacp_port_selected(struct lacp_port *lacp_port)
{
return lacp_port->agg_lead;
}
static int lacp_port_should_be_enabled(struct lacp_port *lacp_port)
{
struct lacp *lacp = lacp_port->lacp;
if (lacp_port_selected(lacp_port) &&
lacp_port->agg_lead == lacp->selected_agg_lead &&
lacp_port->partner.state & INFO_STATE_SYNCHRONIZATION)
return true;
return false;
}
static int lacp_port_should_be_disabled(struct lacp_port *lacp_port)
{
struct lacp *lacp = lacp_port->lacp;
if (!lacp_port_selected(lacp_port) ||
lacp_port->agg_lead != lacp->selected_agg_lead ||
!(lacp_port->partner.state & INFO_STATE_SYNCHRONIZATION))
return true;
return false;
}
static int lacp_port_update_enabled(struct lacp_port *lacp_port)
{
return teamd_port_check_enable(lacp_port->ctx, lacp_port->tdport,
lacp_port_should_be_enabled(lacp_port),
lacp_port_should_be_disabled(lacp_port));
}
static bool lacp_ports_aggregable(struct lacp_port *lacp_port1,
struct lacp_port *lacp_port2)
{
if (lacp_port1->partner.key != lacp_port2->partner.key ||
lacp_port1->actor.key != lacp_port2->actor.key)
return false;
if (memcmp(lacp_port1->partner.system,
lacp_port2->partner.system, ETH_ALEN))
return false;
return true;
}
static bool lacp_port_correct_aggregation(struct lacp_port *checked_lacp_port)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
/* Find first port in same aggregator and see if checked port is
* aggregable with that. That's enough because all of the ports
* in aggregator besides the checked one are aggregable with each other.
*/
teamd_for_each_tdport(tdport, checked_lacp_port->ctx) {
lacp_port = lacp_port_get(checked_lacp_port->lacp, tdport);
if (!lacp_port_selected(lacp_port) ||
lacp_port->agg_lead != checked_lacp_port->agg_lead ||
lacp_port == checked_lacp_port)
continue;
return lacp_ports_aggregable(lacp_port, checked_lacp_port);
}
return true;
}
static void get_lacp_port_prio_info(struct lacp_port *lacp_port,
struct lacpdu_info *prio_info)
{
int prio_diff;
int system_diff;
prio_diff = ntohs(lacp_port->actor.system_priority) -
ntohs(lacp_port->partner.system_priority);
system_diff = memcmp(lacp_port->actor.system,
lacp_port->partner.system, ETH_ALEN);
if (prio_diff < 0 || (prio_diff == 0 && system_diff < 0))
*prio_info = lacp_port->actor;
if (prio_diff > 0 || (prio_diff == 0 && system_diff >= 0))
*prio_info = lacp_port->partner;
/* adjust values for further memcmp comparison */
prio_info->system_priority = ntohs(prio_info->system_priority);
prio_info->key = 0;
prio_info->port_priority = ntohs(prio_info->port_priority);
prio_info->port = ntohs(prio_info->port);
prio_info->state = 0;
}
static bool lacp_port_better_by_lacp_prio(struct lacp_port *lacp_port1,
struct lacp_port *lacp_port2)
{
struct lacpdu_info prio_info1;
struct lacpdu_info prio_info2;
get_lacp_port_prio_info(lacp_port1, &prio_info1);
get_lacp_port_prio_info(lacp_port2, &prio_info2);
return memcmp(&prio_info1, &prio_info2, sizeof(prio_info1)) < 0;
}
static bool lacp_port_better_by_port_config(struct lacp_port *lacp_port1,
struct lacp_port *lacp_port2)
{
int prio1 = teamd_port_prio(lacp_port1->ctx, lacp_port1->tdport);
int prio2 = teamd_port_prio(lacp_port2->ctx, lacp_port2->tdport);
return prio1 > prio2;
}
static bool lacp_port_better(struct lacp_port *lacp_port1,
struct lacp_port *lacp_port2)
{
if (!lacp_port2)
return true;
switch (lacp_port1->lacp->cfg.agg_select_policy) {
case LACP_AGG_SELECT_PORT_CONFIG:
return lacp_port_better_by_port_config(lacp_port1, lacp_port2);
default:
return lacp_port_better_by_lacp_prio(lacp_port1, lacp_port2);
}
}
static int lacp_set_carrier(struct lacp *lacp, bool carrier_up)
{
struct teamd_context *ctx = lacp->ctx;
int err;
if (lacp->carrier_up != carrier_up) {
lacp->carrier_up = carrier_up;
err = team_carrier_set(ctx->th, carrier_up);
if (err)
return err == -EOPNOTSUPP ? 0 : err;
teamd_log_info("carrier changed to %s",
carrier_up ? "UP" : "DOWN" );
}
return 0;
}
static int lacp_update_carrier(struct lacp *lacp)
{
struct teamd_port *tdport;
int ports_enabled;
bool state;
int err;
ports_enabled = 0;
teamd_for_each_tdport(tdport, lacp->ctx) {
err = teamd_port_enabled(lacp->ctx, tdport, &state);
if (err)
return err;
if (state && ++ports_enabled >= lacp->cfg.min_ports)
return lacp_set_carrier(lacp, true);
}
return lacp_set_carrier(lacp, false);
}
static uint32_t lacp_get_agg_bandwidth(struct lacp_port *agg_lead)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
uint32_t speed = 0;
teamd_for_each_tdport(tdport, agg_lead->ctx) {
lacp_port = lacp_port_get(agg_lead->lacp, tdport);
if (lacp_port->agg_lead == agg_lead)
speed += team_get_port_speed(tdport->team_port);
}
return speed;
}
static struct lacp_port *lacp_get_best_agg_by_bandwidth(struct lacp *lacp)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
uint32_t speed;
uint32_t best_speed = 0;
struct lacp_port *best_agg_lead = NULL;
teamd_for_each_tdport(tdport, lacp->ctx) {
lacp_port = lacp_port_get(lacp, tdport);
if (!lacp_port_selected(lacp_port))
continue;
speed = lacp_get_agg_bandwidth(lacp_port->agg_lead);
if (speed > best_speed) {
best_speed = speed;
best_agg_lead = lacp_port->agg_lead;
}
}
return best_agg_lead;
}
static unsigned int lacp_get_agg_port_count(struct lacp_port *agg_lead)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
unsigned int port_count = 0;
teamd_for_each_tdport(tdport, agg_lead->ctx) {
lacp_port = lacp_port_get(agg_lead->lacp, tdport);
if (lacp_port->agg_lead == agg_lead)
port_count++;
}
return port_count;
}
static struct lacp_port *lacp_get_best_agg_by_port_count(struct lacp *lacp)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
unsigned int port_count;
unsigned int best_port_count = 0;
struct lacp_port *best_agg_lead = NULL;
teamd_for_each_tdport(tdport, lacp->ctx) {
lacp_port = lacp_port_get(lacp, tdport);
if (!lacp_port_selected(lacp_port))
continue;
port_count = lacp_get_agg_port_count(lacp_port->agg_lead);
if (port_count > best_port_count) {
best_port_count = port_count;
best_agg_lead = lacp_port->agg_lead;
}
}
return best_agg_lead;
}
static struct lacp_port *lacp_get_best_agg_by_best_port(struct lacp *lacp)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
struct lacp_port *best_agg_lead = NULL;
teamd_for_each_tdport(tdport, lacp->ctx) {
lacp_port = lacp_port_get(lacp, tdport);
if (!lacp_port_selected(lacp_port))
continue;
if (lacp_port_better(lacp_port->agg_lead, best_agg_lead))
best_agg_lead = lacp_port->agg_lead;
}
return best_agg_lead;
}
static bool lacp_agg_sticky(struct lacp_port *agg_lead)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
teamd_for_each_tdport(tdport, agg_lead->ctx) {
lacp_port = lacp_port_get(agg_lead->lacp, tdport);
if (lacp_port->agg_lead == agg_lead &&
lacp_port->cfg.sticky)
return true;
}
return false;
}
static struct lacp_port *lacp_get_next_agg(struct lacp *lacp)
{
struct lacp_port *next_agg_lead = lacp->selected_agg_lead;
switch (lacp->cfg.agg_select_policy) {
case LACP_AGG_SELECT_LACP_PRIO:
next_agg_lead = lacp_get_best_agg_by_best_port(lacp);
break;
case LACP_AGG_SELECT_LACP_PRIO_STABLE:
if (!lacp->selected_agg_lead)
next_agg_lead = lacp_get_best_agg_by_best_port(lacp);
break;
case LACP_AGG_SELECT_BANDWIDTH:
next_agg_lead = lacp_get_best_agg_by_bandwidth(lacp);
break;
case LACP_AGG_SELECT_COUNT:
next_agg_lead = lacp_get_best_agg_by_port_count(lacp);
break;
case LACP_AGG_SELECT_PORT_CONFIG:
if (!lacp->selected_agg_lead ||
!lacp_agg_sticky(lacp->selected_agg_lead))
next_agg_lead = lacp_get_best_agg_by_best_port(lacp);
break;
}
return next_agg_lead;
}
static struct lacp_port *lacp_get_agg_lead(struct lacp_port *for_lacp_port)
{
struct lacp *lacp = for_lacp_port->lacp;
struct teamd_port *tdport;
struct lacp_port *lacp_port;
teamd_for_each_tdport(tdport, lacp->ctx) {
lacp_port = lacp_port_get(lacp, tdport);
if (!lacp_port_selected(lacp_port) ||
lacp_port == for_lacp_port)
continue;
if (lacp_ports_aggregable(lacp_port, for_lacp_port))
return lacp_port->agg_lead;
}
/* If no suitable aggregator found, the port is self-lead. */
return for_lacp_port;
}
static void lacp_switch_agg_lead(struct lacp_port *agg_lead,
struct lacp_port *new_agg_lead)
{
struct lacp *lacp = agg_lead->lacp;
struct teamd_port *tdport;
struct lacp_port *lacp_port;
teamd_log_dbg(agg_lead->ctx, "Renaming aggregator %u to %u",
lacp_agg_id(agg_lead), lacp_agg_id(new_agg_lead));
if (lacp->selected_agg_lead == agg_lead)
lacp->selected_agg_lead = new_agg_lead;
teamd_for_each_tdport(tdport, lacp->ctx) {
lacp_port = lacp_port_get(lacp, tdport);
if (lacp_port->agg_lead == agg_lead)
lacp_port->agg_lead = new_agg_lead;
}
}
static void lacp_port_agg_select(struct lacp_port *lacp_port)
{
struct lacp_port *agg_lead;
teamd_log_dbg(lacp_port->ctx, "%s: Selecting LACP port", lacp_port->tdport->ifname);
agg_lead = lacp_get_agg_lead(lacp_port);
lacp_port->agg_lead = agg_lead;
if (lacp_port_better(lacp_port, agg_lead))
lacp_switch_agg_lead(agg_lead, lacp_port);
teamd_log_dbg(lacp_port->ctx, "%s: LACP port selected into aggregator %u",
lacp_port->tdport->ifname, lacp_port_agg_id(lacp_port));
}
static struct lacp_port *lacp_find_new_agg_lead(struct lacp_port *old_agg_lead)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
struct lacp_port *new_agg_lead = NULL;
teamd_for_each_tdport(tdport, old_agg_lead->ctx) {
lacp_port = lacp_port_get(old_agg_lead->lacp, tdport);
if (lacp_port->agg_lead == old_agg_lead &&
lacp_port_better(lacp_port, new_agg_lead))
new_agg_lead = lacp_port;
}
return new_agg_lead;
}
static void lacp_port_agg_unselect(struct lacp_port *lacp_port)
{
struct lacp_port *agg_lead = lacp_port->agg_lead;
teamd_log_dbg(lacp_port->ctx, "%s: Unselecting LACP port", lacp_port->tdport->ifname);
teamd_log_dbg(lacp_port->ctx, "%s: LACP port unselected from aggregator %u",
lacp_port->tdport->ifname, lacp_port_agg_id(lacp_port));
lacp_port->agg_lead = NULL;
if (lacp_port == agg_lead) {
/* In case currently unselected port is aggregator lead lead,
* find new one.
*/
struct lacp_port *new_agg_lead;
new_agg_lead = lacp_find_new_agg_lead(agg_lead);
lacp_switch_agg_lead(agg_lead, new_agg_lead);
}
}
static int lacp_ports_update_enabled(struct lacp *lacp)
{
struct teamd_port *tdport;
struct lacp_port *lacp_port;
int err;
teamd_for_each_tdport(tdport, lacp->ctx) {
lacp_port = lacp_port_get(lacp, tdport);
err = lacp_port_update_enabled(lacp_port);
if (err)
return err;
}
return 0;
}
static int lacp_selected_agg_update(struct lacp *lacp,
struct lacp_port *next_agg_lead)
{
int err;
if (!next_agg_lead)
next_agg_lead = lacp_get_next_agg(lacp);
if (lacp->selected_agg_lead != next_agg_lead)
teamd_log_dbg(lacp->ctx, "Selecting aggregator %u",
lacp_agg_id(next_agg_lead));
lacp->selected_agg_lead = next_agg_lead;
err = lacp_ports_update_enabled(lacp);
if (err)
return err;
err = lacp_update_carrier(lacp);
if (err)
return err;
return 0;
}
static bool lacp_port_mergeable(struct lacp_port *lacp_port)
{
/* Port can be merged with other aggregator only in case it is
* alone in aggragator and is aggregable with some other port.
*/
return lacp_port_is_agg_lead(lacp_port) &&
lacp_get_agg_port_count(lacp_port) == 1 &&
lacp_get_agg_lead(lacp_port) != lacp_port;
}
static int lacp_port_agg_update(struct lacp_port *lacp_port)
{
if (lacp_port_selected(lacp_port) &&
(lacp_port_unselectable_state(lacp_port) ||
!lacp_port_loopback_free(lacp_port) ||
!lacp_port_correct_aggregation(lacp_port) ||
lacp_port_mergeable(lacp_port)))
lacp_port_agg_unselect(lacp_port);
if (!lacp_port_selected(lacp_port) &&
(lacp_port_selectable_state(lacp_port) &&
lacp_port_loopback_free(lacp_port)))
lacp_port_agg_select(lacp_port);
return lacp_selected_agg_update(lacp_port->lacp, NULL);
}
static const char slow_addr[ETH_ALEN] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 };
static int __slow_addr_add_del(struct lacp_port *lacp_port, bool add)
{
struct ifreq ifr;
struct sockaddr *sa;
char *devname = lacp_port->tdport->ifname;
int ret;
memset(&ifr, 0, sizeof(struct ifreq));
sa = (struct sockaddr *) &ifr.ifr_addr;
sa->sa_family = AF_UNSPEC;
memcpy(sa->sa_data, slow_addr, sizeof(slow_addr));
memcpy(ifr.ifr_name, devname, strlen(devname));
ret = ioctl(lacp_port->sock, add ? SIOCADDMULTI : SIOCDELMULTI, &ifr);
if (ret == -1) {
teamd_log_err("ioctl %s failed.",
add ? "SIOCADDMULTI" : "SIOCDELMULTI");
return -errno;
}
return 0;
}
static int slow_addr_add(struct lacp_port *lacp_port)
{
return __slow_addr_add_del(lacp_port, true);
}
static int slow_addr_del(struct lacp_port *lacp_port)
{
return __slow_addr_add_del(lacp_port, false);
}
/* Values are in ms */
#define LACP_PERIODIC_SHORT 1000
#define LACP_PERIODIC_LONG 30000
/* time = periodic_interval * LACP_PERIODIC_MUL */
#define LACP_PERIODIC_MUL 3
#define LACP_SOCKET_CB_NAME "lacp_socket"
#define LACP_PERIODIC_CB_NAME "lacp_periodic"
#define LACP_TIMEOUT_CB_NAME "lacp_timeout"
static int lacp_port_timeout_set(struct lacp_port *lacp_port, bool fast_forced)
{
int err;
struct timespec ts;
int ms;
ms = fast_forced || lacp_port->lacp->cfg.fast_rate ?
LACP_PERIODIC_SHORT: LACP_PERIODIC_LONG;
ms *= LACP_PERIODIC_MUL;
ms_to_timespec(&ts, ms);
err = teamd_loop_callback_timer_set(lacp_port->ctx,
LACP_TIMEOUT_CB_NAME,
lacp_port, NULL, &ts);
if (err) {
teamd_log_err("Failed to set timeout timer.");
return err;
}
return 0;
}
static int lacp_port_periodic_set(struct lacp_port *lacp_port)
{
int err;
struct timespec ts;
int ms;
int fast_on;
fast_on = lacp_port->partner.state & INFO_STATE_LACP_TIMEOUT;
teamd_log_dbg(lacp_port->ctx, "%s: Setting periodic timer to \"%s\".",
lacp_port->tdport->ifname, fast_on ? "fast": "slow");
ms = fast_on ? LACP_PERIODIC_SHORT: LACP_PERIODIC_LONG;
ms_to_timespec(&ts, ms);
err = teamd_loop_callback_timer_set(lacp_port->ctx,
LACP_PERIODIC_CB_NAME,
lacp_port, &ts, NULL);
if (err) {
teamd_log_err("Failed to set periodic timer.");
return err;
}
return 0;
}
static bool lacp_port_should_be_active(struct lacp_port *lacp_port)
{
return (lacp_port->partner.state & INFO_STATE_LACP_ACTIVITY) ||
(lacp_port->lacp->cfg.active);
}
static void lacp_port_periodic_cb_change_enabled(struct lacp_port *lacp_port)
{
if (lacp_port_should_be_active(lacp_port) && lacp_port->periodic_on)
teamd_loop_callback_enable(lacp_port->ctx,
LACP_PERIODIC_CB_NAME, lacp_port);
else
teamd_loop_callback_disable(lacp_port->ctx,
LACP_PERIODIC_CB_NAME, lacp_port);
}
static void lacp_port_periodic_on(struct lacp_port *lacp_port)
{
lacp_port->periodic_on = true;
lacp_port_periodic_cb_change_enabled(lacp_port);
}
static void lacp_port_periodic_off(struct lacp_port *lacp_port)
{
lacp_port->periodic_on = false;
lacp_port_periodic_cb_change_enabled(lacp_port);
}
static int lacp_port_partner_update(struct lacp_port *lacp_port)
{
uint8_t state_changed;
int err;
state_changed = lacp_port->partner.state ^
lacp_port->__partner_last.state;
if (state_changed & INFO_STATE_LACP_TIMEOUT) {
err = lacp_port_periodic_set(lacp_port);
if (err)
return err;
}
if (state_changed & INFO_STATE_LACP_ACTIVITY)
lacp_port_periodic_cb_change_enabled(lacp_port);
lacp_port->__partner_last = lacp_port->partner;
return 0;
}
static void lacp_port_actor_system_update(struct lacp_port *lacp_port)
{
struct lacpdu_info *actor = &lacp_port->actor;
memcpy(actor->system, lacp_port->ctx->hwaddr, ETH_ALEN);
}
static void lacp_port_actor_init(struct lacp_port *lacp_port)
{
struct lacpdu_info *actor = &lacp_port->actor;
actor->system_priority = htons(lacp_port->lacp->cfg.sys_prio);
actor->key = htons(lacp_port->cfg.lacp_key);
actor->port_priority = htons(lacp_port->cfg.lacp_prio);
actor->port = htons(lacp_port->tdport->ifindex);
lacp_port_actor_system_update(lacp_port);
}
static void lacp_port_actor_update(struct lacp_port *lacp_port)
{
uint8_t state = 0;
if (lacp_port->lacp->cfg.active)
state |= INFO_STATE_LACP_ACTIVITY;
if (lacp_port->lacp->cfg.fast_rate)
state |= INFO_STATE_LACP_TIMEOUT;
if (lacp_port_selected(lacp_port) &&
lacp_port_agg_selected(lacp_port)) {
state |= INFO_STATE_SYNCHRONIZATION;
state &= ~(INFO_STATE_COLLECTING | INFO_STATE_DISTRIBUTING);
if (lacp_port->partner.state & INFO_STATE_SYNCHRONIZATION)
state |= INFO_STATE_COLLECTING |
INFO_STATE_DISTRIBUTING;
}
if (lacp_port->state == PORT_STATE_EXPIRED)
state |= INFO_STATE_EXPIRED;
if (lacp_port->state == PORT_STATE_DEFAULTED)
state |= INFO_STATE_DEFAULTED;
if (teamd_port_count(lacp_port->ctx) > 0)
state |= INFO_STATE_AGGREGATION;
teamd_log_dbg(lacp_port->ctx, "%s: lacp info state: 0x%02X.",
lacp_port->tdport->ifname, state);
lacp_port->actor.state = state;
}
static int lacpdu_send(struct lacp_port *lacp_port);
static int lacp_port_set_state(struct lacp_port *lacp_port,
enum lacp_port_state new_state)
{
int err;
if (new_state == lacp_port->state)
return 0;
if (new_state == PORT_STATE_DISABLED)
lacp_port_periodic_off(lacp_port);
else
lacp_port_periodic_on(lacp_port);
switch(new_state) {
case PORT_STATE_CURRENT:
break;
case PORT_STATE_EXPIRED:
teamd_loop_callback_enable(lacp_port->ctx,
LACP_PERIODIC_CB_NAME, lacp_port);
/*
* This is a transient state; the LACP_Timeout settings allow
* the Actor to transmit LACPDUs rapidly in an attempt to
* re-establish communication with the Partner.
*/
lacp_port->partner.state |= INFO_STATE_LACP_TIMEOUT;
lacp_port->partner.state &= ~INFO_STATE_SYNCHRONIZATION;
err = lacp_port_partner_update(lacp_port);
if (err)
return err;
lacp_port_timeout_set(lacp_port, true);
teamd_loop_callback_enable(lacp_port->ctx,
LACP_TIMEOUT_CB_NAME, lacp_port);
break;
case PORT_STATE_DEFAULTED:
teamd_loop_callback_disable(lacp_port->ctx,
LACP_TIMEOUT_CB_NAME, lacp_port);
memset(&lacp_port->partner, 0, sizeof(lacp_port->partner));
lacp_port->partner.state |= INFO_STATE_LACP_TIMEOUT;
err = lacp_port_partner_update(lacp_port);
if (err)
return err;
break;
case PORT_STATE_DISABLED:
memset(&lacp_port->partner, 0, sizeof(lacp_port->partner));
err = lacp_port_partner_update(lacp_port);
if (err)
return err;
break;
}
teamd_log_info("%s: Changed port state: \"%s\" -> \"%s\"",
lacp_port->tdport->ifname,
lacp_port_state_name[lacp_port->state],
lacp_port_state_name[new_state]);
lacp_port->state = new_state;