Skip to contentSkip to conversation

liburing: Add {g,s}etsockopt command support

PATCH Applied Breno Leitao 2 participants 7 messages Oct 20, 2023 io_uring

lore
Share & tools
lore
#0 lore raw

cover liburing: Add {g,s}etsockopt command support

These are liburing patches that add support for the new SOCKET_URING_OP_GETSOCKOPT and SOCKET_URING_OP_SETSOCKOPT socket commands.

This patchset basically synchronize the UAPI bits, teach io_uring_prep_cmd(3) how to use the new fields, create a unit test and add the proper man page documentation.

Breno Leitao (5):
io_uring: uapi: Sync the {g,s}etsockopt fields liburing.h: Populate SQE for {s,g} etsockopt tests/socket-getsetsock-cmd: New test for {g,s}etsockopt man/io_uring_prep_cmd: Fix argument name man/io_uring_prep_cmd: Add the new sockopt commands

 CHANGELOG                       |   4 +
 man/io_uring_prep_cmd.3         |  34 +++-
 src/include/liburing.h          |  11 +-
 src/include/liburing/io_uring.h |   8 +
 test/Makefile                   |   1 +
 test/socket-getsetsock-cmd.c    | 328 ++++++++++++++++++++++++++++++++
 6 files changed, 378 insertions(+), 8 deletions(-)
 create mode 100644 test/socket-getsetsock-cmd.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/5 io_uring: uapi: Sync the {g,s}etsockopt fields

Copy the UAPI fields necessary to support the new SOCKET_URING_OP_GETSOCKOPT and SOCKET_URING_OP_SETSOCKOPT socket commands.

Sync the necessary bits for io_uring_sqe struct from the kernel, and also adds the SOCKET_URING_OP_GETSOCKOPT and SOCKET_URING_OP_SETSOCKOPT to the enum that defines the commands.

---
 src/include/liburing/io_uring.h | 8 ++++++++
 1 file changed, 8 insertions(+)
