Changeset d439067


Ignore:
Timestamp:
12/10/18 15:59:45 (23 months ago)
Author:
Shane Alcock <salcock@…>
Branches:
develop
Children:
10fd24b
Parents:
418c78d
Message:

Move packet cached fields into a distinct structure.

This will help tidy up the packet structure a little, as well as
simplify the cache clearing process (and maybe even speed it up
a little).

Location:
lib
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • lib/format_erf.c

    r509ee47 rd439067  
    852852         * wrong value here and subsequent get_capture_length() calls will
    853853         * return the wrong value. */
    854         packet->capture_length = -1;
     854        packet->cached.capture_length = -1;
    855855        erfptr->rlen = htons(size + trace_get_framing_length(packet));
    856856        wlen = ntohs(erfptr->wlen);
  • lib/format_linux_int.c

    r2193905 rd439067  
    461461       
    462462        /* Reset the cached capture length */
    463         packet->capture_length = -1;
     463        packet->cached.capture_length = -1;
    464464
    465465        linux_hdr = (struct libtrace_linuxnative_header *)packet->header;
  • lib/format_linux_ring.c

    rc337c62f rd439067  
    468468
    469469        /* Reset the cached capture length */
    470         packet->capture_length = -1;
     470        packet->cached.capture_length = -1;
    471471
    472472        TO_TP_HDR2(packet->buffer)->tp_snaplen = size;
  • lib/format_pcap.c

    rc434445 rd439067  
    765765        }
    766766        /* Reset the cached capture length */
    767         packet->capture_length = -1;
     767        packet->cached.capture_length = -1;
    768768        pcapptr = (struct pcap_pkthdr *)packet->header;
    769769        pcapptr->caplen = size;
  • lib/format_pcapfile.c

    r509ee47 rd439067  
    425425        /* We may as well cache this value now, seeing as we already had to
    426426         * look it up */
    427         packet->capture_length = bytes_to_read;
     427        packet->cached.capture_length = bytes_to_read; 
    428428        return sizeof(libtrace_pcapfile_pkt_hdr_t) + bytes_to_read;
    429429}
     
    712712        }
    713713        /* Reset the cached capture length */
    714         packet->capture_length = -1;
     714        packet->cached.capture_length = -1;
    715715        pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header;
    716716        pcapptr->caplen = swapl(packet->trace,(uint32_t)size);
  • lib/format_pcapng.c

    r509ee47 rd439067  
    840840        /* May as well cache the capture length now, since we've
    841841         * already got it in the right byte order */
    842         packet->capture_length = caplen;
     842        packet->cached.capture_length = caplen;
    843843
    844844        if (pcapng_prepare_packet(libtrace, packet, packet->buffer,
     
    899899        /* May as well cache the capture length now, since we've
    900900         * already got it in the right byte order */
    901         packet->capture_length = caplen;
     901        packet->cached.capture_length = caplen;
    902902
    903903        if (pcapng_prepare_packet(libtrace, packet, packet->buffer,
     
    13491349                }
    13501350        }
    1351         packet->capture_length = -1;
     1351        packet->cached.capture_length = -1;
    13521352        return trace_get_capture_length(packet);
    13531353}
  • lib/libtrace.h.in

    r509ee47 rd439067  
    577577} libtrace_ethertype_t;
    578578
    579 /** The libtrace packet structure. Applications shouldn't be
    580  * meddling around in here
    581  */
    582 typedef struct libtrace_packet_t {
    583         struct libtrace_t *trace;       /**< Pointer to the trace */
    584         void *header;                   /**< Pointer to the framing header */
    585         void *payload;                  /**< Pointer to the link layer */
    586         void *buffer;                   /**< Allocated buffer */
    587         libtrace_rt_types_t  type;      /**< RT protocol type for the packet */
    588         buf_control_t buf_control;      /**< Describes memory ownership */
     579typedef struct libtrace_packet_cache {
    589580        int capture_length;             /**< Cached capture length */
    590581        int wire_length;                /**< Cached wire length */
     
    600591        uint8_t transport_proto;        /**< Cached transport protocol */
    601592        uint32_t l4_remaining;          /**< Cached transport remaining */
     593} libtrace_packet_cache_t;
     594
     595/** The libtrace packet structure. Applications shouldn't be
     596 * meddling around in here
     597 */
     598typedef struct libtrace_packet_t {
     599        struct libtrace_t *trace;       /**< Pointer to the trace */
     600        void *header;                   /**< Pointer to the framing header */
     601        void *payload;                  /**< Pointer to the link layer */
     602        void *buffer;                   /**< Allocated buffer */
     603        libtrace_rt_types_t  type;      /**< RT protocol type for the packet */
     604        buf_control_t buf_control;      /**< Describes memory ownership */
     605        libtrace_packet_cache_t cached; /**< Cached packet properties / headers */
    602606        uint64_t order; /**< Notes the order of this packet in relation to the input */
    603607        uint64_t hash; /**< A hash of the packet as supplied by the user */
  • lib/protocols_l2.c

    r2193905 rd439067  
    181181                        (dest - (char *)packet->payload));
    182182                packet->payload = nextpayload - (dest - (char *)packet->payload);
    183                 packet->l2_header = NULL;
     183                packet->cached.l2_header = NULL;
    184184        }
    185185       
     
    456456                remaining = &dummyrem;
    457457
    458         if (packet->l2_header) {
     458        if (packet->cached.l2_header) {
    459459                /* Use cached values */
    460                 *linktype = packet->link_type;
    461                 *remaining = packet->l2_remaining;
    462                 return packet->l2_header;
     460                *linktype = packet->cached.link_type;
     461                *remaining = packet->cached.l2_remaining;
     462                return packet->cached.l2_header;
    463463        }
    464464
     
    487487                case TRACE_TYPE_NONDATA:
    488488                case TRACE_TYPE_OPENBSD_LOOP:
    489                         ((libtrace_packet_t*)packet)->l2_header = meta;
    490                         ((libtrace_packet_t*)packet)->l2_remaining = *remaining;
     489                        ((libtrace_packet_t*)packet)->cached.l2_header = meta;
     490                        ((libtrace_packet_t*)packet)->cached.l2_remaining = *remaining;
    491491                        return meta;
    492492                case TRACE_TYPE_LINUX_SLL:
     
    525525                                case TRACE_TYPE_NONDATA:
    526526                                case TRACE_TYPE_OPENBSD_LOOP:
    527                                         ((libtrace_packet_t*)packet)->l2_header = meta;
    528                                         ((libtrace_packet_t*)packet)->l2_remaining = *remaining;
     527                                        ((libtrace_packet_t*)packet)->cached.l2_header = meta;
     528                                        ((libtrace_packet_t*)packet)->cached.l2_remaining = *remaining;
    529529                                        return meta;
    530530                                case TRACE_TYPE_LINUX_SLL:
  • lib/protocols_l3.c

    r2193905 rd439067  
    226226
    227227        /* use l3 cache */
    228         if (packet->l3_header)
     228        if (packet->cached.l3_header)
    229229        {
    230230                /*
     
    235235                */
    236236
    237                 *ethertype = packet->l3_ethertype;
    238                 /* *remaining -= (packet->l3_header - link); */
    239                 *remaining = packet->l3_remaining;
    240 
    241                 return packet->l3_header;
    242         }
    243 
    244         if (packet->l2_header) {
    245                 link = packet->l2_header;
    246                 linktype = packet->link_type;
    247                 *remaining = packet->l2_remaining;
     237                *ethertype = packet->cached.l3_ethertype;
     238                /* *remaining -= (packet->cached.l3_header - link); */
     239                *remaining = packet->cached.l3_remaining;
     240
     241                return packet->cached.l3_header;
     242        }
     243
     244        if (packet->cached.l2_header) {
     245                link = packet->cached.l2_header;
     246                linktype = packet->cached.link_type;
     247                *remaining = packet->cached.l2_remaining;
    248248        } else {
    249249                link = trace_get_layer2(packet,&linktype,remaining);
     
    288288        /* Store values in the cache for later */
    289289        /* Cast away constness, nasty, but this is just a cache */
    290         ((libtrace_packet_t*)packet)->l3_ethertype = *ethertype;
    291         ((libtrace_packet_t*)packet)->l3_header = iphdr;
    292         ((libtrace_packet_t*)packet)->l3_remaining = *remaining;
     290        ((libtrace_packet_t*)packet)->cached.l3_ethertype = *ethertype;
     291        ((libtrace_packet_t*)packet)->cached.l3_header = iphdr;
     292        ((libtrace_packet_t*)packet)->cached.l3_remaining = *remaining;
    293293
    294294        return iphdr;
  • lib/protocols_transport.c

    r2193905 rd439067  
    6464
    6565        /* Just use the cached length if we can */
    66         if (packet->payload_length != -1)
    67                 return packet->payload_length; 
     66        if (packet->cached.payload_length != -1)
     67                return packet->cached.payload_length;   
    6868
    6969        /* Set to zero so that we can return early without having to
    7070         * worry about forgetting to update the cached value */
    71         ((libtrace_packet_t *)packet)->payload_length = 0;
     71        ((libtrace_packet_t *)packet)->cached.payload_length = 0;
    7272        layer = trace_get_layer3(packet, &ethertype, &rem);
    7373        if (!layer)
     
    162162        }
    163163
    164         ((libtrace_packet_t *)packet)->payload_length = len;
     164        ((libtrace_packet_t *)packet)->cached.payload_length = len;
    165165        return len;
    166166
     
    181181        if (!remaining) remaining=&dummy_remaining;
    182182
    183         if (packet->l4_header) {
     183        if (packet->cached.l4_header) {
    184184                /*
    185185                void *link;
     
    189189                        return NULL;
    190190                */
    191                 *proto = packet->transport_proto;
    192                 /* *remaining -= (packet->l4_header - link); */
    193                 *remaining = packet->l4_remaining;
    194                 return packet->l4_header;
     191                *proto = packet->cached.transport_proto;
     192                *remaining = packet->cached.l4_remaining;
     193                return packet->cached.l4_header;
    195194        }
    196195
     
    221220        }
    222221
    223         ((libtrace_packet_t *)packet)->transport_proto = *proto;
    224         ((libtrace_packet_t *)packet)->l4_header = transport;
    225         ((libtrace_packet_t *)packet)->l4_remaining = *remaining;
     222        ((libtrace_packet_t *)packet)->cached.transport_proto = *proto;
     223        ((libtrace_packet_t *)packet)->cached.l4_header = transport;
     224        ((libtrace_packet_t *)packet)->cached.l4_remaining = *remaining;
    226225
    227226
  • lib/trace.c

    r509ee47 rd439067  
    103103/* Set once pstart is called used for backwards compatibility reasons */
    104104int libtrace_parallel = 0;
     105
     106static const libtrace_packet_cache_t clearcache = {
     107        -1, -1, -1, -1, NULL, 0, 0, NULL, 0, 0, NULL, 0, 0};
    105108
    106109/* strncpy is not assured to copy the final \0, so we
     
    14021405                return ~0U;
    14031406        }
    1404         if (packet->capture_length == -1) {
     1407        if (packet->cached.capture_length == -1) {
    14051408                if (!packet->trace->format->get_capture_length)
    14061409                        return ~0U;
    14071410                /* Cast away constness because this is "just" a cache */
    1408                 ((libtrace_packet_t*)packet)->capture_length =
     1411                ((libtrace_packet_t*)packet)->cached.capture_length =
    14091412                        packet->trace->format->get_capture_length(packet);
    14101413        }
    14111414
    1412         if (!(packet->capture_length < LIBTRACE_PACKET_BUFSIZE)) {
     1415        if (!(packet->cached.capture_length < LIBTRACE_PACKET_BUFSIZE)) {
    14131416                fprintf(stderr, "Capture length is greater than the buffer size in trace_get_capture_length()\n");
    14141417                return 0;
     
    14161419        }
    14171420
    1418         return packet->capture_length;
     1421        return packet->cached.capture_length;
    14191422}
    14201423
     
    14321435        }
    14331436
    1434         if (packet->wire_length == -1) {
     1437        if (packet->cached.wire_length == -1) {
    14351438                if (!packet->trace->format->get_wire_length)
    14361439                        return ~0U;
    1437                 ((libtrace_packet_t *)packet)->wire_length =
     1440                ((libtrace_packet_t *)packet)->cached.wire_length =
    14381441                        packet->trace->format->get_wire_length(packet);
    14391442        }
    14401443
    1441         if (!(packet->wire_length < LIBTRACE_PACKET_BUFSIZE)) {
     1444        if (!(packet->cached.wire_length < LIBTRACE_PACKET_BUFSIZE)) {
    14421445                fprintf(stderr, "Wire length is greater than the buffer size in trace_get_wire_length()\n");
    14431446                return 0;
    14441447                /* should we be returning ~OU here? */
    14451448        }
    1446         return packet->wire_length;
     1449        return packet->cached.wire_length;
    14471450
    14481451}
     
    14601463        }
    14611464
    1462         if (packet->framing_length >= 0) {
    1463                 return packet->framing_length;
     1465        if (packet->cached.framing_length >= 0) {
     1466                return packet->cached.framing_length;
    14641467        }
    14651468
    14661469        if (packet->trace->format->get_framing_length) {
    1467                 ((libtrace_packet_t *)packet)->framing_length =
     1470                ((libtrace_packet_t *)packet)->cached.framing_length =
    14681471                       packet->trace->format->get_framing_length(packet);
    1469                 return packet->framing_length;
     1472                return packet->cached.framing_length;
    14701473        }
    14711474        return ~0U;
     
    14831486        }
    14841487
    1485         if (packet->link_type == 0) {
     1488        if (packet->cached.link_type == 0) {
    14861489                if (!packet->trace->format->get_link_type)
    14871490                        return TRACE_TYPE_UNKNOWN;
    1488                 ((libtrace_packet_t *)packet)->link_type =
     1491                ((libtrace_packet_t *)packet)->cached.link_type =
    14891492                        packet->trace->format->get_link_type(packet);
    14901493        }
    14911494
    1492         return packet->link_type;
     1495        return packet->cached.link_type;
    14931496}
    14941497
     
    19761979
    19771980        if (packet->trace->format->set_capture_length) {
    1978                 packet->capture_length = packet->trace->format->set_capture_length(packet,size);
    1979                 return packet->capture_length;
     1981                packet->cached.capture_length = packet->trace->format->set_capture_length(packet,size);
     1982                return packet->cached.capture_length;
    19801983        }
    19811984
     
    25702573
    25712574
    2572 void trace_clear_cache(libtrace_packet_t *packet) {
    2573 
    2574         packet->l2_header = NULL;
    2575         packet->l3_header = NULL;
    2576         packet->l4_header = NULL;
    2577         packet->link_type = 0;
    2578         packet->l3_ethertype = 0;
    2579         packet->transport_proto = 0;
    2580         packet->capture_length = -1;
    2581         packet->wire_length = -1;
    2582         packet->framing_length = -1;
    2583         packet->payload_length = -1;
    2584         packet->l2_remaining = 0;
    2585         packet->l3_remaining = 0;
    2586         packet->l4_remaining = 0;
    2587         packet->refcount = 0;
     2575inline void trace_clear_cache(libtrace_packet_t *packet) {
     2576
     2577        packet->cached = clearcache;
    25882578}
    25892579
Note: See TracChangeset for help on using the changeset viewer.