Linux Audio

Check our new training course

Loading...
v6.13.7
  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
 30static DEFINE_IDA(mem_id_pool);
 31static DEFINE_MUTEX(mem_id_lock);
 32#define MEM_ID_MAX 0xFFFE
 33#define MEM_ID_MIN 1
 34static int mem_id_next = MEM_ID_MIN;
 35
 36static bool mem_id_init; /* false */
 37static struct rhashtable *mem_id_ht;
 38
 39static 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
 51static 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
 60static 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
 72static 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
 84static 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
 92static 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
116void 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}
134EXPORT_SYMBOL_GPL(xdp_unreg_mem_model);
135
136void 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}
145EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
146
 
 
 
 
147void 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}
158EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
159
160static 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 */
166int __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}
194EXPORT_SYMBOL_GPL(__xdp_rxq_info_reg);
195
196void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
197{
198	xdp_rxq->reg_state = REG_STATE_UNUSED;
199}
200EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
201
202bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
203{
204	return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
205}
206EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
207
208static 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 */
237static int __mem_id_cyclic_get(gfp_t gfp)
238{
239	int retries = 1;
240	int id;
241
242again:
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
259static 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
270static 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;
 
328err:
329	mutex_unlock(&mem_id_lock);
330	kfree(xdp_alloc);
331	return ERR_PTR(errno);
332}
333
334int 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}
344EXPORT_SYMBOL_GPL(xdp_reg_mem_model);
345
346int 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
365EXPORT_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 */
373void __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
406void 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	}
420out:
421	__xdp_return(xdpf->data, &xdpf->mem, false, NULL);
422}
423EXPORT_SYMBOL_GPL(xdp_return_frame);
424
425void 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	}
439out:
440	__xdp_return(xdpf->data, &xdpf->mem, true, NULL);
441}
442EXPORT_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 */
454void 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}
465EXPORT_SYMBOL_GPL(xdp_flush_frame_bulk);
466
467/* Must be called with rcu_read_lock held */
468void 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}
509EXPORT_SYMBOL_GPL(xdp_return_frame_bulk);
510
511void 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	}
525out:
526	__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp);
527}
528EXPORT_SYMBOL_GPL(xdp_return_buff);
529
530void 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}
538EXPORT_SYMBOL_GPL(xdp_attachment_setup);
539
540struct 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}
577EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame);
578
579/* Used by XDP_WARN macro, to avoid inlining WARN() in fast-path */
580void 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};
584EXPORT_SYMBOL_GPL(xdp_warn);
585
586int 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}
594EXPORT_SYMBOL_GPL(xdp_alloc_skb_bulk);
595
596struct 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}
650EXPORT_SYMBOL_GPL(__xdp_build_skb_from_frame);
651
652struct 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}
665EXPORT_SYMBOL_GPL(xdp_build_skb_from_frame);
666
667struct 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
770BTF_KFUNCS_START(xdp_metadata_kfunc_ids)
771#define XDP_METADATA_KFUNC(_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
772XDP_METADATA_KFUNC_xxx
773#undef XDP_METADATA_KFUNC
774BTF_KFUNCS_END(xdp_metadata_kfunc_ids)
775
776static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
777	.owner = THIS_MODULE,
778	.set   = &xdp_metadata_kfunc_ids,
779};
780
781BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
782#define XDP_METADATA_KFUNC(name, _, str, __) BTF_ID(func, str)
783XDP_METADATA_KFUNC_xxx
784#undef XDP_METADATA_KFUNC
785
786u32 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
792bool bpf_dev_bound_kfunc_id(u32 btf_id)
793{
794	return btf_id_set8_contains(&xdp_metadata_kfunc_ids, btf_id);
795}
796
797static int __init xdp_metadata_init(void)
798{
799	return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set);
800}
801late_initcall(xdp_metadata_init);
802
803void 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}
814EXPORT_SYMBOL_GPL(xdp_set_features_flag);
815
816void 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}
824EXPORT_SYMBOL_GPL(xdp_features_set_redirect_target);
825
826void 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}
833EXPORT_SYMBOL_GPL(xdp_features_clear_redirect_target);
v5.4
  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/filter.h>
  8#include <linux/types.h>
  9#include <linux/mm.h>
 10#include <linux/netdevice.h>
 11#include <linux/slab.h>
 12#include <linux/idr.h>
 13#include <linux/rhashtable.h>
 14#include <net/page_pool.h>
 
 15
 
 16#include <net/xdp.h>
 17#include <net/xdp_priv.h> /* struct xdp_mem_allocator */
 18#include <trace/events/xdp.h>
 
 19
 20#define REG_STATE_NEW		0x0
 21#define REG_STATE_REGISTERED	0x1
 22#define REG_STATE_UNREGISTERED	0x2
 23#define REG_STATE_UNUSED	0x3
 24
 25static DEFINE_IDA(mem_id_pool);
 26static DEFINE_MUTEX(mem_id_lock);
 27#define MEM_ID_MAX 0xFFFE
 28#define MEM_ID_MIN 1
 29static int mem_id_next = MEM_ID_MIN;
 30
 31static bool mem_id_init; /* false */
 32static struct rhashtable *mem_id_ht;
 33
 34static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed)
 35{
 36	const u32 *k = data;
 37	const u32 key = *k;
 38
 39	BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_mem_allocator, mem.id)
 40		     != sizeof(u32));
 41
 42	/* Use cyclic increasing ID as direct hash key */
 43	return key;
 44}
 45
 46static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg,
 47			  const void *ptr)
 48{
 49	const struct xdp_mem_allocator *xa = ptr;
 50	u32 mem_id = *(u32 *)arg->key;
 51
 52	return xa->mem.id != mem_id;
 53}
 54
 55static const struct rhashtable_params mem_id_rht_params = {
 56	.nelem_hint = 64,
 57	.head_offset = offsetof(struct xdp_mem_allocator, node),
 58	.key_offset  = offsetof(struct xdp_mem_allocator, mem.id),
 59	.key_len = FIELD_SIZEOF(struct xdp_mem_allocator, mem.id),
 60	.max_size = MEM_ID_MAX,
 61	.min_size = 8,
 62	.automatic_shrinking = true,
 63	.hashfn    = xdp_mem_id_hashfn,
 64	.obj_cmpfn = xdp_mem_id_cmp,
 65};
 66
 67static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu)
 68{
 69	struct xdp_mem_allocator *xa;
 70
 71	xa = container_of(rcu, struct xdp_mem_allocator, rcu);
 72
 73	/* Allocator have indicated safe to remove before this is called */
 74	if (xa->mem.type == MEM_TYPE_PAGE_POOL)
 75		page_pool_free(xa->page_pool);
 76
 77	/* Allow this ID to be reused */
 78	ida_simple_remove(&mem_id_pool, xa->mem.id);
 79
 80	/* Poison memory */
 81	xa->mem.id = 0xFFFF;
 82	xa->mem.type = 0xF0F0;
 83	xa->allocator = (void *)0xDEAD9001;
 84
 85	kfree(xa);
 
 
 
 
 
 86}
 87
 88static bool __mem_id_disconnect(int id, bool force)
 89{
 90	struct xdp_mem_allocator *xa;
 91	bool safe_to_remove = true;
 92
 93	mutex_lock(&mem_id_lock);
 94
 95	xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
 96	if (!xa) {
 97		mutex_unlock(&mem_id_lock);
 98		WARN(1, "Request remove non-existing id(%d), driver bug?", id);
 99		return true;
100	}
101	xa->disconnect_cnt++;
 
102
103	/* Detects in-flight packet-pages for page_pool */
104	if (xa->mem.type == MEM_TYPE_PAGE_POOL)
105		safe_to_remove = page_pool_request_shutdown(xa->page_pool);
106
107	trace_mem_disconnect(xa, safe_to_remove, force);
108
109	if ((safe_to_remove || force) &&
110	    !rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params))
111		call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free);
112
113	mutex_unlock(&mem_id_lock);
114	return (safe_to_remove|force);
115}
116
117#define DEFER_TIME (msecs_to_jiffies(1000))
118#define DEFER_WARN_INTERVAL (30 * HZ)
119#define DEFER_MAX_RETRIES 120
120
121static void mem_id_disconnect_defer_retry(struct work_struct *wq)
122{
123	struct delayed_work *dwq = to_delayed_work(wq);
124	struct xdp_mem_allocator *xa = container_of(dwq, typeof(*xa), defer_wq);
125	bool force = false;
126
127	if (xa->disconnect_cnt > DEFER_MAX_RETRIES)
128		force = true;
 
129
130	if (__mem_id_disconnect(xa->mem.id, force))
131		return;
132
133	/* Periodic warning */
134	if (time_after_eq(jiffies, xa->defer_warn)) {
135		int sec = (s32)((u32)jiffies - (u32)xa->defer_start) / HZ;
136
137		pr_warn("%s() stalled mem.id=%u shutdown %d attempts %d sec\n",
138			__func__, xa->mem.id, xa->disconnect_cnt, sec);
139		xa->defer_warn = jiffies + DEFER_WARN_INTERVAL;
140	}
141
142	/* Still not ready to be disconnected, retry later */
143	schedule_delayed_work(&xa->defer_wq, DEFER_TIME);
144}
 
