From 94a3e03903878cdb51a3b88216a63ecae393520c Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Tue, 17 Mar 2026 22:39:56 +0100 Subject: [PATCH] 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 --- src/app-layer-detect-proto.c | 2 +- src/app-layer-ftp.c | 2 +- src/app-layer-smtp.c | 2 +- src/detect-engine-mpm.c | 19 ++++++++++++++++--- src/detect-engine-mpm.h | 2 +- src/detect-engine.c | 2 +- src/util-mpm.c | 4 ++-- src/util-mpm.h | 2 +- 8 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index f2307fbe82..3f8cfb231b 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -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); } } diff --git a/src/app-layer-ftp.c b/src/app-layer-ftp.c index 6b90683eee..87c6b91365 100644 --- a/src/app-layer-ftp.c +++ b/src/app-layer-ftp.c @@ -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; } diff --git a/src/app-layer-smtp.c b/src/app-layer-smtp.c index 7b0f2fdf02..14ae13715d 100644 --- a/src/app-layer-smtp.c +++ b/src/app-layer-smtp.c @@ -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; } diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index e786f0da59..9714539f43 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -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 diff --git a/src/detect-engine-mpm.h b/src/detect-engine-mpm.h index 6bde23a202..4ce41d4fe0 100644 --- a/src/detect-engine-mpm.h +++ b/src/detect-engine-mpm.h @@ -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); diff --git a/src/detect-engine.c b/src/detect-engine.c index 87ad11d9ba..34452208c0 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -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); diff --git a/src/util-mpm.c b/src/util-mpm.c index 7f8663a67c..bfdb469437 100644 --- a/src/util-mpm.c +++ b/src/util-mpm.c @@ -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); } } diff --git a/src/util-mpm.h b/src/util-mpm.h index 859ceae126..89699591d3 100644 --- a/src/util-mpm.h +++ b/src/util-mpm.h @@ -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,