Skip to content

Commit

Permalink
allow pcap{} config to override sampling rate
Browse files Browse the repository at this point in the history
  • Loading branch information
sflow committed Mar 14, 2023
1 parent a7f9127 commit 82a19ca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hsflowd.spec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 20 additions & 5 deletions src/Linux/hsflowconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/Linux/hsflowd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions src/Linux/mod_pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
bool promisc:1;
bool vport:1;
bool vport_set:1;
bool samplingRateSet:1; // set with pcap{sampling=<n>}
pcap_t *pcap;
char pcap_err[PCAP_ERRBUF_SIZE];
int n_dlts;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 82a19ca

Please sign in to comment.