1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2017 Red Hat, Inc.
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
18 #include <sys/epoll.h>
25 /* Install signal handlers */
26 void init_signals(void)
28 struct sigaction act = {
29 .sa_handler = sigalrm,
32 sigaction(SIGALRM, &act, NULL);
33 signal(SIGPIPE, SIG_IGN);
36 /* Parse a CID in string representation */
37 unsigned int parse_cid(const char *str)
43 n = strtoul(str, &endptr, 10);
44 if (errno || *endptr != '\0') {
45 fprintf(stderr, "malformed CID \"%s\"\n", str);
51 /* Wait for the remote to close the connection */
52 void vsock_wait_remote_close(int fd)
54 struct epoll_event ev;
57 epollfd = epoll_create1(0);
59 perror("epoll_create1");
63 ev.events = EPOLLRDHUP | EPOLLHUP;
65 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
70 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
77 fprintf(stderr, "epoll_wait timed out\n");
82 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
83 assert(ev.data.fd == fd);
88 /* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
89 int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
91 struct sockaddr_vm sa_client = {
92 .svm_family = AF_VSOCK,
93 .svm_cid = VMADDR_CID_ANY,
94 .svm_port = bind_port,
96 struct sockaddr_vm sa_server = {
97 .svm_family = AF_VSOCK,
104 client_fd = socket(AF_VSOCK, type, 0);
110 if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
115 timeout_begin(TIMEOUT);
117 ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
118 timeout_check("connect");
119 } while (ret < 0 && errno == EINTR);
130 /* Connect to <cid, port> and return the file descriptor. */
131 static int vsock_connect(unsigned int cid, unsigned int port, int type)
135 struct sockaddr_vm svm;
138 .svm_family = AF_VSOCK,
146 control_expectln("LISTENING");
148 fd = socket(AF_VSOCK, type, 0);
154 timeout_begin(TIMEOUT);
156 ret = connect(fd, &addr.sa, sizeof(addr.svm));
157 timeout_check("connect");
158 } while (ret < 0 && errno == EINTR);
162 int old_errno = errno;
171 int vsock_stream_connect(unsigned int cid, unsigned int port)
173 return vsock_connect(cid, port, SOCK_STREAM);
176 int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
178 return vsock_connect(cid, port, SOCK_SEQPACKET);
181 /* Listen on <cid, port> and return the file descriptor. */
182 static int vsock_listen(unsigned int cid, unsigned int port, int type)
186 struct sockaddr_vm svm;
189 .svm_family = AF_VSOCK,
196 fd = socket(AF_VSOCK, type, 0);
202 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
207 if (listen(fd, 1) < 0) {
215 /* Listen on <cid, port> and return the first incoming connection. The remote
216 * address is stored to clientaddrp. clientaddrp may be NULL.
218 static int vsock_accept(unsigned int cid, unsigned int port,
219 struct sockaddr_vm *clientaddrp, int type)
223 struct sockaddr_vm svm;
225 socklen_t clientaddr_len = sizeof(clientaddr.svm);
226 int fd, client_fd, old_errno;
228 fd = vsock_listen(cid, port, type);
230 control_writeln("LISTENING");
232 timeout_begin(TIMEOUT);
234 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
235 timeout_check("accept");
236 } while (client_fd < 0 && errno == EINTR);
246 if (clientaddr_len != sizeof(clientaddr.svm)) {
247 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
248 (size_t)clientaddr_len);
251 if (clientaddr.sa.sa_family != AF_VSOCK) {
252 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
253 clientaddr.sa.sa_family);
258 *clientaddrp = clientaddr.svm;
262 int vsock_stream_accept(unsigned int cid, unsigned int port,
263 struct sockaddr_vm *clientaddrp)
265 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
268 int vsock_stream_listen(unsigned int cid, unsigned int port)
270 return vsock_listen(cid, port, SOCK_STREAM);
273 int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
274 struct sockaddr_vm *clientaddrp)
276 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
279 /* Transmit bytes from a buffer and check the return value.
282 * <0 Negative errno (for testing errors)
284 * >0 Success (bytes successfully written)
286 void send_buf(int fd, const void *buf, size_t len, int flags,
287 ssize_t expected_ret)
289 ssize_t nwritten = 0;
292 timeout_begin(TIMEOUT);
294 ret = send(fd, buf + nwritten, len - nwritten, flags);
295 timeout_check("send");
297 if (ret == 0 || (ret < 0 && errno != EINTR))
301 } while (nwritten < len);
304 if (expected_ret < 0) {
306 fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
310 if (errno != -expected_ret) {
322 if (nwritten != expected_ret) {
324 fprintf(stderr, "unexpected EOF while sending bytes\n");
326 fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
327 nwritten, expected_ret);
332 /* Receive bytes in a buffer and check the return value.
335 * <0 Negative errno (for testing errors)
337 * >0 Success (bytes successfully read)
339 void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
344 timeout_begin(TIMEOUT);
346 ret = recv(fd, buf + nread, len - nread, flags);
347 timeout_check("recv");
349 if (ret == 0 || (ret < 0 && errno != EINTR))
353 } while (nread < len);
356 if (expected_ret < 0) {
358 fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
362 if (errno != -expected_ret) {
374 if (nread != expected_ret) {
376 fprintf(stderr, "unexpected EOF while receiving bytes\n");
378 fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
379 nread, expected_ret);
384 /* Transmit one byte and check the return value.
387 * <0 Negative errno (for testing errors)
391 void send_byte(int fd, int expected_ret, int flags)
393 const uint8_t byte = 'A';
395 send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
398 /* Receive one byte and check the return value.
401 * <0 Negative errno (for testing errors)
405 void recv_byte(int fd, int expected_ret, int flags)
409 recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
412 fprintf(stderr, "unexpected byte read %c\n", byte);
417 /* Run test cases. The program terminates if a failure occurs. */
418 void run_tests(const struct test_case *test_cases,
419 const struct test_opts *opts)
423 for (i = 0; test_cases[i].name; i++) {
424 void (*run)(const struct test_opts *opts);
427 printf("%d - %s...", i, test_cases[i].name);
430 /* Full barrier before executing the next test. This
431 * ensures that client and server are executing the
432 * same test case. In particular, it means whoever is
433 * faster will not see the peer still executing the
434 * last test. This is important because port numbers
435 * can be used by multiple test cases.
437 if (test_cases[i].skip)
438 control_writeln("SKIP");
440 control_writeln("NEXT");
442 line = control_readln();
443 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
451 control_cmpln(line, "NEXT", true);
454 if (opts->mode == TEST_MODE_CLIENT)
455 run = test_cases[i].run_client;
457 run = test_cases[i].run_server;
466 void list_tests(const struct test_case *test_cases)
470 printf("ID\tTest name\n");
472 for (i = 0; test_cases[i].name; i++)
473 printf("%d\t%s\n", i, test_cases[i].name);
478 void skip_test(struct test_case *test_cases, size_t test_cases_len,
479 const char *test_id_str)
481 unsigned long test_id;
485 test_id = strtoul(test_id_str, &endptr, 10);
486 if (errno || *endptr != '\0') {
487 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
491 if (test_id >= test_cases_len) {
492 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
493 test_id, test_cases_len - 1);
497 test_cases[test_id].skip = true;
500 unsigned long hash_djb2(const void *data, size_t len)
502 unsigned long hash = 5381;
506 hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
513 size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
518 for (bytes = 0, i = 0; i < iovnum; i++)
519 bytes += iov[i].iov_len;
524 unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
532 iov_bytes = iovec_bytes(iov, iovnum);
534 tmp = malloc(iov_bytes);
540 for (offs = 0, i = 0; i < iovnum; i++) {
541 memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
542 offs += iov[i].iov_len;
545 hash = hash_djb2(tmp, iov_bytes);
551 /* Allocates and returns new 'struct iovec *' according pattern
552 * in the 'test_iovec'. For each element in the 'test_iovec' it
553 * allocates new element in the resulting 'iovec'. 'iov_len'
554 * of the new element is copied from 'test_iovec'. 'iov_base' is
555 * allocated depending on the 'iov_base' of 'test_iovec':
557 * 'iov_base' == NULL -> valid buf: mmap('iov_len').
559 * 'iov_base' == MAP_FAILED -> invalid buf:
560 * mmap('iov_len'), then munmap('iov_len').
561 * 'iov_base' still contains result of
564 * 'iov_base' == number -> unaligned valid buf:
565 * mmap('iov_len') + number.
567 * 'iovnum' is number of elements in 'test_iovec'.
569 * Returns new 'iovec' or calls 'exit()' on error.
571 struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
576 iovec = malloc(sizeof(*iovec) * iovnum);
582 for (i = 0; i < iovnum; i++) {
583 iovec[i].iov_len = test_iovec[i].iov_len;
585 iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
586 PROT_READ | PROT_WRITE,
587 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
589 if (iovec[i].iov_base == MAP_FAILED) {
594 if (test_iovec[i].iov_base != MAP_FAILED)
595 iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
598 /* Unmap "invalid" elements. */
599 for (i = 0; i < iovnum; i++) {
600 if (test_iovec[i].iov_base == MAP_FAILED) {
601 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
608 for (i = 0; i < iovnum; i++) {
611 if (test_iovec[i].iov_base == MAP_FAILED)
614 for (j = 0; j < iovec[i].iov_len; j++)
615 ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
621 /* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'.
622 * On error calls 'exit()'.
624 void free_test_iovec(const struct iovec *test_iovec,
625 struct iovec *iovec, int iovnum)
629 for (i = 0; i < iovnum; i++) {
630 if (test_iovec[i].iov_base != MAP_FAILED) {
631 if (test_iovec[i].iov_base)
632 iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
634 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {