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