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