summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2021-12-23 09:43:40 -0700
committerJens Axboe <axboe@kernel.dk>2021-12-23 09:43:40 -0700
commit03be3e4fbddd491ef0426b6f9c9085a168acc1c4 (patch)
treed490d062f2f443388b7c7f98e490f4a332f90dd9
parentd3ff18ac5c1b98d47d8a1856654a68b06f5be301 (diff)
downloadliburing-03be3e4fbddd491ef0426b6f9c9085a168acc1c4.tar.gz
liburing-03be3e4fbddd491ef0426b6f9c9085a168acc1c4.tar.bz2
Add test case for socket read with offset == -1
This should just get ignored by the kernel, so check that it is. This is fixed by kernel commit: commit 7b9762a5e8837b92a027d58d396a9d27f6440c36 Author: Jens Axboe <axboe@kernel.dk> Date: Wed Dec 22 20:26:56 2021 -0700 io_uring: zero iocb->ki_pos for stream file types Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--.gitignore1
-rw-r--r--test/Makefile1
-rw-r--r--test/socket-rw-offset.c155
3 files changed, 157 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 0a72f03..26458bd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -103,6 +103,7 @@
/test/sigfd-deadlock
/test/socket-rw
/test/socket-rw-eagain
+/test/socket-rw-offset
/test/splice
/test/sq-full
/test/sq-full-cpp
diff --git a/test/Makefile b/test/Makefile
index b93af75..ccf4fde 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -126,6 +126,7 @@ test_srcs := \
sigfd-deadlock.c \
socket-rw.c \
socket-rw-eagain.c \
+ socket-rw-offset.c \
splice.c \
sq-full.c \
sq-full-cpp.cc \
diff --git a/test/socket-rw-offset.c b/test/socket-rw-offset.c
new file mode 100644
index 0000000..d44dd8c
--- /dev/null
+++ b/test/socket-rw-offset.c
@@ -0,0 +1,155 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Check that a readv on a socket queued before a writev doesn't hang
+ * the processing.
+ *
+ * From Hrvoje Zeba <zeba.hrvoje@gmail.com>
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <assert.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 "liburing.h"
+
+int main(int argc, char *argv[])
+{
+ int p_fd[2], ret;
+ int32_t recv_s0;
+ int32_t val = 1;
+ struct sockaddr_in addr;
+
+ if (argc > 1)
+ return 0;
+
+ srand(getpid());
+
+ recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
+
+ ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
+ assert(ret != -1);
+ ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
+ assert(ret != -1);
+
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+
+ do {
+ addr.sin_port = htons((rand() % 61440) + 4096);
+ ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr));
+ if (!ret)
+ break;
+ if (errno != EADDRINUSE) {
+ perror("bind");
+ exit(1);
+ }
+ } while (1);
+ ret = listen(recv_s0, 128);
+ assert(ret != -1);
+
+
+ p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
+
+ val = 1;
+ ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
+ assert(ret != -1);
+
+ int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
+ assert(flags != -1);
+
+ flags |= O_NONBLOCK;
+ ret = fcntl(p_fd[1], F_SETFL, flags);
+ assert(ret != -1);
+
+ ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
+ assert(ret == -1);
+
+ flags = fcntl(p_fd[1], F_GETFL, 0);
+ assert(flags != -1);
+
+ flags &= ~O_NONBLOCK;
+ ret = fcntl(p_fd[1], F_SETFL, flags);
+ assert(ret != -1);
+
+ p_fd[0] = accept(recv_s0, NULL, NULL);
+ assert(p_fd[0] != -1);
+
+ while (1) {
+ int32_t code;
+ socklen_t code_len = sizeof(code);
+
+ ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
+ assert(ret != -1);
+
+ if (!code)
+ break;
+ }
+
+ struct io_uring m_io_uring;
+
+ ret = io_uring_queue_init(32, &m_io_uring, 0);
+ assert(ret >= 0);
+
+ char recv_buff[128];
+ char send_buff[128];
+
+ {
+ struct iovec iov[1];
+
+ iov[0].iov_base = recv_buff;
+ iov[0].iov_len = sizeof(recv_buff);
+
+ struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
+ assert(sqe != NULL);
+
+ io_uring_prep_readv(sqe, p_fd[0], iov, 1, -1);
+ }
+
+ {
+ struct iovec iov[1];
+
+ iov[0].iov_base = send_buff;
+ iov[0].iov_len = sizeof(send_buff);
+
+ struct io_uring_sqe* sqe = io_uring_get_sqe(&m_io_uring);
+ assert(sqe != NULL);
+
+ io_uring_prep_writev(sqe, p_fd[1], iov, 1, 0);
+ }
+
+ ret = io_uring_submit_and_wait(&m_io_uring, 2);
+ assert(ret != -1);
+
+ struct io_uring_cqe* cqe;
+ uint32_t head;
+ uint32_t count = 0;
+
+ ret = 0;
+ while (count != 2) {
+ io_uring_for_each_cqe(&m_io_uring, head, cqe) {
+ if (cqe->res != 128) {
+ fprintf(stderr, "Got %d, expected 128\n", cqe->res);
+ ret = 1;
+ goto err;
+ }
+ assert(cqe->res == 128);
+ count++;
+ }
+
+ assert(count <= 2);
+ io_uring_cq_advance(&m_io_uring, count);
+ }
+
+err:
+ io_uring_queue_exit(&m_io_uring);
+ return ret;
+}