145
146void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
147{
148	struct xdp_mem_allocator *xa;
149	int id = xdp_rxq->mem.id;
150
151	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
152		WARN(1, "Missing register, driver bug");
153		return;
154	}
155
156	if (xdp_rxq->mem.type != MEM_TYPE_PAGE_POOL &&
157	    xdp_rxq->mem.type != MEM_TYPE_ZERO_COPY) {
158		return;
159	}
160
161	if (id == 0)
162		return;
163
164	if (__mem_id_disconnect(id, false))
165		return;
166
167	/* Could not disconnect, defer new disconnect attempt to later */
168	mutex_lock(&mem_id_lock);
169
170	xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params);
171	if (!xa) {
172		mutex_unlock(&mem_id_lock);
173		return;
174	}
175	xa->defer_start = jiffies;
176	xa->defer_warn  = jiffies + DEFER_WARN_INTERVAL;
177
178	INIT_DELAYED_WORK(&xa->defer_wq, mem_id_disconnect_defer_retry);
179	mutex_unlock(&mem_id_lock);
180	schedule_delayed_work(&xa->defer_wq, DEFER_TIME);
181}
182EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
183
184/* This unregister operation will also cleanup and destroy the
185 * allocator. The page_pool_free() operation is first called when it's
186 * safe to remove, possibly deferred to a workqueue.
187 */
188void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
189{
190	/* Simplify driver cleanup code paths, allow unreg "unused" */
191	if (xdp_rxq->reg_state == REG_STATE_UNUSED)
192		return;
193
194	WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
195
196	xdp_rxq_info_unreg_mem_model(xdp_rxq);
197
198	xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
199	xdp_rxq->dev = NULL;
200
201	/* Reset mem info to defaults */
202	xdp_rxq->mem.id = 0;
203	xdp_rxq->mem.type = 0;
204}
205EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
206
207static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
208{
209	memset(xdp_rxq, 0, sizeof(*xdp_rxq));
210}
211
212/* Returns 0 on success, negative on failure */
213int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
214		     struct net_device *dev, u32 queue_index)
 
