50b7379ad75f985586b47f4b15b30af779d6b176
[dpdk.git] / lib / librte_eal / common / eal_common_tailqs.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <sys/queue.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <inttypes.h>
42
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_launch.h>
46 #include <rte_tailq.h>
47 #include <rte_eal.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_memory.h>
52 #include <rte_atomic.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_log.h>
55 #include <rte_string_fns.h>
56
57 #include "eal_private.h"
58
59 /**
60  * Name of tailq_head
61  */
62 const char* rte_tailq_names[RTE_MAX_TAILQ] = {
63 #define rte_tailq_elem(idx, name)     name,
64 #include <rte_tailq_elem.h>
65 };
66
67 struct rte_tailq_head *
68 rte_eal_tailq_lookup(const char *name)
69 {
70         unsigned i;
71         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
72
73         if (name == NULL)
74                 return NULL;
75
76         for (i = 0; i < RTE_MAX_TAILQ; i++) {
77                 if (rte_tailq_names[i] == NULL)
78                         continue;
79                 if (!strncmp(name, rte_tailq_names[i], RTE_TAILQ_NAMESIZE-1))
80                         return &mcfg->tailq_head[i];
81         }
82
83         return NULL;
84 }
85
86 inline struct rte_tailq_head *
87 rte_eal_tailq_lookup_by_idx(const unsigned tailq_idx)
88 {
89         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
90
91         if (tailq_idx >= RTE_MAX_TAILQ) {
92                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
93                 return NULL;
94         }
95
96         return &mcfg->tailq_head[tailq_idx];
97 }
98
99 struct rte_tailq_head *
100 rte_eal_tailq_reserve(const char *name)
101 {
102         return rte_eal_tailq_lookup(name);
103 }
104
105 inline struct rte_tailq_head *
106 rte_eal_tailq_reserve_by_idx(const unsigned tailq_idx)
107 {
108         return rte_eal_tailq_lookup_by_idx(tailq_idx);
109 }
110
111 void
112 rte_dump_tailq(void)
113 {
114         struct rte_mem_config *mcfg;
115         unsigned i = 0;
116
117         mcfg = rte_eal_get_configuration()->mem_config;
118
119         rte_rwlock_read_lock(&mcfg->qlock);
120         for (i=0; i < RTE_MAX_TAILQ; i++) {
121                 const struct rte_tailq_head *tailq = &mcfg->tailq_head[i];
122                 const struct rte_dummy_head *head = &tailq->tailq_head;
123
124                 printf("Tailq %o: qname:<%s>, tqh_first:%p, tqh_last:%p\n", i,
125                        (rte_tailq_names[i] != NULL ? rte_tailq_names[i]:"nil"),
126                        head->tqh_first, head->tqh_last);
127         }
128         rte_rwlock_read_unlock(&mcfg->qlock);
129 }
130
131 int
132 rte_eal_tailqs_init(void)
133 {
134         unsigned i;
135         struct rte_mem_config *mcfg = NULL; 
136
137         RTE_BUILD_BUG_ON(RTE_MAX_TAILQ < RTE_TAILQ_NUM);
138
139         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
140                 mcfg = rte_eal_get_configuration()->mem_config;
141                 for (i = 0; i < RTE_MAX_TAILQ; i++)
142                         TAILQ_INIT(&mcfg->tailq_head[i].tailq_head);
143         }
144
145         return 0;
146 }
147