* [RFC liburing 0/5] IORING_OP_BIND/LISTEN support
@ 2024-06-04 0:04 Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command Gabriel Krisman Bertazi
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-06-04 0:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
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
--
2.44.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command
2024-06-04 0:04 [RFC liburing 0/5] IORING_OP_BIND/LISTEN support Gabriel Krisman Bertazi
@ 2024-06-04 0:04 ` Gabriel Krisman Bertazi
2024-07-21 21:18 ` Jens Axboe
2024-06-04 0:04 ` [PATCH liburing 2/5] liburing: Add helper to prepare IORING_OP_LISTEN command Gabriel Krisman Bertazi
` (3 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-06-04 0:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
src/include/liburing.h | 7 +++++++
src/include/liburing/io_uring.h | 1 +
2 files changed, 8 insertions(+)
diff --git a/src/include/liburing.h b/src/include/liburing.h
index 0a02364..818e27c 100644
--- a/src/include/liburing.h
+++ b/src/include/liburing.h
@@ -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)
diff --git a/src/include/liburing/io_uring.h b/src/include/liburing/io_uring.h
index 9330733..177ace6 100644
--- a/src/include/liburing/io_uring.h
+++ b/src/include/liburing/io_uring.h
@@ -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,
--
2.44.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH liburing 2/5] liburing: Add helper to prepare IORING_OP_LISTEN command
2024-06-04 0:04 [RFC liburing 0/5] IORING_OP_BIND/LISTEN support Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command Gabriel Krisman Bertazi
@ 2024-06-04 0:04 ` Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 3/5] tests: Add test for bind/listen commands Gabriel Krisman Bertazi
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-06-04 0:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
src/include/liburing.h | 6 ++++++
src/include/liburing/io_uring.h | 1 +
2 files changed, 7 insertions(+)
diff --git a/src/include/liburing.h b/src/include/liburing.h
index 818e27c..52d732a 100644
--- a/src/include/liburing.h
+++ b/src/include/liburing.h
@@ -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)
diff --git a/src/include/liburing/io_uring.h b/src/include/liburing/io_uring.h
index 177ace6..f99d41f 100644
--- a/src/include/liburing/io_uring.h
+++ b/src/include/liburing/io_uring.h
@@ -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,
--
2.44.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH liburing 3/5] tests: Add test for bind/listen commands
2024-06-04 0:04 [RFC liburing 0/5] IORING_OP_BIND/LISTEN support Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 2/5] liburing: Add helper to prepare IORING_OP_LISTEN command Gabriel Krisman Bertazi
@ 2024-06-04 0:04 ` Gabriel Krisman Bertazi
2024-07-21 21:21 ` Jens Axboe
2024-06-04 0:04 ` [PATCH liburing 4/5] man/io_uring_prep_bind.3: Document the IORING_OP_BIND operation Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 5/5] man/io_uring_prep_listen.3: Document IORING_OP_LISTEN operation Gabriel Krisman Bertazi
4 siblings, 1 reply; 8+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-06-04 0:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
This is implemented as a server/client tool that only uses io_uring
commands for setup.
Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
test/Makefile | 1 +
test/bind-listen.c | 231 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 232 insertions(+)
create mode 100644 test/bind-listen.c
diff --git a/test/Makefile b/test/Makefile
index fcf6554..a47ca6f 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -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 \
diff --git a/test/bind-listen.c b/test/bind-listen.c
new file mode 100644
index 0000000..cd1e386
--- /dev/null
+++ b/test/bind-listen.c
@@ -0,0 +1,231 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * echo server using solely io_uring operations
+ */
+#include <stdio.h>
+#include <string.h>
+#include <liburing.h>
+#include <err.h>
+#include <sys/mman.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <netinet/ip.h>
+#include <pthread.h>
+
+#include "liburing.h"
+#include "helpers.h"
+
+static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec)
+{
+ ts->tv_sec = msec / 1000;
+ ts->tv_nsec = (msec % 1000) * 1000000;
+}
+
+struct srv_data {
+ pthread_mutex_t mutex;
+};
+struct sockaddr_in server_addr;
+
+const char *magic = "Hello World!";
+
+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;
+
+ 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;
+ }
+
+ msec_to_ts(&ts, 300);
+ ret = io_uring_wait_cqes(&ring, &cqe, 4, &ts, NULL);
+ if (ret < 0) {
+ printf("submission failed. %d\n", ret);
+ goto err;
+ }
+
+ io_uring_for_each_cqe(&ring, head, cqe) {
+ if (cqe->res < 0) {
+ printf("Server startup failed. step %d got %d \n", head, cqe->res);
+ goto err;
+ }
+ }
+ io_uring_cq_advance(&ring, 4);
+
+ pthread_mutex_unlock(&rd->mutex);
+
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_accept(sqe, sock_index, NULL, NULL, 0);
+ sqe->flags |= IOSQE_FIXED_FILE;
+
+ io_uring_submit(&ring);
+ io_uring_wait_cqe(&ring, &cqe);
+
+
+ if (cqe->res < 0) {
+ printf("accept failed. %d\n", cqe->res);
+ goto err;
+ }
+ conn = cqe->res;
+ io_uring_cqe_seen(&ring, cqe);
+
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_recv(sqe, conn, buf, BUFSIZ, 0);
+ io_uring_submit(&ring);
+ io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
+
+ if (cqe->res < 0) {
+ printf("bad receive cqe. %d\n", cqe->res);
+ goto err;
+ }
+ val = cqe->res;
+
+ if (val != strlen(magic) || strncmp(buf, magic, val)) {
+ printf("didn't receive expected string\n");
+ ret = -1;
+ goto err;
+ }
+
+ io_uring_queue_exit(&ring);
+
+ ret = 0;
+err:
+ return (void *)(intptr_t)ret;
+}
+
+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);
+ 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);
+
+ 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;
+ 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);
+
+ ret = pthread_create(&srv_thread, NULL, do_server, &srv_data);
+ if (ret) {
+ fprintf(stderr, "Thread create failed: %d\n", ret);
+ pthread_mutex_unlock(&srv_data.mutex);
+ return 1;
+ }
+ pthread_mutex_lock(&srv_data.mutex);
+ do_client();
+
+ pthread_join(srv_thread, &retval);
+ return (intptr_t)retval;
+}
+
+
--
2.44.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH liburing 4/5] man/io_uring_prep_bind.3: Document the IORING_OP_BIND operation
2024-06-04 0:04 [RFC liburing 0/5] IORING_OP_BIND/LISTEN support Gabriel Krisman Bertazi
` (2 preceding siblings ...)
2024-06-04 0:04 ` [PATCH liburing 3/5] tests: Add test for bind/listen commands Gabriel Krisman Bertazi
@ 2024-06-04 0:04 ` Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 5/5] man/io_uring_prep_listen.3: Document IORING_OP_LISTEN operation Gabriel Krisman Bertazi
4 siblings, 0 replies; 8+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-06-04 0:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
man/io_uring_prep_bind.3 | 54 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
create mode 100644 man/io_uring_prep_bind.3
diff --git a/man/io_uring_prep_bind.3 b/man/io_uring_prep_bind.3
new file mode 100644
index 0000000..e2a1cf9
--- /dev/null
+++ b/man/io_uring_prep_bind.3
@@ -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)
--
2.44.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH liburing 5/5] man/io_uring_prep_listen.3: Document IORING_OP_LISTEN operation
2024-06-04 0:04 [RFC liburing 0/5] IORING_OP_BIND/LISTEN support Gabriel Krisman Bertazi
` (3 preceding siblings ...)
2024-06-04 0:04 ` [PATCH liburing 4/5] man/io_uring_prep_bind.3: Document the IORING_OP_BIND operation Gabriel Krisman Bertazi
@ 2024-06-04 0:04 ` Gabriel Krisman Bertazi
4 siblings, 0 replies; 8+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-06-04 0:04 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
man/io_uring_prep_listen.3 | 52 ++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)
create mode 100644 man/io_uring_prep_listen.3
diff --git a/man/io_uring_prep_listen.3 b/man/io_uring_prep_listen.3
new file mode 100644
index 0000000..b765298
--- /dev/null
+++ b/man/io_uring_prep_listen.3
@@ -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)
--
2.44.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command
2024-06-04 0:04 ` [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command Gabriel Krisman Bertazi
@ 2024-07-21 21:18 ` Jens Axboe
0 siblings, 0 replies; 8+ messages in thread
From: Jens Axboe @ 2024-07-21 21:18 UTC (permalink / raw)
To: Gabriel Krisman Bertazi; +Cc: io-uring
On 6/3/24 6:04 PM, Gabriel Krisman Bertazi wrote:
> Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
Finally got around to these, as the kernel support has landed...
> diff --git a/src/include/liburing.h b/src/include/liburing.h
> index 0a02364..818e27c 100644
> --- a/src/include/liburing.h
> +++ b/src/include/liburing.h
> @@ -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.
--
Jens Axboe
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH liburing 3/5] tests: Add test for bind/listen commands
2024-06-04 0:04 ` [PATCH liburing 3/5] tests: Add test for bind/listen commands Gabriel Krisman Bertazi
@ 2024-07-21 21:21 ` Jens Axboe
0 siblings, 0 replies; 8+ messages in thread
From: Jens Axboe @ 2024-07-21 21:21 UTC (permalink / raw)
To: Gabriel Krisman Bertazi; +Cc: io-uring
On 6/3/24 6:04 PM, Gabriel Krisman Bertazi wrote:
> +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;
> +
> + 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.
> +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);
> + 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.
> +
> + 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;
> + 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?
--
Jens Axboe
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-07-21 21:21 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-04 0:04 [RFC liburing 0/5] IORING_OP_BIND/LISTEN support Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 1/5] liburing: Add helper to prepare IORING_OP_BIND command Gabriel Krisman Bertazi
2024-07-21 21:18 ` Jens Axboe
2024-06-04 0:04 ` [PATCH liburing 2/5] liburing: Add helper to prepare IORING_OP_LISTEN command Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 3/5] tests: Add test for bind/listen commands Gabriel Krisman Bertazi
2024-07-21 21:21 ` Jens Axboe
2024-06-04 0:04 ` [PATCH liburing 4/5] man/io_uring_prep_bind.3: Document the IORING_OP_BIND operation Gabriel Krisman Bertazi
2024-06-04 0:04 ` [PATCH liburing 5/5] man/io_uring_prep_listen.3: Document IORING_OP_LISTEN operation Gabriel Krisman Bertazi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox