NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
dma_local_copy_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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 <stdint.h>
27 #include <string.h>
28 #include <time.h>
29 
30 #include <doca_buf.h>
31 #include <doca_buf_inventory.h>
32 #include <doca_ctx.h>
33 #include <doca_dma.h>
34 #include <doca_error.h>
35 #include <doca_log.h>
36 #include <doca_mmap.h>
37 #include <doca_pe.h>
38 
39 #include "dma_common.h"
40 
41 DOCA_LOG_REGISTER(DPU_LOCAL_DMA_COPY);
42 
43 #define SLEEP_IN_NANOS (10 * 1000) /* Sample the task every 10 microseconds */
44 
45 /*
46  * Checks that the two buffers are not overlap each other
47  *
48  * @dst_buffer [in]: Destination buffer
49  * @src_buffer [in]: Source buffer
50  * @length [in]: Length of both buffers
51  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
52  */
53 static doca_error_t memory_ranges_overlap(const char *dst_buffer, const char *src_buffer, size_t length)
54 {
55  const char *dst_range_end = dst_buffer + length;
56  const char *src_range_end = src_buffer + length;
57 
58  if (((dst_buffer >= src_buffer) && (dst_buffer < src_range_end)) ||
59  ((src_buffer >= dst_buffer) && (src_buffer < dst_range_end))) {
61  }
62 
63  return DOCA_SUCCESS;
64 }
65 
66 /*
67  * Register buffer with mmap and start it
68  *
69  * @mmap [in]: Memory Map object
70  * @buffer [in]: Buffer
71  * @length [in]: Buffer's size
72  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
73  */
74 static doca_error_t register_memory_range_and_start_mmap(struct doca_mmap *mmap, char *buffer, size_t length)
75 {
77 
79  if (result != DOCA_SUCCESS)
80  return result;
81 
82  return doca_mmap_start(mmap);
83 }
84 
85 /*
86  * Run DOCA DMA local copy sample
87  *
88  * @pcie_addr [in]: Device PCI address
89  * @dst_buffer [in]: Destination buffer
90  * @src_buffer [in]: Source buffer to copy
91  * @length [in]: Buffer's size
92  * @num_src_buf [in]: Number of source doca_buf to allocate
93  * @num_dst_buf [in]: Number of destination doca_buf to allocate
94  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
95  */
96 doca_error_t dma_local_copy(const char *pcie_addr,
97  char *dst_buffer,
98  char *src_buffer,
99  size_t length,
100  int num_src_buf,
101  int num_dst_buf)
102 {
103  struct dma_resources resources;
104  struct program_core_objects *state = &resources.state;
105  struct doca_dma_task_memcpy *dma_task = NULL;
106  struct doca_task *task = NULL;
107  union doca_data task_user_data = {0};
108  struct doca_buf *src_doca_buf = NULL;
109  struct doca_buf *dst_doca_buf = NULL;
110  struct timespec ts = {
111  .tv_sec = 0,
112  .tv_nsec = SLEEP_IN_NANOS,
113  };
114  doca_error_t result, tmp_result, task_result;
115 
116  if (dst_buffer == NULL || src_buffer == NULL || length == 0) {
117  DOCA_LOG_ERR("Invalid input values, addresses and sizes must not be 0");
119  }
120 
121  result = memory_ranges_overlap(dst_buffer, src_buffer, length);
122  if (result != DOCA_SUCCESS) {
123  DOCA_LOG_ERR("Memory ranges must not overlap");
124  return result;
125  }
126 
127  /* Allocate resources */
128  result = allocate_dma_resources(pcie_addr, num_src_buf + num_dst_buf, &resources);
129  if (result != DOCA_SUCCESS) {
130  DOCA_LOG_ERR("Failed to allocate DMA resources: %s", doca_error_get_descr(result));
131  return result;
132  }
133 
134  /* Connect context to progress engine */
135  result = doca_pe_connect_ctx(state->pe, state->ctx);
136  if (result != DOCA_SUCCESS) {
137  DOCA_LOG_ERR("Failed to connect progress engine to context: %s", doca_error_get_descr(result));
138  goto destroy_resources;
139  }
140 
141  result = doca_ctx_start(state->ctx);
142  if (result != DOCA_SUCCESS) {
143  DOCA_LOG_ERR("Failed to start context: %s", doca_error_get_descr(result));
144  goto destroy_resources;
145  }
146 
148  if (result != DOCA_SUCCESS) {
149  DOCA_LOG_ERR("Failed to create and start source mmap: %s", doca_error_get_descr(result));
150  goto stop_dma;
151  }
152 
154  if (result != DOCA_SUCCESS) {
155  DOCA_LOG_ERR("Failed to create and start destination mmap: %s", doca_error_get_descr(result));
156  goto stop_dma;
157  }
158 
159  /* Clear destination memory buffer */
160  memset(dst_buffer, 0, length);
161 
162  /* Construct DOCA buffer for each address range */
164  state->src_mmap,
165  src_buffer,
166  length,
167  num_src_buf,
168  true,
169  &src_doca_buf);
170  if (result != DOCA_SUCCESS) {
171  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing source buffer: %s",
173  goto stop_dma;
174  }
175 
176  /* Construct DOCA buffer for each address range */
178  state->dst_mmap,
179  dst_buffer,
180  length,
181  num_dst_buf,
182  false,
183  &dst_doca_buf);
184  if (result != DOCA_SUCCESS) {
185  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing destination buffer: %s",
187  goto destroy_src_buf;
188  }
189 
190  /* Include result in user data of task to be used in the callbacks */
191  task_user_data.ptr = &task_result;
192  /* Allocate and construct DMA task */
194  src_doca_buf,
195  dst_doca_buf,
196  task_user_data,
197  &dma_task);
198  if (result != DOCA_SUCCESS) {
199  DOCA_LOG_ERR("Failed to allocate DMA memcpy task: %s", doca_error_get_descr(result));
200  goto destroy_dst_buf;
201  }
202  /* Number of tasks submitted to progress engine */
204 
205  task = doca_dma_task_memcpy_as_task(dma_task);
206 
207  /* Submit DMA task */
208  result = doca_task_submit(task);
209  if (result != DOCA_SUCCESS) {
210  DOCA_LOG_ERR("Failed to submit DMA task: %s", doca_error_get_descr(result));
211  doca_task_free(task);
212  goto destroy_dst_buf;
213  }
214 
215  resources.run_pe_progress = true;
216 
217  /* Wait for all tasks to be completed and context stopped */
218  while (resources.run_pe_progress) {
219  if (doca_pe_progress(state->pe) == 0)
220  nanosleep(&ts, &ts);
221  }
222 
223  /* Check result of task according to the result we update in the callbacks */
224  if (task_result == DOCA_SUCCESS)
225  DOCA_LOG_INFO("Success, memory copied and verified as correct");
226  else
227  DOCA_LOG_ERR("DMA memcpy task failed: %s", doca_error_get_descr(task_result));
228 
229  result = task_result;
230 
231 destroy_dst_buf:
232  tmp_result = doca_buf_dec_refcount(dst_doca_buf, NULL);
233  if (tmp_result != DOCA_SUCCESS) {
234  DOCA_ERROR_PROPAGATE(result, tmp_result);
235  DOCA_LOG_ERR("Failed to decrease DOCA destination buffer reference count: %s",
236  doca_error_get_descr(tmp_result));
237  }
238 destroy_src_buf:
239  tmp_result = doca_buf_dec_refcount(src_doca_buf, NULL);
240  if (tmp_result != DOCA_SUCCESS) {
241  DOCA_ERROR_PROPAGATE(result, tmp_result);
242  DOCA_LOG_ERR("Failed to decrease DOCA source buffer reference count: %s",
243  doca_error_get_descr(tmp_result));
244  }
245 stop_dma:
246  tmp_result = doca_ctx_stop(state->ctx);
247  if (tmp_result != DOCA_SUCCESS) {
248  DOCA_ERROR_PROPAGATE(result, tmp_result);
249  DOCA_LOG_ERR("Unable to stop context: %s", doca_error_get_descr(tmp_result));
250  }
251  state->ctx = NULL;
252 destroy_resources:
253  tmp_result = destroy_dma_resources(&resources);
254  if (tmp_result != DOCA_SUCCESS) {
255  DOCA_ERROR_PROPAGATE(result, tmp_result);
256  DOCA_LOG_ERR("Failed to destroy DMA resources: %s", doca_error_get_descr(tmp_result));
257  }
258 
259  return result;
260 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t allocat_doca_buf_list(struct doca_buf_inventory *buf_inv, struct doca_mmap *mmap, void *buf_addr, size_t buf_len, int num_buf, bool set_data_pos, struct doca_buf **dbuf)
Definition: common.c:527
doca_error_t destroy_dma_resources(struct dma_resources *resources)
Definition: dma_common.c:513
doca_error_t allocate_dma_resources(const char *pcie_addr, int num_buf, struct dma_resources *resources)
Definition: dma_common.c:427
DOCA_LOG_REGISTER(DPU_LOCAL_DMA_COPY)
static doca_error_t memory_ranges_overlap(const char *dst_buffer, const char *src_buffer, size_t length)
static doca_error_t register_memory_range_and_start_mmap(struct doca_mmap *mmap, char *buffer, size_t length)
doca_error_t dma_local_copy(const char *pcie_addr, char *dst_buffer, char *src_buffer, size_t length, int num_src_buf, int num_dst_buf)
#define SLEEP_IN_NANOS
doca_dpa_dev_mmap_t mmap
struct rdma_resources resources
DOCA_STABLE doca_error_t doca_buf_dec_refcount(struct doca_buf *buf, uint16_t *refcount)
Decrease the object reference count by 1, if 0 reached, return the element back to the inventory.
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
DOCA_STABLE struct doca_task * doca_dma_task_memcpy_as_task(struct doca_dma_task_memcpy *task)
This method converts a memcpy task to doca_task.
DOCA_STABLE doca_error_t doca_dma_task_memcpy_alloc_init(struct doca_dma *dma, const struct doca_buf *src, struct doca_buf *dst, union doca_data user_data, struct doca_dma_task_memcpy **task)
This method allocates and initializes a DMA memcpy task.
#define DOCA_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
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
#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_STABLE doca_error_t doca_mmap_set_memrange(struct doca_mmap *mmap, void *addr, size_t len)
Set the memory range of DOCA memory map.
DOCA_STABLE doca_error_t doca_mmap_start(struct doca_mmap *mmap)
Start DOCA Memory Map.
DOCA_STABLE doca_error_t doca_pe_connect_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
This method connects a context to a progress engine.
DOCA_STABLE doca_error_t doca_task_submit(struct doca_task *task)
Submit a task to a progress engine.
DOCA_STABLE uint8_t doca_pe_progress(struct doca_pe *pe)
Run the progress engine.
DOCA_STABLE void doca_task_free(struct doca_task *task)
Free a task back to where it was allocated from.
struct doca_pe * pe
Definition: common.h:51
struct doca_mmap * src_mmap
Definition: common.h:47
struct doca_buf_inventory * buf_inv
Definition: common.h:49
struct doca_mmap * dst_mmap
Definition: common.h:48
struct doca_ctx * ctx
Definition: common.h:50
size_t num_remaining_tasks
Definition: rdma_common.h:134
bool run_pe_progress
Definition: rdma_common.h:133
Convenience type for representing opaque data.
Definition: doca_types.h:56
void * ptr
Definition: doca_types.h:57