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;
74 params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
78 bnxt_ulp_init_mapper_params(struct bnxt_ulp_mapper_create_parms *mapper_cparms,
79 struct ulp_rte_parser_params *params,
80 enum bnxt_ulp_fdb_type flow_type)
82 memset(mapper_cparms, 0, sizeof(*mapper_cparms));
83 mapper_cparms->flow_type = flow_type;
84 mapper_cparms->app_priority = params->priority;
85 mapper_cparms->dir_attr = params->dir_attr;
86 mapper_cparms->class_tid = params->class_id;
87 mapper_cparms->act_tid = params->act_tmpl;
88 mapper_cparms->func_id = params->func_id;
89 mapper_cparms->hdr_bitmap = ¶ms->hdr_bitmap;
90 mapper_cparms->hdr_field = params->hdr_field;
91 mapper_cparms->comp_fld = params->comp_fld;
92 mapper_cparms->act = ¶ms->act_bitmap;
93 mapper_cparms->act_prop = ¶ms->act_prop;
94 mapper_cparms->flow_id = params->fid;
95 mapper_cparms->parent_flow = params->parent_flow;
96 mapper_cparms->parent_fid = params->parent_fid;
97 mapper_cparms->fld_bitmap = ¶ms->fld_bitmap;
100 /* Function to create the rte flow. */
101 static struct rte_flow *
102 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
103 const struct rte_flow_attr *attr,
104 const struct rte_flow_item pattern[],
105 const struct rte_flow_action actions[],
106 struct rte_flow_error *error)
108 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
109 struct ulp_rte_parser_params params;
110 struct bnxt_ulp_context *ulp_ctx;
111 int rc, ret = BNXT_TF_RC_ERROR;
112 struct rte_flow *flow_id;
116 if (bnxt_ulp_flow_validate_args(attr,
118 error) == BNXT_TF_RC_ERROR) {
119 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
123 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
125 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
129 /* Initialize the parser params */
130 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
131 params.ulp_ctx = ulp_ctx;
133 /* Set the flow attributes */
134 bnxt_ulp_set_dir_attributes(¶ms, attr);
136 /* copy the device port id and direction for further processing */
137 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
139 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
140 BNXT_ULP_INVALID_SVIF_VAL);
142 /* Get the function id */
143 if (ulp_port_db_port_func_id_get(ulp_ctx,
146 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
150 /* Protect flow creation */
151 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
152 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
156 /* Allocate a Flow ID for attaching all resources for the flow to.
157 * Once allocated, all errors have to walk the list of resources and
160 rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
163 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
167 /* Parse the rte flow pattern */
168 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
169 if (ret != BNXT_TF_RC_SUCCESS)
172 /* Parse the rte flow action */
173 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
174 if (ret != BNXT_TF_RC_SUCCESS)
178 params.func_id = func_id;
179 params.priority = attr->priority;
180 params.port_id = dev->data->port_id;
181 /* Perform the rte flow post process */
182 ret = bnxt_ulp_rte_parser_post_process(¶ms);
183 if (ret == BNXT_TF_RC_ERROR)
185 else if (ret == BNXT_TF_RC_FID)
188 ret = ulp_matcher_pattern_match(¶ms, ¶ms.class_id);
189 if (ret != BNXT_TF_RC_SUCCESS)
192 ret = ulp_matcher_action_match(¶ms, ¶ms.act_tmpl);
193 if (ret != BNXT_TF_RC_SUCCESS)
196 bnxt_ulp_init_mapper_params(&mapper_cparms, ¶ms,
197 BNXT_ULP_FDB_TYPE_REGULAR);
198 /* Call the ulp mapper to create the flow in the hardware. */
199 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
204 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
206 flow_id = (struct rte_flow *)((uintptr_t)fid);
210 ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
212 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
214 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
215 "Failed to create flow.");
219 /* Function to validate the rte flow. */
221 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
222 const struct rte_flow_attr *attr,
223 const struct rte_flow_item pattern[],
224 const struct rte_flow_action actions[],
225 struct rte_flow_error *error)
227 struct ulp_rte_parser_params params;
228 struct bnxt_ulp_context *ulp_ctx;
229 uint32_t class_id, act_tmpl;
230 int ret = BNXT_TF_RC_ERROR;
232 if (bnxt_ulp_flow_validate_args(attr,
234 error) == BNXT_TF_RC_ERROR) {
235 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
239 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
241 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
245 /* Initialize the parser params */
246 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
247 params.ulp_ctx = ulp_ctx;
249 /* Set the flow attributes */
250 bnxt_ulp_set_dir_attributes(¶ms, attr);
252 /* Parse the rte flow pattern */
253 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
254 if (ret != BNXT_TF_RC_SUCCESS)
257 /* Parse the rte flow action */
258 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
259 if (ret != BNXT_TF_RC_SUCCESS)
262 /* Perform the rte flow post process */
263 ret = bnxt_ulp_rte_parser_post_process(¶ms);
264 if (ret == BNXT_TF_RC_ERROR)
266 else if (ret == BNXT_TF_RC_FID)
269 ret = ulp_matcher_pattern_match(¶ms, &class_id);
271 if (ret != BNXT_TF_RC_SUCCESS)
274 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
275 if (ret != BNXT_TF_RC_SUCCESS)
278 /* all good return success */
282 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
283 "Failed to validate flow.");
287 /* Function to destroy the rte flow. */
289 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
290 struct rte_flow *flow,
291 struct rte_flow_error *error)
293 struct bnxt_ulp_context *ulp_ctx;
298 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
300 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
302 rte_flow_error_set(error, EINVAL,
303 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
304 "Failed to destroy flow.");
308 flow_id = (uint32_t)(uintptr_t)flow;
310 if (ulp_port_db_port_func_id_get(ulp_ctx,
313 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
315 rte_flow_error_set(error, EINVAL,
316 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
317 "Failed to destroy flow.");
321 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
323 BNXT_TF_DBG(ERR, "Incorrect device params\n");
325 rte_flow_error_set(error, EINVAL,
326 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
327 "Failed to destroy flow.");
331 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
332 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
335 ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
338 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
340 rte_flow_error_set(error, -ret,
341 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
342 "Failed to destroy flow.");
344 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
349 /* Function to destroy the rte flows. */
351 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
352 struct rte_flow_error *error)
354 struct bnxt_ulp_context *ulp_ctx;
358 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
363 /* Free the resources for the last device */
364 if (ulp_ctx_deinit_allowed(ulp_ctx)) {
365 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
366 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
367 ret = ulp_port_db_port_func_id_get(ulp_ctx,
368 eth_dev->data->port_id,
371 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
373 BNXT_TF_DBG(ERR, "convert port to func id failed\n");
376 rte_flow_error_set(error, ret,
377 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
378 "Failed to flush flow.");
382 /* Function to query the rte flows. */
384 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
385 struct rte_flow *flow,
386 const struct rte_flow_action *action,
388 struct rte_flow_error *error)
391 struct bnxt_ulp_context *ulp_ctx;
392 struct rte_flow_query_count *count;
395 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
397 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
398 rte_flow_error_set(error, EINVAL,
399 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
400 "Failed to query flow.");
404 flow_id = (uint32_t)(uintptr_t)flow;
406 switch (action->type) {
407 case RTE_FLOW_ACTION_TYPE_COUNT:
409 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
411 rte_flow_error_set(error, EINVAL,
412 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
413 "Failed to query flow.");
417 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
418 NULL, "Unsupported action item");
424 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
425 .validate = bnxt_ulp_flow_validate,
426 .create = bnxt_ulp_flow_create,
427 .destroy = bnxt_ulp_flow_destroy,
428 .flush = bnxt_ulp_flow_flush,
429 .query = bnxt_ulp_flow_query,