Skip to content

Commit

Permalink
selftests/bpf: add strparser test for bpf
Browse files Browse the repository at this point in the history
Add test cases for bpf + strparser and separated them from
sockmap_basic, as strparser has more encapsulation and parsing
capabilities compared to standard sockmap.

Signed-off-by: Jiayuan Chen <[email protected]>
Acked-by: Jakub Sitnicki <[email protected]>
  • Loading branch information
mrpre authored and Kernel Patches Daemon committed Jan 29, 2025
1 parent efcf6dc commit 2c729f1
Show file tree
Hide file tree
Showing 3 changed files with 507 additions and 53 deletions.
53 changes: 0 additions & 53 deletions tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,57 +531,6 @@ static void test_sockmap_skb_verdict_shutdown(void)
test_sockmap_pass_prog__destroy(skel);
}

static void test_sockmap_stream_pass(void)
{
int zero = 0, sent, recvd;
int verdict, parser;
int err, map;
int c = -1, p = -1;
struct test_sockmap_pass_prog *pass = NULL;
char snd[256] = "0123456789";
char rcv[256] = "0";

pass = test_sockmap_pass_prog__open_and_load();
verdict = bpf_program__fd(pass->progs.prog_skb_verdict);
parser = bpf_program__fd(pass->progs.prog_skb_parser);
map = bpf_map__fd(pass->maps.sock_map_rx);

err = bpf_prog_attach(parser, map, BPF_SK_SKB_STREAM_PARSER, 0);
if (!ASSERT_OK(err, "bpf_prog_attach stream parser"))
goto out;

err = bpf_prog_attach(verdict, map, BPF_SK_SKB_STREAM_VERDICT, 0);
if (!ASSERT_OK(err, "bpf_prog_attach stream verdict"))
goto out;

err = create_pair(AF_INET, SOCK_STREAM, &c, &p);
if (err)
goto out;

/* sk_data_ready of 'p' will be replaced by strparser handler */
err = bpf_map_update_elem(map, &zero, &p, BPF_NOEXIST);
if (!ASSERT_OK(err, "bpf_map_update_elem(p)"))
goto out_close;

/*
* as 'prog_skb_parser' return the original skb len and
* 'prog_skb_verdict' return SK_PASS, the kernel will just
* pass it through to original socket 'p'
*/
sent = xsend(c, snd, sizeof(snd), 0);
ASSERT_EQ(sent, sizeof(snd), "xsend(c)");

recvd = recv_timeout(p, rcv, sizeof(rcv), SOCK_NONBLOCK,
IO_TIMEOUT_SEC);
ASSERT_EQ(recvd, sizeof(rcv), "recv_timeout(p)");

out_close:
close(c);
close(p);

out:
test_sockmap_pass_prog__destroy(pass);
}

static void test_sockmap_skb_verdict_fionread(bool pass_prog)
{
Expand Down Expand Up @@ -1101,8 +1050,6 @@ void test_sockmap_basic(void)
test_sockmap_progs_query(BPF_SK_SKB_VERDICT);
if (test__start_subtest("sockmap skb_verdict shutdown"))
test_sockmap_skb_verdict_shutdown();
if (test__start_subtest("sockmap stream parser and verdict pass"))
test_sockmap_stream_pass();
if (test__start_subtest("sockmap skb_verdict fionread"))
test_sockmap_skb_verdict_fionread(true);
if (test__start_subtest("sockmap skb_verdict fionread on drop"))
Expand Down
Loading

0 comments on commit 2c729f1

Please sign in to comment.