]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
htp: get/set memcap value
authorGiuseppe Longo <glongo@stamus-networks.com>
Mon, 20 Nov 2017 14:06:54 +0000 (15:06 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 19 Dec 2017 10:18:57 +0000 (11:18 +0100)
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.

src/app-layer-htp-mem.c
src/app-layer-htp-mem.h

index 37ac6db1c5099f4dd904903d11a625da18d734cb..a6b37ac20f540f0e06c4bd308b1d22b2a31243e3 100644 (file)
@@ -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;
index 8ac36057b6893fdc3473a074aa28aecd97a98675..414b48821a74719e7b48963209fac88f300d80ce 100644 (file)
@@ -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);