2020-05-21 03:20:53 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include <net/xsk_buff_pool.h>
|
|
|
|
#include <net/xdp_sock.h>
|
2020-08-28 16:26:17 +08:00
|
|
|
#include <net/xdp_sock_drv.h>
|
2020-05-21 03:20:53 +08:00
|
|
|
|
|
|
|
#include "xsk_queue.h"
|
2020-08-28 16:26:17 +08:00
|
|
|
#include "xdp_umem.h"
|
|
|
|
#include "xsk.h"
|
2020-05-21 03:20:53 +08:00
|
|
|
|
2020-08-28 16:26:20 +08:00
|
|
|
void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!xs->tx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
|
|
|
|
list_add_rcu(&xs->tx_list, &pool->xsk_tx_list);
|
|
|
|
spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!xs->tx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
|
|
|
|
list_del_rcu(&xs->tx_list);
|
|
|
|
spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
|
|
|
|
}
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
void xp_destroy(struct xsk_buff_pool *pool)
|
|
|
|
{
|
|
|
|
if (!pool)
|
|
|
|
return;
|
|
|
|
|
2022-01-26 00:04:43 +08:00
|
|
|
kvfree(pool->tx_descs);
|
2020-05-21 03:20:53 +08:00
|
|
|
kvfree(pool->heads);
|
|
|
|
kvfree(pool);
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:37:45 +08:00
|
|
|
int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs)
|
|
|
|
{
|
|
|
|
pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!pool->tx_descs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:17 +08:00
|
|
|
struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
|
|
|
|
struct xdp_umem *umem)
|
2020-05-21 03:20:53 +08:00
|
|
|
{
|
2021-09-22 15:56:06 +08:00
|
|
|
bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
|
2020-05-21 03:20:53 +08:00
|
|
|
struct xsk_buff_pool *pool;
|
|
|
|
struct xdp_buff_xsk *xskb;
|
2021-09-22 15:56:06 +08:00
|
|
|
u32 i, entries;
|
2020-05-21 03:20:53 +08:00
|
|
|
|
2021-09-22 15:56:06 +08:00
|
|
|
entries = unaligned ? umem->chunks : 0;
|
|
|
|
pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL);
|
2020-05-21 03:20:53 +08:00
|
|
|
if (!pool)
|
|
|
|
goto out;
|
|
|
|
|
2020-08-28 16:26:17 +08:00
|
|
|
pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
|
2020-05-21 03:20:53 +08:00
|
|
|
if (!pool->heads)
|
|
|
|
goto out;
|
|
|
|
|
2022-04-25 23:37:45 +08:00
|
|
|
if (xs->tx)
|
|
|
|
if (xp_alloc_tx_descs(pool, xs))
|
2022-01-26 00:04:43 +08:00
|
|
|
goto out;
|
|
|
|
|
2020-08-28 16:26:17 +08:00
|
|
|
pool->chunk_mask = ~((u64)umem->chunk_size - 1);
|
|
|
|
pool->addrs_cnt = umem->size;
|
|
|
|
pool->heads_cnt = umem->chunks;
|
|
|
|
pool->free_heads_cnt = umem->chunks;
|
|
|
|
pool->headroom = umem->headroom;
|
|
|
|
pool->chunk_size = umem->chunk_size;
|
2021-09-22 15:56:06 +08:00
|
|
|
pool->chunk_shift = ffs(umem->chunk_size) - 1;
|
|
|
|
pool->unaligned = unaligned;
|
2020-08-28 16:26:17 +08:00
|
|
|
pool->frame_len = umem->chunk_size - umem->headroom -
|
|
|
|
XDP_PACKET_HEADROOM;
|
2020-08-28 16:26:15 +08:00
|
|
|
pool->umem = umem;
|
2020-08-28 16:26:21 +08:00
|
|
|
pool->addrs = umem->addrs;
|
2023-11-28 03:03:07 +08:00
|
|
|
pool->tx_metadata_len = umem->tx_metadata_len;
|
2023-11-28 03:03:14 +08:00
|
|
|
pool->tx_sw_csum = umem->flags & XDP_UMEM_TX_SW_CSUM;
|
2020-05-21 03:20:53 +08:00
|
|
|
INIT_LIST_HEAD(&pool->free_list);
|
2023-07-19 21:24:08 +08:00
|
|
|
INIT_LIST_HEAD(&pool->xskb_list);
|
2020-08-28 16:26:20 +08:00
|
|
|
INIT_LIST_HEAD(&pool->xsk_tx_list);
|
|
|
|
spin_lock_init(&pool->xsk_tx_list_lock);
|
2020-12-18 21:45:24 +08:00
|
|
|
spin_lock_init(&pool->cq_lock);
|
2020-08-28 16:26:17 +08:00
|
|
|
refcount_set(&pool->users, 1);
|
2020-05-21 03:20:53 +08:00
|
|
|
|
2020-08-28 16:26:18 +08:00
|
|
|
pool->fq = xs->fq_tmp;
|
|
|
|
pool->cq = xs->cq_tmp;
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
for (i = 0; i < pool->free_heads_cnt; i++) {
|
|
|
|
xskb = &pool->heads[i];
|
|
|
|
xskb->pool = pool;
|
2020-08-28 16:26:17 +08:00
|
|
|
xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
|
2021-12-20 23:52:50 +08:00
|
|
|
INIT_LIST_HEAD(&xskb->free_list_node);
|
2023-07-19 21:24:08 +08:00
|
|
|
INIT_LIST_HEAD(&xskb->xskb_list_node);
|
2021-09-22 15:56:06 +08:00
|
|
|
if (pool->unaligned)
|
|
|
|
pool->free_heads[i] = xskb;
|
|
|
|
else
|
|
|
|
xp_init_xskb_addr(xskb, pool, i * pool->chunk_size);
|
2020-05-21 03:20:53 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:21 +08:00
|
|
|
return pool;
|
2020-05-21 03:20:53 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
xp_destroy(pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
|
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
for (i = 0; i < pool->heads_cnt; i++)
|
|
|
|
pool->heads[i].xdp.rxq = rxq;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_set_rxq_info);
|
|
|
|
|
2023-12-06 05:08:36 +08:00
|
|
|
void xp_fill_cb(struct xsk_buff_pool *pool, struct xsk_cb_desc *desc)
|
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
for (i = 0; i < pool->heads_cnt; i++) {
|
|
|
|
struct xdp_buff_xsk *xskb = &pool->heads[i];
|
|
|
|
|
|
|
|
memcpy(xskb->cb + desc->off, desc->src, desc->bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_fill_cb);
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
|
|
|
|
{
|
|
|
|
struct netdev_bpf bpf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
ASSERT_RTNL();
|
|
|
|
|
|
|
|
if (pool->umem->zc) {
|
|
|
|
bpf.command = XDP_SETUP_XSK_POOL;
|
|
|
|
bpf.xsk.pool = NULL;
|
|
|
|
bpf.xsk.queue_id = pool->queue_id;
|
|
|
|
|
|
|
|
err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
WARN(1, "Failed to disable zero-copy!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:24:19 +08:00
|
|
|
#define NETDEV_XDP_ACT_ZC (NETDEV_XDP_ACT_BASIC | \
|
|
|
|
NETDEV_XDP_ACT_REDIRECT | \
|
|
|
|
NETDEV_XDP_ACT_XSK_ZEROCOPY)
|
|
|
|
|
2021-01-22 18:53:50 +08:00
|
|
|
int xp_assign_dev(struct xsk_buff_pool *pool,
|
|
|
|
struct net_device *netdev, u16 queue_id, u16 flags)
|
2020-08-28 16:26:17 +08:00
|
|
|
{
|
|
|
|
bool force_zc, force_copy;
|
|
|
|
struct netdev_bpf bpf;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
ASSERT_RTNL();
|
|
|
|
|
|
|
|
force_zc = flags & XDP_ZEROCOPY;
|
|
|
|
force_copy = flags & XDP_COPY;
|
|
|
|
|
|
|
|
if (force_zc && force_copy)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-08-28 16:26:19 +08:00
|
|
|
if (xsk_get_pool_from_qid(netdev, queue_id))
|
2020-08-28 16:26:17 +08:00
|
|
|
return -EBUSY;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
pool->netdev = netdev;
|
|
|
|
pool->queue_id = queue_id;
|
2020-08-28 16:26:19 +08:00
|
|
|
err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
|
2020-08-28 16:26:17 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2023-09-07 11:50:32 +08:00
|
|
|
if (flags & XDP_USE_SG)
|
|
|
|
pool->umem->flags |= XDP_UMEM_SG_FLAG;
|
|
|
|
|
2020-12-01 02:51:59 +08:00
|
|
|
if (flags & XDP_USE_NEED_WAKEUP)
|
2020-08-28 16:26:19 +08:00
|
|
|
pool->uses_need_wakeup = true;
|
2020-12-01 02:51:59 +08:00
|
|
|
/* Tx needs to be explicitly woken up the first time. Also
|
|
|
|
* for supporting drivers that do not implement this
|
|
|
|
* feature. They will always have to call sendto() or poll().
|
|
|
|
*/
|
|
|
|
pool->cached_need_wakeup = XDP_WAKEUP_TX;
|
2020-08-28 16:26:17 +08:00
|
|
|
|
2020-08-28 16:26:19 +08:00
|
|
|
dev_hold(netdev);
|
|
|
|
|
2020-08-28 16:26:17 +08:00
|
|
|
if (force_copy)
|
|
|
|
/* For copy-mode, we are done. */
|
|
|
|
return 0;
|
|
|
|
|
2023-02-01 18:24:19 +08:00
|
|
|
if ((netdev->xdp_features & NETDEV_XDP_ACT_ZC) != NETDEV_XDP_ACT_ZC) {
|
2020-08-28 16:26:17 +08:00
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
goto err_unreg_pool;
|
|
|
|
}
|
|
|
|
|
2023-07-19 21:24:08 +08:00
|
|
|
if (netdev->xdp_zc_max_segs == 1 && (flags & XDP_USE_SG)) {
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
goto err_unreg_pool;
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:17 +08:00
|
|
|
bpf.command = XDP_SETUP_XSK_POOL;
|
|
|
|
bpf.xsk.pool = pool;
|
|
|
|
bpf.xsk.queue_id = queue_id;
|
|
|
|
|
2020-08-28 16:26:19 +08:00
|
|
|
err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
|
2020-08-28 16:26:17 +08:00
|
|
|
if (err)
|
|
|
|
goto err_unreg_pool;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
if (!pool->dma_pages) {
|
|
|
|
WARN(1, "Driver did not DMA map zero-copy buffers");
|
2020-12-04 18:21:16 +08:00
|
|
|
err = -EINVAL;
|
2020-08-28 16:26:22 +08:00
|
|
|
goto err_unreg_xsk;
|
|
|
|
}
|
2020-08-28 16:26:19 +08:00
|
|
|
pool->umem->zc = true;
|
2020-08-28 16:26:17 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
err_unreg_xsk:
|
|
|
|
xp_disable_drv_zc(pool);
|
2020-08-28 16:26:17 +08:00
|
|
|
err_unreg_pool:
|
|
|
|
if (!force_zc)
|
|
|
|
err = 0; /* fallback to copy mode */
|
2020-11-20 23:14:43 +08:00
|
|
|
if (err) {
|
2020-08-28 16:26:19 +08:00
|
|
|
xsk_clear_pool_at_qid(netdev, queue_id);
|
2020-11-20 23:14:43 +08:00
|
|
|
dev_put(netdev);
|
|
|
|
}
|
2020-08-28 16:26:17 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-09-21 21:57:01 +08:00
|
|
|
int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
|
2020-08-28 16:26:25 +08:00
|
|
|
struct net_device *dev, u16 queue_id)
|
|
|
|
{
|
|
|
|
u16 flags;
|
2022-09-21 21:57:01 +08:00
|
|
|
struct xdp_umem *umem = umem_xs->umem;
|
2020-08-28 16:26:25 +08:00
|
|
|
|
|
|
|
/* One fill and completion ring required for each queue id. */
|
|
|
|
if (!pool->fq || !pool->cq)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
|
2022-09-21 21:57:01 +08:00
|
|
|
if (umem_xs->pool->uses_need_wakeup)
|
2020-08-28 16:26:25 +08:00
|
|
|
flags |= XDP_USE_NEED_WAKEUP;
|
|
|
|
|
2021-01-22 18:53:50 +08:00
|
|
|
return xp_assign_dev(pool, dev, queue_id, flags);
|
2020-08-28 16:26:25 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:17 +08:00
|
|
|
void xp_clear_dev(struct xsk_buff_pool *pool)
|
|
|
|
{
|
2020-08-28 16:26:19 +08:00
|
|
|
if (!pool->netdev)
|
2020-08-28 16:26:17 +08:00
|
|
|
return;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
xp_disable_drv_zc(pool);
|
2020-08-28 16:26:19 +08:00
|
|
|
xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
|
|
|
|
dev_put(pool->netdev);
|
|
|
|
pool->netdev = NULL;
|
2020-08-28 16:26:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xp_release_deferred(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
|
|
|
|
work);
|
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
xp_clear_dev(pool);
|
|
|
|
rtnl_unlock();
|
|
|
|
|
2020-08-28 16:26:18 +08:00
|
|
|
if (pool->fq) {
|
|
|
|
xskq_destroy(pool->fq);
|
|
|
|
pool->fq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->cq) {
|
|
|
|
xskq_destroy(pool->cq);
|
|
|
|
pool->cq = NULL;
|
|
|
|
}
|
|
|
|
|
xsk: Fix umem cleanup bug at socket destruct
Fix a bug that is triggered when a partially setup socket is
destroyed. For a fully setup socket, a socket that has been bound to a
device, the cleanup of the umem is performed at the end of the buffer
pool's cleanup work queue item. This has to be performed in a work
queue, and not in RCU cleanup, as it is doing a vunmap that cannot
execute in interrupt context. However, when a socket has only been
partially set up so that a umem has been created but the buffer pool
has not, the code erroneously directly calls the umem cleanup function
instead of using a work queue, and this leads to a BUG_ON() in
vunmap().
As there in this case is no buffer pool, we cannot use its work queue,
so we need to introduce a work queue for the umem and schedule this for
the cleanup. So in the case there is no pool, we are going to use the
umem's own work queue to schedule the cleanup. But if there is a
pool, the cleanup of the umem is still being performed by the pool's
work queue, as it is important that the umem is cleaned up after the
pool.
Fixes: e5e1a4bc916d ("xsk: Fix possible memory leak at socket close")
Reported-by: Marek Majtyka <marekx.majtyka@intel.com>
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Marek Majtyka <marekx.majtyka@intel.com>
Link: https://lore.kernel.org/bpf/1605873219-21629-1-git-send-email-magnus.karlsson@gmail.com
2020-11-20 19:53:39 +08:00
|
|
|
xdp_put_umem(pool->umem, false);
|
2020-08-28 16:26:17 +08:00
|
|
|
xp_destroy(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_get_pool(struct xsk_buff_pool *pool)
|
|
|
|
{
|
|
|
|
refcount_inc(&pool->users);
|
|
|
|
}
|
|
|
|
|
2020-10-27 20:32:01 +08:00
|
|
|
bool xp_put_pool(struct xsk_buff_pool *pool)
|
2020-08-28 16:26:17 +08:00
|
|
|
{
|
|
|
|
if (!pool)
|
2020-10-27 20:32:01 +08:00
|
|
|
return false;
|
2020-08-28 16:26:17 +08:00
|
|
|
|
|
|
|
if (refcount_dec_and_test(&pool->users)) {
|
|
|
|
INIT_WORK(&pool->work, xp_release_deferred);
|
|
|
|
schedule_work(&pool->work);
|
2020-10-27 20:32:01 +08:00
|
|
|
return true;
|
2020-08-28 16:26:17 +08:00
|
|
|
}
|
2020-10-27 20:32:01 +08:00
|
|
|
|
|
|
|
return false;
|
2020-08-28 16:26:17 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
|
|
|
|
{
|
|
|
|
struct xsk_dma_map *dma_map;
|
|
|
|
|
|
|
|
list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
|
|
|
|
if (dma_map->netdev == pool->netdev)
|
|
|
|
return dma_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
|
|
|
|
u32 nr_pages, struct xdp_umem *umem)
|
|
|
|
{
|
|
|
|
struct xsk_dma_map *dma_map;
|
|
|
|
|
|
|
|
dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
|
|
|
|
if (!dma_map)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
|
2020-09-02 23:07:50 +08:00
|
|
|
if (!dma_map->dma_pages) {
|
2020-08-28 16:26:22 +08:00
|
|
|
kfree(dma_map);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma_map->netdev = netdev;
|
|
|
|
dma_map->dev = dev;
|
|
|
|
dma_map->dma_pages_cnt = nr_pages;
|
2020-09-14 22:50:36 +08:00
|
|
|
refcount_set(&dma_map->users, 1);
|
2020-08-28 16:26:22 +08:00
|
|
|
list_add(&dma_map->list, &umem->xsk_dma_list);
|
|
|
|
return dma_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
|
|
|
|
{
|
|
|
|
list_del(&dma_map->list);
|
|
|
|
kvfree(dma_map->dma_pages);
|
|
|
|
kfree(dma_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
|
2020-05-21 03:20:53 +08:00
|
|
|
{
|
|
|
|
dma_addr_t *dma;
|
|
|
|
u32 i;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
for (i = 0; i < dma_map->dma_pages_cnt; i++) {
|
|
|
|
dma = &dma_map->dma_pages[i];
|
2020-05-21 03:20:53 +08:00
|
|
|
if (*dma) {
|
2022-06-28 17:18:48 +08:00
|
|
|
*dma &= ~XSK_NEXT_PG_CONTIG_MASK;
|
2020-08-28 16:26:22 +08:00
|
|
|
dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
|
2020-05-21 03:20:53 +08:00
|
|
|
DMA_BIDIRECTIONAL, attrs);
|
|
|
|
*dma = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
xp_destroy_dma_map(dma_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
|
|
|
|
{
|
|
|
|
struct xsk_dma_map *dma_map;
|
|
|
|
|
2023-04-24 02:01:56 +08:00
|
|
|
if (!pool->dma_pages)
|
2020-08-28 16:26:22 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
dma_map = xp_find_dma_map(pool);
|
|
|
|
if (!dma_map) {
|
|
|
|
WARN(1, "Could not find dma_map for device");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!refcount_dec_and_test(&dma_map->users))
|
|
|
|
return;
|
|
|
|
|
|
|
|
__xp_dma_unmap(dma_map, attrs);
|
2020-05-21 03:20:53 +08:00
|
|
|
kvfree(pool->dma_pages);
|
2023-04-24 02:01:56 +08:00
|
|
|
pool->dma_pages = NULL;
|
2020-05-21 03:20:53 +08:00
|
|
|
pool->dma_pages_cnt = 0;
|
|
|
|
pool->dev = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_dma_unmap);
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
|
2020-05-21 03:20:53 +08:00
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
|
|
|
|
if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
|
|
|
|
dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
|
2020-05-21 03:20:53 +08:00
|
|
|
else
|
2020-08-28 16:26:22 +08:00
|
|
|
dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
|
2020-05-21 03:20:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
|
|
|
|
{
|
2022-08-12 19:32:59 +08:00
|
|
|
if (!pool->unaligned) {
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
for (i = 0; i < pool->heads_cnt; i++) {
|
|
|
|
struct xdp_buff_xsk *xskb = &pool->heads[i];
|
|
|
|
|
|
|
|
xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
|
|
|
|
if (!pool->dma_pages)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
pool->dev = dma_map->dev;
|
|
|
|
pool->dma_pages_cnt = dma_map->dma_pages_cnt;
|
|
|
|
memcpy(pool->dma_pages, dma_map->dma_pages,
|
|
|
|
pool->dma_pages_cnt * sizeof(*pool->dma_pages));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
|
|
|
|
unsigned long attrs, struct page **pages, u32 nr_pages)
|
|
|
|
{
|
2020-08-28 16:26:22 +08:00
|
|
|
struct xsk_dma_map *dma_map;
|
2020-05-21 03:20:53 +08:00
|
|
|
dma_addr_t dma;
|
2020-08-28 16:26:22 +08:00
|
|
|
int err;
|
2020-05-21 03:20:53 +08:00
|
|
|
u32 i;
|
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
dma_map = xp_find_dma_map(pool);
|
|
|
|
if (dma_map) {
|
|
|
|
err = xp_init_dma_info(pool, dma_map);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2020-05-21 03:20:53 +08:00
|
|
|
|
2020-09-14 22:50:36 +08:00
|
|
|
refcount_inc(&dma_map->users);
|
2020-08-28 16:26:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-21 03:20:53 +08:00
|
|
|
|
2020-08-28 16:26:22 +08:00
|
|
|
dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
|
|
|
|
if (!dma_map)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < dma_map->dma_pages_cnt; i++) {
|
2020-05-21 03:20:53 +08:00
|
|
|
dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
|
|
|
|
DMA_BIDIRECTIONAL, attrs);
|
|
|
|
if (dma_mapping_error(dev, dma)) {
|
2020-08-28 16:26:22 +08:00
|
|
|
__xp_dma_unmap(dma_map, attrs);
|
2020-05-21 03:20:53 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2020-08-28 16:26:22 +08:00
|
|
|
dma_map->dma_pages[i] = dma;
|
2020-05-21 03:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pool->unaligned)
|
2020-08-28 16:26:22 +08:00
|
|
|
xp_check_dma_contiguity(dma_map);
|
|
|
|
|
|
|
|
err = xp_init_dma_info(pool, dma_map);
|
|
|
|
if (err) {
|
|
|
|
__xp_dma_unmap(dma_map, attrs);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_dma_map);
|
|
|
|
|
|
|
|
static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
|
|
|
|
u64 addr)
|
|
|
|
{
|
|
|
|
return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
|
|
|
|
{
|
|
|
|
*addr = xp_unaligned_extract_addr(*addr);
|
|
|
|
if (*addr >= pool->addrs_cnt ||
|
|
|
|
*addr + pool->chunk_size > pool->addrs_cnt ||
|
|
|
|
xp_addr_crosses_non_contig_pg(pool, *addr))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
|
|
|
|
{
|
|
|
|
*addr = xp_aligned_extract_addr(pool, *addr);
|
|
|
|
return *addr < pool->addrs_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb;
|
|
|
|
u64 addr;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
if (pool->free_heads_cnt == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
|
2020-07-08 15:28:33 +08:00
|
|
|
pool->fq->queue_empty_descs++;
|
2020-05-21 03:20:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
|
|
|
|
xp_check_aligned(pool, &addr);
|
|
|
|
if (!ok) {
|
|
|
|
pool->fq->invalid_descs++;
|
|
|
|
xskq_cons_release(pool->fq);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:56:06 +08:00
|
|
|
if (pool->unaligned) {
|
|
|
|
xskb = pool->free_heads[--pool->free_heads_cnt];
|
|
|
|
xp_init_xskb_addr(xskb, pool, addr);
|
2023-04-24 02:01:56 +08:00
|
|
|
if (pool->dma_pages)
|
2021-09-22 15:56:06 +08:00
|
|
|
xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
|
|
|
|
} else {
|
|
|
|
xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
|
2020-05-21 03:20:53 +08:00
|
|
|
}
|
2021-09-22 15:56:06 +08:00
|
|
|
|
|
|
|
xskq_cons_release(pool->fq);
|
2020-05-21 03:20:53 +08:00
|
|
|
return xskb;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb;
|
|
|
|
|
|
|
|
if (!pool->free_list_cnt) {
|
|
|
|
xskb = __xp_alloc(pool);
|
|
|
|
if (!xskb)
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
pool->free_list_cnt--;
|
|
|
|
xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
|
|
|
|
free_list_node);
|
2021-11-11 15:57:07 +08:00
|
|
|
list_del_init(&xskb->free_list_node);
|
2020-05-21 03:20:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
|
|
|
|
xskb->xdp.data_meta = xskb->xdp.data;
|
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-25 03:15:53 +08:00
|
|
|
xskb->xdp.flags = 0;
|
2020-05-21 03:20:53 +08:00
|
|
|
|
2024-05-07 19:20:26 +08:00
|
|
|
if (pool->dev)
|
|
|
|
xp_dma_sync_for_device(pool, xskb->dma, pool->frame_len);
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
return &xskb->xdp;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_alloc);
|
|
|
|
|
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 15:56:02 +08:00
|
|
|
static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
|
|
|
|
{
|
|
|
|
u32 i, cached_cons, nb_entries;
|
|
|
|
|
|
|
|
if (max > pool->free_heads_cnt)
|
|
|
|
max = pool->free_heads_cnt;
|
|
|
|
max = xskq_cons_nb_entries(pool->fq, max);
|
|
|
|
|
|
|
|
cached_cons = pool->fq->cached_cons;
|
|
|
|
nb_entries = max;
|
|
|
|
i = max;
|
|
|
|
while (i--) {
|
|
|
|
struct xdp_buff_xsk *xskb;
|
|
|
|
u64 addr;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
__xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
|
|
|
|
|
|
|
|
ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
|
|
|
|
xp_check_aligned(pool, &addr);
|
|
|
|
if (unlikely(!ok)) {
|
|
|
|
pool->fq->invalid_descs++;
|
|
|
|
nb_entries--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:56:06 +08:00
|
|
|
if (pool->unaligned) {
|
|
|
|
xskb = pool->free_heads[--pool->free_heads_cnt];
|
|
|
|
xp_init_xskb_addr(xskb, pool, addr);
|
2023-04-24 02:01:56 +08:00
|
|
|
if (pool->dma_pages)
|
2021-09-22 15:56:06 +08:00
|
|
|
xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
|
|
|
|
} else {
|
|
|
|
xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
|
|
|
|
}
|
|
|
|
|
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 15:56:02 +08:00
|
|
|
*xdp = &xskb->xdp;
|
|
|
|
xdp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
xskq_cons_release_n(pool->fq, max);
|
|
|
|
return nb_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
|
|
|
|
{
|
|
|
|
struct xdp_buff_xsk *xskb;
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
|
|
|
|
|
|
|
|
i = nb_entries;
|
|
|
|
while (i--) {
|
|
|
|
xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
|
2021-11-11 15:57:07 +08:00
|
|
|
list_del_init(&xskb->free_list_node);
|
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 15:56:02 +08:00
|
|
|
|
|
|
|
*xdp = &xskb->xdp;
|
|
|
|
xdp++;
|
|
|
|
}
|
|
|
|
pool->free_list_cnt -= nb_entries;
|
|
|
|
|
|
|
|
return nb_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
|
|
|
|
{
|
|
|
|
u32 nb_entries1 = 0, nb_entries2;
|
|
|
|
|
2024-05-07 19:20:26 +08:00
|
|
|
if (unlikely(pool->dev && dma_dev_need_sync(pool->dev))) {
|
2022-03-28 22:21:20 +08:00
|
|
|
struct xdp_buff *buff;
|
|
|
|
|
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 15:56:02 +08:00
|
|
|
/* Slow path */
|
2022-03-28 22:21:20 +08:00
|
|
|
buff = xp_alloc(pool);
|
|
|
|
if (buff)
|
|
|
|
*xdp = buff;
|
|
|
|
return !!buff;
|
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 15:56:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(pool->free_list_cnt)) {
|
|
|
|
nb_entries1 = xp_alloc_reused(pool, xdp, max);
|
|
|
|
if (nb_entries1 == max)
|
|
|
|
return nb_entries1;
|
|
|
|
|
|
|
|
max -= nb_entries1;
|
|
|
|
xdp += nb_entries1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
|
|
|
|
if (!nb_entries2)
|
|
|
|
pool->fq->queue_empty_descs++;
|
|
|
|
|
|
|
|
return nb_entries1 + nb_entries2;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_alloc_batch);
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
|
|
|
|
{
|
|
|
|
if (pool->free_list_cnt >= count)
|
|
|
|
return true;
|
|
|
|
return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_can_alloc);
|
|
|
|
|
|
|
|
void xp_free(struct xdp_buff_xsk *xskb)
|
|
|
|
{
|
2021-11-11 15:57:07 +08:00
|
|
|
if (!list_empty(&xskb->free_list_node))
|
|
|
|
return;
|
|
|
|
|
2020-05-21 03:20:53 +08:00
|
|
|
xskb->pool->free_list_cnt++;
|
|
|
|
list_add(&xskb->free_list_node, &xskb->pool->free_list);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_free);
|
|
|
|
|
|
|
|
void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
|
|
|
|
{
|
|
|
|
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
|
|
|
|
return pool->addrs + addr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_raw_get_data);
|
|
|
|
|
|
|
|
dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
|
|
|
|
{
|
|
|
|
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
|
|
|
|
return (pool->dma_pages[addr >> PAGE_SHIFT] &
|
|
|
|
~XSK_NEXT_PG_CONTIG_MASK) +
|
|
|
|
(addr & ~PAGE_MASK);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(xp_raw_get_dma);
|