NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
worker_graph.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 <string.h>
27 
28 #include <doca_buf.h>
29 #include <doca_log.h>
30 #include <doca_pe.h>
31 
32 #include "urom_graph.h"
33 #include "worker_graph.h"
34 
35 DOCA_LOG_REGISTER(UROM::WORKER::GRAPH);
36 
37 static uint64_t graph_id; /* Graph plugin id, will be set in init function */
38 static uint64_t graph_version = 0x01; /* Graph plugin version */
39 
40 /* Graph task metadata */
42  union doca_data cookie; /* User cookie */
44 };
45 
46 /*
47  * Packs graph commands
48  *
49  * @graph_cmd [in]: internal graph command to pack
50  * @packed_cmd_len [in/out]: packed buffer size, at the end will store packed command size
51  * @packed_cmd [out]: Store graph packed command
52  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
53  */
55  size_t *packed_cmd_len,
56  void *packed_cmd)
57 {
58  size_t pack_len;
59  void *pack_head = packed_cmd;
60 
61  pack_len = sizeof(struct urom_worker_graph_cmd);
62  if (pack_len > *packed_cmd_len)
64 
65  /* pack base command */
66  memcpy(pack_head, graph_cmd, pack_len);
67  *packed_cmd_len = pack_len;
68  return DOCA_SUCCESS;
69 }
70 
71 /*
72  * Graph commands completion callback function
73  *
74  * @task [in]: UROM worker task
75  * @task_user_data [in]: task user data
76  * @ctx_user_data [in]: worker context user data
77  */
78 static void urom_graph_completed(struct doca_urom_worker_cmd_task *task,
79  union doca_data task_user_data,
80  union doca_data ctx_user_data)
81 {
82  (void)task_user_data;
83  (void)ctx_user_data;
84 
85  size_t data_len;
87  struct doca_buf *response;
88  struct urom_worker_notify_graph notify_error = {0};
89  struct urom_worker_notify_graph *graph_notify = &notify_error;
90  struct doca_graph_task_data *task_data;
91 
93  if (task_data == NULL) {
94  DOCA_LOG_ERR("Failed to get task data buffer");
96  goto error_exit;
97  }
98 
100  if (response == NULL) {
101  DOCA_LOG_ERR("Failed to get task response buffer");
103  goto error_exit;
104  }
105 
106  result = doca_buf_get_data_len(response, &data_len);
107  if (result != DOCA_SUCCESS) {
108  DOCA_LOG_ERR("Failed to get response data length");
109  goto error_exit;
110  }
111 
112  if (data_len != sizeof(*graph_notify)) {
113  DOCA_LOG_ERR("Task response data length is different from notification expected length");
115  goto error_exit;
116  }
117 
118  result = doca_buf_get_data(response, (void **)&graph_notify);
119  if (result != DOCA_SUCCESS) {
120  DOCA_LOG_ERR("Failed to get response data");
121  goto error_exit;
122  }
123 
125  if (result != DOCA_SUCCESS)
126  DOCA_LOG_ERR("Bad worker command task status %s", doca_error_get_descr(result));
127 
128 error_exit:
129  (task_data->user_cb)(result, task_data->cookie, graph_notify->loopback.data);
131  if (result != DOCA_SUCCESS)
132  DOCA_LOG_ERR("Failed to release worker command task %s", doca_error_get_descr(result));
133 }
134 
136  union doca_data cookie,
137  uint64_t data,
139 {
140  size_t cmd_len = 0;
141  doca_error_t tmp_result, result;
142  struct doca_buf *payload;
143  struct doca_urom_worker_cmd_task *task;
144  struct doca_graph_task_data *task_data;
145  struct urom_worker_graph_cmd *graph_cmd;
146 
147  /* Allocate task */
149  if (result != DOCA_SUCCESS)
150  return result;
151 
153  result = doca_buf_get_data(payload, (void **)&graph_cmd);
154  if (result != DOCA_SUCCESS)
155  goto task_destroy;
156 
157  result = doca_buf_get_data_len(payload, &cmd_len);
158  if (result != DOCA_SUCCESS)
159  goto task_destroy;
160 
161  /* Populate commands attributes */
163  graph_cmd->loopback.data = data;
164 
165  result = urom_worker_graph_cmd_pack(graph_cmd, &cmd_len, (void *)graph_cmd);
166  if (result != DOCA_SUCCESS) {
167  DOCA_LOG_ERR("Failed to pack graph command");
168  goto task_destroy;
169  }
170 
171  result = doca_buf_set_data(payload, graph_cmd, cmd_len);
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Failed to set task command data");
174  goto task_destroy;
175  }
176 
178  task_data->user_cb = cb;
179  task_data->cookie = cookie;
180 
183  if (result != DOCA_SUCCESS)
184  goto task_destroy;
185 
186  return DOCA_SUCCESS;
187 task_destroy:
188  tmp_result = doca_urom_worker_cmd_task_release(task);
189  if (tmp_result != DOCA_SUCCESS)
190  DOCA_LOG_ERR("Failed to release worker command task, error [%s]", doca_error_get_descr(tmp_result));
191  return result;
192 }
193 
194 doca_error_t urom_graph_init(uint64_t plugin_id, uint64_t version)
195 {
196  if (version != graph_version)
198 
199  graph_id = plugin_id;
200  return DOCA_SUCCESS;
201 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t cookie
DOCA_STABLE doca_error_t doca_buf_get_data(const struct doca_buf *buf, void **data)
Get the buffer's data.
DOCA_STABLE doca_error_t doca_buf_get_data_len(const struct doca_buf *buf, size_t *data_len)
Get buffer's data length.
DOCA_STABLE doca_error_t doca_buf_set_data(struct doca_buf *buf, void *data, size_t data_len)
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_ERROR_INITIALIZATION
Definition: doca_error.h:46
@ DOCA_ERROR_UNSUPPORTED_VERSION
Definition: doca_error.h:57
@ DOCA_SUCCESS
Definition: doca_error.h:38
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
DOCA_STABLE doca_error_t doca_task_get_status(const struct doca_task *task)
Get task status.
DOCA_STABLE doca_error_t doca_task_submit(struct doca_task *task)
Submit a task to a progress engine.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_cmd_task_release(struct doca_urom_worker_cmd_task *task)
Release Worker Command task.
DOCA_EXPERIMENTAL void doca_urom_worker_cmd_task_set_cb(struct doca_urom_worker_cmd_task *task, doca_urom_worker_cmd_task_completion_cb_t cb)
Set Worker Command task callback.
DOCA_EXPERIMENTAL struct doca_buf * doca_urom_worker_cmd_task_get_response(struct doca_urom_worker_cmd_task *task)
Get Worker Command task response.
DOCA_EXPERIMENTAL void * doca_urom_worker_cmd_task_get_user_data(struct doca_urom_worker_cmd_task *task)
Get Worker Command user data to populate.
DOCA_EXPERIMENTAL doca_error_t doca_urom_worker_cmd_task_allocate_init(struct doca_urom_worker *worker_ctx, uint64_t plugin, struct doca_urom_worker_cmd_task **task)
Allocate and initialize Worker Command task.
DOCA_EXPERIMENTAL struct doca_buf * doca_urom_worker_cmd_task_get_payload(struct doca_urom_worker_cmd_task *task)
Get Worker Command task payload.
DOCA_EXPERIMENTAL struct doca_task * doca_urom_worker_cmd_task_as_task(struct doca_urom_worker_cmd_task *task)
This method converts a worker cmd task to doca_task.
union doca_data cookie
Definition: worker_graph.c:42
urom_graph_loopback_finished user_cb
Definition: worker_graph.c:43
struct urom_worker_graph_cmd_loopback loopback
Definition: urom_graph.h:49
struct urom_worker_graph_notify_loopback loopback
Definition: urom_graph.h:68
Convenience type for representing opaque data.
Definition: doca_types.h:56
@ UROM_WORKER_CMD_GRAPH_LOOPBACK
Definition: urom_graph.h:37
static uint64_t graph_version
Definition: worker_graph.c:38
doca_error_t urom_graph_init(uint64_t plugin_id, uint64_t version)
Definition: worker_graph.c:194
static uint64_t graph_id
Definition: worker_graph.c:37
static doca_error_t urom_worker_graph_cmd_pack(struct urom_worker_graph_cmd *graph_cmd, size_t *packed_cmd_len, void *packed_cmd)
Definition: worker_graph.c:54
doca_error_t urom_graph_task_loopback(struct doca_urom_worker *worker_ctx, union doca_data cookie, uint64_t data, urom_graph_loopback_finished cb)
Definition: worker_graph.c:135
DOCA_LOG_REGISTER(UROM::WORKER::GRAPH)
static void urom_graph_completed(struct doca_urom_worker_cmd_task *task, union doca_data task_user_data, union doca_data ctx_user_data)
Definition: worker_graph.c:78
void(* urom_graph_loopback_finished)(doca_error_t result, union doca_data cookie, uint64_t data)
Definition: worker_graph.h:44