Skip to contentSkip to conversation

transition sockmap testing to test_progs

PATCH Superseded John Fastabend 2 participants 8 messages Jan 23, 2024 – Jan 24, 2024

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover transition sockmap testing to test_progs

Its much easier to write and read tests than it was when sockmap was originally created. At that time we created a test_sockmap prog that did sockmap tests. But, its showing its age now. For example it reads user vars out of maps, is hard to run targetted tests, has a different format from the familiar test_progs and so on.

I recently thought there was an issue with pop helpers so I created some tests to try and track it down. It turns out it was a bug in the BPF program we had not the kernel. But, I think it makes sense to start deprecating test_sockmap and converting these to the nicer test_progs.

So this is a first round of test_prog tests for sockmap cork and pop helpers. I'll add push and pull tests shortly. I think its fine, maybe preferred to review smaller patchsets, to send these incrementally as I get them created.

Thanks!

John Fastabend (4):
bpf: Add modern test for sk_msg prog pop msg header
bpf: sockmap, add a sendmsg test so we can check that path
bpf: sockmap, add a cork to force buffering of the scatterlist
bpf: sockmap test cork and pop combined

 .../bpf/prog_tests/sockmap_helpers.h          |  18 +
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 351 ++++++++++++++++++
 .../bpf/progs/test_sockmap_msg_helpers.c      |  67 ++++
 3 files changed, 436 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/4 bpf: sockmap, add test for sk_msg prog pop msg helper

For msg_pop sk_msg helpers we only have older tests in test_sockmap, but these are showing their age. They don't use any of the newer style BPF and also require running test_sockmap. Lets use the prog_test framework and add a test for msg_pop.

This is a much nicer test env using newer style BPF. We can extend this to support all the other helpers shortly.

The bpf program is a template that lets us run through all the helpers so we can cover not just pop, but all the other helpers as well.

---
 .../bpf/prog_tests/sockmap_helpers.h          |  10 +
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 210 ++++++++++++++++++
 .../bpf/progs/test_sockmap_msg_helpers.c      |  52 +++++
 3 files changed, 272 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c
tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h +10 -0
index e880f97bc44d..781cbdf01d7b 100644
@@ -112,6 +112,16 @@
 		__ret;                                                         \
 	})
 
+#define xrecv_nonblock(fd, buf, len, flags)                                    \
+	({                                                                     \
+		ssize_t __ret = recv_timeout((fd), (buf), (len), (flags),      \
+					     IO_TIMEOUT_SEC);                  \
+		if (__ret == -1)                                               \
+			FAIL_ERRNO("recv");                                    \
+		__ret;                                                         \
+	})
+
+
 #define xsocket(family, sotype, flags)                                         \
 	({                                                                     \
 		int __ret = socket(family, sotype, flags);                     \
tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c new file +210 -0

210 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/4 bpf: sockmap, add a sendmsg test so we can check that path

In reply to John Fastabend

Sendmsg path with multiple buffers is slightly different from a single send in how we have to handle and walk the sg when doing pops. Lets ensure this walk is correct.

---
 .../bpf/prog_tests/sockmap_helpers.h          |  8 +++
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 53 +++++++++++++++++++
 2 files changed, 61 insertions(+)
tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h +8 -0
index 781cbdf01d7b..4d8d24482032 100644
@@ -103,6 +103,14 @@
 		__ret;                                                         \
 	})
 
