1 // SPDX-License-Identifier: GPL-2.0
3 #include <bpf/bpf_tracing.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_core_read.h>
6 #include <bpf/bpf_endian.h>
8 #include "bpf_experimental.h"
11 #define ETH_P_IP 0x0800
15 __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
16 __uint(max_entries, 4);
17 __uint(key_size, sizeof(__u32));
18 __uint(value_size, sizeof(__u32));
19 } jmp_table SEC(".maps");
21 static __noinline int static_func(u64 i)
27 __noinline int global2static_simple(u64 i)
33 __noinline int global2static(u64 i)
37 return static_func(i);
40 static __noinline int static2global(u64 i)
42 return global2static(i) + i;
46 int exception_throw_always_1(struct __sk_buff *ctx)
52 /* In this case, the global func will never be seen executing after call to
53 * static subprog, hence verifier will DCE the remaining instructions. Ensure we
54 * are resilient to that.
57 int exception_throw_always_2(struct __sk_buff *ctx)
59 return global2static_simple(ctx->protocol);
63 int exception_throw_unwind_1(struct __sk_buff *ctx)
65 return static2global(bpf_ntohs(ctx->protocol));
69 int exception_throw_unwind_2(struct __sk_buff *ctx)
71 return static2global(bpf_ntohs(ctx->protocol) - 1);
75 int exception_throw_default(struct __sk_buff *ctx)
82 int exception_throw_default_value(struct __sk_buff *ctx)
89 int exception_tail_call_target(struct __sk_buff *ctx)
96 int exception_tail_call_subprog(struct __sk_buff *ctx)
98 volatile int ret = 10;
100 bpf_tail_call_static(ctx, &jmp_table, 0);
105 int exception_tail_call(struct __sk_buff *ctx) {
106 volatile int ret = 0;
108 ret = exception_tail_call_subprog(ctx);
112 __noinline int exception_ext_global(struct __sk_buff *ctx)
114 volatile int ret = 0;
119 static __noinline int exception_ext_static(struct __sk_buff *ctx)
121 return exception_ext_global(ctx);
125 int exception_ext(struct __sk_buff *ctx)
127 return exception_ext_static(ctx);
130 __noinline int exception_cb_mod_global(u64 cookie)
132 volatile int ret = 0;
137 /* Example of how the exception callback supplied during verification can still
138 * introduce extensions by calling to dummy global functions, and alter runtime
141 * Right now we don't allow freplace attachment to exception callback itself,
142 * but if the need arises this restriction is technically feasible to relax in
145 __noinline int exception_cb_mod(u64 cookie)
147 return exception_cb_mod_global(cookie) + cookie + 10;
151 __exception_cb(exception_cb_mod)
152 int exception_ext_mod_cb_runtime(struct __sk_buff *ctx)
158 __noinline static int subprog(struct __sk_buff *ctx)
160 return bpf_ktime_get_ns();
163 __noinline static int throwing_subprog(struct __sk_buff *ctx)
167 return bpf_ktime_get_ns();
170 __noinline int global_subprog(struct __sk_buff *ctx)
172 return bpf_ktime_get_ns();
175 __noinline int throwing_global_subprog(struct __sk_buff *ctx)
179 return bpf_ktime_get_ns();
183 int exception_throw_subprog(struct __sk_buff *ctx)
185 switch (ctx->protocol) {
189 return global_subprog(ctx);
191 return throwing_subprog(ctx);
193 return throwing_global_subprog(ctx);
201 __noinline int assert_nz_gfunc(u64 c)
203 volatile u64 cookie = c;
205 bpf_assert(cookie != 0);
209 __noinline int assert_zero_gfunc(u64 c)
211 volatile u64 cookie = c;
213 bpf_assert(bpf_cmp_unlikely(cookie, ==, 0));
217 __noinline int assert_neg_gfunc(s64 c)
219 volatile s64 cookie = c;
221 bpf_assert(bpf_cmp_unlikely(cookie, <, 0));
225 __noinline int assert_pos_gfunc(s64 c)
227 volatile s64 cookie = c;
229 bpf_assert(bpf_cmp_unlikely(cookie, >, 0));
233 __noinline int assert_negeq_gfunc(s64 c)
235 volatile s64 cookie = c;
237 bpf_assert(bpf_cmp_unlikely(cookie, <=, -1));
241 __noinline int assert_poseq_gfunc(s64 c)
243 volatile s64 cookie = c;
245 bpf_assert(bpf_cmp_unlikely(cookie, >=, 1));
249 __noinline int assert_nz_gfunc_with(u64 c)
251 volatile u64 cookie = c;
253 bpf_assert_with(cookie != 0, cookie + 100);
257 __noinline int assert_zero_gfunc_with(u64 c)
259 volatile u64 cookie = c;
261 bpf_assert_with(bpf_cmp_unlikely(cookie, ==, 0), cookie + 100);
265 __noinline int assert_neg_gfunc_with(s64 c)
267 volatile s64 cookie = c;
269 bpf_assert_with(bpf_cmp_unlikely(cookie, <, 0), cookie + 100);
273 __noinline int assert_pos_gfunc_with(s64 c)
275 volatile s64 cookie = c;
277 bpf_assert_with(bpf_cmp_unlikely(cookie, >, 0), cookie + 100);
281 __noinline int assert_negeq_gfunc_with(s64 c)
283 volatile s64 cookie = c;
285 bpf_assert_with(bpf_cmp_unlikely(cookie, <=, -1), cookie + 100);
289 __noinline int assert_poseq_gfunc_with(s64 c)
291 volatile s64 cookie = c;
293 bpf_assert_with(bpf_cmp_unlikely(cookie, >=, 1), cookie + 100);
297 #define check_assert(name, cookie, tag) \
299 int exception##tag##name(struct __sk_buff *ctx) \
301 return name(cookie) + 1; \
304 check_assert(assert_nz_gfunc, 5, _);
305 check_assert(assert_zero_gfunc, 0, _);
306 check_assert(assert_neg_gfunc, -100, _);
307 check_assert(assert_pos_gfunc, 100, _);
308 check_assert(assert_negeq_gfunc, -1, _);
309 check_assert(assert_poseq_gfunc, 1, _);
311 check_assert(assert_nz_gfunc_with, 5, _);
312 check_assert(assert_zero_gfunc_with, 0, _);
313 check_assert(assert_neg_gfunc_with, -100, _);
314 check_assert(assert_pos_gfunc_with, 100, _);
315 check_assert(assert_negeq_gfunc_with, -1, _);
316 check_assert(assert_poseq_gfunc_with, 1, _);
318 check_assert(assert_nz_gfunc, 0, _bad_);
319 check_assert(assert_zero_gfunc, 5, _bad_);
320 check_assert(assert_neg_gfunc, 100, _bad_);
321 check_assert(assert_pos_gfunc, -100, _bad_);
322 check_assert(assert_negeq_gfunc, 1, _bad_);
323 check_assert(assert_poseq_gfunc, -1, _bad_);
325 check_assert(assert_nz_gfunc_with, 0, _bad_);
326 check_assert(assert_zero_gfunc_with, 5, _bad_);
327 check_assert(assert_neg_gfunc_with, 100, _bad_);
328 check_assert(assert_pos_gfunc_with, -100, _bad_);
329 check_assert(assert_negeq_gfunc_with, 1, _bad_);
330 check_assert(assert_poseq_gfunc_with, -1, _bad_);
333 int exception_assert_range(struct __sk_buff *ctx)
335 u64 time = bpf_ktime_get_ns();
337 bpf_assert_range(time, 0, ~0ULL);
342 int exception_assert_range_with(struct __sk_buff *ctx)
344 u64 time = bpf_ktime_get_ns();
346 bpf_assert_range_with(time, 0, ~0ULL, 10);
351 int exception_bad_assert_range(struct __sk_buff *ctx)
353 u64 time = bpf_ktime_get_ns();
355 bpf_assert_range(time, -100, 100);
360 int exception_bad_assert_range_with(struct __sk_buff *ctx)
362 u64 time = bpf_ktime_get_ns();
364 bpf_assert_range_with(time, -1000, 1000, 10);
368 char _license[] SEC("license") = "GPL";