Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* XDP user-space ring structure |
| 3 | * Copyright(c) 2018 Intel Corporation. |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/slab.h> |
| 7 | |
| 8 | #include "xsk_queue.h" |
| 9 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 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 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 18 | static 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öpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 23 | static u32 xskq_rxtx_get_ring_size(struct xsk_queue *q) |
| 24 | { |
Björn Töpel | da60cf0 | 2018-05-18 14:00:23 +0200 | [diff] [blame^] | 25 | return sizeof(struct xdp_ring) + q->nentries * sizeof(struct xdp_desc); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | struct xsk_queue *xskq_create(u32 nentries, bool umem_queue) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 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; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 43 | size = umem_queue ? xskq_umem_get_ring_size(q) : |
| 44 | xskq_rxtx_get_ring_size(q); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 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 | } |