Skip to content

Commit 5d24321

Browse files
krismanaxboe
authored andcommitted
io_uring: Introduce getsockname io_uring cmd
Introduce 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] axboe/liburing#1356 Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d73c167 commit 5d24321

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

include/uapi/linux/io_uring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ enum io_uring_socket_op {
10091009
SOCKET_URING_OP_GETSOCKOPT,
10101010
SOCKET_URING_OP_SETSOCKOPT,
10111011
SOCKET_URING_OP_TX_TIMESTAMP,
1012+
SOCKET_URING_OP_GETSOCKNAME,
10121013
};
10131014

10141015
/*

io_uring/cmd_net.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ static int io_uring_cmd_timestamp(struct socket *sock,
132132
return -EAGAIN;
133133
}
134134

135+
static int io_uring_cmd_getsockname(struct socket *sock,
136+
struct io_uring_cmd *cmd,
137+
unsigned int issue_flags)
138+
{
139+
const struct io_uring_sqe *sqe = cmd->sqe;
140+
struct sockaddr __user *uaddr;
141+
unsigned int peer;
142+
int __user *ulen;
143+
144+
if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags)
145+
return -EINVAL;
146+
147+
uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
148+
ulen = u64_to_user_ptr(sqe->addr3);
149+
peer = READ_ONCE(sqe->optlen);
150+
if (peer > 1)
151+
return -EINVAL;
152+
return do_getsockname(sock, peer, uaddr, ulen);
153+
}
154+
135155
int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)
136156
{
137157
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)
159179
return io_uring_cmd_setsockopt(sock, cmd, issue_flags);
160180
case SOCKET_URING_OP_TX_TIMESTAMP:
161181
return io_uring_cmd_timestamp(sock, cmd, issue_flags);
182+
case SOCKET_URING_OP_GETSOCKNAME:
183+
return io_uring_cmd_getsockname(sock, cmd, issue_flags);
162184
default:
163185
return -EOPNOTSUPP;
164186
}

0 commit comments

Comments
 (0)