+#define xsendmsg(fd, msg, flags)                                               \
+	({                                                                     \
+		ssize_t __ret = sendmsg((fd), (msg), (flags));                 \
+		if (__ret == -1)                                               \
+			FAIL_ERRNO("sendmsg");                                 \
+		__ret;                                                         \
+	})
+
 #define xrecv_nonblock(fd, buf, len, flags)                                    \
 	({                                                                     \
 		ssize_t __ret = recv_timeout((fd), (buf), (len), (flags),      \
tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c +53 -0

65 lines not on this page · raw message

Signature · 1 line
2.33.0
#3 lore raw

PATCH 3/4 bpf: sockmap, add a cork to force buffering of the scatterlist

In reply to John Fastabend

By using cork we can force multiple sends into a single scatterlist and test that first the cork gives us the correct number of bytes, but then also test the pop over the corked data.

---
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 81 +++++++++++++++++++
 .../bpf/progs/test_sockmap_msg_helpers.c      |  3 +
 2 files changed, 84 insertions(+)
tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c +81 -0
index 7eeba3a35242..a05000b07891 100644
@@ -21,6 +21,85 @@ struct msg_test_opts {
 
 #define POP_END -1
 
+static void cork_send(struct msg_test_opts *opts, int cork)
+{
+	struct test_sockmap_msg_helpers *skel = opts->skel;
+	char buf[] = "abcdefghijklmnopqrstuvwxyz";
+	size_t sent, total = 0, recv;
+	char *recvbuf;
+	int i;
+
+	skel->bss->pop = false;
+	skel->bss->cork = cork;
+
+	/* Send N bytes in 27B chunks */
+	for (i = 0; i < cork / sizeof(buf); i++) {
+		sent = xsend(opts->client, buf, sizeof(buf), 0);
+		if (sent < sizeof(buf))
+			FAIL("xsend failed");
+		total += sent;
+	}
+
+	recvbuf = malloc(total);
+	if (!recvbuf)
+		FAIL("cork send malloc failure\n");
+
+	ASSERT_OK(skel->bss->err, "cork error");
+	ASSERT_EQ(skel->bss->size, cork, "cork did not receive all bytes");
+
+	recv = xrecv_nonblock(opts->server, recvbuf, total, 0);
+	if (recv != total)
+		FAIL("Received incorrect number of bytes");
+
+	free(recvbuf);
+}
+
+static void test_sockmap_cork()
+{
+	struct test_sockmap_msg_helpers *skel;
+	struct msg_test_opts opts;
+	int s, client, server;
+	int err, map, prog;
+
+	skel = test_sockmap_msg_helpers__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+
+	map = bpf_map__fd(skel->maps.sock_map);
+	prog = bpf_program__fd(skel->progs.msg_helpers);
+	err = bpf_prog_attach(prog, map, BPF_SK_MSG_VERDICT, 0);
+	if (!ASSERT_OK(err, "bpf_prog_attach"))
+		goto out;
+
+	s = socket_loopback(AF_INET, SOCK_STREAM);
+	if (s < 0)
+		goto close_sockets;
+
+	err = create_pair(s, AF_INET, SOCK_STREAM, &client, &server);
+	if (err < 0)
+		goto close_loopback;
+
+	err = add_to_sockmap(map, client, server);
+	if (err < 0)
+		FAIL("add to sockmap");
+
+	opts.client = client;
+	opts.server = server;
+	opts.skel = skel;
+
+	/* Small cork */
+	cork_send(&opts, 54);
+	/* Full cork */
+	cork_send(&opts, 270);
+close_sockets:
+	close(client);
+	close(server);
+close_loopback:
+	close(s);
+out:
+	test_sockmap_msg_helpers__destroy(skel);
+}
+
 static void pop_simple_send(struct msg_test_opts *opts, int start, int len)
 {
 	struct test_sockmap_msg_helpers *skel = opts->skel;
@@ -260,4 +339,6 @@ void test_sockmap_msg_helpers(void)
 		test_sockmap_pop();
 	if (test__start_subtest("sockmap pop errors"))
 		test_sockmap_pop_errors();
+	if (test__start_subtest("sockmap cork"))
+		test_sockmap_cork();
 }
tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c +3 -0
index c721a00b6001..9622f154d016 100644
@@ -30,10 +30,13 @@ int pop_start = 0;
 int pop_len = 0;
 
 int err;
+int size;
 
 SEC("sk_msg")
 int msg_helpers(struct sk_msg_md *msg)
 {
+	size = msg->size;
+
 	if (cork)
 		err = bpf_msg_cork_bytes(msg, cork);
 
Signature · 1 line
2.33.0
#4 lore raw

PATCH 4/4 bpf: sockmap test cork and pop combined

In reply to John Fastabend

Its possible to cork data for some N bytes and then pop a some bytes off that scatterlist. Test combining cork and pop here.

---
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 19 ++++++++++++++-----
 .../bpf/progs/test_sockmap_msg_helpers.c      | 14 +++++++++++++-
 2 files changed, 27 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c +14 -5
index a05000b07891..cf38d6bb3f94 100644
@@ -21,7 +21,7 @@ struct msg_test_opts {
 
 #define POP_END -1
 
-static void cork_send(struct msg_test_opts *opts, int cork)
+static void cork_send(struct msg_test_opts *opts, int cork, int start, int len)
 {
 	struct test_sockmap_msg_helpers *skel = opts->skel;
 	char buf[] = "abcdefghijklmnopqrstuvwxyz";
@@ -29,9 +29,12 @@ static void cork_send(struct msg_test_opts *opts, int cork)
 	char *recvbuf;
 	int i;
 
-	skel->bss->pop = false;
+	skel->bss->pop = !!len;
 	skel->bss->cork = cork;
 
+	skel->bss->pop_start = start;
+	skel->bss->pop_len = len;
+
 	/* Send N bytes in 27B chunks */
 	for (i = 0; i < cork / sizeof(buf); i++) {
 		sent = xsend(opts->client, buf, sizeof(buf), 0);
@@ -48,7 +51,7 @@ static void cork_send(struct msg_test_opts *opts, int cork)
 	ASSERT_EQ(skel->bss->size, cork, "cork did not receive all bytes");
 
 	recv = xrecv_nonblock(opts->server, recvbuf, total, 0);
-	if (recv != total)
+	if (recv != total - len)
 		FAIL("Received incorrect number of bytes");
 
 	free(recvbuf);
@@ -88,9 +91,15 @@ static void test_sockmap_cork()
 	opts.skel = skel;
 
 	/* Small cork */
-	cork_send(&opts, 54);
+	cork_send(&opts, 54, 0, 0);
 	/* Full cork */
-	cork_send(&opts, 270);
+	cork_send(&opts, 270, 0, 0);
+
+	/* Combine cork and pop small */
+	cork_send(&opts, 54, 0, 10);
+	/* Full cork and pop */
+	cork_send(&opts, 270, 200, 50);
+
 close_sockets:
 	close(client);
 	close(server);
tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c +13 -1
index 9622f154d016..4c7e70367e35 100644
@@ -37,8 +37,19 @@ int msg_helpers(struct sk_msg_md *msg)
 {
 	size = msg->size;
 
-	if (cork)
+	/* If message is not yet fully cork'ed skip push, pull, pop */
+	if (cork && cork > msg->size) {
 		err = bpf_msg_cork_bytes(msg, cork);
+		goto out;
+	} else if (cork) {
+	/* If we previously corked the msg we need to clear the cork
+	 * otherwise next pop would cause datapath to wait for the
+	 * popped bytes to actually do the send.
+	 */
+		err = bpf_msg_cork_bytes(msg, 0);
+		if (err)
+			goto out;
+	}
 
 	if (pull)
 		err = bpf_msg_pull_data(msg, pull_start, pull_end, 0);
@@ -49,6 +60,7 @@ int msg_helpers(struct sk_msg_md *msg)
 	if (pop)
 		err = bpf_msg_pop_data(msg, pop_start, pop_len, 0);
 
+out:
 	return SK_PASS;
 }
 
Signature · 1 line
2.33.0
#5 lore raw
In reply to John Fastabend
Quoted 25 lines · John Fastabend

Its much easier to write and read tests than it was when sockmap was originally created. At that time we created a test_sockmap prog that did sockmap tests. But, its showing its age now. For example it reads user vars out of maps, is hard to run targetted tests, has a different format from the familiar test_progs and so on.

I recently thought there was an issue with pop helpers so I created some tests to try and track it down. It turns out it was a bug in the BPF program we had not the kernel. But, I think it makes sense to start deprecating test_sockmap and converting these to the nicer test_progs.

So this is a first round of test_prog tests for sockmap cork and pop helpers. I'll add push and pull tests shortly. I think its fine, maybe preferred to review smaller patchsets, to send these incrementally as I get them created.

Thanks!

John Fastabend (4):
bpf: Add modern test for sk_msg prog pop msg header
bpf: sockmap, add a sendmsg test so we can check that path
bpf: sockmap, add a cork to force buffering of the scatterlist
bpf: sockmap test cork and pop combined

Quoted 10 lines
 .../bpf/prog_tests/sockmap_helpers.h          |  18 +
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 351 ++++++++++++++++++
 .../bpf/progs/test_sockmap_msg_helpers.c      |  67 ++++
 3 files changed, 436 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c
Signature · 1 line
2.33.0

Will need a v2 to fixup a couple things here. Thanks.

#6 lore raw
Quoted 34 lines · John Fastabend
Quoted 32 lines · John Fastabend

Its much easier to write and read tests than it was when sockmap was originally created. At that time we created a test_sockmap prog that did sockmap tests. But, its showing its age now. For example it reads user vars out of maps, is hard to run targetted tests, has a different format from the familiar test_progs and so on.

I recently thought there was an issue with pop helpers so I created some tests to try and track it down. It turns out it was a bug in the BPF program we had not the kernel. But, I think it makes sense to start deprecating test_sockmap and converting these to the nicer test_progs.

So this is a first round of test_prog tests for sockmap cork and pop helpers. I'll add push and pull tests shortly. I think its fine, maybe preferred to review smaller patchsets, to send these incrementally as I get them created.

Thanks!

John Fastabend (4):
bpf: Add modern test for sk_msg prog pop msg header
bpf: sockmap, add a sendmsg test so we can check that path
bpf: sockmap, add a cork to force buffering of the scatterlist
bpf: sockmap test cork and pop combined

 .../bpf/prog_tests/sockmap_helpers.h          |  18 +
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 351 ++++++++++++++++++
 .../bpf/progs/test_sockmap_msg_helpers.c      |  67 ++++
 3 files changed, 436 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c
Quoted 6 lines
Quoted 3 lines

--
2.33.0

Will need a v2 to fixup a couple things here. Thanks.

Can you please also try compiling selftests with make RELEASE=1 and making sure the compiler doesn't complain about uninitialized variables and such. Unfortunately we don't do this automatically in CI yet.

#7 lore raw
Quoted 39 lines · Andrii Nakryiko
Quoted 38 lines · John Fastabend
Quoted 35 lines · John Fastabend

Its much easier to write and read tests than it was when sockmap was originally created. At that time we created a test_sockmap prog that did sockmap tests. But, its showing its age now. For example it reads user vars out of maps, is hard to run targetted tests, has a different format from the familiar test_progs and so on.

I recently thought there was an issue with pop helpers so I created some tests to try and track it down. It turns out it was a bug in the BPF program we had not the kernel. But, I think it makes sense to start deprecating test_sockmap and converting these to the nicer test_progs.

So this is a first round of test_prog tests for sockmap cork and pop helpers. I'll add push and pull tests shortly. I think its fine, maybe preferred to review smaller patchsets, to send these incrementally as I get them created.

Thanks!

John Fastabend (4):
bpf: Add modern test for sk_msg prog pop msg header
bpf: sockmap, add a sendmsg test so we can check that path
bpf: sockmap, add a cork to force buffering of the scatterlist
bpf: sockmap test cork and pop combined

 .../bpf/prog_tests/sockmap_helpers.h          |  18 +
 .../bpf/prog_tests/sockmap_msg_helpers.c      | 351 ++++++++++++++++++
 .../bpf/progs/test_sockmap_msg_helpers.c      |  67 ++++
 3 files changed, 436 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c

--
2.33.0

Quoted 7 lines
Quoted 2 lines

Will need a v2 to fixup a couple things here. Thanks.

Can you please also try compiling selftests with make RELEASE=1 and making sure the compiler doesn't complain about uninitialized variables and such. Unfortunately we don't do this automatically in CI yet.

Yep thats what I realized I missed after submitting. Thanks.