Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2026 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _COMMON_INTEL_FLOW_ENGINE_H_
6 : : #define _COMMON_INTEL_FLOW_ENGINE_H_
7 : :
8 : : #include <stddef.h>
9 : : #include <stdio.h>
10 : : #include <stdlib.h>
11 : : #include <sys/queue.h>
12 : :
13 : : #include <rte_bitops.h>
14 : : #include <rte_errno.h>
15 : : #include <rte_malloc.h>
16 : :
17 : : #include <ethdev_driver.h>
18 : : #include <rte_flow.h>
19 : : #include <flow_graph.h>
20 : : #include <rte_tailq.h>
21 : : #include <rte_rwlock.h>
22 : : #include <rte_hexdump.h>
23 : :
24 : : #include "log.h"
25 : :
26 : : /*
27 : : * This is a common header for Intel Ethernet drivers' flow engine
28 : : * implementations. It defines the interfaces and data structures required to
29 : : * implement flow rule engines that plug into the drivers' flow handling logic.
30 : : *
31 : : * Design considerations:
32 : : *
33 : : * 1. Ease of implementation
34 : : *
35 : : * The flow engine interface is kept simple, with obvious defaults (not
36 : : * specifying something leads to the most expected behavior in context). The
37 : : * point is not to produce a monstrous driver-within-a-driver framework, but to
38 : : * make engine definitions follow semantic expectations of what the engine does.
39 : : *
40 : : * All boilerplate (flow management, engine enablement tracking, etc.) is
41 : : * handled by the common flow infrastructure, so an engine only needs to parse
42 : : * and install/uninstall flow rules. Drivers are expected to use other common
43 : : * flow utilities where applicable (e.g. flow_util.h, flow_check.h).
44 : : *
45 : : * 2. Full secondary process compatibility
46 : : *
47 : : * To support rte_flow operations in secondary processes, the engines enabled
48 : : * for a driver instance are tracked and resolved at runtime. The engine index
49 : : * (its position in the engine list) is used as a bit position in a
50 : : * driver-specific 64-bit field of enabled engines, so engine definitions can
51 : : * live in read-only memory shared by primary and secondary processes. For this
52 : : * to be safe, engine lists and definitions must be immutable for process
53 : : * lifetime (declare them as const).
54 : : *
55 : : * This does not mean every driver supports rte_flow in secondary processes -
56 : : * that is still up to each driver. It only ensures the framework itself does not
57 : : * prevent it.
58 : : *
59 : : * Engine callbacks must not access or retain a `struct rte_eth_dev *`, as it is
60 : : * process-local; use the process-independent `struct rte_eth_dev_data *`
61 : : * provided by the framework instead.
62 : : *
63 : : * The per-instance engine configuration is set up and torn down exclusively by
64 : : * `ci_flow_engine_conf_init()` and `ci_flow_engine_conf_reset()`, which should
65 : : * only be called by the primary process at device setup/teardown.
66 : : *
67 : : * 3. Flow object lifecycle is framework-owned
68 : : *
69 : : * Engines should treat framework-provided context and flow objects as storage
70 : : * they fill in, not storage they own: engine logic focuses on the contents of
71 : : * flow data, while object lifetime is managed by the framework. Engines may
72 : : * still allocate auxiliary data, but only where the framework guarantees a
73 : : * matching teardown call that lets the engine release it.
74 : : *
75 : : * 4. Pattern parsing: flow_graph and pattern_parse callback
76 : : *
77 : : * The flow engine framework works hand-in-hand with the `flow_graph` parsing
78 : : * infrastructure. Each engine may provide a pattern graph that is used to match
79 : : * the flow pattern and extract relevant data into the engine context provided
80 : : * by the framework.
81 : : *
82 : : * Engines may also provide a `pattern_parse` callback that is invoked before
83 : : * the graph parser runs. This allows engines to handle pattern items that don't
84 : : * fit neatly into the graph model (e.g. FUZZY items that can appear at any
85 : : * position), or to bypass the graph parser entirely with custom parsing.
86 : : *
87 : : * Pattern contents cannot be ignored. This is by design, as ignoring them is
88 : : * considered rte_flow API misuse. By default, even in the empty fallback case,
89 : : * a meaningful pattern (one that is not empty or ANY) is treated as an error.
90 : : *
91 : : * 5. Setup, teardown, and flow list lifecycle ordering
92 : : *
93 : : * The expected sequence of calls for a driver instance is:
94 : : *
95 : : * - `ci_flow_engine_conf_init()` should run from the driver's `dev_init` path.
96 : : *
97 : : * - At `dev_close`, `ci_flow_engine_conf_cleanup()` should run first to drop
98 : : * all flows and their internal tracking, followed by
99 : : * `ci_flow_engine_conf_reset()`.
100 : : *
101 : : * - Devices may or may not advertise `RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP`, i.e.
102 : : * support for keeping flow rules across a `dev_stop`/`dev_start` cycle.
103 : : *
104 : : * - If the device does not advertise this capability, flows must be flushed
105 : : * via `ci_flow_flush()` as the first step of `dev_stop()` (flushing later
106 : : * may interfere with flow uninstall).
107 : : *
108 : : * - If the flows are kept but device loses its hardware state across a
109 : : * `dev_stop`/`dev_start` cycle, the driver should call
110 : : * `ci_flow_engine_conf_replay()` as the last step of `dev_start`.
111 : : */
112 : :
113 : : struct ci_flow_engine_ops;
114 : : struct ci_flow_engine_ctx;
115 : : struct ci_flow_engine;
116 : : struct ci_flow_engine_ref;
117 : : struct ci_flow_engine_list;
118 : : struct ci_flow_engine_conf;
119 : : struct ci_flow;
120 : :
121 : : /*
122 : : * Flow engine ops.
123 : : *
124 : : * Each flow engine provides a set of operations that make up the flow engine
125 : : * pipeline. Each stage has a setup step (run when a flow is created) and,
126 : : * where applicable, a teardown step (run when a flow is removed). Operations
127 : : * are compositions of stages: setup steps run in forward order, teardown steps
128 : : * in reverse. Optional callbacks are shown in [brackets].
129 : : *
130 : : * - Allocation stage
131 : : * setup: [flow_alloc] teardown: [flow_free]
132 : : * Engine-specific flow object allocation. Skipped on the validate path.
133 : : *
134 : : * - Parse stage
135 : : * setup: ctx_init -> [pattern_parse] -> [graph parser] ->
136 : : * [ctx_finalize] -> [ctx_to_flow]
137 : : * teardown: (none)
138 : : * Interprets the request into the flow structure. Must NOT allocate.
139 : : *
140 : : * - Track stage
141 : : * setup: [flow_register] teardown: [flow_unregister]
142 : : * Updates driver-internal state. Permitted to allocate.
143 : : *
144 : : * - Apply stage
145 : : * setup: [flow_install] teardown: [flow_uninstall]
146 : : * Programs the hardware. Must NOT allocate.
147 : : *
148 : : * Typical operation sequences:
149 : : *
150 : : * - rte_flow_create: Allocation -> Parse -> Track -> Apply (setup)
151 : : * - rte_flow_validate: Parse
152 : : * - rte_flow_destroy: Apply -> Track -> Allocation (teardown)
153 : : * - rte_flow_flush: Apply -> Track -> Allocation (teardown, per flow)
154 : : * - ci_flow_engine_conf_replay: Apply (setup, per flow)
155 : : * - ci_flow_engine_conf_cleanup: Track -> Allocation (teardown, per flow)
156 : : *
157 : : * 1) Engine availability and lifecycle
158 : : *
159 : : * Availability is checked by the driver at init time. The criteria are up to
160 : : * the driver (hardware capability bits, PHY type, devargs, etc.). If
161 : : * `engine_init` is not implemented, the engine is always available.
162 : : *
163 : : * If `priv_size` is non-zero, the framework allocates a zeroed private block
164 : : * and passes it to engine_init/engine_uninit. Each allocated flow also carries
165 : : * a pointer to this per-device data in `flow->engine_priv`.
166 : : *
167 : : * 2) Input parsing
168 : : *
169 : : * `ctx_init` is mandatory and is the gateway for processing actions and
170 : : * attributes into engine context.
171 : : *
172 : : * Patterns are matched against the pattern graph in `ci_flow_engine`, the
173 : : * `pattern_parse` callback, or both:
174 : : *
175 : : * - graph only: graph parser runs
176 : : * - callback only: pattern_parse runs
177 : : * - callback + graph: pattern_parse runs, then graph parser runs
178 : : * - neither: empty fallback matcher is used
179 : : *
180 : : * The empty fallback matcher accepts only a NULL pattern, an empty pattern
181 : : * (start -> end), or an ANY-only pattern (start -> any -> end).
182 : : *
183 : : * In callback + graph mode, the callback does not consume items: the graph
184 : : * parser sees the same pattern, so the graph should list the relevant
185 : : * `ignore_nodes`.
186 : : *
187 : : * 3) Rule finalization
188 : : *
189 : : * `ctx_finalize` is an optional final consistency check between parsed
190 : : * action/attribute data and pattern-derived data, and a place for any other
191 : : * post-parse work.
192 : : *
193 : : * 4) Rule materialization
194 : : *
195 : : * `ctx_to_flow` translates the parsed context into the engine-specific flow
196 : : * rule. The rule should contain only what is needed for installation or
197 : : * querying; temporary data belongs in the build context.
198 : : *
199 : : * 5) Rule lifecycle hooks
200 : : *
201 : : * `flow_alloc`/`flow_free` are optional. If `flow_alloc` is absent or returns
202 : : * NULL, the framework falls back to rte_zmalloc. `flow_free` is required if
203 : : * `flow_alloc` is provided.
204 : : *
205 : : * `flow_register`/`flow_unregister` (Track) and `flow_install`/`flow_uninstall`
206 : : * (Apply) are optional. Register/unregister must be both defined or both NULL.
207 : : *
208 : : * Engines own only engine-specific fields. Common `ci_flow` fields (engine_idx,
209 : : * engine_priv, fallback_alloc, etc.) belong to the framework and must not be
210 : : * modified.
211 : : *
212 : : * IMPORTANT: by the time Track and Apply run, the engine has already accepted
213 : : * the flow. A failure in `flow_register` or `flow_install` (e.g. resource
214 : : * exhaustion) is a hard failure: no other engines are tried. Any check
215 : : * intrinsic to the pattern that would prevent tracking or installation must
216 : : * therefore be done at the parse stage.
217 : : *
218 : : * 6) Memory ownership and allocation policy
219 : : *
220 : : * An engine may allocate in exactly three places:
221 : : *
222 : : * - engine init: engine-wide resources used by other callbacks (e.g. a mempool
223 : : * from which `flow_alloc` takes flow objects)
224 : : * - flow alloc: memory for the flow itself (e.g. a mempool object), plus
225 : : * allocator-associated bookkeeping. It must not produce anything the parse
226 : : * stage depends on, because `rte_flow_validate()` bypasses `flow_alloc`.
227 : : * - flow register: the only per-flow callback that may allocate memory and
228 : : * store the pointer in the flow object or driver-internal structures. This is
229 : : * where "parsing the flow" ends and "updating driver state" begins.
230 : : *
231 : : * Parse callbacks (`ctx_init`, `pattern_parse`, `ctx_finalize`, `ctx_to_flow`)
232 : : * and `flow_install` must not allocate. These steps may fail with no
233 : : * teardown.
234 : : *
235 : : * 7) Flow query
236 : : *
237 : : * `flow_query` is optional. If not provided, querying the rule fails.
238 : : *
239 : : * 8) Concurrency model
240 : : *
241 : : * The framework serializes access to each driver instance's flow state with a
242 : : * single rwlock. Create, destroy and flush take it exclusively; validate and
243 : : * query take it shared. Parse callbacks (ctx_init, pattern_parse,
244 : : * ctx_finalize, ctx_to_flow) can therefore run concurrently on the validate
245 : : * path, as can flow_query, and must treat shared engine state (including
246 : : * engine_priv) as read-only. State mutation belongs in the write-locked
247 : : * callbacks (engine_init/engine_uninit, flow_alloc/flow_free,
248 : : * flow_register/flow_unregister, flow_install/flow_uninstall).
249 : : */
250 : : struct ci_flow_engine_ops {
251 : : /* engine init callback - can be NULL */
252 : : int (*engine_init)(const struct ci_flow_engine *engine,
253 : : struct rte_eth_dev_data *dev_data,
254 : : void *priv);
255 : : /* engine uninit callback - can be NULL */
256 : : void (*engine_uninit)(const struct ci_flow_engine *engine,
257 : : void *priv);
258 : : /* allocation callback for flow rules - can be NULL */
259 : : struct ci_flow *(*flow_alloc)(const struct ci_flow_engine *engine,
260 : : struct rte_eth_dev_data *dev_data,
261 : : void *priv);
262 : : /* deallocation callback for flow rules - can be NULL */
263 : : void (*flow_free)(struct ci_flow *flow,
264 : : struct rte_eth_dev_data *dev_data,
265 : : void *priv);
266 : : /* initialize engine context from flow attr/actions - mandatory */
267 : : int (*ctx_init)(const struct rte_flow_action actions[],
268 : : const struct rte_flow_attr *attr,
269 : : struct ci_flow_engine_ctx *ctx,
270 : : struct rte_flow_error *error);
271 : : /* pattern parsing callback - can be NULL */
272 : : int (*pattern_parse)(struct ci_flow_engine_ctx *ctx,
273 : : const struct rte_flow_item pattern[],
274 : : struct rte_flow_error *error);
275 : : /* final pass before converting context to flow - can be NULL */
276 : : int (*ctx_finalize)(struct ci_flow_engine_ctx *ctx,
277 : : struct rte_flow_error *error);
278 : : /* initialize flow rule from parsed context - can be NULL */
279 : : int (*ctx_to_flow)(const struct ci_flow_engine_ctx *ctx,
280 : : struct ci_flow *flow,
281 : : struct rte_flow_error *error);
282 : : /* track a flow rule in driver-internal state - can be NULL */
283 : : int (*flow_register)(struct ci_flow *flow,
284 : : struct rte_flow_error *error);
285 : : /* untrack a flow rule from driver-internal state - can be NULL */
286 : : int (*flow_unregister)(struct ci_flow *flow,
287 : : struct rte_flow_error *error);
288 : : /* install a flow rule to hardware - can be NULL */
289 : : int (*flow_install)(struct ci_flow *flow,
290 : : struct rte_flow_error *error);
291 : : /* uninstall a flow rule from hardware - can be NULL */
292 : : int (*flow_uninstall)(struct ci_flow *flow,
293 : : struct rte_flow_error *error);
294 : : /* query flow - can be NULL */
295 : : int (*flow_query)(struct ci_flow *flow,
296 : : const struct rte_flow_action *action,
297 : : void *data,
298 : : struct rte_flow_error *error);
299 : : };
300 : :
301 : : /* base parse context; engine contexts must start with this struct */
302 : : struct ci_flow_engine_ctx {
303 : : /* ethernet device this context belongs to */
304 : : struct rte_eth_dev_data *dev_data;
305 : : /* per-engine private data, same pointer as flow->engine_priv */
306 : : void *engine_priv;
307 : : /* original flow attributes, as passed by the caller */
308 : : const struct rte_flow_attr *attr;
309 : : /* original flow pattern, as passed by the caller */
310 : : const struct rte_flow_item *pattern;
311 : : /* original flow actions, as passed by the caller */
312 : : const struct rte_flow_action *actions;
313 : : };
314 : :
315 : : /*
316 : : * Common definition for flow rules.
317 : : *
318 : : * A flow rule has three layers of data:
319 : : *
320 : : * 1) Common data (`ci_flow`, defined here)
321 : : * 2) Driver-specific data
322 : : * 3) Engine-specific data
323 : : *
324 : : * `ci_flow` holds the fields common to all drivers and engines. Some are used
325 : : * internally by the framework (e.g. list linkage), others are provided as a
326 : : * basic service for engines' convenience (e.g. a reference to the owning
327 : : * device).
328 : : *
329 : : * Each driver defines its own `rte_flow` structure, which must start with
330 : : * `ci_flow`, followed by driver-specific fields. Each engine may in turn define
331 : : * its own flow structure, which must start with the driver's `rte_flow`,
332 : : * followed by engine-specific fields.
333 : : *
334 : : * IMPORTANT:
335 : : *
336 : : * All of these structures are referred to by the same pointer and can be safely
337 : : * cast between each other *as long as* each one has its parent structure as its
338 : : * first member:
339 : : *
340 : : * struct rte_flow {
341 : : * struct ci_flow base;
342 : : * ...driver-specific fields...
343 : : * };
344 : : *
345 : : * struct ixgbe_fdir_flow {
346 : : * struct rte_flow base;
347 : : * ...engine-specific fields...
348 : : * };
349 : : *
350 : : * struct ci_flow *flow = ...;
351 : : * struct rte_flow *rte_flow = (struct rte_flow *)flow;
352 : : * struct ixgbe_fdir_flow *es_flow = (struct ixgbe_fdir_flow *)flow;
353 : : *
354 : : * Each engine provides a `flow_size` field giving the memory required for its
355 : : * flow structure. The driver must set it for every engine, as it is used to size
356 : : * flow allocations. An engine that needs nothing beyond the driver's `rte_flow`
357 : : * should use `sizeof(struct rte_flow)`.
358 : : *
359 : : * Engine references and engine_idx:
360 : : *
361 : : * Every flow stores its engine index as the engine type discriminator. Runtime
362 : : * code rebuilds a `ci_flow_engine_ref` (engine pointer + index) from the
363 : : * immutable engine list and this index. This means:
364 : : *
365 : : * - The framework only ever works with immutable engine definitions/lists.
366 : : * - No pointer-to-index lookups are needed after flow creation.
367 : : * - Secondary processes work correctly: the index is the stable identity, while
368 : : * pointer values are only meaningful within the shared immutable engine list.
369 : : *
370 : : * Treat engine references as the basic building block when handling engine
371 : : * pointers and indices.
372 : : *
373 : : * Per-engine private data (engine_priv):
374 : : *
375 : : * Each flow carries a pointer to engine-private data, such as custom allocation
376 : : * structures used with flow_alloc/flow_free.
377 : : *
378 : : * Fallback allocation:
379 : : *
380 : : * If a driver-provided `flow_alloc` returns NULL, the framework falls back to
381 : : * rte_zmalloc(). The `fallback_alloc` flag then routes deallocation to
382 : : * rte_free(). Consequently, `flow_alloc` returning NULL is *not* treated as an
383 : : * error.
384 : : */
385 : : struct ci_flow {
386 : : /* linkage in the per-device flow list */
387 : : TAILQ_ENTRY(ci_flow) node;
388 : : /* device this flow belongs to */
389 : : struct rte_eth_dev_data *dev_data;
390 : : /* index of engine this flow was created by */
391 : : size_t engine_idx;
392 : : /* per-engine private data pointer, set by the framework at alloc time */
393 : : void *engine_priv;
394 : : /* set if flow_alloc returned NULL and rte_zmalloc() was used instead */
395 : : bool fallback_alloc;
396 : : };
397 : :
398 : : /* flow engine definition */
399 : : struct ci_flow_engine {
400 : : /* engine name */
401 : : const char *name;
402 : : /* size of engine parse context, can be omitted */
403 : : size_t ctx_size;
404 : : /* size of engine flow struct, at least sizeof(struct ci_flow) */
405 : : size_t flow_size;
406 : : /* size of per-device engine private data, can be omitted */
407 : : size_t priv_size;
408 : : /* ops for this flow engine */
409 : : const struct ci_flow_engine_ops *ops;
410 : : /* pattern graph this engine supports, can be omitted */
411 : : const struct flow_graph *graph;
412 : : };
413 : :
414 : : /* engine reference: immutable engine pointer paired with its list index */
415 : : struct ci_flow_engine_ref {
416 : : /* engine definition */
417 : : const struct ci_flow_engine *engine;
418 : : /* index of the engine in the driver's engine list */
419 : : size_t engine_idx;
420 : : };
421 : :
422 : : #define CI_FLOW_ENGINE_MAX 64
423 : :
424 : : /* flow engine list definition */
425 : : struct ci_flow_engine_list {
426 : : /* NULL-terminated immutable array of flow engine pointers */
427 : : const struct ci_flow_engine * const engines[CI_FLOW_ENGINE_MAX];
428 : : };
429 : :
430 : : /* per-device engine configuration */
431 : : struct ci_flow_engine_conf {
432 : : /* lock protecting the config and the flow list */
433 : : rte_rwlock_t config_lock;
434 : : /* list of flows created on this device */
435 : : TAILQ_HEAD(ci_flow_list, ci_flow) flows;
436 : : /* bitmask of enabled engines, bit N set if engine at index N is enabled */
437 : : uint64_t enabled_engines;
438 : : /* back-reference to device data */
439 : : struct rte_eth_dev_data *dev_data;
440 : : /* reference to the driver's engine list */
441 : : const struct ci_flow_engine_list *engines;
442 : : /* per-engine private data pointers, indexed by engine index */
443 : : void *engine_priv[CI_FLOW_ENGINE_MAX];
444 : : };
445 : :
446 : : /* Engine primitives. */
447 : :
448 : : /* caller must hold config lock */
449 : : static inline struct ci_flow_engine_ref
450 : : ci_flow_engine_ref_from_idx(const struct ci_flow_engine_conf *engine_conf,
451 : : const size_t engine_idx)
452 : : {
453 : : /* check if valid reference can be constructed at all */
454 [ # # # # : 0 : if (engine_conf == NULL || engine_conf->engines == NULL ||
# # # # ]
455 : : engine_idx >= CI_FLOW_ENGINE_MAX) {
456 : : return (struct ci_flow_engine_ref) {
457 : : .engine = NULL,
458 : : .engine_idx = CI_FLOW_ENGINE_MAX
459 : : };
460 : : }
461 : : /* the inputs are valid, but there may be no engine */
462 [ # # # # : 0 : if (engine_conf->engines->engines[engine_idx] == NULL) {
# # # # #
# # # ]
463 : : return (struct ci_flow_engine_ref) {
464 : : .engine = NULL,
465 : : .engine_idx = CI_FLOW_ENGINE_MAX
466 : : };
467 : : }
468 : :
469 : 0 : return (struct ci_flow_engine_ref) {
470 : : .engine = engine_conf->engines->engines[engine_idx],
471 : : .engine_idx = engine_idx,
472 : : };
473 : : }
474 : :
475 : : #define CI_FLOW_ENGINE_REF_FOREACH(engine_ref, engine_conf) \
476 : : for (size_t __ci_flow_engine_idx = 0; \
477 : : __ci_flow_engine_idx < CI_FLOW_ENGINE_MAX && \
478 : : (((engine_ref) = ci_flow_engine_ref_from_idx((engine_conf), \
479 : : __ci_flow_engine_idx)).engine != NULL); \
480 : : __ci_flow_engine_idx++)
481 : :
482 : : static inline bool
483 : 0 : ci_flow_engine_is_valid(const struct ci_flow_engine *engine)
484 : : {
485 [ # # ]: 0 : if (engine == NULL)
486 : : return false;
487 [ # # ]: 0 : if (engine->name == NULL)
488 : : return false;
489 [ # # ]: 0 : if (engine->ops == NULL)
490 : : return false;
491 [ # # ]: 0 : if (engine->ops->ctx_init == NULL)
492 : : return false;
493 : : /* flow size cannot be less than ci_flow */
494 [ # # ]: 0 : if (engine->flow_size < sizeof(struct ci_flow))
495 : : return false;
496 : : /* alloc and free must both be defined or NULL */
497 [ # # ]: 0 : if ((engine->ops->flow_alloc == NULL) != (engine->ops->flow_free == NULL))
498 : : return false;
499 : : /* register and unregister must both be defined or NULL */
500 [ # # ]: 0 : if ((engine->ops->flow_register == NULL) != (engine->ops->flow_unregister == NULL))
501 : 0 : return false;
502 : : return true;
503 : : }
504 : :
505 : : /* caller must hold config lock */
506 : : static inline bool
507 : : ci_flow_engine_is_enabled(const struct ci_flow_engine_conf *conf,
508 : : const size_t engine_idx)
509 : : {
510 : 0 : return (conf->enabled_engines & RTE_BIT64(engine_idx)) != 0;
511 : : }
512 : :
513 : : /* caller must hold config lock */
514 : : static inline void
515 : : ci_flow_engine_set_enabled(struct ci_flow_engine_conf *conf, const size_t engine_idx,
516 : : bool enabled)
517 : : {
518 : : if (enabled)
519 : 0 : conf->enabled_engines |= RTE_BIT64(engine_idx);
520 : : else
521 : 0 : conf->enabled_engines &= ~RTE_BIT64(engine_idx);
522 : : }
523 : :
524 : : /* Engine lifecycle primitives. */
525 : :
526 : : /* caller must hold config lock */
527 : : static inline int
528 : 0 : ci_flow_engine_init(struct ci_flow_engine_conf *engine_conf,
529 : : struct ci_flow_engine_ref engine_ref)
530 : : {
531 : 0 : const struct ci_flow_engine *engine = engine_ref.engine;
532 : : void *priv = NULL;
533 : : int ret;
534 : :
535 [ # # ]: 0 : if (engine->priv_size > 0) {
536 : 0 : priv = rte_zmalloc(engine->name, engine->priv_size, 0);
537 [ # # ]: 0 : if (priv == NULL) {
538 : : ret = -ENOMEM;
539 : 0 : goto err;
540 : : }
541 : : }
542 : :
543 [ # # ]: 0 : if (engine->ops->engine_init != NULL) {
544 : 0 : ret = engine->ops->engine_init(engine, engine_conf->dev_data, priv);
545 [ # # ]: 0 : if (ret != 0)
546 : 0 : goto err;
547 : : }
548 : 0 : engine_conf->engine_priv[engine_ref.engine_idx] = priv;
549 : 0 : return 0;
550 : 0 : err:
551 : 0 : rte_free(priv);
552 : 0 : return ret;
553 : : }
554 : :
555 : : /* caller must hold config lock */
556 : : static inline void
557 : 0 : ci_flow_engine_uninit(struct ci_flow_engine_conf *engine_conf,
558 : : struct ci_flow_engine_ref engine_ref)
559 : : {
560 : 0 : const struct ci_flow_engine *engine = engine_ref.engine;
561 : : void *priv;
562 : :
563 : 0 : priv = engine_conf->engine_priv[engine_ref.engine_idx];
564 : :
565 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': uninit", engine->name);
566 : : /* ignore uninit errors */
567 [ # # ]: 0 : if (engine->ops->engine_uninit != NULL)
568 : 0 : engine->ops->engine_uninit(engine, priv);
569 : :
570 : 0 : rte_free(priv);
571 : 0 : engine_conf->engine_priv[engine_ref.engine_idx] = NULL;
572 : 0 : }
573 : :
574 : : /* Flow primitives. */
575 : :
576 : : /* caller must hold config lock */
577 : : static inline bool
578 : : ci_flow_is_valid(const struct ci_flow *flow,
579 : : const struct ci_flow_engine_conf *engine_conf)
580 : : {
581 [ # # ]: 0 : if (flow == NULL)
582 : : return false;
583 [ # # # # : 0 : if (engine_conf == NULL || engine_conf->dev_data == NULL ||
# # # # #
# ]
584 [ # # # # : 0 : engine_conf->engines == NULL)
# # # # ]
585 : : return false;
586 [ # # # # : 0 : if (flow->dev_data != engine_conf->dev_data)
# # # # ]
587 : : return false;
588 [ # # # # : 0 : if (flow->engine_idx >= CI_FLOW_ENGINE_MAX)
# # # # ]
589 : : return false;
590 [ # # # # : 0 : if (!ci_flow_engine_is_enabled(engine_conf, flow->engine_idx))
# # # # ]
591 : : return false;
592 : : return true;
593 : : }
594 : :
595 : : static inline struct ci_flow *
596 : 0 : ci_flow_alloc(const struct ci_flow_engine_conf *engine_conf,
597 : : struct ci_flow_engine_ref engine_ref)
598 : : {
599 : 0 : const struct ci_flow_engine *engine = engine_ref.engine;
600 : 0 : void *priv = engine_conf->engine_priv[engine_ref.engine_idx];
601 : : struct ci_flow *flow = NULL;
602 : : bool fallback = false;
603 : :
604 : : /* if engine has an allocator callback, try it first */
605 [ # # ]: 0 : if (engine->ops->flow_alloc != NULL)
606 : 0 : flow = engine->ops->flow_alloc(engine, engine_conf->dev_data, priv);
607 : : /* if allocator callback is not defined or failed, use default allocator */
608 [ # # ]: 0 : if (flow == NULL) {
609 : 0 : flow = (struct ci_flow *)rte_zmalloc(NULL, engine->flow_size, 0);
610 : :
611 : : /* if callback exists and we're here, allocation has fallen back */
612 [ # # # # ]: 0 : if (flow != NULL && engine->ops->flow_alloc != NULL)
613 : : fallback = true;
614 : : }
615 [ # # ]: 0 : if (flow != NULL) {
616 : : /* initialize the common part */
617 : : memset(flow, 0, sizeof(struct ci_flow));
618 : 0 : flow->fallback_alloc = fallback;
619 : 0 : flow->engine_idx = engine_ref.engine_idx;
620 : 0 : flow->dev_data = engine_conf->dev_data;
621 : 0 : flow->engine_priv = priv;
622 : : }
623 : 0 : return flow;
624 : : }
625 : :
626 : : static inline void
627 : 0 : ci_flow_free(struct ci_flow_engine_ref engine_ref, struct ci_flow *flow)
628 : : {
629 : 0 : const struct ci_flow_engine *engine = engine_ref.engine;
630 : :
631 [ # # # # ]: 0 : if (engine->ops->flow_free != NULL && !flow->fallback_alloc)
632 : 0 : engine->ops->flow_free(flow, flow->dev_data, flow->engine_priv);
633 : : else
634 : 0 : rte_free(flow);
635 : 0 : }
636 : :
637 : : /* Track stage - caller must hold config lock */
638 : : static inline int
639 : : ci_flow_register(struct ci_flow_engine_ref engine_ref,
640 : : struct ci_flow *flow,
641 : : struct rte_flow_error *error)
642 : : {
643 : : const struct ci_flow_engine *engine = engine_ref.engine;
644 : :
645 [ # # ]: 0 : if (engine->ops->flow_register != NULL)
646 : 0 : return engine->ops->flow_register(flow, error);
647 : :
648 : : return 0;
649 : : }
650 : :
651 : : /* Track stage - caller must hold config lock */
652 : : static inline int
653 : : ci_flow_unregister(struct ci_flow_engine_ref engine_ref,
654 : : struct ci_flow *flow,
655 : : struct rte_flow_error *error)
656 : : {
657 : : const struct ci_flow_engine *engine = engine_ref.engine;
658 : :
659 [ # # # # ]: 0 : if (engine->ops->flow_unregister != NULL)
660 : 0 : return engine->ops->flow_unregister(flow, error);
661 : :
662 : : return 0;
663 : : }
664 : :
665 : : /* Apply stage - caller must hold config lock */
666 : : static inline int
667 : : ci_flow_install(struct ci_flow_engine_ref engine_ref,
668 : : struct ci_flow *flow,
669 : : struct rte_flow_error *error)
670 : : {
671 : : const struct ci_flow_engine *engine = engine_ref.engine;
672 : :
673 [ # # ]: 0 : if (engine->ops->flow_install != NULL)
674 : 0 : return engine->ops->flow_install(flow, error);
675 : :
676 : : return 0;
677 : : }
678 : :
679 : : /* Apply stage - caller must hold config lock */
680 : : static inline int
681 : : ci_flow_uninstall(struct ci_flow_engine_ref engine_ref,
682 : : struct ci_flow *flow,
683 : : struct rte_flow_error *error)
684 : : {
685 : : const struct ci_flow_engine *engine = engine_ref.engine;
686 : :
687 [ # # # # ]: 0 : if (engine->ops->flow_uninstall != NULL)
688 : 0 : return engine->ops->flow_uninstall(flow, error);
689 : :
690 : : return 0;
691 : : }
692 : :
693 : : /* Configuration primitives. */
694 : :
695 : : /* caller must serialize initialization */
696 : : static inline int
697 : 0 : ci_flow_engine_conf_init(struct ci_flow_engine_conf *engine_conf,
698 : : const struct ci_flow_engine_list *engine_list,
699 : : struct rte_eth_dev_data *dev_data)
700 : : {
701 : : struct ci_flow_engine_ref engine_ref;
702 : :
703 [ # # # # ]: 0 : if (engine_conf == NULL || engine_list == NULL || dev_data == NULL)
704 : : return -1;
705 : :
706 : : rte_rwlock_init(&engine_conf->config_lock);
707 : :
708 : 0 : engine_conf->dev_data = dev_data;
709 : 0 : engine_conf->engines = engine_list;
710 : :
711 : 0 : TAILQ_INIT(&engine_conf->flows);
712 : :
713 [ # # # # ]: 0 : CI_FLOW_ENGINE_REF_FOREACH(engine_ref, engine_conf) {
714 [ # # ]: 0 : if (!ci_flow_engine_is_valid(engine_ref.engine)) {
715 : 0 : CI_DRV_LOG(DEBUG, "engine[%zu]: invalid, skipping",
716 : : engine_ref.engine_idx);
717 : 0 : continue;
718 : : }
719 : 0 : int ret = ci_flow_engine_init(engine_conf, engine_ref);
720 : :
721 [ # # ]: 0 : if (ret != 0) {
722 [ # # ]: 0 : if (ret == -ENOTSUP)
723 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': not supported, skipping",
724 : : engine_ref.engine->name);
725 : : else
726 : 0 : CI_DRV_LOG(ERR, "engine '%s': init failed (%d: %s), skipping",
727 : : engine_ref.engine->name, ret, rte_strerror(-ret));
728 : 0 : continue;
729 : : }
730 : :
731 : : ci_flow_engine_set_enabled(engine_conf,
732 : : engine_ref.engine_idx, true);
733 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': enabled", engine_ref.engine->name);
734 : : }
735 : : return 0;
736 : : }
737 : :
738 : : /* caller must hold config lock */
739 : : static inline void
740 : 0 : ci_flow_engine_conf_clear(struct ci_flow_engine_conf *engine_conf)
741 : : {
742 : : struct ci_flow *flow, *tmp;
743 : :
744 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(flow, &engine_conf->flows, node, tmp) {
745 : : struct ci_flow_engine_ref engine_ref;
746 : 0 : struct rte_flow_error error = {0};
747 : : int unregister_ret;
748 : :
749 [ # # ]: 0 : engine_ref = ci_flow_engine_ref_from_idx(engine_conf, flow->engine_idx);
750 : : unregister_ret = ci_flow_unregister(engine_ref, flow, &error);
751 [ # # ]: 0 : if (unregister_ret != 0)
752 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to unregister flow: %s",
753 : : engine_ref.engine->name, error.message);
754 : :
755 [ # # ]: 0 : TAILQ_REMOVE(&engine_conf->flows, flow, node);
756 : 0 : ci_flow_free(engine_ref, flow);
757 : : }
758 : 0 : }
759 : : /* caller must serialize teardown */
760 : : static inline void
761 : 0 : ci_flow_engine_conf_reset(struct ci_flow_engine_conf *engine_conf)
762 : : {
763 : : struct ci_flow_engine_ref engine_ref;
764 : :
765 : 0 : ci_flow_engine_conf_clear(engine_conf);
766 : :
767 [ # # # # ]: 0 : CI_FLOW_ENGINE_REF_FOREACH(engine_ref, engine_conf) {
768 [ # # ]: 0 : if (!ci_flow_engine_is_enabled(engine_conf, engine_ref.engine_idx))
769 : 0 : continue;
770 : : /* ignore errors */
771 : 0 : ci_flow_engine_uninit(engine_conf, engine_ref);
772 : : ci_flow_engine_set_enabled(engine_conf,
773 : : engine_ref.engine_idx, false);
774 : : }
775 : :
776 : 0 : engine_conf->dev_data = NULL;
777 : 0 : engine_conf->engines = NULL;
778 : 0 : }
779 : :
780 : : /* Driver-facing configuration operations. These take the config lock internally. */
781 : :
782 : : /* reinstall kept flows to hardware */
783 : : static inline int
784 : 0 : ci_flow_engine_conf_replay(struct ci_flow_engine_conf *engine_conf)
785 : : {
786 : 0 : struct rte_flow_error error = {0};
787 : : struct ci_flow *flow;
788 : :
789 : 0 : CI_DRV_LOG(DEBUG, "replaying all flows");
790 : :
791 : 0 : rte_rwlock_write_lock(&engine_conf->config_lock);
792 : :
793 [ # # ]: 0 : TAILQ_FOREACH(flow, &engine_conf->flows, node) {
794 : : struct ci_flow_engine_ref engine_ref;
795 : : int install_ret;
796 : :
797 : : /* this shouldn't happen */
798 : 0 : if (!ci_flow_is_valid(flow, engine_conf))
799 : : continue;
800 : :
801 : : engine_ref = ci_flow_engine_ref_from_idx(engine_conf,
802 : : flow->engine_idx);
803 : :
804 : : install_ret = ci_flow_install(engine_ref, flow, &error);
805 [ # # ]: 0 : if (install_ret != 0) {
806 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to install flow: %s",
807 : : engine_ref.engine->name, error.message);
808 : : }
809 : : }
810 : :
811 : : rte_rwlock_write_unlock(&engine_conf->config_lock);
812 : :
813 : 0 : return 0;
814 : : }
815 : :
816 : : /* drop all flows from config */
817 : : static inline int
818 : 0 : ci_flow_engine_conf_cleanup(struct ci_flow_engine_conf *engine_conf)
819 : : {
820 : 0 : CI_DRV_LOG(DEBUG, "cleaning up all flows");
821 : :
822 : 0 : rte_rwlock_write_lock(&engine_conf->config_lock);
823 : 0 : ci_flow_engine_conf_clear(engine_conf);
824 : : rte_rwlock_write_unlock(&engine_conf->config_lock);
825 : :
826 : 0 : return 0;
827 : : }
828 : :
829 : : /* Pattern parsing primitives. */
830 : :
831 : : /* accepts only empty and ANY-only patterns */
832 : : enum ci_flow_empty_graph_node_id {
833 : : CI_FLOW_EMPTY_GRAPH_NODE_START = FLOW_GRAPH_NODE_FIRST,
834 : : CI_FLOW_EMPTY_GRAPH_NODE_ANY,
835 : : CI_FLOW_EMPTY_GRAPH_NODE_END,
836 : : };
837 : :
838 : : static const struct flow_graph ci_flow_empty_graph = {
839 : : .nodes = (struct flow_graph_node []) {
840 : : [CI_FLOW_EMPTY_GRAPH_NODE_START] = {
841 : : .name = "START",
842 : : },
843 : : [CI_FLOW_EMPTY_GRAPH_NODE_ANY] = {
844 : : .name = "ANY",
845 : : .type = RTE_FLOW_ITEM_TYPE_ANY,
846 : : .constraints = FLOW_GRAPH_NODE_EXPECT_EMPTY,
847 : : },
848 : : [CI_FLOW_EMPTY_GRAPH_NODE_END] = {
849 : : .name = "END",
850 : : .type = RTE_FLOW_ITEM_TYPE_END,
851 : : },
852 : : },
853 : : .edges = (struct flow_graph_edge []) {
854 : : [CI_FLOW_EMPTY_GRAPH_NODE_START] = {
855 : : .next = (size_t []) {
856 : : CI_FLOW_EMPTY_GRAPH_NODE_ANY,
857 : : CI_FLOW_EMPTY_GRAPH_NODE_END,
858 : : FLOW_GRAPH_NODE_EDGE_END,
859 : : },
860 : : },
861 : : [CI_FLOW_EMPTY_GRAPH_NODE_ANY] = {
862 : : .next = (size_t []) {
863 : : CI_FLOW_EMPTY_GRAPH_NODE_END,
864 : : FLOW_GRAPH_NODE_EDGE_END,
865 : : },
866 : : },
867 : : }
868 : : };
869 : :
870 : : /* concrete pattern-matching mode selected from graph/callback presence */
871 : : enum ci_match_type {
872 : : CI_MATCH_EMPTY, /* no graph, no callback */
873 : : CI_MATCH_CALLBACK, /* callback only */
874 : : CI_MATCH_GRAPH, /* graph only */
875 : : CI_MATCH_ALL, /* callback + graph */
876 : : };
877 : :
878 : : /* caller must hold config lock */
879 : : static inline int
880 : 0 : ci_flow_match(const struct ci_flow_engine *engine,
881 : : const struct rte_flow_item pattern[],
882 : : struct ci_flow_engine_ctx *ctx,
883 : : enum ci_match_type match_type,
884 : : struct rte_flow_error *error)
885 : : {
886 [ # # # # : 0 : switch (match_type) {
# ]
887 : 0 : case CI_MATCH_EMPTY:
888 : : /* for empty matching, NULL pattern is not an error */
889 [ # # ]: 0 : if (pattern != NULL) {
890 : 0 : return flow_graph_parse(&ci_flow_empty_graph,
891 : : pattern, error, ctx);
892 : : }
893 : : return 0;
894 : :
895 : 0 : case CI_MATCH_CALLBACK:
896 [ # # ]: 0 : if (pattern == NULL) {
897 : 0 : return rte_flow_error_set(error, EINVAL,
898 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
899 : : "Pattern cannot be NULL");
900 : : }
901 : 0 : return engine->ops->pattern_parse(ctx, pattern, error);
902 : :
903 : 0 : case CI_MATCH_GRAPH:
904 [ # # ]: 0 : if (pattern == NULL) {
905 : 0 : return rte_flow_error_set(error, EINVAL,
906 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
907 : : "Pattern cannot be NULL");
908 : : }
909 : 0 : return flow_graph_parse(engine->graph, pattern, error, ctx);
910 : :
911 : 0 : case CI_MATCH_ALL:
912 : : {
913 : : int ret;
914 : :
915 [ # # ]: 0 : if (pattern == NULL) {
916 : 0 : return rte_flow_error_set(error, EINVAL,
917 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
918 : : "Pattern cannot be NULL");
919 : : }
920 : 0 : ret = engine->ops->pattern_parse(ctx, pattern, error);
921 [ # # ]: 0 : if (ret != 0)
922 : : return ret;
923 : 0 : return flow_graph_parse(engine->graph, pattern, error, ctx);
924 : : }
925 : :
926 : 0 : default:
927 : 0 : return rte_flow_error_set(error, EINVAL,
928 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
929 : : "Invalid match type");
930 : : }
931 : : }
932 : :
933 : : /* caller must hold config lock */
934 : : static inline int
935 : 0 : ci_flow_parse(const struct ci_flow_engine_conf *engine_conf,
936 : : const struct ci_flow_engine *engine,
937 : : const struct rte_flow_attr *attr,
938 : : const struct rte_flow_item pattern[],
939 : : const struct rte_flow_action actions[],
940 : : struct ci_flow *flow,
941 : : struct rte_flow_error *error)
942 : : {
943 : : enum ci_match_type match_type;
944 : : struct ci_flow_engine_ctx *ctx;
945 : : int ret = 0;
946 : :
947 : : /*
948 : : * Matching mode depends on whether a pattern graph and/or a
949 : : * pattern_parse callback are provided:
950 : : *
951 : : * - graph only: match against the graph. This is the expected default;
952 : : * no special handling of pattern items.
953 : : *
954 : : * - graph + callback: the callback preprocesses the pattern, then the
955 : : * graph engine does the matching. The graph is expected to have an
956 : : * ignore list to skip nodes not meant for it.
957 : : *
958 : : * - callback only: fully custom pattern parsing.
959 : : *
960 : : * - neither: match against an empty graph, for engines that don't care
961 : : * about the pattern. Default behavior accepts a NULL pattern and
962 : : * END or ANY -> END patterns, and rejects anything else (so the
963 : : * pattern is still checked, not ignored).
964 : : */
965 : 0 : match_type = engine->graph == NULL ?
966 [ # # ]: 0 : (engine->ops->pattern_parse == NULL ? CI_MATCH_EMPTY : CI_MATCH_CALLBACK) :
967 [ # # ]: 0 : (engine->ops->pattern_parse == NULL ? CI_MATCH_GRAPH : CI_MATCH_ALL);
968 : :
969 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': parsing flow", engine->name);
970 : :
971 : 0 : ctx = (struct ci_flow_engine_ctx *)calloc(1,
972 : 0 : RTE_MAX(engine->ctx_size, sizeof(struct ci_flow_engine_ctx)));
973 [ # # ]: 0 : if (ctx == NULL) {
974 : 0 : return rte_flow_error_set(error, ENOMEM,
975 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
976 : : "Failed to allocate memory for rule engine context");
977 : : }
978 : 0 : ctx->dev_data = engine_conf->dev_data;
979 : 0 : ctx->engine_priv = flow->engine_priv;
980 : 0 : ctx->attr = attr;
981 : 0 : ctx->pattern = pattern;
982 : 0 : ctx->actions = actions;
983 : 0 : flow->dev_data = engine_conf->dev_data;
984 : :
985 : 0 : ret = engine->ops->ctx_init(actions, attr, ctx, error);
986 [ # # ]: 0 : if (ret != 0)
987 : 0 : goto free_ctx;
988 : :
989 : 0 : ret = ci_flow_match(engine, pattern, ctx, match_type, error);
990 [ # # ]: 0 : if (ret != 0)
991 : 0 : goto free_ctx;
992 : :
993 [ # # ]: 0 : if (engine->ops->ctx_finalize != NULL)
994 : 0 : ret = engine->ops->ctx_finalize(ctx, error);
995 [ # # ]: 0 : if (ret != 0)
996 : 0 : goto free_ctx;
997 : :
998 [ # # ]: 0 : if (engine->ops->ctx_to_flow != NULL) {
999 : 0 : ret = engine->ops->ctx_to_flow(ctx, flow, error);
1000 [ # # ]: 0 : if (ret != 0)
1001 : 0 : goto free_ctx;
1002 : : }
1003 : : ret = 0;
1004 : :
1005 : 0 : free_ctx:
1006 : 0 : free(ctx);
1007 : 0 : return ret;
1008 : : }
1009 : :
1010 : : /* Default rte_flow op implementations. These take the config lock internally. */
1011 : :
1012 : : static inline struct rte_flow *
1013 : 0 : ci_flow_create(struct ci_flow_engine_conf *engine_conf,
1014 : : const struct rte_flow_attr *attr,
1015 : : const struct rte_flow_item pattern[],
1016 : : const struct rte_flow_action actions[],
1017 : : struct rte_flow_error *error)
1018 : : {
1019 : : struct ci_flow_engine_ref engine_ref;
1020 : : struct ci_flow *flow = NULL;
1021 : 0 : struct rte_flow_error local_error = {0};
1022 : : int ret;
1023 : :
1024 [ # # ]: 0 : if (error == NULL)
1025 : : error = &local_error;
1026 : :
1027 [ # # ]: 0 : if (attr == NULL || actions == NULL) {
1028 : 0 : CI_DRV_LOG(DEBUG, "attr or actions is NULL");
1029 : 0 : rte_flow_error_set(error, EINVAL,
1030 : : RTE_FLOW_ERROR_TYPE_ATTR, NULL,
1031 : : "Attributes and actions cannot be NULL");
1032 : 0 : return NULL;
1033 : : }
1034 : :
1035 : 0 : rte_rwlock_write_lock(&engine_conf->config_lock);
1036 : :
1037 [ # # # # ]: 0 : CI_FLOW_ENGINE_REF_FOREACH(engine_ref, engine_conf) {
1038 [ # # ]: 0 : if (!ci_flow_engine_is_enabled(engine_conf, engine_ref.engine_idx))
1039 : 0 : continue;
1040 : :
1041 : 0 : flow = ci_flow_alloc(engine_conf, engine_ref);
1042 [ # # ]: 0 : if (flow == NULL) {
1043 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to allocate flow",
1044 : : engine_ref.engine->name);
1045 : 0 : rte_flow_error_set(error, ENOMEM,
1046 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1047 : : "Failed to allocate memory for flow rule");
1048 : : /* this is a serious error so don't continue */
1049 : 0 : goto unlock;
1050 : : }
1051 : :
1052 : 0 : ret = ci_flow_parse(engine_conf, engine_ref.engine, attr, pattern,
1053 : : actions, flow, error);
1054 : :
1055 : : /* parsing failed - free the flow and try next engine */
1056 [ # # ]: 0 : if (ret != 0) {
1057 : 0 : ci_flow_free(engine_ref, flow);
1058 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' rejected flow: %s",
1059 : : engine_ref.engine->name, error->message);
1060 : : /* if parsing failed due to calloc failure */
1061 [ # # ]: 0 : if (ret == -ENOMEM) {
1062 : : flow = NULL;
1063 : 0 : goto unlock;
1064 : : }
1065 : 0 : continue;
1066 : : }
1067 : :
1068 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' accepted flow, committing",
1069 : : engine_ref.engine->name);
1070 : :
1071 : : ret = ci_flow_register(engine_ref, flow, error);
1072 [ # # ]: 0 : if (ret != 0) {
1073 : : /* track failed after parse accepted - do not try other engines */
1074 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' register failed: %s",
1075 : : engine_ref.engine->name, error->message);
1076 : 0 : ci_flow_free(engine_ref, flow);
1077 : : flow = NULL;
1078 : 0 : goto unlock;
1079 : : }
1080 : :
1081 : : ret = ci_flow_install(engine_ref, flow, error);
1082 [ # # ]: 0 : if (ret != 0) {
1083 : 0 : struct rte_flow_error unreg_error = { 0 };
1084 : : int unreg_ret;
1085 : :
1086 : : /* install failed after parse accepted - do not try other engines */
1087 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' install failed: %s",
1088 : : engine_ref.engine->name, error->message);
1089 : : /* separate error struct to preserve the install error */
1090 : : unreg_ret = ci_flow_unregister(engine_ref, flow, &unreg_error);
1091 [ # # ]: 0 : if (unreg_ret != 0)
1092 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to unregister: %s",
1093 : : engine_ref.engine->name, unreg_error.message);
1094 : 0 : ci_flow_free(engine_ref, flow);
1095 : : flow = NULL;
1096 : 0 : goto unlock;
1097 : : }
1098 : :
1099 : 0 : CI_DRV_LOG(DEBUG, "flow installed by engine '%s'",
1100 : : engine_ref.engine->name);
1101 : 0 : TAILQ_INSERT_TAIL(&engine_conf->flows, flow, node);
1102 : 0 : goto unlock;
1103 : : }
1104 : :
1105 : 0 : CI_DRV_LOG(DEBUG, "no engine accepted the flow");
1106 : : flow = NULL;
1107 : 0 : rte_flow_error_set(error, ENOTSUP,
1108 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1109 : : "No flow engine could handle the requested flow");
1110 : 0 : unlock:
1111 : : rte_rwlock_write_unlock(&engine_conf->config_lock);
1112 : :
1113 : 0 : return (struct rte_flow *)flow;
1114 : : }
1115 : :
1116 : : static inline int
1117 : 0 : ci_flow_validate(struct ci_flow_engine_conf *engine_conf,
1118 : : const struct rte_flow_attr *attr,
1119 : : const struct rte_flow_item pattern[],
1120 : : const struct rte_flow_action actions[],
1121 : : struct rte_flow_error *error)
1122 : : {
1123 : : struct ci_flow_engine_ref engine_ref;
1124 : 0 : struct rte_flow_error local_error = {0};
1125 : : int ret;
1126 : :
1127 [ # # ]: 0 : if (error == NULL)
1128 : : error = &local_error;
1129 : :
1130 [ # # ]: 0 : if (attr == NULL || actions == NULL) {
1131 : 0 : CI_DRV_LOG(DEBUG, "attr or actions is NULL");
1132 : 0 : return rte_flow_error_set(error, EINVAL,
1133 : : RTE_FLOW_ERROR_TYPE_ATTR, NULL,
1134 : : "Attributes and actions cannot be NULL");
1135 : : }
1136 : :
1137 : 0 : rte_rwlock_read_lock(&engine_conf->config_lock);
1138 : :
1139 [ # # # # ]: 0 : CI_FLOW_ENGINE_REF_FOREACH(engine_ref, engine_conf) {
1140 : : struct ci_flow *flow;
1141 : :
1142 [ # # ]: 0 : if (!ci_flow_engine_is_enabled(engine_conf, engine_ref.engine_idx))
1143 : 0 : continue;
1144 : :
1145 : : /* flow is not kept, so skip the engine allocator */
1146 : 0 : flow = (struct ci_flow *)calloc(1, engine_ref.engine->flow_size);
1147 [ # # ]: 0 : if (flow == NULL) {
1148 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to allocate flow",
1149 : : engine_ref.engine->name);
1150 : 0 : ret = rte_flow_error_set(error, ENOMEM,
1151 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1152 : : "Failed to allocate memory for flow rule");
1153 : 0 : goto unlock;
1154 : : }
1155 : 0 : flow->fallback_alloc = false;
1156 : 0 : flow->engine_idx = engine_ref.engine_idx;
1157 : 0 : flow->dev_data = engine_conf->dev_data;
1158 : 0 : flow->engine_priv = engine_conf->engine_priv[engine_ref.engine_idx];
1159 : :
1160 : 0 : ret = ci_flow_parse(engine_conf, engine_ref.engine, attr, pattern,
1161 : : actions, flow, error);
1162 : 0 : free(flow);
1163 : :
1164 [ # # ]: 0 : if (ret == 0) {
1165 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' accepted flow",
1166 : : engine_ref.engine->name);
1167 : 0 : goto unlock;
1168 : : } else {
1169 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' rejected flow: %s",
1170 : : engine_ref.engine->name, error->message);
1171 : : }
1172 : : /* if parsing failed to due to calloc failure */
1173 [ # # ]: 0 : if (ret == -ENOMEM) {
1174 : 0 : goto unlock;
1175 : : }
1176 : : }
1177 : 0 : CI_DRV_LOG(DEBUG, "no engine accepted the flow");
1178 : 0 : ret = rte_flow_error_set(error, ENOTSUP,
1179 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1180 : : "No flow engine could handle the requested flow");
1181 : 0 : unlock:
1182 : : rte_rwlock_read_unlock(&engine_conf->config_lock);
1183 : 0 : return ret;
1184 : : }
1185 : :
1186 : : static inline int
1187 : 0 : ci_flow_destroy(struct ci_flow_engine_conf *engine_conf,
1188 : : struct rte_flow *rte_flow,
1189 : : struct rte_flow_error *error)
1190 : : {
1191 : : struct ci_flow *flow = (struct ci_flow *)rte_flow;
1192 : : struct ci_flow_engine_ref engine_ref;
1193 : 0 : struct rte_flow_error local_error = {0};
1194 : 0 : struct rte_flow_error unreg_error = {0};
1195 : : int unreg_ret, ret = 0;
1196 : :
1197 [ # # ]: 0 : if (error == NULL)
1198 : : error = &local_error;
1199 : :
1200 [ # # ]: 0 : if (rte_flow == NULL) {
1201 : 0 : CI_DRV_LOG(DEBUG, "flow handle is NULL");
1202 : 0 : return rte_flow_error_set(error, EINVAL,
1203 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1204 : : "Flow handle cannot be NULL");
1205 : : }
1206 : :
1207 : 0 : rte_rwlock_write_lock(&engine_conf->config_lock);
1208 : :
1209 : : if (!ci_flow_is_valid(flow, engine_conf)) {
1210 : 0 : CI_DRV_LOG(DEBUG, "invalid flow handle");
1211 : 0 : ret = rte_flow_error_set(error, EINVAL,
1212 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1213 : : "Invalid flow handle");
1214 : 0 : goto unlock;
1215 : : }
1216 : : engine_ref = ci_flow_engine_ref_from_idx(engine_conf, flow->engine_idx);
1217 : 0 : CI_DRV_LOG(DEBUG, "uninstalling flow (engine '%s')",
1218 : : engine_ref.engine->name);
1219 : :
1220 : : ret = ci_flow_uninstall(engine_ref, flow, error);
1221 : :
1222 [ # # ]: 0 : if (ret != 0) {
1223 : 0 : CI_DRV_LOG(DEBUG, "uninstall failed: %s", error->message);
1224 : 0 : goto unlock;
1225 : : }
1226 : :
1227 : : /* hardware is already clean, so untrack failure is not fatal */
1228 : : unreg_ret = ci_flow_unregister(engine_ref, flow, &unreg_error);
1229 [ # # ]: 0 : if (unreg_ret != 0)
1230 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to unregister flow: %s",
1231 : : engine_ref.engine->name, unreg_error.message);
1232 : :
1233 [ # # ]: 0 : TAILQ_REMOVE(&engine_conf->flows, flow, node);
1234 : 0 : ci_flow_free(engine_ref, flow);
1235 : 0 : unlock:
1236 : : rte_rwlock_write_unlock(&engine_conf->config_lock);
1237 : :
1238 : 0 : return ret;
1239 : : }
1240 : :
1241 : : static inline int
1242 : 0 : ci_flow_flush(struct ci_flow_engine_conf *engine_conf,
1243 : : struct rte_flow_error *error)
1244 : : {
1245 : : struct ci_flow *flow, *tmp;
1246 : 0 : struct rte_flow_error local_error = {0};
1247 : : bool error_set = false;
1248 : : int ret = 0;
1249 : :
1250 [ # # ]: 0 : if (error == NULL)
1251 : : error = &local_error;
1252 : :
1253 : 0 : CI_DRV_LOG(DEBUG, "removing all flows");
1254 : :
1255 : 0 : rte_rwlock_write_lock(&engine_conf->config_lock);
1256 : :
1257 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(flow, &engine_conf->flows, node, tmp) {
1258 : : struct ci_flow_engine_ref engine_ref;
1259 [ # # ]: 0 : struct rte_flow_error flow_error = {0};
1260 : : int uninstall_ret;
1261 : : int unreg_ret;
1262 : :
1263 : : /* this shouldn't happen */
1264 : 0 : if (!ci_flow_is_valid(flow, engine_conf))
1265 : 0 : continue;
1266 : :
1267 : : engine_ref = ci_flow_engine_ref_from_idx(engine_conf,
1268 : : flow->engine_idx);
1269 : :
1270 : : uninstall_ret = ci_flow_uninstall(engine_ref, flow, &flow_error);
1271 : :
1272 : : /* uninstall might legitimately be expected to fail at flush time */
1273 [ # # ]: 0 : if (uninstall_ret != 0)
1274 : 0 : CI_DRV_LOG(DEBUG, "engine '%s': failed to uninstall flow: %s",
1275 : : engine_ref.engine->name, flow_error.message);
1276 : :
1277 [ # # ]: 0 : flow_error = (struct rte_flow_error){0};
1278 : : unreg_ret = ci_flow_unregister(engine_ref, flow, &flow_error);
1279 [ # # ]: 0 : if (unreg_ret != 0) {
1280 : 0 : CI_DRV_LOG(ERR, "engine '%s': failed to untrack flow: %s",
1281 : : engine_ref.engine->name, flow_error.message);
1282 [ # # ]: 0 : if (!error_set) {
1283 : : ret = unreg_ret;
1284 : 0 : *error = flow_error;
1285 : : error_set = true;
1286 : : }
1287 : : }
1288 : :
1289 [ # # ]: 0 : TAILQ_REMOVE(&engine_conf->flows, flow, node);
1290 : 0 : ci_flow_free(engine_ref, flow);
1291 : : }
1292 : :
1293 : : rte_rwlock_write_unlock(&engine_conf->config_lock);
1294 [ # # ]: 0 : if (error_set)
1295 : 0 : rte_errno = -ret;
1296 : :
1297 : 0 : return ret;
1298 : : }
1299 : :
1300 : : static inline int
1301 : 0 : ci_flow_query(struct ci_flow_engine_conf *engine_conf,
1302 : : struct rte_flow *rte_flow,
1303 : : const struct rte_flow_action *action,
1304 : : void *data,
1305 : : struct rte_flow_error *error)
1306 : : {
1307 : : struct ci_flow *flow = (struct ci_flow *)rte_flow;
1308 : : struct ci_flow_engine_ref engine_ref;
1309 : 0 : struct rte_flow_error local_error = {0};
1310 : : int ret;
1311 : :
1312 [ # # ]: 0 : if (error == NULL)
1313 : : error = &local_error;
1314 : :
1315 [ # # ]: 0 : if (action == NULL || data == NULL) {
1316 : 0 : CI_DRV_LOG(DEBUG, "action or data is NULL");
1317 : 0 : return rte_flow_error_set(error, EINVAL,
1318 : : RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1319 : : "Action or data cannot be NULL");
1320 : : }
1321 : :
1322 : 0 : rte_rwlock_read_lock(&engine_conf->config_lock);
1323 : :
1324 : : if (!ci_flow_is_valid(flow, engine_conf)) {
1325 : 0 : CI_DRV_LOG(DEBUG, "invalid flow handle");
1326 : 0 : ret = rte_flow_error_set(error, EINVAL,
1327 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1328 : : "Invalid flow handle");
1329 : 0 : goto unlock;
1330 : : }
1331 : : engine_ref = ci_flow_engine_ref_from_idx(engine_conf, flow->engine_idx);
1332 [ # # ]: 0 : if (engine_ref.engine->ops->flow_query != NULL) {
1333 : 0 : ret = engine_ref.engine->ops->flow_query(flow, action, data, error);
1334 : :
1335 [ # # ]: 0 : if (ret != 0)
1336 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' query failed: %s",
1337 : : engine_ref.engine->name, error->message);
1338 : : } else {
1339 : 0 : CI_DRV_LOG(DEBUG, "engine '%s' does not support querying",
1340 : : engine_ref.engine->name);
1341 : 0 : ret = rte_flow_error_set(error, ENOTSUP,
1342 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1343 : : "Flow engine does not support querying");
1344 : : }
1345 : 0 : unlock:
1346 : : rte_rwlock_read_unlock(&engine_conf->config_lock);
1347 : :
1348 : 0 : return ret;
1349 : : }
1350 : :
1351 : : #define CI_FLOW_DUMP_CHUNK_BYTES 32
1352 : :
1353 : : static inline void
1354 : 0 : ci_flow_dump_one(FILE *file, const char *driver, const char *engine,
1355 : : const void *data, size_t data_len)
1356 : : {
1357 : : const uint8_t *raw = (const uint8_t *)data;
1358 : 0 : const size_t nchunks =
1359 : 0 : (data_len + CI_FLOW_DUMP_CHUNK_BYTES - 1) /
1360 : : CI_FLOW_DUMP_CHUNK_BYTES;
1361 : : char title[64];
1362 : : size_t ci;
1363 : :
1364 : : fprintf(file, "FLOW DUMP: driver=%s engine=%s\n", driver, engine);
1365 : : fprintf(file, "FLOW DUMP: DATA size=%zu chunks=%zu chunk_bytes=%d\n",
1366 : : data_len, nchunks, CI_FLOW_DUMP_CHUNK_BYTES);
1367 : :
1368 [ # # ]: 0 : for (ci = 0; ci < nchunks; ci++) {
1369 : 0 : const size_t off = ci * CI_FLOW_DUMP_CHUNK_BYTES;
1370 : : const size_t clen =
1371 : 0 : RTE_MIN((size_t)CI_FLOW_DUMP_CHUNK_BYTES,
1372 : : data_len - off);
1373 : 0 : snprintf(title, sizeof(title), "FLOW DUMP: chunk %03zu/%03zu",
1374 : : ci + 1, nchunks);
1375 : 0 : rte_memdump(file, title, raw + off, clen);
1376 : : }
1377 : 0 : }
1378 : :
1379 : : /* rte_flow_dev_dump implementation */
1380 : : static inline int
1381 : 0 : ci_flow_dump(struct ci_flow_engine_conf *engine_conf,
1382 : : struct rte_flow *flow,
1383 : : FILE *file,
1384 : : struct rte_flow_error *error)
1385 : : {
1386 : : struct ci_flow_engine_ref engine_ref;
1387 : : struct ci_flow *cur;
1388 : 0 : struct rte_flow_error local_error = {0};
1389 : : bool found = false;
1390 : : const char *driver_name =
1391 : : #ifdef RTE_COMPONENT_NAME
1392 : : RTE_STR(RTE_COMPONENT_NAME);
1393 : : #else
1394 : : "unknown";
1395 : : #endif
1396 : :
1397 [ # # ]: 0 : if (error == NULL)
1398 : : error = &local_error;
1399 : :
1400 : 0 : rte_rwlock_read_lock(&engine_conf->config_lock);
1401 : :
1402 [ # # ]: 0 : TAILQ_FOREACH(cur, &engine_conf->flows, node) {
1403 : : const void *data;
1404 : : size_t data_len;
1405 : :
1406 [ # # ]: 0 : if (flow != NULL && (struct rte_flow *)cur != flow)
1407 : 0 : continue;
1408 : :
1409 : 0 : if (!ci_flow_is_valid(cur, engine_conf)) {
1410 : 0 : CI_DRV_LOG(DEBUG, "invalid flow handle: %p, skipping", cur);
1411 : 0 : continue;
1412 : : }
1413 : :
1414 : : found = true;
1415 : :
1416 : : engine_ref = ci_flow_engine_ref_from_idx(engine_conf, cur->engine_idx);
1417 : :
1418 : 0 : data = RTE_PTR_ADD(cur, sizeof(struct ci_flow));
1419 : 0 : data_len = engine_ref.engine->flow_size - sizeof(struct ci_flow);
1420 : :
1421 : : /* skipping explicitly keeps static analyzers quiet */
1422 [ # # ]: 0 : if (data_len == 0) {
1423 : 0 : CI_DRV_LOG(DEBUG, "flow data length is 0, skipping dump");
1424 : 0 : continue;
1425 : : }
1426 : :
1427 : 0 : ci_flow_dump_one(file, driver_name, engine_ref.engine->name,
1428 : : data, data_len);
1429 : : }
1430 : :
1431 : : rte_rwlock_read_unlock(&engine_conf->config_lock);
1432 : :
1433 [ # # ]: 0 : if (flow != NULL && !found) {
1434 : 0 : return rte_flow_error_set(error, ENOENT,
1435 : : RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1436 : : "Flow not found");
1437 : : }
1438 : :
1439 : : return 0;
1440 : : }
1441 : :
1442 : : #endif /* _COMMON_INTEL_FLOW_ENGINE_H_ */
|