qemu/hw
thomas f937309fbd 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>
2024-08-02 11:09:52 +08:00
..
9pfs
acpi hw/acpi: Update CPUs AML with cpu-(ctrl)dev change 2024-07-22 20:15:41 -04:00
adc aspeed/adc: Add AST2700 support 2024-07-21 07:46:38 +02:00
alpha
arm hw/arm/mps2-tz.c: fix RX/TX interrupts order 2024-08-01 10:15:02 +01:00
audio virtio-snd: check for invalid param shift operands 2024-07-21 14:43:02 -04:00
avr
block hw/block: Add Microchip's 25CSM04 to m25p80 2024-07-26 09:21:06 +10:00
char hw/char/bcm2835_aux: Fix assert when receive FIFO fills up 2024-07-29 13:34:18 +01:00
core virtio,pci,pc: features,fixes 2024-07-24 09:32:04 +10:00
cpu
cris
cxl Misc HW patch queue 2024-07-24 15:39:43 +10:00
display virtio-gpu-gl: declare dependency on ui-opengl 2024-07-22 12:47:28 +04:00
dma
fsi
gpio
hppa
hyperv
i2c hw/i2c/mpc_i2c: Fix mmio region size 2024-07-23 20:30:36 +02:00
i386 intel_iommu: Fix for IQA reg read dropped DW field 2024-08-01 04:32:00 -04:00
ide
input
intc pnv/xive2: Dump more END state with 'info pic' 2024-07-26 09:51:33 +10:00
ipack
ipmi
isa hw/isa/vt82c686: Turn "intr" irq into a named gpio 2024-07-16 20:04:08 +02:00
loongarch
m68k
mem hw/cxl/cxl-mailbox-utils: Add device DDR5 ECS control feature 2024-07-21 14:42:04 -04:00
microblaze
mips hw/mips/loongson3_virt: remove useless type cast 2024-07-23 20:30:36 +02:00
misc hw/misc/bcm2835_property: Reduce scope of variables in mbox push function 2024-07-29 16:55:59 +01:00
net virtio-net: Fix network stall at the host side waiting for kick 2024-08-02 11:09:52 +08:00
nubus hw/nubus/virtio-mmio: Fix missing ERRP_GUARD() in realize handler 2024-07-23 22:34:09 +02:00
nvme Revert "pcie_sriov: Ensure VF function number does not overflow" 2024-08-01 04:32:00 -04:00
nvram
openrisc
pci Revert "hw/pci: Rename has_power to enabled" 2024-08-01 04:32:00 -04:00
pci-bridge
pci-host acpi/gpex: Create PCI link devices outside PCI root bridge 2024-07-22 20:15:42 -04:00
pcmcia
ppc Revert "hw/ppc/spapr_pci: Do not create DT for disabled PCI device" 2024-08-01 04:32:00 -04:00
remote
riscv hw/riscv/virt-acpi-build.c: Update the HID of RISC-V UART 2024-07-22 20:15:42 -04:00
rtc docs: Correct Loongarch -> LoongArch 2024-07-23 20:30:36 +02:00
rx
s390x
scsi Misc HW patch queue 2024-07-24 15:39:43 +10:00
sd hw/sd/sdcard: Support boot area in emmc image 2024-07-16 20:30:15 +02:00
sensor
sh4
smbios smbios: make memory device size configurable per Machine 2024-07-22 20:15:41 -04:00
sparc
sparc64
ssi hw/ppc: SPI controller wiring to P10 chip 2024-07-26 09:21:06 +10:00
timer hpet: avoid timer storms on periodic timers 2024-07-22 19:19:44 +02:00
tpm
tricore
ufs hw/ufs: Fix mcq register range check logic 2024-07-14 17:11:21 +09:00
usb hw/usb/u2f-passthru: Get rid of qemu_open_old() 2024-07-17 14:04:15 +03:00
vfio vfio queue: 2024-07-24 12:58:46 +10:00
virtio virtio-net: Fix network stall at the host side waiting for kick 2024-08-02 11:09:52 +08:00
watchdog
xen
xenpv
xtensa
Kconfig
meson.build