Add basic support for a sync MSG_RING without having a source ring
Normally MSG_RING requires both a source and a destination ring. But
some users don't always have a ring avilable to send a message from,
yet they still need to notify a target ring.
Add support for using io_uring_register(2) without having a source
ring, using a file descriptor of -1 for that. Implement
IORING_REGISTER_SEND_MSG_RING, which simply takes an sqe that the
application can put on the stack and use the normal liburing helpers
to get it setup. For an application, it may look like:
struct io_uring_sqe sqe = { };
io_uring_prep_msg_ring(&sqe, target_ring->ring_fd, len, data, flags);
ret = io_uring_register_sync_msg_ring(&sqe);
and 'ret' here would be what you would've seen in cqe->res if the
MSG_RING request was issued in the usual fashion as an sqe that was
gotten from a source ring.
On the destination side, no changes should be observed compared to
sending a normal sqe based async MSG_RING request.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/src/include/liburing.h b/src/include/liburing.h
index 7a8337b..52f2fb8 100644
--- a/src/include/liburing.h
+++ b/src/include/liburing.h
@@ -250,6 +250,7 @@
int io_uring_buf_ring_head(struct io_uring *ring, int buf_group, uint16_t *head);
int io_uring_register_sync_cancel(struct io_uring *ring,
struct io_uring_sync_cancel_reg *reg);
+int io_uring_register_sync_msg_ring(struct io_uring_sqe *sqe);
int io_uring_register_file_alloc_range(struct io_uring *ring,
unsigned off, unsigned len);
diff --git a/src/include/liburing/io_uring.h b/src/include/liburing/io_uring.h
index 4ac13f7..05e99d9 100644
--- a/src/include/liburing/io_uring.h
+++ b/src/include/liburing/io_uring.h
@@ -612,6 +612,8 @@
/* clone registered buffers from source ring to current ring */
IORING_REGISTER_CLONE_BUFFERS = 30,
+ IORING_REGISTER_SEND_MSG_RING = 31,
+
/* this goes last */
IORING_REGISTER_LAST,
diff --git a/src/liburing-ffi.map b/src/liburing-ffi.map
index 66fcef7..f072add 100644
--- a/src/liburing-ffi.map
+++ b/src/liburing-ffi.map
@@ -211,4 +211,5 @@
io_uring_submit_and_wait_min_timeout;
io_uring_wait_cqes_min_timeout;
io_uring_clone_buffers;
+ io_uring_register_sync_msg_ring;
} LIBURING_2.7;
diff --git a/src/liburing.map b/src/liburing.map
index c13cd36..5d16684 100644
--- a/src/liburing.map
+++ b/src/liburing.map
@@ -102,4 +102,5 @@
io_uring_submit_and_wait_min_timeout;
io_uring_wait_cqes_min_timeout;
io_uring_clone_buffers;
+ io_uring_register_sync_msg_ring;
} LIBURING_2.7;
diff --git a/src/register.c b/src/register.c
index 4fa2255..3d4ab91 100644
--- a/src/register.c
+++ b/src/register.c
@@ -408,3 +408,8 @@
return do_register(dst, IORING_REGISTER_CLONE_BUFFERS, &buf, 1);
}
+
+int io_uring_register_sync_msg_ring(struct io_uring_sqe *sqe)
+{
+ return __sys_io_uring_register(-1, IORING_REGISTER_SEND_MSG_RING, sqe, 1);
+}
diff --git a/test/Makefile b/test/Makefile
index ceabf59..2038976 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -133,6 +133,7 @@
msg-ring-fd.c \
msg-ring-flags.c \
msg-ring-overflow.c \
+ msg-ring-sync.c \
multicqes_drain.c \
napi-test.c \
no-mmap-inval.c \
diff --git a/test/msg-ring-sync.c b/test/msg-ring-sync.c
new file mode 100644
index 0000000..f09e5d8
--- /dev/null
+++ b/test/msg-ring-sync.c
@@ -0,0 +1,210 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Description: test ring messaging command
+ *
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <pthread.h>
+
+#include "liburing.h"
+#include "helpers.h"
+
+static int no_msg;
+
+struct data {
+ struct io_uring *ring;
+ unsigned int flags;
+ pthread_barrier_t startup;
+ pthread_barrier_t barrier;
+};
+
+static void *wait_cqe_fn(void *__data)
+{
+ struct data *d = __data;
+ struct io_uring_cqe *cqe;
+ struct io_uring ring;
+ int ret;
+
+ ret = io_uring_queue_init(4, &ring, d->flags);
+ d->ring = ˚
+ pthread_barrier_wait(&d->startup);
+
+ pthread_barrier_wait(&d->barrier);
+
+ if (ret == -EINVAL)
+ goto skip;
+
+ ret = io_uring_wait_cqe(&ring, &cqe);
+ if (ret) {
+ fprintf(stderr, "wait cqe %d\n", ret);
+ goto err_no_cqe;
+ }
+
+ if (cqe->user_data != 0x5aa5) {
+ fprintf(stderr, "user_data %llx\n", (long long) cqe->user_data);
+ goto err;
+ }
+ if (cqe->res != 0x20) {
+ fprintf(stderr, "len %x\n", cqe->res);
+ goto err;
+ }
+
+ io_uring_cqe_seen(&ring, cqe);
+skip:
+ io_uring_queue_exit(&ring);
+ return NULL;
+err:
+ io_uring_cqe_seen(&ring, cqe);
+err_no_cqe:
+ io_uring_queue_exit(&ring);
+ return (void *) (unsigned long) 1;
+}
+
+static int test_remote(unsigned int ring_flags)
+{
+ struct io_uring *target;
+ pthread_t thread;
+ void *tret;
+ struct io_uring_sqe sqe = { };
+ struct data d;
+ int ret;
+
+ d.flags = ring_flags;
+ pthread_barrier_init(&d.barrier, NULL, 2);
+ pthread_barrier_init(&d.startup, NULL, 2);
+ pthread_create(&thread, NULL, wait_cqe_fn, &d);
+
+ pthread_barrier_wait(&d.startup);
+ target = d.ring;
+
+ io_uring_prep_msg_ring(&sqe, target->ring_fd, 0x20, 0x5aa5, 0);
+ sqe.user_data = 1;
+
+ ret = io_uring_register_sync_msg_ring(&sqe);
+ if (ret == -EINVAL) {
+ no_msg = 1;
+ return T_EXIT_SKIP;
+ }
+ if (ret < 0) {
+ fprintf(stderr, "send_msg_ring_sync %d\n", ret);
+ goto err;
+ }
+
+ pthread_barrier_wait(&d.barrier);
+
+ if (ret != 0) {
+ fprintf(stderr, "res %d\n", ret);
+ return -1;
+ }
+ pthread_join(thread, &tret);
+ return 0;
+err:
+ return 1;
+}
+
+static int test_invalid(void)
+{
+ struct io_uring_sqe sqe = { };
+ int ret;
+
+ io_uring_prep_msg_ring(&sqe, 1, 0, 0x8989, 0);
+ sqe.user_data = 1;
+
+ ret = io_uring_register_sync_msg_ring(&sqe);
+
+ if (ret != -EBADFD) {
+ fprintf(stderr, "res %d\n", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int test_disabled_ring(int flags)
+{
+ struct io_uring_sqe sqe = { };
+ struct io_uring disabled_ring;
+ int ret;
+
+ flags |= IORING_SETUP_R_DISABLED;
+ ret = io_uring_queue_init(8, &disabled_ring, flags);
+ if (ret) {
+ fprintf(stderr, "ring setup failed: %d\n", ret);
+ return 1;
+ }
+
+ io_uring_prep_msg_ring(&sqe, disabled_ring.ring_fd, 0x10, 0x1234, 0);
+ sqe.user_data = 1;
+
+ ret = io_uring_register_sync_msg_ring(&sqe);
+ if (ret != 0 && ret != -EBADFD) {
+ fprintf(stderr, "res %d\n", ret);
+ return 1;
+ }
+
+ return 0;
+}
+
+static int test(int ring_flags)
+{
+ int ret;
+
+ ret = test_invalid();
+ if (ret) {
+ fprintf(stderr, "test_invalid failed\n");
+ return T_EXIT_FAIL;
+ }
+
+ ret = test_remote(ring_flags);
+ if (ret) {
+ fprintf(stderr, "test_remote failed\n");
+ return T_EXIT_FAIL;
+ }
+
+ ret = test_remote(ring_flags | IORING_SETUP_IOPOLL);
+ if (ret) {
+ fprintf(stderr, "test_remote failed\n");
+ return T_EXIT_FAIL;
+ }
+
+ if (test_disabled_ring(0)) {
+ fprintf(stderr, "test_disabled_ring failed\n");
+ return T_EXIT_FAIL;
+ }
+
+ if (test_disabled_ring(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN)) {
+ fprintf(stderr, "test_disabled_ring defer failed\n");
+ return T_EXIT_FAIL;
+ }
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ int ret;
+
+ if (argc > 1)
+ return T_EXIT_SKIP;
+
+ ret = test(0);
+ if (ret != T_EXIT_PASS) {
+ fprintf(stderr, "ring flags 0 failed\n");
+ return ret;
+ }
+ if (no_msg)
+ return T_EXIT_SKIP;
+
+ ret = test(IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_DEFER_TASKRUN);
+ if (ret != T_EXIT_PASS) {
+ fprintf(stderr, "ring flags defer failed\n");
+ return ret;
+ }
+
+ return ret;
+}