NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
rmax_create_stream_hds_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_HDS);
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 *header = comp->memblk_ptr_arr[0];
55  uint8_t *payload = comp->memblk_ptr_arr[1];
56 
57  for (size_t i = 0; i < comp->elements_count;
58  ++i, header += state->stride_size[0], payload += state->stride_size[1]) {
59  char *hdr_dump = hex_dump(header, state->config->hdr_size);
60  char *pld_dump = hex_dump(payload, state->config->data_size);
61 
62  DOCA_LOG_DBG("Header:\n%s", hdr_dump);
63  DOCA_LOG_DBG("Payload:\n%s", pld_dump);
64  free(hdr_dump);
65  free(pld_dump);
66  }
67 }
68 
69 /*
70  * Handle a failed Rx data event
71  *
72  * @event_rx_data [in]: the event that occurred. Holds error data.
73  * @event_user_data [in]: user defined data that was previously provided on registration. Holds rmax_program_state.
74  */
75 void rx_error_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
76 {
77  struct doca_rmax_stream_error *err = doca_rmax_in_stream_event_rx_data_get_error(event_rx_data);
78  struct rmax_program_state *state = (struct rmax_program_state *)event_user_data.ptr;
79 
80  DOCA_LOG_ERR("Error in Rx event: code=%d message=%s", err->code, err->message);
81 
82  state->run_pe_progress = false;
84 }
85 
86 /*
87  * Run create_stream_hds sample, which creates stream in header-split mode
88  *
89  * @state [in]: DOCA core related objects
90  * @stream_config [in]: stream configurations
91  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
92  */
94 {
95  struct doca_rmax_in_stream *stream = NULL;
96  struct doca_buf *buf = NULL;
97  struct doca_rmax_flow *flow = NULL;
99 
100  memset(state, 0, sizeof(*state));
101  state->config = stream_config;
102 
103  /* open DOCA device with the given PCI address */
104  result = open_doca_device_with_pci(stream_config->pci_address, NULL, &state->core_objects.dev);
105  if (result != DOCA_SUCCESS)
106  return result;
107 
108  /* DOCA RMAX library Initialization */
109  result = doca_rmax_init();
110  if (result != DOCA_SUCCESS) {
111  DOCA_LOG_ERR("Failed to initialize DOCA RMAX library: %s", doca_error_get_descr(result));
112  rmax_create_stream_cleanup(state, stream, flow, buf);
113  return result;
114  }
115 
116  /* create core DOCA objects */
118  if (result != DOCA_SUCCESS) {
119  DOCA_LOG_ERR("Failed to create DOCA core related objects: %s", doca_error_get_descr(result));
120  rmax_create_stream_cleanup(state, stream, flow, buf);
121  return result;
122  }
123 
124  /* create stream object */
125  result = doca_rmax_in_stream_create(state->core_objects.dev, &stream);
126  if (result != DOCA_SUCCESS) {
127  DOCA_LOG_ERR("Failed to create DOCA Rivermax stream: %s", doca_error_get_descr(result));
128  rmax_create_stream_cleanup(state, stream, flow, buf);
129  return result;
130  }
131 
132  /* set stream attributes, based on received or default configurations */
133  result = rmax_stream_set_attributes(stream, stream_config);
134  if (result != DOCA_SUCCESS) {
135  DOCA_LOG_ERR("Failed to set stream attributes: %s", doca_error_get_descr(result));
136  rmax_create_stream_cleanup(state, stream, flow, buf);
137  return result;
138  }
139 
140  /* Register Rx data event handlers */
141  union doca_data event_user_data;
142 
143  event_user_data.ptr = (void *)state;
144  result = doca_rmax_in_stream_event_rx_data_register(stream, event_user_data, rx_success_cb, rx_error_cb);
145  if (result != DOCA_SUCCESS) {
146  DOCA_LOG_ERR("Failed to register to Rx data event: %s", doca_error_get_descr(result));
147  rmax_create_stream_cleanup(state, stream, flow, buf);
148  return result;
149  }
150 
151  /* convert DOCA Rmax stream to DOCA context */
152  state->core_objects.ctx = doca_rmax_in_stream_as_ctx(stream);
153  if (state->core_objects.ctx == NULL) {
154  DOCA_LOG_ERR("Failed to convert stream to a context");
155  rmax_create_stream_cleanup(state, stream, flow, buf);
156  return result;
157  }
158 
159  /* allocate buffers for received data */
160  result = rmax_stream_allocate_buf(state, stream, stream_config, &buf, state->stride_size);
161  if (result != DOCA_SUCCESS) {
162  DOCA_LOG_ERR("Failed to allocate RX buffers: %s", doca_error_get_descr(result));
163  rmax_create_stream_cleanup(state, stream, flow, buf);
164  return result;
165  }
166 
167  /* connect the rivermax context to a progress engine and start it */
168  result = rmax_stream_start(state);
169  if (result != DOCA_SUCCESS) {
170  DOCA_LOG_ERR("Failed to start rmax context: %s", doca_error_get_descr(result));
171  rmax_create_stream_cleanup(state, stream, flow, buf);
172  return result;
173  }
174 
175  /* create a flow using DOCA Rmax API */
176  result = doca_rmax_flow_create(&flow);
177  if (result != DOCA_SUCCESS) {
178  DOCA_LOG_ERR("Failed to create a flow: %s", doca_error_get_descr(result));
179  rmax_create_stream_cleanup(state, stream, flow, buf);
180  return result;
181  }
182 
183  /* create a DOCA Rmax flow and attach it to the given stream */
184  result = rmax_flow_set_attributes(stream_config, flow);
185  if (result != DOCA_SUCCESS) {
186  DOCA_LOG_ERR("Failed to set flow attributes: %s", doca_error_get_descr(result));
187  rmax_create_stream_cleanup(state, stream, flow, buf);
188  return result;
189  }
190 
191  /* Attach flow to a stream using DOCA Rmax API */
192  result = doca_rmax_flow_attach(flow, stream);
193  if (result != DOCA_SUCCESS) {
194  DOCA_LOG_ERR("Failed to attach a flow: %s", doca_error_get_descr(result));
195  rmax_create_stream_cleanup(state, stream, flow, buf);
196  return result;
197  }
198 
199  state->run_pe_progress = true;
200  state->exit_status = DOCA_SUCCESS;
201 
202  while (state->run_pe_progress)
203  (void)doca_pe_progress(state->core_objects.pe);
204 
205  if (state->exit_status != DOCA_SUCCESS)
206  DOCA_LOG_ERR("Program exiting with failure to receive data. err=%s",
208 
209  /* detach flow */
210  result = doca_rmax_flow_detach(flow, stream);
211  if (result != DOCA_SUCCESS)
212  DOCA_LOG_ERR("Failed to detach flow from stream: %s", doca_error_get_descr(result));
213 
214  /* Clean and destroy all relevant objects */
215  rmax_create_stream_cleanup(state, stream, flow, buf);
216 
217  return DOCA_SUCCESS;
218 }
#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
void rx_success_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
doca_error_t rmax_create_stream_hds(struct rmax_program_state *state, struct rmax_stream_config *stream_config)
DOCA_LOG_REGISTER(RMAX_CREATE_STREAM_HDS)
void rx_error_cb(struct doca_rmax_in_stream_event_rx_data *event_rx_data, union doca_data event_user_data)
#define MAX_BUFFERS
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