Skip to content

Commit 7c48cb0

Browse files
LorenzoBianconiAlexei Starovoitov
authored andcommitted
xdp: add frags support to xdp_return_{buff/frame}
Take into account if the received xdp_buff/xdp_frame is non-linear recycling/returning the frame memory to the allocator or into xdp_frame_bulk. Acked-by: Toke Hoiland-Jorgensen <[email protected]> Acked-by: John Fastabend <[email protected]> Signed-off-by: Lorenzo Bianconi <[email protected]> Link: https://lore.kernel.org/r/a961069febc868508ce1bdf5e53a343eb4e57cb2.1642758637.git.lorenzo@kernel.org Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ed7a58c commit 7c48cb0

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

include/net/xdp.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,24 @@ void __xdp_release_frame(void *data, struct xdp_mem_info *mem);
306306
static inline void xdp_release_frame(struct xdp_frame *xdpf)
307307
{
308308
struct xdp_mem_info *mem = &xdpf->mem;
309+
struct skb_shared_info *sinfo;
310+
int i;
309311

310312
/* Curr only page_pool needs this */
311-
if (mem->type == MEM_TYPE_PAGE_POOL)
312-
__xdp_release_frame(xdpf->data, mem);
313+
if (mem->type != MEM_TYPE_PAGE_POOL)
314+
return;
315+
316+
if (likely(!xdp_frame_has_frags(xdpf)))
317+
goto out;
318+
319+
sinfo = xdp_get_shared_info_from_frame(xdpf);
320+
for (i = 0; i < sinfo->nr_frags; i++) {
321+
struct page *page = skb_frag_page(&sinfo->frags[i]);
322+
323+
__xdp_release_frame(page_address(page), mem);
324+
}
325+
out:
326+
__xdp_release_frame(xdpf->data, mem);
313327
}
314328

315329
int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,

net/core/xdp.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,38 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
406406

407407
void xdp_return_frame(struct xdp_frame *xdpf)
408408
{
409+
struct skb_shared_info *sinfo;
410+
int i;
411+
412+
if (likely(!xdp_frame_has_frags(xdpf)))
413+
goto out;
414+
415+
sinfo = xdp_get_shared_info_from_frame(xdpf);
416+
for (i = 0; i < sinfo->nr_frags; i++) {
417+
struct page *page = skb_frag_page(&sinfo->frags[i]);
418+
419+
__xdp_return(page_address(page), &xdpf->mem, false, NULL);
420+
}
421+
out:
409422
__xdp_return(xdpf->data, &xdpf->mem, false, NULL);
410423
}
411424
EXPORT_SYMBOL_GPL(xdp_return_frame);
412425

413426
void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
414427
{
428+
struct skb_shared_info *sinfo;
429+
int i;
430+
431+
if (likely(!xdp_frame_has_frags(xdpf)))
432+
goto out;
433+
434+
sinfo = xdp_get_shared_info_from_frame(xdpf);
435+
for (i = 0; i < sinfo->nr_frags; i++) {
436+
struct page *page = skb_frag_page(&sinfo->frags[i]);
437+
438+
__xdp_return(page_address(page), &xdpf->mem, true, NULL);
439+
}
440+
out:
415441
__xdp_return(xdpf->data, &xdpf->mem, true, NULL);
416442
}
417443
EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
@@ -447,7 +473,7 @@ void xdp_return_frame_bulk(struct xdp_frame *xdpf,
447473
struct xdp_mem_allocator *xa;
448474

449475
if (mem->type != MEM_TYPE_PAGE_POOL) {
450-
__xdp_return(xdpf->data, &xdpf->mem, false, NULL);
476+
xdp_return_frame(xdpf);
451477
return;
452478
}
453479

@@ -466,12 +492,38 @@ void xdp_return_frame_bulk(struct xdp_frame *xdpf,
466492
bq->xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
467493
}
468494

495+
if (unlikely(xdp_frame_has_frags(xdpf))) {
496+
struct skb_shared_info *sinfo;
497+
int i;
498+
499+
sinfo = xdp_get_shared_info_from_frame(xdpf);
500+
for (i = 0; i < sinfo->nr_frags; i++) {
501+
skb_frag_t *frag = &sinfo->frags[i];
502+
503+
bq->q[bq->count++] = skb_frag_address(frag);
504+
if (bq->count == XDP_BULK_QUEUE_SIZE)
505+
xdp_flush_frame_bulk(bq);
506+
}
507+
}
469508
bq->q[bq->count++] = xdpf->data;
470509
}
471510
EXPORT_SYMBOL_GPL(xdp_return_frame_bulk);
472511

473512
void xdp_return_buff(struct xdp_buff *xdp)
474513
{
514+
struct skb_shared_info *sinfo;
515+
int i;
516+
517+
if (likely(!xdp_buff_has_frags(xdp)))
518+
goto out;
519+
520+
sinfo = xdp_get_shared_info_from_buff(xdp);
521+
for (i = 0; i < sinfo->nr_frags; i++) {
522+
struct page *page = skb_frag_page(&sinfo->frags[i]);
523+
524+
__xdp_return(page_address(page), &xdp->rxq->mem, true, xdp);
525+
}
526+
out:
475527
__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp);
476528
}
477529

0 commit comments

Comments
 (0)