NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
eth_rxq_managed_mempool_receive_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 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 <stdbool.h>
28 #include <string.h>
29 
30 #include <doca_argp.h>
31 #include <doca_dev.h>
32 #include <doca_error.h>
33 #include <doca_log.h>
34 
35 #include "eth_common.h"
36 
37 DOCA_LOG_REGISTER(ETH_RXQ_MANAGED_MEMPOOL_RECEIVE::MAIN);
38 
39 /* Configuration struct */
40 struct eth_rxq_cfg {
41  char ib_dev_name[DOCA_DEVINFO_IBDEV_NAME_SIZE]; /* DOCA IB device name */
42  bool timestamp_enable; /* timestamp enable */
43  uint16_t headroom_size; /* headroom size */
44  uint16_t tailroom_size; /* tailroom size */
45 };
46 
47 /* Sample's Logic */
48 doca_error_t eth_rxq_managed_mempool_receive(const char *ib_dev_name,
49  bool timestamp_enable,
50  uint16_t headroom_size,
51  uint16_t tailroom_size);
52 
53 /*
54  * ARGP Callback - Handle IB device name parameter
55  *
56  * @param [in]: Input parameter
57  * @config [out]: Program configuration context
58  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
59  */
60 static doca_error_t device_address_callback(void *param, void *config)
61 {
62  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
63 
64  return extract_ibdev_name((char *)param, eth_rxq_cfg->ib_dev_name);
65 }
66 
67 /*
68  * ARGP Callback - timestamp enable parameter
69  *
70  * @param [in]: Input parameter
71  * @config [out]: Program configuration context
72  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
73  */
74 static doca_error_t timestamp_callback(void *param, void *config)
75 {
76  (void)param;
77  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
78 
80 
81  return DOCA_SUCCESS;
82 }
83 
84 /*
85  * ARGP Callback - headroom size parameter
86  *
87  * @param [in]: Input parameter
88  * @config [out]: Program configuration context
89  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
90  */
91 static doca_error_t headroom_callback(void *param, void *config)
92 {
93  uint16_t headroom_size = strtoul((char *)param, NULL, 10);
94  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
95 
97 
98  return DOCA_SUCCESS;
99 }
100 
101 /*
102  * ARGP Callback - tailroom size parameter
103  *
104  * @param [in]: Input parameter
105  * @config [out]: Program configuration context
106  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
107  */
108 static doca_error_t tailroom_callback(void *param, void *config)
109 {
110  uint16_t tailroom_size = strtoul((char *)param, NULL, 10);
111  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
112 
114 
115  return DOCA_SUCCESS;
116 }
117 
118 /*
119  * Register the command line parameters for the sample.
120  *
121  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
122  */
124 {
126  struct doca_argp_param *dev_ib_name_param;
127  struct doca_argp_param *enable_timestamp_param;
128  struct doca_argp_param *headroom_param;
129  struct doca_argp_param *tailroom_param;
130 
131  result = doca_argp_param_create(&dev_ib_name_param);
132  if (result != DOCA_SUCCESS) {
133  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
134  return result;
135  }
136 
137  doca_argp_param_set_short_name(dev_ib_name_param, "d");
138  doca_argp_param_set_long_name(dev_ib_name_param, "device");
139  doca_argp_param_set_description(dev_ib_name_param, "IB device name - default: mlx5_0");
142  result = doca_argp_register_param(dev_ib_name_param);
143  if (result != DOCA_SUCCESS) {
144  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
145  return result;
146  }
147 
148  result = doca_argp_param_create(&enable_timestamp_param);
149  if (result != DOCA_SUCCESS) {
150  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
151  return result;
152  }
153 
154  doca_argp_param_set_short_name(enable_timestamp_param, "ts");
155  doca_argp_param_set_long_name(enable_timestamp_param, "timestamp");
156  doca_argp_param_set_description(enable_timestamp_param, "Enable timestamp retrieval - default: disabled");
157  doca_argp_param_set_callback(enable_timestamp_param, timestamp_callback);
158  doca_argp_param_set_type(enable_timestamp_param, DOCA_ARGP_TYPE_BOOLEAN);
159  result = doca_argp_register_param(enable_timestamp_param);
160  if (result != DOCA_SUCCESS) {
161  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
162  return result;
163  }
164 
165  result = doca_argp_param_create(&headroom_param);
166  if (result != DOCA_SUCCESS) {
167  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
168  return result;
169  }
170 
171  doca_argp_param_set_short_name(headroom_param, "hr");
172  doca_argp_param_set_long_name(headroom_param, "headroom");
173  doca_argp_param_set_description(headroom_param, "Packet headroom size - default: 0");
176  result = doca_argp_register_param(headroom_param);
177  if (result != DOCA_SUCCESS) {
178  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
179  return result;
180  }
181 
182  result = doca_argp_param_create(&tailroom_param);
183  if (result != DOCA_SUCCESS) {
184  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
185  return result;
186  }
187 
188  doca_argp_param_set_short_name(tailroom_param, "tr");
189  doca_argp_param_set_long_name(tailroom_param, "tailroom");
190  doca_argp_param_set_description(tailroom_param, "Packet tailroom size - default: 0");
193  result = doca_argp_register_param(tailroom_param);
194  if (result != DOCA_SUCCESS) {
195  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
196  return result;
197  }
198 
199  return DOCA_SUCCESS;
200 }
201 
202 /*
203  * Sample main function
204  *
205  * @argc [in]: command line arguments size
206  * @argv [in]: array of command line arguments
207  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
208  */
209 int main(int argc, char **argv)
210 {
212  struct eth_rxq_cfg eth_rxq_cfg;
213  struct doca_log_backend *sdk_log;
214  int exit_status = EXIT_FAILURE;
215 
216  /* Register a logger backend */
218  if (result != DOCA_SUCCESS)
219  goto sample_exit;
220 
221  /* Register a logger backend for internal SDK errors and warnings */
222  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
223  if (result != DOCA_SUCCESS)
224  goto sample_exit;
226  if (result != DOCA_SUCCESS)
227  goto sample_exit;
228 
229  strcpy(eth_rxq_cfg.ib_dev_name, "mlx5_0");
233 
235  if (result != DOCA_SUCCESS) {
236  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
237  goto sample_exit;
238  }
239 
241  if (result != DOCA_SUCCESS) {
242  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
243  goto argp_cleanup;
244  }
245 
246  result = doca_argp_start(argc, argv);
247  if (result != DOCA_SUCCESS) {
248  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
249  goto argp_cleanup;
250  }
251 
256  if (result != DOCA_SUCCESS) {
257  DOCA_LOG_ERR("eth_rxq_managed_mempool_receive() encountered an error: %s",
259  goto argp_cleanup;
260  }
261 
262  exit_status = EXIT_SUCCESS;
263 
264 argp_cleanup:
266 sample_exit:
267  if (exit_status == EXIT_SUCCESS)
268  DOCA_LOG_INFO("Sample finished successfully");
269  else
270  DOCA_LOG_INFO("Sample finished with errors");
271  return exit_status;
272 }
#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_LOG_REGISTER(ETH_RXQ_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_error_t eth_rxq_managed_mempool_receive(const char *ib_dev_name, bool timestamp_enable, uint16_t headroom_size, uint16_t tailroom_size)
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]