NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_msix_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_MSIX_DPU::MAIN);
37 
38 /* Sample's Logic */
39 doca_error_t devemu_pci_device_msix_dpu(const char *pci_address, const char *emulated_dev_vuid, uint16_t msix_idx);
40 
41 /* Configuration struct */
42 struct devemu_pci_cfg {
43  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* device PCI address */
44  char vuid[DOCA_DEVINFO_REP_VUID_SIZE]; /* VUID of emulated device with MSI-X regions */
45  uint16_t msix_idx; /* Index of MSI-X to raise */
46 };
47 
48 #ifdef DOCA_ARCH_DPU
49 
50 /*
51  * ARGP Callback - Handle PCI device address parameter
52  *
53  * @param [in]: Input parameter
54  * @config [in/out]: Program configuration context
55  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
56  */
57 static doca_error_t pci_callback(void *param, void *config)
58 {
59  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
60  const char *addr = (char *)param;
61 
62  return parse_pci_address(addr, conf->pci_address);
63 }
64 
65 /*
66  * ARGP Callback - Handle VUID parameter
67  *
68  * @param [in]: Input parameter
69  * @config [in/out]: Program configuration context
70  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
71  */
72 static doca_error_t vuid_callback(void *param, void *config)
73 {
74  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
75  const char *vuid = (char *)param;
76 
77  return parse_vuid(vuid, conf->vuid);
78 }
79 
80 /*
81  * ARGP Callback - Handle MSI-X index parameter
82  *
83  * @param [in]: Input parameter
84  * @config [in/out]: Program configuration context
85  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
86  */
87 static doca_error_t msix_index_callback(void *param, void *config)
88 {
89  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
90  int msix = *(int *)param;
91 
92  if (msix < 0 || msix >= PCI_TYPE_NUM_MSIX) {
93  DOCA_LOG_ERR("Entered MSI-X index must be in [0, %u (PCI_TYPE_NUM_MSIX)) but received %d instead",
95  msix);
97  }
98 
99  conf->msix_idx = (uint16_t)msix;
100 
101  return DOCA_SUCCESS;
102 }
103 
104 /*
105  * Register MSI-X index command line parameter
106  *
107  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
108  */
109 static doca_error_t register_msix_index_param(void)
110 {
111  struct doca_argp_param *param;
113 
114  /* Create and register PCI address param */
115  result = doca_argp_param_create(&param);
116  if (result != DOCA_SUCCESS) {
117  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
118  return result;
119  }
120  doca_argp_param_set_short_name(param, "x");
121  doca_argp_param_set_long_name(param, "msix-index");
123  param,
124  "DOCA Devemu device MSI-X vector index. Positive integer that is lower than PCI_TYPE_NUM_MSIX");
125  doca_argp_param_set_callback(param, msix_index_callback);
128  if (result != DOCA_SUCCESS) {
129  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
130  return result;
131  }
132 
133  return DOCA_SUCCESS;
134 }
135 
136 /*
137  * Register the command line parameters for the sample
138  *
139  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
140  */
142 {
144 
146  if (result != DOCA_SUCCESS)
147  return result;
148 
150  "DOCA Devemu emulated device VUID. Sample will use this device to raise MSI-X towards Host",
151  vuid_callback);
152  if (result != DOCA_SUCCESS)
153  return result;
154 
155  result = register_msix_index_param();
156  if (result != DOCA_SUCCESS)
157  return result;
158 
159  return DOCA_SUCCESS;
160 }
161 
162 #endif // DOCA_ARCH_DPU
163 
164 /*
165  * Sample main function
166  *
167  * @argc [in]: command line arguments size
168  * @argv [in]: array of command line arguments
169  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
170  */
171 int main(int argc, char **argv)
172 {
175  struct doca_log_backend *sdk_log;
176  int exit_status = EXIT_FAILURE;
177 
178  /* Set the default configuration values (Example values) */
179  strcpy(devemu_pci_cfg.pci_address, "0000:03:00.0");
180  strcpy(devemu_pci_cfg.vuid, "");
181 
182  /* Register a logger backend */
184  if (result != DOCA_SUCCESS)
185  goto sample_exit;
186 
187  /* Register a logger backend for internal SDK errors and warnings */
188  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
189  if (result != DOCA_SUCCESS)
190  goto sample_exit;
192  if (result != DOCA_SUCCESS)
193  goto sample_exit;
194 
195  DOCA_LOG_INFO("Starting the sample");
196 
197 #ifdef DOCA_ARCH_DPU
199  if (result != DOCA_SUCCESS) {
200  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
201  goto sample_exit;
202  }
204  if (result != DOCA_SUCCESS) {
205  DOCA_LOG_ERR("Failed to register sample command line parameters: %s", doca_error_get_descr(result));
206  goto argp_cleanup;
207  }
208  result = doca_argp_start(argc, argv);
209  if (result != DOCA_SUCCESS) {
210  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
211  goto argp_cleanup;
212  }
213 
214  if (*devemu_pci_cfg.vuid == 0) {
215  DOCA_LOG_ERR("The VUID parameter is missing");
216  goto argp_cleanup;
217  }
218 
220  if (result != DOCA_SUCCESS) {
221  DOCA_LOG_ERR("devemu_pci_device_msix_dpu() encountered an error: %s", doca_error_get_descr(result));
222  goto argp_cleanup;
223  }
224 
225  exit_status = EXIT_SUCCESS;
226 
227 argp_cleanup:
229 
230 #else // DOCA_ARCH_DPU
231  (void)argc;
232  (void)argv;
233 
234  DOCA_LOG_ERR("PCI Emulated Device MSI-X DPU can run only on the DPU");
235  exit_status = EXIT_FAILURE;
236 
237 #endif // DOCA_ARCH_DPU
238 
239 sample_exit:
240  if (exit_status == EXIT_SUCCESS)
241  DOCA_LOG_INFO("Sample finished successfully");
242  else
243  DOCA_LOG_INFO("Sample finished with errors");
244  return exit_status;
245 }
#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)
static doca_error_t register_devemu_pci_params(void)
static doca_error_t pci_callback(void *param, void *config)
DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_MSIX_DPU::MAIN)
int main(int argc, char **argv)
doca_error_t devemu_pci_device_msix_dpu(const char *pci_address, const char *emulated_dev_vuid, uint16_t msix_idx)
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_MSIX
char vuid[DOCA_DEVINFO_REP_VUID_SIZE]
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]