On Thu, 2026-10-15 at 11:37 +0800, Jay Chang wrote:
> In the Xilinx AXI Ethernet device model, U-Boot network commands
> (ping, dhcp, tftp) all fail due to a packet duplication bug.
> 
> The original implementation had a packet duplication issue where the
> same
> packet could exist in both the internal buffer and the incoming
> queue:
> 
> Problem Flow:
> 1. eth_rx() receives a packet from the network layer.
> 2. Packet is processed and stored in s->rxmem buffer.
> 3. axienet_eth_rx_notify() is called to push data to DMA.
> 4. If DMA cannot accept data immediately, the rxpos is 0 and wth_rx
> will return 0
> 5. Because eth_rx callback returns 0, the network layer queues this
> packet again in the incoming queue.
> 6. Now the same packet exists in TWO places:
>    - Already copied in s->rxmem buffer
>    - Queued in the incoming packet queue
> 7. This causes duplicate packets to be delivered to the guest
> 8. Network protocols fail due to receiving identical packets twice
> 
> Root Cause:
> eth_rx() did not check if the DMA stream could accept data before
> copying the packet into the internal buffer. This caused unnecessary
> buffering and potential double-processing.
> 
> Fix:
> 1. Check stream_can_push() at the beginning of eth_rx()
>    - If DMA cannot accept data, set need_flush and return 0
> immediately
>    - This prevents packet buffering when DMA is not ready
>    - The packet will be redelivered by QEMU's network layer
> 
> 2. Move need_flush check outside the while loop in
> axienet_eth_rx_notify()
>    - Ensures flush happens even when rxsize == 0
>    - Handles edge cases where the buffer is already empty but flush
> is needed
> 
> Signed-off-by: Jay Chang <[email protected]>
> Reviewed-by: Frank Chang <[email protected]>
> Reviewed-by: Jim Shu <[email protected]>

Acked-by: Alistair Francis <[email protected]>

Alistair

> ---
>  hw/net/xilinx_axienet.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/hw/net/xilinx_axienet.c b/hw/net/xilinx_axienet.c
> index d35f4a847d..cb9aa47707 100644
> --- a/hw/net/xilinx_axienet.c
> +++ b/hw/net/xilinx_axienet.c
> @@ -699,6 +699,12 @@ static void axienet_eth_rx_notify(void *opaque)
>              }
>          }
>      }
> +
> +    /* Check need_flush outside the loop for an empty RX buffer. */
> +    if (!s->rxsize && s->need_flush) {
> +        s->need_flush = false;
> +        qemu_flush_queued_packets(qemu_get_queue(s->nic));
> +    }
>      enet_update_irq(s);
>  }
>  
> @@ -722,6 +728,12 @@ static ssize_t eth_rx(NetClientState *nc, const
> uint8_t *buf, size_t size)
>          return 0;
>      }
>  
> +    /* Check if DMA can accept data before processing packet */
> +    if (!stream_can_push(s->tx_data_dev, axienet_eth_rx_notify, s))
> {
> +        s->need_flush = true;
> +        return 0;
> +    }
> +
>      unicast = ~buf[0] & 0x1;
>      broadcast = memcmp(buf, sa_bcast, 6) == 0;
>      multicast = !unicast && !broadcast;

Reply via email to