summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/lib/librte_graph/graph_populate.c
blob: 093512efabae2d176a3c7a5e90e6ae4df19956dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2020 Marvell International Ltd.
 */

#include <fnmatch.h>
#include <stdbool.h>

#include <rte_common.h>
#include <rte_errno.h>
#include <rte_malloc.h>
#include <rte_memzone.h>

#include "graph_private.h"

static size_t
graph_fp_mem_calc_size(struct graph *graph)
{
	struct graph_node *graph_node;
	rte_node_t val;
	size_t sz;

	/* Graph header */
	sz = sizeof(struct rte_graph);
	/* Source nodes list */
	sz += sizeof(rte_graph_off_t) * graph->src_node_count;
	/* Circular buffer for pending streams of size number of nodes */
	val = rte_align32pow2(graph->node_count * sizeof(rte_graph_off_t));
	sz = RTE_ALIGN(sz, val);
	graph->cir_start = sz;
	graph->cir_mask = rte_align32pow2(graph->node_count) - 1;
	sz += val;
	/* Fence */
	sz += sizeof(RTE_GRAPH_FENCE);
	sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
	graph->nodes_start = sz;
	/* For 0..N node objects with fence */
	STAILQ_FOREACH(graph_node, &graph->node_list, next) {
		sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
		sz += sizeof(struct rte_node);
		/* Pointer to next nodes(edges) */
		sz += sizeof(struct rte_node *) * graph_node->node->nb_edges;
	}

	graph->mem_sz = sz;
	return sz;
}

static void
graph_header_popluate(struct graph *_graph)
{
	struct rte_graph *graph = _graph->graph;

	graph->tail = 0;
	graph->head = (int32_t)-_graph->src_node_count;
	graph->cir_mask = _graph->cir_mask;
	graph->nb_nodes = _graph->node_count;
	graph->cir_start = RTE_PTR_ADD(graph, _graph->cir_start);
	graph->nodes_start = _graph->nodes_start;
	graph->socket = _graph->socket;
	graph->id = _graph->id;
	memcpy(graph->name, _graph->name, RTE_GRAPH_NAMESIZE);
	graph->fence = RTE_GRAPH_FENCE;
}

static void
graph_nodes_populate(struct graph *_graph)
{
	rte_graph_off_t off = _graph->nodes_start;
	struct rte_graph *graph = _graph->graph;
	struct graph_node *graph_node;
	rte_edge_t count, nb_edges;
	const char *parent;
	rte_node_t pid;

	STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
		struct rte_node *node = RTE_PTR_ADD(graph, off);
		memset(node, 0, sizeof(*node));
		node->fence = RTE_GRAPH_FENCE;
		node->off = off;
		node->process = graph_node->node->process;
		memcpy(node->name, graph_node->node->name, RTE_GRAPH_NAMESIZE);
		pid = graph_node->node->parent_id;
		if (pid != RTE_NODE_ID_INVALID) { /* Cloned node */
			parent = rte_node_id_to_name(pid);
			memcpy(node->parent, parent, RTE_GRAPH_NAMESIZE);
		}
		node->id = graph_node->node->id;
		node->parent_id = pid;
		nb_edges = graph_node->node->nb_edges;
		node->nb_edges = nb_edges;
		off += sizeof(struct rte_node);
		/* Copy the name in first pass to replace with rte_node* later*/
		for (count = 0; count < nb_edges; count++)
			node->nodes[count] = (struct rte_node *)&graph_node
						     ->adjacency_list[count]
						     ->node->name[0];

		off += sizeof(struct rte_node *) * nb_edges;
		off = RTE_ALIGN(off, RTE_CACHE_LINE_SIZE);
		node->next = off;
		__rte_node_stream_alloc(graph, node);
	}
}

struct rte_node *
graph_node_id_to_ptr(const struct rte_graph *graph, rte_node_t id)
{
	rte_node_t count;
	rte_graph_off_t off;
	struct rte_node *node;

	rte_graph_foreach_node(count, off, graph, node)
		if (unlikely(node->id == id))
			return node;

	return NULL;
}

struct rte_node *
graph_node_name_to_ptr(const struct rte_graph *graph, const char *name)
{
	rte_node_t count;
	rte_graph_off_t off;
	struct rte_node *node;

	rte_graph_foreach_node(count, off, graph, node)
		if (strncmp(name, node->name, RTE_NODE_NAMESIZE) == 0)
			return node;

	return NULL;
}

static int
graph_node_nexts_populate(struct graph *_graph)
{
	rte_node_t count, val;
	rte_graph_off_t off;
	struct rte_node *node;
	const struct rte_graph *graph = _graph->graph;
	const char *name;

	rte_graph_foreach_node(count, off, graph, node) {
		for (val = 0; val < node->nb_edges; val++) {
			name = (const char *)node->nodes[val];
			node->nodes[val] = graph_node_name_to_ptr(graph, name);
			if (node->nodes[val] == NULL)
				SET_ERR_JMP(EINVAL, fail, "%s not found", name);
		}
	}

	return 0;
fail:
	return -rte_errno;
}

static int
graph_src_nodes_populate(struct graph *_graph)
{
	struct rte_graph *graph = _graph->graph;
	struct graph_node *graph_node;
	struct rte_node *node;
	int32_t head = -1;
	const char *name;

	STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
		if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
			name = graph_node->node->name;
			node = graph_node_name_to_ptr(graph, name);
			if (node == NULL)
				SET_ERR_JMP(EINVAL, fail, "%s not found", name);

			__rte_node_stream_alloc(graph, node);
			graph->cir_start[head--] = node->off;
		}
	}

	return 0;
fail:
	return -rte_errno;
}

static int
graph_fp_mem_populate(struct graph *graph)
{
	int rc;

	graph_header_popluate(graph);
	graph_nodes_populate(graph);
	rc = graph_node_nexts_populate(graph);
	rc |= graph_src_nodes_populate(graph);

	return rc;
}

int
graph_fp_mem_create(struct graph *graph)
{
	const struct rte_memzone *mz;
	size_t sz;

	sz = graph_fp_mem_calc_size(graph);
	mz = rte_memzone_reserve(graph->name, sz, graph->socket, 0);
	if (mz == NULL)
		SET_ERR_JMP(ENOMEM, fail, "Memzone %s reserve failed",
			    graph->name);

	graph->graph = mz->addr;
	graph->mz = mz;

	return graph_fp_mem_populate(graph);
fail:
	return -rte_errno;
}

static void
graph_nodes_mem_destroy(struct rte_graph *graph)
{
	rte_node_t count;
	rte_graph_off_t off;
	struct rte_node *node;

	if (graph == NULL)
		return;

	rte_graph_foreach_node(count, off, graph, node)
		rte_free(node->objs);
}

int
graph_fp_mem_destroy(struct graph *graph)
{
	graph_nodes_mem_destroy(graph->graph);
	return rte_memzone_free(graph->mz);
}