public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [RFC v2] tcp: add support for read with offset when using MSG_PEEK
@ 2023-06-23  2:12 Jon Maloy
  2023-06-23  2:36 ` Jon Maloy
  2023-06-23 11:02 ` Stefano Brivio
  0 siblings, 2 replies; 6+ messages in thread
From: Jon Maloy @ 2023-06-23  2:12 UTC (permalink / raw)
  To: sbrivio, lvivier, dgibson, jmaloy, passt-dev

When reading received messages with MSG_PEEK, we sometines have to read
the leading bytes of the stream several times, only to reach the bytes
we really want. This is clearly non-optimal.

What we would want is something similar to pread/preadv(), but working
even for tcp sockets. At the same time, we obviously don't want to add
any new arguments to the recv/recvmsg() calls.

In this commit, we allow the user to set iovec.iov_base in the first
vector entry to NULL. This tells the socket to skip the first entry,
hence making the iov_len field of that entry indicate the offset value.
This way, there is no need to add any new arguments.

This change is simple and non-intrusive, and should be safe addition to
the socket API. We have measured it to give a throughput improvement of
8-10 % for the protocol splicer 'passst', which is used in KubeVirt
containers.

Signed-off-by: Jon Maloy <jmaloy@redhat.com>

works with original msghdr

Signed-off-by: Jon Maloy <jmaloy@redhat.com>
---
 net/ipv4/tcp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 33f559f491c8..1d89337e89b6 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2428,6 +2428,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 	struct tcp_sock *tp = tcp_sk(sk);
 	int copied = 0;
 	u32 peek_seq;
+	u32 peek_offset;
 	u32 *seq;
 	unsigned long used;
 	int err;
@@ -2435,7 +2436,6 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 	long timeo;
 	struct sk_buff *skb, *last;
 	u32 urg_hole = 0;
-
 	err = -ENOTCONN;
 	if (sk->sk_state == TCP_LISTEN)
 		goto out;
@@ -2469,6 +2469,14 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 	if (flags & MSG_PEEK) {
 		peek_seq = tp->copied_seq;
 		seq = &peek_seq;
+		if (msg->msg_iter.iov[0].iov_base == NULL) {
+			peek_offset = msg->msg_iter.iov[0].iov_len;
+			msg->msg_iter.iov = &msg->msg_iter.iov[1];
+			msg->msg_iter.nr_segs -= 1;
+			msg->msg_iter.count -= peek_offset;
+			len -= peek_offset;
+			*seq += peek_offset;
+		}
 	}
 
 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
-- 
@@ -2428,6 +2428,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 	struct tcp_sock *tp = tcp_sk(sk);
 	int copied = 0;
 	u32 peek_seq;
+	u32 peek_offset;
 	u32 *seq;
 	unsigned long used;
 	int err;
@@ -2435,7 +2436,6 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 	long timeo;
 	struct sk_buff *skb, *last;
 	u32 urg_hole = 0;
-
 	err = -ENOTCONN;
 	if (sk->sk_state == TCP_LISTEN)
 		goto out;
@@ -2469,6 +2469,14 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 	if (flags & MSG_PEEK) {
 		peek_seq = tp->copied_seq;
 		seq = &peek_seq;
+		if (msg->msg_iter.iov[0].iov_base == NULL) {
+			peek_offset = msg->msg_iter.iov[0].iov_len;
+			msg->msg_iter.iov = &msg->msg_iter.iov[1];
+			msg->msg_iter.nr_segs -= 1;
+			msg->msg_iter.count -= peek_offset;
+			len -= peek_offset;
+			*seq += peek_offset;
+		}
 	}
 
 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
-- 
2.39.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC v2] tcp: add support for read with offset when using MSG_PEEK
  2023-06-23  2:12 [RFC v2] tcp: add support for read with offset when using MSG_PEEK Jon Maloy
@ 2023-06-23  2:36 ` Jon Maloy
  2023-06-23 11:02 ` Stefano Brivio
  1 sibling, 0 replies; 6+ messages in thread
From: Jon Maloy @ 2023-06-23  2:36 UTC (permalink / raw)
  To: passt-dev, Stefano Brivio, David Gibson, Laurent Vivier

