Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026 Intel Corporation
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <stdbool.h>
7 : : #include <stddef.h>
8 : : #include <stdint.h>
9 : :
10 : : #include <eal_export.h>
11 : : #include <rte_common.h>
12 : : #include <rte_errno.h>
13 : :
14 : : #include "rte_ethdev.h"
15 : : #include "rte_flow.h"
16 : : #include "rte_flow_driver.h"
17 : : #include "flow_graph.h"
18 : :
19 : : #define FLOW_GRAPH_LOG(level, fmt, ...) \
20 : : RTE_ETHDEV_LOG_LINE(level, "FLOW GRAPH: %s(): " fmt, __func__, ##__VA_ARGS__)
21 : :
22 : : static const char *
23 : : flow_item_type_to_str(enum rte_flow_item_type type)
24 : : {
25 : : const char *name;
26 : : int ret;
27 : :
28 : 0 : ret = rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR,
29 : 0 : &name, sizeof(name), (const void *)(uintptr_t)type, NULL);
30 [ # # # # : 0 : if (ret < 0)
# # # # #
# ]
31 : : return "UNKNOWN";
32 : :
33 : 0 : return name;
34 : : }
35 : :
36 : : static bool
37 : 0 : flow_graph_node_is_expected(const struct flow_graph_node *node,
38 : : const struct rte_flow_item *item, struct rte_flow_error *error)
39 : : {
40 : 0 : flow_graph_node_expect_t c = node->constraints;
41 : 0 : bool has_spec = (item->spec != NULL);
42 : 0 : bool has_mask = (item->mask != NULL);
43 : 0 : bool has_last = (item->last != NULL);
44 : : const char *msg;
45 : :
46 : : /* mask or last without spec is invalid per rte_flow API */
47 [ # # # # ]: 0 : if (!has_spec && (has_mask || has_last)) {
48 [ # # ]: 0 : msg = has_mask ? "Unexpected mask without spec in flow item" :
49 : : "Unexpected last without spec in flow item";
50 : 0 : goto fail;
51 : : }
52 : :
53 [ # # ]: 0 : if (c == FLOW_GRAPH_NODE_EXPECT_NONE)
54 : : return true;
55 [ # # # # ]: 0 : if ((c & FLOW_GRAPH_NODE_EXPECT_EMPTY) && !has_spec)
56 : : return true;
57 [ # # # # ]: 0 : if ((c & FLOW_GRAPH_NODE_EXPECT_SPEC) && has_spec && !has_last)
58 : : return true;
59 [ # # # # ]: 0 : if ((c & FLOW_GRAPH_NODE_EXPECT_RANGE) && has_spec && has_last)
60 : : return true;
61 : :
62 [ # # ]: 0 : if (!has_spec)
63 : : msg = "Missing spec in flow item";
64 [ # # ]: 0 : else if (has_last)
65 : : msg = "Unexpected last in flow item";
66 [ # # ]: 0 : else if (c & FLOW_GRAPH_NODE_EXPECT_RANGE)
67 : : msg = "Missing last in flow item";
68 : : else
69 : : msg = "Unexpected spec in flow item";
70 : 0 : fail:
71 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item, msg);
72 : :
73 : 0 : return false;
74 : : }
75 : :
76 : : static bool
77 : : flow_graph_item_is_ignored(const struct flow_graph *graph,
78 : : enum rte_flow_item_type fi_type)
79 : : {
80 : : const enum rte_flow_item_type *ignored;
81 : :
82 : 0 : if (fi_type == RTE_FLOW_ITEM_TYPE_VOID)
83 : : return true;
84 : :
85 [ # # ]: 0 : if (graph->ignore_nodes == NULL)
86 : : return false;
87 : :
88 [ # # ]: 0 : for (ignored = graph->ignore_nodes; *ignored != RTE_FLOW_ITEM_TYPE_END; ignored++) {
89 [ # # ]: 0 : if (*ignored == fi_type)
90 : : return true;
91 : : }
92 : :
93 : : return false;
94 : : }
95 : :
96 : : static size_t
97 : : flow_graph_get_node_idx(const struct flow_graph *graph,
98 : : const struct flow_graph_node *node)
99 : : {
100 : 0 : return (size_t)(node - graph->nodes);
101 : : }
102 : :
103 : : static bool
104 : 0 : flow_graph_node_is_valid(const struct flow_graph *graph,
105 : : const struct flow_graph_node *node,
106 : : struct rte_flow_error *error)
107 : : {
108 : : size_t node_idx;
109 : :
110 [ # # ]: 0 : if (node == NULL) {
111 : 0 : rte_flow_error_set(error, EINVAL,
112 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
113 : : "Flow graph node pointer is NULL");
114 : 0 : return false;
115 : : }
116 : :
117 [ # # ]: 0 : if (node->name == NULL) {
118 : 0 : rte_flow_error_set(error, EINVAL,
119 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, node,
120 : : "Flow graph node name is not defined");
121 : 0 : return false;
122 : : }
123 : :
124 : : node_idx = flow_graph_get_node_idx(graph, node);
125 : :
126 : : /* first node can't have callbacks because there's no flow item */
127 [ # # ]: 0 : if (node_idx == FLOW_GRAPH_NODE_FIRST &&
128 [ # # # # ]: 0 : (node->validate != NULL || node->process != NULL)) {
129 : 0 : rte_flow_error_set(error, EINVAL,
130 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, node,
131 : : "Flow graph start node callbacks are not allowed");
132 : 0 : return false;
133 : : }
134 : :
135 : : /* all non-END nodes must have edges */
136 [ # # # # ]: 0 : if ((node->type != RTE_FLOW_ITEM_TYPE_END || node_idx == FLOW_GRAPH_NODE_FIRST) &&
137 [ # # ]: 0 : graph->edges[node_idx].next == NULL) {
138 : 0 : rte_flow_error_set(error, EINVAL,
139 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, node,
140 : : "Flow graph edge list is not defined for non-END node");
141 : 0 : return false;
142 : : }
143 : :
144 : : return true;
145 : : }
146 : :
147 : : static const struct flow_graph_node *
148 : 0 : flow_graph_find_next_node(const struct flow_graph *graph,
149 : : const struct flow_graph_node *cur_node,
150 : : const struct rte_flow_item *item,
151 : : struct rte_flow_error *error)
152 : : {
153 : 0 : enum rte_flow_item_type next_type = item->type;
154 : : const size_t *next_nodes;
155 : : size_t cur_idx, edge_idx;
156 : :
157 : : cur_idx = flow_graph_get_node_idx(graph, cur_node);
158 : 0 : next_nodes = graph->edges[cur_idx].next;
159 : :
160 [ # # ]: 0 : for (edge_idx = 0; next_nodes[edge_idx] != FLOW_GRAPH_NODE_EDGE_END; edge_idx++) {
161 : 0 : const struct flow_graph_node *tmp =
162 : 0 : &graph->nodes[next_nodes[edge_idx]];
163 [ # # ]: 0 : if (!flow_graph_node_is_valid(graph, tmp, error))
164 : : return NULL;
165 [ # # ]: 0 : if (tmp->type == next_type)
166 : 0 : return tmp;
167 : : }
168 : :
169 : 0 : rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
170 : : item, "Pattern item not supported");
171 : :
172 : 0 : return NULL;
173 : : }
174 : :
175 : : static int
176 : 0 : flow_graph_visit_node(const struct flow_graph_node *node, void *ctx,
177 : : const struct rte_flow_item *item,
178 : : struct rte_flow_error *error)
179 : : {
180 : : int ret;
181 : :
182 [ # # ]: 0 : if (!flow_graph_node_is_expected(node, item, error))
183 : 0 : return -rte_errno;
184 : :
185 [ # # ]: 0 : if (node->validate != NULL) {
186 : 0 : FLOW_GRAPH_LOG(DEBUG, "validating node: %s", node->name);
187 : 0 : ret = node->validate(ctx, item, error);
188 [ # # ]: 0 : if (ret != 0)
189 : : return ret;
190 : : }
191 : :
192 [ # # ]: 0 : if (node->process != NULL) {
193 : 0 : FLOW_GRAPH_LOG(DEBUG, "processing node: %s", node->name);
194 : 0 : ret = node->process(ctx, item, error);
195 [ # # ]: 0 : if (ret != 0)
196 : 0 : return ret;
197 : : }
198 : :
199 : : return 0;
200 : : }
201 : :
202 : : RTE_EXPORT_INTERNAL_SYMBOL(flow_graph_parse)
203 : : int
204 : 0 : flow_graph_parse(const struct flow_graph *graph,
205 : : const struct rte_flow_item *pattern,
206 : : struct rte_flow_error *error,
207 : : void *ctx)
208 : : {
209 : : const struct flow_graph_node *cur_node;
210 : : const struct rte_flow_item *item;
211 : 0 : struct rte_flow_error local_error = {0};
212 : : int ret;
213 : :
214 : : /* error may be NULL, but we want the message for logging */
215 [ # # ]: 0 : if (error == NULL)
216 : : error = &local_error;
217 : :
218 [ # # # # : 0 : if (graph == NULL || graph->nodes == NULL || graph->edges == NULL) {
# # ]
219 : 0 : FLOW_GRAPH_LOG(DEBUG, "flow graph is not defined");
220 : 0 : return rte_flow_error_set(error, ENOTSUP,
221 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
222 : : "Flow graph is not defined");
223 : : }
224 [ # # ]: 0 : if (pattern == NULL) {
225 : 0 : FLOW_GRAPH_LOG(DEBUG, "flow pattern is NULL");
226 : 0 : return rte_flow_error_set(error, EINVAL,
227 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
228 : : "Flow pattern is NULL");
229 : : }
230 : :
231 : : /* use start node as traversal anchor */
232 : : cur_node = &graph->nodes[FLOW_GRAPH_NODE_FIRST];
233 : :
234 [ # # ]: 0 : if (!flow_graph_node_is_valid(graph, cur_node, error)) {
235 : 0 : FLOW_GRAPH_LOG(DEBUG, "invalid start node: %s", error->message);
236 : 0 : return -rte_errno;
237 : : }
238 : :
239 : : item = pattern;
240 : : do {
241 : : const struct flow_graph_node *next_node;
242 : :
243 : 0 : FLOW_GRAPH_LOG(DEBUG, "current node: %s (%s)",
244 : : cur_node->name, flow_item_type_to_str(cur_node->type));
245 : :
246 [ # # # # ]: 0 : if (flow_graph_item_is_ignored(graph, item->type)) {
247 : 0 : FLOW_GRAPH_LOG(DEBUG, "ignored pattern item: %s",
248 : : flow_item_type_to_str(item->type));
249 : 0 : continue;
250 : : }
251 : 0 : FLOW_GRAPH_LOG(DEBUG, "next pattern item: %s",
252 : : flow_item_type_to_str(item->type));
253 : :
254 : 0 : next_node = flow_graph_find_next_node(graph, cur_node, item, error);
255 [ # # ]: 0 : if (next_node == NULL) {
256 : 0 : ret = -rte_errno;
257 : 0 : goto fail;
258 : : }
259 : 0 : FLOW_GRAPH_LOG(DEBUG, "found next node: %s (%s)", next_node->name,
260 : : flow_item_type_to_str(next_node->type));
261 : :
262 : 0 : ret = flow_graph_visit_node(next_node, ctx, item, error);
263 [ # # ]: 0 : if (ret != 0)
264 : 0 : goto fail;
265 : 0 : FLOW_GRAPH_LOG(DEBUG, "visited node: %s", next_node->name);
266 : :
267 : : cur_node = next_node;
268 [ # # ]: 0 : } while ((item++)->type != RTE_FLOW_ITEM_TYPE_END);
269 : :
270 : : return 0;
271 : 0 : fail:
272 : 0 : FLOW_GRAPH_LOG(DEBUG, "failed to transition from node '%s' to flow item '%s': %s",
273 : : cur_node->name, flow_item_type_to_str(item->type), error->message);
274 : 0 : return ret;
275 : : }
|