# Vhost-user devices

Crosvm supports [vhost-user] devices for most virtio devices (block, net, etc ) so that device
emulation can be done outside of the main vmm process.

Here is a diagram showing how vhost-user block device back-end (implementing the actual disk in
userspace) and a vhost-user block front-end (implementing the device facing the guest OS) in crosvm
VMM work together.

<!-- Image from https://docs.google.com/presentation/d/1s6wH5L_F8NNiXls5UgWbD34jtBmijoZuiyLu76Fc2NM/edit#slide=id.ge5067b4ec2_0_55 -->

![vhost-user diagram](images/vhost_user.png)

## How to run

Let's take a block device as an example and see how to start vhost-user devices.

First, start vhost-user block backend with `crosvm devices` command, which waits for a vmm process
connecting to the socket.

```sh
# One-time commands to create a disk image.
fallocate -l 1G disk.img
mkfs.ext4 disk.img

VHOST_USER_SOCK=/tmp/vhost-user.socket

# Start vhost-user block backend listening on $VHOST_USER_SOCK
crosvm devices --block vhost=${VHOST_USER_SOCK},path=disk.img
```

Then, open another terminal and start a vmm process with `--vhost-user` flag (the frontend).

```sh
crosvm run \
  --vhost-user block,socket="${VHOST_USER_SOCK}" \
  <usual crosvm arguments>
  /path/to/bzImage
```

As a result, `disk.img` should be exposed as `/dev/vda` just like with `--block disk.img`.

[vhost-user]: https://qemu-project.gitlab.io/qemu/interop/vhost-user.html
