4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <sys/queue.h>
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"
42 TAILQ_HEAD(rte_event_ring_list, rte_tailq_entry);
44 static struct rte_tailq_elem rte_event_ring_tailq = {
45 .name = RTE_TAILQ_EVENT_RING_NAME,
47 EAL_REGISTER_TAILQ(rte_event_ring_tailq)
50 rte_event_ring_init(struct rte_event_ring *r, const char *name,
51 unsigned int count, unsigned int flags)
53 /* compilation-time checks */
54 RTE_BUILD_BUG_ON((sizeof(struct rte_event_ring) &
55 RTE_CACHE_LINE_MASK) != 0);
57 /* init the ring structure */
58 return rte_ring_init(&r->r, name, count, flags);
62 struct rte_event_ring *
63 rte_event_ring_create(const char *name, unsigned int count, int socket_id,
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;
72 struct rte_event_ring_list *ring_list = NULL;
73 const unsigned int requested_count = count;
76 ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
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)) {
87 ring_size = sizeof(*r) + (count * sizeof(struct rte_event));
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;
96 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
98 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
103 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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
110 mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
114 * no need to check return value here, we already checked the
117 rte_event_ring_init(r, name, requested_count, flags);
119 te->data = (void *) r;
122 TAILQ_INSERT_TAIL(ring_list, te, next);
125 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
128 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
134 struct rte_event_ring *
135 rte_event_ring_lookup(const char *name)
137 struct rte_tailq_entry *te;
138 struct rte_event_ring *r = NULL;
139 struct rte_event_ring_list *ring_list;
141 ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
142 rte_event_ring_list);
144 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
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)
152 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
164 rte_event_ring_free(struct rte_event_ring *r)
166 struct rte_event_ring_list *ring_list = NULL;
167 struct rte_tailq_entry *te;
173 * Ring was not created with rte_event_ring_create,
174 * therefore, there is no memzone to free.
176 if (r->r.memzone == NULL) {
178 "Cannot free ring (not created with rte_event_ring_create()");
182 if (rte_memzone_free(r->r.memzone) != 0) {
183 RTE_LOG(ERR, RING, "Cannot free memory\n");
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);
191 /* find out tailq entry */
192 TAILQ_FOREACH(te, ring_list, next) {
193 if (te->data == (void *) r)
198 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
202 TAILQ_REMOVE(ring_list, te, next);
204 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);