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>
16 #include <linux/kernel.h>
21 #include "msg_zerocopy_common.h"
24 #define PAGE_SIZE 4096
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'. */
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. */
43 { NULL
, 2 * PAGE_SIZE
},
44 { NULL
, 3 * PAGE_SIZE
},
47 /* Middle element has both non-page aligned base and size. */
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
,
62 struct io_uring_sqe
*sqe
;
63 struct io_uring_cqe
*cqe
;
69 fd
= vsock_stream_connect(opts
->peer_cid
, opts
->peer_port
);
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
));
88 msg
.msg_iovlen
= test_data
->vecs_cnt
;
89 sqe
= io_uring_get_sqe(&ring
);
92 io_uring_prep_sendmsg_zc(sqe
, fd
, &msg
, 0);
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
);
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
;
123 fd
= vsock_stream_accept(VMADDR_CID_ANY
, opts
->peer_port
, NULL
);
129 data_len
= iovec_bytes(test_data
->vecs
, test_data
->vecs_cnt
);
131 data
= malloc(data_len
);
137 if (io_uring_queue_init(RING_ENTRIES_NUM
, &ring
, 0))
138 error(1, errno
, "io_uring_queue_init");
142 while (recv_len
< data_len
) {
143 struct io_uring_sqe
*sqe
;
144 struct io_uring_cqe
*cqe
;
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
,
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");
177 control_expectln("DONE");
178 io_uring_queue_exit(&ring
);
182 void test_stream_uring_server(const struct test_opts
*opts
)
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
)
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
)
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
)
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
,
236 .name
= "control-port",
237 .has_arg
= required_argument
,
242 .has_arg
= required_argument
,
247 .has_arg
= required_argument
,
252 .has_arg
= required_argument
,
257 .has_arg
= no_argument
,
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"
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"
270 "Run transmission tests using io_uring. Usage is the same as\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",
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
,
298 int opt
= getopt_long(argc
, argv
, optstring
, longopts
, NULL
);
305 control_host
= optarg
;
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
;
313 fprintf(stderr
, "--mode must be \"client\" or \"server\"\n");
318 opts
.peer_cid
= parse_cid(optarg
);
321 opts
.peer_port
= parse_port(optarg
);
324 control_port
= optarg
;
334 if (opts
.mode
== TEST_MODE_UNSET
)
336 if (opts
.peer_cid
== VMADDR_CID_ANY
)
340 if (opts
.mode
!= TEST_MODE_SERVER
)
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
);