2018-05-02 11:01:24 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* XDP user-space ring structure
|
|
|
|
* Copyright(c) 2018 Intel Corporation.
|
|
|
|
*/
|
|
|
|
|
2018-09-07 08:18:46 +00:00
|
|
|
#include <linux/log2.h>
|
2018-05-02 11:01:24 +00:00
|
|
|
#include <linux/slab.h>
|
2018-09-07 08:18:46 +00:00
|
|
|
#include <linux/overflow.h>
|
2020-05-20 19:20:51 +00:00
|
|
|
#include <net/xdp_sock_drv.h>
|
2018-05-02 11:01:24 +00:00
|
|
|
|
|
|
|
#include "xsk_queue.h"
|
|
|
|
|
2019-12-19 12:39:31 +00:00
|
|
|
static size_t xskq_get_ring_size(struct xsk_queue *q, bool umem_queue)
|
2018-05-02 11:01:24 +00:00
|
|
|
{
|
2019-12-19 12:39:31 +00:00
|
|
|
struct xdp_umem_ring *umem_ring;
|
|
|
|
struct xdp_rxtx_ring *rxtx_ring;
|
2018-05-02 11:01:24 +00:00
|
|
|
|
2019-12-19 12:39:31 +00:00
|
|
|
if (umem_queue)
|
|
|
|
return struct_size(umem_ring, desc, q->nentries);
|
|
|
|
return struct_size(rxtx_ring, desc, q->nentries);
|
2018-05-02 11:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
|
2018-05-02 11:01:24 +00:00
|
|
|
{
|
|
|
|
struct xsk_queue *q;
|
|
|
|
gfp_t gfp_flags;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
q = kzalloc(sizeof(*q), GFP_KERNEL);
|
|
|
|
if (!q)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
q->nentries = nentries;
|
|
|
|
q->ring_mask = nentries - 1;
|
|
|
|
|
|
|
|
gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
|
|
|
|
__GFP_COMP | __GFP_NORETRY;
|
2019-12-19 12:39:31 +00:00
|
|
|
size = xskq_get_ring_size(q, umem_queue);
|
2018-05-02 11:01:24 +00:00
|
|
|
|
|
|
|
q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
|
|
|
|
get_order(size));
|
|
|
|
if (!q->ring) {
|
|
|
|
kfree(q);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xskq_destroy(struct xsk_queue *q)
|
|
|
|
{
|
|
|
|
if (!q)
|
|
|
|
return;
|
|
|
|
|
|
|
|
page_frag_free(q->ring);
|
|
|
|
kfree(q);
|
|
|
|
}
|