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-cpu.h"
29 #include "util-affinity.h"
30 
31 #include "util-runmodes.h"
32 
34 {
35  return "autofp";
36 }
37 
39 {
40  RunModeRegisterNewRunMode(RUNMODE_ERF_FILE, "single", "Single threaded ERF file mode",
41  RunModeErfFileSingle, NULL);
42 
44  "Multi threaded ERF file mode. Packets from "
45  "each flow are assigned to a single detect thread",
46  RunModeErfFileAutoFp, NULL);
47 }
48 
50 {
51  const char *file;
52 
53  SCEnter();
54 
55  if (SCConfGetNonNull("erf-file.file", &file) == 0) {
56  FatalError("Failed to get erf-file.file from config.");
57  }
58 
60 
61  /* Basically the same setup as PCAP files. */
62 
64  "packetpool", "packetpool",
65  "packetpool", "packetpool",
66  "pktacqloop");
67  if (tv == NULL) {
68  printf("ERROR: TmThreadsCreate failed\n");
69  exit(EXIT_FAILURE);
70  }
71 
72  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
73  if (tm_module == NULL) {
74  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
75  exit(EXIT_FAILURE);
76  }
77  TmSlotSetFuncAppend(tv, tm_module, file);
78 
79  tm_module = TmModuleGetByName("DecodeErfFile");
80  if (tm_module == NULL) {
81  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
82  exit(EXIT_FAILURE);
83  }
84  TmSlotSetFuncAppend(tv, tm_module, NULL);
85 
86  tm_module = TmModuleGetByName("FlowWorker");
87  if (tm_module == NULL) {
88  FatalError("TmModuleGetByName for FlowWorker failed");
89  }
90  TmSlotSetFuncAppend(tv, tm_module, NULL);
91 
92  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
93  printf("ERROR: TmThreadSpawn failed\n");
94  exit(EXIT_FAILURE);
95  }
96 
97  SCLogInfo("RunModeErfFileSingle initialised");
98 
99  SCReturnInt(0);
100 }
101 
103 {
104  SCEnter();
105  char tname[TM_THREAD_NAME_MAX];
106  char qname[TM_QUEUE_NAME_MAX];
107  uint16_t cpu = 0;
108  char *queues = NULL;
109  uint16_t thread;
110 
111  const char *file = NULL;
112  if (SCConfGetNonNull("erf-file.file", &file) == 0) {
113  FatalError("Failed retrieving erf-file.file from config");
114  }
115 
117 
118  /* Available cpus */
119  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
120 
121  /* start with cpu 1 so that if we're creating an odd number of detect
122  * threads we're not creating the most on CPU0. */
123  if (ncpus > 0)
124  cpu = 1;
125 
126  /* always create at least one thread */
127  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
128  if (thread_max == 0)
129  thread_max = ncpus * threading_detect_ratio;
130  if (thread_max < 1)
131  thread_max = 1;
132  if (thread_max > 1024)
133  thread_max = 1024;
134 
135  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
136  if (queues == NULL) {
137  FatalError("RunmodeAutoFpCreatePickupQueuesString failed");
138  }
139 
140  /* create the threads */
141  ThreadVars *tv =
143  "packetpool", "packetpool",
144  queues, "flow",
145  "pktacqloop");
146  SCFree(queues);
147 
148  if (tv == NULL) {
149  printf("ERROR: TmThreadsCreate failed\n");
150  exit(EXIT_FAILURE);
151  }
152  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
153  if (tm_module == NULL) {
154  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
155  exit(EXIT_FAILURE);
156  }
157  TmSlotSetFuncAppend(tv, tm_module, file);
158 
159  tm_module = TmModuleGetByName("DecodeErfFile");
160  if (tm_module == NULL) {
161  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
162  exit(EXIT_FAILURE);
163  }
164  TmSlotSetFuncAppend(tv, tm_module, NULL);
165 
168  if (ncpus > 1)
170  }
171 
172  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
173  printf("ERROR: TmThreadSpawn failed\n");
174  exit(EXIT_FAILURE);
175  }
176 
177  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
178  snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread + 1);
179  snprintf(qname, sizeof(qname), "pickup%d", thread + 1);
180 
181  SCLogDebug("tname %s, qname %s", tname, qname);
182 
183  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
184 
185  ThreadVars *tv_detect_ncpu =
187  qname, "flow",
188  "packetpool", "packetpool",
189  "varslot");
190  if (tv_detect_ncpu == NULL) {
191  printf("ERROR: TmThreadsCreate failed\n");
192  exit(EXIT_FAILURE);
193  }
194 
195  tm_module = TmModuleGetByName("FlowWorker");
196  if (tm_module == NULL) {
197  FatalError("TmModuleGetByName for FlowWorker failed");
198  }
199  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
200 
202  TmThreadSetCPUAffinity(tv_detect_ncpu, cpu);
203  /* If we have more than one core/cpu, the first Detect thread
204  * (at cpu 0) will have less priority (higher 'nice' value)
205  * In this case we will set the thread priority to +10 (default is 0)
206  */
207  if (cpu == 0 && ncpus > 1) {
208  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW);
209  } else if (ncpus > 1) {
210  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM);
211  }
212  }
213 
214  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
215  printf("ERROR: TmThreadSpawn failed\n");
216  exit(EXIT_FAILURE);
217  }
218 
219  if ((cpu + 1) == ncpus)
220  cpu = 0;
221  else
222  cpu++;
223  }
224 
225  SCLogInfo("RunModeErfFileAutoFp initialised");
226 
227  SCReturnInt(0);
228 }
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:832
tm-threads.h
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1710
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:38
RunModeErfFileGetDefaultMode
const char * RunModeErfFileGetDefaultMode(void)
Definition: runmode-erf-file.c:33
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:1074
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
PRIO_MEDIUM
@ PRIO_MEDIUM
Definition: threads.h:89
runmode-erf-file.h
util-runmodes.h
thread_name_autofp
const char * thread_name_autofp
Definition: runmodes.c:65
WORKER_CPU_SET
@ WORKER_CPU_SET
Definition: util-affinity.h:58
TmThreadSetThreadPriority
TmEcode TmThreadSetThreadPriority(ThreadVars *tv, int prio)
Set the thread options (thread priority).
Definition: tm-threads.c:784
thread_name_single
const char * thread_name_single
Definition: runmodes.c:66
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:81
TmModuleGetByName
TmModule * TmModuleGetByName(const char *name)
get a tm module ptr by name
Definition: tm-modules.c:46
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:284
SCConfGetNonNull
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
Definition: conf.c:380
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
util-affinity.h
TM_QUEUE_NAME_MAX
#define TM_QUEUE_NAME_MAX
Definition: tm-threads.h:48
conf.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum SCRunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:481
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
TmModule_
Definition: tm-modules.h:47
TmSlotSetFuncAppend
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, const void *data)
Appends a new entry to the slots.
Definition: tm-threads.c:668
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:857
RunModeErfFileSingle
int RunModeErfFileSingle(void)
Definition: runmode-erf-file.c:49
FatalError
#define FatalError(...)
Definition: util-debug.h:517
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
RunModeErfFileAutoFp
int RunModeErfFileAutoFp(void)
Definition: runmode-erf-file.c:102
RUNMODE_ERF_FILE
@ RUNMODE_ERF_FILE
Definition: runmodes.h:34
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:288
PRIO_LOW
@ PRIO_LOW
Definition: threads.h:88
threading_detect_ratio
float threading_detect_ratio
Definition: runmodes.c:953
output.h