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 | |
Jakub Kicinski | f5bd913 | 2018-09-07 10:18:46 +0200 | [diff] [blame] | 6 | #include <linux/log2.h> |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 7 | #include <linux/slab.h> |
Jakub Kicinski | f5bd913 | 2018-09-07 10:18:46 +0200 | [diff] [blame] | 8 | #include <linux/overflow.h> |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 9 | #include <net/xdp_sock_drv.h> |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 10 | |
| 11 | #include "xsk_queue.h" |
| 12 | |
Magnus Karlsson | 1d9cb1f | 2019-12-19 13:39:31 +0100 | [diff] [blame] | 13 | static size_t xskq_get_ring_size(struct xsk_queue *q, bool umem_queue) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 14 | { |
Magnus Karlsson | 1d9cb1f | 2019-12-19 13:39:31 +0100 | [diff] [blame] | 15 | struct xdp_umem_ring *umem_ring; |
| 16 | struct xdp_rxtx_ring *rxtx_ring; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 17 | |
Magnus Karlsson | 1d9cb1f | 2019-12-19 13:39:31 +0100 | [diff] [blame] | 18 | if (umem_queue) |
| 19 | return struct_size(umem_ring, desc, q->nentries); |
| 20 | return struct_size(rxtx_ring, desc, q->nentries); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | struct xsk_queue *xskq_create(u32 nentries, bool umem_queue) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 24 | { |
| 25 | struct xsk_queue *q; |
| 26 | gfp_t gfp_flags; |
| 27 | size_t size; |
| 28 | |
| 29 | q = kzalloc(sizeof(*q), GFP_KERNEL); |
| 30 | if (!q) |
| 31 | return NULL; |
| 32 | |
| 33 | q->nentries = nentries; |
| 34 | q->ring_mask = nentries - 1; |
| 35 | |
| 36 | gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | |
| 37 | __GFP_COMP | __GFP_NORETRY; |
Magnus Karlsson | 1d9cb1f | 2019-12-19 13:39:31 +0100 | [diff] [blame] | 38 | size = xskq_get_ring_size(q, umem_queue); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 39 | |
| 40 | q->ring = (struct xdp_ring *)__get_free_pages(gfp_flags, |
| 41 | get_order(size)); |
| 42 | if (!q->ring) { |
| 43 | kfree(q); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | return q; |
| 48 | } |
| 49 | |
| 50 | void xskq_destroy(struct xsk_queue *q) |
| 51 | { |
| 52 | if (!q) |
| 53 | return; |
| 54 | |
| 55 | page_frag_free(q->ring); |
| 56 | kfree(q); |
| 57 | } |