ring: return free space when enqueuing
[dpdk.git] / lib / librte_mempool / rte_mempool_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36
37 #include <rte_errno.h>
38 #include <rte_ring.h>
39 #include <rte_mempool.h>
40
41 static int
42 common_ring_mp_enqueue(struct rte_mempool *mp, void * const *obj_table,
43                 unsigned n)
44 {
45         return rte_ring_mp_enqueue_bulk(mp->pool_data,
46                         obj_table, n, NULL) == 0 ? -ENOBUFS : 0;
47 }
48
49 static int
50 common_ring_sp_enqueue(struct rte_mempool *mp, void * const *obj_table,
51                 unsigned n)
52 {
53         return rte_ring_sp_enqueue_bulk(mp->pool_data,
54                         obj_table, n, NULL) == 0 ? -ENOBUFS : 0;
55 }
56
57 static int
58 common_ring_mc_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
59 {
60         return rte_ring_mc_dequeue_bulk(mp->pool_data,
61                         obj_table, n) == 0 ? -ENOBUFS : 0;
62 }
63
64 static int
65 common_ring_sc_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
66 {
67         return rte_ring_sc_dequeue_bulk(mp->pool_data,
68                         obj_table, n) == 0 ? -ENOBUFS : 0;
69 }
70
71 static unsigned
72 common_ring_get_count(const struct rte_mempool *mp)
73 {
74         return rte_ring_count(mp->pool_data);
75 }
76
77
78 static int
79 common_ring_alloc(struct rte_mempool *mp)
80 {
81         int rg_flags = 0, ret;
82         char rg_name[RTE_RING_NAMESIZE];
83         struct rte_ring *r;
84
85         ret = snprintf(rg_name, sizeof(rg_name),
86                 RTE_MEMPOOL_MZ_FORMAT, mp->name);
87         if (ret < 0 || ret >= (int)sizeof(rg_name)) {
88                 rte_errno = ENAMETOOLONG;
89                 return -rte_errno;
90         }
91
92         /* ring flags */
93         if (mp->flags & MEMPOOL_F_SP_PUT)
94                 rg_flags |= RING_F_SP_ENQ;
95         if (mp->flags & MEMPOOL_F_SC_GET)
96                 rg_flags |= RING_F_SC_DEQ;
97
98         /*
99          * Allocate the ring that will be used to store objects.
100          * Ring functions will return appropriate errors if we are
101          * running as a secondary process etc., so no checks made
102          * in this function for that condition.
103          */
104         r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
105                 mp->socket_id, rg_flags);
106         if (r == NULL)
107                 return -rte_errno;
108
109         mp->pool_data = r;
110
111         return 0;
112 }
113
114 static void
115 common_ring_free(struct rte_mempool *mp)
116 {
117         rte_ring_free(mp->pool_data);
118 }
119
120 /*
121  * The following 4 declarations of mempool ops structs address
122  * the need for the backward compatible mempool handlers for
123  * single/multi producers and single/multi consumers as dictated by the
124  * flags provided to the rte_mempool_create function
125  */
126 static const struct rte_mempool_ops ops_mp_mc = {
127         .name = "ring_mp_mc",
128         .alloc = common_ring_alloc,
129         .free = common_ring_free,
130         .enqueue = common_ring_mp_enqueue,
131         .dequeue = common_ring_mc_dequeue,
132         .get_count = common_ring_get_count,
133 };
134
135 static const struct rte_mempool_ops ops_sp_sc = {
136         .name = "ring_sp_sc",
137         .alloc = common_ring_alloc,
138         .free = common_ring_free,
139         .enqueue = common_ring_sp_enqueue,
140         .dequeue = common_ring_sc_dequeue,
141         .get_count = common_ring_get_count,
142 };
143
144 static const struct rte_mempool_ops ops_mp_sc = {
145         .name = "ring_mp_sc",
146         .alloc = common_ring_alloc,
147         .free = common_ring_free,
148         .enqueue = common_ring_mp_enqueue,
149         .dequeue = common_ring_sc_dequeue,
150         .get_count = common_ring_get_count,
151 };
152
153 static const struct rte_mempool_ops ops_sp_mc = {
154         .name = "ring_sp_mc",
155         .alloc = common_ring_alloc,
156         .free = common_ring_free,
157         .enqueue = common_ring_sp_enqueue,
158         .dequeue = common_ring_mc_dequeue,
159         .get_count = common_ring_get_count,
160 };
161
162 MEMPOOL_REGISTER_OPS(ops_mp_mc);
163 MEMPOOL_REGISTER_OPS(ops_sp_sc);
164 MEMPOOL_REGISTER_OPS(ops_mp_sc);
165 MEMPOOL_REGISTER_OPS(ops_sp_mc);