Changeset 5511c14 for lib/format_erf.c


Ignore:
Timestamp:
02/08/10 14:11:04 (12 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
238d50a
Parents:
81c0b9e
Message:
  • Tidied up the format data structures in format_erf. In particular, removed unnecessary unions.
  • Updated licensing and documentation for format_duck and format_erf
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/format_erf.c

    r3d9cf9f r5511c14  
    22 * This file is part of libtrace
    33 *
    4  * Copyright (c) 2007,2008 The University of Waikato, Hamilton, New Zealand.
     4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
     5 * New Zealand.
     6 *
    57 * Authors: Daniel Lawson
    6  *          Perry Lorier
     8 *          Perry Lorier
     9 *          Shane Alcock
    710 *         
    811 * All rights reserved.
     
    2831 *
    2932 */
     33
     34
    3035#define _GNU_SOURCE
    3136
     
    5762#endif
    5863
    59 
    60 #define COLLECTOR_PORT 3435
     64/* This format module deals with reading and writing ERF traces. ERF is the
     65 * trace format developed by Endace for use by DAG hardware capture cards.
     66 *
     67 * ERF is not a live capture format.
     68 *
     69 */
     70
    6171
    6272static struct libtrace_format_t erfformat;
     
    6676
    6777#define IN_OPTIONS DATA(libtrace)->options
    68 #define OUTPUT DATAOUT(libtrace)->output
     78#define OUTPUT DATAOUT(libtrace)
    6979#define OUT_OPTIONS DATAOUT(libtrace)->options
     80
     81/* "Global" data that is stored for each ERF input trace */
    7082struct erf_format_data_t {
    7183       
     84        /* Index used for seeking within a trace */
    7285        struct {
     86                /* The index itself */
    7387                io_t *index;
     88                /* The offset of the index */
    7489                off_t index_len;
     90                /* Indicates the existence of an index */
    7591                enum { INDEX_UNKNOWN=0, INDEX_NONE, INDEX_EXISTS } exists;
    7692        } seek;
    7793
     94        /* Number of packets that were dropped during the capture */
    7895        uint64_t drops;
     96
     97        /* Config options for the input trace */
    7998        struct {
     99                /* Flag indicating whether the event API should replicate the
     100                 * time gaps between each packet or return a PACKET event for
     101                 * each packet */
    80102                int real_time;
    81103        } options;
    82104};
    83105
     106/* "Global" data that is stored for each ERF output trace */
    84107struct erf_format_data_out_t {
    85         union {
    86                 struct {
    87                         int level;
    88                         int fileflag;
    89                 } erf;
    90                
     108
     109        /* Config options for the output trace */
     110        struct {
     111                /* Compression level for the output file */
     112                int level;
     113                /* File flags used to open the file, e.g. O_CREATE */
     114                int fileflag;
    91115        } options;
    92        
    93         union {
    94                 int fd;
    95                 struct rtserver_t * rtserver;
    96                 iow_t *file;
    97         } output;
     116
     117        /* The output file itself */
     118        iow_t *file;
     119       
    98120};
    99 
    100 /** Structure holding status information for a packet */
    101 typedef struct libtrace_packet_status {
    102         uint8_t type;
    103         uint8_t reserved;
    104         uint16_t message;
    105 } libtrace_packet_status_t;
    106121
    107122typedef struct erf_index_t {
     
    111126
    112127
    113 /* Dag erf ether packets have a 2 byte padding before the packet
    114  * so that the ip header is aligned on a 32 bit boundary.
     128/* Ethernet packets have a 2 byte padding before the packet
     129 * so that the IP header is aligned on a 32 bit boundary.
    115130 */
    116131static int erf_get_padding(const libtrace_packet_t *packet)
     
    138153}
    139154
     155/* Attempts to determine whether a given trace file is using the ERF format
     156 *
     157 * Returns 1 if the trace is probably ERF, 0 otherwise
     158 */
    140159static int erf_probe_magic(io_t *io)
    141160{
     
    278297}
    279298
     299/* Seek within an ERF trace based on an ERF timestamp */
    280300static int erf_seek_erf(libtrace_t *libtrace,uint64_t erfts)
    281301{
     
    328348        libtrace->format_data = malloc(sizeof(struct erf_format_data_out_t));
    329349
    330         OUT_OPTIONS.erf.level = 0;
    331         OUT_OPTIONS.erf.fileflag = O_CREAT | O_WRONLY;
    332         OUTPUT.file = 0;
     350        OUT_OPTIONS.level = 0;
     351        OUT_OPTIONS.fileflag = O_CREAT | O_WRONLY;
     352        OUTPUT->file = 0;
    333353
    334354        return 0;
    335355}
    336356
    337 static int erf_config_output(libtrace_out_t *libtrace, trace_option_output_t option,
    338                 void *value) {
     357static int erf_config_output(libtrace_out_t *libtrace,
     358                trace_option_output_t option, void *value) {
    339359
    340360        switch (option) {
    341361                case TRACE_OPTION_OUTPUT_COMPRESS:
    342                         OUT_OPTIONS.erf.level = *(int*)value;
     362                        OUT_OPTIONS.level = *(int*)value;
    343363                        return 0;
    344364                case TRACE_OPTION_OUTPUT_FILEFLAGS:
    345                         OUT_OPTIONS.erf.fileflag = *(int*)value;
     365                        OUT_OPTIONS.fileflag = *(int*)value;
    346366                        return 0;
    347367                default:
     
    363383
    364384static int erf_fin_output(libtrace_out_t *libtrace) {
    365         if (OUTPUT.file)
    366                 wandio_wdestroy(OUTPUT.file);
     385        if (OUTPUT->file)
     386                wandio_wdestroy(OUTPUT->file);
    367387        free(libtrace->format_data);
    368388        return 0;
     
    391411        if (erfptr->flags.rxerror == 1) {
    392412                packet->payload = NULL;
    393                 /* XXX: Do we want to do this? We never used to do this
    394                  
    395                 erfptr->rlen = htons(erf_get_framing_length(packet));
    396                 */
    397413        } else {
    398414                packet->payload = (char*)packet->buffer + erf_get_framing_length(packet);
     
    452468
    453469        if (size >= LIBTRACE_PACKET_BUFSIZE) {
    454                 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Packet size %u larger than supported by libtrace - packet is probably corrupt", size);
     470                trace_set_err(libtrace, TRACE_ERR_BAD_PACKET,
     471                                "Packet size %u larger than supported by libtrace - packet is probably corrupt",
     472                                size);
    455473                return -1;
    456474        }
    457475
    458476        /* Unknown/corrupt */
    459         if (((dag_record_t *)packet->buffer)->type >= TYPE_AAL2) {
    460                 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Corrupt or Unknown ERF type");
     477        if (((dag_record_t *)packet->buffer)->type >= TYPE_RAW_LINK) {
     478                trace_set_err(libtrace, TRACE_ERR_BAD_PACKET,
     479                                "Corrupt or Unknown ERF type");
    461480                return -1;
    462481        }
     
    467486                                        (size_t)size)) != (int)size) {
    468487                if (numbytes==-1) {
    469                         trace_set_err(libtrace,errno, "read(%s)", libtrace->uridata);
     488                        trace_set_err(libtrace,errno, "read(%s)",
     489                                        libtrace->uridata);
    470490                        return -1;
    471491                }
    472                 trace_set_err(libtrace,EIO,"Truncated packet (wanted %d, got %d)", size, numbytes);
     492                trace_set_err(libtrace,EIO,
     493                                "Truncated packet (wanted %d, got %d)",
     494                                size, numbytes);
    473495                /* Failed to read the full packet?  must be EOF */
    474496                return -1;
    475497        }
    476498       
    477         if (erf_prepare_packet(libtrace, packet, packet->buffer, TRACE_RT_DATA_ERF, flags))
     499        if (erf_prepare_packet(libtrace, packet, packet->buffer,
     500                                TRACE_RT_DATA_ERF, flags))
    478501                return -1;
    479502       
     
    487510
    488511        if ((numbytes =
    489                 wandio_wwrite(OUTPUT.file,
     512                wandio_wwrite(OUTPUT->file,
    490513                                erfptr,
    491514                                (size_t)(dag_record_size + pad)))
     
    497520
    498521        size=ntohs(erfptr->rlen)-(dag_record_size+pad);
    499         numbytes=wandio_wwrite(OUTPUT.file, buffer, (size_t)size);
     522        numbytes=wandio_wwrite(OUTPUT->file, buffer, (size_t)size);
    500523        if (numbytes != size) {
    501524                trace_set_err_out(libtrace,errno,
     
    508531static int erf_start_output(libtrace_out_t *libtrace)
    509532{
    510         OUTPUT.file = trace_open_file_out(libtrace,
    511                         OUT_OPTIONS.erf.level,
    512                         OUT_OPTIONS.erf.fileflag);
    513         if (!OUTPUT.file) {
     533        OUTPUT->file = trace_open_file_out(libtrace,
     534                        OUT_OPTIONS.level,
     535                        OUT_OPTIONS.fileflag);
     536        if (!OUTPUT->file) {
    514537                return -1;
    515538        }
     
    550573        void *payload = packet->payload;
    551574
    552         assert(OUTPUT.file);
     575        assert(OUTPUT->file);
    553576
    554577        if (!packet->header) {
     
    579602        } else {
    580603                dag_record_t erfhdr;
     604                int rlen;
    581605                /* convert format - build up a new erf header */
    582606                /* Timestamp */
     
    601625                assert(erf_get_framing_length(packet)>0
    602626                                && trace_get_framing_length(packet)<=65536);
    603                 assert(
    604                         trace_get_capture_length(packet)+erf_get_framing_length(packet)>0
    605                       &&trace_get_capture_length(packet)+erf_get_framing_length(packet)<=65536);
    606                 erfhdr.rlen = htons(trace_get_capture_length(packet)
    607                         + erf_get_framing_length(packet));
     627
     628                rlen = trace_get_capture_length(packet) +
     629                                erf_get_framing_length(packet);
     630                assert(rlen > 0 && rlen <= 65536);
     631                erfhdr.rlen = htons(rlen);
    608632                /* loss counter. Can't do this */
    609633                erfhdr.lctr = 0;
     
    676700        assert(packet);
    677701        if(size  > trace_get_capture_length(packet)) {
    678                 /* can't make a packet larger */
     702                /* Can't make a packet larger */
    679703                return trace_get_capture_length(packet);
    680704        }
     
    691715        struct libtrace_eventobj_t event = {0,0,0.0,0};
    692716       
     717        /* If we are being told to replay packets as fast as possible, then
     718         * we just need to read and return the next packet in the trace */
    693719        if (IN_OPTIONS.real_time) {
    694720                event.size = erf_read_packet(libtrace, packet);
     
    700726               
    701727        } else {
     728                /* Otherwise, use the generic event function */
    702729                return trace_event_trace(libtrace, packet);
    703730        }
     
    728755        printf("\n");
    729756        printf("\te.g.: erf:/tmp/trace\n");
    730         printf("\n");
    731         printf("Supported output options:\n");
    732         printf("\t-z\tSpecify the gzip compression, ranging from 0 (uncompressed) to 9 - defaults to 1\n");
    733757        printf("\n");
    734758
Note: See TracChangeset for help on using the changeset viewer.