public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 net] selftests: af_unix: drain after peek and verify SO_PEEK_OFF reset
@ 2026-01-22  3:36 Soichiro Ueda
  2026-01-22 21:33 ` Willem de Bruijn
  0 siblings, 1 reply; 3+ messages in thread
From: Soichiro Ueda @ 2026-01-22  3:36 UTC (permalink / raw)
  To: Kuniyuki Iwashima, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev
  Cc: Simon Horman, Soichiro Ueda, Miao Wang

Extend the so_peek_off selftest to validate behavior after MSG_PEEK.

After exercising SO_PEEK_OFF via MSG_PEEK, drain the receive queue with a
non-peek recv() and verify that it can receive all the content in the
buffer and SO_PEEK_OFF returns back to 0.

This improvement is suggested by Miao Wang when the so_peek_off selftest
was added.

Link: https://lore.kernel.org/all/7B657CC7-B5CA-46D2-8A4B-8AB5FB83C6DA@gmail.com/
Suggested-by: Miao Wang <shankerwangmiao@gmail.com>
Signed-off-by: Soichiro Ueda <the.latticeheart@gmail.com>
---
 .../selftests/net/af_unix/so_peek_off.c       | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/tools/testing/selftests/net/af_unix/so_peek_off.c b/tools/testing/selftests/net/af_unix/so_peek_off.c
index 86e7b0fb522d..813e3b3655d3 100644
--- a/tools/testing/selftests/net/af_unix/so_peek_off.c
+++ b/tools/testing/selftests/net/af_unix/so_peek_off.c
@@ -76,6 +76,19 @@ FIXTURE_TEARDOWN(so_peek_off)
 		ASSERT_STREQ(str, buf);				\
 	} while (0)
 
+#define peekoffeq(fd, expected)					\
+	do {							\
+		int off = -1;					\
+		socklen_t optlen = sizeof(off);		\
+		int ret;					\
+								\
+		ret = getsockopt(fd, SOL_SOCKET, SO_PEEK_OFF,	\
+				 &off, &optlen);		\
+		ASSERT_EQ(0, ret);				\
+		ASSERT_EQ((socklen_t)sizeof(off), optlen);	\
+		ASSERT_EQ(expected, off);			\
+	} while (0)
+
 #define async							\
 	for (pid_t pid = (pid = fork(),				\
 			  pid < 0 ?				\
@@ -92,6 +105,14 @@ TEST_F(so_peek_off, single_chunk)
 
 	recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
 	recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+
+	if (variant->type == SOCK_STREAM) {
+		recveq(self->fd[1], "aaaa", 4, 0);
+		recveq(self->fd[1], "bbbb", 100, 0);
+	} else {
+		recveq(self->fd[1], "aaaabbbb", 100, 0);
+	}
+	peekoffeq(self->fd[1], 0);
 }
 
 TEST_F(so_peek_off, two_chunks)
@@ -101,6 +122,13 @@ TEST_F(so_peek_off, two_chunks)
 
 	recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
 	recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+
+	if (variant->type == SOCK_STREAM)
+		recveq(self->fd[1], "aaaa", 4, 0);
+	else
+		recveq(self->fd[1], "aaaa", 100, 0);
+	recveq(self->fd[1], "bbbb", 100, 0);
+	peekoffeq(self->fd[1], 0);
 }
 
 TEST_F(so_peek_off, two_chunks_blocking)
@@ -119,6 +147,13 @@ TEST_F(so_peek_off, two_chunks_blocking)
 
 	/* goto again; -> goto redo; in unix_stream_read_generic(). */
 	recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+
+	if (variant->type == SOCK_STREAM)
+		recveq(self->fd[1], "aaaa", 4, 0);
+	else
+		recveq(self->fd[1], "aaaa", 100, 0);
+	recveq(self->fd[1], "bbbb", 100, 0);
+	peekoffeq(self->fd[1], 0);
 }
 
 TEST_F(so_peek_off, two_chunks_overlap)
@@ -137,6 +172,13 @@ TEST_F(so_peek_off, two_chunks_overlap)
 		recveq(self->fd[1], "aa", 100, MSG_PEEK);
 		recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
 	}
+
+	if (variant->type == SOCK_STREAM)
+		recveq(self->fd[1], "aaaa", 4, 0);
+	else
+		recveq(self->fd[1], "aaaa", 100, 0);
+	recveq(self->fd[1], "bbbb", 100, 0);
+	peekoffeq(self->fd[1], 0);
 }
 
 TEST_F(so_peek_off, two_chunks_overlap_blocking)
@@ -157,6 +199,13 @@ TEST_F(so_peek_off, two_chunks_overlap_blocking)
 	recveq(self->fd[1], "aa", 100, MSG_PEEK);
 
 	recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+
+	if (variant->type == SOCK_STREAM)
+		recveq(self->fd[1], "aaaa", 4, 0);
+	else
+		recveq(self->fd[1], "aaaa", 100, 0);
+	recveq(self->fd[1], "bbbb", 100, 0);
+	peekoffeq(self->fd[1], 0);
 }
 
 TEST_HARNESS_MAIN
-- 
2.52.0


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

* Re: [PATCH v1 net] selftests: af_unix: drain after peek and verify SO_PEEK_OFF reset
  2026-01-22  3:36 [PATCH v1 net] selftests: af_unix: drain after peek and verify SO_PEEK_OFF reset Soichiro Ueda
@ 2026-01-22 21:33 ` Willem de Bruijn
  2026-02-12 22:49   ` Soichiro Ueda
  0 siblings, 1 reply; 3+ messages in thread
From: Willem de Bruijn @ 2026-01-22 21:33 UTC (permalink / raw)
  To: Soichiro Ueda, Kuniyuki Iwashima, David S. Miller, Eric Dumazet,
	Jakub Kicinski, netdev
  Cc: Simon Horman, Soichiro Ueda, Miao Wang

Soichiro Ueda wrote:
> Extend the so_peek_off selftest to validate behavior after MSG_PEEK.
> 
> After exercising SO_PEEK_OFF via MSG_PEEK, drain the receive queue with a
> non-peek recv() and verify that it can receive all the content in the
> buffer and SO_PEEK_OFF returns back to 0.
> 
> This improvement is suggested by Miao Wang when the so_peek_off selftest
> was added.
> 
> Link: https://lore.kernel.org/all/7B657CC7-B5CA-46D2-8A4B-8AB5FB83C6DA@gmail.com/
> Suggested-by: Miao Wang <shankerwangmiao@gmail.com>
> Signed-off-by: Soichiro Ueda <the.latticeheart@gmail.com>
> ---
>  .../selftests/net/af_unix/so_peek_off.c       | 49 +++++++++++++++++++
>  1 file changed, 49 insertions(+)
> 
> diff --git a/tools/testing/selftests/net/af_unix/so_peek_off.c b/tools/testing/selftests/net/af_unix/so_peek_off.c
> index 86e7b0fb522d..813e3b3655d3 100644
> --- a/tools/testing/selftests/net/af_unix/so_peek_off.c
> +++ b/tools/testing/selftests/net/af_unix/so_peek_off.c
> @@ -76,6 +76,19 @@ FIXTURE_TEARDOWN(so_peek_off)
>  		ASSERT_STREQ(str, buf);				\
>  	} while (0)
>  
> +#define peekoffeq(fd, expected)					\
> +	do {							\
> +		int off = -1;					\
> +		socklen_t optlen = sizeof(off);		\
> +		int ret;					\
> +								\
> +		ret = getsockopt(fd, SOL_SOCKET, SO_PEEK_OFF,	\
> +				 &off, &optlen);		\
> +		ASSERT_EQ(0, ret);				\
> +		ASSERT_EQ((socklen_t)sizeof(off), optlen);	\
> +		ASSERT_EQ(expected, off);			\
> +	} while (0)
> +
>  #define async							\
>  	for (pid_t pid = (pid = fork(),				\
>  			  pid < 0 ?				\
> @@ -92,6 +105,14 @@ TEST_F(so_peek_off, single_chunk)
>  
>  	recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
>  	recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
> +
> +	if (variant->type == SOCK_STREAM) {
> +		recveq(self->fd[1], "aaaa", 4, 0);
> +		recveq(self->fd[1], "bbbb", 100, 0);
> +	} else {
> +		recveq(self->fd[1], "aaaabbbb", 100, 0);
> +	}
> +	peekoffeq(self->fd[1], 0);

Do you want to test peekoffeq before the non-peek read too?

>  }
>  
>  TEST_F(so_peek_off, two_chunks)
> @@ -101,6 +122,13 @@ TEST_F(so_peek_off, two_chunks)
>  
>  	recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
>  	recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
> +
> +	if (variant->type == SOCK_STREAM)
> +		recveq(self->fd[1], "aaaa", 4, 0);
> +	else
> +		recveq(self->fd[1], "aaaa", 100, 0);

Why this difference in length?

Because stream read will block if > 4, while datagram does not?

If so, can perhaps use 4 for both or explicitly use non-blocking read.

> +	recveq(self->fd[1], "bbbb", 100, 0);
> +	peekoffeq(self->fd[1], 0);
>  }

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

* Re: [PATCH v1 net] selftests: af_unix: drain after peek and verify SO_PEEK_OFF reset
  2026-01-22 21:33 ` Willem de Bruijn
