]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Various small flow and host table fixes.
authorVictor Julien <victor@inliniac.net>
Thu, 22 Mar 2012 10:53:35 +0000 (11:53 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 22 Mar 2012 11:41:19 +0000 (12:41 +0100)
src/flow-hash.c
src/flow-manager.c
src/flow-util.c
src/flow-util.h
src/flow.c
src/host.c
src/host.h
src/util-error.c
src/util-error.h

index 0b6fcb2e7dcd3e851c319cfcde4b8ea0f0948903..3c8604b46c22c80154ef78c307f717b901e6ec9f 100644 (file)
@@ -309,7 +309,7 @@ static Flow *FlowGetNew(Packet *p) {
     f = FlowDequeue(&flow_spare_q);
     if (f == NULL) {
         /* If we reached the max memcap, we get a used flow */
-        if ((SC_ATOMIC_GET(flow_memuse) + sizeof(Flow)) > flow_config.memcap) {
+        if (!(FLOW_CHECK_MEMCAP(sizeof(Flow)))) {
             /* declare state of emergency */
             if (!(SC_ATOMIC_GET(flow_flags) & FLOW_EMERGENCY)) {
                 SC_ATOMIC_OR(flow_flags, FLOW_EMERGENCY);
index c13bd67acdb1758f440133e31a86cb3cfd8f98c5..e6dabceadeaee59dd7c5d6fea763d06002ec5c65 100644 (file)
@@ -800,7 +800,7 @@ static int FlowMgrTest05 (void) {
     UTHBuildPacketOfFlows(ini, end, 0);
 
     /* And now let's try to reach the memcap val */
-    while (SC_ATOMIC_GET(flow_memuse) + sizeof(Flow) < flow_config.memcap) {
+    while (FLOW_CHECK_MEMCAP(sizeof(Flow))) {
         ini = end + 1;
         end = end + 2;
         UTHBuildPacketOfFlows(ini, end, 0);
index 71016c997a6d0f59043751937476d2a74a09d107..bfa6ebcbe31e63f921c53f935109431e3583d3b0 100644 (file)
@@ -49,7 +49,7 @@ Flow *FlowAlloc(void)
 {
     Flow *f;
 
-    if ((SC_ATOMIC_GET(flow_memuse) + sizeof(Flow)) > flow_config.memcap) {
+    if (!(FLOW_CHECK_MEMCAP(sizeof(Flow)))) {
         return NULL;
     }
 
index a8d4dd58206fce965a0c5108fe8211d53d40cb50..b1315e714de6e21ebe7babe6f921ac159ba29caa 100644 (file)
         (f)->tag_list = NULL; \
     } while(0)
 
+/** \brief check if a memory alloc would fit in the memcap
+ *
+ *  \param size memory allocation size to check
+ *
+ *  \retval 1 it fits
+ *  \retval 0 no fit
+ */
+#define FLOW_CHECK_MEMCAP(size) \
+    ((((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)(size)) <= flow_config.memcap))
+
 Flow *FlowAlloc(void);
 Flow *FlowAllocDirect(void);
 void FlowFree(Flow *);
index c11517b587f5777c2b6cdd293755afd376bafbaa..2bc884a329e6d51181b9bcb06205cd461e4185c0 100644 (file)
@@ -413,16 +413,20 @@ void FlowInitConfig(char quiet)
 
     /* pre allocate flows */
     for (i = 0; i < flow_config.prealloc; i++) {
-        if ((SC_ATOMIC_GET(flow_memuse) + sizeof(Flow)) > flow_config.memcap) {
-            printf("ERROR: FlowAlloc failed (max flow memcap reached): %s\n", strerror(errno));
-            exit(1);
+        if (!(FLOW_CHECK_MEMCAP(sizeof(Flow)))) {
+            SCLogError(SC_ERR_FLOW_INIT, "preallocating flows failed: "
+                    "max flow memcap reached. Memcap %"PRIu64", "
+                    "Memuse %"PRIu64".", flow_config.memcap,
+                    ((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)sizeof(Flow)));
+            exit(EXIT_FAILURE);
         }
 
         Flow *f = FlowAlloc();
         if (f == NULL) {
-            printf("ERROR: FlowAlloc failed: %s\n", strerror(errno));
-            exit(1);
+            SCLogError(SC_ERR_FLOW_INIT, "preallocating flow failed: %s", strerror(errno));
+            exit(EXIT_FAILURE);
         }
+
         FlowEnqueue(&flow_spare_q,f);
     }
 
@@ -947,7 +951,7 @@ static int FlowTest07 (void) {
     UTHBuildPacketOfFlows(ini, end, 0);
 
     /* And now let's try to reach the memcap val */
-    while (SC_ATOMIC_GET(flow_memuse) + sizeof(Flow) < flow_config.memcap) {
+    while (FLOW_CHECK_MEMCAP(sizeof(Flow))) {
         ini = end + 1;
         end = end + 2;
         UTHBuildPacketOfFlows(ini, end, 0);
@@ -994,7 +998,7 @@ static int FlowTest08 (void) {
     UTHBuildPacketOfFlows(ini, end, 0);
 
     /* And now let's try to reach the memcap val */
-    while (SC_ATOMIC_GET(flow_memuse) + sizeof(Flow) < flow_config.memcap) {
+    while (FLOW_CHECK_MEMCAP(sizeof(Flow))) {
         ini = end + 1;
         end = end + 2;
         UTHBuildPacketOfFlows(ini, end, 0);
@@ -1041,7 +1045,7 @@ static int FlowTest09 (void) {
     UTHBuildPacketOfFlows(ini, end, 0);
 
     /* And now let's try to reach the memcap val */
-    while (SC_ATOMIC_GET(flow_memuse) + sizeof(Flow) < flow_config.memcap) {
+    while (FLOW_CHECK_MEMCAP(sizeof(Flow))) {
         ini = end + 1;
         end = end + 2;
         UTHBuildPacketOfFlows(ini, end, 0);
index 2e24c8a31a4be512c2f4e812c34cfebad1ea3770..5737d5a1fce1bec6f9158181c3065727b7dc5f98 100644 (file)
@@ -53,7 +53,7 @@ void HostMoveToSpare(Host *h) {
 }
 
 Host *HostAlloc(void) {
-    if ((SC_ATOMIC_GET(host_memuse) + sizeof(Host)) > host_config.memcap) {
+    if (!(HOST_CHECK_MEMCAP(sizeof(Host)))) {
         return NULL;
     }
 
@@ -119,6 +119,7 @@ void HostInitConfig(char quiet)
 
     memset(&host_config,  0, sizeof(host_config));
     //SC_ATOMIC_INIT(flow_flags);
+    SC_ATOMIC_INIT(host_counter);
     SC_ATOMIC_INIT(host_memuse);
     SC_ATOMIC_INIT(host_prune_idx);
     HostQueueInit(&host_spare_q);
@@ -187,15 +188,18 @@ void HostInitConfig(char quiet)
 
     /* pre allocate hosts */
     for (i = 0; i < host_config.prealloc; i++) {
-        if ((SC_ATOMIC_GET(host_memuse) + sizeof(Host)) > host_config.memcap) {
-            printf("ERROR: HostAlloc failed (max host memcap reached): %s\n", strerror(errno));
-            exit(1);
+        if (!(HOST_CHECK_MEMCAP(sizeof(Host)))) {
+            SCLogError(SC_ERR_HOST_INIT, "preallocating hosts failed: "
+                    "max host memcap reached. Memcap %"PRIu64", "
+                    "Memuse %"PRIu64".", host_config.memcap,
+                    ((uint64_t)SC_ATOMIC_GET(host_memuse) + (uint64_t)sizeof(Host)));
+            exit(EXIT_FAILURE);
         }
 
         Host *h = HostAlloc();
         if (h == NULL) {
-            printf("ERROR: HostAlloc failed: %s\n", strerror(errno));
-            exit(1);
+            SCLogError(SC_ERR_HOST_INIT, "preallocating host failed: %s", strerror(errno));
+            exit(EXIT_FAILURE);
         }
         HostEnqueue(&host_spare_q,h);
     }
@@ -257,6 +261,7 @@ void HostShutdown(void)
 
     SC_ATOMIC_DESTROY(host_prune_idx);
     SC_ATOMIC_DESTROY(host_memuse);
+    SC_ATOMIC_DESTROY(host_counter);
     //SC_ATOMIC_DESTROY(flow_flags);
     return;
 }
@@ -306,7 +311,7 @@ static Host *HostGetNew(Address *a) {
     h = HostDequeue(&host_spare_q);
     if (h == NULL) {
         /* If we reached the max memcap, we get a used host */
-        if ((SC_ATOMIC_GET(host_memuse) + sizeof(Host)) > host_config.memcap) {
+        if (!(HOST_CHECK_MEMCAP(sizeof(Host)))) {
             /* declare state of emergency */
             //if (!(SC_ATOMIC_GET(host_flags) & HOST_EMERGENCY)) {
             //    SC_ATOMIC_OR(host_flags, HOST_EMERGENCY);
@@ -349,7 +354,6 @@ static Host *HostGetNew(Address *a) {
     (h)->use_cnt--
 
 void HostInit(Host *h, Address *a) {
-//    SCMutexLock(&h->m);
     COPY_ADDRESS(a, &h->a);
     HostIncrUsecnt(h);
 }
index a4603e5b2d5aab1729fa79d5d45c45c5e7025f43..e3610b81294509d2c98ea6abb34a273273a9cee3 100644 (file)
@@ -96,6 +96,16 @@ typedef struct HostConfig_ {
     uint32_t prealloc;
 } HostConfig;
 
+/** \brief check if a memory alloc would fit in the memcap
+ *
+ *  \param size memory allocation size to check
+ *
+ *  \retval 1 it fits
+ *  \retval 0 no fit
+ */
+#define HOST_CHECK_MEMCAP(size) \
+    ((((uint64_t)SC_ATOMIC_GET(host_memuse) + (uint64_t)(size)) <= host_config.memcap))
+
 HostConfig host_config;
 SC_ATOMIC_DECLARE(unsigned long long int,host_memuse);
 SC_ATOMIC_DECLARE(unsigned int,host_counter);
index 450fbba0df75d1e4aef5eceec85e229c458e865d..e39131cc0404f2ceea6bf33258af60595a080432 100644 (file)
@@ -223,6 +223,8 @@ const char * SCErrorToString(SCError err)
         CASE_CODE (SC_WARN_OUTDATED_LIBHTP);
         CASE_CODE (SC_WARN_DEPRECATED);
         CASE_CODE (SC_WARN_PROFILE);
+        CASE_CODE (SC_ERR_FLOW_INIT);
+        CASE_CODE (SC_ERR_HOST_INIT);
 
         default:
             return "UNKNOWN_ERROR";
index ee0063a869634dde7c92f81337c5b0f12fd1bf01..1bd80a709688bce3a7b9d0d758a5610d57f11ac1 100644 (file)
@@ -238,6 +238,8 @@ typedef enum {
     SC_WARN_OUTDATED_LIBHTP,
     SC_WARN_DEPRECATED,
     SC_WARN_PROFILE,
+    SC_ERR_FLOW_INIT,
+    SC_ERR_HOST_INIT,
 } SCError;
 
 const char *SCErrorToString(SCError);