suricata
runmode-af-xdp.c
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 /**
19  * \ingroup afxdppacket
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Richard McConnell <richard_mcconnell@rapid7.com>
28  *
29  * AF_XDP socket runmode
30  *
31  */
32 #define SC_PCAP_DONT_INCLUDE_PCAP_H 1
34 #include "tm-threads.h"
35 #include "conf.h"
36 #include "runmodes.h"
37 #include "runmode-af-xdp.h"
38 #include "output.h"
39 #include "detect-engine-mpm.h"
40 
41 #include "alert-fastlog.h"
42 #include "alert-debuglog.h"
43 
44 #include "flow-bypass.h"
45 
46 #include "util-conf.h"
47 #include "util-debug.h"
48 #include "util-time.h"
49 #include "util-cpu.h"
50 #include "util-affinity.h"
51 #include "util-device-private.h"
52 #include "util-runmodes.h"
53 #include "util-ioctl.h"
54 #include "util-ebpf.h"
55 #include "util-byte.h"
56 
57 #include "source-af-xdp.h"
58 
59 #ifdef HAVE_AF_XDP
60 #include <linux/if_xdp.h>
61 #include <linux/if_link.h>
62 #include <xdp/xsk.h>
63 #endif
64 
65 const char *RunModeAFXDPGetDefaultMode(void)
66 {
67  return "workers";
68 }
69 
71 {
72  RunModeRegisterNewRunMode(RUNMODE_AFXDP_DEV, "single", "Single threaded af-xdp mode",
73  RunModeIdsAFXDPSingle, NULL);
75  "Workers af-xdp mode, each thread does all"
76  " tasks from acquisition to logging",
78 }
79 
80 #ifdef HAVE_AF_XDP
81 
82 #define DEFAULT_BUSY_POLL_TIME 20
83 #define DEFAULT_BUSY_POLL_BUDGET 64
84 #define DEFAULT_GRO_FLUSH_TIMEOUT 2000000
85 #define DEFAULT_NAPI_HARD_IRQS 2
86 
87 static void AFXDPDerefConfig(void *conf)
88 {
89  AFXDPIfaceConfig *pfp = (AFXDPIfaceConfig *)conf;
90  /* Pcap config is used only once but cost of this low. */
91  if (SC_ATOMIC_SUB(pfp->ref, 1) <= 1) {
92  SCFree(pfp);
93  }
94 }
95 
96 static TmEcode ConfigSetThreads(AFXDPIfaceConfig *aconf, const char *entry_str)
97 {
98  SCEnter();
99  const char *active_runmode = RunmodeGetActive();
100 
101  if (active_runmode && !strcmp("single", active_runmode)) {
102  aconf->threads = 1;
103  SCReturnInt(0);
104  }
105 
106  if (entry_str == NULL) {
107  SCLogError("Number of threads for interface \"%s\" not specified", aconf->iface);
109  }
110 
111  const uint16_t nr_queues = (uint16_t)GetIfaceRSSQueuesNum(aconf->iface);
112 
113  if (strcmp(entry_str, "auto") == 0) {
114 
115  const uint16_t nr_cores = UtilCpuGetNumProcessorsOnline();
116 
117  /* Threads limited to MIN(cores vs queues) */
118  aconf->threads = (nr_cores <= nr_queues) ? nr_cores : nr_queues;
119  const char *sys_type = nr_cores <= nr_queues ? "cores" : "queues";
120 
121  SCLogPerf("%u %s, so using %u threads", aconf->threads, sys_type, aconf->threads);
123  }
124 
125  if (StringParseUint16(&aconf->threads, 10, 0, entry_str) < 0) {
126  SCLogError("Threads entry for interface %s contain non-numerical characters - \"%s\"",
127  aconf->iface, entry_str);
129  }
130 
131  if (aconf->threads > nr_queues) {
132  SCLogWarning(
133  "Selected threads greater than configured queues, using: %u thread(s)", nr_queues);
134  aconf->threads = nr_queues;
135  }
136 
138 }
139 
140 /**
141  * \brief extract information from config file
142  *
143  * The returned structure will be freed by the thread init function.
144  * This is thus necessary to copy the structure before giving it
145  * to thread or to reparse the file for each thread (and thus have
146  * new structure.
147  *
148  * \return a AFXDPIfaceConfig corresponding to the interface name
149  */
150 static void *ParseAFXDPConfig(const char *iface)
151 {
152  const char *confstr = NULL;
153  SCConfNode *if_root;
154  SCConfNode *if_default = NULL;
155  SCConfNode *af_xdp_node = NULL;
156  int conf_val = 0;
157  intmax_t conf_val_int = 0;
158  int boolval = 0;
159 
160  if (iface == NULL) {
161  return NULL;
162  }
163 
164  AFXDPIfaceConfig *aconf = SCCalloc(1, sizeof(*aconf));
165  if (unlikely(aconf == NULL)) {
166  return NULL;
167  }
168 
169  /* default/basic config setup */
170  strlcpy(aconf->iface, iface, sizeof(aconf->iface));
171  aconf->DerefFunc = AFXDPDerefConfig;
172  aconf->threads = 1;
173  aconf->promisc = 1;
174  aconf->enable_busy_poll = true;
175  aconf->busy_poll_time = DEFAULT_BUSY_POLL_TIME;
176  aconf->busy_poll_budget = DEFAULT_BUSY_POLL_BUDGET;
177  aconf->mode = XDP_FLAGS_UPDATE_IF_NOEXIST;
178  aconf->gro_flush_timeout = DEFAULT_GRO_FLUSH_TIMEOUT;
179  aconf->napi_defer_hard_irqs = DEFAULT_NAPI_HARD_IRQS;
180  aconf->mem_alignment = XSK_UMEM__DEFAULT_FLAGS;
181 
182  /* Find initial node */
183  af_xdp_node = SCConfGetNode("af-xdp");
184  if (af_xdp_node == NULL) {
185  SCLogInfo("unable to find af-xdp config using default values");
186  goto finalize;
187  }
188 
189  if_root = ConfFindDeviceConfig(af_xdp_node, iface);
190  if_default = ConfFindDeviceConfig(af_xdp_node, "default");
191 
192  if (if_root == NULL && if_default == NULL) {
193  SCLogInfo("unable to find af-xdp config for "
194  "interface \"%s\" or \"default\", using default values",
195  iface);
196  goto finalize;
197  }
198 
199  /* If there is no setting for current interface use default one as main iface */
200  if (if_root == NULL) {
201  if_root = if_default;
202  if_default = NULL;
203  }
204 
205  /* Threading */
206  confstr = "auto";
207  (void)SCConfGetChildValueWithDefault(if_root, if_default, "threads", &confstr);
208  if (ConfigSetThreads(aconf, confstr) != TM_ECODE_OK) {
209  aconf->DerefFunc(aconf);
210  return NULL;
211  }
212 
213  SC_ATOMIC_RESET(aconf->ref);
214  (void)SC_ATOMIC_ADD(aconf->ref, aconf->threads);
215 
216  /* Promisc Mode */
217  (void)SCConfGetChildValueBoolWithDefault(if_root, if_default, "disable-promisc", &boolval);
218  if (boolval) {
219  SCLogConfig("Disabling promiscuous mode on iface %s", aconf->iface);
220  aconf->promisc = 0;
221  }
222 
223 #ifdef HAVE_AF_XDP
224  /* AF_XDP socket mode options */
225  if (SCConfGetChildValueWithDefault(if_root, if_default, "force-xdp-mode", &confstr) == 1) {
226  if (strncasecmp(confstr, "drv", 3) == 0) {
227  aconf->mode |= XDP_FLAGS_DRV_MODE;
228  } else if (strncasecmp(confstr, "skb", 3) == 0) {
229  aconf->mode |= XDP_FLAGS_SKB_MODE;
230  } else if (strncasecmp(confstr, "none", 4) == 0) {
231  } else {
232  SCLogWarning("Incorrect af-xdp xdp-mode setting, default (none) shall be applied");
233  }
234  }
235 
236  /* copy and zerocopy binding options */
237  if (SCConfGetChildValueWithDefault(if_root, if_default, "force-bind-mode", &confstr) == 1) {
238  if (strncasecmp(confstr, "zero", 4) == 0) {
239  aconf->bind_flags |= XDP_ZEROCOPY;
240  } else if (strncasecmp(confstr, "copy", 4) == 0) {
241  aconf->bind_flags |= XDP_COPY;
242  } else if (strncasecmp(confstr, "none", 4) == 0) {
243  } else {
244  SCLogWarning("Incorrect af-xdp copy-mode setting, default (none) shall be applied");
245  }
246  }
247 
248  /* memory alignment mode selection */
249  if (SCConfGetChildValueWithDefault(if_root, if_default, "mem-unaligned", &confstr) == 1) {
250  if (strncasecmp(confstr, "yes", 3) == 0) {
251  aconf->mem_alignment = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
252  }
253  }
254 
255  /* Busy polling options */
256  if (SCConfGetChildValueBoolWithDefault(if_root, if_default, "enable-busy-poll", &conf_val) ==
257  1) {
258  if (conf_val == 0) {
259  aconf->enable_busy_poll = false;
260  }
261  }
262 
263  if (aconf->enable_busy_poll) {
265  if_root, if_default, "busy-poll-time", &conf_val_int) == 1) {
266  if (conf_val_int) {
267  aconf->busy_poll_time = (uint32_t)conf_val_int;
268  }
269  }
270 
272  if_root, if_default, "busy-poll-budget", &conf_val_int) == 1) {
273  if (conf_val_int) {
274  aconf->busy_poll_budget = (uint32_t)conf_val_int;
275  }
276  }
277 
278  /* 0 value is valid for these Linux tunable's */
280  if_root, if_default, "gro-flush-timeout", &conf_val_int) == 1) {
281  aconf->gro_flush_timeout = (uint32_t)conf_val_int;
282  }
283 
285  if_root, if_default, "napi-defer-hard-irq", &conf_val_int) == 1) {
286  aconf->napi_defer_hard_irqs = (uint32_t)conf_val_int;
287  }
288  }
289 #endif
290 
291 finalize:
292  if (LiveGetOffload() == 0) {
293  if (GetIfaceOffloading(iface, 0, 1) == 1) {
294  SCLogWarning("Using AF_XDP with offloading activated leads to capture problems");
295  }
296  } else {
298  }
299 
300  return aconf;
301 }
302 
303 static uint16_t AFXDPConfigGetThreadsCount(void *conf)
304 {
305  if (conf == NULL)
306  FatalError("Configuration file is NULL");
307 
308  AFXDPIfaceConfig *afxdp_conf = (AFXDPIfaceConfig *)conf;
309  return afxdp_conf->threads;
310 }
311 
312 #endif /* HAVE_AF_XDP */
313 
314 /**
315  * \brief Single thread version of the AF_XDP processing.
316  */
318 {
319  SCEnter();
320 
321 #ifdef HAVE_AF_XDP
322  int ret;
323  const char *live_dev = NULL;
324 
325  TimeModeSetLive();
326 
327  (void)SCConfGet("af-xdp.live-interface", &live_dev);
328 
330  FatalError("Unable to init AF_XDP queue protection.");
331  }
332 
333  ret = RunModeSetLiveCaptureSingle(ParseAFXDPConfig, AFXDPConfigGetThreadsCount, "ReceiveAFXDP",
334  "DecodeAFXDP", thread_name_single, live_dev);
335  if (ret != 0) {
336  FatalError("Unable to start runmode");
337  }
338 
339  SCLogDebug("RunModeIdsAFXDPSingle initialised");
340 
341 #endif /* HAVE_AF_XDP */
342  SCReturnInt(0);
343 }
344 
345 /**
346  * \brief Workers version of the AF_XDP processing.
347  *
348  * Start N threads with each thread doing all the work.
349  *
350  */
352 {
353  SCEnter();
354 
355 #ifdef HAVE_AF_XDP
356  int ret;
357  const char *live_dev = NULL;
358 
359  TimeModeSetLive();
360 
361  (void)SCConfGet("af-xdp.live-interface", &live_dev);
362 
364  FatalError("Unable to init AF_XDP queue protection.");
365  }
366 
367  ret = RunModeSetLiveCaptureWorkers(ParseAFXDPConfig, AFXDPConfigGetThreadsCount, "ReceiveAFXDP",
368  "DecodeAFXDP", thread_name_workers, live_dev);
369  if (ret != 0) {
370  FatalError("Unable to start runmode");
371  }
372 
373  SCLogDebug("RunModeIdsAFXDPWorkers initialised");
374 
375 #endif /* HAVE_AF_XDP */
376  SCReturnInt(0);
377 }
378 /**
379  * @}
380  */
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:68
util-device-private.h
util-byte.h
tm-threads.h
flow-bypass.h
RUNMODE_AFXDP_DEV
@ RUNMODE_AFXDP_DEV
Definition: runmodes.h:37
RunModeSetLiveCaptureWorkers
int RunModeSetLiveCaptureWorkers(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
Definition: util-runmodes.c:330
alert-debuglog.h
AFXDPIfaceConfig::mode
uint32_t mode
Definition: source-af-xdp.h:36
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
AFXDPIfaceConfig::iface
char iface[AFXDP_IFACE_NAME_LENGTH]
Definition: source-af-xdp.h:30
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
LiveGetOffload
int LiveGetOffload(void)
Definition: util-device.c:87
AFXDPIfaceConfig::mem_alignment
int mem_alignment
Definition: source-af-xdp.h:38
AFXDPIfaceConfig::gro_flush_timeout
uint32_t gro_flush_timeout
Definition: source-af-xdp.h:42
AFXDPIfaceConfig::DerefFunc
void(* DerefFunc)(void *)
Definition: source-af-xdp.h:46
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:332
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:296
SCConfGet
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:351
util-runmodes.h
RunmodeGetActive
char * RunmodeGetActive(void)
Definition: runmodes.c:199
AFXDPIfaceConfig::bind_flags
uint16_t bind_flags
Definition: source-af-xdp.h:37
RunModeAFXDPGetDefaultMode
const char * RunModeAFXDPGetDefaultMode(void)
Definition: runmode-af-xdp.c:65
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
thread_name_single
const char * thread_name_single
Definition: runmodes.c:67
AFXDPIfaceConfig::napi_defer_hard_irqs
uint32_t napi_defer_hard_irqs
Definition: source-af-xdp.h:43
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
AFXDPQueueProtectionInit
TmEcode AFXDPQueueProtectionInit(void)
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
util-debug.h
SCConfGetChildValueIntWithDefault
int SCConfGetChildValueIntWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, intmax_t *val)
Definition: conf.c:477
AFXDPIfaceConfig::enable_busy_poll
bool enable_busy_poll
Definition: source-af-xdp.h:39
util-cpu.h
LiveGetDevice
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
Definition: util-device.c:269
GetIfaceRSSQueuesNum
int GetIfaceRSSQueuesNum(const char *dev)
Definition: util-ioctl.c:713
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
util-ebpf.h
AFXDPIfaceConfig::busy_poll_budget
uint32_t busy_poll_budget
Definition: source-af-xdp.h:41
util-affinity.h
AFXDPIfaceConfig::promisc
int promisc
Definition: source-af-xdp.h:33
util-time.h
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:341
AFXDPIfaceConfig::threads
uint16_t threads
Definition: source-af-xdp.h:32
AFXDPIfaceConfig::busy_poll_time
uint32_t busy_poll_time
Definition: source-af-xdp.h:40
conf.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum SCRunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:482
TmEcode
TmEcode
Definition: tm-threads-common.h:80
GetIfaceOffloading
int GetIfaceOffloading(const char *dev, int csum, int other)
output offloading status of the link
Definition: util-ioctl.c:670
DisableIfaceOffloading
int DisableIfaceOffloading(LiveDevice *dev, int csum, int other)
Definition: util-ioctl.c:683
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:100
alert-fastlog.h
RunModeIdsAFXDPRegister
void RunModeIdsAFXDPRegister(void)
Definition: runmode-af-xdp.c:70
util-conf.h
suricata-common.h
source-af-xdp.h
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:241
FatalError
#define FatalError(...)
Definition: util-debug.h:517
RunModeIdsAFXDPWorkers
int RunModeIdsAFXDPWorkers(void)
Workers version of the AF_XDP processing.
Definition: runmode-af-xdp.c:351
SC_ATOMIC_RESET
#define SC_ATOMIC_RESET(name)
wrapper for reinitializing an atomic variable.
Definition: util-atomic.h:323
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
AFXDPIfaceConfig
Definition: source-af-xdp.h:29
runmode-af-xdp.h
SCConfGetNode
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition: conf.c:182
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
util-ioctl.h
RunModeSetLiveCaptureSingle
int RunModeSetLiveCaptureSingle(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
Definition: util-runmodes.c:354
ConfFindDeviceConfig
SCConfNode * ConfFindDeviceConfig(SCConfNode *node, const char *iface)
Find the configuration node for a specific device.
Definition: util-conf.c:126
RunModeIdsAFXDPSingle
int RunModeIdsAFXDPSingle(void)
Single thread version of the AF_XDP processing.
Definition: runmode-af-xdp.c:317
SCConfGetChildValueWithDefault
int SCConfGetChildValueWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, const char **vptr)
Definition: conf.c:394
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:108
SCConfGetChildValueBoolWithDefault
int SCConfGetChildValueBoolWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, int *val)
Definition: conf.c:529
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCConfNode_
Definition: conf.h:37
output.h