NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
sync_event_local_pci_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 <unistd.h>
27 
28 #include <doca_log.h>
29 #include <doca_comch.h>
30 #include <doca_sync_event.h>
31 
32 #include <common.h>
33 
34 #include "common_common.h"
35 
36 DOCA_LOG_REGISTER(SYNC_EVENT::SAMPLE);
37 
38 /*
39  * DOCA device with export-to-dpu capability filter callback
40  *
41  * @devinfo [in]: doca_devinfo
42  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
43  */
45 {
47 }
48 
49 /*
50  * Initialize sample's DOCA Sync Event
51  *
52  * @se_rt_objs [in/out]: sample's runtime resources
53  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
54  */
56 {
58 
59  result = doca_sync_event_create(&se_rt_objs->se);
60  if (result != DOCA_SUCCESS) {
61  DOCA_LOG_ERR("Failed to create sync event: %s", doca_error_get_descr(result));
62  return result;
63  }
64 
66  if (result != DOCA_SUCCESS) {
67  DOCA_LOG_ERR("Failed to configure sync event publisher: %s", doca_error_get_descr(result));
68  return result;
69  }
70 
71  result = doca_sync_event_add_subscriber_location_cpu(se_rt_objs->se, se_rt_objs->dev);
72  if (result != DOCA_SUCCESS) {
73  DOCA_LOG_ERR("Failed to configure sync event subscriber: %s", doca_error_get_descr(result));
74  return result;
75  }
76 
77  return DOCA_SUCCESS;
78 }
79 
80 /*
81  * Export sample's DOCA Sync Event to remote side
82  *
83  * @se_rt_objs [in]: sample's runtime resources
84  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
85  */
87 {
89  const uint8_t *se_blob = NULL;
90  size_t se_blob_sz = 0;
91  struct doca_comch_task_send *task;
92 
93  result = doca_sync_event_export_to_remote_pci(se_rt_objs->se, se_rt_objs->dev, &se_blob, &se_blob_sz);
94  if (result != DOCA_SUCCESS) {
95  DOCA_LOG_ERR("Failed to export sync event to remote side: %s", doca_error_get_descr(result));
96  return result;
97  }
98 
99  DOCA_LOG_INFO("Sending exported DOCA Sync Event to remote side");
100 #ifdef DOCA_ARCH_DPU
102  se_rt_objs->comch_connection,
103  se_blob,
104  se_blob_sz,
105  &task);
106 #else
108  se_rt_objs->comch_connection,
109  se_blob,
110  se_blob_sz,
111  &task);
112 #endif
113  /* Assume there are available tasks for sending a single message */
114  if (result != DOCA_SUCCESS) {
115  DOCA_LOG_ERR("Failed to allocate a send task: %s", doca_error_get_descr(result));
116  return result;
117  }
118 
120  if (result != DOCA_SUCCESS) {
122  DOCA_LOG_ERR("Failed to submit send task: %s", doca_error_get_descr(result));
123  return result;
124  }
125 
126  DOCA_LOG_INFO("Exported DOCA Sync Event has been sent to remote side successfully");
127  return DOCA_SUCCESS;
128 }
129 
130 /*
131  * Communicate with remote side through DOCA Sync Event in synchronous mode
132  *
133  * @se_cfg [in]: user configuration represents command line arguments
134  * @se_rt_objs [in]: sample's runtime resources
135  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
136  */
138  struct sync_event_runtime_objects *se_rt_objs)
139 {
141  uint64_t fetched = 0;
142  uint64_t se_value = 1;
143 
144  DOCA_LOG_INFO("Signaling sync event for remote side");
145 
146  if (se_cfg->is_update_atomic)
147  result = doca_sync_event_update_add(se_rt_objs->se, 1, &fetched);
148  else
149  result = doca_sync_event_update_set(se_rt_objs->se, se_value);
150 
151  if (result != DOCA_SUCCESS) {
152  DOCA_LOG_ERR("Failed to signal sync event: %s", doca_error_get_descr(result));
153  return result;
154  }
155 
156  se_value++;
157 
158  DOCA_LOG_INFO("Waiting for sync event to be signaled from remote side");
159  result = doca_sync_event_wait_eq_yield(se_rt_objs->se, se_value, UINT64_MAX);
160  if (result != DOCA_SUCCESS) {
161  DOCA_LOG_ERR("Failed to wait for sync event: %s", doca_error_get_descr(result));
162  return result;
163  }
164 
165  DOCA_LOG_INFO("Done");
166 
167  return DOCA_SUCCESS;
168 }
169 
170 /*
171  * Communicate with remote side through DOCA Sync Event in asynchronous mode
172  *
173  * @se_cfg [in]: user configuration represents command line arguments
174  * @se_rt_objs [in]: sample's runtime resources
175  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
176  */
178  struct sync_event_runtime_objects *se_rt_objs)
179 {
181  struct doca_sync_event_task_wait_eq *wait_eq_task;
182  struct doca_sync_event_task_notify_add *notify_add_task;
183  struct doca_sync_event_task_notify_set *notify_set_task;
184  uint64_t fetched = 0;
185  union doca_data user_data;
186  user_data.u64 = 0;
187  uint64_t se_value = 1;
188 
189  DOCA_LOG_INFO("Signaling sync event for remote side");
190  if (se_cfg->is_update_atomic) {
192  1,
193  &fetched,
194  user_data,
195  &notify_add_task);
196  if (result != DOCA_SUCCESS)
197  return result;
198 
201 
203 
204  if (result != DOCA_SUCCESS)
205  return result;
206  } else {
208  se_value,
209  user_data,
210  &notify_set_task);
211  if (result != DOCA_SUCCESS)
212  return result;
213 
216 
218 
219  if (result != DOCA_SUCCESS)
220  return result;
221  }
222 
223  se_value++;
224 
225  result =
226  doca_sync_event_task_wait_eq_alloc_init(se_rt_objs->se, se_value, UINT64_MAX, user_data, &wait_eq_task);
227  if (result != DOCA_SUCCESS)
228  return result;
229 
230  DOCA_LOG_INFO("Waiting for sync event to be signaled from remote side");
232 
234 
235  return result;
236 }
237 
238 /*
239  * Callback event comch messages
240  *
241  * @event [in]: message receive event
242  * @recv_buffer [in]: array of bytes containing the message data
243  * @msg_len [in]: number of bytes in the recv_buffer
244  * @comch_connection [in]: comm channel connection over which the event occurred
245  */
246 static void comch_recv_event_cb(struct doca_comch_event_msg_recv *event,
247  uint8_t *recv_buffer,
248  uint32_t msg_len,
249  struct doca_comch_connection *comch_connection)
250 {
251  /* Sample does not expect to receive messages */
252  (void)event;
253  (void)recv_buffer;
254  (void)msg_len;
255  (void)comch_connection;
256 }
257 
258 /*
259  * Sample's logic
260  *
261  * @se_cfg [in]: user configuration represents command line arguments
262  * @se_rt_objs [in/out]: sample's runtime resources
263  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
264  */
266 {
268 
271  &se_rt_objs->dev);
272  if (result != DOCA_SUCCESS) {
273  sync_event_tear_down(se_rt_objs);
274  return result;
275  }
276 
277 #ifdef DOCA_ARCH_DPU
280  se_cfg->rep_pci_addr,
281  &se_rt_objs->rep);
282  if (result != DOCA_SUCCESS) {
283  sync_event_tear_down(se_rt_objs);
284  return result;
285  }
286 #endif
287 
288  result = sync_event_config_validate(se_cfg, se_rt_objs);
289  if (result != DOCA_SUCCESS) {
290  sync_event_tear_down(se_rt_objs);
291  return result;
292  }
293 
294  result = se_init(se_rt_objs);
295  if (result != DOCA_SUCCESS) {
296  sync_event_tear_down(se_rt_objs);
297  return result;
298  }
299 
300  if (se_cfg->is_async_mode)
301  result = sync_event_start_async(se_cfg, se_rt_objs);
302  else
303  result = doca_sync_event_start(se_rt_objs->se);
304 
305  if (result != DOCA_SUCCESS) {
306  sync_event_tear_down(se_rt_objs);
307  return result;
308  }
309 
310  /* Set sample specific cb message recv callback even for comch */
312 
313  result = sync_event_cc_handshake(se_rt_objs);
314  if (result != DOCA_SUCCESS) {
315  sync_event_tear_down(se_rt_objs);
316  return result;
317  }
318 
319  result = se_export(se_rt_objs);
320  if (result != DOCA_SUCCESS) {
321  sync_event_tear_down(se_rt_objs);
322  return result;
323  }
324 
325  if (se_cfg->is_async_mode)
326  result = se_communicate_async(se_cfg, se_rt_objs);
327  else
328  result = se_communicate_sync(se_cfg, se_rt_objs);
329 
330  sync_event_tear_down(se_rt_objs);
331 
332  return result;
333 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t open_doca_device_rep_with_pci(struct doca_dev *local, enum doca_devinfo_rep_filter filter, const char *pci_addr, struct doca_dev_rep **retval)
Definition: common.c:267
void sync_event_tear_down(struct sync_event_runtime_objects *se_rt_objs)
doca_error_t sync_event_async_task_submit(struct sync_event_runtime_objects *se_rt_objs, struct doca_task *se_task)
doca_error_t sync_event_start_async(const struct sync_event_config *se_cfg, struct sync_event_runtime_objects *se_rt_objs)
doca_error_t sync_event_cc_handshake(struct sync_event_runtime_objects *se_rt_objs)
doca_error_t sync_event_config_validate(const struct sync_event_config *se_cfg, const struct sync_event_runtime_objects *se_rt_objs)
static doca_error_t open_doca_device_with_pci(const char *pcie_value, struct doca_dev **retval)
Definition: device.c:43
DOCA_STABLE doca_error_t doca_comch_client_task_send_alloc_init(struct doca_comch_client *comch_client, struct doca_comch_connection *peer, const void *msg, uint32_t len, struct doca_comch_task_send **task)
DOCA_STABLE struct doca_task * doca_comch_task_send_as_task(struct doca_comch_task_send *task)
DOCA_STABLE doca_error_t doca_comch_server_task_send_alloc_init(struct doca_comch_server *comch_server, struct doca_comch_connection *peer, const void *msg, uint32_t len, struct doca_comch_task_send **task)
@ DOCA_DEVINFO_REP_FILTER_NET
Definition: doca_dev.h:67
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_submit(struct doca_task *task)
Submit a task to a progress engine.
DOCA_STABLE void doca_task_free(struct doca_task *task)
Free a task back to where it was allocated from.
DOCA_EXPERIMENTAL struct doca_task * doca_sync_event_task_notify_set_as_doca_task(struct doca_sync_event_task_notify_set *task)
Convert a DOCA Sync Event notify-set task to a DOCA Task.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_add_publisher_location_remote_pci(struct doca_sync_event *event)
Declare Sync Event publisher as remote pci.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_start(struct doca_sync_event *event)
Start a Sync Event to be operate as stand-alone DOCA Core object only.
DOCA_EXPERIMENTAL struct doca_task * doca_sync_event_task_wait_eq_as_doca_task(struct doca_sync_event_task_wait_eq *task)
Convert a DOCA Sync Event wait-equal task to a DOCA Task.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_update_add(struct doca_sync_event *event, uint64_t value, uint64_t *fetched)
Atomically increase the value of a Sync Event by some value synchronously.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_wait_eq_yield(struct doca_sync_event *event, uint64_t value, uint64_t mask)
Wait for the value of a Sync Event to be equal to some value synchronously in a periodically busy wai...
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_create(struct doca_sync_event **event)
Create a Sync Event handle.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_cap_is_export_to_remote_pci_supported(const struct doca_devinfo *devinfo)
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_task_wait_eq_alloc_init(struct doca_sync_event *event, uint64_t wait_val, uint64_t mask, union doca_data user_data, struct doca_sync_event_task_wait_eq **task)
Allocate a DOCA Sync Event wait-equal task.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_task_notify_add_alloc_init(struct doca_sync_event *event, uint64_t inc_val, uint64_t *fetched_val_ptr, union doca_data user_data, struct doca_sync_event_task_notify_add **task)
Allocate a DOCA Sync Event notify-add task.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_update_set(struct doca_sync_event *event, uint64_t value)
Set the value of a Sync Event to some value synchronously.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_task_notify_set_alloc_init(struct doca_sync_event *event, uint64_t set_val, union doca_data user_data, struct doca_sync_event_task_notify_set **task)
Allocate a DOCA Sync Event notify-set task.
DOCA_EXPERIMENTAL struct doca_task * doca_sync_event_task_notify_add_as_doca_task(struct doca_sync_event_task_notify_add *task)
Convert a DOCA Sync Event notify-add task to a DOCA Task.
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_add_subscriber_location_cpu(struct doca_sync_event *event, struct doca_dev *dev)
DOCA_EXPERIMENTAL doca_error_t doca_sync_event_export_to_remote_pci(struct doca_sync_event *event, struct doca_dev *dev, const uint8_t **data, size_t *sz)
Export Sync Event to be shared with remote PCI.
char rep_pci_addr[DOCA_DEVINFO_REP_PCI_ADDR_SIZE]
Definition: common_common.h:48
char dev_pci_addr[DOCA_DEVINFO_PCI_ADDR_SIZE]
Definition: common_common.h:47
struct doca_dev * dev
Definition: common_common.h:56
struct doca_comch_connection * comch_connection
Definition: common_common.h:69
struct doca_comch_server * server
Definition: common_common.h:66
doca_comch_event_msg_recv_cb_t comch_recv_event_cb
Definition: common_common.h:70
struct doca_comch_client * client
Definition: common_common.h:67
struct doca_sync_event * se
Definition: common_common.h:58
struct doca_dev_rep * rep
Definition: common_common.h:57
static doca_error_t se_export(struct sync_event_runtime_objects *se_rt_objs)
static doca_error_t se_init(struct sync_event_runtime_objects *se_rt_objs)
static doca_error_t se_communicate_sync(const struct sync_event_config *se_cfg, struct sync_event_runtime_objects *se_rt_objs)
doca_error_t sync_event_run(const struct sync_event_config *se_cfg, struct sync_event_runtime_objects *se_rt_objs)
static void comch_recv_event_cb(struct doca_comch_event_msg_recv *event, uint8_t *recv_buffer, uint32_t msg_len, struct doca_comch_connection *comch_connection)
DOCA_LOG_REGISTER(SYNC_EVENT::SAMPLE)
static doca_error_t se_communicate_async(const struct sync_event_config *se_cfg, struct sync_event_runtime_objects *se_rt_objs)
doca_error_t sync_event_get_export_to_dpu_supported(struct doca_devinfo *devinfo)
Convenience type for representing opaque data.
Definition: doca_types.h:56
uint64_t u64
Definition: doca_types.h:58