From 77ddbbb9c88cb499d702d1273383fb2c6fd7df1e Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 27 Nov 2025 13:06:46 +0100 Subject: [PATCH] counters: regular counter get local API update --- plugins/pfring/source-pfring.c | 17 +++++++++-------- src/counters.c | 4 ++-- src/counters.h | 2 +- src/source-af-packet.c | 4 ++-- src/source-af-xdp.c | 11 ++++++----- src/source-erf-dag.c | 15 +++++---------- src/source-netmap.c | 6 +++--- src/tests/detect.c | 8 ++++---- 8 files changed, 32 insertions(+), 35 deletions(-) diff --git a/plugins/pfring/source-pfring.c b/plugins/pfring/source-pfring.c index dc4db0737c..5cc29fb299 100644 --- a/plugins/pfring/source-pfring.c +++ b/plugins/pfring/source-pfring.c @@ -151,8 +151,8 @@ static inline void PfringDumpCounters(PfringThreadVars *ptv) * So to get the number of packet on the interface we can add * the newly seen packets and drops for this thread and add it * to the interface counter */ - uint64_t th_pkts = StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_packets); - uint64_t th_drops = StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops); + uint64_t th_pkts = StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_packets); + uint64_t th_drops = StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_drops); LiveDevicePktsAdd(ptv->livedev, pfring_s.recv - th_pkts); LiveDeviceDropAdd(ptv->livedev, pfring_s.drop - th_drops); StatsCounterSetI64(&ptv->tv->stats, ptv->capture_kernel_packets, pfring_s.recv); @@ -160,7 +160,8 @@ static inline void PfringDumpCounters(PfringThreadVars *ptv) #ifdef HAVE_PF_RING_FLOW_OFFLOAD if (ptv->flags & PFRING_FLAGS_BYPASS) { - uint64_t th_bypassed = StatsGetLocalCounterValue(ptv->tv, ptv->capture_bypassed); + uint64_t th_bypassed = + StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_bypassed); LiveDeviceBypassedAdd(ptv->livedev, pfring_s.shunt - th_bypassed); StatsCounterSetI64(&ptv->tv->stats, ptv->capture_bypassed, pfring_s.shunt); } @@ -620,14 +621,14 @@ void ReceivePfringThreadExitStats(ThreadVars *tv, void *data) PfringThreadVars *ptv = (PfringThreadVars *)data; PfringDumpCounters(ptv); - SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", dropped %" PRIu64 "", tv->name, - StatsGetLocalCounterValue(tv, ptv->capture_kernel_packets), - StatsGetLocalCounterValue(tv, ptv->capture_kernel_drops)); + SCLogPerf("(%s) Kernel: Packets %" PRIi64 ", dropped %" PRIi64 "", tv->name, + StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_packets), + StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_drops)); SCLogPerf("(%s) Packets %" PRIu64 ", bytes %" PRIu64 "", tv->name, ptv->pkts, ptv->bytes); #ifdef HAVE_PF_RING_FLOW_OFFLOAD if (ptv->flags & PFRING_FLAGS_BYPASS) { - SCLogPerf("(%s) Bypass: Packets %" PRIu64 "", tv->name, - StatsGetLocalCounterValue(tv, ptv->capture_bypassed)); + SCLogPerf("(%s) Bypass: Packets %" PRIi64 "", tv->name, + StatsCounterGetLocalValue(&tv->stats, ptv->capture_bypassed)); } #endif } diff --git a/src/counters.c b/src/counters.c index 622afa8821..228c13103f 100644 --- a/src/counters.c +++ b/src/counters.c @@ -1308,9 +1308,9 @@ static int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicTh * \retval 0 on success. * \retval -1 on error. */ -uint64_t StatsGetLocalCounterValue(ThreadVars *tv, StatsCounterId id) +int64_t StatsCounterGetLocalValue(StatsThreadContext *stats, StatsCounterId id) { - StatsPrivateThreadContext *pca = &tv->stats.priv; + StatsPrivateThreadContext *pca = &stats->priv; #ifdef DEBUG BUG_ON((id.id < 1) || (id.id > pca->size)); #endif diff --git a/src/counters.h b/src/counters.h index f26dd7c755..77b43cbd1b 100644 --- a/src/counters.h +++ b/src/counters.h @@ -148,7 +148,7 @@ void StatsCounterMaxUpdateI64(StatsThreadContext *, StatsCounterMaxId id, int64_ void StatsCounterAvgAddI64(StatsThreadContext *, StatsCounterAvgId id, int64_t x); /* utility functions */ -uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, StatsCounterId); +int64_t StatsCounterGetLocalValue(StatsThreadContext *, StatsCounterId); void StatsThreadInit(StatsThreadContext *); int StatsSetupPrivate(StatsThreadContext *, const char *); void StatsThreadCleanup(StatsThreadContext *); diff --git a/src/source-af-packet.c b/src/source-af-packet.c index 8b0ace2e12..d6a0907ff8 100644 --- a/src/source-af-packet.c +++ b/src/source-af-packet.c @@ -2696,8 +2696,8 @@ void ReceiveAFPThreadExitStats(ThreadVars *tv, void *data) #ifdef PACKET_STATISTICS AFPDumpCounters(ptv); SCLogPerf("%s: (%s) kernel: Packets %" PRIu64 ", dropped %" PRIu64 "", ptv->iface, tv->name, - StatsGetLocalCounterValue(tv, ptv->capture_kernel_packets), - StatsGetLocalCounterValue(tv, ptv->capture_kernel_drops)); + StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_packets), + StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_drops)); #endif } diff --git a/src/source-af-xdp.c b/src/source-af-xdp.c index 18567aebf2..09ece5b793 100644 --- a/src/source-af-xdp.c +++ b/src/source-af-xdp.c @@ -249,15 +249,16 @@ static inline void AFXDPDumpCounters(AFXDPThreadVars *ptv) uint64_t rx_dropped = stats.rx_dropped + stats.rx_invalid_descs + stats.rx_ring_full; StatsCounterAddI64(&ptv->tv->stats, ptv->capture_kernel_drops, - rx_dropped - StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops)); + rx_dropped - StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_drops)); StatsCounterAddI64(&ptv->tv->stats, ptv->capture_afxdp_packets, ptv->pkts); (void)SC_ATOMIC_SET(ptv->livedev->drop, rx_dropped); (void)SC_ATOMIC_ADD(ptv->livedev->pkts, ptv->pkts); SCLogDebug("(%s) Kernel: Packets %" PRIu64 ", bytes %" PRIu64 ", dropped %" PRIu64 "", - ptv->tv->name, StatsGetLocalCounterValue(ptv->tv, ptv->capture_afxdp_packets), - ptv->bytes, StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops)); + ptv->tv->name, + StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_afxdp_packets), ptv->bytes, + StatsCounterGetLocalValue(&ptv->tv->stats, ptv->capture_kernel_drops)); ptv->pkts = 0; } @@ -904,8 +905,8 @@ static void ReceiveAFXDPThreadExitStats(ThreadVars *tv, void *data) AFXDPDumpCounters(ptv); SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", bytes %" PRIu64 ", dropped %" PRIu64 "", tv->name, - StatsGetLocalCounterValue(tv, ptv->capture_afxdp_packets), ptv->bytes, - StatsGetLocalCounterValue(tv, ptv->capture_kernel_drops)); + StatsCounterGetLocalValue(&tv->stats, ptv->capture_afxdp_packets), ptv->bytes, + StatsCounterGetLocalValue(&tv->stats, ptv->capture_kernel_drops)); } /** diff --git a/src/source-erf-dag.c b/src/source-erf-dag.c index 2d396e93fe..624e60818f 100644 --- a/src/source-erf-dag.c +++ b/src/source-erf-dag.c @@ -534,17 +534,12 @@ ReceiveErfDagThreadExitStats(ThreadVars *tv, void *data) { ErfDagThreadVars *ewtn = (ErfDagThreadVars *)data; - (void)SC_ATOMIC_SET(ewtn->livedev->pkts, - StatsGetLocalCounterValue(tv, ewtn->packets)); - (void)SC_ATOMIC_SET(ewtn->livedev->drop, - StatsGetLocalCounterValue(tv, ewtn->drops)); + (void)SC_ATOMIC_SET(ewtn->livedev->pkts, StatsCounterGetLocalValue(&tv->stats, ewtn->packets)); + (void)SC_ATOMIC_SET(ewtn->livedev->drop, StatsCounterGetLocalValue(&tv->stats, ewtn->drops)); - SCLogInfo("Stream: %d; Bytes: %"PRIu64"; Packets: %"PRIu64 - "; Drops: %"PRIu64, - ewtn->dagstream, - ewtn->bytes, - StatsGetLocalCounterValue(tv, ewtn->packets), - StatsGetLocalCounterValue(tv, ewtn->drops)); + SCLogInfo("Stream: %d; Bytes: %" PRIu64 "; Packets: %" PRIi64 "; Drops: %" PRIi64, + ewtn->dagstream, ewtn->bytes, StatsCounterGetLocalValue(&tv->stats, ewtn->packets), + StatsCounterGetLocalValue(&tv->stats, ewtn->drops)); } /** diff --git a/src/source-netmap.c b/src/source-netmap.c index 78cd6cb431..ef736f5be5 100644 --- a/src/source-netmap.c +++ b/src/source-netmap.c @@ -867,10 +867,10 @@ static void ReceiveNetmapThreadExitStats(ThreadVars *tv, void *data) NetmapThreadVars *ntv = (NetmapThreadVars *)data; NetmapDumpCounters(ntv); - SCLogPerf("%s: (%s) packets %" PRIu64 ", dropped %" PRIu64 ", bytes %" PRIu64 "", + SCLogPerf("%s: (%s) packets %" PRIi64 ", dropped %" PRIi64 ", bytes %" PRIu64 "", ntv->ifsrc->ifname, tv->name, - StatsGetLocalCounterValue(tv, ntv->capture_kernel_packets), - StatsGetLocalCounterValue(tv, ntv->capture_kernel_drops), ntv->bytes); + StatsCounterGetLocalValue(&tv->stats, ntv->capture_kernel_packets), + StatsCounterGetLocalValue(&tv->stats, ntv->capture_kernel_drops), ntv->bytes); } /** diff --git a/src/tests/detect.c b/src/tests/detect.c index 45b8eed1d1..2109a686e9 100644 --- a/src/tests/detect.c +++ b/src/tests/detect.c @@ -4060,20 +4060,20 @@ static int SigTestDetectAlertCounter(void) p = UTHBuildPacket((uint8_t *)"boo", strlen("boo"), IPPROTO_TCP); Detect(&tv, p, det_ctx); - FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 1); + FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 1); Detect(&tv, p, det_ctx); - FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 2); + FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 2); UTHFreePackets(&p, 1); p = UTHBuildPacket((uint8_t *)"roo", strlen("roo"), IPPROTO_TCP); Detect(&tv, p, det_ctx); - FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 2); + FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 2); UTHFreePackets(&p, 1); p = UTHBuildPacket((uint8_t *)"laboosa", strlen("laboosa"), IPPROTO_TCP); Detect(&tv, p, det_ctx); - FAIL_IF_NOT(StatsGetLocalCounterValue(&tv, det_ctx->counter_alerts) == 3); + FAIL_IF_NOT(StatsCounterGetLocalValue(&tv.stats, det_ctx->counter_alerts) == 3); UTHFreePackets(&p, 1); DetectEngineThreadCtxDeinit(&tv, (void *)det_ctx);