Changeset 0277ab8 for lib/protocols_l2.c


Ignore:
Timestamp:
08/05/15 13:50:02 (5 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, 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:
d280f48
Parents:
84d137d
Message:

Completed improvement of trace_strip_packet()

  • Removed the trace_copy_packet() if the packet is not totally owned by libtrace. I think we can get away without it and we really don't want the performance hit when dealing with live captures.
  • Replaced call to trace_get_layer3 with the old header parsing code from the previous version of trace_strip_packet(). The overhead of calling trace_get_layer3 was outweighing the performance gain from the change in memmove strategy.
  • Removed ability to selectively strip VLAN or MPLS headers only -- this was not that useful and supporting it would make the code much more complex.
  • Updated trace_get_layer3 to use the cached layer 2 header if available rather than making a call to trace_get_layer2 and getting it from there. This saves us a bit of function call overhead.
  • Make sure we reset the layer 2 cached info if we shift the Ethernet header.
  • Strip PPPoE in addition to MPLS and VLAN.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/protocols_l2.c

    r84d137d r0277ab8  
    101101}
    102102
    103 libtrace_packet_t *trace_strip_packet(libtrace_packet_t *packet,
    104                 int stripopts) {
     103libtrace_packet_t *trace_strip_packet(libtrace_packet_t *packet) {
    105104
    106105        libtrace_ether_t *ethernet;
     
    108107        uint16_t ethertype;
    109108        uint32_t remaining;
    110         libtrace_packet_t *copy;
    111         char *payload;
     109        char *nextpayload;
    112110        uint16_t finalethertype = 0;
    113111        uint16_t caplen, removed = 0;
     
    130128        }
    131129
    132         /* Copy the packet as we need to be sure that the packet
    133          * payload is contiguous. This won't be guaranteed for live
    134          * formats, for instance.
     130        if (remaining <= sizeof(libtrace_ether_t))
     131                return packet;
     132
     133        caplen = trace_get_capture_length(packet);
     134        ethertype = ntohs(ethernet->ether_type);
     135        dest = ((char *)ethernet) + sizeof(libtrace_ether_t);
     136        nextpayload = dest;
     137        remaining -= sizeof(libtrace_ether_t);
     138
     139        /* I'd normally use trace_get_layer3 here, but it works out faster
     140         * to do it this way (mostly less function call overhead).
     141         *
     142         * XXX This approach is going to just strip everything between the
     143         * Ethernet and IP headers -- is there a use case where someone
     144         * might want to selectively strip headers?
    135145         */
    136         if (packet->buf_control == TRACE_CTRL_EXTERNAL) {
    137                 copy = trace_copy_packet(packet);
    138                 trace_destroy_packet(packet);
    139                 packet = copy;
    140 
    141                 /* Re-grab the ethernet header from the copy */
    142                 ethernet = (libtrace_ether_t *)trace_get_layer2(packet,
    143                         &linktype, &remaining);
    144 
    145         }
    146 
    147         payload = (char *)trace_get_layer3(packet, &ethertype, &remaining);
    148         caplen = trace_get_capture_length(packet);
    149         dest = ((char *)ethernet) + sizeof(libtrace_ether_t);
    150 
    151         if (payload == NULL || remaining == 0)
    152                 return packet;
    153 
    154         if (payload == dest)
    155                 return packet;
    156 
    157         ethernet->ether_type = ntohs(ethertype);
    158         trace_set_capture_length(packet, caplen - (payload - dest));
    159         memmove(payload - (dest - (char *)packet->payload), packet->payload,
    160                         (dest - (char *)packet->payload));
    161         packet->payload = payload - (dest - (char *)packet->payload);
    162         packet->l2_header = NULL;
    163         packet->l3_header = NULL;
    164         packet->l4_header = NULL;
    165        
    166         /*
    167         payload = trace_get_payload_from_layer2(ethernet, linktype,
    168                         &ethertype, &remaining);
    169 
    170146        while (!done) {
    171147
    172                 if (payload == NULL || remaining == 0)
     148                if (nextpayload == NULL || remaining == 0)
    173149                        break;
    174150
     
    177153
    178154                case TRACE_ETHERTYPE_8021Q:
    179                         payload = (void *)trace_get_payload_from_vlan(payload,
     155                        nextpayload = (char *)trace_get_payload_from_vlan(
     156                                        nextpayload,
    180157                                        &ethertype, &remaining);
    181                         if (stripopts == 0 || (stripopts & TRACE_STRIP_VLAN))
    182                         {
    183                                 removed += (oldrem - remaining);
    184                                 //memmove(dest, payload, remaining);
    185                                 //payload = dest;
    186 
    187                         } else {
    188                                 if (finalethertype == 0) {
    189                                         finalethertype = TRACE_ETHERTYPE_8021Q;
    190                                 }
    191                                 //dest = payload;
    192                         }
     158                        removed += (oldrem - remaining);
    193159                        break;
    194160
    195161                case TRACE_ETHERTYPE_MPLS:
    196                         payload = (void *)trace_get_payload_from_mpls(payload,
     162                        nextpayload = (char *)trace_get_payload_from_mpls(
     163                                        nextpayload,
    197164                                        &ethertype, &remaining);
    198                         if (stripopts == 0 || (stripopts & TRACE_STRIP_MPLS))
    199                         {
    200                                 removed += (oldrem - remaining);
    201                                 //memmove(dest, payload, remaining);
    202                                 //payload = dest;
    203 
    204                         } else {
    205                                 if (finalethertype == 0) {
    206                                         finalethertype = TRACE_ETHERTYPE_MPLS;
    207                                 }
    208                                 //dest = payload;
    209                         }
     165                        removed += (oldrem - remaining);
     166                        break;
     167                case TRACE_ETHERTYPE_PPP_SES:
     168                        nextpayload = (char *)trace_get_payload_from_pppoe(
     169                                        nextpayload,
     170                                        &ethertype, &remaining);
     171                        removed += (oldrem - remaining);
    210172                        break;
    211173
     
    219181                }
    220182        }
    221                 */
    222         /* Update the preceding headers to match the new packet contents */
     183
     184        if (nextpayload != NULL) {
     185
     186                ethernet->ether_type = ntohs(finalethertype);
     187                trace_set_capture_length(packet, caplen - removed);
     188                memmove(nextpayload - (dest - (char *)packet->payload),
     189                        packet->payload,
     190                        (dest - (char *)packet->payload));
     191                packet->payload = nextpayload - (dest - (char *)packet->payload);
     192                packet->l2_header = NULL;
     193                packet->link_type = finalethertype;
     194        }
     195       
    223196        return packet;
    224197
Note: See TracChangeset for help on using the changeset viewer.