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