215{
 
 
 
 
 
216	if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
217		WARN(1, "Driver promised not to register this");
218		return -EINVAL;
219	}
220
221	if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
222		WARN(1, "Missing unregister, handled but fix driver");
223		xdp_rxq_info_unreg(xdp_rxq);
224	}
225
226	if (!dev) {
227		WARN(1, "Missing net_device from driver");
228		return -ENODEV;
229	}
230
231	/* State either UNREGISTERED or NEW */
232	xdp_rxq_info_init(xdp_rxq);
233	xdp_rxq->dev = dev;
234	xdp_rxq->queue_index = queue_index;
 
235
236	xdp_rxq->reg_state = REG_STATE_REGISTERED;
237	return 0;
238}
239EXPORT_SYMBOL_GPL(xdp_rxq_info_reg);
240
241void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
242{
243	xdp_rxq->reg_state = REG_STATE_UNUSED;
244}
245EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);
246
247bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq)
248{
249	return (xdp_rxq->reg_state == REG_STATE_REGISTERED);
250}
251EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg);
252
253static int __mem_id_init_hash_table(void)
254{
255	struct rhashtable *rht;
256	int ret;
257
258	if (unlikely(mem_id_init))
259		return 0;
260
261	rht = kzalloc(sizeof(*rht), GFP_KERNEL);
262	if (!rht)
263		return -ENOMEM;
264
265	ret = rhashtable_init(rht, &mem_id_rht_params);
266	if (ret < 0) {
267		kfree(rht);
268		return ret;
269	}
270	mem_id_ht = rht;
271	smp_mb(); /* mutex lock should provide enough pairing */
272	mem_id_init = true;
273
274	return 0;
275}
276
277/* Allocate a cyclic ID that maps to allocator pointer.
278 * See: https://www.kernel.org/doc/html/latest/core-api/idr.html
279 *
280 * Caller must lock mem_id_lock.
281 */
282static int __mem_id_cyclic_get(gfp_t gfp)
283{
284	int retries = 1;
285	int id;
286
287again:
288	id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp);
289	if (id < 0) {
290		if (id == -ENOSPC) {
291			/* Cyclic allocator, reset next id */
292			if (retries--) {
293				mem_id_next = MEM_ID_MIN;
294				goto again;
295			}
296		}
297		return id; /* errno */
298	}
299	mem_id_next = id + 1;
300
301	return id;
302}
303
304static bool __is_supported_mem_type(enum xdp_mem_type type)
305{
306	if (type == MEM_TYPE_PAGE_POOL)
307		return is_page_pool_compiled_in();
308
309	if (type >= MEM_TYPE_MAX)
310		return false;
311
312	return true;
313}
314
315int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
316			       enum xdp_mem_type type, void *allocator)
 
