1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
6 #include <linux/btf_ids.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/vmalloc.h>
10 #include <linux/etherdevice.h>
11 #include <linux/filter.h>
12 #include <linux/rcupdate_trace.h>
13 #include <linux/sched/signal.h>
14 #include <net/bpf_sk_storage.h>
17 #include <net/net_namespace.h>
18 #include <net/page_pool.h>
19 #include <linux/error-injection.h>
20 #include <linux/smp.h>
21 #include <linux/sock_diag.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/bpf_test_run.h>
27 struct bpf_test_timer {
28 enum { NO_PREEMPT, NO_MIGRATE } mode;
30 u64 time_start, time_spent;
33 static void bpf_test_timer_enter(struct bpf_test_timer *t)
37 if (t->mode == NO_PREEMPT)
42 t->time_start = ktime_get_ns();
45 static void bpf_test_timer_leave(struct bpf_test_timer *t)
50 if (t->mode == NO_PREEMPT)
57 static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
58 u32 repeat, int *err, u32 *duration)
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;
71 if (signal_pending(current)) {
72 /* During iteration: we've been cancelled, abort. */
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);
82 bpf_test_timer_enter(t);
85 /* Do another round. */
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.
97 struct xdp_page_head {
98 struct xdp_buff orig_ctx;
101 /* ::data_hard_start starts here */
102 DECLARE_FLEX_ARRAY(struct xdp_frame, frame);
103 DECLARE_FLEX_ARRAY(u8, data);
107 struct xdp_test_data {
108 struct xdp_buff *orig_ctx;
109 struct xdp_rxq_info rxq;
110 struct net_device *dev;
111 struct page_pool *pp;
112 struct xdp_frame **frames;
113 struct sk_buff **skbs;
114 struct xdp_mem_info mem;
119 #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head))
120 #define TEST_XDP_MAX_BATCH 256
122 #if BITS_PER_LONG == 64 && PAGE_SIZE == SZ_4K
123 /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE
124 * must be updated accordingly when any of these changes, otherwise BPF
125 * selftests will fail.
128 #define TEST_MAX_PKT_SIZE 3216
130 #define TEST_MAX_PKT_SIZE 3408
132 static_assert(SKB_WITH_OVERHEAD(TEST_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM) ==
136 static void xdp_test_run_init_page(struct page *page, void *arg)
138 struct xdp_page_head *head = phys_to_virt(page_to_phys(page));
139 struct xdp_buff *new_ctx, *orig_ctx;
140 u32 headroom = XDP_PACKET_HEADROOM;
141 struct xdp_test_data *xdp = arg;
142 size_t frm_len, meta_len;
143 struct xdp_frame *frm;
146 orig_ctx = xdp->orig_ctx;
147 frm_len = orig_ctx->data_end - orig_ctx->data_meta;
148 meta_len = orig_ctx->data - orig_ctx->data_meta;
149 headroom -= meta_len;
151 new_ctx = &head->ctx;
154 memcpy(data + headroom, orig_ctx->data_meta, frm_len);
156 xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq);
157 xdp_prepare_buff(new_ctx, data, headroom, frm_len, true);
158 new_ctx->data = new_ctx->data_meta + meta_len;
160 xdp_update_frame_from_buff(new_ctx, frm);
161 frm->mem = new_ctx->rxq->mem;
163 memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx));
166 static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx)
168 struct page_pool *pp;
170 struct page_pool_params pp_params = {
173 .pool_size = xdp->batch_size,
175 .init_callback = xdp_test_run_init_page,
179 xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
183 xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
187 pp = page_pool_create(&pp_params);
193 /* will copy 'mem.id' into pp->xdp_mem_id */
194 err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp);
200 /* We create a 'fake' RXQ referencing the original dev, but with an
201 * xdp_mem_info pointing to our page_pool
203 xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0);
204 xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL;
205 xdp->rxq.mem.id = pp->xdp_mem_id;
206 xdp->dev = orig_ctx->rxq->dev;
207 xdp->orig_ctx = orig_ctx;
212 page_pool_destroy(pp);
220 static void xdp_test_run_teardown(struct xdp_test_data *xdp)
222 xdp_unreg_mem_model(&xdp->mem);
223 page_pool_destroy(xdp->pp);
228 static bool ctx_was_changed(struct xdp_page_head *head)
230 return head->orig_ctx.data != head->ctx.data ||
231 head->orig_ctx.data_meta != head->ctx.data_meta ||
232 head->orig_ctx.data_end != head->ctx.data_end;
235 static void reset_ctx(struct xdp_page_head *head)
237 if (likely(!ctx_was_changed(head)))
240 head->ctx.data = head->orig_ctx.data;
241 head->ctx.data_meta = head->orig_ctx.data_meta;
242 head->ctx.data_end = head->orig_ctx.data_end;
243 xdp_update_frame_from_buff(&head->ctx, head->frame);
246 static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
247 struct sk_buff **skbs,
248 struct net_device *dev)
250 gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
254 n = kmem_cache_alloc_bulk(skbuff_cache, gfp, nframes, (void **)skbs);
255 if (unlikely(n == 0)) {
256 for (i = 0; i < nframes; i++)
257 xdp_return_frame(frames[i]);
261 for (i = 0; i < nframes; i++) {
262 struct xdp_frame *xdpf = frames[i];
263 struct sk_buff *skb = skbs[i];
265 skb = __xdp_build_skb_from_frame(xdpf, skb, dev);
267 xdp_return_frame(xdpf);
271 list_add_tail(&skb->list, &list);
273 netif_receive_skb_list(&list);
278 static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog,
281 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
282 int err = 0, act, ret, i, nframes = 0, batch_sz;
283 struct xdp_frame **frames = xdp->frames;
284 struct xdp_page_head *head;
285 struct xdp_frame *frm;
286 bool redirect = false;
287 struct xdp_buff *ctx;
290 batch_sz = min_t(u32, repeat, xdp->batch_size);
293 xdp_set_return_frame_no_direct();
295 for (i = 0; i < batch_sz; i++) {
296 page = page_pool_dev_alloc_pages(xdp->pp);
302 head = phys_to_virt(page_to_phys(page));
308 act = bpf_prog_run_xdp(prog, ctx);
310 /* if program changed pkt bounds we need to update the xdp_frame */
311 if (unlikely(ctx_was_changed(head))) {
312 ret = xdp_update_frame_from_buff(ctx, frm);
314 xdp_return_buff(ctx);
321 /* we can't do a real XDP_TX since we're not in the
322 * driver, so turn it into a REDIRECT back to the same
325 ri->tgt_index = xdp->dev->ifindex;
326 ri->map_id = INT_MAX;
327 ri->map_type = BPF_MAP_TYPE_UNSPEC;
331 ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog);
333 xdp_return_buff(ctx);
336 frames[nframes++] = frm;
339 bpf_warn_invalid_xdp_action(NULL, prog, act);
342 xdp_return_buff(ctx);
351 ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev);
356 xdp_clear_return_frame_no_direct();
361 static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx,
362 u32 repeat, u32 batch_size, u32 *time)
365 struct xdp_test_data xdp = { .batch_size = batch_size };
366 struct bpf_test_timer t = { .mode = NO_MIGRATE };
372 ret = xdp_test_run_setup(&xdp, ctx);
376 bpf_test_timer_enter(&t);
379 ret = xdp_test_run_batch(&xdp, prog, repeat - t.i);
380 if (unlikely(ret < 0))
382 } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time));
383 bpf_test_timer_leave(&t);
385 xdp_test_run_teardown(&xdp);
389 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
390 u32 *retval, u32 *time, bool xdp)
392 struct bpf_prog_array_item item = {.prog = prog};
393 struct bpf_run_ctx *old_ctx;
394 struct bpf_cg_run_ctx run_ctx;
395 struct bpf_test_timer t = { NO_MIGRATE };
396 enum bpf_cgroup_storage_type stype;
399 for_each_cgroup_storage_type(stype) {
400 item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
401 if (IS_ERR(item.cgroup_storage[stype])) {
402 item.cgroup_storage[stype] = NULL;
403 for_each_cgroup_storage_type(stype)
404 bpf_cgroup_storage_free(item.cgroup_storage[stype]);
412 bpf_test_timer_enter(&t);
413 old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
415 run_ctx.prog_item = &item;
418 *retval = bpf_prog_run_xdp(prog, ctx);
420 *retval = bpf_prog_run(prog, ctx);
422 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time));
423 bpf_reset_run_ctx(old_ctx);
424 bpf_test_timer_leave(&t);
426 for_each_cgroup_storage_type(stype)
427 bpf_cgroup_storage_free(item.cgroup_storage[stype]);
432 static int bpf_test_finish(const union bpf_attr *kattr,
433 union bpf_attr __user *uattr, const void *data,
434 struct skb_shared_info *sinfo, u32 size,
435 u32 retval, u32 duration)
437 void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
439 u32 copy_size = size;
441 /* Clamp copy if the user has provided a size hint, but copy the full
442 * buffer if not to retain old behaviour.
444 if (kattr->test.data_size_out &&
445 copy_size > kattr->test.data_size_out) {
446 copy_size = kattr->test.data_size_out;
451 int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size;
458 if (copy_to_user(data_out, data, len))
465 for (i = 0; i < sinfo->nr_frags; i++) {
466 skb_frag_t *frag = &sinfo->frags[i];
468 if (offset >= copy_size) {
473 data_len = min_t(u32, copy_size - offset,
474 skb_frag_size(frag));
476 if (copy_to_user(data_out + offset,
477 skb_frag_address(frag),
486 if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
488 if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
490 if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
495 trace_bpf_test_finish(&err);
499 /* Integer types of various sizes and pointer combinations cover variety of
500 * architecture dependent calling conventions. 7+ can be supported in the
504 __diag_ignore_all("-Wmissing-prototypes",
505 "Global functions as their definitions will be in vmlinux BTF");
506 __bpf_kfunc int bpf_fentry_test1(int a)
510 EXPORT_SYMBOL_GPL(bpf_fentry_test1);
512 int noinline bpf_fentry_test2(int a, u64 b)
517 int noinline bpf_fentry_test3(char a, int b, u64 c)
522 int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
524 return (long)a + b + c + d;
527 int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
529 return a + (long)b + c + d + e;
532 int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
534 return a + (long)b + c + d + (long)e + f;
537 struct bpf_fentry_test_t {
538 struct bpf_fentry_test_t *a;
541 int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
546 int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
551 __bpf_kfunc int bpf_modify_return_test(int a, int *b)
557 __bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
559 return a + b + c + d;
562 __bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
567 __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk)
572 long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d)
574 /* Provoke the compiler to assume that the caller has sign-extended a,
575 * b and c on platforms where this is required (e.g. s390x).
577 return (long)a + (long)b + (long)c + d;
580 struct prog_test_member1 {
584 struct prog_test_member {
585 struct prog_test_member1 m;
589 struct prog_test_ref_kfunc {
592 struct prog_test_member memb;
593 struct prog_test_ref_kfunc *next;
597 static struct prog_test_ref_kfunc prog_test_struct = {
600 .next = &prog_test_struct,
601 .cnt = REFCOUNT_INIT(1),
604 __bpf_kfunc struct prog_test_ref_kfunc *
605 bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
607 refcount_inc(&prog_test_struct.cnt);
608 return &prog_test_struct;
611 __bpf_kfunc struct prog_test_member *
612 bpf_kfunc_call_memb_acquire(void)
618 __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p)
623 refcount_dec(&p->cnt);
626 __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p)
630 __bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
635 static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size)
637 if (size > 2 * sizeof(int))
643 __bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p,
644 const int rdwr_buf_size)
646 return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size);
649 __bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p,
650 const int rdonly_buf_size)
652 return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
655 /* the next 2 ones can't be really used for testing expect to ensure
656 * that the verifier rejects the call.
657 * Acquire functions must return struct pointers, so these ones are
660 __bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p,
661 const int rdonly_buf_size)
663 return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
666 __bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p)
670 __bpf_kfunc struct prog_test_ref_kfunc *
671 bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **pp, int a, int b)
673 struct prog_test_ref_kfunc *p = READ_ONCE(*pp);
677 refcount_inc(&p->cnt);
681 struct prog_test_pass1 {
694 struct prog_test_pass2 {
699 unsigned long arr3[8];
703 struct prog_test_fail1 {
708 struct prog_test_fail2 {
710 struct prog_test_pass1 x;
713 struct prog_test_fail3 {
719 __bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb)
723 __bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p)
727 __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
731 __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)
735 __bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p)
739 __bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p)
743 __bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz)
747 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len)
751 __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
755 __bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
759 __bpf_kfunc void bpf_kfunc_call_test_destructive(void)
763 __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused)
770 BTF_SET8_START(bpf_test_modify_return_ids)
771 BTF_ID_FLAGS(func, bpf_modify_return_test)
772 BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE)
773 BTF_SET8_END(bpf_test_modify_return_ids)
775 static const struct btf_kfunc_id_set bpf_test_modify_return_set = {
776 .owner = THIS_MODULE,
777 .set = &bpf_test_modify_return_ids,
780 BTF_SET8_START(test_sk_check_kfunc_ids)
781 BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
782 BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
783 BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
784 BTF_ID_FLAGS(func, bpf_kfunc_call_test4)
785 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
786 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
787 BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE)
788 BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE)
789 BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
790 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL)
791 BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL)
792 BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL)
793 BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)
794 BTF_ID_FLAGS(func, bpf_kfunc_call_test_kptr_get, KF_ACQUIRE | KF_RET_NULL | KF_KPTR_GET)
795 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
796 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
797 BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
798 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
799 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
800 BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
801 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
802 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
803 BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
804 BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS)
805 BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
806 BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
807 BTF_SET8_END(test_sk_check_kfunc_ids)
809 static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
810 u32 size, u32 headroom, u32 tailroom)
812 void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
815 if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
816 return ERR_PTR(-EINVAL);
818 if (user_size > size)
819 return ERR_PTR(-EMSGSIZE);
821 size = SKB_DATA_ALIGN(size);
822 data = kzalloc(size + headroom + tailroom, GFP_USER);
824 return ERR_PTR(-ENOMEM);
826 if (copy_from_user(data + headroom, data_in, user_size)) {
828 return ERR_PTR(-EFAULT);
834 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
835 const union bpf_attr *kattr,
836 union bpf_attr __user *uattr)
838 struct bpf_fentry_test_t arg = {};
839 u16 side_effect = 0, ret = 0;
840 int b = 2, err = -EFAULT;
843 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
846 switch (prog->expected_attach_type) {
847 case BPF_TRACE_FENTRY:
848 case BPF_TRACE_FEXIT:
849 if (bpf_fentry_test1(1) != 2 ||
850 bpf_fentry_test2(2, 3) != 5 ||
851 bpf_fentry_test3(4, 5, 6) != 15 ||
852 bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
853 bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
854 bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
855 bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
856 bpf_fentry_test8(&arg) != 0)
859 case BPF_MODIFY_RETURN:
860 ret = bpf_modify_return_test(1, &b);
868 retval = ((u32)side_effect << 16) | ret;
869 if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
874 trace_bpf_test_finish(&err);
878 struct bpf_raw_tp_test_run_info {
879 struct bpf_prog *prog;
885 __bpf_prog_test_run_raw_tp(void *data)
887 struct bpf_raw_tp_test_run_info *info = data;
890 info->retval = bpf_prog_run(info->prog, info->ctx);
894 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
895 const union bpf_attr *kattr,
896 union bpf_attr __user *uattr)
898 void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
899 __u32 ctx_size_in = kattr->test.ctx_size_in;
900 struct bpf_raw_tp_test_run_info info;
901 int cpu = kattr->test.cpu, err = 0;
904 /* doesn't support data_in/out, ctx_out, duration, or repeat */
905 if (kattr->test.data_in || kattr->test.data_out ||
906 kattr->test.ctx_out || kattr->test.duration ||
907 kattr->test.repeat || kattr->test.batch_size)
910 if (ctx_size_in < prog->aux->max_ctx_offset ||
911 ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64))
914 if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0)
918 info.ctx = memdup_user(ctx_in, ctx_size_in);
919 if (IS_ERR(info.ctx))
920 return PTR_ERR(info.ctx);
927 current_cpu = get_cpu();
928 if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
929 cpu == current_cpu) {
930 __bpf_prog_test_run_raw_tp(&info);
931 } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
932 /* smp_call_function_single() also checks cpu_online()
933 * after csd_lock(). However, since cpu is from user
934 * space, let's do an extra quick check to filter out
935 * invalid value before smp_call_function_single().
939 err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
945 copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
952 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
954 void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
955 void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
956 u32 size = kattr->test.ctx_size_in;
960 if (!data_in && !data_out)
963 data = kzalloc(max_size, GFP_USER);
965 return ERR_PTR(-ENOMEM);
968 err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size);
974 size = min_t(u32, max_size, size);
975 if (copy_from_user(data, data_in, size)) {
977 return ERR_PTR(-EFAULT);
983 static int bpf_ctx_finish(const union bpf_attr *kattr,
984 union bpf_attr __user *uattr, const void *data,
987 void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
989 u32 copy_size = size;
991 if (!data || !data_out)
994 if (copy_size > kattr->test.ctx_size_out) {
995 copy_size = kattr->test.ctx_size_out;
999 if (copy_to_user(data_out, data, copy_size))
1001 if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
1010 * range_is_zero - test whether buffer is initialized
1011 * @buf: buffer to check
1012 * @from: check from this position
1013 * @to: check up until (excluding) this position
1015 * This function returns true if the there is a non-zero byte
1016 * in the buf in the range [from,to).
1018 static inline bool range_is_zero(void *buf, size_t from, size_t to)
1020 return !memchr_inv((u8 *)buf + from, 0, to - from);
1023 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
1025 struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
1030 /* make sure the fields we don't use are zeroed */
1031 if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
1034 /* mark is allowed */
1036 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
1037 offsetof(struct __sk_buff, priority)))
1040 /* priority is allowed */
1041 /* ingress_ifindex is allowed */
1042 /* ifindex is allowed */
1044 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
1045 offsetof(struct __sk_buff, cb)))
1050 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
1051 offsetof(struct __sk_buff, tstamp)))
1054 /* tstamp is allowed */
1055 /* wire_len is allowed */
1056 /* gso_segs is allowed */
1058 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
1059 offsetof(struct __sk_buff, gso_size)))
1062 /* gso_size is allowed */
1064 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
1065 offsetof(struct __sk_buff, hwtstamp)))
1068 /* hwtstamp is allowed */
1070 if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp),
1071 sizeof(struct __sk_buff)))
1074 skb->mark = __skb->mark;
1075 skb->priority = __skb->priority;
1076 skb->skb_iif = __skb->ingress_ifindex;
1077 skb->tstamp = __skb->tstamp;
1078 memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
1080 if (__skb->wire_len == 0) {
1081 cb->pkt_len = skb->len;
1083 if (__skb->wire_len < skb->len ||
1084 __skb->wire_len > GSO_LEGACY_MAX_SIZE)
1086 cb->pkt_len = __skb->wire_len;
1089 if (__skb->gso_segs > GSO_MAX_SEGS)
1091 skb_shinfo(skb)->gso_segs = __skb->gso_segs;
1092 skb_shinfo(skb)->gso_size = __skb->gso_size;
1093 skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp;
1098 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
1100 struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
1105 __skb->mark = skb->mark;
1106 __skb->priority = skb->priority;
1107 __skb->ingress_ifindex = skb->skb_iif;
1108 __skb->ifindex = skb->dev->ifindex;
1109 __skb->tstamp = skb->tstamp;
1110 memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
1111 __skb->wire_len = cb->pkt_len;
1112 __skb->gso_segs = skb_shinfo(skb)->gso_segs;
1113 __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp;
1116 static struct proto bpf_dummy_proto = {
1117 .name = "bpf_dummy",
1118 .owner = THIS_MODULE,
1119 .obj_size = sizeof(struct sock),
1122 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1123 union bpf_attr __user *uattr)
1125 bool is_l2 = false, is_direct_pkt_access = false;
1126 struct net *net = current->nsproxy->net_ns;
1127 struct net_device *dev = net->loopback_dev;
1128 u32 size = kattr->test.data_size_in;
1129 u32 repeat = kattr->test.repeat;
1130 struct __sk_buff *ctx = NULL;
1131 u32 retval, duration;
1132 int hh_len = ETH_HLEN;
1133 struct sk_buff *skb;
1138 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1141 data = bpf_test_init(kattr, kattr->test.data_size_in,
1142 size, NET_SKB_PAD + NET_IP_ALIGN,
1143 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1145 return PTR_ERR(data);
1147 ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
1150 return PTR_ERR(ctx);
1153 switch (prog->type) {
1154 case BPF_PROG_TYPE_SCHED_CLS:
1155 case BPF_PROG_TYPE_SCHED_ACT:
1158 case BPF_PROG_TYPE_LWT_IN:
1159 case BPF_PROG_TYPE_LWT_OUT:
1160 case BPF_PROG_TYPE_LWT_XMIT:
1161 is_direct_pkt_access = true;
1167 sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
1173 sock_init_data(NULL, sk);
1175 skb = slab_build_skb(data);
1184 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1185 __skb_put(skb, size);
1186 if (ctx && ctx->ifindex > 1) {
1187 dev = dev_get_by_index(net, ctx->ifindex);
1193 skb->protocol = eth_type_trans(skb, dev);
1194 skb_reset_network_header(skb);
1196 switch (skb->protocol) {
1197 case htons(ETH_P_IP):
1198 sk->sk_family = AF_INET;
1199 if (sizeof(struct iphdr) <= skb_headlen(skb)) {
1200 sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
1201 sk->sk_daddr = ip_hdr(skb)->daddr;
1204 #if IS_ENABLED(CONFIG_IPV6)
1205 case htons(ETH_P_IPV6):
1206 sk->sk_family = AF_INET6;
1207 if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
1208 sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
1209 sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
1218 __skb_push(skb, hh_len);
1219 if (is_direct_pkt_access)
1220 bpf_compute_data_pointers(skb);
1221 ret = convert___skb_to_skb(skb, ctx);
1224 ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
1228 if (skb_headroom(skb) < hh_len) {
1229 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
1231 if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
1236 memset(__skb_push(skb, hh_len), 0, hh_len);
1238 convert_skb_to___skb(skb, ctx);
1241 /* bpf program can never convert linear skb to non-linear */
1242 if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
1243 size = skb_headlen(skb);
1244 ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval,
1247 ret = bpf_ctx_finish(kattr, uattr, ctx,
1248 sizeof(struct __sk_buff));
1250 if (dev && dev != net->loopback_dev)
1258 static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp)
1260 unsigned int ingress_ifindex, rx_queue_index;
1261 struct netdev_rx_queue *rxqueue;
1262 struct net_device *device;
1267 if (xdp_md->egress_ifindex != 0)
1270 ingress_ifindex = xdp_md->ingress_ifindex;
1271 rx_queue_index = xdp_md->rx_queue_index;
1273 if (!ingress_ifindex && rx_queue_index)
1276 if (ingress_ifindex) {
1277 device = dev_get_by_index(current->nsproxy->net_ns,
1282 if (rx_queue_index >= device->real_num_rx_queues)
1285 rxqueue = __netif_get_rx_queue(device, rx_queue_index);
1287 if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq))
1290 xdp->rxq = &rxqueue->xdp_rxq;
1291 /* The device is now tracked in the xdp->rxq for later
1296 xdp->data = xdp->data_meta + xdp_md->data;
1304 static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md)
1309 xdp_md->data = xdp->data - xdp->data_meta;
1310 xdp_md->data_end = xdp->data_end - xdp->data_meta;
1312 if (xdp_md->ingress_ifindex)
1313 dev_put(xdp->rxq->dev);
1316 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1317 union bpf_attr __user *uattr)
1319 bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
1320 u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1321 u32 batch_size = kattr->test.batch_size;
1322 u32 retval = 0, duration, max_data_sz;
1323 u32 size = kattr->test.data_size_in;
1324 u32 headroom = XDP_PACKET_HEADROOM;
1325 u32 repeat = kattr->test.repeat;
1326 struct netdev_rx_queue *rxqueue;
1327 struct skb_shared_info *sinfo;
1328 struct xdp_buff xdp = {};
1329 int i, ret = -EINVAL;
1333 if (prog->expected_attach_type == BPF_XDP_DEVMAP ||
1334 prog->expected_attach_type == BPF_XDP_CPUMAP)
1337 if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES)
1340 if (bpf_prog_is_dev_bound(prog->aux))
1345 batch_size = NAPI_POLL_WEIGHT;
1346 else if (batch_size > TEST_XDP_MAX_BATCH)
1349 headroom += sizeof(struct xdp_page_head);
1350 } else if (batch_size) {
1354 ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md));
1356 return PTR_ERR(ctx);
1359 /* There can't be user provided data before the meta data */
1360 if (ctx->data_meta || ctx->data_end != size ||
1361 ctx->data > ctx->data_end ||
1362 unlikely(xdp_metalen_invalid(ctx->data)) ||
1363 (do_live && (kattr->test.data_out || kattr->test.ctx_out)))
1365 /* Meta data is allocated from the headroom */
1366 headroom -= ctx->data;
1369 max_data_sz = 4096 - headroom - tailroom;
1370 if (size > max_data_sz) {
1371 /* disallow live data mode for jumbo frames */
1377 data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
1379 ret = PTR_ERR(data);
1383 rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
1384 rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
1385 xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
1386 xdp_prepare_buff(&xdp, data, headroom, size, true);
1387 sinfo = xdp_get_shared_info_from_buff(&xdp);
1389 ret = xdp_convert_md_to_buff(ctx, &xdp);
1393 if (unlikely(kattr->test.data_size_in > size)) {
1394 void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
1396 while (size < kattr->test.data_size_in) {
1401 if (sinfo->nr_frags == MAX_SKB_FRAGS) {
1406 page = alloc_page(GFP_KERNEL);
1412 frag = &sinfo->frags[sinfo->nr_frags++];
1413 __skb_frag_set_page(frag, page);
1415 data_len = min_t(u32, kattr->test.data_size_in - size,
1417 skb_frag_size_set(frag, data_len);
1419 if (copy_from_user(page_address(page), data_in + size,
1424 sinfo->xdp_frags_size += data_len;
1427 xdp_buff_set_frags_flag(&xdp);
1431 bpf_prog_change_xdp(NULL, prog);
1434 ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration);
1436 ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
1437 /* We convert the xdp_buff back to an xdp_md before checking the return
1438 * code so the reference count of any held netdevice will be decremented
1439 * even if the test run failed.
1441 xdp_convert_buff_to_md(&xdp, ctx);
1445 size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size;
1446 ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size,
1449 ret = bpf_ctx_finish(kattr, uattr, ctx,
1450 sizeof(struct xdp_md));
1454 bpf_prog_change_xdp(prog, NULL);
1456 for (i = 0; i < sinfo->nr_frags; i++)
1457 __free_page(skb_frag_page(&sinfo->frags[i]));
1464 static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
1466 /* make sure the fields we don't use are zeroed */
1467 if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
1470 /* flags is allowed */
1472 if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
1473 sizeof(struct bpf_flow_keys)))
1479 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1480 const union bpf_attr *kattr,
1481 union bpf_attr __user *uattr)
1483 struct bpf_test_timer t = { NO_PREEMPT };
1484 u32 size = kattr->test.data_size_in;
1485 struct bpf_flow_dissector ctx = {};
1486 u32 repeat = kattr->test.repeat;
1487 struct bpf_flow_keys *user_ctx;
1488 struct bpf_flow_keys flow_keys;
1489 const struct ethhdr *eth;
1490 unsigned int flags = 0;
1491 u32 retval, duration;
1495 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1498 if (size < ETH_HLEN)
1501 data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0);
1503 return PTR_ERR(data);
1505 eth = (struct ethhdr *)data;
1510 user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
1511 if (IS_ERR(user_ctx)) {
1513 return PTR_ERR(user_ctx);
1516 ret = verify_user_bpf_flow_keys(user_ctx);
1519 flags = user_ctx->flags;
1522 ctx.flow_keys = &flow_keys;
1524 ctx.data_end = (__u8 *)data + size;
1526 bpf_test_timer_enter(&t);
1528 retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
1530 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1531 bpf_test_timer_leave(&t);
1536 ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
1537 sizeof(flow_keys), retval, duration);
1539 ret = bpf_ctx_finish(kattr, uattr, user_ctx,
1540 sizeof(struct bpf_flow_keys));
1548 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
1549 union bpf_attr __user *uattr)
1551 struct bpf_test_timer t = { NO_PREEMPT };
1552 struct bpf_prog_array *progs = NULL;
1553 struct bpf_sk_lookup_kern ctx = {};
1554 u32 repeat = kattr->test.repeat;
1555 struct bpf_sk_lookup *user_ctx;
1556 u32 retval, duration;
1559 if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
1562 if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out ||
1563 kattr->test.data_size_out)
1569 user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx));
1570 if (IS_ERR(user_ctx))
1571 return PTR_ERR(user_ctx);
1579 if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
1582 if (user_ctx->local_port > U16_MAX) {
1587 ctx.family = (u16)user_ctx->family;
1588 ctx.protocol = (u16)user_ctx->protocol;
1589 ctx.dport = (u16)user_ctx->local_port;
1590 ctx.sport = user_ctx->remote_port;
1592 switch (ctx.family) {
1594 ctx.v4.daddr = (__force __be32)user_ctx->local_ip4;
1595 ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4;
1598 #if IS_ENABLED(CONFIG_IPV6)
1600 ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6;
1601 ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6;
1606 ret = -EAFNOSUPPORT;
1610 progs = bpf_prog_array_alloc(1, GFP_KERNEL);
1616 progs->items[0].prog = prog;
1618 bpf_test_timer_enter(&t);
1620 ctx.selected_sk = NULL;
1621 retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run);
1622 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1623 bpf_test_timer_leave(&t);
1628 user_ctx->cookie = 0;
1629 if (ctx.selected_sk) {
1630 if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) {
1635 user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
1638 ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
1640 ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
1643 bpf_prog_array_free(progs);
1648 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
1649 const union bpf_attr *kattr,
1650 union bpf_attr __user *uattr)
1652 void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
1653 __u32 ctx_size_in = kattr->test.ctx_size_in;
1658 /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */
1659 if (kattr->test.data_in || kattr->test.data_out ||
1660 kattr->test.ctx_out || kattr->test.duration ||
1661 kattr->test.repeat || kattr->test.flags ||
1662 kattr->test.batch_size)
1665 if (ctx_size_in < prog->aux->max_ctx_offset ||
1666 ctx_size_in > U16_MAX)
1670 ctx = memdup_user(ctx_in, ctx_size_in);
1672 return PTR_ERR(ctx);
1675 rcu_read_lock_trace();
1676 retval = bpf_prog_run_pin_on_cpu(prog, ctx);
1677 rcu_read_unlock_trace();
1679 if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
1684 if (copy_to_user(ctx_in, ctx, ctx_size_in))
1691 static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
1692 .owner = THIS_MODULE,
1693 .set = &test_sk_check_kfunc_ids,
1696 BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
1697 BTF_ID(struct, prog_test_ref_kfunc)
1698 BTF_ID(func, bpf_kfunc_call_test_release)
1699 BTF_ID(struct, prog_test_member)
1700 BTF_ID(func, bpf_kfunc_call_memb_release)
1702 static int __init bpf_prog_test_run_init(void)
1704 const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
1706 .btf_id = bpf_prog_test_dtor_kfunc_ids[0],
1707 .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
1710 .btf_id = bpf_prog_test_dtor_kfunc_ids[2],
1711 .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3],
1716 ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set);
1717 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
1718 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set);
1719 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set);
1720 return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
1721 ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
1724 late_initcall(bpf_prog_test_run_init);