NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
devemu_pci_device_msix_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 DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_MSIX_HOST::MAIN);
37 
38 /* Sample's Logic */
39 doca_error_t devemu_pci_device_msix_host(const char *pci_address, int vfio_group);
40 
41 /* Configuration struct */
42 struct devemu_pci_cfg {
43  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* Emulated device PCI address */
44  int vfio_group; /* The VFIO group ID of the emulated device */
45 };
46 
47 #ifndef DOCA_ARCH_DPU
48 
49 /*
50  * ARGP Callback - Handle PCI device address parameter
51  *
52  * @param [in]: Input parameter
53  * @config [in/out]: Program configuration context
54  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
55  */
56 static doca_error_t pci_callback(void *param, void *config)
57 {
58  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
59  const char *addr = (char *)param;
60 
62 }
63 
64 /*
65  * ARGP Callback - Handle VFIO Group ID parameter
66  *
67  * @param [in]: Input parameter
68  * @config [in/out]: Program configuration context
69  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
70  */
71 static doca_error_t vfio_group_callback(void *param, void *config)
72 {
73  struct devemu_pci_cfg *conf = (struct devemu_pci_cfg *)config;
74  int group_id = *(int *)param;
75 
76  if (group_id < 0) {
77  DOCA_LOG_ERR("Entered VFIO group ID, is invalid. Must be positive integer. Received %d", group_id);
79  }
80 
81  conf->vfio_group = group_id;
82 
83  return DOCA_SUCCESS;
84 }
85 
86 /*
87  * Register the command line parameters for the sample
88  *
89  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
90  */
92 {
94 
96  if (result != DOCA_SUCCESS)
97  return result;
98 
100  if (result != DOCA_SUCCESS)
101  return result;
102 
103  return DOCA_SUCCESS;
104 }
105 
106 #endif // DOCA_ARCH_DPU
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  /* Register a logger backend */
124  if (result != DOCA_SUCCESS)
125  goto sample_exit;
126 
127  /* Register a logger backend for internal SDK errors and warnings */
128  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
129  if (result != DOCA_SUCCESS)
130  goto sample_exit;
132  if (result != DOCA_SUCCESS)
133  goto sample_exit;
134 
135  DOCA_LOG_INFO("Starting the sample");
136 
137 #ifndef DOCA_ARCH_DPU
138 
140  if (result != DOCA_SUCCESS) {
141  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
142  goto sample_exit;
143  }
145  if (result != DOCA_SUCCESS) {
146  DOCA_LOG_ERR("Failed to register sample command line parameters: %s", doca_error_get_descr(result));
147  goto argp_cleanup;
148  }
149  result = doca_argp_start(argc, argv);
150  if (result != DOCA_SUCCESS) {
151  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
152  goto argp_cleanup;
153  }
154 
156  if (result != DOCA_SUCCESS) {
157  DOCA_LOG_ERR("devemu_pci_device_msix_host() encountered an error: %s", doca_error_get_descr(result));
158  goto argp_cleanup;
159  }
160 
161  exit_status = EXIT_SUCCESS;
162 
163 argp_cleanup:
165 
166 #else // DOCA_ARCH_DPU
167  (void)argc;
168  (void)argv;
169  (void)devemu_pci_cfg;
170 
171  DOCA_LOG_ERR("PCI Emulated Device MSI-X Host can run only on the Host");
172  exit_status = EXIT_FAILURE;
173 
174 #endif // DOCA_ARCH_DPU
175 
176 sample_exit:
177  if (exit_status == EXIT_SUCCESS)
178  DOCA_LOG_INFO("Sample finished successfully");
179  else
180  DOCA_LOG_INFO("Sample finished with errors");
181  return exit_status;
182 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t devemu_pci_device_msix_host(const char *pci_address, int vfio_group)
DOCA_LOG_REGISTER(DEVEMU_PCI_DEVICE_MSIX_HOST::MAIN)
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)
static doca_error_t vfio_group_callback(void *param, void *config)
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 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 doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
#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]