Repository navigation
drivers/virtio: Accept VIRTIO_F_VERSION_1 in virtio-mmio - #22731
Open
DavidMCerdeira wants to merge 2 commits into
Open
DavidMCerdeira wants to merge 2 commits into
DavidMCerdeira wants to merge 2 commits into
Conversation
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>
DavidMCerdeira
requested review from
masayuki2009 and
pkarashchenko
as code owners
December 2, 2026 11:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: Please adhere to Contributing Guidelines.
Summary
VIRTIO_F_VERSION_1when 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 acceptVIRTIO_F_VERSION_1. The setup is the linux+nuttx-virtio demo on thefeat/linux-nuttx-virtiobranch of bao-demos. It is a follow-up to drivers/virtio: Do not clear the device status bits in virtio-mmio. #22072.drivers/virtio/virtio-mmio.canddrivers/virtio/virtio-net.c, in two commits that have to be applied in this order.VIRTIO_F_VERSION_1the virtio-net header always has thenum_buffersfield 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.virtio_mmio_negotiate_features()returns the features that were written to the device, so that the drivers see the bit.Impact
VIRTIO_F_VERSION_1, and virtio-net supports the header that comes with it.CONFIG_NET_LL_GUARDSIZEnow 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.VIRTIO_F_VERSION_1with 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.VIRTIO_F_VERSION_1now work. Devices that already worked keep working.CONFIG_NET_LL_GUARDSIZEwould 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.virtio_net_free_vq_buffers()now reads the netpkt fromvq->vq_descx[idx].cookie, sincevirtqueue_get_first_avail_buffer()does not return the cookie.lentovirtqueue_get_first_avail_buffer(), which writes to it. This PR does not change that.Assisted-bytrailers); 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:netnsh64andrv-virt:netnsh, ARM64qemu-armv8a:netnsh, all unmodified, with virtio-serial, virtio-rng, virtio-net and virtio-blk.Each target was run with
-global virtio-mmio.force-legacy=falseand with legacy virtio-mmio devices, andqemu-armv8a:netnshalso with the virtio-pci devices. The NSH session below gives the same results in all of them.rv-virt:netnsh64was 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 getVIRTIO_F_ANY_LAYOUTand 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:netnsh64with 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,playrawto a QEMU wav file). After the change all of them except virtio-snd acceptVIRTIO_F_VERSION_1on 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.
Testing logs before change:
Features accepted by the guest on
rv-virt:netnsh64with 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.With the second commit alone (and the
VIRTIO_F_VERSION_1define it needs), the device uses the 12 byte header and the driver the 10 byte one: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:
Testing logs after change:
Features accepted by the guest, for virtio-serial and virtio-net:
virtcon.logon the host containshello-from-nuttx.On the Bao demo, the device model log and the NuttX console (
hello-from-linuxwas written to the pseudo-terminal of the device model):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.