xref: /dpdk/drivers/net/ntnic/include/flow_api_engine.h (revision 9bd46cf2599ed080b3a64a6b9295b12f61eb553d)
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(c) 2023 Napatech A/S
4  */
5 
6 #ifndef _FLOW_API_ENGINE_H_
7 #define _FLOW_API_ENGINE_H_
8 
9 #include <stdint.h>
10 #include <stdatomic.h>
11 
12 #include "hw_mod_backend.h"
13 #include "stream_binary_flow_api.h"
14 
15 /*
16  * Resource management
17  */
18 #define BIT_CONTAINER_8_ALIGN(x) (((x) + 7) / 8)
19 
20 /*
21  * Resource management
22  * These are free resources in FPGA
23  * Other FPGA memory lists are linked to one of these
24  * and will implicitly follow them
25  */
26 enum res_type_e {
27 	RES_QUEUE,
28 	RES_CAT_CFN,
29 	RES_CAT_COT,
30 	RES_CAT_EXO,
31 	RES_CAT_LEN,
32 	RES_KM_FLOW_TYPE,
33 	RES_KM_CATEGORY,
34 	RES_HSH_RCP,
35 	RES_PDB_RCP,
36 	RES_QSL_RCP,
37 	RES_QSL_QST,
38 	RES_SLC_LR_RCP,
39 
40 	RES_FLM_FLOW_TYPE,
41 	RES_FLM_RCP,
42 	RES_TPE_RCP,
43 	RES_TPE_EXT,
44 	RES_TPE_RPL,
45 	RES_SCRUB_RCP,
46 	RES_COUNT,
47 	RES_INVALID
48 };
49 
50 /*
51  * Flow NIC offload management
52  */
53 #define MAX_OUTPUT_DEST (128)
54 
55 #define MAX_WORD_NUM 24
56 #define MAX_BANKS 6
57 
58 #define MAX_TCAM_START_OFFSETS 4
59 
60 #define MAX_CPY_WRITERS_SUPPORTED 8
61 
62 #define MAX_MATCH_FIELDS 16
63 
64 /*
65  *          128      128     32     32    32
66  * Have  |  QW0  ||  QW4  || SW8 || SW9 | SWX   in FPGA
67  *
68  * Each word may start at any offset, though
69  * they are combined in chronological order, with all enabled to
70  * build the extracted match data, thus that is how the match key
71  * must be build
72  */
73 enum extractor_e {
74 	KM_USE_EXTRACTOR_UNDEF,
75 	KM_USE_EXTRACTOR_QWORD,
76 	KM_USE_EXTRACTOR_SWORD,
77 };
78 
79 struct match_elem_s {
80 	enum extractor_e extr;
81 	int masked_for_tcam;	/* if potentially selected for TCAM */
82 	uint32_t e_word[4];
83 	uint32_t e_mask[4];
84 
85 	int extr_start_offs_id;
86 	int8_t rel_offs;
87 	uint32_t word_len;
88 };
89 
90 enum cam_tech_use_e {
91 	KM_CAM,
92 	KM_TCAM,
93 	KM_SYNERGY
94 };
95 
96 struct km_flow_def_s {
97 	struct flow_api_backend_s *be;
98 
99 	/* For keeping track of identical entries */
100 	struct km_flow_def_s *reference;
101 	struct km_flow_def_s *root;
102 
103 	/* For collect flow elements and sorting */
104 	struct match_elem_s match[MAX_MATCH_FIELDS];
105 	struct match_elem_s *match_map[MAX_MATCH_FIELDS];
106 	int num_ftype_elem;
107 
108 	/* Finally formatted CAM/TCAM entry */
109 	enum cam_tech_use_e target;
110 	uint32_t entry_word[MAX_WORD_NUM];
111 	uint32_t entry_mask[MAX_WORD_NUM];
112 	int key_word_size;
113 
114 	/* TCAM calculated possible bank start offsets */
115 	int start_offsets[MAX_TCAM_START_OFFSETS];
116 	int num_start_offsets;
117 
118 	/* Flow information */
119 	/* HW input port ID needed for compare. In port must be identical on flow types */
120 	uint32_t port_id;
121 	uint32_t info;	/* used for color (actions) */
122 	int info_set;
123 	int flow_type;	/* 0 is illegal and used as unset */
124 	int flushed_to_target;	/* if this km entry has been finally programmed into NIC hw */
125 
126 	/* CAM specific bank management */
127 	int cam_paired;
128 	int record_indexes[MAX_BANKS];
129 	int bank_used;
130 	uint32_t *cuckoo_moves;	/* for CAM statistics only */
131 	struct cam_distrib_s *cam_dist;
132 
133 	/* TCAM specific bank management */
134 	struct tcam_distrib_s *tcam_dist;
135 	int tcam_start_bank;
136 	int tcam_record;
137 };
138 
139 /*
140  * Tunnel encapsulation header definition
141  */
142 #define MAX_TUN_HDR_SIZE 128
143 
144 struct tunnel_header_s {
145 	union {
146 		uint8_t hdr8[MAX_TUN_HDR_SIZE];
147 		uint32_t hdr32[(MAX_TUN_HDR_SIZE + 3) / 4];
148 	} d;
149 
150 	uint8_t len;
151 
152 	uint8_t nb_vlans;
153 
154 	uint8_t ip_version;	/* 4: v4, 6: v6 */
155 
156 	uint8_t new_outer;
157 	uint8_t l2_len;
158 	uint8_t l3_len;
159 	uint8_t l4_len;
160 };
161 
162 enum flow_port_type_e {
163 	PORT_NONE,	/* not defined or drop */
164 	PORT_INTERNAL,	/* no queues attached */
165 	PORT_PHY,	/* MAC phy output queue */
166 	PORT_VIRT,	/* Memory queues to Host */
167 };
168 
169 struct output_s {
170 	uint32_t owning_port_id;/* the port who owns this output destination */
171 	enum flow_port_type_e type;
172 	int id;	/* depending on port type: queue ID or physical port id or not used */
173 	int active;	/* activated */
174 };
175 
176 struct nic_flow_def {
177 	/*
178 	 * Frame Decoder match info collected
179 	 */
180 	int l2_prot;
181 	int l3_prot;
182 	int l4_prot;
183 	int tunnel_prot;
184 	int tunnel_l3_prot;
185 	int tunnel_l4_prot;
186 	int vlans;
187 	int fragmentation;
188 	int ip_prot;
189 	int tunnel_ip_prot;
190 	/*
191 	 * Additional meta data for various functions
192 	 */
193 	int in_port_override;
194 	int non_empty;	/* default value is -1; value 1 means flow actions update */
195 	struct output_s dst_id[MAX_OUTPUT_DEST];/* define the output to use */
196 	/* total number of available queues defined for all outputs - i.e. number of dst_id's */
197 	int dst_num_avail;
198 
199 	/*
200 	 * Mark or Action info collection
201 	 */
202 	uint32_t mark;
203 
204 	uint32_t jump_to_group;
205 
206 	int full_offload;
207 
208 	/*
209 	 * Action push tunnel
210 	 */
211 	struct tunnel_header_s tun_hdr;
212 
213 	/*
214 	 * If DPDK RTE tunnel helper API used
215 	 * this holds the tunnel if used in flow
216 	 */
217 	struct tunnel_s *tnl;
218 
219 	/*
220 	 * Header Stripper
221 	 */
222 	int header_strip_end_dyn;
223 	int header_strip_end_ofs;
224 
225 	/*
226 	 * Modify field
227 	 */
228 	struct {
229 		uint32_t select;
230 		uint32_t dyn;
231 		uint32_t ofs;
232 		uint32_t len;
233 		uint32_t level;
234 		union {
235 			uint8_t value8[16];
236 			uint16_t value16[8];
237 			uint32_t value32[4];
238 		};
239 	} modify_field[MAX_CPY_WRITERS_SUPPORTED];
240 
241 	uint32_t modify_field_count;
242 	uint8_t ttl_sub_enable;
243 	uint8_t ttl_sub_ipv4;
244 	uint8_t ttl_sub_outer;
245 
246 	/*
247 	 * Key Matcher flow definitions
248 	 */
249 	struct km_flow_def_s km;
250 };
251 
252 enum flow_handle_type {
253 	FLOW_HANDLE_TYPE_FLOW,
254 	FLOW_HANDLE_TYPE_FLM,
255 };
256 
257 struct flow_handle {
258 	enum flow_handle_type type;
259 	uint32_t flm_id;
260 	uint16_t caller_id;
261 	uint16_t learn_ignored;
262 
263 	struct flow_eth_dev *dev;
264 	struct flow_handle *next;
265 	struct flow_handle *prev;
266 
267 	void *user_data;
268 
269 	union {
270 		struct {
271 			/*
272 			 * 1st step conversion and validation of flow
273 			 * verified and converted flow match + actions structure
274 			 */
275 			struct nic_flow_def *fd;
276 			/*
277 			 * 2nd step NIC HW resource allocation and configuration
278 			 * NIC resource management structures
279 			 */
280 			struct {
281 				uint32_t db_idx_counter;
282 				uint32_t db_idxs[RES_COUNT];
283 			};
284 			uint32_t port_id;	/* MAC port ID or override of virtual in_port */
285 		};
286 
287 		struct {
288 			uint32_t flm_db_idx_counter;
289 			uint32_t flm_db_idxs[RES_COUNT];
290 
291 			uint32_t flm_data[10];
292 			uint8_t flm_prot;
293 			uint8_t flm_kid;
294 			uint8_t flm_prio;
295 			uint8_t flm_ft;
296 
297 			uint16_t flm_rpl_ext_ptr;
298 			uint32_t flm_nat_ipv4;
299 			uint16_t flm_nat_port;
300 			uint8_t flm_dscp;
301 			uint32_t flm_teid;
302 			uint8_t flm_rqi;
303 			uint8_t flm_qfi;
304 		};
305 	};
306 };
307 
308 void km_attach_ndev_resource_management(struct km_flow_def_s *km, void **handle);
309 void km_free_ndev_resource_management(void **handle);
310 
311 int km_add_match_elem(struct km_flow_def_s *km, uint32_t e_word[4], uint32_t e_mask[4],
312 	uint32_t word_len, enum frame_offs_e start, int8_t offset);
313 
314 int km_key_create(struct km_flow_def_s *km, uint32_t port_id);
315 /*
316  * Compares 2 KM key definitions after first collect validate and optimization.
317  * km is compared against an existing km1.
318  * if identical, km1 flow_type is returned
319  */
320 int km_key_compare(struct km_flow_def_s *km, struct km_flow_def_s *km1);
321 
322 int km_rcp_set(struct km_flow_def_s *km, int index);
323 
324 int km_write_data_match_entry(struct km_flow_def_s *km, uint32_t color);
325 int km_clear_data_match_entry(struct km_flow_def_s *km);
326 
327 void kcc_free_ndev_resource_management(void **handle);
328 
329 /*
330  * Group management
331  */
332 int flow_group_handle_create(void **handle, uint32_t group_count);
333 int flow_group_handle_destroy(void **handle);
334 
335 int flow_group_translate_get(void *handle, uint8_t owner_id, uint8_t port_id, uint32_t group_in,
336 	uint32_t *group_out);
337 
338 #endif  /* _FLOW_API_ENGINE_H_ */
339