From d6d8a08a8f9770f66e2984fb39371f96c1edff51 Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Mon, 30 Dec 2013 16:14:54 +0100 Subject: [PATCH] htp layer: add memory usage counter This patch adds a memory counter for HTP memory usage. As there is no thread variables available in application layer the counter has been added to the TCP reassembly thread. --- src/app-layer-htp-mem.c | 8 ++++++++ src/app-layer-htp-mem.h | 5 +++++ src/app-layer.c | 3 +++ src/stream-tcp-reassemble.h | 3 +++ src/stream-tcp.c | 4 ++++ 5 files changed, 23 insertions(+) diff --git a/src/app-layer-htp-mem.c b/src/app-layer-htp-mem.c index b21e874ced..ca300c81ff 100644 --- a/src/app-layer-htp-mem.c +++ b/src/app-layer-htp-mem.c @@ -36,6 +36,8 @@ #include "util-mem.h" #include "util-misc.h" +#include "app-layer-htp-mem.h" + uint64_t htp_config_memcap = 0; SC_ATOMIC_DECLARE(uint64_t, htp_memuse); @@ -71,6 +73,12 @@ void HTPDecrMemuse(uint64_t size) return; } +void HTPMemuseCounter(ThreadVars *tv, TcpReassemblyThreadCtx *trt) +{ + uint64_t memusecopy = SC_ATOMIC_GET(htp_memuse); + SCPerfCounterSetUI64(trt->counter_htp_memuse, tv->sc_perf_pca, memusecopy); + return; +} /** * \brief Check if alloc'ing "size" would mean we're over memcap * diff --git a/src/app-layer-htp-mem.h b/src/app-layer-htp-mem.h index 9cf1f18f40..54b21f5d27 100644 --- a/src/app-layer-htp-mem.h +++ b/src/app-layer-htp-mem.h @@ -15,7 +15,12 @@ * 02110-1301, USA. */ +#include "stream-tcp-reassemble.h" + void HTPParseMemcap(); void *HTPMalloc(size_t size); void *HTPRealloc(void *ptr, size_t orig_size, size_t size); void HTPFree(void *ptr, size_t size); + + +void HTPMemuseCounter(ThreadVars *tv, TcpReassemblyThreadCtx *trt); diff --git a/src/app-layer.c b/src/app-layer.c index 29d97b8d2e..9a63314690 100644 --- a/src/app-layer.c +++ b/src/app-layer.c @@ -41,6 +41,7 @@ #include "util-profiling.h" #include "util-validate.h" #include "decode-events.h" +#include "app-layer-htp-mem.h" /** * \brief This is for the app layer in general and it contains per thread @@ -341,6 +342,8 @@ int AppLayerHandleTCPData(ThreadVars *tv, TcpReassemblyThreadCtx *ra_ctx, } } + /** \fixme a bit hacky but will be improved in 2.1 */ + HTPMemuseCounter(tv, ra_ctx); goto end; failure: r = -1; diff --git a/src/stream-tcp-reassemble.h b/src/stream-tcp-reassemble.h index 281a50d5f2..5ae43960e0 100644 --- a/src/stream-tcp-reassemble.h +++ b/src/stream-tcp-reassemble.h @@ -62,6 +62,9 @@ typedef struct TcpReassemblyThreadCtx_ { uint16_t counter_tcp_reass_memuse; /** count number of streams with a unrecoverable stream gap (missing pkts) */ uint16_t counter_tcp_reass_gap; + /** account memory usage by suricata to handle HTTP protocol (not counting + * libhtp memory usage)*/ + uint16_t counter_htp_memuse; } TcpReassemblyThreadCtx; #define OS_POLICY_DEFAULT OS_POLICY_BSD diff --git a/src/stream-tcp.c b/src/stream-tcp.c index f004b602c1..f8a8947a4d 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -4559,6 +4559,10 @@ TmEcode StreamTcpThreadInit(ThreadVars *tv, void *initdata, void **data) stt->ra_ctx->counter_tcp_reass_gap = SCPerfTVRegisterCounter("tcp.reassembly_gap", tv, SC_PERF_TYPE_UINT64, "NULL"); + /** \fixme Find a better place in 2.1 as it is linked with app layer */ + stt->ra_ctx->counter_htp_memuse = SCPerfTVRegisterCounter("http.memuse", tv, + SC_PERF_TYPE_UINT64, + "NULL"); SCLogDebug("StreamTcp thread specific ctx online at %p, reassembly ctx %p", stt, stt->ra_ctx);