source: lib/libtrace_int.h @ fac8c46

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since fac8c46 was fac8c46, checked in by Richard Sanger <rsangerarj@…>, 8 years ago

Tidies up the pausing so that it now works as expected and a trace can easily be paused and restarted.
Ensures that packets will not be lost if pause is called on a file, any queued packets will be read (a message is sent allowing the user to drop these packets if they are unwanted).
Differentiates packets from other results in the queues to the reducer/reporter and makes a copy of the packets in result queues when pausing

  • this is needed to ensure that bad memory isn't referenced if a zero-copy trace is paused by closing sockets/associated data like in the case of ring:.

Fixed up the re-starting of traces which hadn't been finished to account for different configurations.
Adds a 'state' to libtrace to handle the state of parallel traces, rather than hacking around the existing 'started' boolean. Also provides two levels of checks for consistency if the trace is using existing that are checking started.

Various other bug fixes and tidy ups.

  • Property mode set to 100644
File size: 39.5 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.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/vector.h"
152#include "data-struct/message_queue.h"
153#include "data-struct/deque.h"
154#include "data-struct/sliding_window.h"
155
156//#define RP_BUFSIZE 65536U
157
158/** Data about the most recent event from a trace file */
159struct libtrace_event_status_t {
160        /** A libtrace packet to store the packet when a PACKET event occurs */
161        libtrace_packet_t *packet;
162        /** Time between the timestamp for the current packet and the current
163         * walltime */
164        double tdelta;
165        /** The timestamp of the previous PACKET event */
166        double trace_last_ts;
167        /** The size of the current PACKET event */
168        int psize;
169        /** Whether there is a packet stored in *packet above waiting for an
170         * event to occur */
171        bool waiting;
172};
173
174enum thread_types {
175        THREAD_EMPTY,
176        THREAD_HASHER,
177        THREAD_PERPKT,
178        THREAD_REDUCER
179};
180
181enum thread_states {
182        THREAD_RUNNING,
183        THREAD_FINISHING,
184        THREAD_FINISHED,
185        THREAD_PAUSED
186};
187
188// Reduce expects sequential data
189#define REDUCE_SEQUENTIAL 0x1
190// Reduce is working on ordered data
191#define REDUCE_ORDERED 0x2
192// Reduce should sort the data
193#define REDUCE_SORT 0x4
194// Drop out of order valid with
195#define REDUCE_DROP_OOO 0x8
196// Reduce reads all queues with same key
197#define REDUCE_STEPPING 0x10
198
199#define PERPKT_USE_SLIDING_WINDOW 0x20
200
201/**
202 * Information of this thread
203 */
204typedef struct libtrace_thread_t {
205        libtrace_t * trace;
206        void* ret;
207        enum thread_types type;
208        enum thread_states state;
209        void* user_data; // TLS for the user to use
210        pthread_t tid;
211        int perpkt_num; // A number from 0-X that represents this perpkt threads number
212                                // in the table, intended to quickly identify this thread
213                                // -1 represents NA (such as the case this is not a perpkt thread)
214        libtrace_ringbuffer_t rbuffer; // Input
215        libtrace_vector_t vector; // Output
216        libtrace_queue_t deque; // Real Output type makes more sense
217        libtrace_message_queue_t messages; // Message handling
218        // Temp storage for time sensitive results
219        uint64_t tmp_key;
220        void *tmp_data;
221        pthread_spinlock_t tmp_spinlock;
222        // Set to true once the first packet has been stored
223        bool recorded_first;
224        // For thread safety reason we actually must store this here
225        int64_t tracetime_offset_usec;
226} libtrace_thread_t;
227
228/**
229 * Storage to note time value against each.
230 * Used both internally to do trace time playback
231 * and can be used externally to assist applications which need
232 * a trace starting time such as tracertstats.
233 */
234struct first_packets {
235        pthread_spinlock_t lock;
236        size_t count; // If == perpkt_thread_count threads we have all
237        size_t first; // Valid if count != 0
238        struct __packet_storage_magic_type {
239                libtrace_packet_t * packet;
240                struct timeval tv;
241        } * packets;
242};
243
244#define TRACE_STATES \
245        X(STATE_NEW) \
246        X(STATE_RUNNING) \
247        X(STATE_PAUSING) \
248        X(STATE_PAUSED) \
249        X(STATE_FINSHED) \
250        X(STATE_DESTROYED) \
251        X(STATE_ERROR) // Currently unused
252
253#define X(a) a,
254enum trace_state {
255        TRACE_STATES
256};
257#undef X
258
259#define X(a) case a: return #a;
260static inline char *get_trace_state_name(enum trace_state ts){
261        switch(ts) {
262                TRACE_STATES
263                default:
264                        return "UNKNOWN";
265        }
266}
267#undef X
268
269/** A libtrace input trace
270 * @internal
271 */
272struct libtrace_t {
273        /** The capture format for the input trace */
274        struct libtrace_format_t *format; 
275        /** Details of the most recent PACKET event reported by the trace */
276        struct libtrace_event_status_t event;
277        /** Pointer to the "global" data for the capture format module */       
278        void *format_data;             
279        /** A BPF filter to be applied to all packets read by the trace -
280         * used only if the capture format does not support filters natively */
281        struct libtrace_filter_t *filter; 
282        /** The snap length to be applied to all packets read by the trace -
283         * used only if the capture format does not support snapping natively */
284        size_t snaplen;                 
285        /** Count of the number of packets returned to the libtrace user */
286        uint64_t accepted_packets;     
287        /** Count of the number of packets filtered by libtrace */
288        uint64_t filtered_packets;     
289        /** The filename from the uri for the trace */
290        char *uridata;
291        /** The libtrace IO reader for this trace (if applicable) */
292        io_t *io;
293        /** Error information for the trace */
294        libtrace_err_t err;
295        /** Boolean flag indicating whether the trace has been started */
296        bool started;
297        /** Synchronise writes/reads across this format object and attached threads etc */
298        pthread_mutex_t libtrace_lock;
299        /** State */
300        enum trace_state state;
301        /** Use to control pausing threads and finishing threads etc always used with libtrace_lock */
302        pthread_cond_t perpkt_cond;
303        /** Set to the number of perpkt threads that are finishing (or have finished), or to -1 once all have been joined, 0 implies all are running */
304        int perpkts_finishing;
305        /** A count of perpkt threads that are pausing */
306        int perpkts_pausing;
307
308        /** For the sliding window hasher implementation */
309        pthread_rwlock_t window_lock;
310        /** Set once trace_join has been called */
311        bool joined;
312        /** Set to indicate a perpkt's queue is full as such the writing perpkt cannot proceed */
313        bool perpkt_queue_full;
314        /** Global storage for this trace, shared among all the threads  */
315        void* global_blob;
316        /** Requested size of the pkt buffer (currently only used if using dedicated hasher thread) */
317        int packet_freelist_size;
318        /** The actual freelist */
319        libtrace_ringbuffer_t packet_freelist;
320        /** The number of packets that can queue per thread - XXX consider deadlocks with non malloc()'d packets that need to be released */
321        int perpkt_buffer_size;
322        /** The reducer flags */
323        int reducer_flags;
324        /** Used to track the next expected key */
325        uint64_t expected_key;
326        /** User defined per_pkt function called when a pkt is ready */
327        fn_per_pkt per_pkt;
328        /** User defined reducer function entry point XXX not hooked up */
329        fn_reducer reducer;
330        /** The hasher function */
331        enum hasher_types hasher_type;
332        /** The hasher function - NULL implies they don't care or balance */
333        fn_hasher hasher; // If valid using a separate thread
334        void *hasher_data;
335       
336        libtrace_thread_t hasher_thread;
337        libtrace_thread_t reducer_thread;
338        int perpkt_thread_count;
339        libtrace_thread_t * perpkt_threads; // All our perpkt threads
340        libtrace_slidingwindow_t sliding_window;
341        sem_t sem;
342        // Used to keep track of the first packet seen on each thread
343        struct first_packets first_packets;
344        int tracetime;
345};
346
347inline void libtrace_zero_thread(libtrace_thread_t * t);
348inline void store_first_packet(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t);
349libtrace_thread_t * get_thread_table(libtrace_t *libtrace);
350int get_thread_table_num(libtrace_t *libtrace);
351
352
353/** A libtrace output trace
354 * @internal
355 */
356struct libtrace_out_t {
357        /** The capture format for the output trace */
358        struct libtrace_format_t *format;
359        /** Pointer to the "global" data for the capture format module */
360        void *format_data;             
361        /** The filename for the uri for the output trace */
362        char *uridata;                 
363        /** Error information for the output trace */
364        libtrace_err_t err;
365        /** Boolean flag indicating whether the trace has been started */
366        bool started;
367};
368
369/** Sets the error status on an input trace
370 *
371 * @param trace         The input trace to set the error status for
372 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
373 * @param msg           A message to print when reporting the error
374 */
375void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...) 
376
377                                                                PRINTF(3,4);
378/** Sets the error status on an output trace
379 *
380 * @param trace         The output trace to set the error status for
381 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
382 * @param msg           A message to print when reporting the error
383 */
384void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
385                                                                PRINTF(3,4);
386
387/** Clears the cached values for a libtrace packet
388 *
389 * @param packet        The libtrace packet that requires a cache reset
390 */
391void trace_clear_cache(libtrace_packet_t *packet);
392
393/** Converts the data provided in buffer into a valid libtrace packet
394 *
395 * @param trace         An input trace of the same format as the "packet"
396 *                      contained in the buffer
397 * @param packet        The libtrace packet to prepare
398 * @param buffer        A buffer containing the packet data, including the
399 *                      capture format header
400 * @param rt_type       The RT type for the packet that is being prepared
401 * @param flags         Used to specify options for the preparation function,
402 *                      e.g. who owns the packet buffer
403 *
404 * @return -1 if an error occurs, 0 otherwise
405 *
406 * Packet preparation is a tricky concept - the idea is to take the data
407 * pointed to by 'buffer' and treat it as a packet record of the same capture
408 * format as that used by the input trace. The provided libtrace packet then
409 * has its internal pointers and values set to describe the packet record in
410 * the buffer.
411 *
412 * The primary use of this function is to allow the RT packet reader to
413 * easily and safely convert packets from the RT format back into the format
414 * that they were originally captured with., essentially removing the RT
415 * encapsulation.
416 *
417 * We've decided not to make this function available via the exported API
418 * because there are several issues that can arise if it is not used very
419 * carefully and it is not very useful outside of internal contexts anyway.
420 */
421int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet,
422                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags);
423
424/** Flags for prepare_packet functions */
425enum {
426        /** The buffer memory has been allocated by libtrace and should be
427         * freed when the packet is destroyed. */
428        TRACE_PREP_OWN_BUFFER           =1,
429       
430        /** The buffer memory is externally-owned and must not be freed by
431         * libtrace when the packet is destroyed. */
432        TRACE_PREP_DO_NOT_OWN_BUFFER    =0
433};
434
435
436#ifndef PF_RULESET_NAME_SIZE
437#define PF_RULESET_NAME_SIZE 16
438#endif
439
440#ifndef IFNAMSIZ
441#define IFNAMSIZ 16
442#endif
443
444
445/** A local definition of a PFLOG header */
446typedef struct libtrace_pflog_header_t {
447        uint8_t    length;     
448        sa_family_t   af;
449        uint8_t    action;
450        uint8_t    reason;
451        char       ifname[IFNAMSIZ];
452        char       ruleset[PF_RULESET_NAME_SIZE];
453        uint32_t   rulenr;
454        uint32_t   subrulenr;
455        uint8_t    dir;
456        uint8_t    pad[3];
457} PACKED libtrace_pflog_header_t;
458
459
460
461/** A libtrace capture format module */
462/* All functions should return -1, or NULL on failure */
463struct libtrace_format_t {
464        /** The name of this module, used in the libtrace URI to identify the
465         * capture format */
466        const char *name;
467        /** The version of this module */
468        const char *version;
469        /** The RT protocol type of this module */
470        enum base_format_t type;
471
472
473        /** Given a filename, return if this is the most likely capture format
474         * (used for devices). Used to "guess" the capture format when the
475         * URI is not fully specified.
476         *
477         * @param fname         The name of the device or file to examine
478         * @return 1 if the name matches the capture format, 0 otherwise
479         */
480        int (*probe_filename)(const char *fname);
481       
482        /** Given a file, looks at the start of the file to determine if this
483         * is the capture format. Used to "guess" the capture format when the
484         * URI is not fully specified.
485         *
486         * @param io            An open libtrace IO reader for the file to check
487         * @return 1 if the file matches the capture format, 0 otherwise
488         */
489        int (*probe_magic)(io_t *io);
490
491        /** Initialises an input trace using the capture format.
492         *
493         * @param libtrace      The input trace to be initialised
494         * @return 0 if successful, -1 in the event of error
495         */
496        int (*init_input)(libtrace_t *libtrace);
497       
498        /** Applies a configuration option to an input trace.
499         *
500         * @param libtrace      The input trace to apply the option to
501         * @param option        The option that is being configured
502         * @param value         A pointer to the value that the option is to be
503         *                      set to
504         * @return 0 if successful, -1 if the option is unsupported or an error
505         * occurs
506         */
507        int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
508        /** Starts or unpauses an input trace - note that this function is
509         * often the one that opens the file or device for reading.
510         *
511         * @param libtrace      The input trace to be started or unpaused
512         * @return 0 if successful, -1 in the event of error */
513        int (*start_input)(libtrace_t *libtrace);
514
515        /** Pauses an input trace - this function should close or detach the
516         * file or device that is being read from.
517         *
518         * @param libtrace      The input trace to be paused
519         * @return 0 if successful, -1 in the event of error
520         */
521        int (*pause_input)(libtrace_t *libtrace);
522
523        /** Initialises an output trace using the capture format.
524         *
525         * @param libtrace      The output trace to be initialised
526         * @return 0 if successful, -1 in the event of error
527         */
528        int (*init_output)(libtrace_out_t *libtrace);
529       
530        /** Applies a configuration option to an output trace.
531         *
532         * @param libtrace      The output 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_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *value);
540
541        /** Starts an output trace - note that this function is often the one
542         * that opens the file or device for writing.
543         *
544         * @param libtrace      The output trace to be started
545         * @return 0 if successful, -1 if an error occurs
546         *
547         * There is no pause for output traces, as writing is not performed
548         * asynchronously.
549         */
550        int (*start_output)(libtrace_out_t *libtrace);
551
552        /** Concludes an input trace and cleans up the capture format data.
553         *
554         * @param libtrace      The input trace to be concluded
555         * @return 0 if successful, -1 if an error occurs
556         *
557         * Libtrace will call the pause_input function if the input trace is
558         * currently active prior to calling this function.
559         */
560        int (*fin_input)(libtrace_t *libtrace);
561
562        /** Concludes an output trace and cleans up the capture format data.
563         *
564         * @param libtrace      The output trace to be concluded
565         * @return 0 if successful, -1 if an error occurs
566         */
567        int (*fin_output)(libtrace_out_t *libtrace);
568
569        /** Reads the next packet from an input trace into the provided packet
570         * structure.
571         *
572         * @param libtrace      The input trace to read from
573         * @param packet        The libtrace packet to read into
574         * @return The size of the packet read (in bytes) including the capture
575         * framing header, or -1 if an error occurs. 0 is returned in the
576         * event of an EOF.
577         *
578         * If no packets are available for reading, this function should block
579         * until one appears or return 0 if the end of a trace file has been
580         * reached.
581         */
582        int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
583       
584        /** Converts a buffer containing a packet record into a libtrace packet
585         *
586         * @param libtrace      An input trace in the capture format for the
587         *                      packet
588         * @param packet        A libtrace packet to put the prepared packet
589         *                      into
590         * @param buffer        The buffer containing the packet record
591         *                      (including the capture format header)
592         * @param rt_type       The RT type for the packet
593         * @param flags         Flags describing properties that should be
594         *                      applied to the new packet
595         * @return 0 if successful, -1 if an error occurs.
596         *
597         * Updates internal trace and packet details, such as payload pointers,
598         * loss counters and packet types to match the packet record provided
599         * in the buffer. This is a zero-copy function.
600         *
601         * Intended (at this stage) only for internal use, particularly by
602         * RT which needs to decapsulate RT packets */
603        int (*prepare_packet)(libtrace_t *libtrace, libtrace_packet_t *packet,
604                        void *buffer, libtrace_rt_types_t rt_type, 
605                        uint32_t flags);
606       
607        /** Frees any resources allocated by the capture format module for a
608         * libtrace packet.
609         *
610         * @param The packet to be finalised
611         *       */
612        void (*fin_packet)(libtrace_packet_t *packet);
613
614        /** Write a libtrace packet to an output trace.
615         *
616         * @param libtrace      The output trace to write the packet to
617         * @param packet        The packet to be written out
618         * @return The number of bytes written, or -1 if an error occurs
619         */
620        int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
621        /** Returns the libtrace link type for a packet.
622         *
623         * @param packet        The packet to get the link type for
624         * @return The libtrace link type, or -1 if this link type is unknown
625         */ 
626        libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
627
628        /** Returns the direction of a packet.
629         *
630         * @param packet        The packet to get the direction for
631         * @return The direction of the packet, or -1 if no direction tag is
632         * present or an error occurs
633         */ 
634        libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
635       
636        /** Sets the direction of a packet.
637         *
638         * @param packet        The packet to set the direction for
639         * @param direction     The direction to assign to the packet
640         * @return The updated direction for the packet, or -1 if an error
641         * occurs
642         *
643         * @note Some capture formats do not feature direction tagging, so it
644         * will not make sense to implement a set_direction function for them.
645         */ 
646        libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
647       
648        /** Returns the timestamp for a packet in the ERF timestamp format.
649         *
650         * @param packet        The packet to get the timestamp from
651         * @return The 64-bit ERF timestamp
652         *
653         * @note Each format must implement at least one of the four "get
654         * timestamp" functions.
655         *
656         * If not implemented, libtrace will convert the result of one of the
657         * other timestamp functions into the appropriate format instead.
658         * This means each capture format only needs to implement the most
659         * sensible of the four and let libtrace handle any conversions.
660         *
661         */
662        uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
663
664        /** Returns the timestamp for a packet in the timeval format
665         *
666         * @param packet        The packet to get the timestamp from
667         * @return The timestamp from the packet as a timeval
668         *
669         * @note Each format must implement at least one of the four "get
670         * timestamp" functions.
671         *
672         * If not implemented, libtrace will convert the result of one of the
673         * other timestamp functions into the appropriate format instead.
674         * This means each capture format only needs to implement the most
675         * sensible of the four and let libtrace handle any conversions.
676         */
677        struct timeval (*get_timeval)(const libtrace_packet_t *packet);
678       
679        /** Returns the timestamp for a packet in the timespec format.
680         *
681         * @param packet        The packet to get the timestamp from
682         * @return The timestamp from the packet as a timespec
683         *
684         * @note Each format must implement at least one of the four "get
685         * timestamp" functions.
686         *
687         * If not implemented, libtrace will convert the result of one of the
688         * other timestamp functions into the appropriate format instead.
689         * This means each capture format only needs to implement the most
690         * sensible of the four and let libtrace handle any conversions.
691         */
692        struct timespec (*get_timespec)(const libtrace_packet_t *packet);
693       
694        /** Returns the timestamp for a packet in floating point seconds.
695         *
696         * @param packet        The packet to get the timestamp from
697         * @return The timestamp from the packet as a floating point number of
698         * seconds since 1970-01-01 00:00:00 UTC
699         *
700         * @note Each format must implement at least one of the four "get
701         * timestamp" functions.
702         *
703         * If not implemented, libtrace will convert the result of one of the
704         * other timestamp functions into the appropriate format instead.
705         * This means each capture format only needs to implement the most
706         * sensible of the four and let libtrace handle any conversions.
707         */
708        double (*get_seconds)(const libtrace_packet_t *packet);
709       
710        /** Moves the read pointer to a certain ERF timestamp within an input
711         * trace file.
712         *
713         * @param trace         The input trace to seek within
714         * @param timestamp     The timestamp to seek to, as an ERF timestamp
715         *
716         * @return 0 on success, -1 on failure.
717         *
718         * The next packet read from this trace will now be the first packet
719         * to have a timestamp equal to or greater than the provided timestamp.
720         *
721         * @note Each format that supports seeking must implement at least one
722         * of the seek functions.
723         *
724         * If not implemented, libtrace will convert the timestamp into the
725         * appropriate format to use a seek function that has been implemented.
726         * This means each capture format only needs to implement the seek
727         * function that matches the native timestamp format for that capture.
728         *
729         */
730        int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
731        /** Moves the read pointer to a certain timestamp represented using a
732         * timeval within an input trace file.
733         *
734         * @param trace         The input trace to seek within
735         * @param timestamp     The timestamp to seek to, as a timeval
736         *
737         * @return 0 on success, -1 on failure.
738         *
739         * The next packet read from this trace will now be the first packet
740         * to have a timestamp equal to or greater than the provided timestamp.
741         *
742         * @note Each format that supports seeking must implement at least one
743         * of the seek functions.
744         *
745         * If not implemented, libtrace will convert the timestamp into the
746         * appropriate format to use a seek function that has been implemented.
747         * This means each capture format only needs to implement the seek
748         * function that matches the native timestamp format for that capture.
749         *
750         */
751        int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
752       
753        /** Moves the read pointer to a certain timestamp represented using
754         * floating point seconds within an input trace file.
755         *
756         * @param trace         The input trace to seek within
757         * @param timestamp     The timestamp to seek to, as floating point
758         *                      seconds since 1970-01-01 00:00:00 UTC
759         *
760         * @return 0 on success, -1 on failure.
761         *
762         * The next packet read from this trace will now be the first packet
763         * to have a timestamp equal to or greater than the provided timestamp.
764         *
765         * @note Each format that supports seeking must implement at least one
766         * of the seek functions.
767         *
768         * If not implemented, libtrace will convert the timestamp into the
769         * appropriate format to use a seek function that has been implemented.
770         * This means each capture format only needs to implement the seek
771         * function that matches the native timestamp format for that capture.
772         *
773         */
774        int (*seek_seconds)(libtrace_t *trace, double seconds);
775       
776        /** Returns the payload length of the captured packet record.
777         *
778         * @param packet        The packet to get the capture length from
779         * @return The capture length for the packet, or -1 if an error occurs
780         *
781         * Capture length is the current size of the packet record itself,
782         * following any truncation that may have occurred during the capture
783         * process. This length does not include the capture format framing
784         * header.
785         */
786        int (*get_capture_length)(const libtrace_packet_t *packet);
787
788        /** Returns the original length of the packet as it was on the wire.
789         *
790         * @param packet        The packet to get the wire length from
791         * @return The length of the packet on the wire at the time of capture,
792         * or -1 if an error occurs
793         *
794         * Wire length is the original size of the packet prior to any
795         * truncation that may have occurred as part of the capture process.
796         * This length does not include the capture format framing header.
797         */
798        int (*get_wire_length)(const libtrace_packet_t *packet);
799       
800        /** Returns the length of the capture format framing header
801         *
802         * @param packet        The packet to get the framing length from
803         * @return The length of the framing header, or -1 if an error occurs
804         *
805         * The framing header is the extra metadata that the capture process
806         * records about a packet.  The framing length does not include any
807         * of the packet payload itself. The total size of the packet record
808         * can be calculated be adding this value with the capture length.
809         */
810        int (*get_framing_length)(const libtrace_packet_t *packet);
811
812        /** Sets the capture length for a packet.
813         *
814         * @param packet        The packet to adjust the capture length for.
815         * @param size          The new capture length
816         * @return The new capture length of the packet, or -1 if an error
817         * occurs
818         *
819         * @note This function should only reduce the capture length. If the
820         * provided length is larger than the current capture length, -1 should
821         * be returned.
822         */
823        size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
824        /** Returns the number of packets observed by an input trace.
825         *
826         * @param trace         The input trace to get the packet count for
827         * @return The number of packets observed by an input trace, or
828         * UINT64_MAX if the number is unknown
829         *
830         * This count includes packets that have been filtered and dropped.
831         */
832        uint64_t (*get_received_packets)(libtrace_t *trace);
833
834        /** Returns the number of packets filtered by an input trace.
835         *
836         * @param trace         The input trace to get the filtered count for
837         * @return The number of packets filtered by the input trace, or
838         * UINT64_MAX if the number is unknown
839         *
840         */
841        uint64_t (*get_filtered_packets)(libtrace_t *trace);
842       
843        /** Returns the number of packets dropped by an input trace.
844         *
845         * @param trace         The input trace to get the dropped count for
846         * @return The number of packets dropped by the input trace, or
847         * UINT64_MAX if the number is unknown
848         *
849         */
850        uint64_t (*get_dropped_packets)(libtrace_t *trace);
851       
852        /** Returns the number of packets captured and returned by an input
853         * trace.
854         *
855         * @param trace         The input trace to get the capture count for
856         * @return The number of packets returned to the libtrace user, or
857         * UINT64_MAX if the number is unknown
858         *
859         * This is the number of packets that have been successfully returned
860         * to the libtrace user via the read_packet() function.
861         *
862         */
863        uint64_t (*get_captured_packets)(libtrace_t *trace);
864       
865        /** Returns the file descriptor used by the input trace.
866         *
867         * @param trace         The input trace to get the file descriptor for
868         * @return The file descriptor used by the input trace to read packets
869         *
870         */
871        int (*get_fd)(const libtrace_t *trace);
872       
873        /** Returns the next libtrace event for the input trace.
874         *
875         * @param trace         The input trace to get the next event from
876         * @param packet        A libtrace packet to read a packet into
877         * @return A libtrace event describing the event that occured
878         *
879         * The event API allows for non-blocking reading of packets from an
880         * input trace. If a packet is available and ready to be read, a packet
881         * event should be returned. Otherwise a sleep or fd event should be
882         * returned to indicate that the caller needs to wait. If the input
883         * trace has an error or reaches EOF, a terminate event should be
884         * returned.
885         */
886        struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);       
887
888        /** Prints some useful help information to standard output. */
889        void (*help)(void);
890       
891        /** Starts or unpauses an input trace in parallel mode - note that
892         * this function is often the one that opens the file or device for
893         * reading.
894         *
895         * @param libtrace      The input trace to be started or unpaused
896         * @return If successful the number of threads started, 0 indicates
897         *                 no threads started and this should be done automatically.
898         *                 Otherwise in event of an error -1 is returned.
899         *
900         */
901        int (*pstart_input)(libtrace_t *trace);
902       
903        /** Read a packet in the new parallel mode
904         * @return same as read_packet, with the addition of return -2 to represent
905         * interrupted due to message waiting. */
906        int (*pread_packet)(libtrace_t *trace, libtrace_packet_t *packet);
907       
908        /** Pause a parallel trace */
909        int (*ppause_input)(libtrace_t *trace);
910       
911        /** Called after all threads have been paused, Finish (close) a parallel trace */
912        int (*pfin_input)(libtrace_t *trace);
913       
914        /** Applies a configuration option to an input trace.
915         *
916         * @param libtrace      The input trace to apply the option to
917         * @param option        The option that is being configured
918         * @param value         A pointer to the value that the option is to be
919         *                      set to
920         * @return 0 if successful, -1 if the option is unsupported or an error
921         * occurs
922         */
923        int (*pconfig_input)(libtrace_t *libtrace,trace_parallel_option_t option,void *value);
924       
925        /** Next pointer, should always be NULL - used by the format module
926         * manager. */
927        struct libtrace_format_t *next;
928};
929
930/** The list of registered capture formats */
931//extern struct libtrace_format_t *form;
932
933/** Specifies whether any blocking packet readers should cease reading
934 * immediately
935 */
936extern int libtrace_halt;
937
938/** Registers a new capture format module.
939 *
940 * @param format        The format module to be registered
941 */
942void register_format(struct libtrace_format_t *format);
943
944/** Converts a PCAP DLT into a libtrace link type.
945 *
946 * @param linktype      The PCAP DLT to be converted
947 * @return The libtrace link type that is equivalent to the provided DLT, or
948 * -1 if the DLT is unknown
949 */
950libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype);
951
952/** Converts a PCAP DLT into an RT protocol type.
953 *
954 * @param linktype      The PCAP DLT to be converted
955 * @return The RT type that is equivalent to the provided DLT
956 */
957libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype);
958
959/** Converts a libtrace link type into a PCAP linktype.
960 *
961 * @param type          The libtrace link type to be converted
962 * @return The PCAP linktype that is equivalent to the provided libtrace link
963 * type, or -1 if the link type is unknown
964 */
965libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type);
966
967/** Converts a libtrace link type into a PCAP DLT.
968 *
969 * @param type          The libtrace link type to be converted
970 * @return The PCAP DLT that is equivalent to the provided libtrace link
971 * type, or -1 if the link type is unknown
972 */
973libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
974
975/** Converts an RT protocol type into a PCAP DLT.
976 *
977 * @param rt_type       The RT type to be converted
978 * @return The PCAP DLT that is equivalent to the provided RT protocol
979 */
980libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type);
981
982/** Converts a PCAP DLT into an RT protocol type for the BPF format.
983 *
984 * @param linktype      The PCAP DLT to be converted
985 * @return The RT type that is equivalent to the provided DLT for BPF
986 */
987libtrace_rt_types_t bpf_linktype_to_rt(libtrace_dlt_t linktype);
988
989/** Converts an ERF type into a libtrace link type.
990 *
991 * @param erf           The ERF type to be converted
992 * @return The libtrace link type that is equivalent to the provided ERF type,
993 * or -1 if the ERF type is unknown
994 */
995libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
996
997/** Converts a libtrace link type into an ERF type.
998 *
999 * @param linktype      The libtrace link type to be converted
1000 * @return The ERF type that is equivalent to the provided libtrace link type,
1001 * or -1 if the link type cannot be matched to an ERF type.
1002 */
1003uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
1004
1005/** Converts an ARPHRD type into a libtrace link type.
1006 *
1007 * @param arphrd        The ARPHRD type to be converted
1008 * @return The libtrace link type that is equivalent to the provided ARPHRD
1009 * type, or -1 if the ARPHRD type is unknown
1010 */
1011libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd);
1012
1013/** Converts a libtrace link type into an ARPHRD type.
1014 *
1015 * @param type          The libtrace link type to be converted
1016 * @return The ARPHRD type that is equivalent to the provided libtrace link
1017 * type, or -1 if the link type cannot be matched to an ARPHRD type
1018 */
1019unsigned int libtrace_to_arphrd_type(libtrace_linktype_t type);
1020
1021/** Converts a libtrace packet to the Linux SLL type.
1022 *
1023 * @param packet        The packet to be promoted
1024 *
1025 * @note This will involve memcpy() so use sparingly.
1026 *
1027 * This function prepends a Linux SLL header to a packet so that we can store
1028 * direction tagging information.
1029 */
1030void promote_packet(libtrace_packet_t *packet);
1031
1032/** Attempts to demote a packet by removing the first header.
1033 *
1034 * @param packet        The packet to be demoted
1035 * @return True if the packet was demoted, false otherwise.
1036 *
1037 * Essentially the opposite of promote_packet, except that it will also remove
1038 * an ATM header as well as Linux SLL.
1039 *
1040 */
1041bool demote_packet(libtrace_packet_t *packet);
1042
1043/** Returns a pointer to the header following a Linux SLL header.
1044 *
1045 * @param link          A pointer to the Linux SLL header to be skipped
1046 * @param[out] arphrd_type      The arp hardware type of the packet
1047 * @param[out] next_header      The ethertype of the next header
1048 * @param[in,out] remaining     Updated with the number of captured bytes
1049 *                              remaining
1050 * @return A pointer to the header following the Linux SLL header, or NULL if
1051 * no subsequent header is present.
1052 *
1053 * Remaining must point to the number of bytes captured from the Linux SLL
1054 * header and beyond.  It will be decremented by the number of bytes skipped
1055 * to find the payload.
1056 *
1057 * If the Linux SLL header is complete but there are zero bytes of payload
1058 * after the end of the header, a pointer to where the payload would be is
1059 * returned and remaining will be set to zero. If the Linux SLL header is
1060 * incomplete (truncated), then NULL is returned and remaining will be set to
1061 * 0. Therefore, it is very important to check the value of remaining after
1062 * calling this function.
1063 */     
1064void *trace_get_payload_from_linux_sll(const void *link,
1065                uint16_t *arphrd_type, 
1066                uint16_t *next_header, 
1067                uint32_t *remaining);
1068
1069/** Returns a pointer to the header following an ATM header.
1070 *
1071 * @param link          A pointer to the ATM header to be skipped
1072 * @param[out] type     The ethertype of the next header
1073 * @param[in,out] remaining     Updated with the number of captured bytes
1074 *                              remaining
1075 * @return A pointer to the header following the ATM header, or NULL if
1076 * no subsequent header is present.
1077 *
1078 * Remaining must point to the number of bytes captured from the ATM header
1079 * and beyond.  It will be decremented by the number of bytes skipped to find
1080 * the payload.
1081 *
1082 * If the ATM header is complete but there are zero bytes of payload
1083 * after the end of the header, a pointer to where the payload would be is
1084 * returned and remaining will be set to zero. If the ATM header is
1085 * incomplete (truncated), then NULL is returned and remaining will be set to
1086 * 0. Therefore, it is very important to check the value of remaining after
1087 * calling this function.
1088 */     
1089DLLEXPORT void *trace_get_payload_from_atm(void *link, uint8_t *type, 
1090                uint32_t *remaining);
1091
1092
1093#ifdef HAVE_BPF
1094/* A type encapsulating a bpf filter
1095 * This type covers the compiled bpf filter, as well as the original filter
1096 * string
1097 *
1098 */
1099
1100/** Internal representation of a BPF filter */
1101struct libtrace_filter_t {
1102        struct bpf_program filter;      /**< The BPF program itself */
1103        char * filterstring;            /**< The filter string */
1104        int flag;                       /**< Indicates if the filter is valid */
1105        struct bpf_jit_t *jitfilter;
1106};
1107#else
1108/** BPF not supported by this system, but we still need to define a structure
1109 * for the filter */
1110struct libtrace_filter_t {};
1111#endif
1112
1113/** Local definition of a PCAP header */
1114typedef struct libtrace_pcapfile_pkt_hdr_t {
1115        uint32_t ts_sec;        /* Seconds portion of the timestamp */
1116        uint32_t ts_usec;       /* Microseconds portion of the timestamp */
1117        uint32_t caplen;        /* Capture length of the packet */
1118        uint32_t wirelen;       /* The wire length of the packet */
1119} libtrace_pcapfile_pkt_hdr_t;
1120
1121#ifdef HAVE_DAG
1122/** Constructor for the DAG format module */
1123void dag_constructor(void);
1124#endif
1125/** Constructor for the ERF format module */
1126void erf_constructor(void);
1127/** Constructor for the TSH format module */
1128void tsh_constructor(void);
1129/** Constructor for the Legacy DAG format module */
1130void legacy_constructor(void);
1131/** Constructor for the Linux Native format module */
1132void linuxnative_constructor(void);
1133/** Constructor for the PCAP format module */
1134void pcap_constructor(void);
1135/** Constructor for the PCAP File format module */
1136void pcapfile_constructor(void);
1137/** Constructor for the RT format module */
1138void rt_constructor(void);
1139/** Constructor for the DUCK format module */
1140void duck_constructor(void);
1141/** Constructor for the ATM Header format module */
1142void atmhdr_constructor(void);
1143#ifdef HAVE_BPF
1144/** Constructor for the BPF format module */
1145void bpf_constructor(void);
1146#endif
1147#if HAVE_DPDK
1148/** Constructor for Intels DPDK format module */
1149void dpdk_constructor(void);
1150#endif
1151
1152/** Extracts the RadioTap flags from a wireless link header
1153 *
1154 * @param link          A pointer to the wireless link header
1155 * @param linktype      The link type of the wireless header
1156 * @param[out] flags    Space to store the extracted flags
1157 * @return True if libtrace was able to extract flags from the link header,
1158 * false otherwise.
1159 *
1160 * This function has been left internal because it is not portable across
1161 * drivers.
1162 */
1163bool trace_get_wireless_flags(void *link, libtrace_linktype_t linktype, uint8_t *flags);
1164#define TRACE_RADIOTAP_F_FCS 0x10
1165       
1166#ifdef __cplusplus
1167}
1168#endif
1169
1170#endif /* LIBTRACE_INT_H */
Note: See TracBrowser for help on using the repository browser.