htp: get/set memcap value

This adds new functions that will be called
through unix-socket and permit to update
and show memcap value.

The memcap value needs to be handled in a
thread safe way, so for this reason it is
declared as atomic var.
pull/3104/head
Giuseppe Longo 7 years ago committed by Victor Julien
parent 6fdad7d9e5
commit 85ddeb3afa

@ -38,8 +38,7 @@
#include "app-layer-htp-mem.h"
uint64_t htp_config_memcap = 0;
SC_ATOMIC_DECLARE(uint64_t, htp_config_memcap);
SC_ATOMIC_DECLARE(uint64_t, htp_memuse);
SC_ATOMIC_DECLARE(uint64_t, htp_memcap);
@ -47,19 +46,24 @@ void HTPParseMemcap()
{
const char *conf_val;
SC_ATOMIC_INIT(htp_config_memcap);
/** set config values for memcap, prealloc and hash_size */
uint64_t memcap;
if ((ConfGet("app-layer.protocols.http.memcap", &conf_val)) == 1)
{
if (ParseSizeStringU64(conf_val, &htp_config_memcap) < 0) {
if (ParseSizeStringU64(conf_val, &memcap) < 0) {
SCLogError(SC_ERR_SIZE_PARSE, "Error parsing http.memcap "
"from conf file - %s. Killing engine",
conf_val);
exit(EXIT_FAILURE);
} else {
SC_ATOMIC_SET(htp_config_memcap, memcap);
}
SCLogInfo("HTTP memcap: %"PRIu64, htp_config_memcap);
SCLogInfo("HTTP memcap: %"PRIu64, SC_ATOMIC_GET(htp_config_memcap));
} else {
/* default to unlimited */
htp_config_memcap = 0;
SC_ATOMIC_SET(htp_config_memcap, 0);
}
SC_ATOMIC_INIT(htp_memuse);
@ -98,12 +102,38 @@ uint64_t HTPMemcapGlobalCounter(void)
*/
static int HTPCheckMemcap(uint64_t size)
{
if (htp_config_memcap == 0 || size + SC_ATOMIC_GET(htp_memuse) <= htp_config_memcap)
uint64_t memcapcopy = SC_ATOMIC_GET(htp_config_memcap);
if (memcapcopy == 0 || size + SC_ATOMIC_GET(htp_memuse) <= memcapcopy)
return 1;
(void) SC_ATOMIC_ADD(htp_memcap, 1);
return 0;
}
/**
* \brief Update memcap value
*
* \param size new memcap value
*/
int HTPSetMemcap(uint64_t size)
{
if (size == 0 || (uint64_t)SC_ATOMIC_GET(htp_memuse) < size) {
SC_ATOMIC_SET(htp_config_memcap, size);
return 1;
}
return 0;
}
/**
* \brief Update memcap value
*
* \retval memcap value
*/
uint64_t HTPGetMemcap(void)
{
uint64_t memcapcopy = SC_ATOMIC_GET(htp_config_memcap);
return memcapcopy;
}
void *HTPMalloc(size_t size)
{
void *ptr = NULL;

@ -23,5 +23,8 @@ void *HTPCalloc(size_t n, size_t size);
void *HTPRealloc(void *ptr, size_t orig_size, size_t size);
void HTPFree(void *ptr, size_t size);
int HTPSetMemcap(uint64_t size);
uint64_t HTPGetMemcap(void);
uint64_t HTPMemuseGlobalCounter(void);
uint64_t HTPMemcapGlobalCounter(void);

Loading…
Cancel
Save