Skip to contentSkip to conversation

liburing: getsockname support

PATCH Superseded Gabriel Krisman Bertazi 2 participants 11 messages Nov 25, 2025 – Dec 3, 2025 io_uring

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover liburing: getsockname support

Since V1:
- bind-test.t: use client socket used when doing a getpeername
- Use the new io_uring_prep_uring_cmd to define prep_cmc_getsockname

This is the library counterpart for the kernel support. Also available at:

https://github.com/krisman/liburing -b socket

---
To: Jens Axboe <[email protected]>
CC: [email protected]
CC: [email protected]

Gabriel Krisman Bertazi (4):
liburing: Introduce getsockname operation
test/bind-listen.t: Use ephemeral port
bind-listen.t: Add tests for getsockname
man/io_uring_prep_getsockname.3: Add man page

 man/io_uring_prep_getsockname.3 |  76 ++++++++++++++++++
 src/include/liburing.h          |  13 +++
 src/include/liburing/io_uring.h |   1 +
 test/bind-listen.c              | 137 +++++++++++++++++++++++++++++---
 4 files changed, 215 insertions(+), 12 deletions(-)
 create mode 100644 man/io_uring_prep_getsockname.3
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/4 liburing: Introduce getsockname operation

This implements the functionality of getsockname(2) and getpeername(2) under a single operation.

---
 src/include/liburing.h          | 13 +++++++++++++
 src/include/liburing/io_uring.h |  1 +
 2 files changed, 14 insertions(+)
src/include/liburing.h +13 -0
index 83819eb7..1626f3bb 100644
@@ -1572,6 +1572,19 @@ IOURINGINLINE void io_uring_prep_cmd_sock(struct io_uring_sqe *sqe,
 	sqe->level = level;
 }
 
