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 <rte_malloc.h>
16 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
17 const struct rte_flow_item pattern[],
18 const struct rte_flow_action actions[],
19 struct rte_flow_error *error)
21 /* Perform the validation of the arguments for null */
23 return BNXT_TF_RC_ERROR;
26 rte_flow_error_set(error,
28 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
31 return BNXT_TF_RC_ERROR;
35 rte_flow_error_set(error,
37 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
40 return BNXT_TF_RC_ERROR;
44 rte_flow_error_set(error,
46 RTE_FLOW_ERROR_TYPE_ATTR,
49 return BNXT_TF_RC_ERROR;
52 if (attr->egress && attr->ingress) {
53 rte_flow_error_set(error,
55 RTE_FLOW_ERROR_TYPE_ATTR,
57 "EGRESS AND INGRESS UNSUPPORTED");
58 return BNXT_TF_RC_ERROR;
60 return BNXT_TF_RC_SUCCESS;
64 bnxt_ulp_set_dir_attributes(struct ulp_rte_parser_params *params,
65 const struct rte_flow_attr *attr)
67 /* Set the flow attributes */
69 params->dir_attr |= BNXT_ULP_FLOW_ATTR_EGRESS;
71 params->dir_attr |= BNXT_ULP_FLOW_ATTR_INGRESS;
73 params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
76 /* Function to create the rte flow. */
77 static struct rte_flow *
78 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
79 const struct rte_flow_attr *attr,
80 const struct rte_flow_item pattern[],
81 const struct rte_flow_action actions[],
82 struct rte_flow_error *error)
84 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
85 struct ulp_rte_parser_params params;
86 struct bnxt_ulp_context *ulp_ctx;
87 uint32_t class_id, act_tmpl;
88 struct rte_flow *flow_id;
90 int ret = BNXT_TF_RC_ERROR;
92 if (bnxt_ulp_flow_validate_args(attr,
94 error) == BNXT_TF_RC_ERROR) {
95 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
99 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
101 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
105 /* Initialize the parser params */
106 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
107 params.ulp_ctx = ulp_ctx;
109 /* Set the flow attributes */
110 bnxt_ulp_set_dir_attributes(¶ms, attr);
112 /* copy the device port id and direction for further processing */
113 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
115 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
116 BNXT_ULP_INVALID_SVIF_VAL);
118 /* Parse the rte flow pattern */
119 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
120 if (ret != BNXT_TF_RC_SUCCESS)
123 /* Parse the rte flow action */
124 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
125 if (ret != BNXT_TF_RC_SUCCESS)
128 /* Perform the rte flow post process */
129 ret = bnxt_ulp_rte_parser_post_process(¶ms);
130 if (ret != BNXT_TF_RC_SUCCESS)
133 ret = ulp_matcher_pattern_match(¶ms, &class_id);
134 if (ret != BNXT_TF_RC_SUCCESS)
137 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
138 if (ret != BNXT_TF_RC_SUCCESS)
141 mapper_cparms.app_priority = attr->priority;
142 mapper_cparms.hdr_bitmap = ¶ms.hdr_bitmap;
143 mapper_cparms.hdr_field = params.hdr_field;
144 mapper_cparms.comp_fld = params.comp_fld;
145 mapper_cparms.act = ¶ms.act_bitmap;
146 mapper_cparms.act_prop = ¶ms.act_prop;
147 mapper_cparms.class_tid = class_id;
148 mapper_cparms.act_tid = act_tmpl;
149 mapper_cparms.func_id = bnxt_get_fw_func_id(dev->data->port_id,
150 BNXT_ULP_INTF_TYPE_INVALID);
151 mapper_cparms.dir_attr = params.dir_attr;
153 /* Call the ulp mapper to create the flow in the hardware. */
154 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms, &fid);
156 flow_id = (struct rte_flow *)((uintptr_t)fid);
161 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
162 "Failed to create flow.");
166 /* Function to validate the rte flow. */
168 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
169 const struct rte_flow_attr *attr,
170 const struct rte_flow_item pattern[],
171 const struct rte_flow_action actions[],
172 struct rte_flow_error *error)
174 struct ulp_rte_parser_params params;
175 uint32_t class_id, act_tmpl;
176 int ret = BNXT_TF_RC_ERROR;
177 struct bnxt_ulp_context *ulp_ctx;
179 if (bnxt_ulp_flow_validate_args(attr,
181 error) == BNXT_TF_RC_ERROR) {
182 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
186 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
188 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
192 /* Initialize the parser params */
193 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
194 params.ulp_ctx = ulp_ctx;
196 /* Set the flow attributes */
197 bnxt_ulp_set_dir_attributes(¶ms, attr);
199 /* Parse the rte flow pattern */
200 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
201 if (ret != BNXT_TF_RC_SUCCESS)
204 /* Parse the rte flow action */
205 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
206 if (ret != BNXT_TF_RC_SUCCESS)
209 /* Perform the rte flow post process */
210 ret = bnxt_ulp_rte_parser_post_process(¶ms);
211 if (ret != BNXT_TF_RC_SUCCESS)
214 ret = ulp_matcher_pattern_match(¶ms, &class_id);
216 if (ret != BNXT_TF_RC_SUCCESS)
219 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
220 if (ret != BNXT_TF_RC_SUCCESS)
223 /* all good return success */
227 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
228 "Failed to validate flow.");
232 /* Function to destroy the rte flow. */
234 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
235 struct rte_flow *flow,
236 struct rte_flow_error *error)
239 struct bnxt_ulp_context *ulp_ctx;
243 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
245 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
247 rte_flow_error_set(error, EINVAL,
248 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
249 "Failed to destroy flow.");
253 flow_id = (uint32_t)(uintptr_t)flow;
254 func_id = bnxt_get_fw_func_id(dev->data->port_id,
255 BNXT_ULP_INTF_TYPE_INVALID);
257 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
259 BNXT_TF_DBG(ERR, "Incorrect device params\n");
261 rte_flow_error_set(error, EINVAL,
262 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
263 "Failed to destroy flow.");
267 ret = ulp_mapper_flow_destroy(ulp_ctx, flow_id,
268 BNXT_ULP_REGULAR_FLOW_TABLE);
270 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
272 rte_flow_error_set(error, -ret,
273 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
274 "Failed to destroy flow.");
280 /* Function to destroy the rte flows. */
282 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
283 struct rte_flow_error *error)
285 struct bnxt_ulp_context *ulp_ctx;
290 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
292 BNXT_TF_DBG(DEBUG, "ULP context is not initialized\n");
295 bp = eth_dev->data->dev_private;
297 /* Free the resources for the last device */
298 if (ulp_ctx_deinit_allowed(bp)) {
299 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
300 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
301 func_id = bnxt_get_fw_func_id(eth_dev->data->port_id,
302 BNXT_ULP_INTF_TYPE_INVALID);
303 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
306 rte_flow_error_set(error, ret,
307 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
308 "Failed to flush flow.");
312 /* Function to query the rte flows. */
314 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
315 struct rte_flow *flow,
316 const struct rte_flow_action *action,
318 struct rte_flow_error *error)
321 struct bnxt_ulp_context *ulp_ctx;
322 struct rte_flow_query_count *count;
325 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
327 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
328 rte_flow_error_set(error, EINVAL,
329 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
330 "Failed to query flow.");
334 flow_id = (uint32_t)(uintptr_t)flow;
336 switch (action->type) {
337 case RTE_FLOW_ACTION_TYPE_COUNT:
339 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
341 rte_flow_error_set(error, EINVAL,
342 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
343 "Failed to query flow.");
347 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
348 NULL, "Unsupported action item");
354 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
355 .validate = bnxt_ulp_flow_validate,
356 .create = bnxt_ulp_flow_create,
357 .destroy = bnxt_ulp_flow_destroy,
358 .flush = bnxt_ulp_flow_flush,
359 .query = bnxt_ulp_flow_query,