forked from openSUSE/wicked
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bonding.c
1251 lines (1088 loc) · 33 KB
/
bonding.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
/*
* Routines for handling bonding devices
*
* Copyright (C) 2009-2012 Olaf Kirch <okir@suse.de>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <arpa/inet.h>
#include <limits.h>
#include <wicked/netinfo.h>
#include <wicked/bridge.h>
#include <wicked/bonding.h>
#include "netinfo_priv.h"
#include "util_priv.h"
#include "sysfs.h"
#include "modprobe.h"
/*
* Kernel module and default parameter.
*
* The max_bonds=0 parameter is used by default to avoid automatic
* "bond0" interface creation at load time. We may need another one.
*/
#ifndef BONDING_MODULE_NAME
#define BONDING_MODULE_NAME "bonding"
#endif
#ifndef BONDING_MODULE_OPTS
#define BONDING_MODULE_OPTS "max_bonds=0"
#endif
/*
* Maps of kernel/sysctl named bonding option settings
*/
static const ni_intmap_t __map_kern_mode[] = {
{ "balance-rr", NI_BOND_MODE_BALANCE_RR },
{ "active-backup", NI_BOND_MODE_ACTIVE_BACKUP },
{ "balance-xor", NI_BOND_MODE_BALANCE_XOR },
{ "broadcast", NI_BOND_MODE_BROADCAST },
{ "802.3ad", NI_BOND_MODE_802_3AD },
{ "balance-tlb", NI_BOND_MODE_BALANCE_TLB },
{ "balance-alb", NI_BOND_MODE_BALANCE_ALB },
{ NULL }
};
static const ni_intmap_t __map_kern_arp_validate[] = {
{ "none", NI_BOND_ARP_VALIDATE_NONE },
{ "active", NI_BOND_ARP_VALIDATE_ACTIVE },
{ "backup", NI_BOND_ARP_VALIDATE_BACKUP },
{ "all", NI_BOND_ARP_VALIDATE_ALL },
{ NULL }
};
static const ni_intmap_t __map_kern_xmit_hash_policy[] = {
{ "layer2", NI_BOND_XMIT_HASH_LAYER2 },
{ "layer2+3", NI_BOND_XMIT_HASH_LAYER2_3 },
{ "layer3+4", NI_BOND_XMIT_HASH_LAYER3_4 },
{ NULL }
};
static const ni_intmap_t __map_kern_lacp_rate[] = {
{ "slow", NI_BOND_LACP_RATE_SLOW },
{ "fast", NI_BOND_LACP_RATE_FAST },
{ NULL }
};
static const ni_intmap_t __map_kern_ad_select[] = {
{ "stable", NI_BOND_AD_SELECT_STABLE },
{ "bandwidth", NI_BOND_AD_SELECT_BANDWIDTH },
{ "count", NI_BOND_AD_SELECT_COUNT },
{ NULL }
};
static const ni_intmap_t __map_kern_fail_over_mac[] = {
{ "none", NI_BOND_FAIL_OVER_MAC_NONE },
{ "active", NI_BOND_FAIL_OVER_MAC_ACTIVE },
{ "follow", NI_BOND_FAIL_OVER_MAC_FOLLOW },
{ NULL }
};
static const ni_intmap_t __map_kern_primary_reselect[] = {
{ "always", NI_BOND_PRIMARY_RESELECT_ALWAYS },
{ "better", NI_BOND_PRIMARY_RESELECT_BETTER },
{ "failure", NI_BOND_PRIMARY_RESELECT_FAILURE },
{ NULL }
};
/*
* For now, the enum names in the xml schema use almost the same mode names as
* the kernel. 802.3ad being the notable exception, as starts with a digit,
* which is illegal in xml element names.
*/
static const ni_intmap_t __map_user_mode[] = {
{ "balance-rr", NI_BOND_MODE_BALANCE_RR },
{ "active-backup", NI_BOND_MODE_ACTIVE_BACKUP },
{ "balance-xor", NI_BOND_MODE_BALANCE_XOR },
{ "broadcast", NI_BOND_MODE_BROADCAST },
{ "ieee802-3ad", NI_BOND_MODE_802_3AD },
{ "balance-tlb", NI_BOND_MODE_BALANCE_TLB },
{ "balance-alb", NI_BOND_MODE_BALANCE_ALB },
{ NULL }
};
/*
* The kernel's xmit hash policies contain a + character.
*/
static const ni_intmap_t __map_user_xmit_hash_policy[] = {
{ "layer2", NI_BOND_XMIT_HASH_LAYER2 },
{ "layer23", NI_BOND_XMIT_HASH_LAYER2_3 },
{ "layer34", NI_BOND_XMIT_HASH_LAYER3_4 },
{ NULL }
};
/*
* This setting is used in the schema; the kernel wants use_carrier=0/1.
*/
static const ni_intmap_t __map_user_carrier_detect[] = {
{ "ioctl", NI_BOND_MII_CARRIER_DETECT_IOCTL },
{ "netif", NI_BOND_MII_CARRIER_DETECT_NETIF },
{ NULL }
};
/*
* Load bonding module with specified arguments.
*
* Default is max_bonds=1 that automatically creates a bond0
* interface. We use max_bonds=0 option as default to avoid
* an automatic bond0 interface creation. They're requested
* explicitly by the factory, which may need another name.
*/
int
ni_bonding_load(const char *options)
{
if (options == NULL)
options = BONDING_MODULE_OPTS;
return ni_modprobe(BONDING_MODULE_NAME, options);
}
/*
* Initialize defaults
*/
static inline void
__ni_bonding_init(ni_bonding_t *bonding)
{
/* Apply non-zero kernel defaults */
bonding->miimon.carrier_detect = NI_BOND_MII_CARRIER_DETECT_NETIF;
bonding->num_grat_arp = 1;
bonding->num_unsol_na = 1;
bonding->resend_igmp = 1;
}
/*
* Reinitialize the bonding configuration
*/
static inline void
__ni_bonding_clear(ni_bonding_t *bonding)
{
ni_string_free(&bonding->active_slave);
ni_string_free(&bonding->primary_slave);
ni_string_array_destroy(&bonding->slave_names);
ni_string_array_destroy(&bonding->arpmon.targets);
memset(bonding, 0, sizeof(*bonding));
__ni_bonding_init(bonding);
}
/*
* Create a bonding config
*/
ni_bonding_t *
ni_bonding_new(void)
{
ni_bonding_t *bonding;
bonding = xcalloc(1, sizeof(ni_bonding_t));
__ni_bonding_init(bonding);
return bonding;
}
/*
* Free bonding configuration
*/
void
ni_bonding_free(ni_bonding_t *bonding)
{
__ni_bonding_clear(bonding);
free(bonding);
}
/*
* Check whether the given bonding settings are valid
*/
const char *
ni_bonding_validate(const ni_bonding_t *bonding)
{
unsigned int i;
if (bonding == NULL)
return "uninitialized bonding options";
switch (bonding->mode) {
case NI_BOND_MODE_BALANCE_RR:
case NI_BOND_MODE_ACTIVE_BACKUP:
case NI_BOND_MODE_BALANCE_XOR:
case NI_BOND_MODE_BROADCAST:
case NI_BOND_MODE_802_3AD:
case NI_BOND_MODE_BALANCE_TLB:
case NI_BOND_MODE_BALANCE_ALB:
break;
default:
return "unsupported bonding mode";
}
/*
* Bonding without monitoring is nonsense/unsupported:
*
* "[...] It is critical that either the miimon or arp_interval and
* arp_ip_target parameters be specified, otherwise serious network
* degradation will occur during link failures. [...]"
*
* While miimon can be used in any mode, arpmon is not usable in
* balance-tlb/-alb and arp-validate limited to active-bacup only.
*/
switch (bonding->monitoring) {
case NI_BOND_MONITOR_ARP:
if (bonding->miimon.frequency > 0)
return "invalid arp and mii monitoring option mix";
if (bonding->mode == NI_BOND_MODE_BALANCE_TLB ||
bonding->mode == NI_BOND_MODE_BALANCE_ALB)
return "invalid arp monitoring in balance-tlb/-alb mode";
if (bonding->arpmon.interval == 0 ||
bonding->arpmon.interval > INT_MAX)
return "invalid arp monitoring interval";
switch (bonding->arpmon.validate) {
case NI_BOND_ARP_VALIDATE_NONE:
case NI_BOND_ARP_VALIDATE_ACTIVE:
case NI_BOND_ARP_VALIDATE_BACKUP:
case NI_BOND_ARP_VALIDATE_ALL:
if (bonding->mode == NI_BOND_MODE_ACTIVE_BACKUP)
break;
return "arp validate is valid in active-backup mode only";
default:
return "invalid arp validate setting";
}
if (bonding->arpmon.targets.count == 0)
return "no targets for arp monitoring";
for (i = 0; i < bonding->arpmon.targets.count; ++i) {
const char *target = bonding->arpmon.targets.data[i];
if (!ni_bonding_is_valid_arp_ip_target(target))
return "invalid arp ip target address";
}
break;
case NI_BOND_MONITOR_MII:
if (bonding->arpmon.interval > 0 ||
bonding->arpmon.targets.count > 0)
return "invalid mii and arp monitoring option mix";
if (bonding->miimon.frequency == 0)
return "invalid mii monitoring frequency";
/*
* Both should be a multiple of frequency and are rounded down
* with a warning from the kernel driver. That is, when they're
* smaller than miimon, they're rounded down to 0 / disabled.
*/
if (bonding->miimon.updelay > 0 &&
(bonding->miimon.updelay < bonding->miimon.frequency))
return "miimon updelay is smaller than frequency";
if (bonding->miimon.downdelay > 0 &&
(bonding->miimon.downdelay < bonding->miimon.frequency))
return "miimon downdelay is smaller than frequency";
switch (bonding->miimon.carrier_detect) {
case NI_BOND_MII_CARRIER_DETECT_IOCTL:
case NI_BOND_MII_CARRIER_DETECT_NETIF:
break;
default:
return "invalid miimon carrier detect setting";
}
break;
case NI_BOND_MONITOR_MII|NI_BOND_MONITOR_ARP:
return "unsupported mii / arp monintoring mix";
default:
return "unsupported, insufficient monitoring settings";
}
switch(bonding->mode) {
case NI_BOND_MODE_BALANCE_XOR:
case NI_BOND_MODE_802_3AD:
switch (bonding->xmit_hash_policy) {
case NI_BOND_XMIT_HASH_LAYER2:
case NI_BOND_XMIT_HASH_LAYER2_3:
case NI_BOND_XMIT_HASH_LAYER3_4:
break;
default:
return "unsupported xmit hash policy";
}
break;
default:
if (bonding->xmit_hash_policy != NI_BOND_XMIT_HASH_LAYER2)
return "invalid xmit hash policy and mode combination";
}
if (bonding->mode == NI_BOND_MODE_802_3AD) {
switch (bonding->lacp_rate) {
case NI_BOND_LACP_RATE_SLOW:
case NI_BOND_LACP_RATE_FAST:
break;
default:
return "unsupported ieee802-3ad lacp-rate setting";
}
switch (bonding->ad_select) {
case NI_BOND_AD_SELECT_STABLE:
case NI_BOND_AD_SELECT_BANDWIDTH:
case NI_BOND_AD_SELECT_COUNT:
break;
default:
return "unsupported ieee802-3ad ad-select setting";
}
if (bonding->min_links > INT_MAX)
return "ieee802-3ad min-links option not in range 0-INT_MAX";
} else {
if (bonding->lacp_rate != NI_BOND_LACP_RATE_SLOW)
return "lacp rate only valid in ieee802-3ad mode";
if (bonding->ad_select != NI_BOND_AD_SELECT_STABLE)
return "ad-select only valid in ieee802-3ad mode";
if (bonding->min_links > 0)
return "min-links option valid only in ieee802-3ad mode";
}
switch (bonding->mode) {
case NI_BOND_MODE_ACTIVE_BACKUP:
case NI_BOND_MODE_BALANCE_RR:
case NI_BOND_MODE_BALANCE_TLB:
case NI_BOND_MODE_BALANCE_ALB:
if (bonding->resend_igmp > 255)
return "resend IGMP count not in range 0-255";
break;
default:
if (bonding->resend_igmp > 1)
return "resend IGMP count is not valid in this mode";
break;
}
if (bonding->mode == NI_BOND_MODE_ACTIVE_BACKUP) {
switch (bonding->fail_over_mac) {
case NI_BOND_FAIL_OVER_MAC_NONE:
case NI_BOND_FAIL_OVER_MAC_ACTIVE:
case NI_BOND_FAIL_OVER_MAC_FOLLOW:
break;
default:
return "unsupported fail-over-mac setting";
}
if (bonding->num_grat_arp > 255)
return "gratuitous ARP count not in range 0-255";
if (bonding->num_unsol_na > 255)
return "unsolicited IPv6-NA count not in range 0-255";
} else {
if (bonding->fail_over_mac != NI_BOND_FAIL_OVER_MAC_NONE)
return "fail-over-mac only valid in active-backup mode";
if (bonding->num_grat_arp > 1)
return "gratuitous ARP count valid in active-backup only ";
if (bonding->num_unsol_na > 1)
return "unsolicited IPv6-NA count valid in active-backup only";
}
switch (bonding->mode) {
case NI_BOND_MODE_ACTIVE_BACKUP:
case NI_BOND_MODE_BALANCE_TLB:
case NI_BOND_MODE_BALANCE_ALB:
switch (bonding->primary_reselect) {
case NI_BOND_PRIMARY_RESELECT_ALWAYS:
case NI_BOND_PRIMARY_RESELECT_BETTER:
case NI_BOND_PRIMARY_RESELECT_FAILURE:
break;
default:
return "unsupported primary reselect setting";
}
break;
default:
if (bonding->primary_reselect != NI_BOND_PRIMARY_RESELECT_ALWAYS)
return "primary reselect is not supported in current bonding mode";
if (bonding->primary_slave != NULL)
return "primary slave is not supported in current bonding mode";
if (bonding->active_slave != NULL)
return "active slave is not supported in current bonding mode";
break;
}
if (bonding->all_slaves_active > 1)
return "invalid all slaves active flag";
return NULL;
}
ni_bool_t
ni_bonding_is_valid_arp_ip_target(const char *target)
{
struct in_addr addr;
if (!target || inet_pton(AF_INET, target, &addr) != 1)
return FALSE;
if (addr.s_addr == INADDR_ANY || addr.s_addr == INADDR_NONE)
return FALSE;
#if 0 /* senseless, but the kernel does not check it either */
if ((addr.s_addr >> 24) == IN_LOOPBACKNET)
return FALSE;
#endif
return TRUE;
}
/*
* Report if the bond contains a slave device
*/
ni_bool_t
ni_bonding_has_slave(ni_bonding_t *bonding, const char *ifname)
{
unsigned int i;
if (!bonding || !ifname || !*ifname)
return FALSE;
for (i = 0; i < bonding->slave_names.count; ++i) {
if (ni_string_eq(bonding->slave_names.data[i], ifname))
return TRUE;
}
return FALSE;
}
/*
* Add a slave device to the bond
*/
ni_bool_t
ni_bonding_add_slave(ni_bonding_t *bonding, const char *ifname)
{
if (!bonding || !ifname || !*ifname)
return FALSE;
if (ni_bonding_has_slave(bonding, ifname))
return FALSE;
return ni_string_array_append(&bonding->slave_names, ifname) == 0;
}
/*
* Set the bonding mode, using the strings supported by the
* module options
*/
static int
__ni_bonding_set_module_option_mode(ni_bonding_t *bonding, char *value)
{
int rv;
/* When we parse /sys/net/class/<ifname>/bonding/mode, we end up
* with "balance-rr 0" or similar; strip off the int value */
value[strcspn(value, " \t\n")] = '\0';
rv = ni_parse_uint_mapped(value, __map_kern_mode, &bonding->mode);
if (rv < 0)
ni_error("bonding: kernel reports unknown mode \"%s\"", value);
return rv;
}
static int
__ni_bonding_get_module_option_mode(const ni_bonding_t *bonding, char *buffer, size_t bufsize)
{
const char *name;
name = ni_format_uint_mapped(bonding->mode, __map_kern_mode);
if (name == NULL) {
ni_error("bonding: unsupported bonding mode %u", bonding->mode);
return -1;
}
strncpy(buffer, name, bufsize - 1);
buffer[bufsize - 1] = '\0';
return 0;
}
const char *
ni_bonding_mode_type_to_name(unsigned int mode)
{
return ni_format_uint_mapped(mode, __map_user_mode);
}
int
ni_bonding_mode_name_to_type(const char *name)
{
unsigned int value;
if (ni_parse_uint_maybe_mapped(name, __map_user_mode, &value, 10) != 0)
return -1;
return value;
}
/*
* Set the validation mode of ARP probes.
*/
static int
__ni_bonding_set_module_option_arp_validate(ni_bonding_t *bonding, char *value)
{
int rv;
/* When we parse /sys/net/class/<ifname>/bonding/arp_validate, we end up
* with "none 0" or similar; strip off the int value */
value[strcspn(value, " \t\n")] = '\0';
rv = ni_parse_uint_mapped(value, __map_kern_arp_validate,
&bonding->arpmon.validate);
if (rv < 0)
ni_error("bonding: kernel reports unknown arp_validate mode \"%s\"", value);
return rv;
}
static int
__ni_bonding_get_module_option_arp_validate(const ni_bonding_t *bonding, char *buffer, size_t bufsize)
{
const char *name;
name = ni_format_uint_mapped(bonding->arpmon.validate,
__map_kern_arp_validate);
if (name == NULL) {
ni_error("bonding: unsupported arp_validate mode %u", bonding->arpmon.validate);
return -1;
}
strncpy(buffer, name, bufsize - 1);
return 0;
}
const char *
ni_bonding_arp_validate_type_to_name(unsigned int value)
{
return ni_format_uint_mapped(value, __map_kern_arp_validate);
}
int
ni_bonding_arp_validate_name_to_type(const char *name)
{
unsigned int value;
if (ni_parse_uint_mapped(name, __map_kern_arp_validate, &value) < 0)
return -1;
return value;
}
const char *
ni_bonding_mii_carrier_detect_name(unsigned int type)
{
return ni_format_uint_mapped(type, __map_user_carrier_detect);
}
int
ni_bonding_mii_carrier_detect_type(const char *name)
{
unsigned int type;
if (ni_parse_uint_mapped(name, __map_user_carrier_detect, &type) < 0)
return -1;
return type;
}
/*
* Set the xmit hash policy
*/
static int
__ni_bonding_set_module_option_xmit_hash_policy(ni_bonding_t *bonding, char *value)
{
int rv;
value[strcspn(value, " \t\n")] = '\0';
rv = ni_parse_uint_mapped(value, __map_kern_xmit_hash_policy,
&bonding->xmit_hash_policy);
if (rv < 0)
ni_error("bonding: kernel reports unknown xmit_hash_policy mode \"%s\"", value);
return rv;
}
int
__ni_bonding_get_module_option_xmit_hash_policy(const ni_bonding_t *bonding, char *buffer, size_t bufsize)
{
const char *name;
name = ni_format_uint_mapped(bonding->xmit_hash_policy,
__map_kern_xmit_hash_policy);
if (name == NULL) {
ni_error("bonding: unsupported xmit_hash_policy %u", bonding->xmit_hash_policy);
return -1;
}
strncpy(buffer, name, bufsize - 1);
return 0;
}
/*
* For now, the enum names in the xml schema use the same xmit hash policy names as
* the kernel.
*/
const char *
ni_bonding_xmit_hash_policy_to_name(unsigned int value)
{
return ni_format_uint_mapped(value, __map_user_xmit_hash_policy);
}
int
ni_bonding_xmit_hash_policy_to_type(const char *name)
{
unsigned int value;
if (ni_parse_uint_mapped(name, __map_user_xmit_hash_policy, &value) < 0)
return -1;
return value;
}
const char *
ni_bonding_lacp_rate_name(unsigned int mode)
{
return ni_format_uint_mapped(mode, __map_kern_lacp_rate);
}
int
ni_bonding_lacp_rate_mode(const char *name)
{
unsigned int value;
if (ni_parse_uint_maybe_mapped(name, __map_kern_lacp_rate, &value, 10) < 0)
return -1;
return value;
}
const char *
ni_bonding_ad_select_name(unsigned int mode)
{
return ni_format_uint_mapped(mode, __map_kern_ad_select);
}
int
ni_bonding_ad_select_mode(const char *name)
{
unsigned int value;
if (ni_parse_uint_maybe_mapped(name, __map_kern_ad_select, &value, 10) < 0)
return -1;
return value;
}
const char *
ni_bonding_primary_reselect_name(unsigned int mode)
{
return ni_format_uint_mapped(mode, __map_kern_primary_reselect);
}
int
ni_bonding_primary_reselect_mode(const char *name)
{
unsigned int value;
if (ni_parse_uint_maybe_mapped(name, __map_kern_primary_reselect, &value, 10) < 0)
return -1;
return value;
}
const char *
ni_bonding_fail_over_mac_name(unsigned int mode)
{
return ni_format_uint_mapped(mode, __map_kern_fail_over_mac);
}
int
ni_bonding_fail_over_mac_mode(const char *name)
{
unsigned int value;
if (ni_parse_uint_maybe_mapped(name, __map_kern_fail_over_mac, &value, 10) < 0)
return -1;
return value;
}
/*
* Set one bonding module option/attribute
*/
static int
ni_bonding_parse_sysfs_attribute(ni_bonding_t *bonding, const char *attr, char *value)
{
if (!strcmp(attr, "mode")) {
if (__ni_bonding_set_module_option_mode(bonding, value) < 0)
return -1;
} else if (!strcmp(attr, "fail_over_mac")) {
value[strcspn(value, " \t\n")] = '\0';
if (ni_parse_uint_mapped(value, __map_kern_fail_over_mac,
&bonding->fail_over_mac) < 0)
return -1;
} else if (!strcmp(attr, "primary_reselect")) {
value[strcspn(value, " \t\n")] = '\0';
if (ni_parse_uint_mapped(value, __map_kern_primary_reselect,
&bonding->primary_reselect) < 0)
return -1;
} else if (!strcmp(attr, "xmit_hash_policy")) {
if (__ni_bonding_set_module_option_xmit_hash_policy(bonding, value) < 0)
return -1;
} else if (!strcmp(attr, "lacp_rate")) {
value[strcspn(value, " \t\n")] = '\0';
if (ni_parse_uint_mapped(value, __map_kern_lacp_rate,
&bonding->lacp_rate) < 0)
return -1;
} else if (!strcmp(attr, "ad_select")) {
value[strcspn(value, " \t\n")] = '\0';
if (ni_parse_uint_mapped(value, __map_kern_ad_select,
&bonding->ad_select) < 0)
return -1;
} else if (!strcmp(attr, "min_links")) {
if (ni_parse_uint(value, &bonding->min_links, 10) < 0)
return -1;
} else if (!strcmp(attr, "num_grat_arp")) {
if (ni_parse_uint(value, &bonding->num_grat_arp, 10) < 0)
return -1;
} else if (!strcmp(attr, "num_unsol_na")) {
if (ni_parse_uint(value, &bonding->num_unsol_na, 10) < 0)
return -1;
} else if (!strcmp(attr, "resend_igmp")) {
if (ni_parse_uint(value, &bonding->resend_igmp, 10) < 0)
return -1;
} else if (!strcmp(attr, "all_slaves_active")) {
unsigned int tmp;
if (ni_parse_uint(value, &tmp, 10) < 0)
return -1;
bonding->all_slaves_active = tmp;
} else if (!strcmp(attr, "miimon")) {
if (ni_parse_uint(value, &bonding->miimon.frequency, 10) < 0)
return -1;
if (bonding->miimon.frequency > 0)
bonding->monitoring = NI_BOND_MONITOR_MII;
} else if (!strcmp(attr, "updelay")) {
if (ni_parse_uint(value, &bonding->miimon.updelay, 10) < 0)
return -1;
} else if (!strcmp(attr, "downdelay")) {
if (ni_parse_uint(value, &bonding->miimon.downdelay, 10) < 0)
return -1;
} else if (!strcmp(attr, "use_carrier")) {
if (ni_parse_uint(value, &bonding->miimon.carrier_detect, 10) < 0)
return -1;
} else if (!strcmp(attr, "arp_validate")) {
if (__ni_bonding_set_module_option_arp_validate(bonding, value) < 0)
return -1;
} else if (!strcmp(attr, "arp_interval")) {
if (ni_parse_uint(value, &bonding->arpmon.interval, 10) < 0)
return -1;
if (bonding->arpmon.interval > 0)
bonding->monitoring = NI_BOND_MONITOR_ARP;
} else if (!strcmp(attr, "arp_ip_target")) {
char *s, *p = NULL;
for (s = strtok_r(value, ",", &p); s; s = strtok_r(NULL, ",", &p)) {
if (ni_bonding_is_valid_arp_ip_target(s))
ni_string_array_append(&bonding->arpmon.targets, s);
}
} else if (!strcmp(attr, "primary")) {
ni_string_dup(&bonding->primary_slave, value);
} else if (!strcmp(attr, "active_slave")) {
ni_string_dup(&bonding->active_slave, value);
} else {
return -2;
}
return 0;
}
/*
* Get one bonding module option/attribute
*/
static int
ni_bonding_format_sysfs_attribute(const ni_bonding_t *bonding, const char *attr, char *buffer, size_t bufsize)
{
const char *ptr;
memset(buffer, 0, bufsize);
if (!strcmp(attr, "mode")) {
return __ni_bonding_get_module_option_mode(bonding, buffer, bufsize);
} else if (!strcmp(attr, "fail_over_mac")) {
if ((ptr = ni_bonding_fail_over_mac_name(bonding->fail_over_mac)) == NULL)
return -1;
snprintf(buffer, bufsize, "%s", ptr);
} else if (!strcmp(attr, "primary_reselect")) {
if ((ptr = ni_bonding_primary_reselect_name(bonding->primary_reselect)) == NULL)
return -1;
snprintf(buffer, bufsize, "%s", ptr);
} else if (!strcmp(attr, "xmit_hash_policy")) {
return __ni_bonding_get_module_option_xmit_hash_policy(bonding, buffer, bufsize);
} else if (!strcmp(attr, "lacp_rate")) {
if ((ptr = ni_bonding_lacp_rate_name(bonding->lacp_rate)) == NULL)
return -1;
snprintf(buffer, bufsize, "%s", ptr);
} else if (!strcmp(attr, "ad_select")) {
if ((ptr = ni_bonding_ad_select_name(bonding->ad_select)) == NULL)
return -1;
snprintf(buffer, bufsize, "%s", ptr);
} else if (!strcmp(attr, "min_links")) {
snprintf(buffer, bufsize, "%u", bonding->min_links);
} else if (!strcmp(attr, "num_grat_arp")) {
snprintf(buffer, bufsize, "%u", bonding->num_grat_arp);
} else if (!strcmp(attr, "num_unsol_na")) {
snprintf(buffer, bufsize, "%u", bonding->num_unsol_na);
} else if (!strcmp(attr, "resend_igmp")) {
snprintf(buffer, bufsize, "%u", bonding->resend_igmp);
} else if (!strcmp(attr, "all_slaves_active")) {
snprintf(buffer, bufsize, "%u", bonding->all_slaves_active);
} else if (!strcmp(attr, "miimon")) {
unsigned int freq = 0;
if (bonding->monitoring == NI_BOND_MONITOR_MII)
freq = bonding->miimon.frequency;
snprintf(buffer, bufsize, "%u", freq);
} else if (!strcmp(attr, "updelay")) {
if (bonding->monitoring != NI_BOND_MONITOR_MII)
return 0;
snprintf(buffer, bufsize, "%u", bonding->miimon.updelay);
} else if (!strcmp(attr, "downdelay")) {
if (bonding->monitoring != NI_BOND_MONITOR_MII)
return 0;
snprintf(buffer, bufsize, "%u", bonding->miimon.downdelay);
} else if (!strcmp(attr, "use_carrier")) {
if (bonding->monitoring != NI_BOND_MONITOR_MII)
return 0;
snprintf(buffer, bufsize, "%u", bonding->miimon.carrier_detect);
} else if (!strcmp(attr, "arp_validate")) {
if (bonding->monitoring != NI_BOND_MONITOR_ARP)
return 0;
return __ni_bonding_get_module_option_arp_validate(bonding, buffer, bufsize);
} else if (!strcmp(attr, "arp_interval")) {
if (bonding->monitoring != NI_BOND_MONITOR_ARP)
return 0;
snprintf(buffer, bufsize, "%u", bonding->arpmon.interval);
} else if (!strcmp(attr, "active_slave")) {
if (!bonding->active_slave)
return 0;
snprintf(buffer, bufsize, "%s", bonding->active_slave);
} else if (!strcmp(attr, "primary")) {
if (!bonding->primary_slave)
return 0;
snprintf(buffer, bufsize, "%s", bonding->primary_slave);
} else {
return -1;
}
return 0;
}
/*
* Load bonding configuration from sysfs
*/
int
ni_bonding_parse_sysfs_attrs(const char *ifname, ni_bonding_t *bonding)
{
static const struct {
const char *name;
ni_bool_t nofail; /* don't fail, may be missed */
} attrs[] = {
{ "mode", FALSE },
{ "fail_over_mac", FALSE },
{ "primary_reselect", FALSE },
{ "xmit_hash_policy", FALSE },
{ "lacp_rate", FALSE },
{ "ad_select", FALSE },
{ "min_links", TRUE },
{ "num_grat_arp", FALSE },
{ "num_unsol_na", FALSE },
{ "resend_igmp", FALSE },
{ "all_slaves_active", FALSE },
{ "active_slave", FALSE },
{ "primary", FALSE },
{ "miimon", FALSE },
{ "updelay", FALSE },
{ "downdelay", FALSE },
{ "use_carrier", FALSE },
{ "arp_validate", FALSE },
{ "arp_interval", FALSE },
{ NULL, FALSE },
};
char *attrval = NULL;
unsigned int i;
__ni_bonding_clear(bonding);
ni_sysfs_bonding_get_slaves(ifname, &bonding->slave_names);
for (i = 0; attrs[i].name; ++i) {
const char *attrname = attrs[i].name;
int rv;
if (ni_sysfs_bonding_get_attr(ifname, attrname, &attrval) < 0) {
if (attrs[i].nofail)
continue;
ni_error("%s: cannot get bonding attribute %s", ifname, attrname);
goto failed;
}
if (attrval == NULL)
continue;
rv = ni_bonding_parse_sysfs_attribute(bonding, attrname, attrval);
if (rv == -2) {
ni_error("ignoring unknown bonding module option %s=%s", attrname, attrval);
} else if (rv < 0) {
ni_error("unable to parse bonding module option %s=%s", attrname, attrval);
goto failed;
}
}
ni_sysfs_bonding_get_arp_targets(ifname, &bonding->arpmon.targets);
ni_string_free(&attrval);
return 0;
failed:
ni_string_free(&attrval);
return -1;
}
/*
* Write one sysfs attribute
*/
static int
ni_bonding_write_one_sysfs_attr(const char *ifname, const ni_bonding_t *bonding,
const ni_bonding_t *current, const char *attrname)
{
char current_value[128], config_value[128];
if (ni_bonding_format_sysfs_attribute(current, attrname, current_value, sizeof(current_value)) < 0
|| ni_bonding_format_sysfs_attribute(bonding, attrname, config_value, sizeof(config_value)) < 0) {
ni_error("%s: cannot represent attribute %s", ifname, attrname);
return -1;
}
#if 0
ni_debug_ifconfig("%s: checking attr %s: cur=%s cfg=%s", ifname, attrname,
current_value, config_value);
#endif
if (config_value[0] == '\0') {
ni_debug_ifconfig("%s: ignoring attr: %s", ifname, attrname);
return 0;
}
if (!strcmp(current_value, config_value)) {
ni_debug_ifconfig("%s: unchanged attr: %s", ifname, attrname);
return 0;
}
ni_debug_ifconfig("%s: setting attr: %s=%s", ifname, attrname, config_value);
if (ni_sysfs_bonding_set_attr(ifname, attrname, config_value) < 0) {
ni_error("%s: cannot set bonding attribute %s=%s", ifname, attrname, config_value);
return -1;
}
return 0;
}
/*
* Write bonding configuration to sysfs.
* This happens in two stages, prior to enslaving interfaces and after,
* as well as in dependency of the bonding up/down state and slave count.
*/
int
ni_bonding_write_sysfs_attrs(const char *ifname, const ni_bonding_t *bonding, const ni_bonding_t *current, ni_bool_t is_up, ni_bool_t has_slaves)
{
/*
* option up/down slaves modes
* -------------------------------------------------------------
* mode: down !slaves
* fail_over_mac: !slaves AB
* xmit_hash_policy: down 3AD, XOR
* lacp_rate: down 3AD
* ad_select: down 3AD
* min_links: (down) 3AD
* num_grat_arp: AB
* num_unsol_na: AB
* resend_igmp: AB, TLB, ALB, RR
* all_slaves_active:
* primary_reselect: AB, TLB, ALB
* primary: AB, TLB, ALB
* active_slave: up slaves AB, TLB, ALB
* arp_interval: !TLB, !ALB, !3AD
* arp_ip_target: !TLB, !ALB, !3AD
* arp_validate: BAK
* miimon:
* updelay:
* downdelay:
* use_carrier:
*/
struct attr_matrix {
const char * name;
int bstate; /* 1: bond down, 2: bond up */