{"id":4112,"date":"2022-12-20T17:39:25","date_gmt":"2022-12-20T20:39:25","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/vsock-man7\/"},"modified":"2022-12-20T17:39:25","modified_gmt":"2022-12-20T20:39:25","slug":"vsock-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/vsock-man7\/","title":{"rendered":"VSOCK (man7)"},"content":{"rendered":"<h1 align=\"center\">VSOCK<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#VERSIONS\">VERSIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">vsock \u2212 Linux VSOCK address family<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <sys\/socket.h> <br \/> #include <linux\/vm_sockets.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>stream_socket<\/i> <b>= socket(AF_VSOCK, SOCK_STREAM, 0);<\/b> <i><br \/> datagram_socket<\/i> <b>= socket(AF_VSOCK, SOCK_DGRAM, 0);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Valid socket types are <b>SOCK_STREAM<\/b> and <b>SOCK_DGRAM<\/b>. <b>SOCK_STREAM<\/b> provides connection-oriented byte streams with guaranteed, in-order delivery. <b>SOCK_DGRAM<\/b> provides a connectionless datagram packet service with best-effort delivery and best-effort ordering. Availability of these socket types is dependent on the underlying hypervisor.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A new socket is created with<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">socket(AF_VSOCK, socket_type, 0);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When a process wants to establish a connection, it calls <b>connect<\/b>(2) with a given destination socket address. The socket is automatically bound to a free port if unbound.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A process can listen for incoming connections by first binding to a socket address using <b>bind<\/b>(2) and then calling <b>listen<\/b>(2).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Data is transmitted using the <b>send<\/b>(2) or <b>write<\/b>(2) families of system calls and data is received using the <b>recv<\/b>(2) or <b>read<\/b>(2) families of system calls.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Address format<\/b> <br \/> 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.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">struct sockaddr_vm { <br \/> sa_family_t svm_family; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Address family: AF_VSOCK bodies\/ usr\/ <br \/> unsigned short svm_reserved1; <br \/> unsigned int svm_port; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Port # in host byte order bodies\/ usr\/ <br \/> unsigned int svm_cid; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Address in host byte order bodies\/ usr\/ <br \/> unsigned char svm_zero[sizeof(struct sockaddr) \u2212 <br \/> sizeof(sa_family_t) \u2212 <br \/> sizeof(unsigned short) \u2212 <br \/> sizeof(unsigned int) \u2212 <br \/> sizeof(unsigned int)]; <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>svm_family<\/i> is always set to <b>AF_VSOCK<\/b>. <i>svm_reserved1<\/i> is always set to 0. <i>svm_port<\/i> contains the port number in host byte order. The port numbers below 1024 are called <i>privileged ports<\/i>. Only a process with the <b>CAP_NET_BIND_SERVICE<\/b> capability may <b>bind<\/b>(2) to these port numbers. <i>svm_zero<\/i> must be zero-filled.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There are several special addresses: <b>VMADDR_CID_ANY<\/b> (\u22121U) means any address for binding; <b>VMADDR_CID_HYPERVISOR<\/b> (0) is reserved for services built into the hypervisor; <b>VMADDR_CID_LOCAL<\/b> (1) is the well-known address for local communication (loopback); <b>VMADDR_CID_HOST<\/b> (2) is the well-known address of the host.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The special constant <b>VMADDR_PORT_ANY<\/b> (\u22121U) means any port number for binding.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Live migration<\/b> <br \/> Sockets are affected by live migration of virtual machines. Connected <b>SOCK_STREAM<\/b> sockets become disconnected when the virtual machine migrates to a new host. Applications must reconnect when this happens.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Ioctls <br \/> IOCTL_VM_SOCKETS_GET_LOCAL_CID<\/b><\/p>\n<p style=\"margin-left:22%;\">Get the CID of the local machine. The argument is a pointer to an <i>unsigned int<\/i>.<\/p>\n<p style=\"margin-left:28%; margin-top: 1em\">ioctl(socket, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &#038;cid);<\/p>\n<p style=\"margin-left:22%; margin-top: 1em\">Consider using <b>VMADDR_CID_ANY<\/b> when binding instead of getting the local CID with <b>IOCTL_VM_SOCKETS_GET_LOCAL_CID<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Local communication <br \/> VMADDR_CID_LOCAL<\/b> (1) directs packets to the same host that generated them. This is useful for testing applications on a single host and for debugging.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The local CID obtained with <b>IOCTL_VM_SOCKETS_GET_LOCAL_CID<\/b> can be used for the same purpose, but it is preferable to use <b>VMADDR_CID_LOCAL .<\/b><\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\"><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">Unable to bind to a privileged port without the <b>CAP_NET_BIND_SERVICE<\/b> capability.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EADDRINUSE<\/b><\/p>\n<p style=\"margin-left:22%;\">Unable to bind to a port that is already in use.<\/p>\n<p style=\"margin-left:11%;\"><b>EADDRNOTAVAIL<\/b><\/p>\n<p style=\"margin-left:22%;\">Unable to find a free port for binding or unable to bind to a nonlocal CID.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Invalid parameters. This includes: attempting to bind a socket that is already bound, providing an invalid struct <i>sockaddr_vm<\/i>, and other input validation errors.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOPROTOOPT<\/b><\/p>\n<p style=\"margin-left:22%;\">Invalid socket option in <b>setsockopt<\/b>(2) or <b>getsockopt<\/b>(2).<\/p>\n<p style=\"margin-left:11%;\"><b>ENOTCONN<\/b><\/p>\n<p style=\"margin-left:22%;\">Unable to perform operation on an unconnected socket.<\/p>\n<p style=\"margin-left:11%;\"><b>EOPNOTSUPP<\/b><\/p>\n<p style=\"margin-left:22%;\">Operation not supported. This includes: the <b>MSG_OOB<\/b> flag that is not implemented for the <b>send<\/b>(2) family of syscalls and <b>MSG_PEEK<\/b> for the <b>recv<\/b>(2) family of syscalls.<\/p>\n<p style=\"margin-left:11%;\"><b>EPROTONOSUPPORT<\/b><\/p>\n<p style=\"margin-left:22%;\">Invalid socket protocol number. The protocol should always be 0.<\/p>\n<p style=\"margin-left:11%;\"><b>ESOCKTNOSUPPORT<\/b><\/p>\n<p style=\"margin-left:22%;\">Unsupported socket type in <b>socket<\/b>(2). Only <b>SOCK_STREAM<\/b> and <b>SOCK_DGRAM<\/b> are valid.<\/p>\n<h2>VERSIONS <a name=\"VERSIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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).<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>bind<\/b>(2), <b>connect<\/b>(2), <b>listen<\/b>(2), <b>recv<\/b>(2), <b>send<\/b>(2), <b>socket<\/b>(2), <b>capabilities<\/b>(7)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> 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\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  vsock \u2212 Linux VSOCK address family <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,1191],"class_list":["post-4112","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-vsock"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4112"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4112\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}