xref: /xnu-10002.41.9/bsd/man/man4/vsock.4 (revision 699cd48037512bf4380799317ca44ca453c82f57)
1*699cd480SApple OSS Distributions.\"
2*699cd480SApple OSS Distributions.\" Copyright (c) 2020 Apple Inc. All rights reserved.
3*699cd480SApple OSS Distributions.\"
4*699cd480SApple OSS Distributions.\" @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions.\"
6*699cd480SApple OSS Distributions.\" This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions.\" as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions.\" Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions.\" compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions.\" may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions.\" unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions.\" circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions.\" terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions.\"
15*699cd480SApple OSS Distributions.\" Please obtain a copy of the License at
16*699cd480SApple OSS Distributions.\" http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions.\"
18*699cd480SApple OSS Distributions.\" The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions.\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions.\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions.\" Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions.\" limitations under the License.
25*699cd480SApple OSS Distributions.\"
26*699cd480SApple OSS Distributions.\" @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions.\"
28*699cd480SApple OSS Distributions.\"     @(#)vsock.4 7/9/2020
29*699cd480SApple OSS Distributions.\"
30*699cd480SApple OSS Distributions.Dd July 9, 2020
31*699cd480SApple OSS Distributions.Dt VSOCK 4
32*699cd480SApple OSS Distributions.Os macOS
33*699cd480SApple OSS Distributions.Sh NAME
34*699cd480SApple OSS Distributions.Nm vsock
35*699cd480SApple OSS Distributions.Nd VM Sockets
36*699cd480SApple OSS Distributions.Sh SYNOPSIS
37*699cd480SApple OSS Distributions.In sys/socket.h
38*699cd480SApple OSS Distributions.In sys/vsock.h
39*699cd480SApple OSS Distributions.Ft int
40*699cd480SApple OSS Distributions.Fn socket AF_VSOCK SOCK_STREAM 0
41*699cd480SApple OSS Distributions.Sh DESCRIPTION
42*699cd480SApple OSS DistributionsThe
43*699cd480SApple OSS Distributions.Tn vsock
44*699cd480SApple OSS Distributionsprotocol 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.
45*699cd480SApple OSS Distributions.Pp
46*699cd480SApple OSS Distributions.Ss "Non-blocking connect"
47*699cd480SApple OSS DistributionsWhen a
48*699cd480SApple OSS Distributions.Tn vsock
49*699cd480SApple OSS Distributionssocket is set non-blocking, and the connection cannot be established immediately,
50*699cd480SApple OSS Distributions.Xr connect 2
51*699cd480SApple OSS Distributionsreturns with the error
52*699cd480SApple OSS Distributions.Dv EINPROGRESS ,
53*699cd480SApple OSS Distributionsand the connection is established asynchronously.
54*699cd480SApple OSS Distributions.Pp
55*699cd480SApple OSS DistributionsWhen the asynchronous connection completes successfully,
56*699cd480SApple OSS Distributions.Xr select 2
57*699cd480SApple OSS Distributionsor
58*699cd480SApple OSS Distributions.Xr poll 2
59*699cd480SApple OSS Distributionsor
60*699cd480SApple OSS Distributions.Xr kqueue 2
61*699cd480SApple OSS Distributionswill indicate the file descriptor is ready for writing.
62*699cd480SApple OSS DistributionsIf the connection encounters an error, the file descriptor
63*699cd480SApple OSS Distributionsis marked ready for both reading and writing, and the pending error
64*699cd480SApple OSS Distributionscan be retrieved via the socket option
65*699cd480SApple OSS Distributions.Dv SO_ERROR .
66*699cd480SApple OSS Distributions.Pp
67*699cd480SApple OSS DistributionsNote that even if the socket is non-blocking, it is possible for the connection
68*699cd480SApple OSS Distributionsto be established immediately. In that case
69*699cd480SApple OSS Distributions.Xr connect 2
70*699cd480SApple OSS Distributionsdoes not return with
71*699cd480SApple OSS Distributions.Dv EINPROGRESS .
72*699cd480SApple OSS Distributions.Sh ADDRESSING
73*699cd480SApple OSS DistributionsSockets bound to the vsock protocol family utilize
74*699cd480SApple OSS Distributionsthe following addressing structure which can be found in the header
75*699cd480SApple OSS Distributions.Aq Pa sys/vsock.h .
76*699cd480SApple OSS Distributions.Bd -literal -offset indent
77*699cd480SApple OSS Distributionsstruct sockaddr_vm {
78*699cd480SApple OSS Distributions	uint8_t 	svm_len;
79*699cd480SApple OSS Distributions	sa_family_t	svm_family;
80*699cd480SApple OSS Distributions	uint16_t	svm_reserved1;
81*699cd480SApple OSS Distributions	uint32_t	svm_port;
82*699cd480SApple OSS Distributions	uint32_t	svm_cid;
83*699cd480SApple OSS Distributions};
84*699cd480SApple OSS Distributions.Ed
85*699cd480SApple OSS Distributions.Pp
86*699cd480SApple OSS DistributionsAddresses consist of a cid and a port.
87*699cd480SApple OSS DistributionsThe field
88*699cd480SApple OSS Distributions.Ar svm_len
89*699cd480SApple OSS Distributionscontains the total length of the structure, while the field
90*699cd480SApple OSS Distributions.Ar svm_family
91*699cd480SApple OSS Distributionscontains the value
92*699cd480SApple OSS Distributions.Fa AF_VSOCK .
93*699cd480SApple OSS DistributionsThe field
94*699cd480SApple OSS Distributions.Fa svm_reserved1
95*699cd480SApple OSS Distributionsis reserved and should be set to zero.
96*699cd480SApple OSS Distributions.Pp
97*699cd480SApple OSS DistributionsSockets may be created with the local address
98*699cd480SApple OSS Distributions.Dv VMADDR_CID_ANY
99*699cd480SApple OSS Distributionsto effect
100*699cd480SApple OSS Distributions.Dq wildcard
101*699cd480SApple OSS Distributionsmatching on incoming messages.
102*699cd480SApple OSS DistributionsThe address in a
103*699cd480SApple OSS Distributions.Xr connect 2
104*699cd480SApple OSS Distributionscall may be given as
105*699cd480SApple OSS Distributions.Dv VMADDR_CID_ANY
106*699cd480SApple OSS Distributionsto mean
107*699cd480SApple OSS Distributions.Dq this host .
108*699cd480SApple OSS DistributionsThe cid addresses
109*699cd480SApple OSS Distributions.Dv VMADDR_CID_HYPERVISOR
110*699cd480SApple OSS Distributionsor
111*699cd480SApple OSS Distributions.Dv VMADDR_CID_HOST
112*699cd480SApple OSS Distributionsmay be used to
113*699cd480SApple OSS Distributions.Xr connect 2
114*699cd480SApple OSS Distributionsor
115*699cd480SApple OSS Distributions.Xr bind 2
116*699cd480SApple OSS Distributionsto the hypervisor or host respectively.
117*699cd480SApple OSS Distributions.Dv VMADDR_PORT_ANY
118*699cd480SApple OSS Distributionsmay be used to obtain the next available free port when calling
119*699cd480SApple OSS Distributions.Xr bind 2 .
120*699cd480SApple OSS Distributions.Ss CID Constants
121*699cd480SApple OSS Distributions.Bl -tag -width ".Dv VMADDR_CID_HYPERVISOR"
122*699cd480SApple OSS Distributions.It Dv VMADDR_CID_ANY
123*699cd480SApple OSS DistributionsWildcard matches any address.
124*699cd480SApple OSS Distributions.It Dv VMADDR_CID_HYPERVISOR
125*699cd480SApple OSS DistributionsThe address of the hypervisor.
126*699cd480SApple OSS Distributions.It Dv VMADDR_CID_HOST
127*699cd480SApple OSS DistributionsThe address of the host.
128*699cd480SApple OSS Distributions.El
129*699cd480SApple OSS Distributions.Ss Port Constants
130*699cd480SApple OSS Distributions.Bl -tag -width ".Dv VMADDR_CID_HYPERVISOR"
131*699cd480SApple OSS Distributions.It Dv VMADDR_PORT_ANY
132*699cd480SApple OSS DistributionsWildcard matches any port.
133*699cd480SApple OSS Distributions.El
134*699cd480SApple OSS Distributions.Sh ERRORS
135*699cd480SApple OSS DistributionsA
136*699cd480SApple OSS Distributions.Tn vsock
137*699cd480SApple OSS Distributionssocket operation may fail with a general socket error or one of the following
138*699cd480SApple OSS Distributions.Tn vsock
139*699cd480SApple OSS Distributionsspecific errors:
140*699cd480SApple OSS Distributions.Bl -tag -width ".It Bq Er EADDRNOTAVAIL"
141*699cd480SApple OSS Distributions.It Bq Er EACCES
142*699cd480SApple OSS Distributionsreturned by
143*699cd480SApple OSS Distributions.Xr bind 2
144*699cd480SApple OSS Distributionswhen attempting to bind to a privileged port;
145*699cd480SApple OSS Distributions.It Bq Er EADDRINUSE
146*699cd480SApple OSS Distributionsreturned by
147*699cd480SApple OSS Distributions.Xr bind 2
148*699cd480SApple OSS Distributionswhen attempting to bind to a cid and port that is already in use;
149*699cd480SApple OSS Distributions.It Bq Er EADDRNOTAVAIL
150*699cd480SApple OSS Distributionsreturned by
151*699cd480SApple OSS Distributions.Xr bind 2
152*699cd480SApple OSS Distributionswhen attempting to bind to an invalid cid or port;
153*699cd480SApple OSS Distributions.It Bq Er EFAULT
154*699cd480SApple OSS Distributionsreturned by
155*699cd480SApple OSS Distributions.Xr connect 2
156*699cd480SApple OSS Distributionswhen attempting to connect to an invalid cid;
157*699cd480SApple OSS Distributions.It Bq Er EINPROGRESS
158*699cd480SApple OSS Distributionsreturned by
159*699cd480SApple OSS Distributions.Xr connect 2
160*699cd480SApple OSS Distributionswhen attempting to connect using a non-blocking socket;
161*699cd480SApple OSS Distributions.It Bq Er EINVAL
162*699cd480SApple OSS Distributionswhen passing an invalid parameter;
163*699cd480SApple OSS Distributions.It Bq Er ENODEV
164*699cd480SApple OSS Distributionswhen a vsock transport does not exist;
165*699cd480SApple OSS Distributions.It Bq Er ENOTCONN
166*699cd480SApple OSS Distributionswhen performing an operation on a non-connected socket;
167*699cd480SApple OSS Distributions.It Bq Er ETIMEDOUT
168*699cd480SApple OSS Distributionsreturned by
169*699cd480SApple OSS Distributions.Xr connect 2
170*699cd480SApple OSS Distributionswhen a connection attempt has timed out;
171*699cd480SApple OSS Distributions.It Bq Er EWOULDBLOCK
172*699cd480SApple OSS Distributionsreturned by
173*699cd480SApple OSS Distributions.Xr send 2
174*699cd480SApple OSS Distributionsor
175*699cd480SApple OSS Distributions.Xr recv 2
176*699cd480SApple OSS Distributionswhen sending or receiving using a non-blocking socket.
177*699cd480SApple OSS Distributions.El
178*699cd480SApple OSS Distributions.Sh IOCTLS
179*699cd480SApple OSS DistributionsThe
180*699cd480SApple OSS Distributions.Xr ioctl 2
181*699cd480SApple OSS Distributionscommand codes below are defined in
182*699cd480SApple OSS Distributions.Aq Pa sys/vsock.h .
183*699cd480SApple OSS DistributionsAll commands require
184*699cd480SApple OSS Distributionsthese includes:
185*699cd480SApple OSS Distributions.Bd -literal
186*699cd480SApple OSS Distributions        #include <sys/ioctl.h>
187*699cd480SApple OSS Distributions        #include <sys/vsock.h>
188*699cd480SApple OSS Distributions.Ed
189*699cd480SApple OSS Distributions.Pp
190*699cd480SApple OSS DistributionsThe third argument to
191*699cd480SApple OSS Distributions.Xr ioctl 2
192*699cd480SApple OSS Distributionsshould be a pointer to the type indicated in parenthesis.
193*699cd480SApple OSS Distributions.Bl -tag -width IOCTL_VM_SOCKETS_GET_LOCAL_CID
194*699cd480SApple OSS Distributions.It Dv IOCTL_VM_SOCKETS_GET_LOCAL_CID
195*699cd480SApple OSS Distributions.Pq Li uint32_t
196*699cd480SApple OSS DistributionsReturns the local cid of this socket's transport.
197*699cd480SApple OSS Distributions.El
198*699cd480SApple OSS Distributions.Sh SEE ALSO
199*699cd480SApple OSS Distributions.Xr bind 2 ,
200*699cd480SApple OSS Distributions.Xr connect 2 ,
201*699cd480SApple OSS Distributions.Xr ioctl 2 ,
202*699cd480SApple OSS Distributions.Xr kqueue 2 ,
203*699cd480SApple OSS Distributions.Xr poll 2 ,
204*699cd480SApple OSS Distributions.Xr select 2 ,
205*699cd480SApple OSS Distributions.Xr socket 2
206