NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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 FLOW_COMMON_H_
27 #define FLOW_COMMON_H_
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <rte_byteorder.h>
34 #include <rte_common.h>
35 
36 #include <doca_flow.h>
37 #include <doca_dev.h>
38 
39 #define BE_IPV4_ADDR(a, b, c, d) (RTE_BE32(((uint32_t)a << 24) + (b << 16) + (c << 8) + d)) /* create IPV4 address */
40 #define SET_IPV6_ADDR(addr, a, b, c, d) \
41  do { \
42  addr[0] = a & 0xffffffff; \
43  addr[1] = b & 0xffffffff; \
44  addr[2] = c & 0xffffffff; \
45  addr[3] = d & 0xffffffff; \
46  } while (0) /* create IPv6 address */
47 #define SET_MAC_ADDR(addr, a, b, c, d, e, f) \
48  do { \
49  addr[0] = a & 0xff; \
50  addr[1] = b & 0xff; \
51  addr[2] = c & 0xff; \
52  addr[3] = d & 0xff; \
53  addr[4] = e & 0xff; \
54  addr[5] = f & 0xff; \
55  } while (0) /* create source mac address */
56 #define BUILD_VNI(uint24_vni) (RTE_BE32((uint32_t)uint24_vni << 8)) /* create VNI */
57 #define DEFAULT_TIMEOUT_US (10000) /* default timeout for processing entries */
58 #define NB_ACTIONS_ARR (1) /* default length for action array */
59 #define SHARED_RESOURCE_NUM_VALUES (8) /* Number of doca_flow_shared_resource_type values */
60 
61 #ifndef MAX
62 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
63 #endif
64 #define MIN_ACTIONS_MEM_SIZE_PER_QUEUE (256) /* Minimal actions memory size required per queue */
65 #define INITIAL_ACTIONS_MEM_SIZE (2048) /* Needed when app creates a small number of entries */
66 #define ACTIONS_MEM_SIZE(nr_queues, entries) \
67  rte_align32pow2( \
68  MAX((uint32_t)(entries * DOCA_FLOW_MAX_ENTRY_ACTIONS_MEM_SIZE), \
69  (uint32_t)(nr_queues * MIN_ACTIONS_MEM_SIZE_PER_QUEUE + INITIAL_ACTIONS_MEM_SIZE))) /* Total \
70  actions \
71  memory size \
72  */
73 #define ARRAY_DIM(a) (sizeof(a) / sizeof((a)[0]))
74 #define ARRAY_INIT(array, val) \
75  do { \
76  for (size_t i = 0; i < ARRAY_DIM(array); i++) { \
77  array[i] = val; \
78  } \
79  } while (0)
80 #define DEFS_REG_OPCODE(opcode_str, ___sname, ___field) \
81  do { \
82  int rc; \
83  int __off = offsetof(struct ___sname, ___field); \
84  int __sz = sizeof(((struct ___sname *)0)->___field); \
85 \
86  rc = doca_flow_definitions_add_field(defs, opcode_str, __off, __sz); \
87  if (rc < 0) \
88  return rc; \
89  } while (0)
90 
91 /* user context struct that will be used in entries process callback */
92 struct entries_status {
93  bool failure; /* will be set to true if some entry status will not be success */
94  int nb_processed; /* will hold the number of entries that was already processed */
95 };
96 
97 /* User struct that hold number of counters and meters to configure for doca_flow */
98 struct flow_resources {
99  uint32_t nr_counters; /* number of counters to configure */
100  uint32_t nr_meters; /* number of traffic meters to configure */
101 };
102 
103 /*
104  * Initialize DOCA Flow library
105  *
106  * @nb_queues [in]: number of queues the sample will use
107  * @mode [in]: doca flow architecture mode
108  * @resource [in]: number of meters and counters to configure
109  * @nr_shared_resources [in]: total shared resource per type
110  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
111  */
112 doca_error_t init_doca_flow(int nb_queues,
113  const char *mode,
114  struct flow_resources *resource,
115  uint32_t nr_shared_resources[]);
116 
117 /*
118  * Initialize DOCA Flow library with defs
119  *
120  * @nb_queues [in]: number of queues the sample will use
121  * @mode [in]: doca flow architecture mode
122  * @resource [in]: number of meters and counters to configure
123  * @nr_shared_resources [in]: total shared resource per type
124  * @defs: doca flow configured definitions to be set
125  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
126  */
128  const char *mode,
129  struct flow_resources *resource,
130  uint32_t nr_shared_resources[],
131  struct doca_flow_definitions *defs);
132 
133 /*
134  * Initialize DOCA Flow library with callback
135  *
136  * @nb_queues [in]: number of queues the sample will use
137  * @mode [in]: doca flow architecture mode
138  * @resource [in]: number of meters and counters to configure
139  * @nr_shared_resources [in]: total shared resource per type
140  * @cb [in]: entry process callback pointer
141  * @pipe_process_cb [in]: pipe process callback pointer
142  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
143  */
144 doca_error_t init_doca_flow_cb(int nb_queues,
145  const char *mode,
146  struct flow_resources *resource,
147  uint32_t nr_shared_resources[],
150  struct doca_flow_definitions *defs);
151 
152 /*
153  * Initialize DOCA Flow ports
154  *
155  * @nb_ports [in]: number of ports to create
156  * @ports [in]: array of ports to create
157  * @is_hairpin [in]: port pair should run if is_hairpin = true
158  * @dev_arr [in]: doca device array for each port
159  * @actions_mem_size[in]: array of actions memory size
160  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
161  */
163  struct doca_flow_port *ports[],
164  bool is_hairpin,
165  struct doca_dev *dev_arr[],
166  uint32_t actions_mem_size[]);
167 
168 /*
169  * Initialize DOCA Flow ports with operation state
170  *
171  * @nb_ports [in]: number of ports to create
172  * @ports [in]: array of ports to create
173  * @is_hairpin [in]: port pair should run if is_hairpin = true
174  * @dev_arr [in]: doca device array for each port
175  * @states [in]: operation states array for each port
176  * @actions_mem_size[in]: actions memory size
177  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
178  */
180  struct doca_flow_port *ports[],
181  bool is_hairpin,
182  struct doca_dev *dev_arr[],
183  enum doca_flow_port_operation_state *states,
184  uint32_t actions_mem_size[]);
185 
186 /*
187  * Stop DOCA Flow ports
188  *
189  * @nb_ports [in]: number of ports to stop
190  * @ports [in]: array of ports to stop
191  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
192  */
193 doca_error_t stop_doca_flow_ports(int nb_ports, struct doca_flow_port *ports[]);
194 
195 /*
196  * Entry processing callback
197  *
198  * @entry [in]: DOCA Flow entry pointer
199  * @pipe_queue [in]: queue identifier
200  * @status [in]: DOCA Flow entry status
201  * @op [in]: DOCA Flow entry operation
202  * @user_ctx [out]: user context
203  */
204 void check_for_valid_entry(struct doca_flow_pipe_entry *entry,
205  uint16_t pipe_queue,
206  enum doca_flow_entry_status status,
207  enum doca_flow_entry_op op,
208  void *user_ctx);
209 
210 /*
211  * Set DOCA Flow pipe configurations
212  *
213  * @cfg [in]: DOCA Flow pipe configurations
214  * @name [in]: Pipe name
215  * @type [in]: Pipe type
216  * @is_root [in]: Indicates if the pipe is a root pipe
217  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
218  */
219 doca_error_t set_flow_pipe_cfg(struct doca_flow_pipe_cfg *cfg,
220  const char *name,
222  bool is_root);
223 
224 /*
225  * Process entries and check their status.
226  *
227  * @port [in]: DOCA Flow port structure.
228  * @status [in]: user context struct provided in entries adding.
229  * @nr_entries [in]: number of entries to process.
230  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
231  */
232 doca_error_t flow_process_entries(struct doca_flow_port *port, struct entries_status *status, uint32_t nr_entries);
233 
234 #ifdef __cplusplus
235 } /* extern "C" */
236 #endif
237 
238 #endif /* FLOW_COMMON_H_ */
static void pipe_process_cb(struct doca_flow_pipe *pipe, enum doca_flow_pipe_status status, enum doca_flow_pipe_op op, void *user_ctx)
static struct doca_flow_pipe_entry * entry[MAX_ENTRIES]
enum doca_error doca_error_t
DOCA API return codes.
doca_flow_port_operation_state
Defines the operation states for a port instance.
Definition: doca_flow.h:1147
doca_flow_entry_op
doca flow entry operation
Definition: doca_flow.h:146
doca_flow_pipe_type
doca flow pipe type
Definition: doca_flow.h:220
void(* doca_flow_pipe_process_cb)(struct doca_flow_pipe *pipe, enum doca_flow_pipe_status status, enum doca_flow_pipe_op op, void *user_ctx)
doca flow pipe process callback
Definition: doca_flow.h:196
doca_flow_entry_status
doca flow entry status
Definition: doca_flow.h:160
void(* doca_flow_entry_process_cb)(struct doca_flow_pipe_entry *entry, uint16_t pipe_queue, enum doca_flow_entry_status status, enum doca_flow_entry_op op, void *user_ctx)
doca flow entry process callback
Definition: doca_flow.h:204
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
uint8_t type
Definition: packets.h:0
doca_error_t flow_process_entries(struct doca_flow_port *port, struct entries_status *status, uint32_t nr_entries)
Definition: flow_common.c:338
doca_error_t init_doca_flow(int nb_queues, const char *mode, struct flow_resources *resource, uint32_t nr_shared_resources[])
Definition: flow_common.c:62
doca_error_t stop_doca_flow_ports(int nb_ports, struct doca_flow_port *ports[])
Definition: flow_common.c:240
doca_error_t init_doca_flow_with_defs(int nb_queues, const char *mode, struct flow_resources *resource, uint32_t nr_shared_resources[], struct doca_flow_definitions *defs)
Definition: flow_common.c:70
void check_for_valid_entry(struct doca_flow_pipe_entry *entry, uint16_t pipe_queue, enum doca_flow_entry_status status, enum doca_flow_entry_op op, void *user_ctx)
Definition: flow_common.c:44
doca_error_t init_doca_flow_ports_with_op_state(int nb_ports, struct doca_flow_port *ports[], bool is_hairpin, struct doca_dev *dev_arr[], enum doca_flow_port_operation_state *states, uint32_t actions_mem_size[])
Definition: flow_common.c:260
doca_error_t init_doca_flow_ports(int nb_ports, struct doca_flow_port *ports[], bool is_hairpin, struct doca_dev *dev_arr[], uint32_t actions_mem_size[])
Definition: flow_common.c:296
doca_error_t set_flow_pipe_cfg(struct doca_flow_pipe_cfg *cfg, const char *name, enum doca_flow_pipe_type type, bool is_root)
Definition: flow_common.c:305
doca_error_t init_doca_flow_cb(int nb_queues, const char *mode, struct flow_resources *resource, uint32_t nr_shared_resources[], doca_flow_entry_process_cb cb, doca_flow_pipe_process_cb pipe_process_cb, struct doca_flow_definitions *defs)
Definition: flow_common.c:79
user context struct that will be used in entries process callback
Definition: flow_common.h:78
uint32_t nr_counters
Definition: flow_common.h:96
uint32_t nr_meters
Definition: flow_common.h:97
static int nb_ports
Definition: switch_core.c:44
static uint32_t actions_mem_size[FLOW_SWITCH_PORTS_MAX]
Definition: switch_core.c:43
static struct doca_flow_port * ports[FLOW_SWITCH_PORTS_MAX]
Definition: switch_core.c:42