]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
app-layer/files: optimize GetFiles calls
authorVictor Julien <victor@inliniac.net>
Tue, 26 Feb 2019 09:56:53 +0000 (10:56 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 11 Feb 2020 14:04:28 +0000 (15:04 +0100)
Remove FlowGetProtoMapping calls from the GetFiles wrapper and
get the alstate from the flow directly.

12 files changed:
src/app-layer-parser.c
src/app-layer-parser.h
src/detect-engine-file.c
src/detect-engine-state.c
src/detect-file-data.c
src/detect-filemagic.c
src/detect-filename.c
src/detect-filestore.c
src/flow-worker.c
src/output-file.c
src/output-filedata.c
src/util-file.c

index 00b16a29e04e3502b8e4942e34d403df7ea34f70..da54b6fddd53a3f7dc3b1fdaf92b7e25fe51f6d8 100644 (file)
@@ -875,18 +875,16 @@ AppLayerDecoderEvents *AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alp
     SCReturnPtr(ptr, "AppLayerDecoderEvents *");
 }
 
-FileContainer *AppLayerParserGetFiles(uint8_t ipproto, AppProto alproto,
-                           void *alstate, uint8_t direction)
+FileContainer *AppLayerParserGetFiles(const Flow *f, const uint8_t direction)
 {
     SCEnter();
 
     FileContainer *ptr = NULL;
 
-    if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
-        StateGetFiles != NULL)
+    if (alp_ctx.ctxs[f->protomap][f->alproto].StateGetFiles != NULL)
     {
-        ptr = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
-            StateGetFiles(alstate, direction);
+        ptr = alp_ctx.ctxs[f->protomap][f->alproto].
+            StateGetFiles(f->alstate, direction);
     }
 
     SCReturnPtr(ptr, "FileContainer *");
@@ -2020,12 +2018,23 @@ static void TestProtocolStateFree(void *s)
     SCFree(s);
 }
 
-static uint64_t TestProtocolGetTxCnt(void *state)
+static uint64_t TestGetTxCnt(void *state)
 {
     /* single tx */
     return 1;
 }
 
+static void TestStateTransactionFree(void *state, uint64_t tx_id)
+{
+    /* do nothing */
+}
+
+static void *TestGetTx(void *state, uint64_t tx_id)
+{
+    TestState *test_state = (TestState *)state;
+    return test_state;
+}
+
 void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto,
                                   void (*RegisterUnittests)(void))
 {
@@ -2073,7 +2082,9 @@ static int AppLayerParserTest01(void)
                       TestProtocolParser);
     AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TEST,
                           TestProtocolStateAlloc, TestProtocolStateFree);
-    AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TEST, TestProtocolGetTxCnt);
+    AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TEST, TestStateTransactionFree);
+    AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TEST, TestGetTx);
+    AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TEST, TestGetTxCnt);
 
     f = UTHBuildFlow(AF_INET, "1.2.3.4", "4.3.2.1", 20, 40);
     if (f == NULL)
@@ -2128,7 +2139,9 @@ static int AppLayerParserTest02(void)
                       TestProtocolParser);
     AppLayerParserRegisterStateFuncs(IPPROTO_UDP, ALPROTO_TEST,
                           TestProtocolStateAlloc, TestProtocolStateFree);
-    AppLayerParserRegisterGetTxCnt(IPPROTO_UDP, ALPROTO_TEST, TestProtocolGetTxCnt);
+    AppLayerParserRegisterTxFreeFunc(IPPROTO_UDP, ALPROTO_TEST, TestStateTransactionFree);
+    AppLayerParserRegisterGetTx(IPPROTO_UDP, ALPROTO_TEST, TestGetTx);
+    AppLayerParserRegisterGetTxCnt(IPPROTO_UDP, ALPROTO_TEST, TestGetTxCnt);
 
     f = UTHBuildFlow(AF_INET, "1.2.3.4", "4.3.2.1", 20, 40);
     if (f == NULL)
