2 .\" Copyright (c) 2020 Apple Inc. All rights reserved.
4 .\" @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 .\" This file contains Original Code and/or Modifications of Original Code
7 .\" as defined in and that are subject to the Apple Public Source License
8 .\" Version 2.0 (the 'License'). You may not use this file except in
9 .\" compliance with the License. The rights granted to you under the License
10 .\" may not be used to create, or enable the creation or redistribution of,
11 .\" unlawful or unlicensed copies of an Apple operating system, or to
12 .\" circumvent, violate, or enable the circumvention or violation of, any
13 .\" terms of an Apple operating system software license agreement.
15 .\" Please obtain a copy of the License at
16 .\" http://www.opensource.apple.com/apsl/ and read it before using this file.
18 .\" The Original Code and all software distributed under the License are
19 .\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 .\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 .\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 .\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 .\" Please see the License for the specific language governing rights and
24 .\" limitations under the License.
26 .\" @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 .\" @(#)vsock.4 7/9/2020
40 .Fn socket AF_VSOCK SOCK_STREAM 0
44 protocol allows for socket communication between a virtual machine and its host. Socket connections may be established using standard socket interfaces. Currently, only stream connections from a guest are supported using this protocol.
46 .Ss "Non-blocking connect"
49 socket is set non-blocking, and the connection cannot be established immediately,
51 returns with the error
53 and the connection is established asynchronously.
55 When the asynchronous connection completes successfully,
61 will indicate the file descriptor is ready for writing.
62 If the connection encounters an error, the file descriptor
63 is marked ready for both reading and writing, and the pending error
64 can be retrieved via the socket option
67 Note that even if the socket is non-blocking, it is possible for the connection
68 to be established immediately. In that case
73 Sockets bound to the vsock protocol family utilize
74 the following addressing structure which can be found in the header
76 .Bd -literal -offset indent
79 sa_family_t svm_family;
80 uint16_t svm_reserved1;
86 Addresses consist of a cid and a port.
89 contains the total length of the structure, while the field
95 is reserved and should be set to zero.
97 Sockets may be created with the local address
101 matching on incoming messages.
109 .Dv VMADDR_CID_HYPERVISOR
116 to the hypervisor or host respectively.
118 may be used to obtain the next available free port when calling
121 .Bl -tag -width ".Dv VMADDR_CID_HYPERVISOR"
122 .It Dv VMADDR_CID_ANY
123 Wildcard matches any address.
124 .It Dv VMADDR_CID_HYPERVISOR
125 The address of the hypervisor.
126 .It Dv VMADDR_CID_HOST
127 The address of the host.
130 .Bl -tag -width ".Dv VMADDR_CID_HYPERVISOR"
131 .It Dv VMADDR_PORT_ANY
132 Wildcard matches any port.
137 socket operation may fail with a general socket error or one of the following
140 .Bl -tag -width ".It Bq Er EADDRNOTAVAIL"
144 when attempting to bind to a privileged port;
148 when attempting to bind to a cid and port that is already in use;
149 .It Bq Er EADDRNOTAVAIL
152 when attempting to bind to an invalid cid or port;
156 when attempting to connect to an invalid cid;
157 .It Bq Er EINPROGRESS
160 when attempting to connect using a non-blocking socket;
162 when passing an invalid parameter;
164 when a vsock transport does not exist;
166 when performing an operation on a non-connected socket;
170 when a connection attempt has timed out;
171 .It Bq Er EWOULDBLOCK
176 when sending or receiving using a non-blocking socket.
181 command codes below are defined in
186 #include <sys/ioctl.h>
187 #include <sys/vsock.h>
190 The third argument to
192 should be a pointer to the type indicated in parenthesis.
193 .Bl -tag -width IOCTL_VM_SOCKETS_GET_LOCAL_CID
194 .It Dv IOCTL_VM_SOCKETS_GET_LOCAL_CID
196 Returns the local cid of this socket's transport.