suricata
source-pfring.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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  * \file
20  *
21  * \author William Metcalf <william.metcalf@gmail.com>
22  * \author Eric Leblond <eric@regit.org>
23  *
24  * PF_RING packet acquisition support
25  *
26  * \todo remove requirement for setting cluster so old 3.x versions are supported
27  * \todo implement DNA support
28  * \todo Allow ring options such as snaplen etc, to be user configurable.
29  */
30 
31 #include "suricata-common.h"
32 #include "suricata.h"
33 #include "conf.h"
34 #include "decode.h"
35 #include "packet-queue.h"
36 #include "threads.h"
37 #include "threadvars.h"
38 #include "tm-queuehandlers.h"
39 #include "tm-threads.h"
40 #include "source-pfring.h"
41 #include "util-debug.h"
42 #include "util-checksum.h"
43 #include "util-privs.h"
44 #include "util-device.h"
45 #include "util-host-info.h"
46 #include "runmodes.h"
47 #include "util-profiling.h"
48 
49 TmEcode ReceivePfringLoop(ThreadVars *tv, void *data, void *slot);
50 TmEcode PfringBreakLoop(ThreadVars *tv, void *data);
51 TmEcode ReceivePfringThreadInit(ThreadVars *, const void *, void **);
54 
55 TmEcode DecodePfringThreadInit(ThreadVars *, const void *, void **);
58 
59 extern int max_pending_packets;
60 
61 #ifndef HAVE_PFRING
62 
63 /*Handle cases where we don't have PF_RING support built-in*/
64 TmEcode NoPfringSupportExit(ThreadVars *, const void *, void **);
65 
67 {
68  tmm_modules[TMM_RECEIVEPFRING].name = "ReceivePfring";
69  tmm_modules[TMM_RECEIVEPFRING].ThreadInit = NoPfringSupportExit;
77 }
78 
80 {
81  tmm_modules[TMM_DECODEPFRING].name = "DecodePfring";
82  tmm_modules[TMM_DECODEPFRING].ThreadInit = NoPfringSupportExit;
89 }
90 
91 /**
92  * \brief this function prints an error message and exits.
93  * \param tv pointer to ThreadVars
94  * \param initdata pointer to the interface passed from the user
95  * \param data pointer gets populated with PfringThreadVars
96  */
97 TmEcode NoPfringSupportExit(ThreadVars *tv, const void *initdata, void **data)
98 {
99  SCLogError(SC_ERR_NO_PF_RING,"Error creating thread %s: you do not have support for pfring "
100  "enabled please recompile with --enable-pfring", tv->name);
101  exit(EXIT_FAILURE);
102 }
103 
104 #else /* implied we do have PF_RING support */
105 
106 #include <pfring.h>
107 
108 /** protect pfring_set_bpf_filter, as it is not thread safe */
109 static SCMutex pfring_bpf_set_filter_lock = SCMUTEX_INITIALIZER;
110 
111 /* XXX replace with user configurable options */
112 #define LIBPFRING_PROMISC 1
113 #define LIBPFRING_REENTRANT 0
114 #define LIBPFRING_WAIT_FOR_INCOMING 1
115 
116 /* PfringThreadVars flags */
117 #define PFRING_FLAGS_ZERO_COPY (1 << 0)
118 #define PFRING_FLAGS_BYPASS (1 << 1)
119 
120 /**
121  * \brief Structure to hold thread specific variables.
122  */
124 {
125  /* thread specific handle */
126  pfring *pd;
127 
128  /* counters */
129  uint64_t bytes;
130  uint64_t pkts;
131 
135 
136  uint32_t flags;
137 
140 
142 
143  /* threads count */
144  int threads;
145 
146  cluster_type ctype;
147 
148  uint8_t cluster_id;
149  char *interface;
151 
152  char *bpf_filter;
153 
155 
157 };
158 
159 /**
160  * \brief Registration Function for RecievePfring.
161  * \todo Unit tests are needed for this module.
162  */
164 {
165  tmm_modules[TMM_RECEIVEPFRING].name = "ReceivePfring";
174 }
175 
176 /**
177  * \brief Registration Function for DecodePfring.
178  * \todo Unit tests are needed for this module.
179  */
181 {
182  tmm_modules[TMM_DECODEPFRING].name = "DecodePfring";
189 }
190 
191 static inline void PfringDumpCounters(PfringThreadVars *ptv)
192 {
193  pfring_stat pfring_s;
194  if (likely((pfring_stats(ptv->pd, &pfring_s) >= 0))) {
195  /* pfring counter is per socket and is not cleared after read.
196  * So to get the number of packet on the interface we can add
197  * the newly seen packets and drops for this thread and add it
198  * to the interface counter */
199  uint64_t th_pkts = StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_packets);
200  uint64_t th_drops = StatsGetLocalCounterValue(ptv->tv, ptv->capture_kernel_drops);
201  SC_ATOMIC_ADD(ptv->livedev->pkts, pfring_s.recv - th_pkts);
202  SC_ATOMIC_ADD(ptv->livedev->drop, pfring_s.drop - th_drops);
203  StatsSetUI64(ptv->tv, ptv->capture_kernel_packets, pfring_s.recv);
204  StatsSetUI64(ptv->tv, ptv->capture_kernel_drops, pfring_s.drop);
205 
206 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
207  if (ptv->flags & PFRING_FLAGS_BYPASS) {
208  uint64_t th_bypassed = StatsGetLocalCounterValue(ptv->tv, ptv->capture_bypassed);
209  SC_ATOMIC_ADD(ptv->livedev->bypassed, pfring_s.shunt - th_bypassed);
210  StatsSetUI64(ptv->tv, ptv->capture_bypassed, pfring_s.shunt);
211  }
212 #endif
213  }
214 }
215 
216 /**
217  * \brief Pfring Packet Process function.
218  *
219  * This function fills in our packet structure from libpfring.
220  * From here the packets are picked up by the DecodePfring thread.
221  *
222  * \param user pointer to PfringThreadVars
223  * \param h pointer to pfring packet header
224  * \param p pointer to the current packet
225  */
226 static inline void PfringProcessPacket(void *user, struct pfring_pkthdr *h, Packet *p)
227 {
228  PfringThreadVars *ptv = (PfringThreadVars *)user;
229 
230  ptv->bytes += h->caplen;
231  ptv->pkts++;
232  p->livedev = ptv->livedev;
233 
234  /* PF_RING may fail to set timestamp */
235  if (h->ts.tv_sec == 0) {
236  gettimeofday((struct timeval *)&h->ts, NULL);
237  }
238 
239  p->ts.tv_sec = h->ts.tv_sec;
240  p->ts.tv_usec = h->ts.tv_usec;
241 
242  /* PF_RING all packets are marked as a link type of ethernet
243  * so that is what we do here. */
245 
246  /* In the past, we needed this vlan handling in cases
247  * where the vlan header was stripped from the raw packet.
248  * With modern (at least >= 6) versions of PF_RING, the
249  * 'copy_data_to_ring' function (kernel/pf_ring.c) makes
250  * sure that if the hardware stripped the vlan header,
251  * it is put back by PF_RING.
252  *
253  * PF_RING should put it back in all cases, but as a extra
254  * precaution keep the check here. If the vlan header is
255  * part of the raw packet, the vlan_offset will be set.
256  * So if it is not set, use the parsed info from PF_RING's
257  * extended header.
258  */
259  if (ptv->vlan_in_ext_header &&
260  h->extended_hdr.parsed_pkt.offset.vlan_offset == 0 &&
261  h->extended_hdr.parsed_pkt.vlan_id)
262  {
263  p->vlan_id[0] = h->extended_hdr.parsed_pkt.vlan_id & 0x0fff;
264  p->vlan_idx = 1;
265 
266  if (!ptv->vlan_hdr_warned) {
267  SCLogWarning(SC_ERR_PF_RING_VLAN, "no VLAN header in the raw "
268  "packet. See #2355.");
269  ptv->vlan_hdr_warned = true;
270  }
271  }
272 
273  switch (ptv->checksum_mode) {
275  if (h->extended_hdr.rx_direction == 0) {
277  }
278  break;
281  break;
283  if (ChecksumAutoModeCheck(ptv->pkts,
284  SC_ATOMIC_GET(ptv->livedev->pkts),
285  SC_ATOMIC_GET(ptv->livedev->invalid_checksums))) {
288  }
289  break;
290  default:
291  break;
292  }
293 
294  SET_PKT_LEN(p, h->caplen);
295 }
296 
297 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
298 /**
299  * \brief Pfring bypass callback function
300  *
301  * \param p a Packet to use information from to trigger bypass
302  * \return 1 if bypass is successful, 0 if not
303  */
304 static int PfringBypassCallback(Packet *p)
305 {
306  hw_filtering_rule r;
307 
308  /* Only bypass TCP and UDP */
309  if (!(PKT_IS_TCP(p) || PKT_IS_UDP(p))) {
310  return 0;
311  }
312 
313  /* Bypassing tunneled packets is currently not supported */
314  if (IS_TUNNEL_PKT(p)) {
315  return 0;
316  }
317 
318  r.rule_family_type = generic_flow_id_rule;
319  r.rule_family.flow_id_rule.action = flow_drop_rule;
320  r.rule_family.flow_id_rule.thread = 0;
321  r.rule_family.flow_id_rule.flow_id = p->pfring_v.flow_id;
322 
323  SCLogDebug("Bypass set for flow ID = %u", p->pfring_v.flow_id);
324 
325  if (pfring_add_hw_rule(p->pfring_v.ptv->pd, &r) < 0) {
326  return 0;
327  }
328 
329  return 1;
330 }
331 #endif
332 
333 /**
334  * \brief Recieves packets from an interface via libpfring.
335  *
336  * This function recieves packets from an interface and passes
337  * the packet on to the pfring callback function.
338  *
339  * \param tv pointer to ThreadVars
340  * \param data pointer that gets cast into PfringThreadVars for ptv
341  * \param slot slot containing task information
342  * \retval TM_ECODE_OK on success
343  * \retval TM_ECODE_FAILED on failure
344  */
345 TmEcode ReceivePfringLoop(ThreadVars *tv, void *data, void *slot)
346 {
347  SCEnter();
348 
349  PfringThreadVars *ptv = (PfringThreadVars *)data;
350  Packet *p = NULL;
351  struct pfring_pkthdr hdr;
352  TmSlot *s = (TmSlot *)slot;
353  time_t last_dump = 0;
354  u_int buffer_size;
355  u_char *pkt_buffer;
356 
357  ptv->slot = s->slot_next;
358 
359  /* we have to enable the ring here as we need to do it after all
360  * the threads have called pfring_set_cluster(). */
361  int rc = pfring_enable_ring(ptv->pd);
362  if (rc != 0) {
363  SCLogError(SC_ERR_PF_RING_OPEN, "pfring_enable_ring failed returned %d ", rc);
365  }
366 
367  while(1) {
370  }
371 
372  /* make sure we have at least one packet in the packet pool, to prevent
373  * us from alloc'ing packets at line rate */
374  PacketPoolWait();
375 
377  if (p == NULL) {
379  }
381 
382  /* Some flavours of PF_RING may fail to set timestamp - see PF-RING-enabled libpcap code*/
383  hdr.ts.tv_sec = hdr.ts.tv_usec = 0;
384 
385  /* Check for Zero-copy mode */
386  if (ptv->flags & PFRING_FLAGS_ZERO_COPY) {
387  buffer_size = 0;
388  pkt_buffer = NULL;
389  } else {
390  buffer_size = GET_PKT_DIRECT_MAX_SIZE(p);
391  pkt_buffer = GET_PKT_DIRECT_DATA(p);
392  }
393 
394  int r = pfring_recv(ptv->pd, &pkt_buffer,
395  buffer_size,
396  &hdr,
398  if (likely(r == 1)) {
399  /* profiling started before blocking pfring_recv call, so
400  * reset it here */
402 
403 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
404  if (ptv->flags & PFRING_FLAGS_BYPASS) {
405  /* pkt hash contains the flow id in this configuration */
406  p->pfring_v.flow_id = hdr.extended_hdr.pkt_hash;
407  p->pfring_v.ptv = ptv;
408  p->BypassPacketsFlow = PfringBypassCallback;
409  }
410 #endif
411 
412  /* Check for Zero-copy mode */
413  if (ptv->flags & PFRING_FLAGS_ZERO_COPY) {
414  PacketSetData(p, pkt_buffer, hdr.caplen);
415  }
416 
417  PfringProcessPacket(ptv, &hdr, p);
418 
419  if (TmThreadsSlotProcessPkt(ptv->tv, ptv->slot, p) != TM_ECODE_OK) {
421  }
422 
423  /* Trigger one dump of stats every second */
424  if (p->ts.tv_sec != last_dump) {
425  PfringDumpCounters(ptv);
426  last_dump = p->ts.tv_sec;
427  }
428  } else if (unlikely(r == 0)) {
431  }
432 
433  /* pfring didn't use the packet yet */
434  TmThreadsCaptureHandleTimeout(tv, p);
435 
436  } else {
437  SCLogError(SC_ERR_PF_RING_RECV,"pfring_recv error %" PRId32 "", r);
438  TmqhOutputPacketpool(ptv->tv, p);
440  }
442  }
443 
444  return TM_ECODE_OK;
445 }
446 
447 /**
448  * \brief Stop function for ReceivePfringLoop.
449  *
450  * This function forces ReceivePfringLoop to stop the
451  * execution, exiting the packet capture loop.
452  *
453  * \param tv pointer to ThreadVars
454  * \param data pointer that gets cast into PfringThreadVars for ptv
455  * \retval TM_ECODE_OK on success
456  * \retval TM_ECODE_FAILED on failure
457  */
459 {
460  PfringThreadVars *ptv = (PfringThreadVars *)data;
461 
462  /* Safety check */
463  if (ptv->pd == NULL) {
464  return TM_ECODE_FAILED;
465  }
466 
467  pfring_breakloop(ptv->pd);
468 
469  return TM_ECODE_OK;
470 }
471 
472 /**
473  * \brief Init function for RecievePfring.
474  *
475  * This is a setup function for recieving packets
476  * via libpfring.
477  *
478  * \param tv pointer to ThreadVars
479  * \param initdata pointer to the interface passed from the user
480  * \param data pointer gets populated with PfringThreadVars
481  * \todo add a config option for setting cluster id
482  * \todo Create a general pfring setup function.
483  * \retval TM_ECODE_OK on success
484  * \retval TM_ECODE_FAILED on error
485  */
486 TmEcode ReceivePfringThreadInit(ThreadVars *tv, const void *initdata, void **data)
487 {
488  int rc;
489  u_int32_t version = 0;
490  PfringIfaceConfig *pfconf = (PfringIfaceConfig *) initdata;
491  unsigned int opflag;
492  char const *active_runmode = RunmodeGetActive();
493 
494  if (pfconf == NULL)
495  return TM_ECODE_FAILED;
496 
498  if (unlikely(ptv == NULL)) {
499  pfconf->DerefFunc(pfconf);
500  return TM_ECODE_FAILED;
501  }
502  memset(ptv, 0, sizeof(PfringThreadVars));
503 
504  ptv->tv = tv;
505  ptv->threads = 1;
506 
507  ptv->interface = SCStrdup(pfconf->iface);
508  if (unlikely(ptv->interface == NULL)) {
509  SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate device string");
510  SCFree(ptv);
512  }
513 
514  ptv->livedev = LiveGetDevice(pfconf->iface);
515  if (ptv->livedev == NULL) {
516  SCLogError(SC_ERR_INVALID_VALUE, "Unable to find Live device");
517  SCFree(ptv);
519  }
520 
521  /* enable zero-copy mode for workers runmode */
522  if (active_runmode && strcmp("workers", active_runmode) == 0) {
524  SCLogPerf("Enabling zero-copy for %s", ptv->interface);
525  }
526 
527  ptv->checksum_mode = pfconf->checksum_mode;
528 
529  opflag = PF_RING_PROMISC;
530 
531  /* if we have a recent kernel, we need to use parsed_pkt to get VLAN info */
532  if (ptv->vlan_in_ext_header) {
533  opflag |= PF_RING_LONG_HEADER;
534  }
535 
537  if (strncmp(ptv->interface, "dna", 3) == 0) {
539  "Can't use rxonly checksum-checks on DNA interface,"
540  " resetting to auto");
542  } else {
543  opflag |= PF_RING_LONG_HEADER;
544  }
545  }
546 
547 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
548  if (pfconf->flags & PFRING_CONF_FLAGS_BYPASS) {
549  opflag |= PF_RING_FLOW_OFFLOAD | PF_RING_FLOW_OFFLOAD_NOUPDATES;
550  ptv->flags |= PFRING_FLAGS_BYPASS;
551  }
552 #endif
553 
554  ptv->pd = pfring_open(ptv->interface, (uint32_t)default_packet_size, opflag);
555  if (ptv->pd == NULL) {
556  SCLogError(SC_ERR_PF_RING_OPEN,"Failed to open %s: pfring_open error."
557  " Check if %s exists and pf_ring module is loaded.",
558  ptv->interface,
559  ptv->interface);
560  pfconf->DerefFunc(pfconf);
561  SCFree(ptv);
562  return TM_ECODE_FAILED;
563  }
564 
565  pfring_set_application_name(ptv->pd, (char *)PROG_NAME);
566  pfring_version(ptv->pd, &version);
567 
568  /* We only set cluster info if the number of pfring threads is greater than 1 */
569  ptv->threads = pfconf->threads;
570 
571  ptv->cluster_id = pfconf->cluster_id;
572 
573  if ((ptv->threads == 1) && (strncmp(ptv->interface, "dna", 3) == 0)) {
574  SCLogInfo("DNA interface detected, not adding thread to cluster");
575  } else if (strncmp(ptv->interface, "zc", 2) == 0) {
576  SCLogInfo("ZC interface detected, not adding thread to cluster");
577  } else {
578  ptv->ctype = (cluster_type)pfconf->ctype;
579  rc = pfring_set_cluster(ptv->pd, ptv->cluster_id, ptv->ctype);
580 
581  if (rc != 0) {
582  SCLogError(SC_ERR_PF_RING_SET_CLUSTER_FAILED, "pfring_set_cluster "
583  "returned %d for cluster-id: %d", rc, ptv->cluster_id);
584  if (rc != PF_RING_ERROR_NOT_SUPPORTED || (pfconf->flags & PFRING_CONF_FLAGS_CLUSTER)) {
585  /* cluster is mandatory as explicitly specified in the configuration */
586  pfconf->DerefFunc(pfconf);
587  return TM_ECODE_FAILED;
588  }
589  }
590  }
591 
592  if (ptv->threads > 1) {
593  SCLogPerf("(%s) Using PF_RING v.%d.%d.%d, interface %s, cluster-id %d",
594  tv->name, (version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8,
595  version & 0x000000FF, ptv->interface, ptv->cluster_id);
596  } else {
597  SCLogPerf("(%s) Using PF_RING v.%d.%d.%d, interface %s, cluster-id %d, single-pfring-thread",
598  tv->name, (version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8,
599  version & 0x000000FF, ptv->interface, ptv->cluster_id);
600  }
601 
602  if (pfconf->bpf_filter) {
603  ptv->bpf_filter = SCStrdup(pfconf->bpf_filter);
604  if (unlikely(ptv->bpf_filter == NULL)) {
605  SCLogError(SC_ERR_MEM_ALLOC, "Set PF_RING bpf filter failed.");
606  } else {
607  SCMutexLock(&pfring_bpf_set_filter_lock);
608  rc = pfring_set_bpf_filter(ptv->pd, ptv->bpf_filter);
609  SCMutexUnlock(&pfring_bpf_set_filter_lock);
610 
611  if (rc < 0) {
612  SCLogError(SC_ERR_INVALID_VALUE, "Failed to compile BPF \"%s\"",
613  ptv->bpf_filter);
614  return TM_ECODE_FAILED;
615  }
616  }
617  }
618 
619  ptv->capture_kernel_packets = StatsRegisterCounter("capture.kernel_packets",
620  ptv->tv);
621  ptv->capture_kernel_drops = StatsRegisterCounter("capture.kernel_drops",
622  ptv->tv);
623 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
624  ptv->capture_bypassed = StatsRegisterCounter("capture.bypassed",
625  ptv->tv);
626 #endif
627 
628  /* If kernel is older than 3.0, VLAN is not stripped so we don't
629  * get the info from packt extended header but we will use a standard
630  * parsing */
631  ptv->vlan_in_ext_header = 1;
632  if (! SCKernelVersionIsAtLeast(3, 0)) {
633  ptv->vlan_in_ext_header = 0;
634  }
635 
636  /* If VLAN tags are not in the extended header, set cluster type to 5-tuple
637  * or in case of a ZC interface, do nothing */
638  if ((! ptv->vlan_in_ext_header) && ptv->ctype == CLUSTER_FLOW &&
639  strncmp(ptv->interface, "zc", 2) != 0) {
640  SCLogPerf("VLAN not in extended header, setting cluster type to CLUSTER_FLOW_5_TUPLE");
641  rc = pfring_set_cluster(ptv->pd, ptv->cluster_id, CLUSTER_FLOW_5_TUPLE);
642 
643  if (rc != 0) {
644  SCLogError(SC_ERR_PF_RING_SET_CLUSTER_FAILED, "pfring_set_cluster "
645  "returned %d for cluster-id: %d", rc, ptv->cluster_id);
646  pfconf->DerefFunc(pfconf);
647  return TM_ECODE_FAILED;
648  }
649  }
650 
651  *data = (void *)ptv;
652  pfconf->DerefFunc(pfconf);
653 
654  return TM_ECODE_OK;
655 }
656 
657 /**
658  * \brief This function prints stats to the screen at exit.
659  * \param tv pointer to ThreadVars
660  * \param data pointer that gets cast into PfringThreadVars for ptv
661  */
663 {
664  PfringThreadVars *ptv = (PfringThreadVars *)data;
665 
666  PfringDumpCounters(ptv);
667  SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", dropped %" PRIu64 "",
668  tv->name,
671  SCLogPerf("(%s) Packets %" PRIu64 ", bytes %" PRIu64 "", tv->name, ptv->pkts, ptv->bytes);
672 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
673  if (ptv->flags & PFRING_FLAGS_BYPASS) {
674  SCLogPerf("(%s) Bypass: Packets %" PRIu64 "",
675  tv->name,
677  }
678 #endif
679 }
680 
681 /**
682  * \brief DeInit function closes pd at exit.
683  * \param tv pointer to ThreadVars
684  * \param data pointer that gets cast into PfringThreadVars for ptvi
685  * \retval TM_ECODE_OK is always returned
686  */
688 {
689  PfringThreadVars *ptv = (PfringThreadVars *)data;
690  if (ptv->interface)
691  SCFree(ptv->interface);
692  pfring_remove_from_cluster(ptv->pd);
693 
694  if (ptv->bpf_filter) {
695  pfring_remove_bpf_filter(ptv->pd);
696  SCFree(ptv->bpf_filter);
697  }
698 
699  pfring_close(ptv->pd);
700  return TM_ECODE_OK;
701 }
702 
703 /**
704  * \brief This function passes off to link type decoders.
705  *
706  * DecodePfring decodes raw packets from PF_RING. Inside of libpcap version of
707  * PF_RING all packets are marked as a link type of ethernet so that is what we do here.
708  *
709  * \param tv pointer to ThreadVars
710  * \param p pointer to the current packet
711  * \param data pointer that gets cast into PfringThreadVars for ptv
712  *
713  * \todo Verify that PF_RING only deals with ethernet traffic
714  *
715  * \warning This function bypasses the pkt buf and len macro's
716  *
717  * \retval TM_ECODE_OK is always returned
718  */
720 {
722 
724 
725  /* update counters */
727 
728  /* If suri has set vlan during reading, we increase vlan counter */
729  if (p->vlan_idx) {
731  }
732 
734 
736 
737  return TM_ECODE_OK;
738 }
739 
740 /**
741  * \brief This an Init function for DecodePfring
742  *
743  * \param tv pointer to ThreadVars
744  * \param initdata pointer to initilization data.
745  * \param data pointer that gets cast into PfringThreadVars for ptv
746  * \retval TM_ECODE_OK is returned on success
747  * \retval TM_ECODE_FAILED is returned on error
748  */
749 TmEcode DecodePfringThreadInit(ThreadVars *tv, const void *initdata, void **data)
750 {
751  DecodeThreadVars *dtv = NULL;
752 
754  if (dtv == NULL)
756 
758 
759  *data = (void *)dtv;
760 
761  return TM_ECODE_OK;
762 }
763 
765 {
766  if (data != NULL)
767  DecodeThreadVarsFree(tv, data);
769 }
770 
771 #endif /* HAVE_PFRING */
772 /* eof */
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:67
PKT_IS_UDP
#define PKT_IS_UDP(p)
Definition: decode.h:257
tm-threads.h
ReceivePfringThreadInit
TmEcode ReceivePfringThreadInit(ThreadVars *, const void *, void **)
Init function for RecievePfring.
Definition: source-pfring.c:486
PfringIfaceConfig_
Definition: source-pfring.h:38
max_pending_packets
int max_pending_packets
Definition: suricata.c:211
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:169
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
PfringThreadVars_::cluster_id
uint8_t cluster_id
Definition: source-pfring.c:148
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
Packet_::vlan_id
uint16_t vlan_id[2]
Definition: decode.h:438
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1125
SC_ERR_PF_RING_RECV
@ SC_ERR_PF_RING_RECV
Definition: util-error.h:61
PfringIfaceConfig_::cluster_id
int cluster_id
Definition: source-pfring.h:42
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
ReceivePfringLoop
TmEcode ReceivePfringLoop(ThreadVars *tv, void *data, void *slot)
Recieves packets from an interface via libpfring.
Definition: source-pfring.c:345
ReceivePfringThreadExitStats
void ReceivePfringThreadExitStats(ThreadVars *, void *)
This function prints stats to the screen at exit.
Definition: source-pfring.c:662
PfringBreakLoop
TmEcode PfringBreakLoop(ThreadVars *tv, void *data)
Stop function for ReceivePfringLoop.
Definition: source-pfring.c:458
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
DecodePfringThreadDeinit
TmEcode DecodePfringThreadDeinit(ThreadVars *tv, void *data)
Definition: source-pfring.c:764
TMM_DECODEPFRING
@ TMM_DECODEPFRING
Definition: tm-threads-common.h:43
util-checksum.h
Packet_::flags
uint32_t flags
Definition: decode.h:446
threads.h
TMM_RECEIVEPFRING
@ TMM_RECEIVEPFRING
Definition: tm-threads-common.h:42
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:439
PfringThreadVars_::tv
ThreadVars * tv
Definition: source-pfring.c:138
PACKET_PROFILING_RESTART
#define PACKET_PROFILING_RESTART(p)
Definition: util-profiling.h:99
PfringIfaceConfig_::bpf_filter
char * bpf_filter
Definition: source-pfring.h:49
LiveDevice_
Definition: util-device.h:40
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:304
PfringThreadVars_::livedev
LiveDevice * livedev
Definition: source-pfring.c:150
StatsSetUI64
void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Sets a value of type double to the local counter.
Definition: counters.c:191
PfringThreadVars_::ctype
cluster_type ctype
Definition: source-pfring.c:146
packet-queue.h
CHECKSUM_VALIDATION_RXONLY
@ CHECKSUM_VALIDATION_RXONLY
Definition: decode.h:45
SURICATA_STOP
#define SURICATA_STOP
Definition: suricata.h:90
SCKernelVersionIsAtLeast
int SCKernelVersionIsAtLeast(int major, int minor)
Definition: util-host-info.c:37
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
PfringThreadVars_::vlan_hdr_warned
bool vlan_hdr_warned
Definition: source-pfring.c:156
SC_ERR_PF_RING_OPEN
@ SC_ERR_PF_RING_OPEN
Definition: util-error.h:64
util-privs.h
PfringThreadVars_::slot
TmSlot * slot
Definition: source-pfring.c:139
StatsSyncCountersIfSignalled
#define StatsSyncCountersIfSignalled(tv)
Definition: counters.h:137
CHECKSUM_VALIDATION_DISABLE
@ CHECKSUM_VALIDATION_DISABLE
Definition: decode.h:42
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
SC_CAP_NET_BROADCAST
#define SC_CAP_NET_BROADCAST
Definition: util-privs.h:34
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:118
RunmodeGetActive
char * RunmodeGetActive(void)
Definition: runmodes.c:187
TmqhOutputPacketpool
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
Definition: tmqh-packetpool.c:375
Packet_::BypassPacketsFlow
int(* BypassPacketsFlow)(struct Packet_ *)
Definition: decode.h:490
TmModuleReceivePfringRegister
void TmModuleReceivePfringRegister(void)
Registration Function for RecievePfring.
Definition: source-pfring.c:163
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
PfringThreadVars_::pd
pfring * pd
Definition: source-pfring.c:126
GET_PKT_DIRECT_MAX_SIZE
#define GET_PKT_DIRECT_MAX_SIZE(p)
Definition: decode.h:230
ChecksumAutoModeCheck
int ChecksumAutoModeCheck(uint64_t thread_count, uint64_t iface_count, uint64_t iface_fail)
Check if the number of invalid checksums indicate checksum offloading in place.
Definition: util-checksum.c:70
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:54
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
Packet_::datalink
int datalink
Definition: decode.h:577
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1128
SC_CAP_NET_BIND_SERVICE
#define SC_CAP_NET_BIND_SERVICE
Definition: util-privs.h:33
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:256
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:478
PfringIfaceConfig_::iface
char iface[PFRING_IFACE_NAME_LENGTH]
Definition: source-pfring.h:45
CHECKSUM_VALIDATION_AUTO
@ CHECKSUM_VALIDATION_AUTO
Definition: decode.h:44
SET_PKT_LEN
#define SET_PKT_LEN(p, len)
Definition: decode.h:232
decode.h
SC_ERR_PF_RING_VLAN
@ SC_ERR_PF_RING_VLAN
Definition: util-error.h:337
util-device.h
util-debug.h
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:50
TmModule_::PktAcqBreakLoop
TmEcode(* PktAcqBreakLoop)(ThreadVars *, void *)
Definition: tm-modules.h:57
PfringThreadVars_::threads
int threads
Definition: source-pfring.c:144
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
LiveGetDevice
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
Definition: util-device.c:278
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:228
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:52
SC_CAP_NET_ADMIN
#define SC_CAP_NET_ADMIN
Definition: util-privs.h:31
source-pfring.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
util-profiling.h
StatsGetLocalCounterValue
uint64_t StatsGetLocalCounterValue(ThreadVars *tv, uint16_t id)
Get the value of the local copy of the counter that hold this id.
Definition: counters.c:1240
SC_ERR_PF_RING_SET_CLUSTER_FAILED
@ SC_ERR_PF_RING_SET_CLUSTER_FAILED
Definition: util-error.h:67
PFRING_CONF_FLAGS_CLUSTER
#define PFRING_CONF_FLAGS_CLUSTER
Definition: source-pfring.h:34
PacketPoolWait
void PacketPoolWait(void)
Definition: tmqh-packetpool.c:84
DecodePfringThreadInit
TmEcode DecodePfringThreadInit(ThreadVars *, const void *, void **)
This an Init function for DecodePfring.
Definition: source-pfring.c:749
Packet_
Definition: decode.h:411
TM_FLAG_DECODE_TM
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:32
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:33
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:227
conf.h
TmModule_::RegisterTests
void(* RegisterTests)(void)
Definition: tm-modules.h:65
TmSlot_
Definition: tm-threads.h:52
PKT_IGNORE_CHECKSUM
#define PKT_IGNORE_CHECKSUM
Definition: decode.h:1096
Packet_::livedev
struct LiveDevice_ * livedev
Definition: decode.h:556
TmEcode
TmEcode
Definition: tm-threads-common.h:77
IS_TUNNEL_PKT
#define IS_TUNNEL_PKT(p)
Definition: decode.h:874
util-host-info.h
TmModule_::name
const char * name
Definition: tm-modules.h:44
PFRING_CONF_FLAGS_BYPASS
#define PFRING_CONF_FLAGS_BYPASS
Definition: source-pfring.h:35
PfringThreadVars_::capture_bypassed
uint16_t capture_bypassed
Definition: source-pfring.c:134
DecodeThreadVars_::counter_vlan
uint16_t counter_vlan
Definition: decode.h:652
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
TM_FLAG_RECEIVE_TM
#define TM_FLAG_RECEIVE_TM
Definition: tm-modules.h:31
ReceivePfringThreadDeinit
TmEcode ReceivePfringThreadDeinit(ThreadVars *, void *)
DeInit function closes pd at exit.
Definition: source-pfring.c:687
PfringThreadVars_
Structure to hold thread specific variables.
Definition: source-pfring.c:124
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:30
default_packet_size
uint32_t default_packet_size
Definition: decode.c:71
tm-queuehandlers.h
PfringIfaceConfig_::flags
uint32_t flags
Definition: source-pfring.h:39
PROG_NAME
#define PROG_NAME
Definition: suricata.h:71
PfringThreadVars_::interface
char * interface
Definition: source-pfring.c:149
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:638
Packet_::ts
struct timeval ts
Definition: decode.h:454
ChecksumValidationMode
ChecksumValidationMode
Definition: decode.h:41
suricata-common.h
CLUSTER_FLOW_5_TUPLE
#define CLUSTER_FLOW_5_TUPLE
Definition: source-pfring.h:77
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:224
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
version
uint8_t version
Definition: decode-gre.h:1
PfringIfaceConfig_::DerefFunc
void(* DerefFunc)(void *)
Definition: source-pfring.h:53
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
TmModuleDecodePfringRegister
void TmModuleDecodePfringRegister(void)
Registration Function for DecodePfring.
Definition: source-pfring.c:180
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
PfringThreadVars_::flags
uint32_t flags
Definition: source-pfring.c:136
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:48
threadvars.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
PfringIfaceConfig_::threads
int threads
Definition: source-pfring.h:47
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:625
PfringThreadVars_::capture_kernel_drops
uint16_t capture_kernel_drops
Definition: source-pfring.c:133
PFRING_FLAGS_ZERO_COPY
#define PFRING_FLAGS_ZERO_COPY
Definition: source-pfring.c:117
PfringThreadVars_::vlan_in_ext_header
int vlan_in_ext_header
Definition: source-pfring.c:141
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:619
PacketSetData
int PacketSetData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Set data for Packet and set length when zero copy is used.
Definition: decode.c:658
GET_PKT_DIRECT_DATA
#define GET_PKT_DIRECT_DATA(p)
Definition: decode.h:229
PfringThreadVars_::bpf_filter
char * bpf_filter
Definition: source-pfring.c:152
PfringThreadVars_::bytes
uint64_t bytes
Definition: source-pfring.c:129
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
suricata.h
PfringThreadVars_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-pfring.c:154
CLUSTER_FLOW
#define CLUSTER_FLOW
Definition: source-pfring.h:75
likely
#define likely(expr)
Definition: util-optimize.h:32
TmSlot_::slot_next
struct TmSlot_ * slot_next
Definition: tm-threads.h:61
PfringThreadVars_::capture_kernel_packets
uint16_t capture_kernel_packets
Definition: source-pfring.c:132
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:347
LIBPFRING_WAIT_FOR_INCOMING
#define LIBPFRING_WAIT_FOR_INCOMING
Definition: source-pfring.c:114
PfringThreadVars_::pkts
uint64_t pkts
Definition: source-pfring.c:130
StatsRegisterCounter
uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
Registers a normal, unqualified counter.
Definition: counters.c:939
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
PfringIfaceConfig_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-pfring.h:51
DecodePfring
TmEcode DecodePfring(ThreadVars *, Packet *, void *)
This function passes off to link type decoders.
Definition: source-pfring.c:719
SCMutex
#define SCMutex
Definition: threads-debug.h:114
PacketGetFromQueueOrAlloc
Packet * PacketGetFromQueueOrAlloc(void)
Get a packet. We try to get a packet from the packetpool first, but if that is empty we alloc a packe...
Definition: decode.c:180
SC_CAP_NET_RAW
#define SC_CAP_NET_RAW
Definition: util-privs.h:32
DecodeEthernet
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-ethernet.c:41
PFRING_FLAGS_BYPASS
#define PFRING_FLAGS_BYPASS
Definition: source-pfring.c:118
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:70
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:585
LINKTYPE_ETHERNET
#define LINKTYPE_ETHERNET
Definition: decode.h:1066
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:197
SC_ERR_NO_PF_RING
@ SC_ERR_NO_PF_RING
Definition: util-error.h:60
PfringIfaceConfig_::ctype
unsigned int ctype
Definition: source-pfring.h:43