net/mlx5: fix ARM build
[dpdk.git] / lib / librte_eal / common / eal_common_tailqs.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <stdint.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <inttypes.h>
12
13 #include <rte_memory.h>
14 #include <rte_launch.h>
15 #include <rte_eal.h>
16 #include <rte_eal_memconfig.h>
17 #include <rte_per_lcore.h>
18 #include <rte_lcore.h>
19 #include <rte_atomic.h>
20 #include <rte_branch_prediction.h>
21 #include <rte_log.h>
22 #include <rte_string_fns.h>
23 #include <rte_debug.h>
24
25 #include "eal_private.h"
26
27 TAILQ_HEAD(rte_tailq_elem_head, rte_tailq_elem);
28 /* local tailq list */
29 static struct rte_tailq_elem_head rte_tailq_elem_head =
30         TAILQ_HEAD_INITIALIZER(rte_tailq_elem_head);
31
32 /* number of tailqs registered, -1 before call to rte_eal_tailqs_init */
33 static int rte_tailqs_count = -1;
34
35 struct rte_tailq_head *
36 rte_eal_tailq_lookup(const char *name)
37 {
38         unsigned i;
39         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
40
41         if (name == NULL)
42                 return NULL;
43
44         for (i = 0; i < RTE_MAX_TAILQ; i++) {
45                 if (!strncmp(name, mcfg->tailq_head[i].name,
46                              RTE_TAILQ_NAMESIZE-1))
47                         return &mcfg->tailq_head[i];
48         }
49
50         return NULL;
51 }
52
53 void
54 rte_dump_tailq(FILE *f)
55 {
56         struct rte_mem_config *mcfg;
57         unsigned i = 0;
58
59         mcfg = rte_eal_get_configuration()->mem_config;
60
61         rte_rwlock_read_lock(&mcfg->qlock);
62         for (i = 0; i < RTE_MAX_TAILQ; i++) {
63                 const struct rte_tailq_head *tailq = &mcfg->tailq_head[i];
64                 const struct rte_tailq_entry_head *head = &tailq->tailq_head;
65
66                 fprintf(f, "Tailq %u: qname:<%s>, tqh_first:%p, tqh_last:%p\n",
67                         i, tailq->name, head->tqh_first, head->tqh_last);
68         }
69         rte_rwlock_read_unlock(&mcfg->qlock);
70 }
71
72 static struct rte_tailq_head *
73 rte_eal_tailq_create(const char *name)
74 {
75         struct rte_tailq_head *head = NULL;
76
77         if (!rte_eal_tailq_lookup(name) &&
78             (rte_tailqs_count + 1 < RTE_MAX_TAILQ)) {
79                 struct rte_mem_config *mcfg;
80
81                 mcfg = rte_eal_get_configuration()->mem_config;
82                 head = &mcfg->tailq_head[rte_tailqs_count];
83                 snprintf(head->name, sizeof(head->name) - 1, "%s", name);
84                 TAILQ_INIT(&head->tailq_head);
85                 rte_tailqs_count++;
86         }
87
88         return head;
89 }
90
91 /* local register, used to store "early" tailqs before rte_eal_init() and to
92  * ensure secondary process only registers tailqs once. */
93 static int
94 rte_eal_tailq_local_register(struct rte_tailq_elem *t)
95 {
96         struct rte_tailq_elem *temp;
97
98         TAILQ_FOREACH(temp, &rte_tailq_elem_head, next) {
99                 if (!strncmp(t->name, temp->name, sizeof(temp->name)))
100                         return -1;
101         }
102
103         TAILQ_INSERT_TAIL(&rte_tailq_elem_head, t, next);
104         return 0;
105 }
106
107 static void
108 rte_eal_tailq_update(struct rte_tailq_elem *t)
109 {
110         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
111                 /* primary process is the only one that creates */
112                 t->head = rte_eal_tailq_create(t->name);
113         } else {
114                 t->head = rte_eal_tailq_lookup(t->name);
115         }
116 }
117
118 int
119 rte_eal_tailq_register(struct rte_tailq_elem *t)
120 {
121         if (rte_eal_tailq_local_register(t) < 0) {
122                 RTE_LOG(ERR, EAL,
123                         "%s tailq is already registered\n", t->name);
124                 goto error;
125         }
126
127         /* if a register happens after rte_eal_tailqs_init(), then we can update
128          * tailq head */
129         if (rte_tailqs_count >= 0) {
130                 rte_eal_tailq_update(t);
131                 if (t->head == NULL) {
132                         RTE_LOG(ERR, EAL,
133                                 "Cannot initialize tailq: %s\n", t->name);
134                         TAILQ_REMOVE(&rte_tailq_elem_head, t, next);
135                         goto error;
136                 }
137         }
138
139         return 0;
140
141 error:
142         t->head = NULL;
143         return -1;
144 }
145
146 int
147 rte_eal_tailqs_init(void)
148 {
149         struct rte_tailq_elem *t;
150
151         rte_tailqs_count = 0;
152
153         TAILQ_FOREACH(t, &rte_tailq_elem_head, next) {
154                 /* second part of register job for "early" tailqs, see
155                  * rte_eal_tailq_register and EAL_REGISTER_TAILQ */
156                 rte_eal_tailq_update(t);
157                 if (t->head == NULL) {
158                         RTE_LOG(ERR, EAL,
159                                 "Cannot initialize tailq: %s\n", t->name);
160                         /* TAILQ_REMOVE not needed, error is already fatal */
161                         goto fail;
162                 }
163         }
164
165         return 0;
166
167 fail:
168         rte_dump_tailq(stderr);
169         return -1;
170 }