[dpdk-dev] [PATCH v2 1/7] ether: mark ethernet addresses as being 2-byte aligned

Stephen Hemminger stephen at networkplumber.org
Thu May 16 20:04:21 CEST 2019


From: Bruce Richardson <bruce.richardson at intel.com>

When including the rte_ether.h header in applications with warnings
enabled, a warning was given because of the assumption of 2-byte alignment
of ethernet addresses when processing them.

.../include/rte_ether.h:149:2: warning: converting a packed ‘const
  struct ether_addr’ pointer (alignment 1) to a ‘unaligned_uint16_t’
  {aka ‘const short unsigned int’} pointer (alignment 2) may result in
  an unaligned pointer value [-Waddress-of-packed-member]
149 |  const unaligned_uint16_t *ea_words = (const unaligned_uint16_t *)ea;
    |  ^~~~~

Since ethernet addresses should always be aligned on a two-byte boundary,
we can just inform the compiler of this assumption to remove the warnings
and allow us to always access the addresses using 16-bit operations.

Signed-off-by: Bruce Richardson <bruce.richardson at intel.com>
Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
 lib/librte_net/rte_ether.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h
index 3a87ff184900..ac8897681aca 100644
--- a/lib/librte_net/rte_ether.h
+++ b/lib/librte_net/rte_ether.h
@@ -55,7 +55,8 @@ extern "C" {
  * See http://standards.ieee.org/regauth/groupmac/tutorial.html
  */
 struct ether_addr {
-	uint8_t addr_bytes[ETHER_ADDR_LEN]; /**< Addr bytes in tx order */
+	/** Addr bytes in tx order */
+	uint8_t addr_bytes[ETHER_ADDR_LEN] __rte_aligned(2);
 } __attribute__((__packed__));
 
 #define ETHER_LOCAL_ADMIN_ADDR 0x02 /**< Locally assigned Eth. address. */
@@ -146,7 +147,7 @@ static inline int is_multicast_ether_addr(const struct ether_addr *ea)
  */
 static inline int is_broadcast_ether_addr(const struct ether_addr *ea)
 {
-	const unaligned_uint16_t *ea_words = (const unaligned_uint16_t *)ea;
+	const uint16_t *ea_words = (const uint16_t *)ea;
 
 	return (ea_words[0] == 0xFFFF && ea_words[1] == 0xFFFF &&
 		ea_words[2] == 0xFFFF);
-- 
2.20.1



More information about the dev mailing list