qemu/hw/net
thomas 681e338ee6 virtio-net: Fix network stall at the host side waiting for kick
Patch 06b1297017 ("virtio-net: fix network stall under load")
added double-check to test whether the available buffer size
can satisfy the request or not, in case the guest has added
some buffers to the avail ring simultaneously after the first
check. It will be lucky if the available buffer size becomes
okay after the double-check, then the host can send the packet
to the guest. If the buffer size still can't satisfy the request,
even if the guest has added some buffers, viritio-net would
stall at the host side forever.

The patch enables notification and checks whether the guest has
added some buffers since last check of available buffers when
the available buffers are insufficient. If no buffer is added,
return false, else recheck the available buffers in the loop.
If the available buffers are sufficient, disable notification
and return true.

Changes:
1. Change the return type of virtqueue_get_avail_bytes() from void
   to int, it returns an opaque that represents the shadow_avail_idx
   of the virtqueue on success, else -1 on error.
2. Add a new API: virtio_queue_enable_notification_and_check(),
   it takes an opaque as input arg which is returned from
   virtqueue_get_avail_bytes(). It enables notification firstly,
   then checks whether the guest has added some buffers since
   last check of available buffers or not by virtio_queue_poll(),
   return ture if yes.

The patch also reverts patch "06b12970174".

The case below can reproduce the stall.

                                       Guest 0
                                     +--------+
                                     | iperf  |
                    ---------------> | server |
         Host       |                +--------+
       +--------+   |                    ...
       | iperf  |----
       | client |----                  Guest n
       +--------+   |                +--------+
                    |                | iperf  |
                    ---------------> | server |
                                     +--------+

Boot many guests from qemu with virtio network:
 qemu ... -netdev tap,id=net_x \
    -device virtio-net-pci-non-transitional,\
    iommu_platform=on,mac=xx:xx:xx:xx:xx:xx,netdev=net_x

Each guest acts as iperf server with commands below:
 iperf3 -s -D -i 10 -p 8001
 iperf3 -s -D -i 10 -p 8002

The host as iperf client:
 iperf3 -c guest_IP -p 8001 -i 30 -w 256k -P 20 -t 40000
 iperf3 -c guest_IP -p 8002 -i 30 -w 256k -P 20 -t 40000

After some time, the host loses connection to the guest,
the guest can send packet to the host, but can't receive
packet from the host.

It's more likely to happen if SWIOTLB is enabled in the guest,
allocating and freeing bounce buffer takes some CPU ticks,
copying from/to bounce buffer takes more CPU ticks, compared
with that there is no bounce buffer in the guest.
Once the rate of producing packets from the host approximates
the rate of receiveing packets in the guest, the guest would
loop in NAPI.

         receive packets    ---
               |             |
               v             |
           free buf      virtnet_poll
               |             |
               v             |
     add buf to avail ring  ---
               |
               |  need kick the host?
               |  NAPI continues
               v
         receive packets    ---
               |             |
               v             |
           free buf      virtnet_poll
               |             |
               v             |
     add buf to avail ring  ---
               |
               v
              ...           ...

On the other hand, the host fetches free buf from avail
ring, if the buf in the avail ring is not enough, the
host notifies the guest the event by writing the avail
idx read from avail ring to the event idx of used ring,
then the host goes to sleep, waiting for the kick signal
from the guest.

Once the guest finds the host is waiting for kick singal
(in virtqueue_kick_prepare_split()), it kicks the host.

The host may stall forever at the sequences below:

         Host                        Guest
     ------------                 -----------
 fetch buf, send packet           receive packet ---
         ...                          ...         |
 fetch buf, send packet             add buf       |
         ...                        add buf   virtnet_poll
    buf not enough      avail idx-> add buf       |
    read avail idx                  add buf       |
                                    add buf      ---
                                  receive packet ---
    write event idx                   ...         |
    wait for kick                   add buf   virtnet_poll
                                      ...         |
                                                 ---
                                 no more packet, exit NAPI

