'What is linux vhost and how it helps virtio offloading?

I finished reading https://www.redhat.com/en/blog/introduction-virtio-networking-and-vhost-net and couldn't understand exactly.

I understand how virtio works. How the host kernel can read/write to a virtqueue and vice-versa for the guest kernel.

I'm reading the drivers/host/vhost.c and trying to understand what it does.

static int __init vhost_init(void)
{
    return 0;
}

static void __exit vhost_exit(void)
{
}

module_init(vhost_init);
module_exit(vhost_exit);

MODULE_VERSION("0.0.1");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Michael S. Tsirkin");
MODULE_DESCRIPTION("Host kernel accelerator for virtio");

it has no init function so I don't know how anything can interact with it. All I know is that vhost-net (on the guest) talks to vhost (on the kernel).

According to the website:

vhost protocol - A protocol that allows the virtio dataplane implementation to be offloaded to another element (user process or kernel module) in order to enhance performance.

How can this dataplane implementation be offloaded? I don't see any way to interact with vhost.c module as the init function has nothing.



Solution 1:[1]

It has a vhost_init function, but the function does not do anything, just return 0. That means the vhost kernel module does not need to do anything during initialization. But you can still interact with it by other means.

In the Linux kernel, one common way to interact with other kernel part or kernel module is to export symbols. In vhost.c, you will see lots of export symbols, e.g.

EXPORT_SYMBOL_GPL(vhost_vring_ioctl);

https://elixir.bootlin.com/linux/v5.15.3/source/drivers/vhost/vhost.c#L1718

By exporting symbols, other kernel code or kernel module can call this function. If you search vhost_vring_ioctl, you will see it is called from a few places in other files. That is how they interact with vhost.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jiang