NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_vfs_device_hotplug_unplug_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 <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 <doca_devemu_vfs.h>
35 
36 DOCA_LOG_REGISTER(DPU_DEVEMU_VFS_DEVICE_CREATE::MAIN);
37 
38 /* Sample's Logic */
40 
41 /* Configuration struct */
42 struct devemu_pci_cfg {
43  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* device PCI address */
44 };
45 
46 /*
47  * ARGP Callback - Handle PCI device address parameter
48  *
49  * @param [in]: Input parameter
50  * @config [in/out]: Program configuration context
51  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
52  */
53 static doca_error_t pci_callback(void *param, void *config)
54 {
55  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
56  const char *addr = (char *)param;
57  int addr_len = strnlen(addr, DOCA_DEVINFO_PCI_ADDR_SIZE) + 1;
58 
59  /* Check using > to make static code analysis satisfied */
60  if (addr_len > DOCA_DEVINFO_PCI_ADDR_SIZE) {
61  DOCA_LOG_ERR("Entered device PCI address exceeding the maximum size of %d",
64  }
65 
66  if (addr_len != DOCA_DEVINFO_PCI_ADDR_SIZE && addr_len != DOCA_DEVINFO_PCI_BDF_SIZE) {
67  DOCA_LOG_ERR("Entered device PCI address does not match supported formats: XXXX:XX:XX.X or XX:XX.X");
69  }
70 
71  /* The string will be '\0' terminated due to the strnlen check above */
72  strncpy(conf->pci_address, addr, addr_len);
73 
74  return DOCA_SUCCESS;
75 }
76 
77 /*
78  * Register the command line parameters for the sample
79  *
80  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
81  */
83 {
84  struct doca_argp_param *pci_address_param;
86 
87  /* Create and register PCI address param */
88  result = doca_argp_param_create(&pci_address_param);
89  if (result != DOCA_SUCCESS) {
90  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
91  return result;
92  }
93  doca_argp_param_set_short_name(pci_address_param, "p");
94  doca_argp_param_set_long_name(pci_address_param, "pci-addr");
95  doca_argp_param_set_description(pci_address_param,
96  "DOCA Devemu device PCI address. Format: XXXX:XX:XX.X or XX:XX.X");
97  doca_argp_param_set_callback(pci_address_param, pci_callback);
99  result = doca_argp_register_param(pci_address_param);
100  if (result != DOCA_SUCCESS) {
101  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
102  return result;
103  }
104 
105  return DOCA_SUCCESS;
106 }
107 
108 /*
109  * Sample main function
110  *
111  * @argc [in]: command line arguments size
112  * @argv [in]: array of command line arguments
113  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
114  */
115 int main(int argc, char **argv)
116 {
119  struct doca_log_backend *sdk_log;
120  int exit_status = EXIT_FAILURE;
121 
122  /* Set the default configuration values (Example values) */
123  strcpy(devemu_pci_cfg.pci_address, "0000:03:00.0");
124 
125  /* Register a logger backend */
127  if (result != DOCA_SUCCESS)
128  goto sample_exit;
129 
130  /* Register a logger backend for internal SDK errors and warnings */
131  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
132  if (result != DOCA_SUCCESS)
133  goto sample_exit;
135  if (result != DOCA_SUCCESS)
136  goto sample_exit;
137 
138  DOCA_LOG_INFO("Starting the sample");
139 
140 #ifndef DOCA_ARCH_DPU
141  DOCA_LOG_ERR("Create PCI Emulated Device can run only on the DPU");
142  goto sample_exit;
143 #endif
144 
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
148  goto sample_exit;
149  }
151  if (result != DOCA_SUCCESS) {
152  DOCA_LOG_ERR("Failed to register sample command line parameters: %s", doca_error_get_descr(result));
153  goto argp_cleanup;
154  }
155  result = doca_argp_start(argc, argv);
156  if (result != DOCA_SUCCESS) {
157  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
158  goto argp_cleanup;
159  }
160 
162  if (result != DOCA_SUCCESS) {
163  DOCA_LOG_ERR("devemu_vfs_device_create() encountered an error: %s", doca_error_get_descr(result));
164  goto argp_cleanup;
165  }
166 
167  exit_status = EXIT_SUCCESS;
168 
169 argp_cleanup:
171 sample_exit:
172  if (exit_status == EXIT_SUCCESS)
173  DOCA_LOG_INFO("Sample finished successfully");
174  else
175  DOCA_LOG_INFO("Sample finished with errors");
176  return exit_status;
177 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t devemu_vfs_device_create(char *pci_address)
int main(int argc, char **argv)
static doca_error_t register_devemu_pci_params(void)
static doca_error_t pci_callback(void *param, void *config)
DOCA_LOG_REGISTER(DPU_DEVEMU_VFS_DEVICE_CREATE::MAIN)
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_STRING
Definition: doca_argp.h:56
#define DOCA_DEVINFO_PCI_BDF_SIZE
Buffer size to hold PCI BDF format: "XX:XX.X". Including a null terminator.
Definition: doca_dev.h:317
#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
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]