From ece1cb3e94cf346c9b039e7956158ec56970fef0 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Mon, 24 Jul 2023 20:33:35 +0200 Subject: [PATCH] stats: simplify ips capture stats logic Since many implementations use the ReleasePacket callback to issue their verdict, no thread ctx is available. To work around this just register the stats in a `thread_local` variable instead. --- src/decode.c | 15 +++++++++++++-- src/decode.h | 13 ++----------- src/source-nfq.c | 7 ++----- src/source-windivert.c | 5 ++--- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/decode.c b/src/decode.c index a91ea26066..43220543bf 100644 --- a/src/decode.c +++ b/src/decode.c @@ -822,9 +822,19 @@ const char *PacketDropReasonToString(enum PacketDropReason r) return NULL; } +typedef struct CaptureStats_ { + uint16_t counter_ips_accepted; + uint16_t counter_ips_blocked; + uint16_t counter_ips_rejected; + uint16_t counter_ips_replaced; +} CaptureStats; + +thread_local CaptureStats t_capture_stats; + /* TODO drop reason stats! */ -void CaptureStatsUpdate(ThreadVars *tv, CaptureStats *s, const Packet *p) +void CaptureStatsUpdate(ThreadVars *tv, const Packet *p) { + CaptureStats *s = &t_capture_stats; if (unlikely(PacketCheckAction(p, ACTION_REJECT_ANY))) { StatsIncr(tv, s->counter_ips_rejected); } else if (unlikely(PacketCheckAction(p, ACTION_DROP))) { @@ -836,8 +846,9 @@ void CaptureStatsUpdate(ThreadVars *tv, CaptureStats *s, const Packet *p) } } -void CaptureStatsSetup(ThreadVars *tv, CaptureStats *s) +void CaptureStatsSetup(ThreadVars *tv) { + CaptureStats *s = &t_capture_stats; s->counter_ips_accepted = StatsRegisterCounter("ips.accepted", tv); s->counter_ips_blocked = StatsRegisterCounter("ips.blocked", tv); s->counter_ips_rejected = StatsRegisterCounter("ips.rejected", tv); diff --git a/src/decode.h b/src/decode.h index 0627bea7f2..b50324c98d 100644 --- a/src/decode.h +++ b/src/decode.h @@ -749,17 +749,8 @@ typedef struct DecodeThreadVars_ } DecodeThreadVars; -typedef struct CaptureStats_ { - - uint16_t counter_ips_accepted; - uint16_t counter_ips_blocked; - uint16_t counter_ips_rejected; - uint16_t counter_ips_replaced; - -} CaptureStats; - -void CaptureStatsUpdate(ThreadVars *tv, CaptureStats *s, const Packet *p); -void CaptureStatsSetup(ThreadVars *tv, CaptureStats *s); +void CaptureStatsUpdate(ThreadVars *tv, const Packet *p); +void CaptureStatsSetup(ThreadVars *tv); #define PACKET_CLEAR_L4VARS(p) do { \ memset(&(p)->l4vars, 0x00, sizeof((p)->l4vars)); \ diff --git a/src/source-nfq.c b/src/source-nfq.c index 2262f10730..5c3d7a39f8 100644 --- a/src/source-nfq.c +++ b/src/source-nfq.c @@ -120,8 +120,6 @@ typedef struct NFQThreadVars_ char *data; /** Per function and thread data */ int datalen; /** Length of per function and thread data */ - - CaptureStats stats; } NFQThreadVars; /* shared vars for all for nfq queues and threads */ static NFQGlobalVars nfq_g; @@ -779,7 +777,7 @@ TmEcode VerdictNFQThreadInit(ThreadVars *tv, const void *initdata, void **data) { NFQThreadVars *ntv = (NFQThreadVars *) initdata; - CaptureStatsSetup(tv, &ntv->stats); + CaptureStatsSetup(tv); *data = (void *)ntv; return TM_ECODE_OK; @@ -1191,9 +1189,8 @@ TmEcode NFQSetVerdict(Packet *p) */ TmEcode VerdictNFQ(ThreadVars *tv, Packet *p, void *data) { - NFQThreadVars *ntv = (NFQThreadVars *)data; /* update counters */ - CaptureStatsUpdate(tv, &ntv->stats, p); + CaptureStatsUpdate(tv, p); /* if this is a tunnel packet we check if we are ready to verdict * already. */ diff --git a/src/source-windivert.c b/src/source-windivert.c index f0d956973d..89bf8009ea 100644 --- a/src/source-windivert.c +++ b/src/source-windivert.c @@ -95,7 +95,6 @@ typedef struct WinDivertThreadVars_ { WinDivertHandle filter_handle; int thread_num; - CaptureStats stats; int64_t qpc_start_time; int64_t qpc_start_count; int64_t qpc_freq_usec; @@ -750,7 +749,7 @@ static TmEcode WinDivertVerdictHelper(ThreadVars *tv, Packet *p) WinDivertThreadVars *wd_tv = WinDivertGetThread(p->windivert_v.thread_num); /* update counters */ - CaptureStatsUpdate(tv, &wd_tv->stats, p); + CaptureStatsUpdate(tv, p); #ifdef COUNTERS WinDivertQueueVars *wd_qv = WinDivertGetQueue(wd_tv->thread_num); @@ -823,7 +822,7 @@ TmEcode VerdictWinDivertThreadInit(ThreadVars *tv, const void *initdata, WinDivertThreadVars *wd_tv = (WinDivertThreadVars *)initdata; - CaptureStatsSetup(tv, &wd_tv->stats); + CaptureStatsSetup(tv); *data = wd_tv; -- 2.47.2