317{
318	struct xdp_mem_allocator *xdp_alloc;
319	gfp_t gfp = GFP_KERNEL;
320	int id, errno, ret;
321	void *ptr;
322
323	if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
324		WARN(1, "Missing register, driver bug");
325		return -EFAULT;
326	}
327
328	if (!__is_supported_mem_type(type))
329		return -EOPNOTSUPP;
330
331	xdp_rxq->mem.type = type;
332
333	if (!allocator) {
334		if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY)
335			return -EINVAL; /* Setup time check page_pool req */
336		return 0;
337	}
338
339	/* Delay init of rhashtable to save memory if feature isn't used */
340	if (!mem_id_init) {
341		mutex_lock(&mem_id_lock);
342		ret = __mem_id_init_hash_table();
343		mutex_unlock(&mem_id_lock);
344		if (ret < 0) {
345			WARN_ON(1);
346			return ret;
347		}
348	}
349
350	xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp);
351	if (!xdp_alloc)
352		return -ENOMEM;
353
354	mutex_lock(&mem_id_lock);
355	id = __mem_id_cyclic_get(gfp);
356	if (id < 0) {
357		errno = id;
358		goto err;
359	}
360	xdp_rxq->mem.id = id;
361	xdp_alloc->mem  = xdp_rxq->mem;
362	xdp_alloc->allocator = allocator;
363
364	/* Insert allocator into ID lookup table */
365	ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node);
366	if (IS_ERR(ptr)) {
367		ida_simple_remove(&mem_id_pool, xdp_rxq->mem.id);
368		xdp_rxq->mem.id = 0;
369		errno = PTR_ERR(ptr);
370		goto err;
371	}
372
373	if (type == MEM_TYPE_PAGE_POOL)
374		page_pool_get(xdp_alloc->page_pool);
375
376	mutex_unlock(&mem_id_lock);
377
378	trace_mem_connect(xdp_alloc, xdp_rxq);
379	return 0;
380err:
381	mutex_unlock(&mem_id_lock);
382	kfree(xdp_alloc);
383	return errno;
 
 
 
 
 
 
 
 
 
 
 
