xref: /dpdk/examples/flow_filtering/flow_skeleton.c (revision 16158f34900075f2f30b879bf3708e54e07455f4)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2022 NVIDIA Corporation & Affiliates
3  */
4 
5 #include <stdint.h>
6 
7 #include <rte_errno.h>
8 #include <rte_flow.h>
9 
10 #include "common.h"
11 #include "snippets/snippet_match_ipv4.h"
12 
13 
14 struct rte_flow_attr attr = { .ingress = 1 };
15 struct rte_flow_op_attr ops_attr = { .postpone = 0 };
16 
17 static struct rte_flow *
18 create_flow_non_template(uint16_t port_id, struct rte_flow_attr *attr,
19 						struct rte_flow_item *patterns,
20 						struct rte_flow_action *actions,
21 						struct rte_flow_error *error)
22 {
23 	struct rte_flow *flow = NULL;
24 
25 	/* Validate the rule and create it. */
26 	if (rte_flow_validate(port_id, attr, patterns, actions, error) == 0)
27 		flow = rte_flow_create(port_id, attr, patterns, actions, error);
28 	return flow;
29 }
30 
31 static struct rte_flow *
32 create_flow_template(uint16_t port_id, struct rte_flow_op_attr *ops_attr,
33 					struct rte_flow_item *patterns,
34 					struct rte_flow_action *actions,
35 					struct rte_flow_error *error)
36 {
37 	/* Replace this function call with
38 	 * snippet_*_create_table() function from the snippets directory.
39 	 */
40 	struct rte_flow_template_table *table = snippet_ipv4_flow_create_table(port_id, error);
41 	if (table == NULL) {
42 		printf("Failed to create table: %s (%s)\n",
43 		error->message, rte_strerror(rte_errno));
44 		return NULL;
45 	}
46 
47 	return rte_flow_async_create(port_id,
48 		QUEUE_ID, /* Flow queue used to insert the rule. */
49 		ops_attr,
50 		table,
51 		patterns,
52 		0, /* Pattern template index in the table. */
53 		actions,
54 		0, /* Actions template index in the table. */
55 		0, /* user data */
56 		error);
57 }
58 
59 struct rte_flow *
60 generate_flow_skeleton(uint16_t port_id, struct rte_flow_error *error, int use_template_api)
61 {
62 	/* Set the common action and pattern structures 8< */
63 	struct rte_flow_action actions[MAX_ACTION_NUM] = {0};
64 	struct rte_flow_item patterns[MAX_PATTERN_NUM] = {0};
65 
66 	/* Replace this function call with
67 	 * snippet_*_create_actions() function from the snippets directory
68 	 */
69 	snippet_ipv4_flow_create_actions(actions);
70 
71 	/* Replace this function call with
72 	 * snippet_*_create_patterns() function from the snippets directory
73 	 */
74 	snippet_ipv4_flow_create_patterns(patterns);
75 	/* >8 End of setting the common action and pattern structures. */
76 
77 	/* Create a flow rule using template API 8< */
78 	if (use_template_api)
79 		return create_flow_template(port_id, &ops_attr, patterns, actions, error);
80 	/* >8 End of creating a flow rule using template API. */
81 
82 	/* Validate and create the rule 8< */
83 	return create_flow_non_template(port_id, &attr, patterns, actions, error);
84 	/* >8 End of validating and creating the rule. */
85 }
86