NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
eth_rxq_regular_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_REGULAR_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 };
44 
45 /* Sample's Logic */
46 doca_error_t eth_rxq_regular_receive(const char *ib_dev_name, bool timestamp_enable);
47 
48 /*
49  * ARGP Callback - Handle IB device name parameter
50  *
51  * @param [in]: Input parameter
52  * @config [out]: Program configuration context
53  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
54  */
55 static doca_error_t device_address_callback(void *param, void *config)
56 {
57  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
58 
59  return extract_ibdev_name((char *)param, eth_rxq_cfg->ib_dev_name);
60 }
61 
62 /*
63  * ARGP Callback - timestamp enable parameter
64  *
65  * @param [in]: Input parameter
66  * @config [out]: Program configuration context
67  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
68  */
69 static doca_error_t timestamp_callback(void *param, void *config)
70 {
71  (void)param;
72  struct eth_rxq_cfg *eth_rxq_cfg = (struct eth_rxq_cfg *)config;
73 
75 
76  return DOCA_SUCCESS;
77 }
78 
79 /*
80  * Register the command line parameters for the sample.
81  *
82  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
83  */
85 {
87  struct doca_argp_param *dev_ib_name_param;
88  struct doca_argp_param *enable_timestamp_param;
89 
90  result = doca_argp_param_create(&dev_ib_name_param);
91  if (result != DOCA_SUCCESS) {
92  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
93  return result;
94  }
95 
96  doca_argp_param_set_short_name(dev_ib_name_param, "d");
97  doca_argp_param_set_long_name(dev_ib_name_param, "device");
98  doca_argp_param_set_description(dev_ib_name_param, "IB device name - default: mlx5_0");
101  result = doca_argp_register_param(dev_ib_name_param);
102  if (result != DOCA_SUCCESS) {
103  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
104  return result;
105  }
106 
107  result = doca_argp_param_create(&enable_timestamp_param);
108  if (result != DOCA_SUCCESS) {
109  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
110  return result;
111  }
112 
113  doca_argp_param_set_short_name(enable_timestamp_param, "ts");
114  doca_argp_param_set_long_name(enable_timestamp_param, "timestamp");
115  doca_argp_param_set_description(enable_timestamp_param, "Enable timestamp retrieval - default: disabled");
116  doca_argp_param_set_callback(enable_timestamp_param, timestamp_callback);
117  doca_argp_param_set_type(enable_timestamp_param, DOCA_ARGP_TYPE_BOOLEAN);
118  result = doca_argp_register_param(enable_timestamp_param);
119  if (result != DOCA_SUCCESS) {
120  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
121  return result;
122  }
123 
124  return DOCA_SUCCESS;
125 }
126 
127 /*
128  * Sample main function
129  *
130  * @argc [in]: command line arguments size
131  * @argv [in]: array of command line arguments
132  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
133  */
134 int main(int argc, char **argv)
135 {
137  struct eth_rxq_cfg eth_rxq_cfg;
138  struct doca_log_backend *sdk_log;
139  int exit_status = EXIT_FAILURE;
140 
141  /* Register a logger backend */
143  if (result != DOCA_SUCCESS)
144  goto sample_exit;
145 
146  /* Register a logger backend for internal SDK errors and warnings */
147  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
148  if (result != DOCA_SUCCESS)
149  goto sample_exit;
151  if (result != DOCA_SUCCESS)
152  goto sample_exit;
153 
154  strcpy(eth_rxq_cfg.ib_dev_name, "mlx5_0");
156 
158  if (result != DOCA_SUCCESS) {
159  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
160  goto sample_exit;
161  }
162 
164  if (result != DOCA_SUCCESS) {
165  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
166  goto argp_cleanup;
167  }
168 
169  result = doca_argp_start(argc, argv);
170  if (result != DOCA_SUCCESS) {
171  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
172  goto argp_cleanup;
173  }
174 
176  if (result != DOCA_SUCCESS) {
177  DOCA_LOG_ERR("eth_rxq_regular_receive() encountered an error: %s", doca_error_get_descr(result));
178  goto argp_cleanup;
179  }
180 
181  exit_status = EXIT_SUCCESS;
182 
183 argp_cleanup:
185 sample_exit:
186  if (exit_status == EXIT_SUCCESS)
187  DOCA_LOG_INFO("Sample finished successfully");
188  else
189  DOCA_LOG_INFO("Sample finished with errors");
190  return exit_status;
191 }
#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 register_eth_rxq_params(void)
int main(int argc, char **argv)
static doca_error_t timestamp_callback(void *param, void *config)
static doca_error_t device_address_callback(void *param, void *config)
doca_error_t eth_rxq_regular_receive(const char *ib_dev_name, bool timestamp_enable)
DOCA_LOG_REGISTER(ETH_RXQ_REGULAR_RECEIVE::MAIN)
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]