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 
51 {
52  const char *file;
53 
54  SCEnter();
55 
56  if (ConfGet("erf-file.file", &file) == 0) {
57  FatalError("Failed to get erf-file.file from config.");
58  }
59 
61 
62  /* Basically the same setup as PCAP files. */
63 
65  "packetpool", "packetpool",
66  "packetpool", "packetpool",
67  "pktacqloop");
68  if (tv == NULL) {
69  printf("ERROR: TmThreadsCreate failed\n");
70  exit(EXIT_FAILURE);
71  }
72 
73  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
74  if (tm_module == NULL) {
75  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
76  exit(EXIT_FAILURE);
77  }
78  TmSlotSetFuncAppend(tv, tm_module, file);
79 
80  tm_module = TmModuleGetByName("DecodeErfFile");
81  if (tm_module == NULL) {
82  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
83  exit(EXIT_FAILURE);
84  }
85  TmSlotSetFuncAppend(tv, tm_module, NULL);
86 
87  tm_module = TmModuleGetByName("FlowWorker");
88  if (tm_module == NULL) {
89  FatalError("TmModuleGetByName for FlowWorker failed");
90  }
91  TmSlotSetFuncAppend(tv, tm_module, NULL);
92 
93  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
94  printf("ERROR: TmThreadSpawn failed\n");
95  exit(EXIT_FAILURE);
96  }
97 
98  SCLogInfo("RunModeErfFileSingle initialised");
99 
100  SCReturnInt(0);
101 }
102 
104 {
105  SCEnter();
106  char tname[TM_THREAD_NAME_MAX];
107  char qname[TM_QUEUE_NAME_MAX];
108  uint16_t cpu = 0;
109  char *queues = NULL;
110  uint16_t thread;
111 
112  const char *file = NULL;
113  if (ConfGet("erf-file.file", &file) == 0) {
114  FatalError("Failed retrieving erf-file.file from config");
115  }
116 
118 
119  /* Available cpus */
120  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
121 
122  /* start with cpu 1 so that if we're creating an odd number of detect
123  * threads we're not creating the most on CPU0. */
124  if (ncpus > 0)
125  cpu = 1;
126 
127  /* always create at least one thread */
128  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
129  if (thread_max == 0)
130  thread_max = ncpus * threading_detect_ratio;
131  if (thread_max < 1)
132  thread_max = 1;
133  if (thread_max > 1024)
134  thread_max = 1024;
135 
136  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
137  if (queues == NULL) {
138  FatalError("RunmodeAutoFpCreatePickupQueuesString failed");
139  }
140 
141  /* create the threads */
142  ThreadVars *tv =
144  "packetpool", "packetpool",
145  queues, "flow",
146  "pktacqloop");
147  SCFree(queues);
148 
149  if (tv == NULL) {
150  printf("ERROR: TmThreadsCreate failed\n");
151  exit(EXIT_FAILURE);
152  }
153  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
154  if (tm_module == NULL) {
155  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
156  exit(EXIT_FAILURE);
157  }
158  TmSlotSetFuncAppend(tv, tm_module, file);
159 
160  tm_module = TmModuleGetByName("DecodeErfFile");
161  if (tm_module == NULL) {
162  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
163  exit(EXIT_FAILURE);
164  }
165  TmSlotSetFuncAppend(tv, tm_module, NULL);
166 
169  if (ncpus > 1)
171  }
172 
173  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
174  printf("ERROR: TmThreadSpawn failed\n");
175  exit(EXIT_FAILURE);
176  }
177 
178  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
179  snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread + 1);
180  snprintf(qname, sizeof(qname), "pickup%d", thread + 1);
181 
182  SCLogDebug("tname %s, qname %s", tname, qname);
183 
184  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
185 
186  ThreadVars *tv_detect_ncpu =
188  qname, "flow",
189  "packetpool", "packetpool",
190  "varslot");
191  if (tv_detect_ncpu == NULL) {
192  printf("ERROR: TmThreadsCreate failed\n");
193  exit(EXIT_FAILURE);
194  }
195 
196  tm_module = TmModuleGetByName("FlowWorker");
197  if (tm_module == NULL) {
198  FatalError("TmModuleGetByName for FlowWorker failed");
199  }
200  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
201 
203  TmThreadSetCPUAffinity(tv_detect_ncpu, cpu);
204  /* If we have more than one core/cpu, the first Detect thread
205  * (at cpu 0) will have less priority (higher 'nice' value)
206  * In this case we will set the thread priority to +10 (default is 0)
207  */
208  if (cpu == 0 && ncpus > 1) {
209  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW);
210  } else if (ncpus > 1) {
211  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM);
212  }
213  }
214 
215  TmThreadSetGroupName(tv_detect_ncpu, "Detect");
216 
217  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
218  printf("ERROR: TmThreadSpawn failed\n");
219  exit(EXIT_FAILURE);
220  }
221 
222  if ((cpu + 1) == ncpus)
223  cpu = 0;
224  else
225  cpu++;
226  }
227 
228  SCLogInfo("RunModeErfFileAutoFp initialised");
229 
230  SCReturnInt(0);
231 }
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:67
TmThreadSetCPUAffinity
TmEcode TmThreadSetCPUAffinity(ThreadVars *tv, uint16_t cpu)
Set the thread options (cpu affinity).
Definition: tm-threads.c:804
tm-threads.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1650
detect-engine.h
threading_set_cpu_affinity
bool threading_set_cpu_affinity
Definition: runmodes.c:61
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:1033
TmThreadSetGroupName
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1607
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
RUNMODE_ERF_FILE
@ RUNMODE_ERF_FILE
Definition: runmodes.h:34
runmode-erf-file.h
util-runmodes.h
thread_name_autofp
const char * thread_name_autofp
Definition: runmodes.c:65
TmThreadSetThreadPriority
TmEcode TmThreadSetThreadPriority(ThreadVars *tv, int prio)
Set the thread options (thread priority).
Definition: tm-threads.c:756
thread_name_single
const char * thread_name_single
Definition: runmodes.c:66
WORKER_CPU_SET
@ WORKER_CPU_SET
Definition: util-affinity.h:53
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:82
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
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:472
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:640
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
PRIO_LOW
@ PRIO_LOW
Definition: threads.h:88
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:829
RunModeErfFileSingle
int RunModeErfFileSingle(void)
Definition: runmode-erf-file.c:50
PRIO_MEDIUM
@ PRIO_MEDIUM
Definition: threads.h:89
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:103
SCFree
#define SCFree(p)
Definition: util-mem.h:61
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:945
output.h