Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / vsock / vsock_uring_test.c
blob6c3e6f70c457da2ab6c0b993e90e1a5b4f8ac439
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* io_uring tests for vsock
4 * Copyright (C) 2023 SberDevices.
6 * Author: Arseniy Krasnov <avkrasnov@salutedevices.com>
7 */
9 #include <getopt.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <liburing.h>
14 #include <unistd.h>
15 #include <sys/mman.h>
16 #include <linux/kernel.h>
17 #include <error.h>
19 #include "util.h"
20 #include "control.h"
21 #include "msg_zerocopy_common.h"
23 #ifndef PAGE_SIZE
24 #define PAGE_SIZE 4096
25 #endif
27 #define RING_ENTRIES_NUM 4
29 #define VSOCK_TEST_DATA_MAX_IOV 3
31 struct vsock_io_uring_test {
32 /* Number of valid elements in 'vecs'. */
33 int vecs_cnt;
34 struct iovec vecs[VSOCK_TEST_DATA_MAX_IOV];
37 static struct vsock_io_uring_test test_data_array[] = {
38 /* All elements have page aligned base and size. */
40 .vecs_cnt = 3,
42 { NULL, PAGE_SIZE },
43 { NULL, 2 * PAGE_SIZE },
44 { NULL, 3 * PAGE_SIZE },
47 /* Middle element has both non-page aligned base and size. */
49 .vecs_cnt = 3,
51 { NULL, PAGE_SIZE },
52 { (void *)1, 200 },
53 { NULL, 3 * PAGE_SIZE },
58 static void vsock_io_uring_client(const struct test_opts *opts,
59 const struct vsock_io_uring_test *test_data,
60 bool msg_zerocopy)
62 struct io_uring_sqe *sqe;
63 struct io_uring_cqe *cqe;
64 struct io_uring ring;
65 struct iovec *iovec;
66 struct msghdr msg;
67 int fd;
69 fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
70 if (fd < 0) {
71 perror("connect");
72 exit(EXIT_FAILURE);
75 if (msg_zerocopy)
76 enable_so_zerocopy(fd);
78 iovec = alloc_test_iovec(test_data->vecs, test_data->vecs_cnt);
80 if (io_uring_queue_init(RING_ENTRIES_NUM, &ring, 0))
81 error(1, errno, "io_uring_queue_init");
83 if (io_uring_register_buffers(&ring, iovec, test_data->vecs_cnt))
84 error(1, errno, "io_uring_register_buffers");
86 memset(&msg, 0, sizeof(msg));
87 msg.msg_iov = iovec;
88 msg.msg_iovlen = test_data->vecs_cnt;
89 sqe = io_uring_get_sqe(&ring);
91 if (msg_zerocopy)
92 io_uring_prep_sendmsg_zc(sqe, fd, &msg, 0);
93 else
94 io_uring_prep_sendmsg(sqe, fd, &msg, 0);
96 if (io_uring_submit(&ring) != 1)
97 error(1, errno, "io_uring_submit");
99 if (io_uring_wait_cqe(&ring, &cqe))
100 error(1, errno, "io_uring_wait_cqe");
102 io_uring_cqe_seen(&ring, cqe);
104 control_writeulong(iovec_hash_djb2(iovec, test_data->vecs_cnt));
106 control_writeln("DONE");
107 io_uring_queue_exit(&ring);
108 free_test_iovec(test_data->vecs, iovec, test_data->vecs_cnt);
109 close(fd);
112 static void vsock_io_uring_server(const struct test_opts *opts,
113 const struct vsock_io_uring_test *test_data)
115 unsigned long remote_hash;
116 unsigned long local_hash;
117 struct io_uring ring;
118 size_t data_len;
119 size_t recv_len;
120 void *data;
121 int fd;
123 fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
124 if (fd < 0) {
125 perror("accept");
126 exit(EXIT_FAILURE);
129 data_len = iovec_bytes(test_data->vecs, test_data->vecs_cnt);
131 data = malloc(data_len);
132 if (!data) {
133 perror("malloc");
134 exit(EXIT_FAILURE);
137 if (io_uring_queue_init(RING_ENTRIES_NUM, &ring, 0))
138 error(1, errno, "io_uring_queue_init");
140 recv_len = 0;
142 while (recv_len < data_len) {
143 struct io_uring_sqe *sqe;
144 struct io_uring_cqe *cqe;
145 struct iovec iovec;
147 sqe = io_uring_get_sqe(&ring);
148 iovec.iov_base = data + recv_len;
149 iovec.iov_len = data_len;
151 io_uring_prep_readv(sqe, fd, &iovec, 1, 0);
153 if (io_uring_submit(&ring) != 1)
154 error(1, errno, "io_uring_submit");
156 if (io_uring_wait_cqe(&ring, &cqe))
157 error(1, errno, "io_uring_wait_cqe");
159 recv_len += cqe->res;
160 io_uring_cqe_seen(&ring, cqe);
163 if (recv_len != data_len) {
164 fprintf(stderr, "expected %zu, got %zu\n", data_len,
165 recv_len);
166 exit(EXIT_FAILURE);
169 local_hash = hash_djb2(data, data_len);
171 remote_hash = control_readulong();
172 if (remote_hash != local_hash) {
173 fprintf(stderr, "hash mismatch\n");
174 exit(EXIT_FAILURE);
177 control_expectln("DONE");
178 io_uring_queue_exit(&ring);
179 free(data);
182 void test_stream_uring_server(const struct test_opts *opts)
184 int i;
186 for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
187 vsock_io_uring_server(opts, &test_data_array[i]);
190 void test_stream_uring_client(const struct test_opts *opts)
192 int i;
194 for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
195 vsock_io_uring_client(opts, &test_data_array[i], false);
198 void test_stream_uring_msg_zc_server(const struct test_opts *opts)
200 int i;
202 for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
203 vsock_io_uring_server(opts, &test_data_array[i]);
206 void test_stream_uring_msg_zc_client(const struct test_opts *opts)
208 int i;
210 for (i = 0; i < ARRAY_SIZE(test_data_array); i++)
211 vsock_io_uring_client(opts, &test_data_array[i], true);
214 static struct test_case test_cases[] = {
216 .name = "SOCK_STREAM io_uring test",
217 .run_server = test_stream_uring_server,
218 .run_client = test_stream_uring_client,
221 .name = "SOCK_STREAM io_uring MSG_ZEROCOPY test",
222 .run_server = test_stream_uring_msg_zc_server,
223 .run_client = test_stream_uring_msg_zc_client,
228 static const char optstring[] = "";
229 static const struct option longopts[] = {
231 .name = "control-host",
232 .has_arg = required_argument,
233 .val = 'H',
236 .name = "control-port",
237 .has_arg = required_argument,
238 .val = 'P',
241 .name = "mode",
242 .has_arg = required_argument,
243 .val = 'm',
246 .name = "peer-cid",
247 .has_arg = required_argument,
248 .val = 'p',
251 .name = "peer-port",
252 .has_arg = required_argument,
253 .val = 'q',
256 .name = "help",
257 .has_arg = no_argument,
258 .val = '?',
263 static void usage(void)
265 fprintf(stderr, "Usage: vsock_uring_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>]\n"
266 "\n"
267 " Server: vsock_uring_test --control-port=1234 --mode=server --peer-cid=3\n"
268 " Client: vsock_uring_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"
269 "\n"
270 "Run transmission tests using io_uring. Usage is the same as\n"
271 "in ./vsock_test\n"
272 "\n"
273 "Options:\n"
274 " --help This help message\n"
275 " --control-host <host> Server IP address to connect to\n"
276 " --control-port <port> Server port to listen on/connect to\n"
277 " --mode client|server Server or client mode\n"
278 " --peer-cid <cid> CID of the other side\n"
279 " --peer-port <port> AF_VSOCK port used for the test [default: %d]\n",
280 DEFAULT_PEER_PORT
282 exit(EXIT_FAILURE);
285 int main(int argc, char **argv)
287 const char *control_host = NULL;
288 const char *control_port = NULL;
289 struct test_opts opts = {
290 .mode = TEST_MODE_UNSET,
291 .peer_cid = VMADDR_CID_ANY,
292 .peer_port = DEFAULT_PEER_PORT,
295 init_signals();
297 for (;;) {
298 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
300 if (opt == -1)
301 break;
303 switch (opt) {
304 case 'H':
305 control_host = optarg;
306 break;
307 case 'm':
308 if (strcmp(optarg, "client") == 0) {
309 opts.mode = TEST_MODE_CLIENT;
310 } else if (strcmp(optarg, "server") == 0) {
311 opts.mode = TEST_MODE_SERVER;
312 } else {
313 fprintf(stderr, "--mode must be \"client\" or \"server\"\n");
314 return EXIT_FAILURE;
316 break;
317 case 'p':
318 opts.peer_cid = parse_cid(optarg);
319 break;
320 case 'q':
321 opts.peer_port = parse_port(optarg);
322 break;
323 case 'P':
324 control_port = optarg;
325 break;
326 case '?':
327 default:
328 usage();
332 if (!control_port)
333 usage();
334 if (opts.mode == TEST_MODE_UNSET)
335 usage();
336 if (opts.peer_cid == VMADDR_CID_ANY)
337 usage();
339 if (!control_host) {
340 if (opts.mode != TEST_MODE_SERVER)
341 usage();
342 control_host = "0.0.0.0";
345 control_init(control_host, control_port,
346 opts.mode == TEST_MODE_SERVER);
348 run_tests(test_cases, &opts);
350 control_cleanup();
352 return 0;