xref: /dpdk/drivers/net/intel/ice/base/ice_parser.c (revision c1d145834f287aa8cf53de914618a7312f2c360e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2023 Intel Corporation
3  */
4 
5 #include "ice_common.h"
6 #include "ice_parser_util.h"
7 
8 #define ICE_SEC_DATA_OFFSET				4
9 #define ICE_SID_RXPARSER_IMEM_ENTRY_SIZE		48
10 #define ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE	24
11 #define ICE_SID_RXPARSER_CAM_ENTRY_SIZE			16
12 #define ICE_SID_RXPARSER_PG_SPILL_ENTRY_SIZE		17
13 #define ICE_SID_RXPARSER_NOMATCH_CAM_ENTRY_SIZE		12
14 #define ICE_SID_RXPARSER_NOMATCH_SPILL_ENTRY_SIZE	13
15 #define ICE_SID_RXPARSER_BOOST_TCAM_ENTRY_SIZE		88
16 #define ICE_SID_RXPARSER_MARKER_TYPE_ENTRY_SIZE		24
17 #define ICE_SID_RXPARSER_MARKER_GRP_ENTRY_SIZE		8
18 #define ICE_SID_RXPARSER_PROTO_GRP_ENTRY_SIZE		24
19 #define ICE_SID_RXPARSER_FLAG_REDIR_ENTRY_SIZE		1
20 
21 #define ICE_SEC_LBL_DATA_OFFSET				2
22 #define ICE_SID_LBL_ENTRY_SIZE				66
23 
24 void ice_lbl_dump(struct ice_hw *hw, struct ice_lbl_item *item)
25 {
26 	ice_info(hw, "index = %d\n", item->idx);
27 	ice_info(hw, "label = %s\n", item->label);
28 }
29 
30 void ice_parse_item_dflt(struct ice_hw *hw, u16 idx, void *item,
31 			 void *data, int size)
32 {
33 	ice_memcpy(item, data, size, ICE_DMA_TO_NONDMA);
34 }
35 
36 /**
37  * ice_parser_sect_item_get - parse a item from a section
38  * @sect_type: section type
39  * @section: section object
40  * @index: index of the item to get
41  * @offset: dummy as prototype of ice_pkg_enum_entry's last parameter
42  */
43 void *ice_parser_sect_item_get(u32 sect_type, void *section,
44 			       u32 index, u32 *offset)
45 {
46 	struct ice_pkg_sect_hdr *hdr;
47 	int data_off = ICE_SEC_DATA_OFFSET;
48 	int size;
49 
50 	if (!section)
51 		return NULL;
52 
53 	switch (sect_type) {
54 	case ICE_SID_RXPARSER_IMEM:
55 		size = ICE_SID_RXPARSER_IMEM_ENTRY_SIZE;
56 		break;
57 	case ICE_SID_RXPARSER_METADATA_INIT:
58 		size = ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE;
59 		break;
60 	case ICE_SID_RXPARSER_CAM:
61 		size = ICE_SID_RXPARSER_CAM_ENTRY_SIZE;
62 		break;
63 	case ICE_SID_RXPARSER_PG_SPILL:
64 		size = ICE_SID_RXPARSER_PG_SPILL_ENTRY_SIZE;
65 		break;
66 	case ICE_SID_RXPARSER_NOMATCH_CAM:
67 		size = ICE_SID_RXPARSER_NOMATCH_CAM_ENTRY_SIZE;
68 		break;
69 	case ICE_SID_RXPARSER_NOMATCH_SPILL:
70 		size = ICE_SID_RXPARSER_NOMATCH_SPILL_ENTRY_SIZE;
71 		break;
72 	case ICE_SID_RXPARSER_BOOST_TCAM:
73 		size = ICE_SID_RXPARSER_BOOST_TCAM_ENTRY_SIZE;
74 		break;
75 	case ICE_SID_LBL_RXPARSER_TMEM:
76 		data_off = ICE_SEC_LBL_DATA_OFFSET;
77 		size = ICE_SID_LBL_ENTRY_SIZE;
78 		break;
79 	case ICE_SID_RXPARSER_MARKER_PTYPE:
80 		size = ICE_SID_RXPARSER_MARKER_TYPE_ENTRY_SIZE;
81 		break;
82 	case ICE_SID_RXPARSER_MARKER_GRP:
83 		size = ICE_SID_RXPARSER_MARKER_GRP_ENTRY_SIZE;
84 		break;
85 	case ICE_SID_RXPARSER_PROTO_GRP:
86 		size = ICE_SID_RXPARSER_PROTO_GRP_ENTRY_SIZE;
87 		break;
88 	case ICE_SID_RXPARSER_FLAG_REDIR:
89 		size = ICE_SID_RXPARSER_FLAG_REDIR_ENTRY_SIZE;
90 		break;
91 	default:
92 		return NULL;
93 	}
94 
95 	hdr = (struct ice_pkg_sect_hdr *)section;
96 	if (index >= LE16_TO_CPU(hdr->count))
97 		return NULL;
98 
99 	return (void *)((uintptr_t)section + data_off + index * size);
100 }
101 
102 /**
103  * ice_parser_create_table - create a item table from a section
104  * @hw: pointer to the hardware structure
105  * @sect_type: section type
106  * @item_size: item size in byte
107  * @length: number of items in the table to create
108  * @item_get: the function will be parsed to ice_pkg_enum_entry
109  * @parse_item: the function to parse the item
110  * @no_offset: ignore header offset, calculate index from 0
111  */
112 void *ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
113 			      u32 item_size, u32 length,
114 			      void *(*item_get)(u32 sect_type, void *section,
115 						u32 index, u32 *offset),
116 			      void (*parse_item)(struct ice_hw *hw, u16 idx,
117 						 void *item, void *data,
118 						 int size),
119 			      bool no_offset)
120 {
121 	struct ice_seg *seg = hw->seg;
122 	struct ice_pkg_enum state;
123 	u16 idx = 0xffff;
124 	void *table;
125 	void *data;
126 
127 	if (!seg)
128 		return NULL;
129 
130 	table = ice_malloc(hw, item_size * length);
131 	if (!table) {
132 		ice_debug(hw, ICE_DBG_PARSER, "failed to allocate memory for table type %d.\n",
133 			  sect_type);
134 		return NULL;
135 	}
136 
137 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
138 	do {
139 		data = ice_pkg_enum_entry(seg, &state, sect_type, NULL,
140 					  item_get);
141 		seg = NULL;
142 		if (data) {
143 			struct ice_pkg_sect_hdr *hdr =
144 				(struct ice_pkg_sect_hdr *)state.sect;
145 
146 			if (no_offset)
147 				idx++;
148 			else
149 				idx = LE16_TO_CPU(hdr->offset) +
150 							state.entry_idx;
151 			parse_item(hw, idx,
152 				   (void *)((uintptr_t)table + idx * item_size),
153 				   data, item_size);
154 		}
155 	} while (data);
156 
157 	return table;
158 }
159 
160 /**
161  * ice_parser_create - create a parser instance
162  * @hw: pointer to the hardware structure
163  * @psr: output parameter for a new parser instance be created
164  */
165 int ice_parser_create(struct ice_hw *hw, struct ice_parser **psr)
166 {
167 	struct ice_parser *p;
168 	int status;
169 
170 	p = (struct ice_parser *)ice_malloc(hw, sizeof(struct ice_parser));
171 	if (!p)
172 		return ICE_ERR_NO_MEMORY;
173 
174 	p->hw = hw;
175 	p->rt.psr = p;
176 
177 	p->imem_table = ice_imem_table_get(hw);
178 	if (!p->imem_table) {
179 		status = ICE_ERR_PARAM;
180 		goto err;
181 	}
182 
183 	p->mi_table = ice_metainit_table_get(hw);
184 	if (!p->mi_table) {
185 		status = ICE_ERR_PARAM;
186 		goto err;
187 	}
188 
189 	p->pg_cam_table = ice_pg_cam_table_get(hw);
190 	if (!p->pg_cam_table) {
191 		status = ICE_ERR_PARAM;
192 		goto err;
193 	}
194 
195 	p->pg_sp_cam_table = ice_pg_sp_cam_table_get(hw);
196 	if (!p->pg_sp_cam_table) {
197 		status = ICE_ERR_PARAM;
198 		goto err;
199 	}
200 
201 	p->pg_nm_cam_table = ice_pg_nm_cam_table_get(hw);
202 	if (!p->pg_nm_cam_table) {
203 		status = ICE_ERR_PARAM;
204 		goto err;
205 	}
206 
207 	p->pg_nm_sp_cam_table = ice_pg_nm_sp_cam_table_get(hw);
208 	if (!p->pg_nm_sp_cam_table) {
209 		status = ICE_ERR_PARAM;
210 		goto err;
211 	}
212 
213 	p->bst_tcam_table = ice_bst_tcam_table_get(hw);
214 	if (!p->bst_tcam_table) {
215 		status = ICE_ERR_PARAM;
216 		goto err;
217 	}
218 
219 	p->bst_lbl_table = ice_bst_lbl_table_get(hw);
220 	if (!p->bst_lbl_table) {
221 		status = ICE_ERR_PARAM;
222 		goto err;
223 	}
224 
225 	p->ptype_mk_tcam_table = ice_ptype_mk_tcam_table_get(hw);
226 	if (!p->ptype_mk_tcam_table) {
227 		status = ICE_ERR_PARAM;
228 		goto err;
229 	}
230 
231 	p->mk_grp_table = ice_mk_grp_table_get(hw);
232 	if (!p->mk_grp_table) {
233 		status = ICE_ERR_PARAM;
234 		goto err;
235 	}
236 
237 	p->proto_grp_table = ice_proto_grp_table_get(hw);
238 	if (!p->proto_grp_table) {
239 		status = ICE_ERR_PARAM;
240 		goto err;
241 	}
242 
243 	p->flg_rd_table = ice_flg_rd_table_get(hw);
244 	if (!p->flg_rd_table) {
245 		status = ICE_ERR_PARAM;
246 		goto err;
247 	}
248 
249 	p->xlt_kb_sw = ice_xlt_kb_get_sw(hw);
250 	if (!p->xlt_kb_sw) {
251 		status = ICE_ERR_PARAM;
252 		goto err;
253 	}
254 
255 	p->xlt_kb_acl = ice_xlt_kb_get_acl(hw);
256 	if (!p->xlt_kb_acl) {
257 		status = ICE_ERR_PARAM;
258 		goto err;
259 	}
260 
261 	p->xlt_kb_fd = ice_xlt_kb_get_fd(hw);
262 	if (!p->xlt_kb_fd) {
263 		status = ICE_ERR_PARAM;
264 		goto err;
265 	}
266 
267 	p->xlt_kb_rss = ice_xlt_kb_get_rss(hw);
268 	if (!p->xlt_kb_rss) {
269 		status = ICE_ERR_PARAM;
270 		goto err;
271 	}
272 
273 	*psr = p;
274 	return 0;
275 err:
276 	ice_parser_destroy(p);
277 	return status;
278 }
279 
280 /**
281  * ice_parser_destroy - destroy a parser instance
282  * @psr: pointer to a parser instance
283  */
284 void ice_parser_destroy(struct ice_parser *psr)
285 {
286 	ice_free(psr->hw, psr->imem_table);
287 	ice_free(psr->hw, psr->mi_table);
288 	ice_free(psr->hw, psr->pg_cam_table);
289 	ice_free(psr->hw, psr->pg_sp_cam_table);
290 	ice_free(psr->hw, psr->pg_nm_cam_table);
291 	ice_free(psr->hw, psr->pg_nm_sp_cam_table);
292 	ice_free(psr->hw, psr->bst_tcam_table);
293 	ice_free(psr->hw, psr->bst_lbl_table);
294 	ice_free(psr->hw, psr->ptype_mk_tcam_table);
295 	ice_free(psr->hw, psr->mk_grp_table);
296 	ice_free(psr->hw, psr->proto_grp_table);
297 	ice_free(psr->hw, psr->flg_rd_table);
298 	ice_free(psr->hw, psr->xlt_kb_sw);
299 	ice_free(psr->hw, psr->xlt_kb_acl);
300 	ice_free(psr->hw, psr->xlt_kb_fd);
301 	ice_free(psr->hw, psr->xlt_kb_rss);
302 
303 	ice_free(psr->hw, psr);
304 }
305 
306 /**
307  * ice_parser_run - parse on a packet in binary and return the result
308  * @psr: pointer to a parser instance
309  * @pkt_buf: packet data
310  * @pkt_len: packet length
311  * @rslt: input/output parameter to save parser result.
312  */
313 int ice_parser_run(struct ice_parser *psr, const u8 *pkt_buf,
314 		   int pkt_len, struct ice_parser_result *rslt)
315 {
316 	ice_parser_rt_reset(&psr->rt);
317 	ice_parser_rt_pktbuf_set(&psr->rt, pkt_buf, pkt_len);
318 
319 	return ice_parser_rt_execute(&psr->rt, rslt);
320 }
321 
322 /**
323  * ice_parser_result_dump - dump a parser result info
324  * @hw: pointer to the hardware structure
325  * @rslt: parser result info to dump
326  */
327 void ice_parser_result_dump(struct ice_hw *hw, struct ice_parser_result *rslt)
328 {
329 	int i;
330 
331 	ice_info(hw, "ptype = %d\n", rslt->ptype);
332 	for (i = 0; i < rslt->po_num; i++)
333 		ice_info(hw, "proto = %d, offset = %d\n",
334 			 rslt->po[i].proto_id, rslt->po[i].offset);
335 
336 	ice_info(hw, "flags_psr = 0x%016" PRIx64 "\n", rslt->flags_psr);
337 	ice_info(hw, "flags_pkt = 0x%016" PRIx64 "\n", rslt->flags_pkt);
338 	ice_info(hw, "flags_sw = 0x%04x\n", rslt->flags_sw);
339 	ice_info(hw, "flags_fd = 0x%04x\n", rslt->flags_fd);
340 	ice_info(hw, "flags_rss = 0x%04x\n", rslt->flags_rss);
341 }
342 
343 static void _bst_vm_set(struct ice_parser *psr, const char *prefix, bool on)
344 {
345 	u16 i = 0;
346 
347 	while (true) {
348 		struct ice_bst_tcam_item *item;
349 		item = ice_bst_tcam_search(psr->bst_tcam_table,
350 					   psr->bst_lbl_table,
351 					   prefix, &i);
352 		if (!item)
353 			break;
354 		item->key[0] = (u8)(on ? 0xff : 0xfe);
355 		item->key_inv[0] = (u8)(on ? 0xff : 0xfe);
356 		i++;
357 	}
358 }
359 
360 /**
361  * ice_parser_dvm_set - configure double vlan mode for parser
362  * @psr: pointer to a parser instance
363  * @on: true to turn on; false to turn off
364  */
365 void ice_parser_dvm_set(struct ice_parser *psr, bool on)
366 {
367 	_bst_vm_set(psr, "BOOST_MAC_VLAN_DVM", on);
368 	_bst_vm_set(psr, "BOOST_MAC_VLAN_SVM", !on);
369 }
370 
371 static int
372 _tunnel_port_set(struct ice_parser *psr, const char *prefix, u16 udp_port,
373 		 bool on)
374 {
375 	u8 *buf = (u8 *)&udp_port;
376 	u16 i = 0;
377 
378 	while (true) {
379 		struct ice_bst_tcam_item *item;
380 		item = ice_bst_tcam_search(psr->bst_tcam_table,
381 					   psr->bst_lbl_table,
382 					   prefix, &i);
383 		if (!item)
384 			break;
385 
386 		/* found empty slot to add */
387 		if (on && item->key[16] == 0xfe && item->key_inv[16] == 0xfe) {
388 			item->key_inv[15] = buf[0];
389 			item->key_inv[16] = buf[1];
390 			item->key[15] = (u8)(0xff - buf[0]);
391 			item->key[16] = (u8)(0xff - buf[1]);
392 
393 			return 0;
394 		/* found a matched slot to delete */
395 		} else if (!on && (item->key_inv[15] == buf[0] ||
396 			   item->key_inv[16] == buf[1])) {
397 			item->key_inv[15] = 0xff;
398 			item->key_inv[16] = 0xfe;
399 			item->key[15] = 0xff;
400 			item->key[16] = 0xfe;
401 
402 			return 0;
403 		}
404 		i++;
405 	}
406 
407 	return ICE_ERR_PARAM;
408 }
409 
410 /**
411  * ice_parser_vxlan_tunnel_set - configure vxlan tunnel for parser
412  * @psr: pointer to a parser instance
413  * @udp_port: vxlan tunnel port in UDP header
414  * @on: true to turn on; false to turn off
415  */
416 int ice_parser_vxlan_tunnel_set(struct ice_parser *psr,
417 				u16 udp_port, bool on)
418 {
419 	return _tunnel_port_set(psr, "TNL_VXLAN", udp_port, on);
420 }
421 
422 /**
423  * ice_parser_geneve_tunnel_set - configure geneve tunnel for parser
424  * @psr: pointer to a parser instance
425  * @udp_port: geneve tunnel port in UDP header
426  * @on: true to turn on; false to turn off
427  */
428 int ice_parser_geneve_tunnel_set(struct ice_parser *psr,
429 				 u16 udp_port, bool on)
430 {
431 	return _tunnel_port_set(psr, "TNL_GENEVE", udp_port, on);
432 }
433 
434 /**
435  * ice_parser_ecpri_tunnel_set - configure ecpri tunnel for parser
436  * @psr: pointer to a parser instance
437  * @udp_port: ecpri tunnel port in UDP header
438  * @on: true to turn on; false to turn off
439  */
440 int ice_parser_ecpri_tunnel_set(struct ice_parser *psr,
441 				u16 udp_port, bool on)
442 {
443 	return _tunnel_port_set(psr, "TNL_UDP_ECPRI", udp_port, on);
444 }
445 
446 static bool _nearest_proto_id(struct ice_parser_result *rslt, u16 offset,
447 			      u8 *proto_id, u16 *proto_off)
448 {
449 	u16 dist = 0xffff;
450 	u8 p = 0;
451 	int i;
452 
453 	for (i = 0; i < rslt->po_num; i++) {
454 		if (offset < rslt->po[i].offset)
455 			continue;
456 		if (offset - rslt->po[i].offset < dist) {
457 			p = rslt->po[i].proto_id;
458 			dist = offset - rslt->po[i].offset;
459 		}
460 	}
461 
462 	if (dist % 2)
463 		return false;
464 
465 	*proto_id = p;
466 	*proto_off = dist;
467 
468 	return true;
469 }
470 
471 /** default flag mask to cover GTP_EH_PDU, GTP_EH_PDU_LINK and TUN2
472  * In future, the flag masks should learn from DDP
473  */
474 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_SW	0x4002
475 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_ACL	0x0000
476 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_FD	0x6080
477 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_RSS	0x6010
478 
479 /**
480  * ice_parser_profile_init  - initialize a FXP profile base on parser result
481  * @rslt: a instance of a parser result
482  * @pkt_buf: packet data buffer
483  * @msk_buf: packet mask buffer
484  * @buf_len: packet length
485  * @blk: FXP pipeline stage
486  * @prefix_match: match protocol stack exactly or only prefix
487  * @prof: input/output parameter to save the profile
488  */
489 int ice_parser_profile_init(struct ice_parser_result *rslt,
490 			    const u8 *pkt_buf, const u8 *msk_buf,
491 			    int buf_len, enum ice_block blk,
492 			    bool prefix_match,
493 			    struct ice_parser_profile *prof)
494 {
495 	u8 proto_id = 0xff;
496 	u16 proto_off = 0;
497 	u16 off;
498 
499 	ice_memset(prof, 0, sizeof(*prof), ICE_NONDMA_MEM);
500 	ice_set_bit(rslt->ptype, prof->ptypes);
501 	if (blk == ICE_BLK_SW) {
502 		prof->flags = rslt->flags_sw;
503 		prof->flags_msk = ICE_KEYBUILD_FLAG_MASK_DEFAULT_SW;
504 	} else if (blk == ICE_BLK_ACL) {
505 		prof->flags = rslt->flags_acl;
506 		prof->flags_msk = ICE_KEYBUILD_FLAG_MASK_DEFAULT_ACL;
507 	} else if (blk == ICE_BLK_FD) {
508 		prof->flags = rslt->flags_fd;
509 		prof->flags_msk = ICE_KEYBUILD_FLAG_MASK_DEFAULT_FD;
510 	} else if (blk == ICE_BLK_RSS) {
511 		prof->flags = rslt->flags_rss;
512 		prof->flags_msk = ICE_KEYBUILD_FLAG_MASK_DEFAULT_RSS;
513 	} else {
514 		return ICE_ERR_PARAM;
515 	}
516 
517 	for (off = 0; off < buf_len - 1; off++) {
518 		if (msk_buf[off] == 0 && msk_buf[off + 1] == 0)
519 			continue;
520 		if (!_nearest_proto_id(rslt, off, &proto_id, &proto_off))
521 			continue;
522 		if (prof->fv_num >= 32)
523 			return ICE_ERR_PARAM;
524 
525 		prof->fv[prof->fv_num].proto_id = proto_id;
526 		prof->fv[prof->fv_num].offset = proto_off;
527 		prof->fv[prof->fv_num].spec = *(const u16 *)&pkt_buf[off];
528 		prof->fv[prof->fv_num].msk = *(const u16 *)&msk_buf[off];
529 		prof->fv_num++;
530 	}
531 
532 	return 0;
533 }
534 
535 /**
536  * ice_parser_profile_dump - dump an FXP profile info
537  * @hw: pointer to the hardware structure
538  * @prof: profile info to dump
539  */
540 void ice_parser_profile_dump(struct ice_hw *hw, struct ice_parser_profile *prof)
541 {
542 	u16 i;
543 
544 	ice_info(hw, "ptypes:\n");
545 	for (i = 0; i < ICE_FLOW_PTYPE_MAX; i++)
546 		if (ice_is_bit_set(prof->ptypes, i))
547 			ice_info(hw, "\t%d\n", i);
548 
549 	for (i = 0; i < prof->fv_num; i++)
550 		ice_info(hw, "proto = %d, offset = %d spec = 0x%04x, mask = 0x%04x\n",
551 			 prof->fv[i].proto_id, prof->fv[i].offset,
552 			 prof->fv[i].spec, prof->fv[i].msk);
553 
554 	ice_info(hw, "flags = 0x%04x\n", prof->flags);
555 	ice_info(hw, "flags_msk = 0x%04x\n", prof->flags_msk);
556 }
557 
558 /**
559  * ice_check_ddp_support_proto_id - check DDP package file support protocol ID
560  * @hw: pointer to the HW struct
561  * @proto_id: protocol ID value
562  *
563  * This function maintains the compatibility of the program process by checking
564  * whether the current DDP file supports the required protocol ID.
565  */
566 bool ice_check_ddp_support_proto_id(struct ice_hw *hw,
567 				    enum ice_prot_id proto_id)
568 {
569 	struct ice_proto_grp_item *proto_grp_table;
570 	struct ice_proto_grp_item *proto_grp;
571 	bool exist = false;
572 	u16 idx, i;
573 
574 	proto_grp_table = ice_proto_grp_table_get(hw);
575 	if (!proto_grp_table)
576 		return false;
577 
578 	for (idx = 0; idx < ICE_PROTO_GRP_TABLE_SIZE; idx++) {
579 		proto_grp = &proto_grp_table[idx];
580 		for (i = 0; i < ICE_PROTO_COUNT_PER_GRP; i++) {
581 			if (proto_grp->po[i].proto_id == proto_id) {
582 				exist = true;
583 				goto exit;
584 			}
585 		}
586 	}
587 
588 exit:
589 	ice_free(hw, proto_grp_table);
590 	return exist;
591 }
592