Branch data Line data Source code
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 : : #include <rte_errno.h>
15 : : #include <rte_string_fns.h>
16 : :
17 : : #include "rte_graph.h"
18 : : #include "rte_graph_worker.h"
19 : :
20 : : extern int rte_graph_logtype;
21 : : #define RTE_LOGTYPE_GRAPH rte_graph_logtype
22 : :
23 : : #define GRAPH_LOG(level, ...) \
24 : : RTE_LOG_LINE_PREFIX(level, GRAPH, \
25 : : "%s():%u ", __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
26 : :
27 : : #define GRAPH_LOG2(level, _fname, _linenum, ...) \
28 : : RTE_LOG_LINE_PREFIX(level, GRAPH, \
29 : : "%s():%u ", _fname RTE_LOG_COMMA _linenum, __VA_ARGS__)
30 : :
31 : : #define graph_err(...) GRAPH_LOG(ERR, __VA_ARGS__)
32 : : #define graph_warn(...) GRAPH_LOG(WARNING, __VA_ARGS__)
33 : : #define graph_info(...) GRAPH_LOG(INFO, __VA_ARGS__)
34 : : #define graph_dbg(...) GRAPH_LOG(DEBUG, __VA_ARGS__)
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 : : unsigned int lcore_id;
53 : : /**< Node runs on the Lcore ID used for mcore dispatch model. */
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 : : struct rte_node_xstats *xstats; /**< Node specific xstats. */
61 : : char next_nodes[][RTE_NODE_NAMESIZE]; /**< Names of next nodes. */
62 : : };
63 : :
64 : : /**
65 : : * @internal
66 : : *
67 : : * Structure that holds the graph scheduling workqueue node stream.
68 : : * Used for mcore dispatch model.
69 : : */
70 : : struct __rte_cache_aligned graph_mcore_dispatch_wq_node {
71 : : rte_graph_off_t node_off;
72 : : uint16_t nb_objs;
73 : : void *objs[RTE_GRAPH_BURST_SIZE];
74 : : };
75 : :
76 : : /**
77 : : * @internal
78 : : *
79 : : * Structure that holds the graph node data.
80 : : */
81 : : struct graph_node {
82 : : STAILQ_ENTRY(graph_node) next; /**< Next graph node in the list. */
83 : : struct node *node; /**< Pointer to internal node. */
84 : : bool visited; /**< Flag used in BFS to mark node visited. */
85 : : uint32_t topo_order; /**< Topological depth from source nodes. */
86 : : struct graph_node *adjacency_list[]; /**< Adjacency list of the node. */
87 : : };
88 : :
89 : : /**
90 : : * @internal
91 : : *
92 : : * Structure that holds graph internal data.
93 : : */
94 : : struct graph {
95 : : STAILQ_ENTRY(graph) next;
96 : : /**< List of graphs. */
97 : : char name[RTE_GRAPH_NAMESIZE];
98 : : /**< Name of the graph. */
99 : : const struct rte_memzone *mz;
100 : : /**< Memzone to store graph data. */
101 : : rte_graph_off_t nodes_start;
102 : : /**< Node memory start offset in graph memory. */
103 : : rte_graph_off_t xstats_start;
104 : : /**< Node xstats memory start offset in graph memory. */
105 : : rte_node_t src_node_count;
106 : : /**< Number of source nodes in a graph. */
107 : : struct rte_graph *graph;
108 : : /**< Pointer to graph data. */
109 : : rte_node_t node_count;
110 : : /**< Total number of nodes. */
111 : : uint32_t sched_table_off;
112 : : /**< Schedule table start offset in graph memory. */
113 : : uint32_t pending_off;
114 : : /**< Pending bitmap start offset in graph memory. */
115 : : uint32_t src_pending_off;
116 : : /**< Source pending bitmap start offset in graph memory. */
117 : : uint16_t nb_sched_words;
118 : : /**< Number of uint64_t words in pending bitmaps. */
119 : : rte_graph_t id;
120 : : /**< Graph identifier. */
121 : : rte_graph_t parent_id;
122 : : /**< Parent graph identifier. */
123 : : unsigned int lcore_id;
124 : : /**< Lcore identifier where the graph prefer to run on. Used for mcore dispatch model. */
125 : : size_t mem_sz;
126 : : /**< Memory size of the graph. */
127 : : int socket;
128 : : /**< Socket identifier where memory is allocated. */
129 : : uint64_t num_pkt_to_capture;
130 : : /**< Number of packets to be captured per core. */
131 : : char pcap_filename[RTE_GRAPH_PCAP_FILE_SZ];
132 : : /**< pcap file name/path. */
133 : : STAILQ_HEAD(gnode_list, graph_node) node_list;
134 : : /**< Nodes in a graph. */
135 : : };
136 : :
137 : : /* Node and graph common functions */
138 : : /**
139 : : * @internal
140 : : *
141 : : * Naming a cloned graph or node by appending a string to base name.
142 : : *
143 : : * @param new_name
144 : : * Pointer to the name of the cloned object.
145 : : * @param base_name
146 : : * Pointer to the name of original object.
147 : : * @param append_str
148 : : * Pointer to the appended string.
149 : : *
150 : : * @return
151 : : * 0 on success, negative errno value otherwise.
152 : : */
153 : 14 : static inline int clone_name(char *new_name, char *base_name, const char *append_str)
154 : : {
155 : : ssize_t sz, rc;
156 : :
157 : : #define SZ RTE_MIN(RTE_NODE_NAMESIZE, RTE_GRAPH_NAMESIZE)
158 : 14 : rc = rte_strscpy(new_name, base_name, SZ);
159 [ - + ]: 14 : if (rc < 0)
160 : 0 : goto fail;
161 : : sz = rc;
162 : 14 : rc = rte_strscpy(new_name + sz, "-", RTE_MAX((int16_t)(SZ - sz), 0));
163 [ - + ]: 14 : if (rc < 0)
164 : 0 : goto fail;
165 : 14 : sz += rc;
166 : 14 : sz = rte_strscpy(new_name + sz, append_str, RTE_MAX((int16_t)(SZ - sz), 0));
167 [ - + ]: 14 : if (sz < 0)
168 : 0 : goto fail;
169 : :
170 : : return 0;
171 : 0 : fail:
172 : 0 : rte_errno = E2BIG;
173 : 0 : return -rte_errno;
174 : : }
175 : :
176 : : /* Node functions */
177 : : STAILQ_HEAD(node_head, node);
178 : :
179 : : /**
180 : : * @internal
181 : : *
182 : : * Get the head of the node list.
183 : : *
184 : : * @return
185 : : * Pointer to the node head.
186 : : */
187 : : struct node_head *node_list_head_get(void);
188 : :
189 : : /**
190 : : * @internal
191 : : *
192 : : * Get node pointer from node name.
193 : : *
194 : : * @param name
195 : : * Pointer to character string containing the node name.
196 : : *
197 : : * @return
198 : : * Pointer to the node.
199 : : */
200 : : struct node *node_from_name(const char *name);
201 : :
202 : : /**
203 : : * @internal
204 : : *
205 : : * Override process func of a node.
206 : : *
207 : : * @return
208 : : * - 0: Success.
209 : : * - <0: Error
210 : : */
211 : : int node_override_process_func(rte_node_t id, rte_node_process_t process);
212 : :
213 : : /* Graph list functions */
214 : : STAILQ_HEAD(graph_head, graph);
215 : :
216 : : /**
217 : : * @internal
218 : : *
219 : : * Get the head of the graph list.
220 : : *
221 : : * @return
222 : : * Pointer to the graph head.
223 : : */
224 : : struct graph_head *graph_list_head_get(void);
225 : :
226 : : rte_spinlock_t *
227 : : graph_spinlock_get(void);
228 : :
229 : : /* Lock functions */
230 : : /**
231 : : * @internal
232 : : *
233 : : * Take a lock on the graph internal spin lock.
234 : : */
235 : : void graph_spinlock_lock(void)
236 : : __rte_acquire_capability(graph_spinlock_get());
237 : :
238 : : /**
239 : : * @internal
240 : : *
241 : : * Release a lock on the graph internal spin lock.
242 : : */
243 : : void graph_spinlock_unlock(void)
244 : : __rte_release_capability(graph_spinlock_get());
245 : :
246 : : /* Graph operations */
247 : : /**
248 : : * @internal
249 : : *
250 : : * Run a BFS(Breadth First Search) on the graph marking all
251 : : * the graph nodes as visited.
252 : : *
253 : : * @param graph
254 : : * Pointer to the internal graph object.
255 : : * @param start
256 : : * Pointer to the starting graph node.
257 : : *
258 : : * @return
259 : : * - 0: Success.
260 : : * - -ENOMEM: Not enough memory for BFS.
261 : : */
262 : : int graph_bfs(struct graph *graph, struct graph_node *start);
263 : :
264 : : /**
265 : : * @internal
266 : : *
267 : : * Check if there is an isolated node in the given graph.
268 : : *
269 : : * @param graph
270 : : * Pointer to the internal graph object.
271 : : *
272 : : * @return
273 : : * - 0: No isolated node found.
274 : : * - 1: Isolated node found.
275 : : */
276 : : int graph_has_isolated_node(struct graph *graph);
277 : :
278 : : /**
279 : : * @internal
280 : : *
281 : : * Check whether a node in the graph has next_node to a source node.
282 : : *
283 : : * @param graph
284 : : * Pointer to the internal graph object.
285 : : *
286 : : * @return
287 : : * - 0: Node has an edge to source node.
288 : : * - 1: Node doesn't have an edge to source node.
289 : : */
290 : : int graph_node_has_edge_to_src_node(struct graph *graph);
291 : :
292 : : /**
293 : : * @internal
294 : : *
295 : : * Checks whether node in the graph has a edge to itself i.e. forms a
296 : : * loop.
297 : : *
298 : : * @param graph
299 : : * Pointer to the internal graph object.
300 : : *
301 : : * @return
302 : : * - 0: Node has an edge to itself.
303 : : * - 1: Node doesn't have an edge to itself.
304 : : */
305 : : int graph_node_has_loop_edge(struct graph *graph);
306 : :
307 : : /**
308 : : * @internal
309 : : *
310 : : * Replace all pointers of a given node with another one in all active graphs.
311 : : *
312 : : * @param old
313 : : * Node pointer to replace in all graphs.
314 : : * @param new
315 : : * Updated pointer.
316 : : */
317 : : void graph_node_replace_all(struct node *old, struct node *new);
318 : :
319 : : /**
320 : : * @internal
321 : : *
322 : : * Get the count of source nodes in the graph.
323 : : *
324 : : * @param graph
325 : : * Pointer to the internal graph object.
326 : : *
327 : : * @return
328 : : * Number of source nodes.
329 : : */
330 : : rte_node_t graph_src_nodes_count(struct graph *graph);
331 : :
332 : : /**
333 : : * @internal
334 : : *
335 : : * Get the count of total number of nodes in the graph.
336 : : *
337 : : * @param graph
338 : : * Pointer to the internal graph object.
339 : : *
340 : : * @return
341 : : * Number of nodes.
342 : : */
343 : : rte_node_t graph_nodes_count(struct graph *graph);
344 : :
345 : : /**
346 : : * @internal
347 : : *
348 : : * Clear the visited flag of all the nodes in the graph.
349 : : *
350 : : * @param graph
351 : : * Pointer to the internal graph object.
352 : : */
353 : : void graph_mark_nodes_as_not_visited(struct graph *graph);
354 : :
355 : : /**
356 : : * @internal
357 : : *
358 : : * Compute topological depth for all nodes via BFS from source nodes.
359 : : *
360 : : * @param graph
361 : : * Pointer to the internal graph object.
362 : : *
363 : : * @return
364 : : * - 0: Success.
365 : : * - <0: Not enough memory for BFS queue.
366 : : */
367 : : int graph_topo_order_compute(struct graph *graph);
368 : :
369 : : /* Fast path graph memory populate unctions */
370 : :
371 : : /**
372 : : * @internal
373 : : *
374 : : * Create fast-path memory for the graph and nodes.
375 : : *
376 : : * @param graph
377 : : * Pointer to the internal graph object.
378 : : *
379 : : * @return
380 : : * - 0: Success.
381 : : * - -ENOMEM: Not enough for graph and nodes.
382 : : * - -EINVAL: Graph nodes not found.
383 : : */
384 : : int graph_fp_mem_create(struct graph *graph);
385 : :
386 : : /**
387 : : * @internal
388 : : *
389 : : * Free fast-path memory used by graph and nodes.
390 : : *
391 : : * @param graph
392 : : * Pointer to the internal graph object.
393 : : *
394 : : * @return
395 : : * - 0: Success.
396 : : * - <0: Graph memzone related error.
397 : : */
398 : : int graph_fp_mem_destroy(struct graph *graph);
399 : :
400 : : /**
401 : : * @internal
402 : : *
403 : : * Rebuild the source pending bitmap based on lcore affinity.
404 : : *
405 : : * @param graph
406 : : * Pointer to the internal graph object.
407 : : */
408 : : void graph_src_bitmap_rebuild(struct graph *graph);
409 : :
410 : : /* Lookup functions */
411 : : /**
412 : : * @internal
413 : : *
414 : : * Get graph node object from node id.
415 : : *
416 : : * @param graph
417 : : * Pointer to rte_graph object.
418 : : * @param id
419 : : * Node Identifier.
420 : : *
421 : : * @return
422 : : * Pointer to rte_node if identifier is valid else NULL.
423 : : */
424 : : struct rte_node *graph_node_id_to_ptr(const struct rte_graph *graph,
425 : : rte_node_t id);
426 : :
427 : : /**
428 : : * @internal
429 : : *
430 : : * Get graph node object from node name.
431 : : *
432 : : * @param graph
433 : : * Pointer to rte_graph object.
434 : : * @param node_name
435 : : * Pointer to character string holding the node name.
436 : : *
437 : : * @return
438 : : * Pointer to rte_node if identifier is valid else NULL.
439 : : */
440 : : struct rte_node *graph_node_name_to_ptr(const struct rte_graph *graph,
441 : : const char *node_name);
442 : :
443 : : /* Debug functions */
444 : : /**
445 : : * @internal
446 : : *
447 : : * Dump internal graph object data.
448 : : *
449 : : * @param f
450 : : * FILE pointer to dump the data.
451 : : * @param g
452 : : * Pointer to the internal graph object.
453 : : */
454 : : void graph_dump(FILE *f, struct graph *g);
455 : :
456 : : /**
457 : : * @internal
458 : : *
459 : : * Dump internal node object data.
460 : : *
461 : : * @param f
462 : : * FILE pointer to dump the info.
463 : : * @param g
464 : : * Pointer to the internal node object.
465 : : */
466 : : void node_dump(FILE *f, struct node *n);
467 : :
468 : : /**
469 : : * @internal
470 : : *
471 : : * Create the graph schedule work queue for mcore dispatch model.
472 : : * All cloned graphs attached to the parent graph MUST be destroyed together
473 : : * for fast schedule design limitation.
474 : : *
475 : : * @param _graph
476 : : * The graph object
477 : : * @param _parent_graph
478 : : * The parent graph object which holds the run-queue head.
479 : : * @param prm
480 : : * Graph parameter, includes model-specific parameters in this graph.
481 : : *
482 : : * @return
483 : : * - 0: Success.
484 : : * - <0: Graph schedule work queue related error.
485 : : */
486 : : int graph_sched_wq_create(struct graph *_graph, struct graph *_parent_graph,
487 : : struct rte_graph_param *prm);
488 : :
489 : : /**
490 : : * @internal
491 : : *
492 : : * Destroy the graph schedule work queue for mcore dispatch model.
493 : : *
494 : : * @param _graph
495 : : * The graph object
496 : : */
497 : : void graph_sched_wq_destroy(struct graph *_graph);
498 : :
499 : : /**
500 : : * @internal
501 : : *
502 : : * Check if given node present in any of the created graphs.
503 : : *
504 : : * @param node
505 : : * The node object
506 : : *
507 : : * @return
508 : : * 0 if not present in any graph, else return 1.
509 : : */
510 : : bool graph_is_node_active_in_graph(struct node *_node);
511 : :
512 : : #endif /* _RTE_GRAPH_PRIVATE_H_ */
|