diff options
author | Vadim Kochan <vadim4j@gmail.com> | 2016-08-13 02:11:16 +0300 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2016-09-21 09:56:15 +0200 |
commit | 81ca1cd1c362ae536a6bee260827c395a78dc637 (patch) | |
tree | 6ff75b9b554f69e935c43fe721a1b7bc5338dffe /trafgen_proto.c | |
parent | 149e070148b14f34688e71a4c958ea844ff5c7c3 (diff) |
trafgen: proto: Improve to find lower header by index
Extended struct proto_hdr with 'index' field which is used for faster
lookup of lower header without doing a loop.
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_proto.c')
-rw-r--r-- | trafgen_proto.c | 25 |
1 files changed, 11 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; } |