Skip to content

drivers/virtio: Accept VIRTIO_F_VERSION_1 in virtio-mmio - #22731

Open
DavidMCerdeira wants to merge 2 commits into
apache:masterfrom
bao-project:fix/virtio-version-1
Open

DavidMCerdeira wants to merge 2 commits into
apache:masterfrom
bao-project:fix/virtio-version-1

Conversation

@DavidMCerdeira

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

  • Why: the VirtIO specification requires the driver of a device that is not legacy to accept VIRTIO_F_VERSION_1 when it is offered (version 1.2, section 6.1: "A driver MUST accept VIRTIO_F_VERSION_1 if it is offered"). The virtio drivers only select device specific features, and drivers such as virtio-serial select none, so the bit was never accepted on virtio-mmio. QEMU tolerates this, but a device that checks the negotiated features rejects the driver. This was found running NuttX as a guest of the Bao hypervisor, where the VirtIO devices are implemented by another virtual machine with the Bao VirtIO device model, built on the rust-vmm components. It does not support legacy drivers and refuses to activate a device whose driver did not accept VIRTIO_F_VERSION_1. The setup is the linux+nuttx-virtio demo on the feat/linux-nuttx-virtio branch of bao-demos. It is a follow-up to drivers/virtio: Do not clear the device status bits in virtio-mmio. #22072.
  • What: drivers/virtio/virtio-mmio.c and drivers/virtio/virtio-net.c, in two commits that have to be applied in this order.
  • How, first commit: with VIRTIO_F_VERSION_1 the virtio-net header always has the num_buffers field and takes 12 bytes instead of 10 (VirtIO specification 1.2, section 5.1.6). The driver now chooses the header size from the negotiated features. The link layer guard was sized for a netpkt pointer followed by the 10 byte header, so the larger header does not fit in it: the netpkt is passed as the cookie of the buffer in the virtqueue instead of being stored in the guard. This commit changes nothing by itself, since no transport negotiates the feature yet.
  • How, second commit: the virtio-mmio transport accepts the bit for every device whose MMIO version is 2, and virtio_mmio_negotiate_features() returns the features that were written to the device, so that the drivers see the bit.

