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 | |
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>
-rw-r--r-- | trafgen_proto.c | 19 | ||||
-rw-r--r-- | trafgen_proto.h | 3 |
2 files changed, 9 insertions, 13 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); diff --git a/trafgen_proto.h b/trafgen_proto.h index f25078a..a66f8d3 100644 --- a/trafgen_proto.h +++ b/trafgen_proto.h @@ -6,7 +6,7 @@ #include <stdbool.h> enum proto_id { - PROTO_NONE, + PROTO_NONE = 0, PROTO_ETH, PROTO_VLAN, PROTO_ARP, @@ -17,6 +17,7 @@ enum proto_id { PROTO_ICMP6, PROTO_UDP, PROTO_TCP, + __PROTO_MAX, }; enum proto_layer { |