Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2025 NVIDIA Corporation & Affiliates
3 : : */
4 : :
5 : : #include <rte_flow.h>
6 : : #include "mlx5_malloc.h"
7 : : #include "mlx5.h"
8 : : #include "mlx5_defs.h"
9 : : #include "mlx5_flow.h"
10 : : #include "mlx5_rx.h"
11 : :
12 : : SLIST_HEAD(mlx5_flow_head, rte_flow_hw);
13 : :
14 : : struct mlx5_nta_sample_ctx {
15 : : uint32_t groups_num;
16 : : struct mlx5_indexed_pool *group_ids;
17 : : struct mlx5_list *mirror_actions; /* cache FW mirror actions */
18 : : struct mlx5_list *mirror_groups; /* cache groups for sample and suffix actions */
19 : : };
20 : :
21 : : static void
22 : : release_chained_flows(struct rte_eth_dev *dev, struct mlx5_flow_head *flow_head,
23 : : enum mlx5_flow_type type)
24 : : {
25 : : struct rte_flow_hw *flow = SLIST_FIRST(flow_head);
26 : :
27 : : if (flow) {
28 : 0 : flow->nt2hws->chaned_flow = 0;
29 : 0 : flow_hw_list_destroy(dev, type, (uintptr_t)flow);
30 : : }
31 : 0 : }
32 : :
33 : : static void
34 : : release_cached_group(struct rte_eth_dev *dev, uint32_t group)
35 : : {
36 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
37 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
38 : :
39 : 0 : mlx5_ipool_free(ctx->group_ids, group - MLX5_FLOW_TABLE_SAMPLE_BASE);
40 : 0 : }
41 : :
42 : : static uint32_t
43 : 0 : alloc_cached_group(struct rte_eth_dev *dev)
44 : : {
45 : : void *obj;
46 : 0 : uint32_t idx = 0;
47 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
48 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
49 : :
50 : 0 : obj = mlx5_ipool_malloc(ctx->group_ids, &idx);
51 [ # # ]: 0 : if (obj == NULL)
52 : : return 0;
53 : 0 : return idx + MLX5_FLOW_TABLE_SAMPLE_BASE;
54 : : }
55 : :
56 : : void
57 : 0 : mlx5_nta_sample_context_free(struct rte_eth_dev *dev)
58 : : {
59 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
60 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
61 : :
62 [ # # ]: 0 : if (ctx == NULL)
63 : : return;
64 [ # # ]: 0 : if (ctx->mirror_groups != NULL)
65 : 0 : mlx5_list_destroy(ctx->mirror_groups);
66 [ # # ]: 0 : if (ctx->group_ids != NULL)
67 : 0 : mlx5_ipool_destroy(ctx->group_ids);
68 [ # # ]: 0 : if (ctx->mirror_actions != NULL)
69 : 0 : mlx5_list_destroy(ctx->mirror_actions);
70 : 0 : mlx5_free(ctx);
71 : 0 : priv->nta_sample_ctx = NULL;
72 : : }
73 : :
74 : : struct mlx5_nta_sample_cached_group {
75 : : const struct rte_flow_action *actions;
76 : : size_t actions_size;
77 : : uint32_t group;
78 : : struct mlx5_list_entry entry;
79 : : };
80 : :
81 : : struct mlx5_nta_sample_cached_group_ctx {
82 : : struct rte_flow_action *actions;
83 : : size_t actions_size;
84 : : };
85 : :
86 : : struct mlx5_nta_sample_cached_mirror {
87 : : struct mlx5_flow_template_table_cfg table_cfg;
88 : : struct mlx5_list_entry *sample;
89 : : struct mlx5_list_entry *suffix;
90 : : struct mlx5_mirror *mirror;
91 : : struct mlx5_list_entry entry;
92 : : };
93 : :
94 : : struct mlx5_nta_sample_cached_mirror_ctx {
95 : : struct mlx5_flow_template_table_cfg *table_cfg;
96 : : struct mlx5_list_entry *sample;
97 : : struct mlx5_list_entry *suffix;
98 : : };
99 : :
100 : : static struct mlx5_mirror *
101 : : mirror_entry_to_mirror_action(struct mlx5_list_entry *entry)
102 : : {
103 : 0 : return container_of(entry, struct mlx5_nta_sample_cached_mirror, entry)->mirror;
104 : : }
105 : :
106 : : static uint32_t
107 : : mirror_entry_to_mirror_sample_group(struct mlx5_list_entry *entry)
108 : : {
109 : 0 : struct mlx5_list_entry *sample = container_of(entry,
110 : : struct mlx5_nta_sample_cached_mirror,
111 : : entry)->sample;
112 : :
113 : 0 : return container_of(sample, struct mlx5_nta_sample_cached_group, entry)->group;
114 : : }
115 : :
116 : : static uint32_t
117 : : mirror_entry_to_mirror_suffix_group(struct mlx5_list_entry *entry)
118 : : {
119 : 0 : struct mlx5_list_entry *suffix = container_of(entry,
120 : : struct mlx5_nta_sample_cached_mirror,
121 : : entry)->suffix;
122 : :
123 : 0 : return container_of(suffix, struct mlx5_nta_sample_cached_group, entry)->group;
124 : : }
125 : :
126 : : static struct mlx5_list_entry *
127 : 0 : mlx5_nta_sample_cached_mirror_create(void *cache_ctx, void *cb_ctx)
128 : : {
129 : : struct rte_eth_dev *dev = cache_ctx;
130 : : struct mlx5_nta_sample_cached_mirror_ctx *ctx = cb_ctx;
131 : 0 : struct rte_flow_action_jump sample_jump_conf = {
132 : 0 : .group = container_of(ctx->sample,
133 : 0 : struct mlx5_nta_sample_cached_group, entry)->group
134 : : };
135 : 0 : struct rte_flow_action_jump suffix_jump_conf = {
136 : 0 : .group = container_of(ctx->suffix,
137 : 0 : struct mlx5_nta_sample_cached_group, entry)->group
138 : : };
139 : 0 : struct rte_flow_action mirror_sample_actions[2] = {
140 : : [0] = {
141 : : .type = RTE_FLOW_ACTION_TYPE_JUMP,
142 : : .conf = &sample_jump_conf,
143 : : },
144 : : [1] = {
145 : : .type = RTE_FLOW_ACTION_TYPE_END
146 : : }
147 : : };
148 : 0 : struct rte_flow_action_sample mirror_conf = {
149 : : .ratio = 1,
150 : : .actions = mirror_sample_actions,
151 : : };
152 : 0 : struct rte_flow_action mirror_actions[3] = {
153 : : [0] = {
154 : : .type = RTE_FLOW_ACTION_TYPE_SAMPLE,
155 : : .conf = &mirror_conf,
156 : : },
157 : : [1] = {
158 : : .type = RTE_FLOW_ACTION_TYPE_JUMP,
159 : : .conf = &suffix_jump_conf,
160 : : },
161 : : [2] = {
162 : : .type = RTE_FLOW_ACTION_TYPE_END
163 : : }
164 : : };
165 : 0 : struct mlx5_nta_sample_cached_mirror *obj = mlx5_malloc(MLX5_MEM_ANY,
166 : : sizeof(*obj), 0,
167 : : SOCKET_ID_ANY);
168 [ # # ]: 0 : if (obj == NULL)
169 : : return NULL;
170 : 0 : obj->mirror = mlx5_hw_create_mirror(dev, ctx->table_cfg, mirror_actions, NULL);
171 [ # # ]: 0 : if (obj->mirror == NULL) {
172 : 0 : mlx5_free(obj);
173 : 0 : return NULL;
174 : : }
175 : 0 : obj->sample = ctx->sample;
176 : 0 : obj->suffix = ctx->suffix;
177 : 0 : obj->table_cfg = *ctx->table_cfg;
178 : 0 : return &obj->entry;
179 : : }
180 : :
181 : : static struct mlx5_list_entry *
182 : 0 : mlx5_nta_sample_cached_mirror_clone(void *tool_ctx __rte_unused,
183 : : struct mlx5_list_entry *entry,
184 : : void *cb_ctx __rte_unused)
185 : : {
186 : : struct mlx5_nta_sample_cached_mirror *cached_obj =
187 : 0 : container_of(entry, struct mlx5_nta_sample_cached_mirror, entry);
188 : 0 : struct mlx5_nta_sample_cached_mirror *new_obj = mlx5_malloc(MLX5_MEM_ANY,
189 : : sizeof(*new_obj), 0,
190 : : SOCKET_ID_ANY);
191 : :
192 [ # # ]: 0 : if (new_obj == NULL)
193 : : return NULL;
194 : : memcpy(new_obj, cached_obj, sizeof(*new_obj));
195 : 0 : return &new_obj->entry;
196 : : }
197 : :
198 : : static int
199 : 0 : mlx5_nta_sample_cached_mirror_match(void *cache_ctx __rte_unused,
200 : : struct mlx5_list_entry *entry, void *cb_ctx)
201 : : {
202 : : bool match;
203 : : struct mlx5_nta_sample_cached_mirror_ctx *ctx = cb_ctx;
204 : : struct mlx5_nta_sample_cached_mirror *obj =
205 : 0 : container_of(entry, struct mlx5_nta_sample_cached_mirror, entry);
206 : :
207 : 0 : match = obj->sample == ctx->sample &&
208 [ # # # # ]: 0 : obj->suffix == ctx->suffix &&
209 [ # # ]: 0 : memcmp(&obj->table_cfg, ctx->table_cfg, sizeof(obj->table_cfg)) == 0;
210 : :
211 [ # # ]: 0 : return match ? 0 : ~0;
212 : : }
213 : :
214 : : static void
215 : 0 : mlx5_nta_sample_cached_mirror_remove(void *cache_ctx, struct mlx5_list_entry *entry)
216 : : {
217 : : struct rte_eth_dev *dev = cache_ctx;
218 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
219 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
220 : :
221 : : struct mlx5_nta_sample_cached_mirror *obj =
222 : 0 : container_of(entry, struct mlx5_nta_sample_cached_mirror, entry);
223 : 0 : mlx5_hw_mirror_destroy(dev, obj->mirror);
224 : :
225 : 0 : mlx5_list_unregister(ctx->mirror_groups, obj->sample);
226 : 0 : mlx5_list_unregister(ctx->mirror_groups, obj->suffix);
227 : 0 : mlx5_free(obj);
228 : 0 : }
229 : :
230 : : static void
231 : 0 : mlx5_nta_sample_cached_mirror_free_cloned(void *cache_ctx __rte_unused,
232 : : struct mlx5_list_entry *entry)
233 : : {
234 : : struct mlx5_nta_sample_cached_mirror *cloned_obj =
235 : 0 : container_of(entry, struct mlx5_nta_sample_cached_mirror, entry);
236 : :
237 : 0 : mlx5_free(cloned_obj);
238 : 0 : }
239 : :
240 : : static int
241 : 0 : serialize_actions(struct mlx5_nta_sample_cached_group_ctx *obj_ctx)
242 : : {
243 [ # # ]: 0 : if (obj_ctx->actions_size == 0) {
244 : : uint8_t *tgt_buffer;
245 : 0 : int size = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, NULL, 0, obj_ctx->actions, NULL);
246 [ # # ]: 0 : if (size < 0)
247 : : return size;
248 : 0 : tgt_buffer = mlx5_malloc(MLX5_MEM_ANY, size, 0, SOCKET_ID_ANY);
249 [ # # ]: 0 : if (tgt_buffer == NULL)
250 : : return -ENOMEM;
251 : 0 : obj_ctx->actions_size = size;
252 : 0 : size = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, tgt_buffer, size,
253 : 0 : obj_ctx->actions, NULL);
254 [ # # ]: 0 : if (size < 0) {
255 : 0 : mlx5_free(tgt_buffer);
256 : 0 : return size;
257 : : }
258 : 0 : obj_ctx->actions = (struct rte_flow_action *)tgt_buffer;
259 : : }
260 : 0 : return obj_ctx->actions_size;
261 : : }
262 : :
263 : : static struct mlx5_list_entry *
264 : 0 : mlx5_nta_sample_cached_group_create(void *cache_ctx, void *cb_ctx)
265 : : {
266 : : struct rte_eth_dev *dev = cache_ctx;
267 : : struct mlx5_nta_sample_cached_group_ctx *obj_ctx = cb_ctx;
268 : : struct mlx5_nta_sample_cached_group *obj;
269 : 0 : int actions_size = serialize_actions(obj_ctx);
270 : :
271 [ # # ]: 0 : if (actions_size < 0)
272 : : return NULL;
273 : 0 : obj = mlx5_malloc(MLX5_MEM_ANY, sizeof(*obj), 0, SOCKET_ID_ANY);
274 [ # # ]: 0 : if (obj == NULL)
275 : : return NULL;
276 : 0 : obj->group = alloc_cached_group(dev);
277 [ # # ]: 0 : if (obj->group == 0) {
278 : 0 : mlx5_free(obj);
279 : 0 : return NULL;
280 : : }
281 : 0 : obj->actions = obj_ctx->actions;
282 : 0 : obj->actions_size = obj_ctx->actions_size;
283 : 0 : return &obj->entry;
284 : : }
285 : :
286 : : static int
287 : 0 : mlx5_nta_sample_cached_group_match(void *cache_ctx __rte_unused,
288 : : struct mlx5_list_entry *entry, void *cb_ctx)
289 : : {
290 : : struct mlx5_nta_sample_cached_group_ctx *obj_ctx = cb_ctx;
291 : 0 : int actions_size = serialize_actions(obj_ctx);
292 : : struct mlx5_nta_sample_cached_group *cached_obj =
293 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
294 [ # # ]: 0 : if (actions_size < 0)
295 : : return ~0;
296 : 0 : return memcmp(cached_obj->actions, obj_ctx->actions, actions_size);
297 : : }
298 : :
299 : : static void
300 : 0 : mlx5_nta_sample_cached_group_remove(void *cache_ctx, struct mlx5_list_entry *entry)
301 : : {
302 : : struct rte_eth_dev *dev = cache_ctx;
303 : : struct mlx5_nta_sample_cached_group *cached_obj =
304 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
305 : :
306 : 0 : release_cached_group(dev, cached_obj->group);
307 : 0 : mlx5_free((void *)(uintptr_t)cached_obj->actions);
308 : 0 : mlx5_free(cached_obj);
309 : 0 : }
310 : :
311 : : static struct mlx5_list_entry *
312 : 0 : mlx5_nta_sample_cached_group_clone(void *tool_ctx __rte_unused,
313 : : struct mlx5_list_entry *entry,
314 : : void *cb_ctx __rte_unused)
315 : : {
316 : : struct mlx5_nta_sample_cached_group *cached_obj =
317 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
318 : : struct mlx5_nta_sample_cached_group *new_obj;
319 : :
320 : 0 : new_obj = mlx5_malloc(MLX5_MEM_ANY, sizeof(*new_obj), 0, SOCKET_ID_ANY);
321 [ # # ]: 0 : if (new_obj == NULL)
322 : : return NULL;
323 : : memcpy(new_obj, cached_obj, sizeof(*new_obj));
324 : 0 : return &new_obj->entry;
325 : : }
326 : :
327 : : static void
328 : 0 : mlx5_nta_sample_cached_group_free_cloned(void *cache_ctx __rte_unused,
329 : : struct mlx5_list_entry *entry)
330 : : {
331 : : struct mlx5_nta_sample_cached_group *cloned_obj =
332 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
333 : :
334 : 0 : mlx5_free(cloned_obj);
335 : 0 : }
336 : :
337 : : static int
338 : 0 : mlx5_init_nta_sample_context(struct rte_eth_dev *dev)
339 : : {
340 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
341 : 0 : struct mlx5_indexed_pool_config ipool_cfg = {
342 : : .size = 0,
343 : : .trunk_size = 32,
344 : : .grow_trunk = 5,
345 : : .grow_shift = 1,
346 : : .need_lock = 1,
347 : 0 : .release_mem_en = !!priv->sh->config.reclaim_mode,
348 : : .max_idx = MLX5_FLOW_TABLE_SAMPLE_NUM,
349 : : .type = "mlx5_nta_sample"
350 : : };
351 : 0 : struct mlx5_nta_sample_ctx *ctx = mlx5_malloc(MLX5_MEM_ZERO,
352 : : sizeof(*ctx), 0, SOCKET_ID_ANY);
353 : :
354 [ # # ]: 0 : if (ctx == NULL)
355 : : return -ENOMEM;
356 : 0 : priv->nta_sample_ctx = ctx;
357 : 0 : ctx->group_ids = mlx5_ipool_create(&ipool_cfg);
358 [ # # ]: 0 : if (ctx->group_ids == NULL)
359 : 0 : goto error;
360 : 0 : ctx->mirror_groups = mlx5_list_create("nta sample groups", dev, true,
361 : : mlx5_nta_sample_cached_group_create,
362 : : mlx5_nta_sample_cached_group_match,
363 : : mlx5_nta_sample_cached_group_remove,
364 : : mlx5_nta_sample_cached_group_clone,
365 : : mlx5_nta_sample_cached_group_free_cloned);
366 [ # # ]: 0 : if (ctx->mirror_groups == NULL)
367 : 0 : goto error;
368 : 0 : ctx->mirror_actions = mlx5_list_create("nta sample mirror actions", dev, true,
369 : : mlx5_nta_sample_cached_mirror_create,
370 : : mlx5_nta_sample_cached_mirror_match,
371 : : mlx5_nta_sample_cached_mirror_remove,
372 : : mlx5_nta_sample_cached_mirror_clone,
373 : : mlx5_nta_sample_cached_mirror_free_cloned);
374 [ # # ]: 0 : if (ctx->mirror_actions == NULL)
375 : 0 : goto error;
376 : : return 0;
377 : :
378 : 0 : error:
379 : 0 : mlx5_nta_sample_context_free(dev);
380 : 0 : return -ENOMEM;
381 : : }
382 : :
383 : : static struct mlx5_list_entry *
384 : : register_mirror(struct mlx5_flow_template_table_cfg *table_cfg,
385 : : struct mlx5_list *cache,
386 : : struct mlx5_list_entry *sample,
387 : : struct mlx5_list_entry *suffix)
388 : : {
389 : 0 : struct mlx5_nta_sample_cached_mirror_ctx ctx = {
390 : : .table_cfg = table_cfg,
391 : : .sample = sample,
392 : : .suffix = suffix
393 : : };
394 : :
395 : 0 : return mlx5_list_register(cache, &ctx);
396 : : }
397 : :
398 : : static struct mlx5_list_entry *
399 : : register_mirror_actions(struct rte_flow_action *actions, struct mlx5_list *cache)
400 : : {
401 : 0 : struct mlx5_nta_sample_cached_group_ctx ctx = {
402 : : .actions = actions
403 : : };
404 : 0 : return mlx5_list_register(cache, &ctx);
405 : : }
406 : :
407 : : void
408 : 0 : mlx5_nta_sample_mirror_entry_release(struct rte_eth_dev *dev,
409 : : struct mlx5_sample_release_ctx *release)
410 : : {
411 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
412 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
413 : :
414 : 0 : mlx5_list_unregister(ctx->mirror_actions, release->mirror_entry);
415 : 0 : release_cached_group(dev, release->sample_group);
416 : 0 : mlx5_free(release);
417 : 0 : }
418 : :
419 : : static struct mlx5_list_entry *
420 : 0 : mlx5_create_nta_mirror(struct rte_eth_dev *dev,
421 : : const struct rte_flow_attr *attr,
422 : : struct rte_flow_action *sample_actions,
423 : : struct rte_flow_action *suffix_actions,
424 : : struct rte_flow_error *error)
425 : : {
426 : : struct mlx5_list_entry *entry;
427 : : struct mlx5_list_entry *mirror_sample = NULL, *mirror_suffix = NULL;
428 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
429 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
430 : 0 : struct mlx5_flow_template_table_cfg table_cfg = {
431 : : .external = true,
432 : : .attr = {
433 : : .flow_attr = *attr
434 : : }
435 : : };
436 : :
437 : 0 : mirror_sample = register_mirror_actions(sample_actions, ctx->mirror_groups);
438 [ # # ]: 0 : if (mirror_sample == NULL) {
439 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
440 : : NULL, "Failed to register sample group");
441 : 0 : goto error;
442 : : }
443 : 0 : mirror_suffix = register_mirror_actions(suffix_actions, ctx->mirror_groups);
444 [ # # ]: 0 : if (mirror_suffix == NULL) {
445 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
446 : : NULL, "Failed to register suffix group");
447 : 0 : goto error;
448 : : }
449 : 0 : entry = register_mirror(&table_cfg, ctx->mirror_actions, mirror_sample, mirror_suffix);
450 [ # # ]: 0 : if (entry == NULL) {
451 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
452 : : NULL, "Failed to register HWS mirror action");
453 : 0 : goto error;
454 : : }
455 : :
456 : : return entry;
457 : :
458 : 0 : error:
459 [ # # ]: 0 : if (mirror_sample)
460 : 0 : mlx5_list_unregister(ctx->mirror_groups, mirror_sample);
461 [ # # ]: 0 : if (mirror_suffix)
462 : 0 : mlx5_list_unregister(ctx->mirror_groups, mirror_suffix);
463 : : return NULL;
464 : : }
465 : :
466 : : static uint32_t
467 : 0 : generate_random_mask(uint32_t ratio)
468 : : {
469 : : uint32_t i;
470 [ # # ]: 0 : double goal = 1.0 / ratio;
471 : :
472 : : /* Check if the ratio value is power of 2 */
473 [ # # ]: 0 : if (rte_popcount32(ratio) == 1) {
474 [ # # ]: 0 : for (i = 2; i < 32; i++) {
475 [ # # ]: 0 : if (RTE_BIT32(i) == ratio)
476 : 0 : return RTE_BIT32(i) - 1;
477 : : }
478 : : }
479 : :
480 : : /*
481 : : * Find the last power of 2 with ratio larger then the goal.
482 : : */
483 [ # # ]: 0 : for (i = 2; i < 32; i++) {
484 : 0 : double res = 1.0 / RTE_BIT32(i);
485 : :
486 [ # # ]: 0 : if (res < goal)
487 : 0 : return RTE_BIT32(i - 1) - 1;
488 : : }
489 : :
490 : : return UINT32_MAX;
491 : : }
492 : :
493 : : static void
494 : 0 : mlx5_nta_parse_sample_actions(const struct rte_flow_action *action,
495 : : const struct rte_flow_action **sample_action,
496 : : struct rte_flow_action *prefix_actions,
497 : : struct rte_flow_action *suffix_actions)
498 : : {
499 : : struct rte_flow_action *pa = prefix_actions;
500 : : struct rte_flow_action *sa = suffix_actions;
501 : :
502 : 0 : *sample_action = NULL;
503 : : do {
504 [ # # ]: 0 : if (action->type == RTE_FLOW_ACTION_TYPE_SAMPLE) {
505 : 0 : *sample_action = action;
506 [ # # ]: 0 : } else if (*sample_action == NULL) {
507 [ # # ]: 0 : if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
508 : 0 : continue;
509 : 0 : *(pa++) = *action;
510 : : } else {
511 [ # # ]: 0 : if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
512 : 0 : continue;
513 : 0 : *(sa++) = *action;
514 : : }
515 [ # # ]: 0 : } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
516 : 0 : }
517 : :
518 : : static bool
519 : : validate_prefix_actions(const struct rte_flow_action *actions)
520 : : {
521 : : uint32_t i = 0;
522 : :
523 [ # # ]: 0 : while (actions[i].type != RTE_FLOW_ACTION_TYPE_END)
524 : 0 : i++;
525 : : return i < MLX5_HW_MAX_ACTS - 1;
526 : : }
527 : :
528 : : static void
529 : : action_append(struct rte_flow_action *actions, const struct rte_flow_action *last)
530 : : {
531 : : uint32_t i = 0;
532 : :
533 [ # # # # ]: 0 : while (actions[i].type != RTE_FLOW_ACTION_TYPE_END)
534 : 0 : i++;
535 : 0 : actions[i] = *last;
536 : : }
537 : :
538 : : static int
539 : 0 : create_mirror_aux_flows(struct rte_eth_dev *dev,
540 : : enum mlx5_flow_type type,
541 : : const struct rte_flow_attr *attr,
542 : : struct rte_flow_action *suffix_actions,
543 : : struct rte_flow_action *sample_actions,
544 : : struct mlx5_list_entry *mirror_entry,
545 : : struct mlx5_flow_head *flow_head,
546 : : struct rte_flow_error *error)
547 : : {
548 : 0 : const struct rte_flow_attr suffix_attr = {
549 : 0 : .ingress = attr->ingress,
550 : 0 : .egress = attr->egress,
551 : 0 : .transfer = attr->transfer,
552 : : .group = mirror_entry_to_mirror_suffix_group(mirror_entry),
553 : : };
554 : 0 : const struct rte_flow_attr sample_attr = {
555 : : .ingress = attr->ingress,
556 : : .egress = attr->egress,
557 : : .transfer = attr->transfer,
558 : : .group = mirror_entry_to_mirror_sample_group(mirror_entry),
559 : : };
560 : 0 : const struct rte_flow_item secondary_pattern[1] = {
561 : : [0] = { .type = RTE_FLOW_ITEM_TYPE_END }
562 : : };
563 : : int ret, encap_idx, actions_num;
564 : : uint64_t suffix_action_flags, sample_action_flags;
565 : 0 : const struct rte_flow_action *qrss_action = NULL, *mark_action = NULL;
566 : 0 : struct rte_flow_hw *suffix_flow = NULL, *sample_flow = NULL;
567 : :
568 : 0 : suffix_action_flags = mlx5_flow_hw_action_flags_get(suffix_actions,
569 : : &qrss_action, &mark_action,
570 : : &encap_idx, &actions_num, error);
571 [ # # # # ]: 0 : if (qrss_action != NULL && qrss_action->type == RTE_FLOW_ACTION_TYPE_RSS)
572 : 0 : return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION, NULL,
573 : : "RSS action is not supported in suffix sample action");
574 : 0 : sample_action_flags = mlx5_flow_hw_action_flags_get(sample_actions,
575 : : &qrss_action, &mark_action,
576 : : &encap_idx, &actions_num, error);
577 [ # # # # ]: 0 : if (qrss_action != NULL && qrss_action->type == RTE_FLOW_ACTION_TYPE_RSS)
578 : 0 : return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION, NULL,
579 : : "RSS action is not supported in sample action");
580 : 0 : ret = flow_hw_create_flow(dev, type, &suffix_attr,
581 : : secondary_pattern, suffix_actions,
582 : : MLX5_FLOW_LAYER_OUTER_L2, suffix_action_flags,
583 : : true, &suffix_flow, error);
584 [ # # ]: 0 : if (ret != 0)
585 : : return ret;
586 : 0 : ret = flow_hw_create_flow(dev, type, &sample_attr,
587 : : secondary_pattern, sample_actions,
588 : : MLX5_FLOW_LAYER_OUTER_L2, sample_action_flags,
589 : : true, &sample_flow, error);
590 [ # # ]: 0 : if (ret != 0) {
591 : 0 : flow_hw_destroy(dev, suffix_flow);
592 : 0 : return ret;
593 : : }
594 : 0 : suffix_flow->nt2hws->chaned_flow = 1;
595 : 0 : SLIST_INSERT_HEAD(flow_head, suffix_flow, nt2hws->next);
596 : 0 : sample_flow->nt2hws->chaned_flow = 1;
597 : 0 : SLIST_INSERT_HEAD(flow_head, sample_flow, nt2hws->next);
598 : 0 : return 0;
599 : : }
600 : :
601 : : static struct rte_flow_hw *
602 : 0 : create_sample_flow(struct rte_eth_dev *dev,
603 : : enum mlx5_flow_type type,
604 : : const struct rte_flow_attr *attr,
605 : : uint32_t ratio,
606 : : uint32_t sample_group,
607 : : struct mlx5_list_entry *mirror_entry,
608 : : struct rte_flow_error *error)
609 : : {
610 : 0 : struct rte_flow_hw *sample_flow = NULL;
611 : 0 : uint32_t random_mask = generate_random_mask(ratio);
612 : 0 : const struct rte_flow_attr sample_attr = {
613 : 0 : .ingress = attr->ingress,
614 : 0 : .egress = attr->egress,
615 : 0 : .transfer = attr->transfer,
616 : : .group = sample_group,
617 : : };
618 : 0 : const struct rte_flow_item sample_pattern[2] = {
619 : : [0] = {
620 : : .type = RTE_FLOW_ITEM_TYPE_RANDOM,
621 : 0 : .mask = &(struct rte_flow_item_random) {
622 : : .value = random_mask
623 : : },
624 : 0 : .spec = &(struct rte_flow_item_random) {
625 : : .value = 1
626 : : },
627 : : },
628 : : [1] = { .type = RTE_FLOW_ITEM_TYPE_END }
629 : : };
630 : 0 : const struct rte_flow_action sample_actions[2] = {
631 : : [0] = {
632 : : .type = (enum rte_flow_action_type)MLX5_RTE_FLOW_ACTION_TYPE_MIRROR,
633 : : .conf = mirror_entry_to_mirror_action(mirror_entry)
634 : : },
635 : : [1] = { .type = RTE_FLOW_ACTION_TYPE_END }
636 : : };
637 : :
638 [ # # ]: 0 : if (random_mask > UINT16_MAX)
639 : : return NULL;
640 : 0 : flow_hw_create_flow(dev, type, &sample_attr, sample_pattern, sample_actions,
641 : : 0, 0, true, &sample_flow, error);
642 : 0 : return sample_flow;
643 : : }
644 : :
645 : : static int
646 : 0 : create_random_miss_actions(struct rte_eth_dev *dev,
647 : : const struct rte_flow_attr *attr,
648 : : uint32_t sample_group,
649 : : struct mlx5_list_entry *mirror_entry,
650 : : struct rte_flow_error *error)
651 : : {
652 : 0 : const struct rte_flow_group_attr sample_group_attr = {
653 : 0 : .ingress = attr->ingress,
654 : 0 : .egress = attr->egress,
655 : 0 : .transfer = attr->transfer,
656 : : };
657 : 0 : const struct rte_flow_action sample_miss_actions[2] = {
658 : : [0] = {
659 : : .type = RTE_FLOW_ACTION_TYPE_JUMP,
660 : 0 : .conf = &(struct rte_flow_action_jump) {
661 : : .group = mirror_entry_to_mirror_suffix_group(mirror_entry)
662 : : }
663 : : },
664 : : [1] = { .type = RTE_FLOW_ACTION_TYPE_END }
665 : : };
666 : :
667 : 0 : return mlx5_flow_hw_group_set_miss_actions(dev, sample_group, &sample_group_attr,
668 : : sample_miss_actions, error);
669 : : }
670 : :
671 : : static void
672 : : save_sample_release_cxt(struct mlx5_sample_release_ctx *release,
673 : : struct mlx5_flow_head *flow_head,
674 : : struct mlx5_list_entry *mirror_entry,
675 : : uint32_t sample_group)
676 : : {
677 : : struct rte_flow_hw *var;
678 : :
679 : 0 : release->mirror_entry = mirror_entry;
680 : 0 : release->sample_group = sample_group;
681 : :
682 [ # # # # ]: 0 : SLIST_FOREACH(var, flow_head, nt2hws->next) {
683 [ # # # # ]: 0 : if (SLIST_NEXT(var, nt2hws->next) == NULL)
684 : 0 : var->nt2hws->sample_release_ctx = release;
685 : : }
686 : : }
687 : :
688 : : static struct rte_flow_hw *
689 : 0 : mlx5_nta_create_sample_flow(struct rte_eth_dev *dev,
690 : : enum mlx5_flow_type type,
691 : : const struct rte_flow_attr *attr,
692 : : uint32_t sample_ratio,
693 : : uint64_t item_flags, uint64_t action_flags,
694 : : const struct rte_flow_item *pattern,
695 : : struct rte_flow_action *prefix_actions,
696 : : struct rte_flow_action *suffix_actions,
697 : : struct rte_flow_action *sample_actions,
698 : : struct mlx5_list_entry *mirror_entry,
699 : : struct mlx5_flow_head *flow_head,
700 : : struct rte_flow_error *error)
701 : : {
702 : : int ret;
703 : 0 : uint32_t sample_group = alloc_cached_group(dev);
704 : 0 : struct rte_flow_hw *base_flow = NULL, *sample_flow = NULL;
705 : : struct mlx5_sample_release_ctx *release = NULL;
706 : :
707 [ # # ]: 0 : if (sample_group == 0)
708 : : return NULL;
709 : 0 : release = mlx5_malloc(MLX5_MEM_ANY, sizeof(*release), 0, SOCKET_ID_ANY);
710 [ # # ]: 0 : if (release == NULL) {
711 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ACTION,
712 : : NULL, "Failed to allocate release context");
713 : 0 : goto error;
714 : : }
715 : 0 : ret = create_mirror_aux_flows(dev, type, attr,
716 : : suffix_actions, sample_actions,
717 : : mirror_entry, flow_head, error);
718 [ # # ]: 0 : if (ret != 0)
719 : 0 : goto error;
720 : 0 : ret = create_random_miss_actions(dev, attr, sample_group,
721 : : mirror_entry, error);
722 [ # # ]: 0 : if (ret != 0)
723 : 0 : goto error;
724 : 0 : sample_flow = create_sample_flow(dev, type, attr, sample_ratio, sample_group,
725 : : mirror_entry, error);
726 [ # # ]: 0 : if (sample_flow == NULL)
727 : 0 : goto error;
728 : 0 : sample_flow->nt2hws->chaned_flow = 1;
729 : 0 : SLIST_INSERT_HEAD(flow_head, sample_flow, nt2hws->next);
730 : : action_append(prefix_actions,
731 : : &(struct rte_flow_action) {
732 : : .type = RTE_FLOW_ACTION_TYPE_JUMP,
733 : 0 : .conf = &(struct rte_flow_action_jump) { .group = sample_group }
734 : : });
735 : 0 : ret = flow_hw_create_flow(dev, type, attr, pattern, prefix_actions,
736 : : item_flags, action_flags, true, &base_flow, error);
737 [ # # ]: 0 : if (ret != 0)
738 : 0 : goto error;
739 : 0 : SLIST_INSERT_HEAD(flow_head, base_flow, nt2hws->next);
740 : : save_sample_release_cxt(release, flow_head, mirror_entry, sample_group);
741 : : return base_flow;
742 : :
743 : 0 : error:
744 [ # # ]: 0 : if (!SLIST_EMPTY(flow_head))
745 : : release_chained_flows(dev, flow_head, type);
746 [ # # ]: 0 : if (release)
747 : 0 : mlx5_free(release);
748 [ # # ]: 0 : if (sample_flow)
749 : : release_cached_group(dev, sample_group);
750 : : return NULL;
751 : : }
752 : :
753 : : static struct rte_flow_hw *
754 : 0 : mlx5_nta_create_mirror_flow(struct rte_eth_dev *dev,
755 : : enum mlx5_flow_type type,
756 : : const struct rte_flow_attr *attr,
757 : : uint64_t item_flags, uint64_t action_flags,
758 : : const struct rte_flow_item *pattern,
759 : : struct rte_flow_action *prefix_actions,
760 : : struct rte_flow_action *suffix_actions,
761 : : struct rte_flow_action *sample_actions,
762 : : struct mlx5_list_entry *mirror_entry,
763 : : struct mlx5_flow_head *flow_head,
764 : : struct rte_flow_error *error)
765 : : {
766 : : int ret;
767 : 0 : struct rte_flow_hw *base_flow = NULL;
768 : 0 : struct mlx5_sample_release_ctx *release = mlx5_malloc(MLX5_MEM_ANY, sizeof(*release),
769 : : 0, SOCKET_ID_ANY);
770 : :
771 [ # # ]: 0 : if (release == NULL) {
772 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ACTION,
773 : : NULL, "Failed to allocate release context");
774 : 0 : return NULL;
775 : : }
776 : 0 : ret = create_mirror_aux_flows(dev, type, attr,
777 : : suffix_actions, sample_actions,
778 : : mirror_entry, flow_head, error);
779 [ # # ]: 0 : if (ret != 0)
780 : 0 : goto error;
781 : : action_append(prefix_actions,
782 : : &(struct rte_flow_action) {
783 : : .type = (enum rte_flow_action_type)MLX5_RTE_FLOW_ACTION_TYPE_MIRROR,
784 : : .conf = mirror_entry_to_mirror_action(mirror_entry)
785 : : });
786 : 0 : ret = flow_hw_create_flow(dev, type, attr, pattern, prefix_actions,
787 : : item_flags, action_flags,
788 : : true, &base_flow, error);
789 [ # # ]: 0 : if (ret != 0)
790 : 0 : goto error;
791 : 0 : SLIST_INSERT_HEAD(flow_head, base_flow, nt2hws->next);
792 : : save_sample_release_cxt(release, flow_head, mirror_entry, 0);
793 : : return base_flow;
794 : :
795 : 0 : error:
796 [ # # ]: 0 : if (!SLIST_EMPTY(flow_head))
797 : : release_chained_flows(dev, flow_head, type);
798 : : if (release)
799 : 0 : mlx5_free(release);
800 : 0 : return NULL;
801 : : }
802 : :
803 : : struct rte_flow_hw *
804 : 0 : mlx5_nta_sample_flow_list_create(struct rte_eth_dev *dev,
805 : : enum mlx5_flow_type type,
806 : : const struct rte_flow_attr *attr,
807 : : const struct rte_flow_item pattern[],
808 : : const struct rte_flow_action actions[],
809 : : uint64_t item_flags, uint64_t action_flags,
810 : : struct rte_flow_error *error)
811 : : {
812 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
813 : : struct mlx5_list_entry *mirror_entry;
814 : : struct rte_flow_hw *flow = NULL;
815 : : const struct rte_flow_action *sample;
816 : : struct rte_flow_action *sample_actions;
817 : : const struct rte_flow_action_sample *sample_conf;
818 : 0 : struct rte_flow_action prefix_actions[MLX5_HW_MAX_ACTS] = { 0 };
819 : 0 : struct rte_flow_action suffix_actions[MLX5_HW_MAX_ACTS] = { 0 };
820 : 0 : struct mlx5_flow_head flow_head = SLIST_HEAD_INITIALIZER(NULL);
821 : :
822 [ # # ]: 0 : if (priv->nta_sample_ctx == NULL) {
823 : 0 : int rc = mlx5_init_nta_sample_context(dev);
824 [ # # ]: 0 : if (rc != 0) {
825 : 0 : rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION,
826 : : NULL, "Failed to allocate sample context");
827 : 0 : return NULL;
828 : : }
829 : : }
830 : 0 : mlx5_nta_parse_sample_actions(actions, &sample, prefix_actions, suffix_actions);
831 [ # # ]: 0 : if (!validate_prefix_actions(prefix_actions)) {
832 : 0 : rte_flow_error_set(error, -EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
833 : : NULL, "Too many actions");
834 : 0 : return NULL;
835 : : }
836 : 0 : sample_conf = (const struct rte_flow_action_sample *)sample->conf;
837 : 0 : sample_actions = (struct rte_flow_action *)(uintptr_t)sample_conf->actions;
838 : 0 : mirror_entry = mlx5_create_nta_mirror(dev, attr, sample_actions,
839 : : suffix_actions, error);
840 [ # # ]: 0 : if (mirror_entry == NULL)
841 : : return NULL;
842 [ # # ]: 0 : if (sample_conf->ratio == 1) {
843 : 0 : flow = mlx5_nta_create_mirror_flow(dev, type, attr, item_flags, action_flags,
844 : : pattern, prefix_actions, suffix_actions,
845 : : sample_actions, mirror_entry,
846 : : &flow_head, error);
847 : : } else {
848 : 0 : flow = mlx5_nta_create_sample_flow(dev, type, attr, sample_conf->ratio,
849 : : item_flags, action_flags, pattern,
850 : : prefix_actions, suffix_actions,
851 : : sample_actions, mirror_entry,
852 : : &flow_head, error);
853 : : }
854 [ # # ]: 0 : if (flow == NULL)
855 : 0 : mlx5_list_unregister(priv->nta_sample_ctx->mirror_actions, mirror_entry);
856 : : return flow;
857 : : }
|