]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
stats: simplify ips capture stats logic
authorVictor Julien <vjulien@oisf.net>
Mon, 24 Jul 2023 18:33:35 +0000 (20:33 +0200)
committerVictor Julien <vjulien@oisf.net>
Tue, 25 Jul 2023 13:09:45 +0000 (15:09 +0200)
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
src/decode.h
src/source-nfq.c
src/source-windivert.c

index a91ea26066947ce316ba7bdae4569b3f207ff38f..43220543bf1ebcb52f8a7428d3287092125ab7f6 100644 (file)
@@ -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);
index 0627bea7f2166ba25578ea16b07c07ef1f7f0f3d..b50324c98d1b70b3c76ebb2294a64c082ab354d8 100644 (file)
@@ -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));    \
index 2262f10730f989c9d92ca2094d588427780288dc..5c3d7a39f83e874e9566eae5cab25cdb657a19dc 100644 (file)
@@ -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. */
index f0d956973de5bd037ddc1e2e609e21870edd088e..89bf8009eaaea96817cee176d0d3e872dba86c03 100644 (file)
@@ -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;