Skip to contentSkip to conversation

add direct open/accept helpers

PATCH Applied Pavel Begunkov 2 participants 4 messages Aug 31, 2021 io_uring

lore
Share & tools
lore
#0 lore raw

cover add direct open/accept helpers

Add helpers for direct open/accept. Also, use them in tests as an example.

Pavel Begunkov (2):
liburing: add helpers for direct open/accept
tests: use helpers for direct open/accept

 src/include/liburing.h | 38 ++++++++++++++++++++++++++++++++++++++
 test/accept.c          |  7 ++++---
 test/openat2.c         | 27 +++++++++++++++------------
 3 files changed, 57 insertions(+), 15 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/2 liburing: add helpers for direct open/accept

We allow openat/openat2/accept to place new files right into the internal fixed table, add helpers.

---
 src/include/liburing.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
src/include/liburing.h +38 -0
index 7b364ce..3b9cfb5 100644
@@ -231,6 +231,13 @@ static inline void io_uring_sqe_set_flags(struct io_uring_sqe *sqe,
 	sqe->flags = flags;
 }
 
+static inline void __io_uring_set_target_fixed_file(struct io_uring_sqe *sqe,
+						    unsigned int file_index)
+{
+	/* 0 means no fixed files, indexes should be encoded as "index + 1" */
+	sqe->file_index = file_index + 1;
+}
+
 static inline void io_uring_prep_rw(int op, struct io_uring_sqe *sqe, int fd,
 				    const void *addr, unsigned len,
 				    __u64 offset)
@@ -423,6 +430,16 @@ static inline void io_uring_prep_accept(struct io_uring_sqe *sqe, int fd,
 	sqe->accept_flags = flags;
 }
 
