diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2016-08-09 12:09:23 +0200 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2016-08-09 13:56:10 +0200 |
commit | 48178cd649b48196e16e0481ca44dea206fe04d4 (patch) | |
tree | 314f976af644cffe639c8c8655dbe294cac7481b /trafgen_proto.c | |
parent | 7c14a23050eed1575a696dd77c4d5d845eab22c9 (diff) |
trafgen: proto: Store registered protocols in an array
Protocols are registered early at startup and aren't changed at runtime.
In order to speed up lookup while parsing, store the pointers to the
protocol definitions (struct proto_hdr) in an array, indexed by protocol
id rather than in a linked list.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_proto.c')
-rw-r--r-- | trafgen_proto.c | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/trafgen_proto.c b/trafgen_proto.c index f55c1b7..8baacbe 100644 --- a/trafgen_proto.c +++ b/trafgen_proto.c @@ -27,7 +27,7 @@ struct ctx { }; static struct ctx ctx; -static struct proto_hdr *registered; +static const struct proto_hdr *registered[__PROTO_MAX]; struct proto_hdr *proto_lower_header(struct proto_hdr *hdr) { @@ -52,21 +52,16 @@ uint8_t *proto_header_ptr(struct proto_hdr *hdr) return &packet_get(hdr->pkt_id)->payload[hdr->pkt_offset]; } -static struct proto_hdr *proto_header_by_id(enum proto_id id) +static const struct proto_hdr *proto_header_by_id(enum proto_id id) { - struct proto_hdr *p = registered; - - for (; p; p = p->next) - if (p->id == id) - return p; - - panic("Can't lookup proto by id %u\n", id); + bug_on(id >= __PROTO_MAX); + return registered[id]; } void proto_header_register(struct proto_hdr *hdr) { - hdr->next = registered; - registered = hdr; + bug_on(hdr->id >= __PROTO_MAX); + registered[hdr->id] = hdr; hdr->fields = NULL; hdr->fields_count = 0; @@ -128,7 +123,7 @@ 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 proto_hdr *hdr = proto_header_by_id(pid); + const struct proto_hdr *hdr = proto_header_by_id(pid); struct proto_hdr *new_hdr; bug_on(current_packet()->headers_count >= PROTO_MAX_LAYERS); |