NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
stream_receive_perf_core.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef STREAM_RECEIVE_PERF_CORE_H
27 #define STREAM_RECEIVE_PERF_CORE_H
28 
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdnoreturn.h>
32 #include <unistd.h>
33 
34 #include <doca_buf.h>
35 #include <doca_buf_inventory.h>
36 #include <doca_dev.h>
37 #include <doca_mmap.h>
38 #include <doca_pe.h>
39 #include <doca_rmax.h>
40 
41 #define APP_NAME "doca_stream_receive_perf"
42 #define MAX_BUFFERS 2 /* Maximum number of buffers allowed */
43 
44 /* Scatter type enum for packet processing */
46  SCATTER_TYPE_RAW, /* Access to full raw packets, including the network headers */
47  SCATTER_TYPE_ULP /* Access to the contents of the L4 network layer */
48 };
49 
50 /* Timestamp format for packet processing */
52  TIMESTAMP_FORMAT_RAW_COUNTER, /* Raw counter for timestamps */
53  TIMESTAMP_FORMAT_FREE_RUNNING, /* Free-running timestamp */
54  TIMESTAMP_FORMAT_PTP_SYNCED /* PTP (Precision Time Protocol)-synced timestamp */
55 };
56 
57 /* Application configuration used to customize the application behavior */
58 struct app_config {
59  bool list; /* Whether to list available devices */
60  bool dump; /* Whether to dump packet content */
61  enum scatter_type scatter_type; /* Scatter type for processing packets */
62  enum timestamp_format tstamp_format; /* Timestamp format to apply */
63  struct in_addr src_ip; /* Source IP address to read from */
64  struct in_addr dst_ip; /* Destination IP address to bind to */
65  struct in_addr dev_ip; /* IP address of the device to use */
66  uint16_t dst_port; /* Destination port to bind to */
67  uint16_t data_size; /* Data (payload) size of the packet */
68  uint16_t hdr_size; /* Header size of the packet */
69  uint32_t num_elements; /* Number of elements in the stream buffer */
70  bool affinity_mask_set; /* Whether a CPU affinity mask is set */
71  struct doca_rmax_cpu_affinity *affinity_mask; /* CPU affinity mask */
72  useconds_t sleep_us; /* Sleep duration between packet processing steps (in microseconds) */
73  uint32_t min_packets; /* Minimum number of packets to process in a single step */
74  uint32_t max_packets; /* Maximum number of packets to process in a single step */
75 };
76 
77 /* Global resources required by the application */
78 struct globals {
79  struct doca_mmap *mmap; /* Memory map for application resources */
80  struct doca_buf_inventory *inventory; /* Buffer inventory for managing memory buffers */
81  struct doca_pe *pe; /* Progress engine for processing tasks */
82 };
83 
84 /* Stream data structure for managing the state and data associated with streaming */
85 struct stream_data {
86  size_t num_buffers; /* Number of buffers used*/
87  struct timespec start; /* Start time of the streaming process */
88  struct doca_rmax_in_stream *stream; /* Stream object for receiving data */
89  struct doca_buf *buffer; /* Memory buffer linked to the stream */
90  struct doca_rmax_flow *flow; /* Flow object attached to the stream */
91  uint16_t pkt_size[MAX_BUFFERS]; /* Size of an element in each buffers */
92  uint16_t stride_size[MAX_BUFFERS]; /* Stride size for packet data in the buffers */
93  /* statistics */
94  size_t recv_pkts; /* Number of packets received */
95  size_t recv_bytes; /* Total number of bytes received */
96  /* control flow */
97  bool dump; /* Whether to dump the content of received packets */
98  bool run_recv_loop; /* Flag to indicate whether the receive loop should continue running */
99 };
100 
101 /*
102  * Initializes the application configuration with default values.
103  * Also creates the CPU affinity mask used for assigning tasks to specific cores
104  *
105  * @config [in/out]: Pointer to the application configuration structure to initialize
106  * @return: true on successful initialization; false otherwise
107  */
108 bool init_config(struct app_config *config);
109 
110 /*
111  * Releases resources allocated by the application configuration.
112  * This includes destroying the CPU affinity mask to clean up system resources
113  *
114  * @config [in]: Pointer to the application configuration structure to destroy
115  */
116 void destroy_config(struct app_config *config);
117 
118 /*
119  * Registers all configurable parameters for handling command-line arguments.
120  * These parameters help customize the application's behavior
121  *
122  * @return: true on successful registration of all parameters; false otherwise
123  */
124 bool register_argp_params(void);
125 
126 /*
127  * Verifies that all mandatory arguments (e.g., IP addresses and port) are set
128  * in the application configuration before proceeding
129  *
130  * @config [in]: Pointer to the application configuration structure to validate
131  * @return: true if all mandatory arguments are set; false otherwise
132  */
133 bool mandatory_args_set(struct app_config *config);
134 
135 /*
136  * Enumerates and lists all available devices that the application
137  * can interact with, along with their details like network interface name, PCI address,
138  * and IP address
139  */
140 void list_devices(void);
141 
142 /*
143  * Opens a specific device based on its IP address. Uses the DOCA library
144  * to locate the device, open it, and return a device handle
145  *
146  * @dev_ip [in]: Pointer to the IP address of the device to open
147  * @return: Pointer to the opened device on success; NULL otherwise
148  */
149 struct doca_dev *open_device(struct in_addr *dev_ip);
150 
151 /*
152  * Initializes global application resources like memory maps, progress engines,
153  * and buffer inventories. These resources are essential for handling stream operations
154  *
155  * @config [in]: Pointer to the application configuration structure
156  * @dev [in]: Pointer to the device handle opened by the application
157  * @globals [in/out]: Pointer to the global resources structure to initialize
158  * @return: DOCA_SUCCESS on successful initialization; appropriate DOCA error otherwise
159  */
160 doca_error_t init_globals(struct app_config *config, struct doca_dev *dev, struct globals *globals);
161 
162 /*
163  * Releases and destroys global application resources initialized earlier. This
164  * includes stopping memory maps, destroying buffer inventories, and releasing progress engines
165  *
166  * @globals [in]: Pointer to the global resources structure to destroy
167  * @dev [in]: Pointer to the device handle associated with these resources
168  * @return: true if resources were successfully destroyed; false otherwise
169  */
170 bool destroy_globals(struct globals *globals, struct doca_dev *dev);
171 
172 /*
173  * Configures and initializes a stream for receiving packets.
174  * Sets stream parameters, links to memory buffers, and connects to a flow for data reception
175  *
176  * @config [in]: Pointer to the application configuration structure
177  * @dev [in]: Pointer to the device handle opened by the application
178  * @globals [in]: Pointer to the global resources structure
179  * @data [in/out]: Pointer to the stream data structure to initialize
180  * @return: DOCA_SUCCESS on successful initialization; appropriate DOCA error otherwise
181  */
182 doca_error_t init_stream(struct app_config *config,
183  struct doca_dev *dev,
184  struct globals *globals,
185  struct stream_data *data);
186 
187 /*
188  * Cleans up and destroys a stream, including flow detachment, stopping the context,
189  * buffer release, and stream destruction
190  *
191  * @dev [in]: Pointer to the device handle associated with the stream
192  * @globals [in]: Pointer to the global resources structure
193  * @data [in/out]: Pointer to the stream data structure to destroy
194  * @return: true if all resources are successfully destroyed; false otherwise
195  */
196 bool destroy_stream(struct doca_dev *dev, struct globals *globals, struct stream_data *data);
197 
198 /*
199  * Runs the packet reception loop. Progresses the DOCA progress engine,
200  * collects statistics periodically, and handles sleep intervals if specified.
201  * The loop runs until the `run_recv_loop` flag in the stream_data structure is set to false
202  *
203  * @config [in]: Pointer to the application configuration structure, defining loop behavior such as sleep intervals
204  * @globals [in]: Pointer to the global resources structure, including progress engine
205  * @data [in/out]: Pointer to the stream data structure, managing control flow and maintaining statistics
206  * @return: true if the loop runs and exits successfully; false otherwise
207  */
208 bool run_recv_loop(const struct app_config *config, struct globals *globals, struct stream_data *data);
209 
210 #endif // STREAM_RECEIVE_PERF_CORE_H
enum doca_error doca_error_t
DOCA API return codes.
bool destroy_stream(struct doca_dev *dev, struct globals *globals, struct stream_data *data)
@ SCATTER_TYPE_RAW
@ SCATTER_TYPE_ULP
bool register_argp_params(void)
doca_error_t init_globals(struct app_config *config, struct doca_dev *dev, struct globals *globals)
bool run_recv_loop(const struct app_config *config, struct globals *globals, struct stream_data *data)
struct doca_dev * open_device(struct in_addr *dev_ip)
#define MAX_BUFFERS
bool destroy_globals(struct globals *globals, struct doca_dev *dev)
doca_error_t init_stream(struct app_config *config, struct doca_dev *dev, struct globals *globals, struct stream_data *data)
bool mandatory_args_set(struct app_config *config)
@ TIMESTAMP_FORMAT_PTP_SYNCED
@ TIMESTAMP_FORMAT_RAW_COUNTER
@ TIMESTAMP_FORMAT_FREE_RUNNING
void list_devices(void)
bool init_config(struct app_config *config)
void destroy_config(struct app_config *config)
struct in_addr dev_ip
struct in_addr dst_ip
enum timestamp_format tstamp_format
struct doca_rmax_cpu_affinity * affinity_mask
struct in_addr src_ip
enum scatter_type scatter_type
struct doca_buf_inventory * inventory
struct doca_pe * pe
struct doca_mmap * mmap
uint16_t stride_size[MAX_BUFFERS]
uint16_t pkt_size[MAX_BUFFERS]
struct timespec start
struct doca_buf * buffer
struct doca_rmax_flow * flow
struct doca_rmax_in_stream * stream