1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// SPDX-License-Identifier: MIT
/*
* Description: test pipe creation through io_uring
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "helpers.h"
#include "liburing.h"
static int no_pipe;
struct params {
int fixed;
int async;
int too_small;
};
static int pipe_comms(struct io_uring *ring, int *fds, struct params *p)
{
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
char src[32], dst[32];
int i, ret;
memset(src, 0x5a, sizeof(src));
memset(dst, 0, sizeof(dst));
sqe = io_uring_get_sqe(ring);
io_uring_prep_write(sqe, fds[1], src, sizeof(src), 0);
if (p->fixed)
sqe->flags |= IOSQE_FIXED_FILE;
sqe->user_data = 1;
io_uring_submit(ring);
sqe = io_uring_get_sqe(ring);
io_uring_prep_read(sqe, fds[0], dst, sizeof(dst), 0);
if (p->fixed)
sqe->flags |= IOSQE_FIXED_FILE;
sqe->user_data = 2;
io_uring_submit(ring);
for (i = 0; i < 2; i++) {
ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
fprintf(stderr, "wait cqe %d\n", ret);
return 1;
}
if (cqe->res != 32) {
printf("ud=%d, res=%d\n", (int) cqe->user_data, cqe->res);
return 1;
}
io_uring_cqe_seen(ring, cqe);
}
return memcmp(dst, src, sizeof(src));
}
static int pipe_test(int init_flags, struct params *p)
{
struct io_uring ring;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
int ret, fds[2];
ret = io_uring_queue_init(8, &ring, init_flags);
/* can hit -ENOMEM due to repeated ring creation and teardowns */
if (ret == -ENOMEM) {
usleep(1000);
return 0;
} else if (ret) {
fprintf(stderr, "ring_init: %d\n", ret);
return 1;
}
if (p->fixed) {
int sz;
if (p->too_small)
sz = 1;
else
sz = 100;
ret = io_uring_register_files_sparse(&ring, sz);
if (ret) {
if (ret == -EINVAL) {
no_pipe = 1;
return 0;
}
fprintf(stderr, "Failed to register sparse table %d\n", ret);
return 1;
}
}
fds[0] = fds[1] = -1;
sqe = io_uring_get_sqe(&ring);
if (!p->fixed)
io_uring_prep_pipe(sqe, fds, 0);
else
io_uring_prep_pipe_direct(sqe, fds, 0, IORING_FILE_INDEX_ALLOC);
if (p->async)
sqe->flags |= IOSQE_ASYNC;
io_uring_submit(&ring);
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait: %d\n", ret);
return 1;
}
if (cqe->res) {
if (cqe->res == -EINVAL) {
no_pipe = 1;
return 0;
}
if (p->fixed && p->too_small && cqe->res == -ENFILE)
goto done;
fprintf(stderr, "Bad cqe res %d\n", cqe->res);
return 1;
}
io_uring_cqe_seen(&ring, cqe);
ret = pipe_comms(&ring, fds, p);
if (!p->fixed) {
close(fds[0]);
close(fds[1]);
}
done:
io_uring_queue_exit(&ring);
return ret;
}
int main(int argc, char *argv[])
{
int init_flags[] = { 0, IORING_SETUP_SQPOLL, IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_SINGLE_ISSUER };
struct params ps[] = {
{ 0, 0, 0 },
{ 0, 1, 0 },
{ 1, 0, 0 },
{ 1, 1, 0 },
{ 0, 0, 1 },
{ 0, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 } };
int i, j;
if (argc > 1)
return T_EXIT_SKIP;
for (i = 0; i < ARRAY_SIZE(init_flags); i++) {
for (j = 0; j < ARRAY_SIZE(ps); j++) {
if (pipe_test(init_flags[i], &ps[j])) {
fprintf(stderr, "pipe %x %d/%d/%d failed\n",
init_flags[i], ps[j].fixed, ps[j].async,
ps[j].too_small);
return T_EXIT_FAIL;
}
if (no_pipe)
return T_EXIT_SKIP;
}
}
return T_EXIT_PASS;
}
|