(Added <passt-dev@passt.top> to distribution list.

I found that my first approach was too simplistic, since it only moved 
the reading
area forward in the receive buffer, but continue to fill in iov[0] with 
the indicated
length.
This commit exactly what we want: we indicate a NULL pointer in iov[0], but
want the actually read bytes to end up in the remaining entries, and also
the returned value to indicate the actually read length.

I look forward to feedback to this, then I can hopefully post it to the 
netdev
list next week.

///jon


On 2023-06-22 22:12, Jon Maloy wrote:
> When reading received messages with MSG_PEEK, we sometines have to read
> the leading bytes of the stream several times, only to reach the bytes
> we really want. This is clearly non-optimal.
>
> What we would want is something similar to pread/preadv(), but working
> even for tcp sockets. At the same time, we obviously don't want to add
> any new arguments to the recv/recvmsg() calls.
>
> In this commit, we allow the user to set iovec.iov_base in the first
> vector entry to NULL. This tells the socket to skip the first entry,
> hence making the iov_len field of that entry indicate the offset value.
> This way, there is no need to add any new arguments.
>
> This change is simple and non-intrusive, and should be safe addition to
> the socket API. We have measured it to give a throughput improvement of
> 8-10 % for the protocol splicer 'passst', which is used in KubeVirt
> containers.
>
> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
>
> works with original msghdr
>
> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
> ---
>   net/ipv4/tcp.c | 10 +++++++++-
>   1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 33f559f491c8..1d89337e89b6 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -2428,6 +2428,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>   	struct tcp_sock *tp = tcp_sk(sk);
>   	int copied = 0;
>   	u32 peek_seq;
> +	u32 peek_offset;
>   	u32 *seq;
>   	unsigned long used;
>   	int err;
> @@ -2435,7 +2436,6 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>   	long timeo;
>   	struct sk_buff *skb, *last;
>   	u32 urg_hole = 0;
> -
>   	err = -ENOTCONN;
>   	if (sk->sk_state == TCP_LISTEN)
>   		goto out;
> @@ -2469,6 +2469,14 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>   	if (flags & MSG_PEEK) {
>   		peek_seq = tp->copied_seq;
>   		seq = &peek_seq;
> +		if (msg->msg_iter.iov[0].iov_base == NULL) {
> +			peek_offset = msg->msg_iter.iov[0].iov_len;
> +			msg->msg_iter.iov = &msg->msg_iter.iov[1];
> +			msg->msg_iter.nr_segs -= 1;
> +			msg->msg_iter.count -= peek_offset;
> +			len -= peek_offset;
> +			*seq += peek_offset;
> +		}
>   	}
>   
>   	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v2] tcp: add support for read with offset when using MSG_PEEK
  2023-06-23  2:12 [RFC v2] tcp: add support for read with offset when using MSG_PEEK Jon Maloy
  2023-06-23  2:36 ` Jon Maloy
@ 2023-06-23 11:02 ` Stefano Brivio
  2023-09-04 17:14   ` Jon Maloy
  1 sibling, 1 reply; 6+ messages in thread
From: Stefano Brivio @ 2023-06-23 11:02 UTC (permalink / raw)
  To: Jon Maloy; +Cc: lvivier, dgibson, passt-dev

On Thu, 22 Jun 2023 22:12:27 -0400
Jon Maloy <jmaloy@redhat.com> wrote:

> When reading received messages with MSG_PEEK, we sometines have to read
> the leading bytes of the stream several times, only to reach the bytes
> we really want. This is clearly non-optimal.
> 
> What we would want is something similar to pread/preadv(), but working
> even for tcp sockets. At the same time, we obviously don't want to add
> any new arguments to the recv/recvmsg() calls.
> 
> In this commit, we allow the user to set iovec.iov_base in the first
> vector entry to NULL. This tells the socket to skip the first entry,
> hence making the iov_len field of that entry indicate the offset value.
> This way, there is no need to add any new arguments.

Ah-ha! I'm glad you found an acceptable way to pass a NULL pointer
there. :)

> This change is simple and non-intrusive, and should be safe addition to
> the socket API. We have measured it to give a throughput improvement of

...it would be nice to also do a bit of profiling with perf(1) --
that's where I originally noticed we were wasting cycles on filling up
tcp_buf_discard. Plus, sure, there's also some value in dropping a
useless 16 MiB buffer.

If you need examples/inspiration: the pasta (automated) demo shows
that, skip at 9:20 in: https://passt.top/passt/about/#pasta_2 (the one
on the left)

and that's simply done like this:
  https://passt.top/passt/tree/test/demo/pasta#n163

> 8-10 % for the protocol splicer 'passst', which is used in KubeVirt

passt

> containers.

Those are virtual machines in containers -- but other than KubeVirt,
passt is generally supported in libvirt (>= 9.2) with QEMU (>= 7.2)

> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
> 
> works with original msghdr
> 
> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
> ---
>  net/ipv4/tcp.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 33f559f491c8..1d89337e89b6 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -2428,6 +2428,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>  	struct tcp_sock *tp = tcp_sk(sk);
>  	int copied = 0;
>  	u32 peek_seq;
> +	u32 peek_offset;

Kernel networking code observes the reverse-Christmas tree notation (at
least for new additions), this would need to go after *tp:

	struct tcp_sock *tp = tcp_sk(sk);
	u32 peek_offset;

>  	u32 *seq;
>  	unsigned long used;
>  	int err;
> @@ -2435,7 +2436,6 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>  	long timeo;
>  	struct sk_buff *skb, *last;
>  	u32 urg_hole = 0;
> -

Probably not intended.

>  	err = -ENOTCONN;
>  	if (sk->sk_state == TCP_LISTEN)
>  		goto out;
> @@ -2469,6 +2469,14 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>  	if (flags & MSG_PEEK) {
>  		peek_seq = tp->copied_seq;
>  		seq = &peek_seq;
> +		if (msg->msg_iter.iov[0].iov_base == NULL) {
> +			peek_offset = msg->msg_iter.iov[0].iov_len;
> +			msg->msg_iter.iov = &msg->msg_iter.iov[1];
> +			msg->msg_iter.nr_segs -= 1;

Do you also need to make sure that nr_segs > 1 if iov[0].iov_base is
NULL? I'm not sure if we need to check explicitly that msg_iter.iov[1]
is valid here (but I haven't followed the whole path from the syscall
handler).

> +			msg->msg_iter.count -= peek_offset;
> +			len -= peek_offset;
> +			*seq += peek_offset;
> +		}
>  	}
>  
>  	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);

-- 
Stefano


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v2] tcp: add support for read with offset when using MSG_PEEK
  2023-06-23 11:02 ` Stefano Brivio
