Skip to contentSkip to conversation

hw/net: npcm7xx_emc: fix alignment to eth_hdr

PATCH In review Patrick Venture 2 participants 7 messages Feb 27, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net: npcm7xx_emc: fix alignment to eth_hdr

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/npcm7xx_emc.c +6 -1
index e06f652629..11ed4a9e6a 100644
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t *buf,
                                 size_t len, const char **fail_reason)
 {
-    eth_pkt_types_e pkt_type = get_eth_packet_type(PKT_GET_ETH_HDR(buf));
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    pkt_type = get_eth_packet_type(&eth_hdr);
 
     switch (pkt_type) {
     case ETH_PKT_BCAST:
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#1 lore raw
Quoted 13 lines · Patrick Venture

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/npcm7xx_emc.c +0 -0
index e06f652629..11ed4a9e6a 100644

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 432

Quoted 10 lines
hw/net/npcm7xx_emc.c +6 -1
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t *buf,
                                 size_t len, const char **fail_reason)
 {
-    eth_pkt_types_e pkt_type = get_eth_packet_type(PKT_GET_ETH_HDR(buf));
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    pkt_type = get_eth_packet_type(&eth_hdr);

Maybe better to mark struct eth_header as QEMU_PACKED? Compare commit f8b94b4c5201 ("net: mark struct ip_header as QEMU_PACKED"). The handling of these header structs in eth.h is in general pretty suspect IMHO. We do the same "get_eth_packet_type(PKT_GET_ETH_HDR(buf))" in other devices, so this isn't just this device's bug.

thanks
-- PMM

#2 lore raw
Quoted 27 lines · Peter Maydell
Quoted 13 lines · Patrick Venture

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/npcm7xx_emc.c +0 -0
index e06f652629..11ed4a9e6a 100644

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 424

Quoted 1 line
hw/net/npcm7xx_emc.c +0 -0
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t

*buf,

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc)

Quoted 3 lines
hw/net/npcm7xx_emc.c +0 -1
                                 size_t len, const char **fail_reason)
 {
-    eth_pkt_types_e pkt_type =

get_eth_packet_type(PKT_GET_ETH_HDR(buf));

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 432

Quoted 6 lines
hw/net/npcm7xx_emc.c +6 -0
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    pkt_type = get_eth_packet_type(&eth_hdr);
Quoted 9 lines

Maybe better to mark struct eth_header as QEMU_PACKED? Compare commit f8b94b4c5201 ("net: mark struct ip_header as QEMU_PACKED"). The handling of these header structs in eth.h is in general pretty suspect IMHO. We do the same "get_eth_packet_type(PKT_GET_ETH_HDR(buf))" in other devices, so this isn't just this device's bug.

thanks

Roger that. We saw this in the two NICs we happened to be testing that day, and yeah, I grepped and just figured that those other NICs were doing something with their buffer allocations that we didn't. I'll give QEMU_PACKED whirl.

Quoted 2 lines

-- PMM

#3 lore raw
Quoted 37 lines · Patrick Venture
Quoted 24 lines · Peter Maydell
Quoted 22 lines · Patrick Venture

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/npcm7xx_emc.c +5 -1
index e06f652629..11ed4a9e6a 100644
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t *buf,
                                 size_t len, const char **fail_reason)
 {
-    eth_pkt_types_e pkt_type = get_eth_packet_type(PKT_GET_ETH_HDR(buf));
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
Quoted 8 lines

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 432

Quoted 1 line
hw/net/npcm7xx_emc.c +1 -0
+    pkt_type = get_eth_packet_type(&eth_hdr);

Maybe better to mark struct eth_header as QEMU_PACKED? Compare commit f8b94b4c5201 ("net: mark struct ip_header as QEMU_PACKED"). The handling of these header structs in eth.h is in general pretty suspect IMHO. We do the same "get_eth_packet_type(PKT_GET_ETH_HDR(buf))" in other devices, so this isn't just this device's bug.

Quoted 1 line

Roger that. We saw this in the two NICs we happened to be testing that day, and yeah, I grepped and just figured that those other NICs were doing something with their buffer allocations that we didn't. I'll give QEMU_PACKED whirl.

You might find you need to make some fixes to other devices to get the QEMU_PACKED change to compile (do an all-targets build to test that). For instance for the ip_header change I had to first fix virtio-net.c in commit 5814c0846793715. The kind of thing that will need fixing is if there are places where code takes the address of the h_proto field and puts it into a uint16_t* : the compiler will complain about that. A quick grep suggests that the rocker_of_dpa.c code might be doing something like this, but hopefully that's it.

thanks
-- PMM

#4 lore raw
Quoted 47 lines · Peter Maydell
Quoted 4 lines · Patrick Venture

On Thu, Feb 27, 2025 at 7:52 AM Peter Maydell <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines

On Thu, 27 Feb 2025 at 15:40, Patrick Venture <[email protected]>

Quoted 8 lines · wrote:
Quoted 8 lines
Quoted 8 lines

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 6 lines
Quoted 6 lines

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 424

Quoted 6 lines
hw/net/npcm7xx_emc.c +0 -0
index e06f652629..11ed4a9e6a 100644
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t

*buf,

Quoted 3 lines
Quoted 3 lines

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc)

