tailq: introduce dynamic register system
[dpdk.git] / lib / librte_eal / common / eal_common_tailqs.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <sys/queue.h>
35 #include <stdint.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <inttypes.h>
41
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_launch.h>
45 #include <rte_eal.h>
46 #include <rte_eal_memconfig.h>
47 #include <rte_per_lcore.h>
48 #include <rte_lcore.h>
49 #include <rte_memory.h>
50 #include <rte_atomic.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_log.h>
53 #include <rte_string_fns.h>
54 #include <rte_debug.h>
55
56 #include "eal_private.h"
57
58 /**
59  * Name of tailq_head
60  */
61 const char* rte_tailq_names[RTE_MAX_TAILQ] = {
62 #define rte_tailq_elem(idx, name)     name,
63 #include <rte_tailq_elem.h>
64 };
65
66 TAILQ_HEAD(rte_tailq_elem_head, rte_tailq_elem);
67 /* local tailq list */
68 static struct rte_tailq_elem_head rte_tailq_elem_head =
69         TAILQ_HEAD_INITIALIZER(rte_tailq_elem_head);
70
71 /* number of tailqs registered, -1 before call to rte_eal_tailqs_init */
72 static int rte_tailqs_count = -1;
73
74 struct rte_tailq_head *
75 rte_eal_tailq_lookup(const char *name)
76 {
77         unsigned i;
78         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
79
80         if (name == NULL)
81                 return NULL;
82
83         for (i = 0; i < RTE_MAX_TAILQ; i++) {
84                 if (i < RTE_TAILQ_NUM &&
85                     !strncmp(name, rte_tailq_names[i], RTE_TAILQ_NAMESIZE-1))
86                         return &mcfg->tailq_head[i];
87
88                 /* if past static entries, look at shared mem for names */
89                 if (!strncmp(name, mcfg->tailq_head[i].name,
90                              RTE_TAILQ_NAMESIZE-1))
91                         return &mcfg->tailq_head[i];
92         }
93
94         return NULL;
95 }
96
97 inline struct rte_tailq_head *
98 rte_eal_tailq_lookup_by_idx(const unsigned tailq_idx)
99 {
100         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
101
102         if (tailq_idx >= RTE_MAX_TAILQ) {
103                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
104                 return NULL;
105         }
106
107         return &mcfg->tailq_head[tailq_idx];
108 }
109
110 void
111 rte_dump_tailq(FILE *f)
112 {
113         struct rte_mem_config *mcfg;
114         unsigned i = 0;
115
116         mcfg = rte_eal_get_configuration()->mem_config;
117
118         rte_rwlock_read_lock(&mcfg->qlock);
119         for (i = 0; i < RTE_MAX_TAILQ; i++) {
120                 const struct rte_tailq_head *tailq = &mcfg->tailq_head[i];
121                 const struct rte_tailq_entry_head *head = &tailq->tailq_head;
122                 const char *name = "nil";
123
124                 if (rte_tailq_names[i])
125                         name = rte_tailq_names[i];
126                 else if (tailq->name)
127                         name = tailq->name;
128
129                 fprintf(f, "Tailq %u: qname:<%s>, tqh_first:%p, tqh_last:%p\n",
130                         i, name, head->tqh_first, head->tqh_last);
131         }
132         rte_rwlock_read_unlock(&mcfg->qlock);
133 }
134
135 static struct rte_tailq_head *
136 rte_eal_tailq_create(const char *name)
137 {
138         struct rte_tailq_head *head = NULL;
139
140         if (!rte_eal_tailq_lookup(name) &&
141             (rte_tailqs_count + 1 < RTE_MAX_TAILQ)) {
142                 struct rte_mem_config *mcfg;
143
144                 mcfg = rte_eal_get_configuration()->mem_config;
145                 head = &mcfg->tailq_head[rte_tailqs_count];
146                 snprintf(head->name, sizeof(head->name) - 1, "%s", name);
147                 TAILQ_INIT(&head->tailq_head);
148                 rte_tailqs_count++;
149         }
150
151         return head;
152 }
153
154 /* local register, used to store "early" tailqs before rte_eal_init() and to
155  * ensure secondary process only registers tailqs once. */
156 static int
157 rte_eal_tailq_local_register(struct rte_tailq_elem *t)
158 {
159         struct rte_tailq_elem *temp;
160
161         TAILQ_FOREACH(temp, &rte_tailq_elem_head, next) {
162                 if (!strncmp(t->name, temp->name, sizeof(temp->name)))
163                         return -1;
164         }
165
166         TAILQ_INSERT_TAIL(&rte_tailq_elem_head, t, next);
167         return 0;
168 }
169
170 static void
171 rte_eal_tailq_update(struct rte_tailq_elem *t)
172 {
173         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
174                 /* primary process is the only one that creates */
175                 t->head = rte_eal_tailq_create(t->name);
176         } else {
177                 t->head = rte_eal_tailq_lookup(t->name);
178         }
179 }
180
181 int
182 rte_eal_tailq_register(struct rte_tailq_elem *t)
183 {
184         if (rte_eal_tailq_local_register(t) < 0) {
185                 rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
186                         "%s tailq is already registered\n", t->name);
187                 goto error;
188         }
189
190         /* if a register happens after rte_eal_tailqs_init(), then we can update
191          * tailq head */
192         if (rte_tailqs_count >= 0) {
193                 rte_eal_tailq_update(t);
194                 if (t->head == NULL) {
195                         rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
196                                 "Cannot initialize tailq: %s\n", t->name);
197                         TAILQ_REMOVE(&rte_tailq_elem_head, t, next);
198                         goto error;
199                 }
200         }
201
202         return 0;
203
204 error:
205         t->head = NULL;
206         return -1;
207 }
208
209 int
210 rte_eal_tailqs_init(void)
211 {
212         unsigned i;
213         struct rte_mem_config *mcfg = NULL;
214         struct rte_tailq_elem *t;
215
216         RTE_BUILD_BUG_ON(RTE_MAX_TAILQ < RTE_TAILQ_NUM);
217
218         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
219                 mcfg = rte_eal_get_configuration()->mem_config;
220                 for (i = 0; i < RTE_TAILQ_NUM; i++)
221                         TAILQ_INIT(&mcfg->tailq_head[i].tailq_head);
222         }
223
224         /* mark those static entries as already taken */
225         rte_tailqs_count = RTE_TAILQ_NUM;
226
227         TAILQ_FOREACH(t, &rte_tailq_elem_head, next) {
228                 /* second part of register job for "early" tailqs, see
229                  * rte_eal_tailq_register and EAL_REGISTER_TAILQ */
230                 rte_eal_tailq_update(t);
231                 if (t->head == NULL) {
232                         rte_log(RTE_LOG_ERR, RTE_LOGTYPE_EAL,
233                                 "Cannot initialize tailq: %s\n", t->name);
234                         /* no need to TAILQ_REMOVE, we are going to panic in
235                          * rte_eal_init() */
236                         goto fail;
237                 }
238         }
239
240         return 0;
241
242 fail:
243         rte_dump_tailq(stderr);
244         return -1;
245 }