Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2020 Marvell International Ltd.
3 : : */
4 : :
5 : :
6 : : #include <stdlib.h>
7 : :
8 : : #include <rte_common.h>
9 : : #include <rte_errno.h>
10 : : #include <rte_malloc.h>
11 : : #include <rte_memzone.h>
12 : :
13 : : #include "graph_private.h"
14 : : #include "graph_pcap_private.h"
15 : :
16 : : static size_t
17 : 13 : graph_fp_mem_calc_size(struct graph *graph)
18 : : {
19 : : struct graph_node *graph_node;
20 : : uint16_t nwords;
21 : : size_t sz;
22 : :
23 : : /* Graph header */
24 : : sz = sizeof(struct rte_graph);
25 : :
26 : : /* Schedule table: node offset indexed by sched_idx */
27 : : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
28 : 13 : graph->sched_table_off = sz;
29 : 13 : sz += sizeof(rte_graph_off_t) * graph->node_count;
30 : :
31 : : /* Pending and source pending bitmaps */
32 : 13 : nwords = (graph->node_count + 63) / 64;
33 : 13 : graph->nb_sched_words = nwords;
34 : 13 : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
35 : 13 : graph->pending_off = sz;
36 : 13 : sz += sizeof(uint64_t) * nwords;
37 : 13 : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
38 : 13 : graph->src_pending_off = sz;
39 : 13 : sz += sizeof(uint64_t) * nwords;
40 : :
41 : : /* Fence */
42 : : sz += sizeof(RTE_GRAPH_FENCE);
43 : 13 : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
44 : 13 : graph->nodes_start = sz;
45 : : /* For 0..N node objects with fence */
46 [ + + ]: 96 : STAILQ_FOREACH(graph_node, &graph->node_list, next) {
47 : 83 : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
48 : 83 : sz += sizeof(struct rte_node);
49 : : /* Pointer to next nodes(edges) */
50 : 83 : sz += sizeof(struct rte_node *) * graph_node->node->nb_edges;
51 : : }
52 : 13 : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
53 : 13 : graph->xstats_start = sz;
54 : : /* For 0..N node objects with xstats */
55 [ + + ]: 96 : STAILQ_FOREACH(graph_node, &graph->node_list, next) {
56 [ + - ]: 83 : if (graph_node->node->xstats == NULL)
57 : 83 : continue;
58 : 0 : sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
59 : 0 : sz += sizeof(uint64_t) * graph_node->node->xstats->nb_xstats;
60 : : }
61 : :
62 : 13 : graph->mem_sz = sz;
63 : 13 : return sz;
64 : : }
65 : :
66 : : static void
67 : 13 : graph_header_populate(struct graph *_graph)
68 : : {
69 : 13 : struct rte_graph *graph = _graph->graph;
70 : :
71 : 13 : graph->nb_nodes = _graph->node_count;
72 : 13 : graph->nb_sched_words = _graph->nb_sched_words;
73 : 13 : graph->sched_table = RTE_PTR_ADD(graph, _graph->sched_table_off);
74 : 13 : graph->pending = RTE_PTR_ADD(graph, _graph->pending_off);
75 : 13 : graph->src_pending = RTE_PTR_ADD(graph, _graph->src_pending_off);
76 : 13 : graph->nodes_start = _graph->nodes_start;
77 : 13 : graph->socket = _graph->socket;
78 : 13 : graph->id = _graph->id;
79 : 13 : memcpy(graph->name, _graph->name, RTE_GRAPH_NAMESIZE);
80 : 13 : graph->fence = RTE_GRAPH_FENCE;
81 : :
82 : 13 : memset(graph->pending, 0, sizeof(uint64_t) * _graph->nb_sched_words);
83 : 13 : memset(graph->src_pending, 0, sizeof(uint64_t) * _graph->nb_sched_words);
84 : 13 : }
85 : :
86 : : static int
87 : 139 : graph_node_topo_cmp(const void *a, const void *b)
88 : : {
89 : : const struct graph_node *const *na = a;
90 : : const struct graph_node *const *nb = b;
91 : :
92 [ + + ]: 139 : if ((*na)->topo_order != (*nb)->topo_order)
93 : 109 : return (int)(*na)->topo_order - (int)(*nb)->topo_order;
94 : 30 : return (int)(*na)->node->id - (int)(*nb)->node->id;
95 : : }
96 : :
97 : : static void
98 : 13 : graph_nodes_populate(struct graph *_graph)
99 : : {
100 : 13 : rte_graph_off_t xstat_off = _graph->xstats_start;
101 : 13 : rte_graph_off_t off = _graph->nodes_start;
102 : 13 : struct rte_graph *graph = _graph->graph;
103 : : struct graph_node **sorted, *graph_node;
104 : : rte_edge_t count, nb_edges;
105 : : rte_node_t pid;
106 : : uint32_t n;
107 : :
108 : : /* Build a sorted array of graph_node pointers by (topo_order, id) */
109 : 13 : sorted = calloc(_graph->node_count, sizeof(*sorted));
110 [ - + ]: 13 : RTE_VERIFY(sorted != NULL);
111 : : n = 0;
112 [ + + ]: 96 : STAILQ_FOREACH(graph_node, &_graph->node_list, next)
113 : 83 : sorted[n++] = graph_node;
114 : 13 : qsort(sorted, n, sizeof(*sorted), graph_node_topo_cmp);
115 : :
116 [ + + ]: 96 : for (n = 0; n < _graph->node_count; n++) {
117 : 83 : graph_node = sorted[n];
118 : 83 : struct rte_node *node = RTE_PTR_ADD(graph, off);
119 : : memset(node, 0, sizeof(*node));
120 : 83 : node->fence = RTE_GRAPH_FENCE;
121 : 83 : node->off = off;
122 : 83 : node->sched_idx = n;
123 [ - + ]: 83 : if (graph_pcap_is_enable()) {
124 : 0 : node->process = graph_pcap_dispatch;
125 : 0 : node->original_process = graph_node->node->process;
126 : : } else
127 : 83 : node->process = graph_node->node->process;
128 [ + + ]: 83 : memcpy(node->name, graph_node->node->name, RTE_GRAPH_NAMESIZE);
129 : 83 : pid = graph_node->node->parent_id;
130 [ + + ]: 83 : if (pid != RTE_NODE_ID_INVALID) { /* Cloned node */
131 : : struct node *pnode;
132 : :
133 [ + - ]: 60 : STAILQ_FOREACH(pnode, node_list_head_get(), next) {
134 [ + + ]: 60 : if (pnode->id == pid) {
135 : 30 : memcpy(node->parent, pnode->name, RTE_GRAPH_NAMESIZE);
136 : : break;
137 : : }
138 : : }
139 : : }
140 : 83 : node->id = graph_node->node->id;
141 : 83 : node->parent_id = pid;
142 : 83 : node->dispatch.lcore_id = graph_node->node->lcore_id;
143 : 83 : nb_edges = graph_node->node->nb_edges;
144 : 83 : node->nb_edges = nb_edges;
145 : 83 : off += sizeof(struct rte_node);
146 : : /* Copy the name in first pass to replace with rte_node* later*/
147 [ + + ]: 240 : for (count = 0; count < nb_edges; count++)
148 : 157 : node->nodes[count] = (struct rte_node *)&graph_node
149 : 157 : ->adjacency_list[count]
150 : 157 : ->node->name[0];
151 : :
152 [ - + ]: 83 : if (graph_node->node->xstats != NULL) {
153 : 0 : node->xstat_off = xstat_off - off;
154 : 0 : xstat_off += sizeof(uint64_t) * graph_node->node->xstats->nb_xstats;
155 : 0 : xstat_off = RTE_ALIGN(xstat_off, RTE_CACHE_LINE_SIZE);
156 : : }
157 : :
158 : 83 : off += sizeof(struct rte_node *) * nb_edges;
159 : 83 : off = RTE_ALIGN(off, RTE_CACHE_LINE_SIZE);
160 : 83 : node->next = off;
161 : :
162 : : /* Fill the schedule table */
163 : 83 : graph->sched_table[n] = node->off;
164 : :
165 : 83 : __rte_node_stream_alloc(graph, node);
166 : : }
167 : :
168 : 13 : free(sorted);
169 : 13 : }
170 : :
171 : : struct rte_node *
172 : 5 : graph_node_id_to_ptr(const struct rte_graph *graph, rte_node_t id)
173 : : {
174 : : rte_node_t count;
175 : : rte_graph_off_t off;
176 : : struct rte_node *node;
177 : :
178 [ + - ]: 15 : rte_graph_foreach_node(count, off, graph, node)
179 [ + + ]: 15 : if (unlikely(node->id == id))
180 : 5 : return node;
181 : :
182 : : return NULL;
183 : : }
184 : :
185 : : struct rte_node *
186 : 254 : graph_node_name_to_ptr(const struct rte_graph *graph, const char *name)
187 : : {
188 : : rte_node_t count;
189 : : rte_graph_off_t off;
190 : : struct rte_node *node;
191 : :
192 [ + - ]: 1187 : rte_graph_foreach_node(count, off, graph, node)
193 [ + + ]: 1187 : if (strncmp(name, node->name, RTE_NODE_NAMESIZE) == 0)
194 : 254 : return node;
195 : :
196 : : return NULL;
197 : : }
198 : :
199 : : static int
200 : 13 : graph_node_nexts_populate(struct graph *_graph)
201 : : {
202 : : rte_node_t count, val;
203 : : rte_graph_off_t off;
204 : : struct rte_node *node;
205 : 13 : const struct rte_graph *graph = _graph->graph;
206 : : const char *name;
207 : :
208 [ + + ]: 96 : rte_graph_foreach_node(count, off, graph, node) {
209 [ + + ]: 240 : for (val = 0; val < node->nb_edges; val++) {
210 : 157 : name = (const char *)node->nodes[val];
211 : 157 : node->nodes[val] = graph_node_name_to_ptr(graph, name);
212 [ - + ]: 157 : if (node->nodes[val] == NULL)
213 : 0 : SET_ERR_JMP(EINVAL, fail, "%s not found", name);
214 : : }
215 : : }
216 : :
217 : : return 0;
218 : : fail:
219 : 0 : return -rte_errno;
220 : : }
221 : :
222 : : static int
223 : 13 : graph_src_bitmap_populate(struct graph *_graph)
224 : : {
225 : 13 : struct rte_graph *graph = _graph->graph;
226 : : struct graph_node *graph_node;
227 : : struct rte_node *node;
228 : : const char *name;
229 : :
230 [ + + ]: 96 : STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
231 [ + + ]: 83 : if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
232 : 13 : name = graph_node->node->name;
233 : 13 : node = graph_node_name_to_ptr(graph, name);
234 [ - + ]: 13 : if (node == NULL)
235 : 0 : SET_ERR_JMP(EINVAL, fail, "%s not found", name);
236 : :
237 : 13 : __rte_node_stream_alloc(graph, node);
238 : 13 : __rte_node_pending_set(graph->src_pending, node);
239 : : }
240 : : }
241 : :
242 : : return 0;
243 : : fail:
244 : 0 : return -rte_errno;
245 : : }
246 : :
247 : : void
248 : 1 : graph_src_bitmap_rebuild(struct graph *_graph)
249 : : {
250 : 1 : struct rte_graph *graph = _graph->graph;
251 : : struct graph_node *graph_node;
252 : : struct rte_node *node;
253 : : const char *name;
254 : :
255 : 1 : memset(graph->src_pending, 0,
256 : 1 : sizeof(uint64_t) * graph->nb_sched_words);
257 : :
258 [ + + ]: 6 : STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
259 [ + + ]: 5 : if (!(graph_node->node->flags & RTE_NODE_SOURCE_F))
260 : 4 : continue;
261 [ - + ]: 1 : if (graph_node->node->lcore_id != RTE_MAX_LCORE &&
262 [ # # ]: 0 : graph_node->node->lcore_id != _graph->lcore_id)
263 : 0 : continue;
264 : 1 : name = graph_node->node->name;
265 : 1 : node = graph_node_name_to_ptr(graph, name);
266 [ - + ]: 1 : if (node == NULL)
267 : 0 : continue;
268 : 1 : __rte_node_pending_set(graph->src_pending, node);
269 : : }
270 : 1 : }
271 : :
272 : : static int
273 : 13 : graph_fp_mem_populate(struct graph *graph)
274 : : {
275 : : int rc;
276 : :
277 : 13 : graph_header_populate(graph);
278 [ - + ]: 13 : if (graph_pcap_is_enable())
279 : 0 : graph_pcap_init(graph);
280 : 13 : graph_nodes_populate(graph);
281 : 13 : rc = graph_node_nexts_populate(graph);
282 : 13 : rc |= graph_src_bitmap_populate(graph);
283 : :
284 : 13 : return rc;
285 : : }
286 : :
287 : : int
288 : 13 : graph_fp_mem_create(struct graph *graph)
289 : : {
290 : : const struct rte_memzone *mz;
291 : : size_t sz;
292 : :
293 : 13 : sz = graph_fp_mem_calc_size(graph);
294 : 13 : mz = rte_memzone_reserve(graph->name, sz, graph->socket, 0);
295 [ - + ]: 13 : if (mz == NULL)
296 : 0 : SET_ERR_JMP(ENOMEM, fail, "Memzone %s reserve failed",
297 : : graph->name);
298 : :
299 : 13 : graph->graph = mz->addr;
300 : 13 : graph->mz = mz;
301 : :
302 : 13 : return graph_fp_mem_populate(graph);
303 : : fail:
304 : 0 : return -rte_errno;
305 : : }
306 : :
307 : : static void
308 : 13 : graph_nodes_mem_destroy(struct rte_graph *graph)
309 : : {
310 : : rte_node_t count;
311 : : rte_graph_off_t off;
312 : : struct rte_node *node;
313 : :
314 [ + - ]: 13 : if (graph == NULL)
315 : : return;
316 : :
317 [ + + ]: 96 : rte_graph_foreach_node(count, off, graph, node)
318 : 83 : rte_free(node->objs);
319 : : }
320 : :
321 : : int
322 : 13 : graph_fp_mem_destroy(struct graph *graph)
323 : : {
324 [ - + ]: 13 : if (graph_pcap_is_enable())
325 : 0 : graph_pcap_exit(graph->graph);
326 : :
327 : 13 : graph_nodes_mem_destroy(graph->graph);
328 : 13 : return rte_memzone_free(graph->mz);
329 : : }
|