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 {
42  "Single threaded ERF file mode",
44 
46  "Multi threaded ERF file mode. Packets from "
47  "each flow are assigned to a single detect thread",
49 
50  return;
51 }
52 
54 {
55  const char *file;
56 
57  SCEnter();
58 
59  if (ConfGet("erf-file.file", &file) == 0) {
60  FatalError(SC_ERR_FATAL, "Failed to get erf-file.file from config.");
61  }
62 
64 
66 
67  /* Basically the same setup as PCAP files. */
68 
70  "packetpool", "packetpool",
71  "packetpool", "packetpool",
72  "pktacqloop");
73  if (tv == NULL) {
74  printf("ERROR: TmThreadsCreate failed\n");
75  exit(EXIT_FAILURE);
76  }
77 
78  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
79  if (tm_module == NULL) {
80  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
81  exit(EXIT_FAILURE);
82  }
83  TmSlotSetFuncAppend(tv, tm_module, file);
84 
85  tm_module = TmModuleGetByName("DecodeErfFile");
86  if (tm_module == NULL) {
87  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
88  exit(EXIT_FAILURE);
89  }
90  TmSlotSetFuncAppend(tv, tm_module, NULL);
91 
92  tm_module = TmModuleGetByName("FlowWorker");
93  if (tm_module == NULL) {
94  FatalError(SC_ERR_FATAL, "TmModuleGetByName for FlowWorker failed");
95  }
96  TmSlotSetFuncAppend(tv, tm_module, NULL);
97 
98  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
99  printf("ERROR: TmThreadSpawn failed\n");
100  exit(EXIT_FAILURE);
101  }
102 
103  SCLogInfo("RunModeErfFileSingle initialised");
104 
105  SCReturnInt(0);
106 }
107 
109 {
110  SCEnter();
111  char tname[TM_THREAD_NAME_MAX];
112  char qname[TM_QUEUE_NAME_MAX];
113  uint16_t cpu = 0;
114  char *queues = NULL;
115  uint16_t thread;
116 
118 
119  const char *file = NULL;
120  if (ConfGet("erf-file.file", &file) == 0) {
122  "Failed retrieving erf-file.file from config");
123  }
124 
126 
127  /* Available cpus */
128  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
129 
130  /* start with cpu 1 so that if we're creating an odd number of detect
131  * threads we're not creating the most on CPU0. */
132  if (ncpus > 0)
133  cpu = 1;
134 
135  /* always create at least one thread */
136  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
137  if (thread_max == 0)
138  thread_max = ncpus * threading_detect_ratio;
139  if (thread_max < 1)
140  thread_max = 1;
141  if (thread_max > 1024)
142  thread_max = 1024;
143 
144  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
145  if (queues == NULL) {
147  "RunmodeAutoFpCreatePickupQueuesString failed");
148  }
149 
150  /* create the threads */
151  ThreadVars *tv =
153  "packetpool", "packetpool",
154  queues, "flow",
155  "pktacqloop");
156  SCFree(queues);
157 
158  if (tv == NULL) {
159  printf("ERROR: TmThreadsCreate failed\n");
160  exit(EXIT_FAILURE);
161  }
162  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
163  if (tm_module == NULL) {
164  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
165  exit(EXIT_FAILURE);
166  }
167  TmSlotSetFuncAppend(tv, tm_module, file);
168 
169  tm_module = TmModuleGetByName("DecodeErfFile");
170  if (tm_module == NULL) {
171  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
172  exit(EXIT_FAILURE);
173  }
174  TmSlotSetFuncAppend(tv, tm_module, NULL);
175 
178  if (ncpus > 1)
180  }
181 
182  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
183  printf("ERROR: TmThreadSpawn failed\n");
184  exit(EXIT_FAILURE);
185  }
186 
187  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
188  snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread+1);
189  snprintf(qname, sizeof(qname), "pickup%u", thread+1);
190 
191  SCLogDebug("tname %s, qname %s", tname, qname);
192 
193  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
194 
195  ThreadVars *tv_detect_ncpu =
197  qname, "flow",
198  "packetpool", "packetpool",
199  "varslot");
200  if (tv_detect_ncpu == NULL) {
201  printf("ERROR: TmThreadsCreate failed\n");
202  exit(EXIT_FAILURE);
203  }
204 
205  tm_module = TmModuleGetByName("FlowWorker");
206  if (tm_module == NULL) {
208  "TmModuleGetByName for FlowWorker failed");
209  }
210  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
211 
213  TmThreadSetCPUAffinity(tv_detect_ncpu, (int)cpu);
214  /* If we have more than one core/cpu, the first Detect thread
215  * (at cpu 0) will have less priority (higher 'nice' value)
216  * In this case we will set the thread priority to +10 (default is 0)
217  */
218  if (cpu == 0 && ncpus > 1) {
219  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW);
220  } else if (ncpus > 1) {
221  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM);
222  }
223  }
224 
225  TmThreadSetGroupName(tv_detect_ncpu, "Detect");
226 
227  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
228  printf("ERROR: TmThreadSpawn failed\n");
229  exit(EXIT_FAILURE);
230  }
231 
232  if ((cpu + 1) == ncpus)
233  cpu = 0;
234  else
235  cpu++;
236  }
237 
238  SCLogInfo("RunModeErfFileAutoFp initialised");
239 
240  SCReturnInt(0);
241 }
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:66
TmThreadSetCPUAffinity
TmEcode TmThreadSetCPUAffinity(ThreadVars *tv, uint16_t cpu)
Set the thread options (cpu affinity).
Definition: tm-threads.c:835
tm-threads.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1708
detect-engine.h
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:1065
TmThreadSetGroupName
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1665
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
RunModeInitialize
void RunModeInitialize(void)
Definition: runmodes.c:921
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:64
WORKER_CPU_SET
@ WORKER_CPU_SET
Definition: util-affinity.h:52
TmThreadSetThreadPriority
TmEcode TmThreadSetThreadPriority(ThreadVars *tv, int prio)
Set the thread options (thread priority).
Definition: tm-threads.c:788
thread_name_single
const char * thread_name_single
Definition: runmodes.c:65
TM_THREAD_NAME_MAX
#define TM_THREAD_NAME_MAX
Definition: tm-threads.h:48
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
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:330
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:57
util-cpu.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
util-affinity.h
util-time.h
threading_set_cpu_affinity
int threading_set_cpu_affinity
Definition: runmodes.c:61
TM_QUEUE_NAME_MAX
#define TM_QUEUE_NAME_MAX
Definition: tm-threads.h:47
conf.h
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
TmModule_
Definition: tm-modules.h:43
TmSlotSetFuncAppend
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, const void *data)
Appends a new entry to the slots.
Definition: tm-threads.c:647
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:103
suricata-common.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void))
Registers a new runmode.
Definition: runmodes.c:433
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:860
RunModeErfFileSingle
int RunModeErfFileSingle(void)
Definition: runmode-erf-file.c:53
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
RunModeErfFileAutoFp
int RunModeErfFileAutoFp(void)
Definition: runmode-erf-file.c:108
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
PRIO_LOW
@ PRIO_LOW
Definition: threads.h:90
PRIO_MEDIUM
@ PRIO_MEDIUM
Definition: threads.h:91
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:106
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
threading_detect_ratio
float threading_detect_ratio
Definition: runmodes.c:916
output.h