Skip to content

Commit 8f9081c

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
selftests/bpf: Add a fexit/bpf2bpf test with target bpf prog no callees
The existing fexit_bpf2bpf test covers the target progrm with callees. This patch added a test for the target program without callees. Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e9eeec5 commit 8f9081c

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

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

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@
22
/* Copyright (c) 2019 Facebook */
33
#include <test_progs.h>
44

5-
#define PROG_CNT 3
6-
7-
void test_fexit_bpf2bpf(void)
5+
static void test_fexit_bpf2bpf_common(const char *obj_file,
6+
const char *target_obj_file,
7+
int prog_cnt,
8+
const char **prog_name)
89
{
9-
const char *prog_name[PROG_CNT] = {
10-
"fexit/test_pkt_access",
11-
"fexit/test_pkt_access_subprog1",
12-
"fexit/test_pkt_access_subprog2",
13-
};
1410
struct bpf_object *obj = NULL, *pkt_obj;
1511
int err, pkt_fd, i;
16-
struct bpf_link *link[PROG_CNT] = {};
17-
struct bpf_program *prog[PROG_CNT];
12+
struct bpf_link **link = NULL;
13+
struct bpf_program **prog = NULL;
1814
__u32 duration, retval;
1915
struct bpf_map *data_map;
2016
const int zero = 0;
21-
u64 result[PROG_CNT];
17+
u64 *result = NULL;
2218

23-
err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_UNSPEC,
19+
err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
2420
&pkt_obj, &pkt_fd);
2521
if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
2622
return;
2723
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
2824
.attach_prog_fd = pkt_fd,
2925
);
3026

31-
obj = bpf_object__open_file("./fexit_bpf2bpf.o", &opts);
27+
link = calloc(sizeof(struct bpf_link *), prog_cnt);
28+
prog = calloc(sizeof(struct bpf_program *), prog_cnt);
29+
result = malloc(prog_cnt * sizeof(u64));
30+
if (CHECK(!link || !prog || !result, "alloc_memory",
31+
"failed to alloc memory"))
32+
goto close_prog;
33+
34+
obj = bpf_object__open_file(obj_file, &opts);
3235
if (CHECK(IS_ERR_OR_NULL(obj), "obj_open",
3336
"failed to open fexit_bpf2bpf: %ld\n",
3437
PTR_ERR(obj)))
@@ -38,7 +41,7 @@ void test_fexit_bpf2bpf(void)
3841
if (CHECK(err, "obj_load", "err %d\n", err))
3942
goto close_prog;
4043

41-
for (i = 0; i < PROG_CNT; i++) {
44+
for (i = 0; i < prog_cnt; i++) {
4245
prog[i] = bpf_object__find_program_by_title(obj, prog_name[i]);
4346
if (CHECK(!prog[i], "find_prog", "prog %s not found\n", prog_name[i]))
4447
goto close_prog;
@@ -56,21 +59,54 @@ void test_fexit_bpf2bpf(void)
5659
"err %d errno %d retval %d duration %d\n",
5760
err, errno, retval, duration);
5861

59-
err = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, &result);
62+
err = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, result);
6063
if (CHECK(err, "get_result",
6164
"failed to get output data: %d\n", err))
6265
goto close_prog;
6366

64-
for (i = 0; i < PROG_CNT; i++)
67+
for (i = 0; i < prog_cnt; i++)
6568
if (CHECK(result[i] != 1, "result", "fexit_bpf2bpf failed err %ld\n",
6669
result[i]))
6770
goto close_prog;
6871

6972
close_prog:
70-
for (i = 0; i < PROG_CNT; i++)
73+
for (i = 0; i < prog_cnt; i++)
7174
if (!IS_ERR_OR_NULL(link[i]))
7275
bpf_link__destroy(link[i]);
7376
if (!IS_ERR_OR_NULL(obj))
7477
bpf_object__close(obj);
7578
bpf_object__close(pkt_obj);
79+
free(link);
80+
free(prog);
81+
free(result);
82+
}
83+
84+
static void test_target_no_callees(void)
85+
{
86+
const char *prog_name[] = {
87+
"fexit/test_pkt_md_access",
88+
};
89+
test_fexit_bpf2bpf_common("./fexit_bpf2bpf_simple.o",
90+
"./test_pkt_md_access.o",
91+
ARRAY_SIZE(prog_name),
92+
prog_name);
93+
}
94+
95+
static void test_target_yes_callees(void)
96+
{
97+
const char *prog_name[] = {
98+
"fexit/test_pkt_access",
99+
"fexit/test_pkt_access_subprog1",
100+
"fexit/test_pkt_access_subprog2",
101+
};
102+
test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
103+
"./test_pkt_access.o",
104+
ARRAY_SIZE(prog_name),
105+
prog_name);
106+
}
107+
108+
void test_fexit_bpf2bpf(void)
109+
{
110+
test_target_no_callees();
111+
test_target_yes_callees();
76112
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2019 Facebook */
3+
#include <linux/bpf.h>
4+
#include "bpf_helpers.h"
5+
#include "bpf_trace_helpers.h"
6+
7+
struct sk_buff {
8+
unsigned int len;
9+
};
10+
11+
__u64 test_result = 0;
12+
BPF_TRACE_2("fexit/test_pkt_md_access", test_main2,
13+
struct sk_buff *, skb, int, ret)
14+
{
15+
int len;
16+
17+
__builtin_preserve_access_index(({
18+
len = skb->len;
19+
}));
20+
if (len != 74 || ret != 0)
21+
return 0;
22+
23+
test_result = 1;
24+
return 0;
25+
}
26+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/progs/test_pkt_md_access.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ int _version SEC("version") = 1;
2727
}
2828
#endif
2929

30-
SEC("test1")
31-
int process(struct __sk_buff *skb)
30+
SEC("classifier/test_pkt_md_access")
31+
int test_pkt_md_access(struct __sk_buff *skb)
3232
{
3333
TEST_FIELD(__u8, len, 0xFF);
3434
TEST_FIELD(__u16, len, 0xFFFF);

0 commit comments

Comments
 (0)