vsock_uring_test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* io_uring tests for vsock
  3. *
  4. * Copyright (C) 2023 SberDevices.
  5. *
  6. * Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
  7. */
  8. #include <getopt.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <liburing.h>
  13. #include <unistd.h>
  14. #include <sys/mman.h>
  15. #include <linux/kernel.h>
  16. #include <error.h>
  17. #include "util.h"
  18. #include "control.h"
  19. #include "msg_zerocopy_common.h"
  20. #ifndef PAGE_SIZE
  21. #define PAGE_SIZE 4096
  22. #endif
  23. #define RING_ENTRIES_NUM 4
  24. #define VSOCK_TEST_DATA_MAX_IOV 3
  25. struct vsock_io_uring_test {
  26. /* Number of valid elements in 'vecs'. */
  27. int vecs_cnt;
  28. struct iovec vecs[VSOCK_TEST_DATA_MAX_IOV];
  29. };
  30. static struct vsock_io_uring_test test_data_array[] = {
  31. /* All elements have page aligned base and size. */
  32. {
  33. .vecs_cnt = 3,
  34. {
  35. { NULL, PAGE_SIZE },
  36. { NULL, 2 * PAGE_SIZE },
  37. { NULL, 3 * PAGE_SIZE },
  38. }
  39. },
  40. /* Middle element has both non-page aligned base and size. */
  41. {
  42. .vecs_cnt = 3,
  43. {
  44. { NULL, PAGE_SIZE },
  45. { (void *)1, 200 },
  46. { NULL, 3 * PAGE_SIZE },
  47. }
  48. }
  49. };
  50. static void vsock_io_uring_client(const struct test_opts *opts,
  51. const struct vsock_io_uring_test *test_data,
  52. bool msg_zerocopy)
  53. {
  54. struct io_uring_sqe *sqe;
  55. struct io_uring_cqe *cqe;
  56. struct io_uring ring;
  57. struct iovec *iovec;
  58. struct msghdr msg;
  59. int fd;
  60. fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
  61. if (fd < 0) {
  62. perror("connect");
  63. exit(EXIT_FAILURE);
  64. }
  65. if (msg_zerocopy)
  66. enable_so_zerocopy_check(fd);
  67. iovec = alloc_test_iovec(test_data->vecs, test_data->vecs_cnt);
  68. if (io_uring_queue_init(RING_ENTRIES_NUM, &ring, 0))
  69. error(1, errno, "io_uring_queue_init");
  70. if (io_uring_register_buffers(&ring, iovec, test_data->vecs_cnt))
  71. error(1, errno, "io_uring_register_buffers");
  72. memset(&msg, 0, sizeof(msg));
  73. msg.msg_iov = iovec;
  74. msg.msg_iovlen = test_data->vecs_cnt;
  75. sqe = io_uring_get_sqe(&ring);
  76. if (msg_zerocopy)
  77. io_uring_prep_sendmsg_zc(sqe, fd, &msg, 0);
  78. else
  79. io_uring_prep_sendmsg(sqe, fd, &msg, 0);
  80. if (io_uring_submit(&ring) != 1)
  81. error(1, errno, "io_uring_submit");
  82. if (io_uring_wait_cqe(&ring, &cqe))
  83. error(1, errno, "io_uring_wait_cqe");
  84. io_uring_cqe_seen(&ring, cqe);
  85. control_writeulong(iovec_hash_djb2(iovec, test_data->vecs_cnt));
  86. control_writeln("DONE");
  87. io_uring_queue_exit(&ring);
  88. free_test_iovec(test_data->vecs, iovec, test_data->vecs_cnt);
  89. close(fd);
  90. }
  91. static void vsock_io_uring_server(const struct test_opts *opts,
  92. const struct vsock_io_uring_test *test_data)
  93. {
  94. unsigned long remote_hash;
  95. unsigned long local_hash;
  96. struct io_uring ring;
  97. size_t data_len;
  98. size_t recv_len;
  99. void *data;
  100. int fd;
  101. fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
  102. if (fd < 0) {
  103. perror("accept");
  104. exit(EXIT_FAILURE);
  105. }
  106. data_len = iovec_bytes(test_data->vecs, test_data->vecs_cnt);
  107. data = malloc(data_len);
  108. if (!data) {
  109. perror("malloc");
  110. exit(EXIT_FAILURE);
  111. }
  112. if (io_uring_queue_init(RING_ENTRIES_NUM, &ring, 0))
  113. error(1, errno, "io_uring_queue_init");
  114. recv_len = 0;
  115. while (recv_len < data_len) {
  116. struct io_uring_sqe *sqe;
  117. struct io_uring_cqe *cqe;
  118. struct iovec iovec;
  119. sqe = io_uring_get_sqe(&ring);
  120. iovec.iov_base = data + recv_len;
  121. iovec.iov_len = data_len;
  122. io_uring_prep_readv(sqe, fd, &iovec, 1, 0);
  123. if (io_uring_submit(&ring) != 1)
  124. error(1, errno, "io_uring_submit");
  125. if (io_uring_wait_cqe(&ring, &cqe))
  126. error(1, errno, "io_uring_wait_cqe");
  127. recv_len += cqe->res;
  128. io_uring_cqe_seen(&ring, cqe);
  129. }
  130. if (recv_len != data_len) {
  131. fprintf(stderr, "expected %zu, got %zu\n", data_len,
  132. recv_len);
  133. exit(EXIT_FAILURE);
  134. }
  135. local_hash = hash_djb2(data, data_len);
  136. remote_hash = control_readulong();
  137. if (remote_hash != local_hash) {
  138. fprintf(stderr, "hash mismatch\n");
  139. exit(EXIT_FAILURE);
  140. }
  141. control_expectln("DONE");
  142. io_uring_queue_exit(&ring);
  143. free(data);
  144. }
  145. void test_stream_uring_server(const struct test_opts *opts)
  146. {
  147. int i;
  148. for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
  149. vsock_io_uring_server(opts, &test_data_array[i]);
  150. }
  151. void test_stream_uring_client(const struct test_opts *opts)
  152. {
  153. int i;
  154. for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
  155. vsock_io_uring_client(opts, &test_data_array[i], false);
  156. }
  157. void test_stream_uring_msg_zc_server(const struct test_opts *opts)
  158. {
  159. int i;
  160. for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
  161. vsock_io_uring_server(opts, &test_data_array[i]);
  162. }
  163. void test_stream_uring_msg_zc_client(const struct test_opts *opts)
  164. {
  165. int i;
  166. for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
  167. vsock_io_uring_client(opts, &test_data_array[i], true);
  168. }
  169. static struct test_case test_cases[] = {
  170. {
  171. .name = "SOCK_STREAM io_uring test",
  172. .run_server = test_stream_uring_server,
  173. .run_client = test_stream_uring_client,
  174. },
  175. {
  176. .name = "SOCK_STREAM io_uring MSG_ZEROCOPY test",
  177. .run_server = test_stream_uring_msg_zc_server,
  178. .run_client = test_stream_uring_msg_zc_client,
  179. },
  180. {},
  181. };
  182. static const char optstring[] = "";
  183. static const struct option longopts[] = {
  184. {
  185. .name = "control-host",
  186. .has_arg = required_argument,
  187. .val = 'H',
  188. },
  189. {
  190. .name = "control-port",
  191. .has_arg = required_argument,
  192. .val = 'P',
  193. },
  194. {
  195. .name = "mode",
  196. .has_arg = required_argument,
  197. .val = 'm',
  198. },
  199. {
  200. .name = "peer-cid",
  201. .has_arg = required_argument,
  202. .val = 'p',
  203. },
  204. {
  205. .name = "peer-port",
  206. .has_arg = required_argument,
  207. .val = 'q',
  208. },
  209. {
  210. .name = "help",
  211. .has_arg = no_argument,
  212. .val = '?',
  213. },
  214. {},
  215. };
  216. static void usage(void)
  217. {
  218. fprintf(stderr, "Usage: vsock_uring_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>]\n"
  219. "\n"
  220. " Server: vsock_uring_test --control-port=1234 --mode=server --peer-cid=3\n"
  221. " Client: vsock_uring_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
  222. "\n"
  223. "Run transmission tests using io_uring. Usage is the same as\n"
  224. "in ./vsock_test\n"
  225. "\n"
  226. "Options:\n"
  227. " --help This help message\n"
  228. " --control-host <host> Server IP address to connect to\n"
  229. " --control-port <port> Server port to listen on/connect to\n"
  230. " --mode client|server Server or client mode\n"
  231. " --peer-cid <cid> CID of the other side\n"
  232. " --peer-port <port> AF_VSOCK port used for the test [default: %d]\n",
  233. DEFAULT_PEER_PORT
  234. );
  235. exit(EXIT_FAILURE);
  236. }
  237. int main(int argc, char **argv)
  238. {
  239. const char *control_host = NULL;
  240. const char *control_port = NULL;
  241. struct test_opts opts = {
  242. .mode = TEST_MODE_UNSET,
  243. .peer_cid = VMADDR_CID_ANY,
  244. .peer_port = DEFAULT_PEER_PORT,
  245. };
  246. init_signals();
  247. for (;;) {
  248. int opt = getopt_long(argc, argv, optstring, longopts, NULL);
  249. if (opt == -1)
  250. break;
  251. switch (opt) {
  252. case 'H':
  253. control_host = optarg;
  254. break;
  255. case 'm':
  256. if (strcmp(optarg, "client") == 0) {
  257. opts.mode = TEST_MODE_CLIENT;
  258. } else if (strcmp(optarg, "server") == 0) {
  259. opts.mode = TEST_MODE_SERVER;
  260. } else {
  261. fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
  262. return EXIT_FAILURE;
  263. }
  264. break;
  265. case 'p':
  266. opts.peer_cid = parse_cid(optarg);
  267. break;
  268. case 'q':
  269. opts.peer_port = parse_port(optarg);
  270. break;
  271. case 'P':
  272. control_port = optarg;
  273. break;
  274. case '?':
  275. default:
  276. usage();
  277. }
  278. }
  279. if (!control_port)
  280. usage();
  281. if (opts.mode == TEST_MODE_UNSET)
  282. usage();
  283. if (opts.peer_cid == VMADDR_CID_ANY)
  284. usage();
  285. if (!control_host) {
  286. if (opts.mode != TEST_MODE_SERVER)
  287. usage();
  288. control_host = "0.0.0.0";
  289. }
  290. control_init(control_host, control_port,
  291. opts.mode == TEST_MODE_SERVER);
  292. run_tests(test_cases, &opts);
  293. control_cleanup();
  294. return 0;
  295. }