1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
8 #include <rte_common.h>
10 #include <rte_malloc.h>
11 #include <rte_memzone.h>
13 #include "graph_private.h"
16 graph_fp_mem_calc_size(struct graph *graph)
18 struct graph_node *graph_node;
23 sz = sizeof(struct rte_graph);
24 /* Source nodes list */
25 sz += sizeof(rte_graph_off_t) * graph->src_node_count;
26 /* Circular buffer for pending streams of size number of nodes */
27 val = rte_align32pow2(graph->node_count * sizeof(rte_graph_off_t));
28 sz = RTE_ALIGN(sz, val);
29 graph->cir_start = sz;
30 graph->cir_mask = rte_align32pow2(graph->node_count) - 1;
33 sz += sizeof(RTE_GRAPH_FENCE);
34 sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
35 graph->nodes_start = sz;
36 /* For 0..N node objects with fence */
37 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
38 sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
39 sz += sizeof(struct rte_node);
40 /* Pointer to next nodes(edges) */
41 sz += sizeof(struct rte_node *) * graph_node->node->nb_edges;
49 graph_header_popluate(struct graph *_graph)
51 struct rte_graph *graph = _graph->graph;
54 graph->head = (int32_t)-_graph->src_node_count;
55 graph->cir_mask = _graph->cir_mask;
56 graph->nb_nodes = _graph->node_count;
57 graph->cir_start = RTE_PTR_ADD(graph, _graph->cir_start);
58 graph->nodes_start = _graph->nodes_start;
59 graph->socket = _graph->socket;
60 graph->id = _graph->id;
61 memcpy(graph->name, _graph->name, RTE_GRAPH_NAMESIZE);
62 graph->fence = RTE_GRAPH_FENCE;
66 graph_nodes_populate(struct graph *_graph)
68 rte_graph_off_t off = _graph->nodes_start;
69 struct rte_graph *graph = _graph->graph;
70 struct graph_node *graph_node;
71 rte_edge_t count, nb_edges;
75 STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
76 struct rte_node *node = RTE_PTR_ADD(graph, off);
77 memset(node, 0, sizeof(*node));
78 node->fence = RTE_GRAPH_FENCE;
80 node->process = graph_node->node->process;
81 memcpy(node->name, graph_node->node->name, RTE_GRAPH_NAMESIZE);
82 pid = graph_node->node->parent_id;
83 if (pid != RTE_NODE_ID_INVALID) { /* Cloned node */
84 parent = rte_node_id_to_name(pid);
85 memcpy(node->parent, parent, RTE_GRAPH_NAMESIZE);
87 node->id = graph_node->node->id;
88 node->parent_id = pid;
89 nb_edges = graph_node->node->nb_edges;
90 node->nb_edges = nb_edges;
91 off += sizeof(struct rte_node);
92 /* Copy the name in first pass to replace with rte_node* later*/
93 for (count = 0; count < nb_edges; count++)
94 node->nodes[count] = (struct rte_node *)&graph_node
95 ->adjacency_list[count]
98 off += sizeof(struct rte_node *) * nb_edges;
99 off = RTE_ALIGN(off, RTE_CACHE_LINE_SIZE);
101 __rte_node_stream_alloc(graph, node);
106 graph_node_id_to_ptr(const struct rte_graph *graph, rte_node_t id)
110 struct rte_node *node;
112 rte_graph_foreach_node(count, off, graph, node)
113 if (unlikely(node->id == id))
120 graph_node_name_to_ptr(const struct rte_graph *graph, const char *name)
124 struct rte_node *node;
126 rte_graph_foreach_node(count, off, graph, node)
127 if (strncmp(name, node->name, RTE_NODE_NAMESIZE) == 0)
134 graph_node_nexts_populate(struct graph *_graph)
136 rte_node_t count, val;
138 struct rte_node *node;
139 const struct rte_graph *graph = _graph->graph;
142 rte_graph_foreach_node(count, off, graph, node) {
143 for (val = 0; val < node->nb_edges; val++) {
144 name = (const char *)node->nodes[val];
145 node->nodes[val] = graph_node_name_to_ptr(graph, name);
146 if (node->nodes[val] == NULL)
147 SET_ERR_JMP(EINVAL, fail, "%s not found", name);
157 graph_src_nodes_populate(struct graph *_graph)
159 struct rte_graph *graph = _graph->graph;
160 struct graph_node *graph_node;
161 struct rte_node *node;
165 STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
166 if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
167 name = graph_node->node->name;
168 node = graph_node_name_to_ptr(graph, name);
170 SET_ERR_JMP(EINVAL, fail, "%s not found", name);
172 __rte_node_stream_alloc(graph, node);
173 graph->cir_start[head--] = node->off;
183 graph_fp_mem_populate(struct graph *graph)
187 graph_header_popluate(graph);
188 graph_nodes_populate(graph);
189 rc = graph_node_nexts_populate(graph);
190 rc |= graph_src_nodes_populate(graph);
196 graph_fp_mem_create(struct graph *graph)
198 const struct rte_memzone *mz;
201 sz = graph_fp_mem_calc_size(graph);
202 mz = rte_memzone_reserve(graph->name, sz, graph->socket, 0);
204 SET_ERR_JMP(ENOMEM, fail, "Memzone %s reserve failed",
207 graph->graph = mz->addr;
210 return graph_fp_mem_populate(graph);
216 graph_nodes_mem_destroy(struct rte_graph *graph)
220 struct rte_node *node;
225 rte_graph_foreach_node(count, off, graph, node)
226 rte_free(node->objs);
230 graph_fp_mem_destroy(struct graph *graph)
232 graph_nodes_mem_destroy(graph->graph);
233 return rte_memzone_free(graph->mz);