suricata
runmode-erf-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "suricata-common.h"
19 #include "tm-threads.h"
20 #include "conf.h"
21 #include "runmodes.h"
22 #include "runmode-erf-file.h"
23 #include "output.h"
24 
25 #include "detect-engine.h"
26 
27 #include "util-debug.h"
28 #include "util-time.h"
29 #include "util-cpu.h"
30 #include "util-affinity.h"
31 
32 #include "util-runmodes.h"
33 
35 {
36  return "autofp";
37 }
38 
40 {
41  RunModeRegisterNewRunMode(RUNMODE_ERF_FILE, "single", "Single threaded ERF file mode",
42  RunModeErfFileSingle, NULL);
43 
45  "Multi threaded ERF file mode. Packets from "
46  "each flow are assigned to a single detect thread",
47  RunModeErfFileAutoFp, NULL);
48 
49  return;
50 }
51 
53 {
54  const char *file;
55 
56  SCEnter();
57 
58  if (ConfGet("erf-file.file", &file) == 0) {
59  FatalError("Failed to get erf-file.file from config.");
60  }
61 
63 
64  /* Basically the same setup as PCAP files. */
65 
67  "packetpool", "packetpool",
68  "packetpool", "packetpool",
69  "pktacqloop");
70  if (tv == NULL) {
71  printf("ERROR: TmThreadsCreate failed\n");
72  exit(EXIT_FAILURE);
73  }
74 
75  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
76  if (tm_module == NULL) {
77  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
78  exit(EXIT_FAILURE);
79  }
80  TmSlotSetFuncAppend(tv, tm_module, file);
81 
82  tm_module = TmModuleGetByName("DecodeErfFile");
83  if (tm_module == NULL) {
84  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
85  exit(EXIT_FAILURE);
86  }
87  TmSlotSetFuncAppend(tv, tm_module, NULL);
88 
89  tm_module = TmModuleGetByName("FlowWorker");
90  if (tm_module == NULL) {
91  FatalError("TmModuleGetByName for FlowWorker failed");
92  }
93  TmSlotSetFuncAppend(tv, tm_module, NULL);
94 
95  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
96  printf("ERROR: TmThreadSpawn failed\n");
97  exit(EXIT_FAILURE);
98  }
99 
100  SCLogInfo("RunModeErfFileSingle initialised");
101 
102  SCReturnInt(0);
103 }
104 
106 {
107  SCEnter();
108  char tname[TM_THREAD_NAME_MAX];
109  char qname[TM_QUEUE_NAME_MAX];
110  uint16_t cpu = 0;
111  char *queues = NULL;
112  uint16_t thread;
113 
114  const char *file = NULL;
115  if (ConfGet("erf-file.file", &file) == 0) {
116  FatalError("Failed retrieving erf-file.file from config");
117  }
118 
120 
121  /* Available cpus */
122  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
123 
124  /* start with cpu 1 so that if we're creating an odd number of detect
125  * threads we're not creating the most on CPU0. */
126  if (ncpus > 0)
127  cpu = 1;
128 
129  /* always create at least one thread */
130  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
131  if (thread_max == 0)
132  thread_max = ncpus * threading_detect_ratio;
133  if (thread_max < 1)
134  thread_max = 1;
135  if (thread_max > 1024)
136  thread_max = 1024;
137 
138  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
139  if (queues == NULL) {
140  FatalError("RunmodeAutoFpCreatePickupQueuesString failed");
141  }
142 
143  /* create the threads */
144  ThreadVars *tv =
146  "packetpool", "packetpool",
147  queues, "flow",
148  "pktacqloop");
149  SCFree(queues);
150 
151  if (tv == NULL) {
152  printf("ERROR: TmThreadsCreate failed\n");
153  exit(EXIT_FAILURE);
154  }
155  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
156  if (tm_module == NULL) {
157  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
158  exit(EXIT_FAILURE);
159  }
160  TmSlotSetFuncAppend(tv, tm_module, file);
161 
162  tm_module = TmModuleGetByName("DecodeErfFile");
163  if (tm_module == NULL) {
164  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
165  exit(EXIT_FAILURE);
166  }
167  TmSlotSetFuncAppend(tv, tm_module, NULL);
168 
171  if (ncpus > 1)
173  }
174 
175  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
176  printf("ERROR: TmThreadSpawn failed\n");
177  exit(EXIT_FAILURE);
178  }
179 
180  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
181  snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread + 1);
182  snprintf(qname, sizeof(qname), "pickup%d", thread + 1);
183 
184  SCLogDebug("tname %s, qname %s", tname, qname);
185 
186  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
187 
188  ThreadVars *tv_detect_ncpu =
190  qname, "flow",
191  "packetpool", "packetpool",
192  "varslot");
193  if (tv_detect_ncpu == NULL) {
194  printf("ERROR: TmThreadsCreate failed\n");
195  exit(EXIT_FAILURE);
196  }
197 
198  tm_module = TmModuleGetByName("FlowWorker");
199  if (tm_module == NULL) {
200  FatalError("TmModuleGetByName for FlowWorker failed");
201  }
202  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
203 
205  TmThreadSetCPUAffinity(tv_detect_ncpu, cpu);
206  /* If we have more than one core/cpu, the first Detect thread
207  * (at cpu 0) will have less priority (higher 'nice' value)
208  * In this case we will set the thread priority to +10 (default is 0)
209  */
210  if (cpu == 0 && ncpus > 1) {
211  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW);
212  } else if (ncpus > 1) {
213  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM);
214  }
215  }
216 
217  TmThreadSetGroupName(tv_detect_ncpu, "Detect");
218 
219  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
220  printf("ERROR: TmThreadSpawn failed\n");
221  exit(EXIT_FAILURE);
222  }
223 
224  if ((cpu + 1) == ncpus)
225  cpu = 0;
226  else
227  cpu++;
228  }
229 
230  SCLogInfo("RunModeErfFileAutoFp initialised");
231 
232  SCReturnInt(0);
233 }
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:81
TmThreadSetCPUAffinity
TmEcode TmThreadSetCPUAffinity(ThreadVars *tv, uint16_t cpu)
Set the thread options (cpu affinity).
Definition: tm-threads.c:807
tm-threads.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1660
detect-engine.h
threading_set_cpu_affinity
bool threading_set_cpu_affinity
Definition: runmodes.c:75
RunModeErfFileRegister
void RunModeErfFileRegister(void)
Definition: runmode-erf-file.c:39
RunModeErfFileGetDefaultMode
const char * RunModeErfFileGetDefaultMode(void)
Definition: runmode-erf-file.c:34
TmThreadCreatePacketHandler
ThreadVars * TmThreadCreatePacketHandler(const char *name, const char *inq_name, const char *inqh_name, const char *outq_name, const char *outqh_name, const char *slots)
Creates and returns a TV instance for a Packet Processing Thread. This function doesn't support custo...
Definition: tm-threads.c:1036
TmThreadSetGroupName
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1617
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
RUNMODE_ERF_FILE
@ RUNMODE_ERF_FILE
Definition: runmodes.h:35
runmode-erf-file.h
util-runmodes.h
thread_name_autofp
const char * thread_name_autofp
Definition: runmodes.c:79
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), void(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:491
TmThreadSetThreadPriority
TmEcode TmThreadSetThreadPriority(ThreadVars *tv, int prio)
Set the thread options (thread priority).
Definition: tm-threads.c:759
thread_name_single
const char * thread_name_single
Definition: runmodes.c:80
TM_THREAD_NAME_MAX
#define TM_THREAD_NAME_MAX
Definition: tm-threads.h:49
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
TmModuleGetByName
TmModule * TmModuleGetByName(const char *name)
get a tm module ptr by name
Definition: tm-modules.c:53
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
util-debug.h
RunmodeAutoFpCreatePickupQueuesString
char * RunmodeAutoFpCreatePickupQueuesString(int n)
create a queue string for autofp to pass to the flow queue handler.
Definition: util-runmodes.c:56
util-cpu.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
util-affinity.h
util-time.h
TM_QUEUE_NAME_MAX
#define TM_QUEUE_NAME_MAX
Definition: tm-threads.h:48
conf.h
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
TmModule_
Definition: tm-modules.h:44
TmSlotSetFuncAppend
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, const void *data)
Appends a new entry to the slots.
Definition: tm-threads.c:642
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:832
RunModeErfFileSingle
int RunModeErfFileSingle(void)
Definition: runmode-erf-file.c:52
FatalError
#define FatalError(...)
Definition: util-debug.h:502
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
RunModeErfFileAutoFp
int RunModeErfFileAutoFp(void)
Definition: runmode-erf-file.c:105
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PRIO_MEDIUM
@ PRIO_MEDIUM
Definition: threads.h:89
PRIO_LOW
@ PRIO_LOW
Definition: threads.h:88
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:108
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
threading_detect_ratio
float threading_detect_ratio
Definition: runmodes.c:978
output.h
WORKER_CPU_SET
@ WORKER_CPU_SET
Definition: util-affinity.h:53