Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2020 Marvell International Ltd.
3 : : */
4 : :
5 : : #ifndef _RTE_GRAPH_PRIVATE_H_
6 : : #define _RTE_GRAPH_PRIVATE_H_
7 : :
8 : : #include <inttypes.h>
9 : : #include <sys/queue.h>
10 : :
11 : : #include <rte_common.h>
12 : : #include <rte_eal.h>
13 : : #include <rte_spinlock.h>
14 : : #include <rte_errno.h>
15 : : #include <rte_string_fns.h>
16 : :
17 : : #include "rte_graph.h"
18 : : #include "rte_graph_worker.h"
19 : :
20 : : extern int rte_graph_logtype;
21 : : #define RTE_LOGTYPE_GRAPH rte_graph_logtype
22 : :
23 : : #define GRAPH_LOG(level, ...) \
24 : : RTE_LOG_LINE_PREFIX(level, GRAPH, \
25 : : "%s():%u ", __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
26 : :
27 : : #define GRAPH_LOG2(level, _fname, _linenum, ...) \
28 : : RTE_LOG_LINE_PREFIX(level, GRAPH, \
29 : : "%s():%u ", _fname RTE_LOG_COMMA _linenum, __VA_ARGS__)
30 : :
31 : : #define graph_err(...) GRAPH_LOG(ERR, __VA_ARGS__)
32 : : #define graph_warn(...) GRAPH_LOG(WARNING, __VA_ARGS__)
33 : : #define graph_info(...) GRAPH_LOG(INFO, __VA_ARGS__)
34 : : #define graph_dbg(...) GRAPH_LOG(DEBUG, __VA_ARGS__)
35 : :
36 : : #define SET_ERR_JMP(err, where, fmt, ...) \
37 : : do { \
38 : : graph_err(fmt, ##__VA_ARGS__); \
39 : : rte_errno = err; \
40 : : goto where; \
41 : : } while (0)
42 : :
43 : : /**
44 : : * @internal
45 : : *
46 : : * Structure that holds node internal data.
47 : : */
48 : : struct node {
49 : : STAILQ_ENTRY(node) next; /**< Next node in the list. */
50 : : char name[RTE_NODE_NAMESIZE]; /**< Name of the node. */
51 : : uint64_t flags; /**< Node configuration flag. */
52 : : unsigned int lcore_id;
53 : : /**< Node runs on the Lcore ID used for mcore dispatch model. */
54 : : rte_node_process_t process; /**< Node process function. */
55 : : rte_node_init_t init; /**< Node init function. */
56 : : rte_node_fini_t fini; /**< Node fini function. */
57 : : rte_node_t id; /**< Allocated identifier for the node. */
58 : : rte_node_t parent_id; /**< Parent node identifier. */
59 : : rte_edge_t nb_edges; /**< Number of edges from this node. */
60 : : struct rte_node_xstats *xstats; /**< Node specific xstats. */
61 : : char next_nodes[][RTE_NODE_NAMESIZE]; /**< Names of next nodes. */
62 : : };
63 : :
64 : : /**
65 : : * @internal
66 : : *
67 : : * Structure that holds the graph scheduling workqueue node stream.
68 : : * Used for mcore dispatch model.
69 : : */
70 : : struct __rte_cache_aligned graph_mcore_dispatch_wq_node {
71 : : rte_graph_off_t node_off;
72 : : uint16_t nb_objs;
73 : : void *objs[RTE_GRAPH_BURST_SIZE];
74 : : };
75 : :
76 : : /**
77 : : * @internal
78 : : *
79 : : * Structure that holds the graph node data.
80 : : */
81 : : struct graph_node {
82 : : STAILQ_ENTRY(graph_node) next; /**< Next graph node in the list. */
83 : : struct node *node; /**< Pointer to internal node. */
84 : : bool visited; /**< Flag used in BFS to mark node visited. */
85 : : struct graph_node *adjacency_list[]; /**< Adjacency list of the node. */
86 : : };
87 : :
88 : : /**
89 : : * @internal
90 : : *
91 : : * Structure that holds graph internal data.
92 : : */
93 : : struct graph {
94 : : STAILQ_ENTRY(graph) next;
95 : : /**< List of graphs. */
96 : : char name[RTE_GRAPH_NAMESIZE];
97 : : /**< Name of the graph. */
98 : : const struct rte_memzone *mz;
99 : : /**< Memzone to store graph data. */
100 : : rte_graph_off_t nodes_start;
101 : : /**< Node memory start offset in graph reel. */
102 : : rte_graph_off_t xstats_start;
103 : : /**< Node xstats memory start offset in graph reel. */
104 : : rte_node_t src_node_count;
105 : : /**< Number of source nodes in a graph. */
106 : : struct rte_graph *graph;
107 : : /**< Pointer to graph data. */
108 : : rte_node_t node_count;
109 : : /**< Total number of nodes. */
110 : : uint32_t cir_start;
111 : : /**< Circular buffer start offset in graph reel. */
112 : : uint32_t cir_mask;
113 : : /**< Circular buffer mask for wrap around. */
114 : : rte_graph_t id;
115 : : /**< Graph identifier. */
116 : : rte_graph_t parent_id;
117 : : /**< Parent graph identifier. */
118 : : unsigned int lcore_id;
119 : : /**< Lcore identifier where the graph prefer to run on. Used for mcore dispatch model. */
120 : : size_t mem_sz;
121 : : /**< Memory size of the graph. */
122 : : int socket;
123 : : /**< Socket identifier where memory is allocated. */
124 : : uint64_t num_pkt_to_capture;
125 : : /**< Number of packets to be captured per core. */
126 : : char pcap_filename[RTE_GRAPH_PCAP_FILE_SZ];
127 : : /**< pcap file name/path. */
128 : : STAILQ_HEAD(gnode_list, graph_node) node_list;
129 : : /**< Nodes in a graph. */
130 : : };
131 : :
132 : : /* Node and graph common functions */
133 : : /**
134 : : * @internal
135 : : *
136 : : * Naming a cloned graph or node by appending a string to base name.
137 : : *
138 : : * @param new_name
139 : : * Pointer to the name of the cloned object.
140 : : * @param base_name
141 : : * Pointer to the name of original object.
142 : : * @param append_str
143 : : * Pointer to the appended string.
144 : : *
145 : : * @return
146 : : * 0 on success, negative errno value otherwise.
147 : : */
148 : 14 : static inline int clone_name(char *new_name, char *base_name, const char *append_str)
149 : : {
150 : : ssize_t sz, rc;
151 : :
152 : : #define SZ RTE_MIN(RTE_NODE_NAMESIZE, RTE_GRAPH_NAMESIZE)
153 : 14 : rc = rte_strscpy(new_name, base_name, SZ);
154 [ - + ]: 14 : if (rc < 0)
155 : 0 : goto fail;
156 : : sz = rc;
157 : 14 : rc = rte_strscpy(new_name + sz, "-", RTE_MAX((int16_t)(SZ - sz), 0));
158 [ - + ]: 14 : if (rc < 0)
159 : 0 : goto fail;
160 : 14 : sz += rc;
161 : 14 : sz = rte_strscpy(new_name + sz, append_str, RTE_MAX((int16_t)(SZ - sz), 0));
162 [ - + ]: 14 : if (sz < 0)
163 : 0 : goto fail;
164 : :
165 : : return 0;
166 : 0 : fail:
167 : 0 : rte_errno = E2BIG;
168 : 0 : return -rte_errno;
169 : : }
170 : :
171 : : /* Node functions */
172 : : STAILQ_HEAD(node_head, node);
173 : :
174 : : /**
175 : : * @internal
176 : : *
177 : : * Get the head of the node list.
178 : : *
179 : : * @return
180 : : * Pointer to the node head.
181 : : */
182 : : struct node_head *node_list_head_get(void);
183 : :
184 : : /**
185 : : * @internal
186 : : *
187 : : * Get node pointer from node name.
188 : : *
189 : : * @param name
190 : : * Pointer to character string containing the node name.
191 : : *
192 : : * @return
193 : : * Pointer to the node.
194 : : */
195 : : struct node *node_from_name(const char *name);
196 : :
197 : : /**
198 : : * @internal
199 : : *
200 : : * Override process func of a node.
201 : : *
202 : : * @return
203 : : * - 0: Success.
204 : : * - <0: Error
205 : : */
206 : : int node_override_process_func(rte_node_t id, rte_node_process_t process);
207 : :
208 : : /* Graph list functions */
209 : : STAILQ_HEAD(graph_head, graph);
210 : :
211 : : /**
212 : : * @internal
213 : : *
214 : : * Get the head of the graph list.
215 : : *
216 : : * @return
217 : : * Pointer to the graph head.
218 : : */
219 : : struct graph_head *graph_list_head_get(void);
220 : :
221 : : rte_spinlock_t *
222 : : graph_spinlock_get(void);
223 : :
224 : : /* Lock functions */
225 : : /**
226 : : * @internal
227 : : *
228 : : * Take a lock on the graph internal spin lock.
229 : : */
230 : : void graph_spinlock_lock(void)
231 : : __rte_acquire_capability(graph_spinlock_get());
232 : :
233 : : /**
234 : : * @internal
235 : : *
236 : : * Release a lock on the graph internal spin lock.
237 : : */
238 : : void graph_spinlock_unlock(void)
239 : : __rte_release_capability(graph_spinlock_get());
240 : :
241 : : /* Graph operations */
242 : : /**
243 : : * @internal
244 : : *
245 : : * Run a BFS(Breadth First Search) on the graph marking all
246 : : * the graph nodes as visited.
247 : : *
248 : : * @param graph
249 : : * Pointer to the internal graph object.
250 : : * @param start
251 : : * Pointer to the starting graph node.
252 : : *
253 : : * @return
254 : : * - 0: Success.
255 : : * - -ENOMEM: Not enough memory for BFS.
256 : : */
257 : : int graph_bfs(struct graph *graph, struct graph_node *start);
258 : :
259 : : /**
260 : : * @internal
261 : : *
262 : : * Check if there is an isolated node in the given graph.
263 : : *
264 : : * @param graph
265 : : * Pointer to the internal graph object.
266 : : *
267 : : * @return
268 : : * - 0: No isolated node found.
269 : : * - 1: Isolated node found.
270 : : */
271 : : int graph_has_isolated_node(struct graph *graph);
272 : :
273 : : /**
274 : : * @internal
275 : : *
276 : : * Check whether a node in the graph has next_node to a source node.
277 : : *
278 : : * @param graph
279 : : * Pointer to the internal graph object.
280 : : *
281 : : * @return
282 : : * - 0: Node has an edge to source node.
283 : : * - 1: Node doesn't have an edge to source node.
284 : : */
285 : : int graph_node_has_edge_to_src_node(struct graph *graph);
286 : :
287 : : /**
288 : : * @internal
289 : : *
290 : : * Checks whether node in the graph has a edge to itself i.e. forms a
291 : : * loop.
292 : : *
293 : : * @param graph
294 : : * Pointer to the internal graph object.
295 : : *
296 : : * @return
297 : : * - 0: Node has an edge to itself.
298 : : * - 1: Node doesn't have an edge to itself.
299 : : */
300 : : int graph_node_has_loop_edge(struct graph *graph);
301 : :
302 : : /**
303 : : * @internal
304 : : *
305 : : * Get the count of source nodes in the graph.
306 : : *
307 : : * @param graph
308 : : * Pointer to the internal graph object.
309 : : *
310 : : * @return
311 : : * Number of source nodes.
312 : : */
313 : : rte_node_t graph_src_nodes_count(struct graph *graph);
314 : :
315 : : /**
316 : : * @internal
317 : : *
318 : : * Get the count of total number of nodes in the graph.
319 : : *
320 : : * @param graph
321 : : * Pointer to the internal graph object.
322 : : *
323 : : * @return
324 : : * Number of nodes.
325 : : */
326 : : rte_node_t graph_nodes_count(struct graph *graph);
327 : :
328 : : /**
329 : : * @internal
330 : : *
331 : : * Clear the visited flag of all the nodes in the graph.
332 : : *
333 : : * @param graph
334 : : * Pointer to the internal graph object.
335 : : */
336 : : void graph_mark_nodes_as_not_visited(struct graph *graph);
337 : :
338 : : /* Fast path graph memory populate unctions */
339 : :
340 : : /**
341 : : * @internal
342 : : *
343 : : * Create fast-path memory for the graph and nodes.
344 : : *
345 : : * @param graph
346 : : * Pointer to the internal graph object.
347 : : *
348 : : * @return
349 : : * - 0: Success.
350 : : * - -ENOMEM: Not enough for graph and nodes.
351 : : * - -EINVAL: Graph nodes not found.
352 : : */
353 : : int graph_fp_mem_create(struct graph *graph);
354 : :
355 : : /**
356 : : * @internal
357 : : *
358 : : * Free fast-path memory used by graph and nodes.
359 : : *
360 : : * @param graph
361 : : * Pointer to the internal graph object.
362 : : *
363 : : * @return
364 : : * - 0: Success.
365 : : * - <0: Graph memzone related error.
366 : : */
367 : : int graph_fp_mem_destroy(struct graph *graph);
368 : :
369 : : /* Lookup functions */
370 : : /**
371 : : * @internal
372 : : *
373 : : * Get graph node object from node id.
374 : : *
375 : : * @param graph
376 : : * Pointer to rte_graph object.
377 : : * @param id
378 : : * Node Identifier.
379 : : *
380 : : * @return
381 : : * Pointer to rte_node if identifier is valid else NULL.
382 : : */
383 : : struct rte_node *graph_node_id_to_ptr(const struct rte_graph *graph,
384 : : rte_node_t id);
385 : :
386 : : /**
387 : : * @internal
388 : : *
389 : : * Get graph node object from node name.
390 : : *
391 : : * @param graph
392 : : * Pointer to rte_graph object.
393 : : * @param node_name
394 : : * Pointer to character string holding the node name.
395 : : *
396 : : * @return
397 : : * Pointer to rte_node if identifier is valid else NULL.
398 : : */
399 : : struct rte_node *graph_node_name_to_ptr(const struct rte_graph *graph,
400 : : const char *node_name);
401 : :
402 : : /* Debug functions */
403 : : /**
404 : : * @internal
405 : : *
406 : : * Dump internal graph object data.
407 : : *
408 : : * @param f
409 : : * FILE pointer to dump the data.
410 : : * @param g
411 : : * Pointer to the internal graph object.
412 : : */
413 : : void graph_dump(FILE *f, struct graph *g);
414 : :
415 : : /**
416 : : * @internal
417 : : *
418 : : * Dump internal node object data.
419 : : *
420 : : * @param f
421 : : * FILE pointer to dump the info.
422 : : * @param g
423 : : * Pointer to the internal node object.
424 : : */
425 : : void node_dump(FILE *f, struct node *n);
426 : :
427 : : /**
428 : : * @internal
429 : : *
430 : : * Create the graph schedule work queue for mcore dispatch model.
431 : : * All cloned graphs attached to the parent graph MUST be destroyed together
432 : : * for fast schedule design limitation.
433 : : *
434 : : * @param _graph
435 : : * The graph object
436 : : * @param _parent_graph
437 : : * The parent graph object which holds the run-queue head.
438 : : * @param prm
439 : : * Graph parameter, includes model-specific parameters in this graph.
440 : : *
441 : : * @return
442 : : * - 0: Success.
443 : : * - <0: Graph schedule work queue related error.
444 : : */
445 : : int graph_sched_wq_create(struct graph *_graph, struct graph *_parent_graph,
446 : : struct rte_graph_param *prm);
447 : :
448 : : /**
449 : : * @internal
450 : : *
451 : : * Destroy the graph schedule work queue for mcore dispatch model.
452 : : *
453 : : * @param _graph
454 : : * The graph object
455 : : */
456 : : void graph_sched_wq_destroy(struct graph *_graph);
457 : :
458 : : /**
459 : : * @internal
460 : : *
461 : : * Check if given node present in any of the created graphs.
462 : : *
463 : : * @param node
464 : : * The node object
465 : : *
466 : : * @return
467 : : * 0 if not present in any graph, else return 1.
468 : : */
469 : : bool graph_is_node_active_in_graph(struct node *_node);
470 : :
471 : : #endif /* _RTE_GRAPH_PRIVATE_H_ */
|