xref: /linux/net/core/xdp.c (revision a634dda26186cf9a51567020fcce52bcba5e1e59)
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/hotdata.h>
20 #include <net/xdp.h>
21 #include <net/xdp_priv.h> /* struct xdp_mem_allocator */
22 #include <trace/events/xdp.h>
23 #include <net/xdp_sock_drv.h>
24 
25 #define REG_STATE_NEW		0x0
26 #define REG_STATE_REGISTERED	0x1
27 #define REG_STATE_UNREGISTERED	0x2
28 #define REG_STATE_UNUSED	0x3
29 
30 static DEFINE_IDA(mem_id_pool);
31 static DEFINE_MUTEX(mem_id_lock);
32 #define MEM_ID_MAX 0xFFFE
33 #define MEM_ID_MIN 1
34 static int mem_id_next = MEM_ID_MIN;
35 
36 static bool mem_id_init; /* false */
37 static struct rhashtable *mem_id_ht;
38 
39 static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
40 {
41 	const u32 *k = data;
42 	const u32 key = *k;
43 
44 	BUILD_BUG_ON(sizeof_field(struct xdp_mem_allocator, mem.id)
45 		     != sizeof(u32));
46 
47 	/* Use cyclic increasing ID as direct hash key */
48 	return key;
49 }
50 
51 static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
52 			  const void *ptr)
53 {
54 	const struct xdp_mem_allocator *xa = ptr;
55 	u32 mem_id = *(u32 *)arg->key;
56 
57 	return xa->mem.id != mem_id;
58 }
59 
60 static const struct rhashtable_params mem_id_rht_params = {
61 	.nelem_hint = 64,
62 	.head_offset = offsetof(struct xdp_mem_allocator, node),
63 	.key_offset  = offsetof(struct xdp_mem_allocator, mem.id),
64 	.key_len = sizeof_field(struct xdp_mem_allocator, mem.id),
65 	.max_size = MEM_ID_MAX,
66 	.min_size = 8,
67 	.automatic_shrinking = true,
68 	.hashfn    = xdp_mem_id_hashfn,
69 	.obj_cmpfn = xdp_mem_id_cmp,
70 };
71 
72 static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
73 {
74 	struct xdp_mem_allocator *xa;
75 
76 	xa = container_of(rcu, struct xdp_mem_allocator, rcu);
77 
78 	/* Allow this ID to be reused */
79 	ida_free(&mem_id_pool, xa->mem.id);
80 
81 	kfree(xa);
82 }
83 
84 static void mem_xa_remove(struct xdp_mem_allocator *xa)
85 {
86 	trace_mem_disconnect(xa);
87 
88 	if (!rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params))
89 		call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
90 }
91 
92 static void mem_allocator_disconnect(void *allocator)
93 {
94 	struct xdp_mem_allocator *xa;
95 	struct rhashtable_iter iter;
96 
97 	mutex_lock(&mem_id_lock);
98 
99 	rhashtable_walk_enter(mem_id_ht, &iter);
100 	do {
101 		rhashtable_walk_start(&iter);
102 
103 		while ((xa = rhashtable_walk_next(&iter)) && !IS_ERR(xa)) {
104 			if (xa->allocator == allocator)
105 				mem_xa_remove(xa);
106 		}
107 
108 		rhashtable_walk_stop(&iter);
109 
110 	} while (xa == ERR_PTR(-EAGAIN));
111 	rhashtable_walk_exit(&iter);
112 
113 	mutex_unlock(&mem_id_lock);
114 }
115 
116 void xdp_unreg_mem_model(struct xdp_mem_info *mem)
117 {
118 	struct xdp_mem_allocator *xa;
119 	int type = mem->type;
120 	int id = mem->id;
121 
122 	/* Reset mem info to defaults */
123 	mem->id = 0;
124 	mem->type = 0;
125 
126 	if (id == 0)
127 		return;
128 
129 	if (type == MEM_TYPE_PAGE_POOL) {
130 		xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
131 		page_pool_destroy(xa->page_pool);
132 	}
133 }
134 EXPORT_SYMBOL_GPL(xdp_unreg_mem_model);
135 
136 void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
137 {
138 	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
139 		WARN(1, "Missing register, driver bug");
140 		return;
141 	}
142 
143 	xdp_unreg_mem_model(&xdp_rxq->mem);
144 }
145 EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
146 
147 void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
148 {
149 	/* Simplify driver cleanup code paths, allow unreg "unused" */
150 	if (xdp_rxq->reg_state == REG_STATE_UNUSED)
151 		return;
152 
153 	xdp_rxq_info_unreg_mem_model(xdp_rxq);
154 
155 	xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
156 	xdp_rxq->dev = NULL;
157 }
158 EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
159 
160 static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
161 {
162 	memset(xdp_rxq, 0, sizeof(*xdp_rxq));
163 }
164 
165 /* Returns 0 on success, negative on failure */
166 int __xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
167 		       struct net_device *dev, u32 queue_index,
168 		       unsigned int napi_id, u32 frag_size)
169 {
170 	if (!dev) {
171 		WARN(1, "Missing net_device from driver");
172 		return -ENODEV;
173 	}
174 
175 	if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
176 		WARN(1, "Driver promised not to register this");
177 		return -EINVAL;
178 	}
179 
180 	if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
181 		WARN(1, "Missing unregister, handled but fix driver");
182 		xdp_rxq_info_unreg(xdp_rxq);
183 	}
184 
185 	/* State either UNREGISTERED or NEW */
186 	xdp_rxq_info_init(xdp_rxq);
187 	xdp_rxq->dev = dev;
188 	xdp_rxq->queue_index = queue_index;
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_alloc_range(&mem_id_pool, mem_id_next, MEM_ID_MAX - 1, 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_free(&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(net_hotdata.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(net_hotdata.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 __bpf_kfunc_start_defs();
696 
697 /**
698  * bpf_xdp_metadata_rx_timestamp - Read XDP frame RX timestamp.
699  * @ctx: XDP context pointer.
700  * @timestamp: Return value pointer.
701  *
702  * Return:
703  * * Returns 0 on success or ``-errno`` on error.
704  * * ``-EOPNOTSUPP`` : means device driver does not implement kfunc
705  * * ``-ENODATA``    : means no RX-timestamp available for this frame
706  */
707 __bpf_kfunc int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
708 {
709 	return -EOPNOTSUPP;
710 }
711 
712 /**
713  * bpf_xdp_metadata_rx_hash - Read XDP frame RX hash.
714  * @ctx: XDP context pointer.
715  * @hash: Return value pointer.
716  * @rss_type: Return value pointer for RSS type.
717  *
718  * The RSS hash type (@rss_type) specifies what portion of packet headers NIC
719  * hardware used when calculating RSS hash value.  The RSS type can be decoded
720  * via &enum xdp_rss_hash_type either matching on individual L3/L4 bits
721  * ``XDP_RSS_L*`` or by combined traditional *RSS Hashing Types*
722  * ``XDP_RSS_TYPE_L*``.
723  *
724  * Return:
725  * * Returns 0 on success or ``-errno`` on error.
726  * * ``-EOPNOTSUPP`` : means device driver doesn't implement kfunc
727  * * ``-ENODATA``    : means no RX-hash available for this frame
728  */
729 __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,
730 					 enum xdp_rss_hash_type *rss_type)
731 {
732 	return -EOPNOTSUPP;
733 }
734 
735 /**
736  * bpf_xdp_metadata_rx_vlan_tag - Get XDP packet outermost VLAN tag
737  * @ctx: XDP context pointer.
738  * @vlan_proto: Destination pointer for VLAN Tag protocol identifier (TPID).
739  * @vlan_tci: Destination pointer for VLAN TCI (VID + DEI + PCP)
740  *
741  * In case of success, ``vlan_proto`` contains *Tag protocol identifier (TPID)*,
742  * usually ``ETH_P_8021Q`` or ``ETH_P_8021AD``, but some networks can use
743  * custom TPIDs. ``vlan_proto`` is stored in **network byte order (BE)**
744  * and should be used as follows:
745  * ``if (vlan_proto == bpf_htons(ETH_P_8021Q)) do_something();``
746  *
747  * ``vlan_tci`` contains the remaining 16 bits of a VLAN tag.
748  * Driver is expected to provide those in **host byte order (usually LE)**,
749  * so the bpf program should not perform byte conversion.
750  * According to 802.1Q standard, *VLAN TCI (Tag control information)*
751  * is a bit field that contains:
752  * *VLAN identifier (VID)* that can be read with ``vlan_tci & 0xfff``,
753  * *Drop eligible indicator (DEI)* - 1 bit,
754  * *Priority code point (PCP)* - 3 bits.
755  * For detailed meaning of DEI and PCP, please refer to other sources.
756  *
757  * Return:
758  * * Returns 0 on success or ``-errno`` on error.
759  * * ``-EOPNOTSUPP`` : device driver doesn't implement kfunc
760  * * ``-ENODATA``    : VLAN tag was not stripped or is not available
761  */
762 __bpf_kfunc int bpf_xdp_metadata_rx_vlan_tag(const struct xdp_md *ctx,
763 					     __be16 *vlan_proto, u16 *vlan_tci)
764 {
765 	return -EOPNOTSUPP;
766 }
767 
768 __bpf_kfunc_end_defs();
769 
770 BTF_KFUNCS_START(xdp_metadata_kfunc_ids)
771 #define XDP_METADATA_KFUNC(_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
772 XDP_METADATA_KFUNC_xxx
773 #undef XDP_METADATA_KFUNC
774 BTF_KFUNCS_END(xdp_metadata_kfunc_ids)
775 
776 static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
777 	.owner = THIS_MODULE,
778 	.set   = &xdp_metadata_kfunc_ids,
779 };
780 
781 BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
782 #define XDP_METADATA_KFUNC(name, _, str, __) BTF_ID(func, str)
783 XDP_METADATA_KFUNC_xxx
784 #undef XDP_METADATA_KFUNC
785 
786 u32 bpf_xdp_metadata_kfunc_id(int id)
787 {
788 	/* xdp_metadata_kfunc_ids is sorted and can't be used */
789 	return xdp_metadata_kfunc_ids_unsorted[id];
790 }
791 
792 bool bpf_dev_bound_kfunc_id(u32 btf_id)
793 {
794 	return btf_id_set8_contains(&xdp_metadata_kfunc_ids, btf_id);
795 }
796 
797 static int __init xdp_metadata_init(void)
798 {
799 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set);
800 }
801 late_initcall(xdp_metadata_init);
802 
803 void xdp_set_features_flag(struct net_device *dev, xdp_features_t val)
804 {
805 	val &= NETDEV_XDP_ACT_MASK;
806 	if (dev->xdp_features == val)
807 		return;
808 
809 	dev->xdp_features = val;
810 
811 	if (dev->reg_state == NETREG_REGISTERED)
812 		call_netdevice_notifiers(NETDEV_XDP_FEAT_CHANGE, dev);
813 }
814 EXPORT_SYMBOL_GPL(xdp_set_features_flag);
815 
816 void xdp_features_set_redirect_target(struct net_device *dev, bool support_sg)
817 {
818 	xdp_features_t val = (dev->xdp_features | NETDEV_XDP_ACT_NDO_XMIT);
819 
820 	if (support_sg)
821 		val |= NETDEV_XDP_ACT_NDO_XMIT_SG;
822 	xdp_set_features_flag(dev, val);
823 }
824 EXPORT_SYMBOL_GPL(xdp_features_set_redirect_target);
825 
826 void xdp_features_clear_redirect_target(struct net_device *dev)
827 {
828 	xdp_features_t val = dev->xdp_features;
829 
830 	val &= ~(NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_NDO_XMIT_SG);
831 	xdp_set_features_flag(dev, val);
832 }
833 EXPORT_SYMBOL_GPL(xdp_features_clear_redirect_target);
834