11cf1cae9SAlexei Starovoitov /* Copyright (c) 2017 Facebook 21cf1cae9SAlexei Starovoitov * 31cf1cae9SAlexei Starovoitov * This program is free software; you can redistribute it and/or 41cf1cae9SAlexei Starovoitov * modify it under the terms of version 2 of the GNU General Public 51cf1cae9SAlexei Starovoitov * License as published by the Free Software Foundation. 61cf1cae9SAlexei Starovoitov */ 71cf1cae9SAlexei Starovoitov #include <linux/bpf.h> 81cf1cae9SAlexei Starovoitov #include <linux/slab.h> 91cf1cae9SAlexei Starovoitov #include <linux/vmalloc.h> 101cf1cae9SAlexei Starovoitov #include <linux/etherdevice.h> 111cf1cae9SAlexei Starovoitov #include <linux/filter.h> 121cf1cae9SAlexei Starovoitov #include <linux/sched/signal.h> 131cf1cae9SAlexei Starovoitov 141cf1cae9SAlexei Starovoitov static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx) 151cf1cae9SAlexei Starovoitov { 161cf1cae9SAlexei Starovoitov u32 ret; 171cf1cae9SAlexei Starovoitov 181cf1cae9SAlexei Starovoitov preempt_disable(); 191cf1cae9SAlexei Starovoitov rcu_read_lock(); 201cf1cae9SAlexei Starovoitov ret = BPF_PROG_RUN(prog, ctx); 211cf1cae9SAlexei Starovoitov rcu_read_unlock(); 221cf1cae9SAlexei Starovoitov preempt_enable(); 231cf1cae9SAlexei Starovoitov 241cf1cae9SAlexei Starovoitov return ret; 251cf1cae9SAlexei Starovoitov } 261cf1cae9SAlexei Starovoitov 271cf1cae9SAlexei Starovoitov static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time) 281cf1cae9SAlexei Starovoitov { 291cf1cae9SAlexei Starovoitov u64 time_start, time_spent = 0; 301cf1cae9SAlexei Starovoitov u32 ret = 0, i; 311cf1cae9SAlexei Starovoitov 321cf1cae9SAlexei Starovoitov if (!repeat) 331cf1cae9SAlexei Starovoitov repeat = 1; 341cf1cae9SAlexei Starovoitov time_start = ktime_get_ns(); 351cf1cae9SAlexei Starovoitov for (i = 0; i < repeat; i++) { 361cf1cae9SAlexei Starovoitov ret = bpf_test_run_one(prog, ctx); 371cf1cae9SAlexei Starovoitov if (need_resched()) { 381cf1cae9SAlexei Starovoitov if (signal_pending(current)) 391cf1cae9SAlexei Starovoitov break; 401cf1cae9SAlexei Starovoitov time_spent += ktime_get_ns() - time_start; 411cf1cae9SAlexei Starovoitov cond_resched(); 421cf1cae9SAlexei Starovoitov time_start = ktime_get_ns(); 431cf1cae9SAlexei Starovoitov } 441cf1cae9SAlexei Starovoitov } 451cf1cae9SAlexei Starovoitov time_spent += ktime_get_ns() - time_start; 461cf1cae9SAlexei Starovoitov do_div(time_spent, repeat); 471cf1cae9SAlexei Starovoitov *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; 481cf1cae9SAlexei Starovoitov 491cf1cae9SAlexei Starovoitov return ret; 501cf1cae9SAlexei Starovoitov } 511cf1cae9SAlexei Starovoitov 5278e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr, 5378e52272SDavid Miller union bpf_attr __user *uattr, const void *data, 541cf1cae9SAlexei Starovoitov u32 size, u32 retval, u32 duration) 551cf1cae9SAlexei Starovoitov { 5678e52272SDavid Miller void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 571cf1cae9SAlexei Starovoitov int err = -EFAULT; 581cf1cae9SAlexei Starovoitov 591cf1cae9SAlexei Starovoitov if (data_out && copy_to_user(data_out, data, size)) 601cf1cae9SAlexei Starovoitov goto out; 611cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 621cf1cae9SAlexei Starovoitov goto out; 631cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 641cf1cae9SAlexei Starovoitov goto out; 651cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 661cf1cae9SAlexei Starovoitov goto out; 671cf1cae9SAlexei Starovoitov err = 0; 681cf1cae9SAlexei Starovoitov out: 691cf1cae9SAlexei Starovoitov return err; 701cf1cae9SAlexei Starovoitov } 711cf1cae9SAlexei Starovoitov 721cf1cae9SAlexei Starovoitov static void *bpf_test_init(const union bpf_attr *kattr, u32 size, 731cf1cae9SAlexei Starovoitov u32 headroom, u32 tailroom) 741cf1cae9SAlexei Starovoitov { 751cf1cae9SAlexei Starovoitov void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 761cf1cae9SAlexei Starovoitov void *data; 771cf1cae9SAlexei Starovoitov 781cf1cae9SAlexei Starovoitov if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) 791cf1cae9SAlexei Starovoitov return ERR_PTR(-EINVAL); 801cf1cae9SAlexei Starovoitov 811cf1cae9SAlexei Starovoitov data = kzalloc(size + headroom + tailroom, GFP_USER); 821cf1cae9SAlexei Starovoitov if (!data) 831cf1cae9SAlexei Starovoitov return ERR_PTR(-ENOMEM); 841cf1cae9SAlexei Starovoitov 851cf1cae9SAlexei Starovoitov if (copy_from_user(data + headroom, data_in, size)) { 861cf1cae9SAlexei Starovoitov kfree(data); 871cf1cae9SAlexei Starovoitov return ERR_PTR(-EFAULT); 881cf1cae9SAlexei Starovoitov } 891cf1cae9SAlexei Starovoitov return data; 901cf1cae9SAlexei Starovoitov } 911cf1cae9SAlexei Starovoitov 921cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 931cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 941cf1cae9SAlexei Starovoitov { 951cf1cae9SAlexei Starovoitov bool is_l2 = false, is_direct_pkt_access = false; 961cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 971cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 981cf1cae9SAlexei Starovoitov u32 retval, duration; 991cf1cae9SAlexei Starovoitov struct sk_buff *skb; 1001cf1cae9SAlexei Starovoitov void *data; 1011cf1cae9SAlexei Starovoitov int ret; 1021cf1cae9SAlexei Starovoitov 103586f8525SDavid Miller data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, 1041cf1cae9SAlexei Starovoitov SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 1051cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 1061cf1cae9SAlexei Starovoitov return PTR_ERR(data); 1071cf1cae9SAlexei Starovoitov 1081cf1cae9SAlexei Starovoitov switch (prog->type) { 1091cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 1101cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 1111cf1cae9SAlexei Starovoitov is_l2 = true; 1121cf1cae9SAlexei Starovoitov /* fall through */ 1131cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 1141cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 1151cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 1161cf1cae9SAlexei Starovoitov is_direct_pkt_access = true; 1171cf1cae9SAlexei Starovoitov break; 1181cf1cae9SAlexei Starovoitov default: 1191cf1cae9SAlexei Starovoitov break; 1201cf1cae9SAlexei Starovoitov } 1211cf1cae9SAlexei Starovoitov 1221cf1cae9SAlexei Starovoitov skb = build_skb(data, 0); 1231cf1cae9SAlexei Starovoitov if (!skb) { 1241cf1cae9SAlexei Starovoitov kfree(data); 1251cf1cae9SAlexei Starovoitov return -ENOMEM; 1261cf1cae9SAlexei Starovoitov } 1271cf1cae9SAlexei Starovoitov 128586f8525SDavid Miller skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 1291cf1cae9SAlexei Starovoitov __skb_put(skb, size); 1301cf1cae9SAlexei Starovoitov skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev); 1311cf1cae9SAlexei Starovoitov skb_reset_network_header(skb); 1321cf1cae9SAlexei Starovoitov 1331cf1cae9SAlexei Starovoitov if (is_l2) 1341cf1cae9SAlexei Starovoitov __skb_push(skb, ETH_HLEN); 1351cf1cae9SAlexei Starovoitov if (is_direct_pkt_access) 136*6aaae2b6SDaniel Borkmann bpf_compute_data_pointers(skb); 1371cf1cae9SAlexei Starovoitov retval = bpf_test_run(prog, skb, repeat, &duration); 1381cf1cae9SAlexei Starovoitov if (!is_l2) 1391cf1cae9SAlexei Starovoitov __skb_push(skb, ETH_HLEN); 1401cf1cae9SAlexei Starovoitov size = skb->len; 1411cf1cae9SAlexei Starovoitov /* bpf program can never convert linear skb to non-linear */ 1421cf1cae9SAlexei Starovoitov if (WARN_ON_ONCE(skb_is_nonlinear(skb))) 1431cf1cae9SAlexei Starovoitov size = skb_headlen(skb); 14478e52272SDavid Miller ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); 1451cf1cae9SAlexei Starovoitov kfree_skb(skb); 1461cf1cae9SAlexei Starovoitov return ret; 1471cf1cae9SAlexei Starovoitov } 1481cf1cae9SAlexei Starovoitov 1491cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 1501cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 1511cf1cae9SAlexei Starovoitov { 1521cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 1531cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 1541cf1cae9SAlexei Starovoitov struct xdp_buff xdp = {}; 1551cf1cae9SAlexei Starovoitov u32 retval, duration; 1561cf1cae9SAlexei Starovoitov void *data; 1571cf1cae9SAlexei Starovoitov int ret; 1581cf1cae9SAlexei Starovoitov 159586f8525SDavid Miller data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0); 1601cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 1611cf1cae9SAlexei Starovoitov return PTR_ERR(data); 1621cf1cae9SAlexei Starovoitov 1631cf1cae9SAlexei Starovoitov xdp.data_hard_start = data; 164586f8525SDavid Miller xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN; 1651cf1cae9SAlexei Starovoitov xdp.data_end = xdp.data + size; 1661cf1cae9SAlexei Starovoitov 1671cf1cae9SAlexei Starovoitov retval = bpf_test_run(prog, &xdp, repeat, &duration); 168586f8525SDavid Miller if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN) 1691cf1cae9SAlexei Starovoitov size = xdp.data_end - xdp.data; 17078e52272SDavid Miller ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration); 1711cf1cae9SAlexei Starovoitov kfree(data); 1721cf1cae9SAlexei Starovoitov return ret; 1731cf1cae9SAlexei Starovoitov } 174