本文将记录vsock相关Notes。

What

VM Sockets(vsock) is a fast and efficient communication mechanism between guest virtual machines and their host. 说白了,就是允许guest与host利用socket进行通信(不依赖于虚拟机的网卡),借助于网卡,guest与host也是可以进行socket通信的,但此时就不是vsock了。使用vsock进行socket编程时,需要使用新的socket address family AF_VSOCK。

Why



How


There are several layers here.

  • application, use <cid,port> as a socket address
  • socket layer, support for socket API
  • AF_VSOCK address family, implement the vsock core
  • transport, trasnport the data between guest and host.

The transport layer is the mostly needed to talk as the other three just need to implement standand interfaces in kernel.

Transport as its name indicated, is used to transport the data between guest and host just like the networking card transport data between local and remote socket. There are two kinds of transports according to data’s flow direction.

  • G2H: guest->host transport, they run in the guest and the guest vsock networking protocol uses this to communication with the host.
  • H2G: host->guest transport, they run in the host and the host vsock networing protocol uses this to communiction with the guest.

Usually H2G transport is implemented as a device emulation, and G2H transport is implemented as the emulated device’s driver. For example, in VMware the H2G transport is a emulated vmci PCI device and the G2H is vmci device driver. In qemu the H2G transport is a emulated vhost-vsock device and the G2H transport is the vosck device’s driver.

Following picture shows the virtio(in guest) and vhost(in host) transport.

Example

可以参考 https://gist.github.com/nrdmn/7971be650919b112343b1cb2757a3fe6
在qemu-kvm + Linux guest环境中搭建vsock环境。


参考资料:

  1. virtio-vsock Zero-configuration host/guest communication
  2. https://gist.github.com/nrdmn/7971be650919b112343b1cb2757a3fe6
  3. Linux vsock internals
  4. VSOCK: VM ↔ host socket with minimal configuration
  5. man vsock
  6. vsock 介绍与使用
  7. Introduce VM Sockets virtio transport