NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_db_dpu_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 <string.h>
28 
29 #include <doca_argp.h>
30 #include <doca_error.h>
31 #include <doca_dev.h>
32 #include <doca_log.h>
33 
34 #include <devemu_pci_common.h>
35 
36 DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_DB_DPU::MAIN);
37 
38 /* Sample's Logic */
39 doca_error_t devemu_pci_device_db_dpu(const char *pci_address,
40  const char *emulated_dev_vuid,
41  uint16_t db_region_idx,
42  uint32_t db_id);
43 
44 /* Configuration struct */
46  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* device PCI address */
47  char vuid[DOCA_DEVINFO_REP_VUID_SIZE]; /* VUID of emulated device with DB regions */
48  uint16_t db_region_idx; /* Index of DB region to listen on */
49  uint32_t db_id; /* The DB ID of the DB to listen on */
50 };
51 
52 #ifdef DOCA_ARCH_DPU
53 
54 /*
55  * ARGP Callback - Handle PCI device address parameter
56  *
57  * @param [in]: Input parameter
58  * @config [in/out]: Program configuration context
59  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
60  */
61 static doca_error_t pci_callback(void *param, void *config)
62 {
63  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
64  const char *addr = (char *)param;
65 
66  return parse_pci_address(addr, conf->pci_address);
67 }
68 
69 /*
70  * ARGP Callback - Handle VUID parameter
71  *
72  * @param [in]: Input parameter
73  * @config [in/out]: Program configuration context
74  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
75  */
76 static doca_error_t vuid_callback(void *param, void *config)
77 {
78  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
79  const char *vuid = (char *)param;
80 
81  return parse_vuid(vuid, conf->vuid);
82 }
83 
84 /*
85  * ARGP Callback - Handle DB region index parameter
86  *
87  * @param [in]: Input parameter
88  * @config [in/out]: Program configuration context
89  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
90  */
91 static doca_error_t region_index_callback(void *param, void *config)
92 {
93  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
94  int db_region_idx = *(int *)param;
95 
96  if (db_region_idx < 0 || db_region_idx >= PCI_TYPE_NUM_BAR_DB_REGIONS) {
98  "Entered DB region index must be in [0, %u (PCI_TYPE_NUM_BAR_DB_REGIONS)) but received %d instead",
100  db_region_idx);
102  }
103 
104  conf->db_region_idx = (uint16_t)db_region_idx;
105 
106  return DOCA_SUCCESS;
107 }
108 
109 /*
110  * Register DB region index command line parameter
111  *
112  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
113  */
115 {
116  struct doca_argp_param *param;
118 
119  result = doca_argp_param_create(&param);
120  if (result != DOCA_SUCCESS) {
121  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
122  return result;
123  }
124  doca_argp_param_set_short_name(param, "r");
125  doca_argp_param_set_long_name(param, "region-index");
127  "The index of the DB region as defined in devemu_pci_type_config.h. Integer");
131  if (result != DOCA_SUCCESS) {
132  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
133  return result;
134  }
135 
136  return DOCA_SUCCESS;
137 }
138 
139 /*
140  * ARGP Callback - Handle DB ID parameter
141  *
142  * @param [in]: Input parameter
143  * @config [in/out]: Program configuration context
144  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
145  */
146 static doca_error_t db_id_callback(void *param, void *config)
147 {
148  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
149  int db_id = *(int *)param;
150 
151  if (db_id < 0) {
152  DOCA_LOG_ERR("Entered DB ID must be greater than 0 but received %d instead", db_id);
154  }
155 
156  conf->db_id = (uint32_t)db_id;
157 
158  return DOCA_SUCCESS;
159 }
160 
161 /*
162  * Register DB ID command line parameter
163  *
164  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
165  */
166 static doca_error_t register_db_id_param(void)
167 {
168  struct doca_argp_param *param;
170 
171  result = doca_argp_param_create(&param);
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
174  return result;
175  }
176  doca_argp_param_set_short_name(param, "i");
177  doca_argp_param_set_long_name(param, "db-id");
179  param,
180  "The DB ID of the DB. Sample will listen on DBs related to this DB ID. Integer");
181  doca_argp_param_set_callback(param, db_id_callback);
184  if (result != DOCA_SUCCESS) {
185  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
186  return result;
187  }
188 
189  return DOCA_SUCCESS;
190 }
191 
192 /*
193  * Register the command line parameters for the sample
194  *
195  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
196  */
198 {
200 
202  if (result != DOCA_SUCCESS)
203  return result;
204 
206  "DOCA Devemu emulated device VUID. Sample will use this device to handle Doorbells from Host",
207  vuid_callback);
208  if (result != DOCA_SUCCESS)
209  return result;
210 
212  if (result != DOCA_SUCCESS)
213  return result;
214 
215  result = register_db_id_param();
216  if (result != DOCA_SUCCESS)
217  return result;
218 
219  return DOCA_SUCCESS;
220 }
221 
222 #endif // DOCA_ARCH_DPU
223 
224 /*
225  * Sample main function
226  *
227  * @argc [in]: command line arguments size
228  * @argv [in]: array of command line arguments
229  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
230  */
231 int main(int argc, char **argv)
232 {
235  struct doca_log_backend *sdk_log;
236  int exit_status = EXIT_FAILURE;
237 
238  /* Set the default configuration values (Example values) */
239  strcpy(devemu_pci_cfg.pci_address, "0000:03:00.0");
240  strcpy(devemu_pci_cfg.vuid, "");
242  devemu_pci_cfg.db_id = 0;
243 
244  /* Register a logger backend */
246  if (result != DOCA_SUCCESS)
247  goto sample_exit;
248 
249  /* Register a logger backend for internal SDK errors and warnings */
250  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
251  if (result != DOCA_SUCCESS)
252  goto sample_exit;
254  if (result != DOCA_SUCCESS)
255  goto sample_exit;
256 
257  DOCA_LOG_INFO("Starting the sample");
258 
259 #ifdef DOCA_ARCH_DPU
261  if (result != DOCA_SUCCESS) {
262  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
263  goto sample_exit;
264  }
266  if (result != DOCA_SUCCESS) {
267  DOCA_LOG_ERR("Failed to register sample command line parameters: %s", doca_error_get_descr(result));
268  goto argp_cleanup;
269  }
270  result = doca_argp_start(argc, argv);
271  if (result != DOCA_SUCCESS) {
272  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
273  goto argp_cleanup;
274  }
275 
276  if (*devemu_pci_cfg.vuid == 0) {
277  DOCA_LOG_ERR("The VUID parameter is missing");
278  goto argp_cleanup;
279  }
280 
285  if (result != DOCA_SUCCESS) {
286  DOCA_LOG_ERR("devemu_pci_device_db_dpu() encountered an error: %s", doca_error_get_descr(result));
287  goto argp_cleanup;
288  }
289 
290  exit_status = EXIT_SUCCESS;
291 
292 argp_cleanup:
294 
295 #else // DOCA_ARCH_DPU
296  (void)argc;
297  (void)argv;
298 
299  DOCA_LOG_ERR("PCI Emulated Device DB DPU can run only on the DPU");
300  exit_status = EXIT_FAILURE;
301 
302 #endif // DOCA_ARCH_DPU
303 
304 sample_exit:
305  if (exit_status == EXIT_SUCCESS)
306  DOCA_LOG_INFO("Sample finished successfully");
307  else
308  DOCA_LOG_INFO("Sample finished with errors");
309  return exit_status;
310 }
#define NULL
Definition: __stddef_null.h:26
static doca_error_t vuid_callback(void *param, void *config)
int32_t result
doca_error_t parse_pci_address(const char *addr, char *parsed_addr)
doca_error_t register_vuid_param(const char *description, doca_argp_param_cb_t vuid_callback)
doca_error_t parse_vuid(const char *vuid, char *parsed_vuid)
doca_error_t register_pci_address_param(doca_argp_param_cb_t pci_callback)
int main(int argc, char **argv)
doca_error_t devemu_pci_device_db_dpu(const char *pci_address, const char *emulated_dev_vuid, uint16_t db_region_idx, uint32_t db_id)
DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_DB_DPU::MAIN)
static doca_error_t register_devemu_pci_params(void)
static doca_error_t region_index_callback(void *param, void *config)
static doca_error_t pci_callback(void *param, void *config)
doca_error_t register_region_index_param(const char *description, doca_argp_param_cb_t region_callback)
uintptr_t addr
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_INT
Definition: doca_argp.h:57
#define DOCA_DEVINFO_REP_VUID_SIZE
Buffer size to hold VUID. Including a null terminator.
Definition: doca_dev.h:661
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
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_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ 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
#define PCI_TYPE_NUM_BAR_DB_REGIONS
char vuid[DOCA_DEVINFO_REP_VUID_SIZE]
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]