@ 2023-09-04 17:14   ` Jon Maloy
  2023-09-05  2:16     ` David Gibson
  0 siblings, 1 reply; 6+ messages in thread
From: Jon Maloy @ 2023-09-04 17:14 UTC (permalink / raw)
  To: Stefano Brivio; +Cc: lvivier, dgibson, passt-dev



On 2023-06-23 07:02, Stefano Brivio wrote:
> On Thu, 22 Jun 2023 22:12:27 -0400
> Jon Maloy <jmaloy@redhat.com> wrote:
>
>> When reading received messages with MSG_PEEK, we sometines have to read
>> the leading bytes of the stream several times, only to reach the bytes
>> we really want. This is clearly non-optimal.
>>
>> What we would want is something similar to pread/preadv(), but working
>> even for tcp sockets. At the same time, we obviously don't want to add
>> any new arguments to the recv/recvmsg() calls.
>>
>> In this commit, we allow the user to set iovec.iov_base in the first
>> vector entry to NULL. This tells the socket to skip the first entry,
>> hence making the iov_len field of that entry indicate the offset value.
>> This way, there is no need to add any new arguments.
> Ah-ha! I'm glad you found an acceptable way to pass a NULL pointer
> there. :)
>
>> This change is simple and non-intrusive, and should be safe addition to
>> the socket API. We have measured it to give a throughput improvement of
> ...it would be nice to also do a bit of profiling with perf(1) --
> that's where I originally noticed we were wasting cycles on filling up
> tcp_buf_discard. Plus, sure, there's also some value in dropping a
> useless 16 MiB buffer.
>
> If you need examples/inspiration: the pasta (automated) demo shows
> that, skip at 9:20 in: https://passt.top/passt/about/#pasta_2 (the one
> on the left)
>
> and that's simply done like this:
>    https://passt.top/passt/tree/test/demo/pasta#n163
This looks like a script language, but I don't recognize it. How do I 
run it?

