Skip to content

Commit 539c1fb

Browse files
alobakinkuba-moo
authored andcommitted
xdp: add generic xdp_build_skb_from_buff()
The code which builds an skb from an &xdp_buff keeps multiplying itself around the drivers with almost no changes. Let's try to stop that by adding a generic function. Unlike __xdp_build_skb_from_frame(), always allocate an skbuff head using napi_build_skb() and make use of the available xdp_rxq pointer to assign the Rx queue index. In case of PP-backed buffer, mark the skb to be recycled, as every PP user's been switched to recycle skbs. Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Alexander Lobakin <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 68ddc8a commit 539c1fb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

include/net/xdp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
336336
void xdp_warn(const char *msg, const char *func, const int line);
337337
#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
338338

339+
struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp);
339340
struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
340341
struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
341342
struct sk_buff *skb,

net/core/xdp.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,61 @@ int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp)
629629
}
630630
EXPORT_SYMBOL_GPL(xdp_alloc_skb_bulk);
631631

632+
/**
633+
* xdp_build_skb_from_buff - create an skb from &xdp_buff
634+
* @xdp: &xdp_buff to convert to an skb
635+
*
636+
* Perform common operations to create a new skb to pass up the stack from
637+
* &xdp_buff: allocate an skb head from the NAPI percpu cache, initialize
638+
* skb data pointers and offsets, set the recycle bit if the buff is
639+
* PP-backed, Rx queue index, protocol and update frags info.
640+
*
641+
* Return: new &sk_buff on success, %NULL on error.
642+
*/
643+
struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
644+
{
645+
const struct xdp_rxq_info *rxq = xdp->rxq;
646+
const struct skb_shared_info *sinfo;
647+
struct sk_buff *skb;
648+
u32 nr_frags = 0;
649+
int metalen;
650+
651+
if (unlikely(xdp_buff_has_frags(xdp))) {
652+
sinfo = xdp_get_shared_info_from_buff(xdp);
653+
nr_frags = sinfo->nr_frags;
654+
}
655+
656+
skb = napi_build_skb(xdp->data_hard_start, xdp->frame_sz);
657+
if (unlikely(!skb))
658+
return NULL;
659+
660+
skb_reserve(skb, xdp->data - xdp->data_hard_start);
661+
__skb_put(skb, xdp->data_end - xdp->data);
662+
663+
metalen = xdp->data - xdp->data_meta;
664+
if (metalen > 0)
665+
skb_metadata_set(skb, metalen);
666+
667+
if (rxq->mem.type == MEM_TYPE_PAGE_POOL)
668+
skb_mark_for_recycle(skb);
669+
670+
skb_record_rx_queue(skb, rxq->queue_index);
671+
672+
if (unlikely(nr_frags)) {
673+
u32 tsize;
674+
675+
tsize = sinfo->xdp_frags_truesize ? : nr_frags * xdp->frame_sz;
676+
xdp_update_skb_shared_info(skb, nr_frags,
677+
sinfo->xdp_frags_size, tsize,
678+
xdp_buff_is_frag_pfmemalloc(xdp));
679+
}
680+
681+
skb->protocol = eth_type_trans(skb, rxq->dev);
682+
683+
return skb;
684+
}
685+
EXPORT_SYMBOL_GPL(xdp_build_skb_from_buff);
686+
632687
struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
633688
struct sk_buff *skb,
634689
struct net_device *dev)

0 commit comments

Comments
 (0)