NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
rmax_create_stream_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2024 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 <stdlib.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <netinet/in.h>
31 
32 #include "rmax_common.h"
33 
34 DOCA_LOG_REGISTER(RMAX_CREATE_STREAM);
35 
36 /*
37  * Handle a successful Rx data event
38  *
39  * @event_rx_data [in]: the event that occurred. Holds result data.
40  * @event_user_data [in]: user defined data that was previously provided on registration. Holds rmax_program_state.
41  */
42 void rx_success_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
43 {
44  struct doca_rmax_in_stream_result *comp = doca_rmax_in_stream_event_rx_data_get_result(event_rx_data);
45 
46  if (comp->elements_count == 0)
47  return;
48 
49  DOCA_LOG_INFO("Received %4d packet(s), first %lu last %lu", comp->elements_count, comp->ts_first, comp->ts_last);
50 
51  struct rmax_program_state *state = (struct rmax_program_state *)event_user_data.ptr;
52 
53  /* dump packets */
54  uint8_t *data = comp->memblk_ptr_arr[0];
55 
56  for (size_t i = 0; i < comp->elements_count; ++i, data += state->stride_size[0]) {
57  char *dump = hex_dump(data, state->config->data_size);
58 
59  DOCA_LOG_DBG("Packet:\n%s", dump);
60  free(dump);
61  }
62 }
63 
64 /*
65  * Handle a failed Rx data event
66  *
67  * @event_rx_data [in]: the event that occurred. Holds error data.
68  * @event_user_data [in]: user defined data that was previously provided on registration. Holds rmax_program_state.
69  */
70 void rx_error_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
71 {
72  struct doca_rmax_stream_error *err = doca_rmax_in_stream_event_rx_data_get_error(event_rx_data);
73  struct rmax_program_state *state = (struct rmax_program_state *)event_user_data.ptr;
74 
75  DOCA_LOG_ERR("Error in Rx event: code=%d message=%s", err->code, err->message);
76 
77  state->run_pe_progress = false;
79 }
80 
81 /*
82  * Run create_stream sample, which creates stream
83  *
84  * @state [in]: DOCA core related objects
85  * @stream_config [in]: stream configurations
86  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
87  */
89 {
90  struct doca_rmax_in_stream *stream = NULL;
91  struct doca_buf *buf = NULL;
92  struct doca_rmax_flow *flow = NULL;
94 
95  memset(state, 0, sizeof(*state));
96  state->config = stream_config;
97 
98  /* open DOCA device with the given PCI address */
100  if (result != DOCA_SUCCESS)
101  return result;
102 
103  /* DOCA RMAX library Initialization */
104  result = doca_rmax_init();
105  if (result != DOCA_SUCCESS) {
106  DOCA_LOG_ERR("Failed to initialize DOCA RMAX library: %s", doca_error_get_descr(result));
107  rmax_create_stream_cleanup(state, stream, flow, buf);
108  return result;
109  }
110 
111  /* create core DOCA objects */
113  if (result != DOCA_SUCCESS) {
114  DOCA_LOG_ERR("Failed to create DOCA core related objects: %s", doca_error_get_descr(result));
115  rmax_create_stream_cleanup(state, stream, flow, buf);
116  return result;
117  }
118 
119  /* create stream object */
120  result = doca_rmax_in_stream_create(state->core_objects.dev, &stream);
121  if (result != DOCA_SUCCESS) {
122  DOCA_LOG_ERR("Failed to create DOCA Rivermax stream: %s", doca_error_get_descr(result));
123  rmax_create_stream_cleanup(state, stream, flow, buf);
124  return result;
125  }
126 
127  /* set stream attributes, based on received or default configurations */
128  result = rmax_stream_set_attributes(stream, stream_config);
129  if (result != DOCA_SUCCESS) {
130  DOCA_LOG_ERR("Failed to set stream attributes: %s", doca_error_get_descr(result));
131  rmax_create_stream_cleanup(state, stream, flow, buf);
132  return result;
133  }
134 
135  /* Register Rx data event handlers */
136  union doca_data event_user_data;
137 
138  event_user_data.ptr = (void *)state;
139  result = doca_rmax_in_stream_event_rx_data_register(stream, event_user_data, rx_success_cb, rx_error_cb);
140  if (result != DOCA_SUCCESS) {
141  DOCA_LOG_ERR("Failed to register to Rx data event: %s", doca_error_get_descr(result));
142  rmax_create_stream_cleanup(state, stream, flow, buf);
143  return result;
144  }
145 
146  /* convert DOCA Rmax stream to DOCA context */
147  state->core_objects.ctx = doca_rmax_in_stream_as_ctx(stream);
148  if (state->core_objects.ctx == NULL) {
149  DOCA_LOG_ERR("Failed to convert stream to a context");
150  rmax_create_stream_cleanup(state, stream, flow, buf);
151  return result;
152  }
153 
154  /* allocate buffers for received data */
155  result = rmax_stream_allocate_buf(state, stream, stream_config, &buf, state->stride_size);
156  if (result != DOCA_SUCCESS) {
157  DOCA_LOG_ERR("Failed to allocate RX buffers: %s", doca_error_get_descr(result));
158  rmax_create_stream_cleanup(state, stream, flow, buf);
159  return result;
160  }
161 
162  /* connect the rivermax context to a progress engine and start it */
163  result = rmax_stream_start(state);
164  if (result != DOCA_SUCCESS) {
165  DOCA_LOG_ERR("Failed to start rmax context: %s", doca_error_get_descr(result));
166  rmax_create_stream_cleanup(state, stream, flow, buf);
167  return result;
168  }
169 
170  /* create a flow using DOCA Rmax API */
171  result = doca_rmax_flow_create(&flow);
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Failed to create a flow: %s", doca_error_get_descr(result));
174  rmax_create_stream_cleanup(state, stream, flow, buf);
175  return result;
176  }
177 
178  /* create a DOCA Rmax flow and attach it to the given stream */
179  result = rmax_flow_set_attributes(stream_config, flow);
180  if (result != DOCA_SUCCESS) {
181  DOCA_LOG_ERR("Failed to set flow attributes: %s", doca_error_get_descr(result));
182  rmax_create_stream_cleanup(state, stream, flow, buf);
183  return result;
184  }
185 
186  /* Attach flow to a stream using DOCA Rmax API */
187  result = doca_rmax_flow_attach(flow, stream);
188  if (result != DOCA_SUCCESS) {
189  DOCA_LOG_ERR("Failed to attach a flow: %s", doca_error_get_descr(result));
190  rmax_create_stream_cleanup(state, stream, flow, buf);
191  return result;
192  }
193 
194  state->run_pe_progress = true;
195  state->exit_status = DOCA_SUCCESS;
196 
197  while (state->run_pe_progress)
198  (void)doca_pe_progress(state->core_objects.pe);
199 
200  if (state->exit_status != DOCA_SUCCESS)
201  DOCA_LOG_ERR("Program exiting with failure to receive data. err=%s",
203 
204  /* detach flow */
205  result = doca_rmax_flow_detach(flow, stream);
206  if (result != DOCA_SUCCESS)
207  DOCA_LOG_ERR("Failed to detach flow from stream: %s", doca_error_get_descr(result));
208 
209  /* Clean and destroy all relevant objects */
210  rmax_create_stream_cleanup(state, stream, flow, buf);
211 
212  return DOCA_SUCCESS;
213 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t create_core_objects(struct program_core_objects *state, uint32_t max_bufs)
Definition: common.c:302
static doca_error_t open_doca_device_with_pci(const char *pcie_value, struct doca_dev **retval)
Definition: device.c:43
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_name(doca_error_t error)
Returns the string representation of an error code name.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_IO_FAILED
Definition: doca_error.h:55
@ 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
#define DOCA_LOG_DBG(format,...)
Generates a DEBUG application log message.
Definition: doca_log.h:496
DOCA_STABLE uint8_t doca_pe_progress(struct doca_pe *pe)
Run the progress engine.
char * hex_dump(const void *data, size_t size)
void rmax_create_stream_cleanup(struct rmax_program_state *state, struct doca_rmax_in_stream *stream, struct doca_rmax_flow *flow, struct doca_buf *buf)
Definition: rmax_common.c:375
doca_error_t rmax_stream_allocate_buf(struct rmax_program_state *state, struct doca_rmax_in_stream *stream, struct rmax_stream_config *config, struct doca_buf **buffer, uint16_t *stride_size)
Definition: rmax_common.c:284
doca_error_t rmax_stream_set_attributes(struct doca_rmax_in_stream *stream, struct rmax_stream_config *config)
Definition: rmax_common.c:226
doca_error_t rmax_flow_set_attributes(struct rmax_stream_config *config, struct doca_rmax_flow *flow)
Definition: rmax_common.c:207
doca_error_t rmax_stream_start(struct rmax_program_state *state)
Definition: rmax_common.c:267
doca_error_t rmax_create_stream(struct rmax_program_state *state, struct rmax_stream_config *stream_config)
void rx_success_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
DOCA_LOG_REGISTER(RMAX_CREATE_STREAM)
void rx_error_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
struct doca_pe * pe
Definition: common.h:51
struct doca_dev * dev
Definition: common.h:46
struct doca_ctx * ctx
Definition: common.h:50
struct program_core_objects core_objects
Definition: rmax_common.h:71
struct rmax_stream_config * config
Definition: rmax_common.h:70
uint16_t stride_size[MAX_BUFFERS]
Definition: rmax_common.h:72
doca_error_t exit_status
Definition: rmax_common.h:74
uint16_t data_size
Definition: rmax_common.h:57
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]
Definition: rmax_common.h:52
Convenience type for representing opaque data.
Definition: doca_types.h:56
void * ptr
Definition: doca_types.h:57