Merge branch 'xdp-a-fistful-of-generic-changes-pt-iii'
Alexander Lobakin says: ==================== xdp: a fistful of generic changes pt. III XDP for idpf is currently 5.(6) chapters: * convert Rx to libeth; * convert Tx and stats to libeth; * generic XDP and XSk code changes; * generic XDP and XSk code additions pt. 1; * generic XDP and XSk code additions pt. 2 (you are here); * actual XDP for idpf via new libeth_xdp; * XSk for idpf (via ^). Part III.3 does the following: * adds generic functions to build skbs from xdp_buffs (regular and XSk) and attach frags to xdp_buffs (regular and XSk); * adds helper to optimize XSk xmit in drivers. Everything is prereq for libeth_xdp, but will be useful standalone as well: less code in drivers, faster XSk XDP_PASS, smaller object code. ==================== Link: https://patch.msgid.link/20241218174435.1445282-1-aleksander.lobakin@intel.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
@@ -395,32 +395,6 @@ static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring,
|
||||
WARN_ON_ONCE(1);
|
||||
}
|
||||
|
||||
static int
|
||||
i40e_add_xsk_frag(struct i40e_ring *rx_ring, struct xdp_buff *first,
|
||||
struct xdp_buff *xdp, const unsigned int size)
|
||||
{
|
||||
struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(first);
|
||||
|
||||
if (!xdp_buff_has_frags(first)) {
|
||||
sinfo->nr_frags = 0;
|
||||
sinfo->xdp_frags_size = 0;
|
||||
xdp_buff_set_frags_flag(first);
|
||||
}
|
||||
|
||||
if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS)) {
|
||||
xsk_buff_free(first);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
__skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++,
|
||||
virt_to_page(xdp->data_hard_start),
|
||||
XDP_PACKET_HEADROOM, size);
|
||||
sinfo->xdp_frags_size += size;
|
||||
xsk_buff_add_frag(xdp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
|
||||
* @rx_ring: Rx ring
|
||||
@@ -486,8 +460,10 @@ int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
|
||||
|
||||
if (!first)
|
||||
first = bi;
|
||||
else if (i40e_add_xsk_frag(rx_ring, first, bi, size))
|
||||
else if (!xsk_buff_add_frag(first, bi)) {
|
||||
xsk_buff_free(first);
|
||||
break;
|
||||
}
|
||||
|
||||
if (++next_to_process == count)
|
||||
next_to_process = 0;
|
||||
|
||||
@@ -801,35 +801,6 @@ out_failure:
|
||||
return result;
|
||||
}
|
||||
|
||||
static int
|
||||
ice_add_xsk_frag(struct ice_rx_ring *rx_ring, struct xdp_buff *first,
|
||||
struct xdp_buff *xdp, const unsigned int size)
|
||||
{
|
||||
struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(first);
|
||||
|
||||
if (!size)
|
||||
return 0;
|
||||
|
||||
if (!xdp_buff_has_frags(first)) {
|
||||
sinfo->nr_frags = 0;
|
||||
sinfo->xdp_frags_size = 0;
|
||||
xdp_buff_set_frags_flag(first);
|
||||
}
|
||||
|
||||
if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS)) {
|
||||
xsk_buff_free(first);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
__skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++,
|
||||
virt_to_page(xdp->data_hard_start),
|
||||
XDP_PACKET_HEADROOM, size);
|
||||
sinfo->xdp_frags_size += size;
|
||||
xsk_buff_add_frag(xdp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ice_clean_rx_irq_zc - consumes packets from the hardware ring
|
||||
* @rx_ring: AF_XDP Rx ring
|
||||
@@ -895,7 +866,8 @@ int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring,
|
||||
|
||||
if (!first) {
|
||||
first = xdp;
|
||||
} else if (ice_add_xsk_frag(rx_ring, first, xdp, size)) {
|
||||
} else if (likely(size) && !xsk_buff_add_frag(first, xdp)) {
|
||||
xsk_buff_free(first);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -608,11 +608,19 @@ struct skb_shared_info {
|
||||
* Warning : all fields before dataref are cleared in __alloc_skb()
|
||||
*/
|
||||
atomic_t dataref;
|
||||
unsigned int xdp_frags_size;
|
||||
|
||||
/* Intermediate layers must ensure that destructor_arg
|
||||
* remains valid until skb destructor */
|
||||
void * destructor_arg;
|
||||
union {
|
||||
struct {
|
||||
u32 xdp_frags_size;
|
||||
u32 xdp_frags_truesize;
|
||||
};
|
||||
|
||||
/*
|
||||
* Intermediate layers must ensure that destructor_arg
|
||||
* remains valid until skb destructor.
|
||||
*/
|
||||
void *destructor_arg;
|
||||
};
|
||||
|
||||
/* must be last field, see pskb_expand_head() */
|
||||
skb_frag_t frags[MAX_SKB_FRAGS];
|
||||
|
||||
@@ -144,6 +144,15 @@ static inline netmem_ref page_pool_alloc_netmem(struct page_pool *pool,
|
||||
return netmem;
|
||||
}
|
||||
|
||||
static inline netmem_ref page_pool_dev_alloc_netmem(struct page_pool *pool,
|
||||
unsigned int *offset,
|
||||
unsigned int *size)
|
||||
{
|
||||
gfp_t gfp = GFP_ATOMIC | __GFP_NOWARN;
|
||||
|
||||
return page_pool_alloc_netmem(pool, offset, size, gfp);
|
||||
}
|
||||
|
||||
static inline struct page *page_pool_alloc(struct page_pool *pool,
|
||||
unsigned int *offset,
|
||||
unsigned int *size, gfp_t gfp)
|
||||
|
||||
+97
-1
@@ -167,6 +167,93 @@ out:
|
||||
return len;
|
||||
}
|
||||
|
||||
void xdp_return_frag(netmem_ref netmem, const struct xdp_buff *xdp);
|
||||
|
||||
/**
|
||||
* __xdp_buff_add_frag - attach frag to &xdp_buff
|
||||
* @xdp: XDP buffer to attach the frag to
|
||||
* @netmem: network memory containing the frag
|
||||
* @offset: offset at which the frag starts
|
||||
* @size: size of the frag
|
||||
* @truesize: total memory size occupied by the frag
|
||||
* @try_coalesce: whether to try coalescing the frags (not valid for XSk)
|
||||
*
|
||||
* Attach frag to the XDP buffer. If it currently has no frags attached,
|
||||
* initialize the related fields, otherwise check that the frag number
|
||||
* didn't reach the limit of ``MAX_SKB_FRAGS``. If possible, try coalescing
|
||||
* the frag with the previous one.
|
||||
* The function doesn't check/update the pfmemalloc bit. Please use the
|
||||
* non-underscored wrapper in drivers.
|
||||
*
|
||||
* Return: true on success, false if there's no space for the frag in
|
||||
* the shared info struct.
|
||||
*/
|
||||
static inline bool __xdp_buff_add_frag(struct xdp_buff *xdp, netmem_ref netmem,
|
||||
u32 offset, u32 size, u32 truesize,
|
||||
bool try_coalesce)
|
||||
{
|
||||
struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
|
||||
skb_frag_t *prev;
|
||||
u32 nr_frags;
|
||||
|
||||
if (!xdp_buff_has_frags(xdp)) {
|
||||
xdp_buff_set_frags_flag(xdp);
|
||||
|
||||
nr_frags = 0;
|
||||
sinfo->xdp_frags_size = 0;
|
||||
sinfo->xdp_frags_truesize = 0;
|
||||
|
||||
goto fill;
|
||||
}
|
||||
|
||||
nr_frags = sinfo->nr_frags;
|
||||
prev = &sinfo->frags[nr_frags - 1];
|
||||
|
||||
if (try_coalesce && netmem == skb_frag_netmem(prev) &&
|
||||
offset == skb_frag_off(prev) + skb_frag_size(prev)) {
|
||||
skb_frag_size_add(prev, size);
|
||||
/* Guaranteed to only decrement the refcount */
|
||||
xdp_return_frag(netmem, xdp);
|
||||
} else if (unlikely(nr_frags == MAX_SKB_FRAGS)) {
|
||||
return false;
|
||||
} else {
|
||||
fill:
|
||||
__skb_fill_netmem_desc_noacc(sinfo, nr_frags++, netmem,
|
||||
offset, size);
|
||||
}
|
||||
|
||||
sinfo->nr_frags = nr_frags;
|
||||
sinfo->xdp_frags_size += size;
|
||||
sinfo->xdp_frags_truesize += truesize;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* xdp_buff_add_frag - attach frag to &xdp_buff
|
||||
* @xdp: XDP buffer to attach the frag to
|
||||
* @netmem: network memory containing the frag
|
||||
* @offset: offset at which the frag starts
|
||||
* @size: size of the frag
|
||||
* @truesize: total memory size occupied by the frag
|
||||
*
|
||||
* Version of __xdp_buff_add_frag() which takes care of the pfmemalloc bit.
|
||||
*
|
||||
* Return: true on success, false if there's no space for the frag in
|
||||
* the shared info struct.
|
||||
*/
|
||||
static inline bool xdp_buff_add_frag(struct xdp_buff *xdp, netmem_ref netmem,
|
||||
u32 offset, u32 size, u32 truesize)
|
||||
{
|
||||
if (!__xdp_buff_add_frag(xdp, netmem, offset, size, truesize, true))
|
||||
return false;
|
||||
|
||||
if (unlikely(netmem_is_pfmemalloc(netmem)))
|
||||
xdp_buff_set_frag_pfmemalloc(xdp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct xdp_frame {
|
||||
void *data;
|
||||
u32 len;
|
||||
@@ -230,7 +317,14 @@ xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
|
||||
unsigned int size, unsigned int truesize,
|
||||
bool pfmemalloc)
|
||||
{
|
||||
skb_shinfo(skb)->nr_frags = nr_frags;
|
||||
struct skb_shared_info *sinfo = skb_shinfo(skb);
|
||||
|
||||
sinfo->nr_frags = nr_frags;
|
||||
/*
|
||||
* ``destructor_arg`` is unionized with ``xdp_frags_{,true}size``,
|
||||
* reset it after that these fields aren't used anymore.
|
||||
*/
|
||||
sinfo->destructor_arg = NULL;
|
||||
|
||||
skb->len += size;
|
||||
skb->data_len += size;
|
||||
@@ -242,6 +336,8 @@ xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,
|
||||
void xdp_warn(const char *msg, const char *func, const int line);
|
||||
#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
|
||||
|
||||
struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp);
|
||||
struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp);
|
||||
struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
|
||||
struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
|
||||
struct sk_buff *skb,
|
||||
|
||||
@@ -136,11 +136,21 @@ out:
|
||||
xp_free(xskb);
|
||||
}
|
||||
|
||||
static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
|
||||
static inline bool xsk_buff_add_frag(struct xdp_buff *head,
|
||||
struct xdp_buff *xdp)
|
||||
{
|
||||
struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp);
|
||||
const void *data = xdp->data;
|
||||
struct xdp_buff_xsk *frag;
|
||||
|
||||
if (!__xdp_buff_add_frag(head, virt_to_netmem(data),
|
||||
offset_in_page(data), xdp->data_end - data,
|
||||
xdp->frame_sz, false))
|
||||
return false;
|
||||
|
||||
frag = container_of(xdp, struct xdp_buff_xsk, xdp);
|
||||
list_add_tail(&frag->list_node, &frag->pool->xskb_list);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
|
||||
@@ -357,8 +367,10 @@ static inline void xsk_buff_free(struct xdp_buff *xdp)
|
||||
{
|
||||
}
|
||||
|
||||
static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
|
||||
static inline bool xsk_buff_add_frag(struct xdp_buff *head,
|
||||
struct xdp_buff *xdp)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
|
||||
|
||||
+178
@@ -535,6 +535,17 @@ void xdp_return_frame_bulk(struct xdp_frame *xdpf,
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xdp_return_frame_bulk);
|
||||
|
||||
/**
|
||||
* xdp_return_frag -- free one XDP frag or decrement its refcount
|
||||
* @netmem: network memory reference to release
|
||||
* @xdp: &xdp_buff to release the frag for
|
||||
*/
|
||||
void xdp_return_frag(netmem_ref netmem, const struct xdp_buff *xdp)
|
||||
{
|
||||
__xdp_return(netmem, xdp->rxq->mem.type, true, NULL);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xdp_return_frag);
|
||||
|
||||
void xdp_return_buff(struct xdp_buff *xdp)
|
||||
{
|
||||
struct skb_shared_info *sinfo;
|
||||
@@ -618,6 +629,173 @@ int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp)
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xdp_alloc_skb_bulk);
|
||||
|
||||
/**
|
||||
* xdp_build_skb_from_buff - create an skb from &xdp_buff
|
||||
* @xdp: &xdp_buff to convert to an skb
|
||||
*
|
||||
* Perform common operations to create a new skb to pass up the stack from
|
||||
* &xdp_buff: allocate an skb head from the NAPI percpu cache, initialize
|
||||
* skb data pointers and offsets, set the recycle bit if the buff is
|
||||
* PP-backed, Rx queue index, protocol and update frags info.
|
||||
*
|
||||
* Return: new &sk_buff on success, %NULL on error.
|
||||
*/
|
||||
struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
|
||||
{
|
||||
const struct xdp_rxq_info *rxq = xdp->rxq;
|
||||
const struct skb_shared_info *sinfo;
|
||||
struct sk_buff *skb;
|
||||
u32 nr_frags = 0;
|
||||
int metalen;
|
||||
|
||||
if (unlikely(xdp_buff_has_frags(xdp))) {
|
||||
sinfo = xdp_get_shared_info_from_buff(xdp);
|
||||
nr_frags = sinfo->nr_frags;
|
||||
}
|
||||
|
||||
skb = napi_build_skb(xdp->data_hard_start, xdp->frame_sz);
|
||||
if (unlikely(!skb))
|
||||
return NULL;
|
||||
|
||||
skb_reserve(skb, xdp->data - xdp->data_hard_start);
|
||||
__skb_put(skb, xdp->data_end - xdp->data);
|
||||
|
||||
metalen = xdp->data - xdp->data_meta;
|
||||
if (metalen > 0)
|
||||
skb_metadata_set(skb, metalen);
|
||||
|
||||
if (rxq->mem.type == MEM_TYPE_PAGE_POOL)
|
||||
skb_mark_for_recycle(skb);
|
||||
|
||||
skb_record_rx_queue(skb, rxq->queue_index);
|
||||
|
||||
if (unlikely(nr_frags)) {
|
||||
u32 tsize;
|
||||
|
||||
tsize = sinfo->xdp_frags_truesize ? : nr_frags * xdp->frame_sz;
|
||||
xdp_update_skb_shared_info(skb, nr_frags,
|
||||
sinfo->xdp_frags_size, tsize,
|
||||
xdp_buff_is_frag_pfmemalloc(xdp));
|
||||
}
|
||||
|
||||
skb->protocol = eth_type_trans(skb, rxq->dev);
|
||||
|
||||
return skb;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xdp_build_skb_from_buff);
|
||||
|
||||
/**
|
||||
* xdp_copy_frags_from_zc - copy frags from XSk buff to skb
|
||||
* @skb: skb to copy frags to
|
||||
* @xdp: XSk &xdp_buff from which the frags will be copied
|
||||
* @pp: &page_pool backing page allocation, if available
|
||||
*
|
||||
* Copy all frags from XSk &xdp_buff to the skb to pass it up the stack.
|
||||
* Allocate a new buffer for each frag, copy it and attach to the skb.
|
||||
*
|
||||
* Return: true on success, false on netmem allocation fail.
|
||||
*/
|
||||
static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
|
||||
const struct xdp_buff *xdp,
|
||||
struct page_pool *pp)
|
||||
{
|
||||
struct skb_shared_info *sinfo = skb_shinfo(skb);
|
||||
const struct skb_shared_info *xinfo;
|
||||
u32 nr_frags, tsize = 0;
|
||||
bool pfmemalloc = false;
|
||||
|
||||
xinfo = xdp_get_shared_info_from_buff(xdp);
|
||||
nr_frags = xinfo->nr_frags;
|
||||
|
||||
for (u32 i = 0; i < nr_frags; i++) {
|
||||
u32 len = skb_frag_size(&xinfo->frags[i]);
|
||||
u32 offset, truesize = len;
|
||||
netmem_ref netmem;
|
||||
|
||||
netmem = page_pool_dev_alloc_netmem(pp, &offset, &truesize);
|
||||
if (unlikely(!netmem)) {
|
||||
sinfo->nr_frags = i;
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(__netmem_address(netmem),
|
||||
__netmem_address(xinfo->frags[i].netmem),
|
||||
LARGEST_ALIGN(len));
|
||||
__skb_fill_netmem_desc_noacc(sinfo, i, netmem, offset, len);
|
||||
|
||||
tsize += truesize;
|
||||
pfmemalloc |= netmem_is_pfmemalloc(netmem);
|
||||
}
|
||||
|
||||
xdp_update_skb_shared_info(skb, nr_frags, xinfo->xdp_frags_size,
|
||||
tsize, pfmemalloc);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* xdp_build_skb_from_zc - create an skb from XSk &xdp_buff
|
||||
* @xdp: source XSk buff
|
||||
*
|
||||
* Similar to xdp_build_skb_from_buff(), but for XSk frames. Allocate an skb
|
||||
* head, new buffer for the head, copy the data and initialize the skb fields.
|
||||
* If there are frags, allocate new buffers for them and copy.
|
||||
* Buffers are allocated from the system percpu pools to try recycling them.
|
||||
* If new skb was built successfully, @xdp is returned to XSk pool's freelist.
|
||||
* On error, it remains untouched and the caller must take care of this.
|
||||
*
|
||||
* Return: new &sk_buff on success, %NULL on error.
|
||||
*/
|
||||
struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)
|
||||
{
|
||||
struct page_pool *pp = this_cpu_read(system_page_pool);
|
||||
const struct xdp_rxq_info *rxq = xdp->rxq;
|
||||
u32 len = xdp->data_end - xdp->data_meta;
|
||||
u32 truesize = xdp->frame_sz;
|
||||
struct sk_buff *skb;
|
||||
int metalen;
|
||||
void *data;
|
||||
|
||||
if (!IS_ENABLED(CONFIG_PAGE_POOL))
|
||||
return NULL;
|
||||
|
||||
data = page_pool_dev_alloc_va(pp, &truesize);
|
||||
if (unlikely(!data))
|
||||
return NULL;
|
||||
|
||||
skb = napi_build_skb(data, truesize);
|
||||
if (unlikely(!skb)) {
|
||||
page_pool_free_va(pp, data, true);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
skb_mark_for_recycle(skb);
|
||||
skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
|
||||
|
||||
memcpy(__skb_put(skb, len), xdp->data_meta, LARGEST_ALIGN(len));
|
||||
|
||||
metalen = xdp->data - xdp->data_meta;
|
||||
if (metalen > 0) {
|
||||
skb_metadata_set(skb, metalen);
|
||||
__skb_pull(skb, metalen);
|
||||
}
|
||||
|
||||
skb_record_rx_queue(skb, rxq->queue_index);
|
||||
|
||||
if (unlikely(xdp_buff_has_frags(xdp)) &&
|
||||
unlikely(!xdp_copy_frags_from_zc(skb, xdp, pp))) {
|
||||
napi_consume_skb(skb, true);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
xsk_buff_free(xdp);
|
||||
|
||||
skb->protocol = eth_type_trans(skb, rxq->dev);
|
||||
|
||||
return skb;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xdp_build_skb_from_zc);
|
||||
|
||||
struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
|
||||
struct sk_buff *skb,
|
||||
struct net_device *dev)
|
||||
|
||||
Reference in New Issue
Block a user