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