NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
dma_local_copy_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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 "dma_common.h"
35 
36 DOCA_LOG_REGISTER(DPU_LOCAL_DMA_COPY::MAIN);
37 
38 /* Sample's Logic */
39 doca_error_t dma_local_copy(const char *pcie_addr,
40  char *dst_buffer,
41  const char *src_buffer,
42  size_t length,
43  int num_src_buf,
44  int num_dst_buf);
45 
46 /*
47  * Sample main function
48  *
49  * @argc [in]: command line arguments size
50  * @argv [in]: array of command line arguments
51  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
52  */
53 int main(int argc, char **argv)
54 {
55  struct dma_config dma_conf;
56  char *dst_buffer = NULL, *src_buffer = NULL;
57  size_t length;
59  struct doca_log_backend *sdk_log;
60  int exit_status = EXIT_FAILURE;
61 
62  /* Set the default configuration values (Example values) */
63  strcpy(dma_conf.pci_address, "03:00.0");
64  strcpy(dma_conf.cpy_txt, "This is a sample piece of text");
65  dma_conf.num_dst_buf = 1;
66  dma_conf.num_src_buf = 1;
67  /* No need to set export_desc_path and buf_info_path which are only needed for DMA across devices */
68 
69  /* Register a logger backend */
71  if (result != DOCA_SUCCESS)
72  goto sample_exit;
73 
74  /* Register a logger backend for internal SDK errors and warnings */
76  if (result != DOCA_SUCCESS)
77  goto sample_exit;
79  if (result != DOCA_SUCCESS)
80  goto sample_exit;
81 
82  DOCA_LOG_INFO("Starting the sample");
83 
84 #ifndef DOCA_ARCH_DPU
85  DOCA_LOG_ERR("Local DMA copy can run only on the DPU");
86  goto sample_exit;
87 #endif
88 
89  result = doca_argp_init(NULL, &dma_conf);
90  if (result != DOCA_SUCCESS) {
91  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
92  goto sample_exit;
93  }
94  result = register_dma_params(false);
95  if (result != DOCA_SUCCESS) {
96  DOCA_LOG_ERR("Failed to register DMA sample parameters: %s", doca_error_get_descr(result));
97  goto argp_cleanup;
98  }
99  result = doca_argp_start(argc, argv);
100  if (result != DOCA_SUCCESS) {
101  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
102  goto argp_cleanup;
103  }
104 
105  length = strlen(dma_conf.cpy_txt) + 1;
106  dst_buffer = (char *)calloc(1, length);
107  if (dst_buffer == NULL) {
108  DOCA_LOG_ERR("Destination buffer allocation failed");
109  goto argp_cleanup;
110  }
111 
112  src_buffer = (char *)malloc(length);
113  if (src_buffer == NULL) {
114  DOCA_LOG_ERR("Source buffer allocation failed");
115  goto dst_buffer_cleanup;
116  }
117 
118  memcpy(src_buffer, dma_conf.cpy_txt, length);
119 
120  result = dma_local_copy(dma_conf.pci_address,
121  dst_buffer,
122  src_buffer,
123  length,
124  dma_conf.num_src_buf,
125  dma_conf.num_dst_buf);
126  if (result != DOCA_SUCCESS) {
127  DOCA_LOG_ERR("dma_local_copy() encountered an error: %s", doca_error_get_descr(result));
128  goto src_buffer_cleanup;
129  }
130 
131  exit_status = EXIT_SUCCESS;
132 
133 src_buffer_cleanup:
134  if (src_buffer != NULL)
135  free(src_buffer);
136 dst_buffer_cleanup:
137  if (dst_buffer != NULL)
138  free(dst_buffer);
139 argp_cleanup:
141 sample_exit:
142  if (exit_status == EXIT_SUCCESS)
143  DOCA_LOG_INFO("Sample finished successfully");
144  else
145  DOCA_LOG_INFO("Sample finished with errors");
146  return exit_status;
147 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t register_dma_params(bool is_remote)
Definition: dma_common.c:204
doca_error_t dma_local_copy(const char *pcie_addr, char *dst_buffer, const char *src_buffer, size_t length, int num_src_buf, int num_dst_buf)
int main(int argc, char **argv)
DOCA_LOG_REGISTER(DPU_LOCAL_DMA_COPY::MAIN)
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.
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_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
int num_dst_buf
Definition: dma_common.h:51
char cpy_txt[MAX_TXT_SIZE]
Definition: dma_common.h:47
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]
Definition: dma_common.h:46
int num_src_buf
Definition: dma_common.h:50