public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH liburing 1/1] tests: test open/accept directly into fixed table
@ 2021-08-21 15:53 Pavel Begunkov
  2021-08-25 17:58 ` Jens Axboe
  0 siblings, 1 reply; 2+ messages in thread
From: Pavel Begunkov @ 2021-08-21 15:53 UTC (permalink / raw)
  To: Jens Axboe, io-uring, Josh Triplett

Test a new feature allowing to open/accept directly into io_uring's
fixed table bypassing normal fdtable.

Signed-off-by: Pavel Begunkov <[email protected]>
---
 test/accept.c  |  74 ++++++++++++++++++------
 test/openat2.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 199 insertions(+), 27 deletions(-)

diff --git a/test/accept.c b/test/accept.c
index f096f8a..41caa48 100644
--- a/test/accept.c
+++ b/test/accept.c
@@ -39,9 +39,10 @@ static void queue_send(struct io_uring *ring, int fd)
 
 	sqe = io_uring_get_sqe(ring);
 	io_uring_prep_writev(sqe, fd, &d->iov, 1, 0);
+	sqe->user_data = 1;
 }
 
-static void queue_recv(struct io_uring *ring, int fd)
+static void queue_recv(struct io_uring *ring, int fd, bool fixed)
 {
 	struct io_uring_sqe *sqe;
 	struct data *d;
@@ -52,16 +53,21 @@ static void queue_recv(struct io_uring *ring, int fd)
 
 	sqe = io_uring_get_sqe(ring);
 	io_uring_prep_readv(sqe, fd, &d->iov, 1, 0);
+	sqe->user_data = 2;
+	if (fixed)
+		sqe->flags |= IOSQE_FIXED_FILE;
 }
 
-static int accept_conn(struct io_uring *ring, int fd)
+static int accept_conn(struct io_uring *ring, int fd, bool fixed)
 {
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
-	int ret;
+	int ret, fixed_idx = 0;
 
 	sqe = io_uring_get_sqe(ring);
 	io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
+	if (fixed)
+		sqe->splice_fd_in = fixed_idx + 1;
 
 	ret = io_uring_submit(ring);
 	assert(ret != -1);
@@ -70,6 +76,15 @@ static int accept_conn(struct io_uring *ring, int fd)
 	assert(!ret);
 	ret = cqe->res;
 	io_uring_cqe_seen(ring, cqe);
+
+	if (fixed) {
+		if (ret > 0) {
+			close(ret);
+			return -EINVAL;
+		} else if (!ret) {
+			ret = fixed_idx;
+		}
+	}
 	return ret;
 }
 
@@ -102,15 +117,12 @@ static int start_accept_listen(struct sockaddr_in *addr, int port_off)
 	return fd;
 }
 
-static int test(struct io_uring *ring, int accept_should_error)
+static int test(struct io_uring *ring, int accept_should_error, bool fixed)
 {
 	struct io_uring_cqe *cqe;
 	struct sockaddr_in addr;
-	uint32_t head;
-	uint32_t count = 0;
-	int done = 0;
-	int p_fd[2];
-        int ret;
+	uint32_t head, count = 0;
+	int ret, p_fd[2], done = 0;
 
 	int32_t val, recv_s0 = start_accept_listen(&addr, 0);
 
@@ -137,11 +149,14 @@ static int test(struct io_uring *ring, int accept_should_error)
 	ret = fcntl(p_fd[1], F_SETFL, flags);
 	assert(ret != -1);
 
-	p_fd[0] = accept_conn(ring, recv_s0);
+	p_fd[0] = accept_conn(ring, recv_s0, fixed);
 	if (p_fd[0] == -EINVAL) {
 		if (accept_should_error)
 			goto out;
-		fprintf(stdout, "Accept not supported, skipping\n");
+		if (fixed)
+			fprintf(stdout, "Fixed accept not supported, skipping\n");
+		else
+			fprintf(stdout, "Accept not supported, skipping\n");
 		no_accept = 1;
 		goto out;
 	} else if (p_fd[0] < 0) {
@@ -153,7 +168,7 @@ static int test(struct io_uring *ring, int accept_should_error)
 	}
 
 	queue_send(ring, p_fd[1]);
-	queue_recv(ring, p_fd[0]);
+	queue_recv(ring, p_fd[0], fixed);
 
 	ret = io_uring_submit_and_wait(ring, 2);
 	assert(ret != -1);
@@ -161,7 +176,8 @@ static int test(struct io_uring *ring, int accept_should_error)
 	while (count < 2) {
 		io_uring_for_each_cqe(ring, head, cqe) {
 			if (cqe->res < 0) {
-				fprintf(stderr, "Got cqe res %d\n", cqe->res);
+				fprintf(stderr, "Got cqe res %d, user_data %i\n",
+						cqe->res, (int)cqe->user_data);
 				done = 1;
 				break;
 			}
@@ -176,12 +192,14 @@ static int test(struct io_uring *ring, int accept_should_error)
 	}
 
 out:
-	close(p_fd[0]);
+	if (!fixed)
+		close(p_fd[0]);
 	close(p_fd[1]);
 	close(recv_s0);
 	return 0;
 err:
-	close(p_fd[0]);
+	if (!fixed)
+		close(p_fd[0]);
 	close(p_fd[1]);
 	close(recv_s0);
 	return 1;
@@ -302,7 +320,7 @@ static int test_accept_cancel(unsigned usecs)
 	sqe = io_uring_get_sqe(&m_io_uring);
 	io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
 	sqe->user_data = 1;
-        ret = io_uring_submit(&m_io_uring);
+	ret = io_uring_submit(&m_io_uring);
 	assert(ret == 1);
 
 	if (usecs)
@@ -355,7 +373,21 @@ static int test_accept(void)
 
 	ret = io_uring_queue_init(32, &m_io_uring, 0);
 	assert(ret >= 0);
-	ret = test(&m_io_uring, 0);
+	ret = test(&m_io_uring, 0, false);
+	io_uring_queue_exit(&m_io_uring);
+	return ret;
+}
+
+static int test_accept_fixed(void)
+{
+	struct io_uring m_io_uring;
+	int ret, fd = -1;
+
+	ret = io_uring_queue_init(32, &m_io_uring, 0);
+	assert(ret >= 0);
+	ret = io_uring_register_files(&m_io_uring, &fd, 1);
+	assert(ret == 0);
+	ret = test(&m_io_uring, 0, true);
 	io_uring_queue_exit(&m_io_uring);
 	return ret;
 }
@@ -377,7 +409,7 @@ static int test_accept_sqpoll(void)
 	if (p.features & IORING_FEAT_SQPOLL_NONFIXED)
 		should_fail = 0;
 
-	ret = test(&m_io_uring, should_fail);
+	ret = test(&m_io_uring, should_fail, false);
 	io_uring_queue_exit(&m_io_uring);
 	return ret;
 }
@@ -397,6 +429,12 @@ int main(int argc, char *argv[])
 	if (no_accept)
 		return 0;
 
+	ret = test_accept_fixed();
+	if (ret) {
+		fprintf(stderr, "test_accept_fixed failed\n");
+		return ret;
+	}
+
 	ret = test_accept_sqpoll();
 	if (ret) {
 		fprintf(stderr, "test_accept_sqpoll failed\n");
diff --git a/test/openat2.c b/test/openat2.c
index 65f81b1..8964208 100644
--- a/test/openat2.c
+++ b/test/openat2.c
@@ -13,7 +13,8 @@
 #include "helpers.h"
 #include "liburing.h"
 
-static int test_openat2(struct io_uring *ring, const char *path, int dfd)
+static int test_openat2(struct io_uring *ring, const char *path, int dfd,
+			int fixed_slot)
 {
 	struct io_uring_cqe *cqe;
 	struct io_uring_sqe *sqe;
@@ -23,30 +24,151 @@ static int test_openat2(struct io_uring *ring, const char *path, int dfd)
 	sqe = io_uring_get_sqe(ring);
 	if (!sqe) {
 		fprintf(stderr, "get sqe failed\n");
-		goto err;
+		return -1;
 	}
 	memset(&how, 0, sizeof(how));
-	how.flags = O_RDONLY;
+	how.flags = O_RDWR;
 	io_uring_prep_openat2(sqe, dfd, path, &how);
+	sqe->splice_fd_in = fixed_slot;
 
 	ret = io_uring_submit(ring);
 	if (ret <= 0) {
 		fprintf(stderr, "sqe submit failed: %d\n", ret);
-		goto err;
+		return -1;
 	}
 
 	ret = io_uring_wait_cqe(ring, &cqe);
 	if (ret < 0) {
 		fprintf(stderr, "wait completion %d\n", ret);
-		goto err;
+		return -1;
 	}
 	ret = cqe->res;
 	io_uring_cqe_seen(ring, cqe);
+
+	if (fixed_slot && ret > 0) {
+		close(ret);
+		return -EINVAL;
+	}
 	return ret;
-err:
-	return -1;
 }
 
+static int test_open_fixed(const char *path, int dfd)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	struct io_uring ring;
+	const char pattern = 0xac;
+	char buffer[] = { 0, 0 };
+	int i, ret, fd = -1;
+
+	ret = io_uring_queue_init(8, &ring, 0);
+	if (ret) {
+		fprintf(stderr, "ring setup failed\n");
+		return -1;
+	}
+	ret = io_uring_register_files(&ring, &fd, 1);
+	if (ret) {
+		fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
+		return -1;
+	}
+
+	ret = test_openat2(&ring, path, dfd, 1);
+	if (ret == -EINVAL) {
+		printf("fixed open isn't supported\n");
+		return 1;
+	} else if (ret) {
+		fprintf(stderr, "direct open failed %d\n", ret);
+		return -1;
+	}
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_write(sqe, 0, &pattern, 1, 0);
+	sqe->user_data = 1;
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_read(sqe, 0, buffer, 1, 0);
+	sqe->user_data = 2;
+	sqe->flags |= IOSQE_FIXED_FILE;
+
+	ret = io_uring_submit(&ring);
+	if (ret != 2) {
+		fprintf(stderr, "%s: got %d, wanted 2\n", __FUNCTION__, ret);
+		return -1;
+	}
+
+	for (i = 0; i < 2; i++) {
+		ret = io_uring_wait_cqe(&ring, &cqe);
+		if (ret < 0) {
+			fprintf(stderr, "wait completion %d\n", ret);
+			return -1;
+		}
+		if (cqe->res != 1) {
+			fprintf(stderr, "unexpectetd ret %d\n", cqe->res);
+			return -1;
+		}
+		io_uring_cqe_seen(&ring, cqe);
+	}
+	if (memcmp(&pattern, buffer, 1) != 0) {
+		fprintf(stderr, "buf validation failed\n");
+		return -1;
+	}
+
+	ret = test_openat2(&ring, path, dfd, 1);
+	if (ret != -EBADF) {
+		fprintf(stderr, "bogus double register %d\n", ret);
+		return -1;
+	}
+	io_uring_queue_exit(&ring);
+	return 0;
+}
+
+static int test_open_fixed_fail(const char *path, int dfd)
+{
+	struct io_uring ring;
+	int ret, fd = -1;
+
+	ret = io_uring_queue_init(8, &ring, 0);
+	if (ret) {
+		fprintf(stderr, "ring setup failed\n");
+		return -1;
+	}
+
+	ret = test_openat2(&ring, path, dfd, 1);
+	if (ret != -ENXIO) {
+		fprintf(stderr, "install into not existing table, %i\n", ret);
+		return 1;
+	}
+
+	ret = io_uring_register_files(&ring, &fd, 1);
+	if (ret) {
+		fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
+		return -1;
+	}
+
+	ret = test_openat2(&ring, path, dfd, 2);
+	if (ret != -EINVAL) {
+		fprintf(stderr, "install out of bounds, %i\n", ret);
+		return 1;
+	}
+
+	ret = test_openat2(&ring, path, dfd, (1u << 16));
+	if (ret != -EINVAL) {
+		fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
+		return 1;
+	}
+
+	ret = test_openat2(&ring, path, dfd, (1u << 16) + 1);
+	if (ret != -EINVAL) {
+		fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
+		return 1;
+	}
+
+	io_uring_queue_exit(&ring);
+	return 0;
+}
+
+
 int main(int argc, char *argv[])
 {
 	struct io_uring ring;
@@ -74,7 +196,7 @@ int main(int argc, char *argv[])
 	if (do_unlink)
 		t_create_file(path_rel, 4096);
 
-	ret = test_openat2(&ring, path, -1);
+	ret = test_openat2(&ring, path, -1, 0);
 	if (ret < 0) {
 		if (ret == -EINVAL) {
 			fprintf(stdout, "openat2 not supported, skipping\n");
@@ -84,12 +206,24 @@ int main(int argc, char *argv[])
 		goto err;
 	}
 
-	ret = test_openat2(&ring, path_rel, AT_FDCWD);
+	ret = test_openat2(&ring, path_rel, AT_FDCWD, 0);
 	if (ret < 0) {
 		fprintf(stderr, "test_openat2 relative failed: %d\n", ret);
 		goto err;
 	}
 
+	ret = test_open_fixed(path, -1);
+	if (ret > 0)
+		goto done;
+	if (ret) {
+		fprintf(stderr, "test_open_fixed failed\n");
+		goto err;
+	}
+	ret = test_open_fixed_fail(path, -1);
+	if (ret) {
+		fprintf(stderr, "test_open_fixed_fail failed\n");
+		goto err;
+	}
 done:
 	unlink(path);
 	if (do_unlink)
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH liburing 1/1] tests: test open/accept directly into fixed table
  2021-08-21 15:53 [PATCH liburing 1/1] tests: test open/accept directly into fixed table Pavel Begunkov
@ 2021-08-25 17:58 ` Jens Axboe
  0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2021-08-25 17:58 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring, Josh Triplett

On 8/21/21 9:53 AM, Pavel Begunkov wrote:
> Test a new feature allowing to open/accept directly into io_uring's
> fixed table bypassing normal fdtable.

Applied, thanks.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-25 17:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-21 15:53 [PATCH liburing 1/1] tests: test open/accept directly into fixed table Pavel Begunkov
2021-08-25 17:58 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox