New upstream version 17.08
[deb_dpdk.git] / lib / librte_eventdev / rte_event_ring.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/queue.h>
34 #include <string.h>
35
36 #include <rte_tailq.h>
37 #include <rte_memzone.h>
38 #include <rte_rwlock.h>
39 #include <rte_eal_memconfig.h>
40 #include "rte_event_ring.h"
41
42 TAILQ_HEAD(rte_event_ring_list, rte_tailq_entry);
43
44 static struct rte_tailq_elem rte_event_ring_tailq = {
45         .name = RTE_TAILQ_EVENT_RING_NAME,
46 };
47 EAL_REGISTER_TAILQ(rte_event_ring_tailq)
48
49 int
50 rte_event_ring_init(struct rte_event_ring *r, const char *name,
51         unsigned int count, unsigned int flags)
52 {
53         /* compilation-time checks */
54         RTE_BUILD_BUG_ON((sizeof(struct rte_event_ring) &
55                           RTE_CACHE_LINE_MASK) != 0);
56
57         /* init the ring structure */
58         return rte_ring_init(&r->r, name, count, flags);
59 }
60
61 /* create the ring */
62 struct rte_event_ring *
63 rte_event_ring_create(const char *name, unsigned int count, int socket_id,
64                 unsigned int flags)
65 {
66         char mz_name[RTE_MEMZONE_NAMESIZE];
67         struct rte_event_ring *r;
68         struct rte_tailq_entry *te;
69         const struct rte_memzone *mz;
70         ssize_t ring_size;
71         int mz_flags = 0;
72         struct rte_event_ring_list *ring_list = NULL;
73         const unsigned int requested_count = count;
74         int ret;
75
76         ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
77                 rte_event_ring_list);
78
79         /* for an exact size ring, round up from count to a power of two */
80         if (flags & RING_F_EXACT_SZ)
81                 count = rte_align32pow2(count + 1);
82         else if (!rte_is_power_of_2(count)) {
83                 rte_errno = EINVAL;
84                 return NULL;
85         }
86
87         ring_size = sizeof(*r) + (count * sizeof(struct rte_event));
88
89         ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
90                 RTE_RING_MZ_PREFIX, name);
91         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
92                 rte_errno = ENAMETOOLONG;
93                 return NULL;
94         }
95
96         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
97         if (te == NULL) {
98                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
99                 rte_errno = ENOMEM;
100                 return NULL;
101         }
102
103         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
104
105         /*
106          * reserve a memory zone for this ring. If we can't get rte_config or
107          * we are secondary process, the memzone_reserve function will set
108          * rte_errno for us appropriately - hence no check in this this function
109          */
110         mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
111         if (mz != NULL) {
112                 r = mz->addr;
113                 /*
114                  * no need to check return value here, we already checked the
115                  * arguments above
116                  */
117                 rte_event_ring_init(r, name, requested_count, flags);
118
119                 te->data = (void *) r;
120                 r->r.memzone = mz;
121
122                 TAILQ_INSERT_TAIL(ring_list, te, next);
123         } else {
124                 r = NULL;
125                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
126                 rte_free(te);
127         }
128         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
129
130         return r;
131 }
132
133
134 struct rte_event_ring *
135 rte_event_ring_lookup(const char *name)
136 {
137         struct rte_tailq_entry *te;
138         struct rte_event_ring *r = NULL;
139         struct rte_event_ring_list *ring_list;
140
141         ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
142                         rte_event_ring_list);
143
144         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
145
146         TAILQ_FOREACH(te, ring_list, next) {
147                 r = (struct rte_event_ring *) te->data;
148                 if (strncmp(name, r->r.name, RTE_RING_NAMESIZE) == 0)
149                         break;
150         }
151
152         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
153
154         if (te == NULL) {
155                 rte_errno = ENOENT;
156                 return NULL;
157         }
158
159         return r;
160 }
161
162 /* free the ring */
163 void
164 rte_event_ring_free(struct rte_event_ring *r)
165 {
166         struct rte_event_ring_list *ring_list = NULL;
167         struct rte_tailq_entry *te;
168
169         if (r == NULL)
170                 return;
171
172         /*
173          * Ring was not created with rte_event_ring_create,
174          * therefore, there is no memzone to free.
175          */
176         if (r->r.memzone == NULL) {
177                 RTE_LOG(ERR, RING,
178                         "Cannot free ring (not created with rte_event_ring_create()");
179                 return;
180         }
181
182         if (rte_memzone_free(r->r.memzone) != 0) {
183                 RTE_LOG(ERR, RING, "Cannot free memory\n");
184                 return;
185         }
186
187         ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
188                         rte_event_ring_list);
189         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
190
191         /* find out tailq entry */
192         TAILQ_FOREACH(te, ring_list, next) {
193                 if (te->data == (void *) r)
194                         break;
195         }
196
197         if (te == NULL) {
198                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
199                 return;
200         }
201
202         TAILQ_REMOVE(ring_list, te, next);
203
204         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
205
206         rte_free(te);
207 }