selftests/bpf: Extend kfunc selftests
Use the prog_test kfuncs to test the referenced PTR_TO_BTF_ID kfunc support, and PTR_TO_CTX, PTR_TO_MEM argument passing support. Also testing the various failure cases for invalid kfunc prototypes. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/r/20220114163953.1455836-10-memxor@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
committed by
Alexei Starovoitov
parent
0201b80772
commit
c1ff181ffa
@@ -27,6 +27,12 @@ static void test_main(void)
|
||||
ASSERT_OK(err, "bpf_prog_test_run(test2)");
|
||||
ASSERT_EQ(retval, 3, "test2-retval");
|
||||
|
||||
prog_fd = skel->progs.kfunc_call_test_ref_btf_id.prog_fd;
|
||||
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
|
||||
NULL, NULL, (__u32 *)&retval, NULL);
|
||||
ASSERT_OK(err, "bpf_prog_test_run(test_ref_btf_id)");
|
||||
ASSERT_EQ(retval, 0, "test_ref_btf_id-retval");
|
||||
|
||||
kfunc_call_test_lskel__destroy(skel);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/* Copyright (c) 2021 Facebook */
|
||||
#include <linux/bpf.h>
|
||||
#include <vmlinux.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "bpf_tcp_helpers.h"
|
||||
|
||||
extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;
|
||||
extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
|
||||
__u32 c, __u64 d) __ksym;
|
||||
|
||||
extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
|
||||
extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
|
||||
extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;
|
||||
extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
|
||||
extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
|
||||
extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
|
||||
extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
|
||||
|
||||
SEC("tc")
|
||||
int kfunc_call_test2(struct __sk_buff *skb)
|
||||
{
|
||||
@@ -44,4 +51,45 @@ int kfunc_call_test1(struct __sk_buff *skb)
|
||||
return ret;
|
||||
}
|
||||
|
||||
SEC("tc")
|
||||
int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
|
||||
{
|
||||
struct prog_test_ref_kfunc *pt;
|
||||
unsigned long s = 0;
|
||||
int ret = 0;
|
||||
|
||||
pt = bpf_kfunc_call_test_acquire(&s);
|
||||
if (pt) {
|
||||
if (pt->a != 42 || pt->b != 108)
|
||||
ret = -1;
|
||||
bpf_kfunc_call_test_release(pt);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
SEC("tc")
|
||||
int kfunc_call_test_pass(struct __sk_buff *skb)
|
||||
{
|
||||
struct prog_test_pass1 p1 = {};
|
||||
struct prog_test_pass2 p2 = {};
|
||||
short a = 0;
|
||||
__u64 b = 0;
|
||||
long c = 0;
|
||||
char d = 0;
|
||||
int e = 0;
|
||||
|
||||
bpf_kfunc_call_test_pass_ctx(skb);
|
||||
bpf_kfunc_call_test_pass1(&p1);
|
||||
bpf_kfunc_call_test_pass2(&p2);
|
||||
|
||||
bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
|
||||
bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
|
||||
bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
|
||||
bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
|
||||
bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
|
||||
bpf_kfunc_call_test_mem_len_fail2(&b, -1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
|
||||
@@ -21,6 +21,81 @@
|
||||
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
|
||||
.result = ACCEPT,
|
||||
},
|
||||
{
|
||||
"calls: invalid kfunc call: ptr_to_mem to struct with non-scalar",
|
||||
.insns = {
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = REJECT,
|
||||
.errstr = "arg#0 pointer type STRUCT prog_test_fail1 must point to scalar",
|
||||
.fixup_kfunc_btf_id = {
|
||||
{ "bpf_kfunc_call_test_fail1", 2 },
|
||||
},
|
||||
},
|
||||
{
|
||||
"calls: invalid kfunc call: ptr_to_mem to struct with nesting depth > 4",
|
||||
.insns = {
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = REJECT,
|
||||
.errstr = "max struct nesting depth exceeded\narg#0 pointer type STRUCT prog_test_fail2",
|
||||
.fixup_kfunc_btf_id = {
|
||||
{ "bpf_kfunc_call_test_fail2", 2 },
|
||||
},
|
||||
},
|
||||
{
|
||||
"calls: invalid kfunc call: ptr_to_mem to struct with FAM",
|
||||
.insns = {
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = REJECT,
|
||||
.errstr = "arg#0 pointer type STRUCT prog_test_fail3 must point to scalar",
|
||||
.fixup_kfunc_btf_id = {
|
||||
{ "bpf_kfunc_call_test_fail3", 2 },
|
||||
},
|
||||
},
|
||||
{
|
||||
"calls: invalid kfunc call: reg->type != PTR_TO_CTX",
|
||||
.insns = {
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = REJECT,
|
||||
.errstr = "arg#0 expected pointer to ctx, but got PTR",
|
||||
.fixup_kfunc_btf_id = {
|
||||
{ "bpf_kfunc_call_test_pass_ctx", 2 },
|
||||
},
|
||||
},
|
||||
{
|
||||
"calls: invalid kfunc call: void * not allowed in func proto without mem size arg",
|
||||
.insns = {
|
||||
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
|
||||
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),
|
||||
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, 0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
},
|
||||
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
|
||||
.result = REJECT,
|
||||
.errstr = "arg#0 pointer type UNKNOWN must point to scalar",
|
||||
.fixup_kfunc_btf_id = {
|
||||
{ "bpf_kfunc_call_test_mem_len_fail1", 2 },
|
||||
},
|
||||
},
|
||||
{
|
||||
"calls: basic sanity",
|
||||
.insns = {
|
||||
|
||||
Reference in New Issue
Block a user