lib: remove librte_ prefix from directory names
[dpdk.git] / lib / 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 #include "rte_graph_worker.h"
16
17 extern int rte_graph_logtype;
18
19 #define GRAPH_LOG(level, ...)                                                  \
20         rte_log(RTE_LOG_##level, rte_graph_logtype,                            \
21                 RTE_FMT("GRAPH: %s():%u " RTE_FMT_HEAD(__VA_ARGS__, ) "\n",    \
22                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__, )))
23
24 #define graph_err(...) GRAPH_LOG(ERR, __VA_ARGS__)
25 #define graph_info(...) GRAPH_LOG(INFO, __VA_ARGS__)
26 #define graph_dbg(...) GRAPH_LOG(DEBUG, __VA_ARGS__)
27
28 #define ID_CHECK(id, id_max)                                                   \
29         do {                                                                   \
30                 if ((id) >= (id_max)) {                                        \
31                         rte_errno = EINVAL;                                    \
32                         goto fail;                                             \
33                 }                                                              \
34         } while (0)
35
36 #define SET_ERR_JMP(err, where, fmt, ...)                                      \
37         do {                                                                   \
38                 graph_err(fmt, ##__VA_ARGS__);                                 \
39                 rte_errno = err;                                               \
40                 goto where;                                                    \
41         } while (0)
42
43 /**
44  * @internal
45  *
46  * Structure that holds node internal data.
47  */
48 struct node {
49         STAILQ_ENTRY(node) next;      /**< Next node in the list. */
50         char name[RTE_NODE_NAMESIZE]; /**< Name of the node. */
51         uint64_t flags;               /**< Node configuration flag. */
52         rte_node_process_t process;   /**< Node process function. */
53         rte_node_init_t init;         /**< Node init function. */
54         rte_node_fini_t fini;         /**< Node fini function. */
55         rte_node_t id;                /**< Allocated identifier for the node. */
56         rte_node_t parent_id;         /**< Parent node identifier. */
57         rte_edge_t nb_edges;          /**< Number of edges from this node. */
58         char next_nodes[][RTE_NODE_NAMESIZE]; /**< Names of next nodes. */
59 };
60
61 /**
62  * @internal
63  *
64  * Structure that holds the graph node data.
65  */
66 struct graph_node {
67         STAILQ_ENTRY(graph_node) next; /**< Next graph node in the list. */
68         struct node *node; /**< Pointer to internal node. */
69         bool visited;      /**< Flag used in BFS to mark node visited. */
70         struct graph_node *adjacency_list[]; /**< Adjacency list of the node. */
71 };
72
73 /**
74  * @internal
75  *
76  * Structure that holds graph internal data.
77  */
78 struct graph {
79         STAILQ_ENTRY(graph) next;
80         /**< List of graphs. */
81         char name[RTE_GRAPH_NAMESIZE];
82         /**< Name of the graph. */
83         const struct rte_memzone *mz;
84         /**< Memzone to store graph data. */
85         rte_graph_off_t nodes_start;
86         /**< Node memory start offset in graph reel. */
87         rte_node_t src_node_count;
88         /**< Number of source nodes in a graph. */
89         struct rte_graph *graph;
90         /**< Pointer to graph data. */
91         rte_node_t node_count;
92         /**< Total number of nodes. */
93         uint32_t cir_start;
94         /**< Circular buffer start offset in graph reel. */
95         uint32_t cir_mask;
96         /**< Circular buffer mask for wrap around. */
97         rte_graph_t id;
98         /**< Graph identifier. */
99         size_t mem_sz;
100         /**< Memory size of the graph. */
101         int socket;
102         /**< Socket identifier where memory is allocated. */
103         STAILQ_HEAD(gnode_list, graph_node) node_list;
104         /**< Nodes in a graph. */
105 };
106
107 /* Node functions */
108 STAILQ_HEAD(node_head, node);
109
110 /**
111  * @internal
112  *
113  * Get the head of the node list.
114  *
115  * @return
116  *   Pointer to the node head.
117  */
118 struct node_head *node_list_head_get(void);
119
120 /**
121  * @internal
122  *
123  * Get node pointer from node name.
124  *
125  * @param name
126  *   Pointer to character string containing the node name.
127  *
128  * @return
129  *   Pointer to the node.
130  */
131 struct node *node_from_name(const char *name);
132
133 /* Graph list functions */
134 STAILQ_HEAD(graph_head, graph);
135
136 /**
137  * @internal
138  *
139  * Get the head of the graph list.
140  *
141  * @return
142  *   Pointer to the graph head.
143  */
144 struct graph_head *graph_list_head_get(void);
145
146 /* Lock functions */
147 /**
148  * @internal
149  *
150  * Take a lock on the graph internal spin lock.
151  */
152 void graph_spinlock_lock(void);
153
154 /**
155  * @internal
156  *
157  * Release a lock on the graph internal spin lock.
158  */
159 void graph_spinlock_unlock(void);
160
161 /* Graph operations */
162 /**
163  * @internal
164  *
165  * Run a BFS(Breadth First Search) on the graph marking all
166  * the graph nodes as visited.
167  *
168  * @param graph
169  *   Pointer to the internal graph object.
170  * @param start
171  *   Pointer to the starting graph node.
172  *
173  * @return
174  *   - 0: Success.
175  *   - -ENOMEM: Not enough memory for BFS.
176  */
177 int graph_bfs(struct graph *graph, struct graph_node *start);
178
179 /**
180  * @internal
181  *
182  * Check if there is an isolated node in the given graph.
183  *
184  * @param graph
185  *   Pointer to the internal graph object.
186  *
187  * @return
188  *   - 0: No isolated node found.
189  *   - 1: Isolated node found.
190  */
191 int graph_has_isolated_node(struct graph *graph);
192
193 /**
194  * @internal
195  *
196  * Check whether a node in the graph has next_node to a source node.
197  *
198  * @param graph
199  *   Pointer to the internal graph object.
200  *
201  * @return
202  *   - 0: Node has an edge to source node.
203  *   - 1: Node doesn't have an edge to source node.
204  */
205 int graph_node_has_edge_to_src_node(struct graph *graph);
206
207 /**
208  * @internal
209  *
210  * Checks whether node in the graph has a edge to itself i.e. forms a
211  * loop.
212  *
213  * @param graph
214  *   Pointer to the internal graph object.
215  *
216  * @return
217  *   - 0: Node has an edge to itself.
218  *   - 1: Node doesn't have an edge to itself.
219  */
220 int graph_node_has_loop_edge(struct graph *graph);
221
222 /**
223  * @internal
224  *
225  * Get the count of source nodes in the graph.
226  *
227  * @param graph
228  *   Pointer to the internal graph object.
229  *
230  * @return
231  *   Number of source nodes.
232  */
233 rte_node_t graph_src_nodes_count(struct graph *graph);
234
235 /**
236  * @internal
237  *
238  * Get the count of total number of nodes in the graph.
239  *
240  * @param graph
241  *   Pointer to the internal graph object.
242  *
243  * @return
244  *   Number of nodes.
245  */
246 rte_node_t graph_nodes_count(struct graph *graph);
247
248 /**
249  * @internal
250  *
251  * Clear the visited flag of all the nodes in the graph.
252  *
253  * @param graph
254  *   Pointer to the internal graph object.
255  */
256 void graph_mark_nodes_as_not_visited(struct graph *graph);
257
258 /* Fast path graph memory populate unctions */
259
260 /**
261  * @internal
262  *
263  * Create fast-path memory for the graph and nodes.
264  *
265  * @param graph
266  *   Pointer to the internal graph object.
267  *
268  * @return
269  *   - 0: Success.
270  *   - -ENOMEM: Not enough for graph and nodes.
271  *   - -EINVAL: Graph nodes not found.
272  */
273 int graph_fp_mem_create(struct graph *graph);
274
275 /**
276  * @internal
277  *
278  * Free fast-path memory used by graph and nodes.
279  *
280  * @param graph
281  *   Pointer to the internal graph object.
282  *
283  * @return
284  *   - 0: Success.
285  *   - <0: Graph memzone related error.
286  */
287 int graph_fp_mem_destroy(struct graph *graph);
288
289 /* Lookup functions */
290 /**
291  * @internal
292  *
293  * Get graph node object from node id.
294  *
295  * @param graph
296  *   Pointer to rte_graph object.
297  * @param id
298  *   Node Identifier.
299  *
300  * @return
301  *   Pointer to rte_node if identifier is valid else NULL.
302  */
303 struct rte_node *graph_node_id_to_ptr(const struct rte_graph *graph,
304                                       rte_node_t id);
305
306 /**
307  * @internal
308  *
309  * Get graph node object from node name.
310  *
311  * @param graph
312  *   Pointer to rte_graph object.
313  * @param node_name
314  *   Pointer to character string holding the node name.
315  *
316  * @return
317  *   Pointer to rte_node if identifier is valid else NULL.
318  */
319 struct rte_node *graph_node_name_to_ptr(const struct rte_graph *graph,
320                                         const char *node_name);
321
322 /* Debug functions */
323 /**
324  * @internal
325  *
326  * Dump internal graph object data.
327  *
328  * @param f
329  *   FILE pointer to dump the data.
330  * @param g
331  *   Pointer to the internal graph object.
332  */
333 void graph_dump(FILE *f, struct graph *g);
334
335 /**
336  * @internal
337  *
338  * Dump internal node object data.
339  *
340  * @param f
341  *   FILE pointer to dump the info.
342  * @param g
343  *   Pointer to the internal node object.
344  */
345 void node_dump(FILE *f, struct node *n);
346
347 #endif /* _RTE_GRAPH_PRIVATE_H_ */