Changeset 37ee856 for lib


Ignore:
Timestamp:
09/04/18 14:27:26 (2 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
develop, master, ringdecrementfix, ringperformance
Children:
ebed638
Parents:
6646938
Message:

Tag each packet with the start iteration of the parent trace.

Traces can be started, paused and then restarted. For some
formats (especially live ones), this will mean that buffers
containing received packets can be destroyed and recreated as
a result of that process. However, in our parallel world we might
also have lingering references to packets that lived in a
now-destroyed buffer and bad things will happen if we try to
operate on it.

To try and avoid this, we keep track of how many times a trace has
been "started" and each packet read is tagged with the start count
at the time it was read. Later processing functions can now check
if the packet was read before the most recent "start" -- if it was,
then it is potentially bogus and should be ignored.

This shouldn't change anything for the vast majority of libtrace
use-cases. Normally, pausing is only used prior to ending an
input without subsequent restarting, i.e. there is only one start
iteration.

Location:
lib
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • lib/format_pcap.c

    r32ee9b2 r37ee856  
    500500        /* Silently discard RT metadata packets and packets with an
    501501         * unknown linktype. */
    502         if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META) {
     502        if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META || linktype == TRACE_TYPE_CONTENT_INVALID) {
    503503                return 0;
    504504        }
     
    595595        /* Silently discard RT metadata packets and packets with an
    596596         * unknown linktype. */
    597         if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META) {
     597        if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META || linktype == TRACE_TYPE_CONTENT_INVALID) {
    598598                return 0;
    599599        }
  • lib/format_pcapfile.c

    r32ee9b2 r37ee856  
    432432        /* Silently discard RT metadata packets and packets with an
    433433         * unknown linktype. */
    434         if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META) {
     434        if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META || linktype == TRACE_TYPE_CONTENT_INVALID) {
    435435                return 0;
    436436        }
  • lib/libtrace.h.in

    r47d4f8c r37ee856  
    347347
    348348/** Enumeration of link layer types supported by libtrace */
    349 typedef enum {
     349typedef enum {
     350       TRACE_TYPE_CONTENT_INVALID = -2, /**< Packet contents are not valid */
    350351       TRACE_TYPE_UNKNOWN = -1,         /**< Unable to determine link type */
    351352    /* TRACE_TYPE_LEGACY = 0            Obsolete */
     
    567568        pthread_mutex_t ref_lock;       /**< Lock for reference counter */
    568569        int refcount;                 /**< Reference counter */
     570        int which_trace_start;          /**< Used to match packet to a started instance of the parent trace */
    569571
    570572} libtrace_packet_t;
  • lib/libtrace_int.h

    r823d8e1 r37ee856  
    337337        /** Boolean flag indicating whether the trace has been started */
    338338        bool started;
     339        /** Number of times this trace has been started */
     340        int startcount;
    339341        /** Synchronise writes/reads across this format object and attached threads etc */
    340342        pthread_mutex_t libtrace_lock;
  • lib/linktypes.c

    rb663d33 r37ee856  
    112112                        break;
    113113                case TRACE_TYPE_UNKNOWN:
     114                case TRACE_TYPE_CONTENT_INVALID:
    114115                        break;
    115116        }
     
    214215                case TRACE_TYPE_ETSILI:
    215216                case TRACE_TYPE_UNKNOWN:
     217                case TRACE_TYPE_CONTENT_INVALID:
    216218                        break;
    217219        }
     
    333335        struct timeval tv;
    334336        static libtrace_t *trace = NULL;
     337
    335338        switch(trace_get_link_type(packet)) {
    336339                case TRACE_TYPE_ATM:
     
    371374                        }
    372375
     376                        trace->startcount = packet->trace->startcount;
    373377                        packet->trace=trace;
    374378
     
    410414                        break;
    411415                default:
     416                        assert(0);
    412417                        return false;
    413418        }
  • lib/protocols_l2.c

    rfa555ec r37ee856  
    486486                        break;
    487487                case TRACE_TYPE_UNKNOWN:
     488                case TRACE_TYPE_CONTENT_INVALID:
    488489                        return NULL;
    489490        }
     
    523524                                        break;
    524525                                case TRACE_TYPE_UNKNOWN:
     526                                case TRACE_TYPE_CONTENT_INVALID:
    525527                                        return NULL;
    526528                        }
     
    565567        void *l;
    566568
    567         if (linktype == TRACE_TYPE_UNKNOWN) {
     569        if (linktype == TRACE_TYPE_UNKNOWN ||
     570                        linktype == TRACE_TYPE_CONTENT_INVALID) {
    568571                fprintf(stderr, "Unable to determine linktype for packet\n");
    569572                return NULL;
     
    589592                case TRACE_TYPE_NONDATA:
    590593                case TRACE_TYPE_ERF_META:
     594                case TRACE_TYPE_CONTENT_INVALID:
    591595                case TRACE_TYPE_UNKNOWN:
    592596                        return NULL;
     
    693697                case TRACE_TYPE_ERF_META:
    694698                case TRACE_TYPE_UNKNOWN:
     699                case TRACE_TYPE_CONTENT_INVALID:
    695700                        return NULL;
    696701
     
    744749                case TRACE_TYPE_ERF_META:
    745750                case TRACE_TYPE_UNKNOWN:
     751                case TRACE_TYPE_CONTENT_INVALID:
    746752                        /* No MAC address */
    747753                        return NULL;
  • lib/protocols_pktmeta.c

    r59b2ed8 r37ee856  
    186186                case TRACE_TYPE_OPENBSD_LOOP:
    187187                case TRACE_TYPE_UNKNOWN:
     188                case TRACE_TYPE_CONTENT_INVALID:
    188189                        return NULL;
    189190        }
     
    249250                case TRACE_TYPE_ERF_META:
    250251                case TRACE_TYPE_UNKNOWN:
     252                case TRACE_TYPE_CONTENT_INVALID:
    251253                        /* In this case, the pointer passed in does not point
    252254                         * to a metadata header and so we cannot get the
  • lib/trace.c

    rc862ad1 r37ee856  
    265265        libtrace->replayspeedup = 1;
    266266        libtrace->started=false;
     267        libtrace->startcount=0;
    267268        libtrace->uridata = NULL;
    268269        libtrace->io = NULL;
     
    534535                }
    535536        }
    536 
     537        libtrace->startcount ++;
    537538        libtrace->started=true;
    538539        return 0;
     
    840841
    841842        packet->buf_control=TRACE_CTRL_PACKET;
     843        packet->which_trace_start = 0;
    842844        pthread_mutex_init(&(packet->ref_lock), NULL);
    843845        trace_clear_cache(packet);
     
    846848
    847849DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) {
    848         libtrace_packet_t *dest =
    849                 (libtrace_packet_t *)calloc((size_t)1, sizeof(libtrace_packet_t));
     850        libtrace_packet_t *dest;
     851
     852        if (packet->which_trace_start != packet->trace->startcount) {
     853                return NULL;
     854        }
     855
     856        dest = (libtrace_packet_t *)calloc((size_t)1, sizeof(libtrace_packet_t));
    850857        if (!dest) {
    851858                printf("Out of memory constructing packet\n");
     
    866873        dest->hash = packet->hash;
    867874        dest->error = packet->error;
     875        dest->which_trace_start = packet->which_trace_start;
    868876        /* Reset the cache - better to recalculate than try to convert
    869877         * the values over to the new packet */
     
    10111019                                trace_packet_set_order(packet, libtrace->sequence_number);
    10121020                        }
     1021                        packet->which_trace_start = libtrace->startcount;
    10131022                        ++libtrace->sequence_number;
    10141023                        if (!libtrace_parallel && packet->trace == libtrace)
     
    11091118        if (linktype) *linktype = trace_get_link_type(packet);
    11101119        if (remaining) {
     1120                if (linktype && *linktype == TRACE_TYPE_CONTENT_INVALID) {
     1121                        return (void *)packet->payload;
     1122                }
     1123
    11111124                /* I think we should choose the minimum of the capture and
    11121125                 * wire lengths to be the "remaining" value. If the packet has
     
    11601173 */
    11611174DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) {
     1175        if (packet->which_trace_start != packet->trace->startcount) {
     1176                return (uint64_t)0;
     1177        }
     1178
    11621179        if (packet->trace->format->get_erf_timestamp) {
    11631180                /* timestamp -> timestamp */
     
    11961213        struct timeval tv;
    11971214        uint64_t ts = 0;
    1198         if (packet->trace->format->get_timeval) {
     1215
     1216        if (packet->which_trace_start != packet->trace->startcount) {
     1217                tv.tv_sec=-1;
     1218                tv.tv_usec=-1;
     1219        } else if (packet->trace->format->get_timeval) {
    11991220                /* timeval -> timeval */
    12001221                tv = packet->trace->format->get_timeval(packet);
     
    12291250        struct timespec ts;
    12301251
    1231         if (packet->trace->format->get_timespec) {
     1252        if (packet->which_trace_start != packet->trace->startcount) {
     1253                ts.tv_sec=-1;
     1254                ts.tv_nsec=-1;
     1255        } else if (packet->trace->format->get_timespec) {
    12321256                return packet->trace->format->get_timespec(packet);
    12331257        } else if (packet->trace->format->get_erf_timestamp) {
     
    12401264                        ts.tv_sec += 1;
    12411265                }
    1242                 return ts;
    12431266        } else if (packet->trace->format->get_timeval) {
    12441267                /* timeval -> timespec */
     
    12461269                ts.tv_sec = tv.tv_sec;
    12471270                ts.tv_nsec = tv.tv_usec*1000;
    1248                 return ts;
    12491271        } else if (packet->trace->format->get_seconds) {
    12501272                /* seconds -> timespec */
     
    12521274                ts.tv_sec = (uint32_t)seconds;
    12531275                ts.tv_nsec = (long)(((seconds - ts.tv_sec) * 1000000000)/UINT_MAX);
    1254                 return ts;
    12551276        }
    12561277        else {
    12571278                ts.tv_sec=-1;
    12581279                ts.tv_nsec=-1;
    1259                 return ts;
    1260         }
     1280        }
     1281        return ts;
    12611282}
    12621283
     
    12681289DLLEXPORT double trace_get_seconds(const libtrace_packet_t *packet) {
    12691290        double seconds = 0.0;
     1291
     1292        if (packet->which_trace_start != packet->trace->startcount) {
     1293                return 0.0;
     1294        }
    12701295
    12711296        if (packet->trace->format->get_seconds) {
     
    12951320{
    12961321        /* Cache the capture length */
     1322        if (packet->which_trace_start != packet->trace->startcount) {
     1323                return ~0U;
     1324        }
    12971325        if (packet->capture_length == -1) {
    12981326                if (!packet->trace->format->get_capture_length)
     
    13171345DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){
    13181346
     1347        if (packet->which_trace_start != packet->trace->startcount) {
     1348                return ~0U;
     1349        }
     1350
    13191351        if (packet->wire_length == -1) {
    13201352                if (!packet->trace->format->get_wire_length)
     
    13371369DLLEXPORT SIMPLE_FUNCTION
    13381370size_t trace_get_framing_length(const libtrace_packet_t *packet) {
     1371        if (packet->which_trace_start != packet->trace->startcount) {
     1372                return ~0U;
     1373        }
     1374
    13391375        if (packet->trace->format->get_framing_length) {
    13401376                return packet->trace->format->get_framing_length(packet);
     
    13491385 */
    13501386DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) {
     1387
     1388        if (packet->which_trace_start != packet->trace->startcount) {
     1389                return TRACE_TYPE_CONTENT_INVALID;
     1390        }
    13511391
    13521392        if (packet->link_type == 0) {
     
    15811621                                        trace_destroy_packet(packet_copy);
    15821622                                }
     1623                                printf("x\n");
    15831624                                return -1;
    15841625                        }
     
    16041645                        trace_destroy_packet(packet_copy);
    16051646                }
     1647                printf("xx\n");
    16061648                return -1;
    16071649        }
     
    16331675                trace_destroy_packet(packet_copy);
    16341676        }
     1677        if (ret < 0) {
     1678                printf("xxx\n");
     1679        }
    16351680        return ret;
    16361681#else
     
    16661711{
    16671712        assert(packet);
     1713        if (packet->which_trace_start != packet->trace->startcount) {
     1714                return (libtrace_direction_t)~0U;
     1715        }
    16681716        if (packet->trace->format->get_direction) {
    16691717                return packet->trace->format->get_direction(packet);
     
    23562404        packet->l4_remaining = 0;
    23572405        packet->refcount = 0;
    2358 
    23592406}
    23602407
  • lib/trace_parallel.c

    reeabc19b r37ee856  
    13451345                                return -1;
    13461346                        }
     1347                        packets[i]->which_trace_start = libtrace->startcount;
    13471348                }
    13481349                do {
     
    14601461        if (err == 0) {
    14611462                libtrace->started = true;
     1463                libtrace->startcount ++;
    14621464                libtrace_change_state(libtrace, STATE_RUNNING, false);
    14631465        }
     
    18631865        /* Threads don't start */
    18641866        libtrace->started = true;
     1867        libtrace->startcount ++;
    18651868        libtrace_change_state(libtrace, STATE_RUNNING, false);
    18661869
Note: See TracChangeset for help on using the changeset viewer.