Project

General

Profile

Feature #488 » 0001-Delay-Detect-threads-initialization.patch

Eric Leblond, 08/11/2012 06:35 AM

View differences:

src/suricata.c
685 685
#endif /* OS_WIN32 */
686 686
    int build_info = 0;
687 687
    int rule_reload = 0;
688
    int delayed_detect = 1;
688 689

  
689 690
    char *log_dir;
690 691
#ifdef OS_WIN32
......
1705 1706
    if (MagicInit() != 0)
1706 1707
        exit(EXIT_FAILURE);
1707 1708

  
1708
    if (SigLoadSignatures(de_ctx, sig_file, sig_file_exclusive) < 0) {
1709
        if (sig_file == NULL) {
1710
            SCLogError(SC_ERR_OPENING_FILE, "Signature file has not been provided");
1711
        } else {
1712
            SCLogError(SC_ERR_NO_RULES_LOADED, "Loading signatures failed.");
1709
    /* In offline mode delayed init of detect is a bad idea */
1710
    if ((run_mode == RUNMODE_PCAP_FILE) ||
1711
        (run_mode == RUNMODE_ERF_FILE) ||
1712
        engine_analysis) {
1713
        delayed_detect = 0;
1714
    } else {
1715
        if (ConfGetBool("delayed-detect", &delayed_detect) != 1) {
1716
            delayed_detect = 1;
1713 1717
        }
1714
        if (de_ctx->failure_fatal)
1715
            exit(EXIT_FAILURE);
1716 1718
    }
1717 1719

  
1718
    if (engine_analysis) {
1719
        exit(EXIT_SUCCESS);
1720
    if (!delayed_detect) {
1721
        if (SigLoadSignatures(de_ctx, sig_file, sig_file_exclusive) < 0) {
1722
            if (sig_file == NULL) {
1723
                SCLogError(SC_ERR_OPENING_FILE, "Signature file has not been provided");
1724
            } else {
1725
                SCLogError(SC_ERR_NO_RULES_LOADED, "Loading signatures failed.");
1726
            }
1727
            if (de_ctx->failure_fatal)
1728
                exit(EXIT_FAILURE);
1729
        }
1730
        if (engine_analysis) {
1731
            exit(EXIT_SUCCESS);
1732
        }
1720 1733
    }
1721 1734

  
1722 1735
    /* registering singal handlers we use.  We register usr2 here, so that one
......
1826 1839
    /* Un-pause all the paused threads */
1827 1840
    TmThreadContinueThreads();
1828 1841

  
1842
    if (delayed_detect) {
1843
        if (SigLoadSignatures(de_ctx, sig_file, sig_file_exclusive) < 0) {
1844
            if (sig_file == NULL) {
1845
                SCLogError(SC_ERR_OPENING_FILE, "Signature file has not been provided");
1846
            } else {
1847
                SCLogError(SC_ERR_NO_RULES_LOADED, "Loading signatures failed.");
1848
            }
1849
            if (de_ctx->failure_fatal)
1850
                exit(EXIT_FAILURE);
1851
        }
1852
        SCLogInfo("Signature(s) loaded, Detect thread(s) activated.");
1853
    }
1854

  
1855
    TmThreadActivateDummySlot();
1856

  
1829 1857
#ifdef DBG_MEM_ALLOC
1830 1858
    SCLogInfo("Memory used at startup: %"PRIdMAX, (intmax_t)global_mem);
1831 1859
#ifdef DBG_MEM_ALLOC_SKIP_STARTUP
src/tm-threads.c
43 43
#include "util-optimize.h"
44 44
#include "util-profiling.h"
45 45
#include "util-signal.h"
46
#include "queue.h"
46 47

  
47 48
#ifdef PROFILE_LOCKING
48 49
__thread uint64_t mutex_lock_contention;
......
119 120
    SC_ATOMIC_AND(tv->flags, ~flag);
120 121
}
121 122

  
123

  
124
TmEcode TmDummyFunc(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq)
125
{
126
    return TM_ECODE_OK;
127
}
128

  
122 129
/* 1 slot functions */
123 130
void *TmThreadsSlot1NoIn(void *td)
124 131
{
......
885 892
 * \param tm   TM to append.
886 893
 * \param data Data to be passed on to the slot init function.
887 894
 */
888
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, void *data)
895

  
896
static inline TmSlot * _TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, void *data)
889 897
{
890 898
    TmSlot *s = (TmSlot *)tv->tm_slots;
891 899

  
892 900
    TmSlot *slot = SCMalloc(sizeof(TmSlot));
893 901
    if (slot == NULL)
894
        return;
902
        return NULL;
895 903
    memset(slot, 0, sizeof(TmSlot));
896 904
    SC_ATOMIC_INIT(slot->slot_data);
897 905
    slot->tv = tv;
......
924 932
        }
925 933
    }
926 934

  
935
    return slot;
936
}
937

  
938
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, void *data)
939
{
940
    _TmSlotSetFuncAppend(tv, tm, data);
941
}
942

  
943
typedef struct TmDummySlot_ {
944
    TmSlot *slot;
945
    TmEcode (*SlotFunc)(ThreadVars *, Packet *, void *, PacketQueue *,
946
                        PacketQueue *);
947
    TmEcode (*SlotThreadInit)(ThreadVars *, void *, void **);
948
    TAILQ_ENTRY(TmDummySlot_) next;
949
} TmDummySlot;
950

  
951
static TAILQ_HEAD(, TmDummySlot_) dummy_slots =
952
    TAILQ_HEAD_INITIALIZER(dummy_slots);
