Introduce getsockname io_uring_cmd
Subsystems (4)
v2 cover Introduce getsockname io_uring_cmd
Since V1:
- minor style fixes
- Resend with (more) maintainers cc'ed
- rebased to axboe/for-next.
--
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
---
To: Jens Axboe <[email protected]>
CC: [email protected]
CC: [email protected]
CC: Jakub Kicinski <[email protected]>
CC: David S. Miller <[email protected]>
CC: Eric Dumazet <[email protected]>
CC: Kuniyuki Iwashima <[email protected]>
CC: Paolo Abeni <[email protected]>
CC: Willem de Bruijn <[email protected]>
CC: Simon Horman <[email protected]>
Base: axboe/for-next
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 | 23 ++++++++++++ net/compat.c | 4 +- net/socket.c | 69 +++++++++++------------------------ 5 files changed, 51 insertions(+), 52 deletions(-)
Signature · 1 line
2.51.0
PATCH v2 1/3 socket: Unify getsockname and getpeername implementation
They 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 v2 2/3 socket: Split out a getsockname helper for io_uring
In reply to Gabriel Krisman BertaziSimilar 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 v2 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 | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+)
include/uapi/linux/io_uring.h +1 -0
@@ -1010,6 +1010,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 +23 -0
@@ -132,6 +132,27 @@ 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; +unsigned int peer; +int __user *ulen; + +if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags) +return -EINVAL ; + + uaddr =u64_to_user_ptr (READ_ONCE (sqe->addr)); + ulen =u64_to_user_ptr (sqe->addr3); + peer =READ_ONCE (sqe->optlen); +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 +180,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