vsock(7)

๐Ÿ’– vsock(7) โ€” the Linux VSOCK address family, bestie edition ๐Ÿ’–

okay so welcome to the vsock(7) page, part of the Miscellaneous Information Manual, and bestie you are about to learn how virtual machines and their host gossip with each other ๐Ÿ’…โœจ


๐ŸŒธ NAME ๐ŸŒธ

vsock โ€” Linux VSOCK address family


โœจ SYNOPSIS โœจ

#include <sys/socket.h>
#include <linux/vm_sockets.h>

stream_socket = socket(AF_VSOCK, SOCK_STREAM, 0);
datagram_socket = socket(AF_VSOCK, SOCK_DGRAM, 0);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

The VSOCK address family is literally the group chat between virtual machines and the host they're running on. It exists so guest agents and hypervisor services can have a communications channel that does NOT care about virtual machine network configuration. No network setup drama, no IP addresses, none of that. Iconic behavior honestly.

Valid socket types are SOCK_STREAM and SOCK_DGRAM.

  • SOCK_STREAM gives you connection-oriented byte streams with guaranteed, in-order delivery. Main character energy: every byte arrives, in the exact order you sent it. ๐Ÿ‘‘
  • SOCK_DGRAM gives you a connectionless datagram packet service with best-effort delivery and best-effort ordering. Chill, casual, no promises. ๐Ÿ™ˆ
Heads up bestie: whether these socket types are even available depends on the underlying hypervisor. Not every hypervisor shows up to the party. ๐Ÿ’€

A new socket is created with

socket(AF_VSOCK, socket_type, 0);

When a process wants to establish a connection, it calls connect(2) with a given destination socket address. And here's the cute part: the socket is automatically bound to a free port if unbound. It handles that for you, no cap.

A process can listen for incoming connections by first binding to a socket address using bind(2) and then calling listen(2). Bind first, then listen, in that order, don't skip steps. โœจ

Data is transmitted using the send(2) or write(2) families of system calls, and data is received using the recv(2) or read(2) families of system calls.

๐Ÿ’œ Address format ๐Ÿ’œ

A socket address is defined as a combination of a 32-bit Context Identifier (CID) and a 32-bit port number. The CID identifies the source or destination, which is either a virtual machine or the host. The port number differentiates between multiple services running on a single machine.

struct sockaddr_vm {
    sa_family_t    svm_family;    /* Address family: AF_VSOCK */
    unsigned short svm_reserved1;
    unsigned int   svm_port;      /* Port # in host byte order */
    unsigned int   svm_cid;       /* Address in host byte order */
    unsigned char  svm_zero[sizeof(struct sockaddr) -
                            sizeof(sa_family_t) -
                            sizeof(unsigned short) -
                            sizeof(unsigned int) -
                            sizeof(unsigned int)];
};

okay let's break down every field because we do not skim here:

  • svm_family is always set to AF_VSOCK. Always.
  • svm_reserved1 is always set to 0.
  • svm_port contains the port number in host byte order. Port numbers below 1024 are called privileged ports. Only a process with the CAP_NET_BIND_SERVICE capability may bind(2) to these port numbers. No capability, no entry. ๐Ÿšซ
  • svm_zero must be zero-filled.

There are several special addresses, and you're gonna want these memorized fr fr:

  • VMADDR_CID_ANY (-1U) โ€” means any address for binding.
  • VMADDR_CID_HYPERVISOR (0) โ€” reserved for services built into the hypervisor.
  • VMADDR_CID_LOCAL (1) โ€” the well-known address for local communication (loopback).
  • VMADDR_CID_HOST (2) โ€” the well-known address of the host.

The special constant VMADDR_PORT_ANY (-1U) means any port number for binding.

๐Ÿงš Live migration ๐Ÿงš

Sockets are affected by live migration of virtual machines. It's giving plot twist. ๐ŸŒ€

Connected SOCK_STREAM sockets become disconnected when the virtual machine migrates to a new host. Applications must reconnect when this happens. So if your stream socket suddenly ghosts you, that's why โ€” go reconnect, bestie.

The local CID may change across live migration if the old CID is not available on the new host. Bound sockets are automatically updated to the new CID. Slay, they fixed themselves. ๐Ÿ’…

๐ŸŒˆ Ioctls ๐ŸŒˆ

The following ioctls are available on the /dev/vsock device.

  • IOCTL_VM_SOCKETS_GET_LOCAL_CID โ€” Get the CID of the local machine. The argument is a pointer to an unsigned int.
ioctl(fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &cid);

Consider using VMADDR_CID_ANY when binding instead of getting the local CID with IOCTL_VM_SOCKETS_GET_LOCAL_CID. Pro tip from the manpage itself, we love an easy shortcut. ๐ŸŽ€

๐Ÿซถ Local communication ๐Ÿซถ

VMADDR_CID_LOCAL (1) directs packets to the same host that generated them. This is useful for testing applications on a single host and for debugging. Perfect for when you just wanna test your own vibe in isolation. โœจ

The local CID obtained with IOCTL_VM_SOCKETS_GET_LOCAL_CID can be used for the same purpose, but it is preferable to use VMADDR_CID_LOCAL.


๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

  • EACCES โ€” Unable to bind to a privileged port without the CAP_NET_BIND_SERVICE capability.
  • EADDRINUSE โ€” Unable to bind to a port that is already in use. That port is taken, bestie, it has a boyfriend.
  • EADDRNOTAVAIL โ€” Unable to find a free port for binding or unable to bind to a nonlocal CID.
  • EINVAL โ€” Invalid parameters. This includes: attempting to bind a socket that is already bound, providing an invalid struct sockaddr_vm, and other input validation errors.
  • ENOPROTOOPT โ€” Invalid socket option in setsockopt(2) or getsockopt(2).
  • ENOTCONN โ€” Unable to perform operation on an unconnected socket.
  • EOPNOTSUPP โ€” Operation not supported. This includes: the MSG_OOB flag that is not implemented for the send(2) family of syscalls and MSG_PEEK for the recv(2) family of syscalls.
  • EPROTONOSUPPORT โ€” Invalid socket protocol number. The protocol should always be 0.
  • ESOCKTNOSUPPORT โ€” Unsupported socket type in socket(2). Only SOCK_STREAM and SOCK_DGRAM are valid.

โญ VERSIONS โญ

Support for VMware (VMCI) has been available since Linux 3.9. KVM (virtio) is supported since Linux 4.8. Hyper-V is supported since Linux 4.14.

VMADDR_CID_LOCAL is supported since Linux 5.6. Local communication in the guest and on the host is available since Linux 5.6. Previous versions supported only local communication within a guest (not on the host), and with only some transports (VMCI and virtio).


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

bind(2), connect(2), listen(2), recv(2), send(2), socket(2), capabilities(7)


and that's the whole page, bestie, you ate it up fr fr ๐Ÿ“๐Ÿ’– this was vsock(7) from the Linux man-pages 6.20, dated 2025-05-17 โ€” go forth and socket responsibly, mwah ๐Ÿ’…๐ŸŒˆ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms