Skip to contentSkip to conversation

IORING_OP_BIND/LISTEN support

RFC Superseded Gabriel Krisman Bertazi 2 participants 8 messages Jun 4, 2024 – Jul 21, 2024 io_uring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover IORING_OP_BIND/LISTEN support

Hi,

As promised, this is the userspace side of the IORING_OP_BIND/LISTEN patches.

I decided to write a separate test instead of integrating in i.e. socket.c Let me know if you prefer I merge it to some existing testcase.

Keeping as an RFC for now, until we have the kernel side ready.

Gabriel Krisman Bertazi (5):
liburing: Add helper to prepare IORING_OP_BIND command
liburing: Add helper to prepare IORING_OP_LISTEN command
tests: Add test for bind/listen commands man/io_uring_prep_bind.3: Document the IORING_OP_BIND operation man/io_uring_prep_listen.3: Document IORING_OP_LISTEN operation

 man/io_uring_prep_bind.3        |  54 ++++++++
 man/io_uring_prep_listen.3      |  52 +++++++
 src/include/liburing.h          |  13 ++
 src/include/liburing/io_uring.h |   2 +
 test/Makefile                   |   1 +
 test/bind-listen.c              | 231 ++++++++++++++++++++++++++++++++
 6 files changed, 353 insertions(+)
 create mode 100644 man/io_uring_prep_bind.3
 create mode 100644 man/io_uring_prep_listen.3
 create mode 100644 test/bind-listen.c
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/5 liburing: Add helper to prepare IORING_OP_BIND command

---
 src/include/liburing.h          | 7 +++++++
 src/include/liburing/io_uring.h | 1 +
 2 files changed, 8 insertions(+)
src/include/liburing.h +7 -0
index 0a02364..818e27c 100644
@@ -669,6 +669,13 @@ IOURINGINLINE void io_uring_prep_connect(struct io_uring_sqe *sqe, int fd,
 	io_uring_prep_rw(IORING_OP_CONNECT, sqe, fd, addr, 0, addrlen);
 }
 
+IOURINGINLINE void io_uring_prep_bind(struct io_uring_sqe *sqe, int fd,
+				      struct sockaddr *addr,
+				      socklen_t addrlen)
+{
+	io_uring_prep_rw(IORING_OP_BIND, sqe, fd, addr, 0, addrlen);
+}
+
 IOURINGINLINE void io_uring_prep_files_update(struct io_uring_sqe *sqe,
 					      int *fds, unsigned nr_fds,
 					      int offset)
