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 | 25 +++++++++++--------------
 trafgen_proto.h |  1 +
 2 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/trafgen_proto.c b/trafgen_proto.c
index 8c316b1..b0a198f 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -31,20 +31,13 @@ static const struct proto_ops *registered_ops[__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)
@@ -121,11 +114,12 @@ bool proto_field_is_set(struct proto_hdr *hdr, uint32_t 
fid)
 
 struct proto_hdr *proto_header_push(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_ops *ops = proto_ops_by_id(pid);
        struct proto_hdr *hdr;
 
-       bug_on(current_packet()->headers_count >= PROTO_MAX_LAYERS);
+       bug_on(pkt->headers_count >= PROTO_MAX_LAYERS);
 
        hdr = xzmalloc(sizeof(*hdr));
        hdr->ops = ops;
@@ -134,8 +128,11 @@ struct proto_hdr *proto_header_push(enum proto_id pid)
        if (ops && ops->header_init)
                ops->header_init(hdr);
 
-       headers[current_packet()->headers_count++] = hdr;
+       /* This is very important to have it after header_init as
+        * pkt->headers_count might be changed by adding default lower headers 
*/
+       hdr->index = pkt->headers_count;
 
+       headers[pkt->headers_count++] = hdr;
        return hdr;
 }
 
diff --git a/trafgen_proto.h b/trafgen_proto.h
index ab294df..f9f4e13 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -46,6 +46,7 @@ struct proto_hdr {
        const struct proto_ops *ops;
        uint16_t pkt_offset;
        uint32_t pkt_id;
+       uint32_t index;
        struct proto_field *fields;
        size_t fields_count;
        bool is_csum_valid;
-- 
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