diff mbox series

[bpf-next,v1,1/5] af_unix: add read_sock for stream socket types

Message ID 20210727001252.1287673-2-jiang.wang@bytedance.com
State Superseded
Headers show
Series sockmap: add sockmap support for unix stream socket | expand

Commit Message

Jiang Wang July 27, 2021, 12:12 a.m. UTC
To support sockmap for af_unix stream type, implement
read_sock, which is similar to the read_sock for unix
dgram sockets.

Signed-off-by: Jiang Wang <jiang.wang@bytedance.com>
Reviewed-by: Cong Wang <cong.wang@bytedance.com>.
---
 net/unix/af_unix.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Jakub Sitnicki July 29, 2021, 8:37 a.m. UTC | #1
On Tue, Jul 27, 2021 at 02:12 AM CEST, Jiang Wang wrote:
> To support sockmap for af_unix stream type, implement
> read_sock, which is similar to the read_sock for unix
> dgram sockets.
>
> Signed-off-by: Jiang Wang <jiang.wang@bytedance.com>
> Reviewed-by: Cong Wang <cong.wang@bytedance.com>.
> ---
>  net/unix/af_unix.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index 89927678c..32eeb4a6a 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -672,6 +672,8 @@ static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
>  static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
>  static int unix_read_sock(struct sock *sk, read_descriptor_t *desc,
>  			  sk_read_actor_t recv_actor);
> +static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
> +				 sk_read_actor_t recv_actor);
>  static int unix_dgram_connect(struct socket *, struct sockaddr *,
>  			      int, int);
>  static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
> @@ -725,6 +727,7 @@ static const struct proto_ops unix_stream_ops = {
>  	.shutdown =	unix_shutdown,
>  	.sendmsg =	unix_stream_sendmsg,
>  	.recvmsg =	unix_stream_recvmsg,
> +	.read_sock =	unix_stream_read_sock,
>  	.mmap =		sock_no_mmap,
>  	.sendpage =	unix_stream_sendpage,
>  	.splice_read =	unix_stream_splice_read,
> @@ -2311,6 +2314,15 @@ struct unix_stream_read_state {
>  	unsigned int splice_flags;
>  };
>
> +static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
> +				 sk_read_actor_t recv_actor)
> +{
> +	if (unlikely(sk->sk_state != TCP_ESTABLISHED))
> +		return -EINVAL;

tcp_read_sock returns -ENOTCONN if socket is not connected.

For the sake of being consistent, and in case we start propagating the
error up the call chain, I'd use the same error code.

> +
> +	return unix_read_sock(sk, desc, recv_actor);
> +}
> +
>  static int unix_stream_read_generic(struct unix_stream_read_state *state,
>  				    bool freezable)
>  {
diff mbox series

Patch

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 89927678c..32eeb4a6a 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -672,6 +672,8 @@  static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
 static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
 static int unix_read_sock(struct sock *sk, read_descriptor_t *desc,
 			  sk_read_actor_t recv_actor);
+static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
+				 sk_read_actor_t recv_actor);
 static int unix_dgram_connect(struct socket *, struct sockaddr *,
 			      int, int);
 static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
@@ -725,6 +727,7 @@  static const struct proto_ops unix_stream_ops = {
 	.shutdown =	unix_shutdown,
 	.sendmsg =	unix_stream_sendmsg,
 	.recvmsg =	unix_stream_recvmsg,
+	.read_sock =	unix_stream_read_sock,
 	.mmap =		sock_no_mmap,
 	.sendpage =	unix_stream_sendpage,
 	.splice_read =	unix_stream_splice_read,
@@ -2311,6 +2314,15 @@  struct unix_stream_read_state {
 	unsigned int splice_flags;
 };
 
+static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
+				 sk_read_actor_t recv_actor)
+{
+	if (unlikely(sk->sk_state != TCP_ESTABLISHED))
+		return -EINVAL;
+
+	return unix_read_sock(sk, desc, recv_actor);
+}
+
 static int unix_stream_read_generic(struct unix_stream_read_state *state,
 				    bool freezable)
 {