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;
63 /* Function to create the rte flow. */
64 static struct rte_flow *
65 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
66 const struct rte_flow_attr *attr,
67 const struct rte_flow_item pattern[],
68 const struct rte_flow_action actions[],
69 struct rte_flow_error *error)
71 struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
72 struct ulp_rte_parser_params params;
73 struct bnxt_ulp_context *ulp_ctx;
74 uint32_t class_id, act_tmpl;
75 struct rte_flow *flow_id;
79 if (bnxt_ulp_flow_validate_args(attr,
81 error) == BNXT_TF_RC_ERROR) {
82 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
86 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
88 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
92 /* Initialize the parser params */
93 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
94 params.ulp_ctx = ulp_ctx;
97 params.dir = ULP_DIR_EGRESS;
99 /* copy the device port id and direction for further processing */
100 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_INCOMING_IF,
102 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_DIRECTION, params.dir);
103 ULP_COMP_FLD_IDX_WR(¶ms, BNXT_ULP_CF_IDX_SVIF_FLAG,
104 BNXT_ULP_INVALID_SVIF_VAL);
106 /* Parse the rte flow pattern */
107 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
108 if (ret != BNXT_TF_RC_SUCCESS)
111 /* Parse the rte flow action */
112 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
113 if (ret != BNXT_TF_RC_SUCCESS)
116 ret = ulp_matcher_pattern_match(¶ms, &class_id);
117 if (ret != BNXT_TF_RC_SUCCESS)
120 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
121 if (ret != BNXT_TF_RC_SUCCESS)
124 mapper_cparms.app_priority = attr->priority;
125 mapper_cparms.hdr_bitmap = ¶ms.hdr_bitmap;
126 mapper_cparms.hdr_field = params.hdr_field;
127 mapper_cparms.comp_fld = params.comp_fld;
128 mapper_cparms.act = ¶ms.act_bitmap;
129 mapper_cparms.act_prop = ¶ms.act_prop;
130 mapper_cparms.class_tid = class_id;
131 mapper_cparms.act_tid = act_tmpl;
132 mapper_cparms.func_id = bnxt_get_fw_func_id(dev->data->port_id,
133 BNXT_ULP_INTF_TYPE_INVALID);
134 mapper_cparms.dir = params.dir;
136 /* Call the ulp mapper to create the flow in the hardware. */
137 ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms, &fid);
139 flow_id = (struct rte_flow *)((uintptr_t)fid);
144 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
145 "Failed to create flow.");
149 /* Function to validate the rte flow. */
151 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
152 const struct rte_flow_attr *attr,
153 const struct rte_flow_item pattern[],
154 const struct rte_flow_action actions[],
155 struct rte_flow_error *error)
157 struct ulp_rte_parser_params params;
158 uint32_t class_id, act_tmpl;
160 struct bnxt_ulp_context *ulp_ctx;
162 if (bnxt_ulp_flow_validate_args(attr,
164 error) == BNXT_TF_RC_ERROR) {
165 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
169 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
171 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
175 /* Initialize the parser params */
176 memset(¶ms, 0, sizeof(struct ulp_rte_parser_params));
177 params.ulp_ctx = ulp_ctx;
180 params.dir = ULP_DIR_EGRESS;
182 /* Parse the rte flow pattern */
183 ret = bnxt_ulp_rte_parser_hdr_parse(pattern, ¶ms);
184 if (ret != BNXT_TF_RC_SUCCESS)
187 /* Parse the rte flow action */
188 ret = bnxt_ulp_rte_parser_act_parse(actions, ¶ms);
189 if (ret != BNXT_TF_RC_SUCCESS)
192 ret = ulp_matcher_pattern_match(¶ms, &class_id);
194 if (ret != BNXT_TF_RC_SUCCESS)
197 ret = ulp_matcher_action_match(¶ms, &act_tmpl);
198 if (ret != BNXT_TF_RC_SUCCESS)
201 /* all good return success */
205 rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
206 "Failed to validate flow.");
210 /* Function to destroy the rte flow. */
212 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
213 struct rte_flow *flow,
214 struct rte_flow_error *error)
217 struct bnxt_ulp_context *ulp_ctx;
221 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
223 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
225 rte_flow_error_set(error, EINVAL,
226 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
227 "Failed to destroy flow.");
231 flow_id = (uint32_t)(uintptr_t)flow;
232 func_id = bnxt_get_fw_func_id(dev->data->port_id,
233 BNXT_ULP_INTF_TYPE_INVALID);
235 if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
237 BNXT_TF_DBG(ERR, "Incorrect device params\n");
239 rte_flow_error_set(error, EINVAL,
240 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
241 "Failed to destroy flow.");
245 ret = ulp_mapper_flow_destroy(ulp_ctx, flow_id,
246 BNXT_ULP_REGULAR_FLOW_TABLE);
248 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
250 rte_flow_error_set(error, -ret,
251 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
252 "Failed to destroy flow.");
258 /* Function to destroy the rte flows. */
260 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
261 struct rte_flow_error *error)
263 struct bnxt_ulp_context *ulp_ctx;
268 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
270 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
271 rte_flow_error_set(error, EINVAL,
272 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
273 "Failed to flush flow.");
276 bp = eth_dev->data->dev_private;
278 /* Free the resources for the last device */
279 if (ulp_ctx_deinit_allowed(bp)) {
280 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
281 } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
282 func_id = bnxt_get_fw_func_id(eth_dev->data->port_id,
283 BNXT_ULP_INTF_TYPE_INVALID);
284 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
287 rte_flow_error_set(error, ret,
288 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
289 "Failed to flush flow.");
293 /* Function to query the rte flows. */
295 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
296 struct rte_flow *flow,
297 const struct rte_flow_action *action,
299 struct rte_flow_error *error)
302 struct bnxt_ulp_context *ulp_ctx;
303 struct rte_flow_query_count *count;
306 ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
308 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
309 rte_flow_error_set(error, EINVAL,
310 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
311 "Failed to query flow.");
315 flow_id = (uint32_t)(uintptr_t)flow;
317 switch (action->type) {
318 case RTE_FLOW_ACTION_TYPE_COUNT:
320 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
322 rte_flow_error_set(error, EINVAL,
323 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
324 "Failed to query flow.");
328 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
329 NULL, "Unsupported action item");
335 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
336 .validate = bnxt_ulp_flow_validate,
337 .create = bnxt_ulp_flow_create,
338 .destroy = bnxt_ulp_flow_destroy,
339 .flush = bnxt_ulp_flow_flush,
340 .query = bnxt_ulp_flow_query,