1 /* Copyright (c) 2017 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/etherdevice.h>
11 #include <linux/filter.h>
12 #include <linux/sched/signal.h>
14 static __always_inline u32
bpf_test_run_one(struct bpf_prog
*prog
, void *ctx
,
15 struct bpf_cgroup_storage
*storage
)
21 bpf_cgroup_storage_set(storage
);
22 ret
= BPF_PROG_RUN(prog
, ctx
);
29 static u32
bpf_test_run(struct bpf_prog
*prog
, void *ctx
, u32 repeat
, u32
*time
)
31 struct bpf_cgroup_storage
*storage
= NULL
;
32 u64 time_start
, time_spent
= 0;
35 storage
= bpf_cgroup_storage_alloc(prog
);
37 return PTR_ERR(storage
);
41 time_start
= ktime_get_ns();
42 for (i
= 0; i
< repeat
; i
++) {
43 ret
= bpf_test_run_one(prog
, ctx
, storage
);
45 if (signal_pending(current
))
47 time_spent
+= ktime_get_ns() - time_start
;
49 time_start
= ktime_get_ns();
52 time_spent
+= ktime_get_ns() - time_start
;
53 do_div(time_spent
, repeat
);
54 *time
= time_spent
> U32_MAX
? U32_MAX
: (u32
)time_spent
;
56 bpf_cgroup_storage_free(storage
);
61 static int bpf_test_finish(const union bpf_attr
*kattr
,
62 union bpf_attr __user
*uattr
, const void *data
,
63 u32 size
, u32 retval
, u32 duration
)
65 void __user
*data_out
= u64_to_user_ptr(kattr
->test
.data_out
);
68 if (data_out
&& copy_to_user(data_out
, data
, size
))
70 if (copy_to_user(&uattr
->test
.data_size_out
, &size
, sizeof(size
)))
72 if (copy_to_user(&uattr
->test
.retval
, &retval
, sizeof(retval
)))
74 if (copy_to_user(&uattr
->test
.duration
, &duration
, sizeof(duration
)))
81 static void *bpf_test_init(const union bpf_attr
*kattr
, u32 size
,
82 u32 headroom
, u32 tailroom
)
84 void __user
*data_in
= u64_to_user_ptr(kattr
->test
.data_in
);
87 if (size
< ETH_HLEN
|| size
> PAGE_SIZE
- headroom
- tailroom
)
88 return ERR_PTR(-EINVAL
);
90 data
= kzalloc(size
+ headroom
+ tailroom
, GFP_USER
);
92 return ERR_PTR(-ENOMEM
);
94 if (copy_from_user(data
+ headroom
, data_in
, size
)) {
96 return ERR_PTR(-EFAULT
);
101 int bpf_prog_test_run_skb(struct bpf_prog
*prog
, const union bpf_attr
*kattr
,
102 union bpf_attr __user
*uattr
)
104 bool is_l2
= false, is_direct_pkt_access
= false;
105 u32 size
= kattr
->test
.data_size_in
;
106 u32 repeat
= kattr
->test
.repeat
;
107 u32 retval
, duration
;
108 int hh_len
= ETH_HLEN
;
113 data
= bpf_test_init(kattr
, size
, NET_SKB_PAD
+ NET_IP_ALIGN
,
114 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
)));
116 return PTR_ERR(data
);
118 switch (prog
->type
) {
119 case BPF_PROG_TYPE_SCHED_CLS
:
120 case BPF_PROG_TYPE_SCHED_ACT
:
123 case BPF_PROG_TYPE_LWT_IN
:
124 case BPF_PROG_TYPE_LWT_OUT
:
125 case BPF_PROG_TYPE_LWT_XMIT
:
126 is_direct_pkt_access
= true;
132 skb
= build_skb(data
, 0);
138 skb_reserve(skb
, NET_SKB_PAD
+ NET_IP_ALIGN
);
139 __skb_put(skb
, size
);
140 skb
->protocol
= eth_type_trans(skb
, current
->nsproxy
->net_ns
->loopback_dev
);
141 skb_reset_network_header(skb
);
144 __skb_push(skb
, hh_len
);
145 if (is_direct_pkt_access
)
146 bpf_compute_data_pointers(skb
);
147 retval
= bpf_test_run(prog
, skb
, repeat
, &duration
);
149 if (skb_headroom(skb
) < hh_len
) {
150 int nhead
= HH_DATA_ALIGN(hh_len
- skb_headroom(skb
));
152 if (pskb_expand_head(skb
, nhead
, 0, GFP_USER
)) {
157 memset(__skb_push(skb
, hh_len
), 0, hh_len
);
161 /* bpf program can never convert linear skb to non-linear */
162 if (WARN_ON_ONCE(skb_is_nonlinear(skb
)))
163 size
= skb_headlen(skb
);
164 ret
= bpf_test_finish(kattr
, uattr
, skb
->data
, size
, retval
, duration
);
169 int bpf_prog_test_run_xdp(struct bpf_prog
*prog
, const union bpf_attr
*kattr
,
170 union bpf_attr __user
*uattr
)
172 u32 size
= kattr
->test
.data_size_in
;
173 u32 repeat
= kattr
->test
.repeat
;
174 struct netdev_rx_queue
*rxqueue
;
175 struct xdp_buff xdp
= {};
176 u32 retval
, duration
;
180 data
= bpf_test_init(kattr
, size
, XDP_PACKET_HEADROOM
+ NET_IP_ALIGN
, 0);
182 return PTR_ERR(data
);
184 xdp
.data_hard_start
= data
;
185 xdp
.data
= data
+ XDP_PACKET_HEADROOM
+ NET_IP_ALIGN
;
186 xdp
.data_meta
= xdp
.data
;
187 xdp
.data_end
= xdp
.data
+ size
;
189 rxqueue
= __netif_get_rx_queue(current
->nsproxy
->net_ns
->loopback_dev
, 0);
190 xdp
.rxq
= &rxqueue
->xdp_rxq
;
192 retval
= bpf_test_run(prog
, &xdp
, repeat
, &duration
);
193 if (xdp
.data
!= data
+ XDP_PACKET_HEADROOM
+ NET_IP_ALIGN
||
194 xdp
.data_end
!= xdp
.data
+ size
)
195 size
= xdp
.data_end
- xdp
.data
;
196 ret
= bpf_test_finish(kattr
, uattr
, xdp
.data
, size
, retval
, duration
);