forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds a few kernel function bpf_kfunc_call_test*() for the selftest's test_run purpose. They will be allowed for tc_cls prog. The selftest calling the kernel function bpf_kfunc_call_test*() is also added in this patch. Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Facebook */ | ||
#include <test_progs.h> | ||
#include <network_helpers.h> | ||
#include "kfunc_call_test.skel.h" | ||
#include "kfunc_call_test_subprog.skel.h" | ||
|
||
static void test_main(void) | ||
{ | ||
struct kfunc_call_test *skel; | ||
int prog_fd, retval, err; | ||
|
||
skel = kfunc_call_test__open_and_load(); | ||
if (!ASSERT_OK_PTR(skel, "skel")) | ||
return; | ||
|
||
prog_fd = bpf_program__fd(skel->progs.kfunc_call_test1); | ||
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(test1)"); | ||
ASSERT_EQ(retval, 12, "test1-retval"); | ||
|
||
prog_fd = bpf_program__fd(skel->progs.kfunc_call_test2); | ||
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(test2)"); | ||
ASSERT_EQ(retval, 3, "test2-retval"); | ||
|
||
kfunc_call_test__destroy(skel); | ||
} | ||
|
||
static void test_subprog(void) | ||
{ | ||
struct kfunc_call_test_subprog *skel; | ||
int prog_fd, retval, err; | ||
|
||
skel = kfunc_call_test_subprog__open_and_load(); | ||
if (!ASSERT_OK_PTR(skel, "skel")) | ||
return; | ||
|
||
prog_fd = bpf_program__fd(skel->progs.kfunc_call_test1); | ||
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(test1)"); | ||
ASSERT_EQ(retval, 10, "test1-retval"); | ||
ASSERT_NEQ(skel->data->active_res, -1, "active_res"); | ||
ASSERT_EQ(skel->data->sk_state, BPF_TCP_CLOSE, "sk_state"); | ||
|
||
kfunc_call_test_subprog__destroy(skel); | ||
} | ||
|
||
void test_kfunc_call(void) | ||
{ | ||
if (test__start_subtest("main")) | ||
test_main(); | ||
|
||
if (test__start_subtest("subprog")) | ||
test_subprog(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Facebook */ | ||
#include <linux/bpf.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; | ||
|
||
SEC("classifier") | ||
int kfunc_call_test2(struct __sk_buff *skb) | ||
{ | ||
struct bpf_sock *sk = skb->sk; | ||
|
||
if (!sk) | ||
return -1; | ||
|
||
sk = bpf_sk_fullsock(sk); | ||
if (!sk) | ||
return -1; | ||
|
||
return bpf_kfunc_call_test2((struct sock *)sk, 1, 2); | ||
} | ||
|
||
SEC("classifier") | ||
int kfunc_call_test1(struct __sk_buff *skb) | ||
{ | ||
struct bpf_sock *sk = skb->sk; | ||
__u64 a = 1ULL << 32; | ||
__u32 ret; | ||
|
||
if (!sk) | ||
return -1; | ||
|
||
sk = bpf_sk_fullsock(sk); | ||
if (!sk) | ||
return -1; | ||
|
||
a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4); | ||
ret = a >> 32; /* ret should be 2 */ | ||
ret += (__u32)a; /* ret should be 12 */ | ||
|
||
return ret; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
42 changes: 42 additions & 0 deletions
42
tools/testing/selftests/bpf/progs/kfunc_call_test_subprog.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Facebook */ | ||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include "bpf_tcp_helpers.h" | ||
|
||
extern const int bpf_prog_active __ksym; | ||
extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, | ||
__u32 c, __u64 d) __ksym; | ||
extern struct sock *bpf_kfunc_call_test3(struct sock *sk) __ksym; | ||
int active_res = -1; | ||
int sk_state = -1; | ||
|
||
int __noinline f1(struct __sk_buff *skb) | ||
{ | ||
struct bpf_sock *sk = skb->sk; | ||
int *active; | ||
|
||
if (!sk) | ||
return -1; | ||
|
||
sk = bpf_sk_fullsock(sk); | ||
if (!sk) | ||
return -1; | ||
|
||
active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, | ||
bpf_get_smp_processor_id()); | ||
if (active) | ||
active_res = *active; | ||
|
||
sk_state = bpf_kfunc_call_test3((struct sock *)sk)->__sk_common.skc_state; | ||
|
||
return (__u32)bpf_kfunc_call_test1((struct sock *)sk, 1, 2, 3, 4); | ||
} | ||
|
||
SEC("classifier") | ||
int kfunc_call_test1(struct __sk_buff *skb) | ||
{ | ||
return f1(skb); | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |