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