How do you turn on/off Linux kernel Virtio features?

359 Views Asked by At

Virtio documentation, like here, describes many feature bits that can be set or "negotiated" for a virtio-device. For instance, there is the VIRTIO_RING_F_EVENT_IDX feature that can be turned on or off.

I have not come across any documentation that describes how to actually turn features on or off. Is there any way to turn features on or off at runtime (like through a /proc/ variable)? Are features something to be configured using make menuconfig when compiling a kernel? Or is the only way to modify the source code of a virtio device file and add/remove features there (like modifying this enum in vhost/vhost.h)?

2

There are 2 best solutions below

1
On BEST ANSWER

As mentioned in other comments and the other answer, there is no way to add/remove features for a virtio-device at runtime.

To change a virtio feature, you must modify the corresponding driver and device source code.

As an example, the enum in vhost/vhost.h you linked to is inside a driver file (the full path is actually drivers/vhost/vhost.h). So for turning off a feature, you could set features to false from this enum and reload the driver. For adding a feature, though, modifying the driver alone is not sufficient.

To add a feature for a virtio driver/device, you need to make sure that the feature bit is turned on in both the virtio driver (guest-level) and the device (host-level). You already know how to modify the driver code, so the only piece is how to modify the device level feature.

As explained here, device level features can be turned on/off by modifying the emulator code, typically Qemu. The VIRTIO_RING_F_EVENT_IDX feature you mentioned can be found in Qemu on this line, set to true by default (as of version 8.1.2).

2
On

The latest Virtio Committee specification, v1.2, describes the virtio feature bits here. Specifically:

Each virtio device offers all the features it understands. During device initialization, the driver reads this and tells the device the subset that it accepts. The only way to renegotiate is to reset the device.

This allows for forwards and backwards compatibility: if the device is enhanced with a new feature bit, older drivers will not write that feature bit back to the device. Similarly, if a driver is enhanced with a feature that the device doesn’t support, it see the new feature is not offered.

So the answer is: you need to decide at initialization time which feature(s) you want your device to support and prepare to do the feature negotiation properly per the Device Initialization section.

Suggestion: If you're going to invest seriously in virtio driver development, check out the virtio-comment mailing list. Best of luck :-@)