1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 
6 extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;
7 extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
8 				  __u32 c, __u64 d) __ksym;
9 
10 extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
11 extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
12 extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;
13 extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
14 extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
15 extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
16 extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
17 
18 SEC("tc")
19 int kfunc_call_test2(struct __sk_buff *skb)
20 {
21 	struct bpf_sock *sk = skb->sk;
22 
23 	if (!sk)
24 		return -1;
25 
26 	sk = bpf_sk_fullsock(sk);
27 	if (!sk)
28 		return -1;
29 
30 	return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
31 }
32 
33 SEC("tc")
34 int kfunc_call_test1(struct __sk_buff *skb)
35 {
36 	struct bpf_sock *sk = skb->sk;
37 	__u64 a = 1ULL << 32;
38 	__u32 ret;
39 
40 	if (!sk)
41 		return -1;
42 
43 	sk = bpf_sk_fullsock(sk);
44 	if (!sk)
45 		return -1;
46 
47 	a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
48 	ret = a >> 32;   /* ret should be 2 */
49 	ret += (__u32)a; /* ret should be 12 */
50 
51 	return ret;
52 }
53 
54 SEC("tc")
55 int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
56 {
57 	struct prog_test_ref_kfunc *pt;
58 	unsigned long s = 0;
59 	int ret = 0;
60 
61 	pt = bpf_kfunc_call_test_acquire(&s);
62 	if (pt) {
63 		if (pt->a != 42 || pt->b != 108)
64 			ret = -1;
65 		bpf_kfunc_call_test_release(pt);
66 	}
67 	return ret;
68 }
69 
70 SEC("tc")
71 int kfunc_call_test_pass(struct __sk_buff *skb)
72 {
73 	struct prog_test_pass1 p1 = {};
74 	struct prog_test_pass2 p2 = {};
75 	short a = 0;
76 	__u64 b = 0;
77 	long c = 0;
78 	char d = 0;
79 	int e = 0;
80 
81 	bpf_kfunc_call_test_pass_ctx(skb);
82 	bpf_kfunc_call_test_pass1(&p1);
83 	bpf_kfunc_call_test_pass2(&p2);
84 
85 	bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
86 	bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
87 	bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
88 	bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
89 	bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
90 	bpf_kfunc_call_test_mem_len_fail2(&b, -1);
91 
92 	return 0;
93 }
94 
95 char _license[] SEC("license") = "GPL";
96