source: lib/libtrace_int.h @ f2066fa

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

Fix #5 make trace_pstart fallback to the single threaded format

If starting a parallel format fails we now retry as a single threaded format.
This fixes ring/int on older (pre 3.1 kernels) machines without PACKET_FANOUT.
This behaviour can be detected using trace_is_parallel()

  • Property mode set to 100644
File size: 42.2 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
287/** A libtrace input trace
288 * @internal
289 */
290struct libtrace_t {
291        /** The capture format for the input trace */
292        struct libtrace_format_t *format; 
293        /** Details of the most recent PACKET event reported by the trace */
294        struct libtrace_event_status_t event;
295        /** Pointer to the "global" data for the capture format module */       
296        void *format_data;             
297        /** A BPF filter to be applied to all packets read by the trace -
298         * used only if the capture format does not support filters natively */
299        struct libtrace_filter_t *filter; 
300        /** The snap length to be applied to all packets read by the trace -
301         * used only if the capture format does not support snapping natively */
302        size_t snaplen;                 
303        /** Count of the number of packets returned to the libtrace user */
304        uint64_t accepted_packets;
305        /** Count of the number of packets filtered by libtrace */
306        uint64_t filtered_packets;
307        /** The sequence is like accepted_packets but we don't reset this after a pause. */
308        uint64_t sequence_number;
309        /** The filename from the uri for the trace */
310        char *uridata;
311        /** The libtrace IO reader for this trace (if applicable) */
312        io_t *io;
313        /** Error information for the trace */
314        libtrace_err_t err;
315        /** Boolean flag indicating whether the trace has been started */
316        bool started;
317        /** Synchronise writes/reads across this format object and attached threads etc */
318        pthread_mutex_t libtrace_lock;
319        /** State */
320        enum trace_state state;
321        /** Use to control pausing threads and finishing threads etc always used with libtrace_lock */
322        pthread_cond_t perpkt_cond;
323        /** Keeps track of counts of threads in any given state */
324        int perpkt_thread_states[THREAD_STATE_MAX]; 
325
326        /** Set to indicate a perpkt's queue is full as such the writing perpkt cannot proceed */
327        bool perpkt_queue_full;
328        /** Global storage for this trace, shared among all the threads  */
329        void* global_blob;
330        /** The actual freelist */
331        libtrace_ocache_t packet_freelist;
332        /** User defined per_msg function called when a message is ready */
333        fn_cb_msg per_msg;
334        /** User defined reporter function entry point */
335        fn_reporter reporter;
336        /** The hasher function */
337        enum hasher_types hasher_type;
338        /** The hasher function - NULL implies they don't care or balance */
339        fn_hasher hasher;
340        void *hasher_data;
341        /** The pread_packet choosen path for the configuration */
342        int (*pread)(libtrace_t *, libtrace_thread_t *, libtrace_packet_t **, size_t);
343
344        libtrace_thread_t hasher_thread;
345        libtrace_thread_t reporter_thread;
346        libtrace_thread_t keepalive_thread;
347        int perpkt_thread_count;
348        libtrace_thread_t * perpkt_threads; // All our perpkt threads
349        // Used to keep track of the first packet seen on each thread
350        struct first_packets first_packets;
351        int tracetime;
352
353        /*
354         * Caches statistic counters in the case that our trace is
355         * paused or stopped before this counter is taken
356         */
357        libtrace_stat_t *stats;
358        struct user_configuration config;
359        libtrace_combine_t combiner;
360        struct {
361                fn_cb_starting message_starting;
362                fn_cb_dataless message_stopping;
363                fn_cb_dataless message_resuming;
364                fn_cb_dataless message_pausing;
365                fn_cb_packet message_packet;
366                fn_cb_first_packet message_first_packet;
367                fn_cb_tick message_tick_count;
368                fn_cb_tick message_tick_interval;
369        } callbacks;
370};
371
372#define LIBTRACE_STAT_MAGIC 0x41
373
374void trace_fin_packet(libtrace_packet_t *packet);
375void libtrace_zero_thread(libtrace_thread_t * t);
376void store_first_packet(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t);
377libtrace_thread_t * get_thread_table(libtrace_t *libtrace);
378
379
380/** A libtrace output trace
381 * @internal
382 */
383struct libtrace_out_t {
384        /** The capture format for the output trace */
385        struct libtrace_format_t *format;
386        /** Pointer to the "global" data for the capture format module */
387        void *format_data;             
388        /** The filename for the uri for the output trace */
389        char *uridata;                 
390        /** Error information for the output trace */
391        libtrace_err_t err;
392        /** Boolean flag indicating whether the trace has been started */
393        bool started;
394};
395
396/** Sets the error status on an input trace
397 *
398 * @param trace         The input trace to set the error status for
399 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
400 * @param msg           A message to print when reporting the error
401 */
402void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...) 
403
404                                                                PRINTF(3,4);
405/** Sets the error status on an output trace
406 *
407 * @param trace         The output trace to set the error status for
408 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
409 * @param msg           A message to print when reporting the error
410 */
411void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
412                                                                PRINTF(3,4);
413
414/** Clears the cached values for a libtrace packet
415 *
416 * @param packet        The libtrace packet that requires a cache reset
417 */
418void trace_clear_cache(libtrace_packet_t *packet);
419
420/** Converts the data provided in buffer into a valid libtrace packet
421 *
422 * @param trace         An input trace of the same format as the "packet"
423 *                      contained in the buffer
424 * @param packet        The libtrace packet to prepare
425 * @param buffer        A buffer containing the packet data, including the
426 *                      capture format header
427 * @param rt_type       The RT type for the packet that is being prepared
428 * @param flags         Used to specify options for the preparation function,
429 *                      e.g. who owns the packet buffer
430 *
431 * @return -1 if an error occurs, 0 otherwise
432 *
433 * Packet preparation is a tricky concept - the idea is to take the data
434 * pointed to by 'buffer' and treat it as a packet record of the same capture
435 * format as that used by the input trace. The provided libtrace packet then
436 * has its internal pointers and values set to describe the packet record in
437 * the buffer.
438 *
439 * The primary use of this function is to allow the RT packet reader to
440 * easily and safely convert packets from the RT format back into the format
441 * that they were originally captured with., essentially removing the RT
442 * encapsulation.
443 *
444 * We've decided not to make this function available via the exported API
445 * because there are several issues that can arise if it is not used very
446 * carefully and it is not very useful outside of internal contexts anyway.
447 */
448int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet,
449                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags);
450
451/** Flags for prepare_packet functions */
452enum {
453        /** The buffer memory has been allocated by libtrace and should be
454         * freed when the packet is destroyed. */
455        TRACE_PREP_OWN_BUFFER           =1,
456       
457        /** The buffer memory is externally-owned and must not be freed by
458         * libtrace when the packet is destroyed. */
459        TRACE_PREP_DO_NOT_OWN_BUFFER    =0
460};
461
462
463#ifndef PF_RULESET_NAME_SIZE
464#define PF_RULESET_NAME_SIZE 16
465#endif
466
467#ifndef IFNAMSIZ
468#define IFNAMSIZ 16
469#endif
470
471
472/** A local definition of a PFLOG header */
473typedef struct libtrace_pflog_header_t {
474        uint8_t    length;     
475        sa_family_t   af;
476        uint8_t    action;
477        uint8_t    reason;
478        char       ifname[IFNAMSIZ];
479        char       ruleset[PF_RULESET_NAME_SIZE];
480        uint32_t   rulenr;
481        uint32_t   subrulenr;
482        uint8_t    dir;
483        uint8_t    pad[3];
484} PACKED libtrace_pflog_header_t;
485
486/** A libtrace capture format module */
487/* All functions should return -1, or NULL on failure */
488struct libtrace_format_t {
489        /** The name of this module, used in the libtrace URI to identify the
490         * capture format */
491        const char *name;
492        /** The version of this module */
493        const char *version;
494        /** The RT protocol type of this module */
495        enum base_format_t type;
496
497
498        /** Given a filename, return if this is the most likely capture format
499         * (used for devices). Used to "guess" the capture format when the
500         * URI is not fully specified.
501         *
502         * @param fname         The name of the device or file to examine
503         * @return 1 if the name matches the capture format, 0 otherwise
504         */
505        int (*probe_filename)(const char *fname);
506       
507        /** Given a file, looks at the start of the file to determine if this
508         * is the capture format. Used to "guess" the capture format when the
509         * URI is not fully specified.
510         *
511         * @param io            An open libtrace IO reader for the file to check
512         * @return 1 if the file matches the capture format, 0 otherwise
513         */
514        int (*probe_magic)(io_t *io);
515
516        /** Initialises an input trace using the capture format.
517         *
518         * @param libtrace      The input trace to be initialised
519         * @return 0 if successful, -1 in the event of error
520         */
521        int (*init_input)(libtrace_t *libtrace);
522       
523        /** Applies a configuration option to an input trace.
524         *
525         * @param libtrace      The input trace to apply the option to
526         * @param option        The option that is being configured
527         * @param value         A pointer to the value that the option is to be
528         *                      set to
529         * @return 0 if successful, -1 if the option is unsupported or an error
530         * occurs
531         */
532        int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
533        /** Starts or unpauses an input trace - note that this function is
534         * often the one that opens the file or device for reading.
535         *
536         * @param libtrace      The input trace to be started or unpaused
537         * @return 0 if successful, -1 in the event of error */
538        int (*start_input)(libtrace_t *libtrace);
539
540        /** Pauses an input trace - this function should close or detach the
541         * file or device that is being read from.
542         *
543         * @param libtrace      The input trace to be paused
544         * @return 0 if successful, -1 in the event of error
545         */
546        int (*pause_input)(libtrace_t *libtrace);
547
548        /** Initialises an output trace using the capture format.
549         *
550         * @param libtrace      The output trace to be initialised
551         * @return 0 if successful, -1 in the event of error
552         */
553        int (*init_output)(libtrace_out_t *libtrace);
554       
555        /** Applies a configuration option to an output trace.
556         *
557         * @param libtrace      The output trace to apply the option to
558         * @param option        The option that is being configured
559         * @param value         A pointer to the value that the option is to be
560         *                      set to
561         * @return 0 if successful, -1 if the option is unsupported or an error
562         * occurs
563         * */
564        int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *value);
565
566        /** Starts an output trace - note that this function is often the one
567         * that opens the file or device for writing.
568         *
569         * @param libtrace      The output trace to be started
570         * @return 0 if successful, -1 if an error occurs
571         *
572         * There is no pause for output traces, as writing is not performed
573         * asynchronously.
574         */
575        int (*start_output)(libtrace_out_t *libtrace);
576
577        /** Concludes an input trace and cleans up the capture format data.
578         *
579         * @param libtrace      The input trace to be concluded
580         * @return 0 if successful, -1 if an error occurs
581         *
582         * Libtrace will call the pause_input function if the input trace is
583         * currently active prior to calling this function.
584         */
585        int (*fin_input)(libtrace_t *libtrace);
586
587        /** Concludes an output trace and cleans up the capture format data.
588         *
589         * @param libtrace      The output trace to be concluded
590         * @return 0 if successful, -1 if an error occurs
591         */
592        int (*fin_output)(libtrace_out_t *libtrace);
593
594        /** Reads the next packet from an input trace into the provided packet
595         * structure.
596         *
597         * @param libtrace      The input trace to read from
598         * @param packet        The libtrace packet to read into
599         * @return The size of the packet read (in bytes) including the capture
600         * framing header, or -1 if an error occurs. 0 is returned in the
601         * event of an EOF.
602         *
603         * If no packets are available for reading, this function should block
604         * until one appears or return 0 if the end of a trace file has been
605         * reached.
606         */
607        int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
608       
609        /** Converts a buffer containing a packet record into a libtrace packet
610         *
611         * @param libtrace      An input trace in the capture format for the
612         *                      packet
613         * @param packet        A libtrace packet to put the prepared packet
614         *                      into
615         * @param buffer        The buffer containing the packet record
616         *                      (including the capture format header)
617         * @param rt_type       The RT type for the packet
618         * @param flags         Flags describing properties that should be
619         *                      applied to the new packet
620         * @return 0 if successful, -1 if an error occurs.
621         *
622         * Updates internal trace and packet details, such as payload pointers,
623         * loss counters and packet types to match the packet record provided
624         * in the buffer. This is a zero-copy function.
625         *
626         * Intended (at this stage) only for internal use, particularly by
627         * RT which needs to decapsulate RT packets */
628        int (*prepare_packet)(libtrace_t *libtrace, libtrace_packet_t *packet,
629                        void *buffer, libtrace_rt_types_t rt_type, 
630                        uint32_t flags);
631       
632        /** Frees any resources allocated by the capture format module for a
633         * libtrace packet.
634         *
635         * @param The packet to be finalised
636         *       */
637        void (*fin_packet)(libtrace_packet_t *packet);
638
639        /** Write a libtrace packet to an output trace.
640         *
641         * @param libtrace      The output trace to write the packet to
642         * @param packet        The packet to be written out
643         * @return The number of bytes written, or -1 if an error occurs
644         */
645        int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
646        /** Returns the libtrace link type for a packet.
647         *
648         * @param packet        The packet to get the link type for
649         * @return The libtrace link type, or -1 if this link type is unknown
650         */ 
651        libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
652
653        /** Returns the direction of a packet.
654         *
655         * @param packet        The packet to get the direction for
656         * @return The direction of the packet, or -1 if no direction tag is
657         * present or an error occurs
658         */ 
659        libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
660       
661        /** Sets the direction of a packet.
662         *
663         * @param packet        The packet to set the direction for
664         * @param direction     The direction to assign to the packet
665         * @return The updated direction for the packet, or -1 if an error
666         * occurs
667         *
668         * @note Some capture formats do not feature direction tagging, so it
669         * will not make sense to implement a set_direction function for them.
670         */ 
671        libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
672       
673        /** Returns the timestamp for a packet in the ERF timestamp format.
674         *
675         * @param packet        The packet to get the timestamp from
676         * @return The 64-bit ERF timestamp
677         *
678         * @note Each format must implement at least one of the four "get
679         * timestamp" functions.
680         *
681         * If not implemented, libtrace will convert the result of one of the
682         * other timestamp functions into the appropriate format instead.
683         * This means each capture format only needs to implement the most
684         * sensible of the four and let libtrace handle any conversions.
685         *
686         */
687        uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
688
689        /** Returns the timestamp for a packet in the timeval format
690         *
691         * @param packet        The packet to get the timestamp from
692         * @return The timestamp from the packet as a timeval
693         *
694         * @note Each format must implement at least one of the four "get
695         * timestamp" functions.
696         *
697         * If not implemented, libtrace will convert the result of one of the
698         * other timestamp functions into the appropriate format instead.
699         * This means each capture format only needs to implement the most
700         * sensible of the four and let libtrace handle any conversions.
701         */
702        struct timeval (*get_timeval)(const libtrace_packet_t *packet);
703       
704        /** Returns the timestamp for a packet in the timespec format.
705         *
706         * @param packet        The packet to get the timestamp from
707         * @return The timestamp from the packet as a timespec
708         *
709         * @note Each format must implement at least one of the four "get
710         * timestamp" functions.
711         *
712         * If not implemented, libtrace will convert the result of one of the
713         * other timestamp functions into the appropriate format instead.
714         * This means each capture format only needs to implement the most
715         * sensible of the four and let libtrace handle any conversions.
716         */
717        struct timespec (*get_timespec)(const libtrace_packet_t *packet);
718       
719        /** Returns the timestamp for a packet in floating point seconds.
720         *
721         * @param packet        The packet to get the timestamp from
722         * @return The timestamp from the packet as a floating point number of
723         * seconds since 1970-01-01 00:00:00 UTC
724         *
725         * @note Each format must implement at least one of the four "get
726         * timestamp" functions.
727         *
728         * If not implemented, libtrace will convert the result of one of the
729         * other timestamp functions into the appropriate format instead.
730         * This means each capture format only needs to implement the most
731         * sensible of the four and let libtrace handle any conversions.
732         */
733        double (*get_seconds)(const libtrace_packet_t *packet);
734       
735        /** Moves the read pointer to a certain ERF timestamp within an input
736         * trace file.
737         *
738         * @param trace         The input trace to seek within
739         * @param timestamp     The timestamp to seek to, as an ERF timestamp
740         *
741         * @return 0 on success, -1 on failure.
742         *
743         * The next packet read from this trace will now be the first packet
744         * to have a timestamp equal to or greater than the provided timestamp.
745         *
746         * @note Each format that supports seeking must implement at least one
747         * of the seek functions.
748         *
749         * If not implemented, libtrace will convert the timestamp into the
750         * appropriate format to use a seek function that has been implemented.
751         * This means each capture format only needs to implement the seek
752         * function that matches the native timestamp format for that capture.
753         *
754         */
755        int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
756        /** Moves the read pointer to a certain timestamp represented using a
757         * timeval within an input trace file.
758         *
759         * @param trace         The input trace to seek within
760         * @param timestamp     The timestamp to seek to, as a timeval
761         *
762         * @return 0 on success, -1 on failure.
763         *
764         * The next packet read from this trace will now be the first packet
765         * to have a timestamp equal to or greater than the provided timestamp.
766         *
767         * @note Each format that supports seeking must implement at least one
768         * of the seek functions.
769         *
770         * If not implemented, libtrace will convert the timestamp into the
771         * appropriate format to use a seek function that has been implemented.
772         * This means each capture format only needs to implement the seek
773         * function that matches the native timestamp format for that capture.
774         *
775         */
776        int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
777       
778        /** Moves the read pointer to a certain timestamp represented using
779         * floating point seconds within an input trace file.
780         *
781         * @param trace         The input trace to seek within
782         * @param timestamp     The timestamp to seek to, as floating point
783         *                      seconds since 1970-01-01 00:00:00 UTC
784         *
785         * @return 0 on success, -1 on failure.
786         *
787         * The next packet read from this trace will now be the first packet
788         * to have a timestamp equal to or greater than the provided timestamp.
789         *
790         * @note Each format that supports seeking must implement at least one
791         * of the seek functions.
792         *
793         * If not implemented, libtrace will convert the timestamp into the
794         * appropriate format to use a seek function that has been implemented.
795         * This means each capture format only needs to implement the seek
796         * function that matches the native timestamp format for that capture.
797         *
798         */
799        int (*seek_seconds)(libtrace_t *trace, double seconds);
800       
801        /** Returns the payload length of the captured packet record.
802         *
803         * @param packet        The packet to get the capture length from
804         * @return The capture length for the packet, or -1 if an error occurs
805         *
806         * Capture length is the current size of the packet record itself,
807         * following any truncation that may have occurred during the capture
808         * process. This length does not include the capture format framing
809         * header.
810         */
811        int (*get_capture_length)(const libtrace_packet_t *packet);
812
813        /** Returns the original length of the packet as it was on the wire.
814         *
815         * @param packet        The packet to get the wire length from
816         * @return The length of the packet on the wire at the time of capture,
817         * or -1 if an error occurs
818         *
819         * Wire length is the original size of the packet prior to any
820         * truncation that may have occurred as part of the capture process.
821         * This length does not include the capture format framing header.
822         */
823        int (*get_wire_length)(const libtrace_packet_t *packet);
824       
825        /** Returns the length of the capture format framing header
826         *
827         * @param packet        The packet to get the framing length from
828         * @return The length of the framing header, or -1 if an error occurs
829         *
830         * The framing header is the extra metadata that the capture process
831         * records about a packet.  The framing length does not include any
832         * of the packet payload itself. The total size of the packet record
833         * can be calculated be adding this value with the capture length.
834         */
835        int (*get_framing_length)(const libtrace_packet_t *packet);
836
837        /** Sets the capture length for a packet.
838         *
839         * @param packet        The packet to adjust the capture length for.
840         * @param size          The new capture length
841         * @return The new capture length of the packet, or -1 if an error
842         * occurs
843         *
844         * @note This function should only reduce the capture length. If the
845         * provided length is larger than the current capture length, -1 should
846         * be returned.
847         */
848        size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
849        /** Returns the number of packets observed by an input trace.
850         *
851         * @param trace         The input trace to get the packet count for
852         * @return The number of packets observed by an input trace, or
853         * UINT64_MAX if the number is unknown
854         *
855         * This count includes packets that have been filtered and dropped.
856         */
857        uint64_t (*get_received_packets)(libtrace_t *trace);
858
859        /** Returns the number of packets filtered by an input trace.
860         *
861         * @param trace         The input trace to get the filtered count for
862         * @return The number of packets filtered by the input trace, or
863         * UINT64_MAX if the number is unknown
864         *
865         */
866        uint64_t (*get_filtered_packets)(libtrace_t *trace);
867       
868        /** Returns the number of packets dropped by an input trace.
869         *
870         * @param trace         The input trace to get the dropped count for
871         * @return The number of packets dropped by the input trace, or
872         * UINT64_MAX if the number is unknown
873         *
874         */
875        uint64_t (*get_dropped_packets)(libtrace_t *trace);
876
877        /** Returns statistics about a trace.
878         *
879         * @param trace The libtrace object
880         * @param stat [in,out] A statistics structure ready to be filled
881         *
882         * The filtered and accepted statistics will be set to the values
883         * stored in the library. All other statistics are not set.
884         *
885         * @note If filtering of packets is performed by a trace and the number
886         * of filtered packets is unknown this should be marked as invalid by
887         * the format.
888         */
889        void (*get_statistics)(libtrace_t *trace, libtrace_stat_t *stat);
890       
891        /** Returns the file descriptor used by the input trace.
892         *
893         * @param trace         The input trace to get the file descriptor for
894         * @return The file descriptor used by the input trace to read packets
895         *
896         */
897        int (*get_fd)(const libtrace_t *trace);
898       
899        /** Returns the next libtrace event for the input trace.
900         *
901         * @param trace         The input trace to get the next event from
902         * @param packet        A libtrace packet to read a packet into
903         * @return A libtrace event describing the event that occured
904         *
905         * The event API allows for non-blocking reading of packets from an
906         * input trace. If a packet is available and ready to be read, a packet
907         * event should be returned. Otherwise a sleep or fd event should be
908         * returned to indicate that the caller needs to wait. If the input
909         * trace has an error or reaches EOF, a terminate event should be
910         * returned.
911         */
912        struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);       
913
914        /** Prints some useful help information to standard output. */
915        void (*help)(void);
916       
917        /** Next pointer, should always be NULL - used by the format module
918         * manager. */
919        struct libtrace_format_t *next;
920
921        /** Holds information about the trace format */
922        struct libtrace_info_t info;
923
924        /**
925         * Starts or unpauses an input trace in parallel mode - note that
926         * this function is often the one that opens the file or device for
927         * reading.
928         *
929         * @param libtrace      The input trace to be started or unpaused
930         * @return 0 upon success.
931         *         Otherwise in event of an error -1 is returned.
932         *
933         */
934        int (*pstart_input)(libtrace_t *trace);
935       
936        /**
937         * Read a batch of packets from the input stream related to thread.
938         * At most read nb_packets, however should return with less if packets
939         * are not waiting. However still must return at least 1, 0 still indicates
940         * EOF.
941         *
942         * @param libtrace      The input trace
943         * @param t     The thread
944         * @param packets       An array of packets
945         * @param nb_packets    The number of packets in the array (the maximum to read)
946         * @return The number of packets read, or 0 in the case of EOF or -1 in error or -2 to represent
947         * interrupted due to message waiting before packets had been read.
948         */
949        int (*pread_packets)(libtrace_t *trace, libtrace_thread_t *t, libtrace_packet_t **packets, size_t nb_packets);
950       
951        /** Pause a parallel trace
952         *
953         * @param libtrace      The input trace to be paused
954         */
955        int (*ppause_input)(libtrace_t *trace);
956       
957        /** Called after all threads have been paused, Finish (close) a parallel trace
958         *
959         * @param libtrace      The input trace to be stopped
960         */
961        int (*pfin_input)(libtrace_t *trace);
962
963        /**
964         * Register a thread for use with the format or using the packets produced
965         * by it. This is NOT only used for threads reading packets in fact all
966         * threads use this.
967         *
968         * The libtrace lock is not held by this format but can be aquired
969         * by the format.
970         *
971         * Some use cases include setting up any thread local storage required for
972         * to read packets and free packets. For DPDK we require any thread that
973         * may release or read a packet to have have an internal number associated
974         * with it.
975         *
976         * The thread type can be used to see if this thread is going to be used
977         * to read packets or otherwise.
978         *
979         * @return 0 if successful, -1 if the option is unsupported or an error
980         * occurs (such as a maximum of threads being reached)
981         */
982        int (*pregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t, bool reader);
983
984        /**
985         * If needed any memory allocated with pregister_thread can be released
986         * in this function. The thread will be destroyed directly after this
987         * function is called.
988         */
989        void (*punregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t);
990
991        /** Returns statistics for a single thread.
992         *
993         * @param trace The libtrace object
994         * @param t The thread to return statistics for
995         * @param stat [in,out] A statistics structure ready to be filled
996         *
997         * The filtered and accepted statistics will be set to the values
998         * stored in the library. All other statistics are not set.
999         *
1000         * @note If filtering of packets is performed by a trace and the number
1001         * of filtered packets is unknown this should be marked as invalid by
1002         * the format.
1003         */
1004        void (*get_thread_statistics)(libtrace_t *libtrace,
1005                                      libtrace_thread_t *t,
1006                                      libtrace_stat_t *stat);
1007};
1008
1009/** Macro to zero out a single thread format */
1010#define NON_PARALLEL(live) \
1011        {live, 1},              /* trace info */ \
1012        NULL,                   /* pstart_input */ \
1013        NULL,                   /* pread_packet */ \
1014        NULL,                   /* ppause_input */ \
1015        NULL,                   /* pfin_input */ \
1016        NULL,                   /* pregister_thread */ \
1017        NULL,                   /* punregister_thread */ \
1018        NULL,                   /* get_thread_statistics */
1019
1020/** The list of registered capture formats */
1021//extern struct libtrace_format_t *form;
1022
1023/** Specifies whether any blocking packet readers should cease reading
1024 * immediately
1025 */
1026extern volatile int libtrace_halt;
1027
1028/** Registers a new capture format module.
1029 *
1030 * @param format        The format module to be registered
1031 */
1032void register_format(struct libtrace_format_t *format);
1033
1034/** Converts a PCAP DLT into a libtrace link type.
1035 *
1036 * @param linktype      The PCAP DLT to be converted
1037 * @return The libtrace link type that is equivalent to the provided DLT, or
1038 * -1 if the DLT is unknown
1039 */
1040libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype);
1041
1042/** Converts a PCAP DLT into an RT protocol type.
1043 *
1044 * @param linktype      The PCAP DLT to be converted
1045 * @return The RT type that is equivalent to the provided DLT
1046 */
1047libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype);
1048
1049/** Converts a libtrace link type into a PCAP linktype.
1050 *
1051 * @param type          The libtrace link type to be converted
1052 * @return The PCAP linktype that is equivalent to the provided libtrace link
1053 * type, or -1 if the link type is unknown
1054 */
1055libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type);
1056
1057/** Converts a libtrace link type into a PCAP DLT.
1058 *
1059 * @param type          The libtrace link type to be converted
1060 * @return The PCAP DLT that is equivalent to the provided libtrace link
1061 * type, or -1 if the link type is unknown
1062 */
1063libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
1064
1065/** Converts an RT protocol type into a PCAP DLT.
1066 *
1067 * @param rt_type       The RT type to be converted
1068 * @return The PCAP DLT that is equivalent to the provided RT protocol
1069 */
1070libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type);
1071
1072/** Converts a PCAP DLT into an RT protocol type for the BPF format.
1073 *
1074 * @param linktype      The PCAP DLT to be converted
1075 * @return The RT type that is equivalent to the provided DLT for BPF
1076 */
1077libtrace_rt_types_t bpf_linktype_to_rt(libtrace_dlt_t linktype);
1078
1079/** Converts an ERF type into a libtrace link type.
1080 *
1081 * @param erf           The ERF type to be converted
1082 * @return The libtrace link type that is equivalent to the provided ERF type,
1083 * or -1 if the ERF type is unknown
1084 */
1085libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
1086
1087/** Converts a libtrace link type into an ERF type.
1088 *
1089 * @param linktype      The libtrace link type to be converted
1090 * @return The ERF type that is equivalent to the provided libtrace link type,
1091 * or -1 if the link type cannot be matched to an ERF type.
1092 */
1093uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
1094
1095/** Converts an ARPHRD type into a libtrace link type.
1096 *
1097 * @param arphrd        The ARPHRD type to be converted
1098 * @return The libtrace link type that is equivalent to the provided ARPHRD
1099 * type, or -1 if the ARPHRD type is unknown
1100 */
1101libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd);
1102
1103/** Converts a libtrace link type into an ARPHRD type.
1104 *
1105 * @param type          The libtrace link type to be converted
1106 * @return The ARPHRD type that is equivalent to the provided libtrace link
1107 * type, or -1 if the link type cannot be matched to an ARPHRD type
1108 */
1109unsigned int libtrace_to_arphrd_type(libtrace_linktype_t type);
1110
1111/** Converts a libtrace packet to the Linux SLL type.
1112 *
1113 * @param packet        The packet to be promoted
1114 *
1115 * @note This will involve memcpy() so use sparingly.
1116 *
1117 * This function prepends a Linux SLL header to a packet so that we can store
1118 * direction tagging information.
1119 */
1120void promote_packet(libtrace_packet_t *packet);
1121
1122/** Attempts to demote a packet by removing the first header.
1123 *
1124 * @param packet        The packet to be demoted
1125 * @return True if the packet was demoted, false otherwise.
1126 *
1127 * Essentially the opposite of promote_packet, except that it will also remove
1128 * an ATM header as well as Linux SLL.
1129 *
1130 */
1131bool demote_packet(libtrace_packet_t *packet);
1132
1133/** Returns a pointer to the header following a Linux SLL header.
1134 *
1135 * @param link          A pointer to the Linux SLL header to be skipped
1136 * @param[out] arphrd_type      The arp hardware type of the packet
1137 * @param[out] next_header      The ethertype of the next header
1138 * @param[in,out] remaining     Updated with the number of captured bytes
1139 *                              remaining
1140 * @return A pointer to the header following the Linux SLL header, or NULL if
1141 * no subsequent header is present.
1142 *
1143 * Remaining must point to the number of bytes captured from the Linux SLL
1144 * header and beyond.  It will be decremented by the number of bytes skipped
1145 * to find the payload.
1146 *
1147 * If the Linux SLL header is complete but there are zero bytes of payload
1148 * after the end of the header, a pointer to where the payload would be is
1149 * returned and remaining will be set to zero. If the Linux SLL header is
1150 * incomplete (truncated), then NULL is returned and remaining will be set to
1151 * 0. Therefore, it is very important to check the value of remaining after
1152 * calling this function.
1153 */     
1154void *trace_get_payload_from_linux_sll(const void *link,
1155                uint16_t *arphrd_type, 
1156                uint16_t *next_header, 
1157                uint32_t *remaining);
1158
1159/** Returns a pointer to the header following an ATM header.
1160 *
1161 * @param link          A pointer to the ATM header to be skipped
1162 * @param[out] type     The ethertype of the next header
1163 * @param[in,out] remaining     Updated with the number of captured bytes
1164 *                              remaining
1165 * @return A pointer to the header following the ATM header, or NULL if
1166 * no subsequent header is present.
1167 *
1168 * Remaining must point to the number of bytes captured from the ATM header
1169 * and beyond.  It will be decremented by the number of bytes skipped to find
1170 * the payload.
1171 *
1172 * If the ATM header is complete but there are zero bytes of payload
1173 * after the end of the header, a pointer to where the payload would be is
1174 * returned and remaining will be set to zero. If the ATM header is
1175 * incomplete (truncated), then NULL is returned and remaining will be set to
1176 * 0. Therefore, it is very important to check the value of remaining after
1177 * calling this function.
1178 */     
1179DLLEXPORT void *trace_get_payload_from_atm(void *link, uint8_t *type, 
1180                uint32_t *remaining);
1181
1182
1183#ifdef HAVE_BPF
1184/* A type encapsulating a bpf filter
1185 * This type covers the compiled bpf filter, as well as the original filter
1186 * string
1187 *
1188 */
1189
1190/** Internal representation of a BPF filter */
1191struct libtrace_filter_t {
1192        struct bpf_program filter;      /**< The BPF program itself */
1193        char * filterstring;            /**< The filter string */
1194        int flag;                       /**< Indicates if the filter is valid */
1195        struct bpf_jit_t *jitfilter;
1196};
1197#else
1198/** BPF not supported by this system, but we still need to define a structure
1199 * for the filter */
1200struct libtrace_filter_t {};
1201#endif
1202
1203/** Local definition of a PCAP header */
1204typedef struct libtrace_pcapfile_pkt_hdr_t {
1205        uint32_t ts_sec;        /* Seconds portion of the timestamp */
1206        uint32_t ts_usec;       /* Microseconds portion of the timestamp */
1207        uint32_t caplen;        /* Capture length of the packet */
1208        uint32_t wirelen;       /* The wire length of the packet */
1209} libtrace_pcapfile_pkt_hdr_t;
1210
1211#ifdef HAVE_DAG
1212/** Constructor for the DAG format module */
1213void dag_constructor(void);
1214#endif
1215/** Constructor for the ERF format module */
1216void erf_constructor(void);
1217/** Constructor for the TSH format module */
1218void tsh_constructor(void);
1219/** Constructor for the Legacy DAG format module */
1220void legacy_constructor(void);
1221/** Constructor for the Linux Native format module */
1222void linuxnative_constructor(void);
1223/** Constructor for the Linux Ring format module */
1224void linuxring_constructor(void);
1225/** Constructor for the PCAP format module */
1226void pcap_constructor(void);
1227/** Constructor for the PCAP File format module */
1228void pcapfile_constructor(void);
1229/** Constructor for the RT format module */
1230void rt_constructor(void);
1231/** Constructor for the DUCK format module */
1232void duck_constructor(void);
1233/** Constructor for the ATM Header format module */
1234void atmhdr_constructor(void);
1235#ifdef HAVE_BPF
1236/** Constructor for the BPF format module */
1237void bpf_constructor(void);
1238#endif
1239#if HAVE_DPDK
1240/** Constructor for Intels DPDK format module */
1241void dpdk_constructor(void);
1242#endif
1243
1244/** Extracts the RadioTap flags from a wireless link header
1245 *
1246 * @param link          A pointer to the wireless link header
1247 * @param linktype      The link type of the wireless header
1248 * @param[out] flags    Space to store the extracted flags
1249 * @return True if libtrace was able to extract flags from the link header,
1250 * false otherwise.
1251 *
1252 * This function has been left internal because it is not portable across
1253 * drivers.
1254 */
1255bool trace_get_wireless_flags(void *link, libtrace_linktype_t linktype, uint8_t *flags);
1256#define TRACE_RADIOTAP_F_FCS 0x10
1257       
1258#ifdef __cplusplus
1259}
1260#endif
1261
1262#endif /* LIBTRACE_INT_H */
Note: See TracBrowser for help on using the repository browser.