>
>> 8-10 % for the protocol splicer 'passst', which is used in KubeVirt
Now at 20+ %
> passt
>
>> containers.
> Those are virtual machines in containers -- but other than KubeVirt,
> passt is generally supported in libvirt (>= 9.2) with QEMU (>= 7.2)
>
>> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
>>
>> works with original msghdr
>>
>> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
>> ---
>>   net/ipv4/tcp.c | 10 +++++++++-
>>   1 file changed, 9 insertions(+), 1 deletion(-)
>>
>> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
>> index 33f559f491c8..1d89337e89b6 100644
>> --- a/net/ipv4/tcp.c
>> +++ b/net/ipv4/tcp.c
>> @@ -2428,6 +2428,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>>   	struct tcp_sock *tp = tcp_sk(sk);
>>   	int copied = 0;
>>   	u32 peek_seq;
>> +	u32 peek_offset;
> Kernel networking code observes the reverse-Christmas tree notation (at
> least for new additions), this would need to go after *tp:
>
> 	struct tcp_sock *tp = tcp_sk(sk);
> 	u32 peek_offset;
>
>>   	u32 *seq;
>>   	unsigned long used;
>>   	int err;
>> @@ -2435,7 +2436,6 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>>   	long timeo;
>>   	struct sk_buff *skb, *last;
>>   	u32 urg_hole = 0;
>> -
> Probably not intended.
>
>>   	err = -ENOTCONN;
>>   	if (sk->sk_state == TCP_LISTEN)
>>   		goto out;
>> @@ -2469,6 +2469,14 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
>>   	if (flags & MSG_PEEK) {
>>   		peek_seq = tp->copied_seq;
>>   		seq = &peek_seq;
>> +		if (msg->msg_iter.iov[0].iov_base == NULL) {
>> +			peek_offset = msg->msg_iter.iov[0].iov_len;
>> +			msg->msg_iter.iov = &msg->msg_iter.iov[1];
>> +			msg->msg_iter.nr_segs -= 1;
> Do you also need to make sure that nr_segs > 1 if iov[0].iov_base is
> NULL? I'm not sure if we need to check explicitly that msg_iter.iov[1]
> is valid here (but I haven't followed the whole path from the syscall
> handler).
Checked. This is taken care of by the generic code.

///jon

>
>> +			msg->msg_iter.count -= peek_offset;
>> +			len -= peek_offset;
>> +			*seq += peek_offset;
>> +		}
>>   	}
>>   
>>   	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v2] tcp: add support for read with offset when using MSG_PEEK
  2023-09-04 17:14   ` Jon Maloy
@ 2023-09-05  2:16     ` David Gibson
  2023-09-05  6:19       ` Stefano Brivio
  0 siblings, 1 reply; 6+ messages in thread
From: David Gibson @ 2023-09-05  2:16 UTC (permalink / raw)
  To: Jon Maloy; +Cc: Stefano Brivio, lvivier, dgibson, passt-dev

[-- Attachment #1: Type: text/plain, Size: 2224 bytes --]

On Mon, Sep 04, 2023 at 01:14:17PM -0400, Jon Maloy wrote:
> 
> 
> On 2023-06-23 07:02, Stefano Brivio wrote:
> > On Thu, 22 Jun 2023 22:12:27 -0400
> > Jon Maloy <jmaloy@redhat.com> wrote:
> > 
> > > When reading received messages with MSG_PEEK, we sometines have to read
> > > the leading bytes of the stream several times, only to reach the bytes
> > > we really want. This is clearly non-optimal.
> > > 
> > > What we would want is something similar to pread/preadv(), but working
> > > even for tcp sockets. At the same time, we obviously don't want to add
> > > any new arguments to the recv/recvmsg() calls.
> > > 
> > > In this commit, we allow the user to set iovec.iov_base in the first
> > > vector entry to NULL. This tells the socket to skip the first entry,
> > > hence making the iov_len field of that entry indicate the offset value.
> > > This way, there is no need to add any new arguments.
> > Ah-ha! I'm glad you found an acceptable way to pass a NULL pointer
> > there. :)
> > 
> > > This change is simple and non-intrusive, and should be safe addition to
> > > the socket API. We have measured it to give a throughput improvement of
> > ...it would be nice to also do a bit of profiling with perf(1) --
> > that's where I originally noticed we were wasting cycles on filling up
> > tcp_buf_discard. Plus, sure, there's also some value in dropping a
> > useless 16 MiB buffer.
> > 
> > If you need examples/inspiration: the pasta (automated) demo shows
> > that, skip at 9:20 in: https://passt.top/passt/about/#pasta_2 (the one
> > on the left)
> > 
> > and that's simply done like this:
> >    https://passt.top/passt/tree/test/demo/pasta#n163
> This looks like a script language, but I don't recognize it. How do I run
> it?

It's the hand rolled script language of the passt tests.  The
interpreter is in test/lib/test.  Easiest way to run it is probably to
"make check" in test/ of the passt tree.  If you want to selectively
run certain things, edit test/run.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC v2] tcp: add support for read with offset when using MSG_PEEK
  2023-09-05  2:16     ` David Gibson
