hw/net/bcm2838_genet: TX index wrap and full RX ring
Subsystems (3)
cover hw/net/bcm2838_genet: TX index wrap and full RX ring
Based-on: <[email protected]>
Two fixes for the BCM2838 GENET model of "[PATCH v6 00/17] hw/arm/raspi4b: working PCIe and GENET (real networking)", which is in target-arm.next. They apply to target-arm.next; if that is simpler, they can also be folded into patch 12/17 (implement GENET TX path) and patch 13/17 (implement GENET RX path) of that series.
- The TX consumer index counted past 16 bits, so the first transmit across the wrap never ended: the device sent the ring's descriptors forever with the BQL held, and the guest and the monitor hung. A guest sending a sustained stream hung after about 65k descriptors.
- The RX path wrote frames past the consumer index of a full ring and overwrote frames the guest had not read. The fix keeps them queued in the net layer until the guest frees descriptors, as e1000 does; real GENET drops them and counts them in DISCARD_CNT.
Patch 1 adds tests/qtest/bcm2838-genet-test.c. Each test fails with only its fix reverted (the TX test hangs).
I found these bugs while running Raspberry Pi OS images on a model of the Compute Module 4: https://github.com/LorandMarton/boardtwin. Its QEMU changes are on the boardtwin-develop branch of
https://github.com/LorandMarton/qemu. I intend to send the board model (raspicm4) after the 11.2 release.
Checked: each patch builds and passes the test; the aarch64 qtests pass (33, 3 skipped: the slow cdrom test and the two swtpm tests).
Lorand Marton (2):
hw/net/bcm2838_genet: wrap the TX consumer index at 16 bits hw/net/bcm2838_genet: hold RX frames while the ring is full
hw/net/bcm2838_genet.c | 42 ++++++- tests/qtest/bcm2838-genet-test.c | 204 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 3 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/bcm2838-genet-test.c
base-commit: eb5ac4b5bb4e221771cef0c53004f99499133f3e
Signature · 1 line
2.56.0
PATCH 1/2 hw/net/bcm2838_genet: wrap the TX consumer index at 16 bits
The producer and consumer indexes of a GENET DMA ring are 16-bit counters (Linux bcmgenet masks them with DMA_P_INDEX_MASK and DMA_C_INDEX_MASK). bcm2838_genet_tx() counted the consumer index up in a uint32_t and compared it with the 16-bit producer index, so the first transmit that crossed the wrap (consumer 0xfffe, producer 0x0001) never ended the loop: the device sent the ring's descriptors forever with the BQL held, and the guest and the monitor hung. A guest sending a sustained stream hung after about 65k descriptors on one ring.
Wrap the index, and test a transmit across the wrap.
--- hw/net/bcm2838_genet.c | 4 +- tests/qtest/bcm2838-genet-test.c | 101 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/bcm2838-genet-test.c
hw/net/bcm2838_genet.c +3 -1
@@ -521,7 +521,9 @@ static uint64_t bcm2838_genet_tx(BCM2838GenetState *s, unsigned int ring_index, } num_descs++; - cons_index_fld++; +/* The indexes count modulo 2^16 */ + cons_index_fld = (cons_index_fld +1 ) + &R_GENET_DMA_CONS_INDEX_INDEX_MASK ; s->regs.tdma.descs[desc_index].length_status =FIELD_DP32 (s->regs.tdma.descs[desc_index].length_status,GENET_RDMA_LENGTH_STATUS ,OWN ,1 );
tests/qtest/bcm2838-genet-test.c new file +101 -0
101 lines not on this page · raw message
tests/qtest/meson.build +2 -0
@@ -284,6 +284,8 @@ qtests_aarch64 = \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \ (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test', 'bcm2835-i2c-test', 'bcm2835-ic-test'] : []) + \ + (config_all_devices.has_key('CONFIG_RASPI') and host_os != 'windows' ? + ['bcm2838-genet-test'] : []) + \ (config_all_accel.has_key('CONFIG_TCG') and \ config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test', 'tpm-tis-i2c-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed64 : []) + \
Signature · 1 line
2.56.0
PATCH 2/2 hw/net/bcm2838_genet: hold RX frames while the ring is full
In reply to Lorand Martonbcm2838_genet_rdma() never looked at the consumer index of the RX
ring: it wrote each frame to the next descriptor whatever the guest had read, so a burst longer than the ring (Linux bcmgenet gives the default ring 256 descriptors) overwrote frames the guest had not taken yet, and the producer index ran more than a ring ahead of the consumer index, which bcmgenet_desc_rx() does not expect.
Count the free descriptors of the ring from its start and end addresses and the two indexes. When there are none, return 0 so that the net layer keeps the frame queued, and flush the queue when the guest writes the consumer index of an RX ring, as e1000 does on a write of RDT. If the ring fills inside a frame that takes several descriptors, stop there instead of writing past the consumer index.
Real GENET drops such frames and counts them in the DISCARD_CNT field of the producer index; holding them costs no frame on a slow host and matches the other QEMU NICs.
Test that a ring of 4 descriptors keeps 6 frames apart until the guest moves its consumer index.
--- hw/net/bcm2838_genet.c | 38 +++++++++++ tests/qtest/bcm2838-genet-test.c | 107 ++++++++++++++++++++++++++++++- 2 files changed, 143 insertions(+), 2 deletions(-)
hw/net/bcm2838_genet.c +38 -0
@@ -779,6 +779,13 @@ static void bcm2838_genet_write(void *opaque, hwaddr offset, uint64_t value, FIELD_EX32(value, GENET_DMA_CTRL, EN)) {qemu_flush_queued_packets (ncs); } +/* The guest gave descriptors back to an RX ring */ +if (offset >=BCM2838_GENET_RDMA_RINGS && + offset <BCM2838_GENET_RDMA_RINGS +sizeof (s->regs.rdma.rings) && + (offset -BCM2838_GENET_RDMA_RINGS ) %sizeof (BCM2838GenetRdmaRing) + ==BCM2838_GENET_RRING_CONS_INDEX ) { +qemu_flush_queued_packets (ncs); + }bcm2838_genet_set_irq_default (s);bcm2838_genet_set_irq_prio (s); }
@@ -823,6 +830,23 @@ static bool is_packet_multicast(const uint8_t *buf, size_t size)return !!(buf[0 ] &0x01 ); } +/* +* Descriptors of an RX ring that the guest has handed back: the ring +* holds END - START + 1 of them, and PROD - CONS (modulo 2^16) are full. +*/ +static unsigned bcm2838_genet_rdma_free_descs (BCM2838GenetRdmaRing *ring, +hwaddr start_index, +hwaddr end_index) +{ +uint16_t used =FIELD_EX32 (ring->prod_index,GENET_DMA_PROD_INDEX ,INDEX ) + -FIELD_EX32 (ring->cons_index,GENET_DMA_CONS_INDEX ,INDEX ); + +if (end_index < start_index || used > end_index - start_index) { +return 0 ; + } +return end_index - start_index +1 - used; +} +static ssize_t bcm2838_genet_rdma (BCM2838GenetState *s,uint32_t ring_idx,const void *buf,size_t size) {
@@ -855,6 +879,12 @@ static ssize_t bcm2838_genet_rdma(BCM2838GenetState *s, uint32_t ring_idx, BCM2838_GENET_DMA_DESC_CNT - 1);return -1 ; } + +/* A full ring: keep the frame queued until the guest frees some */ +if (!bcm2838_genet_rdma_free_descs (ring, start_index, end_index)) { +return 0 ; + } + desc = &rdma->descs[write_index]; desc->length_status = FIELD_DP32(desc->length_status,
@@ -948,6 +978,14 @@ static ssize_t bcm2838_genet_rdma(BCM2838GenetState *s, uint32_t ring_idx, desc = &rdma->descs[write_index]; ring->write_ptr = write_index * desc_word_size; ring->write_ptr_hi = ((hwaddr )write_index * desc_word_size) >>32 ; + +if (len < size && + !bcm2838_genet_rdma_free_descs (ring, start_index, end_index)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: RX ring %u full inside a frame\n" , + __func__, ring_idx); +break ; + } }if (ring_idx ==BCM2838_GENET_DMA_RING_DEFAULT ) {
tests/qtest/bcm2838-genet-test.c +105 -2
142 lines not on this page · raw message
Signature · 1 line
2.56.0
Quoted 9 lines · Lorand Marton
Based-on: <[email protected]>
Two fixes for the BCM2838 GENET model of "[PATCH v6 00/17] hw/arm/raspi4b: working PCIe and GENET (real networking)", which is in target-arm.next. They apply to target-arm.next; if that is simpler, they can also be folded into patch 12/17 (implement GENET TX path) and patch 13/17 (implement GENET RX path) of that series.
Quoted 8 lines
- The TX consumer index counted past 16 bits, so the first transmit across the wrap never ended: the device sent the ring's descriptors forever with the BQL held, and the guest and the monitor hung. A guest sending a sustained stream hung after about 65k descriptors.
- The RX path wrote frames past the consumer index of a full ring and overwrote frames the guest had not read. The fix keeps them queued in the net layer until the guest frees descriptors, as e1000 does; real GENET drops them and counts them inDISCARD_CNT.
I'm just about to send a pullreq with the GENET changes in it, to hit the softfreeze deadline. I think what I'll do with these bugfix patches is put them in for the next lot after that, so we have some time for code review, rather than trying to apply them in a hurry and maybe merge them with the existing tx and rx patches.
thanks
-- PMM
Re: [PATCH 1/2] hw/net/bcm2838_genet: wrap the TX consumer index at 16 bits
In reply to Lorand MartonHi Lorand,
Thanks for the fix. I reproduced the hang on our v6 series: with a stock Raspberry Pi OS Trixie guest, a 400 MB upload through GENET dies after roughly 47 MB (about 65k TX descriptors), and the guest stops responding. With this patch applied, the same 400 MB upload completes in about 13 seconds, and the guest stays healthy afterwards. The loop was inherited from the original code and our earlier, lighter transfer tests never reached the 16-bit wrap, so thank you for catching it.
The change itself looks correct to me: masking the consumer index with R_GENET_DMA_CONS_INDEX_INDEX_MASK keeps it consistent with the 16-bit producer index.
Marcelo
Quoted 167 lines · Lorand Marton
The producer and consumer indexes of a GENET DMA ring are 16-bit counters (Linux bcmgenet masks them with
DMA_P_INDEX_MASKandDMA_C_INDEX_MASK).bcm2838_genet_tx()counted the consumer index up in auint32_tand compared it with the 16-bit producer index, so the first transmit that crossed the wrap (consumer0xfffe, producer0x0001) never ended the loop: the device sent the ring's descriptors forever with the BQL held, and the guest and the monitor hung. A guest sending a sustained stream hung after about 65k descriptors on one ring.Wrap the index, and test a transmit across the wrap.
Signed-off-by Lorand Marton <[email protected]>--- hw/net/bcm2838_genet.c | 4 +- tests/qtest/bcm2838-genet-test.c | 101 +++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/bcm2838-genet-test.chw/net/bcm2838_genet.c +3
-1@@ -521,7 +521,9 @@ static uint64_t bcm2838_genet_tx(BCM2838GenetState *s, unsigned int ring_index, } num_descs++; - cons_index_fld++; +/* The indexes count modulo 2^16 */ + cons_index_fld = (cons_index_fld +1 ) + &R_GENET_DMA_CONS_INDEX_INDEX_MASK ; s->regs.tdma.descs[desc_index].length_status = FIELD_DP32(s->regs.tdma.descs[desc_index].length_status,GENET_RDMA_LENGTH_STATUS, OWN, 1);tests/qtest/bcm2838-genet-test.c new file +73
-0@@ -0,0 +1,101 @@ +/* +* QTest testcase for the BCM2838 GENET DMA rings +* +* Frames go out over a socket netdev. The producer and consumer indexes +* of a ring count modulo 2^16. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/bitops.h" +#include "qemu/bswap.h" +#include "libqtest.h" + +#define GENET_BASE 0xfd580000 + +#define RING_START_ADDR 0x14 +#define RING_END_ADDR 0x1c + +#define TDMA_DESC (i) (0x4000 + (i) * 12) +#define TDMA_RING (r) (0x4c00 + (r) * 0x40) +#define TRING_READ_PTR 0x00 +#define TRING_CONS_INDEX 0x08 +#define TRING_PROD_INDEX 0x0c +#define TDMA_RING_CFG 0x5040 +#define TDMA_CTRL 0x5044 +#define TSB_LEN 64/* transmit status block */ + +#define DESCS_PER_RING 4 +#define BUF_SIZE 2048 +#define BUF_BASE 0x100000 +#define FRAME_LEN 60 + +static void genet_writel (QTestState *qts,uint32_t off,uint32_t val) +{ +qtest_writel (qts,GENET_BASE + off, val); +} + +static uint32_t genet_readl (QTestState *qts,uint32_t off) +{ +return qtest_readl (qts,GENET_BASE + off); +} + +/* Transmit across the wrap of the 16-bit consumer index */ +static void test_tx_index_wrap (void ) +{ +const unsigned r =0 , n =3 ; +const uint16_t first =0xfffe ; +uint8_t frame[FRAME_LEN ] = {0 }; +uint8_t pkt[4 +FRAME_LEN ]; +int sv[2 ]; +QTestState *qts; + + g_assert_cmpint(socketpair(PF_UNIX, SOCK_STREAM, 0, sv), ==, 0); + qts =qtest_initf ("-M raspi4b -nic socket,fd=%d,model=bcm2838-genet" , + sv[1 ]); +close (sv[1 ]); + +memset (frame,0xff ,6 ); +stw_be_p (frame +12 ,0x88b5 ); +for (unsigned i =0 ; i < n; i++) { + frame[14 ] = i; +qtest_memset (qts,BUF_BASE + i *BUF_SIZE ,0 ,TSB_LEN ); +qtest_memwrite (qts,BUF_BASE + i *BUF_SIZE +TSB_LEN , frame, +sizeof (frame)); +/* SOP, EOP, buffer length */ +genet_writel (qts,TDMA_DESC (i),BIT (13 ) |BIT (14 ) | + (TSB_LEN +FRAME_LEN ) <<16 ); +genet_writel (qts,TDMA_DESC (i) +4 ,BUF_BASE + i *BUF_SIZE ); +genet_writel (qts,TDMA_DESC (i) +8 ,0 ); + } +genet_writel (qts,TDMA_RING (r) +RING_START_ADDR ,0 ); + genet_writel(qts, TDMA_RING(r) + RING_END_ADDR, DESCS_PER_RING * 3 -1);
tests/qtest/bcm2838-genet-test.c new file +28
-0+genet_writel (qts,TDMA_RING (r) +TRING_READ_PTR ,0 ); +genet_writel (qts,TDMA_RING (r) +TRING_CONS_INDEX , first); +genet_writel (qts,TDMA_RING (r) +TRING_PROD_INDEX , first); +genet_writel (qts,TDMA_RING_CFG ,BIT (r)); +genet_writel (qts,TDMA_CTRL ,1 |BIT (1 + r)); + +genet_writel (qts,TDMA_RING (r) +TRING_PROD_INDEX , + (uint16_t )(first + n)); +g_assert_cmphex (genet_readl (qts,TDMA_RING (r) +TRING_CONS_INDEX ) & + 0xffff, ==, (uint16_t)(first + n)); + +for (unsigned i =0 ; i < n; i++) { + g_assert_cmpint(recv(sv[0], pkt, sizeof(pkt), MSG_WAITALL), ==, +sizeof (pkt)); + g_assert_cmpuint(ldl_be_p(pkt), ==, FRAME_LEN); + g_assert_cmpuint(pkt[4 + 14], ==, i); + } + +qtest_quit (qts); +close (sv[0 ]); +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); +qtest_add_func ("/bcm2838-genet/tx-index-wrap" , test_tx_index_wrap); +return g_test_run (); +}tests/qtest/meson.build +2
-0@@ -284,6 +284,8 @@ qtests_aarch64 = \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \ (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test', 'bcm2835-i2c-test', 'bcm2835-ic-test'] : []) + \ + (config_all_devices.has_key('CONFIG_RASPI') and host_os != 'windows' ? + ['bcm2838-genet-test'] : []) + \(config_all_accel.has_key('CONFIG_TCG') and \ config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ?['tpm-tis-i2c-test', 'tpm-tis-i2c-swtpm-test'] : []) + \
(config_all_devices.has_key('CONFIG_ASPEED_SOC')?qtests_aspeed64:
[]) + \
--
2.56.0
Re: [PATCH 2/2] hw/net/bcm2838_genet: hold RX frames while the ring is full
In reply to Lorand MartonHi Lorand,
I tested this patch together with 1/2 on our v6 series, running a stock Raspberry Pi OS Trixie guest. A 300 MB download through GENET completed at about 28 MB/s with no timeouts or checksum errors in the guest log, and the new tests/qtest/bcm2838-genet-test.c passes.
Marcelo
Quoted 267 lines · Lorand Marton
bcm2838_genet_rdma()never looked at the consumer index of the RX
ring: it wrote each frame to the next descriptor whatever the guest had read, so a burst longer than the ring (Linux bcmgenet gives the default ring 256 descriptors) overwrote frames the guest had not taken yet, and the producer index ran more than a ring ahead of the consumer index, whichbcmgenet_desc_rx()does not expect.Count the free descriptors of the ring from its start and end addresses and the two indexes. When there are none, return 0 so that the net layer keeps the frame queued, and flush the queue when the guest writes the consumer index of an RX ring, as e1000 does on a write of RDT. If the ring fills inside a frame that takes several descriptors, stop there instead of writing past the consumer index.
Real GENET drops such frames and counts them in the
DISCARD_CNTfield of the producer index; holding them costs no frame on a slow host and matches the other QEMU NICs.Test that a ring of 4 descriptors keeps 6 frames apart until the guest moves its consumer index.
Signed-off-by Lorand Marton <[email protected]>--- hw/net/bcm2838_genet.c | 38 +++++++++++ tests/qtest/bcm2838-genet-test.c | 107 ++++++++++++++++++++++++++++++- 2 files changed, 143 insertions(+), 2 deletions(-)hw/net/bcm2838_genet.c +7
-0@@ -779,6 +779,13 @@ static void bcm2838_genet_write(void *opaque, hwaddr offset, uint64_t value, FIELD_EX32(value, GENET_DMA_CTRL, EN)) {qemu_flush_queued_packets (ncs); } +/* The guest gave descriptors back to an RX ring */ +if (offset >=BCM2838_GENET_RDMA_RINGS && + offset <BCM2838_GENET_RDMA_RINGS +sizeof (s->regs.rdma.rings) && + (offset -BCM2838_GENET_RDMA_RINGS ) %sizeof (BCM2838GenetRdmaRing) + ==BCM2838_GENET_RRING_CONS_INDEX ) { +qemu_flush_queued_packets (ncs); + }bcm2838_genet_set_irq_default (s);bcm2838_genet_set_irq_prio (s);}
hw/net/bcm2838_genet.c +17
-0@@ -823,6 +830,23 @@ static bool is_packet_multicast(const uint8_t *buf, size_t size)return !!(buf[0 ] &0x01 ); } +/* +* Descriptors of an RX ring that the guest has handed back: the ring +* holds END - START + 1 of them, and PROD - CONS (modulo 2^16) are full. +*/ +static unsigned bcm2838_genet_rdma_free_descs (BCM2838GenetRdmaRing *ring, +hwaddr start_index, +hwaddr end_index) +{ +uint16_t used =FIELD_EX32 (ring->prod_index,GENET_DMA_PROD_INDEX ,INDEX ) + -FIELD_EX32 (ring->cons_index,GENET_DMA_CONS_INDEX ,INDEX ); + +if (end_index < start_index || used > end_index - start_index) { +return 0 ; + } +return end_index - start_index +1 - used; +} +static
ssize_tbcm2838_genet_rdma(BCM2838GenetState *s,uint32_tring_idx,
const void *buf,size_tsize)
{hw/net/bcm2838_genet.c +6
-0@@ -855,6 +879,12 @@ static ssize_t bcm2838_genet_rdma(BCM2838GenetState *s, uint32_t ring_idx, BCM2838_GENET_DMA_DESC_CNT - 1);return -1 ; } + +/* A full ring: keep the frame queued until the guest frees some */ +if (!bcm2838_genet_rdma_free_descs (ring, start_index, end_index)) { +return 0 ; + } + desc = &rdma->descs[write_index];desc->length_status = FIELD_DP32(desc->length_status,hw/net/bcm2838_genet.c +8
-0@@ -948,6 +978,14 @@ static ssize_t bcm2838_genet_rdma(BCM2838GenetState *s, uint32_t ring_idx, desc = &rdma->descs[write_index]; ring->write_ptr = write_index * desc_word_size; ring->write_ptr_hi = ((hwaddr )write_index * desc_word_size) >>32 ; + +if (len < size && + !bcm2838_genet_rdma_free_descs (ring, start_index, end_index)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: RX ring %u full inside a frame\n" , + __func__, ring_idx); +break ; + } }if (ring_idx == BCM2838_GENET_DMA_RING_DEFAULT) {tests/qtest/bcm2838-genet-test.c +12
-2@@ -1,8 +1,8 @@/* * QTest testcase for the BCM2838 GENET DMA rings * -* Frames go out over a socket netdev. The producer and consumer indexes -* of a ring count modulo 2^16. +* Frames come in and go out over a socket netdev. The producer and +* consumer indexes of a ring count modulo 2^16. * * SPDX-License-Identifier: GPL-2.0-or-later */ @@ -14,8 +14,16 @@#define GENET_BASE 0xfd580000 +#define RDMA_DESC (i) (0x2000 + (i) * 12) +#define RDMA_RING (r) (0x2c00 + (r) * 0x40) +#define RING_WRITE_PTR 0x00 +#define RING_PROD_INDEX 0x08 +#define RING_CONS_INDEX 0x0c +#define RING_BUF_SIZE 0x10#define RING_START_ADDR 0x14#define RING_END_ADDR 0x1c +#define RDMA_RING_CFG 0x3040 +#define RDMA_CTRL 0x3044#define TDMA_DESC (i) (0x4000 + (i) * 12)#define TDMA_RING (r) (0x4c00 + (r) * 0x40)@@ -26,9 +34,11 @@#define TDMA_CTRL 0x5044#define TSB_LEN 64/* transmit status block */ +#define DEFAULT_RING 16#define DESCS_PER_RING 4#define BUF_SIZE 2048#define BUF_BASE 0x100000 +#define STATUS_LEN 64/* receive status block, then 2 bytes */ #define FRAME_LEN 60static void
genet_writel(QTestState *qts, uint32_t off, uint32_t val)tests/qtest/bcm2838-genet-test.c +65
-0@@ -41,6 +51,98 @@ static uint32_t genet_readl(QTestState *qts, uint32_t off)return qtest_readl (qts,GENET_BASE + off); } +/* The default ring uses the first DESCS_PER_RING descriptors */ +static QTestState *genet_rx_init (int *fd) +{ +const unsigned r =DEFAULT_RING ; +int sv[2 ]; +QTestState *qts; + + g_assert_cmpint(socketpair(PF_UNIX, SOCK_STREAM, 0, sv), ==, 0); + qts =qtest_initf ("-M raspi4b -nic socket,fd=%d,model=bcm2838-genet" , + sv[1 ]); +close (sv[1 ]); + *fd = sv[0 ]; + +for (unsigned i =0 ; i <DESCS_PER_RING ; i++) { +genet_writel (qts,RDMA_DESC (i) +4 ,BUF_BASE + i *BUF_SIZE ); +genet_writel (qts,RDMA_DESC (i) +8 ,0 ); + } +genet_writel (qts,RDMA_RING (r) +RING_START_ADDR ,0 ); +genet_writel (qts,RDMA_RING (r) +RING_END_ADDR , (DESCS_PER_RING -1 ) *3 ); +genet_writel (qts,RDMA_RING (r) +RING_WRITE_PTR ,0 ); +genet_writel (qts,RDMA_RING (r) +RING_BUF_SIZE ,BUF_SIZE ); +genet_writel (qts,RDMA_RING_CFG ,BIT (r)); +genet_writel (qts,RDMA_CTRL ,1 |BIT (1 + r)); +return qts; +} + +static uint32_t prod_index (QTestState *qts,unsigned r) +{ +return genet_readl (qts,RDMA_RING (r) +RING_PROD_INDEX ) &0xffff ; +} + +/* Send a broadcast frame into the socket netdev */ +static void send_frame (int fd,uint16_t ethertype,uint8_t byte14) +{ +uint8_t pkt[4 +FRAME_LEN ] = {0 }; + +stl_be_p (pkt,FRAME_LEN ); +memset (pkt +4 ,0xff ,6 ); +stw_be_p (pkt +4 +12 , ethertype); + pkt[4 +14 ] = byte14; + g_assert_cmpint(send(fd, pkt, sizeof(pkt), 0), ==, sizeof(pkt)); +} + +/* The byte at 14 of the frame that descriptor I holds */ +static uint8_t rx_byte14 (QTestState *qts,unsigned i) +{ +return qtest_readb (qts,BUF_BASE + i *BUF_SIZE +STATUS_LEN +2 +14 ); +} + +/* Wait up to MS milliseconds for the producer index of ring R to be N */ +static bool wait_prod_index (QTestState *qts,unsigned r,uint32_t n, +unsigned ms) +{ +gint64 end =g_get_monotonic_time () + ms *G_TIME_SPAN_MILLISECOND ; + +do { +qtest_clock_step (qts,100 ); +if (prod_index (qts, r) == n) { +return true; + } + }while (g_get_monotonic_time () < end); +return false; +} + +/* A full RX ring keeps the next frames until the guest frees descriptors */
tests/qtest/bcm2838-genet-test.c +12
-0+static void test_rx_ring_full(void) +{ + const unsigned n = DESCS_PER_RING + 2; + int fd; + QTestState *qts = genet_rx_init(&fd); + + for (unsigned i = 0; i < n; i++) { + send_frame(fd, 0x0800, i); + } + g_assert(wait_prod_index(qts, DEFAULT_RING, DESCS_PER_RING, 5000)); + /* the rest must not overwrite descriptors the guest has not read */ + g_assert(!wait_prod_index(qts, DEFAULT_RING, DESCS_PER_RING + 1,500));
tests/qtest/bcm2838-genet-test.c +16
-1+for (unsigned i =0 ; i <DESCS_PER_RING ; i++) { + g_assert_cmpuint(rx_byte14(qts, i), ==, i); + } + +genet_writel (qts,RDMA_RING (DEFAULT_RING ) +RING_CONS_INDEX ,2 ); +g_assert (wait_prod_index (qts,DEFAULT_RING , n,5000 )); +for (unsigned i =0 ; i < n; i++) { + g_assert_cmpuint(rx_byte14(qts, i % DESCS_PER_RING), ==, + i <2 ? i +DESCS_PER_RING : i); + } + +qtest_quit (qts); +close (fd); +} +/* Transmit across the wrap of the 16-bit consumer index */ static void test_tx_index_wrap (void ) {@@ -96,6 +198,7 @@ static void test_tx_index_wrap(void)int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL ); +qtest_add_func ("/bcm2838-genet/rx-ring-full" , test_rx_ring_full);qtest_add_func ("/bcm2838-genet/tx-index-wrap" , test_tx_index_wrap);return g_test_run (); } --2.56.0
Peter Maydell
AppliedQuoted 27 lines · Lorand Marton
Based-on: <[email protected]>
Two fixes for the BCM2838 GENET model of "[PATCH v6 00/17] hw/arm/raspi4b: working PCIe and GENET (real networking)", which is in target-arm.next. They apply to target-arm.next; if that is simpler, they can also be folded into patch 12/17 (implement GENET TX path) and patch 13/17 (implement GENET RX path) of that series.
- The TX consumer index counted past 16 bits, so the first transmit across the wrap never ended: the device sent the ring's descriptors forever with the BQL held, and the guest and the monitor hung. A guest sending a sustained stream hung after about 65k descriptors.
- The RX path wrote frames past the consumer index of a full ring and overwrote frames the guest had not read. The fix keeps them queued in the net layer until the guest frees descriptors, as e1000 does; real GENET drops them and counts them inDISCARD_CNT.Patch 1 adds
tests/qtest/bcm2838-genet-test.c. Each test fails with only its fix reverted (the TX test hangs).I found these bugs while running Raspberry Pi OS images on a model of the Compute Module 4: https://github.com/LorandMarton/boardtwin. Its QEMU changes are on the boardtwin-develop branch of
https://github.com/LorandMarton/qemu. I intend to send the board model (raspicm4) after the 11.2 release.
Quoted 6 lines
Checked: each patch builds and passes the test; the aarch64 qtests pass (33, 3 skipped: the slow cdrom test and the two swtpm tests).
Lorand Marton (2):
hw/net/bcm2838_genet: wrap the TX consumer index at 16 bits hw/net/bcm2838_genet: hold RX frames while the ring is full
Applied to target-arm.next, thanks.
-- PMM