src/include/liburing/io_uring.h +1 -0
index 9330733..177ace6 100644
@@ -257,6 +257,7 @@ enum io_uring_op {
 	IORING_OP_FUTEX_WAITV,
 	IORING_OP_FIXED_FD_INSTALL,
 	IORING_OP_FTRUNCATE,
+	IORING_OP_BIND,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
Signature · 1 line
2.44.0
maintainer
#2 lore raw

Re: [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command

Quoted 1 line · Gabriel Krisman Bertazi

Finally got around to these, as the kernel support has landed...

Quoted 5 lines
src/include/liburing.h +0 -0
index 0a02364..818e27c 100644

src/include/liburing.h IOURINGINLINE void io_uring_prep_connect(struct io_uring_sqe *sqe, int fd, · line 677

Quoted 9 lines
src/include/liburing.h +6 -0
@@ -669,6 +669,13 @@ IOURINGINLINE void io_uring_prep_connect(struct io_uring_sqe *sqe, int fd,
 	io_uring_prep_rw(IORING_OP_CONNECT, sqe, fd, addr, 0, addrlen);
 }
 
+IOURINGINLINE void io_uring_prep_bind(struct io_uring_sqe *sqe, int fd,
+				      struct sockaddr *addr,
+				      socklen_t addrlen)
+{
+	io_uring_prep_rw(IORING_OP_BIND, sqe, fd, addr, 0, addrlen);
+}

This needs an ffi and liburing-ffi.map entry as well. Ditto for the next patch.

Signature · 1 line
Jens Axboe
#3 lore raw

PATCH 2/5 liburing: Add helper to prepare IORING_OP_LISTEN command

In reply to Gabriel Krisman Bertazi
---
 src/include/liburing.h          | 6 ++++++
 src/include/liburing/io_uring.h | 1 +
 2 files changed, 7 insertions(+)
src/include/liburing.h +6 -0
index 818e27c..52d732a 100644
@@ -676,6 +676,12 @@ IOURINGINLINE void io_uring_prep_bind(struct io_uring_sqe *sqe, int fd,
 	io_uring_prep_rw(IORING_OP_BIND, sqe, fd, addr, 0, addrlen);
 }
 
+IOURINGINLINE void io_uring_prep_listen(struct io_uring_sqe *sqe, int fd,
+				      int backlog)
+{
+	io_uring_prep_rw(IORING_OP_LISTEN, sqe, fd, 0, backlog, 0);
+}
+
 IOURINGINLINE void io_uring_prep_files_update(struct io_uring_sqe *sqe,
 					      int *fds, unsigned nr_fds,
 					      int offset)
src/include/liburing/io_uring.h +1 -0
index 177ace6..f99d41f 100644
@@ -258,6 +258,7 @@ enum io_uring_op {
 	IORING_OP_FIXED_FD_INSTALL,
 	IORING_OP_FTRUNCATE,
 	IORING_OP_BIND,
+	IORING_OP_LISTEN,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
Signature · 1 line
2.44.0
#4 lore raw

PATCH 3/5 tests: Add test for bind/listen commands

In reply to Gabriel Krisman Bertazi

This is implemented as a server/client tool that only uses io_uring commands for setup.

---
 test/Makefile      |   1 +
 test/bind-listen.c | 231 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 232 insertions(+)
 create mode 100644 test/bind-listen.c
test/Makefile +1 -0
index fcf6554..a47ca6f 100644
@@ -52,6 +52,7 @@ test_srcs := \
 	across-fork.c \
 	b19062a56726.c \
 	b5837bd5311d.c \
+	bind-listen.c \
 	buf-ring.c \
 	buf-ring-nommap.c \
 	buf-ring-put.c \
test/bind-listen.c new file +231 -0

231 lines not on this page · raw message

Signature · 1 line
2.44.0
maintainer
#5 lore raw

Re: [PATCH liburing 3/5] tests: Add test for bind/listen commands

Quoted 46 lines · Gabriel Krisman Bertazi
test/bind-listen.c new file +46 -0
+static void *do_server(void *data)
+{
+	struct srv_data *rd = data;
+
+	struct io_uring_params p = { };
+	struct __kernel_timespec ts;
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	struct io_uring ring;
+	int ret, conn, sock_index;
+	unsigned head;
+	int fd, val;
+	char buf[1024];
+
+	ret = t_create_ring_params(4, &ring, &p);
+	if (ret < 0) {
+		fprintf(stderr, "queue_init: %s\n", strerror(-ret));
+		goto err;
+	}
+
+	ret = io_uring_register_files(&ring, &fd, 1);
+	if (ret) {
+		fprintf(stderr, "file register %d\n", ret);
+		goto err;
+	}
+
+	memset(&server_addr, 0, sizeof(struct sockaddr_in));
+	server_addr.sin_family = AF_INET;
+	server_addr.sin_port = htons(8000);
+	server_addr.sin_addr.s_addr = htons(INADDR_ANY);
+
+	sock_index = 0;
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_socket_direct(sqe, AF_INET, SOCK_STREAM, 0,
+				    sock_index, 0);
+
+	sqe = io_uring_get_sqe(&ring);
+	val = 1;
+	io_uring_prep_cmd_sock(sqe, SOCKET_URING_OP_SETSOCKOPT, 0,
+			       SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_bind(sqe, sock_index, (struct sockaddr *) &server_addr,
+			   sizeof(struct sockaddr_in));
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;

test/bind-listen.c line 88

Quoted 10 lines
test/bind-listen.c new file +10 -0
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_listen(sqe, sock_index, 1);
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
+
+	ret = io_uring_submit(&ring);
+	if (ret < 0) {
+		printf("submission failed. %d\n", ret);
+		goto err;
+	}

Did you test this on older kernels? I suspect they will fail without IORING_SETUP_SUBMIT_ALL set, and they will fail even if set for kernels that don't support bind/listen.

We need to reliably return T_EXIT_SKIP for kernels that don't support this feature.

Quoted 33 lines
test/bind-listen.c new file +33 -0
+static int do_client()
+{
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	struct sockaddr_in peer_addr;
+	socklen_t addr_len = sizeof(peer_addr);
+	struct io_uring ring;
+	int ret, fd = -1, sock_index;
+	int i;
+
+	ret = io_uring_queue_init(3, &ring, 0);
+	if (ret < 0) {
+		fprintf(stderr, "queue_init: %s\n", strerror(-ret));
+		return -1;
+	}
+
+	ret = io_uring_register_files(&ring, &fd, 1);
+	if (ret) {
+		fprintf(stderr, "file register %d\n", ret);
+		goto err;
+	}
+
+
+	peer_addr.sin_family = AF_INET;
+	peer_addr.sin_port = server_addr.sin_port;
+	peer_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+	sock_index = 0;
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_socket_direct(sqe, AF_INET, SOCK_STREAM, 0,
+				    sock_index, 0);
+
+	sqe = io_uring_get_sqe(&ring);

test/bind-listen.c line 188

Quoted 10 lines
test/bind-listen.c new file +10 -0
+	io_uring_prep_connect(sqe, sock_index, (struct sockaddr*) &peer_addr, addr_len);
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
+
+	sqe = io_uring_get_sqe(&ring);
+
+	io_uring_prep_send(sqe, sock_index, magic, strlen(magic), 0);
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
+
+	io_uring_submit(&ring);
+	io_uring_wait_cqe_nr(&ring, &cqe, 3);

Ditto for these, if socket/connect/send aren't supported.

Quoted 18 lines
test/bind-listen.c new file +18 -0
+
+	io_uring_for_each_cqe(&ring, i, cqe) {
+		if (cqe->res < 0) {
+			printf("client cqe. idx=%d, %d\n", i, cqe->res);
+		}
+	}
+	io_uring_cq_advance(&ring, 2);
+
+	return 0;
+err:
+	return -1;
+}
+
+int main(int argc, char *argv[])
+{
+	pthread_mutexattr_t attr;
+	pthread_t srv_thread;
+	struct srv_data srv_data;

test/bind-listen.c line 216

Quoted 10 lines
test/bind-listen.c new file +10 -0
+	int ret;
+	void *retval;
+
+	if (argc > 1)
+		return 0;
+
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_setpshared(&attr, 1);
+	pthread_mutex_init(&srv_data.mutex, &attr);
+	pthread_mutex_lock(&srv_data.mutex);

Probably be better with a barrier rather than a mutex?

Signature · 1 line
Jens Axboe
#6 lore raw

PATCH 4/5 man/io_uring_prep_bind.3: Document the IORING_OP_BIND operation

In reply to Gabriel Krisman Bertazi
---
 man/io_uring_prep_bind.3 | 54 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 man/io_uring_prep_bind.3
man/io_uring_prep_bind.3 new file +54 -0
new file mode 100644
index 0000000..e2a1cf9
@@ -0,0 +1,54 @@
+.\" Copyright (C) 2024 SUSE LLC
+.\"
+.\" SPDX-License-Identifier: LGPL-2.0-or-later
+.\"
+.TH io_uring_prep_bind 3 "Jun 3, 2024" "liburing-2.7" "liburing Manual"
+.SH NAME
+io_uring_prep_bind \- prepare a bind request
+.SH SYNOPSIS
+.nf
+.B #include <sys/socket.h>
+.B #include <liburing.h>
+.PP
+.BI "void io_uring_prep_bind(struct io_uring_sqe *" sqe ","
+.BI "                          int " sockfd ","
+.BI "                          struct sockaddr *" addr ","
+.BI "                          socklen_t " addrlen ");"
+.fi
+.SH DESCRIPTION
+The
+.BR io_uring_prep_bind (3)
+function prepares a bind request. The submission queue entry
+.I sqe
+is setup to assign the network address at
+.IR addr ,
+of length
+.IR addrlen ,
+to the socket descriptor
+.IR sockfd.
+
+This function prepares an async
+.BR bind (2)
+request. See that man page for details.
+
+.SH RETURN VALUE
+None
+.SH ERRORS
+The CQE
+.I res
+field will contain the result of the operation. See the related man page for
+details on possible values. Note that where synchronous system calls will return
+.B -1
+on failure and set
+.I errno
+to the actual error value, io_uring never uses
+.IR errno .
+Instead it returns the negated
+.I errno
+directly in the CQE
+.I res
+field.
+.SH SEE ALSO
+.BR io_uring_get_sqe (3),
+.BR io_uring_submit (3),
+.BR bind (2)
Signature · 1 line
2.44.0
#7 lore raw

PATCH 5/5 man/io_uring_prep_listen.3: Document IORING_OP_LISTEN operation

In reply to Gabriel Krisman Bertazi
---
 man/io_uring_prep_listen.3 | 52 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 man/io_uring_prep_listen.3
man/io_uring_prep_listen.3 new file +52 -0
new file mode 100644
index 0000000..b765298
@@ -0,0 +1,52 @@
+.\" Copyright (C) 2024 SUSE LLC.
+.\"
+.\" SPDX-License-Identifier: LGPL-2.0-or-later
+.\"
+.TH io_uring_prep_listen 3 "Jun 3, 2024" "liburing-2.7" "liburing Manual"
+.SH NAME
+io_uring_prep_listen \- prepare a listen request
+.SH SYNOPSIS
+.nf
+.B #include <sys/socket.h>
+.B #include <liburing.h>
+.PP
+.BI "void io_uring_prep_listen(struct io_uring_sqe *" sqe ","
+.BI "                          int " sockfd ","
+.BI "                          int" backlog ");"
+.fi
+.SH DESCRIPTION
+The
+.BR io_uring_prep_listen (3)
+function prepares a listen request. The submission queue entry
+.I sqe
+is setup to place the socket file descriptor pointed by
+.IR sockfd
+into a state to accept incoming connections.  The parameter
+.IR backlog ,
+defines the maximum length of the queue of pending connections.
+
+This function prepares an async
+.BR listen (2)
+request. See that man page for details.
+
+.SH RETURN VALUE
+None
+.SH ERRORS
+The CQE
+.I res
+field will contain the result of the operation. See the related man page for
+details on possible values. Note that where synchronous system calls will return
+.B -1
+on failure and set
+.I errno
+to the actual error value, io_uring never uses
+.IR errno .
+Instead it returns the negated
+.I errno
+directly in the CQE
+.I res
+field.
+.SH SEE ALSO
+.BR io_uring_get_sqe (3),
+.BR io_uring_submit (3),
+.BR listen (2)
Signature · 1 line
2.44.0