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 <rte_malloc.h>
17 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
18 const struct rte_flow_item pattern[],
19 const struct rte_flow_action actions[],
20 struct rte_flow_error *error)
22 /* Perform the validation of the arguments for null */
24 return BNXT_TF_RC_ERROR;
27 rte_flow_error_set(error,
29 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
32 return BNXT_TF_RC_ERROR;
36 rte_flow_error_set(error,
38 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
41 return BNXT_TF_RC_ERROR;
45 rte_flow_error_set(error,
47 RTE_FLOW_ERROR_TYPE_ATTR,
50 return BNXT_TF_RC_ERROR;
53 if (attr->egress && attr->ingress) {
54 rte_flow_error_set(error,
56 RTE_FLOW_ERROR_TYPE_ATTR,
58 "EGRESS AND INGRESS UNSUPPORTED");
59 return BNXT_TF_RC_ERROR;
61 return BNXT_TF_RC_SUCCESS;
65 bnxt_ulp_set_dir_attributes(struct ulp_rte_parser_params *params,
66 const struct rte_flow_attr *attr)
68 /* Set the flow attributes */
70 params->dir_attr |= BNXT_ULP_FLOW_ATTR_EGRESS;
72 params->dir_attr |= BNXT_ULP_FLOW_ATTR_INGRESS;
73 #if RTE_VERSION_NUM(17, 11, 10, 16) < RTE_VERSION
75 params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
80 bnxt_ulp_init_mapper_params(struct bnxt_ulp_mapper_create_parms *mapper_cparms,
81 struct ulp_rte_parser_params *params,
82 enum bnxt_ulp_fdb_type flow_type)
84 memset(mapper_cparms, 0, sizeof(*mapper_cparms));
85 mapper_cparms->flow_type = flow_type;
86 mapper_cparms->app_priority = params->priority;
87 mapper_cparms->dir_attr = params->dir_attr;
88 mapper_cparms->class_tid = params->class_id;
89 mapper_cparms->act_tid = params->act_tmpl;
90 mapper_cparms->func_id = params->func_id;
91 mapper_cparms->hdr_bitmap = ¶ms->hdr_bitmap;
92 mapper_cparms->hdr_field = params->hdr_field;
93 mapper_cparms->comp_fld = params->comp_fld;
94 mapper_cparms->act = ¶ms->act_bitmap;
95 mapper_cparms->act_prop = ¶ms->act_prop;
96 mapper_cparms->flow_id = params->fid;
97 mapper_cparms->parent_flow = params->parent_flow;
98 mapper_cparms->parent_fid = params->parent_fid;
99 mapper_cparms->fld_bitmap = ¶ms->fld_bitmap;
100 mapper_cparms->flow_pattern_id = params->flow_pattern_id;
101 mapper_cparms->act_pattern_id = params->act_pattern_id;
102 mapper_cparms->app_id = params->app_id;
104 /* update the signature fields into the computed field list */
105 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_HDR_SIG_ID,
107 ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_FLOW_SIG_ID,
108 params->flow_sig_id);
111 /* Function to create the rte flow. */
112 static struct rte_flow *
113 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
114 const struct rte_flow_attr *attr,
115 const struct rte_flow_item pattern[],
116 const struct rte_flow_action actions[],
117 struct rte_flow_error *error)
119 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
120 struct ulp_rte_parser_params params;
121 struct bnxt_ulp_context *ulp_ctx;
122 int rc, ret = BNXT_TF_RC_ERROR;
123 struct rte_flow *flow_id;
127 if (bnxt_ulp_flow_validate_args(attr,
129 error) == BNXT_TF_RC_ERROR) {
130 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
134 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
136 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
140 /* Initialize the parser params */
141 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
142 params.ulp_ctx = ulp_ctx;
144 if (bnxt_ulp_cntxt_app_id_get(params.ulp_ctx, ¶ms.app_id)) {
145 BNXT_TF_DBG(ERR, "failed to get the app id\n");
149 /* Set the flow attributes */
150 bnxt_ulp_set_dir_attributes(¶ms, attr);
152 /* copy the device port id and direction for further processing */
153 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
155 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_DEV_PORT_ID,
157 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
158 BNXT_ULP_INVALID_SVIF_VAL);
160 /* Get the function id */
161 if (ulp_port_db_port_func_id_get(ulp_ctx,
164 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
168 /* Protect flow creation */
169 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
170 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
174 /* Allocate a Flow ID for attaching all resources for the flow to.
175 * Once allocated, all errors have to walk the list of resources and
178 rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
181 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
185 /* Parse the rte flow pattern */
186 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
187 if (ret != BNXT_TF_RC_SUCCESS)
190 /* Parse the rte flow action */
191 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
192 if (ret != BNXT_TF_RC_SUCCESS)
196 params.func_id = func_id;
197 params.priority = attr->priority;
198 params.port_id = dev->data->port_id;
199 /* Perform the rte flow post process */
200 ret = bnxt_ulp_rte_parser_post_process(¶ms);
201 if (ret == BNXT_TF_RC_ERROR)
203 else if (ret == BNXT_TF_RC_FID)
206 ret = ulp_matcher_pattern_match(¶ms, ¶ms.class_id);
207 if (ret != BNXT_TF_RC_SUCCESS)
210 ret = ulp_matcher_action_match(¶ms, ¶ms.act_tmpl);
211 if (ret != BNXT_TF_RC_SUCCESS)
214 bnxt_ulp_init_mapper_params(&mapper_cparms, ¶ms,
215 BNXT_ULP_FDB_TYPE_REGULAR);
216 /* Call the ulp mapper to create the flow in the hardware. */
217 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
222 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
224 flow_id = (struct rte_flow *)((uintptr_t)fid);
228 ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
230 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
232 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
233 "Failed to create flow.");
237 /* Function to validate the rte flow. */
239 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
240 const struct rte_flow_attr *attr,
241 const struct rte_flow_item pattern[],
242 const struct rte_flow_action actions[],
243 struct rte_flow_error *error)
245 struct ulp_rte_parser_params params;
246 struct bnxt_ulp_context *ulp_ctx;
247 uint32_t class_id, act_tmpl;
248 int ret = BNXT_TF_RC_ERROR;
250 if (bnxt_ulp_flow_validate_args(attr,
252 error) == BNXT_TF_RC_ERROR) {
253 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
257 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
259 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
263 /* Initialize the parser params */
264 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
265 params.ulp_ctx = ulp_ctx;
267 if (bnxt_ulp_cntxt_app_id_get(params.ulp_ctx, ¶ms.app_id)) {
268 BNXT_TF_DBG(ERR, "failed to get the app id\n");
272 /* Set the flow attributes */
273 bnxt_ulp_set_dir_attributes(¶ms, attr);
275 /* Parse the rte flow pattern */
276 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
277 if (ret != BNXT_TF_RC_SUCCESS)
280 /* Parse the rte flow action */
281 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
282 if (ret != BNXT_TF_RC_SUCCESS)
285 /* Perform the rte flow post process */
286 ret = bnxt_ulp_rte_parser_post_process(¶ms);
287 if (ret == BNXT_TF_RC_ERROR)
289 else if (ret == BNXT_TF_RC_FID)
292 ret = ulp_matcher_pattern_match(¶ms, &class_id);
294 if (ret != BNXT_TF_RC_SUCCESS)
297 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
298 if (ret != BNXT_TF_RC_SUCCESS)
301 /* all good return success */
305 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
306 "Failed to validate flow.");
310 /* Function to destroy the rte flow. */
312 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
313 struct rte_flow *flow,
314 struct rte_flow_error *error)
316 struct bnxt_ulp_context *ulp_ctx;
321 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
323 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
325 rte_flow_error_set(error, EINVAL,
326 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
327 "Failed to destroy flow.");
331 flow_id = (uint32_t)(uintptr_t)flow;
333 if (ulp_port_db_port_func_id_get(ulp_ctx,
336 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
338 rte_flow_error_set(error, EINVAL,
339 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
340 "Failed to destroy flow.");
344 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
346 BNXT_TF_DBG(ERR, "Incorrect device params\n");
348 rte_flow_error_set(error, EINVAL,
349 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
350 "Failed to destroy flow.");
354 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
355 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
358 ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
361 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
363 rte_flow_error_set(error, -ret,
364 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
365 "Failed to destroy flow.");
367 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
372 /* Function to destroy the rte flows. */
374 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
375 struct rte_flow_error *error)
377 struct bnxt_ulp_context *ulp_ctx;
381 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
386 /* Free the resources for the last device */
387 if (ulp_ctx_deinit_allowed(ulp_ctx)) {
388 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
389 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
390 ret = ulp_port_db_port_func_id_get(ulp_ctx,
391 eth_dev->data->port_id,
394 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
396 BNXT_TF_DBG(ERR, "convert port to func id failed\n");
399 rte_flow_error_set(error, ret,
400 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
401 "Failed to flush flow.");
405 /* Function to query the rte flows. */
407 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
408 struct rte_flow *flow,
409 const struct rte_flow_action *action,
411 struct rte_flow_error *error)
414 struct bnxt_ulp_context *ulp_ctx;
415 struct rte_flow_query_count *count;
418 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
420 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
421 rte_flow_error_set(error, EINVAL,
422 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
423 "Failed to query flow.");
427 flow_id = (uint32_t)(uintptr_t)flow;
429 switch (action->type) {
430 case RTE_FLOW_ACTION_TYPE_COUNT:
432 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
434 rte_flow_error_set(error, EINVAL,
435 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
436 "Failed to query flow.");
440 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
441 NULL, "Unsupported action item");
447 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
448 .validate = bnxt_ulp_flow_validate,
449 .create = bnxt_ulp_flow_create,
450 .destroy = bnxt_ulp_flow_destroy,
451 .flush = bnxt_ulp_flow_flush,
452 .query = bnxt_ulp_flow_query,