summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2016-08-05 15:04:38 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-08-05 15:09:01 +0200
commita0e2b28801d2ddb5ccf80a7c5d0b9ac9b75f736d (patch)
tree3b8f27a81ebec2d53e561fab9961024fc0217088
parentb073c8e5fd7f8c723680b659bd81afd231ef32f5 (diff)
trafgen: proto: Use field id as array index
Usually proto fields array are sorted in the same order as the respective enum. Thus, the id can be used used as an array index for faster lookup. Add an explanatory comment and enforce the correspondence of id and index using bug_on(). This will make csum field calculation a little faster at runtime. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--trafgen_l3.h4
-rw-r--r--trafgen_proto.c11
2 files changed, 7 insertions, 8 deletions
diff --git a/trafgen_l3.h b/trafgen_l3.h
index a1b1523..e0c9a1c 100644
--- a/trafgen_l3.h
+++ b/trafgen_l3.h
@@ -10,14 +10,14 @@ enum ip4_field {
IP4_LEN,
IP4_ID,
IP4_FLAGS,
+ IP4_MF,
+ IP4_DF,
IP4_FRAG_OFFS,
IP4_TTL,
IP4_PROTO,
IP4_CSUM,
IP4_SADDR,
IP4_DADDR,
- IP4_DF,
- IP4_MF,
};
enum ip6_field {
diff --git a/trafgen_proto.c b/trafgen_proto.c
index e08c9c2..212e968 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -107,13 +107,12 @@ void proto_header_fields_add(struct proto_hdr *hdr,
static struct proto_field *proto_field_by_id(struct proto_hdr *hdr, uint32_t fid)
{
- int i;
-
- for (i = 0; i < hdr->fields_count; i++)
- if (hdr->fields[i].id == fid)
- return &hdr->fields[i];
+ /* Assume the fields are stored in the same order as the respective
+ * enum, so the index can be used for faster lookup here.
+ */
+ bug_on(hdr->fields[fid].id != fid);
- panic("Failed lookup field id %u for proto id %u\n", fid, hdr->id);
+ return &hdr->fields[fid];
}
bool proto_field_is_set(struct proto_hdr *hdr, uint32_t fid)