Extended struct proto_hdr with 'index' field which is used for
faster lookup of lower header w/o doing a loop.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_proto.c | 26 ++++++++++++--------------
 trafgen_proto.h |  1 +
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/trafgen_proto.c b/trafgen_proto.c
index 072cbcd..fdda2a8 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -31,20 +31,13 @@ static const struct proto_hdr *registered[__PROTO_MAX];
 
 struct proto_hdr *proto_lower_header(struct proto_hdr *hdr)
 {
-       struct proto_hdr **headers = current_packet()->headers;
-       size_t headers_count = current_packet()->headers_count;
-       struct proto_hdr *lower = NULL;
-       size_t i;
+       struct packet *pkt = packet_get(hdr->pkt_id);
+       struct proto_hdr **headers = &pkt->headers[0];
 
-       if (headers_count == 0)
+       if (hdr->index == 0)
                return NULL;
 
-       for (i = 1, lower = headers[0]; i < headers_count; i++) {
-               if (headers[i] == hdr)
-                       return headers[i - 1];
-       }
-
-       return lower;
+       return headers[hdr->index - 1];
 }
 
 uint8_t *proto_header_ptr(struct proto_hdr *hdr)
@@ -122,11 +115,12 @@ bool proto_field_is_set(struct proto_hdr *hdr, uint32_t 
fid)
 
 struct proto_hdr *proto_header_init(enum proto_id pid)
 {
-       struct proto_hdr **headers = current_packet()->headers;
+       struct packet *pkt = current_packet();
+       struct proto_hdr **headers = &pkt->headers[0];
        const struct proto_hdr *hdr = proto_header_by_id(pid);
        struct proto_hdr *new_hdr;
 
-       bug_on(current_packet()->headers_count >= PROTO_MAX_LAYERS);
+       bug_on(pkt->headers_count >= PROTO_MAX_LAYERS);
 
        new_hdr = xmalloc(sizeof(*new_hdr));
        memcpy(new_hdr, hdr, sizeof(*new_hdr));
@@ -136,7 +130,11 @@ struct proto_hdr *proto_header_init(enum proto_id pid)
        if (new_hdr->header_init)
                new_hdr->header_init(new_hdr);
 
-       headers[current_packet()->headers_count++] = new_hdr;
+       /* This is very important to have it after header_init as
+        * pkt->headers_count might be changed by adding default lower headers 
*/
+       new_hdr->index = pkt->headers_count;
+
+       headers[pkt->headers_count++] = new_hdr;
        return new_hdr;
 }
 
diff --git a/trafgen_proto.h b/trafgen_proto.h
index ddf9fb4..5f6deab 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -63,6 +63,7 @@ struct proto_field {
 struct proto_hdr {
        enum proto_id id;
        enum proto_layer layer;
+       uint32_t index;
 
        struct proto_hdr *next;
        uint16_t pkt_offset;
-- 
2.9.2

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to