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 "
46  "each flow are assigned to a single detect thread, "
47  "unlike \"pcap-file-auto\" where packets from "
48  "the same flow can be processed by any detect "
49  "thread",
50  RunModeFilePcapAutoFp, NULL);
51 
52  return;
53 }
54 
55 /**
56  * \brief Single thread version of the Pcap file processing.
57  */
59 {
60  const char *file = NULL;
61  char tname[TM_THREAD_NAME_MAX];
62 
63  if (ConfGet("pcap-file.file", &file) == 0) {
64  FatalError("Failed retrieving pcap-file from Conf");
65  }
66 
69 
71 
72  snprintf(tname, sizeof(tname), "%s#01", thread_name_single);
73 
74  /* create the threads */
76  "packetpool", "packetpool",
77  "packetpool", "packetpool",
78  "pktacqloop");
79  if (tv == NULL) {
80  FatalError("threading setup failed");
81  }
82 
83  TmModule *tm_module = TmModuleGetByName("ReceivePcapFile");
84  if (tm_module == NULL) {
85  FatalError("TmModuleGetByName failed for ReceivePcap");
86  }
87  TmSlotSetFuncAppend(tv, tm_module, file);
88 
89  tm_module = TmModuleGetByName("DecodePcapFile");
90  if (tm_module == NULL) {
91  FatalError("TmModuleGetByName DecodePcap failed");
92  }
93  TmSlotSetFuncAppend(tv, tm_module, NULL);
94 
95  tm_module = TmModuleGetByName("FlowWorker");
96  if (tm_module == NULL) {
97  FatalError("TmModuleGetByName for FlowWorker failed");
98  }
99  TmSlotSetFuncAppend(tv, tm_module, NULL);
100 
102 
103  if (TmThreadSpawn(tv) != TM_ECODE_OK) {
104  FatalError("TmThreadSpawn failed");
105  }
106  return 0;
107 }
108 
109 /**
110  * \brief RunModeFilePcapAutoFp set up the following thread packet handlers:
111  * - Receive thread (from pcap file)
112  * - Decode thread
113  * - Stream thread
114  * - Detect: If we have only 1 cpu, it will setup one Detect thread
115  * If we have more than one, it will setup num_cpus - 1
116  * starting from the second cpu available.
117  * - Outputs thread
118  * By default the threads will use the first cpu available
119  * except the Detection threads if we have more than one cpu.
120  *
121  * \retval 0 If all goes well. (If any problem is detected the engine will
122  * exit()).
123  */
125 {
126  SCEnter();
127  char tname[TM_THREAD_NAME_MAX];
128  char qname[TM_QUEUE_NAME_MAX];
129  uint16_t cpu = 0;
130  char *queues = NULL;
131  uint16_t thread;
132 
134 
135  const char *file = NULL;
136  if (ConfGet("pcap-file.file", &file) == 0) {
137  FatalError("Failed retrieving pcap-file from Conf");
138  }
139  SCLogDebug("file %s", file);
140 
142 
144 
145  /* Available cpus */
146  uint16_t ncpus = UtilCpuGetNumProcessorsOnline();
147 
148  /* start with cpu 1 so that if we're creating an odd number of detect
149  * threads we're not creating the most on CPU0. */
150  if (ncpus > 0)
151  cpu = 1;
152 
153  /* always create at least one thread */
154  int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET);
155  if (thread_max == 0)
156  thread_max = ncpus * threading_detect_ratio;
157  if (thread_max < 1)
158  thread_max = 1;
159  if (thread_max > 1024)
160  thread_max = 1024;
161 
162  queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
163  if (queues == NULL) {
164  FatalError("RunmodeAutoFpCreatePickupQueuesString failed");
165  }
166 
167  snprintf(tname, sizeof(tname), "%s#01", thread_name_autofp);
168 
169  /* create the threads */
170  ThreadVars *tv_receivepcap =
172  "packetpool", "packetpool",
173  queues, "flow",
174  "pktacqloop");
175  SCFree(queues);
176 
177  if (tv_receivepcap == NULL) {
178  FatalError("threading setup failed");
179  }
180  TmModule *tm_module = TmModuleGetByName("ReceivePcapFile");
181  if (tm_module == NULL) {
182  FatalError("TmModuleGetByName failed for ReceivePcap");
183  }
184  TmSlotSetFuncAppend(tv_receivepcap, tm_module, file);
185 
186  tm_module = TmModuleGetByName("DecodePcapFile");
187  if (tm_module == NULL) {
188  FatalError("TmModuleGetByName DecodePcap failed");
189  }
190  TmSlotSetFuncAppend(tv_receivepcap, tm_module, NULL);
191 
192  TmThreadSetCPU(tv_receivepcap, RECEIVE_CPU_SET);
193 
194  if (TmThreadSpawn(tv_receivepcap) != TM_ECODE_OK) {
195  FatalError("TmThreadSpawn failed");
196  }
197 
198  for (thread = 0; thread < (uint16_t)thread_max; thread++) {
199  snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread + 1);
200  snprintf(qname, sizeof(qname), "pickup%d", thread + 1);
201 
202  SCLogDebug("tname %s, qname %s", tname, qname);
203  SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu);
204 
205  ThreadVars *tv_detect_ncpu =
207  qname, "flow",
208  "packetpool", "packetpool",
209  "varslot");
210  if (tv_detect_ncpu == NULL) {
211  FatalError("TmThreadsCreate failed");
212  }
213 
214  tm_module = TmModuleGetByName("FlowWorker");
215  if (tm_module == NULL) {
216  FatalError("TmModuleGetByName for FlowWorker failed");
217  }
218  TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
219 
220  TmThreadSetGroupName(tv_detect_ncpu, "Detect");
221 
222  TmThreadSetCPU(tv_detect_ncpu, WORKER_CPU_SET);
223 
224  if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
225  FatalError("TmThreadSpawn failed");
226  }
227 
228  if ((cpu + 1) == ncpus)
229  cpu = 0;
230  else
231  cpu++;
232  }
233 
234  return 0;
235 }
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:1646
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:1032
TmThreadSetGroupName
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
Definition: tm-threads.c:1603
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
RunModeInitialize
void RunModeInitialize(void)
Definition: runmodes.c:985
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
WORKER_CPU_SET
@ WORKER_CPU_SET
Definition: util-affinity.h:53
conf.h
runmode-pcap-file.h
runmodes.h
TmModule_
Definition: tm-modules.h:43
TmSlotSetFuncAppend
void TmSlotSetFuncAppend(ThreadVars *tv, TmModule *tm, const void *data)
Appends a new entry to the slots.
Definition: tm-threads.c:637
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
TmThreadSetCPU
TmEcode TmThreadSetCPU(ThreadVars *tv, uint8_t type)
Definition: tm-threads.c:811
TmThreadGetNbThreads
int TmThreadGetNbThreads(uint8_t type)
Definition: tm-threads.c:827
RunModeFilePcapSingle
int RunModeFilePcapSingle(void)
Single thread version of the Pcap file processing.
Definition: runmode-pcap-file.c:58
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:138
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:980
output.h
RunModeFilePcapAutoFp
int RunModeFilePcapAutoFp(void)
RunModeFilePcapAutoFp set up the following thread packet handlers:
Definition: runmode-pcap-file.c:124