NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
dma_copy_dpu_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 <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <limits.h>
31 
32 #include <doca_buf.h>
33 #include <doca_buf_inventory.h>
34 #include <doca_ctx.h>
35 #include <doca_dev.h>
36 #include <doca_dma.h>
37 #include <doca_error.h>
38 #include <doca_log.h>
39 #include <doca_mmap.h>
40 #include <doca_pe.h>
41 
42 #include "dma_common.h"
43 
44 DOCA_LOG_REGISTER(DMA_COPY_DPU);
45 
46 #define SLEEP_IN_NANOS (10 * 1000) /* Sample the task every 10 microseconds */
47 #define RECV_BUF_SIZE (512) /* Buffer which contains config information */
48 
49 /*
50  * Saves export descriptor and buffer information content into memory buffers
51  *
52  * @export_desc_file_path [in]: Export descriptor file path
53  * @buffer_info_file_path [in]: Buffer information file path
54  * @export_desc [in]: Export descriptor buffer
55  * @export_desc_len [in]: Export descriptor buffer length
56  * @remote_addr [in]: Remote buffer address
57  * @remote_addr_len [in]: Remote buffer total length
58  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
59  */
60 static doca_error_t save_config_info_to_buffers(const char *export_desc_file_path,
61  const char *buffer_info_file_path,
62  char *export_desc,
63  size_t *export_desc_len,
64  char **remote_addr,
65  size_t *remote_addr_len)
66 {
67  FILE *fp;
68  long file_size;
69  char buffer[RECV_BUF_SIZE];
70  size_t convert_value;
71 
72  fp = fopen(export_desc_file_path, "r");
73  if (fp == NULL) {
74  DOCA_LOG_ERR("Failed to open %s", export_desc_file_path);
75  return DOCA_ERROR_IO_FAILED;
76  }
77 
78  if (fseek(fp, 0, SEEK_END) != 0) {
79  DOCA_LOG_ERR("Failed to calculate file size");
80  fclose(fp);
81  return DOCA_ERROR_IO_FAILED;
82  }
83 
84  file_size = ftell(fp);
85  if (file_size == -1) {
86  DOCA_LOG_ERR("Failed to calculate file size");
87  fclose(fp);
88  return DOCA_ERROR_IO_FAILED;
89  }
90 
91  if (file_size > RECV_BUF_SIZE)
92  file_size = RECV_BUF_SIZE;
93 
94  *export_desc_len = file_size;
95 
96  if (fseek(fp, 0L, SEEK_SET) != 0) {
97  DOCA_LOG_ERR("Failed to calculate file size");
98  fclose(fp);
99  return DOCA_ERROR_IO_FAILED;
100  }
101 
102  if (fread(export_desc, 1, file_size, fp) != (size_t)file_size) {
103  DOCA_LOG_ERR("Failed to allocate memory for source buffer");
104  fclose(fp);
105  return DOCA_ERROR_IO_FAILED;
106  }
107 
108  fclose(fp);
109 
110  /* Read source buffer information from file */
111  fp = fopen(buffer_info_file_path, "r");
112  if (fp == NULL) {
113  DOCA_LOG_ERR("Failed to open %s", buffer_info_file_path);
114  return DOCA_ERROR_IO_FAILED;
115  }
116 
117  /* Get source buffer address */
118  if (fgets(buffer, RECV_BUF_SIZE, fp) == NULL) {
119  DOCA_LOG_ERR("Failed to read the source (host) buffer address");
120  fclose(fp);
121  return DOCA_ERROR_IO_FAILED;
122  }
123  convert_value = strtoull(buffer, NULL, 0);
124  if (convert_value == ULLONG_MAX) {
125  DOCA_LOG_ERR("Failed to read the source (host) buffer address. Data is corrupted");
126  fclose(fp);
127  return DOCA_ERROR_IO_FAILED;
128  }
129  *remote_addr = (char *)convert_value;
130 
131  memset(buffer, 0, RECV_BUF_SIZE);
132 
133  /* Get source buffer length */
134  if (fgets(buffer, RECV_BUF_SIZE, fp) == NULL) {
135  DOCA_LOG_ERR("Failed to read the source (host) buffer length");
136  fclose(fp);
137  return DOCA_ERROR_IO_FAILED;
138  }
139  convert_value = strtoull(buffer, NULL, 0);
140  if (convert_value == ULLONG_MAX) {
141  DOCA_LOG_ERR("Failed to read the source (host) buffer length. Data is corrupted");
142  fclose(fp);
143  return DOCA_ERROR_IO_FAILED;
144  }
145  *remote_addr_len = convert_value;
146 
147  fclose(fp);
148 
149  return DOCA_SUCCESS;
150 }
151 
152 /*
153  * Run DOCA DMA DPU copy sample
154  *
155  * @export_desc_file_path [in]: Export descriptor file path
156  * @buffer_info_file_path [in]: Buffer info file path
157  * @pcie_addr [in]: Device PCI address
158  * @num_src_buf [in]: Number of source doca_buf to allocate
159  * @num_dst_buf [in]: Number of destination doca_buf to allocate
160  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
161  */
162 doca_error_t dma_copy_dpu(char *export_desc_file_path,
163  char *buffer_info_file_path,
164  const char *pcie_addr,
165  int num_src_buf,
166  int num_dst_buf)
167 {
168  struct dma_resources resources;
169  struct program_core_objects *state = &resources.state;
170  struct doca_dma_task_memcpy *dma_task = NULL;
171  struct doca_task *task = NULL;
172  union doca_data task_user_data = {0};
173  struct doca_buf *src_doca_buf = NULL;
174  struct doca_buf *dst_doca_buf = NULL;
175  struct doca_mmap *remote_mmap = NULL;
176  char export_desc[1024] = {0};
177  char *remote_addr = NULL;
178  char *dpu_buffer;
179  size_t dst_buffer_size, remote_addr_len = 0, export_desc_len = 0;
180  size_t max_buffer_size;
181  struct timespec ts = {
182  .tv_sec = 0,
183  .tv_nsec = SLEEP_IN_NANOS,
184  };
185  doca_error_t result, tmp_result, task_result;
186 
187  /* Allocate resources */
188  result = allocate_dma_resources(pcie_addr, num_src_buf + num_dst_buf, &resources);
189  if (result != DOCA_SUCCESS) {
190  DOCA_LOG_ERR("Failed to allocate DMA resources: %s", doca_error_get_descr(result));
191  return result;
192  }
193 
194  /* Connect context to progress engine */
195  result = doca_pe_connect_ctx(state->pe, state->ctx);
196  if (result != DOCA_SUCCESS) {
197  DOCA_LOG_ERR("Failed to connect progress engine to context: %s", doca_error_get_descr(result));
198  goto destroy_resources;
199  }
200 
201  result = doca_ctx_start(state->ctx);
202  if (result != DOCA_SUCCESS) {
203  DOCA_LOG_ERR("Failed to start context: %s", doca_error_get_descr(result));
204  goto destroy_resources;
205  }
206 
207  /* Get maximum buffer size allowed */
209  if (result != DOCA_SUCCESS) {
210  DOCA_LOG_ERR("Failed to get max buffer size: %s", doca_error_get_descr(result));
211  goto stop_dma;
212  }
213 
214  /* Copy all relevant information into local buffers */
215  result = save_config_info_to_buffers(export_desc_file_path,
216  buffer_info_file_path,
217  export_desc,
218  &export_desc_len,
219  &remote_addr,
220  &remote_addr_len);
221  if (result != DOCA_SUCCESS) {
222  DOCA_LOG_ERR("Failed to read memory configuration from file: %s", doca_error_get_descr(result));
223  goto stop_dma;
224  }
225 
226  if (remote_addr_len == 0) {
228  DOCA_LOG_ERR("Remote buffer from host is zero: failed to parse the buffer information file");
229  goto stop_dma;
230  } else if (remote_addr_len > max_buffer_size) {
232  DOCA_LOG_ERR("Remote buffer from Host exceeds max allowed buffer: %zu > %zu",
233  remote_addr_len,
234  max_buffer_size);
235  goto stop_dma;
236  }
237 
238  /* Copy the entire host buffer */
239  dst_buffer_size = remote_addr_len;
240  dpu_buffer = (char *)malloc(dst_buffer_size);
241  if (dpu_buffer == NULL) {
243  DOCA_LOG_ERR("Failed to allocate buffer memory: %s", doca_error_get_descr(result));
244  goto stop_dma;
245  }
246 
247  result = doca_mmap_set_memrange(state->dst_mmap, dpu_buffer, dst_buffer_size);
248  if (result != DOCA_SUCCESS) {
249  DOCA_LOG_ERR("Failed to set memory range for destination mmap: %s", doca_error_get_descr(result));
250  goto free_dpu_buffer;
251  }
252 
253  result = doca_mmap_start(state->dst_mmap);
254  if (result != DOCA_SUCCESS) {
255  DOCA_LOG_ERR("Failed to start destination mmap: %s", doca_error_get_descr(result));
256  goto free_dpu_buffer;
257  }
258 
259  /* Create a local DOCA mmap from exported data */
260  result =
261  doca_mmap_create_from_export(NULL, (const void *)export_desc, export_desc_len, state->dev, &remote_mmap);
262  if (result != DOCA_SUCCESS) {
263  DOCA_LOG_ERR("Failed to create mmap from export: %s", doca_error_get_descr(result));
264  goto free_dpu_buffer;
265  }
266 
267  /* Construct DOCA buffer for each address range */
269  remote_mmap,
270  remote_addr,
271  remote_addr_len,
272  num_src_buf,
273  true,
274  &src_doca_buf);
275  if (result != DOCA_SUCCESS) {
276  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing remote buffer: %s",
278  goto destroy_remote_mmap;
279  }
280 
281  /* Construct DOCA buffer for each address range */
283  state->dst_mmap,
284  dpu_buffer,
285  dst_buffer_size,
286  num_dst_buf,
287  false,
288  &dst_doca_buf);
289  if (result != DOCA_SUCCESS) {
290  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing destination buffer: %s",
292  goto destroy_src_buf;
293  }
294 
295  /* Include result in user data of task to be used in the callbacks */
296  task_user_data.ptr = &task_result;
297 
298  /* Allocate and construct DMA task */
300  src_doca_buf,
301  dst_doca_buf,
302  task_user_data,
303  &dma_task);
304  if (result != DOCA_SUCCESS) {
305  DOCA_LOG_ERR("Failed to allocate DMA memcpy task: %s", doca_error_get_descr(result));
306  goto destroy_dst_buf;
307  }
308  /* Number of tasks submitted to progress engine */
310 
311  task = doca_dma_task_memcpy_as_task(dma_task);
312 
313  /* Submit DMA task */
314  result = doca_task_submit(task);
315  if (result != DOCA_SUCCESS) {
316  DOCA_LOG_ERR("Failed to submit DMA task: %s", doca_error_get_descr(result));
317  doca_task_free(task);
318  goto destroy_dst_buf;
319  }
320 
321  resources.run_pe_progress = true;
322 
323  /* Wait for all tasks to be completed and context stopped */
324  while (resources.run_pe_progress) {
325  if (doca_pe_progress(state->pe) == 0)
326  nanosleep(&ts, &ts);
327  }
328 
329  /* Check result of task according to the result we update in the callbacks */
330  if (task_result == DOCA_SUCCESS) {
331  DOCA_LOG_INFO("Remote DMA copy was done Successfully");
332  dpu_buffer[dst_buffer_size - 1] = '\0';
333  DOCA_LOG_INFO("Memory content: %s", dpu_buffer);
334  } else
335  DOCA_LOG_ERR("DMA memcpy task failed: %s", doca_error_get_descr(task_result));
336 
337  result = task_result;
338 
339  /* Inform host that DMA operation is done */
340  DOCA_LOG_INFO("Host sample can be closed, DMA copy ended");
341 
342 destroy_dst_buf:
343  tmp_result = doca_buf_dec_refcount(dst_doca_buf, NULL);
344  if (tmp_result != DOCA_SUCCESS) {
345  DOCA_ERROR_PROPAGATE(result, tmp_result);
346  DOCA_LOG_ERR("Failed to decrease DOCA destination buffer reference count: %s",
347  doca_error_get_descr(tmp_result));
348  }
349 destroy_src_buf:
350  tmp_result = doca_buf_dec_refcount(src_doca_buf, NULL);
351  if (tmp_result != DOCA_SUCCESS) {
352  DOCA_ERROR_PROPAGATE(result, tmp_result);
353  DOCA_LOG_ERR("Failed to decrease DOCA source buffer reference count: %s",
354  doca_error_get_descr(tmp_result));
355  }
356 destroy_remote_mmap:
357  tmp_result = doca_mmap_destroy(remote_mmap);
358  if (tmp_result != DOCA_SUCCESS) {
359  DOCA_ERROR_PROPAGATE(result, tmp_result);
360  DOCA_LOG_ERR("Failed to decrease remote mmap: %s", doca_error_get_descr(tmp_result));
361  }
362 free_dpu_buffer:
363  free(dpu_buffer);
364 stop_dma:
365  tmp_result = doca_ctx_stop(state->ctx);
366  if (tmp_result != DOCA_SUCCESS) {
367  DOCA_ERROR_PROPAGATE(result, tmp_result);
368  DOCA_LOG_ERR("Unable to stop context: %s", doca_error_get_descr(tmp_result));
369  }
370  state->ctx = NULL;
371 destroy_resources:
372  tmp_result = destroy_dma_resources(&resources);
373  if (tmp_result != DOCA_SUCCESS) {
374  DOCA_ERROR_PROPAGATE(result, tmp_result);
375  DOCA_LOG_ERR("Failed to destroy DMA resources: %s", doca_error_get_descr(tmp_result));
376  }
377 
378  return result;
379 }
#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_error_t dma_copy_dpu(char *export_desc_file_path, char *buffer_info_file_path, const char *pcie_addr, int num_src_buf, int num_dst_buf)
DOCA_LOG_REGISTER(DMA_COPY_DPU)
static doca_error_t save_config_info_to_buffers(const char *export_desc_file_path, const char *buffer_info_file_path, char *export_desc, size_t *export_desc_len, char **remote_addr, size_t *remote_addr_len)
#define RECV_BUF_SIZE
#define SLEEP_IN_NANOS
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_devinfo * doca_dev_as_devinfo(const struct doca_dev *dev)
Get local device info from device. This should be useful when wanting to query information about devi...
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.
DOCA_STABLE doca_error_t doca_dma_cap_task_memcpy_get_max_buf_size(const struct doca_devinfo *devinfo, uint64_t *buf_size)
#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_TOO_BIG
Definition: doca_error.h:65
@ DOCA_ERROR_IO_FAILED
Definition: doca_error.h:55
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_NO_MEMORY
Definition: doca_error.h:45
@ DOCA_ERROR_EMPTY
Definition: doca_error.h:63
#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_destroy(struct doca_mmap *mmap)
Destroy DOCA Memory Map structure.
DOCA_STABLE doca_error_t doca_mmap_create_from_export(const union doca_data *user_data, const void *export_desc, size_t export_desc_len, struct doca_dev *dev, struct doca_mmap **mmap)
Creates a memory map object representing memory ranges in remote system memory space.
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_buf_inventory * buf_inv
Definition: common.h:49
struct doca_dev * dev
Definition: common.h:46
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