Skip to content

Commit

Permalink
xsk: Introduce AF_XDP buffer allocation API
Browse files Browse the repository at this point in the history
In order to simplify AF_XDP zero-copy enablement for NIC driver
developers, a new AF_XDP buffer allocation API is added. The
implementation is based on a single core (single producer/consumer)
buffer pool for the AF_XDP UMEM.

A buffer is allocated using the xsk_buff_alloc() function, and
returned using xsk_buff_free(). If a buffer is disassociated with the
pool, e.g. when a buffer is passed to an AF_XDP socket, a buffer is
said to be released. Currently, the release function is only used by
the AF_XDP internals and not visible to the driver.

Drivers using this API should register the XDP memory model with the
new MEM_TYPE_XSK_BUFF_POOL type.

The API is defined in net/xdp_sock_drv.h.

The buffer type is struct xdp_buff, and follows the lifetime of
regular xdp_buffs, i.e.  the lifetime of an xdp_buff is restricted to
a NAPI context. In other words, the API is not replacing xdp_frames.

In addition to introducing the API and implementations, the AF_XDP
core is migrated to use the new APIs.

rfc->v1: Fixed build errors/warnings for m68k and riscv. (kbuild test
         robot)
         Added headroom/chunk size getter. (Maxim/Björn)

v1->v2: Swapped SoBs. (Maxim)

v2->v3: Initialize struct xdp_buff member frame_sz. (Björn)
        Add API to query the DMA address of a frame. (Maxim)
        Do DMA sync for CPU till the end of the frame to handle
        possible growth (frame_sz). (Maxim)

Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
Signed-off-by: Maxim Mikityanskiy <maximmi@mellanox.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200520192103.355233-6-bjorn.topel@gmail.com
  • Loading branch information
Björn Töpel authored and Alexei Starovoitov committed May 22, 2020
1 parent 89e4a37 commit 2b43470
Show file tree
Hide file tree
Showing 12 changed files with 819 additions and 119 deletions.
4 changes: 3 additions & 1 deletion include/net/xdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum xdp_mem_type {
MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
MEM_TYPE_PAGE_POOL,
MEM_TYPE_ZERO_COPY,
MEM_TYPE_XSK_BUFF_POOL,
MEM_TYPE_MAX,
};

Expand Down Expand Up @@ -119,7 +120,8 @@ struct xdp_frame *convert_to_xdp_frame(struct xdp_buff *xdp)
int metasize;
int headroom;

if (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY)
if (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY ||
xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
return xdp_convert_zc_to_xdp_frame(xdp);

/* Assure headroom is available for storing info */
Expand Down
2 changes: 2 additions & 0 deletions include/net/xdp_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ struct xdp_umem_fq_reuse {
struct xdp_umem {
struct xsk_queue *fq;
struct xsk_queue *cq;
struct xsk_buff_pool *pool;
struct xdp_umem_page *pages;
u64 chunk_mask;
u64 size;
u32 headroom;
u32 chunk_size_nohr;
u32 chunk_size;
struct user_struct *user;
refcount_t users;
struct work_struct work;
Expand Down
164 changes: 164 additions & 0 deletions include/net/xdp_sock_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define _LINUX_XDP_SOCK_DRV_H

#include <net/xdp_sock.h>
#include <net/xsk_buff_pool.h>

#ifdef CONFIG_XDP_SOCKETS

Expand Down Expand Up @@ -101,6 +102,94 @@ static inline u32 xsk_umem_xdp_frame_sz(struct xdp_umem *umem)
return umem->chunk_size_nohr;
}

static inline u32 xsk_umem_get_headroom(struct xdp_umem *umem)
{
return XDP_PACKET_HEADROOM + umem->headroom;
}

static inline u32 xsk_umem_get_chunk_size(struct xdp_umem *umem)
{
return umem->chunk_size;
}

static inline u32 xsk_umem_get_rx_frame_size(struct xdp_umem *umem)
{
return xsk_umem_get_chunk_size(umem) - xsk_umem_get_headroom(umem);
}

static inline void xsk_buff_set_rxq_info(struct xdp_umem *umem,
struct xdp_rxq_info *rxq)
{
xp_set_rxq_info(umem->pool, rxq);
}

static inline void xsk_buff_dma_unmap(struct xdp_umem *umem,
unsigned long attrs)
{
xp_dma_unmap(umem->pool, attrs);
}

static inline int xsk_buff_dma_map(struct xdp_umem *umem, struct device *dev,
unsigned long attrs)
{
return xp_dma_map(umem->pool, dev, attrs, umem->pgs, umem->npgs);
}

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);
}

