New upstream version 18.02
[deb_dpdk.git] / drivers / mempool / ring / rte_mempool_ring.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <rte_errno.h>
9 #include <rte_ring.h>
10 #include <rte_mempool.h>
11
12 static int
13 common_ring_mp_enqueue(struct rte_mempool *mp, void * const *obj_table,
14                 unsigned n)
15 {
16         return rte_ring_mp_enqueue_bulk(mp->pool_data,
17                         obj_table, n, NULL) == 0 ? -ENOBUFS : 0;
18 }
19
20 static int
21 common_ring_sp_enqueue(struct rte_mempool *mp, void * const *obj_table,
22                 unsigned n)
23 {
24         return rte_ring_sp_enqueue_bulk(mp->pool_data,
25                         obj_table, n, NULL) == 0 ? -ENOBUFS : 0;
26 }
27
28 static int
29 common_ring_mc_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
30 {
31         return rte_ring_mc_dequeue_bulk(mp->pool_data,
32                         obj_table, n, NULL) == 0 ? -ENOBUFS : 0;
33 }
34
35 static int
36 common_ring_sc_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
37 {
38         return rte_ring_sc_dequeue_bulk(mp->pool_data,
39                         obj_table, n, NULL) == 0 ? -ENOBUFS : 0;
40 }
41
42 static unsigned
43 common_ring_get_count(const struct rte_mempool *mp)
44 {
45         return rte_ring_count(mp->pool_data);
46 }
47
48
49 static int
50 common_ring_alloc(struct rte_mempool *mp)
51 {
52         int rg_flags = 0, ret;
53         char rg_name[RTE_RING_NAMESIZE];
54         struct rte_ring *r;
55
56         ret = snprintf(rg_name, sizeof(rg_name),
57                 RTE_MEMPOOL_MZ_FORMAT, mp->name);
58         if (ret < 0 || ret >= (int)sizeof(rg_name)) {
59                 rte_errno = ENAMETOOLONG;
60                 return -rte_errno;
61         }
62
63         /* ring flags */
64         if (mp->flags & MEMPOOL_F_SP_PUT)
65                 rg_flags |= RING_F_SP_ENQ;
66         if (mp->flags & MEMPOOL_F_SC_GET)
67                 rg_flags |= RING_F_SC_DEQ;
68
69         /*
70          * Allocate the ring that will be used to store objects.
71          * Ring functions will return appropriate errors if we are
72          * running as a secondary process etc., so no checks made
73          * in this function for that condition.
74          */
75         r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
76                 mp->socket_id, rg_flags);
77         if (r == NULL)
78                 return -rte_errno;
79
80         mp->pool_data = r;
81
82         return 0;
83 }
84
85 static void
86 common_ring_free(struct rte_mempool *mp)
87 {
88         rte_ring_free(mp->pool_data);
89 }
90
91 /*
92  * The following 4 declarations of mempool ops structs address
93  * the need for the backward compatible mempool handlers for
94  * single/multi producers and single/multi consumers as dictated by the
95  * flags provided to the rte_mempool_create function
96  */
97 static const struct rte_mempool_ops ops_mp_mc = {
98         .name = "ring_mp_mc",
99         .alloc = common_ring_alloc,
100         .free = common_ring_free,
101         .enqueue = common_ring_mp_enqueue,
102         .dequeue = common_ring_mc_dequeue,
103         .get_count = common_ring_get_count,
104 };
105
106 static const struct rte_mempool_ops ops_sp_sc = {
107         .name = "ring_sp_sc",
108         .alloc = common_ring_alloc,
109         .free = common_ring_free,
110         .enqueue = common_ring_sp_enqueue,
111         .dequeue = common_ring_sc_dequeue,
112         .get_count = common_ring_get_count,
113 };
114
115 static const struct rte_mempool_ops ops_mp_sc = {
116         .name = "ring_mp_sc",
117         .alloc = common_ring_alloc,
118         .free = common_ring_free,
119         .enqueue = common_ring_mp_enqueue,
120         .dequeue = common_ring_sc_dequeue,
121         .get_count = common_ring_get_count,
122 };
123
124 static const struct rte_mempool_ops ops_sp_mc = {
125         .name = "ring_sp_mc",
126         .alloc = common_ring_alloc,
127         .free = common_ring_free,
128         .enqueue = common_ring_sp_enqueue,
129         .dequeue = common_ring_mc_dequeue,
130         .get_count = common_ring_get_count,
131 };
132
133 MEMPOOL_REGISTER_OPS(ops_mp_mc);
134 MEMPOOL_REGISTER_OPS(ops_sp_sc);
135 MEMPOOL_REGISTER_OPS(ops_mp_sc);
136 MEMPOOL_REGISTER_OPS(ops_sp_mc);