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