Imported Upstream version 16.04
[deb_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 TAILQ_HEAD(rte_tailq_elem_head, rte_tailq_elem);
59 /* local tailq list */
60 static struct rte_tailq_elem_head rte_tailq_elem_head =
61         TAILQ_HEAD_INITIALIZER(rte_tailq_elem_head);
62
63 /* number of tailqs registered, -1 before call to rte_eal_tailqs_init */
64 static int rte_tailqs_count = -1;
65
66 struct rte_tailq_head *
67 rte_eal_tailq_lookup(const char *name)
68 {
69         unsigned i;
70         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
71
72         if (name == NULL)
73                 return NULL;
74
75         for (i = 0; i < RTE_MAX_TAILQ; i++) {
76                 if (!strncmp(name, mcfg->tailq_head[i].name,
77                              RTE_TAILQ_NAMESIZE-1))
78                         return &mcfg->tailq_head[i];
79         }
80
81         return NULL;
82 }
83
84 void
85 rte_dump_tailq(FILE *f)
86 {
87         struct rte_mem_config *mcfg;
88         unsigned i = 0;
89
90         mcfg = rte_eal_get_configuration()->mem_config;
91
92         rte_rwlock_read_lock(&mcfg->qlock);
93         for (i = 0; i < RTE_MAX_TAILQ; i++) {
94                 const struct rte_tailq_head *tailq = &mcfg->tailq_head[i];
95                 const struct rte_tailq_entry_head *head = &tailq->tailq_head;
96
97                 fprintf(f, "Tailq %u: qname:<%s>, tqh_first:%p, tqh_last:%p\n",
98                         i, tailq->name, head->tqh_first, head->tqh_last);
99         }
100         rte_rwlock_read_unlock(&mcfg->qlock);
101 }
102
103 static struct rte_tailq_head *
104 rte_eal_tailq_create(const char *name)
105 {
106         struct rte_tailq_head *head = NULL;
107
108         if (!rte_eal_tailq_lookup(name) &&
109             (rte_tailqs_count + 1 < RTE_MAX_TAILQ)) {
110                 struct rte_mem_config *mcfg;
111
112                 mcfg = rte_eal_get_configuration()->mem_config;
113                 head = &mcfg->tailq_head[rte_tailqs_count];
114                 snprintf(head->name, sizeof(head->name) - 1, "%s", name);
115                 TAILQ_INIT(&head->tailq_head);
116                 rte_tailqs_count++;
117         }
118
119         return head;
120 }
121
122 /* local register, used to store "early" tailqs before rte_eal_init() and to
123  * ensure secondary process only registers tailqs once. */
124 static int
125 rte_eal_tailq_local_register(struct rte_tailq_elem *t)
126 {
127         struct rte_tailq_elem *temp;
128
129         TAILQ_FOREACH(temp, &rte_tailq_elem_head, next) {
130                 if (!strncmp(t->name, temp->name, sizeof(temp->name)))
131                         return -1;
132         }
133
134         TAILQ_INSERT_TAIL(&rte_tailq_elem_head, t, next);
135         return 0;
136 }
137
138 static void
139 rte_eal_tailq_update(struct rte_tailq_elem *t)
140 {
141         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
142                 /* primary process is the only one that creates */
143                 t->head = rte_eal_tailq_create(t->name);
144         } else {
145                 t->head = rte_eal_tailq_lookup(t->name);
146         }
147 }
148
149 int
150 rte_eal_tailq_register(struct rte_tailq_elem *t)
151 {
152         if (rte_eal_tailq_local_register(t) < 0) {
153                 RTE_LOG(ERR, EAL,
154                         "%s tailq is already registered\n", t->name);
155                 goto error;
156         }
157
158         /* if a register happens after rte_eal_tailqs_init(), then we can update
159          * tailq head */
160         if (rte_tailqs_count >= 0) {
161                 rte_eal_tailq_update(t);
162                 if (t->head == NULL) {
163                         RTE_LOG(ERR, EAL,
164                                 "Cannot initialize tailq: %s\n", t->name);
165                         TAILQ_REMOVE(&rte_tailq_elem_head, t, next);
166                         goto error;
167                 }
168         }
169
170         return 0;
171
172 error:
173         t->head = NULL;
174         return -1;
175 }
176
177 int
178 rte_eal_tailqs_init(void)
179 {
180         struct rte_tailq_elem *t;
181
182         rte_tailqs_count = 0;
183
184         TAILQ_FOREACH(t, &rte_tailq_elem_head, next) {
185                 /* second part of register job for "early" tailqs, see
186                  * rte_eal_tailq_register and EAL_REGISTER_TAILQ */
187                 rte_eal_tailq_update(t);
188                 if (t->head == NULL) {
189                         RTE_LOG(ERR, EAL,
190                                 "Cannot initialize tailq: %s\n", t->name);
191                         /* no need to TAILQ_REMOVE, we are going to panic in
192                          * rte_eal_init() */
193                         goto fail;
194                 }
195         }
196
197         return 0;
198
199 fail:
200         rte_dump_tailq(stderr);
201         return -1;
202 }