From 03bfca429c99c2c519e661461201f7c7c2fc2d60 Mon Sep 17 00:00:00 2001
From: Neal Cardwell <ncardwell@google.com>
Date: Mon, 20 Jan 2014 19:05:51 -0500
Subject: [PATCH] net-test: packetdrill: fix packet_append_header()
 calculations for remote mode

Fix the packet header location calculations in packet_append_header()
forgot to account for the fact that there might be layer 2 headers.

Remote mode has been broken since the addition of encapsulation
support, partly due to this issue.

Change-Id: Idfb0670da8799e11fe1b72771ed13d52d8e991fe
---
 gtests/net/packetdrill/packet.c        | 4 +++-
 gtests/net/packetdrill/packet.h        | 3 ++-
 gtests/net/packetdrill/packet_parser.c | 1 +
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/gtests/net/packetdrill/packet.c b/gtests/net/packetdrill/packet.c
index c52bb143..b1810ff1 100644
--- a/gtests/net/packetdrill/packet.c
+++ b/gtests/net/packetdrill/packet.c
@@ -102,6 +102,7 @@ struct header *packet_append_header(struct packet *packet,
 {
 	struct header *header = NULL;
 	int num_headers = packet_header_count(packet);
+	int packet_bytes;
 
 	assert(num_headers <= PACKET_MAX_HEADERS);
 	if (num_headers == PACKET_MAX_HEADERS)
@@ -111,7 +112,8 @@ struct header *packet_append_header(struct packet *packet,
 
 	if (packet->ip_bytes + header_bytes > packet->buffer_bytes)
 		return NULL;
-	header->h.ptr = packet->buffer + packet->ip_bytes;
+	packet_bytes = packet->l2_header_bytes + packet->ip_bytes;
+	header->h.ptr = packet->buffer + packet_bytes;
 	packet->ip_bytes += header_bytes;
 
 	header->type = header_type;
diff --git a/gtests/net/packetdrill/packet.h b/gtests/net/packetdrill/packet.h
index 4ca1059a..83869bcf 100644
--- a/gtests/net/packetdrill/packet.h
+++ b/gtests/net/packetdrill/packet.h
@@ -69,7 +69,8 @@ static const int PACKET_READ_BYTES = 64 * 1024;
 struct packet {
 	u8 *buffer;		/* data buffer: full contents of packet */
 	u32 buffer_bytes;	/* bytes of space in data buffer */
-	u32 ip_bytes;		/* bytes on wire: outermost IP hdrs/payload */
+	u32 l2_header_bytes;	/* bytes in outer hardware/layer-2 header */
+	u32 ip_bytes;		/* bytes in outermost IP hdrs/payload */
 	enum direction_t direction;	/* direction packet is traveling */
 
 	/* Metadata about all the headers in the packet, including all
diff --git a/gtests/net/packetdrill/packet_parser.c b/gtests/net/packetdrill/packet_parser.c
index ff64dedb..1b4d631d 100644
--- a/gtests/net/packetdrill/packet_parser.c
+++ b/gtests/net/packetdrill/packet_parser.c
@@ -71,6 +71,7 @@ static int parse_layer2_packet(struct packet *packet,
 	}
 	ether = (struct ether_header *)p;
 	p += sizeof(*ether);
+	packet->l2_header_bytes = sizeof(*ether);
 
 	return parse_layer3_packet_by_proto(packet, ntohs(ether->ether_type),
 					    p, packet_end, error);
-- 
GitLab