[PATCH bpf-next v1 3/3] selftests/bpf: Add edge case tests for sockmap

Jiayuan Chen posted 3 patches 9 months, 3 weeks ago
There is a newer version of this series
[PATCH bpf-next v1 3/3] selftests/bpf: Add edge case tests for sockmap
Posted by Jiayuan Chen 9 months, 3 weeks ago
Add edge case tests for sockmap.

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

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
index 1e3e4392dcca..e36b5c78fc98 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
@@ -1042,6 +1042,61 @@ static void test_sockmap_vsock_unconnected(void)
 	xclose(map);
 }
 
+void *close_thread(void *arg)
+{
+	int fd = *(int *)arg;
+
+	sleep(1);
+	close(fd);
+	return NULL;
+}
+
+void test_sockmap_with_close_on_write(int family, int sotype)
+{
+	struct test_sockmap_pass_prog *skel;
+	int err, map, verdict, sent;
+	pthread_t tid;
+	int zero = 0;
+	int c = -1, p = -1;
+
+	skel = test_sockmap_pass_prog__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+
+	verdict = bpf_program__fd(skel->progs.prog_skb_verdict);
+	map = bpf_map__fd(skel->maps.sock_map_rx);
+
+	err = bpf_prog_attach(verdict, map, BPF_SK_SKB_STREAM_VERDICT, 0);
+	if (!ASSERT_OK(err, "bpf_prog_attach"))
+		goto out;
+
+	err = create_pair(family, sotype, &c, &p);
+	if (!ASSERT_OK(err, "create_pair"))
+		goto out;
+
+	err = bpf_map_update_elem(map, &zero, &p, BPF_ANY);
+	if (!ASSERT_OK(err, "bpf_map_update_elem"))
+		goto out;
+
+	err = pthread_create(&tid, 0, close_thread, &p);
+	if (!ASSERT_OK(err, "pthread_create"))
+		goto out;
+
+	sent = xsend(c, "a", 1, 0);
+	if (!ASSERT_EQ(sent, 1, "xsend"))
+		goto out;
+
+	pthread_join(tid, NULL);
+	/* p was closed in thread */
+	p = -1;
+out:
+	if (c > 0)
+		close(c);
+	if (p > 0)
+		close(p);
+	test_sockmap_pass_prog__destroy(skel);
+}
+
 void test_sockmap_basic(void)
 {
 	if (test__start_subtest("sockmap create_update_free"))
@@ -1108,4 +1163,6 @@ void test_sockmap_basic(void)
 		test_sockmap_skb_verdict_vsock_poll();
 	if (test__start_subtest("sockmap vsock unconnected"))
 		test_sockmap_vsock_unconnected();
+	if (test__start_subtest("sockmap with unix and close"))
+		test_sockmap_with_close_on_write(AF_UNIX, SOCK_STREAM);
 }
-- 
2.47.1
Re: [PATCH bpf-next v1 3/3] selftests/bpf: Add edge case tests for sockmap
Posted by John Fastabend 9 months, 3 weeks ago
On 2025-02-26 21:22:42, Jiayuan Chen wrote:
> Add edge case tests for sockmap.
> 
> Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
> ---

Reviewed-by: John Fastabend <john.fastabend@gmail.com>

>  .../selftests/bpf/prog_tests/sockmap_basic.c  | 57> +
>  void test_sockmap_basic(void)
>  {
>  	if (test__start_subtest("sockmap create_update_free"))
> @@ -1108,4 +1163,6 @@ void test_sockmap_basic(void)
>  		test_sockmap_skb_verdict_vsock_poll();
>  	if (test__start_subtest("sockmap vsock unconnected"))
>  		test_sockmap_vsock_unconnected();
> +	if (test__start_subtest("sockmap with unix and close"))
> +		test_sockmap_with_close_on_write(AF_UNIX, SOCK_STREAM);

Might also be worth running these with other types? No bug was
fixed there but still testing close while writing is useful
for testing RCU paths in TCP/UDP.

Fine as a follow up patch IMO.
Re: [PATCH bpf-next v1 3/3] selftests/bpf: Add edge case tests for sockmap
Posted by Cong Wang 9 months, 3 weeks ago
On Wed, Feb 26, 2025 at 09:22:42PM +0800, Jiayuan Chen wrote:
> Add edge case tests for sockmap.
> 
> Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>

Acked-by: Cong Wang <xiyou.wangcong@gmail.com>

I always love to see fixes with test cases.

Thanks!