1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/* Copyright (C) 2022 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \ingroup afxdppacket
*
* @{
*/
/**
* \file
*
* \author Richard McConnell <richard_mcconnell@rapid7.com>
*
* AF_XDP socket runmode
*
*/
#define PCAP_DONT_INCLUDE_PCAP_BPF_H 1
#define SC_PCAP_DONT_INCLUDE_PCAP_H 1
#include "suricata-common.h"
#include "tm-threads.h"
#include "conf.h"
#include "runmodes.h"
#include "runmode-af-xdp.h"
#include "output.h"
#include "log-httplog.h"
#include "detect-engine-mpm.h"
#include "alert-fastlog.h"
#include "alert-debuglog.h"
#include "flow-bypass.h"
#include "util-conf.h"
#include "util-debug.h"
#include "util-time.h"
#include "util-cpu.h"
#include "util-affinity.h"
#include "util-device.h"
#include "util-runmodes.h"
#include "util-ioctl.h"
#include "util-ebpf.h"
#include "util-byte.h"
#include "source-af-xdp.h"
#ifdef HAVE_AF_XDP
#include <linux/if_xdp.h>
#include <linux/if_link.h>
#include <xdp/xsk.h>
#endif
const char *RunModeAFXDPGetDefaultMode(void)
{
return "workers";
}
void RunModeIdsAFXDPRegister(void)
{
RunModeRegisterNewRunMode(RUNMODE_AFXDP_DEV, "single", "Single threaded af-xdp mode",
RunModeIdsAFXDPSingle, NULL);
RunModeRegisterNewRunMode(RUNMODE_AFXDP_DEV, "workers",
"Workers af-xdp mode, each thread does all"
" tasks from acquisition to logging",
RunModeIdsAFXDPWorkers, NULL);
return;
}
#ifdef HAVE_AF_XDP
#define DEFAULT_BUSY_POLL_TIME 20
#define DEFAULT_BUSY_POLL_BUDGET 64
#define DEFAULT_GRO_FLUSH_TIMEOUT 2000000
#define DEFAULT_NAPI_HARD_IRQS 2
static void AFXDPDerefConfig(void *conf)
{
AFXDPIfaceConfig *pfp = (AFXDPIfaceConfig *)conf;
/* Pcap config is used only once but cost of this low. */
if (SC_ATOMIC_SUB(pfp->ref, 1) <= 1) {
SCFree(pfp);
}
}
static TmEcode ConfigSetThreads(AFXDPIfaceConfig *aconf, const char *entry_str)
{
SCEnter();
const char *active_runmode = RunmodeGetActive();
if (active_runmode && !strcmp("single", active_runmode)) {
aconf->threads = 1;
SCReturnInt(0);
}
if (entry_str == NULL) {
SCLogError("Number of threads for interface \"%s\" not specified", aconf->iface);
SCReturnInt(TM_ECODE_FAILED);
}
const int nr_queues = GetIfaceRSSQueuesNum(aconf->iface);
if (strcmp(entry_str, "auto") == 0) {
const int nr_cores = (int)UtilCpuGetNumProcessorsOnline();
/* Threads limited to MIN(cores vs queues) */
aconf->threads = (nr_cores <= nr_queues) ? nr_cores : nr_queues;
const char *sys_type = nr_cores <= nr_queues ? "cores" : "queues";
SCLogPerf("%u %s, so using %u threads", aconf->threads, sys_type, aconf->threads);
SCReturnInt(TM_ECODE_OK);
}
if (StringParseInt32(&aconf->threads, 10, 0, entry_str) < 0) {
SCLogError("Threads entry for interface %s contain non-numerical characters - \"%s\"",
aconf->iface, entry_str);
SCReturnInt(TM_ECODE_FAILED);
}
if (aconf->threads < 0) {
SCLogError("Interface %s has a negative number of threads", aconf->iface);
SCReturnInt(TM_ECODE_FAILED);
}
if (aconf->threads > nr_queues) {
SCLogWarning(
"Selected threads greater than configured queues, using: %d thread(s)", nr_queues);
aconf->threads = nr_queues;
}
SCReturnInt(TM_ECODE_OK);
}
/**
* \brief extract information from config file
*
* The returned structure will be freed by the thread init function.
* This is thus necessary to copy the structure before giving it
* to thread or to reparse the file for each thread (and thus have
* new structure.
*
* \return a AFXDPIfaceConfig corresponding to the interface name
*/
static void *ParseAFXDPConfig(const char *iface)
{
const char *confstr = NULL;
ConfNode *if_root;
ConfNode *if_default = NULL;
ConfNode *af_xdp_node = NULL;
int conf_val = 0;
intmax_t conf_val_int = 0;
bool boolval = false;
if (iface == NULL) {
return NULL;
}
AFXDPIfaceConfig *aconf = SCCalloc(1, sizeof(*aconf));
if (unlikely(aconf == NULL)) {
return NULL;
}
/* default/basic config setup */
strlcpy(aconf->iface, iface, sizeof(aconf->iface));
aconf->DerefFunc = AFXDPDerefConfig;
aconf->threads = 1;
aconf->promisc = 1;
aconf->enable_busy_poll = true;
aconf->busy_poll_time = DEFAULT_BUSY_POLL_TIME;
aconf->busy_poll_budget = DEFAULT_BUSY_POLL_BUDGET;
aconf->mode = XDP_FLAGS_UPDATE_IF_NOEXIST;
aconf->gro_flush_timeout = DEFAULT_GRO_FLUSH_TIMEOUT;
aconf->napi_defer_hard_irqs = DEFAULT_NAPI_HARD_IRQS;
aconf->mem_alignment = XSK_UMEM__DEFAULT_FLAGS;
/* Find initial node */
af_xdp_node = ConfGetNode("af-xdp");
if (af_xdp_node == NULL) {
SCLogInfo("unable to find af-xdp config using default values");
goto finalize;
}
if_root = ConfFindDeviceConfig(af_xdp_node, iface);
if_default = ConfFindDeviceConfig(af_xdp_node, "default");
if (if_root == NULL && if_default == NULL) {
SCLogInfo("unable to find af-xdp config for "
"interface \"%s\" or \"default\", using default values",
iface);
goto finalize;
}
/* If there is no setting for current interface use default one as main iface */
if (if_root == NULL) {
if_root = if_default;
if_default = NULL;
}
/* Threading */
confstr = "auto";
(void)ConfGetChildValueWithDefault(if_root, if_default, "threads", &confstr);
if (ConfigSetThreads(aconf, confstr) != TM_ECODE_OK) {
aconf->DerefFunc(aconf);
return NULL;
}
SC_ATOMIC_RESET(aconf->ref);
(void)SC_ATOMIC_ADD(aconf->ref, aconf->threads);
/* Promisc Mode */
(void)ConfGetChildValueBoolWithDefault(if_root, if_default, "disable-promisc", (int *)&boolval);
if (boolval) {
SCLogConfig("Disabling promiscuous mode on iface %s", aconf->iface);
aconf->promisc = 0;
}
#ifdef HAVE_AF_XDP
/* AF_XDP socket mode options */
if (ConfGetChildValueWithDefault(if_root, if_default, "force-xdp-mode", &confstr) == 1) {
if (strncasecmp(confstr, "drv", 3) == 0) {
aconf->mode |= XDP_FLAGS_DRV_MODE;
} else if (strncasecmp(confstr, "skb", 3) == 0) {
aconf->mode |= XDP_FLAGS_SKB_MODE;
} else if (strncasecmp(confstr, "none", 4) == 0) {
} else {
SCLogWarning("Incorrect af-xdp xdp-mode setting, default (none) shall be applied");
}
}
/* copy and zerocopy binding options */
if (ConfGetChildValueWithDefault(if_root, if_default, "force-bind-mode", &confstr) == 1) {
if (strncasecmp(confstr, "zero", 4) == 0) {
aconf->bind_flags |= XDP_ZEROCOPY;
} else if (strncasecmp(confstr, "copy", 4) == 0) {
aconf->bind_flags |= XDP_COPY;
} else if (strncasecmp(confstr, "none", 4) == 0) {
} else {
SCLogWarning("Incorrect af-xdp copy-mode setting, default (none) shall be applied");
}
}
/* memory alignment mode selection */
if (ConfGetChildValueWithDefault(if_root, if_default, "mem-unaligned", &confstr) == 1) {
if (strncasecmp(confstr, "yes", 3) == 0) {
aconf->mem_alignment = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
}
}
/* Busy polling options */
if (ConfGetChildValueBoolWithDefault(if_root, if_default, "enable-busy-poll", &conf_val) == 1) {
if (conf_val == 0) {
aconf->enable_busy_poll = false;
}
}
if (aconf->enable_busy_poll) {
if (ConfGetChildValueIntWithDefault(if_root, if_default, "busy-poll-time", &conf_val_int) ==
1) {
if (conf_val_int) {
aconf->busy_poll_time = conf_val_int;
}
}
if (ConfGetChildValueIntWithDefault(
if_root, if_default, "busy-poll-budget", &conf_val_int) == 1) {
if (conf_val_int) {
aconf->busy_poll_budget = conf_val_int;
}
}
/* 0 value is valid for these Linux tunable's */
if (ConfGetChildValueIntWithDefault(
if_root, if_default, "gro-flush-timeout", &conf_val_int) == 1) {
aconf->gro_flush_timeout = conf_val_int;
}
if (ConfGetChildValueIntWithDefault(
if_root, if_default, "napi-defer-hard-irq", &conf_val_int) == 1) {
aconf->napi_defer_hard_irqs = conf_val_int;
}
}
#endif
finalize:
if (LiveGetOffload() == 0) {
if (GetIfaceOffloading(iface, 0, 1) == 1) {
SCLogWarning("Using AF_XDP with offloading activated leads to capture problems");
}
} else {
DisableIfaceOffloading(LiveGetDevice(iface), 0, 1);
}
return aconf;
}
static int AFXDPConfigGetThreadsCount(void *conf)
{
if (conf == NULL)
FatalError("Configuration file is NULL");
AFXDPIfaceConfig *afxdp_conf = (AFXDPIfaceConfig *)conf;
return afxdp_conf->threads;
}
#endif /* HAVE_AF_XDP */
/**
* \brief Single thread version of the AF_XDP processing.
*/
int RunModeIdsAFXDPSingle(void)
{
SCEnter();
#ifdef HAVE_AF_XDP
int ret;
const char *live_dev = NULL;
TimeModeSetLive();
(void)ConfGet("af-xdp.live-interface", &live_dev);
if (AFXDPQueueProtectionInit() != TM_ECODE_OK) {
FatalError("Unable to init AF_XDP queue protection.");
}
ret = RunModeSetLiveCaptureSingle(ParseAFXDPConfig, AFXDPConfigGetThreadsCount, "ReceiveAFXDP",
"DecodeAFXDP", thread_name_single, live_dev);
if (ret != 0) {
FatalError("Unable to start runmode");
}
SCLogDebug("RunModeIdsAFXDPSingle initialised");
#endif /* HAVE_AF_XDP */
SCReturnInt(0);
}
/**
* \brief Workers version of the AF_XDP processing.
*
* Start N threads with each thread doing all the work.
*
*/
int RunModeIdsAFXDPWorkers(void)
{
SCEnter();
#ifdef HAVE_AF_XDP
int ret;
const char *live_dev = NULL;
TimeModeSetLive();
(void)ConfGet("af-xdp.live-interface", &live_dev);
if (AFXDPQueueProtectionInit() != TM_ECODE_OK) {
FatalError("Unable to init AF_XDP queue protection.");
}
ret = RunModeSetLiveCaptureWorkers(ParseAFXDPConfig, AFXDPConfigGetThreadsCount, "ReceiveAFXDP",
"DecodeAFXDP", thread_name_workers, live_dev);
if (ret != 0) {
FatalError("Unable to start runmode");
}
SCLogDebug("RunModeIdsAFXDPWorkers initialised");
#endif /* HAVE_AF_XDP */
SCReturnInt(0);
}
/**
* @}
*/
|