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