On Wed, May 28, 2025 at 10:44:42PM +0200, Michal Luczaj wrote:
Return a bitmap of registered vsock transports. As guesstimated by grepping
/proc/kallsyms (CONFIG_KALLSYMS=y) for known symbols of type `struct
virtio_transport`.

Suggested-by: Stefano Garzarella <sgarz...@redhat.com>
Signed-off-by: Michal Luczaj <m...@rbox.co>
---
tools/testing/vsock/util.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++
tools/testing/vsock/util.h | 12 ++++++++++
2 files changed, 72 insertions(+)

diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c
index 
b7b3fb2221c1682ecde58cf12e2f0b0ded1cff39..74fb52f566148b16436251216dd9d9275f0ec95b
 100644
--- a/tools/testing/vsock/util.c
+++ b/tools/testing/vsock/util.c
@@ -7,6 +7,7 @@
 * Author: Stefan Hajnoczi <stefa...@redhat.com>
 */

+#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
@@ -17,6 +18,7 @@
#include <assert.h>
#include <sys/epoll.h>
#include <sys/mman.h>
+#include <linux/kernel.h>
#include <linux/sockios.h>

#include "timeout.h"
@@ -854,3 +856,61 @@ void enable_so_linger(int fd, int timeout)
                exit(EXIT_FAILURE);
        }
}
+
+static int __get_transports(void)
+{
+       /* Order must match transports defined in util.h.
+        * man nm: "d" The symbol is in the initialized data section.
+        */
+       const char * const syms[] = {
+               "d loopback_transport",
+               "d virtio_transport",
+               "d vhost_transport",
+               "d vmci_transport",
+               "d hvs_transport",
+       };

I would move this array (or a macro that define it), near the transport defined in util.h, so they are near and we can easily update/review changes.

BTW what about adding static asserts to check we are aligned?

+       char buf[KALLSYMS_LINE_LEN];
+       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 < ARRAY_SIZE(syms); ++i) {
+                       match = strstr(buf, syms[i]);
+
+                       /* Match should be followed by '\t' or '\n'.
+                        * See kallsyms.c:s_show().
+                        */
+                       if (match && isspace(match[strlen(syms[i])])) {
+                               ret |= _BITUL(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;
+}
diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h
index 
0afe7cbae12e5194172c639ccfbeb8b81f7c25ac..63953e32c3e18e1aa5c2addcf6f09f433660fa84
 100644
--- a/tools/testing/vsock/util.h
+++ b/tools/testing/vsock/util.h
@@ -3,8 +3,19 @@
#define UTIL_H

#include <sys/socket.h>
+#include <linux/bitops.h>
#include <linux/vm_sockets.h>

+#define KALLSYMS_PATH          "/proc/kallsyms"
+#define KALLSYMS_LINE_LEN      512

We don't need to expose them in util.h IMO, we can keep in util.c

+
+/* All known transports */
+#define TRANSPORT_LOOPBACK     _BITUL(0)
+#define TRANSPORT_VIRTIO       _BITUL(1)
+#define TRANSPORT_VHOST                _BITUL(2)
+#define TRANSPORT_VMCI         _BITUL(3)
+#define TRANSPORT_HYPERV       _BITUL(4)
+
/* Tests can either run as the client or the server */
enum test_mode {
        TEST_MODE_UNSET,
@@ -82,4 +93,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 */

--
2.49.0



Reply via email to