blob: ebe85e59507ec4f6ace0e6b5213c097b7d5c47e7 [file] [log] [blame]
Magnus Karlsson423f3832018-05-02 13:01:24 +02001// SPDX-License-Identifier: GPL-2.0
2/* XDP user-space ring structure
3 * Copyright(c) 2018 Intel Corporation.
Magnus Karlsson423f3832018-05-02 13:01:24 +02004 */
5
6#include <linux/slab.h>
7
8#include "xsk_queue.h"
9
Magnus Karlsson965a9902018-05-02 13:01:26 +020010void xskq_set_umem(struct xsk_queue *q, struct xdp_umem_props *umem_props)
11{
12 if (!q)
13 return;
14
15 q->umem_props = *umem_props;
16}
17
Magnus Karlsson423f3832018-05-02 13:01:24 +020018static u32 xskq_umem_get_ring_size(struct xsk_queue *q)
19{
20 return sizeof(struct xdp_umem_ring) + q->nentries * sizeof(u32);
21}
22
Björn Töpelb9b6b682018-05-02 13:01:25 +020023static u32 xskq_rxtx_get_ring_size(struct xsk_queue *q)
24{
Björn Töpelda60cf02018-05-18 14:00:23 +020025 return sizeof(struct xdp_ring) + q->nentries * sizeof(struct xdp_desc);
Björn Töpelb9b6b682018-05-02 13:01:25 +020026}
27
28struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
Magnus Karlsson423f3832018-05-02 13:01:24 +020029{
30 struct xsk_queue *q;
31 gfp_t gfp_flags;
32 size_t size;
33
34 q = kzalloc(sizeof(*q), GFP_KERNEL);
35 if (!q)
36 return NULL;
37
38 q->nentries = nentries;
39 q->ring_mask = nentries - 1;
40
41 gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
42 __GFP_COMP | __GFP_NORETRY;
Björn Töpelb9b6b682018-05-02 13:01:25 +020043 size = umem_queue ? xskq_umem_get_ring_size(q) :
44 xskq_rxtx_get_ring_size(q);
Magnus Karlsson423f3832018-05-02 13:01:24 +020045
46 q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
47 get_order(size));
48 if (!q->ring) {
49 kfree(q);
50 return NULL;
51 }
52
53 return q;
54}
55
56void xskq_destroy(struct xsk_queue *q)
57{
58 if (!q)
59 return;
60
61 page_frag_free(q->ring);
62 kfree(q);
63}