-
Notifications
You must be signed in to change notification settings - Fork 55
/
hsflowconfig.c
2232 lines (2055 loc) · 68.8 KB
/
hsflowconfig.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
/* This software is distributed under the following license:
* http://sflow.net/license.html
*/
#if defined(__cplusplus)
extern "C" {
#endif
#include "hsflowd.h"
#define HSP_MAX_SAMPLING_N 10000000
#define HSP_MAX_POLLING_S 300
#define HSP_MAX_NOTIFY_RATELIMIT 10000
#define HSP_MAX_LINELEN 2048
#define HSP_MAX_CONFIG_DEPTH 3
#define HSP_SEPARATORS " \t\r\n=;"
typedef enum { HSPTOKENTYPE_UNDEFINED=0,
HSPTOKENTYPE_SYNTAX,
HSPTOKENTYPE_OBJ,
HSPTOKENTYPE_ATTRIB } EnumHSPTokenType;
// read the special tokens include twice,
// first to generate the enum, then to
// populate the special token lookup table
typedef enum {
#define HSPTOKEN_DATA(tag, token, type, deprecated) tag,
#include "hsflowtokens.h"
#undef HSPTOKEN_DATA
HSPTOKEN_NUM_TOKENS } EnumHSPSpecialToken;
typedef struct _HSPSpecialToken {
EnumHSPSpecialToken tag;
char *str;
EnumHSPTokenType type;
char *deprecated;
} HSPSpecialToken;
static const HSPSpecialToken HSPSpecialTokens[] = {
#define HSPTOKEN_DATA(tag, token, type, deprecated) { tag, token, type, deprecated },
#include "hsflowtokens.h"
#undef HSPTOKEN_DATA
};
typedef struct _HSPToken {
struct _HSPToken *nxt;
uint32_t lineNo;
char *str;
EnumHSPSpecialToken stok;
} HSPToken;
typedef enum {
HSPOBJ_HSP=0,
HSPOBJ_SFLOW,
HSPOBJ_DNSSD,
HSPOBJ_COLLECTOR,
HSPOBJ_JSON,
HSPOBJ_XEN,
HSPOBJ_KVM,
HSPOBJ_DOCKER,
HSPOBJ_CONTAINERD,
HSPOBJ_K8S,
HSPOBJ_ULOG,
HSPOBJ_NFLOG,
HSPOBJ_PSAMPLE,
HSPOBJ_DROPMON,
HSPOBJ_PCAP,
HSPOBJ_TCP,
HSPOBJ_CUMULUS,
HSPOBJ_DENT,
HSPOBJ_NVML,
HSPOBJ_OVS,
HSPOBJ_OS10,
HSPOBJ_OPX,
HSPOBJ_SONIC,
HSPOBJ_DBUS,
HSPOBJ_SYSTEMD,
HSPOBJ_EAPI,
HSPOBJ_PORT
} EnumHSPObject;
static const char *HSPObjectNames[] = {
"host-sflow-probe",
"sflow",
"dns-sd",
"collector",
"json",
"xen",
"kvm",
"docker",
"containerd",
"k8s",
"ulog",
"nflog",
"psample",
"dropmon",
"pcap",
"tcp",
"cumulus",
"dent",
"nvml",
"ovs",
"os10",
"opx",
"sonic",
"dbus",
"systemd",
"eapi",
"port"
};
static void copyApplicationSettings(HSPSFlowSettings *from, HSPSFlowSettings *to);
static void copyAgentCIDRs(HSPSFlowSettings *from, HSPSFlowSettings *to);
/*_________________---------------------------__________________
_________________ parseError __________________
-----------------___________________________------------------
*/
static void parseError(HSP *sp, HSPToken *tok, char *msg1, char *msg2)
{
myLog(LOG_ERR, "parse error at <%s><%s> on line %d of %s : %s %s",
tok->str,
tok->nxt ? tok->nxt->str : "",
tok->lineNo,
sp->configFile,
msg1,
msg2);
}
/*_________________---------------------------__________________
_________________ unexpected __________________
-----------------___________________________------------------
*/
static void unexpectedToken(HSP *sp, HSPToken *tok, EnumHSPObject level)
{
myLog(LOG_ERR, "parse error at <%s><%s> on line %d of %s : unexpected %s setting",
tok->str,
tok->nxt ? tok->nxt->str : "",
tok->lineNo,
sp->configFile,
HSPObjectNames[level]);
}
/*_________________---------------------------__________________
_________________ attribute extraction __________________
-----------------___________________________------------------
*/
// expectToken
static HSPToken *expectToken(HSP *sp, HSPToken *tok, EnumHSPSpecialToken stok)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || t->stok != stok) {
parseError(sp, tok, "expected ", HSPSpecialTokens[stok].str);
return NULL;
}
return t;
}
// expectInteger32
static uint32_t getMultiplier32(char *str)
{
uint32_t mult = 1;
uint32_t len = my_strlen(str);
char last = toupper(str[len - 1]);
if(last == 'K' || last == 'M' || last == 'G') {
// number of the form "100M" or "1G"
str[len - 1] = '\0'; // blat the K, M or G
if(last == 'K') mult = 1000;
if(last == 'M') mult = 1000000;
if(last == 'G') mult = 1000000000;
}
return mult;
}
static HSPToken *expectInteger32(HSP *sp, HSPToken *tok, uint32_t *arg, uint32_t minVal, uint32_t maxVal)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || !isdigit(t->str[0])) {
parseError(sp, tok, "expected integer", "");
return NULL;
}
char *str = my_strdup(t->str); // take a copy so we can modify it
uint32_t mult = getMultiplier32(str);
*arg = (mult * strtol(str, NULL, 0));
my_free(str);
if(*arg < minVal || *arg > maxVal) {
parseError(sp, tok, "range error", "");
return NULL;
}
return t;
}
static uint64_t getMultiplier64(char *str)
{
uint64_t mult = 1;
uint32_t len = my_strlen(str);
char last = toupper(str[len - 1]);
if(last == 'K' || last == 'M' || last == 'G' || last == 'T' || last == 'P') {
// number of the form "100M" or "1G"
str[len - 1] = '\0'; // blat the K, M, G, T or P
if(last == 'K') mult = 1000LL;
if(last == 'M') mult = 1000000LL;
if(last == 'G') mult = 1000000000LL;
if(last == 'T') mult = 1000000000000LL;
if(last == 'P') mult = 1000000000000000LL;
}
return mult;
}
#if 0 // expectInteger64 not needed yet
static HSPToken *expectInteger64(HSP *sp, HSPToken *tok, uint64_t *arg, uint64_t minVal, uint64_t maxVal)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || !isdigit(t->str[0])) {
parseError(sp, tok, "expected integer", "");
return NULL;
}
char *str = my_strdup(t->str); // take a copy so we can modify it
uint64_t mult = getMultiplier64(str);
*arg = (mult * strtoll(str, NULL, 0));
my_free(str);
if(*arg < minVal || *arg > maxVal) {
parseError(sp, tok, "range error", "");
return NULL;
}
return t;
}
#endif // expectInteger64
static HSPToken *expectIntegerRange64(HSP *sp, HSPToken *tok, uint64_t *arg1, uint64_t *arg2, uint64_t minVal, uint64_t maxVal)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || !isdigit(t->str[0])) {
parseError(sp, tok, "expected integer", "");
return NULL;
}
char *str = my_strdup(t->str); // take a copy so we can modify it
int len = my_strlen(str);
int len1 = strcspn(str, "-");
str[len1] = '\0';
uint64_t mult1 = getMultiplier64(str);
*arg1 = (mult1 * strtoll(str, NULL, 0));
if(*arg1 < minVal || *arg1 > maxVal) {
parseError(sp, tok, "range error", "");
return NULL;
}
if(len > len1) {
// we have at least a trailing '-' such as "1G-"
char *str2 = str + len1 + 1;
if(my_strlen(str2) == 0) {
// trailing dash. Allow that to mean "<max>"
*arg2 = maxVal;
}
else {
uint64_t mult2 = getMultiplier64(str2);
*arg2 = (mult2 * strtoll(str2, NULL, 0));
if(*arg2 < minVal || *arg2 > maxVal) {
parseError(sp, tok, "range error", "");
return NULL;
}
}
}
else {
// no second number - indicate by setting arg2 to 0
*arg2 = 0;
}
my_free(str);
return t;
}
// expectDouble
static HSPToken *expectDouble(HSP *sp, HSPToken *tok, double *arg, double minVal, double maxVal)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || !isdigit(t->str[0])) {
parseError(sp, tok, "expected floating-point number", "");
return NULL;
}
*arg = strtod(t->str, NULL);
if(*arg < minVal || *arg > maxVal) {
parseError(sp, tok, "range error", "");
return NULL;
}
return t;
}
// expectIP
static HSPToken *expectIP(HSP *sp, HSPToken *tok, SFLAddress *addr, struct sockaddr *sa)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || lookupAddress(t->str, sa, addr, 0) == NO) {
parseError(sp, tok, "expected hostname or IP", "");
return NULL;
}
return t;
}
// expectCIDR
static HSPToken *expectCIDR(HSP *sp, HSPToken *tok, HSPCIDR *cidr)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL
|| t->str == NULL) {
parseError(sp, tok, "expected IP CIDR", "");
return NULL;
}
char *cidrStr = t->str;
cidr->notFlag = NO;
if(cidrStr[0] == '!') {
cidr->notFlag = YES;
cidrStr++;
}
if(SFLAddress_parseCIDR(cidrStr, &cidr->ipAddr, &cidr->mask, &cidr->maskBits) == NO) {
parseError(sp, tok, "expected IP CIDR", "");
return NULL;
}
return t;
}
// expectONOFF
static HSPToken *expectONOFF(HSP *sp, HSPToken *tok, bool *arg)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || (strcasecmp(t->str, "on") != 0 && strcasecmp(t->str, "off") != 0)) {
parseError(sp, tok, "expected 'on' or 'off'", "");
return NULL;
}
(*arg) = (strcasecmp(t->str, "on") == 0);
return t;
}
// expectDirection
static HSPToken *expectDirection(HSP *sp, HSPToken *tok, int *arg)
{
HSPToken *t = tok;
t = t->nxt;
if(t && strcasecmp(t->str, "in") == 0) (*arg) = HSP_DIRN_IN;
else if(t && strcasecmp(t->str, "out") == 0) (*arg) = HSP_DIRN_OUT;
else if(t && strcasecmp(t->str, "both") == 0) (*arg) = HSP_DIRN_BOTH;
else {
parseError(sp, tok, "expected 'in' or 'out' or 'both'", "");
return NULL;
}
return t;
}
// expectDNSSD_domain
static HSPToken *expectDNSSD_domain(HSP *sp, HSPToken *tok)
{
HSPToken *t = tok;
t = t->nxt;
if(t && t->str) {
if(sp->DNSSD.domain) my_free(sp->DNSSD.domain);
sp->DNSSD.domain = my_strdup(t->str);
return t;
}
parseError(sp, tok, "expected domain", "");
return NULL;
}
// expectLoopback
static HSPToken *expectLoopback(HSP *sp, HSPToken *tok)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || (strcasecmp(t->str, "on") != 0 && strcasecmp(t->str, "off") != 0)) {
parseError(sp, tok, "expected 'on' or 'off'", "");
return NULL;
}
// this flag is effectively always-on now. So just consume and ignore it.
// enable or disable the inclusion of loopback interfaces
// sp->loopback = (strcasecmp(t->str, "on") == 0);
// have to force another read here, otherwise we have to wait for ever
// sp->refreshAdaptorList = YES;
return t;
}
// expectDevice
static HSPToken *expectDevice(HSP *sp, HSPToken *tok, char **p_devName)
{
HSPToken *t = tok;
t = t->nxt;
if(t && t->str) {
*p_devName = my_strdup(t->str);
return t;
// We now read the config file before we read the interfaces, so checking
// to ensure that this is a valid deviceName is now done later. Could therefore
// just use expectString() for this, but leave it here as a placeholder in
// case we want to tighten up the checks.
}
parseError(sp, tok, "expected device name", "");
return NULL;
}
// expectUUID
static HSPToken *expectUUID(HSP *sp, HSPToken *tok, char *uuid)
{
HSPToken *t = tok;
t = t->nxt;
if(t == NULL || parseUUID(t->str, uuid) == NO) {
parseError(sp, tok, "expected UUID", "");
return NULL;
}
return t;
}
// expectFile
static HSPToken *expectFile(HSP *sp, HSPToken *tok, char **p_fileName)
{
HSPToken *t = tok;
t = t->nxt;
if(t && t->str) {
struct stat statBuf;
if(stat(t->str, &statBuf) != 0) {
parseError(sp, tok, "WARNING:", "path does not exist");
// not a show-stopper. Let it go through.
}
*p_fileName = my_strdup(t->str);
return t;
}
parseError(sp, tok, "expected file name", "");
return NULL;
}
// expectString
static HSPToken *expectString(HSP *sp, HSPToken *tok, char **p_str, char *tokenType)
{
HSPToken *t = tok;
t = t->nxt;
if(t && t->str) {
*p_str = my_strdup(t->str);
return t;
}
parseError(sp, tok, "expected", tokenType);
return NULL;
}
// expectRegex
static HSPToken *expectRegex(HSP *sp, HSPToken *tok, regex_t **pattern)
{
HSPToken *t = tok;
t = t->nxt;
if(t && t->str) {
*pattern = UTRegexCompile(t->str);
return (*pattern) ? t : NULL;
}
parseError(sp, tok, "expected regex pattern", "");
return NULL;
}
/*_________________---------------------------__________________
_________________ new object fns __________________
-----------------___________________________------------------
*/
HSPCollector *newCollector(HSPSFlowSettings *sFlowSettings) {
HSPCollector *col = (HSPCollector *)my_calloc(sizeof(HSPCollector));
ADD_TO_LIST(sFlowSettings->collectors, col);
sFlowSettings->numCollectors++;
col->udpPort = SFL_DEFAULT_COLLECTOR_PORT;
col->namespace = NULL;
col->deviceName = NULL;
return col;
}
void clearCollectors(HSPSFlowSettings *settings)
{
for(HSPCollector *coll = settings->collectors; coll; ) {
HSPCollector *nextColl = coll->nxt;
if(coll->socket > 0) {
// treat this as an error condition. The sockets should
// be closed or zeroed in advance. This way it is easier
// to create and free configs without incurring unwelcome
// side effects.
myLog(LOG_ERR, "clearCollectors: socket still open");
}
if(coll->namespace)
my_free(coll->namespace);
if(coll->deviceName)
my_free(coll->deviceName);
my_free(coll);
coll = nextColl;
}
settings->collectors = NULL;
}
static void copyCollectors(HSPSFlowSettings *from, HSPSFlowSettings *to)
{
for(HSPCollector *coll = from->collectors; coll; coll = coll->nxt) {
HSPCollector *newColl = newCollector(to);
HSPCollector *nxtPtr = newColl->nxt;
// shallow copy - note this may also copy open socket fd.
*newColl = *coll;
// post copy
newColl->nxt = nxtPtr;
newColl->namespace = my_strdup(newColl->namespace);
newColl->deviceName = my_strdup(newColl->deviceName);
}
}
static HSPPcap *newPcap(HSP *sp) {
HSPPcap *col = (HSPPcap *)my_calloc(sizeof(HSPPcap));
ADD_TO_LIST(sp->pcap.pcaps, col);
sp->pcap.numPcaps++;
return col;
}
static HSPPort *newOPXPort(HSP *sp) {
HSPPort *prt = (HSPPort *)my_calloc(sizeof(HSPPort));
ADD_TO_LIST(sp->opx.ports, prt);
sp->opx.numPorts++;
return prt;
}
/*_________________---------------------------__________________
_________________ sFlowSettings lifecycle __________________
-----------------___________________________------------------
*/
HSPSFlowSettings *newSFlowSettings(void) {
HSPSFlowSettings *st = (HSPSFlowSettings *)my_calloc(sizeof(HSPSFlowSettings));
// initialize defaults
st->samplingRate = SFL_DEFAULT_SAMPLING_RATE;
st->pollingInterval = SFL_DEFAULT_POLLING_INTERVAL;
st->headerBytes = SFL_DEFAULT_HEADER_SIZE;
st->datagramBytes = SFL_DEFAULT_DATAGRAM_SIZE;
st->samplingDirection = HSP_DIRN_IN;
return st;
}
void freeSFlowSettings(HSPSFlowSettings *sFlowSettings) {
if(sFlowSettings) {
clearApplicationSettings(sFlowSettings);
clearAgentCIDRs(sFlowSettings);
clearCollectors(sFlowSettings);
if(sFlowSettings->agentDevice)
my_free(sFlowSettings->agentDevice);
my_free(sFlowSettings);
}
}
HSPSFlowSettings *copySFlowSettings(HSPSFlowSettings *from) {
HSPSFlowSettings *to = newSFlowSettings();
if(from) {
*to = *from;
to->collectors = NULL;
copyCollectors(from, to);
to->applicationSettings = NULL;
copyApplicationSettings(from, to);
to->agentCIDRs = NULL;
copyAgentCIDRs(from, to);
to->agentDevice = NULL;
if(from->agentDevice)
to->agentDevice = my_strdup(from->agentDevice);
}
return to;
}
/*_________________---------------------------__________________
_________________ sFlowSettingsString __________________
-----------------___________________________------------------
Only print the config fields that can be overridden dynamically
(e.g. via DNS-SD or from mod_sonic)
*/
char *sFlowSettingsString(HSP *sp, HSPSFlowSettings *settings)
{
UTStrBuf *buf = UTStrBuf_new();
if(settings) {
char ipbuf[64];
UTStrBuf_printf(buf, "hostname=%s\n", sp->hostname);
UTStrBuf_printf(buf, "sampling=%u\n", settings->samplingRate);
UTStrBuf_printf(buf, "header=%u\n", settings->headerBytes);
UTStrBuf_printf(buf, "datagram=%u\n", settings->datagramBytes);
UTStrBuf_printf(buf, "polling=%u\n", settings->pollingInterval);
// make sure the application specific ones always come after the general ones - to simplify the override logic there
for(HSPApplicationSettings *appSettings = settings->applicationSettings; appSettings; appSettings = appSettings->nxt) {
if(appSettings->got_sampling_n) {
UTStrBuf_printf(buf, "sampling.%s=%u\n", appSettings->application, appSettings->sampling_n);
}
if(appSettings->got_polling_secs) {
UTStrBuf_printf(buf, "polling.%s=%u\n", appSettings->application, appSettings->polling_secs);
}
}
// agentIP and/or agentDevice can override the config file (and auto-selection) if set.
// If these overrides are removed again in another dynamic update then we simply leave them
// out here and it should trigger a return to the previous behavior.
if(settings->agentIP.type)
UTStrBuf_printf(buf, "agentIP=%s\n", SFLAddress_print(&settings->agentIP, ipbuf, 63));
if(settings->agentDevice)
UTStrBuf_printf(buf, "agent=%s\n", settings->agentDevice);
// the DNS-SD responses seem to be reordering the collectors every time, so we have to take
// another step here to make sure they are sorted. Otherwise we think the config has changed
// every time(!)
UTStringArray *iplist = strArrayNew();
for(HSPCollector *collector = settings->collectors; collector; collector = collector->nxt) {
// make sure we ignore any where the foward lookup failed
// this might mean we write a .auto file with no collectors in it,
// so let's hope the slave agents all do the right thing with that(!)
if(collector->ipAddr.type != SFLADDRESSTYPE_UNDEFINED) {
char collectorStr[256];
// the evt_config_line syntax uses collector=ip/port/deviceName/namespace
// and the dnsSD and SONiC modules pass config lines around that way, but here we
// need to preserve the original collector=IP[ PORT] syntax so that unmodified
// sub-agents reading this file (such as mod_sflow for apache, nginx-sflow-module
// and jmx-sflow-agent) all see what they were expecting. At least, that's
// OK provided the dev and namespace are not set. If dev/namespace _are_ set then we
// need the sub-agents to interpret them correctly or not at all. So that's how
// we end up here, using the old syntax if it's just ip and port, but adopting
// the new syntax when we need to express ip/port/dev/ns.
SFLAddress_print(&collector->ipAddr, ipbuf, 63);
if(collector->deviceName == NULL
&& collector->namespace == NULL) {
// old syntax: collector=IP or collector=IP PORT
if(collector->udpPort == SFL_DEFAULT_COLLECTOR_PORT)
sprintf(collectorStr, "collector=%s\n", ipbuf);
else
sprintf(collectorStr, "collector=%s %u\n", ipbuf, collector->udpPort);
}
else {
// new syntax: collector=IP/PORT/DEV/NS
// TODO: use snprintf or another UTStrBuf here
sprintf(collectorStr, "collector=%s/%u/%s/%s\n",
ipbuf,
collector->udpPort,
collector->deviceName ?: "",
collector->namespace ?: "");
}
strArrayAdd(iplist, collectorStr);
}
}
strArraySort(iplist);
char *arrayStr = strArrayStr(iplist, NULL/*start*/, NULL/*quote*/, NULL/*delim*/, NULL/*end*/);
UTStrBuf_printf(buf, "%s", arrayStr);
my_free(arrayStr);
strArrayFree(iplist);
}
return UTStrBuf_unwrap(buf);
}
/*_________________---------------------------__________________
_________________ newSFlow __________________
-----------------___________________________------------------
*/
static void newSFlow(HSP *sp) {
sp->sFlowSettings_file = newSFlowSettings();
sp->subAgentId = HSP_DEFAULT_SUBAGENTID;
sp->xen.update_dominfo = 0;
sp->xen.dsk = 1;
sp->xen.vbd = STRINGIFY_DEF(HSP_XEN_VBD_PATH);
}
/*_________________---------------------------__________________
_________________ newToken __________________
-----------------___________________________------------------
*/
static HSPToken *newToken(char *str, int len) {
HSPToken *token = (HSPToken *)my_calloc(sizeof(HSPToken));
token->str = (char *)my_calloc(len + 1);
memcpy(token->str, str, len);
// if it is special token, then record the index number here too
for(uint32_t i = 0; i < HSPTOKEN_NUM_TOKENS; i++) {
if(strcasecmp(token->str, HSPSpecialTokens[i].str) == 0) {
token->stok = (EnumHSPSpecialToken)i;
}
}
return token;
}
/*_________________---------------------------__________________
_________________ getApplicationSettings __________________
-----------------___________________________------------------
*/
static HSPApplicationSettings *getApplicationSettings(HSPSFlowSettings *settings, char *app, bool create)
{
HSPApplicationSettings *appSettings = settings->applicationSettings;
for(; appSettings; appSettings = appSettings->nxt) if(my_strequal(app, appSettings->application)) break;
if(appSettings == NULL && create) {
appSettings = (HSPApplicationSettings *)my_calloc(sizeof(HSPApplicationSettings));
appSettings->application = my_strdup(app);
appSettings->nxt = settings->applicationSettings;
settings->applicationSettings = appSettings;
}
return appSettings;
}
/*_________________----------------------------__________________
_________________ clearApplicationSettings __________________
-----------------____________________________------------------
*/
void clearApplicationSettings(HSPSFlowSettings *settings)
{
for(HSPApplicationSettings *appSettings = settings->applicationSettings; appSettings; ) {
HSPApplicationSettings *nextAppSettings = appSettings->nxt;
my_free(appSettings->application);
my_free(appSettings);
appSettings = nextAppSettings;
}
settings->applicationSettings = NULL;
}
/*_________________----------------------------__________________
_________________ copyApplicationSettings __________________
-----------------____________________________------------------
*/
void copyApplicationSettings(HSPSFlowSettings *from, HSPSFlowSettings *to)
{
for(HSPApplicationSettings *appSettings = from->applicationSettings; appSettings; appSettings = appSettings->nxt) {
HSPApplicationSettings *newAppSettings = getApplicationSettings(to, appSettings->application, YES);
newAppSettings->got_sampling_n = appSettings->got_sampling_n;
newAppSettings->sampling_n = appSettings->sampling_n;
newAppSettings->got_polling_secs = appSettings->got_polling_secs;
newAppSettings->polling_secs = appSettings->polling_secs;
}
}
/*_________________----------------------------__________________
_________________ setApplicationSampling __________________
-----------------____________________________------------------
*/
void setApplicationSampling(HSPSFlowSettings *settings, char *app, uint32_t n)
{
HSPApplicationSettings *appSettings = getApplicationSettings(settings, app, YES);
appSettings->sampling_n = n;
appSettings->got_sampling_n = YES;
}
/*_________________----------------------------__________________
_________________ setApplicationPolling __________________
-----------------____________________________------------------
*/
void setApplicationPolling(HSPSFlowSettings *settings, char *app, uint32_t secs)
{
HSPApplicationSettings *appSettings = getApplicationSettings(settings, app, YES);
appSettings->polling_secs = secs;
appSettings->got_polling_secs = YES;
}
/*_________________----------------------------__________________
_________________ lookupApplicationSettings __________________
-----------------____________________________------------------
return a deepest match lookup, so that
a setting of sampling.app.xyz.pqr = 100 will apply to
an application named "app.xyz.pqr.abc" and take precendence
over a setting of sampling.app.xyz = 200
*/
int lookupApplicationSettings(HSPSFlowSettings *settings, char *prefix, char *app, uint32_t *p_sampling, uint32_t *p_polling)
{
// in the config, the sFlow-APPLICATION settings should always start with sampling.app.<name> or polling.app.<name>
// so add the .app prefix here before we start searching...
char *search = app;
int search_len = my_strlen(app);
if(prefix) {
search_len = my_strlen(app) + my_strlen(prefix) + 1;
search = my_calloc(search_len + 1);
snprintf(search, search_len + 1, "%s.%s", prefix, app);
}
// the top level settings are the defaults
if(p_polling) *p_polling = settings->pollingInterval;
if(p_sampling) *p_sampling = settings->samplingRate;
// now search for the deepest match
HSPApplicationSettings *deepest = NULL;
uint32_t deepest_len = 0;
for(HSPApplicationSettings *appSettings = settings->applicationSettings; appSettings; appSettings = appSettings->nxt) {
int len = my_strlen(appSettings->application);
if(len > deepest_len
&& len <= search_len
&& my_strnequal(search, appSettings->application, len)) {
// has to be an exact match, or one that matches up to a '.'
if(len == search_len || search[len] == '.') {
deepest = appSettings;
deepest_len = len;
}
}
}
if(prefix) {
my_free(search);
}
if(deepest) {
if(p_polling && deepest->got_polling_secs) *p_sampling = deepest->polling_secs;
if(p_sampling && deepest->got_sampling_n) *p_sampling = deepest->sampling_n;
return YES;
}
return NO;
}
/*_________________----------------------------__________________
_________________ lookupPacketSamplingRate __________________
-----------------____________________________------------------
*/
uint32_t lookupPacketSamplingRate(SFLAdaptor *adaptor, HSPSFlowSettings *settings)
{
assert(settings != NULL); // too soon! wait for config to be established
// This falls back on the default "sampling=<n>" setting if the speed is unknown or zero
uint32_t sampling_n = settings->samplingRate;
char *method = "global_default";
if(adaptor) {
HSPAdaptorNIO *adaptorNIO = ADAPTOR_NIO(adaptor);
if(adaptorNIO->up == NO) {
sampling_n = 0;
method = "interface_down";
}
else {
if(adaptor->ifSpeed) {
char speedStr[51];
if(printSpeed(adaptor->ifSpeed, speedStr, 50)
&& lookupApplicationSettings(settings, NULL, speedStr, &sampling_n, NULL)) {
method = speedStr;
}
else {
// calcuate default sampling rate based on link speed. This ensures
// that a network switch comes up with manageable defaults even if
// the config file is empty...
uint32_t bpsRatio = 0;
if(lookupApplicationSettings(settings, NULL, HSP_BPS_RATIO, &bpsRatio, NULL)) {
// sampling.bps_ratio=0 turns off the behavior, falling back on global default
if(bpsRatio > 0) {
sampling_n = adaptor->ifSpeed / bpsRatio;
if(sampling_n == 0)
sampling_n = 1;
method = HSP_BPS_RATIO;
}
}
else {
// use default bpsratio
sampling_n = adaptor->ifSpeed / HSP_SPEED_SAMPLING_RATIO;
if(sampling_n < HSP_SPEED_SAMPLING_MIN) {
sampling_n = HSP_SPEED_SAMPLING_MIN;
}
method = "speed_default";
}
}
}
}
myDebug(1, "%s (speed=%"PRIu64") using %s sampling rate = %u",
adaptor->deviceName,
adaptor->ifSpeed,
method,
sampling_n);
}
return sampling_n;
}
/*_________________---------------------------__________________
_________________ addAgentCIDR __________________
-----------------___________________________------------------
*/
void addAgentCIDR(HSPSFlowSettings *settings, HSPCIDR *cidr, bool atEnd)
{
HSPCIDR *mycidr = (HSPCIDR *)my_calloc(sizeof(HSPCIDR));
*mycidr = *cidr;
// ordering is important here. We want them in reverse order, but
// when we copy we have to preserve the order, so allow either way:
if(atEnd && settings->agentCIDRs) {
HSPCIDR *last = settings->agentCIDRs;
while(last->nxt) last = last->nxt;
mycidr->nxt = NULL;
last->nxt = mycidr;
}
else {
// at front
mycidr->nxt = settings->agentCIDRs;
settings->agentCIDRs = mycidr;
}
}
/*_________________---------------------------__________________
_________________ clearAgentCIDRs __________________
-----------------___________________________------------------
*/
void clearAgentCIDRs(HSPSFlowSettings *settings)
{
for(HSPCIDR *cidr = settings->agentCIDRs; cidr; ) {
HSPCIDR *next_cidr = cidr->nxt;
my_free(cidr);
cidr = next_cidr;
}
settings->agentCIDRs = NULL;
}
/*_________________---------------------------__________________
_________________ copyAgentCIDRs __________________
-----------------___________________________------------------
*/
static void copyAgentCIDRs(HSPSFlowSettings *from, HSPSFlowSettings *to)
{
for(HSPCIDR *cidr = from->agentCIDRs; cidr; cidr=cidr->nxt) {
addAgentCIDR(to, cidr, YES); // atEnd to preserve order
}
}
/*_________________---------------------------__________________
_________________ readTokens __________________
-----------------___________________________------------------
*/
static HSPToken *nextToken(char *p, char **out)
{
char *r = p;
HSPToken *token = NULL;
#define HSP_MAX_TOKEN_LEN 255
char buf[HSP_MAX_TOKEN_LEN+1];
// allow quoted strings so that a regex can have any chars (including "{}") in it
uint32_t pre_seps = strspn(r, HSP_SEPARATORS);
char *str = parseNextTok(&r, HSP_SEPARATORS, NO, '"', YES, buf, HSP_MAX_TOKEN_LEN);
if(str
&& *str != '\0') {
// found non-empty string, but watch out for a contiguous '{' or '}' token.
// If we find one then we'll just consume part of str...
uint32_t len = strcspn(str, "{}" HSP_SEPARATORS);
if(len == 0) len = 1; // => str started with '{' or '}'
token = newToken(str, len);
// tell the caller how many chars we actually consumed
*out = (p + pre_seps + len);
}
// return token or NULL
return token;
}
static HSPToken *reverseTokens(HSPToken *tokens)
{
HSPToken *rev = NULL;
for(HSPToken *tok = tokens; tok; ) {
HSPToken *nextTok = tok->nxt;
ADD_TO_LIST(rev, tok);
tok = nextTok;
}
return rev;
}
static HSPToken *readTokens(HSP *sp)
{
FILE *cfg = NULL;
if((cfg = fopen(sp->configFile, "r")) == NULL) {
myLog(LOG_ERR,"cannot open config file %s : %s", sp->configFile, strerror(errno));
return NULL;
}
// collect the tokens in a (reversed) list
HSPToken *tokens = newToken("start", 5);
char line[HSP_MAX_LINELEN];
uint32_t lineNo = 0;
int truncated;
while(my_readline(cfg, line, HSP_MAX_LINELEN, &truncated) != EOF) {
lineNo++;
char *p = line;
// comments start with '#'
p[strcspn(p, "#")] = '\0';
HSPToken *tok;