Path: blob/master/tools/testing/vsock/vsock_uring_test.c
26282 views
// SPDX-License-Identifier: GPL-2.0-only1/* io_uring tests for vsock2*3* Copyright (C) 2023 SberDevices.4*5* Author: Arseniy Krasnov <[email protected]>6*/78#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>1718#include "util.h"19#include "control.h"20#include "msg_zerocopy_common.h"2122#ifndef PAGE_SIZE23#define PAGE_SIZE 409624#endif2526#define RING_ENTRIES_NUM 42728#define VSOCK_TEST_DATA_MAX_IOV 32930struct vsock_io_uring_test {31/* Number of valid elements in 'vecs'. */32int vecs_cnt;33struct iovec vecs[VSOCK_TEST_DATA_MAX_IOV];34};3536static struct vsock_io_uring_test test_data_array[] = {37/* All elements have page aligned base and size. */38{39.vecs_cnt = 3,40{41{ NULL, PAGE_SIZE },42{ NULL, 2 * PAGE_SIZE },43{ NULL, 3 * PAGE_SIZE },44}45},46/* Middle element has both non-page aligned base and size. */47{48.vecs_cnt = 3,49{50{ NULL, PAGE_SIZE },51{ (void *)1, 200 },52{ NULL, 3 * PAGE_SIZE },53}54}55};5657static void vsock_io_uring_client(const struct test_opts *opts,58const struct vsock_io_uring_test *test_data,59bool msg_zerocopy)60{61struct io_uring_sqe *sqe;62struct io_uring_cqe *cqe;63struct io_uring ring;64struct iovec *iovec;65struct msghdr msg;66int fd;6768fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);69if (fd < 0) {70perror("connect");71exit(EXIT_FAILURE);72}7374if (msg_zerocopy)75enable_so_zerocopy_check(fd);7677iovec = alloc_test_iovec(test_data->vecs, test_data->vecs_cnt);7879if (io_uring_queue_init(RING_ENTRIES_NUM, &ring, 0))80error(1, errno, "io_uring_queue_init");8182if (io_uring_register_buffers(&ring, iovec, test_data->vecs_cnt))83error(1, errno, "io_uring_register_buffers");8485memset(&msg, 0, sizeof(msg));86msg.msg_iov = iovec;87msg.msg_iovlen = test_data->vecs_cnt;88sqe = io_uring_get_sqe(&ring);8990if (msg_zerocopy)91io_uring_prep_sendmsg_zc(sqe, fd, &msg, 0);92else93io_uring_prep_sendmsg(sqe, fd, &msg, 0);9495if (io_uring_submit(&ring) != 1)96error(1, errno, "io_uring_submit");9798if (io_uring_wait_cqe(&ring, &cqe))99error(1, errno, "io_uring_wait_cqe");100101io_uring_cqe_seen(&ring, cqe);102103control_writeulong(iovec_hash_djb2(iovec, test_data->vecs_cnt));104105control_writeln("DONE");106io_uring_queue_exit(&ring);107free_test_iovec(test_data->vecs, iovec, test_data->vecs_cnt);108close(fd);109}110111static void vsock_io_uring_server(const struct test_opts *opts,112const struct vsock_io_uring_test *test_data)113{114unsigned long remote_hash;115unsigned long local_hash;116struct io_uring ring;117size_t data_len;118size_t recv_len;119void *data;120int fd;121122fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);123if (fd < 0) {124perror("accept");125exit(EXIT_FAILURE);126}127128data_len = iovec_bytes(test_data->vecs, test_data->vecs_cnt);129130data = malloc(data_len);131if (!data) {132perror("malloc");133exit(EXIT_FAILURE);134}135136if (io_uring_queue_init(RING_ENTRIES_NUM, &ring, 0))137error(1, errno, "io_uring_queue_init");138139recv_len = 0;140141while (recv_len < data_len) {142struct io_uring_sqe *sqe;143struct io_uring_cqe *cqe;144struct iovec iovec;145146sqe = io_uring_get_sqe(&ring);147iovec.iov_base = data + recv_len;148iovec.iov_len = data_len;149150io_uring_prep_readv(sqe, fd, &iovec, 1, 0);151152if (io_uring_submit(&ring) != 1)153error(1, errno, "io_uring_submit");154155if (io_uring_wait_cqe(&ring, &cqe))156error(1, errno, "io_uring_wait_cqe");157158recv_len += cqe->res;159io_uring_cqe_seen(&ring, cqe);160}161162if (recv_len != data_len) {163fprintf(stderr, "expected %zu, got %zu\n", data_len,164recv_len);165exit(EXIT_FAILURE);166}167168local_hash = hash_djb2(data, data_len);169170remote_hash = control_readulong();171if (remote_hash != local_hash) {172fprintf(stderr, "hash mismatch\n");173exit(EXIT_FAILURE);174}175176control_expectln("DONE");177io_uring_queue_exit(&ring);178free(data);179}180181void test_stream_uring_server(const struct test_opts *opts)182{183int i;184185for (i = 0; i < ARRAY_SIZE(test_data_array); i++)186vsock_io_uring_server(opts, &test_data_array[i]);187}188189void test_stream_uring_client(const struct test_opts *opts)190{191int i;192193for (i = 0; i < ARRAY_SIZE(test_data_array); i++)194vsock_io_uring_client(opts, &test_data_array[i], false);195}196197void test_stream_uring_msg_zc_server(const struct test_opts *opts)198{199int i;200201for (i = 0; i < ARRAY_SIZE(test_data_array); i++)202vsock_io_uring_server(opts, &test_data_array[i]);203}204205void test_stream_uring_msg_zc_client(const struct test_opts *opts)206{207int i;208209for (i = 0; i < ARRAY_SIZE(test_data_array); i++)210vsock_io_uring_client(opts, &test_data_array[i], true);211}212213static struct test_case test_cases[] = {214{215.name = "SOCK_STREAM io_uring test",216.run_server = test_stream_uring_server,217.run_client = test_stream_uring_client,218},219{220.name = "SOCK_STREAM io_uring MSG_ZEROCOPY test",221.run_server = test_stream_uring_msg_zc_server,222.run_client = test_stream_uring_msg_zc_client,223},224{},225};226227static const char optstring[] = "";228static const struct option longopts[] = {229{230.name = "control-host",231.has_arg = required_argument,232.val = 'H',233},234{235.name = "control-port",236.has_arg = required_argument,237.val = 'P',238},239{240.name = "mode",241.has_arg = required_argument,242.val = 'm',243},244{245.name = "peer-cid",246.has_arg = required_argument,247.val = 'p',248},249{250.name = "peer-port",251.has_arg = required_argument,252.val = 'q',253},254{255.name = "help",256.has_arg = no_argument,257.val = '?',258},259{},260};261262static void usage(void)263{264fprintf(stderr, "Usage: vsock_uring_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid> [--peer-port=<port>]\n"265"\n"266" Server: vsock_uring_test --control-port=1234 --mode=server --peer-cid=3\n"267" Client: vsock_uring_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n"268"\n"269"Run transmission tests using io_uring. Usage is the same as\n"270"in ./vsock_test\n"271"\n"272"Options:\n"273" --help This help message\n"274" --control-host <host> Server IP address to connect to\n"275" --control-port <port> Server port to listen on/connect to\n"276" --mode client|server Server or client mode\n"277" --peer-cid <cid> CID of the other side\n"278" --peer-port <port> AF_VSOCK port used for the test [default: %d]\n",279DEFAULT_PEER_PORT280);281exit(EXIT_FAILURE);282}283284int main(int argc, char **argv)285{286const char *control_host = NULL;287const char *control_port = NULL;288struct test_opts opts = {289.mode = TEST_MODE_UNSET,290.peer_cid = VMADDR_CID_ANY,291.peer_port = DEFAULT_PEER_PORT,292};293294init_signals();295296for (;;) {297int opt = getopt_long(argc, argv, optstring, longopts, NULL);298299if (opt == -1)300break;301302switch (opt) {303case 'H':304control_host = optarg;305break;306case 'm':307if (strcmp(optarg, "client") == 0) {308opts.mode = TEST_MODE_CLIENT;309} else if (strcmp(optarg, "server") == 0) {310opts.mode = TEST_MODE_SERVER;311} else {312fprintf(stderr, "--mode must be \"client\" or \"server\"\n");313return EXIT_FAILURE;314}315break;316case 'p':317opts.peer_cid = parse_cid(optarg);318break;319case 'q':320opts.peer_port = parse_port(optarg);321break;322case 'P':323control_port = optarg;324break;325case '?':326default:327usage();328}329}330331if (!control_port)332usage();333if (opts.mode == TEST_MODE_UNSET)334usage();335if (opts.peer_cid == VMADDR_CID_ANY)336usage();337338if (!control_host) {339if (opts.mode != TEST_MODE_SERVER)340usage();341control_host = "0.0.0.0";342}343344control_init(control_host, control_port,345opts.mode == TEST_MODE_SERVER);346347run_tests(test_cases, &opts);348349control_cleanup();350351return 0;352}353354355