220a35e2a98335c8b401b64023b14dc89a473b33
[dpdk.git] / lib / librte_graph / graph_private.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4
5 #ifndef _RTE_GRAPH_PRIVATE_H_
6 #define _RTE_GRAPH_PRIVATE_H_
7
8 #include <inttypes.h>
9 #include <sys/queue.h>
10
11 #include <rte_common.h>
12 #include <rte_eal.h>
13
14 #include "rte_graph.h"
15
16 extern int rte_graph_logtype;
17
18 #define GRAPH_LOG(level, ...)                                                  \
19         rte_log(RTE_LOG_##level, rte_graph_logtype,                            \
20                 RTE_FMT("GRAPH: %s():%u " RTE_FMT_HEAD(__VA_ARGS__, ) "\n",    \
21                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__, )))
22
23 #define graph_err(...) GRAPH_LOG(ERR, __VA_ARGS__)
24 #define graph_info(...) GRAPH_LOG(INFO, __VA_ARGS__)
25 #define graph_dbg(...) GRAPH_LOG(DEBUG, __VA_ARGS__)
26
27 #define ID_CHECK(id, id_max)                                                   \
28         do {                                                                   \
29                 if ((id) >= (id_max)) {                                        \
30                         rte_errno = EINVAL;                                    \
31                         goto fail;                                             \
32                 }                                                              \
33         } while (0)
34
35 #define SET_ERR_JMP(err, where, fmt, ...)                                      \
36         do {                                                                   \
37                 graph_err(fmt, ##__VA_ARGS__);                                 \
38                 rte_errno = err;                                               \
39                 goto where;                                                    \
40         } while (0)
41
42 /**
43  * @internal
44  *
45  * Structure that holds node internal data.
46  */
47 struct node {
48         STAILQ_ENTRY(node) next;      /**< Next node in the list. */
49         char name[RTE_NODE_NAMESIZE]; /**< Name of the node. */
50         uint64_t flags;               /**< Node configuration flag. */
51         rte_node_process_t process;   /**< Node process function. */
52         rte_node_init_t init;         /**< Node init function. */
53         rte_node_fini_t fini;         /**< Node fini function. */
54         rte_node_t id;                /**< Allocated identifier for the node. */
55         rte_node_t parent_id;         /**< Parent node identifier. */
56         rte_edge_t nb_edges;          /**< Number of edges from this node. */
57         char next_nodes[][RTE_NODE_NAMESIZE]; /**< Names of next nodes. */
58 };
59
60 /**
61  * @internal
62  *
63  * Structure that holds the graph node data.
64  */
65 struct graph_node {
66         STAILQ_ENTRY(graph_node) next; /**< Next graph node in the list. */
67         struct node *node; /**< Pointer to internal node. */
68         bool visited;      /**< Flag used in BFS to mark node visited. */
69         struct graph_node *adjacency_list[]; /**< Adjacency list of the node. */
70 };
71
72 /**
73  * @internal
74  *
75  * Structure that holds graph internal data.
76  */
77 struct graph {
78         STAILQ_ENTRY(graph) next;
79         /**< List of graphs. */
80         char name[RTE_GRAPH_NAMESIZE];
81         /**< Name of the graph. */
82         const struct rte_memzone *mz;
83         /**< Memzone to store graph data. */
84         rte_graph_off_t nodes_start;
85         /**< Node memory start offset in graph reel. */
86         rte_node_t src_node_count;
87         /**< Number of source nodes in a graph. */
88         struct rte_graph *graph;
89         /**< Pointer to graph data. */
90         rte_node_t node_count;
91         /**< Total number of nodes. */
92         uint32_t cir_start;
93         /**< Circular buffer start offset in graph reel. */
94         uint32_t cir_mask;
95         /**< Circular buffer mask for wrap around. */
96         rte_graph_t id;
97         /**< Graph identifier. */
98         size_t mem_sz;
99         /**< Memory size of the graph. */
100         int socket;
101         /**< Socket identifier where memory is allocated. */
102         STAILQ_HEAD(gnode_list, graph_node) node_list;
103         /**< Nodes in a graph. */
104 };
105
106 /* Node functions */
107 STAILQ_HEAD(node_head, node);
108
109 /**
110  * @internal
111  *
112  * Get the head of the node list.
113  *
114  * @return
115  *   Pointer to the node head.
116  */
117 struct node_head *node_list_head_get(void);
118
119 /**
120  * @internal
121  *
122  * Get node pointer from node name.
123  *
124  * @param name
125  *   Pointer to character string containing the node name.
126  *
127  * @return
128  *   Pointer to the node.
129  */
130 struct node *node_from_name(const char *name);
131
132 /* Graph list functions */
133 STAILQ_HEAD(graph_head, graph);
134
135 /**
136  * @internal
137  *
138  * Get the head of the graph list.
139  *
140  * @return
141  *   Pointer to the graph head.
142  */
143 struct graph_head *graph_list_head_get(void);
144
145 /* Lock functions */
146 /**
147  * @internal
148  *
149  * Take a lock on the graph internal spin lock.
150  */
151 void graph_spinlock_lock(void);
152
153 /**
154  * @internal
155  *
156  * Release a lock on the graph internal spin lock.
157  */
158 void graph_spinlock_unlock(void);
159
160 /* Graph operations */
161 /**
162  * @internal
163  *
164  * Run a BFS(Breadth First Search) on the graph marking all
165  * the graph nodes as visited.
166  *
167  * @param graph
168  *   Pointer to the internal graph object.
169  * @param start
170  *   Pointer to the starting graph node.
171  *
172  * @return
173  *   - 0: Success.
174  *   - -ENOMEM: Not enough memory for BFS.
175  */
176 int graph_bfs(struct graph *graph, struct graph_node *start);
177
178 /**
179  * @internal
180  *
181  * Check if there is an isolated node in the given graph.
182  *
183  * @param graph
184  *   Pointer to the internal graph object.
185  *
186  * @return
187  *   - 0: No isolated node found.
188  *   - 1: Isolated node found.
189  */
190 int graph_has_isolated_node(struct graph *graph);
191
192 /**
193  * @internal
194  *
195  * Check whether a node in the graph has next_node to a source node.
196  *
197  * @param graph
198  *   Pointer to the internal graph object.
199  *
200  * @return
201  *   - 0: Node has an edge to source node.
202  *   - 1: Node doesn't have an edge to source node.
203  */
204 int graph_node_has_edge_to_src_node(struct graph *graph);
205
206 /**
207  * @internal
208  *
209  * Checks whether node in the graph has a edge to itself i.e. forms a
210  * loop.
211  *
212  * @param graph
213  *   Pointer to the internal graph object.
214  *
215  * @return
216  *   - 0: Node has an edge to itself.
217  *   - 1: Node doesn't have an edge to itself.
218  */
219 int graph_node_has_loop_edge(struct graph *graph);
220
221 /**
222  * @internal
223  *
224  * Get the count of source nodes in the graph.
225  *
226  * @param graph
227  *   Pointer to the internal graph object.
228  *
229  * @return
230  *   Number of source nodes.
231  */
232 rte_node_t graph_src_nodes_count(struct graph *graph);
233
234 /**
235  * @internal
236  *
237  * Get the count of total number of nodes in the graph.
238  *
239  * @param graph
240  *   Pointer to the internal graph object.
241  *
242  * @return
243  *   Number of nodes.
244  */
245 rte_node_t graph_nodes_count(struct graph *graph);
246
247 /**
248  * @internal
249  *
250  * Clear the visited flag of all the nodes in the graph.
251  *
252  * @param graph
253  *   Pointer to the internal graph object.
254  */
255 void graph_mark_nodes_as_not_visited(struct graph *graph);
256
257
258 /**
259  * @internal
260  *
261  * Dump internal node object data.
262  *
263  * @param f
264  *   FILE pointer to dump the info.
265  * @param g
266  *   Pointer to the internal node object.
267  */
268 void node_dump(FILE *f, struct node *n);
269
270 #endif /* _RTE_GRAPH_PRIVATE_H_ */