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  SCLogError(SC_ERR_RUNMODE, "Failed to get erf-file.file from config.");
61  exit(EXIT_FAILURE);
62  }
63 
65 
67 
68  /* Basically the same setup as PCAP files. */
69 
71  "packetpool", "packetpool",
72  "packetpool", "packetpool",
73  "pktacqloop");
74  if (tv == NULL) {
75  printf("ERROR: TmThreadsCreate failed\n");
76  exit(EXIT_FAILURE);
77  }
78 
79  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
80  if (tm_module == NULL) {
81  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
82  exit(EXIT_FAILURE);
83  }
84  TmSlotSetFuncAppend(tv, tm_module, file);
85 
86  tm_module = TmModuleGetByName("DecodeErfFile");
87  if (tm_module == NULL) {
88  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
89  exit(EXIT_FAILURE);
90  }
91  TmSlotSetFuncAppend(tv, tm_module, NULL);
92 
93  tm_module = TmModuleGetByName("FlowWorker");
94  if (tm_module == NULL) {
95  SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName for FlowWorker failed");
96  exit(EXIT_FAILURE);
97  }
98  TmSlotSetFuncAppend(tv, tm_module, NULL);
99 
100  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
101  printf("ERROR: TmThreadSpawn failed\n");
102  exit(EXIT_FAILURE);
103  }
104 
105  SCLogInfo("RunModeErfFileSingle initialised");
106 
107  SCReturnInt(0);
108 }
109 
111 {
112  SCEnter();
113  char tname[TM_THREAD_NAME_MAX];
114  char qname[TM_QUEUE_NAME_MAX];
115  uint16_t cpu = 0;
116  char *queues = NULL;
117  uint16_t thread;
118 
120 
121  const char *file = NULL;
122  if (ConfGet("erf-file.file", &file) == 0) {
124  "Failed retrieving erf-file.file from config");
125  exit(EXIT_FAILURE);
126  }
127 
129 
130  /* Available cpus */
131  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
132 
133  /* start with cpu 1 so that if we're creating an odd number of detect
134  * threads we're not creating the most on CPU0. */
135  if (ncpus > 0)
136  cpu = 1;
137 
138  /* always create at least one thread */
139  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
140  if (thread_max == 0)
141  thread_max = ncpus * threading_detect_ratio;
142  if (thread_max < 1)
143  thread_max = 1;
144  if (thread_max > 1024)
145  thread_max = 1024;
146 
147  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
148  if (queues == NULL) {
149  SCLogError(SC_ERR_RUNMODE, "RunmodeAutoFpCreatePickupQueuesString failed");
150  exit(EXIT_FAILURE);
151  }
152 
153  /* create the threads */
154  ThreadVars *tv =
156  "packetpool", "packetpool",
157  queues, "flow",
158  "pktacqloop");
159  SCFree(queues);
160 
161  if (tv == NULL) {
162  printf("ERROR: TmThreadsCreate failed\n");
163  exit(EXIT_FAILURE);
164  }
165  TmModule *tm_module = TmModuleGetByName("ReceiveErfFile");
166  if (tm_module == NULL) {
167  printf("ERROR: TmModuleGetByName failed for ReceiveErfFile\n");
168  exit(EXIT_FAILURE);
169  }
170  TmSlotSetFuncAppend(tv, tm_module, file);
171 
172  tm_module = TmModuleGetByName("DecodeErfFile");
173  if (tm_module == NULL) {
174  printf("ERROR: TmModuleGetByName DecodeErfFile failed\n");
175  exit(EXIT_FAILURE);
176  }
177  TmSlotSetFuncAppend(tv, tm_module, NULL);
178 
180  TmThreadSetCPUAffinity(tv, 0);
181  if (ncpus > 1)
183  }
184 
185  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
186  printf("ERROR: TmThreadSpawn failed\n");
187  exit(EXIT_FAILURE);
188  }
189 
190  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
191  snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread+1);
192  snprintf(qname, sizeof(qname), "pickup%u", thread+1);
193 
194  SCLogDebug("tname %s, qname %s", tname, qname);
195 
196  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
197 
198  ThreadVars *tv_detect_ncpu =
200  qname, "flow",
201  "packetpool", "packetpool",
202  "varslot");
203  if (tv_detect_ncpu == NULL) {
204  printf("ERROR: TmThreadsCreate failed\n");
205  exit(EXIT_FAILURE);
206  }
207 
208  tm_module = TmModuleGetByName("FlowWorker");
209  if (tm_module == NULL) {
210  SCLogError(SC_ERR_RUNMODE, "TmModuleGetByName for FlowWorker failed");
211  exit(EXIT_FAILURE);
212  }
213  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
214 
216  TmThreadSetCPUAffinity(tv_detect_ncpu, (int)cpu);
217  /* If we have more than one core/cpu, the first Detect thread
218  * (at cpu 0) will have less priority (higher 'nice' value)
219  * In this case we will set the thread priority to +10 (default is 0)
220  */
221  if (cpu == 0 && ncpus > 1) {
222  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_LOW);
223  } else if (ncpus > 1) {
224  TmThreadSetThreadPriority(tv_detect_ncpu, PRIO_MEDIUM);
225  }
226  }
227 
228  TmThreadSetGroupName(tv_detect_ncpu, "Detect");
229 
230  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
231  printf("ERROR: TmThreadSpawn failed\n");
232  exit(EXIT_FAILURE);
233  }
234 
235  if ((cpu + 1) == ncpus)
236  cpu = 0;
237  else
238  cpu++;
239  }
240 
241  SCLogInfo("RunModeErfFileAutoFp initialised");
242 
243  SCReturnInt(0);
244 }
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, const void *data)
Appends a new entry to the slots.
Definition: tm-threads.c:816
#define SCLogDebug(...)
Definition: util-debug.h:335
float threading_detect_ratio
Definition: runmodes.c:895
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:1029
int RunModeErfFileAutoFp(void)
void RunModeInitialize(void)
Definition: runmodes.c:900
const char * RunModeErfFileGetDefaultMode(void)
TmEcode TmThreadSetThreadPriority(ThreadVars *tv, int prio)
Set the thread options (thread priority).
Definition: tm-threads.c:957
const char * thread_name_single
Definition: runmodes.c:62
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&#39;t support custo...
Definition: tm-threads.c:1225
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1812
void TimeModeSetOffline(void)
Definition: util-time.c:96
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:331
int RunModeErfFileSingle(void)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
char * RunmodeAutoFpCreatePickupQueuesString(int n)
create a queue string for autofp to pass to the flow queue handler.
Definition: util-runmodes.c:59
#define SCEnter(...)
Definition: util-debug.h:337
#define SCReturnInt(x)
Definition: util-debug.h:341
#define TM_QUEUE_NAME_MAX
Definition: tm-threads.h:47
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
#define SCFree(a)
Definition: util-mem.h:322
const char * thread_name_autofp
Definition: runmodes.c:61
#define TM_THREAD_NAME_MAX
Definition: tm-threads.h:48
TmEcode TmThreadSetCPUAffinity(ThreadVars *tv, uint16_t cpu)
Set the thread options (cpu affinity).
Definition: tm-threads.c:1004
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void))
Registers a new runmode.
Definition: runmodes.c:419
TmModule * TmModuleGetByName(const char *name)
get a tm module ptr by name
Definition: tm-modules.c:51
Per thread variable structure.
Definition: threadvars.h:57
void RunModeErfFileRegister(void)
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:99
int threading_set_cpu_affinity
Definition: runmodes.h:112
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1855
const char * thread_name_workers
Definition: runmodes.c:63