1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include "test_bpf_nf.skel.h"
5 
6 enum {
7 	TEST_XDP,
8 	TEST_TC_BPF,
9 };
10 
11 void test_bpf_nf_ct(int mode)
12 {
13 	struct test_bpf_nf *skel;
14 	int prog_fd, err;
15 	LIBBPF_OPTS(bpf_test_run_opts, topts,
16 		.data_in = &pkt_v4,
17 		.data_size_in = sizeof(pkt_v4),
18 		.repeat = 1,
19 	);
20 
21 	skel = test_bpf_nf__open_and_load();
22 	if (!ASSERT_OK_PTR(skel, "test_bpf_nf__open_and_load"))
23 		return;
24 
25 	if (mode == TEST_XDP)
26 		prog_fd = bpf_program__fd(skel->progs.nf_xdp_ct_test);
27 	else
28 		prog_fd = bpf_program__fd(skel->progs.nf_skb_ct_test);
29 
30 	err = bpf_prog_test_run_opts(prog_fd, &topts);
31 	if (!ASSERT_OK(err, "bpf_prog_test_run"))
32 		goto end;
33 
34 	ASSERT_EQ(skel->bss->test_einval_bpf_tuple, -EINVAL, "Test EINVAL for NULL bpf_tuple");
35 	ASSERT_EQ(skel->bss->test_einval_reserved, -EINVAL, "Test EINVAL for reserved not set to 0");
36 	ASSERT_EQ(skel->bss->test_einval_netns_id, -EINVAL, "Test EINVAL for netns_id < -1");
37 	ASSERT_EQ(skel->bss->test_einval_len_opts, -EINVAL, "Test EINVAL for len__opts != NF_BPF_CT_OPTS_SZ");
38 	ASSERT_EQ(skel->bss->test_eproto_l4proto, -EPROTO, "Test EPROTO for l4proto != TCP or UDP");
39 	ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id");
40 	ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup");
41 	ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple");
42 end:
43 	test_bpf_nf__destroy(skel);
44 }
45 
46 void test_bpf_nf(void)
47 {
48 	if (test__start_subtest("xdp-ct"))
49 		test_bpf_nf_ct(TEST_XDP);
50 	if (test__start_subtest("tc-bpf-ct"))
51 		test_bpf_nf_ct(TEST_TC_BPF);
52 }
53