source: lib/libtrace_int.h @ c723e9e

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

Remove unused code get_thread_table_num()

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