util/mpm: add MpmCtx argument to MpmInitThreadCtx

Ticket: 8001

Will allow algorithms such as ac to use the MpmCtx, especially
the number of patterns, to preallocate some structures.

For detect engine PatternMatchThreadPrepare, create a dummy
MpmCtx out of all the MpmCtx in the DetectEngineCtx,
and get the max value for the number of patterns
pull/15486/head
Philippe Antoine 4 months ago committed by Victor Julien
parent 25bf59f238
commit 94a3e03903

@ -2032,7 +2032,7 @@ AppLayerProtoDetectThreadCtx *AppLayerProtoDetectGetCtxThread(void)
for (j = 0; j < 2; j++) {
mpm_ctx = &alpd_ctx.ctx_ipp[i].ctx_pm[j].mpm_ctx;
mpm_tctx = &alpd_tctx->mpm_tctx[i][j];
MpmInitThreadCtx(mpm_tctx, mpm_ctx->mpm_type);
MpmInitThreadCtx(mpm_tctx, mpm_ctx, mpm_ctx->mpm_type);
}
}

@ -197,7 +197,7 @@ static void *FTPLocalStorageAlloc(void)
if (unlikely(td->ftp_mpm_thread_ctx == NULL)) {
exit(EXIT_FAILURE);
}
MpmInitThreadCtx(td->ftp_mpm_thread_ctx, FTP_MPM);
MpmInitThreadCtx(td->ftp_mpm_thread_ctx, ftp_mpm_ctx, FTP_MPM);
return td;
}

@ -1556,7 +1556,7 @@ static void *SMTPLocalStorageAlloc(void)
if (unlikely(td->smtp_mpm_thread_ctx == NULL)) {
exit(EXIT_FAILURE);
}
MpmInitThreadCtx(td->smtp_mpm_thread_ctx, SMTP_MPM);
MpmInitThreadCtx(td->smtp_mpm_thread_ctx, smtp_mpm_ctx, SMTP_MPM);
return td;
}

@ -965,10 +965,23 @@ void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matche
SCLogDebug("mpm_thread_ctx %p, mpm_matcher %"PRIu16"", mpm_thread_ctx, mpm_matcher);
MpmDestroyThreadCtx(mpm_thread_ctx, mpm_matcher);
}
void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, DetectEngineCtx *de_ctx)
{
SCLogDebug("mpm_thread_ctx %p, type %"PRIu16, mpm_thread_ctx, mpm_matcher);
MpmInitThreadCtx(mpm_thread_ctx, mpm_matcher);
SCLogDebug("mpm_thread_ctx %p, type %" PRIu16, mpm_thread_ctx, de_ctx->mpm_matcher);
MpmCtx cum_mpm_ctx = { 0 };
for (HashListTableBucket *htb = HashListTableGetListHead(de_ctx->mpm_hash_table); htb != NULL;
htb = HashListTableGetListNext(htb)) {
// iterate all de_ctx mpms to merge one MpmCtx with max pattern_cnt and max max_pat_id
const MpmStore *ms = (MpmStore *)HashListTableGetListData(htb);
if (ms == NULL || ms->mpm_ctx == NULL) {
continue;
}
if (ms->mpm_ctx->pattern_cnt > cum_mpm_ctx.pattern_cnt)
cum_mpm_ctx.pattern_cnt = ms->mpm_ctx->pattern_cnt;
if (ms->mpm_ctx->max_pat_id > cum_mpm_ctx.max_pat_id)
cum_mpm_ctx.max_pat_id = ms->mpm_ctx->max_pat_id;
}
MpmInitThreadCtx(mpm_thread_ctx, &cum_mpm_ctx, de_ctx->mpm_matcher);
}
/** \brief Predict a strength value for patterns

@ -41,7 +41,7 @@ uint32_t PatternStrength(uint8_t *, uint16_t);
uint8_t PatternMatchDefaultMatcher(void);
void PatternMatchPrepare(MpmCtx *, uint16_t);
void PatternMatchThreadPrepare(MpmThreadCtx *, uint16_t type);
void PatternMatchThreadPrepare(MpmThreadCtx *, DetectEngineCtx *);
void PatternMatchDestroy(MpmCtx *, uint16_t);
void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t);

@ -3358,7 +3358,7 @@ error:
*/
static TmEcode ThreadCtxDoInit (DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
{
PatternMatchThreadPrepare(&det_ctx->mtc, de_ctx->mpm_matcher);
PatternMatchThreadPrepare(&det_ctx->mtc, de_ctx);
PmqSetup(&det_ctx->pmq);

@ -192,10 +192,10 @@ void MpmFactoryDeRegisterAllMpmCtxProfiles(DetectEngineCtx *de_ctx)
de_ctx->mpm_ctx_factory_container = NULL;
}
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, MpmCtx *mpm_ctx, uint16_t matcher)
{
if (mpm_table[matcher].InitThreadCtx != NULL) {
mpm_table[matcher].InitThreadCtx(NULL, mpm_thread_ctx);
mpm_table[matcher].InitThreadCtx(mpm_ctx, mpm_thread_ctx);
}
}

@ -208,7 +208,7 @@ void MpmTableSetup(void);
void MpmRegisterTests(void);
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher);
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t);
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, MpmCtx *mpm_ctx, uint16_t);
void MpmDestroyThreadCtx(MpmThreadCtx *mpm_thread_ctx, const uint16_t matcher);
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen,

Loading…
Cancel
Save