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