]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
log: Add hash table for thread/slot mappings
authorJeff Lucovsky <jeff@lucovsky.org>
Fri, 8 Apr 2022 13:39:35 +0000 (09:39 -0400)
committerJeff Lucovsky <jlucovsky@oisf.net>
Mon, 9 Jan 2023 15:08:23 +0000 (10:08 -0500)
Issue: 5198

This commit adds a hash table to manage thread id to slot mappings. This
ensures that each thread will have its own slot (file output device)

src/util-logopenfile.c
src/util-logopenfile.h

index 9dfa662daa1b7c1b152bedc2d6bea4b6e92d33b5..b6d3732cb33d3d79a6e2f576f3c387e5a553bec9 100644 (file)
@@ -1,5 +1,5 @@
 /* vi: set et ts=4: */
-/* Copyright (C) 2007-2021 Open Information Security Foundation
+/* Copyright (C) 2007-2022 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -323,6 +323,33 @@ static void SCLogFileClose(LogFileCtx *log_ctx)
     SCMutexUnlock(&log_ctx->fp_mutex);
 }
 
+static char ThreadSlotHashCompareFunc(
+        void *data1, uint16_t datalen1, void *data2, uint16_t datalen2)
+{
+    ThreadSlotHashEntry *p1 = (ThreadSlotHashEntry *)data1;
+    ThreadSlotHashEntry *p2 = (ThreadSlotHashEntry *)data2;
+
+    if (p1 == NULL || p2 == NULL)
+        return 0;
+
+    return p1->thread_id == p2->thread_id;
+}
+static uint32_t ThreadSlotHashFunc(HashTable *ht, void *data, uint16_t datalen)
+{
+    const ThreadSlotHashEntry *ent = (ThreadSlotHashEntry *)data;
+
+    return ent->thread_id % ht->array_size;
+}
+
+static void ThreadSlotHashFreeFunc(void *data)
+{
+    ThreadSlotHashEntry *thread_ent = (ThreadSlotHashEntry *)data;
+
+    if (thread_ent) {
+        SCFree(thread_ent);
+    }
+}
+
 bool SCLogOpenThreadedFile(
         const char *log_path, const char *append, LogFileCtx *parent_ctx, int slot_count)
 {
@@ -332,6 +359,12 @@ bool SCLogOpenThreadedFile(
             return false;
         }
 
+        parent_ctx->threads->ht = HashTableInit(
+                255, ThreadSlotHashFunc, ThreadSlotHashCompareFunc, ThreadSlotHashFreeFunc);
+        if (!parent_ctx->threads->ht) {
+            FatalError("Unable to initialize thread/slot table");
+        }
+
         parent_ctx->threads->append = SCStrdup(append == NULL ? DEFAULT_LOG_MODE_APPEND : append);
         if (!parent_ctx->threads->append) {
             SCLogError("Unable to allocate threads append setting");
@@ -363,6 +396,9 @@ error_exit:
         if (parent_ctx->threads->append) {
             SCFree(parent_ctx->threads->append);
         }
+        if (parent_ctx->threads->ht) {
+            HashTableFree(parent_ctx->threads->ht);
+        }
         SCFree(parent_ctx->threads);
         parent_ctx->threads = NULL;
         return false;
@@ -845,6 +881,9 @@ int LogFileFreeCtx(LogFileCtx *lf_ctx)
         SCFree(lf_ctx->threads->lf_slots);
         if (lf_ctx->threads->append)
             SCFree(lf_ctx->threads->append);
+        if (lf_ctx->threads->ht) {
+            HashTableFree(lf_ctx->threads->ht);
+        }
         SCFree(lf_ctx->threads);
     } else {
         if (lf_ctx->type != LOGFILE_TYPE_PLUGIN) {
index 7f0d65ff0366264fad5301110351d5916385dfc2..153e3ccdf09f3e84aeaef19cce64bc3223f1b654 100644 (file)
@@ -27,6 +27,7 @@
 #include "threads.h"
 #include "conf.h"            /* ConfNode   */
 #include "util-buffer.h"
+#include "util-hash.h"
 
 #ifdef HAVE_LIBHIREDIS
 #include "util-log-redis.h"
@@ -47,11 +48,18 @@ typedef struct SyslogSetup_ {
     int alert_syslog_level;
 } SyslogSetup;
 
+typedef struct ThreadSlotHashEntry_ {
+    uint64_t thread_id;
+    int slot; /* table slot */
+} ThreadSlotHashEntry;
+
 struct LogFileCtx_;
 typedef struct LogThreadedFileCtx_ {
-    int slot_count;
     SCMutex mutex;
-    struct LogFileCtx_ **lf_slots;
+    int slot_count;                /* Allocated slot count */
+    struct LogFileCtx_ **lf_slots; /* Slots */
+    int last_slot;                 /* Last slot allocated */
+    HashTable *ht;
     char *append;
 } LogThreadedFileCtx;