+/* accept directly into the fixed file table */
+static inline void io_uring_prep_accept_direct(struct io_uring_sqe *sqe, int fd,
+					       struct sockaddr *addr,
+					       socklen_t *addrlen, int flags,
+					       unsigned int file_index)
+{
+	io_uring_prep_accept(sqe, fd, addr, addrlen, flags);
+	__io_uring_set_target_fixed_file(sqe, file_index);
+}
+
 static inline void io_uring_prep_cancel(struct io_uring_sqe *sqe, void *user_data,
 					int flags)
 {
@@ -467,6 +484,17 @@ static inline void io_uring_prep_openat(struct io_uring_sqe *sqe, int dfd,
 	sqe->open_flags = flags;
 }
 
+/* open directly into the fixed file table */
+static inline void io_uring_prep_openat_direct(struct io_uring_sqe *sqe,
+					       int dfd, const char *path,
+					       int flags, mode_t mode,
+					       unsigned file_index)
+{
+	io_uring_prep_openat(sqe, dfd, path, flags, mode);
+	__io_uring_set_target_fixed_file(sqe, file_index);
+}
+
+
 static inline void io_uring_prep_close(struct io_uring_sqe *sqe, int fd)
 {
 	io_uring_prep_rw(IORING_OP_CLOSE, sqe, fd, NULL, 0, 0);
@@ -529,6 +557,16 @@ static inline void io_uring_prep_openat2(struct io_uring_sqe *sqe, int dfd,
 				(uint64_t) (uintptr_t) how);
 }
 
+/* open directly into the fixed file table */
+static inline void io_uring_prep_openat2_direct(struct io_uring_sqe *sqe,
+						int dfd, const char *path,
+						struct open_how *how,
+						unsigned file_index)
+{
+	io_uring_prep_openat2(sqe, dfd, path, how);
+	__io_uring_set_target_fixed_file(sqe, file_index);
+}
+
 struct epoll_event;
 static inline void io_uring_prep_epoll_ctl(struct io_uring_sqe *sqe, int epfd,
 					   int fd, int op,
Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/2 tests: use helpers for direct open/accept

In reply to Pavel Begunkov

For the test to be less confusing, use helpers in tests that place new files right into the fixed file table.

---
 test/accept.c  |  7 ++++---
 test/openat2.c | 27 +++++++++++++++------------
 2 files changed, 19 insertions(+), 15 deletions(-)
test/accept.c +4 -3
index 21db14f..0c69b98 100644
@@ -65,9 +65,10 @@ static int accept_conn(struct io_uring *ring, int fd, bool fixed)
 	int ret, fixed_idx = 0;
 
 	sqe = io_uring_get_sqe(ring);
-	io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
-	if (fixed)
-		sqe->file_index = fixed_idx + 1;
+	if (!fixed)
+		io_uring_prep_accept(sqe, fd, NULL, NULL, 0);
+	else
+		io_uring_prep_accept_direct(sqe, fd, NULL, NULL, 0, fixed_idx);
 
 	ret = io_uring_submit(ring);
 	assert(ret != -1);
test/openat2.c +15 -12
index afc30a0..7838c05 100644
@@ -14,7 +14,7 @@
 #include "liburing.h"
 
 static int test_openat2(struct io_uring *ring, const char *path, int dfd,
-			int fixed_slot)
+			bool direct, int fixed_index)
 {
 	struct io_uring_cqe *cqe;
 	struct io_uring_sqe *sqe;
@@ -28,8 +28,11 @@ static int test_openat2(struct io_uring *ring, const char *path, int dfd,
 	}
 	memset(&how, 0, sizeof(how));
 	how.flags = O_RDWR;
-	io_uring_prep_openat2(sqe, dfd, path, &how);
-	sqe->file_index = fixed_slot;
+
+	if (!direct)
+		io_uring_prep_openat2(sqe, dfd, path, &how);
+	else
+		io_uring_prep_openat2_direct(sqe, dfd, path, &how, fixed_index);
 
 	ret = io_uring_submit(ring);
 	if (ret <= 0) {
@@ -45,7 +48,7 @@ static int test_openat2(struct io_uring *ring, const char *path, int dfd,
 	ret = cqe->res;
 	io_uring_cqe_seen(ring, cqe);
 
-	if (fixed_slot && ret > 0) {
+	if (direct && ret > 0) {
 		close(ret);
 		return -EINVAL;
 	}
@@ -72,7 +75,7 @@ static int test_open_fixed(const char *path, int dfd)
 		return -1;
 	}
 
-	ret = test_openat2(&ring, path, dfd, 1);
+	ret = test_openat2(&ring, path, dfd, true, 0);
 	if (ret == -EINVAL) {
 		printf("fixed open isn't supported\n");
 		return 1;
@@ -114,7 +117,7 @@ static int test_open_fixed(const char *path, int dfd)
 		return -1;
 	}
 
-	ret = test_openat2(&ring, path, dfd, 1);
+	ret = test_openat2(&ring, path, dfd, true, 0);
 	if (ret != -EBADF) {
 		fprintf(stderr, "bogus double register %d\n", ret);
 		return -1;
@@ -134,7 +137,7 @@ static int test_open_fixed_fail(const char *path, int dfd)
 		return -1;
 	}
 
-	ret = test_openat2(&ring, path, dfd, 1);
+	ret = test_openat2(&ring, path, dfd, true, 0);
 	if (ret != -ENXIO) {
 		fprintf(stderr, "install into not existing table, %i\n", ret);
 		return 1;
@@ -146,19 +149,19 @@ static int test_open_fixed_fail(const char *path, int dfd)
 		return -1;
 	}
 
-	ret = test_openat2(&ring, path, dfd, 2);
+	ret = test_openat2(&ring, path, dfd, true, 1);
 	if (ret != -EINVAL) {
 		fprintf(stderr, "install out of bounds, %i\n", ret);
 		return 1;
 	}
 
-	ret = test_openat2(&ring, path, dfd, (1u << 16));
+	ret = test_openat2(&ring, path, dfd, true, (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);
+	ret = test_openat2(&ring, path, dfd, true, (1u << 16) + 1);
 	if (ret != -EINVAL) {
 		fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
 		return 1;
@@ -196,7 +199,7 @@ int main(int argc, char *argv[])
 	if (do_unlink)
 		t_create_file(path_rel, 4096);
 
-	ret = test_openat2(&ring, path, -1, 0);
+	ret = test_openat2(&ring, path, -1, false, 0);
 	if (ret < 0) {
 		if (ret == -EINVAL) {
 			fprintf(stdout, "openat2 not supported, skipping\n");
@@ -206,7 +209,7 @@ int main(int argc, char *argv[])
 		goto err;
 	}
 
-	ret = test_openat2(&ring, path_rel, AT_FDCWD, 0);
+	ret = test_openat2(&ring, path_rel, AT_FDCWD, false, 0);
 	if (ret < 0) {
 		fprintf(stderr, "test_openat2 relative failed: %d\n", ret);
 		goto err;
Signature · 1 line
2.33.0
maintainer Applied
#3 lore raw
In reply to Pavel Begunkov
Quoted 11 lines · Pavel Begunkov

Add helpers for direct open/accept. Also, use them in tests as an example.

Pavel Begunkov (2):
liburing: add helpers for direct open/accept
tests: use helpers for direct open/accept

 src/include/liburing.h | 38 ++++++++++++++++++++++++++++++++++++++
 test/accept.c          |  7 ++++---
 test/openat2.c         | 27 +++++++++++++++------------
 3 files changed, 57 insertions(+), 15 deletions(-)

Applied, thanks.

Signature · 1 line
Jens Axboe