NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_stateful_region_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 #include <devemu_pci_type_config.h>
36 
37 DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_STATEFUL_REGION_HOST::MAIN);
38 
39 /* Sample's Logic */
41  int vfio_group,
42  int region_index,
43  const char *write_data);
44 
45 /* Configuration struct */
46 struct devemu_pci_cfg {
47  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* Emulated device PCI address */
48  char write_data[PCI_TYPE_MAX_STATEFUL_REGION_SIZE]; /* Data to write to stateful region */
49  int vfio_group; /* The VFIO group ID of the emulated device */
50  int region_index; /* The stateful region index to write to */
51 };
52 
53 #ifndef DOCA_ARCH_DPU
54 
55 /*
56  * ARGP Callback - Handle PCI device address parameter
57  *
58  * @param [in]: Input parameter
59  * @config [in/out]: Program configuration context
60  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
61  */
62 static doca_error_t pci_callback(void *param, void *config)
63 {
64  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
65  const char *addr = (char *)param;
66 
68 }
69 
70 /*
71  * ARGP Callback - Handle VFIO Group ID parameter
72  *
73  * @param [in]: Input parameter
74  * @config [in/out]: Program configuration context
75  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
76  */
77 static doca_error_t vfio_group_callback(void *param, void *config)
78 {
79  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
80  int group_id = *(int *)param;
81 
82  if (group_id < 0) {
83  DOCA_LOG_ERR("Entered VFIO group ID, is invalid. Must be positive integer. Received %d", group_id);
85  }
86 
87  conf->vfio_group = group_id;
88 
89  return DOCA_SUCCESS;
90 }
91 
92 /*
93  * ARGP Callback - Handle Stateful Region Index parameter
94  *
95  * @param [in]: Input parameter
96  * @config [in/out]: Program configuration context
97  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
98  */
99 static doca_error_t region_index_callback(void *param, void *config)
100 {
101  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
102  int region_index = *(int *)param;
103 
104  if (region_index < 0 || region_index >= PCI_TYPE_NUM_BAR_STATEFUL_REGIONS) {
105  DOCA_LOG_ERR(
106  "Entered region index, is invalid. Must be in [0, %d (PCI_TYPE_NUM_BAR_STATEFUL_REGIONS)). Received %d",
108  region_index);
110  }
111 
112  conf->region_index = region_index;
113 
114  return DOCA_SUCCESS;
115 }
116 
117 /*
118  * ARGP Callback - Handle Stateful Region Index parameter
119  *
120  * @param [in]: Input parameter
121  * @config [in/out]: Program configuration context
122  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
123  */
124 static doca_error_t write_data_callback(void *param, void *config)
125 {
126  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
127  const char *write_data = (char *)param;
128  int data_len = strnlen(write_data, PCI_TYPE_MAX_STATEFUL_REGION_SIZE) + 1;
129 
130  /* Check using > to make static code analysis satisfied */
131  if (data_len > PCI_TYPE_MAX_STATEFUL_REGION_SIZE) {
132  DOCA_LOG_ERR("Entered write data exceeds max stateful region size of %d",
135  }
136 
137  strncpy(conf->write_data, write_data, data_len - 1);
138  conf->write_data[data_len - 1] = 0;
139 
140  return DOCA_SUCCESS;
141 }
142 
143 /*
144  * Register write data command line parameter
145  *
146  * @write_data_callback [in]: Callback called for parsing the write data command line param
147  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
148  */
150 {
151  struct doca_argp_param *write_data_param;
153 
154  result = doca_argp_param_create(&write_data_param);
155  if (result != DOCA_SUCCESS) {
156  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
157  return result;
158  }
159  doca_argp_param_set_short_name(write_data_param, "w");
160  doca_argp_param_set_long_name(write_data_param, "write-data");
162  write_data_param,
163  "Data to write to the stateful region, if not provided then the sample will do a read instead. ASCII string");
166  result = doca_argp_register_param(write_data_param);
167  if (result != DOCA_SUCCESS) {
168  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
169  return result;
170  }
171 
172  return DOCA_SUCCESS;
173 }
174 
175 /*
176  * Register the command line parameters for the sample
177  *
178  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
179  */
181 {
183 
185  if (result != DOCA_SUCCESS)
186  return result;
187 
189  if (result != DOCA_SUCCESS)
190  return result;
191 
193  "The index of the stateful region as defined in devemu_pci_type_config.h. Integer",
195  if (result != DOCA_SUCCESS)
196  return result;
197 
199  if (result != DOCA_SUCCESS)
200  return result;
201 
202  return DOCA_SUCCESS;
203 }
204 
205 #endif // DOCA_ARCH_DPU
206 
207 /*
208  * Sample main function
209  *
210  * @argc [in]: command line arguments size
211  * @argv [in]: array of command line arguments
212  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
213  */
214 int main(int argc, char **argv)
215 {
218  struct doca_log_backend *sdk_log;
219  int exit_status = EXIT_FAILURE;
220 
221  /* Set the default configuration values (Example values) */
223 
224  /* Register a logger backend */
226  if (result != DOCA_SUCCESS)
227  goto sample_exit;
228 
229  /* Register a logger backend for internal SDK errors and warnings */
230  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
231  if (result != DOCA_SUCCESS)
232  goto sample_exit;
234  if (result != DOCA_SUCCESS)
235  goto sample_exit;
236 
237  DOCA_LOG_INFO("Starting the sample");
238 
239 #ifndef DOCA_ARCH_DPU
240 
242  if (result != DOCA_SUCCESS) {
243  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
244  goto sample_exit;
245  }
247  if (result != DOCA_SUCCESS) {
248  DOCA_LOG_ERR("Failed to register sample command line parameters: %s", doca_error_get_descr(result));
249  goto argp_cleanup;
250  }
251  result = doca_argp_start(argc, argv);
252  if (result != DOCA_SUCCESS) {
253  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
254  goto argp_cleanup;
255  }
256 
261  if (result != DOCA_SUCCESS) {
262  DOCA_LOG_ERR("devemu_pci_device_stateful_region_host() encountered an error: %s",
264  goto argp_cleanup;
265  }
266 
267  exit_status = EXIT_SUCCESS;
268 
269 argp_cleanup:
271 
272 #else // DOCA_ARCH_DPU
273  (void)argc;
274  (void)argv;
275  (void)devemu_pci_cfg;
276 
277  DOCA_LOG_ERR("PCI Emulated Device Stateful Region Host can run only on the Host");
278  exit_status = EXIT_FAILURE;
279 
280 #endif // DOCA_ARCH_DPU
281 
282 sample_exit:
283  if (exit_status == EXIT_SUCCESS)
284  DOCA_LOG_INFO("Sample finished successfully");
285  else
286  DOCA_LOG_INFO("Sample finished with errors");
287  return exit_status;
288 }
#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)
static doca_error_t region_index_callback(void *param, void *config)
doca_error_t devemu_pci_device_stateful_region_host(const char *pci_address, int vfio_group, int region_index, 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_STATEFUL_REGION_HOST::MAIN)
doca_error_t register_vfio_group_param(doca_argp_param_cb_t vfio_group_callback)
doca_error_t register_region_index_param(const char *description, doca_argp_param_cb_t region_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
#define PCI_TYPE_NUM_BAR_STATEFUL_REGIONS
#define PCI_TYPE_MAX_STATEFUL_REGION_SIZE
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]