384}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
386
387/* XDP RX runs under NAPI protection, and in different delivery error
388 * scenarios (e.g. queue full), it is possible to return the xdp_frame
389 * while still leveraging this protection.  The @napi_direct boolian
390 * is used for those calls sites.  Thus, allowing for faster recycling
391 * of xdp_frames/pages in those cases.
392 */
393static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
394			 unsigned long handle)
395{
396	struct xdp_mem_allocator *xa;
397	struct page *page;
398
399	switch (mem->type) {
400	case MEM_TYPE_PAGE_POOL:
401		rcu_read_lock();
402		/* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
403		xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
404		page = virt_to_head_page(data);
405		if (likely(xa)) {
406			napi_direct &= !xdp_return_frame_no_direct();
407			page_pool_put_page(xa->page_pool, page, napi_direct);
408		} else {
409			/* Hopefully stack show who to blame for late return */
410			WARN_ONCE(1, "page_pool gone mem.id=%d", mem->id);
411			trace_mem_return_failed(mem, page);
412			put_page(page);
413		}
414		rcu_read_unlock();
415		break;
416	case MEM_TYPE_PAGE_SHARED:
417		page_frag_free(data);
418		break;
419	case MEM_TYPE_PAGE_ORDER0:
420		page = virt_to_page(data); /* Assumes order0 page*/
421		put_page(page);
422		break;
423	case MEM_TYPE_ZERO_COPY:
424		/* NB! Only valid from an xdp_buff! */
425		rcu_read_lock();
426		/* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
427		xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
428		xa->zc_alloc->free(xa->zc_alloc, handle);
429		rcu_read_unlock();
430	default:
431		/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
 
432		break;
433	}
434}
435
436void xdp_return_frame(struct xdp_frame *xdpf)
437{
438	__xdp_return(xdpf->data, &xdpf->mem, false, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
439}
440EXPORT_SYMBOL_GPL(xdp_return_frame);
441
442void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
443{
444	__xdp_return(xdpf->data, &xdpf->mem, true, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
445}
446EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
447
448void xdp_return_buff(struct xdp_buff *xdp)
 
 
 
 
 
 
 
 
 
 
449{
450	__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
 
 
 
 
 
 
 
451}
452EXPORT_SYMBOL_GPL(xdp_return_buff);
453
454/* Only called for MEM_TYPE_PAGE_POOL see xdp.h */
455void __xdp_release_frame(void *data, struct xdp_mem_info *mem)
 
456{
 
457	struct xdp_mem_allocator *xa;
458	struct page *page;
459
460	rcu_read_lock();
461	xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
462	page = virt_to_head_page(data);
463	if (xa)
464		page_pool_release_page(xa->page_pool, page);
465	rcu_read_unlock();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466}
467EXPORT_SYMBOL_GPL(__xdp_release_frame);
468
469int xdp_attachment_query(struct xdp_attachment_info *info,
470			 struct netdev_bpf *bpf)
471{
472	bpf->prog_id = info->prog ? info->prog->aux->id : 0;
473	bpf->prog_flags = info->prog ? info->flags : 0;
474	return 0;
475}
476EXPORT_SYMBOL_GPL(xdp_attachment_query);
 
 
 
 
477
478bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
479			     struct netdev_bpf *bpf)
480{
481	if (info->prog && (bpf->flags ^ info->flags) & XDP_FLAGS_MODES) {
482		NL_SET_ERR_MSG(bpf->extack,
483			       "program loaded with different flags");
484		return false;
485	}
486	return true;
 
487}
488EXPORT_SYMBOL_GPL(xdp_attachment_flags_ok);
489
490void xdp_attachment_setup(struct xdp_attachment_info *info,
491			  struct netdev_bpf *bpf)
492{
493	if (info->prog)
494		bpf_prog_put(info->prog);
495	info->prog = bpf->prog;
496	info->flags = bpf->flags;
497}
498EXPORT_SYMBOL_GPL(xdp_attachment_setup);
499
500struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp)
501{
502	unsigned int metasize, totsize;
503	void *addr, *data_to_copy;
504	struct xdp_frame *xdpf;
505	struct page *page;
506
507	/* Clone into a MEM_TYPE_PAGE_ORDER0 xdp_frame. */
508	metasize = xdp_data_meta_unsupported(xdp) ? 0 :
509		   xdp->data - xdp->data_meta;
510	totsize = xdp->data_end - xdp->data + metasize;
511
512	if (sizeof(*xdpf) + totsize > PAGE_SIZE)
513		return NULL;
514
515	page = dev_alloc_page();
516	if (!page)
517		return NULL;
518
519	addr = page_to_virt(page);
520	xdpf = addr;
521	memset(xdpf, 0, sizeof(*xdpf));
522
523	addr += sizeof(*xdpf);
524	data_to_copy = metasize ? xdp->data_meta : xdp->data;
525	memcpy(addr, data_to_copy, totsize);
526
527	xdpf->data = addr + metasize;
528	xdpf->len = totsize - metasize;
529	xdpf->headroom = 0;
530	xdpf->metasize = metasize;
 
531	xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
532
533	xdp_return_buff(xdp);
534	return xdpf;
535}
536EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame);