NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
eth_rxq_batch_managed_mempool_receive_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 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 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <string.h>
30 
31 #include <doca_argp.h>
32 #include <doca_dev.h>
33 #include <doca_error.h>
34 #include <doca_log.h>
35 
36 #include "eth_common.h"
37 
38 DOCA_LOG_REGISTER(ETH_RXQ_BATCH_MANAGED_MEMPOOL_RECEIVE::MAIN);
39 
40 /* Configuration struct */
41 struct eth_rxq_cfg {
42  char ib_dev_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]; /* DOCA IB device name */
43  bool timestamp_enable; /* timestamp enable */
44  uint16_t headroom_size; /* headroom size */
45  uint16_t tailroom_size; /* tailroom size */
46 };
47 
48 /* Sample's Logic */
50  bool timestamp_enable,
51  uint16_t headroom_size,
52  uint16_t tailroom_size);
53 
54 /*
55  * ARGP Callback - Handle IB device name parameter
56  *
57  * @param [in]: Input parameter
58  * @config [out]: Program configuration context
59  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
60  */
61 static doca_error_t device_address_callback(void *param, void *config)
62 {
63  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
64 
65  return extract_ibdev_name((char *)param, eth_rxq_cfg->ib_dev_name);
66 }
67 
68 /*
69  * ARGP Callback - timestamp enable parameter
70  *
71  * @param [in]: Input parameter
72  * @config [out]: Program configuration context
73  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
74  */
75 static doca_error_t timestamp_callback(void *param, void *config)
76 {
77  (void)param;
78  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
79 
81 
82  return DOCA_SUCCESS;
83 }
84 
85 /*
86  * ARGP Callback - headroom size parameter
87  *
88  * @param [in]: Input parameter
89  * @config [out]: Program configuration context
90  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
91  */
92 static doca_error_t headroom_callback(void *param, void *config)
93 {
94  uint16_t headroom_size = strtoul((char *)param, NULL, 10);
95  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
96 
98 
99  return DOCA_SUCCESS;
100 }
101 
102 /*
103  * ARGP Callback - tailroom size parameter
104  *
105  * @param [in]: Input parameter
106  * @config [out]: Program configuration context
107  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
108  */
109 static doca_error_t tailroom_callback(void *param, void *config)
110 {
111  uint16_t tailroom_size = strtoul((char *)param, NULL, 10);
112  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
113 
115 
116  return DOCA_SUCCESS;
117 }
118 
119 /*
120  * Register the command line parameters for the sample.
121  *
122  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
123  */
125 {
127  struct doca_argp_param *dev_ib_name_param;
128  struct doca_argp_param *enable_timestamp_param;
129  struct doca_argp_param *headroom_param;
130  struct doca_argp_param *tailroom_param;
131 
132  result = doca_argp_param_create(&dev_ib_name_param);
133  if (result != DOCA_SUCCESS) {
134  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
135  return result;
136  }
137 
138  doca_argp_param_set_short_name(dev_ib_name_param, "d");
139  doca_argp_param_set_long_name(dev_ib_name_param, "device");
140  doca_argp_param_set_description(dev_ib_name_param, "IB device name - default: mlx5_0");
143  result = doca_argp_register_param(dev_ib_name_param);
144  if (result != DOCA_SUCCESS) {
145  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
146  return result;
147  }
148 
149  result = doca_argp_param_create(&enable_timestamp_param);
150  if (result != DOCA_SUCCESS) {
151  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
152  return result;
153  }
154 
155  doca_argp_param_set_short_name(enable_timestamp_param, "ts");
156  doca_argp_param_set_long_name(enable_timestamp_param, "timestamp");
157  doca_argp_param_set_description(enable_timestamp_param, "Enable timestamp retrieval - default: disabled");
158  doca_argp_param_set_callback(enable_timestamp_param, timestamp_callback);
159  doca_argp_param_set_type(enable_timestamp_param, DOCA_ARGP_TYPE_BOOLEAN);
160  result = doca_argp_register_param(enable_timestamp_param);
161  if (result != DOCA_SUCCESS) {
162  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
163  return result;
164  }
165 
166  result = doca_argp_param_create(&headroom_param);
167  if (result != DOCA_SUCCESS) {
168  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
169  return result;
170  }
171 
172  doca_argp_param_set_short_name(headroom_param, "hr");
173  doca_argp_param_set_long_name(headroom_param, "headroom");
174  doca_argp_param_set_description(headroom_param, "Packet headroom size - default: 0");
177  result = doca_argp_register_param(headroom_param);
178  if (result != DOCA_SUCCESS) {
179  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
180  return result;
181  }
182 
183  result = doca_argp_param_create(&tailroom_param);
184  if (result != DOCA_SUCCESS) {
185  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
186  return result;
187  }
188 
189  doca_argp_param_set_short_name(tailroom_param, "tr");
190  doca_argp_param_set_long_name(tailroom_param, "tailroom");
191  doca_argp_param_set_description(tailroom_param, "Packet tailroom size - default: 0");
194  result = doca_argp_register_param(tailroom_param);
195  if (result != DOCA_SUCCESS) {
196  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
197  return result;
198  }
199 
200  return DOCA_SUCCESS;
201 }
202 
203 /*
204  * Sample main function
205  *
206  * @argc [in]: command line arguments size
207  * @argv [in]: array of command line arguments
208  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
209  */
210 int main(int argc, char **argv)
211 {
213  struct eth_rxq_cfg eth_rxq_cfg;
214  struct doca_log_backend *sdk_log;
215  int exit_status = EXIT_FAILURE;
216 
217  /* Register a logger backend */
219  if (result != DOCA_SUCCESS)
220  goto sample_exit;
221 
222  /* Register a logger backend for internal SDK errors and warnings */
223  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
224  if (result != DOCA_SUCCESS)
225  goto sample_exit;
227  if (result != DOCA_SUCCESS)
228  goto sample_exit;
229 
230  strcpy(eth_rxq_cfg.ib_dev_name, "mlx5_0");
234 
236  if (result != DOCA_SUCCESS) {
237  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
238  goto sample_exit;
239  }
240 
242  if (result != DOCA_SUCCESS) {
243  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
244  goto argp_cleanup;
245  }
246 
247  result = doca_argp_start(argc, argv);
248  if (result != DOCA_SUCCESS) {
249  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
250  goto argp_cleanup;
251  }
252 
257  if (result != DOCA_SUCCESS) {
258  DOCA_LOG_ERR("eth_rxq_batch_managed_mempool_receive() encountered an error: %s",
260  goto argp_cleanup;
261  }
262 
263  exit_status = EXIT_SUCCESS;
264 
265 argp_cleanup:
267 sample_exit:
268  if (exit_status == EXIT_SUCCESS)
269  DOCA_LOG_INFO("Sample finished successfully");
270  else
271  DOCA_LOG_INFO("Sample finished with errors");
272  return exit_status;
273 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t extract_ibdev_name(char *ibdev_name, char *ibdev_name_out)
Definition: eth_common.c:128
static doca_error_t headroom_callback(void *param, void *config)
static doca_error_t register_eth_rxq_params(void)
int main(int argc, char **argv)
static doca_error_t timestamp_callback(void *param, void *config)
doca_error_t eth_rxq_batch_managed_mempool_receive(const char *ib_dev_name, bool timestamp_enable, uint16_t headroom_size, uint16_t tailroom_size)
DOCA_LOG_REGISTER(ETH_RXQ_BATCH_MANAGED_MEMPOOL_RECEIVE::MAIN)
static doca_error_t device_address_callback(void *param, void *config)
static doca_error_t tailroom_callback(void *param, void *config)
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
@ DOCA_ARGP_TYPE_BOOLEAN
Definition: doca_argp.h:58
#define DOCA_DEVINFO_IBDEV_NAME_SIZE
Buffer size to hold Infiniband/RoCE device name. Including a null terminator.
Definition: doca_dev.h:309
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
char ib_dev_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]