1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
7 #include "bnxt_tf_common.h"
8 #include "ulp_rte_parser.h"
9 #include "ulp_matcher.h"
10 #include "ulp_flow_db.h"
11 #include "ulp_mapper.h"
12 #include "ulp_fc_mgr.h"
13 #include "ulp_port_db.h"
14 #include "ulp_ha_mgr.h"
16 #include <rte_malloc.h>
17 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
18 #include "ulp_template_debug_proto.h"
22 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
23 const struct rte_flow_item pattern[],
24 const struct rte_flow_action actions[],
25 struct rte_flow_error *error)
27 /* Perform the validation of the arguments for null */
29 return BNXT_TF_RC_ERROR;
32 rte_flow_error_set(error,
34 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
37 return BNXT_TF_RC_ERROR;
41 rte_flow_error_set(error,
43 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
46 return BNXT_TF_RC_ERROR;
50 rte_flow_error_set(error,
52 RTE_FLOW_ERROR_TYPE_ATTR,
55 return BNXT_TF_RC_ERROR;
58 if (attr->egress && attr->ingress) {
59 rte_flow_error_set(error,
61 RTE_FLOW_ERROR_TYPE_ATTR,
63 "EGRESS AND INGRESS UNSUPPORTED");
64 return BNXT_TF_RC_ERROR;
66 return BNXT_TF_RC_SUCCESS;
70 bnxt_ulp_set_dir_attributes(struct ulp_rte_parser_params *params,
71 const struct rte_flow_attr *attr)
73 /* Set the flow attributes */
75 params->dir_attr |= BNXT_ULP_FLOW_ATTR_EGRESS;
77 params->dir_attr |= BNXT_ULP_FLOW_ATTR_INGRESS;
78 #if RTE_VERSION_NUM(17, 11, 10, 16) < RTE_VERSION
80 params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
85 bnxt_ulp_init_mapper_params(struct bnxt_ulp_mapper_create_parms *mapper_cparms,
86 struct ulp_rte_parser_params *params,
87 enum bnxt_ulp_fdb_type flow_type)
89 uint32_t ulp_flags = 0;
91 memset(mapper_cparms, 0, sizeof(*mapper_cparms));
92 mapper_cparms->flow_type = flow_type;
93 mapper_cparms->app_priority = params->priority;
94 mapper_cparms->dir_attr = params->dir_attr;
95 mapper_cparms->class_tid = params->class_id;
96 mapper_cparms->act_tid = params->act_tmpl;
97 mapper_cparms->func_id = params->func_id;
98 mapper_cparms->hdr_bitmap = ¶ms->hdr_bitmap;
99 mapper_cparms->enc_hdr_bitmap = ¶ms->enc_hdr_bitmap;
100 mapper_cparms->hdr_field = params->hdr_field;
101 mapper_cparms->enc_field = params->enc_field;
102 mapper_cparms->comp_fld = params->comp_fld;
103 mapper_cparms->act = ¶ms->act_bitmap;
104 mapper_cparms->act_prop = ¶ms->act_prop;
105 mapper_cparms->flow_id = params->fid;
106 mapper_cparms->parent_flow = params->parent_flow;
107 mapper_cparms->child_flow = params->child_flow;
108 mapper_cparms->fld_bitmap = ¶ms->fld_bitmap;
109 mapper_cparms->flow_pattern_id = params->flow_pattern_id;
110 mapper_cparms->act_pattern_id = params->act_pattern_id;
111 mapper_cparms->app_id = params->app_id;
112 mapper_cparms->port_id = params->port_id;
113 mapper_cparms->tun_idx = params->tun_idx;
115 /* update the signature fields into the computed field list */
116 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_HDR_SIG_ID,
118 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_FLOW_SIG_ID,
119 params->flow_sig_id);
121 /* update the WC Priority flag */
122 if (!bnxt_ulp_cntxt_ptr2_ulp_flags_get(params->ulp_ctx, &ulp_flags) &&
123 ULP_HIGH_AVAIL_IS_ENABLED(ulp_flags)) {
124 enum ulp_ha_mgr_region region = ULP_HA_REGION_LOW;
127 rc = ulp_ha_mgr_region_get(params->ulp_ctx, ®ion);
129 BNXT_TF_DBG(ERR, "Unable to get WC region\n");
130 if (region == ULP_HA_REGION_HI)
131 ULP_COMP_FLD_IDX_WR(params,
132 BNXT_ULP_CF_IDX_WC_IS_HA_HIGH_REG,
137 /* Function to create the rte flow. */
138 static struct rte_flow *
139 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
140 const struct rte_flow_attr *attr,
141 const struct rte_flow_item pattern[],
142 const struct rte_flow_action actions[],
143 struct rte_flow_error *error)
145 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
146 struct ulp_rte_parser_params params;
147 struct bnxt_ulp_context *ulp_ctx;
148 int rc, ret = BNXT_TF_RC_ERROR;
149 struct rte_flow *flow_id;
153 if (bnxt_ulp_flow_validate_args(attr,
155 error) == BNXT_TF_RC_ERROR) {
156 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
160 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
162 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
166 /* Initialize the parser params */
167 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
168 params.ulp_ctx = ulp_ctx;
170 if (bnxt_ulp_cntxt_app_id_get(params.ulp_ctx, ¶ms.app_id)) {
171 BNXT_TF_DBG(ERR, "failed to get the app id\n");
175 /* Set the flow attributes */
176 bnxt_ulp_set_dir_attributes(¶ms, attr);
178 /* copy the device port id and direction for further processing */
179 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
181 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_DEV_PORT_ID,
183 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
184 BNXT_ULP_INVALID_SVIF_VAL);
186 /* Get the function id */
187 if (ulp_port_db_port_func_id_get(ulp_ctx,
190 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
194 /* Protect flow creation */
195 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
196 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
200 /* Allocate a Flow ID for attaching all resources for the flow to.
201 * Once allocated, all errors have to walk the list of resources and
204 rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
207 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
211 /* Parse the rte flow pattern */
212 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
213 if (ret != BNXT_TF_RC_SUCCESS)
216 /* Parse the rte flow action */
217 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
218 if (ret != BNXT_TF_RC_SUCCESS)
222 params.func_id = func_id;
223 params.priority = attr->priority;
224 params.port_id = dev->data->port_id;
226 /* Perform the rte flow post process */
227 bnxt_ulp_rte_parser_post_process(¶ms);
229 /* do the tunnel offload process if any */
230 ret = ulp_tunnel_offload_process(¶ms);
231 if (ret == BNXT_TF_RC_ERROR)
234 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
235 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_PARSER
236 /* Dump the rte flow pattern */
237 ulp_parser_hdr_info_dump(¶ms);
238 /* Dump the rte flow action */
239 ulp_parser_act_info_dump(¶ms);
243 ret = ulp_matcher_pattern_match(¶ms, ¶ms.class_id);
244 if (ret != BNXT_TF_RC_SUCCESS)
247 ret = ulp_matcher_action_match(¶ms, ¶ms.act_tmpl);
248 if (ret != BNXT_TF_RC_SUCCESS)
251 bnxt_ulp_init_mapper_params(&mapper_cparms, ¶ms,
252 BNXT_ULP_FDB_TYPE_REGULAR);
253 /* Call the ulp mapper to create the flow in the hardware. */
254 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
258 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
260 flow_id = (struct rte_flow *)((uintptr_t)fid);
264 ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
266 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
268 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
269 "Failed to create flow.");
273 /* Function to validate the rte flow. */
275 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
276 const struct rte_flow_attr *attr,
277 const struct rte_flow_item pattern[],
278 const struct rte_flow_action actions[],
279 struct rte_flow_error *error)
281 struct ulp_rte_parser_params params;
282 struct bnxt_ulp_context *ulp_ctx;
283 uint32_t class_id, act_tmpl;
284 int ret = BNXT_TF_RC_ERROR;
286 if (bnxt_ulp_flow_validate_args(attr,
288 error) == BNXT_TF_RC_ERROR) {
289 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
293 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
295 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
299 /* Initialize the parser params */
300 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
301 params.ulp_ctx = ulp_ctx;
303 if (bnxt_ulp_cntxt_app_id_get(params.ulp_ctx, ¶ms.app_id)) {
304 BNXT_TF_DBG(ERR, "failed to get the app id\n");
308 /* Set the flow attributes */
309 bnxt_ulp_set_dir_attributes(¶ms, attr);
311 /* Parse the rte flow pattern */
312 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
313 if (ret != BNXT_TF_RC_SUCCESS)
316 /* Parse the rte flow action */
317 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
318 if (ret != BNXT_TF_RC_SUCCESS)
321 /* Perform the rte flow post process */
322 bnxt_ulp_rte_parser_post_process(¶ms);
324 /* do the tunnel offload process if any */
325 ret = ulp_tunnel_offload_process(¶ms);
326 if (ret == BNXT_TF_RC_ERROR)
329 ret = ulp_matcher_pattern_match(¶ms, &class_id);
331 if (ret != BNXT_TF_RC_SUCCESS)
334 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
335 if (ret != BNXT_TF_RC_SUCCESS)
338 /* all good return success */
342 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
343 "Failed to validate flow.");
347 /* Function to destroy the rte flow. */
349 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
350 struct rte_flow *flow,
351 struct rte_flow_error *error)
353 struct bnxt_ulp_context *ulp_ctx;
358 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
360 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
362 rte_flow_error_set(error, EINVAL,
363 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
364 "Failed to destroy flow.");
368 flow_id = (uint32_t)(uintptr_t)flow;
370 if (ulp_port_db_port_func_id_get(ulp_ctx,
373 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
375 rte_flow_error_set(error, EINVAL,
376 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
377 "Failed to destroy flow.");
381 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
383 BNXT_TF_DBG(ERR, "Incorrect device params\n");
385 rte_flow_error_set(error, EINVAL,
386 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
387 "Failed to destroy flow.");
391 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
392 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
395 ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
398 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
400 rte_flow_error_set(error, -ret,
401 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
402 "Failed to destroy flow.");
404 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
409 /* Function to destroy the rte flows. */
411 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
412 struct rte_flow_error *error)
414 struct bnxt_ulp_context *ulp_ctx;
418 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
423 /* Free the resources for the last device */
424 if (ulp_ctx_deinit_allowed(ulp_ctx)) {
425 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
426 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
427 ret = ulp_port_db_port_func_id_get(ulp_ctx,
428 eth_dev->data->port_id,
431 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
433 BNXT_TF_DBG(ERR, "convert port to func id failed\n");
436 rte_flow_error_set(error, ret,
437 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
438 "Failed to flush flow.");
442 /* Function to query the rte flows. */
444 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
445 struct rte_flow *flow,
446 const struct rte_flow_action *action,
448 struct rte_flow_error *error)
451 struct bnxt_ulp_context *ulp_ctx;
452 struct rte_flow_query_count *count;
455 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
457 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
458 rte_flow_error_set(error, EINVAL,
459 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
460 "Failed to query flow.");
464 flow_id = (uint32_t)(uintptr_t)flow;
466 switch (action->type) {
467 case RTE_FLOW_ACTION_TYPE_COUNT:
469 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
471 rte_flow_error_set(error, EINVAL,
472 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
473 "Failed to query flow.");
477 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
478 NULL, "Unsupported action item");
484 /* Tunnel offload Apis */
485 #define BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS 1
488 bnxt_ulp_tunnel_decap_set(struct rte_eth_dev *eth_dev,
489 struct rte_flow_tunnel *tunnel,
490 struct rte_flow_action **pmd_actions,
491 uint32_t *num_of_actions,
492 struct rte_flow_error *error)
494 struct bnxt_ulp_context *ulp_ctx;
495 struct bnxt_flow_app_tun_ent *tun_entry;
498 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
499 if (ulp_ctx == NULL) {
500 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
501 rte_flow_error_set(error, EINVAL,
502 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
503 "ULP context uninitialized");
507 if (tunnel == NULL) {
508 BNXT_TF_DBG(ERR, "No tunnel specified\n");
509 rte_flow_error_set(error, EINVAL,
510 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
511 "no tunnel specified");
515 if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
516 BNXT_TF_DBG(ERR, "Tunnel type unsupported\n");
517 rte_flow_error_set(error, EINVAL,
518 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
519 "tunnel type unsupported");
523 rc = ulp_app_tun_search_entry(ulp_ctx, tunnel, &tun_entry);
525 rte_flow_error_set(error, EINVAL,
526 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
527 "tunnel decap set failed");
531 rc = ulp_app_tun_entry_set_decap_action(tun_entry);
533 rte_flow_error_set(error, EINVAL,
534 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
535 "tunnel decap set failed");
539 *pmd_actions = &tun_entry->action;
540 *num_of_actions = BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS;
545 bnxt_ulp_tunnel_match(struct rte_eth_dev *eth_dev,
546 struct rte_flow_tunnel *tunnel,
547 struct rte_flow_item **pmd_items,
548 uint32_t *num_of_items,
549 struct rte_flow_error *error)
551 struct bnxt_ulp_context *ulp_ctx;
552 struct bnxt_flow_app_tun_ent *tun_entry;
555 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
556 if (ulp_ctx == NULL) {
557 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
558 rte_flow_error_set(error, EINVAL,
559 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
560 "ULP context uninitialized");
564 if (tunnel == NULL) {
565 BNXT_TF_DBG(ERR, "No tunnel specified\n");
566 rte_flow_error_set(error, EINVAL,
567 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
568 "no tunnel specified");
572 if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
573 BNXT_TF_DBG(ERR, "Tunnel type unsupported\n");
574 rte_flow_error_set(error, EINVAL,
575 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
576 "tunnel type unsupported");
580 rc = ulp_app_tun_search_entry(ulp_ctx, tunnel, &tun_entry);
582 rte_flow_error_set(error, EINVAL,
583 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
584 "tunnel match set failed");
588 rc = ulp_app_tun_entry_set_decap_item(tun_entry);
590 rte_flow_error_set(error, EINVAL,
591 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
592 "tunnel match set failed");
596 *pmd_items = &tun_entry->item;
597 *num_of_items = BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS;
602 bnxt_ulp_tunnel_decap_release(struct rte_eth_dev *eth_dev,
603 struct rte_flow_action *pmd_actions,
604 uint32_t num_actions,
605 struct rte_flow_error *error)
607 struct bnxt_ulp_context *ulp_ctx;
608 struct bnxt_flow_app_tun_ent *tun_entry;
609 const struct rte_flow_action *action_item = pmd_actions;
611 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
612 if (ulp_ctx == NULL) {
613 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
614 rte_flow_error_set(error, EINVAL,
615 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
616 "ULP context uninitialized");
619 if (num_actions != BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS) {
620 BNXT_TF_DBG(ERR, "num actions is invalid\n");
621 rte_flow_error_set(error, EINVAL,
622 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
623 "num actions is invalid");
626 while (action_item && action_item->type != RTE_FLOW_ACTION_TYPE_END) {
627 if (action_item->type == (typeof(tun_entry->action.type))
628 BNXT_RTE_FLOW_ACTION_TYPE_VXLAN_DECAP) {
629 tun_entry = ulp_app_tun_match_entry(ulp_ctx,
631 ulp_app_tun_entry_delete(tun_entry);
639 bnxt_ulp_tunnel_item_release(struct rte_eth_dev *eth_dev,
640 struct rte_flow_item *pmd_items,
642 struct rte_flow_error *error)
644 struct bnxt_ulp_context *ulp_ctx;
645 struct bnxt_flow_app_tun_ent *tun_entry;
647 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
648 if (ulp_ctx == NULL) {
649 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
650 rte_flow_error_set(error, EINVAL,
651 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
652 "ULP context uninitialized");
655 if (num_items != BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS) {
656 BNXT_TF_DBG(ERR, "num items is invalid\n");
657 rte_flow_error_set(error, EINVAL,
658 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
659 "num items is invalid");
663 tun_entry = ulp_app_tun_match_entry(ulp_ctx, pmd_items->spec);
664 ulp_app_tun_entry_delete(tun_entry);
668 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
669 .validate = bnxt_ulp_flow_validate,
670 .create = bnxt_ulp_flow_create,
671 .destroy = bnxt_ulp_flow_destroy,
672 .flush = bnxt_ulp_flow_flush,
673 .query = bnxt_ulp_flow_query,
675 /* Tunnel offload callbacks */
676 .tunnel_decap_set = bnxt_ulp_tunnel_decap_set,
677 .tunnel_match = bnxt_ulp_tunnel_match,
678 .tunnel_action_decap_release = bnxt_ulp_tunnel_decap_release,
679 .tunnel_item_release = bnxt_ulp_tunnel_item_release,
680 .get_restore_info = NULL