1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2020 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions *
4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions *
6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions *
15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions *
18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions * limitations under the License.
25*5e3eaea3SApple OSS Distributions *
26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions */
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions #include <sys/cdefs.h>
30*5e3eaea3SApple OSS Distributions #include <sys/ioctl.h>
31*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
32*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
33*5e3eaea3SApple OSS Distributions #include <sys/vsock.h>
34*5e3eaea3SApple OSS Distributions #include <errno.h>
35*5e3eaea3SApple OSS Distributions
36*5e3eaea3SApple OSS Distributions #include <darwintest.h>
37*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
38*5e3eaea3SApple OSS Distributions
39*5e3eaea3SApple OSS Distributions #define COUNT_ELEMS(array) (sizeof (array) / sizeof (array[0]))
40*5e3eaea3SApple OSS Distributions
41*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
42*5e3eaea3SApple OSS Distributions T_META_RUN_CONCURRENTLY(true),
43*5e3eaea3SApple OSS Distributions T_META_NAMESPACE("xnu.vsock")
44*5e3eaea3SApple OSS Distributions );
45*5e3eaea3SApple OSS Distributions
46*5e3eaea3SApple OSS Distributions static int
vsock_new_socket(void)47*5e3eaea3SApple OSS Distributions vsock_new_socket(void)
48*5e3eaea3SApple OSS Distributions {
49*5e3eaea3SApple OSS Distributions int sock = socket(AF_VSOCK, SOCK_STREAM, 0);
50*5e3eaea3SApple OSS Distributions if (sock < 0 && errno == ENODEV) {
51*5e3eaea3SApple OSS Distributions T_SKIP("no vsock transport available");
52*5e3eaea3SApple OSS Distributions }
53*5e3eaea3SApple OSS Distributions T_ASSERT_GT(sock, 0, "create new vsock socket");
54*5e3eaea3SApple OSS Distributions return sock;
55*5e3eaea3SApple OSS Distributions }
56*5e3eaea3SApple OSS Distributions
57*5e3eaea3SApple OSS Distributions static uint32_t
vsock_get_local_cid(int socket)58*5e3eaea3SApple OSS Distributions vsock_get_local_cid(int socket)
59*5e3eaea3SApple OSS Distributions {
60*5e3eaea3SApple OSS Distributions uint32_t cid = 0;
61*5e3eaea3SApple OSS Distributions int result = ioctl(socket, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &cid);
62*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock ioctl cid successful");
63*5e3eaea3SApple OSS Distributions T_ASSERT_GT(cid, VMADDR_CID_HOST, "cid is set");
64*5e3eaea3SApple OSS Distributions T_ASSERT_NE(cid, VMADDR_CID_ANY, "cid is valid");
65*5e3eaea3SApple OSS Distributions
66*5e3eaea3SApple OSS Distributions return cid;
67*5e3eaea3SApple OSS Distributions }
68*5e3eaea3SApple OSS Distributions
69*5e3eaea3SApple OSS Distributions static int
vsock_bind(uint32_t cid,uint32_t port,struct sockaddr_vm * addr,int * socket)70*5e3eaea3SApple OSS Distributions vsock_bind(uint32_t cid, uint32_t port, struct sockaddr_vm * addr, int *socket)
71*5e3eaea3SApple OSS Distributions {
72*5e3eaea3SApple OSS Distributions *socket = vsock_new_socket();
73*5e3eaea3SApple OSS Distributions
74*5e3eaea3SApple OSS Distributions bzero(addr, sizeof(*addr));
75*5e3eaea3SApple OSS Distributions addr->svm_port = port;
76*5e3eaea3SApple OSS Distributions addr->svm_cid = cid;
77*5e3eaea3SApple OSS Distributions
78*5e3eaea3SApple OSS Distributions return bind(*socket, (struct sockaddr *) addr, sizeof(*addr));
79*5e3eaea3SApple OSS Distributions }
80*5e3eaea3SApple OSS Distributions
81*5e3eaea3SApple OSS Distributions static int
vsock_listen(uint32_t cid,uint32_t port,struct sockaddr_vm * addr,int backlog,int * socket)82*5e3eaea3SApple OSS Distributions vsock_listen(uint32_t cid, uint32_t port, struct sockaddr_vm * addr, int backlog, int *socket)
83*5e3eaea3SApple OSS Distributions {
84*5e3eaea3SApple OSS Distributions int result = vsock_bind(cid, port, addr, socket);
85*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind");
86*5e3eaea3SApple OSS Distributions return listen(*socket, backlog);
87*5e3eaea3SApple OSS Distributions }
88*5e3eaea3SApple OSS Distributions
89*5e3eaea3SApple OSS Distributions static int
vsock_connect(uint32_t cid,uint32_t port,int * socket)90*5e3eaea3SApple OSS Distributions vsock_connect(uint32_t cid, uint32_t port, int *socket)
91*5e3eaea3SApple OSS Distributions {
92*5e3eaea3SApple OSS Distributions *socket = vsock_new_socket();
93*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
94*5e3eaea3SApple OSS Distributions .svm_cid = cid,
95*5e3eaea3SApple OSS Distributions .svm_port = port,
96*5e3eaea3SApple OSS Distributions };
97*5e3eaea3SApple OSS Distributions return connect(*socket, (struct sockaddr *)&addr, sizeof(addr));
98*5e3eaea3SApple OSS Distributions }
99*5e3eaea3SApple OSS Distributions
100*5e3eaea3SApple OSS Distributions static struct sockaddr_vm
vsock_getsockname(int socket)101*5e3eaea3SApple OSS Distributions vsock_getsockname(int socket)
102*5e3eaea3SApple OSS Distributions {
103*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
104*5e3eaea3SApple OSS Distributions socklen_t length = sizeof(addr);
105*5e3eaea3SApple OSS Distributions int result = getsockname(socket, (struct sockaddr *)&addr, &length);
106*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock getsockname");
107*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT((int) sizeof(addr), length, "correct address length");
108*5e3eaea3SApple OSS Distributions T_ASSERT_GT(addr.svm_port, 0, "bound to non-zero local port");
109*5e3eaea3SApple OSS Distributions return addr;
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions
112*5e3eaea3SApple OSS Distributions static void
vsock_close(int socket)113*5e3eaea3SApple OSS Distributions vsock_close(int socket)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions int result = close(socket);
116*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock close");
117*5e3eaea3SApple OSS Distributions }
118*5e3eaea3SApple OSS Distributions
119*5e3eaea3SApple OSS Distributions static void
vsock_connect_peers(uint32_t cid,uint32_t port,int backlog,int * socketA,int * socketB)120*5e3eaea3SApple OSS Distributions vsock_connect_peers(uint32_t cid, uint32_t port, int backlog, int *socketA, int *socketB)
121*5e3eaea3SApple OSS Distributions {
122*5e3eaea3SApple OSS Distributions // Listen.
123*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
124*5e3eaea3SApple OSS Distributions int listen_socket;
125*5e3eaea3SApple OSS Distributions int result = vsock_listen(cid, port, &addr, backlog, &listen_socket);
126*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen");
127*5e3eaea3SApple OSS Distributions
128*5e3eaea3SApple OSS Distributions const uint32_t connection_cid = vsock_get_local_cid(listen_socket);
129*5e3eaea3SApple OSS Distributions
130*5e3eaea3SApple OSS Distributions // Connect.
131*5e3eaea3SApple OSS Distributions int connect_socket;
132*5e3eaea3SApple OSS Distributions result = vsock_connect(connection_cid, addr.svm_port, &connect_socket);
133*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock connect");
134*5e3eaea3SApple OSS Distributions
135*5e3eaea3SApple OSS Distributions // Accept.
136*5e3eaea3SApple OSS Distributions struct sockaddr_vm accepted_addr;
137*5e3eaea3SApple OSS Distributions socklen_t addrlen = sizeof(accepted_addr);
138*5e3eaea3SApple OSS Distributions int accepted_socket = accept(listen_socket, (struct sockaddr *)&accepted_addr, &addrlen);
139*5e3eaea3SApple OSS Distributions T_ASSERT_GT(accepted_socket, 0, "accepted socket");
140*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT((int) sizeof(accepted_addr), addrlen, "correct address length");
141*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT(connection_cid, accepted_addr.svm_cid, "same cid");
142*5e3eaea3SApple OSS Distributions T_ASSERT_NE_INT(VMADDR_CID_ANY, accepted_addr.svm_port, "some valid port");
143*5e3eaea3SApple OSS Distributions T_ASSERT_NE_INT(0, accepted_addr.svm_port, "some non-zero port");
144*5e3eaea3SApple OSS Distributions
145*5e3eaea3SApple OSS Distributions *socketA = connect_socket;
146*5e3eaea3SApple OSS Distributions *socketB = accepted_socket;
147*5e3eaea3SApple OSS Distributions }
148*5e3eaea3SApple OSS Distributions
149*5e3eaea3SApple OSS Distributions static void
vsock_send(int socket,char * msg)150*5e3eaea3SApple OSS Distributions vsock_send(int socket, char *msg)
151*5e3eaea3SApple OSS Distributions {
152*5e3eaea3SApple OSS Distributions T_ASSERT_NOTNULL(msg, "send message is not null");
153*5e3eaea3SApple OSS Distributions ssize_t sent_bytes = send(socket, msg, strlen(msg), 0);
154*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(strlen(msg), (unsigned long)sent_bytes, "sent all bytes");
155*5e3eaea3SApple OSS Distributions }
156*5e3eaea3SApple OSS Distributions
157*5e3eaea3SApple OSS Distributions static void
vsock_disable_sigpipe(int socket)158*5e3eaea3SApple OSS Distributions vsock_disable_sigpipe(int socket)
159*5e3eaea3SApple OSS Distributions {
160*5e3eaea3SApple OSS Distributions int on = 1;
161*5e3eaea3SApple OSS Distributions int result = setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
162*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock disable SIGPIPE");
163*5e3eaea3SApple OSS Distributions }
164*5e3eaea3SApple OSS Distributions
165*5e3eaea3SApple OSS Distributions static bool
vsock_address_exists(struct xvsockpgen * buffer,struct sockaddr_vm addr)166*5e3eaea3SApple OSS Distributions vsock_address_exists(struct xvsockpgen *buffer, struct sockaddr_vm addr)
167*5e3eaea3SApple OSS Distributions {
168*5e3eaea3SApple OSS Distributions struct xvsockpgen *xvg = buffer;
169*5e3eaea3SApple OSS Distributions struct xvsockpgen *oxvg = buffer;
170*5e3eaea3SApple OSS Distributions
171*5e3eaea3SApple OSS Distributions bool found = false;
172*5e3eaea3SApple OSS Distributions for (xvg = (struct xvsockpgen *)((char *)xvg + xvg->xvg_len);
173*5e3eaea3SApple OSS Distributions xvg->xvg_len > sizeof(struct xvsockpgen);
174*5e3eaea3SApple OSS Distributions xvg = (struct xvsockpgen *)((char *)xvg + xvg->xvg_len)) {
175*5e3eaea3SApple OSS Distributions struct xvsockpcb *xpcb = (struct xvsockpcb *)xvg;
176*5e3eaea3SApple OSS Distributions
177*5e3eaea3SApple OSS Distributions /* Ignore PCBs which were freed during copyout. */
178*5e3eaea3SApple OSS Distributions if (xpcb->xvp_gencnt > oxvg->xvg_gen) {
179*5e3eaea3SApple OSS Distributions continue;
180*5e3eaea3SApple OSS Distributions }
181*5e3eaea3SApple OSS Distributions
182*5e3eaea3SApple OSS Distributions if (xpcb->xvp_local_cid == addr.svm_cid && xpcb->xvp_remote_cid == VMADDR_CID_ANY &&
183*5e3eaea3SApple OSS Distributions xpcb->xvp_local_port == addr.svm_port && xpcb->xvp_remote_port == VMADDR_PORT_ANY) {
184*5e3eaea3SApple OSS Distributions found = true;
185*5e3eaea3SApple OSS Distributions break;
186*5e3eaea3SApple OSS Distributions }
187*5e3eaea3SApple OSS Distributions }
188*5e3eaea3SApple OSS Distributions
189*5e3eaea3SApple OSS Distributions T_ASSERT_NE(xvg, oxvg, "first and last xvsockpgen were returned");
190*5e3eaea3SApple OSS Distributions
191*5e3eaea3SApple OSS Distributions return found;
192*5e3eaea3SApple OSS Distributions }
193*5e3eaea3SApple OSS Distributions
194*5e3eaea3SApple OSS Distributions /* New Socket */
195*5e3eaea3SApple OSS Distributions
196*5e3eaea3SApple OSS Distributions T_DECL(new_socket_getsockname, "vsock new - getsockname")
197*5e3eaea3SApple OSS Distributions {
198*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
199*5e3eaea3SApple OSS Distributions
200*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
201*5e3eaea3SApple OSS Distributions socklen_t length = sizeof(struct sockaddr_vm);
202*5e3eaea3SApple OSS Distributions int result = getsockname(socket, (struct sockaddr *)&addr, &length);
203*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock getsockname");
204*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT(addr.svm_port, VMADDR_PORT_ANY, "name is any port");
205*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT(addr.svm_cid, VMADDR_CID_ANY, "name is any cid");
206*5e3eaea3SApple OSS Distributions }
207*5e3eaea3SApple OSS Distributions
208*5e3eaea3SApple OSS Distributions T_DECL(new_socket_getpeername, "vsock new - getpeername")
209*5e3eaea3SApple OSS Distributions {
210*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
211*5e3eaea3SApple OSS Distributions
212*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
213*5e3eaea3SApple OSS Distributions socklen_t length = sizeof(struct sockaddr_vm);
214*5e3eaea3SApple OSS Distributions int result = getpeername(socket, (struct sockaddr *)&addr, &length);
215*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, ENOTCONN, "vsock getpeername");
216*5e3eaea3SApple OSS Distributions }
217*5e3eaea3SApple OSS Distributions
218*5e3eaea3SApple OSS Distributions /* Ioctl */
219*5e3eaea3SApple OSS Distributions
220*5e3eaea3SApple OSS Distributions T_DECL(ioctl_cid, "vsock ioctl cid")
221*5e3eaea3SApple OSS Distributions {
222*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
223*5e3eaea3SApple OSS Distributions vsock_get_local_cid(socket);
224*5e3eaea3SApple OSS Distributions }
225*5e3eaea3SApple OSS Distributions
226*5e3eaea3SApple OSS Distributions /* Socketpair */
227*5e3eaea3SApple OSS Distributions
228*5e3eaea3SApple OSS Distributions T_DECL(socketpair, "vsock socketpair")
229*5e3eaea3SApple OSS Distributions {
230*5e3eaea3SApple OSS Distributions int pair[2];
231*5e3eaea3SApple OSS Distributions int error = socketpair(AF_VSOCK, SOCK_STREAM, 0, pair);
232*5e3eaea3SApple OSS Distributions if (error < 0 && errno == ENODEV) {
233*5e3eaea3SApple OSS Distributions T_SKIP("no vsock transport available");
234*5e3eaea3SApple OSS Distributions }
235*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(error, EOPNOTSUPP, "vsock socketpair not supported");
236*5e3eaea3SApple OSS Distributions }
237*5e3eaea3SApple OSS Distributions
238*5e3eaea3SApple OSS Distributions /* Bind */
239*5e3eaea3SApple OSS Distributions
240*5e3eaea3SApple OSS Distributions T_DECL(bind, "vsock bind to specific port")
241*5e3eaea3SApple OSS Distributions {
242*5e3eaea3SApple OSS Distributions int socket;
243*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
244*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_ANY, 8888, &addr, &socket);
245*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to specific port");
246*5e3eaea3SApple OSS Distributions }
247*5e3eaea3SApple OSS Distributions
248*5e3eaea3SApple OSS Distributions T_DECL(bind_any, "vsock bind to any port")
249*5e3eaea3SApple OSS Distributions {
250*5e3eaea3SApple OSS Distributions int socket;
251*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
252*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_ANY, VMADDR_PORT_ANY, &addr, &socket);
253*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to any port");
254*5e3eaea3SApple OSS Distributions }
255*5e3eaea3SApple OSS Distributions
256*5e3eaea3SApple OSS Distributions T_DECL(bind_getsockname, "vsock bind - getsockname")
257*5e3eaea3SApple OSS Distributions {
258*5e3eaea3SApple OSS Distributions int socket;
259*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
260*5e3eaea3SApple OSS Distributions const uint32_t port = VMADDR_PORT_ANY;
261*5e3eaea3SApple OSS Distributions const uint32_t cid = VMADDR_CID_ANY;
262*5e3eaea3SApple OSS Distributions int result = vsock_bind(cid, port, &addr, &socket);
263*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to any port");
264*5e3eaea3SApple OSS Distributions
265*5e3eaea3SApple OSS Distributions struct sockaddr_vm bound_addr = vsock_getsockname(socket);
266*5e3eaea3SApple OSS Distributions T_ASSERT_NE_INT(bound_addr.svm_port, port, "bound to unique local port");
267*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT(bound_addr.svm_cid, cid, "bound to any cid");
268*5e3eaea3SApple OSS Distributions }
269*5e3eaea3SApple OSS Distributions
270*5e3eaea3SApple OSS Distributions T_DECL(bind_hypervisor, "vsock do not bind to hypervisor cid")
271*5e3eaea3SApple OSS Distributions {
272*5e3eaea3SApple OSS Distributions int socket;
273*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
274*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_HYPERVISOR, VMADDR_PORT_ANY, &addr, &socket);
275*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EADDRNOTAVAIL, "vsock do not bind to hypervisor cid");
276*5e3eaea3SApple OSS Distributions }
277*5e3eaea3SApple OSS Distributions
278*5e3eaea3SApple OSS Distributions T_DECL(bind_reserved, "vsock do not bind to reserved cid")
279*5e3eaea3SApple OSS Distributions {
280*5e3eaea3SApple OSS Distributions int socket;
281*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
282*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_RESERVED, VMADDR_PORT_ANY, &addr, &socket);
283*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EADDRNOTAVAIL, "vsock do not bind to reserved cid");
284*5e3eaea3SApple OSS Distributions }
285*5e3eaea3SApple OSS Distributions
286*5e3eaea3SApple OSS Distributions T_DECL(bind_host, "vsock do not bind to host cid")
287*5e3eaea3SApple OSS Distributions {
288*5e3eaea3SApple OSS Distributions int socket;
289*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
290*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_HOST, VMADDR_PORT_ANY, &addr, &socket);
291*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EADDRNOTAVAIL, "vsock do not bind to host cid");
292*5e3eaea3SApple OSS Distributions }
293*5e3eaea3SApple OSS Distributions
294*5e3eaea3SApple OSS Distributions T_DECL(bind_zero, "vsock bind to port zero", T_META_ASROOT(true))
295*5e3eaea3SApple OSS Distributions {
296*5e3eaea3SApple OSS Distributions const uint32_t port = 0;
297*5e3eaea3SApple OSS Distributions
298*5e3eaea3SApple OSS Distributions int socket;
299*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
300*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_ANY, port, &addr, &socket);
301*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to port zero");
302*5e3eaea3SApple OSS Distributions
303*5e3eaea3SApple OSS Distributions struct sockaddr_vm bound_addr;
304*5e3eaea3SApple OSS Distributions socklen_t length = sizeof(struct sockaddr_vm);
305*5e3eaea3SApple OSS Distributions result = getsockname(socket, (struct sockaddr *)&bound_addr, &length);
306*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock getsockname");
307*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT((int) sizeof(bound_addr), length, "correct address length");
308*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_UINT(bound_addr.svm_port, port, "bound to local port zero");
309*5e3eaea3SApple OSS Distributions }
310*5e3eaea3SApple OSS Distributions
311*5e3eaea3SApple OSS Distributions T_DECL(bind_double, "vsock double bind")
312*5e3eaea3SApple OSS Distributions {
313*5e3eaea3SApple OSS Distributions const uint32_t cid = VMADDR_CID_ANY;
314*5e3eaea3SApple OSS Distributions const uint32_t port = 8899;
315*5e3eaea3SApple OSS Distributions
316*5e3eaea3SApple OSS Distributions int socket;
317*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
318*5e3eaea3SApple OSS Distributions int result = vsock_bind(cid, port, &addr, &socket);
319*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to a port");
320*5e3eaea3SApple OSS Distributions
321*5e3eaea3SApple OSS Distributions result = bind(socket, (struct sockaddr *) &addr, sizeof(addr));
322*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EINVAL, "vsock bind to same port");
323*5e3eaea3SApple OSS Distributions }
324*5e3eaea3SApple OSS Distributions
325*5e3eaea3SApple OSS Distributions T_DECL(bind_same, "vsock bind same address and port")
326*5e3eaea3SApple OSS Distributions {
327*5e3eaea3SApple OSS Distributions const uint32_t cid = VMADDR_CID_ANY;
328*5e3eaea3SApple OSS Distributions const uint32_t port = 3399;
329*5e3eaea3SApple OSS Distributions
330*5e3eaea3SApple OSS Distributions int socket;
331*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
332*5e3eaea3SApple OSS Distributions int result = vsock_bind(cid, port, &addr, &socket);
333*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to a port");
334*5e3eaea3SApple OSS Distributions
335*5e3eaea3SApple OSS Distributions result = vsock_bind(cid, port, &addr, &socket);
336*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EADDRINUSE, "vsock bind to same address and port");
337*5e3eaea3SApple OSS Distributions }
338*5e3eaea3SApple OSS Distributions
339*5e3eaea3SApple OSS Distributions T_DECL(bind_port_reuse, "vsock bind port reuse")
340*5e3eaea3SApple OSS Distributions {
341*5e3eaea3SApple OSS Distributions const uint32_t cid = VMADDR_CID_ANY;
342*5e3eaea3SApple OSS Distributions const uint32_t port = 9111;
343*5e3eaea3SApple OSS Distributions
344*5e3eaea3SApple OSS Distributions int socket;
345*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
346*5e3eaea3SApple OSS Distributions int result = vsock_bind(cid, port, &addr, &socket);
347*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to a port");
348*5e3eaea3SApple OSS Distributions
349*5e3eaea3SApple OSS Distributions vsock_close(socket);
350*5e3eaea3SApple OSS Distributions
351*5e3eaea3SApple OSS Distributions result = vsock_bind(cid, port, &addr, &socket);
352*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind to a port");
353*5e3eaea3SApple OSS Distributions }
354*5e3eaea3SApple OSS Distributions
355*5e3eaea3SApple OSS Distributions T_DECL(bind_privileged_non_root, "vsock bind on privileged port - non-root", T_META_ASROOT(false))
356*5e3eaea3SApple OSS Distributions {
357*5e3eaea3SApple OSS Distributions if (geteuid() == 0) {
358*5e3eaea3SApple OSS Distributions T_SKIP("test requires non-root privileges to run.");
359*5e3eaea3SApple OSS Distributions }
360*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
361*5e3eaea3SApple OSS Distributions int socket;
362*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_ANY, 5, &addr, &socket);
363*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EACCES, "vsock bind privileged as non-root");
364*5e3eaea3SApple OSS Distributions }
365*5e3eaea3SApple OSS Distributions
366*5e3eaea3SApple OSS Distributions T_DECL(bind_privileged_root, "vsock bind on privileged port - root", T_META_ASROOT(true))
367*5e3eaea3SApple OSS Distributions {
368*5e3eaea3SApple OSS Distributions if (geteuid() != 0) {
369*5e3eaea3SApple OSS Distributions T_SKIP("test requires root privileges to run.");
370*5e3eaea3SApple OSS Distributions }
371*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
372*5e3eaea3SApple OSS Distributions int socket;
373*5e3eaea3SApple OSS Distributions int result = vsock_bind(VMADDR_CID_ANY, 6, &addr, &socket);
374*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind privileged as root");
375*5e3eaea3SApple OSS Distributions }
376*5e3eaea3SApple OSS Distributions
377*5e3eaea3SApple OSS Distributions T_DECL(bind_no_family, "vsock bind with unspecified family")
378*5e3eaea3SApple OSS Distributions {
379*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
380*5e3eaea3SApple OSS Distributions
381*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
382*5e3eaea3SApple OSS Distributions .svm_family = AF_UNSPEC,
383*5e3eaea3SApple OSS Distributions .svm_cid = VMADDR_CID_ANY,
384*5e3eaea3SApple OSS Distributions .svm_port = 7321,
385*5e3eaea3SApple OSS Distributions };
386*5e3eaea3SApple OSS Distributions
387*5e3eaea3SApple OSS Distributions int result = bind(socket, (struct sockaddr *) &addr, sizeof(addr));
388*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind with unspecified family");
389*5e3eaea3SApple OSS Distributions }
390*5e3eaea3SApple OSS Distributions
391*5e3eaea3SApple OSS Distributions T_DECL(bind_vsock_family, "vsock bind with vsock family")
392*5e3eaea3SApple OSS Distributions {
393*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
394*5e3eaea3SApple OSS Distributions
395*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
396*5e3eaea3SApple OSS Distributions .svm_family = AF_VSOCK,
397*5e3eaea3SApple OSS Distributions .svm_cid = VMADDR_CID_ANY,
398*5e3eaea3SApple OSS Distributions .svm_port = 7322,
399*5e3eaea3SApple OSS Distributions };
400*5e3eaea3SApple OSS Distributions
401*5e3eaea3SApple OSS Distributions int result = bind(socket, (struct sockaddr *) &addr, sizeof(addr));
402*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock bind with vsock family");
403*5e3eaea3SApple OSS Distributions }
404*5e3eaea3SApple OSS Distributions
405*5e3eaea3SApple OSS Distributions T_DECL(bind_wrong_family, "vsock bind with wrong family")
406*5e3eaea3SApple OSS Distributions {
407*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
408*5e3eaea3SApple OSS Distributions
409*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
410*5e3eaea3SApple OSS Distributions .svm_family = AF_INET,
411*5e3eaea3SApple OSS Distributions .svm_cid = VMADDR_CID_ANY,
412*5e3eaea3SApple OSS Distributions .svm_port = 7323,
413*5e3eaea3SApple OSS Distributions };
414*5e3eaea3SApple OSS Distributions
415*5e3eaea3SApple OSS Distributions int result = bind(socket, (struct sockaddr *) &addr, sizeof(addr));
416*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EAFNOSUPPORT, "vsock bind with wrong family");
417*5e3eaea3SApple OSS Distributions }
418*5e3eaea3SApple OSS Distributions
419*5e3eaea3SApple OSS Distributions /* Listen */
420*5e3eaea3SApple OSS Distributions
421*5e3eaea3SApple OSS Distributions T_DECL(listen, "vsock listen on specific port")
422*5e3eaea3SApple OSS Distributions {
423*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
424*5e3eaea3SApple OSS Distributions int socket;
425*5e3eaea3SApple OSS Distributions int result = vsock_listen(VMADDR_CID_ANY, 8889, &addr, 10, &socket);
426*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen");
427*5e3eaea3SApple OSS Distributions }
428*5e3eaea3SApple OSS Distributions
429*5e3eaea3SApple OSS Distributions T_DECL(listen_any, "vsock listen on any port")
430*5e3eaea3SApple OSS Distributions {
431*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
432*5e3eaea3SApple OSS Distributions int socket;
433*5e3eaea3SApple OSS Distributions int result = vsock_listen(VMADDR_CID_ANY, VMADDR_PORT_ANY, &addr, 10, &socket);
434*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen");
435*5e3eaea3SApple OSS Distributions }
436*5e3eaea3SApple OSS Distributions
437*5e3eaea3SApple OSS Distributions /* Connect */
438*5e3eaea3SApple OSS Distributions
439*5e3eaea3SApple OSS Distributions T_DECL(connect_non_hypervisor, "vsock connect to remote other than hypervisor")
440*5e3eaea3SApple OSS Distributions {
441*5e3eaea3SApple OSS Distributions int socket;
442*5e3eaea3SApple OSS Distributions int result = vsock_connect(5555, 1234, &socket);
443*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EFAULT, "vsock connect non-hypervisor");
444*5e3eaea3SApple OSS Distributions }
445*5e3eaea3SApple OSS Distributions
446*5e3eaea3SApple OSS Distributions T_DECL(connect_non_listening_host, "vsock connect to non-listening host port")
447*5e3eaea3SApple OSS Distributions {
448*5e3eaea3SApple OSS Distributions int socket;
449*5e3eaea3SApple OSS Distributions int result = vsock_connect(VMADDR_CID_HOST, 7777, &socket);
450*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EAGAIN, "vsock connect non-listening host port");
451*5e3eaea3SApple OSS Distributions }
452*5e3eaea3SApple OSS Distributions
453*5e3eaea3SApple OSS Distributions T_DECL(connect_non_listening_hypervisor, "vsock connect to non-listening hypervisor port")
454*5e3eaea3SApple OSS Distributions {
455*5e3eaea3SApple OSS Distributions int socket;
456*5e3eaea3SApple OSS Distributions int result = vsock_connect(VMADDR_CID_HYPERVISOR, 4444, &socket);
457*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EAGAIN, "vsock connect non-listening hypervisor port");
458*5e3eaea3SApple OSS Distributions }
459*5e3eaea3SApple OSS Distributions
460*5e3eaea3SApple OSS Distributions T_DECL(connect_getsockname, "vsock connect - getsockname")
461*5e3eaea3SApple OSS Distributions {
462*5e3eaea3SApple OSS Distributions int socket;
463*5e3eaea3SApple OSS Distributions int result = vsock_connect(VMADDR_CID_HOST, 9999, &socket);
464*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EAGAIN, "vsock connect non-listening");
465*5e3eaea3SApple OSS Distributions
466*5e3eaea3SApple OSS Distributions vsock_getsockname(socket);
467*5e3eaea3SApple OSS Distributions }
468*5e3eaea3SApple OSS Distributions
469*5e3eaea3SApple OSS Distributions T_DECL(connect_timeout, "vsock connect with timeout")
470*5e3eaea3SApple OSS Distributions {
471*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
472*5e3eaea3SApple OSS Distributions
473*5e3eaea3SApple OSS Distributions struct timeval timeout = (struct timeval) {
474*5e3eaea3SApple OSS Distributions .tv_sec = 0,
475*5e3eaea3SApple OSS Distributions .tv_usec = 1,
476*5e3eaea3SApple OSS Distributions };
477*5e3eaea3SApple OSS Distributions int result = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
478*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock set socket timeout");
479*5e3eaea3SApple OSS Distributions
480*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
481*5e3eaea3SApple OSS Distributions .svm_cid = VMADDR_CID_HOST,
482*5e3eaea3SApple OSS Distributions .svm_port = 4321,
483*5e3eaea3SApple OSS Distributions };
484*5e3eaea3SApple OSS Distributions result = connect(socket, (struct sockaddr *)&addr, sizeof(addr));
485*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, ETIMEDOUT, "vsock connect timeout");
486*5e3eaea3SApple OSS Distributions }
487*5e3eaea3SApple OSS Distributions
488*5e3eaea3SApple OSS Distributions T_DECL(connect_non_blocking, "vsock connect non-blocking")
489*5e3eaea3SApple OSS Distributions {
490*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
491*5e3eaea3SApple OSS Distributions
492*5e3eaea3SApple OSS Distributions const uint32_t port = 4321;
493*5e3eaea3SApple OSS Distributions const uint32_t cid = vsock_get_local_cid(socket);
494*5e3eaea3SApple OSS Distributions
495*5e3eaea3SApple OSS Distributions // Listen.
496*5e3eaea3SApple OSS Distributions struct sockaddr_vm listen_addr;
497*5e3eaea3SApple OSS Distributions int listen_socket;
498*5e3eaea3SApple OSS Distributions long result = vsock_listen(cid, port, &listen_addr, 10, &listen_socket);
499*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen");
500*5e3eaea3SApple OSS Distributions
501*5e3eaea3SApple OSS Distributions // Set non-blocking.
502*5e3eaea3SApple OSS Distributions long arg = fcntl(socket, F_GETFL, NULL);
503*5e3eaea3SApple OSS Distributions T_ASSERT_GT(arg, -1L, "vsock get args");
504*5e3eaea3SApple OSS Distributions arg |= O_NONBLOCK;
505*5e3eaea3SApple OSS Distributions result = fcntl(socket, F_SETFL, arg);
506*5e3eaea3SApple OSS Distributions T_ASSERT_GT(arg, -1L, "vsock set args");
507*5e3eaea3SApple OSS Distributions
508*5e3eaea3SApple OSS Distributions // Connect.
509*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
510*5e3eaea3SApple OSS Distributions .svm_cid = cid,
511*5e3eaea3SApple OSS Distributions .svm_port = port,
512*5e3eaea3SApple OSS Distributions };
513*5e3eaea3SApple OSS Distributions result = connect(socket, (struct sockaddr *)&addr, sizeof(addr));
514*5e3eaea3SApple OSS Distributions if (result != 0 && errno != EINPROGRESS) {
515*5e3eaea3SApple OSS Distributions T_ASSERT_FAIL("vsock connect should succeed or return EINPROGRESS. errno: %u", errno);
516*5e3eaea3SApple OSS Distributions }
517*5e3eaea3SApple OSS Distributions
518*5e3eaea3SApple OSS Distributions vsock_close(socket);
519*5e3eaea3SApple OSS Distributions vsock_close(listen_socket);
520*5e3eaea3SApple OSS Distributions }
521*5e3eaea3SApple OSS Distributions
522*5e3eaea3SApple OSS Distributions /* Shutdown */
523*5e3eaea3SApple OSS Distributions
524*5e3eaea3SApple OSS Distributions T_DECL(shutdown_not_connected, "vsock shutdown - not connected")
525*5e3eaea3SApple OSS Distributions {
526*5e3eaea3SApple OSS Distributions int how[] = {SHUT_RD, SHUT_WR, SHUT_RDWR};
527*5e3eaea3SApple OSS Distributions for (unsigned long i = 0; i < COUNT_ELEMS(how); i++) {
528*5e3eaea3SApple OSS Distributions int socket = vsock_new_socket();
529*5e3eaea3SApple OSS Distributions int result = shutdown(socket, how[i]);
530*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, ENOTCONN, "vsock cannot shutdown");
531*5e3eaea3SApple OSS Distributions }
532*5e3eaea3SApple OSS Distributions }
533*5e3eaea3SApple OSS Distributions
534*5e3eaea3SApple OSS Distributions T_DECL(shutdown_reads, "vsock shutdown - reads")
535*5e3eaea3SApple OSS Distributions {
536*5e3eaea3SApple OSS Distributions int socketA, socketB;
537*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 8989, 10, &socketA, &socketB);
538*5e3eaea3SApple OSS Distributions
539*5e3eaea3SApple OSS Distributions char *msg = "This is test message.\n";
540*5e3eaea3SApple OSS Distributions
541*5e3eaea3SApple OSS Distributions // 'A' sends a message.
542*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
543*5e3eaea3SApple OSS Distributions
544*5e3eaea3SApple OSS Distributions // 'B' shutsdown reads.
545*5e3eaea3SApple OSS Distributions int result = shutdown(socketB, SHUT_RD);
546*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock shutdown reads");
547*5e3eaea3SApple OSS Distributions
548*5e3eaea3SApple OSS Distributions // 'B' reads nothing.
549*5e3eaea3SApple OSS Distributions char buffer[1024] = {0};
550*5e3eaea3SApple OSS Distributions ssize_t read_bytes = read(socketB, buffer, 1024);
551*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(0L, read_bytes, "read zero bytes");
552*5e3eaea3SApple OSS Distributions
553*5e3eaea3SApple OSS Distributions // 'B' can still send.
554*5e3eaea3SApple OSS Distributions vsock_send(socketB, msg);
555*5e3eaea3SApple OSS Distributions
556*5e3eaea3SApple OSS Distributions vsock_close(socketA);
557*5e3eaea3SApple OSS Distributions vsock_close(socketB);
558*5e3eaea3SApple OSS Distributions }
559*5e3eaea3SApple OSS Distributions
560*5e3eaea3SApple OSS Distributions T_DECL(shutdown_writes, "vsock shutdown - writes")
561*5e3eaea3SApple OSS Distributions {
562*5e3eaea3SApple OSS Distributions int socketA, socketB;
563*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 8787, 10, &socketA, &socketB);
564*5e3eaea3SApple OSS Distributions
565*5e3eaea3SApple OSS Distributions char *msg = "This is test message.\n";
566*5e3eaea3SApple OSS Distributions
567*5e3eaea3SApple OSS Distributions // 'A' sends a message.
568*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
569*5e3eaea3SApple OSS Distributions
570*5e3eaea3SApple OSS Distributions // 'B' sends a message.
571*5e3eaea3SApple OSS Distributions vsock_send(socketB, msg);
572*5e3eaea3SApple OSS Distributions
573*5e3eaea3SApple OSS Distributions // send() hits us with a SIGPIPE if peer closes. ignore this and catch the error code.
574*5e3eaea3SApple OSS Distributions vsock_disable_sigpipe(socketB);
575*5e3eaea3SApple OSS Distributions
576*5e3eaea3SApple OSS Distributions // 'B' shutsdown writes.
577*5e3eaea3SApple OSS Distributions int result = shutdown(socketB, SHUT_WR);
578*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock shutdown writes");
579*5e3eaea3SApple OSS Distributions
580*5e3eaea3SApple OSS Distributions // 'B' fails to write.
581*5e3eaea3SApple OSS Distributions ssize_t sent_bytes = send(socketB, msg, strlen(msg), 0);
582*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(sent_bytes, EPIPE, "vsock cannot write");
583*5e3eaea3SApple OSS Distributions
584*5e3eaea3SApple OSS Distributions // 'B' can still read.
585*5e3eaea3SApple OSS Distributions char buffer[1024] = {0};
586*5e3eaea3SApple OSS Distributions ssize_t read_bytes = read(socketB, buffer, 1024);
587*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(strlen(msg), (unsigned long)read_bytes, "read all bytes");
588*5e3eaea3SApple OSS Distributions
589*5e3eaea3SApple OSS Distributions vsock_close(socketA);
590*5e3eaea3SApple OSS Distributions vsock_close(socketB);
591*5e3eaea3SApple OSS Distributions }
592*5e3eaea3SApple OSS Distributions
593*5e3eaea3SApple OSS Distributions T_DECL(shutdown_both, "vsock shutdown - both")
594*5e3eaea3SApple OSS Distributions {
595*5e3eaea3SApple OSS Distributions int socketA, socketB;
596*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 8686, 10, &socketA, &socketB);
597*5e3eaea3SApple OSS Distributions
598*5e3eaea3SApple OSS Distributions char *msg = "This is test message.\n";
599*5e3eaea3SApple OSS Distributions char buffer[1024] = {0};
600*5e3eaea3SApple OSS Distributions
601*5e3eaea3SApple OSS Distributions // 'A' sends a message.
602*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
603*5e3eaea3SApple OSS Distributions
604*5e3eaea3SApple OSS Distributions // 'B' sends a message.
605*5e3eaea3SApple OSS Distributions vsock_send(socketB, msg);
606*5e3eaea3SApple OSS Distributions
607*5e3eaea3SApple OSS Distributions // 'B' reads a message.
608*5e3eaea3SApple OSS Distributions ssize_t read_bytes = read(socketB, buffer, 1024);
609*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(strlen(msg), (unsigned long)read_bytes, "read all bytes");
610*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_STR(msg, buffer, "same message");
611*5e3eaea3SApple OSS Distributions
612*5e3eaea3SApple OSS Distributions // 'A' sends a message.
613*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
614*5e3eaea3SApple OSS Distributions
615*5e3eaea3SApple OSS Distributions // send() hits us with a SIGPIPE if peer closes. ignore this and catch the error code.
616*5e3eaea3SApple OSS Distributions vsock_disable_sigpipe(socketB);
617*5e3eaea3SApple OSS Distributions
618*5e3eaea3SApple OSS Distributions // 'B' shutsdown reads and writes.
619*5e3eaea3SApple OSS Distributions int result = shutdown(socketB, SHUT_RDWR);
620*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock shutdown reads and writes");
621*5e3eaea3SApple OSS Distributions
622*5e3eaea3SApple OSS Distributions // 'B' fails to write.
623*5e3eaea3SApple OSS Distributions ssize_t sent_bytes = send(socketB, msg, strlen(msg), 0);
624*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(sent_bytes, EPIPE, "vsock cannot write");
625*5e3eaea3SApple OSS Distributions
626*5e3eaea3SApple OSS Distributions // 'B' reads nothing.
627*5e3eaea3SApple OSS Distributions read_bytes = read(socketB, buffer, 1024);
628*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(0L, read_bytes, "read zero bytes");
629*5e3eaea3SApple OSS Distributions
630*5e3eaea3SApple OSS Distributions vsock_close(socketA);
631*5e3eaea3SApple OSS Distributions vsock_close(socketB);
632*5e3eaea3SApple OSS Distributions }
633*5e3eaea3SApple OSS Distributions
634*5e3eaea3SApple OSS Distributions /* Communication */
635*5e3eaea3SApple OSS Distributions
636*5e3eaea3SApple OSS Distributions T_DECL(talk_self, "vsock talk to self")
637*5e3eaea3SApple OSS Distributions {
638*5e3eaea3SApple OSS Distributions int socketA, socketB;
639*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 4545, 10, &socketA, &socketB);
640*5e3eaea3SApple OSS Distributions
641*5e3eaea3SApple OSS Distributions char buffer[1024] = {0};
642*5e3eaea3SApple OSS Distributions
643*5e3eaea3SApple OSS Distributions for (int i = 0; i < 64; i++) {
644*5e3eaea3SApple OSS Distributions // Send a message.
645*5e3eaea3SApple OSS Distributions char *msg = (char*)malloc(64 * sizeof(char));
646*5e3eaea3SApple OSS Distributions sprintf(msg, "This is test message %d\n", i);
647*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
648*5e3eaea3SApple OSS Distributions
649*5e3eaea3SApple OSS Distributions // Receive a message.
650*5e3eaea3SApple OSS Distributions ssize_t read_bytes = read(socketB, buffer, 1024);
651*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(strlen(msg), (unsigned long)read_bytes, "read all bytes");
652*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_STR(msg, buffer, "same message");
653*5e3eaea3SApple OSS Distributions free(msg);
654*5e3eaea3SApple OSS Distributions }
655*5e3eaea3SApple OSS Distributions
656*5e3eaea3SApple OSS Distributions vsock_close(socketA);
657*5e3eaea3SApple OSS Distributions vsock_close(socketB);
658*5e3eaea3SApple OSS Distributions }
659*5e3eaea3SApple OSS Distributions
660*5e3eaea3SApple OSS Distributions T_DECL(talk_self_double, "vsock talk to self - double sends")
661*5e3eaea3SApple OSS Distributions {
662*5e3eaea3SApple OSS Distributions int socketA, socketB;
663*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 4646, 10, &socketA, &socketB);
664*5e3eaea3SApple OSS Distributions
665*5e3eaea3SApple OSS Distributions char buffer[1024] = {0};
666*5e3eaea3SApple OSS Distributions
667*5e3eaea3SApple OSS Distributions for (int i = 0; i < 64; i++) {
668*5e3eaea3SApple OSS Distributions // Send a message.
669*5e3eaea3SApple OSS Distributions char *msg = (char*)malloc(64 * sizeof(char));
670*5e3eaea3SApple OSS Distributions sprintf(msg, "This is test message %d\n", i);
671*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
672*5e3eaea3SApple OSS Distributions
673*5e3eaea3SApple OSS Distributions // Send the same message.
674*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
675*5e3eaea3SApple OSS Distributions
676*5e3eaea3SApple OSS Distributions // Receive a message.
677*5e3eaea3SApple OSS Distributions ssize_t read_bytes = read(socketB, buffer, 1024);
678*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(strlen(msg) * 2, (unsigned long)read_bytes, "read all bytes");
679*5e3eaea3SApple OSS Distributions char *expected_msg = (char*)malloc(64 * sizeof(char));
680*5e3eaea3SApple OSS Distributions sprintf(expected_msg, "%s%s", msg, msg);
681*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_STR(expected_msg, buffer, "same message");
682*5e3eaea3SApple OSS Distributions free(msg);
683*5e3eaea3SApple OSS Distributions free(expected_msg);
684*5e3eaea3SApple OSS Distributions }
685*5e3eaea3SApple OSS Distributions
686*5e3eaea3SApple OSS Distributions vsock_close(socketA);
687*5e3eaea3SApple OSS Distributions vsock_close(socketB);
688*5e3eaea3SApple OSS Distributions }
689*5e3eaea3SApple OSS Distributions
690*5e3eaea3SApple OSS Distributions T_DECL(talk_self_early_close, "vsock talk to self - peer closes early")
691*5e3eaea3SApple OSS Distributions {
692*5e3eaea3SApple OSS Distributions int socketA, socketB;
693*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 4646, 10, &socketA, &socketB);
694*5e3eaea3SApple OSS Distributions
695*5e3eaea3SApple OSS Distributions char *msg = "This is a message.";
696*5e3eaea3SApple OSS Distributions vsock_send(socketA, msg);
697*5e3eaea3SApple OSS Distributions
698*5e3eaea3SApple OSS Distributions // send() hits us with a SIGPIPE if peer closes. ignore this and catch the error code.
699*5e3eaea3SApple OSS Distributions vsock_disable_sigpipe(socketA);
700*5e3eaea3SApple OSS Distributions
701*5e3eaea3SApple OSS Distributions vsock_close(socketB);
702*5e3eaea3SApple OSS Distributions
703*5e3eaea3SApple OSS Distributions ssize_t result = send(socketA, msg, strlen(msg), 0);
704*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, EPIPE, "vsock peer closed");
705*5e3eaea3SApple OSS Distributions
706*5e3eaea3SApple OSS Distributions vsock_close(socketA);
707*5e3eaea3SApple OSS Distributions }
708*5e3eaea3SApple OSS Distributions
709*5e3eaea3SApple OSS Distributions T_DECL(talk_self_connections, "vsock talk to self - too many connections")
710*5e3eaea3SApple OSS Distributions {
711*5e3eaea3SApple OSS Distributions const uint32_t port = 4747;
712*5e3eaea3SApple OSS Distributions const int backlog = 1;
713*5e3eaea3SApple OSS Distributions
714*5e3eaea3SApple OSS Distributions struct sockaddr_vm listen_addr;
715*5e3eaea3SApple OSS Distributions int listen_socket;
716*5e3eaea3SApple OSS Distributions int result = vsock_listen(VMADDR_CID_ANY, port, &listen_addr, backlog, &listen_socket);
717*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen");
718*5e3eaea3SApple OSS Distributions
719*5e3eaea3SApple OSS Distributions const uint32_t connection_cid = vsock_get_local_cid(listen_socket);
720*5e3eaea3SApple OSS Distributions
721*5e3eaea3SApple OSS Distributions // One backlog.
722*5e3eaea3SApple OSS Distributions int connected_socket = vsock_new_socket();
723*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr = (struct sockaddr_vm) {
724*5e3eaea3SApple OSS Distributions .svm_cid = connection_cid,
725*5e3eaea3SApple OSS Distributions .svm_port = port,
726*5e3eaea3SApple OSS Distributions };
727*5e3eaea3SApple OSS Distributions result = connect(connected_socket, (struct sockaddr *)&addr, sizeof(addr));
728*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock connection successful");
729*5e3eaea3SApple OSS Distributions
730*5e3eaea3SApple OSS Distributions int bad_socket = vsock_new_socket();
731*5e3eaea3SApple OSS Distributions result = connect(bad_socket, (struct sockaddr *)&addr, sizeof(addr));
732*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_FAILURE(result, ECONNREFUSED, "vsock connection refused");
733*5e3eaea3SApple OSS Distributions
734*5e3eaea3SApple OSS Distributions vsock_close(connected_socket);
735*5e3eaea3SApple OSS Distributions vsock_close(listen_socket);
736*5e3eaea3SApple OSS Distributions }
737*5e3eaea3SApple OSS Distributions
738*5e3eaea3SApple OSS Distributions // rdar://84098487 (SEED: Web: Virtio-socket sent data lost after 128KB)
739*5e3eaea3SApple OSS Distributions T_DECL(talk_self_large_writes, "vsock talk to self with large writes")
740*5e3eaea3SApple OSS Distributions {
741*5e3eaea3SApple OSS Distributions int socketA, socketB;
742*5e3eaea3SApple OSS Distributions vsock_connect_peers(VMADDR_CID_ANY, 4848, 10, &socketA, &socketB);
743*5e3eaea3SApple OSS Distributions
744*5e3eaea3SApple OSS Distributions size_t size = 65536 * 4;
745*5e3eaea3SApple OSS Distributions char buffer[65536 * 4] = {0};
746*5e3eaea3SApple OSS Distributions void *random = malloc(size);
747*5e3eaea3SApple OSS Distributions
748*5e3eaea3SApple OSS Distributions for (int i = 0; i < 64; i++) {
749*5e3eaea3SApple OSS Distributions // Send a message.
750*5e3eaea3SApple OSS Distributions ssize_t sent = write(socketA, random, size);
751*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(size, sent, "sent all bytes");
752*5e3eaea3SApple OSS Distributions
753*5e3eaea3SApple OSS Distributions // Receive a message.
754*5e3eaea3SApple OSS Distributions ssize_t read_bytes = read(socketB, buffer, size);
755*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_LONG(size, (unsigned long)read_bytes, "read all bytes");
756*5e3eaea3SApple OSS Distributions
757*5e3eaea3SApple OSS Distributions // Sent and received same data.
758*5e3eaea3SApple OSS Distributions T_ASSERT_EQ_INT(0, memcmp(random, buffer, size), "sent and received same data");
759*5e3eaea3SApple OSS Distributions }
760*5e3eaea3SApple OSS Distributions
761*5e3eaea3SApple OSS Distributions free(random);
762*5e3eaea3SApple OSS Distributions vsock_close(socketA);
763*5e3eaea3SApple OSS Distributions vsock_close(socketB);
764*5e3eaea3SApple OSS Distributions }
765*5e3eaea3SApple OSS Distributions
766*5e3eaea3SApple OSS Distributions /* Sysctl */
767*5e3eaea3SApple OSS Distributions
768*5e3eaea3SApple OSS Distributions static const char* pcblist = "net.vsock.pcblist";
769*5e3eaea3SApple OSS Distributions
770*5e3eaea3SApple OSS Distributions T_DECL(vsock_pcblist_simple, "vsock pcblist sysctl - simple")
771*5e3eaea3SApple OSS Distributions {
772*5e3eaea3SApple OSS Distributions // Create some socket to discover in the pcblist.
773*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
774*5e3eaea3SApple OSS Distributions int socket;
775*5e3eaea3SApple OSS Distributions int result = vsock_listen(VMADDR_CID_ANY, 88899, &addr, 10, &socket);
776*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen on a port");
777*5e3eaea3SApple OSS Distributions
778*5e3eaea3SApple OSS Distributions // Get the buffer length for the pcblist.
779*5e3eaea3SApple OSS Distributions size_t length = 0;
780*5e3eaea3SApple OSS Distributions result = sysctlbyname(pcblist, 0, &length, 0, 0);
781*5e3eaea3SApple OSS Distributions if (result == ENOENT) {
782*5e3eaea3SApple OSS Distributions T_SKIP("%s missing", pcblist);
783*5e3eaea3SApple OSS Distributions }
784*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock pcblist get buffer size (result %d)", result);
785*5e3eaea3SApple OSS Distributions
786*5e3eaea3SApple OSS Distributions // Allocate the buffer.
787*5e3eaea3SApple OSS Distributions struct xvsockpgen *buffer = malloc(length);
788*5e3eaea3SApple OSS Distributions T_ASSERT_NOTNULL(buffer, "allocated buffer is not null");
789*5e3eaea3SApple OSS Distributions
790*5e3eaea3SApple OSS Distributions // Populate the buffer with the pcblist.
791*5e3eaea3SApple OSS Distributions result = sysctlbyname(pcblist, buffer, &length, 0, 0);
792*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock pcblist populate buffer");
793*5e3eaea3SApple OSS Distributions
794*5e3eaea3SApple OSS Distributions // The socket should exist in the list.
795*5e3eaea3SApple OSS Distributions bool exists = vsock_address_exists(buffer, addr);
796*5e3eaea3SApple OSS Distributions T_ASSERT_TRUE(exists, "vsock pcblist contains the specified socket");
797*5e3eaea3SApple OSS Distributions
798*5e3eaea3SApple OSS Distributions vsock_close(socket);
799*5e3eaea3SApple OSS Distributions free(buffer);
800*5e3eaea3SApple OSS Distributions }
801*5e3eaea3SApple OSS Distributions
802*5e3eaea3SApple OSS Distributions T_DECL(vsock_pcblist_added, "vsock pcblist sysctl - socket added")
803*5e3eaea3SApple OSS Distributions {
804*5e3eaea3SApple OSS Distributions // Get the buffer length for the pcblist.
805*5e3eaea3SApple OSS Distributions size_t length = 0;
806*5e3eaea3SApple OSS Distributions int result = sysctlbyname(pcblist, 0, &length, 0, 0);
807*5e3eaea3SApple OSS Distributions if (result == ENOENT) {
808*5e3eaea3SApple OSS Distributions T_SKIP("%s missing", pcblist);
809*5e3eaea3SApple OSS Distributions }
810*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock pcblist get buffer size (result %d)", result);
811*5e3eaea3SApple OSS Distributions
812*5e3eaea3SApple OSS Distributions // Create some socket to discover in the pcblist after making the first sysctl.
813*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
814*5e3eaea3SApple OSS Distributions int socket;
815*5e3eaea3SApple OSS Distributions result = vsock_listen(VMADDR_CID_ANY, 77799, &addr, 10, &socket);
816*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen on a port");
817*5e3eaea3SApple OSS Distributions
818*5e3eaea3SApple OSS Distributions // Allocate the buffer.
819*5e3eaea3SApple OSS Distributions struct xvsockpgen *buffer = malloc(length);
820*5e3eaea3SApple OSS Distributions T_ASSERT_NOTNULL(buffer, "allocated buffer is not null");
821*5e3eaea3SApple OSS Distributions
822*5e3eaea3SApple OSS Distributions // Populate the buffer with the pcblist.
823*5e3eaea3SApple OSS Distributions result = sysctlbyname(pcblist, buffer, &length, 0, 0);
824*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock pcblist populate buffer");
825*5e3eaea3SApple OSS Distributions
826*5e3eaea3SApple OSS Distributions // The socket was created after the buffer and cannot fit.
827*5e3eaea3SApple OSS Distributions bool exists = vsock_address_exists(buffer, addr);
828*5e3eaea3SApple OSS Distributions T_ASSERT_FALSE(exists, "vsock pcblist should not contain the new socket");
829*5e3eaea3SApple OSS Distributions
830*5e3eaea3SApple OSS Distributions vsock_close(socket);
831*5e3eaea3SApple OSS Distributions free(buffer);
832*5e3eaea3SApple OSS Distributions }
833*5e3eaea3SApple OSS Distributions
834*5e3eaea3SApple OSS Distributions T_DECL(vsock_pcblist_removed, "vsock pcblist sysctl - socket removed")
835*5e3eaea3SApple OSS Distributions {
836*5e3eaea3SApple OSS Distributions // Create some socket to be removed after making the first sysctl.
837*5e3eaea3SApple OSS Distributions struct sockaddr_vm addr;
838*5e3eaea3SApple OSS Distributions int socket;
839*5e3eaea3SApple OSS Distributions int result = vsock_listen(VMADDR_CID_ANY, 66699, &addr, 10, &socket);
840*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock listen on a port");
841*5e3eaea3SApple OSS Distributions
842*5e3eaea3SApple OSS Distributions // Get the buffer length for the pcblist.
843*5e3eaea3SApple OSS Distributions size_t length = 0;
844*5e3eaea3SApple OSS Distributions result = sysctlbyname(pcblist, 0, &length, 0, 0);
845*5e3eaea3SApple OSS Distributions if (result == ENOENT) {
846*5e3eaea3SApple OSS Distributions T_SKIP("%s missing", pcblist);
847*5e3eaea3SApple OSS Distributions }
848*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock pcblist get buffer size (result %d)", result);
849*5e3eaea3SApple OSS Distributions
850*5e3eaea3SApple OSS Distributions // Close the socket early.
851*5e3eaea3SApple OSS Distributions vsock_close(socket);
852*5e3eaea3SApple OSS Distributions
853*5e3eaea3SApple OSS Distributions // Allocate the buffer.
854*5e3eaea3SApple OSS Distributions struct xvsockpgen *buffer = malloc(length);
855*5e3eaea3SApple OSS Distributions T_ASSERT_NOTNULL(buffer, "allocated buffer is not null");
856*5e3eaea3SApple OSS Distributions
857*5e3eaea3SApple OSS Distributions // Populate the buffer with the pcblist.
858*5e3eaea3SApple OSS Distributions result = sysctlbyname(pcblist, buffer, &length, 0, 0);
859*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(result, "vsock pcblist populate buffer");
860*5e3eaea3SApple OSS Distributions
861*5e3eaea3SApple OSS Distributions // The socket was destroyed before populating the list and should not exist.
862*5e3eaea3SApple OSS Distributions bool exists = vsock_address_exists(buffer, addr);
863*5e3eaea3SApple OSS Distributions T_ASSERT_FALSE(exists, "vsock pcblist should not contain the deleted socket");
864*5e3eaea3SApple OSS Distributions
865*5e3eaea3SApple OSS Distributions free(buffer);
866*5e3eaea3SApple OSS Distributions }
867