detect: make detect engine types explicit

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'.
pull/3409/head
Victor Julien 8 years ago
parent b5bc509857
commit 6e9d81289d

@ -1474,7 +1474,7 @@ static int DetectEngineReloadThreads(DetectEngineCtx *new_de_ctx)
return -1; return -1;
} }
static DetectEngineCtx *DetectEngineCtxInitReal(int minimal, const char *prefix) static DetectEngineCtx *DetectEngineCtxInitReal(int stub, const char *prefix)
{ {
DetectEngineCtx *de_ctx; DetectEngineCtx *de_ctx;
@ -1487,8 +1487,8 @@ static DetectEngineCtx *DetectEngineCtxInitReal(int minimal, const char *prefix)
TAILQ_INIT(&de_ctx->sig_stat.failed_sigs); TAILQ_INIT(&de_ctx->sig_stat.failed_sigs);
de_ctx->sigerror = NULL; de_ctx->sigerror = NULL;
if (minimal) { if (stub) {
de_ctx->minimal = 1; de_ctx->type = DETECT_ENGINE_TYPE_STUB;
de_ctx->version = DetectEngineGetVersion(); de_ctx->version = DetectEngineGetVersion();
SCLogDebug("minimal with version %u", de_ctx->version); SCLogDebug("minimal with version %u", de_ctx->version);
return de_ctx; return de_ctx;
@ -1548,7 +1548,7 @@ error:
} }
DetectEngineCtx *DetectEngineCtxInitMinimal(void) DetectEngineCtx *DetectEngineCtxInitStub(void)
{ {
return DetectEngineCtxInitReal(1, NULL); return DetectEngineCtxInitReal(1, NULL);
} }
@ -2100,7 +2100,7 @@ static TmEcode DetectEngineThreadCtxInitForMT(ThreadVars *tv, DetectEngineThread
goto error; goto error;
} }
if (max_tenant_id == 0) { if (tcnt == 0) {
SCLogInfo("no tenants left, or none registered yet"); SCLogInfo("no tenants left, or none registered yet");
} else { } else {
max_tenant_id++; max_tenant_id++;
@ -2310,7 +2310,7 @@ TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
#endif #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) { if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
DetectEngineThreadCtxDeinit(tv, det_ctx); DetectEngineThreadCtxDeinit(tv, det_ctx);
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
@ -2363,7 +2363,7 @@ static DetectEngineThreadCtx *DetectEngineThreadCtxInitForReload(
} }
/* most of the init happens here */ /* 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) { if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
DetectEngineDeReference(&det_ctx->de_ctx); DetectEngineDeReference(&det_ctx->de_ctx);
SCFree(det_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); 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->tenant_id = tenant_id;
de_ctx->loader_id = loader_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); 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->tenant_id = tenant_id;
new_de_ctx->loader_id = old_de_ctx->loader_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; DetectEngineCtx *de_ctx = master->list;
while (de_ctx) { 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++; de_ctx->ref_cnt++;
break; break;
} }
@ -3493,23 +3497,23 @@ int DetectEngineMTApply(void)
return -1; return -1;
} }
DetectEngineCtx *minimal_de_ctx = NULL; DetectEngineCtx *stub_de_ctx = NULL;
/* if we have no tenants, we need a minimal one */ /* if we have no tenants, we need a stub */
if (master->list == NULL) { if (master->list == NULL) {
minimal_de_ctx = master->list = DetectEngineCtxInitMinimal(); stub_de_ctx = master->list = DetectEngineCtxInitStub();
SCLogDebug("no tenants, using minimal %p", minimal_de_ctx); SCLogDebug("no tenants, using stub %p", stub_de_ctx);
} else if (master->list->next == NULL && master->list->tenant_id == 0) { } else if (master->list->next == NULL && master->list->type == DETECT_ENGINE_TYPE_STUB) {
minimal_de_ctx = master->list; stub_de_ctx = master->list;
SCLogDebug("no tenants, using original %p", minimal_de_ctx); 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 { } else {
DetectEngineCtx *list = master->list; DetectEngineCtx *list = master->list;
for ( ; list != NULL; list = list->next) { for ( ; list != NULL; list = list->next) {
SCLogDebug("list %p tenant %u", list, list->tenant_id); SCLogDebug("list %p tenant %u", list, list->tenant_id);
if (list->tenant_id == 0) { if (list->type == DETECT_ENGINE_TYPE_NORMAL) {
minimal_de_ctx = list; stub_de_ctx = list;
break; break;
} }
} }
@ -3517,7 +3521,7 @@ int DetectEngineMTApply(void)
/* update the threads */ /* update the threads */
SCLogDebug("MT reload starting"); SCLogDebug("MT reload starting");
DetectEngineReloadThreads(minimal_de_ctx); DetectEngineReloadThreads(stub_de_ctx);
SCLogDebug("MT reload done"); SCLogDebug("MT reload done");
SCMutexUnlock(&master->lock); SCMutexUnlock(&master->lock);

@ -62,7 +62,7 @@ bool DetectBufferRunValidateCallback(const DetectEngineCtx *de_ctx, const int id
/* prototypes */ /* prototypes */
DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix); DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix);
DetectEngineCtx *DetectEngineCtxInit(void); DetectEngineCtx *DetectEngineCtxInit(void);
DetectEngineCtx *DetectEngineCtxInitMinimal(void); DetectEngineCtx *DetectEngineCtxInitStub(void);
void DetectEngineCtxFree(DetectEngineCtx *); void DetectEngineCtxFree(DetectEngineCtx *);
int DetectRegisterThreadCtxGlobalFuncs(const char *name, int DetectRegisterThreadCtxGlobalFuncs(const char *name,

@ -695,6 +695,13 @@ enum DetectEnginePrefilterSetting
DETECT_PREFILTER_AUTO = 1, /**< use mpm + keyword prefilters */ 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 */ /** \brief main detection engine ctx */
typedef struct DetectEngineCtx_ { typedef struct DetectEngineCtx_ {
uint8_t flags; uint8_t flags;
@ -827,8 +834,7 @@ typedef struct DetectEngineCtx_ {
char config_prefix[64]; char config_prefix[64];
/** minimal: essentially a stub */ enum DetectEngineType type;
int minimal;
/** how many de_ctx' are referencing this */ /** how many de_ctx' are referencing this */
uint32_t ref_cnt; uint32_t ref_cnt;

@ -2574,7 +2574,7 @@ static void PostConfLoadedDetectSetup(SCInstance *suri)
} }
if ((suri->delayed_detect || (mt_enabled && !default_tenant)) && if ((suri->delayed_detect || (mt_enabled && !default_tenant)) &&
(suri->run_mode != RUNMODE_CONF_TEST)) { (suri->run_mode != RUNMODE_CONF_TEST)) {
de_ctx = DetectEngineCtxInitMinimal(); de_ctx = DetectEngineCtxInitStub();
} else { } else {
de_ctx = DetectEngineCtxInit(); de_ctx = DetectEngineCtxInit();
} }
@ -2584,7 +2584,7 @@ static void PostConfLoadedDetectSetup(SCInstance *suri)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!de_ctx->minimal) { if (de_ctx->type == DETECT_ENGINE_TYPE_NORMAL) {
if (LoadSignatures(de_ctx, suri) != TM_ECODE_OK) if (LoadSignatures(de_ctx, suri) != TM_ECODE_OK)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (suri->run_mode == RUNMODE_ENGINE_ANALYSIS) { if (suri->run_mode == RUNMODE_ENGINE_ANALYSIS) {

Loading…
Cancel
Save