xref: /openbmc/linux/net/bpf/test_run.c (revision 8bad74f9840f87661f20ced3dc80c84ab4fd55a1)
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 
14f42ee093SRoman Gushchin static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx,
15*8bad74f9SRoman Gushchin 		struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
161cf1cae9SAlexei Starovoitov {
171cf1cae9SAlexei Starovoitov 	u32 ret;
181cf1cae9SAlexei Starovoitov 
191cf1cae9SAlexei Starovoitov 	preempt_disable();
201cf1cae9SAlexei Starovoitov 	rcu_read_lock();
21f42ee093SRoman Gushchin 	bpf_cgroup_storage_set(storage);
221cf1cae9SAlexei Starovoitov 	ret = BPF_PROG_RUN(prog, ctx);
231cf1cae9SAlexei Starovoitov 	rcu_read_unlock();
241cf1cae9SAlexei Starovoitov 	preempt_enable();
251cf1cae9SAlexei Starovoitov 
261cf1cae9SAlexei Starovoitov 	return ret;
271cf1cae9SAlexei Starovoitov }
281cf1cae9SAlexei Starovoitov 
291cf1cae9SAlexei Starovoitov static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
301cf1cae9SAlexei Starovoitov {
31*8bad74f9SRoman Gushchin 	struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
32*8bad74f9SRoman Gushchin 	enum bpf_cgroup_storage_type stype;
331cf1cae9SAlexei Starovoitov 	u64 time_start, time_spent = 0;
341cf1cae9SAlexei Starovoitov 	u32 ret = 0, i;
351cf1cae9SAlexei Starovoitov 
36*8bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype) {
37*8bad74f9SRoman Gushchin 		storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
38*8bad74f9SRoman Gushchin 		if (IS_ERR(storage[stype])) {
39*8bad74f9SRoman Gushchin 			storage[stype] = NULL;
40*8bad74f9SRoman Gushchin 			for_each_cgroup_storage_type(stype)
41*8bad74f9SRoman Gushchin 				bpf_cgroup_storage_free(storage[stype]);
42*8bad74f9SRoman Gushchin 			return -ENOMEM;
43*8bad74f9SRoman Gushchin 		}
44*8bad74f9SRoman Gushchin 	}
45f42ee093SRoman Gushchin 
461cf1cae9SAlexei Starovoitov 	if (!repeat)
471cf1cae9SAlexei Starovoitov 		repeat = 1;
481cf1cae9SAlexei Starovoitov 	time_start = ktime_get_ns();
491cf1cae9SAlexei Starovoitov 	for (i = 0; i < repeat; i++) {
50f42ee093SRoman Gushchin 		ret = bpf_test_run_one(prog, ctx, storage);
511cf1cae9SAlexei Starovoitov 		if (need_resched()) {
521cf1cae9SAlexei Starovoitov 			if (signal_pending(current))
531cf1cae9SAlexei Starovoitov 				break;
541cf1cae9SAlexei Starovoitov 			time_spent += ktime_get_ns() - time_start;
551cf1cae9SAlexei Starovoitov 			cond_resched();
561cf1cae9SAlexei Starovoitov 			time_start = ktime_get_ns();
571cf1cae9SAlexei Starovoitov 		}
581cf1cae9SAlexei Starovoitov 	}
591cf1cae9SAlexei Starovoitov 	time_spent += ktime_get_ns() - time_start;
601cf1cae9SAlexei Starovoitov 	do_div(time_spent, repeat);
611cf1cae9SAlexei Starovoitov 	*time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
621cf1cae9SAlexei Starovoitov 
63*8bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype)
64*8bad74f9SRoman Gushchin 		bpf_cgroup_storage_free(storage[stype]);
65f42ee093SRoman Gushchin 
661cf1cae9SAlexei Starovoitov 	return ret;
671cf1cae9SAlexei Starovoitov }
681cf1cae9SAlexei Starovoitov 
6978e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr,
7078e52272SDavid Miller 			   union bpf_attr __user *uattr, const void *data,
711cf1cae9SAlexei Starovoitov 			   u32 size, u32 retval, u32 duration)
721cf1cae9SAlexei Starovoitov {
7378e52272SDavid Miller 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
741cf1cae9SAlexei Starovoitov 	int err = -EFAULT;
751cf1cae9SAlexei Starovoitov 
761cf1cae9SAlexei Starovoitov 	if (data_out && copy_to_user(data_out, data, size))
771cf1cae9SAlexei Starovoitov 		goto out;
781cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
791cf1cae9SAlexei Starovoitov 		goto out;
801cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
811cf1cae9SAlexei Starovoitov 		goto out;
821cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
831cf1cae9SAlexei Starovoitov 		goto out;
841cf1cae9SAlexei Starovoitov 	err = 0;
851cf1cae9SAlexei Starovoitov out:
861cf1cae9SAlexei Starovoitov 	return err;
871cf1cae9SAlexei Starovoitov }
881cf1cae9SAlexei Starovoitov 
891cf1cae9SAlexei Starovoitov static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
901cf1cae9SAlexei Starovoitov 			   u32 headroom, u32 tailroom)
911cf1cae9SAlexei Starovoitov {
921cf1cae9SAlexei Starovoitov 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
931cf1cae9SAlexei Starovoitov 	void *data;
941cf1cae9SAlexei Starovoitov 
951cf1cae9SAlexei Starovoitov 	if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
961cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
971cf1cae9SAlexei Starovoitov 
981cf1cae9SAlexei Starovoitov 	data = kzalloc(size + headroom + tailroom, GFP_USER);
991cf1cae9SAlexei Starovoitov 	if (!data)
1001cf1cae9SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
1011cf1cae9SAlexei Starovoitov 
1021cf1cae9SAlexei Starovoitov 	if (copy_from_user(data + headroom, data_in, size)) {
1031cf1cae9SAlexei Starovoitov 		kfree(data);
1041cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EFAULT);
1051cf1cae9SAlexei Starovoitov 	}
1061cf1cae9SAlexei Starovoitov 	return data;
1071cf1cae9SAlexei Starovoitov }
1081cf1cae9SAlexei Starovoitov 
1091cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1101cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
1111cf1cae9SAlexei Starovoitov {
1121cf1cae9SAlexei Starovoitov 	bool is_l2 = false, is_direct_pkt_access = false;
1131cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
1141cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
1151cf1cae9SAlexei Starovoitov 	u32 retval, duration;
1166e6fddc7SDaniel Borkmann 	int hh_len = ETH_HLEN;
1171cf1cae9SAlexei Starovoitov 	struct sk_buff *skb;
1181cf1cae9SAlexei Starovoitov 	void *data;
1191cf1cae9SAlexei Starovoitov 	int ret;
1201cf1cae9SAlexei Starovoitov 
121586f8525SDavid Miller 	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
1221cf1cae9SAlexei Starovoitov 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1231cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
1241cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
1251cf1cae9SAlexei Starovoitov 
1261cf1cae9SAlexei Starovoitov 	switch (prog->type) {
1271cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_CLS:
1281cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_ACT:
1291cf1cae9SAlexei Starovoitov 		is_l2 = true;
1301cf1cae9SAlexei Starovoitov 		/* fall through */
1311cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_IN:
1321cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_OUT:
1331cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_XMIT:
1341cf1cae9SAlexei Starovoitov 		is_direct_pkt_access = true;
1351cf1cae9SAlexei Starovoitov 		break;
1361cf1cae9SAlexei Starovoitov 	default:
1371cf1cae9SAlexei Starovoitov 		break;
1381cf1cae9SAlexei Starovoitov 	}
1391cf1cae9SAlexei Starovoitov 
1401cf1cae9SAlexei Starovoitov 	skb = build_skb(data, 0);
1411cf1cae9SAlexei Starovoitov 	if (!skb) {
1421cf1cae9SAlexei Starovoitov 		kfree(data);
1431cf1cae9SAlexei Starovoitov 		return -ENOMEM;
1441cf1cae9SAlexei Starovoitov 	}
1451cf1cae9SAlexei Starovoitov 
146586f8525SDavid Miller 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1471cf1cae9SAlexei Starovoitov 	__skb_put(skb, size);
1481cf1cae9SAlexei Starovoitov 	skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
1491cf1cae9SAlexei Starovoitov 	skb_reset_network_header(skb);
1501cf1cae9SAlexei Starovoitov 
1511cf1cae9SAlexei Starovoitov 	if (is_l2)
1526e6fddc7SDaniel Borkmann 		__skb_push(skb, hh_len);
1531cf1cae9SAlexei Starovoitov 	if (is_direct_pkt_access)
1546aaae2b6SDaniel Borkmann 		bpf_compute_data_pointers(skb);
1551cf1cae9SAlexei Starovoitov 	retval = bpf_test_run(prog, skb, repeat, &duration);
1566e6fddc7SDaniel Borkmann 	if (!is_l2) {
1576e6fddc7SDaniel Borkmann 		if (skb_headroom(skb) < hh_len) {
1586e6fddc7SDaniel Borkmann 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
1596e6fddc7SDaniel Borkmann 
1606e6fddc7SDaniel Borkmann 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
1616e6fddc7SDaniel Borkmann 				kfree_skb(skb);
1626e6fddc7SDaniel Borkmann 				return -ENOMEM;
1636e6fddc7SDaniel Borkmann 			}
1646e6fddc7SDaniel Borkmann 		}
1656e6fddc7SDaniel Borkmann 		memset(__skb_push(skb, hh_len), 0, hh_len);
1666e6fddc7SDaniel Borkmann 	}
1676e6fddc7SDaniel Borkmann 
1681cf1cae9SAlexei Starovoitov 	size = skb->len;
1691cf1cae9SAlexei Starovoitov 	/* bpf program can never convert linear skb to non-linear */
1701cf1cae9SAlexei Starovoitov 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
1711cf1cae9SAlexei Starovoitov 		size = skb_headlen(skb);
17278e52272SDavid Miller 	ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
1731cf1cae9SAlexei Starovoitov 	kfree_skb(skb);
1741cf1cae9SAlexei Starovoitov 	return ret;
1751cf1cae9SAlexei Starovoitov }
1761cf1cae9SAlexei Starovoitov 
1771cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1781cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
1791cf1cae9SAlexei Starovoitov {
1801cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
1811cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
18265073a67SDaniel Borkmann 	struct netdev_rx_queue *rxqueue;
1831cf1cae9SAlexei Starovoitov 	struct xdp_buff xdp = {};
1841cf1cae9SAlexei Starovoitov 	u32 retval, duration;
1851cf1cae9SAlexei Starovoitov 	void *data;
1861cf1cae9SAlexei Starovoitov 	int ret;
1871cf1cae9SAlexei Starovoitov 
188586f8525SDavid Miller 	data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
1891cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
1901cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
1911cf1cae9SAlexei Starovoitov 
1921cf1cae9SAlexei Starovoitov 	xdp.data_hard_start = data;
193586f8525SDavid Miller 	xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
194de8f3a83SDaniel Borkmann 	xdp.data_meta = xdp.data;
1951cf1cae9SAlexei Starovoitov 	xdp.data_end = xdp.data + size;
1961cf1cae9SAlexei Starovoitov 
19765073a67SDaniel Borkmann 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
19865073a67SDaniel Borkmann 	xdp.rxq = &rxqueue->xdp_rxq;
19965073a67SDaniel Borkmann 
2001cf1cae9SAlexei Starovoitov 	retval = bpf_test_run(prog, &xdp, repeat, &duration);
201587b80ccSNikita V. Shirokov 	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
202587b80ccSNikita V. Shirokov 	    xdp.data_end != xdp.data + size)
2031cf1cae9SAlexei Starovoitov 		size = xdp.data_end - xdp.data;
20478e52272SDavid Miller 	ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
2051cf1cae9SAlexei Starovoitov 	kfree(data);
2061cf1cae9SAlexei Starovoitov 	return ret;
2071cf1cae9SAlexei Starovoitov }
208