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#ifndef _RTE_TAILQ_H_
35#define _RTE_TAILQ_H_
36
37/**
38 * @file
39 *  Here defines rte_tailq APIs for only internal use
40 *
41 */
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47#include <sys/queue.h>
48#include <stdio.h>
49#include <rte_debug.h>
50
51/** dummy structure type used by the rte_tailq APIs */
52struct rte_tailq_entry {
53	TAILQ_ENTRY(rte_tailq_entry) next; /**< Pointer entries for a tailq list */
54	void *data; /**< Pointer to the data referenced by this tailq entry */
55};
56/** dummy */
57TAILQ_HEAD(rte_tailq_entry_head, rte_tailq_entry);
58
59#define RTE_TAILQ_NAMESIZE 32
60
61/**
62 * The structure defining a tailq header entry for storing
63 * in the rte_config structure in shared memory. Each tailq
64 * is identified by name.
65 * Any library storing a set of objects e.g. rings, mempools, hash-tables,
66 * is recommended to use an entry here, so as to make it easy for
67 * a multi-process app to find already-created elements in shared memory.
68 */
69struct rte_tailq_head {
70	struct rte_tailq_entry_head tailq_head; /**< NOTE: must be first element */
71	char name[RTE_TAILQ_NAMESIZE];
72};
73
74struct rte_tailq_elem {
75	/**
76	 * Reference to head in shared mem, updated at init time by
77	 * rte_eal_tailqs_init()
78	 */
79	struct rte_tailq_head *head;
80	TAILQ_ENTRY(rte_tailq_elem) next;
81	const char name[RTE_TAILQ_NAMESIZE];
82};
83
84/**
85 * Return the first tailq entry casted to the right struct.
86 */
87#define RTE_TAILQ_CAST(tailq_entry, struct_name) \
88	(struct struct_name *)&(tailq_entry)->tailq_head
89
90/**
91 * Utility macro to make looking up a tailqueue for a particular struct easier.
92 *
93 * @param name
94 *   The name of tailq
95 *
96 * @param struct_name
97 *   The name of the list type we are using. (Generally this is the same as the
98 *   first parameter passed to TAILQ_HEAD macro)
99 *
100 * @return
101 *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
102 *   structure pointer type.
103 *   NULL on error, since the tailq_head is the first
104 *   element in the rte_tailq_head structure.
105 */
106#define RTE_TAILQ_LOOKUP(name, struct_name) \
107	RTE_TAILQ_CAST(rte_eal_tailq_lookup(name), struct_name)
108
109/**
110 * Dump tail queues to a file.
111 *
112 * @param f
113 *   A pointer to a file for output
114 */
115void rte_dump_tailq(FILE *f);
116
117/**
118 * Lookup for a tail queue.
119 *
120 * Get a pointer to a tail queue header of a tail
121 * queue identified by the name given as an argument.
122 * Note: this function is not multi-thread safe, and should only be called from
123 * a single thread at a time
124 *
125 * @param name
126 *   The name of the queue.
127 * @return
128 *   A pointer to the tail queue head structure.
129 */
130struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);
131
132/**
133 * Register a tail queue.
134 *
135 * Register a tail queue from shared memory.
136 * This function is mainly used by EAL_REGISTER_TAILQ macro which is used to
137 * register tailq from the different dpdk libraries. Since this macro is a
138 * constructor, the function has no access to dpdk shared memory, so the
139 * registered tailq can not be used before call to rte_eal_init() which calls
140 * rte_eal_tailqs_init().
141 *
142 * @param t
143 *   The tailq element which contains the name of the tailq you want to
144 *   create (/retrieve when in secondary process).
145 * @return
146 *   0 on success or -1 in case of an error.
147 */
148int rte_eal_tailq_register(struct rte_tailq_elem *t);
149
150#define EAL_REGISTER_TAILQ(t) \
151RTE_INIT(tailqinitfn_ ##t); \
152static void tailqinitfn_ ##t(void) \
153{ \
154	if (rte_eal_tailq_register(&t) < 0) \
155		rte_panic("Cannot initialize tailq: %s\n", t.name); \
156}
157
158/* This macro permits both remove and free var within the loop safely.*/
159#ifndef TAILQ_FOREACH_SAFE
160#define TAILQ_FOREACH_SAFE(var, head, field, tvar)		\
161	for ((var) = TAILQ_FIRST((head));			\
162	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);	\
163	    (var) = (tvar))
164#endif
165
166#ifdef __cplusplus
167}
168#endif
169
170#endif /* _RTE_TAILQ_H_ */
171