# phpman > man > VSOCK(7)

[VSOCK(7)](https://www.chedong.com/phpMan.php/man/VSOCK/7/markdown)                              Linux Programmer's Manual                             [VSOCK(7)](https://www.chedong.com/phpMan.php/man/VSOCK/7/markdown)



## 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 facilitates communication between virtual machines and the host they
       are running on.  This address family is used by guest agents  and  hypervisor  services  that
       need a communications channel that is independent of virtual machine network configuration.

       Valid  socket types are **SOCK**___**STREAM** and **SOCK**___**DGRAM**.  **SOCK**___**STREAM** provides connection-oriented
       byte streams with guaranteed, in-order delivery.  **SOCK**___**DGRAM** provides a connectionless  data‐
       gram  packet  service  with  best-effort  delivery and best-effort ordering.  Availability of
       these socket types is dependent on the underlying hypervisor.

       A new socket is created with

           socket(AF_VSOCK, socket_type, 0);

       When a process wants to establish a connection, it calls [**connect**(2)](https://www.chedong.com/phpMan.php/man/connect/2/markdown) with a given  destination
       socket address.  The socket is automatically bound to a free port if unbound.

       A  process  can  listen  for  incoming connections by first binding to a socket address using
       [**bind**(2)](https://www.chedong.com/phpMan.php/man/bind/2/markdown) and then calling [**listen**(2)](https://www.chedong.com/phpMan.php/man/listen/2/markdown).

       Data is transmitted using the [**send**(2)](https://www.chedong.com/phpMan.php/man/send/2/markdown) or [**write**(2)](https://www.chedong.com/phpMan.php/man/write/2/markdown) families of system calls and  data  is  re‐
       ceived using the [**recv**(2)](https://www.chedong.com/phpMan.php/man/recv/2/markdown) or [**read**(2)](https://www.chedong.com/phpMan.php/man/read/2/markdown) 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)];
           };

       _svm_family_ is always set to **AF**___**VSOCK**.  _svm_reserved1_ is always set to 0.   _svm_port_  contains
       the port number in host byte order.  The port numbers below 1024 are called _privileged_ _ports_.
       Only a process with the **CAP**___**NET**___**BIND**___**SERVICE** capability may [**bind**(2)](https://www.chedong.com/phpMan.php/man/bind/2/markdown) to  these  port  numbers.
       _svm_zero_ must be zero-filled.

       There  are  several  special  addresses:  **VMADDR**___**CID**___**ANY** (-1U) means any address for binding;
       **VMADDR**___**CID**___**HYPERVISOR** (0) is reserved for services built into the hypervisor;  **VMADDR**___**CID**___**LO**‐‐
       **CAL**  (1) is the well-known address for local communication (loopback); **VMADDR**___**CID**___**HOST** (2) is
       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.   Connected  **SOCK**___**STREAM**  sockets
       become  disconnected  when the virtual machine migrates to a new host.  Applications must re‐
       connect when this happens.

       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.

### Ioctls
       **IOCTL**___**VM**___**SOCKETS**___**GET**___**LOCAL**___**CID**
              Get the CID of the local machine.  The argument is a pointer to an _unsigned_ _int_.

                  ioctl(socket, 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**.

### 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.

       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.

       **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)](https://www.chedong.com/phpMan.php/man/setsockopt/2/markdown) or [**getsockopt**(2)](https://www.chedong.com/phpMan.php/man/getsockopt/2/markdown).

       **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)](https://www.chedong.com/phpMan.php/man/send/2/markdown) family of syscalls and **MSG**___**PEEK** for the [**recv**(2)](https://www.chedong.com/phpMan.php/man/recv/2/markdown) family of syscalls.

       **EPROTONOSUPPORT**
              Invalid socket protocol number.  The protocol should always be 0.

       **ESOCKTNOSUPPORT**
              Unsupported socket type in [**socket**(2)](https://www.chedong.com/phpMan.php/man/socket/2/markdown).  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)](https://www.chedong.com/phpMan.php/man/bind/2/markdown), [**connect**(2)](https://www.chedong.com/phpMan.php/man/connect/2/markdown), [**listen**(2)](https://www.chedong.com/phpMan.php/man/listen/2/markdown), [**recv**(2)](https://www.chedong.com/phpMan.php/man/recv/2/markdown), [**send**(2)](https://www.chedong.com/phpMan.php/man/send/2/markdown), [**socket**(2)](https://www.chedong.com/phpMan.php/man/socket/2/markdown), [**capabilities**(7)](https://www.chedong.com/phpMan.php/man/capabilities/7/markdown)

## COLOPHON
       This  page  is  part  of  release  5.10 of the Linux _man-pages_ project.  A description of the
       project, information about reporting bugs, and the latest version of this page, can be  found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-02-09                                     [VSOCK(7)](https://www.chedong.com/phpMan.php/man/VSOCK/7/markdown)
