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 15d4734c7a ("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>
This commit is contained in:
Michal Luczaj
2025-06-11 21:56:51 +02:00
committed by Jakub Kicinski
parent d56a8dbff8
commit 3070c05b7a
2 changed files with 85 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
* Author: Stefan Hajnoczi <stefanha@redhat.com>
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
@@ -23,6 +24,9 @@
#include "control.h"
#include "util.h"
#define KALLSYMS_PATH "/proc/kallsyms"
#define KALLSYMS_LINE_LEN 512
/* Install signal handlers */
void init_signals(void)
{
@@ -854,3 +858,55 @@ void enable_so_linger(int fd, int timeout)
exit(EXIT_FAILURE);
}
}
static int __get_transports(void)
{
char buf[KALLSYMS_LINE_LEN];
const char *ksym;
int ret = 0;
FILE *f;
f = fopen(KALLSYMS_PATH, "r");
if (!f) {
perror("Can't open " KALLSYMS_PATH);
exit(EXIT_FAILURE);
}
while (fgets(buf, sizeof(buf), f)) {
char *match;
int i;
assert(buf[strlen(buf) - 1] == '\n');
for (i = 0; i < TRANSPORT_NUM; ++i) {
if (ret & BIT(i))
continue;
/* Match should be followed by '\t' or '\n'.
* See kallsyms.c:s_show().
*/
ksym = transport_ksyms[i];
match = strstr(buf, ksym);
if (match && isspace(match[strlen(ksym)])) {
ret |= BIT(i);
break;
}
}
}
fclose(f);
return ret;
}
/* Return integer with TRANSPORT_* bit set for every (known) registered vsock
* transport.
*/
int get_transports(void)
{
static int tr = -1;
if (tr == -1)
tr = __get_transports();
return tr;
}

View File

@@ -3,8 +3,36 @@
#define UTIL_H
#include <sys/socket.h>
#include <linux/bitops.h>
#include <linux/kernel.h>
#include <linux/vm_sockets.h>
/* 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);
/* Tests can either run as the client or the server */
enum test_mode {
TEST_MODE_UNSET,
@@ -82,4 +110,5 @@ void setsockopt_timeval_check(int fd, int level, int optname,
struct timeval val, char const *errmsg);
void enable_so_zerocopy_check(int fd);
void enable_so_linger(int fd, int timeout);
int get_transports(void);
#endif /* UTIL_H */