NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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 #ifndef COMMON_H_
27 #define COMMON_H_
28 
29 #include <stdbool.h>
30 
31 #include <doca_buf.h>
32 #include <doca_buf_inventory.h>
33 #include <doca_dev.h>
34 #include <doca_error.h>
35 #include <doca_mmap.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* Function to check if a given device is capable of executing some task */
42 typedef doca_error_t (*tasks_check)(struct doca_devinfo *);
43 
44 /* DOCA core objects used by the samples / applications */
46  struct doca_dev *dev; /* doca device */
47  struct doca_mmap *src_mmap; /* doca mmap for source buffer */
48  struct doca_mmap *dst_mmap; /* doca mmap for destination buffer */
49  struct doca_buf_inventory *buf_inv; /* doca buffer inventory */
50  struct doca_ctx *ctx; /* doca context */
51  struct doca_pe *pe; /* doca progress engine */
52 };
53 
54 /*
55  * Open a DOCA device according to a given PCI address
56  *
57  * @pci_addr [in]: PCI address
58  * @func [in]: pointer to a function that checks if the device have some task capabilities (Ignored if set to NULL)
59  * @retval [out]: pointer to doca_dev struct, NULL if not found
60  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
61  */
62 doca_error_t open_doca_device_with_pci(const char *pci_addr, tasks_check func, struct doca_dev **retval);
63 
64 /*
65  * Open a DOCA device according to a given IB device name
66  *
67  * @value [in]: IB device name
68  * @val_size [in]: input length, in bytes
69  * @func [in]: pointer to a function that checks if the device have some task capabilities (Ignored if set to NULL)
70  * @retval [out]: pointer to doca_dev struct, NULL if not found
71  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
72  */
74  size_t val_size,
75  tasks_check func,
76  struct doca_dev **retval);
77 
78 /*
79  * Open a DOCA device according to a given interface name
80  *
81  * @value [in]: interface name
82  * @val_size [in]: input length, in bytes
83  * @func [in]: pointer to a function that checks if the device have some task capabilities (Ignored if set to NULL)
84  * @retval [out]: pointer to doca_dev struct, NULL if not found
85  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
86  */
88  size_t val_size,
89  tasks_check func,
90  struct doca_dev **retval);
91 
92 /*
93  * Open a DOCA device with a custom set of capabilities
94  *
95  * @func [in]: pointer to a function that checks if the device have some task capabilities
96  * @retval [out]: pointer to doca_dev struct, NULL if not found
97  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
98  */
99 doca_error_t open_doca_device_with_capabilities(tasks_check func, struct doca_dev **retval);
100 
101 /*
102  * Open a DOCA device representor according to a given VUID string
103  *
104  * @local [in]: queries representors of the given local doca device
105  * @filter [in]: bitflags filter to narrow the representors in the search
106  * @value [in]: IB device name
107  * @val_size [in]: input length, in bytes
108  * @retval [out]: pointer to doca_dev_rep struct, NULL if not found
109  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
110  */
111 doca_error_t open_doca_device_rep_with_vuid(struct doca_dev *local,
112  enum doca_devinfo_rep_filter filter,
113  const uint8_t *value,
114  size_t val_size,
115  struct doca_dev_rep **retval);
116 
117 /*
118  * Open a DOCA device according to a given PCI address
119  *
120  * @local [in]: queries representors of the given local doca device
121  * @filter [in]: bitflags filter to narrow the representors in the search
122  * @pci_addr [in]: PCI address
123  * @retval [out]: pointer to doca_dev_rep struct, NULL if not found
124  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
125  */
126 doca_error_t open_doca_device_rep_with_pci(struct doca_dev *local,
127  enum doca_devinfo_rep_filter filter,
128  const char *pci_addr,
129  struct doca_dev_rep **retval);
130 
131 /*
132  * Initialize a series of DOCA Core objects needed for the program's execution
133  *
134  * @state [in]: struct containing the set of initialized DOCA Core objects
135  * @max_bufs [in]: maximum number of buffers for DOCA Inventory
136  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
137  */
138 doca_error_t create_core_objects(struct program_core_objects *state, uint32_t max_bufs);
139 
140 /*
141  * Request to stop context
142  *
143  * @pe [in]: DOCA progress engine
144  * @ctx [in]: DOCA context added to the progress engine
145  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
146  */
147 doca_error_t request_stop_ctx(struct doca_pe *pe, struct doca_ctx *ctx);
148 
149 /*
150  * Cleanup the series of DOCA Core objects created by create_core_objects
151  *
152  * @state [in]: struct containing the set of initialized DOCA Core objects
153  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
154  */
156 
157 /*
158  * Create a string Hex dump representation of the given input buffer
159  *
160  * @data [in]: Pointer to the input buffer
161  * @size [in]: Number of bytes to be analyzed
162  * @return: pointer to the string representation, or NULL if an error was encountered
163  */
164 char *hex_dump(const void *data, size_t size);
165 
173 uint64_t align_up_uint64(uint64_t value, uint64_t alignment);
174 
182 uint64_t align_down_uint64(uint64_t value, uint64_t alignment);
183 
184 /*
185  * Allocate DOCA buf list
186  *
187  * @buf_inv [in]: Doca_buf_inventory instance
188  * @mmap [in]: Mmap instance
189  * @buf_addr [in]: Start address of the data buffer
190  * @buf_len [in]: Byte length of the data buffer
191  * @num_buf [in]: Number of doca_buf to allocate
192  * @set_data_pos [in]: Whether need to set the data position of a doca_buf
193  * @dbuf [out]: The head of allocated doca_buf list
194  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
195  */
196 doca_error_t allocat_doca_buf_list(struct doca_buf_inventory *buf_inv,
197  struct doca_mmap *mmap,
198  void *buf_addr,
199  size_t buf_len,
200  int num_buf,
201  bool set_data_pos,
202  struct doca_buf **dbuf);
203 
204 #ifdef __cplusplus
205 } /* extern "C" */
206 #endif
207 
208 #endif
doca_dpa_dev_mmap_t mmap
static struct doca_pe * pe
doca_devinfo_rep_filter
Definition: doca_dev.h:65
enum doca_error doca_error_t
DOCA API return codes.
type value
doca_error_t open_doca_device_with_pci(const char *pci_addr, tasks_check func, struct doca_dev **retval)
Definition: common.c:43
doca_error_t allocat_doca_buf_list(struct doca_buf_inventory *buf_inv, struct doca_mmap *mmap, void *buf_addr, size_t buf_len, int num_buf, bool set_data_pos, struct doca_buf **dbuf)
Definition: common.c:527
char * hex_dump(const void *data, size_t size)
doca_error_t request_stop_ctx(struct doca_pe *pe, struct doca_ctx *ctx)
Definition: common.c:367
doca_error_t open_doca_device_rep_with_vuid(struct doca_dev *local, enum doca_devinfo_rep_filter filter, const uint8_t *value, size_t val_size, struct doca_dev_rep **retval)
Definition: common.c:222
doca_error_t(* tasks_check)(struct doca_devinfo *)
Definition: common.h:42
doca_error_t open_doca_device_with_iface_name(const uint8_t *value, size_t val_size, tasks_check func, struct doca_dev **retval)
Definition: common.c:136
doca_error_t open_doca_device_rep_with_pci(struct doca_dev *local, enum doca_devinfo_rep_filter filter, const char *pci_addr, struct doca_dev_rep **retval)
Definition: common.c:267
uint64_t align_up_uint64(uint64_t value, uint64_t alignment)
Definition: common.c:512
doca_error_t destroy_core_objects(struct program_core_objects *state)
Definition: common.c:392
doca_error_t create_core_objects(struct program_core_objects *state, uint32_t max_bufs)
Definition: common.c:302
doca_error_t open_doca_device_with_ibdev_name(const uint8_t *value, size_t val_size, tasks_check func, struct doca_dev **retval)
Definition: common.c:84
doca_error_t open_doca_device_with_capabilities(tasks_check func, struct doca_dev **retval)
Definition: common.c:188
uint64_t align_down_uint64(uint64_t value, uint64_t alignment)
Definition: common.c:522
struct doca_pe * pe
Definition: common.h:51
struct doca_mmap * src_mmap
Definition: common.h:47
struct doca_buf_inventory * buf_inv
Definition: common.h:49
struct doca_dev * dev
Definition: common.h:46
struct doca_mmap * dst_mmap
Definition: common.h:48
struct doca_ctx * ctx
Definition: common.h:50
struct upf_accel_ctx * ctx