Loading...
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
7SEC("tc")
8int 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
24SEC("tc")
25int 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
39SEC("tc")
40int 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
60SEC("tc")
61int 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
76SEC("tc")
77int 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
101struct syscall_test_args {
102 __u8 data[16];
103 size_t size;
104};
105
106SEC("syscall")
107int 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
121SEC("syscall")
122int 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
139SEC("tc")
140int 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
170SEC("tc")
171int 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
180struct ctx_val {
181 struct bpf_testmod_ctx __kptr *ctx;
182};
183
184struct {
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
191SEC("tc")
192int 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
217char _license[] SEC("license") = "GPL";
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2021 Facebook */
3#include <vmlinux.h>
4#include <bpf/bpf_helpers.h>
5
6extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;
7extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
8 __u32 c, __u64 d) __ksym;
9
10extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
11extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
12extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;
13extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
14extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
15extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
16extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
17extern int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p, const int rdwr_buf_size) __ksym;
18extern int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) __ksym;
19
20SEC("tc")
21int kfunc_call_test2(struct __sk_buff *skb)
22{
23 struct bpf_sock *sk = skb->sk;
24
25 if (!sk)
26 return -1;
27
28 sk = bpf_sk_fullsock(sk);
29 if (!sk)
30 return -1;
31
32 return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
33}
34
35SEC("tc")
36int kfunc_call_test1(struct __sk_buff *skb)
37{
38 struct bpf_sock *sk = skb->sk;
39 __u64 a = 1ULL << 32;
40 __u32 ret;
41
42 if (!sk)
43 return -1;
44
45 sk = bpf_sk_fullsock(sk);
46 if (!sk)
47 return -1;
48
49 a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
50 ret = a >> 32; /* ret should be 2 */
51 ret += (__u32)a; /* ret should be 12 */
52
53 return ret;
54}
55
56SEC("tc")
57int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
58{
59 struct prog_test_ref_kfunc *pt;
60 unsigned long s = 0;
61 int ret = 0;
62
63 pt = bpf_kfunc_call_test_acquire(&s);
64 if (pt) {
65 if (pt->a != 42 || pt->b != 108)
66 ret = -1;
67 bpf_kfunc_call_test_release(pt);
68 }
69 return ret;
70}
71
72SEC("tc")
73int kfunc_call_test_pass(struct __sk_buff *skb)
74{
75 struct prog_test_pass1 p1 = {};
76 struct prog_test_pass2 p2 = {};
77 short a = 0;
78 __u64 b = 0;
79 long c = 0;
80 char d = 0;
81 int e = 0;
82
83 bpf_kfunc_call_test_pass_ctx(skb);
84 bpf_kfunc_call_test_pass1(&p1);
85 bpf_kfunc_call_test_pass2(&p2);
86
87 bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
88 bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
89 bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
90 bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
91 bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
92 bpf_kfunc_call_test_mem_len_fail2(&b, -1);
93
94 return 0;
95}
96
97struct syscall_test_args {
98 __u8 data[16];
99 size_t size;
100};
101
102SEC("syscall")
103int kfunc_syscall_test(struct syscall_test_args *args)
104{
105 const long size = args->size;
106
107 if (size > sizeof(args->data))
108 return -7; /* -E2BIG */
109
110 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(args->data));
111 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args));
112 bpf_kfunc_call_test_mem_len_pass1(&args->data, size);
113
114 return 0;
115}
116
117SEC("syscall")
118int kfunc_syscall_test_null(struct syscall_test_args *args)
119{
120 /* Must be called with args as a NULL pointer
121 * we do not check for it to have the verifier consider that
122 * the pointer might not be null, and so we can load it.
123 *
124 * So the following can not be added:
125 *
126 * if (args)
127 * return -22;
128 */
129
130 bpf_kfunc_call_test_mem_len_pass1(args, 0);
131
132 return 0;
133}
134
135SEC("tc")
136int kfunc_call_test_get_mem(struct __sk_buff *skb)
137{
138 struct prog_test_ref_kfunc *pt;
139 unsigned long s = 0;
140 int *p = NULL;
141 int ret = 0;
142
143 pt = bpf_kfunc_call_test_acquire(&s);
144 if (pt) {
145 p = bpf_kfunc_call_test_get_rdwr_mem(pt, 2 * sizeof(int));
146 if (p) {
147 p[0] = 42;
148 ret = p[1]; /* 108 */
149 } else {
150 ret = -1;
151 }
152
153 if (ret >= 0) {
154 p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int));
155 if (p)
156 ret = p[0]; /* 42 */
157 else
158 ret = -1;
159 }
160
161 bpf_kfunc_call_test_release(pt);
162 }
163 return ret;
164}
165
166char _license[] SEC("license") = "GPL";