NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
icmp_queues.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-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 <stdlib.h>
27 #include <string.h>
28 #include <rte_ethdev.h>
29 
30 #include "common.h"
31 
32 DOCA_LOG_REGISTER(GPU_PACKET_PROCESSING_UDP);
33 
35  struct doca_flow_port *df_port,
36  struct doca_gpu *gpu_dev,
37  struct doca_dev *ddev,
38  uint32_t queue_num,
39  struct doca_pe *pe,
40  doca_eth_txq_gpu_event_error_send_packet_cb_t event_error_send_packet_cb,
41  doca_eth_txq_gpu_event_notify_send_packet_cb_t event_notify_send_packet_cb)
42 {
43  uint32_t cyclic_buffer_size = 0;
45  union doca_data event_user_data[MAX_QUEUES_ICMP] = {0};
46 
47  if (icmp_queues == NULL || df_port == NULL || gpu_dev == NULL || ddev == NULL || queue_num == 0) {
48  DOCA_LOG_ERR("Can't create ICMP queues, invalid input");
50  }
51 
55  icmp_queues->numq = queue_num;
56 
57  for (uint32_t idx = 0; idx < queue_num; idx++) {
58  DOCA_LOG_INFO("Creating ICMP Eth Rxq %d", idx);
59 
63  &(icmp_queues->eth_rxq_cpu[idx]));
64  if (result != DOCA_SUCCESS) {
65  DOCA_LOG_ERR("Failed doca_eth_rxq_create: %s", doca_error_get_descr(result));
66  return DOCA_ERROR_BAD_STATE;
67  }
68 
70  if (result != DOCA_SUCCESS) {
71  DOCA_LOG_ERR("Failed doca_eth_rxq_set_type: %s", doca_error_get_descr(result));
72  return DOCA_ERROR_BAD_STATE;
73  }
74 
76  0,
77  0,
80  0,
81  0,
82  0,
83  &cyclic_buffer_size);
84  if (result != DOCA_SUCCESS) {
85  DOCA_LOG_ERR("Failed to get eth_rxq cyclic buffer size: %s", doca_error_get_descr(result));
87  return DOCA_ERROR_BAD_STATE;
88  }
89 
91  if (result != DOCA_SUCCESS) {
92  DOCA_LOG_ERR("Failed to create mmap: %s", doca_error_get_descr(result));
94  return DOCA_ERROR_BAD_STATE;
95  }
96 
98  if (result != DOCA_SUCCESS) {
99  DOCA_LOG_ERR("Failed to add dev to mmap: %s", doca_error_get_descr(result));
101  return DOCA_ERROR_BAD_STATE;
102  }
103 
104  result = doca_gpu_mem_alloc(icmp_queues->gpu_dev,
105  cyclic_buffer_size,
108  &icmp_queues->gpu_pkt_addr[idx],
109  NULL);
110  if (result != DOCA_SUCCESS || icmp_queues->gpu_pkt_addr[idx] == NULL) {
111  DOCA_LOG_ERR("Failed to allocate gpu memory %s", doca_error_get_descr(result));
113  return DOCA_ERROR_BAD_STATE;
114  }
115 
116  /* Map GPU memory buffer used to receive packets with DMABuf */
117  result = doca_gpu_dmabuf_fd(icmp_queues->gpu_dev,
119  cyclic_buffer_size,
120  &(icmp_queues->dmabuf_fd[idx]));
121  if (result != DOCA_SUCCESS) {
122  DOCA_LOG_INFO("Mapping receive queue buffer (0x%p size %dB) with nvidia-peermem mode",
124  cyclic_buffer_size);
125 
126  /* If failed, use nvidia-peermem method */
129  cyclic_buffer_size);
130  if (result != DOCA_SUCCESS) {
131  DOCA_LOG_ERR("Failed to set memrange for mmap %s", doca_error_get_descr(result));
133  return DOCA_ERROR_BAD_STATE;
134  }
135  } else {
136  DOCA_LOG_INFO("Mapping receive queue buffer (0x%p size %dB dmabuf fd %d) with dmabuf mode",
138  cyclic_buffer_size,
139  icmp_queues->dmabuf_fd[idx]);
140 
142  icmp_queues->dmabuf_fd[idx],
144  0,
145  cyclic_buffer_size);
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("Failed to set dmabuf memrange for mmap %s", doca_error_get_descr(result));
149  return DOCA_ERROR_BAD_STATE;
150  }
151  }
152 
156  if (result != DOCA_SUCCESS) {
157  DOCA_LOG_ERR("Failed to set permissions for mmap %s", doca_error_get_descr(result));
159  return DOCA_ERROR_BAD_STATE;
160  }
161 
163  if (result != DOCA_SUCCESS) {
164  DOCA_LOG_ERR("Failed to start mmap %s", doca_error_get_descr(result));
166  return DOCA_ERROR_BAD_STATE;
167  }
168 
171  0,
172  cyclic_buffer_size);
173  if (result != DOCA_SUCCESS) {
174  DOCA_LOG_ERR("Failed to set cyclic buffer %s", doca_error_get_descr(result));
176  return DOCA_ERROR_BAD_STATE;
177  }
178 
180  if (icmp_queues->eth_rxq_ctx[idx] == NULL) {
181  DOCA_LOG_ERR("Failed doca_eth_rxq_as_doca_ctx: %s", doca_error_get_descr(result));
183  return DOCA_ERROR_BAD_STATE;
184  }
185 
187  if (result != DOCA_SUCCESS) {
188  DOCA_LOG_ERR("Failed doca_ctx_set_datapath_on_gpu: %s", doca_error_get_descr(result));
190  return DOCA_ERROR_BAD_STATE;
191  }
192 
194  if (result != DOCA_SUCCESS) {
195  DOCA_LOG_ERR("Failed doca_ctx_start: %s", doca_error_get_descr(result));
197  return DOCA_ERROR_BAD_STATE;
198  }
199 
201  if (result != DOCA_SUCCESS) {
202  DOCA_LOG_ERR("Failed doca_eth_rxq_get_gpu_handle: %s", doca_error_get_descr(result));
204  return DOCA_ERROR_BAD_STATE;
205  }
206 
208  if (result != DOCA_SUCCESS) {
209  DOCA_LOG_ERR("Failed doca_eth_txq_create: %s", doca_error_get_descr(result));
211  return DOCA_ERROR_BAD_STATE;
212  }
213 
215  if (result != DOCA_SUCCESS) {
216  DOCA_LOG_ERR("Failed to set eth_txq l3 offloads: %s", doca_error_get_descr(result));
218  return DOCA_ERROR_BAD_STATE;
219  }
220 
222  if (icmp_queues->eth_txq_ctx[idx] == NULL) {
223  DOCA_LOG_ERR("Failed doca_eth_txq_as_doca_ctx: %s", doca_error_get_descr(result));
225  return DOCA_ERROR_BAD_STATE;
226  }
227 
229  if (result != DOCA_SUCCESS) {
230  DOCA_LOG_ERR("Failed doca_ctx_set_datapath_on_gpu: %s", doca_error_get_descr(result));
232  return DOCA_ERROR_BAD_STATE;
233  }
234 
235  if (pe != NULL) {
236  event_user_data[idx].u64 = idx;
238  event_error_send_packet_cb,
239  event_user_data[idx]);
240  if (result != DOCA_SUCCESS) {
241  DOCA_LOG_ERR("Unable to set DOCA progress engine callback: %s",
244  return DOCA_ERROR_BAD_STATE;
245  }
246 
248  event_notify_send_packet_cb,
249  event_user_data[idx]);
250  if (result != DOCA_SUCCESS) {
251  DOCA_LOG_ERR("Unable to set DOCA progress engine callback: %s",
254  return DOCA_ERROR_BAD_STATE;
255  }
256 
258  if (result != DOCA_SUCCESS) {
259  DOCA_LOG_ERR("Unable to set DOCA progress engine to DOCA Eth Txq: %s",
262  return DOCA_ERROR_BAD_STATE;
263  }
264  }
265 
267  if (result != DOCA_SUCCESS) {
268  DOCA_LOG_ERR("Failed doca_ctx_start: %s", doca_error_get_descr(result));
270  return DOCA_ERROR_BAD_STATE;
271  }
272 
274  if (result != DOCA_SUCCESS) {
275  DOCA_LOG_ERR("Failed doca_eth_txq_get_gpu_handle: %s", doca_error_get_descr(result));
277  return DOCA_ERROR_BAD_STATE;
278  }
279  }
280 
281  /* Create UDP based flow pipe */
283  if (result != DOCA_SUCCESS) {
284  DOCA_LOG_ERR("Function build_rxq_pipe returned %s", doca_error_get_descr(result));
285  return DOCA_ERROR_BAD_STATE;
286  }
287 
288  return DOCA_SUCCESS;
289 }
290 
292 {
294 
295  if (icmp_queues == NULL) {
296  DOCA_LOG_ERR("Can't destroy ICMP queues, invalid input");
298  }
299 
300  for (int idx = 0; idx < icmp_queues->numq; idx++) {
301  DOCA_LOG_INFO("Destroying ICMP queue %d", idx);
302 
304  if (result != DOCA_SUCCESS) {
305  DOCA_LOG_ERR("Failed doca_ctx_stop: %s", doca_error_get_descr(result));
306  return DOCA_ERROR_BAD_STATE;
307  }
308 
310  if (result != DOCA_SUCCESS) {
311  DOCA_LOG_ERR("Failed doca_eth_rxq_destroy: %s", doca_error_get_descr(result));
312  return DOCA_ERROR_BAD_STATE;
313  }
314 
316  if (result != DOCA_SUCCESS) {
317  DOCA_LOG_ERR("Failed to destroy mmap: %s", doca_error_get_descr(result));
318  return DOCA_ERROR_BAD_STATE;
319  }
320 
321  result = doca_gpu_mem_free(icmp_queues->gpu_dev, icmp_queues->gpu_pkt_addr[idx]);
322  if (result != DOCA_SUCCESS) {
323  DOCA_LOG_ERR("Failed to free gpu memory: %s", doca_error_get_descr(result));
324  return DOCA_ERROR_BAD_STATE;
325  }
326 
328  if (result != DOCA_SUCCESS) {
329  DOCA_LOG_ERR("Failed doca_ctx_stop: %s", doca_error_get_descr(result));
330  return DOCA_ERROR_BAD_STATE;
331  }
332 
334  if (result != DOCA_SUCCESS) {
335  DOCA_LOG_ERR("Failed doca_eth_rxq_destroy: %s", doca_error_get_descr(result));
336  return DOCA_ERROR_BAD_STATE;
337  }
338  }
339 
340  return DOCA_SUCCESS;
341 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t create_icmp_gpu_pipe(struct rxq_icmp_queues *icmp_queues, struct doca_flow_port *port)
Definition: flow.c:526
#define MAX_PKT_NUM_ICMP
Definition: defines.h:68
#define GPU_PAGE_SIZE
Definition: defines.h:58
#define MAX_PKT_SIZE_ICMP
Definition: defines.h:69
#define MAX_QUEUES_ICMP
Definition: defines.h:63
#define MAX_SQ_DESCR_NUM
Definition: defines.h:74
static struct doca_pe * pe
static struct rxq_icmp_queues icmp_queues
static struct doca_flow_port * df_port
static struct doca_gpu * gpu_dev
static struct doca_dev * ddev
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_EXPERIMENTAL doca_error_t doca_ctx_set_datapath_on_gpu(struct doca_ctx *ctx, struct doca_gpu *gpu_dev)
This function binds the DOCA context to a gpu device.
DOCA_STABLE doca_error_t doca_ctx_stop(struct doca_ctx *ctx)
Stops the context allowing reconfiguration.
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_BAD_STATE
Definition: doca_error.h:56
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_eth_txq_gpu_event_notify_send_packet_register(struct doca_eth_txq *eth_txq, doca_eth_txq_gpu_event_notify_send_packet_cb_t event_notify_send_packet_cb, union doca_data event_user_data)
This method registers a doca_eth_txq_gpu_event_notify_send_packet event. can only be called before ca...
DOCA_EXPERIMENTAL doca_error_t doca_eth_txq_gpu_event_error_send_packet_register(struct doca_eth_txq *eth_txq, doca_eth_txq_gpu_event_error_send_packet_cb_t event_error_send_packet_cb, union doca_data event_user_data)
This method registers a doca_eth_txq_gpu_event_error_send_packet event. can only be called before cal...
void(* doca_eth_txq_gpu_event_error_send_packet_cb_t)(struct doca_eth_txq_gpu_event_error_send_packet *event_error, union doca_data event_user_data)
Function to be executed on send packet error event occurrence.
void(* doca_eth_txq_gpu_event_notify_send_packet_cb_t)(struct doca_eth_txq_gpu_event_notify_send_packet *event_notify, union doca_data event_user_data)
Function to be executed on send packet notify event occurrence.
DOCA_EXPERIMENTAL struct doca_ctx * doca_eth_txq_as_doca_ctx(struct doca_eth_txq *eth_txq)
Convert doca_eth_txq instance into a generalized context for use with doca core objects.
DOCA_EXPERIMENTAL doca_error_t doca_eth_txq_get_gpu_handle(const struct doca_eth_txq *eth_txq, struct doca_gpu_eth_txq **eth_txq_ext)
Get a gpu handle of a doca_eth_txq.
DOCA_EXPERIMENTAL doca_error_t doca_eth_txq_create(struct doca_dev *dev, uint32_t max_burst_size, struct doca_eth_txq **eth_txq)
Create a DOCA ETH TXQ instance.
DOCA_EXPERIMENTAL doca_error_t doca_eth_txq_set_l3_chksum_offload(struct doca_eth_txq *eth_txq, uint8_t enable_l3_chksum)
Set offload for the calculation of IPv4 checksum (L3) on transmitted packets. If the users enables L3...
DOCA_EXPERIMENTAL doca_error_t doca_eth_txq_destroy(struct doca_eth_txq *eth_txq)
Destroy a DOCA ETH TXQ instance.
DOCA_EXPERIMENTAL doca_error_t doca_eth_rxq_set_type(struct doca_eth_rxq *eth_rxq, enum doca_eth_rxq_type type)
Set RX queue type property for doca_eth_rxq. can only be called before calling doca_ctx_start().
DOCA_EXPERIMENTAL doca_error_t doca_eth_rxq_destroy(struct doca_eth_rxq *eth_rxq)
Destroy a DOCA ETH RXQ instance.
DOCA_EXPERIMENTAL doca_error_t doca_eth_rxq_get_gpu_handle(const struct doca_eth_rxq *eth_rxq, struct doca_gpu_eth_rxq **eth_rxq_ext)
Get a gpu handle of a doca_eth_rxq.
DOCA_EXPERIMENTAL struct doca_ctx * doca_eth_rxq_as_doca_ctx(struct doca_eth_rxq *eth_rxq)
Convert doca_eth_rxq instance into a generalized context for use with doca core objects.
DOCA_EXPERIMENTAL doca_error_t doca_eth_rxq_create(struct doca_dev *dev, uint32_t max_burst_size, uint32_t max_packet_size, struct doca_eth_rxq **eth_rxq)
Create a DOCA ETH RXQ instance.
DOCA_EXPERIMENTAL doca_error_t doca_eth_rxq_set_pkt_buf(struct doca_eth_rxq *eth_rxq, struct doca_mmap *mmap, uint32_t mmap_offset, uint32_t mmap_len)
Set Eth packet buffer for a doca_eth_rxq. can only be called before calling doca_ctx_start().
DOCA_EXPERIMENTAL doca_error_t doca_eth_rxq_estimate_packet_buf_size(enum doca_eth_rxq_type type, uint32_t rate, uint16_t pkt_max_time, uint32_t max_packet_size, uint32_t max_burst_size, uint8_t log_max_lro_pkt_sz, uint16_t head_size, uint16_t tail_size, uint32_t *buf_size)
Get the recommended size for the mmap buffer of a doca_eth_rxq.
@ DOCA_ETH_RXQ_TYPE_CYCLIC
Definition: doca_eth_rxq.h:65
#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_mmap_set_memrange(struct doca_mmap *mmap, void *addr, size_t len)
Set the memory range of DOCA memory map.
DOCA_STABLE doca_error_t doca_mmap_destroy(struct doca_mmap *mmap)
Destroy DOCA Memory Map structure.
DOCA_STABLE doca_error_t doca_mmap_create(struct doca_mmap **mmap)
Allocates zero size memory map object with default/unset attributes.
DOCA_STABLE doca_error_t doca_mmap_set_permissions(struct doca_mmap *mmap, uint32_t access_mask)
Set access flags of the registered memory.
DOCA_STABLE doca_error_t doca_mmap_start(struct doca_mmap *mmap)
Start DOCA Memory Map.
DOCA_STABLE doca_error_t doca_mmap_add_dev(struct doca_mmap *mmap, struct doca_dev *dev)
Register DOCA memory map on a given device.
DOCA_STABLE doca_error_t doca_mmap_set_dmabuf_memrange(struct doca_mmap *mmap, int dmabuf_fd, void *addr, size_t dmabuf_offset, size_t len)
Set the memory range of DOCA memory map using dmabuf.
DOCA_STABLE doca_error_t doca_pe_connect_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
This method connects a context to a progress engine.
@ DOCA_GPU_MEM_TYPE_GPU
Definition: doca_types.h:131
@ DOCA_ACCESS_FLAG_LOCAL_READ_WRITE
Definition: doca_types.h:83
@ DOCA_ACCESS_FLAG_PCI_RELAXED_ORDERING
Definition: doca_types.h:95
doca_error_t destroy_icmp_queues(struct rxq_icmp_queues *icmp_queues)
Definition: icmp_queues.c:291
doca_error_t create_icmp_queues(struct rxq_icmp_queues *icmp_queues, struct doca_flow_port *df_port, struct doca_gpu *gpu_dev, struct doca_dev *ddev, uint32_t queue_num, struct doca_pe *pe, doca_eth_txq_gpu_event_error_send_packet_cb_t event_error_send_packet_cb, doca_eth_txq_gpu_event_notify_send_packet_cb_t event_notify_send_packet_cb)
Definition: icmp_queues.c:34
DOCA_LOG_REGISTER(GPU_PACKET_PROCESSING_UDP)
struct doca_gpu * gpu_dev
Definition: common.h:100
struct doca_flow_port * port
Definition: common.h:111
uint16_t numq
Definition: common.h:103
struct doca_mmap * pkt_buff_mmap[MAX_QUEUES]
Definition: common.h:107
struct doca_eth_txq * eth_txq_cpu[MAX_QUEUES]
Definition: common.h:115
struct doca_dev * ddev
Definition: common.h:101
void * gpu_pkt_addr[MAX_QUEUES]
Definition: common.h:108
int dmabuf_fd[MAX_QUEUES]
Definition: common.h:109
struct doca_ctx * eth_txq_ctx[MAX_QUEUES]
Definition: common.h:114
struct doca_ctx * eth_rxq_ctx[MAX_QUEUES]
Definition: common.h:104
struct doca_gpu_eth_txq * eth_txq_gpu[MAX_QUEUES]
Definition: common.h:116
struct doca_gpu_eth_rxq * eth_rxq_gpu[MAX_QUEUES]
Definition: common.h:106
struct doca_eth_rxq * eth_rxq_cpu[MAX_QUEUES]
Definition: common.h:105
Convenience type for representing opaque data.
Definition: doca_types.h:56
uint64_t u64
Definition: doca_types.h:58