From 439af62bca4794d78d53fb4634f560d6a75f0adb Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Sat, 29 Jul 2017 12:46:09 +0300 Subject: trafgen: Dump proto headers in *.cfg format Added trafgen_dump.c module which dumps headers from packet in .cfg format. Packet is dumped if -o .cfg was specified, it might be useful to specify *.pcap file as input and convert it into .cfg file to edit proto fields in human readable format. To make it possible several main changes were added: 1) packet id is embedded into struct packet.id, and it is updated on each realloc_packet() 2) Added new struct proto_hdr.get_next_proto callback to make possible apply fields of next header. 3) Added new dev_io ops for writting packets into .cfg file, to re-use common dev_io mechsnism for packets dumping. Before dump the default ETH_PROTO fields are applied as first header and then next proto_hdr is identified via .get_next_proto(...) callback. Meanwhile only eth, arp, vlan, ip4, udp, & tcp protos can be dissected into *.cfg format. Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- trafgen_l2.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'trafgen_l2.c') diff --git a/trafgen_l2.c b/trafgen_l2.c index 4858c5f..48c6f6f 100644 --- a/trafgen_l2.c +++ b/trafgen_l2.c @@ -40,11 +40,37 @@ static uint16_t pid_to_eth(enum proto_id pid) } } +static uint16_t eth_to_pid(uint16_t etype) +{ + switch (etype) { + case ETH_P_ARP: + return PROTO_ARP; + case ETH_P_IP: + return PROTO_IP4; + case ETH_P_IPV6: + return PROTO_IP6; + case ETH_P_MPLS_UC: + return PROTO_MPLS; + case ETH_P_8021Q: + case ETH_P_8021AD: + return PROTO_VLAN; + case ETH_P_PAUSE: + return PROTO_PAUSE; + default: + return __PROTO_MAX; + } +} + static void eth_set_next_proto(struct proto_hdr *hdr, enum proto_id pid) { proto_hdr_field_set_default_be16(hdr, ETH_TYPE, pid_to_eth(pid)); } +static enum proto_id eth_get_next_proto(struct proto_hdr *hdr) +{ + return eth_to_pid(proto_hdr_field_get_u16(hdr, ETH_TYPE)); +} + static void eth_header_init(struct proto_hdr *hdr) { proto_header_fields_add(hdr, eth_fields, array_size(eth_fields)); @@ -59,6 +85,7 @@ static const struct proto_ops eth_proto_ops = { .layer = PROTO_L2, .header_init = eth_header_init, .set_next_proto = eth_set_next_proto, + .get_next_proto = eth_get_next_proto, }; static struct proto_field pause_fields[] = { @@ -158,11 +185,17 @@ static void vlan_set_next_proto(struct proto_hdr *hdr, enum proto_id pid) proto_hdr_field_set_be16(hdr, VLAN_ETYPE, pid_to_eth(pid)); } +static enum proto_id vlan_get_next_proto(struct proto_hdr *hdr) +{ + return eth_to_pid(proto_hdr_field_get_u16(hdr, VLAN_ETYPE)); +} + static const struct proto_ops vlan_proto_ops = { .id = PROTO_VLAN, .layer = PROTO_L2, .header_init = vlan_header_init, .set_next_proto = vlan_set_next_proto, + .get_next_proto = vlan_get_next_proto, }; static struct proto_field arp_fields[] = { -- cgit v1.2.3-54-g00ecf