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 : mlx5_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 : if ((size_t)actions_size != cached_obj->actions_size)
297 : : return ~0;
298 : 0 : return memcmp(cached_obj->actions, obj_ctx->actions, actions_size);
299 : : }
300 : :
301 : : static void
302 : 0 : mlx5_nta_sample_cached_group_remove(void *cache_ctx, struct mlx5_list_entry *entry)
303 : : {
304 : : struct rte_eth_dev *dev = cache_ctx;
305 : : struct mlx5_nta_sample_cached_group *cached_obj =
306 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
307 : :
308 : 0 : release_cached_group(dev, cached_obj->group);
309 : 0 : mlx5_free((void *)(uintptr_t)cached_obj->actions);
310 : 0 : mlx5_free(cached_obj);
311 : 0 : }
312 : :
313 : : static struct mlx5_list_entry *
314 : 0 : mlx5_nta_sample_cached_group_clone(void *tool_ctx __rte_unused,
315 : : struct mlx5_list_entry *entry,
316 : : void *cb_ctx __rte_unused)
317 : : {
318 : : struct mlx5_nta_sample_cached_group *cached_obj =
319 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
320 : : struct mlx5_nta_sample_cached_group *new_obj;
321 : :
322 : 0 : new_obj = mlx5_malloc(MLX5_MEM_ANY, sizeof(*new_obj), 0, SOCKET_ID_ANY);
323 [ # # ]: 0 : if (new_obj == NULL)
324 : : return NULL;
325 : : memcpy(new_obj, cached_obj, sizeof(*new_obj));
326 : 0 : return &new_obj->entry;
327 : : }
328 : :
329 : : static void
330 : 0 : mlx5_nta_sample_cached_group_free_cloned(void *cache_ctx __rte_unused,
331 : : struct mlx5_list_entry *entry)
332 : : {
333 : : struct mlx5_nta_sample_cached_group *cloned_obj =
334 : 0 : container_of(entry, struct mlx5_nta_sample_cached_group, entry);
335 : :
336 : 0 : mlx5_free(cloned_obj);
337 : 0 : }
338 : :
339 : : static int
340 : 0 : mlx5_init_nta_sample_context(struct rte_eth_dev *dev)
341 : : {
342 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
343 : 0 : struct mlx5_indexed_pool_config ipool_cfg = {
344 : : .size = 0,
345 : : .trunk_size = 32,
346 : : .grow_trunk = 5,
347 : : .grow_shift = 1,
348 : : .need_lock = 1,
349 : 0 : .release_mem_en = !!priv->sh->config.reclaim_mode,
350 : : .max_idx = MLX5_FLOW_TABLE_SAMPLE_NUM,
351 : : .type = "mlx5_nta_sample"
352 : : };
353 : 0 : struct mlx5_nta_sample_ctx *ctx = mlx5_malloc(MLX5_MEM_ZERO,
354 : : sizeof(*ctx), 0, SOCKET_ID_ANY);
355 : :
356 [ # # ]: 0 : if (ctx == NULL)
357 : : return -ENOMEM;
358 : 0 : priv->nta_sample_ctx = ctx;
359 : 0 : ctx->group_ids = mlx5_ipool_create(&ipool_cfg);
360 [ # # ]: 0 : if (ctx->group_ids == NULL)
361 : 0 : goto error;
362 : 0 : ctx->mirror_groups = mlx5_list_create("nta sample groups", dev, true,
363 : : mlx5_nta_sample_cached_group_create,
364 : : mlx5_nta_sample_cached_group_match,
365 : : mlx5_nta_sample_cached_group_remove,
366 : : mlx5_nta_sample_cached_group_clone,
367 : : mlx5_nta_sample_cached_group_free_cloned);
368 [ # # ]: 0 : if (ctx->mirror_groups == NULL)
369 : 0 : goto error;
370 : 0 : ctx->mirror_actions = mlx5_list_create("nta sample mirror actions", dev, true,
371 : : mlx5_nta_sample_cached_mirror_create,
372 : : mlx5_nta_sample_cached_mirror_match,
373 : : mlx5_nta_sample_cached_mirror_remove,
374 : : mlx5_nta_sample_cached_mirror_clone,
375 : : mlx5_nta_sample_cached_mirror_free_cloned);
376 [ # # ]: 0 : if (ctx->mirror_actions == NULL)
377 : 0 : goto error;
378 : : return 0;
379 : :
380 : 0 : error:
381 : 0 : mlx5_nta_sample_context_free(dev);
382 : 0 : return -ENOMEM;
383 : : }
384 : :
385 : : static struct mlx5_list_entry *
386 : : register_mirror(struct mlx5_flow_template_table_cfg *table_cfg,
387 : : struct mlx5_list *cache,
388 : : struct mlx5_list_entry *sample,
389 : : struct mlx5_list_entry *suffix)
390 : : {
391 : 0 : struct mlx5_nta_sample_cached_mirror_ctx ctx = {
392 : : .table_cfg = table_cfg,
393 : : .sample = sample,
394 : : .suffix = suffix
395 : : };
396 : :
397 : 0 : return mlx5_list_register(cache, &ctx);
398 : : }
399 : :
400 : : static struct mlx5_list_entry *
401 : : register_mirror_actions(struct rte_flow_action *actions, struct mlx5_list *cache)
402 : : {
403 : 0 : struct mlx5_nta_sample_cached_group_ctx ctx = {
404 : : .actions = actions
405 : : };
406 : 0 : return mlx5_list_register(cache, &ctx);
407 : : }
408 : :
409 : : void
410 : 0 : mlx5_nta_sample_mirror_entry_release(struct rte_eth_dev *dev,
411 : : struct mlx5_sample_release_ctx *release)
412 : : {
413 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
414 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
415 : :
416 : 0 : mlx5_list_unregister(ctx->mirror_actions, release->mirror_entry);
417 : 0 : release_cached_group(dev, release->sample_group);
418 : 0 : mlx5_free(release);
419 : 0 : }
420 : :
421 : : static struct mlx5_list_entry *
422 : 0 : mlx5_create_nta_mirror(struct rte_eth_dev *dev,
423 : : const struct rte_flow_attr *attr,
424 : : struct rte_flow_action *sample_actions,
425 : : struct rte_flow_action *suffix_actions,
426 : : struct rte_flow_error *error)
427 : : {
428 : : struct mlx5_list_entry *entry;
429 : : struct mlx5_list_entry *mirror_sample = NULL, *mirror_suffix = NULL;
430 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
431 : 0 : struct mlx5_nta_sample_ctx *ctx = priv->nta_sample_ctx;
432 : 0 : struct mlx5_flow_template_table_cfg table_cfg = {
433 : : .external = true,
434 : : .attr = {
435 : : .flow_attr = *attr
436 : : }
437 : : };
438 : :
439 : 0 : mirror_sample = register_mirror_actions(sample_actions, ctx->mirror_groups);
440 [ # # ]: 0 : if (mirror_sample == NULL) {
441 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
442 : : NULL, "Failed to register sample group");
443 : 0 : goto error;
444 : : }
445 : 0 : mirror_suffix = register_mirror_actions(suffix_actions, ctx->mirror_groups);
446 [ # # ]: 0 : if (mirror_suffix == NULL) {
447 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
448 : : NULL, "Failed to register suffix group");
449 : 0 : goto error;
450 : : }
451 : 0 : entry = register_mirror(&table_cfg, ctx->mirror_actions, mirror_sample, mirror_suffix);
452 [ # # ]: 0 : if (entry == NULL) {
453 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
454 : : NULL, "Failed to register HWS mirror action");
455 : 0 : goto error;
456 : : }
457 : :
458 : : return entry;
459 : :
460 : 0 : error:
461 [ # # ]: 0 : if (mirror_sample)
462 : 0 : mlx5_list_unregister(ctx->mirror_groups, mirror_sample);
463 [ # # ]: 0 : if (mirror_suffix)
464 : 0 : mlx5_list_unregister(ctx->mirror_groups, mirror_suffix);
465 : : return NULL;
466 : : }
467 : :
468 : : static uint32_t
469 : 0 : generate_random_mask(uint32_t ratio)
470 : : {
471 : : uint32_t i;
472 [ # # ]: 0 : double goal = 1.0 / ratio;
473 : :
474 : : /* Check if the ratio value is power of 2 */
475 [ # # ]: 0 : if (rte_popcount32(ratio) == 1) {
476 [ # # ]: 0 : for (i = 2; i < 32; i++) {
477 [ # # ]: 0 : if (RTE_BIT32(i) == ratio)
478 : 0 : return RTE_BIT32(i) - 1;
479 : : }
480 : : }
481 : :
482 : : /*
483 : : * Find the last power of 2 with ratio larger then the goal.
484 : : */
485 [ # # ]: 0 : for (i = 2; i < 32; i++) {
486 : 0 : double res = 1.0 / RTE_BIT32(i);
487 : :
488 [ # # ]: 0 : if (res < goal)
489 : 0 : return RTE_BIT32(i - 1) - 1;
490 : : }
491 : :
492 : : return UINT32_MAX;
493 : : }
494 : :
495 : : static void
496 : 0 : mlx5_nta_parse_sample_actions(const struct rte_flow_action *action,
497 : : const struct rte_flow_action **sample_action,
498 : : struct rte_flow_action *prefix_actions,
499 : : struct rte_flow_action *suffix_actions)
500 : : {
501 : : struct rte_flow_action *pa = prefix_actions;
502 : : struct rte_flow_action *sa = suffix_actions;
503 : :
504 : 0 : *sample_action = NULL;
505 : : do {
506 [ # # ]: 0 : if (action->type == RTE_FLOW_ACTION_TYPE_SAMPLE) {
507 : 0 : *sample_action = action;
508 [ # # ]: 0 : } else if (*sample_action == NULL) {
509 [ # # ]: 0 : if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
510 : 0 : continue;
511 : 0 : *(pa++) = *action;
512 : : } else {
513 [ # # ]: 0 : if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
514 : 0 : continue;
515 : 0 : *(sa++) = *action;
516 : : }
517 [ # # ]: 0 : } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
518 : 0 : }
519 : :
520 : : static bool
521 : : validate_prefix_actions(const struct rte_flow_action *actions)
522 : : {
523 : : uint32_t i = 0;
524 : :
525 [ # # ]: 0 : while (actions[i].type != RTE_FLOW_ACTION_TYPE_END)
526 : 0 : i++;
527 : : return i < MLX5_HW_MAX_ACTS - 1;
528 : : }
529 : :
530 : : static bool
531 : 0 : validate_sample_terminal_actions(const struct rte_eth_dev *dev,
532 : : const struct rte_flow_attr *flow_attr,
533 : : const struct rte_flow_action *actions)
534 : : {
535 : : uint32_t i;
536 : 0 : const struct mlx5_priv *priv = dev->data->dev_private;
537 : : const struct rte_flow_action_ethdev *port = NULL;
538 [ # # # # ]: 0 : bool is_proxy = MLX5_HW_PORT_IS_PROXY(priv);
539 : : const struct rte_flow_action *a = NULL;
540 : :
541 [ # # ]: 0 : for (i = 0; actions[i].type != RTE_FLOW_ACTION_TYPE_END; i++) {
542 [ # # ]: 0 : if (actions[i].type != RTE_FLOW_ACTION_TYPE_VOID)
543 : : a = &actions[i];
544 : : }
545 [ # # ]: 0 : if (a == NULL)
546 : : return false;
547 [ # # # ]: 0 : switch (a->type) {
548 : : case RTE_FLOW_ACTION_TYPE_JUMP:
549 : : case RTE_FLOW_ACTION_TYPE_QUEUE:
550 : : case RTE_FLOW_ACTION_TYPE_DROP:
551 : : case RTE_FLOW_ACTION_TYPE_REPRESENTED_PORT:
552 : : return true;
553 : 0 : case RTE_FLOW_ACTION_TYPE_PORT_REPRESENTOR:
554 [ # # # # ]: 0 : if (!is_proxy || !flow_attr->transfer)
555 : : return false;
556 : 0 : port = a->conf;
557 [ # # # # ]: 0 : if (!port || port->port_id != MLX5_REPRESENTED_PORT_ESW_MGR)
558 : 0 : return false;
559 : : return true;
560 : : default:
561 : : break;
562 : : }
563 : 0 : return false;
564 : : }
565 : :
566 : : static void
567 : : action_append(struct rte_flow_action *actions, const struct rte_flow_action *last)
568 : : {
569 : : uint32_t i = 0;
570 : :
571 [ # # # # ]: 0 : while (actions[i].type != RTE_FLOW_ACTION_TYPE_END)
572 : 0 : i++;
573 : 0 : actions[i] = *last;
574 : : }
575 : :
576 : : static int
577 : 0 : create_mirror_aux_flows(struct rte_eth_dev *dev,
578 : : enum mlx5_flow_type type,
579 : : const struct rte_flow_attr *attr,
580 : : struct rte_flow_action *suffix_actions,
581 : : struct rte_flow_action *sample_actions,
582 : : struct mlx5_list_entry *mirror_entry,
583 : : struct mlx5_flow_head *flow_head,
584 : : struct rte_flow_error *error)
585 : : {
586 : 0 : const struct rte_flow_attr suffix_attr = {
587 : 0 : .ingress = attr->ingress,
588 : 0 : .egress = attr->egress,
589 : 0 : .transfer = attr->transfer,
590 : : .group = mirror_entry_to_mirror_suffix_group(mirror_entry),
591 : : };
592 : 0 : const struct rte_flow_attr sample_attr = {
593 : : .ingress = attr->ingress,
594 : : .egress = attr->egress,
595 : : .transfer = attr->transfer,
596 : : .group = mirror_entry_to_mirror_sample_group(mirror_entry),
597 : : };
598 : 0 : const struct rte_flow_item secondary_pattern[1] = {
599 : : [0] = { .type = RTE_FLOW_ITEM_TYPE_END }
600 : : };
601 : : int ret, encap_idx, actions_num;
602 : : uint64_t suffix_action_flags, sample_action_flags;
603 : 0 : const struct rte_flow_action *qrss_action = NULL, *mark_action = NULL;
604 : 0 : struct rte_flow_hw *suffix_flow = NULL, *sample_flow = NULL;
605 : :
606 : 0 : suffix_action_flags = mlx5_flow_hw_action_flags_get(suffix_actions,
607 : : &qrss_action, &mark_action,
608 : : &encap_idx, &actions_num, error);
609 [ # # # # ]: 0 : if (qrss_action != NULL && qrss_action->type == RTE_FLOW_ACTION_TYPE_RSS)
610 : 0 : return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION, NULL,
611 : : "RSS action is not supported in suffix sample action");
612 : 0 : sample_action_flags = mlx5_flow_hw_action_flags_get(sample_actions,
613 : : &qrss_action, &mark_action,
614 : : &encap_idx, &actions_num, error);
615 [ # # # # ]: 0 : if (qrss_action != NULL && qrss_action->type == RTE_FLOW_ACTION_TYPE_RSS)
616 : 0 : return rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION, NULL,
617 : : "RSS action is not supported in sample action");
618 : 0 : ret = mlx5_flow_hw_create_flow(dev, type, &suffix_attr,
619 : : secondary_pattern, suffix_actions,
620 : : MLX5_FLOW_LAYER_OUTER_L2, suffix_action_flags,
621 : : true, &suffix_flow, error);
622 [ # # ]: 0 : if (ret != 0)
623 : : return ret;
624 : 0 : ret = mlx5_flow_hw_create_flow(dev, type, &sample_attr,
625 : : secondary_pattern, sample_actions,
626 : : MLX5_FLOW_LAYER_OUTER_L2, sample_action_flags,
627 : : true, &sample_flow, error);
628 [ # # ]: 0 : if (ret != 0) {
629 : 0 : mlx5_flow_hw_destroy(dev, suffix_flow);
630 : 0 : return ret;
631 : : }
632 : 0 : suffix_flow->nt2hws->chaned_flow = 1;
633 : 0 : SLIST_INSERT_HEAD(flow_head, suffix_flow, nt2hws->next);
634 : 0 : sample_flow->nt2hws->chaned_flow = 1;
635 : 0 : SLIST_INSERT_HEAD(flow_head, sample_flow, nt2hws->next);
636 : 0 : return 0;
637 : : }
638 : :
639 : : static struct rte_flow_hw *
640 : 0 : create_sample_flow(struct rte_eth_dev *dev,
641 : : enum mlx5_flow_type type,
642 : : const struct rte_flow_attr *attr,
643 : : uint32_t ratio,
644 : : uint32_t sample_group,
645 : : struct mlx5_list_entry *mirror_entry,
646 : : struct rte_flow_error *error)
647 : : {
648 : 0 : struct rte_flow_hw *sample_flow = NULL;
649 : 0 : uint32_t random_mask = generate_random_mask(ratio);
650 : 0 : const struct rte_flow_attr sample_attr = {
651 : 0 : .ingress = attr->ingress,
652 : 0 : .egress = attr->egress,
653 : 0 : .transfer = attr->transfer,
654 : : .group = sample_group,
655 : : };
656 : 0 : const struct rte_flow_item sample_pattern[2] = {
657 : : [0] = {
658 : : .type = RTE_FLOW_ITEM_TYPE_RANDOM,
659 : 0 : .mask = &(struct rte_flow_item_random) {
660 : : .value = random_mask
661 : : },
662 : 0 : .spec = &(struct rte_flow_item_random) {
663 : : .value = 1
664 : : },
665 : : },
666 : : [1] = { .type = RTE_FLOW_ITEM_TYPE_END }
667 : : };
668 : 0 : const struct rte_flow_action sample_actions[2] = {
669 : : [0] = {
670 : : .type = (enum rte_flow_action_type)MLX5_RTE_FLOW_ACTION_TYPE_MIRROR,
671 : : .conf = mirror_entry_to_mirror_action(mirror_entry)
672 : : },
673 : : [1] = { .type = RTE_FLOW_ACTION_TYPE_END }
674 : : };
675 : :
676 [ # # ]: 0 : if (random_mask > UINT16_MAX)
677 : : return NULL;
678 : 0 : mlx5_flow_hw_create_flow(dev, type, &sample_attr, sample_pattern, sample_actions,
679 : : 0, 0, true, &sample_flow, error);
680 : 0 : return sample_flow;
681 : : }
682 : :
683 : : static int
684 : 0 : create_random_miss_actions(struct rte_eth_dev *dev,
685 : : const struct rte_flow_attr *attr,
686 : : uint32_t sample_group,
687 : : struct mlx5_list_entry *mirror_entry,
688 : : struct rte_flow_error *error)
689 : : {
690 : 0 : const struct rte_flow_group_attr sample_group_attr = {
691 : 0 : .ingress = attr->ingress,
692 : 0 : .egress = attr->egress,
693 : 0 : .transfer = attr->transfer,
694 : : };
695 : 0 : const struct rte_flow_action sample_miss_actions[2] = {
696 : : [0] = {
697 : : .type = RTE_FLOW_ACTION_TYPE_JUMP,
698 : 0 : .conf = &(struct rte_flow_action_jump) {
699 : : .group = mirror_entry_to_mirror_suffix_group(mirror_entry)
700 : : }
701 : : },
702 : : [1] = { .type = RTE_FLOW_ACTION_TYPE_END }
703 : : };
704 : :
705 : 0 : return mlx5_flow_hw_group_set_miss_actions(dev, sample_group, &sample_group_attr,
706 : : sample_miss_actions, error);
707 : : }
708 : :
709 : : static void
710 : : save_sample_release_cxt(struct mlx5_sample_release_ctx *release,
711 : : struct mlx5_flow_head *flow_head,
712 : : struct mlx5_list_entry *mirror_entry,
713 : : uint32_t sample_group)
714 : : {
715 : : struct rte_flow_hw *var;
716 : :
717 : 0 : release->mirror_entry = mirror_entry;
718 : 0 : release->sample_group = sample_group;
719 : :
720 [ # # # # ]: 0 : SLIST_FOREACH(var, flow_head, nt2hws->next) {
721 [ # # # # ]: 0 : if (SLIST_NEXT(var, nt2hws->next) == NULL)
722 : 0 : var->nt2hws->sample_release_ctx = release;
723 : : }
724 : : }
725 : :
726 : : static struct rte_flow_hw *
727 : 0 : mlx5_nta_create_sample_flow(struct rte_eth_dev *dev,
728 : : enum mlx5_flow_type type,
729 : : const struct rte_flow_attr *attr,
730 : : uint32_t sample_ratio,
731 : : uint64_t item_flags, uint64_t action_flags,
732 : : const struct rte_flow_item *pattern,
733 : : struct rte_flow_action *prefix_actions,
734 : : struct rte_flow_action *suffix_actions,
735 : : struct rte_flow_action *sample_actions,
736 : : struct mlx5_list_entry *mirror_entry,
737 : : struct mlx5_flow_head *flow_head,
738 : : struct rte_flow_error *error)
739 : : {
740 : : int ret;
741 : 0 : uint32_t sample_group = alloc_cached_group(dev);
742 : 0 : struct rte_flow_hw *base_flow = NULL, *sample_flow = NULL;
743 : : struct mlx5_sample_release_ctx *release = NULL;
744 : :
745 [ # # ]: 0 : if (sample_group == 0)
746 : : return NULL;
747 : 0 : release = mlx5_malloc(MLX5_MEM_ANY, sizeof(*release), 0, SOCKET_ID_ANY);
748 [ # # ]: 0 : if (release == NULL) {
749 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ACTION,
750 : : NULL, "Failed to allocate release context");
751 : 0 : goto error;
752 : : }
753 : 0 : ret = create_mirror_aux_flows(dev, type, attr,
754 : : suffix_actions, sample_actions,
755 : : mirror_entry, flow_head, error);
756 [ # # ]: 0 : if (ret != 0)
757 : 0 : goto error;
758 : 0 : ret = create_random_miss_actions(dev, attr, sample_group,
759 : : mirror_entry, error);
760 [ # # ]: 0 : if (ret != 0)
761 : 0 : goto error;
762 : 0 : sample_flow = create_sample_flow(dev, type, attr, sample_ratio, sample_group,
763 : : mirror_entry, error);
764 [ # # ]: 0 : if (sample_flow == NULL)
765 : 0 : goto error;
766 : 0 : sample_flow->nt2hws->chaned_flow = 1;
767 : 0 : SLIST_INSERT_HEAD(flow_head, sample_flow, nt2hws->next);
768 : : action_append(prefix_actions,
769 : : &(struct rte_flow_action) {
770 : : .type = RTE_FLOW_ACTION_TYPE_JUMP,
771 : 0 : .conf = &(struct rte_flow_action_jump) { .group = sample_group }
772 : : });
773 : 0 : ret = mlx5_flow_hw_create_flow(dev, type, attr, pattern, prefix_actions,
774 : : item_flags, action_flags, true, &base_flow, error);
775 [ # # ]: 0 : if (ret != 0)
776 : 0 : goto error;
777 : 0 : SLIST_INSERT_HEAD(flow_head, base_flow, nt2hws->next);
778 : : save_sample_release_cxt(release, flow_head, mirror_entry, sample_group);
779 : : return base_flow;
780 : :
781 : 0 : error:
782 [ # # ]: 0 : if (!SLIST_EMPTY(flow_head))
783 : : release_chained_flows(dev, flow_head, type);
784 [ # # ]: 0 : if (release)
785 : 0 : mlx5_free(release);
786 [ # # ]: 0 : if (sample_flow)
787 : : release_cached_group(dev, sample_group);
788 : : return NULL;
789 : : }
790 : :
791 : : static struct rte_flow_hw *
792 : 0 : mlx5_nta_create_mirror_flow(struct rte_eth_dev *dev,
793 : : enum mlx5_flow_type type,
794 : : const struct rte_flow_attr *attr,
795 : : uint64_t item_flags, uint64_t action_flags,
796 : : const struct rte_flow_item *pattern,
797 : : struct rte_flow_action *prefix_actions,
798 : : struct rte_flow_action *suffix_actions,
799 : : struct rte_flow_action *sample_actions,
800 : : struct mlx5_list_entry *mirror_entry,
801 : : struct mlx5_flow_head *flow_head,
802 : : struct rte_flow_error *error)
803 : : {
804 : : int ret;
805 : 0 : struct rte_flow_hw *base_flow = NULL;
806 : 0 : struct mlx5_sample_release_ctx *release = mlx5_malloc(MLX5_MEM_ANY, sizeof(*release),
807 : : 0, SOCKET_ID_ANY);
808 : :
809 [ # # ]: 0 : if (release == NULL) {
810 : 0 : rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_ACTION,
811 : : NULL, "Failed to allocate release context");
812 : 0 : return NULL;
813 : : }
814 : 0 : ret = create_mirror_aux_flows(dev, type, attr,
815 : : suffix_actions, sample_actions,
816 : : mirror_entry, flow_head, error);
817 [ # # ]: 0 : if (ret != 0)
818 : 0 : goto error;
819 : : action_append(prefix_actions,
820 : : &(struct rte_flow_action) {
821 : : .type = (enum rte_flow_action_type)MLX5_RTE_FLOW_ACTION_TYPE_MIRROR,
822 : : .conf = mirror_entry_to_mirror_action(mirror_entry)
823 : : });
824 : 0 : ret = mlx5_flow_hw_create_flow(dev, type, attr, pattern, prefix_actions,
825 : : item_flags, action_flags,
826 : : true, &base_flow, error);
827 [ # # ]: 0 : if (ret != 0)
828 : 0 : goto error;
829 : 0 : SLIST_INSERT_HEAD(flow_head, base_flow, nt2hws->next);
830 : : save_sample_release_cxt(release, flow_head, mirror_entry, 0);
831 : : return base_flow;
832 : :
833 : 0 : error:
834 [ # # ]: 0 : if (!SLIST_EMPTY(flow_head))
835 : : release_chained_flows(dev, flow_head, type);
836 : : if (release)
837 : 0 : mlx5_free(release);
838 : 0 : return NULL;
839 : : }
840 : :
841 : : struct rte_flow_hw *
842 : 0 : mlx5_nta_sample_flow_list_create(struct rte_eth_dev *dev,
843 : : enum mlx5_flow_type type,
844 : : const struct rte_flow_attr *attr,
845 : : const struct rte_flow_item pattern[],
846 : : const struct rte_flow_action actions[],
847 : : uint64_t item_flags, uint64_t action_flags,
848 : : struct rte_flow_error *error)
849 : : {
850 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
851 : : struct mlx5_list_entry *mirror_entry;
852 : : struct rte_flow_hw *flow = NULL;
853 : : const struct rte_flow_action *sample;
854 : : struct rte_flow_action *sample_actions;
855 : : const struct rte_flow_action_sample *sample_conf;
856 : 0 : struct rte_flow_action prefix_actions[MLX5_HW_MAX_ACTS] = { 0 };
857 : 0 : struct rte_flow_action suffix_actions[MLX5_HW_MAX_ACTS] = { 0 };
858 : 0 : struct mlx5_flow_head flow_head = SLIST_HEAD_INITIALIZER(NULL);
859 : :
860 [ # # ]: 0 : if (priv->nta_sample_ctx == NULL) {
861 : 0 : int rc = mlx5_init_nta_sample_context(dev);
862 [ # # ]: 0 : if (rc != 0) {
863 : 0 : rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION,
864 : : NULL, "Failed to allocate sample context");
865 : 0 : return NULL;
866 : : }
867 : : }
868 : 0 : mlx5_nta_parse_sample_actions(actions, &sample, prefix_actions, suffix_actions);
869 [ # # ]: 0 : if (!validate_prefix_actions(prefix_actions)) {
870 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
871 : : NULL, "Too many actions");
872 : 0 : return NULL;
873 : : }
874 [ # # ]: 0 : if (!validate_sample_terminal_actions(dev, attr, sample)) {
875 : 0 : rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
876 : : NULL, "Invalid sample actions");
877 : 0 : return NULL;
878 : : }
879 : 0 : sample_conf = (const struct rte_flow_action_sample *)sample->conf;
880 : 0 : sample_actions = (struct rte_flow_action *)(uintptr_t)sample_conf->actions;
881 : 0 : mirror_entry = mlx5_create_nta_mirror(dev, attr, sample_actions,
882 : : suffix_actions, error);
883 [ # # ]: 0 : if (mirror_entry == NULL)
884 : : return NULL;
885 [ # # ]: 0 : if (sample_conf->ratio == 1) {
886 : 0 : flow = mlx5_nta_create_mirror_flow(dev, type, attr, item_flags, action_flags,
887 : : pattern, prefix_actions, suffix_actions,
888 : : sample_actions, mirror_entry,
889 : : &flow_head, error);
890 : : } else {
891 : 0 : flow = mlx5_nta_create_sample_flow(dev, type, attr, sample_conf->ratio,
892 : : item_flags, action_flags, pattern,
893 : : prefix_actions, suffix_actions,
894 : : sample_actions, mirror_entry,
895 : : &flow_head, error);
896 : : }
897 [ # # ]: 0 : if (flow == NULL)
898 : 0 : mlx5_list_unregister(priv->nta_sample_ctx->mirror_actions, mirror_entry);
899 : : return flow;
900 : : }
|