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>
19 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
20 const struct rte_flow_item pattern[],
21 const struct rte_flow_action actions[],
22 struct rte_flow_error *error)
24 /* Perform the validation of the arguments for null */
26 return BNXT_TF_RC_ERROR;
29 rte_flow_error_set(error,
31 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
34 return BNXT_TF_RC_ERROR;
38 rte_flow_error_set(error,
40 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
43 return BNXT_TF_RC_ERROR;
47 rte_flow_error_set(error,
49 RTE_FLOW_ERROR_TYPE_ATTR,
52 return BNXT_TF_RC_ERROR;
55 if (attr->egress && attr->ingress) {
56 rte_flow_error_set(error,
58 RTE_FLOW_ERROR_TYPE_ATTR,
60 "EGRESS AND INGRESS UNSUPPORTED");
61 return BNXT_TF_RC_ERROR;
63 return BNXT_TF_RC_SUCCESS;
67 bnxt_ulp_set_dir_attributes(struct ulp_rte_parser_params *params,
68 const struct rte_flow_attr *attr)
70 /* Set the flow attributes */
72 params->dir_attr |= BNXT_ULP_FLOW_ATTR_EGRESS;
74 params->dir_attr |= BNXT_ULP_FLOW_ATTR_INGRESS;
75 #if RTE_VERSION_NUM(17, 11, 10, 16) < RTE_VERSION
77 params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
82 bnxt_ulp_init_mapper_params(struct bnxt_ulp_mapper_create_parms *mapper_cparms,
83 struct ulp_rte_parser_params *params,
84 enum bnxt_ulp_fdb_type flow_type)
86 uint32_t ulp_flags = 0;
88 memset(mapper_cparms, 0, sizeof(*mapper_cparms));
89 mapper_cparms->flow_type = flow_type;
90 mapper_cparms->app_priority = params->priority;
91 mapper_cparms->dir_attr = params->dir_attr;
92 mapper_cparms->class_tid = params->class_id;
93 mapper_cparms->act_tid = params->act_tmpl;
94 mapper_cparms->func_id = params->func_id;
95 mapper_cparms->hdr_bitmap = ¶ms->hdr_bitmap;
96 mapper_cparms->enc_hdr_bitmap = ¶ms->enc_hdr_bitmap;
97 mapper_cparms->hdr_field = params->hdr_field;
98 mapper_cparms->enc_field = params->enc_field;
99 mapper_cparms->comp_fld = params->comp_fld;
100 mapper_cparms->act = ¶ms->act_bitmap;
101 mapper_cparms->act_prop = ¶ms->act_prop;
102 mapper_cparms->flow_id = params->fid;
103 mapper_cparms->parent_flow = params->parent_flow;
104 mapper_cparms->child_flow = params->child_flow;
105 mapper_cparms->fld_bitmap = ¶ms->fld_bitmap;
106 mapper_cparms->flow_pattern_id = params->flow_pattern_id;
107 mapper_cparms->act_pattern_id = params->act_pattern_id;
108 mapper_cparms->app_id = params->app_id;
109 mapper_cparms->port_id = params->port_id;
110 mapper_cparms->tun_idx = params->tun_idx;
112 /* update the signature fields into the computed field list */
113 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_HDR_SIG_ID,
115 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_FLOW_SIG_ID,
116 params->flow_sig_id);
118 if (bnxt_ulp_cntxt_ptr2_ulp_flags_get(params->ulp_ctx, &ulp_flags))
121 /* update the WC Priority flag */
122 if (ULP_HIGH_AVAIL_IS_ENABLED(ulp_flags)) {
123 enum ulp_ha_mgr_region region = ULP_HA_REGION_LOW;
126 rc = ulp_ha_mgr_region_get(params->ulp_ctx, ®ion);
128 BNXT_TF_DBG(ERR, "Unable to get WC region\n");
129 if (region == ULP_HA_REGION_HI)
130 ULP_COMP_FLD_IDX_WR(params,
131 BNXT_ULP_CF_IDX_WC_IS_HA_HIGH_REG,
135 /* Update the socket direct flag */
136 if (ULP_BITMAP_ISSET(params->hdr_bitmap.bits,
137 BNXT_ULP_HDR_BIT_SVIF_IGNORE)) {
141 /* Get the port db ifindex */
142 if (ulp_port_db_dev_port_to_ulp_index(params->ulp_ctx,
145 BNXT_TF_DBG(ERR, "Invalid port id %u\n",
149 /* Update the phy port of the other interface */
150 if (ulp_port_db_vport_get(params->ulp_ctx, ifindex, &vport)) {
151 BNXT_TF_DBG(ERR, "Invalid port if index %u\n", ifindex);
154 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_SOCKET_DIRECT_VPORT,
155 (vport == 1) ? 2 : 1);
159 /* Function to create the rte flow. */
160 static struct rte_flow *
161 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
162 const struct rte_flow_attr *attr,
163 const struct rte_flow_item pattern[],
164 const struct rte_flow_action actions[],
165 struct rte_flow_error *error)
167 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
168 struct ulp_rte_parser_params params;
169 struct bnxt_ulp_context *ulp_ctx;
170 int rc, ret = BNXT_TF_RC_ERROR;
171 struct rte_flow *flow_id;
175 if (bnxt_ulp_flow_validate_args(attr,
177 error) == BNXT_TF_RC_ERROR) {
178 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
182 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
184 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
188 /* Initialize the parser params */
189 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
190 params.ulp_ctx = ulp_ctx;
192 if (bnxt_ulp_cntxt_app_id_get(params.ulp_ctx, ¶ms.app_id)) {
193 BNXT_TF_DBG(ERR, "failed to get the app id\n");
197 /* Set the flow attributes */
198 bnxt_ulp_set_dir_attributes(¶ms, attr);
200 /* copy the device port id and direction for further processing */
201 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
203 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_DEV_PORT_ID,
205 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
206 BNXT_ULP_INVALID_SVIF_VAL);
208 /* Get the function id */
209 if (ulp_port_db_port_func_id_get(ulp_ctx,
212 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
216 /* Protect flow creation */
217 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
218 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
222 /* Allocate a Flow ID for attaching all resources for the flow to.
223 * Once allocated, all errors have to walk the list of resources and
226 rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
229 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
233 /* Parse the rte flow pattern */
234 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
235 if (ret != BNXT_TF_RC_SUCCESS)
238 /* Parse the rte flow action */
239 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
240 if (ret != BNXT_TF_RC_SUCCESS)
244 params.func_id = func_id;
245 params.priority = attr->priority;
246 params.port_id = dev->data->port_id;
248 /* Perform the rte flow post process */
249 bnxt_ulp_rte_parser_post_process(¶ms);
251 /* do the tunnel offload process if any */
252 ret = ulp_tunnel_offload_process(¶ms);
253 if (ret == BNXT_TF_RC_ERROR)
256 ret = ulp_matcher_pattern_match(¶ms, ¶ms.class_id);
257 if (ret != BNXT_TF_RC_SUCCESS)
260 ret = ulp_matcher_action_match(¶ms, ¶ms.act_tmpl);
261 if (ret != BNXT_TF_RC_SUCCESS)
264 bnxt_ulp_init_mapper_params(&mapper_cparms, ¶ms,
265 BNXT_ULP_FDB_TYPE_REGULAR);
266 /* Call the ulp mapper to create the flow in the hardware. */
267 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
271 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
273 flow_id = (struct rte_flow *)((uintptr_t)fid);
277 ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
279 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
281 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
282 "Failed to create flow.");
286 /* Function to validate the rte flow. */
288 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
289 const struct rte_flow_attr *attr,
290 const struct rte_flow_item pattern[],
291 const struct rte_flow_action actions[],
292 struct rte_flow_error *error)
294 struct ulp_rte_parser_params params;
295 struct bnxt_ulp_context *ulp_ctx;
296 uint32_t class_id, act_tmpl;
297 int ret = BNXT_TF_RC_ERROR;
299 if (bnxt_ulp_flow_validate_args(attr,
301 error) == BNXT_TF_RC_ERROR) {
302 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
306 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
308 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
312 /* Initialize the parser params */
313 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
314 params.ulp_ctx = ulp_ctx;
316 if (bnxt_ulp_cntxt_app_id_get(params.ulp_ctx, ¶ms.app_id)) {
317 BNXT_TF_DBG(ERR, "failed to get the app id\n");
321 /* Set the flow attributes */
322 bnxt_ulp_set_dir_attributes(¶ms, attr);
324 /* Parse the rte flow pattern */
325 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
326 if (ret != BNXT_TF_RC_SUCCESS)
329 /* Parse the rte flow action */
330 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
331 if (ret != BNXT_TF_RC_SUCCESS)
334 /* Perform the rte flow post process */
335 bnxt_ulp_rte_parser_post_process(¶ms);
337 /* do the tunnel offload process if any */
338 ret = ulp_tunnel_offload_process(¶ms);
339 if (ret == BNXT_TF_RC_ERROR)
342 ret = ulp_matcher_pattern_match(¶ms, &class_id);
344 if (ret != BNXT_TF_RC_SUCCESS)
347 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
348 if (ret != BNXT_TF_RC_SUCCESS)
351 /* all good return success */
355 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
356 "Failed to validate flow.");
360 /* Function to destroy the rte flow. */
362 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
363 struct rte_flow *flow,
364 struct rte_flow_error *error)
366 struct bnxt_ulp_context *ulp_ctx;
371 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
373 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
375 rte_flow_error_set(error, EINVAL,
376 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
377 "Failed to destroy flow.");
381 flow_id = (uint32_t)(uintptr_t)flow;
383 if (ulp_port_db_port_func_id_get(ulp_ctx,
386 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
388 rte_flow_error_set(error, EINVAL,
389 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
390 "Failed to destroy flow.");
394 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
396 BNXT_TF_DBG(ERR, "Incorrect device params\n");
398 rte_flow_error_set(error, EINVAL,
399 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
400 "Failed to destroy flow.");
404 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
405 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
408 ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
411 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
413 rte_flow_error_set(error, -ret,
414 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
415 "Failed to destroy flow.");
417 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
422 /* Function to destroy the rte flows. */
424 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
425 struct rte_flow_error *error)
427 struct bnxt_ulp_context *ulp_ctx;
431 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
436 /* Free the resources for the last device */
437 if (ulp_ctx_deinit_allowed(ulp_ctx)) {
438 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
439 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
440 ret = ulp_port_db_port_func_id_get(ulp_ctx,
441 eth_dev->data->port_id,
444 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
446 BNXT_TF_DBG(ERR, "convert port to func id failed\n");
449 rte_flow_error_set(error, ret,
450 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
451 "Failed to flush flow.");
455 /* Function to query the rte flows. */
457 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
458 struct rte_flow *flow,
459 const struct rte_flow_action *action,
461 struct rte_flow_error *error)
464 struct bnxt_ulp_context *ulp_ctx;
465 struct rte_flow_query_count *count;
468 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
470 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
471 rte_flow_error_set(error, EINVAL,
472 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
473 "Failed to query flow.");
477 flow_id = (uint32_t)(uintptr_t)flow;
479 switch (action->type) {
480 case RTE_FLOW_ACTION_TYPE_COUNT:
482 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
484 rte_flow_error_set(error, EINVAL,
485 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
486 "Failed to query flow.");
490 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
491 NULL, "Unsupported action item");
497 /* Tunnel offload Apis */
498 #define BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS 1
501 bnxt_ulp_tunnel_decap_set(struct rte_eth_dev *eth_dev,
502 struct rte_flow_tunnel *tunnel,
503 struct rte_flow_action **pmd_actions,
504 uint32_t *num_of_actions,
505 struct rte_flow_error *error)
507 struct bnxt_ulp_context *ulp_ctx;
508 struct bnxt_flow_app_tun_ent *tun_entry;
511 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
512 if (ulp_ctx == NULL) {
513 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
514 rte_flow_error_set(error, EINVAL,
515 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
516 "ULP context uninitialized");
520 if (tunnel == NULL) {
521 BNXT_TF_DBG(ERR, "No tunnel specified\n");
522 rte_flow_error_set(error, EINVAL,
523 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
524 "no tunnel specified");
528 if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
529 BNXT_TF_DBG(ERR, "Tunnel type unsupported\n");
530 rte_flow_error_set(error, EINVAL,
531 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
532 "tunnel type unsupported");
536 rc = ulp_app_tun_search_entry(ulp_ctx, tunnel, &tun_entry);
538 rte_flow_error_set(error, EINVAL,
539 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
540 "tunnel decap set failed");
544 rc = ulp_app_tun_entry_set_decap_action(tun_entry);
546 rte_flow_error_set(error, EINVAL,
547 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
548 "tunnel decap set failed");
552 *pmd_actions = &tun_entry->action;
553 *num_of_actions = BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS;
558 bnxt_ulp_tunnel_match(struct rte_eth_dev *eth_dev,
559 struct rte_flow_tunnel *tunnel,
560 struct rte_flow_item **pmd_items,
561 uint32_t *num_of_items,
562 struct rte_flow_error *error)
564 struct bnxt_ulp_context *ulp_ctx;
565 struct bnxt_flow_app_tun_ent *tun_entry;
568 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
569 if (ulp_ctx == NULL) {
570 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
571 rte_flow_error_set(error, EINVAL,
572 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
573 "ULP context uninitialized");
577 if (tunnel == NULL) {
578 BNXT_TF_DBG(ERR, "No tunnel specified\n");
579 rte_flow_error_set(error, EINVAL,
580 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
581 "no tunnel specified");
585 if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
586 BNXT_TF_DBG(ERR, "Tunnel type unsupported\n");
587 rte_flow_error_set(error, EINVAL,
588 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
589 "tunnel type unsupported");
593 rc = ulp_app_tun_search_entry(ulp_ctx, tunnel, &tun_entry);
595 rte_flow_error_set(error, EINVAL,
596 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
597 "tunnel match set failed");
601 rc = ulp_app_tun_entry_set_decap_item(tun_entry);
603 rte_flow_error_set(error, EINVAL,
604 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
605 "tunnel match set failed");
609 *pmd_items = &tun_entry->item;
610 *num_of_items = BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS;
615 bnxt_ulp_tunnel_decap_release(struct rte_eth_dev *eth_dev,
616 struct rte_flow_action *pmd_actions,
617 uint32_t num_actions,
618 struct rte_flow_error *error)
620 struct bnxt_ulp_context *ulp_ctx;
621 struct bnxt_flow_app_tun_ent *tun_entry;
622 const struct rte_flow_action *action_item = pmd_actions;
624 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
625 if (ulp_ctx == NULL) {
626 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
627 rte_flow_error_set(error, EINVAL,
628 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
629 "ULP context uninitialized");
632 if (num_actions != BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS) {
633 BNXT_TF_DBG(ERR, "num actions is invalid\n");
634 rte_flow_error_set(error, EINVAL,
635 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
636 "num actions is invalid");
639 while (action_item && action_item->type != RTE_FLOW_ACTION_TYPE_END) {
640 if (action_item->type == (typeof(tun_entry->action.type))
641 BNXT_RTE_FLOW_ACTION_TYPE_VXLAN_DECAP) {
642 tun_entry = ulp_app_tun_match_entry(ulp_ctx,
644 ulp_app_tun_entry_delete(tun_entry);
652 bnxt_ulp_tunnel_item_release(struct rte_eth_dev *eth_dev,
653 struct rte_flow_item *pmd_items,
655 struct rte_flow_error *error)
657 struct bnxt_ulp_context *ulp_ctx;
658 struct bnxt_flow_app_tun_ent *tun_entry;
660 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
661 if (ulp_ctx == NULL) {
662 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
663 rte_flow_error_set(error, EINVAL,
664 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
665 "ULP context uninitialized");
668 if (num_items != BNXT_ULP_TUNNEL_OFFLOAD_NUM_ITEMS) {
669 BNXT_TF_DBG(ERR, "num items is invalid\n");
670 rte_flow_error_set(error, EINVAL,
671 RTE_FLOW_ERROR_TYPE_ATTR, NULL,
672 "num items is invalid");
676 tun_entry = ulp_app_tun_match_entry(ulp_ctx, pmd_items->spec);
677 ulp_app_tun_entry_delete(tun_entry);
681 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
682 .validate = bnxt_ulp_flow_validate,
683 .create = bnxt_ulp_flow_create,
684 .destroy = bnxt_ulp_flow_destroy,
685 .flush = bnxt_ulp_flow_flush,
686 .query = bnxt_ulp_flow_query,
688 /* Tunnel offload callbacks */
689 .tunnel_decap_set = bnxt_ulp_tunnel_decap_set,
690 .tunnel_match = bnxt_ulp_tunnel_match,
691 .tunnel_action_decap_release = bnxt_ulp_tunnel_decap_release,
692 .tunnel_item_release = bnxt_ulp_tunnel_item_release,
693 .get_restore_info = NULL