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 /* Tests can either run as the client or the server */ 37 enum test_mode { 38 TEST_MODE_UNSET, 39 TEST_MODE_CLIENT, 40 TEST_MODE_SERVER 41 }; 42 43 #define DEFAULT_PEER_PORT 1234 44 45 /* Test runner options */ 46 struct test_opts { 47 enum test_mode mode; 48 unsigned int peer_cid; 49 unsigned int peer_port; 50 }; 51 52 /* A test case definition. Test functions must print failures to stderr and 53 * terminate with exit(EXIT_FAILURE). 54 */ 55 struct test_case { 56 const char *name; /* human-readable name */ 57 58 /* Called when test mode is TEST_MODE_CLIENT */ 59 void (*run_client)(const struct test_opts *opts); 60 61 /* Called when test mode is TEST_MODE_SERVER */ 62 void (*run_server)(const struct test_opts *opts); 63 64 bool skip; 65 }; 66 67 void init_signals(void); 68 unsigned int parse_cid(const char *str); 69 unsigned int parse_port(const char *str); 70 int vsock_connect_fd(int fd, unsigned int cid, unsigned int port); 71 int vsock_connect(unsigned int cid, unsigned int port, int type); 72 int vsock_accept(unsigned int cid, unsigned int port, 73 struct sockaddr_vm *clientaddrp, int type); 74 int vsock_stream_connect(unsigned int cid, unsigned int port); 75 int vsock_bind_try(unsigned int cid, unsigned int port, int type); 76 int vsock_bind(unsigned int cid, unsigned int port, int type); 77 int vsock_bind_connect(unsigned int cid, unsigned int port, 78 unsigned int bind_port, int type); 79 int vsock_seqpacket_connect(unsigned int cid, unsigned int port); 80 int vsock_stream_accept(unsigned int cid, unsigned int port, 81 struct sockaddr_vm *clientaddrp); 82 int vsock_stream_listen(unsigned int cid, unsigned int port); 83 int vsock_seqpacket_accept(unsigned int cid, unsigned int port, 84 struct sockaddr_vm *clientaddrp); 85 void vsock_wait_remote_close(int fd); 86 bool vsock_wait_sent(int fd); 87 void send_buf(int fd, const void *buf, size_t len, int flags, 88 ssize_t expected_ret); 89 void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret); 90 void send_byte(int fd, int expected_ret, int flags); 91 void recv_byte(int fd, int expected_ret, int flags); 92 void run_tests(const struct test_case *test_cases, 93 const struct test_opts *opts); 94 void list_tests(const struct test_case *test_cases); 95 void skip_test(struct test_case *test_cases, size_t test_cases_len, 96 const char *test_id_str); 97 void pick_test(struct test_case *test_cases, size_t test_cases_len, 98 const char *test_id_str); 99 unsigned long hash_djb2(const void *data, size_t len); 100 size_t iovec_bytes(const struct iovec *iov, size_t iovnum); 101 unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum); 102 struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum); 103 void free_test_iovec(const struct iovec *test_iovec, 104 struct iovec *iovec, int iovnum); 105 void setsockopt_ull_check(int fd, int level, int optname, 106 unsigned long long val, char const *errmsg); 107 void setsockopt_int_check(int fd, int level, int optname, int val, 108 char const *errmsg); 109 void setsockopt_timeval_check(int fd, int level, int optname, 110 struct timeval val, char const *errmsg); 111 void enable_so_zerocopy_check(int fd); 112 void enable_so_linger(int fd, int timeout); 113 int get_transports(void); 114 #endif /* UTIL_H */ 115