liburing: multishot receive
v3 cover liburing: multishot receive
This adds an API, tests and documentation for the multi shot receive functionality.
It also adds some testing for overflow paths in accept & poll which previously was not tested.
Patch 1 adds a helper t_create_socket_pair which provides two connected sockets without needing a hard coded port
Patch 2-5 adds multishot recv api, tests and docs
Patch 6,7 add tests for overflow in poll
v3:
* remove TCP_NODELAY stuff from patch 1 as it was useless
* fix return codes from recv-multishot test
* use ioprio field for multishot
v2:
* added a multishot recv api rather than expecting applications to use the flags directly
* fixed the io_uring.h include
* added overflow tests for recv multishot
* added tests for poll/accept
Dylan Yudaken (7):
add t_create_socket_pair
add IORING_RECV_MULTISHOT to io_uring.h
add io_uring_prep_(recv|recvmsg)_multishot
add IORING_RECV_MULTISHOT docs
add recv-multishot test
add poll overflow test
add accept with overflow test
man/io_uring_prep_recv.3 | 22 ++ man/io_uring_prep_recv_multishot.3 | 1 + man/io_uring_prep_recvmsg.3 | 20 ++ man/io_uring_prep_recvmsg_multishot.3 | 1 + src/include/liburing.h | 16 ++ src/include/liburing/io_uring.h | 5 + test/Makefile | 2 + test/accept.c | 129 +++++++--- test/helpers.c | 90 +++++++ test/helpers.h | 5 + test/poll-mshot-overflow.c | 128 ++++++++++ test/recv-multishot.c | 343 ++++++++++++++++++++++++++ 12 files changed, 734 insertions(+), 28 deletions(-) create mode 120000 man/io_uring_prep_recv_multishot.3 create mode 120000 man/io_uring_prep_recvmsg_multishot.3 create mode 100644 test/poll-mshot-overflow.c create mode 100644 test/recv-multishot.c
base-commit: 6c90ac3c9fb874d406656c9e7a68805c83a055a0
Signature · 1 line
2.30.2
PATCH v3 1/7 add t_create_socket_pair
This is a useful tool for making networking tests, and does not require a hard coded port which is useful
--- test/helpers.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/helpers.h | 5 +++ 2 files changed, 95 insertions(+)
test/helpers.c +90 -0
@@ -10,6 +10,10 @@#include <unistd.h> #include <sys/types.h> +#include <arpa/inet.h> +#include <netinet/ip.h> +#include <netinet/tcp.h> +#include "helpers.h" #include "liburing.h"
@@ -143,3 +147,89 @@ enum t_setup_ret t_register_buffers(struct io_uring *ring,fprintf (stderr,"buffer register failed: %s\n" ,strerror (-ret));return ret; } + +int t_create_socket_pair (int fd[2 ],bool stream) +{ +int ret; +int type = stream ?SOCK_STREAM :SOCK_DGRAM ; +int val; +struct sockaddr_in serv_addr; +struct sockaddr *paddr; +size_t paddrlen; + + type |=SOCK_CLOEXEC ; + fd[0 ] =socket (AF_INET , type,0 ); +if (fd[0 ] <0 ) +return errno; + fd[1 ] =socket (AF_INET , type,0 ); +if (fd[1 ] <0 ) { + ret = errno; +close (fd[0 ]); +return ret; + } + + val =1 ; +if (setsockopt (fd[0 ],SOL_SOCKET ,SO_REUSEADDR , &val,sizeof (val))) + goto errno_cleanup; + +memset (&serv_addr,0 ,sizeof (serv_addr)); + serv_addr.sin_family =AF_INET ; + serv_addr.sin_port =0 ; +inet_pton (AF_INET ,"127.0.0.1" , &serv_addr.sin_addr); + + paddr = (struct sockaddr *)&serv_addr; + paddrlen =sizeof (serv_addr); + +if (bind (fd[0 ], paddr, paddrlen)) { +fprintf (stderr,"bind failed\n" ); + goto errno_cleanup; + } + +if (stream &&listen (fd[0 ],16 )) { +fprintf (stderr,"listen failed\n" ); + goto errno_cleanup; + } + +if (getsockname (fd[0 ], &serv_addr, (socklen_t *)&paddrlen)) { +fprintf (stderr,"getsockname failed\n" ); + goto errno_cleanup; + } +inet_pton (AF_INET ,"127.0.0.1" , &serv_addr.sin_addr); + +if (connect (fd[1 ], &serv_addr, paddrlen)) { +fprintf (stderr,"connect failed\n" ); + goto errno_cleanup; + } + +if (!stream) { +/* connect the other udp side */ +if (getsockname (fd[1 ], &serv_addr, (socklen_t *)&paddrlen)) { +fprintf (stderr,"getsockname failed\n" ); + goto errno_cleanup; + } +inet_pton (AF_INET ,"127.0.0.1" , &serv_addr.sin_addr); + +if (connect (fd[0 ], &serv_addr, paddrlen)) { +fprintf (stderr,"connect failed\n" ); + goto errno_cleanup; + } +return 0 ; + } + +/* for stream case we must accept and cleanup the listen socket */ + + ret =accept (fd[0 ],NULL ,NULL ); +if (ret <0 ) + goto errno_cleanup; + +close (fd[0 ]); + fd[0 ] = ret; + +return 0 ; + +errno_cleanup: + ret = errno; +close (fd[0 ]); +close (fd[1 ]); +return ret; +}
test/helpers.h +5 -0
@@ -59,6 +59,11 @@ void t_create_file_pattern(const char *file, size_t size, char pattern);*/ struct iovec *t_create_buffers (size_t buf_num,size_t buf_size); +/* +* Helper for creating connected socket pairs +*/ +int t_create_socket_pair (int fd[2 ],bool stream); +/* * Helper for setting up a ring and checking for user privs */
Signature · 1 line
2.30.2
PATCH v3 2/7 add IORING_RECV_MULTISHOT to io_uring.h
In reply to Dylan Yudakencopy relevant part from include/uapi/linux/io_uring.h from for-5.20/io_uring branch
--- src/include/liburing/io_uring.h | 5 +++++ 1 file changed, 5 insertions(+)
src/include/liburing/io_uring.h +5 -0
@@ -244,8 +244,13 @@ enum io_uring_op {* or receive and arm poll if that yields an * -EAGAIN result, arm poll upfront and skip * the initial transfer attempt. +* +* IORING_RECV_MULTISHOT Multishot recv. Sets IORING_CQE_F_MORE if +* the handler will continue to report +* CQEs on behalf of the same SQE. */ #define IORING_RECVSEND_POLL_FIRST (1U << 0) +#define IORING_RECV_MULTISHOT (1U << 1)/* * accept flags stored in sqe->ioprio
Signature · 1 line
2.30.2
PATCH v3 3/7 add io_uring_prep_(recv|recvmsg)_multishot
In reply to Dylan YudakenAdd a nice API for multishot recv and recvmsg.
--- src/include/liburing.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
src/include/liburing.h +16 -0
@@ -416,6 +416,14 @@ static inline void io_uring_prep_recvmsg(struct io_uring_sqe *sqe, int fd, sqe->msg_flags = flags; } +static inline void io_uring_prep_recvmsg_multishot (struct io_uring_sqe *sqe, +int fd,struct msghdr *msg, +unsigned flags) +{ +io_uring_prep_recvmsg (sqe, fd, msg, flags); + sqe->ioprio |=IORING_RECV_MULTISHOT ; +} +static inline void io_uring_prep_sendmsg (struct io_uring_sqe *sqe,int fd,const struct msghdr *msg,unsigned flags)
@@ -674,6 +682,14 @@ static inline void io_uring_prep_recv(struct io_uring_sqe *sqe, int sockfd, sqe->msg_flags = (__u32 ) flags; } +static inline void io_uring_prep_recv_multishot (struct io_uring_sqe *sqe, +int sockfd,void *buf, +size_t len,int flags) +{ +io_uring_prep_recv (sqe, sockfd, buf, len, flags); + sqe->ioprio |=IORING_RECV_MULTISHOT ; +} +static inline void io_uring_prep_openat2 (struct io_uring_sqe *sqe,int dfd,const char *path,struct open_how *how) {
Signature · 1 line
2.30.2
PATCH v3 4/7 add IORING_RECV_MULTISHOT docs
In reply to Dylan Yudakenadd appropriate docs to io_uring_prep_recvmsg/io_uring_prep_recv man pages
--- man/io_uring_prep_recv.3 | 22 ++++++++++++++++++++++ man/io_uring_prep_recv_multishot.3 | 1 + man/io_uring_prep_recvmsg.3 | 20 ++++++++++++++++++++ man/io_uring_prep_recvmsg_multishot.3 | 1 + 4 files changed, 44 insertions(+) create mode 120000 man/io_uring_prep_recv_multishot.3 create mode 120000 man/io_uring_prep_recvmsg_multishot.3
man/io_uring_prep_recv.3 +22 -0
@@ -14,6 +14,12 @@ io_uring_prep_recv \- prepare a recv request .BI " void *" buf "," .BI " size_t " len "," .BI " int " flags ");" +.PP +.BI "void io_uring_prep_recv_multishot(struct io_uring_sqe *" sqe "," +.BI " int " sockfd "," +.BI " void *" buf "," +.BI " size_t " len "," +.BI " int " flags ");" .fi .SH DESCRIPTION .PP
@@ -36,6 +42,22 @@ This function prepares an async request. See that man page for details on the arguments specified to this prep helper. +The multishot version allows the application to issue a single receive request, +which repeatedly posts a CQE when data is available. It requires length to be 0 +, the +.B IOSQE_BUFFER_SELECT +flag to be set and no +.B MSG_WAITALL +flag to be set. +Therefore each CQE will take a buffer out of a provided buffer pool for receiving. +The application should check the flags of each CQE, regardless of it's result. +If a posted CQE does not have the +.B IORING_CQE_F_MORE +flag set then the multishot receive will be done and the application should issue a +new request. +Multishot variants are available since kernel 5.20. + + After calling this function, additional io_uring internal modifier flags may be set in the SQE .I off
man/io_uring_prep_recv_multishot.3 new file +1 -0
@@ -0,0 +1 @@ +io_uring_prep_recv.3
man/io_uring_prep_recvmsg.3 +20 -0
@@ -15,6 +15,11 @@ io_uring_prep_recvmsg \- prepare a recvmsg request .BI " int " fd "," .BI " struct msghdr *" msg "," .BI " unsigned " flags ");" +.PP +.BI "void io_uring_prep_recvmsg_multishot(struct io_uring_sqe *" sqe "," +.BI " int " fd "," +.BI " struct msghdr *" msg "," +.BI " unsigned " flags ");" .fi .SH DESCRIPTION .PP
@@ -37,6 +42,21 @@ This function prepares an async request. See that man page for details on the arguments specified to this prep helper. +The multishot version allows the application to issue a single receive request, +which repeatedly posts a CQE when data is available. It requires length to be 0 +, the +.B IOSQE_BUFFER_SELECT +flag to be set and no +.B MSG_WAITALL +flag to be set. +Therefore each CQE will take a buffer out of a provided buffer pool for receiving. +The application should check the flags of each CQE, regardless of it's result. +If a posted CQE does not have the +.B IORING_CQE_F_MORE +flag set then the multishot receive will be done and the application should issue a +new request. +Multishot variants are available since kernel 5.20. + After calling this function, additional io_uring internal modifier flags may be set in the SQE .I off
man/io_uring_prep_recvmsg_multishot.3 new file +1 -0
@@ -0,0 +1 @@ +io_uring_prep_recvmsg.3
Signature · 1 line
2.30.2
PATCH v3 5/7 add recv-multishot test
In reply to Dylan Yudakenadd a test for multishot receive functionality
--- test/Makefile | 1 + test/recv-multishot.c | 343 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 344 insertions(+) create mode 100644 test/recv-multishot.c
test/Makefile +1 -0
@@ -125,6 +125,7 @@ test_srcs := \ read-write.c \ recv-msgall.c \ recv-msgall-stream.c \ + recv-multishot.c \ register-restrictions.c \ rename.c \ ringbuf-read.c \
test/recv-multishot.c new file +343 -0
343 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v3 6/7 add poll overflow test
In reply to Dylan YudakenAdd a test that when CQE overflows, multishot poll doesn't give out of order completions.
--- test/Makefile | 1 + test/poll-mshot-overflow.c | 128 +++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 test/poll-mshot-overflow.c
test/Makefile +1 -0
@@ -117,6 +117,7 @@ test_srcs := \ poll-link.c \ poll-many.c \ poll-mshot-update.c \ + poll-mshot-overflow.c \ poll-ring.c \ poll-v-poll.c \ pollfree.c \
test/poll-mshot-overflow.c new file +128 -0
128 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v3 7/7 add accept with overflow test
In reply to Dylan Yudakenadd test to exercise the overflow codepath for multishot accept. this doesn't actually fail previously, but does at least exerceise the codepath and ensure that some invariants hold wrt flags and IORING_CQE_F_MORE.
--- test/accept.c | 129 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 101 insertions(+), 28 deletions(-)
test/accept.c +101 -28
@@ -24,6 +24,9 @@#include "liburing.h" #define MAX_FDS 32 +#define NOP_USER_DATA (1LLU << 50) +#define INITIAL_USER_DATA 1000 +static int no_accept;static int no_accept_multi;
@@ -39,6 +42,7 @@ struct accept_test_args {bool queue_accept_before_connect;bool multishot;int extra_loops; +bool overflow; };static void close_fds (int fds[],int nr)
@@ -86,6 +90,24 @@ static void queue_recv(struct io_uring *ring, int fd, bool fixed) sqe->flags |=IOSQE_FIXED_FILE ; } +static void queue_accept_multishot (struct io_uring *ring,int fd, +int idx,bool fixed) +{ +struct io_uring_sqe *sqe =io_uring_get_sqe (ring); +int ret; + +if (fixed) +io_uring_prep_multishot_accept_direct (sqe, fd, +NULL ,NULL , +0 ); +else +io_uring_prep_multishot_accept (sqe, fd,NULL ,NULL ,0 ); + +io_uring_sqe_set_data64 (sqe, idx); + ret =io_uring_submit (ring); +assert (ret !=-1 ); +} +static void queue_accept_conn (struct io_uring *ring,int fd,struct accept_test_args args) {
@@ -93,40 +115,51 @@ static void queue_accept_conn(struct io_uring *ring, int fd,int ret;int fixed_idx = args.fixed ?0 :-1 ;int count =1 + args.extra_loops; -bool multishot = args.multishot; + +if (args.multishot) { +queue_accept_multishot (ring, fd,INITIAL_USER_DATA , args.fixed); +return ; + }while (count--) { sqe =io_uring_get_sqe (ring);if (fixed_idx <0 ) { -if (!multishot) -io_uring_prep_accept (sqe, fd,NULL ,NULL ,0 ); -else -io_uring_prep_multishot_accept (sqe, fd,NULL , -NULL ,0 ); +io_uring_prep_accept (sqe, fd,NULL ,NULL ,0 ); }else { -if (!multishot) -io_uring_prep_accept_direct (sqe, fd,NULL ,NULL , -0 , fixed_idx); -else -io_uring_prep_multishot_accept_direct (sqe, fd, -NULL ,NULL , -0 ); +io_uring_prep_accept_direct (sqe, fd,NULL ,NULL , +0 , fixed_idx); } - ret =io_uring_submit (ring);assert (ret !=-1 ); } } -static int accept_conn (struct io_uring *ring,int fixed_idx,bool multishot) +static int accept_conn (struct io_uring *ring,int fixed_idx,int *multishot,int fd) { -struct io_uring_cqe *cqe; +struct io_uring_cqe *pcqe; +struct io_uring_cqe cqe;int ret; - ret =io_uring_wait_cqe (ring, &cqe); -assert (!ret); - ret = cqe->res; -io_uring_cqe_seen (ring, cqe); +do { + ret =io_uring_wait_cqe (ring, &pcqe); +assert (!ret); + cqe = *pcqe; +io_uring_cqe_seen (ring, pcqe); + }while (cqe.user_data ==NOP_USER_DATA ); + +if (*multishot) { +if (!(cqe.flags &IORING_CQE_F_MORE )) { + (*multishot)++; +queue_accept_multishot (ring, fd, *multishot, fixed_idx ==0 ); + }else { +if (cqe.user_data != *multishot) { +fprintf (stderr,"received multishot after told done!\n" ); +return -ECANCELED ; + } + } + } + + ret = cqe.res;if (fixed_idx >=0 ) {if (ret >0 ) {
@@ -203,6 +236,32 @@ static int set_client_fd(struct sockaddr_in *addr)return fd; } +static void cause_overflow (struct io_uring *ring) +{ +int i, ret; + +for (i =0 ; i < *ring->cq.kring_entries; i++) { +struct io_uring_sqe *sqe =io_uring_get_sqe (ring); + +io_uring_prep_nop (sqe); +io_uring_sqe_set_data64 (sqe,NOP_USER_DATA ); + ret =io_uring_submit (ring); +assert (ret !=-1 ); + } + +} + +static void clear_overflow (struct io_uring *ring) +{ +struct io_uring_cqe *cqe; + +while (!io_uring_peek_cqe (ring, &cqe)) { +if (cqe->user_data !=NOP_USER_DATA ) +break ; +io_uring_cqe_seen (ring, cqe); + } +} + static int test_loop(struct io_uring *ring, struct accept_test_args args, int recv_s0,
@@ -215,15 +274,22 @@ static int test_loop(struct io_uring *ring,bool multishot = args.multishot;uint32_t multishot_mask =0 ;int nr_fds = multishot ?MAX_FDS :1 ; +int multishot_idx = multishot ?INITIAL_USER_DATA :0 ; -for (i =0 ; i < nr_fds; i++) +if (args.overflow) +cause_overflow (ring); + +for (i =0 ; i < nr_fds; i++) { c_fd[i] =set_client_fd (addr); +if (args.overflow && i == nr_fds /2 ) +clear_overflow (ring); + }if (!args.queue_accept_before_connect)queue_accept_conn (ring, recv_s0, args);for (i =0 ; i < nr_fds; i++) { - s_fd[i] =accept_conn (ring, fixed ?0 :-1 , multishot); + s_fd[i] =accept_conn (ring, fixed ?0 :-1 , &multishot_idx, recv_s0);if (s_fd[i] == -EINVAL ) {if (args.accept_should_error) goto out;
@@ -527,14 +593,15 @@ static int test_accept(int count, bool before)return ret; } -static int test_multishot_accept (int count,bool before) +static int test_multishot_accept (int count,bool before,bool overflow) {struct io_uring m_io_uring;int ret;struct accept_test_args args = { .queue_accept_before_connect = before, .multishot = true, - .extra_loops = count -1 + .extra_loops = count -1 , + .overflow = overflow };if (no_accept_multi)
@@ -779,15 +846,21 @@ int main(int argc, char *argv[])return ret; } - ret =test_multishot_accept (1 , false); + ret =test_multishot_accept (1 , true, true); +if (ret) { +fprintf (stderr,"test_multishot_accept(1, false, true) failed\n" ); +return ret; + } + + ret =test_multishot_accept (1 , false, false);if (ret) { -fprintf (stderr,"test_multishot_accept(1, false) failed\n" ); +fprintf (stderr,"test_multishot_accept(1, false, false) failed\n" );return ret; } - ret =test_multishot_accept (1 , true); + ret =test_multishot_accept (1 , true, false);if (ret) { -fprintf (stderr,"test_multishot_accept(1, true) failed\n" ); +fprintf (stderr,"test_multishot_accept(1, true, false) failed\n" );return ret; }
Signature · 1 line
2.30.2
Quoted 9 lines · Dylan Yudaken
This adds an API, tests and documentation for the multi shot receive functionality.
It also adds some testing for overflow paths in accept & poll which previously was not tested.
Patch 1 adds a helper
t_create_socket_pairwhich provides two connected sockets without needing a hard coded port[...]
Applied, thanks!
[1/7] add t_create_socket_pair
commit: 9167905ca187064ba1d9ac4c8bb8484157bef86b
[2/7] add IORING_RECV_MULTISHOT to io_uring.h
commit: 791fc0998b0bdb913f71320caf3128aae23d8f39
[3/7] add io_uring_prep_(recv|recvmsg)_multishot
commit: 5279d6abefcdb3eedfbcae87559cfdda0ec6e94b
[4/7] add IORING_RECV_MULTISHOT docs
commit: 8e182bbdff0f5d6e1640190f713ed11060470b5f
[5/7] add recv-multishot test
commit: b367a5273328d05b8e118bdaf1e87c0c8d9f8606
[6/7] add poll overflow test
commit: 4218ad94b03db5f9109f02db3ba45bebd55eb744
[7/7] add accept with overflow test
commit: afc6be1619b4d9eeda5a432353fd7285a543640fBest regards,
Signature · 1 line
Jens Axboe