]>
Commit | Line | Data |
---|---|---|
25763b3c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1cf1cae9 | 2 | /* Copyright (c) 2017 Facebook |
1cf1cae9 AS |
3 | */ |
4 | #include <linux/bpf.h> | |
c48e51c8 | 5 | #include <linux/btf.h> |
7bd1590d | 6 | #include <linux/btf_ids.h> |
1cf1cae9 | 7 | #include <linux/slab.h> |
b202d844 | 8 | #include <linux/init.h> |
1cf1cae9 AS |
9 | #include <linux/vmalloc.h> |
10 | #include <linux/etherdevice.h> | |
11 | #include <linux/filter.h> | |
87b7b533 | 12 | #include <linux/rcupdate_trace.h> |
1cf1cae9 | 13 | #include <linux/sched/signal.h> |
6ac99e8f | 14 | #include <net/bpf_sk_storage.h> |
2cb494a3 SL |
15 | #include <net/sock.h> |
16 | #include <net/tcp.h> | |
7c32e8f8 | 17 | #include <net/net_namespace.h> |
b530e9e1 | 18 | #include <net/page_pool.h> |
3d08b6f2 | 19 | #include <linux/error-injection.h> |
1b4d60ec | 20 | #include <linux/smp.h> |
7c32e8f8 | 21 | #include <linux/sock_diag.h> |
47316f4a | 22 | #include <net/xdp.h> |
1cf1cae9 | 23 | |
e950e843 MM |
24 | #define CREATE_TRACE_POINTS |
25 | #include <trace/events/bpf_test_run.h> | |
26 | ||
607b9cc9 LB |
27 | struct bpf_test_timer { |
28 | enum { NO_PREEMPT, NO_MIGRATE } mode; | |
29 | u32 i; | |
30 | u64 time_start, time_spent; | |
31 | }; | |
32 | ||
33 | static void bpf_test_timer_enter(struct bpf_test_timer *t) | |
34 | __acquires(rcu) | |
35 | { | |
36 | rcu_read_lock(); | |
37 | if (t->mode == NO_PREEMPT) | |
38 | preempt_disable(); | |
39 | else | |
40 | migrate_disable(); | |
41 | ||
42 | t->time_start = ktime_get_ns(); | |
43 | } | |
44 | ||
45 | static void bpf_test_timer_leave(struct bpf_test_timer *t) | |
46 | __releases(rcu) | |
47 | { | |
48 | t->time_start = 0; | |
49 | ||
50 | if (t->mode == NO_PREEMPT) | |
51 | preempt_enable(); | |
52 | else | |
53 | migrate_enable(); | |
54 | rcu_read_unlock(); | |
55 | } | |
56 | ||
b530e9e1 THJ |
57 | static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations, |
58 | u32 repeat, int *err, u32 *duration) | |
607b9cc9 LB |
59 | __must_hold(rcu) |
60 | { | |
b530e9e1 | 61 | t->i += iterations; |
607b9cc9 LB |
62 | if (t->i >= repeat) { |
63 | /* We're done. */ | |
64 | t->time_spent += ktime_get_ns() - t->time_start; | |
65 | do_div(t->time_spent, t->i); | |
66 | *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent; | |
67 | *err = 0; | |
68 | goto reset; | |
69 | } | |
70 | ||
71 | if (signal_pending(current)) { | |
72 | /* During iteration: we've been cancelled, abort. */ | |
73 | *err = -EINTR; | |
74 | goto reset; | |
75 | } | |
76 | ||
77 | if (need_resched()) { | |
78 | /* During iteration: we need to reschedule between runs. */ | |
79 | t->time_spent += ktime_get_ns() - t->time_start; | |
80 | bpf_test_timer_leave(t); | |
81 | cond_resched(); | |
82 | bpf_test_timer_enter(t); | |
83 | } | |
84 | ||
85 | /* Do another round. */ | |
86 | return true; | |
87 | ||
88 | reset: | |
89 | t->i = 0; | |
90 | return false; | |
91 | } | |
92 | ||
b530e9e1 THJ |
93 | /* We put this struct at the head of each page with a context and frame |
94 | * initialised when the page is allocated, so we don't have to do this on each | |
95 | * repetition of the test run. | |
96 | */ | |
97 | struct xdp_page_head { | |
98 | struct xdp_buff orig_ctx; | |
99 | struct xdp_buff ctx; | |
100 | struct xdp_frame frm; | |
101 | u8 data[]; | |
102 | }; | |
103 | ||
104 | struct xdp_test_data { | |
105 | struct xdp_buff *orig_ctx; | |
106 | struct xdp_rxq_info rxq; | |
107 | struct net_device *dev; | |
108 | struct page_pool *pp; | |
109 | struct xdp_frame **frames; | |
110 | struct sk_buff **skbs; | |
425d2393 | 111 | struct xdp_mem_info mem; |
b530e9e1 THJ |
112 | u32 batch_size; |
113 | u32 frame_cnt; | |
114 | }; | |
115 | ||
b6f1f780 | 116 | #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head)) |
b530e9e1 THJ |
117 | #define TEST_XDP_MAX_BATCH 256 |
118 | ||
119 | static void xdp_test_run_init_page(struct page *page, void *arg) | |
120 | { | |
121 | struct xdp_page_head *head = phys_to_virt(page_to_phys(page)); | |
122 | struct xdp_buff *new_ctx, *orig_ctx; | |
123 | u32 headroom = XDP_PACKET_HEADROOM; | |
124 | struct xdp_test_data *xdp = arg; | |
125 | size_t frm_len, meta_len; | |
126 | struct xdp_frame *frm; | |
127 | void *data; | |
128 | ||
129 | orig_ctx = xdp->orig_ctx; | |
130 | frm_len = orig_ctx->data_end - orig_ctx->data_meta; | |
131 | meta_len = orig_ctx->data - orig_ctx->data_meta; | |
132 | headroom -= meta_len; | |
133 | ||
134 | new_ctx = &head->ctx; | |
135 | frm = &head->frm; | |
136 | data = &head->data; | |
137 | memcpy(data + headroom, orig_ctx->data_meta, frm_len); | |
138 | ||
139 | xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); | |
140 | xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); | |
141 | new_ctx->data = new_ctx->data_meta + meta_len; | |
142 | ||
143 | xdp_update_frame_from_buff(new_ctx, frm); | |
144 | frm->mem = new_ctx->rxq->mem; | |
145 | ||
146 | memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); | |
147 | } | |
148 | ||
149 | static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx) | |
150 | { | |
b530e9e1 THJ |
151 | struct page_pool *pp; |
152 | int err = -ENOMEM; | |
153 | struct page_pool_params pp_params = { | |
154 | .order = 0, | |
155 | .flags = 0, | |
156 | .pool_size = xdp->batch_size, | |
157 | .nid = NUMA_NO_NODE, | |
b530e9e1 THJ |
158 | .init_callback = xdp_test_run_init_page, |
159 | .init_arg = xdp, | |
160 | }; | |
161 | ||
162 | xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); | |
163 | if (!xdp->frames) | |
164 | return -ENOMEM; | |
165 | ||
166 | xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); | |
167 | if (!xdp->skbs) | |
168 | goto err_skbs; | |
169 | ||
170 | pp = page_pool_create(&pp_params); | |
171 | if (IS_ERR(pp)) { | |
172 | err = PTR_ERR(pp); | |
173 | goto err_pp; | |
174 | } | |
175 | ||
176 | /* will copy 'mem.id' into pp->xdp_mem_id */ | |
425d2393 | 177 | err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp); |
b530e9e1 THJ |
178 | if (err) |
179 | goto err_mmodel; | |
180 | ||
181 | xdp->pp = pp; | |
182 | ||
183 | /* We create a 'fake' RXQ referencing the original dev, but with an | |
184 | * xdp_mem_info pointing to our page_pool | |
185 | */ | |
186 | xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0); | |
187 | xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL; | |
188 | xdp->rxq.mem.id = pp->xdp_mem_id; | |
189 | xdp->dev = orig_ctx->rxq->dev; | |
190 | xdp->orig_ctx = orig_ctx; | |
191 | ||
192 | return 0; | |
193 | ||
194 | err_mmodel: | |
195 | page_pool_destroy(pp); | |
196 | err_pp: | |
743bec1b | 197 | kvfree(xdp->skbs); |
b530e9e1 | 198 | err_skbs: |
743bec1b | 199 | kvfree(xdp->frames); |
b530e9e1 THJ |
200 | return err; |
201 | } | |
202 | ||
203 | static void xdp_test_run_teardown(struct xdp_test_data *xdp) | |
204 | { | |
425d2393 | 205 | xdp_unreg_mem_model(&xdp->mem); |
b530e9e1 THJ |
206 | page_pool_destroy(xdp->pp); |
207 | kfree(xdp->frames); | |
208 | kfree(xdp->skbs); | |
209 | } | |
210 | ||
211 | static bool ctx_was_changed(struct xdp_page_head *head) | |
212 | { | |
213 | return head->orig_ctx.data != head->ctx.data || | |
214 | head->orig_ctx.data_meta != head->ctx.data_meta || | |
215 | head->orig_ctx.data_end != head->ctx.data_end; | |
216 | } | |
217 | ||
218 | static void reset_ctx(struct xdp_page_head *head) | |
219 | { | |
220 | if (likely(!ctx_was_changed(head))) | |
221 | return; | |
222 | ||
223 | head->ctx.data = head->orig_ctx.data; | |
224 | head->ctx.data_meta = head->orig_ctx.data_meta; | |
225 | head->ctx.data_end = head->orig_ctx.data_end; | |
226 | xdp_update_frame_from_buff(&head->ctx, &head->frm); | |
227 | } | |
228 | ||
229 | static int xdp_recv_frames(struct xdp_frame **frames, int nframes, | |
230 | struct sk_buff **skbs, | |
231 | struct net_device *dev) | |
232 | { | |
233 | gfp_t gfp = __GFP_ZERO | GFP_ATOMIC; | |
234 | int i, n; | |
235 | LIST_HEAD(list); | |
236 | ||
237 | n = kmem_cache_alloc_bulk(skbuff_head_cache, gfp, nframes, (void **)skbs); | |
238 | if (unlikely(n == 0)) { | |
239 | for (i = 0; i < nframes; i++) | |
240 | xdp_return_frame(frames[i]); | |
241 | return -ENOMEM; | |
242 | } | |
243 | ||
244 | for (i = 0; i < nframes; i++) { | |
245 | struct xdp_frame *xdpf = frames[i]; | |
246 | struct sk_buff *skb = skbs[i]; | |
247 | ||
248 | skb = __xdp_build_skb_from_frame(xdpf, skb, dev); | |
249 | if (!skb) { | |
250 | xdp_return_frame(xdpf); | |
251 | continue; | |
252 | } | |
253 | ||
254 | list_add_tail(&skb->list, &list); | |
255 | } | |
256 | netif_receive_skb_list(&list); | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, | |
262 | u32 repeat) | |
263 | { | |
264 | struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); | |
265 | int err = 0, act, ret, i, nframes = 0, batch_sz; | |
266 | struct xdp_frame **frames = xdp->frames; | |
267 | struct xdp_page_head *head; | |
268 | struct xdp_frame *frm; | |
269 | bool redirect = false; | |
270 | struct xdp_buff *ctx; | |
271 | struct page *page; | |
272 | ||
273 | batch_sz = min_t(u32, repeat, xdp->batch_size); | |
274 | ||
275 | local_bh_disable(); | |
276 | xdp_set_return_frame_no_direct(); | |
277 | ||
278 | for (i = 0; i < batch_sz; i++) { | |
279 | page = page_pool_dev_alloc_pages(xdp->pp); | |
280 | if (!page) { | |
281 | err = -ENOMEM; | |
282 | goto out; | |
283 | } | |
284 | ||
285 | head = phys_to_virt(page_to_phys(page)); | |
286 | reset_ctx(head); | |
287 | ctx = &head->ctx; | |
288 | frm = &head->frm; | |
289 | xdp->frame_cnt++; | |
290 | ||
291 | act = bpf_prog_run_xdp(prog, ctx); | |
292 | ||
293 | /* if program changed pkt bounds we need to update the xdp_frame */ | |
294 | if (unlikely(ctx_was_changed(head))) { | |
295 | ret = xdp_update_frame_from_buff(ctx, frm); | |
296 | if (ret) { | |
297 | xdp_return_buff(ctx); | |
298 | continue; | |
299 | } | |
300 | } | |
301 | ||
302 | switch (act) { | |
303 | case XDP_TX: | |
304 | /* we can't do a real XDP_TX since we're not in the | |
305 | * driver, so turn it into a REDIRECT back to the same | |
306 | * index | |
307 | */ | |
308 | ri->tgt_index = xdp->dev->ifindex; | |
309 | ri->map_id = INT_MAX; | |
310 | ri->map_type = BPF_MAP_TYPE_UNSPEC; | |
311 | fallthrough; | |
312 | case XDP_REDIRECT: | |
313 | redirect = true; | |
314 | ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog); | |
315 | if (ret) | |
316 | xdp_return_buff(ctx); | |
317 | break; | |
318 | case XDP_PASS: | |
319 | frames[nframes++] = frm; | |
320 | break; | |
321 | default: | |
322 | bpf_warn_invalid_xdp_action(NULL, prog, act); | |
323 | fallthrough; | |
324 | case XDP_DROP: | |
325 | xdp_return_buff(ctx); | |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
330 | out: | |
331 | if (redirect) | |
332 | xdp_do_flush(); | |
333 | if (nframes) { | |
334 | ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev); | |
335 | if (ret) | |
336 | err = ret; | |
337 | } | |
338 | ||
339 | xdp_clear_return_frame_no_direct(); | |
340 | local_bh_enable(); | |
341 | return err; | |
342 | } | |
343 | ||
344 | static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx, | |
345 | u32 repeat, u32 batch_size, u32 *time) | |
346 | ||
347 | { | |
348 | struct xdp_test_data xdp = { .batch_size = batch_size }; | |
349 | struct bpf_test_timer t = { .mode = NO_MIGRATE }; | |
350 | int ret; | |
351 | ||
352 | if (!repeat) | |
353 | repeat = 1; | |
354 | ||
355 | ret = xdp_test_run_setup(&xdp, ctx); | |
356 | if (ret) | |
357 | return ret; | |
358 | ||
359 | bpf_test_timer_enter(&t); | |
360 | do { | |
361 | xdp.frame_cnt = 0; | |
362 | ret = xdp_test_run_batch(&xdp, prog, repeat - t.i); | |
363 | if (unlikely(ret < 0)) | |
364 | break; | |
365 | } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time)); | |
366 | bpf_test_timer_leave(&t); | |
367 | ||
368 | xdp_test_run_teardown(&xdp); | |
369 | return ret; | |
370 | } | |
371 | ||
df1a2cb7 | 372 | static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, |
f23c4b39 | 373 | u32 *retval, u32 *time, bool xdp) |
1cf1cae9 | 374 | { |
c7603cfa AN |
375 | struct bpf_prog_array_item item = {.prog = prog}; |
376 | struct bpf_run_ctx *old_ctx; | |
377 | struct bpf_cg_run_ctx run_ctx; | |
607b9cc9 | 378 | struct bpf_test_timer t = { NO_MIGRATE }; |
8bad74f9 | 379 | enum bpf_cgroup_storage_type stype; |
607b9cc9 | 380 | int ret; |
1cf1cae9 | 381 | |
8bad74f9 | 382 | for_each_cgroup_storage_type(stype) { |
c7603cfa AN |
383 | item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype); |
384 | if (IS_ERR(item.cgroup_storage[stype])) { | |
385 | item.cgroup_storage[stype] = NULL; | |
8bad74f9 | 386 | for_each_cgroup_storage_type(stype) |
c7603cfa | 387 | bpf_cgroup_storage_free(item.cgroup_storage[stype]); |
8bad74f9 RG |
388 | return -ENOMEM; |
389 | } | |
390 | } | |
f42ee093 | 391 | |
1cf1cae9 AS |
392 | if (!repeat) |
393 | repeat = 1; | |
df1a2cb7 | 394 | |
607b9cc9 | 395 | bpf_test_timer_enter(&t); |
c7603cfa | 396 | old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); |
607b9cc9 | 397 | do { |
c7603cfa | 398 | run_ctx.prog_item = &item; |
f23c4b39 BT |
399 | if (xdp) |
400 | *retval = bpf_prog_run_xdp(prog, ctx); | |
401 | else | |
fb7dd8bc | 402 | *retval = bpf_prog_run(prog, ctx); |
b530e9e1 | 403 | } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time)); |
c7603cfa | 404 | bpf_reset_run_ctx(old_ctx); |
607b9cc9 | 405 | bpf_test_timer_leave(&t); |
1cf1cae9 | 406 | |
8bad74f9 | 407 | for_each_cgroup_storage_type(stype) |
c7603cfa | 408 | bpf_cgroup_storage_free(item.cgroup_storage[stype]); |
f42ee093 | 409 | |
df1a2cb7 | 410 | return ret; |
1cf1cae9 AS |
411 | } |
412 | ||
78e52272 DM |
413 | static int bpf_test_finish(const union bpf_attr *kattr, |
414 | union bpf_attr __user *uattr, const void *data, | |
7855e0db LB |
415 | struct skb_shared_info *sinfo, u32 size, |
416 | u32 retval, u32 duration) | |
1cf1cae9 | 417 | { |
78e52272 | 418 | void __user *data_out = u64_to_user_ptr(kattr->test.data_out); |
1cf1cae9 | 419 | int err = -EFAULT; |
b5a36b1e | 420 | u32 copy_size = size; |
1cf1cae9 | 421 | |
b5a36b1e LB |
422 | /* Clamp copy if the user has provided a size hint, but copy the full |
423 | * buffer if not to retain old behaviour. | |
424 | */ | |
425 | if (kattr->test.data_size_out && | |
426 | copy_size > kattr->test.data_size_out) { | |
427 | copy_size = kattr->test.data_size_out; | |
428 | err = -ENOSPC; | |
429 | } | |
430 | ||
7855e0db LB |
431 | if (data_out) { |
432 | int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size; | |
433 | ||
530e214c SF |
434 | if (len < 0) { |
435 | err = -ENOSPC; | |
436 | goto out; | |
437 | } | |
438 | ||
7855e0db LB |
439 | if (copy_to_user(data_out, data, len)) |
440 | goto out; | |
441 | ||
442 | if (sinfo) { | |
5d1e9f43 SF |
443 | int i, offset = len; |
444 | u32 data_len; | |
7855e0db LB |
445 | |
446 | for (i = 0; i < sinfo->nr_frags; i++) { | |
447 | skb_frag_t *frag = &sinfo->frags[i]; | |
448 | ||
449 | if (offset >= copy_size) { | |
450 | err = -ENOSPC; | |
451 | break; | |
452 | } | |
453 | ||
5d1e9f43 | 454 | data_len = min_t(u32, copy_size - offset, |
7855e0db LB |
455 | skb_frag_size(frag)); |
456 | ||
457 | if (copy_to_user(data_out + offset, | |
458 | skb_frag_address(frag), | |
459 | data_len)) | |
460 | goto out; | |
461 | ||
462 | offset += data_len; | |
463 | } | |
464 | } | |
465 | } | |
466 | ||
1cf1cae9 AS |
467 | if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) |
468 | goto out; | |
469 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) | |
470 | goto out; | |
471 | if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) | |
472 | goto out; | |
b5a36b1e LB |
473 | if (err != -ENOSPC) |
474 | err = 0; | |
1cf1cae9 | 475 | out: |
e950e843 | 476 | trace_bpf_test_finish(&err); |
1cf1cae9 AS |
477 | return err; |
478 | } | |
479 | ||
faeb2dce AS |
480 | /* Integer types of various sizes and pointer combinations cover variety of |
481 | * architecture dependent calling conventions. 7+ can be supported in the | |
482 | * future. | |
483 | */ | |
e9ff9d52 | 484 | __diag_push(); |
0b206c6d KKD |
485 | __diag_ignore_all("-Wmissing-prototypes", |
486 | "Global functions as their definitions will be in vmlinux BTF"); | |
faeb2dce AS |
487 | int noinline bpf_fentry_test1(int a) |
488 | { | |
489 | return a + 1; | |
490 | } | |
46565696 KKD |
491 | EXPORT_SYMBOL_GPL(bpf_fentry_test1); |
492 | ALLOW_ERROR_INJECTION(bpf_fentry_test1, ERRNO); | |
faeb2dce AS |
493 | |
494 | int noinline bpf_fentry_test2(int a, u64 b) | |
495 | { | |
496 | return a + b; | |
497 | } | |
498 | ||
499 | int noinline bpf_fentry_test3(char a, int b, u64 c) | |
500 | { | |
501 | return a + b + c; | |
502 | } | |
503 | ||
504 | int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) | |
505 | { | |
506 | return (long)a + b + c + d; | |
507 | } | |
508 | ||
509 | int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) | |
510 | { | |
511 | return a + (long)b + c + d + e; | |
512 | } | |
513 | ||
514 | int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) | |
515 | { | |
516 | return a + (long)b + c + d + (long)e + f; | |
517 | } | |
518 | ||
d923021c YS |
519 | struct bpf_fentry_test_t { |
520 | struct bpf_fentry_test_t *a; | |
521 | }; | |
522 | ||
523 | int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg) | |
524 | { | |
525 | return (long)arg; | |
526 | } | |
527 | ||
528 | int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg) | |
529 | { | |
530 | return (long)arg->a; | |
531 | } | |
532 | ||
3d08b6f2 KS |
533 | int noinline bpf_modify_return_test(int a, int *b) |
534 | { | |
535 | *b += 1; | |
536 | return a + *b; | |
537 | } | |
7bd1590d MKL |
538 | |
539 | u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d) | |
540 | { | |
541 | return a + b + c + d; | |
542 | } | |
543 | ||
544 | int noinline bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b) | |
545 | { | |
546 | return a + b; | |
547 | } | |
548 | ||
549 | struct sock * noinline bpf_kfunc_call_test3(struct sock *sk) | |
550 | { | |
551 | return sk; | |
552 | } | |
553 | ||
792c0a34 KKD |
554 | struct prog_test_member1 { |
555 | int a; | |
556 | }; | |
557 | ||
8218ccb5 | 558 | struct prog_test_member { |
792c0a34 KKD |
559 | struct prog_test_member1 m; |
560 | int c; | |
8218ccb5 KKD |
561 | }; |
562 | ||
c1ff181f KKD |
563 | struct prog_test_ref_kfunc { |
564 | int a; | |
565 | int b; | |
8218ccb5 | 566 | struct prog_test_member memb; |
c1ff181f | 567 | struct prog_test_ref_kfunc *next; |
5cdccadc | 568 | refcount_t cnt; |
c1ff181f KKD |
569 | }; |
570 | ||
571 | static struct prog_test_ref_kfunc prog_test_struct = { | |
572 | .a = 42, | |
573 | .b = 108, | |
574 | .next = &prog_test_struct, | |
5cdccadc | 575 | .cnt = REFCOUNT_INIT(1), |
c1ff181f KKD |
576 | }; |
577 | ||
578 | noinline struct prog_test_ref_kfunc * | |
579 | bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) | |
580 | { | |
5cdccadc | 581 | refcount_inc(&prog_test_struct.cnt); |
c1ff181f KKD |
582 | return &prog_test_struct; |
583 | } | |
584 | ||
792c0a34 KKD |
585 | noinline struct prog_test_member * |
586 | bpf_kfunc_call_memb_acquire(void) | |
587 | { | |
5cdccadc KKD |
588 | WARN_ON_ONCE(1); |
589 | return NULL; | |
792c0a34 KKD |
590 | } |
591 | ||
c1ff181f KKD |
592 | noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) |
593 | { | |
5cdccadc KKD |
594 | if (!p) |
595 | return; | |
596 | ||
597 | refcount_dec(&p->cnt); | |
c1ff181f KKD |
598 | } |
599 | ||
8218ccb5 KKD |
600 | noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p) |
601 | { | |
602 | } | |
603 | ||
792c0a34 KKD |
604 | noinline void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p) |
605 | { | |
5cdccadc | 606 | WARN_ON_ONCE(1); |
792c0a34 KKD |
607 | } |
608 | ||
22ed8d5a BT |
609 | static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size) |
610 | { | |
611 | if (size > 2 * sizeof(int)) | |
612 | return NULL; | |
613 | ||
614 | return (int *)p; | |
615 | } | |
616 | ||
617 | noinline int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p, const int rdwr_buf_size) | |
618 | { | |
619 | return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size); | |
620 | } | |
621 | ||
622 | noinline int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) | |
623 | { | |
624 | return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size); | |
625 | } | |
626 | ||
627 | /* the next 2 ones can't be really used for testing expect to ensure | |
628 | * that the verifier rejects the call. | |
629 | * Acquire functions must return struct pointers, so these ones are | |
630 | * failing. | |
631 | */ | |
632 | noinline int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) | |
633 | { | |
634 | return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size); | |
635 | } | |
636 | ||
637 | noinline void bpf_kfunc_call_int_mem_release(int *p) | |
638 | { | |
639 | } | |
640 | ||
05a945de | 641 | noinline struct prog_test_ref_kfunc * |
5cdccadc | 642 | bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **pp, int a, int b) |
05a945de | 643 | { |
5cdccadc KKD |
644 | struct prog_test_ref_kfunc *p = READ_ONCE(*pp); |
645 | ||
646 | if (!p) | |
647 | return NULL; | |
648 | refcount_inc(&p->cnt); | |
649 | return p; | |
05a945de KKD |
650 | } |
651 | ||
c1ff181f KKD |
652 | struct prog_test_pass1 { |
653 | int x0; | |
654 | struct { | |
655 | int x1; | |
656 | struct { | |
657 | int x2; | |
658 | struct { | |
659 | int x3; | |
660 | }; | |
661 | }; | |
662 | }; | |
663 | }; | |
664 | ||
665 | struct prog_test_pass2 { | |
666 | int len; | |
667 | short arr1[4]; | |
668 | struct { | |
669 | char arr2[4]; | |
670 | unsigned long arr3[8]; | |
671 | } x; | |
672 | }; | |
673 | ||
674 | struct prog_test_fail1 { | |
675 | void *p; | |
676 | int x; | |
677 | }; | |
678 | ||
679 | struct prog_test_fail2 { | |
680 | int x8; | |
681 | struct prog_test_pass1 x; | |
682 | }; | |
683 | ||
684 | struct prog_test_fail3 { | |
685 | int len; | |
686 | char arr1[2]; | |
ed8bb032 | 687 | char arr2[]; |
c1ff181f KKD |
688 | }; |
689 | ||
690 | noinline void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) | |
691 | { | |
692 | } | |
693 | ||
694 | noinline void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) | |
695 | { | |
696 | } | |
697 | ||
698 | noinline void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) | |
699 | { | |
700 | } | |
701 | ||
702 | noinline void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) | |
703 | { | |
704 | } | |
705 | ||
706 | noinline void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p) | |
707 | { | |
708 | } | |
709 | ||
710 | noinline void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p) | |
711 | { | |
712 | } | |
713 | ||
714 | noinline void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz) | |
715 | { | |
716 | } | |
717 | ||
718 | noinline void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len) | |
719 | { | |
720 | } | |
721 | ||
722 | noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) | |
723 | { | |
724 | } | |
725 | ||
56e948ff KKD |
726 | noinline void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p) |
727 | { | |
728 | } | |
729 | ||
e3389458 AS |
730 | noinline void bpf_kfunc_call_test_destructive(void) |
731 | { | |
732 | } | |
733 | ||
e9ff9d52 | 734 | __diag_pop(); |
3d08b6f2 KS |
735 | |
736 | ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); | |
737 | ||
a4703e31 KKD |
738 | BTF_SET8_START(test_sk_check_kfunc_ids) |
739 | BTF_ID_FLAGS(func, bpf_kfunc_call_test1) | |
740 | BTF_ID_FLAGS(func, bpf_kfunc_call_test2) | |
741 | BTF_ID_FLAGS(func, bpf_kfunc_call_test3) | |
742 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL) | |
743 | BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL) | |
744 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE) | |
745 | BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE) | |
746 | BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE) | |
22ed8d5a BT |
747 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL) |
748 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL) | |
749 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL) | |
750 | BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE) | |
a4703e31 KKD |
751 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_kptr_get, KF_ACQUIRE | KF_RET_NULL | KF_KPTR_GET) |
752 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx) | |
753 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1) | |
754 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2) | |
755 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1) | |
756 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2) | |
757 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3) | |
758 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1) | |
759 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1) | |
760 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2) | |
56e948ff | 761 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS) |
e3389458 | 762 | BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) |
a4703e31 | 763 | BTF_SET8_END(test_sk_check_kfunc_ids) |
05a945de | 764 | |
be3d72a2 LB |
765 | static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, |
766 | u32 size, u32 headroom, u32 tailroom) | |
1cf1cae9 AS |
767 | { |
768 | void __user *data_in = u64_to_user_ptr(kattr->test.data_in); | |
769 | void *data; | |
770 | ||
771 | if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) | |
772 | return ERR_PTR(-EINVAL); | |
773 | ||
d800bad6 JDB |
774 | if (user_size > size) |
775 | return ERR_PTR(-EMSGSIZE); | |
776 | ||
d3fd203f | 777 | size = SKB_DATA_ALIGN(size); |
1cf1cae9 AS |
778 | data = kzalloc(size + headroom + tailroom, GFP_USER); |
779 | if (!data) | |
780 | return ERR_PTR(-ENOMEM); | |
781 | ||
d800bad6 | 782 | if (copy_from_user(data + headroom, data_in, user_size)) { |
1cf1cae9 AS |
783 | kfree(data); |
784 | return ERR_PTR(-EFAULT); | |
785 | } | |
da00d2f1 | 786 | |
1cf1cae9 AS |
787 | return data; |
788 | } | |
789 | ||
da00d2f1 KS |
790 | int bpf_prog_test_run_tracing(struct bpf_prog *prog, |
791 | const union bpf_attr *kattr, | |
792 | union bpf_attr __user *uattr) | |
793 | { | |
d923021c | 794 | struct bpf_fentry_test_t arg = {}; |
3d08b6f2 KS |
795 | u16 side_effect = 0, ret = 0; |
796 | int b = 2, err = -EFAULT; | |
797 | u32 retval = 0; | |
da00d2f1 | 798 | |
b530e9e1 | 799 | if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) |
1b4d60ec SL |
800 | return -EINVAL; |
801 | ||
da00d2f1 KS |
802 | switch (prog->expected_attach_type) { |
803 | case BPF_TRACE_FENTRY: | |
804 | case BPF_TRACE_FEXIT: | |
805 | if (bpf_fentry_test1(1) != 2 || | |
806 | bpf_fentry_test2(2, 3) != 5 || | |
807 | bpf_fentry_test3(4, 5, 6) != 15 || | |
808 | bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || | |
809 | bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || | |
d923021c YS |
810 | bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || |
811 | bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || | |
812 | bpf_fentry_test8(&arg) != 0) | |
da00d2f1 KS |
813 | goto out; |
814 | break; | |
3d08b6f2 KS |
815 | case BPF_MODIFY_RETURN: |
816 | ret = bpf_modify_return_test(1, &b); | |
817 | if (b != 2) | |
818 | side_effect = 1; | |
819 | break; | |
da00d2f1 KS |
820 | default: |
821 | goto out; | |
822 | } | |
823 | ||
3d08b6f2 KS |
824 | retval = ((u32)side_effect << 16) | ret; |
825 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) | |
826 | goto out; | |
827 | ||
da00d2f1 KS |
828 | err = 0; |
829 | out: | |
830 | trace_bpf_test_finish(&err); | |
831 | return err; | |
832 | } | |
833 | ||
1b4d60ec SL |
834 | struct bpf_raw_tp_test_run_info { |
835 | struct bpf_prog *prog; | |
836 | void *ctx; | |
837 | u32 retval; | |
838 | }; | |
839 | ||
840 | static void | |
841 | __bpf_prog_test_run_raw_tp(void *data) | |
842 | { | |
843 | struct bpf_raw_tp_test_run_info *info = data; | |
844 | ||
845 | rcu_read_lock(); | |
fb7dd8bc | 846 | info->retval = bpf_prog_run(info->prog, info->ctx); |
1b4d60ec SL |
847 | rcu_read_unlock(); |
848 | } | |
849 | ||
850 | int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, | |
851 | const union bpf_attr *kattr, | |
852 | union bpf_attr __user *uattr) | |
853 | { | |
854 | void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); | |
855 | __u32 ctx_size_in = kattr->test.ctx_size_in; | |
856 | struct bpf_raw_tp_test_run_info info; | |
857 | int cpu = kattr->test.cpu, err = 0; | |
963ec27a | 858 | int current_cpu; |
1b4d60ec SL |
859 | |
860 | /* doesn't support data_in/out, ctx_out, duration, or repeat */ | |
861 | if (kattr->test.data_in || kattr->test.data_out || | |
862 | kattr->test.ctx_out || kattr->test.duration || | |
b530e9e1 | 863 | kattr->test.repeat || kattr->test.batch_size) |
1b4d60ec SL |
864 | return -EINVAL; |
865 | ||
7ac6ad05 SL |
866 | if (ctx_size_in < prog->aux->max_ctx_offset || |
867 | ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64)) | |
1b4d60ec SL |
868 | return -EINVAL; |
869 | ||
870 | if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) | |
871 | return -EINVAL; | |
872 | ||
873 | if (ctx_size_in) { | |
db5b6a46 QW |
874 | info.ctx = memdup_user(ctx_in, ctx_size_in); |
875 | if (IS_ERR(info.ctx)) | |
876 | return PTR_ERR(info.ctx); | |
1b4d60ec SL |
877 | } else { |
878 | info.ctx = NULL; | |
879 | } | |
880 | ||
881 | info.prog = prog; | |
882 | ||
963ec27a | 883 | current_cpu = get_cpu(); |
1b4d60ec | 884 | if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || |
963ec27a | 885 | cpu == current_cpu) { |
1b4d60ec | 886 | __bpf_prog_test_run_raw_tp(&info); |
963ec27a | 887 | } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { |
1b4d60ec SL |
888 | /* smp_call_function_single() also checks cpu_online() |
889 | * after csd_lock(). However, since cpu is from user | |
890 | * space, let's do an extra quick check to filter out | |
891 | * invalid value before smp_call_function_single(). | |
892 | */ | |
963ec27a SL |
893 | err = -ENXIO; |
894 | } else { | |
1b4d60ec SL |
895 | err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, |
896 | &info, 1); | |
1b4d60ec | 897 | } |
963ec27a | 898 | put_cpu(); |
1b4d60ec | 899 | |
963ec27a SL |
900 | if (!err && |
901 | copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) | |
1b4d60ec SL |
902 | err = -EFAULT; |
903 | ||
1b4d60ec SL |
904 | kfree(info.ctx); |
905 | return err; | |
906 | } | |
907 | ||
b0b9395d SF |
908 | static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) |
909 | { | |
910 | void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); | |
911 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); | |
912 | u32 size = kattr->test.ctx_size_in; | |
913 | void *data; | |
914 | int err; | |
915 | ||
916 | if (!data_in && !data_out) | |
917 | return NULL; | |
918 | ||
919 | data = kzalloc(max_size, GFP_USER); | |
920 | if (!data) | |
921 | return ERR_PTR(-ENOMEM); | |
922 | ||
923 | if (data_in) { | |
af2ac3e1 | 924 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size); |
b0b9395d SF |
925 | if (err) { |
926 | kfree(data); | |
927 | return ERR_PTR(err); | |
928 | } | |
929 | ||
930 | size = min_t(u32, max_size, size); | |
931 | if (copy_from_user(data, data_in, size)) { | |
932 | kfree(data); | |
933 | return ERR_PTR(-EFAULT); | |
934 | } | |
935 | } | |
936 | return data; | |
937 | } | |
938 | ||
939 | static int bpf_ctx_finish(const union bpf_attr *kattr, | |
940 | union bpf_attr __user *uattr, const void *data, | |
941 | u32 size) | |
942 | { | |
943 | void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); | |
944 | int err = -EFAULT; | |
945 | u32 copy_size = size; | |
946 | ||
947 | if (!data || !data_out) | |
948 | return 0; | |
949 | ||
950 | if (copy_size > kattr->test.ctx_size_out) { | |
951 | copy_size = kattr->test.ctx_size_out; | |
952 | err = -ENOSPC; | |
953 | } | |
954 | ||
955 | if (copy_to_user(data_out, data, copy_size)) | |
956 | goto out; | |
957 | if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) | |
958 | goto out; | |
959 | if (err != -ENOSPC) | |
960 | err = 0; | |
961 | out: | |
962 | return err; | |
963 | } | |
964 | ||
965 | /** | |
966 | * range_is_zero - test whether buffer is initialized | |
967 | * @buf: buffer to check | |
968 | * @from: check from this position | |
969 | * @to: check up until (excluding) this position | |
970 | * | |
971 | * This function returns true if the there is a non-zero byte | |
972 | * in the buf in the range [from,to). | |
973 | */ | |
974 | static inline bool range_is_zero(void *buf, size_t from, size_t to) | |
975 | { | |
976 | return !memchr_inv((u8 *)buf + from, 0, to - from); | |
977 | } | |
978 | ||
979 | static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) | |
980 | { | |
981 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; | |
982 | ||
fd189422 ZS |
983 | if (!skb->len) |
984 | return -EINVAL; | |
985 | ||
b0b9395d SF |
986 | if (!__skb) |
987 | return 0; | |
988 | ||
989 | /* make sure the fields we don't use are zeroed */ | |
6de6c1f8 NS |
990 | if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) |
991 | return -EINVAL; | |
992 | ||
993 | /* mark is allowed */ | |
994 | ||
995 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), | |
996 | offsetof(struct __sk_buff, priority))) | |
b0b9395d SF |
997 | return -EINVAL; |
998 | ||
999 | /* priority is allowed */ | |
b238290b | 1000 | /* ingress_ifindex is allowed */ |
21594c44 DY |
1001 | /* ifindex is allowed */ |
1002 | ||
1003 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), | |
b0b9395d SF |
1004 | offsetof(struct __sk_buff, cb))) |
1005 | return -EINVAL; | |
1006 | ||
1007 | /* cb is allowed */ | |
1008 | ||
b590cb5f | 1009 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), |
ba940948 SF |
1010 | offsetof(struct __sk_buff, tstamp))) |
1011 | return -EINVAL; | |
1012 | ||
1013 | /* tstamp is allowed */ | |
850a88cc SF |
1014 | /* wire_len is allowed */ |
1015 | /* gso_segs is allowed */ | |
ba940948 | 1016 | |
850a88cc | 1017 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), |
cf62089b WB |
1018 | offsetof(struct __sk_buff, gso_size))) |
1019 | return -EINVAL; | |
1020 | ||
1021 | /* gso_size is allowed */ | |
1022 | ||
1023 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), | |
3384c7c7 VF |
1024 | offsetof(struct __sk_buff, hwtstamp))) |
1025 | return -EINVAL; | |
1026 | ||
1027 | /* hwtstamp is allowed */ | |
1028 | ||
1029 | if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp), | |
b0b9395d SF |
1030 | sizeof(struct __sk_buff))) |
1031 | return -EINVAL; | |
1032 | ||
6de6c1f8 | 1033 | skb->mark = __skb->mark; |
b0b9395d | 1034 | skb->priority = __skb->priority; |
b238290b | 1035 | skb->skb_iif = __skb->ingress_ifindex; |
ba940948 | 1036 | skb->tstamp = __skb->tstamp; |
b0b9395d SF |
1037 | memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); |
1038 | ||
850a88cc SF |
1039 | if (__skb->wire_len == 0) { |
1040 | cb->pkt_len = skb->len; | |
1041 | } else { | |
1042 | if (__skb->wire_len < skb->len || | |
7c4e983c | 1043 | __skb->wire_len > GSO_LEGACY_MAX_SIZE) |
850a88cc SF |
1044 | return -EINVAL; |
1045 | cb->pkt_len = __skb->wire_len; | |
1046 | } | |
1047 | ||
1048 | if (__skb->gso_segs > GSO_MAX_SEGS) | |
1049 | return -EINVAL; | |
1050 | skb_shinfo(skb)->gso_segs = __skb->gso_segs; | |
cf62089b | 1051 | skb_shinfo(skb)->gso_size = __skb->gso_size; |
3384c7c7 | 1052 | skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp; |
850a88cc | 1053 | |
b0b9395d SF |
1054 | return 0; |
1055 | } | |
1056 | ||
1057 | static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) | |
1058 | { | |
1059 | struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; | |
1060 | ||
1061 | if (!__skb) | |
1062 | return; | |
1063 | ||
6de6c1f8 | 1064 | __skb->mark = skb->mark; |
b0b9395d | 1065 | __skb->priority = skb->priority; |
b238290b | 1066 | __skb->ingress_ifindex = skb->skb_iif; |
21594c44 | 1067 | __skb->ifindex = skb->dev->ifindex; |
ba940948 | 1068 | __skb->tstamp = skb->tstamp; |
b0b9395d | 1069 | memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); |
850a88cc SF |
1070 | __skb->wire_len = cb->pkt_len; |
1071 | __skb->gso_segs = skb_shinfo(skb)->gso_segs; | |
3384c7c7 | 1072 | __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp; |
b0b9395d SF |
1073 | } |
1074 | ||
435b08ec DB |
1075 | static struct proto bpf_dummy_proto = { |
1076 | .name = "bpf_dummy", | |
1077 | .owner = THIS_MODULE, | |
1078 | .obj_size = sizeof(struct sock), | |
1079 | }; | |
1080 | ||
1cf1cae9 AS |
1081 | int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, |
1082 | union bpf_attr __user *uattr) | |
1083 | { | |
1084 | bool is_l2 = false, is_direct_pkt_access = false; | |
21594c44 DY |
1085 | struct net *net = current->nsproxy->net_ns; |
1086 | struct net_device *dev = net->loopback_dev; | |
1cf1cae9 AS |
1087 | u32 size = kattr->test.data_size_in; |
1088 | u32 repeat = kattr->test.repeat; | |
b0b9395d | 1089 | struct __sk_buff *ctx = NULL; |
1cf1cae9 | 1090 | u32 retval, duration; |
6e6fddc7 | 1091 | int hh_len = ETH_HLEN; |
1cf1cae9 | 1092 | struct sk_buff *skb; |
2cb494a3 | 1093 | struct sock *sk; |
1cf1cae9 AS |
1094 | void *data; |
1095 | int ret; | |
1096 | ||
b530e9e1 | 1097 | if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) |
1b4d60ec SL |
1098 | return -EINVAL; |
1099 | ||
be3d72a2 LB |
1100 | data = bpf_test_init(kattr, kattr->test.data_size_in, |
1101 | size, NET_SKB_PAD + NET_IP_ALIGN, | |
1cf1cae9 AS |
1102 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); |
1103 | if (IS_ERR(data)) | |
1104 | return PTR_ERR(data); | |
1105 | ||
b0b9395d SF |
1106 | ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); |
1107 | if (IS_ERR(ctx)) { | |
1108 | kfree(data); | |
1109 | return PTR_ERR(ctx); | |
1110 | } | |
1111 | ||
1cf1cae9 AS |
1112 | switch (prog->type) { |
1113 | case BPF_PROG_TYPE_SCHED_CLS: | |
1114 | case BPF_PROG_TYPE_SCHED_ACT: | |
1115 | is_l2 = true; | |
df561f66 | 1116 | fallthrough; |
1cf1cae9 AS |
1117 | case BPF_PROG_TYPE_LWT_IN: |
1118 | case BPF_PROG_TYPE_LWT_OUT: | |
1119 | case BPF_PROG_TYPE_LWT_XMIT: | |
1120 | is_direct_pkt_access = true; | |
1121 | break; | |
1122 | default: | |
1123 | break; | |
1124 | } | |
1125 | ||
435b08ec | 1126 | sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1); |
2cb494a3 SL |
1127 | if (!sk) { |
1128 | kfree(data); | |
b0b9395d | 1129 | kfree(ctx); |
2cb494a3 SL |
1130 | return -ENOMEM; |
1131 | } | |
2cb494a3 SL |
1132 | sock_init_data(NULL, sk); |
1133 | ||
1cf1cae9 AS |
1134 | skb = build_skb(data, 0); |
1135 | if (!skb) { | |
1136 | kfree(data); | |
b0b9395d | 1137 | kfree(ctx); |
435b08ec | 1138 | sk_free(sk); |
1cf1cae9 AS |
1139 | return -ENOMEM; |
1140 | } | |
2cb494a3 | 1141 | skb->sk = sk; |
1cf1cae9 | 1142 | |
586f8525 | 1143 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
1cf1cae9 | 1144 | __skb_put(skb, size); |
21594c44 DY |
1145 | if (ctx && ctx->ifindex > 1) { |
1146 | dev = dev_get_by_index(net, ctx->ifindex); | |
1147 | if (!dev) { | |
1148 | ret = -ENODEV; | |
1149 | goto out; | |
1150 | } | |
1151 | } | |
1152 | skb->protocol = eth_type_trans(skb, dev); | |
1cf1cae9 AS |
1153 | skb_reset_network_header(skb); |
1154 | ||
fa5cb548 DY |
1155 | switch (skb->protocol) { |
1156 | case htons(ETH_P_IP): | |
1157 | sk->sk_family = AF_INET; | |
1158 | if (sizeof(struct iphdr) <= skb_headlen(skb)) { | |
1159 | sk->sk_rcv_saddr = ip_hdr(skb)->saddr; | |
1160 | sk->sk_daddr = ip_hdr(skb)->daddr; | |
1161 | } | |
1162 | break; | |
1163 | #if IS_ENABLED(CONFIG_IPV6) | |
1164 | case htons(ETH_P_IPV6): | |
1165 | sk->sk_family = AF_INET6; | |
1166 | if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) { | |
1167 | sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; | |
1168 | sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; | |
1169 | } | |
1170 | break; | |
1171 | #endif | |
1172 | default: | |
1173 | break; | |
1174 | } | |
1175 | ||
1cf1cae9 | 1176 | if (is_l2) |
6e6fddc7 | 1177 | __skb_push(skb, hh_len); |
1cf1cae9 | 1178 | if (is_direct_pkt_access) |
6aaae2b6 | 1179 | bpf_compute_data_pointers(skb); |
b0b9395d SF |
1180 | ret = convert___skb_to_skb(skb, ctx); |
1181 | if (ret) | |
1182 | goto out; | |
f23c4b39 | 1183 | ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); |
b0b9395d SF |
1184 | if (ret) |
1185 | goto out; | |
6e6fddc7 DB |
1186 | if (!is_l2) { |
1187 | if (skb_headroom(skb) < hh_len) { | |
1188 | int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); | |
1189 | ||
1190 | if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { | |
b0b9395d SF |
1191 | ret = -ENOMEM; |
1192 | goto out; | |
6e6fddc7 DB |
1193 | } |
1194 | } | |
1195 | memset(__skb_push(skb, hh_len), 0, hh_len); | |
1196 | } | |
b0b9395d | 1197 | convert_skb_to___skb(skb, ctx); |
6e6fddc7 | 1198 | |
1cf1cae9 AS |
1199 | size = skb->len; |
1200 | /* bpf program can never convert linear skb to non-linear */ | |
1201 | if (WARN_ON_ONCE(skb_is_nonlinear(skb))) | |
1202 | size = skb_headlen(skb); | |
7855e0db LB |
1203 | ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval, |
1204 | duration); | |
b0b9395d SF |
1205 | if (!ret) |
1206 | ret = bpf_ctx_finish(kattr, uattr, ctx, | |
1207 | sizeof(struct __sk_buff)); | |
1208 | out: | |
21594c44 DY |
1209 | if (dev && dev != net->loopback_dev) |
1210 | dev_put(dev); | |
1cf1cae9 | 1211 | kfree_skb(skb); |
435b08ec | 1212 | sk_free(sk); |
b0b9395d | 1213 | kfree(ctx); |
1cf1cae9 AS |
1214 | return ret; |
1215 | } | |
1216 | ||
47316f4a ZE |
1217 | static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp) |
1218 | { | |
ec94670f ZE |
1219 | unsigned int ingress_ifindex, rx_queue_index; |
1220 | struct netdev_rx_queue *rxqueue; | |
1221 | struct net_device *device; | |
1222 | ||
47316f4a ZE |
1223 | if (!xdp_md) |
1224 | return 0; | |
1225 | ||
1226 | if (xdp_md->egress_ifindex != 0) | |
1227 | return -EINVAL; | |
1228 | ||
ec94670f ZE |
1229 | ingress_ifindex = xdp_md->ingress_ifindex; |
1230 | rx_queue_index = xdp_md->rx_queue_index; | |
1231 | ||
1232 | if (!ingress_ifindex && rx_queue_index) | |
47316f4a ZE |
1233 | return -EINVAL; |
1234 | ||
ec94670f ZE |
1235 | if (ingress_ifindex) { |
1236 | device = dev_get_by_index(current->nsproxy->net_ns, | |
1237 | ingress_ifindex); | |
1238 | if (!device) | |
1239 | return -ENODEV; | |
1240 | ||
1241 | if (rx_queue_index >= device->real_num_rx_queues) | |
1242 | goto free_dev; | |
1243 | ||
1244 | rxqueue = __netif_get_rx_queue(device, rx_queue_index); | |
47316f4a | 1245 | |
ec94670f ZE |
1246 | if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq)) |
1247 | goto free_dev; | |
1248 | ||
1249 | xdp->rxq = &rxqueue->xdp_rxq; | |
1250 | /* The device is now tracked in the xdp->rxq for later | |
1251 | * dev_put() | |
1252 | */ | |
1253 | } | |
1254 | ||
1255 | xdp->data = xdp->data_meta + xdp_md->data; | |
47316f4a | 1256 | return 0; |
ec94670f ZE |
1257 | |
1258 | free_dev: | |
1259 | dev_put(device); | |
1260 | return -EINVAL; | |
1261 | } | |
1262 | ||
1263 | static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md) | |
1264 | { | |
1265 | if (!xdp_md) | |
1266 | return; | |
1267 | ||
1268 | xdp_md->data = xdp->data - xdp->data_meta; | |
1269 | xdp_md->data_end = xdp->data_end - xdp->data_meta; | |
1270 | ||
1271 | if (xdp_md->ingress_ifindex) | |
1272 | dev_put(xdp->rxq->dev); | |
47316f4a ZE |
1273 | } |
1274 | ||
1cf1cae9 AS |
1275 | int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, |
1276 | union bpf_attr __user *uattr) | |
1277 | { | |
b530e9e1 | 1278 | bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES); |
bc56c919 | 1279 | u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
b530e9e1 | 1280 | u32 batch_size = kattr->test.batch_size; |
eecbfd97 | 1281 | u32 retval = 0, duration, max_data_sz; |
1cf1cae9 | 1282 | u32 size = kattr->test.data_size_in; |
1c194998 | 1283 | u32 headroom = XDP_PACKET_HEADROOM; |
1cf1cae9 | 1284 | u32 repeat = kattr->test.repeat; |
65073a67 | 1285 | struct netdev_rx_queue *rxqueue; |
1c194998 | 1286 | struct skb_shared_info *sinfo; |
1cf1cae9 | 1287 | struct xdp_buff xdp = {}; |
1c194998 | 1288 | int i, ret = -EINVAL; |
47316f4a | 1289 | struct xdp_md *ctx; |
1cf1cae9 | 1290 | void *data; |
1cf1cae9 | 1291 | |
5e21bb4e XZ |
1292 | if (prog->expected_attach_type == BPF_XDP_DEVMAP || |
1293 | prog->expected_attach_type == BPF_XDP_CPUMAP) | |
1294 | return -EINVAL; | |
6d4eb36d | 1295 | |
b530e9e1 THJ |
1296 | if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES) |
1297 | return -EINVAL; | |
1298 | ||
1299 | if (do_live) { | |
1300 | if (!batch_size) | |
1301 | batch_size = NAPI_POLL_WEIGHT; | |
1302 | else if (batch_size > TEST_XDP_MAX_BATCH) | |
1303 | return -E2BIG; | |
b6f1f780 THJ |
1304 | |
1305 | headroom += sizeof(struct xdp_page_head); | |
b530e9e1 THJ |
1306 | } else if (batch_size) { |
1307 | return -EINVAL; | |
1308 | } | |
1309 | ||
47316f4a ZE |
1310 | ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md)); |
1311 | if (IS_ERR(ctx)) | |
1312 | return PTR_ERR(ctx); | |
1313 | ||
1314 | if (ctx) { | |
1315 | /* There can't be user provided data before the meta data */ | |
1316 | if (ctx->data_meta || ctx->data_end != size || | |
1317 | ctx->data > ctx->data_end || | |
b530e9e1 THJ |
1318 | unlikely(xdp_metalen_invalid(ctx->data)) || |
1319 | (do_live && (kattr->test.data_out || kattr->test.ctx_out))) | |
47316f4a ZE |
1320 | goto free_ctx; |
1321 | /* Meta data is allocated from the headroom */ | |
1322 | headroom -= ctx->data; | |
1323 | } | |
947e8b59 | 1324 | |
bc56c919 | 1325 | max_data_sz = 4096 - headroom - tailroom; |
b530e9e1 THJ |
1326 | if (size > max_data_sz) { |
1327 | /* disallow live data mode for jumbo frames */ | |
1328 | if (do_live) | |
1329 | goto free_ctx; | |
1330 | size = max_data_sz; | |
1331 | } | |
bc56c919 | 1332 | |
1c194998 | 1333 | data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom); |
47316f4a ZE |
1334 | if (IS_ERR(data)) { |
1335 | ret = PTR_ERR(data); | |
1336 | goto free_ctx; | |
1337 | } | |
1cf1cae9 | 1338 | |
65073a67 | 1339 | rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); |
1c194998 LB |
1340 | rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom; |
1341 | xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq); | |
be9df4af | 1342 | xdp_prepare_buff(&xdp, data, headroom, size, true); |
1c194998 | 1343 | sinfo = xdp_get_shared_info_from_buff(&xdp); |
be9df4af | 1344 | |
47316f4a ZE |
1345 | ret = xdp_convert_md_to_buff(ctx, &xdp); |
1346 | if (ret) | |
1347 | goto free_data; | |
1348 | ||
1c194998 LB |
1349 | if (unlikely(kattr->test.data_size_in > size)) { |
1350 | void __user *data_in = u64_to_user_ptr(kattr->test.data_in); | |
1351 | ||
1352 | while (size < kattr->test.data_size_in) { | |
1353 | struct page *page; | |
1354 | skb_frag_t *frag; | |
9d63b59d | 1355 | u32 data_len; |
1c194998 | 1356 | |
a6763080 LB |
1357 | if (sinfo->nr_frags == MAX_SKB_FRAGS) { |
1358 | ret = -ENOMEM; | |
1359 | goto out; | |
1360 | } | |
1361 | ||
1c194998 LB |
1362 | page = alloc_page(GFP_KERNEL); |
1363 | if (!page) { | |
1364 | ret = -ENOMEM; | |
1365 | goto out; | |
1366 | } | |
1367 | ||
1368 | frag = &sinfo->frags[sinfo->nr_frags++]; | |
1369 | __skb_frag_set_page(frag, page); | |
1370 | ||
9d63b59d | 1371 | data_len = min_t(u32, kattr->test.data_size_in - size, |
1c194998 LB |
1372 | PAGE_SIZE); |
1373 | skb_frag_size_set(frag, data_len); | |
1374 | ||
1375 | if (copy_from_user(page_address(page), data_in + size, | |
1376 | data_len)) { | |
1377 | ret = -EFAULT; | |
1378 | goto out; | |
1379 | } | |
1380 | sinfo->xdp_frags_size += data_len; | |
1381 | size += data_len; | |
1382 | } | |
1383 | xdp_buff_set_frags_flag(&xdp); | |
1384 | } | |
1385 | ||
de21d8bf LB |
1386 | if (repeat > 1) |
1387 | bpf_prog_change_xdp(NULL, prog); | |
1c194998 | 1388 | |
b530e9e1 THJ |
1389 | if (do_live) |
1390 | ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration); | |
1391 | else | |
1392 | ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); | |
ec94670f ZE |
1393 | /* We convert the xdp_buff back to an xdp_md before checking the return |
1394 | * code so the reference count of any held netdevice will be decremented | |
1395 | * even if the test run failed. | |
1396 | */ | |
1397 | xdp_convert_buff_to_md(&xdp, ctx); | |
dcb40590 RG |
1398 | if (ret) |
1399 | goto out; | |
47316f4a | 1400 | |
1c194998 | 1401 | size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; |
7855e0db LB |
1402 | ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size, |
1403 | retval, duration); | |
47316f4a ZE |
1404 | if (!ret) |
1405 | ret = bpf_ctx_finish(kattr, uattr, ctx, | |
1406 | sizeof(struct xdp_md)); | |
1407 | ||
dcb40590 | 1408 | out: |
de21d8bf LB |
1409 | if (repeat > 1) |
1410 | bpf_prog_change_xdp(prog, NULL); | |
47316f4a | 1411 | free_data: |
1c194998 LB |
1412 | for (i = 0; i < sinfo->nr_frags; i++) |
1413 | __free_page(skb_frag_page(&sinfo->frags[i])); | |
1cf1cae9 | 1414 | kfree(data); |
47316f4a ZE |
1415 | free_ctx: |
1416 | kfree(ctx); | |
1cf1cae9 AS |
1417 | return ret; |
1418 | } | |
b7a1848e | 1419 | |
b2ca4e1c SF |
1420 | static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) |
1421 | { | |
1422 | /* make sure the fields we don't use are zeroed */ | |
1423 | if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) | |
1424 | return -EINVAL; | |
1425 | ||
1426 | /* flags is allowed */ | |
1427 | ||
b590cb5f | 1428 | if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), |
b2ca4e1c SF |
1429 | sizeof(struct bpf_flow_keys))) |
1430 | return -EINVAL; | |
1431 | ||
1432 | return 0; | |
1433 | } | |
1434 | ||
b7a1848e SF |
1435 | int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, |
1436 | const union bpf_attr *kattr, | |
1437 | union bpf_attr __user *uattr) | |
1438 | { | |
607b9cc9 | 1439 | struct bpf_test_timer t = { NO_PREEMPT }; |
b7a1848e | 1440 | u32 size = kattr->test.data_size_in; |
7b8a1304 | 1441 | struct bpf_flow_dissector ctx = {}; |
b7a1848e | 1442 | u32 repeat = kattr->test.repeat; |
b2ca4e1c | 1443 | struct bpf_flow_keys *user_ctx; |
b7a1848e | 1444 | struct bpf_flow_keys flow_keys; |
7b8a1304 | 1445 | const struct ethhdr *eth; |
b2ca4e1c | 1446 | unsigned int flags = 0; |
b7a1848e | 1447 | u32 retval, duration; |
b7a1848e SF |
1448 | void *data; |
1449 | int ret; | |
b7a1848e | 1450 | |
b530e9e1 | 1451 | if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) |
1b4d60ec SL |
1452 | return -EINVAL; |
1453 | ||
7b8a1304 SF |
1454 | if (size < ETH_HLEN) |
1455 | return -EINVAL; | |
1456 | ||
be3d72a2 | 1457 | data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0); |
b7a1848e SF |
1458 | if (IS_ERR(data)) |
1459 | return PTR_ERR(data); | |
1460 | ||
7b8a1304 | 1461 | eth = (struct ethhdr *)data; |
b7a1848e | 1462 | |
b7a1848e SF |
1463 | if (!repeat) |
1464 | repeat = 1; | |
1465 | ||
b2ca4e1c SF |
1466 | user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); |
1467 | if (IS_ERR(user_ctx)) { | |
1468 | kfree(data); | |
1469 | return PTR_ERR(user_ctx); | |
1470 | } | |
1471 | if (user_ctx) { | |
1472 | ret = verify_user_bpf_flow_keys(user_ctx); | |
1473 | if (ret) | |
1474 | goto out; | |
1475 | flags = user_ctx->flags; | |
1476 | } | |
1477 | ||
7b8a1304 SF |
1478 | ctx.flow_keys = &flow_keys; |
1479 | ctx.data = data; | |
1480 | ctx.data_end = (__u8 *)data + size; | |
1481 | ||
607b9cc9 LB |
1482 | bpf_test_timer_enter(&t); |
1483 | do { | |
7b8a1304 | 1484 | retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, |
b2ca4e1c | 1485 | size, flags); |
b530e9e1 | 1486 | } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); |
607b9cc9 | 1487 | bpf_test_timer_leave(&t); |
7b8a1304 | 1488 | |
607b9cc9 LB |
1489 | if (ret < 0) |
1490 | goto out; | |
b7a1848e | 1491 | |
7855e0db LB |
1492 | ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL, |
1493 | sizeof(flow_keys), retval, duration); | |
b2ca4e1c SF |
1494 | if (!ret) |
1495 | ret = bpf_ctx_finish(kattr, uattr, user_ctx, | |
1496 | sizeof(struct bpf_flow_keys)); | |
b7a1848e | 1497 | |
a439184d | 1498 | out: |
b2ca4e1c | 1499 | kfree(user_ctx); |
7b8a1304 | 1500 | kfree(data); |
b7a1848e SF |
1501 | return ret; |
1502 | } | |
7c32e8f8 LB |
1503 | |
1504 | int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr, | |
1505 | union bpf_attr __user *uattr) | |
1506 | { | |
1507 | struct bpf_test_timer t = { NO_PREEMPT }; | |
1508 | struct bpf_prog_array *progs = NULL; | |
1509 | struct bpf_sk_lookup_kern ctx = {}; | |
1510 | u32 repeat = kattr->test.repeat; | |
1511 | struct bpf_sk_lookup *user_ctx; | |
1512 | u32 retval, duration; | |
1513 | int ret = -EINVAL; | |
1514 | ||
b530e9e1 | 1515 | if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) |
7c32e8f8 LB |
1516 | return -EINVAL; |
1517 | ||
1518 | if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || | |
1519 | kattr->test.data_size_out) | |
1520 | return -EINVAL; | |
1521 | ||
1522 | if (!repeat) | |
1523 | repeat = 1; | |
1524 | ||
1525 | user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); | |
1526 | if (IS_ERR(user_ctx)) | |
1527 | return PTR_ERR(user_ctx); | |
1528 | ||
1529 | if (!user_ctx) | |
1530 | return -EINVAL; | |
1531 | ||
1532 | if (user_ctx->sk) | |
1533 | goto out; | |
1534 | ||
1535 | if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) | |
1536 | goto out; | |
1537 | ||
9a69e2b3 | 1538 | if (user_ctx->local_port > U16_MAX) { |
7c32e8f8 LB |
1539 | ret = -ERANGE; |
1540 | goto out; | |
1541 | } | |
1542 | ||
1543 | ctx.family = (u16)user_ctx->family; | |
1544 | ctx.protocol = (u16)user_ctx->protocol; | |
1545 | ctx.dport = (u16)user_ctx->local_port; | |
9a69e2b3 | 1546 | ctx.sport = user_ctx->remote_port; |
7c32e8f8 LB |
1547 | |
1548 | switch (ctx.family) { | |
1549 | case AF_INET: | |
1550 | ctx.v4.daddr = (__force __be32)user_ctx->local_ip4; | |
1551 | ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4; | |
1552 | break; | |
1553 | ||
1554 | #if IS_ENABLED(CONFIG_IPV6) | |
1555 | case AF_INET6: | |
1556 | ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; | |
1557 | ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; | |
1558 | break; | |
1559 | #endif | |
1560 | ||
1561 | default: | |
1562 | ret = -EAFNOSUPPORT; | |
1563 | goto out; | |
1564 | } | |
1565 | ||
1566 | progs = bpf_prog_array_alloc(1, GFP_KERNEL); | |
1567 | if (!progs) { | |
1568 | ret = -ENOMEM; | |
1569 | goto out; | |
1570 | } | |
1571 | ||
1572 | progs->items[0].prog = prog; | |
1573 | ||
1574 | bpf_test_timer_enter(&t); | |
1575 | do { | |
1576 | ctx.selected_sk = NULL; | |
fb7dd8bc | 1577 | retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run); |
b530e9e1 | 1578 | } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); |
7c32e8f8 LB |
1579 | bpf_test_timer_leave(&t); |
1580 | ||
1581 | if (ret < 0) | |
1582 | goto out; | |
1583 | ||
1584 | user_ctx->cookie = 0; | |
1585 | if (ctx.selected_sk) { | |
1586 | if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { | |
1587 | ret = -EOPNOTSUPP; | |
1588 | goto out; | |
1589 | } | |
1590 | ||
1591 | user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); | |
1592 | } | |
1593 | ||
7855e0db | 1594 | ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration); |
7c32e8f8 LB |
1595 | if (!ret) |
1596 | ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); | |
1597 | ||
1598 | out: | |
1599 | bpf_prog_array_free(progs); | |
1600 | kfree(user_ctx); | |
1601 | return ret; | |
1602 | } | |
79a7f8bd AS |
1603 | |
1604 | int bpf_prog_test_run_syscall(struct bpf_prog *prog, | |
1605 | const union bpf_attr *kattr, | |
1606 | union bpf_attr __user *uattr) | |
1607 | { | |
1608 | void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); | |
1609 | __u32 ctx_size_in = kattr->test.ctx_size_in; | |
1610 | void *ctx = NULL; | |
1611 | u32 retval; | |
1612 | int err = 0; | |
1613 | ||
1614 | /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ | |
1615 | if (kattr->test.data_in || kattr->test.data_out || | |
1616 | kattr->test.ctx_out || kattr->test.duration || | |
b530e9e1 THJ |
1617 | kattr->test.repeat || kattr->test.flags || |
1618 | kattr->test.batch_size) | |
79a7f8bd AS |
1619 | return -EINVAL; |
1620 | ||
1621 | if (ctx_size_in < prog->aux->max_ctx_offset || | |
1622 | ctx_size_in > U16_MAX) | |
1623 | return -EINVAL; | |
1624 | ||
1625 | if (ctx_size_in) { | |
db5b6a46 QW |
1626 | ctx = memdup_user(ctx_in, ctx_size_in); |
1627 | if (IS_ERR(ctx)) | |
1628 | return PTR_ERR(ctx); | |
79a7f8bd | 1629 | } |
87b7b533 YS |
1630 | |
1631 | rcu_read_lock_trace(); | |
79a7f8bd | 1632 | retval = bpf_prog_run_pin_on_cpu(prog, ctx); |
87b7b533 | 1633 | rcu_read_unlock_trace(); |
79a7f8bd AS |
1634 | |
1635 | if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { | |
1636 | err = -EFAULT; | |
1637 | goto out; | |
1638 | } | |
1639 | if (ctx_size_in) | |
1640 | if (copy_to_user(ctx_in, ctx, ctx_size_in)) | |
1641 | err = -EFAULT; | |
1642 | out: | |
1643 | kfree(ctx); | |
1644 | return err; | |
1645 | } | |
b202d844 KKD |
1646 | |
1647 | static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { | |
a4703e31 KKD |
1648 | .owner = THIS_MODULE, |
1649 | .set = &test_sk_check_kfunc_ids, | |
b202d844 KKD |
1650 | }; |
1651 | ||
05a945de KKD |
1652 | BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids) |
1653 | BTF_ID(struct, prog_test_ref_kfunc) | |
1654 | BTF_ID(func, bpf_kfunc_call_test_release) | |
1655 | BTF_ID(struct, prog_test_member) | |
1656 | BTF_ID(func, bpf_kfunc_call_memb_release) | |
1657 | ||
b202d844 KKD |
1658 | static int __init bpf_prog_test_run_init(void) |
1659 | { | |
05a945de KKD |
1660 | const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = { |
1661 | { | |
1662 | .btf_id = bpf_prog_test_dtor_kfunc_ids[0], | |
1663 | .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1] | |
1664 | }, | |
1665 | { | |
1666 | .btf_id = bpf_prog_test_dtor_kfunc_ids[2], | |
1667 | .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3], | |
1668 | }, | |
1669 | }; | |
1670 | int ret; | |
1671 | ||
1672 | ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); | |
1f075262 | 1673 | ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set); |
fb66223a | 1674 | ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set); |
05a945de KKD |
1675 | return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, |
1676 | ARRAY_SIZE(bpf_prog_test_dtor_kfunc), | |
1677 | THIS_MODULE); | |
b202d844 KKD |
1678 | } |
1679 | late_initcall(bpf_prog_test_run_init); |