suricata
runmode-pcap-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-pcap-file.h"
23 #include "output.h"
24 
25 #include "detect-engine.h"
26 #include "source-pcap-file.h"
27 
28 #include "util-debug.h"
29 #include "util-time.h"
30 #include "util-cpu.h"
31 #include "util-affinity.h"
32 
33 #include "util-runmodes.h"
34 
36 {
37  return "autofp";
38 }
39 
41 {
42  RunModeRegisterNewRunMode(RUNMODE_PCAP_FILE, "single", "Single threaded pcap file mode",
43  RunModeFilePcapSingle, NULL);
45  "Multi-threaded pcap file mode. Packets from each flow are assigned to a consistent "
46  "detection thread",
47  RunModeFilePcapAutoFp, NULL);
48 
49  return;
50 }
51 
52 /**
53  * \brief Single thread version of the Pcap file processing.
54  */
56 {
57  const char *file = NULL;
58  char tname[TM_THREAD_NAME_MAX];
59 
60  if (ConfGet("pcap-file.file", &file) == 0) {
61  FatalError("Failed retrieving pcap-file from Conf");
62  }
63 
65 
67 
68  snprintf(tname, sizeof(tname), "%s#01", thread_name_single);
69 
70  /* create the threads */
72  "packetpool", "packetpool",
73  "packetpool", "packetpool",
74  "pktacqloop");
75  if (tv == NULL) {
76  FatalError("threading setup failed");
77  }
78 
79  TmModule *tm_module = TmModuleGetByName("ReceivePcapFile");
80  if (tm_module == NULL) {
81  FatalError("TmModuleGetByName failed for ReceivePcap");
82  }
83  TmSlotSetFuncAppend(tv, tm_module, file);
84 
85  tm_module = TmModuleGetByName("DecodePcapFile");
86  if (tm_module == NULL) {
87  FatalError("TmModuleGetByName DecodePcap failed");
88  }
89  TmSlotSetFuncAppend(tv, tm_module, NULL);
90 
91  tm_module = TmModuleGetByName("FlowWorker");
92  if (tm_module == NULL) {
93  FatalError("TmModuleGetByName for FlowWorker failed");
94  }
95  TmSlotSetFuncAppend(tv, tm_module, NULL);
96 
98 
99  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
100  FatalError("TmThreadSpawn failed");
101  }
102  return 0;
103 }
104 
105 /**
106  * \brief RunModeFilePcapAutoFp set up the following thread packet handlers:
107  * - Receive thread (from pcap file)
108  * - Decode thread
109  * - Stream thread
110  * - Detect: If we have only 1 cpu, it will setup one Detect thread
111  * If we have more than one, it will setup num_cpus - 1
112  * starting from the second cpu available.
113  * - Outputs thread
114  * By default the threads will use the first cpu available
115  * except the Detection threads if we have more than one cpu.
116  *
117  * \retval 0 If all goes well. (If any problem is detected the engine will
118  * exit()).
119  */
121 {
122  SCEnter();
123  char tname[TM_THREAD_NAME_MAX];
124  char qname[TM_QUEUE_NAME_MAX];
125  uint16_t cpu = 0;
126  char *queues = NULL;
127  uint16_t thread;
128 
129  const char *file = NULL;
130  if (ConfGet("pcap-file.file", &file) == 0) {
131  FatalError("Failed retrieving pcap-file from Conf");
132  }
133  SCLogDebug("file %s", file);
134 
136 
138 
139  /* Available cpus */
140  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
141 
142  /* start with cpu 1 so that if we're creating an odd number of detect
143  * threads we're not creating the most on CPU0. */
144  if (ncpus > 0)
145  cpu = 1;
146 
147  /* always create at least one thread */
148  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
149  if (thread_max == 0)
150  thread_max = ncpus * threading_detect_ratio;
151  if (thread_max < 1)
152  thread_max = 1;
153  if (thread_max > 1024)
154  thread_max = 1024;
155 
156  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
157  if (queues == NULL) {
158  FatalError("RunmodeAutoFpCreatePickupQueuesString failed");
159  }
160 
161  snprintf(tname, sizeof(tname), "%s#01", thread_name_autofp);
162 
163  /* create the threads */
164  ThreadVars *tv_receivepcap =
166  "packetpool", "packetpool",
167  queues, "flow",
168  "pktacqloop");
169  SCFree(queues);
170 
171  if (tv_receivepcap == NULL) {
172  FatalError("threading setup failed");
173  }
174  TmModule *tm_module = TmModuleGetByName("ReceivePcapFile");
175  if (tm_module == NULL) {
176  FatalError("TmModuleGetByName failed for ReceivePcap");
177  }
178  TmSlotSetFuncAppend(tv_receivepcap, tm_module, file);
179 
180  tm_module = TmModuleGetByName("DecodePcapFile");
181  if (tm_module == NULL) {
182  FatalError("TmModuleGetByName DecodePcap failed");
183  }
184  TmSlotSetFuncAppend(tv_receivepcap, tm_module, NULL);
185 
186  TmThreadSetCPU(tv_receivepcap, RECEIVE_CPU_SET);
187 
188  if (TmThreadSpawn(tv_receivepcap) != TM_ECODE_OK) {
189  FatalError("TmThreadSpawn failed");
190  }
191 
192  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
193  snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread + 1);
194  snprintf(qname, sizeof(qname), "pickup%d", thread + 1);
195 
196  SCLogDebug("tname %s, qname %s", tname, qname);
197  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
198 
199  ThreadVars *tv_detect_ncpu =
201  qname, "flow",
202  "packetpool", "packetpool",
203  "varslot");
204  if (tv_detect_ncpu == NULL) {
205  FatalError("TmThreadsCreate failed");
206  }
207 
208  tm_module = TmModuleGetByName("FlowWorker");
209  if (tm_module == NULL) {
210  FatalError("TmModuleGetByName for FlowWorker failed");
211  }
212  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
213 
214  TmThreadSetGroupName(tv_detect_ncpu, "Detect");
215 
216  TmThreadSetCPU(tv_detect_ncpu, WORKER_CPU_SET);
217 
218  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
219  FatalError("TmThreadSpawn failed");
220  }
221 
222  if ((cpu + 1) == ncpus)
223  cpu = 0;
224  else
225  cpu++;
226  }
227 
228  return 0;
229 }
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:81
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
source-pcap-file.h
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
RunModeFilePcapGetDefaultMode
const char * RunModeFilePcapGetDefaultMode(void)
Definition: runmode-pcap-file.c:35
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
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
runmode-pcap-file.h
runmodes.h
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
TmThreadSetCPU
TmEcode TmThreadSetCPU(ThreadVars *tv, uint8_t type)
Definition: tm-threads.c:816
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:832
RunModeFilePcapSingle
int RunModeFilePcapSingle(void)
Single thread version of the Pcap file processing.
Definition: runmode-pcap-file.c:55
RunModeFilePcapRegister
void RunModeFilePcapRegister(void)
Definition: runmode-pcap-file.c:40
FatalError
#define FatalError(...)
Definition: util-debug.h:502
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
SCFree
#define SCFree(p)
Definition: util-mem.h:61
RECEIVE_CPU_SET
@ RECEIVE_CPU_SET
Definition: util-affinity.h:52
PcapFileGlobalInit
void PcapFileGlobalInit(void)
Definition: source-pcap-file.c:139
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:108
RUNMODE_PCAP_FILE
@ RUNMODE_PCAP_FILE
Definition: runmodes.h:30
threading_detect_ratio
float threading_detect_ratio
Definition: runmodes.c:978
output.h
RunModeFilePcapAutoFp
int RunModeFilePcapAutoFp(void)
RunModeFilePcapAutoFp set up the following thread packet handlers:
Definition: runmode-pcap-file.c:120
WORKER_CPU_SET
@ WORKER_CPU_SET
Definition: util-affinity.h:53