xref: /dpdk/drivers/net/bnxt/tf_ulp/bnxt_ulp_flow.c (revision a2417601)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5 
6 #include "bnxt.h"
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>
15 
16 static int32_t
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)
21 {
22 	/* Perform the validation of the arguments for null */
23 	if (!error)
24 		return BNXT_TF_RC_ERROR;
25 
26 	if (!pattern) {
27 		rte_flow_error_set(error,
28 				   EINVAL,
29 				   RTE_FLOW_ERROR_TYPE_ITEM_NUM,
30 				   NULL,
31 				   "NULL pattern.");
32 		return BNXT_TF_RC_ERROR;
33 	}
34 
35 	if (!actions) {
36 		rte_flow_error_set(error,
37 				   EINVAL,
38 				   RTE_FLOW_ERROR_TYPE_ACTION_NUM,
39 				   NULL,
40 				   "NULL action.");
41 		return BNXT_TF_RC_ERROR;
42 	}
43 
44 	if (!attr) {
45 		rte_flow_error_set(error,
46 				   EINVAL,
47 				   RTE_FLOW_ERROR_TYPE_ATTR,
48 				   NULL,
49 				   "NULL attribute.");
50 		return BNXT_TF_RC_ERROR;
51 	}
52 
53 	if (attr->egress && attr->ingress) {
54 		rte_flow_error_set(error,
55 				   EINVAL,
56 				   RTE_FLOW_ERROR_TYPE_ATTR,
57 				   attr,
58 				   "EGRESS AND INGRESS UNSUPPORTED");
59 		return BNXT_TF_RC_ERROR;
60 	}
61 	return BNXT_TF_RC_SUCCESS;
62 }
63 
64 static inline void
65 bnxt_ulp_set_dir_attributes(struct ulp_rte_parser_params *params,
66 			    const struct rte_flow_attr *attr)
67 {
68 	/* Set the flow attributes */
69 	if (attr->egress)
70 		params->dir_attr |= BNXT_ULP_FLOW_ATTR_EGRESS;
71 	if (attr->ingress)
72 		params->dir_attr |= BNXT_ULP_FLOW_ATTR_INGRESS;
73 	if (attr->transfer)
74 		params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
75 }
76 
77 void
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)
81 {
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	= &params->hdr_bitmap;
89 	mapper_cparms->hdr_field	= params->hdr_field;
90 	mapper_cparms->comp_fld		= params->comp_fld;
91 	mapper_cparms->act		= &params->act_bitmap;
92 	mapper_cparms->act_prop		= &params->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;
96 	mapper_cparms->fld_bitmap	= &params->fld_bitmap;
97 
98 	/* update the signature fields into the computed field list */
99 	ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_HDR_SIG_ID,
100 			    params->hdr_sig_id);
101 	ULP_COMP_FLD_IDX_WR(params, BNXT_ULP_CF_IDX_FLOW_SIG_ID,
102 			    params->flow_sig_id);
103 }
104 
105 /* Function to create the rte flow. */
106 static struct rte_flow *
107 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
108 		     const struct rte_flow_attr *attr,
109 		     const struct rte_flow_item pattern[],
110 		     const struct rte_flow_action actions[],
111 		     struct rte_flow_error *error)
112 {
113 	struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
114 	struct ulp_rte_parser_params params;
115 	struct bnxt_ulp_context *ulp_ctx;
116 	int rc, ret = BNXT_TF_RC_ERROR;
117 	struct rte_flow *flow_id;
118 	uint16_t func_id;
119 	uint32_t fid;
120 
121 	if (bnxt_ulp_flow_validate_args(attr,
122 					pattern, actions,
123 					error) == BNXT_TF_RC_ERROR) {
124 		BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
125 		goto flow_error;
126 	}
127 
128 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
129 	if (!ulp_ctx) {
130 		BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
131 		goto flow_error;
132 	}
133 
134 	/* Initialize the parser params */
135 	memset(&params, 0, sizeof(struct ulp_rte_parser_params));
136 	params.ulp_ctx = ulp_ctx;
137 
138 	/* Set the flow attributes */
139 	bnxt_ulp_set_dir_attributes(&params, attr);
140 
141 	/* copy the device port id and direction for further processing */
142 	ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_INCOMING_IF,
143 			    dev->data->port_id);
144 	ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_SVIF_FLAG,
145 			    BNXT_ULP_INVALID_SVIF_VAL);
146 
147 	/* Get the function id */
148 	if (ulp_port_db_port_func_id_get(ulp_ctx,
149 					 dev->data->port_id,
150 					 &func_id)) {
151 		BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
152 		goto flow_error;
153 	}
154 
155 	/* Protect flow creation */
156 	if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
157 		BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
158 		goto flow_error;
159 	}
160 
161 	/* Allocate a Flow ID for attaching all resources for the flow to.
162 	 * Once allocated, all errors have to walk the list of resources and
163 	 * free each of them.
164 	 */
165 	rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
166 				   func_id, &fid);
167 	if (rc) {
168 		BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
169 		goto release_lock;
170 	}
171 
172 	/* Parse the rte flow pattern */
173 	ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
174 	if (ret != BNXT_TF_RC_SUCCESS)
175 		goto free_fid;
176 
177 	/* Parse the rte flow action */
178 	ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
179 	if (ret != BNXT_TF_RC_SUCCESS)
180 		goto free_fid;
181 
182 	params.fid = fid;
183 	params.func_id = func_id;
184 	params.priority = attr->priority;
185 	/* Perform the rte flow post process */
186 	ret = bnxt_ulp_rte_parser_post_process(&params);
187 	if (ret == BNXT_TF_RC_ERROR)
188 		goto free_fid;
189 	else if (ret == BNXT_TF_RC_FID)
190 		goto return_fid;
191 
192 	ret = ulp_matcher_pattern_match(&params, &params.class_id);
193 	if (ret != BNXT_TF_RC_SUCCESS)
194 		goto free_fid;
195 
196 	ret = ulp_matcher_action_match(&params, &params.act_tmpl);
197 	if (ret != BNXT_TF_RC_SUCCESS)
198 		goto free_fid;
199 
200 	bnxt_ulp_init_mapper_params(&mapper_cparms, &params,
201 				    BNXT_ULP_FDB_TYPE_REGULAR);
202 	/* Call the ulp mapper to create the flow in the hardware. */
203 	ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
204 	if (ret)
205 		goto free_fid;
206 
207 return_fid:
208 	bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
209 
210 	flow_id = (struct rte_flow *)((uintptr_t)fid);
211 	return flow_id;
212 
213 free_fid:
214 	ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
215 release_lock:
216 	bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
217 flow_error:
218 	rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
219 			   "Failed to create flow.");
220 	return NULL;
221 }
222 
223 /* Function to validate the rte flow. */
224 static int
225 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
226 		       const struct rte_flow_attr *attr,
227 		       const struct rte_flow_item pattern[],
228 		       const struct rte_flow_action actions[],
229 		       struct rte_flow_error *error)
230 {
231 	struct ulp_rte_parser_params params;
232 	struct bnxt_ulp_context *ulp_ctx;
233 	uint32_t class_id, act_tmpl;
234 	int ret = BNXT_TF_RC_ERROR;
235 
236 	if (bnxt_ulp_flow_validate_args(attr,
237 					pattern, actions,
238 					error) == BNXT_TF_RC_ERROR) {
239 		BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
240 		goto parse_error;
241 	}
242 
243 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
244 	if (!ulp_ctx) {
245 		BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
246 		goto parse_error;
247 	}
248 
249 	/* Initialize the parser params */
250 	memset(&params, 0, sizeof(struct ulp_rte_parser_params));
251 	params.ulp_ctx = ulp_ctx;
252 
253 	/* Set the flow attributes */
254 	bnxt_ulp_set_dir_attributes(&params, attr);
255 
256 	/* Parse the rte flow pattern */
257 	ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
258 	if (ret != BNXT_TF_RC_SUCCESS)
259 		goto parse_error;
260 
261 	/* Parse the rte flow action */
262 	ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
263 	if (ret != BNXT_TF_RC_SUCCESS)
264 		goto parse_error;
265 
266 	/* Perform the rte flow post process */
267 	ret = bnxt_ulp_rte_parser_post_process(&params);
268 	if (ret == BNXT_TF_RC_ERROR)
269 		goto parse_error;
270 	else if (ret == BNXT_TF_RC_FID)
271 		return 0;
272 
273 	ret = ulp_matcher_pattern_match(&params, &class_id);
274 
275 	if (ret != BNXT_TF_RC_SUCCESS)
276 		goto parse_error;
277 
278 	ret = ulp_matcher_action_match(&params, &act_tmpl);
279 	if (ret != BNXT_TF_RC_SUCCESS)
280 		goto parse_error;
281 
282 	/* all good return success */
283 	return ret;
284 
285 parse_error:
286 	rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
287 			   "Failed to validate flow.");
288 	return -EINVAL;
289 }
290 
291 /* Function to destroy the rte flow. */
292 int
293 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
294 		      struct rte_flow *flow,
295 		      struct rte_flow_error *error)
296 {
297 	struct bnxt_ulp_context *ulp_ctx;
298 	uint32_t flow_id;
299 	uint16_t func_id;
300 	int ret;
301 
302 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
303 	if (!ulp_ctx) {
304 		BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
305 		if (error)
306 			rte_flow_error_set(error, EINVAL,
307 					   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
308 					   "Failed to destroy flow.");
309 		return -EINVAL;
310 	}
311 
312 	flow_id = (uint32_t)(uintptr_t)flow;
313 
314 	if (ulp_port_db_port_func_id_get(ulp_ctx,
315 					 dev->data->port_id,
316 					 &func_id)) {
317 		BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
318 		if (error)
319 			rte_flow_error_set(error, EINVAL,
320 					   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
321 					   "Failed to destroy flow.");
322 		return -EINVAL;
323 	}
324 
325 	if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
326 	    false) {
327 		BNXT_TF_DBG(ERR, "Incorrect device params\n");
328 		if (error)
329 			rte_flow_error_set(error, EINVAL,
330 					   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
331 					   "Failed to destroy flow.");
332 		return -EINVAL;
333 	}
334 
335 	if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
336 		BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
337 		return -EINVAL;
338 	}
339 	ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
340 				      flow_id);
341 	if (ret) {
342 		BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
343 		if (error)
344 			rte_flow_error_set(error, -ret,
345 					   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
346 					   "Failed to destroy flow.");
347 	}
348 	bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
349 
350 	return ret;
351 }
352 
353 /* Function to destroy the rte flows. */
354 static int32_t
355 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
356 		    struct rte_flow_error *error)
357 {
358 	struct bnxt_ulp_context *ulp_ctx;
359 	int32_t ret = 0;
360 	uint16_t func_id;
361 
362 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
363 	if (!ulp_ctx) {
364 		return ret;
365 	}
366 
367 	/* Free the resources for the last device */
368 	if (ulp_ctx_deinit_allowed(ulp_ctx)) {
369 		ret = ulp_flow_db_session_flow_flush(ulp_ctx);
370 	} else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
371 		ret = ulp_port_db_port_func_id_get(ulp_ctx,
372 						   eth_dev->data->port_id,
373 						   &func_id);
374 		if (!ret)
375 			ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
376 		else
377 			BNXT_TF_DBG(ERR, "convert port to func id failed\n");
378 	}
379 	if (ret)
380 		rte_flow_error_set(error, ret,
381 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
382 				   "Failed to flush flow.");
383 	return ret;
384 }
385 
386 /* Function to query the rte flows. */
387 static int32_t
388 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
389 		    struct rte_flow *flow,
390 		    const struct rte_flow_action *action,
391 		    void *data,
392 		    struct rte_flow_error *error)
393 {
394 	int rc = 0;
395 	struct bnxt_ulp_context *ulp_ctx;
396 	struct rte_flow_query_count *count;
397 	uint32_t flow_id;
398 
399 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
400 	if (!ulp_ctx) {
401 		BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
402 		rte_flow_error_set(error, EINVAL,
403 				   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
404 				   "Failed to query flow.");
405 		return -EINVAL;
406 	}
407 
408 	flow_id = (uint32_t)(uintptr_t)flow;
409 
410 	switch (action->type) {
411 	case RTE_FLOW_ACTION_TYPE_COUNT:
412 		count = data;
413 		rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
414 		if (rc) {
415 			rte_flow_error_set(error, EINVAL,
416 					   RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
417 					   "Failed to query flow.");
418 		}
419 		break;
420 	default:
421 		rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
422 				   NULL, "Unsupported action item");
423 	}
424 
425 	return rc;
426 }
427 
428 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
429 	.validate = bnxt_ulp_flow_validate,
430 	.create = bnxt_ulp_flow_create,
431 	.destroy = bnxt_ulp_flow_destroy,
432 	.flush = bnxt_ulp_flow_flush,
433 	.query = bnxt_ulp_flow_query,
434 	.isolate = NULL
435 };
436