]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: make detect engine types explicit
authorVictor Julien <victor@inliniac.net>
Wed, 27 Jun 2018 11:44:06 +0000 (13:44 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 28 Jun 2018 09:06:56 +0000 (11:06 +0200)
There are 3 types of detect engine objects:
    1. normal
       The normal detection engine if no multi-tenancy is in use

    2. tenant
       A per tenant detection engine

    3. stub
       A stub (or minimal as it was called before) detect engine
       that is needed to have something in place when there are
       only tenants.

       A stub is also used in case of 'delayed detect', where we
       need a minimal detect engine to start up which is replaced
       by a full (normal type) detect engine after startup.

This patch adds a new field 'type' to the DetectEngineCtx object
to distinguish between the types. This replaces the boolean 'minimal'.

src/detect-engine.c
src/detect-engine.h
src/detect.h
src/suricata.c

index 5407a98dc258deff914efb050a5d3f8478920775..1747695e88ab9da730ef2ad7f8e09cb65243cfce 100644 (file)
@@ -1474,7 +1474,7 @@ static int DetectEngineReloadThreads(DetectEngineCtx *new_de_ctx)
     return -1;
 }
 
-static DetectEngineCtx *DetectEngineCtxInitReal(int minimal, const char *prefix)
+static DetectEngineCtx *DetectEngineCtxInitReal(int stub, const char *prefix)
 {
     DetectEngineCtx *de_ctx;
 
@@ -1487,8 +1487,8 @@ static DetectEngineCtx *DetectEngineCtxInitReal(int minimal, const char *prefix)
     TAILQ_INIT(&de_ctx->sig_stat.failed_sigs);
     de_ctx->sigerror = NULL;
 
-    if (minimal) {
-        de_ctx->minimal = 1;
+    if (stub) {
+        de_ctx->type = DETECT_ENGINE_TYPE_STUB;
         de_ctx->version = DetectEngineGetVersion();
         SCLogDebug("minimal with version %u", de_ctx->version);
         return de_ctx;
@@ -1548,7 +1548,7 @@ error:
 
 }
 
-DetectEngineCtx *DetectEngineCtxInitMinimal(void)
+DetectEngineCtx *DetectEngineCtxInitStub(void)
 {
     return DetectEngineCtxInitReal(1, NULL);
 }
@@ -2100,7 +2100,7 @@ static TmEcode DetectEngineThreadCtxInitForMT(ThreadVars *tv, DetectEngineThread
         goto error;
     }
 
-    if (max_tenant_id == 0) {
+    if (tcnt == 0) {
         SCLogInfo("no tenants left, or none registered yet");
     } else {
         max_tenant_id++;
@@ -2310,7 +2310,7 @@ TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
 #endif
     }
 
-    if (det_ctx->de_ctx->minimal == 0) {
+    if (det_ctx->de_ctx->type != DETECT_ENGINE_TYPE_STUB) {
         if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
             DetectEngineThreadCtxDeinit(tv, det_ctx);
             return TM_ECODE_FAILED;
@@ -2363,7 +2363,7 @@ static DetectEngineThreadCtx *DetectEngineThreadCtxInitForReload(
     }
 
     /* most of the init happens here */
-    if (det_ctx->de_ctx->minimal == 0) {
+    if (det_ctx->de_ctx->type != DETECT_ENGINE_TYPE_STUB) {
         if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
             DetectEngineDeReference(&det_ctx->de_ctx);
             SCFree(det_ctx);
@@ -2761,6 +2761,7 @@ static int DetectEngineMultiTenantLoadTenant(uint32_t tenant_id, const char *fil
     }
     SCLogDebug("de_ctx %p with prefix %s", de_ctx, de_ctx->config_prefix);
 
+    de_ctx->type = DETECT_ENGINE_TYPE_TENANT;
     de_ctx->tenant_id = tenant_id;
     de_ctx->loader_id = loader_id;
 
@@ -2812,6 +2813,7 @@ static int DetectEngineMultiTenantReloadTenant(uint32_t tenant_id, const char *f
     }
     SCLogDebug("de_ctx %p with prefix %s", new_de_ctx, new_de_ctx->config_prefix);
 
+    new_de_ctx->type = DETECT_ENGINE_TYPE_TENANT;
     new_de_ctx->tenant_id = tenant_id;
     new_de_ctx->loader_id = old_de_ctx->loader_id;
 
@@ -3248,7 +3250,9 @@ DetectEngineCtx *DetectEngineGetByTenantId(int tenant_id)
 
     DetectEngineCtx *de_ctx = master->list;
     while (de_ctx) {
-        if (de_ctx->minimal == 0 && de_ctx->tenant_id == tenant_id) {
+        if (de_ctx->type == DETECT_ENGINE_TYPE_TENANT &&
+                de_ctx->tenant_id == tenant_id)
+        {
             de_ctx->ref_cnt++;
             break;
         }
@@ -3493,23 +3497,23 @@ int DetectEngineMTApply(void)
         return -1;
     }
 
-    DetectEngineCtx *minimal_de_ctx = NULL;
-    /* if we have no tenants, we need a minimal one */
+    DetectEngineCtx *stub_de_ctx = NULL;
+    /* if we have no tenants, we need a stub */
     if (master->list == NULL) {
-        minimal_de_ctx = master->list = DetectEngineCtxInitMinimal();
-        SCLogDebug("no tenants, using minimal %p", minimal_de_ctx);
-    } else if (master->list->next == NULL && master->list->tenant_id == 0) {
-        minimal_de_ctx = master->list;
-        SCLogDebug("no tenants, using original %p", minimal_de_ctx);
+        stub_de_ctx = master->list = DetectEngineCtxInitStub();
+        SCLogDebug("no tenants, using stub %p", stub_de_ctx);
+    } else if (master->list->next == NULL && master->list->type == DETECT_ENGINE_TYPE_STUB) {
+        stub_de_ctx = master->list;
+        SCLogDebug("no tenants, using original %p", stub_de_ctx);
 
-    /* the default de_ctx should be in the list with tenant_id 0 */
+    /* the default de_ctx should be in the list */
     } else {
         DetectEngineCtx *list = master->list;
         for ( ; list != NULL; list = list->next) {
             SCLogDebug("list %p tenant %u", list, list->tenant_id);
 
-            if (list->tenant_id == 0) {
-                minimal_de_ctx = list;
+            if (list->type == DETECT_ENGINE_TYPE_NORMAL) {
+                stub_de_ctx = list;
                 break;
             }
         }
@@ -3517,7 +3521,7 @@ int DetectEngineMTApply(void)
 
     /* update the threads */
     SCLogDebug("MT reload starting");
-    DetectEngineReloadThreads(minimal_de_ctx);
+    DetectEngineReloadThreads(stub_de_ctx);
     SCLogDebug("MT reload done");
 
     SCMutexUnlock(&master->lock);
index b9ff1115d79aed1a851ac2dfebf0ca504a481184..9e2039b0310c93de55a10ef8f24e85be4fb37062 100644 (file)
@@ -62,7 +62,7 @@ bool DetectBufferRunValidateCallback(const DetectEngineCtx *de_ctx, const int id
 /* prototypes */
 DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix);
 DetectEngineCtx *DetectEngineCtxInit(void);
-DetectEngineCtx *DetectEngineCtxInitMinimal(void);
+DetectEngineCtx *DetectEngineCtxInitStub(void);
 void DetectEngineCtxFree(DetectEngineCtx *);
 
 int DetectRegisterThreadCtxGlobalFuncs(const char *name,
index 60dc08ae93475303feeb3eb7cffac6e79d2deeb4..cd995010fb741f86d302111e98373b8a906ec9e4 100644 (file)
@@ -695,6 +695,13 @@ enum DetectEnginePrefilterSetting
     DETECT_PREFILTER_AUTO = 1,  /**< use mpm + keyword prefilters */
 };
 
+enum DetectEngineType
+{
+    DETECT_ENGINE_TYPE_NORMAL = 0,
+    DETECT_ENGINE_TYPE_STUB = 1, /* previously 'minimal' */
+    DETECT_ENGINE_TYPE_TENANT = 2,
+};
+
 /** \brief main detection engine ctx */
 typedef struct DetectEngineCtx_ {
     uint8_t flags;
@@ -827,8 +834,7 @@ typedef struct DetectEngineCtx_ {
 
     char config_prefix[64];
 
-    /** minimal: essentially a stub */
-    int minimal;
+    enum DetectEngineType type;
 
     /** how many de_ctx' are referencing this */
     uint32_t ref_cnt;
index 8fb17db279a4c7bf5b8604b4071a33fdbe963057..edc2df4f16f85f75549c227b1df741e92ef8f9a1 100644 (file)
@@ -2574,7 +2574,7 @@ static void PostConfLoadedDetectSetup(SCInstance *suri)
         }
         if ((suri->delayed_detect || (mt_enabled && !default_tenant)) &&
             (suri->run_mode != RUNMODE_CONF_TEST)) {
-            de_ctx = DetectEngineCtxInitMinimal();
+            de_ctx = DetectEngineCtxInitStub();
         } else {
             de_ctx = DetectEngineCtxInit();
         }
@@ -2584,7 +2584,7 @@ static void PostConfLoadedDetectSetup(SCInstance *suri)
             exit(EXIT_FAILURE);
         }
 
-        if (!de_ctx->minimal) {
+        if (de_ctx->type == DETECT_ENGINE_TYPE_NORMAL) {
             if (LoadSignatures(de_ctx, suri) != TM_ECODE_OK)
                 exit(EXIT_FAILURE);
             if (suri->run_mode == RUNMODE_ENGINE_ANALYSIS) {