From 41e6735b923df793e4660142b7d1a87384b7f98d Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Mon, 8 Feb 2010 20:33:13 +0530 Subject: [PATCH] mpm b2g cuda support added --- src/Makefile.am | 2 + src/alert-fastlog.c | 30 + src/app-layer-detect-proto.c | 273 ++- src/decode.h | 37 + src/detect-engine-mpm.c | 117 +- src/detect.c | 26 + src/detect.h | 7 + src/suricata-common.h | 2 + src/suricata.c | 26 +- src/tm-modules.h | 3 + src/tm-threads.c | 85 + src/tm-threads.h | 23 +- src/util-cuda-handlers.c | 631 ++++++ src/util-cuda-handlers.h | 66 + src/util-cuda.c | 18 +- src/util-error.c | 5 + src/util-error.h | 4 + src/util-mpm-b2g-cuda.c | 4072 ++++++++++++++++++++++++++++++++++ src/util-mpm-b2g-cuda.h | 157 ++ src/util-mpm-b2g.c | 86 +- src/util-mpm-b3g.c | 69 +- src/util-mpm-wumanber.c | 133 +- src/util-mpm.c | 8 +- src/util-mpm.h | 6 +- 24 files changed, 5673 insertions(+), 213 deletions(-) create mode 100644 src/util-cuda-handlers.c create mode 100644 src/util-cuda-handlers.h create mode 100644 src/util-mpm-b2g-cuda.c create mode 100644 src/util-mpm-b2g-cuda.h diff --git a/src/Makefile.am b/src/Makefile.am index dd272fcca7..d46d1a89ff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -117,6 +117,7 @@ util-spm-bs2bm.c util-spm-bs2bm.h \ util-spm-bm.c util-spm-bm.h \ util-mpm-wumanber.c util-mpm-wumanber.h \ util-mpm-b2g.c util-mpm-b2g.h \ +util-mpm-b2g-cuda.c util-mpm-b2g-cuda.h \ util-mpm-b3g.c util-mpm-b3g.h \ util-cidr.c util-cidr.h \ util-unittest.c util-unittest.h \ @@ -144,6 +145,7 @@ util-classification-config.c util-classification-config.h \ util-strlcatu.c \ util-strlcpyu.c \ util-cuda.c util-cuda.h \ +util-cuda-handlers.c util-cuda-handlers.h \ tm-modules.c tm-modules.h \ tm-queues.c tm-queues.h \ tm-queuehandlers.c tm-queuehandlers.h \ diff --git a/src/alert-fastlog.c b/src/alert-fastlog.c index 68a9f754e5..91df947729 100644 --- a/src/alert-fastlog.c +++ b/src/alert-fastlog.c @@ -19,6 +19,7 @@ #include "conf.h" #include "threads.h" +#include "tm-threads.h" #include "threadvars.h" #include "tm-modules.h" #include "util-debug.h" @@ -33,6 +34,9 @@ #include "output.h" #include "alert-fastlog.h" +#include "util-mpm-b2g-cuda.h" +#include "util-cuda-handlers.h" + #define DEFAULT_LOG_FILENAME "fast.log" #define MODULE_NAME "AlertFastLog" @@ -306,6 +310,14 @@ int AlertFastLogTest01() else result = 0; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadRC(); + if (SCCudaHlPushCudaContextFromModule("SC_RULES_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); @@ -365,6 +377,14 @@ int AlertFastLogTest02() result = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadRC(); + if (SCCudaHlPushCudaContextFromModule("SC_RULES_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + SigGroupCleanup(de_ctx); SigCleanSignatures(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); @@ -384,9 +404,19 @@ void AlertFastLogRegisterTests(void) #ifdef UNITTESTS +#ifdef __SC_CUDA_SUPPORT__ + UtRegisterTest("AlertFastLogCudaContextInit", + SCCudaHlTestEnvCudaContextInit, 1); +#endif + UtRegisterTest("AlertFastLogTest01", AlertFastLogTest01, 1); UtRegisterTest("AlertFastLogTest02", AlertFastLogTest02, 1); +#ifdef __SC_CUDA_SUPPORT__ + UtRegisterTest("AlertFastLogCudaContextDeInit", + SCCudaHlTestEnvCudaContextDeInit, 1); +#endif + #endif /* UNITTESTS */ } diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index 112649b90b..af629f811f 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -35,7 +35,11 @@ #include "app-layer-protos.h" #include "app-layer-parser.h" +#include "app-layer-detect-proto.h" +#include "util-cuda.h" +#include "util-cuda-handlers.h" +#include "util-mpm-b2g-cuda.h" #include "util-debug.h" #define INSPECT_BYTES 32 @@ -53,6 +57,8 @@ typedef struct AlpProtoDetectDirection_ { typedef struct AlpProtoDetectCtx_ { AlpProtoDetectDirection toserver; AlpProtoDetectDirection toclient; + + int alp_content_module_handle; } AlpProtoDetectCtx; static AlpProtoDetectCtx alp_proto_ctx; @@ -62,8 +68,14 @@ static AlpProtoDetectCtx alp_proto_ctx; void AlpProtoInit(AlpProtoDetectCtx *ctx) { memset(ctx, 0x00, sizeof(AlpProtoDetectCtx)); - MpmInitCtx(&ctx->toserver.mpm_ctx, PatternMatchDefaultMatcher()); - MpmInitCtx(&ctx->toclient.mpm_ctx, PatternMatchDefaultMatcher()); +#ifndef __SC_CUDA_SUPPORT__ + MpmInitCtx(&ctx->toserver.mpm_ctx, PatternMatchDefaultMatcher()), -1); + MpmInitCtx(&ctx->toclient.mpm_ctx, PatternMatchDefaultMatcher()), -1); +#else + ctx->alp_content_module_handle = SCCudaHlRegisterModule("SC_ALP_CONTENT_B2G_CUDA"); + MpmInitCtx(&ctx->toserver.mpm_ctx, MPM_B2G_CUDA, ctx->alp_content_module_handle); + MpmInitCtx(&ctx->toclient.mpm_ctx, MPM_B2G_CUDA, ctx->alp_content_module_handle); +#endif memset(&ctx->toserver.map, 0x00, sizeof(ctx->toserver.map)); memset(&ctx->toclient.map, 0x00, sizeof(ctx->toclient.map)); @@ -165,6 +177,14 @@ void AlpProtoFinalizeGlobal(AlpProtoDetectCtx *ctx) { mpm_table[ctx->toclient.mpm_ctx.mpm_type].Prepare(&ctx->toclient.mpm_ctx); mpm_table[ctx->toserver.mpm_ctx.mpm_type].Prepare(&ctx->toserver.mpm_ctx); +#ifdef __SC_CUDA_SUPPORT__ + CUcontext context; + if (SCCudaCtxPopCurrent(&context) == -1) + exit(EXIT_FAILURE); + if (B2gCudaStartDispatcherThreadAPC("SC_ALP_CONTENT_B2G_CUDA") == -1) + exit(EXIT_FAILURE); +#endif + /* tell the stream reassembler we only want chunks of size max_depth */ StreamMsgQueueSetMinInitChunkLen(FLOW_PKT_TOCLIENT, ctx->toclient.max_depth); StreamMsgQueueSetMinInitChunkLen(FLOW_PKT_TOSERVER, ctx->toserver.max_depth); @@ -275,7 +295,31 @@ uint16_t AppLayerDetectGetProto(AlpProtoDetectCtx *ctx, AlpProtoDetectThreadCtx scanlen = dir->max_depth; uint16_t proto; - uint32_t cnt = mpm_table[dir->mpm_ctx.mpm_type].Scan(&dir->mpm_ctx, &tdir->mpm_ctx, &tdir->pmq, buf, scanlen); + uint32_t cnt; +#ifndef __SC_CUDA_SUPPORT__ + cnt = mpm_table[dir->mpm_ctx.mpm_type].Scan(&dir->mpm_ctx, + &tdir->mpm_ctx, + &tdir->pmq, buf, + scanlen); +#else + Packet *p = malloc(sizeof(Packet)); + if (p == NULL) goto end; + memset(p, 0, sizeof(Packet)); + p->cuda_done = 0; + p->cuda_free_packet = 1; + p->cuda_search = 0; + p->cuda_mpm_ctx = &dir->mpm_ctx; + p->cuda_mtc = &tdir->mpm_ctx; + p->cuda_pmq = &tdir->pmq; + p->payload = buf; + p->payload_len = scanlen; + B2gCudaPushPacketTo_tv_CMB2_APC(p); + SCMutexLock(&p->cuda_mutex_q); + SCondWait(&p->cuda_cond_q, &p->cuda_mutex_q); + p->cuda_done = 1; + SCMutexUnlock(&p->cuda_mutex_q); + cnt = p->cuda_matches; +#endif SCLogDebug("scan cnt %" PRIu32 "", cnt); if (cnt == 0) { proto = ALPROTO_UNKNOWN; @@ -481,6 +525,14 @@ int AlpDetectTest01(void) { int r = 1; AlpProtoDetectCtx ctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -498,6 +550,14 @@ int AlpDetectTest01(void) { free(buf); AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -506,6 +566,14 @@ int AlpDetectTest02(void) { int r = 1; AlpProtoDetectCtx ctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -532,6 +600,14 @@ int AlpDetectTest02(void) { } AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -542,6 +618,14 @@ int AlpDetectTest03(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -570,6 +654,14 @@ int AlpDetectTest03(void) { AlpProtoFinalizeGlobal(&ctx); AlpProtoFinalizeThread(&ctx, &tctx); +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + uint32_t cnt = mpm_table[ctx.toclient.mpm_ctx.mpm_type].Scan(&ctx.toclient.mpm_ctx, &tctx.toclient.mpm_ctx, NULL, l7data, sizeof(l7data)); if (cnt != 1) { printf("cnt %u != 1: ", cnt); @@ -577,6 +669,14 @@ int AlpDetectTest03(void) { } AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -587,6 +687,14 @@ int AlpDetectTest04(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -603,6 +711,14 @@ int AlpDetectTest04(void) { AlpProtoFinalizeGlobal(&ctx); AlpProtoFinalizeThread(&ctx, &tctx); +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + uint32_t cnt = mpm_table[ctx.toclient.mpm_ctx.mpm_type].Scan(&ctx.toclient.mpm_ctx, &tctx.toclient.mpm_ctx, NULL, l7data, sizeof(l7data)); if (cnt != 0) { printf("cnt %u != 0: ", cnt); @@ -610,6 +726,14 @@ int AlpDetectTest04(void) { } AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -621,6 +745,14 @@ int AlpDetectTest05(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -655,7 +787,23 @@ int AlpDetectTest05(void) { r = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -666,6 +814,14 @@ int AlpDetectTest06(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -700,7 +856,23 @@ int AlpDetectTest06(void) { r = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -711,6 +883,14 @@ int AlpDetectTest07(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_HTTP, buf, 4, 0, STREAM_TOCLIENT); @@ -733,7 +913,23 @@ int AlpDetectTest07(void) { r = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -755,6 +951,14 @@ int AlpDetectTest08(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_SMB, buf, 8, 4, STREAM_TOCLIENT); @@ -777,7 +981,23 @@ int AlpDetectTest08(void) { r = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } @@ -796,6 +1016,14 @@ int AlpDetectTest09(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_SMB2, buf, 8, 4, STREAM_TOCLIENT); @@ -818,7 +1046,22 @@ int AlpDetectTest09(void) { r = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif return r; } @@ -833,6 +1076,14 @@ int AlpDetectTest10(void) { AlpProtoDetectCtx ctx; AlpProtoDetectThreadCtx tctx; +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoInit(&ctx); AlpProtoAdd(&ctx, IPPROTO_TCP, ALPROTO_DCERPC, buf, 4, 0, STREAM_TOCLIENT); @@ -855,7 +1106,23 @@ int AlpDetectTest10(void) { r = 0; } +#ifdef __SC_CUDA_SUPPORT__ + B2gCudaKillDispatcherThreadAPC(); + if (SCCudaHlPushCudaContextFromModule("SC_ALP_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + return 0; + } +#endif + AlpProtoTestDestroy(&ctx); + +#ifdef __SC_CUDA_SUPPORT__ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } +#endif + return r; } diff --git a/src/decode.h b/src/decode.h index 7bbce4d70f..d2a9dcc640 100644 --- a/src/decode.h +++ b/src/decode.h @@ -34,6 +34,9 @@ #include "decode-raw.h" #include "decode-vlan.h" +/* forward declaration */ +struct DetectionEngineThreadCtx_; + /* Address */ typedef struct Address_ { @@ -317,6 +320,40 @@ typedef struct Packet_ * It should always point to the lowest * packet in a encapsulated packet */ + /* required for cuda support */ +#ifdef __SC_CUDA_SUPPORT__ + PatternMatcherQueue *cuda_pmq; + MpmCtx *cuda_mpm_ctx; + MpmThreadCtx *cuda_mtc; + /* this mutex corresponds to the condition variable defined below it */ + SCMutex cuda_mutex_q; + /* we need this condition variable so that the cuda dispatcher thread + * can inform the client threads, when they are done with the pattern + * matching */ + SCCondT cuda_cond_q; + /* used to hold the match results. We can instead use a void *result + * instead here. That way we can make them hold any result. *todo* */ + uint16_t cuda_matches; + /* the client thread uses this flag to inform the dispatcher that it + * has woken up and has retrieved the results and that the dispatcher + * can now continue its operations */ + uint8_t cuda_done; + /* indicates if the dispatcher should call the search or the scan phase + * of the pattern matcher. We can instead use a void *cuda_data instead. + * This way we can send any data across to the dispatcher */ + uint8_t cuda_search; + /* indicates if the dispatcher should free this packet it has received. + * For some modules which don't send a packet to the dispatcher, we will + * have to create dummy Packets, fill them with data and send them over + * to the dispatcher. The callling thread can't free the Packet once + * it gets the results, since the dispatcher might still be accessing + * the final stages of the packets. Instead we make the dispatcher + * free the packet, if we want the packet to be destoryed after it has + * been used. A probable *todo* would be using a static Packet, in the + * the modules that requires a dummy Packet. That way we are not forced + * to create a new Packet every time using the expensive malloc() */ + uint8_t cuda_free_packet; +#endif } Packet; typedef struct PacketQueue_ { diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index b5b793fdae..263a014533 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -17,13 +17,19 @@ #include "detect-content.h" #include "detect-uricontent.h" +#include "util-mpm-b2g-cuda.h" #include "util-debug.h" /** \todo make it possible to use multiple pattern matcher algorithms next to eachother. */ //#define PM MPM_WUMANBER +//#define PM MPM_B2G +#ifdef __SC_CUDA_SUPPORT__ +#define PM MPM_B2G_CUDA +#else #define PM MPM_B2G +#endif //#define PM MPM_B3G /** \brief Function to return the default multi pattern matcher algorithm to be @@ -58,14 +64,31 @@ uint32_t PacketPatternScan(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p) { SCEnter(); - uint32_t ret; det_ctx->pmq.mode = PMQ_MODE_SCAN; - ret = mpm_table[det_ctx->sgh->mpm_ctx->mpm_type].Scan - (det_ctx->sgh->mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, p->payload, - p->payload_len); - +#ifndef __SC_CUDA_SUPPORT__ + uint32_t ret; + ret = mpm_table[det_ctx->sgh->mpm_ctx->mpm_type].Scan(det_ctx->sgh->mpm_ctx, + &det_ctx->mtc, + &det_ctx->pmq, + p->payload, + p->payload_len); SCReturnInt(ret); +#else + p->cuda_done = 0; + p->cuda_search = 0; + p->cuda_free_packet = 0; + p->cuda_mpm_ctx = det_ctx->sgh->mpm_ctx; + p->cuda_mtc = &det_ctx->mtc; + p->cuda_pmq = &det_ctx->pmq; + B2gCudaPushPacketTo_tv_CMB2_RC(p); + SCMutexLock(&p->cuda_mutex_q); + SCondWait(&p->cuda_cond_q, &p->cuda_mutex_q); + p->cuda_done = 1; + SCMutexUnlock(&p->cuda_mutex_q); + SCReturnInt(p->cuda_matches); +#endif + } /** \brief Uri Pattern match, scan part -- searches for only 'scan' patterns, @@ -78,14 +101,32 @@ uint32_t UriPatternScan(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, uint8_t *uri, uint16_t uri_len) { SCEnter(); - uint32_t ret; det_ctx->pmq.mode = PMQ_MODE_SCAN; +#ifndef __SC_CUDA_SUPPORT__ + uint32_t ret; ret = mpm_table[det_ctx->sgh->mpm_uri_ctx->mpm_type].Scan (det_ctx->sgh->mpm_uri_ctx, &det_ctx->mtcu, &det_ctx->pmq, uri, uri_len); - SCReturnInt(ret); +#else + Packet *p = malloc(sizeof(Packet)); + memset(p, 0, sizeof(Packet)); + p->cuda_done = 0; + p->cuda_free_packet = 1; + p->cuda_search = 0; + p->cuda_mpm_ctx = det_ctx->sgh->mpm_uri_ctx; + p->cuda_mtc = &det_ctx->mtcu; + p->cuda_pmq = &det_ctx->pmq; + p->payload = uri; + p->payload_len = uri_len; + B2gCudaPushPacketTo_tv_CMB2_RC(p); + SCMutexLock(&p->cuda_mutex_q); + SCondWait(&p->cuda_cond_q, &p->cuda_mutex_q); + p->cuda_done = 1; + SCMutexUnlock(&p->cuda_mutex_q); + SCReturnInt(p->cuda_matches); +#endif } /** \brief Pattern match, search part -- searches for all other patterns @@ -97,14 +138,30 @@ uint32_t PacketPatternMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p) { SCEnter(); - uint32_t ret; det_ctx->pmq.mode = PMQ_MODE_SEARCH; - ret = mpm_table[det_ctx->sgh->mpm_ctx->mpm_type].Search - (det_ctx->sgh->mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, p->payload, - p->payload_len); - +#ifndef __SC_CUDA_SUPPORT__ + uint32_t ret; + ret = mpm_table[det_ctx->sgh->mpm_ctx->mpm_type].Search(det_ctx->sgh->mpm_ctx, + &det_ctx->mtc, + &det_ctx->pmq, + p->payload, + p->payload_len); SCReturnInt(ret); +#else + p->cuda_done = 0; + p->cuda_search = 1; + p->cuda_free_packet = 0; + p->cuda_mpm_ctx = det_ctx->sgh->mpm_ctx; + p->cuda_mtc = &det_ctx->mtc; + p->cuda_pmq = &det_ctx->pmq; + B2gCudaPushPacketTo_tv_CMB2_RC(p); + SCMutexLock(&p->cuda_mutex_q); + SCondWait(&p->cuda_cond_q, &p->cuda_mutex_q); + p->cuda_done = 1; + SCMutexUnlock(&p->cuda_mutex_q); + SCReturnInt(p->cuda_matches); +#endif } /** \brief Uri Pattern match, search part -- searches for all other patterns @@ -116,14 +173,32 @@ uint32_t UriPatternMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, uint8_t *uri, uint16_t uri_len) { SCEnter(); - uint32_t ret; det_ctx->pmq.mode = PMQ_MODE_SEARCH; +#ifndef __SC_CUDA_SUPPORT__ + uint32_t ret; ret = mpm_table[det_ctx->sgh->mpm_uri_ctx->mpm_type].Search (det_ctx->sgh->mpm_uri_ctx, &det_ctx->mtcu, &det_ctx->pmq, uri, uri_len); - SCReturnInt(ret); +#else + Packet *p = malloc(sizeof(Packet)); + memset(p, 0, sizeof(Packet)); + p->cuda_done = 0; + p->cuda_free_packet = 1; + p->cuda_search = 1; + p->cuda_mpm_ctx = det_ctx->sgh->mpm_uri_ctx; + p->cuda_mtc = &det_ctx->mtcu; + p->cuda_pmq = &det_ctx->pmq; + B2gCudaPushPacketTo_tv_CMB2_RC(p); + SCMutexLock(&p->cuda_mutex_q); + SCondWait(&p->cuda_cond_q, &p->cuda_mutex_q); + p->cuda_done = 1; + SCMutexUnlock(&p->cuda_mutex_q); + SCReturnInt(p->cuda_matches); +#endif + + //printf("PacketPatternMatch: ret %" PRIu32 "\n", ret); } /** \brief cleans up the mpm instance after a match */ @@ -150,7 +225,7 @@ void PatternMatchDestroy(MpmCtx *mpm_ctx, uint16_t mpm_matcher) { void PatternMatchPrepare(MpmCtx *mpm_ctx, uint16_t mpm_matcher) { SCLogDebug("mpm_ctx %p, mpm_matcher %"PRIu16"", mpm_ctx, mpm_matcher); - MpmInitCtx(mpm_ctx, mpm_matcher); + MpmInitCtx(mpm_ctx, mpm_matcher, -1); } void PatternMatchThreadPrint(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher) { @@ -621,7 +696,11 @@ int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh) goto error; memset(sh->mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(sh->mpm_ctx, de_ctx->mpm_matcher); +#ifndef __SC_CUDA_SUPPORT__ + MpmInitCtx(sh->mpm_ctx, de_ctx->mpm_matcher, -1); +#else + MpmInitCtx(sh->mpm_ctx, de_ctx->mpm_matcher, de_ctx->cuda_rc_mod_handle); +#endif } if (sh->flags & SIG_GROUP_HAVEURICONTENT && !(sh->flags & SIG_GROUP_HEAD_MPM_URI_COPY)) { sh->mpm_uri_ctx = malloc(sizeof(MpmCtx)); @@ -629,7 +708,11 @@ int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh) goto error; memset(sh->mpm_uri_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(sh->mpm_uri_ctx, de_ctx->mpm_matcher); +#ifndef __SC_CUDA_SUPPORT__ + MpmInitCtx(sh->mpm_uri_ctx, de_ctx->mpm_matcher, -1); +#else + MpmInitCtx(sh->mpm_uri_ctx, de_ctx->mpm_matcher, de_ctx->cuda_rc_mod_handle); +#endif } uint32_t mpm_content_cnt = 0, mpm_uricontent_cnt = 0; diff --git a/src/detect.c b/src/detect.c index c40e246a7a..d32e1056f2 100644 --- a/src/detect.c +++ b/src/detect.c @@ -102,6 +102,10 @@ #include "util-debug.h" #include "util-hashlist.h" +#include "util-cuda-handlers.h" +#include "util-mpm-b2g-cuda.h" +#include "util-cuda.h" + SigMatch *SigMatchAlloc(void); void SigMatchFree(SigMatch *sm); void DetectExitPrintStats(ThreadVars *tv, void *data); @@ -2823,7 +2827,29 @@ int SigAddressPrepareStage5(DetectEngineCtx *de_ctx) { int SigGroupBuild (DetectEngineCtx *de_ctx) { SigAddressPrepareStage1(de_ctx); SigAddressPrepareStage2(de_ctx); + +#ifdef __SC_CUDA_SUPPORT__ + /* we register a module that would require cuda handler service. This + * module would hold the context for all the patterns in the rules */ + de_ctx->cuda_rc_mod_handle = SCCudaHlRegisterModule("SC_RULES_CONTENT_B2G_CUDA"); +#endif + SigAddressPrepareStage3(de_ctx); + +#ifdef __SC_CUDA_SUPPORT__ + /* the AddressPrepareStage3 actually handles the creation of device pointers + * on the gpu. The cuda context that stage3 used would still be attached to + * this host thread. We need to pop this cuda context so that the dispatcher + * thread that we are going to create for the above module we registered + * can attach to this cuda context */ + CUcontext context; + if (SCCudaCtxPopCurrent(&context) == -1) + exit(EXIT_FAILURE); + /* start the dispatcher thread for this module */ + if (B2gCudaStartDispatcherThreadRC("SC_RULES_CONTENT_B2G_CUDA") == -1) + exit(EXIT_FAILURE); +#endif + // SigAddressPrepareStage5(de_ctx); DbgPrintScanSearchStats(); // DetectAddressPrintMemory(); diff --git a/src/detect.h b/src/detect.h index 2df1e60a9a..641aebb1c2 100644 --- a/src/detect.h +++ b/src/detect.h @@ -292,6 +292,13 @@ typedef struct DetectEngineCtx_ { ThresholdCtx ths_ctx; uint16_t mpm_matcher; /**< mpm matcher this ctx uses */ + +#ifdef __SC_CUDA_SUPPORT__ + /* cuda rules content module handle. Holds the handler serivice's + * (util-cuda-handler.c) handle for a module. This module would + * hold the cuda context for all the rules content */ + int cuda_rc_mod_handle; +#endif } DetectEngineCtx; /** diff --git a/src/suricata-common.h b/src/suricata-common.h index 8d434aff0d..954283ee93 100644 --- a/src/suricata-common.h +++ b/src/suricata-common.h @@ -7,6 +7,8 @@ #ifndef __SURICATA_COMMON_H__ #define __SURICATA_COMMON_H__ +//#define __SC_CUDA_SUPPORT__ + #define TRUE 1 #define FALSE 0 diff --git a/src/suricata.c b/src/suricata.c index d2a78f7b1d..aebcecc3de 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -95,6 +95,9 @@ #include "util-daemon.h" #include "reputation.h" +/* holds the cuda b2g module */ +#include "util-mpm-b2g-cuda.h" + #include "output.h" /* @@ -623,16 +626,6 @@ int main(int argc, char **argv) SCPerfInitCounterApi(); SCReputationInitCtx(); - /** \todo we need an api for these */ - AppLayerDetectProtoThreadInit(); - RegisterAppLayerParsers(); - RegisterHTPParsers(); - RegisterTLSParsers(); - RegisterSMBParsers(); - RegisterDCERPCParsers(); - RegisterFTPParsers(); - AppLayerParsersInitPostProcess(); - TmModuleReceiveNFQRegister(); TmModuleVerdictNFQRegister(); TmModuleDecodeNFQRegister(); @@ -659,8 +652,21 @@ int main(int argc, char **argv) TmModuleLogHttpLogRegister(); TmModuleLogHttpLogIPv4Register(); TmModuleLogHttpLogIPv6Register(); +#ifdef __SC_CUDA_SUPPORT__ + TmModuleCudaMpmB2gRegister(); +#endif TmModuleDebugList(); + /** \todo we need an api for these */ + AppLayerDetectProtoThreadInit(); + RegisterAppLayerParsers(); + RegisterHTPParsers(); + RegisterTLSParsers(); + RegisterSMBParsers(); + RegisterDCERPCParsers(); + RegisterFTPParsers(); + AppLayerParsersInitPostProcess(); + #ifdef UNITTESTS if (run_mode == MODE_UNITTEST) { /* test and initialize the unittesting subsystem */ diff --git a/src/tm-modules.h b/src/tm-modules.h index 2f518a4afd..713d94a58c 100644 --- a/src/tm-modules.h +++ b/src/tm-modules.h @@ -50,6 +50,9 @@ enum { TMM_DECODEIPFW, TMM_VERDICTIPFW, TMM_RECEIVEIPFW, +#ifdef __SC_CUDA_SUPPORT__ + TMM_CUDA_MPM_B2G, +#endif TMM_SIZE, }; diff --git a/src/tm-threads.c b/src/tm-threads.c index 56aaa77421..6ff1bc22c9 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -862,6 +862,91 @@ void TmThreadAppend(ThreadVars *tv, int type) //printf("TmThreadAppend: thread \'%s\' is added to the list.\n", tv->name); } +/** + * \brief Removes this TV from tv_root based on its type + * + * \param tv The tv instance to remove from the global tv list. + * \param type Holds the type this TV belongs to. + */ +void TmThreadRemove(ThreadVars *tv, int type) +{ + SCMutexLock(&tv_root_lock); + + if (tv_root[type] == NULL) { + SCMutexUnlock(&tv_root_lock); + return; + } + + ThreadVars *t = tv_root[type]; + while (t != tv) { + t = t->next; + } + + if (t != NULL) { + if (t->prev != NULL) + t->prev->next = t->next; + if (t->next != NULL) + t->next->prev = t->prev; + + if (t == tv_root[type]) + tv_root[type] = t->next;; + } + + SCMutexUnlock(&tv_root_lock); + + return; +} + +/** + * \brief Kill a thread. + * + * \param tv A ThreadVars instance corresponding to the thread that has to be + * killed. + */ +void TmThreadKillThread(ThreadVars *tv) +{ + int i = 0; + + if (tv == NULL) + return; + + /* set the thread flag informing the thread that it needs to be + * terminated */ + TmThreadsSetFlag(tv, THV_KILL); + + if (tv->inq != NULL) { + /* signal the queue for the number of users */ + for (i = 0; i < (tv->inq->reader_cnt + tv->inq->writer_cnt); i++) + SCCondSignal(&trans_q[tv->inq->id].cond_q); + + /* to be sure, signal more */ + while (1) { + if (TmThreadsCheckFlag(tv, THV_CLOSED)) { + break; + } + + for (i = 0; i < (tv->inq->reader_cnt + tv->inq->writer_cnt); i++) + SCCondSignal(&trans_q[tv->inq->id].cond_q); + + usleep(100); + } + } + + if (tv->cond != NULL ) { + while (1) { + if (TmThreadsCheckFlag(tv, THV_CLOSED)) { + break; + } + + pthread_cond_broadcast(tv->cond); + + usleep(100); + } + } + + return; +} + void TmThreadKillThreads(void) { ThreadVars *tv = NULL; int i = 0; diff --git a/src/tm-threads.h b/src/tm-threads.h index d22660ac8a..44b71d345f 100644 --- a/src/tm-threads.h +++ b/src/tm-threads.h @@ -15,54 +15,35 @@ extern ThreadVars *tv_root[TVT_MAX]; extern SCMutex tv_root_lock; void Tm1SlotSetFunc(ThreadVars *, TmModule *, void *); - void TmVarSlotSetFuncAppend(ThreadVars *, TmModule *, void *); - ThreadVars *TmThreadCreate(char *, char *, char *, char *, char *, char *, void *(fn_p)(void *), int); - ThreadVars *TmThreadCreatePacketHandler(char *, char *, char *, char *, char *, char *); - ThreadVars *TmThreadCreateMgmtThread(char *name, void *(fn_p)(void *), int); - TmEcode TmThreadSpawn(ThreadVars *); - void TmThreadSetFlags(ThreadVars *, uint8_t); - void TmThreadSetAOF(ThreadVars *, uint8_t); - +void TmThreadKillThread(ThreadVars *); void TmThreadKillThreads(void); - void TmThreadAppend(ThreadVars *, int); +void TmThreadRemove(ThreadVars *, int); TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t); - TmEcode TmThreadSetThreadPriority(ThreadVars *, int); - TmEcode TmThreadSetupOptions(ThreadVars *); - void TmThreadPrioSummary(char *); void TmThreadInitMC(ThreadVars *); - void TmThreadTestThreadUnPaused(ThreadVars *); - void TmThreadContinue(ThreadVars *); - void TmThreadContinueThreads(void); - void TmThreadPause(ThreadVars *); - void TmThreadPauseThreads(void); - void TmThreadCheckThreadState(void); - TmEcode TmThreadWaitOnThreadInit(void); - inline int TmThreadsCheckFlag(ThreadVars *, uint8_t); inline void TmThreadsSetFlag(ThreadVars *, uint8_t); - ThreadVars *TmThreadsGetCallingThread(void); #endif /* __TM_THREADS_H__ */ diff --git a/src/util-cuda-handlers.c b/src/util-cuda-handlers.c new file mode 100644 index 0000000000..e9781bb280 --- /dev/null +++ b/src/util-cuda-handlers.c @@ -0,0 +1,631 @@ +/** + * Copyright (c) 2010 Open Information Security Foundation. + * + * \author Anoop Saldanha + * + * \file Provides cuda utility functions. + * + * A module in the engine that wants to use the cuda engine, might need + * some utilities to handle contexts, modules and device_pointers. + * + * Let us say we have a module that needs to share a context among various + * sections inside it. To enable it share contexts within various + * sections the module first register itself using the function + * SCCudaHlRegisterModule() and receive a unique handle. Once it has + * retrieved the unique handle, it can then call SCCudaHlGetCudaContext(), + * with the handle. A new cuda context would be created and the internal + * data structures would be updated to associate this newly created + * context with this module handle. Any future calls to + * SCCudaHlGetCudaContext() with the same handle will return the + * cuda_context, which has already been created and associated with the + * handle. Any calls to SCCudaHlGetCudaContext() with a new handle, + * would result in the creation of a new cuda context. + * + * Similarly if we want to create a new cuda_module against a particular + * context, we can call SCCudaHlGetCudaModule() with the handle and it + * should work as above. Please do note that a cuda module can't be + * created against a handle using SCCudaHlGetCudaModule(), unless + * a cuda_context has been associated with the handle by a previous call + * to SCCudaHlGetCudaContext(). Also do note that, a cuda module is + * created against a cuda context that is associated with the current + * host thread. So do takecare to associate your host thread with the + * cuda_context that is associated with the handle, against which you + * want to call SCCudaHlGetCudaModule(). + * + * \todo Provide support for multiple cuda context storage and creating multiple + * cuda modules against a cuda_context, although it is highly unlikely we + * would need this feature. + * + * We also need to use a mutex for module_datas. + */ + +#include "suricata-common.h" +#include "suricata.h" +#include "detect.h" +#include "decode.h" + +#include "util-cuda.h" +#include "util-cuda-handlers.h" + +#include "util-error.h" +#include "util-debug.h" +#include "util-unittest.h" +#include "packet-queue.h" + +/* macros decides if cuda is enabled for the platform or not */ +#ifdef __SC_CUDA_SUPPORT__ + +static SCCudaHlModuleData *module_datas = NULL; + +static uint8_t module_handle = 1; + +/** + * \internal + * \brief Returns a SCCudaHlModuleData instance from the global data store + * that matches the handle sent as arg. + * + * \param handle The handle for the SCCudaHlModuleData that has to be returned. + * + * \retval data The SCCudaHlModuleData instance that matches the handle. + */ +SCCudaHlModuleData *SCCudaHlGetModuleData(uint8_t handle) +{ + SCCudaHlModuleData *data = module_datas; + + if (data == NULL) + return NULL; + + while (data != NULL && data->handle != handle) { + data = data->next; + } + + return data; +} + +/** + * \internal + * \brief Get a unique handle for a new module registration. This new handle + * returned uniquely represents a module. All future calls to functions + * requires suppling this handle. + * + * \param module_handle A unique module handle that needs to used to refer + * to data(like cuda_contexts, cuda_modules, device pointers). + */ +static int SCCudaHlGetUniqueHandle(void) +{ + return module_handle++; +} + +/** + * \brief Returns a cuda context against the handle in the argument. + * + * If a cuda_context is not present for a handle, it is created + * and associated with this handle and the context is returned + * in the argument. If a cuda_context is already present for + * a handle, it is returned. + * + * \param p_context Pointer to a cuda context instance that should be updated + * with a cuda context. + * \param handle A unique handle which identifies a module. Obtained from + * a call to SCCudaHlGetUniqueHandle(). + * + * \retval 0 On success. + * \retval -1 On failure. + */ +int SCCudaHlGetCudaContext(CUcontext *p_context, int handle) +{ + SCCudaHlModuleData *data = NULL; + SCCudaDevices *devices = NULL; + + if (p_context == NULL) { + SCLogError(SC_INVALID_ARGUMENTS, "Error invalid arguments. " + "p_context NULL"); + return -1; + } + + /* check if the particular module that wants a CUDA context + * is already registered or not. If it is not registered + * log a warning and get out of here */ + if ( (data = SCCudaHlGetModuleData(handle)) == NULL) { + SCLogDebug("Module not registered. You can't create a CUDA context " + "without registering a module first. To use this " + "registration facility, first register a module using " + "SCCudaHlRegisterModule(), and then register " + "a cuda context with that module hanle using " + "SCCudaHlGetCudaContext(), after which you can call this " + "function "); + return -1; + } + + if (data->cuda_context != 0) { + p_context[0] = data->cuda_context; + return 0; + } + + /* Get the device list for this CUDA platform and create a new cuda context */ + devices = SCCudaGetDeviceList(); + if (SCCudaCtxCreate(p_context, 0, devices->devices[0]->device) == -1) + goto error; + data->cuda_context = p_context[0]; + + return 0; + + error: + return -1; +} + +/** + * \brief Returns a cuda_module against the handle in the argument. + * + * If a cuda_module is not present for a handle, it is created + * and associated with this handle and the cuda_module is returned + * in the argument. If a cuda_module is already present for + * a handle, it is returned. + * + * \param p_context Pointer to a cuda context instance that should be updated + * with a cuda context. + * \param handle A unique handle which identifies a module. Obtained from + * a call to SCCudaHlGetUniqueHandle(). + * + * \retval 0 On success. + * \retval -1 On failure. + */ +int SCCudaHlGetCudaModule(CUmodule *p_module, const char *ptx_image, int handle) +{ + SCCudaHlModuleData *data = NULL; + + if (p_module == NULL) { + SCLogError(SC_INVALID_ARGUMENTS, "Error invalid arguments" + "p_module NULL"); + return -1; + } + + /* check if the particular module that wants a CUDA module is already + * registered or not. If it is registered, check if a context has + * been associated with the module. If yes, then we can go ahead and + * create a cuda module or return the reference to the cuda module if + * we already have a cuda module associated with the module. If no, " + * log warning and get out of here */ + if ( ((data = SCCudaHlGetModuleData(handle)) == NULL) || + (data->cuda_context == 0)) { + SCLogDebug("Module not registered or no cuda context associated with " + "this module. You can't create a CUDA module without" + "associatin a context with a module first. To use this " + "registration facility, first register a module using " + "context using SCCudaHlRegisterModule(), and then register " + "a cuda context with that module using " + "SCCudaHlGetCudaContext(), after which you can call this " + "function "); + return -1; + } + + /* we already have a cuda module associated with this module. Return the + * cuda module */ + if (data->cuda_module != 0) { + p_module[0] = data->cuda_module; + return 0; + } + + /* we don't have a cuda module associated with this module. Create a + * cuda module, update the module with this cuda module reference and + * then return the module refernce back to the calling function using + * the argument */ + if (SCCudaModuleLoadData(p_module, (void *)ptx_image) == -1) + goto error; + data->cuda_module = p_module[0]; + + return 0; + + error: + return -1; +} + +/** + * \brief Verify if a device pointer by a particular name is registered under + * a module. If it is registered, return this device pointer instance + * back; else return NULL. + * + * \param data Pointer to the module SCCudaHlModuleData instance which has to + * checked for the registration of the device pointer. + * \param name Name of the device pointer to search in the module. + * + * \retval module_device_ptr Pointer to the device pointer instance on finding + * it; NULL otherwise. + */ +SCCudaHlModuleDevicePointer *SCCudaHlCudaDevicePtrAvailable(SCCudaHlModuleData *data, + const char *name) +{ + SCCudaHlModuleDevicePointer *module_device_ptr = data->device_ptrs; + + while (module_device_ptr != NULL && + strcmp(module_device_ptr->name, name) != 0) { + module_device_ptr = module_device_ptr->next; + } + + return module_device_ptr; +} + +/** + * \brief Returns a cuda_device_pointer against the handle in the argument. + * + * If a device pointer by the name \"name\" is not registered for the + * handle, it is created and associated with this handle and cuda mem is + * alloted and the cuda_device_pointer is returned in the argument. + * If a device pointer by the name \"name\" is already registered with + * the handle, the cuda_device_pointer is returned in the argument. + * + * \param device_ptr Pointer to the device pointer instance which should be + * with the cuda_device_pointer that has to be returned back. + * \param name Name of the device pointer by which we have to search + * module for its existance. + * \param size Size of the cuda device memory to be alloted. + * \param host_ptr If any host memory has to be transferred to the cuda device + * memory, it can sent using this argument. host_ptr should + * hold atleast size bytes in memory. + * \param handle A unique handle which identifies a module. Obtained from + * a call to SCCudaHlGetUniqueHandle(). + * + * \retval 0 On success. + * \retval -1 On failure. + */ +int SCCudaHlGetCudaDevicePtr(CUdeviceptr *device_ptr, const char *name, + size_t size, void *host_ptr, int handle) +{ + SCCudaHlModuleData *data = NULL; + SCCudaHlModuleDevicePointer *new_module_device_ptr = NULL; + SCCudaHlModuleDevicePointer *module_device_ptr = NULL; + + if (device_ptr == NULL || name == NULL) { + SCLogError(SC_INVALID_ARGUMENTS, "Error invalid arguments" + "device_ptr is NULL or name is NULL"); + goto error; + } + + /* check if the particular module that wants to allocate device memory is + * already registered or not. If it is registered, check if a context has + * been associated with the module. If yes, then we can go ahead and + * create the device memory or return the reference to the device memory if + * we already have the device memory associated with the module. If no, " + * log warning and get out of here */ + if ( ((data = SCCudaHlGetModuleData(handle)) == NULL) || + (data->cuda_context == 0)) { + SCLogDebug("Module not registered or no cuda context associated with " + "this module. You can't create a CUDA module without" + "associatin a context with a module first. To use this " + "registration facility, first register a module using " + "context using SCCudaHlRegisterModule(), and then register " + "a cuda context with that module using " + "SCCudaHlGetCudaContext(), after which you can call this " + "function "); + goto error; + } + + /* if we already have a device pointer registered by this name return the + * cuda device pointer instance */ + if ( (module_device_ptr = SCCudaHlCudaDevicePtrAvailable(data, name)) != NULL) { + device_ptr[0] = module_device_ptr->d_ptr; + return 0; + } + + new_module_device_ptr = malloc(sizeof(SCCudaHlModuleDevicePointer)); + if (new_module_device_ptr == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); + exit(EXIT_FAILURE); + } + memset(new_module_device_ptr, 0, sizeof(SCCudaHlModuleDevicePointer)); + + if ( (new_module_device_ptr->name = strdup(name)) == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); + exit(EXIT_FAILURE); + } + + /* allocate the cuda memory */ + if (SCCudaMemAlloc(&new_module_device_ptr->d_ptr, size) == -1) + goto error; + + /* if the user has supplied a host buffer, copy contents to the device mem */ + if (host_ptr != NULL) { + if (SCCudaMemcpyHtoD(new_module_device_ptr->d_ptr, host_ptr, + size) == -1) { + goto error; + } + } + + /* insert it into the device_ptr list for the module instance */ + if (data->device_ptrs == NULL) { + data->device_ptrs = new_module_device_ptr; + device_ptr[0] = new_module_device_ptr->d_ptr; + return 0; + } + + module_device_ptr = data->device_ptrs; + while (module_device_ptr->next != NULL) + module_device_ptr = module_device_ptr->next; + module_device_ptr->next = new_module_device_ptr; + + return 0; + + error: + if (new_module_device_ptr != NULL) + free(new_module_device_ptr); + return -1; +} + +/** + * \brief Registers a Dispatcher function against this handle. + * + * \param SCCudaHlDispFunc Pointer to a dispatcher function to be registered + * for this handle. + * \param handle A unique handle which identifies a module. Obtained + * from a call to SCCudaHlGetUniqueHandle(). + * + * \retval 0 On success. + * \retval -1 On failure. + */ +int SCCudaHlRegisterDispatcherFunc(void *(*SCCudaHlDispFunc)(void *), int handle) +{ + SCCudaHlModuleData *data = NULL; + + if (SCCudaHlDispFunc == NULL) { + SCLogError(SC_INVALID_ARGUMENTS, "Error invalid arguments" + "SCCudaHlDispFunc NULL"); + return -1; + } + + if ( (data = SCCudaHlGetModuleData(handle)) == NULL) { + SCLogDebug("Module not registered. To avail the benefits of this " + "registration facility, first register a module using " + "context using SCCudaHlRegisterModule(), after which you " + "can call this function"); + return -1; + } + + data->SCCudaHlDispFunc = SCCudaHlDispFunc; + + return 0; +} + +/** + * \brief Get the name of the module associated with the module whose handle is + * sent as the arg. + * + * \param handle The handle of the module which has to be searched. + * + * \retval data->name The name of the module on finding a module that matches + * the handle sent as argument; NULL on failure. + */ +const char *SCCudaHlGetModuleName(int handle) +{ + SCCudaHlModuleData *data = module_datas; + + while (data != NULL && data->handle != handle) { + data = data->next; + } + + if (data == NULL) + return NULL; + + return data->name; +} + +/** + * \brief Get the handle associated with this module who name is sent as the arg. + * + * \param name The name of the module which has to be searched. + * + * \retval data->handle The handle to the module on finding a module that + * matches the name sent as argument; -1 on failure. + */ +int SCCudaHlGetModuleHandle(const char *name) +{ + SCCudaHlModuleData *data = module_datas; + + while (data != NULL && + strcmp(data->name, name) != 0) { + data = data->next; + } + + if (data == NULL) + return -1; + + return data->handle; +} + +/** + * \brief Register a new module. To understand what exactly these utilities are + * needed for please look at the file comments. + * + * \param name A unique name to register the module with. No module should have + * registered itself previously with this name. + * + * \retval handle A unique handle that is associated with this module and all + * future use of API would require supplying this handle. + */ +int SCCudaHlRegisterModule(const char *name) +{ + SCCudaHlModuleData *data = module_datas; + SCCudaHlModuleData *new_data = NULL; + + while (data != NULL && + strcmp(data->name, name) != 0) { + data = data->next; + } + + if (data != NULL) { + SCLogError(SC_ERR_CUDA_HANDLER_ERROR, "Module \"%s\" already " + "registered. Returning the handle for the already " + "registered module", name); + return data->handle; + } + + /* the module is not already registered. Register the module */ + new_data = malloc(sizeof(SCCudaHlModuleData)); + if (new_data == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); + exit(EXIT_FAILURE); + } + memset(new_data, 0, sizeof(SCCudaHlModuleData)); + + if ( (new_data->name = strdup(name)) == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); + exit(EXIT_FAILURE); + } + + new_data->handle = SCCudaHlGetUniqueHandle(); + + /* first module to be registered */ + if (module_datas == NULL) { + module_datas = new_data; + return new_data->handle; + } + + /* add this new module_data instance to the global module_data list */ + data = module_datas; + while (data->next != NULL) + data = data->next; + data->next = new_data; + + return new_data->handle; +} + +/** + * \brief DeRegister a registered module. + * + * \param name Name of the module to deregister. + * + * \retval 0 On success. + * \retval -1 On failure. + */ +int SCCudaHlDeRegisterModule(const char *name) +{ + SCCudaHlModuleData *data = NULL; + SCCudaHlModuleData *prev_data = NULL; + SCCudaHlModuleDevicePointer *device_ptr = NULL; + SCCudaHlModuleDevicePointer *temp_device_ptr = NULL; + int module_handle = SCCudaHlGetModuleHandle(name); + + /* get the module */ + data = (module_handle == -1) ? NULL : SCCudaHlGetModuleData(module_handle); + + /* a module by this name doesn't exist. Log Error and return */ + if (data == NULL) { + SCLogError(SC_ERR_CUDA_HANDLER_ERROR, "Module \"%s\" not " + "registered", name); + return -1; + } + + /* looks like we do have a module registered by this name */ + /* first clean the cuda device pointers */ + device_ptr = data->device_ptrs; + while (device_ptr != NULL) { + temp_device_ptr = device_ptr; + device_ptr = device_ptr->next; + if (SCCudaMemFree(temp_device_ptr->d_ptr) == -1) + goto error; + free(temp_device_ptr->name); + free(temp_device_ptr); + } + data->device_ptrs = NULL; + + /* clean the dispatcher function registered */ + data->SCCudaHlDispFunc = NULL; + + /* unload the cuda module */ + if (data->cuda_module != 0) { + if (SCCudaModuleUnload(data->cuda_module) == -1) + goto error; + } + + /* destroy the cuda context */ + if (data->cuda_context != 0) { + if (SCCudaCtxDestroy(data->cuda_context) == -1) + goto error; + } + + /* find the previous module data instance */ + if (module_datas == data) { + module_datas = module_datas->next; + } else { + prev_data = module_datas; + while (prev_data->next != data) + prev_data = prev_data->next; + prev_data->next = data->next; + } + + /* delete the module data instance */ + free(data); + + /* mission accomplished. let's go */ + return 0; + error: + return -1; +} + +/** + * \brief Pushes a cuda context for the calling thread. + * + * Before calling this function make sure that the cuda context belonging + * to the registered module, is floating(not attached to any host thread). + * + * \param name Name of the registered module whose cuda context has to be + * pushed for the calling thread. + * + * \retval 0 On success. + * \retval -1 On failure. + */ +int SCCudaHlPushCudaContextFromModule(const char *name) +{ + SCCudaHlModuleData *data = SCCudaHlGetModuleData(SCCudaHlGetModuleHandle(name)); + + if (data == NULL) { + SCLogError(SC_ERR_CUDA_HANDLER_ERROR, "No module registered by the " + "name \"%s\"", name); + return -1; + } + + if (SCCudaCtxPushCurrent(data->cuda_context) == -1) { + SCLogError(SC_ERR_CUDA_HANDLER_ERROR, "Error pushing cuda context from " + "module \"%s\" for this calling thread\n", name); + return -1; + } + + return 0; +} + +/** + * \brief Used for testing purposes. Running tests with cuda enabled + * requires some hacks, which is what this function does. + * + * \retval 1 Always. + */ +int SCCudaHlTestEnvCudaContextInit(void) +{ + CUcontext context; + int module_handle = SCCudaHlRegisterModule("SC_RULES_CONTENT_B2G_CUDA"); + if (SCCudaHlGetCudaContext(&context, module_handle) == -1) { + printf("Error getting a cuda context"); + } + if (SCCudaHlPushCudaContextFromModule("SC_RULES_CONTENT_B2G_CUDA") == -1) { + printf("Call to SCCudaHlPushCudaContextForModule() failed\n"); + } + + return 1; +} + +/** + * \brief Used for testing purposes. Running tests with cuda enabled + * requires some hacks, which is what this function does. + * + * \retval 1 Always. + */ +int SCCudaHlTestEnvCudaContextDeInit(void) +{ + if (SCCudaCtxPopCurrent(NULL) == -1) { + printf("Call to SCCudaCtxPopCurrent() failed\n"); + return 0; + } + + return 1; +} + +#endif /* __SC_CUDA_SUPPORT */ diff --git a/src/util-cuda-handlers.h b/src/util-cuda-handlers.h new file mode 100644 index 0000000000..7a7eefe900 --- /dev/null +++ b/src/util-cuda-handlers.h @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2010 Open Information Security Foundation. + * + * \author Anoop Saldanha + */ + +/* macros decides if cuda is enabled for the platform or not */ +#ifdef __SC_CUDA_SUPPORT__ + +#include + +#ifndef __UTIL_MPM_CUDA_HANDLERS_H__ +#define __UTIL_MPM_CUDA_HANDLERS_H__ + +typedef enum { + SC_CUDA_HL_MTYPE_RULE_NONE = -1, + SC_CUDA_HL_MTYPE_RULE_CONTENTS = 0, + SC_CUDA_HL_MTYPE_RULE_URICONTENTS, + SC_CUDA_HL_MTYPE_APP_LAYER, + SC_CUDA_HL_MTYPE_RULE_CUSTOM, + SC_CUDA_HL_MTYPE_MAX, +} SCCudaHlModuleType; + +typedef struct SCCudaHlModuleDevicePointer_ { + /* device pointer name. This is a primary key. For the same module you + * can't register different device pointers */ + char *name; + CUdeviceptr d_ptr; + + struct SCCudaHlModuleDevicePointer_ *next; +} SCCudaHlModuleDevicePointer; + +typedef struct SCCudaHlModuleData_ { + /* The unique module handle. This has to be first obtained from the + * call to SCCudaHlGetUniqueHandle() */ + const char *name; + int handle; + + CUcontext cuda_context; + CUmodule cuda_module; + void *(*SCCudaHlDispFunc)(void *); + SCCudaHlModuleDevicePointer *device_ptrs; + + struct SCCudaHlModuleData_ *next; +} SCCudaHlModuleData; + +int SCCudaHlGetCudaContext(CUcontext *, int); +int SCCudaHlGetCudaModule(CUmodule *, const char *, int); +int SCCudaHlGetCudaDevicePtr(CUdeviceptr *, const char *, size_t, void *, int); +int SCCudaHlRegisterDispatcherFunc(void *(*SCCudaHlDispFunc)(void *), int); + +SCCudaHlModuleData *SCCudaHlGetModuleData(uint8_t); +const char *SCCudaHlGetModuleName(int); +int SCCudaHlGetModuleHandle(const char *); + +int SCCudaHlRegisterModule(const char *); +int SCCudaHlDeRegisterModule(const char *); + +int SCCudaHlPushCudaContextFromModule(const char *); + +int SCCudaHlTestEnvCudaContextInit(void); +int SCCudaHlTestEnvCudaContextDeInit(void); + +#endif /* __UTIL_CUDA_HANDLERS__ */ + +#endif /* __SC_CUDA_SUPPORT__ */ diff --git a/src/util-cuda.c b/src/util-cuda.c index 90f5f25ab5..97aa33fdad 100644 --- a/src/util-cuda.c +++ b/src/util-cuda.c @@ -7,10 +7,6 @@ // use this for now #define __SC_CUDA_SUPPORT__ -/* compile in, only if we have a CUDA enabled device on the machine, with the - * toolkit and the driver installed */ -#ifdef __SC_CUDA_SUPPORT__ - #include #include "util-cuda.h" #include "suricata-common.h" @@ -19,6 +15,10 @@ #include "util-debug.h" #include "util-unittest.h" +/* compile in, only if we have a CUDA enabled device on the machine, with the + * toolkit and the driver installed */ +#ifdef __SC_CUDA_SUPPORT__ + #define CASE_CODE(E) case E: return #E typedef enum SCCudaAPIS_ { @@ -3213,12 +3213,6 @@ int SCCudaCtxPopCurrent(CUcontext *pctx) { CUresult result = 0; - if (pctx == NULL) { - SCLogError(SC_INVALID_ARGUMENTS, "Invalid argument supplied. " - "pctx NULL"); - goto error; - } - result = cuCtxPopCurrent(pctx); if (SCCudaHandleRetValue(result, SC_CUDA_CU_CTX_POP_CURRENT) == -1) goto error; @@ -3995,8 +3989,6 @@ static int SCCudaInit(unsigned int flags) return -1; } -#endif /* __SC_CUDA_SUPPORT__ */ - /**************************Cuda_Env_Initialization_API*************************/ /** @@ -4171,3 +4163,5 @@ void SCCudaRegisterTests(void) return; } + +#endif /* __SC_CUDA_SUPPORT__ */ diff --git a/src/util-error.c b/src/util-error.c index f273e300a8..abaa7b8d37 100644 --- a/src/util-error.c +++ b/src/util-error.c @@ -106,6 +106,11 @@ const char * SCErrorToString(SCError err) CASE_CODE (SC_ERR_PCAP_RECV_INIT); CASE_CODE (SC_ERR_NFQ_UNBIND); CASE_CODE (SC_ERR_NFQ_BIND); + CASE_CODE (SC_ERR_CUDA_ERROR); + CASE_CODE (SC_ERR_CUDA_HANDLER_ERROR); + CASE_CODE (SC_ERR_TM_THREADS_ERROR); + CASE_CODE (SC_ERR_TM_MODULES_ERROR); + CASE_CODE (SC_ERR_B2G_CUDA_ERROR); default: return "UNKNOWN_ERROR"; } diff --git a/src/util-error.h b/src/util-error.h index fc01b5c835..43e59a00a6 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -124,6 +124,10 @@ typedef enum { SC_ERR_INVALID_OPERATOR, SC_ERR_PCAP_RECV_INIT, SC_ERR_CUDA_ERROR, + SC_ERR_CUDA_HANDLER_ERROR, + SC_ERR_TM_THREADS_ERROR, + SC_ERR_TM_MODULES_ERROR, + SC_ERR_B2G_CUDA_ERROR, } SCError; const char *SCErrorToString(SCError); diff --git a/src/util-mpm-b2g-cuda.c b/src/util-mpm-b2g-cuda.c new file mode 100644 index 0000000000..7ac0cf8667 --- /dev/null +++ b/src/util-mpm-b2g-cuda.c @@ -0,0 +1,4072 @@ +/** + * Copyright (c) 2009 Open Information Security Foundation. + * + * \author Victor Julien + * \author Anoop Saldanha + * + * \todo Lot of work on the kernel pending. Includes handling kernel block + * handling, optimization with shared memory, blah blah blah.... We + * will come back to that once we have the cuda framework in place. + */ + +#include "suricata-common.h" +#include "suricata.h" +#include "detect.h" +#include "util-bloomfilter.h" +#include "util-mpm-b2g-cuda.h" +#include "util-mpm.h" +#include "util-print.h" +#include "threadvars.h" +#include "tm-modules.h" + +#include "util-error.h" +#include "util-debug.h" +#include "util-unittest.h" + +#include "app-layer-detect-proto.h" +#include "util-cuda-handlers.h" +#include "util-cuda.h" +#include "tm-threads.h" +#include "threads.h" + +/* macros decides if cuda is enabled for the platform or not */ +#ifdef __SC_CUDA_SUPPORT__ + +#define INIT_HASH_SIZE 65536 + +#ifdef B2G_CUDA_COUNTERS +#define COUNT(counter) (counter) +#else +#define COUNT(counter) +#endif /* B2G_CUDA_COUNTERS */ + +/* threadvars Cuda(C) Mpm(M) B2G(B) Rules(R) Content(C) */ +ThreadVars *tv_CMB2_RC = NULL; + +/* threadvars Cuda(C) Mpm(M) B2G(B) App(A) Proto(P) Content(C) */ +ThreadVars *tv_CMB2_APC = NULL; + +/** + * \todo Would break on x86_64 I believe. We will fix this in a later version. + */ +#define B2G_CUDA_KERNEL_ARG0_OFFSET 0 +#define B2G_CUDA_KERNEL_ARG1_OFFSET 4 +#define B2G_CUDA_KERNEL_ARG2_OFFSET 8 +#define B2G_CUDA_KERNEL_ARG3_OFFSET 12 +#define B2G_CUDA_KERNEL_ARG4_OFFSET 16 +#define B2G_CUDA_KERNEL_ARG5_OFFSET 20 +#define B2G_CUDA_KERNEL_TOTAL_ARG_SIZE 24 + +void B2gCudaInitCtx(MpmCtx *, int); +void B2gCudaThreadInitCtx(MpmCtx *, MpmThreadCtx *, uint32_t); +void B2gCudaDestroyCtx(MpmCtx *); +void B2gCudaThreadDestroyCtx(MpmCtx *, MpmThreadCtx *); +int B2gCudaAddScanPatternCI(MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t, + uint32_t, uint32_t, uint8_t); +int B2gCudaAddScanPatternCS(MpmCtx *, uint8_t *, uint16_t, uint16_t, uint16_t, + uint32_t, uint32_t, uint8_t); +int B2gCudaAddPatternCI(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, + uint16_t offset, uint16_t depth, uint32_t pid, + uint32_t sid); +int B2gCudaAddPatternCS(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, + uint16_t offset, uint16_t depth, uint32_t pid, + uint32_t sid); +int B2gCudaPreparePatterns(MpmCtx *mpm_ctx); +inline uint32_t B2gCudaScanWrap(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, + uint16_t buflen); +inline uint32_t B2gCudaSearchWrap(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, + uint16_t buflen); +uint32_t B2gCudaScan1(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, uint16_t buflen); + +#ifdef B2G_SCAN2 +uint32_t B2gCudaScan2(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, uint16_t buflen); +#endif + +uint32_t B2gCudaScan(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, uint16_t buflen); +uint32_t B2gCudaScanBNDMq(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen); +uint32_t B2gCudaSearch1(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, uint16_t buflen); +uint32_t B2gCudaSearchBNDMq(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, + uint16_t buflen); +uint32_t B2gCudaSearch(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *, uint8_t *buf, uint16_t buflen); +void B2gCudaPrintInfo(MpmCtx *mpm_ctx); +void B2gCudaPrintSearchStats(MpmThreadCtx *mpm_thread_ctx); +void B2gCudaRegisterTests(void); + +/* for debugging purposes. keep it for now */ +int arg0 = 0; +int arg1 = 0; +int arg2 = 0; +int arg3 = 0; +int arg4 = 0; +int arg5 = 0; +int arg_total = 0; + +/** + * \todo Optimize the kernel. Also explore the options for compiling the + * *.cu file at compile/runtime. + */ +const char *b2g_cuda_ptx_image = + " .version 1.4\n" + " .target sm_10, map_f64_to_f32\n" + " .entry B2gCudaSearchBNDMq (\n" + " .param .u32 __cudaparm_B2gCudaSearchBNDMq_offsets,\n" + " .param .u32 __cudaparm_B2gCudaSearchBNDMq_search_B2G,\n" + " .param .u32 __cudaparm_B2gCudaSearchBNDMq_g_u8_lowercasetable,\n" + " .param .u32 __cudaparm_B2gCudaSearchBNDMq_buf,\n" + " .param .u16 __cudaparm_B2gCudaSearchBNDMq_arg_buflen,\n" + " .param .u32 __cudaparm_B2gCudaSearchBNDMq_search_m)\n" + " {\n" + " .reg .u32 %r<81>;\n" + " .reg .pred %p<14>;\n" + " .loc 15 14 0\n" + "$LBB1_B2gCudaSearchBNDMq:\n" + " .loc 15 16 0\n" + " ld.param.u32 %r1, [__cudaparm_B2gCudaSearchBNDMq_search_m];\n" + " sub.u32 %r2, %r1, 1;\n" + " mov.s32 %r3, %r2;\n" + " .loc 15 22 0\n" + " ld.param.u16 %r4, [__cudaparm_B2gCudaSearchBNDMq_arg_buflen];\n" + " shr.u32 %r5, %r4, 4;\n" + " cvt.u16.u32 %r6, %r5;\n" + " mov.s32 %r7, %r6;\n" + " setp.ge.u32 %p1, %r6, %r1;\n" + " @%p1 bra $Lt_0_8450;\n" + " .loc 15 27 0\n" + " cvt.u16.u32 %r7, %r1;\n" + "$Lt_0_8450:\n" + " cvt.u32.u16 %r8, %tid.x;\n" + " mul.lo.u32 %r9, %r7, %r8;\n" + " cvt.u16.u32 %r10, %r9;\n" + " add.s32 %r11, %r7, %r10;\n" + " setp.ge.s32 %p2, %r4, %r11;\n" + " @%p2 bra $Lt_0_8962;\n" + " bra.uni $LBB23_B2gCudaSearchBNDMq;\n" + "$Lt_0_8962:\n" + " .loc 15 33 0\n" + " mul24.lo.s32 %r12, %r7, 2;\n" + " sub.s32 %r13, %r12, 1;\n" + " mov.s32 %r14, %r13;\n" + " cvt.u16.u32 %r15, %r14;\n" + " mov.s32 %r16, %r15;\n" + " add.s32 %r17, %r10, %r15;\n" + " set.lt.u32.s32 %r18, %r4, %r17;\n" + " neg.s32 %r19, %r18;\n" + " mov.u32 %r20, 15;\n" + " set.eq.u32.u32 %r21, %r8, %r20;\n" + " neg.s32 %r22, %r21;\n" + " or.b32 %r23, %r19, %r22;\n" + " mov.u32 %r24, 0;\n" + " setp.eq.s32 %p3, %r23, %r24;\n" + " @%p3 bra $Lt_0_9474;\n" + " .loc 15 35 0\n" + " sub.u32 %r25, %r4, %r9;\n" + " cvt.u16.u32 %r16, %r25;\n" + "$Lt_0_9474:\n" + " mov.u32 %r26, 0;\n" + " setp.eq.u32 %p4, %r16, %r26;\n" + " @%p4 bra $Lt_0_9986;\n" + " mov.s32 %r27, %r16;\n" + " ld.param.u32 %r28, [__cudaparm_B2gCudaSearchBNDMq_offsets];\n" + " mov.u32 %r29, 0;\n" + " mov.s32 %r30, %r27;\n" + "$Lt_0_10498:\n" + " // Loop body line 35, nesting depth: 1, estimated iterations: unknown\n" + " .loc 15 40 0\n" + " mov.u32 %r31, 0;\n" + " add.u32 %r32, %r10, %r29;\n" + " mul.lo.u32 %r33, %r32, 4;\n" + " add.u32 %r34, %r28, %r33;\n" + " st.global.u32 [%r34+0], %r31;\n" + " add.u32 %r29, %r29, 1;\n" + " setp.ne.u32 %p5, %r16, %r29;\n" + " @%p5 bra $Lt_0_10498;\n" + "$Lt_0_9986:\n" + " sub.u32 %r35, %r16, 1;\n" + " setp.gt.u32 %p6, %r2, %r35;\n" + " @%p6 bra $LBB23_B2gCudaSearchBNDMq;\n" + " ld.param.u32 %r36, [__cudaparm_B2gCudaSearchBNDMq_g_u8_lowercasetable];\n" + " ld.param.u32 %r37, [__cudaparm_B2gCudaSearchBNDMq_search_B2G];\n" + " ld.param.u32 %r38, [__cudaparm_B2gCudaSearchBNDMq_buf];\n" + "$Lt_0_11522:\n" + " // Loop body line 46\n" + " .loc 15 46 0\n" + " add.u32 %r39, %r10, %r3;\n" + " add.u32 %r40, %r39, %r38;\n" + " ld.global.u8 %r41, [%r40+0];\n" + " add.u32 %r42, %r41, %r36;\n" + " ld.global.u8 %r43, [%r42+0];\n" + " ld.global.u8 %r44, [%r40+-1];\n" + " add.u32 %r45, %r44, %r36;\n" + " ld.global.u8 %r46, [%r45+0];\n" + " shl.b32 %r47, %r46, 4;\n" + " or.b32 %r48, %r43, %r47;\n" + " mul.lo.u32 %r49, %r48, 4;\n" + " add.u32 %r50, %r37, %r49;\n" + " ld.global.u32 %r51, [%r50+0];\n" + " mov.u32 %r52, 0;\n" + " setp.eq.u32 %p7, %r51, %r52;\n" + " @%p7 bra $Lt_0_258;\n" + " // Part of loop body line 46, head labeled $Lt_0_11522\n" + " .loc 15 49 0\n" + " mov.s32 %r29, %r3;\n" + " .loc 15 50 0\n" + " sub.u32 %r53, %r3, %r1;\n" + " add.u32 %r54, %r53, 1;\n" + " sub.s32 %r55, %r1, 1;\n" + "$Lt_0_12546:\n" + " // Loop body line 53\n" + " .loc 15 53 0\n" + " sub.u32 %r29, %r29, 1;\n" + " shr.u32 %r56, %r51, %r55;\n" + " mov.u32 %r57, 0;\n" + " setp.eq.u32 %p8, %r56, %r57;\n" + " @%p8 bra $Lt_0_13314;\n" + " // Part of loop body line 53, head labeled $Lt_0_12546\n" + " setp.ge.u32 %p9, %r54, %r29;\n" + " @%p9 bra $Lt_0_13570;\n" + " // Part of loop body line 53, head labeled $Lt_0_12546\n" + " .loc 15 56 0\n" + " mov.s32 %r3, %r29;\n" + " bra.uni $Lt_0_13314;\n" + "$Lt_0_13570:\n" + " // Part of loop body line 53, head labeled $Lt_0_12546\n" + " .loc 15 58 0\n" + " mov.u32 %r58, 1;\n" + " ld.param.u32 %r59, [__cudaparm_B2gCudaSearchBNDMq_offsets];\n" + " add.u32 %r60, %r10, %r29;\n" + " mul.lo.u32 %r61, %r60, 4;\n" + " add.u32 %r62, %r59, %r61;\n" + " st.global.u32 [%r62+0], %r58;\n" + "$Lt_0_13314:\n" + "$Lt_0_12802:\n" + " // Part of loop body line 53, head labeled $Lt_0_12546\n" + " .loc 15 63 0\n" + " mov.u32 %r63, 0;\n" + " setp.eq.u32 %p10, %r29, %r63;\n" + " @%p10 bra $Lt_0_258;\n" + " // Part of loop body line 53, head labeled $Lt_0_12546\n" + " .loc 15 66 0\n" + " add.u32 %r64, %r10, %r29;\n" + " add.u32 %r65, %r64, %r38;\n" + " ld.global.u8 %r66, [%r65+0];\n" + " add.u32 %r67, %r66, %r36;\n" + " ld.global.u8 %r68, [%r67+0];\n" + " ld.global.u8 %r69, [%r65+-1];\n" + " add.u32 %r70, %r69, %r36;\n" + " ld.global.u8 %r71, [%r70+0];\n" + " shl.b32 %r72, %r71, 4;\n" + " or.b32 %r73, %r68, %r72;\n" + " mul.lo.u32 %r74, %r73, 4;\n" + " add.u32 %r75, %r37, %r74;\n" + " ld.global.u32 %r76, [%r75+0];\n" + " shl.b32 %r77, %r51, 1;\n" + " and.b32 %r51, %r76, %r77;\n" + " mov.u32 %r78, 0;\n" + " setp.ne.u32 %p11, %r51, %r78;\n" + " @%p11 bra $Lt_0_12546;\n" + "$Lt_0_258:\n" + "$Lt_0_11778:\n" + " // Part of loop body line 46, head labeled $Lt_0_11522\n" + " .loc 15 69 0\n" + " add.u32 %r79, %r3, %r1;\n" + " sub.u32 %r3, %r79, 1;\n" + " setp.ge.u32 %p12, %r35, %r3;\n" + " @%p12 bra $Lt_0_11522;\n" + "$LBB23_B2gCudaSearchBNDMq:\n" + " .loc 15 72 0\n" + " exit;\n" + "$LDWend_B2gCudaSearchBNDMq:\n" + " } // B2gCudaSearchBNDMq\n" + "\n" + " .entry B2gCudaScanBNDMq (\n" + " .param .u32 __cudaparm_B2gCudaScanBNDMq_offsets,\n" + " .param .u32 __cudaparm_B2gCudaScanBNDMq_scan_B2G,\n" + " .param .u32 __cudaparm_B2gCudaScanBNDMq_g_u8_lowercasetable,\n" + " .param .u32 __cudaparm_B2gCudaScanBNDMq_buf,\n" + " .param .u16 __cudaparm_B2gCudaScanBNDMq_arg_buflen,\n" + " .param .u32 __cudaparm_B2gCudaScanBNDMq_scan_m)\n" + " {\n" + " .reg .u32 %r<81>;\n" + " .reg .pred %p<14>;\n" + " .loc 15 80 0\n" + "$LBB1_B2gCudaScanBNDMq:\n" + " .loc 15 82 0\n" + " ld.param.u32 %r1, [__cudaparm_B2gCudaScanBNDMq_scan_m];\n" + " sub.u32 %r2, %r1, 1;\n" + " mov.s32 %r3, %r2;\n" + " .loc 15 88 0\n" + " ld.param.u16 %r4, [__cudaparm_B2gCudaScanBNDMq_arg_buflen];\n" + " shr.u32 %r5, %r4, 4;\n" + " cvt.u16.u32 %r6, %r5;\n" + " mov.s32 %r7, %r6;\n" + " setp.ge.u32 %p1, %r6, %r1;\n" + " @%p1 bra $Lt_1_8450;\n" + " .loc 15 93 0\n" + " cvt.u16.u32 %r7, %r1;\n" + "$Lt_1_8450:\n" + " cvt.u32.u16 %r8, %tid.x;\n" + " mul.lo.u32 %r9, %r7, %r8;\n" + " cvt.u16.u32 %r10, %r9;\n" + " add.s32 %r11, %r7, %r10;\n" + " setp.ge.s32 %p2, %r4, %r11;\n" + " @%p2 bra $Lt_1_8962;\n" + " bra.uni $LBB23_B2gCudaScanBNDMq;\n" + "$Lt_1_8962:\n" + " .loc 15 99 0\n" + " mul24.lo.s32 %r12, %r7, 2;\n" + " sub.s32 %r13, %r12, 1;\n" + " mov.s32 %r14, %r13;\n" + " cvt.u16.u32 %r15, %r14;\n" + " mov.s32 %r16, %r15;\n" + " add.s32 %r17, %r10, %r15;\n" + " set.lt.u32.s32 %r18, %r4, %r17;\n" + " neg.s32 %r19, %r18;\n" + " mov.u32 %r20, 15;\n" + " set.eq.u32.u32 %r21, %r8, %r20;\n" + " neg.s32 %r22, %r21;\n" + " or.b32 %r23, %r19, %r22;\n" + " mov.u32 %r24, 0;\n" + " setp.eq.s32 %p3, %r23, %r24;\n" + " @%p3 bra $Lt_1_9474;\n" + " .loc 15 101 0\n" + " sub.u32 %r25, %r4, %r9;\n" + " cvt.u16.u32 %r16, %r25;\n" + "$Lt_1_9474:\n" + " mov.u32 %r26, 0;\n" + " setp.eq.u32 %p4, %r16, %r26;\n" + " @%p4 bra $Lt_1_9986;\n" + " mov.s32 %r27, %r16;\n" + " ld.param.u32 %r28, [__cudaparm_B2gCudaScanBNDMq_offsets];\n" + " mov.u32 %r29, 0;\n" + " mov.s32 %r30, %r27;\n" + "$Lt_1_10498:\n" + " // Loop body line 101, nesting depth: 1, estimated iterations: unknown\n" + " .loc 15 106 0\n" + " mov.u32 %r31, 0;\n" + " add.u32 %r32, %r10, %r29;\n" + " mul.lo.u32 %r33, %r32, 4;\n" + " add.u32 %r34, %r28, %r33;\n" + " st.global.u32 [%r34+0], %r31;\n" + " add.u32 %r29, %r29, 1;\n" + " setp.ne.u32 %p5, %r16, %r29;\n" + " @%p5 bra $Lt_1_10498;\n" + "$Lt_1_9986:\n" + " sub.u32 %r35, %r16, 1;\n" + " setp.gt.u32 %p6, %r2, %r35;\n" + " @%p6 bra $LBB23_B2gCudaScanBNDMq;\n" + " ld.param.u32 %r36, [__cudaparm_B2gCudaScanBNDMq_g_u8_lowercasetable];\n" + " ld.param.u32 %r37, [__cudaparm_B2gCudaScanBNDMq_scan_B2G];\n" + " ld.param.u32 %r38, [__cudaparm_B2gCudaScanBNDMq_buf];\n" + "$Lt_1_11522:\n" + " // Loop body line 112\n" + " .loc 15 112 0\n" + " add.u32 %r39, %r10, %r3;\n" + " add.u32 %r40, %r39, %r38;\n" + " ld.global.u8 %r41, [%r40+0];\n" + " add.u32 %r42, %r41, %r36;\n" + " ld.global.u8 %r43, [%r42+0];\n" + " ld.global.u8 %r44, [%r40+-1];\n" + " add.u32 %r45, %r44, %r36;\n" + " ld.global.u8 %r46, [%r45+0];\n" + " shl.b32 %r47, %r46, 4;\n" + " or.b32 %r48, %r43, %r47;\n" + " mul.lo.u32 %r49, %r48, 4;\n" + " add.u32 %r50, %r37, %r49;\n" + " ld.global.u32 %r51, [%r50+0];\n" + " mov.u32 %r52, 0;\n" + " setp.eq.u32 %p7, %r51, %r52;\n" + " @%p7 bra $Lt_1_258;\n" + " // Part of loop body line 112, head labeled $Lt_1_11522\n" + " .loc 15 115 0\n" + " mov.s32 %r29, %r3;\n" + " .loc 15 116 0\n" + " sub.u32 %r53, %r3, %r1;\n" + " add.u32 %r54, %r53, 1;\n" + " sub.s32 %r55, %r1, 1;\n" + "$Lt_1_12546:\n" + " // Loop body line 119\n" + " .loc 15 119 0\n" + " sub.u32 %r29, %r29, 1;\n" + " shr.u32 %r56, %r51, %r55;\n" + " mov.u32 %r57, 0;\n" + " setp.eq.u32 %p8, %r56, %r57;\n" + " @%p8 bra $Lt_1_13314;\n" + " // Part of loop body line 119, head labeled $Lt_1_12546\n" + " setp.ge.u32 %p9, %r54, %r29;\n" + " @%p9 bra $Lt_1_13570;\n" + " // Part of loop body line 119, head labeled $Lt_1_12546\n" + " .loc 15 122 0\n" + " mov.s32 %r3, %r29;\n" + " bra.uni $Lt_1_13314;\n" + "$Lt_1_13570:\n" + " // Part of loop body line 119, head labeled $Lt_1_12546\n" + " .loc 15 124 0\n" + " mov.u32 %r58, 1;\n" + " ld.param.u32 %r59, [__cudaparm_B2gCudaScanBNDMq_offsets];\n" + " add.u32 %r60, %r10, %r29;\n" + " mul.lo.u32 %r61, %r60, 4;\n" + " add.u32 %r62, %r59, %r61;\n" + " st.global.u32 [%r62+0], %r58;\n" + "$Lt_1_13314:\n" + "$Lt_1_12802:\n" + " // Part of loop body line 119, head labeled $Lt_1_12546\n" + " .loc 15 129 0\n" + " mov.u32 %r63, 0;\n" + " setp.eq.u32 %p10, %r29, %r63;\n" + " @%p10 bra $Lt_1_258;\n" + " // Part of loop body line 119, head labeled $Lt_1_12546\n" + " .loc 15 132 0\n" + " add.u32 %r64, %r10, %r29;\n" + " add.u32 %r65, %r64, %r38;\n" + " ld.global.u8 %r66, [%r65+0];\n" + " add.u32 %r67, %r66, %r36;\n" + " ld.global.u8 %r68, [%r67+0];\n" + " ld.global.u8 %r69, [%r65+-1];\n" + " add.u32 %r70, %r69, %r36;\n" + " ld.global.u8 %r71, [%r70+0];\n" + " shl.b32 %r72, %r71, 4;\n" + " or.b32 %r73, %r68, %r72;\n" + " mul.lo.u32 %r74, %r73, 4;\n" + " add.u32 %r75, %r37, %r74;\n" + " ld.global.u32 %r76, [%r75+0];\n" + " shl.b32 %r77, %r51, 1;\n" + " and.b32 %r51, %r76, %r77;\n" + " mov.u32 %r78, 0;\n" + " setp.ne.u32 %p11, %r51, %r78;\n" + " @%p11 bra $Lt_1_12546;\n" + "$Lt_1_258:\n" + "$Lt_1_11778:\n" + " // Part of loop body line 112, head labeled $Lt_1_11522\n" + " .loc 15 135 0\n" + " add.u32 %r79, %r3, %r1;\n" + " sub.u32 %r3, %r79, 1;\n" + " setp.ge.u32 %p12, %r35, %r3;\n" + " @%p12 bra $Lt_1_11522;\n" + "$LBB23_B2gCudaScanBNDMq:\n" + " .loc 15 138 0\n" + " exit;\n" + "$LDWend_B2gCudaScanBNDMq:\n" + " } // B2gCudaScanBNDMq\n" + "\n"; + +/** + * \brief Register the CUDA B2g Mpm. + */ +void MpmB2gCudaRegister(void) +{ + mpm_table[MPM_B2G_CUDA].name = "b2g_cuda"; + mpm_table[MPM_B2G_CUDA].max_pattern_length = B2G_CUDA_WORD_SIZE; + mpm_table[MPM_B2G_CUDA].InitCtx = B2gCudaInitCtx; + mpm_table[MPM_B2G_CUDA].InitThreadCtx = B2gCudaThreadInitCtx; + mpm_table[MPM_B2G_CUDA].DestroyCtx = B2gCudaDestroyCtx; + mpm_table[MPM_B2G_CUDA].DestroyThreadCtx = B2gCudaThreadDestroyCtx; + mpm_table[MPM_B2G_CUDA].AddScanPattern = B2gCudaAddScanPatternCS; + mpm_table[MPM_B2G_CUDA].AddScanPatternNocase = B2gCudaAddScanPatternCI; + mpm_table[MPM_B2G_CUDA].AddPattern = B2gCudaAddPatternCS; + mpm_table[MPM_B2G_CUDA].AddPatternNocase = B2gCudaAddPatternCI; + mpm_table[MPM_B2G_CUDA].Prepare = B2gCudaPreparePatterns; + mpm_table[MPM_B2G_CUDA].Scan = B2gCudaScanWrap; + mpm_table[MPM_B2G_CUDA].Search = B2gCudaSearchWrap; + mpm_table[MPM_B2G_CUDA].Cleanup = MpmMatchCleanup; + mpm_table[MPM_B2G_CUDA].PrintCtx = B2gCudaPrintInfo; + mpm_table[MPM_B2G_CUDA].PrintThreadCtx = B2gCudaPrintSearchStats; + mpm_table[MPM_B2G_CUDA].RegisterUnittests = B2gCudaRegisterTests; +} + +static inline void B2gCudaEndMatchAppend(MpmCtx *mpm_ctx, B2gCudaPattern *p, + uint16_t offset, uint16_t depth, + uint32_t pid, uint32_t sid, + uint8_t nosearch) +{ + MpmEndMatch *em = MpmAllocEndMatch(mpm_ctx); + if (em == NULL) { + SCLogDebug("ERROR: B2gAllocEndMatch failed"); + return; + } + + SCLogDebug("em alloced at %p", em); + + em->id = pid; + em->sig_id = sid; + em->depth = depth; + em->offset = offset; + + if (nosearch) + em->flags |= MPM_ENDMATCH_NOSEARCH; + + if (p->em == NULL) { + p->em = em; + SCLogDebug("m %p m->sig_id %"PRIu32"", em, em->sig_id); + return; + } + + MpmEndMatch *m = p->em; + while (m->next) + m = m->next; + m->next = em; + + m = p->em; + SCLogDebug("m %p m->sig_id %" PRIu32, m, m->sig_id); + while (m->next) { + m = m->next; + SCLogDebug("m %p m->sig_id %" PRIu32, m, m->sig_id); + } + + return; +} + +void B2gCudaPrintInfo(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + SCLogDebug("MPM B2g Cuda Information:"); + SCLogDebug("Memory allocs: %" PRIu32, mpm_ctx->memory_cnt); + SCLogDebug("Memory alloced: %" PRIu32, mpm_ctx->memory_size); + SCLogDebug(" Sizeofs:"); + SCLogDebug(" MpmCtx %" PRIuMAX, (uintmax_t)sizeof(MpmCtx)); + SCLogDebug(" B2gCuda %" PRIuMAX, (uintmax_t)sizeof(B2gCudaCtx)); + SCLogDebug(" B2gCudaPattern %" PRIuMAX, (uintmax_t)sizeof(B2gCudaPattern)); + SCLogDebug(" B2gCudaHashIte %" PRIuMAX, (uintmax_t)sizeof(B2gCudaHashItem)); + SCLogDebug("Unique Patterns: %" PRIu32, mpm_ctx->pattern_cnt); + SCLogDebug("Scan Patterns: %" PRIu32, mpm_ctx->scan_pattern_cnt); + SCLogDebug("Total Patterns: %" PRIu32, mpm_ctx->total_pattern_cnt); + SCLogDebug("Smallest: %" PRIu32, mpm_ctx->scan_minlen); + SCLogDebug("Largest: %" PRIu32, mpm_ctx->scan_maxlen); + SCLogDebug("Hash size: %" PRIu32, ctx->scan_hash_size); + + return; +} + +static inline B2gCudaPattern *B2gCudaAllocPattern(MpmCtx *mpm_ctx) +{ + B2gCudaPattern *p = malloc(sizeof(B2gCudaPattern)); + if (p == NULL) { + printf("ERROR: B2gAllocPattern: malloc failed\n"); + exit(EXIT_FAILURE); + } + memset(p, 0, sizeof(B2gCudaPattern)); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += sizeof(B2gCudaPattern); + + return p; +} + +static inline B2gCudaHashItem *B2gCudaAllocHashItem(MpmCtx *mpm_ctx) +{ + B2gCudaHashItem *hi = malloc(sizeof(B2gCudaHashItem)); + if (hi == NULL) { + printf("ERROR: B2gCudaAllocHashItem: malloc failed\n"); + exit(EXIT_FAILURE); + } + memset(hi, 0, sizeof(B2gCudaHashItem)); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += sizeof(B2gCudaHashItem); + + return hi; +} + +static void B2gCudaHashFree(MpmCtx *mpm_ctx, B2gCudaHashItem *hi) +{ + if (hi == NULL) + return; + + B2gCudaHashItem *t = hi->nxt; + B2gCudaHashFree(mpm_ctx, t); + + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= sizeof(B2gCudaHashItem); + free(hi); + + return; +} + +static inline void memcpy_tolower(uint8_t *d, uint8_t *s, uint16_t len) +{ + uint16_t i; + for (i = 0; i < len; i++) + d[i] = u8_tolower(s[i]); + + return; +} + +static inline uint32_t B2gCudaInitHash(B2gCudaPattern *p) +{ + uint32_t hash = p->len * p->cs[0]; + if (p->len > 1) + hash += p->cs[1]; + + return (hash % INIT_HASH_SIZE); +} + +static inline uint32_t B2gCudaInitHashRaw(uint8_t *pat, uint16_t patlen) +{ + uint32_t hash = patlen * pat[0]; + if (patlen > 1) + hash += pat[1]; + + return (hash % INIT_HASH_SIZE); +} + +static inline int B2gCudaInitHashAdd(B2gCudaCtx *ctx, B2gCudaPattern *p) +{ + uint32_t hash = B2gCudaInitHash(p); + + if (ctx->init_hash[hash] == NULL) { + ctx->init_hash[hash] = p; + return 0; + } + + B2gCudaPattern *tt = NULL; + B2gCudaPattern *t = ctx->init_hash[hash]; + + /* get the list tail */ + do { + tt = t; + t = t->next; + } while (t != NULL); + + tt->next = p; + + return 0; +} + +static inline int B2gCudaCmpPattern(B2gCudaPattern *p, uint8_t *pat, + uint16_t patlen, char nocase); + +static inline B2gCudaPattern *B2gCudaInitHashLookup(B2gCudaCtx *ctx, uint8_t *pat, + uint16_t patlen, char nocase) +{ + uint32_t hash = B2gCudaInitHashRaw(pat, patlen); + + if (ctx->init_hash[hash] == NULL) + return NULL; + + B2gCudaPattern *t = ctx->init_hash[hash]; + for ( ; t != NULL; t = t->next) { + if (B2gCudaCmpPattern(t, pat, patlen, nocase) == 1) + return t; + } + + return NULL; +} + +static inline int B2gCudaCmpPattern(B2gCudaPattern *p, uint8_t *pat, + uint16_t patlen, char nocase) +{ + if (p->len != patlen) + return 0; + + if (!((nocase && p->flags & B2G_CUDA_NOCASE) || + (!nocase && !(p->flags & B2G_CUDA_NOCASE)))) { + return 0; + } + + if (memcmp(p->cs, pat, patlen) != 0) + return 0; + + return 1; +} + +void B2gCudaFreePattern(MpmCtx *mpm_ctx, B2gCudaPattern *p) +{ + if (p && p->em) + MpmEndMatchFreeAll(mpm_ctx, p->em); + + if (p && p->cs && p->cs != p->ci) { + free(p->cs); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= p->len; + } + + if (p && p->ci) { + free(p->ci); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= p->len; + } + + if (p) { + free(p); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= sizeof(B2gCudaPattern); + } + + return; +} + +static inline int B2gCudaAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, + uint16_t patlen, uint16_t offset, + uint16_t depth, char nocase, char scan, + uint32_t pid, uint32_t sid, + uint8_t nosearch) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + SCLogDebug("ctx %p len %"PRIu16" pid %" PRIu32 ", nocase %s", + ctx, patlen, pid, nocase ? "true" : "false"); + + if (patlen == 0) + return 0; + + /* get a memory piece */ + B2gCudaPattern *p = B2gCudaInitHashLookup(ctx, pat, patlen, nocase); + if (p == NULL) { + SCLogDebug("allocing new pattern"); + + p = B2gCudaAllocPattern(mpm_ctx); + if (p == NULL) + goto error; + + p->len = patlen; + + if (nocase) + p->flags |= B2G_CUDA_NOCASE; + + /* setup the case insensitive part of the pattern */ + p->ci = malloc(patlen); + if (p->ci == NULL) + goto error; + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += patlen; + memcpy_tolower(p->ci, pat, patlen); + + /* setup the case sensitive part of the pattern */ + if (p->flags & B2G_CUDA_NOCASE) { + /* nocase means no difference between cs and ci */ + p->cs = p->ci; + } else { + if (memcmp(p->ci,pat,p->len) == 0) { + /* no diff between cs and ci: pat is lowercase */ + p->cs = p->ci; + } else { + p->cs = malloc(patlen); + if (p->cs == NULL) + goto error; + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += patlen; + memcpy(p->cs, pat, patlen); + } + } + + /* put in the pattern hash */ + B2gCudaInitHashAdd(ctx, p); + + if (mpm_ctx->pattern_cnt == 65535) { + printf("Max search words reached\n"); + exit(1); + } + if (scan) + mpm_ctx->scan_pattern_cnt++; + mpm_ctx->pattern_cnt++; + + if (scan) { /* SCAN */ + if (mpm_ctx->scan_maxlen < patlen) + mpm_ctx->scan_maxlen = patlen; + + if (mpm_ctx->scan_minlen == 0) + mpm_ctx->scan_minlen = patlen; + else if (mpm_ctx->scan_minlen > patlen) + mpm_ctx->scan_minlen = patlen; + + p->flags |= B2G_CUDA_SCAN; + } else { /* SEARCH */ + if (mpm_ctx->search_maxlen < patlen) + mpm_ctx->search_maxlen = patlen; + + if (mpm_ctx->search_minlen == 0) + mpm_ctx->search_minlen = patlen; + else if (mpm_ctx->search_minlen > patlen) + mpm_ctx->search_minlen = patlen; + } + } else { + /* if we're reusing a pattern, check we need to check that it is a + * scan pattern if that is what we're adding. If so we set the pattern + * to be a scan pattern. */ + if (scan) { + p->flags |= B2G_CUDA_SCAN; + + if (mpm_ctx->scan_maxlen < patlen) + mpm_ctx->scan_maxlen = patlen; + + if (mpm_ctx->scan_minlen == 0) + mpm_ctx->scan_minlen = patlen; + else if (mpm_ctx->scan_minlen > patlen) + mpm_ctx->scan_minlen = patlen; + } + } + + /* we need a match */ + B2gCudaEndMatchAppend(mpm_ctx, p, offset, depth, pid, sid, nosearch); + + mpm_ctx->total_pattern_cnt++; + + return 0; + +error: + B2gCudaFreePattern(mpm_ctx, p); + return -1; +} + +int B2gCudaAddScanPatternCI(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, + uint16_t offset, uint16_t depth, uint32_t pid, + uint32_t sid, uint8_t nosearch) +{ + return B2gCudaAddPattern(mpm_ctx, pat, patlen, offset, depth, /* nocase */1, + /* scan */1, pid, sid, nosearch); +} + +int B2gCudaAddScanPatternCS(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, + uint16_t offset, uint16_t depth, uint32_t pid, + uint32_t sid, uint8_t nosearch) +{ + return B2gCudaAddPattern(mpm_ctx, pat, patlen, offset, depth, /* nocase */0, + /* scan */1, pid, sid, nosearch); +} + +int B2gCudaAddPatternCI(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, + uint16_t offset, uint16_t depth, uint32_t pid, + uint32_t sid) +{ + return B2gCudaAddPattern(mpm_ctx, pat, patlen, offset, depth, /* nocase */1, + /* scan */0, pid, sid, 0); +} + +int B2gCudaAddPatternCS(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, + uint16_t offset, uint16_t depth, uint32_t pid, + uint32_t sid) +{ + return B2gCudaAddPattern(mpm_ctx, pat, patlen, offset, depth, /* nocase */0, + /* scan */0, pid, sid, 0); +} + +static inline uint32_t B2gCudaBloomHash(void *data, uint16_t datalen, uint8_t iter, + uint32_t hash_size) +{ + uint8_t *d = (uint8_t *)data; + uint16_t i; + uint32_t hash = (uint32_t)u8_tolower(*d); + + for (i = 1; i < datalen; i++) { + d++; + hash += (u8_tolower(*d)) ^ i; + } + hash <<= (iter+1); + hash %= hash_size; + + return hash; +} + +static void B2gCudaPrepareScanHash(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + uint16_t i; + uint16_t idx = 0; + uint8_t idx8 = 0; + + ctx->scan_hash = (B2gCudaHashItem **)malloc(sizeof(B2gCudaHashItem *) * + ctx->scan_hash_size); + if (ctx->scan_hash == NULL) + goto error; + memset(ctx->scan_hash, 0, sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); + +#ifdef B2G_CUDA_SCAN2 + ctx->scan_hash2 = (B2gCudaHashItem **)malloc(sizeof(B2gCudaHashItem *) * + ctx->scan_hash_size); + if (ctx->scan_hash2 == NULL) + goto error; + memset(ctx->scan_hash2, 0, sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(B2gCudaHashItem *) * ctx->scan_hash_size); +#endif + + /* alloc the pminlen array */ + ctx->scan_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->scan_hash_size); + if (ctx->scan_pminlen == NULL) + goto error; + memset(ctx->scan_pminlen, 0, sizeof(uint8_t) * ctx->scan_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(uint8_t) * ctx->scan_hash_size); + + for (i = 0; i < mpm_ctx->pattern_cnt; i++) + { + /* ignore patterns that don't have the scan flag set */ + if (!(ctx->parray[i]->flags & B2G_CUDA_SCAN)) + continue; + + if (ctx->parray[i]->len == 1) { + idx8 = (uint8_t)ctx->parray[i]->ci[0]; + if (ctx->scan_hash1[idx8].flags == 0) { + ctx->scan_hash1[idx8].idx = i; + ctx->scan_hash1[idx8].flags |= 0x01; + } else { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + + /* Append this HashItem to the list */ + B2gCudaHashItem *thi = &ctx->scan_hash1[idx8]; + while (thi->nxt) thi = thi->nxt; + thi->nxt = hi; + } + ctx->scan_1_pat_cnt++; +#ifdef B2G_CUDA_SCAN2 + } else if(ctx->parray[i]->len == 2) { + idx = B2G_CUDA_HASH16(ctx->parray[i]->ci[0], ctx->parray[i]->ci[1]); + if (ctx->scan_hash2[idx] == NULL) { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + + ctx->scan_hash2[idx] = hi; + } else { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + + /* Append this HashItem to the list */ + B2gCudaHashItem *thi = ctx->scan_hash2[idx]; + while (thi->nxt) thi = thi->nxt; + thi->nxt = hi; + } + ctx->scan_2_pat_cnt++; +#endif + } else { + idx = B2G_CUDA_HASH16(ctx->parray[i]->ci[ctx->scan_m - 2], + ctx->parray[i]->ci[ctx->scan_m - 1]); + SCLogDebug("idx %" PRIu32 ", %c.%c", idx, + ctx->parray[i]->ci[ctx->scan_m - 2], + ctx->parray[i]->ci[ctx->scan_m - 1]); + + if (ctx->scan_hash[idx] == NULL) { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + ctx->scan_pminlen[idx] = ctx->parray[i]->len; + + ctx->scan_hash[idx] = hi; + } else { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + + if (ctx->parray[i]->len < ctx->scan_pminlen[idx]) + ctx->scan_pminlen[idx] = ctx->parray[i]->len; + + /* Append this HashItem to the list */ + B2gCudaHashItem *thi = ctx->scan_hash[idx]; + while (thi->nxt) thi = thi->nxt; + thi->nxt = hi; + } + ctx->scan_x_pat_cnt++; + } + } + + /* alloc the bloom array */ + ctx->scan_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->scan_hash_size); + if (ctx->scan_bloom == NULL) goto error; + memset(ctx->scan_bloom, 0, sizeof(BloomFilter *) * ctx->scan_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(BloomFilter *) * ctx->scan_hash_size); + + uint32_t h; + for (h = 0; h < ctx->scan_hash_size; h++) { + B2gCudaHashItem *hi = ctx->scan_hash[h]; + if (hi == NULL) + continue; + + ctx->scan_bloom[h] = BloomFilterInit(B2G_CUDA_BLOOMSIZE, 2, + B2gCudaBloomHash); + if (ctx->scan_bloom[h] == NULL) + continue; + + mpm_ctx->memory_cnt += BloomFilterMemoryCnt(ctx->scan_bloom[h]); + mpm_ctx->memory_size += BloomFilterMemorySize(ctx->scan_bloom[h]); + + if (ctx->scan_pminlen[h] > 8) + ctx->scan_pminlen[h] = 8; + + B2gCudaHashItem *thi = hi; + do { + SCLogDebug("adding \"%c%c\" to the bloom", + ctx->parray[thi->idx]->ci[0], + ctx->parray[thi->idx]->ci[1]); + BloomFilterAdd(ctx->scan_bloom[h], ctx->parray[thi->idx]->ci, + ctx->scan_pminlen[h]); + thi = thi->nxt; + } while (thi != NULL); + } + + return; +error: + return; +} + +static void B2gCudaPrepareSearchHash(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + uint16_t i; + uint16_t idx = 0; + uint8_t idx8 = 0; + + ctx->search_hash = (B2gCudaHashItem **)malloc(sizeof(B2gCudaHashItem *) * + ctx->search_hash_size); + if (ctx->search_hash == NULL) goto error; + memset(ctx->search_hash, 0, sizeof(B2gCudaHashItem *) * ctx->search_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(B2gCudaHashItem *) * ctx->search_hash_size); + + /* alloc the pminlen array */ + ctx->search_pminlen = (uint8_t *)malloc(sizeof(uint8_t) * ctx->search_hash_size); + if (ctx->search_pminlen == NULL) + goto error; + memset(ctx->search_pminlen, 0, sizeof(uint8_t) * ctx->search_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(uint8_t) * ctx->search_hash_size); + + for (i = 0; i < mpm_ctx->pattern_cnt; i++) + { + /* ignore patterns that have the scan flag set */ + if (ctx->parray[i]->flags & B2G_CUDA_SCAN) + continue; + + if(ctx->parray[i]->len == 1) { + idx8 = (uint8_t)ctx->parray[i]->ci[0]; + if (ctx->search_hash1[idx8].flags == 0) { + ctx->search_hash1[idx8].idx = i; + ctx->search_hash1[idx8].flags |= 0x01; + } else { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + + /* Append this HashItem to the list */ + B2gCudaHashItem *thi = &ctx->search_hash1[idx8]; + while (thi->nxt) + thi = thi->nxt; + thi->nxt = hi; + } + } else { + idx = B2G_CUDA_HASH16(ctx->parray[i]->ci[ctx->search_m - 2], + ctx->parray[i]->ci[ctx->search_m - 1]); + + if (ctx->search_hash[idx] == NULL) { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + ctx->search_pminlen[idx] = ctx->parray[i]->len; + + ctx->search_hash[idx] = hi; + } else { + B2gCudaHashItem *hi = B2gCudaAllocHashItem(mpm_ctx); + hi->idx = i; + hi->flags |= 0x01; + + if (ctx->parray[i]->len < ctx->search_pminlen[idx]) + ctx->search_pminlen[idx] = ctx->parray[i]->len; + + /* Append this HashItem to the list */ + B2gCudaHashItem *thi = ctx->search_hash[idx]; + while (thi->nxt) + thi = thi->nxt; + thi->nxt = hi; + } + } + } + + /* alloc the bloom array */ + ctx->search_bloom = (BloomFilter **)malloc(sizeof(BloomFilter *) * ctx->search_hash_size); + if (ctx->search_bloom == NULL) + goto error; + memset(ctx->search_bloom, 0, sizeof(BloomFilter *) * ctx->search_hash_size); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(BloomFilter *) * ctx->search_hash_size); + + uint32_t h; + for (h = 0; h < ctx->search_hash_size; h++) { + B2gCudaHashItem *hi = ctx->search_hash[h]; + if (hi == NULL) + continue; + + ctx->search_bloom[h] = BloomFilterInit(B2G_CUDA_BLOOMSIZE, 2, B2gCudaBloomHash); + if (ctx->search_bloom[h] == NULL) + continue; + + mpm_ctx->memory_cnt += BloomFilterMemoryCnt(ctx->search_bloom[h]); + mpm_ctx->memory_size += BloomFilterMemorySize(ctx->search_bloom[h]); + + if (ctx->search_pminlen[h] > 8) + ctx->search_pminlen[h] = 8; + + B2gCudaHashItem *thi = hi; + do { + BloomFilterAdd(ctx->search_bloom[h], ctx->parray[thi->idx]->ci, ctx->search_pminlen[h]); + thi = thi->nxt; + } while (thi != NULL); + } + return; + +error: + return; +} + +int B2gCudaBuildScanMatchArray(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + ctx->scan_B2G = malloc(sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size); + if (ctx->scan_B2G == NULL) + return -1; + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size); + + memset(ctx->scan_B2G,0, B2G_CUDA_HASHSIZE * sizeof(B2G_CUDA_TYPE)); + + uint32_t j; + uint32_t a; + + /* fill the match array */ + for (j = 0; j <= (ctx->scan_m - B2G_CUDA_Q); j++) { + for (a = 0; a < mpm_ctx->pattern_cnt; a++) { + if (!(ctx->parray[a]->flags & B2G_CUDA_SCAN)) + continue; + + if (ctx->parray[a]->len < ctx->scan_m) + continue; + + uint16_t h = B2G_CUDA_HASH16(u8_tolower(ctx->parray[a]->ci[j]), + u8_tolower(ctx->parray[a]->ci[j+1])); + ctx->scan_B2G[h] = ctx->scan_B2G[h] | (1 << (ctx->scan_m - j)); + + SCLogDebug("h %"PRIu16", ctx->scan_B2G[h] %" PRIu32 "", h, + ctx->scan_B2G[h]); + } + } + + ctx->scan_s0 = 1; + + return 0; +} + +int B2gCudaBuildSearchMatchArray(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + ctx->search_B2G = malloc(sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size); + if (ctx->search_B2G == NULL) + return -1; + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size); + + memset(ctx->search_B2G,0, B2G_CUDA_HASHSIZE * sizeof(B2G_CUDA_TYPE)); + + uint32_t j; + uint32_t a; + + /* fill the match array */ + for (j = 0; j <= (ctx->search_m - B2G_CUDA_Q); j++) { + for (a = 0; a < mpm_ctx->pattern_cnt; a++) { + if (ctx->parray[a]->flags & B2G_CUDA_SCAN) + continue; + + if (ctx->parray[a]->len < ctx->search_m) + continue; + + uint16_t h = B2G_CUDA_HASH16(u8_tolower(ctx->parray[a]->ci[j]), + u8_tolower(ctx->parray[a]->ci[j+1])); + + ctx->search_B2G[h] = ctx->search_B2G[h] | (1 << (ctx->search_m - j)); + } + } + + return 0; +} + +int B2gCudaSetDeviceBuffers(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + if (SCCudaHlGetCudaDevicePtr(&ctx->cuda_g_u8_lowercasetable, + "G_U8_LOWERCASETABLE", 256 * sizeof(char), + g_u8_lowercasetable, ctx->module_handle) == -1) { + goto error; + } + + /* search kernel */ + if (SCCudaMemAlloc(&ctx->cuda_search_B2G, + sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size) == -1) { + goto error; + } + if (SCCudaMemcpyHtoD(ctx->cuda_search_B2G, ctx->search_B2G, + sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size) == -1) { + goto error; + } + + /* scan kernel */ + if (SCCudaMemAlloc(&ctx->cuda_scan_B2G, + sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size) == -1) { + goto error; + } + if (SCCudaMemcpyHtoD(ctx->cuda_scan_B2G, ctx->scan_B2G, + sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size) == -1) { + goto error; + } + + return 0; + + error: + return -1; +} + +int B2gCudaSetKernelArgs(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + /* search kernel */ + if (SCCudaParamSetv(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_ARG2_OFFSET, + (void *)&ctx->cuda_g_u8_lowercasetable, + sizeof(void *)) == -1) { + goto error; + } + + /* scan kernel */ + if (SCCudaParamSetv(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG1_OFFSET, + (void *)&ctx->cuda_scan_B2G, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG2_OFFSET, + (void *)&ctx->cuda_g_u8_lowercasetable, + sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSeti(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG5_OFFSET, + ctx->scan_m) == -1) { + goto error; + } + + return 0; + + error: + return -1; +} + +int B2gCudaPreparePatterns(MpmCtx *mpm_ctx) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + + /* alloc the pattern array */ + ctx->parray = (B2gCudaPattern **)malloc(mpm_ctx->pattern_cnt * + sizeof(B2gCudaPattern *)); + if (ctx->parray == NULL) + goto error; + memset(ctx->parray, 0, mpm_ctx->pattern_cnt * sizeof(B2gCudaPattern *)); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += (mpm_ctx->pattern_cnt * sizeof(B2gCudaPattern *)); + + /* populate it with the patterns in the hash */ + uint32_t i = 0, p = 0; + for (i = 0; i < INIT_HASH_SIZE; i++) { + B2gCudaPattern *node = ctx->init_hash[i], *nnode = NULL; + for ( ; node != NULL; ) { + nnode = node->next; + node->next = NULL; + + ctx->parray[p] = node; + + p++; + node = nnode; + } + } + /* we no longer need the hash, so free it's memory */ + free(ctx->init_hash); + ctx->init_hash = NULL; + + /* set 'm' to the smallest pattern size */ + ctx->scan_m = mpm_ctx->scan_minlen; + ctx->search_m = mpm_ctx->search_minlen; + + if (mpm_ctx->search_minlen == 1) { + ctx->Search = B2gCudaSearch1; + ctx->MBSearch = B2G_CUDA_SEARCHFUNC; + } + /* make sure 'm' stays in bounds + m can be max WORD_SIZE - 1 */ + if (ctx->scan_m >= B2G_CUDA_WORD_SIZE) { + ctx->scan_m = B2G_CUDA_WORD_SIZE - 1; + } + if (ctx->scan_m < 2) + ctx->scan_m = 2; + + if (ctx->search_m >= B2G_CUDA_WORD_SIZE) { + ctx->search_m = B2G_CUDA_WORD_SIZE - 1; + } + if (ctx->search_m < 2) + ctx->search_m = 2; + + ctx->scan_hash_size = B2G_CUDA_HASHSIZE; + ctx->search_hash_size = B2G_CUDA_HASHSIZE; + B2gCudaPrepareScanHash(mpm_ctx); + B2gCudaPrepareSearchHash(mpm_ctx); + B2gCudaBuildScanMatchArray(mpm_ctx); + B2gCudaBuildSearchMatchArray(mpm_ctx); + + if (B2gCudaSetDeviceBuffers(mpm_ctx) == -1) + goto error; + + if (B2gCudaSetKernelArgs(mpm_ctx) == -1) + goto error; + + SCLogDebug("ctx->scan_1_pat_cnt %"PRIu16"", ctx->scan_1_pat_cnt); + if (ctx->scan_1_pat_cnt) { + ctx->Scan = B2gCudaScan1; +#ifdef B2G_CUDA_SCAN2 + ctx->Scan = B2gCudaScan2; + if (ctx->scan_2_pat_cnt) { + ctx->MBScan2 = B2gCudaScan2; + } +#endif + ctx->MBScan = B2G_CUDA_SCANFUNC; +#ifdef B2G_SCAN2 + } else if (ctx->scan_2_pat_cnt) { + ctx->Scan = B2gCudaScan2; + ctx->MBScan = B2G_CUDA_SCANFUNC; +#endif + } + + return 0; + +error: + return -1; +} + +void B2gCudaPrintSearchStats(MpmThreadCtx *mpm_thread_ctx) +{ + +#ifdef B2G_CUDA_COUNTERS + B2gCudaThreadCtx *tctx = (B2gCudaThreadCtx *)mpm_thread_ctx->ctx; + + printf("B2gCuda Thread Search stats (tctx %p)\n", tctx); + printf("Scan phase:\n"); + printf("Total calls/scans: %" PRIu32 "\n", tctx->scan_stat_calls); + printf("Avg m/scan: %0.2f\n", (tctx->scan_stat_calls ? + ((float)tctx->scan_stat_m_total / + (float)tctx->scan_stat_calls)) : 0); + printf("D != 0 (possible match): %" PRIu32 "\n", tctx->scan_stat_d0); + printf("Avg hash items per bucket %0.2f (%" PRIu32 ")\n", + (tctx->scan_stat_d0 ? ((float)tctx->scan_stat_d0_hashloop / + (float)tctx->scan_stat_d0)) : 0, + tctx->scan_stat_d0_hashloop); + printf("Loop match: %" PRIu32 "\n", tctx->scan_stat_loop_match); + printf("Loop no match: %" PRIu32 "\n", tctx->scan_stat_loop_no_match); + printf("Num shifts: %" PRIu32 "\n", tctx->scan_stat_num_shift); + printf("Total shifts: %" PRIu32 "\n", tctx->scan_stat_total_shift); + printf("Avg shifts: %0.2f\n", (tctx->scan_stat_num_shift ? + ((float)tctx->scan_stat_total_shift / + (float)tctx->scan_stat_num_shift)) : 0); + printf("Total BloomFilter checks: %" PRIu32 "\n", tctx->scan_stat_bloom_calls); + printf("BloomFilter hits: %0.4f%% (%" PRIu32 ")\n", + (tctx->scan_stat_bloom_calls ? + ((float)tctx->scan_stat_bloom_hits / + (float)tctx->scan_stat_bloom_calls) * (float)100) : 0, + tctx->scan_stat_bloom_hits); + printf("Avg pminlen: %0.2f\n\n", (tctx->scan_stat_pminlen_calls ? + ((float)tctx->scan_stat_pminlen_total / + (float)tctx->scan_stat_pminlen_calls)) : 0); + printf("Search phase:\n"); + printf("D 0 (possible match, shift = 1): %" PRIu32 "\n", tctx->search_stat_d0); + printf("Loop match: %" PRIu32 "\n", tctx->search_stat_loop_match); + printf("Loop no match: %" PRIu32 "\n", tctx->search_stat_loop_no_match); + printf("Num shifts: %" PRIu32 "\n", tctx->search_stat_num_shift); + printf("Total shifts: %" PRIu32 "\n", tctx->search_stat_total_shift); + printf("Avg shifts: %0.2f\n\n", (tctx->search_stat_num_shift ? + ((float)tctx->search_stat_total_shift / + (float)tctx->search_stat_num_shift)) : 0); +#endif /* B2G_CUDA_COUNTERS */ + + return; +} + +static inline int memcmp_lowercase(uint8_t *s1, uint8_t *s2, uint16_t n) +{ + size_t i; + + /* check backwards because we already tested the first + * 2 to 4 chars. This way we are more likely to detect + * a miss and thus speed up a little... */ + for (i = n - 1; i; i--) { + if (u8_tolower(*(s2+i)) != s1[i]) + return 1; + } + + return 0; +} + +void B2gCudaInitCtx(MpmCtx *mpm_ctx, int module_handle) +{ + SCLogDebug("mpm_ctx %p, ctx %p", mpm_ctx, mpm_ctx->ctx); + + BUG_ON(mpm_ctx->ctx != NULL); + + mpm_ctx->ctx = malloc(sizeof(B2gCudaCtx)); + if (mpm_ctx->ctx == NULL) + return; + + memset(mpm_ctx->ctx, 0, sizeof(B2gCudaCtx)); + + mpm_ctx->memory_cnt++; + mpm_ctx->memory_size += sizeof(B2gCudaCtx); + + /* initialize the hash we use to speed up pattern insertions */ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + ctx->init_hash = malloc(sizeof(B2gCudaPattern *) * INIT_HASH_SIZE); + if (ctx->init_hash == NULL) + return; + + memset(ctx->init_hash, 0, sizeof(B2gCudaPattern *) * INIT_HASH_SIZE); + + /* init defaults */ + ctx->Scan = B2G_CUDA_SCANFUNC; + ctx->Search = B2G_CUDA_SEARCHFUNC; + + ctx->module_handle = module_handle; + + if (SCCudaHlGetCudaContext(&ctx->cuda_context, module_handle) == -1) { + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error getting a cuda context"); + } + + if (SCCudaHlGetCudaModule(&ctx->cuda_module, b2g_cuda_ptx_image, + module_handle) == -1) { + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error getting a cuda module"); + } + + if (SCCudaModuleGetFunction(&ctx->cuda_search_kernel, ctx->cuda_module, + B2G_CUDA_SEARCHFUNC_NAME) == -1) { + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error getting a cuda function"); + } + + if (SCCudaModuleGetFunction(&ctx->cuda_scan_kernel, ctx->cuda_module, + B2G_CUDA_SCANFUNC_NAME) == -1) { + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error getting a cuda function"); + } + + /* we will need this for debugging purposes. keep it here now */ +//#define ALIGN_UP(offset, alignment) +// (offset) = ((offset) + (alignment) - 1) & ~((alignment) - 1) +// +// int offset = 0; +// +// ALIGN_UP(offset, __alignof(void *)); +// arg0 = offset; +// offset += sizeof(void *); +// +// ALIGN_UP(offset, __alignof(void *)); +// arg1 = offset; +// offset += sizeof(void *); +// +// ALIGN_UP(offset, __alignof(void *)); +// arg2 = offset; +// offset += sizeof(void *); +// +// ALIGN_UP(offset, __alignof(void *)); +// arg3 = offset; +// offset += sizeof(void *); +// +// ALIGN_UP(offset, __alignof(unsigned short)); +// arg4 = offset; +// offset += sizeof(unsigned short); +// +// ALIGN_UP(offset, __alignof(unsigned int)); +// arg5 = offset; +// offset += sizeof(unsigned int); +// +// printf("arg0: %d\n", arg0); +// printf("arg1: %d\n", arg1); +// printf("arg2: %d\n", arg2); +// printf("arg3: %d\n", arg3); +// printf("arg4: %d\n", arg4); +// printf("arg5: %d\n", arg5); +// +// arg_total = offset; +// +// printf("arg_total: %d\n", arg_total); + + return; +} + +void B2gCudaDestroyCtx(MpmCtx *mpm_ctx) +{ + SCLogDebug("mpm_ctx %p", mpm_ctx); + + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + if (ctx == NULL) + return; + + if (ctx->init_hash) { + free(ctx->init_hash); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (INIT_HASH_SIZE * sizeof(B2gCudaPattern *)); + } + + if (ctx->parray) { + uint32_t i; + for (i = 0; i < mpm_ctx->pattern_cnt; i++) { + if (ctx->parray[i] != NULL) { + B2gCudaFreePattern(mpm_ctx, ctx->parray[i]); + } + } + + free(ctx->parray); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (mpm_ctx->pattern_cnt * sizeof(B2gCudaPattern)); + } + + if (ctx->scan_B2G) { + free(ctx->scan_B2G); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(B2G_CUDA_TYPE) * ctx->scan_hash_size); + } + + if (ctx->search_B2G) { + free(ctx->search_B2G); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(B2G_CUDA_TYPE) * ctx->search_hash_size); + } + + if (ctx->scan_bloom) { + uint32_t h; + for (h = 0; h < ctx->scan_hash_size; h++) { + if (ctx->scan_bloom[h] == NULL) + continue; + + mpm_ctx->memory_cnt -= BloomFilterMemoryCnt(ctx->scan_bloom[h]); + mpm_ctx->memory_size -= BloomFilterMemorySize(ctx->scan_bloom[h]); + + BloomFilterFree(ctx->scan_bloom[h]); + } + + free(ctx->scan_bloom); + + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->scan_hash_size); + } + + if (ctx->scan_hash) { + uint32_t h; + for (h = 0; h < ctx->scan_hash_size; h++) { + if (ctx->scan_hash[h] == NULL) + continue; + + B2gCudaHashFree(mpm_ctx, ctx->scan_hash[h]); + } + + free(ctx->scan_hash); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(B2gCudaHashItem) * ctx->scan_hash_size); + } + + if (ctx->search_bloom) { + uint32_t h; + for (h = 0; h < ctx->search_hash_size; h++) { + if (ctx->search_bloom[h] == NULL) + continue; + + mpm_ctx->memory_cnt -= BloomFilterMemoryCnt(ctx->search_bloom[h]); + mpm_ctx->memory_size -= BloomFilterMemorySize(ctx->search_bloom[h]); + + BloomFilterFree(ctx->search_bloom[h]); + } + + free(ctx->search_bloom); + + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(BloomFilter *) * ctx->search_hash_size); + } + + if (ctx->search_hash) { + uint32_t h; + for (h = 0; h < ctx->search_hash_size; h++) { + if (ctx->search_hash[h] == NULL) + continue; + + B2gCudaHashFree(mpm_ctx, ctx->search_hash[h]); + } + + free(ctx->search_hash); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(B2gCudaHashItem) * ctx->search_hash_size); + } + + if (ctx->scan_pminlen) { + free(ctx->scan_pminlen); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->scan_hash_size); + } + + if (ctx->search_pminlen) { + free(ctx->search_pminlen); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= (sizeof(uint8_t) * ctx->search_hash_size); + } + + if (ctx->cuda_search_B2G != 0) { + if (SCCudaMemFree(ctx->cuda_search_B2G) == -1) + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error freeing ctx->cuda_search_B2G "); + ctx->cuda_search_B2G = 0; + } + + free(mpm_ctx->ctx); + mpm_ctx->memory_cnt--; + mpm_ctx->memory_size -= sizeof(B2gCudaCtx); + + return; +} + +void B2gCudaThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + uint32_t matchsize) +{ + memset(mpm_thread_ctx, 0, sizeof(MpmThreadCtx)); + + /* size can be null when optimized */ + if (sizeof(B2gCudaThreadCtx) > 0) { + mpm_thread_ctx->ctx = malloc(sizeof(B2gCudaThreadCtx)); + if (mpm_thread_ctx->ctx == NULL) + return; + + memset(mpm_thread_ctx->ctx, 0, sizeof(B2gCudaThreadCtx)); + + mpm_thread_ctx->memory_cnt++; + mpm_thread_ctx->memory_size += sizeof(B2gCudaThreadCtx); + } + + /* alloc an array with the size of _all_ keys in all instances. + * this is done so the detect engine won't have to care about + * what instance it's looking up in. The matches all have a + * unique id and is the array lookup key at the same time */ + uint32_t keys = matchsize + 1; + if (keys > 0) { + mpm_thread_ctx->match = malloc(keys * sizeof(MpmMatchBucket)); + if (mpm_thread_ctx->match == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Could not setup memory for " + "pattern matcher: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + memset(mpm_thread_ctx->match, 0, keys * sizeof(MpmMatchBucket)); + + mpm_thread_ctx->memory_cnt++; + mpm_thread_ctx->memory_size += (keys * sizeof(MpmMatchBucket)); + } + + mpm_thread_ctx->matchsize = matchsize; + + return; +} + +void B2gCudaThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx) +{ + B2gCudaThreadCtx *ctx = (B2gCudaThreadCtx *)mpm_thread_ctx->ctx; + + B2gCudaPrintSearchStats(mpm_thread_ctx); + + /* can be NULL if B2gCudaThreadCtx is optimized to 0 */ + if (ctx != NULL) { + mpm_thread_ctx->memory_cnt--; + mpm_thread_ctx->memory_size -= sizeof(B2gCudaThreadCtx); + free(mpm_thread_ctx->ctx); + } + + if (mpm_thread_ctx->match != NULL) { + mpm_thread_ctx->memory_cnt--; + mpm_thread_ctx->memory_size -= ((mpm_thread_ctx->matchsize + 1) * + sizeof(MpmMatchBucket)); + free(mpm_thread_ctx->match); + } + + MpmMatchFreeSpares(mpm_thread_ctx, mpm_thread_ctx->sparelist); + MpmMatchFreeSpares(mpm_thread_ctx, mpm_thread_ctx->qlist); + + return; +} + +inline uint32_t B2gCudaScanWrap(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, + uint16_t buflen) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + return ctx ? ctx->Scan(mpm_ctx, mpm_thread_ctx, pmq, buf, buflen) : 0; +} + +inline uint32_t B2gCudaSearchWrap(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, + uint16_t buflen) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + return ctx ? ctx->Search(mpm_ctx, mpm_thread_ctx, pmq, buf, buflen) : 0; +} + +uint32_t B2gCudaScanBNDMq(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, + uint16_t buflen) +{ +#define CUDA_THREADS 16 + CUdeviceptr cuda_buf = 0; + CUdeviceptr cuda_offsets = 0; + uint32_t matches = 0; + B2gCudaCtx *ctx = mpm_ctx->ctx; + uint16_t h = 0; + int i = 0; + int host_offsets[UINT16_MAX]; + + if (buflen < ctx->search_m) + return 0; + + if (SCCudaMemAlloc(&cuda_buf, buflen * sizeof(char)) == -1) { + goto error; + } + if (SCCudaMemcpyHtoD(cuda_buf, buf, + buflen * sizeof(char)) == -1) { + goto error; + } + + if (SCCudaMemAlloc(&cuda_offsets, buflen * sizeof(int)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG0_OFFSET, + (void *)&cuda_offsets, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG1_OFFSET, + (void *)&ctx->cuda_scan_B2G, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG3_OFFSET, + (void *)&cuda_buf, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSeti(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG4_OFFSET, + buflen) == -1) { + goto error; + } + + if (SCCudaParamSeti(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_ARG5_OFFSET, + ctx->scan_m) == -1) { + goto error; + } + + if (SCCudaParamSetSize(ctx->cuda_scan_kernel, B2G_CUDA_KERNEL_TOTAL_ARG_SIZE) == -1) + goto error; + + if (SCCudaFuncSetBlockShape(ctx->cuda_scan_kernel, CUDA_THREADS, 1, 1) == -1) + goto error; + + if (SCCudaLaunchGrid(ctx->cuda_scan_kernel, 1, 1) == -1) + goto error; + + if (SCCudaMemcpyDtoH(host_offsets, cuda_offsets, buflen * sizeof(int)) == -1) + goto error; + + //printf("Raw matches: "); + //for (i = 0; i < buflen; i++) { + // printf("%d",offsets_buffer[i]); + //} + //printf("\n"); + + //printf("Scan Matches: "); + for (i = 0; i < buflen; i++) { + if (host_offsets[i] == 0) + continue; + //printf("%d ", i); + + /* get our patterns from the hash */ + h = B2G_CUDA_HASH16(u8_tolower(buf[i + ctx->scan_m - 2]), + u8_tolower(buf[i + ctx->scan_m - 1])); + + if (ctx->scan_bloom[h] != NULL) { + COUNT(tctx->scan_stat_pminlen_calls++); + COUNT(tctx->scan_stat_pminlen_total+=ctx->scan_pminlen[h]); + + if ((buflen - i) < ctx->scan_pminlen[h]) { + continue; + } else { + COUNT(tctx->scan_stat_bloom_calls++); + + if (BloomFilterTest(ctx->scan_bloom[h], buf+i, + ctx->scan_pminlen[h]) == 0) { + COUNT(tctx->scan_stat_bloom_hits++); + + continue; + } + } + } + + B2gCudaHashItem *hi = ctx->scan_hash[h], *thi; + for (thi = hi; thi != NULL; thi = thi->nxt) { + COUNT(tctx->scan_stat_d0_hashloop++); + B2gCudaPattern *p = ctx->parray[thi->idx]; + + if (p->flags & B2G_CUDA_NOCASE) { + if ((buflen - i) < p->len) + continue; + + if (memcmp_lowercase(p->ci, buf+i, p->len) == 0) { + COUNT(tctx->scan_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + SCLogDebug("em %p id %" PRIu32 "", em, em->id); + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + i, p->len)) + matches++; + } + } else { + COUNT(tctx->scan_stat_loop_no_match++); + } + } else { + if (buflen - i < p->len) + continue; + + if (memcmp(p->cs, buf+i, p->len) == 0) { + COUNT(tctx->scan_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + SCLogDebug("em %p pid %" PRIu32 ", sid " + "%"PRIu32"", em, em->id, em->sig_id); + if (MpmMatchAppend(mpm_thread_ctx, pmq, + em, + &mpm_thread_ctx->match[em->id], + i, p->len)) + matches++; + } + } else { + COUNT(tctx->scan_stat_loop_no_match++); + } + } + } + } /* for(i = 0; i < buflen; i++) */ + + SCCudaMemFree(cuda_buf); + SCCudaMemFree(cuda_offsets); + + return matches; + + error: + if (cuda_buf != 0) + SCCudaMemFree(cuda_buf); + if (cuda_offsets != 0) + SCCudaMemFree(cuda_offsets); + return 0; +} + +uint32_t B2gCudaScan(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; +#ifdef B2G_CUDA_COUNTERS + B2gCudaThreadCtx *tctx = (B2gCudaThreadCtx *)mpm_thread_ctx->ctx; +#endif + uint32_t pos = 0, matches = 0; + B2G_CUDA_TYPE d; + uint32_t j; + + COUNT(tctx->scan_stat_calls++); + COUNT(tctx->scan_stat_m_total+=ctx->scan_m); + + if (buflen < ctx->scan_m) + return 0; + + while (pos <= (buflen - ctx->scan_m)) { + j = ctx->scan_m - 1; + d = ~0; + + do { + uint16_t h = B2G_CUDA_HASH16(u8_tolower(buf[pos + j - 1]), + u8_tolower(buf[pos + j])); + d = ((d << 1) & ctx->scan_B2G[h]); + j = j - 1; + } while (d != 0 && j != 0); + + /* (partial) match, move on to verification */ + if (d != 0) { + COUNT(tctx->scan_stat_d0++); + + /* get our patterns from the hash */ + uint16_t h = B2G_CUDA_HASH16(u8_tolower(buf[pos + ctx->scan_m - 2]), + u8_tolower(buf[pos + ctx->scan_m - 1])); + + if (ctx->scan_bloom[h] != NULL) { + COUNT(tctx->scan_stat_pminlen_calls++); + COUNT(tctx->scan_stat_pminlen_total+=ctx->scan_pminlen[h]); + + if ((buflen - pos) < ctx->scan_pminlen[h]) { + goto skip_loop; + } else { + COUNT(tctx->scan_stat_bloom_calls++); + + if (BloomFilterTest(ctx->scan_bloom[h], buf+pos, + ctx->scan_pminlen[h]) == 0) { + COUNT(tctx->scan_stat_bloom_hits++); + + goto skip_loop; + } + } + } + + B2gCudaHashItem *hi = ctx->scan_hash[h], *thi; + for (thi = hi; thi != NULL; thi = thi->nxt) { + COUNT(tctx->scan_stat_d0_hashloop++); + B2gCudaPattern *p = ctx->parray[thi->idx]; + + if (p->flags & B2G_CUDA_NOCASE) { + if (buflen - pos < p->len) + continue; + + if (memcmp_lowercase(p->ci, buf+pos, p->len) == 0) { + COUNT(tctx->scan_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + pos, p->len)) + matches++; + } + } else { + COUNT(tctx->scan_stat_loop_no_match++); + } + } else { + if (buflen - pos < p->len) + continue; + + if (memcmp(p->cs, buf+pos, p->len) == 0) { + COUNT(tctx->scan_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + pos, p->len)) + matches++; + } + } else { + COUNT(tctx->scan_stat_loop_no_match++); + } + } + } +skip_loop: + pos = pos + 1; + } else { + COUNT(tctx->scan_stat_num_shift++); + COUNT(tctx->scan_stat_total_shift += (j + 1)); + + pos = pos + j + 1; + } + } + + return matches; +} + +#ifdef B2G_CUDA_SCAN2 +uint32_t B2gCudaScan2(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + uint8_t *bufmin = buf; + uint8_t *bufend = buf + buflen - 1; + uint32_t cnt = 0; + B2gCudaPattern *p; + MpmEndMatch *em; + B2gCudaHashItem *thi, *hi; + + if (buflen < 2) + return 0; + + while (buf <= bufend) { + uint8_t h8 = u8_tolower(*buf); + hi = &ctx->scan_hash1[h8]; + + if (hi->flags & 0x01) { + for (thi = hi; thi != NULL; thi = thi->nxt) { + p = ctx->parray[thi->idx]; + + if (p->flags & B2G_CUDA_NOCASE) { + if (h8 == p->ci[0]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } else { + if (*buf == p->cs[0]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } + } + } + + /* save one conversion by reusing h8 */ + uint16_t h16 = B2G_CUDA_HASH16(h8, u8_tolower(*(buf+1))); + hi = ctx->scan_hash2[h16]; + + for (thi = hi; thi != NULL; thi = thi->nxt) { + p = ctx->parray[thi->idx]; + + if (p->flags & B2G_CUDA_NOCASE) { + if (h8 == p->ci[0] && u8_tolower(*(buf+1)) == p->ci[1]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } else { + if (*buf == p->cs[0] && *(buf+1) == p->cs[1]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } + } + buf += 1; + } + + if (ctx->scan_x_pat_cnt > 0) { + /* Pass bufmin on because buf no longer points to the + * start of the buffer. */ + cnt += ctx->MBScan(mpm_ctx, mpm_thread_ctx, pmq, bufmin, buflen); + } + + return cnt; +} +#endif + +uint32_t B2gCudaScan1(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen) +{ + SCEnter(); + + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + uint8_t *bufmin = buf; + uint8_t *bufend = buf + buflen - 1; + uint32_t cnt = 0; + B2gCudaPattern *p; + MpmEndMatch *em; + B2gCudaHashItem *thi, *hi; + + if (buflen == 0) + SCReturnUInt(0); + + while (buf <= bufend) { + uint8_t h = u8_tolower(*buf); + hi = &ctx->scan_hash1[h]; + + if (hi->flags & 0x01) { + for (thi = hi; thi != NULL; thi = thi->nxt) { + p = ctx->parray[thi->idx]; + + if (p->len != 1) + continue; + + if (p->flags & B2G_CUDA_NOCASE) { + if (u8_tolower(*buf) == p->ci[0]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } else { + if (*buf == p->cs[0]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } + } + } + buf += 1; + } + +#ifdef B2G_CUDA_SCAN2 + if (ctx->scan_2_pat_cnt) { + /* Pass bufmin on because buf no longer points to the + * start of the buffer. */ + cnt += ctx->MBScan2(mpm_ctx, mpm_thread_ctx, pmq, bufmin, buflen); + } else +#endif + if (ctx->scan_x_pat_cnt) { + cnt += ctx->MBScan(mpm_ctx, mpm_thread_ctx, pmq, bufmin, buflen); + } + + SCReturnUInt(cnt); +} + +uint32_t B2gCudaSearchBNDMq(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, + uint16_t buflen) +{ +#define CUDA_THREADS 16 + CUdeviceptr cuda_buf = 0; + CUdeviceptr cuda_offsets = 0; + uint32_t matches = 0; + B2gCudaCtx *ctx = mpm_ctx->ctx; + uint16_t h = 0; + int i = 0; + int host_offsets[UINT16_MAX]; + + if (buflen < ctx->search_m) + return 0; + + if (SCCudaMemAlloc(&cuda_buf, buflen * sizeof(char)) == -1) { + goto error; + } + if (SCCudaMemcpyHtoD(cuda_buf, buf, + buflen * sizeof(char)) == -1) { + goto error; + } + + if (SCCudaMemAlloc(&cuda_offsets, buflen * sizeof(int)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_ARG0_OFFSET, + (void *)&cuda_offsets, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_ARG1_OFFSET, + (void *)&ctx->cuda_search_B2G, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSetv(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_ARG3_OFFSET, + (void *)&cuda_buf, sizeof(void *)) == -1) { + goto error; + } + + if (SCCudaParamSeti(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_ARG4_OFFSET, + buflen) == -1) { + goto error; + } + + if (SCCudaParamSeti(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_ARG5_OFFSET, + ctx->search_m) == -1) { + goto error; + } + + if (SCCudaParamSetSize(ctx->cuda_search_kernel, B2G_CUDA_KERNEL_TOTAL_ARG_SIZE) == -1) + goto error; + + if (SCCudaFuncSetBlockShape(ctx->cuda_search_kernel, CUDA_THREADS, 1, 1) == -1) + goto error; + + if (SCCudaLaunchGrid(ctx->cuda_search_kernel, 1, 1) == -1) + goto error; + + if (SCCudaMemcpyDtoH(host_offsets, cuda_offsets, buflen * sizeof(int)) == -1) + goto error; + + //printf("Raw matches: "); + //for (i = 0; i < buflen; i++) { + // printf("%d",offsets_buffer[i]); + //} + //printf("\n"); + + //printf("Matches: "); + for (i = 0; i < buflen; i++) { + if (host_offsets[i] == 0) + continue; + //printf("%d ", i); + /* get our patterns from the hash */ + h = B2G_CUDA_HASH16(u8_tolower(buf[i + ctx->search_m - 2]), + u8_tolower(buf[i + ctx->search_m - 1])); + + if (ctx->search_bloom[h] != NULL) { + COUNT(tctx->search_stat_pminlen_calls++); + COUNT(tctx->search_stat_pminlen_total += ctx->search_pminlen[h]); + + if ((buflen - i) < ctx->search_pminlen[h]) { + continue; + } else { + COUNT(tctx->search_stat_bloom_calls++); + + if (BloomFilterTest(ctx->search_bloom[h], buf + i, + ctx->search_pminlen[h]) == 0) { + COUNT(tctx->search_stat_bloom_hits++); + continue; + } + } + } + + B2gCudaHashItem *hi = ctx->search_hash[h], *thi; + for (thi = hi; thi != NULL; thi = thi->nxt) { + COUNT(tctx->search_stat_d0_hashloop++); + B2gCudaPattern *p = ctx->parray[thi->idx]; + + if (p->flags & B2G_CUDA_NOCASE) { + if (buflen - i < p->len) + continue; + + if (memcmp_lowercase(p->ci, buf + i, p->len) == 0) { + COUNT(tctx->search_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], i, p->len)) + matches++; + } + } else { + COUNT(tctx->search_stat_loop_no_match++); + } + } else { + if (buflen - i < p->len) + continue; + + if (memcmp(p->cs, buf + i, p->len) == 0) { + COUNT(tctx->search_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], i, p->len)) + matches++; + } + } else { + COUNT(tctx->search_stat_loop_no_match++); + } + } + } + } /* for(i = 0; i < buflen; i++) */ + + SCCudaMemFree(cuda_buf); + SCCudaMemFree(cuda_offsets); + + return matches; + + error: + if (cuda_buf != 0) + SCCudaMemFree(cuda_buf); + if (cuda_offsets != 0) + SCCudaMemFree(cuda_offsets); + return 0; +} + +uint32_t B2gCudaSearch(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen) +{ + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; +#ifdef B2G_CUDA_COUNTERS + B2gCudaThreadCtx *tctx = (B2gCudaThreadCtx *)mpm_thread_ctx->ctx; +#endif + uint32_t pos = 0, matches = 0; + B2G_CUDA_TYPE d; + uint32_t j; + + if (buflen < ctx->search_m) + return 0; + + while (pos <= (buflen - ctx->search_m)) { + j = ctx->search_m - 1; + d = ~0; + + do { + uint16_t h = B2G_CUDA_HASH16(u8_tolower(buf[pos + j - 1]), + u8_tolower(buf[pos + j])); + d &= ctx->search_B2G[h]; + d <<= 1; + j = j - 1; + } while (d != 0 && j != 0); + + /* (partial) match, move on to verification */ + if (d != 0) { + COUNT(tctx->search_stat_d0++); + + /* get our patterns from the hash */ + uint16_t h = B2G_CUDA_HASH16(u8_tolower(buf[pos + ctx->search_m - 2]), + u8_tolower(buf[pos + ctx->search_m - 1])); + + if (ctx->scan_bloom[h] != NULL) { + COUNT(tctx->scan_stat_pminlen_calls++); + COUNT(tctx->scan_stat_pminlen_total+=ctx->scan_pminlen[h]); + + if ((buflen - pos) < ctx->scan_pminlen[h]) { + goto skip_loop; + } else { + COUNT(tctx->scan_stat_bloom_calls++); + + if (BloomFilterTest(ctx->scan_bloom[h], buf+pos, + ctx->scan_pminlen[h]) == 0) { + COUNT(tctx->scan_stat_bloom_hits++); + + goto skip_loop; + } + } + } + + B2gCudaHashItem *hi = ctx->search_hash[h], *thi; + for (thi = hi; thi != NULL; thi = thi->nxt) { + B2gCudaPattern *p = ctx->parray[thi->idx]; + if (p->flags & B2G_CUDA_NOCASE) { + if (buflen - pos < p->len) + continue; + + if (memcmp_lowercase(p->ci, buf+pos, p->len) == 0) { + COUNT(tctx->search_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + pos, p->len)) + matches++; + } + + } else { + COUNT(tctx->search_stat_loop_no_match++); + } + } else { + if (buflen - pos < p->len) + continue; + + if (memcmp(p->cs, buf+pos, p->len) == 0) { + COUNT(tctx->search_stat_loop_match++); + + MpmEndMatch *em; + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + pos, p->len)) + matches++; + } + + } else { + COUNT(tctx->search_stat_loop_no_match++); + } + } + } + +skip_loop: + pos = pos + 1; + } else { + COUNT(tctx->search_stat_num_shift++); + COUNT(tctx->search_stat_total_shift += (j + 1)); + pos = pos + j + 1; + } + } + + return matches; +} + +uint32_t B2gCudaSearch1(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, + PatternMatcherQueue *pmq, uint8_t *buf, uint16_t buflen) +{ + SCEnter(); + + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx->ctx; + uint8_t *bufmin = buf; + uint8_t *bufend = buf + buflen - 1; + uint32_t cnt = 0; + B2gCudaPattern *p; + MpmEndMatch *em; + B2gCudaHashItem *thi, *hi; + + if (buflen == 0) + SCReturnUInt(0); + + while (buf <= bufend) { + uint8_t h = u8_tolower(*buf); + hi = &ctx->search_hash1[h]; + + if (hi->flags & 0x01) { + for (thi = hi; thi != NULL; thi = thi->nxt) { + p = ctx->parray[thi->idx]; + + if (p->len != 1) + continue; + + if (p->flags & B2G_CUDA_NOCASE) { + if (u8_tolower(*buf) == p->ci[0]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } else { + if (*buf == p->cs[0]) { + for (em = p->em; em; em = em->next) { + if (MpmMatchAppend(mpm_thread_ctx, pmq, em, + &mpm_thread_ctx->match[em->id], + (buf+1 - bufmin), p->len)) + cnt++; + } + } + } + } + } + buf += 1; + } + + if (mpm_ctx->search_maxlen > 1) { + /* Pass bufmin on because buf no longer points to the + * start of the buffer. */ + cnt += ctx->MBSearch(mpm_ctx, mpm_thread_ctx, pmq, bufmin, buflen); + } + + SCReturnUInt(cnt); +} + +/*********************Cuda_Specific_Mgmt_Code_Starts_Here**********************/ + +/** + * \brief The Cuda MPM B2G module's thread init function. + * + * \param tv Pointer to the ThreadVars which has invoked this function. + * \param initdata Pointer to some user sent data. + * \param data Pointer to a pointer which can be used to send data to the + * dispatcher thread. + * + * \retval TM_ECODE_OK Always. + */ +TmEcode B2gCudaMpmDispThreadInit(ThreadVars *tv, void *initdata, void **data) +{ + SCCudaHlModuleData *module_data = (SCCudaHlModuleData *)initdata; + + if (SCCudaCtxPushCurrent(module_data->cuda_context) == -1) { + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error pushing cuda context"); + } + + return TM_ECODE_OK; +} + +/** + * \brief The Cuda MPM B2G module's thread de-init function. + * + * \param tv Pointer to the ThreadVars which has invoked this function. + * \param data Pointer to the slot data if anything had been attached in + * the thread init function. + * + * \retval TM_ECODE_OK Always. + */ +TmEcode B2gCudaMpmDispThreadDeInit(ThreadVars *tv, void *data) +{ + if (SCCudaCtxPopCurrent(NULL) == -1) { + SCLogError(SC_ERR_B2G_CUDA_ERROR, "Error popping cuda context"); + } + + return TM_ECODE_OK; +} + +/** + * \brief The dispatcher function for the cuda mpm. Takes a packet, feeds + * it to the gpu and informs the calling client when it has the + * results ready. + * + * \param tv We don't need this. + * \param p Pointer to the Packet which contains all the relevant data, + * like the bufffer, buflen, the contexts. + * \param data Pointer to the slot data if anything had been attached in + * the thread init function. + * \param pq We don't need this. + * + * \retval TM_ECODE_OK Always. + */ +TmEcode B2gCudaMpmDispatcher(ThreadVars *tv, Packet *p, void *data, + PacketQueue *pq) +{ + if (p == NULL) + return TM_ECODE_OK; + + if (p->cuda_search) { + p->cuda_matches = mpm_table[p->cuda_mpm_ctx->mpm_type].Search(p->cuda_mpm_ctx, + p->cuda_mtc, + p->cuda_pmq, + p->payload, + p->payload_len); + } else { + p->cuda_matches = mpm_table[p->cuda_mpm_ctx->mpm_type].Scan(p->cuda_mpm_ctx, + p->cuda_mtc, + p->cuda_pmq, + p->payload, + p->payload_len); + } + + /* signal the client that the result is ready */ + SCCondSignal(&p->cuda_cond_q); + /* wait for the client indication that it has read the results. If the + * client still hasn't sent the indication, signal it again and do so + * every 50 microseconds */ + while (p->cuda_done == 0) { + SCCondSignal(&p->cuda_cond_q); + usleep(50); + } + + if (p->cuda_free_packet == 1) { + free(p); + } + + return TM_ECODE_OK; +} + +/** + * \brief Registers the Cuda B2G MPM Module. + */ +void TmModuleCudaMpmB2gRegister(void) +{ + tmm_modules[TMM_CUDA_MPM_B2G].name = "Cuda_Mpm_B2g"; + tmm_modules[TMM_CUDA_MPM_B2G].ThreadInit = B2gCudaMpmDispThreadInit; + tmm_modules[TMM_CUDA_MPM_B2G].Func = B2gCudaMpmDispatcher; + tmm_modules[TMM_CUDA_MPM_B2G].ThreadExitPrintStats = NULL; + tmm_modules[TMM_CUDA_MPM_B2G].ThreadDeinit = B2gCudaMpmDispThreadDeInit; + tmm_modules[TMM_CUDA_MPM_B2G].RegisterTests = NULL; +} + +/***************************Code_Specific_To_Mpm_B2g***************************/ + +#ifdef UNITTESTS + +int B2gCudaStartDispatcherThreadRC(const char *name) +{ + SCCudaHlModuleData *data = NULL; + TmModule *tm_module = NULL; + + if (name == NULL) { + SCLogError(SC_INVALID_ARGUMENTS, "Error invalid arguments. " + "name NULL"); + return -1; + } + + if (tv_CMB2_RC != NULL) { + SCLogError(SC_ERR_TM_THREADS_ERROR, "We already have this thread " + "running from b2g-cuda"); + return 0; + } + + data = SCCudaHlGetModuleData(SCCudaHlGetModuleHandle(name)); + if (data == NULL) { + SCLogDebug("Module not registered. To avail the benefits of this " + "registration facility, first register a module using " + "context using SCCudaHlRegisterModule(), after which you " + "can call this function"); + return -1; + } + + /* create the threads */ + tv_CMB2_RC = TmThreadCreatePacketHandler("Cuda_Mpm_B2g_RC", + "rules_content_mpm_inqueue", "simple", + NULL, NULL, + "1slot_noout"); + if (tv_CMB2_RC == NULL) { + SCLogError(SC_ERR_TM_THREADS_ERROR, "ERROR: TmThreadsCreate failed"); + exit(EXIT_FAILURE); + } + tv_CMB2_RC->inq->writer_cnt++; + + tm_module = TmModuleGetByName("Cuda_Mpm_B2g"); + if (tm_module == NULL) { + SCLogError(SC_ERR_TM_MODULES_ERROR, + "ERROR: TmModuleGetByName failed for Cuda_Mpm_B2g_RC"); + exit(EXIT_FAILURE); + } + Tm1SlotSetFunc(tv_CMB2_RC, tm_module, data); + + if (TmThreadSpawn(tv_CMB2_RC) != TM_ECODE_OK) { + SCLogError(SC_ERR_TM_THREADS_ERROR, "ERROR: TmThreadSpawn failed"); + exit(EXIT_FAILURE); + } + + TmThreadContinue(tv_CMB2_RC); + + return 0; +} + +/** + * \brief Hacks for the tests. While running the tests, we sometimes need to + * kill the threads to make them pop the cuda contexts. We don't need + * these under normal running. + */ +void B2gCudaKillDispatcherThreadRC(void) +{ + if (tv_CMB2_RC == NULL) + return; + + TmThreadKillThread(tv_CMB2_RC); + TmThreadRemove(tv_CMB2_RC, tv_CMB2_RC->type); + free(tv_CMB2_RC); + tv_CMB2_RC = NULL; + + return; +} + +/** + * \brief Hacks for the tests. While running the tests, we sometimes need to + * kill the threads to make them pop the cuda contexts. We don't need + * these under normal running. + */ +void B2gCudaKillDispatcherThreadAPC(void) +{ + if (tv_CMB2_APC == NULL) + return; + + TmThreadKillThread(tv_CMB2_APC); + TmThreadRemove(tv_CMB2_APC, tv_CMB2_APC->type); + free(tv_CMB2_APC); + tv_CMB2_APC = NULL; + + return; +} + +int B2gCudaStartDispatcherThreadAPC(const char *name) +{ + SCCudaHlModuleData *data = NULL; + TmModule *tm_module = NULL; + + if (name == NULL) { + SCLogError(SC_INVALID_ARGUMENTS, "Error invalid arguments. " + "name NULL"); + return -1; + } + + if (tv_CMB2_APC != NULL) { + SCLogError(SC_ERR_TM_THREADS_ERROR, "We already have this thread " + "running from b2g-cuda"); + return 0; + } + + data = SCCudaHlGetModuleData(SCCudaHlGetModuleHandle(name)); + if (data == NULL) { + SCLogDebug("Module not registered. To avail the benefits of this " + "registration facility, first register a module using " + "context using SCCudaHlRegisterModule(), after which you " + "can call this function"); + return -1; + } + + /* create the threads */ + tv_CMB2_APC = TmThreadCreatePacketHandler("Cuda_Mpm_B2g_APC", + "app_proto_content_mpm_inqueue", "simple", + NULL, NULL, + "1slot_noout"); + if (tv_CMB2_APC == NULL) { + SCLogError(SC_ERR_TM_THREADS_ERROR, "ERROR: TmThreadsCreate failed"); + exit(EXIT_FAILURE); + } + tv_CMB2_APC->inq->writer_cnt++; + + tm_module = TmModuleGetByName("Cuda_Mpm_B2g"); + if (tm_module == NULL) { + SCLogError(SC_ERR_TM_MODULES_ERROR, + "ERROR: TmModuleGetByName failed for Cuda_Mpm_B2g_APC"); + exit(EXIT_FAILURE); + } + Tm1SlotSetFunc(tv_CMB2_APC, tm_module, data); + + if (TmThreadSpawn(tv_CMB2_APC) != TM_ECODE_OK) { + SCLogError(SC_ERR_TM_THREADS_ERROR, "ERROR: TmThreadSpawn failed"); + exit(EXIT_FAILURE); + } + + TmThreadContinue(tv_CMB2_APC); + + return 0; +} + +void B2gCudaPushPacketTo_tv_CMB2_RC(Packet *p) +{ + PacketQueue *q = &trans_q[tv_CMB2_RC->inq->id]; + + SCMutexLock(&q->mutex_q); + PacketEnqueue(q, p); + SCCondSignal(&q->cond_q); + SCMutexUnlock(&q->mutex_q); + + return; +} + +void B2gCudaPushPacketTo_tv_CMB2_APC(Packet *p) +{ + PacketQueue *q = &trans_q[tv_CMB2_APC->inq->id]; + + SCMutexLock(&q->mutex_q); + PacketEnqueue(q, p); + SCCondSignal(&q->cond_q); + SCMutexUnlock(&q->mutex_q); + + return; +} + +/*********************************Unittests************************************/ + +static int B2gCudaTestInitTestEnv(void) +{ + SCCudaHlRegisterModule("B2G_CUDA_TEST"); + + return 1; +} + +static int B2gCudaTest01(void) +{ + MpmCtx mpm_ctx; + MpmThreadCtx mpm_thread_ctx; + B2gCudaCtx *ctx = NULL; + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + + memset(&mpm_ctx, 0, sizeof(MpmCtx)); + B2gCudaInitCtx(&mpm_ctx, module_handle); + + ctx = mpm_ctx.ctx; + + if (ctx->cuda_context == 0) + goto end; + if (ctx->cuda_module == 0) + goto end; + if (ctx->cuda_search_kernel == 0) + goto end; + + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"one", 3, 0, 0, 1, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"two", 3, 0, 0, 2, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"three", 5, 0, 0, 3, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"four", 4, 0, 0, 4, 1) == -1) + goto end; + + if (B2gCudaPreparePatterns(&mpm_ctx) == -1) + goto end; + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 4 /* 4 patterns */); + + char *string = "onetwothreeaaaaoneaatwobbbthrbsonwehowvonwoonsldffoursadnothreewtowoneowtwo"; + result = (B2gCudaSearchBNDMq(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)string, strlen(string)) == 9); + + end: + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTest02(void) +{ + MpmCtx mpm_ctx; + MpmThreadCtx mpm_thread_ctx; + B2gCudaCtx *ctx = NULL; + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + + memset(&mpm_ctx, 0, sizeof(MpmCtx)); + B2gCudaInitCtx(&mpm_ctx, module_handle); + + ctx = mpm_ctx.ctx; + + if (ctx->cuda_context == 0) + goto end; + if (ctx->cuda_module == 0) + goto end; + if (ctx->cuda_search_kernel == 0) + goto end; + + if (B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"one", 3, 0, 0, 1, 1, 0) == -1) + goto end; + if (B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"two", 3, 0, 0, 2, 1, 0) == -1) + goto end; + if (B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"three", 5, 0, 0, 3, 1, 0) == -1) + goto end; + if (B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"four", 4, 0, 0, 4, 1, 0) == -1) + goto end; + + if (B2gCudaPreparePatterns(&mpm_ctx) == -1) + goto end; + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 4 /* 4 patterns */); + + char *string = "onetwothreeaaaaoneaatwobbbthrbsonwehowvonwoonsldffoursadnothreewtowoneowtwo"; + result = (B2gCudaScanBNDMq(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)string, strlen(string)) == 9); + + end: + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/** + * \test Test that the *AddPattern* functions work as expected. + */ +static int B2gCudaTest03(void) +{ + MpmCtx mpm_ctx; + B2gCudaCtx *ctx = NULL; + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + + memset(&mpm_ctx, 0, sizeof(MpmCtx)); + B2gCudaInitCtx(&mpm_ctx, module_handle); + + ctx = mpm_ctx.ctx; + if (ctx->cuda_context == 0) + goto end; + if (ctx->cuda_module == 0) + goto end; + if (ctx->cuda_search_kernel == 0) + goto end; + + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"onee", 4, 0, 0, 1, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"twoo", 4, 0, 0, 2, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"three", 5, 0, 0, 3, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"four", 4, 0, 0, 4, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"onee", 4, 0, 0, 1, 2) == -1) + goto end; + + if (B2gCudaPreparePatterns(&mpm_ctx) == -1) + goto end; + + char *string = "one"; + result = (B2gCudaSearchBNDMq(&mpm_ctx, NULL, NULL, (uint8_t *)string, strlen(string)) == 0); + + end: + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/** + * \test Test that the *AddPattern* functions work as expected. + */ +static int B2gCudaTest04(void) +{ + MpmCtx mpm_ctx; + MpmThreadCtx mpm_thread_ctx; + B2gCudaCtx *ctx = NULL; + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + + memset(&mpm_ctx, 0, sizeof(MpmCtx)); + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + + ctx = mpm_ctx.ctx; + if (ctx->cuda_context == 0) + goto end; + if (ctx->cuda_module == 0) + goto end; + if (ctx->cuda_search_kernel == 0) + goto end; + + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"one", 3, 0, 0, 1, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"two", 3, 0, 0, 2, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"three", 5, 0, 0, 3, 1) == -1) + goto end; + if (B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"four", 4, 0, 0, 4, 1) == -1) + goto end; + + if (B2gCudaPreparePatterns(&mpm_ctx) == -1) + goto end; + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 4 /* 4 patterns */); + + char *string = "onetwothreeaaaaoneaatwobbbthrbsonwehowvonwfouoonsldffoursadnothreewtowoneowtwo"; + result = (B2gCudaSearchBNDMq(&mpm_ctx, &mpm_thread_ctx, + NULL, (uint8_t *)string, strlen(string)) == 9); + + result = 1; + + end: + MpmMatchCleanup(&mpm_thread_ctx); + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan01(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "abcdefghjiklmnopqrstuvwxyz"; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + + return result; +} + +static int B2gCudaTestScan02(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "abcdefghjiklmnopqrstuvwxyz"; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 0) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + + return result; +} + +static int B2gCudaTestScan03(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "abcdefghjiklmnopqrstuvwxyz"; + + /* a match each for these strings */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"bcde", 4, 0, 0, 1, 0, 0); + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"fghj", 4, 0, 0, 2, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 3 /* 3 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 3) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + + return result; +} + +/** + * \test Test patterns longer than 'm'. M is 4 here. + */ +static int B2gCudaTestScan04(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "abcdefghjiklmnopqrstuvwxyz"; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"bcdegh", 6, 0, 0, 1, 0, 0); + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"fghjxyz", 7, 0, 0, 2, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 3 /* 3 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/** + * \test Case insensitive test patterns longer than 'm'. M is 4 here. + */ +static int B2gCudaTestScan05(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "abcdefghjiklmnopqrstuvwxyz"; + + B2gCudaAddScanPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0); + B2gCudaAddScanPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0, 0); + B2gCudaAddScanPatternCI(&mpm_ctx, (uint8_t *)"fghJikl", 7, 0, 0, 2, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 3 /* 3 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 3) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + + return result; +} + +static int B2gCudaTestScan06(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "abcd"; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + + return result; +} + +static int B2gCudaTestScan07(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + + /* total matches: 135 */ + /* should match 30 times */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0, 0); + /* should match 29 times */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 1, 0, 0); + /* should match 28 times */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAA", 3, 0, 0, 2, 0, 0); + /* should match 26 times */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAAAA", 5, 0, 0, 3, 0, 0); + /* should match 21 times */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAA", 10, 0, 0, 4, 0, 0); + /* should match 1 time */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 30, 0, 0, 5, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 6 /* 6 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 135) + result = 1; + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + + return result; +} + +static int B2gCudaTestScan08(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)"a", 1); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 0) + result = 1; + else + printf("0 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/* we segfault with this test */ +static int B2gCudaTestScan09(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)"ab", 2); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan10(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0, 0); /* 1 match */ + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + char *buf = "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789" + "abcdefgh" + "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789"; + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan11(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcde", 5, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 2 /* 2 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 2) + result = 1; + else + printf("2 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan12(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0, 0); /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"vwxyz", 5, 0, 0, 0, 0, 0); /* 1 match */ + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 2 /* 2 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 2) + result = 1; + else + printf("2 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan13(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCD", + 30, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCD", 30); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan14(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCDE", + 31, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCDE", 31); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan15(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCDEF", + 32, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCDEF", 32); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan16(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABC", + 29, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyzABC", 29); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan17(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzAB", + 28, 0, 0, 0, 0, 0); /* 1 match */ + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghijklmnopqrstuvwxyzAB", 28); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan18(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddScanPatternCS(&mpm_ctx, + (uint8_t *)"abcde""fghij""klmno""pqrst""uvwxy""z", + 26, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcde""fghij""klmno""pqrst""uvwxy""z", + 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan19(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + 30, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 30); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan20(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 */ + B2gCudaAddScanPatternCS(&mpm_ctx, + (uint8_t *)"AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AA", + 32, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AA", + 32); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestScan21(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 */ + B2gCudaAddScanPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 patterns */); + + uint32_t cnt = ctx->Scan(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)"AA", 2); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + + + +static int B2gCudaTestSearch01(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch02(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 0) + result = 1; + else + printf("0 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch03(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"bcde", 4, 0, 0, 1, 0); + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"fghj", 4, 0, 0, 2, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 3 /* 3 patterns */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 3) + result = 1; + else + printf("3 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/** + * \test test patterns longer than 'm'. M is 4 here. + */ +static int B2gCudaTestSearch04(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"bcdegh", 6, 0, 0, 1, 0); + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"fghjxyz", 7, 0, 0, 2, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 3 /* 3 patterns */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/** + * \test case insensitive test patterns longer than 'm'. M is 4 here. + */ +static int B2gCudaTestSearch05(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0); + /* 1 match */ + B2gCudaAddPatternCI(&mpm_ctx, (uint8_t *)"bCdEfG", 6, 0, 0, 1, 0); + /* 1 match */ + B2gCudaAddPatternCI(&mpm_ctx, (uint8_t *)"fghJikl", 7, 0, 0, 2, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 3 /* 3 patterns */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 3) + result = 1; + else + printf("3 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch06(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)"abcd", 4); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch07(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* should match 30 times */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); + /* should match 29 times */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 1, 0); + /* should match 28 times */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"AAA", 3, 0, 0, 2, 0); + /* 26 */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAA", 5, 0, 0, 3, 0); + /* 21 */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAA", 10, 0, 0, 4, 0); + /* 1 */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + 30, 0, 0, 5, 0); + /* total matches: 135 */ + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 6 /* 6 patterns */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 30); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 135) + result = 1; + else + printf("135 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch08(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)"a", 1); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 0) + result = 1; + else + printf("0 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch09(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)"ab", 2); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +/* 1 match */ +static int B2gCudaTestSearch10(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1 /* 1 pattern */); + + char *buf = "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789" + "abcdefgh" + "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789"; + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, (uint8_t *)buf, + strlen(buf)); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 1) + result = 1; + else + printf("1 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch11(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"abcde", 5, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 2 /* 2 patterns */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 2) + result = 1; + else + printf("2 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestSearch12(void) +{ + int result = 0; + int module_handle = SCCudaHlGetModuleHandle("B2G_CUDA_TEST"); + MpmCtx mpm_ctx; + memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); + MpmThreadCtx mpm_thread_ctx; + MpmInitCtx(&mpm_ctx, MPM_B2G_CUDA, module_handle); + B2gCudaCtx *ctx = (B2gCudaCtx *)mpm_ctx.ctx; + + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0); + /* 1 match */ + B2gCudaAddPatternCS(&mpm_ctx, (uint8_t *)"vwxyz", 5, 0, 0, 0, 0); + + B2gCudaPreparePatterns(&mpm_ctx); + B2gCudaThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 2 /* 2 patterns */); + + uint32_t cnt = ctx->Search(&mpm_ctx, &mpm_thread_ctx, NULL, + (uint8_t *)"abcdefghjiklmnopqrstuvwxyz", 26); + + MpmMatchCleanup(&mpm_thread_ctx); + + if (cnt == 2) + result = 1; + else + printf("2 != %" PRIu32 " ",cnt); + + B2gCudaThreadDestroyCtx(&mpm_ctx, &mpm_thread_ctx); + B2gCudaDestroyCtx(&mpm_ctx); + return result; +} + +static int B2gCudaTestDeInitTestEnv(void) +{ + SCCudaHlDeRegisterModule("B2G_CUDA_TEST"); + + return 1; +} + +#endif /* UNITTESTS */ + +/*********************************Unittests************************************/ + +void B2gCudaRegisterTests(void) +{ +#ifdef UNITTESTS + UtRegisterTest("B2gCudaTestInitTestEnv", B2gCudaTestInitTestEnv, 1); + UtRegisterTest("B2gCudaTest01", B2gCudaTest01, 1); + UtRegisterTest("B2gCudaTest02", B2gCudaTest02, 1); + UtRegisterTest("B2gCudaTest03", B2gCudaTest03, 1); + UtRegisterTest("B2gCudaTest04", B2gCudaTest04, 1); + UtRegisterTest("B2gCudaTestScan01", B2gCudaTestScan01, 1); + UtRegisterTest("B2gCudaTestScan02", B2gCudaTestScan02, 1); + UtRegisterTest("B2gCudaTestScan03", B2gCudaTestScan03, 1); + UtRegisterTest("B2gCudaTestScan04", B2gCudaTestScan04, 1); + UtRegisterTest("B2gCudaTestScan05", B2gCudaTestScan05, 1); + UtRegisterTest("B2gCudaTestScan06", B2gCudaTestScan06, 1); + UtRegisterTest("B2gCudaTestScan07", B2gCudaTestScan07, 1); + UtRegisterTest("B2gCudaTestScan08", B2gCudaTestScan08, 1); + UtRegisterTest("B2gCudaTestScan09", B2gCudaTestScan09, 1); + UtRegisterTest("B2gCudaTestScan10", B2gCudaTestScan10, 1); + UtRegisterTest("B2gCudaTestScan11", B2gCudaTestScan11, 1); + UtRegisterTest("B2gCudaTestScan12", B2gCudaTestScan12, 1); + UtRegisterTest("B2gCudaTestScan13", B2gCudaTestScan13, 1); + + UtRegisterTest("B2gCudaTestScan14", B2gCudaTestScan14, 1); + UtRegisterTest("B2gCudaTestScan15", B2gCudaTestScan15, 1); + UtRegisterTest("B2gCudaTestScan16", B2gCudaTestScan16, 1); + UtRegisterTest("B2gCudaTestScan17", B2gCudaTestScan17, 1); + UtRegisterTest("B2gCudaTestScan18", B2gCudaTestScan18, 1); + UtRegisterTest("B2gCudaTestScan19", B2gCudaTestScan19, 1); + UtRegisterTest("B2gCudaTestScan20", B2gCudaTestScan20, 1); + UtRegisterTest("B2gCudaTestScan21", B2gCudaTestScan21, 1); + + UtRegisterTest("B2gCudaTestSearch01", B2gCudaTestSearch01, 1); + UtRegisterTest("B2gCudaTestSearch02", B2gCudaTestSearch02, 1); + UtRegisterTest("B2gCudaTestSearch03", B2gCudaTestSearch03, 1); + UtRegisterTest("B2gCudaTestSearch04", B2gCudaTestSearch04, 1); + UtRegisterTest("B2gCudaTestSearch05", B2gCudaTestSearch05, 1); + UtRegisterTest("B2gCudaTestSearch06", B2gCudaTestSearch06, 1); + UtRegisterTest("B2gCudaTestSearch07", B2gCudaTestSearch07, 1); + UtRegisterTest("B2gCudaTestSearch08", B2gCudaTestSearch08, 1); + UtRegisterTest("B2gCudaTestSearch09", B2gCudaTestSearch09, 1); + UtRegisterTest("B2gCudaTestSearch10", B2gCudaTestSearch10, 1); + UtRegisterTest("B2gCudaTestSearch11", B2gCudaTestSearch11, 1); + UtRegisterTest("B2gCudaTestSearch12", B2gCudaTestSearch12, 1); + UtRegisterTest("B2gCudaTestDeInitTestEnv", B2gCudaTestDeInitTestEnv, 1); +#endif /* UNITTESTS */ +} + +#endif /* __SC_CUDA_SUPPORT */ diff --git a/src/util-mpm-b2g-cuda.h b/src/util-mpm-b2g-cuda.h new file mode 100644 index 0000000000..1b737ef72d --- /dev/null +++ b/src/util-mpm-b2g-cuda.h @@ -0,0 +1,157 @@ +/** + * Copyright (c) 2009 Open Information Security Foundation. + * + * \author Anoop Saldanha + */ + +#ifndef __UTIL_MPM_B2G_CUDA_H__ +#define __UTIL_MPM_B2G_CUDA_H__ + +#include +#include "decode.h" +#include "util-mpm.h" +#include "util-bloomfilter.h" + +#define B2G_CUDA_NOCASE 0x01 +#define B2G_CUDA_SCAN 0x02 + +#define B2G_CUDA_HASHSIZE 4096 +#define B2G_CUDA_HASHSHIFT 4 +#define B2G_CUDA_TYPE uint32_t +#define B2G_CUDA_WORD_SIZE 32 +#define B2G_CUDA_BLOOMSIZE 1024 +#define B2G_CUDA_Q 2 + +#define B2G_CUDA_HASH16(a, b) (((a) << B2G_CUDA_HASHSHIFT) | (b)) + +#define B2G_CUDA_SCANFUNC_NAME "B2gCudaScanBNDMq" +#define B2G_CUDA_SEARCHFUNC_NAME "B2gCudaSearchBNDMq" + +#define B2G_CUDA_SCANFUNC B2gCudaScanBNDMq +#define B2G_CUDA_SEARCHFUNC B2gCudaSearchBNDMq + +typedef struct B2gCudaPattern_ { + uint8_t flags; + /** \todo we're limited to 32/64 byte lengths, uint8_t would be fine here */ + uint16_t len; + /* case sensitive */ + uint8_t *cs; + /* case INsensitive */ + uint8_t *ci; + struct B2gCudaPattern_ *next; + MpmEndMatch *em; +} B2gCudaPattern; + +typedef struct B2gCudaHashItem_ { + uint16_t idx; + uint8_t flags; + struct B2gCudaHashItem_ *nxt; +} B2gCudaHashItem; + +typedef struct B2gCudaCtx_ { + int module_handle; + + CUcontext cuda_context; + CUmodule cuda_module; + + CUfunction cuda_search_kernel; + CUfunction cuda_scan_kernel; + + CUdeviceptr cuda_g_u8_lowercasetable; + CUdeviceptr cuda_search_B2G; + CUdeviceptr cuda_scan_B2G; + + B2G_CUDA_TYPE *scan_B2G; + B2G_CUDA_TYPE scan_m; + BloomFilter **scan_bloom; + /* array containing the minimal length of the patters in a hash bucket. + * Used for the BloomFilter. */ + uint8_t *scan_pminlen; + /* pattern arrays */ + B2gCudaPattern **parray; + + B2G_CUDA_TYPE search_m; + B2G_CUDA_TYPE *search_B2G; + + uint16_t scan_1_pat_cnt; +#ifdef B2G_CUDA_SCAN2 + uint16_t scan_2_pat_cnt; +#endif + uint16_t scan_x_pat_cnt; + + uint32_t scan_hash_size; + B2gCudaHashItem **scan_hash; + B2gCudaHashItem scan_hash1[256]; +#ifdef B2G_CUDA_SCAN2 + B2gHashItem **scan_hash2; +#endif + uint32_t search_hash_size; + BloomFilter **search_bloom; + /* array containing the minimal length of the patters in a hash bucket. + * Used for the BloomFilter. */ + uint8_t *search_pminlen; + + B2gCudaHashItem **search_hash; + B2gCudaHashItem search_hash1[256]; + + /* hash used during ctx initialization */ + B2gCudaPattern **init_hash; + + uint8_t scan_s0; + uint8_t search_s0; + + /* we store our own multi byte scan ptr here for B2gCudaSearch1 */ + uint32_t (*Scan)(struct MpmCtx_ *, struct MpmThreadCtx_ *, + PatternMatcherQueue *, uint8_t *, uint16_t); + /* we store our own multi byte search ptr here for B2gCudaSearch1 */ + uint32_t (*Search)(struct MpmCtx_ *, struct MpmThreadCtx_ *, + PatternMatcherQueue *, uint8_t *, uint16_t); + + /* we store our own multi byte scan ptr here for B2gCudaSearch1 */ + uint32_t (*MBScan2)(struct MpmCtx_ *, struct MpmThreadCtx_ *, + PatternMatcherQueue *, uint8_t *, uint16_t); + uint32_t (*MBScan)(struct MpmCtx_ *, struct MpmThreadCtx_ *, + PatternMatcherQueue *, uint8_t *, uint16_t); + /* we store our own multi byte search ptr here for B2gCudaSearch1 */ + uint32_t (*MBSearch)(struct MpmCtx_ *, struct MpmThreadCtx_ *, + PatternMatcherQueue *, uint8_t *, uint16_t); + +} B2gCudaCtx; + +typedef struct B2gCudaThreadCtx_ { +#ifdef B2G_CUDA_COUNTERS + uint32_t scan_stat_pminlen_calls; + uint32_t scan_stat_pminlen_total; + uint32_t scan_stat_bloom_calls; + uint32_t scan_stat_bloom_hits; + uint32_t scan_stat_calls; + uint32_t scan_stat_m_total; + uint32_t scan_stat_d0; + uint32_t scan_stat_d0_hashloop; + uint32_t scan_stat_loop_match; + uint32_t scan_stat_loop_no_match; + uint32_t scan_stat_num_shift; + uint32_t scan_stat_total_shift; + + uint32_t search_stat_d0; + uint32_t search_stat_loop_match; + uint32_t search_stat_loop_no_match; + uint32_t search_stat_num_shift; + uint32_t search_stat_total_shift; +#endif /* B2G_CUDA_COUNTERS */ +} B2gCudaThreadCtx; + +void MpmB2gCudaRegister(void); + +void TmModuleCudaMpmB2gRegister(void); + +int B2gCudaStartDispatcherThreadRC(const char *); +int B2gCudaStartDispatcherThreadAPC(const char *); + +void B2gCudaKillDispatcherThreadRC(void); +void B2gCudaKillDispatcherThreadAPC(void); + +void B2gCudaPushPacketTo_tv_CMB2_RC(Packet *); +void B2gCudaPushPacketTo_tv_CMB2_APC(Packet *); + +#endif /* __UTIL_MPM_B2G_CUDA_H__ */ diff --git a/src/util-mpm-b2g.c b/src/util-mpm-b2g.c index 0bb0af704c..8e97babef2 100644 --- a/src/util-mpm-b2g.c +++ b/src/util-mpm-b2g.c @@ -39,7 +39,7 @@ static uint32_t b2g_bloom_size = 0; static void *b2g_scan_func; static void *b2g_search_func; -void B2gInitCtx (MpmCtx *); +void B2gInitCtx (MpmCtx *, int); void B2gThreadInitCtx(MpmCtx *, MpmThreadCtx *, uint32_t); void B2gDestroyCtx(MpmCtx *); void B2gThreadDestroyCtx(MpmCtx *, MpmThreadCtx *); @@ -987,9 +987,7 @@ static void B2gGetConfig() } } -void B2gInitCtx (MpmCtx *mpm_ctx) -{ - SCEnter(); +void B2gInitCtx (MpmCtx *mpm_ctx, int module_handle) { SCLogDebug("mpm_ctx %p, ctx %p", mpm_ctx, mpm_ctx->ctx); BUG_ON(mpm_ctx->ctx != NULL); @@ -1888,7 +1886,7 @@ static int B2gTestInit01 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -1908,7 +1906,7 @@ static int B2gTestInit01 (void) { static int B2gTestS0Init01 (void) { int result = 0; MpmCtx mpm_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -1927,7 +1925,7 @@ static int B2gTestS0Init01 (void) { static int B2gTestS0Init02 (void) { int result = 0; MpmCtx mpm_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -1947,7 +1945,7 @@ static int B2gTestS0Init02 (void) { static int B2gTestS0Init03 (void) { int result = 0; MpmCtx mpm_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -1967,7 +1965,7 @@ static int B2gTestS0Init03 (void) { static int B2gTestS0Init04 (void) { int result = 0; MpmCtx mpm_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abab", 4, 0, 0, 0, 0); /* 1 match */ @@ -1986,7 +1984,7 @@ static int B2gTestS0Init04 (void) { static int B2gTestS0Init05 (void) { int result = 0; MpmCtx mpm_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcab", 5, 0, 0, 0, 0); /* 1 match */ @@ -2008,7 +2006,7 @@ static int B2gTestScan01 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2035,7 +2033,7 @@ static int B2gTestScan02 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2062,7 +2060,7 @@ static int B2gTestScan03 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2092,7 +2090,7 @@ static int B2gTestScan04 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2122,7 +2120,7 @@ static int B2gTestScan05 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2151,7 +2149,7 @@ static int B2gTestScan06 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2178,7 +2176,7 @@ static int B2gTestScan07 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0, 0); /* should match 30 times */ @@ -2211,7 +2209,7 @@ static int B2gTestScan08 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2238,7 +2236,7 @@ static int B2gTestScan09 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0, 0); /* 1 match */ @@ -2265,7 +2263,7 @@ static int B2gTestScan10 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0, 0); /* 1 match */ @@ -2297,7 +2295,7 @@ static int B2gTestScan11 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2325,7 +2323,7 @@ static int B2gTestScan12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2353,7 +2351,7 @@ static int B2gTestScan13 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCD", 30, 0, 0, 0, 0, 0); /* 1 match */ @@ -2380,7 +2378,7 @@ static int B2gTestScan14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCDE", 31, 0, 0, 0, 0, 0); /* 1 match */ @@ -2407,7 +2405,7 @@ static int B2gTestScan15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABCDEF", 32, 0, 0, 0, 0, 0); /* 1 match */ @@ -2434,7 +2432,7 @@ static int B2gTestScan16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzABC", 29, 0, 0, 0, 0, 0); /* 1 match */ @@ -2461,7 +2459,7 @@ static int B2gTestScan17 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefghijklmnopqrstuvwxyzAB", 28, 0, 0, 0, 0, 0); /* 1 match */ @@ -2488,7 +2486,7 @@ static int B2gTestScan18 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcde""fghij""klmno""pqrst""uvwxy""z", 26, 0, 0, 0, 0, 0); /* 1 match */ @@ -2515,7 +2513,7 @@ static int B2gTestScan19 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 30, 0, 0, 0, 0, 0); /* 1 */ @@ -2542,7 +2540,7 @@ static int B2gTestScan20 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AAAAA""AA", 32, 0, 0, 0, 0, 0); /* 1 */ @@ -2571,7 +2569,7 @@ static int B2gTestScan21 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddScanPatternCS(&mpm_ctx, (uint8_t *)"AA", 2, 0, 0, 0, 0, 0); /* 1 */ @@ -2598,7 +2596,7 @@ static int B2gTestSearch01 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2625,7 +2623,7 @@ static int B2gTestSearch02 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0); /* 1 match */ @@ -2652,7 +2650,7 @@ static int B2gTestSearch03 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2682,7 +2680,7 @@ static int B2gTestSearch04 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2712,7 +2710,7 @@ static int B2gTestSearch05 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0); /* 1 match */ @@ -2741,7 +2739,7 @@ static int B2gTestSearch06 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2768,7 +2766,7 @@ static int B2gTestSearch07 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ @@ -2801,7 +2799,7 @@ static int B2gTestSearch08 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2828,7 +2826,7 @@ static int B2gTestSearch09 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0); /* 1 match */ @@ -2855,7 +2853,7 @@ static int B2gTestSearch10 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0); /* 1 match */ @@ -2887,7 +2885,7 @@ static int B2gTestSearch11 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2915,7 +2913,7 @@ static int B2gTestSearch12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B2G); + MpmInitCtx(&mpm_ctx, MPM_B2G, -1); B2gCtx *ctx = (B2gCtx *)mpm_ctx.ctx; B2gAddPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0); /* 1 match */ @@ -2991,7 +2989,7 @@ int main () { #define R 4 int i; B2gCtx bg_ctx; - B2gInitCtx(&bg_ctx); + B2gInitCtx(&bg_ctx, -1); B2gAddPatternCI(&bg_ctx, "grep", 4, 0, 0, 0, 0); B2pPrepare(&bg_ctx); diff --git a/src/util-mpm-b3g.c b/src/util-mpm-b3g.c index 34b1da5b8a..397aed6162 100644 --- a/src/util-mpm-b3g.c +++ b/src/util-mpm-b3g.c @@ -34,7 +34,7 @@ static uint32_t b3g_bloom_size = 0; static void *b3g_scan_func; static void *b3g_search_func; -void B3gInitCtx (MpmCtx *); +void B3gInitCtx (MpmCtx *, int); void B3gThreadInitCtx(MpmCtx *, MpmThreadCtx *, uint32_t); void B3gDestroyCtx(MpmCtx *); void B3gThreadDestroyCtx(MpmCtx *, MpmThreadCtx *); @@ -993,9 +993,8 @@ void B3gGetConfig() } } -void B3gInitCtx (MpmCtx *mpm_ctx) -{ - SCLogDebug("mpm_ctx %p\n", mpm_ctx); +void B3gInitCtx (MpmCtx *mpm_ctx, int module_handle) { + //printf("B3gInitCtx: mpm_ctx %p\n", mpm_ctx); mpm_ctx->ctx = malloc(sizeof(B3gCtx)); if (mpm_ctx->ctx == NULL) @@ -2064,7 +2063,7 @@ static int B3gTestInit01 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2087,7 +2086,7 @@ static int B3gTestS0Init01 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2107,7 +2106,7 @@ static int B3gTestS0Init02 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2128,7 +2127,7 @@ static int B3gTestS0Init03 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2149,7 +2148,7 @@ static int B3gTestS0Init04 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abab", 4, 0, 0, 0, 0); /* 1 match */ @@ -2169,7 +2168,7 @@ static int B3gTestS0Init05 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcab", 5, 0, 0, 0, 0); /* 1 match */ @@ -2191,7 +2190,7 @@ static int B3gTestScan01 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2218,7 +2217,7 @@ static int B3gTestScan02 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2245,7 +2244,7 @@ static int B3gTestScan03 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2275,7 +2274,7 @@ static int B3gTestScan04 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2305,7 +2304,7 @@ static int B3gTestScan05 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2334,7 +2333,7 @@ static int B3gTestScan06 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2361,7 +2360,7 @@ static int B3gTestScan07 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0, 0); /* should match 30 times */ @@ -2394,7 +2393,7 @@ static int B3gTestScan08 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2421,7 +2420,7 @@ static int B3gTestScan09 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0, 0); /* 1 match */ @@ -2448,7 +2447,7 @@ static int B3gTestScan10 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0, 0); /* 1 match */ @@ -2475,7 +2474,7 @@ static int B3gTestScan11 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2503,7 +2502,7 @@ static int B3gTestScan12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddScanPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0, 0); /* 1 match */ @@ -2531,7 +2530,7 @@ static int B3gTestSearch01 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2558,7 +2557,7 @@ static int B3gTestSearch02 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abce", 4, 0, 0, 0, 0); /* 1 match */ @@ -2585,7 +2584,7 @@ static int B3gTestSearch03 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2615,7 +2614,7 @@ static int B3gTestSearch04 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2645,7 +2644,7 @@ static int B3gTestSearch05 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCI(&mpm_ctx, (uint8_t *)"ABCD", 4, 0, 0, 0, 0); /* 1 match */ @@ -2674,7 +2673,7 @@ static int B3gTestSearch06 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2701,7 +2700,7 @@ static int B3gTestSearch07 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ @@ -2734,7 +2733,7 @@ static int B3gTestSearch08 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2761,7 +2760,7 @@ static int B3gTestSearch09 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 0, 0); /* 1 match */ @@ -2788,7 +2787,7 @@ static int B3gTestSearch10 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcdefgh", 8, 0, 0, 0, 0); /* 1 match */ @@ -2815,7 +2814,7 @@ static int B3gTestSearch11 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0); /* 1 match */ @@ -2843,7 +2842,7 @@ static int B3gTestSearch12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_B3G); + MpmInitCtx(&mpm_ctx, MPM_B3G, -1); B3gCtx *ctx = (B3gCtx *)mpm_ctx.ctx; B3gAddPatternCS(&mpm_ctx, (uint8_t *)"wxyz", 4, 0, 0, 0, 0); /* 1 match */ @@ -2911,7 +2910,7 @@ int main () { #define R 4 int i; B3gCtx bg_ctx; - B3gInitCtx(&bg_ctx); + B3gInitCtx(&bg_ctx, -1); B3gAddPatternCI(&bg_ctx, "grep", 4, 0, 0, 0, 0); B2pPrepare(&bg_ctx); diff --git a/src/util-mpm-wumanber.c b/src/util-mpm-wumanber.c index ae7a1e9b24..88b24d420e 100644 --- a/src/util-mpm-wumanber.c +++ b/src/util-mpm-wumanber.c @@ -40,7 +40,7 @@ static uint32_t wm_hash_size = 0; static uint32_t wm_bloom_size = 0; -void WmInitCtx (MpmCtx *mpm_ctx); +void WmInitCtx (MpmCtx *mpm_ctx, int); void WmThreadInitCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx, uint32_t); void WmDestroyCtx(MpmCtx *mpm_ctx); void WmThreadDestroyCtx(MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx); @@ -2230,9 +2230,8 @@ void WmGetConfig() } } -void WmInitCtx (MpmCtx *mpm_ctx) -{ - SCLogDebug("mpm_ctx %p\n", mpm_ctx); +void WmInitCtx (MpmCtx *mpm_ctx, int module_handle) { + SCLogDebug("mpm_ctx %p", mpm_ctx); mpm_ctx->ctx = malloc(sizeof(WmCtx)); if (mpm_ctx->ctx == NULL) @@ -2436,7 +2435,7 @@ int WmTestInitCtx01 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - WmInitCtx(&mpm_ctx); + WmInitCtx(&mpm_ctx, -1); if (mpm_ctx.ctx != NULL) result = 1; @@ -2449,7 +2448,7 @@ int WmTestInitCtx02 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - WmInitCtx(&mpm_ctx); + WmInitCtx(&mpm_ctx, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; @@ -2464,7 +2463,7 @@ int WmTestInitCtx03 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); if (mpm_table[MPM_WUMANBER].Search == WmSearch) result = 1; @@ -2480,7 +2479,7 @@ int WmTestThreadInitCtx01 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); if (mpm_thread_ctx.memory_cnt == 2) @@ -2501,7 +2500,7 @@ int WmTestThreadInitCtx02 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); WmThreadCtx *tctx = (WmThreadCtx *)mpm_thread_ctx.ctx; @@ -2523,7 +2522,7 @@ int WmTestInitAddPattern01 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); int ret = WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 1234, 0, 0); @@ -2541,7 +2540,7 @@ int WmTestInitAddPattern02 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; @@ -2560,7 +2559,7 @@ int WmTestInitAddPattern03 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; @@ -2582,7 +2581,7 @@ int WmTestInitAddPattern04 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; @@ -2604,7 +2603,7 @@ int WmTestInitAddPattern05 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; @@ -2626,7 +2625,7 @@ int WmTestInitAddPattern06 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmThreadInitCtx(&mpm_ctx, &mpm_thread_ctx, 1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; @@ -2646,7 +2645,7 @@ int WmTestPrepare01 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"a", 1, 0, 0, 1, 0, 0, 0, 0); @@ -2663,7 +2662,7 @@ int WmTestPrepare02 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); WmPreparePatterns(&mpm_ctx); @@ -2680,7 +2679,7 @@ int WmTestPrepare03 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); WmPreparePatterns(&mpm_ctx); @@ -2699,7 +2698,7 @@ int WmTestPrepare04 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"a", 1, 0, 0, 1, 1, 0, 0, 0); @@ -2716,7 +2715,7 @@ int WmTestPrepare05 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 1, 0, 0, 0); WmPreparePatterns(&mpm_ctx); @@ -2733,7 +2732,7 @@ int WmTestPrepare06 (void) { int result = 0; MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 1, 0, 0, 0); WmPreparePatterns(&mpm_ctx); @@ -2754,7 +2753,7 @@ int WmTestSearch01 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2781,7 +2780,7 @@ int WmTestSearch01Hash12 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2810,7 +2809,7 @@ int WmTestSearch01Hash14 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2839,7 +2838,7 @@ int WmTestSearch01Hash15 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2868,7 +2867,7 @@ int WmTestSearch01Hash16 (void) { memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2896,7 +2895,7 @@ int WmTestSearch02 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2919,7 +2918,7 @@ int WmTestSearch03 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2943,7 +2942,7 @@ int WmTestSearch04 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"bcde", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2966,7 +2965,7 @@ int WmTestSearch05 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"efgh", 4, 0, 0, 1, 0, 0, 0, 0); @@ -2989,7 +2988,7 @@ int WmTestSearch06 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"eFgH", 4, 0, 0, 1, 0, 0, 0, 0); @@ -3012,7 +3011,7 @@ int WmTestSearch07 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcd", 4, 0, 0, 0, 0, 0, 0, 0); @@ -3036,7 +3035,7 @@ int WmTestSearch08 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"abcde", 5, 0, 0, 1, 0, 0, 0, 0); @@ -3060,7 +3059,7 @@ int WmTestSearch09 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"ab", 2, 0, 0, 1, 0, 0, 0, 0); @@ -3083,7 +3082,7 @@ int WmTestSearch10 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmAddPattern(&mpm_ctx, (uint8_t *)"bc", 2, 0, 0, 1, 0, 0, 0, 0); WmAddPattern(&mpm_ctx, (uint8_t *)"gh", 2, 0, 0, 1, 0, 1, 0, 0); @@ -3107,7 +3106,7 @@ int WmTestSearch11 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmAddPattern(&mpm_ctx, (uint8_t *)"a", 1, 0, 0, 1, 0, 0, 0, 0); WmAddPattern(&mpm_ctx, (uint8_t *)"d", 1, 0, 0, 1, 0, 1, 0, 0); @@ -3132,7 +3131,7 @@ int WmTestSearch12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 1, 0, 0, 0, 0); @@ -3159,7 +3158,7 @@ int WmTestSearch13 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"a", 1, 0, 0, 1, 0, 0, 0, 0); @@ -3186,7 +3185,7 @@ int WmTestSearch14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 1, 0, 0, 0, 0); @@ -3211,7 +3210,7 @@ int WmTestSearch15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 1, 0, 0, 0, 0); @@ -3239,7 +3238,7 @@ int WmTestSearch16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPattern(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 1, 0, 0, 0, 0); @@ -3267,7 +3266,7 @@ int WmTestSearch17 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3293,7 +3292,7 @@ int WmTestSearch18Hash12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3320,7 +3319,7 @@ int WmTestSearch18Hash14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3347,7 +3346,7 @@ int WmTestSearch18Hash15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3374,7 +3373,7 @@ int WmTestSearch18 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3401,7 +3400,7 @@ int WmTestSearch18Hash16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3428,7 +3427,7 @@ int WmTestSearch19 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCI(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3454,7 +3453,7 @@ int WmTestSearch19Hash12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCI(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3481,7 +3480,7 @@ int WmTestSearch19Hash14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCI(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3508,7 +3507,7 @@ int WmTestSearch19Hash15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCI(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3535,7 +3534,7 @@ int WmTestSearch19Hash16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCI(&mpm_ctx, (uint8_t *)"/VideoAccessCodecInstall.exe", 28, 0, 0, 0, 0); @@ -3562,7 +3561,7 @@ int WmTestSearch20 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3588,7 +3587,7 @@ int WmTestSearch20Hash12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3615,7 +3614,7 @@ int WmTestSearch20Hash14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3642,7 +3641,7 @@ int WmTestSearch20Hash15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3669,7 +3668,7 @@ int WmTestSearch20Hash16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3696,7 +3695,7 @@ int WmTestSearch21 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3722,7 +3721,7 @@ static int WmTestSearch21Hash12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3749,7 +3748,7 @@ static int WmTestSearch21Hash14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3776,7 +3775,7 @@ static int WmTestSearch21Hash15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3803,7 +3802,7 @@ static int WmTestSearch21Hash16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"/videoaccesscodecinstall.exe", 28, 0, 0, 0, 0); @@ -3830,7 +3829,7 @@ static int WmTestSearch22Hash9 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ @@ -3864,7 +3863,7 @@ static int WmTestSearch22Hash12 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ @@ -3898,7 +3897,7 @@ static int WmTestSearch22Hash14 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ @@ -3932,7 +3931,7 @@ static int WmTestSearch22Hash15 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ @@ -3966,7 +3965,7 @@ static int WmTestSearch22Hash16 (void) { MpmCtx mpm_ctx; memset(&mpm_ctx, 0x00, sizeof(MpmCtx)); MpmThreadCtx mpm_thread_ctx; - MpmInitCtx(&mpm_ctx, MPM_WUMANBER); + MpmInitCtx(&mpm_ctx, MPM_WUMANBER, -1); WmCtx *ctx = (WmCtx *)mpm_ctx.ctx; WmAddPatternCS(&mpm_ctx, (uint8_t *)"A", 1, 0, 0, 0, 0); /* should match 30 times */ diff --git a/src/util-mpm.c b/src/util-mpm.c index 7a22a718e1..81cf72117b 100644 --- a/src/util-mpm.c +++ b/src/util-mpm.c @@ -5,6 +5,7 @@ /* include pattern matchers */ #include "util-mpm-wumanber.h" #include "util-mpm-b2g.h" +#include "util-mpm-b2g-cuda.h" #include "util-mpm-b3g.h" #include "util-hashlist.h" @@ -282,9 +283,9 @@ void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher, uint32_t m mpm_table[matcher].InitThreadCtx(NULL, mpm_thread_ctx, max_id); } -void MpmInitCtx (MpmCtx *mpm_ctx, uint16_t matcher) { +void MpmInitCtx (MpmCtx *mpm_ctx, uint16_t matcher, int module_handle) { mpm_ctx->mpm_type = matcher; - mpm_table[matcher].InitCtx(mpm_ctx); + mpm_table[matcher].InitCtx(mpm_ctx, module_handle); } void MpmTableSetup(void) { @@ -292,6 +293,9 @@ void MpmTableSetup(void) { MpmWuManberRegister(); MpmB2gRegister(); +#ifdef __SC_CUDA_SUPPORT__ + MpmB2gCudaRegister(); +#endif MpmB3gRegister(); } diff --git a/src/util-mpm.h b/src/util-mpm.h index b6df4f3f97..92d23f045a 100644 --- a/src/util-mpm.h +++ b/src/util-mpm.h @@ -33,7 +33,9 @@ enum { MPM_WUMANBER, MPM_B2G, +#ifdef __SC_CUDA_SUPPORT__ MPM_B2G_CUDA, +#endif MPM_B3G, /* table size */ @@ -119,7 +121,7 @@ typedef struct MpmCtx_ { typedef struct MpmTableElmt_ { char *name; uint8_t max_pattern_length; - void (*InitCtx)(struct MpmCtx_ *); + void (*InitCtx)(struct MpmCtx_ *, int); void (*InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *, uint32_t); void (*DestroyCtx)(struct MpmCtx_ *); void (*DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *); @@ -157,7 +159,7 @@ void MpmRegisterTests(void); /** Return the max pattern length of a Matcher type given as arg */ int32_t MpmMatcherGetMaxPatternLength(uint16_t); -void MpmInitCtx (MpmCtx *mpm_ctx, uint16_t matcher); +void MpmInitCtx (MpmCtx *mpm_ctx, uint16_t matcher, int module_handle); void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t, uint32_t); uint32_t MpmGetHashSize(const char *); uint32_t MpmGetBloomSize(const char *);