xref: /openbmc/linux/net/xdp/xsk_queue.c (revision dd2934a95701576203b2f61e8ded4e4a2f9183ea)
1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP user-space ring structure
3  * Copyright(c) 2018 Intel Corporation.
4  */
5 
6 #include <linux/slab.h>
7 
8 #include "xsk_queue.h"
9 
10 void xskq_set_umem(struct xsk_queue *q, u64 size, u64 chunk_mask)
11 {
12 	if (!q)
13 		return;
14 
15 	q->size = size;
16 	q->chunk_mask = chunk_mask;
17 }
18 
19 static u32 xskq_umem_get_ring_size(struct xsk_queue *q)
20 {
21 	return sizeof(struct xdp_umem_ring) + q->nentries * sizeof(u64);
22 }
23 
24 static u32 xskq_rxtx_get_ring_size(struct xsk_queue *q)
25 {
26 	return sizeof(struct xdp_ring) + q->nentries * sizeof(struct xdp_desc);
27 }
28 
29 struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
30 {
31 	struct xsk_queue *q;
32 	gfp_t gfp_flags;
33 	size_t size;
34 
35 	q = kzalloc(sizeof(*q), GFP_KERNEL);
36 	if (!q)
37 		return NULL;
38 
39 	q->nentries = nentries;
40 	q->ring_mask = nentries - 1;
41 
42 	gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN |
43 		    __GFP_COMP  | __GFP_NORETRY;
44 	size = umem_queue ? xskq_umem_get_ring_size(q) :
45 	       xskq_rxtx_get_ring_size(q);
46 
47 	q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags,
48 						      get_order(size));
49 	if (!q->ring) {
50 		kfree(q);
51 		return NULL;
52 	}
53 
54 	return q;
55 }
56 
57 void xskq_destroy(struct xsk_queue *q)
58 {
59 	if (!q)
60 		return;
61 
62 	page_frag_free(q->ring);
63 	kfree(q);
64 }
65