xref: /openbmc/linux/net/core/xdp.c (revision 8d01e63f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* net/core/xdp.c
3  *
4  * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
5  */
6 #include <linux/bpf.h>
7 #include <linux/btf.h>
8 #include <linux/btf_ids.h>
9 #include <linux/filter.h>
10 #include <linux/types.h>
11 #include <linux/mm.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/idr.h>
15 #include <linux/rhashtable.h>
16 #include <linux/bug.h>
17 #include <net/page_pool/helpers.h>
18 
19 #include <net/xdp.h>
20 #include <net/xdp_priv.h> /* struct xdp_mem_allocator */
21 #include <trace/events/xdp.h>
22 #include <net/xdp_sock_drv.h>
23 
24 #define REG_STATE_NEW		0x0
25 #define REG_STATE_REGISTERED	0x1
26 #define REG_STATE_UNREGISTERED	0x2
27 #define REG_STATE_UNUSED	0x3
28 
29 static DEFINE_IDA(mem_id_pool);
30 static DEFINE_MUTEX(mem_id_lock);
31 #define MEM_ID_MAX 0xFFFE
32 #define MEM_ID_MIN 1
33 static int mem_id_next = MEM_ID_MIN;
34 
35 static bool mem_id_init; /* false */
36 static struct rhashtable *mem_id_ht;
37 
38 static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
39 {
40 	const u32 *k = data;
41 	const u32 key = *k;
42 
43 	BUILD_BUG_ON(sizeof_field(struct xdp_mem_allocator, mem.id)
44 		     != sizeof(u32));
45 
46 	/* Use cyclic increasing ID as direct hash key */
47 	return key;
48 }
49 
50 static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
51 			  const void *ptr)
52 {
53 	const struct xdp_mem_allocator *xa = ptr;
54 	u32 mem_id = *(u32 *)arg->key;
55 
56 	return xa->mem.id != mem_id;
57 }
58 
59 static const struct rhashtable_params mem_id_rht_params = {
60 	.nelem_hint = 64,
61 	.head_offset = offsetof(struct xdp_mem_allocator, node),
62 	.key_offset  = offsetof(struct xdp_mem_allocator, mem.id),
63 	.key_len = sizeof_field(struct xdp_mem_allocator, mem.id),
64 	.max_size = MEM_ID_MAX,
65 	.min_size = 8,
66 	.automatic_shrinking = true,
67 	.hashfn    = xdp_mem_id_hashfn,
68 	.obj_cmpfn = xdp_mem_id_cmp,
69 };
70 
71 static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
72 {
73 	struct xdp_mem_allocator *xa;
74 
75 	xa = container_of(rcu, struct xdp_mem_allocator, rcu);
76 
77 	/* Allow this ID to be reused */
78 	ida_simple_remove(&mem_id_pool, xa->mem.id);
79 
80 	kfree(xa);
81 }
82 
83 static void mem_xa_remove(struct xdp_mem_allocator *xa)
84 {
85 	trace_mem_disconnect(xa);
86 
87 	if (!rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params))
88 		call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
89 }
90 
91 static void mem_allocator_disconnect(void *allocator)
92 {
93 	struct xdp_mem_allocator *xa;
94 	struct rhashtable_iter iter;
95 
96 	mutex_lock(&mem_id_lock);
97 
98 	rhashtable_walk_enter(mem_id_ht, &iter);
99 	do {
100 		rhashtable_walk_start(&iter);
101 
102 		while ((xa = rhashtable_walk_next(&iter)) && !IS_ERR(xa)) {
103 			if (xa->allocator == allocator)
104 				mem_xa_remove(xa);
105 		}
106 
107 		rhashtable_walk_stop(&iter);
108 
109 	} while (xa == ERR_PTR(-EAGAIN));
110 	rhashtable_walk_exit(&iter);
111 
112 	mutex_unlock(&mem_id_lock);
113 }
114 
115 void xdp_unreg_mem_model(struct xdp_mem_info *mem)
116 {
117 	struct xdp_mem_allocator *xa;
118 	int type = mem->type;
119 	int id = mem->id;
120 
121 	/* Reset mem info to defaults */
122 	mem->id = 0;
123 	mem->type = 0;
124 
125 	if (id == 0)
126 		return;
127 
128 	if (type == MEM_TYPE_PAGE_POOL) {
129 		xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
130 		page_pool_destroy(xa->page_pool);
131 	}
132 }
133 EXPORT_SYMBOL_GPL(xdp_unreg_mem_model);
134 
135 void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
136 {
137 	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
138 		WARN(1, "Missing register, driver bug");
139 		return;
140 	}
141 
142 	xdp_unreg_mem_model(&xdp_rxq->mem);
143 }
144 EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
145 
146 void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
147 {
148 	/* Simplify driver cleanup code paths, allow unreg "unused" */
149 	if (xdp_rxq->reg_state == REG_STATE_UNUSED)
150 		return;
151 
152 	xdp_rxq_info_unreg_mem_model(xdp_rxq);
153 
154 	xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
155 	xdp_rxq->dev = NULL;
156 }
157 EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
158 
159 static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
160 {
161 	memset(xdp_rxq, 0, sizeof(*xdp_rxq));
162 }
163 
164 /* Returns 0 on success, negative on failure */
165 int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
166 		       struct net_device *dev, u32 queue_index,
167 		       unsigned int napi_id, u32 frag_size)
168 {
169 	if (!dev) {
170 		WARN(1, "Missing net_device from driver");
171 		return -ENODEV;
172 	}
173 
174 	if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
175 		WARN(1, "Driver promised not to register this");
176 		return -EINVAL;
177 	}
178 
179 	if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
180 		WARN(1, "Missing unregister, handled but fix driver");
181 		xdp_rxq_info_unreg(xdp_rxq);
182 	}
183 
184 	/* State either UNREGISTERED or NEW */
185 	xdp_rxq_info_init(xdp_rxq);
186 	xdp_rxq->dev = dev;
187 	xdp_rxq->queue_index = queue_index;
188 	xdp_rxq->napi_id = napi_id;
189 	xdp_rxq->frag_size = frag_size;
190 
191 	xdp_rxq->reg_state = REG_STATE_REGISTERED;
192 	return 0;
193 }
194 EXPORT_SYMBOL_GPL(__xdp_rxq_info_reg);
195 
196 void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
197 {
198 	xdp_rxq->reg_state = REG_STATE_UNUSED;
199 }
200 EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
201 
202 bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
203 {
204 	return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
205 }
206 EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
207 
208 static int __mem_id_init_hash_table(void)
209 {
210 	struct rhashtable *rht;
211 	int ret;
212 
213 	if (unlikely(mem_id_init))
214 		return 0;
215 
216 	rht = kzalloc(sizeof(*rht), GFP_KERNEL);
217 	if (!rht)
218 		return -ENOMEM;
219 
220 	ret = rhashtable_init(rht, &mem_id_rht_params);
221 	if (ret < 0) {
222 		kfree(rht);
223 		return ret;
224 	}
225 	mem_id_ht = rht;
226 	smp_mb(); /* mutex lock should provide enough pairing */
227 	mem_id_init = true;
228 
229 	return 0;
230 }
231 
232 /* Allocate a cyclic ID that maps to allocator pointer.
233  * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
234  *
235  * Caller must lock mem_id_lock.
236  */
237 static int __mem_id_cyclic_get(gfp_t gfp)
238 {
239 	int retries = 1;
240 	int id;
241 
242 again:
243 	id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
244 	if (id < 0) {
245 		if (id == -ENOSPC) {
246 			/* Cyclic allocator, reset next id */
247 			if (retries--) {
248 				mem_id_next = MEM_ID_MIN;
249 				goto again;
250 			}
251 		}
252 		return id; /* errno */
253 	}
254 	mem_id_next = id + 1;
255 
256 	return id;
257 }
258 
259 static bool __is_supported_mem_type(enum xdp_mem_type type)
260 {
261 	if (type == MEM_TYPE_PAGE_POOL)
262 		return is_page_pool_compiled_in();
263 
264 	if (type >= MEM_TYPE_MAX)
265 		return false;
266 
267 	return true;
268 }
269 
270 static struct xdp_mem_allocator *__xdp_reg_mem_model(struct xdp_mem_info *mem,
271 						     enum xdp_mem_type type,
272 						     void *allocator)
273 {
274 	struct xdp_mem_allocator *xdp_alloc;
275 	gfp_t gfp = GFP_KERNEL;
276 	int id, errno, ret;
277 	void *ptr;
278 
279 	if (!__is_supported_mem_type(type))
280 		return ERR_PTR(-EOPNOTSUPP);
281 
282 	mem->type = type;
283 
284 	if (!allocator) {
285 		if (type == MEM_TYPE_PAGE_POOL)
286 			return ERR_PTR(-EINVAL); /* Setup time check page_pool req */
287 		return NULL;
288 	}
289 
290 	/* Delay init of rhashtable to save memory if feature isn't used */
291 	if (!mem_id_init) {
292 		mutex_lock(&mem_id_lock);
293 		ret = __mem_id_init_hash_table();
294 		mutex_unlock(&mem_id_lock);
295 		if (ret < 0)
296 			return ERR_PTR(ret);
297 	}
298 
299 	xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
300 	if (!xdp_alloc)
301 		return ERR_PTR(-ENOMEM);
302 
303 	mutex_lock(&mem_id_lock);
304 	id = __mem_id_cyclic_get(gfp);
305 	if (id < 0) {
306 		errno = id;
307 		goto err;
308 	}
309 	mem->id = id;
310 	xdp_alloc->mem = *mem;
311 	xdp_alloc->allocator = allocator;
312 
313 	/* Insert allocator into ID lookup table */
314 	ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
315 	if (IS_ERR(ptr)) {
316 		ida_simple_remove(&mem_id_pool, mem->id);
317 		mem->id = 0;
318 		errno = PTR_ERR(ptr);
319 		goto err;
320 	}
321 
322 	if (type == MEM_TYPE_PAGE_POOL)
323 		page_pool_use_xdp_mem(allocator, mem_allocator_disconnect, mem);
324 
325 	mutex_unlock(&mem_id_lock);
326 
327 	return xdp_alloc;
328 err:
329 	mutex_unlock(&mem_id_lock);
330 	kfree(xdp_alloc);
331 	return ERR_PTR(errno);
332 }
333 
334 int xdp_reg_mem_model(struct xdp_mem_info *mem,
335 		      enum xdp_mem_type type, void *allocator)
336 {
337 	struct xdp_mem_allocator *xdp_alloc;
338 
339 	xdp_alloc = __xdp_reg_mem_model(mem, type, allocator);
340 	if (IS_ERR(xdp_alloc))
341 		return PTR_ERR(xdp_alloc);
342 	return 0;
343 }
344 EXPORT_SYMBOL_GPL(xdp_reg_mem_model);
345 
346 int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
347 			       enum xdp_mem_type type, void *allocator)
348 {
349 	struct xdp_mem_allocator *xdp_alloc;
350 
351 	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
352 		WARN(1, "Missing register, driver bug");
353 		return -EFAULT;
354 	}
355 
356 	xdp_alloc = __xdp_reg_mem_model(&xdp_rxq->mem, type, allocator);
357 	if (IS_ERR(xdp_alloc))
358 		return PTR_ERR(xdp_alloc);
359 
360 	if (trace_mem_connect_enabled() && xdp_alloc)
361 		trace_mem_connect(xdp_alloc, xdp_rxq);
362 	return 0;
363 }
364 
365 EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
366 
367 /* XDP RX runs under NAPI protection, and in different delivery error
368  * scenarios (e.g. queue full), it is possible to return the xdp_frame
369  * while still leveraging this protection.  The @napi_direct boolean
370  * is used for those calls sites.  Thus, allowing for faster recycling
371  * of xdp_frames/pages in those cases.
372  */
373 void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
374 		  struct xdp_buff *xdp)
375 {
376 	struct page *page;
377 
378 	switch (mem->type) {
379 	case MEM_TYPE_PAGE_POOL:
380 		page = virt_to_head_page(data);
381 		if (napi_direct && xdp_return_frame_no_direct())
382 			napi_direct = false;
383 		/* No need to check ((page->pp_magic & ~0x3UL) == PP_SIGNATURE)
384 		 * as mem->type knows this a page_pool page
385 		 */
386 		page_pool_put_full_page(page->pp, page, napi_direct);
387 		break;
388 	case MEM_TYPE_PAGE_SHARED:
389 		page_frag_free(data);
390 		break;
391 	case MEM_TYPE_PAGE_ORDER0:
392 		page = virt_to_page(data); /* Assumes order0 page*/
393 		put_page(page);
394 		break;
395 	case MEM_TYPE_XSK_BUFF_POOL:
396 		/* NB! Only valid from an xdp_buff! */
397 		xsk_buff_free(xdp);
398 		break;
399 	default:
400 		/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
401 		WARN(1, "Incorrect XDP memory type (%d) usage", mem->type);
402 		break;
403 	}
404 }
405 
406 void xdp_return_frame(struct xdp_frame *xdpf)
407 {
408 	struct skb_shared_info *sinfo;
409 	int i;
410 
411 	if (likely(!xdp_frame_has_frags(xdpf)))
412 		goto out;
413 
414 	sinfo = xdp_get_shared_info_from_frame(xdpf);
415 	for (i = 0; i < sinfo->nr_frags; i++) {
416 		struct page *page = skb_frag_page(&sinfo->frags[i]);
417 
418 		__xdp_return(page_address(page), &xdpf->mem, false, NULL);
419 	}
420 out:
421 	__xdp_return(xdpf->data, &xdpf->mem, false, NULL);
422 }
423 EXPORT_SYMBOL_GPL(xdp_return_frame);
424 
425 void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
426 {
427 	struct skb_shared_info *sinfo;
428 	int i;
429 
430 	if (likely(!xdp_frame_has_frags(xdpf)))
431 		goto out;
432 
433 	sinfo = xdp_get_shared_info_from_frame(xdpf);
434 	for (i = 0; i < sinfo->nr_frags; i++) {
435 		struct page *page = skb_frag_page(&sinfo->frags[i]);
436 
437 		__xdp_return(page_address(page), &xdpf->mem, true, NULL);
438 	}
439 out:
440 	__xdp_return(xdpf->data, &xdpf->mem, true, NULL);
441 }
442 EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
443 
444 /* XDP bulk APIs introduce a defer/flush mechanism to return
445  * pages belonging to the same xdp_mem_allocator object
446  * (identified via the mem.id field) in bulk to optimize
447  * I-cache and D-cache.
448  * The bulk queue size is set to 16 to be aligned to how
449  * XDP_REDIRECT bulking works. The bulk is flushed when
450  * it is full or when mem.id changes.
451  * xdp_frame_bulk is usually stored/allocated on the function
452  * call-stack to avoid locking penalties.
453  */
454 void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq)
455 {
456 	struct xdp_mem_allocator *xa = bq->xa;
457 
458 	if (unlikely(!xa || !bq->count))
459 		return;
460 
461 	page_pool_put_page_bulk(xa->page_pool, bq->q, bq->count);
462 	/* bq->xa is not cleared to save lookup, if mem.id same in next bulk */
463 	bq->count = 0;
464 }
465 EXPORT_SYMBOL_GPL(xdp_flush_frame_bulk);
466 
467 /* Must be called with rcu_read_lock held */
468 void xdp_return_frame_bulk(struct xdp_frame *xdpf,
469 			   struct xdp_frame_bulk *bq)
470 {
471 	struct xdp_mem_info *mem = &xdpf->mem;
472 	struct xdp_mem_allocator *xa;
473 
474 	if (mem->type != MEM_TYPE_PAGE_POOL) {
475 		xdp_return_frame(xdpf);
476 		return;
477 	}
478 
479 	xa = bq->xa;
480 	if (unlikely(!xa)) {
481 		xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
482 		bq->count = 0;
483 		bq->xa = xa;
484 	}
485 
486 	if (bq->count == XDP_BULK_QUEUE_SIZE)
487 		xdp_flush_frame_bulk(bq);
488 
489 	if (unlikely(mem->id != xa->mem.id)) {
490 		xdp_flush_frame_bulk(bq);
491 		bq->xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
492 	}
493 
494 	if (unlikely(xdp_frame_has_frags(xdpf))) {
495 		struct skb_shared_info *sinfo;
496 		int i;
497 
498 		sinfo = xdp_get_shared_info_from_frame(xdpf);
499 		for (i = 0; i < sinfo->nr_frags; i++) {
500 			skb_frag_t *frag = &sinfo->frags[i];
501 
502 			bq->q[bq->count++] = skb_frag_address(frag);
503 			if (bq->count == XDP_BULK_QUEUE_SIZE)
504 				xdp_flush_frame_bulk(bq);
505 		}
506 	}
507 	bq->q[bq->count++] = xdpf->data;
508 }
509 EXPORT_SYMBOL_GPL(xdp_return_frame_bulk);
510 
511 void xdp_return_buff(struct xdp_buff *xdp)
512 {
513 	struct skb_shared_info *sinfo;
514 	int i;
515 
516 	if (likely(!xdp_buff_has_frags(xdp)))
517 		goto out;
518 
519 	sinfo = xdp_get_shared_info_from_buff(xdp);
520 	for (i = 0; i < sinfo->nr_frags; i++) {
521 		struct page *page = skb_frag_page(&sinfo->frags[i]);
522 
523 		__xdp_return(page_address(page), &xdp->rxq->mem, true, xdp);
524 	}
525 out:
526 	__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp);
527 }
528 EXPORT_SYMBOL_GPL(xdp_return_buff);
529 
530 void xdp_attachment_setup(struct xdp_attachment_info *info,
531 			  struct netdev_bpf *bpf)
532 {
533 	if (info->prog)
534 		bpf_prog_put(info->prog);
535 	info->prog = bpf->prog;
536 	info->flags = bpf->flags;
537 }
538 EXPORT_SYMBOL_GPL(xdp_attachment_setup);
539 
540 struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp)
541 {
542 	unsigned int metasize, totsize;
543 	void *addr, *data_to_copy;
544 	struct xdp_frame *xdpf;
545 	struct page *page;
546 
547 	/* Clone into a MEM_TYPE_PAGE_ORDER0 xdp_frame. */
548 	metasize = xdp_data_meta_unsupported(xdp) ? 0 :
549 		   xdp->data - xdp->data_meta;
550 	totsize = xdp->data_end - xdp->data + metasize;
551 
552 	if (sizeof(*xdpf) + totsize > PAGE_SIZE)
553 		return NULL;
554 
555 	page = dev_alloc_page();
556 	if (!page)
557 		return NULL;
558 
559 	addr = page_to_virt(page);
560 	xdpf = addr;
561 	memset(xdpf, 0, sizeof(*xdpf));
562 
563 	addr += sizeof(*xdpf);
564 	data_to_copy = metasize ? xdp->data_meta : xdp->data;
565 	memcpy(addr, data_to_copy, totsize);
566 
567 	xdpf->data = addr + metasize;
568 	xdpf->len = totsize - metasize;
569 	xdpf->headroom = 0;
570 	xdpf->metasize = metasize;
571 	xdpf->frame_sz = PAGE_SIZE;
572 	xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
573 
574 	xsk_buff_free(xdp);
575 	return xdpf;
576 }
577 EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame);
578 
579 /* Used by XDP_WARN macro, to avoid inlining WARN() in fast-path */
580 void xdp_warn(const char *msg, const char *func, const int line)
581 {
582 	WARN(1, "XDP_WARN: %s(line:%d): %s\n", func, line, msg);
583 };
584 EXPORT_SYMBOL_GPL(xdp_warn);
585 
586 int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp)
587 {
588 	n_skb = kmem_cache_alloc_bulk(skbuff_cache, gfp, n_skb, skbs);
589 	if (unlikely(!n_skb))
590 		return -ENOMEM;
591 
592 	return 0;
593 }
594 EXPORT_SYMBOL_GPL(xdp_alloc_skb_bulk);
595 
596 struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
597 					   struct sk_buff *skb,
598 					   struct net_device *dev)
599 {
600 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
601 	unsigned int headroom, frame_size;
602 	void *hard_start;
603 	u8 nr_frags;
604 
605 	/* xdp frags frame */
606 	if (unlikely(xdp_frame_has_frags(xdpf)))
607 		nr_frags = sinfo->nr_frags;
608 
609 	/* Part of headroom was reserved to xdpf */
610 	headroom = sizeof(*xdpf) + xdpf->headroom;
611 
612 	/* Memory size backing xdp_frame data already have reserved
613 	 * room for build_skb to place skb_shared_info in tailroom.
614 	 */
615 	frame_size = xdpf->frame_sz;
616 
617 	hard_start = xdpf->data - headroom;
618 	skb = build_skb_around(skb, hard_start, frame_size);
619 	if (unlikely(!skb))
620 		return NULL;
621 
622 	skb_reserve(skb, headroom);
623 	__skb_put(skb, xdpf->len);
624 	if (xdpf->metasize)
625 		skb_metadata_set(skb, xdpf->metasize);
626 
627 	if (unlikely(xdp_frame_has_frags(xdpf)))
628 		xdp_update_skb_shared_info(skb, nr_frags,
629 					   sinfo->xdp_frags_size,
630 					   nr_frags * xdpf->frame_sz,
631 					   xdp_frame_is_frag_pfmemalloc(xdpf));
632 
633 	/* Essential SKB info: protocol and skb->dev */
634 	skb->protocol = eth_type_trans(skb, dev);
635 
636 	/* Optional SKB info, currently missing:
637 	 * - HW checksum info		(skb->ip_summed)
638 	 * - HW RX hash			(skb_set_hash)
639 	 * - RX ring dev queue index	(skb_record_rx_queue)
640 	 */
641 
642 	if (xdpf->mem.type == MEM_TYPE_PAGE_POOL)
643 		skb_mark_for_recycle(skb);
644 
645 	/* Allow SKB to reuse area used by xdp_frame */
646 	xdp_scrub_frame(xdpf);
647 
648 	return skb;
649 }
650 EXPORT_SYMBOL_GPL(__xdp_build_skb_from_frame);
651 
652 struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
653 					 struct net_device *dev)
654 {
655 	struct sk_buff *skb;
656 
657 	skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC);
658 	if (unlikely(!skb))
659 		return NULL;
660 
661 	memset(skb, 0, offsetof(struct sk_buff, tail));
662 
663 	return __xdp_build_skb_from_frame(xdpf, skb, dev);
664 }
665 EXPORT_SYMBOL_GPL(xdp_build_skb_from_frame);
666 
667 struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf)
668 {
669 	unsigned int headroom, totalsize;
670 	struct xdp_frame *nxdpf;
671 	struct page *page;
672 	void *addr;
673 
674 	headroom = xdpf->headroom + sizeof(*xdpf);
675 	totalsize = headroom + xdpf->len;
676 
677 	if (unlikely(totalsize > PAGE_SIZE))
678 		return NULL;
679 	page = dev_alloc_page();
680 	if (!page)
681 		return NULL;
682 	addr = page_to_virt(page);
683 
684 	memcpy(addr, xdpf, totalsize);
685 
686 	nxdpf = addr;
687 	nxdpf->data = addr + headroom;
688 	nxdpf->frame_sz = PAGE_SIZE;
689 	nxdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
690 	nxdpf->mem.id = 0;
691 
692 	return nxdpf;
693 }
694 
695 __diag_push();
696 __diag_ignore_all("-Wmissing-prototypes",
697 		  "Global functions as their definitions will be in vmlinux BTF");
698 
699 /**
700  * bpf_xdp_metadata_rx_timestamp - Read XDP frame RX timestamp.
701  * @ctx: XDP context pointer.
702  * @timestamp: Return value pointer.
703  *
704  * Return:
705  * * Returns 0 on success or ``-errno`` on error.
706  * * ``-EOPNOTSUPP`` : means device driver does not implement kfunc
707  * * ``-ENODATA``    : means no RX-timestamp available for this frame
708  */
709 __bpf_kfunc int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
710 {
711 	return -EOPNOTSUPP;
712 }
713 
714 /**
715  * bpf_xdp_metadata_rx_hash - Read XDP frame RX hash.
716  * @ctx: XDP context pointer.
717  * @hash: Return value pointer.
718  * @rss_type: Return value pointer for RSS type.
719  *
720  * The RSS hash type (@rss_type) specifies what portion of packet headers NIC
721  * hardware used when calculating RSS hash value.  The RSS type can be decoded
722  * via &enum xdp_rss_hash_type either matching on individual L3/L4 bits
723  * ``XDP_RSS_L*`` or by combined traditional *RSS Hashing Types*
724  * ``XDP_RSS_TYPE_L*``.
725  *
726  * Return:
727  * * Returns 0 on success or ``-errno`` on error.
728  * * ``-EOPNOTSUPP`` : means device driver doesn't implement kfunc
729  * * ``-ENODATA``    : means no RX-hash available for this frame
730  */
731 __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,
732 					 enum xdp_rss_hash_type *rss_type)
733 {
734 	return -EOPNOTSUPP;
735 }
736 
737 __diag_pop();
738 
739 BTF_SET8_START(xdp_metadata_kfunc_ids)
740 #define XDP_METADATA_KFUNC(_, name) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
741 XDP_METADATA_KFUNC_xxx
742 #undef XDP_METADATA_KFUNC
743 BTF_SET8_END(xdp_metadata_kfunc_ids)
744 
745 static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
746 	.owner = THIS_MODULE,
747 	.set   = &xdp_metadata_kfunc_ids,
748 };
749 
750 BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
751 #define XDP_METADATA_KFUNC(name, str) BTF_ID(func, str)
752 XDP_METADATA_KFUNC_xxx
753 #undef XDP_METADATA_KFUNC
754 
755 u32 bpf_xdp_metadata_kfunc_id(int id)
756 {
757 	/* xdp_metadata_kfunc_ids is sorted and can't be used */
758 	return xdp_metadata_kfunc_ids_unsorted[id];
759 }
760 
761 bool bpf_dev_bound_kfunc_id(u32 btf_id)
762 {
763 	return btf_id_set8_contains(&xdp_metadata_kfunc_ids, btf_id);
764 }
765 
766 static int __init xdp_metadata_init(void)
767 {
768 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set);
769 }
770 late_initcall(xdp_metadata_init);
771 
772 void xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
773 {
774 	val &= NETDEV_XDP_ACT_MASK;
775 	if (dev->xdp_features == val)
776 		return;
777 
778 	dev->xdp_features = val;
779 
780 	if (dev->reg_state == NETREG_REGISTERED)
781 		call_netdevice_notifiers(NETDEV_XDP_FEAT_CHANGE, dev);
782 }
783 EXPORT_SYMBOL_GPL(xdp_set_features_flag);
784 
785 void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
786 {
787 	xdp_features_t val = (dev->xdp_features | NETDEV_XDP_ACT_NDO_XMIT);
788 
789 	if (support_sg)
790 		val |= NETDEV_XDP_ACT_NDO_XMIT_SG;
791 	xdp_set_features_flag(dev, val);
792 }
793 EXPORT_SYMBOL_GPL(xdp_features_set_redirect_target);
794 
795 void xdp_features_clear_redirect_target(struct net_device *dev)
796 {
797 	xdp_features_t val = dev->xdp_features;
798 
799 	val &= ~(NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_NDO_XMIT_SG);
800 	xdp_set_features_flag(dev, val);
801 }
802 EXPORT_SYMBOL_GPL(xdp_features_clear_redirect_target);
803