NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
dpdk_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-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 #ifndef COMMON_DPDK_UTILS_H_
27 #define COMMON_DPDK_UTILS_H_
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 
34 #include <rte_mbuf.h>
35 #include <rte_flow.h>
36 
37 #include <doca_error.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #define RX_RING_SIZE 1024 /* RX ring size */
44 #define TX_RING_SIZE 1024 /* TX ring size */
45 #define NUM_MBUFS (8 * 1024) /* Number of mbufs to be allocated in the mempool */
46 #define MBUF_CACHE_SIZE 250 /* mempool cache size */
47 
48 struct doca_dev;
49 struct dpdk_mempool_shadow;
50 struct doca_buf_inventory;
51 struct doca_buf;
52 
53 /* Port configuration */
55  int nb_ports; /* Set on init to 0 for don't care, required ports otherwise */
56  uint16_t nb_queues; /* Set on init to 0 for don't care, required minimum cores otherwise */
57  int nb_hairpin_q; /* Set on init to 0 to disable, hairpin queues otherwise */
58  uint16_t mbuf_size; /* Set on init to 0 for don't care, packet mbuf size (including headroom) otherwise */
59  uint64_t tx_offloads; /* Set on init to 0 for don't care, port tx offload flags otherwise */
60  uint16_t enable_mbuf_metadata : 1; /* Set on init to 0 to disable, otherwise it will add meta to each mbuf */
61  uint16_t self_hairpin : 1; /* Set on init to 1 enable both self and peer hairpin */
62  uint16_t rss_support : 1; /* Set on init to 0 for no RSS support, RSS support otherwise */
63  uint16_t lpbk_support : 1; /* Enable loopback support */
64  uint16_t isolated_mode : 1; /* Set on init to 0 for no isolation, isolated mode otherwise */
65  uint16_t switch_mode : 1; /* Set on init to 1 for switch mode */
66 };
67 
68 /* DPDK configuration */
70  struct application_port_config port_config; /* DPDK port configuration */
71  bool reserve_main_thread; /* Reserve lcore for the main thread */
72  struct rte_mempool *mbuf_pool; /* Will be filled by "dpdk_queues_and_ports_init".
73  * Memory pool that will be used by the DPDK ports
74  * for allocating rte_pktmbuf
75  */
76 };
77 
78 /*
79  * Initialize DPDK environment
80  *
81  * @argc [in]: number of program command line arguments
82  * @argv [in]: program command line arguments
83  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
84  */
85 doca_error_t dpdk_init(int argc, char **argv);
86 
87 /*
88  * Destroy DPDK environment
89  */
90 void dpdk_fini(void);
91 
92 /*
93  * Initialize DPDK ports and queues
94  *
95  * @app_dpdk_config [in/out]: application DPDK configuration values
96  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
97  */
99 
100 /*
101  * Destroy DPDK ports and queues
102  *
103  * @app_dpdk_config [in]: application DPDK configuration values
104  */
105 void dpdk_queues_and_ports_fini(struct application_dpdk_config *app_dpdk_config);
106 
107 /*
108  * Initialize a shadow of a DPDK memory pool the shadow will have all of DPDK's memory registered to a device
109  *
110  * @mbuf_pool [in]: the DPDK memory pool
111  * @device [in]: DOCA device for memory registration
112  * @return: dpdk_mempool_shadow with memory of the DPDK pool registered on success, NULL otherwise
113  */
114 struct dpdk_mempool_shadow *dpdk_mempool_shadow_create(struct rte_mempool *mbuf_pool, struct doca_dev *device);
115 
116 /*
117  * Initialize a shadow of a DPDK memory pool the shadow will have all of RTE external memory
118  *
119  * @ext_mem [in]: Pointer to the array of structures describing the external memory for data buffers.
120  * @ext_num [in]: Number of elements in the ext_mem array.
121  * @device [in]: DOCA device for memory registration
122  * @return: dpdk_mempool_shadow with memory of the DPDK pool registered on success, NULL otherwise
123  */
124 struct dpdk_mempool_shadow *dpdk_mempool_shadow_create_extbuf(const struct rte_pktmbuf_extmem **ext_mem,
125  uint32_t ext_num,
126  struct doca_dev *device);
127 
128 /*
129  * Find the DOCA mmap instance that contains the requested address range then allocate DOCA buffer from it
130  *
131  * @mempool_shadow [in]: shadow of a DPDK memory pool
132  * @inventory [in]: a DOCA buffer inventory used for allocating the buffer
133  * @mem_range_start [in]: start address of memory range, must be within range of some 'rte_mbuf'
134  * @mem_range_size [in]: the size of the memory range in bytes, must be within range of some 'rte_mbuf'
135  * @out_buf [out]: DOCA buffer allocated with data pointing to the given range
136  * @return: DOCA_SUCCESS on success, and doca_error_t otherwise
137  */
139  struct doca_buf_inventory *inventory,
140  uintptr_t mem_range_start,
141  size_t mem_range_size,
142  struct doca_buf **out_buf);
143 
144 /*
145  * Destroy the DPDK memory pool shadow
146  *
147  * @mempool_shadow [in]: shadow of a DPDK memory pool to be destroyed
148  * @Note: this should be done before destroying the DPDK memory pool
149  */
150 void dpdk_mempool_shadow_destroy(struct dpdk_mempool_shadow *mempool_shadow);
151 
152 /*
153  * Print packet header information
154  *
155  * @packet [in]: packet mbuf
156  * @l2 [in]: if true the function prints l2 header
157  * @l3 [in]: if true the function prints l3 header
158  * @l4 [in]: if true the function prints l4 header
159  */
160 void print_header_info(const struct rte_mbuf *packet, const bool l2, const bool l3, const bool l4);
161 
162 #ifdef __cplusplus
163 } /* extern "C" */
164 #endif
165 
166 #endif /* COMMON_DPDK_UTILS_H_ */
void dpdk_mempool_shadow_destroy(struct dpdk_mempool_shadow *mempool_shadow)
Definition: dpdk_utils.c:857
doca_error_t dpdk_init(int argc, char **argv)
Definition: dpdk_utils.c:907
struct dpdk_mempool_shadow * dpdk_mempool_shadow_create_extbuf(const struct rte_pktmbuf_extmem **ext_mem, uint32_t ext_num, struct doca_dev *device)
Definition: dpdk_utils.c:824
void dpdk_fini(void)
Definition: dpdk_utils.c:919
doca_error_t dpdk_queues_and_ports_init(struct application_dpdk_config *app_dpdk_config)
Definition: dpdk_utils.c:515
void dpdk_queues_and_ports_fini(struct application_dpdk_config *app_dpdk_config)
Definition: dpdk_utils.c:564
doca_error_t dpdk_mempool_shadow_find_buf_by_data(struct dpdk_mempool_shadow *mempool_shadow, struct doca_buf_inventory *inventory, uintptr_t mem_range_start, size_t mem_range_size, struct doca_buf **out_buf)
Definition: dpdk_utils.c:869
struct dpdk_mempool_shadow * dpdk_mempool_shadow_create(struct rte_mempool *mbuf_pool, struct doca_dev *device)
Definition: dpdk_utils.c:749
void print_header_info(const struct rte_mbuf *packet, const bool l2, const bool l3, const bool l4)
Definition: dpdk_utils.c:897
enum doca_error doca_error_t
DOCA API return codes.
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:298
struct rte_mempool * mbuf_pool
Definition: dpdk_utils.h:72
struct application_port_config port_config
Definition: dpdk_utils.h:70
uint16_t enable_mbuf_metadata
Definition: dpdk_utils.h:60
struct doca_dev * device
Definition: dpdk_utils.c:50