From 1389cf691392bd61dca0b8594d6dd0e7d6c5836e Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Mon, 26 Dec 2011 20:38:18 +0530 Subject: [PATCH] update cuda mpm to support per proto mpm contexts. Fix faulty stream mpm usage of cuda --- src/cuda-packet-batcher.c | 17 ++++++++--- src/detect-engine-mpm.c | 59 +++++++++++++-------------------------- src/util-mpm.c | 6 ++-- src/util-mpm.h | 2 +- 4 files changed, 36 insertions(+), 48 deletions(-) diff --git a/src/cuda-packet-batcher.c b/src/cuda-packet-batcher.c index 21307faee7..cf3eb94e8c 100644 --- a/src/cuda-packet-batcher.c +++ b/src/cuda-packet-batcher.c @@ -706,13 +706,22 @@ TmEcode SCCudaPBBatchPackets(ThreadVars *tv, Packet *p, void *data, PacketQueue return TM_ECODE_OK; } + MpmCtx *mpm_ctx = NULL; + if (p->proto == IPPROTO_TCP) { + mpm_ctx = sgh->mpm_proto_tcp_ctx; + } else if (p->proto == IPPROTO_UDP) { + mpm_ctx = sgh->mpm_proto_udp_ctx; + } else { + mpm_ctx = sgh->mpm_proto_other_ctx; + } + /* if one of these conditions fail we don't have to run the mpm on this * packet. Firstly if the payload_len is == 0, we don't have a payload * to match against. Next if we don't have a mpm_context against this * sgh, indicating we don't have any patterns in this sgh, again we don't * have anything to run the PM against. Finally if the flow doesn't want * to analyze packets for this flow, we can chuck this packet out as well */ - if ( !(p->payload_len > 0 && sgh->mpm_ctx != NULL && + if ( !(p->payload_len > 0 && mpm_ctx != NULL && !(p->flags & PKT_NOPAYLOAD_INSPECTION)) ) { SCLogDebug("Either p->payload_len <= 0 or mpm_ctx for the packet is NULL " "or PKT_NOPAYLOAD_INSPECTION set for this packet"); @@ -720,7 +729,7 @@ TmEcode SCCudaPBBatchPackets(ThreadVars *tv, Packet *p, void *data, PacketQueue } /* the cuda b2g context */ - B2gCudaCtx *ctx = sgh->mpm_ctx->ctx; + B2gCudaCtx *ctx = mpm_ctx->ctx; /* if we have a 1 byte search kernel set we don't buffer this packet for * cuda matching and instead run this non-cuda mpm function to be run on @@ -767,8 +776,8 @@ TmEcode SCCudaPBBatchPackets(ThreadVars *tv, Packet *p, void *data, PacketQueue /* store the data in the packets_buffer for this packet, which would be passed * over to the GPU for processing */ - curr_packet->m = ((B2gCudaCtx *)(sgh->mpm_ctx->ctx))->m; - curr_packet->table = ((B2gCudaCtx *)(sgh->mpm_ctx->ctx))->cuda_B2G; + curr_packet->m = ((B2gCudaCtx *)(mpm_ctx->ctx))->m; + curr_packet->table = ((B2gCudaCtx *)(mpm_ctx->ctx))->cuda_B2G; curr_packet->payload_len = p->payload_len; memcpy(curr_packet->payload, p->payload, p->payload_len); diff --git a/src/detect-engine-mpm.c b/src/detect-engine-mpm.c index 3e1bb01297..c054ac3ad9 100644 --- a/src/detect-engine-mpm.c +++ b/src/detect-engine-mpm.c @@ -168,31 +168,9 @@ uint32_t PacketPatternSearchWithStreamCtx(DetectEngineThreadCtx *det_ctx, uint32_t ret; -#ifndef __SC_CUDA_SUPPORT__ ret = mpm_table[det_ctx->sgh->mpm_stream_ctx->mpm_type]. Search(det_ctx->sgh->mpm_stream_ctx, &det_ctx->mtc, &det_ctx->pmq, p->payload, p->payload_len); -#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_stream_ctx->mpm_type != MPM_B2G_CUDA) { - ret = mpm_table[det_ctx->sgh->mpm_stream_ctx->mpm_type]. - Search(det_ctx->sgh->mpm_stream_ctx, &det_ctx->mtc, &det_ctx->pmq, - p->payload, p->payload_len); - SCReturnInt(ret); - } - - if (p->cuda_mpm_enabled) { - ret = B2gCudaResultsPostProcessing(p, det_ctx->sgh->mpm_stream_ctx, - &det_ctx->mtc, &det_ctx->pmq); - } else { - ret = mpm_table[det_ctx->sgh->mpm_stream_ctx->mpm_type]. - Search(det_ctx->sgh->mpm_stream_ctx, &det_ctx->mtc, &det_ctx->pmq, - p->payload, p->payload_len); - } - -#endif SCReturnInt(ret); } @@ -211,8 +189,6 @@ uint32_t PacketPatternSearch(DetectEngineThreadCtx *det_ctx, Packet *p) uint32_t ret; MpmCtx *mpm_ctx = NULL; - -#ifndef __SC_CUDA_SUPPORT__ if (p->proto == IPPROTO_TCP) { mpm_ctx = det_ctx->sgh->mpm_proto_tcp_ctx; } else if (p->proto == IPPROTO_UDP) { @@ -224,6 +200,7 @@ uint32_t PacketPatternSearch(DetectEngineThreadCtx *det_ctx, Packet *p) if (mpm_ctx == NULL) SCReturnInt(0); +#ifndef __SC_CUDA_SUPPORT__ ret = mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, @@ -233,26 +210,25 @@ uint32_t PacketPatternSearch(DetectEngineThreadCtx *det_ctx, Packet *p) /* 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) { - 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); + if (mpm_ctx->mpm_type != MPM_B2G_CUDA) { + ret = mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx, + &det_ctx->mtc, + &det_ctx->pmq, + p->payload, + p->payload_len); SCReturnInt(ret); } if (p->cuda_mpm_enabled) { - ret = B2gCudaResultsPostProcessing(p, det_ctx->sgh->mpm_ctx, - &det_ctx->mtc, &det_ctx->pmq); + ret = B2gCudaResultsPostProcessing(p, mpm_ctx, &det_ctx->mtc, + &det_ctx->pmq); } else { - 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); + ret = mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx, + &det_ctx->mtc, + &det_ctx->pmq, + p->payload, + p->payload_len); } - #endif SCReturnInt(ret); @@ -564,8 +540,9 @@ void PatternMatchDestroyGroup(SigGroupHead *sh) { /* content */ if (sh->flags & SIG_GROUP_HAVECONTENT && !(sh->flags & SIG_GROUP_HEAD_MPM_COPY)) { - SCLogDebug("destroying mpm_ctx %p (sh %p)", sh->mpm_ctx, sh); + SCLogDebug("destroying mpm_ctx %p (sh %p)", + sh->mpm_proto_tcp_ctx, sh); if (sh->mpm_proto_tcp_ctx != NULL && !MpmFactoryIsMpmCtxAvailable(sh->mpm_proto_tcp_ctx)) { mpm_table[sh->mpm_proto_tcp_ctx->mpm_type]. @@ -575,6 +552,8 @@ void PatternMatchDestroyGroup(SigGroupHead *sh) { /* ready for reuse */ sh->mpm_proto_tcp_ctx = NULL; + SCLogDebug("destroying mpm_ctx %p (sh %p)", + sh->mpm_proto_udp_ctx, sh); if (sh->mpm_proto_udp_ctx != NULL && !MpmFactoryIsMpmCtxAvailable(sh->mpm_proto_udp_ctx)) { mpm_table[sh->mpm_proto_udp_ctx->mpm_type]. @@ -584,6 +563,8 @@ void PatternMatchDestroyGroup(SigGroupHead *sh) { /* ready for reuse */ sh->mpm_proto_udp_ctx = NULL; + SCLogDebug("destroying mpm_ctx %p (sh %p)", + sh->mpm_proto_other_ctx, sh); if (sh->mpm_proto_other_ctx != NULL && !MpmFactoryIsMpmCtxAvailable(sh->mpm_proto_other_ctx)) { mpm_table[sh->mpm_proto_other_ctx->mpm_type]. diff --git a/src/util-mpm.c b/src/util-mpm.c index 4100f65093..6d3b40b239 100644 --- a/src/util-mpm.c +++ b/src/util-mpm.c @@ -41,6 +41,7 @@ #include "detect-engine.h" #include "util-cuda-handlers.h" #include "util-cuda.h" +#include "util-misc.h" #include "conf.h" #include "conf-yaml-loader.h" #include "queue.h" @@ -541,15 +542,12 @@ MpmCudaConf *MpmCudaConfParse(void) SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "Invalid entry for " "cuda.mpm.packet_size_limit. Either NULL or empty"); } else { - long double res; - if (ParseSizeString(packet_size_limit, &res) < 0) { + if (ParseSizeStringU16(packet_size_limit, &profile->packet_size_limit) < 0) { SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "Invalid entry for " "cuda.mpm.packet_size_limit - %s", packet_size_limit); exit(EXIT_FAILURE); } - profile->packet_size_limit = res; - if (profile->packet_size_limit <= 0) { SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "Invalid entry for " "cuda.mpm.packet_size_limit - %s", packet_size_limit); diff --git a/src/util-mpm.h b/src/util-mpm.h index 764c8405d4..a3f5dbd1e4 100644 --- a/src/util-mpm.h +++ b/src/util-mpm.h @@ -195,7 +195,7 @@ int32_t MpmFactoryIsMpmCtxAvailable(MpmCtx *); */ typedef struct MpmCudaConf_ { int32_t packet_buffer_limit; - int16_t packet_size_limit; + uint16_t packet_size_limit; int8_t packet_buffers; double batching_timeout; int8_t page_locked;