1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
6 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_memzone.h>
11 #include "graph_private.h"
14 graph_fp_mem_calc_size(struct graph *graph)
16 struct graph_node *graph_node;
21 sz = sizeof(struct rte_graph);
22 /* Source nodes list */
23 sz += sizeof(rte_graph_off_t) * graph->src_node_count;
24 /* Circular buffer for pending streams of size number of nodes */
25 val = rte_align32pow2(graph->node_count * sizeof(rte_graph_off_t));
26 sz = RTE_ALIGN(sz, val);
27 graph->cir_start = sz;
28 graph->cir_mask = rte_align32pow2(graph->node_count) - 1;
31 sz += sizeof(RTE_GRAPH_FENCE);
32 sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
33 graph->nodes_start = sz;
34 /* For 0..N node objects with fence */
35 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
36 sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
37 sz += sizeof(struct rte_node);
38 /* Pointer to next nodes(edges) */
39 sz += sizeof(struct rte_node *) * graph_node->node->nb_edges;
47 graph_header_popluate(struct graph *_graph)
49 struct rte_graph *graph = _graph->graph;
52 graph->head = (int32_t)-_graph->src_node_count;
53 graph->cir_mask = _graph->cir_mask;
54 graph->nb_nodes = _graph->node_count;
55 graph->cir_start = RTE_PTR_ADD(graph, _graph->cir_start);
56 graph->nodes_start = _graph->nodes_start;
57 graph->socket = _graph->socket;
58 graph->id = _graph->id;
59 memcpy(graph->name, _graph->name, RTE_GRAPH_NAMESIZE);
60 graph->fence = RTE_GRAPH_FENCE;
64 graph_nodes_populate(struct graph *_graph)
66 rte_graph_off_t off = _graph->nodes_start;
67 struct rte_graph *graph = _graph->graph;
68 struct graph_node *graph_node;
69 rte_edge_t count, nb_edges;
73 STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
74 struct rte_node *node = RTE_PTR_ADD(graph, off);
75 memset(node, 0, sizeof(*node));
76 node->fence = RTE_GRAPH_FENCE;
78 node->process = graph_node->node->process;
79 memcpy(node->name, graph_node->node->name, RTE_GRAPH_NAMESIZE);
80 pid = graph_node->node->parent_id;
81 if (pid != RTE_NODE_ID_INVALID) { /* Cloned node */
82 parent = rte_node_id_to_name(pid);
83 memcpy(node->parent, parent, RTE_GRAPH_NAMESIZE);
85 node->id = graph_node->node->id;
86 node->parent_id = pid;
87 nb_edges = graph_node->node->nb_edges;
88 node->nb_edges = nb_edges;
89 off += sizeof(struct rte_node);
90 /* Copy the name in first pass to replace with rte_node* later*/
91 for (count = 0; count < nb_edges; count++)
92 node->nodes[count] = (struct rte_node *)&graph_node
93 ->adjacency_list[count]
96 off += sizeof(struct rte_node *) * nb_edges;
97 off = RTE_ALIGN(off, RTE_CACHE_LINE_SIZE);
99 __rte_node_stream_alloc(graph, node);
104 graph_node_id_to_ptr(const struct rte_graph *graph, rte_node_t id)
108 struct rte_node *node;
110 rte_graph_foreach_node(count, off, graph, node)
111 if (unlikely(node->id == id))
118 graph_node_name_to_ptr(const struct rte_graph *graph, const char *name)
122 struct rte_node *node;
124 rte_graph_foreach_node(count, off, graph, node)
125 if (strncmp(name, node->name, RTE_NODE_NAMESIZE) == 0)
132 graph_node_nexts_populate(struct graph *_graph)
134 rte_node_t count, val;
136 struct rte_node *node;
137 const struct rte_graph *graph = _graph->graph;
140 rte_graph_foreach_node(count, off, graph, node) {
141 for (val = 0; val < node->nb_edges; val++) {
142 name = (const char *)node->nodes[val];
143 node->nodes[val] = graph_node_name_to_ptr(graph, name);
144 if (node->nodes[val] == NULL)
145 SET_ERR_JMP(EINVAL, fail, "%s not found", name);
155 graph_src_nodes_populate(struct graph *_graph)
157 struct rte_graph *graph = _graph->graph;
158 struct graph_node *graph_node;
159 struct rte_node *node;
163 STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
164 if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
165 name = graph_node->node->name;
166 node = graph_node_name_to_ptr(graph, name);
168 SET_ERR_JMP(EINVAL, fail, "%s not found", name);
170 __rte_node_stream_alloc(graph, node);
171 graph->cir_start[head--] = node->off;
181 graph_fp_mem_populate(struct graph *graph)
185 graph_header_popluate(graph);
186 graph_nodes_populate(graph);
187 rc = graph_node_nexts_populate(graph);
188 rc |= graph_src_nodes_populate(graph);
194 graph_fp_mem_create(struct graph *graph)
196 const struct rte_memzone *mz;
199 sz = graph_fp_mem_calc_size(graph);
200 mz = rte_memzone_reserve(graph->name, sz, graph->socket, 0);
202 SET_ERR_JMP(ENOMEM, fail, "Memzone %s reserve failed",
205 graph->graph = mz->addr;
208 return graph_fp_mem_populate(graph);
214 graph_nodes_mem_destroy(struct rte_graph *graph)
218 struct rte_node *node;
223 rte_graph_foreach_node(count, off, graph, node)
224 rte_free(node->objs);
228 graph_fp_mem_destroy(struct graph *graph)
230 graph_nodes_mem_destroy(graph->graph);
231 return rte_memzone_free(graph->mz);