public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Soichiro Ueda <the.latticeheart@gmail.com>
To: Kuniyuki Iwashima <kuniyu@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>,
	netdev@vger.kernel.org
Cc: Soichiro Ueda <the.latticeheart@gmail.com>,
	Simon Horman <horms@kernel.org>,
	Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	Shuah Khan <shuah@kernel.org>,
	Miao Wang <shankerwangmiao@gmail.com>,
	Willem de Bruijn <willemb@google.com>
Subject: [PATCH net-next v3] selftests: af_unix: validate SO_PEEK_OFF advancement and reset
Date: Tue, 10 Mar 2026 16:28:31 +0900	[thread overview]
Message-ID: <20260310072832.127848-1-the.latticeheart@gmail.com> (raw)

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>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Signed-off-by: Soichiro Ueda <the.latticeheart@gmail.com>
---
v2 -> v3:
- Reorder variables in peekoffeq() to follow reverse xmas tree style
- Fix formatting by adding a missing tab
v1 -> v2:
- Add peekoffeq checks after MSG_PEEK to verify offset advancement
- Unify the length in the non-peek read

v2: https://lore.kernel.org/netdev/20260305070525.184945-1-the.latticeheart@gmail.com/
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..f6466a717f49 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 {							\
+		socklen_t optlen = sizeof(int);			\
+		int off = -1;					\
+		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


             reply	other threads:[~2026-03-10  7:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10  7:28 Soichiro Ueda [this message]
2026-03-12  1:30 ` [PATCH net-next v3] selftests: af_unix: validate SO_PEEK_OFF advancement and reset patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260310072832.127848-1-the.latticeheart@gmail.com \
    --to=the.latticeheart@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=shankerwangmiao@gmail.com \
    --cc=shuah@kernel.org \
    --cc=willemb@google.com \
    --cc=willemdebruijn.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox