From f51cf342104ef800d86278288b051b9b27fa1225 Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Thu, 24 Mar 2011 23:27:52 +0530 Subject: [PATCH] move erf file runmode into its own file runmode-erf-file.[ch] --- src/Makefile.am | 1 + src/runmode-erf-file.c | 190 +++++++++++++++++++++++++++++++++++++++++ src/runmode-erf-file.h | 28 ++++++ src/runmodes.c | 152 --------------------------------- src/runmodes.h | 2 +- 5 files changed, 220 insertions(+), 153 deletions(-) create mode 100644 src/runmode-erf-file.c create mode 100644 src/runmode-erf-file.h diff --git a/src/Makefile.am b/src/Makefile.am index cfce8c2626..ee616d890b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,6 +12,7 @@ runmode-pcap-file.c runmode-pcap-file.h \ runmode-pfring.c runmode-pfring.h \ runmode-nfq.c runmode-nfq.h \ runmode-ipfw.c runmode-ipfw.h \ +runmode-erf-file.c runmode-erf-file.h \ packet-queue.c packet-queue.h \ data-queue.c data-queue.h \ threads.c threads.h \ diff --git a/src/runmode-erf-file.c b/src/runmode-erf-file.c new file mode 100644 index 0000000000..6e20d39b1c --- /dev/null +++ b/src/runmode-erf-file.c @@ -0,0 +1,190 @@ +/* Copyright (C) 2007-2010 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. + */ + +#include "suricata-common.h" +#include "tm-threads.h" +#include "conf.h" +#include "runmodes.h" +#include "runmode-erf-file.h" +#include "log-httplog.h" +#include "output.h" +#include "cuda-packet-batcher.h" +#include "source-pfring.h" + +#include "alert-fastlog.h" +#include "alert-prelude.h" +#include "alert-unified-log.h" +#include "alert-unified-alert.h" +#include "alert-unified2-alert.h" +#include "alert-debuglog.h" + +#include "util-debug.h" +#include "util-time.h" +#include "util-cpu.h" +#include "util-affinity.h" + +int RunModeErfFileAuto(DetectEngineCtx *de_ctx, char *file) +{ + SCEnter(); + char tname[12]; + uint16_t cpu = 0; + + /* Available cpus */ + uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); + + RunModeInitialize(); + + SCLogDebug("file %s", file); + TimeModeSetOffline(); + + /* create the threads */ + ThreadVars *tv_receiveerf = TmThreadCreatePacketHandler("ReceiveErfFile", + "packetpool","packetpool","pickup-queue","simple","1slot"); + if (tv_receiveerf == NULL) { + printf("ERROR: TmThreadsCreate failed\n"); + exit(EXIT_FAILURE); + } + TmModule *tm_module = TmModuleGetByName("ReceiveErfFile"); + if (tm_module == NULL) { + printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n"); + exit(EXIT_FAILURE); + } + Tm1SlotSetFunc(tv_receiveerf, tm_module, file); + + if (threading_set_cpu_affinity) { + TmThreadSetCPUAffinity(tv_receiveerf, 0); + if (ncpus > 1) + TmThreadSetThreadPriority(tv_receiveerf, PRIO_MEDIUM); + } + + if (TmThreadSpawn(tv_receiveerf) != TM_ECODE_OK) { + printf("ERROR: TmThreadSpawn failed\n"); + exit(EXIT_FAILURE); + } + + ThreadVars *tv_decode1 = TmThreadCreatePacketHandler("Decode & Stream", + "pickup-queue","simple","stream-queue1","simple","varslot"); + if (tv_decode1 == NULL) { + printf("ERROR: TmThreadsCreate failed for Decode1\n"); + exit(EXIT_FAILURE); + } + tm_module = TmModuleGetByName("DecodeErfFile"); + if (tm_module == NULL) { + printf("ERROR: TmModuleGetByName DecodeErfFile failed\n"); + exit(EXIT_FAILURE); + } + TmVarSlotSetFuncAppend(tv_decode1,tm_module,NULL); + + tm_module = TmModuleGetByName("StreamTcp"); + if (tm_module == NULL) { + printf("ERROR: TmModuleGetByName StreamTcp failed\n"); + exit(EXIT_FAILURE); + } + TmVarSlotSetFuncAppend(tv_decode1,tm_module,NULL); + + if (threading_set_cpu_affinity) { + TmThreadSetCPUAffinity(tv_decode1, 0); + if (ncpus > 1) + TmThreadSetThreadPriority(tv_decode1, PRIO_MEDIUM); + } + + if (TmThreadSpawn(tv_decode1) != TM_ECODE_OK) { + printf("ERROR: TmThreadSpawn failed\n"); + exit(EXIT_FAILURE); + } + + /* start with cpu 1 so that if we're creating an odd number of detect + * threads we're not creating the most on CPU0. */ + if (ncpus > 0) + cpu = 1; + + /* always create at least one thread */ + int thread_max = TmThreadGetNbThreads(DETECT_CPU_SET); + if (thread_max == 0) + thread_max = ncpus * threading_detect_ratio; + if (thread_max < 1) + thread_max = 1; + + int thread; + for (thread = 0; thread < thread_max; thread++) { + snprintf(tname, sizeof(tname),"Detect%"PRIu16, thread+1); + if (tname == NULL) + break; + + char *thread_name = SCStrdup(tname); + SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); + + ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","alert-queue1","simple","1slot"); + if (tv_detect_ncpu == NULL) { + printf("ERROR: TmThreadsCreate failed\n"); + exit(EXIT_FAILURE); + } + tm_module = TmModuleGetByName("Detect"); + if (tm_module == NULL) { + printf("ERROR: TmModuleGetByName Detect failed\n"); + exit(EXIT_FAILURE); + } + Tm1SlotSetFunc(tv_detect_ncpu,tm_module,(void *)de_ctx); + + if (threading_set_cpu_affinity) { + TmThreadSetCPUAffinity(tv_detect_ncpu, (int)cpu); + /* If we have more than one core/cpu, the first Detect thread + * (at cpu 0) will have less priority (higher 'nice' value) + * In this case we will set the thread priority to +10 (default is 0) + */ + if (cpu == 0 && ncpus > 1) { + TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW); + } else if (ncpus > 1) { + TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM); + } + } + + char *thread_group_name = SCStrdup("Detect"); + if (thread_group_name == NULL) { + printf("Error allocating memory\n"); + exit(EXIT_FAILURE); + } + tv_detect_ncpu->thread_group_name = thread_group_name; + + if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { + printf("ERROR: TmThreadSpawn failed\n"); + exit(EXIT_FAILURE); + } + + if ((cpu + 1) == ncpus) + cpu = 0; + else + cpu++; + } + + ThreadVars *tv_outputs = TmThreadCreatePacketHandler("Outputs", + "alert-queue1", "simple", "packetpool", "packetpool", "varslot"); + SetupOutputs(tv_outputs); + + if (threading_set_cpu_affinity) { + TmThreadSetCPUAffinity(tv_outputs, 0); + if (ncpus > 1) + TmThreadSetThreadPriority(tv_outputs, PRIO_MEDIUM); + } + + if (TmThreadSpawn(tv_outputs) != TM_ECODE_OK) { + printf("ERROR: TmThreadSpawn failed\n"); + exit(EXIT_FAILURE); + } + + return 0; +} diff --git a/src/runmode-erf-file.h b/src/runmode-erf-file.h new file mode 100644 index 0000000000..78f56de9f2 --- /dev/null +++ b/src/runmode-erf-file.h @@ -0,0 +1,28 @@ +/* Copyright (C) 2007-2010 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 + */ + +#ifndef __RUNMODE_ERF_FILE_H__ +#define __RUNMODE_ERF_FILE_H__ + +int RunModeErfFileAuto(DetectEngineCtx *, char *); + +#endif /* __RUNMODE_ERF_FILE_H__ */ diff --git a/src/runmodes.c b/src/runmodes.c index a50593bd55..75d51e22b6 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -177,158 +177,6 @@ void RunModeInitialize(void) SCLogDebug("threading_detect_ratio %f", threading_detect_ratio); } -int RunModeErfFileAuto(DetectEngineCtx *de_ctx, char *file) -{ - SCEnter(); - char tname[12]; - uint16_t cpu = 0; - - /* Available cpus */ - uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); - - RunModeInitialize(); - - SCLogDebug("file %s", file); - TimeModeSetOffline(); - - /* create the threads */ - ThreadVars *tv_receiveerf = TmThreadCreatePacketHandler("ReceiveErfFile", - "packetpool","packetpool","pickup-queue","simple","1slot"); - if (tv_receiveerf == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); - exit(EXIT_FAILURE); - } - TmModule *tm_module = TmModuleGetByName("ReceiveErfFile"); - if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n"); - exit(EXIT_FAILURE); - } - Tm1SlotSetFunc(tv_receiveerf, tm_module, file); - - if (threading_set_cpu_affinity) { - TmThreadSetCPUAffinity(tv_receiveerf, 0); - if (ncpus > 1) - TmThreadSetThreadPriority(tv_receiveerf, PRIO_MEDIUM); - } - - if (TmThreadSpawn(tv_receiveerf) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); - exit(EXIT_FAILURE); - } - - ThreadVars *tv_decode1 = TmThreadCreatePacketHandler("Decode & Stream", - "pickup-queue","simple","stream-queue1","simple","varslot"); - if (tv_decode1 == NULL) { - printf("ERROR: TmThreadsCreate failed for Decode1\n"); - exit(EXIT_FAILURE); - } - tm_module = TmModuleGetByName("DecodeErfFile"); - if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName DecodeErfFile failed\n"); - exit(EXIT_FAILURE); - } - TmVarSlotSetFuncAppend(tv_decode1,tm_module,NULL); - - tm_module = TmModuleGetByName("StreamTcp"); - if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName StreamTcp failed\n"); - exit(EXIT_FAILURE); - } - TmVarSlotSetFuncAppend(tv_decode1,tm_module,NULL); - - if (threading_set_cpu_affinity) { - TmThreadSetCPUAffinity(tv_decode1, 0); - if (ncpus > 1) - TmThreadSetThreadPriority(tv_decode1, PRIO_MEDIUM); - } - - if (TmThreadSpawn(tv_decode1) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); - exit(EXIT_FAILURE); - } - - /* start with cpu 1 so that if we're creating an odd number of detect - * threads we're not creating the most on CPU0. */ - if (ncpus > 0) - cpu = 1; - - /* always create at least one thread */ - int thread_max = TmThreadGetNbThreads(DETECT_CPU_SET); - if (thread_max == 0) - thread_max = ncpus * threading_detect_ratio; - if (thread_max < 1) - thread_max = 1; - - int thread; - for (thread = 0; thread < thread_max; thread++) { - snprintf(tname, sizeof(tname),"Detect%"PRIu16, thread+1); - if (tname == NULL) - break; - - char *thread_name = SCStrdup(tname); - SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); - - ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(thread_name,"stream-queue1","simple","alert-queue1","simple","1slot"); - if (tv_detect_ncpu == NULL) { - printf("ERROR: TmThreadsCreate failed\n"); - exit(EXIT_FAILURE); - } - tm_module = TmModuleGetByName("Detect"); - if (tm_module == NULL) { - printf("ERROR: TmModuleGetByName Detect failed\n"); - exit(EXIT_FAILURE); - } - Tm1SlotSetFunc(tv_detect_ncpu,tm_module,(void *)de_ctx); - - if (threading_set_cpu_affinity) { - TmThreadSetCPUAffinity(tv_detect_ncpu, (int)cpu); - /* If we have more than one core/cpu, the first Detect thread - * (at cpu 0) will have less priority (higher 'nice' value) - * In this case we will set the thread priority to +10 (default is 0) - */ - if (cpu == 0 && ncpus > 1) { - TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW); - } else if (ncpus > 1) { - TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM); - } - } - - char *thread_group_name = SCStrdup("Detect"); - if (thread_group_name == NULL) { - printf("Error allocating memory\n"); - exit(EXIT_FAILURE); - } - tv_detect_ncpu->thread_group_name = thread_group_name; - - if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); - exit(EXIT_FAILURE); - } - - if ((cpu + 1) == ncpus) - cpu = 0; - else - cpu++; - } - - ThreadVars *tv_outputs = TmThreadCreatePacketHandler("Outputs", - "alert-queue1", "simple", "packetpool", "packetpool", "varslot"); - SetupOutputs(tv_outputs); - - if (threading_set_cpu_affinity) { - TmThreadSetCPUAffinity(tv_outputs, 0); - if (ncpus > 1) - TmThreadSetThreadPriority(tv_outputs, PRIO_MEDIUM); - } - - if (TmThreadSpawn(tv_outputs) != TM_ECODE_OK) { - printf("ERROR: TmThreadSpawn failed\n"); - exit(EXIT_FAILURE); - } - - return 0; -} - /** * * \brief Sets up support for reading from a DAG card. diff --git a/src/runmodes.h b/src/runmodes.h index c3d7ef6d2d..2a447cdc9f 100644 --- a/src/runmodes.h +++ b/src/runmodes.h @@ -32,8 +32,8 @@ void SetupOutputs(ThreadVars *); #include "runmode-pfring.h" #include "runmode-nfq.h" #include "runmode-ipfw.h" +#include "runmode-erf-file.h" -int RunModeErfFileAuto(DetectEngineCtx *, char *); int RunModeErfDagAuto(DetectEngineCtx *, char *); void RunModeShutDown(void);