From 82a19cac1ff99b0dbc59622a2430f0f8e0050e48 Mon Sep 17 00:00:00 2001 From: Neil McKee Date: Tue, 14 Mar 2023 16:30:32 -0700 Subject: [PATCH] allow pcap{} config to override sampling rate --- hsflowd.spec | 2 +- src/Linux/hsflowconfig.c | 25 ++++++++++++++++++++----- src/Linux/hsflowd.h | 3 +++ src/Linux/mod_pcap.c | 8 ++++++-- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/hsflowd.spec b/hsflowd.spec index a543471..7a38307 100644 --- a/hsflowd.spec +++ b/hsflowd.spec @@ -1,7 +1,7 @@ Summary: host sFlow daemon Name: hsflowd Version: 2.0.47 -Release: 2 +Release: 3 License: http://sflow.net/license.html Group: Applications/Internet URL: http://sflow.net diff --git a/src/Linux/hsflowconfig.c b/src/Linux/hsflowconfig.c index e9ec0c4..77d3e40 100644 --- a/src/Linux/hsflowconfig.c +++ b/src/Linux/hsflowconfig.c @@ -824,8 +824,8 @@ extern "C" { method = "interface_down"; } else { - char speedStr[51]; if(adaptor->ifSpeed) { + char speedStr[51]; if(printSpeed(adaptor->ifSpeed, speedStr, 50) && lookupApplicationSettings(settings, NULL, speedStr, &sampling_n, NULL)) { method = speedStr; @@ -834,11 +834,22 @@ extern "C" { // 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... - sampling_n = adaptor->ifSpeed / HSP_SPEED_SAMPLING_RATIO; - if(sampling_n < HSP_SPEED_SAMPLING_MIN) { - sampling_n = HSP_SPEED_SAMPLING_MIN; + uint32_t bpsRatio = 0; + if(lookupApplicationSettings(settings, NULL, HSP_BPS_RATIO, &bpsRatio, NULL)) { + // samlping.bps_ratio=0 turns off the behavior, falling back on global default + if(bpsRatio > 0) { + sampling_n = adaptor->ifSpeed / bpsRatio; + 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"; } - method = "speed_default"; } } } @@ -1749,6 +1760,10 @@ extern "C" { if((tok = expectIntegerRange64(sp, tok, &pc->speed_min, &pc->speed_max, 0, LLONG_MAX)) == NULL) return NO; pc->speed_set = YES; break; + case HSPTOKEN_SAMPLING: + if((tok = expectInteger32(sp, tok, &pc->sampling_n, 0, HSP_MAX_SAMPLING_N)) == NULL) return NO; + pc->sampling_n_set = YES; + break; default: unexpectedToken(sp, tok, level[depth]); return NO; diff --git a/src/Linux/hsflowd.h b/src/Linux/hsflowd.h index e905c28..aa2aab1 100644 --- a/src/Linux/hsflowd.h +++ b/src/Linux/hsflowd.h @@ -146,6 +146,7 @@ extern "C" { // For when a switch-port is configured using the default // (calculated) sampling-rate (based on link speed) +#define HSP_BPS_RATIO "bps_ratio" #define HSP_SPEED_SAMPLING_RATIO 1000000 #define HSP_SPEED_SAMPLING_MIN 100 @@ -170,6 +171,8 @@ extern "C" { uint64_t speed_min; uint64_t speed_max; bool speed_set; + uint32_t sampling_n; + bool sampling_n_set; } HSPPcap; typedef struct _HSPPort { diff --git a/src/Linux/mod_pcap.c b/src/Linux/mod_pcap.c index dbcd2b7..70f47fa 100644 --- a/src/Linux/mod_pcap.c +++ b/src/Linux/mod_pcap.c @@ -32,6 +32,7 @@ extern "C" { bool promisc:1; bool vport:1; bool vport_set:1; + bool samplingRateSet:1; // set with pcap{sampling=} pcap_t *pcap; char pcap_err[PCAP_ERRBUF_SIZE]; int n_dlts; @@ -266,8 +267,9 @@ extern "C" { static void tap_open(EVMod *mod, BPFSoc *bpfs) { HSP_mod_PCAP *mdata = (HSP_mod_PCAP *)mod->data; HSP *sp = (HSP *)EVROOTDATA(mod); - - bpfs->samplingRate = lookupPacketSamplingRate(bpfs->adaptor, sp->sFlowSettings); + + if(!bpfs->samplingRateSet) + bpfs->samplingRate = lookupPacketSamplingRate(bpfs->adaptor, sp->sFlowSettings); bpfs->subSamplingRate = bpfs->samplingRate; // create pcap @@ -397,6 +399,8 @@ extern "C" { bpfs->promisc = pcap->promisc; bpfs->vport = pcap->vport; bpfs->vport_set = pcap->vport_set; + bpfs->samplingRate = pcap->sampling_n; + bpfs->samplingRateSet = pcap->sampling_n_set; tap_open(mod, bpfs); }