2020-05-20 21:20:51 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/* Interface for implementing AF_XDP zero-copy support in drivers.
|
|
|
|
* Copyright(c) 2020 Intel Corporation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_XDP_SOCK_DRV_H
|
|
|
|
#define _LINUX_XDP_SOCK_DRV_H
|
|
|
|
|
|
|
|
#include <net/xdp_sock.h>
|
2020-05-20 21:20:53 +02:00
|
|
|
#include <net/xsk_buff_pool.h>
|
2020-05-20 21:20:51 +02:00
|
|
|
|
2022-09-29 00:21:41 -07:00
|
|
|
#define XDP_UMEM_MIN_CHUNK_SHIFT 11
|
|
|
|
#define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
|
|
|
|
|
2023-12-05 22:08:36 +01:00
|
|
|
struct xsk_cb_desc {
|
|
|
|
void *src;
|
|
|
|
u8 off;
|
|
|
|
u8 bytes;
|
|
|
|
};
|
|
|
|
|
2023-12-19 13:02:05 +02:00
|
|
|
#ifdef CONFIG_XDP_SOCKETS
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
|
|
|
|
bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
|
2022-01-25 17:04:43 +01:00
|
|
|
u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
|
2020-08-28 10:26:16 +02:00
|
|
|
void xsk_tx_release(struct xsk_buff_pool *pool);
|
|
|
|
struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
|
|
|
|
u16 queue_id);
|
|
|
|
void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
|
|
|
|
void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
|
|
|
|
void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
|
|
|
|
void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
|
|
|
|
bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
|
2020-05-20 21:20:51 +02:00
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return XDP_PACKET_HEADROOM + pool->headroom;
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return pool->chunk_size;
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
|
2020-05-20 21:20:53 +02:00
|
|
|
struct xdp_rxq_info *rxq)
|
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
xp_set_rxq_info(pool, rxq);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2023-12-05 22:08:36 +01:00
|
|
|
static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
|
|
|
|
struct xsk_cb_desc *desc)
|
|
|
|
{
|
|
|
|
xp_fill_cb(pool, desc);
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
|
2020-05-20 21:20:53 +02:00
|
|
|
unsigned long attrs)
|
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
xp_dma_unmap(pool, attrs);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
|
|
|
|
struct device *dev, unsigned long attrs)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
struct xdp_umem *umem = pool->umem;
|
|
|
|
|
|
|
|
return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
|
|
|
|
|
|
|
|
return xp_get_dma(xskb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
|
|
|
|
|
|
|
|
return xp_get_frame_dma(xskb);
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return xp_alloc(pool);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2024-12-03 18:37:26 +01:00
|
|
|
static inline bool xsk_is_eop_desc(const struct xdp_desc *desc)
|
2023-07-19 15:24:04 +02:00
|
|
|
{
|
|
|
|
return !xp_mb_desc(desc);
|
|
|
|
}
|
|
|
|
|
xsk: Batched buffer allocation for the pool
Add a new driver interface xsk_buff_alloc_batch() offering batched
buffer allocations to improve performance. The new interface takes
three arguments: the buffer pool to allocated from, a pointer to an
array of struct xdp_buff pointers which will contain pointers to the
allocated xdp_buffs, and an unsigned integer specifying the max number
of buffers to allocate. The return value is the actual number of
buffers that the allocator managed to allocate and it will be in the
range 0 <= N <= max, where max is the third parameter to the function.
u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
u32 max);
A second driver interface is also introduced that need to be used in
conjunction with xsk_buff_alloc_batch(). It is a helper that sets the
size of struct xdp_buff and is used by the NIC Rx irq routine when
receiving a packet. This helper sets the three struct members data,
data_meta, and data_end. The two first ones is in the xsk_buff_alloc()
case set in the allocation routine and data_end is set when a packet
is received in the receive irq function. This unfortunately leads to
worse performance since the xdp_buff is touched twice with a long time
period in between leading to an extra cache miss. Instead, we fill out
the xdp_buff with all 3 fields at one single point in time in the
driver, when the size of the packet is known. Hence this helper. Note
that the driver has to use this helper (or set all three fields
itself) when using xsk_buff_alloc_batch(). xsk_buff_alloc() works as
before and does not require this.
void xsk_buff_set_size(struct xdp_buff *xdp, u32 size);
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210922075613.12186-3-magnus.karlsson@gmail.com
2021-09-22 09:56:02 +02:00
|
|
|
/* Returns as many entries as possible up to max. 0 <= N <= max. */
|
|
|
|
static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
|
|
|
|
{
|
|
|
|
return xp_alloc_batch(pool, xdp, max);
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return xp_can_alloc(pool, count);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void xsk_buff_free(struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
|
2023-07-19 15:24:08 +02:00
|
|
|
struct list_head *xskb_list = &xskb->pool->xskb_list;
|
|
|
|
struct xdp_buff_xsk *pos, *tmp;
|
2020-05-20 21:20:53 +02:00
|
|
|
|
2023-07-19 15:24:08 +02:00
|
|
|
if (likely(!xdp_buff_has_frags(xdp)))
|
|
|
|
goto out;
|
|
|
|
|
2024-10-07 14:24:54 +02:00
|
|
|
list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
|
|
|
|
list_del(&pos->list_node);
|
2023-07-19 15:24:08 +02:00
|
|
|
xp_free(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
xdp_get_shared_info_from_buff(xdp)->nr_frags = 0;
|
|
|
|
out:
|
2020-05-20 21:20:53 +02:00
|
|
|
xp_free(xskb);
|
|
|
|
}
|
|
|
|
|
2024-12-18 18:44:32 +01:00
|
|
|
static inline bool xsk_buff_add_frag(struct xdp_buff *head,
|
|
|
|
struct xdp_buff *xdp)
|
2023-07-19 15:24:08 +02:00
|
|
|
{
|
2024-12-18 18:44:32 +01:00
|
|
|
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;
|
2023-07-19 15:24:08 +02:00
|
|
|
|
2024-12-18 18:44:32 +01:00
|
|
|
frag = container_of(xdp, struct xdp_buff_xsk, xdp);
|
2024-10-07 14:24:54 +02:00
|
|
|
list_add_tail(&frag->list_node, &frag->pool->xskb_list);
|
2024-12-18 18:44:32 +01:00
|
|
|
|
|
|
|
return true;
|
2023-07-19 15:24:08 +02:00
|
|
|
}
|
|
|
|
|
2024-12-03 18:37:26 +01:00
|
|
|
static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
|
2023-07-19 15:24:08 +02:00
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
|
|
|
|
struct xdp_buff *ret = NULL;
|
|
|
|
struct xdp_buff_xsk *frag;
|
|
|
|
|
|
|
|
frag = list_first_entry_or_null(&xskb->pool->xskb_list,
|
2024-10-07 14:24:54 +02:00
|
|
|
struct xdp_buff_xsk, list_node);
|
2023-07-19 15:24:08 +02:00
|
|
|
if (frag) {
|
2024-10-07 14:24:54 +02:00
|
|
|
list_del(&frag->list_node);
|
2023-07-19 15:24:08 +02:00
|
|
|
ret = &frag->xdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-01-24 20:15:54 +01:00
|
|
|
static inline void xsk_buff_del_tail(struct xdp_buff *tail)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp);
|
|
|
|
|
2024-10-07 14:24:54 +02:00
|
|
|
list_del(&xskb->list_node);
|
2024-01-24 20:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
|
|
|
|
struct xdp_buff_xsk *frag;
|
|
|
|
|
|
|
|
frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
|
2024-10-07 14:24:54 +02:00
|
|
|
list_node);
|
2024-01-24 20:15:54 +01:00
|
|
|
return &frag->xdp;
|
|
|
|
}
|
|
|
|
|
xsk: Batched buffer allocation for the pool
Add a new driver interface xsk_buff_alloc_batch() offering batched
buffer allocations to improve performance. The new interface takes
three arguments: the buffer pool to allocated from, a pointer to an
array of struct xdp_buff pointers which will contain pointers to the
allocated xdp_buffs, and an unsigned integer specifying the max number
of buffers to allocate. The return value is the actual number of
buffers that the allocator managed to allocate and it will be in the
range 0 <= N <= max, where max is the third parameter to the function.
u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
u32 max);
A second driver interface is also introduced that need to be used in
conjunction with xsk_buff_alloc_batch(). It is a helper that sets the
size of struct xdp_buff and is used by the NIC Rx irq routine when
receiving a packet. This helper sets the three struct members data,
data_meta, and data_end. The two first ones is in the xsk_buff_alloc()
case set in the allocation routine and data_end is set when a packet
is received in the receive irq function. This unfortunately leads to
worse performance since the xdp_buff is touched twice with a long time
period in between leading to an extra cache miss. Instead, we fill out
the xdp_buff with all 3 fields at one single point in time in the
driver, when the size of the packet is known. Hence this helper. Note
that the driver has to use this helper (or set all three fields
itself) when using xsk_buff_alloc_batch(). xsk_buff_alloc() works as
before and does not require this.
void xsk_buff_set_size(struct xdp_buff *xdp, u32 size);
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210922075613.12186-3-magnus.karlsson@gmail.com
2021-09-22 09:56:02 +02:00
|
|
|
static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
|
|
|
|
{
|
|
|
|
xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
|
|
|
|
xdp->data_meta = xdp->data;
|
|
|
|
xdp->data_end = xdp->data + size;
|
xsk: make xsk_buff_pool responsible for clearing xdp_buff::flags
XDP multi-buffer support introduced XDP_FLAGS_HAS_FRAGS flag that is
used by drivers to notify data path whether xdp_buff contains fragments
or not. Data path looks up mentioned flag on first buffer that occupies
the linear part of xdp_buff, so drivers only modify it there. This is
sufficient for SKB and XDP_DRV modes as usually xdp_buff is allocated on
stack or it resides within struct representing driver's queue and
fragments are carried via skb_frag_t structs. IOW, we are dealing with
only one xdp_buff.
ZC mode though relies on list of xdp_buff structs that is carried via
xsk_buff_pool::xskb_list, so ZC data path has to make sure that
fragments do *not* have XDP_FLAGS_HAS_FRAGS set. Otherwise,
xsk_buff_free() could misbehave if it would be executed against xdp_buff
that carries a frag with XDP_FLAGS_HAS_FRAGS flag set. Such scenario can
take place when within supplied XDP program bpf_xdp_adjust_tail() is
used with negative offset that would in turn release the tail fragment
from multi-buffer frame.
Calling xsk_buff_free() on tail fragment with XDP_FLAGS_HAS_FRAGS would
result in releasing all the nodes from xskb_list that were produced by
driver before XDP program execution, which is not what is intended -
only tail fragment should be deleted from xskb_list and then it should
be put onto xsk_buff_pool::free_list. Such multi-buffer frame will never
make it up to user space, so from AF_XDP application POV there would be
no traffic running, however due to free_list getting constantly new
nodes, driver will be able to feed HW Rx queue with recycled buffers.
Bottom line is that instead of traffic being redirected to user space,
it would be continuously dropped.
To fix this, let us clear the mentioned flag on xsk_buff_pool side
during xdp_buff initialization, which is what should have been done
right from the start of XSK multi-buffer support.
Fixes: 1bbc04de607b ("ice: xsk: add RX multi-buffer support")
Fixes: 1c9ba9c14658 ("i40e: xsk: add RX multi-buffer support")
Fixes: 24ea50127ecf ("xsk: support mbuf on ZC RX")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Link: https://lore.kernel.org/r/20240124191602.566724-3-maciej.fijalkowski@intel.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-01-24 20:15:53 +01:00
|
|
|
xdp->flags = 0;
|
xsk: Batched buffer allocation for the pool
Add a new driver interface xsk_buff_alloc_batch() offering batched
buffer allocations to improve performance. The new interface takes
three arguments: the buffer pool to allocated from, a pointer to an
array of struct xdp_buff pointers which will contain pointers to the
allocated xdp_buffs, and an unsigned integer specifying the max number
of buffers to allocate. The return value is the actual number of
buffers that the allocator managed to allocate and it will be in the
range 0 <= N <= max, where max is the third parameter to the function.
u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
u32 max);
A second driver interface is also introduced that need to be used in
conjunction with xsk_buff_alloc_batch(). It is a helper that sets the
size of struct xdp_buff and is used by the NIC Rx irq routine when
receiving a packet. This helper sets the three struct members data,
data_meta, and data_end. The two first ones is in the xsk_buff_alloc()
case set in the allocation routine and data_end is set when a packet
is received in the receive irq function. This unfortunately leads to
worse performance since the xdp_buff is touched twice with a long time
period in between leading to an extra cache miss. Instead, we fill out
the xdp_buff with all 3 fields at one single point in time in the
driver, when the size of the packet is known. Hence this helper. Note
that the driver has to use this helper (or set all three fields
itself) when using xsk_buff_alloc_batch(). xsk_buff_alloc() works as
before and does not require this.
void xsk_buff_set_size(struct xdp_buff *xdp, u32 size);
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210922075613.12186-3-magnus.karlsson@gmail.com
2021-09-22 09:56:02 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
|
|
|
|
u64 addr)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return xp_raw_get_dma(pool, addr);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
return xp_raw_get_data(pool, addr);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
Currently, when your driver supports XSk Tx metadata and you want to
send an XSk frame, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external
xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus
this:
meta = pool->addrs + addr - pool->tx_metadata_len;
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that
in one go. It returns a small structure with 2 fields: DMA address,
filled unconditionally, and metadata pointer, non-NULL only if it's
present and valid. The address correction is performed only once and
you also have only 1 external call per XSk frame, which does all the
calculations and checks outside of your hotpath. You only need to
check `if (ctx.meta)` for the metadata presence.
To not copy any existing code, derive address correction and getting
virtual and DMA address into small helpers. bloat-o-meter reports no
object code changes for the existing functionality.
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://patch.msgid.link/20250206182630.3914318-5-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-02-06 19:26:29 +01:00
|
|
|
/**
|
|
|
|
* xsk_buff_raw_get_ctx - get &xdp_desc context
|
|
|
|
* @pool: XSk buff pool desc address belongs to
|
|
|
|
* @addr: desc address (from userspace)
|
|
|
|
*
|
|
|
|
* Wrapper for xp_raw_get_ctx() to be used in drivers, see its kdoc for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata
|
|
|
|
* pointer, if it is present and valid (initialized to %NULL otherwise).
|
|
|
|
*/
|
|
|
|
static inline struct xdp_desc_ctx
|
|
|
|
xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr)
|
|
|
|
{
|
|
|
|
return xp_raw_get_ctx(pool, addr);
|
|
|
|
}
|
|
|
|
|
2023-11-27 11:03:13 -08:00
|
|
|
#define XDP_TXMD_FLAGS_VALID ( \
|
|
|
|
XDP_TXMD_FLAGS_TIMESTAMP | \
|
|
|
|
XDP_TXMD_FLAGS_CHECKSUM | \
|
2025-02-16 17:34:26 +08:00
|
|
|
XDP_TXMD_FLAGS_LAUNCH_TIME | \
|
2023-11-27 11:03:13 -08:00
|
|
|
0)
|
|
|
|
|
2024-12-03 18:37:26 +01:00
|
|
|
static inline bool
|
|
|
|
xsk_buff_valid_tx_metadata(const struct xsk_tx_metadata *meta)
|
2023-11-27 11:03:13 -08:00
|
|
|
{
|
|
|
|
return !(meta->flags & ~XDP_TXMD_FLAGS_VALID);
|
|
|
|
}
|
|
|
|
|
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
Currently, when your driver supports XSk Tx metadata and you want to
send an XSk frame, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external
xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus
this:
meta = pool->addrs + addr - pool->tx_metadata_len;
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that
in one go. It returns a small structure with 2 fields: DMA address,
filled unconditionally, and metadata pointer, non-NULL only if it's
present and valid. The address correction is performed only once and
you also have only 1 external call per XSk frame, which does all the
calculations and checks outside of your hotpath. You only need to
check `if (ctx.meta)` for the metadata presence.
To not copy any existing code, derive address correction and getting
virtual and DMA address into small helpers. bloat-o-meter reports no
object code changes for the existing functionality.
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://patch.msgid.link/20250206182630.3914318-5-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-02-06 19:26:29 +01:00
|
|
|
static inline struct xsk_tx_metadata *
|
|
|
|
__xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data)
|
2023-11-27 11:03:08 -08:00
|
|
|
{
|
2023-11-27 11:03:13 -08:00
|
|
|
struct xsk_tx_metadata *meta;
|
|
|
|
|
2023-11-27 11:03:08 -08:00
|
|
|
if (!pool->tx_metadata_len)
|
|
|
|
return NULL;
|
|
|
|
|
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
Currently, when your driver supports XSk Tx metadata and you want to
send an XSk frame, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external
xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus
this:
meta = pool->addrs + addr - pool->tx_metadata_len;
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that
in one go. It returns a small structure with 2 fields: DMA address,
filled unconditionally, and metadata pointer, non-NULL only if it's
present and valid. The address correction is performed only once and
you also have only 1 external call per XSk frame, which does all the
calculations and checks outside of your hotpath. You only need to
check `if (ctx.meta)` for the metadata presence.
To not copy any existing code, derive address correction and getting
virtual and DMA address into small helpers. bloat-o-meter reports no
object code changes for the existing functionality.
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://patch.msgid.link/20250206182630.3914318-5-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-02-06 19:26:29 +01:00
|
|
|
meta = data - pool->tx_metadata_len;
|
2023-11-27 11:03:13 -08:00
|
|
|
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
|
|
|
|
return NULL; /* no way to signal the error to the user */
|
|
|
|
|
|
|
|
return meta;
|
2023-11-27 11:03:08 -08:00
|
|
|
}
|
|
|
|
|
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
Currently, when your driver supports XSk Tx metadata and you want to
send an XSk frame, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external
xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus
this:
meta = pool->addrs + addr - pool->tx_metadata_len;
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that
in one go. It returns a small structure with 2 fields: DMA address,
filled unconditionally, and metadata pointer, non-NULL only if it's
present and valid. The address correction is performed only once and
you also have only 1 external call per XSk frame, which does all the
calculations and checks outside of your hotpath. You only need to
check `if (ctx.meta)` for the metadata presence.
To not copy any existing code, derive address correction and getting
virtual and DMA address into small helpers. bloat-o-meter reports no
object code changes for the existing functionality.
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://patch.msgid.link/20250206182630.3914318-5-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-02-06 19:26:29 +01:00
|
|
|
static inline struct xsk_tx_metadata *
|
|
|
|
xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr)
|
|
|
|
{
|
|
|
|
return __xsk_buff_get_metadata(pool, xp_raw_get_data(pool, addr));
|
|
|
|
}
|
|
|
|
|
2024-05-07 13:20:26 +02:00
|
|
|
static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
|
|
|
|
|
|
|
|
xp_dma_sync_for_cpu(xskb);
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
|
2020-05-20 21:20:53 +02:00
|
|
|
dma_addr_t dma,
|
|
|
|
size_t size)
|
|
|
|
{
|
2020-08-28 10:26:16 +02:00
|
|
|
xp_dma_sync_for_device(pool, dma, size);
|
2020-05-20 21:20:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 21:20:51 +02:00
|
|
|
#else
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
|
|
|
|
struct xdp_desc *desc)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-25 17:04:43 +01:00
|
|
|
static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
|
2020-11-16 12:12:46 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_tx_release(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:15 +02:00
|
|
|
static inline struct xsk_buff_pool *
|
2020-08-28 10:26:16 +02:00
|
|
|
xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
|
2020-05-20 21:20:53 +02:00
|
|
|
struct xdp_rxq_info *rxq)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-05 22:08:36 +01:00
|
|
|
static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
|
|
|
|
struct xsk_cb_desc *desc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
|
2020-05-20 21:20:53 +02:00
|
|
|
unsigned long attrs)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
|
|
|
|
struct device *dev, unsigned long attrs)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-12-03 18:37:26 +01:00
|
|
|
static inline bool xsk_is_eop_desc(const struct xdp_desc *desc)
|
2023-07-19 15:24:04 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
xsk: Batched buffer allocation for the pool
Add a new driver interface xsk_buff_alloc_batch() offering batched
buffer allocations to improve performance. The new interface takes
three arguments: the buffer pool to allocated from, a pointer to an
array of struct xdp_buff pointers which will contain pointers to the
allocated xdp_buffs, and an unsigned integer specifying the max number
of buffers to allocate. The return value is the actual number of
buffers that the allocator managed to allocate and it will be in the
range 0 <= N <= max, where max is the third parameter to the function.
u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
u32 max);
A second driver interface is also introduced that need to be used in
conjunction with xsk_buff_alloc_batch(). It is a helper that sets the
size of struct xdp_buff and is used by the NIC Rx irq routine when
receiving a packet. This helper sets the three struct members data,
data_meta, and data_end. The two first ones is in the xsk_buff_alloc()
case set in the allocation routine and data_end is set when a packet
is received in the receive irq function. This unfortunately leads to
worse performance since the xdp_buff is touched twice with a long time
period in between leading to an extra cache miss. Instead, we fill out
the xdp_buff with all 3 fields at one single point in time in the
driver, when the size of the packet is known. Hence this helper. Note
that the driver has to use this helper (or set all three fields
itself) when using xsk_buff_alloc_batch(). xsk_buff_alloc() works as
before and does not require this.
void xsk_buff_set_size(struct xdp_buff *xdp, u32 size);
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210922075613.12186-3-magnus.karlsson@gmail.com
2021-09-22 09:56:02 +02:00
|
|
|
static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void xsk_buff_free(struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-12-18 18:44:32 +01:00
|
|
|
static inline bool xsk_buff_add_frag(struct xdp_buff *head,
|
|
|
|
struct xdp_buff *xdp)
|
2023-07-19 15:24:08 +02:00
|
|
|
{
|
2024-12-18 18:44:32 +01:00
|
|
|
return false;
|
2023-07-19 15:24:08 +02:00
|
|
|
}
|
|
|
|
|
2024-12-03 18:37:26 +01:00
|
|
|
static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
|
2023-07-19 15:24:08 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-01-24 20:15:54 +01:00
|
|
|
static inline void xsk_buff_del_tail(struct xdp_buff *tail)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
xsk: Batched buffer allocation for the pool
Add a new driver interface xsk_buff_alloc_batch() offering batched
buffer allocations to improve performance. The new interface takes
three arguments: the buffer pool to allocated from, a pointer to an
array of struct xdp_buff pointers which will contain pointers to the
allocated xdp_buffs, and an unsigned integer specifying the max number
of buffers to allocate. The return value is the actual number of
buffers that the allocator managed to allocate and it will be in the
range 0 <= N <= max, where max is the third parameter to the function.
u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
u32 max);
A second driver interface is also introduced that need to be used in
conjunction with xsk_buff_alloc_batch(). It is a helper that sets the
size of struct xdp_buff and is used by the NIC Rx irq routine when
receiving a packet. This helper sets the three struct members data,
data_meta, and data_end. The two first ones is in the xsk_buff_alloc()
case set in the allocation routine and data_end is set when a packet
is received in the receive irq function. This unfortunately leads to
worse performance since the xdp_buff is touched twice with a long time
period in between leading to an extra cache miss. Instead, we fill out
the xdp_buff with all 3 fields at one single point in time in the
driver, when the size of the packet is known. Hence this helper. Note
that the driver has to use this helper (or set all three fields
itself) when using xsk_buff_alloc_batch(). xsk_buff_alloc() works as
before and does not require this.
void xsk_buff_set_size(struct xdp_buff *xdp, u32 size);
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20210922075613.12186-3-magnus.karlsson@gmail.com
2021-09-22 09:56:02 +02:00
|
|
|
static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
|
|
|
|
u64 addr)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
Currently, when your driver supports XSk Tx metadata and you want to
send an XSk frame, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external
xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus
this:
meta = pool->addrs + addr - pool->tx_metadata_len;
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that
in one go. It returns a small structure with 2 fields: DMA address,
filled unconditionally, and metadata pointer, non-NULL only if it's
present and valid. The address correction is performed only once and
you also have only 1 external call per XSk frame, which does all the
calculations and checks outside of your hotpath. You only need to
check `if (ctx.meta)` for the metadata presence.
To not copy any existing code, derive address correction and getting
virtual and DMA address into small helpers. bloat-o-meter reports no
object code changes for the existing functionality.
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://patch.msgid.link/20250206182630.3914318-5-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-02-06 19:26:29 +01:00
|
|
|
static inline struct xdp_desc_ctx
|
|
|
|
xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr)
|
|
|
|
{
|
|
|
|
return (struct xdp_desc_ctx){ };
|
|
|
|
}
|
|
|
|
|
2023-11-27 11:03:13 -08:00
|
|
|
static inline bool xsk_buff_valid_tx_metadata(struct xsk_tx_metadata *meta)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
Currently, when your driver supports XSk Tx metadata and you want to
send an XSk frame, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external
xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus
this:
meta = pool->addrs + addr - pool->tx_metadata_len;
if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that
in one go. It returns a small structure with 2 fields: DMA address,
filled unconditionally, and metadata pointer, non-NULL only if it's
present and valid. The address correction is performed only once and
you also have only 1 external call per XSk frame, which does all the
calculations and checks outside of your hotpath. You only need to
check `if (ctx.meta)` for the metadata presence.
To not copy any existing code, derive address correction and getting
virtual and DMA address into small helpers. bloat-o-meter reports no
object code changes for the existing functionality.
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Link: https://patch.msgid.link/20250206182630.3914318-5-aleksander.lobakin@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-02-06 19:26:29 +01:00
|
|
|
static inline struct xsk_tx_metadata *
|
|
|
|
__xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct xsk_tx_metadata *
|
|
|
|
xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr)
|
2023-11-27 11:03:08 -08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-05-07 13:20:26 +02:00
|
|
|
static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
|
2020-05-20 21:20:53 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:26:16 +02:00
|
|
|
static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
|
2020-05-20 21:20:53 +02:00
|
|
|
dma_addr_t dma,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-20 21:20:51 +02:00
|
|
|
#endif /* CONFIG_XDP_SOCKETS */
|
|
|
|
|
|
|
|
#endif /* _LINUX_XDP_SOCK_DRV_H */
|