Changeset 43c00e5


Ignore:
Timestamp:
02/04/10 14:31:38 (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:
8488c15
Parents:
5952ff0
Message:
  • Updated documentation and licensing on DAG 2.5 format module
  • Also removed a few variables from the output format data that were not being used for anything important
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/format_dag25.c

    rcce868c r43c00e5  
    22 * This file is part of libtrace
    33 *
    4  * Copyright (c) 2007,2008 The University of Waikato, Hamilton, New Zealand.
    5  * Authors: Daniel Lawson
     4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
     5 * New Zealand.
     6 *
     7 * Authors: Daniel Lawson
    68 *          Perry Lorier
    7  *          Shane Alcock
    8  *
     9 *          Shane Alcock
     10 *         
    911 * All rights reserved.
    1012 *
    11  * This code has been developed by the University of Waikato WAND
     13 * This code has been developed by the University of Waikato WAND 
    1214 * research group. For further information please see http://www.wand.net.nz/
    1315 *
     
    2931 *
    3032 */
     33
    3134#define _GNU_SOURCE
    3235
     
    6467#endif
    6568
     69/* This format deals with DAG cards that are using drivers from the 2.5 version
     70 * onwards, including 3.X.
     71 *
     72 * DAG is a LIVE capture format.
     73 *
     74 * This format does support writing, provided the DAG card that you are using
     75 * has transmit (Tx) support. Additionally, packets read using this format
     76 * are in the ERF format, so can easily be written as ERF traces without
     77 * losing any data.
     78 */
     79
    6680
    6781#define DATA(x) ((struct dag_format_data_t *)x->format_data)
     
    7488static struct libtrace_format_t dag;
    7589
     90/* A DAG device - a DAG device can support multiple streams (and therefore
     91 * multiple input traces) so each trace needs to refer to a device */
    7692struct dag_dev_t {
    77         //pthread_mutex_t dag_mutex;
    78         char * dev_name;
    79         int fd;
    80         uint16_t ref_count;
    81         struct dag_dev_t *prev;
    82         struct dag_dev_t *next;
     93        char * dev_name;                /* Device name */
     94        int fd;                         /* File descriptor */
     95        uint16_t ref_count;             /* Number of input / output traces
     96                                           that are using this device */
     97        struct dag_dev_t *prev;         /* Pointer to the previous device in
     98                                           the device list */
     99        struct dag_dev_t *next;         /* Pointer to the next device in the
     100                                           device list */
    83101};
    84102
     103/* "Global" data that is stored for each DAG output trace */
    85104struct dag_format_data_out_t {
     105        /* The DAG device being used for writing */
    86106        struct dag_dev_t *device;
     107        /* The DAG stream that is being written on */
    87108        unsigned int dagstream;
     109        /* Boolean flag indicating whether the stream is currently attached */
    88110        int stream_attached;
    89         uint8_t *bottom;
    90         uint8_t *top;
    91         uint32_t processed;
    92         uint64_t drops;
     111        /* The amount of data waiting to be transmitted, in bytes */
    93112        uint64_t waiting;
     113        /* A buffer to hold the data to be transmittted */
    94114        uint8_t *txbuffer;
    95115};
    96116
     117/* "Global" data that is stored for each DAG input trace */
    97118struct dag_format_data_t {
     119
     120        /* Data required for regular DUCK reporting */
    98121        struct {
     122                /* Timestamp of the last DUCK report */
    99123                uint32_t last_duck;
     124                /* The number of seconds between each DUCK report */
    100125                uint32_t duck_freq;
     126                /* Timestamp of the last packet read from the DAG card */
    101127                uint32_t last_pkt;
     128                /* Dummy trace to ensure DUCK packets are dealt with using the
     129                 * DUCK format functions */
    102130                libtrace_t *dummy_duck;
    103131        } duck;
    104132
     133        /* The DAG device that we are reading from */
    105134        struct dag_dev_t *device;
     135        /* The DAG stream that we are reading from */
    106136        unsigned int dagstream;
     137        /* Boolean flag indicating whether the stream is currently attached */
    107138        int stream_attached;
     139        /* Pointer to the first unread byte in the DAG memory hole */
    108140        uint8_t *bottom;
     141        /* Pointer to the last unread byte in the DAG memory hole */
    109142        uint8_t *top;
     143        /* The amount of data processed thus far from the bottom pointer */
    110144        uint32_t processed;
     145        /* The number of packets that have been dropped */
    111146        uint64_t drops;
    112147};
    113148
     149/* To be thread-safe, we're going to need a mutex for operating on the list
     150 * of DAG devices */
    114151pthread_mutex_t open_dag_mutex;
     152
     153/* The list of DAG devices that have been opened by libtrace.
     154 *
     155 * We can only open each DAG device once, but we might want to read from
     156 * multiple streams. Therefore, we need to maintain a list of devices that we
     157 * have opened (with ref counts!) so that we don't try to open a device too
     158 * many times or close a device that we're still using */
    115159struct dag_dev_t *open_dags = NULL;
    116160
    117 /* Dag erf ether packets have a 2 byte padding before the packet
    118  * so that the ip header is aligned on a 32 bit boundary.
    119  */
     161/* Returns the amount of padding between the ERF header and the start of the
     162 * captured packet data */
    120163static int dag_get_padding(const libtrace_packet_t *packet)
    121164{
     165        /* ERF Ethernet records have a 2 byte padding before the packet itself
     166         * so that the IP header is aligned on a 32 bit boundary.
     167         */
    122168        if (packet->trace->format->type==TRACE_FORMAT_ERF) {
    123169                dag_record_t *erfptr = (dag_record_t *)packet->header;
     
    137183}
    138184
     185/* Attempts to determine if the given filename refers to a DAG device */
    139186static int dag_probe_filename(const char *filename)
    140187{
     
    152199}
    153200
     201/* Initialises the DAG output data structure */
    154202static void dag_init_format_out_data(libtrace_out_t *libtrace) {
    155203        libtrace->format_data = (struct dag_format_data_out_t *) malloc(sizeof(struct dag_format_data_out_t));
    156204        // no DUCK on output
    157205        FORMAT_DATA_OUT->stream_attached = 0;
    158         FORMAT_DATA_OUT->drops = 0;
    159206        FORMAT_DATA_OUT->device = NULL;
    160207        FORMAT_DATA_OUT->dagstream = 0;
    161         FORMAT_DATA_OUT->processed = 0;
    162         FORMAT_DATA_OUT->bottom = NULL;
    163         FORMAT_DATA_OUT->top = NULL;
    164208        FORMAT_DATA_OUT->waiting = 0;
    165209
    166210}
    167211
     212/* Initialises the DAG input data structure */
    168213static void dag_init_format_data(libtrace_t *libtrace) {
    169214        libtrace->format_data = (struct dag_format_data_t *)
     
    182227}
    183228
    184 /* NOTE: This function assumes the open_dag_mutex is held by the caller */
     229/* Determines if there is already an entry for the given DAG device in the
     230 * device list and increments the reference count for that device, if found.
     231 *
     232 * NOTE: This function assumes the open_dag_mutex is held by the caller */
    185233static struct dag_dev_t *dag_find_open_device(char *dev_name) {
    186234        struct dag_dev_t *dag_dev;
     
    203251}
    204252
    205 /* NOTE: This function assumes the open_dag_mutex is held by the caller */
     253/* Closes a DAG device and removes it from the device list.
     254 *
     255 * Attempting to close a DAG device that has a non-zero reference count will
     256 * cause an assertion failure!
     257 *
     258 * NOTE: This function assumes the open_dag_mutex is held by the caller */
    206259static void dag_close_device(struct dag_dev_t *dev) {
    207260        /* Need to remove from the device list */
     
    225278}
    226279
     280
     281/* Opens a new DAG device for writing and adds it to the DAG device list
     282 *
     283 * NOTE: this function should only be called when opening a DAG device for
     284 * writing - there is little practical difference between this and the
     285 * function below that covers the reading case, but we need the output trace
     286 * object to report errors properly so the two functions take slightly
     287 * different arguments. This is really lame and there should be a much better
     288 * way of doing this.
     289 *
     290 * NOTE: This function assumes the open_dag_mutex is held by the caller
     291 */
    227292static struct dag_dev_t *dag_open_output_device(libtrace_out_t *libtrace, char *dev_name) {
    228293        struct stat buf;
     
    230295        struct dag_dev_t *new_dev;
    231296
     297        /* Make sure the device exists */
    232298        if (stat(dev_name, &buf) == -1) {
    233299                trace_set_err_out(libtrace,errno,"stat(%s)",dev_name);
     
    235301}
    236302
     303        /* Make sure it is the appropriate type of device */
    237304        if (S_ISCHR(buf.st_mode)) {
     305                /* Try opening the DAG device */
    238306                if((fd = dag_open(dev_name)) < 0) {
    239307                        trace_set_err_out(libtrace,errno,"Cannot open DAG %s",
     
    247315        }
    248316
     317        /* Add the device to our device list - it is just a doubly linked
     318         * list with no inherent ordering; just tack the new one on the front
     319         */
    249320        new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t));
    250321        new_dev->fd = fd;
     
    262333}
    263334
    264 /* NOTE: This function assumes the open_dag_mutex is held by the caller */
     335/* Opens a new DAG device for reading and adds it to the DAG device list
     336 *
     337 * NOTE: this function should only be called when opening a DAG device for
     338 * reading - there is little practical difference between this and the
     339 * function above that covers the writing case, but we need the input trace
     340 * object to report errors properly so the two functions take slightly
     341 * different arguments. This is really lame and there should be a much better
     342 * way of doing this.
     343 *
     344 * NOTE: This function assumes the open_dag_mutex is held by the caller */
    265345static struct dag_dev_t *dag_open_device(libtrace_t *libtrace, char *dev_name) {
    266346        struct stat buf;
     
    268348        struct dag_dev_t *new_dev;
    269349
     350        /* Make sure the device exists */
    270351        if (stat(dev_name, &buf) == -1) {
    271352                trace_set_err(libtrace,errno,"stat(%s)",dev_name);
     
    273354        }
    274355
     356        /* Make sure it is the appropriate type of device */
    275357        if (S_ISCHR(buf.st_mode)) {
     358                /* Try opening the DAG device */
    276359                if((fd = dag_open(dev_name)) < 0) {
    277360                        trace_set_err(libtrace,errno,"Cannot open DAG %s",
     
    285368        }
    286369
     370        /* Add the device to our device list - it is just a doubly linked
     371         * list with no inherent ordering; just tack the new one on the front
     372         */
    287373        new_dev = (struct dag_dev_t *)malloc(sizeof(struct dag_dev_t));
    288374        new_dev->fd = fd;
     
    300386}
    301387
     388/* Creates and initialises a DAG output trace */
    302389static int dag_init_output(libtrace_out_t *libtrace) {
    303390        char *dag_dev_name = NULL;
     
    315402
    316403        dag_init_format_out_data(libtrace);
     404        /* Grab the mutex while we're likely to be messing with the device
     405         * list */
    317406        pthread_mutex_lock(&open_dag_mutex);
     407       
     408        /* Specific streams are signified using a comma in the libtrace URI,
     409         * e.g. dag:/dev/dag0,1 refers to stream 1 on the dag0 device.
     410         *
     411         * If no stream is specified, we will write using stream 1 */
    318412        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
    319413                dag_dev_name = strdup(libtrace->uridata);
     
    325419        FORMAT_DATA_OUT->dagstream = stream;
    326420
     421        /* See if our DAG device is already open */
    327422        dag_device = dag_find_open_device(dag_dev_name);
    328423
     
    331426                dag_device = dag_open_output_device(libtrace, dag_dev_name);
    332427        } else {
     428                /* Otherwise, just use the existing one */
    333429                free(dag_dev_name);
    334430                dag_dev_name = NULL;
    335431        }
    336432
     433        /* Make sure we have successfully opened a DAG device */
    337434        if (dag_device == NULL) {
    338435                if (dag_dev_name) {
    339436                        free(dag_dev_name);
    340437                }
     438                pthread_mutex_unlock(&open_dag_mutex);
    341439                return -1;
    342440        }
     
    347445}
    348446
     447/* Creates and initialises a DAG input trace */
    349448static int dag_init_input(libtrace_t *libtrace) {
    350449        char *dag_dev_name = NULL;
     
    354453
    355454        dag_init_format_data(libtrace);
     455        /* Grab the mutex while we're likely to be messing with the device
     456         * list */
    356457        pthread_mutex_lock(&open_dag_mutex);
     458       
     459       
     460        /* Specific streams are signified using a comma in the libtrace URI,
     461         * e.g. dag:/dev/dag0,2 refers to stream 2 on the dag0 device.
     462         *
     463         * If no stream is specified, we will read from stream 0 */
    357464        if ((scan = strchr(libtrace->uridata,',')) == NULL) {
    358465                dag_dev_name = strdup(libtrace->uridata);
     
    365472        FORMAT_DATA->dagstream = stream;
    366473
     474        /* See if our DAG device is already open */
    367475        dag_device = dag_find_open_device(dag_dev_name);
    368476
     
    371479                dag_device = dag_open_device(libtrace, dag_dev_name);
    372480        } else {
     481                /* Otherwise, just use the existing one */
    373482                free(dag_dev_name);
    374483                dag_dev_name = NULL;
    375484        }
    376485
     486        /* Make sure we have successfully opened a DAG device */
    377487        if (dag_device == NULL) {
    378488                if (dag_dev_name)
    379489                        free(dag_dev_name);
    380490                dag_dev_name = NULL;
     491                pthread_mutex_unlock(&open_dag_mutex);
    381492                return -1;
    382493        }
     
    388499}
    389500
     501/* Configures a DAG input trace */
    390502static int dag_config_input(libtrace_t *libtrace, trace_option_t option,
    391503                                void *data) {
     
    393505        switch(option) {
    394506                case TRACE_OPTION_META_FREQ:
     507                        /* This option is used to specify the frequency of DUCK
     508                         * updates */
    395509                        DUCK.duck_freq = *(int *)data;
    396510                        return 0;
    397511                case TRACE_OPTION_SNAPLEN:
     512                        /* Tell the card our new snap length */
    398513                        snprintf(conf_str, 4096, "varlen slen=%i", *(int *)data);
    399514                        if (dag_configure(FORMAT_DATA->device->fd,
     
    407522                        return -1;
    408523                case TRACE_OPTION_FILTER:
     524                        /* We don't yet support pushing filters into DAG
     525                         * cards */
    409526                        return -1;
    410527                case TRACE_OPTION_EVENT_REALTIME:
     528                        /* Live capture is always going to be realtime */
    411529                        return -1;
    412530        }
    413531        return -1;
    414532}
     533
     534/* Starts a DAG output trace */
    415535static int dag_start_output(libtrace_out_t *libtrace) {
    416536        struct timeval zero, nopoll;
     
    422542        nopoll = zero;
    423543
     544        /* Attach and start the DAG stream */
     545
    424546        if (dag_attach_stream(FORMAT_DATA_OUT->device->fd,
    425547                        FORMAT_DATA_OUT->dagstream, 0, 1048576) < 0) {
     
    444566}
    445567
     568/* Starts a DAG input trace */
    446569static int dag_start_input(libtrace_t *libtrace) {
    447570        struct timeval zero, nopoll;
     
    454577        nopoll = zero;
    455578
    456 
    457 
     579        /* Attach and start the DAG stream */
    458580        if (dag_attach_stream(FORMAT_DATA->device->fd,
    459581                                FORMAT_DATA->dagstream, 0, 0) < 0) {
     
    468590        }
    469591        FORMAT_DATA->stream_attached = 1;
     592       
    470593        /* We don't want the dag card to do any sleeping */
    471594        dag_set_stream_poll(FORMAT_DATA->device->fd,
     
    474597
    475598        /* Should probably flush the memory hole now */
    476 
    477599        do {
    478600                top = dag_advance_stream(FORMAT_DATA->device->fd,
     
    491613}
    492614
     615/* Pauses a DAG output trace */
    493616static int dag_pause_output(libtrace_out_t *libtrace) {
     617
     618        /* Stop and detach the stream */
    494619        if (dag_stop_stream(FORMAT_DATA_OUT->device->fd,
    495620                        FORMAT_DATA_OUT->dagstream) < 0) {
     
    506631}
    507632
     633/* Pauses a DAG input trace */
    508634static int dag_pause_input(libtrace_t *libtrace) {
     635
     636        /* Stop and detach the stream */
    509637        if (dag_stop_stream(FORMAT_DATA->device->fd,
    510638                                FORMAT_DATA->dagstream) < 0) {
     
    521649}
    522650
     651/* Closes a DAG input trace */
    523652static int dag_fin_input(libtrace_t *libtrace) {
     653        /* Need the lock, since we're going to be handling the device list */
    524654        pthread_mutex_lock(&open_dag_mutex);
     655       
     656        /* Detach the stream if we are not paused */
    525657        if (FORMAT_DATA->stream_attached)
    526658                dag_pause_input(libtrace);
    527659        FORMAT_DATA->device->ref_count --;
    528660
     661        /* Close the DAG device if there are no more references to it */
    529662        if (FORMAT_DATA->device->ref_count == 0)
    530663                dag_close_device(FORMAT_DATA->device);
     
    536669}
    537670
     671/* Closes a DAG output trace */
    538672static int dag_fin_output(libtrace_out_t *libtrace) {
    539         // commit any outstanding traffic in the txbuffer
     673       
     674        /* Commit any outstanding traffic in the txbuffer */
    540675        if (FORMAT_DATA_OUT->waiting) {
    541676                dag_tx_stream_commit_bytes(FORMAT_DATA_OUT->device->fd, FORMAT_DATA_OUT->dagstream,
     
    543678        }
    544679
    545         // wait until the buffer is nearly clear before exiting the program, as we
    546         // will lose packets otherwise
     680        /* Wait until the buffer is nearly clear before exiting the program,
     681         * as we will lose packets otherwise */
    547682        dag_tx_get_stream_space(FORMAT_DATA_OUT->device->fd,
    548683                        FORMAT_DATA_OUT->dagstream,
     
    551686                        );
    552687
     688        /* Need the lock, since we're going to be handling the device list */
    553689        pthread_mutex_lock(&open_dag_mutex);
     690
     691        /* Detach the stream if we are not paused */
    554692        if (FORMAT_DATA_OUT->stream_attached)
    555693                dag_pause_output(libtrace);
    556694        FORMAT_DATA_OUT->device->ref_count --;
    557695
     696        /* Close the DAG device if there are no more references to it */
    558697        if (FORMAT_DATA_OUT->device->ref_count == 0)
    559698                dag_close_device(FORMAT_DATA_OUT->device);
     
    563702}
    564703
     704/* Extracts DUCK information from the DAG card and produces a DUCK packet */
    565705static int dag_get_duckinfo(libtrace_t *libtrace,
    566706                                libtrace_packet_t *packet) {
     707
     708        /* Allocate memory for the DUCK data */
    567709        if (packet->buf_control == TRACE_CTRL_EXTERNAL ||
    568710                        !packet->buffer) {
     
    576718        }
    577719
     720        /* DUCK doesn't have a format header */
    578721        packet->header = 0;
    579722        packet->payload = packet->buffer;
    580723
    581724        /* No need to check if we can get DUCK or not - we're modern
    582          * enough */
     725         * enough so just grab the DUCK info */
    583726        if ((ioctl(FORMAT_DATA->device->fd, DAGIOCDUCK,
    584727                                        (duckinf_t *)packet->payload) < 0)) {
     
    588731
    589732        packet->type = TRACE_RT_DUCK_2_5;
     733
     734        /* Set the packet's tracce to point at a DUCK trace, so that the
     735         * DUCK format functions will be called on the packet rather than the
     736         * DAG ones */
    590737        if (!DUCK.dummy_duck)
    591738                DUCK.dummy_duck = trace_create_dead("rt:localhost:3434");
     
    594741}
    595742
     743/* Determines the amount of data available to read from the DAG card */
    596744static int dag_available(libtrace_t *libtrace) {
    597745        uint32_t diff = FORMAT_DATA->top - FORMAT_DATA->bottom;
     746
    598747        /* If we've processed more than 4MB of data since we last called
    599748         * dag_advance_stream, then we should call it again to allow the
     
    601750        if (diff >= dag_record_size && FORMAT_DATA->processed < 4 * 1024 * 1024)
    602751                return diff;
     752       
     753        /* Update the top and bottom pointers */
    603754        FORMAT_DATA->top = dag_advance_stream(FORMAT_DATA->device->fd,
    604755                        FORMAT_DATA->dagstream,
    605756                        &(FORMAT_DATA->bottom));
     757       
    606758        if (FORMAT_DATA->top == NULL) {
    607759                trace_set_err(libtrace, errno, "dag_advance_stream failed!");
     
    613765}
    614766
     767/* Returns a pointer to the start of the next complete ERF record */
    615768static dag_record_t *dag_get_record(libtrace_t *libtrace) {
    616769        dag_record_t *erfptr = NULL;
     
    629782}
    630783
     784/* Converts a buffer containing a recently read DAG packet record into a
     785 * libtrace packet */
    631786static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
    632787                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
    633788
    634789        dag_record_t *erfptr;
    635 
     790       
     791        /* If the packet previously owned a buffer that is not the buffer
     792         * that contains the new packet data, we're going to need to free the
     793         * old one to avoid memory leaks */
    636794        if (packet->buffer != buffer &&
    637795                        packet->buf_control == TRACE_CTRL_PACKET) {
     
    639797        }
    640798
     799        /* Set the buffer owner appropriately */
    641800        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
    642801                packet->buf_control = TRACE_CTRL_PACKET;
     
    644803                packet->buf_control = TRACE_CTRL_EXTERNAL;
    645804
     805        /* Update the packet pointers and type appropriately */
    646806        erfptr = (dag_record_t *)buffer;
    647807        packet->buffer = erfptr;
     
    650810
    651811        if (erfptr->flags.rxerror == 1) {
    652                 /* rxerror means the payload is corrupt - drop it
     812                /* rxerror means the payload is corrupt - drop the payload
    653813                 * by tweaking rlen */
    654814                packet->payload = NULL;
     
    663823        }
    664824
     825        /* Update the dropped packets counter */
     826
    665827        /* No loss counter for DSM coloured records - have to use
    666828         * some other API */
    667829        if (erfptr->type == TYPE_DSM_COLOR_ETH) {
    668 
     830                /* TODO */
    669831        } else {
     832                /* Use the ERF loss counter */
    670833                DATA(libtrace)->drops += ntohs(erfptr->lctr);
    671834        }
     
    682845 */
    683846
     847/* Pushes an ERF record onto the transmit stream */
    684848static int dag_dump_packet(libtrace_out_t *libtrace,
    685849                dag_record_t *erfptr, unsigned int pad, void *buffer) {
    686         //int numbytes = 0;
    687850        int size;
    688851
     
    702865
    703866        /*
    704          * Copy the header separately to the body, as we can't guarantee they are
    705          * in contiguous memory
     867         * Copy the header separately to the body, as we can't guarantee they
     868         * are in contiguous memory
    706869         */
    707870        memcpy(FORMAT_DATA_OUT->txbuffer + FORMAT_DATA_OUT->waiting,erfptr,(dag_record_size + pad));
     
    711874
    712875        /*
    713          * Copy our incoming packet into the outgoing buffer, and increment our waiting count
     876         * Copy our incoming packet into the outgoing buffer, and increment
     877         * our waiting count
    714878         */
    715879        size = ntohs(erfptr->rlen)-(dag_record_size + pad);
     
    718882
    719883        /*
    720          * if our output buffer has more than 16 Mebibytes in it, commit those bytes and
    721          * reset the waiting count to 0.
    722          * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in case
    723          * there is still data in the buffer at program exit.
     884         * If our output buffer has more than 16 Mebibytes in it, commit those
     885         * bytes and reset the waiting count to 0.
     886         * Note: dag_fin_output will also call dag_tx_stream_commit_bytes() in
     887         * case there is still data in the buffer at program exit.
    724888         */
    725889
     
    734898}
    735899
     900/* Attempts to determine a suitable ERF type for a given packet. Returns true
     901 * if one is found, false otherwise */
    736902static bool find_compatible_linktype(libtrace_out_t *libtrace,
    737                                 libtrace_packet_t *packet)
     903                                libtrace_packet_t *packet, char *type)
    738904{
    739905         // Keep trying to simplify the packet until we can find
     
    741907
    742908        do {
    743                 char type=libtrace_to_erf_type(trace_get_link_type(packet));
     909                *type=libtrace_to_erf_type(trace_get_link_type(packet));
    744910
    745911                // Success
    746                 if (type != (char)-1)
     912                if (*type != (char)-1)
    747913                        return true;
    748914
     
    760926}
    761927
     928/* Writes a packet to the provided DAG output trace */
    762929static int dag_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) {
    763930        /*
    764          * This is heavily borrowed from erf_write_packet(). Yes, CnP coding sucks,
    765          * sorry about that.
     931         * This is heavily borrowed from erf_write_packet(). Yes, CnP coding
     932         * sucks, sorry about that.
    766933         */
    767934        unsigned int pad = 0;
     
    769936        void *payload = packet->payload;
    770937        dag_record_t *header = (dag_record_t *)packet->header;
    771 
    772         FORMAT_DATA_OUT->processed ++;
     938        char erf_type = 0;
     939
    773940        if(!packet->header) {
    774                 // No header, probably an RT packet. Lifted from erf_write_packet().
     941                /* No header, probably an RT packet. Lifted from
     942                 * erf_write_packet(). */
    775943                return -1;
    776944        }
     
    779947
    780948        /*
    781          * if the payload is null, adjust the rlen. Discussion of this is
     949         * If the payload is null, adjust the rlen. Discussion of this is
    782950         * attached to erf_write_packet()
    783951         */
     
    796964                /* Build up a new packet header from the existing header */
    797965
    798                 // Simplify the packet first - if we can't do this, break early
    799                 if (!find_compatible_linktype(libtrace,packet))
     966                /* Simplify the packet first - if we can't do this, break
     967                 * early */
     968                if (!find_compatible_linktype(libtrace,packet,&erf_type))
    800969                        return -1;
    801970
     
    811980                        erfhdr.flags.iface = trace_get_direction(packet);
    812981
    813                 erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet));
     982                erfhdr.type = erf_type;
    814983
    815984                /* Packet length (rlen includes format overhead) */
     
    825994
    826995
    827                 /* loss counter. Can't do this */
     996                /* Loss counter. Can't do this */
    828997                erfhdr.lctr = 0;
    829998                /* Wire length, does not include padding! */
     
    8411010}
    8421011
     1012/* Reads the next available packet from a DAG card, in a BLOCKING fashion
     1013 *
     1014 * If DUCK reporting is enabled, the packet returned may be a DUCK update
     1015 */
    8431016static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
    8441017        int size = 0;
     
    8481021        uint32_t flags = 0;
    8491022
    850         if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq &&
     1023        /* Check if we're due for a DUCK report */
     1024        if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq &&
    8511025                        DUCK.duck_freq != 0) {
    8521026                size = dag_get_duckinfo(libtrace, packet);
     
    8591033        }
    8601034
     1035        /* Don't let anyone try to free our DAG memory hole! */
    8611036        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
    8621037
     1038        /* If the packet buffer is currently owned by libtrace, free it so
     1039         * that we can set the packet to point into the DAG memory hole */
    8631040        if (packet->buf_control == TRACE_CTRL_PACKET) {
    8641041                free(packet->buffer);
     
    8661043        }
    8671044
     1045        /* Grab a full ERF record */
    8681046        do {
    8691047                numbytes = dag_available(libtrace);
     
    8761054        } while (erfptr == NULL);
    8771055
    878         //dag_form_packet(erfptr, packet);
     1056        /* Prepare the libtrace packet */
    8791057        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF,
    8801058                                flags))
    8811059                return -1;
     1060
     1061        /* Update the DUCK timer */
    8821062        tv = trace_get_timeval(packet);
    8831063        DUCK.last_pkt = tv.tv_sec;
     
    8871067}
    8881068
     1069/* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If a
     1070 * packet is available, we will return a packet event. Otherwise we will
     1071 * return a SLEEP event (as we cannot select on the DAG file descriptor).
     1072 */
    8891073static libtrace_eventobj_t trace_event_dag(libtrace_t *trace,
    8901074                                        libtrace_packet_t *packet) {
     
    9071091                        erfptr = dag_get_record(trace);
    9081092                if (erfptr == NULL) {
    909                         /* No packet available */
     1093                        /* No packet available - sleep for a very short time */
    9101094                        event.type = TRACE_EVENT_SLEEP;
    9111095                        event.seconds = 0.0001;
    9121096                        break;
    9131097                }
    914                 //dag_form_packet(erfptr, packet);
    9151098                if (dag_prepare_packet(trace, packet, erfptr,
    9161099                                        TRACE_RT_DATA_ERF, flags)) {
     
    9221105                event.size = trace_get_capture_length(packet) +
    9231106                                trace_get_framing_length(packet);
     1107               
     1108                /* XXX trace_read_packet() normally applies the following
     1109                 * config options for us, but this function is called via
     1110                 * trace_event() so we have to do it ourselves */
     1111
    9241112                if (trace->filter) {
    9251113                        if (trace_apply_filter(trace->filter, packet)) {
     
    9461134}
    9471135
     1136/* Gets the number of dropped packets */
    9481137static uint64_t dag_get_dropped_packets(libtrace_t *trace) {
    9491138        if (trace->format_data == NULL)
     
    9521141}
    9531142
     1143/* Prints some semi-useful help text about the DAG format module */
    9541144static void dag_help(void) {
    9551145        printf("dag format module: $Revision$\n");
Note: See TracChangeset for help on using the changeset viewer.