1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef UTIL_H 3 #define UTIL_H 4 5 #include <sys/socket.h> 6 #include <linux/bitops.h> 7 #include <linux/kernel.h> 8 #include <linux/vm_sockets.h> 9 10 /* All known vsock transports, see callers of vsock_core_register() */ 11 #define KNOWN_TRANSPORTS(x) \ 12 x(LOOPBACK, "loopback") \ 13 x(VIRTIO, "virtio") \ 14 x(VHOST, "vhost") \ 15 x(VMCI, "vmci") \ 16 x(HYPERV, "hvs") 17 18 enum transport { 19 TRANSPORT_COUNTER_BASE = __COUNTER__ + 1, 20 #define x(name, symbol) \ 21 TRANSPORT_##name = BIT(__COUNTER__ - TRANSPORT_COUNTER_BASE), 22 KNOWN_TRANSPORTS(x) 23 TRANSPORT_NUM = __COUNTER__ - TRANSPORT_COUNTER_BASE, 24 #undef x 25 }; 26 27 static const char * const transport_ksyms[] = { 28 #define x(name, symbol) "d " symbol "_transport", 29 KNOWN_TRANSPORTS(x) 30 #undef x 31 }; 32 33 static_assert(ARRAY_SIZE(transport_ksyms) == TRANSPORT_NUM); 34 static_assert(BITS_PER_TYPE(int) >= TRANSPORT_NUM); 35 36 #define TRANSPORTS_G2H (TRANSPORT_VIRTIO | TRANSPORT_VMCI | TRANSPORT_HYPERV) 37 #define TRANSPORTS_H2G (TRANSPORT_VHOST | TRANSPORT_VMCI) 38 #define TRANSPORTS_LOCAL (TRANSPORT_LOOPBACK) 39 40 /* Tests can either run as the client or the server */ 41 enum test_mode { 42 TEST_MODE_UNSET, 43 TEST_MODE_CLIENT, 44 TEST_MODE_SERVER 45 }; 46 47 #define DEFAULT_PEER_PORT 1234 48 49 /* Test runner options */ 50 struct test_opts { 51 enum test_mode mode; 52 unsigned int peer_cid; 53 unsigned int peer_port; 54 }; 55 56 /* A test case definition. Test functions must print failures to stderr and 57 * terminate with exit(EXIT_FAILURE). 58 */ 59 struct test_case { 60 const char *name; /* human-readable name */ 61 62 /* Called when test mode is TEST_MODE_CLIENT */ 63 void (*run_client)(const struct test_opts *opts); 64 65 /* Called when test mode is TEST_MODE_SERVER */ 66 void (*run_server)(const struct test_opts *opts); 67 68 bool skip; 69 }; 70 71 void init_signals(void); 72 unsigned int parse_cid(const char *str); 73 unsigned int parse_port(const char *str); 74 int vsock_connect_fd(int fd, unsigned int cid, unsigned int port); 75 int vsock_connect(unsigned int cid, unsigned int port, int type); 76 int vsock_accept(unsigned int cid, unsigned int port, 77 struct sockaddr_vm *clientaddrp, int type); 78 int vsock_stream_connect(unsigned int cid, unsigned int port); 79 int vsock_bind_try(unsigned int cid, unsigned int port, int type); 80 int vsock_bind(unsigned int cid, unsigned int port, int type); 81 int vsock_bind_connect(unsigned int cid, unsigned int port, 82 unsigned int bind_port, int type); 83 int vsock_seqpacket_connect(unsigned int cid, unsigned int port); 84 int vsock_stream_accept(unsigned int cid, unsigned int port, 85 struct sockaddr_vm *clientaddrp); 86 int vsock_stream_listen(unsigned int cid, unsigned int port); 87 int vsock_seqpacket_accept(unsigned int cid, unsigned int port, 88 struct sockaddr_vm *clientaddrp); 89 void vsock_wait_remote_close(int fd); 90 bool vsock_ioctl_int(int fd, unsigned long op, int expected); 91 bool vsock_wait_sent(int fd); 92 void send_buf(int fd, const void *buf, size_t len, int flags, 93 ssize_t expected_ret); 94 void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret); 95 void send_byte(int fd, int expected_ret, int flags); 96 void recv_byte(int fd, int expected_ret, int flags); 97 void run_tests(const struct test_case *test_cases, 98 const struct test_opts *opts); 99 void list_tests(const struct test_case *test_cases); 100 void skip_test(struct test_case *test_cases, size_t test_cases_len, 101 const char *test_id_str); 102 void pick_test(struct test_case *test_cases, size_t test_cases_len, 103 const char *test_id_str); 104 unsigned long hash_djb2(const void *data, size_t len); 105 size_t iovec_bytes(const struct iovec *iov, size_t iovnum); 106 unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum); 107 struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum); 108 void free_test_iovec(const struct iovec *test_iovec, 109 struct iovec *iovec, int iovnum); 110 void setsockopt_ull_check(int fd, int level, int optname, 111 unsigned long long val, char const *errmsg); 112 void setsockopt_int_check(int fd, int level, int optname, int val, 113 char const *errmsg); 114 void setsockopt_timeval_check(int fd, int level, int optname, 115 struct timeval val, char const *errmsg); 116 void enable_so_zerocopy_check(int fd); 117 void enable_so_linger(int fd, int timeout); 118 int get_transports(void); 119 #endif /* UTIL_H */ 120