projects/linux·blob master · refs

166 lines · 3.3 KiB

1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3
4#include <vmlinux.h>
5#include <bpf/bpf_helpers.h>
6#include "bpf_kfuncs.h"
7#include "../test_kmods/bpf_testmod_kfunc.h"
8
9#if (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)) && \
10 defined(__BPF_FEATURE_STACK_ARGUMENT)
11
12const volatile bool has_stack_arg = true;
13
14struct bpf_iter_testmod_seq {
15 u64 :64;
16 u64 :64;
17};
18
19extern int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt) __ksym;
20extern void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it) __ksym;
21
22struct timer_map_value {
23 struct bpf_timer timer;
24};
25
26struct {
27 __uint(type, BPF_MAP_TYPE_ARRAY);
28 __uint(max_entries, 1);
29 __type(key, int);
30 __type(value, struct timer_map_value);
31} kfunc_timer_map SEC(".maps");
32
33SEC("tc")
34int test_stack_arg_scalar(struct __sk_buff *skb)
35{
36 return bpf_kfunc_call_stack_arg(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
37}
38
39SEC("tc")
40int test_stack_arg_ptr(struct __sk_buff *skb)
41{
42 struct prog_test_pass1 p = { .x0 = 10, .x1 = 20 };
43
44 return bpf_kfunc_call_stack_arg_ptr(1, 2, 3, 4, 5, 6, 7, 8, 9, &p);
45}
46
47SEC("tc")
48int test_stack_arg_mix(struct __sk_buff *skb)
49{
50 struct prog_test_pass1 p = { .x0 = 10 };
51 struct prog_test_pass1 q = { .x1 = 20 };
52
53 return bpf_kfunc_call_stack_arg_mix(1, 2, 3, 4, 5, 6, 7, &p, 8, &q);
54}
55
56/* 1+2+3+4+5+6+7+8+9+sizeof(pkt_v4) = 45+54 = 99 */
57SEC("tc")
58int test_stack_arg_dynptr(struct __sk_buff *skb)
59{
60 struct bpf_dynptr ptr;
61
62 bpf_dynptr_from_skb(skb, 0, &ptr);
63 return bpf_kfunc_call_stack_arg_dynptr(1, 2, 3, 4, 5, 6, 7, 8, 9, &ptr);
64}
65
66/* 1 + 2 + 3 + 4 + 5 + (1 + 2 + ... + 16) = 15 + 136 = 151 */
67SEC("tc")
68int test_stack_arg_mem(struct __sk_buff *skb)
69{
70 char buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
71
72 return bpf_kfunc_call_stack_arg_mem(1, 2, 3, 4, 5, buf, sizeof(buf));
73}
74
75/* 1+2+3+4+5+6+7+8+9+100 = 145 */
76SEC("tc")
77int test_stack_arg_iter(struct __sk_buff *skb)
78{
79 struct bpf_iter_testmod_seq it;
80 u64 ret;
81
82 bpf_iter_testmod_seq_new(&it, 100, 10);
83 ret = bpf_kfunc_call_stack_arg_iter(1, 2, 3, 4, 5, 6, 7, 8, 9, &it);
84 bpf_iter_testmod_seq_destroy(&it);
85 return ret;
86}
87
88const char cstr[] = "hello";
89
90/* 1+2+3+4+5+6+7+8+9 = 45 */
91SEC("tc")
92int test_stack_arg_const_str(struct __sk_buff *skb)
93{
94 return bpf_kfunc_call_stack_arg_const_str(1, 2, 3, 4, 5, 6, 7, 8, 9,
95 cstr);
96}
97
98/* 1+2+3+4+5+6+7+8+9 = 45 */
99SEC("tc")
100int test_stack_arg_timer(struct __sk_buff *skb)
101{
102 struct timer_map_value *val;
103 int key = 0;
104
105 val = bpf_map_lookup_elem(&kfunc_timer_map, &key);
106 if (!val)
107 return 0;
108 return bpf_kfunc_call_stack_arg_timer(1, 2, 3, 4, 5, 6, 7, 8, 9,
109 &val->timer);
110}
111
112#else
113
114const volatile bool has_stack_arg = false;
115
116SEC("tc")
117int test_stack_arg_scalar(struct __sk_buff *skb)
118{
119 return 0;
120}
121
122SEC("tc")
123int test_stack_arg_ptr(struct __sk_buff *skb)
124{
125 return 0;
126}
127
128SEC("tc")
129int test_stack_arg_mix(struct __sk_buff *skb)
130{
131 return 0;
132}
133
134SEC("tc")
135int test_stack_arg_dynptr(struct __sk_buff *skb)
136{
137 return 0;
138}
139
140SEC("tc")
141int test_stack_arg_mem(struct __sk_buff *skb)
142{
143 return 0;
144}
145
146SEC("tc")
147int test_stack_arg_iter(struct __sk_buff *skb)
148{
149 return 0;
150}
151
152SEC("tc")
153int test_stack_arg_const_str(struct __sk_buff *skb)
154{
155 return 0;
156}
157
158SEC("tc")
159int test_stack_arg_timer(struct __sk_buff *skb)
160{
161 return 0;
162}
163
164#endif
165
166char _license[] SEC("license") = "GPL";