xref: /openbmc/linux/net/bpf/test_run.c (revision 5f8b7d4b2e9604d03ae06f1a2dd5a1f34c33e533)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21cf1cae9SAlexei Starovoitov /* Copyright (c) 2017 Facebook
31cf1cae9SAlexei Starovoitov  */
41cf1cae9SAlexei Starovoitov #include <linux/bpf.h>
5c48e51c8SKumar Kartikeya Dwivedi #include <linux/btf.h>
67bd1590dSMartin KaFai Lau #include <linux/btf_ids.h>
71cf1cae9SAlexei Starovoitov #include <linux/slab.h>
8b202d844SKumar Kartikeya Dwivedi #include <linux/init.h>
91cf1cae9SAlexei Starovoitov #include <linux/vmalloc.h>
101cf1cae9SAlexei Starovoitov #include <linux/etherdevice.h>
111cf1cae9SAlexei Starovoitov #include <linux/filter.h>
1287b7b533SYonghong Song #include <linux/rcupdate_trace.h>
131cf1cae9SAlexei Starovoitov #include <linux/sched/signal.h>
146ac99e8fSMartin KaFai Lau #include <net/bpf_sk_storage.h>
152cb494a3SSong Liu #include <net/sock.h>
162cb494a3SSong Liu #include <net/tcp.h>
177c32e8f8SLorenz Bauer #include <net/net_namespace.h>
18a9ca9f9cSYunsheng Lin #include <net/page_pool/helpers.h>
193d08b6f2SKP Singh #include <linux/error-injection.h>
201b4d60ecSSong Liu #include <linux/smp.h>
217c32e8f8SLorenz Bauer #include <linux/sock_diag.h>
222b99ef22SFlorian Westphal #include <linux/netfilter.h>
2349e47a5bSJakub Kicinski #include <net/netdev_rx_queue.h>
2447316f4aSZvi Effron #include <net/xdp.h>
252b99ef22SFlorian Westphal #include <net/netfilter/nf_bpf_link.h>
261cf1cae9SAlexei Starovoitov 
27e950e843SMatt Mullins #define CREATE_TRACE_POINTS
28e950e843SMatt Mullins #include <trace/events/bpf_test_run.h>
29e950e843SMatt Mullins 
30607b9cc9SLorenz Bauer struct bpf_test_timer {
31607b9cc9SLorenz Bauer 	enum { NO_PREEMPT, NO_MIGRATE } mode;
32607b9cc9SLorenz Bauer 	u32 i;
33607b9cc9SLorenz Bauer 	u64 time_start, time_spent;
34607b9cc9SLorenz Bauer };
35607b9cc9SLorenz Bauer 
bpf_test_timer_enter(struct bpf_test_timer * t)36607b9cc9SLorenz Bauer static void bpf_test_timer_enter(struct bpf_test_timer *t)
37607b9cc9SLorenz Bauer 	__acquires(rcu)
38607b9cc9SLorenz Bauer {
39607b9cc9SLorenz Bauer 	rcu_read_lock();
40607b9cc9SLorenz Bauer 	if (t->mode == NO_PREEMPT)
41607b9cc9SLorenz Bauer 		preempt_disable();
42607b9cc9SLorenz Bauer 	else
43607b9cc9SLorenz Bauer 		migrate_disable();
44607b9cc9SLorenz Bauer 
45607b9cc9SLorenz Bauer 	t->time_start = ktime_get_ns();
46607b9cc9SLorenz Bauer }
47607b9cc9SLorenz Bauer 
bpf_test_timer_leave(struct bpf_test_timer * t)48607b9cc9SLorenz Bauer static void bpf_test_timer_leave(struct bpf_test_timer *t)
49607b9cc9SLorenz Bauer 	__releases(rcu)
50607b9cc9SLorenz Bauer {
51607b9cc9SLorenz Bauer 	t->time_start = 0;
52607b9cc9SLorenz Bauer 
53607b9cc9SLorenz Bauer 	if (t->mode == NO_PREEMPT)
54607b9cc9SLorenz Bauer 		preempt_enable();
55607b9cc9SLorenz Bauer 	else
56607b9cc9SLorenz Bauer 		migrate_enable();
57607b9cc9SLorenz Bauer 	rcu_read_unlock();
58607b9cc9SLorenz Bauer }
59607b9cc9SLorenz Bauer 
bpf_test_timer_continue(struct bpf_test_timer * t,int iterations,u32 repeat,int * err,u32 * duration)60b530e9e1SToke Høiland-Jørgensen static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
61b530e9e1SToke Høiland-Jørgensen 				    u32 repeat, int *err, u32 *duration)
62607b9cc9SLorenz Bauer 	__must_hold(rcu)
63607b9cc9SLorenz Bauer {
64b530e9e1SToke Høiland-Jørgensen 	t->i += iterations;
65607b9cc9SLorenz Bauer 	if (t->i >= repeat) {
66607b9cc9SLorenz Bauer 		/* We're done. */
67607b9cc9SLorenz Bauer 		t->time_spent += ktime_get_ns() - t->time_start;
68607b9cc9SLorenz Bauer 		do_div(t->time_spent, t->i);
69607b9cc9SLorenz Bauer 		*duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent;
70607b9cc9SLorenz Bauer 		*err = 0;
71607b9cc9SLorenz Bauer 		goto reset;
72607b9cc9SLorenz Bauer 	}
73607b9cc9SLorenz Bauer 
74607b9cc9SLorenz Bauer 	if (signal_pending(current)) {
75607b9cc9SLorenz Bauer 		/* During iteration: we've been cancelled, abort. */
76607b9cc9SLorenz Bauer 		*err = -EINTR;
77607b9cc9SLorenz Bauer 		goto reset;
78607b9cc9SLorenz Bauer 	}
79607b9cc9SLorenz Bauer 
80607b9cc9SLorenz Bauer 	if (need_resched()) {
81607b9cc9SLorenz Bauer 		/* During iteration: we need to reschedule between runs. */
82607b9cc9SLorenz Bauer 		t->time_spent += ktime_get_ns() - t->time_start;
83607b9cc9SLorenz Bauer 		bpf_test_timer_leave(t);
84607b9cc9SLorenz Bauer 		cond_resched();
85607b9cc9SLorenz Bauer 		bpf_test_timer_enter(t);
86607b9cc9SLorenz Bauer 	}
87607b9cc9SLorenz Bauer 
88607b9cc9SLorenz Bauer 	/* Do another round. */
89607b9cc9SLorenz Bauer 	return true;
90607b9cc9SLorenz Bauer 
91607b9cc9SLorenz Bauer reset:
92607b9cc9SLorenz Bauer 	t->i = 0;
93607b9cc9SLorenz Bauer 	return false;
94607b9cc9SLorenz Bauer }
95607b9cc9SLorenz Bauer 
96b530e9e1SToke Høiland-Jørgensen /* We put this struct at the head of each page with a context and frame
97b530e9e1SToke Høiland-Jørgensen  * initialised when the page is allocated, so we don't have to do this on each
98b530e9e1SToke Høiland-Jørgensen  * repetition of the test run.
99b530e9e1SToke Høiland-Jørgensen  */
100b530e9e1SToke Høiland-Jørgensen struct xdp_page_head {
101b530e9e1SToke Høiland-Jørgensen 	struct xdp_buff orig_ctx;
102b530e9e1SToke Høiland-Jørgensen 	struct xdp_buff ctx;
103294635a8SAlexander Lobakin 	union {
104294635a8SAlexander Lobakin 		/* ::data_hard_start starts here */
105294635a8SAlexander Lobakin 		DECLARE_FLEX_ARRAY(struct xdp_frame, frame);
106294635a8SAlexander Lobakin 		DECLARE_FLEX_ARRAY(u8, data);
107294635a8SAlexander Lobakin 	};
108b530e9e1SToke Høiland-Jørgensen };
109b530e9e1SToke Høiland-Jørgensen 
110b530e9e1SToke Høiland-Jørgensen struct xdp_test_data {
111b530e9e1SToke Høiland-Jørgensen 	struct xdp_buff *orig_ctx;
112b530e9e1SToke Høiland-Jørgensen 	struct xdp_rxq_info rxq;
113b530e9e1SToke Høiland-Jørgensen 	struct net_device *dev;
114b530e9e1SToke Høiland-Jørgensen 	struct page_pool *pp;
115b530e9e1SToke Høiland-Jørgensen 	struct xdp_frame **frames;
116b530e9e1SToke Høiland-Jørgensen 	struct sk_buff **skbs;
117425d2393SToke Høiland-Jørgensen 	struct xdp_mem_info mem;
118b530e9e1SToke Høiland-Jørgensen 	u32 batch_size;
119b530e9e1SToke Høiland-Jørgensen 	u32 frame_cnt;
120b530e9e1SToke Høiland-Jørgensen };
121b530e9e1SToke Høiland-Jørgensen 
122294635a8SAlexander Lobakin /* tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c:%MAX_PKT_SIZE
123294635a8SAlexander Lobakin  * must be updated accordingly this gets changed, otherwise BPF selftests
124294635a8SAlexander Lobakin  * will fail.
125294635a8SAlexander Lobakin  */
126b6f1f780SToke Høiland-Jørgensen #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head))
127b530e9e1SToke Høiland-Jørgensen #define TEST_XDP_MAX_BATCH 256
128b530e9e1SToke Høiland-Jørgensen 
xdp_test_run_init_page(struct page * page,void * arg)129b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_init_page(struct page *page, void *arg)
130b530e9e1SToke Høiland-Jørgensen {
131b530e9e1SToke Høiland-Jørgensen 	struct xdp_page_head *head = phys_to_virt(page_to_phys(page));
132b530e9e1SToke Høiland-Jørgensen 	struct xdp_buff *new_ctx, *orig_ctx;
133b530e9e1SToke Høiland-Jørgensen 	u32 headroom = XDP_PACKET_HEADROOM;
134b530e9e1SToke Høiland-Jørgensen 	struct xdp_test_data *xdp = arg;
135b530e9e1SToke Høiland-Jørgensen 	size_t frm_len, meta_len;
136b530e9e1SToke Høiland-Jørgensen 	struct xdp_frame *frm;
137b530e9e1SToke Høiland-Jørgensen 	void *data;
138b530e9e1SToke Høiland-Jørgensen 
139b530e9e1SToke Høiland-Jørgensen 	orig_ctx = xdp->orig_ctx;
140b530e9e1SToke Høiland-Jørgensen 	frm_len = orig_ctx->data_end - orig_ctx->data_meta;
141b530e9e1SToke Høiland-Jørgensen 	meta_len = orig_ctx->data - orig_ctx->data_meta;
142b530e9e1SToke Høiland-Jørgensen 	headroom -= meta_len;
143b530e9e1SToke Høiland-Jørgensen 
144b530e9e1SToke Høiland-Jørgensen 	new_ctx = &head->ctx;
145294635a8SAlexander Lobakin 	frm = head->frame;
146294635a8SAlexander Lobakin 	data = head->data;
147b530e9e1SToke Høiland-Jørgensen 	memcpy(data + headroom, orig_ctx->data_meta, frm_len);
148b530e9e1SToke Høiland-Jørgensen 
149b530e9e1SToke Høiland-Jørgensen 	xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq);
150b530e9e1SToke Høiland-Jørgensen 	xdp_prepare_buff(new_ctx, data, headroom, frm_len, true);
151b530e9e1SToke Høiland-Jørgensen 	new_ctx->data = new_ctx->data_meta + meta_len;
152b530e9e1SToke Høiland-Jørgensen 
153b530e9e1SToke Høiland-Jørgensen 	xdp_update_frame_from_buff(new_ctx, frm);
154b530e9e1SToke Høiland-Jørgensen 	frm->mem = new_ctx->rxq->mem;
155b530e9e1SToke Høiland-Jørgensen 
156b530e9e1SToke Høiland-Jørgensen 	memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx));
157b530e9e1SToke Høiland-Jørgensen }
158b530e9e1SToke Høiland-Jørgensen 
xdp_test_run_setup(struct xdp_test_data * xdp,struct xdp_buff * orig_ctx)159b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx)
160b530e9e1SToke Høiland-Jørgensen {
161b530e9e1SToke Høiland-Jørgensen 	struct page_pool *pp;
162b530e9e1SToke Høiland-Jørgensen 	int err = -ENOMEM;
163b530e9e1SToke Høiland-Jørgensen 	struct page_pool_params pp_params = {
164b530e9e1SToke Høiland-Jørgensen 		.order = 0,
165b530e9e1SToke Høiland-Jørgensen 		.flags = 0,
166b530e9e1SToke Høiland-Jørgensen 		.pool_size = xdp->batch_size,
167b530e9e1SToke Høiland-Jørgensen 		.nid = NUMA_NO_NODE,
168b530e9e1SToke Høiland-Jørgensen 		.init_callback = xdp_test_run_init_page,
169b530e9e1SToke Høiland-Jørgensen 		.init_arg = xdp,
170b530e9e1SToke Høiland-Jørgensen 	};
171b530e9e1SToke Høiland-Jørgensen 
172b530e9e1SToke Høiland-Jørgensen 	xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
173b530e9e1SToke Høiland-Jørgensen 	if (!xdp->frames)
174b530e9e1SToke Høiland-Jørgensen 		return -ENOMEM;
175b530e9e1SToke Høiland-Jørgensen 
176b530e9e1SToke Høiland-Jørgensen 	xdp->skbs = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL);
177b530e9e1SToke Høiland-Jørgensen 	if (!xdp->skbs)
178b530e9e1SToke Høiland-Jørgensen 		goto err_skbs;
179b530e9e1SToke Høiland-Jørgensen 
180b530e9e1SToke Høiland-Jørgensen 	pp = page_pool_create(&pp_params);
181b530e9e1SToke Høiland-Jørgensen 	if (IS_ERR(pp)) {
182b530e9e1SToke Høiland-Jørgensen 		err = PTR_ERR(pp);
183b530e9e1SToke Høiland-Jørgensen 		goto err_pp;
184b530e9e1SToke Høiland-Jørgensen 	}
185b530e9e1SToke Høiland-Jørgensen 
186b530e9e1SToke Høiland-Jørgensen 	/* will copy 'mem.id' into pp->xdp_mem_id */
187425d2393SToke Høiland-Jørgensen 	err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp);
188b530e9e1SToke Høiland-Jørgensen 	if (err)
189b530e9e1SToke Høiland-Jørgensen 		goto err_mmodel;
190b530e9e1SToke Høiland-Jørgensen 
191b530e9e1SToke Høiland-Jørgensen 	xdp->pp = pp;
192b530e9e1SToke Høiland-Jørgensen 
193b530e9e1SToke Høiland-Jørgensen 	/* We create a 'fake' RXQ referencing the original dev, but with an
194b530e9e1SToke Høiland-Jørgensen 	 * xdp_mem_info pointing to our page_pool
195b530e9e1SToke Høiland-Jørgensen 	 */
196b530e9e1SToke Høiland-Jørgensen 	xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0);
197b530e9e1SToke Høiland-Jørgensen 	xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL;
198b530e9e1SToke Høiland-Jørgensen 	xdp->rxq.mem.id = pp->xdp_mem_id;
199b530e9e1SToke Høiland-Jørgensen 	xdp->dev = orig_ctx->rxq->dev;
200b530e9e1SToke Høiland-Jørgensen 	xdp->orig_ctx = orig_ctx;
201b530e9e1SToke Høiland-Jørgensen 
202b530e9e1SToke Høiland-Jørgensen 	return 0;
203b530e9e1SToke Høiland-Jørgensen 
204b530e9e1SToke Høiland-Jørgensen err_mmodel:
205b530e9e1SToke Høiland-Jørgensen 	page_pool_destroy(pp);
206b530e9e1SToke Høiland-Jørgensen err_pp:
207743bec1bSYihao Han 	kvfree(xdp->skbs);
208b530e9e1SToke Høiland-Jørgensen err_skbs:
209743bec1bSYihao Han 	kvfree(xdp->frames);
210b530e9e1SToke Høiland-Jørgensen 	return err;
211b530e9e1SToke Høiland-Jørgensen }
212b530e9e1SToke Høiland-Jørgensen 
xdp_test_run_teardown(struct xdp_test_data * xdp)213b530e9e1SToke Høiland-Jørgensen static void xdp_test_run_teardown(struct xdp_test_data *xdp)
214b530e9e1SToke Høiland-Jørgensen {
215425d2393SToke Høiland-Jørgensen 	xdp_unreg_mem_model(&xdp->mem);
216b530e9e1SToke Høiland-Jørgensen 	page_pool_destroy(xdp->pp);
217b530e9e1SToke Høiland-Jørgensen 	kfree(xdp->frames);
218b530e9e1SToke Høiland-Jørgensen 	kfree(xdp->skbs);
219b530e9e1SToke Høiland-Jørgensen }
220b530e9e1SToke Høiland-Jørgensen 
frame_was_changed(const struct xdp_page_head * head)221e5995bc7SAlexander Lobakin static bool frame_was_changed(const struct xdp_page_head *head)
222e5995bc7SAlexander Lobakin {
223e5995bc7SAlexander Lobakin 	/* xdp_scrub_frame() zeroes the data pointer, flags is the last field,
224e5995bc7SAlexander Lobakin 	 * i.e. has the highest chances to be overwritten. If those two are
225e5995bc7SAlexander Lobakin 	 * untouched, it's most likely safe to skip the context reset.
226e5995bc7SAlexander Lobakin 	 */
227c2865b11SJakub Kicinski 	return head->frame->data != head->orig_ctx.data ||
228c2865b11SJakub Kicinski 	       head->frame->flags != head->orig_ctx.flags;
229e5995bc7SAlexander Lobakin }
230e5995bc7SAlexander Lobakin 
ctx_was_changed(struct xdp_page_head * head)231b530e9e1SToke Høiland-Jørgensen static bool ctx_was_changed(struct xdp_page_head *head)
232b530e9e1SToke Høiland-Jørgensen {
233b530e9e1SToke Høiland-Jørgensen 	return head->orig_ctx.data != head->ctx.data ||
234b530e9e1SToke Høiland-Jørgensen 		head->orig_ctx.data_meta != head->ctx.data_meta ||
235b530e9e1SToke Høiland-Jørgensen 		head->orig_ctx.data_end != head->ctx.data_end;
236b530e9e1SToke Høiland-Jørgensen }
237b530e9e1SToke Høiland-Jørgensen 
reset_ctx(struct xdp_page_head * head)238b530e9e1SToke Høiland-Jørgensen static void reset_ctx(struct xdp_page_head *head)
239b530e9e1SToke Høiland-Jørgensen {
240e5995bc7SAlexander Lobakin 	if (likely(!frame_was_changed(head) && !ctx_was_changed(head)))
241b530e9e1SToke Høiland-Jørgensen 		return;
242b530e9e1SToke Høiland-Jørgensen 
243b530e9e1SToke Høiland-Jørgensen 	head->ctx.data = head->orig_ctx.data;
244b530e9e1SToke Høiland-Jørgensen 	head->ctx.data_meta = head->orig_ctx.data_meta;
245b530e9e1SToke Høiland-Jørgensen 	head->ctx.data_end = head->orig_ctx.data_end;
246294635a8SAlexander Lobakin 	xdp_update_frame_from_buff(&head->ctx, head->frame);
247*d4d5767cSToke Høiland-Jørgensen 	head->frame->mem = head->orig_ctx.rxq->mem;
248b530e9e1SToke Høiland-Jørgensen }
249b530e9e1SToke Høiland-Jørgensen 
xdp_recv_frames(struct xdp_frame ** frames,int nframes,struct sk_buff ** skbs,struct net_device * dev)250b530e9e1SToke Høiland-Jørgensen static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
251b530e9e1SToke Høiland-Jørgensen 			   struct sk_buff **skbs,
252b530e9e1SToke Høiland-Jørgensen 			   struct net_device *dev)
253b530e9e1SToke Høiland-Jørgensen {
254b530e9e1SToke Høiland-Jørgensen 	gfp_t gfp = __GFP_ZERO | GFP_ATOMIC;
255b530e9e1SToke Høiland-Jørgensen 	int i, n;
256b530e9e1SToke Høiland-Jørgensen 	LIST_HEAD(list);
257b530e9e1SToke Høiland-Jørgensen 
258025a785fSJakub Kicinski 	n = kmem_cache_alloc_bulk(skbuff_cache, gfp, nframes, (void **)skbs);
259b530e9e1SToke Høiland-Jørgensen 	if (unlikely(n == 0)) {
260b530e9e1SToke Høiland-Jørgensen 		for (i = 0; i < nframes; i++)
261b530e9e1SToke Høiland-Jørgensen 			xdp_return_frame(frames[i]);
262b530e9e1SToke Høiland-Jørgensen 		return -ENOMEM;
263b530e9e1SToke Høiland-Jørgensen 	}
264b530e9e1SToke Høiland-Jørgensen 
265b530e9e1SToke Høiland-Jørgensen 	for (i = 0; i < nframes; i++) {
266b530e9e1SToke Høiland-Jørgensen 		struct xdp_frame *xdpf = frames[i];
267b530e9e1SToke Høiland-Jørgensen 		struct sk_buff *skb = skbs[i];
268b530e9e1SToke Høiland-Jørgensen 
269b530e9e1SToke Høiland-Jørgensen 		skb = __xdp_build_skb_from_frame(xdpf, skb, dev);
270b530e9e1SToke Høiland-Jørgensen 		if (!skb) {
271b530e9e1SToke Høiland-Jørgensen 			xdp_return_frame(xdpf);
272b530e9e1SToke Høiland-Jørgensen 			continue;
273b530e9e1SToke Høiland-Jørgensen 		}
274b530e9e1SToke Høiland-Jørgensen 
275b530e9e1SToke Høiland-Jørgensen 		list_add_tail(&skb->list, &list);
276b530e9e1SToke Høiland-Jørgensen 	}
277b530e9e1SToke Høiland-Jørgensen 	netif_receive_skb_list(&list);
278b530e9e1SToke Høiland-Jørgensen 
279b530e9e1SToke Høiland-Jørgensen 	return 0;
280b530e9e1SToke Høiland-Jørgensen }
281b530e9e1SToke Høiland-Jørgensen 
xdp_test_run_batch(struct xdp_test_data * xdp,struct bpf_prog * prog,u32 repeat)282b530e9e1SToke Høiland-Jørgensen static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog,
283b530e9e1SToke Høiland-Jørgensen 			      u32 repeat)
284b530e9e1SToke Høiland-Jørgensen {
285b530e9e1SToke Høiland-Jørgensen 	struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
286b530e9e1SToke Høiland-Jørgensen 	int err = 0, act, ret, i, nframes = 0, batch_sz;
287b530e9e1SToke Høiland-Jørgensen 	struct xdp_frame **frames = xdp->frames;
288b530e9e1SToke Høiland-Jørgensen 	struct xdp_page_head *head;
289b530e9e1SToke Høiland-Jørgensen 	struct xdp_frame *frm;
290b530e9e1SToke Høiland-Jørgensen 	bool redirect = false;
291b530e9e1SToke Høiland-Jørgensen 	struct xdp_buff *ctx;
292b530e9e1SToke Høiland-Jørgensen 	struct page *page;
293b530e9e1SToke Høiland-Jørgensen 
294b530e9e1SToke Høiland-Jørgensen 	batch_sz = min_t(u32, repeat, xdp->batch_size);
295b530e9e1SToke Høiland-Jørgensen 
296b530e9e1SToke Høiland-Jørgensen 	local_bh_disable();
297b530e9e1SToke Høiland-Jørgensen 	xdp_set_return_frame_no_direct();
298b530e9e1SToke Høiland-Jørgensen 
299b530e9e1SToke Høiland-Jørgensen 	for (i = 0; i < batch_sz; i++) {
300b530e9e1SToke Høiland-Jørgensen 		page = page_pool_dev_alloc_pages(xdp->pp);
301b530e9e1SToke Høiland-Jørgensen 		if (!page) {
302b530e9e1SToke Høiland-Jørgensen 			err = -ENOMEM;
303b530e9e1SToke Høiland-Jørgensen 			goto out;
304b530e9e1SToke Høiland-Jørgensen 		}
305b530e9e1SToke Høiland-Jørgensen 
306b530e9e1SToke Høiland-Jørgensen 		head = phys_to_virt(page_to_phys(page));
307b530e9e1SToke Høiland-Jørgensen 		reset_ctx(head);
308b530e9e1SToke Høiland-Jørgensen 		ctx = &head->ctx;
309294635a8SAlexander Lobakin 		frm = head->frame;
310b530e9e1SToke Høiland-Jørgensen 		xdp->frame_cnt++;
311b530e9e1SToke Høiland-Jørgensen 
312b530e9e1SToke Høiland-Jørgensen 		act = bpf_prog_run_xdp(prog, ctx);
313b530e9e1SToke Høiland-Jørgensen 
314b530e9e1SToke Høiland-Jørgensen 		/* if program changed pkt bounds we need to update the xdp_frame */
315b530e9e1SToke Høiland-Jørgensen 		if (unlikely(ctx_was_changed(head))) {
316b530e9e1SToke Høiland-Jørgensen 			ret = xdp_update_frame_from_buff(ctx, frm);
317b530e9e1SToke Høiland-Jørgensen 			if (ret) {
318b530e9e1SToke Høiland-Jørgensen 				xdp_return_buff(ctx);
319b530e9e1SToke Høiland-Jørgensen 				continue;
320b530e9e1SToke Høiland-Jørgensen 			}
321b530e9e1SToke Høiland-Jørgensen 		}
322b530e9e1SToke Høiland-Jørgensen 
323b530e9e1SToke Høiland-Jørgensen 		switch (act) {
324b530e9e1SToke Høiland-Jørgensen 		case XDP_TX:
325b530e9e1SToke Høiland-Jørgensen 			/* we can't do a real XDP_TX since we're not in the
326b530e9e1SToke Høiland-Jørgensen 			 * driver, so turn it into a REDIRECT back to the same
327b530e9e1SToke Høiland-Jørgensen 			 * index
328b530e9e1SToke Høiland-Jørgensen 			 */
329b530e9e1SToke Høiland-Jørgensen 			ri->tgt_index = xdp->dev->ifindex;
330b530e9e1SToke Høiland-Jørgensen 			ri->map_id = INT_MAX;
331b530e9e1SToke Høiland-Jørgensen 			ri->map_type = BPF_MAP_TYPE_UNSPEC;
332b530e9e1SToke Høiland-Jørgensen 			fallthrough;
333b530e9e1SToke Høiland-Jørgensen 		case XDP_REDIRECT:
334b530e9e1SToke Høiland-Jørgensen 			redirect = true;
335b530e9e1SToke Høiland-Jørgensen 			ret = xdp_do_redirect_frame(xdp->dev, ctx, frm, prog);
336b530e9e1SToke Høiland-Jørgensen 			if (ret)
337b530e9e1SToke Høiland-Jørgensen 				xdp_return_buff(ctx);
338b530e9e1SToke Høiland-Jørgensen 			break;
339b530e9e1SToke Høiland-Jørgensen 		case XDP_PASS:
340b530e9e1SToke Høiland-Jørgensen 			frames[nframes++] = frm;
341b530e9e1SToke Høiland-Jørgensen 			break;
342b530e9e1SToke Høiland-Jørgensen 		default:
343b530e9e1SToke Høiland-Jørgensen 			bpf_warn_invalid_xdp_action(NULL, prog, act);
344b530e9e1SToke Høiland-Jørgensen 			fallthrough;
345b530e9e1SToke Høiland-Jørgensen 		case XDP_DROP:
346b530e9e1SToke Høiland-Jørgensen 			xdp_return_buff(ctx);
347b530e9e1SToke Høiland-Jørgensen 			break;
348b530e9e1SToke Høiland-Jørgensen 		}
349b530e9e1SToke Høiland-Jørgensen 	}
350b530e9e1SToke Høiland-Jørgensen 
351b530e9e1SToke Høiland-Jørgensen out:
352b530e9e1SToke Høiland-Jørgensen 	if (redirect)
353b530e9e1SToke Høiland-Jørgensen 		xdp_do_flush();
354b530e9e1SToke Høiland-Jørgensen 	if (nframes) {
355b530e9e1SToke Høiland-Jørgensen 		ret = xdp_recv_frames(frames, nframes, xdp->skbs, xdp->dev);
356b530e9e1SToke Høiland-Jørgensen 		if (ret)
357b530e9e1SToke Høiland-Jørgensen 			err = ret;
358b530e9e1SToke Høiland-Jørgensen 	}
359b530e9e1SToke Høiland-Jørgensen 
360b530e9e1SToke Høiland-Jørgensen 	xdp_clear_return_frame_no_direct();
361b530e9e1SToke Høiland-Jørgensen 	local_bh_enable();
362b530e9e1SToke Høiland-Jørgensen 	return err;
363b530e9e1SToke Høiland-Jørgensen }
364b530e9e1SToke Høiland-Jørgensen 
bpf_test_run_xdp_live(struct bpf_prog * prog,struct xdp_buff * ctx,u32 repeat,u32 batch_size,u32 * time)365b530e9e1SToke Høiland-Jørgensen static int bpf_test_run_xdp_live(struct bpf_prog *prog, struct xdp_buff *ctx,
366b530e9e1SToke Høiland-Jørgensen 				 u32 repeat, u32 batch_size, u32 *time)
367b530e9e1SToke Høiland-Jørgensen 
368b530e9e1SToke Høiland-Jørgensen {
369b530e9e1SToke Høiland-Jørgensen 	struct xdp_test_data xdp = { .batch_size = batch_size };
370b530e9e1SToke Høiland-Jørgensen 	struct bpf_test_timer t = { .mode = NO_MIGRATE };
371b530e9e1SToke Høiland-Jørgensen 	int ret;
372b530e9e1SToke Høiland-Jørgensen 
373b530e9e1SToke Høiland-Jørgensen 	if (!repeat)
374b530e9e1SToke Høiland-Jørgensen 		repeat = 1;
375b530e9e1SToke Høiland-Jørgensen 
376b530e9e1SToke Høiland-Jørgensen 	ret = xdp_test_run_setup(&xdp, ctx);
377b530e9e1SToke Høiland-Jørgensen 	if (ret)
378b530e9e1SToke Høiland-Jørgensen 		return ret;
379b530e9e1SToke Høiland-Jørgensen 
380b530e9e1SToke Høiland-Jørgensen 	bpf_test_timer_enter(&t);
381b530e9e1SToke Høiland-Jørgensen 	do {
382b530e9e1SToke Høiland-Jørgensen 		xdp.frame_cnt = 0;
383b530e9e1SToke Høiland-Jørgensen 		ret = xdp_test_run_batch(&xdp, prog, repeat - t.i);
384b530e9e1SToke Høiland-Jørgensen 		if (unlikely(ret < 0))
385b530e9e1SToke Høiland-Jørgensen 			break;
386b530e9e1SToke Høiland-Jørgensen 	} while (bpf_test_timer_continue(&t, xdp.frame_cnt, repeat, &ret, time));
387b530e9e1SToke Høiland-Jørgensen 	bpf_test_timer_leave(&t);
388b530e9e1SToke Høiland-Jørgensen 
389b530e9e1SToke Høiland-Jørgensen 	xdp_test_run_teardown(&xdp);
390b530e9e1SToke Høiland-Jørgensen 	return ret;
391b530e9e1SToke Høiland-Jørgensen }
392b530e9e1SToke Høiland-Jørgensen 
bpf_test_run(struct bpf_prog * prog,void * ctx,u32 repeat,u32 * retval,u32 * time,bool xdp)393df1a2cb7SStanislav Fomichev static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat,
394f23c4b39SBjörn Töpel 			u32 *retval, u32 *time, bool xdp)
3951cf1cae9SAlexei Starovoitov {
396c7603cfaSAndrii Nakryiko 	struct bpf_prog_array_item item = {.prog = prog};
397c7603cfaSAndrii Nakryiko 	struct bpf_run_ctx *old_ctx;
398c7603cfaSAndrii Nakryiko 	struct bpf_cg_run_ctx run_ctx;
399607b9cc9SLorenz Bauer 	struct bpf_test_timer t = { NO_MIGRATE };
4008bad74f9SRoman Gushchin 	enum bpf_cgroup_storage_type stype;
401607b9cc9SLorenz Bauer 	int ret;
4021cf1cae9SAlexei Starovoitov 
4038bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype) {
404c7603cfaSAndrii Nakryiko 		item.cgroup_storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
405c7603cfaSAndrii Nakryiko 		if (IS_ERR(item.cgroup_storage[stype])) {
406c7603cfaSAndrii Nakryiko 			item.cgroup_storage[stype] = NULL;
4078bad74f9SRoman Gushchin 			for_each_cgroup_storage_type(stype)
408c7603cfaSAndrii Nakryiko 				bpf_cgroup_storage_free(item.cgroup_storage[stype]);
4098bad74f9SRoman Gushchin 			return -ENOMEM;
4108bad74f9SRoman Gushchin 		}
4118bad74f9SRoman Gushchin 	}
412f42ee093SRoman Gushchin 
4131cf1cae9SAlexei Starovoitov 	if (!repeat)
4141cf1cae9SAlexei Starovoitov 		repeat = 1;
415df1a2cb7SStanislav Fomichev 
416607b9cc9SLorenz Bauer 	bpf_test_timer_enter(&t);
417c7603cfaSAndrii Nakryiko 	old_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
418607b9cc9SLorenz Bauer 	do {
419c7603cfaSAndrii Nakryiko 		run_ctx.prog_item = &item;
420af2d0d09SMartin KaFai Lau 		local_bh_disable();
421f23c4b39SBjörn Töpel 		if (xdp)
422f23c4b39SBjörn Töpel 			*retval = bpf_prog_run_xdp(prog, ctx);
423f23c4b39SBjörn Töpel 		else
424fb7dd8bcSAndrii Nakryiko 			*retval = bpf_prog_run(prog, ctx);
425af2d0d09SMartin KaFai Lau 		local_bh_enable();
426b530e9e1SToke Høiland-Jørgensen 	} while (bpf_test_timer_continue(&t, 1, repeat, &ret, time));
427c7603cfaSAndrii Nakryiko 	bpf_reset_run_ctx(old_ctx);
428607b9cc9SLorenz Bauer 	bpf_test_timer_leave(&t);
4291cf1cae9SAlexei Starovoitov 
4308bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype)
431c7603cfaSAndrii Nakryiko 		bpf_cgroup_storage_free(item.cgroup_storage[stype]);
432f42ee093SRoman Gushchin 
433df1a2cb7SStanislav Fomichev 	return ret;
4341cf1cae9SAlexei Starovoitov }
4351cf1cae9SAlexei Starovoitov 
bpf_test_finish(const union bpf_attr * kattr,union bpf_attr __user * uattr,const void * data,struct skb_shared_info * sinfo,u32 size,u32 retval,u32 duration)43678e52272SDavid Miller static int bpf_test_finish(const union bpf_attr *kattr,
43778e52272SDavid Miller 			   union bpf_attr __user *uattr, const void *data,
4387855e0dbSLorenzo Bianconi 			   struct skb_shared_info *sinfo, u32 size,
4397855e0dbSLorenzo Bianconi 			   u32 retval, u32 duration)
4401cf1cae9SAlexei Starovoitov {
44178e52272SDavid Miller 	void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
4421cf1cae9SAlexei Starovoitov 	int err = -EFAULT;
443b5a36b1eSLorenz Bauer 	u32 copy_size = size;
4441cf1cae9SAlexei Starovoitov 
445b5a36b1eSLorenz Bauer 	/* Clamp copy if the user has provided a size hint, but copy the full
446b5a36b1eSLorenz Bauer 	 * buffer if not to retain old behaviour.
447b5a36b1eSLorenz Bauer 	 */
448b5a36b1eSLorenz Bauer 	if (kattr->test.data_size_out &&
449b5a36b1eSLorenz Bauer 	    copy_size > kattr->test.data_size_out) {
450b5a36b1eSLorenz Bauer 		copy_size = kattr->test.data_size_out;
451b5a36b1eSLorenz Bauer 		err = -ENOSPC;
452b5a36b1eSLorenz Bauer 	}
453b5a36b1eSLorenz Bauer 
4547855e0dbSLorenzo Bianconi 	if (data_out) {
4557855e0dbSLorenzo Bianconi 		int len = sinfo ? copy_size - sinfo->xdp_frags_size : copy_size;
4567855e0dbSLorenzo Bianconi 
457530e214cSStanislav Fomichev 		if (len < 0) {
458530e214cSStanislav Fomichev 			err = -ENOSPC;
459530e214cSStanislav Fomichev 			goto out;
460530e214cSStanislav Fomichev 		}
461530e214cSStanislav Fomichev 
4627855e0dbSLorenzo Bianconi 		if (copy_to_user(data_out, data, len))
4631cf1cae9SAlexei Starovoitov 			goto out;
4647855e0dbSLorenzo Bianconi 
4657855e0dbSLorenzo Bianconi 		if (sinfo) {
4665d1e9f43SStanislav Fomichev 			int i, offset = len;
4675d1e9f43SStanislav Fomichev 			u32 data_len;
4687855e0dbSLorenzo Bianconi 
4697855e0dbSLorenzo Bianconi 			for (i = 0; i < sinfo->nr_frags; i++) {
4707855e0dbSLorenzo Bianconi 				skb_frag_t *frag = &sinfo->frags[i];
4717855e0dbSLorenzo Bianconi 
4727855e0dbSLorenzo Bianconi 				if (offset >= copy_size) {
4737855e0dbSLorenzo Bianconi 					err = -ENOSPC;
4747855e0dbSLorenzo Bianconi 					break;
4757855e0dbSLorenzo Bianconi 				}
4767855e0dbSLorenzo Bianconi 
4775d1e9f43SStanislav Fomichev 				data_len = min_t(u32, copy_size - offset,
4787855e0dbSLorenzo Bianconi 						 skb_frag_size(frag));
4797855e0dbSLorenzo Bianconi 
4807855e0dbSLorenzo Bianconi 				if (copy_to_user(data_out + offset,
4817855e0dbSLorenzo Bianconi 						 skb_frag_address(frag),
4827855e0dbSLorenzo Bianconi 						 data_len))
4837855e0dbSLorenzo Bianconi 					goto out;
4847855e0dbSLorenzo Bianconi 
4857855e0dbSLorenzo Bianconi 				offset += data_len;
4867855e0dbSLorenzo Bianconi 			}
4877855e0dbSLorenzo Bianconi 		}
4887855e0dbSLorenzo Bianconi 	}
4897855e0dbSLorenzo Bianconi 
4901cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
4911cf1cae9SAlexei Starovoitov 		goto out;
4921cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
4931cf1cae9SAlexei Starovoitov 		goto out;
4941cf1cae9SAlexei Starovoitov 	if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
4951cf1cae9SAlexei Starovoitov 		goto out;
496b5a36b1eSLorenz Bauer 	if (err != -ENOSPC)
4971cf1cae9SAlexei Starovoitov 		err = 0;
4981cf1cae9SAlexei Starovoitov out:
499e950e843SMatt Mullins 	trace_bpf_test_finish(&err);
5001cf1cae9SAlexei Starovoitov 	return err;
5011cf1cae9SAlexei Starovoitov }
5021cf1cae9SAlexei Starovoitov 
503faeb2dceSAlexei Starovoitov /* Integer types of various sizes and pointer combinations cover variety of
504faeb2dceSAlexei Starovoitov  * architecture dependent calling conventions. 7+ can be supported in the
505faeb2dceSAlexei Starovoitov  * future.
506faeb2dceSAlexei Starovoitov  */
507e9ff9d52SJean-Philippe Menil __diag_push();
5080b206c6dSKumar Kartikeya Dwivedi __diag_ignore_all("-Wmissing-prototypes",
509e9ff9d52SJean-Philippe Menil 		  "Global functions as their definitions will be in vmlinux BTF");
bpf_fentry_test1(int a)510400031e0SDavid Vernet __bpf_kfunc int bpf_fentry_test1(int a)
511faeb2dceSAlexei Starovoitov {
512faeb2dceSAlexei Starovoitov 	return a + 1;
513faeb2dceSAlexei Starovoitov }
51446565696SKumar Kartikeya Dwivedi EXPORT_SYMBOL_GPL(bpf_fentry_test1);
515faeb2dceSAlexei Starovoitov 
bpf_fentry_test2(int a,u64 b)516faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test2(int a, u64 b)
517faeb2dceSAlexei Starovoitov {
518faeb2dceSAlexei Starovoitov 	return a + b;
519faeb2dceSAlexei Starovoitov }
520faeb2dceSAlexei Starovoitov 
bpf_fentry_test3(char a,int b,u64 c)521faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test3(char a, int b, u64 c)
522faeb2dceSAlexei Starovoitov {
523faeb2dceSAlexei Starovoitov 	return a + b + c;
524faeb2dceSAlexei Starovoitov }
525faeb2dceSAlexei Starovoitov 
bpf_fentry_test4(void * a,char b,int c,u64 d)526faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test4(void *a, char b, int c, u64 d)
527faeb2dceSAlexei Starovoitov {
528faeb2dceSAlexei Starovoitov 	return (long)a + b + c + d;
529faeb2dceSAlexei Starovoitov }
530faeb2dceSAlexei Starovoitov 
bpf_fentry_test5(u64 a,void * b,short c,int d,u64 e)531faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test5(u64 a, void *b, short c, int d, u64 e)
532faeb2dceSAlexei Starovoitov {
533faeb2dceSAlexei Starovoitov 	return a + (long)b + c + d + e;
534faeb2dceSAlexei Starovoitov }
535faeb2dceSAlexei Starovoitov 
bpf_fentry_test6(u64 a,void * b,short c,int d,void * e,u64 f)536faeb2dceSAlexei Starovoitov int noinline bpf_fentry_test6(u64 a, void *b, short c, int d, void *e, u64 f)
537faeb2dceSAlexei Starovoitov {
538faeb2dceSAlexei Starovoitov 	return a + (long)b + c + d + (long)e + f;
539faeb2dceSAlexei Starovoitov }
540faeb2dceSAlexei Starovoitov 
541d923021cSYonghong Song struct bpf_fentry_test_t {
542d923021cSYonghong Song 	struct bpf_fentry_test_t *a;
543d923021cSYonghong Song };
544d923021cSYonghong Song 
bpf_fentry_test7(struct bpf_fentry_test_t * arg)545d923021cSYonghong Song int noinline bpf_fentry_test7(struct bpf_fentry_test_t *arg)
546d923021cSYonghong Song {
547e02851adSYonghong Song 	asm volatile ("": "+r"(arg));
548d923021cSYonghong Song 	return (long)arg;
549d923021cSYonghong Song }
550d923021cSYonghong Song 
bpf_fentry_test8(struct bpf_fentry_test_t * arg)551d923021cSYonghong Song int noinline bpf_fentry_test8(struct bpf_fentry_test_t *arg)
552d923021cSYonghong Song {
553d923021cSYonghong Song 	return (long)arg->a;
554d923021cSYonghong Song }
555d923021cSYonghong Song 
bpf_fentry_test9(u32 * a)55675dcef8dSFeng Zhou __bpf_kfunc u32 bpf_fentry_test9(u32 *a)
55775dcef8dSFeng Zhou {
55875dcef8dSFeng Zhou 	return *a;
55975dcef8dSFeng Zhou }
56075dcef8dSFeng Zhou 
bpf_fentry_test_sinfo(struct skb_shared_info * sinfo)5612597a25cSStanislav Fomichev void noinline bpf_fentry_test_sinfo(struct skb_shared_info *sinfo)
5622597a25cSStanislav Fomichev {
5632597a25cSStanislav Fomichev }
5642597a25cSStanislav Fomichev 
bpf_modify_return_test(int a,int * b)565400031e0SDavid Vernet __bpf_kfunc int bpf_modify_return_test(int a, int *b)
5663d08b6f2SKP Singh {
5673d08b6f2SKP Singh 	*b += 1;
5683d08b6f2SKP Singh 	return a + *b;
5693d08b6f2SKP Singh }
5707bd1590dSMartin KaFai Lau 
bpf_modify_return_test2(int a,int * b,short c,int d,void * e,char f,int g)5715e9cf77dSMenglong Dong __bpf_kfunc int bpf_modify_return_test2(int a, int *b, short c, int d,
5725e9cf77dSMenglong Dong 					void *e, char f, int g)
5735e9cf77dSMenglong Dong {
5745e9cf77dSMenglong Dong 	*b += 1;
5755e9cf77dSMenglong Dong 	return a + *b + c + d + (long)e + f + g;
5765e9cf77dSMenglong Dong }
5775e9cf77dSMenglong Dong 
bpf_fentry_shadow_test(int a)578aa3d65deSViktor Malik int noinline bpf_fentry_shadow_test(int a)
579aa3d65deSViktor Malik {
580aa3d65deSViktor Malik 	return a + 1;
581aa3d65deSViktor Malik }
582aa3d65deSViktor Malik 
583792c0a34SKumar Kartikeya Dwivedi struct prog_test_member1 {
584792c0a34SKumar Kartikeya Dwivedi 	int a;
585792c0a34SKumar Kartikeya Dwivedi };
586792c0a34SKumar Kartikeya Dwivedi 
5878218ccb5SKumar Kartikeya Dwivedi struct prog_test_member {
588792c0a34SKumar Kartikeya Dwivedi 	struct prog_test_member1 m;
589792c0a34SKumar Kartikeya Dwivedi 	int c;
5908218ccb5SKumar Kartikeya Dwivedi };
5918218ccb5SKumar Kartikeya Dwivedi 
592c1ff181fSKumar Kartikeya Dwivedi struct prog_test_ref_kfunc {
593c1ff181fSKumar Kartikeya Dwivedi 	int a;
594c1ff181fSKumar Kartikeya Dwivedi 	int b;
5958218ccb5SKumar Kartikeya Dwivedi 	struct prog_test_member memb;
596c1ff181fSKumar Kartikeya Dwivedi 	struct prog_test_ref_kfunc *next;
5975cdccadcSKumar Kartikeya Dwivedi 	refcount_t cnt;
598c1ff181fSKumar Kartikeya Dwivedi };
599c1ff181fSKumar Kartikeya Dwivedi 
bpf_kfunc_call_test_release(struct prog_test_ref_kfunc * p)600400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p)
601c1ff181fSKumar Kartikeya Dwivedi {
6025cdccadcSKumar Kartikeya Dwivedi 	refcount_dec(&p->cnt);
603c1ff181fSKumar Kartikeya Dwivedi }
604c1ff181fSKumar Kartikeya Dwivedi 
bpf_kfunc_call_memb_release(struct prog_test_member * p)605400031e0SDavid Vernet __bpf_kfunc void bpf_kfunc_call_memb_release(struct prog_test_member *p)
6068218ccb5SKumar Kartikeya Dwivedi {
6078218ccb5SKumar Kartikeya Dwivedi }
6088218ccb5SKumar Kartikeya Dwivedi 
609e9ff9d52SJean-Philippe Menil __diag_pop();
6103d08b6f2SKP Singh 
6115b481acaSBenjamin Tissoires BTF_SET8_START(bpf_test_modify_return_ids)
6125b481acaSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_modify_return_test)
6135e9cf77dSMenglong Dong BTF_ID_FLAGS(func, bpf_modify_return_test2)
6145b481acaSBenjamin Tissoires BTF_ID_FLAGS(func, bpf_fentry_test1, KF_SLEEPABLE)
6155b481acaSBenjamin Tissoires BTF_SET8_END(bpf_test_modify_return_ids)
6165b481acaSBenjamin Tissoires 
6175b481acaSBenjamin Tissoires static const struct btf_kfunc_id_set bpf_test_modify_return_set = {
6185b481acaSBenjamin Tissoires 	.owner = THIS_MODULE,
6195b481acaSBenjamin Tissoires 	.set   = &bpf_test_modify_return_ids,
6205b481acaSBenjamin Tissoires };
6213d08b6f2SKP Singh 
622a4703e31SKumar Kartikeya Dwivedi BTF_SET8_START(test_sk_check_kfunc_ids)
BTF_ID_FLAGS(func,bpf_kfunc_call_test_release,KF_RELEASE)623a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_test_release, KF_RELEASE)
624a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, bpf_kfunc_call_memb_release, KF_RELEASE)
625a4703e31SKumar Kartikeya Dwivedi BTF_SET8_END(test_sk_check_kfunc_ids)
62605a945deSKumar Kartikeya Dwivedi 
627be3d72a2SLorenzo Bianconi static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
628be3d72a2SLorenzo Bianconi 			   u32 size, u32 headroom, u32 tailroom)
6291cf1cae9SAlexei Starovoitov {
6301cf1cae9SAlexei Starovoitov 	void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
6311cf1cae9SAlexei Starovoitov 	void *data;
6321cf1cae9SAlexei Starovoitov 
6331cf1cae9SAlexei Starovoitov 	if (size < ETH_HLEN || size > PAGE_SIZE - headroom - tailroom)
6341cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
6351cf1cae9SAlexei Starovoitov 
636d800bad6SJesper Dangaard Brouer 	if (user_size > size)
637d800bad6SJesper Dangaard Brouer 		return ERR_PTR(-EMSGSIZE);
638d800bad6SJesper Dangaard Brouer 
639d3fd203fSBaisong Zhong 	size = SKB_DATA_ALIGN(size);
6401cf1cae9SAlexei Starovoitov 	data = kzalloc(size + headroom + tailroom, GFP_USER);
6411cf1cae9SAlexei Starovoitov 	if (!data)
6421cf1cae9SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
6431cf1cae9SAlexei Starovoitov 
644d800bad6SJesper Dangaard Brouer 	if (copy_from_user(data + headroom, data_in, user_size)) {
6451cf1cae9SAlexei Starovoitov 		kfree(data);
6461cf1cae9SAlexei Starovoitov 		return ERR_PTR(-EFAULT);
6471cf1cae9SAlexei Starovoitov 	}
648da00d2f1SKP Singh 
649da00d2f1SKP Singh 	return data;
650da00d2f1SKP Singh }
651da00d2f1SKP Singh 
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)652da00d2f1SKP Singh int bpf_prog_test_run_tracing(struct bpf_prog *prog,
653da00d2f1SKP Singh 			      const union bpf_attr *kattr,
654da00d2f1SKP Singh 			      union bpf_attr __user *uattr)
655da00d2f1SKP Singh {
656d923021cSYonghong Song 	struct bpf_fentry_test_t arg = {};
6573d08b6f2SKP Singh 	u16 side_effect = 0, ret = 0;
6583d08b6f2SKP Singh 	int b = 2, err = -EFAULT;
6593d08b6f2SKP Singh 	u32 retval = 0;
660da00d2f1SKP Singh 
661b530e9e1SToke Høiland-Jørgensen 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
6621b4d60ecSSong Liu 		return -EINVAL;
6631b4d60ecSSong Liu 
664da00d2f1SKP Singh 	switch (prog->expected_attach_type) {
665da00d2f1SKP Singh 	case BPF_TRACE_FENTRY:
666da00d2f1SKP Singh 	case BPF_TRACE_FEXIT:
667faeb2dceSAlexei Starovoitov 		if (bpf_fentry_test1(1) != 2 ||
668faeb2dceSAlexei Starovoitov 		    bpf_fentry_test2(2, 3) != 5 ||
669faeb2dceSAlexei Starovoitov 		    bpf_fentry_test3(4, 5, 6) != 15 ||
670faeb2dceSAlexei Starovoitov 		    bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
671faeb2dceSAlexei Starovoitov 		    bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
672d923021cSYonghong Song 		    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111 ||
673d923021cSYonghong Song 		    bpf_fentry_test7((struct bpf_fentry_test_t *)0) != 0 ||
67475dcef8dSFeng Zhou 		    bpf_fentry_test8(&arg) != 0 ||
67575dcef8dSFeng Zhou 		    bpf_fentry_test9(&retval) != 0)
676da00d2f1SKP Singh 			goto out;
677da00d2f1SKP Singh 		break;
6783d08b6f2SKP Singh 	case BPF_MODIFY_RETURN:
6793d08b6f2SKP Singh 		ret = bpf_modify_return_test(1, &b);
6803d08b6f2SKP Singh 		if (b != 2)
6815e9cf77dSMenglong Dong 			side_effect++;
6825e9cf77dSMenglong Dong 		b = 2;
6835e9cf77dSMenglong Dong 		ret += bpf_modify_return_test2(1, &b, 3, 4, (void *)5, 6, 7);
6845e9cf77dSMenglong Dong 		if (b != 2)
6855e9cf77dSMenglong Dong 			side_effect++;
6863d08b6f2SKP Singh 		break;
687da00d2f1SKP Singh 	default:
688da00d2f1SKP Singh 		goto out;
689a25ecd9dSColin Ian King 	}
690da00d2f1SKP Singh 
6913d08b6f2SKP Singh 	retval = ((u32)side_effect << 16) | ret;
6923d08b6f2SKP Singh 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
6933d08b6f2SKP Singh 		goto out;
6943d08b6f2SKP Singh 
695da00d2f1SKP Singh 	err = 0;
696da00d2f1SKP Singh out:
697da00d2f1SKP Singh 	trace_bpf_test_finish(&err);
698da00d2f1SKP Singh 	return err;
6991cf1cae9SAlexei Starovoitov }
7001cf1cae9SAlexei Starovoitov 
7011b4d60ecSSong Liu struct bpf_raw_tp_test_run_info {
7021b4d60ecSSong Liu 	struct bpf_prog *prog;
7031b4d60ecSSong Liu 	void *ctx;
7041b4d60ecSSong Liu 	u32 retval;
7051b4d60ecSSong Liu };
7061b4d60ecSSong Liu 
7071b4d60ecSSong Liu static void
__bpf_prog_test_run_raw_tp(void * data)7081b4d60ecSSong Liu __bpf_prog_test_run_raw_tp(void *data)
7091b4d60ecSSong Liu {
7101b4d60ecSSong Liu 	struct bpf_raw_tp_test_run_info *info = data;
711d387805dSJiri Olsa 	struct bpf_trace_run_ctx run_ctx = {};
712d387805dSJiri Olsa 	struct bpf_run_ctx *old_run_ctx;
713d387805dSJiri Olsa 
714d387805dSJiri Olsa 	old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
7151b4d60ecSSong Liu 
7161b4d60ecSSong Liu 	rcu_read_lock();
717fb7dd8bcSAndrii Nakryiko 	info->retval = bpf_prog_run(info->prog, info->ctx);
7181b4d60ecSSong Liu 	rcu_read_unlock();
719d387805dSJiri Olsa 
720d387805dSJiri Olsa 	bpf_reset_run_ctx(old_run_ctx);
7211b4d60ecSSong Liu }
7221b4d60ecSSong Liu 
bpf_prog_test_run_raw_tp(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)7231b4d60ecSSong Liu int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
7241b4d60ecSSong Liu 			     const union bpf_attr *kattr,
7251b4d60ecSSong Liu 			     union bpf_attr __user *uattr)
7261b4d60ecSSong Liu {
7271b4d60ecSSong Liu 	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
7281b4d60ecSSong Liu 	__u32 ctx_size_in = kattr->test.ctx_size_in;
7291b4d60ecSSong Liu 	struct bpf_raw_tp_test_run_info info;
7301b4d60ecSSong Liu 	int cpu = kattr->test.cpu, err = 0;
731963ec27aSSong Liu 	int current_cpu;
7321b4d60ecSSong Liu 
7331b4d60ecSSong Liu 	/* doesn't support data_in/out, ctx_out, duration, or repeat */
7341b4d60ecSSong Liu 	if (kattr->test.data_in || kattr->test.data_out ||
7351b4d60ecSSong Liu 	    kattr->test.ctx_out || kattr->test.duration ||
736b530e9e1SToke Høiland-Jørgensen 	    kattr->test.repeat || kattr->test.batch_size)
7371b4d60ecSSong Liu 		return -EINVAL;
7381b4d60ecSSong Liu 
7397ac6ad05SSong Liu 	if (ctx_size_in < prog->aux->max_ctx_offset ||
7407ac6ad05SSong Liu 	    ctx_size_in > MAX_BPF_FUNC_ARGS * sizeof(u64))
7411b4d60ecSSong Liu 		return -EINVAL;
7421b4d60ecSSong Liu 
7431b4d60ecSSong Liu 	if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 && cpu != 0)
7441b4d60ecSSong Liu 		return -EINVAL;
7451b4d60ecSSong Liu 
7461b4d60ecSSong Liu 	if (ctx_size_in) {
747db5b6a46SQing Wang 		info.ctx = memdup_user(ctx_in, ctx_size_in);
748db5b6a46SQing Wang 		if (IS_ERR(info.ctx))
749db5b6a46SQing Wang 			return PTR_ERR(info.ctx);
7501b4d60ecSSong Liu 	} else {
7511b4d60ecSSong Liu 		info.ctx = NULL;
7521b4d60ecSSong Liu 	}
7531b4d60ecSSong Liu 
7541b4d60ecSSong Liu 	info.prog = prog;
7551b4d60ecSSong Liu 
756963ec27aSSong Liu 	current_cpu = get_cpu();
7571b4d60ecSSong Liu 	if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
758963ec27aSSong Liu 	    cpu == current_cpu) {
7591b4d60ecSSong Liu 		__bpf_prog_test_run_raw_tp(&info);
760963ec27aSSong Liu 	} else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
7611b4d60ecSSong Liu 		/* smp_call_function_single() also checks cpu_online()
7621b4d60ecSSong Liu 		 * after csd_lock(). However, since cpu is from user
7631b4d60ecSSong Liu 		 * space, let's do an extra quick check to filter out
7641b4d60ecSSong Liu 		 * invalid value before smp_call_function_single().
7651b4d60ecSSong Liu 		 */
7661b4d60ecSSong Liu 		err = -ENXIO;
767963ec27aSSong Liu 	} else {
7681b4d60ecSSong Liu 		err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
7691b4d60ecSSong Liu 					       &info, 1);
7701b4d60ecSSong Liu 	}
771963ec27aSSong Liu 	put_cpu();
7721b4d60ecSSong Liu 
773963ec27aSSong Liu 	if (!err &&
774963ec27aSSong Liu 	    copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
7751b4d60ecSSong Liu 		err = -EFAULT;
7761b4d60ecSSong Liu 
7771b4d60ecSSong Liu 	kfree(info.ctx);
7781b4d60ecSSong Liu 	return err;
7791b4d60ecSSong Liu }
7801b4d60ecSSong Liu 
bpf_ctx_init(const union bpf_attr * kattr,u32 max_size)781b0b9395dSStanislav Fomichev static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
782b0b9395dSStanislav Fomichev {
783b0b9395dSStanislav Fomichev 	void __user *data_in = u64_to_user_ptr(kattr->test.ctx_in);
784b0b9395dSStanislav Fomichev 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
785b0b9395dSStanislav Fomichev 	u32 size = kattr->test.ctx_size_in;
786b0b9395dSStanislav Fomichev 	void *data;
787b0b9395dSStanislav Fomichev 	int err;
788b0b9395dSStanislav Fomichev 
789b0b9395dSStanislav Fomichev 	if (!data_in && !data_out)
790b0b9395dSStanislav Fomichev 		return NULL;
791b0b9395dSStanislav Fomichev 
792b0b9395dSStanislav Fomichev 	data = kzalloc(max_size, GFP_USER);
793b0b9395dSStanislav Fomichev 	if (!data)
794b0b9395dSStanislav Fomichev 		return ERR_PTR(-ENOMEM);
795b0b9395dSStanislav Fomichev 
796b0b9395dSStanislav Fomichev 	if (data_in) {
797af2ac3e1SAlexei Starovoitov 		err = bpf_check_uarg_tail_zero(USER_BPFPTR(data_in), max_size, size);
798b0b9395dSStanislav Fomichev 		if (err) {
799b0b9395dSStanislav Fomichev 			kfree(data);
800b0b9395dSStanislav Fomichev 			return ERR_PTR(err);
801b0b9395dSStanislav Fomichev 		}
802b0b9395dSStanislav Fomichev 
803b0b9395dSStanislav Fomichev 		size = min_t(u32, max_size, size);
804b0b9395dSStanislav Fomichev 		if (copy_from_user(data, data_in, size)) {
805b0b9395dSStanislav Fomichev 			kfree(data);
806b0b9395dSStanislav Fomichev 			return ERR_PTR(-EFAULT);
807b0b9395dSStanislav Fomichev 		}
808b0b9395dSStanislav Fomichev 	}
809b0b9395dSStanislav Fomichev 	return data;
810b0b9395dSStanislav Fomichev }
811b0b9395dSStanislav Fomichev 
bpf_ctx_finish(const union bpf_attr * kattr,union bpf_attr __user * uattr,const void * data,u32 size)812b0b9395dSStanislav Fomichev static int bpf_ctx_finish(const union bpf_attr *kattr,
813b0b9395dSStanislav Fomichev 			  union bpf_attr __user *uattr, const void *data,
814b0b9395dSStanislav Fomichev 			  u32 size)
815b0b9395dSStanislav Fomichev {
816b0b9395dSStanislav Fomichev 	void __user *data_out = u64_to_user_ptr(kattr->test.ctx_out);
817b0b9395dSStanislav Fomichev 	int err = -EFAULT;
818b0b9395dSStanislav Fomichev 	u32 copy_size = size;
819b0b9395dSStanislav Fomichev 
820b0b9395dSStanislav Fomichev 	if (!data || !data_out)
821b0b9395dSStanislav Fomichev 		return 0;
822b0b9395dSStanislav Fomichev 
823b0b9395dSStanislav Fomichev 	if (copy_size > kattr->test.ctx_size_out) {
824b0b9395dSStanislav Fomichev 		copy_size = kattr->test.ctx_size_out;
825b0b9395dSStanislav Fomichev 		err = -ENOSPC;
826b0b9395dSStanislav Fomichev 	}
827b0b9395dSStanislav Fomichev 
828b0b9395dSStanislav Fomichev 	if (copy_to_user(data_out, data, copy_size))
829b0b9395dSStanislav Fomichev 		goto out;
830b0b9395dSStanislav Fomichev 	if (copy_to_user(&uattr->test.ctx_size_out, &size, sizeof(size)))
831b0b9395dSStanislav Fomichev 		goto out;
832b0b9395dSStanislav Fomichev 	if (err != -ENOSPC)
833b0b9395dSStanislav Fomichev 		err = 0;
834b0b9395dSStanislav Fomichev out:
835b0b9395dSStanislav Fomichev 	return err;
836b0b9395dSStanislav Fomichev }
837b0b9395dSStanislav Fomichev 
838b0b9395dSStanislav Fomichev /**
839b0b9395dSStanislav Fomichev  * range_is_zero - test whether buffer is initialized
840b0b9395dSStanislav Fomichev  * @buf: buffer to check
841b0b9395dSStanislav Fomichev  * @from: check from this position
842b0b9395dSStanislav Fomichev  * @to: check up until (excluding) this position
843b0b9395dSStanislav Fomichev  *
844b0b9395dSStanislav Fomichev  * This function returns true if the there is a non-zero byte
845b0b9395dSStanislav Fomichev  * in the buf in the range [from,to).
846b0b9395dSStanislav Fomichev  */
range_is_zero(void * buf,size_t from,size_t to)847b0b9395dSStanislav Fomichev static inline bool range_is_zero(void *buf, size_t from, size_t to)
848b0b9395dSStanislav Fomichev {
849b0b9395dSStanislav Fomichev 	return !memchr_inv((u8 *)buf + from, 0, to - from);
850b0b9395dSStanislav Fomichev }
851b0b9395dSStanislav Fomichev 
convert___skb_to_skb(struct sk_buff * skb,struct __sk_buff * __skb)852b0b9395dSStanislav Fomichev static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
853b0b9395dSStanislav Fomichev {
854b0b9395dSStanislav Fomichev 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
855b0b9395dSStanislav Fomichev 
856b0b9395dSStanislav Fomichev 	if (!__skb)
857b0b9395dSStanislav Fomichev 		return 0;
858b0b9395dSStanislav Fomichev 
859b0b9395dSStanislav Fomichev 	/* make sure the fields we don't use are zeroed */
8606de6c1f8SNikita V. Shirokov 	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
8616de6c1f8SNikita V. Shirokov 		return -EINVAL;
8626de6c1f8SNikita V. Shirokov 
8636de6c1f8SNikita V. Shirokov 	/* mark is allowed */
8646de6c1f8SNikita V. Shirokov 
8656de6c1f8SNikita V. Shirokov 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
8666de6c1f8SNikita V. Shirokov 			   offsetof(struct __sk_buff, priority)))
867b0b9395dSStanislav Fomichev 		return -EINVAL;
868b0b9395dSStanislav Fomichev 
869b0b9395dSStanislav Fomichev 	/* priority is allowed */
870b238290bSNeil Spring 	/* ingress_ifindex is allowed */
87121594c44SDmitry Yakunin 	/* ifindex is allowed */
87221594c44SDmitry Yakunin 
87321594c44SDmitry Yakunin 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, ifindex),
874b0b9395dSStanislav Fomichev 			   offsetof(struct __sk_buff, cb)))
875b0b9395dSStanislav Fomichev 		return -EINVAL;
876b0b9395dSStanislav Fomichev 
877b0b9395dSStanislav Fomichev 	/* cb is allowed */
878b0b9395dSStanislav Fomichev 
879b590cb5fSStanislav Fomichev 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, cb),
880ba940948SStanislav Fomichev 			   offsetof(struct __sk_buff, tstamp)))
881ba940948SStanislav Fomichev 		return -EINVAL;
882ba940948SStanislav Fomichev 
883ba940948SStanislav Fomichev 	/* tstamp is allowed */
884850a88ccSStanislav Fomichev 	/* wire_len is allowed */
885850a88ccSStanislav Fomichev 	/* gso_segs is allowed */
886ba940948SStanislav Fomichev 
887850a88ccSStanislav Fomichev 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
888cf62089bSWillem de Bruijn 			   offsetof(struct __sk_buff, gso_size)))
889cf62089bSWillem de Bruijn 		return -EINVAL;
890cf62089bSWillem de Bruijn 
891cf62089bSWillem de Bruijn 	/* gso_size is allowed */
892cf62089bSWillem de Bruijn 
893cf62089bSWillem de Bruijn 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
8943384c7c7SVadim Fedorenko 			   offsetof(struct __sk_buff, hwtstamp)))
8953384c7c7SVadim Fedorenko 		return -EINVAL;
8963384c7c7SVadim Fedorenko 
8973384c7c7SVadim Fedorenko 	/* hwtstamp is allowed */
8983384c7c7SVadim Fedorenko 
8993384c7c7SVadim Fedorenko 	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, hwtstamp),
900b0b9395dSStanislav Fomichev 			   sizeof(struct __sk_buff)))
901b0b9395dSStanislav Fomichev 		return -EINVAL;
902b0b9395dSStanislav Fomichev 
9036de6c1f8SNikita V. Shirokov 	skb->mark = __skb->mark;
904b0b9395dSStanislav Fomichev 	skb->priority = __skb->priority;
905b238290bSNeil Spring 	skb->skb_iif = __skb->ingress_ifindex;
906ba940948SStanislav Fomichev 	skb->tstamp = __skb->tstamp;
907b0b9395dSStanislav Fomichev 	memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
908b0b9395dSStanislav Fomichev 
909850a88ccSStanislav Fomichev 	if (__skb->wire_len == 0) {
910850a88ccSStanislav Fomichev 		cb->pkt_len = skb->len;
911850a88ccSStanislav Fomichev 	} else {
912850a88ccSStanislav Fomichev 		if (__skb->wire_len < skb->len ||
9137c4e983cSAlexander Duyck 		    __skb->wire_len > GSO_LEGACY_MAX_SIZE)
914850a88ccSStanislav Fomichev 			return -EINVAL;
915850a88ccSStanislav Fomichev 		cb->pkt_len = __skb->wire_len;
916850a88ccSStanislav Fomichev 	}
917850a88ccSStanislav Fomichev 
918850a88ccSStanislav Fomichev 	if (__skb->gso_segs > GSO_MAX_SEGS)
919850a88ccSStanislav Fomichev 		return -EINVAL;
920850a88ccSStanislav Fomichev 	skb_shinfo(skb)->gso_segs = __skb->gso_segs;
921cf62089bSWillem de Bruijn 	skb_shinfo(skb)->gso_size = __skb->gso_size;
9223384c7c7SVadim Fedorenko 	skb_shinfo(skb)->hwtstamps.hwtstamp = __skb->hwtstamp;
923850a88ccSStanislav Fomichev 
924b0b9395dSStanislav Fomichev 	return 0;
925b0b9395dSStanislav Fomichev }
926b0b9395dSStanislav Fomichev 
convert_skb_to___skb(struct sk_buff * skb,struct __sk_buff * __skb)927b0b9395dSStanislav Fomichev static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
928b0b9395dSStanislav Fomichev {
929b0b9395dSStanislav Fomichev 	struct qdisc_skb_cb *cb = (struct qdisc_skb_cb *)skb->cb;
930b0b9395dSStanislav Fomichev 
931b0b9395dSStanislav Fomichev 	if (!__skb)
932b0b9395dSStanislav Fomichev 		return;
933b0b9395dSStanislav Fomichev 
9346de6c1f8SNikita V. Shirokov 	__skb->mark = skb->mark;
935b0b9395dSStanislav Fomichev 	__skb->priority = skb->priority;
936b238290bSNeil Spring 	__skb->ingress_ifindex = skb->skb_iif;
93721594c44SDmitry Yakunin 	__skb->ifindex = skb->dev->ifindex;
938ba940948SStanislav Fomichev 	__skb->tstamp = skb->tstamp;
939b0b9395dSStanislav Fomichev 	memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
940850a88ccSStanislav Fomichev 	__skb->wire_len = cb->pkt_len;
941850a88ccSStanislav Fomichev 	__skb->gso_segs = skb_shinfo(skb)->gso_segs;
9423384c7c7SVadim Fedorenko 	__skb->hwtstamp = skb_shinfo(skb)->hwtstamps.hwtstamp;
943b0b9395dSStanislav Fomichev }
944b0b9395dSStanislav Fomichev 
945435b08ecSDaniel Borkmann static struct proto bpf_dummy_proto = {
946435b08ecSDaniel Borkmann 	.name   = "bpf_dummy",
947435b08ecSDaniel Borkmann 	.owner  = THIS_MODULE,
948435b08ecSDaniel Borkmann 	.obj_size = sizeof(struct sock),
949435b08ecSDaniel Borkmann };
950435b08ecSDaniel Borkmann 
bpf_prog_test_run_skb(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)9511cf1cae9SAlexei Starovoitov int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
9521cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
9531cf1cae9SAlexei Starovoitov {
9541cf1cae9SAlexei Starovoitov 	bool is_l2 = false, is_direct_pkt_access = false;
95521594c44SDmitry Yakunin 	struct net *net = current->nsproxy->net_ns;
95621594c44SDmitry Yakunin 	struct net_device *dev = net->loopback_dev;
9571cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
9581cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
959b0b9395dSStanislav Fomichev 	struct __sk_buff *ctx = NULL;
9601cf1cae9SAlexei Starovoitov 	u32 retval, duration;
9616e6fddc7SDaniel Borkmann 	int hh_len = ETH_HLEN;
9621cf1cae9SAlexei Starovoitov 	struct sk_buff *skb;
9632cb494a3SSong Liu 	struct sock *sk;
9641cf1cae9SAlexei Starovoitov 	void *data;
9651cf1cae9SAlexei Starovoitov 	int ret;
9661cf1cae9SAlexei Starovoitov 
967b530e9e1SToke Høiland-Jørgensen 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
9681b4d60ecSSong Liu 		return -EINVAL;
9691b4d60ecSSong Liu 
970be3d72a2SLorenzo Bianconi 	data = bpf_test_init(kattr, kattr->test.data_size_in,
971be3d72a2SLorenzo Bianconi 			     size, NET_SKB_PAD + NET_IP_ALIGN,
9721cf1cae9SAlexei Starovoitov 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
9731cf1cae9SAlexei Starovoitov 	if (IS_ERR(data))
9741cf1cae9SAlexei Starovoitov 		return PTR_ERR(data);
9751cf1cae9SAlexei Starovoitov 
976b0b9395dSStanislav Fomichev 	ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
977b0b9395dSStanislav Fomichev 	if (IS_ERR(ctx)) {
978b0b9395dSStanislav Fomichev 		kfree(data);
979b0b9395dSStanislav Fomichev 		return PTR_ERR(ctx);
980b0b9395dSStanislav Fomichev 	}
981b0b9395dSStanislav Fomichev 
9821cf1cae9SAlexei Starovoitov 	switch (prog->type) {
9831cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_CLS:
9841cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_ACT:
9851cf1cae9SAlexei Starovoitov 		is_l2 = true;
986df561f66SGustavo A. R. Silva 		fallthrough;
9871cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_IN:
9881cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_OUT:
9891cf1cae9SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_XMIT:
9901cf1cae9SAlexei Starovoitov 		is_direct_pkt_access = true;
9911cf1cae9SAlexei Starovoitov 		break;
9921cf1cae9SAlexei Starovoitov 	default:
9931cf1cae9SAlexei Starovoitov 		break;
9941cf1cae9SAlexei Starovoitov 	}
9951cf1cae9SAlexei Starovoitov 
996435b08ecSDaniel Borkmann 	sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
9972cb494a3SSong Liu 	if (!sk) {
9981cf1cae9SAlexei Starovoitov 		kfree(data);
999b0b9395dSStanislav Fomichev 		kfree(ctx);
10001cf1cae9SAlexei Starovoitov 		return -ENOMEM;
10011cf1cae9SAlexei Starovoitov 	}
10022cb494a3SSong Liu 	sock_init_data(NULL, sk);
10032cb494a3SSong Liu 
1004ce098da1SKees Cook 	skb = slab_build_skb(data);
10052cb494a3SSong Liu 	if (!skb) {
10062cb494a3SSong Liu 		kfree(data);
1007b0b9395dSStanislav Fomichev 		kfree(ctx);
1008435b08ecSDaniel Borkmann 		sk_free(sk);
10092cb494a3SSong Liu 		return -ENOMEM;
10102cb494a3SSong Liu 	}
10112cb494a3SSong Liu 	skb->sk = sk;
10121cf1cae9SAlexei Starovoitov 
1013586f8525SDavid Miller 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
10141cf1cae9SAlexei Starovoitov 	__skb_put(skb, size);
101521594c44SDmitry Yakunin 	if (ctx && ctx->ifindex > 1) {
101621594c44SDmitry Yakunin 		dev = dev_get_by_index(net, ctx->ifindex);
101721594c44SDmitry Yakunin 		if (!dev) {
101821594c44SDmitry Yakunin 			ret = -ENODEV;
101921594c44SDmitry Yakunin 			goto out;
102021594c44SDmitry Yakunin 		}
102121594c44SDmitry Yakunin 	}
102221594c44SDmitry Yakunin 	skb->protocol = eth_type_trans(skb, dev);
10231cf1cae9SAlexei Starovoitov 	skb_reset_network_header(skb);
10241cf1cae9SAlexei Starovoitov 
1025fa5cb548SDmitry Yakunin 	switch (skb->protocol) {
1026fa5cb548SDmitry Yakunin 	case htons(ETH_P_IP):
1027fa5cb548SDmitry Yakunin 		sk->sk_family = AF_INET;
1028fa5cb548SDmitry Yakunin 		if (sizeof(struct iphdr) <= skb_headlen(skb)) {
1029fa5cb548SDmitry Yakunin 			sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
1030fa5cb548SDmitry Yakunin 			sk->sk_daddr = ip_hdr(skb)->daddr;
1031fa5cb548SDmitry Yakunin 		}
1032fa5cb548SDmitry Yakunin 		break;
1033fa5cb548SDmitry Yakunin #if IS_ENABLED(CONFIG_IPV6)
1034fa5cb548SDmitry Yakunin 	case htons(ETH_P_IPV6):
1035fa5cb548SDmitry Yakunin 		sk->sk_family = AF_INET6;
1036fa5cb548SDmitry Yakunin 		if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
1037fa5cb548SDmitry Yakunin 			sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
1038fa5cb548SDmitry Yakunin 			sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
1039fa5cb548SDmitry Yakunin 		}
1040fa5cb548SDmitry Yakunin 		break;
1041fa5cb548SDmitry Yakunin #endif
1042fa5cb548SDmitry Yakunin 	default:
1043fa5cb548SDmitry Yakunin 		break;
1044fa5cb548SDmitry Yakunin 	}
1045fa5cb548SDmitry Yakunin 
10461cf1cae9SAlexei Starovoitov 	if (is_l2)
10476e6fddc7SDaniel Borkmann 		__skb_push(skb, hh_len);
10481cf1cae9SAlexei Starovoitov 	if (is_direct_pkt_access)
10496aaae2b6SDaniel Borkmann 		bpf_compute_data_pointers(skb);
1050b0b9395dSStanislav Fomichev 	ret = convert___skb_to_skb(skb, ctx);
1051b0b9395dSStanislav Fomichev 	if (ret)
1052b0b9395dSStanislav Fomichev 		goto out;
1053f23c4b39SBjörn Töpel 	ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
1054b0b9395dSStanislav Fomichev 	if (ret)
1055b0b9395dSStanislav Fomichev 		goto out;
10566e6fddc7SDaniel Borkmann 	if (!is_l2) {
10576e6fddc7SDaniel Borkmann 		if (skb_headroom(skb) < hh_len) {
10586e6fddc7SDaniel Borkmann 			int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
10596e6fddc7SDaniel Borkmann 
10606e6fddc7SDaniel Borkmann 			if (pskb_expand_head(skb, nhead, 0, GFP_USER)) {
1061b0b9395dSStanislav Fomichev 				ret = -ENOMEM;
1062b0b9395dSStanislav Fomichev 				goto out;
10636e6fddc7SDaniel Borkmann 			}
10646e6fddc7SDaniel Borkmann 		}
10656e6fddc7SDaniel Borkmann 		memset(__skb_push(skb, hh_len), 0, hh_len);
10666e6fddc7SDaniel Borkmann 	}
1067b0b9395dSStanislav Fomichev 	convert_skb_to___skb(skb, ctx);
10686e6fddc7SDaniel Borkmann 
10691cf1cae9SAlexei Starovoitov 	size = skb->len;
10701cf1cae9SAlexei Starovoitov 	/* bpf program can never convert linear skb to non-linear */
10711cf1cae9SAlexei Starovoitov 	if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
10721cf1cae9SAlexei Starovoitov 		size = skb_headlen(skb);
10737855e0dbSLorenzo Bianconi 	ret = bpf_test_finish(kattr, uattr, skb->data, NULL, size, retval,
10747855e0dbSLorenzo Bianconi 			      duration);
1075b0b9395dSStanislav Fomichev 	if (!ret)
1076b0b9395dSStanislav Fomichev 		ret = bpf_ctx_finish(kattr, uattr, ctx,
1077b0b9395dSStanislav Fomichev 				     sizeof(struct __sk_buff));
1078b0b9395dSStanislav Fomichev out:
107921594c44SDmitry Yakunin 	if (dev && dev != net->loopback_dev)
108021594c44SDmitry Yakunin 		dev_put(dev);
10811cf1cae9SAlexei Starovoitov 	kfree_skb(skb);
1082435b08ecSDaniel Borkmann 	sk_free(sk);
1083b0b9395dSStanislav Fomichev 	kfree(ctx);
10841cf1cae9SAlexei Starovoitov 	return ret;
10851cf1cae9SAlexei Starovoitov }
10861cf1cae9SAlexei Starovoitov 
xdp_convert_md_to_buff(struct xdp_md * xdp_md,struct xdp_buff * xdp)108747316f4aSZvi Effron static int xdp_convert_md_to_buff(struct xdp_md *xdp_md, struct xdp_buff *xdp)
108847316f4aSZvi Effron {
1089ec94670fSZvi Effron 	unsigned int ingress_ifindex, rx_queue_index;
1090ec94670fSZvi Effron 	struct netdev_rx_queue *rxqueue;
1091ec94670fSZvi Effron 	struct net_device *device;
1092ec94670fSZvi Effron 
109347316f4aSZvi Effron 	if (!xdp_md)
109447316f4aSZvi Effron 		return 0;
109547316f4aSZvi Effron 
109647316f4aSZvi Effron 	if (xdp_md->egress_ifindex != 0)
109747316f4aSZvi Effron 		return -EINVAL;
109847316f4aSZvi Effron 
1099ec94670fSZvi Effron 	ingress_ifindex = xdp_md->ingress_ifindex;
1100ec94670fSZvi Effron 	rx_queue_index = xdp_md->rx_queue_index;
1101ec94670fSZvi Effron 
1102ec94670fSZvi Effron 	if (!ingress_ifindex && rx_queue_index)
110347316f4aSZvi Effron 		return -EINVAL;
110447316f4aSZvi Effron 
1105ec94670fSZvi Effron 	if (ingress_ifindex) {
1106ec94670fSZvi Effron 		device = dev_get_by_index(current->nsproxy->net_ns,
1107ec94670fSZvi Effron 					  ingress_ifindex);
1108ec94670fSZvi Effron 		if (!device)
1109ec94670fSZvi Effron 			return -ENODEV;
111047316f4aSZvi Effron 
1111ec94670fSZvi Effron 		if (rx_queue_index >= device->real_num_rx_queues)
1112ec94670fSZvi Effron 			goto free_dev;
1113ec94670fSZvi Effron 
1114ec94670fSZvi Effron 		rxqueue = __netif_get_rx_queue(device, rx_queue_index);
1115ec94670fSZvi Effron 
1116ec94670fSZvi Effron 		if (!xdp_rxq_info_is_reg(&rxqueue->xdp_rxq))
1117ec94670fSZvi Effron 			goto free_dev;
1118ec94670fSZvi Effron 
1119ec94670fSZvi Effron 		xdp->rxq = &rxqueue->xdp_rxq;
1120ec94670fSZvi Effron 		/* The device is now tracked in the xdp->rxq for later
1121ec94670fSZvi Effron 		 * dev_put()
1122ec94670fSZvi Effron 		 */
1123ec94670fSZvi Effron 	}
1124ec94670fSZvi Effron 
1125ec94670fSZvi Effron 	xdp->data = xdp->data_meta + xdp_md->data;
112647316f4aSZvi Effron 	return 0;
1127ec94670fSZvi Effron 
1128ec94670fSZvi Effron free_dev:
1129ec94670fSZvi Effron 	dev_put(device);
1130ec94670fSZvi Effron 	return -EINVAL;
1131ec94670fSZvi Effron }
1132ec94670fSZvi Effron 
xdp_convert_buff_to_md(struct xdp_buff * xdp,struct xdp_md * xdp_md)1133ec94670fSZvi Effron static void xdp_convert_buff_to_md(struct xdp_buff *xdp, struct xdp_md *xdp_md)
1134ec94670fSZvi Effron {
1135ec94670fSZvi Effron 	if (!xdp_md)
1136ec94670fSZvi Effron 		return;
1137ec94670fSZvi Effron 
1138ec94670fSZvi Effron 	xdp_md->data = xdp->data - xdp->data_meta;
1139ec94670fSZvi Effron 	xdp_md->data_end = xdp->data_end - xdp->data_meta;
1140ec94670fSZvi Effron 
1141ec94670fSZvi Effron 	if (xdp_md->ingress_ifindex)
1142ec94670fSZvi Effron 		dev_put(xdp->rxq->dev);
114347316f4aSZvi Effron }
114447316f4aSZvi Effron 
bpf_prog_test_run_xdp(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)11451cf1cae9SAlexei Starovoitov int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
11461cf1cae9SAlexei Starovoitov 			  union bpf_attr __user *uattr)
11471cf1cae9SAlexei Starovoitov {
1148b530e9e1SToke Høiland-Jørgensen 	bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
1149bc56c919SJesper Dangaard Brouer 	u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1150b530e9e1SToke Høiland-Jørgensen 	u32 batch_size = kattr->test.batch_size;
1151eecbfd97SToke Høiland-Jørgensen 	u32 retval = 0, duration, max_data_sz;
11521cf1cae9SAlexei Starovoitov 	u32 size = kattr->test.data_size_in;
11531c194998SLorenzo Bianconi 	u32 headroom = XDP_PACKET_HEADROOM;
11541cf1cae9SAlexei Starovoitov 	u32 repeat = kattr->test.repeat;
115565073a67SDaniel Borkmann 	struct netdev_rx_queue *rxqueue;
11561c194998SLorenzo Bianconi 	struct skb_shared_info *sinfo;
11571cf1cae9SAlexei Starovoitov 	struct xdp_buff xdp = {};
11581c194998SLorenzo Bianconi 	int i, ret = -EINVAL;
115947316f4aSZvi Effron 	struct xdp_md *ctx;
11601cf1cae9SAlexei Starovoitov 	void *data;
11611cf1cae9SAlexei Starovoitov 
11625e21bb4eSXuan Zhuo 	if (prog->expected_attach_type == BPF_XDP_DEVMAP ||
11635e21bb4eSXuan Zhuo 	    prog->expected_attach_type == BPF_XDP_CPUMAP)
11645e21bb4eSXuan Zhuo 		return -EINVAL;
11656d4eb36dSAndrii Nakryiko 
1166b530e9e1SToke Høiland-Jørgensen 	if (kattr->test.flags & ~BPF_F_TEST_XDP_LIVE_FRAMES)
1167b530e9e1SToke Høiland-Jørgensen 		return -EINVAL;
1168b530e9e1SToke Høiland-Jørgensen 
11693d76a4d3SStanislav Fomichev 	if (bpf_prog_is_dev_bound(prog->aux))
11703d76a4d3SStanislav Fomichev 		return -EINVAL;
11713d76a4d3SStanislav Fomichev 
1172b530e9e1SToke Høiland-Jørgensen 	if (do_live) {
1173b530e9e1SToke Høiland-Jørgensen 		if (!batch_size)
1174b530e9e1SToke Høiland-Jørgensen 			batch_size = NAPI_POLL_WEIGHT;
1175b530e9e1SToke Høiland-Jørgensen 		else if (batch_size > TEST_XDP_MAX_BATCH)
1176b530e9e1SToke Høiland-Jørgensen 			return -E2BIG;
1177b6f1f780SToke Høiland-Jørgensen 
1178b6f1f780SToke Høiland-Jørgensen 		headroom += sizeof(struct xdp_page_head);
1179b530e9e1SToke Høiland-Jørgensen 	} else if (batch_size) {
1180b530e9e1SToke Høiland-Jørgensen 		return -EINVAL;
1181b530e9e1SToke Høiland-Jørgensen 	}
1182b530e9e1SToke Høiland-Jørgensen 
118347316f4aSZvi Effron 	ctx = bpf_ctx_init(kattr, sizeof(struct xdp_md));
118447316f4aSZvi Effron 	if (IS_ERR(ctx))
118547316f4aSZvi Effron 		return PTR_ERR(ctx);
118647316f4aSZvi Effron 
118747316f4aSZvi Effron 	if (ctx) {
118847316f4aSZvi Effron 		/* There can't be user provided data before the meta data */
118947316f4aSZvi Effron 		if (ctx->data_meta || ctx->data_end != size ||
119047316f4aSZvi Effron 		    ctx->data > ctx->data_end ||
1191b530e9e1SToke Høiland-Jørgensen 		    unlikely(xdp_metalen_invalid(ctx->data)) ||
1192b530e9e1SToke Høiland-Jørgensen 		    (do_live && (kattr->test.data_out || kattr->test.ctx_out)))
119347316f4aSZvi Effron 			goto free_ctx;
119447316f4aSZvi Effron 		/* Meta data is allocated from the headroom */
119547316f4aSZvi Effron 		headroom -= ctx->data;
119647316f4aSZvi Effron 	}
1197947e8b59SStanislav Fomichev 
1198bc56c919SJesper Dangaard Brouer 	max_data_sz = 4096 - headroom - tailroom;
1199b530e9e1SToke Høiland-Jørgensen 	if (size > max_data_sz) {
1200b530e9e1SToke Høiland-Jørgensen 		/* disallow live data mode for jumbo frames */
1201b530e9e1SToke Høiland-Jørgensen 		if (do_live)
1202b530e9e1SToke Høiland-Jørgensen 			goto free_ctx;
1203b530e9e1SToke Høiland-Jørgensen 		size = max_data_sz;
1204b530e9e1SToke Høiland-Jørgensen 	}
1205bc56c919SJesper Dangaard Brouer 
12061c194998SLorenzo Bianconi 	data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
120747316f4aSZvi Effron 	if (IS_ERR(data)) {
120847316f4aSZvi Effron 		ret = PTR_ERR(data);
120947316f4aSZvi Effron 		goto free_ctx;
121047316f4aSZvi Effron 	}
12111cf1cae9SAlexei Starovoitov 
121265073a67SDaniel Borkmann 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
12131c194998SLorenzo Bianconi 	rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
12141c194998SLorenzo Bianconi 	xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
1215be9df4afSLorenzo Bianconi 	xdp_prepare_buff(&xdp, data, headroom, size, true);
12161c194998SLorenzo Bianconi 	sinfo = xdp_get_shared_info_from_buff(&xdp);
1217be9df4afSLorenzo Bianconi 
121847316f4aSZvi Effron 	ret = xdp_convert_md_to_buff(ctx, &xdp);
121947316f4aSZvi Effron 	if (ret)
122047316f4aSZvi Effron 		goto free_data;
122147316f4aSZvi Effron 
12221c194998SLorenzo Bianconi 	if (unlikely(kattr->test.data_size_in > size)) {
12231c194998SLorenzo Bianconi 		void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
12241c194998SLorenzo Bianconi 
12251c194998SLorenzo Bianconi 		while (size < kattr->test.data_size_in) {
12261c194998SLorenzo Bianconi 			struct page *page;
12271c194998SLorenzo Bianconi 			skb_frag_t *frag;
12289d63b59dSStanislav Fomichev 			u32 data_len;
12291c194998SLorenzo Bianconi 
1230a6763080SLorenzo Bianconi 			if (sinfo->nr_frags == MAX_SKB_FRAGS) {
1231a6763080SLorenzo Bianconi 				ret = -ENOMEM;
1232a6763080SLorenzo Bianconi 				goto out;
1233a6763080SLorenzo Bianconi 			}
1234a6763080SLorenzo Bianconi 
12351c194998SLorenzo Bianconi 			page = alloc_page(GFP_KERNEL);
12361c194998SLorenzo Bianconi 			if (!page) {
12371c194998SLorenzo Bianconi 				ret = -ENOMEM;
12381c194998SLorenzo Bianconi 				goto out;
12391c194998SLorenzo Bianconi 			}
12401c194998SLorenzo Bianconi 
12411c194998SLorenzo Bianconi 			frag = &sinfo->frags[sinfo->nr_frags++];
12421c194998SLorenzo Bianconi 
12439d63b59dSStanislav Fomichev 			data_len = min_t(u32, kattr->test.data_size_in - size,
12441c194998SLorenzo Bianconi 					 PAGE_SIZE);
1245b51f4113SYunsheng Lin 			skb_frag_fill_page_desc(frag, page, 0, data_len);
12461c194998SLorenzo Bianconi 
12471c194998SLorenzo Bianconi 			if (copy_from_user(page_address(page), data_in + size,
12481c194998SLorenzo Bianconi 					   data_len)) {
12491c194998SLorenzo Bianconi 				ret = -EFAULT;
12501c194998SLorenzo Bianconi 				goto out;
12511c194998SLorenzo Bianconi 			}
12521c194998SLorenzo Bianconi 			sinfo->xdp_frags_size += data_len;
12531c194998SLorenzo Bianconi 			size += data_len;
12541c194998SLorenzo Bianconi 		}
12551c194998SLorenzo Bianconi 		xdp_buff_set_frags_flag(&xdp);
12561c194998SLorenzo Bianconi 	}
12571c194998SLorenzo Bianconi 
1258de21d8bfSLorenz Bauer 	if (repeat > 1)
1259f23c4b39SBjörn Töpel 		bpf_prog_change_xdp(NULL, prog);
12601c194998SLorenzo Bianconi 
1261b530e9e1SToke Høiland-Jørgensen 	if (do_live)
1262b530e9e1SToke Høiland-Jørgensen 		ret = bpf_test_run_xdp_live(prog, &xdp, repeat, batch_size, &duration);
1263b530e9e1SToke Høiland-Jørgensen 	else
1264f23c4b39SBjörn Töpel 		ret = bpf_test_run(prog, &xdp, repeat, &retval, &duration, true);
1265ec94670fSZvi Effron 	/* We convert the xdp_buff back to an xdp_md before checking the return
1266ec94670fSZvi Effron 	 * code so the reference count of any held netdevice will be decremented
1267ec94670fSZvi Effron 	 * even if the test run failed.
1268ec94670fSZvi Effron 	 */
1269ec94670fSZvi Effron 	xdp_convert_buff_to_md(&xdp, ctx);
1270dcb40590SRoman Gushchin 	if (ret)
1271dcb40590SRoman Gushchin 		goto out;
127247316f4aSZvi Effron 
12731c194998SLorenzo Bianconi 	size = xdp.data_end - xdp.data_meta + sinfo->xdp_frags_size;
12747855e0dbSLorenzo Bianconi 	ret = bpf_test_finish(kattr, uattr, xdp.data_meta, sinfo, size,
12757855e0dbSLorenzo Bianconi 			      retval, duration);
127647316f4aSZvi Effron 	if (!ret)
127747316f4aSZvi Effron 		ret = bpf_ctx_finish(kattr, uattr, ctx,
127847316f4aSZvi Effron 				     sizeof(struct xdp_md));
127947316f4aSZvi Effron 
1280dcb40590SRoman Gushchin out:
1281de21d8bfSLorenz Bauer 	if (repeat > 1)
1282f23c4b39SBjörn Töpel 		bpf_prog_change_xdp(prog, NULL);
128347316f4aSZvi Effron free_data:
12841c194998SLorenzo Bianconi 	for (i = 0; i < sinfo->nr_frags; i++)
12851c194998SLorenzo Bianconi 		__free_page(skb_frag_page(&sinfo->frags[i]));
12861cf1cae9SAlexei Starovoitov 	kfree(data);
128747316f4aSZvi Effron free_ctx:
128847316f4aSZvi Effron 	kfree(ctx);
12891cf1cae9SAlexei Starovoitov 	return ret;
12901cf1cae9SAlexei Starovoitov }
1291b7a1848eSStanislav Fomichev 
verify_user_bpf_flow_keys(struct bpf_flow_keys * ctx)1292b2ca4e1cSStanislav Fomichev static int verify_user_bpf_flow_keys(struct bpf_flow_keys *ctx)
1293b2ca4e1cSStanislav Fomichev {
1294b2ca4e1cSStanislav Fomichev 	/* make sure the fields we don't use are zeroed */
1295b2ca4e1cSStanislav Fomichev 	if (!range_is_zero(ctx, 0, offsetof(struct bpf_flow_keys, flags)))
1296b2ca4e1cSStanislav Fomichev 		return -EINVAL;
1297b2ca4e1cSStanislav Fomichev 
1298b2ca4e1cSStanislav Fomichev 	/* flags is allowed */
1299b2ca4e1cSStanislav Fomichev 
1300b590cb5fSStanislav Fomichev 	if (!range_is_zero(ctx, offsetofend(struct bpf_flow_keys, flags),
1301b2ca4e1cSStanislav Fomichev 			   sizeof(struct bpf_flow_keys)))
1302b2ca4e1cSStanislav Fomichev 		return -EINVAL;
1303b2ca4e1cSStanislav Fomichev 
1304b2ca4e1cSStanislav Fomichev 	return 0;
1305b2ca4e1cSStanislav Fomichev }
1306b2ca4e1cSStanislav Fomichev 
bpf_prog_test_run_flow_dissector(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)1307b7a1848eSStanislav Fomichev int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
1308b7a1848eSStanislav Fomichev 				     const union bpf_attr *kattr,
1309b7a1848eSStanislav Fomichev 				     union bpf_attr __user *uattr)
1310b7a1848eSStanislav Fomichev {
1311607b9cc9SLorenz Bauer 	struct bpf_test_timer t = { NO_PREEMPT };
1312b7a1848eSStanislav Fomichev 	u32 size = kattr->test.data_size_in;
13137b8a1304SStanislav Fomichev 	struct bpf_flow_dissector ctx = {};
1314b7a1848eSStanislav Fomichev 	u32 repeat = kattr->test.repeat;
1315b2ca4e1cSStanislav Fomichev 	struct bpf_flow_keys *user_ctx;
1316b7a1848eSStanislav Fomichev 	struct bpf_flow_keys flow_keys;
13177b8a1304SStanislav Fomichev 	const struct ethhdr *eth;
1318b2ca4e1cSStanislav Fomichev 	unsigned int flags = 0;
1319b7a1848eSStanislav Fomichev 	u32 retval, duration;
1320b7a1848eSStanislav Fomichev 	void *data;
1321b7a1848eSStanislav Fomichev 	int ret;
1322b7a1848eSStanislav Fomichev 
1323b530e9e1SToke Høiland-Jørgensen 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
13241b4d60ecSSong Liu 		return -EINVAL;
13251b4d60ecSSong Liu 
13267b8a1304SStanislav Fomichev 	if (size < ETH_HLEN)
13277b8a1304SStanislav Fomichev 		return -EINVAL;
13287b8a1304SStanislav Fomichev 
1329be3d72a2SLorenzo Bianconi 	data = bpf_test_init(kattr, kattr->test.data_size_in, size, 0, 0);
1330b7a1848eSStanislav Fomichev 	if (IS_ERR(data))
1331b7a1848eSStanislav Fomichev 		return PTR_ERR(data);
1332b7a1848eSStanislav Fomichev 
13337b8a1304SStanislav Fomichev 	eth = (struct ethhdr *)data;
1334b7a1848eSStanislav Fomichev 
1335b7a1848eSStanislav Fomichev 	if (!repeat)
1336b7a1848eSStanislav Fomichev 		repeat = 1;
1337b7a1848eSStanislav Fomichev 
1338b2ca4e1cSStanislav Fomichev 	user_ctx = bpf_ctx_init(kattr, sizeof(struct bpf_flow_keys));
1339b2ca4e1cSStanislav Fomichev 	if (IS_ERR(user_ctx)) {
1340b2ca4e1cSStanislav Fomichev 		kfree(data);
1341b2ca4e1cSStanislav Fomichev 		return PTR_ERR(user_ctx);
1342b2ca4e1cSStanislav Fomichev 	}
1343b2ca4e1cSStanislav Fomichev 	if (user_ctx) {
1344b2ca4e1cSStanislav Fomichev 		ret = verify_user_bpf_flow_keys(user_ctx);
1345b2ca4e1cSStanislav Fomichev 		if (ret)
1346b2ca4e1cSStanislav Fomichev 			goto out;
1347b2ca4e1cSStanislav Fomichev 		flags = user_ctx->flags;
1348b2ca4e1cSStanislav Fomichev 	}
1349b2ca4e1cSStanislav Fomichev 
13507b8a1304SStanislav Fomichev 	ctx.flow_keys = &flow_keys;
13517b8a1304SStanislav Fomichev 	ctx.data = data;
13527b8a1304SStanislav Fomichev 	ctx.data_end = (__u8 *)data + size;
13537b8a1304SStanislav Fomichev 
1354607b9cc9SLorenz Bauer 	bpf_test_timer_enter(&t);
1355607b9cc9SLorenz Bauer 	do {
13567b8a1304SStanislav Fomichev 		retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN,
1357b2ca4e1cSStanislav Fomichev 					  size, flags);
1358b530e9e1SToke Høiland-Jørgensen 	} while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
1359607b9cc9SLorenz Bauer 	bpf_test_timer_leave(&t);
13607b8a1304SStanislav Fomichev 
1361607b9cc9SLorenz Bauer 	if (ret < 0)
1362a439184dSStanislav Fomichev 		goto out;
1363b7a1848eSStanislav Fomichev 
13647855e0dbSLorenzo Bianconi 	ret = bpf_test_finish(kattr, uattr, &flow_keys, NULL,
13657855e0dbSLorenzo Bianconi 			      sizeof(flow_keys), retval, duration);
1366b2ca4e1cSStanislav Fomichev 	if (!ret)
1367b2ca4e1cSStanislav Fomichev 		ret = bpf_ctx_finish(kattr, uattr, user_ctx,
1368b2ca4e1cSStanislav Fomichev 				     sizeof(struct bpf_flow_keys));
1369b7a1848eSStanislav Fomichev 
1370a439184dSStanislav Fomichev out:
1371b2ca4e1cSStanislav Fomichev 	kfree(user_ctx);
13727b8a1304SStanislav Fomichev 	kfree(data);
1373b7a1848eSStanislav Fomichev 	return ret;
1374b7a1848eSStanislav Fomichev }
13757c32e8f8SLorenz Bauer 
bpf_prog_test_run_sk_lookup(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)13767c32e8f8SLorenz Bauer int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, const union bpf_attr *kattr,
13777c32e8f8SLorenz Bauer 				union bpf_attr __user *uattr)
13787c32e8f8SLorenz Bauer {
13797c32e8f8SLorenz Bauer 	struct bpf_test_timer t = { NO_PREEMPT };
13807c32e8f8SLorenz Bauer 	struct bpf_prog_array *progs = NULL;
13817c32e8f8SLorenz Bauer 	struct bpf_sk_lookup_kern ctx = {};
13827c32e8f8SLorenz Bauer 	u32 repeat = kattr->test.repeat;
13837c32e8f8SLorenz Bauer 	struct bpf_sk_lookup *user_ctx;
13847c32e8f8SLorenz Bauer 	u32 retval, duration;
13857c32e8f8SLorenz Bauer 	int ret = -EINVAL;
13867c32e8f8SLorenz Bauer 
1387b530e9e1SToke Høiland-Jørgensen 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
13887c32e8f8SLorenz Bauer 		return -EINVAL;
13897c32e8f8SLorenz Bauer 
13907c32e8f8SLorenz Bauer 	if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out ||
13917c32e8f8SLorenz Bauer 	    kattr->test.data_size_out)
13927c32e8f8SLorenz Bauer 		return -EINVAL;
13937c32e8f8SLorenz Bauer 
13947c32e8f8SLorenz Bauer 	if (!repeat)
13957c32e8f8SLorenz Bauer 		repeat = 1;
13967c32e8f8SLorenz Bauer 
13977c32e8f8SLorenz Bauer 	user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx));
13987c32e8f8SLorenz Bauer 	if (IS_ERR(user_ctx))
13997c32e8f8SLorenz Bauer 		return PTR_ERR(user_ctx);
14007c32e8f8SLorenz Bauer 
14017c32e8f8SLorenz Bauer 	if (!user_ctx)
14027c32e8f8SLorenz Bauer 		return -EINVAL;
14037c32e8f8SLorenz Bauer 
14047c32e8f8SLorenz Bauer 	if (user_ctx->sk)
14057c32e8f8SLorenz Bauer 		goto out;
14067c32e8f8SLorenz Bauer 
14077c32e8f8SLorenz Bauer 	if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx)))
14087c32e8f8SLorenz Bauer 		goto out;
14097c32e8f8SLorenz Bauer 
14109a69e2b3SJakub Sitnicki 	if (user_ctx->local_port > U16_MAX) {
14117c32e8f8SLorenz Bauer 		ret = -ERANGE;
14127c32e8f8SLorenz Bauer 		goto out;
14137c32e8f8SLorenz Bauer 	}
14147c32e8f8SLorenz Bauer 
14157c32e8f8SLorenz Bauer 	ctx.family = (u16)user_ctx->family;
14167c32e8f8SLorenz Bauer 	ctx.protocol = (u16)user_ctx->protocol;
14177c32e8f8SLorenz Bauer 	ctx.dport = (u16)user_ctx->local_port;
14189a69e2b3SJakub Sitnicki 	ctx.sport = user_ctx->remote_port;
14197c32e8f8SLorenz Bauer 
14207c32e8f8SLorenz Bauer 	switch (ctx.family) {
14217c32e8f8SLorenz Bauer 	case AF_INET:
14227c32e8f8SLorenz Bauer 		ctx.v4.daddr = (__force __be32)user_ctx->local_ip4;
14237c32e8f8SLorenz Bauer 		ctx.v4.saddr = (__force __be32)user_ctx->remote_ip4;
14247c32e8f8SLorenz Bauer 		break;
14257c32e8f8SLorenz Bauer 
14267c32e8f8SLorenz Bauer #if IS_ENABLED(CONFIG_IPV6)
14277c32e8f8SLorenz Bauer 	case AF_INET6:
14287c32e8f8SLorenz Bauer 		ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6;
14297c32e8f8SLorenz Bauer 		ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6;
14307c32e8f8SLorenz Bauer 		break;
14317c32e8f8SLorenz Bauer #endif
14327c32e8f8SLorenz Bauer 
14337c32e8f8SLorenz Bauer 	default:
14347c32e8f8SLorenz Bauer 		ret = -EAFNOSUPPORT;
14357c32e8f8SLorenz Bauer 		goto out;
14367c32e8f8SLorenz Bauer 	}
14377c32e8f8SLorenz Bauer 
14387c32e8f8SLorenz Bauer 	progs = bpf_prog_array_alloc(1, GFP_KERNEL);
14397c32e8f8SLorenz Bauer 	if (!progs) {
14407c32e8f8SLorenz Bauer 		ret = -ENOMEM;
14417c32e8f8SLorenz Bauer 		goto out;
14427c32e8f8SLorenz Bauer 	}
14437c32e8f8SLorenz Bauer 
14447c32e8f8SLorenz Bauer 	progs->items[0].prog = prog;
14457c32e8f8SLorenz Bauer 
14467c32e8f8SLorenz Bauer 	bpf_test_timer_enter(&t);
14477c32e8f8SLorenz Bauer 	do {
14487c32e8f8SLorenz Bauer 		ctx.selected_sk = NULL;
1449fb7dd8bcSAndrii Nakryiko 		retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, bpf_prog_run);
1450b530e9e1SToke Høiland-Jørgensen 	} while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration));
14517c32e8f8SLorenz Bauer 	bpf_test_timer_leave(&t);
14527c32e8f8SLorenz Bauer 
14537c32e8f8SLorenz Bauer 	if (ret < 0)
14547c32e8f8SLorenz Bauer 		goto out;
14557c32e8f8SLorenz Bauer 
14567c32e8f8SLorenz Bauer 	user_ctx->cookie = 0;
14577c32e8f8SLorenz Bauer 	if (ctx.selected_sk) {
14587c32e8f8SLorenz Bauer 		if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) {
14597c32e8f8SLorenz Bauer 			ret = -EOPNOTSUPP;
14607c32e8f8SLorenz Bauer 			goto out;
14617c32e8f8SLorenz Bauer 		}
14627c32e8f8SLorenz Bauer 
14637c32e8f8SLorenz Bauer 		user_ctx->cookie = sock_gen_cookie(ctx.selected_sk);
14647c32e8f8SLorenz Bauer 	}
14657c32e8f8SLorenz Bauer 
14667855e0dbSLorenzo Bianconi 	ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
14677c32e8f8SLorenz Bauer 	if (!ret)
14687c32e8f8SLorenz Bauer 		ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx));
14697c32e8f8SLorenz Bauer 
14707c32e8f8SLorenz Bauer out:
14717c32e8f8SLorenz Bauer 	bpf_prog_array_free(progs);
14727c32e8f8SLorenz Bauer 	kfree(user_ctx);
14737c32e8f8SLorenz Bauer 	return ret;
14747c32e8f8SLorenz Bauer }
147579a7f8bdSAlexei Starovoitov 
bpf_prog_test_run_syscall(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)147679a7f8bdSAlexei Starovoitov int bpf_prog_test_run_syscall(struct bpf_prog *prog,
147779a7f8bdSAlexei Starovoitov 			      const union bpf_attr *kattr,
147879a7f8bdSAlexei Starovoitov 			      union bpf_attr __user *uattr)
147979a7f8bdSAlexei Starovoitov {
148079a7f8bdSAlexei Starovoitov 	void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
148179a7f8bdSAlexei Starovoitov 	__u32 ctx_size_in = kattr->test.ctx_size_in;
148279a7f8bdSAlexei Starovoitov 	void *ctx = NULL;
148379a7f8bdSAlexei Starovoitov 	u32 retval;
148479a7f8bdSAlexei Starovoitov 	int err = 0;
148579a7f8bdSAlexei Starovoitov 
148679a7f8bdSAlexei Starovoitov 	/* doesn't support data_in/out, ctx_out, duration, or repeat or flags */
148779a7f8bdSAlexei Starovoitov 	if (kattr->test.data_in || kattr->test.data_out ||
148879a7f8bdSAlexei Starovoitov 	    kattr->test.ctx_out || kattr->test.duration ||
1489b530e9e1SToke Høiland-Jørgensen 	    kattr->test.repeat || kattr->test.flags ||
1490b530e9e1SToke Høiland-Jørgensen 	    kattr->test.batch_size)
149179a7f8bdSAlexei Starovoitov 		return -EINVAL;
149279a7f8bdSAlexei Starovoitov 
149379a7f8bdSAlexei Starovoitov 	if (ctx_size_in < prog->aux->max_ctx_offset ||
149479a7f8bdSAlexei Starovoitov 	    ctx_size_in > U16_MAX)
149579a7f8bdSAlexei Starovoitov 		return -EINVAL;
149679a7f8bdSAlexei Starovoitov 
149779a7f8bdSAlexei Starovoitov 	if (ctx_size_in) {
1498db5b6a46SQing Wang 		ctx = memdup_user(ctx_in, ctx_size_in);
1499db5b6a46SQing Wang 		if (IS_ERR(ctx))
1500db5b6a46SQing Wang 			return PTR_ERR(ctx);
150179a7f8bdSAlexei Starovoitov 	}
150287b7b533SYonghong Song 
150387b7b533SYonghong Song 	rcu_read_lock_trace();
150479a7f8bdSAlexei Starovoitov 	retval = bpf_prog_run_pin_on_cpu(prog, ctx);
150587b7b533SYonghong Song 	rcu_read_unlock_trace();
150679a7f8bdSAlexei Starovoitov 
150779a7f8bdSAlexei Starovoitov 	if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) {
150879a7f8bdSAlexei Starovoitov 		err = -EFAULT;
150979a7f8bdSAlexei Starovoitov 		goto out;
151079a7f8bdSAlexei Starovoitov 	}
151179a7f8bdSAlexei Starovoitov 	if (ctx_size_in)
151279a7f8bdSAlexei Starovoitov 		if (copy_to_user(ctx_in, ctx, ctx_size_in))
151379a7f8bdSAlexei Starovoitov 			err = -EFAULT;
151479a7f8bdSAlexei Starovoitov out:
151579a7f8bdSAlexei Starovoitov 	kfree(ctx);
151679a7f8bdSAlexei Starovoitov 	return err;
151779a7f8bdSAlexei Starovoitov }
1518b202d844SKumar Kartikeya Dwivedi 
verify_and_copy_hook_state(struct nf_hook_state * state,const struct nf_hook_state * user,struct net_device * dev)15192b99ef22SFlorian Westphal static int verify_and_copy_hook_state(struct nf_hook_state *state,
15202b99ef22SFlorian Westphal 				      const struct nf_hook_state *user,
15212b99ef22SFlorian Westphal 				      struct net_device *dev)
15222b99ef22SFlorian Westphal {
15232b99ef22SFlorian Westphal 	if (user->in || user->out)
15242b99ef22SFlorian Westphal 		return -EINVAL;
15252b99ef22SFlorian Westphal 
15262b99ef22SFlorian Westphal 	if (user->net || user->sk || user->okfn)
15272b99ef22SFlorian Westphal 		return -EINVAL;
15282b99ef22SFlorian Westphal 
15292b99ef22SFlorian Westphal 	switch (user->pf) {
15302b99ef22SFlorian Westphal 	case NFPROTO_IPV4:
15312b99ef22SFlorian Westphal 	case NFPROTO_IPV6:
15322b99ef22SFlorian Westphal 		switch (state->hook) {
15332b99ef22SFlorian Westphal 		case NF_INET_PRE_ROUTING:
15342b99ef22SFlorian Westphal 			state->in = dev;
15352b99ef22SFlorian Westphal 			break;
15362b99ef22SFlorian Westphal 		case NF_INET_LOCAL_IN:
15372b99ef22SFlorian Westphal 			state->in = dev;
15382b99ef22SFlorian Westphal 			break;
15392b99ef22SFlorian Westphal 		case NF_INET_FORWARD:
15402b99ef22SFlorian Westphal 			state->in = dev;
15412b99ef22SFlorian Westphal 			state->out = dev;
15422b99ef22SFlorian Westphal 			break;
15432b99ef22SFlorian Westphal 		case NF_INET_LOCAL_OUT:
15442b99ef22SFlorian Westphal 			state->out = dev;
15452b99ef22SFlorian Westphal 			break;
15462b99ef22SFlorian Westphal 		case NF_INET_POST_ROUTING:
15472b99ef22SFlorian Westphal 			state->out = dev;
15482b99ef22SFlorian Westphal 			break;
15492b99ef22SFlorian Westphal 		}
15502b99ef22SFlorian Westphal 
15512b99ef22SFlorian Westphal 		break;
15522b99ef22SFlorian Westphal 	default:
15532b99ef22SFlorian Westphal 		return -EINVAL;
15542b99ef22SFlorian Westphal 	}
15552b99ef22SFlorian Westphal 
15562b99ef22SFlorian Westphal 	state->pf = user->pf;
15572b99ef22SFlorian Westphal 	state->hook = user->hook;
15582b99ef22SFlorian Westphal 
15592b99ef22SFlorian Westphal 	return 0;
15602b99ef22SFlorian Westphal }
15612b99ef22SFlorian Westphal 
nfproto_eth(int nfproto)15622b99ef22SFlorian Westphal static __be16 nfproto_eth(int nfproto)
15632b99ef22SFlorian Westphal {
15642b99ef22SFlorian Westphal 	switch (nfproto) {
15652b99ef22SFlorian Westphal 	case NFPROTO_IPV4:
15662b99ef22SFlorian Westphal 		return htons(ETH_P_IP);
15672b99ef22SFlorian Westphal 	case NFPROTO_IPV6:
15682b99ef22SFlorian Westphal 		break;
15692b99ef22SFlorian Westphal 	}
15702b99ef22SFlorian Westphal 
15712b99ef22SFlorian Westphal 	return htons(ETH_P_IPV6);
15722b99ef22SFlorian Westphal }
15732b99ef22SFlorian Westphal 
bpf_prog_test_run_nf(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)15742b99ef22SFlorian Westphal int bpf_prog_test_run_nf(struct bpf_prog *prog,
15752b99ef22SFlorian Westphal 			 const union bpf_attr *kattr,
15762b99ef22SFlorian Westphal 			 union bpf_attr __user *uattr)
15772b99ef22SFlorian Westphal {
15782b99ef22SFlorian Westphal 	struct net *net = current->nsproxy->net_ns;
15792b99ef22SFlorian Westphal 	struct net_device *dev = net->loopback_dev;
15802b99ef22SFlorian Westphal 	struct nf_hook_state *user_ctx, hook_state = {
15812b99ef22SFlorian Westphal 		.pf = NFPROTO_IPV4,
15822b99ef22SFlorian Westphal 		.hook = NF_INET_LOCAL_OUT,
15832b99ef22SFlorian Westphal 	};
15842b99ef22SFlorian Westphal 	u32 size = kattr->test.data_size_in;
15852b99ef22SFlorian Westphal 	u32 repeat = kattr->test.repeat;
15862b99ef22SFlorian Westphal 	struct bpf_nf_ctx ctx = {
15872b99ef22SFlorian Westphal 		.state = &hook_state,
15882b99ef22SFlorian Westphal 	};
15892b99ef22SFlorian Westphal 	struct sk_buff *skb = NULL;
15902b99ef22SFlorian Westphal 	u32 retval, duration;
15912b99ef22SFlorian Westphal 	void *data;
15922b99ef22SFlorian Westphal 	int ret;
15932b99ef22SFlorian Westphal 
15942b99ef22SFlorian Westphal 	if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
15952b99ef22SFlorian Westphal 		return -EINVAL;
15962b99ef22SFlorian Westphal 
15972b99ef22SFlorian Westphal 	if (size < sizeof(struct iphdr))
15982b99ef22SFlorian Westphal 		return -EINVAL;
15992b99ef22SFlorian Westphal 
16002b99ef22SFlorian Westphal 	data = bpf_test_init(kattr, kattr->test.data_size_in, size,
16012b99ef22SFlorian Westphal 			     NET_SKB_PAD + NET_IP_ALIGN,
16022b99ef22SFlorian Westphal 			     SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
16032b99ef22SFlorian Westphal 	if (IS_ERR(data))
16042b99ef22SFlorian Westphal 		return PTR_ERR(data);
16052b99ef22SFlorian Westphal 
16062b99ef22SFlorian Westphal 	if (!repeat)
16072b99ef22SFlorian Westphal 		repeat = 1;
16082b99ef22SFlorian Westphal 
16092b99ef22SFlorian Westphal 	user_ctx = bpf_ctx_init(kattr, sizeof(struct nf_hook_state));
16102b99ef22SFlorian Westphal 	if (IS_ERR(user_ctx)) {
16112b99ef22SFlorian Westphal 		kfree(data);
16122b99ef22SFlorian Westphal 		return PTR_ERR(user_ctx);
16132b99ef22SFlorian Westphal 	}
16142b99ef22SFlorian Westphal 
16152b99ef22SFlorian Westphal 	if (user_ctx) {
16162b99ef22SFlorian Westphal 		ret = verify_and_copy_hook_state(&hook_state, user_ctx, dev);
16172b99ef22SFlorian Westphal 		if (ret)
16182b99ef22SFlorian Westphal 			goto out;
16192b99ef22SFlorian Westphal 	}
16202b99ef22SFlorian Westphal 
16212b99ef22SFlorian Westphal 	skb = slab_build_skb(data);
16222b99ef22SFlorian Westphal 	if (!skb) {
16232b99ef22SFlorian Westphal 		ret = -ENOMEM;
16242b99ef22SFlorian Westphal 		goto out;
16252b99ef22SFlorian Westphal 	}
16262b99ef22SFlorian Westphal 
16272b99ef22SFlorian Westphal 	data = NULL; /* data released via kfree_skb */
16282b99ef22SFlorian Westphal 
16292b99ef22SFlorian Westphal 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
16302b99ef22SFlorian Westphal 	__skb_put(skb, size);
16312b99ef22SFlorian Westphal 
16322b99ef22SFlorian Westphal 	ret = -EINVAL;
16332b99ef22SFlorian Westphal 
16342b99ef22SFlorian Westphal 	if (hook_state.hook != NF_INET_LOCAL_OUT) {
16352b99ef22SFlorian Westphal 		if (size < ETH_HLEN + sizeof(struct iphdr))
16362b99ef22SFlorian Westphal 			goto out;
16372b99ef22SFlorian Westphal 
16382b99ef22SFlorian Westphal 		skb->protocol = eth_type_trans(skb, dev);
16392b99ef22SFlorian Westphal 		switch (skb->protocol) {
16402b99ef22SFlorian Westphal 		case htons(ETH_P_IP):
16412b99ef22SFlorian Westphal 			if (hook_state.pf == NFPROTO_IPV4)
16422b99ef22SFlorian Westphal 				break;
16432b99ef22SFlorian Westphal 			goto out;
16442b99ef22SFlorian Westphal 		case htons(ETH_P_IPV6):
16452b99ef22SFlorian Westphal 			if (size < ETH_HLEN + sizeof(struct ipv6hdr))
16462b99ef22SFlorian Westphal 				goto out;
16472b99ef22SFlorian Westphal 			if (hook_state.pf == NFPROTO_IPV6)
16482b99ef22SFlorian Westphal 				break;
16492b99ef22SFlorian Westphal 			goto out;
16502b99ef22SFlorian Westphal 		default:
16512b99ef22SFlorian Westphal 			ret = -EPROTO;
16522b99ef22SFlorian Westphal 			goto out;
16532b99ef22SFlorian Westphal 		}
16542b99ef22SFlorian Westphal 
16552b99ef22SFlorian Westphal 		skb_reset_network_header(skb);
16562b99ef22SFlorian Westphal 	} else {
16572b99ef22SFlorian Westphal 		skb->protocol = nfproto_eth(hook_state.pf);
16582b99ef22SFlorian Westphal 	}
16592b99ef22SFlorian Westphal 
16602b99ef22SFlorian Westphal 	ctx.skb = skb;
16612b99ef22SFlorian Westphal 
16622b99ef22SFlorian Westphal 	ret = bpf_test_run(prog, &ctx, repeat, &retval, &duration, false);
16632b99ef22SFlorian Westphal 	if (ret)
16642b99ef22SFlorian Westphal 		goto out;
16652b99ef22SFlorian Westphal 
16662b99ef22SFlorian Westphal 	ret = bpf_test_finish(kattr, uattr, NULL, NULL, 0, retval, duration);
16672b99ef22SFlorian Westphal 
16682b99ef22SFlorian Westphal out:
16692b99ef22SFlorian Westphal 	kfree(user_ctx);
16702b99ef22SFlorian Westphal 	kfree_skb(skb);
16712b99ef22SFlorian Westphal 	kfree(data);
16722b99ef22SFlorian Westphal 	return ret;
16732b99ef22SFlorian Westphal }
16742b99ef22SFlorian Westphal 
1675b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = {
1676b202d844SKumar Kartikeya Dwivedi 	.owner = THIS_MODULE,
1677a4703e31SKumar Kartikeya Dwivedi 	.set   = &test_sk_check_kfunc_ids,
1678b202d844SKumar Kartikeya Dwivedi };
1679b202d844SKumar Kartikeya Dwivedi 
168005a945deSKumar Kartikeya Dwivedi BTF_ID_LIST(bpf_prog_test_dtor_kfunc_ids)
BTF_ID(struct,prog_test_ref_kfunc)168105a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_ref_kfunc)
168205a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_test_release)
168305a945deSKumar Kartikeya Dwivedi BTF_ID(struct, prog_test_member)
168405a945deSKumar Kartikeya Dwivedi BTF_ID(func, bpf_kfunc_call_memb_release)
168505a945deSKumar Kartikeya Dwivedi 
1686b202d844SKumar Kartikeya Dwivedi static int __init bpf_prog_test_run_init(void)
1687b202d844SKumar Kartikeya Dwivedi {
168805a945deSKumar Kartikeya Dwivedi 	const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = {
168905a945deSKumar Kartikeya Dwivedi 		{
169005a945deSKumar Kartikeya Dwivedi 		  .btf_id       = bpf_prog_test_dtor_kfunc_ids[0],
169105a945deSKumar Kartikeya Dwivedi 		  .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1]
169205a945deSKumar Kartikeya Dwivedi 		},
169305a945deSKumar Kartikeya Dwivedi 		{
169405a945deSKumar Kartikeya Dwivedi 		  .btf_id	= bpf_prog_test_dtor_kfunc_ids[2],
169505a945deSKumar Kartikeya Dwivedi 		  .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3],
169605a945deSKumar Kartikeya Dwivedi 		},
169705a945deSKumar Kartikeya Dwivedi 	};
169805a945deSKumar Kartikeya Dwivedi 	int ret;
169905a945deSKumar Kartikeya Dwivedi 
17005b481acaSBenjamin Tissoires 	ret = register_btf_fmodret_id_set(&bpf_test_modify_return_set);
17015b481acaSBenjamin Tissoires 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_prog_test_kfunc_set);
17021f075262SKumar Kartikeya Dwivedi 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_prog_test_kfunc_set);
1703fb66223aSBenjamin Tissoires 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_prog_test_kfunc_set);
170405a945deSKumar Kartikeya Dwivedi 	return ret ?: register_btf_id_dtor_kfuncs(bpf_prog_test_dtor_kfunc,
170505a945deSKumar Kartikeya Dwivedi 						  ARRAY_SIZE(bpf_prog_test_dtor_kfunc),
170605a945deSKumar Kartikeya Dwivedi 						  THIS_MODULE);
1707b202d844SKumar Kartikeya Dwivedi }
1708b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_prog_test_run_init);
1709