+IOURINGINLINE void io_uring_prep_cmd_getsockname(struct io_uring_sqe *sqe,
+						 int fd, struct sockaddr *sockaddr,
+						 socklen_t *sockaddr_len,
+						 int peer)
+	LIBURING_NOEXCEPT
+{
+	io_uring_prep_uring_cmd(sqe, SOCKET_URING_OP_GETSOCKNAME, fd);
+
+	sqe->addr = (uintptr_t) sockaddr;
+	sqe->addr3 = (unsigned long) (uintptr_t) sockaddr_len;
+	sqe->optlen = peer;
+}
+
 IOURINGINLINE void io_uring_prep_waitid(struct io_uring_sqe *sqe,
 					idtype_t idtype,
 					id_t id,
src/include/liburing/io_uring.h +1 -0
index a54e5b42..8e8b8e6a 100644
@@ -966,6 +966,7 @@ enum io_uring_socket_op {
 	SOCKET_URING_OP_GETSOCKOPT,
 	SOCKET_URING_OP_SETSOCKOPT,
 	SOCKET_URING_OP_TX_TIMESTAMP,
+	SOCKET_URING_OP_GETSOCKNAME,
 };
 
 /*
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/4 test/bind-listen.t: Use ephemeral port

In reply to Gabriel Krisman Bertazi

This test fails if port 8000 is already in use by something else. Now that we have getsockname with direct file descriptors, use an ephemeral port instead.

---
 test/bind-listen.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
test/bind-listen.c +23 -10
index 6f80f177..7c229a17 100644
@@ -22,7 +22,6 @@ static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec)
 }
 
 static const char *magic = "Hello World!";
-static int use_port = 8000;
 
 enum {
 	SRV_INDEX = 0,
@@ -74,18 +73,19 @@ static int connect_client(struct io_uring *ring, unsigned short peer_port)
 	return T_SETUP_OK;
 }
 
-static int setup_srv(struct io_uring *ring, struct sockaddr_in *server_addr)
+static int setup_srv(struct io_uring *ring)
 {
+	struct sockaddr_in server_addr;
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
 	struct __kernel_timespec ts;
 	int ret, val, submitted;
 	unsigned head;
 
-	memset(server_addr, 0, sizeof(struct sockaddr_in));
-	server_addr->sin_family = AF_INET;
-	server_addr->sin_port = htons(use_port++);
-	server_addr->sin_addr.s_addr = htons(INADDR_ANY);
+	memset(&server_addr, 0, sizeof(struct sockaddr_in));
+	server_addr.sin_family = AF_INET;
+	server_addr.sin_port = htons(0);
+	server_addr.sin_addr.s_addr = htons(INADDR_ANY);
 
 	sqe = io_uring_get_sqe(ring);
 	io_uring_prep_socket_direct(sqe, AF_INET, SOCK_STREAM, 0, SRV_INDEX, 0);
@@ -98,7 +98,7 @@ static int setup_srv(struct io_uring *ring, struct sockaddr_in *server_addr)
 	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
 
 	sqe = io_uring_get_sqe(ring);
-	io_uring_prep_bind(sqe, SRV_INDEX, (struct sockaddr *) server_addr,
+	io_uring_prep_bind(sqe, SRV_INDEX, (struct sockaddr *) &server_addr,
 			   sizeof(struct sockaddr_in));
 	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
 
@@ -132,7 +132,8 @@ static int setup_srv(struct io_uring *ring, struct sockaddr_in *server_addr)
 
 static int test_good_server(unsigned int ring_flags)
 {
-	struct sockaddr_in server_addr;
+	struct sockaddr_in saddr = {};
+	socklen_t saddr_len = sizeof(saddr);
 	struct __kernel_timespec ts;
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
@@ -155,13 +156,25 @@ static int test_good_server(unsigned int ring_flags)
 		return T_SETUP_SKIP;
 	}
 
-	ret = setup_srv(&ring, &server_addr);
+	ret = setup_srv(&ring);
 	if (ret != T_SETUP_OK) {
 		fprintf(stderr, "srv startup failed.\n");
 		return T_EXIT_FAIL;
 	}
 
-	if (connect_client(&ring, server_addr.sin_port) != T_SETUP_OK) {
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_cmd_getsockname(sqe, SRV_INDEX, (struct sockaddr*)&saddr,
+				      &saddr_len, 0);
+	sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
+	io_uring_submit(&ring);
+	io_uring_wait_cqe(&ring, &cqe);
+	if (cqe->res < 0) {
+		fprintf(stderr, "getsockname server failed. %d\n", cqe->res);
+		return T_EXIT_FAIL;
+	}
+	io_uring_cqe_seen(&ring, cqe);
+
+	if (connect_client(&ring, saddr.sin_port) != T_SETUP_OK) {
 		fprintf(stderr, "cli startup failed.\n");
 		return T_SETUP_SKIP;
 	}
Signature · 1 line
2.51.0
maintainer
#3 lore raw

Re: [PATCH liburing v2 2/4] test/bind-listen.t: Use ephemeral port

Quoted 3 lines · Gabriel Krisman Bertazi

This test fails if port 8000 is already in use by something else. Now that we have getsockname with direct file descriptors, use an ephemeral port instead.

How is this going to work on older kernels? Probably retain the old behavior, even if kind of shitty, on old kernels. Otherwise anything pre 6.19 will now not run the bind-listen test at all.

Signature · 1 line
Jens Axboe
#4 lore raw

Re: [PATCH liburing v2 2/4] test/bind-listen.t: Use ephemeral port

Quoted 8 lines · Jens Axboe
Quoted 3 lines · Gabriel Krisman Bertazi

This test fails if port 8000 is already in use by something else. Now that we have getsockname with direct file descriptors, use an ephemeral port instead.

How is this going to work on older kernels? Probably retain the old behavior, even if kind of shitty, on old kernels. Otherwise anything pre 6.19 will now not run the bind-listen test at all.

Do you have a suggestion on how to check getsockname without doing the whole socket setup just to probe, considering this is a uring_cmd? Perhaps checking a feature that was merged at the same time?

If not, I'll come with something and send the v4 early tomorrow so you can cut the new version still this week.

Signature · 1 line
Gabriel Krisman Bertazi
maintainer
#5 lore raw

Re: [PATCH liburing v2 2/4] test/bind-listen.t: Use ephemeral port

Quoted 7 lines · Gabriel Krisman Bertazi
Quoted 5 lines · Jens Axboe
Quoted 3 lines · Gabriel Krisman Bertazi

This test fails if port 8000 is already in use by something else. Now that we have getsockname with direct file descriptors, use an ephemeral port instead.

Quoted 7 lines
Quoted 3 lines

How is this going to work on older kernels? Probably retain the old behavior, even if kind of shitty, on old kernels. Otherwise anything pre 6.19 will now not run the bind-listen test at all.

Do you have a suggestion on how to check getsockname without doing the whole socket setup just to probe, considering this is a uring_cmd? Perhaps checking a feature that was merged at the same time?

I think just retain the old code - do the getsockname via io_uring, and if it fails, then fall back to the old code. Then it'll both exercise the new code, when available, yet still work on older kernels still.

Signature · 1 line
Jens Axboe
#6 lore raw

PATCH v2 3/4 bind-listen.t: Add tests for getsockname

In reply to Gabriel Krisman Bertazi
---
 test/bind-listen.c | 104 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 102 insertions(+), 2 deletions(-)
test/bind-listen.c +102 -2
index 7c229a17..d3e3c9c8 100644
@@ -138,7 +138,7 @@ static int test_good_server(unsigned int ring_flags)
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
 	struct io_uring ring;
-	int ret;
+	int ret, port;
 	int fds[3];
 	char buf[1024];
 
@@ -179,7 +179,7 @@ static int test_good_server(unsigned int ring_flags)
 		return T_SETUP_SKIP;
 	}
 
-	/* Wait for a request */
+	/* Wait for a connection */
 	sqe = io_uring_get_sqe(&ring);
 	io_uring_prep_accept_direct(sqe, SRV_INDEX, NULL, NULL, 0, CONN_INDEX);
 	sqe->flags |= IOSQE_FIXED_FILE;
@@ -192,6 +192,30 @@ static int test_good_server(unsigned int ring_flags)
 	}
 	io_uring_cqe_seen(&ring, cqe);
 
+	/* Test that getsockname on the peer (getpeername) yields a
+         * sane result.
+	 */
+	sqe = io_uring_get_sqe(&ring);
+	saddr_len = sizeof(saddr);
+	port = saddr.sin_port;
+	io_uring_prep_cmd_getsockname(sqe, CLI_INDEX, (struct sockaddr*)&saddr,
+				      &saddr_len, 1);
+	sqe->flags |= IOSQE_FIXED_FILE;
+	io_uring_submit(&ring);
+	io_uring_wait_cqe(&ring, &cqe);
+	if (cqe->res < 0) {
+		fprintf(stderr, "getsockname client failed. %d\n", cqe->res);
+		return T_EXIT_FAIL;
+	} else {
+		if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK) ||
+		    saddr.sin_port != port) {
+			fprintf(stderr, "getsockname peer got wrong address: %s:%d\n",
+				inet_ntoa(saddr.sin_addr), saddr.sin_port);
+			return T_EXIT_FAIL;
+		}
+	}
+	io_uring_cqe_seen(&ring, cqe);
+
 	sqe = io_uring_get_sqe(&ring);
 	io_uring_prep_recv(sqe, CONN_INDEX, buf, sizeof(buf), 0);
 	sqe->flags |= IOSQE_FIXED_FILE;
@@ -368,6 +392,77 @@ fail:
 	return ret;
 }
 
+static int test_bad_sockname(void)
+{
+	struct sockaddr_in saddr;
+	socklen_t saddr_len;
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	struct io_uring ring;
+	int sock = -1, err;
+	int ret = T_EXIT_FAIL;
+
+	memset(&saddr, 0, sizeof(struct sockaddr_in));
+	saddr.sin_family = AF_INET;
+	saddr.sin_port = htons(8001);
+	saddr.sin_addr.s_addr = htons(INADDR_ANY);
+
+	err = t_create_ring(1, &ring, 0);
+	if (err < 0) {
+		fprintf(stderr, "queue_init: %d\n", err);
+		return T_SETUP_SKIP;
+	}
+
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+	if (sock < 0) {
+		perror("socket");
+		goto fail;
+	}
+
+	err = t_bind_ephemeral_port(sock, &saddr);
+	if (err) {
+		fprintf(stderr, "bind: %s\n", strerror(-err));
+		goto fail;
+	}
+
+	/* getsockname on a !socket fd.  with getsockname(2), this would
+	 * return -ENOTSOCK, but we can't do it in an io_uring_cmd.
+	 */
+	sqe = io_uring_get_sqe(&ring);
+	saddr_len = sizeof(saddr);
+	io_uring_prep_cmd_getsockname(sqe, 1, (struct sockaddr*)&saddr, &saddr_len, 0);
+	err = io_uring_submit(&ring);
+	if (err < 0)
+		goto fail;
+	err = io_uring_wait_cqe(&ring, &cqe);
+	if (err)
+		goto fail;
+	if (cqe->res != -ENOTSUP)
+		goto fail;
+	io_uring_cqe_seen(&ring, cqe);
+
+	/* getsockname with weird parameters */
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_cmd_getsockname(sqe, sock, (struct sockaddr*)&saddr,
+				      &saddr_len, 3);
+	err = io_uring_submit(&ring);
+	if (err < 0)
+		goto fail;
+	err = io_uring_wait_cqe(&ring, &cqe);
+	if (err)
+		goto fail;
+	if (cqe->res != -EINVAL)
+		goto fail;
+	io_uring_cqe_seen(&ring, cqe);
+
+	ret = T_EXIT_PASS;
+fail:
+	io_uring_queue_exit(&ring);
+	if (sock != -1)
+		close(sock);
+	return ret;
+}
+
 int main(int argc, char *argv[])
 {
 	struct io_uring_probe *probe;
@@ -417,5 +512,10 @@ int main(int argc, char *argv[])
 		return T_EXIT_FAIL;
 	}
 
+	ret = test_bad_sockname();
+	if (ret) {
+		fprintf(stderr, "bad sockname failed\n");
+		return T_EXIT_FAIL;
+	}
 	return T_EXIT_PASS;
 }
Signature · 1 line
2.51.0
maintainer
#7 lore raw

Re: [PATCH liburing v2 3/4] bind-listen.t: Add tests for getsockname

test/bind-listen.c static int test_good_server(unsigned int ring_flags) · line 197

Quoted 7 lines · Gabriel Krisman Bertazi
test/bind-listen.c +3 -0
@@ -192,6 +192,30 @@ static int test_good_server(unsigned int ring_flags)
 	}
 	io_uring_cqe_seen(&ring, cqe);
 
+	/* Test that getsockname on the peer (getpeername) yields a
+         * sane result.
+	 */

Please use:

/*
* xxx
*/

for multi-line comments.

test/bind-listen.c static int test_good_server(unsigned int ring_flags) · line 208

Quoted 11 lines
test/bind-listen.c +11 -0
+	sqe = io_uring_get_sqe(&ring);
+	saddr_len = sizeof(saddr);
+	port = saddr.sin_port;
+	io_uring_prep_cmd_getsockname(sqe, CLI_INDEX, (struct sockaddr*)&saddr,
+				      &saddr_len, 1);
+	sqe->flags |= IOSQE_FIXED_FILE;
+	io_uring_submit(&ring);
+	io_uring_wait_cqe(&ring, &cqe);
+	if (cqe->res < 0) {
+		fprintf(stderr, "getsockname client failed. %d\n", cqe->res);
+		return T_EXIT_FAIL;

This will also fail on older kernels, where it should just return T_EXIT_SKIP and the main "call these tests" part of main() should handle that.

Any liburing test should run on any kernel. This is important as regression tests are in there too, it's not just feature tests. I should be able to run this on eg 5.10-stable and not get spurious errors and then need to look at why that happened.

test/bind-listen.c int main(int argc, char *argv[]) · line 521

Quoted 11 lines
test/bind-listen.c +5 -0
@@ -417,5 +512,10 @@ int main(int argc, char *argv[])
 		return T_EXIT_FAIL;
 	}
 
+	ret = test_bad_sockname();
+	if (ret) {
+		fprintf(stderr, "bad sockname failed\n");
+		return T_EXIT_FAIL;
+	}
 	return T_EXIT_PASS;
 }

So here you'd just do:

	if (ret == T_EXIT_FAIL) {
		...
	}

	return T_EXIT_PASS;

so the test only fails if the test explicitly failed, which it should not if getsockname isn't available on the running kernel.

Signature · 1 line
Jens Axboe
#8 lore raw

PATCH v2 4/4 man/io_uring_prep_getsockname.3: Add man page

In reply to Gabriel Krisman Bertazi
---
 man/io_uring_prep_getsockname.3 | 76 +++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 man/io_uring_prep_getsockname.3
man/io_uring_prep_getsockname.3 new file +76 -0
new file mode 100644
index 00000000..71e65f1d
@@ -0,0 +1,76 @@
+.\" Copyright (C) 2024 SUSE LLC.
+.\"
+.\" SPDX-License-Identifier: LGPL-2.0-or-later
+.\"
+.TH io_uring_prep_getsockname 3 "Oct 23, 2025" "liburing-2.11" "liburing Manual"
+.SH NAME
+io_uring_prep_getsockname \- prepare a getsockname or getpeername request
+.SH SYNOPSIS
+.nf
+.B #include <sys/socket.h>
+.B #include <liburing.h>
+.PP
+.BI "void io_uring_prep_getsockname(struct io_uring_sqe *" sqe ","
+.BI "                          int " sockfd ","
+.BI "                          struct sockaddr *" sockaddr ","
+.BI "                          socklen_t *" sockaddr_len ","
+.BI "                          int " peer ");"
+.fi
+.SH DESCRIPTION
+The
+.BR io_uring_prep_getsockname (3)
+function prepares a getsockname/getpeername request.
+The submission queue entry
+.I sqe
+is setup to fetch the locally bound address or peer address of the socket
+file descriptor pointed by
+.IR sockfd.
+The parameter
+.IR sockaddr
+points to a region of size
+.IR sockaddr_len
+where the output is written.
+.IR sockaddr_len
+is modified by the kernel to indicate how many bytes were written.
+The output address is the locally bound address if
+.IR peer
+is set to 0
+or the peer address if
+.IR peer
+is set to 1.
+
+This function prepares an async
+.BR getsockname (2)
+or
+.BR getpeername (2)
+request. See those man pages 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.
+.BR
+Differently from the equivalent system calls, if the user attempts to
+use this operation on a non-socket file descriptor, the CQE error result
+is
+.IR ENOTSUP
+instead of
+.IR ENOSOCK.
+.SH SEE ALSO
+.BR io_uring_get_sqe (3),
+.BR io_uring_submit (3),
+.BR io_uring_prep_getsockname (2)
+.BR io_uring_prep_getpeername (2)
Signature · 1 line
2.51.0
maintainer
#9 lore raw

Re: [PATCH liburing v2 4/4] man/io_uring_prep_getsockname.3: Add man page

Quoted 12 lines · Gabriel Krisman Bertazi
---
 man/io_uring_prep_getsockname.3 | 76 +++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 man/io_uring_prep_getsockname.3
man/io_uring_prep_getsockname.3 new file +0 -0
new file mode 100644
index 00000000..71e65f1d

man/io_uring_prep_getsockname.3 line 5

Quoted 5 lines
man/io_uring_prep_getsockname.3 new file +5 -0
@@ -0,0 +1,76 @@
+.\" Copyright (C) 2024 SUSE LLC.
+.\"
+.\" SPDX-License-Identifier: LGPL-2.0-or-later
+.\"
+.TH io_uring_prep_getsockname 3 "Oct 23, 2025" "liburing-2.11" "liburing Manual"

Should be 2.13 at this point, as that will be the next release.

man/io_uring_prep_getsockname.3 line 17

Quoted 12 lines
man/io_uring_prep_getsockname.3 new file +12 -0
+.SH NAME
+io_uring_prep_getsockname \- prepare a getsockname or getpeername request
+.SH SYNOPSIS
+.nf
+.B #include <sys/socket.h>
+.B #include <liburing.h>
+.PP
+.BI "void io_uring_prep_getsockname(struct io_uring_sqe *" sqe ","
+.BI "                          int " sockfd ","
+.BI "                          struct sockaddr *" sockaddr ","
+.BI "                          socklen_t *" sockaddr_len ","
+.BI "                          int " peer ");"

Line up the arguments with the first one.

man/io_uring_prep_getsockname.3 line 27

Quoted 10 lines
man/io_uring_prep_getsockname.3 new file +10 -0
+.fi
+.SH DESCRIPTION
+The
+.BR io_uring_prep_getsockname (3)
+function prepares a getsockname/getpeername request.
+The submission queue entry
+.I sqe
+is setup to fetch the locally bound address or peer address of the socket
+file descriptor pointed by
+.IR sockfd.

Space after sockfd and before '.'.

man/io_uring_prep_getsockname.3 line 37

Quoted 10 lines
man/io_uring_prep_getsockname.3 new file +10 -0
+The parameter
+.IR sockaddr
+points to a region of size
+.IR sockaddr_len
+where the output is written.
+.IR sockaddr_len
+is modified by the kernel to indicate how many bytes were written.
+The output address is the locally bound address if
+.IR peer
+is set to 0

Probably

is set to
.B 0
or the peer address if

Quoted 24 lines
man/io_uring_prep_getsockname.3 new file +24 -0
+or the peer address if
+.IR peer
+is set to 1.
+
+This function prepares an async
+.BR getsockname (2)
+or
+.BR getpeername (2)
+request. See those man pages 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

man/io_uring_prep_getsockname.3 line 71

Quoted 10 lines
man/io_uring_prep_getsockname.3 new file +10 -0
+directly in the CQE
+.I res
+field.
+.BR
+Differently from the equivalent system calls, if the user attempts to
+use this operation on a non-socket file descriptor, the CQE error result
+is
+.IR ENOTSUP
+instead of
+.IR ENOSOCK.

cqe->res is always negative, so it'd be -ENOTSUP.

Other than these nits, looks good! Thanks for writing it.

Signature · 1 line
Jens Axboe
maintainer
#10 lore raw
In reply to Gabriel Krisman Bertazi
Quoted 3 lines · Gabriel Krisman Bertazi

Since V1:
- bind-test.t: use client socket used when doing a getpeername
- Use the new io_uring_prep_uring_cmd to define prep_cmc_getsockname

Can you send a v3 please, with the things highlighted in my review sorted? I want to release liburing 2.13 before the holidays, and since we'll have the kernel support for this in the 6.19 kernel, we really should have the liburing support in place too.

Signature · 1 line
Jens Axboe