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; 100b530e9e1SToke Høiland-Jørgensen struct xdp_frame frm; 101b530e9e1SToke Høiland-Jørgensen u8 data[]; 102b530e9e1SToke Høiland-Jørgensen }; 103b530e9e1SToke Høiland-Jørgensen 104b530e9e1SToke Høiland-Jørgensen struct xdp_test_data { 105b530e9e1SToke Høiland-Jørgensen struct xdp_buff *orig_ctx; 106b530e9e1SToke Høiland-Jørgensen struct xdp_rxq_info rxq; 107b530e9e1SToke Høiland-Jørgensen struct net_device *dev; 108b530e9e1SToke Høiland-Jørgensen struct page_pool *pp; 109b530e9e1SToke Høiland-Jørgensen struct xdp_frame **frames; 110b530e9e1SToke Høiland-Jørgensen struct sk_buff **skbs; 111b530e9e1SToke Høiland-Jørgensen u32 batch_size; 112b530e9e1SToke Høiland-Jørgensen u32 frame_cnt; 113b530e9e1SToke Høiland-Jørgensen }; 114b530e9e1SToke Høiland-Jørgensen 115b6f1f780SToke Høiland-Jørgensen #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head)) 116b530e9e1SToke Høiland-Jørgensen #define TEST_XDP_MAX_BATCH 256 117b530e9e1SToke Høiland-Jørgensen 118b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_init_page(struct page *page, void *arg) 119b530e9e1SToke Høiland-Jørgensen { 120b530e9e1SToke Høiland-Jørgensen struct xdp_page_head *head = phys_to_virt(page_to_phys(page)); 121b530e9e1SToke Høiland-Jørgensen struct xdp_buff *new_ctx, *orig_ctx; 122b530e9e1SToke Høiland-Jørgensen u32 headroom = XDP_PACKET_HEADROOM; 123b530e9e1SToke Høiland-Jørgensen struct xdp_test_data *xdp = arg; 124b530e9e1SToke Høiland-Jørgensen size_t frm_len, meta_len; 125b530e9e1SToke Høiland-Jørgensen struct xdp_frame *frm; 126b530e9e1SToke Høiland-Jørgensen void *data; 127b530e9e1SToke Høiland-Jørgensen 128b530e9e1SToke Høiland-Jørgensen orig_ctx = xdp->orig_ctx; 129b530e9e1SToke Høiland-Jørgensen frm_len = orig_ctx->data_end - orig_ctx->data_meta; 130b530e9e1SToke Høiland-Jørgensen meta_len = orig_ctx->data - orig_ctx->data_meta; 131b530e9e1SToke Høiland-Jørgensen headroom -= meta_len; 132b530e9e1SToke Høiland-Jørgensen 133b530e9e1SToke Høiland-Jørgensen new_ctx = &head->ctx; 134b530e9e1SToke Høiland-Jørgensen frm = &head->frm; 135b530e9e1SToke Høiland-Jørgensen data = &head->data; 136b530e9e1SToke Høiland-Jørgensen memcpy(data + headroom, orig_ctx->data_meta, frm_len); 137b530e9e1SToke Høiland-Jørgensen 138b530e9e1SToke Høiland-Jørgensen xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); 139b530e9e1SToke Høiland-Jørgensen xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); 140b530e9e1SToke Høiland-Jørgensen new_ctx->data = new_ctx->data_meta + meta_len; 141b530e9e1SToke Høiland-Jørgensen 142b530e9e1SToke Høiland-Jørgensen xdp_update_frame_from_buff(new_ctx, frm); 143b530e9e1SToke Høiland-Jørgensen frm->mem = new_ctx->rxq->mem; 144b530e9e1SToke Høiland-Jørgensen 145b530e9e1SToke Høiland-Jørgensen memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); 146b530e9e1SToke Høiland-Jørgensen } 147b530e9e1SToke Høiland-Jørgensen 148b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx) 149b530e9e1SToke Høiland-Jørgensen { 150b530e9e1SToke Høiland-Jørgensen struct xdp_mem_info mem = {}; 151b530e9e1SToke Høiland-Jørgensen struct page_pool *pp; 152b530e9e1SToke Høiland-Jørgensen int err = -ENOMEM; 153b530e9e1SToke Høiland-Jørgensen struct page_pool_params pp_params = { 154b530e9e1SToke Høiland-Jørgensen .order = 0, 155b530e9e1SToke Høiland-Jørgensen .flags = 0, 156b530e9e1SToke Høiland-Jørgensen .pool_size = xdp->batch_size, 157b530e9e1SToke Høiland-Jørgensen .nid = NUMA_NO_NODE, 158b530e9e1SToke Høiland-Jørgensen .init_callback = xdp_test_run_init_page, 159b530e9e1SToke Høiland-Jørgensen .init_arg = xdp, 160b530e9e1SToke Høiland-Jørgensen }; 161b530e9e1SToke Høiland-Jørgensen 162b530e9e1SToke Høiland-Jørgensen xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); 163b530e9e1SToke Høiland-Jørgensen if (!xdp->frames) 164b530e9e1SToke Høiland-Jørgensen return -ENOMEM; 165b530e9e1SToke Høiland-Jørgensen 166b530e9e1SToke Høiland-Jørgensen xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); 167b530e9e1SToke Høiland-Jørgensen if (!xdp->skbs) 168b530e9e1SToke Høiland-Jørgensen goto err_skbs; 169b530e9e1SToke Høiland-Jørgensen 170b530e9e1SToke Høiland-Jørgensen pp = page_pool_create(&pp_params); 171b530e9e1SToke Høiland-Jørgensen if (IS_ERR(pp)) { 172b530e9e1SToke Høiland-Jørgensen err = PTR_ERR(pp); 173b530e9e1SToke Høiland-Jørgensen goto err_pp; 174b530e9e1SToke Høiland-Jørgensen } 175b530e9e1SToke Høiland-Jørgensen 176b530e9e1SToke Høiland-Jørgensen /* will copy 'mem.id' into pp->xdp_mem_id */ 177b530e9e1SToke Høiland-Jørgensen err = xdp_reg_mem_model(&mem, MEM_TYPE_PAGE_POOL, pp); 178b530e9e1SToke Høiland-Jørgensen if (err) 179b530e9e1SToke Høiland-Jørgensen goto err_mmodel; 180b530e9e1SToke Høiland-Jørgensen 181b530e9e1SToke Høiland-Jørgensen xdp->pp = pp; 182b530e9e1SToke Høiland-Jørgensen 183b530e9e1SToke Høiland-Jørgensen /* We create a 'fake' RXQ referencing the original dev, but with an 184b530e9e1SToke Høiland-Jørgensen * xdp_mem_info pointing to our page_pool 185b530e9e1SToke Høiland-Jørgensen */ 186b530e9e1SToke Høiland-Jørgensen xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0); 187b530e9e1SToke Høiland-Jørgensen xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL; 188b530e9e1SToke Høiland-Jørgensen xdp->rxq.mem.id = pp->xdp_mem_id; 189b530e9e1SToke Høiland-Jørgensen xdp->dev = orig_ctx->rxq->dev; 190b530e9e1SToke Høiland-Jørgensen xdp->orig_ctx = orig_ctx; 191b530e9e1SToke Høiland-Jørgensen 192b530e9e1SToke Høiland-Jørgensen return 0; 193b530e9e1SToke Høiland-Jørgensen 194b530e9e1SToke Høiland-Jørgensen err_mmodel: 195b530e9e1SToke Høiland-Jørgensen page_pool_destroy(pp); 196b530e9e1SToke Høiland-Jørgensen err_pp: 197743bec1bSYihao Han kvfree(xdp->skbs); 198b530e9e1SToke Høiland-Jørgensen err_skbs: 199743bec1bSYihao Han kvfree(xdp->frames); 200b530e9e1SToke Høiland-Jørgensen return err; 201b530e9e1SToke Høiland-Jørgensen } 202b530e9e1SToke Høiland-Jørgensen 203b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_teardown(struct xdp_test_data *xdp) 204b530e9e1SToke Høiland-Jørgensen { 205b530e9e1SToke Høiland-Jørgensen page_pool_destroy(xdp->pp); 206b530e9e1SToke Høiland-Jørgensen kfree(xdp->frames); 207b530e9e1SToke Høiland-Jørgensen kfree(xdp->skbs); 208b530e9e1SToke Høiland-Jørgensen } 209b530e9e1SToke Høiland-Jørgensen 210b530e9e1SToke Høiland-Jørgensen static bool ctx_was_changed(struct xdp_page_head *head) 211b530e9e1SToke Høiland-Jørgensen { 212b530e9e1SToke Høiland-Jørgensen return head->orig_ctx.data != head->ctx.data || 213b530e9e1SToke Høiland-Jørgensen head->orig_ctx.data_meta != head->ctx.data_meta || 214b530e9e1SToke Høiland-Jørgensen head->orig_ctx.data_end != head->ctx.data_end; 215b530e9e1SToke Høiland-Jørgensen } 216b530e9e1SToke Høiland-Jørgensen 217b530e9e1SToke Høiland-Jørgensen static void reset_ctx(struct xdp_page_head *head) 218b530e9e1SToke Høiland-Jørgensen { 219b530e9e1SToke Høiland-Jørgensen if (likely(!ctx_was_changed(head))) 220b530e9e1SToke Høiland-Jørgensen return; 221b530e9e1SToke Høiland-Jørgensen 222b530e9e1SToke Høiland-Jørgensen head->ctx.data = head->orig_ctx.data; 223b530e9e1SToke Høiland-Jørgensen head->ctx.data_meta = head->orig_ctx.data_meta; 224b530e9e1SToke Høiland-Jørgensen head->ctx.data_end = head->orig_ctx.data_end; 225b530e9e1SToke Høiland-Jørgensen xdp_update_frame_from_buff(&head->ctx, &head->frm); 226b530e9e1SToke Høiland-Jørgensen } 227b530e9e1SToke Høiland-Jørgensen 228b530e9e1SToke Høiland-Jørgensen static int xdp_recv_frames(struct xdp_frame **frames, int nframes, 229b530e9e1SToke Høiland-Jørgensen struct sk_buff **skbs, 230b530e9e1SToke Høiland-Jørgensen struct net_device *dev) 231b530e9e1SToke Høiland-Jørgensen { 232b530e9e1SToke Høiland-Jørgensen gfp_t gfp = __GFP_ZERO | GFP_ATOMIC; 233b530e9e1SToke Høiland-Jørgensen int i, n; 234b530e9e1SToke Høiland-Jørgensen LIST_HEAD(list); 235b530e9e1SToke Høiland-Jørgensen 236b530e9e1SToke Høiland-Jørgensen n = kmem_cache_alloc_bulk(skbuff_head_cache, gfp, nframes, (void **)skbs); 237b530e9e1SToke Høiland-Jørgensen if (unlikely(n == 0)) { 238b530e9e1SToke Høiland-Jørgensen for (i = 0; i < nframes; i++) 239b530e9e1SToke Høiland-Jørgensen xdp_return_frame(frames[i]); 240b530e9e1SToke Høiland-Jørgensen return -ENOMEM; 241b530e9e1SToke Høiland-Jørgensen } 242b530e9e1SToke Høiland-Jørgensen 243b530e9e1SToke Høiland-Jørgensen for (i = 0; i < nframes; i++) { 244b530e9e1SToke Høiland-Jørgensen struct xdp_frame *xdpf = frames[i]; 245b530e9e1SToke Høiland-Jørgensen struct sk_buff *skb = skbs[i]; 246b530e9e1SToke Høiland-Jørgensen 247b530e9e1SToke Høiland-Jørgensen skb = __xdp_build_skb_from_frame(xdpf, skb, dev); 248b530e9e1SToke Høiland-Jørgensen if (!skb) { 249b530e9e1SToke Høiland-Jørgensen xdp_return_frame(xdpf); 250b530e9e1SToke Høiland-Jørgensen continue; 251b530e9e1SToke Høiland-Jørgensen } 252b530e9e1SToke Høiland-Jørgensen 253b530e9e1SToke Høiland-Jørgensen list_add_tail(&skb->list, &list); 254b530e9e1SToke Høiland-Jørgensen } 255b530e9e1SToke Høiland-Jørgensen netif_receive_skb_list(&list); 256b530e9e1SToke Høiland-Jørgensen 257b530e9e1SToke Høiland-Jørgensen return 0; 258b530e9e1SToke Høiland-Jørgensen } 259b530e9e1SToke Høiland-Jørgensen 260b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, 261b530e9e1SToke Høiland-Jørgensen u32 repeat) 262b530e9e1SToke Høiland-Jørgensen { 263b530e9e1SToke Høiland-Jørgensen struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 264b530e9e1SToke Høiland-Jørgensen int err = 0, act, ret, i, nframes = 0, batch_sz; 265b530e9e1SToke Høiland-Jørgensen struct xdp_frame **frames = xdp->frames; 266b530e9e1SToke Høiland-Jørgensen struct xdp_page_head *head; 267b530e9e1SToke Høiland-Jørgensen struct xdp_frame *frm; 268b530e9e1SToke Høiland-Jørgensen bool redirect = false; 269b530e9e1SToke Høiland-Jørgensen struct xdp_buff *ctx; 270b530e9e1SToke Høiland-Jørgensen struct page *page; 271b530e9e1SToke Høiland-Jørgensen 272b530e9e1SToke Høiland-Jørgensen batch_sz = min_t(u32, repeat, xdp->batch_size); 273b530e9e1SToke Høiland-Jørgensen 274b530e9e1SToke Høiland-Jørgensen local_bh_disable(); 275b530e9e1SToke Høiland-Jørgensen xdp_set_return_frame_no_direct(); 276b530e9e1SToke Høiland-Jørgensen 277b530e9e1SToke Høiland-Jørgensen for (i = 0; i < batch_sz; i++) { 278b530e9e1SToke Høiland-Jørgensen page = page_pool_dev_alloc_pages(xdp->pp); 279b530e9e1SToke Høiland-Jørgensen if (!page) { 280b530e9e1SToke Høiland-Jørgensen err = -ENOMEM; 281b530e9e1SToke Høiland-Jørgensen goto out; 282b530e9e1SToke Høiland-Jørgensen } 283b530e9e1SToke Høiland-Jørgensen 284b530e9e1SToke Høiland-Jørgensen head = phys_to_virt(page_to_phys(page)); 285b530e9e1SToke Høiland-Jørgensen reset_ctx(head); 286b530e9e1SToke Høiland-Jørgensen ctx = &head->ctx; 287b530e9e1SToke Høiland-Jørgensen frm = &head->frm; 288b530e9e1SToke Høiland-Jørgensen xdp->frame_cnt++; 289b530e9e1SToke Høiland-Jørgensen 290b530e9e1SToke Høiland-Jørgensen act = bpf_prog_run_xdp(prog, ctx); 291b530e9e1SToke Høiland-Jørgensen 292b530e9e1SToke Høiland-Jørgensen /* if program changed pkt bounds we need to update the xdp_frame */ 293b530e9e1SToke Høiland-Jørgensen if (unlikely(ctx_was_changed(head))) { 294b530e9e1SToke Høiland-Jørgensen ret = xdp_update_frame_from_buff(ctx, frm); 295b530e9e1SToke Høiland-Jørgensen if (ret) { 296b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 297b530e9e1SToke Høiland-Jørgensen continue; 298b530e9e1SToke Høiland-Jørgensen } 299b530e9e1SToke Høiland-Jørgensen } 300b530e9e1SToke Høiland-Jørgensen 301b530e9e1SToke Høiland-Jørgensen switch (act) { 302b530e9e1SToke Høiland-Jørgensen case XDP_TX: 303b530e9e1SToke Høiland-Jørgensen /* we can't do a real XDP_TX since we're not in the 304b530e9e1SToke Høiland-Jørgensen * driver, so turn it into a REDIRECT back to the same 305b530e9e1SToke Høiland-Jørgensen * index 306b530e9e1SToke Høiland-Jørgensen */ 307b530e9e1SToke Høiland-Jørgensen ri->tgt_index = xdp->dev->ifindex; 308b530e9e1SToke Høiland-Jørgensen ri->map_id = INT_MAX; 309b530e9e1SToke Høiland-Jørgensen ri->map_type = BPF_MAP_TYPE_UNSPEC; 310b530e9e1SToke Høiland-Jørgensen fallthrough; 311b530e9e1SToke Høiland-Jørgensen case XDP_REDIRECT: 312b530e9e1SToke Høiland-Jørgensen redirect = true; 313b530e9e1SToke Høiland-Jørgensen ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog); 314b530e9e1SToke Høiland-Jørgensen if (ret) 315b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 316b530e9e1SToke Høiland-Jørgensen break; 317b530e9e1SToke Høiland-Jørgensen case XDP_PASS: 318b530e9e1SToke Høiland-Jørgensen frames[nframes++] = frm; 319b530e9e1SToke Høiland-Jørgensen break; 320b530e9e1SToke Høiland-Jørgensen default: 321b530e9e1SToke Høiland-Jørgensen bpf_warn_invalid_xdp_action(NULL, prog, act); 322b530e9e1SToke Høiland-Jørgensen fallthrough; 323b530e9e1SToke Høiland-Jørgensen case XDP_DROP: 324b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 325b530e9e1SToke Høiland-Jørgensen break; 326b530e9e1SToke Høiland-Jørgensen } 327b530e9e1SToke Høiland-Jørgensen } 328b530e9e1SToke Høiland-Jørgensen 329b530e9e1SToke Høiland-Jørgensen out: 330b530e9e1SToke Høiland-Jørgensen if (redirect) 331b530e9e1SToke Høiland-Jørgensen xdp_do_flush(); 332b530e9e1SToke Høiland-Jørgensen if (nframes) { 333b530e9e1SToke Høiland-Jørgensen ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev); 334b530e9e1SToke Høiland-Jørgensen if (ret) 335b530e9e1SToke Høiland-Jørgensen err = ret; 336b530e9e1SToke Høiland-Jørgensen } 337b530e9e1SToke Høiland-Jørgensen 338b530e9e1SToke Høiland-Jørgensen xdp_clear_return_frame_no_direct(); 339b530e9e1SToke Høiland-Jørgensen local_bh_enable(); 340b530e9e1SToke Høiland-Jørgensen return err; 341b530e9e1SToke Høiland-Jørgensen } 342b530e9e1SToke Høiland-Jørgensen 343b530e9e1SToke Høiland-Jørgensen static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx, 344b530e9e1SToke Høiland-Jørgensen u32 repeat, u32 batch_size, u32 *time) 345b530e9e1SToke Høiland-Jørgensen 346b530e9e1SToke Høiland-Jørgensen { 347b530e9e1SToke Høiland-Jørgensen struct xdp_test_data xdp = { .batch_size = batch_size }; 348b530e9e1SToke Høiland-Jørgensen struct bpf_test_timer t = { .mode = NO_MIGRATE }; 349b530e9e1SToke Høiland-Jørgensen int ret; 350b530e9e1SToke Høiland-Jørgensen 351b530e9e1SToke Høiland-Jørgensen if (!repeat) 352b530e9e1SToke Høiland-Jørgensen repeat = 1; 353b530e9e1SToke Høiland-Jørgensen 354b530e9e1SToke Høiland-Jørgensen ret = xdp_test_run_setup(&xdp, ctx); 355b530e9e1SToke Høiland-Jørgensen if (ret) 356b530e9e1SToke Høiland-Jørgensen return ret; 357b530e9e1SToke Høiland-Jørgensen 358b530e9e1SToke Høiland-Jørgensen bpf_test_timer_enter(&t); 359b530e9e1SToke Høiland-Jørgensen do { 360b530e9e1SToke Høiland-Jørgensen xdp.frame_cnt = 0; 361b530e9e1SToke Høiland-Jørgensen ret = xdp_test_run_batch(&xdp, prog, repeat - t.i); 362b530e9e1SToke Høiland-Jørgensen if (unlikely(ret < 0)) 363b530e9e1SToke Høiland-Jørgensen break; 364b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time)); 365b530e9e1SToke Høiland-Jørgensen bpf_test_timer_leave(&t); 366b530e9e1SToke Høiland-Jørgensen 367b530e9e1SToke Høiland-Jørgensen xdp_test_run_teardown(&xdp); 368b530e9e1SToke Høiland-Jørgensen return ret; 369b530e9e1SToke Høiland-Jørgensen } 370b530e9e1SToke Høiland-Jørgensen 371df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, 372f23c4b39SBjörn Töpel u32 *retval, u32 *time, bool xdp) 3731cf1cae9SAlexei Starovoitov { 374c7603cfaSAndrii Nakryiko struct bpf_prog_array_item item = {.prog = prog}; 375c7603cfaSAndrii Nakryiko struct bpf_run_ctx *old_ctx; 376c7603cfaSAndrii Nakryiko struct bpf_cg_run_ctx run_ctx; 377607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_MIGRATE }; 3788bad74f9SRoman Gushchin enum bpf_cgroup_storage_type stype; 379607b9cc9SLorenz Bauer int ret; 3801cf1cae9SAlexei Starovoitov 3818bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) { 382c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype); 383c7603cfaSAndrii Nakryiko if (IS_ERR(item.cgroup_storage[stype])) { 384c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = NULL; 3858bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 386c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 3878bad74f9SRoman Gushchin return -ENOMEM; 3888bad74f9SRoman Gushchin } 3898bad74f9SRoman Gushchin } 390f42ee093SRoman Gushchin 3911cf1cae9SAlexei Starovoitov if (!repeat) 3921cf1cae9SAlexei Starovoitov repeat = 1; 393df1a2cb7SStanislav Fomichev 394607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 395c7603cfaSAndrii Nakryiko old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 396607b9cc9SLorenz Bauer do { 397c7603cfaSAndrii Nakryiko run_ctx.prog_item = &item; 398f23c4b39SBjörn Töpel if (xdp) 399f23c4b39SBjörn Töpel *retval = bpf_prog_run_xdp(prog, ctx); 400f23c4b39SBjörn Töpel else 401fb7dd8bcSAndrii Nakryiko *retval = bpf_prog_run(prog, ctx); 402b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time)); 403c7603cfaSAndrii Nakryiko bpf_reset_run_ctx(old_ctx); 404607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 4051cf1cae9SAlexei Starovoitov 4068bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 407c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 408f42ee093SRoman Gushchin 409df1a2cb7SStanislav Fomichev return ret; 4101cf1cae9SAlexei Starovoitov } 4111cf1cae9SAlexei Starovoitov 41278e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr, 41378e52272SDavid Miller union bpf_attr __user *uattr, const void *data, 4147855e0dbSLorenzo Bianconi struct skb_shared_info *sinfo, u32 size, 4157855e0dbSLorenzo Bianconi u32 retval, u32 duration) 4161cf1cae9SAlexei Starovoitov { 41778e52272SDavid Miller void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 4181cf1cae9SAlexei Starovoitov int err = -EFAULT; 419b5a36b1eSLorenz Bauer u32 copy_size = size; 4201cf1cae9SAlexei Starovoitov 421b5a36b1eSLorenz Bauer /* Clamp copy if the user has provided a size hint, but copy the full 422b5a36b1eSLorenz Bauer * buffer if not to retain old behaviour. 423b5a36b1eSLorenz Bauer */ 424b5a36b1eSLorenz Bauer if (kattr->test.data_size_out && 425b5a36b1eSLorenz Bauer copy_size > kattr->test.data_size_out) { 426b5a36b1eSLorenz Bauer copy_size = kattr->test.data_size_out; 427b5a36b1eSLorenz Bauer err = -ENOSPC; 428b5a36b1eSLorenz Bauer } 429b5a36b1eSLorenz Bauer 4307855e0dbSLorenzo Bianconi if (data_out) { 4317855e0dbSLorenzo Bianconi int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size; 4327855e0dbSLorenzo Bianconi 433530e214cSStanislav Fomichev if (len < 0) { 434530e214cSStanislav Fomichev err = -ENOSPC; 435530e214cSStanislav Fomichev goto out; 436530e214cSStanislav Fomichev } 437530e214cSStanislav Fomichev 4387855e0dbSLorenzo Bianconi if (copy_to_user(data_out, data, len)) 4391cf1cae9SAlexei Starovoitov goto out; 4407855e0dbSLorenzo Bianconi 4417855e0dbSLorenzo Bianconi if (sinfo) { 4425d1e9f43SStanislav Fomichev int i, offset = len; 4435d1e9f43SStanislav Fomichev u32 data_len; 4447855e0dbSLorenzo Bianconi 4457855e0dbSLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) { 4467855e0dbSLorenzo Bianconi skb_frag_t *frag = &sinfo->frags[i]; 4477855e0dbSLorenzo Bianconi 4487855e0dbSLorenzo Bianconi if (offset >= copy_size) { 4497855e0dbSLorenzo Bianconi err = -ENOSPC; 4507855e0dbSLorenzo Bianconi break; 4517855e0dbSLorenzo Bianconi } 4527855e0dbSLorenzo Bianconi 4535d1e9f43SStanislav Fomichev data_len = min_t(u32, copy_size - offset, 4547855e0dbSLorenzo Bianconi skb_frag_size(frag)); 4557855e0dbSLorenzo Bianconi 4567855e0dbSLorenzo Bianconi if (copy_to_user(data_out + offset, 4577855e0dbSLorenzo Bianconi skb_frag_address(frag), 4587855e0dbSLorenzo Bianconi data_len)) 4597855e0dbSLorenzo Bianconi goto out; 4607855e0dbSLorenzo Bianconi 4617855e0dbSLorenzo Bianconi offset += data_len; 4627855e0dbSLorenzo Bianconi } 4637855e0dbSLorenzo Bianconi } 4647855e0dbSLorenzo Bianconi } 4657855e0dbSLorenzo Bianconi 4661cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 4671cf1cae9SAlexei Starovoitov goto out; 4681cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 4691cf1cae9SAlexei Starovoitov goto out; 4701cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 4711cf1cae9SAlexei Starovoitov goto out; 472b5a36b1eSLorenz Bauer if (err != -ENOSPC) 4731cf1cae9SAlexei Starovoitov err = 0; 4741cf1cae9SAlexei Starovoitov out: 475e950e843SMatt Mullins trace_bpf_test_finish(&err); 4761cf1cae9SAlexei Starovoitov return err; 4771cf1cae9SAlexei Starovoitov } 4781cf1cae9SAlexei Starovoitov 479faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of 480faeb2dceSAlexei Starovoitov * architecture dependent calling conventions. 7+ can be supported in the 481faeb2dceSAlexei Starovoitov * future. 482faeb2dceSAlexei Starovoitov */ 483e9ff9d52SJean-Philippe Menil __diag_push(); 4840b206c6dSKumar Kartikeya Dwivedi __diag_ignore_all("-Wmissing-prototypes", 485e9ff9d52SJean-Philippe Menil "Global functions as their definitions will be in vmlinux BTF"); 486faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test1(int a) 487faeb2dceSAlexei Starovoitov { 488faeb2dceSAlexei Starovoitov return a + 1; 489faeb2dceSAlexei Starovoitov } 49046565696SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(bpf_fentry_test1); 49146565696SKumar Kartikeya Dwivedi ALLOW_ERROR_INJECTION(bpf_fentry_test1, ERRNO); 492faeb2dceSAlexei Starovoitov 493faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b) 494faeb2dceSAlexei Starovoitov { 495faeb2dceSAlexei Starovoitov return a + b; 496faeb2dceSAlexei Starovoitov } 497faeb2dceSAlexei Starovoitov 498faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c) 499faeb2dceSAlexei Starovoitov { 500faeb2dceSAlexei Starovoitov return a + b + c; 501faeb2dceSAlexei Starovoitov } 502faeb2dceSAlexei Starovoitov 503faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) 504faeb2dceSAlexei Starovoitov { 505faeb2dceSAlexei Starovoitov return (long)a + b + c + d; 506faeb2dceSAlexei Starovoitov } 507faeb2dceSAlexei Starovoitov 508faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) 509faeb2dceSAlexei Starovoitov { 510faeb2dceSAlexei Starovoitov return a + (long)b + c + d + e; 511faeb2dceSAlexei Starovoitov } 512faeb2dceSAlexei Starovoitov 513faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) 514faeb2dceSAlexei Starovoitov { 515faeb2dceSAlexei Starovoitov return a + (long)b + c + d + (long)e + f; 516faeb2dceSAlexei Starovoitov } 517faeb2dceSAlexei Starovoitov 518d923021cSYonghong Song struct bpf_fentry_test_t { 519d923021cSYonghong Song struct bpf_fentry_test_t *a; 520d923021cSYonghong Song }; 521d923021cSYonghong Song 522d923021cSYonghong Song int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg) 523d923021cSYonghong Song { 524d923021cSYonghong Song return (long)arg; 525d923021cSYonghong Song } 526d923021cSYonghong Song 527d923021cSYonghong Song int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg) 528d923021cSYonghong Song { 529d923021cSYonghong Song return (long)arg->a; 530d923021cSYonghong Song } 531d923021cSYonghong Song 5323d08b6f2SKP Singh int noinline bpf_modify_return_test(int a, int *b) 5333d08b6f2SKP Singh { 5343d08b6f2SKP Singh *b += 1; 5353d08b6f2SKP Singh return a + *b; 5363d08b6f2SKP Singh } 5377bd1590dSMartin KaFai Lau 5387bd1590dSMartin KaFai Lau u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d) 5397bd1590dSMartin KaFai Lau { 5407bd1590dSMartin KaFai Lau return a + b + c + d; 5417bd1590dSMartin KaFai Lau } 5427bd1590dSMartin KaFai Lau 5437bd1590dSMartin KaFai Lau int noinline bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b) 5447bd1590dSMartin KaFai Lau { 5457bd1590dSMartin KaFai Lau return a + b; 5467bd1590dSMartin KaFai Lau } 5477bd1590dSMartin KaFai Lau 5487bd1590dSMartin KaFai Lau struct sock * noinline bpf_kfunc_call_test3(struct sock *sk) 5497bd1590dSMartin KaFai Lau { 5507bd1590dSMartin KaFai Lau return sk; 5517bd1590dSMartin KaFai Lau } 5527bd1590dSMartin KaFai Lau 5538218ccb5SKumar Kartikeya Dwivedi struct prog_test_member { 5548218ccb5SKumar Kartikeya Dwivedi u64 c; 5558218ccb5SKumar Kartikeya Dwivedi }; 5568218ccb5SKumar Kartikeya Dwivedi 557c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc { 558c1ff181fSKumar Kartikeya Dwivedi int a; 559c1ff181fSKumar Kartikeya Dwivedi int b; 5608218ccb5SKumar Kartikeya Dwivedi struct prog_test_member memb; 561c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc *next; 562c1ff181fSKumar Kartikeya Dwivedi }; 563c1ff181fSKumar Kartikeya Dwivedi 564c1ff181fSKumar Kartikeya Dwivedi static struct prog_test_ref_kfunc prog_test_struct = { 565c1ff181fSKumar Kartikeya Dwivedi .a = 42, 566c1ff181fSKumar Kartikeya Dwivedi .b = 108, 567c1ff181fSKumar Kartikeya Dwivedi .next = &prog_test_struct, 568c1ff181fSKumar Kartikeya Dwivedi }; 569c1ff181fSKumar Kartikeya Dwivedi 570c1ff181fSKumar Kartikeya Dwivedi noinline struct prog_test_ref_kfunc * 571c1ff181fSKumar Kartikeya Dwivedi bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) 572c1ff181fSKumar Kartikeya Dwivedi { 573c1ff181fSKumar Kartikeya Dwivedi /* randomly return NULL */ 574c1ff181fSKumar Kartikeya Dwivedi if (get_jiffies_64() % 2) 575c1ff181fSKumar Kartikeya Dwivedi return NULL; 576c1ff181fSKumar Kartikeya Dwivedi return &prog_test_struct; 577c1ff181fSKumar Kartikeya Dwivedi } 578c1ff181fSKumar Kartikeya Dwivedi 579c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) 580c1ff181fSKumar Kartikeya Dwivedi { 581c1ff181fSKumar Kartikeya Dwivedi } 582c1ff181fSKumar Kartikeya Dwivedi 5838218ccb5SKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p) 5848218ccb5SKumar Kartikeya Dwivedi { 5858218ccb5SKumar Kartikeya Dwivedi } 5868218ccb5SKumar Kartikeya Dwivedi 587*05a945deSKumar Kartikeya Dwivedi noinline struct prog_test_ref_kfunc * 588*05a945deSKumar Kartikeya Dwivedi bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **p, int a, int b) 589*05a945deSKumar Kartikeya Dwivedi { 590*05a945deSKumar Kartikeya Dwivedi return &prog_test_struct; 591*05a945deSKumar Kartikeya Dwivedi } 592*05a945deSKumar Kartikeya Dwivedi 593c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 { 594c1ff181fSKumar Kartikeya Dwivedi int x0; 595c1ff181fSKumar Kartikeya Dwivedi struct { 596c1ff181fSKumar Kartikeya Dwivedi int x1; 597c1ff181fSKumar Kartikeya Dwivedi struct { 598c1ff181fSKumar Kartikeya Dwivedi int x2; 599c1ff181fSKumar Kartikeya Dwivedi struct { 600c1ff181fSKumar Kartikeya Dwivedi int x3; 601c1ff181fSKumar Kartikeya Dwivedi }; 602c1ff181fSKumar Kartikeya Dwivedi }; 603c1ff181fSKumar Kartikeya Dwivedi }; 604c1ff181fSKumar Kartikeya Dwivedi }; 605c1ff181fSKumar Kartikeya Dwivedi 606c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass2 { 607c1ff181fSKumar Kartikeya Dwivedi int len; 608c1ff181fSKumar Kartikeya Dwivedi short arr1[4]; 609c1ff181fSKumar Kartikeya Dwivedi struct { 610c1ff181fSKumar Kartikeya Dwivedi char arr2[4]; 611c1ff181fSKumar Kartikeya Dwivedi unsigned long arr3[8]; 612c1ff181fSKumar Kartikeya Dwivedi } x; 613c1ff181fSKumar Kartikeya Dwivedi }; 614c1ff181fSKumar Kartikeya Dwivedi 615c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail1 { 616c1ff181fSKumar Kartikeya Dwivedi void *p; 617c1ff181fSKumar Kartikeya Dwivedi int x; 618c1ff181fSKumar Kartikeya Dwivedi }; 619c1ff181fSKumar Kartikeya Dwivedi 620c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail2 { 621c1ff181fSKumar Kartikeya Dwivedi int x8; 622c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 x; 623c1ff181fSKumar Kartikeya Dwivedi }; 624c1ff181fSKumar Kartikeya Dwivedi 625c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail3 { 626c1ff181fSKumar Kartikeya Dwivedi int len; 627c1ff181fSKumar Kartikeya Dwivedi char arr1[2]; 628ed8bb032Skernel test robot char arr2[]; 629c1ff181fSKumar Kartikeya Dwivedi }; 630c1ff181fSKumar Kartikeya Dwivedi 631c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) 632c1ff181fSKumar Kartikeya Dwivedi { 633c1ff181fSKumar Kartikeya Dwivedi } 634c1ff181fSKumar Kartikeya Dwivedi 635c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) 636c1ff181fSKumar Kartikeya Dwivedi { 637c1ff181fSKumar Kartikeya Dwivedi } 638c1ff181fSKumar Kartikeya Dwivedi 639c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) 640c1ff181fSKumar Kartikeya Dwivedi { 641c1ff181fSKumar Kartikeya Dwivedi } 642c1ff181fSKumar Kartikeya Dwivedi 643c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) 644c1ff181fSKumar Kartikeya Dwivedi { 645c1ff181fSKumar Kartikeya Dwivedi } 646c1ff181fSKumar Kartikeya Dwivedi 647c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p) 648c1ff181fSKumar Kartikeya Dwivedi { 649c1ff181fSKumar Kartikeya Dwivedi } 650c1ff181fSKumar Kartikeya Dwivedi 651c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p) 652c1ff181fSKumar Kartikeya Dwivedi { 653c1ff181fSKumar Kartikeya Dwivedi } 654c1ff181fSKumar Kartikeya Dwivedi 655c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz) 656c1ff181fSKumar Kartikeya Dwivedi { 657c1ff181fSKumar Kartikeya Dwivedi } 658c1ff181fSKumar Kartikeya Dwivedi 659c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len) 660c1ff181fSKumar Kartikeya Dwivedi { 661c1ff181fSKumar Kartikeya Dwivedi } 662c1ff181fSKumar Kartikeya Dwivedi 663c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) 664c1ff181fSKumar Kartikeya Dwivedi { 665c1ff181fSKumar Kartikeya Dwivedi } 666c1ff181fSKumar Kartikeya Dwivedi 667e9ff9d52SJean-Philippe Menil __diag_pop(); 6683d08b6f2SKP Singh 6693d08b6f2SKP Singh ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); 6703d08b6f2SKP Singh 671b202d844SKumar Kartikeya Dwivedi BTF_SET_START(test_sk_check_kfunc_ids) 6727bd1590dSMartin KaFai Lau BTF_ID(func, bpf_kfunc_call_test1) 6737bd1590dSMartin KaFai Lau BTF_ID(func, bpf_kfunc_call_test2) 6747bd1590dSMartin KaFai Lau BTF_ID(func, bpf_kfunc_call_test3) 675c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_acquire) 676c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 6778218ccb5SKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_memb_release) 678*05a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_kptr_get) 679c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_pass_ctx) 680c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_pass1) 681c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_pass2) 682c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_fail1) 683c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_fail2) 684c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_fail3) 685c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_mem_len_pass1) 686c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_mem_len_fail1) 687c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_mem_len_fail2) 688b202d844SKumar Kartikeya Dwivedi BTF_SET_END(test_sk_check_kfunc_ids) 6897bd1590dSMartin KaFai Lau 690c1ff181fSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_acquire_kfunc_ids) 691c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_acquire) 692*05a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_kptr_get) 693c1ff181fSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_acquire_kfunc_ids) 694c1ff181fSKumar Kartikeya Dwivedi 695c1ff181fSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_release_kfunc_ids) 696c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 6978218ccb5SKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_memb_release) 698c1ff181fSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_release_kfunc_ids) 699c1ff181fSKumar Kartikeya Dwivedi 700c1ff181fSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_ret_null_kfunc_ids) 701c1ff181fSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_acquire) 702*05a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_kptr_get) 703c1ff181fSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_ret_null_kfunc_ids) 704c1ff181fSKumar Kartikeya Dwivedi 705*05a945deSKumar Kartikeya Dwivedi BTF_SET_START(test_sk_kptr_acquire_kfunc_ids) 706*05a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_kptr_get) 707*05a945deSKumar Kartikeya Dwivedi BTF_SET_END(test_sk_kptr_acquire_kfunc_ids) 708*05a945deSKumar Kartikeya Dwivedi 709be3d72a2SLorenzo Bianconi static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, 710be3d72a2SLorenzo Bianconi u32 size, u32 headroom, u32 tailroom) 7111cf1cae9SAlexei Starovoitov { 7121cf1cae9SAlexei Starovoitov void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 7131cf1cae9SAlexei Starovoitov void *data; 7141cf1cae9SAlexei Starovoitov 7151cf1cae9SAlexei Starovoitov if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) 7161cf1cae9SAlexei Starovoitov return ERR_PTR(-EINVAL); 7171cf1cae9SAlexei Starovoitov 718d800bad6SJesper Dangaard Brouer if (user_size > size) 719d800bad6SJesper Dangaard Brouer return ERR_PTR(-EMSGSIZE); 720d800bad6SJesper Dangaard Brouer 7211cf1cae9SAlexei Starovoitov data = kzalloc(size + headroom + tailroom, GFP_USER); 7221cf1cae9SAlexei Starovoitov if (!data) 7231cf1cae9SAlexei Starovoitov return ERR_PTR(-ENOMEM); 7241cf1cae9SAlexei Starovoitov 725d800bad6SJesper Dangaard Brouer if (copy_from_user(data + headroom, data_in, user_size)) { 7261cf1cae9SAlexei Starovoitov kfree(data); 7271cf1cae9SAlexei Starovoitov return ERR_PTR(-EFAULT); 7281cf1cae9SAlexei Starovoitov } 729da00d2f1SKP Singh 730da00d2f1SKP Singh return data; 731da00d2f1SKP Singh } 732da00d2f1SKP Singh 733da00d2f1SKP Singh int bpf_prog_test_run_tracing(struct bpf_prog *prog, 734da00d2f1SKP Singh const union bpf_attr *kattr, 735da00d2f1SKP Singh union bpf_attr __user *uattr) 736da00d2f1SKP Singh { 737d923021cSYonghong Song struct bpf_fentry_test_t arg = {}; 7383d08b6f2SKP Singh u16 side_effect = 0, ret = 0; 7393d08b6f2SKP Singh int b = 2, err = -EFAULT; 7403d08b6f2SKP Singh u32 retval = 0; 741da00d2f1SKP Singh 742b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 7431b4d60ecSSong Liu return -EINVAL; 7441b4d60ecSSong Liu 745da00d2f1SKP Singh switch (prog->expected_attach_type) { 746da00d2f1SKP Singh case BPF_TRACE_FENTRY: 747da00d2f1SKP Singh case BPF_TRACE_FEXIT: 748faeb2dceSAlexei Starovoitov if (bpf_fentry_test1(1) != 2 || 749faeb2dceSAlexei Starovoitov bpf_fentry_test2(2, 3) != 5 || 750faeb2dceSAlexei Starovoitov bpf_fentry_test3(4, 5, 6) != 15 || 751faeb2dceSAlexei Starovoitov bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || 752faeb2dceSAlexei Starovoitov bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || 753d923021cSYonghong Song bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || 754d923021cSYonghong Song bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || 755d923021cSYonghong Song bpf_fentry_test8(&arg) != 0) 756da00d2f1SKP Singh goto out; 757da00d2f1SKP Singh break; 7583d08b6f2SKP Singh case BPF_MODIFY_RETURN: 7593d08b6f2SKP Singh ret = bpf_modify_return_test(1, &b); 7603d08b6f2SKP Singh if (b != 2) 7613d08b6f2SKP Singh side_effect = 1; 7623d08b6f2SKP Singh break; 763da00d2f1SKP Singh default: 764da00d2f1SKP Singh goto out; 765a25ecd9dSColin Ian King } 766da00d2f1SKP Singh 7673d08b6f2SKP Singh retval = ((u32)side_effect << 16) | ret; 7683d08b6f2SKP Singh if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 7693d08b6f2SKP Singh goto out; 7703d08b6f2SKP Singh 771da00d2f1SKP Singh err = 0; 772da00d2f1SKP Singh out: 773da00d2f1SKP Singh trace_bpf_test_finish(&err); 774da00d2f1SKP Singh return err; 7751cf1cae9SAlexei Starovoitov } 7761cf1cae9SAlexei Starovoitov 7771b4d60ecSSong Liu struct bpf_raw_tp_test_run_info { 7781b4d60ecSSong Liu struct bpf_prog *prog; 7791b4d60ecSSong Liu void *ctx; 7801b4d60ecSSong Liu u32 retval; 7811b4d60ecSSong Liu }; 7821b4d60ecSSong Liu 7831b4d60ecSSong Liu static void 7841b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(void *data) 7851b4d60ecSSong Liu { 7861b4d60ecSSong Liu struct bpf_raw_tp_test_run_info *info = data; 7871b4d60ecSSong Liu 7881b4d60ecSSong Liu rcu_read_lock(); 789fb7dd8bcSAndrii Nakryiko info->retval = bpf_prog_run(info->prog, info->ctx); 7901b4d60ecSSong Liu rcu_read_unlock(); 7911b4d60ecSSong Liu } 7921b4d60ecSSong Liu 7931b4d60ecSSong Liu int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, 7941b4d60ecSSong Liu const union bpf_attr *kattr, 7951b4d60ecSSong Liu union bpf_attr __user *uattr) 7961b4d60ecSSong Liu { 7971b4d60ecSSong Liu void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 7981b4d60ecSSong Liu __u32 ctx_size_in = kattr->test.ctx_size_in; 7991b4d60ecSSong Liu struct bpf_raw_tp_test_run_info info; 8001b4d60ecSSong Liu int cpu = kattr->test.cpu, err = 0; 801963ec27aSSong Liu int current_cpu; 8021b4d60ecSSong Liu 8031b4d60ecSSong Liu /* doesn't support data_in/out, ctx_out, duration, or repeat */ 8041b4d60ecSSong Liu if (kattr->test.data_in || kattr->test.data_out || 8051b4d60ecSSong Liu kattr->test.ctx_out || kattr->test.duration || 806b530e9e1SToke Høiland-Jørgensen kattr->test.repeat || kattr->test.batch_size) 8071b4d60ecSSong Liu return -EINVAL; 8081b4d60ecSSong Liu 8097ac6ad05SSong Liu if (ctx_size_in < prog->aux->max_ctx_offset || 8107ac6ad05SSong Liu ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64)) 8111b4d60ecSSong Liu return -EINVAL; 8121b4d60ecSSong Liu 8131b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) 8141b4d60ecSSong Liu return -EINVAL; 8151b4d60ecSSong Liu 8161b4d60ecSSong Liu if (ctx_size_in) { 817db5b6a46SQing Wang info.ctx = memdup_user(ctx_in, ctx_size_in); 818db5b6a46SQing Wang if (IS_ERR(info.ctx)) 819db5b6a46SQing Wang return PTR_ERR(info.ctx); 8201b4d60ecSSong Liu } else { 8211b4d60ecSSong Liu info.ctx = NULL; 8221b4d60ecSSong Liu } 8231b4d60ecSSong Liu 8241b4d60ecSSong Liu info.prog = prog; 8251b4d60ecSSong Liu 826963ec27aSSong Liu current_cpu = get_cpu(); 8271b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || 828963ec27aSSong Liu cpu == current_cpu) { 8291b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(&info); 830963ec27aSSong Liu } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { 8311b4d60ecSSong Liu /* smp_call_function_single() also checks cpu_online() 8321b4d60ecSSong Liu * after csd_lock(). However, since cpu is from user 8331b4d60ecSSong Liu * space, let's do an extra quick check to filter out 8341b4d60ecSSong Liu * invalid value before smp_call_function_single(). 8351b4d60ecSSong Liu */ 8361b4d60ecSSong Liu err = -ENXIO; 837963ec27aSSong Liu } else { 8381b4d60ecSSong Liu err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, 8391b4d60ecSSong Liu &info, 1); 8401b4d60ecSSong Liu } 841963ec27aSSong Liu put_cpu(); 8421b4d60ecSSong Liu 843963ec27aSSong Liu if (!err && 844963ec27aSSong Liu copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) 8451b4d60ecSSong Liu err = -EFAULT; 8461b4d60ecSSong Liu 8471b4d60ecSSong Liu kfree(info.ctx); 8481b4d60ecSSong Liu return err; 8491b4d60ecSSong Liu } 8501b4d60ecSSong Liu 851b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) 852b0b9395dSStanislav Fomichev { 853b0b9395dSStanislav Fomichev void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); 854b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 855b0b9395dSStanislav Fomichev u32 size = kattr->test.ctx_size_in; 856b0b9395dSStanislav Fomichev void *data; 857b0b9395dSStanislav Fomichev int err; 858b0b9395dSStanislav Fomichev 859b0b9395dSStanislav Fomichev if (!data_in && !data_out) 860b0b9395dSStanislav Fomichev return NULL; 861b0b9395dSStanislav Fomichev 862b0b9395dSStanislav Fomichev data = kzalloc(max_size, GFP_USER); 863b0b9395dSStanislav Fomichev if (!data) 864b0b9395dSStanislav Fomichev return ERR_PTR(-ENOMEM); 865b0b9395dSStanislav Fomichev 866b0b9395dSStanislav Fomichev if (data_in) { 867af2ac3e1SAlexei Starovoitov err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size); 868b0b9395dSStanislav Fomichev if (err) { 869b0b9395dSStanislav Fomichev kfree(data); 870b0b9395dSStanislav Fomichev return ERR_PTR(err); 871b0b9395dSStanislav Fomichev } 872b0b9395dSStanislav Fomichev 873b0b9395dSStanislav Fomichev size = min_t(u32, max_size, size); 874b0b9395dSStanislav Fomichev if (copy_from_user(data, data_in, size)) { 875b0b9395dSStanislav Fomichev kfree(data); 876b0b9395dSStanislav Fomichev return ERR_PTR(-EFAULT); 877b0b9395dSStanislav Fomichev } 878b0b9395dSStanislav Fomichev } 879b0b9395dSStanislav Fomichev return data; 880b0b9395dSStanislav Fomichev } 881b0b9395dSStanislav Fomichev 882b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr, 883b0b9395dSStanislav Fomichev union bpf_attr __user *uattr, const void *data, 884b0b9395dSStanislav Fomichev u32 size) 885b0b9395dSStanislav Fomichev { 886b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 887b0b9395dSStanislav Fomichev int err = -EFAULT; 888b0b9395dSStanislav Fomichev u32 copy_size = size; 889b0b9395dSStanislav Fomichev 890b0b9395dSStanislav Fomichev if (!data || !data_out) 891b0b9395dSStanislav Fomichev return 0; 892b0b9395dSStanislav Fomichev 893b0b9395dSStanislav Fomichev if (copy_size > kattr->test.ctx_size_out) { 894b0b9395dSStanislav Fomichev copy_size = kattr->test.ctx_size_out; 895b0b9395dSStanislav Fomichev err = -ENOSPC; 896b0b9395dSStanislav Fomichev } 897b0b9395dSStanislav Fomichev 898b0b9395dSStanislav Fomichev if (copy_to_user(data_out, data, copy_size)) 899b0b9395dSStanislav Fomichev goto out; 900b0b9395dSStanislav Fomichev if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) 901b0b9395dSStanislav Fomichev goto out; 902b0b9395dSStanislav Fomichev if (err != -ENOSPC) 903b0b9395dSStanislav Fomichev err = 0; 904b0b9395dSStanislav Fomichev out: 905b0b9395dSStanislav Fomichev return err; 906b0b9395dSStanislav Fomichev } 907b0b9395dSStanislav Fomichev 908b0b9395dSStanislav Fomichev /** 909b0b9395dSStanislav Fomichev * range_is_zero - test whether buffer is initialized 910b0b9395dSStanislav Fomichev * @buf: buffer to check 911b0b9395dSStanislav Fomichev * @from: check from this position 912b0b9395dSStanislav Fomichev * @to: check up until (excluding) this position 913b0b9395dSStanislav Fomichev * 914b0b9395dSStanislav Fomichev * This function returns true if the there is a non-zero byte 915b0b9395dSStanislav Fomichev * in the buf in the range [from,to). 916b0b9395dSStanislav Fomichev */ 917b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to) 918b0b9395dSStanislav Fomichev { 919b0b9395dSStanislav Fomichev return !memchr_inv((u8 *)buf + from, 0, to - from); 920b0b9395dSStanislav Fomichev } 921b0b9395dSStanislav Fomichev 922b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) 923b0b9395dSStanislav Fomichev { 924b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 925b0b9395dSStanislav Fomichev 926b0b9395dSStanislav Fomichev if (!__skb) 927b0b9395dSStanislav Fomichev return 0; 928b0b9395dSStanislav Fomichev 929b0b9395dSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 9306de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) 9316de6c1f8SNikita V. Shirokov return -EINVAL; 9326de6c1f8SNikita V. Shirokov 9336de6c1f8SNikita V. Shirokov /* mark is allowed */ 9346de6c1f8SNikita V. Shirokov 9356de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), 9366de6c1f8SNikita V. Shirokov offsetof(struct __sk_buff, priority))) 937b0b9395dSStanislav Fomichev return -EINVAL; 938b0b9395dSStanislav Fomichev 939b0b9395dSStanislav Fomichev /* priority is allowed */ 940b238290bSNeil Spring /* ingress_ifindex is allowed */ 94121594c44SDmitry Yakunin /* ifindex is allowed */ 94221594c44SDmitry Yakunin 94321594c44SDmitry Yakunin if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), 944b0b9395dSStanislav Fomichev offsetof(struct __sk_buff, cb))) 945b0b9395dSStanislav Fomichev return -EINVAL; 946b0b9395dSStanislav Fomichev 947b0b9395dSStanislav Fomichev /* cb is allowed */ 948b0b9395dSStanislav Fomichev 949b590cb5fSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), 950ba940948SStanislav Fomichev offsetof(struct __sk_buff, tstamp))) 951ba940948SStanislav Fomichev return -EINVAL; 952ba940948SStanislav Fomichev 953ba940948SStanislav Fomichev /* tstamp is allowed */ 954850a88ccSStanislav Fomichev /* wire_len is allowed */ 955850a88ccSStanislav Fomichev /* gso_segs is allowed */ 956ba940948SStanislav Fomichev 957850a88ccSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), 958cf62089bSWillem de Bruijn offsetof(struct __sk_buff, gso_size))) 959cf62089bSWillem de Bruijn return -EINVAL; 960cf62089bSWillem de Bruijn 961cf62089bSWillem de Bruijn /* gso_size is allowed */ 962cf62089bSWillem de Bruijn 963cf62089bSWillem de Bruijn if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), 9643384c7c7SVadim Fedorenko offsetof(struct __sk_buff, hwtstamp))) 9653384c7c7SVadim Fedorenko return -EINVAL; 9663384c7c7SVadim Fedorenko 9673384c7c7SVadim Fedorenko /* hwtstamp is allowed */ 9683384c7c7SVadim Fedorenko 9693384c7c7SVadim Fedorenko if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp), 970b0b9395dSStanislav Fomichev sizeof(struct __sk_buff))) 971b0b9395dSStanislav Fomichev return -EINVAL; 972b0b9395dSStanislav Fomichev 9736de6c1f8SNikita V. Shirokov skb->mark = __skb->mark; 974b0b9395dSStanislav Fomichev skb->priority = __skb->priority; 975b238290bSNeil Spring skb->skb_iif = __skb->ingress_ifindex; 976ba940948SStanislav Fomichev skb->tstamp = __skb->tstamp; 977b0b9395dSStanislav Fomichev memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); 978b0b9395dSStanislav Fomichev 979850a88ccSStanislav Fomichev if (__skb->wire_len == 0) { 980850a88ccSStanislav Fomichev cb->pkt_len = skb->len; 981850a88ccSStanislav Fomichev } else { 982850a88ccSStanislav Fomichev if (__skb->wire_len < skb->len || 983850a88ccSStanislav Fomichev __skb->wire_len > GSO_MAX_SIZE) 984850a88ccSStanislav Fomichev return -EINVAL; 985850a88ccSStanislav Fomichev cb->pkt_len = __skb->wire_len; 986850a88ccSStanislav Fomichev } 987850a88ccSStanislav Fomichev 988850a88ccSStanislav Fomichev if (__skb->gso_segs > GSO_MAX_SEGS) 989850a88ccSStanislav Fomichev return -EINVAL; 990850a88ccSStanislav Fomichev skb_shinfo(skb)->gso_segs = __skb->gso_segs; 991cf62089bSWillem de Bruijn skb_shinfo(skb)->gso_size = __skb->gso_size; 9923384c7c7SVadim Fedorenko skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp; 993850a88ccSStanislav Fomichev 994b0b9395dSStanislav Fomichev return 0; 995b0b9395dSStanislav Fomichev } 996b0b9395dSStanislav Fomichev 997b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) 998b0b9395dSStanislav Fomichev { 999b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 1000b0b9395dSStanislav Fomichev 1001b0b9395dSStanislav Fomichev if (!__skb) 1002b0b9395dSStanislav Fomichev return; 1003b0b9395dSStanislav Fomichev 10046de6c1f8SNikita V. Shirokov __skb->mark = skb->mark; 1005b0b9395dSStanislav Fomichev __skb->priority = skb->priority; 1006b238290bSNeil Spring __skb->ingress_ifindex = skb->skb_iif; 100721594c44SDmitry Yakunin __skb->ifindex = skb->dev->ifindex; 1008ba940948SStanislav Fomichev __skb->tstamp = skb->tstamp; 1009b0b9395dSStanislav Fomichev memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); 1010850a88ccSStanislav Fomichev __skb->wire_len = cb->pkt_len; 1011850a88ccSStanislav Fomichev __skb->gso_segs = skb_shinfo(skb)->gso_segs; 10123384c7c7SVadim Fedorenko __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp; 1013b0b9395dSStanislav Fomichev } 1014b0b9395dSStanislav Fomichev 1015435b08ecSDaniel Borkmann static struct proto bpf_dummy_proto = { 1016435b08ecSDaniel Borkmann .name = "bpf_dummy", 1017435b08ecSDaniel Borkmann .owner = THIS_MODULE, 1018435b08ecSDaniel Borkmann .obj_size = sizeof(struct sock), 1019435b08ecSDaniel Borkmann }; 1020435b08ecSDaniel Borkmann 10211cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 10221cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 10231cf1cae9SAlexei Starovoitov { 10241cf1cae9SAlexei Starovoitov bool is_l2 = false, is_direct_pkt_access = false; 102521594c44SDmitry Yakunin struct net *net = current->nsproxy->net_ns; 102621594c44SDmitry Yakunin struct net_device *dev = net->loopback_dev; 10271cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 10281cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 1029b0b9395dSStanislav Fomichev struct __sk_buff *ctx = NULL; 10301cf1cae9SAlexei Starovoitov u32 retval, duration; 10316e6fddc7SDaniel Borkmann int hh_len = ETH_HLEN; 10321cf1cae9SAlexei Starovoitov struct sk_buff *skb; 10332cb494a3SSong Liu struct sock *sk; 10341cf1cae9SAlexei Starovoitov void *data; 10351cf1cae9SAlexei Starovoitov int ret; 10361cf1cae9SAlexei Starovoitov 1037b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 10381b4d60ecSSong Liu return -EINVAL; 10391b4d60ecSSong Liu 1040be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, 1041be3d72a2SLorenzo Bianconi size, NET_SKB_PAD + NET_IP_ALIGN, 10421cf1cae9SAlexei Starovoitov SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 10431cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 10441cf1cae9SAlexei Starovoitov return PTR_ERR(data); 10451cf1cae9SAlexei Starovoitov 1046b0b9395dSStanislav Fomichev ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); 1047b0b9395dSStanislav Fomichev if (IS_ERR(ctx)) { 1048b0b9395dSStanislav Fomichev kfree(data); 1049b0b9395dSStanislav Fomichev return PTR_ERR(ctx); 1050b0b9395dSStanislav Fomichev } 1051b0b9395dSStanislav Fomichev 10521cf1cae9SAlexei Starovoitov switch (prog->type) { 10531cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 10541cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 10551cf1cae9SAlexei Starovoitov is_l2 = true; 1056df561f66SGustavo A. R. Silva fallthrough; 10571cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 10581cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 10591cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 10601cf1cae9SAlexei Starovoitov is_direct_pkt_access = true; 10611cf1cae9SAlexei Starovoitov break; 10621cf1cae9SAlexei Starovoitov default: 10631cf1cae9SAlexei Starovoitov break; 10641cf1cae9SAlexei Starovoitov } 10651cf1cae9SAlexei Starovoitov 1066435b08ecSDaniel Borkmann sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1); 10672cb494a3SSong Liu if (!sk) { 10681cf1cae9SAlexei Starovoitov kfree(data); 1069b0b9395dSStanislav Fomichev kfree(ctx); 10701cf1cae9SAlexei Starovoitov return -ENOMEM; 10711cf1cae9SAlexei Starovoitov } 10722cb494a3SSong Liu sock_init_data(NULL, sk); 10732cb494a3SSong Liu 10742cb494a3SSong Liu skb = build_skb(data, 0); 10752cb494a3SSong Liu if (!skb) { 10762cb494a3SSong Liu kfree(data); 1077b0b9395dSStanislav Fomichev kfree(ctx); 1078435b08ecSDaniel Borkmann sk_free(sk); 10792cb494a3SSong Liu return -ENOMEM; 10802cb494a3SSong Liu } 10812cb494a3SSong Liu skb->sk = sk; 10821cf1cae9SAlexei Starovoitov 1083586f8525SDavid Miller skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 10841cf1cae9SAlexei Starovoitov __skb_put(skb, size); 108521594c44SDmitry Yakunin if (ctx && ctx->ifindex > 1) { 108621594c44SDmitry Yakunin dev = dev_get_by_index(net, ctx->ifindex); 108721594c44SDmitry Yakunin if (!dev) { 108821594c44SDmitry Yakunin ret = -ENODEV; 108921594c44SDmitry Yakunin goto out; 109021594c44SDmitry Yakunin } 109121594c44SDmitry Yakunin } 109221594c44SDmitry Yakunin skb->protocol = eth_type_trans(skb, dev); 10931cf1cae9SAlexei Starovoitov skb_reset_network_header(skb); 10941cf1cae9SAlexei Starovoitov 1095fa5cb548SDmitry Yakunin switch (skb->protocol) { 1096fa5cb548SDmitry Yakunin case htons(ETH_P_IP): 1097fa5cb548SDmitry Yakunin sk->sk_family = AF_INET; 1098fa5cb548SDmitry Yakunin if (sizeof(struct iphdr) <= skb_headlen(skb)) { 1099fa5cb548SDmitry Yakunin sk->sk_rcv_saddr = ip_hdr(skb)->saddr; 1100fa5cb548SDmitry Yakunin sk->sk_daddr = ip_hdr(skb)->daddr; 1101fa5cb548SDmitry Yakunin } 1102fa5cb548SDmitry Yakunin break; 1103fa5cb548SDmitry Yakunin #if IS_ENABLED(CONFIG_IPV6) 1104fa5cb548SDmitry Yakunin case htons(ETH_P_IPV6): 1105fa5cb548SDmitry Yakunin sk->sk_family = AF_INET6; 1106fa5cb548SDmitry Yakunin if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) { 1107fa5cb548SDmitry Yakunin sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; 1108fa5cb548SDmitry Yakunin sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; 1109fa5cb548SDmitry Yakunin } 1110fa5cb548SDmitry Yakunin break; 1111fa5cb548SDmitry Yakunin #endif 1112fa5cb548SDmitry Yakunin default: 1113fa5cb548SDmitry Yakunin break; 1114fa5cb548SDmitry Yakunin } 1115fa5cb548SDmitry Yakunin 11161cf1cae9SAlexei Starovoitov if (is_l2) 11176e6fddc7SDaniel Borkmann __skb_push(skb, hh_len); 11181cf1cae9SAlexei Starovoitov if (is_direct_pkt_access) 11196aaae2b6SDaniel Borkmann bpf_compute_data_pointers(skb); 1120b0b9395dSStanislav Fomichev ret = convert___skb_to_skb(skb, ctx); 1121b0b9395dSStanislav Fomichev if (ret) 1122b0b9395dSStanislav Fomichev goto out; 1123f23c4b39SBjörn Töpel ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); 1124b0b9395dSStanislav Fomichev if (ret) 1125b0b9395dSStanislav Fomichev goto out; 11266e6fddc7SDaniel Borkmann if (!is_l2) { 11276e6fddc7SDaniel Borkmann if (skb_headroom(skb) < hh_len) { 11286e6fddc7SDaniel Borkmann int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 11296e6fddc7SDaniel Borkmann 11306e6fddc7SDaniel Borkmann if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { 1131b0b9395dSStanislav Fomichev ret = -ENOMEM; 1132b0b9395dSStanislav Fomichev goto out; 11336e6fddc7SDaniel Borkmann } 11346e6fddc7SDaniel Borkmann } 11356e6fddc7SDaniel Borkmann memset(__skb_push(skb, hh_len), 0, hh_len); 11366e6fddc7SDaniel Borkmann } 1137b0b9395dSStanislav Fomichev convert_skb_to___skb(skb, ctx); 11386e6fddc7SDaniel Borkmann 11391cf1cae9SAlexei Starovoitov size = skb->len; 11401cf1cae9SAlexei Starovoitov /* bpf program can never convert linear skb to non-linear */ 11411cf1cae9SAlexei Starovoitov if (WARN_ON_ONCE(skb_is_nonlinear(skb))) 11421cf1cae9SAlexei Starovoitov size = skb_headlen(skb); 11437855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval, 11447855e0dbSLorenzo Bianconi duration); 1145b0b9395dSStanislav Fomichev if (!ret) 1146b0b9395dSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, ctx, 1147b0b9395dSStanislav Fomichev sizeof(struct __sk_buff)); 1148b0b9395dSStanislav Fomichev out: 114921594c44SDmitry Yakunin if (dev && dev != net->loopback_dev) 115021594c44SDmitry Yakunin dev_put(dev); 11511cf1cae9SAlexei Starovoitov kfree_skb(skb); 1152435b08ecSDaniel Borkmann sk_free(sk); 1153b0b9395dSStanislav Fomichev kfree(ctx); 11541cf1cae9SAlexei Starovoitov return ret; 11551cf1cae9SAlexei Starovoitov } 11561cf1cae9SAlexei Starovoitov 115747316f4aSZvi Effron static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp) 115847316f4aSZvi Effron { 1159ec94670fSZvi Effron unsigned int ingress_ifindex, rx_queue_index; 1160ec94670fSZvi Effron struct netdev_rx_queue *rxqueue; 1161ec94670fSZvi Effron struct net_device *device; 1162ec94670fSZvi Effron 116347316f4aSZvi Effron if (!xdp_md) 116447316f4aSZvi Effron return 0; 116547316f4aSZvi Effron 116647316f4aSZvi Effron if (xdp_md->egress_ifindex != 0) 116747316f4aSZvi Effron return -EINVAL; 116847316f4aSZvi Effron 1169ec94670fSZvi Effron ingress_ifindex = xdp_md->ingress_ifindex; 1170ec94670fSZvi Effron rx_queue_index = xdp_md->rx_queue_index; 1171ec94670fSZvi Effron 1172ec94670fSZvi Effron if (!ingress_ifindex && rx_queue_index) 117347316f4aSZvi Effron return -EINVAL; 117447316f4aSZvi Effron 1175ec94670fSZvi Effron if (ingress_ifindex) { 1176ec94670fSZvi Effron device = dev_get_by_index(current->nsproxy->net_ns, 1177ec94670fSZvi Effron ingress_ifindex); 1178ec94670fSZvi Effron if (!device) 1179ec94670fSZvi Effron return -ENODEV; 118047316f4aSZvi Effron 1181ec94670fSZvi Effron if (rx_queue_index >= device->real_num_rx_queues) 1182ec94670fSZvi Effron goto free_dev; 1183ec94670fSZvi Effron 1184ec94670fSZvi Effron rxqueue = __netif_get_rx_queue(device, rx_queue_index); 1185ec94670fSZvi Effron 1186ec94670fSZvi Effron if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq)) 1187ec94670fSZvi Effron goto free_dev; 1188ec94670fSZvi Effron 1189ec94670fSZvi Effron xdp->rxq = &rxqueue->xdp_rxq; 1190ec94670fSZvi Effron /* The device is now tracked in the xdp->rxq for later 1191ec94670fSZvi Effron * dev_put() 1192ec94670fSZvi Effron */ 1193ec94670fSZvi Effron } 1194ec94670fSZvi Effron 1195ec94670fSZvi Effron xdp->data = xdp->data_meta + xdp_md->data; 119647316f4aSZvi Effron return 0; 1197ec94670fSZvi Effron 1198ec94670fSZvi Effron free_dev: 1199ec94670fSZvi Effron dev_put(device); 1200ec94670fSZvi Effron return -EINVAL; 1201ec94670fSZvi Effron } 1202ec94670fSZvi Effron 1203ec94670fSZvi Effron static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md) 1204ec94670fSZvi Effron { 1205ec94670fSZvi Effron if (!xdp_md) 1206ec94670fSZvi Effron return; 1207ec94670fSZvi Effron 1208ec94670fSZvi Effron xdp_md->data = xdp->data - xdp->data_meta; 1209ec94670fSZvi Effron xdp_md->data_end = xdp->data_end - xdp->data_meta; 1210ec94670fSZvi Effron 1211ec94670fSZvi Effron if (xdp_md->ingress_ifindex) 1212ec94670fSZvi Effron dev_put(xdp->rxq->dev); 121347316f4aSZvi Effron } 121447316f4aSZvi Effron 12151cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 12161cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 12171cf1cae9SAlexei Starovoitov { 1218b530e9e1SToke Høiland-Jørgensen bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES); 1219bc56c919SJesper Dangaard Brouer u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1220b530e9e1SToke Høiland-Jørgensen u32 batch_size = kattr->test.batch_size; 1221eecbfd97SToke Høiland-Jørgensen u32 retval = 0, duration, max_data_sz; 12221cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 12231c194998SLorenzo Bianconi u32 headroom = XDP_PACKET_HEADROOM; 12241cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 122565073a67SDaniel Borkmann struct netdev_rx_queue *rxqueue; 12261c194998SLorenzo Bianconi struct skb_shared_info *sinfo; 12271cf1cae9SAlexei Starovoitov struct xdp_buff xdp = {}; 12281c194998SLorenzo Bianconi int i, ret = -EINVAL; 122947316f4aSZvi Effron struct xdp_md *ctx; 12301cf1cae9SAlexei Starovoitov void *data; 12311cf1cae9SAlexei Starovoitov 12325e21bb4eSXuan Zhuo if (prog->expected_attach_type == BPF_XDP_DEVMAP || 12335e21bb4eSXuan Zhuo prog->expected_attach_type == BPF_XDP_CPUMAP) 12345e21bb4eSXuan Zhuo return -EINVAL; 12356d4eb36dSAndrii Nakryiko 1236b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES) 1237b530e9e1SToke Høiland-Jørgensen return -EINVAL; 1238b530e9e1SToke Høiland-Jørgensen 1239b530e9e1SToke Høiland-Jørgensen if (do_live) { 1240b530e9e1SToke Høiland-Jørgensen if (!batch_size) 1241b530e9e1SToke Høiland-Jørgensen batch_size = NAPI_POLL_WEIGHT; 1242b530e9e1SToke Høiland-Jørgensen else if (batch_size > TEST_XDP_MAX_BATCH) 1243b530e9e1SToke Høiland-Jørgensen return -E2BIG; 1244b6f1f780SToke Høiland-Jørgensen 1245b6f1f780SToke Høiland-Jørgensen headroom += sizeof(struct xdp_page_head); 1246b530e9e1SToke Høiland-Jørgensen } else if (batch_size) { 1247b530e9e1SToke Høiland-Jørgensen return -EINVAL; 1248b530e9e1SToke Høiland-Jørgensen } 1249b530e9e1SToke Høiland-Jørgensen 125047316f4aSZvi Effron ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md)); 125147316f4aSZvi Effron if (IS_ERR(ctx)) 125247316f4aSZvi Effron return PTR_ERR(ctx); 125347316f4aSZvi Effron 125447316f4aSZvi Effron if (ctx) { 125547316f4aSZvi Effron /* There can't be user provided data before the meta data */ 125647316f4aSZvi Effron if (ctx->data_meta || ctx->data_end != size || 125747316f4aSZvi Effron ctx->data > ctx->data_end || 1258b530e9e1SToke Høiland-Jørgensen unlikely(xdp_metalen_invalid(ctx->data)) || 1259b530e9e1SToke Høiland-Jørgensen (do_live && (kattr->test.data_out || kattr->test.ctx_out))) 126047316f4aSZvi Effron goto free_ctx; 126147316f4aSZvi Effron /* Meta data is allocated from the headroom */ 126247316f4aSZvi Effron headroom -= ctx->data; 126347316f4aSZvi Effron } 1264947e8b59SStanislav Fomichev 1265bc56c919SJesper Dangaard Brouer max_data_sz = 4096 - headroom - tailroom; 1266b530e9e1SToke Høiland-Jørgensen if (size > max_data_sz) { 1267b530e9e1SToke Høiland-Jørgensen /* disallow live data mode for jumbo frames */ 1268b530e9e1SToke Høiland-Jørgensen if (do_live) 1269b530e9e1SToke Høiland-Jørgensen goto free_ctx; 1270b530e9e1SToke Høiland-Jørgensen size = max_data_sz; 1271b530e9e1SToke Høiland-Jørgensen } 1272bc56c919SJesper Dangaard Brouer 12731c194998SLorenzo Bianconi data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom); 127447316f4aSZvi Effron if (IS_ERR(data)) { 127547316f4aSZvi Effron ret = PTR_ERR(data); 127647316f4aSZvi Effron goto free_ctx; 127747316f4aSZvi Effron } 12781cf1cae9SAlexei Starovoitov 127965073a67SDaniel Borkmann rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 12801c194998SLorenzo Bianconi rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom; 12811c194998SLorenzo Bianconi xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq); 1282be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data, headroom, size, true); 12831c194998SLorenzo Bianconi sinfo = xdp_get_shared_info_from_buff(&xdp); 1284be9df4afSLorenzo Bianconi 128547316f4aSZvi Effron ret = xdp_convert_md_to_buff(ctx, &xdp); 128647316f4aSZvi Effron if (ret) 128747316f4aSZvi Effron goto free_data; 128847316f4aSZvi Effron 12891c194998SLorenzo Bianconi if (unlikely(kattr->test.data_size_in > size)) { 12901c194998SLorenzo Bianconi void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 12911c194998SLorenzo Bianconi 12921c194998SLorenzo Bianconi while (size < kattr->test.data_size_in) { 12931c194998SLorenzo Bianconi struct page *page; 12941c194998SLorenzo Bianconi skb_frag_t *frag; 12959d63b59dSStanislav Fomichev u32 data_len; 12961c194998SLorenzo Bianconi 1297a6763080SLorenzo Bianconi if (sinfo->nr_frags == MAX_SKB_FRAGS) { 1298a6763080SLorenzo Bianconi ret = -ENOMEM; 1299a6763080SLorenzo Bianconi goto out; 1300a6763080SLorenzo Bianconi } 1301a6763080SLorenzo Bianconi 13021c194998SLorenzo Bianconi page = alloc_page(GFP_KERNEL); 13031c194998SLorenzo Bianconi if (!page) { 13041c194998SLorenzo Bianconi ret = -ENOMEM; 13051c194998SLorenzo Bianconi goto out; 13061c194998SLorenzo Bianconi } 13071c194998SLorenzo Bianconi 13081c194998SLorenzo Bianconi frag = &sinfo->frags[sinfo->nr_frags++]; 13091c194998SLorenzo Bianconi __skb_frag_set_page(frag, page); 13101c194998SLorenzo Bianconi 13119d63b59dSStanislav Fomichev data_len = min_t(u32, kattr->test.data_size_in - size, 13121c194998SLorenzo Bianconi PAGE_SIZE); 13131c194998SLorenzo Bianconi skb_frag_size_set(frag, data_len); 13141c194998SLorenzo Bianconi 13151c194998SLorenzo Bianconi if (copy_from_user(page_address(page), data_in + size, 13161c194998SLorenzo Bianconi data_len)) { 13171c194998SLorenzo Bianconi ret = -EFAULT; 13181c194998SLorenzo Bianconi goto out; 13191c194998SLorenzo Bianconi } 13201c194998SLorenzo Bianconi sinfo->xdp_frags_size += data_len; 13211c194998SLorenzo Bianconi size += data_len; 13221c194998SLorenzo Bianconi } 13231c194998SLorenzo Bianconi xdp_buff_set_frags_flag(&xdp); 13241c194998SLorenzo Bianconi } 13251c194998SLorenzo Bianconi 1326de21d8bfSLorenz Bauer if (repeat > 1) 1327f23c4b39SBjörn Töpel bpf_prog_change_xdp(NULL, prog); 13281c194998SLorenzo Bianconi 1329b530e9e1SToke Høiland-Jørgensen if (do_live) 1330b530e9e1SToke Høiland-Jørgensen ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration); 1331b530e9e1SToke Høiland-Jørgensen else 1332f23c4b39SBjörn Töpel ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); 1333ec94670fSZvi Effron /* We convert the xdp_buff back to an xdp_md before checking the return 1334ec94670fSZvi Effron * code so the reference count of any held netdevice will be decremented 1335ec94670fSZvi Effron * even if the test run failed. 1336ec94670fSZvi Effron */ 1337ec94670fSZvi Effron xdp_convert_buff_to_md(&xdp, ctx); 1338dcb40590SRoman Gushchin if (ret) 1339dcb40590SRoman Gushchin goto out; 134047316f4aSZvi Effron 13411c194998SLorenzo Bianconi size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; 13427855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size, 13437855e0dbSLorenzo Bianconi retval, duration); 134447316f4aSZvi Effron if (!ret) 134547316f4aSZvi Effron ret = bpf_ctx_finish(kattr, uattr, ctx, 134647316f4aSZvi Effron sizeof(struct xdp_md)); 134747316f4aSZvi Effron 1348dcb40590SRoman Gushchin out: 1349de21d8bfSLorenz Bauer if (repeat > 1) 1350f23c4b39SBjörn Töpel bpf_prog_change_xdp(prog, NULL); 135147316f4aSZvi Effron free_data: 13521c194998SLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) 13531c194998SLorenzo Bianconi __free_page(skb_frag_page(&sinfo->frags[i])); 13541cf1cae9SAlexei Starovoitov kfree(data); 135547316f4aSZvi Effron free_ctx: 135647316f4aSZvi Effron kfree(ctx); 13571cf1cae9SAlexei Starovoitov return ret; 13581cf1cae9SAlexei Starovoitov } 1359b7a1848eSStanislav Fomichev 1360b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) 1361b2ca4e1cSStanislav Fomichev { 1362b2ca4e1cSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 1363b2ca4e1cSStanislav Fomichev if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) 1364b2ca4e1cSStanislav Fomichev return -EINVAL; 1365b2ca4e1cSStanislav Fomichev 1366b2ca4e1cSStanislav Fomichev /* flags is allowed */ 1367b2ca4e1cSStanislav Fomichev 1368b590cb5fSStanislav Fomichev if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), 1369b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys))) 1370b2ca4e1cSStanislav Fomichev return -EINVAL; 1371b2ca4e1cSStanislav Fomichev 1372b2ca4e1cSStanislav Fomichev return 0; 1373b2ca4e1cSStanislav Fomichev } 1374b2ca4e1cSStanislav Fomichev 1375b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, 1376b7a1848eSStanislav Fomichev const union bpf_attr *kattr, 1377b7a1848eSStanislav Fomichev union bpf_attr __user *uattr) 1378b7a1848eSStanislav Fomichev { 1379607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 1380b7a1848eSStanislav Fomichev u32 size = kattr->test.data_size_in; 13817b8a1304SStanislav Fomichev struct bpf_flow_dissector ctx = {}; 1382b7a1848eSStanislav Fomichev u32 repeat = kattr->test.repeat; 1383b2ca4e1cSStanislav Fomichev struct bpf_flow_keys *user_ctx; 1384b7a1848eSStanislav Fomichev struct bpf_flow_keys flow_keys; 13857b8a1304SStanislav Fomichev const struct ethhdr *eth; 1386b2ca4e1cSStanislav Fomichev unsigned int flags = 0; 1387b7a1848eSStanislav Fomichev u32 retval, duration; 1388b7a1848eSStanislav Fomichev void *data; 1389b7a1848eSStanislav Fomichev int ret; 1390b7a1848eSStanislav Fomichev 1391b7a1848eSStanislav Fomichev if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) 1392b7a1848eSStanislav Fomichev return -EINVAL; 1393b7a1848eSStanislav Fomichev 1394b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 13951b4d60ecSSong Liu return -EINVAL; 13961b4d60ecSSong Liu 13977b8a1304SStanislav Fomichev if (size < ETH_HLEN) 13987b8a1304SStanislav Fomichev return -EINVAL; 13997b8a1304SStanislav Fomichev 1400be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0); 1401b7a1848eSStanislav Fomichev if (IS_ERR(data)) 1402b7a1848eSStanislav Fomichev return PTR_ERR(data); 1403b7a1848eSStanislav Fomichev 14047b8a1304SStanislav Fomichev eth = (struct ethhdr *)data; 1405b7a1848eSStanislav Fomichev 1406b7a1848eSStanislav Fomichev if (!repeat) 1407b7a1848eSStanislav Fomichev repeat = 1; 1408b7a1848eSStanislav Fomichev 1409b2ca4e1cSStanislav Fomichev user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); 1410b2ca4e1cSStanislav Fomichev if (IS_ERR(user_ctx)) { 1411b2ca4e1cSStanislav Fomichev kfree(data); 1412b2ca4e1cSStanislav Fomichev return PTR_ERR(user_ctx); 1413b2ca4e1cSStanislav Fomichev } 1414b2ca4e1cSStanislav Fomichev if (user_ctx) { 1415b2ca4e1cSStanislav Fomichev ret = verify_user_bpf_flow_keys(user_ctx); 1416b2ca4e1cSStanislav Fomichev if (ret) 1417b2ca4e1cSStanislav Fomichev goto out; 1418b2ca4e1cSStanislav Fomichev flags = user_ctx->flags; 1419b2ca4e1cSStanislav Fomichev } 1420b2ca4e1cSStanislav Fomichev 14217b8a1304SStanislav Fomichev ctx.flow_keys = &flow_keys; 14227b8a1304SStanislav Fomichev ctx.data = data; 14237b8a1304SStanislav Fomichev ctx.data_end = (__u8 *)data + size; 14247b8a1304SStanislav Fomichev 1425607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 1426607b9cc9SLorenz Bauer do { 14277b8a1304SStanislav Fomichev retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 1428b2ca4e1cSStanislav Fomichev size, flags); 1429b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 1430607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 14317b8a1304SStanislav Fomichev 1432607b9cc9SLorenz Bauer if (ret < 0) 1433a439184dSStanislav Fomichev goto out; 1434b7a1848eSStanislav Fomichev 14357855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL, 14367855e0dbSLorenzo Bianconi sizeof(flow_keys), retval, duration); 1437b2ca4e1cSStanislav Fomichev if (!ret) 1438b2ca4e1cSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, user_ctx, 1439b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys)); 1440b7a1848eSStanislav Fomichev 1441a439184dSStanislav Fomichev out: 1442b2ca4e1cSStanislav Fomichev kfree(user_ctx); 14437b8a1304SStanislav Fomichev kfree(data); 1444b7a1848eSStanislav Fomichev return ret; 1445b7a1848eSStanislav Fomichev } 14467c32e8f8SLorenz Bauer 14477c32e8f8SLorenz Bauer int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr, 14487c32e8f8SLorenz Bauer union bpf_attr __user *uattr) 14497c32e8f8SLorenz Bauer { 14507c32e8f8SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 14517c32e8f8SLorenz Bauer struct bpf_prog_array *progs = NULL; 14527c32e8f8SLorenz Bauer struct bpf_sk_lookup_kern ctx = {}; 14537c32e8f8SLorenz Bauer u32 repeat = kattr->test.repeat; 14547c32e8f8SLorenz Bauer struct bpf_sk_lookup *user_ctx; 14557c32e8f8SLorenz Bauer u32 retval, duration; 14567c32e8f8SLorenz Bauer int ret = -EINVAL; 14577c32e8f8SLorenz Bauer 14587c32e8f8SLorenz Bauer if (prog->type != BPF_PROG_TYPE_SK_LOOKUP) 14597c32e8f8SLorenz Bauer return -EINVAL; 14607c32e8f8SLorenz Bauer 1461b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 14627c32e8f8SLorenz Bauer return -EINVAL; 14637c32e8f8SLorenz Bauer 14647c32e8f8SLorenz Bauer if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || 14657c32e8f8SLorenz Bauer kattr->test.data_size_out) 14667c32e8f8SLorenz Bauer return -EINVAL; 14677c32e8f8SLorenz Bauer 14687c32e8f8SLorenz Bauer if (!repeat) 14697c32e8f8SLorenz Bauer repeat = 1; 14707c32e8f8SLorenz Bauer 14717c32e8f8SLorenz Bauer user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); 14727c32e8f8SLorenz Bauer if (IS_ERR(user_ctx)) 14737c32e8f8SLorenz Bauer return PTR_ERR(user_ctx); 14747c32e8f8SLorenz Bauer 14757c32e8f8SLorenz Bauer if (!user_ctx) 14767c32e8f8SLorenz Bauer return -EINVAL; 14777c32e8f8SLorenz Bauer 14787c32e8f8SLorenz Bauer if (user_ctx->sk) 14797c32e8f8SLorenz Bauer goto out; 14807c32e8f8SLorenz Bauer 14817c32e8f8SLorenz Bauer if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) 14827c32e8f8SLorenz Bauer goto out; 14837c32e8f8SLorenz Bauer 14849a69e2b3SJakub Sitnicki if (user_ctx->local_port > U16_MAX) { 14857c32e8f8SLorenz Bauer ret = -ERANGE; 14867c32e8f8SLorenz Bauer goto out; 14877c32e8f8SLorenz Bauer } 14887c32e8f8SLorenz Bauer 14897c32e8f8SLorenz Bauer ctx.family = (u16)user_ctx->family; 14907c32e8f8SLorenz Bauer ctx.protocol = (u16)user_ctx->protocol; 14917c32e8f8SLorenz Bauer ctx.dport = (u16)user_ctx->local_port; 14929a69e2b3SJakub Sitnicki ctx.sport = user_ctx->remote_port; 14937c32e8f8SLorenz Bauer 14947c32e8f8SLorenz Bauer switch (ctx.family) { 14957c32e8f8SLorenz Bauer case AF_INET: 14967c32e8f8SLorenz Bauer ctx.v4.daddr = (__force __be32)user_ctx->local_ip4; 14977c32e8f8SLorenz Bauer ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4; 14987c32e8f8SLorenz Bauer break; 14997c32e8f8SLorenz Bauer 15007c32e8f8SLorenz Bauer #if IS_ENABLED(CONFIG_IPV6) 15017c32e8f8SLorenz Bauer case AF_INET6: 15027c32e8f8SLorenz Bauer ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; 15037c32e8f8SLorenz Bauer ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; 15047c32e8f8SLorenz Bauer break; 15057c32e8f8SLorenz Bauer #endif 15067c32e8f8SLorenz Bauer 15077c32e8f8SLorenz Bauer default: 15087c32e8f8SLorenz Bauer ret = -EAFNOSUPPORT; 15097c32e8f8SLorenz Bauer goto out; 15107c32e8f8SLorenz Bauer } 15117c32e8f8SLorenz Bauer 15127c32e8f8SLorenz Bauer progs = bpf_prog_array_alloc(1, GFP_KERNEL); 15137c32e8f8SLorenz Bauer if (!progs) { 15147c32e8f8SLorenz Bauer ret = -ENOMEM; 15157c32e8f8SLorenz Bauer goto out; 15167c32e8f8SLorenz Bauer } 15177c32e8f8SLorenz Bauer 15187c32e8f8SLorenz Bauer progs->items[0].prog = prog; 15197c32e8f8SLorenz Bauer 15207c32e8f8SLorenz Bauer bpf_test_timer_enter(&t); 15217c32e8f8SLorenz Bauer do { 15227c32e8f8SLorenz Bauer ctx.selected_sk = NULL; 1523fb7dd8bcSAndrii Nakryiko retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run); 1524b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 15257c32e8f8SLorenz Bauer bpf_test_timer_leave(&t); 15267c32e8f8SLorenz Bauer 15277c32e8f8SLorenz Bauer if (ret < 0) 15287c32e8f8SLorenz Bauer goto out; 15297c32e8f8SLorenz Bauer 15307c32e8f8SLorenz Bauer user_ctx->cookie = 0; 15317c32e8f8SLorenz Bauer if (ctx.selected_sk) { 15327c32e8f8SLorenz Bauer if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { 15337c32e8f8SLorenz Bauer ret = -EOPNOTSUPP; 15347c32e8f8SLorenz Bauer goto out; 15357c32e8f8SLorenz Bauer } 15367c32e8f8SLorenz Bauer 15377c32e8f8SLorenz Bauer user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); 15387c32e8f8SLorenz Bauer } 15397c32e8f8SLorenz Bauer 15407855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration); 15417c32e8f8SLorenz Bauer if (!ret) 15427c32e8f8SLorenz Bauer ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); 15437c32e8f8SLorenz Bauer 15447c32e8f8SLorenz Bauer out: 15457c32e8f8SLorenz Bauer bpf_prog_array_free(progs); 15467c32e8f8SLorenz Bauer kfree(user_ctx); 15477c32e8f8SLorenz Bauer return ret; 15487c32e8f8SLorenz Bauer } 154979a7f8bdSAlexei Starovoitov 155079a7f8bdSAlexei Starovoitov int bpf_prog_test_run_syscall(struct bpf_prog *prog, 155179a7f8bdSAlexei Starovoitov const union bpf_attr *kattr, 155279a7f8bdSAlexei Starovoitov union bpf_attr __user *uattr) 155379a7f8bdSAlexei Starovoitov { 155479a7f8bdSAlexei Starovoitov void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 155579a7f8bdSAlexei Starovoitov __u32 ctx_size_in = kattr->test.ctx_size_in; 155679a7f8bdSAlexei Starovoitov void *ctx = NULL; 155779a7f8bdSAlexei Starovoitov u32 retval; 155879a7f8bdSAlexei Starovoitov int err = 0; 155979a7f8bdSAlexei Starovoitov 156079a7f8bdSAlexei Starovoitov /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ 156179a7f8bdSAlexei Starovoitov if (kattr->test.data_in || kattr->test.data_out || 156279a7f8bdSAlexei Starovoitov kattr->test.ctx_out || kattr->test.duration || 1563b530e9e1SToke Høiland-Jørgensen kattr->test.repeat || kattr->test.flags || 1564b530e9e1SToke Høiland-Jørgensen kattr->test.batch_size) 156579a7f8bdSAlexei Starovoitov return -EINVAL; 156679a7f8bdSAlexei Starovoitov 156779a7f8bdSAlexei Starovoitov if (ctx_size_in < prog->aux->max_ctx_offset || 156879a7f8bdSAlexei Starovoitov ctx_size_in > U16_MAX) 156979a7f8bdSAlexei Starovoitov return -EINVAL; 157079a7f8bdSAlexei Starovoitov 157179a7f8bdSAlexei Starovoitov if (ctx_size_in) { 1572db5b6a46SQing Wang ctx = memdup_user(ctx_in, ctx_size_in); 1573db5b6a46SQing Wang if (IS_ERR(ctx)) 1574db5b6a46SQing Wang return PTR_ERR(ctx); 157579a7f8bdSAlexei Starovoitov } 157687b7b533SYonghong Song 157787b7b533SYonghong Song rcu_read_lock_trace(); 157879a7f8bdSAlexei Starovoitov retval = bpf_prog_run_pin_on_cpu(prog, ctx); 157987b7b533SYonghong Song rcu_read_unlock_trace(); 158079a7f8bdSAlexei Starovoitov 158179a7f8bdSAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { 158279a7f8bdSAlexei Starovoitov err = -EFAULT; 158379a7f8bdSAlexei Starovoitov goto out; 158479a7f8bdSAlexei Starovoitov } 158579a7f8bdSAlexei Starovoitov if (ctx_size_in) 158679a7f8bdSAlexei Starovoitov if (copy_to_user(ctx_in, ctx, ctx_size_in)) 158779a7f8bdSAlexei Starovoitov err = -EFAULT; 158879a7f8bdSAlexei Starovoitov out: 158979a7f8bdSAlexei Starovoitov kfree(ctx); 159079a7f8bdSAlexei Starovoitov return err; 159179a7f8bdSAlexei Starovoitov } 1592b202d844SKumar Kartikeya Dwivedi 1593b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { 1594b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE, 1595b202d844SKumar Kartikeya Dwivedi .check_set = &test_sk_check_kfunc_ids, 1596c1ff181fSKumar Kartikeya Dwivedi .acquire_set = &test_sk_acquire_kfunc_ids, 1597c1ff181fSKumar Kartikeya Dwivedi .release_set = &test_sk_release_kfunc_ids, 1598c1ff181fSKumar Kartikeya Dwivedi .ret_null_set = &test_sk_ret_null_kfunc_ids, 1599*05a945deSKumar Kartikeya Dwivedi .kptr_acquire_set = &test_sk_kptr_acquire_kfunc_ids 1600b202d844SKumar Kartikeya Dwivedi }; 1601b202d844SKumar Kartikeya Dwivedi 1602*05a945deSKumar Kartikeya Dwivedi BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids) 1603*05a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_ref_kfunc) 1604*05a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 1605*05a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_member) 1606*05a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_memb_release) 1607*05a945deSKumar Kartikeya Dwivedi 1608b202d844SKumar Kartikeya Dwivedi static int __init bpf_prog_test_run_init(void) 1609b202d844SKumar Kartikeya Dwivedi { 1610*05a945deSKumar Kartikeya Dwivedi const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = { 1611*05a945deSKumar Kartikeya Dwivedi { 1612*05a945deSKumar Kartikeya Dwivedi .btf_id = bpf_prog_test_dtor_kfunc_ids[0], 1613*05a945deSKumar Kartikeya Dwivedi .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1] 1614*05a945deSKumar Kartikeya Dwivedi }, 1615*05a945deSKumar Kartikeya Dwivedi { 1616*05a945deSKumar Kartikeya Dwivedi .btf_id = bpf_prog_test_dtor_kfunc_ids[2], 1617*05a945deSKumar Kartikeya Dwivedi .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3], 1618*05a945deSKumar Kartikeya Dwivedi }, 1619*05a945deSKumar Kartikeya Dwivedi }; 1620*05a945deSKumar Kartikeya Dwivedi int ret; 1621*05a945deSKumar Kartikeya Dwivedi 1622*05a945deSKumar Kartikeya Dwivedi ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); 1623*05a945deSKumar Kartikeya Dwivedi return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, 1624*05a945deSKumar Kartikeya Dwivedi ARRAY_SIZE(bpf_prog_test_dtor_kfunc), 1625*05a945deSKumar Kartikeya Dwivedi THIS_MODULE); 1626b202d844SKumar Kartikeya Dwivedi } 1627b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_prog_test_run_init); 1628