index 0b087683093577a1661719151e2b432161980f03..fc367d2ff39b14d5f222bd63461bd9badceeae78 100644 (file)
@@ -198,8 +198,7 @@ void AppLayerParserSetTransactionInspectId(const Flow *f, AppLayerParserState *p
 AppLayerDecoderEvents *AppLayerParserGetDecoderEvents(AppLayerParserState *pstate);
 void AppLayerParserSetDecoderEvents(AppLayerParserState *pstate, AppLayerDecoderEvents *devents);
 AppLayerDecoderEvents *AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alproto, void *tx);
-FileContainer *AppLayerParserGetFiles(uint8_t ipproto, AppProto alproto,
-                           void *alstate, uint8_t direction);
+FileContainer *AppLayerParserGetFiles(const Flow *f, const uint8_t direction);
 int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto,
                         void *alstate, uint8_t direction);
 uint64_t AppLayerParserGetTxCnt(const Flow *, void *alstate);
index a6f8cfa0585466d8843c10c9d74565a2c047054e..4e9e9de021853f5322fffe6ed8ee48272ca34d5e 100644 (file)
@@ -49,6 +49,7 @@
 #include "util-unittest.h"
 #include "util-unittest-helper.h"
 #include "util-profiling.h"
+#include "util-validate.h"
 
 
 /**
@@ -218,16 +219,13 @@ static int DetectFileInspect(ThreadVars *tv, DetectEngineThreadCtx *det_ctx,
 int DetectFileInspectGeneric(ThreadVars *tv,
         DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
         const Signature *s, const SigMatchData *smd,
-        Flow *f, uint8_t flags, void *alstate, void *tx, uint64_t tx_id)
+        Flow *f, uint8_t flags, void *_alstate, void *tx, uint64_t tx_id)
 {
     SCEnter();
-
-    if (alstate == NULL) {
-        SCReturnInt(DETECT_ENGINE_INSPECT_SIG_NO_MATCH);
-    }
+    DEBUG_VALIDATE_BUG_ON(f->alstate != _alstate);
 
     const uint8_t direction = flags & (STREAM_TOSERVER|STREAM_TOCLIENT);
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, direction);
     if (ffc == NULL || ffc->head == NULL) {
         SCReturnInt(DETECT_ENGINE_INSPECT_SIG_NO_MATCH);
     }
index 2f2c2b4c414f97afcfbe66428e7c6e9c02a02bdb..008f9c861e2400e8619c6206c76df3b991fd7668 100644 (file)
@@ -663,8 +663,7 @@ static int DeStateSigTest03(void)
     SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
     FAIL_IF(!(PacketAlertCheck(p, 1)));
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
 
     File *file = files->head;
@@ -741,8 +740,7 @@ static int DeStateSigTest04(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -818,8 +816,7 @@ static int DeStateSigTest05(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -896,8 +893,7 @@ static int DeStateSigTest06(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -979,8 +975,7 @@ static int DeStateSigTest07(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -1077,8 +1072,7 @@ static int DeStateSigTest08(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -1102,8 +1096,7 @@ static int DeStateSigTest08(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     file = files->head;
     FAIL_IF_NULL(file);
@@ -1202,8 +1195,7 @@ static int DeStateSigTest09(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -1227,8 +1219,7 @@ static int DeStateSigTest09(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     file = files->head;
     FAIL_IF_NULL(file);
@@ -1325,8 +1316,7 @@ static int DeStateSigTest10(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    FileContainer *files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    FileContainer *files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     File *file = files->head;
     FAIL_IF_NULL(file);
@@ -1350,8 +1340,7 @@ static int DeStateSigTest10(void)
     FAIL_IF_NULL(http_state);
     FAIL_IF_NULL(http_state->files_ts);
 
-    files = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                  p->flow->alstate, STREAM_TOSERVER);
+    files = AppLayerParserGetFiles(p->flow, STREAM_TOSERVER);
     FAIL_IF_NULL(files);
     file = files->head;
     FAIL_IF_NULL(file);
index 6558668f5da73c7fefdabdd180d5fe372b68bec9..0479315728b89b18a60cb5e679f4658a6c2fe573 100644 (file)
@@ -415,8 +415,7 @@ static int DetectEngineInspectFiledata(
         transforms = engine->v2.transforms;
     }
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto,
-                                                f->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(f, flags);
     if (ffc == NULL) {
         return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
     }
@@ -484,8 +483,7 @@ static void PrefilterTxFiledata(DetectEngineThreadCtx *det_ctx,
     const MpmCtx *mpm_ctx = ctx->mpm_ctx;
     const int list_id = ctx->list_id;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto,
-                                                f->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(f, flags);
     int local_file_id = 0;
     if (ffc != NULL) {
         File *file = ffc->head;
index 0b3b9943205054288095bf0e3ee2fe6feb947c72..003082e79a10e52ff850c098af63b1fb421597e2 100644 (file)
@@ -535,8 +535,7 @@ static int DetectEngineInspectFilemagic(
         transforms = engine->v2.transforms;
     }
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto,
-                                                f->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(f, flags);
     if (ffc == NULL) {
         return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
     }
@@ -596,8 +595,7 @@ static void PrefilterTxFilemagic(DetectEngineThreadCtx *det_ctx,
     const MpmCtx *mpm_ctx = ctx->mpm_ctx;
     const int list_id = ctx->list_id;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto,
-                                                f->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(f, flags);
     int local_file_id = 0;
     if (ffc != NULL) {
         File *file = ffc->head;
index bbfc3abaaa99c208c7274558bfb89a9fd05b783c..a90096804497acf8c5f8d37bda0a583408a44815 100644 (file)
@@ -381,8 +381,7 @@ static int DetectEngineInspectFilename(
         transforms = engine->v2.transforms;
     }
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto,
-                                                f->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(f, flags);
     if (ffc == NULL) {
         return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
     }
@@ -442,8 +441,7 @@ static void PrefilterTxFilename(DetectEngineThreadCtx *det_ctx,
     const MpmCtx *mpm_ctx = ctx->mpm_ctx;
     const int list_id = ctx->list_id;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto,
-                                                f->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(f, flags);
     int local_file_id = 0;
     if (ffc != NULL) {
         File *file = ffc->head;
index c2d1340c22a96787a8b8bbb2a6a6856030b5c3cb..614579634fb274703b4ebb7420823c7fb8452422 100644 (file)
@@ -226,8 +226,7 @@ static int DetectFilestorePostMatch(DetectEngineThreadCtx *det_ctx,
                                          flags);
     }
 
-    FileContainer *ffc = AppLayerParserGetFiles(p->flow->proto, p->flow->alproto,
-                                                p->flow->alstate, flags);
+    FileContainer *ffc = AppLayerParserGetFiles(p->flow, flags);
 
     /* filestore for single files only */
     if (s->filestore_ctx == NULL) {
index 74e81c57de37b8d3d498c84b6036666b617c49bc..35bfe367006741fdd7aacd308eada03ca0cfd53a 100644 (file)
@@ -179,8 +179,8 @@ static void FlowPruneFiles(Packet *p)
 {
     if (p->flow && p->flow->alstate) {
         Flow *f = p->flow;
-        FileContainer *fc = AppLayerParserGetFiles(p->proto, f->alproto,
-            f->alstate, PKT_IS_TOSERVER(p) ? STREAM_TOSERVER : STREAM_TOCLIENT);
+        FileContainer *fc = AppLayerParserGetFiles(f,
+                PKT_IS_TOSERVER(p) ? STREAM_TOSERVER : STREAM_TOCLIENT);
         if (fc != NULL) {
             FilePrune(fc);
         }
index 97aec6c2a8072cb5030bc1a5f2acfefad13c8f54..9eccf48b60d6316208542922b8a98d3462f37b80 100644 (file)
@@ -170,10 +170,8 @@ static TmEcode OutputFileLog(ThreadVars *tv, Packet *p, void *thread_data)
             (p->flowflags & FLOW_PKT_TOCLIENT));
     const bool file_trunc = StreamTcpReassembleDepthReached(p);
 
-    FileContainer *ffc_ts = AppLayerParserGetFiles(p->proto, f->alproto,
-                                                   f->alstate, STREAM_TOSERVER);
-    FileContainer *ffc_tc = AppLayerParserGetFiles(p->proto, f->alproto,
-                                                   f->alstate, STREAM_TOCLIENT);
+    FileContainer *ffc_ts = AppLayerParserGetFiles(f, STREAM_TOSERVER);
+    FileContainer *ffc_tc = AppLayerParserGetFiles(f, STREAM_TOCLIENT);
 
     OutputFileLogFfc(tv, op_thread_data, p, ffc_ts, file_close_ts, file_trunc, STREAM_TOSERVER);
     OutputFileLogFfc(tv, op_thread_data, p, ffc_tc, file_close_tc, file_trunc, STREAM_TOCLIENT);
index 893818234c6018bf96968f412b4e88702d963961..9414513759522375fafd8bd38f3c22c88a88b9ec 100644 (file)
@@ -227,10 +227,8 @@ static TmEcode OutputFiledataLog(ThreadVars *tv, Packet *p, void *thread_data)
             (p->flowflags & FLOW_PKT_TOCLIENT));
     const bool file_trunc = StreamTcpReassembleDepthReached(p);
 
-    FileContainer *ffc_ts = AppLayerParserGetFiles(p->proto, f->alproto,
-                                                   f->alstate, STREAM_TOSERVER);
-    FileContainer *ffc_tc = AppLayerParserGetFiles(p->proto, f->alproto,
-                                                   f->alstate, STREAM_TOCLIENT);
+    FileContainer *ffc_ts = AppLayerParserGetFiles(f, STREAM_TOSERVER);
+    FileContainer *ffc_tc = AppLayerParserGetFiles(f, STREAM_TOCLIENT);
     SCLogDebug("ffc_ts %p", ffc_ts);
     OutputFiledataLogFfc(tv, store, p, ffc_ts, STREAM_TOSERVER, file_close_ts, file_trunc, STREAM_TOSERVER);
     SCLogDebug("ffc_tc %p", ffc_tc);
index f148dff88a3e18d436c0404e512a2dab4efad811..fd11244c3007aa34a1721c4ce05de766841fb7da 100644 (file)
@@ -1051,7 +1051,7 @@ void FileDisableStoring(Flow *f, uint8_t direction)
     else
         f->file_flags |= FLOWFILE_NO_STORE_TC;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, f->alstate, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             /* if we're already storing, we'll continue */
@@ -1083,7 +1083,7 @@ void FileDisableMagic(Flow *f, uint8_t direction)
     else
         f->file_flags |= FLOWFILE_NO_MAGIC_TC;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, f->alstate, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             SCLogDebug("disabling magic for file %p from direction %s",
@@ -1114,7 +1114,7 @@ void FileDisableMd5(Flow *f, uint8_t direction)
     else
         f->file_flags |= FLOWFILE_NO_MD5_TC;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, f->alstate, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             SCLogDebug("disabling md5 for file %p from direction %s",
@@ -1153,7 +1153,7 @@ void FileDisableSha1(Flow *f, uint8_t direction)
     else
         f->file_flags |= FLOWFILE_NO_SHA1_TC;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, f->alstate, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             SCLogDebug("disabling sha1 for file %p from direction %s",
@@ -1192,7 +1192,7 @@ void FileDisableSha256(Flow *f, uint8_t direction)
     else
         f->file_flags |= FLOWFILE_NO_SHA256_TC;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, f->alstate, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             SCLogDebug("disabling sha256 for file %p from direction %s",
@@ -1231,7 +1231,7 @@ void FileDisableFilesize(Flow *f, uint8_t direction)
     else
         f->file_flags |= FLOWFILE_NO_SIZE_TC;
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, f->alstate, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             SCLogDebug("disabling size tracking for file %p from direction %s",
@@ -1284,7 +1284,7 @@ void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id
 
     SCEnter();
 
-    FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
+    FileContainer *ffc = AppLayerParserGetFiles(f, direction);
     if (ffc != NULL) {
         for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
             if (ptr->txid == tx_id) {