xref: /cloud-hypervisor/docs/vsock.md (revision dd37f33a433861150e2e13a8765c296543639f75)
11d3bb63eSAnatol Belski# VSOCK support
21d3bb63eSAnatol Belski
3*dd37f33aSBouke van der BijlVSOCK provides a way for guest and host to communicate through a socket. `cloud-hypervisor` only supports stream VSOCK sockets.
41d3bb63eSAnatol Belski
51d3bb63eSAnatol BelskiThe `virtio-vsock` is based on the [Firecracker](https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md) implementation, where additional details can be found.
61d3bb63eSAnatol Belski
71d3bb63eSAnatol Belski## What is a CID?
81d3bb63eSAnatol Belski
91d3bb63eSAnatol BelskiCID is a 32-bit context identifier describing the source or destination. In combination with the port, the complete addressing can be achieved to describe multiple listeners running on the same machine.
101d3bb63eSAnatol Belski
111d3bb63eSAnatol BelskiThe table below depicts the well known CID values:
121d3bb63eSAnatol Belski
131d3bb63eSAnatol Belski| CID | Description |
141d3bb63eSAnatol Belski|-----|-------------|
151d3bb63eSAnatol Belski| -1  | Random CID |
161d3bb63eSAnatol Belski|  0  | Hypervisor |
171d3bb63eSAnatol Belski|  1  | Loopback |
181d3bb63eSAnatol Belski|  2  | Host |
191d3bb63eSAnatol Belski
201d3bb63eSAnatol Belski## Prerequisites
211d3bb63eSAnatol Belski
221d3bb63eSAnatol Belski### Kernel Requirements
231d3bb63eSAnatol Belski
241d3bb63eSAnatol BelskiHost kernel: CONFIG_VHOST_VSOCK
251d3bb63eSAnatol Belski
261d3bb63eSAnatol BelskiGuest kernel: CONFIG_VIRTIO_VSOCKETS
271d3bb63eSAnatol Belski
281d3bb63eSAnatol Belski### Nested VM support
291d3bb63eSAnatol Belski
301d3bb63eSAnatol BelskiLinux __v5.5__ or newer is required for the L1 VM.
311d3bb63eSAnatol Belski
321d3bb63eSAnatol Belski### Loopback support
331d3bb63eSAnatol Belski
341d3bb63eSAnatol BelskiLinux __v5.6__ or newer is required.
351d3bb63eSAnatol Belski
361d3bb63eSAnatol Belski## Establishing VSOCK Connection
371d3bb63eSAnatol Belski
381d3bb63eSAnatol BelskiVSOCK device becomes available with `--vsock` option passed by the VM start. Cloud Hypervisor can be invoked for instance as below:
391d3bb63eSAnatol Belski
401d3bb63eSAnatol Belski```bash
411d3bb63eSAnatol Belskicloud-hypervisor \
421d3bb63eSAnatol Belski	--cpus boot=1 \
431d3bb63eSAnatol Belski	--memory size=4G \
441d3bb63eSAnatol Belski	--firmware CLOUDHV.fd \
451d3bb63eSAnatol Belski	--disk path=jammy-server-cloudimg.raw \
461d3bb63eSAnatol Belski	--vsock cid=3,socket=/tmp/ch.vsock
471d3bb63eSAnatol Belski```
481d3bb63eSAnatol Belski
491d3bb63eSAnatol BelskiThe examples use __socat__ `>=1.7.4` to illustrate the VSOCK functionality. However, there are other tools supporting VSOCK, like [ncat](https://stefano-garzarella.github.io/posts/2019-11-08-kvmforum-2019-vsock/).
501d3bb63eSAnatol Belski
511d3bb63eSAnatol Belski### Connecting from Host to Guest
521d3bb63eSAnatol Belski
5383b1a30fSYi WangThe guest starts to listen on the defined port:
541d3bb63eSAnatol Belski
551d3bb63eSAnatol Belski`$ socat - VSOCK-LISTEN:1234`
561d3bb63eSAnatol Belski
5783b1a30fSYi WangOnce the guest is listening, the host can send data:
581d3bb63eSAnatol Belski
591d3bb63eSAnatol Belski`echo -e "CONNECT 1234\\nHello from host!" | socat - UNIX-CONNECT:/tmp/ch.vsock
601d3bb63eSAnatol Belski
611d3bb63eSAnatol BelskiNote the string `CONNECT <port>` prepended to the actual data. It is possible for the guest to start listening on different ports, thus the specific command is needed to instruct VSOCK to which listener the host wants to connect. It needs to be sent once per connection. Once the connection established, data transfers can take place directly.
621d3bb63eSAnatol Belski
631d3bb63eSAnatol Belski### Connecting from Guest to Host
641d3bb63eSAnatol Belski
651d3bb63eSAnatol BelskiThis first requires a listening UNIX socket on the host side. The UNIX socket path has to be constructed by using the socket path used at the VM launch time with appended `_` and the port number to be used on the guest side. As in the example above, if we'd intended to connect from the guest to the port `1234`, the Unix socket path on the host side would be `/tmp/ch.vsock_1234`.
661d3bb63eSAnatol Belski
671d3bb63eSAnatol BelskiAlso note that the CID used on the guest side is the well known CID value `2`.
681d3bb63eSAnatol Belski
691d3bb63eSAnatol BelskiListening on the host side:
701d3bb63eSAnatol Belski
711d3bb63eSAnatol Belski`$ socat - UNIX-LISTEN:/tmp/ch.vsock_1234`
721d3bb63eSAnatol Belski
731d3bb63eSAnatol BelskiFrom the guest:
741d3bb63eSAnatol Belski
751d3bb63eSAnatol Belski`$ echo -e "Hello from guest!" | socat - VSOCK-CONNECT:2:1234`
761d3bb63eSAnatol Belski
771d3bb63eSAnatol Belski## Links
781d3bb63eSAnatol Belski
791d3bb63eSAnatol Belski- [virtio-vsock in QEMU, Firecracker and Linux: Status, Performance and Challenges](https://kvmforum2019.sched.com/event/TmwK)
801d3bb63eSAnatol Belski- [Leveraging virtio-vsock in the cloud and containers](https://archive.fosdem.org/2021/schedule/event/vai_virtio_vsock/)
811d3bb63eSAnatol Belski- [VSOCK man page](https://manpages.ubuntu.com/manpages/focal/man7/vsock.7.html)
821d3bb63eSAnatol Belski- [https://stefano-garzarella.github.io/posts/2020-02-20-vsock-nested-vms-loopback/](https://stefano-garzarella.github.io/posts/2020-02-20-vsock-nested-vms-loopback/)
831d3bb63eSAnatol Belski- [https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md](https://github.com/firecracker-microvm/firecracker/blob/main/docs/vsock.md)
841d3bb63eSAnatol Belski
85