From a7f35557331a283f4064c578f76be6330d54eaf4 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Mon, 1 Feb 2016 19:01:37 +0200 Subject: [PATCH] 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 +++++++++++++++++++++++- trafgen_l3.c | 4 +--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/trafgen_l2.c b/trafgen_l2.c index 529dc363..c0e92a31 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)); diff --git a/trafgen_l3.c b/trafgen_l3.c index 9e5126af..58eaa011 100644 --- a/trafgen_l3.c +++ b/trafgen_l3.c @@ -37,9 +37,7 @@ static void ipv4_header_init(struct proto_hdr *hdr) lower = proto_lower_default_add(hdr, PROTO_ETH); - if (lower->id == PROTO_ETH) - proto_field_set_default_be16(lower, ETH_TYPE, ETH_P_IP); - else if (lower->id == PROTO_IP4) + if (lower->id == PROTO_IP4) proto_field_set_default_u8(lower, IP4_PROTO, IPPROTO_IPIP); proto_header_fields_add(hdr, ipv4_fields, array_size(ipv4_fields)); -- 2.11.4.GIT