xref: /openbmc/linux/net/bpf/test_run.c (revision dcb40590e69e306030e944a39d0e4bf54247fb68)
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>
132cb494a3SSong Liu #include <net/sock.h>
142cb494a3SSong Liu #include <net/tcp.h>
151cf1cae9SAlexei Starovoitov 
16f42ee093SRoman Gushchin static __always_inline u32 bpf_test_run_one(struct bpf_prog *prog, void *ctx,
178bad74f9SRoman Gushchin 		struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE])
181cf1cae9SAlexei Starovoitov {
191cf1cae9SAlexei Starovoitov 	u32 ret;
201cf1cae9SAlexei Starovoitov 
211cf1cae9SAlexei Starovoitov 	preempt_disable();
221cf1cae9SAlexei Starovoitov 	rcu_read_lock();
23f42ee093SRoman Gushchin 	bpf_cgroup_storage_set(storage);
241cf1cae9SAlexei Starovoitov 	ret = BPF_PROG_RUN(prog, ctx);
251cf1cae9SAlexei Starovoitov 	rcu_read_unlock();
261cf1cae9SAlexei Starovoitov 	preempt_enable();
271cf1cae9SAlexei Starovoitov 
281cf1cae9SAlexei Starovoitov 	return ret;
291cf1cae9SAlexei Starovoitov }
301cf1cae9SAlexei Starovoitov 
31*dcb40590SRoman Gushchin static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *ret,
32*dcb40590SRoman Gushchin 			u32 *time)
331cf1cae9SAlexei Starovoitov {
348bad74f9SRoman Gushchin 	struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { 0 };
358bad74f9SRoman Gushchin 	enum bpf_cgroup_storage_type stype;
361cf1cae9SAlexei Starovoitov 	u64 time_start, time_spent = 0;
37*dcb40590SRoman Gushchin 	u32 i;
381cf1cae9SAlexei Starovoitov 
398bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype) {
408bad74f9SRoman Gushchin 		storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
418bad74f9SRoman Gushchin 		if (IS_ERR(storage[stype])) {
428bad74f9SRoman Gushchin 			storage[stype] = NULL;
438bad74f9SRoman Gushchin 			for_each_cgroup_storage_type(stype)
448bad74f9SRoman Gushchin 				bpf_cgroup_storage_free(storage[stype]);
458bad74f9SRoman Gushchin 			return -ENOMEM;
468bad74f9SRoman Gushchin 		}
478bad74f9SRoman Gushchin 	}
48f42ee093SRoman Gushchin 
491cf1cae9SAlexei Starovoitov 	if (!repeat)
501cf1cae9SAlexei Starovoitov 		repeat = 1;
511cf1cae9SAlexei Starovoitov 	time_start = ktime_get_ns();
521cf1cae9SAlexei Starovoitov 	for (i = 0; i < repeat; i++) {
53*dcb40590SRoman Gushchin 		*ret = bpf_test_run_one(prog, ctx, storage);
541cf1cae9SAlexei Starovoitov 		if (need_resched()) {
551cf1cae9SAlexei Starovoitov 			if (signal_pending(current))
561cf1cae9SAlexei Starovoitov 				break;
571cf1cae9SAlexei Starovoitov 			time_spent += ktime_get_ns() - time_start;
581cf1cae9SAlexei Starovoitov 			cond_resched();
591cf1cae9SAlexei Starovoitov 			time_start = ktime_get_ns();
601cf1cae9SAlexei Starovoitov 		}
611cf1cae9SAlexei Starovoitov 	}
621cf1cae9SAlexei Starovoitov 	time_spent += ktime_get_ns() - time_start;
631cf1cae9SAlexei Starovoitov 	do_div(time_spent, repeat);
641cf1cae9SAlexei Starovoitov 	*time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent;
651cf1cae9SAlexei Starovoitov 
668bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype)
678bad74f9SRoman Gushchin 		bpf_cgroup_storage_free(storage[stype]);
68f42ee093SRoman Gushchin 
69*dcb40590SRoman Gushchin 	return 0;
701cf1cae9SAlexei Starovoitov }
711cf1cae9SAlexei Starovoitov 
7278e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr,
7378e52272SDavid Miller 			   union bpf_attr __user *uattr, const void *data,
741cf1cae9SAlexei Starovoitov 			   u32 size, u32 retval, u32 duration)
751cf1cae9SAlexei Starovoitov {
7678e52272SDavid Miller 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
771cf1cae9SAlexei Starovoitov 	int err = -EFAULT;
781cf1cae9SAlexei Starovoitov 
791cf1cae9SAlexei Starovoitov 	if (data_out && copy_to_user(data_out, data, size))
801cf1cae9SAlexei Starovoitov 		goto out;
811cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
821cf1cae9SAlexei Starovoitov 		goto out;
831cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
841cf1cae9SAlexei Starovoitov 		goto out;
851cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
861cf1cae9SAlexei Starovoitov 		goto out;
871cf1cae9SAlexei Starovoitov 	err = 0;
881cf1cae9SAlexei Starovoitov out:
891cf1cae9SAlexei Starovoitov 	return err;
901cf1cae9SAlexei Starovoitov }
911cf1cae9SAlexei Starovoitov 
921cf1cae9SAlexei Starovoitov static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
931cf1cae9SAlexei Starovoitov 			   u32 headroom, u32 tailroom)
941cf1cae9SAlexei Starovoitov {
951cf1cae9SAlexei Starovoitov 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
961cf1cae9SAlexei Starovoitov 	void *data;
971cf1cae9SAlexei Starovoitov 
981cf1cae9SAlexei Starovoitov 	if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
991cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
1001cf1cae9SAlexei Starovoitov 
1011cf1cae9SAlexei Starovoitov 	data = kzalloc(size + headroom + tailroom, GFP_USER);
1021cf1cae9SAlexei Starovoitov 	if (!data)
1031cf1cae9SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
1041cf1cae9SAlexei Starovoitov 
1051cf1cae9SAlexei Starovoitov 	if (copy_from_user(data + headroom, data_in, size)) {
1061cf1cae9SAlexei Starovoitov 		kfree(data);
1071cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EFAULT);
1081cf1cae9SAlexei Starovoitov 	}
1091cf1cae9SAlexei Starovoitov 	return data;
1101cf1cae9SAlexei Starovoitov }
1111cf1cae9SAlexei Starovoitov 
1121cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
1131cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
1141cf1cae9SAlexei Starovoitov {
1151cf1cae9SAlexei Starovoitov 	bool is_l2 = false, is_direct_pkt_access = false;
1161cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
1171cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
1181cf1cae9SAlexei Starovoitov 	u32 retval, duration;
1196e6fddc7SDaniel Borkmann 	int hh_len = ETH_HLEN;
1201cf1cae9SAlexei Starovoitov 	struct sk_buff *skb;
1212cb494a3SSong Liu 	struct sock *sk;
1221cf1cae9SAlexei Starovoitov 	void *data;
1231cf1cae9SAlexei Starovoitov 	int ret;
1241cf1cae9SAlexei Starovoitov 
125586f8525SDavid Miller 	data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
1261cf1cae9SAlexei Starovoitov 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1271cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
1281cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
1291cf1cae9SAlexei Starovoitov 
1301cf1cae9SAlexei Starovoitov 	switch (prog->type) {
1311cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_CLS:
1321cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_ACT:
1331cf1cae9SAlexei Starovoitov 		is_l2 = true;
1341cf1cae9SAlexei Starovoitov 		/* fall through */
1351cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_IN:
1361cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_OUT:
1371cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_XMIT:
1381cf1cae9SAlexei Starovoitov 		is_direct_pkt_access = true;
1391cf1cae9SAlexei Starovoitov 		break;
1401cf1cae9SAlexei Starovoitov 	default:
1411cf1cae9SAlexei Starovoitov 		break;
1421cf1cae9SAlexei Starovoitov 	}
1431cf1cae9SAlexei Starovoitov 
1442cb494a3SSong Liu 	sk = kzalloc(sizeof(struct sock), GFP_USER);
1452cb494a3SSong Liu 	if (!sk) {
1461cf1cae9SAlexei Starovoitov 		kfree(data);
1471cf1cae9SAlexei Starovoitov 		return -ENOMEM;
1481cf1cae9SAlexei Starovoitov 	}
1492cb494a3SSong Liu 	sock_net_set(sk, current->nsproxy->net_ns);
1502cb494a3SSong Liu 	sock_init_data(NULL, sk);
1512cb494a3SSong Liu 
1522cb494a3SSong Liu 	skb = build_skb(data, 0);
1532cb494a3SSong Liu 	if (!skb) {
1542cb494a3SSong Liu 		kfree(data);
1552cb494a3SSong Liu 		kfree(sk);
1562cb494a3SSong Liu 		return -ENOMEM;
1572cb494a3SSong Liu 	}
1582cb494a3SSong Liu 	skb->sk = sk;
1591cf1cae9SAlexei Starovoitov 
160586f8525SDavid Miller 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1611cf1cae9SAlexei Starovoitov 	__skb_put(skb, size);
1621cf1cae9SAlexei Starovoitov 	skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
1631cf1cae9SAlexei Starovoitov 	skb_reset_network_header(skb);
1641cf1cae9SAlexei Starovoitov 
1651cf1cae9SAlexei Starovoitov 	if (is_l2)
1666e6fddc7SDaniel Borkmann 		__skb_push(skb, hh_len);
1671cf1cae9SAlexei Starovoitov 	if (is_direct_pkt_access)
1686aaae2b6SDaniel Borkmann 		bpf_compute_data_pointers(skb);
169*dcb40590SRoman Gushchin 	ret = bpf_test_run(prog, skb, repeat, &retval, &duration);
170*dcb40590SRoman Gushchin 	if (ret) {
171*dcb40590SRoman Gushchin 		kfree_skb(skb);
172*dcb40590SRoman Gushchin 		kfree(sk);
173*dcb40590SRoman Gushchin 		return ret;
174*dcb40590SRoman Gushchin 	}
1756e6fddc7SDaniel Borkmann 	if (!is_l2) {
1766e6fddc7SDaniel Borkmann 		if (skb_headroom(skb) < hh_len) {
1776e6fddc7SDaniel Borkmann 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
1786e6fddc7SDaniel Borkmann 
1796e6fddc7SDaniel Borkmann 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
1806e6fddc7SDaniel Borkmann 				kfree_skb(skb);
1812cb494a3SSong Liu 				kfree(sk);
1826e6fddc7SDaniel Borkmann 				return -ENOMEM;
1836e6fddc7SDaniel Borkmann 			}
1846e6fddc7SDaniel Borkmann 		}
1856e6fddc7SDaniel Borkmann 		memset(__skb_push(skb, hh_len), 0, hh_len);
1866e6fddc7SDaniel Borkmann 	}
1876e6fddc7SDaniel Borkmann 
1881cf1cae9SAlexei Starovoitov 	size = skb->len;
1891cf1cae9SAlexei Starovoitov 	/* bpf program can never convert linear skb to non-linear */
1901cf1cae9SAlexei Starovoitov 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
1911cf1cae9SAlexei Starovoitov 		size = skb_headlen(skb);
19278e52272SDavid Miller 	ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
1931cf1cae9SAlexei Starovoitov 	kfree_skb(skb);
1942cb494a3SSong Liu 	kfree(sk);
1951cf1cae9SAlexei Starovoitov 	return ret;
1961cf1cae9SAlexei Starovoitov }
1971cf1cae9SAlexei Starovoitov 
1981cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
1991cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
2001cf1cae9SAlexei Starovoitov {
2011cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
2021cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
20365073a67SDaniel Borkmann 	struct netdev_rx_queue *rxqueue;
2041cf1cae9SAlexei Starovoitov 	struct xdp_buff xdp = {};
2051cf1cae9SAlexei Starovoitov 	u32 retval, duration;
2061cf1cae9SAlexei Starovoitov 	void *data;
2071cf1cae9SAlexei Starovoitov 	int ret;
2081cf1cae9SAlexei Starovoitov 
209586f8525SDavid Miller 	data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
2101cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
2111cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
2121cf1cae9SAlexei Starovoitov 
2131cf1cae9SAlexei Starovoitov 	xdp.data_hard_start = data;
214586f8525SDavid Miller 	xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
215de8f3a83SDaniel Borkmann 	xdp.data_meta = xdp.data;
2161cf1cae9SAlexei Starovoitov 	xdp.data_end = xdp.data + size;
2171cf1cae9SAlexei Starovoitov 
21865073a67SDaniel Borkmann 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
21965073a67SDaniel Borkmann 	xdp.rxq = &rxqueue->xdp_rxq;
22065073a67SDaniel Borkmann 
221*dcb40590SRoman Gushchin 	ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration);
222*dcb40590SRoman Gushchin 	if (ret)
223*dcb40590SRoman Gushchin 		goto out;
224587b80ccSNikita V. Shirokov 	if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN ||
225587b80ccSNikita V. Shirokov 	    xdp.data_end != xdp.data + size)
2261cf1cae9SAlexei Starovoitov 		size = xdp.data_end - xdp.data;
22778e52272SDavid Miller 	ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
228*dcb40590SRoman Gushchin out:
2291cf1cae9SAlexei Starovoitov 	kfree(data);
2301cf1cae9SAlexei Starovoitov 	return ret;
2311cf1cae9SAlexei Starovoitov }
232