A vhost target provides a local storage service as a process running on a local machine. It is capable of exposing virtualized block devices to QEMU instances or other arbitrary processes.
The following diagram presents how QEMU-based VM communicates with SPDK Vhost-SCSI device.
The diagram, and the vhost protocol itself is described in Virtualized I/O with Vhost-user doc.
SPDK provides an accelerated vhost target by applying the same user space and polling techniques as other components in SPDK. Since SPDK is polling for vhost submissions, it can signal the VM to skip notifications on submission. This avoids VMEXITs on I/O submission and can significantly reduce CPU usage in the VM on heavy I/O workloads.
This guide assumes the SPDK has been built according to the instructions in Getting Started. The SPDK vhost target is built with the default configure options.
Additional command line flags are available for Vhost target.
Param | Type | Default | Description |
---|---|---|---|
-S | string | $PWD | directory where UNIX domain sockets will be created |
The guest OS must contain virtio-scsi or virtio-blk drivers. Most Linux and FreeBSD distributions include virtio drivers. Windows virtio drivers must be installed separately. The SPDK vhost target has been tested with recent versions of Ubuntu, Fedora, and Windows
Userspace vhost-scsi target support was added to upstream QEMU in v2.10.0. Run the following command to confirm your QEMU supports userspace vhost-scsi.
Userspace vhost-blk target support was added to upstream QEMU in v2.12.0. Run the following command to confirm your QEMU supports userspace vhost-blk.
First, run the SPDK setup.sh script to setup some hugepages for the SPDK vhost target application. This will allocate 4096MiB (4GiB) of hugepages, enough for the SPDK vhost target and the virtual machine.
Next, start the SPDK vhost target application. The following command will start vhost on CPU cores 0 and 1 (cpumask 0x3) with all future socket files placed in /var/tmp. Vhost will fully occupy given CPU cores for I/O polling. Particular vhost devices can be restricted to run on a subset of these CPU cores. See Create a vhost device for details.
To list all available vhost options use the following command.
SPDK bdevs are block devices which will be exposed to the guest OS. For vhost-scsi, bdevs are exposed as SCSI LUNs on SCSI devices attached to the vhost-scsi controller in the guest OS. For vhost-blk, bdevs are exposed directly as block devices in the guest OS and are not associated at all with SCSI.
SPDK supports several different types of storage backends, including NVMe, Linux AIO, malloc ramdisk and Ceph RBD. Refer to Block Device User Guide for additional information on configuring SPDK storage backends.
This guide will use a malloc bdev (ramdisk) named Malloc0. The following RPC will create a 64MB malloc bdev with 512-byte block size.
The following RPC will create a vhost-scsi controller which can be accessed by QEMU via /var/tmp/vhost.0. At the time of creation the controller will be bound to a single CPU core with the smallest number of vhost controllers. The optional --cpumask
parameter can directly specify which cores should be taken into account - in this case always CPU 0. To achieve optimal performance on NUMA systems, the cpumask should specify cores on the same CPU socket as its associated VM.
The following RPC will attach the Malloc0 bdev to the vhost.0 vhost-scsi controller. Malloc0 will appear as a single LUN on a SCSI device with target ID 0. SPDK Vhost-SCSI device currently supports only one LUN per SCSI target. Additional LUNs can be added by specifying a different target ID.
To remove a bdev from a vhost-scsi controller use the following RPC:
The following RPC will create a vhost-blk device exposing Malloc0 bdev. The device will be accessible to QEMU via /var/tmp/vhost.1. All the I/O polling will be pinned to the least occupied CPU core within given cpumask - in this case always CPU 0. For NUMA systems, the cpumask should specify cores on the same CPU socket as its associated VM.
It is also possible to create a read-only vhost-blk device by specifying an extra -r
or --readonly
parameter.
Now the virtual machine can be started with QEMU. The following command-line parameters must be added to connect the virtual machine to its vhost controller.
First, specify the memory backend for the virtual machine. Since QEMU must share the virtual machine's memory with the SPDK vhost target, the memory must be specified in this format with share=on.
Second, ensure QEMU boots from the virtual machine image and not the SPDK malloc block device by specifying bootindex=0 for the boot image.
Finally, specify the SPDK vhost devices:
This example uses an NVMe bdev alongside Mallocs. SPDK vhost application is started on CPU cores 0 and 1, QEMU on cores 2 and 3.
Please note the following two commands are run on the guest VM.
We can see that sdb
and sdc
are SPDK vhost-scsi LUNs, and vda
is SPDK a vhost-blk disk.
For best performance use the Linux kernel block multi-queue feature with vhost. To enable it on Linux, it is required to modify kernel options inside the virtual machine.
Instructions below for Ubuntu OS:
vi /etc/default/grub
GRUB_CMDLINE_LINUX="scsi_mod.use_blk_mq=1"
sudo update-grub
To achieve better performance, make sure to increase number of cores assigned to the VM and add num_queues
parameter to the QEMU device
. It should be enough to set num_queues=4
to saturate physical device. Adding too many queues might lead to SPDK vhost performance degradation if many vhost devices are used because each device will require additional num_queues
to be polled.
Some Linux distributions report a kernel panic when starting the VM if the number of I/O queues specified via the num-queues
parameter is greater than number of vCPUs. If you need to use more I/O queues than vCPUs, check that your OS image supports that configuration.
Hotplug/hotremove within a vhost controller is called hot-attach/detach. This is to distinguish it from SPDK bdev hotplug/hotremove. E.g. if an NVMe bdev is attached to a vhost-scsi controller, physically hotremoving the NVMe will trigger vhost-scsi hot-detach. It is also possible to hot-detach a bdev manually via RPC - for example when the bdev is about to be attached to another controller. See the details below.
Please also note that hot-attach/detach is Vhost-SCSI-specific. There are no RPCs to hot-attach/detach the bdev from a Vhost-BLK device. If Vhost-BLK device exposes an NVMe bdev that is hotremoved, all the I/O traffic on that Vhost-BLK device will be aborted - possibly flooding a VM with syslog warnings and errors.
Hot-attach is done by simply attaching a bdev to a vhost controller with a QEMU VM already started. No other extra action is necessary.
Just like hot-attach, the hot-detach is done by simply removing bdev from a controller when QEMU VM is already started.
Removing an entire bdev will hot-detach it from a controller as well.
The Windows viostor
driver before version 0.1.130-1 is buggy and does not correctly support vhost-blk devices with non-512-byte block size. See the bug report for more information.
QEMU vhost-user-blk is supported from version 2.12.