NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pe_task_resubmit_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 
30 #include <doca_mmap.h>
31 #include <doca_buf.h>
32 #include <doca_buf_inventory.h>
33 #include <doca_ctx.h>
34 #include <doca_dma.h>
35 #include <doca_types.h>
36 #include <doca_log.h>
37 #include <doca_pe.h>
38 
39 #include <samples/common.h>
40 #include "pe_common.h"
41 
42 DOCA_LOG_REGISTER(PE_TASK_RESUBMIT::SAMPLE);
43 
59 #define EXIT_ON_FAILURE(_expression_) \
60  { \
61  doca_error_t _status_ = _expression_; \
62 \
63  if (_status_ != DOCA_SUCCESS) { \
64  DOCA_LOG_ERR("%s failed with status %s", __func__, doca_error_get_descr(_status_)); \
65  return _status_; \
66  } \
67  }
68 
69 #define NUM_TASKS (4)
70 #define NUM_BUFFER_PAIRS (NUM_TASKS * 4)
71 #define DMA_BUFFER_SIZE (1024)
72 #define BUFFER_SIZE (DMA_BUFFER_SIZE * 2 * NUM_BUFFER_PAIRS)
73 #define BUF_INVENTORY_SIZE (NUM_BUFFER_PAIRS * 2)
74 
80 
81  struct doca_dma *dma;
82  struct doca_ctx *dma_ctx;
83  struct doca_dma_task_memcpy *tasks[NUM_TASKS];
84  uint32_t buff_pair_index;
85  struct doca_buf *src_buffers[NUM_BUFFER_PAIRS];
86  struct doca_buf *dst_buffers[NUM_BUFFER_PAIRS];
87 };
88 
89 /*
90  * Resubmit task
91  *
92  * @details This function resubmits a task. The function sets a new set of buffers every time that it is called,
93  * assuming that the old buffers were released.
94  *
95  * @state [in]: sample state
96  * @dma_task [in]: task to resubmit
97  */
98 void dma_task_resubmit(struct pe_task_resubmit_sample_state *state, struct doca_dma_task_memcpy *dma_task)
99 {
100  doca_error_t status = DOCA_SUCCESS;
101  struct doca_task *task = doca_dma_task_memcpy_as_task(dma_task);
102 
103  if (state->buff_pair_index < NUM_BUFFER_PAIRS) {
104  union doca_data user_data = {0};
105 
106  DOCA_LOG_INFO("Task %p resubmitting with buffers index %d", dma_task, state->buff_pair_index);
107 
108  /* Source buffer is filled with index + 1 that matches state->buff_pair_index + 1 */
109  user_data.u64 = (state->buff_pair_index + 1);
110  doca_task_set_user_data(task, user_data);
111 
112  doca_dma_task_memcpy_set_src(dma_task, state->src_buffers[state->buff_pair_index]);
113  doca_dma_task_memcpy_set_dst(dma_task, state->dst_buffers[state->buff_pair_index]);
114  state->buff_pair_index++;
115 
116  status = doca_task_submit(task);
117  if (status != DOCA_SUCCESS) {
118  DOCA_LOG_ERR("Failed to submit task with status %s",
120 
121  /* Program owns a task if it failed to submit (and has to free it eventually) */
122  (void)dma_task_free(dma_task);
123 
124  /* The method must increment num_completed_tasks because this task will never complete */
125  state->base.num_completed_tasks++;
126  }
127  } else
128  doca_task_free(task);
129 }
130 
131 /*
132  * DMA Memcpy task completed callback
133  *
134  * @dma_task [in]: Completed task
135  * @task_user_data [in]: doca_data from the task
136  * @ctx_user_data [in]: doca_data from the context
137  */
138 static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task,
139  union doca_data task_user_data,
140  union doca_data ctx_user_data)
141 {
142  uint8_t expected_value = (uint8_t)task_user_data.u64;
143  struct pe_task_resubmit_sample_state *state = (struct pe_task_resubmit_sample_state *)ctx_user_data.ptr;
144 
145  state->base.num_completed_tasks++;
146 
151  (void)process_completed_dma_memcpy_task(dma_task, expected_value);
152 
153  (void)free_dma_memcpy_task_buffers(dma_task);
154 
155  dma_task_resubmit(state, dma_task);
156 }
157 
158 /*
159  * Memcpy task error callback
160  *
161  * @dma_task [in]: failed task
162  * @task_user_data [in]: doca_data from the task
163  * @ctx_user_data [in]: doca_data from the context
164  */
165 static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task,
166  union doca_data task_user_data,
167  union doca_data ctx_user_data)
168 {
169  struct pe_task_resubmit_sample_state *state = (struct pe_task_resubmit_sample_state *)ctx_user_data.ptr;
170  struct doca_task *task = doca_dma_task_memcpy_as_task(dma_task);
171 
172  (void)task_user_data;
173 
174  /* This sample defines that a task is completed even if it is completed with error */
175  state->base.num_completed_tasks++;
176 
177  DOCA_LOG_ERR("Task failed with status %s", doca_error_get_descr(doca_task_get_status(task)));
178 
179  (void)free_dma_memcpy_task_buffers(dma_task);
180 
181  dma_task_resubmit(state, dma_task);
182 }
183 
191 {
192  union doca_data ctx_user_data = {0};
193 
194  DOCA_LOG_INFO("Creating DMA");
195 
196  EXIT_ON_FAILURE(doca_dma_create(state->base.device, &state->dma));
197  state->dma_ctx = doca_dma_as_ctx(state->dma);
198 
199  /* A context can only be connected to one PE (PE can run multiple contexts) */
201 
207  ctx_user_data.ptr = state;
208  EXIT_ON_FAILURE(doca_ctx_set_user_data(state->dma_ctx, ctx_user_data));
209 
213  NUM_TASKS));
214 
215  return DOCA_SUCCESS;
216 }
217 
225 {
226  uint32_t i = 0;
227 
228  DOCA_LOG_INFO("Allocating doca buffers");
229 
230  for (i = 0; i < NUM_BUFFER_PAIRS; i++) {
231  /* Use doca_buf_inventory_buf_get_by_data to initialize the source buffer */
233  state->base.mmap,
234  state->base.available_buffer,
236  &state->src_buffers[i]));
237 
238  memset(state->base.available_buffer, (i + 1), DMA_BUFFER_SIZE);
240 
242  state->base.mmap,
243  state->base.available_buffer,
245  &state->dst_buffers[i]));
246 
247  memset(state->base.available_buffer, 0, DMA_BUFFER_SIZE);
249  }
250 
251  return DOCA_SUCCESS;
252 }
253 
262 {
263  uint32_t i = 0;
264 
265  DOCA_LOG_INFO("Allocating tasks");
266 
267  for (i = 0; i < NUM_TASKS; i++) {
268  union doca_data user_data = {0};
269 
270  user_data.u64 = (state->buff_pair_index + 1);
272  state->src_buffers[state->buff_pair_index],
273  state->dst_buffers[state->buff_pair_index],
274  user_data,
275  &state->tasks[i]));
276 
277  DOCA_LOG_INFO("Task %p allocated with buffers index %d", state->tasks[i], state->buff_pair_index);
278 
279  state->buff_pair_index++;
280  }
281 
282  return DOCA_SUCCESS;
283 }
284 
294 {
295  /* A context must be stopped before it is destroyed */
296  if (state->dma_ctx != NULL)
297  (void)doca_ctx_stop(state->dma_ctx);
298 
299  /* All contexts must be destroyed before PE is destroyed. Context destroy disconnects it from the PE */
300  if (state->dma != NULL)
301  (void)doca_dma_destroy(state->dma);
302 
303  pe_sample_base_cleanup(&state->base);
304 }
305 
315 {
316  memset(state, 0, sizeof(*state));
317 
318  state->base.buffer_size = BUFFER_SIZE;
320 
322  EXIT_ON_FAILURE(open_device(&state->base));
323  EXIT_ON_FAILURE(create_mmap(&state->base));
325  EXIT_ON_FAILURE(create_pe(&state->base));
326  EXIT_ON_FAILURE(create_dma(state));
332 
333  return DOCA_SUCCESS;
334 }
335 
342 {
343  struct pe_task_resubmit_sample_state state;
344  doca_error_t status = run(&state);
345 
346  cleanup(&state);
347 
348  return status;
349 }
#define NULL
Definition: __stddef_null.h:26
static doca_error_t allocate_buffer(struct cache_invalidate_sample_state *state)
static doca_error_t create_buf_inventory(struct cache_invalidate_sample_state *state)
static doca_error_t poll_for_completion(struct cache_invalidate_sample_state *state)
static doca_error_t create_pe(struct cache_invalidate_sample_state *state)
static doca_error_t create_mmap(struct doca_dev *doca_device, unsigned int mmap_permissions, void *memrange_addr, size_t memrange_len, struct doca_mmap **mmap, doca_dpa_dev_mmap_t *dpa_mmap_handle)
static doca_error_t doca_buf_inventory_buf_get_by_addr(struct doca_buf_inventory *inventory, struct doca_mmap *mmap, void *addr, size_t len, struct doca_buf **buf)
Allocate single element from buffer inventory and point it to the buffer defined by addr & len argume...
static doca_error_t doca_buf_inventory_buf_get_by_data(struct doca_buf_inventory *inventory, struct doca_mmap *mmap, void *data, size_t data_len, struct doca_buf **buf)
Allocate single element from buffer inventory and point it to the buffer defined by data & data_len a...
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_set_user_data(struct doca_ctx *ctx, union doca_data user_data)
set user data to context
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 void doca_dma_task_memcpy_set_dst(struct doca_dma_task_memcpy *task, struct doca_buf *dst)
This method sets destination buffer to memcpy 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 struct doca_ctx * doca_dma_as_ctx(struct doca_dma *dma)
DOCA_STABLE void doca_dma_task_memcpy_set_src(struct doca_dma_task_memcpy *task, const struct doca_buf *src)
This method sets source buffer to memcpy task.
DOCA_STABLE doca_error_t doca_dma_task_memcpy_set_conf(struct doca_dma *dma, doca_dma_task_memcpy_completion_cb_t task_completion_cb, doca_dma_task_memcpy_completion_cb_t task_error_cb, uint32_t num_memcpy_tasks)
This method sets the DMA memcpy tasks configuration.
DOCA_STABLE doca_error_t doca_dma_create(struct doca_dev *dev, struct doca_dma **dma)
DOCA_STABLE doca_error_t doca_dma_destroy(struct doca_dma *dma)
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
#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_task_get_status(const struct doca_task *task)
Get task status.
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 void doca_task_set_user_data(struct doca_task *task, union doca_data user_data)
Set user data to a task.
DOCA_STABLE void doca_task_free(struct doca_task *task)
Free a task back to where it was allocated from.
doca_dev * open_device(std::string const &identifier)
Definition: doca_utils.cpp:43
void pe_sample_base_cleanup(struct pe_sample_state_base *state)
Definition: pe_common.c:311
doca_error_t dma_task_free(struct doca_dma_task_memcpy *dma_task)
Definition: pe_common.c:108
doca_error_t free_dma_memcpy_task_buffers(struct doca_dma_task_memcpy *dma_task)
Definition: pe_common.c:93
doca_error_t submit_dma_tasks(uint32_t num_tasks, struct doca_dma_task_memcpy **tasks)
Definition: pe_common.c:211
doca_error_t process_completed_dma_memcpy_task(struct doca_dma_task_memcpy *dma_task, uint8_t expected_value)
Definition: pe_common.c:66
doca_error_t allocate_doca_bufs(struct pe_task_resubmit_sample_state *state)
#define EXIT_ON_FAILURE(_expression_)
doca_error_t create_dma(struct pe_task_resubmit_sample_state *state)
doca_error_t run(struct pe_task_resubmit_sample_state *state)
void cleanup(struct pe_task_resubmit_sample_state *state)
#define BUF_INVENTORY_SIZE
doca_error_t run_pe_task_resubmit_sample(void)
doca_error_t allocate_tasks_for_resubmit(struct pe_task_resubmit_sample_state *state)
void dma_task_resubmit(struct pe_task_resubmit_sample_state *state, struct doca_dma_task_memcpy *dma_task)
#define BUFFER_SIZE
static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task, union doca_data task_user_data, union doca_data ctx_user_data)
#define NUM_BUFFER_PAIRS
#define NUM_TASKS
static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task, union doca_data task_user_data, union doca_data ctx_user_data)
DOCA_LOG_REGISTER(PE_TASK_RESUBMIT::SAMPLE)
#define DMA_BUFFER_SIZE
uint8_t * available_buffer
Definition: pe_common.h:53
struct doca_dev * device
Definition: pe_common.h:39
size_t buf_inventory_size
Definition: pe_common.h:45
struct doca_buf_inventory * inventory
Definition: pe_common.h:41
struct doca_mmap * mmap
Definition: pe_common.h:40
struct doca_pe * pe
Definition: pe_common.h:42
uint32_t num_completed_tasks
Definition: pe_common.h:56
struct pe_sample_state_base base
struct doca_dma_task_memcpy * tasks[NUM_TASKS]
struct doca_buf * dst_buffers[NUM_BUFFER_PAIRS]
struct doca_buf * src_buffers[NUM_BUFFER_PAIRS]
Convenience type for representing opaque data.
Definition: doca_types.h:56
uint64_t u64
Definition: doca_types.h:58
void * ptr
Definition: doca_types.h:57