[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230927093013.1951659-5-liujian56@huawei.com>
Date: Wed, 27 Sep 2023 17:30:10 +0800
From: Liu Jian <liujian56@...wei.com>
To: <john.fastabend@...il.com>, <jakub@...udflare.com>, <ast@...nel.org>,
<daniel@...earbox.net>, <andrii@...nel.org>, <martin.lau@...ux.dev>,
<song@...nel.org>, <yonghong.song@...ux.dev>, <kpsingh@...nel.org>,
<sdf@...gle.com>, <haoluo@...gle.com>, <jolsa@...nel.org>,
<davem@...emloft.net>, <edumazet@...gle.com>, <kuba@...nel.org>,
<pabeni@...hat.com>, <dsahern@...nel.org>
CC: <netdev@...r.kernel.org>, <bpf@...r.kernel.org>, <liujian56@...wei.com>
Subject: [PATCH bpf-next v5 4/7] selftests/bpf: add skmsg verdict tests
Add two normal skmsg verdict tests in sockmap_basic.c
Signed-off-by: Liu Jian <liujian56@...wei.com>
---
.../selftests/bpf/prog_tests/sockmap_basic.c | 71 +++++++++++++++++++
.../bpf/progs/test_sockmap_msg_verdict.c | 25 +++++++
2 files changed, 96 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_verdict.c
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
index 064cc5e8d9ad..93bf1907abd9 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
@@ -12,6 +12,7 @@
#include "test_sockmap_progs_query.skel.h"
#include "test_sockmap_pass_prog.skel.h"
#include "test_sockmap_drop_prog.skel.h"
+#include "test_sockmap_msg_verdict.skel.h"
#include "bpf_iter_sockmap.skel.h"
#include "sockmap_helpers.h"
@@ -475,6 +476,72 @@ static void test_sockmap_skb_verdict_fionread(bool pass_prog)
test_sockmap_drop_prog__destroy(drop);
}
+static void test_sockmap_msg_verdict(bool is_ingress)
+{
+ int key, sent, recvd, recv_fd;
+ int err, map, verdict, s, c0, c1, p0, p1;
+ struct test_sockmap_msg_verdict *skel;
+ char buf[256] = "0123456789";
+
+ skel = test_sockmap_msg_verdict__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+ verdict = bpf_program__fd(skel->progs.prog_skmsg_verdict);
+ map = bpf_map__fd(skel->maps.sock_map);
+
+
+ err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0);
+ if (!ASSERT_OK(err, "bpf_prog_attach"))
+ goto out;
+
+ s = socket_loopback(AF_INET, SOCK_STREAM);
+ if (!ASSERT_GT(s, -1, "socket_loopback(s)"))
+ goto out;
+ err = create_socket_pairs(s, AF_INET, SOCK_STREAM, &c0, &c1, &p0, &p1);
+ if (!ASSERT_OK(err, "create_socket_pairs(s)"))
+ goto out;
+
+ key = 0;
+ err = bpf_map_update_elem(map, &key, &p1, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(key0)"))
+ goto out_close;
+ key = 1;
+ err = bpf_map_update_elem(map, &key, &c1, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(key1)"))
+ goto out_close;
+ key = 2;
+ err = bpf_map_update_elem(map, &key, &p0, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(key2)"))
+ goto out_close;
+ key = 3;
+ err = bpf_map_update_elem(map, &key, &c0, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(key3)"))
+ goto out_close;
+
+ if (is_ingress) {
+ recv_fd = c1;
+ skel->bss->skmsg_redir_flags = BPF_F_INGRESS;
+ skel->bss->skmsg_redir_key = 1;
+ } else {
+ recv_fd = c0;
+ skel->bss->skmsg_redir_flags = 0;
+ skel->bss->skmsg_redir_key = 2;
+ }
+
+ sent = xsend(p1, &buf, sizeof(buf), 0);
+ ASSERT_EQ(sent, sizeof(buf), "xsend(p1)");
+ recvd = recv_timeout(recv_fd, &buf, sizeof(buf), SOCK_NONBLOCK, IO_TIMEOUT_SEC);
+ ASSERT_EQ(recvd, sizeof(buf), "recv_timeout(recv_fd)");
+
+out_close:
+ close(c0);
+ close(p0);
+ close(c1);
+ close(p1);
+out:
+ test_sockmap_msg_verdict__destroy(skel);
+}
+
void test_sockmap_basic(void)
{
if (test__start_subtest("sockmap create_update_free"))
@@ -515,4 +582,8 @@ void test_sockmap_basic(void)
test_sockmap_skb_verdict_fionread(true);
if (test__start_subtest("sockmap skb_verdict fionread on drop"))
test_sockmap_skb_verdict_fionread(false);
+ if (test__start_subtest("sockmap msg_verdict"))
+ test_sockmap_msg_verdict(false);
+ if (test__start_subtest("sockmap msg_verdict ingress"))
+ test_sockmap_msg_verdict(true);
}
diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_msg_verdict.c b/tools/testing/selftests/bpf/progs/test_sockmap_msg_verdict.c
new file mode 100644
index 000000000000..002b76a1ae35
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_sockmap_msg_verdict.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_SOCKMAP);
+ __uint(max_entries, 4);
+ __type(key, int);
+ __type(value, int);
+} sock_map SEC(".maps");
+
+u64 skmsg_redir_flags = 0;
+u32 skmsg_redir_key = 0;
+
+SEC("sk_msg")
+int prog_skmsg_verdict(struct sk_msg_md *msg)
+{
+ u64 flags = skmsg_redir_flags;
+ int key = skmsg_redir_key;
+
+ bpf_msg_redirect_map(msg, &sock_map, key, flags);
+ return SK_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.34.1
Powered by blists - more mailing lists