1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
9 #include <rte_memzone.h>
10 #include <rte_rwlock.h>
11 #include <rte_eal_memconfig.h>
12 #include "rte_event_ring.h"
14 TAILQ_HEAD(rte_event_ring_list, rte_tailq_entry);
16 static struct rte_tailq_elem rte_event_ring_tailq = {
17 .name = RTE_TAILQ_EVENT_RING_NAME,
19 EAL_REGISTER_TAILQ(rte_event_ring_tailq)
22 rte_event_ring_init(struct rte_event_ring *r, const char *name,
23 unsigned int count, unsigned int flags)
25 /* compilation-time checks */
26 RTE_BUILD_BUG_ON((sizeof(struct rte_event_ring) &
27 RTE_CACHE_LINE_MASK) != 0);
29 /* init the ring structure */
30 return rte_ring_init(&r->r, name, count, flags);
34 struct rte_event_ring *
35 rte_event_ring_create(const char *name, unsigned int count, int socket_id,
38 char mz_name[RTE_MEMZONE_NAMESIZE];
39 struct rte_event_ring *r;
40 struct rte_tailq_entry *te;
41 const struct rte_memzone *mz;
44 struct rte_event_ring_list *ring_list = NULL;
45 const unsigned int requested_count = count;
48 ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
51 /* for an exact size ring, round up from count to a power of two */
52 if (flags & RING_F_EXACT_SZ)
53 count = rte_align32pow2(count + 1);
54 else if (!rte_is_power_of_2(count)) {
59 ring_size = sizeof(*r) + (count * sizeof(struct rte_event));
61 ret = snprintf(mz_name, sizeof(mz_name), "%s%s",
62 RTE_RING_MZ_PREFIX, name);
63 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
64 rte_errno = ENAMETOOLONG;
68 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
70 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
75 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
78 * reserve a memory zone for this ring. If we can't get rte_config or
79 * we are secondary process, the memzone_reserve function will set
80 * rte_errno for us appropriately - hence no check in this this function
82 mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
85 /* Check return value in case rte_ring_init() fails on size */
86 int err = rte_event_ring_init(r, name, requested_count, flags);
88 RTE_LOG(ERR, RING, "Ring init failed\n");
89 if (rte_memzone_free(mz) != 0)
90 RTE_LOG(ERR, RING, "Cannot free memzone\n");
92 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
96 te->data = (void *) r;
99 TAILQ_INSERT_TAIL(ring_list, te, next);
102 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
105 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
111 struct rte_event_ring *
112 rte_event_ring_lookup(const char *name)
114 struct rte_tailq_entry *te;
115 struct rte_event_ring *r = NULL;
116 struct rte_event_ring_list *ring_list;
118 ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
119 rte_event_ring_list);
121 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
123 TAILQ_FOREACH(te, ring_list, next) {
124 r = (struct rte_event_ring *) te->data;
125 if (strncmp(name, r->r.name, RTE_RING_NAMESIZE) == 0)
129 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
141 rte_event_ring_free(struct rte_event_ring *r)
143 struct rte_event_ring_list *ring_list = NULL;
144 struct rte_tailq_entry *te;
150 * Ring was not created with rte_event_ring_create,
151 * therefore, there is no memzone to free.
153 if (r->r.memzone == NULL) {
155 "Cannot free ring (not created with rte_event_ring_create()");
159 if (rte_memzone_free(r->r.memzone) != 0) {
160 RTE_LOG(ERR, RING, "Cannot free memory\n");
164 ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
165 rte_event_ring_list);
166 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
168 /* find out tailq entry */
169 TAILQ_FOREACH(te, ring_list, next) {
170 if (te->data == (void *) r)
175 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
179 TAILQ_REMOVE(ring_list, te, next);
181 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);