NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_dma_host_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_host_common.h>
35 
36 #define MEM_BUF_LEN (4 * 1024) /* Mem buffer size. It's the same as DPU side*/
37 
38 DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_DMA_HOST::MAIN);
39 
40 /* Sample's Logic */
41 doca_error_t devemu_pci_device_dma_host(const char *pci_address, int vfio_group, const char *write_data);
42 
43 /* Configuration struct */
44 struct devemu_pci_cfg {
45  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* Emulated device PCI address */
46  char write_data[MEM_BUF_LEN]; /* Data to write to DPU memory */
47  int vfio_group; /* The VFIO group ID of the emulated device */
48 };
49 
50 #ifndef DOCA_ARCH_DPU
51 
52 /*
53  * ARGP Callback - Handle PCI device address parameter
54  *
55  * @param [in]: Input parameter
56  * @config [in/out]: Program configuration context
57  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
58  */
59 static doca_error_t pci_callback(void *param, void *config)
60 {
61  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
62  const char *addr = (char *)param;
64 }
65 
66 /*
67  * ARGP Callback - Handle VFIO Group ID parameter
68  *
69  * @param [in]: Input parameter
70  * @config [in/out]: Program configuration context
71  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
72  */
73 static doca_error_t vfio_group_callback(void *param, void *config)
74 {
75  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
76  int group_id = *(int *)param;
77 
78  if (group_id < 0) {
79  DOCA_LOG_ERR("Entered VFIO group ID, is invalid. Must be positive integer. Received %d", group_id);
81  }
82 
83  conf->vfio_group = group_id;
84 
85  return DOCA_SUCCESS;
86 }
87 
88 /*
89  * ARGP Callback - Handle Stateful Region Index parameter
90  *
91  * @param [in]: Input parameter
92  * @config [in/out]: Program configuration context
93  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
94  */
95 static doca_error_t write_data_callback(void *param, void *config)
96 {
97  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
98  const char *write_data = (char *)param;
99  int data_len = strnlen(write_data, MEM_BUF_LEN) + 1;
100 
101  /* Check using > to make static code analysis satisfied */
102  if (data_len > MEM_BUF_LEN) {
103  DOCA_LOG_ERR("Entered write data exceeds max memory buffer size of %d", MEM_BUF_LEN - 1);
105  }
106 
107  strncpy(conf->write_data, write_data, data_len - 1);
108  conf->write_data[data_len - 1] = 0;
109 
110  return DOCA_SUCCESS;
111 }
112 
113 /*
114  * Register write data command line parameter
115  *
116  * @write_data_callback [in]: Callback called for parsing the write data command line param
117  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
118  */
120 {
121  struct doca_argp_param *write_data_param;
123 
124  result = doca_argp_param_create(&write_data_param);
125  if (result != DOCA_SUCCESS) {
126  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
127  return result;
128  }
129  doca_argp_param_set_short_name(write_data_param, "w");
130  doca_argp_param_set_long_name(write_data_param, "write-data");
132  write_data_param,
133  "Data to write to DPU memory, if not provided then the sample will do a read instead. ASCII string");
136  result = doca_argp_register_param(write_data_param);
137  if (result != DOCA_SUCCESS) {
138  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
139  return result;
140  }
141 
142  return DOCA_SUCCESS;
143 }
144 
145 /*
146  * Register the command line parameters for the sample
147  *
148  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
149  */
151 {
153 
155  if (result != DOCA_SUCCESS)
156  return result;
157 
159  if (result != DOCA_SUCCESS)
160  return result;
161 
163  if (result != DOCA_SUCCESS)
164  return result;
165 
166  return DOCA_SUCCESS;
167 }
168 
169 #endif // DOCA_ARCH_DPU
170 
171 /*
172  * Sample main function
173  *
174  * @argc [in]: command line arguments size
175  * @argv [in]: array of command line arguments
176  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
177  */
178 int main(int argc, char **argv)
179 {
182  struct doca_log_backend *sdk_log;
183  int exit_status = EXIT_FAILURE;
184 
185  /* Set the default configuration values (Example values) */
186  strcpy(devemu_pci_cfg.pci_address, "");
187  strcpy(devemu_pci_cfg.write_data, "This is a sample piece of data from Host!");
189 
190  /* Register a logger backend */
192  if (result != DOCA_SUCCESS)
193  goto sample_exit;
194 
195  /* Register a logger backend for internal SDK errors and warnings */
196  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
197  if (result != DOCA_SUCCESS)
198  goto sample_exit;
200  if (result != DOCA_SUCCESS)
201  goto sample_exit;
202 
203  DOCA_LOG_INFO("Starting the sample");
204 
205 #ifndef DOCA_ARCH_DPU
206 
208  if (result != DOCA_SUCCESS) {
209  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
210  goto sample_exit;
211  }
213  if (result != DOCA_SUCCESS) {
214  DOCA_LOG_ERR("Failed to register sample command line parameters: %s", doca_error_get_descr(result));
215  goto argp_cleanup;
216  }
217  result = doca_argp_start(argc, argv);
218  if (result != DOCA_SUCCESS) {
219  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
220  goto argp_cleanup;
221  }
222 
223  if (*devemu_pci_cfg.pci_address == 0) {
224  DOCA_LOG_ERR("The PCI address parameter is missing");
225  goto argp_cleanup;
226  }
227  if (devemu_pci_cfg.vfio_group == -1) {
228  DOCA_LOG_ERR("The VFIO group ID parameter is missing");
229  goto argp_cleanup;
230  }
231 
235  if (result != DOCA_SUCCESS) {
236  DOCA_LOG_ERR("devemu_pci_device_host() encountered an error: %s", doca_error_get_descr(result));
237  goto argp_cleanup;
238  }
239 
240  exit_status = EXIT_SUCCESS;
241 
242 argp_cleanup:
244 
245 #else // DOCA_ARCH_DPU
246  (void)argc;
247  (void)argv;
248 
249  DOCA_LOG_ERR("PCI Emulated Device DMA Host can run only on the Host");
250  exit_status = EXIT_FAILURE;
251 
252 #endif // DOCA_ARCH_DPU
253 
254 sample_exit:
255  if (exit_status == EXIT_SUCCESS)
256  DOCA_LOG_INFO("Sample finished successfully");
257  else
258  DOCA_LOG_INFO("Sample finished with errors");
259  return exit_status;
260 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
int main(int argc, char **argv)
static doca_error_t register_devemu_pci_params(void)
doca_error_t devemu_pci_device_dma_host(const char *pci_address, int vfio_group, const char *write_data)
static doca_error_t pci_callback(void *param, void *config)
static doca_error_t vfio_group_callback(void *param, void *config)
static doca_error_t write_data_callback(void *param, void *config)
static doca_error_t register_write_data_param(doca_argp_param_cb_t write_data_callback)
DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_DMA_HOST::MAIN)
doca_error_t register_vfio_group_param(doca_argp_param_cb_t vfio_group_callback)
doca_error_t register_emulated_pci_address_param(doca_argp_param_cb_t pci_callback)
doca_error_t parse_emulated_pci_address(const char *addr, char *parsed_addr)
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_error_t(* doca_argp_param_cb_t)(void *, void *)
Flag callback function type.
Definition: doca_argp.h:37
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
#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_INFO
Definition: doca_log.h:48
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]