]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Add per-flow generic storage
authorEric Leblond <eric@regit.org>
Fri, 15 Mar 2013 14:25:46 +0000 (15:25 +0100)
committerVictor Julien <victor@inliniac.net>
Sun, 28 Jul 2013 21:41:11 +0000 (23:41 +0200)
This patch adds a per-flow storage that can be created via the functions
available in flow-storage.c.

src/Makefile.am
src/flow-storage.c [new file with mode: 0644]
src/flow-storage.h [new file with mode: 0644]
src/flow-util.c
src/flow.c

index b28643401dd9907524d1e5dc6173f1e7649d2e26..fbd0558992d6b3b9479574d3219ffc28b48078ff 100644 (file)
@@ -192,6 +192,7 @@ flow.c flow.h \
 flow-hash.c flow-hash.h \
 flow-manager.c flow-manager.h \
 flow-queue.c flow-queue.h \
+flow-storage.c flow-storage.h \
 flow-timeout.c flow-timeout.h \
 flow-util.c flow-util.h \
 flow-var.c flow-var.h \
diff --git a/src/flow-storage.c b/src/flow-storage.c
new file mode 100644 (file)
index 0000000..5685e71
--- /dev/null
@@ -0,0 +1,270 @@
+/* Copyright (C) 2013 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Eric Leblond <eric@regit.org>
+ *
+ * based on host-storage by Victor Julien <victor@inliniac.net>
+ *
+ * Flow wrapper around storage api
+ */
+
+#include "suricata-common.h"
+#include "host-storage.h"
+#include "flow-hash.h"
+#include "flow-util.h"
+#include "util-unittest.h"
+
+unsigned int FlowStorageSize(void) {
+    return StorageGetSize(STORAGE_FLOW);
+}
+
+void *FlowGetStorageById(Flow *f, int id) {
+    return StorageGetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id);
+}
+
+int FlowSetStorageById(Flow *f, int id, void *ptr) {
+    return StorageSetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id, ptr);
+}
+
+void *FlowAllocStorageById(Flow *f, int id) {
+    return StorageAllocByIdPrealloc((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id);
+}
+
+void FlowFreeStorageById(Flow *f, int id) {
+    StorageFreeById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id);
+}
+
+void FlowFreeStorage(Flow *f) {
+    StorageFreeAll((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW);
+}
+
+int FlowStorageRegister(const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *)) {
+    return StorageRegister(STORAGE_FLOW, name, size, Init, Free);
+}
+
+#ifdef UNITTESTS
+
+static void *StorageTestInit(unsigned int size) {
+    void *x = SCMalloc(size);
+    return x;
+}
+static void StorageTestFree(void *x) {
+    if (x)
+        SCFree(x);
+}
+
+static int FlowStorageTest01(void) {
+    StorageInit();
+
+    int id1 = FlowStorageRegister("test", 8, StorageTestInit, StorageTestFree);
+    if (id1 < 0)
+        goto error;
+    int id2 = FlowStorageRegister("variable", 24, StorageTestInit, StorageTestFree);
+    if (id2 < 0)
+        goto error;
+    int id3 = FlowStorageRegister("store", sizeof(void *), StorageTestInit, StorageTestFree);
+    if (id3 < 0)
+        goto error;
+
+    if (StorageFinalize() < 0)
+        goto error;
+
+    FlowInitConfig(FLOW_QUIET);
+    Flow *f = FlowAlloc();
+    if (f == NULL) {
+        goto error;
+    }
+
+    void *ptr = FlowGetStorageById(f, id1);
+    if (ptr != NULL) {
+        goto error;
+    }
+    ptr = FlowGetStorageById(f, id2);
+    if (ptr != NULL) {
+        goto error;
+    }
+    ptr = FlowGetStorageById(f, id3);
+    if (ptr != NULL) {
+        goto error;
+    }
+
+    void *ptr1a = FlowAllocStorageById(f, id1);
+    if (ptr1a == NULL) {
+        goto error;
+    }
+    void *ptr2a = FlowAllocStorageById(f, id2);
+    if (ptr2a == NULL) {
+        goto error;
+    }
+    void *ptr3a = FlowAllocStorageById(f, id3);
+    if (ptr3a == NULL) {
+        goto error;
+    }
+
+    void *ptr1b = FlowGetStorageById(f, id1);
+    if (ptr1a != ptr1b) {
+        goto error;
+    }
+    void *ptr2b = FlowGetStorageById(f, id2);
+    if (ptr2a != ptr2b) {
+        goto error;
+    }
+    void *ptr3b = FlowGetStorageById(f, id3);
+    if (ptr3a != ptr3b) {
+        goto error;
+    }
+
+    FlowClearMemory(f, 0);
+    FlowFree(f);
+    FlowShutdown();
+    StorageCleanup();
+    return 1;
+error:
+    FlowClearMemory(f, 0);
+    FlowFree(f);
+    FlowShutdown();
+    StorageCleanup();
+    return 0;
+}
+
+static int FlowStorageTest02(void) {
+    StorageInit();
+
+    int id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
+    if (id1 < 0)
+        goto error;
+
+    if (StorageFinalize() < 0)
+        goto error;
+
+    FlowInitConfig(FLOW_QUIET);
+    Flow *f = FlowAlloc();
+    if (f == NULL) {
+        goto error;
+    }
+
+    void *ptr = FlowGetStorageById(f, id1);
+    if (ptr != NULL) {
+        goto error;
+    }
+
+    void *ptr1a = SCMalloc(128);
+    if (ptr1a == NULL) {
+        goto error;
+    }
+    FlowSetStorageById(f, id1, ptr1a);
+
+    void *ptr1b = FlowGetStorageById(f, id1);
+    if (ptr1a != ptr1b) {
+        goto error;
+    }
+
+
+    FlowClearMemory(f, 0);
+    FlowFree(f);
+    FlowShutdown();
+    StorageCleanup();
+    return 1;
+error:
+    FlowClearMemory(f, 0);
+    FlowFree(f);
+    FlowShutdown();
+    StorageCleanup();
+    return 0;
+}
+
+static int FlowStorageTest03(void) {
+    StorageInit();
+
+    int id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
+    if (id1 < 0)
+        goto error;
+    int id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
+    if (id2 < 0)
+        goto error;
+    int id3 = FlowStorageRegister("test3", 32, StorageTestInit, StorageTestFree);
+    if (id3 < 0)
+        goto error;
+
+    if (StorageFinalize() < 0)
+        goto error;
+
+    FlowInitConfig(FLOW_QUIET);
+    Flow *f = FlowAlloc();
+    if (f == NULL) {
+        goto error;
+    }
+
+    void *ptr = FlowGetStorageById(f, id1);
+    if (ptr != NULL) {
+        goto error;
+    }
+
+    void *ptr1a = SCMalloc(128);
+    if (ptr1a == NULL) {
+        goto error;
+    }
+    FlowSetStorageById(f, id1, ptr1a);
+
+    void *ptr2a = SCMalloc(256);
+    if (ptr2a == NULL) {
+        goto error;
+    }
+    FlowSetStorageById(f, id2, ptr2a);
+
+    void *ptr3a = FlowAllocStorageById(f, id3);
+    if (ptr3a == NULL) {
+        goto error;
+    }
+
+    void *ptr1b = FlowGetStorageById(f, id1);
+    if (ptr1a != ptr1b) {
+        goto error;
+    }
+    void *ptr2b = FlowGetStorageById(f, id2);
+    if (ptr2a != ptr2b) {
+        goto error;
+    }
+    void *ptr3b = FlowGetStorageById(f, id3);
+    if (ptr3a != ptr3b) {
+        goto error;
+    }
+
+    FlowClearMemory(f, 0);
+    FlowFree(f);
+    FlowShutdown();
+    StorageCleanup();
+    return 1;
+error:
+    FlowClearMemory(f, 0);
+    FlowFree(f);
+    FlowShutdown();
+    StorageCleanup();
+    return 0;
+}
+#endif
+
+void RegisterFlowStorageTests(void) {
+#ifdef UNITTESTS
+    UtRegisterTest("FlowStorageTest01", FlowStorageTest01, 1);
+    UtRegisterTest("FlowStorageTest02", FlowStorageTest02, 1);
+    UtRegisterTest("FlowStorageTest03", FlowStorageTest03, 1);
+#endif
+}
diff --git a/src/flow-storage.h b/src/flow-storage.h
new file mode 100644 (file)
index 0000000..aa5a98d
--- /dev/null
@@ -0,0 +1,45 @@
+/* Copyright (C) 2007-2013 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Victor Julien <victor@inliniac.net>
+ *
+ * Flow wrapper around storage api
+ */
+
+#ifndef __FLOW_STORAGE_H__
+#define __FLOW_STORAGE_H__
+
+#include "util-storage.h"
+#include "flow.h"
+
+unsigned int FlowStorageSize(void);
+
+void *FlowGetStorageById(Flow *h, int id);
+int FlowSetStorageById(Flow *h, int id, void *ptr);
+void *FlowAllocStorageById(Flow *h, int id);
+
+void FlowFreeStorageById(Flow *h, int id);
+void FlowFreeStorage(Flow *h);
+
+void RegisterFlowStorageTests(void);
+
+int FlowStorageRegister(const char *name, const unsigned int size, void *(*Init)(unsigned int), void (*Free)(void *));
+
+#endif /* __FLOW_STORAGE_H__ */
index 93b227ad0cfda072860017ee4eb1c21c2f2378e0..26071790a573dd06aaca720bea6fff0572da303a 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "util-var.h"
 #include "util-debug.h"