static inline struct xdp_buff *xsk_buff_alloc(struct xdp_umem *umem)
{
return xp_alloc(umem->pool);
}

static inline bool xsk_buff_can_alloc(struct xdp_umem *umem, u32 count)
{
return xp_can_alloc(umem->pool, count);
}

static inline void xsk_buff_free(struct xdp_buff *xdp)
{
struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);

xp_free(xskb);
}

static inline dma_addr_t xsk_buff_raw_get_dma(struct xdp_umem *umem, u64 addr)
{
return xp_raw_get_dma(umem->pool, addr);
}

static inline void *xsk_buff_raw_get_data(struct xdp_umem *umem, u64 addr)
{
return xp_raw_get_data(umem->pool, addr);
}

static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
{
struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);

xp_dma_sync_for_cpu(xskb);
}

static inline void xsk_buff_raw_dma_sync_for_device(struct xdp_umem *umem,
dma_addr_t dma,
size_t size)
{
xp_dma_sync_for_device(umem->pool, dma, size);
}

#else

static inline bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt)
Expand Down Expand Up @@ -212,6 +301,81 @@ static inline u32 xsk_umem_xdp_frame_sz(struct xdp_umem *umem)
return 0;
}

static inline u32 xsk_umem_get_headroom(struct xdp_umem *umem)
{
return 0;
}

static inline u32 xsk_umem_get_chunk_size(struct xdp_umem *umem)
{
return 0;
}

static inline u32 xsk_umem_get_rx_frame_size(struct xdp_umem *umem)
{
return 0;
}

static inline void xsk_buff_set_rxq_info(struct xdp_umem *umem,
struct xdp_rxq_info *rxq)
{
}

static inline void xsk_buff_dma_unmap(struct xdp_umem *umem,
unsigned long attrs)
{
}

static inline int xsk_buff_dma_map(struct xdp_umem *umem, struct device *dev,
unsigned long attrs)
{
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;
}

static inline struct xdp_buff *xsk_buff_alloc(struct xdp_umem *umem)
{
return NULL;
}

static inline bool xsk_buff_can_alloc(struct xdp_umem *umem, u32 count)
{
return false;
}

static inline void xsk_buff_free(struct xdp_buff *xdp)
{
}

static inline dma_addr_t xsk_buff_raw_get_dma(struct xdp_umem *umem, u64 addr)
{
return 0;
}

static inline void *xsk_buff_raw_get_data(struct xdp_umem *umem, u64 addr)
{
return NULL;
}

static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
{
}

static inline void xsk_buff_raw_dma_sync_for_device(struct xdp_umem *umem,
dma_addr_t dma,
size_t size)
{
}

#endif /* CONFIG_XDP_SOCKETS */

#endif /* _LINUX_XDP_SOCK_DRV_H */
56 changes: 56 additions & 0 deletions include/net/xsk_buff_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright(c) 2020 Intel Corporation. */

#ifndef XSK_BUFF_POOL_H_
#define XSK_BUFF_POOL_H_

#include <linux/types.h>
#include <linux/dma-mapping.h>
#include <net/xdp.h>

struct xsk_buff_pool;
struct xdp_rxq_info;
struct xsk_queue;
struct xdp_desc;
struct device;
struct page;

struct xdp_buff_xsk {
struct xdp_buff xdp;
dma_addr_t dma;
dma_addr_t frame_dma;
struct xsk_buff_pool *pool;
bool unaligned;
u64 orig_addr;
struct list_head free_list_node;
};

/* AF_XDP core. */
struct xsk_buff_pool *xp_create(struct page **pages, u32 nr_pages, u32 chunks,
u32 chunk_size, u32 headroom, u64 size,
bool unaligned);
void xp_set_fq(struct xsk_buff_pool *pool, struct xsk_queue *fq);
void xp_destroy(struct xsk_buff_pool *pool);
void xp_release(struct xdp_buff_xsk *xskb);
u64 xp_get_handle(struct xdp_buff_xsk *xskb);
bool xp_validate_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);

