Changeset 5952ff0


Ignore:
Timestamp:
02/04/10 10:36:53 (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:
43c00e5
Parents:
2c743a7
Message:
  • Updated internal documentation and licensing for several format modules
Location:
lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lib/format_atmhdr.c

    r1aa4bf7 r5952ff0  
     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
    134#define _GNU_SOURCE
    235
     
    1649#include <stdlib.h>
    1750
     51/* This particular format covers the ATM cell header capture format used to
     52 * take the Auckland VII trace set.
     53 *
     54 * Each capture record contains only a timestamp and the first four bytes of
     55 * the ATM header - nothing else.
     56 *
     57 * As a result, there isn't a lot you can actually do with these traces!
     58 *
     59 * Libtrace does not support writing using this format, because it is so
     60 * useless :)
     61 */
     62
     63/* Returns the size of the ATM cell framing header */
    1864static int atmhdr_get_framing_length(const libtrace_packet_t *packet UNUSED)
    1965{
     
    2167}
    2268
     69/* Initialise an input trace to read an ATM cell header capture */
    2370static int atmhdr_init_input(libtrace_t *libtrace) {
    2471        libtrace->format_data = NULL; /* No format data needed */
     
    2673}
    2774
     75/* Start an ATM cell header input trace */
    2876static int atmhdr_start_input(libtrace_t *libtrace)
    2977{
     
    3684}
    3785
     86/* Close an ATM cell header input trace */
    3887static int atmhdr_fin_input(libtrace_t *libtrace)
    3988{
     
    4291}
    4392
     93
     94/* Converts a buffer containing a recently read ATM cell header record into
     95 * a libtrace packet */
    4496static int atmhdr_prepare_packet(libtrace_t *libtrace,
    4597                libtrace_packet_t *packet, void *buffer,
    4698                libtrace_rt_types_t rt_type, uint32_t flags) {
    4799
     100        /* If the packet previously owned a buffer that was not the buffer
     101         * containing the new packet data, we need to free the old one to
     102         * avoid leaking memory */
    48103        if (packet->buffer != buffer &&
    49104                        packet->buf_control == TRACE_CTRL_PACKET) {
     
    51106        }
    52107
     108        /* Set the buffer owner appropriately */
    53109        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
    54110                packet->buf_control = TRACE_CTRL_PACKET;
     
    56112                packet->buf_control = TRACE_CTRL_EXTERNAL;
    57113
     114        /* Update the packet pointers appropriately */
    58115        packet->buffer = buffer;
    59116        packet->header = buffer;
    60117        packet->payload = (void*)((char*)packet->buffer +
    61118                        libtrace->format->get_framing_length(packet));
     119
     120        /* Set the packet type */
    62121        packet->type = rt_type;
    63122
    64         if (libtrace->format_data == NULL) {
    65                 if (atmhdr_init_input(libtrace))
    66                         return -1;
    67         }
    68123        return 0;
    69124}
    70125
     126/* Reads the next ATM cell header record from the given trace and writes it
     127 * into a libtrace packet */
    71128static int atmhdr_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
    72129        int numbytes;
     
    74131        uint32_t flags = 0;
    75132       
     133        /* Make sure we have a buffer available to read the next record into */
    76134        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) {
    77135                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE);
     
    82140        packet->type = TRACE_RT_DATA_ATMHDR;
    83141
     142        /* The records are a fixed size so we can read the entire record in
     143         * one go */
    84144        if ((numbytes=wandio_read(libtrace->io, buffer, (size_t)12)) != 12)
    85145        {
     
    90150        }
    91151
     152        /* Update all our packet pointers appropriately */
    92153        if (atmhdr_prepare_packet(libtrace, packet, buffer,
    93154                                TRACE_RT_DATA_ATMHDR, flags)) {
     
    99160}
    100161
     162/* Get the link type for an ATM cell header record */
    101163static libtrace_linktype_t atmhdr_get_link_type(const libtrace_packet_t *packet UNUSED) {
     164        /* Unsurprisingly, we're always going to be an ATM header */
    102165        return TRACE_TYPE_ATM;
    103166}
    104167
     168/* Get the capture length for an ATM cell header record */
    105169static int atmhdr_get_capture_length(const libtrace_packet_t *packet UNUSED) {
     170        /* There is always 4 bytes of ATM header retained by this format */
    106171        return 4;
    107172}
    108173
     174/* Get the wire length for an ATM cell header record */
    109175static int atmhdr_get_wire_length(const libtrace_packet_t *packet UNUSED) {
     176        /* ATM packets are 53 byte fixed length records */
    110177        return 53;
    111178}
    112179
     180/* Returns the timestamp for an ATM cell header record in the ERF timestamp
     181 * format */
    113182static uint64_t atmhdr_get_erf_timestamp(const libtrace_packet_t *packet) {
    114183        uint64_t ts;
    115184        atmhdr_t *atm = (atmhdr_t *)packet->header;
     185       
     186        /* Basically, the capture format header is an ERF timestamp except
     187         * the two 32-bit segments are reversed */
    116188        ts = (uint64_t)atm->ts_fraction + ((uint64_t)atm->ts_sec << 32);
    117189
  • lib/format_bpf.c

    r099c35e r5952ff0  
    22 * This file is part of libtrace
    33 *
    4  * Copyright (c) 2007,2008 The University of Waikato, Hamilton, New Zealand.
    5  * Authors: Perry Lorier
     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
    610 *         
    711 * All rights reserved.
     
    5256#include <fcntl.h>
    5357
    54 
     58/* This format deals with the BSD Native capture format, perhaps better
     59 * known as BPF, which is the equivalent of the Linux Native format for
     60 * *BSD systems.
     61 *
     62 * This is a LIVE capture format - we're always dealing with reading from an
     63 * interface.
     64 *
     65 * This format does not support writing, but BPF packet records can be easily
     66 * converted to PCAP or ERF.
     67 */
     68
     69/* "Global" data that is stored for each BPF input trace */
    5570struct libtrace_format_data_t {
    56         int fd;
     71        /* The file descriptor that is being captured from */
     72        int fd;
     73        /* The snap length for the capture */
    5774        int snaplen;
     75        /* A boolean flag indicating whether the capture interface should be
     76         * in promiscuous mode */
    5877        int promisc;
     78        /* A buffer to write captured data into */
    5979        void *buffer;
     80        /* The current read location in the capture buffer */
    6081        void *bufptr;
     82        /* The total size of the capture buffer */
    6183        unsigned int buffersize;
     84        /* The amount of space remaining before the capture buffer is full */
    6285        int remaining;
     86        /* The linktype of the capture interface */
    6387        unsigned int linktype;
     88        /* Statistics about how many packets have been dropped, received etc. */
    6489        struct bpf_stat stats;
     90        /* A boolean flag indicating whether the statistics are up-to-date */
    6591        int stats_valid;
    6692};
     
    7096#define BPFHDR(x) ((struct bpf_hdr *)((x)->header))
    7197
     98/* Attempts to determine if a given filename could refer to a BPF interface */
    7299static int bpf_probe_filename(const char *filename)
    73100{
     
    75102}
    76103
     104/* XXX This function doesn't appear to be used anywhere - nor does it do
     105 * anything particularly useful :) */
    77106static int bpf_start_filename(const char *filename)
    78107{
     
    80109}
    81110
     111/* Initialises a BPF input trace */
    82112static int bpf_init_input(libtrace_t *libtrace)
    83113{
    84114        libtrace->format_data = (struct libtrace_format_data_t *)
    85115                malloc(sizeof(struct libtrace_format_data_t));
     116       
     117        /* Throw some default values into the format data */
    86118        FORMATIN(libtrace)->fd = -1;
    87119        FORMATIN(libtrace)->promisc = 0;
     
    92124}
    93125
     126/* Starts a BPF input trace */
    94127static int bpf_start_input(libtrace_t *libtrace)
    95128{
     
    155188        FORMATIN(libtrace)->remaining = 0;
    156189
    157         /* attach to the device */
     190        /* Attach to the device */
    158191        strncpy(ifr.ifr_name, libtrace->uridata, sizeof(ifr.ifr_name));
    159192        if (ioctl(FORMATIN(libtrace)->fd, BIOCSETIF, &ifr) == -1) {
     
    163196        }
    164197
     198        /* Set the link type */
    165199        if (ioctl(FORMATIN(libtrace)->fd, BIOCGDLT,
    166200                         &FORMATIN(libtrace)->linktype) == -1) {
     
    185219         * pray the kernel is smart enough that if a another packet arrives
    186220         * while we're processing this one that it will buffer them into it's
    187          * kernel buffer so we can recieve packets later. (It'll need to do this
     221         * kernel buffer so we can receive packets later. (It'll need to do this
    188222         * to deal with us spending time processing the last 'n' packets anyway)
    189223         */
     
    196230        }
    197231
     232        /* Set promiscous mode, if the user has asked us to do so */
    198233        if (FORMATIN(libtrace)->promisc) {
    199234                if (ioctl(FORMATIN(libtrace)->fd, BIOCPROMISC, NULL) == -1) {
     
    214249}
    215250
     251/* Gets a count of the number of packets received on the BPF interface */
    216252static uint64_t bpf_get_received_packets(libtrace_t *trace)
    217253{
     
    237273}
    238274
     275/* Gets a count of the number of packets dropped on the BPF interface */
    239276static uint64_t bpf_get_dropped_packets(libtrace_t *trace)
    240277{
     
    260297}
    261298
     299/* Pauses a BPF input trace */
    262300static int bpf_pause_input(libtrace_t *libtrace)
    263301{
     
    268306}
    269307
     308/* Closes a BPF input trace */
    270309static int bpf_fin_input(libtrace_t *libtrace)
    271310{
     
    274313}
    275314
     315/* Configures a BPF input trace */
    276316static int bpf_config_input(libtrace_t *libtrace,
    277317                trace_option_t option,
     
    295335                        break;
    296336                case TRACE_OPTION_EVENT_REALTIME:
    297                         /* captures are always realtime */
     337                        /* Captures are always realtime */
    298338                        break;
    299339
     
    308348}
    309349
     350/* Converts a buffer containing a recently read BPF packet record into a
     351 * libtrace packet */
    310352static int bpf_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
    311353                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
    312         if (packet->buffer != buffer &&
     354       
     355        /* If the packet previously owned a buffer that is not the buffer
     356         * that contains the new packet data, we're going to need to free the
     357         * old one to avoid memory leaks */
     358        if (packet->buffer != buffer &&
    313359                        packet->buf_control == TRACE_CTRL_PACKET) {
    314360                free(packet->buffer);
    315361        }
    316362
     363        /* Set the buffer owner appropriately */
    317364        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
    318365                packet->buf_control = TRACE_CTRL_PACKET;
     
    320367                packet->buf_control = TRACE_CTRL_EXTERNAL;
    321368
    322 
     369        /* Update the packet pointers and type appropriately */
    323370        packet->buffer = buffer;
    324371        packet->header = buffer;
     
    336383        return 0;
    337384}
    338        
     385
     386/* Reads the next packet record from a BPF interface and writes it into a
     387 * libtrace packet */   
    339388static int bpf_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet)
    340389{
    341390        uint32_t flags = 0;
    342391       
    343         /* Fill the buffer */
     392        /* Read from the BPF interface into our capture buffer */
    344393        if (FORMATIN(libtrace)->remaining<=0) {
    345394                int ret;
     
    363412                                FORMATIN(libtrace)->buffer;
    364413        }
     414
     415        /* We do NOT want anything trying to free the memory the packet is
     416         * stored in */
    365417        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
    366         /* Read one packet out */
    367        
     418
    368419        if (packet->buf_control == TRACE_CTRL_PACKET)
    369420                free(packet->buffer);
    370421
     422        /* Update 'packet' to point to the first packet in our capture
     423         * buffer */
    371424        if (bpf_prepare_packet(libtrace, packet, FORMATIN(libtrace)->bufptr,
    372425                TRACE_RT_DATA_BPF, flags)) {
     
    375428       
    376429
    377         /* Now deal with any padding */
     430        /* Skip past the packet record we're going to return, making sure
     431         * that we deal with padding correctly */
    378432        FORMATIN(libtrace)->bufptr+=
    379433                BPF_WORDALIGN(BPFHDR(packet)->bh_hdrlen
     
    386440}
    387441
     442/* Returns the linktype for the interface that we are capturing from */
    388443static libtrace_linktype_t bpf_get_link_type(const libtrace_packet_t *packet) {
     444        /* Convert the linktype that we recorded when we started the trace
     445         * into a suitable libtrace linktype */
    389446        return pcap_linktype_to_libtrace(FORMATIN(packet->trace)->linktype);
    390447}
    391448
     449/* Returns the direction for a given BPF packet record */
    392450static libtrace_direction_t bpf_get_direction(const libtrace_packet_t *packet) {
    393         /* BPF Sadly can't do direction tagging */
     451        /* BPF sadly can't do direction tagging */
    394452        return ~0;
    395453}
    396454
     455/* Returns the timestamp for a given BPF packet record, in the form of a
     456 * struct timeval */
    397457static struct timeval bpf_get_timeval(const libtrace_packet_t *packet)
    398458{
     
    407467}
    408468
     469/* Returns the capture length for a given BPF packet record */
    409470static int bpf_get_capture_length(const libtrace_packet_t *packet)
    410471{
    411         /* BPF Doesn't include the FCS, we do. */
     472        /* BPF doesn't include the FCS in its caplen field, but libtrace
     473         * does so we need to add this extra 4 bytes */
    412474        return BPFHDR(packet)->bh_caplen+4;
    413475}
    414476
     477/* Returns the wire length for a given BPF packet record */
    415478static int bpf_get_wire_length(const libtrace_packet_t *packet)
    416479{
     480
     481        /* BPF doesn't include the FCS in its datalen field, but libtrace
     482         * does so we need to add this extra 4 bytes */
    417483        return BPFHDR(packet)->bh_datalen+4;
    418484}
    419485
     486/* Returns the framing length for a given BPF packet record */
    420487static int bpf_get_framing_length(UNUSED
    421488                const libtrace_packet_t *packet)
     
    424491}
    425492
     493/* Returns the file descriptor that the capture interface is operating on */
    426494static int bpf_get_fd(const libtrace_t *trace) {
    427495        return FORMATIN(trace)->fd;
    428496}
    429497
     498/* Prints some slightly useful help text for the BPF capture format */
    430499static void bpf_help() {
    431500        printf("bpf format module: $Revision$\n");
  • lib/format_dag24.c

    r5778738 r5952ff0  
    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
     
    5962#endif
    6063
     64/* This format deals with DAG cards that are using drivers from the 2.4.X
     65 * versions.
     66 *
     67 * DAG is a LIVE capture format.
     68 *
     69 * We do not support writing using this format, as transmit support was not
     70 * added until a subsequent version of the DAG software (see format_dag25.c).
     71 * Instead, you should write the packets read using this format as ERF traces.
     72 */
    6173
    6274static struct libtrace_format_t dag;
     
    6678#define FORMAT_DATA DATA(libtrace)
    6779
     80/* "Global" data that is stored for each DAG input trace */
    6881struct dag_format_data_t {
     82
     83        /* Data required for regular DUCK reporting */
    6984        struct {
     85                /* Timestamp of the last DUCK report */
    7086                uint32_t last_duck;
     87                /* The number of seconds between each DUCK report */
    7188                uint32_t duck_freq;
     89                /* Timestamp of the last packet read from the DAG card */
    7290                uint32_t last_pkt;
     91                /* Dummy trace to ensure DUCK packets are dealt with using
     92                 * the DUCK format functions */
    7393                libtrace_t *dummy_duck;
    7494        } duck;
    7595       
     96        /* File descriptor for the DAG card */
    7697        int fd;
     98        /* Pointer to DAG memory hole */
    7799        void *buf;
     100        /* Difference between the top and bottom pointers in the DAG memory
     101         * hole, i.e. the amount of available data to read */
    78102        uint32_t diff;
     103        /* The amount of data read thus far from the start of the bottom
     104         * pointer */
    79105        uint32_t offset;
     106        /* The offset for the first unread byte in the DAG memory hole */
    80107        uint32_t bottom;
     108        /* The offset for the last unread byte in the DAG memory hole */
    81109        uint32_t top;
     110        /* The number of packets that have been dropped */
    82111        uint64_t drops;
    83112};
    84113
     114/* Determines if a given filename refers to a DAG device */
    85115static void dag_probe_filename(const char *filename)
    86116{
     
    98128}
    99129
     130/* Initialises the DAG "global" variables */
    100131static void dag_init_format_data(libtrace_t *libtrace) {
    101132        libtrace->format_data = (struct dag_format_data_t *)
     
    115146}
    116147
     148/* Determines how much data is available for reading on the DAG card and
     149 * updates the various offsets accordingly */
    117150static int dag_available(libtrace_t *libtrace) {
    118151
     
    130163}
    131164
     165/* Initialises a DAG input trace */
    132166static int dag_init_input(libtrace_t *libtrace) {
    133167        struct stat buf;
     
    145179        }
    146180
    147        
     181
     182        /* Make sure a DAG device with the right name exists */
    148183        if (stat(dag_dev_name, &buf) == -1) {
    149184                trace_set_err(libtrace,errno,"stat(%s)",dag_dev_name);
     
    161196                        return -1;
    162197                }
     198
     199                /* Memory-map ourselves a pointer to the DAG memory hole */
    163200                if((FORMAT_DATA->buf = (void *)dag_mmap(FORMAT_DATA->fd)) == MAP_FAILED) {
    164201                        trace_set_err(libtrace,errno,"Cannot mmap DAG %s",
     
    179216}
    180217
     218/* Configures a DAG input trace */
    181219static int dag_config_input(libtrace_t *libtrace, trace_option_t option,
    182220                                void *data) {
    183221        switch(option) {
    184222                case TRACE_OPTION_META_FREQ:
     223                        /* We use this option to specify the frequency of
     224                         * DUCK updates */
    185225                        DUCK.duck_freq = *(int *)data;
    186226                        return 0;
     
    192232                        return -1;
    193233                case TRACE_OPTION_FILTER:
     234                        /* Cards that use the older drivers don't do
     235                         * filtering */
    194236                        return -1;
    195237                case TRACE_OPTION_EVENT_REALTIME:
     238                        /* Live capture is always going to be realtime */
    196239                        return -1;
    197240        }
     
    199242}
    200243
     244/* Starts a DAG input trace */
    201245static int dag_start_input(libtrace_t *libtrace) {     
    202246        if(dag_start(FORMAT_DATA->fd) < 0) {
     
    213257}
    214258
     259/* Pauses a DAG input trace */
    215260static int dag_pause_input(libtrace_t *libtrace) {
    216261        dag_stop(FORMAT_DATA->fd);
     
    218263}
    219264
     265/* Destroys a DAG input trace */
    220266static int dag_fin_input(libtrace_t *libtrace) {
    221267        dag_close(FORMAT_DATA->fd);
     
    226272}
    227273
     274/* Extracts DUCK information from the DAG card and produces a DUCK packet */
    228275static int dag_get_duckinfo(libtrace_t *libtrace,
    229276                                libtrace_packet_t *packet) {
    230277        dag_inf lt_dag_inf;
    231278
     279        /* Allocate memory for the DUCK data */
    232280        if (packet->buf_control == TRACE_CTRL_EXTERNAL ||
    233281                        !packet->buffer) {
     
    241289        }
    242290
     291        /* DUCK doesn't actually have a format header, as such */
    243292        packet->header = 0;
    244293        packet->payload = packet->buffer;
    245294
     295        /* Check that the DAG card supports DUCK */
    246296        if ((ioctl(FORMAT_DATA->fd, DAG_IOINF, &lt_dag_inf) < 0)) {
    247297                trace_set_err(libtrace, errno,
     
    254304        }
    255305
     306        /* Get the DUCK information from the card */
    256307        if ((ioctl(FORMAT_DATA->fd, DAG_IOGETDUCK, (duck_inf *)packet->payload)
    257308                                < 0)) {
     
    260311        }
    261312
     313        /* Set the type */
    262314        packet->type = TRACE_RT_DUCK_2_4;
     315
     316        /* Set the packet's trace to point at a DUCK trace, so that the
     317         * DUCK format functions will be called on the packet rather than the
     318         * DAG ones */
    263319        if (!DUCK.dummy_duck)
    264320                DUCK.dummy_duck = trace_create_dead("duck:dummy");
     
    267323}
    268324
    269 
     325/* Reads the next ERF record from the DAG memory hole */
    270326static dag_record_t *dag_get_record(libtrace_t *libtrace) {
    271327        dag_record_t *erfptr = NULL;
     
    283339}
    284340
     341/* Converts a buffer containing a recently read DAG packet record into a
     342 * libtrace packet */
    285343static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet,
    286344                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
    287345
    288346        dag_record_t *erfptr;
     347        /* If the packet previously owned a buffer that is not the buffer
     348         * that contains the new packet data, we're going to need to free the
     349         * old one to avoid memory leaks */
    289350        if (packet->buffer != buffer &&
    290351                        packet->buf_control == TRACE_CTRL_PACKET) {
     
    292353        }
    293354
     355        /* Set the buffer owner appropriately */
    294356        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
    295357                packet->buf_control = TRACE_CTRL_PACKET;
     
    297359                packet->buf_control = TRACE_CTRL_EXTERNAL;
    298360       
     361        /* Update packet pointers and type appropriately */
    299362        erfptr = (dag_record_t *)buffer;
    300363        packet->buffer = erfptr;
     
    303366
    304367        if (erfptr->flags.rxerror == 1) {
    305                 /* rxerror means the payload is corrupt - drop it
     368                /* rxerror means the payload is corrupt - drop the payload
    306369                 * by tweaking rlen */
    307370                packet->payload = NULL;
     
    316379        }
    317380
     381        /* Update the dropped packets counter, using the value of the ERF
     382         * loss counter */
    318383        DATA(libtrace)->drops += ntohs(erfptr->lctr);
    319384
     
    322387}
    323388
     389/* Reads the next available packet from a DAG card, in a BLOCKING fashion
     390 *
     391 * If DUCK reporting is enabled, the packet returned may be a DUCK update */
    324392static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
    325393        int numbytes;
     
    329397        dag_record_t *erfptr = NULL;
    330398
     399        /* Check if we're due for a DUCK report */
    331400        if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq &&
    332401                        DUCK.duck_freq != 0) {
     
    340409        }
    341410
     411        /* Don't let anyone try to free our DAG memory hole */
    342412        flags |= TRACE_PREP_DO_NOT_OWN_BUFFER;
    343413       
    344         if (packet->buf_control == TRACE_CTRL_PACKET) {
     414        /* If the packet buffer is currently owned by libtrace, free it so
     415         * that we can set the packet to point into the DAG memory hole */
     416        if (packet->buf_control == TRACE_CTRL_PACKET) {
    345417                packet->buf_control = TRACE_CTRL_EXTERNAL;
    346418                free(packet->buffer);
     
    348420        }
    349421
    350 
     422        /* Grab a full ERF record */
    351423        do {
    352424                numbytes = dag_available(libtrace);
     
    358430        } while (erfptr == NULL);
    359431       
    360        
     432        /* Prepare the libtrace packet */
    361433        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF,
    362434                                flags))
    363435                return -1;
     436       
     437        /* Update the DUCK timer */
    364438        tv = trace_get_timeval(packet);
    365439        DUCK.last_pkt = tv.tv_sec;
     
    367441}
    368442
     443/* Attempts to read a packet from a DAG card in a NON-BLOCKING fashion. If
     444 * a packet is available, we will return a packet event. Otherwise we will
     445 * return a SLEEP event (as we cannot select on the DAG file descriptor).
     446 */
    369447static libtrace_eventobj_t trace_event_dag(libtrace_t *trace,
    370448                                        libtrace_packet_t *packet) {
     
    375453                data = dag_available(trace);
    376454
     455                /* If no data is available, drop out and return a sleep event */
    377456                if (data <= 0)
    378457                        break;
    379458
     459                /* Data is available, so we can call the blocking read because
     460                 * we know that we will get a packet straight away */
    380461                event.size = dag_read_packet(trace,packet);
    381462                //DATA(trace)->dag.diff -= event.size;
     463               
     464                /* XXX trace_read_packet() normally applies the following
     465                 * config options for us, but this function is called via
     466                 * trace_event() so we have to do it ourselves */
     467
     468                /* Check that the packet matches any pre-existing filter */
    382469                if (trace->filter) {
    383470                        if (trace_apply_filter(trace->filter, packet)) {
     
    390477                        event.type = TRACE_EVENT_PACKET;
    391478                }
     479
     480                /* If the user has specified a snap length, apply that too */
    392481                if (trace->snaplen > 0) {
    393482                        trace_set_capture_length(packet, trace->snaplen);
     
    397486        } while (1);
    398487
     488
     489        /* We only want to sleep for a very short time */
    399490        assert(data == 0);
    400491        event.type = TRACE_EVENT_SLEEP;
     
    404495}
    405496
     497/* Gets the number of dropped packets */
    406498static uint64_t dag_get_dropped_packets(libtrace_t *trace)
    407499{
     
    411503}
    412504
     505/* Prints some semi-useful help text about the DAG format module */
    413506static void dag_help(void) {
    414507        printf("dag format module: $Revision$\n");
Note: See TracChangeset for help on using the changeset viewer.