net: improve vlan header type alignment
PATCH 1/1 net: improve vlan header type alignment
Ethernet packets can be VLAN tagged, i.e. an Ethernet header can have a VLAN tag (a.k.a. VLAN header) embedded.
Since the Ethernet header is 2 byte aligned, and the VLAN tag is directly related to the Ethernet header, the VLAN tag is also 2 byte aligned, so packing the VLAN tag structure is not necessary.
Furthermore, the Ethernet header type is implictly 2 byte aligned, so removed the superfluous explicit 2 byte alignment.
Added static_asserts to verify the size and alignment of the various Ethernet types.
--- lib/net/rte_ether.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
lib/net/rte_ether.h +19 -2
@@ -11,6 +11,8 @@* Ethernet Helpers in RTE */ +#include <assert.h> +#include <stdalign.h> #include <stdint.h> #include <stdio.h>
@@ -75,6 +77,11 @@ struct __rte_aligned(2) rte_ether_addr {uint8_t addr_bytes[RTE_ETHER_ADDR_LEN ];/**< Addr bytes in tx order */ }; +static_assert (sizeof (struct rte_ether_addr ) ==6 , +"sizeof(struct rte_ether_addr) == 6" ); +static_assert (alignof(struct rte_ether_addr ) ==2 , +"alignof(struct rte_ether_addr) == 2" ); +#define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02/**< Locally assigned Eth. address. */ #define RTE_ETHER_GROUP_ADDR 0x01/**< Multicast or broadcast Eth. address. */
@@ -290,12 +297,17 @@ rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr);* Ethernet header: Contains the destination address, source address * and frame type. */ -struct __rte_aligned(2) rte_ether_hdr { +struct rte_ether_hdr {struct rte_ether_addr dst_addr;/**< Destination address. */ struct rte_ether_addr src_addr;/**< Source address. */ rte_be16_t ether_type;/**< Frame type. */ }; +static_assert (sizeof (struct rte_ether_hdr ) ==14 , +"sizeof(struct rte_ether_hdr) == 14" ); +static_assert (alignof(struct rte_ether_hdr ) ==2 , +"alignof(struct rte_ether_hdr) == 2" ); +/** * Ethernet VLAN Header. * Contains the 16-bit VLAN Tag Control Identifier and the Ethernet type
@@ -304,7 +316,12 @@ struct __rte_aligned(2) rte_ether_hdr {struct rte_vlan_hdr {rte_be16_t vlan_tci;/**< Priority (3) + CFI (1) + Identifier Code (12) */ rte_be16_t eth_proto;/**< Ethernet type of encapsulated frame. */ -} __rte_packed; +}; + +static_assert (sizeof (struct rte_vlan_hdr ) ==4 , +"sizeof(struct rte_vlan_hdr) == 4" ); +static_assert (alignof(struct rte_vlan_hdr ) ==2 , +"alignof(struct rte_vlan_hdr) == 2" );
Signature · 1 line
2.43.0
Quoted 9 lines
From: Morten Brørup [mailto:[email protected]]
Sent: Sunday, 13 October 2024 10.36Ethernet packets can be VLAN tagged, i.e. an Ethernet header can have a VLAN tag (a.k.a. VLAN header) embedded.
Since the Ethernet header is 2 byte aligned, and the VLAN tag is directly
related to the Ethernet header, the VLAN tag is also 2 byte aligned, so packing the VLAN tag structure is not necessary.
Stephen, Bruce, you missed the VLAN header type in this patch:
https://git.dpdk.org/dpdk/commit/lib/librte_net/rte_ether.h?id=da5350ef29afd35c1adabe76f60832f3092269ad
This patch completes your work.
Quoted 79 lines
Furthermore, the Ethernet header type is implictly 2 byte aligned, so removed the superfluous explicit 2 byte alignment.
Added
static_assertsto verify the size and alignment of the various Ethernet types.Signed-off-by Morten Brørup <[email protected]>--- lib/net/rte_ether.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)lib/net/rte_ether.h +2
-0@@ -11,6 +11,8 @@* Ethernet Helpers in RTE */ +#include <assert.h> +#include <stdalign.h> #include <stdint.h> #include <stdio.h> @@ -75,6 +77,11 @@ struct __rte_aligned(2) rte_ether_addr {uint8_t addr_bytes[RTE_ETHER_ADDR_LEN ];/**< Addr bytes in tx order */
};lib/net/rte_ether.h +5
-0+static_assert (sizeof (struct rte_ether_addr ) ==6 , +"sizeof(struct rte_ether_addr) == 6" ); +static_assert (alignof(struct rte_ether_addr ) ==2 , +"alignof(struct rte_ether_addr) == 2" ); +#define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02/**< Locally assigned Eth. address. */
#defineRTE_ETHER_GROUP_ADDR0x01/**< Multicast or broadcast Eth.
address. */lib/net/rte_ether.h +6
-1@@ -290,12 +297,17 @@ rte_ether_unformat_addr(const char *str, structrte_ether_addr *eth_addr); * Ethernet header: Contains the destination address, source address * and frame type. */ -struct __rte_aligned(2) rte_ether_hdr { +struct rte_ether_hdr {struct rte_ether_addr dst_addr;/**< Destination address. */ struct rte_ether_addr src_addr;/**< Source address. */ rte_be16_t ether_type;/**< Frame type. */ }; +static_assert (sizeof (struct rte_ether_hdr ) ==14 , +"sizeof(struct rte_ether_hdr) == 14" ); +static_assert (alignof(struct rte_ether_hdr ) ==2 , +"alignof(struct rte_ether_hdr) == 2" ); +/** * Ethernet VLAN Header. * Contains the 16-bit VLAN Tag Control Identifier and the Ethernet
typelib/net/rte_ether.h +0
-0@@ -304,7 +316,12 @@ struct __rte_aligned(2) rte_ether_hdr {struct rte_vlan_hdr {rte_be16_t vlan_tci;/**< Priority (3) + CFI (1) + Identifier Code (12) */
rte_be16_teth_proto; /**< Ethernet type of encapsulated frame.
*/lib/net/rte_ether.h +6
-2-} __rte_packed; +}; + +static_assert (sizeof (struct rte_vlan_hdr ) ==4 , +"sizeof(struct rte_vlan_hdr) == 4" ); +static_assert (alignof(struct rte_vlan_hdr ) ==2 , +"alignof(struct rte_vlan_hdr) == 2" ); --2.43.0
Quoted 13 lines · Morten Brørup
Ethernet packets can be VLAN tagged, i.e. an Ethernet header can have a VLAN tag (a.k.a. VLAN header) embedded.
Since the Ethernet header is 2 byte aligned, and the VLAN tag is directly related to the Ethernet header, the VLAN tag is also 2 byte aligned, so packing the VLAN tag structure is not necessary.Furthermore, the Ethernet header type is implictly 2 byte aligned, so removed the superfluous explicit 2 byte alignment.
Added
static_assertsto verify the size and alignment of the various Ethernet types.Signed-off-by Morten Brørup <[email protected]>
Quoted 9 lines · Morten Brørup
Ethernet packets can be VLAN tagged, i.e. an Ethernet header can have a VLAN tag (a.k.a. VLAN header) embedded.
Since the Ethernet header is 2 byte aligned, and the VLAN tag is directly related to the Ethernet header, the VLAN tag is also 2 byte aligned, so packing the VLAN tag structure is not necessary.Furthermore, the Ethernet header type is implictly 2 byte aligned, so removed the superfluous explicit 2 byte alignment.
Quoted 8 lines
Added
static_assertsto verify the size and alignment of the various Ethernet types.Signed-off-by Morten Brørup <[email protected]>--- lib/net/rte_ether.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
13/10/2024 10:35, Morten Brørup:
lib/net/rte_ether.h struct __rte_aligned(2) rte_ether_addr { · line 83
Quoted 4 lines
lib/net/rte_ether.h +4
-0+static_assert (sizeof (struct rte_ether_addr ) ==6 , +"sizeof(struct rte_ether_addr) == 6" ); +static_assert (alignof(struct rte_ether_addr ) ==2 , +"alignof(struct rte_ether_addr) == 2" );
Instead of repeating the condition twice,
it would be simpler to use RTE_BUILD_BUG_ON
Quoted 11 lines
From: Thomas Monjalon [mailto:[email protected]]
Sent: Thursday, 17 October 2024 22.4413/10/2024 10:35, Morten Brørup:
lib/net/rte_ether.h struct __rte_aligned(2) rte_ether_addr { · line 83
Quoted 4 lines
lib/net/rte_ether.h +4
-0+static_assert (sizeof (struct rte_ether_addr ) ==6 , +"sizeof(struct rte_ether_addr) == 6" ); +static_assert (alignof(struct rte_ether_addr ) ==2 , +"alignof(struct rte_ether_addr) == 2" );Instead of repeating the condition twice,
it would be simpler to useRTE_BUILD_BUG_ON
RTE_BUILD_BUG_ON can only be used in code blocks, so it would need to be wrapped in some dummy function.
Thomas Monjalon
Applied13/10/2024 10:35, Morten Brørup:
Quoted 6 lines
Ethernet packets can be VLAN tagged, i.e. an Ethernet header can have a VLAN tag (a.k.a. VLAN header) embedded.
Since the Ethernet header is 2 byte aligned, and the VLAN tag is directly related to the Ethernet header, the VLAN tag is also 2 byte aligned, so packing the VLAN tag structure is not necessary.
Quoted 7 lines
Furthermore, the Ethernet header type is implictly 2 byte aligned, so removed the superfluous explicit 2 byte alignment.
Added
static_assertsto verify the size and alignment of the various Ethernet types.Signed-off-by Morten Brørup <[email protected]>
Applied, thanks.