/* AF_XDP, and XDP core. */
void xp_free(struct xdp_buff_xsk *xskb);

/* AF_XDP ZC drivers, via xdp_sock_buff.h */
void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
unsigned long attrs, struct page **pages, u32 nr_pages);
void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb);
dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb);
void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb);
void xp_dma_sync_for_device(struct xsk_buff_pool *pool, dma_addr_t dma,
size_t size);

#endif /* XSK_BUFF_POOL_H_ */
3 changes: 2 additions & 1 deletion include/trace/events/xdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ TRACE_EVENT(xdp_devmap_xmit,
FN(PAGE_SHARED) \
FN(PAGE_ORDER0) \
FN(PAGE_POOL) \
FN(ZERO_COPY)
FN(ZERO_COPY) \
FN(XSK_BUFF_POOL)

#define __MEM_TYPE_TP_FN(x) \
TRACE_DEFINE_ENUM(MEM_TYPE_##x);
Expand Down
14 changes: 10 additions & 4 deletions net/core/xdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <net/xdp.h>
#include <net/xdp_priv.h> /* struct xdp_mem_allocator */
#include <trace/events/xdp.h>
#include <net/xdp_sock_drv.h>

#define REG_STATE_NEW 0x0
#define REG_STATE_REGISTERED 0x1
Expand Down Expand Up @@ -361,7 +362,7 @@ EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
* of xdp_frames/pages in those cases.
*/
static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
unsigned long handle)
unsigned long handle, struct xdp_buff *xdp)
{
struct xdp_mem_allocator *xa;
struct page *page;
Expand Down Expand Up @@ -390,6 +391,11 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
xa->zc_alloc->free(xa->zc_alloc, handle);
rcu_read_unlock();
break;
case MEM_TYPE_XSK_BUFF_POOL:
/* NB! Only valid from an xdp_buff! */
xsk_buff_free(xdp);
break;
default:
/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
break;
Expand All @@ -398,19 +404,19 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,

void xdp_return_frame(struct xdp_frame *xdpf)
{
__xdp_return(xdpf->data, &xdpf->mem, false, 0);
__xdp_return(xdpf->data, &xdpf->mem, false, 0, NULL);
}
EXPORT_SYMBOL_GPL(xdp_return_frame);

void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
{
__xdp_return(xdpf->data, &xdpf->mem, true, 0);
__xdp_return(xdpf->data, &xdpf->mem, true, 0, NULL);
}
EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);

void xdp_return_buff(struct xdp_buff *xdp)
{
__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle, xdp);
}
EXPORT_SYMBOL_GPL(xdp_return_buff);

Expand Down
1 change: 1 addition & 0 deletions net/xdp/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o xskmap.o
obj-$(CONFIG_XDP_SOCKETS) += xsk_buff_pool.o
obj-$(CONFIG_XDP_SOCKETS_DIAG) += xsk_diag.o
19 changes: 15 additions & 4 deletions net/xdp/xdp_umem.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void xdp_umem_release(struct xdp_umem *umem)
}

xsk_reuseq_destroy(umem);

xp_destroy(umem->pool);
xdp_umem_unmap_pages(umem);
xdp_umem_unpin_pages(umem);

Expand Down Expand Up @@ -390,6 +390,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
umem->size = size;
umem->headroom = headroom;
umem->chunk_size_nohr = chunk_size - headroom;
umem->chunk_size = chunk_size;
umem->npgs = size / PAGE_SIZE;
umem->pgs = NULL;
umem->user = NULL;
Expand All @@ -415,11 +416,21 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
}

err = xdp_umem_map_pages(umem);
if (!err)
return 0;
if (err)
goto out_pages;

kvfree(umem->pages);
umem->pool = xp_create(umem->pgs, umem->npgs, chunks, chunk_size,
headroom, size, unaligned_chunks);
if (!umem->pool) {
err = -ENOMEM;
goto out_unmap;
}
return 0;

out_unmap:
xdp_umem_unmap_pages(umem);
out_pages:
kvfree(umem->pages);
out_pin:
xdp_umem_unpin_pages(umem);
out_account:
Expand Down
Loading

0 comments on commit 2b43470

Please sign in to comment.