From b12ceb07177473337a32022ecabaded6f9187c17 Mon Sep 17 00:00:00 2001
From: Julien Grall <julien.grall@linaro.org>
Date: Wed, 4 Jun 2014 22:13:20 +0100
Subject: [PATCH 02/48] xen/netfront: Add 2 bytes padding in the rx mbuf

The ethernet header size is not word aligned. Therefore the IP packet and so
on won't be align. On some architecture (such as ARM) unaligned access may
be slower and/or defined. Therefore we might reveice an alignement fault.
To void this case, we need to pull-up the data of ETHER_ALIGN bytes.

I'm not sure how this patch will impact x86, we need to do some benchmarking
without and with it.

I'mi also not sure m_copyup is the right function to any. Can any expert to the
network stack can tell me if there is a better solution?
---
 sys/dev/xen/netfront/mbufq.h    |  2 ++
 sys/dev/xen/netfront/netfront.c | 15 +++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/sys/dev/xen/netfront/mbufq.h b/sys/dev/xen/netfront/mbufq.h
index 0d6c604..4e3a7f7 100644
--- a/sys/dev/xen/netfront/mbufq.h
+++ b/sys/dev/xen/netfront/mbufq.h
@@ -32,6 +32,8 @@ $FreeBSD$
 #ifndef CXGB_MBUFQ_H_
 #define CXGB_MBUFQ_H_
 
+#define ETHER_ALIGN	2
+
 struct mbuf_head {
 	struct mbuf *head;
 	struct mbuf *tail;
diff --git a/sys/dev/xen/netfront/netfront.c b/sys/dev/xen/netfront/netfront.c
index c3c8d92..8478aea 100644
--- a/sys/dev/xen/netfront/netfront.c
+++ b/sys/dev/xen/netfront/netfront.c
@@ -1430,6 +1430,21 @@ next_skip_queue:
 		ref_cons = *cons + frags;
 		frags++;
 	}
+
+	if (!err) {
+		/* The ethernet header size is not word aligned. Therefore,
+		 * the IP packet and so on won't be align. On some architecture
+		 * (such as ARM) unaligned access may be slower and/or denied.
+		 * Therefore we might receive an alignment fault.
+		 * To avoid this case, we need to pull-up the data of
+		 * ETHER_ALIGN bytes.
+		 */
+		m0 = m_copyup(m0, min(m0->m_pkthdr.len, max_protohdr),
+			      ETHER_ALIGN);
+		if (!m0)
+			err = ENOMEM;
+	}
+
 	*list = m0;
 	*cons += frags;
 	*pages_flipped_p = pages_flipped;
-- 
2.1.0

