All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: bpf@vger.kernel.org
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Jakub Sitnicki <jakub@cloudflare.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Willem de Bruijn <willemb@google.com>,
	Neal Cardwell <ncardwell@google.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Shuah Khan <shuah@kernel.org>, Sechang Lim <rhkrqnwk98@gmail.com>,
	Michal Luczaj <mhal@rbox.co>,
	Mattia Meleleo <mattia.meleleo@coralogix.com>,
	Zijian Zhang <zijianzhang@bytedance.com>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data with strparser
Date: Tue,  1 Dec 2026 20:18:50 +0800	[thread overview]
Message-ID: <20261201122055.269577-3-jiayuan.chen@linux.dev> (raw)
In-Reply-To: <20261201122055.269577-1-jiayuan.chen@linux.dev>

Send two packets and wait for each to reach ingress_msg before sending
the next, so that the parser runs once per packet with the first one
still unread. Read both, then send a third packet and expect it to be
delivered.

Without the fix the third packet is never read: copied_seq has passed
rcv_nxt after the first recv(), so the parser is not woken anymore.

Also drop a packet in the verdict test by redirecting to an empty slot,
and expect the next packet to be delivered.

Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 .../selftests/bpf/prog_tests/sockmap_strp.c   | 109 ++++++++++++++++++
 1 file changed, 109 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c b/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c
index 1d7231728eafd..56832db867107 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c
@@ -364,6 +364,89 @@ static void test_sockmap_strp_pass(int family, int sotype, bool fionread)
 	test_sockmap_strp__destroy(strp);
 }
 
+static int sockmap_strp_wait_fionread(int fd, int expected)
+{
+	int err, avail = 0;
+
+	/* No limit on the loop, test_progs has its own watchdog */
+	while (1) {
+		err = ioctl(fd, FIONREAD, &avail);
+		if (!ASSERT_OK(err, "ioctl(FIONREAD) error"))
+			return -1;
+		if (avail >= expected)
+			break;
+		usleep(10000);
+	}
+	return ASSERT_EQ(avail, expected, "ioctl(FIONREAD)") ? 0 : -1;
+}
+
+/*
+ * Let the parser run twice before the reader reads anything. Each round
+ * must keep the SK_PASS bytes of the earlier rounds out of copied_seq,
+ * otherwise copied_seq runs past rcv_nxt and the stream stalls.
+ */
+static void test_sockmap_strp_pass_unread(int family, int sotype)
+{
+	int zero = 0, one = 1, pkt_size = STRP_PKT_FULL_LEN, sent, recvd;
+	int err, map, i;
+	int c = -1, p = -1;
+	struct test_sockmap_strp *strp = NULL;
+	char rcv[STRP_PKT_FULL_LEN * 2 + 1] = "0";
+
+	strp = sockmap_strp_init(&map, true, true);
+	if (!ASSERT_TRUE(strp, "sockmap_strp_init"))
+		return;
+
+	err = create_pair(family, sotype, &c, &p);
+	if (err)
+		goto out;
+
+	err = bpf_map_update_elem(map, &zero, &p, BPF_NOEXIST);
+	if (!ASSERT_OK(err, "bpf_map_update_elem(p)"))
+		goto out_close;
+
+	err = setsockopt(c, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
+	if (!ASSERT_OK(err, "setsockopt(TCP_NODELAY)"))
+		goto out_close;
+
+	/*
+	 * wait for each packet to reach ingress_msg before sending the
+	 * next one, so that the parser runs once per packet
+	 */
+	for (i = 1; i <= 2; i++) {
+		sent = xsend(c, packet, pkt_size, 0);
+		if (!ASSERT_EQ(sent, pkt_size, "xsend(c)"))
+			goto out_close;
+		if (sockmap_strp_wait_fionread(p, pkt_size * i))
+			goto out_close;
+	}
+
+	recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, IO_TIMEOUT_SEC);
+	if (!ASSERT_EQ(recvd, pkt_size * 2, "recv_timeout(p)") ||
+	    !ASSERT_OK(memcmp(packet, rcv, pkt_size), "memcmp, data mismatch") ||
+	    !ASSERT_OK(memcmp(packet, rcv + pkt_size, pkt_size),
+		       "memcmp, second data mismatch"))
+		goto out_close;
+
+	/* the next packet must still wake the parser */
+	sent = xsend(c, packet, pkt_size, 0);
+	if (!ASSERT_EQ(sent, pkt_size, "xsend(c) after read"))
+		goto out_close;
+
+	recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, IO_TIMEOUT_SEC);
+	if (!ASSERT_EQ(recvd, pkt_size, "recv_timeout(p) after read") ||
+	    !ASSERT_OK(memcmp(packet, rcv, pkt_size),
+		       "memcmp, data mismatch after read"))
+		goto out_close;
+
+out_close:
+	close(c);
+	close(p);
+
+out:
+	test_sockmap_strp__destroy(strp);
+}
+
 /* Test strparser with verdict mode */
 static void test_sockmap_strp_verdict(int family, int sotype)
 {
@@ -422,6 +505,28 @@ static void test_sockmap_strp_verdict(int family, int sotype)
 			  "partial received data does not match the sent data"))
 		goto out_close;
 
