util.h 4.2 KB

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