125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21cf1cae9SAlexei Starovoitov /* Copyright (c) 2017 Facebook 31cf1cae9SAlexei Starovoitov */ 41cf1cae9SAlexei Starovoitov #include <linux/bpf.h> 51cf1cae9SAlexei Starovoitov #include <linux/slab.h> 61cf1cae9SAlexei Starovoitov #include <linux/vmalloc.h> 71cf1cae9SAlexei Starovoitov #include <linux/etherdevice.h> 81cf1cae9SAlexei Starovoitov #include <linux/filter.h> 91cf1cae9SAlexei Starovoitov #include <linux/sched/signal.h> 106ac99e8fSMartin KaFai Lau #include <net/bpf_sk_storage.h> 112cb494a3SSong Liu #include <net/sock.h> 122cb494a3SSong Liu #include <net/tcp.h> 131cf1cae9SAlexei Starovoitov 14e950e843SMatt Mullins #define CREATE_TRACE_POINTS 15e950e843SMatt Mullins #include <trace/events/bpf_test_run.h> 16e950e843SMatt Mullins 17df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, 18df1a2cb7SStanislav Fomichev u32 *retval, u32 *time) 191cf1cae9SAlexei Starovoitov { 2071b91a50SBo YU struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL }; 218bad74f9SRoman Gushchin enum bpf_cgroup_storage_type stype; 221cf1cae9SAlexei Starovoitov u64 time_start, time_spent = 0; 23df1a2cb7SStanislav Fomichev int ret = 0; 24dcb40590SRoman Gushchin u32 i; 251cf1cae9SAlexei Starovoitov 268bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) { 278bad74f9SRoman Gushchin storage[stype] = bpf_cgroup_storage_alloc(prog, stype); 288bad74f9SRoman Gushchin if (IS_ERR(storage[stype])) { 298bad74f9SRoman Gushchin storage[stype] = NULL; 308bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 318bad74f9SRoman Gushchin bpf_cgroup_storage_free(storage[stype]); 328bad74f9SRoman Gushchin return -ENOMEM; 338bad74f9SRoman Gushchin } 348bad74f9SRoman Gushchin } 35f42ee093SRoman Gushchin 361cf1cae9SAlexei Starovoitov if (!repeat) 371cf1cae9SAlexei Starovoitov repeat = 1; 38df1a2cb7SStanislav Fomichev 39df1a2cb7SStanislav Fomichev rcu_read_lock(); 40df1a2cb7SStanislav Fomichev preempt_disable(); 411cf1cae9SAlexei Starovoitov time_start = ktime_get_ns(); 421cf1cae9SAlexei Starovoitov for (i = 0; i < repeat; i++) { 43df1a2cb7SStanislav Fomichev bpf_cgroup_storage_set(storage); 44df1a2cb7SStanislav Fomichev *retval = BPF_PROG_RUN(prog, ctx); 45df1a2cb7SStanislav Fomichev 46df1a2cb7SStanislav Fomichev if (signal_pending(current)) { 47df1a2cb7SStanislav Fomichev ret = -EINTR; 481cf1cae9SAlexei Starovoitov break; 49df1a2cb7SStanislav Fomichev } 50df1a2cb7SStanislav Fomichev 51df1a2cb7SStanislav Fomichev if (need_resched()) { 521cf1cae9SAlexei Starovoitov time_spent += ktime_get_ns() - time_start; 53df1a2cb7SStanislav Fomichev preempt_enable(); 54df1a2cb7SStanislav Fomichev rcu_read_unlock(); 55df1a2cb7SStanislav Fomichev 561cf1cae9SAlexei Starovoitov cond_resched(); 57df1a2cb7SStanislav Fomichev 58df1a2cb7SStanislav Fomichev rcu_read_lock(); 59df1a2cb7SStanislav Fomichev preempt_disable(); 601cf1cae9SAlexei Starovoitov time_start = ktime_get_ns(); 611cf1cae9SAlexei Starovoitov } 621cf1cae9SAlexei Starovoitov } 631cf1cae9SAlexei Starovoitov time_spent += ktime_get_ns() - time_start; 64df1a2cb7SStanislav Fomichev preempt_enable(); 65df1a2cb7SStanislav Fomichev rcu_read_unlock(); 66df1a2cb7SStanislav Fomichev 671cf1cae9SAlexei Starovoitov do_div(time_spent, repeat); 681cf1cae9SAlexei Starovoitov *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; 691cf1cae9SAlexei Starovoitov 708bad74f9SRoman Gushchin for_each_cgroup_storage_type(stype) 718bad74f9SRoman Gushchin bpf_cgroup_storage_free(storage[stype]); 72f42ee093SRoman Gushchin 73df1a2cb7SStanislav Fomichev return ret; 741cf1cae9SAlexei Starovoitov } 751cf1cae9SAlexei Starovoitov 7678e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr, 7778e52272SDavid Miller union bpf_attr __user *uattr, const void *data, 781cf1cae9SAlexei Starovoitov u32 size, u32 retval, u32 duration) 791cf1cae9SAlexei Starovoitov { 8078e52272SDavid Miller void __user *data_out = u64_to_user_ptr(kattr->test.data_out); 811cf1cae9SAlexei Starovoitov int err = -EFAULT; 82b5a36b1eSLorenz Bauer u32 copy_size = size; 831cf1cae9SAlexei Starovoitov 84b5a36b1eSLorenz Bauer /* Clamp copy if the user has provided a size hint, but copy the full 85b5a36b1eSLorenz Bauer * buffer if not to retain old behaviour. 86b5a36b1eSLorenz Bauer */ 87b5a36b1eSLorenz Bauer if (kattr->test.data_size_out && 88b5a36b1eSLorenz Bauer copy_size > kattr->test.data_size_out) { 89b5a36b1eSLorenz Bauer copy_size = kattr->test.data_size_out; 90b5a36b1eSLorenz Bauer err = -ENOSPC; 91b5a36b1eSLorenz Bauer } 92b5a36b1eSLorenz Bauer 93b5a36b1eSLorenz Bauer if (data_out && copy_to_user(data_out, data, copy_size)) 941cf1cae9SAlexei Starovoitov goto out; 951cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size))) 961cf1cae9SAlexei Starovoitov goto out; 971cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval))) 981cf1cae9SAlexei Starovoitov goto out; 991cf1cae9SAlexei Starovoitov if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration))) 1001cf1cae9SAlexei Starovoitov goto out; 101b5a36b1eSLorenz Bauer if (err != -ENOSPC) 1021cf1cae9SAlexei Starovoitov err = 0; 1031cf1cae9SAlexei Starovoitov out: 104e950e843SMatt Mullins trace_bpf_test_finish(&err); 1051cf1cae9SAlexei Starovoitov return err; 1061cf1cae9SAlexei Starovoitov } 1071cf1cae9SAlexei Starovoitov 108*faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of 109*faeb2dceSAlexei Starovoitov * architecture dependent calling conventions. 7+ can be supported in the 110*faeb2dceSAlexei Starovoitov * future. 111*faeb2dceSAlexei Starovoitov */ 112*faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test1(int a) 113*faeb2dceSAlexei Starovoitov { 114*faeb2dceSAlexei Starovoitov return a + 1; 115*faeb2dceSAlexei Starovoitov } 116*faeb2dceSAlexei Starovoitov 117*faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b) 118*faeb2dceSAlexei Starovoitov { 119*faeb2dceSAlexei Starovoitov return a + b; 120*faeb2dceSAlexei Starovoitov } 121*faeb2dceSAlexei Starovoitov 122*faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c) 123*faeb2dceSAlexei Starovoitov { 124*faeb2dceSAlexei Starovoitov return a + b + c; 125*faeb2dceSAlexei Starovoitov } 126*faeb2dceSAlexei Starovoitov 127*faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d) 128*faeb2dceSAlexei Starovoitov { 129*faeb2dceSAlexei Starovoitov return (long)a + b + c + d; 130*faeb2dceSAlexei Starovoitov } 131*faeb2dceSAlexei Starovoitov 132*faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e) 133*faeb2dceSAlexei Starovoitov { 134*faeb2dceSAlexei Starovoitov return a + (long)b + c + d + e; 135*faeb2dceSAlexei Starovoitov } 136*faeb2dceSAlexei Starovoitov 137*faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f) 138*faeb2dceSAlexei Starovoitov { 139*faeb2dceSAlexei Starovoitov return a + (long)b + c + d + (long)e + f; 140*faeb2dceSAlexei Starovoitov } 141*faeb2dceSAlexei Starovoitov 1421cf1cae9SAlexei Starovoitov static void *bpf_test_init(const union bpf_attr *kattr, u32 size, 1431cf1cae9SAlexei Starovoitov u32 headroom, u32 tailroom) 1441cf1cae9SAlexei Starovoitov { 1451cf1cae9SAlexei Starovoitov void __user *data_in = u64_to_user_ptr(kattr->test.data_in); 1461cf1cae9SAlexei Starovoitov void *data; 1471cf1cae9SAlexei Starovoitov 1481cf1cae9SAlexei Starovoitov if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom) 1491cf1cae9SAlexei Starovoitov return ERR_PTR(-EINVAL); 1501cf1cae9SAlexei Starovoitov 1511cf1cae9SAlexei Starovoitov data = kzalloc(size + headroom + tailroom, GFP_USER); 1521cf1cae9SAlexei Starovoitov if (!data) 1531cf1cae9SAlexei Starovoitov return ERR_PTR(-ENOMEM); 1541cf1cae9SAlexei Starovoitov 1551cf1cae9SAlexei Starovoitov if (copy_from_user(data + headroom, data_in, size)) { 1561cf1cae9SAlexei Starovoitov kfree(data); 1571cf1cae9SAlexei Starovoitov return ERR_PTR(-EFAULT); 1581cf1cae9SAlexei Starovoitov } 159*faeb2dceSAlexei Starovoitov if (bpf_fentry_test1(1) != 2 || 160*faeb2dceSAlexei Starovoitov bpf_fentry_test2(2, 3) != 5 || 161*faeb2dceSAlexei Starovoitov bpf_fentry_test3(4, 5, 6) != 15 || 162*faeb2dceSAlexei Starovoitov bpf_fentry_test4((void *)7, 8, 9, 10) != 34 || 163*faeb2dceSAlexei Starovoitov bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 || 164*faeb2dceSAlexei Starovoitov bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111) 165*faeb2dceSAlexei Starovoitov return ERR_PTR(-EFAULT); 1661cf1cae9SAlexei Starovoitov return data; 1671cf1cae9SAlexei Starovoitov } 1681cf1cae9SAlexei Starovoitov 169b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size) 170b0b9395dSStanislav Fomichev { 171b0b9395dSStanislav Fomichev void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in); 172b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 173b0b9395dSStanislav Fomichev u32 size = kattr->test.ctx_size_in; 174b0b9395dSStanislav Fomichev void *data; 175b0b9395dSStanislav Fomichev int err; 176b0b9395dSStanislav Fomichev 177b0b9395dSStanislav Fomichev if (!data_in && !data_out) 178b0b9395dSStanislav Fomichev return NULL; 179b0b9395dSStanislav Fomichev 180b0b9395dSStanislav Fomichev data = kzalloc(max_size, GFP_USER); 181b0b9395dSStanislav Fomichev if (!data) 182b0b9395dSStanislav Fomichev return ERR_PTR(-ENOMEM); 183b0b9395dSStanislav Fomichev 184b0b9395dSStanislav Fomichev if (data_in) { 185b0b9395dSStanislav Fomichev err = bpf_check_uarg_tail_zero(data_in, max_size, size); 186b0b9395dSStanislav Fomichev if (err) { 187b0b9395dSStanislav Fomichev kfree(data); 188b0b9395dSStanislav Fomichev return ERR_PTR(err); 189b0b9395dSStanislav Fomichev } 190b0b9395dSStanislav Fomichev 191b0b9395dSStanislav Fomichev size = min_t(u32, max_size, size); 192b0b9395dSStanislav Fomichev if (copy_from_user(data, data_in, size)) { 193b0b9395dSStanislav Fomichev kfree(data); 194b0b9395dSStanislav Fomichev return ERR_PTR(-EFAULT); 195b0b9395dSStanislav Fomichev } 196b0b9395dSStanislav Fomichev } 197b0b9395dSStanislav Fomichev return data; 198b0b9395dSStanislav Fomichev } 199b0b9395dSStanislav Fomichev 200b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr, 201b0b9395dSStanislav Fomichev union bpf_attr __user *uattr, const void *data, 202b0b9395dSStanislav Fomichev u32 size) 203b0b9395dSStanislav Fomichev { 204b0b9395dSStanislav Fomichev void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out); 205b0b9395dSStanislav Fomichev int err = -EFAULT; 206b0b9395dSStanislav Fomichev u32 copy_size = size; 207b0b9395dSStanislav Fomichev 208b0b9395dSStanislav Fomichev if (!data || !data_out) 209b0b9395dSStanislav Fomichev return 0; 210b0b9395dSStanislav Fomichev 211b0b9395dSStanislav Fomichev if (copy_size > kattr->test.ctx_size_out) { 212b0b9395dSStanislav Fomichev copy_size = kattr->test.ctx_size_out; 213b0b9395dSStanislav Fomichev err = -ENOSPC; 214b0b9395dSStanislav Fomichev } 215b0b9395dSStanislav Fomichev 216b0b9395dSStanislav Fomichev if (copy_to_user(data_out, data, copy_size)) 217b0b9395dSStanislav Fomichev goto out; 218b0b9395dSStanislav Fomichev if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size))) 219b0b9395dSStanislav Fomichev goto out; 220b0b9395dSStanislav Fomichev if (err != -ENOSPC) 221b0b9395dSStanislav Fomichev err = 0; 222b0b9395dSStanislav Fomichev out: 223b0b9395dSStanislav Fomichev return err; 224b0b9395dSStanislav Fomichev } 225b0b9395dSStanislav Fomichev 226b0b9395dSStanislav Fomichev /** 227b0b9395dSStanislav Fomichev * range_is_zero - test whether buffer is initialized 228b0b9395dSStanislav Fomichev * @buf: buffer to check 229b0b9395dSStanislav Fomichev * @from: check from this position 230b0b9395dSStanislav Fomichev * @to: check up until (excluding) this position 231b0b9395dSStanislav Fomichev * 232b0b9395dSStanislav Fomichev * This function returns true if the there is a non-zero byte 233b0b9395dSStanislav Fomichev * in the buf in the range [from,to). 234b0b9395dSStanislav Fomichev */ 235b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to) 236b0b9395dSStanislav Fomichev { 237b0b9395dSStanislav Fomichev return !memchr_inv((u8 *)buf + from, 0, to - from); 238b0b9395dSStanislav Fomichev } 239b0b9395dSStanislav Fomichev 240b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb) 241b0b9395dSStanislav Fomichev { 242b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 243b0b9395dSStanislav Fomichev 244b0b9395dSStanislav Fomichev if (!__skb) 245b0b9395dSStanislav Fomichev return 0; 246b0b9395dSStanislav Fomichev 247b0b9395dSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 248b0b9395dSStanislav Fomichev if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, priority))) 249b0b9395dSStanislav Fomichev return -EINVAL; 250b0b9395dSStanislav Fomichev 251b0b9395dSStanislav Fomichev /* priority is allowed */ 252b0b9395dSStanislav Fomichev 253b0b9395dSStanislav Fomichev if (!range_is_zero(__skb, offsetof(struct __sk_buff, priority) + 254b0b9395dSStanislav Fomichev FIELD_SIZEOF(struct __sk_buff, priority), 255b0b9395dSStanislav Fomichev offsetof(struct __sk_buff, cb))) 256b0b9395dSStanislav Fomichev return -EINVAL; 257b0b9395dSStanislav Fomichev 258b0b9395dSStanislav Fomichev /* cb is allowed */ 259b0b9395dSStanislav Fomichev 260b0b9395dSStanislav Fomichev if (!range_is_zero(__skb, offsetof(struct __sk_buff, cb) + 261b0b9395dSStanislav Fomichev FIELD_SIZEOF(struct __sk_buff, cb), 262ba940948SStanislav Fomichev offsetof(struct __sk_buff, tstamp))) 263ba940948SStanislav Fomichev return -EINVAL; 264ba940948SStanislav Fomichev 265ba940948SStanislav Fomichev /* tstamp is allowed */ 266ba940948SStanislav Fomichev 267ba940948SStanislav Fomichev if (!range_is_zero(__skb, offsetof(struct __sk_buff, tstamp) + 268ba940948SStanislav Fomichev FIELD_SIZEOF(struct __sk_buff, tstamp), 269b0b9395dSStanislav Fomichev sizeof(struct __sk_buff))) 270b0b9395dSStanislav Fomichev return -EINVAL; 271b0b9395dSStanislav Fomichev 272b0b9395dSStanislav Fomichev skb->priority = __skb->priority; 273ba940948SStanislav Fomichev skb->tstamp = __skb->tstamp; 274b0b9395dSStanislav Fomichev memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN); 275b0b9395dSStanislav Fomichev 276b0b9395dSStanislav Fomichev return 0; 277b0b9395dSStanislav Fomichev } 278b0b9395dSStanislav Fomichev 279b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb) 280b0b9395dSStanislav Fomichev { 281b0b9395dSStanislav Fomichev struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb; 282b0b9395dSStanislav Fomichev 283b0b9395dSStanislav Fomichev if (!__skb) 284b0b9395dSStanislav Fomichev return; 285b0b9395dSStanislav Fomichev 286b0b9395dSStanislav Fomichev __skb->priority = skb->priority; 287ba940948SStanislav Fomichev __skb->tstamp = skb->tstamp; 288b0b9395dSStanislav Fomichev memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN); 289b0b9395dSStanislav Fomichev } 290b0b9395dSStanislav Fomichev 2911cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, 2921cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 2931cf1cae9SAlexei Starovoitov { 2941cf1cae9SAlexei Starovoitov bool is_l2 = false, is_direct_pkt_access = false; 2951cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 2961cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 297b0b9395dSStanislav Fomichev struct __sk_buff *ctx = NULL; 2981cf1cae9SAlexei Starovoitov u32 retval, duration; 2996e6fddc7SDaniel Borkmann int hh_len = ETH_HLEN; 3001cf1cae9SAlexei Starovoitov struct sk_buff *skb; 3012cb494a3SSong Liu struct sock *sk; 3021cf1cae9SAlexei Starovoitov void *data; 3031cf1cae9SAlexei Starovoitov int ret; 3041cf1cae9SAlexei Starovoitov 305586f8525SDavid Miller data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, 3061cf1cae9SAlexei Starovoitov SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 3071cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 3081cf1cae9SAlexei Starovoitov return PTR_ERR(data); 3091cf1cae9SAlexei Starovoitov 310b0b9395dSStanislav Fomichev ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff)); 311b0b9395dSStanislav Fomichev if (IS_ERR(ctx)) { 312b0b9395dSStanislav Fomichev kfree(data); 313b0b9395dSStanislav Fomichev return PTR_ERR(ctx); 314b0b9395dSStanislav Fomichev } 315b0b9395dSStanislav Fomichev 3161cf1cae9SAlexei Starovoitov switch (prog->type) { 3171cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_CLS: 3181cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_SCHED_ACT: 3191cf1cae9SAlexei Starovoitov is_l2 = true; 3201cf1cae9SAlexei Starovoitov /* fall through */ 3211cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_IN: 3221cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_OUT: 3231cf1cae9SAlexei Starovoitov case BPF_PROG_TYPE_LWT_XMIT: 3241cf1cae9SAlexei Starovoitov is_direct_pkt_access = true; 3251cf1cae9SAlexei Starovoitov break; 3261cf1cae9SAlexei Starovoitov default: 3271cf1cae9SAlexei Starovoitov break; 3281cf1cae9SAlexei Starovoitov } 3291cf1cae9SAlexei Starovoitov 3302cb494a3SSong Liu sk = kzalloc(sizeof(struct sock), GFP_USER); 3312cb494a3SSong Liu if (!sk) { 3321cf1cae9SAlexei Starovoitov kfree(data); 333b0b9395dSStanislav Fomichev kfree(ctx); 3341cf1cae9SAlexei Starovoitov return -ENOMEM; 3351cf1cae9SAlexei Starovoitov } 3362cb494a3SSong Liu sock_net_set(sk, current->nsproxy->net_ns); 3372cb494a3SSong Liu sock_init_data(NULL, sk); 3382cb494a3SSong Liu 3392cb494a3SSong Liu skb = build_skb(data, 0); 3402cb494a3SSong Liu if (!skb) { 3412cb494a3SSong Liu kfree(data); 342b0b9395dSStanislav Fomichev kfree(ctx); 3432cb494a3SSong Liu kfree(sk); 3442cb494a3SSong Liu return -ENOMEM; 3452cb494a3SSong Liu } 3462cb494a3SSong Liu skb->sk = sk; 3471cf1cae9SAlexei Starovoitov 348586f8525SDavid Miller skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 3491cf1cae9SAlexei Starovoitov __skb_put(skb, size); 3501cf1cae9SAlexei Starovoitov skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev); 3511cf1cae9SAlexei Starovoitov skb_reset_network_header(skb); 3521cf1cae9SAlexei Starovoitov 3531cf1cae9SAlexei Starovoitov if (is_l2) 3546e6fddc7SDaniel Borkmann __skb_push(skb, hh_len); 3551cf1cae9SAlexei Starovoitov if (is_direct_pkt_access) 3566aaae2b6SDaniel Borkmann bpf_compute_data_pointers(skb); 357b0b9395dSStanislav Fomichev ret = convert___skb_to_skb(skb, ctx); 358b0b9395dSStanislav Fomichev if (ret) 359b0b9395dSStanislav Fomichev goto out; 360dcb40590SRoman Gushchin ret = bpf_test_run(prog, skb, repeat, &retval, &duration); 361b0b9395dSStanislav Fomichev if (ret) 362b0b9395dSStanislav Fomichev goto out; 3636e6fddc7SDaniel Borkmann if (!is_l2) { 3646e6fddc7SDaniel Borkmann if (skb_headroom(skb) < hh_len) { 3656e6fddc7SDaniel Borkmann int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb)); 3666e6fddc7SDaniel Borkmann 3676e6fddc7SDaniel Borkmann if (pskb_expand_head(skb, nhead, 0, GFP_USER)) { 368b0b9395dSStanislav Fomichev ret = -ENOMEM; 369b0b9395dSStanislav Fomichev goto out; 3706e6fddc7SDaniel Borkmann } 3716e6fddc7SDaniel Borkmann } 3726e6fddc7SDaniel Borkmann memset(__skb_push(skb, hh_len), 0, hh_len); 3736e6fddc7SDaniel Borkmann } 374b0b9395dSStanislav Fomichev convert_skb_to___skb(skb, ctx); 3756e6fddc7SDaniel Borkmann 3761cf1cae9SAlexei Starovoitov size = skb->len; 3771cf1cae9SAlexei Starovoitov /* bpf program can never convert linear skb to non-linear */ 3781cf1cae9SAlexei Starovoitov if (WARN_ON_ONCE(skb_is_nonlinear(skb))) 3791cf1cae9SAlexei Starovoitov size = skb_headlen(skb); 38078e52272SDavid Miller ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration); 381b0b9395dSStanislav Fomichev if (!ret) 382b0b9395dSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, ctx, 383b0b9395dSStanislav Fomichev sizeof(struct __sk_buff)); 384b0b9395dSStanislav Fomichev out: 3851cf1cae9SAlexei Starovoitov kfree_skb(skb); 3866ac99e8fSMartin KaFai Lau bpf_sk_storage_free(sk); 3872cb494a3SSong Liu kfree(sk); 388b0b9395dSStanislav Fomichev kfree(ctx); 3891cf1cae9SAlexei Starovoitov return ret; 3901cf1cae9SAlexei Starovoitov } 3911cf1cae9SAlexei Starovoitov 3921cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, 3931cf1cae9SAlexei Starovoitov union bpf_attr __user *uattr) 3941cf1cae9SAlexei Starovoitov { 3951cf1cae9SAlexei Starovoitov u32 size = kattr->test.data_size_in; 3961cf1cae9SAlexei Starovoitov u32 repeat = kattr->test.repeat; 39765073a67SDaniel Borkmann struct netdev_rx_queue *rxqueue; 3981cf1cae9SAlexei Starovoitov struct xdp_buff xdp = {}; 3991cf1cae9SAlexei Starovoitov u32 retval, duration; 4001cf1cae9SAlexei Starovoitov void *data; 4011cf1cae9SAlexei Starovoitov int ret; 4021cf1cae9SAlexei Starovoitov 403947e8b59SStanislav Fomichev if (kattr->test.ctx_in || kattr->test.ctx_out) 404947e8b59SStanislav Fomichev return -EINVAL; 405947e8b59SStanislav Fomichev 406586f8525SDavid Miller data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0); 4071cf1cae9SAlexei Starovoitov if (IS_ERR(data)) 4081cf1cae9SAlexei Starovoitov return PTR_ERR(data); 4091cf1cae9SAlexei Starovoitov 4101cf1cae9SAlexei Starovoitov xdp.data_hard_start = data; 411586f8525SDavid Miller xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN; 412de8f3a83SDaniel Borkmann xdp.data_meta = xdp.data; 4131cf1cae9SAlexei Starovoitov xdp.data_end = xdp.data + size; 4141cf1cae9SAlexei Starovoitov 41565073a67SDaniel Borkmann rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0); 41665073a67SDaniel Borkmann xdp.rxq = &rxqueue->xdp_rxq; 41765073a67SDaniel Borkmann 418dcb40590SRoman Gushchin ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration); 419dcb40590SRoman Gushchin if (ret) 420dcb40590SRoman Gushchin goto out; 421587b80ccSNikita V. Shirokov if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN || 422587b80ccSNikita V. Shirokov xdp.data_end != xdp.data + size) 4231cf1cae9SAlexei Starovoitov size = xdp.data_end - xdp.data; 42478e52272SDavid Miller ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration); 425dcb40590SRoman Gushchin out: 4261cf1cae9SAlexei Starovoitov kfree(data); 4271cf1cae9SAlexei Starovoitov return ret; 4281cf1cae9SAlexei Starovoitov } 429b7a1848eSStanislav Fomichev 430b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx) 431b2ca4e1cSStanislav Fomichev { 432b2ca4e1cSStanislav Fomichev /* make sure the fields we don't use are zeroed */ 433b2ca4e1cSStanislav Fomichev if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags))) 434b2ca4e1cSStanislav Fomichev return -EINVAL; 435b2ca4e1cSStanislav Fomichev 436b2ca4e1cSStanislav Fomichev /* flags is allowed */ 437b2ca4e1cSStanislav Fomichev 438b2ca4e1cSStanislav Fomichev if (!range_is_zero(ctx, offsetof(struct bpf_flow_keys, flags) + 439b2ca4e1cSStanislav Fomichev FIELD_SIZEOF(struct bpf_flow_keys, flags), 440b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys))) 441b2ca4e1cSStanislav Fomichev return -EINVAL; 442b2ca4e1cSStanislav Fomichev 443b2ca4e1cSStanislav Fomichev return 0; 444b2ca4e1cSStanislav Fomichev } 445b2ca4e1cSStanislav Fomichev 446b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, 447b7a1848eSStanislav Fomichev const union bpf_attr *kattr, 448b7a1848eSStanislav Fomichev union bpf_attr __user *uattr) 449b7a1848eSStanislav Fomichev { 450b7a1848eSStanislav Fomichev u32 size = kattr->test.data_size_in; 4517b8a1304SStanislav Fomichev struct bpf_flow_dissector ctx = {}; 452b7a1848eSStanislav Fomichev u32 repeat = kattr->test.repeat; 453b2ca4e1cSStanislav Fomichev struct bpf_flow_keys *user_ctx; 454b7a1848eSStanislav Fomichev struct bpf_flow_keys flow_keys; 455b7a1848eSStanislav Fomichev u64 time_start, time_spent = 0; 4567b8a1304SStanislav Fomichev const struct ethhdr *eth; 457b2ca4e1cSStanislav Fomichev unsigned int flags = 0; 458b7a1848eSStanislav Fomichev u32 retval, duration; 459b7a1848eSStanislav Fomichev void *data; 460b7a1848eSStanislav Fomichev int ret; 461b7a1848eSStanislav Fomichev u32 i; 462b7a1848eSStanislav Fomichev 463b7a1848eSStanislav Fomichev if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) 464b7a1848eSStanislav Fomichev return -EINVAL; 465b7a1848eSStanislav Fomichev 4667b8a1304SStanislav Fomichev if (size < ETH_HLEN) 4677b8a1304SStanislav Fomichev return -EINVAL; 4687b8a1304SStanislav Fomichev 4697b8a1304SStanislav Fomichev data = bpf_test_init(kattr, size, 0, 0); 470b7a1848eSStanislav Fomichev if (IS_ERR(data)) 471b7a1848eSStanislav Fomichev return PTR_ERR(data); 472b7a1848eSStanislav Fomichev 4737b8a1304SStanislav Fomichev eth = (struct ethhdr *)data; 474b7a1848eSStanislav Fomichev 475b7a1848eSStanislav Fomichev if (!repeat) 476b7a1848eSStanislav Fomichev repeat = 1; 477b7a1848eSStanislav Fomichev 478b2ca4e1cSStanislav Fomichev user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys)); 479b2ca4e1cSStanislav Fomichev if (IS_ERR(user_ctx)) { 480b2ca4e1cSStanislav Fomichev kfree(data); 481b2ca4e1cSStanislav Fomichev return PTR_ERR(user_ctx); 482b2ca4e1cSStanislav Fomichev } 483b2ca4e1cSStanislav Fomichev if (user_ctx) { 484b2ca4e1cSStanislav Fomichev ret = verify_user_bpf_flow_keys(user_ctx); 485b2ca4e1cSStanislav Fomichev if (ret) 486b2ca4e1cSStanislav Fomichev goto out; 487b2ca4e1cSStanislav Fomichev flags = user_ctx->flags; 488b2ca4e1cSStanislav Fomichev } 489b2ca4e1cSStanislav Fomichev 4907b8a1304SStanislav Fomichev ctx.flow_keys = &flow_keys; 4917b8a1304SStanislav Fomichev ctx.data = data; 4927b8a1304SStanislav Fomichev ctx.data_end = (__u8 *)data + size; 4937b8a1304SStanislav Fomichev 494a439184dSStanislav Fomichev rcu_read_lock(); 495a439184dSStanislav Fomichev preempt_disable(); 496b7a1848eSStanislav Fomichev time_start = ktime_get_ns(); 497b7a1848eSStanislav Fomichev for (i = 0; i < repeat; i++) { 4987b8a1304SStanislav Fomichev retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 499b2ca4e1cSStanislav Fomichev size, flags); 5007b8a1304SStanislav Fomichev 501a439184dSStanislav Fomichev if (signal_pending(current)) { 502b7a1848eSStanislav Fomichev preempt_enable(); 503a439184dSStanislav Fomichev rcu_read_unlock(); 504a439184dSStanislav Fomichev 505a439184dSStanislav Fomichev ret = -EINTR; 506a439184dSStanislav Fomichev goto out; 507a439184dSStanislav Fomichev } 508b7a1848eSStanislav Fomichev 509b7a1848eSStanislav Fomichev if (need_resched()) { 510b7a1848eSStanislav Fomichev time_spent += ktime_get_ns() - time_start; 511a439184dSStanislav Fomichev preempt_enable(); 512a439184dSStanislav Fomichev rcu_read_unlock(); 513a439184dSStanislav Fomichev 514b7a1848eSStanislav Fomichev cond_resched(); 515a439184dSStanislav Fomichev 516a439184dSStanislav Fomichev rcu_read_lock(); 517a439184dSStanislav Fomichev preempt_disable(); 518b7a1848eSStanislav Fomichev time_start = ktime_get_ns(); 519b7a1848eSStanislav Fomichev } 520b7a1848eSStanislav Fomichev } 521b7a1848eSStanislav Fomichev time_spent += ktime_get_ns() - time_start; 522a439184dSStanislav Fomichev preempt_enable(); 523a439184dSStanislav Fomichev rcu_read_unlock(); 524a439184dSStanislav Fomichev 525b7a1848eSStanislav Fomichev do_div(time_spent, repeat); 526b7a1848eSStanislav Fomichev duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; 527b7a1848eSStanislav Fomichev 528b7a1848eSStanislav Fomichev ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), 529b7a1848eSStanislav Fomichev retval, duration); 530b2ca4e1cSStanislav Fomichev if (!ret) 531b2ca4e1cSStanislav Fomichev ret = bpf_ctx_finish(kattr, uattr, user_ctx, 532b2ca4e1cSStanislav Fomichev sizeof(struct bpf_flow_keys)); 533b7a1848eSStanislav Fomichev 534a439184dSStanislav Fomichev out: 535b2ca4e1cSStanislav Fomichev kfree(user_ctx); 5367b8a1304SStanislav Fomichev kfree(data); 537b7a1848eSStanislav Fomichev return ret; 538b7a1848eSStanislav Fomichev } 539