2019-12-18 19:07:00 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
|
|
#ifndef UTIL_H
|
|
|
|
#define UTIL_H
|
|
|
|
|
2019-12-18 19:07:01 +01:00
|
|
|
#include <sys/socket.h>
|
vsock/test: Introduce get_transports()
Return a bitmap of registered vsock transports. As guesstimated by grepping
/proc/kallsyms (CONFIG_KALLSYMS=y) for known symbols of type `struct
vsock_transport`, or `struct virtio_transport` in case the vsock_transport
is embedded within.
Note that the way `enum transport` and `transport_ksyms[]` are defined
triggers checkpatch.pl:
util.h:11: ERROR: Macros with complex values should be enclosed in parentheses
util.h:20: ERROR: Macros with complex values should be enclosed in parentheses
util.h:20: WARNING: Argument 'symbol' is not used in function-like macro
util.h:28: WARNING: Argument 'name' is not used in function-like macro
While commit 15d4734c7a58 ("checkpatch: qualify do-while-0 advice")
suggests it is known that the ERRORs heuristics are insufficient, I can not
find many other places where preprocessor is used in this
checkpatch-unhappy fashion. Notable exception being bcachefs, e.g.
fs/bcachefs/alloc_background_format.h. WARNINGs regarding unused macro
arguments seem more common, e.g. __ASM_SEL in arch/x86/include/asm/asm.h.
In other words, this might be unnecessarily complex. The same can be
achieved by just telling human to keep the order:
enum transport {
TRANSPORT_LOOPBACK = BIT(0),
TRANSPORT_VIRTIO = BIT(1),
TRANSPORT_VHOST = BIT(2),
TRANSPORT_VMCI = BIT(3),
TRANSPORT_HYPERV = BIT(4),
TRANSPORT_NUM = 5,
};
#define KSYM_ENTRY(sym) "d " sym "_transport"
/* Keep `enum transport` order */
static const char * const transport_ksyms[] = {
KSYM_ENTRY("loopback"),
KSYM_ENTRY("virtio"),
KSYM_ENTRY("vhost"),
KSYM_ENTRY("vmci"),
KSYM_ENTRY("vhs"),
};
Suggested-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Tested-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20250611-vsock-test-inc-cov-v3-2-5834060d9c20@rbox.co
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-06-11 21:56:51 +02:00
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/kernel.h>
|
2019-12-18 19:07:01 +01:00
|
|
|
#include <linux/vm_sockets.h>
|
|
|
|
|
vsock/test: Introduce get_transports()
Return a bitmap of registered vsock transports. As guesstimated by grepping
/proc/kallsyms (CONFIG_KALLSYMS=y) for known symbols of type `struct
vsock_transport`, or `struct virtio_transport` in case the vsock_transport
is embedded within.
Note that the way `enum transport` and `transport_ksyms[]` are defined
triggers checkpatch.pl:
util.h:11: ERROR: Macros with complex values should be enclosed in parentheses
util.h:20: ERROR: Macros with complex values should be enclosed in parentheses
util.h:20: WARNING: Argument 'symbol' is not used in function-like macro
util.h:28: WARNING: Argument 'name' is not used in function-like macro
While commit 15d4734c7a58 ("checkpatch: qualify do-while-0 advice")
suggests it is known that the ERRORs heuristics are insufficient, I can not
find many other places where preprocessor is used in this
checkpatch-unhappy fashion. Notable exception being bcachefs, e.g.
fs/bcachefs/alloc_background_format.h. WARNINGs regarding unused macro
arguments seem more common, e.g. __ASM_SEL in arch/x86/include/asm/asm.h.
In other words, this might be unnecessarily complex. The same can be
achieved by just telling human to keep the order:
enum transport {
TRANSPORT_LOOPBACK = BIT(0),
TRANSPORT_VIRTIO = BIT(1),
TRANSPORT_VHOST = BIT(2),
TRANSPORT_VMCI = BIT(3),
TRANSPORT_HYPERV = BIT(4),
TRANSPORT_NUM = 5,
};
#define KSYM_ENTRY(sym) "d " sym "_transport"
/* Keep `enum transport` order */
static const char * const transport_ksyms[] = {
KSYM_ENTRY("loopback"),
KSYM_ENTRY("virtio"),
KSYM_ENTRY("vhost"),
KSYM_ENTRY("vmci"),
KSYM_ENTRY("vhs"),
};
Suggested-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Tested-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20250611-vsock-test-inc-cov-v3-2-5834060d9c20@rbox.co
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-06-11 21:56:51 +02:00
|
|
|
/* All known vsock transports, see callers of vsock_core_register() */
|
|
|
|
#define KNOWN_TRANSPORTS(x) \
|
|
|
|
x(LOOPBACK, "loopback") \
|
|
|
|
x(VIRTIO, "virtio") \
|
|
|
|
x(VHOST, "vhost") \
|
|
|
|
x(VMCI, "vmci") \
|
|
|
|
x(HYPERV, "hvs")
|
|
|
|
|
|
|
|
enum transport {
|
|
|
|
TRANSPORT_COUNTER_BASE = __COUNTER__ + 1,
|
|
|
|
#define x(name, symbol) \
|
|
|
|
TRANSPORT_##name = BIT(__COUNTER__ - TRANSPORT_COUNTER_BASE),
|
|
|
|
KNOWN_TRANSPORTS(x)
|
|
|
|
TRANSPORT_NUM = __COUNTER__ - TRANSPORT_COUNTER_BASE,
|
|
|
|
#undef x
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const transport_ksyms[] = {
|
|
|
|
#define x(name, symbol) "d " symbol "_transport",
|
|
|
|
KNOWN_TRANSPORTS(x)
|
|
|
|
#undef x
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(ARRAY_SIZE(transport_ksyms) == TRANSPORT_NUM);
|
|
|
|
static_assert(BITS_PER_TYPE(int) >= TRANSPORT_NUM);
|
|
|
|
|
2025-06-30 18:33:03 +02:00
|
|
|
#define TRANSPORTS_G2H (TRANSPORT_VIRTIO | TRANSPORT_VMCI | TRANSPORT_HYPERV)
|
|
|
|
#define TRANSPORTS_H2G (TRANSPORT_VHOST | TRANSPORT_VMCI)
|
|
|
|
#define TRANSPORTS_LOCAL (TRANSPORT_LOOPBACK)
|
|
|
|
|
2019-12-18 19:07:00 +01:00
|
|
|
/* Tests can either run as the client or the server */
|
|
|
|
enum test_mode {
|
|
|
|
TEST_MODE_UNSET,
|
|
|
|
TEST_MODE_CLIENT,
|
|
|
|
TEST_MODE_SERVER
|
|
|
|
};
|
|
|
|
|
2024-01-23 10:27:50 +03:00
|
|
|
#define DEFAULT_PEER_PORT 1234
|
|
|
|
|
2019-12-18 19:07:00 +01:00
|
|
|
/* Test runner options */
|
|
|
|
struct test_opts {
|
|
|
|
enum test_mode mode;
|
|
|
|
unsigned int peer_cid;
|
2024-01-23 10:27:50 +03:00
|
|
|
unsigned int peer_port;
|
2019-12-18 19:07:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A test case definition. Test functions must print failures to stderr and
|
|
|
|
* terminate with exit(EXIT_FAILURE).
|
|
|
|
*/
|
|
|
|
struct test_case {
|
|
|
|
const char *name; /* human-readable name */
|
|
|
|
|
|
|
|
/* Called when test mode is TEST_MODE_CLIENT */
|
|
|
|
void (*run_client)(const struct test_opts *opts);
|
|
|
|
|
|
|
|
/* Called when test mode is TEST_MODE_SERVER */
|
|
|
|
void (*run_server)(const struct test_opts *opts);
|
2019-12-18 19:07:06 +01:00
|
|
|
|
|
|
|
bool skip;
|
2019-12-18 19:07:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void init_signals(void);
|
|
|
|
unsigned int parse_cid(const char *str);
|
2024-01-23 10:27:50 +03:00
|
|
|
unsigned int parse_port(const char *str);
|
2025-01-28 14:15:30 +01:00
|
|
|
int vsock_connect_fd(int fd, unsigned int cid, unsigned int port);
|
2024-07-30 21:43:08 +02:00
|
|
|
int vsock_connect(unsigned int cid, unsigned int port, int type);
|
|
|
|
int vsock_accept(unsigned int cid, unsigned int port,
|
|
|
|
struct sockaddr_vm *clientaddrp, int type);
|
2019-12-18 19:07:01 +01:00
|
|
|
int vsock_stream_connect(unsigned int cid, unsigned int port);
|
2025-06-11 21:56:50 +02:00
|
|
|
int vsock_bind_try(unsigned int cid, unsigned int port, int type);
|
2025-01-28 14:15:29 +01:00
|
|
|
int vsock_bind(unsigned int cid, unsigned int port, int type);
|
2023-11-03 18:55:51 +01:00
|
|
|
int vsock_bind_connect(unsigned int cid, unsigned int port,
|
|
|
|
unsigned int bind_port, int type);
|
2021-06-11 14:14:04 +03:00
|
|
|
int vsock_seqpacket_connect(unsigned int cid, unsigned int port);
|
2019-12-18 19:07:01 +01:00
|
|
|
int vsock_stream_accept(unsigned int cid, unsigned int port,
|
|
|
|
struct sockaddr_vm *clientaddrp);
|
2023-11-03 18:55:51 +01:00
|
|
|
int vsock_stream_listen(unsigned int cid, unsigned int port);
|
2021-06-11 14:14:04 +03:00
|
|
|
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
|
|
|
|
struct sockaddr_vm *clientaddrp);
|
2019-12-18 19:07:05 +01:00
|
|
|
void vsock_wait_remote_close(int fd);
|
2025-07-08 14:36:13 +08:00
|
|
|
bool vsock_ioctl_int(int fd, unsigned long op, int expected);
|
2025-05-22 01:18:23 +02:00
|
|
|
bool vsock_wait_sent(int fd);
|
2023-09-15 14:14:50 +02:00
|
|
|
void send_buf(int fd, const void *buf, size_t len, int flags,
|
|
|
|
ssize_t expected_ret);
|
2023-09-15 14:14:48 +02:00
|
|
|
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);
|
2019-12-18 19:07:03 +01:00
|
|
|
void send_byte(int fd, int expected_ret, int flags);
|
|
|
|
void recv_byte(int fd, int expected_ret, int flags);
|
2019-12-18 19:07:00 +01:00
|
|
|
void run_tests(const struct test_case *test_cases,
|
|
|
|
const struct test_opts *opts);
|
2019-12-18 19:07:06 +01:00
|
|
|
void list_tests(const struct test_case *test_cases);
|
|
|
|
void skip_test(struct test_case *test_cases, size_t test_cases_len,
|
|
|
|
const char *test_id_str);
|
2024-12-19 10:49:29 +01:00
|
|
|
void pick_test(struct test_case *test_cases, size_t test_cases_len,
|
|
|
|
const char *test_id_str);
|
2023-01-10 10:15:15 +00:00
|
|
|
unsigned long hash_djb2(const void *data, size_t len);
|
2023-10-10 22:15:22 +03:00
|
|
|
size_t iovec_bytes(const struct iovec *iov, size_t iovnum);
|
|
|
|
unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum);
|
|
|
|
struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum);
|
|
|
|
void free_test_iovec(const struct iovec *test_iovec,
|
|
|
|
struct iovec *iovec, int iovnum);
|
2024-12-03 09:06:56 -06:00
|
|
|
void setsockopt_ull_check(int fd, int level, int optname,
|
|
|
|
unsigned long long val, char const *errmsg);
|
|
|
|
void setsockopt_int_check(int fd, int level, int optname, int val,
|
|
|
|
char const *errmsg);
|
|
|
|
void setsockopt_timeval_check(int fd, int level, int optname,
|
|
|
|
struct timeval val, char const *errmsg);
|
|
|
|
void enable_so_zerocopy_check(int fd);
|
2025-05-22 01:18:24 +02:00
|
|
|
void enable_so_linger(int fd, int timeout);
|
vsock/test: Introduce get_transports()
Return a bitmap of registered vsock transports. As guesstimated by grepping
/proc/kallsyms (CONFIG_KALLSYMS=y) for known symbols of type `struct
vsock_transport`, or `struct virtio_transport` in case the vsock_transport
is embedded within.
Note that the way `enum transport` and `transport_ksyms[]` are defined
triggers checkpatch.pl:
util.h:11: ERROR: Macros with complex values should be enclosed in parentheses
util.h:20: ERROR: Macros with complex values should be enclosed in parentheses
util.h:20: WARNING: Argument 'symbol' is not used in function-like macro
util.h:28: WARNING: Argument 'name' is not used in function-like macro
While commit 15d4734c7a58 ("checkpatch: qualify do-while-0 advice")
suggests it is known that the ERRORs heuristics are insufficient, I can not
find many other places where preprocessor is used in this
checkpatch-unhappy fashion. Notable exception being bcachefs, e.g.
fs/bcachefs/alloc_background_format.h. WARNINGs regarding unused macro
arguments seem more common, e.g. __ASM_SEL in arch/x86/include/asm/asm.h.
In other words, this might be unnecessarily complex. The same can be
achieved by just telling human to keep the order:
enum transport {
TRANSPORT_LOOPBACK = BIT(0),
TRANSPORT_VIRTIO = BIT(1),
TRANSPORT_VHOST = BIT(2),
TRANSPORT_VMCI = BIT(3),
TRANSPORT_HYPERV = BIT(4),
TRANSPORT_NUM = 5,
};
#define KSYM_ENTRY(sym) "d " sym "_transport"
/* Keep `enum transport` order */
static const char * const transport_ksyms[] = {
KSYM_ENTRY("loopback"),
KSYM_ENTRY("virtio"),
KSYM_ENTRY("vhost"),
KSYM_ENTRY("vmci"),
KSYM_ENTRY("vhs"),
};
Suggested-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Tested-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20250611-vsock-test-inc-cov-v3-2-5834060d9c20@rbox.co
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-06-11 21:56:51 +02:00
|
|
|
int get_transports(void);
|
2019-12-18 19:07:00 +01:00
|
|
|
#endif /* UTIL_H */
|