1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
8 #include <rte_common.h>
10 #include <rte_errno.h>
11 #include <rte_malloc.h>
12 #include <rte_memzone.h>
13 #include <rte_spinlock.h>
14 #include <rte_string_fns.h>
16 #include "graph_private.h"
18 static struct graph_head graph_list = STAILQ_HEAD_INITIALIZER(graph_list);
19 static rte_spinlock_t graph_lock = RTE_SPINLOCK_INITIALIZER;
20 static rte_graph_t graph_id;
22 #define GRAPH_ID_CHECK(id) ID_CHECK(id, graph_id)
24 /* Private functions */
26 graph_list_head_get(void)
32 graph_spinlock_lock(void)
34 rte_spinlock_lock(&graph_lock);
38 graph_spinlock_unlock(void)
40 rte_spinlock_unlock(&graph_lock);
44 graph_node_add(struct graph *graph, struct node *node)
46 struct graph_node *graph_node;
49 /* Skip the duplicate nodes */
50 STAILQ_FOREACH(graph_node, &graph->node_list, next)
51 if (strncmp(node->name, graph_node->node->name,
52 RTE_NODE_NAMESIZE) == 0)
55 /* Allocate new graph node object */
56 sz = sizeof(*graph_node) + node->nb_edges * sizeof(struct node *);
57 graph_node = calloc(1, sz);
59 if (graph_node == NULL)
60 SET_ERR_JMP(ENOMEM, free, "Failed to calloc %s object",
63 /* Initialize the graph node */
64 graph_node->node = node;
66 /* Add to graph node list */
67 STAILQ_INSERT_TAIL(&graph->node_list, graph_node, next);
75 static struct graph_node *
76 node_to_graph_node(struct graph *graph, struct node *node)
78 struct graph_node *graph_node;
80 STAILQ_FOREACH(graph_node, &graph->node_list, next)
81 if (graph_node->node == node)
84 SET_ERR_JMP(ENODEV, fail, "Found isolated node %s", node->name);
90 graph_node_edges_add(struct graph *graph)
92 struct graph_node *graph_node;
93 struct node *adjacency;
97 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
98 for (i = 0; i < graph_node->node->nb_edges; i++) {
99 next = graph_node->node->next_nodes[i];
100 adjacency = node_from_name(next);
101 if (adjacency == NULL)
102 SET_ERR_JMP(EINVAL, fail,
103 "Node %s not registered", next);
104 if (graph_node_add(graph, adjacency))
114 graph_adjacency_list_update(struct graph *graph)
116 struct graph_node *graph_node, *tmp;
117 struct node *adjacency;
121 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
122 for (i = 0; i < graph_node->node->nb_edges; i++) {
123 next = graph_node->node->next_nodes[i];
124 adjacency = node_from_name(next);
125 if (adjacency == NULL)
126 SET_ERR_JMP(EINVAL, fail,
127 "Node %s not registered", next);
128 tmp = node_to_graph_node(graph, adjacency);
131 graph_node->adjacency_list[i] = tmp;
141 expand_pattern_to_node(struct graph *graph, const char *pattern)
143 struct node_head *node_head = node_list_head_get();
147 /* Check for pattern match */
148 STAILQ_FOREACH(node, node_head, next) {
149 if (fnmatch(pattern, node->name, 0) == 0) {
150 if (graph_node_add(graph, node))
156 SET_ERR_JMP(EFAULT, fail, "Pattern %s node not found", pattern);
164 graph_cleanup(struct graph *graph)
166 struct graph_node *graph_node;
168 while (!STAILQ_EMPTY(&graph->node_list)) {
169 graph_node = STAILQ_FIRST(&graph->node_list);
170 STAILQ_REMOVE_HEAD(&graph->node_list, next);
176 graph_node_init(struct graph *graph)
178 struct graph_node *graph_node;
182 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
183 if (graph_node->node->init) {
184 name = graph_node->node->name;
185 rc = graph_node->node->init(
187 graph_node_name_to_ptr(graph->graph, name));
189 SET_ERR_JMP(rc, err, "Node %s init() failed",
200 graph_node_fini(struct graph *graph)
202 struct graph_node *graph_node;
204 STAILQ_FOREACH(graph_node, &graph->node_list, next)
205 if (graph_node->node->fini)
206 graph_node->node->fini(
208 graph_node_name_to_ptr(graph->graph,
209 graph_node->node->name));
212 static struct rte_graph *
213 graph_mem_fixup_node_ctx(struct rte_graph *graph)
215 struct rte_node *node;
216 struct node *node_db;
221 rte_graph_foreach_node(count, off, graph, node) {
222 if (node->parent_id == RTE_NODE_ID_INVALID) /* Static node */
224 else /* Cloned node */
227 node_db = node_from_name(name);
229 SET_ERR_JMP(ENOLINK, fail, "Node %s not found", name);
230 node->process = node_db->process;
238 static struct rte_graph *
239 graph_mem_fixup_secondary(struct rte_graph *graph)
241 if (graph == NULL || rte_eal_process_type() == RTE_PROC_PRIMARY)
244 return graph_mem_fixup_node_ctx(graph);
248 rte_graph_lookup(const char *name)
250 const struct rte_memzone *mz;
251 struct rte_graph *rc = NULL;
253 mz = rte_memzone_lookup(name);
257 return graph_mem_fixup_secondary(rc);
261 rte_graph_create(const char *name, struct rte_graph_param *prm)
263 rte_node_t src_node_count;
268 graph_spinlock_lock();
270 /* Check arguments sanity */
272 SET_ERR_JMP(EINVAL, fail, "Param should not be NULL");
275 SET_ERR_JMP(EINVAL, fail, "Graph name should not be NULL");
277 /* Check for existence of duplicate graph */
278 STAILQ_FOREACH(graph, &graph_list, next)
279 if (strncmp(name, graph->name, RTE_GRAPH_NAMESIZE) == 0)
280 SET_ERR_JMP(EEXIST, fail, "Found duplicate graph %s",
283 /* Create graph object */
284 graph = calloc(1, sizeof(*graph));
286 SET_ERR_JMP(ENOMEM, fail, "Failed to calloc graph object");
288 /* Initialize the graph object */
289 STAILQ_INIT(&graph->node_list);
290 if (rte_strscpy(graph->name, name, RTE_GRAPH_NAMESIZE) < 0)
291 SET_ERR_JMP(E2BIG, free, "Too big name=%s", name);
293 /* Expand node pattern and add the nodes to the graph */
294 for (i = 0; i < prm->nb_node_patterns; i++) {
295 pattern = prm->node_patterns[i];
296 if (expand_pattern_to_node(graph, pattern))
300 /* Go over all the nodes edges and add them to the graph */
301 if (graph_node_edges_add(graph))
304 /* Update adjacency list of all nodes in the graph */
305 if (graph_adjacency_list_update(graph))
308 /* Make sure at least a source node present in the graph */
309 src_node_count = graph_src_nodes_count(graph);
310 if (src_node_count == 0)
313 /* Make sure no node is pointing to source node */
314 if (graph_node_has_edge_to_src_node(graph))
317 /* Don't allow node has loop to self */
318 if (graph_node_has_loop_edge(graph))
321 /* Do BFS from src nodes on the graph to find isolated nodes */
322 if (graph_has_isolated_node(graph))
325 /* Initialize graph object */
326 graph->socket = prm->socket_id;
327 graph->src_node_count = src_node_count;
328 graph->node_count = graph_nodes_count(graph);
329 graph->id = graph_id;
331 /* Allocate the Graph fast path memory and populate the data */
332 if (graph_fp_mem_create(graph))
335 /* Call init() of the all the nodes in the graph */
336 if (graph_node_init(graph))
337 goto graph_mem_destroy;
339 /* All good, Lets add the graph to the list */
341 STAILQ_INSERT_TAIL(&graph_list, graph, next);
343 graph_spinlock_unlock();
347 graph_fp_mem_destroy(graph);
349 graph_cleanup(graph);
353 graph_spinlock_unlock();
354 return RTE_GRAPH_ID_INVALID;
358 rte_graph_destroy(rte_graph_t id)
360 struct graph *graph, *tmp;
363 graph_spinlock_lock();
365 graph = STAILQ_FIRST(&graph_list);
366 while (graph != NULL) {
367 tmp = STAILQ_NEXT(graph, next);
368 if (graph->id == id) {
369 /* Call fini() of the all the nodes in the graph */
370 graph_node_fini(graph);
371 /* Destroy graph fast path memory */
372 rc = graph_fp_mem_destroy(graph);
374 SET_ERR_JMP(rc, done, "Graph %s destroy failed",
377 graph_cleanup(graph);
378 STAILQ_REMOVE(&graph_list, graph, graph, next);
386 graph_spinlock_unlock();
391 rte_graph_from_name(const char *name)
395 STAILQ_FOREACH(graph, &graph_list, next)
396 if (strncmp(graph->name, name, RTE_GRAPH_NAMESIZE) == 0)
399 return RTE_GRAPH_ID_INVALID;
403 rte_graph_id_to_name(rte_graph_t id)
408 STAILQ_FOREACH(graph, &graph_list, next)
417 rte_graph_node_get(rte_graph_t gid, uint32_t nid)
419 struct rte_node *node;
425 STAILQ_FOREACH(graph, &graph_list, next)
426 if (graph->id == gid) {
427 rte_graph_foreach_node(count, off, graph->graph,
439 rte_graph_node_get_by_name(const char *graph_name, const char *node_name)
441 struct rte_node *node;
446 STAILQ_FOREACH(graph, &graph_list, next)
447 if (!strncmp(graph->name, graph_name, RTE_GRAPH_NAMESIZE)) {
448 rte_graph_foreach_node(count, off, graph->graph,
450 if (!strncmp(node->name, node_name,
461 __rte_node_stream_alloc(struct rte_graph *graph, struct rte_node *node)
463 uint16_t size = node->size;
465 RTE_VERIFY(size != UINT16_MAX);
466 /* Allocate double amount of size to avoid immediate realloc */
467 size = RTE_MIN(UINT16_MAX, RTE_MAX(RTE_GRAPH_BURST_SIZE, size * 2));
468 node->objs = rte_realloc_socket(node->objs, size * sizeof(void *),
469 RTE_CACHE_LINE_SIZE, graph->socket);
470 RTE_VERIFY(node->objs);
472 node->realloc_count++;
476 __rte_node_stream_alloc_size(struct rte_graph *graph, struct rte_node *node,
479 uint16_t size = node->size;
481 RTE_VERIFY(size != UINT16_MAX);
482 /* Allocate double amount of size to avoid immediate realloc */
483 size = RTE_MIN(UINT16_MAX, RTE_MAX(RTE_GRAPH_BURST_SIZE, req_size * 2));
484 node->objs = rte_realloc_socket(node->objs, size * sizeof(void *),
485 RTE_CACHE_LINE_SIZE, graph->socket);
486 RTE_VERIFY(node->objs);
488 node->realloc_count++;
492 graph_to_dot(FILE *f, struct graph *graph)
494 const char *src_edge_color = " [color=blue]\n";
495 const char *edge_color = "\n";
496 struct graph_node *graph_node;
501 rc = fprintf(f, "Digraph %s {\n\trankdir=LR;\n", graph->name);
505 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
506 node_name = graph_node->node->name;
507 for (i = 0; i < graph_node->node->nb_edges; i++) {
508 rc = fprintf(f, "\t\"%s\"->\"%s\"%s", node_name,
509 graph_node->adjacency_list[i]->node->name,
510 graph_node->node->flags & RTE_NODE_SOURCE_F
517 rc = fprintf(f, "}\n");
528 rte_graph_export(const char *name, FILE *f)
533 STAILQ_FOREACH(graph, &graph_list, next) {
534 if (strncmp(graph->name, name, RTE_GRAPH_NAMESIZE) == 0) {
535 rc = graph_to_dot(f, graph);
544 graph_scan_dump(FILE *f, rte_graph_t id, bool all)
551 STAILQ_FOREACH(graph, &graph_list, next) {
553 graph_dump(f, graph);
554 } else if (graph->id == id) {
555 graph_dump(f, graph);
564 rte_graph_dump(FILE *f, rte_graph_t id)
566 graph_scan_dump(f, id, false);
570 rte_graph_list_dump(FILE *f)
572 graph_scan_dump(f, 0, true);
576 rte_graph_max_count(void)
581 RTE_LOG_REGISTER_DEFAULT(rte_graph_logtype, INFO);