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