* [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset
@ 2026-03-05 7:05 Soichiro Ueda
2026-03-05 14:30 ` Willem de Bruijn
2026-03-06 5:26 ` Kuniyuki Iwashima
0 siblings, 2 replies; 4+ messages in thread
From: Soichiro Ueda @ 2026-03-05 7:05 UTC (permalink / raw)
To: Kuniyuki Iwashima, David S. Miller, Eric Dumazet, Jakub Kicinski,
netdev
Cc: Soichiro Ueda, Simon Horman, Willem de Bruijn, Miao Wang
Extend the so_peek_off selftest to ensure the socket peek offset is handled
correctly after both MSG_PEEK and actual data consumption.
Verify that the peek offset advances by the same amount as the number of
bytes read when performing a read with 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.
The verification after actual data consumption was suggested by Miao Wang
when the original so_peek_off selftest was introduced.
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>
---
v1 -> v2:
- Add peekoffeq checks after MSG_PEEK to verify offset advancement
- Unify the length in the non-peek read
v1: https://lore.kernel.org/netdev/20260122033605.148626-1-the.latticeheart@gmail.com/
.../selftests/net/af_unix/so_peek_off.c | 46 +++++++++++++++++++
1 file changed, 46 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..9640a3975aee 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 ? \
@@ -91,7 +104,12 @@ TEST_F(so_peek_off, single_chunk)
sendeq(self->fd[0], "aaaabbbb", 0);
recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
+ peekoffeq(self->fd[1], 4);
recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 8);
+
+ recveq(self->fd[1], "aaaabbbb", 8, 0);
+ peekoffeq(self->fd[1], 0);
}
TEST_F(so_peek_off, two_chunks)
@@ -100,7 +118,13 @@ TEST_F(so_peek_off, two_chunks)
sendeq(self->fd[0], "bbbb", 0);
recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
+ peekoffeq(self->fd[1], 4);
recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 8);
+
+ recveq(self->fd[1], "aaaa", 4, 0);
+ recveq(self->fd[1], "bbbb", 4, 0);
+ peekoffeq(self->fd[1], 0);
}
TEST_F(so_peek_off, two_chunks_blocking)
@@ -111,6 +135,7 @@ TEST_F(so_peek_off, two_chunks_blocking)
}
recveq(self->fd[1], "aaaa", 4, MSG_PEEK);
+ peekoffeq(self->fd[1], 4);
async {
usleep(1000);
@@ -119,24 +144,38 @@ 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);
+ peekoffeq(self->fd[1], 8);
+
+ recveq(self->fd[1], "aaaa", 4, 0);
+ recveq(self->fd[1], "bbbb", 4, 0);
+ peekoffeq(self->fd[1], 0);
}
TEST_F(so_peek_off, two_chunks_overlap)
{
sendeq(self->fd[0], "aaaa", 0);
recveq(self->fd[1], "aa", 2, MSG_PEEK);
+ peekoffeq(self->fd[1], 2);
sendeq(self->fd[0], "bbbb", 0);
if (variant->type == SOCK_STREAM) {
/* SOCK_STREAM tries to fill the buffer. */
recveq(self->fd[1], "aabb", 4, MSG_PEEK);
+ peekoffeq(self->fd[1], 6);
recveq(self->fd[1], "bb", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 8);
} else {
/* SOCK_DGRAM and SOCK_SEQPACKET returns at the skb boundary. */
recveq(self->fd[1], "aa", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 4);
recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 8);
}
+
+ recveq(self->fd[1], "aaaa", 4, 0);
+ recveq(self->fd[1], "bbbb", 4, 0);
+ peekoffeq(self->fd[1], 0);
}
TEST_F(so_peek_off, two_chunks_overlap_blocking)
@@ -147,6 +186,7 @@ TEST_F(so_peek_off, two_chunks_overlap_blocking)
}
recveq(self->fd[1], "aa", 2, MSG_PEEK);
+ peekoffeq(self->fd[1], 2);
async {
usleep(1000);
@@ -155,8 +195,14 @@ TEST_F(so_peek_off, two_chunks_overlap_blocking)
/* Even SOCK_STREAM does not wait if at least one byte is read. */
recveq(self->fd[1], "aa", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 4);
recveq(self->fd[1], "bbbb", 100, MSG_PEEK);
+ peekoffeq(self->fd[1], 8);
+
+ recveq(self->fd[1], "aaaa", 4, 0);
+ recveq(self->fd[1], "bbbb", 4, 0);
+ peekoffeq(self->fd[1], 0);
}
TEST_HARNESS_MAIN
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset
2026-03-05 7:05 [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset Soichiro Ueda
@ 2026-03-05 14:30 ` Willem de Bruijn
2026-03-06 5:26 ` Kuniyuki Iwashima
1 sibling, 0 replies; 4+ messages in thread
From: Willem de Bruijn @ 2026-03-05 14:30 UTC (permalink / raw)
To: Soichiro Ueda, Kuniyuki Iwashima, David S. Miller, Eric Dumazet,
Jakub Kicinski, netdev
Cc: Soichiro Ueda, Simon Horman, Willem de Bruijn, Miao Wang
Soichiro Ueda wrote:
> Extend the so_peek_off selftest to ensure the socket peek offset is handled
> correctly after both MSG_PEEK and actual data consumption.
>
> Verify that the peek offset advances by the same amount as the number of
> bytes read when performing a read with 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.
>
> The verification after actual data consumption was suggested by Miao Wang
> when the original so_peek_off selftest was introduced.
>
> 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>
Reviewed-by: Willem de Bruijn <willemb@google.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset
2026-03-05 7:05 [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset Soichiro Ueda
2026-03-05 14:30 ` Willem de Bruijn
@ 2026-03-06 5:26 ` Kuniyuki Iwashima
2026-03-06 17:01 ` Soichiro Ueda
1 sibling, 1 reply; 4+ messages in thread
From: Kuniyuki Iwashima @ 2026-03-06 5:26 UTC (permalink / raw)
To: Soichiro Ueda
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, netdev,
Simon Horman, Willem de Bruijn, Miao Wang
On Wed, Mar 4, 2026 at 11:05 PM Soichiro Ueda
<the.latticeheart@gmail.com> wrote:
>
> Extend the so_peek_off selftest to ensure the socket peek offset is handled
> correctly after both MSG_PEEK and actual data consumption.
>
> Verify that the peek offset advances by the same amount as the number of
> bytes read when performing a read with 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.
>
> The verification after actual data consumption was suggested by Miao Wang
> when the original so_peek_off selftest was introduced.
>
> 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>
> ---
> v1 -> v2:
> - Add peekoffeq checks after MSG_PEEK to verify offset advancement
> - Unify the length in the non-peek read
>
> v1: https://lore.kernel.org/netdev/20260122033605.148626-1-the.latticeheart@gmail.com/
>
> .../selftests/net/af_unix/so_peek_off.c | 46 +++++++++++++++++++
> 1 file changed, 46 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..9640a3975aee 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); \
nit: Can you insert one more tab before \ here ?
While at it, please sort variables in reverse xmas tree order.
socklen_t optlen = sizeof(off);
int ret, off = -1;
Otherwise looks good, thanks !
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset
2026-03-06 5:26 ` Kuniyuki Iwashima
@ 2026-03-06 17:01 ` Soichiro Ueda
0 siblings, 0 replies; 4+ messages in thread
From: Soichiro Ueda @ 2026-03-06 17:01 UTC (permalink / raw)
To: Kuniyuki Iwashima
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, netdev,
Simon Horman, Willem de Bruijn, Miao Wang
> On Wed, Mar 4, 2026 at 11:05 PM Soichiro Ueda
> <the.latticeheart@gmail.com> wrote:
> >
> > Extend the so_peek_off selftest to ensure the socket peek offset is handled
> > correctly after both MSG_PEEK and actual data consumption.
> >
> > Verify that the peek offset advances by the same amount as the number of
> > bytes read when performing a read with 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.
> >
> > The verification after actual data consumption was suggested by Miao Wang
> > when the original so_peek_off selftest was introduced.
> >
> > 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>
> > ---
> > v1 -> v2:
> > - Add peekoffeq checks after MSG_PEEK to verify offset advancement
> > - Unify the length in the non-peek read
> >
> > v1: https://lore.kernel.org/netdev/20260122033605.148626-1-the.latticeheart@gmail.com/
> >
> > .../selftests/net/af_unix/so_peek_off.c | 46 +++++++++++++++++++
> > 1 file changed, 46 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..9640a3975aee 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); \
>
> nit: Can you insert one more tab before \ here ?
>
> While at it, please sort variables in reverse xmas tree order.
>
> socklen_t optlen = sizeof(off);
> int ret, off = -1;
Thank you for the review, Willem and Kuniyuki.
I'll fix these formatting issues and variable ordering in v3.
I'll send it shortly.
Thanks!
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-03-06 17:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-05 7:05 [PATCH net-next v2] selftests: af_unix: validate SO_PEEK_OFF advancement and reset Soichiro Ueda
2026-03-05 14:30 ` Willem de Bruijn
2026-03-06 5:26 ` Kuniyuki Iwashima
2026-03-06 17:01 ` Soichiro Ueda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox