125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21cf1cae9SAlexei Starovoitov /* Copyright (c) 2017 Facebook 31cf1cae9SAlexei Starovoitov */ 41cf1cae9SAlexei Starovoitov #include <linux/bpf.h> 5c48e51c8SKumar Kartikeya Dwivedi #include <linux/btf.h> 67bd1590dSMartin KaFai Lau #include <linux/btf_ids.h> 71cf1cae9SAlexei Starovoitov #include <linux/slab.h> 8b202d844SKumar Kartikeya Dwivedi #include <linux/init.h> 91cf1cae9SAlexei Starovoitov #include <linux/vmalloc.h> 101cf1cae9SAlexei Starovoitov #include <linux/etherdevice.h> 111cf1cae9SAlexei Starovoitov #include <linux/filter.h> 1287b7b533SYonghong Song #include <linux/rcupdate_trace.h> 131cf1cae9SAlexei Starovoitov #include <linux/sched/signal.h> 146ac99e8fSMartin KaFai Lau #include <net/bpf_sk_storage.h> 152cb494a3SSong Liu #include <net/sock.h> 162cb494a3SSong Liu #include <net/tcp.h> 177c32e8f8SLorenz Bauer #include <net/net_namespace.h> 18b530e9e1SToke Høiland-Jørgensen #include <net/page_pool.h> 193d08b6f2SKP Singh #include <linux/error-injection.h> 201b4d60ecSSong Liu #include <linux/smp.h> 217c32e8f8SLorenz Bauer #include <linux/sock_diag.h> 2247316f4aSZvi Effron #include <net/xdp.h> 231cf1cae9SAlexei Starovoitov 24e950e843SMatt Mullins #define CREATE_TRACE_POINTS 25e950e843SMatt Mullins #include <trace/events/bpf_test_run.h> 26e950e843SMatt Mullins 27607b9cc9SLorenz Bauer struct bpf_test_timer { 28607b9cc9SLorenz Bauer enum { NO_PREEMPT, NO_MIGRATE } mode; 29607b9cc9SLorenz Bauer u32 i; 30607b9cc9SLorenz Bauer u64 time_start, time_spent; 31607b9cc9SLorenz Bauer }; 32607b9cc9SLorenz Bauer 33607b9cc9SLorenz Bauer static void bpf_test_timer_enter(struct bpf_test_timer *t) 34607b9cc9SLorenz Bauer __acquires(rcu) 35607b9cc9SLorenz Bauer { 36607b9cc9SLorenz Bauer rcu_read_lock(); 37607b9cc9SLorenz Bauer if (t->mode == NO_PREEMPT) 38607b9cc9SLorenz Bauer preempt_disable(); 39607b9cc9SLorenz Bauer else 40607b9cc9SLorenz Bauer migrate_disable(); 41607b9cc9SLorenz Bauer 42607b9cc9SLorenz Bauer t->time_start = ktime_get_ns(); 43607b9cc9SLorenz Bauer } 44607b9cc9SLorenz Bauer 45607b9cc9SLorenz Bauer static void bpf_test_timer_leave(struct bpf_test_timer *t) 46607b9cc9SLorenz Bauer __releases(rcu) 47607b9cc9SLorenz Bauer { 48607b9cc9SLorenz Bauer t->time_start = 0; 49607b9cc9SLorenz Bauer 50607b9cc9SLorenz Bauer if (t->mode == NO_PREEMPT) 51607b9cc9SLorenz Bauer preempt_enable(); 52607b9cc9SLorenz Bauer else 53607b9cc9SLorenz Bauer migrate_enable(); 54607b9cc9SLorenz Bauer rcu_read_unlock(); 55607b9cc9SLorenz Bauer } 56607b9cc9SLorenz Bauer 57b530e9e1SToke Høiland-Jørgensen static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations, 58b530e9e1SToke Høiland-Jørgensen u32 repeat, int *err, u32 *duration) 59607b9cc9SLorenz Bauer __must_hold(rcu) 60607b9cc9SLorenz Bauer { 61b530e9e1SToke Høiland-Jørgensen t->i += iterations; 62607b9cc9SLorenz Bauer if (t->i >= repeat) { 63607b9cc9SLorenz Bauer /* We're done. */ 64607b9cc9SLorenz Bauer t->time_spent += ktime_get_ns() - t->time_start; 65607b9cc9SLorenz Bauer do_div(t->time_spent, t->i); 66607b9cc9SLorenz Bauer *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent; 67607b9cc9SLorenz Bauer *err = 0; 68607b9cc9SLorenz Bauer goto reset; 69607b9cc9SLorenz Bauer } 70607b9cc9SLorenz Bauer 71607b9cc9SLorenz Bauer if (signal_pending(current)) { 72607b9cc9SLorenz Bauer /* During iteration: we've been cancelled, abort. */ 73607b9cc9SLorenz Bauer *err = -EINTR; 74607b9cc9SLorenz Bauer goto reset; 75607b9cc9SLorenz Bauer } 76607b9cc9SLorenz Bauer 77607b9cc9SLorenz Bauer if (need_resched()) { 78607b9cc9SLorenz Bauer /* During iteration: we need to reschedule between runs. */ 79607b9cc9SLorenz Bauer t->time_spent += ktime_get_ns() - t->time_start; 80607b9cc9SLorenz Bauer bpf_test_timer_leave(t); 81607b9cc9SLorenz Bauer cond_resched(); 82607b9cc9SLorenz Bauer bpf_test_timer_enter(t); 83607b9cc9SLorenz Bauer } 84607b9cc9SLorenz Bauer 85607b9cc9SLorenz Bauer /* Do another round. */ 86607b9cc9SLorenz Bauer return true; 87607b9cc9SLorenz Bauer 88607b9cc9SLorenz Bauer reset: 89607b9cc9SLorenz Bauer t->i = 0; 90607b9cc9SLorenz Bauer return false; 91607b9cc9SLorenz Bauer } 92607b9cc9SLorenz Bauer 93b530e9e1SToke Høiland-Jørgensen /* We put this struct at the head of each page with a context and frame 94b530e9e1SToke Høiland-Jørgensen * initialised when the page is allocated, so we don't have to do this on each 95b530e9e1SToke Høiland-Jørgensen * repetition of the test run. 96b530e9e1SToke Høiland-Jørgensen */ 97b530e9e1SToke Høiland-Jørgensen struct xdp_page_head { 98b530e9e1SToke Høiland-Jørgensen struct xdp_buff orig_ctx; 99b530e9e1SToke Høiland-Jørgensen struct xdp_buff ctx; 1006c20822fSAlexander Lobakin union { 1016c20822fSAlexander Lobakin /* ::data_hard_start starts here */ 1026c20822fSAlexander Lobakin DECLARE_FLEX_ARRAY(struct xdp_frame, frame); 1036c20822fSAlexander Lobakin DECLARE_FLEX_ARRAY(u8, data); 1046c20822fSAlexander Lobakin }; 105b530e9e1SToke Høiland-Jørgensen }; 106b530e9e1SToke Høiland-Jørgensen 107b530e9e1SToke Høiland-Jørgensen struct xdp_test_data { 108b530e9e1SToke Høiland-Jørgensen struct xdp_buff *orig_ctx; 109b530e9e1SToke Høiland-Jørgensen struct xdp_rxq_info rxq; 110b530e9e1SToke Høiland-Jørgensen struct net_device *dev; 111b530e9e1SToke Høiland-Jørgensen struct page_pool *pp; 112b530e9e1SToke Høiland-Jørgensen struct xdp_frame **frames; 113b530e9e1SToke Høiland-Jørgensen struct sk_buff **skbs; 114425d2393SToke Høiland-Jørgensen struct xdp_mem_info mem; 115b530e9e1SToke Høiland-Jørgensen u32 batch_size; 116b530e9e1SToke Høiland-Jørgensen u32 frame_cnt; 117b530e9e1SToke Høiland-Jørgensen }; 118b530e9e1SToke Høiland-Jørgensen 119b6f1f780SToke Høiland-Jørgensen #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head)) 120b530e9e1SToke Høiland-Jørgensen #define TEST_XDP_MAX_BATCH 256 121b530e9e1SToke Høiland-Jørgensen 1226c20822fSAlexander Lobakin #if BITS_PER_LONG == 64 && PAGE_SIZE == SZ_4K 1236c20822fSAlexander Lobakin /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE 1246c20822fSAlexander Lobakin * must be updated accordingly when any of these changes, otherwise BPF 1256c20822fSAlexander Lobakin * selftests will fail. 1266c20822fSAlexander Lobakin */ 1276c20822fSAlexander Lobakin #ifdef __s390x__ 1286c20822fSAlexander Lobakin #define TEST_MAX_PKT_SIZE 3216 1296c20822fSAlexander Lobakin #else 1306c20822fSAlexander Lobakin #define TEST_MAX_PKT_SIZE 3408 1316c20822fSAlexander Lobakin #endif 1326c20822fSAlexander Lobakin static_assert(SKB_WITH_OVERHEAD(TEST_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM) == 1336c20822fSAlexander Lobakin TEST_MAX_PKT_SIZE); 1346c20822fSAlexander Lobakin #endif 1356c20822fSAlexander Lobakin 136b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_init_page(struct page *page, void *arg) 137b530e9e1SToke Høiland-Jørgensen { 138b530e9e1SToke Høiland-Jørgensen struct xdp_page_head *head = phys_to_virt(page_to_phys(page)); 139b530e9e1SToke Høiland-Jørgensen struct xdp_buff *new_ctx, *orig_ctx; 140b530e9e1SToke Høiland-Jørgensen u32 headroom = XDP_PACKET_HEADROOM; 141b530e9e1SToke Høiland-Jørgensen struct xdp_test_data *xdp = arg; 142b530e9e1SToke Høiland-Jørgensen size_t frm_len, meta_len; 143b530e9e1SToke Høiland-Jørgensen struct xdp_frame *frm; 144b530e9e1SToke Høiland-Jørgensen void *data; 145b530e9e1SToke Høiland-Jørgensen 146b530e9e1SToke Høiland-Jørgensen orig_ctx = xdp->orig_ctx; 147b530e9e1SToke Høiland-Jørgensen frm_len = orig_ctx->data_end - orig_ctx->data_meta; 148b530e9e1SToke Høiland-Jørgensen meta_len = orig_ctx->data - orig_ctx->data_meta; 149b530e9e1SToke Høiland-Jørgensen headroom -= meta_len; 150b530e9e1SToke Høiland-Jørgensen 151b530e9e1SToke Høiland-Jørgensen new_ctx = &head->ctx; 1526c20822fSAlexander Lobakin frm = head->frame; 1536c20822fSAlexander Lobakin data = head->data; 154b530e9e1SToke Høiland-Jørgensen memcpy(data + headroom, orig_ctx->data_meta, frm_len); 155b530e9e1SToke Høiland-Jørgensen 156b530e9e1SToke Høiland-Jørgensen xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); 157b530e9e1SToke Høiland-Jørgensen xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); 158b530e9e1SToke Høiland-Jørgensen new_ctx->data = new_ctx->data_meta + meta_len; 159b530e9e1SToke Høiland-Jørgensen 160b530e9e1SToke Høiland-Jørgensen xdp_update_frame_from_buff(new_ctx, frm); 161b530e9e1SToke Høiland-Jørgensen frm->mem = new_ctx->rxq->mem; 162b530e9e1SToke Høiland-Jørgensen 163b530e9e1SToke Høiland-Jørgensen memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); 164b530e9e1SToke Høiland-Jørgensen } 165b530e9e1SToke Høiland-Jørgensen 166b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx) 167b530e9e1SToke Høiland-Jørgensen { 168b530e9e1SToke Høiland-Jørgensen struct page_pool *pp; 169b530e9e1SToke Høiland-Jørgensen int err = -ENOMEM; 170b530e9e1SToke Høiland-Jørgensen struct page_pool_params pp_params = { 171b530e9e1SToke Høiland-Jørgensen .order = 0, 172b530e9e1SToke Høiland-Jørgensen .flags = 0, 173b530e9e1SToke Høiland-Jørgensen .pool_size = xdp->batch_size, 174b530e9e1SToke Høiland-Jørgensen .nid = NUMA_NO_NODE, 175b530e9e1SToke Høiland-Jørgensen .init_callback = xdp_test_run_init_page, 176b530e9e1SToke Høiland-Jørgensen .init_arg = xdp, 177b530e9e1SToke Høiland-Jørgensen }; 178b530e9e1SToke Høiland-Jørgensen 179b530e9e1SToke Høiland-Jørgensen xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); 180b530e9e1SToke Høiland-Jørgensen if (!xdp->frames) 181b530e9e1SToke Høiland-Jørgensen return -ENOMEM; 182b530e9e1SToke Høiland-Jørgensen 183b530e9e1SToke Høiland-Jørgensen xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); 184b530e9e1SToke Høiland-Jørgensen if (!xdp->skbs) 185b530e9e1SToke Høiland-Jørgensen goto err_skbs; 186b530e9e1SToke Høiland-Jørgensen 187b530e9e1SToke Høiland-Jørgensen pp = page_pool_create(&pp_params); 188b530e9e1SToke Høiland-Jørgensen if (IS_ERR(pp)) { 189b530e9e1SToke Høiland-Jørgensen err = PTR_ERR(pp); 190b530e9e1SToke Høiland-Jørgensen goto err_pp; 191b530e9e1SToke Høiland-Jørgensen } 192b530e9e1SToke Høiland-Jørgensen 193b530e9e1SToke Høiland-Jørgensen /* will copy 'mem.id' into pp->xdp_mem_id */ 194425d2393SToke Høiland-Jørgensen err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp); 195b530e9e1SToke Høiland-Jørgensen if (err) 196b530e9e1SToke Høiland-Jørgensen goto err_mmodel; 197b530e9e1SToke Høiland-Jørgensen 198b530e9e1SToke Høiland-Jørgensen xdp->pp = pp; 199b530e9e1SToke Høiland-Jørgensen 200b530e9e1SToke Høiland-Jørgensen /* We create a 'fake' RXQ referencing the original dev, but with an 201b530e9e1SToke Høiland-Jørgensen * xdp_mem_info pointing to our page_pool 202b530e9e1SToke Høiland-Jørgensen */ 203b530e9e1SToke Høiland-Jørgensen xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0); 204b530e9e1SToke Høiland-Jørgensen xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL; 205b530e9e1SToke Høiland-Jørgensen xdp->rxq.mem.id = pp->xdp_mem_id; 206b530e9e1SToke Høiland-Jørgensen xdp->dev = orig_ctx->rxq->dev; 207b530e9e1SToke Høiland-Jørgensen xdp->orig_ctx = orig_ctx; 208b530e9e1SToke Høiland-Jørgensen 209b530e9e1SToke Høiland-Jørgensen return 0; 210b530e9e1SToke Høiland-Jørgensen 211b530e9e1SToke Høiland-Jørgensen err_mmodel: 212b530e9e1SToke Høiland-Jørgensen page_pool_destroy(pp); 213b530e9e1SToke Høiland-Jørgensen err_pp: 214743bec1bSYihao Han kvfree(xdp->skbs); 215b530e9e1SToke Høiland-Jørgensen err_skbs: 216743bec1bSYihao Han kvfree(xdp->frames); 217b530e9e1SToke Høiland-Jørgensen return err; 218b530e9e1SToke Høiland-Jørgensen } 219b530e9e1SToke Høiland-Jørgensen 220b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_teardown(struct xdp_test_data *xdp) 221b530e9e1SToke Høiland-Jørgensen { 222425d2393SToke Høiland-Jørgensen xdp_unreg_mem_model(&xdp->mem); 223b530e9e1SToke Høiland-Jørgensen page_pool_destroy(xdp->pp); 224b530e9e1SToke Høiland-Jørgensen kfree(xdp->frames); 225b530e9e1SToke Høiland-Jørgensen kfree(xdp->skbs); 226b530e9e1SToke Høiland-Jørgensen } 227b530e9e1SToke Høiland-Jørgensen 228b530e9e1SToke Høiland-Jørgensen static bool ctx_was_changed(struct xdp_page_head *head) 229b530e9e1SToke Høiland-Jørgensen { 230b530e9e1SToke Høiland-Jørgensen return head->orig_ctx.data != head->ctx.data || 231b530e9e1SToke Høiland-Jørgensen head->orig_ctx.data_meta != head->ctx.data_meta || 232b530e9e1SToke Høiland-Jørgensen head->orig_ctx.data_end != head->ctx.data_end; 233b530e9e1SToke Høiland-Jørgensen } 234b530e9e1SToke Høiland-Jørgensen 235b530e9e1SToke Høiland-Jørgensen static void reset_ctx(struct xdp_page_head *head) 236b530e9e1SToke Høiland-Jørgensen { 237b530e9e1SToke Høiland-Jørgensen if (likely(!ctx_was_changed(head))) 238b530e9e1SToke Høiland-Jørgensen return; 239b530e9e1SToke Høiland-Jørgensen 240b530e9e1SToke Høiland-Jørgensen head->ctx.data = head->orig_ctx.data; 241b530e9e1SToke Høiland-Jørgensen head->ctx.data_meta = head->orig_ctx.data_meta; 242b530e9e1SToke Høiland-Jørgensen head->ctx.data_end = head->orig_ctx.data_end; 2436c20822fSAlexander Lobakin xdp_update_frame_from_buff(&head->ctx, head->frame); 244b530e9e1SToke Høiland-Jørgensen } 245b530e9e1SToke Høiland-Jørgensen 246b530e9e1SToke Høiland-Jørgensen static int xdp_recv_frames(struct xdp_frame **frames, int nframes, 247b530e9e1SToke Høiland-Jørgensen struct sk_buff **skbs, 248b530e9e1SToke Høiland-Jørgensen struct net_device *dev) 249b530e9e1SToke Høiland-Jørgensen { 250b530e9e1SToke Høiland-Jørgensen gfp_t gfp = __GFP_ZERO | GFP_ATOMIC; 251b530e9e1SToke Høiland-Jørgensen int i, n; 252b530e9e1SToke Høiland-Jørgensen LIST_HEAD(list); 253b530e9e1SToke Høiland-Jørgensen 254025a785fSJakub Kicinski n = kmem_cache_alloc_bulk(skbuff_cache, gfp, nframes, (void **)skbs); 255b530e9e1SToke Høiland-Jørgensen if (unlikely(n == 0)) { 256b530e9e1SToke Høiland-Jørgensen for (i = 0; i < nframes; i++) 257b530e9e1SToke Høiland-Jørgensen xdp_return_frame(frames[i]); 258b530e9e1SToke Høiland-Jørgensen return -ENOMEM; 259b530e9e1SToke Høiland-Jørgensen } 260b530e9e1SToke Høiland-Jørgensen 261b530e9e1SToke Høiland-Jørgensen for (i = 0; i < nframes; i++) { 262b530e9e1SToke Høiland-Jørgensen struct xdp_frame *xdpf = frames[i]; 263b530e9e1SToke Høiland-Jørgensen struct sk_buff *skb = skbs[i]; 264b530e9e1SToke Høiland-Jørgensen 265b530e9e1SToke Høiland-Jørgensen skb = __xdp_build_skb_from_frame(xdpf, skb, dev); 266b530e9e1SToke Høiland-Jørgensen if (!skb) { 267b530e9e1SToke Høiland-Jørgensen xdp_return_frame(xdpf); 268b530e9e1SToke Høiland-Jørgensen continue; 269b530e9e1SToke Høiland-Jørgensen } 270b530e9e1SToke Høiland-Jørgensen 271b530e9e1SToke Høiland-Jørgensen list_add_tail(&skb->list, &list); 272b530e9e1SToke Høiland-Jørgensen } 273b530e9e1SToke Høiland-Jørgensen netif_receive_skb_list(&list); 274b530e9e1SToke Høiland-Jørgensen 275b530e9e1SToke Høiland-Jørgensen return 0; 276b530e9e1SToke Høiland-Jørgensen } 277b530e9e1SToke Høiland-Jørgensen 278b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, 279b530e9e1SToke Høiland-Jørgensen u32 repeat) 280b530e9e1SToke Høiland-Jørgensen { 281b530e9e1SToke Høiland-Jørgensen struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 282b530e9e1SToke Høiland-Jørgensen int err = 0, act, ret, i, nframes = 0, batch_sz; 283b530e9e1SToke Høiland-Jørgensen struct xdp_frame **frames = xdp->frames; 284b530e9e1SToke Høiland-Jørgensen struct xdp_page_head *head; 285b530e9e1SToke Høiland-Jørgensen struct xdp_frame *frm; 286b530e9e1SToke Høiland-Jørgensen bool redirect = false; 287b530e9e1SToke Høiland-Jørgensen struct xdp_buff *ctx; 288b530e9e1SToke Høiland-Jørgensen struct page *page; 289b530e9e1SToke Høiland-Jørgensen 290b530e9e1SToke Høiland-Jørgensen batch_sz = min_t(u32, repeat, xdp->batch_size); 291b530e9e1SToke Høiland-Jørgensen 292b530e9e1SToke Høiland-Jørgensen local_bh_disable(); 293b530e9e1SToke Høiland-Jørgensen xdp_set_return_frame_no_direct(); 294b530e9e1SToke Høiland-Jørgensen 295b530e9e1SToke Høiland-Jørgensen for (i = 0; i < batch_sz; i++) { 296b530e9e1SToke Høiland-Jørgensen page = page_pool_dev_alloc_pages(xdp->pp); 297b530e9e1SToke Høiland-Jørgensen if (!page) { 298b530e9e1SToke Høiland-Jørgensen err = -ENOMEM; 299b530e9e1SToke Høiland-Jørgensen goto out; 300b530e9e1SToke Høiland-Jørgensen } 301b530e9e1SToke Høiland-Jørgensen 302b530e9e1SToke Høiland-Jørgensen head = phys_to_virt(page_to_phys(page)); 303b530e9e1SToke Høiland-Jørgensen reset_ctx(head); 304b530e9e1SToke Høiland-Jørgensen ctx = &head->ctx; 3056c20822fSAlexander Lobakin frm = head->frame; 306b530e9e1SToke Høiland-Jørgensen xdp->frame_cnt++; 307b530e9e1SToke Høiland-Jørgensen 308b530e9e1SToke Høiland-Jørgensen act = bpf_prog_run_xdp(prog, ctx); 309b530e9e1SToke Høiland-Jørgensen 310b530e9e1SToke Høiland-Jørgensen /* if program changed pkt bounds we need to update the xdp_frame */ 311b530e9e1SToke Høiland-Jørgensen if (unlikely(ctx_was_changed(head))) { 312b530e9e1SToke Høiland-Jørgensen ret = xdp_update_frame_from_buff(ctx, frm); 313b530e9e1SToke Høiland-Jørgensen if (ret) { 314b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 315b530e9e1SToke Høiland-Jørgensen continue; 316b530e9e1SToke Høiland-Jørgensen } 317b530e9e1SToke Høiland-Jørgensen } 318b530e9e1SToke Høiland-Jørgensen 319b530e9e1SToke Høiland-Jørgensen switch (act) { 320b530e9e1SToke Høiland-Jørgensen case XDP_TX: 321b530e9e1SToke Høiland-Jørgensen /* we can't do a real XDP_TX since we're not in the 322b530e9e1SToke Høiland-Jørgensen * driver, so turn it into a REDIRECT back to the same 323b530e9e1SToke Høiland-Jørgensen * index 324b530e9e1SToke Høiland-Jørgensen */ 325b530e9e1SToke Høiland-Jørgensen ri->tgt_index = xdp->dev->ifindex; 326b530e9e1SToke Høiland-Jørgensen ri->map_id = INT_MAX; 327b530e9e1SToke Høiland-Jørgensen ri->map_type = BPF_MAP_TYPE_UNSPEC; 328b530e9e1SToke Høiland-Jørgensen fallthrough; 329b530e9e1SToke Høiland-Jørgensen case XDP_REDIRECT: 330b530e9e1SToke Høiland-Jørgensen redirect = true; 331b530e9e1SToke Høiland-Jørgensen ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog); 332b530e9e1SToke Høiland-Jørgensen if (ret) 333b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 334b530e9e1SToke Høiland-Jørgensen break; 335b530e9e1SToke Høiland-Jørgensen case XDP_PASS: 336b530e9e1SToke Høiland-Jørgensen frames[nframes++] = frm; 337b530e9e1SToke Høiland-Jørgensen break; 338b530e9e1SToke Høiland-Jørgensen default: 339b530e9e1SToke Høiland-Jørgensen bpf_warn_invalid_xdp_action(NULL, prog, act); 340b530e9e1SToke Høiland-Jørgensen fallthrough; 341b530e9e1SToke Høiland-Jørgensen case XDP_DROP: 342b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 343b530e9e1SToke Høiland-Jørgensen break; 344b530e9e1SToke Høiland-Jørgensen } 345b530e9e1SToke Høiland-Jørgensen } 346b530e9e1SToke Høiland-Jørgensen 347b530e9e1SToke Høiland-Jørgensen out: 348b530e9e1SToke Høiland-Jørgensen if (redirect) 349b530e9e1SToke Høiland-Jørgensen xdp_do_flush(); 350b530e9e1SToke Høiland-Jørgensen if (nframes) { 351b530e9e1SToke Høiland-Jørgensen ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev); 352b530e9e1SToke Høiland-Jørgensen if (ret) 353b530e9e1SToke Høiland-Jørgensen err = ret; 354b530e9e1SToke Høiland-Jørgensen } 355b530e9e1SToke Høiland-Jørgensen 356b530e9e1SToke Høiland-Jørgensen xdp_clear_return_frame_no_direct(); 357b530e9e1SToke Høiland-Jørgensen local_bh_enable(); 358b530e9e1SToke Høiland-Jørgensen return err; 359b530e9e1SToke Høiland-Jørgensen } 360b530e9e1SToke Høiland-Jørgensen 361b530e9e1SToke Høiland-Jørgensen static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx, 362b530e9e1SToke Høiland-Jørgensen u32 repeat, u32 batch_size, u32 *time) 363b530e9e1SToke Høiland-Jørgensen 364b530e9e1SToke Høiland-Jørgensen { 365b530e9e1SToke Høiland-Jørgensen struct xdp_test_data xdp = { .batch_size = batch_size }; 366b530e9e1SToke Høiland-Jørgensen struct bpf_test_timer t = { .mode = NO_MIGRATE }; 367b530e9e1SToke Høiland-Jørgensen int ret; 368b530e9e1SToke Høiland-Jørgensen 369b530e9e1SToke Høiland-Jørgensen if (!repeat) 370b530e9e1SToke Høiland-Jørgensen repeat = 1; 371b530e9e1SToke Høiland-Jørgensen 372b530e9e1SToke Høiland-Jørgensen ret = xdp_test_run_setup(&xdp, ctx); 373b530e9e1SToke Høiland-Jørgensen if (ret) 374b530e9e1SToke Høiland-Jørgensen return ret; 375b530e9e1SToke Høiland-Jørgensen 376b530e9e1SToke Høiland-Jørgensen bpf_test_timer_enter(&t); 377b530e9e1SToke Høiland-Jørgensen do { 378b530e9e1SToke Høiland-Jørgensen xdp.frame_cnt = 0; 379b530e9e1SToke Høiland-Jørgensen ret = xdp_test_run_batch(&xdp, prog, repeat - t.i); 380b530e9e1SToke Høiland-Jørgensen if (unlikely(ret < 0)) 381b530e9e1SToke Høiland-Jørgensen break; 382b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time)); 383b530e9e1SToke Høiland-Jørgensen bpf_test_timer_leave(&t); 384b530e9e1SToke Høiland-Jørgensen 385b530e9e1SToke Høiland-Jørgensen xdp_test_run_teardown(&xdp); 386b530e9e1SToke Høiland-Jørgensen return ret; 387b530e9e1SToke Høiland-Jørgensen } 388b530e9e1SToke Høiland-Jørgensen 389df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, 390f23c4b39SBjörn Töpel u32 *retval, u32 *time, bool xdp) 3911cf1cae9SAlexei Starovoitov { 392c7603cfaSAndrii Nakryiko struct bpf_prog_array_item item = {.prog = prog}; 393c7603cfaSAndrii Nakryiko struct bpf_run_ctx *old_ctx; 394c7603cfaSAndrii Nakryiko struct bpf_cg_run_ctx run_ctx; 395607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_MIGRATE }; 3968bad74f9SRoman Gushchin enum bpf_cgroup_storage_type stype; 397607b9cc9SLorenz Bauer int ret; 3981cf1cae9SAlexei Starovoitov 3998bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) { 400c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype); 401c7603cfaSAndrii Nakryiko if (IS_ERR(item.cgroup_storage[stype])) { 402c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = NULL; 4038bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 404c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 4058bad74f9SRoman Gushchin return -ENOMEM; 4068bad74f9SRoman Gushchin } 4078bad74f9SRoman Gushchin } 408f42ee093SRoman Gushchin 4091cf1cae9SAlexei Starovoitov if (!repeat) 4101cf1cae9SAlexei Starovoitov repeat = 1; 411df1a2cb7SStanislav Fomichev 412607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 413c7603cfaSAndrii Nakryiko old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 414607b9cc9SLorenz Bauer do { 415c7603cfaSAndrii Nakryiko run_ctx.prog_item = &item; 416*af2d0d09SMartin KaFai Lau local_bh_disable(); 417f23c4b39SBjörn Töpel if (xdp) 418f23c4b39SBjörn Töpel *retval = bpf_prog_run_xdp(prog, ctx); 419f23c4b39SBjörn Töpel else 420fb7dd8bcSAndrii Nakryiko *retval = bpf_prog_run(prog, ctx); 421*af2d0d09SMartin KaFai Lau local_bh_enable(); 422b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time)); 423c7603cfaSAndrii Nakryiko bpf_reset_run_ctx(old_ctx); 424607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 4251cf1cae9SAlexei Starovoitov 4268bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 427c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 428f42ee093SRoman Gushchin 429df1a2cb7SStanislav Fomichev return ret; 4301cf1cae9SAlexei Starovoitov } 4311cf1cae9SAlexei Starovoitov 43278e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr, 43378e52272SDavid Miller union bpf_attr __user *uattr, const void *data, 4347855e0dbSLorenzo Bianconi struct skb_shared_info *sinfo, u32 size, 4357855e0dbSLorenzo Bianconi u32 retval, u32 duration) 4361cf1cae9SAlexei Starovoitov { 43778e52272SDavid Miller void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 4381cf1cae9SAlexei Starovoitov int err = -EFAULT; 439b5a36b1eSLorenz Bauer u32 copy_size = size; 4401cf1cae9SAlexei Starovoitov 441b5a36b1eSLorenz Bauer /* Clamp copy if the user has provided a size hint, but copy the full 442b5a36b1eSLorenz Bauer * buffer if not to retain old behaviour. 443b5a36b1eSLorenz Bauer */ 444b5a36b1eSLorenz Bauer if (kattr->test.data_size_out && 445b5a36b1eSLorenz Bauer copy_size > kattr->test.data_size_out) { 446b5a36b1eSLorenz Bauer copy_size = kattr->test.data_size_out; 447b5a36b1eSLorenz Bauer err = -ENOSPC; 448b5a36b1eSLorenz Bauer } 449b5a36b1eSLorenz Bauer 4507855e0dbSLorenzo Bianconi if (data_out) { 4517855e0dbSLorenzo Bianconi int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size; 4527855e0dbSLorenzo Bianconi 453530e214cSStanislav Fomichev if (len < 0) { 454530e214cSStanislav Fomichev err = -ENOSPC; 455530e214cSStanislav Fomichev goto out; 456530e214cSStanislav Fomichev } 457530e214cSStanislav Fomichev 4587855e0dbSLorenzo Bianconi if (copy_to_user(data_out, data, len)) 4591cf1cae9SAlexei Starovoitov goto out; 4607855e0dbSLorenzo Bianconi 4617855e0dbSLorenzo Bianconi if (sinfo) { 4625d1e9f43SStanislav Fomichev int i, offset = len; 4635d1e9f43SStanislav Fomichev u32 data_len; 4647855e0dbSLorenzo Bianconi 4657855e0dbSLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) { 4667855e0dbSLorenzo Bianconi skb_frag_t *frag = &sinfo->frags[i]; 4677855e0dbSLorenzo Bianconi 4687855e0dbSLorenzo Bianconi if (offset >= copy_size) { 4697855e0dbSLorenzo Bianconi err = -ENOSPC; 4707855e0dbSLorenzo Bianconi break; 4717855e0dbSLorenzo Bianconi } 4727855e0dbSLorenzo Bianconi 4735d1e9f43SStanislav Fomichev data_len = min_t(u32, copy_size - offset, 4747855e0dbSLorenzo Bianconi skb_frag_size(frag)); 4757855e0dbSLorenzo Bianconi 4767855e0dbSLorenzo Bianconi if (copy_to_user(data_out + offset, 4777855e0dbSLorenzo Bianconi skb_frag_address(frag), 4787855e0dbSLorenzo Bianconi data_len)) 4797855e0dbSLorenzo Bianconi goto out; 4807855e0dbSLorenzo Bianconi 4817855e0dbSLorenzo Bianconi offset += data_len; 4827855e0dbSLorenzo Bianconi } 4837855e0dbSLorenzo Bianconi } 4847855e0dbSLorenzo Bianconi } 4857855e0dbSLorenzo Bianconi 4861cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 4871cf1cae9SAlexei Starovoitov goto out; 4881cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 4891cf1cae9SAlexei Starovoitov goto out; 4901cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 4911cf1cae9SAlexei Starovoitov goto out; 492b5a36b1eSLorenz Bauer if (err != -ENOSPC) 4931cf1cae9SAlexei Starovoitov err = 0; 4941cf1cae9SAlexei Starovoitov out: 495e950e843SMatt Mullins trace_bpf_test_finish(&err); 4961cf1cae9SAlexei Starovoitov return err; 4971cf1cae9SAlexei Starovoitov } 4981cf1cae9SAlexei Starovoitov 499faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of 500faeb2dceSAlexei Starovoitov * architecture dependent calling conventions. 7+ can be supported in the 501faeb2dceSAlexei Starovoitov * future. 502faeb2dceSAlexei Starovoitov */ 503e9ff9d52SJean-Philippe Menil __diag_push(); 5040b206c6dSKumar Kartikeya Dwivedi __diag_ignore_all("-Wmissing-prototypes", 505e9ff9d52SJean-Philippe Menil "Global functions as their definitions will be in vmlinux BTF"); 506400031e0SDavid Vernet __bpf_kfunc int bpf_fentry_test1(int a) 507faeb2dceSAlexei Starovoitov { 508faeb2dceSAlexei Starovoitov return a + 1; 509faeb2dceSAlexei Starovoitov } 51046565696SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(bpf_fentry_test1); 511faeb2dceSAlexei Starovoitov 512faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b) 513faeb2dceSAlexei Starovoitov { 514faeb2dceSAlexei Starovoitov return a + b; 515faeb2dceSAlexei Starovoitov } 516faeb2dceSAlexei Starovoitov 517faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c) 518faeb2dceSAlexei Starovoitov { 519faeb2dceSAlexei Starovoitov return a + b + c; 520faeb2dceSAlexei Starovoitov } 521faeb2dceSAlexei Starovoitov 522faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) 523faeb2dceSAlexei Starovoitov { 524faeb2dceSAlexei Starovoitov return (long)a + b + c + d; 525faeb2dceSAlexei Starovoitov } 526faeb2dceSAlexei Starovoitov 527faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) 528faeb2dceSAlexei Starovoitov { 529faeb2dceSAlexei Starovoitov return a + (long)b + c + d + e; 530faeb2dceSAlexei Starovoitov } 531faeb2dceSAlexei Starovoitov 532faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) 533faeb2dceSAlexei Starovoitov { 534faeb2dceSAlexei Starovoitov return a + (long)b + c + d + (long)e + f; 535faeb2dceSAlexei Starovoitov } 536faeb2dceSAlexei Starovoitov 537d923021cSYonghong Song struct bpf_fentry_test_t { 538d923021cSYonghong Song struct bpf_fentry_test_t *a; 539d923021cSYonghong Song }; 540d923021cSYonghong Song 541d923021cSYonghong Song int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg) 542d923021cSYonghong Song { 543d923021cSYonghong Song return (long)arg; 544d923021cSYonghong Song } 545d923021cSYonghong Song 546d923021cSYonghong Song int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg) 547d923021cSYonghong Song { 548d923021cSYonghong Song return (long)arg->a; 549d923021cSYonghong Song } 550d923021cSYonghong Song 551400031e0SDavid Vernet __bpf_kfunc int bpf_modify_return_test(int a, int *b) 5523d08b6f2SKP Singh { 5533d08b6f2SKP Singh *b += 1; 5543d08b6f2SKP Singh return a + *b; 5553d08b6f2SKP Singh } 5567bd1590dSMartin KaFai Lau 557400031e0SDavid Vernet __bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d) 5587bd1590dSMartin KaFai Lau { 5597bd1590dSMartin KaFai Lau return a + b + c + d; 5607bd1590dSMartin KaFai Lau } 5617bd1590dSMartin KaFai Lau 562400031e0SDavid Vernet __bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b) 5637bd1590dSMartin KaFai Lau { 5647bd1590dSMartin KaFai Lau return a + b; 5657bd1590dSMartin KaFai Lau } 5667bd1590dSMartin KaFai Lau 567400031e0SDavid Vernet __bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk) 5687bd1590dSMartin KaFai Lau { 5697bd1590dSMartin KaFai Lau return sk; 5707bd1590dSMartin KaFai Lau } 5717bd1590dSMartin KaFai Lau 572be6b5c10SIlya Leoshkevich long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d) 573be6b5c10SIlya Leoshkevich { 574be6b5c10SIlya Leoshkevich /* Provoke the compiler to assume that the caller has sign-extended a, 575be6b5c10SIlya Leoshkevich * b and c on platforms where this is required (e.g. s390x). 576be6b5c10SIlya Leoshkevich */ 577be6b5c10SIlya Leoshkevich return (long)a + (long)b + (long)c + d; 578be6b5c10SIlya Leoshkevich } 579be6b5c10SIlya Leoshkevich 580792c0a34SKumar Kartikeya Dwivedi struct prog_test_member1 { 581792c0a34SKumar Kartikeya Dwivedi int a; 582792c0a34SKumar Kartikeya Dwivedi }; 583792c0a34SKumar Kartikeya Dwivedi 5848218ccb5SKumar Kartikeya Dwivedi struct prog_test_member { 585792c0a34SKumar Kartikeya Dwivedi struct prog_test_member1 m; 586792c0a34SKumar Kartikeya Dwivedi int c; 5878218ccb5SKumar Kartikeya Dwivedi }; 5888218ccb5SKumar Kartikeya Dwivedi 589c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc { 590c1ff181fSKumar Kartikeya Dwivedi int a; 591c1ff181fSKumar Kartikeya Dwivedi int b; 5928218ccb5SKumar Kartikeya Dwivedi struct prog_test_member memb; 593c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc *next; 5945cdccadcSKumar Kartikeya Dwivedi refcount_t cnt; 595c1ff181fSKumar Kartikeya Dwivedi }; 596c1ff181fSKumar Kartikeya Dwivedi 597c1ff181fSKumar Kartikeya Dwivedi static struct prog_test_ref_kfunc prog_test_struct = { 598c1ff181fSKumar Kartikeya Dwivedi .a = 42, 599c1ff181fSKumar Kartikeya Dwivedi .b = 108, 600c1ff181fSKumar Kartikeya Dwivedi .next = &prog_test_struct, 6015cdccadcSKumar Kartikeya Dwivedi .cnt = REFCOUNT_INIT(1), 602c1ff181fSKumar Kartikeya Dwivedi }; 603c1ff181fSKumar Kartikeya Dwivedi 604400031e0SDavid Vernet __bpf_kfunc struct prog_test_ref_kfunc * 605c1ff181fSKumar Kartikeya Dwivedi bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) 606c1ff181fSKumar Kartikeya Dwivedi { 6075cdccadcSKumar Kartikeya Dwivedi refcount_inc(&prog_test_struct.cnt); 608c1ff181fSKumar Kartikeya Dwivedi return &prog_test_struct; 609c1ff181fSKumar Kartikeya Dwivedi } 610c1ff181fSKumar Kartikeya Dwivedi 611400031e0SDavid Vernet __bpf_kfunc struct prog_test_member * 612792c0a34SKumar Kartikeya Dwivedi bpf_kfunc_call_memb_acquire(void) 613792c0a34SKumar Kartikeya Dwivedi { 6145cdccadcSKumar Kartikeya Dwivedi WARN_ON_ONCE(1); 6155cdccadcSKumar Kartikeya Dwivedi return NULL; 616792c0a34SKumar Kartikeya Dwivedi } 617792c0a34SKumar Kartikeya Dwivedi 618400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) 619c1ff181fSKumar Kartikeya Dwivedi { 6205cdccadcSKumar Kartikeya Dwivedi if (!p) 6215cdccadcSKumar Kartikeya Dwivedi return; 6225cdccadcSKumar Kartikeya Dwivedi 6235cdccadcSKumar Kartikeya Dwivedi refcount_dec(&p->cnt); 624c1ff181fSKumar Kartikeya Dwivedi } 625c1ff181fSKumar Kartikeya Dwivedi 626400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p) 6278218ccb5SKumar Kartikeya Dwivedi { 6288218ccb5SKumar Kartikeya Dwivedi } 6298218ccb5SKumar Kartikeya Dwivedi 630400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p) 631792c0a34SKumar Kartikeya Dwivedi { 6325cdccadcSKumar Kartikeya Dwivedi WARN_ON_ONCE(1); 633792c0a34SKumar Kartikeya Dwivedi } 634792c0a34SKumar Kartikeya Dwivedi 63522ed8d5aSBenjamin Tissoires static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size) 63622ed8d5aSBenjamin Tissoires { 63722ed8d5aSBenjamin Tissoires if (size > 2 * sizeof(int)) 63822ed8d5aSBenjamin Tissoires return NULL; 63922ed8d5aSBenjamin Tissoires 64022ed8d5aSBenjamin Tissoires return (int *)p; 64122ed8d5aSBenjamin Tissoires } 64222ed8d5aSBenjamin Tissoires 643400031e0SDavid Vernet __bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p, 644400031e0SDavid Vernet const int rdwr_buf_size) 64522ed8d5aSBenjamin Tissoires { 64622ed8d5aSBenjamin Tissoires return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size); 64722ed8d5aSBenjamin Tissoires } 64822ed8d5aSBenjamin Tissoires 649400031e0SDavid Vernet __bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p, 650400031e0SDavid Vernet const int rdonly_buf_size) 65122ed8d5aSBenjamin Tissoires { 65222ed8d5aSBenjamin Tissoires return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size); 65322ed8d5aSBenjamin Tissoires } 65422ed8d5aSBenjamin Tissoires 65522ed8d5aSBenjamin Tissoires /* the next 2 ones can't be really used for testing expect to ensure 65622ed8d5aSBenjamin Tissoires * that the verifier rejects the call. 65722ed8d5aSBenjamin Tissoires * Acquire functions must return struct pointers, so these ones are 65822ed8d5aSBenjamin Tissoires * failing. 65922ed8d5aSBenjamin Tissoires */ 660400031e0SDavid Vernet __bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p, 661400031e0SDavid Vernet const int rdonly_buf_size) 66222ed8d5aSBenjamin Tissoires { 66322ed8d5aSBenjamin Tissoires return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size); 66422ed8d5aSBenjamin Tissoires } 66522ed8d5aSBenjamin Tissoires 666400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p) 66722ed8d5aSBenjamin Tissoires { 66822ed8d5aSBenjamin Tissoires } 66922ed8d5aSBenjamin Tissoires 670400031e0SDavid Vernet __bpf_kfunc struct prog_test_ref_kfunc * 6715cdccadcSKumar Kartikeya Dwivedi bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **pp, int a, int b) 67205a945deSKumar Kartikeya Dwivedi { 6735cdccadcSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc *p = READ_ONCE(*pp); 6745cdccadcSKumar Kartikeya Dwivedi 6755cdccadcSKumar Kartikeya Dwivedi if (!p) 6765cdccadcSKumar Kartikeya Dwivedi return NULL; 6775cdccadcSKumar Kartikeya Dwivedi refcount_inc(&p->cnt); 6785cdccadcSKumar Kartikeya Dwivedi return p; 67905a945deSKumar Kartikeya Dwivedi } 68005a945deSKumar Kartikeya Dwivedi 681c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 { 682c1ff181fSKumar Kartikeya Dwivedi int x0; 683c1ff181fSKumar Kartikeya Dwivedi struct { 684c1ff181fSKumar Kartikeya Dwivedi int x1; 685c1ff181fSKumar Kartikeya Dwivedi struct { 686c1ff181fSKumar Kartikeya Dwivedi int x2; 687c1ff181fSKumar Kartikeya Dwivedi struct { 688c1ff181fSKumar Kartikeya Dwivedi int x3; 689c1ff181fSKumar Kartikeya Dwivedi }; 690c1ff181fSKumar Kartikeya Dwivedi }; 691c1ff181fSKumar Kartikeya Dwivedi }; 692c1ff181fSKumar Kartikeya Dwivedi }; 693c1ff181fSKumar Kartikeya Dwivedi 694c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass2 { 695c1ff181fSKumar Kartikeya Dwivedi int len; 696c1ff181fSKumar Kartikeya Dwivedi short arr1[4]; 697c1ff181fSKumar Kartikeya Dwivedi struct { 698c1ff181fSKumar Kartikeya Dwivedi char arr2[4]; 699c1ff181fSKumar Kartikeya Dwivedi unsigned long arr3[8]; 700c1ff181fSKumar Kartikeya Dwivedi } x; 701c1ff181fSKumar Kartikeya Dwivedi }; 702c1ff181fSKumar Kartikeya Dwivedi 703c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail1 { 704c1ff181fSKumar Kartikeya Dwivedi void *p; 705c1ff181fSKumar Kartikeya Dwivedi int x; 706c1ff181fSKumar Kartikeya Dwivedi }; 707c1ff181fSKumar Kartikeya Dwivedi 708c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail2 { 709c1ff181fSKumar Kartikeya Dwivedi int x8; 710c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 x; 711c1ff181fSKumar Kartikeya Dwivedi }; 712c1ff181fSKumar Kartikeya Dwivedi 713c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail3 { 714c1ff181fSKumar Kartikeya Dwivedi int len; 715c1ff181fSKumar Kartikeya Dwivedi char arr1[2]; 716ed8bb032Skernel test robot char arr2[]; 717c1ff181fSKumar Kartikeya Dwivedi }; 718c1ff181fSKumar Kartikeya Dwivedi 719400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) 720c1ff181fSKumar Kartikeya Dwivedi { 721c1ff181fSKumar Kartikeya Dwivedi } 722c1ff181fSKumar Kartikeya Dwivedi 723400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) 724c1ff181fSKumar Kartikeya Dwivedi { 725c1ff181fSKumar Kartikeya Dwivedi } 726c1ff181fSKumar Kartikeya Dwivedi 727400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) 728c1ff181fSKumar Kartikeya Dwivedi { 729c1ff181fSKumar Kartikeya Dwivedi } 730c1ff181fSKumar Kartikeya Dwivedi 731400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) 732c1ff181fSKumar Kartikeya Dwivedi { 733c1ff181fSKumar Kartikeya Dwivedi } 734c1ff181fSKumar Kartikeya Dwivedi 735400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p) 736c1ff181fSKumar Kartikeya Dwivedi { 737c1ff181fSKumar Kartikeya Dwivedi } 738c1ff181fSKumar Kartikeya Dwivedi 739400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p) 740c1ff181fSKumar Kartikeya Dwivedi { 741c1ff181fSKumar Kartikeya Dwivedi } 742c1ff181fSKumar Kartikeya Dwivedi 743400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz) 744c1ff181fSKumar Kartikeya Dwivedi { 745c1ff181fSKumar Kartikeya Dwivedi } 746c1ff181fSKumar Kartikeya Dwivedi 747400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len) 748c1ff181fSKumar Kartikeya Dwivedi { 749c1ff181fSKumar Kartikeya Dwivedi } 750c1ff181fSKumar Kartikeya Dwivedi 751400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) 752c1ff181fSKumar Kartikeya Dwivedi { 753c1ff181fSKumar Kartikeya Dwivedi } 754c1ff181fSKumar Kartikeya Dwivedi 755400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p) 75656e948ffSKumar Kartikeya Dwivedi { 75756e948ffSKumar Kartikeya Dwivedi } 75856e948ffSKumar Kartikeya Dwivedi 759400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_destructive(void) 760e3389458SArtem Savkov { 761e3389458SArtem Savkov } 762e3389458SArtem Savkov 7636aed15e3SDavid Vernet __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused) 7646aed15e3SDavid Vernet { 7656aed15e3SDavid Vernet return arg; 7666aed15e3SDavid Vernet } 7676aed15e3SDavid Vernet 768e9ff9d52SJean-Philippe Menil __diag_pop(); 7693d08b6f2SKP Singh 7705b481acaSBenjamin Tissoires BTF_SET8_START(bpf_test_modify_return_ids) 7715b481acaSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_modify_return_test) 7725b481acaSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE) 7735b481acaSBenjamin Tissoires BTF_SET8_END(bpf_test_modify_return_ids) 7745b481acaSBenjamin Tissoires 7755b481acaSBenjamin Tissoires static const struct btf_kfunc_id_set bpf_test_modify_return_set = { 7765b481acaSBenjamin Tissoires .owner = THIS_MODULE, 7775b481acaSBenjamin Tissoires .set = &bpf_test_modify_return_ids, 7785b481acaSBenjamin Tissoires }; 7793d08b6f2SKP Singh 780a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(test_sk_check_kfunc_ids) 781a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test1) 782a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test2) 783a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test3) 784be6b5c10SIlya Leoshkevich BTF_ID_FLAGS(func, bpf_kfunc_call_test4) 785a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL) 786a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL) 787a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE) 788a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE) 789a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE) 79022ed8d5aSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL) 79122ed8d5aSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL) 79222ed8d5aSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL) 79322ed8d5aSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE) 794a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_kptr_get, KF_ACQUIRE | KF_RET_NULL | KF_KPTR_GET) 795a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx) 796a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1) 797a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2) 798a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1) 799a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2) 800a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3) 801a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1) 802a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1) 803a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2) 80456e948ffSKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS) 805e3389458SArtem Savkov BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE) 8066aed15e3SDavid Vernet BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg) 807a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(test_sk_check_kfunc_ids) 80805a945deSKumar Kartikeya Dwivedi 809be3d72a2SLorenzo Bianconi static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, 810be3d72a2SLorenzo Bianconi u32 size, u32 headroom, u32 tailroom) 8111cf1cae9SAlexei Starovoitov { 8121cf1cae9SAlexei Starovoitov void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 8131cf1cae9SAlexei Starovoitov void *data; 8141cf1cae9SAlexei Starovoitov 8151cf1cae9SAlexei Starovoitov if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) 8161cf1cae9SAlexei Starovoitov return ERR_PTR(-EINVAL); 8171cf1cae9SAlexei Starovoitov 818d800bad6SJesper Dangaard Brouer if (user_size > size) 819d800bad6SJesper Dangaard Brouer return ERR_PTR(-EMSGSIZE); 820d800bad6SJesper Dangaard Brouer 821d3fd203fSBaisong Zhong size = SKB_DATA_ALIGN(size); 8221cf1cae9SAlexei Starovoitov data = kzalloc(size + headroom + tailroom, GFP_USER); 8231cf1cae9SAlexei Starovoitov if (!data) 8241cf1cae9SAlexei Starovoitov return ERR_PTR(-ENOMEM); 8251cf1cae9SAlexei Starovoitov 826d800bad6SJesper Dangaard Brouer if (copy_from_user(data + headroom, data_in, user_size)) { 8271cf1cae9SAlexei Starovoitov kfree(data); 8281cf1cae9SAlexei Starovoitov return ERR_PTR(-EFAULT); 8291cf1cae9SAlexei Starovoitov } 830da00d2f1SKP Singh 831da00d2f1SKP Singh return data; 832da00d2f1SKP Singh } 833da00d2f1SKP Singh 834da00d2f1SKP Singh int bpf_prog_test_run_tracing(struct bpf_prog *prog, 835da00d2f1SKP Singh const union bpf_attr *kattr, 836da00d2f1SKP Singh union bpf_attr __user *uattr) 837da00d2f1SKP Singh { 838d923021cSYonghong Song struct bpf_fentry_test_t arg = {}; 8393d08b6f2SKP Singh u16 side_effect = 0, ret = 0; 8403d08b6f2SKP Singh int b = 2, err = -EFAULT; 8413d08b6f2SKP Singh u32 retval = 0; 842da00d2f1SKP Singh 843b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 8441b4d60ecSSong Liu return -EINVAL; 8451b4d60ecSSong Liu 846da00d2f1SKP Singh switch (prog->expected_attach_type) { 847da00d2f1SKP Singh case BPF_TRACE_FENTRY: 848da00d2f1SKP Singh case BPF_TRACE_FEXIT: 849faeb2dceSAlexei Starovoitov if (bpf_fentry_test1(1) != 2 || 850faeb2dceSAlexei Starovoitov bpf_fentry_test2(2, 3) != 5 || 851faeb2dceSAlexei Starovoitov bpf_fentry_test3(4, 5, 6) != 15 || 852faeb2dceSAlexei Starovoitov bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || 853faeb2dceSAlexei Starovoitov bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || 854d923021cSYonghong Song bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || 855d923021cSYonghong Song bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || 856d923021cSYonghong Song bpf_fentry_test8(&arg) != 0) 857da00d2f1SKP Singh goto out; 858da00d2f1SKP Singh break; 8593d08b6f2SKP Singh case BPF_MODIFY_RETURN: 8603d08b6f2SKP Singh ret = bpf_modify_return_test(1, &b); 8613d08b6f2SKP Singh if (b != 2) 8623d08b6f2SKP Singh side_effect = 1; 8633d08b6f2SKP Singh break; 864da00d2f1SKP Singh default: 865da00d2f1SKP Singh goto out; 866a25ecd9dSColin Ian King } 867da00d2f1SKP Singh 8683d08b6f2SKP Singh retval = ((u32)side_effect << 16) | ret; 8693d08b6f2SKP Singh if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 8703d08b6f2SKP Singh goto out; 8713d08b6f2SKP Singh 872da00d2f1SKP Singh err = 0; 873da00d2f1SKP Singh out: 874da00d2f1SKP Singh trace_bpf_test_finish(&err); 875da00d2f1SKP Singh return err; 8761cf1cae9SAlexei Starovoitov } 8771cf1cae9SAlexei Starovoitov 8781b4d60ecSSong Liu struct bpf_raw_tp_test_run_info { 8791b4d60ecSSong Liu struct bpf_prog *prog; 8801b4d60ecSSong Liu void *ctx; 8811b4d60ecSSong Liu u32 retval; 8821b4d60ecSSong Liu }; 8831b4d60ecSSong Liu 8841b4d60ecSSong Liu static void 8851b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(void *data) 8861b4d60ecSSong Liu { 8871b4d60ecSSong Liu struct bpf_raw_tp_test_run_info *info = data; 8881b4d60ecSSong Liu 8891b4d60ecSSong Liu rcu_read_lock(); 890fb7dd8bcSAndrii Nakryiko info->retval = bpf_prog_run(info->prog, info->ctx); 8911b4d60ecSSong Liu rcu_read_unlock(); 8921b4d60ecSSong Liu } 8931b4d60ecSSong Liu 8941b4d60ecSSong Liu int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, 8951b4d60ecSSong Liu const union bpf_attr *kattr, 8961b4d60ecSSong Liu union bpf_attr __user *uattr) 8971b4d60ecSSong Liu { 8981b4d60ecSSong Liu void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 8991b4d60ecSSong Liu __u32 ctx_size_in = kattr->test.ctx_size_in; 9001b4d60ecSSong Liu struct bpf_raw_tp_test_run_info info; 9011b4d60ecSSong Liu int cpu = kattr->test.cpu, err = 0; 902963ec27aSSong Liu int current_cpu; 9031b4d60ecSSong Liu 9041b4d60ecSSong Liu /* doesn't support data_in/out, ctx_out, duration, or repeat */ 9051b4d60ecSSong Liu if (kattr->test.data_in || kattr->test.data_out || 9061b4d60ecSSong Liu kattr->test.ctx_out || kattr->test.duration || 907b530e9e1SToke Høiland-Jørgensen kattr->test.repeat || kattr->test.batch_size) 9081b4d60ecSSong Liu return -EINVAL; 9091b4d60ecSSong Liu 9107ac6ad05SSong Liu if (ctx_size_in < prog->aux->max_ctx_offset || 9117ac6ad05SSong Liu ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64)) 9121b4d60ecSSong Liu return -EINVAL; 9131b4d60ecSSong Liu 9141b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) 9151b4d60ecSSong Liu return -EINVAL; 9161b4d60ecSSong Liu 9171b4d60ecSSong Liu if (ctx_size_in) { 918db5b6a46SQing Wang info.ctx = memdup_user(ctx_in, ctx_size_in); 919db5b6a46SQing Wang if (IS_ERR(info.ctx)) 920db5b6a46SQing Wang return PTR_ERR(info.ctx); 9211b4d60ecSSong Liu } else { 9221b4d60ecSSong Liu info.ctx = NULL; 9231b4d60ecSSong Liu } 9241b4d60ecSSong Liu 9251b4d60ecSSong Liu info.prog = prog; 9261b4d60ecSSong Liu 927963ec27aSSong Liu current_cpu = get_cpu(); 9281b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || 929963ec27aSSong Liu cpu == current_cpu) { 9301b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(&info); 931963ec27aSSong Liu } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { 9321b4d60ecSSong Liu /* smp_call_function_single() also checks cpu_online() 9331b4d60ecSSong Liu * after csd_lock(). However, since cpu is from user 9341b4d60ecSSong Liu * space, let's do an extra quick check to filter out 9351b4d60ecSSong Liu * invalid value before smp_call_function_single(). 9361b4d60ecSSong Liu */ 9371b4d60ecSSong Liu err = -ENXIO; 938963ec27aSSong Liu } else { 9391b4d60ecSSong Liu err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, 9401b4d60ecSSong Liu &info, 1); 9411b4d60ecSSong Liu } 942963ec27aSSong Liu put_cpu(); 9431b4d60ecSSong Liu 944963ec27aSSong Liu if (!err && 945963ec27aSSong Liu copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) 9461b4d60ecSSong Liu err = -EFAULT; 9471b4d60ecSSong Liu 9481b4d60ecSSong Liu kfree(info.ctx); 9491b4d60ecSSong Liu return err; 9501b4d60ecSSong Liu } 9511b4d60ecSSong Liu 952b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) 953b0b9395dSStanislav Fomichev { 954b0b9395dSStanislav Fomichev void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); 955b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 956b0b9395dSStanislav Fomichev u32 size = kattr->test.ctx_size_in; 957b0b9395dSStanislav Fomichev void *data; 958b0b9395dSStanislav Fomichev int err; 959b0b9395dSStanislav Fomichev 960b0b9395dSStanislav Fomichev if (!data_in && !data_out) 961b0b9395dSStanislav Fomichev return NULL; 962b0b9395dSStanislav Fomichev 963b0b9395dSStanislav Fomichev data = kzalloc(max_size, GFP_USER); 964b0b9395dSStanislav Fomichev if (!data) 965b0b9395dSStanislav Fomichev return ERR_PTR(-ENOMEM); 966b0b9395dSStanislav Fomichev 967b0b9395dSStanislav Fomichev if (data_in) { 968af2ac3e1SAlexei Starovoitov err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size); 969b0b9395dSStanislav Fomichev if (err) { 970b0b9395dSStanislav Fomichev kfree(data); 971b0b9395dSStanislav Fomichev return ERR_PTR(err); 972b0b9395dSStanislav Fomichev } 973b0b9395dSStanislav Fomichev 974b0b9395dSStanislav Fomichev size = min_t(u32, max_size, size); 975b0b9395dSStanislav Fomichev if (copy_from_user(data, data_in, size)) { 976b0b9395dSStanislav Fomichev kfree(data); 977b0b9395dSStanislav Fomichev return ERR_PTR(-EFAULT); 978b0b9395dSStanislav Fomichev } 979b0b9395dSStanislav Fomichev } 980b0b9395dSStanislav Fomichev return data; 981b0b9395dSStanislav Fomichev } 982b0b9395dSStanislav Fomichev 983b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr, 984b0b9395dSStanislav Fomichev union bpf_attr __user *uattr, const void *data, 985b0b9395dSStanislav Fomichev u32 size) 986b0b9395dSStanislav Fomichev { 987b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 988b0b9395dSStanislav Fomichev int err = -EFAULT; 989b0b9395dSStanislav Fomichev u32 copy_size = size; 990b0b9395dSStanislav Fomichev 991b0b9395dSStanislav Fomichev if (!data || !data_out) 992b0b9395dSStanislav Fomichev return 0; 993b0b9395dSStanislav Fomichev 994b0b9395dSStanislav Fomichev if (copy_size > kattr->test.ctx_size_out) { 995b0b9395dSStanislav Fomichev copy_size = kattr->test.ctx_size_out; 996b0b9395dSStanislav Fomichev err = -ENOSPC; 997b0b9395dSStanislav Fomichev } 998b0b9395dSStanislav Fomichev 999b0b9395dSStanislav Fomichev if (copy_to_user(data_out, data, copy_size)) 1000b0b9395dSStanislav Fomichev goto out; 1001b0b9395dSStanislav Fomichev if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) 1002b0b9395dSStanislav Fomichev goto out; 1003b0b9395dSStanislav Fomichev if (err != -ENOSPC) 1004b0b9395dSStanislav Fomichev err = 0; 1005b0b9395dSStanislav Fomichev out: 1006b0b9395dSStanislav Fomichev return err; 1007b0b9395dSStanislav Fomichev } 1008b0b9395dSStanislav Fomichev 1009b0b9395dSStanislav Fomichev /** 1010b0b9395dSStanislav Fomichev * range_is_zero - test whether buffer is initialized 1011b0b9395dSStanislav Fomichev * @buf: buffer to check 1012b0b9395dSStanislav Fomichev * @from: check from this position 1013b0b9395dSStanislav Fomichev * @to: check up until (excluding) this position 1014b0b9395dSStanislav Fomichev * 1015b0b9395dSStanislav Fomichev * This function returns true if the there is a non-zero byte 1016b0b9395dSStanislav Fomichev * in the buf in the range [from,to). 1017b0b9395dSStanislav Fomichev */ 1018b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to) 1019b0b9395dSStanislav Fomichev { 1020b0b9395dSStanislav Fomichev return !memchr_inv((u8 *)buf + from, 0, to - from); 1021b0b9395dSStanislav Fomichev } 1022b0b9395dSStanislav Fomichev 1023b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) 1024b0b9395dSStanislav Fomichev { 1025b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 1026b0b9395dSStanislav Fomichev 1027b0b9395dSStanislav Fomichev if (!__skb) 1028b0b9395dSStanislav Fomichev return 0; 1029b0b9395dSStanislav Fomichev 1030b0b9395dSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 10316de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) 10326de6c1f8SNikita V. Shirokov return -EINVAL; 10336de6c1f8SNikita V. Shirokov 10346de6c1f8SNikita V. Shirokov /* mark is allowed */ 10356de6c1f8SNikita V. Shirokov 10366de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), 10376de6c1f8SNikita V. Shirokov offsetof(struct __sk_buff, priority))) 1038b0b9395dSStanislav Fomichev return -EINVAL; 1039b0b9395dSStanislav Fomichev 1040b0b9395dSStanislav Fomichev /* priority is allowed */ 1041b238290bSNeil Spring /* ingress_ifindex is allowed */ 104221594c44SDmitry Yakunin /* ifindex is allowed */ 104321594c44SDmitry Yakunin 104421594c44SDmitry Yakunin if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), 1045b0b9395dSStanislav Fomichev offsetof(struct __sk_buff, cb))) 1046b0b9395dSStanislav Fomichev return -EINVAL; 1047b0b9395dSStanislav Fomichev 1048b0b9395dSStanislav Fomichev /* cb is allowed */ 1049b0b9395dSStanislav Fomichev 1050b590cb5fSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), 1051ba940948SStanislav Fomichev offsetof(struct __sk_buff, tstamp))) 1052ba940948SStanislav Fomichev return -EINVAL; 1053ba940948SStanislav Fomichev 1054ba940948SStanislav Fomichev /* tstamp is allowed */ 1055850a88ccSStanislav Fomichev /* wire_len is allowed */ 1056850a88ccSStanislav Fomichev /* gso_segs is allowed */ 1057ba940948SStanislav Fomichev 1058850a88ccSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), 1059cf62089bSWillem de Bruijn offsetof(struct __sk_buff, gso_size))) 1060cf62089bSWillem de Bruijn return -EINVAL; 1061cf62089bSWillem de Bruijn 1062cf62089bSWillem de Bruijn /* gso_size is allowed */ 1063cf62089bSWillem de Bruijn 1064cf62089bSWillem de Bruijn if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), 10653384c7c7SVadim Fedorenko offsetof(struct __sk_buff, hwtstamp))) 10663384c7c7SVadim Fedorenko return -EINVAL; 10673384c7c7SVadim Fedorenko 10683384c7c7SVadim Fedorenko /* hwtstamp is allowed */ 10693384c7c7SVadim Fedorenko 10703384c7c7SVadim Fedorenko if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp), 1071b0b9395dSStanislav Fomichev sizeof(struct __sk_buff))) 1072b0b9395dSStanislav Fomichev return -EINVAL; 1073b0b9395dSStanislav Fomichev 10746de6c1f8SNikita V. Shirokov skb->mark = __skb->mark; 1075b0b9395dSStanislav Fomichev skb->priority = __skb->priority; 1076b238290bSNeil Spring skb->skb_iif = __skb->ingress_ifindex; 1077ba940948SStanislav Fomichev skb->tstamp = __skb->tstamp; 1078b0b9395dSStanislav Fomichev memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); 1079b0b9395dSStanislav Fomichev 1080850a88ccSStanislav Fomichev if (__skb->wire_len == 0) { 1081850a88ccSStanislav Fomichev cb->pkt_len = skb->len; 1082850a88ccSStanislav Fomichev } else { 1083850a88ccSStanislav Fomichev if (__skb->wire_len < skb->len || 10847c4e983cSAlexander Duyck __skb->wire_len > GSO_LEGACY_MAX_SIZE) 1085850a88ccSStanislav Fomichev return -EINVAL; 1086850a88ccSStanislav Fomichev cb->pkt_len = __skb->wire_len; 1087850a88ccSStanislav Fomichev } 1088850a88ccSStanislav Fomichev 1089850a88ccSStanislav Fomichev if (__skb->gso_segs > GSO_MAX_SEGS) 1090850a88ccSStanislav Fomichev return -EINVAL; 1091850a88ccSStanislav Fomichev skb_shinfo(skb)->gso_segs = __skb->gso_segs; 1092cf62089bSWillem de Bruijn skb_shinfo(skb)->gso_size = __skb->gso_size; 10933384c7c7SVadim Fedorenko skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp; 1094850a88ccSStanislav Fomichev 1095b0b9395dSStanislav Fomichev return 0; 1096b0b9395dSStanislav Fomichev } 1097b0b9395dSStanislav Fomichev 1098b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) 1099b0b9395dSStanislav Fomichev { 1100b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 1101b0b9395dSStanislav Fomichev 1102b0b9395dSStanislav Fomichev if (!__skb) 1103b0b9395dSStanislav Fomichev return; 1104b0b9395dSStanislav Fomichev 11056de6c1f8SNikita V. Shirokov __skb->mark = skb->mark; 1106b0b9395dSStanislav Fomichev __skb->priority = skb->priority; 1107b238290bSNeil Spring __skb->ingress_ifindex = skb->skb_iif; 110821594c44SDmitry Yakunin __skb->ifindex = skb->dev->ifindex; 1109ba940948SStanislav Fomichev __skb->tstamp = skb->tstamp; 1110b0b9395dSStanislav Fomichev memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); 1111850a88ccSStanislav Fomichev __skb->wire_len = cb->pkt_len; 1112850a88ccSStanislav Fomichev __skb->gso_segs = skb_shinfo(skb)->gso_segs; 11133384c7c7SVadim Fedorenko __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp; 1114b0b9395dSStanislav Fomichev } 1115b0b9395dSStanislav Fomichev 1116435b08ecSDaniel Borkmann static struct proto bpf_dummy_proto = { 1117435b08ecSDaniel Borkmann .name = "bpf_dummy", 1118435b08ecSDaniel Borkmann .owner = THIS_MODULE, 1119435b08ecSDaniel Borkmann .obj_size = sizeof(struct sock), 1120435b08ecSDaniel Borkmann }; 1121435b08ecSDaniel Borkmann 11221cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 11231cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 11241cf1cae9SAlexei Starovoitov { 11251cf1cae9SAlexei Starovoitov bool is_l2 = false, is_direct_pkt_access = false; 112621594c44SDmitry Yakunin struct net *net = current->nsproxy->net_ns; 112721594c44SDmitry Yakunin struct net_device *dev = net->loopback_dev; 11281cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 11291cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 1130b0b9395dSStanislav Fomichev struct __sk_buff *ctx = NULL; 11311cf1cae9SAlexei Starovoitov u32 retval, duration; 11326e6fddc7SDaniel Borkmann int hh_len = ETH_HLEN; 11331cf1cae9SAlexei Starovoitov struct sk_buff *skb; 11342cb494a3SSong Liu struct sock *sk; 11351cf1cae9SAlexei Starovoitov void *data; 11361cf1cae9SAlexei Starovoitov int ret; 11371cf1cae9SAlexei Starovoitov 1138b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 11391b4d60ecSSong Liu return -EINVAL; 11401b4d60ecSSong Liu 1141be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, 1142be3d72a2SLorenzo Bianconi size, NET_SKB_PAD + NET_IP_ALIGN, 11431cf1cae9SAlexei Starovoitov SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 11441cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 11451cf1cae9SAlexei Starovoitov return PTR_ERR(data); 11461cf1cae9SAlexei Starovoitov 1147b0b9395dSStanislav Fomichev ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); 1148b0b9395dSStanislav Fomichev if (IS_ERR(ctx)) { 1149b0b9395dSStanislav Fomichev kfree(data); 1150b0b9395dSStanislav Fomichev return PTR_ERR(ctx); 1151b0b9395dSStanislav Fomichev } 1152b0b9395dSStanislav Fomichev 11531cf1cae9SAlexei Starovoitov switch (prog->type) { 11541cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 11551cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 11561cf1cae9SAlexei Starovoitov is_l2 = true; 1157df561f66SGustavo A. R. Silva fallthrough; 11581cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 11591cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 11601cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 11611cf1cae9SAlexei Starovoitov is_direct_pkt_access = true; 11621cf1cae9SAlexei Starovoitov break; 11631cf1cae9SAlexei Starovoitov default: 11641cf1cae9SAlexei Starovoitov break; 11651cf1cae9SAlexei Starovoitov } 11661cf1cae9SAlexei Starovoitov 1167435b08ecSDaniel Borkmann sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1); 11682cb494a3SSong Liu if (!sk) { 11691cf1cae9SAlexei Starovoitov kfree(data); 1170b0b9395dSStanislav Fomichev kfree(ctx); 11711cf1cae9SAlexei Starovoitov return -ENOMEM; 11721cf1cae9SAlexei Starovoitov } 11732cb494a3SSong Liu sock_init_data(NULL, sk); 11742cb494a3SSong Liu 1175ce098da1SKees Cook skb = slab_build_skb(data); 11762cb494a3SSong Liu if (!skb) { 11772cb494a3SSong Liu kfree(data); 1178b0b9395dSStanislav Fomichev kfree(ctx); 1179435b08ecSDaniel Borkmann sk_free(sk); 11802cb494a3SSong Liu return -ENOMEM; 11812cb494a3SSong Liu } 11822cb494a3SSong Liu skb->sk = sk; 11831cf1cae9SAlexei Starovoitov 1184586f8525SDavid Miller skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 11851cf1cae9SAlexei Starovoitov __skb_put(skb, size); 118621594c44SDmitry Yakunin if (ctx && ctx->ifindex > 1) { 118721594c44SDmitry Yakunin dev = dev_get_by_index(net, ctx->ifindex); 118821594c44SDmitry Yakunin if (!dev) { 118921594c44SDmitry Yakunin ret = -ENODEV; 119021594c44SDmitry Yakunin goto out; 119121594c44SDmitry Yakunin } 119221594c44SDmitry Yakunin } 119321594c44SDmitry Yakunin skb->protocol = eth_type_trans(skb, dev); 11941cf1cae9SAlexei Starovoitov skb_reset_network_header(skb); 11951cf1cae9SAlexei Starovoitov 1196fa5cb548SDmitry Yakunin switch (skb->protocol) { 1197fa5cb548SDmitry Yakunin case htons(ETH_P_IP): 1198fa5cb548SDmitry Yakunin sk->sk_family = AF_INET; 1199fa5cb548SDmitry Yakunin if (sizeof(struct iphdr) <= skb_headlen(skb)) { 1200fa5cb548SDmitry Yakunin sk->sk_rcv_saddr = ip_hdr(skb)->saddr; 1201fa5cb548SDmitry Yakunin sk->sk_daddr = ip_hdr(skb)->daddr; 1202fa5cb548SDmitry Yakunin } 1203fa5cb548SDmitry Yakunin break; 1204fa5cb548SDmitry Yakunin #if IS_ENABLED(CONFIG_IPV6) 1205fa5cb548SDmitry Yakunin case htons(ETH_P_IPV6): 1206fa5cb548SDmitry Yakunin sk->sk_family = AF_INET6; 1207fa5cb548SDmitry Yakunin if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) { 1208fa5cb548SDmitry Yakunin sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; 1209fa5cb548SDmitry Yakunin sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; 1210fa5cb548SDmitry Yakunin } 1211fa5cb548SDmitry Yakunin break; 1212fa5cb548SDmitry Yakunin #endif 1213fa5cb548SDmitry Yakunin default: 1214fa5cb548SDmitry Yakunin break; 1215fa5cb548SDmitry Yakunin } 1216fa5cb548SDmitry Yakunin 12171cf1cae9SAlexei Starovoitov if (is_l2) 12186e6fddc7SDaniel Borkmann __skb_push(skb, hh_len); 12191cf1cae9SAlexei Starovoitov if (is_direct_pkt_access) 12206aaae2b6SDaniel Borkmann bpf_compute_data_pointers(skb); 1221b0b9395dSStanislav Fomichev ret = convert___skb_to_skb(skb, ctx); 1222b0b9395dSStanislav Fomichev if (ret) 1223b0b9395dSStanislav Fomichev goto out; 1224f23c4b39SBjörn Töpel ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); 1225b0b9395dSStanislav Fomichev if (ret) 1226b0b9395dSStanislav Fomichev goto out; 12276e6fddc7SDaniel Borkmann if (!is_l2) { 12286e6fddc7SDaniel Borkmann if (skb_headroom(skb) < hh_len) { 12296e6fddc7SDaniel Borkmann int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 12306e6fddc7SDaniel Borkmann 12316e6fddc7SDaniel Borkmann if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { 1232b0b9395dSStanislav Fomichev ret = -ENOMEM; 1233b0b9395dSStanislav Fomichev goto out; 12346e6fddc7SDaniel Borkmann } 12356e6fddc7SDaniel Borkmann } 12366e6fddc7SDaniel Borkmann memset(__skb_push(skb, hh_len), 0, hh_len); 12376e6fddc7SDaniel Borkmann } 1238b0b9395dSStanislav Fomichev convert_skb_to___skb(skb, ctx); 12396e6fddc7SDaniel Borkmann 12401cf1cae9SAlexei Starovoitov size = skb->len; 12411cf1cae9SAlexei Starovoitov /* bpf program can never convert linear skb to non-linear */ 12421cf1cae9SAlexei Starovoitov if (WARN_ON_ONCE(skb_is_nonlinear(skb))) 12431cf1cae9SAlexei Starovoitov size = skb_headlen(skb); 12447855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval, 12457855e0dbSLorenzo Bianconi duration); 1246b0b9395dSStanislav Fomichev if (!ret) 1247b0b9395dSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, ctx, 1248b0b9395dSStanislav Fomichev sizeof(struct __sk_buff)); 1249b0b9395dSStanislav Fomichev out: 125021594c44SDmitry Yakunin if (dev && dev != net->loopback_dev) 125121594c44SDmitry Yakunin dev_put(dev); 12521cf1cae9SAlexei Starovoitov kfree_skb(skb); 1253435b08ecSDaniel Borkmann sk_free(sk); 1254b0b9395dSStanislav Fomichev kfree(ctx); 12551cf1cae9SAlexei Starovoitov return ret; 12561cf1cae9SAlexei Starovoitov } 12571cf1cae9SAlexei Starovoitov 125847316f4aSZvi Effron static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp) 125947316f4aSZvi Effron { 1260ec94670fSZvi Effron unsigned int ingress_ifindex, rx_queue_index; 1261ec94670fSZvi Effron struct netdev_rx_queue *rxqueue; 1262ec94670fSZvi Effron struct net_device *device; 1263ec94670fSZvi Effron 126447316f4aSZvi Effron if (!xdp_md) 126547316f4aSZvi Effron return 0; 126647316f4aSZvi Effron 126747316f4aSZvi Effron if (xdp_md->egress_ifindex != 0) 126847316f4aSZvi Effron return -EINVAL; 126947316f4aSZvi Effron 1270ec94670fSZvi Effron ingress_ifindex = xdp_md->ingress_ifindex; 1271ec94670fSZvi Effron rx_queue_index = xdp_md->rx_queue_index; 1272ec94670fSZvi Effron 1273ec94670fSZvi Effron if (!ingress_ifindex && rx_queue_index) 127447316f4aSZvi Effron return -EINVAL; 127547316f4aSZvi Effron 1276ec94670fSZvi Effron if (ingress_ifindex) { 1277ec94670fSZvi Effron device = dev_get_by_index(current->nsproxy->net_ns, 1278ec94670fSZvi Effron ingress_ifindex); 1279ec94670fSZvi Effron if (!device) 1280ec94670fSZvi Effron return -ENODEV; 128147316f4aSZvi Effron 1282ec94670fSZvi Effron if (rx_queue_index >= device->real_num_rx_queues) 1283ec94670fSZvi Effron goto free_dev; 1284ec94670fSZvi Effron 1285ec94670fSZvi Effron rxqueue = __netif_get_rx_queue(device, rx_queue_index); 1286ec94670fSZvi Effron 1287ec94670fSZvi Effron if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq)) 1288ec94670fSZvi Effron goto free_dev; 1289ec94670fSZvi Effron 1290ec94670fSZvi Effron xdp->rxq = &rxqueue->xdp_rxq; 1291ec94670fSZvi Effron /* The device is now tracked in the xdp->rxq for later 1292ec94670fSZvi Effron * dev_put() 1293ec94670fSZvi Effron */ 1294ec94670fSZvi Effron } 1295ec94670fSZvi Effron 1296ec94670fSZvi Effron xdp->data = xdp->data_meta + xdp_md->data; 129747316f4aSZvi Effron return 0; 1298ec94670fSZvi Effron 1299ec94670fSZvi Effron free_dev: 1300ec94670fSZvi Effron dev_put(device); 1301ec94670fSZvi Effron return -EINVAL; 1302ec94670fSZvi Effron } 1303ec94670fSZvi Effron 1304ec94670fSZvi Effron static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md) 1305ec94670fSZvi Effron { 1306ec94670fSZvi Effron if (!xdp_md) 1307ec94670fSZvi Effron return; 1308ec94670fSZvi Effron 1309ec94670fSZvi Effron xdp_md->data = xdp->data - xdp->data_meta; 1310ec94670fSZvi Effron xdp_md->data_end = xdp->data_end - xdp->data_meta; 1311ec94670fSZvi Effron 1312ec94670fSZvi Effron if (xdp_md->ingress_ifindex) 1313ec94670fSZvi Effron dev_put(xdp->rxq->dev); 131447316f4aSZvi Effron } 131547316f4aSZvi Effron 13161cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 13171cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 13181cf1cae9SAlexei Starovoitov { 1319b530e9e1SToke Høiland-Jørgensen bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES); 1320bc56c919SJesper Dangaard Brouer u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1321b530e9e1SToke Høiland-Jørgensen u32 batch_size = kattr->test.batch_size; 1322eecbfd97SToke Høiland-Jørgensen u32 retval = 0, duration, max_data_sz; 13231cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 13241c194998SLorenzo Bianconi u32 headroom = XDP_PACKET_HEADROOM; 13251cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 132665073a67SDaniel Borkmann struct netdev_rx_queue *rxqueue; 13271c194998SLorenzo Bianconi struct skb_shared_info *sinfo; 13281cf1cae9SAlexei Starovoitov struct xdp_buff xdp = {}; 13291c194998SLorenzo Bianconi int i, ret = -EINVAL; 133047316f4aSZvi Effron struct xdp_md *ctx; 13311cf1cae9SAlexei Starovoitov void *data; 13321cf1cae9SAlexei Starovoitov 13335e21bb4eSXuan Zhuo if (prog->expected_attach_type == BPF_XDP_DEVMAP || 13345e21bb4eSXuan Zhuo prog->expected_attach_type == BPF_XDP_CPUMAP) 13355e21bb4eSXuan Zhuo return -EINVAL; 13366d4eb36dSAndrii Nakryiko 1337b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES) 1338b530e9e1SToke Høiland-Jørgensen return -EINVAL; 1339b530e9e1SToke Høiland-Jørgensen 13403d76a4d3SStanislav Fomichev if (bpf_prog_is_dev_bound(prog->aux)) 13413d76a4d3SStanislav Fomichev return -EINVAL; 13423d76a4d3SStanislav Fomichev 1343b530e9e1SToke Høiland-Jørgensen if (do_live) { 1344b530e9e1SToke Høiland-Jørgensen if (!batch_size) 1345b530e9e1SToke Høiland-Jørgensen batch_size = NAPI_POLL_WEIGHT; 1346b530e9e1SToke Høiland-Jørgensen else if (batch_size > TEST_XDP_MAX_BATCH) 1347b530e9e1SToke Høiland-Jørgensen return -E2BIG; 1348b6f1f780SToke Høiland-Jørgensen 1349b6f1f780SToke Høiland-Jørgensen headroom += sizeof(struct xdp_page_head); 1350b530e9e1SToke Høiland-Jørgensen } else if (batch_size) { 1351b530e9e1SToke Høiland-Jørgensen return -EINVAL; 1352b530e9e1SToke Høiland-Jørgensen } 1353b530e9e1SToke Høiland-Jørgensen 135447316f4aSZvi Effron ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md)); 135547316f4aSZvi Effron if (IS_ERR(ctx)) 135647316f4aSZvi Effron return PTR_ERR(ctx); 135747316f4aSZvi Effron 135847316f4aSZvi Effron if (ctx) { 135947316f4aSZvi Effron /* There can't be user provided data before the meta data */ 136047316f4aSZvi Effron if (ctx->data_meta || ctx->data_end != size || 136147316f4aSZvi Effron ctx->data > ctx->data_end || 1362b530e9e1SToke Høiland-Jørgensen unlikely(xdp_metalen_invalid(ctx->data)) || 1363b530e9e1SToke Høiland-Jørgensen (do_live && (kattr->test.data_out || kattr->test.ctx_out))) 136447316f4aSZvi Effron goto free_ctx; 136547316f4aSZvi Effron /* Meta data is allocated from the headroom */ 136647316f4aSZvi Effron headroom -= ctx->data; 136747316f4aSZvi Effron } 1368947e8b59SStanislav Fomichev 1369bc56c919SJesper Dangaard Brouer max_data_sz = 4096 - headroom - tailroom; 1370b530e9e1SToke Høiland-Jørgensen if (size > max_data_sz) { 1371b530e9e1SToke Høiland-Jørgensen /* disallow live data mode for jumbo frames */ 1372b530e9e1SToke Høiland-Jørgensen if (do_live) 1373b530e9e1SToke Høiland-Jørgensen goto free_ctx; 1374b530e9e1SToke Høiland-Jørgensen size = max_data_sz; 1375b530e9e1SToke Høiland-Jørgensen } 1376bc56c919SJesper Dangaard Brouer 13771c194998SLorenzo Bianconi data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom); 137847316f4aSZvi Effron if (IS_ERR(data)) { 137947316f4aSZvi Effron ret = PTR_ERR(data); 138047316f4aSZvi Effron goto free_ctx; 138147316f4aSZvi Effron } 13821cf1cae9SAlexei Starovoitov 138365073a67SDaniel Borkmann rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 13841c194998SLorenzo Bianconi rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom; 13851c194998SLorenzo Bianconi xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq); 1386be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data, headroom, size, true); 13871c194998SLorenzo Bianconi sinfo = xdp_get_shared_info_from_buff(&xdp); 1388be9df4afSLorenzo Bianconi 138947316f4aSZvi Effron ret = xdp_convert_md_to_buff(ctx, &xdp); 139047316f4aSZvi Effron if (ret) 139147316f4aSZvi Effron goto free_data; 139247316f4aSZvi Effron 13931c194998SLorenzo Bianconi if (unlikely(kattr->test.data_size_in > size)) { 13941c194998SLorenzo Bianconi void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 13951c194998SLorenzo Bianconi 13961c194998SLorenzo Bianconi while (size < kattr->test.data_size_in) { 13971c194998SLorenzo Bianconi struct page *page; 13981c194998SLorenzo Bianconi skb_frag_t *frag; 13999d63b59dSStanislav Fomichev u32 data_len; 14001c194998SLorenzo Bianconi 1401a6763080SLorenzo Bianconi if (sinfo->nr_frags == MAX_SKB_FRAGS) { 1402a6763080SLorenzo Bianconi ret = -ENOMEM; 1403a6763080SLorenzo Bianconi goto out; 1404a6763080SLorenzo Bianconi } 1405a6763080SLorenzo Bianconi 14061c194998SLorenzo Bianconi page = alloc_page(GFP_KERNEL); 14071c194998SLorenzo Bianconi if (!page) { 14081c194998SLorenzo Bianconi ret = -ENOMEM; 14091c194998SLorenzo Bianconi goto out; 14101c194998SLorenzo Bianconi } 14111c194998SLorenzo Bianconi 14121c194998SLorenzo Bianconi frag = &sinfo->frags[sinfo->nr_frags++]; 14131c194998SLorenzo Bianconi __skb_frag_set_page(frag, page); 14141c194998SLorenzo Bianconi 14159d63b59dSStanislav Fomichev data_len = min_t(u32, kattr->test.data_size_in - size, 14161c194998SLorenzo Bianconi PAGE_SIZE); 14171c194998SLorenzo Bianconi skb_frag_size_set(frag, data_len); 14181c194998SLorenzo Bianconi 14191c194998SLorenzo Bianconi if (copy_from_user(page_address(page), data_in + size, 14201c194998SLorenzo Bianconi data_len)) { 14211c194998SLorenzo Bianconi ret = -EFAULT; 14221c194998SLorenzo Bianconi goto out; 14231c194998SLorenzo Bianconi } 14241c194998SLorenzo Bianconi sinfo->xdp_frags_size += data_len; 14251c194998SLorenzo Bianconi size += data_len; 14261c194998SLorenzo Bianconi } 14271c194998SLorenzo Bianconi xdp_buff_set_frags_flag(&xdp); 14281c194998SLorenzo Bianconi } 14291c194998SLorenzo Bianconi 1430de21d8bfSLorenz Bauer if (repeat > 1) 1431f23c4b39SBjörn Töpel bpf_prog_change_xdp(NULL, prog); 14321c194998SLorenzo Bianconi 1433b530e9e1SToke Høiland-Jørgensen if (do_live) 1434b530e9e1SToke Høiland-Jørgensen ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration); 1435b530e9e1SToke Høiland-Jørgensen else 1436f23c4b39SBjörn Töpel ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); 1437ec94670fSZvi Effron /* We convert the xdp_buff back to an xdp_md before checking the return 1438ec94670fSZvi Effron * code so the reference count of any held netdevice will be decremented 1439ec94670fSZvi Effron * even if the test run failed. 1440ec94670fSZvi Effron */ 1441ec94670fSZvi Effron xdp_convert_buff_to_md(&xdp, ctx); 1442dcb40590SRoman Gushchin if (ret) 1443dcb40590SRoman Gushchin goto out; 144447316f4aSZvi Effron 14451c194998SLorenzo Bianconi size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; 14467855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size, 14477855e0dbSLorenzo Bianconi retval, duration); 144847316f4aSZvi Effron if (!ret) 144947316f4aSZvi Effron ret = bpf_ctx_finish(kattr, uattr, ctx, 145047316f4aSZvi Effron sizeof(struct xdp_md)); 145147316f4aSZvi Effron 1452dcb40590SRoman Gushchin out: 1453de21d8bfSLorenz Bauer if (repeat > 1) 1454f23c4b39SBjörn Töpel bpf_prog_change_xdp(prog, NULL); 145547316f4aSZvi Effron free_data: 14561c194998SLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) 14571c194998SLorenzo Bianconi __free_page(skb_frag_page(&sinfo->frags[i])); 14581cf1cae9SAlexei Starovoitov kfree(data); 145947316f4aSZvi Effron free_ctx: 146047316f4aSZvi Effron kfree(ctx); 14611cf1cae9SAlexei Starovoitov return ret; 14621cf1cae9SAlexei Starovoitov } 1463b7a1848eSStanislav Fomichev 1464b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) 1465b2ca4e1cSStanislav Fomichev { 1466b2ca4e1cSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 1467b2ca4e1cSStanislav Fomichev if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) 1468b2ca4e1cSStanislav Fomichev return -EINVAL; 1469b2ca4e1cSStanislav Fomichev 1470b2ca4e1cSStanislav Fomichev /* flags is allowed */ 1471b2ca4e1cSStanislav Fomichev 1472b590cb5fSStanislav Fomichev if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), 1473b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys))) 1474b2ca4e1cSStanislav Fomichev return -EINVAL; 1475b2ca4e1cSStanislav Fomichev 1476b2ca4e1cSStanislav Fomichev return 0; 1477b2ca4e1cSStanislav Fomichev } 1478b2ca4e1cSStanislav Fomichev 1479b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, 1480b7a1848eSStanislav Fomichev const union bpf_attr *kattr, 1481b7a1848eSStanislav Fomichev union bpf_attr __user *uattr) 1482b7a1848eSStanislav Fomichev { 1483607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 1484b7a1848eSStanislav Fomichev u32 size = kattr->test.data_size_in; 14857b8a1304SStanislav Fomichev struct bpf_flow_dissector ctx = {}; 1486b7a1848eSStanislav Fomichev u32 repeat = kattr->test.repeat; 1487b2ca4e1cSStanislav Fomichev struct bpf_flow_keys *user_ctx; 1488b7a1848eSStanislav Fomichev struct bpf_flow_keys flow_keys; 14897b8a1304SStanislav Fomichev const struct ethhdr *eth; 1490b2ca4e1cSStanislav Fomichev unsigned int flags = 0; 1491b7a1848eSStanislav Fomichev u32 retval, duration; 1492b7a1848eSStanislav Fomichev void *data; 1493b7a1848eSStanislav Fomichev int ret; 1494b7a1848eSStanislav Fomichev 1495b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 14961b4d60ecSSong Liu return -EINVAL; 14971b4d60ecSSong Liu 14987b8a1304SStanislav Fomichev if (size < ETH_HLEN) 14997b8a1304SStanislav Fomichev return -EINVAL; 15007b8a1304SStanislav Fomichev 1501be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0); 1502b7a1848eSStanislav Fomichev if (IS_ERR(data)) 1503b7a1848eSStanislav Fomichev return PTR_ERR(data); 1504b7a1848eSStanislav Fomichev 15057b8a1304SStanislav Fomichev eth = (struct ethhdr *)data; 1506b7a1848eSStanislav Fomichev 1507b7a1848eSStanislav Fomichev if (!repeat) 1508b7a1848eSStanislav Fomichev repeat = 1; 1509b7a1848eSStanislav Fomichev 1510b2ca4e1cSStanislav Fomichev user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); 1511b2ca4e1cSStanislav Fomichev if (IS_ERR(user_ctx)) { 1512b2ca4e1cSStanislav Fomichev kfree(data); 1513b2ca4e1cSStanislav Fomichev return PTR_ERR(user_ctx); 1514b2ca4e1cSStanislav Fomichev } 1515b2ca4e1cSStanislav Fomichev if (user_ctx) { 1516b2ca4e1cSStanislav Fomichev ret = verify_user_bpf_flow_keys(user_ctx); 1517b2ca4e1cSStanislav Fomichev if (ret) 1518b2ca4e1cSStanislav Fomichev goto out; 1519b2ca4e1cSStanislav Fomichev flags = user_ctx->flags; 1520b2ca4e1cSStanislav Fomichev } 1521b2ca4e1cSStanislav Fomichev 15227b8a1304SStanislav Fomichev ctx.flow_keys = &flow_keys; 15237b8a1304SStanislav Fomichev ctx.data = data; 15247b8a1304SStanislav Fomichev ctx.data_end = (__u8 *)data + size; 15257b8a1304SStanislav Fomichev 1526607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 1527607b9cc9SLorenz Bauer do { 15287b8a1304SStanislav Fomichev retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 1529b2ca4e1cSStanislav Fomichev size, flags); 1530b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 1531607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 15327b8a1304SStanislav Fomichev 1533607b9cc9SLorenz Bauer if (ret < 0) 1534a439184dSStanislav Fomichev goto out; 1535b7a1848eSStanislav Fomichev 15367855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL, 15377855e0dbSLorenzo Bianconi sizeof(flow_keys), retval, duration); 1538b2ca4e1cSStanislav Fomichev if (!ret) 1539b2ca4e1cSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, user_ctx, 1540b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys)); 1541b7a1848eSStanislav Fomichev 1542a439184dSStanislav Fomichev out: 1543b2ca4e1cSStanislav Fomichev kfree(user_ctx); 15447b8a1304SStanislav Fomichev kfree(data); 1545b7a1848eSStanislav Fomichev return ret; 1546b7a1848eSStanislav Fomichev } 15477c32e8f8SLorenz Bauer 15487c32e8f8SLorenz Bauer int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr, 15497c32e8f8SLorenz Bauer union bpf_attr __user *uattr) 15507c32e8f8SLorenz Bauer { 15517c32e8f8SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 15527c32e8f8SLorenz Bauer struct bpf_prog_array *progs = NULL; 15537c32e8f8SLorenz Bauer struct bpf_sk_lookup_kern ctx = {}; 15547c32e8f8SLorenz Bauer u32 repeat = kattr->test.repeat; 15557c32e8f8SLorenz Bauer struct bpf_sk_lookup *user_ctx; 15567c32e8f8SLorenz Bauer u32 retval, duration; 15577c32e8f8SLorenz Bauer int ret = -EINVAL; 15587c32e8f8SLorenz Bauer 1559b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 15607c32e8f8SLorenz Bauer return -EINVAL; 15617c32e8f8SLorenz Bauer 15627c32e8f8SLorenz Bauer if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || 15637c32e8f8SLorenz Bauer kattr->test.data_size_out) 15647c32e8f8SLorenz Bauer return -EINVAL; 15657c32e8f8SLorenz Bauer 15667c32e8f8SLorenz Bauer if (!repeat) 15677c32e8f8SLorenz Bauer repeat = 1; 15687c32e8f8SLorenz Bauer 15697c32e8f8SLorenz Bauer user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); 15707c32e8f8SLorenz Bauer if (IS_ERR(user_ctx)) 15717c32e8f8SLorenz Bauer return PTR_ERR(user_ctx); 15727c32e8f8SLorenz Bauer 15737c32e8f8SLorenz Bauer if (!user_ctx) 15747c32e8f8SLorenz Bauer return -EINVAL; 15757c32e8f8SLorenz Bauer 15767c32e8f8SLorenz Bauer if (user_ctx->sk) 15777c32e8f8SLorenz Bauer goto out; 15787c32e8f8SLorenz Bauer 15797c32e8f8SLorenz Bauer if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) 15807c32e8f8SLorenz Bauer goto out; 15817c32e8f8SLorenz Bauer 15829a69e2b3SJakub Sitnicki if (user_ctx->local_port > U16_MAX) { 15837c32e8f8SLorenz Bauer ret = -ERANGE; 15847c32e8f8SLorenz Bauer goto out; 15857c32e8f8SLorenz Bauer } 15867c32e8f8SLorenz Bauer 15877c32e8f8SLorenz Bauer ctx.family = (u16)user_ctx->family; 15887c32e8f8SLorenz Bauer ctx.protocol = (u16)user_ctx->protocol; 15897c32e8f8SLorenz Bauer ctx.dport = (u16)user_ctx->local_port; 15909a69e2b3SJakub Sitnicki ctx.sport = user_ctx->remote_port; 15917c32e8f8SLorenz Bauer 15927c32e8f8SLorenz Bauer switch (ctx.family) { 15937c32e8f8SLorenz Bauer case AF_INET: 15947c32e8f8SLorenz Bauer ctx.v4.daddr = (__force __be32)user_ctx->local_ip4; 15957c32e8f8SLorenz Bauer ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4; 15967c32e8f8SLorenz Bauer break; 15977c32e8f8SLorenz Bauer 15987c32e8f8SLorenz Bauer #if IS_ENABLED(CONFIG_IPV6) 15997c32e8f8SLorenz Bauer case AF_INET6: 16007c32e8f8SLorenz Bauer ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; 16017c32e8f8SLorenz Bauer ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; 16027c32e8f8SLorenz Bauer break; 16037c32e8f8SLorenz Bauer #endif 16047c32e8f8SLorenz Bauer 16057c32e8f8SLorenz Bauer default: 16067c32e8f8SLorenz Bauer ret = -EAFNOSUPPORT; 16077c32e8f8SLorenz Bauer goto out; 16087c32e8f8SLorenz Bauer } 16097c32e8f8SLorenz Bauer 16107c32e8f8SLorenz Bauer progs = bpf_prog_array_alloc(1, GFP_KERNEL); 16117c32e8f8SLorenz Bauer if (!progs) { 16127c32e8f8SLorenz Bauer ret = -ENOMEM; 16137c32e8f8SLorenz Bauer goto out; 16147c32e8f8SLorenz Bauer } 16157c32e8f8SLorenz Bauer 16167c32e8f8SLorenz Bauer progs->items[0].prog = prog; 16177c32e8f8SLorenz Bauer 16187c32e8f8SLorenz Bauer bpf_test_timer_enter(&t); 16197c32e8f8SLorenz Bauer do { 16207c32e8f8SLorenz Bauer ctx.selected_sk = NULL; 1621fb7dd8bcSAndrii Nakryiko retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run); 1622b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 16237c32e8f8SLorenz Bauer bpf_test_timer_leave(&t); 16247c32e8f8SLorenz Bauer 16257c32e8f8SLorenz Bauer if (ret < 0) 16267c32e8f8SLorenz Bauer goto out; 16277c32e8f8SLorenz Bauer 16287c32e8f8SLorenz Bauer user_ctx->cookie = 0; 16297c32e8f8SLorenz Bauer if (ctx.selected_sk) { 16307c32e8f8SLorenz Bauer if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { 16317c32e8f8SLorenz Bauer ret = -EOPNOTSUPP; 16327c32e8f8SLorenz Bauer goto out; 16337c32e8f8SLorenz Bauer } 16347c32e8f8SLorenz Bauer 16357c32e8f8SLorenz Bauer user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); 16367c32e8f8SLorenz Bauer } 16377c32e8f8SLorenz Bauer 16387855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration); 16397c32e8f8SLorenz Bauer if (!ret) 16407c32e8f8SLorenz Bauer ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); 16417c32e8f8SLorenz Bauer 16427c32e8f8SLorenz Bauer out: 16437c32e8f8SLorenz Bauer bpf_prog_array_free(progs); 16447c32e8f8SLorenz Bauer kfree(user_ctx); 16457c32e8f8SLorenz Bauer return ret; 16467c32e8f8SLorenz Bauer } 164779a7f8bdSAlexei Starovoitov 164879a7f8bdSAlexei Starovoitov int bpf_prog_test_run_syscall(struct bpf_prog *prog, 164979a7f8bdSAlexei Starovoitov const union bpf_attr *kattr, 165079a7f8bdSAlexei Starovoitov union bpf_attr __user *uattr) 165179a7f8bdSAlexei Starovoitov { 165279a7f8bdSAlexei Starovoitov void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 165379a7f8bdSAlexei Starovoitov __u32 ctx_size_in = kattr->test.ctx_size_in; 165479a7f8bdSAlexei Starovoitov void *ctx = NULL; 165579a7f8bdSAlexei Starovoitov u32 retval; 165679a7f8bdSAlexei Starovoitov int err = 0; 165779a7f8bdSAlexei Starovoitov 165879a7f8bdSAlexei Starovoitov /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ 165979a7f8bdSAlexei Starovoitov if (kattr->test.data_in || kattr->test.data_out || 166079a7f8bdSAlexei Starovoitov kattr->test.ctx_out || kattr->test.duration || 1661b530e9e1SToke Høiland-Jørgensen kattr->test.repeat || kattr->test.flags || 1662b530e9e1SToke Høiland-Jørgensen kattr->test.batch_size) 166379a7f8bdSAlexei Starovoitov return -EINVAL; 166479a7f8bdSAlexei Starovoitov 166579a7f8bdSAlexei Starovoitov if (ctx_size_in < prog->aux->max_ctx_offset || 166679a7f8bdSAlexei Starovoitov ctx_size_in > U16_MAX) 166779a7f8bdSAlexei Starovoitov return -EINVAL; 166879a7f8bdSAlexei Starovoitov 166979a7f8bdSAlexei Starovoitov if (ctx_size_in) { 1670db5b6a46SQing Wang ctx = memdup_user(ctx_in, ctx_size_in); 1671db5b6a46SQing Wang if (IS_ERR(ctx)) 1672db5b6a46SQing Wang return PTR_ERR(ctx); 167379a7f8bdSAlexei Starovoitov } 167487b7b533SYonghong Song 167587b7b533SYonghong Song rcu_read_lock_trace(); 167679a7f8bdSAlexei Starovoitov retval = bpf_prog_run_pin_on_cpu(prog, ctx); 167787b7b533SYonghong Song rcu_read_unlock_trace(); 167879a7f8bdSAlexei Starovoitov 167979a7f8bdSAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { 168079a7f8bdSAlexei Starovoitov err = -EFAULT; 168179a7f8bdSAlexei Starovoitov goto out; 168279a7f8bdSAlexei Starovoitov } 168379a7f8bdSAlexei Starovoitov if (ctx_size_in) 168479a7f8bdSAlexei Starovoitov if (copy_to_user(ctx_in, ctx, ctx_size_in)) 168579a7f8bdSAlexei Starovoitov err = -EFAULT; 168679a7f8bdSAlexei Starovoitov out: 168779a7f8bdSAlexei Starovoitov kfree(ctx); 168879a7f8bdSAlexei Starovoitov return err; 168979a7f8bdSAlexei Starovoitov } 1690b202d844SKumar Kartikeya Dwivedi 1691b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { 1692b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE, 1693a4703e31SKumar Kartikeya Dwivedi .set = &test_sk_check_kfunc_ids, 1694b202d844SKumar Kartikeya Dwivedi }; 1695b202d844SKumar Kartikeya Dwivedi 169605a945deSKumar Kartikeya Dwivedi BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids) 169705a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_ref_kfunc) 169805a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 169905a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_member) 170005a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_memb_release) 170105a945deSKumar Kartikeya Dwivedi 1702b202d844SKumar Kartikeya Dwivedi static int __init bpf_prog_test_run_init(void) 1703b202d844SKumar Kartikeya Dwivedi { 170405a945deSKumar Kartikeya Dwivedi const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = { 170505a945deSKumar Kartikeya Dwivedi { 170605a945deSKumar Kartikeya Dwivedi .btf_id = bpf_prog_test_dtor_kfunc_ids[0], 170705a945deSKumar Kartikeya Dwivedi .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1] 170805a945deSKumar Kartikeya Dwivedi }, 170905a945deSKumar Kartikeya Dwivedi { 171005a945deSKumar Kartikeya Dwivedi .btf_id = bpf_prog_test_dtor_kfunc_ids[2], 171105a945deSKumar Kartikeya Dwivedi .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3], 171205a945deSKumar Kartikeya Dwivedi }, 171305a945deSKumar Kartikeya Dwivedi }; 171405a945deSKumar Kartikeya Dwivedi int ret; 171505a945deSKumar Kartikeya Dwivedi 17165b481acaSBenjamin Tissoires ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set); 17175b481acaSBenjamin Tissoires ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); 17181f075262SKumar Kartikeya Dwivedi ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set); 1719fb66223aSBenjamin Tissoires ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set); 172005a945deSKumar Kartikeya Dwivedi return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, 172105a945deSKumar Kartikeya Dwivedi ARRAY_SIZE(bpf_prog_test_dtor_kfunc), 172205a945deSKumar Kartikeya Dwivedi THIS_MODULE); 1723b202d844SKumar Kartikeya Dwivedi } 1724b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_prog_test_run_init); 1725