source: lib/libtrace_int.h @ 8485d99

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 8485d99 was ee6e802, checked in by Shane Alcock <salcock@…>, 5 years ago

Updated copyright blurb on all source files

In some cases, this meant adding copyright blurbs to files that
had never had them before.

  • Property mode set to 100644
File size: 42.9 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26/** @file
27 *
28 * @brief Header file containing definitions for structures and functions that
29 * are internal
30 *
31 * @author Daniel Lawson
32 * @author Perry Lorier
33 * @author Shane Alcock
34 *
35 * @version $Id$
36 *
37 * All of the structures and functions defined in this header file are intended
38 * for internal use within Libtrace only. They should not be exported as part
39 * of the library API as we don't want users accessing things like the
40 * contents of the libtrace packet structure directly!
41 */
42#ifndef LIBTRACE_INT_H
43#define LIBTRACE_INT_H
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#include "config.h"
50#include "common.h"
51#include "libtrace_parallel.h"
52#include "wandio.h"
53#include "lt_bswap.h"
54
55#ifdef _MSC_VER
56// warning: deprecated function
57#pragma warning(disable:4996)
58// warning: benign redefinitions of types
59#pragma warning(disable:4142)
60#endif
61
62#ifdef HAVE_INTTYPES_H
63# include <inttypes.h>
64#else
65# include "lt_inttypes.h"
66#endif
67
68#ifdef HAVE_STDDEF_H
69# include <stddef.h>
70#else
71#ifndef WIN32
72# error "Can't find stddev.h -- do you define ptrdiff_t elsewhere?"
73#endif
74#endif
75
76
77#include "rt_protocol.h"
78       
79/* Prefer net/bpf.h over pcap-bpf.h for format_bpf.c on MacOS */
80#ifdef HAVE_NET_BPF_H
81#    include <net/bpf.h>
82#    define HAVE_BPF 1
83#else
84#ifdef HAVE_PCAP_BPF_H
85#  include <pcap-bpf.h>
86#  define HAVE_BPF 1
87#endif
88#endif
89
90#ifdef HAVE_PCAP_H
91#  include <pcap.h>
92#  ifdef HAVE_PCAP_INT_H
93#    include <pcap-int.h>
94#  endif
95#endif
96
97#ifdef HAVE_ZLIB_H
98#  include <zlib.h>
99#endif
100
101#ifndef HAVE_STRNDUP
102char *strndup(const char *s, size_t size);
103#endif
104
105#ifndef HAVE_STRNCASECMP
106# ifndef HAVE__STRNICMP
107/** A local implementation of strncasecmp (as some systems do not have it) */
108int strncasecmp(const char *str1, const char *str2, size_t n);
109# else
110# define strncasecmp _strnicmp
111# endif
112#endif
113
114#ifndef HAVE_SNPRINTF
115# ifndef HAVE_SPRINTF_S
116/** A local implementation of snprintf (as some systems do not have it) */
117int snprintf(char *str, size_t size, const char *format, ...);
118# else
119# define snprintf sprintf_s
120# endif
121#endif
122
123#include "daglegacy.h"
124       
125#ifdef HAVE_DAG_API
126#  include "dagnew.h"
127#  include "dagapi.h"
128#       if DAG_VERSION == 24
129#               include <erftypes.h>
130#       else
131#               include <daginf.h>
132#       endif
133#  include "erftypes.h"
134#else
135#  include "dagformat.h"
136#endif
137
138#ifdef HAVE_LLVM
139#include "bpf-jit/bpf-jit.h"
140#endif
141
142#include "data-struct/ring_buffer.h"
143#include "data-struct/object_cache.h"
144#include "data-struct/vector.h"
145#include "data-struct/message_queue.h"
146#include "data-struct/deque.h"
147#include "data-struct/linked_list.h"
148#include "data-struct/sliding_window.h"
149#include "data-struct/buckets.h"
150#include "pthread_spinlock.h"
151
152//#define RP_BUFSIZE 65536U
153
154/** Data about the most recent event from a trace file */
155struct libtrace_event_status_t {
156        /** A libtrace packet to store the packet when a PACKET event occurs */
157        libtrace_packet_t *packet;
158        /** Time between the timestamp for the current packet and the current
159         * walltime */
160        double tdelta;
161        /** The timestamp of the previous PACKET event */
162        double trace_last_ts;
163        /** The size of the current PACKET event */
164        int psize;
165        /** Whether there is a packet stored in *packet above waiting for an
166         * event to occur */
167        bool waiting;
168};
169
170enum thread_types {
171        THREAD_EMPTY,
172        THREAD_HASHER,
173        THREAD_PERPKT,
174        THREAD_REPORTER,
175        THREAD_KEEPALIVE
176};
177
178enum thread_states {
179        THREAD_RUNNING,
180        THREAD_FINISHING,
181        THREAD_FINISHED,
182        THREAD_PAUSED,
183        THREAD_STATE_MAX
184};
185
186/**
187 * Information of this thread
188 */
189struct libtrace_thread_t {
190        uint64_t accepted_packets; // The number of packets accepted only used if pread
191        uint64_t filtered_packets;
192        // is retreving packets
193        // Set to true once the first packet has been stored
194        bool recorded_first;
195        // For thread safety reason we actually must store this here
196        int64_t tracetime_offset_usec;
197        void* user_data; // TLS for the user to use
198        void* format_data; // TLS for the format to use
199        libtrace_message_queue_t messages; // Message handling
200        libtrace_ringbuffer_t rbuffer; // Input
201        libtrace_t * trace;
202        void* ret;
203        enum thread_types type;
204        enum thread_states state;
205        pthread_t tid;
206        int perpkt_num; // A number from 0-X that represents this perpkt threads number
207                                // in the table, intended to quickly identify this thread
208                                // -1 represents NA (such as the case this is not a perpkt thread)
209} ALIGN_STRUCT(CACHE_LINE_SIZE);
210
211/**
212 * Storage to note time value against each.
213 * Used both internally to do trace time playback
214 * and can be used externally to assist applications which need
215 * a trace starting time such as tracertstats.
216 */
217struct first_packets {
218        pthread_spinlock_t lock;
219        size_t count; // If == perpkt_thread_count threads we have all
220        size_t first; // Valid if count != 0
221        struct {
222                libtrace_packet_t * packet;
223                struct timeval tv;
224        } * packets;
225};
226
227#define TRACE_STATES \
228        X(STATE_NEW) \
229        X(STATE_RUNNING) \
230        X(STATE_PAUSING) \
231        X(STATE_PAUSED) \
232        X(STATE_FINISHED) \
233        X(STATE_FINISHING) \
234        X(STATE_DESTROYED) \
235        X(STATE_JOINED) \
236        X(STATE_ERROR)
237
238#define X(a) a,
239enum trace_state {
240        TRACE_STATES
241};
242#undef X
243
244#define X(a) case a: return #a;
245static inline char *get_trace_state_name(enum trace_state ts){
246        switch(ts) {
247                TRACE_STATES
248                default:
249                        return "UNKNOWN";
250        }
251}
252#undef X
253
254#define READ_EOF 0
255#define READ_ERROR -1
256#define READ_MESSAGE -2
257// Used for inband tick message
258#define READ_TICK -3
259
260/**
261 * Tuning the parallel sizes
262 * See the user documentation trace_set_x
263 */
264struct user_configuration {
265        size_t cache_size;
266        size_t thread_cache_size;
267        bool fixed_count;
268        size_t burst_size;
269        size_t tick_interval;
270        size_t tick_count;
271        size_t perpkt_threads;
272        size_t hasher_queue_size;
273        bool hasher_polling;
274        bool reporter_polling;
275        size_t reporter_thold;
276        bool debug_state;
277};
278#define ZERO_USER_CONFIG(config) memset(&config, 0, sizeof(struct user_configuration));
279
280struct callback_set {
281
282        fn_cb_starting message_starting;
283        fn_cb_dataless message_stopping;
284        fn_cb_dataless message_resuming;
285        fn_cb_dataless message_pausing;
286        fn_cb_packet message_packet;
287        fn_cb_result message_result;
288        fn_cb_first_packet message_first_packet;
289        fn_cb_tick message_tick_count;
290        fn_cb_tick message_tick_interval;
291        fn_cb_usermessage message_user;
292};
293
294/** A libtrace input trace
295 * @internal
296 */
297struct libtrace_t {
298        /** The capture format for the input trace */
299        struct libtrace_format_t *format; 
300        /** Details of the most recent PACKET event reported by the trace */
301        struct libtrace_event_status_t event;
302        /** Pointer to the "global" data for the capture format module */       
303        void *format_data;             
304        /** A BPF filter to be applied to all packets read by the trace -
305         * used only if the capture format does not support filters natively */
306        struct libtrace_filter_t *filter; 
307        /** The snap length to be applied to all packets read by the trace -
308         * used only if the capture format does not support snapping natively */
309        size_t snaplen;                 
310        /** Count of the number of packets returned to the libtrace user */
311        uint64_t accepted_packets;
312        /** Count of the number of packets filtered by libtrace */
313        uint64_t filtered_packets;
314        /** The sequence is like accepted_packets but we don't reset this after a pause. */
315        uint64_t sequence_number;
316        /** The packet read out by the trace, backwards compatibility to allow us to finalise
317         * a packet when the trace is destroyed */
318        libtrace_packet_t *last_packet;
319        /** The filename from the uri for the trace */
320        char *uridata;
321        /** The libtrace IO reader for this trace (if applicable) */
322        io_t *io;
323        /** Error information for the trace */
324        libtrace_err_t err;
325        /** Boolean flag indicating whether the trace has been started */
326        bool started;
327        /** Synchronise writes/reads across this format object and attached threads etc */
328        pthread_mutex_t libtrace_lock;
329        /** State */
330        enum trace_state state;
331        /** Use to control pausing threads and finishing threads etc always used with libtrace_lock */
332        pthread_cond_t perpkt_cond;
333        /** Keeps track of counts of threads in any given state */
334        int perpkt_thread_states[THREAD_STATE_MAX]; 
335
336        /** Set to indicate a perpkt's queue is full as such the writing perpkt cannot proceed */
337        bool perpkt_queue_full;
338        /** Global storage for this trace, shared among all the threads  */
339        void* global_blob;
340        /** The actual freelist */
341        libtrace_ocache_t packet_freelist;
342        /** The hasher function */
343        enum hasher_types hasher_type;
344        /** The hasher function - NULL implies they don't care or balance */
345        fn_hasher hasher;
346        void *hasher_data;
347        /** The pread_packet choosen path for the configuration */
348        int (*pread)(libtrace_t *, libtrace_thread_t *, libtrace_packet_t **, size_t);
349
350        libtrace_thread_t hasher_thread;
351        libtrace_thread_t reporter_thread;
352        libtrace_thread_t keepalive_thread;
353        int perpkt_thread_count;
354        libtrace_thread_t * perpkt_threads; // All our perpkt threads
355        // Used to keep track of the first packet seen on each thread
356        struct first_packets first_packets;
357        int tracetime;
358
359        /*
360         * Caches statistic counters in the case that our trace is
361         * paused or stopped before this counter is taken
362         */
363        libtrace_stat_t *stats;
364        struct user_configuration config;
365        libtrace_combine_t combiner;
366       
367        /* Set of callbacks to be executed by per packet threads in response
368         * to various messages. */
369        struct callback_set *perpkt_cbs;
370        /* Set of callbacks to be executed by the reporter thread in response
371         * to various messages. */
372        struct callback_set *reporter_cbs;
373};
374
375#define LIBTRACE_STAT_MAGIC 0x41
376
377void trace_fin_packet(libtrace_packet_t *packet);
378void libtrace_zero_thread(libtrace_thread_t * t);
379void store_first_packet(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t);
380libtrace_thread_t * get_thread_table(libtrace_t *libtrace);
381
382
383void send_message(libtrace_t *trace, libtrace_thread_t *target,
384                const enum libtrace_messages type,
385                libtrace_generic_t data, libtrace_thread_t *sender);
386
387/** A libtrace output trace
388 * @internal
389 */
390struct libtrace_out_t {
391        /** The capture format for the output trace */
392        struct libtrace_format_t *format;
393        /** Pointer to the "global" data for the capture format module */
394        void *format_data;             
395        /** The filename for the uri for the output trace */
396        char *uridata;                 
397        /** Error information for the output trace */
398        libtrace_err_t err;
399        /** Boolean flag indicating whether the trace has been started */
400        bool started;
401};
402
403/** Sets the error status on an input trace
404 *
405 * @param trace         The input trace to set the error status for
406 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
407 * @param msg           A message to print when reporting the error
408 */
409void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...) 
410
411                                                                PRINTF(3,4);
412/** Sets the error status on an output trace
413 *
414 * @param trace         The output trace to set the error status for
415 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
416 * @param msg           A message to print when reporting the error
417 */
418void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
419                                                                PRINTF(3,4);
420
421/** Clears the cached values for a libtrace packet
422 *
423 * @param packet        The libtrace packet that requires a cache reset
424 */
425void trace_clear_cache(libtrace_packet_t *packet);
426
427/** Converts the data provided in buffer into a valid libtrace packet
428 *
429 * @param trace         An input trace of the same format as the "packet"
430 *                      contained in the buffer
431 * @param packet        The libtrace packet to prepare
432 * @param buffer        A buffer containing the packet data, including the
433 *                      capture format header
434 * @param rt_type       The RT type for the packet that is being prepared
435 * @param flags         Used to specify options for the preparation function,
436 *                      e.g. who owns the packet buffer
437 *
438 * @return -1 if an error occurs, 0 otherwise
439 *
440 * Packet preparation is a tricky concept - the idea is to take the data
441 * pointed to by 'buffer' and treat it as a packet record of the same capture
442 * format as that used by the input trace. The provided libtrace packet then
443 * has its internal pointers and values set to describe the packet record in
444 * the buffer.
445 *
446 * The primary use of this function is to allow the RT packet reader to
447 * easily and safely convert packets from the RT format back into the format
448 * that they were originally captured with., essentially removing the RT
449 * encapsulation.
450 *
451 * We've decided not to make this function available via the exported API
452 * because there are several issues that can arise if it is not used very
453 * carefully and it is not very useful outside of internal contexts anyway.
454 */
455int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet,
456                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags);
457
458/** Flags for prepare_packet functions */
459enum {
460        /** The buffer memory has been allocated by libtrace and should be
461         * freed when the packet is destroyed. */
462        TRACE_PREP_OWN_BUFFER           =1,
463       
464        /** The buffer memory is externally-owned and must not be freed by
465         * libtrace when the packet is destroyed. */
466        TRACE_PREP_DO_NOT_OWN_BUFFER    =0
467};
468
469
470#ifndef PF_RULESET_NAME_SIZE
471#define PF_RULESET_NAME_SIZE 16
472#endif
473
474#ifndef IFNAMSIZ
475#define IFNAMSIZ 16
476#endif
477
478
479/** A local definition of a PFLOG header */
480typedef struct libtrace_pflog_header_t {
481        uint8_t    length;     
482        sa_family_t   af;
483        uint8_t    action;
484        uint8_t    reason;
485        char       ifname[IFNAMSIZ];
486        char       ruleset[PF_RULESET_NAME_SIZE];
487        uint32_t   rulenr;
488        uint32_t   subrulenr;
489        uint8_t    dir;
490        uint8_t    pad[3];
491} PACKED libtrace_pflog_header_t;
492
493/** A libtrace capture format module */
494/* All functions should return -1, or NULL on failure */
495struct libtrace_format_t {
496        /** The name of this module, used in the libtrace URI to identify the
497         * capture format */
498        const char *name;
499        /** The version of this module */
500        const char *version;
501        /** The RT protocol type of this module */
502        enum base_format_t type;
503
504
505        /** Given a filename, return if this is the most likely capture format
506         * (used for devices). Used to "guess" the capture format when the
507         * URI is not fully specified.
508         *
509         * @param fname         The name of the device or file to examine
510         * @return 1 if the name matches the capture format, 0 otherwise
511         */
512        int (*probe_filename)(const char *fname);
513       
514        /** Given a file, looks at the start of the file to determine if this
515         * is the capture format. Used to "guess" the capture format when the
516         * URI is not fully specified.
517         *
518         * @param io            An open libtrace IO reader for the file to check
519         * @return 1 if the file matches the capture format, 0 otherwise
520         */
521        int (*probe_magic)(io_t *io);
522
523        /** Initialises an input trace using the capture format.
524         *
525         * @param libtrace      The input trace to be initialised
526         * @return 0 if successful, -1 in the event of error
527         */
528        int (*init_input)(libtrace_t *libtrace);
529       
530        /** Applies a configuration option to an input trace.
531         *
532         * @param libtrace      The input trace to apply the option to
533         * @param option        The option that is being configured
534         * @param value         A pointer to the value that the option is to be
535         *                      set to
536         * @return 0 if successful, -1 if the option is unsupported or an error
537         * occurs
538         */
539        int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
540        /** Starts or unpauses an input trace - note that this function is
541         * often the one that opens the file or device for reading.
542         *
543         * @param libtrace      The input trace to be started or unpaused
544         * @return 0 if successful, -1 in the event of error */
545        int (*start_input)(libtrace_t *libtrace);
546
547        /** Pauses an input trace - this function should close or detach the
548         * file or device that is being read from.
549         *
550         * @param libtrace      The input trace to be paused
551         * @return 0 if successful, -1 in the event of error
552         */
553        int (*pause_input)(libtrace_t *libtrace);
554
555        /** Initialises an output trace using the capture format.
556         *
557         * @param libtrace      The output trace to be initialised
558         * @return 0 if successful, -1 in the event of error
559         */
560        int (*init_output)(libtrace_out_t *libtrace);
561       
562        /** Applies a configuration option to an output trace.
563         *
564         * @param libtrace      The output trace to apply the option to
565         * @param option        The option that is being configured
566         * @param value         A pointer to the value that the option is to be
567         *                      set to
568         * @return 0 if successful, -1 if the option is unsupported or an error
569         * occurs
570         * */
571        int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *value);
572
573        /** Starts an output trace - note that this function is often the one
574         * that opens the file or device for writing.
575         *
576         * @param libtrace      The output trace to be started
577         * @return 0 if successful, -1 if an error occurs
578         *
579         * There is no pause for output traces, as writing is not performed
580         * asynchronously.
581         */
582        int (*start_output)(libtrace_out_t *libtrace);
583
584        /** Concludes an input trace and cleans up the capture format data.
585         *
586         * @param libtrace      The input trace to be concluded
587         * @return 0 if successful, -1 if an error occurs
588         *
589         * Libtrace will call the pause_input function if the input trace is
590         * currently active prior to calling this function.
591         */
592        int (*fin_input)(libtrace_t *libtrace);
593
594        /** Concludes an output trace and cleans up the capture format data.
595         *
596         * @param libtrace      The output trace to be concluded
597         * @return 0 if successful, -1 if an error occurs
598         */
599        int (*fin_output)(libtrace_out_t *libtrace);
600
601        /** Reads the next packet from an input trace into the provided packet
602         * structure.
603         *
604         * @param libtrace      The input trace to read from
605         * @param packet        The libtrace packet to read into
606         * @return The size of the packet read (in bytes) including the capture
607         * framing header, or -1 if an error occurs. 0 is returned in the
608         * event of an EOF.
609         *
610         * If no packets are available for reading, this function should block
611         * until one appears or return 0 if the end of a trace file has been
612         * reached.
613         */
614        int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
615       
616        /** Converts a buffer containing a packet record into a libtrace packet
617         *
618         * @param libtrace      An input trace in the capture format for the
619         *                      packet
620         * @param packet        A libtrace packet to put the prepared packet
621         *                      into
622         * @param buffer        The buffer containing the packet record
623         *                      (including the capture format header)
624         * @param rt_type       The RT type for the packet
625         * @param flags         Flags describing properties that should be
626         *                      applied to the new packet
627         * @return 0 if successful, -1 if an error occurs.
628         *
629         * Updates internal trace and packet details, such as payload pointers,
630         * loss counters and packet types to match the packet record provided
631         * in the buffer. This is a zero-copy function.
632         *
633         * Intended (at this stage) only for internal use, particularly by
634         * RT which needs to decapsulate RT packets */
635        int (*prepare_packet)(libtrace_t *libtrace, libtrace_packet_t *packet,
636                        void *buffer, libtrace_rt_types_t rt_type, 
637                        uint32_t flags);
638       
639        /** Frees any resources allocated by the capture format module for a
640         * libtrace packet.
641         *
642         * @param The packet to be finalised
643         *       */
644        void (*fin_packet)(libtrace_packet_t *packet);
645
646        /** Write a libtrace packet to an output trace.
647         *
648         * @param libtrace      The output trace to write the packet to
649         * @param packet        The packet to be written out
650         * @return The number of bytes written, or -1 if an error occurs
651         */
652        int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
653        /** Returns the libtrace link type for a packet.
654         *
655         * @param packet        The packet to get the link type for
656         * @return The libtrace link type, or -1 if this link type is unknown
657         */ 
658        libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
659
660        /** Returns the direction of a packet.
661         *
662         * @param packet        The packet to get the direction for
663         * @return The direction of the packet, or -1 if no direction tag is
664         * present or an error occurs
665         */ 
666        libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
667       
668        /** Sets the direction of a packet.
669         *
670         * @param packet        The packet to set the direction for
671         * @param direction     The direction to assign to the packet
672         * @return The updated direction for the packet, or -1 if an error
673         * occurs
674         *
675         * @note Some capture formats do not feature direction tagging, so it
676         * will not make sense to implement a set_direction function for them.
677         */ 
678        libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
679       
680        /** Returns the timestamp for a packet in the ERF timestamp format.
681         *
682         * @param packet        The packet to get the timestamp from
683         * @return The 64-bit ERF timestamp
684         *
685         * @note Each format must implement at least one of the four "get
686         * timestamp" functions.
687         *
688         * If not implemented, libtrace will convert the result of one of the
689         * other timestamp functions into the appropriate format instead.
690         * This means each capture format only needs to implement the most
691         * sensible of the four and let libtrace handle any conversions.
692         *
693         */
694        uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
695
696        /** Returns the timestamp for a packet in the timeval format
697         *
698         * @param packet        The packet to get the timestamp from
699         * @return The timestamp from the packet as a timeval
700         *
701         * @note Each format must implement at least one of the four "get
702         * timestamp" functions.
703         *
704         * If not implemented, libtrace will convert the result of one of the
705         * other timestamp functions into the appropriate format instead.
706         * This means each capture format only needs to implement the most
707         * sensible of the four and let libtrace handle any conversions.
708         */
709        struct timeval (*get_timeval)(const libtrace_packet_t *packet);
710       
711        /** Returns the timestamp for a packet in the timespec format.
712         *
713         * @param packet        The packet to get the timestamp from
714         * @return The timestamp from the packet as a timespec
715         *
716         * @note Each format must implement at least one of the four "get
717         * timestamp" functions.
718         *
719         * If not implemented, libtrace will convert the result of one of the
720         * other timestamp functions into the appropriate format instead.
721         * This means each capture format only needs to implement the most
722         * sensible of the four and let libtrace handle any conversions.
723         */
724        struct timespec (*get_timespec)(const libtrace_packet_t *packet);
725       
726        /** Returns the timestamp for a packet in floating point seconds.
727         *
728         * @param packet        The packet to get the timestamp from
729         * @return The timestamp from the packet as a floating point number of
730         * seconds since 1970-01-01 00:00:00 UTC
731         *
732         * @note Each format must implement at least one of the four "get
733         * timestamp" functions.
734         *
735         * If not implemented, libtrace will convert the result of one of the
736         * other timestamp functions into the appropriate format instead.
737         * This means each capture format only needs to implement the most
738         * sensible of the four and let libtrace handle any conversions.
739         */
740        double (*get_seconds)(const libtrace_packet_t *packet);
741       
742        /** Moves the read pointer to a certain ERF timestamp within an input
743         * trace file.
744         *
745         * @param trace         The input trace to seek within
746         * @param timestamp     The timestamp to seek to, as an ERF timestamp
747         *
748         * @return 0 on success, -1 on failure.
749         *
750         * The next packet read from this trace will now be the first packet
751         * to have a timestamp equal to or greater than the provided timestamp.
752         *
753         * @note Each format that supports seeking must implement at least one
754         * of the seek functions.
755         *
756         * If not implemented, libtrace will convert the timestamp into the
757         * appropriate format to use a seek function that has been implemented.
758         * This means each capture format only needs to implement the seek
759         * function that matches the native timestamp format for that capture.
760         *
761         */
762        int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
763        /** Moves the read pointer to a certain timestamp represented using a
764         * timeval within an input trace file.
765         *
766         * @param trace         The input trace to seek within
767         * @param timestamp     The timestamp to seek to, as a timeval
768         *
769         * @return 0 on success, -1 on failure.
770         *
771         * The next packet read from this trace will now be the first packet
772         * to have a timestamp equal to or greater than the provided timestamp.
773         *
774         * @note Each format that supports seeking must implement at least one
775         * of the seek functions.
776         *
777         * If not implemented, libtrace will convert the timestamp into the
778         * appropriate format to use a seek function that has been implemented.
779         * This means each capture format only needs to implement the seek
780         * function that matches the native timestamp format for that capture.
781         *
782         */
783        int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
784       
785        /** Moves the read pointer to a certain timestamp represented using
786         * floating point seconds within an input trace file.
787         *
788         * @param trace         The input trace to seek within
789         * @param timestamp     The timestamp to seek to, as floating point
790         *                      seconds since 1970-01-01 00:00:00 UTC
791         *
792         * @return 0 on success, -1 on failure.
793         *
794         * The next packet read from this trace will now be the first packet
795         * to have a timestamp equal to or greater than the provided timestamp.
796         *
797         * @note Each format that supports seeking must implement at least one
798         * of the seek functions.
799         *
800         * If not implemented, libtrace will convert the timestamp into the
801         * appropriate format to use a seek function that has been implemented.
802         * This means each capture format only needs to implement the seek
803         * function that matches the native timestamp format for that capture.
804         *
805         */
806        int (*seek_seconds)(libtrace_t *trace, double seconds);
807       
808        /** Returns the payload length of the captured packet record.
809         *
810         * @param packet        The packet to get the capture length from
811         * @return The capture length for the packet, or -1 if an error occurs
812         *
813         * Capture length is the current size of the packet record itself,
814         * following any truncation that may have occurred during the capture
815         * process. This length does not include the capture format framing
816         * header.
817         */
818        int (*get_capture_length)(const libtrace_packet_t *packet);
819
820        /** Returns the original length of the packet as it was on the wire.
821         *
822         * @param packet        The packet to get the wire length from
823         * @return The length of the packet on the wire at the time of capture,
824         * or -1 if an error occurs
825         *
826         * Wire length is the original size of the packet prior to any
827         * truncation that may have occurred as part of the capture process.
828         * This length does not include the capture format framing header.
829         */
830        int (*get_wire_length)(const libtrace_packet_t *packet);
831       
832        /** Returns the length of the capture format framing header
833         *
834         * @param packet        The packet to get the framing length from
835         * @return The length of the framing header, or -1 if an error occurs
836         *
837         * The framing header is the extra metadata that the capture process
838         * records about a packet.  The framing length does not include any
839         * of the packet payload itself. The total size of the packet record
840         * can be calculated be adding this value with the capture length.
841         */
842        int (*get_framing_length)(const libtrace_packet_t *packet);
843
844        /** Sets the capture length for a packet.
845         *
846         * @param packet        The packet to adjust the capture length for.
847         * @param size          The new capture length
848         * @return The new capture length of the packet, or -1 if an error
849         * occurs
850         *
851         * @note This function should only reduce the capture length. If the
852         * provided length is larger than the current capture length, -1 should
853         * be returned.
854         */
855        size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
856        /** Returns the number of packets observed by an input trace.
857         *
858         * @param trace         The input trace to get the packet count for
859         * @return The number of packets observed by an input trace, or
860         * UINT64_MAX if the number is unknown
861         *
862         * This count includes packets that have been filtered and dropped.
863         */
864        uint64_t (*get_received_packets)(libtrace_t *trace);
865
866        /** Returns the number of packets filtered by an input trace.
867         *
868         * @param trace         The input trace to get the filtered count for
869         * @return The number of packets filtered by the input trace, or
870         * UINT64_MAX if the number is unknown
871         *
872         */
873        uint64_t (*get_filtered_packets)(libtrace_t *trace);
874       
875        /** Returns the number of packets dropped by an input trace.
876         *
877         * @param trace         The input trace to get the dropped count for
878         * @return The number of packets dropped by the input trace, or
879         * UINT64_MAX if the number is unknown
880         *
881         */
882        uint64_t (*get_dropped_packets)(libtrace_t *trace);
883
884        /** Returns statistics about a trace.
885         *
886         * @param trace The libtrace object
887         * @param stat [in,out] A statistics structure ready to be filled
888         *
889         * The filtered and accepted statistics will be set to the values
890         * stored in the library. All other statistics are not set.
891         *
892         * @note If filtering of packets is performed by a trace and the number
893         * of filtered packets is unknown this should be marked as invalid by
894         * the format.
895         */
896        void (*get_statistics)(libtrace_t *trace, libtrace_stat_t *stat);
897       
898        /** Returns the file descriptor used by the input trace.
899         *
900         * @param trace         The input trace to get the file descriptor for
901         * @return The file descriptor used by the input trace to read packets
902         *
903         */
904        int (*get_fd)(const libtrace_t *trace);
905       
906        /** Returns the next libtrace event for the input trace.
907         *
908         * @param trace         The input trace to get the next event from
909         * @param packet        A libtrace packet to read a packet into
910         * @return A libtrace event describing the event that occured
911         *
912         * The event API allows for non-blocking reading of packets from an
913         * input trace. If a packet is available and ready to be read, a packet
914         * event should be returned. Otherwise a sleep or fd event should be
915         * returned to indicate that the caller needs to wait. If the input
916         * trace has an error or reaches EOF, a terminate event should be
917         * returned.
918         */
919        struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);       
920
921        /** Prints some useful help information to standard output. */
922        void (*help)(void);
923       
924        /** Next pointer, should always be NULL - used by the format module
925         * manager. */
926        struct libtrace_format_t *next;
927
928        /** Holds information about the trace format */
929        struct libtrace_info_t info;
930
931        /**
932         * Starts or unpauses an input trace in parallel mode - note that
933         * this function is often the one that opens the file or device for
934         * reading.
935         *
936         * @param libtrace      The input trace to be started or unpaused
937         * @return 0 upon success.
938         *         Otherwise in event of an error -1 is returned.
939         *
940         */
941        int (*pstart_input)(libtrace_t *trace);
942       
943        /**
944         * Read a batch of packets from the input stream related to thread.
945         * At most read nb_packets, however should return with less if packets
946         * are not waiting. However still must return at least 1, 0 still indicates
947         * EOF.
948         *
949         * @param libtrace      The input trace
950         * @param t     The thread
951         * @param packets       An array of packets
952         * @param nb_packets    The number of packets in the array (the maximum to read)
953         * @return The number of packets read, or 0 in the case of EOF or -1 in error or -2 to represent
954         * interrupted due to message waiting before packets had been read.
955         */
956        int (*pread_packets)(libtrace_t *trace, libtrace_thread_t *t, libtrace_packet_t **packets, size_t nb_packets);
957       
958        /** Pause a parallel trace
959         *
960         * @param libtrace      The input trace to be paused
961         */
962        int (*ppause_input)(libtrace_t *trace);
963       
964        /** Called after all threads have been paused, Finish (close) a parallel trace
965         *
966         * @param libtrace      The input trace to be stopped
967         */
968        int (*pfin_input)(libtrace_t *trace);
969
970        /**
971         * Register a thread for use with the format or using the packets produced
972         * by it. This is NOT only used for threads reading packets in fact all
973         * threads use this.
974         *
975         * The libtrace lock is not held by this format but can be aquired
976         * by the format.
977         *
978         * Some use cases include setting up any thread local storage required for
979         * to read packets and free packets. For DPDK we require any thread that
980         * may release or read a packet to have have an internal number associated
981         * with it.
982         *
983         * The thread type can be used to see if this thread is going to be used
984         * to read packets or otherwise.
985         *
986         * @return 0 if successful, -1 if the option is unsupported or an error
987         * occurs (such as a maximum of threads being reached)
988         */
989        int (*pregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t, bool reader);
990
991        /**
992         * If needed any memory allocated with pregister_thread can be released
993         * in this function. The thread will be destroyed directly after this
994         * function is called.
995         */
996        void (*punregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t);
997
998        /** Returns statistics for a single thread.
999         *
1000         * @param trace The libtrace object
1001         * @param t The thread to return statistics for
1002         * @param stat [in,out] A statistics structure ready to be filled
1003         *
1004         * The filtered and accepted statistics will be set to the values
1005         * stored in the library. All other statistics are not set.
1006         *
1007         * @note If filtering of packets is performed by a trace and the number
1008         * of filtered packets is unknown this should be marked as invalid by
1009         * the format.
1010         */
1011        void (*get_thread_statistics)(libtrace_t *libtrace,
1012                                      libtrace_thread_t *t,
1013                                      libtrace_stat_t *stat);
1014};
1015
1016/** Macro to zero out a single thread format */
1017#define NON_PARALLEL(live) \
1018        {live, 1},              /* trace info */ \
1019        NULL,                   /* pstart_input */ \
1020        NULL,                   /* pread_packet */ \
1021        NULL,                   /* ppause_input */ \
1022        NULL,                   /* pfin_input */ \
1023        NULL,                   /* pregister_thread */ \
1024        NULL,                   /* punregister_thread */ \
1025        NULL,                   /* get_thread_statistics */
1026
1027/** The list of registered capture formats */
1028//extern struct libtrace_format_t *form;
1029
1030/** Specifies whether any blocking packet readers should cease reading
1031 * immediately
1032 */
1033extern volatile int libtrace_halt;
1034
1035/** Registers a new capture format module.
1036 *
1037 * @param format        The format module to be registered
1038 */
1039void register_format(struct libtrace_format_t *format);
1040
1041/** Converts a timeval into a timestamp in microseconds since the epoch.
1042 *
1043 * @param tv    The timeval to be converted.
1044 * @return A 64 bit timestamp in microseconds since the epoch.
1045 */
1046uint64_t tv_to_usec(const struct timeval *tv);
1047
1048/** Converts a PCAP DLT into a libtrace link type.
1049 *
1050 * @param linktype      The PCAP DLT to be converted
1051 * @return The libtrace link type that is equivalent to the provided DLT, or
1052 * -1 if the DLT is unknown
1053 */
1054libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype);
1055
1056/** Converts a PCAP DLT into an RT protocol type.
1057 *
1058 * @param linktype      The PCAP DLT to be converted
1059 * @return The RT type that is equivalent to the provided DLT
1060 */
1061libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype);
1062
1063/** Converts a libtrace link type into a PCAP linktype.
1064 *
1065 * @param type          The libtrace link type to be converted
1066 * @return The PCAP linktype that is equivalent to the provided libtrace link
1067 * type, or -1 if the link type is unknown
1068 */
1069libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type);
1070
1071/** Converts a libtrace link type into a PCAP DLT.
1072 *
1073 * @param type          The libtrace link type to be converted
1074 * @return The PCAP DLT that is equivalent to the provided libtrace link
1075 * type, or -1 if the link type is unknown
1076 */
1077libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
1078
1079/** Converts an RT protocol type into a PCAP DLT.
1080 *
1081 * @param rt_type       The RT type to be converted
1082 * @return The PCAP DLT that is equivalent to the provided RT protocol
1083 */
1084libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type);
1085
1086/** Converts a PCAP DLT into an RT protocol type for the BPF format.
1087 *
1088 * @param linktype      The PCAP DLT to be converted
1089 * @return The RT type that is equivalent to the provided DLT for BPF
1090 */
1091libtrace_rt_types_t bpf_linktype_to_rt(libtrace_dlt_t linktype);
1092
1093/** Converts an ERF type into a libtrace link type.
1094 *
1095 * @param erf           The ERF type to be converted
1096 * @return The libtrace link type that is equivalent to the provided ERF type,
1097 * or -1 if the ERF type is unknown
1098 */
1099libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
1100
1101/** Converts a libtrace link type into an ERF type.
1102 *
1103 * @param linktype      The libtrace link type to be converted
1104 * @return The ERF type that is equivalent to the provided libtrace link type,
1105 * or -1 if the link type cannot be matched to an ERF type.
1106 */
1107uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
1108
1109/** Converts an ARPHRD type into a libtrace link type.
1110 *
1111 * @param arphrd        The ARPHRD type to be converted
1112 * @return The libtrace link type that is equivalent to the provided ARPHRD
1113 * type, or -1 if the ARPHRD type is unknown
1114 */
1115libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd);
1116
1117/** Converts a libtrace link type into an ARPHRD type.
1118 *
1119 * @param type          The libtrace link type to be converted
1120 * @return The ARPHRD type that is equivalent to the provided libtrace link
1121 * type, or -1 if the link type cannot be matched to an ARPHRD type
1122 */
1123unsigned int libtrace_to_arphrd_type(libtrace_linktype_t type);
1124
1125/** Converts a libtrace packet to the Linux SLL type.
1126 *
1127 * @param packet        The packet to be promoted
1128 *
1129 * @note This will involve memcpy() so use sparingly.
1130 *
1131 * This function prepends a Linux SLL header to a packet so that we can store
1132 * direction tagging information.
1133 */
1134void promote_packet(libtrace_packet_t *packet);
1135
1136/** Attempts to demote a packet by removing the first header.
1137 *
1138 * @param packet        The packet to be demoted
1139 * @return True if the packet was demoted, false otherwise.
1140 *
1141 * Essentially the opposite of promote_packet, except that it will also remove
1142 * an ATM header as well as Linux SLL.
1143 *
1144 */
1145bool demote_packet(libtrace_packet_t *packet);
1146
1147/** Returns a pointer to the header following a Linux SLL header.
1148 *
1149 * @param link          A pointer to the Linux SLL header to be skipped
1150 * @param[out] arphrd_type      The arp hardware type of the packet
1151 * @param[out] next_header      The ethertype of the next header
1152 * @param[in,out] remaining     Updated with the number of captured bytes
1153 *                              remaining
1154 * @return A pointer to the header following the Linux SLL header, or NULL if
1155 * no subsequent header is present.
1156 *
1157 * Remaining must point to the number of bytes captured from the Linux SLL
1158 * header and beyond.  It will be decremented by the number of bytes skipped
1159 * to find the payload.
1160 *
1161 * If the Linux SLL header is complete but there are zero bytes of payload
1162 * after the end of the header, a pointer to where the payload would be is
1163 * returned and remaining will be set to zero. If the Linux SLL header is
1164 * incomplete (truncated), then NULL is returned and remaining will be set to
1165 * 0. Therefore, it is very important to check the value of remaining after
1166 * calling this function.
1167 */     
1168void *trace_get_payload_from_linux_sll(const void *link,
1169                uint16_t *arphrd_type, 
1170                uint16_t *next_header, 
1171                uint32_t *remaining);
1172
1173/** Returns a pointer to the header following an ATM header.
1174 *
1175 * @param link          A pointer to the ATM header to be skipped
1176 * @param[out] type     The ethertype of the next header
1177 * @param[in,out] remaining     Updated with the number of captured bytes
1178 *                              remaining
1179 * @return A pointer to the header following the ATM header, or NULL if
1180 * no subsequent header is present.
1181 *
1182 * Remaining must point to the number of bytes captured from the ATM header
1183 * and beyond.  It will be decremented by the number of bytes skipped to find
1184 * the payload.
1185 *
1186 * If the ATM header is complete but there are zero bytes of payload
1187 * after the end of the header, a pointer to where the payload would be is
1188 * returned and remaining will be set to zero. If the ATM header is
1189 * incomplete (truncated), then NULL is returned and remaining will be set to
1190 * 0. Therefore, it is very important to check the value of remaining after
1191 * calling this function.
1192 */     
1193DLLEXPORT void *trace_get_payload_from_atm(void *link, uint8_t *type, 
1194                uint32_t *remaining);
1195
1196
1197#ifdef HAVE_BPF
1198/* A type encapsulating a bpf filter
1199 * This type covers the compiled bpf filter, as well as the original filter
1200 * string
1201 *
1202 */
1203
1204/** Internal representation of a BPF filter */
1205struct libtrace_filter_t {
1206        struct bpf_program filter;      /**< The BPF program itself */
1207        char * filterstring;            /**< The filter string */
1208        int flag;                       /**< Indicates if the filter is valid */
1209        struct bpf_jit_t *jitfilter;
1210};
1211#else
1212/** BPF not supported by this system, but we still need to define a structure
1213 * for the filter */
1214struct libtrace_filter_t {};
1215#endif
1216
1217/** Local definition of a PCAP header */
1218typedef struct libtrace_pcapfile_pkt_hdr_t {
1219        uint32_t ts_sec;        /* Seconds portion of the timestamp */
1220        uint32_t ts_usec;       /* Microseconds portion of the timestamp */
1221        uint32_t caplen;        /* Capture length of the packet */
1222        uint32_t wirelen;       /* The wire length of the packet */
1223} libtrace_pcapfile_pkt_hdr_t;
1224
1225#ifdef HAVE_DAG
1226/** Constructor for the DAG format module */
1227void dag_constructor(void);
1228#endif
1229/** Constructor for the ERF format module */
1230void erf_constructor(void);
1231/** Constructor for the TSH format module */
1232void tsh_constructor(void);
1233/** Constructor for the Legacy DAG format module */
1234void legacy_constructor(void);
1235/** Constructor for the Linux Native format module */
1236void linuxnative_constructor(void);
1237/** Constructor for the Linux Ring format module */
1238void linuxring_constructor(void);
1239/** Constructor for the PCAP format module */
1240void pcap_constructor(void);
1241/** Constructor for the PCAP File format module */
1242void pcapfile_constructor(void);
1243/** Constructor for the RT format module */
1244void rt_constructor(void);
1245/** Constructor for the DUCK format module */
1246void duck_constructor(void);
1247/** Constructor for the ATM Header format module */
1248void atmhdr_constructor(void);
1249#ifdef HAVE_BPF
1250/** Constructor for the BPF format module */
1251void bpf_constructor(void);
1252#endif
1253#if HAVE_DPDK
1254/** Constructor for Intels DPDK format module */
1255void dpdk_constructor(void);
1256#endif
1257
1258/** Extracts the RadioTap flags from a wireless link header
1259 *
1260 * @param link          A pointer to the wireless link header
1261 * @param linktype      The link type of the wireless header
1262 * @param[out] flags    Space to store the extracted flags
1263 * @return True if libtrace was able to extract flags from the link header,
1264 * false otherwise.
1265 *
1266 * This function has been left internal because it is not portable across
1267 * drivers.
1268 */
1269bool trace_get_wireless_flags(void *link, libtrace_linktype_t linktype, uint8_t *flags);
1270#define TRACE_RADIOTAP_F_FCS 0x10
1271       
1272#ifdef __cplusplus
1273}
1274#endif
1275
1276#endif /* LIBTRACE_INT_H */
Note: See TracBrowser for help on using the repository browser.