Skip to content

Commit

Permalink
selftests/bpf: add skmsg verdict tests
Browse files Browse the repository at this point in the history
Add two normal skmsg verdict tests in sockmap_basic.c

Signed-off-by: Liu Jian <liujian56@huawei.com>
  • Loading branch information
liujian56 authored and d-e-s-o committed Nov 9, 2023
1 parent 19ed5a3 commit e05f012
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -524,6 +525,72 @@ static void test_sockmap_skb_verdict_peek(void)
test_sockmap_pass_prog__destroy(pass);
}

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"))
Expand Down Expand Up @@ -566,4 +633,8 @@ void test_sockmap_basic(void)
test_sockmap_skb_verdict_fionread(false);
if (test__start_subtest("sockmap skb_verdict msg_f_peek"))
test_sockmap_skb_verdict_peek();
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);
}
25 changes: 25 additions & 0 deletions tools/testing/selftests/bpf/progs/test_sockmap_msg_verdict.c
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit e05f012

Please sign in to comment.