Skip to content

Commit 0e8e60e

Browse files
borkmannAlexei Starovoitov
authored andcommitted
selftests/bpf: Add test case for different expected_attach_type
Add a small test case which adds two programs - one calling the other through a tailcall - and check that BPF rejects them in case of different expected_attach_type values: # ./vmtest.sh -- ./test_progs -t xdp_devmap [...] #641/1 xdp_devmap_attach/DEVMAP with programs in entries:OK #641/2 xdp_devmap_attach/DEVMAP with frags programs in entries:OK #641/3 xdp_devmap_attach/Verifier check of DEVMAP programs:OK #641/4 xdp_devmap_attach/DEVMAP with programs in entries on veth:OK #641 xdp_devmap_attach:OK Summary: 2/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 4540aed commit 0e8e60e

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

tools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <test_progs.h>
88

99
#include "test_xdp_devmap_helpers.skel.h"
10+
#include "test_xdp_devmap_tailcall.skel.h"
1011
#include "test_xdp_with_devmap_frags_helpers.skel.h"
1112
#include "test_xdp_with_devmap_helpers.skel.h"
1213

@@ -107,6 +108,29 @@ static void test_neg_xdp_devmap_helpers(void)
107108
}
108109
}
109110

111+
static void test_xdp_devmap_tailcall(enum bpf_attach_type prog_dev,
112+
enum bpf_attach_type prog_tail,
113+
bool expect_reject)
114+
{
115+
struct test_xdp_devmap_tailcall *skel;
116+
int err;
117+
118+
skel = test_xdp_devmap_tailcall__open();
119+
if (!ASSERT_OK_PTR(skel, "test_xdp_devmap_tailcall__open"))
120+
return;
121+
122+
bpf_program__set_expected_attach_type(skel->progs.xdp_devmap, prog_dev);
123+
bpf_program__set_expected_attach_type(skel->progs.xdp_entry, prog_tail);
124+
125+
err = test_xdp_devmap_tailcall__load(skel);
126+
if (expect_reject)
127+
ASSERT_ERR(err, "test_xdp_devmap_tailcall__load");
128+
else
129+
ASSERT_OK(err, "test_xdp_devmap_tailcall__load");
130+
131+
test_xdp_devmap_tailcall__destroy(skel);
132+
}
133+
110134
static void test_xdp_with_devmap_frags_helpers(void)
111135
{
112136
struct test_xdp_with_devmap_frags_helpers *skel;
@@ -238,8 +262,13 @@ void serial_test_xdp_devmap_attach(void)
238262
if (test__start_subtest("DEVMAP with frags programs in entries"))
239263
test_xdp_with_devmap_frags_helpers();
240264

241-
if (test__start_subtest("Verifier check of DEVMAP programs"))
265+
if (test__start_subtest("Verifier check of DEVMAP programs")) {
242266
test_neg_xdp_devmap_helpers();
267+
test_xdp_devmap_tailcall(BPF_XDP_DEVMAP, BPF_XDP_DEVMAP, false);
268+
test_xdp_devmap_tailcall(0, 0, true);
269+
test_xdp_devmap_tailcall(BPF_XDP_DEVMAP, 0, true);
270+
test_xdp_devmap_tailcall(0, BPF_XDP_DEVMAP, true);
271+
}
243272

244273
if (test__start_subtest("DEVMAP with programs in entries on veth"))
245274
test_xdp_with_devmap_helpers_veth();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
SEC("xdp")
8+
int xdp_devmap(struct xdp_md *ctx)
9+
{
10+
return ctx->egress_ifindex;
11+
}
12+
13+
struct {
14+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
15+
__uint(max_entries, 1);
16+
__uint(key_size, sizeof(__u32));
17+
__array(values, int (void *));
18+
} xdp_map SEC(".maps") = {
19+
.values = {
20+
[0] = (void *)&xdp_devmap,
21+
},
22+
};
23+
24+
SEC("xdp")
25+
int xdp_entry(struct xdp_md *ctx)
26+
{
27+
bpf_tail_call(ctx, &xdp_map, 0);
28+
return 0;
29+
}

0 commit comments

Comments
 (0)