Quoted 3 lines
hw/net/npcm7xx_emc.c +0 -1
                                 size_t len, const char **fail_reason)
 {
-    eth_pkt_types_e pkt_type =

get_eth_packet_type(PKT_GET_ETH_HDR(buf));

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
hw/net/npcm7xx_emc.c +6 -0
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    pkt_type = get_eth_packet_type(&eth_hdr);
Quoted 8 lines
Quoted 6 lines

Maybe better to mark struct eth_header as QEMU_PACKED? Compare commit f8b94b4c5201 ("net: mark struct ip_header as QEMU_PACKED"). The handling of these header structs in eth.h is in general pretty suspect IMHO. We do the same "get_eth_packet_type(PKT_GET_ETH_HDR(buf))" in other devices, so this isn't just this device's bug.

Roger that. We saw this in the two NICs we happened to be testing that

day, and yeah, I grepped and just figured that those other NICs were doing something with their buffer allocations that we didn't. I'll give QEMU_PACKED whirl.

Quoted 11 lines

You might find you need to make some fixes to other devices to get the QEMU_PACKED change to compile (do an all-targets build to test that). For instance for the ip_header change I had to first fix virtio-net.c in commit 5814c0846793715. The kind of thing that will need fixing is if there are places where code takes the address of the h_proto field and puts it into a uint16_t* : the compiler will complain about that. A quick grep suggests that the rocker_of_dpa.c code might be doing something like this, but hopefully that's it.

Thanks for the head's up.

Quoted 4 lines

thanks
-- PMM

#5 lore raw
Quoted 53 lines · Patrick Venture
Quoted 48 lines · Peter Maydell
Quoted 4 lines · Patrick Venture

On Thu, Feb 27, 2025 at 7:52 AM Peter Maydell <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines

On Thu, 27 Feb 2025 at 15:40, Patrick Venture <[email protected]>

Quoted 8 lines · wrote:
Quoted 8 lines
Quoted 8 lines

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 6 lines
Quoted 6 lines

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 424

Quoted 6 lines
hw/net/npcm7xx_emc.c +0 -0
index e06f652629..11ed4a9e6a 100644
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t

*buf,

Quoted 1 line
Quoted 1 line

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 425

Quoted 1 line
hw/net/npcm7xx_emc.c +0 -0
                                 size_t len, const char

**fail_reason)

Quoted 2 lines
Quoted 2 lines

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 427