953

  
954
void TmSlotSetFuncAppendDelayed(ThreadVars *tv, TmModule *tm, void *data)
955
{
956
    TmSlot *slot = _TmSlotSetFuncAppend(tv, tm, data);
957
    TmDummySlot *dslot = SCMalloc(sizeof(TmDummySlot));
958

  
959
    if (slot == NULL) {
960
        return;
961
    }
962
    if (dslot == NULL) {
963
        return;
964
    }
965

  
966
    memset(dslot, 0, sizeof(*dslot));
967

  
968
    dslot->SlotFunc = slot->SlotFunc;
969
    slot->SlotFunc = TmDummyFunc;
970
    dslot->SlotThreadInit = slot->SlotThreadInit;
971
    slot->SlotThreadInit = NULL;
972
    dslot->slot = slot;
973

  
974
    TAILQ_INSERT_TAIL(&dummy_slots, dslot, next);
975

  
927 976
    return;
928 977
}
929 978

  
979
void TmThreadActivateDummySlot()
980
{
981
    TmDummySlot *dslot;
982
    TmSlot *s;
983
    TmEcode r = TM_ECODE_OK;
984

  
985
    TAILQ_FOREACH(dslot, &dummy_slots, next) {
986
        void *slot_data = NULL;
987
        s = dslot->slot;
988
        if (dslot->SlotThreadInit != NULL) {
989
            s->SlotThreadInit = dslot->SlotThreadInit;
990
            r = s->SlotThreadInit(s->tv, s->slot_initdata, &slot_data);
991
            if (r != TM_ECODE_OK) {
992
                EngineKill();
993
                TmThreadsSetFlag(s->tv, THV_CLOSED | THV_RUNNING_DONE);
994
            }
995
            SC_ATOMIC_SET(s->slot_data, slot_data);
996
        }
997
        s->SlotFunc = dslot->SlotFunc;
998
    }
999
}
1000

  
1001
void TmThreadDeActivateDummySlot()
1002
{
1003
    TmDummySlot *dslot;
1004

  
1005
    TAILQ_FOREACH(dslot, &dummy_slots, next) {
1006
        dslot->slot->SlotFunc = TmDummyFunc;
1007
        dslot->slot->SlotThreadInit = NULL;
1008
    }
1009
}
1010

  
1011

  
1012

  
930 1013
/**
931 1014
 * \brief Returns the slot holding a TM with the particular tm_id.
932 1015
 *
src/tm-threads.h
72 72
extern SCMutex tv_root_lock;
73 73

  
74 74
void TmSlotSetFuncAppend(ThreadVars *, TmModule *, void *);
75
void TmSlotSetFuncAppendDelayed(ThreadVars *, TmModule *, void *);
75 76
TmSlot *TmSlotGetSlotForTM(int);
76 77

  
77 78
ThreadVars *TmThreadCreate(char *, char *, char *, char *, char *, char *,
......
104 105
TmEcode TmThreadWaitOnThreadInit(void);
105 106
ThreadVars *TmThreadsGetCallingThread(void);
106 107

  
108
void TmThreadActivateDummySlot(void);
109
void TmThreadDeActivateDummySlot(void);
110

  
107 111
int TmThreadsCheckFlag(ThreadVars *, uint8_t);
108 112
void TmThreadsSetFlag(ThreadVars *, uint8_t);
109 113
void TmThreadsUnsetFlag(ThreadVars *, uint8_t);
src/util-runmodes.c
330 330
            SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed");
331 331
            exit(EXIT_FAILURE);
332 332
        }
333
        TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx);
333
        TmSlotSetFuncAppendDelayed(tv_detect_ncpu, tm_module, (void *)de_ctx);
334 334

  
335 335
        TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET);
336 336

  
......
574 574
            SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed");
575 575
            exit(EXIT_FAILURE);
576 576
        }
577
        TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx);
577
        TmSlotSetFuncAppendDelayed(tv_detect_ncpu, tm_module, (void *)de_ctx);
578 578

  
579 579
        TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET);
580 580

  
......
674 674
            SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed");
675 675
            exit(EXIT_FAILURE);
676 676
        }
677
        TmSlotSetFuncAppend(tv, tm_module, (void *)de_ctx);
677
        TmSlotSetFuncAppendDelayed(tv, tm_module, (void *)de_ctx);
678 678

  
679 679
        tm_module = TmModuleGetByName("RespondReject");
680 680
        if (tm_module == NULL) {
......
879 879
            printf("ERROR: TmModuleGetByName Detect failed\n");
880 880
            exit(EXIT_FAILURE);
881 881
        }
882
        TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx);
882
        TmSlotSetFuncAppendDelayed(tv_detect_ncpu, tm_module, (void *)de_ctx);
883 883

  
884 884
        TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET);
885 885

  
......
1064 1064
            SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed");
1065 1065
            exit(EXIT_FAILURE);
1066 1066
        }
1067
        TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, (void *)de_ctx);
1067
        TmSlotSetFuncAppendDelayed(tv_detect_ncpu, tm_module, (void *)de_ctx);
1068 1068

  
1069 1069
        TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET);
1070 1070

  
......
1181 1181
            SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName Detect failed");
1182 1182
            exit(EXIT_FAILURE);
1183 1183
        }
1184
        TmSlotSetFuncAppend(tv, tm_module, (void *)de_ctx);
1184
        TmSlotSetFuncAppendDelayed(tv, tm_module, (void *)de_ctx);
1185 1185

  
1186 1186
        tm_module = TmModuleGetByName(verdict_mod_name);
1187 1187
        if (tm_module == NULL) {
suricata.yaml.in
921 921

  
922 922
coredump:
923 923
  max-dump: unlimited
924

  
925
# If set to no, the loading of signatures will be made before the engine
926
# is started.
927
#delayed-detect: no
924
- 
(1-1/4)