From 30940c9a946911bcaece6632bcbf9b8a2b916531 Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Tue, 16 Feb 2010 19:45:27 +0530 Subject: [PATCH] pack all the packet pattern scan and search packet setup for cuda into a function inside util-cuda-handlers.[ch] --- src/detect-engine-mpm.c | 90 ++++++---------------------------------- src/util-cuda-handlers.c | 73 ++++++++++++++++++++++++++++++++ src/util-cuda-handlers.h | 5 +++ 3 files changed, 91 insertions(+), 77 deletions(-) diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index b319ab2d85..1265971a70 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -18,10 +18,9 @@ #include "detect-content.h" #include "detect-uricontent.h" +#include "util-cuda-handlers.h" #include "util-mpm-b2g-cuda.h" -#include "tmqh-simple.h" - #include "util-enum.h" #include "util-debug.h" @@ -82,20 +81,18 @@ uint32_t PacketPatternScan(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, det_ctx->pmq.mode = PMQ_MODE_SCAN; -#ifndef __SC_CUDA_SUPPORT__ uint32_t ret; +#ifndef __SC_CUDA_SUPPORT__ 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 /* if the user has enabled cuda support, but is not using the cuda mpm * algo, then we shouldn't take the path of the dispatcher. Call the mpm * directly */ if (det_ctx->sgh->mpm_ctx->mpm_type != MPM_B2G_CUDA) { - 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, @@ -104,20 +101,10 @@ uint32_t PacketPatternScan(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, SCReturnInt(ret); } - p->cuda_search = 0; - p->cuda_mpm_ctx = det_ctx->sgh->mpm_ctx; - p->cuda_mtc = &det_ctx->mtc; - p->cuda_pmq = &det_ctx->pmq; - /* this outq is unique to this detection thread instance. The dispatcher thread - * would use this queue to pump the packets back to this detection thread once - * it has processed the packet */ - p->cuda_outq = &trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]; - B2gCudaPushPacketTo_tv_CMB2_RC(p); - /* wait on the detection thread instance */ - Packet *out_p = TmqhInputSimpleOnQ(&trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]); - SCReturnInt(out_p->cuda_matches); + SCCudaHlProcessPacketWithDispatcher(p, det_ctx, /* scan */ 0, &ret); #endif + SCReturnInt(ret); } /** \brief Uri Pattern match, scan part -- searches for only 'scan' patterns, @@ -138,7 +125,6 @@ uint32_t UriPatternScan(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, 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 /* if the user has enabled cuda support, but is not using the cuda mpm * algo, then we shouldn't take the path of the dispatcher. Call the mpm @@ -150,28 +136,10 @@ uint32_t UriPatternScan(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, SCReturnInt(ret); } - Packet *p = malloc(sizeof(Packet)); - if (p == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); - exit(EXIT_FAILURE); - } - memset(p, 0, sizeof(Packet)); - - 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; - /* this outq is unique to this detection thread instance. The dispatcher thread - * would use this queue to pump the packets back to this detection thread once - * it has processed the packet */ - p->cuda_outq = &trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]; - B2gCudaPushPacketTo_tv_CMB2_RC(p); - Packet *out_p = TmqhInputSimpleOnQ(&trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]); - ret = out_p->cuda_matches; - free(p); - SCReturnInt(ret); + SCCudaHlProcessUriWithDispatcher(uri, uri_len, det_ctx, /* scan */ 0, &ret); #endif + + SCReturnInt(ret); } /** \brief Pattern match, search part -- searches for all other patterns @@ -186,20 +154,18 @@ uint32_t PacketPatternMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, det_ctx->pmq.mode = PMQ_MODE_SEARCH; -#ifndef __SC_CUDA_SUPPORT__ uint32_t ret; +#ifndef __SC_CUDA_SUPPORT__ 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 /* if the user has enabled cuda support, but is not using the cuda mpm * algo, then we shouldn't take the path of the dispatcher. Call the mpm * directly */ if (det_ctx->sgh->mpm_ctx->mpm_type != MPM_B2G_CUDA) { - 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, @@ -208,18 +174,10 @@ uint32_t PacketPatternMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, SCReturnInt(ret); } - p->cuda_search = 1; - p->cuda_mpm_ctx = det_ctx->sgh->mpm_ctx; - p->cuda_mtc = &det_ctx->mtc; - p->cuda_pmq = &det_ctx->pmq; - /* this outq is unique to this detection thread instance. The dispatcher thread - * would use this queue to pump the packets back to this detection thread once - * it has processed the packet */ - p->cuda_outq = &trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]; - B2gCudaPushPacketTo_tv_CMB2_RC(p); - Packet *out_p = TmqhInputSimpleOnQ(&trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]); - SCReturnInt(out_p->cuda_matches); + SCCudaHlProcessPacketWithDispatcher(p, det_ctx, /* search */ 1, &ret); #endif + + SCReturnInt(ret); } /** \brief Uri Pattern match, search part -- searches for all other patterns @@ -239,7 +197,6 @@ uint32_t UriPatternMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, 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 /* if the user has enabled cuda support, but is not using the cuda mpm * algo, then we shouldn't take the path of the dispatcher. Call the mpm @@ -251,31 +208,10 @@ uint32_t UriPatternMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, SCReturnInt(ret); } - Packet *p = malloc(sizeof(Packet)); - if (p == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); - exit(EXIT_FAILURE); - } - memset(p, 0, sizeof(Packet)); - - 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; - p->payload = uri; - p->payload_len = uri_len; - /* this outq is unique to this detection thread instance. The dispatcher thread - * would use this queue to pump the packets back to this detection thread once - * it has processed the packet */ - p->cuda_outq = &trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]; - B2gCudaPushPacketTo_tv_CMB2_RC(p); - Packet *out_p = TmqhInputSimpleOnQ(&trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]); - ret = out_p->cuda_matches; - free(p); - SCReturnInt(ret); + SCCudaHlProcessUriWithDispatcher(uri, uri_len, det_ctx, /* search */ 1, &ret); #endif - //printf("PacketPatternMatch: ret %" PRIu32 "\n", ret); + SCReturnInt(ret); } /** \brief cleans up the mpm instance after a match */ diff --git a/src/util-cuda-handlers.c b/src/util-cuda-handlers.c index 3e57fce115..1576df52c3 100644 --- a/src/util-cuda-handlers.c +++ b/src/util-cuda-handlers.c @@ -46,6 +46,9 @@ #include "util-cuda.h" #include "util-cuda-handlers.h" +#include "util-mpm-b2g-cuda.h" + +#include "tmqh-simple.h" #include "util-error.h" #include "util-debug.h" @@ -659,4 +662,74 @@ int SCCudaHlTestEnvCudaContextDeInit(void) return 1; } +void SCCudaHlProcessPacketWithDispatcher(Packet *p, DetectEngineThreadCtx *det_ctx, + uint8_t search, void *result) +{ + Packet *out_p = NULL; + + p->cuda_search = search; + p->cuda_mpm_ctx = det_ctx->sgh->mpm_ctx; + p->cuda_mtc = &det_ctx->mtc; + p->cuda_pmq = &det_ctx->pmq; + /* this outq is unique to this detection thread instance. The dispatcher thread + * would use this queue to pump the packets back to this detection thread once + * it has processed the packet */ + p->cuda_outq = &trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]; + /* for now it is hardcoded. \todo Make the access to the right queue or the + * ThreadVars generic */ + + /* Push the packet into the dispatcher's input queue */ + B2gCudaPushPacketTo_tv_CMB2_RC(p); + + /* wait for the dispatcher to process and return the packet we pushed */ + out_p = TmqhInputSimpleOnQ(&trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]); + + /* todo make this generic, so that if we have more than 2 modules using the + * cuda interface, we can call update function for the module that has + * queued the packet and retrieve the results */ + *((uint32_t *)result) = p->cuda_matches; + + return; +} + +void SCCudaHlProcessUriWithDispatcher(uint8_t *uri, uint16_t uri_len, + DetectEngineThreadCtx *det_ctx, + uint8_t search, void *result) +{ + Packet *out_p = NULL; + + Packet *p = malloc(sizeof(Packet)); + if (p == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); + exit(EXIT_FAILURE); + } + memset(p, 0, sizeof(Packet)); + + p->cuda_search = search; + 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; + /* this outq is unique to this detection thread instance. The dispatcher thread + * would use this queue to pump the packets back to this detection thread once + * it has processed the packet */ + p->cuda_outq = &trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]; + + /* Push the packet into the dispatcher's input queue */ + B2gCudaPushPacketTo_tv_CMB2_RC(p); + + /* wait for the dispatcher to process and return the packet we pushed */ + out_p = TmqhInputSimpleOnQ(&trans_q[det_ctx->cuda_mpm_rc_disp_outq->id]); + + /* todo make this generic, so that if we have more than 2 modules using the + * cuda interface, we can call update function for the module that has + * queued the packet and retrieve the results */ + *((uint32_t *)result) = p->cuda_matches; + + free(p); + + return; +} + #endif /* __SC_CUDA_SUPPORT */ diff --git a/src/util-cuda-handlers.h b/src/util-cuda-handlers.h index 79bfa945c4..ed94abab16 100644 --- a/src/util-cuda-handlers.h +++ b/src/util-cuda-handlers.h @@ -62,6 +62,11 @@ int SCCudaHlPushCudaContextFromModule(const char *); int SCCudaHlTestEnvCudaContextInit(void); int SCCudaHlTestEnvCudaContextDeInit(void); +void SCCudaHlProcessPacketWithDispatcher(Packet *, DetectEngineThreadCtx *, + uint8_t, void *); +void SCCudaHlProcessUriWithDispatcher(uint8_t *, uint16_t, DetectEngineThreadCtx *, + uint8_t, void *); + #endif /* __UTIL_CUDA_HANDLERS__ */ #endif /* __SC_CUDA_SUPPORT__ */