1# Vsock device 2 3crosvm supports [virtio-vsock] device for communication between the host and a guest VM. 4 5Assign a context id to a guest VM by passing it with the `--vsock` flag. 6 7```sh 8GUEST_CID=3 9 10crosvm run \ 11 --vsock "${GUEST_CID}" \ 12 <usual crosvm arguments> 13 /path/to/bzImage 14``` 15 16Then, the guest and the host can communicate with each other via vsock. Host always has 2 as its 17context id. 18 19crosvm assumes that the host has a vsock device at `/dev/vhost-vsock`. If you want to use a device 20at a different path or one given as an fd, you can use `--vhost-vsock-device` flag or 21`--vhost-vsock-fd` flag respectively. 22 23## Example usage 24 25This example assumes `ncat` is installed. If you are using a VM image created using `virt-builder`, 26it needs to come pre-installed with `ncat`. This can be achieved by running the following command: 27 28```sh 29 # Build a simple ubuntu image and create a user with no password. 30 virt-builder ubuntu-20.04 \ 31 --run-command "useradd -m -g sudo -p '' $USER ; chage -d 0 $USER" \ 32 -o ./rootfs \ 33 --install ncat 34``` 35 36At host shell: 37 38```sh 39PORT=11111 40 41# Listen at host 42ncat -l --vsock ${PORT} 43``` 44 45At guest shell: 46 47```sh 48HOST_CID=2 49PORT=11111 50 51# Make a connection to the host 52ncat --vsock ${HOST_CID} ${PORT} 53``` 54 55If a vsock device is configured properly in the guest VM, a connection between the host and the 56guest can be established and packets can be sent from both side. In the above example, your inputs 57to a shell on one's side should be shown at the shell on the other side if a connection is 58successfully established. 59 60[virtio-vsock]: https://docs.oasis-open.org/virtio/virtio/v1.1/csprd01/virtio-v1.1-csprd01.html#x1-389001r356 61