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 (ptv->livedev->ignore_checksum) {
285  } else if (ChecksumAutoModeCheck(ptv->pkts,
286  SC_ATOMIC_GET(ptv->livedev->pkts),
287  SC_ATOMIC_GET(ptv->livedev->invalid_checksums))) {
288  ptv->livedev->ignore_checksum = 1;
290  }
291  break;
292  default:
293  break;
294  }
295 
296  SET_PKT_LEN(p, h->caplen);
297 }
298 
299 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
300 /**
301  * \brief Pfring bypass callback function
302  *
303  * \param p a Packet to use information from to trigger bypass
304  * \return 1 if bypass is successful, 0 if not
305  */
306 static int PfringBypassCallback(Packet *p)
307 {
308  hw_filtering_rule r;
309 
310  /* Only bypass TCP and UDP */
311  if (!(PKT_IS_TCP(p) || PKT_IS_UDP(p))) {
312  return 0;
313  }
314 
315  /* Bypassing tunneled packets is currently not supported */
316  if (IS_TUNNEL_PKT(p)) {
317  return 0;
318  }
319 
320  r.rule_family_type = generic_flow_id_rule;
321  r.rule_family.flow_id_rule.action = flow_drop_rule;
322  r.rule_family.flow_id_rule.thread = 0;
323  r.rule_family.flow_id_rule.flow_id = p->pfring_v.flow_id;
324 
325  SCLogDebug("Bypass set for flow ID = %u", p->pfring_v.flow_id);
326 
327  if (pfring_add_hw_rule(p->pfring_v.ptv->pd, &r) < 0) {
328  return 0;
329  }
330 
331  return 1;
332 }
333 #endif
334 
335 /**
336  * \brief Recieves packets from an interface via libpfring.
337  *
338  * This function recieves packets from an interface and passes
339  * the packet on to the pfring callback function.
340  *
341  * \param tv pointer to ThreadVars
342  * \param data pointer that gets cast into PfringThreadVars for ptv
343  * \param slot slot containing task information
344  * \retval TM_ECODE_OK on success
345  * \retval TM_ECODE_FAILED on failure
346  */
347 TmEcode ReceivePfringLoop(ThreadVars *tv, void *data, void *slot)
348 {
349  SCEnter();
350 
351  PfringThreadVars *ptv = (PfringThreadVars *)data;
352  Packet *p = NULL;
353  struct pfring_pkthdr hdr;
354  TmSlot *s = (TmSlot *)slot;
355  time_t last_dump = 0;
356  u_int buffer_size;
357  u_char *pkt_buffer;
358 
359  ptv->slot = s->slot_next;
360 
361  /* we have to enable the ring here as we need to do it after all
362  * the threads have called pfring_set_cluster(). */
363  int rc = pfring_enable_ring(ptv->pd);
364  if (rc != 0) {
365  SCLogError(SC_ERR_PF_RING_OPEN, "pfring_enable_ring failed returned %d ", rc);
367  }
368 
369  while(1) {
372  }
373 
374  /* make sure we have at least one packet in the packet pool, to prevent
375  * us from alloc'ing packets at line rate */
376  PacketPoolWait();
377 
379  if (p == NULL) {
381  }
383 
384  /* Some flavours of PF_RING may fail to set timestamp - see PF-RING-enabled libpcap code*/
385  hdr.ts.tv_sec = hdr.ts.tv_usec = 0;
386 
387  /* Check for Zero-copy mode */
388  if (ptv->flags & PFRING_FLAGS_ZERO_COPY) {
389  buffer_size = 0;
390  pkt_buffer = NULL;
391  } else {
392  buffer_size = GET_PKT_DIRECT_MAX_SIZE(p);
393  pkt_buffer = GET_PKT_DIRECT_DATA(p);
394  }
395 
396  int r = pfring_recv(ptv->pd, &pkt_buffer,
397  buffer_size,
398  &hdr,
400  if (likely(r == 1)) {
401  /* profiling started before blocking pfring_recv call, so
402  * reset it here */
404 
405 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
406  if (ptv->flags & PFRING_FLAGS_BYPASS) {
407  /* pkt hash contains the flow id in this configuration */
408  p->pfring_v.flow_id = hdr.extended_hdr.pkt_hash;
409  p->pfring_v.ptv = ptv;
410  p->BypassPacketsFlow = PfringBypassCallback;
411  }
412 #endif
413 
414  /* Check for Zero-copy mode */
415  if (ptv->flags & PFRING_FLAGS_ZERO_COPY) {
416  PacketSetData(p, pkt_buffer, hdr.caplen);
417  }
418 
419  PfringProcessPacket(ptv, &hdr, p);
420 
421  if (TmThreadsSlotProcessPkt(ptv->tv, ptv->slot, p) != TM_ECODE_OK) {
423  }
424 
425  /* Trigger one dump of stats every second */
426  if (p->ts.tv_sec != last_dump) {
427  PfringDumpCounters(ptv);
428  last_dump = p->ts.tv_sec;
429  }
430  } else if (unlikely(r == 0)) {
433  }
434 
435  /* pfring didn't use the packet yet */
436  TmThreadsCaptureHandleTimeout(tv, p);
437 
438  } else {
439  SCLogError(SC_ERR_PF_RING_RECV,"pfring_recv error %" PRId32 "", r);
440  TmqhOutputPacketpool(ptv->tv, p);
442  }
444  }
445 
446  return TM_ECODE_OK;
447 }
448 
449 /**
450  * \brief Stop function for ReceivePfringLoop.
451  *
452  * This function forces ReceivePfringLoop to stop the
453  * execution, exiting the packet capture loop.
454  *
455  * \param tv pointer to ThreadVars
456  * \param data pointer that gets cast into PfringThreadVars for ptv
457  * \retval TM_ECODE_OK on success
458  * \retval TM_ECODE_FAILED on failure
459  */
461 {
462  PfringThreadVars *ptv = (PfringThreadVars *)data;
463 
464  /* Safety check */
465  if (ptv->pd == NULL) {
466  return TM_ECODE_FAILED;
467  }
468 
469  pfring_breakloop(ptv->pd);
470 
471  return TM_ECODE_OK;
472 }
473 
474 /**
475  * \brief Init function for RecievePfring.
476  *
477  * This is a setup function for recieving packets
478  * via libpfring.
479  *
480  * \param tv pointer to ThreadVars
481  * \param initdata pointer to the interface passed from the user
482  * \param data pointer gets populated with PfringThreadVars
483  * \todo add a config option for setting cluster id
484  * \todo Create a general pfring setup function.
485  * \retval TM_ECODE_OK on success
486  * \retval TM_ECODE_FAILED on error
487  */
488 TmEcode ReceivePfringThreadInit(ThreadVars *tv, const void *initdata, void **data)
489 {
490  int rc;
491  u_int32_t version = 0;
492  PfringIfaceConfig *pfconf = (PfringIfaceConfig *) initdata;
493  unsigned int opflag;
494  char const *active_runmode = RunmodeGetActive();
495 
496  if (pfconf == NULL)
497  return TM_ECODE_FAILED;
498 
500  if (unlikely(ptv == NULL)) {
501  pfconf->DerefFunc(pfconf);
502  return TM_ECODE_FAILED;
503  }
504  memset(ptv, 0, sizeof(PfringThreadVars));
505 
506  ptv->tv = tv;
507  ptv->threads = 1;
508 
509  ptv->interface = SCStrdup(pfconf->iface);
510  if (unlikely(ptv->interface == NULL)) {
511  SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate device string");
512  SCFree(ptv);
514  }
515 
516  ptv->livedev = LiveGetDevice(pfconf->iface);
517  if (ptv->livedev == NULL) {
518  SCLogError(SC_ERR_INVALID_VALUE, "Unable to find Live device");
519  SCFree(ptv);
521  }
522 
523  /* enable zero-copy mode for workers runmode */
524  if (active_runmode && strcmp("workers", active_runmode) == 0) {
526  SCLogPerf("Enabling zero-copy for %s", ptv->interface);
527  }
528 
529  ptv->checksum_mode = pfconf->checksum_mode;
530 
531  opflag = PF_RING_PROMISC;
532 
533  /* if we have a recent kernel, we need to use parsed_pkt to get VLAN info */
534  if (ptv->vlan_in_ext_header) {
535  opflag |= PF_RING_LONG_HEADER;
536  }
537 
539  if (strncmp(ptv->interface, "dna", 3) == 0) {
541  "Can't use rxonly checksum-checks on DNA interface,"
542  " resetting to auto");
544  } else {
545  opflag |= PF_RING_LONG_HEADER;
546  }
547  }
548 
549 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
550  if (pfconf->flags & PFRING_CONF_FLAGS_BYPASS) {
551  opflag |= PF_RING_FLOW_OFFLOAD | PF_RING_FLOW_OFFLOAD_NOUPDATES;
552  ptv->flags |= PFRING_FLAGS_BYPASS;
553  }
554 #endif
555 
556  ptv->pd = pfring_open(ptv->interface, (uint32_t)default_packet_size, opflag);
557  if (ptv->pd == NULL) {
558  SCLogError(SC_ERR_PF_RING_OPEN,"Failed to open %s: pfring_open error."
559  " Check if %s exists and pf_ring module is loaded.",
560  ptv->interface,
561  ptv->interface);
562  pfconf->DerefFunc(pfconf);
563  SCFree(ptv);
564  return TM_ECODE_FAILED;
565  }
566 
567  pfring_set_application_name(ptv->pd, (char *)PROG_NAME);
568  pfring_version(ptv->pd, &version);
569 
570  /* We only set cluster info if the number of pfring threads is greater than 1 */
571  ptv->threads = pfconf->threads;
572 
573  ptv->cluster_id = pfconf->cluster_id;
574 
575  if ((ptv->threads == 1) && (strncmp(ptv->interface, "dna", 3) == 0)) {
576  SCLogInfo("DNA interface detected, not adding thread to cluster");
577  } else if (strncmp(ptv->interface, "zc", 2) == 0) {
578  SCLogInfo("ZC interface detected, not adding thread to cluster");
579  } else {
580  ptv->ctype = (cluster_type)pfconf->ctype;
581  rc = pfring_set_cluster(ptv->pd, ptv->cluster_id, ptv->ctype);
582 
583  if (rc != 0) {
584  SCLogError(SC_ERR_PF_RING_SET_CLUSTER_FAILED, "pfring_set_cluster "
585  "returned %d for cluster-id: %d", rc, ptv->cluster_id);
586  if (rc != PF_RING_ERROR_NOT_SUPPORTED || (pfconf->flags & PFRING_CONF_FLAGS_CLUSTER)) {
587  /* cluster is mandatory as explicitly specified in the configuration */
588  pfconf->DerefFunc(pfconf);
589  return TM_ECODE_FAILED;
590  }
591  }
592  }
593 
594  if (ptv->threads > 1) {
595  SCLogPerf("(%s) Using PF_RING v.%d.%d.%d, interface %s, cluster-id %d",
596  tv->name, (version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8,
597  version & 0x000000FF, ptv->interface, ptv->cluster_id);
598  } else {
599  SCLogPerf("(%s) Using PF_RING v.%d.%d.%d, interface %s, cluster-id %d, single-pfring-thread",
600  tv->name, (version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8,
601  version & 0x000000FF, ptv->interface, ptv->cluster_id);
602  }
603 
604  if (pfconf->bpf_filter) {
605  ptv->bpf_filter = SCStrdup(pfconf->bpf_filter);
606  if (unlikely(ptv->bpf_filter == NULL)) {
607  SCLogError(SC_ERR_MEM_ALLOC, "Set PF_RING bpf filter failed.");
608  } else {
609  SCMutexLock(&pfring_bpf_set_filter_lock);
610  rc = pfring_set_bpf_filter(ptv->pd, ptv->bpf_filter);
611  SCMutexUnlock(&pfring_bpf_set_filter_lock);
612 
613  if (rc < 0) {
614  SCLogError(SC_ERR_INVALID_VALUE, "Failed to compile BPF \"%s\"",
615  ptv->bpf_filter);
616  return TM_ECODE_FAILED;
617  }
618  }
619  }
620 
621  ptv->capture_kernel_packets = StatsRegisterCounter("capture.kernel_packets",
622  ptv->tv);
623  ptv->capture_kernel_drops = StatsRegisterCounter("capture.kernel_drops",
624  ptv->tv);
625 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
626  ptv->capture_bypassed = StatsRegisterCounter("capture.bypassed",
627  ptv->tv);
628 #endif
629 
630  /* If kernel is older than 3.0, VLAN is not stripped so we don't
631  * get the info from packt extended header but we will use a standard
632  * parsing */
633  ptv->vlan_in_ext_header = 1;
634  if (! SCKernelVersionIsAtLeast(3, 0)) {
635  ptv->vlan_in_ext_header = 0;
636  }
637 
638  /* If VLAN tags are not in the extended header, set cluster type to 5-tuple
639  * or in case of a ZC interface, do nothing */
640  if ((! ptv->vlan_in_ext_header) && ptv->ctype == CLUSTER_FLOW &&
641  strncmp(ptv->interface, "zc", 2) != 0) {
642  SCLogPerf("VLAN not in extended header, setting cluster type to CLUSTER_FLOW_5_TUPLE");
643  rc = pfring_set_cluster(ptv->pd, ptv->cluster_id, CLUSTER_FLOW_5_TUPLE);
644 
645  if (rc != 0) {
646  SCLogError(SC_ERR_PF_RING_SET_CLUSTER_FAILED, "pfring_set_cluster "
647  "returned %d for cluster-id: %d", rc, ptv->cluster_id);
648  pfconf->DerefFunc(pfconf);
649  return TM_ECODE_FAILED;
650  }
651  }
652 
653  *data = (void *)ptv;
654  pfconf->DerefFunc(pfconf);
655 
656  return TM_ECODE_OK;
657 }
658 
659 /**
660  * \brief This function prints stats to the screen at exit.
661  * \param tv pointer to ThreadVars
662  * \param data pointer that gets cast into PfringThreadVars for ptv
663  */
665 {
666  PfringThreadVars *ptv = (PfringThreadVars *)data;
667 
668  PfringDumpCounters(ptv);
669  SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", dropped %" PRIu64 "",
670  tv->name,
673  SCLogPerf("(%s) Packets %" PRIu64 ", bytes %" PRIu64 "", tv->name, ptv->pkts, ptv->bytes);
674 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
675  if (ptv->flags & PFRING_FLAGS_BYPASS) {
676  SCLogPerf("(%s) Bypass: Packets %" PRIu64 "",
677  tv->name,
679  }
680 #endif
681 }
682 
683 /**
684  * \brief DeInit function closes pd at exit.
685  * \param tv pointer to ThreadVars
686  * \param data pointer that gets cast into PfringThreadVars for ptvi
687  * \retval TM_ECODE_OK is always returned
688  */
690 {
691  PfringThreadVars *ptv = (PfringThreadVars *)data;
692  if (ptv->interface)
693  SCFree(ptv->interface);
694  pfring_remove_from_cluster(ptv->pd);
695 
696  if (ptv->bpf_filter) {
697  pfring_remove_bpf_filter(ptv->pd);
698  SCFree(ptv->bpf_filter);
699  }
700 
701  pfring_close(ptv->pd);
702  return TM_ECODE_OK;
703 }
704 
705 /**
706  * \brief This function passes off to link type decoders.
707  *
708  * DecodePfring decodes raw packets from PF_RING. Inside of libpcap version of
709  * PF_RING all packets are marked as a link type of ethernet so that is what we do here.
710  *
711  * \param tv pointer to ThreadVars
712  * \param p pointer to the current packet
713  * \param data pointer that gets cast into PfringThreadVars for ptv
714  *
715  * \todo Verify that PF_RING only deals with ethernet traffic
716  *
717  * \warning This function bypasses the pkt buf and len macro's
718  *
719  * \retval TM_ECODE_OK is always returned
720  */
722 {
724 
726 
727  /* update counters */
729 
730  /* If suri has set vlan during reading, we increase vlan counter */
731  if (p->vlan_idx) {
733  }
734 
736 
738 
739  return TM_ECODE_OK;
740 }
741 
742 /**
743  * \brief This an Init function for DecodePfring
744  *
745  * \param tv pointer to ThreadVars
746  * \param initdata pointer to initilization data.
747  * \param data pointer that gets cast into PfringThreadVars for ptv
748  * \retval TM_ECODE_OK is returned on success
749  * \retval TM_ECODE_FAILED is returned on error
750  */
751 TmEcode DecodePfringThreadInit(ThreadVars *tv, const void *initdata, void **data)
752 {
753  DecodeThreadVars *dtv = NULL;
754 
756  if (dtv == NULL)
758 
760 
761  *data = (void *)dtv;
762 
763  return TM_ECODE_OK;
764 }
765 
767 {
768  if (data != NULL)
769  DecodeThreadVarsFree(tv, data);
771 }
772 
773 #endif /* HAVE_PFRING */
774 /* eof */
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:67
PKT_IS_UDP
#define PKT_IS_UDP(p)
Definition: decode.h:255
tm-threads.h
ReceivePfringThreadInit
TmEcode ReceivePfringThreadInit(ThreadVars *, const void *, void **)
Init function for RecievePfring.
Definition: source-pfring.c:488
PfringIfaceConfig_
Definition: source-pfring.h:37
SCStrdup
#define SCStrdup(a)
Definition: util-mem.h:268
max_pending_packets
int max_pending_packets
Definition: suricata.c:212
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:168
ThreadVars_::name
char name[16]
Definition: threadvars.h:64
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:436
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1115
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
SCFree
#define SCFree(a)
Definition: util-mem.h:322
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:347
ReceivePfringThreadExitStats
void ReceivePfringThreadExitStats(ThreadVars *, void *)
This function prints stats to the screen at exit.
Definition: source-pfring.c:664
PfringBreakLoop
TmEcode PfringBreakLoop(ThreadVars *tv, void *data)
Stop function for ReceivePfringLoop.
Definition: source-pfring.c:460
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
DecodePfringThreadDeinit
TmEcode DecodePfringThreadDeinit(ThreadVars *tv, void *data)
Definition: source-pfring.c:766
TMM_DECODEPFRING
@ TMM_DECODEPFRING
Definition: tm-threads-common.h:43
util-checksum.h
Packet_::flags
uint32_t flags
Definition: decode.h:444
threads.h
TMM_RECEIVEPFRING
@ TMM_RECEIVEPFRING
Definition: tm-threads-common.h:42
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:437
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:107
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:190
PfringThreadVars_::ctype
cluster_type ctype
Definition: source-pfring.c:146
packet-queue.h
CHECKSUM_VALIDATION_RXONLY
@ CHECKSUM_VALIDATION_RXONLY
Definition: decode.h:44
SURICATA_STOP
#define SURICATA_STOP
Definition: suricata.h:95
SCKernelVersionIsAtLeast
int SCKernelVersionIsAtLeast(int major, int minor)
Definition: util-host-info.c:36
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:41
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:449
Packet_::BypassPacketsFlow
int(* BypassPacketsFlow)(struct Packet_ *)
Definition: decode.h:488
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:228
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:575
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1118
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:254
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:477
PfringIfaceConfig_::iface
char iface[PFRING_IFACE_NAME_LENGTH]
Definition: source-pfring.h:45
CHECKSUM_VALIDATION_AUTO
@ CHECKSUM_VALIDATION_AUTO
Definition: decode.h:43
SET_PKT_LEN
#define SET_PKT_LEN(p, len)
Definition: decode.h:230
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:49
LiveDevice_::ignore_checksum
int ignore_checksum
Definition: util-device.h:45
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:279
SCEnter
#define SCEnter(...)
Definition: util-debug.h:337
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:226
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
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
SCMalloc
#define SCMalloc(a)
Definition: util-mem.h:222
source-pfring.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:265
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:148
DecodePfringThreadInit
TmEcode DecodePfringThreadInit(ThreadVars *, const void *, void **)
This an Init function for DecodePfring.
Definition: source-pfring.c:751
Packet_
Definition: decode.h:408
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:225
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:1086
Packet_::livedev
struct LiveDevice_ * livedev
Definition: decode.h:554
TmEcode
TmEcode
Definition: tm-threads-common.h:77
IS_TUNNEL_PKT
#define IS_TUNNEL_PKT(p)
Definition: decode.h:871
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:649
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
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:689
PfringThreadVars_
Structure to hold thread specific variables.
Definition: source-pfring.c:123
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
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:636
Packet_::ts
struct timeval ts
Definition: decode.h:452
ChecksumValidationMode
ChecksumValidationMode
Definition: decode.h:40
suricata-common.h
CLUSTER_FLOW_5_TUPLE
#define CLUSTER_FLOW_5_TUPLE
Definition: source-pfring.h:77
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:261
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
version
uint8_t version
Definition: decode-gre.h:3
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
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
PfringThreadVars_::flags
uint32_t flags
Definition: source-pfring.c:136
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:48
threadvars.h
PfringIfaceConfig_::threads
int threads
Definition: source-pfring.h:47
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:622
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:617
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:656
GET_PKT_DIRECT_DATA
#define GET_PKT_DIRECT_DATA(p)
Definition: decode.h:227
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:192
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:341
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:721
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:583
LINKTYPE_ETHERNET
#define LINKTYPE_ETHERNET
Definition: decode.h:1057
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:198
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