@ 2023-09-05  6:19       ` Stefano Brivio
  0 siblings, 0 replies; 6+ messages in thread
From: Stefano Brivio @ 2023-09-05  6:19 UTC (permalink / raw)
  To: Jon Maloy; +Cc: David Gibson, lvivier, dgibson, passt-dev

On Tue, 5 Sep 2023 12:16:11 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Mon, Sep 04, 2023 at 01:14:17PM -0400, Jon Maloy wrote:
> > 
> > 
> > On 2023-06-23 07:02, Stefano Brivio wrote:  
> > > On Thu, 22 Jun 2023 22:12:27 -0400
> > > Jon Maloy <jmaloy@redhat.com> wrote:
> > >   
> > > > When reading received messages with MSG_PEEK, we sometines have to read
> > > > the leading bytes of the stream several times, only to reach the bytes
> > > > we really want. This is clearly non-optimal.
> > > > 
> > > > What we would want is something similar to pread/preadv(), but working
> > > > even for tcp sockets. At the same time, we obviously don't want to add
> > > > any new arguments to the recv/recvmsg() calls.
> > > > 
> > > > In this commit, we allow the user to set iovec.iov_base in the first
> > > > vector entry to NULL. This tells the socket to skip the first entry,
> > > > hence making the iov_len field of that entry indicate the offset value.
> > > > This way, there is no need to add any new arguments.  
> > > Ah-ha! I'm glad you found an acceptable way to pass a NULL pointer
> > > there. :)
> > >   
> > > > This change is simple and non-intrusive, and should be safe addition to
> > > > the socket API. We have measured it to give a throughput improvement of  
> > > ...it would be nice to also do a bit of profiling with perf(1) --
> > > that's where I originally noticed we were wasting cycles on filling up
> > > tcp_buf_discard. Plus, sure, there's also some value in dropping a
> > > useless 16 MiB buffer.
> > > 
> > > If you need examples/inspiration: the pasta (automated) demo shows
> > > that, skip at 9:20 in: https://passt.top/passt/about/#pasta_2 (the one
> > > on the left)
> > > 
> > > and that's simply done like this:
> > >    https://passt.top/passt/tree/test/demo/pasta#n163  
> > This looks like a script language, but I don't recognize it. How do I run
> > it?  
> 
> It's the hand rolled script language of the passt tests.  The
> interpreter is in test/lib/test.  Easiest way to run it is probably to
> "make check" in test/ of the passt tree.  If you want to selectively
> run certain things, edit test/run.

...some more details at test/README.md.

-- 
Stefano


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-09-05  6:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-23  2:12 [RFC v2] tcp: add support for read with offset when using MSG_PEEK Jon Maloy
2023-06-23  2:36 ` Jon Maloy
2023-06-23 11:02 ` Stefano Brivio
2023-09-04 17:14   ` Jon Maloy
2023-09-05  2:16     ` David Gibson
2023-09-05  6:19       ` Stefano Brivio

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).