DPDK  23.03.0
rte_graph_worker.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4 
5 #ifndef _RTE_GRAPH_WORKER_H_
6 #define _RTE_GRAPH_WORKER_H_
7 
19 #include <rte_compat.h>
20 #include <rte_common.h>
21 #include <rte_cycles.h>
22 #include <rte_prefetch.h>
23 #include <rte_memcpy.h>
24 #include <rte_memory.h>
25 
26 #include "rte_graph.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
37 struct rte_graph {
38  uint32_t tail;
39  uint32_t head;
40  uint32_t cir_mask;
41  rte_node_t nb_nodes;
42  rte_graph_off_t *cir_start;
43  rte_graph_off_t nodes_start;
44  rte_graph_t id;
45  int socket;
46  char name[RTE_GRAPH_NAMESIZE];
47  bool pcap_enable;
49  uint64_t nb_pkt_captured;
51  uint64_t nb_pkt_to_capture;
52  char pcap_filename[RTE_GRAPH_PCAP_FILE_SZ];
53  uint64_t fence;
55 
61 struct rte_node {
62  /* Slow path area */
63  uint64_t fence;
64  rte_graph_off_t next;
65  rte_node_t id;
66  rte_node_t parent_id;
67  rte_edge_t nb_edges;
68  uint32_t realloc_count;
70  char parent[RTE_NODE_NAMESIZE];
71  char name[RTE_NODE_NAMESIZE];
74  rte_node_process_t original_process;
75 
76  /* Fast path area */
77 #define RTE_NODE_CTX_SZ 16
78  uint8_t ctx[RTE_NODE_CTX_SZ] __rte_cache_aligned;
79  uint16_t size;
80  uint16_t idx;
81  rte_graph_off_t off;
82  uint64_t total_cycles;
83  uint64_t total_calls;
84  uint64_t total_objs;
86  union {
87  void **objs;
88  uint64_t objs_u64;
89  };
91  union {
92  rte_node_process_t process;
93  uint64_t process_u64;
94  };
95  struct rte_node *nodes[] __rte_cache_min_aligned;
97 
110 __rte_experimental
111 void __rte_node_stream_alloc(struct rte_graph *graph, struct rte_node *node);
112 
127 __rte_experimental
128 void __rte_node_stream_alloc_size(struct rte_graph *graph,
129  struct rte_node *node, uint16_t req_size);
130 
140 __rte_experimental
141 static inline void
142 rte_graph_walk(struct rte_graph *graph)
143 {
144  const rte_graph_off_t *cir_start = graph->cir_start;
145  const rte_node_t mask = graph->cir_mask;
146  uint32_t head = graph->head;
147  struct rte_node *node;
148  uint64_t start;
149  uint16_t rc;
150  void **objs;
151 
152  /*
153  * Walk on the source node(s) ((cir_start - head) -> cir_start) and then
154  * on the pending streams (cir_start -> (cir_start + mask) -> cir_start)
155  * in a circular buffer fashion.
156  *
157  * +-----+ <= cir_start - head [number of source nodes]
158  * | |
159  * | ... | <= source nodes
160  * | |
161  * +-----+ <= cir_start [head = 0] [tail = 0]
162  * | |
163  * | ... | <= pending streams
164  * | |
165  * +-----+ <= cir_start + mask
166  */
167  while (likely(head != graph->tail)) {
168  node = (struct rte_node *)RTE_PTR_ADD(graph, cir_start[(int32_t)head++]);
169  RTE_ASSERT(node->fence == RTE_GRAPH_FENCE);
170  objs = node->objs;
171  rte_prefetch0(objs);
172 
174  start = rte_rdtsc();
175  rc = node->process(graph, node, objs, node->idx);
176  node->total_cycles += rte_rdtsc() - start;
177  node->total_calls++;
178  node->total_objs += rc;
179  } else {
180  node->process(graph, node, objs, node->idx);
181  }
182  node->idx = 0;
183  head = likely((int32_t)head > 0) ? head & mask : head;
184  }
185  graph->tail = 0;
186 }
187 
188 /* Fast path helper functions */
189 
200 static __rte_always_inline void
201 __rte_node_enqueue_tail_update(struct rte_graph *graph, struct rte_node *node)
202 {
203  uint32_t tail;
204 
205  tail = graph->tail;
206  graph->cir_start[tail++] = node->off;
207  graph->tail = tail & graph->cir_mask;
208 }
209 
227 static __rte_always_inline void
228 __rte_node_enqueue_prologue(struct rte_graph *graph, struct rte_node *node,
229  const uint16_t idx, const uint16_t space)
230 {
231 
232  /* Add to the pending stream list if the node is new */
233  if (idx == 0)
234  __rte_node_enqueue_tail_update(graph, node);
235 
236  if (unlikely(node->size < (idx + space)))
237  __rte_node_stream_alloc_size(graph, node, node->size + space);
238 }
239 
253 static __rte_always_inline struct rte_node *
254 __rte_node_next_node_get(struct rte_node *node, rte_edge_t next)
255 {
256  RTE_ASSERT(next < node->nb_edges);
257  RTE_ASSERT(node->fence == RTE_GRAPH_FENCE);
258  node = node->nodes[next];
259  RTE_ASSERT(node->fence == RTE_GRAPH_FENCE);
260 
261  return node;
262 }
263 
279 __rte_experimental
280 static inline void
281 rte_node_enqueue(struct rte_graph *graph, struct rte_node *node,
282  rte_edge_t next, void **objs, uint16_t nb_objs)
283 {
284  node = __rte_node_next_node_get(node, next);
285  const uint16_t idx = node->idx;
286 
287  __rte_node_enqueue_prologue(graph, node, idx, nb_objs);
288 
289  rte_memcpy(&node->objs[idx], objs, nb_objs * sizeof(void *));
290  node->idx = idx + nb_objs;
291 }
292 
306 __rte_experimental
307 static inline void
308 rte_node_enqueue_x1(struct rte_graph *graph, struct rte_node *node,
309  rte_edge_t next, void *obj)
310 {
311  node = __rte_node_next_node_get(node, next);
312  uint16_t idx = node->idx;
313 
314  __rte_node_enqueue_prologue(graph, node, idx, 1);
315 
316  node->objs[idx++] = obj;
317  node->idx = idx;
318 }
319 
336 __rte_experimental
337 static inline void
338 rte_node_enqueue_x2(struct rte_graph *graph, struct rte_node *node,
339  rte_edge_t next, void *obj0, void *obj1)
340 {
341  node = __rte_node_next_node_get(node, next);
342  uint16_t idx = node->idx;
343 
344  __rte_node_enqueue_prologue(graph, node, idx, 2);
345 
346  node->objs[idx++] = obj0;
347  node->objs[idx++] = obj1;
348  node->idx = idx;
349 }
350 
371 __rte_experimental
372 static inline void
373 rte_node_enqueue_x4(struct rte_graph *graph, struct rte_node *node,
374  rte_edge_t next, void *obj0, void *obj1, void *obj2,
375  void *obj3)
376 {
377  node = __rte_node_next_node_get(node, next);
378  uint16_t idx = node->idx;
379 
380  __rte_node_enqueue_prologue(graph, node, idx, 4);
381 
382  node->objs[idx++] = obj0;
383  node->objs[idx++] = obj1;
384  node->objs[idx++] = obj2;
385  node->objs[idx++] = obj3;
386  node->idx = idx;
387 }
388 
405 __rte_experimental
406 static inline void
407 rte_node_enqueue_next(struct rte_graph *graph, struct rte_node *node,
408  rte_edge_t *nexts, void **objs, uint16_t nb_objs)
409 {
410  uint16_t i;
411 
412  for (i = 0; i < nb_objs; i++)
413  rte_node_enqueue_x1(graph, node, nexts[i], objs[i]);
414 }
415 
435 __rte_experimental
436 static inline void **
437 rte_node_next_stream_get(struct rte_graph *graph, struct rte_node *node,
438  rte_edge_t next, uint16_t nb_objs)
439 {
440  node = __rte_node_next_node_get(node, next);
441  const uint16_t idx = node->idx;
442  uint16_t free_space = node->size - idx;
443 
444  if (unlikely(free_space < nb_objs))
445  __rte_node_stream_alloc_size(graph, node, node->size + nb_objs);
446 
447  return &node->objs[idx];
448 }
449 
466 __rte_experimental
467 static inline void
468 rte_node_next_stream_put(struct rte_graph *graph, struct rte_node *node,
469  rte_edge_t next, uint16_t idx)
470 {
471  if (unlikely(!idx))
472  return;
473 
474  node = __rte_node_next_node_get(node, next);
475  if (node->idx == 0)
476  __rte_node_enqueue_tail_update(graph, node);
477 
478  node->idx += idx;
479 }
480 
495 __rte_experimental
496 static inline void
497 rte_node_next_stream_move(struct rte_graph *graph, struct rte_node *src,
498  rte_edge_t next)
499 {
500  struct rte_node *dst = __rte_node_next_node_get(src, next);
501 
502  /* Let swap the pointers if dst don't have valid objs */
503  if (likely(dst->idx == 0)) {
504  void **dobjs = dst->objs;
505  uint16_t dsz = dst->size;
506  dst->objs = src->objs;
507  dst->size = src->size;
508  src->objs = dobjs;
509  src->size = dsz;
510  dst->idx = src->idx;
511  __rte_node_enqueue_tail_update(graph, dst);
512  } else { /* Move the objects from src node to dst node */
513  rte_node_enqueue(graph, src, next, src->objs, src->idx);
514  }
515 }
516 
517 #ifdef __cplusplus
518 }
519 #endif
520 
521 #endif /* _RTE_GRAPH_WORKER_H_ */
uint32_t rte_node_t
Definition: rte_graph.h:46
static __rte_experimental void rte_node_enqueue(struct rte_graph *graph, struct rte_node *node, rte_edge_t next, void **objs, uint16_t nb_objs)
#define __rte_always_inline
Definition: rte_common.h:255
static __rte_experimental void rte_node_next_stream_move(struct rte_graph *graph, struct rte_node *src, rte_edge_t next)
uint16_t rte_edge_t
Definition: rte_graph.h:47
#define __rte_cache_min_aligned
Definition: rte_common.h:443
#define likely(x)
static __rte_experimental void rte_node_enqueue_x2(struct rte_graph *graph, struct rte_node *node, rte_edge_t next, void *obj0, void *obj1)
#define RTE_NODE_NAMESIZE
Definition: rte_graph.h:37
#define RTE_GRAPH_PCAP_FILE_SZ
Definition: rte_graph.h:38
static __rte_always_inline int rte_graph_has_stats_feature(void)
Definition: rte_graph.h:661
uint16_t rte_graph_t
Definition: rte_graph.h:48
uint16_t(* rte_node_process_t)(struct rte_graph *graph, struct rte_node *node, void **objs, uint16_t nb_objs)
Definition: rte_graph.h:100
#define RTE_GRAPH_NAMESIZE
Definition: rte_graph.h:36
static __rte_experimental void rte_node_next_stream_put(struct rte_graph *graph, struct rte_node *node, rte_edge_t next, uint16_t idx)
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:290
static __rte_experimental void rte_node_enqueue_next(struct rte_graph *graph, struct rte_node *node, rte_edge_t *nexts, void **objs, uint16_t nb_objs)
#define unlikely(x)
static __rte_experimental void rte_graph_walk(struct rte_graph *graph)
#define RTE_GRAPH_FENCE
Definition: rte_graph.h:43
static __rte_experimental void rte_node_enqueue_x1(struct rte_graph *graph, struct rte_node *node, rte_edge_t next, void *obj)
#define __rte_cache_aligned
Definition: rte_common.h:440
#define RTE_STD_C11
Definition: rte_common.h:39
uint32_t rte_graph_off_t
Definition: rte_graph.h:45
static __rte_experimental void ** rte_node_next_stream_get(struct rte_graph *graph, struct rte_node *node, rte_edge_t next, uint16_t nb_objs)
static void * rte_memcpy(void *dst, const void *src, size_t n)
static __rte_experimental void rte_node_enqueue_x4(struct rte_graph *graph, struct rte_node *node, rte_edge_t next, void *obj0, void *obj1, void *obj2, void *obj3)
static void rte_prefetch0(const volatile void *p)