Commit 0c586079 authored by Joe Stringer's avatar Joe Stringer Committed by Daniel Borkmann

selftests/bpf: Generalize dummy program types

Don't hardcode the dummy program types to SOCKET_FILTER type, as this
prevents testing bpf_tail_call in conjunction with other program types.
Instead, use the program type specified in the test case.
Signed-off-by: default avatarJoe Stringer <joe@wand.net.nz>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 6acc9b43
...@@ -12652,18 +12652,18 @@ static int create_map(uint32_t type, uint32_t size_key, ...@@ -12652,18 +12652,18 @@ static int create_map(uint32_t type, uint32_t size_key,
return fd; return fd;
} }
static int create_prog_dummy1(void) static int create_prog_dummy1(enum bpf_map_type prog_type)
{ {
struct bpf_insn prog[] = { struct bpf_insn prog[] = {
BPF_MOV64_IMM(BPF_REG_0, 42), BPF_MOV64_IMM(BPF_REG_0, 42),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, return bpf_load_program(prog_type, prog,
ARRAY_SIZE(prog), "GPL", 0, NULL, 0); ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
} }
static int create_prog_dummy2(int mfd, int idx) static int create_prog_dummy2(enum bpf_map_type prog_type, int mfd, int idx)
{ {
struct bpf_insn prog[] = { struct bpf_insn prog[] = {
BPF_MOV64_IMM(BPF_REG_3, idx), BPF_MOV64_IMM(BPF_REG_3, idx),
...@@ -12674,11 +12674,12 @@ static int create_prog_dummy2(int mfd, int idx) ...@@ -12674,11 +12674,12 @@ static int create_prog_dummy2(int mfd, int idx)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, return bpf_load_program(prog_type, prog,
ARRAY_SIZE(prog), "GPL", 0, NULL, 0); ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
} }
static int create_prog_array(uint32_t max_elem, int p1key) static int create_prog_array(enum bpf_map_type prog_type, uint32_t max_elem,
int p1key)
{ {
int p2key = 1; int p2key = 1;
int mfd, p1fd, p2fd; int mfd, p1fd, p2fd;
...@@ -12690,8 +12691,8 @@ static int create_prog_array(uint32_t max_elem, int p1key) ...@@ -12690,8 +12691,8 @@ static int create_prog_array(uint32_t max_elem, int p1key)
return -1; return -1;
} }
p1fd = create_prog_dummy1(); p1fd = create_prog_dummy1(prog_type);
p2fd = create_prog_dummy2(mfd, p2key); p2fd = create_prog_dummy2(prog_type, mfd, p2key);
if (p1fd < 0 || p2fd < 0) if (p1fd < 0 || p2fd < 0)
goto out; goto out;
if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0) if (bpf_map_update_elem(mfd, &p1key, &p1fd, BPF_ANY) < 0)
...@@ -12748,8 +12749,8 @@ static int create_cgroup_storage(bool percpu) ...@@ -12748,8 +12749,8 @@ static int create_cgroup_storage(bool percpu)
static char bpf_vlog[UINT_MAX >> 8]; static char bpf_vlog[UINT_MAX >> 8];
static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog, static void do_test_fixup(struct bpf_test *test, enum bpf_map_type prog_type,
int *map_fds) struct bpf_insn *prog, int *map_fds)
{ {
int *fixup_map1 = test->fixup_map1; int *fixup_map1 = test->fixup_map1;
int *fixup_map2 = test->fixup_map2; int *fixup_map2 = test->fixup_map2;
...@@ -12805,7 +12806,7 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog, ...@@ -12805,7 +12806,7 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
} }
if (*fixup_prog1) { if (*fixup_prog1) {
map_fds[4] = create_prog_array(4, 0); map_fds[4] = create_prog_array(prog_type, 4, 0);
do { do {
prog[*fixup_prog1].imm = map_fds[4]; prog[*fixup_prog1].imm = map_fds[4];
fixup_prog1++; fixup_prog1++;
...@@ -12813,7 +12814,7 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog, ...@@ -12813,7 +12814,7 @@ static void do_test_fixup(struct bpf_test *test, struct bpf_insn *prog,
} }
if (*fixup_prog2) { if (*fixup_prog2) {
map_fds[5] = create_prog_array(8, 7); map_fds[5] = create_prog_array(prog_type, 8, 7);
do { do {
prog[*fixup_prog2].imm = map_fds[5]; prog[*fixup_prog2].imm = map_fds[5];
fixup_prog2++; fixup_prog2++;
...@@ -12859,11 +12860,13 @@ static void do_test_single(struct bpf_test *test, bool unpriv, ...@@ -12859,11 +12860,13 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
for (i = 0; i < MAX_NR_MAPS; i++) for (i = 0; i < MAX_NR_MAPS; i++)
map_fds[i] = -1; map_fds[i] = -1;
do_test_fixup(test, prog, map_fds); if (!prog_type)
prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
do_test_fixup(test, prog_type, prog, map_fds);
prog_len = probe_filter_length(prog); prog_len = probe_filter_length(prog);
fd_prog = bpf_verify_program(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, fd_prog = bpf_verify_program(prog_type, prog, prog_len,
prog, prog_len, test->flags & F_LOAD_WITH_STRICT_ALIGNMENT, test->flags & F_LOAD_WITH_STRICT_ALIGNMENT,
"GPL", 0, bpf_vlog, sizeof(bpf_vlog), 1); "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 1);
expected_ret = unpriv && test->result_unpriv != UNDEF ? expected_ret = unpriv && test->result_unpriv != UNDEF ?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment