Introduce getsockname io_uring_cmd
Subsystems (4)
v4 cover Introduce getsockname io_uring_cmd
Since V3:
- Fix passing of 'peer' in io_uring side.
Since V2:
- Move sockaddr_storage to do_sockname
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
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 | 22 ++++++++++++ net/compat.c | 4 +-- net/socket.c | 67 +++++++++++------------------------ 5 files changed, 49 insertions(+), 51 deletions(-)
Signature · 1 line
2.51.0
PATCH v4 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.
---
v2 to v3:
- Adjust comment (Kuniyuki)
--- 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 v4 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.
---
v2 -> v3:
- Move sockaddr_storage parameter into do_getsockname(Kuniyuki)
--- include/linux/socket.h | 2 ++ net/socket.c | 36 ++++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 16 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,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 +20 -16
@@ -2127,39 +2127,43 @@ SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,return __sys_connect (fd, uservaddr, addrlen); } -/* -* Get the remote or local address ('name') of a socket object. Move the -* obtained name to user space. -*/ -int __sys_getsockname (int fd,struct sockaddr __user *usockaddr, -int __user *usockaddr_len,int peer) +int do_getsockname (struct socket *sock,int peer, +struct sockaddr __user *usockaddr,int __user *usockaddr_len) { -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); } +/* +* Get the remote or local address ('name') of a socket object. Move the +* obtained name to user space. +*/ +int __sys_getsockname (int fd,struct sockaddr __user *usockaddr, +int __user *usockaddr_len,int peer) +{ +struct socket *sock; +CLASS (fd, f)(fd); + +if (fd_empty (f)) +return -EBADF ; + sock =sock_from_file (fd_file (f)); +if (unlikely (!sock)) +return -ENOTSOCK ; +return do_getsockname (sock, peer, usockaddr, usockaddr_len); +} + SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr, int __user *, usockaddr_len) {
Signature · 1 line
2.51.0
Re: [PATCH v4 2/3] socket: Split out a getsockname helper for io_uring
Quoted 5 lines · Gabriel Krisman Bertazi
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.Signed-off-by Gabriel Krisman Bertazi <[email protected]>
PATCH v4 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
---v3->v4:
- properly pass peer down to network layer (Stefan)v2->v3:
- Don't pass sockaddr_storage pointer parameter
--- include/uapi/linux/io_uring.h | 1 + io_uring/cmd_net.c | 22 ++++++++++++++++++++++ 2 files changed, 23 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 +22 -0
@@ -132,6 +132,26 @@ 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 __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, peer, 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 +179,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 v4 3/3] io_uring: Introduce getsockname io_uring cmd
Quoted 4 lines · Gabriel Krisman Bertazi
io_uring/cmd_net.c +4
-0+static int io_uring_cmd_getsockname (struct socket *sock, +struct io_uring_cmd *cmd, +unsigned int issue_flags) +{
io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 148
Quoted 10 lines
io_uring/cmd_net.c +10
-0+const struct io_uring_sqe *sqe = cmd->sqe; +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);
Missing READ_ONCE() for this one. But I can just amend that, pretty trivial.
Outside of that, this looks good to me. Bigger question is how to stage this, in terms of the two networking patches. Jakub?
Signature · 1 line
Jens Axboe
Quoted 11 lines · Gabriel Krisman Bertazi
Since V3:
- Fix passing of 'peer' inio_uringside.
Since V2:
- Movesockaddr_storagetodo_sockname
Since V1:
- minor style fixes
- Resend with (more) maintainers cc'ed
- rebased to axboe/for-next.
--[...]
Applied, thanks!
[1/3] socket: Unify getsockname and getpeername implementation
commit: 4677e78800bbde62a9edce0eb3b40c775ec55e0d
[2/3] socket: Split out a getsockname helper for io_uring
commit: d73c1677087391379441c0bb444c7fb4238fc6e7
[3/3] io_uring: Introduce getsockname io_uring cmd
commit: 5d24321e4c159088604512d7a5c5cf634d23e01aBest regards,
Signature · 1 line
Jens Axboe