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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/* SPDX-License-Identifier: MIT */
/*
* Description: test ring messaging with flags command
*
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include "liburing.h"
#include "helpers.h"
#define CUSTOM_FLAG 0x42
#define USER_DATA 0x5aa5
#define LEN 0x20
#define ID 0x1
struct data {
pthread_barrier_t barrier;
int fd;
};
static int recv_msg(struct io_uring *ring)
{
struct io_uring_cqe *cqe;
int ret;
ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
fprintf(stderr, "wait cqe %d\n", ret);
return T_EXIT_FAIL;
}
if (cqe->user_data != USER_DATA) {
fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
return T_EXIT_FAIL;
}
if (cqe->res != LEN) {
fprintf(stderr, "len %x\n", cqe->res);
return T_EXIT_FAIL;
}
if (cqe->flags != CUSTOM_FLAG) {
fprintf(stderr, "flags %x\n", cqe->flags);
return T_EXIT_FAIL;
}
return T_EXIT_PASS;
}
static int send_msg(struct io_uring *ring, int target_fd)
{
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int ret;
sqe = io_uring_get_sqe(ring);
if (!sqe) {
fprintf(stderr, "get sqe failed\n");
return T_EXIT_FAIL;
}
io_uring_prep_msg_ring_cqe_flags(sqe, target_fd, LEN, USER_DATA,
0, CUSTOM_FLAG);
sqe->user_data = ID;
ret = io_uring_submit(ring);
if (ret <= 0) {
if (ret == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "sqe submit failed: %d\n", ret);
return T_EXIT_FAIL;
}
ret = io_uring_wait_cqe(ring, &cqe);
if (ret < 0) {
fprintf(stderr, "wait completion %d\n", ret);
return T_EXIT_FAIL;
}
if (cqe->res != 0) {
if (cqe->res == -EINVAL)
return T_EXIT_SKIP;
fprintf(stderr, "cqe res %d\n", cqe->res);
return T_EXIT_FAIL;
}
if (cqe->user_data != ID) {
fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
return T_EXIT_FAIL;
}
io_uring_cqe_seen(ring, cqe);
return T_EXIT_PASS;
}
static void *thread_fn(void *data)
{
struct data *d = data;
struct io_uring ring;
int ret;
ret = io_uring_queue_init(2, &ring, IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_SINGLE_ISSUER);
if (ret) {
fprintf(stderr, "ring init failed %d\n", ret);
pthread_barrier_wait(&d->barrier);
return NULL;
}
d->fd = ring.ring_fd;
pthread_barrier_wait(&d->barrier);
if (recv_msg(&ring))
return (void *) 1;
return NULL;
}
static int test(int ring_flags)
{
struct io_uring ring, ring2;
pthread_t thread;
struct data d;
void *ret2;
int ret, i;
ret = io_uring_queue_init(2, &ring, ring_flags);
if (ret) {
fprintf(stderr, "io_uring_queue_init failed for ring1: %d\n", ret);
return T_EXIT_FAIL;
}
ret = io_uring_queue_init(2, &ring2, ring_flags);
if (ret) {
fprintf(stderr, "io_uring_queue_init failed for ring2: %d\n", ret);
return T_EXIT_FAIL;
}
ret = send_msg(&ring, ring2.ring_fd);
if (ret) {
if (ret != T_EXIT_SKIP)
fprintf(stderr, "send_msg failed: %d\n", ret);
return ret;
}
ret = recv_msg(&ring2);
if (ret) {
fprintf(stderr, "recv_msg failed: %d\n", ret);
return ret;
}
for (i = 0; i < 8; i++) {
ret = send_msg(&ring, ring2.ring_fd);
if (ret) {
if (ret != T_EXIT_SKIP)
fprintf(stderr, "send_msg failed: %d\n", ret);
return ret;
}
}
for (i = 0; i < 8; i++) {
ret = recv_msg(&ring2);
if (ret) {
fprintf(stderr, "recv_msg failed: %d\n", ret);
return ret;
}
}
pthread_barrier_init(&d.barrier, NULL, 2);
d.fd = -1;
pthread_create(&thread, NULL, thread_fn, &d);
pthread_barrier_wait(&d.barrier);
if (d.fd == -1)
return T_EXIT_FAIL;
ret = send_msg(&ring, d.fd);
if (ret) {
fprintf(stderr, "send msg failed: %d\n", ret);
return ret;
}
pthread_join(thread, &ret2);
if (ret2) {
fprintf(stderr, "Remote test failed\n");
return T_EXIT_FAIL;
}
return T_EXIT_PASS;
}
int main(int argc, char *argv[])
{
int ret;
if (argc > 1)
return T_EXIT_SKIP;
ret = test(0);
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test ring_flags 0 failed\n");
return ret;
} else if (ret == T_EXIT_SKIP)
return ret;
ret = test(IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_DEFER_TASKRUN);
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test ring_flags defer failed\n");
return ret;
}
return ret;
}
|