1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
13 * All functions in this file may be changed or removed without prior notice.
15 * Graph architecture abstracts the data processing functions as
16 * "node" and "link" them together to create a complex "graph" to enable
17 * reusable/modular data processing functions.
19 * This API enables graph framework operations such as create, lookup,
20 * dump and destroy on graph and node operations such as clone,
21 * edge update, and edge shrink, etc. The API also allows to create the stats
22 * cluster to monitor per graph and per node stats.
29 #include <rte_common.h>
30 #include <rte_compat.h>
36 #define RTE_GRAPH_NAMESIZE 64 /**< Max length of graph name. */
37 #define RTE_NODE_NAMESIZE 64 /**< Max length of node name. */
38 #define RTE_GRAPH_OFF_INVALID UINT32_MAX /**< Invalid graph offset. */
39 #define RTE_NODE_ID_INVALID UINT32_MAX /**< Invalid node id. */
40 #define RTE_EDGE_ID_INVALID UINT16_MAX /**< Invalid edge id. */
41 #define RTE_GRAPH_ID_INVALID UINT16_MAX /**< Invalid graph id. */
42 #define RTE_GRAPH_FENCE 0xdeadbeef12345678ULL /**< Graph fence data. */
44 typedef uint32_t rte_graph_off_t; /**< Graph offset type. */
45 typedef uint32_t rte_node_t; /**< Node id type. */
46 typedef uint16_t rte_edge_t; /**< Edge id type. */
47 typedef uint16_t rte_graph_t; /**< Graph id type. */
49 /** Burst size in terms of log2 */
50 #if RTE_GRAPH_BURST_SIZE == 1
51 #define RTE_GRAPH_BURST_SIZE_LOG2 0 /**< Object burst size of 1. */
52 #elif RTE_GRAPH_BURST_SIZE == 2
53 #define RTE_GRAPH_BURST_SIZE_LOG2 1 /**< Object burst size of 2. */
54 #elif RTE_GRAPH_BURST_SIZE == 4
55 #define RTE_GRAPH_BURST_SIZE_LOG2 2 /**< Object burst size of 4. */
56 #elif RTE_GRAPH_BURST_SIZE == 8
57 #define RTE_GRAPH_BURST_SIZE_LOG2 3 /**< Object burst size of 8. */
58 #elif RTE_GRAPH_BURST_SIZE == 16
59 #define RTE_GRAPH_BURST_SIZE_LOG2 4 /**< Object burst size of 16. */
60 #elif RTE_GRAPH_BURST_SIZE == 32
61 #define RTE_GRAPH_BURST_SIZE_LOG2 5 /**< Object burst size of 32. */
62 #elif RTE_GRAPH_BURST_SIZE == 64
63 #define RTE_GRAPH_BURST_SIZE_LOG2 6 /**< Object burst size of 64. */
64 #elif RTE_GRAPH_BURST_SIZE == 128
65 #define RTE_GRAPH_BURST_SIZE_LOG2 7 /**< Object burst size of 128. */
66 #elif RTE_GRAPH_BURST_SIZE == 256
67 #define RTE_GRAPH_BURST_SIZE_LOG2 8 /**< Object burst size of 256. */
69 #error "Unsupported burst size"
72 /* Forward declaration */
73 struct rte_node; /**< Node object */
74 struct rte_graph; /**< Graph object */
75 struct rte_graph_cluster_stats; /**< Stats for Cluster of graphs */
76 struct rte_graph_cluster_node_stats; /**< Node stats within cluster of graphs */
79 * Node process function.
81 * The function invoked when the worker thread walks on nodes using
85 * Pointer to the graph object.
87 * Pointer to the node object.
89 * Pointer to an array of objects to be processed.
91 * Number of objects in the array.
94 * Number of objects processed.
96 * @see rte_graph_walk()
99 typedef uint16_t (*rte_node_process_t)(struct rte_graph *graph,
100 struct rte_node *node, void **objs,
104 * Node initialization function.
106 * The function invoked when the user creates the graph using rte_graph_create()
109 * Pointer to the graph object.
111 * Pointer to the node object.
117 * @see rte_graph_create()
119 typedef int (*rte_node_init_t)(const struct rte_graph *graph,
120 struct rte_node *node);
123 * Node finalization function.
125 * The function invoked when the user destroys the graph using
126 * rte_graph_destroy().
129 * Pointer to the graph object.
131 * Pointer to the node object.
133 * @see rte_graph_destroy()
135 typedef void (*rte_node_fini_t)(const struct rte_graph *graph,
136 struct rte_node *node);
139 * Graph cluster stats callback.
142 * Flag to denote that stats are of the first node.
144 * Flag to denote that stats are of the last node.
146 * Cookie supplied during stats creation.
148 * Node cluster stats data.
154 typedef int (*rte_graph_cluster_stats_cb_t)(bool is_first, bool is_last,
155 void *cookie, const struct rte_graph_cluster_node_stats *stats);
158 * Structure to hold configuration parameters for creating the graph.
160 * @see rte_graph_create()
162 struct rte_graph_param {
163 int socket_id; /**< Socket id where memory is allocated. */
164 uint16_t nb_node_patterns; /**< Number of node patterns. */
165 const char **node_patterns;
166 /**< Array of node patterns based on shell pattern. */
170 * Structure to hold configuration parameters for graph cluster stats create.
172 * @see rte_graph_cluster_stats_create()
174 struct rte_graph_cluster_stats_param {
176 /**< Socket id where memory is allocated */
177 rte_graph_cluster_stats_cb_t fn;
178 /**< Stats print callback function. NULL value allowed, in that case,
179 * default print stat function used.
184 FILE *f; /**< File pointer to dump the stats when fn == NULL. */
186 uint16_t nb_graph_patterns; /**< Number of graph patterns. */
187 const char **graph_patterns;
188 /**< Array of graph patterns based on shell pattern. */
192 * Node cluster stats data structure.
194 * @see struct rte_graph_cluster_stats_param::fn
196 struct rte_graph_cluster_node_stats {
197 uint64_t ts; /**< Current timestamp. */
198 uint64_t calls; /**< Current number of calls made. */
199 uint64_t objs; /**< Current number of objs processed. */
200 uint64_t cycles; /**< Current number of cycles. */
202 uint64_t prev_ts; /**< Previous call timestamp. */
203 uint64_t prev_calls; /**< Previous number of calls. */
204 uint64_t prev_objs; /**< Previous number of processed objs. */
205 uint64_t prev_cycles; /**< Previous number of cycles. */
207 uint64_t realloc_count; /**< Realloc count. */
209 rte_node_t id; /**< Node identifier of stats. */
210 uint64_t hz; /**< Cycles per seconds. */
211 char name[RTE_NODE_NAMESIZE]; /**< Name of the node. */
212 } __rte_cache_aligned;
217 * Create memory reel, detect loops and find isolated nodes.
220 * Unique name for this graph.
222 * Graph parameter, includes node names and count to be included
226 * Unique graph id on success, RTE_GRAPH_ID_INVALID otherwise.
229 rte_graph_t rte_graph_create(const char *name, struct rte_graph_param *prm);
234 * Free Graph memory reel.
237 * id of the graph to destroy.
240 * 0 on success, error otherwise.
243 int rte_graph_destroy(rte_graph_t id);
246 * Get graph id from graph name.
249 * Name of the graph to get id.
252 * Graph id on success, RTE_GRAPH_ID_INVALID otherwise.
255 rte_graph_t rte_graph_from_name(const char *name);
258 * Get graph name from graph id.
261 * id of the graph to get name.
264 * Graph name on success, NULL otherwise.
267 char *rte_graph_id_to_name(rte_graph_t id);
270 * Export the graph as graph viz dot file
273 * Name of the graph to export.
275 * File pointer to export the graph.
278 * 0 on success, error otherwise.
281 int rte_graph_export(const char *name, FILE *f);
284 * Get graph object from its name.
286 * Typical usage of this API to get graph objects in the worker thread and
287 * followed calling rte_graph_walk() in a loop.
293 * Graph pointer on success, NULL otherwise.
295 * @see rte_graph_walk()
298 struct rte_graph *rte_graph_lookup(const char *name);
301 * Get maximum number of graph available.
304 * Maximum graph count.
307 rte_graph_t rte_graph_max_count(void);
310 * Dump the graph information to file.
313 * File pointer to dump graph info.
315 * Graph id to get graph info.
318 void rte_graph_dump(FILE *f, rte_graph_t id);
321 * Dump all graphs information to file
324 * File pointer to dump graph info.
327 void rte_graph_list_dump(FILE *f);
330 * Dump graph information along with node info to file
333 * File pointer to dump graph info.
335 * Graph pointer to get graph info.
337 * true to dump nodes in the graph.
340 void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
342 /** Macro to browse rte_node object after the graph creation */
343 #define rte_graph_foreach_node(count, off, graph, node) \
344 for (count = 0, off = graph->nodes_start, \
345 node = RTE_PTR_ADD(graph, off); \
346 count < graph->nb_nodes; \
347 off = node->next, node = RTE_PTR_ADD(graph, off), count++)
350 * Get node object with in graph from id.
353 * Graph id to get node pointer from.
355 * Node id to get node pointer.
358 * Node pointer on success, NULL otherwise.
361 struct rte_node *rte_graph_node_get(rte_graph_t graph_id, rte_node_t node_id);
364 * Get node pointer with in graph from name.
367 * Graph name to get node pointer from.
369 * Node name to get the node pointer.
372 * Node pointer on success, NULL otherwise.
375 struct rte_node *rte_graph_node_get_by_name(const char *graph,
379 * Create graph stats cluster to aggregate runtime node stats.
382 * Parameters including file pointer to dump stats,
383 * Graph pattern to create cluster and callback function.
386 * Valid pointer on success, NULL otherwise.
389 struct rte_graph_cluster_stats *rte_graph_cluster_stats_create(
390 const struct rte_graph_cluster_stats_param *prm);
393 * Destroy cluster stats.
396 * Valid cluster pointer to destroy.
399 void rte_graph_cluster_stats_destroy(struct rte_graph_cluster_stats *stat);
402 * Get stats to application.
407 * true to skip callback function invocation.
410 void rte_graph_cluster_stats_get(struct rte_graph_cluster_stats *stat,
414 * Reset cluster stats to zero.
417 * Valid cluster stats pointer.
420 void rte_graph_cluster_stats_reset(struct rte_graph_cluster_stats *stat);
423 * Structure defines the node registration parameters.
425 * @see __rte_node_register(), RTE_NODE_REGISTER()
427 struct rte_node_register {
428 char name[RTE_NODE_NAMESIZE]; /**< Name of the node. */
429 uint64_t flags; /**< Node configuration flag. */
430 #define RTE_NODE_SOURCE_F (1ULL << 0) /**< Node type is source. */
431 rte_node_process_t process; /**< Node process function. */
432 rte_node_init_t init; /**< Node init function. */
433 rte_node_fini_t fini; /**< Node fini function. */
434 rte_node_t id; /**< Node Identifier. */
435 rte_node_t parent_id; /**< Identifier of parent node. */
436 rte_edge_t nb_edges; /**< Number of edges from this node. */
437 const char *next_nodes[]; /**< Names of next nodes. */
441 * Register new packet processing node. Nodes can be registered
442 * dynamically via this call or statically via the RTE_NODE_REGISTER
446 * Valid node pointer with name, process function and next_nodes.
449 * Valid node id on success, RTE_NODE_ID_INVALID otherwise.
451 * @see RTE_NODE_REGISTER()
454 rte_node_t __rte_node_register(const struct rte_node_register *node);
457 * Register a static node.
459 * The static node is registered through the constructor scheme, thereby, it can
460 * be used in a multi-process scenario.
463 * Valid node pointer with name, process function, and next_nodes.
465 #define RTE_NODE_REGISTER(node) \
466 RTE_INIT(rte_node_register_##node) \
468 node.parent_id = RTE_NODE_ID_INVALID; \
469 node.id = __rte_node_register(&node); \
473 * Clone a node from static node(node created from RTE_NODE_REGISTER).
476 * Static node id to clone from.
478 * Name of the new node. The library prepends the parent node name to the
479 * user-specified name. The final node name will be,
480 * "parent node name" + "-" + name.
483 * Valid node id on success, RTE_NODE_ID_INVALID otherwise.
486 rte_node_t rte_node_clone(rte_node_t id, const char *name);
489 * Get node id from node name.
492 * Valid node name. In the case of the cloned node, the name will be
493 * "parent node name" + "-" + name.
496 * Valid node id on success, RTE_NODE_ID_INVALID otherwise.
499 rte_node_t rte_node_from_name(const char *name);
502 * Get node name from node id.
508 * Valid node name on success, NULL otherwise.
511 char *rte_node_id_to_name(rte_node_t id);
514 * Get the number of edges(next-nodes) for a node from node id.
520 * Valid edge count on success, RTE_EDGE_ID_INVALID otherwise.
523 rte_edge_t rte_node_edge_count(rte_node_t id);
526 * Update the edges for a node from node id.
531 * Index to update the edges from. RTE_EDGE_ID_INVALID is valid,
532 * in that case, it will be added to the end of the list.
534 * Name of the edges to update.
536 * Number of edges to update.
539 * Valid edge count on success, 0 otherwise.
542 rte_edge_t rte_node_edge_update(rte_node_t id, rte_edge_t from,
543 const char **next_nodes, uint16_t nb_edges);
546 * Shrink the edges to a given size.
551 * New size to shrink the edges.
554 * New size on success, RTE_EDGE_ID_INVALID otherwise.
557 rte_edge_t rte_node_edge_shrink(rte_node_t id, rte_edge_t size);
560 * Get the edge names from a given node.
564 * @param[out] next_nodes
565 * Buffer to copy the edge names. The NULL value is allowed in that case,
566 * the function returns the size of the array that needs to be allocated.
569 * When next_nodes == NULL, it returns the size of the array else
570 * number of item copied.
573 rte_node_t rte_node_edge_get(rte_node_t id, char *next_nodes[]);
576 * Get maximum nodes available.
579 * Maximum nodes count.
582 rte_node_t rte_node_max_count(void);
585 * Dump node info to file.
588 * File pointer to dump the node info.
590 * Node id to get the info.
593 void rte_node_dump(FILE *f, rte_node_t id);
596 * Dump all node info to file.
599 * File pointer to dump the node info.
602 void rte_node_list_dump(FILE *f);
605 * Test the validity of node id.
611 * 1 if valid id, 0 otherwise.
613 static __rte_always_inline int
614 rte_node_is_invalid(rte_node_t id)
616 return (id == RTE_NODE_ID_INVALID);
620 * Test the validity of edge id.
623 * Edge node id to check.
626 * 1 if valid id, 0 otherwise.
628 static __rte_always_inline int
629 rte_edge_is_invalid(rte_edge_t id)
631 return (id == RTE_EDGE_ID_INVALID);
635 * Test the validity of graph id.
641 * 1 if valid id, 0 otherwise.
643 static __rte_always_inline int
644 rte_graph_is_invalid(rte_graph_t id)
646 return (id == RTE_GRAPH_ID_INVALID);
650 * Test stats feature support.
653 * 1 if stats enabled, 0 otherwise.
655 static __rte_always_inline int
656 rte_graph_has_stats_feature(void)
658 #ifdef RTE_LIBRTE_GRAPH_STATS
659 return RTE_LIBRTE_GRAPH_STATS;
669 #endif /* _RTE_GRAPH_H_ */