Impact

  • Is new feature added? Is existing feature changed? YES. New feature: support for modern (non-legacy) VirtIO device implementations on virtio-mmio. The transport accepts VIRTIO_F_VERSION_1, and virtio-net supports the header that comes with it.
  • Impact on user (will user need to adapt to change)? NO. CONFIG_NET_LL_GUARDSIZE now needs 26 bytes for virtio-net (30 with VLAN) instead of 32 (36) on 64-bit and 28 (32) on 32-bit targets, so the existing configurations keep building.
  • Impact on build (will build process change)? NO.
  • Impact on hardware (will arch(s) / board(s) / driver(s) change)? YES. The virtio drivers now negotiate VIRTIO_F_VERSION_1 with a version 2 virtio-mmio device, and virtio-net uses the 12 byte header with it. virtio-snd is the exception: it does not set any features, so it does not get the bit. Nothing changes for legacy (version 1) virtio-mmio devices and for virtio-pci.
  • Impact on documentation (is update required / provided)? NO.
  • Impact on security (any sort of implications)? NO.
  • Impact on compatibility (backward/forward/interoperability)? YES. Devices that reject a driver that does not accept VIRTIO_F_VERSION_1 now work. Devices that already worked keep working.
  • Anything else to consider or add? YES, an alternative that was considered, the state of the removal path and the use of AI assistance:
    • Alternative: keep the virtio-net driver mostly as it is, with the netpkt pointer in the link layer guard, and only make the header structure wider for the modern devices. That is a smaller change to the driver, but the guard then has to hold the pointer, the 12 byte header and the ETH header: 34 bytes on 64-bit and 30 on 32-bit targets (4 more with VLAN), where the configurations set 32 and 28 today. CONFIG_NET_LL_GUARDSIZE would have to be raised in the defconfig of every board that uses virtio-net (15 in the tree, 12 at 32 and 3 at 28) and in the out-of-tree ones, which fail to build otherwise: a breaking change. This PR passes the netpkt as the cookie of the buffer in the virtqueue instead, so that no configuration has to change. I can switch to the alternative if it is preferred.
    • Consequence of the choice made here: to reclaim the buffers the device did not return, virtio_net_free_vq_buffers() now reads the netpkt from vq->vq_descx[idx].cookie, since virtqueue_get_first_avail_buffer() does not return the cookie.
    • That removal path was not run: on master it already passes a NULL len to virtqueue_get_first_avail_buffer(), which writes to it. This PR does not change that.
    • The commits were developed with AI assistance (see the Assisted-by trailers); I have reviewed and tested every change.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Ubuntu 22.04.5, x86_64, riscv64-unknown-elf-gcc 13.2.0, riscv32-unknown-elf-gcc 13.2.0, aarch64-none-elf-gcc 14.2.1, QEMU 10.0.2.

  • Target(s): RISC-V rv-virt:netnsh64 and rv-virt:netnsh, ARM64 qemu-armv8a:netnsh, all unmodified, with virtio-serial, virtio-rng, virtio-net and virtio-blk.

  • Each target was run with -global virtio-mmio.force-legacy=false and with legacy virtio-mmio devices, and qemu-armv8a:netnsh also with the virtio-pci devices. The NSH session below gives the same results in all of them. rv-virt:netnsh64 was also run with the first commit alone.

  • The QEMU monitor shows the features the guest accepted (info virtio-status <path>). On modern virtio-mmio devices virtio-net does not get VIRTIO_F_ANY_LAYOUT and sends the header in a descriptor of its own, on legacy devices it gets it and sends the header and the frame in one, so both layouts are covered.

  • The other devices QEMU implements were run on rv-virt:netnsh64 with the drivers and test programs added to the configuration, on modern and legacy virtio-mmio, before and after the change, with the same results: virtio-gpu (fb), virtio-input (kbd, keys sent from the QEMU monitor), virtio-crypto (aescbc), virtio-9p (mount, read and write a file) and virtio-snd (nxplayer, playraw to a QEMU wav file). After the change all of them except virtio-snd accept VIRTIO_F_VERSION_1 on modern devices.

  • The Bao demo referenced in the summary was also run with these commits, on QEMU for RISC-V and ARM64: a virtio-serial console in both directions, virtio-net (ping from the backend virtual machine) and virtio-blk (a sector read and a sector written). On RISC-V it was also run with the first commit alone, where the device model rejects the driver (logs below).

  • Not tested: real hardware, the removal of the virtio-net driver (see above), and the drivers whose device QEMU does not implement itself (virtio-gpio, virtio-i2c, virtio-spi, virtio-rpmb, virtio-ping, virtio-rpmsg) or that need access to the host kernel (vsock). No regression test is added: QEMU accepts a driver with and without the feature, so a test on QEMU passes on master too.

    qemu-system-riscv64 -semihosting -M virt,aclint=on -cpu rv64 -smp 1 \
      -global virtio-mmio.force-legacy=false \
      -device virtio-serial-device,bus=virtio-mmio-bus.0 \
      -chardev file,id=foo,path=virtcon.log -device virtconsole,chardev=foo \
      -device virtio-rng-device,bus=virtio-mmio-bus.1 \
      -netdev user,id=u1 -device virtio-net-device,netdev=u1,bus=virtio-mmio-bus.2 \
      -drive file=disk.img,if=none,format=raw,id=hd \
      -device virtio-blk-device,bus=virtio-mmio-bus.3,drive=hd \
      -bios none -kernel nuttx -nographic
    

    Testing logs before change:

    Features accepted by the guest on rv-virt:netnsh64 with modern devices, for virtio-serial and virtio-net. The NSH session gives the same results as after the change, since QEMU accepts a driver without the feature.

    Guest features:
    
    Guest features:
    	VIRTIO_NET_F_MAC: Device has given MAC address
    

    With the second commit alone (and the VIRTIO_F_VERSION_1 define it needs), the device uses the 12 byte header and the driver the 10 byte one:

    nsh> ping -c 3 10.0.2.2
    PING 10.0.2.2 56 bytes of data
    ERROR: sendto failed at seqno 0: 113
    ERROR: sendto failed at seqno 1: 113
    ERROR: sendto failed at seqno 2: 113
    3 packets transmitted, 0 received, 100% packet loss, time 3034 ms
    

    On the Bao demo, with the first commit alone, the device model stops when the virtio-serial driver finishes the initialization (the value is the features the driver accepted) and NuttX waits for the device forever:

    thread 'vm_0_io' panicked at src/virtio/src/console/virtio/device.rs:135:59:
    called `Result::unwrap()` on an `Err` value: DeviceBadFeatures(0)
    
    Initializing the virtio console (waiting for the backend)...
    

    Testing logs after change:

    Features accepted by the guest, for virtio-serial and virtio-net:

    Guest features:
    	VIRTIO_F_VERSION_1: Device compliant for v1 spec (legacy)
    
    Guest features:
    	VIRTIO_F_VERSION_1: Device compliant for v1 spec (legacy)
    	VIRTIO_NET_F_MAC: Device has given MAC address
    
    nsh> uname -a
    NuttX  12.12.0 34a27affa Dec  2 2026 11:13:51 risc-v rv-virt
    nsh> ls /dev
    /dev:
     console
     null
     ram0
     random
     telnet
     ttyS0
     ttyV0
     virtblk0
     virtio
     zero
    nsh> hexdump /dev/random count=16
    /dev/random at 00000000:
    0000: e0 56 40 60 13 c1 5e c0 8a 88 8f ec 1f 19 8d c2 .V@`..^.........
    nsh> echo hello-from-nuttx > /dev/ttyV0
    nsh> mkfatfs /dev/virtblk0
    nsh> mount -t vfat /dev/virtblk0 /mnt
    nsh> echo virtio-blk-ok > /mnt/test.txt
    nsh> cat /mnt/test.txt
    virtio-blk-ok
    nsh> ping -c 3 10.0.2.2
    PING 10.0.2.2 56 bytes of data
    56 bytes from 10.0.2.2: icmp_seq=0 time=5.1 ms
    56 bytes from 10.0.2.2: icmp_seq=1 time=0.7 ms
    56 bytes from 10.0.2.2: icmp_seq=2 time=1.8 ms
    3 packets transmitted, 3 received, 0% packet loss, time 3031 ms
    rtt min/avg/max/mdev = 0.730/2.601/5.182/1.886 ms
    

    virtcon.log on the host contains hello-from-nuttx.

    On the Bao demo, the device model log and the NuttX console (hello-from-linux was written to the pseudo-terminal of the device model):

    virtio-console device id 0 at /dev/pts/0
    
    Bao NuttX VirtIO demo
    Initializing the virtio console (waiting for the backend)...
    virtio console initialized
    virtio network interface up
    message from the virtio block device: Hello from the disk image of the Linux backend
    Type here to send text to the virtio console
    message from the virtio console: hello-from-linux
    

    PR verification Self-Check

  • This PR introduces only one functional change.

  • I have updated all required description fields above.

  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).

  • My PR is still work in progress (not ready for review).

  • My PR is ready for review and can be safely merged into a codebase.

When VIRTIO_F_VERSION_1 is negotiated the virtio-net header always
has the num_buffers field and takes 12 bytes. The driver only knows
the 10 byte legacy header, which works as long as no transport
negotiates the feature: the frames are misaligned by 2 bytes otherwise.

Choose the header size from the negotiated features, and place the
header in the last bytes of the link layer guard, right before the
ETH header, so that both sizes can share a buffer with the frame.

The guard was sized for a netpkt pointer followed by the 10 byte
header, so the larger header does not fit in it. Pass the netpkt as
the cookie of the buffer in the virtqueue instead of storing a pointer
to it in the guard. CONFIG_NET_LL_GUARDSIZE then needs 26 bytes (30
with VLAN) instead of 32 (36) on 64-bit and 28 (32) on 32-bit targets,
and no configuration has to change.

Nothing changes for the transports yet, since none of them negotiates
VIRTIO_F_VERSION_1.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: David Cerdeira <davidmcerdeira@gmail.com>
The specification requires the driver of a device that is not legacy
to accept VIRTIO_F_VERSION_1 when it is offered. The virtio drivers
only select device specific features, and drivers such as
virtio-serial select none at all, so the bit was never accepted. A
device that checks the negotiated features rejects them. Accept the
bit in the transport for every device whose MMIO version is 2, the
one that is not legacy.

virtio_negotiate_features() stores the value the transport returns as
the features of the device, so return the features that were written
to the device, or the drivers would not see the bit.

QEMU tolerates the deviation, but other device implementations do not
have to.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: David Cerdeira <davidmcerdeira@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant