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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/* SPDX-License-Identifier: MIT */
/*
* Test case for socket read/write through IORING_OP_READV and
* IORING_OP_WRITEV, using both TCP and sockets and blocking and
* non-blocking IO.
*
* Heavily based on a test case from Hrvoje Zeba <zeba.hrvoje@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "helpers.h"
#include "liburing.h"
#define RECV_BUFF_SIZE 2
#define SEND_BUFF_SIZE 3
struct params {
int tcp;
int non_blocking;
__be16 bind_port;
};
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static int rcv_ready = 0;
static void set_rcv_ready(void)
{
pthread_mutex_lock(&mutex);
rcv_ready = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
static void wait_for_rcv_ready(void)
{
pthread_mutex_lock(&mutex);
while (!rcv_ready)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
static void *rcv(void *arg)
{
struct params *p = arg;
int s0;
int res;
if (p->tcp) {
int ret, val = 1;
s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
res = setsockopt(s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
assert(res != -1);
res = setsockopt(s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
assert(res != -1);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = t_bind_ephemeral_port(s0, &addr);
assert(!ret);
p->bind_port = addr.sin_port;
} else {
s0 = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
assert(s0 != -1);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, "\0sock", 6);
res = bind(s0, (struct sockaddr *) &addr, sizeof(addr));
assert(res != -1);
}
res = listen(s0, 128);
assert(res != -1);
set_rcv_ready();
int s1 = accept(s0, NULL, NULL);
assert(s1 != -1);
if (p->non_blocking)
t_set_nonblock(s1);
struct io_uring m_io_uring;
void *ret = NULL;
res = io_uring_queue_init(32, &m_io_uring, 0);
assert(res >= 0);
int bytes_read = 0;
int expected_byte = 0;
int done = 0;
while (!done && bytes_read != 33) {
char buff[RECV_BUFF_SIZE];
struct iovec iov;
iov.iov_base = buff;
iov.iov_len = sizeof(buff);
struct io_uring_sqe *sqe = io_uring_get_sqe(&m_io_uring);
assert(sqe != NULL);
io_uring_prep_readv(sqe, s1, &iov, 1, 0);
res = io_uring_submit(&m_io_uring);
assert(res != -1);
struct io_uring_cqe *cqe;
unsigned head;
unsigned count = 0;
while (!done && count != 1) {
io_uring_for_each_cqe(&m_io_uring, head, cqe) {
if (cqe->res < 0)
assert(cqe->res == -EAGAIN);
else {
int i;
for (i = 0; i < cqe->res; i++) {
if (buff[i] != expected_byte) {
fprintf(stderr,
"Received %d, wanted %d\n",
buff[i], expected_byte);
ret++;
done = 1;
}
expected_byte++;
}
bytes_read += cqe->res;
}
count++;
}
assert(count <= 1);
io_uring_cq_advance(&m_io_uring, count);
}
}
shutdown(s1, SHUT_RDWR);
close(s1);
close(s0);
io_uring_queue_exit(&m_io_uring);
return ret;
}
static void *snd(void *arg)
{
struct params *p = arg;
int s0;
int ret;
wait_for_rcv_ready();
if (p->tcp) {
int val = 1;
s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
ret = setsockopt(s0, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
assert(ret != -1);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = p->bind_port;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = connect(s0, (struct sockaddr*) &addr, sizeof(addr));
assert(ret != -1);
} else {
s0 = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
assert(s0 != -1);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, "\0sock", 6);
ret = connect(s0, (struct sockaddr*) &addr, sizeof(addr));
assert(ret != -1);
}
if (p->non_blocking)
t_set_nonblock(s0);
struct io_uring m_io_uring;
ret = io_uring_queue_init(32, &m_io_uring, 0);
assert(ret >= 0);
int bytes_written = 0;
int done = 0;
while (!done && bytes_written != 33) {
char buff[SEND_BUFF_SIZE];
int i;
for (i = 0; i < SEND_BUFF_SIZE; i++)
buff[i] = i + bytes_written;
struct iovec iov;
iov.iov_base = buff;
iov.iov_len = sizeof(buff);
struct io_uring_sqe *sqe = io_uring_get_sqe(&m_io_uring);
assert(sqe != NULL);
io_uring_prep_writev(sqe, s0, &iov, 1, 0);
ret = io_uring_submit(&m_io_uring);
assert(ret != -1);
struct io_uring_cqe *cqe;
unsigned head;
unsigned count = 0;
while (!done && count != 1) {
io_uring_for_each_cqe(&m_io_uring, head, cqe) {
if (cqe->res < 0) {
if (cqe->res == -EPIPE) {
done = 1;
break;
}
assert(cqe->res == -EAGAIN);
} else {
bytes_written += cqe->res;
}
count++;
}
assert(count <= 1);
io_uring_cq_advance(&m_io_uring, count);
}
usleep(100000);
}
shutdown(s0, SHUT_RDWR);
close(s0);
io_uring_queue_exit(&m_io_uring);
return NULL;
}
int main(int argc, char *argv[])
{
struct params p;
pthread_t t1, t2;
void *res1, *res2;
int i, exit_val = T_EXIT_PASS;
if (argc > 1)
return T_EXIT_SKIP;
for (i = 0; i < 4; i++) {
p.tcp = i & 1;
p.non_blocking = (i & 2) >> 1;
rcv_ready = 0;
pthread_create(&t1, NULL, rcv, &p);
pthread_create(&t2, NULL, snd, &p);
pthread_join(t1, &res1);
pthread_join(t2, &res2);
if (res1 || res2) {
fprintf(stderr, "Failed tcp=%d, non_blocking=%d\n", p.tcp, p.non_blocking);
exit_val = T_EXIT_FAIL;
}
}
return exit_val;
}
|