suricata
runmode-af-packet.c
Go to the documentation of this file.
1 /* Copyright (C) 2011-2020 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 afppacket
20  *
21  * @{
22  */
23 
24 /**
25  * \file
26  *
27  * \author Eric Leblond <eric@regit.org>
28  *
29  * AF_PACKET socket runmode
30  *
31  */
32 
33 #include "suricata-common.h"
34 #include "suricata.h"
35 #include "tm-threads.h"
36 #include "conf.h"
37 #include "runmodes.h"
38 #include "runmode-af-packet.h"
39 #include "output.h"
40 #include "log-httplog.h"
41 #include "detect-engine-mpm.h"
42 
43 #include "alert-fastlog.h"
44 #include "alert-debuglog.h"
45 
46 #include "flow-bypass.h"
47 
48 #include "util-conf.h"
49 #include "util-debug.h"
50 #include "util-time.h"
51 #include "util-cpu.h"
52 #include "util-affinity.h"
53 #include "util-device.h"
54 #include "util-runmodes.h"
55 #include "util-ioctl.h"
56 #include "util-ebpf.h"
57 #include "util-byte.h"
58 
59 #include "source-af-packet.h"
60 #include "util-bpf.h"
61 
62 extern uint16_t max_pending_packets;
63 
64 const char *RunModeAFPGetDefaultMode(void)
65 {
66  return "workers";
67 }
68 
69 static int AFPRunModeIsIPS(void)
70 {
71  int nlive = LiveGetDeviceCount();
72  int ldev;
73  ConfNode *if_root;
74  ConfNode *if_default = NULL;
75  ConfNode *af_packet_node;
76  int has_ips = 0;
77  int has_ids = 0;
78 
79  /* Find initial node */
80  af_packet_node = ConfGetNode("af-packet");
81  if (af_packet_node == NULL) {
82  return 0;
83  }
84 
85  if_default = ConfNodeLookupKeyValue(af_packet_node, "interface", "default");
86 
87  for (ldev = 0; ldev < nlive; ldev++) {
88  const char *live_dev = LiveGetDeviceName(ldev);
89  if (live_dev == NULL) {
90  SCLogError("Problem with config file");
91  return 0;
92  }
93  const char *copymodestr = NULL;
94  if_root = ConfFindDeviceConfig(af_packet_node, live_dev);
95 
96  if (if_root == NULL) {
97  if (if_default == NULL) {
98  SCLogError("Problem with config file");
99  return 0;
100  }
101  if_root = if_default;
102  }
103 
104  if (ConfGetChildValueWithDefault(if_root, if_default, "copy-mode", &copymodestr) == 1) {
105  if (strcmp(copymodestr, "ips") == 0) {
106  has_ips = 1;
107  } else {
108  has_ids = 1;
109  }
110  } else {
111  has_ids = 1;
112  }
113  }
114 
115  if (has_ids && has_ips) {
116  SCLogWarning("AF_PACKET using both IPS and TAP/IDS mode, this will not "
117  "be allowed in Suricata 8 due to undefined behavior. See ticket #5588.");
118  for (ldev = 0; ldev < nlive; ldev++) {
119  const char *live_dev = LiveGetDeviceName(ldev);
120  if (live_dev == NULL) {
121  SCLogError("Problem with config file");
122  return 0;
123  }
124  if_root = ConfNodeLookupKeyValue(af_packet_node, "interface", live_dev);
125  const char *copymodestr = NULL;
126 
127  if (if_root == NULL) {
128  if (if_default == NULL) {
129  SCLogError("Problem with config file");
130  return 0;
131  }
132  if_root = if_default;
133  }
134 
135  if (!((ConfGetChildValueWithDefault(if_root, if_default, "copy-mode", &copymodestr) ==
136  1) &&
137  (strcmp(copymodestr, "ips") == 0))) {
138  SCLogError("AF_PACKET IPS mode used and interface '%s' is in IDS or TAP mode. "
139  "Sniffing '%s' but expect bad result as stream-inline is activated.",
140  live_dev, live_dev);
141  }
142  }
143  }
144 
145  return has_ips;
146 }
147 
148 static void AFPRunModeEnableIPS(void)
149 {
150  if (AFPRunModeIsIPS()) {
151  SCLogInfo("Setting IPS mode");
153  }
154 }
155 
157 {
158  RunModeRegisterNewRunMode(RUNMODE_AFP_DEV, "single", "Single threaded af-packet mode",
159  RunModeIdsAFPSingle, AFPRunModeEnableIPS);
161  "Workers af-packet mode, each thread does all"
162  " tasks from acquisition to logging",
163  RunModeIdsAFPWorkers, AFPRunModeEnableIPS);
165  "Multi socket AF_PACKET mode. Packets from "
166  "each flow are assigned to a single detect "
167  "thread.",
168  RunModeIdsAFPAutoFp, AFPRunModeEnableIPS);
169  return;
170 }
171 
172 
173 #ifdef HAVE_AF_PACKET
174 
175 static void AFPDerefConfig(void *conf)
176 {
177  AFPIfaceConfig *pfp = (AFPIfaceConfig *)conf;
178  /* Pcap config is used only once but cost of this low. */
179  if (SC_ATOMIC_SUB(pfp->ref, 1) == 1) {
180  SCFree(pfp);
181  }
182 }
183 
184 /* if cluster id is not set, assign it automagically, uniq value per
185  * interface. */
186 static int cluster_id_auto = 1;
187 
188 /**
189  * \brief extract information from config file
190  *
191  * The returned structure will be freed by the thread init function.
192  * This is thus necessary to or copy the structure before giving it
193  * to thread or to reparse the file for each thread (and thus have
194  * new structure.
195  *
196  * \return a AFPIfaceConfig corresponding to the interface name
197  */
198 static void *ParseAFPConfig(const char *iface)
199 {
200  const char *threadsstr = NULL;
201  ConfNode *if_root;
202  ConfNode *if_default = NULL;
203  ConfNode *af_packet_node;
204  const char *tmpclusterid;
205  const char *tmpctype;
206  const char *copymodestr;
207  intmax_t value;
208  int boolval;
209  const char *out_iface = NULL;
210  int cluster_type = PACKET_FANOUT_HASH;
211  const char *ebpf_file = NULL;
212  const char *active_runmode = RunmodeGetActive();
213 
214  if (iface == NULL) {
215  return NULL;
216  }
217 
218  AFPIfaceConfig *aconf = SCCalloc(1, sizeof(*aconf));
219  if (unlikely(aconf == NULL)) {
220  return NULL;
221  }
222 
223  strlcpy(aconf->iface, iface, sizeof(aconf->iface));
224  aconf->threads = 0;
225  SC_ATOMIC_INIT(aconf->ref);
226  (void) SC_ATOMIC_ADD(aconf->ref, 1);
227  aconf->buffer_size = 0;
228  aconf->cluster_id = 1;
229  aconf->cluster_type = cluster_type | PACKET_FANOUT_FLAG_DEFRAG;
230  aconf->promisc = 1;
232  aconf->DerefFunc = AFPDerefConfig;
233  aconf->flags = 0;
234  aconf->bpf_filter = NULL;
235  aconf->ebpf_lb_file = NULL;
236  aconf->ebpf_lb_fd = -1;
237  aconf->ebpf_filter_file = NULL;
238  aconf->ebpf_filter_fd = -1;
239  aconf->out_iface = NULL;
240  aconf->copy_mode = AFP_COPY_MODE_NONE;
241  aconf->block_timeout = 10;
242  aconf->block_size = getpagesize() << AFP_BLOCK_SIZE_DEFAULT_ORDER;
243 #ifdef HAVE_PACKET_EBPF
244  aconf->ebpf_t_config.cpus_count = UtilCpuGetNumProcessorsConfigured();
245 #endif
246 
247  /* Find initial node */
248  af_packet_node = ConfGetNode("af-packet");
249  if (af_packet_node == NULL) {
250  SCLogInfo("%s: unable to find af-packet config using default values", iface);
251  goto finalize;
252  }
253 
254  if_root = ConfFindDeviceConfig(af_packet_node, iface);
255  if_default = ConfFindDeviceConfig(af_packet_node, "default");
256 
257  if (if_root == NULL && if_default == NULL) {
258  SCLogInfo("%s: unable to find af-packet config for "
259  "interface \"%s\" or \"default\", using default values",
260  iface, iface);
261  goto finalize;
262  }
263 
264  /* If there is no setting for current interface use default one as main iface */
265  if (if_root == NULL) {
266  if_root = if_default;
267  if_default = NULL;
268  }
269 
270  if (active_runmode && !strcmp("single", active_runmode)) {
271  aconf->threads = 1;
272  } else if (ConfGetChildValueWithDefault(if_root, if_default, "threads", &threadsstr) != 1) {
273  aconf->threads = 0;
274  } else {
275  if (threadsstr != NULL) {
276  if (strcmp(threadsstr, "auto") == 0) {
277  aconf->threads = 0;
278  } else {
279  if (StringParseInt32(&aconf->threads, 10, 0, (const char *)threadsstr) < 0) {
280  SCLogWarning("%s: invalid number of "
281  "threads, resetting to default",
282  iface);
283  aconf->threads = 0;
284  }
285  }
286  }
287  }
288 
289  if (ConfGetChildValueWithDefault(if_root, if_default, "copy-iface", &out_iface) == 1) {
290  if (out_iface != NULL) {
291  if (strlen(out_iface) > 0) {
292  aconf->out_iface = out_iface;
293  if (strcmp(iface, out_iface) == 0) {
294  FatalError(
295  "Invalid config: interface (%s) and copy-iface (%s) can't be the same",
296  iface, out_iface);
297  }
298  }
299  } else {
300  SCLogWarning("copy-iface corresponding to %s interface cannot be NULL", iface);
301  }
302  }
303 
304  if (ConfGetChildValueBoolWithDefault(if_root, if_default, "use-mmap", (int *)&boolval) == 1) {
305  if (!boolval) {
306  SCLogWarning(
307  "%s: \"use-mmap\" option is obsolete: mmap is always enabled", aconf->iface);
308  }
309  }
310 
311  (void)ConfGetChildValueBoolWithDefault(if_root, if_default, "mmap-locked", (int *)&boolval);
312  if (boolval) {
313  SCLogConfig("%s: enabling locked memory for mmap", aconf->iface);
314  aconf->flags |= AFP_MMAP_LOCKED;
315  }
316 
317  if (ConfGetChildValueBoolWithDefault(if_root, if_default, "tpacket-v3", (int *)&boolval) == 1) {
318  if (boolval) {
319  if (strcasecmp(RunmodeGetActive(), "workers") == 0) {
320 #ifdef HAVE_TPACKET_V3
321  SCLogConfig("%s: enabling tpacket v3", aconf->iface);
322  aconf->flags |= AFP_TPACKET_V3;
323 #else
324  SCLogWarning("%s: system too old for tpacket v3 switching to v2", iface);
325  aconf->flags &= ~AFP_TPACKET_V3;
326 #endif
327  } else {
328  SCLogWarning("%s: tpacket v3 is only implemented for 'workers' runmode."
329  " Switching to tpacket v2.",
330  iface);
331  aconf->flags &= ~AFP_TPACKET_V3;
332  }
333  } else {
334  aconf->flags &= ~AFP_TPACKET_V3;
335  }
336  }
337 
339  if_root, if_default, "use-emergency-flush", (int *)&boolval);
340  if (boolval) {
341  SCLogConfig("%s: using emergency ring flush", aconf->iface);
342  aconf->flags |= AFP_EMERGENCY_MODE;
343  }
344 
345  if (ConfGetChildValueWithDefault(if_root, if_default, "copy-mode", &copymodestr) == 1) {
346  if (aconf->out_iface == NULL) {
347  SCLogWarning("%s: copy mode activated but no destination"
348  " iface. Disabling feature",
349  iface);
350  } else if (strlen(copymodestr) <= 0) {
351  aconf->out_iface = NULL;
352  } else if (strcmp(copymodestr, "ips") == 0) {
353  SCLogInfo("%s: AF_PACKET IPS mode activated %s->%s", iface, iface, aconf->out_iface);
354  aconf->copy_mode = AFP_COPY_MODE_IPS;
355  if (aconf->flags & AFP_TPACKET_V3) {
356  SCLogWarning("%s: using tpacket_v3 in IPS mode will result in high latency", iface);
357  }
358  } else if (strcmp(copymodestr, "tap") == 0) {
359  SCLogInfo("%s: AF_PACKET TAP mode activated %s->%s", iface, iface, aconf->out_iface);
360  aconf->copy_mode = AFP_COPY_MODE_TAP;
361  if (aconf->flags & AFP_TPACKET_V3) {
362  SCLogWarning("%s: using tpacket_v3 in TAP mode will result in high latency", iface);
363  }
364  } else {
365  SCLogWarning("Invalid 'copy-mode' (not in tap, ips)");
366  }
367  }
368 
369  if (ConfGetChildValueWithDefault(if_root, if_default, "cluster-id", &tmpclusterid) != 1) {
370  aconf->cluster_id = (uint16_t)(cluster_id_auto++);
371  } else {
372  if (StringParseUint16(&aconf->cluster_id, 10, 0, (const char *)tmpclusterid) < 0) {
373  SCLogWarning("%s: invalid cluster_id, resetting to 0", iface);
374  aconf->cluster_id = 0;
375  }
376  SCLogDebug("Going to use cluster-id %" PRIu16, aconf->cluster_id);
377  }
378 
379  if (ConfGetChildValueWithDefault(if_root, if_default, "cluster-type", &tmpctype) != 1) {
380  /* default to our safest choice: flow hashing + defrag enabled */
382  cluster_type = PACKET_FANOUT_HASH;
383  } else if (strcmp(tmpctype, "cluster_round_robin") == 0) {
384  SCLogConfig("%s: using round-robin cluster mode for AF_PACKET", aconf->iface);
386  cluster_type = PACKET_FANOUT_LB;
387  } else if (strcmp(tmpctype, "cluster_flow") == 0 || strcmp(tmpctype, "cluster_rollover") == 0) {
388  if (strcmp(tmpctype, "cluster_rollover") == 0) {
389  SCLogWarning("%s: cluster_rollover deprecated; using \"cluster_flow\" instead. See "
390  "ticket #6128",
391  aconf->iface);
392  }
393  /* In hash mode, we also ask for defragmentation needed to
394  * compute the hash */
395  uint16_t defrag = 0;
396  int conf_val = 0;
397  SCLogConfig("%s: using flow cluster mode for AF_PACKET", aconf->iface);
398  ConfGetChildValueBoolWithDefault(if_root, if_default, "defrag", &conf_val);
399  if (conf_val) {
400  SCLogConfig("%s: using defrag kernel functionality for AF_PACKET", aconf->iface);
401  defrag = PACKET_FANOUT_FLAG_DEFRAG;
402  }
403  aconf->cluster_type = PACKET_FANOUT_HASH | defrag;
404  cluster_type = PACKET_FANOUT_HASH;
405  } else if (strcmp(tmpctype, "cluster_cpu") == 0) {
406  SCLogConfig("%s: using cpu cluster mode for AF_PACKET", aconf->iface);
408  cluster_type = PACKET_FANOUT_CPU;
409  } else if (strcmp(tmpctype, "cluster_qm") == 0) {
410  SCLogConfig("%s: using queue based cluster mode for AF_PACKET", aconf->iface);
412  cluster_type = PACKET_FANOUT_QM;
413  } else if (strcmp(tmpctype, "cluster_random") == 0) {
414  SCLogConfig("%s: using random based cluster mode for AF_PACKET", aconf->iface);
416  cluster_type = PACKET_FANOUT_RND;
417 #ifdef HAVE_PACKET_EBPF
418  } else if (strcmp(tmpctype, "cluster_ebpf") == 0) {
419  SCLogInfo("%s: using ebpf based cluster mode for AF_PACKET", aconf->iface);
420  aconf->cluster_type = PACKET_FANOUT_EBPF;
421  cluster_type = PACKET_FANOUT_EBPF;
422 #endif
423  } else {
424  SCLogWarning("invalid cluster-type %s", tmpctype);
425  }
426 
427  int conf_val = 0;
428  ConfGetChildValueBoolWithDefault(if_root, if_default, "rollover", &conf_val);
429  if (conf_val) {
430  SCLogConfig("%s: Rollover requested for AF_PACKET but ignored -- see ticket #6128.",
431  aconf->iface);
432  SCLogWarning("%s: rollover option has been deprecated and will be ignored as it can cause "
433  "severe flow "
434  "tracking issues; see ticket #6128.",
435  iface);
436  }
437 
438  ConfSetBPFFilter(if_root, if_default, iface, &aconf->bpf_filter);
439 
440  if (ConfGetChildValueWithDefault(if_root, if_default, "ebpf-lb-file", &ebpf_file) != 1) {
441  aconf->ebpf_lb_file = NULL;
442  } else {
443 #ifdef HAVE_PACKET_EBPF
444  SCLogConfig("%s: af-packet will use '%s' as eBPF load balancing file", iface, ebpf_file);
445  aconf->ebpf_lb_file = ebpf_file;
446  aconf->ebpf_t_config.flags |= EBPF_SOCKET_FILTER;
447 #endif
448  }
449 
450 #ifdef HAVE_PACKET_EBPF
451  boolval = false;
452  if (ConfGetChildValueBoolWithDefault(if_root, if_default, "pinned-maps", (int *)&boolval) == 1) {
453  if (boolval) {
454  SCLogConfig("%s: using pinned maps", aconf->iface);
455  aconf->ebpf_t_config.flags |= EBPF_PINNED_MAPS;
456  }
457  const char *pinned_maps_name = NULL;
458  if (ConfGetChildValueWithDefault(if_root, if_default,
459  "pinned-maps-name",
460  &pinned_maps_name) != 1) {
461  aconf->ebpf_t_config.pinned_maps_name = pinned_maps_name;
462  } else {
463  aconf->ebpf_t_config.pinned_maps_name = NULL;
464  }
465  } else {
466  aconf->ebpf_t_config.pinned_maps_name = NULL;
467  }
468 #endif
469 
470 #ifdef HAVE_PACKET_EBPF
471  /* One shot loading of the eBPF file */
472  if (aconf->ebpf_lb_file && cluster_type == PACKET_FANOUT_EBPF) {
473  int ret = EBPFLoadFile(aconf->iface, aconf->ebpf_lb_file, "loadbalancer",
474  &aconf->ebpf_lb_fd,
475  &aconf->ebpf_t_config);
476  if (ret != 0) {
477  SCLogWarning("%s: failed to load eBPF lb file", iface);
478  }
479  }
480 #else
481  if (aconf->ebpf_lb_file) {
482  SCLogError("%s: eBPF support is not built-in", iface);
483  }
484 #endif
485 
486  if (ConfGetChildValueWithDefault(if_root, if_default, "ebpf-filter-file", &ebpf_file) != 1) {
487  aconf->ebpf_filter_file = NULL;
488  } else {
489 #ifdef HAVE_PACKET_EBPF
490  SCLogConfig("%s: af-packet will use '%s' as eBPF filter file", iface, ebpf_file);
491  aconf->ebpf_filter_file = ebpf_file;
492  aconf->ebpf_t_config.mode = AFP_MODE_EBPF_BYPASS;
493  aconf->ebpf_t_config.flags |= EBPF_SOCKET_FILTER;
494 #endif
495  ConfGetChildValueBoolWithDefault(if_root, if_default, "bypass", &conf_val);
496  if (conf_val) {
497 #ifdef HAVE_PACKET_EBPF
498  SCLogConfig("%s: using bypass kernel functionality for AF_PACKET", aconf->iface);
499  aconf->flags |= AFP_BYPASS;
500  BypassedFlowManagerRegisterUpdateFunc(EBPFUpdateFlow, NULL);
501 #else
502  SCLogError("%s: bypass set but eBPF support is not built-in", iface);
503 #endif
504  }
505  }
506 
507  /* One shot loading of the eBPF file */
508  if (aconf->ebpf_filter_file) {
509 #ifdef HAVE_PACKET_EBPF
510  int ret = EBPFLoadFile(aconf->iface, aconf->ebpf_filter_file, "filter",
511  &aconf->ebpf_filter_fd,
512  &aconf->ebpf_t_config);
513  if (ret != 0) {
514  SCLogWarning("%s: failed to load eBPF filter file", iface);
515  }
516 #else
517  SCLogError("%s: eBPF support is not built-in", iface);
518 #endif
519  }
520 
521  if (ConfGetChildValueWithDefault(if_root, if_default, "xdp-filter-file", &ebpf_file) != 1) {
522  aconf->xdp_filter_file = NULL;
523  } else {
524 #ifdef HAVE_PACKET_XDP
525  aconf->ebpf_t_config.mode = AFP_MODE_XDP_BYPASS;
526  aconf->ebpf_t_config.flags |= EBPF_XDP_CODE;
527  aconf->xdp_filter_file = ebpf_file;
528  ConfGetChildValueBoolWithDefault(if_root, if_default, "bypass", &conf_val);
529  if (conf_val) {
530  SCLogConfig("%s: using bypass kernel functionality for AF_PACKET", aconf->iface);
531  aconf->flags |= AFP_XDPBYPASS;
532  /* if maps are pinned we need to read them at start */
533  if (aconf->ebpf_t_config.flags & EBPF_PINNED_MAPS) {
535  struct ebpf_timeout_config *ebt = SCCalloc(1, sizeof(struct ebpf_timeout_config));
536  if (ebt == NULL) {
537  SCLogError("%s: flow bypass alloc error", iface);
538  } else {
539  memcpy(ebt, &(aconf->ebpf_t_config), sizeof(struct ebpf_timeout_config));
541  EBPFCheckBypassedFlowCreate,
542  (void *)ebt);
543  }
544  }
545  BypassedFlowManagerRegisterUpdateFunc(EBPFUpdateFlow, NULL);
546  }
547 #else
548  SCLogWarning("%s: XDP filter set but XDP support is not built-in", iface);
549 #endif
550 #ifdef HAVE_PACKET_XDP
551  const char *xdp_mode;
552  if (ConfGetChildValueWithDefault(if_root, if_default, "xdp-mode", &xdp_mode) != 1) {
553  aconf->xdp_mode = XDP_FLAGS_SKB_MODE;
554  } else {
555  if (!strcmp(xdp_mode, "soft")) {
556  aconf->xdp_mode = XDP_FLAGS_SKB_MODE;
557  } else if (!strcmp(xdp_mode, "driver")) {
558  aconf->xdp_mode = XDP_FLAGS_DRV_MODE;
559  } else if (!strcmp(xdp_mode, "hw")) {
560  aconf->xdp_mode = XDP_FLAGS_HW_MODE;
561  aconf->ebpf_t_config.flags |= EBPF_XDP_HW_MODE;
562  } else {
563  SCLogWarning("Invalid xdp-mode value: '%s'", xdp_mode);
564  }
565  }
566 
567  boolval = true;
568  if (ConfGetChildValueBoolWithDefault(if_root, if_default, "use-percpu-hash", (int *)&boolval) == 1) {
569  if (boolval == false) {
570  SCLogConfig("%s: not using percpu hash", aconf->iface);
571  aconf->ebpf_t_config.cpus_count = 1;
572  }
573  }
574 #endif
575  }
576 
577  /* One shot loading of the eBPF file */
578  if (aconf->xdp_filter_file) {
579 #ifdef HAVE_PACKET_XDP
580  int ret = EBPFLoadFile(aconf->iface, aconf->xdp_filter_file, "xdp",
581  &aconf->xdp_filter_fd,
582  &aconf->ebpf_t_config);
583  switch (ret) {
584  case 1:
585  SCLogInfo("%s: loaded pinned maps from sysfs", iface);
586  break;
587  case -1:
588  SCLogWarning("%s: failed to load XDP filter file", iface);
589  break;
590  case 0:
591  ret = EBPFSetupXDP(aconf->iface, aconf->xdp_filter_fd, aconf->xdp_mode);
592  if (ret != 0) {
593  SCLogWarning("%s: failed to set up XDP", iface);
594  } else {
595  /* Try to get the xdp-cpu-redirect key */
596  const char *cpuset;
597  if (ConfGetChildValueWithDefault(if_root, if_default,
598  "xdp-cpu-redirect", &cpuset) == 1) {
599  SCLogConfig("%s: Setting up CPU map XDP", iface);
600  ConfNode *node = ConfGetChildWithDefault(if_root, if_default, "xdp-cpu-redirect");
601  if (node == NULL) {
602  SCLogError("Previously found node has disappeared");
603  } else {
604  EBPFBuildCPUSet(node, aconf->iface);
605  }
606  } else {
607  /* It will just set CPU count to 0 */
608  EBPFBuildCPUSet(NULL, aconf->iface);
609  }
610  }
611  /* we have a peer and we use bypass so we can set up XDP iface redirect */
612  if (aconf->out_iface) {
613  EBPFSetPeerIface(aconf->iface, aconf->out_iface);
614  }
615  }
616 #else
617  SCLogError("%s: XDP support is not built-in", iface);
618 #endif
619  }
620 
621  if ((ConfGetChildValueIntWithDefault(if_root, if_default, "buffer-size", &value)) == 1) {
622  aconf->buffer_size = value;
623  } else {
624  aconf->buffer_size = 0;
625  }
626  if ((ConfGetChildValueIntWithDefault(if_root, if_default, "ring-size", &value)) == 1) {
627  aconf->ring_size = value;
628  }
629 
630  if ((ConfGetChildValueIntWithDefault(if_root, if_default, "block-size", &value)) == 1) {
631  if (value % getpagesize()) {
632  SCLogWarning("%s: block-size %" PRIuMAX " must be a multiple of pagesize (%u).", iface,
633  value, getpagesize());
634  } else {
635  aconf->block_size = value;
636  }
637  }
638 
639  if ((ConfGetChildValueIntWithDefault(if_root, if_default, "block-timeout", &value)) == 1) {
640  aconf->block_timeout = value;
641  } else {
642  aconf->block_timeout = 10;
643  }
644 
645  (void)ConfGetChildValueBoolWithDefault(if_root, if_default, "disable-promisc", (int *)&boolval);
646  if (boolval) {
647  SCLogConfig("%s: disabling promiscuous mode", aconf->iface);
648  aconf->promisc = 0;
649  }
650 
651  if (ConfGetChildValueWithDefault(if_root, if_default, "checksum-checks", &tmpctype) == 1) {
652  if (strcmp(tmpctype, "auto") == 0) {
654  } else if (ConfValIsTrue(tmpctype)) {
656  } else if (ConfValIsFalse(tmpctype)) {
658  } else if (strcmp(tmpctype, "kernel") == 0) {
660  } else {
661  SCLogWarning("%s: invalid value for checksum-checks", aconf->iface);
662  }
663  }
664 
665 finalize:
666 
667  /* if the number of threads is not 1, we need to first check if fanout
668  * functions on this system. */
669  if (aconf->threads != 1) {
670  if (AFPIsFanoutSupported(aconf->cluster_id) == 0) {
671  if (aconf->threads != 0) {
672  SCLogNotice("%s: fanout not supported on this system, falling "
673  "back to 1 capture thread",
674  iface);
675  }
676  aconf->threads = 1;
677  }
678  }
679 
680  /* try to automagically set the proper number of threads */
681  if (aconf->threads == 0) {
682  /* for cluster_flow use core count */
683  if (cluster_type == PACKET_FANOUT_HASH) {
684  aconf->threads = (int)UtilCpuGetNumProcessorsOnline();
685  SCLogPerf("%s: cluster_flow: %u cores, using %u threads", iface, aconf->threads,
686  aconf->threads);
687 
688  /* for cluster_qm use RSS queue count */
689  } else if (cluster_type == PACKET_FANOUT_QM) {
690  int rss_queues = GetIfaceRSSQueuesNum(iface);
691  if (rss_queues > 0) {
692  aconf->threads = rss_queues;
693  SCLogPerf("%s: cluster_qm: %d RSS queues, using %u threads", iface, rss_queues,
694  aconf->threads);
695  }
696  }
697 
698  if (aconf->threads) {
699  SCLogDebug("using %d threads for interface %s", aconf->threads, iface);
700  }
701  }
702  if (aconf->threads <= 0) {
703  aconf->threads = 1;
704  }
705  SC_ATOMIC_RESET(aconf->ref);
706  (void) SC_ATOMIC_ADD(aconf->ref, aconf->threads);
707 
708  if (aconf->ring_size != 0) {
709  if (aconf->ring_size * aconf->threads < max_pending_packets) {
710  aconf->ring_size = max_pending_packets / aconf->threads + 1;
711  SCLogWarning("%s: inefficient setup: ring-size < max_pending_packets. "
712  "Resetting to decent value %d.",
713  iface, aconf->ring_size);
714  /* We want at least that max_pending_packets packets can be handled by the
715  * interface. This is generous if we have multiple interfaces listening. */
716  }
717  } else {
718  /* We want that max_pending_packets packets can be handled by suricata
719  * for this interface. To take burst into account we multiply the obtained
720  * size by 2. */
721  aconf->ring_size = max_pending_packets * 2 / aconf->threads;
722  }
723 
724  int ltype = AFPGetLinkType(iface);
725  switch (ltype) {
726  case LINKTYPE_ETHERNET:
727  /* af-packet can handle csum offloading */
728  if (LiveGetOffload() == 0) {
729  if (GetIfaceOffloading(iface, 0, 1) == 1) {
730  SCLogWarning(
731  "%s: using AF_PACKET with offloads enabled leads to capture problems",
732  iface);
733  }
734  } else {
736  }
737  break;
738  case -1:
739  default:
740  break;
741  }
742 
743  if (active_runmode == NULL || strcmp("workers", active_runmode) != 0) {
744  /* If we are using copy mode we need a lock */
745  aconf->flags |= AFP_SOCK_PROTECT;
746  aconf->flags |= AFP_NEED_PEER;
747  }
748  return aconf;
749 }
750 
751 static int AFPConfigGeThreadsCount(void *conf)
752 {
753  AFPIfaceConfig *afp = (AFPIfaceConfig *)conf;
754  return afp->threads;
755 }
756 
757 #endif /* HAVE_AF_PACKET */
758 
760 {
761  SCEnter();
762 
763 /* We include only if AF_PACKET is enabled */
764 #ifdef HAVE_AF_PACKET
765  int ret;
766  const char *live_dev = NULL;
767 
768  TimeModeSetLive();
769 
770  (void)ConfGet("af-packet.live-interface", &live_dev);
771 
772  SCLogDebug("live_dev %s", live_dev);
773 
774  if (AFPPeersListInit() != TM_ECODE_OK) {
775  FatalError("Unable to init peers list.");
776  }
777 
778  ret = RunModeSetLiveCaptureAutoFp(ParseAFPConfig, AFPConfigGeThreadsCount, "ReceiveAFP",
779  "DecodeAFP", thread_name_autofp, live_dev);
780  if (ret != 0) {
781  FatalError("Unable to start runmode");
782  }
783 
784  /* In IPS mode each threads must have a peer */
785  if (AFPPeersListCheck() != TM_ECODE_OK) {
786  FatalError("Some IPS capture threads did not peer.");
787  }
788 
789  SCLogDebug("RunModeIdsAFPAutoFp initialised");
790 #endif /* HAVE_AF_PACKET */
791 
792  SCReturnInt(0);
793 }
794 
795 /**
796  * \brief Single thread version of the AF_PACKET processing.
797  */
799 {
800  SCEnter();
801 #ifdef HAVE_AF_PACKET
802  int ret;
803  const char *live_dev = NULL;
804 
805  TimeModeSetLive();
806 
807  (void)ConfGet("af-packet.live-interface", &live_dev);
808 
809  if (AFPPeersListInit() != TM_ECODE_OK) {
810  FatalError("Unable to init peers list.");
811  }
812 
813  ret = RunModeSetLiveCaptureSingle(ParseAFPConfig,
814  AFPConfigGeThreadsCount,
815  "ReceiveAFP",
816  "DecodeAFP", thread_name_single,
817  live_dev);
818  if (ret != 0) {
819  FatalError("Unable to start runmode");
820  }
821 
822  /* In IPS mode each threads must have a peer */
823  if (AFPPeersListCheck() != TM_ECODE_OK) {
824  FatalError("Some IPS capture threads did not peer.");
825  }
826 
827  SCLogDebug("RunModeIdsAFPSingle initialised");
828 
829 #endif /* HAVE_AF_PACKET */
830  SCReturnInt(0);
831 }
832 
833 /**
834  * \brief Workers version of the AF_PACKET processing.
835  *
836  * Start N threads with each thread doing all the work.
837  *
838  */
840 {
841  SCEnter();
842 #ifdef HAVE_AF_PACKET
843  int ret;
844  const char *live_dev = NULL;
845 
846  TimeModeSetLive();
847 
848  (void)ConfGet("af-packet.live-interface", &live_dev);
849 
850  if (AFPPeersListInit() != TM_ECODE_OK) {
851  FatalError("Unable to init peers list.");
852  }
853 
854  ret = RunModeSetLiveCaptureWorkers(ParseAFPConfig, AFPConfigGeThreadsCount, "ReceiveAFP",
855  "DecodeAFP", thread_name_workers, live_dev);
856  if (ret != 0) {
857  FatalError("Unable to start runmode");
858  }
859 
860  /* In IPS mode each threads must have a peer */
861  if (AFPPeersListCheck() != TM_ECODE_OK) {
862  FatalError("Some IPS capture threads did not peer.");
863  }
864 
865  SCLogDebug("RunModeIdsAFPWorkers initialised");
866 
867 #endif /* HAVE_AF_PACKET */
868  SCReturnInt(0);
869 }
870 
871 /**
872  * @}
873  */
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:81
AFPIfaceConfig_::promisc
int promisc
Definition: source-af-packet.h:97
util-byte.h
tm-threads.h
AFPIfaceConfig_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-af-packet.h:101
PACKET_FANOUT_FLAG_DEFRAG
#define PACKET_FANOUT_FLAG_DEFRAG
Definition: source-af-packet.h:40
RunModeIdsAFPWorkers
int RunModeIdsAFPWorkers(void)
Workers version of the AF_PACKET processing.
Definition: runmode-af-packet.c:839
flow-bypass.h
AFPIfaceConfig_::xdp_mode
uint8_t xdp_mode
Definition: source-af-packet.h:109
AFP_BYPASS
#define AFP_BYPASS
Definition: source-af-packet.h:65
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:321
alert-debuglog.h
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:314
AFP_COPY_MODE_NONE
#define AFP_COPY_MODE_NONE
Definition: source-af-packet.h:68
RunModeIdsAFPSingle
int RunModeIdsAFPSingle(void)
Single thread version of the AF_PACKET processing.
Definition: runmode-af-packet.c:798
runmode-af-packet.h
util-bpf.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
RunModeIdsAFPRegister
void RunModeIdsAFPRegister(void)
Definition: runmode-af-packet.c:156
ConfGetChildValueBoolWithDefault
int ConfGetChildValueBoolWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, int *val)
Definition: conf.c:514
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
LiveGetOffload
int LiveGetOffload(void)
Definition: util-device.c:81
AFPIfaceConfig_::ebpf_filter_fd
int ebpf_filter_fd
Definition: source-af-packet.h:106
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
PACKET_FANOUT_RND
#define PACKET_FANOUT_RND
Definition: source-af-packet.h:36
UtilCpuGetNumProcessorsConfigured
uint16_t UtilCpuGetNumProcessorsConfigured(void)
Get the number of cpus configured in the system.
Definition: util-cpu.c:59
AFP_SOCK_PROTECT
#define AFP_SOCK_PROTECT
Definition: source-af-packet.h:60
PACKET_FANOUT_HASH
#define PACKET_FANOUT_HASH
Definition: source-af-packet.h:32
AFPPeersListInit
TmEcode AFPPeersListInit(void)
Init the global list of AFPPeer.
Definition: source-af-packet.c:452
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:332
RunModeAFPGetDefaultMode
const char * RunModeAFPGetDefaultMode(void)
Definition: runmode-af-packet.c:64
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:337
PACKET_FANOUT_QM
#define PACKET_FANOUT_QM
Definition: source-af-packet.h:37
AFPIfaceConfig_::threads
int threads
Definition: source-af-packet.h:84
util-runmodes.h
AFPIfaceConfig_::ring_size
int ring_size
Definition: source-af-packet.h:88
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
AFPIfaceConfig_::block_timeout
int block_timeout
Definition: source-af-packet.h:92
ConfNodeLookupKeyValue
ConfNode * ConfNodeLookupKeyValue(const ConfNode *base, const char *key, const char *value)
Lookup for a key value under a specific node.
Definition: conf.c:831
CHECKSUM_VALIDATION_DISABLE
@ CHECKSUM_VALIDATION_DISABLE
Definition: decode.h:46
AFPIfaceConfig_::out_iface
const char * out_iface
Definition: source-af-packet.h:110
CHECKSUM_VALIDATION_KERNEL
@ CHECKSUM_VALIDATION_KERNEL
Definition: decode.h:50
AFP_XDPBYPASS
#define AFP_XDPBYPASS
Definition: source-af-packet.h:66
RunmodeGetActive
char * RunmodeGetActive(void)
Definition: runmodes.c:217
AFPIfaceConfig_::flags
unsigned int flags
Definition: source-af-packet.h:99
StringParseInt32
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:622
AFPIfaceConfig_::xdp_filter_fd
int xdp_filter_fd
Definition: source-af-packet.h:108
ConfGetChildValueIntWithDefault
int ConfGetChildValueIntWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, intmax_t *val)
Definition: conf.c:462
thread_name_single
const char * thread_name_single
Definition: runmodes.c:80
ConfGetChildWithDefault
ConfNode * ConfGetChildWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name)
Definition: conf.c:364
AFPGetLinkType
int AFPGetLinkType(const char *ifname)
Definition: source-af-packet.c:1532
AFPPeersListCheck
TmEcode AFPPeersListCheck(void)
Check that all AFPPeer got a peer.
Definition: source-af-packet.c:469
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:537
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
AFP_BLOCK_SIZE_DEFAULT_ORDER
#define AFP_BLOCK_SIZE_DEFAULT_ORDER
Definition: source-af-packet.h:78
AFPIfaceConfig_::cluster_type
int cluster_type
Definition: source-af-packet.h:95
AFPIfaceConfig_::cluster_id
uint16_t cluster_id
Definition: source-af-packet.h:94
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
RunModeSetLiveCaptureAutoFp
int RunModeSetLiveCaptureAutoFp(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:85
AFPIfaceConfig_::block_size
int block_size
Definition: source-af-packet.h:90
CHECKSUM_VALIDATION_ENABLE
@ CHECKSUM_VALIDATION_ENABLE
Definition: decode.h:47
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
CHECKSUM_VALIDATION_AUTO
@ CHECKSUM_VALIDATION_AUTO
Definition: decode.h:48
util-device.h
util-debug.h
AFPIfaceConfig_::ebpf_lb_fd
int ebpf_lb_fd
Definition: source-af-packet.h:104
AFP_MMAP_LOCKED
#define AFP_MMAP_LOCKED
Definition: source-af-packet.h:64
util-cpu.h
AFPIfaceConfig_::ebpf_filter_file
const char * ebpf_filter_file
Definition: source-af-packet.h:105
PACKET_FANOUT_LB
#define PACKET_FANOUT_LB
Definition: source-af-packet.h:33
RunModeEnablesBypassManager
void RunModeEnablesBypassManager(void)
Definition: runmodes.c:470
LiveGetDevice
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
Definition: util-device.c:248
GetIfaceRSSQueuesNum
int GetIfaceRSSQueuesNum(const char *dev)
Definition: util-ioctl.c:709
ConfFindDeviceConfig
ConfNode * ConfFindDeviceConfig(ConfNode *node, const char *iface)
Find the configuration node for a specific device.
Definition: util-conf.c:121
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
util-ebpf.h
util-affinity.h
PACKET_FANOUT_CPU
#define PACKET_FANOUT_CPU
Definition: source-af-packet.h:34
EngineModeSetIPS
void EngineModeSetIPS(void)
Definition: suricata.c:246
ConfGetChildValueWithDefault
int ConfGetChildValueWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, const char **vptr)
Definition: conf.c:378
util-time.h
AFPIfaceConfig_::DerefFunc
void(* DerefFunc)(void *)
Definition: source-af-packet.h:115
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
AFP_COPY_MODE_TAP
#define AFP_COPY_MODE_TAP
Definition: source-af-packet.h:69
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:341
conf.h
GetIfaceOffloading
int GetIfaceOffloading(const char *dev, int csum, int other)
output offloading status of the link
Definition: util-ioctl.c:666
max_pending_packets
uint16_t max_pending_packets
Definition: suricata.c:186
DisableIfaceOffloading
int DisableIfaceOffloading(LiveDevice *dev, int csum, int other)
Definition: util-ioctl.c:679
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
AFP_NEED_PEER
#define AFP_NEED_PEER
Definition: source-af-packet.h:58
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
alert-fastlog.h
util-conf.h
suricata-common.h
ConfSetBPFFilter
void ConfSetBPFFilter(ConfNode *if_root, ConfNode *if_default, const char *iface, const char **bpf_filter)
Definition: util-bpf.c:30
LiveGetDeviceName
const char * LiveGetDeviceName(int number)
Get a pointer to the device name at idx.
Definition: util-device.c:184
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:230
AFPIfaceConfig_::xdp_filter_file
const char * xdp_filter_file
Definition: source-af-packet.h:107
AFP_TPACKET_V3
#define AFP_TPACKET_V3
Definition: source-af-packet.h:62
FatalError
#define FatalError(...)
Definition: util-debug.h:502
log-httplog.h
RunModeIdsAFPAutoFp
int RunModeIdsAFPAutoFp(void)
Definition: runmode-af-packet.c:759
SC_ATOMIC_RESET
#define SC_ATOMIC_RESET(name)
wrapper for reinitializing an atomic variable.
Definition: util-atomic.h:323
BypassedFlowManagerRegisterUpdateFunc
int BypassedFlowManagerRegisterUpdateFunc(BypassedUpdateFunc UpdateFunc, void *data)
source-af-packet.h
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
AFP_COPY_MODE_IPS
#define AFP_COPY_MODE_IPS
Definition: source-af-packet.h:70
ConfNode_
Definition: conf.h:32
AFPIfaceConfig_::buffer_size
int buffer_size
Definition: source-af-packet.h:86
util-ioctl.h
ConfValIsFalse
int ConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:562
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:350
AFPIfaceConfig_::iface
char iface[AFP_IFACE_NAME_LENGTH]
Definition: source-af-packet.h:82
suricata.h
AFPIfaceConfig_::copy_mode
uint8_t copy_mode
Definition: source-af-packet.h:100
AFPIfaceConfig_
Definition: source-af-packet.h:81
AFPIfaceConfig_::bpf_filter
const char * bpf_filter
Definition: source-af-packet.h:102
LiveGetDeviceCount
int LiveGetDeviceCount(void)
Get the number of registered devices.
Definition: util-device.c:164
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:108
AFPIfaceConfig_::ebpf_lb_file
const char * ebpf_lb_file
Definition: source-af-packet.h:103
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:237
BypassedFlowManagerRegisterCheckFunc
int BypassedFlowManagerRegisterCheckFunc(BypassedCheckFunc CheckFunc, BypassedCheckFuncInit CheckFuncInit, void *data)
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
RUNMODE_AFP_DEV
@ RUNMODE_AFP_DEV
Definition: runmodes.h:37
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
AFPIsFanoutSupported
int AFPIsFanoutSupported(uint16_t cluster_id)
test if we can use FANOUT. Older kernels like those in CentOS6 have HAVE_PACKET_FANOUT defined but fa...
Definition: source-af-packet.c:1811
output.h
LINKTYPE_ETHERNET
#define LINKTYPE_ETHERNET
Definition: decode.h:990
AFP_EMERGENCY_MODE
#define AFP_EMERGENCY_MODE
Definition: source-af-packet.h:61