@ 2026-02-12 22:49   ` Soichiro Ueda
  0 siblings, 0 replies; 3+ messages in thread
From: Soichiro Ueda @ 2026-02-12 22:49 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: Kuniyuki Iwashima, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netdev, Simon Horman, Miao Wang

Hi Willem,
Thank you for your review and helpful comments.

> Soichiro Ueda wrote:
> > Extend the so_peek_off selftest to validate behavior after MSG_PEEK.
> >
> > After exercising SO_PEEK_OFF via MSG_PEEK, drain the receive queue with a
> > non-peek recv() and verify that it can receive all the content in the
> > buffer and SO_PEEK_OFF returns back to 0.
> >
> > This improvement is suggested by Miao Wang when the so_peek_off selftest
> > was added.
> >
> > Link: https://lore.kernel.org/all/7B657CC7-B5CA-46D2-8A4B-8AB5FB83C6DA@gmail.com/
> > Suggested-by: Miao Wang <shankerwangmiao@gmail.com>
> > Signed-off-by: Soichiro Ueda <the.latticeheart@gmail.com>
> > ---
> >  .../selftests/net/af_unix/so_peek_off.c       | 49 +++++++++++++++++++
> >  1 file changed, 49 insertions(+)
> >
> > diff --git a/tools/testing/selftests/net/af_unix/so_peek_off.c b/tools/testing/selftests/net/af_unix/so_peek_off.c
> > index 86e7b0fb522d..813e3b3655d3 100644
> > --- a/tools/testing/selftests/net/af_unix/so_peek_off.c
> > +++ b/tools/testing/selftests/net/af_unix/so_peek_off.c
> > @@ -76,6 +76,19 @@ FIXTURE_TEARDOWN(so_peek_off)
> >               ASSERT_STREQ(str, buf);                         \
> >       } while (0)
> >
> > +#define peekoffeq(fd, expected)                                      \
> > +     do {                                                    \
> > +             int off = -1;                                   \
> > +             socklen_t optlen = sizeof(off);         \
> > +             int ret;                                        \
> > +                                                             \
> > +             ret = getsockopt(fd, SOL_SOCKET, SO_PEEK_OFF,   \
> > +                              &off, &optlen);                \
> > +             ASSERT_EQ(0, ret);                              \
> > +             ASSERT_EQ((socklen_t)sizeof(off), optlen);      \
> > +             ASSERT_EQ(expected, off);                       \
> > +     } while (0)
> > +
> >  #define async                                                        \
> >       for (pid_t pid = (pid = fork(),                         \
> >                         pid < 0 ?                             \
> > @@ -92,6 +105,14 @@ TEST_F(so_peek_off, single_chunk)
> >
> >       recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
> >       recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
> > +
> > +     if (variant->type == SOCK_STREAM) {
> > +             recveq(self->fd[1], "aaaa", 4, 0);
> > +             recveq(self->fd[1], "bbbb", 100, 0);
> > +     } else {
> > +             recveq(self->fd[1], "aaaabbbb", 100, 0);
> > +     }
> > +     peekoffeq(self->fd[1], 0);
>
> Do you want to test peekoffeq before the non-peek read too?
>

That is a great point. I should verify that the offset has correctly
advanced before it gets reset to 0 by the non-peek read. I will add
this check in v2.

> >  }
> >
> >  TEST_F(so_peek_off, two_chunks)
> > @@ -101,6 +122,13 @@ TEST_F(so_peek_off, two_chunks)
> >
> >       recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
> >       recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
> > +
> > +     if (variant->type == SOCK_STREAM)
> > +             recveq(self->fd[1], "aaaa", 4, 0);
> > +     else
> > +             recveq(self->fd[1], "aaaa", 100, 0);
>
> Why this difference in length?
>
> Because stream read will block if > 4, while datagram does not?
>
> If so, can perhaps use 4 for both or explicitly use non-blocking read.
>
> > +     recveq(self->fd[1], "bbbb", 100, 0);
> > +     peekoffeq(self->fd[1], 0);
> >  }

For SOCK_DGRAM, I used 100 simply to ensure the entire first packet
was consumed. But using 4 for both is cleaner and consistent. I will
unify the length to 4 in the next version.

I will send v2 shortly with these changes.

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

end of thread, other threads:[~2026-02-12 22:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-22  3:36 [PATCH v1 net] selftests: af_unix: drain after peek and verify SO_PEEK_OFF reset Soichiro Ueda
2026-01-22 21:33 ` Willem de Bruijn
2026-02-12 22:49   ` Soichiro Ueda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox