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>
16 static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
17 u32 *retval, u32 *time)
19 struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL };
20 enum bpf_cgroup_storage_type stype;
21 u64 time_start, time_spent = 0;
25 for_each_cgroup_storage_type(stype) {
26 storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
27 if (IS_ERR(storage[stype])) {
28 storage[stype] = NULL;
29 for_each_cgroup_storage_type(stype)
30 bpf_cgroup_storage_free(storage[stype]);
40 time_start = ktime_get_ns();
41 for (i = 0; i < repeat; i++) {
42 bpf_cgroup_storage_set(storage);
43 *retval = BPF_PROG_RUN(prog, ctx);
45 if (signal_pending(current)) {
51 time_spent += ktime_get_ns() - time_start;
59 time_start = ktime_get_ns();
62 time_spent += ktime_get_ns() - time_start;
66 do_div(time_spent, repeat);
67 *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
69 for_each_cgroup_storage_type(stype)
70 bpf_cgroup_storage_free(storage[stype]);
75 static int bpf_test_finish(const union bpf_attr *kattr,
76 union bpf_attr __user *uattr, const void *data,
77 u32 size, u32 retval, u32 duration)
79 void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
83 /* Clamp copy if the user has provided a size hint, but copy the full
84 * buffer if not to retain old behaviour.
86 if (kattr->test.data_size_out &&
87 copy_size > kattr->test.data_size_out) {
88 copy_size = kattr->test.data_size_out;
92 if (data_out && copy_to_user(data_out, data, copy_size))
94 if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
96 if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
98 if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
106 static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
107 u32 headroom, u32 tailroom)
109 void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
112 if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
113 return ERR_PTR(-EINVAL);
115 data = kzalloc(size + headroom + tailroom, GFP_USER);
117 return ERR_PTR(-ENOMEM);
119 if (copy_from_user(data + headroom, data_in, size)) {
121 return ERR_PTR(-EFAULT);
126 static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
128 void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
129 void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
130 u32 size = kattr->test.ctx_size_in;
134 if (!data_in && !data_out)
137 data = kzalloc(max_size, GFP_USER);
139 return ERR_PTR(-ENOMEM);
142 err = bpf_check_uarg_tail_zero(data_in, max_size, size);
148 size = min_t(u32, max_size, size);
149 if (copy_from_user(data, data_in, size)) {
151 return ERR_PTR(-EFAULT);
157 static int bpf_ctx_finish(const union bpf_attr *kattr,
158 union bpf_attr __user *uattr, const void *data,
161 void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
163 u32 copy_size = size;
165 if (!data || !data_out)
168 if (copy_size > kattr->test.ctx_size_out) {
169 copy_size = kattr->test.ctx_size_out;
173 if (copy_to_user(data_out, data, copy_size))
175 if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
184 * range_is_zero - test whether buffer is initialized
185 * @buf: buffer to check
186 * @from: check from this position
187 * @to: check up until (excluding) this position
189 * This function returns true if the there is a non-zero byte
190 * in the buf in the range [from,to).
192 static inline bool range_is_zero(void *buf, size_t from, size_t to)
194 return !memchr_inv((u8 *)buf + from, 0, to - from);
197 static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
199 struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
204 /* make sure the fields we don't use are zeroed */
205 if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, priority)))
208 /* priority is allowed */
210 if (!range_is_zero(__skb, offsetof(struct __sk_buff, priority) +
211 FIELD_SIZEOF(struct __sk_buff, priority),
212 offsetof(struct __sk_buff, cb)))
217 if (!range_is_zero(__skb, offsetof(struct __sk_buff, cb) +
218 FIELD_SIZEOF(struct __sk_buff, cb),
219 sizeof(struct __sk_buff)))
222 skb->priority = __skb->priority;
223 memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
228 static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
230 struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
235 __skb->priority = skb->priority;
236 memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
239 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
240 union bpf_attr __user *uattr)
242 bool is_l2 = false, is_direct_pkt_access = false;
243 u32 size = kattr->test.data_size_in;
244 u32 repeat = kattr->test.repeat;
245 struct __sk_buff *ctx = NULL;
246 u32 retval, duration;
247 int hh_len = ETH_HLEN;
253 data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
254 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
256 return PTR_ERR(data);
258 ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
264 switch (prog->type) {
265 case BPF_PROG_TYPE_SCHED_CLS:
266 case BPF_PROG_TYPE_SCHED_ACT:
269 case BPF_PROG_TYPE_LWT_IN:
270 case BPF_PROG_TYPE_LWT_OUT:
271 case BPF_PROG_TYPE_LWT_XMIT:
272 is_direct_pkt_access = true;
278 sk = kzalloc(sizeof(struct sock), GFP_USER);
284 sock_net_set(sk, current->nsproxy->net_ns);
285 sock_init_data(NULL, sk);
287 skb = build_skb(data, 0);
296 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
297 __skb_put(skb, size);
298 skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
299 skb_reset_network_header(skb);
302 __skb_push(skb, hh_len);
303 if (is_direct_pkt_access)
304 bpf_compute_data_pointers(skb);
305 ret = convert___skb_to_skb(skb, ctx);
308 ret = bpf_test_run(prog, skb, repeat, &retval, &duration);
312 if (skb_headroom(skb) < hh_len) {
313 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
315 if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
320 memset(__skb_push(skb, hh_len), 0, hh_len);
322 convert_skb_to___skb(skb, ctx);
325 /* bpf program can never convert linear skb to non-linear */
326 if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
327 size = skb_headlen(skb);
328 ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
330 ret = bpf_ctx_finish(kattr, uattr, ctx,
331 sizeof(struct __sk_buff));
339 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
340 union bpf_attr __user *uattr)
342 u32 size = kattr->test.data_size_in;
343 u32 repeat = kattr->test.repeat;
344 struct netdev_rx_queue *rxqueue;
345 struct xdp_buff xdp = {};
346 u32 retval, duration;
350 if (kattr->test.ctx_in || kattr->test.ctx_out)
353 data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
355 return PTR_ERR(data);
357 xdp.data_hard_start = data;
358 xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
359 xdp.data_meta = xdp.data;
360 xdp.data_end = xdp.data + size;
362 rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
363 xdp.rxq = &rxqueue->xdp_rxq;
365 ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration);
368 if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
369 xdp.data_end != xdp.data + size)
370 size = xdp.data_end - xdp.data;
371 ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
377 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
378 const union bpf_attr *kattr,
379 union bpf_attr __user *uattr)
381 u32 size = kattr->test.data_size_in;
382 u32 repeat = kattr->test.repeat;
383 struct bpf_flow_keys flow_keys;
384 u64 time_start, time_spent = 0;
385 struct bpf_skb_data_end *cb;
386 u32 retval, duration;
393 if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR)
396 if (kattr->test.ctx_in || kattr->test.ctx_out)
399 data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
400 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
402 return PTR_ERR(data);
404 sk = kzalloc(sizeof(*sk), GFP_USER);
409 sock_net_set(sk, current->nsproxy->net_ns);
410 sock_init_data(NULL, sk);
412 skb = build_skb(data, 0);
420 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
421 __skb_put(skb, size);
422 skb->protocol = eth_type_trans(skb,
423 current->nsproxy->net_ns->loopback_dev);
424 skb_reset_network_header(skb);
426 cb = (struct bpf_skb_data_end *)skb->cb;
427 cb->qdisc_cb.flow_keys = &flow_keys;
434 time_start = ktime_get_ns();
435 for (i = 0; i < repeat; i++) {
436 retval = __skb_flow_bpf_dissect(prog, skb,
437 &flow_keys_dissector,
440 if (signal_pending(current)) {
448 if (need_resched()) {
449 time_spent += ktime_get_ns() - time_start;
457 time_start = ktime_get_ns();
460 time_spent += ktime_get_ns() - time_start;
464 do_div(time_spent, repeat);
465 duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
467 ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys),