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  */
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) {
422  TmqhOutputPacketpool(ptv->tv, p);
424  }
425 
426  /* Trigger one dump of stats every second */
427  if (p->ts.tv_sec != last_dump) {
428  PfringDumpCounters(ptv);
429  last_dump = p->ts.tv_sec;
430  }
431  } else if (unlikely(r == 0)) {
432  if (suricata_ctl_flags & SURICATA_STOP) {
434  }
435 
436  /* pfring didn't use the packet yet */
437  TmThreadsCaptureHandleTimeout(tv, ptv->slot, p);
438 
439  } else {
440  SCLogError(SC_ERR_PF_RING_RECV,"pfring_recv error %" PRId32 "", r);
441  TmqhOutputPacketpool(ptv->tv, p);
443  }
445  }
446 
447  return TM_ECODE_OK;
448 }
449 
450 /**
451  * \brief Stop function for ReceivePfringLoop.
452  *
453  * This function forces ReceivePfringLoop to stop the
454  * execution, exiting the packet capture loop.
455  *
456  * \param tv pointer to ThreadVars
457  * \param data pointer that gets cast into PfringThreadVars for ptv
458  * \retval TM_ECODE_OK on success
459  * \retval TM_ECODE_FAILED on failure
460  */
462 {
463  PfringThreadVars *ptv = (PfringThreadVars *)data;
464 
465  /* Safety check */
466  if (ptv->pd == NULL) {
467  return TM_ECODE_FAILED;
468  }
469 
470  pfring_breakloop(ptv->pd);
471 
472  return TM_ECODE_OK;
473 }
474 
475 /**
476  * \brief Init function for RecievePfring.
477  *
478  * This is a setup function for recieving packets
479  * via libpfring.
480  *
481  * \param tv pointer to ThreadVars
482  * \param initdata pointer to the interface passed from the user
483  * \param data pointer gets populated with PfringThreadVars
484  * \todo add a config option for setting cluster id
485  * \todo Create a general pfring setup function.
486  * \retval TM_ECODE_OK on success
487  * \retval TM_ECODE_FAILED on error
488  */
489 TmEcode ReceivePfringThreadInit(ThreadVars *tv, const void *initdata, void **data)
490 {
491  int rc;
492  u_int32_t version = 0;
493  PfringIfaceConfig *pfconf = (PfringIfaceConfig *) initdata;
494  unsigned int opflag;
495  char const *active_runmode = RunmodeGetActive();
496 
497  if (pfconf == NULL)
498  return TM_ECODE_FAILED;
499 
501  if (unlikely(ptv == NULL)) {
502  pfconf->DerefFunc(pfconf);
503  return TM_ECODE_FAILED;
504  }
505  memset(ptv, 0, sizeof(PfringThreadVars));
506 
507  ptv->tv = tv;
508  ptv->threads = 1;
509 
510  ptv->interface = SCStrdup(pfconf->iface);
511  if (unlikely(ptv->interface == NULL)) {
512  SCLogError(SC_ERR_MEM_ALLOC, "Unable to allocate device string");
513  SCFree(ptv);
515  }
516 
517  ptv->livedev = LiveGetDevice(pfconf->iface);
518  if (ptv->livedev == NULL) {
519  SCLogError(SC_ERR_INVALID_VALUE, "Unable to find Live device");
520  SCFree(ptv);
522  }
523 
524  /* enable zero-copy mode for workers runmode */
525  if (active_runmode && strcmp("workers", active_runmode) == 0) {
527  SCLogPerf("Enabling zero-copy for %s", ptv->interface);
528  }
529 
530  ptv->checksum_mode = pfconf->checksum_mode;
531 
532  opflag = PF_RING_PROMISC;
533 
534  /* if we have a recent kernel, we need to use parsed_pkt to get VLAN info */
535  if (ptv->vlan_in_ext_header) {
536  opflag |= PF_RING_LONG_HEADER;
537  }
538 
540  if (strncmp(ptv->interface, "dna", 3) == 0) {
542  "Can't use rxonly checksum-checks on DNA interface,"
543  " resetting to auto");
545  } else {
546  opflag |= PF_RING_LONG_HEADER;
547  }
548  }
549 
550 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
551  if (pfconf->flags & PFRING_CONF_FLAGS_BYPASS) {
552  opflag |= PF_RING_FLOW_OFFLOAD | PF_RING_FLOW_OFFLOAD_NOUPDATES;
553  ptv->flags |= PFRING_FLAGS_BYPASS;
554  }
555 #endif
556 
557  ptv->pd = pfring_open(ptv->interface, (uint32_t)default_packet_size, opflag);
558  if (ptv->pd == NULL) {
559  SCLogError(SC_ERR_PF_RING_OPEN,"Failed to open %s: pfring_open error."
560  " Check if %s exists and pf_ring module is loaded.",
561  ptv->interface,
562  ptv->interface);
563  pfconf->DerefFunc(pfconf);
564  SCFree(ptv);
565  return TM_ECODE_FAILED;
566  }
567 
568  pfring_set_application_name(ptv->pd, (char *)PROG_NAME);
569  pfring_version(ptv->pd, &version);
570 
571  /* We only set cluster info if the number of pfring threads is greater than 1 */
572  ptv->threads = pfconf->threads;
573 
574  ptv->cluster_id = pfconf->cluster_id;
575 
576  if ((ptv->threads == 1) && (strncmp(ptv->interface, "dna", 3) == 0)) {
577  SCLogInfo("DNA interface detected, not adding thread to cluster");
578  } else if (strncmp(ptv->interface, "zc", 2) == 0) {
579  SCLogInfo("ZC interface detected, not adding thread to cluster");
580  } else {
581  ptv->ctype = (cluster_type)pfconf->ctype;
582  rc = pfring_set_cluster(ptv->pd, ptv->cluster_id, ptv->ctype);
583 
584  if (rc != 0) {
585  SCLogError(SC_ERR_PF_RING_SET_CLUSTER_FAILED, "pfring_set_cluster "
586  "returned %d for cluster-id: %d", rc, ptv->cluster_id);
587  if (rc != PF_RING_ERROR_NOT_SUPPORTED || (pfconf->flags & PFRING_CONF_FLAGS_CLUSTER)) {
588  /* cluster is mandatory as explicitly specified in the configuration */
589  pfconf->DerefFunc(pfconf);
590  return TM_ECODE_FAILED;
591  }
592  }
593  }
594 
595  if (ptv->threads > 1) {
596  SCLogPerf("(%s) Using PF_RING v.%d.%d.%d, interface %s, cluster-id %d",
597  tv->name, (version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8,
598  version & 0x000000FF, ptv->interface, ptv->cluster_id);
599  } else {
600  SCLogPerf("(%s) Using PF_RING v.%d.%d.%d, interface %s, cluster-id %d, single-pfring-thread",
601  tv->name, (version & 0xFFFF0000) >> 16, (version & 0x0000FF00) >> 8,
602  version & 0x000000FF, ptv->interface, ptv->cluster_id);
603  }
604 
605  if (pfconf->bpf_filter) {
606  ptv->bpf_filter = SCStrdup(pfconf->bpf_filter);
607  if (unlikely(ptv->bpf_filter == NULL)) {
608  SCLogError(SC_ERR_MEM_ALLOC, "Set PF_RING bpf filter failed.");
609  } else {
610  SCMutexLock(&pfring_bpf_set_filter_lock);
611  rc = pfring_set_bpf_filter(ptv->pd, ptv->bpf_filter);
612  SCMutexUnlock(&pfring_bpf_set_filter_lock);
613 
614  if (rc < 0) {
615  SCLogError(SC_ERR_INVALID_VALUE, "Failed to compile BPF \"%s\"",
616  ptv->bpf_filter);
617  return TM_ECODE_FAILED;
618  }
619  }
620  }
621 
622  ptv->capture_kernel_packets = StatsRegisterCounter("capture.kernel_packets",
623  ptv->tv);
624  ptv->capture_kernel_drops = StatsRegisterCounter("capture.kernel_drops",
625  ptv->tv);
626 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
627  ptv->capture_bypassed = StatsRegisterCounter("capture.bypassed",
628  ptv->tv);
629 #endif
630 
631  /* If kernel is older than 3.0, VLAN is not stripped so we don't
632  * get the info from packt extended header but we will use a standard
633  * parsing */
634  ptv->vlan_in_ext_header = 1;
635  if (! SCKernelVersionIsAtLeast(3, 0)) {
636  ptv->vlan_in_ext_header = 0;
637  }
638 
639  /* If VLAN tags are not in the extended header, set cluster type to 5-tuple
640  * or in case of a ZC interface, do nothing */
641  if ((! ptv->vlan_in_ext_header) && ptv->ctype == CLUSTER_FLOW &&
642  strncmp(ptv->interface, "zc", 2) != 0) {
643  SCLogPerf("VLAN not in extended header, setting cluster type to CLUSTER_FLOW_5_TUPLE");
644  rc = pfring_set_cluster(ptv->pd, ptv->cluster_id, CLUSTER_FLOW_5_TUPLE);
645 
646  if (rc != 0) {
647  SCLogError(SC_ERR_PF_RING_SET_CLUSTER_FAILED, "pfring_set_cluster "
648  "returned %d for cluster-id: %d", rc, ptv->cluster_id);
649  pfconf->DerefFunc(pfconf);
650  return TM_ECODE_FAILED;
651  }
652  }
653 
654  *data = (void *)ptv;
655  pfconf->DerefFunc(pfconf);
656 
657  return TM_ECODE_OK;
658 }
659 
660 /**
661  * \brief This function prints stats to the screen at exit.
662  * \param tv pointer to ThreadVars
663  * \param data pointer that gets cast into PfringThreadVars for ptv
664  */
666 {
667  PfringThreadVars *ptv = (PfringThreadVars *)data;
668 
669  PfringDumpCounters(ptv);
670  SCLogPerf("(%s) Kernel: Packets %" PRIu64 ", dropped %" PRIu64 "",
671  tv->name,
674  SCLogPerf("(%s) Packets %" PRIu64 ", bytes %" PRIu64 "", tv->name, ptv->pkts, ptv->bytes);
675 #ifdef HAVE_PF_RING_FLOW_OFFLOAD
676  if (ptv->flags & PFRING_FLAGS_BYPASS) {
677  SCLogPerf("(%s) Bypass: Packets %" PRIu64 "",
678  tv->name,
680  }
681 #endif
682 }
683 
684 /**
685  * \brief DeInit function closes pd at exit.
686  * \param tv pointer to ThreadVars
687  * \param data pointer that gets cast into PfringThreadVars for ptvi
688  * \retval TM_ECODE_OK is always returned
689  */
691 {
692  PfringThreadVars *ptv = (PfringThreadVars *)data;
693  if (ptv->interface)
694  SCFree(ptv->interface);
695  pfring_remove_from_cluster(ptv->pd);
696 
697  if (ptv->bpf_filter) {
698  pfring_remove_bpf_filter(ptv->pd);
699  SCFree(ptv->bpf_filter);
700  }
701 
702  pfring_close(ptv->pd);
703  return TM_ECODE_OK;
704 }
705 
706 /**
707  * \brief This function passes off to link type decoders.
708  *
709  * DecodePfring reads packets from the PacketQueue. Inside of libpcap version of
710  * PF_RING all packets are marked as a link type of ethernet so that is what we do here.
711  *
712  * \param tv pointer to ThreadVars
713  * \param p pointer to the current packet
714  * \param data pointer that gets cast into PfringThreadVars for ptv
715  * \param pq pointer to the current PacketQueue
716  *
717  * \todo Verify that PF_RING only deals with ethernet traffic
718  *
719  * \warning This function bypasses the pkt buf and len macro's
720  *
721  * \retval TM_ECODE_OK is always returned
722  */
724 {
725  DecodeThreadVars *dtv = (DecodeThreadVars *)data;
726 
727  /* XXX HACK: flow timeout can call us for injected pseudo packets
728  * see bug: https://redmine.openinfosecfoundation.org/issues/1107 */
729  if (p->flags & PKT_PSEUDO_STREAM_END)
730  return TM_ECODE_OK;
731 
732  /* update counters */
733  DecodeUpdatePacketCounters(tv, dtv, p);
734 
735  /* If suri has set vlan during reading, we increase vlan counter */
736  if (p->vlan_idx) {
737  StatsIncr(tv, dtv->counter_vlan);
738  }
739 
740  DecodeEthernet(tv, dtv, p, GET_PKT_DATA(p), GET_PKT_LEN(p), pq);
741 
742  PacketDecodeFinalize(tv, dtv, p);
743 
744  return TM_ECODE_OK;
745 }
746 
747 /**
748  * \brief This an Init function for DecodePfring
749  *
750  * \param tv pointer to ThreadVars
751  * \param initdata pointer to initilization data.
752  * \param data pointer that gets cast into PfringThreadVars for ptv
753  * \retval TM_ECODE_OK is returned on success
754  * \retval TM_ECODE_FAILED is returned on error
755  */
756 TmEcode DecodePfringThreadInit(ThreadVars *tv, const void *initdata, void **data)
757 {
758  DecodeThreadVars *dtv = NULL;
759 
760  dtv = DecodeThreadVarsAlloc(tv);
761  if (dtv == NULL)
763 
765 
766  *data = (void *)dtv;
767 
768  return TM_ECODE_OK;
769 }
770 
772 {
773  if (data != NULL)
774  DecodeThreadVarsFree(tv, data);
776 }
777 
778 #endif /* HAVE_PFRING */
779 /* eof */
#define PFRING_CONF_FLAGS_BYPASS
Definition: source-pfring.h:35
ChecksumValidationMode checksum_mode
TmEcode ReceivePfringLoop(ThreadVars *tv, void *data, void *slot)
Recieves packets from an interface via libpfring.
#define SCMutex
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:32
#define GET_PKT_DIRECT_MAX_SIZE(p)
Definition: decode.h:227
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:605
#define SCLogDebug(...)
Definition: util-debug.h:335
#define PROG_NAME
Definition: suricata.h:71
cluster_type ctype
#define SC_CAP_NET_BROADCAST
Definition: util-privs.h:34
int(* BypassPacketsFlow)(struct Packet_ *)
Definition: decode.h:487
TmEcode PfringBreakLoop(ThreadVars *tv, void *data)
Stop function for ReceivePfringLoop.
unsigned int ctype
Definition: source-pfring.h:43
uint8_t cap_flags
Definition: tm-modules.h:67
uint8_t flags
Definition: tm-modules.h:70
#define SET_PKT_LEN(p, len)
Definition: decode.h:229
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:115
#define unlikely(expr)
Definition: util-optimize.h:35
TmEcode(* Func)(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *)
Definition: tm-modules.h:52
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:465
#define PACKET_PROFILING_RESTART(p)
TmEcode ReceivePfringThreadInit(ThreadVars *, const void *, void **)
Init function for RecievePfring.
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:107
TmEcode DecodePfringThreadInit(ThreadVars *, const void *, void **)
This an Init function for DecodePfring.
ThreadVars * tv
#define LIBPFRING_WAIT_FOR_INCOMING
#define IS_TUNNEL_PKT(p)
Definition: decode.h:881
LiveDevice * livedev
TmEcode DecodePfringThreadDeinit(ThreadVars *tv, void *data)
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:201
#define SC_CAP_NET_ADMIN
Definition: util-privs.h:31
#define SC_CAP_NET_BIND_SERVICE
Definition: util-privs.h:33
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:177
#define CLUSTER_FLOW
Definition: source-pfring.h:75
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:54
#define SCMutexLock(mut)
TmEcode ReceivePfringThreadDeinit(ThreadVars *, void *)
DeInit function closes pd at exit.
char * RunmodeGetActive(void)
Definition: runmodes.c:187
uint16_t vlan_id[2]
Definition: decode.h:435
uint16_t counter_vlan
Definition: decode.h:659
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1132
int SCKernelVersionIsAtLeast(int major, int minor)
#define TM_FLAG_RECEIVE_TM
Definition: tm-modules.h:31
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
TmEcode(* PktAcqBreakLoop)(ThreadVars *, void *)
Definition: tm-modules.h:57
uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
Registers a normal, unqualified counter.
Definition: counters.c:931
void(* DerefFunc)(void *)
Definition: source-pfring.h:53
#define SURICATA_STOP
Definition: suricata.h:95
#define SCMutexUnlock(mut)
void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Sets a value of type double to the local counter.
Definition: counters.c:185
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
int max_pending_packets
Definition: suricata.c:215
int datalink
Definition: decode.h:574
#define SCMUTEX_INITIALIZER
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, uint32_t len, PacketQueue *pq)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
Structure to hold thread specific variables.
Structure to hold thread specific data for all decode modules.
Definition: decode.h:632
#define GET_PKT_DIRECT_DATA(p)
Definition: decode.h:226
void(* RegisterTests)(void)
Definition: tm-modules.h:65
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
#define SCEnter(...)
Definition: util-debug.h:337
struct TmSlot_ * slot_next
Definition: tm-threads.h:87
TmEcode DecodePfring(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *)
This function passes off to link type decoders.
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:163
#define PKT_IGNORE_CHECKSUM
Definition: decode.h:1099
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1091
#define PFRING_FLAGS_ZERO_COPY
#define CLUSTER_FLOW_5_TUPLE
Definition: source-pfring.h:77
#define SCReturnInt(x)
Definition: util-debug.h:341
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
Definition: util-device.c:236
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:48
#define PFRING_CONF_FLAGS_CLUSTER
Definition: source-pfring.h:34
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
uint8_t vlan_idx
Definition: decode.h:436
#define SC_CAP_NET_RAW
Definition: util-privs.h:32
void PacketPoolWait(void)
const char * name
Definition: tm-modules.h:44
int ignore_checksum
Definition: util-device.h:45
#define SCMalloc(a)
Definition: util-mem.h:222
ChecksumValidationMode checksum_mode
Definition: source-pfring.h:51
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
int PacketSetData(Packet *p, uint8_t *pktdata, uint32_t pktlen)
Set data for Packet and set length when zeo copy is used.
Definition: decode.c:644
uint8_t version
Definition: decode-gre.h:405
#define SCFree(a)
Definition: util-mem.h:322
#define PKT_IS_TCP(p)
Definition: decode.h:253
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.h:73
uint16_t capture_bypassed
uint32_t default_packet_size
Definition: decode.h:617
#define SCLogPerf(...)
Definition: util-debug.h:261
#define StatsSyncCountersIfSignalled(tv)
Definition: counters.h:136
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
char iface[PFRING_IFACE_NAME_LENGTH]
Definition: source-pfring.h:45
ChecksumValidationMode
Definition: decode.h:40
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:192
void ReceivePfringThreadExitStats(ThreadVars *, void *)
This function prints stats to the screen at exit.
#define GET_PKT_DATA(p)
Definition: decode.h:225
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:571
void TmModuleDecodePfringRegister(void)
Registration Function for DecodePfring.
#define SCStrdup(a)
Definition: util-mem.h:268
char name[16]
Definition: threadvars.h:59
#define LINKTYPE_ETHERNET
Definition: decode.h:1070
struct LiveDevice_ * livedev
Definition: decode.h:553
#define PKT_IS_UDP(p)
Definition: decode.h:254
Per thread variable structure.
Definition: threadvars.h:57
struct timeval ts
Definition: decode.h:451
#define GET_PKT_LEN(p)
Definition: decode.h:224
uint32_t flags
Definition: decode.h:443
#define likely(expr)
Definition: util-optimize.h:32
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:624
uint16_t capture_kernel_packets
uint16_t capture_kernel_drops
#define PFRING_FLAGS_BYPASS
void TmModuleReceivePfringRegister(void)
Registration Function for RecievePfring.
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:1232