Skip to content

Commit 02b55e5

Browse files
Björn Töpelborkmann
authored andcommitted
xdp: add MEM_TYPE_ZERO_COPY
Here, a new type of allocator support is added to the XDP return API. A zero-copy allocated xdp_buff cannot be converted to an xdp_frame. Instead is the buff has to be copied. This is not supported at all in this commit. Also, an opaque "handle" is added to xdp_buff. This can be used as a context for the zero-copy allocator implementation. Signed-off-by: Björn Töpel <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 74515c5 commit 02b55e5

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

include/net/xdp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum xdp_mem_type {
3737
MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
3838
MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
3939
MEM_TYPE_PAGE_POOL,
40+
MEM_TYPE_ZERO_COPY,
4041
MEM_TYPE_MAX,
4142
};
4243

@@ -51,6 +52,10 @@ struct xdp_mem_info {
5152

5253
struct page_pool;
5354

55+
struct zero_copy_allocator {
56+
void (*free)(struct zero_copy_allocator *zca, unsigned long handle);
57+
};
58+
5459
struct xdp_rxq_info {
5560
struct net_device *dev;
5661
u32 queue_index;
@@ -63,6 +68,7 @@ struct xdp_buff {
6368
void *data_end;
6469
void *data_meta;
6570
void *data_hard_start;
71+
unsigned long handle;
6672
struct xdp_rxq_info *rxq;
6773
};
6874

@@ -86,6 +92,10 @@ struct xdp_frame *convert_to_xdp_frame(struct xdp_buff *xdp)
8692
int metasize;
8793
int headroom;
8894

95+
/* TODO: implement clone, copy, use "native" MEM_TYPE */
96+
if (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY)
97+
return NULL;
98+
8999
/* Assure headroom is available for storing info */
90100
headroom = xdp->data - xdp->data_hard_start;
91101
metasize = xdp->data - xdp->data_meta;

net/core/xdp.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct xdp_mem_allocator {
3131
union {
3232
void *allocator;
3333
struct page_pool *page_pool;
34+
struct zero_copy_allocator *zc_alloc;
3435
};
3536
struct rhash_head node;
3637
struct rcu_head rcu;
@@ -261,7 +262,7 @@ int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
261262
xdp_rxq->mem.type = type;
262263

263264
if (!allocator) {
264-
if (type == MEM_TYPE_PAGE_POOL)
265+
if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY)
265266
return -EINVAL; /* Setup time check page_pool req */
266267
return 0;
267268
}
@@ -314,7 +315,8 @@ EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
314315
* is used for those calls sites. Thus, allowing for faster recycling
315316
* of xdp_frames/pages in those cases.
316317
*/
317-
static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct)
318+
static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,
319+
unsigned long handle)
318320
{
319321
struct xdp_mem_allocator *xa;
320322
struct page *page;
@@ -338,6 +340,13 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct)
338340
page = virt_to_page(data); /* Assumes order0 page*/
339341
put_page(page);
340342
break;
343+
case MEM_TYPE_ZERO_COPY:
344+
/* NB! Only valid from an xdp_buff! */
345+
rcu_read_lock();
346+
/* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */
347+
xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params);
348+
xa->zc_alloc->free(xa->zc_alloc, handle);
349+
rcu_read_unlock();
341350
default:
342351
/* Not possible, checked in xdp_rxq_info_reg_mem_model() */
343352
break;
@@ -346,18 +355,18 @@ static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct)
346355

347356
void xdp_return_frame(struct xdp_frame *xdpf)
348357
{
349-
__xdp_return(xdpf->data, &xdpf->mem, false);
358+
__xdp_return(xdpf->data, &xdpf->mem, false, 0);
350359
}
351360
EXPORT_SYMBOL_GPL(xdp_return_frame);
352361

353362
void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)
354363
{
355-
__xdp_return(xdpf->data, &xdpf->mem, true);
364+
__xdp_return(xdpf->data, &xdpf->mem, true, 0);
356365
}
357366
EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);
358367

359368
void xdp_return_buff(struct xdp_buff *xdp)
360369
{
361-
__xdp_return(xdp->data, &xdp->rxq->mem, true);
370+
__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
362371
}
363372
EXPORT_SYMBOL_GPL(xdp_return_buff);

0 commit comments

Comments
 (0)