1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 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 mapper_cparms->flow_type = flow_type;
83 mapper_cparms->app_priority = params->priority;
84 mapper_cparms->dir_attr = params->dir_attr;
85 mapper_cparms->class_tid = params->class_id;
86 mapper_cparms->act_tid = params->act_tmpl;
87 mapper_cparms->func_id = params->func_id;
88 mapper_cparms->hdr_bitmap = ¶ms->hdr_bitmap;
89 mapper_cparms->hdr_field = params->hdr_field;
90 mapper_cparms->comp_fld = params->comp_fld;
91 mapper_cparms->act = ¶ms->act_bitmap;
92 mapper_cparms->act_prop = ¶ms->act_prop;
93 mapper_cparms->flow_id = params->fid;
94 mapper_cparms->parent_flow = params->parent_flow;
95 mapper_cparms->parent_fid = params->parent_fid;
98 /* Function to create the rte flow. */
99 static struct rte_flow *
100 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
101 const struct rte_flow_attr *attr,
102 const struct rte_flow_item pattern[],
103 const struct rte_flow_action actions[],
104 struct rte_flow_error *error)
106 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
107 struct ulp_rte_parser_params params;
108 struct bnxt_ulp_context *ulp_ctx;
109 int rc, ret = BNXT_TF_RC_ERROR;
110 struct rte_flow *flow_id;
114 if (bnxt_ulp_flow_validate_args(attr,
116 error) == BNXT_TF_RC_ERROR) {
117 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
121 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
123 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
127 /* Initialize the parser params */
128 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
129 params.ulp_ctx = ulp_ctx;
131 /* Set the flow attributes */
132 bnxt_ulp_set_dir_attributes(¶ms, attr);
134 /* copy the device port id and direction for further processing */
135 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
137 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
138 BNXT_ULP_INVALID_SVIF_VAL);
140 /* Get the function id */
141 if (ulp_port_db_port_func_id_get(ulp_ctx,
144 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
148 /* Protect flow creation */
149 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
150 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
154 /* Allocate a Flow ID for attaching all resources for the flow to.
155 * Once allocated, all errors have to walk the list of resources and
158 rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
161 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
165 /* Parse the rte flow pattern */
166 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
167 if (ret != BNXT_TF_RC_SUCCESS)
170 /* Parse the rte flow action */
171 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
172 if (ret != BNXT_TF_RC_SUCCESS)
176 params.func_id = func_id;
177 params.priority = attr->priority;
178 params.port_id = bnxt_get_phy_port_id(dev->data->port_id);
179 /* Perform the rte flow post process */
180 ret = bnxt_ulp_rte_parser_post_process(¶ms);
181 if (ret == BNXT_TF_RC_ERROR)
183 else if (ret == BNXT_TF_RC_FID)
186 ret = ulp_matcher_pattern_match(¶ms, ¶ms.class_id);
187 if (ret != BNXT_TF_RC_SUCCESS)
190 ret = ulp_matcher_action_match(¶ms, ¶ms.act_tmpl);
191 if (ret != BNXT_TF_RC_SUCCESS)
194 bnxt_ulp_init_mapper_params(&mapper_cparms, ¶ms,
195 BNXT_ULP_FDB_TYPE_REGULAR);
196 /* Call the ulp mapper to create the flow in the hardware. */
197 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
202 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
204 flow_id = (struct rte_flow *)((uintptr_t)fid);
208 ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
210 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
212 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
213 "Failed to create flow.");
217 /* Function to validate the rte flow. */
219 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
220 const struct rte_flow_attr *attr,
221 const struct rte_flow_item pattern[],
222 const struct rte_flow_action actions[],
223 struct rte_flow_error *error)
225 struct ulp_rte_parser_params params;
226 struct bnxt_ulp_context *ulp_ctx;
227 uint32_t class_id, act_tmpl;
228 int ret = BNXT_TF_RC_ERROR;
230 if (bnxt_ulp_flow_validate_args(attr,
232 error) == BNXT_TF_RC_ERROR) {
233 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
237 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
239 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
243 /* Initialize the parser params */
244 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
245 params.ulp_ctx = ulp_ctx;
247 /* Set the flow attributes */
248 bnxt_ulp_set_dir_attributes(¶ms, attr);
250 /* Parse the rte flow pattern */
251 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
252 if (ret != BNXT_TF_RC_SUCCESS)
255 /* Parse the rte flow action */
256 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
257 if (ret != BNXT_TF_RC_SUCCESS)
260 /* Perform the rte flow post process */
261 ret = bnxt_ulp_rte_parser_post_process(¶ms);
262 if (ret == BNXT_TF_RC_ERROR)
264 else if (ret == BNXT_TF_RC_FID)
267 ret = ulp_matcher_pattern_match(¶ms, &class_id);
269 if (ret != BNXT_TF_RC_SUCCESS)
272 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
273 if (ret != BNXT_TF_RC_SUCCESS)
276 /* all good return success */
280 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
281 "Failed to validate flow.");
285 /* Function to destroy the rte flow. */
287 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
288 struct rte_flow *flow,
289 struct rte_flow_error *error)
291 struct bnxt_ulp_context *ulp_ctx;
296 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
298 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
300 rte_flow_error_set(error, EINVAL,
301 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
302 "Failed to destroy flow.");
306 flow_id = (uint32_t)(uintptr_t)flow;
308 if (ulp_port_db_port_func_id_get(ulp_ctx,
311 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
313 rte_flow_error_set(error, EINVAL,
314 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
315 "Failed to destroy flow.");
319 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
321 BNXT_TF_DBG(ERR, "Incorrect device params\n");
323 rte_flow_error_set(error, EINVAL,
324 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
325 "Failed to destroy flow.");
329 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
330 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
333 ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
336 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
338 rte_flow_error_set(error, -ret,
339 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
340 "Failed to destroy flow.");
342 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
347 /* Function to destroy the rte flows. */
349 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
350 struct rte_flow_error *error)
352 struct bnxt_ulp_context *ulp_ctx;
356 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
361 /* Free the resources for the last device */
362 if (ulp_ctx_deinit_allowed(ulp_ctx)) {
363 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
364 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
365 ret = ulp_port_db_port_func_id_get(ulp_ctx,
366 eth_dev->data->port_id,
369 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
371 BNXT_TF_DBG(ERR, "convert port to func id failed\n");
374 rte_flow_error_set(error, ret,
375 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
376 "Failed to flush flow.");
380 /* Function to query the rte flows. */
382 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
383 struct rte_flow *flow,
384 const struct rte_flow_action *action,
386 struct rte_flow_error *error)
389 struct bnxt_ulp_context *ulp_ctx;
390 struct rte_flow_query_count *count;
393 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
395 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
396 rte_flow_error_set(error, EINVAL,
397 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
398 "Failed to query flow.");
402 flow_id = (uint32_t)(uintptr_t)flow;
404 switch (action->type) {
405 case RTE_FLOW_ACTION_TYPE_COUNT:
407 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
409 rte_flow_error_set(error, EINVAL,
410 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
411 "Failed to query flow.");
415 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
416 NULL, "Unsupported action item");
422 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
423 .validate = bnxt_ulp_flow_validate,
424 .create = bnxt_ulp_flow_create,
425 .destroy = bnxt_ulp_flow_destroy,
426 .flush = bnxt_ulp_flow_flush,
427 .query = bnxt_ulp_flow_query,