Introduce getsockname io_uring_cmd
Subsystems (4)
cover Introduce getsockname io_uring_cmd
This feature has been requested a few times in the liburing repository and Discord channels, such as in [1,2]. If anything, it also helps solve a long standing issue in the bind-listen test that results in occasional test failures.
The patchset is divided in three parts: Patch 1 merges the getpeername and getsockname implementation in the network layer, making further patches easier; Patch 2 splits out a helper used by io_uring, like done for other network commands; Finally, patch 3 plumbs the new command in io_uring.
The syscall path was tested by booting a Linux distro, which does all sorts of getsockname/getpeername syscalls. The io_uring side was tested with a couple of new liburing subtests available at:
https://github.com/krisman/liburing.git -b socket
Based on top of Jens' for-next.
[1] https://github.com/axboe/liburing/issues/1356
[2] https://discord.com/channels/1241076672589991966/1241076672589991970/1429975797912830074
---
Gabriel Krisman Bertazi (3):
socket: Unify getsockname and getpeername implementation
socket: Split out a getsockname helper for io_uring
io_uring: Introduce getsockname io_uring cmd
include/linux/socket.h | 6 +-- include/uapi/linux/io_uring.h | 1 + io_uring/cmd_net.c | 24 ++++++++++++ net/compat.c | 4 +- net/socket.c | 69 +++++++++++------------------------ 5 files changed, 52 insertions(+), 52 deletions(-)
Signature · 1 line
2.51.0
PATCH 2/3 socket: Split out a getsockname helper for io_uring
Similar to getsockopt, split out a helper to check security and issue the operation from the main handler that can be used by io_uring.
--- include/linux/socket.h | 2 ++ net/socket.c | 34 +++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-)
include/linux/socket.h +2 -0
@@ -453,6 +453,8 @@ extern int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen);extern int __sys_listen (int fd,int backlog);extern int __sys_listen_socket (struct socket *sock,int backlog); +extern int do_getsockname (struct socket *sock,struct sockaddr_storage *address, +int peer,struct sockaddr __user *usockaddr,int __user *usockaddr_len);extern int __sys_getsockname (int fd,struct sockaddr __user *usockaddr,int __user *usockaddr_len,int peer); extern int __sys_socketpair(int family, int type, int protocol,
net/socket.c +19 -15
@@ -2127,6 +2127,24 @@ SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,return __sys_connect (fd, uservaddr, addrlen); } +int do_getsockname (struct socket *sock,struct sockaddr_storage *address,int peer, +struct sockaddr __user *usockaddr,int __user *usockaddr_len) +{ +int err; + +if (peer) + err =security_socket_getpeername (sock); +else + err =security_socket_getsockname (sock); +if (err) +return err; + err =READ_ONCE (sock->ops)->getname (sock, (struct sockaddr *)address, peer); +if (err <0 ) +return err; +/* "err" is actually length in this case */ +return move_addr_to_user (address, err, usockaddr, usockaddr_len); +} +/* * Get the address (remote or local ('name')) of a socket object. Move the * obtained name to user space.
@@ -2137,27 +2155,13 @@ int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,struct socket *sock;struct sockaddr_storage address;CLASS (fd, f)(fd); -int err;if (fd_empty (f))return -EBADF ; sock =sock_from_file (fd_file (f));if (unlikely (!sock))return -ENOTSOCK ; - -if (peer) - err =security_socket_getpeername (sock); -else - err =security_socket_getsockname (sock); -if (err) -return err; - - err =READ_ONCE (sock->ops)->getname (sock, (struct sockaddr *)&address, peer); -if (err <0 ) -return err; - -/* "err" is actually length in this case */ -return move_addr_to_user (&address, err, usockaddr, usockaddr_len); +return do_getsockname (sock, &address, peer, usockaddr, usockaddr_len); } SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
Signature · 1 line
2.51.0
PATCH 1/3 socket: Unify getsockname and getpeername implementation
In reply to Gabriel Krisman BertaziThey are already implemented by the same get_name hook in the protocol level. Bring the unification one level up to reduce code duplication in preparation to supporting these as io_uring operations.
--- include/linux/socket.h | 4 +-- net/compat.c | 4 +-- net/socket.c | 55 ++++++++++-------------------------------- 3 files changed, 16 insertions(+), 47 deletions(-)
include/linux/socket.h +1 -3
@@ -454,9 +454,7 @@ extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,extern int __sys_listen (int fd,int backlog);extern int __sys_listen_socket (struct socket *sock,int backlog);extern int __sys_getsockname (int fd,struct sockaddr __user *usockaddr, -int __user *usockaddr_len); -extern int __sys_getpeername (int fd,struct sockaddr __user *usockaddr, -int __user *usockaddr_len); +int __user *usockaddr_len,int peer);extern int __sys_socketpair (int family,int type,int protocol,int __user *usockvec);extern int __sys_shutdown_sock (struct socket *sock,int how);
net/compat.c +2 -2
@@ -460,10 +460,10 @@ COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args) ret =__sys_accept4 (a0,compat_ptr (a1),compat_ptr (a[2 ]),0 );break ;case SYS_GETSOCKNAME : - ret =__sys_getsockname (a0,compat_ptr (a1),compat_ptr (a[2 ])); + ret =__sys_getsockname (a0,compat_ptr (a1),compat_ptr (a[2 ]),0 );break ;case SYS_GETPEERNAME : - ret =__sys_getpeername (a0,compat_ptr (a1),compat_ptr (a[2 ])); + ret =__sys_getsockname (a0,compat_ptr (a1),compat_ptr (a[2 ]),1 );break ;case SYS_SOCKETPAIR : ret =__sys_socketpair (a0, a1, a[2 ],compat_ptr (a[3 ]));
net/socket.c +13 -42
92 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH 3/3 io_uring: Introduce getsockname io_uring cmd
In reply to Gabriel Krisman BertaziIntroduce a socket-specific io_uring_cmd to support getsockname/getpeername via io_uring. I made this an io_uring_cmd instead of a new operation to avoid polluting the command namespace with what is exclusively a socket operation. In addition, since we don't need to conform to existing interfaces, this merges the getsockname/getpeername in a single operation, since the implementation is pretty much the same.
This has been frequently requested, for instance at [1] and more recently in the project Discord channel. The main use-case is to support fixed socket file descriptors.
[1] https://github.com/axboe/liburing/issues/1356
--- include/uapi/linux/io_uring.h | 1 + io_uring/cmd_net.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
include/uapi/linux/io_uring.h +1 -0
@@ -1001,6 +1001,7 @@ enum io_uring_socket_op {SOCKET_URING_OP_GETSOCKOPT ,SOCKET_URING_OP_SETSOCKOPT ,SOCKET_URING_OP_TX_TIMESTAMP , + SOCKET_URING_OP_GETSOCKNAME, };/*
io_uring/cmd_net.c +24 -0
@@ -132,6 +132,28 @@ static int io_uring_cmd_timestamp(struct socket *sock,return -EAGAIN ; } +static int io_uring_cmd_getsockname (struct socket *sock, +struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +const struct io_uring_sqe *sqe = cmd->sqe; + +struct sockaddr_storage address; +struct sockaddr __user *uaddr; +int __user *ulen; +unsigned int peer; + + uaddr =u64_to_user_ptr (READ_ONCE (sqe->addr)); + ulen =u64_to_user_ptr (sqe->addr3); + peer =READ_ONCE (sqe->optlen); + +if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags) +return -EINVAL ; +if (peer >1 ) +return -EINVAL ; +return do_getsockname (sock, &address,0 , uaddr, ulen); +} +int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags) {struct socket *sock = cmd->file->private_data;
@@ -159,6 +181,8 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)return io_uring_cmd_setsockopt (sock, cmd, issue_flags);case SOCKET_URING_OP_TX_TIMESTAMP :return io_uring_cmd_timestamp (sock, cmd, issue_flags); +case SOCKET_URING_OP_GETSOCKNAME : +return io_uring_cmd_getsockname (sock, cmd, issue_flags);default :return -EOPNOTSUPP ; }
Signature · 1 line
2.51.0
Jens Axboe
maintainerRe: [PATCH 3/3] io_uring: Introduce getsockname io_uring cmd
Quoted 11 lines · Gabriel Krisman Bertazi
Introduce a socket-specific
io_uring_cmdto support getsockname/getpeername viaio_uring. I made this anio_uring_cmdinstead of a new operation to avoid polluting the command namespace with what is exclusively a socket operation. In addition, since we don't need to conform to existing interfaces, this merges the getsockname/getpeername in a single operation, since the implementation is pretty much the same.This has been frequently requested, for instance at [1] and more recently in the project Discord channel. The main use-case is to support fixed socket file descriptors.
Just two nits below, otherwise looks good!
Quoted 5 lines
io_uring/cmd_net.c +0
-0
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 140
Quoted 9 lines
io_uring/cmd_net.c +6
-0@@ -132,6 +132,28 @@ static int io_uring_cmd_timestamp(struct socket *sock,return -EAGAIN ; } +static int io_uring_cmd_getsockname (struct socket *sock, +struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +const struct io_uring_sqe *sqe = cmd->sqe; +
Random newline.
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 151
Quoted 11 lines
io_uring/cmd_net.c +11
-0+struct sockaddr_storage address; +struct sockaddr __user *uaddr; +int __user *ulen; +unsigned int peer; + + uaddr =u64_to_user_ptr (READ_ONCE (sqe->addr)); + ulen =u64_to_user_ptr (sqe->addr3); + peer =READ_ONCE (sqe->optlen); + +if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags) +return -EINVAL ;
Most/all prep handlers tend to check these first, then proceed with setting up if not set. Would probably make sense to mirror that here too.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 16 lines · Gabriel Krisman Bertazi
This feature has been requested a few times in the liburing repository and Discord channels, such as in [1,2]. If anything, it also helps solve a long standing issue in the bind-listen test that results in occasional test failures.
The patchset is divided in three parts: Patch 1 merges the getpeername and getsockname implementation in the network layer, making further patches easier; Patch 2 splits out a helper used by
io_uring, like done for other network commands; Finally, patch 3 plumbs the new command inio_uring.The syscall path was tested by booting a Linux distro, which does all sorts of getsockname/getpeername syscalls. The
io_uringside was tested with a couple of new liburing subtests available at:
Quoted 6 lines
https://github.com/krisman/liburing.git -b socket
Based on top of Jens' for-next.
[1] https://github.com/axboe/liburing/issues/1356
[2] https://discord.com/channels/1241076672589991966/1241076672589991970/1429975797912830074
Looks good to me, and it's not often you can add a new feature and have:
Quoted 1 line
5 files changed, 52 insertions(+), 52 deletions(-)
a net zero diffstat! Nice.
Signature · 1 line
Jens Axboe
Re: [PATCH 3/3] io_uring: Introduce getsockname io_uring cmd
In reply to Jens AxboeQuoted 23 lines · Jens Axboe
Quoted 11 lines · Gabriel Krisman Bertazi
Introduce a socket-specific
io_uring_cmdto support getsockname/getpeername viaio_uring. I made this anio_uring_cmdinstead of a new operation to avoid polluting the command namespace with what is exclusively a socket operation. In addition, since we don't need to conform to existing interfaces, this merges the getsockname/getpeername in a single operation, since the implementation is pretty much the same.This has been frequently requested, for instance at [1] and more recently in the project Discord channel. The main use-case is to support fixed socket file descriptors.
Just two nits below, otherwise looks good!
Quoted 8 lines
io_uring/cmd_net.c +0
-0@@ -132,6 +132,28 @@ static int io_uring_cmd_timestamp(struct socket *sock,return -EAGAIN ; }
Quoted 8 lines
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 140
Quoted 6 lines
io_uring/cmd_net.c +6
-0+static int io_uring_cmd_getsockname (struct socket *sock, +struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +const struct io_uring_sqe *sqe = cmd->sqe; +Random newline.
Done, but this fix will totally ruin the diffstat. :(
Quoted 16 lines
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 151
Quoted 12 lines
io_uring/cmd_net.c +11
-0+struct sockaddr_storage address; +struct sockaddr __user *uaddr; +int __user *ulen; +unsigned int peer; + + uaddr =u64_to_user_ptr (READ_ONCE (sqe->addr)); + ulen =u64_to_user_ptr (sqe->addr3); + peer =READ_ONCE (sqe->optlen); + +if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags) +return -EINVAL ;Most/all prep handlers tend to check these first, then proceed with setting up if not set. Would probably make sense to mirror that here too.
Ack. will wait a few days for feedback on the network side before the v2.
Signature · 1 line
Gabriel Krisman Bertazi
Jens Axboe
maintainerRe: [PATCH 3/3] io_uring: Introduce getsockname io_uring cmd
Quoted 34 lines · Gabriel Krisman Bertazi
Quoted 23 lines · Jens Axboe
Quoted 11 lines · Gabriel Krisman Bertazi
Introduce a socket-specific
io_uring_cmdto support getsockname/getpeername viaio_uring. I made this anio_uring_cmdinstead of a new operation to avoid polluting the command namespace with what is exclusively a socket operation. In addition, since we don't need to conform to existing interfaces, this merges the getsockname/getpeername in a single operation, since the implementation is pretty much the same.This has been frequently requested, for instance at [1] and more recently in the project Discord channel. The main use-case is to support fixed socket file descriptors.
Just two nits below, otherwise looks good!
Quoted 8 lines
io_uring/cmd_net.c +0
-0@@ -132,6 +132,28 @@ static int io_uring_cmd_timestamp(struct socket *sock,return -EAGAIN ; }Quoted 8 lines
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 140
Quoted 6 lines
io_uring/cmd_net.c +6
-0+static int io_uring_cmd_getsockname (struct socket *sock, +struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +const struct io_uring_sqe *sqe = cmd->sqe; +Random newline.
Quoted 1 line
Done, but this fix will totally ruin the diffstat. :(
What do you mean, it'll look even better as you're now killing a redundant line you added :)
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
io_uring/cmd_net.c +7
-0+struct sockaddr_storage address; +struct sockaddr __user *uaddr; +int __user *ulen; +unsigned int peer; + + uaddr =u64_to_user_ptr (READ_ONCE (sqe->addr)); + ulen =u64_to_user_ptr (sqe->addr3);Quoted 8 lines
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 151
Quoted 4 lines
io_uring/cmd_net.c +4
-0+ peer =READ_ONCE (sqe->optlen); + +if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags) +return -EINVAL ;Most/all prep handlers tend to check these first, then proceed with setting up if not set. Would probably make sense to mirror that here too.
Quoted 2 lines
Ack. will wait a few days for feedback on the network side before the v2.
Sounds good, thanks.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 12 lines · Gabriel Krisman Bertazi
This feature has been requested a few times in the liburing repository and Discord channels, such as in [1,2]. If anything, it also helps solve a long standing issue in the bind-listen test that results in occasional test failures.
The patchset is divided in three parts: Patch 1 merges the getpeername and getsockname implementation in the network layer, making further patches easier; Patch 2 splits out a helper used by
io_uring, like done for other network commands; Finally, patch 3 plumbs the new command inio_uring.
Quoted 7 lines
The syscall path was tested by booting a Linux distro, which does all sorts of getsockname/getpeername syscalls. The
io_uringside was tested with a couple of new liburing subtests available at:https://github.com/krisman/liburing.git -b socket
Based on top of Jens' for-next.
Ping netdev / networking folks on patches 1+2...
Signature · 1 line
Jens Axboe
Jakub Kicinski
maintainerQuoted 17 lines · On Wed, 19 Nov 2025 16:30:38 -0700 Jens Axboe wrote:
Quoted 16 lines · Gabriel Krisman Bertazi
This feature has been requested a few times in the liburing repository and Discord channels, such as in [1,2]. If anything, it also helps solve a long standing issue in the bind-listen test that results in occasional test failures.
The patchset is divided in three parts: Patch 1 merges the getpeername and getsockname implementation in the network layer, making further patches easier; Patch 2 splits out a helper used by
io_uring, like done for other network commands; Finally, patch 3 plumbs the new command inio_uring.The syscall path was tested by booting a Linux distro, which does all sorts of getsockname/getpeername syscalls. The
io_uringside was tested with a couple of new liburing subtests available at:
Quoted 5 lines
Quoted 3 lines
https://github.com/krisman/liburing.git -b socket
Based on top of Jens' for-next.
Ping netdev / networking folks on patches 1+2...
Hm, I think I was hoping to exercise the moderately recent MAINTAINERS entry here:
NETWORKING [SOCKETS]
M: Eric Dumazet <[email protected]>
M: Kuniyuki Iwashima <[email protected]>
M: Paolo Abeni <[email protected]>
M: Willem de Bruijn <[email protected]>
S: Maintained
F: include/linux/sock_diag.h
F: include/linux/socket.h
F: include/linux/sockptr.h
F: include/net/sock.h
F: include/net/sock_reuseport.h
F: include/uapi/linux/socket.h
F: net/core/*sock*
F: net/core/scm.c
F: net/socket.c
but now I realize that the submitter hasn't actually CCed the maintainers :S
Gabriel, please repost this with a more comprehensive CC list...