Quoted 2 lines
hw/net/npcm7xx_emc.c +0 -1
 {
-    eth_pkt_types_e pkt_type =

get_eth_packet_type(PKT_GET_ETH_HDR(buf));

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
hw/net/npcm7xx_emc.c +6 -0
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    pkt_type = get_eth_packet_type(&eth_hdr);
Quoted 8 lines
Quoted 6 lines

Maybe better to mark struct eth_header as QEMU_PACKED? Compare commit f8b94b4c5201 ("net: mark struct ip_header as QEMU_PACKED"). The handling of these header structs in eth.h is in general pretty suspect IMHO. We do the same "get_eth_packet_type(PKT_GET_ETH_HDR(buf))" in other devices, so this isn't just this device's bug.

Roger that. We saw this in the two NICs we happened to be testing that

day, and yeah, I grepped and just figured that those other NICs were doing something with their buffer allocations that we didn't. I'll give QEMU_PACKED whirl.

Quoted 12 lines
Quoted 11 lines

You might find you need to make some fixes to other devices to get the QEMU_PACKED change to compile (do an all-targets build to test that). For instance for the ip_header change I had to first fix virtio-net.c in commit 5814c0846793715. The kind of thing that will need fixing is if there are places where code takes the address of the h_proto field and puts it into a uint16_t* : the compiler will complain about that. A quick grep suggests that the rocker_of_dpa.c code might be doing something like this, but hopefully that's it.

Ok, so digging, and I see that vlanhdr is used similarly in the rocker_of_dpa.c code, so, without trying to bit off the yak shave of fixing all ethernet headers, but in reality ethernet packets are packed structures, should we just make them all packed and bite that bullet?

Quoted 8 lines

Thanks for the head's up.

Quoted 4 lines

thanks
-- PMM

#6 lore raw
Quoted 61 lines · Patrick Venture
Quoted 45 lines · Patrick Venture
Quoted 41 lines · Peter Maydell
Quoted 38 lines · Patrick Venture
Quoted 24 lines · Peter Maydell
Quoted 22 lines · Patrick Venture

'const struct eth_header', which requires 2 byte alignment

---
 hw/net/npcm7xx_emc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/net/npcm7xx_emc.c +5 -1
index e06f652629..11ed4a9e6a 100644
@@ -424,7 +424,12 @@ static bool emc_can_receive(NetClientState *nc)
 static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t *buf,
                                 size_t len, const char **fail_reason)
 {
-    eth_pkt_types_e pkt_type = get_eth_packet_type(PKT_GET_ETH_HDR(buf));
+    struct eth_header eth_hdr = {};
+    eth_pkt_types_e pkt_type;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
Quoted 8 lines

hw/net/npcm7xx_emc.c static bool emc_can_receive(NetClientState *nc) · line 432

Quoted 1 line
hw/net/npcm7xx_emc.c +1 -0
+    pkt_type = get_eth_packet_type(&eth_hdr);

Maybe better to mark struct eth_header as QEMU_PACKED? Compare commit f8b94b4c5201 ("net: mark struct ip_header as QEMU_PACKED"). The handling of these header structs in eth.h is in general pretty suspect IMHO. We do the same "get_eth_packet_type(PKT_GET_ETH_HDR(buf))" in other devices, so this isn't just this device's bug.

Roger that. We saw this in the two NICs we happened to be testing that day, and yeah, I grepped and just figured that those other NICs were doing something with their buffer allocations that we didn't. I'll give QEMU_PACKED whirl.

Quoted 10 lines
Quoted 10 lines

You might find you need to make some fixes to other devices to get the QEMU_PACKED change to compile (do an all-targets build to test that). For instance for the ip_header change I had to first fix virtio-net.c in commit 5814c0846793715. The kind of thing that will need fixing is if there are places where code takes the address of the h_proto field and puts it into a uint16_t* : the compiler will complain about that. A quick grep suggests that the rocker_of_dpa.c code might be doing something like this, but hopefully that's it.

Quoted 1 line

Ok, so digging, and I see that vlanhdr is used similarly in the rocker_of_dpa.c code, so, without trying to bit off the yak shave of fixing all ethernet headers, but in reality ethernet packets are packed structures, should we just make them all packed and bite that bullet?

If you want to do all of them that's probably the long term right thing. But the patchset structure would be a series of "fix X that assumes struct A is not packed", "fix Y that assumes struct A is not packed", "mark struct A packed", "fix Z that assumes struct B is not packed", "mark struct B packed", etc -- so I don't mind if you stop partway through without doing all of them. (After all, that's exactly what I did with only doing ip_header :-))

thanks
-- PMM