xref: /dpdk/drivers/net/ntnic/include/flow_api_engine.h (revision e7e01fd15ddee1eb92d68a3aabe800850a8c757a)
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 	struct hasher_s *hsh;
133 
134 	/* TCAM specific bank management */
135 	struct tcam_distrib_s *tcam_dist;
136 	int tcam_start_bank;
137 	int tcam_record;
138 };
139 
140 /*
141  * RSS configuration, see struct rte_flow_action_rss
142  */
143 struct hsh_def_s {
144 	enum rte_eth_hash_function func;	/* RSS hash function to apply */
145 	/* RSS hash types, see definition of RTE_ETH_RSS_* for hash calculation options */
146 	uint64_t types;
147 	uint32_t key_len;	/* Hash key length in bytes. */
148 	const uint8_t *key;	/* Hash key. */
149 };
150 
151 /*
152  * Tunnel encapsulation header definition
153  */
154 #define MAX_TUN_HDR_SIZE 128
155 
156 struct tunnel_header_s {
157 	union {
158 		uint8_t hdr8[MAX_TUN_HDR_SIZE];
159 		uint32_t hdr32[(MAX_TUN_HDR_SIZE + 3) / 4];
160 	} d;
161 
162 	uint8_t len;
163 
164 	uint8_t nb_vlans;
165 
166 	uint8_t ip_version;	/* 4: v4, 6: v6 */
167 
168 	uint8_t new_outer;
169 	uint8_t l2_len;
170 	uint8_t l3_len;
171 	uint8_t l4_len;
172 };
173 
174 enum flow_port_type_e {
175 	PORT_NONE,	/* not defined or drop */
176 	PORT_INTERNAL,	/* no queues attached */
177 	PORT_PHY,	/* MAC phy output queue */
178 	PORT_VIRT,	/* Memory queues to Host */
179 };
180 
181 struct output_s {
182 	uint32_t owning_port_id;/* the port who owns this output destination */
183 	enum flow_port_type_e type;
184 	int id;	/* depending on port type: queue ID or physical port id or not used */
185 	int active;	/* activated */
186 };
187 
188 struct nic_flow_def {
189 	/*
190 	 * Frame Decoder match info collected
191 	 */
192 	int l2_prot;
193 	int l3_prot;
194 	int l4_prot;
195 	int tunnel_prot;
196 	int tunnel_l3_prot;
197 	int tunnel_l4_prot;
198 	int vlans;
199 	int fragmentation;
200 	int ip_prot;
201 	int tunnel_ip_prot;
202 	/*
203 	 * Additional meta data for various functions
204 	 */
205 	int in_port_override;
206 	int non_empty;	/* default value is -1; value 1 means flow actions update */
207 	struct output_s dst_id[MAX_OUTPUT_DEST];/* define the output to use */
208 	/* total number of available queues defined for all outputs - i.e. number of dst_id's */
209 	int dst_num_avail;
210 
211 	/*
212 	 * Mark or Action info collection
213 	 */
214 	uint32_t mark;
215 
216 	uint32_t jump_to_group;
217 
218 	int full_offload;
219 
220 	/*
221 	 * Action push tunnel
222 	 */
223 	struct tunnel_header_s tun_hdr;
224 
225 	/*
226 	 * If DPDK RTE tunnel helper API used
227 	 * this holds the tunnel if used in flow
228 	 */
229 	struct tunnel_s *tnl;
230 
231 	/*
232 	 * Header Stripper
233 	 */
234 	int header_strip_end_dyn;
235 	int header_strip_end_ofs;
236 
237 	/*
238 	 * Modify field
239 	 */
240 	struct {
241 		uint32_t select;
242 		uint32_t dyn;
243 		uint32_t ofs;
244 		uint32_t len;
245 		uint32_t level;
246 		union {
247 			uint8_t value8[16];
248 			uint16_t value16[8];
249 			uint32_t value32[4];
250 		};
251 	} modify_field[MAX_CPY_WRITERS_SUPPORTED];
252 
253 	uint32_t modify_field_count;
254 	uint8_t ttl_sub_enable;
255 	uint8_t ttl_sub_ipv4;
256 	uint8_t ttl_sub_outer;
257 
258 	/*
259 	 * Key Matcher flow definitions
260 	 */
261 	struct km_flow_def_s km;
262 
263 	/*
264 	 * Hash module RSS definitions
265 	 */
266 	struct hsh_def_s hsh;
267 };
268 
269 enum flow_handle_type {
270 	FLOW_HANDLE_TYPE_FLOW,
271 	FLOW_HANDLE_TYPE_FLM,
272 };
273 
274 struct flow_handle {
275 	enum flow_handle_type type;
276 	uint32_t flm_id;
277 	uint16_t caller_id;
278 	uint16_t learn_ignored;
279 
280 	struct flow_eth_dev *dev;
281 	struct flow_handle *next;
282 	struct flow_handle *prev;
283 
284 	/* Flow specific pointer to application data stored during action creation. */
285 	void *context;
286 	void *user_data;
287 
288 	union {
289 		struct {
290 			/*
291 			 * 1st step conversion and validation of flow
292 			 * verified and converted flow match + actions structure
293 			 */
294 			struct nic_flow_def *fd;
295 			/*
296 			 * 2nd step NIC HW resource allocation and configuration
297 			 * NIC resource management structures
298 			 */
299 			struct {
300 				uint32_t db_idx_counter;
301 				uint32_t db_idxs[RES_COUNT];
302 			};
303 			uint32_t port_id;	/* MAC port ID or override of virtual in_port */
304 		};
305 
306 		struct {
307 			uint32_t flm_db_idx_counter;
308 			uint32_t flm_db_idxs[RES_COUNT];
309 
310 			uint32_t flm_data[10];
311 			uint8_t flm_prot;
312 			uint8_t flm_kid;
313 			uint8_t flm_prio;
314 			uint8_t flm_ft;
315 
316 			uint16_t flm_rpl_ext_ptr;
317 			uint32_t flm_nat_ipv4;
318 			uint16_t flm_nat_port;
319 			uint8_t flm_dscp;
320 			uint32_t flm_teid;
321 			uint8_t flm_rqi;
322 			uint8_t flm_qfi;
323 			uint8_t flm_scrub_prof;
324 		};
325 	};
326 };
327 
328 void km_attach_ndev_resource_management(struct km_flow_def_s *km, void **handle);
329 void km_free_ndev_resource_management(void **handle);
330 
331 int km_add_match_elem(struct km_flow_def_s *km, uint32_t e_word[4], uint32_t e_mask[4],
332 	uint32_t word_len, enum frame_offs_e start, int8_t offset);
333 
334 int km_key_create(struct km_flow_def_s *km, uint32_t port_id);
335 /*
336  * Compares 2 KM key definitions after first collect validate and optimization.
337  * km is compared against an existing km1.
338  * if identical, km1 flow_type is returned
339  */
340 int km_key_compare(struct km_flow_def_s *km, struct km_flow_def_s *km1);
341 
342 int km_rcp_set(struct km_flow_def_s *km, int index);
343 
344 int km_write_data_match_entry(struct km_flow_def_s *km, uint32_t color);
345 int km_clear_data_match_entry(struct km_flow_def_s *km);
346 
347 void kcc_free_ndev_resource_management(void **handle);
348 
349 /*
350  * Group management
351  */
352 int flow_group_handle_create(void **handle, uint32_t group_count);
353 int flow_group_handle_destroy(void **handle);
354 
355 int flow_group_translate_get(void *handle, uint8_t owner_id, uint8_t port_id, uint32_t group_in,
356 	uint32_t *group_out);
357 
358 #endif  /* _FLOW_API_ENGINE_H_ */
359