Imported Upstream version 16.07-rc1
[deb_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, obj_table, n);
46 }
47
48 static int
49 common_ring_sp_enqueue(struct rte_mempool *mp, void * const *obj_table,
50                 unsigned n)
51 {
52         return rte_ring_sp_enqueue_bulk(mp->pool_data, obj_table, n);
53 }
54
55 static int
56 common_ring_mc_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
57 {
58         return rte_ring_mc_dequeue_bulk(mp->pool_data, obj_table, n);
59 }
60
61 static int
62 common_ring_sc_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
63 {
64         return rte_ring_sc_dequeue_bulk(mp->pool_data, obj_table, n);
65 }
66
67 static unsigned
68 common_ring_get_count(const struct rte_mempool *mp)
69 {
70         return rte_ring_count(mp->pool_data);
71 }
72
73
74 static int
75 common_ring_alloc(struct rte_mempool *mp)
76 {
77         int rg_flags = 0, ret;
78         char rg_name[RTE_RING_NAMESIZE];
79         struct rte_ring *r;
80
81         ret = snprintf(rg_name, sizeof(rg_name),
82                 RTE_MEMPOOL_MZ_FORMAT, mp->name);
83         if (ret < 0 || ret >= (int)sizeof(rg_name)) {
84                 rte_errno = ENAMETOOLONG;
85                 return -rte_errno;
86         }
87
88         /* ring flags */
89         if (mp->flags & MEMPOOL_F_SP_PUT)
90                 rg_flags |= RING_F_SP_ENQ;
91         if (mp->flags & MEMPOOL_F_SC_GET)
92                 rg_flags |= RING_F_SC_DEQ;
93
94         /*
95          * Allocate the ring that will be used to store objects.
96          * Ring functions will return appropriate errors if we are
97          * running as a secondary process etc., so no checks made
98          * in this function for that condition.
99          */
100         r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
101                 mp->socket_id, rg_flags);
102         if (r == NULL)
103                 return -rte_errno;
104
105         mp->pool_data = r;
106
107         return 0;
108 }
109
110 static void
111 common_ring_free(struct rte_mempool *mp)
112 {
113         rte_ring_free(mp->pool_data);
114 }
115
116 /*
117  * The following 4 declarations of mempool ops structs address
118  * the need for the backward compatible mempool handlers for
119  * single/multi producers and single/multi consumers as dictated by the
120  * flags provided to the rte_mempool_create function
121  */
122 static const struct rte_mempool_ops ops_mp_mc = {
123         .name = "ring_mp_mc",
124         .alloc = common_ring_alloc,
125         .free = common_ring_free,
126         .enqueue = common_ring_mp_enqueue,
127         .dequeue = common_ring_mc_dequeue,
128         .get_count = common_ring_get_count,
129 };
130
131 static const struct rte_mempool_ops ops_sp_sc = {
132         .name = "ring_sp_sc",
133         .alloc = common_ring_alloc,
134         .free = common_ring_free,
135         .enqueue = common_ring_sp_enqueue,
136         .dequeue = common_ring_sc_dequeue,
137         .get_count = common_ring_get_count,
138 };
139
140 static const struct rte_mempool_ops ops_mp_sc = {
141         .name = "ring_mp_sc",
142         .alloc = common_ring_alloc,
143         .free = common_ring_free,
144         .enqueue = common_ring_mp_enqueue,
145         .dequeue = common_ring_sc_dequeue,
146         .get_count = common_ring_get_count,
147 };
148
149 static const struct rte_mempool_ops ops_sp_mc = {
150         .name = "ring_sp_mc",
151         .alloc = common_ring_alloc,
152         .free = common_ring_free,
153         .enqueue = common_ring_sp_enqueue,
154         .dequeue = common_ring_mc_dequeue,
155         .get_count = common_ring_get_count,
156 };
157
158 MEMPOOL_REGISTER_OPS(ops_mp_mc);
159 MEMPOOL_REGISTER_OPS(ops_sp_sc);
160 MEMPOOL_REGISTER_OPS(ops_mp_sc);
161 MEMPOOL_REGISTER_OPS(ops_sp_mc);