+#include "flow-storage.h"
 
 #include "detect.h"
 #include "detect-engine-state.h"
 Flow *FlowAlloc(void)
 {
     Flow *f;
+    size_t size = sizeof(Flow) + FlowStorageSize();
 
-    if (!(FLOW_CHECK_MEMCAP(sizeof(Flow)))) {
+    if (!(FLOW_CHECK_MEMCAP(size))) {
         return NULL;
     }
 
-    (void) SC_ATOMIC_ADD(flow_memuse, sizeof(Flow));
+    (void) SC_ATOMIC_ADD(flow_memuse, size);
 
-    f = SCMalloc(sizeof(Flow));
+    f = SCMalloc(size);
     if (unlikely(f == NULL)) {
-        (void)SC_ATOMIC_SUB(flow_memuse, sizeof(Flow));
+        (void)SC_ATOMIC_SUB(flow_memuse, size);
         return NULL;
     }
+    memset(f, 0, size);
 
     FLOW_INITIALIZE(f);
     return f;
index 5abedb2ddb1c5c00ae287747f4f640aa6c2548c7..f22e6ab80e86bf1fce6e782ba196d8d7545eafff 100644 (file)
@@ -42,6 +42,7 @@
 #include "flow-private.h"
 #include "flow-timeout.h"
 #include "flow-manager.h"
+#include "flow-storage.h"
 
 #include "stream-tcp-private.h"
 #include "stream-tcp-reassemble.h"
@@ -760,6 +761,8 @@ int FlowClearMemory(Flow* f, uint8_t proto_map)
         flow_proto[proto_map].Freefunc(f->protoctx);
     }
 
+    FlowFreeStorage(f);
+
     FLOW_RECYCLE(f);
 
     SCReturnInt(1);
@@ -1085,5 +1088,6 @@ void FlowRegisterTests (void) {
     UtRegisterTest("FlowTest09 -- Test flow Allocations when it reach memcap", FlowTest09, 1);
 
     FlowMgrRegisterTests();
+    RegisterFlowStorageTests();
 #endif /* UNITTESTS */
 }