In the first loop of NAPI above, indicated in the range of
virtnet_poll above, the host is sending packets while the
guest is receiving packets and adding buffers.
 step 1: The buf is not enough, for example, a big packet
         needs 5 buf, but the available buf count is 3.
         The host read current avail idx.
 step 2: The guest adds some buf, then checks whether the
         host is waiting for kick signal, not at this time.
         The used ring is not empty, the guest continues
         the second loop of NAPI.
 step 3: The host writes the avail idx read from avail
         ring to used ring as event idx via
         virtio_queue_set_notification(q->rx_vq, 1).
 step 4: At the end of the second loop of NAPI, recheck
         whether kick is needed, as the event idx in the
         used ring written by the host is beyound the
         range of kick condition, the guest will not
         send kick signal to the host.

Fixes: 06b1297017 ("virtio-net: fix network stall under load")
Cc: qemu-stable@nongnu.org
Signed-off-by: Wencheng Yang <east.moutain.yang@gmail.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
(cherry picked from commit f937309fbd)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
(Mjt: context fixup in include/hw/virtio/virtio.h)
2024-08-06 17:18:25 +03:00
..
can net: Replace TAB indentations with spaces 2022-11-11 09:39:03 +01:00
fsl_etsec net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
rocker net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
allwinner_emac.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
allwinner-sun8i-emac.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
cadence_gem.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
dp8393x.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
e1000_regs.h net: Replace TAB indentations with spaces 2022-11-11 09:39:03 +01:00
e1000.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
e1000e_core.c e1000e: fix link state on resume 2024-03-13 23:09:00 +03:00
e1000e_core.h e1000e: fix link state on resume 2024-03-13 23:09:00 +03:00
e1000e.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
e1000x_common.c e1000e: Fix tx/rx counters 2023-05-23 23:16:42 +03:00
e1000x_common.h
eepro100.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
etraxfs_eth.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
ftgmac100.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
i82596.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
i82596.h
imx_fec.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
Kconfig
lan9118.c hw/net/lan9118: Replace magic '2048' value by MIL_TXFIFO_SIZE definition 2024-04-10 20:20:49 +03:00
lance.c
lasi_i82596.c
mcf_fec.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
meson.build meson: use have_vhost_* variables to pick sources 2022-05-07 07:46:58 +02:00
mipsnet.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
msf2-emac.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
mv88w8618_eth.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
ne2000-isa.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
ne2000-pci.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
ne2000.c net: Replace TAB indentations with spaces 2022-11-11 09:39:03 +01:00
ne2000.h
net_rx_pkt.c
net_rx_pkt.h
net_tx_pkt.c
net_tx_pkt.h
npcm7xx_emc.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
opencores_eth.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
pcnet-pci.c
pcnet.c Avoid unaligned fetch in ladr_match() 2024-03-13 23:09:00 +03:00
pcnet.h net: Replace TAB indentations with spaces 2022-11-11 09:39:03 +01:00
rtl8139.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
smc91c111.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
spapr_llan.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
stellaris_enet.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
sungem.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
sunhme.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
trace-events
trace.h
tulip.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
tulip.h
vhost_net-stub.c vhost-net: vhost-kernel: introduce vhost_net_virtqueue_restart() 2022-11-07 13:12:20 -05:00
vhost_net.c vhost: enable vrings in vhost_dev_start() for vhost-user devices 2022-12-01 02:30:04 -05:00
virtio-net.c virtio-net: Fix network stall at the host side waiting for kick 2024-08-06 17:18:25 +03:00
vmware_utils.h
vmxnet3_defs.h
vmxnet3.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
vmxnet3.h Replace config-time define HOST_WORDS_BIGENDIAN 2022-04-06 10:50:37 +02:00
vmxnet_debug.h
xen_nic.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
xgmac.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
xilinx_axienet.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00
xilinx_ethlite.c net: Provide MemReentrancyGuard * to qemu_new_nic() 2023-11-29 16:19:39 +03:00