src/include/liburing/io_uring.h +8 -0
index 10a6ef0..a7a95a3 100644
@@ -43,6 +43,10 @@ struct io_uring_sqe {
 	union {
 		__u64	addr;	/* pointer to buffer or iovecs */
 		__u64	splice_off_in;
+		struct {
+			__u32	level;
+			__u32	optname;
+		};
 	};
 	__u32	len;		/* buffer size or number of iovecs */
 	union {
@@ -79,6 +83,7 @@ struct io_uring_sqe {
 	union {
 		__s32	splice_fd_in;
 		__u32	file_index;
+		__u32	optlen;
 		struct {
 			__u16	addr_len;
 			__u16	__pad3[1];
@@ -89,6 +94,7 @@ struct io_uring_sqe {
 			__u64	addr3;
 			__u64	__pad2[1];
 		};
+		__u64	optval;
 		/*
 		 * If the ring is initialized with IORING_SETUP_SQE128, then
 		 * this field is used for 80 bytes of arbitrary command data
@@ -721,6 +727,8 @@ struct io_uring_recvmsg_out {
 enum {
 	SOCKET_URING_OP_SIOCINQ		= 0,
 	SOCKET_URING_OP_SIOCOUTQ,
+	SOCKET_URING_OP_GETSOCKOPT,
+	SOCKET_URING_OP_SETSOCKOPT,
 };
 
 #ifdef __cplusplus
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/5 liburing.h: Populate SQE for {s,g} etsockopt

In reply to Breno Leitao

The current io_uring_prep_cmd_sock() liburing function is not populating the SQE will all the fields because the kernel implementation was not ready.

With the integration of the kernel part[1], populate the SQE with the missing fields (optlen, optval, level and optname). This enables the usage of this function to prepare commands that executes setsockopt and getsockopt operations.

[1] Link: https://lore.kernel.org/all/[email protected]/

---
 CHANGELOG              |  4 ++++
 src/include/liburing.h | 11 ++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)
CHANGELOG +4 -0
index 42a7fc1..61199e2 100644
@@ -1,3 +1,7 @@
+liburing-2.5 release
+
+- Add getsockopt and setsockopt socket commands
+
 liburing-2.4 release
 
 - Add io_uring_{major,minor,check}_version() functions.
src/include/liburing.h +4 -7
index 1008544..84400ea 100644
@@ -1129,8 +1129,6 @@ IOURINGINLINE void io_uring_prep_socket_direct_alloc(struct io_uring_sqe *sqe,
 }
 
 
-#define UNUSED(x) (void)(x)
-
 /*
  * Prepare commands for sockets
  */
@@ -1142,13 +1140,12 @@ IOURINGINLINE void io_uring_prep_cmd_sock(struct io_uring_sqe *sqe,
 					  void *optval,
 					  int optlen)
 {
-	/* This will be removed once the get/setsockopt() patches land */
-	UNUSED(optlen);
-	UNUSED(optval);
-	UNUSED(level);
-	UNUSED(optname);
 	io_uring_prep_rw(IORING_OP_URING_CMD, sqe, fd, NULL, 0, 0);
+	sqe->optval = (long long unsigned int)optval;
+	sqe->optname = optname;
+	sqe->optlen = optlen;
 	sqe->cmd_op = cmd_op;
+	sqe->level = level;
 }
 
 /*
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/5 tests/socket-getsetsock-cmd: New test for {g,s}etsockopt

In reply to Breno Leitao

Introduce a new test to exercise the io_uring getsockopt and setsockopt commands.

On the liburing side, use the io_uring_prep_cmd_sock() function to prepare the SQE.

The test executes the same operation using a regular systemcall, such as getsockopt(2), and the io_uring getsockopt command and compare the results. The same for the setsockopt counterpart.

---
 test/Makefile                |   1 +
 test/socket-getsetsock-cmd.c | 328 +++++++++++++++++++++++++++++++++++
 2 files changed, 329 insertions(+)
 create mode 100644 test/socket-getsetsock-cmd.c
test/Makefile +1 -0
index c15b2a3..6b25e58 100644
@@ -167,6 +167,7 @@ test_srcs := \
 	skip-cqe.c \
 	socket.c \
 	socket-io-cmd.c \
+	socket-getsetsock-cmd.c \
 	socket-rw.c \
 	socket-rw-eagain.c \
 	socket-rw-offset.c \
test/socket-getsetsock-cmd.c new file +328 -0

328 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/5 man/io_uring_prep_cmd: Fix argument name

In reply to Breno Leitao

io_uring_prep_cmd(3) documents that one of the argument is SIOCOUTQ, while, in fact, it is called SOCKET_URING_OP_SIOCOUTQ.

Fix the argument name, by replacing SIOCOUTQ to
SOCKET_URING_OP_SIOCOUTQ.

Fixes 2459fef09411 ("io_uring_prep_cmd: Create a new helper for command ops")
---
 man/io_uring_prep_cmd.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
man/io_uring_prep_cmd.3 +1 -1
index 01b48da..d6ec909 100644
@@ -55,7 +55,7 @@ For more information about this command, please check
 
 
 .TP
-.B SIOCOUTQ
+.B SOCKET_URING_OP_SIOCOUTQ
 Returns the amount of unsent data in the socket send queue.
 The socket must not be in LISTEN state, otherwise an error
 .B -EINVAL
Signature · 1 line
2.34.1
#5 lore raw

PATCH 5/5 man/io_uring_prep_cmd: Add the new sockopt commands

In reply to Breno Leitao

io_uring now supports getsockopt and setsockopt socket commands. Document these two new commands in the io_uring_prep_cmd man page.

---
 man/io_uring_prep_cmd.3 | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
man/io_uring_prep_cmd.3 +32 -0
index d6ec909..12f607e 100644
@@ -72,6 +72,38 @@ Negative return value means an error.
 For more information about this command, please check
 .BR unix(7).
 
+.TP
+.B SOCKET_URING_OP_GETSOCKOPT
+Command to get options for the socket referred to by the socket file descriptor
+.I fd.
+The arguments are similar to the
+.BR getsockopt(2)
+system call.
+
+The
+.BR SOCKET_URING_OP_GETSOCKOPT
+command is limited to
+.BR SOL_SOCKET
+.I level.
+
+Differently from the
+.BR getsockopt(2)
+system call, the updated
+.I optlen
+value is returned in the CQE
+.I res
+field, on success. On failure, the CQE
+.I res
+contains a negative error number.
+
+.TP
+.B SOCKET_URING_OP_SETSOCKOPT
+Command to set options for the socket referred to by the socket file descriptor
+.I fd.
+The arguments are similar to the
+.BR setsockopt(2)
+system call.
+
 .SH NOTES
 The memory block pointed by
 .I optval
Signature · 1 line
2.34.1
maintainer Applied
#6 lore raw
In reply to Breno Leitao
Quoted 7 lines · Breno Leitao

These are liburing patches that add support for the new SOCKET_URING_OP_GETSOCKOPT and SOCKET_URING_OP_SETSOCKOPT socket commands.

This patchset basically synchronize the UAPI bits, teach io_uring_prep_cmd(3) how to use the new fields, create a unit test and add the proper man page documentation.

Applied to the 'next' branch. Had to hand apply patch 2, but the rest applied just fine.

Thanks!

Signature · 1 line
Jens Axboe