source: lib/libtrace_int.h @ 10553bf

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 10553bf was 1101175, checked in by Shane Alcock <salcock@…>, 6 years ago

Polish up all the documentation in libtrace_parallel.h

Removed trace_get_local, trace_set_local, trace_get_tls and trace_set_tls.
These are all covered by parameters to the callback functions, so seem
redundant now.

Moved tv_to_usec into libtrace_int.h.

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