eal: export function to get runtime directory
[dpdk.git] / lib / librte_eventdev / rte_event_ring.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <string.h>
7
8 #include <rte_tailq.h>
9 #include <rte_memzone.h>
10 #include <rte_rwlock.h>
11 #include <rte_eal_memconfig.h>
12 #include "rte_event_ring.h"
13
14 TAILQ_HEAD(rte_event_ring_list, rte_tailq_entry);
15
16 static struct rte_tailq_elem rte_event_ring_tailq = {
17         .name = RTE_TAILQ_EVENT_RING_NAME,
18 };
19 EAL_REGISTER_TAILQ(rte_event_ring_tailq)
20
21 int
22 rte_event_ring_init(struct rte_event_ring *r, const char *name,
23         unsigned int count, unsigned int flags)
24 {
25         /* compilation-time checks */
26         RTE_BUILD_BUG_ON((sizeof(struct rte_event_ring) &
27                           RTE_CACHE_LINE_MASK) != 0);
28
29         /* init the ring structure */
30         return rte_ring_init(&r->r, name, count, flags);
31 }
32
33 /* create the ring */
34 struct rte_event_ring *
35 rte_event_ring_create(const char *name, unsigned int count, int socket_id,
36                 unsigned int flags)
37 {
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;
42         ssize_t ring_size;
43         int mz_flags = 0;
44         struct rte_event_ring_list *ring_list = NULL;
45         const unsigned int requested_count = count;
46         int ret;
47
48         ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
49                 rte_event_ring_list);
50
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)) {
55                 rte_errno = EINVAL;
56                 return NULL;
57         }
58
59         ring_size = sizeof(*r) + (count * sizeof(struct rte_event));
60
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;
65                 return NULL;
66         }
67
68         te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
69         if (te == NULL) {
70                 RTE_LOG(ERR, RING, "Cannot reserve memory for tailq\n");
71                 rte_errno = ENOMEM;
72                 return NULL;
73         }
74
75         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
76
77         /*
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
81          */
82         mz = rte_memzone_reserve(mz_name, ring_size, socket_id, mz_flags);
83         if (mz != NULL) {
84                 r = mz->addr;
85                 /* Check return value in case rte_ring_init() fails on size */
86                 int err = rte_event_ring_init(r, name, requested_count, flags);
87                 if (err) {
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");
91                         rte_free(te);
92                         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
93                         return NULL;
94                 }
95
96                 te->data = (void *) r;
97                 r->r.memzone = mz;
98
99                 TAILQ_INSERT_TAIL(ring_list, te, next);
100         } else {
101                 r = NULL;
102                 RTE_LOG(ERR, RING, "Cannot reserve memory\n");
103                 rte_free(te);
104         }
105         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
106
107         return r;
108 }
109
110
111 struct rte_event_ring *
112 rte_event_ring_lookup(const char *name)
113 {
114         struct rte_tailq_entry *te;
115         struct rte_event_ring *r = NULL;
116         struct rte_event_ring_list *ring_list;
117
118         ring_list = RTE_TAILQ_CAST(rte_event_ring_tailq.head,
119                         rte_event_ring_list);
120
121         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
122
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)
126                         break;
127         }
128
129         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
130
131         if (te == NULL) {
132                 rte_errno = ENOENT;
133                 return NULL;
134         }
135
136         return r;
137 }
138
139 /* free the ring */
140 void
141 rte_event_ring_free(struct rte_event_ring *r)
142 {
143         struct rte_event_ring_list *ring_list = NULL;
144         struct rte_tailq_entry *te;
145
146         if (r == NULL)
147                 return;
148
149         /*
150          * Ring was not created with rte_event_ring_create,
151          * therefore, there is no memzone to free.
152          */
153         if (r->r.memzone == NULL) {
154                 RTE_LOG(ERR, RING,
155                         "Cannot free ring (not created with rte_event_ring_create()");
156                 return;
157         }
158
159         if (rte_memzone_free(r->r.memzone) != 0) {
160                 RTE_LOG(ERR, RING, "Cannot free memory\n");
161                 return;
162         }
163
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);
167
168         /* find out tailq entry */
169         TAILQ_FOREACH(te, ring_list, next) {
170                 if (te->data == (void *) r)
171                         break;
172         }
173
174         if (te == NULL) {
175                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
176                 return;
177         }
178
179         TAILQ_REMOVE(ring_list, te, next);
180
181         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
182
183         rte_free(te);
184 }