[PATCH bpf 11/11] selftests/bpf: Add test for apply_bytes when a redirect sends a part

From: Jiayuan Chen

Date: Sat Nov 21 2026 - 05:37:33 EST


An sk_msg prog parses the header of a message and applies its verdict
to the whole message with bpf_msg_apply_bytes(), so it runs once per
message, at the header:

+-----+-----------+-----+-------+------
| hdr | body | hdr | body | ...
+-----+-----------+-----+-------+------
^ ^
prog: apply(len1) prog: apply(len2)

The test sends one such message and redirects it to the egress of a
socket that has the smallest send buffer, whose peer has the smallest
receive buffer. The redirect sends only a part, so send() returns
early, and the test sends the rest:

+-----+-----------+
| hdr | body |
+-----+-----------+
|<-- send -->|
|<-->| send again

Check that the prog ran once, at the header, and that the peer gets the
whole message.

Signed-off-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
---
.../selftests/bpf/prog_tests/sockmap_basic.c | 126 ++++++++++++++++++
.../bpf/progs/test_sockmap_msg_apply.c | 46 +++++++
2 files changed, 172 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
index 1fef6ec2ba7ae..3979cbab7dc76 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
@@ -14,6 +14,7 @@
#include "test_sockmap_drop_prog.skel.h"
#include "test_sockmap_change_tail.skel.h"
#include "test_sockmap_msg_pop_data.skel.h"
+#include "test_sockmap_msg_apply.skel.h"
#include "bpf_iter_sockmap.skel.h"

#include "sockmap_helpers.h"
@@ -1362,6 +1363,129 @@ static void test_sockmap_no_verdict_fionread(void)
test_sockmap_pass_prog__destroy(skel);
}

