xref: /openbmc/linux/net/xdp/xsk_queue.c (revision f220d3eb)
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, struct xdp_umem_props *umem_props)
11 {
12 	if (!q)
13 		return;
14 
15 	q->umem_props = *umem_props;
16 }
17 
18 static u32 xskq_umem_get_ring_size(struct xsk_queue *q)
19 {
20 	return sizeof(struct xdp_umem_ring) + q->nentries * sizeof(u64);
21 }
22 
23 static u32 xskq_rxtx_get_ring_size(struct xsk_queue *q)
24 {
25 	return sizeof(struct xdp_ring) + q->nentries * sizeof(struct xdp_desc);
26 }
27 
28 struct xsk_queue *xskq_create(u32 nentries, bool umem_queue)
29 {
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;
43 	size = umem_queue ? xskq_umem_get_ring_size(q) :
44 	       xskq_rxtx_get_ring_size(q);
45 
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 
56 void xskq_destroy(struct xsk_queue *q)
57 {
58 	if (!q)
59 		return;
60 
61 	page_frag_free(q->ring);
62 	kfree(q);
63 }
64