Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * vsock test utilities
4 *
5 * Copyright (C) 2017 Red Hat, Inc.
6 *
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
9
10#include <errno.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15#include <signal.h>
16#include <unistd.h>
17#include <assert.h>
18#include <sys/epoll.h>
19#include <sys/mman.h>
20
21#include "timeout.h"
22#include "control.h"
23#include "util.h"
24
25/* Install signal handlers */
26void init_signals(void)
27{
28 struct sigaction act = {
29 .sa_handler = sigalrm,
30 };
31
32 sigaction(SIGALRM, &act, NULL);
33 signal(SIGPIPE, SIG_IGN);
34}
35
36static unsigned int parse_uint(const char *str, const char *err_str)
37{
38 char *endptr = NULL;
39 unsigned long n;
40
41 errno = 0;
42 n = strtoul(str, &endptr, 10);
43 if (errno || *endptr != '\0') {
44 fprintf(stderr, "malformed %s \"%s\"\n", err_str, str);
45 exit(EXIT_FAILURE);
46 }
47 return n;
48}
49
50/* Parse a CID in string representation */
51unsigned int parse_cid(const char *str)
52{
53 return parse_uint(str, "CID");
54}
55
56/* Parse a port in string representation */
57unsigned int parse_port(const char *str)
58{
59 return parse_uint(str, "port");
60}
61
62/* Wait for the remote to close the connection */
63void vsock_wait_remote_close(int fd)
64{
65 struct epoll_event ev;
66 int epollfd, nfds;
67
68 epollfd = epoll_create1(0);
69 if (epollfd == -1) {
70 perror("epoll_create1");
71 exit(EXIT_FAILURE);
72 }
73
74 ev.events = EPOLLRDHUP | EPOLLHUP;
75 ev.data.fd = fd;
76 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
77 perror("epoll_ctl");
78 exit(EXIT_FAILURE);
79 }
80
81 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
82 if (nfds == -1) {
83 perror("epoll_wait");
84 exit(EXIT_FAILURE);
85 }
86
87 if (nfds == 0) {
88 fprintf(stderr, "epoll_wait timed out\n");
89 exit(EXIT_FAILURE);
90 }
91
92 assert(nfds == 1);
93 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
94 assert(ev.data.fd == fd);
95
96 close(epollfd);
97}
98
99/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
100int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
101{
102 struct sockaddr_vm sa_client = {
103 .svm_family = AF_VSOCK,
104 .svm_cid = VMADDR_CID_ANY,
105 .svm_port = bind_port,
106 };
107 struct sockaddr_vm sa_server = {
108 .svm_family = AF_VSOCK,
109 .svm_cid = cid,
110 .svm_port = port,
111 };
112
113 int client_fd, ret;
114
115 client_fd = socket(AF_VSOCK, type, 0);
116 if (client_fd < 0) {
117 perror("socket");
118 exit(EXIT_FAILURE);
119 }
120
121 if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
122 perror("bind");
123 exit(EXIT_FAILURE);
124 }
125
126 timeout_begin(TIMEOUT);
127 do {
128 ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
129 timeout_check("connect");
130 } while (ret < 0 && errno == EINTR);
131 timeout_end();
132
133 if (ret < 0) {
134 perror("connect");
135 exit(EXIT_FAILURE);
136 }
137
138 return client_fd;
139}
140
141/* Connect to <cid, port> and return the file descriptor. */
142int vsock_connect(unsigned int cid, unsigned int port, int type)
143{
144 union {
145 struct sockaddr sa;
146 struct sockaddr_vm svm;
147 } addr = {
148 .svm = {
149 .svm_family = AF_VSOCK,
150 .svm_port = port,
151 .svm_cid = cid,
152 },
153 };
154 int ret;
155 int fd;
156
157 control_expectln("LISTENING");
158
159 fd = socket(AF_VSOCK, type, 0);
160 if (fd < 0) {
161 perror("socket");
162 exit(EXIT_FAILURE);
163 }
164
165 timeout_begin(TIMEOUT);
166 do {
167 ret = connect(fd, &addr.sa, sizeof(addr.svm));
168 timeout_check("connect");
169 } while (ret < 0 && errno == EINTR);
170 timeout_end();
171
172 if (ret < 0) {
173 int old_errno = errno;
174
175 close(fd);
176 fd = -1;
177 errno = old_errno;
178 }
179 return fd;
180}
181
182int vsock_stream_connect(unsigned int cid, unsigned int port)
183{
184 return vsock_connect(cid, port, SOCK_STREAM);
185}
186
187int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
188{
189 return vsock_connect(cid, port, SOCK_SEQPACKET);
190}
191
192/* Listen on <cid, port> and return the file descriptor. */
193static int vsock_listen(unsigned int cid, unsigned int port, int type)
194{
195 union {
196 struct sockaddr sa;
197 struct sockaddr_vm svm;
198 } addr = {
199 .svm = {
200 .svm_family = AF_VSOCK,
201 .svm_port = port,
202 .svm_cid = cid,
203 },
204 };
205 int fd;
206
207 fd = socket(AF_VSOCK, type, 0);
208 if (fd < 0) {
209 perror("socket");
210 exit(EXIT_FAILURE);
211 }
212
213 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
214 perror("bind");
215 exit(EXIT_FAILURE);
216 }
217
218 if (listen(fd, 1) < 0) {
219 perror("listen");
220 exit(EXIT_FAILURE);
221 }
222
223 return fd;
224}
225
226/* Listen on <cid, port> and return the first incoming connection. The remote
227 * address is stored to clientaddrp. clientaddrp may be NULL.
228 */
229int vsock_accept(unsigned int cid, unsigned int port,
230 struct sockaddr_vm *clientaddrp, int type)
231{
232 union {
233 struct sockaddr sa;
234 struct sockaddr_vm svm;
235 } clientaddr;
236 socklen_t clientaddr_len = sizeof(clientaddr.svm);
237 int fd, client_fd, old_errno;
238
239 fd = vsock_listen(cid, port, type);
240
241 control_writeln("LISTENING");
242
243 timeout_begin(TIMEOUT);
244 do {
245 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
246 timeout_check("accept");
247 } while (client_fd < 0 && errno == EINTR);
248 timeout_end();
249
250 old_errno = errno;
251 close(fd);
252 errno = old_errno;
253
254 if (client_fd < 0)
255 return client_fd;
256
257 if (clientaddr_len != sizeof(clientaddr.svm)) {
258 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
259 (size_t)clientaddr_len);
260 exit(EXIT_FAILURE);
261 }
262 if (clientaddr.sa.sa_family != AF_VSOCK) {
263 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
264 clientaddr.sa.sa_family);
265 exit(EXIT_FAILURE);
266 }
267
268 if (clientaddrp)
269 *clientaddrp = clientaddr.svm;
270 return client_fd;
271}
272
273int vsock_stream_accept(unsigned int cid, unsigned int port,
274 struct sockaddr_vm *clientaddrp)
275{
276 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
277}
278
279int vsock_stream_listen(unsigned int cid, unsigned int port)
280{
281 return vsock_listen(cid, port, SOCK_STREAM);
282}
283
284int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
285 struct sockaddr_vm *clientaddrp)
286{
287 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
288}
289
290/* Transmit bytes from a buffer and check the return value.
291 *
292 * expected_ret:
293 * <0 Negative errno (for testing errors)
294 * 0 End-of-file
295 * >0 Success (bytes successfully written)
296 */
297void send_buf(int fd, const void *buf, size_t len, int flags,
298 ssize_t expected_ret)
299{
300 ssize_t nwritten = 0;
301 ssize_t ret;
302
303 timeout_begin(TIMEOUT);
304 do {
305 ret = send(fd, buf + nwritten, len - nwritten, flags);
306 timeout_check("send");
307
308 if (ret == 0 || (ret < 0 && errno != EINTR))
309 break;
310
311 nwritten += ret;
312 } while (nwritten < len);
313 timeout_end();
314
315 if (expected_ret < 0) {
316 if (ret != -1) {
317 fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
318 ret, expected_ret);
319 exit(EXIT_FAILURE);
320 }
321 if (errno != -expected_ret) {
322 perror("send");
323 exit(EXIT_FAILURE);
324 }
325 return;
326 }
327
328 if (ret < 0) {
329 perror("send");
330 exit(EXIT_FAILURE);
331 }
332
333 if (nwritten != expected_ret) {
334 if (ret == 0)
335 fprintf(stderr, "unexpected EOF while sending bytes\n");
336
337 fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
338 nwritten, expected_ret);
339 exit(EXIT_FAILURE);
340 }
341}
342
343/* Receive bytes in a buffer and check the return value.
344 *
345 * expected_ret:
346 * <0 Negative errno (for testing errors)
347 * 0 End-of-file
348 * >0 Success (bytes successfully read)
349 */
350void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
351{
352 ssize_t nread = 0;
353 ssize_t ret;
354
355 timeout_begin(TIMEOUT);
356 do {
357 ret = recv(fd, buf + nread, len - nread, flags);
358 timeout_check("recv");
359
360 if (ret == 0 || (ret < 0 && errno != EINTR))
361 break;
362
363 nread += ret;
364 } while (nread < len);
365 timeout_end();
366
367 if (expected_ret < 0) {
368 if (ret != -1) {
369 fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
370 ret, expected_ret);
371 exit(EXIT_FAILURE);
372 }
373 if (errno != -expected_ret) {
374 perror("recv");
375 exit(EXIT_FAILURE);
376 }
377 return;
378 }
379
380 if (ret < 0) {
381 perror("recv");
382 exit(EXIT_FAILURE);
383 }
384
385 if (nread != expected_ret) {
386 if (ret == 0)
387 fprintf(stderr, "unexpected EOF while receiving bytes\n");
388
389 fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
390 nread, expected_ret);
391 exit(EXIT_FAILURE);
392 }
393}
394
395/* Transmit one byte and check the return value.
396 *
397 * expected_ret:
398 * <0 Negative errno (for testing errors)
399 * 0 End-of-file
400 * 1 Success
401 */
402void send_byte(int fd, int expected_ret, int flags)
403{
404 const uint8_t byte = 'A';
405
406 send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
407}
408
409/* Receive one byte and check the return value.
410 *
411 * expected_ret:
412 * <0 Negative errno (for testing errors)
413 * 0 End-of-file
414 * 1 Success
415 */
416void recv_byte(int fd, int expected_ret, int flags)
417{
418 uint8_t byte;
419
420 recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
421
422 if (byte != 'A') {
423 fprintf(stderr, "unexpected byte read %c\n", byte);
424 exit(EXIT_FAILURE);
425 }
426}
427
428/* Run test cases. The program terminates if a failure occurs. */
429void run_tests(const struct test_case *test_cases,
430 const struct test_opts *opts)
431{
432 int i;
433
434 for (i = 0; test_cases[i].name; i++) {
435 void (*run)(const struct test_opts *opts);
436 char *line;
437
438 printf("%d - %s...", i, test_cases[i].name);
439 fflush(stdout);
440
441 /* Full barrier before executing the next test. This
442 * ensures that client and server are executing the
443 * same test case. In particular, it means whoever is
444 * faster will not see the peer still executing the
445 * last test. This is important because port numbers
446 * can be used by multiple test cases.
447 */
448 if (test_cases[i].skip)
449 control_writeln("SKIP");
450 else
451 control_writeln("NEXT");
452
453 line = control_readln();
454 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
455
456 printf("skipped\n");
457
458 free(line);
459 continue;
460 }
461
462 control_cmpln(line, "NEXT", true);
463 free(line);
464
465 if (opts->mode == TEST_MODE_CLIENT)
466 run = test_cases[i].run_client;
467 else
468 run = test_cases[i].run_server;
469
470 if (run)
471 run(opts);
472
473 printf("ok\n");
474 }
475}
476
477void list_tests(const struct test_case *test_cases)
478{
479 int i;
480
481 printf("ID\tTest name\n");
482
483 for (i = 0; test_cases[i].name; i++)
484 printf("%d\t%s\n", i, test_cases[i].name);
485
486 exit(EXIT_FAILURE);
487}
488
489void skip_test(struct test_case *test_cases, size_t test_cases_len,
490 const char *test_id_str)
491{
492 unsigned long test_id;
493 char *endptr = NULL;
494
495 errno = 0;
496 test_id = strtoul(test_id_str, &endptr, 10);
497 if (errno || *endptr != '\0') {
498 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
499 exit(EXIT_FAILURE);
500 }
501
502 if (test_id >= test_cases_len) {
503 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
504 test_id, test_cases_len - 1);
505 exit(EXIT_FAILURE);
506 }
507
508 test_cases[test_id].skip = true;
509}
510
511unsigned long hash_djb2(const void *data, size_t len)
512{
513 unsigned long hash = 5381;
514 int i = 0;
515
516 while (i < len) {
517 hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
518 i++;
519 }
520
521 return hash;
522}
523
524size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
525{
526 size_t bytes;
527 int i;
528
529 for (bytes = 0, i = 0; i < iovnum; i++)
530 bytes += iov[i].iov_len;
531
532 return bytes;
533}
534
535unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
536{
537 unsigned long hash;
538 size_t iov_bytes;
539 size_t offs;
540 void *tmp;
541 int i;
542
543 iov_bytes = iovec_bytes(iov, iovnum);
544
545 tmp = malloc(iov_bytes);
546 if (!tmp) {
547 perror("malloc");
548 exit(EXIT_FAILURE);
549 }
550
551 for (offs = 0, i = 0; i < iovnum; i++) {
552 memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
553 offs += iov[i].iov_len;
554 }
555
556 hash = hash_djb2(tmp, iov_bytes);
557 free(tmp);
558
559 return hash;
560}
561
562/* Allocates and returns new 'struct iovec *' according pattern
563 * in the 'test_iovec'. For each element in the 'test_iovec' it
564 * allocates new element in the resulting 'iovec'. 'iov_len'
565 * of the new element is copied from 'test_iovec'. 'iov_base' is
566 * allocated depending on the 'iov_base' of 'test_iovec':
567 *
568 * 'iov_base' == NULL -> valid buf: mmap('iov_len').
569 *
570 * 'iov_base' == MAP_FAILED -> invalid buf:
571 * mmap('iov_len'), then munmap('iov_len').
572 * 'iov_base' still contains result of
573 * mmap().
574 *
575 * 'iov_base' == number -> unaligned valid buf:
576 * mmap('iov_len') + number.
577 *
578 * 'iovnum' is number of elements in 'test_iovec'.
579 *
580 * Returns new 'iovec' or calls 'exit()' on error.
581 */
582struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
583{
584 struct iovec *iovec;
585 int i;
586
587 iovec = malloc(sizeof(*iovec) * iovnum);
588 if (!iovec) {
589 perror("malloc");
590 exit(EXIT_FAILURE);
591 }
592
593 for (i = 0; i < iovnum; i++) {
594 iovec[i].iov_len = test_iovec[i].iov_len;
595
596 iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
597 PROT_READ | PROT_WRITE,
598 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
599 -1, 0);
600 if (iovec[i].iov_base == MAP_FAILED) {
601 perror("mmap");
602 exit(EXIT_FAILURE);
603 }
604
605 if (test_iovec[i].iov_base != MAP_FAILED)
606 iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
607 }
608
609 /* Unmap "invalid" elements. */
610 for (i = 0; i < iovnum; i++) {
611 if (test_iovec[i].iov_base == MAP_FAILED) {
612 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
613 perror("munmap");
614 exit(EXIT_FAILURE);
615 }
616 }
617 }
618
619 for (i = 0; i < iovnum; i++) {
620 int j;
621
622 if (test_iovec[i].iov_base == MAP_FAILED)
623 continue;
624
625 for (j = 0; j < iovec[i].iov_len; j++)
626 ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
627 }
628
629 return iovec;
630}
631
632/* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'.
633 * On error calls 'exit()'.
634 */
635void free_test_iovec(const struct iovec *test_iovec,
636 struct iovec *iovec, int iovnum)
637{
638 int i;
639
640 for (i = 0; i < iovnum; i++) {
641 if (test_iovec[i].iov_base != MAP_FAILED) {
642 if (test_iovec[i].iov_base)
643 iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
644
645 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
646 perror("munmap");
647 exit(EXIT_FAILURE);
648 }
649 }
650 }
651
652 free(iovec);
653}
654
655/* Set "unsigned long long" socket option and check that it's indeed set */
656void setsockopt_ull_check(int fd, int level, int optname,
657 unsigned long long val, char const *errmsg)
658{
659 unsigned long long chkval;
660 socklen_t chklen;
661 int err;
662
663 err = setsockopt(fd, level, optname, &val, sizeof(val));
664 if (err) {
665 fprintf(stderr, "setsockopt err: %s (%d)\n",
666 strerror(errno), errno);
667 goto fail;
668 }
669
670 chkval = ~val; /* just make storage != val */
671 chklen = sizeof(chkval);
672
673 err = getsockopt(fd, level, optname, &chkval, &chklen);
674 if (err) {
675 fprintf(stderr, "getsockopt err: %s (%d)\n",
676 strerror(errno), errno);
677 goto fail;
678 }
679
680 if (chklen != sizeof(chkval)) {
681 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
682 chklen);
683 goto fail;
684 }
685
686 if (chkval != val) {
687 fprintf(stderr, "value mismatch: set %llu got %llu\n", val,
688 chkval);
689 goto fail;
690 }
691 return;
692fail:
693 fprintf(stderr, "%s val %llu\n", errmsg, val);
694 exit(EXIT_FAILURE);
695;
696}
697
698/* Set "int" socket option and check that it's indeed set */
699void setsockopt_int_check(int fd, int level, int optname, int val,
700 char const *errmsg)
701{
702 int chkval;
703 socklen_t chklen;
704 int err;
705
706 err = setsockopt(fd, level, optname, &val, sizeof(val));
707 if (err) {
708 fprintf(stderr, "setsockopt err: %s (%d)\n",
709 strerror(errno), errno);
710 goto fail;
711 }
712
713 chkval = ~val; /* just make storage != val */
714 chklen = sizeof(chkval);
715
716 err = getsockopt(fd, level, optname, &chkval, &chklen);
717 if (err) {
718 fprintf(stderr, "getsockopt err: %s (%d)\n",
719 strerror(errno), errno);
720 goto fail;
721 }
722
723 if (chklen != sizeof(chkval)) {
724 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
725 chklen);
726 goto fail;
727 }
728
729 if (chkval != val) {
730 fprintf(stderr, "value mismatch: set %d got %d\n", val, chkval);
731 goto fail;
732 }
733 return;
734fail:
735 fprintf(stderr, "%s val %d\n", errmsg, val);
736 exit(EXIT_FAILURE);
737}
738
739static void mem_invert(unsigned char *mem, size_t size)
740{
741 size_t i;
742
743 for (i = 0; i < size; i++)
744 mem[i] = ~mem[i];
745}
746
747/* Set "timeval" socket option and check that it's indeed set */
748void setsockopt_timeval_check(int fd, int level, int optname,
749 struct timeval val, char const *errmsg)
750{
751 struct timeval chkval;
752 socklen_t chklen;
753 int err;
754
755 err = setsockopt(fd, level, optname, &val, sizeof(val));
756 if (err) {
757 fprintf(stderr, "setsockopt err: %s (%d)\n",
758 strerror(errno), errno);
759 goto fail;
760 }
761
762 /* just make storage != val */
763 chkval = val;
764 mem_invert((unsigned char *)&chkval, sizeof(chkval));
765 chklen = sizeof(chkval);
766
767 err = getsockopt(fd, level, optname, &chkval, &chklen);
768 if (err) {
769 fprintf(stderr, "getsockopt err: %s (%d)\n",
770 strerror(errno), errno);
771 goto fail;
772 }
773
774 if (chklen != sizeof(chkval)) {
775 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
776 chklen);
777 goto fail;
778 }
779
780 if (memcmp(&chkval, &val, sizeof(val)) != 0) {
781 fprintf(stderr, "value mismatch: set %ld:%ld got %ld:%ld\n",
782 val.tv_sec, val.tv_usec, chkval.tv_sec, chkval.tv_usec);
783 goto fail;
784 }
785 return;
786fail:
787 fprintf(stderr, "%s val %ld:%ld\n", errmsg, val.tv_sec, val.tv_usec);
788 exit(EXIT_FAILURE);
789}
790
791void enable_so_zerocopy_check(int fd)
792{
793 setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1,
794 "setsockopt SO_ZEROCOPY");
795}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * vsock test utilities
4 *
5 * Copyright (C) 2017 Red Hat, Inc.
6 *
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
9
10#include <errno.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <signal.h>
15#include <unistd.h>
16#include <assert.h>
17#include <sys/epoll.h>
18
19#include "timeout.h"
20#include "control.h"
21#include "util.h"
22
23/* Install signal handlers */
24void init_signals(void)
25{
26 struct sigaction act = {
27 .sa_handler = sigalrm,
28 };
29
30 sigaction(SIGALRM, &act, NULL);
31 signal(SIGPIPE, SIG_IGN);
32}
33
34/* Parse a CID in string representation */
35unsigned int parse_cid(const char *str)
36{
37 char *endptr = NULL;
38 unsigned long n;
39
40 errno = 0;
41 n = strtoul(str, &endptr, 10);
42 if (errno || *endptr != '\0') {
43 fprintf(stderr, "malformed CID \"%s\"\n", str);
44 exit(EXIT_FAILURE);
45 }
46 return n;
47}
48
49/* Wait for the remote to close the connection */
50void vsock_wait_remote_close(int fd)
51{
52 struct epoll_event ev;
53 int epollfd, nfds;
54
55 epollfd = epoll_create1(0);
56 if (epollfd == -1) {
57 perror("epoll_create1");
58 exit(EXIT_FAILURE);
59 }
60
61 ev.events = EPOLLRDHUP | EPOLLHUP;
62 ev.data.fd = fd;
63 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
64 perror("epoll_ctl");
65 exit(EXIT_FAILURE);
66 }
67
68 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
69 if (nfds == -1) {
70 perror("epoll_wait");
71 exit(EXIT_FAILURE);
72 }
73
74 if (nfds == 0) {
75 fprintf(stderr, "epoll_wait timed out\n");
76 exit(EXIT_FAILURE);
77 }
78
79 assert(nfds == 1);
80 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
81 assert(ev.data.fd == fd);
82
83 close(epollfd);
84}
85
86/* Connect to <cid, port> and return the file descriptor. */
87static int vsock_connect(unsigned int cid, unsigned int port, int type)
88{
89 union {
90 struct sockaddr sa;
91 struct sockaddr_vm svm;
92 } addr = {
93 .svm = {
94 .svm_family = AF_VSOCK,
95 .svm_port = port,
96 .svm_cid = cid,
97 },
98 };
99 int ret;
100 int fd;
101
102 control_expectln("LISTENING");
103
104 fd = socket(AF_VSOCK, type, 0);
105
106 timeout_begin(TIMEOUT);
107 do {
108 ret = connect(fd, &addr.sa, sizeof(addr.svm));
109 timeout_check("connect");
110 } while (ret < 0 && errno == EINTR);
111 timeout_end();
112
113 if (ret < 0) {
114 int old_errno = errno;
115
116 close(fd);
117 fd = -1;
118 errno = old_errno;
119 }
120 return fd;
121}
122
123int vsock_stream_connect(unsigned int cid, unsigned int port)
124{
125 return vsock_connect(cid, port, SOCK_STREAM);
126}
127
128int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
129{
130 return vsock_connect(cid, port, SOCK_SEQPACKET);
131}
132
133/* Listen on <cid, port> and return the first incoming connection. The remote
134 * address is stored to clientaddrp. clientaddrp may be NULL.
135 */
136static int vsock_accept(unsigned int cid, unsigned int port,
137 struct sockaddr_vm *clientaddrp, int type)
138{
139 union {
140 struct sockaddr sa;
141 struct sockaddr_vm svm;
142 } addr = {
143 .svm = {
144 .svm_family = AF_VSOCK,
145 .svm_port = port,
146 .svm_cid = cid,
147 },
148 };
149 union {
150 struct sockaddr sa;
151 struct sockaddr_vm svm;
152 } clientaddr;
153 socklen_t clientaddr_len = sizeof(clientaddr.svm);
154 int fd;
155 int client_fd;
156 int old_errno;
157
158 fd = socket(AF_VSOCK, type, 0);
159
160 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
161 perror("bind");
162 exit(EXIT_FAILURE);
163 }
164
165 if (listen(fd, 1) < 0) {
166 perror("listen");
167 exit(EXIT_FAILURE);
168 }
169
170 control_writeln("LISTENING");
171
172 timeout_begin(TIMEOUT);
173 do {
174 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
175 timeout_check("accept");
176 } while (client_fd < 0 && errno == EINTR);
177 timeout_end();
178
179 old_errno = errno;
180 close(fd);
181 errno = old_errno;
182
183 if (client_fd < 0)
184 return client_fd;
185
186 if (clientaddr_len != sizeof(clientaddr.svm)) {
187 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
188 (size_t)clientaddr_len);
189 exit(EXIT_FAILURE);
190 }
191 if (clientaddr.sa.sa_family != AF_VSOCK) {
192 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
193 clientaddr.sa.sa_family);
194 exit(EXIT_FAILURE);
195 }
196
197 if (clientaddrp)
198 *clientaddrp = clientaddr.svm;
199 return client_fd;
200}
201
202int vsock_stream_accept(unsigned int cid, unsigned int port,
203 struct sockaddr_vm *clientaddrp)
204{
205 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
206}
207
208int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
209 struct sockaddr_vm *clientaddrp)
210{
211 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
212}
213
214/* Transmit one byte and check the return value.
215 *
216 * expected_ret:
217 * <0 Negative errno (for testing errors)
218 * 0 End-of-file
219 * 1 Success
220 */
221void send_byte(int fd, int expected_ret, int flags)
222{
223 const uint8_t byte = 'A';
224 ssize_t nwritten;
225
226 timeout_begin(TIMEOUT);
227 do {
228 nwritten = send(fd, &byte, sizeof(byte), flags);
229 timeout_check("write");
230 } while (nwritten < 0 && errno == EINTR);
231 timeout_end();
232
233 if (expected_ret < 0) {
234 if (nwritten != -1) {
235 fprintf(stderr, "bogus send(2) return value %zd\n",
236 nwritten);
237 exit(EXIT_FAILURE);
238 }
239 if (errno != -expected_ret) {
240 perror("write");
241 exit(EXIT_FAILURE);
242 }
243 return;
244 }
245
246 if (nwritten < 0) {
247 perror("write");
248 exit(EXIT_FAILURE);
249 }
250 if (nwritten == 0) {
251 if (expected_ret == 0)
252 return;
253
254 fprintf(stderr, "unexpected EOF while sending byte\n");
255 exit(EXIT_FAILURE);
256 }
257 if (nwritten != sizeof(byte)) {
258 fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
259 exit(EXIT_FAILURE);
260 }
261}
262
263/* Receive one byte and check the return value.
264 *
265 * expected_ret:
266 * <0 Negative errno (for testing errors)
267 * 0 End-of-file
268 * 1 Success
269 */
270void recv_byte(int fd, int expected_ret, int flags)
271{
272 uint8_t byte;
273 ssize_t nread;
274
275 timeout_begin(TIMEOUT);
276 do {
277 nread = recv(fd, &byte, sizeof(byte), flags);
278 timeout_check("read");
279 } while (nread < 0 && errno == EINTR);
280 timeout_end();
281
282 if (expected_ret < 0) {
283 if (nread != -1) {
284 fprintf(stderr, "bogus recv(2) return value %zd\n",
285 nread);
286 exit(EXIT_FAILURE);
287 }
288 if (errno != -expected_ret) {
289 perror("read");
290 exit(EXIT_FAILURE);
291 }
292 return;
293 }
294
295 if (nread < 0) {
296 perror("read");
297 exit(EXIT_FAILURE);
298 }
299 if (nread == 0) {
300 if (expected_ret == 0)
301 return;
302
303 fprintf(stderr, "unexpected EOF while receiving byte\n");
304 exit(EXIT_FAILURE);
305 }
306 if (nread != sizeof(byte)) {
307 fprintf(stderr, "bogus recv(2) return value %zd\n", nread);
308 exit(EXIT_FAILURE);
309 }
310 if (byte != 'A') {
311 fprintf(stderr, "unexpected byte read %c\n", byte);
312 exit(EXIT_FAILURE);
313 }
314}
315
316/* Run test cases. The program terminates if a failure occurs. */
317void run_tests(const struct test_case *test_cases,
318 const struct test_opts *opts)
319{
320 int i;
321
322 for (i = 0; test_cases[i].name; i++) {
323 void (*run)(const struct test_opts *opts);
324 char *line;
325
326 printf("%d - %s...", i, test_cases[i].name);
327 fflush(stdout);
328
329 /* Full barrier before executing the next test. This
330 * ensures that client and server are executing the
331 * same test case. In particular, it means whoever is
332 * faster will not see the peer still executing the
333 * last test. This is important because port numbers
334 * can be used by multiple test cases.
335 */
336 if (test_cases[i].skip)
337 control_writeln("SKIP");
338 else
339 control_writeln("NEXT");
340
341 line = control_readln();
342 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
343
344 printf("skipped\n");
345
346 free(line);
347 continue;
348 }
349
350 control_cmpln(line, "NEXT", true);
351 free(line);
352
353 if (opts->mode == TEST_MODE_CLIENT)
354 run = test_cases[i].run_client;
355 else
356 run = test_cases[i].run_server;
357
358 if (run)
359 run(opts);
360
361 printf("ok\n");
362 }
363}
364
365void list_tests(const struct test_case *test_cases)
366{
367 int i;
368
369 printf("ID\tTest name\n");
370
371 for (i = 0; test_cases[i].name; i++)
372 printf("%d\t%s\n", i, test_cases[i].name);
373
374 exit(EXIT_FAILURE);
375}
376
377void skip_test(struct test_case *test_cases, size_t test_cases_len,
378 const char *test_id_str)
379{
380 unsigned long test_id;
381 char *endptr = NULL;
382
383 errno = 0;
384 test_id = strtoul(test_id_str, &endptr, 10);
385 if (errno || *endptr != '\0') {
386 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
387 exit(EXIT_FAILURE);
388 }
389
390 if (test_id >= test_cases_len) {
391 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
392 test_id, test_cases_len - 1);
393 exit(EXIT_FAILURE);
394 }
395
396 test_cases[test_id].skip = true;
397}