+#define APPLY_MSG_MAGIC 0xbeefcafe
+
+/*
+ * One message: a header with its length, then the body. The sk_msg prog
+ * applies the verdict to the whole message and redirects it to the egress
+ * of b. b has the smallest send buffer and its peer the smallest receive
+ * buffer, so the first send() stops in the middle of the message. The rest
+ * must still go to b without another run of the prog.
+ */
+static void test_sockmap_msg_verdict_apply_partial(void)
+{
+ int a = -1, a_peer = -1, b = -1, b_peer = -1, s = -1;
+ struct test_sockmap_msg_apply *skel;
+ int err, map, one = 1, zero = 0;
+ struct sockaddr_storage addr;
+ size_t len, off = 0, got = 0;
+ char *buf = NULL, *rbuf = NULL;
+ int sndbuf, rcvbuf;
+ socklen_t optlen;
+ struct {
+ __u32 magic;
+ __u32 len;
+ } hdr;
+ ssize_t n;
+
+ skel = test_sockmap_msg_apply__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+ map = bpf_map__fd(skel->maps.sock_map);
+
+ err = bpf_prog_attach(bpf_program__fd(skel->progs.prog_msg_apply_redir),
+ map, BPF_SK_MSG_VERDICT, 0);
+ if (!ASSERT_OK(err, "bpf_prog_attach"))
+ goto out;
+
+ err = create_pair(AF_INET, SOCK_STREAM, &a_peer, &a);
+ if (!ASSERT_OK(err, "create_pair"))
+ goto out;
+
+ /* The peer of b gets its receive buffer before the handshake */
+ s = socket_loopback(AF_INET, SOCK_STREAM);
+ if (!ASSERT_GE(s, 0, "socket_loopback"))
+ goto out_close;
+ if (xsetsockopt(s, SOL_SOCKET, SO_RCVBUF, &one, sizeof(one)))
+ goto out_close;
+ b = xsocket(AF_INET, SOCK_STREAM, 0);
+ if (b < 0)
+ goto out_close;
+ if (xsetsockopt(b, SOL_SOCKET, SO_SNDBUF, &one, sizeof(one)))
+ goto out_close;
+ optlen = sizeof(addr);
+ if (xgetsockname(s, sockaddr(&addr), &optlen))
+ goto out_close;
+ if (xconnect(b, sockaddr(&addr), optlen))
+ goto out_close;
+ b_peer = xaccept_nonblock(s, NULL, NULL);
+ if (b_peer < 0)
+ goto out_close;
+
+ optlen = sizeof(sndbuf);
+ if (xgetsockopt(b, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen))
+ goto out_close;
+ optlen = sizeof(rcvbuf);
+ if (xgetsockopt(b_peer, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen))
+ goto out_close;
+ /* Larger than what b and its peer can hold */
+ len = 4 * (sndbuf + rcvbuf);
+
+ err = bpf_map_update_elem(map, &zero, &b, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(b)"))
+ goto out_close;
+ err = bpf_map_update_elem(map, &one, &a, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(a)"))
+ goto out_close;
+
+ buf = malloc(len);
+ rbuf = calloc(len, 1);
+ if (!ASSERT_OK_PTR(buf, "malloc") || !ASSERT_OK_PTR(rbuf, "calloc"))
+ goto out_close;
+ memset(buf, 0xaa, len);
+ hdr.magic = APPLY_MSG_MAGIC;
+ hdr.len = len;
+ memcpy(buf, &hdr, sizeof(hdr));
+
+ while (got < len) {
+ if (off < len) {
+ n = send(a, buf + off, len - off, MSG_DONTWAIT);
+ if (!ASSERT_TRUE(n >= 0 || errno == EAGAIN, "send"))
+ break;
+ if (n > 0) {
+ if (!off && !ASSERT_LT(n, len, "first send partial"))
+ break;
+ off += n;
+ } else {
+ usleep(1000);
+ }
+ }
+ while (got < off) {
+ n = recv_timeout(b_peer, rbuf + got, off - got, 0,
+ IO_TIMEOUT_SEC);
+ if (!ASSERT_GT(n, 0, "recv_timeout"))
+ goto out_close;
+ got += n;
+ }
+ }
+
+ ASSERT_EQ(got, len, "received");
+ ASSERT_OK(memcmp(buf, rbuf, len), "data");
+ ASSERT_EQ(skel->bss->good_hdrs, 1, "prog runs on the header");
+ ASSERT_EQ(skel->bss->bad_hdrs, 0, "prog runs in the body");
+
+out_close:
+ free(buf);
+ free(rbuf);
+ close(s);
+ close(a);
+ close(a_peer);
+ close(b);
+ close(b_peer);
+out:
+ test_sockmap_msg_apply__destroy(skel);
+}
+
void test_sockmap_basic(void)
{
if (test__start_subtest("sockmap create_update_free"))
@@ -1438,4 +1562,6 @@ void test_sockmap_basic(void)
test_sockmap_multi_channels(SOCK_STREAM);
if (test__start_subtest("sockmap udp multi channels"))
test_sockmap_multi_channels(SOCK_DGRAM);
+ if (test__start_subtest("sockmap msg_verdict apply_bytes partial redirect"))
+ test_sockmap_msg_verdict_apply_partial();
}
diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c b/tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c
new file mode 100644
index 0000000000000..6e24117933d03
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_SOCKMAP);
+ __uint(max_entries, 2);
+ __type(key, int);
+ __type(value, int);
+} sock_map SEC(".maps");
+
+#define MSG_MAGIC 0xbeefcafe
+
+struct msg_hdr {
+ __u32 magic;
+ __u32 len;
+};
+
+int good_hdrs;
+int bad_hdrs;
+
+/*
+ * Each message starts with a header. Apply the verdict to the whole
+ * message, so the prog runs again at the next header.
+ */
+SEC("sk_msg")
+int prog_msg_apply_redir(struct sk_msg_md *msg)
+{
+ struct msg_hdr *hdr;
+
+ if (bpf_msg_pull_data(msg, 0, sizeof(*hdr), 0))
+ goto bad;
+
+ hdr = msg->data;
+ if ((void *)(hdr + 1) > msg->data_end || hdr->magic != MSG_MAGIC)
+ goto bad;
+
+ good_hdrs++;
+ bpf_msg_apply_bytes(msg, hdr->len);
+ return bpf_msg_redirect_map(msg, &sock_map, 0, 0);
+bad:
+ bad_hdrs++;
+ return SK_DROP;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.43.0