]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
flow/spare: implement pool shrinking 5281/head
authorVictor Julien <victor@inliniac.net>
Thu, 6 Aug 2020 12:38:36 +0000 (14:38 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 6 Aug 2020 14:23:28 +0000 (16:23 +0200)
Remove at most one block per run, so it shrinks slowly.

src/flow-manager.c
src/flow-spare-pool.c

index 905f57ed7d4b07649b7db57b0bbf6d2997bc034b..eacf7fe3fedf515e40b5ac9cf32aa7081bf392c4 100644 (file)
@@ -872,7 +872,7 @@ static TmEcode FlowManager(ThreadVars *th_v, void *thread_data)
                 const uint32_t sq_len = FlowSpareGetPoolSize();
                 const uint32_t spare_perc = sq_len * 100 / flow_config.prealloc;
                 /* see if we still have enough spare flows */
-                if (spare_perc < 90) {
+                if (spare_perc < 90 || spare_perc > 110) {
                     FlowSparePoolUpdate(sq_len);
                 }
             }
index 4f72706baee21a601cc25a8c2a3888918c27ec24..ff8402bb4076c175eafdcefaa75e1c650b4d0fae 100644 (file)
@@ -173,7 +173,28 @@ void FlowSparePoolUpdate(uint32_t size)
 {
     const int64_t todo = (int64_t)flow_config.prealloc - (int64_t)size;
     if (todo < 0) {
-        // remove
+        /* remove one block at most at a time */
+        uint32_t to_remove = (uint32_t)(todo * -1) / 10;
+        if (to_remove < flow_spare_pool_block_size)
+            return;
+
+        FlowSparePool *p = NULL;
+        SCMutexLock(&flow_spare_pool_m);
+        p = flow_spare_pool;
+        if (p != NULL) {
+            flow_spare_pool = p->next;
+            flow_spare_pool_flow_cnt -= p->queue.len;
+        }
+        SCMutexUnlock(&flow_spare_pool_m);
+
+        if (p != NULL) {
+            Flow *f;
+            while ((f = FlowQueuePrivateGetFromTop(&p->queue))) {
+                FlowFree(f);
+            }
+            SCFree(p);
+        }
+
     } else if (todo > 0) {
         FlowSparePool *head = NULL, *tail = NULL;