+	/* a redirect to an empty slot is SK_DROP, the next one must pass */
+	err = bpf_map_delete_elem(map, &one);
+	if (!ASSERT_OK(err, "bpf_map_delete_elem(p1)"))
+		goto out_close;
+
+	sent = xsend(c0, packet, STRP_PKT_FULL_LEN, 0);
+	if (!ASSERT_EQ(sent, STRP_PKT_FULL_LEN, "dropped xsend(c0)"))
+		goto out_close;
+
+	err = bpf_map_update_elem(map, &one, &p1, BPF_NOEXIST);
+	if (!ASSERT_OK(err, "bpf_map_update_elem(p1) again"))
+		goto out_close;
+
+	sent = xsend(c0, packet, STRP_PKT_FULL_LEN, 0);
+	if (!ASSERT_EQ(sent, STRP_PKT_FULL_LEN, "xsend(c0) after drop"))
+		goto out_close;
+
+	recvd = recv_timeout(c1, rcv, sizeof(rcv), MSG_DONTWAIT,
+			     IO_TIMEOUT_SEC);
+	if (!ASSERT_EQ(recvd, STRP_PKT_FULL_LEN, "recv_timeout(c1) after drop"))
+		goto out_close;
+
 out_close:
 	close(c0);
 	close(c1);
@@ -470,6 +575,10 @@ void test_sockmap_strp(void)
 		test_sockmap_strp_pass(AF_INET, SOCK_STREAM, true);
 	if (test__start_subtest("sockmap strp tcp v6 pass fionread"))
 		test_sockmap_strp_pass(AF_INET6, SOCK_STREAM, true);
+	if (test__start_subtest("sockmap strp tcp pass unread"))
+		test_sockmap_strp_pass_unread(AF_INET, SOCK_STREAM);
+	if (test__start_subtest("sockmap strp tcp v6 pass unread"))
+		test_sockmap_strp_pass_unread(AF_INET6, SOCK_STREAM);
 	if (test__start_subtest("sockmap strp tcp verdict"))
 		test_sockmap_strp_verdict(AF_INET, SOCK_STREAM);
 	if (test__start_subtest("sockmap strp tcp v6 verdict"))
-- 
2.43.0


  parent reply	other threads:[~2026-12-01 12:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-12-01 12:18 ` Jiayuan Chen [this message]
2026-12-01 12:18 ` [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen

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=20261201122055.269577-3-jiayuan.chen@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jakub@cloudflare.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mattia.meleleo@coralogix.com \
    --cc=memxor@gmail.com \
    --cc=mhal@rbox.co \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rhkrqnwk98@gmail.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=willemb@google.com \
    --cc=yonghong.song@linux.dev \
    --cc=zijianzhang@bytedance.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.