NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
gpunetio_dma_memcpy_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 
28 #include <doca_argp.h>
29 #include <doca_dev.h>
30 #include <doca_error.h>
31 #include <doca_log.h>
32 
33 #include <gpunetio_dma_common.h>
34 
35 DOCA_LOG_REGISTER(GPU_DMA_MEMCPY::MAIN);
36 
37 /*
38  * ARGP Callback - Handle GPU PCIe address parameter
39  *
40  * @param [in]: Input parameter
41  * @config [out]: Program configuration context
42  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
43  */
44 static doca_error_t gpu_pci_address_callback(void *param, void *config)
45 {
46  struct gpu_dma_config *gpu_dma_cfg = (struct gpu_dma_config *)config;
47  char *gpu_pci_address = (char *)param;
48  int len;
49 
50  len = strnlen(gpu_pci_address, DOCA_DEVINFO_PCI_ADDR_SIZE);
52  DOCA_LOG_ERR("PCI address too long. Max %d", DOCA_DEVINFO_PCI_ADDR_SIZE - 1);
54  }
55 
56  strncpy(gpu_dma_cfg->gpu_pcie_addr, gpu_pci_address, len + 1);
57 
58  return DOCA_SUCCESS;
59 }
60 
61 /*
62  * ARGP Callback - Handle NIC PCIe address parameter
63  *
64  * @param [in]: Input parameter
65  * @config [out]: Program configuration context
66  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
67  */
68 static doca_error_t nic_pci_address_callback(void *param, void *config)
69 {
70  struct gpu_dma_config *gpu_dma_cfg = (struct gpu_dma_config *)config;
71  char *nic_pci_address = (char *)param;
72  int len;
73 
74  len = strnlen(nic_pci_address, DOCA_DEVINFO_PCI_ADDR_SIZE);
76  DOCA_LOG_ERR("PCI address too long. Max %d", DOCA_DEVINFO_PCI_ADDR_SIZE - 1);
78  }
79 
80  strncpy(gpu_dma_cfg->nic_pcie_addr, nic_pci_address, len + 1);
81 
82  return DOCA_SUCCESS;
83 }
84 
85 /*
86  * Register the command line parameters for the sample.
87  *
88  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
89  */
91 {
92  doca_error_t status;
93  struct doca_argp_param *nic_param;
94  struct doca_argp_param *gpu_param;
95 
96  status = doca_argp_param_create(&gpu_param);
97  if (status != DOCA_SUCCESS) {
98  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(status));
99  return status;
100  }
101 
102  doca_argp_param_set_short_name(gpu_param, "g");
103  doca_argp_param_set_long_name(gpu_param, "gpu");
104  doca_argp_param_set_arguments(gpu_param, "<GPU PCIe address>");
105  doca_argp_param_set_description(gpu_param, "GPU PCIe address to be used by the sample");
109  status = doca_argp_register_param(gpu_param);
110  if (status != DOCA_SUCCESS) {
111  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(status));
112  return status;
113  }
114 
115  status = doca_argp_param_create(&nic_param);
116  if (status != DOCA_SUCCESS) {
117  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(status));
118  return status;
119  }
120 
121  doca_argp_param_set_short_name(nic_param, "n");
122  doca_argp_param_set_long_name(nic_param, "nic");
123  doca_argp_param_set_arguments(nic_param, "<NIC PCIe address>");
124  doca_argp_param_set_description(nic_param, "NIC PCIe address to be used by the sample");
128  status = doca_argp_register_param(nic_param);
129  if (status != DOCA_SUCCESS) {
130  DOCA_LOG_ERR("Failed to register ARGP param: %s", doca_error_get_descr(status));
131  return status;
132  }
133 
134  return DOCA_SUCCESS;
135 }
136 
137 /*
138  * Sample main function
139  *
140  * @argc [in]: command line arguments size
141  * @argv [in]: array of command line arguments
142  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
143  */
144 int main(int argc, char **argv)
145 {
146  doca_error_t status;
147  struct gpu_dma_config cfg = {0};
148  struct doca_log_backend *sdk_log;
149  int exit_status = EXIT_FAILURE;
150 
151  /* Register a logger backend */
153  if (status != DOCA_SUCCESS)
154  goto sample_exit;
155 
156  /* Register a logger backend for internal SDK errors and warnings */
157  status = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
158  if (status != DOCA_SUCCESS)
159  goto sample_exit;
161  if (status != DOCA_SUCCESS)
162  goto sample_exit;
163 
164  DOCA_LOG_INFO("Starting the sample");
165 
166  /* Initialize argparser */
167  status = doca_argp_init(NULL, &cfg);
168  if (status != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(status));
170  goto sample_exit;
171  }
172 
174  if (status != DOCA_SUCCESS) {
175  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(status));
176  goto argp_cleanup;
177  }
178 
179  status = doca_argp_start(argc, argv);
180  if (status != DOCA_SUCCESS) {
181  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(status));
182  goto argp_cleanup;
183  }
184 
185  status = gpunetio_dma_memcpy(&cfg);
186  if (status != DOCA_SUCCESS) {
187  DOCA_LOG_ERR("gpunetio_dma_memcpy() encountered an error: %s", doca_error_get_descr(status));
188  goto argp_cleanup;
189  }
190 
191  exit_status = EXIT_SUCCESS;
192 
193 argp_cleanup:
195 sample_exit:
196  if (exit_status == EXIT_SUCCESS)
197  DOCA_LOG_INFO("Sample finished successfully");
198  else
199  DOCA_LOG_INFO("Sample finished with errors");
200  return exit_status;
201 }
#define NULL
Definition: __stddef_null.h:26
uint64_t len
doca_error_t gpunetio_dma_memcpy(struct gpu_dma_config *gpu_dma_cfg)
int main(int argc, char **argv)
static doca_error_t nic_pci_address_callback(void *param, void *config)
DOCA_LOG_REGISTER(GPU_DMA_MEMCPY::MAIN)
static doca_error_t register_gpu_dma_memcpy_sample_params(void)
static doca_error_t gpu_pci_address_callback(void *param, void *config)
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 void doca_argp_param_set_arguments(struct doca_argp_param *param, const char *arguments)
Set the description of the expected arguments of the program param, used during program usage.
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 void doca_argp_param_set_mandatory(struct doca_argp_param *param)
Mark the program param as mandatory.
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_WARNING
Definition: doca_log.h:47
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
char nic_pcie_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]
char gpu_pcie_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]