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; 111425d2393SToke Høiland-Jørgensen struct xdp_mem_info mem; 112b530e9e1SToke Høiland-Jørgensen u32 batch_size; 113b530e9e1SToke Høiland-Jørgensen u32 frame_cnt; 114b530e9e1SToke Høiland-Jørgensen }; 115b530e9e1SToke Høiland-Jørgensen 116b6f1f780SToke Høiland-Jørgensen #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head)) 117b530e9e1SToke Høiland-Jørgensen #define TEST_XDP_MAX_BATCH 256 118b530e9e1SToke Høiland-Jørgensen 119b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_init_page(struct page *page, void *arg) 120b530e9e1SToke Høiland-Jørgensen { 121b530e9e1SToke Høiland-Jørgensen struct xdp_page_head *head = phys_to_virt(page_to_phys(page)); 122b530e9e1SToke Høiland-Jørgensen struct xdp_buff *new_ctx, *orig_ctx; 123b530e9e1SToke Høiland-Jørgensen u32 headroom = XDP_PACKET_HEADROOM; 124b530e9e1SToke Høiland-Jørgensen struct xdp_test_data *xdp = arg; 125b530e9e1SToke Høiland-Jørgensen size_t frm_len, meta_len; 126b530e9e1SToke Høiland-Jørgensen struct xdp_frame *frm; 127b530e9e1SToke Høiland-Jørgensen void *data; 128b530e9e1SToke Høiland-Jørgensen 129b530e9e1SToke Høiland-Jørgensen orig_ctx = xdp->orig_ctx; 130b530e9e1SToke Høiland-Jørgensen frm_len = orig_ctx->data_end - orig_ctx->data_meta; 131b530e9e1SToke Høiland-Jørgensen meta_len = orig_ctx->data - orig_ctx->data_meta; 132b530e9e1SToke Høiland-Jørgensen headroom -= meta_len; 133b530e9e1SToke Høiland-Jørgensen 134b530e9e1SToke Høiland-Jørgensen new_ctx = &head->ctx; 135b530e9e1SToke Høiland-Jørgensen frm = &head->frm; 136b530e9e1SToke Høiland-Jørgensen data = &head->data; 137b530e9e1SToke Høiland-Jørgensen memcpy(data + headroom, orig_ctx->data_meta, frm_len); 138b530e9e1SToke Høiland-Jørgensen 139b530e9e1SToke Høiland-Jørgensen xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); 140b530e9e1SToke Høiland-Jørgensen xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); 141b530e9e1SToke Høiland-Jørgensen new_ctx->data = new_ctx->data_meta + meta_len; 142b530e9e1SToke Høiland-Jørgensen 143b530e9e1SToke Høiland-Jørgensen xdp_update_frame_from_buff(new_ctx, frm); 144b530e9e1SToke Høiland-Jørgensen frm->mem = new_ctx->rxq->mem; 145b530e9e1SToke Høiland-Jørgensen 146b530e9e1SToke Høiland-Jørgensen memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); 147b530e9e1SToke Høiland-Jørgensen } 148b530e9e1SToke Høiland-Jørgensen 149b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx) 150b530e9e1SToke Høiland-Jørgensen { 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 */ 177425d2393SToke Høiland-Jørgensen err = xdp_reg_mem_model(&xdp->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 { 205425d2393SToke Høiland-Jørgensen xdp_unreg_mem_model(&xdp->mem); 206b530e9e1SToke Høiland-Jørgensen page_pool_destroy(xdp->pp); 207b530e9e1SToke Høiland-Jørgensen kfree(xdp->frames); 208b530e9e1SToke Høiland-Jørgensen kfree(xdp->skbs); 209b530e9e1SToke Høiland-Jørgensen } 210b530e9e1SToke Høiland-Jørgensen 211b530e9e1SToke Høiland-Jørgensen static bool ctx_was_changed(struct xdp_page_head *head) 212b530e9e1SToke Høiland-Jørgensen { 213b530e9e1SToke Høiland-Jørgensen return head->orig_ctx.data != head->ctx.data || 214b530e9e1SToke Høiland-Jørgensen head->orig_ctx.data_meta != head->ctx.data_meta || 215b530e9e1SToke Høiland-Jørgensen head->orig_ctx.data_end != head->ctx.data_end; 216b530e9e1SToke Høiland-Jørgensen } 217b530e9e1SToke Høiland-Jørgensen 218b530e9e1SToke Høiland-Jørgensen static void reset_ctx(struct xdp_page_head *head) 219b530e9e1SToke Høiland-Jørgensen { 220b530e9e1SToke Høiland-Jørgensen if (likely(!ctx_was_changed(head))) 221b530e9e1SToke Høiland-Jørgensen return; 222b530e9e1SToke Høiland-Jørgensen 223b530e9e1SToke Høiland-Jørgensen head->ctx.data = head->orig_ctx.data; 224b530e9e1SToke Høiland-Jørgensen head->ctx.data_meta = head->orig_ctx.data_meta; 225b530e9e1SToke Høiland-Jørgensen head->ctx.data_end = head->orig_ctx.data_end; 226b530e9e1SToke Høiland-Jørgensen xdp_update_frame_from_buff(&head->ctx, &head->frm); 227b530e9e1SToke Høiland-Jørgensen } 228b530e9e1SToke Høiland-Jørgensen 229b530e9e1SToke Høiland-Jørgensen static int xdp_recv_frames(struct xdp_frame **frames, int nframes, 230b530e9e1SToke Høiland-Jørgensen struct sk_buff **skbs, 231b530e9e1SToke Høiland-Jørgensen struct net_device *dev) 232b530e9e1SToke Høiland-Jørgensen { 233b530e9e1SToke Høiland-Jørgensen gfp_t gfp = __GFP_ZERO | GFP_ATOMIC; 234b530e9e1SToke Høiland-Jørgensen int i, n; 235b530e9e1SToke Høiland-Jørgensen LIST_HEAD(list); 236b530e9e1SToke Høiland-Jørgensen 237b530e9e1SToke Høiland-Jørgensen n = kmem_cache_alloc_bulk(skbuff_head_cache, gfp, nframes, (void **)skbs); 238b530e9e1SToke Høiland-Jørgensen if (unlikely(n == 0)) { 239b530e9e1SToke Høiland-Jørgensen for (i = 0; i < nframes; i++) 240b530e9e1SToke Høiland-Jørgensen xdp_return_frame(frames[i]); 241b530e9e1SToke Høiland-Jørgensen return -ENOMEM; 242b530e9e1SToke Høiland-Jørgensen } 243b530e9e1SToke Høiland-Jørgensen 244b530e9e1SToke Høiland-Jørgensen for (i = 0; i < nframes; i++) { 245b530e9e1SToke Høiland-Jørgensen struct xdp_frame *xdpf = frames[i]; 246b530e9e1SToke Høiland-Jørgensen struct sk_buff *skb = skbs[i]; 247b530e9e1SToke Høiland-Jørgensen 248b530e9e1SToke Høiland-Jørgensen skb = __xdp_build_skb_from_frame(xdpf, skb, dev); 249b530e9e1SToke Høiland-Jørgensen if (!skb) { 250b530e9e1SToke Høiland-Jørgensen xdp_return_frame(xdpf); 251b530e9e1SToke Høiland-Jørgensen continue; 252b530e9e1SToke Høiland-Jørgensen } 253b530e9e1SToke Høiland-Jørgensen 254b530e9e1SToke Høiland-Jørgensen list_add_tail(&skb->list, &list); 255b530e9e1SToke Høiland-Jørgensen } 256b530e9e1SToke Høiland-Jørgensen netif_receive_skb_list(&list); 257b530e9e1SToke Høiland-Jørgensen 258b530e9e1SToke Høiland-Jørgensen return 0; 259b530e9e1SToke Høiland-Jørgensen } 260b530e9e1SToke Høiland-Jørgensen 261b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, 262b530e9e1SToke Høiland-Jørgensen u32 repeat) 263b530e9e1SToke Høiland-Jørgensen { 264b530e9e1SToke Høiland-Jørgensen struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); 265b530e9e1SToke Høiland-Jørgensen int err = 0, act, ret, i, nframes = 0, batch_sz; 266b530e9e1SToke Høiland-Jørgensen struct xdp_frame **frames = xdp->frames; 267b530e9e1SToke Høiland-Jørgensen struct xdp_page_head *head; 268b530e9e1SToke Høiland-Jørgensen struct xdp_frame *frm; 269b530e9e1SToke Høiland-Jørgensen bool redirect = false; 270b530e9e1SToke Høiland-Jørgensen struct xdp_buff *ctx; 271b530e9e1SToke Høiland-Jørgensen struct page *page; 272b530e9e1SToke Høiland-Jørgensen 273b530e9e1SToke Høiland-Jørgensen batch_sz = min_t(u32, repeat, xdp->batch_size); 274b530e9e1SToke Høiland-Jørgensen 275b530e9e1SToke Høiland-Jørgensen local_bh_disable(); 276b530e9e1SToke Høiland-Jørgensen xdp_set_return_frame_no_direct(); 277b530e9e1SToke Høiland-Jørgensen 278b530e9e1SToke Høiland-Jørgensen for (i = 0; i < batch_sz; i++) { 279b530e9e1SToke Høiland-Jørgensen page = page_pool_dev_alloc_pages(xdp->pp); 280b530e9e1SToke Høiland-Jørgensen if (!page) { 281b530e9e1SToke Høiland-Jørgensen err = -ENOMEM; 282b530e9e1SToke Høiland-Jørgensen goto out; 283b530e9e1SToke Høiland-Jørgensen } 284b530e9e1SToke Høiland-Jørgensen 285b530e9e1SToke Høiland-Jørgensen head = phys_to_virt(page_to_phys(page)); 286b530e9e1SToke Høiland-Jørgensen reset_ctx(head); 287b530e9e1SToke Høiland-Jørgensen ctx = &head->ctx; 288b530e9e1SToke Høiland-Jørgensen frm = &head->frm; 289b530e9e1SToke Høiland-Jørgensen xdp->frame_cnt++; 290b530e9e1SToke Høiland-Jørgensen 291b530e9e1SToke Høiland-Jørgensen act = bpf_prog_run_xdp(prog, ctx); 292b530e9e1SToke Høiland-Jørgensen 293b530e9e1SToke Høiland-Jørgensen /* if program changed pkt bounds we need to update the xdp_frame */ 294b530e9e1SToke Høiland-Jørgensen if (unlikely(ctx_was_changed(head))) { 295b530e9e1SToke Høiland-Jørgensen ret = xdp_update_frame_from_buff(ctx, frm); 296b530e9e1SToke Høiland-Jørgensen if (ret) { 297b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 298b530e9e1SToke Høiland-Jørgensen continue; 299b530e9e1SToke Høiland-Jørgensen } 300b530e9e1SToke Høiland-Jørgensen } 301b530e9e1SToke Høiland-Jørgensen 302b530e9e1SToke Høiland-Jørgensen switch (act) { 303b530e9e1SToke Høiland-Jørgensen case XDP_TX: 304b530e9e1SToke Høiland-Jørgensen /* we can't do a real XDP_TX since we're not in the 305b530e9e1SToke Høiland-Jørgensen * driver, so turn it into a REDIRECT back to the same 306b530e9e1SToke Høiland-Jørgensen * index 307b530e9e1SToke Høiland-Jørgensen */ 308b530e9e1SToke Høiland-Jørgensen ri->tgt_index = xdp->dev->ifindex; 309b530e9e1SToke Høiland-Jørgensen ri->map_id = INT_MAX; 310b530e9e1SToke Høiland-Jørgensen ri->map_type = BPF_MAP_TYPE_UNSPEC; 311b530e9e1SToke Høiland-Jørgensen fallthrough; 312b530e9e1SToke Høiland-Jørgensen case XDP_REDIRECT: 313b530e9e1SToke Høiland-Jørgensen redirect = true; 314b530e9e1SToke Høiland-Jørgensen ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog); 315b530e9e1SToke Høiland-Jørgensen if (ret) 316b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 317b530e9e1SToke Høiland-Jørgensen break; 318b530e9e1SToke Høiland-Jørgensen case XDP_PASS: 319b530e9e1SToke Høiland-Jørgensen frames[nframes++] = frm; 320b530e9e1SToke Høiland-Jørgensen break; 321b530e9e1SToke Høiland-Jørgensen default: 322b530e9e1SToke Høiland-Jørgensen bpf_warn_invalid_xdp_action(NULL, prog, act); 323b530e9e1SToke Høiland-Jørgensen fallthrough; 324b530e9e1SToke Høiland-Jørgensen case XDP_DROP: 325b530e9e1SToke Høiland-Jørgensen xdp_return_buff(ctx); 326b530e9e1SToke Høiland-Jørgensen break; 327b530e9e1SToke Høiland-Jørgensen } 328b530e9e1SToke Høiland-Jørgensen } 329b530e9e1SToke Høiland-Jørgensen 330b530e9e1SToke Høiland-Jørgensen out: 331b530e9e1SToke Høiland-Jørgensen if (redirect) 332b530e9e1SToke Høiland-Jørgensen xdp_do_flush(); 333b530e9e1SToke Høiland-Jørgensen if (nframes) { 334b530e9e1SToke Høiland-Jørgensen ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev); 335b530e9e1SToke Høiland-Jørgensen if (ret) 336b530e9e1SToke Høiland-Jørgensen err = ret; 337b530e9e1SToke Høiland-Jørgensen } 338b530e9e1SToke Høiland-Jørgensen 339b530e9e1SToke Høiland-Jørgensen xdp_clear_return_frame_no_direct(); 340b530e9e1SToke Høiland-Jørgensen local_bh_enable(); 341b530e9e1SToke Høiland-Jørgensen return err; 342b530e9e1SToke Høiland-Jørgensen } 343b530e9e1SToke Høiland-Jørgensen 344b530e9e1SToke Høiland-Jørgensen static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx, 345b530e9e1SToke Høiland-Jørgensen u32 repeat, u32 batch_size, u32 *time) 346b530e9e1SToke Høiland-Jørgensen 347b530e9e1SToke Høiland-Jørgensen { 348b530e9e1SToke Høiland-Jørgensen struct xdp_test_data xdp = { .batch_size = batch_size }; 349b530e9e1SToke Høiland-Jørgensen struct bpf_test_timer t = { .mode = NO_MIGRATE }; 350b530e9e1SToke Høiland-Jørgensen int ret; 351b530e9e1SToke Høiland-Jørgensen 352b530e9e1SToke Høiland-Jørgensen if (!repeat) 353b530e9e1SToke Høiland-Jørgensen repeat = 1; 354b530e9e1SToke Høiland-Jørgensen 355b530e9e1SToke Høiland-Jørgensen ret = xdp_test_run_setup(&xdp, ctx); 356b530e9e1SToke Høiland-Jørgensen if (ret) 357b530e9e1SToke Høiland-Jørgensen return ret; 358b530e9e1SToke Høiland-Jørgensen 359b530e9e1SToke Høiland-Jørgensen bpf_test_timer_enter(&t); 360b530e9e1SToke Høiland-Jørgensen do { 361b530e9e1SToke Høiland-Jørgensen xdp.frame_cnt = 0; 362b530e9e1SToke Høiland-Jørgensen ret = xdp_test_run_batch(&xdp, prog, repeat - t.i); 363b530e9e1SToke Høiland-Jørgensen if (unlikely(ret < 0)) 364b530e9e1SToke Høiland-Jørgensen break; 365b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time)); 366b530e9e1SToke Høiland-Jørgensen bpf_test_timer_leave(&t); 367b530e9e1SToke Høiland-Jørgensen 368b530e9e1SToke Høiland-Jørgensen xdp_test_run_teardown(&xdp); 369b530e9e1SToke Høiland-Jørgensen return ret; 370b530e9e1SToke Høiland-Jørgensen } 371b530e9e1SToke Høiland-Jørgensen 372df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, 373f23c4b39SBjörn Töpel u32 *retval, u32 *time, bool xdp) 3741cf1cae9SAlexei Starovoitov { 375c7603cfaSAndrii Nakryiko struct bpf_prog_array_item item = {.prog = prog}; 376c7603cfaSAndrii Nakryiko struct bpf_run_ctx *old_ctx; 377c7603cfaSAndrii Nakryiko struct bpf_cg_run_ctx run_ctx; 378607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_MIGRATE }; 3798bad74f9SRoman Gushchin enum bpf_cgroup_storage_type stype; 380607b9cc9SLorenz Bauer int ret; 3811cf1cae9SAlexei Starovoitov 3828bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) { 383c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype); 384c7603cfaSAndrii Nakryiko if (IS_ERR(item.cgroup_storage[stype])) { 385c7603cfaSAndrii Nakryiko item.cgroup_storage[stype] = NULL; 3868bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 387c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 3888bad74f9SRoman Gushchin return -ENOMEM; 3898bad74f9SRoman Gushchin } 3908bad74f9SRoman Gushchin } 391f42ee093SRoman Gushchin 3921cf1cae9SAlexei Starovoitov if (!repeat) 3931cf1cae9SAlexei Starovoitov repeat = 1; 394df1a2cb7SStanislav Fomichev 395607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 396c7603cfaSAndrii Nakryiko old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); 397607b9cc9SLorenz Bauer do { 398c7603cfaSAndrii Nakryiko run_ctx.prog_item = &item; 399f23c4b39SBjörn Töpel if (xdp) 400f23c4b39SBjörn Töpel *retval = bpf_prog_run_xdp(prog, ctx); 401f23c4b39SBjörn Töpel else 402fb7dd8bcSAndrii Nakryiko *retval = bpf_prog_run(prog, ctx); 403b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, time)); 404c7603cfaSAndrii Nakryiko bpf_reset_run_ctx(old_ctx); 405607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 4061cf1cae9SAlexei Starovoitov 4078bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 408c7603cfaSAndrii Nakryiko bpf_cgroup_storage_free(item.cgroup_storage[stype]); 409f42ee093SRoman Gushchin 410df1a2cb7SStanislav Fomichev return ret; 4111cf1cae9SAlexei Starovoitov } 4121cf1cae9SAlexei Starovoitov 41378e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr, 41478e52272SDavid Miller union bpf_attr __user *uattr, const void *data, 4157855e0dbSLorenzo Bianconi struct skb_shared_info *sinfo, u32 size, 4167855e0dbSLorenzo Bianconi u32 retval, u32 duration) 4171cf1cae9SAlexei Starovoitov { 41878e52272SDavid Miller void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 4191cf1cae9SAlexei Starovoitov int err = -EFAULT; 420b5a36b1eSLorenz Bauer u32 copy_size = size; 4211cf1cae9SAlexei Starovoitov 422b5a36b1eSLorenz Bauer /* Clamp copy if the user has provided a size hint, but copy the full 423b5a36b1eSLorenz Bauer * buffer if not to retain old behaviour. 424b5a36b1eSLorenz Bauer */ 425b5a36b1eSLorenz Bauer if (kattr->test.data_size_out && 426b5a36b1eSLorenz Bauer copy_size > kattr->test.data_size_out) { 427b5a36b1eSLorenz Bauer copy_size = kattr->test.data_size_out; 428b5a36b1eSLorenz Bauer err = -ENOSPC; 429b5a36b1eSLorenz Bauer } 430b5a36b1eSLorenz Bauer 4317855e0dbSLorenzo Bianconi if (data_out) { 4327855e0dbSLorenzo Bianconi int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size; 4337855e0dbSLorenzo Bianconi 434530e214cSStanislav Fomichev if (len < 0) { 435530e214cSStanislav Fomichev err = -ENOSPC; 436530e214cSStanislav Fomichev goto out; 437530e214cSStanislav Fomichev } 438530e214cSStanislav Fomichev 4397855e0dbSLorenzo Bianconi if (copy_to_user(data_out, data, len)) 4401cf1cae9SAlexei Starovoitov goto out; 4417855e0dbSLorenzo Bianconi 4427855e0dbSLorenzo Bianconi if (sinfo) { 4435d1e9f43SStanislav Fomichev int i, offset = len; 4445d1e9f43SStanislav Fomichev u32 data_len; 4457855e0dbSLorenzo Bianconi 4467855e0dbSLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) { 4477855e0dbSLorenzo Bianconi skb_frag_t *frag = &sinfo->frags[i]; 4487855e0dbSLorenzo Bianconi 4497855e0dbSLorenzo Bianconi if (offset >= copy_size) { 4507855e0dbSLorenzo Bianconi err = -ENOSPC; 4517855e0dbSLorenzo Bianconi break; 4527855e0dbSLorenzo Bianconi } 4537855e0dbSLorenzo Bianconi 4545d1e9f43SStanislav Fomichev data_len = min_t(u32, copy_size - offset, 4557855e0dbSLorenzo Bianconi skb_frag_size(frag)); 4567855e0dbSLorenzo Bianconi 4577855e0dbSLorenzo Bianconi if (copy_to_user(data_out + offset, 4587855e0dbSLorenzo Bianconi skb_frag_address(frag), 4597855e0dbSLorenzo Bianconi data_len)) 4607855e0dbSLorenzo Bianconi goto out; 4617855e0dbSLorenzo Bianconi 4627855e0dbSLorenzo Bianconi offset += data_len; 4637855e0dbSLorenzo Bianconi } 4647855e0dbSLorenzo Bianconi } 4657855e0dbSLorenzo Bianconi } 4667855e0dbSLorenzo Bianconi 4671cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 4681cf1cae9SAlexei Starovoitov goto out; 4691cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 4701cf1cae9SAlexei Starovoitov goto out; 4711cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 4721cf1cae9SAlexei Starovoitov goto out; 473b5a36b1eSLorenz Bauer if (err != -ENOSPC) 4741cf1cae9SAlexei Starovoitov err = 0; 4751cf1cae9SAlexei Starovoitov out: 476e950e843SMatt Mullins trace_bpf_test_finish(&err); 4771cf1cae9SAlexei Starovoitov return err; 4781cf1cae9SAlexei Starovoitov } 4791cf1cae9SAlexei Starovoitov 480faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of 481faeb2dceSAlexei Starovoitov * architecture dependent calling conventions. 7+ can be supported in the 482faeb2dceSAlexei Starovoitov * future. 483faeb2dceSAlexei Starovoitov */ 484e9ff9d52SJean-Philippe Menil __diag_push(); 4850b206c6dSKumar Kartikeya Dwivedi __diag_ignore_all("-Wmissing-prototypes", 486e9ff9d52SJean-Philippe Menil "Global functions as their definitions will be in vmlinux BTF"); 487faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test1(int a) 488faeb2dceSAlexei Starovoitov { 489faeb2dceSAlexei Starovoitov return a + 1; 490faeb2dceSAlexei Starovoitov } 49146565696SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(bpf_fentry_test1); 49246565696SKumar Kartikeya Dwivedi ALLOW_ERROR_INJECTION(bpf_fentry_test1, ERRNO); 493faeb2dceSAlexei Starovoitov 494faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b) 495faeb2dceSAlexei Starovoitov { 496faeb2dceSAlexei Starovoitov return a + b; 497faeb2dceSAlexei Starovoitov } 498faeb2dceSAlexei Starovoitov 499faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c) 500faeb2dceSAlexei Starovoitov { 501faeb2dceSAlexei Starovoitov return a + b + c; 502faeb2dceSAlexei Starovoitov } 503faeb2dceSAlexei Starovoitov 504faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) 505faeb2dceSAlexei Starovoitov { 506faeb2dceSAlexei Starovoitov return (long)a + b + c + d; 507faeb2dceSAlexei Starovoitov } 508faeb2dceSAlexei Starovoitov 509faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) 510faeb2dceSAlexei Starovoitov { 511faeb2dceSAlexei Starovoitov return a + (long)b + c + d + e; 512faeb2dceSAlexei Starovoitov } 513faeb2dceSAlexei Starovoitov 514faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) 515faeb2dceSAlexei Starovoitov { 516faeb2dceSAlexei Starovoitov return a + (long)b + c + d + (long)e + f; 517faeb2dceSAlexei Starovoitov } 518faeb2dceSAlexei Starovoitov 519d923021cSYonghong Song struct bpf_fentry_test_t { 520d923021cSYonghong Song struct bpf_fentry_test_t *a; 521d923021cSYonghong Song }; 522d923021cSYonghong Song 523d923021cSYonghong Song int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg) 524d923021cSYonghong Song { 525d923021cSYonghong Song return (long)arg; 526d923021cSYonghong Song } 527d923021cSYonghong Song 528d923021cSYonghong Song int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg) 529d923021cSYonghong Song { 530d923021cSYonghong Song return (long)arg->a; 531d923021cSYonghong Song } 532d923021cSYonghong Song 5333d08b6f2SKP Singh int noinline bpf_modify_return_test(int a, int *b) 5343d08b6f2SKP Singh { 5353d08b6f2SKP Singh *b += 1; 5363d08b6f2SKP Singh return a + *b; 5373d08b6f2SKP Singh } 5387bd1590dSMartin KaFai Lau 5397bd1590dSMartin KaFai Lau u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d) 5407bd1590dSMartin KaFai Lau { 5417bd1590dSMartin KaFai Lau return a + b + c + d; 5427bd1590dSMartin KaFai Lau } 5437bd1590dSMartin KaFai Lau 5447bd1590dSMartin KaFai Lau int noinline bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b) 5457bd1590dSMartin KaFai Lau { 5467bd1590dSMartin KaFai Lau return a + b; 5477bd1590dSMartin KaFai Lau } 5487bd1590dSMartin KaFai Lau 5497bd1590dSMartin KaFai Lau struct sock * noinline bpf_kfunc_call_test3(struct sock *sk) 5507bd1590dSMartin KaFai Lau { 5517bd1590dSMartin KaFai Lau return sk; 5527bd1590dSMartin KaFai Lau } 5537bd1590dSMartin KaFai Lau 554792c0a34SKumar Kartikeya Dwivedi struct prog_test_member1 { 555792c0a34SKumar Kartikeya Dwivedi int a; 556792c0a34SKumar Kartikeya Dwivedi }; 557792c0a34SKumar Kartikeya Dwivedi 5588218ccb5SKumar Kartikeya Dwivedi struct prog_test_member { 559792c0a34SKumar Kartikeya Dwivedi struct prog_test_member1 m; 560792c0a34SKumar Kartikeya Dwivedi int c; 5618218ccb5SKumar Kartikeya Dwivedi }; 5628218ccb5SKumar Kartikeya Dwivedi 563c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc { 564c1ff181fSKumar Kartikeya Dwivedi int a; 565c1ff181fSKumar Kartikeya Dwivedi int b; 5668218ccb5SKumar Kartikeya Dwivedi struct prog_test_member memb; 567c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc *next; 5685cdccadcSKumar Kartikeya Dwivedi refcount_t cnt; 569c1ff181fSKumar Kartikeya Dwivedi }; 570c1ff181fSKumar Kartikeya Dwivedi 571c1ff181fSKumar Kartikeya Dwivedi static struct prog_test_ref_kfunc prog_test_struct = { 572c1ff181fSKumar Kartikeya Dwivedi .a = 42, 573c1ff181fSKumar Kartikeya Dwivedi .b = 108, 574c1ff181fSKumar Kartikeya Dwivedi .next = &prog_test_struct, 5755cdccadcSKumar Kartikeya Dwivedi .cnt = REFCOUNT_INIT(1), 576c1ff181fSKumar Kartikeya Dwivedi }; 577c1ff181fSKumar Kartikeya Dwivedi 578c1ff181fSKumar Kartikeya Dwivedi noinline struct prog_test_ref_kfunc * 579c1ff181fSKumar Kartikeya Dwivedi bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr) 580c1ff181fSKumar Kartikeya Dwivedi { 5815cdccadcSKumar Kartikeya Dwivedi refcount_inc(&prog_test_struct.cnt); 582c1ff181fSKumar Kartikeya Dwivedi return &prog_test_struct; 583c1ff181fSKumar Kartikeya Dwivedi } 584c1ff181fSKumar Kartikeya Dwivedi 585792c0a34SKumar Kartikeya Dwivedi noinline struct prog_test_member * 586792c0a34SKumar Kartikeya Dwivedi bpf_kfunc_call_memb_acquire(void) 587792c0a34SKumar Kartikeya Dwivedi { 5885cdccadcSKumar Kartikeya Dwivedi WARN_ON_ONCE(1); 5895cdccadcSKumar Kartikeya Dwivedi return NULL; 590792c0a34SKumar Kartikeya Dwivedi } 591792c0a34SKumar Kartikeya Dwivedi 592c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) 593c1ff181fSKumar Kartikeya Dwivedi { 5945cdccadcSKumar Kartikeya Dwivedi if (!p) 5955cdccadcSKumar Kartikeya Dwivedi return; 5965cdccadcSKumar Kartikeya Dwivedi 5975cdccadcSKumar Kartikeya Dwivedi refcount_dec(&p->cnt); 598c1ff181fSKumar Kartikeya Dwivedi } 599c1ff181fSKumar Kartikeya Dwivedi 6008218ccb5SKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p) 6018218ccb5SKumar Kartikeya Dwivedi { 6028218ccb5SKumar Kartikeya Dwivedi } 6038218ccb5SKumar Kartikeya Dwivedi 604792c0a34SKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p) 605792c0a34SKumar Kartikeya Dwivedi { 6065cdccadcSKumar Kartikeya Dwivedi WARN_ON_ONCE(1); 607792c0a34SKumar Kartikeya Dwivedi } 608792c0a34SKumar Kartikeya Dwivedi 60905a945deSKumar Kartikeya Dwivedi noinline struct prog_test_ref_kfunc * 6105cdccadcSKumar Kartikeya Dwivedi bpf_kfunc_call_test_kptr_get(struct prog_test_ref_kfunc **pp, int a, int b) 61105a945deSKumar Kartikeya Dwivedi { 6125cdccadcSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc *p = READ_ONCE(*pp); 6135cdccadcSKumar Kartikeya Dwivedi 6145cdccadcSKumar Kartikeya Dwivedi if (!p) 6155cdccadcSKumar Kartikeya Dwivedi return NULL; 6165cdccadcSKumar Kartikeya Dwivedi refcount_inc(&p->cnt); 6175cdccadcSKumar Kartikeya Dwivedi return p; 61805a945deSKumar Kartikeya Dwivedi } 61905a945deSKumar Kartikeya Dwivedi 620c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 { 621c1ff181fSKumar Kartikeya Dwivedi int x0; 622c1ff181fSKumar Kartikeya Dwivedi struct { 623c1ff181fSKumar Kartikeya Dwivedi int x1; 624c1ff181fSKumar Kartikeya Dwivedi struct { 625c1ff181fSKumar Kartikeya Dwivedi int x2; 626c1ff181fSKumar Kartikeya Dwivedi struct { 627c1ff181fSKumar Kartikeya Dwivedi int x3; 628c1ff181fSKumar Kartikeya Dwivedi }; 629c1ff181fSKumar Kartikeya Dwivedi }; 630c1ff181fSKumar Kartikeya Dwivedi }; 631c1ff181fSKumar Kartikeya Dwivedi }; 632c1ff181fSKumar Kartikeya Dwivedi 633c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass2 { 634c1ff181fSKumar Kartikeya Dwivedi int len; 635c1ff181fSKumar Kartikeya Dwivedi short arr1[4]; 636c1ff181fSKumar Kartikeya Dwivedi struct { 637c1ff181fSKumar Kartikeya Dwivedi char arr2[4]; 638c1ff181fSKumar Kartikeya Dwivedi unsigned long arr3[8]; 639c1ff181fSKumar Kartikeya Dwivedi } x; 640c1ff181fSKumar Kartikeya Dwivedi }; 641c1ff181fSKumar Kartikeya Dwivedi 642c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail1 { 643c1ff181fSKumar Kartikeya Dwivedi void *p; 644c1ff181fSKumar Kartikeya Dwivedi int x; 645c1ff181fSKumar Kartikeya Dwivedi }; 646c1ff181fSKumar Kartikeya Dwivedi 647c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail2 { 648c1ff181fSKumar Kartikeya Dwivedi int x8; 649c1ff181fSKumar Kartikeya Dwivedi struct prog_test_pass1 x; 650c1ff181fSKumar Kartikeya Dwivedi }; 651c1ff181fSKumar Kartikeya Dwivedi 652c1ff181fSKumar Kartikeya Dwivedi struct prog_test_fail3 { 653c1ff181fSKumar Kartikeya Dwivedi int len; 654c1ff181fSKumar Kartikeya Dwivedi char arr1[2]; 655ed8bb032Skernel test robot char arr2[]; 656c1ff181fSKumar Kartikeya Dwivedi }; 657c1ff181fSKumar Kartikeya Dwivedi 658c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) 659c1ff181fSKumar Kartikeya Dwivedi { 660c1ff181fSKumar Kartikeya Dwivedi } 661c1ff181fSKumar Kartikeya Dwivedi 662c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) 663c1ff181fSKumar Kartikeya Dwivedi { 664c1ff181fSKumar Kartikeya Dwivedi } 665c1ff181fSKumar Kartikeya Dwivedi 666c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) 667c1ff181fSKumar Kartikeya Dwivedi { 668c1ff181fSKumar Kartikeya Dwivedi } 669c1ff181fSKumar Kartikeya Dwivedi 670c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) 671c1ff181fSKumar Kartikeya Dwivedi { 672c1ff181fSKumar Kartikeya Dwivedi } 673c1ff181fSKumar Kartikeya Dwivedi 674c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p) 675c1ff181fSKumar Kartikeya Dwivedi { 676c1ff181fSKumar Kartikeya Dwivedi } 677c1ff181fSKumar Kartikeya Dwivedi 678c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p) 679c1ff181fSKumar Kartikeya Dwivedi { 680c1ff181fSKumar Kartikeya Dwivedi } 681c1ff181fSKumar Kartikeya Dwivedi 682c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz) 683c1ff181fSKumar Kartikeya Dwivedi { 684c1ff181fSKumar Kartikeya Dwivedi } 685c1ff181fSKumar Kartikeya Dwivedi 686c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len) 687c1ff181fSKumar Kartikeya Dwivedi { 688c1ff181fSKumar Kartikeya Dwivedi } 689c1ff181fSKumar Kartikeya Dwivedi 690c1ff181fSKumar Kartikeya Dwivedi noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) 691c1ff181fSKumar Kartikeya Dwivedi { 692c1ff181fSKumar Kartikeya Dwivedi } 693c1ff181fSKumar Kartikeya Dwivedi 694e9ff9d52SJean-Philippe Menil __diag_pop(); 6953d08b6f2SKP Singh 6963d08b6f2SKP Singh ALLOW_ERROR_INJECTION(bpf_modify_return_test, ERRNO); 6973d08b6f2SKP Singh 698*a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(test_sk_check_kfunc_ids) 699*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test1) 700*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test2) 701*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test3) 702*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL) 703*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL) 704*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE) 705*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE) 706*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE) 707*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_kptr_get, KF_ACQUIRE | KF_RET_NULL | KF_KPTR_GET) 708*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx) 709*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1) 710*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2) 711*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1) 712*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2) 713*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3) 714*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1) 715*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1) 716*a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2) 717*a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(test_sk_check_kfunc_ids) 71805a945deSKumar Kartikeya Dwivedi 719be3d72a2SLorenzo Bianconi static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, 720be3d72a2SLorenzo Bianconi u32 size, u32 headroom, u32 tailroom) 7211cf1cae9SAlexei Starovoitov { 7221cf1cae9SAlexei Starovoitov void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 7231cf1cae9SAlexei Starovoitov void *data; 7241cf1cae9SAlexei Starovoitov 7251cf1cae9SAlexei Starovoitov if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) 7261cf1cae9SAlexei Starovoitov return ERR_PTR(-EINVAL); 7271cf1cae9SAlexei Starovoitov 728d800bad6SJesper Dangaard Brouer if (user_size > size) 729d800bad6SJesper Dangaard Brouer return ERR_PTR(-EMSGSIZE); 730d800bad6SJesper Dangaard Brouer 7311cf1cae9SAlexei Starovoitov data = kzalloc(size + headroom + tailroom, GFP_USER); 7321cf1cae9SAlexei Starovoitov if (!data) 7331cf1cae9SAlexei Starovoitov return ERR_PTR(-ENOMEM); 7341cf1cae9SAlexei Starovoitov 735d800bad6SJesper Dangaard Brouer if (copy_from_user(data + headroom, data_in, user_size)) { 7361cf1cae9SAlexei Starovoitov kfree(data); 7371cf1cae9SAlexei Starovoitov return ERR_PTR(-EFAULT); 7381cf1cae9SAlexei Starovoitov } 739da00d2f1SKP Singh 740da00d2f1SKP Singh return data; 741da00d2f1SKP Singh } 742da00d2f1SKP Singh 743da00d2f1SKP Singh int bpf_prog_test_run_tracing(struct bpf_prog *prog, 744da00d2f1SKP Singh const union bpf_attr *kattr, 745da00d2f1SKP Singh union bpf_attr __user *uattr) 746da00d2f1SKP Singh { 747d923021cSYonghong Song struct bpf_fentry_test_t arg = {}; 7483d08b6f2SKP Singh u16 side_effect = 0, ret = 0; 7493d08b6f2SKP Singh int b = 2, err = -EFAULT; 7503d08b6f2SKP Singh u32 retval = 0; 751da00d2f1SKP Singh 752b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 7531b4d60ecSSong Liu return -EINVAL; 7541b4d60ecSSong Liu 755da00d2f1SKP Singh switch (prog->expected_attach_type) { 756da00d2f1SKP Singh case BPF_TRACE_FENTRY: 757da00d2f1SKP Singh case BPF_TRACE_FEXIT: 758faeb2dceSAlexei Starovoitov if (bpf_fentry_test1(1) != 2 || 759faeb2dceSAlexei Starovoitov bpf_fentry_test2(2, 3) != 5 || 760faeb2dceSAlexei Starovoitov bpf_fentry_test3(4, 5, 6) != 15 || 761faeb2dceSAlexei Starovoitov bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || 762faeb2dceSAlexei Starovoitov bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || 763d923021cSYonghong Song bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 || 764d923021cSYonghong Song bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 || 765d923021cSYonghong Song bpf_fentry_test8(&arg) != 0) 766da00d2f1SKP Singh goto out; 767da00d2f1SKP Singh break; 7683d08b6f2SKP Singh case BPF_MODIFY_RETURN: 7693d08b6f2SKP Singh ret = bpf_modify_return_test(1, &b); 7703d08b6f2SKP Singh if (b != 2) 7713d08b6f2SKP Singh side_effect = 1; 7723d08b6f2SKP Singh break; 773da00d2f1SKP Singh default: 774da00d2f1SKP Singh goto out; 775a25ecd9dSColin Ian King } 776da00d2f1SKP Singh 7773d08b6f2SKP Singh retval = ((u32)side_effect << 16) | ret; 7783d08b6f2SKP Singh if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 7793d08b6f2SKP Singh goto out; 7803d08b6f2SKP Singh 781da00d2f1SKP Singh err = 0; 782da00d2f1SKP Singh out: 783da00d2f1SKP Singh trace_bpf_test_finish(&err); 784da00d2f1SKP Singh return err; 7851cf1cae9SAlexei Starovoitov } 7861cf1cae9SAlexei Starovoitov 7871b4d60ecSSong Liu struct bpf_raw_tp_test_run_info { 7881b4d60ecSSong Liu struct bpf_prog *prog; 7891b4d60ecSSong Liu void *ctx; 7901b4d60ecSSong Liu u32 retval; 7911b4d60ecSSong Liu }; 7921b4d60ecSSong Liu 7931b4d60ecSSong Liu static void 7941b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(void *data) 7951b4d60ecSSong Liu { 7961b4d60ecSSong Liu struct bpf_raw_tp_test_run_info *info = data; 7971b4d60ecSSong Liu 7981b4d60ecSSong Liu rcu_read_lock(); 799fb7dd8bcSAndrii Nakryiko info->retval = bpf_prog_run(info->prog, info->ctx); 8001b4d60ecSSong Liu rcu_read_unlock(); 8011b4d60ecSSong Liu } 8021b4d60ecSSong Liu 8031b4d60ecSSong Liu int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, 8041b4d60ecSSong Liu const union bpf_attr *kattr, 8051b4d60ecSSong Liu union bpf_attr __user *uattr) 8061b4d60ecSSong Liu { 8071b4d60ecSSong Liu void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 8081b4d60ecSSong Liu __u32 ctx_size_in = kattr->test.ctx_size_in; 8091b4d60ecSSong Liu struct bpf_raw_tp_test_run_info info; 8101b4d60ecSSong Liu int cpu = kattr->test.cpu, err = 0; 811963ec27aSSong Liu int current_cpu; 8121b4d60ecSSong Liu 8131b4d60ecSSong Liu /* doesn't support data_in/out, ctx_out, duration, or repeat */ 8141b4d60ecSSong Liu if (kattr->test.data_in || kattr->test.data_out || 8151b4d60ecSSong Liu kattr->test.ctx_out || kattr->test.duration || 816b530e9e1SToke Høiland-Jørgensen kattr->test.repeat || kattr->test.batch_size) 8171b4d60ecSSong Liu return -EINVAL; 8181b4d60ecSSong Liu 8197ac6ad05SSong Liu if (ctx_size_in < prog->aux->max_ctx_offset || 8207ac6ad05SSong Liu ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64)) 8211b4d60ecSSong Liu return -EINVAL; 8221b4d60ecSSong Liu 8231b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0) 8241b4d60ecSSong Liu return -EINVAL; 8251b4d60ecSSong Liu 8261b4d60ecSSong Liu if (ctx_size_in) { 827db5b6a46SQing Wang info.ctx = memdup_user(ctx_in, ctx_size_in); 828db5b6a46SQing Wang if (IS_ERR(info.ctx)) 829db5b6a46SQing Wang return PTR_ERR(info.ctx); 8301b4d60ecSSong Liu } else { 8311b4d60ecSSong Liu info.ctx = NULL; 8321b4d60ecSSong Liu } 8331b4d60ecSSong Liu 8341b4d60ecSSong Liu info.prog = prog; 8351b4d60ecSSong Liu 836963ec27aSSong Liu current_cpu = get_cpu(); 8371b4d60ecSSong Liu if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 || 838963ec27aSSong Liu cpu == current_cpu) { 8391b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(&info); 840963ec27aSSong Liu } else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { 8411b4d60ecSSong Liu /* smp_call_function_single() also checks cpu_online() 8421b4d60ecSSong Liu * after csd_lock(). However, since cpu is from user 8431b4d60ecSSong Liu * space, let's do an extra quick check to filter out 8441b4d60ecSSong Liu * invalid value before smp_call_function_single(). 8451b4d60ecSSong Liu */ 8461b4d60ecSSong Liu err = -ENXIO; 847963ec27aSSong Liu } else { 8481b4d60ecSSong Liu err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp, 8491b4d60ecSSong Liu &info, 1); 8501b4d60ecSSong Liu } 851963ec27aSSong Liu put_cpu(); 8521b4d60ecSSong Liu 853963ec27aSSong Liu if (!err && 854963ec27aSSong Liu copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32))) 8551b4d60ecSSong Liu err = -EFAULT; 8561b4d60ecSSong Liu 8571b4d60ecSSong Liu kfree(info.ctx); 8581b4d60ecSSong Liu return err; 8591b4d60ecSSong Liu } 8601b4d60ecSSong Liu 861b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) 862b0b9395dSStanislav Fomichev { 863b0b9395dSStanislav Fomichev void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); 864b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 865b0b9395dSStanislav Fomichev u32 size = kattr->test.ctx_size_in; 866b0b9395dSStanislav Fomichev void *data; 867b0b9395dSStanislav Fomichev int err; 868b0b9395dSStanislav Fomichev 869b0b9395dSStanislav Fomichev if (!data_in && !data_out) 870b0b9395dSStanislav Fomichev return NULL; 871b0b9395dSStanislav Fomichev 872b0b9395dSStanislav Fomichev data = kzalloc(max_size, GFP_USER); 873b0b9395dSStanislav Fomichev if (!data) 874b0b9395dSStanislav Fomichev return ERR_PTR(-ENOMEM); 875b0b9395dSStanislav Fomichev 876b0b9395dSStanislav Fomichev if (data_in) { 877af2ac3e1SAlexei Starovoitov err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size); 878b0b9395dSStanislav Fomichev if (err) { 879b0b9395dSStanislav Fomichev kfree(data); 880b0b9395dSStanislav Fomichev return ERR_PTR(err); 881b0b9395dSStanislav Fomichev } 882b0b9395dSStanislav Fomichev 883b0b9395dSStanislav Fomichev size = min_t(u32, max_size, size); 884b0b9395dSStanislav Fomichev if (copy_from_user(data, data_in, size)) { 885b0b9395dSStanislav Fomichev kfree(data); 886b0b9395dSStanislav Fomichev return ERR_PTR(-EFAULT); 887b0b9395dSStanislav Fomichev } 888b0b9395dSStanislav Fomichev } 889b0b9395dSStanislav Fomichev return data; 890b0b9395dSStanislav Fomichev } 891b0b9395dSStanislav Fomichev 892b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr, 893b0b9395dSStanislav Fomichev union bpf_attr __user *uattr, const void *data, 894b0b9395dSStanislav Fomichev u32 size) 895b0b9395dSStanislav Fomichev { 896b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 897b0b9395dSStanislav Fomichev int err = -EFAULT; 898b0b9395dSStanislav Fomichev u32 copy_size = size; 899b0b9395dSStanislav Fomichev 900b0b9395dSStanislav Fomichev if (!data || !data_out) 901b0b9395dSStanislav Fomichev return 0; 902b0b9395dSStanislav Fomichev 903b0b9395dSStanislav Fomichev if (copy_size > kattr->test.ctx_size_out) { 904b0b9395dSStanislav Fomichev copy_size = kattr->test.ctx_size_out; 905b0b9395dSStanislav Fomichev err = -ENOSPC; 906b0b9395dSStanislav Fomichev } 907b0b9395dSStanislav Fomichev 908b0b9395dSStanislav Fomichev if (copy_to_user(data_out, data, copy_size)) 909b0b9395dSStanislav Fomichev goto out; 910b0b9395dSStanislav Fomichev if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) 911b0b9395dSStanislav Fomichev goto out; 912b0b9395dSStanislav Fomichev if (err != -ENOSPC) 913b0b9395dSStanislav Fomichev err = 0; 914b0b9395dSStanislav Fomichev out: 915b0b9395dSStanislav Fomichev return err; 916b0b9395dSStanislav Fomichev } 917b0b9395dSStanislav Fomichev 918b0b9395dSStanislav Fomichev /** 919b0b9395dSStanislav Fomichev * range_is_zero - test whether buffer is initialized 920b0b9395dSStanislav Fomichev * @buf: buffer to check 921b0b9395dSStanislav Fomichev * @from: check from this position 922b0b9395dSStanislav Fomichev * @to: check up until (excluding) this position 923b0b9395dSStanislav Fomichev * 924b0b9395dSStanislav Fomichev * This function returns true if the there is a non-zero byte 925b0b9395dSStanislav Fomichev * in the buf in the range [from,to). 926b0b9395dSStanislav Fomichev */ 927b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to) 928b0b9395dSStanislav Fomichev { 929b0b9395dSStanislav Fomichev return !memchr_inv((u8 *)buf + from, 0, to - from); 930b0b9395dSStanislav Fomichev } 931b0b9395dSStanislav Fomichev 932b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) 933b0b9395dSStanislav Fomichev { 934b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 935b0b9395dSStanislav Fomichev 936fd189422SZhengchao Shao if (!skb->len) 937fd189422SZhengchao Shao return -EINVAL; 938fd189422SZhengchao Shao 939b0b9395dSStanislav Fomichev if (!__skb) 940b0b9395dSStanislav Fomichev return 0; 941b0b9395dSStanislav Fomichev 942b0b9395dSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 9436de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark))) 9446de6c1f8SNikita V. Shirokov return -EINVAL; 9456de6c1f8SNikita V. Shirokov 9466de6c1f8SNikita V. Shirokov /* mark is allowed */ 9476de6c1f8SNikita V. Shirokov 9486de6c1f8SNikita V. Shirokov if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark), 9496de6c1f8SNikita V. Shirokov offsetof(struct __sk_buff, priority))) 950b0b9395dSStanislav Fomichev return -EINVAL; 951b0b9395dSStanislav Fomichev 952b0b9395dSStanislav Fomichev /* priority is allowed */ 953b238290bSNeil Spring /* ingress_ifindex is allowed */ 95421594c44SDmitry Yakunin /* ifindex is allowed */ 95521594c44SDmitry Yakunin 95621594c44SDmitry Yakunin if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex), 957b0b9395dSStanislav Fomichev offsetof(struct __sk_buff, cb))) 958b0b9395dSStanislav Fomichev return -EINVAL; 959b0b9395dSStanislav Fomichev 960b0b9395dSStanislav Fomichev /* cb is allowed */ 961b0b9395dSStanislav Fomichev 962b590cb5fSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb), 963ba940948SStanislav Fomichev offsetof(struct __sk_buff, tstamp))) 964ba940948SStanislav Fomichev return -EINVAL; 965ba940948SStanislav Fomichev 966ba940948SStanislav Fomichev /* tstamp is allowed */ 967850a88ccSStanislav Fomichev /* wire_len is allowed */ 968850a88ccSStanislav Fomichev /* gso_segs is allowed */ 969ba940948SStanislav Fomichev 970850a88ccSStanislav Fomichev if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs), 971cf62089bSWillem de Bruijn offsetof(struct __sk_buff, gso_size))) 972cf62089bSWillem de Bruijn return -EINVAL; 973cf62089bSWillem de Bruijn 974cf62089bSWillem de Bruijn /* gso_size is allowed */ 975cf62089bSWillem de Bruijn 976cf62089bSWillem de Bruijn if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size), 9773384c7c7SVadim Fedorenko offsetof(struct __sk_buff, hwtstamp))) 9783384c7c7SVadim Fedorenko return -EINVAL; 9793384c7c7SVadim Fedorenko 9803384c7c7SVadim Fedorenko /* hwtstamp is allowed */ 9813384c7c7SVadim Fedorenko 9823384c7c7SVadim Fedorenko if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp), 983b0b9395dSStanislav Fomichev sizeof(struct __sk_buff))) 984b0b9395dSStanislav Fomichev return -EINVAL; 985b0b9395dSStanislav Fomichev 9866de6c1f8SNikita V. Shirokov skb->mark = __skb->mark; 987b0b9395dSStanislav Fomichev skb->priority = __skb->priority; 988b238290bSNeil Spring skb->skb_iif = __skb->ingress_ifindex; 989ba940948SStanislav Fomichev skb->tstamp = __skb->tstamp; 990b0b9395dSStanislav Fomichev memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); 991b0b9395dSStanislav Fomichev 992850a88ccSStanislav Fomichev if (__skb->wire_len == 0) { 993850a88ccSStanislav Fomichev cb->pkt_len = skb->len; 994850a88ccSStanislav Fomichev } else { 995850a88ccSStanislav Fomichev if (__skb->wire_len < skb->len || 9967c4e983cSAlexander Duyck __skb->wire_len > GSO_LEGACY_MAX_SIZE) 997850a88ccSStanislav Fomichev return -EINVAL; 998850a88ccSStanislav Fomichev cb->pkt_len = __skb->wire_len; 999850a88ccSStanislav Fomichev } 1000850a88ccSStanislav Fomichev 1001850a88ccSStanislav Fomichev if (__skb->gso_segs > GSO_MAX_SEGS) 1002850a88ccSStanislav Fomichev return -EINVAL; 1003850a88ccSStanislav Fomichev skb_shinfo(skb)->gso_segs = __skb->gso_segs; 1004cf62089bSWillem de Bruijn skb_shinfo(skb)->gso_size = __skb->gso_size; 10053384c7c7SVadim Fedorenko skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp; 1006850a88ccSStanislav Fomichev 1007b0b9395dSStanislav Fomichev return 0; 1008b0b9395dSStanislav Fomichev } 1009b0b9395dSStanislav Fomichev 1010b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) 1011b0b9395dSStanislav Fomichev { 1012b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 1013b0b9395dSStanislav Fomichev 1014b0b9395dSStanislav Fomichev if (!__skb) 1015b0b9395dSStanislav Fomichev return; 1016b0b9395dSStanislav Fomichev 10176de6c1f8SNikita V. Shirokov __skb->mark = skb->mark; 1018b0b9395dSStanislav Fomichev __skb->priority = skb->priority; 1019b238290bSNeil Spring __skb->ingress_ifindex = skb->skb_iif; 102021594c44SDmitry Yakunin __skb->ifindex = skb->dev->ifindex; 1021ba940948SStanislav Fomichev __skb->tstamp = skb->tstamp; 1022b0b9395dSStanislav Fomichev memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); 1023850a88ccSStanislav Fomichev __skb->wire_len = cb->pkt_len; 1024850a88ccSStanislav Fomichev __skb->gso_segs = skb_shinfo(skb)->gso_segs; 10253384c7c7SVadim Fedorenko __skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp; 1026b0b9395dSStanislav Fomichev } 1027b0b9395dSStanislav Fomichev 1028435b08ecSDaniel Borkmann static struct proto bpf_dummy_proto = { 1029435b08ecSDaniel Borkmann .name = "bpf_dummy", 1030435b08ecSDaniel Borkmann .owner = THIS_MODULE, 1031435b08ecSDaniel Borkmann .obj_size = sizeof(struct sock), 1032435b08ecSDaniel Borkmann }; 1033435b08ecSDaniel Borkmann 10341cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 10351cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 10361cf1cae9SAlexei Starovoitov { 10371cf1cae9SAlexei Starovoitov bool is_l2 = false, is_direct_pkt_access = false; 103821594c44SDmitry Yakunin struct net *net = current->nsproxy->net_ns; 103921594c44SDmitry Yakunin struct net_device *dev = net->loopback_dev; 10401cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 10411cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 1042b0b9395dSStanislav Fomichev struct __sk_buff *ctx = NULL; 10431cf1cae9SAlexei Starovoitov u32 retval, duration; 10446e6fddc7SDaniel Borkmann int hh_len = ETH_HLEN; 10451cf1cae9SAlexei Starovoitov struct sk_buff *skb; 10462cb494a3SSong Liu struct sock *sk; 10471cf1cae9SAlexei Starovoitov void *data; 10481cf1cae9SAlexei Starovoitov int ret; 10491cf1cae9SAlexei Starovoitov 1050b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 10511b4d60ecSSong Liu return -EINVAL; 10521b4d60ecSSong Liu 1053be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, 1054be3d72a2SLorenzo Bianconi size, NET_SKB_PAD + NET_IP_ALIGN, 10551cf1cae9SAlexei Starovoitov SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 10561cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 10571cf1cae9SAlexei Starovoitov return PTR_ERR(data); 10581cf1cae9SAlexei Starovoitov 1059b0b9395dSStanislav Fomichev ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); 1060b0b9395dSStanislav Fomichev if (IS_ERR(ctx)) { 1061b0b9395dSStanislav Fomichev kfree(data); 1062b0b9395dSStanislav Fomichev return PTR_ERR(ctx); 1063b0b9395dSStanislav Fomichev } 1064b0b9395dSStanislav Fomichev 10651cf1cae9SAlexei Starovoitov switch (prog->type) { 10661cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 10671cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 10681cf1cae9SAlexei Starovoitov is_l2 = true; 1069df561f66SGustavo A. R. Silva fallthrough; 10701cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 10711cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 10721cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 10731cf1cae9SAlexei Starovoitov is_direct_pkt_access = true; 10741cf1cae9SAlexei Starovoitov break; 10751cf1cae9SAlexei Starovoitov default: 10761cf1cae9SAlexei Starovoitov break; 10771cf1cae9SAlexei Starovoitov } 10781cf1cae9SAlexei Starovoitov 1079435b08ecSDaniel Borkmann sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1); 10802cb494a3SSong Liu if (!sk) { 10811cf1cae9SAlexei Starovoitov kfree(data); 1082b0b9395dSStanislav Fomichev kfree(ctx); 10831cf1cae9SAlexei Starovoitov return -ENOMEM; 10841cf1cae9SAlexei Starovoitov } 10852cb494a3SSong Liu sock_init_data(NULL, sk); 10862cb494a3SSong Liu 10872cb494a3SSong Liu skb = build_skb(data, 0); 10882cb494a3SSong Liu if (!skb) { 10892cb494a3SSong Liu kfree(data); 1090b0b9395dSStanislav Fomichev kfree(ctx); 1091435b08ecSDaniel Borkmann sk_free(sk); 10922cb494a3SSong Liu return -ENOMEM; 10932cb494a3SSong Liu } 10942cb494a3SSong Liu skb->sk = sk; 10951cf1cae9SAlexei Starovoitov 1096586f8525SDavid Miller skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 10971cf1cae9SAlexei Starovoitov __skb_put(skb, size); 109821594c44SDmitry Yakunin if (ctx && ctx->ifindex > 1) { 109921594c44SDmitry Yakunin dev = dev_get_by_index(net, ctx->ifindex); 110021594c44SDmitry Yakunin if (!dev) { 110121594c44SDmitry Yakunin ret = -ENODEV; 110221594c44SDmitry Yakunin goto out; 110321594c44SDmitry Yakunin } 110421594c44SDmitry Yakunin } 110521594c44SDmitry Yakunin skb->protocol = eth_type_trans(skb, dev); 11061cf1cae9SAlexei Starovoitov skb_reset_network_header(skb); 11071cf1cae9SAlexei Starovoitov 1108fa5cb548SDmitry Yakunin switch (skb->protocol) { 1109fa5cb548SDmitry Yakunin case htons(ETH_P_IP): 1110fa5cb548SDmitry Yakunin sk->sk_family = AF_INET; 1111fa5cb548SDmitry Yakunin if (sizeof(struct iphdr) <= skb_headlen(skb)) { 1112fa5cb548SDmitry Yakunin sk->sk_rcv_saddr = ip_hdr(skb)->saddr; 1113fa5cb548SDmitry Yakunin sk->sk_daddr = ip_hdr(skb)->daddr; 1114fa5cb548SDmitry Yakunin } 1115fa5cb548SDmitry Yakunin break; 1116fa5cb548SDmitry Yakunin #if IS_ENABLED(CONFIG_IPV6) 1117fa5cb548SDmitry Yakunin case htons(ETH_P_IPV6): 1118fa5cb548SDmitry Yakunin sk->sk_family = AF_INET6; 1119fa5cb548SDmitry Yakunin if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) { 1120fa5cb548SDmitry Yakunin sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr; 1121fa5cb548SDmitry Yakunin sk->sk_v6_daddr = ipv6_hdr(skb)->daddr; 1122fa5cb548SDmitry Yakunin } 1123fa5cb548SDmitry Yakunin break; 1124fa5cb548SDmitry Yakunin #endif 1125fa5cb548SDmitry Yakunin default: 1126fa5cb548SDmitry Yakunin break; 1127fa5cb548SDmitry Yakunin } 1128fa5cb548SDmitry Yakunin 11291cf1cae9SAlexei Starovoitov if (is_l2) 11306e6fddc7SDaniel Borkmann __skb_push(skb, hh_len); 11311cf1cae9SAlexei Starovoitov if (is_direct_pkt_access) 11326aaae2b6SDaniel Borkmann bpf_compute_data_pointers(skb); 1133b0b9395dSStanislav Fomichev ret = convert___skb_to_skb(skb, ctx); 1134b0b9395dSStanislav Fomichev if (ret) 1135b0b9395dSStanislav Fomichev goto out; 1136f23c4b39SBjörn Töpel ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false); 1137b0b9395dSStanislav Fomichev if (ret) 1138b0b9395dSStanislav Fomichev goto out; 11396e6fddc7SDaniel Borkmann if (!is_l2) { 11406e6fddc7SDaniel Borkmann if (skb_headroom(skb) < hh_len) { 11416e6fddc7SDaniel Borkmann int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 11426e6fddc7SDaniel Borkmann 11436e6fddc7SDaniel Borkmann if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { 1144b0b9395dSStanislav Fomichev ret = -ENOMEM; 1145b0b9395dSStanislav Fomichev goto out; 11466e6fddc7SDaniel Borkmann } 11476e6fddc7SDaniel Borkmann } 11486e6fddc7SDaniel Borkmann memset(__skb_push(skb, hh_len), 0, hh_len); 11496e6fddc7SDaniel Borkmann } 1150b0b9395dSStanislav Fomichev convert_skb_to___skb(skb, ctx); 11516e6fddc7SDaniel Borkmann 11521cf1cae9SAlexei Starovoitov size = skb->len; 11531cf1cae9SAlexei Starovoitov /* bpf program can never convert linear skb to non-linear */ 11541cf1cae9SAlexei Starovoitov if (WARN_ON_ONCE(skb_is_nonlinear(skb))) 11551cf1cae9SAlexei Starovoitov size = skb_headlen(skb); 11567855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval, 11577855e0dbSLorenzo Bianconi duration); 1158b0b9395dSStanislav Fomichev if (!ret) 1159b0b9395dSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, ctx, 1160b0b9395dSStanislav Fomichev sizeof(struct __sk_buff)); 1161b0b9395dSStanislav Fomichev out: 116221594c44SDmitry Yakunin if (dev && dev != net->loopback_dev) 116321594c44SDmitry Yakunin dev_put(dev); 11641cf1cae9SAlexei Starovoitov kfree_skb(skb); 1165435b08ecSDaniel Borkmann sk_free(sk); 1166b0b9395dSStanislav Fomichev kfree(ctx); 11671cf1cae9SAlexei Starovoitov return ret; 11681cf1cae9SAlexei Starovoitov } 11691cf1cae9SAlexei Starovoitov 117047316f4aSZvi Effron static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp) 117147316f4aSZvi Effron { 1172ec94670fSZvi Effron unsigned int ingress_ifindex, rx_queue_index; 1173ec94670fSZvi Effron struct netdev_rx_queue *rxqueue; 1174ec94670fSZvi Effron struct net_device *device; 1175ec94670fSZvi Effron 117647316f4aSZvi Effron if (!xdp_md) 117747316f4aSZvi Effron return 0; 117847316f4aSZvi Effron 117947316f4aSZvi Effron if (xdp_md->egress_ifindex != 0) 118047316f4aSZvi Effron return -EINVAL; 118147316f4aSZvi Effron 1182ec94670fSZvi Effron ingress_ifindex = xdp_md->ingress_ifindex; 1183ec94670fSZvi Effron rx_queue_index = xdp_md->rx_queue_index; 1184ec94670fSZvi Effron 1185ec94670fSZvi Effron if (!ingress_ifindex && rx_queue_index) 118647316f4aSZvi Effron return -EINVAL; 118747316f4aSZvi Effron 1188ec94670fSZvi Effron if (ingress_ifindex) { 1189ec94670fSZvi Effron device = dev_get_by_index(current->nsproxy->net_ns, 1190ec94670fSZvi Effron ingress_ifindex); 1191ec94670fSZvi Effron if (!device) 1192ec94670fSZvi Effron return -ENODEV; 119347316f4aSZvi Effron 1194ec94670fSZvi Effron if (rx_queue_index >= device->real_num_rx_queues) 1195ec94670fSZvi Effron goto free_dev; 1196ec94670fSZvi Effron 1197ec94670fSZvi Effron rxqueue = __netif_get_rx_queue(device, rx_queue_index); 1198ec94670fSZvi Effron 1199ec94670fSZvi Effron if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq)) 1200ec94670fSZvi Effron goto free_dev; 1201ec94670fSZvi Effron 1202ec94670fSZvi Effron xdp->rxq = &rxqueue->xdp_rxq; 1203ec94670fSZvi Effron /* The device is now tracked in the xdp->rxq for later 1204ec94670fSZvi Effron * dev_put() 1205ec94670fSZvi Effron */ 1206ec94670fSZvi Effron } 1207ec94670fSZvi Effron 1208ec94670fSZvi Effron xdp->data = xdp->data_meta + xdp_md->data; 120947316f4aSZvi Effron return 0; 1210ec94670fSZvi Effron 1211ec94670fSZvi Effron free_dev: 1212ec94670fSZvi Effron dev_put(device); 1213ec94670fSZvi Effron return -EINVAL; 1214ec94670fSZvi Effron } 1215ec94670fSZvi Effron 1216ec94670fSZvi Effron static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md) 1217ec94670fSZvi Effron { 1218ec94670fSZvi Effron if (!xdp_md) 1219ec94670fSZvi Effron return; 1220ec94670fSZvi Effron 1221ec94670fSZvi Effron xdp_md->data = xdp->data - xdp->data_meta; 1222ec94670fSZvi Effron xdp_md->data_end = xdp->data_end - xdp->data_meta; 1223ec94670fSZvi Effron 1224ec94670fSZvi Effron if (xdp_md->ingress_ifindex) 1225ec94670fSZvi Effron dev_put(xdp->rxq->dev); 122647316f4aSZvi Effron } 122747316f4aSZvi Effron 12281cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 12291cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 12301cf1cae9SAlexei Starovoitov { 1231b530e9e1SToke Høiland-Jørgensen bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES); 1232bc56c919SJesper Dangaard Brouer u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1233b530e9e1SToke Høiland-Jørgensen u32 batch_size = kattr->test.batch_size; 1234eecbfd97SToke Høiland-Jørgensen u32 retval = 0, duration, max_data_sz; 12351cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 12361c194998SLorenzo Bianconi u32 headroom = XDP_PACKET_HEADROOM; 12371cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 123865073a67SDaniel Borkmann struct netdev_rx_queue *rxqueue; 12391c194998SLorenzo Bianconi struct skb_shared_info *sinfo; 12401cf1cae9SAlexei Starovoitov struct xdp_buff xdp = {}; 12411c194998SLorenzo Bianconi int i, ret = -EINVAL; 124247316f4aSZvi Effron struct xdp_md *ctx; 12431cf1cae9SAlexei Starovoitov void *data; 12441cf1cae9SAlexei Starovoitov 12455e21bb4eSXuan Zhuo if (prog->expected_attach_type == BPF_XDP_DEVMAP || 12465e21bb4eSXuan Zhuo prog->expected_attach_type == BPF_XDP_CPUMAP) 12475e21bb4eSXuan Zhuo return -EINVAL; 12486d4eb36dSAndrii Nakryiko 1249b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES) 1250b530e9e1SToke Høiland-Jørgensen return -EINVAL; 1251b530e9e1SToke Høiland-Jørgensen 1252b530e9e1SToke Høiland-Jørgensen if (do_live) { 1253b530e9e1SToke Høiland-Jørgensen if (!batch_size) 1254b530e9e1SToke Høiland-Jørgensen batch_size = NAPI_POLL_WEIGHT; 1255b530e9e1SToke Høiland-Jørgensen else if (batch_size > TEST_XDP_MAX_BATCH) 1256b530e9e1SToke Høiland-Jørgensen return -E2BIG; 1257b6f1f780SToke Høiland-Jørgensen 1258b6f1f780SToke Høiland-Jørgensen headroom += sizeof(struct xdp_page_head); 1259b530e9e1SToke Høiland-Jørgensen } else if (batch_size) { 1260b530e9e1SToke Høiland-Jørgensen return -EINVAL; 1261b530e9e1SToke Høiland-Jørgensen } 1262b530e9e1SToke Høiland-Jørgensen 126347316f4aSZvi Effron ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md)); 126447316f4aSZvi Effron if (IS_ERR(ctx)) 126547316f4aSZvi Effron return PTR_ERR(ctx); 126647316f4aSZvi Effron 126747316f4aSZvi Effron if (ctx) { 126847316f4aSZvi Effron /* There can't be user provided data before the meta data */ 126947316f4aSZvi Effron if (ctx->data_meta || ctx->data_end != size || 127047316f4aSZvi Effron ctx->data > ctx->data_end || 1271b530e9e1SToke Høiland-Jørgensen unlikely(xdp_metalen_invalid(ctx->data)) || 1272b530e9e1SToke Høiland-Jørgensen (do_live && (kattr->test.data_out || kattr->test.ctx_out))) 127347316f4aSZvi Effron goto free_ctx; 127447316f4aSZvi Effron /* Meta data is allocated from the headroom */ 127547316f4aSZvi Effron headroom -= ctx->data; 127647316f4aSZvi Effron } 1277947e8b59SStanislav Fomichev 1278bc56c919SJesper Dangaard Brouer max_data_sz = 4096 - headroom - tailroom; 1279b530e9e1SToke Høiland-Jørgensen if (size > max_data_sz) { 1280b530e9e1SToke Høiland-Jørgensen /* disallow live data mode for jumbo frames */ 1281b530e9e1SToke Høiland-Jørgensen if (do_live) 1282b530e9e1SToke Høiland-Jørgensen goto free_ctx; 1283b530e9e1SToke Høiland-Jørgensen size = max_data_sz; 1284b530e9e1SToke Høiland-Jørgensen } 1285bc56c919SJesper Dangaard Brouer 12861c194998SLorenzo Bianconi data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom); 128747316f4aSZvi Effron if (IS_ERR(data)) { 128847316f4aSZvi Effron ret = PTR_ERR(data); 128947316f4aSZvi Effron goto free_ctx; 129047316f4aSZvi Effron } 12911cf1cae9SAlexei Starovoitov 129265073a67SDaniel Borkmann rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 12931c194998SLorenzo Bianconi rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom; 12941c194998SLorenzo Bianconi xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq); 1295be9df4afSLorenzo Bianconi xdp_prepare_buff(&xdp, data, headroom, size, true); 12961c194998SLorenzo Bianconi sinfo = xdp_get_shared_info_from_buff(&xdp); 1297be9df4afSLorenzo Bianconi 129847316f4aSZvi Effron ret = xdp_convert_md_to_buff(ctx, &xdp); 129947316f4aSZvi Effron if (ret) 130047316f4aSZvi Effron goto free_data; 130147316f4aSZvi Effron 13021c194998SLorenzo Bianconi if (unlikely(kattr->test.data_size_in > size)) { 13031c194998SLorenzo Bianconi void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 13041c194998SLorenzo Bianconi 13051c194998SLorenzo Bianconi while (size < kattr->test.data_size_in) { 13061c194998SLorenzo Bianconi struct page *page; 13071c194998SLorenzo Bianconi skb_frag_t *frag; 13089d63b59dSStanislav Fomichev u32 data_len; 13091c194998SLorenzo Bianconi 1310a6763080SLorenzo Bianconi if (sinfo->nr_frags == MAX_SKB_FRAGS) { 1311a6763080SLorenzo Bianconi ret = -ENOMEM; 1312a6763080SLorenzo Bianconi goto out; 1313a6763080SLorenzo Bianconi } 1314a6763080SLorenzo Bianconi 13151c194998SLorenzo Bianconi page = alloc_page(GFP_KERNEL); 13161c194998SLorenzo Bianconi if (!page) { 13171c194998SLorenzo Bianconi ret = -ENOMEM; 13181c194998SLorenzo Bianconi goto out; 13191c194998SLorenzo Bianconi } 13201c194998SLorenzo Bianconi 13211c194998SLorenzo Bianconi frag = &sinfo->frags[sinfo->nr_frags++]; 13221c194998SLorenzo Bianconi __skb_frag_set_page(frag, page); 13231c194998SLorenzo Bianconi 13249d63b59dSStanislav Fomichev data_len = min_t(u32, kattr->test.data_size_in - size, 13251c194998SLorenzo Bianconi PAGE_SIZE); 13261c194998SLorenzo Bianconi skb_frag_size_set(frag, data_len); 13271c194998SLorenzo Bianconi 13281c194998SLorenzo Bianconi if (copy_from_user(page_address(page), data_in + size, 13291c194998SLorenzo Bianconi data_len)) { 13301c194998SLorenzo Bianconi ret = -EFAULT; 13311c194998SLorenzo Bianconi goto out; 13321c194998SLorenzo Bianconi } 13331c194998SLorenzo Bianconi sinfo->xdp_frags_size += data_len; 13341c194998SLorenzo Bianconi size += data_len; 13351c194998SLorenzo Bianconi } 13361c194998SLorenzo Bianconi xdp_buff_set_frags_flag(&xdp); 13371c194998SLorenzo Bianconi } 13381c194998SLorenzo Bianconi 1339de21d8bfSLorenz Bauer if (repeat > 1) 1340f23c4b39SBjörn Töpel bpf_prog_change_xdp(NULL, prog); 13411c194998SLorenzo Bianconi 1342b530e9e1SToke Høiland-Jørgensen if (do_live) 1343b530e9e1SToke Høiland-Jørgensen ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration); 1344b530e9e1SToke Høiland-Jørgensen else 1345f23c4b39SBjörn Töpel ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true); 1346ec94670fSZvi Effron /* We convert the xdp_buff back to an xdp_md before checking the return 1347ec94670fSZvi Effron * code so the reference count of any held netdevice will be decremented 1348ec94670fSZvi Effron * even if the test run failed. 1349ec94670fSZvi Effron */ 1350ec94670fSZvi Effron xdp_convert_buff_to_md(&xdp, ctx); 1351dcb40590SRoman Gushchin if (ret) 1352dcb40590SRoman Gushchin goto out; 135347316f4aSZvi Effron 13541c194998SLorenzo Bianconi size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size; 13557855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size, 13567855e0dbSLorenzo Bianconi retval, duration); 135747316f4aSZvi Effron if (!ret) 135847316f4aSZvi Effron ret = bpf_ctx_finish(kattr, uattr, ctx, 135947316f4aSZvi Effron sizeof(struct xdp_md)); 136047316f4aSZvi Effron 1361dcb40590SRoman Gushchin out: 1362de21d8bfSLorenz Bauer if (repeat > 1) 1363f23c4b39SBjörn Töpel bpf_prog_change_xdp(prog, NULL); 136447316f4aSZvi Effron free_data: 13651c194998SLorenzo Bianconi for (i = 0; i < sinfo->nr_frags; i++) 13661c194998SLorenzo Bianconi __free_page(skb_frag_page(&sinfo->frags[i])); 13671cf1cae9SAlexei Starovoitov kfree(data); 136847316f4aSZvi Effron free_ctx: 136947316f4aSZvi Effron kfree(ctx); 13701cf1cae9SAlexei Starovoitov return ret; 13711cf1cae9SAlexei Starovoitov } 1372b7a1848eSStanislav Fomichev 1373b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) 1374b2ca4e1cSStanislav Fomichev { 1375b2ca4e1cSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 1376b2ca4e1cSStanislav Fomichev if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) 1377b2ca4e1cSStanislav Fomichev return -EINVAL; 1378b2ca4e1cSStanislav Fomichev 1379b2ca4e1cSStanislav Fomichev /* flags is allowed */ 1380b2ca4e1cSStanislav Fomichev 1381b590cb5fSStanislav Fomichev if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags), 1382b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys))) 1383b2ca4e1cSStanislav Fomichev return -EINVAL; 1384b2ca4e1cSStanislav Fomichev 1385b2ca4e1cSStanislav Fomichev return 0; 1386b2ca4e1cSStanislav Fomichev } 1387b2ca4e1cSStanislav Fomichev 1388b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, 1389b7a1848eSStanislav Fomichev const union bpf_attr *kattr, 1390b7a1848eSStanislav Fomichev union bpf_attr __user *uattr) 1391b7a1848eSStanislav Fomichev { 1392607b9cc9SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 1393b7a1848eSStanislav Fomichev u32 size = kattr->test.data_size_in; 13947b8a1304SStanislav Fomichev struct bpf_flow_dissector ctx = {}; 1395b7a1848eSStanislav Fomichev u32 repeat = kattr->test.repeat; 1396b2ca4e1cSStanislav Fomichev struct bpf_flow_keys *user_ctx; 1397b7a1848eSStanislav Fomichev struct bpf_flow_keys flow_keys; 13987b8a1304SStanislav Fomichev const struct ethhdr *eth; 1399b2ca4e1cSStanislav Fomichev unsigned int flags = 0; 1400b7a1848eSStanislav Fomichev u32 retval, duration; 1401b7a1848eSStanislav Fomichev void *data; 1402b7a1848eSStanislav Fomichev int ret; 1403b7a1848eSStanislav Fomichev 1404b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 14051b4d60ecSSong Liu return -EINVAL; 14061b4d60ecSSong Liu 14077b8a1304SStanislav Fomichev if (size < ETH_HLEN) 14087b8a1304SStanislav Fomichev return -EINVAL; 14097b8a1304SStanislav Fomichev 1410be3d72a2SLorenzo Bianconi data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0); 1411b7a1848eSStanislav Fomichev if (IS_ERR(data)) 1412b7a1848eSStanislav Fomichev return PTR_ERR(data); 1413b7a1848eSStanislav Fomichev 14147b8a1304SStanislav Fomichev eth = (struct ethhdr *)data; 1415b7a1848eSStanislav Fomichev 1416b7a1848eSStanislav Fomichev if (!repeat) 1417b7a1848eSStanislav Fomichev repeat = 1; 1418b7a1848eSStanislav Fomichev 1419b2ca4e1cSStanislav Fomichev user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); 1420b2ca4e1cSStanislav Fomichev if (IS_ERR(user_ctx)) { 1421b2ca4e1cSStanislav Fomichev kfree(data); 1422b2ca4e1cSStanislav Fomichev return PTR_ERR(user_ctx); 1423b2ca4e1cSStanislav Fomichev } 1424b2ca4e1cSStanislav Fomichev if (user_ctx) { 1425b2ca4e1cSStanislav Fomichev ret = verify_user_bpf_flow_keys(user_ctx); 1426b2ca4e1cSStanislav Fomichev if (ret) 1427b2ca4e1cSStanislav Fomichev goto out; 1428b2ca4e1cSStanislav Fomichev flags = user_ctx->flags; 1429b2ca4e1cSStanislav Fomichev } 1430b2ca4e1cSStanislav Fomichev 14317b8a1304SStanislav Fomichev ctx.flow_keys = &flow_keys; 14327b8a1304SStanislav Fomichev ctx.data = data; 14337b8a1304SStanislav Fomichev ctx.data_end = (__u8 *)data + size; 14347b8a1304SStanislav Fomichev 1435607b9cc9SLorenz Bauer bpf_test_timer_enter(&t); 1436607b9cc9SLorenz Bauer do { 14377b8a1304SStanislav Fomichev retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 1438b2ca4e1cSStanislav Fomichev size, flags); 1439b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 1440607b9cc9SLorenz Bauer bpf_test_timer_leave(&t); 14417b8a1304SStanislav Fomichev 1442607b9cc9SLorenz Bauer if (ret < 0) 1443a439184dSStanislav Fomichev goto out; 1444b7a1848eSStanislav Fomichev 14457855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL, 14467855e0dbSLorenzo Bianconi sizeof(flow_keys), retval, duration); 1447b2ca4e1cSStanislav Fomichev if (!ret) 1448b2ca4e1cSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, user_ctx, 1449b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys)); 1450b7a1848eSStanislav Fomichev 1451a439184dSStanislav Fomichev out: 1452b2ca4e1cSStanislav Fomichev kfree(user_ctx); 14537b8a1304SStanislav Fomichev kfree(data); 1454b7a1848eSStanislav Fomichev return ret; 1455b7a1848eSStanislav Fomichev } 14567c32e8f8SLorenz Bauer 14577c32e8f8SLorenz Bauer int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr, 14587c32e8f8SLorenz Bauer union bpf_attr __user *uattr) 14597c32e8f8SLorenz Bauer { 14607c32e8f8SLorenz Bauer struct bpf_test_timer t = { NO_PREEMPT }; 14617c32e8f8SLorenz Bauer struct bpf_prog_array *progs = NULL; 14627c32e8f8SLorenz Bauer struct bpf_sk_lookup_kern ctx = {}; 14637c32e8f8SLorenz Bauer u32 repeat = kattr->test.repeat; 14647c32e8f8SLorenz Bauer struct bpf_sk_lookup *user_ctx; 14657c32e8f8SLorenz Bauer u32 retval, duration; 14667c32e8f8SLorenz Bauer int ret = -EINVAL; 14677c32e8f8SLorenz Bauer 1468b530e9e1SToke Høiland-Jørgensen if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size) 14697c32e8f8SLorenz Bauer return -EINVAL; 14707c32e8f8SLorenz Bauer 14717c32e8f8SLorenz Bauer if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || 14727c32e8f8SLorenz Bauer kattr->test.data_size_out) 14737c32e8f8SLorenz Bauer return -EINVAL; 14747c32e8f8SLorenz Bauer 14757c32e8f8SLorenz Bauer if (!repeat) 14767c32e8f8SLorenz Bauer repeat = 1; 14777c32e8f8SLorenz Bauer 14787c32e8f8SLorenz Bauer user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); 14797c32e8f8SLorenz Bauer if (IS_ERR(user_ctx)) 14807c32e8f8SLorenz Bauer return PTR_ERR(user_ctx); 14817c32e8f8SLorenz Bauer 14827c32e8f8SLorenz Bauer if (!user_ctx) 14837c32e8f8SLorenz Bauer return -EINVAL; 14847c32e8f8SLorenz Bauer 14857c32e8f8SLorenz Bauer if (user_ctx->sk) 14867c32e8f8SLorenz Bauer goto out; 14877c32e8f8SLorenz Bauer 14887c32e8f8SLorenz Bauer if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) 14897c32e8f8SLorenz Bauer goto out; 14907c32e8f8SLorenz Bauer 14919a69e2b3SJakub Sitnicki if (user_ctx->local_port > U16_MAX) { 14927c32e8f8SLorenz Bauer ret = -ERANGE; 14937c32e8f8SLorenz Bauer goto out; 14947c32e8f8SLorenz Bauer } 14957c32e8f8SLorenz Bauer 14967c32e8f8SLorenz Bauer ctx.family = (u16)user_ctx->family; 14977c32e8f8SLorenz Bauer ctx.protocol = (u16)user_ctx->protocol; 14987c32e8f8SLorenz Bauer ctx.dport = (u16)user_ctx->local_port; 14999a69e2b3SJakub Sitnicki ctx.sport = user_ctx->remote_port; 15007c32e8f8SLorenz Bauer 15017c32e8f8SLorenz Bauer switch (ctx.family) { 15027c32e8f8SLorenz Bauer case AF_INET: 15037c32e8f8SLorenz Bauer ctx.v4.daddr = (__force __be32)user_ctx->local_ip4; 15047c32e8f8SLorenz Bauer ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4; 15057c32e8f8SLorenz Bauer break; 15067c32e8f8SLorenz Bauer 15077c32e8f8SLorenz Bauer #if IS_ENABLED(CONFIG_IPV6) 15087c32e8f8SLorenz Bauer case AF_INET6: 15097c32e8f8SLorenz Bauer ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; 15107c32e8f8SLorenz Bauer ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; 15117c32e8f8SLorenz Bauer break; 15127c32e8f8SLorenz Bauer #endif 15137c32e8f8SLorenz Bauer 15147c32e8f8SLorenz Bauer default: 15157c32e8f8SLorenz Bauer ret = -EAFNOSUPPORT; 15167c32e8f8SLorenz Bauer goto out; 15177c32e8f8SLorenz Bauer } 15187c32e8f8SLorenz Bauer 15197c32e8f8SLorenz Bauer progs = bpf_prog_array_alloc(1, GFP_KERNEL); 15207c32e8f8SLorenz Bauer if (!progs) { 15217c32e8f8SLorenz Bauer ret = -ENOMEM; 15227c32e8f8SLorenz Bauer goto out; 15237c32e8f8SLorenz Bauer } 15247c32e8f8SLorenz Bauer 15257c32e8f8SLorenz Bauer progs->items[0].prog = prog; 15267c32e8f8SLorenz Bauer 15277c32e8f8SLorenz Bauer bpf_test_timer_enter(&t); 15287c32e8f8SLorenz Bauer do { 15297c32e8f8SLorenz Bauer ctx.selected_sk = NULL; 1530fb7dd8bcSAndrii Nakryiko retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run); 1531b530e9e1SToke Høiland-Jørgensen } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 15327c32e8f8SLorenz Bauer bpf_test_timer_leave(&t); 15337c32e8f8SLorenz Bauer 15347c32e8f8SLorenz Bauer if (ret < 0) 15357c32e8f8SLorenz Bauer goto out; 15367c32e8f8SLorenz Bauer 15377c32e8f8SLorenz Bauer user_ctx->cookie = 0; 15387c32e8f8SLorenz Bauer if (ctx.selected_sk) { 15397c32e8f8SLorenz Bauer if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { 15407c32e8f8SLorenz Bauer ret = -EOPNOTSUPP; 15417c32e8f8SLorenz Bauer goto out; 15427c32e8f8SLorenz Bauer } 15437c32e8f8SLorenz Bauer 15447c32e8f8SLorenz Bauer user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); 15457c32e8f8SLorenz Bauer } 15467c32e8f8SLorenz Bauer 15477855e0dbSLorenzo Bianconi ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration); 15487c32e8f8SLorenz Bauer if (!ret) 15497c32e8f8SLorenz Bauer ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); 15507c32e8f8SLorenz Bauer 15517c32e8f8SLorenz Bauer out: 15527c32e8f8SLorenz Bauer bpf_prog_array_free(progs); 15537c32e8f8SLorenz Bauer kfree(user_ctx); 15547c32e8f8SLorenz Bauer return ret; 15557c32e8f8SLorenz Bauer } 155679a7f8bdSAlexei Starovoitov 155779a7f8bdSAlexei Starovoitov int bpf_prog_test_run_syscall(struct bpf_prog *prog, 155879a7f8bdSAlexei Starovoitov const union bpf_attr *kattr, 155979a7f8bdSAlexei Starovoitov union bpf_attr __user *uattr) 156079a7f8bdSAlexei Starovoitov { 156179a7f8bdSAlexei Starovoitov void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 156279a7f8bdSAlexei Starovoitov __u32 ctx_size_in = kattr->test.ctx_size_in; 156379a7f8bdSAlexei Starovoitov void *ctx = NULL; 156479a7f8bdSAlexei Starovoitov u32 retval; 156579a7f8bdSAlexei Starovoitov int err = 0; 156679a7f8bdSAlexei Starovoitov 156779a7f8bdSAlexei Starovoitov /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ 156879a7f8bdSAlexei Starovoitov if (kattr->test.data_in || kattr->test.data_out || 156979a7f8bdSAlexei Starovoitov kattr->test.ctx_out || kattr->test.duration || 1570b530e9e1SToke Høiland-Jørgensen kattr->test.repeat || kattr->test.flags || 1571b530e9e1SToke Høiland-Jørgensen kattr->test.batch_size) 157279a7f8bdSAlexei Starovoitov return -EINVAL; 157379a7f8bdSAlexei Starovoitov 157479a7f8bdSAlexei Starovoitov if (ctx_size_in < prog->aux->max_ctx_offset || 157579a7f8bdSAlexei Starovoitov ctx_size_in > U16_MAX) 157679a7f8bdSAlexei Starovoitov return -EINVAL; 157779a7f8bdSAlexei Starovoitov 157879a7f8bdSAlexei Starovoitov if (ctx_size_in) { 1579db5b6a46SQing Wang ctx = memdup_user(ctx_in, ctx_size_in); 1580db5b6a46SQing Wang if (IS_ERR(ctx)) 1581db5b6a46SQing Wang return PTR_ERR(ctx); 158279a7f8bdSAlexei Starovoitov } 158387b7b533SYonghong Song 158487b7b533SYonghong Song rcu_read_lock_trace(); 158579a7f8bdSAlexei Starovoitov retval = bpf_prog_run_pin_on_cpu(prog, ctx); 158687b7b533SYonghong Song rcu_read_unlock_trace(); 158779a7f8bdSAlexei Starovoitov 158879a7f8bdSAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { 158979a7f8bdSAlexei Starovoitov err = -EFAULT; 159079a7f8bdSAlexei Starovoitov goto out; 159179a7f8bdSAlexei Starovoitov } 159279a7f8bdSAlexei Starovoitov if (ctx_size_in) 159379a7f8bdSAlexei Starovoitov if (copy_to_user(ctx_in, ctx, ctx_size_in)) 159479a7f8bdSAlexei Starovoitov err = -EFAULT; 159579a7f8bdSAlexei Starovoitov out: 159679a7f8bdSAlexei Starovoitov kfree(ctx); 159779a7f8bdSAlexei Starovoitov return err; 159879a7f8bdSAlexei Starovoitov } 1599b202d844SKumar Kartikeya Dwivedi 1600b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { 1601b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE, 1602*a4703e31SKumar Kartikeya Dwivedi .set = &test_sk_check_kfunc_ids, 1603b202d844SKumar Kartikeya Dwivedi }; 1604b202d844SKumar Kartikeya Dwivedi 160505a945deSKumar Kartikeya Dwivedi BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids) 160605a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_ref_kfunc) 160705a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release) 160805a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_member) 160905a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_memb_release) 161005a945deSKumar Kartikeya Dwivedi 1611b202d844SKumar Kartikeya Dwivedi static int __init bpf_prog_test_run_init(void) 1612b202d844SKumar Kartikeya Dwivedi { 161305a945deSKumar Kartikeya Dwivedi const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = { 161405a945deSKumar Kartikeya Dwivedi { 161505a945deSKumar Kartikeya Dwivedi .btf_id = bpf_prog_test_dtor_kfunc_ids[0], 161605a945deSKumar Kartikeya Dwivedi .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1] 161705a945deSKumar Kartikeya Dwivedi }, 161805a945deSKumar Kartikeya Dwivedi { 161905a945deSKumar Kartikeya Dwivedi .btf_id = bpf_prog_test_dtor_kfunc_ids[2], 162005a945deSKumar Kartikeya Dwivedi .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3], 162105a945deSKumar Kartikeya Dwivedi }, 162205a945deSKumar Kartikeya Dwivedi }; 162305a945deSKumar Kartikeya Dwivedi int ret; 162405a945deSKumar Kartikeya Dwivedi 162505a945deSKumar Kartikeya Dwivedi ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set); 162605a945deSKumar Kartikeya Dwivedi return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc, 162705a945deSKumar Kartikeya Dwivedi ARRAY_SIZE(bpf_prog_test_dtor_kfunc), 162805a945deSKumar Kartikeya Dwivedi THIS_MODULE); 1629b202d844SKumar Kartikeya Dwivedi } 1630b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_prog_test_run_init); 1631