NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
rdma_send_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 <doca_error.h>
27 #include <doca_log.h>
28 #include <doca_buf_inventory.h>
29 #include <doca_buf.h>
30 #include <doca_ctx.h>
31 
32 #include "rdma_common.h"
33 
34 #define MAX_BUFF_SIZE (256) /* Maximum DOCA buffer size */
35 
36 DOCA_LOG_REGISTER(RDMA_SEND::SAMPLE);
37 
38 /*
39  * Write the connection details for the receiver to read, and read the connection details of the receiver
40  * In DC transport mode it is only needed to read the remote connection details
41  *
42  * @cfg [in]: Configuration parameters
43  * @resources [in/out]: RDMA resources
44  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
45  */
47 {
49 
50  if (cfg->transport_type == DOCA_RDMA_TRANSPORT_TYPE_RC) {
51  /* Write the RDMA connection details */
52  result = write_file(cfg->local_connection_desc_path,
55  if (result != DOCA_SUCCESS) {
56  DOCA_LOG_ERR("Failed to write the RDMA connection details: %s", doca_error_get_descr(result));
57  return result;
58  }
59 
60  DOCA_LOG_INFO("You can now copy %s to the receiver", cfg->local_connection_desc_path);
61  }
62 
63  DOCA_LOG_INFO("Please copy %s from the receiver and then press enter after pressing enter in the receiver side",
64  cfg->remote_connection_desc_path);
65 
66  /* Wait for enter */
68 
69  /* Read the remote RDMA connection details */
70  result = read_file(cfg->remote_connection_desc_path,
73  if (result != DOCA_SUCCESS)
74  DOCA_LOG_ERR("Failed to read the remote RDMA connection details: %s", doca_error_get_descr(result));
75 
76  return result;
77 }
78 
79 /*
80  * RDMA send task completed callback
81  *
82  * @rdma_send_task [in]: Completed task
83  * @task_user_data [in]: doca_data from the task
84  * @ctx_user_data [in]: doca_data from the context
85  */
86 static void rdma_send_completed_callback(struct doca_rdma_task_send *rdma_send_task,
87  union doca_data task_user_data,
88  union doca_data ctx_user_data)
89 {
90  struct rdma_resources *resources = (struct rdma_resources *)ctx_user_data.ptr;
92  doca_error_t result = DOCA_SUCCESS, tmp_result;
93 
94  DOCA_LOG_INFO("RDMA send task was done successfully");
95 
98  if (tmp_result != DOCA_SUCCESS) {
99  DOCA_LOG_ERR("Failed to decrease src_buf count: %s", doca_error_get_descr(tmp_result));
100  DOCA_ERROR_PROPAGATE(result, tmp_result);
101  }
102 
103  /* Update that an error was encountered, if any */
105 
107  /* Stop context once all tasks are completed */
108  if (resources->num_remaining_tasks == 0) {
109  if (resources->cfg->use_rdma_cm == true)
112  }
113 }
114 
115 /*
116  * RDMA send task error callback
117  *
118  * @rdma_send_task [in]: failed task
119  * @task_user_data [in]: doca_data from the task
120  * @ctx_user_data [in]: doca_data from the context
121  */
122 static void rdma_send_error_callback(struct doca_rdma_task_send *rdma_send_task,
123  union doca_data task_user_data,
124  union doca_data ctx_user_data)
125 {
126  struct rdma_resources *resources = (struct rdma_resources *)ctx_user_data.ptr;
127  struct doca_task *task = doca_rdma_task_send_as_task(rdma_send_task);
130 
131  /* Update that an error was encountered */
134  DOCA_LOG_ERR("RDMA send task failed: %s", doca_error_get_descr(result));
135 
136  doca_task_free(task);
138  if (result != DOCA_SUCCESS)
139  DOCA_LOG_ERR("Failed to decrease src_buf count: %s", doca_error_get_descr(result));
140 
142  /* Stop context once all tasks are completed */
143  if (resources->num_remaining_tasks == 0) {
144  if (resources->cfg->use_rdma_cm == true)
147  }
148 }
149 
150 /*
151  * Export and receive connection details, and connect to the remote RDMA
152  *
153  * @resources [in]: RDMA resources
154  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
155  */
157 {
159 
160  if (resources->cfg->use_rdma_cm == true)
161  return rdma_cm_connect(resources);
162 
163  /* Export RDMA connection details */
167  &(resources->connections[0]));
168  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("Failed to export RDMA: %s", doca_error_get_descr(result));
170  return result;
171  }
172 
173  /* Write and read connection details to the receiver */
175  if (result != DOCA_SUCCESS) {
176  DOCA_LOG_ERR("Failed to write and read connection details from receiver: %s",
178  return result;
179  }
180 
181  /* Connect RDMA */
185  resources->connections[0]);
186  if (result != DOCA_SUCCESS)
187  DOCA_LOG_ERR("Failed to connect the sender's RDMA to the receiver's RDMA: %s",
189 
190  return result;
191 }
192 
193 /*
194  * Prepare and submit RDMA send task
195  *
196  * @resources [in]: RDMA resources
197  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
198  */
200 {
201  struct doca_rdma_task_send *rdma_send_task = NULL;
202  union doca_data task_user_data = {0};
203  void *src_buf_data;
204  doca_error_t result, tmp_result;
205 
206  if (resources->cfg->use_rdma_cm == true) {
208  "Please press enter after the receive task has been successfully submitted in the receiver side");
209 
210  /* Wait for enter */
211  wait_for_enter();
212  }
213 
214  /* Add src buffer to DOCA buffer inventory */
216  resources->mmap,
219  &resources->src_buf);
220  if (result != DOCA_SUCCESS) {
221  DOCA_LOG_ERR("Failed to allocate DOCA buffer to DOCA buffer inventory: %s",
223  return result;
224  }
225 
226  /* Set data of src buffer */
227  result = doca_buf_get_data(resources->src_buf, &src_buf_data);
228  if (result != DOCA_SUCCESS) {
229  DOCA_LOG_ERR("Failed to get source buffer data: %s", doca_error_get_descr(result));
230  goto destroy_src_buf;
231  }
232  strncpy(src_buf_data, resources->cfg->send_string, MAX_BUFF_SIZE + 1);
233 
234  /* Include first_encountered_error in user data of task to be used in the callbacks */
235  task_user_data.ptr = &(resources->first_encountered_error);
236  /* Allocate and construct RDMA send task */
240  task_user_data,
241  &rdma_send_task);
242  if (result != DOCA_SUCCESS) {
243  DOCA_LOG_ERR("Failed to allocate RDMA send task: %s", doca_error_get_descr(result));
244  goto destroy_src_buf;
245  }
246 
247  /* Submit RDMA send task */
248  DOCA_LOG_INFO("Submitting RDMA send task that sends \"%s\" to receiver", resources->cfg->send_string);
251  if (result != DOCA_SUCCESS) {
252  DOCA_LOG_ERR("Failed to submit RDMA send task: %s", doca_error_get_descr(result));
253  goto free_task;
254  }
255 
256  return result;
257 
258 free_task:
260 destroy_src_buf:
262  if (tmp_result != DOCA_SUCCESS) {
263  DOCA_LOG_ERR("Failed to decrease src_buf count: %s", doca_error_get_descr(tmp_result));
264  DOCA_ERROR_PROPAGATE(result, tmp_result);
265  }
266  return result;
267 }
268 
269 /*
270  * RDMA send state change callback
271  * This function represents the state machine for this RDMA program
272  *
273  * @user_data [in]: doca_data from the context
274  * @ctx [in]: DOCA context
275  * @prev_state [in]: Previous DOCA context state
276  * @next_state [in]: Next DOCA context state
277  */
278 static void rdma_send_state_change_callback(const union doca_data user_data,
279  struct doca_ctx *ctx,
280  enum doca_ctx_states prev_state,
281  enum doca_ctx_states next_state)
282 {
283  struct rdma_resources *resources = (struct rdma_resources *)user_data.ptr;
284  struct rdma_config *cfg = resources->cfg;
286  (void)prev_state;
287  (void)ctx;
288 
289  switch (next_state) {
291  DOCA_LOG_INFO("RDMA context entered starting state");
292  break;
294  DOCA_LOG_INFO("RDMA context is running");
295 
297  if (result != DOCA_SUCCESS) {
298  DOCA_LOG_ERR("rdma_send_export_and_connect() failed: %s", doca_error_get_descr(result));
299  break;
300  } else
301  DOCA_LOG_INFO("RDMA context finished initialization");
302 
303  if (cfg->use_rdma_cm == true)
304  break;
305 
307  if (result != DOCA_SUCCESS)
308  DOCA_LOG_ERR("rdma_send_prepare_and_submit_task() failed: %s", doca_error_get_descr(result));
309  break;
318  DOCA_LOG_INFO("RDMA context entered into stopping state. Any inflight tasks will be flushed");
319  break;
320  case DOCA_CTX_STATE_IDLE:
321  DOCA_LOG_INFO("RDMA context has been stopped");
322 
323  /* We can stop progressing the PE */
324  resources->run_pe_progress = false;
325  break;
326  default:
327  break;
328  }
329 
330  /* If something failed - update that an error was encountered and stop the ctx */
331  if (result != DOCA_SUCCESS) {
333  (void)doca_ctx_stop(ctx);
334  }
335 }
336 
337 /*
338  * Send a message to the receiver
339  *
340  * @cfg [in]: Configuration parameters
341  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
342  */
344 {
345  struct rdma_resources resources = {0};
346  union doca_data ctx_user_data = {0};
347  const uint32_t mmap_permissions = DOCA_ACCESS_FLAG_LOCAL_READ_WRITE;
348  const uint32_t rdma_permissions = DOCA_ACCESS_FLAG_LOCAL_READ_WRITE;
349  struct timespec ts = {
350  .tv_sec = 0,
351  .tv_nsec = SLEEP_IN_NANOS,
352  };
353  doca_error_t result, tmp_result;
354 
355  /* Allocating resources */
357  mmap_permissions,
358  rdma_permissions,
360  &resources);
361  if (result != DOCA_SUCCESS) {
362  DOCA_LOG_ERR("Failed to allocate RDMA Resources: %s", doca_error_get_descr(result));
363  return result;
364  }
365 
370  if (result != DOCA_SUCCESS) {
371  DOCA_LOG_ERR("Unable to set configurations for RDMA send task: %s", doca_error_get_descr(result));
372  goto destroy_resources;
373  }
374 
376  if (result != DOCA_SUCCESS) {
377  DOCA_LOG_ERR("Unable to set state change callback for RDMA context: %s", doca_error_get_descr(result));
378  goto destroy_resources;
379  }
380 
381  /* Include the program's resources in user data of context to be used in callbacks */
382  ctx_user_data.ptr = &(resources);
384  if (result != DOCA_SUCCESS) {
385  DOCA_LOG_ERR("Failed to set context user data: %s", doca_error_get_descr(result));
386  goto destroy_resources;
387  }
388 
389  /* Create DOCA buffer inventory */
391  if (result != DOCA_SUCCESS) {
392  DOCA_LOG_ERR("Failed to create DOCA buffer inventory: %s", doca_error_get_descr(result));
393  goto destroy_resources;
394  }
395 
396  /* Start DOCA buffer inventory */
398  if (result != DOCA_SUCCESS) {
399  DOCA_LOG_ERR("Failed to start DOCA buffer inventory: %s", doca_error_get_descr(result));
400  goto destroy_buf_inventory;
401  }
402 
403  if (cfg->use_rdma_cm == true) {
404  /* Set rdma cm connection configuration callbacks */
408  /* need_send_mmap_info */ false,
409  /* need_recv_mmap_info */ false);
410  if (result != DOCA_SUCCESS) {
411  DOCA_LOG_ERR("Failed to config RDMA CM callbacks and negotiation functions: %s",
413  goto destroy_buf_inventory;
414  }
415  }
416 
417  /* Start RDMA context */
419  if (result != DOCA_SUCCESS) {
420  DOCA_LOG_ERR("Failed to start RDMA context: %s", doca_error_get_descr(result));
421  goto stop_buf_inventory;
422  }
423 
424  /*
425  * Run the progress engine which will run the state machine defined in rdma_send_state_change_callback()
426  * When the context moves to idle, the context change callback call will signal to stop running the progress
427  * engine.
428  */
429  while (resources.run_pe_progress) {
430  if (doca_pe_progress(resources.pe) == 0)
431  nanosleep(&ts, &ts);
432  }
433 
434  /* Assign the result we update in the callbacks */
436 
437 stop_buf_inventory:
439  if (tmp_result != DOCA_SUCCESS) {
440  DOCA_LOG_ERR("Failed to stop DOCA buffer inventory: %s", doca_error_get_descr(tmp_result));
441  DOCA_ERROR_PROPAGATE(result, tmp_result);
442  }
443 destroy_buf_inventory:
445  if (tmp_result != DOCA_SUCCESS) {
446  DOCA_LOG_ERR("Failed to destroy DOCA buffer inventory: %s", doca_error_get_descr(tmp_result));
447  DOCA_ERROR_PROPAGATE(result, tmp_result);
448  }
449 destroy_resources:
450  tmp_result = destroy_rdma_resources(&resources, cfg);
451  if (tmp_result != DOCA_SUCCESS) {
452  DOCA_LOG_ERR("Failed to destroy DOCA RDMA resources: %s", doca_error_get_descr(tmp_result));
453  DOCA_ERROR_PROPAGATE(result, tmp_result);
454  }
455  return result;
456 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
#define SLEEP_IN_NANOS
Definition: comch_utils.c:40
doca_error_t destroy_rdma_resources(struct rdma_resources *resources)
Definition: rdma_common.c:470
void wait_for_enter(void)
Definition: rdma_common.c:1771
doca_error_t write_file(const char *file_path, const char *string, size_t string_len)
Definition: rdma_common.c:1087
doca_error_t rdma_cm_connect(struct rdma_resources *resources)
Definition: rdma_common.c:1172
doca_error_t allocate_rdma_resources(struct rdma_config *cfg, const uint32_t mmap_permissions, const uint32_t rdma_permissions, task_check func, struct rdma_resources *resources)
Definition: rdma_common.c:758
doca_error_t config_rdma_cm_callback_and_negotiation_task(struct rdma_resources *resources, bool need_send_task, bool need_recv_task)
Definition: rdma_common.c:1720
doca_error_t rdma_cm_disconnect(struct rdma_resources *resources)
Definition: rdma_common.c:1244
#define INVENTORY_NUM_INITIAL_ELEMENTS
Definition: rdma_common.h:47
#define NUM_RDMA_TASKS
Definition: rdma_common.h:57
struct rdma_resources resources
DOCA_STABLE doca_error_t doca_buf_inventory_destroy(struct doca_buf_inventory *inventory)
Destroy buffer inventory structure.
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_inventory_start(struct doca_buf_inventory *inventory)
Start element retrieval from inventory.
DOCA_STABLE doca_error_t doca_buf_inventory_create(size_t num_elements, struct doca_buf_inventory **inventory)
Allocates buffer inventory with default/unset attributes.
DOCA_STABLE doca_error_t doca_buf_inventory_stop(struct doca_buf_inventory *inventory)
Stop element retrieval from inventory.
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_buf_get_data(const struct doca_buf *buf, void **data)
Get the buffer's data.
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_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
#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_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_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.
DOCA_EXPERIMENTAL doca_error_t doca_rdma_task_send_set_conf(struct doca_rdma *rdma, doca_rdma_task_send_completion_cb_t successful_task_completion_cb, doca_rdma_task_send_completion_cb_t error_task_completion_cb, uint32_t num_tasks)
This method sets the send tasks configuration.
DOCA_EXPERIMENTAL struct doca_task * doca_rdma_task_send_as_task(struct doca_rdma_task_send *task)
This method converts a send task to a doca_task.
DOCA_EXPERIMENTAL doca_error_t doca_rdma_export(struct doca_rdma *rdma, const void **local_rdma_conn_details, size_t *local_rdma_conn_details_size, struct doca_rdma_connection **rdma_connection)
Export doca_rdma connection details object The doca_rdma_conn_details are used in doca_rdma_connect()...
DOCA_EXPERIMENTAL doca_error_t doca_rdma_task_send_allocate_init(struct doca_rdma *rdma, struct doca_rdma_connection *rdma_connection, const struct doca_buf *src_buf, union doca_data user_data, struct doca_rdma_task_send **task)
This method allocates and initializes a send task.
DOCA_EXPERIMENTAL doca_error_t doca_rdma_connect(struct doca_rdma *rdma, const void *remote_rdma_conn_details, size_t remote_rdma_conn_details_size, struct doca_rdma_connection *rdma_connection)
Connect to remote doca_rdma peer. Can only be called when the ctx is in DOCA_CTX_STATE_STARTING state...
DOCA_EXPERIMENTAL doca_error_t doca_rdma_cap_task_send_is_supported(const struct doca_devinfo *devinfo)
@ DOCA_RDMA_TRANSPORT_TYPE_RC
Definition: doca_rdma.h:46
@ DOCA_ACCESS_FLAG_LOCAL_READ_WRITE
Definition: doca_types.h:83
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
#define MAX_BUFF_SIZE
DOCA_LOG_REGISTER(RDMA_SEND::SAMPLE)
static void rdma_send_error_callback(struct doca_rdma_task_send *rdma_send_task, union doca_data task_user_data, union doca_data ctx_user_data)
static doca_error_t rdma_send_export_and_connect(struct rdma_resources *resources)
static void rdma_send_completed_callback(struct doca_rdma_task_send *rdma_send_task, union doca_data task_user_data, union doca_data ctx_user_data)
static doca_error_t rdma_send_prepare_and_submit_task(struct rdma_resources *resources)
static doca_error_t write_read_connection(struct rdma_config *cfg, struct rdma_resources *resources)
doca_error_t rdma_send(struct rdma_config *cfg)
static void rdma_send_state_change_callback(const union doca_data user_data, struct doca_ctx *ctx, enum doca_ctx_states prev_state, enum doca_ctx_states next_state)
char send_string[MAX_ARG_SIZE]
Definition: rdma_common.h:80
bool use_rdma_cm
Definition: rdma_common.h:71
size_t remote_rdma_conn_descriptor_size
Definition: rdma_common.h:127
doca_error_t first_encountered_error
Definition: rdma_common.h:132
bool require_remote_mmap
Definition: rdma_common.h:152
struct doca_ctx * rdma_ctx
Definition: rdma_common.h:85
size_t num_remaining_tasks
Definition: rdma_common.h:134
prepare_and_submit_task_fn task_fn
Definition: rdma_common.h:150
const void * rdma_conn_descriptor
Definition: rdma_common.h:124
void * remote_rdma_conn_descriptor
Definition: rdma_common.h:126
struct doca_rdma_connection * connections[MAX_NUM_CONNECTIONS]
Definition: rdma_common.h:138
struct rdma_config * cfg
Definition: rdma_common.h:80
struct doca_rdma * rdma
Definition: rdma_common.h:83
struct doca_pe * pe
Definition: rdma_common.h:86
struct doca_mmap * mmap
Definition: rdma_common.h:112
bool run_pe_progress
Definition: rdma_common.h:133
struct doca_buf * src_buf
Definition: rdma_common.h:122
struct doca_buf_inventory * buf_inventory
Definition: rdma_common.h:117
size_t rdma_conn_descriptor_size
Definition: rdma_common.h:125
char * mmap_memrange
Definition: rdma_common.h:116
Convenience type for representing opaque data.
Definition: doca_types.h:56
void * ptr
Definition: doca_types.h:57
struct upf_accel_ctx * ctx
doca_error_t read_file(char const *path, char **out_bytes, size_t *out_bytes_len)
Definition: utils.c:56