1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include "../bpf_testmod/bpf_testmod_kfunc.h"
6
7 SEC("tc")
kfunc_call_test4(struct __sk_buff * skb)8 int kfunc_call_test4(struct __sk_buff *skb)
9 {
10 struct bpf_sock *sk = skb->sk;
11 long tmp;
12
13 if (!sk)
14 return -1;
15
16 sk = bpf_sk_fullsock(sk);
17 if (!sk)
18 return -1;
19
20 tmp = bpf_kfunc_call_test4(-3, -30, -200, -1000);
21 return (tmp >> 32) + tmp;
22 }
23
24 SEC("tc")
kfunc_call_test2(struct __sk_buff * skb)25 int kfunc_call_test2(struct __sk_buff *skb)
26 {
27 struct bpf_sock *sk = skb->sk;
28
29 if (!sk)
30 return -1;
31
32 sk = bpf_sk_fullsock(sk);
33 if (!sk)
34 return -1;
35
36 return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
37 }
38
39 SEC("tc")
kfunc_call_test1(struct __sk_buff * skb)40 int kfunc_call_test1(struct __sk_buff *skb)
41 {
42 struct bpf_sock *sk = skb->sk;
43 __u64 a = 1ULL << 32;
44 __u32 ret;
45
46 if (!sk)
47 return -1;
48
49 sk = bpf_sk_fullsock(sk);
50 if (!sk)
51 return -1;
52
53 a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
54 ret = a >> 32; /* ret should be 2 */
55 ret += (__u32)a; /* ret should be 12 */
56
57 return ret;
58 }
59
60 SEC("tc")
kfunc_call_test_ref_btf_id(struct __sk_buff * skb)61 int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
62 {
63 struct prog_test_ref_kfunc *pt;
64 unsigned long s = 0;
65 int ret = 0;
66
67 pt = bpf_kfunc_call_test_acquire(&s);
68 if (pt) {
69 if (pt->a != 42 || pt->b != 108)
70 ret = -1;
71 bpf_kfunc_call_test_release(pt);
72 }
73 return ret;
74 }
75
76 SEC("tc")
kfunc_call_test_pass(struct __sk_buff * skb)77 int kfunc_call_test_pass(struct __sk_buff *skb)
78 {
79 struct prog_test_pass1 p1 = {};
80 struct prog_test_pass2 p2 = {};
81 short a = 0;
82 __u64 b = 0;
83 long c = 0;
84 char d = 0;
85 int e = 0;
86
87 bpf_kfunc_call_test_pass_ctx(skb);
88 bpf_kfunc_call_test_pass1(&p1);
89 bpf_kfunc_call_test_pass2(&p2);
90
91 bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
92 bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
93 bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
94 bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
95 bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
96 bpf_kfunc_call_test_mem_len_fail2(&b, -1);
97
98 return 0;
99 }
100
101 struct syscall_test_args {
102 __u8 data[16];
103 size_t size;
104 };
105
106 SEC("syscall")
kfunc_syscall_test(struct syscall_test_args * args)107 int kfunc_syscall_test(struct syscall_test_args *args)
108 {
109 const long size = args->size;
110
111 if (size > sizeof(args->data))
112 return -7; /* -E2BIG */
113
114 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(args->data));
115 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args));
116 bpf_kfunc_call_test_mem_len_pass1(&args->data, size);
117
118 return 0;
119 }
120
121 SEC("syscall")
kfunc_syscall_test_null(struct syscall_test_args * args)122 int kfunc_syscall_test_null(struct syscall_test_args *args)
123 {
124 /* Must be called with args as a NULL pointer
125 * we do not check for it to have the verifier consider that
126 * the pointer might not be null, and so we can load it.
127 *
128 * So the following can not be added:
129 *
130 * if (args)
131 * return -22;
132 */
133
134 bpf_kfunc_call_test_mem_len_pass1(args, 0);
135
136 return 0;
137 }
138
139 SEC("tc")
kfunc_call_test_get_mem(struct __sk_buff * skb)140 int kfunc_call_test_get_mem(struct __sk_buff *skb)
141 {
142 struct prog_test_ref_kfunc *pt;
143 unsigned long s = 0;
144 int *p = NULL;
145 int ret = 0;
146
147 pt = bpf_kfunc_call_test_acquire(&s);
148 if (pt) {
149 p = bpf_kfunc_call_test_get_rdwr_mem(pt, 2 * sizeof(int));
150 if (p) {
151 p[0] = 42;
152 ret = p[1]; /* 108 */
153 } else {
154 ret = -1;
155 }
156
157 if (ret >= 0) {
158 p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int));
159 if (p)
160 ret = p[0]; /* 42 */
161 else
162 ret = -1;
163 }
164
165 bpf_kfunc_call_test_release(pt);
166 }
167 return ret;
168 }
169
170 SEC("tc")
kfunc_call_test_static_unused_arg(struct __sk_buff * skb)171 int kfunc_call_test_static_unused_arg(struct __sk_buff *skb)
172 {
173
174 u32 expected = 5, actual;
175
176 actual = bpf_kfunc_call_test_static_unused_arg(expected, 0xdeadbeef);
177 return actual != expected ? -1 : 0;
178 }
179
180 struct ctx_val {
181 struct bpf_testmod_ctx __kptr *ctx;
182 };
183
184 struct {
185 __uint(type, BPF_MAP_TYPE_ARRAY);
186 __uint(max_entries, 1);
187 __type(key, int);
188 __type(value, struct ctx_val);
189 } ctx_map SEC(".maps");
190
191 SEC("tc")
kfunc_call_ctx(struct __sk_buff * skb)192 int kfunc_call_ctx(struct __sk_buff *skb)
193 {
194 struct bpf_testmod_ctx *ctx;
195 int err = 0;
196
197 ctx = bpf_testmod_ctx_create(&err);
198 if (!ctx && !err)
199 err = -1;
200 if (ctx) {
201 int key = 0;
202 struct ctx_val *ctx_val = bpf_map_lookup_elem(&ctx_map, &key);
203
204 /* Transfer ctx to map to be freed via implicit dtor call
205 * on cleanup.
206 */
207 if (ctx_val)
208 ctx = bpf_kptr_xchg(&ctx_val->ctx, ctx);
209 if (ctx) {
210 bpf_testmod_ctx_release(ctx);
211 err = -1;
212 }
213 }
214 return err;
215 }
216
217 char _license[] SEC("license") = "GPL";
218