]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/state: optimize state keeping
authorVictor Julien <victor@inliniac.net>
Fri, 5 Feb 2021 07:41:22 +0000 (08:41 +0100)
committerVictor Julien <victor@inliniac.net>
Mon, 15 Feb 2021 10:03:44 +0000 (11:03 +0100)
(cherry picked from commit ed05c51d9943771b985bc90f2a319f1688de7483)

src/detect-engine-state.c

index ac83a4bc335f954791a06ed962b0c606330c150a..e247730fe224177579a28dfe3d6ea9b08a89c58a 100644 (file)
@@ -123,27 +123,23 @@ static int DeStateSearchState(DetectEngineState *state, uint8_t direction, SigIn
 static void DeStateSignatureAppend(DetectEngineState *state,
         const Signature *s, uint32_t inspect_flags, uint8_t direction)
 {
-    int jump = 0;
-    int i = 0;
+    SCEnter();
+
     DetectEngineStateDirection *dir_state = &state->dir_state[direction & STREAM_TOSERVER ? 0 : 1];
 
 #ifdef DEBUG_VALIDATION
     BUG_ON(DeStateSearchState(state, direction, s->num));
 #endif
-    DeStateStore *store = dir_state->head;
+    DeStateStore *store = dir_state->tail;
 
     if (store == NULL) {
         store = DeStateStoreAlloc();
-        if (store != NULL) {
-            dir_state->head = store;
-            dir_state->tail = store;
-        }
+        dir_state->head = store;
+        dir_state->tail = store;
     } else {
-        jump = dir_state->cnt / DE_STATE_CHUNK_SIZE;
-        for (i = 0; i < jump; i++) {
-            store = store->next;
-        }
-        if (store == NULL) {
+        SCLogDebug("dir_state->cnt %u mod chunksize %u", dir_state->cnt,
+                dir_state->cnt % DE_STATE_CHUNK_SIZE);
+        if (dir_state->cnt && dir_state->cnt % DE_STATE_CHUNK_SIZE == 0) {
             store = DeStateStoreAlloc();
             if (store != NULL) {
                 dir_state->tail->next = store;
@@ -151,15 +147,14 @@ static void DeStateSignatureAppend(DetectEngineState *state,
             }
         }
     }
-
     if (store == NULL)
-        return;
+        SCReturn;
 
     SigIntId idx = dir_state->cnt++ % DE_STATE_CHUNK_SIZE;
     store->store[idx].sid = s->num;
     store->store[idx].flags = inspect_flags;
 
-    return;
+    SCReturn;
 }
 
 DetectEngineState *DetectEngineStateAlloc(void)