NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pe_task_error_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 <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 
31 #include <doca_mmap.h>
32 #include <doca_buf.h>
33 #include <doca_buf_inventory.h>
34 #include <doca_ctx.h>
35 #include <doca_dma.h>
36 #include <doca_types.h>
37 #include <doca_log.h>
38 #include <doca_pe.h>
39 
40 #include <samples/common.h>
41 #include "pe_common.h"
42 
43 DOCA_LOG_REGISTER(PE_TASK_ERROR::SAMPLE);
44 
58 #define EXIT_ON_FAILURE(_expression_) \
59  { \
60  doca_error_t _status_ = _expression_; \
61 \
62  if (_status_ != DOCA_SUCCESS) { \
63  DOCA_LOG_ERR("%s failed with status %s", __func__, doca_error_get_descr(_status_)); \
64  return _status_; \
65  } \
66  }
67 
68 #define NUM_TASKS (255)
69 #define DMA_BUFFER_SIZE (1024)
70 #define BUFFER_SIZE (DMA_BUFFER_SIZE * 2 * NUM_TASKS)
71 #define BUF_INVENTORY_SIZE (NUM_TASKS * 2)
72 
78 
79  struct doca_dma *dma;
80  struct doca_ctx *dma_ctx;
81  struct doca_dma_task_memcpy *tasks[NUM_TASKS];
84 };
85 
92 static const char *ctx_state_to_string(enum doca_ctx_states state)
93 {
94  switch (state) {
96  return "idle";
98  return "starting";
100  return "running";
102  return "stopping";
103  default:
104  return "unknown";
105  }
106 }
107 
116 static void dma_state_changed_cb(const union doca_data ctx_user_data,
117  struct doca_ctx *ctx,
118  enum doca_ctx_states prev_state,
119  enum doca_ctx_states next_state)
120 {
121  struct pe_task_error_sample_state *state = (struct pe_task_error_sample_state *)ctx_user_data.ptr;
122 
131  DOCA_LOG_INFO("CTX %p state changed from %s to %s",
132  ctx,
133  ctx_state_to_string(prev_state),
134  ctx_state_to_string(next_state));
135 
136  if (next_state == DOCA_CTX_STATE_STOPPING) {
137  if (!state->dma_stop_called)
138  DOCA_LOG_INFO("CTX moved to stopping state unexpectedly");
139  }
140 
141  if ((prev_state == DOCA_CTX_STATE_STOPPING) && (next_state == DOCA_CTX_STATE_IDLE))
142  state->dma_has_stopped = true;
143 }
144 
145 /*
146  * DMA Memcpy task completed callback
147  *
148  * @dma_task [in]: Completed task
149  * @task_user_data [in]: doca_data from the task
150  * @ctx_user_data [in]: doca_data from the context
151  */
152 static void dma_memcpy_completed_callback(struct doca_dma_task_memcpy *dma_task,
153  union doca_data task_user_data,
154  union doca_data ctx_user_data)
155 {
156  uint8_t expected_value = (uint8_t)task_user_data.u64;
157  struct pe_task_error_sample_state *state = (struct pe_task_error_sample_state *)ctx_user_data.ptr;
158 
159  state->base.num_completed_tasks++;
160 
161  DOCA_LOG_INFO("Task completed (expected value = %d)", expected_value);
162 
167  (void)process_completed_dma_memcpy_task(dma_task, expected_value);
168 
169  /* The task is no longer required, therefore it can be freed */
170  (void)dma_task_free(dma_task);
171 }
172 
173 /*
174  * Memcpy task error callback
175  *
176  * @dma_task [in]: failed task
177  * @task_user_data [in]: doca_data from the task
178  * @ctx_user_data [in]: doca_data from the context
179  */
180 static void dma_memcpy_error_callback(struct doca_dma_task_memcpy *dma_task,
181  union doca_data task_user_data,
182  union doca_data ctx_user_data)
183 {
184  struct pe_task_error_sample_state *state = (struct pe_task_error_sample_state *)ctx_user_data.ptr;
185  struct doca_task *task = doca_dma_task_memcpy_as_task(dma_task);
186  uint8_t expected_value = (uint8_t)task_user_data.u64;
187 
188  (void)task_user_data;
189 
190  /* This sample defines that a task is completed even if it is completed with error */
191  state->base.num_completed_tasks++;
192 
193  DOCA_LOG_ERR("Task failed with status %s (expected value = %d)",
195  expected_value);
196 
203  (void)dma_task_free(dma_task);
204 }
205 
213 {
214  union doca_data ctx_user_data = {0};
215 
216  DOCA_LOG_INFO("Creating DMA");
217 
218  EXIT_ON_FAILURE(doca_dma_create(state->base.device, &state->dma));
219  state->dma_ctx = doca_dma_as_ctx(state->dma);
220 
221  /* A context can only be connected to one PE (PE can run multiple contexts) */
223 
229  ctx_user_data.ptr = state;
230  EXIT_ON_FAILURE(doca_ctx_set_user_data(state->dma_ctx, ctx_user_data));
231 
235  NUM_TASKS));
236 
238 
239  return DOCA_SUCCESS;
240 }
241 
257  struct doca_dma *dma,
258  uint32_t num_tasks,
259  size_t dma_buffer_size,
260  struct doca_dma_task_memcpy **tasks)
261 {
262  uint32_t task_id = 0;
263 
264  DOCA_LOG_INFO("Allocating tasks");
265 
266  for (task_id = 0; task_id < num_tasks; task_id++) {
267  struct doca_buf *source = NULL;
268  struct doca_buf *destination = NULL;
269  union doca_data user_data = {0};
270 
271  /* User data will be used to verify copy content */
272  user_data.u64 = (task_id + 1);
273 
274  /* Use doca_buf_inventory_buf_get_by_data to initialize the source buffer */
276  state->mmap,
277  state->available_buffer,
278  dma_buffer_size,
279  &source));
280 
281  memset(state->available_buffer, (task_id + 1), dma_buffer_size);
282  state->available_buffer += dma_buffer_size;
283 
284  /* Setting the buf length to 0 will cause HW to fail */
285  if (task_id == 2)
286  doca_buf_reset_data_len(source);
287 
289  state->mmap,
290  state->available_buffer,
291  dma_buffer_size,
292  &destination));
293 
294  memset(state->available_buffer, 0, dma_buffer_size);
295  state->available_buffer += dma_buffer_size;
296 
297  EXIT_ON_FAILURE(doca_dma_task_memcpy_alloc_init(dma, source, destination, user_data, &tasks[task_id]));
298  }
299 
300  return DOCA_SUCCESS;
301 }
302 
312 {
313  size_t num_inflight_tasks = 0;
314 
315  EXIT_ON_FAILURE(doca_ctx_get_num_inflight_tasks(state->dma_ctx, &num_inflight_tasks));
316  while (num_inflight_tasks > 0) {
317  (void)doca_pe_progress(state->base.pe);
318  EXIT_ON_FAILURE(doca_ctx_get_num_inflight_tasks(state->dma_ctx, &num_inflight_tasks));
319  }
320 
326  while (!state->dma_has_stopped)
327  (void)doca_pe_progress(state->base.pe);
328 
329  return DOCA_SUCCESS;
330 }
331 
340 static void cleanup(struct pe_task_error_sample_state *state)
341 {
342  /* A context must be stopped before it is destroyed */
343  if (state->dma_ctx != NULL)
344  (void)doca_ctx_stop(state->dma_ctx);
345 
346  /* All contexts must be destroyed before PE is destroyed. Context destroy disconnects it from the PE */
347  if (state->dma != NULL)
348  (void)doca_dma_destroy(state->dma);
349 
350  pe_sample_base_cleanup(&state->base);
351 }
352 
362 {
363  memset(state, 0, sizeof(*state));
364 
365  state->base.buffer_size = BUFFER_SIZE;
367 
369  EXIT_ON_FAILURE(open_device(&state->base));
370  EXIT_ON_FAILURE(create_mmap(&state->base));
372  EXIT_ON_FAILURE(create_pe(&state->base));
373  EXIT_ON_FAILURE(create_dma(state));
379 
380  return DOCA_SUCCESS;
381 }
382 
389 {
390  struct pe_task_error_sample_state state;
391  doca_error_t status = run(&state);
392 
393  cleanup(&state);
394 
395  return status;
396 }
#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_buf_reset_data_len(struct doca_buf *buf)
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_state_changed_cb(struct doca_ctx *ctx, doca_ctx_state_changed_callback_t cb)
Set state changed callback.
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_get_num_inflight_tasks(const struct doca_ctx *ctx, size_t *num_inflight_tasks)
Get number of in flight tasks in a doca context.
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
doca_ctx_states
This enum defines the states of a context.
Definition: doca_ctx.h:83
@ DOCA_CTX_STATE_STARTING
Definition: doca_ctx.h:93
@ DOCA_CTX_STATE_STOPPING
Definition: doca_ctx.h:106
@ DOCA_CTX_STATE_IDLE
Definition: doca_ctx.h:88
@ DOCA_CTX_STATE_RUNNING
Definition: doca_ctx.h:98
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 struct doca_ctx * doca_dma_as_ctx(struct doca_dma *dma)
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 uint8_t doca_pe_progress(struct doca_pe *pe)
Run the progress engine.
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 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_LOG_REGISTER(PE_TASK_ERROR::SAMPLE)
#define EXIT_ON_FAILURE(_expression_)
#define BUF_INVENTORY_SIZE
static void cleanup(struct pe_task_error_sample_state *state)
static doca_error_t allocate_tasks(struct pe_sample_state_base *state, struct doca_dma *dma, uint32_t num_tasks, size_t dma_buffer_size, struct doca_dma_task_memcpy **tasks)
static doca_error_t run(struct pe_task_error_sample_state *state)
#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)
doca_error_t poll_for_dma_stop(struct pe_task_error_sample_state *state)
#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)
#define DMA_BUFFER_SIZE
static const char * ctx_state_to_string(enum doca_ctx_states state)
static void dma_state_changed_cb(const union doca_data ctx_user_data, struct doca_ctx *ctx, enum doca_ctx_states prev_state, enum doca_ctx_states next_state)
doca_error_t run_pe_task_error_sample(void)
static doca_error_t create_dma(struct pe_task_error_sample_state *state)
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 doca_dma_task_memcpy * tasks[NUM_TASKS]
struct pe_sample_state_base base
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
struct upf_accel_ctx * ctx