From a7f35557331a283f4064c578f76be6330d54eaf4 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Mon, 1 Feb 2016 19:01:37 +0200 Subject: trafgen: eth: Add setting next protocol id Move setting next protocol id field from higher protocols (ARP, IPv4) to Ethernet. It makes code little more generic w/o checking each lower protocol and setting specific field id. Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- trafgen_l2.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'trafgen_l2.c') diff --git a/trafgen_l2.c b/trafgen_l2.c index 529dc36..c0e92a3 100644 --- a/trafgen_l2.c +++ b/trafgen_l2.c @@ -6,6 +6,7 @@ #include #include +#include "die.h" #include "built_in.h" #include "trafgen_l2.h" #include "trafgen_proto.h" @@ -16,6 +17,27 @@ static struct proto_field eth_fields[] = { { .id = ETH_TYPE, .len = 2, .offset = 12 }, }; +static void eth_set_next_proto(struct proto_hdr *hdr, enum proto_id pid) +{ + uint16_t eth_type; + + switch(pid) { + case PROTO_ARP: + eth_type = ETH_P_ARP; + break; + case PROTO_IP4: + eth_type = ETH_P_IP; + break; + case PROTO_IP6: + eth_type = ETH_P_IPV6; + break; + default: + panic("eth: Not supported protocol id %u\n", pid); + } + + proto_field_set_default_be16(hdr, ETH_TYPE, eth_type); +} + static void eth_header_init(struct proto_hdr *hdr) { proto_header_fields_add(hdr, eth_fields, array_size(eth_fields)); @@ -27,6 +49,7 @@ static struct proto_hdr eth_hdr = { .id = PROTO_ETH, .layer = PROTO_L2, .header_init = eth_header_init, + .set_next_proto = eth_set_next_proto, }; static struct proto_field arp_fields[] = { @@ -51,7 +74,6 @@ static void arp_header_init(struct proto_hdr *hdr) uint8_t bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; proto_field_set_default_bytes(lower, ETH_DST_ADDR, bcast); - proto_field_set_default_be16(lower, ETH_TYPE, ETH_P_ARP); } proto_header_fields_add(hdr, arp_fields, array_size(arp_fields)); -- cgit v1.2.3-54-g00ecf