summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2016-02-01 19:01:37 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-02-02 16:54:47 +0100
commita7f35557331a283f4064c578f76be6330d54eaf4 (patch)
treed49ce92fb34fab66e613b70dc2d9dd600ebb14e5
parent826a16efdd760930eb2cb21ae80c49a9d7d096f4 (diff)
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 <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--trafgen_l2.c24
-rw-r--r--trafgen_l3.c4
2 files changed, 24 insertions, 4 deletions
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 <net/if_arp.h>
#include <linux/if_ether.h>
+#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 9e5126a..58eaa01 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));