1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
6 #include <netinet/in.h>
13 #include <rte_common.h>
14 #include <rte_ether.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_eal_paging.h>
18 #include <rte_cycles.h>
19 #include <rte_flow_driver.h>
20 #include <rte_malloc.h>
23 #include <mlx5_glue.h>
24 #include <mlx5_devx_cmds.h>
26 #include <mlx5_malloc.h>
28 #include "mlx5_defs.h"
30 #include "mlx5_flow.h"
31 #include "mlx5_flow_os.h"
32 #include "mlx5_rxtx.h"
33 #include "mlx5_common_os.h"
34 #include "rte_pmd_mlx5.h"
36 struct tunnel_default_miss_ctx {
40 struct rte_flow_action_rss action_rss;
41 struct rte_flow_action_queue miss_queue;
42 struct rte_flow_action_jump miss_jump;
48 flow_tunnel_add_default_miss(struct rte_eth_dev *dev,
49 struct rte_flow *flow,
50 const struct rte_flow_attr *attr,
51 const struct rte_flow_action *app_actions,
53 struct tunnel_default_miss_ctx *ctx,
54 struct rte_flow_error *error);
55 static struct mlx5_flow_tunnel *
56 mlx5_find_tunnel_id(struct rte_eth_dev *dev, uint32_t id);
58 mlx5_flow_tunnel_free(struct rte_eth_dev *dev, struct mlx5_flow_tunnel *tunnel);
60 tunnel_flow_group_to_flow_table(struct rte_eth_dev *dev,
61 const struct mlx5_flow_tunnel *tunnel,
62 uint32_t group, uint32_t *table,
63 struct rte_flow_error *error);
65 static struct mlx5_flow_workspace *mlx5_flow_push_thread_workspace(void);
66 static void mlx5_flow_pop_thread_workspace(void);
69 /** Device flow drivers. */
70 extern const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops;
72 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops;
74 const struct mlx5_flow_driver_ops *flow_drv_ops[] = {
75 [MLX5_FLOW_TYPE_MIN] = &mlx5_flow_null_drv_ops,
76 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
77 [MLX5_FLOW_TYPE_DV] = &mlx5_flow_dv_drv_ops,
79 [MLX5_FLOW_TYPE_VERBS] = &mlx5_flow_verbs_drv_ops,
80 [MLX5_FLOW_TYPE_MAX] = &mlx5_flow_null_drv_ops
83 /** Helper macro to build input graph for mlx5_flow_expand_rss(). */
84 #define MLX5_FLOW_EXPAND_RSS_NEXT(...) \
89 /** Node object of input graph for mlx5_flow_expand_rss(). */
90 struct mlx5_flow_expand_node {
91 const int *const next;
93 * List of next node indexes. Index 0 is interpreted as a terminator.
95 const enum rte_flow_item_type type;
96 /**< Pattern item type of current node. */
99 * RSS types bit-field associated with this node
100 * (see ETH_RSS_* definitions).
104 /** Object returned by mlx5_flow_expand_rss(). */
105 struct mlx5_flow_expand_rss {
107 /**< Number of entries @p patterns and @p priorities. */
109 struct rte_flow_item *pattern; /**< Expanded pattern array. */
110 uint32_t priority; /**< Priority offset for each expansion. */
114 static enum rte_flow_item_type
115 mlx5_flow_expand_rss_item_complete(const struct rte_flow_item *item)
117 enum rte_flow_item_type ret = RTE_FLOW_ITEM_TYPE_VOID;
118 uint16_t ether_type = 0;
119 uint16_t ether_type_m;
120 uint8_t ip_next_proto = 0;
121 uint8_t ip_next_proto_m;
123 if (item == NULL || item->spec == NULL)
125 switch (item->type) {
126 case RTE_FLOW_ITEM_TYPE_ETH:
128 ether_type_m = ((const struct rte_flow_item_eth *)
131 ether_type_m = rte_flow_item_eth_mask.type;
132 if (ether_type_m != RTE_BE16(0xFFFF))
134 ether_type = ((const struct rte_flow_item_eth *)
136 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
137 ret = RTE_FLOW_ITEM_TYPE_IPV4;
138 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
139 ret = RTE_FLOW_ITEM_TYPE_IPV6;
140 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
141 ret = RTE_FLOW_ITEM_TYPE_VLAN;
143 ret = RTE_FLOW_ITEM_TYPE_END;
145 case RTE_FLOW_ITEM_TYPE_VLAN:
147 ether_type_m = ((const struct rte_flow_item_vlan *)
148 (item->mask))->inner_type;
150 ether_type_m = rte_flow_item_vlan_mask.inner_type;
151 if (ether_type_m != RTE_BE16(0xFFFF))
153 ether_type = ((const struct rte_flow_item_vlan *)
154 (item->spec))->inner_type;
155 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
156 ret = RTE_FLOW_ITEM_TYPE_IPV4;
157 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
158 ret = RTE_FLOW_ITEM_TYPE_IPV6;
159 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
160 ret = RTE_FLOW_ITEM_TYPE_VLAN;
162 ret = RTE_FLOW_ITEM_TYPE_END;
164 case RTE_FLOW_ITEM_TYPE_IPV4:
166 ip_next_proto_m = ((const struct rte_flow_item_ipv4 *)
167 (item->mask))->hdr.next_proto_id;
170 rte_flow_item_ipv4_mask.hdr.next_proto_id;
171 if (ip_next_proto_m != 0xFF)
173 ip_next_proto = ((const struct rte_flow_item_ipv4 *)
174 (item->spec))->hdr.next_proto_id;
175 if (ip_next_proto == IPPROTO_UDP)
176 ret = RTE_FLOW_ITEM_TYPE_UDP;
177 else if (ip_next_proto == IPPROTO_TCP)
178 ret = RTE_FLOW_ITEM_TYPE_TCP;
179 else if (ip_next_proto == IPPROTO_IP)
180 ret = RTE_FLOW_ITEM_TYPE_IPV4;
181 else if (ip_next_proto == IPPROTO_IPV6)
182 ret = RTE_FLOW_ITEM_TYPE_IPV6;
184 ret = RTE_FLOW_ITEM_TYPE_END;
186 case RTE_FLOW_ITEM_TYPE_IPV6:
188 ip_next_proto_m = ((const struct rte_flow_item_ipv6 *)
189 (item->mask))->hdr.proto;
192 rte_flow_item_ipv6_mask.hdr.proto;
193 if (ip_next_proto_m != 0xFF)
195 ip_next_proto = ((const struct rte_flow_item_ipv6 *)
196 (item->spec))->hdr.proto;
197 if (ip_next_proto == IPPROTO_UDP)
198 ret = RTE_FLOW_ITEM_TYPE_UDP;
199 else if (ip_next_proto == IPPROTO_TCP)
200 ret = RTE_FLOW_ITEM_TYPE_TCP;
201 else if (ip_next_proto == IPPROTO_IP)
202 ret = RTE_FLOW_ITEM_TYPE_IPV4;
203 else if (ip_next_proto == IPPROTO_IPV6)
204 ret = RTE_FLOW_ITEM_TYPE_IPV6;
206 ret = RTE_FLOW_ITEM_TYPE_END;
209 ret = RTE_FLOW_ITEM_TYPE_VOID;
216 * Expand RSS flows into several possible flows according to the RSS hash
217 * fields requested and the driver capabilities.
220 * Buffer to store the result expansion.
222 * Buffer size in bytes. If 0, @p buf can be NULL.
226 * RSS types to expand (see ETH_RSS_* definitions).
228 * Input graph to expand @p pattern according to @p types.
229 * @param[in] graph_root_index
230 * Index of root node in @p graph, typically 0.
233 * A positive value representing the size of @p buf in bytes regardless of
234 * @p size on success, a negative errno value otherwise and rte_errno is
235 * set, the following errors are defined:
237 * -E2BIG: graph-depth @p graph is too deep.
240 mlx5_flow_expand_rss(struct mlx5_flow_expand_rss *buf, size_t size,
241 const struct rte_flow_item *pattern, uint64_t types,
242 const struct mlx5_flow_expand_node graph[],
243 int graph_root_index)
246 const struct rte_flow_item *item;
247 const struct mlx5_flow_expand_node *node = &graph[graph_root_index];
248 const int *next_node;
249 const int *stack[elt_n];
251 struct rte_flow_item flow_items[elt_n];
254 size_t user_pattern_size = 0;
256 const struct mlx5_flow_expand_node *next = NULL;
257 struct rte_flow_item missed_item;
260 const struct rte_flow_item *last_item = NULL;
262 memset(&missed_item, 0, sizeof(missed_item));
263 lsize = offsetof(struct mlx5_flow_expand_rss, entry) +
264 elt_n * sizeof(buf->entry[0]);
266 buf->entry[0].priority = 0;
267 buf->entry[0].pattern = (void *)&buf->entry[elt_n];
269 addr = buf->entry[0].pattern;
271 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
272 if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
274 for (i = 0; node->next && node->next[i]; ++i) {
275 next = &graph[node->next[i]];
276 if (next->type == item->type)
281 user_pattern_size += sizeof(*item);
283 user_pattern_size += sizeof(*item); /* Handle END item. */
284 lsize += user_pattern_size;
285 /* Copy the user pattern in the first entry of the buffer. */
287 rte_memcpy(addr, pattern, user_pattern_size);
288 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
291 /* Start expanding. */
292 memset(flow_items, 0, sizeof(flow_items));
293 user_pattern_size -= sizeof(*item);
295 * Check if the last valid item has spec set, need complete pattern,
296 * and the pattern can be used for expansion.
298 missed_item.type = mlx5_flow_expand_rss_item_complete(last_item);
299 if (missed_item.type == RTE_FLOW_ITEM_TYPE_END) {
300 /* Item type END indicates expansion is not required. */
303 if (missed_item.type != RTE_FLOW_ITEM_TYPE_VOID) {
306 for (i = 0; node->next && node->next[i]; ++i) {
307 next = &graph[node->next[i]];
308 if (next->type == missed_item.type) {
309 flow_items[0].type = missed_item.type;
310 flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
316 if (next && missed) {
317 elt = 2; /* missed item + item end. */
319 lsize += elt * sizeof(*item) + user_pattern_size;
320 if ((node->rss_types & types) && lsize <= size) {
321 buf->entry[buf->entries].priority = 1;
322 buf->entry[buf->entries].pattern = addr;
324 rte_memcpy(addr, buf->entry[0].pattern,
326 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
327 rte_memcpy(addr, flow_items, elt * sizeof(*item));
328 addr = (void *)(((uintptr_t)addr) +
329 elt * sizeof(*item));
332 memset(flow_items, 0, sizeof(flow_items));
333 next_node = node->next;
334 stack[stack_pos] = next_node;
335 node = next_node ? &graph[*next_node] : NULL;
337 flow_items[stack_pos].type = node->type;
338 if (node->rss_types & types) {
340 * compute the number of items to copy from the
341 * expansion and copy it.
342 * When the stack_pos is 0, there are 1 element in it,
343 * plus the addition END item.
346 flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
347 lsize += elt * sizeof(*item) + user_pattern_size;
349 size_t n = elt * sizeof(*item);
351 buf->entry[buf->entries].priority =
352 stack_pos + 1 + missed;
353 buf->entry[buf->entries].pattern = addr;
355 rte_memcpy(addr, buf->entry[0].pattern,
357 addr = (void *)(((uintptr_t)addr) +
359 rte_memcpy(addr, &missed_item,
360 missed * sizeof(*item));
361 addr = (void *)(((uintptr_t)addr) +
362 missed * sizeof(*item));
363 rte_memcpy(addr, flow_items, n);
364 addr = (void *)(((uintptr_t)addr) + n);
369 next_node = node->next;
370 if (stack_pos++ == elt_n) {
374 stack[stack_pos] = next_node;
375 } else if (*(next_node + 1)) {
376 /* Follow up with the next possibility. */
379 /* Move to the next path. */
381 next_node = stack[--stack_pos];
383 stack[stack_pos] = next_node;
385 node = *next_node ? &graph[*next_node] : NULL;
387 /* no expanded flows but we have missed item, create one rule for it */
388 if (buf->entries == 1 && missed != 0) {
390 lsize += elt * sizeof(*item) + user_pattern_size;
392 buf->entry[buf->entries].priority = 1;
393 buf->entry[buf->entries].pattern = addr;
395 flow_items[0].type = missed_item.type;
396 flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
397 rte_memcpy(addr, buf->entry[0].pattern,
399 addr = (void *)(((uintptr_t)addr) + user_pattern_size);
400 rte_memcpy(addr, flow_items, elt * sizeof(*item));
406 enum mlx5_expansion {
408 MLX5_EXPANSION_ROOT_OUTER,
409 MLX5_EXPANSION_ROOT_ETH_VLAN,
410 MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN,
411 MLX5_EXPANSION_OUTER_ETH,
412 MLX5_EXPANSION_OUTER_ETH_VLAN,
413 MLX5_EXPANSION_OUTER_VLAN,
414 MLX5_EXPANSION_OUTER_IPV4,
415 MLX5_EXPANSION_OUTER_IPV4_UDP,
416 MLX5_EXPANSION_OUTER_IPV4_TCP,
417 MLX5_EXPANSION_OUTER_IPV6,
418 MLX5_EXPANSION_OUTER_IPV6_UDP,
419 MLX5_EXPANSION_OUTER_IPV6_TCP,
420 MLX5_EXPANSION_VXLAN,
421 MLX5_EXPANSION_VXLAN_GPE,
425 MLX5_EXPANSION_ETH_VLAN,
428 MLX5_EXPANSION_IPV4_UDP,
429 MLX5_EXPANSION_IPV4_TCP,
431 MLX5_EXPANSION_IPV6_UDP,
432 MLX5_EXPANSION_IPV6_TCP,
435 /** Supported expansion of items. */
436 static const struct mlx5_flow_expand_node mlx5_support_expansion[] = {
437 [MLX5_EXPANSION_ROOT] = {
438 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
440 MLX5_EXPANSION_IPV6),
441 .type = RTE_FLOW_ITEM_TYPE_END,
443 [MLX5_EXPANSION_ROOT_OUTER] = {
444 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH,
445 MLX5_EXPANSION_OUTER_IPV4,
446 MLX5_EXPANSION_OUTER_IPV6),
447 .type = RTE_FLOW_ITEM_TYPE_END,
449 [MLX5_EXPANSION_ROOT_ETH_VLAN] = {
450 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH_VLAN),
451 .type = RTE_FLOW_ITEM_TYPE_END,
453 [MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN] = {
454 .next = MLX5_FLOW_EXPAND_RSS_NEXT
455 (MLX5_EXPANSION_OUTER_ETH_VLAN),
456 .type = RTE_FLOW_ITEM_TYPE_END,
458 [MLX5_EXPANSION_OUTER_ETH] = {
459 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
460 MLX5_EXPANSION_OUTER_IPV6,
461 MLX5_EXPANSION_MPLS),
462 .type = RTE_FLOW_ITEM_TYPE_ETH,
465 [MLX5_EXPANSION_OUTER_ETH_VLAN] = {
466 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_VLAN),
467 .type = RTE_FLOW_ITEM_TYPE_ETH,
470 [MLX5_EXPANSION_OUTER_VLAN] = {
471 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
472 MLX5_EXPANSION_OUTER_IPV6),
473 .type = RTE_FLOW_ITEM_TYPE_VLAN,
475 [MLX5_EXPANSION_OUTER_IPV4] = {
476 .next = MLX5_FLOW_EXPAND_RSS_NEXT
477 (MLX5_EXPANSION_OUTER_IPV4_UDP,
478 MLX5_EXPANSION_OUTER_IPV4_TCP,
481 MLX5_EXPANSION_IPV6),
482 .type = RTE_FLOW_ITEM_TYPE_IPV4,
483 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
484 ETH_RSS_NONFRAG_IPV4_OTHER,
486 [MLX5_EXPANSION_OUTER_IPV4_UDP] = {
487 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
488 MLX5_EXPANSION_VXLAN_GPE),
489 .type = RTE_FLOW_ITEM_TYPE_UDP,
490 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
492 [MLX5_EXPANSION_OUTER_IPV4_TCP] = {
493 .type = RTE_FLOW_ITEM_TYPE_TCP,
494 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
496 [MLX5_EXPANSION_OUTER_IPV6] = {
497 .next = MLX5_FLOW_EXPAND_RSS_NEXT
498 (MLX5_EXPANSION_OUTER_IPV6_UDP,
499 MLX5_EXPANSION_OUTER_IPV6_TCP,
501 MLX5_EXPANSION_IPV6),
502 .type = RTE_FLOW_ITEM_TYPE_IPV6,
503 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
504 ETH_RSS_NONFRAG_IPV6_OTHER,
506 [MLX5_EXPANSION_OUTER_IPV6_UDP] = {
507 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
508 MLX5_EXPANSION_VXLAN_GPE),
509 .type = RTE_FLOW_ITEM_TYPE_UDP,
510 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
512 [MLX5_EXPANSION_OUTER_IPV6_TCP] = {
513 .type = RTE_FLOW_ITEM_TYPE_TCP,
514 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
516 [MLX5_EXPANSION_VXLAN] = {
517 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
519 MLX5_EXPANSION_IPV6),
520 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
522 [MLX5_EXPANSION_VXLAN_GPE] = {
523 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
525 MLX5_EXPANSION_IPV6),
526 .type = RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
528 [MLX5_EXPANSION_GRE] = {
529 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4),
530 .type = RTE_FLOW_ITEM_TYPE_GRE,
532 [MLX5_EXPANSION_MPLS] = {
533 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
534 MLX5_EXPANSION_IPV6),
535 .type = RTE_FLOW_ITEM_TYPE_MPLS,
537 [MLX5_EXPANSION_ETH] = {
538 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
539 MLX5_EXPANSION_IPV6),
540 .type = RTE_FLOW_ITEM_TYPE_ETH,
542 [MLX5_EXPANSION_ETH_VLAN] = {
543 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VLAN),
544 .type = RTE_FLOW_ITEM_TYPE_ETH,
546 [MLX5_EXPANSION_VLAN] = {
547 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
548 MLX5_EXPANSION_IPV6),
549 .type = RTE_FLOW_ITEM_TYPE_VLAN,
551 [MLX5_EXPANSION_IPV4] = {
552 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4_UDP,
553 MLX5_EXPANSION_IPV4_TCP),
554 .type = RTE_FLOW_ITEM_TYPE_IPV4,
555 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
556 ETH_RSS_NONFRAG_IPV4_OTHER,
558 [MLX5_EXPANSION_IPV4_UDP] = {
559 .type = RTE_FLOW_ITEM_TYPE_UDP,
560 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
562 [MLX5_EXPANSION_IPV4_TCP] = {
563 .type = RTE_FLOW_ITEM_TYPE_TCP,
564 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
566 [MLX5_EXPANSION_IPV6] = {
567 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV6_UDP,
568 MLX5_EXPANSION_IPV6_TCP),
569 .type = RTE_FLOW_ITEM_TYPE_IPV6,
570 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
571 ETH_RSS_NONFRAG_IPV6_OTHER,
573 [MLX5_EXPANSION_IPV6_UDP] = {
574 .type = RTE_FLOW_ITEM_TYPE_UDP,
575 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
577 [MLX5_EXPANSION_IPV6_TCP] = {
578 .type = RTE_FLOW_ITEM_TYPE_TCP,
579 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
583 static struct rte_flow_shared_action *
584 mlx5_shared_action_create(struct rte_eth_dev *dev,
585 const struct rte_flow_shared_action_conf *conf,
586 const struct rte_flow_action *action,
587 struct rte_flow_error *error);
588 static int mlx5_shared_action_destroy
589 (struct rte_eth_dev *dev,
590 struct rte_flow_shared_action *shared_action,
591 struct rte_flow_error *error);
592 static int mlx5_shared_action_update
593 (struct rte_eth_dev *dev,
594 struct rte_flow_shared_action *shared_action,
595 const struct rte_flow_action *action,
596 struct rte_flow_error *error);
597 static int mlx5_shared_action_query
598 (struct rte_eth_dev *dev,
599 const struct rte_flow_shared_action *action,
601 struct rte_flow_error *error);
603 mlx5_flow_tunnel_decap_set(struct rte_eth_dev *dev,
604 struct rte_flow_tunnel *app_tunnel,
605 struct rte_flow_action **actions,
606 uint32_t *num_of_actions,
607 struct rte_flow_error *error);
609 mlx5_flow_tunnel_match(struct rte_eth_dev *dev,
610 struct rte_flow_tunnel *app_tunnel,
611 struct rte_flow_item **items,
612 uint32_t *num_of_items,
613 struct rte_flow_error *error);
615 mlx5_flow_tunnel_item_release(struct rte_eth_dev *dev,
616 struct rte_flow_item *pmd_items,
617 uint32_t num_items, struct rte_flow_error *err);
619 mlx5_flow_tunnel_action_release(struct rte_eth_dev *dev,
620 struct rte_flow_action *pmd_actions,
621 uint32_t num_actions,
622 struct rte_flow_error *err);
624 mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
626 struct rte_flow_restore_info *info,
627 struct rte_flow_error *err);
629 static const struct rte_flow_ops mlx5_flow_ops = {
630 .validate = mlx5_flow_validate,
631 .create = mlx5_flow_create,
632 .destroy = mlx5_flow_destroy,
633 .flush = mlx5_flow_flush,
634 .isolate = mlx5_flow_isolate,
635 .query = mlx5_flow_query,
636 .dev_dump = mlx5_flow_dev_dump,
637 .get_aged_flows = mlx5_flow_get_aged_flows,
638 .shared_action_create = mlx5_shared_action_create,
639 .shared_action_destroy = mlx5_shared_action_destroy,
640 .shared_action_update = mlx5_shared_action_update,
641 .shared_action_query = mlx5_shared_action_query,
642 .tunnel_decap_set = mlx5_flow_tunnel_decap_set,
643 .tunnel_match = mlx5_flow_tunnel_match,
644 .tunnel_action_decap_release = mlx5_flow_tunnel_action_release,
645 .tunnel_item_release = mlx5_flow_tunnel_item_release,
646 .get_restore_info = mlx5_flow_tunnel_get_restore_info,
649 /* Tunnel information. */
650 struct mlx5_flow_tunnel_info {
651 uint64_t tunnel; /**< Tunnel bit (see MLX5_FLOW_*). */
652 uint32_t ptype; /**< Tunnel Ptype (see RTE_PTYPE_*). */
655 static struct mlx5_flow_tunnel_info tunnels_info[] = {
657 .tunnel = MLX5_FLOW_LAYER_VXLAN,
658 .ptype = RTE_PTYPE_TUNNEL_VXLAN | RTE_PTYPE_L4_UDP,
661 .tunnel = MLX5_FLOW_LAYER_GENEVE,
662 .ptype = RTE_PTYPE_TUNNEL_GENEVE | RTE_PTYPE_L4_UDP,
665 .tunnel = MLX5_FLOW_LAYER_VXLAN_GPE,
666 .ptype = RTE_PTYPE_TUNNEL_VXLAN_GPE | RTE_PTYPE_L4_UDP,
669 .tunnel = MLX5_FLOW_LAYER_GRE,
670 .ptype = RTE_PTYPE_TUNNEL_GRE,
673 .tunnel = MLX5_FLOW_LAYER_MPLS | MLX5_FLOW_LAYER_OUTER_L4_UDP,
674 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_UDP | RTE_PTYPE_L4_UDP,
677 .tunnel = MLX5_FLOW_LAYER_MPLS,
678 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
681 .tunnel = MLX5_FLOW_LAYER_NVGRE,
682 .ptype = RTE_PTYPE_TUNNEL_NVGRE,
685 .tunnel = MLX5_FLOW_LAYER_IPIP,
686 .ptype = RTE_PTYPE_TUNNEL_IP,
689 .tunnel = MLX5_FLOW_LAYER_IPV6_ENCAP,
690 .ptype = RTE_PTYPE_TUNNEL_IP,
693 .tunnel = MLX5_FLOW_LAYER_GTP,
694 .ptype = RTE_PTYPE_TUNNEL_GTPU,
698 /* Key of thread specific flow workspace data. */
699 static pthread_key_t key_workspace;
701 /* Thread specific flow workspace data once initialization data. */
702 static pthread_once_t key_workspace_init;
706 * Translate tag ID to register.
709 * Pointer to the Ethernet device structure.
711 * The feature that request the register.
713 * The request register ID.
715 * Error description in case of any.
718 * The request register on success, a negative errno
719 * value otherwise and rte_errno is set.
722 mlx5_flow_get_reg_id(struct rte_eth_dev *dev,
723 enum mlx5_feature_name feature,
725 struct rte_flow_error *error)
727 struct mlx5_priv *priv = dev->data->dev_private;
728 struct mlx5_dev_config *config = &priv->config;
729 enum modify_reg start_reg;
730 bool skip_mtr_reg = false;
733 case MLX5_HAIRPIN_RX:
735 case MLX5_HAIRPIN_TX:
737 case MLX5_METADATA_RX:
738 switch (config->dv_xmeta_en) {
739 case MLX5_XMETA_MODE_LEGACY:
741 case MLX5_XMETA_MODE_META16:
743 case MLX5_XMETA_MODE_META32:
747 case MLX5_METADATA_TX:
749 case MLX5_METADATA_FDB:
750 switch (config->dv_xmeta_en) {
751 case MLX5_XMETA_MODE_LEGACY:
753 case MLX5_XMETA_MODE_META16:
755 case MLX5_XMETA_MODE_META32:
760 switch (config->dv_xmeta_en) {
761 case MLX5_XMETA_MODE_LEGACY:
763 case MLX5_XMETA_MODE_META16:
765 case MLX5_XMETA_MODE_META32:
771 * If meter color and flow match share one register, flow match
772 * should use the meter color register for match.
774 if (priv->mtr_reg_share)
775 return priv->mtr_color_reg;
777 return priv->mtr_color_reg != REG_C_2 ? REG_C_2 :
780 MLX5_ASSERT(priv->mtr_color_reg != REG_NON);
781 return priv->mtr_color_reg;
784 * Metadata COPY_MARK register using is in meter suffix sub
785 * flow while with meter. It's safe to share the same register.
787 return priv->mtr_color_reg != REG_C_2 ? REG_C_2 : REG_C_3;
790 * If meter is enable, it will engage the register for color
791 * match and flow match. If meter color match is not using the
792 * REG_C_2, need to skip the REG_C_x be used by meter color
794 * If meter is disable, free to use all available registers.
796 start_reg = priv->mtr_color_reg != REG_C_2 ? REG_C_2 :
797 (priv->mtr_reg_share ? REG_C_3 : REG_C_4);
798 skip_mtr_reg = !!(priv->mtr_en && start_reg == REG_C_2);
799 if (id > (REG_C_7 - start_reg))
800 return rte_flow_error_set(error, EINVAL,
801 RTE_FLOW_ERROR_TYPE_ITEM,
802 NULL, "invalid tag id");
803 if (config->flow_mreg_c[id + start_reg - REG_C_0] == REG_NON)
804 return rte_flow_error_set(error, ENOTSUP,
805 RTE_FLOW_ERROR_TYPE_ITEM,
806 NULL, "unsupported tag id");
808 * This case means meter is using the REG_C_x great than 2.
809 * Take care not to conflict with meter color REG_C_x.
810 * If the available index REG_C_y >= REG_C_x, skip the
813 if (skip_mtr_reg && config->flow_mreg_c
814 [id + start_reg - REG_C_0] >= priv->mtr_color_reg) {
815 if (id >= (REG_C_7 - start_reg))
816 return rte_flow_error_set(error, EINVAL,
817 RTE_FLOW_ERROR_TYPE_ITEM,
818 NULL, "invalid tag id");
819 if (config->flow_mreg_c
820 [id + 1 + start_reg - REG_C_0] != REG_NON)
821 return config->flow_mreg_c
822 [id + 1 + start_reg - REG_C_0];
823 return rte_flow_error_set(error, ENOTSUP,
824 RTE_FLOW_ERROR_TYPE_ITEM,
825 NULL, "unsupported tag id");
827 return config->flow_mreg_c[id + start_reg - REG_C_0];
830 return rte_flow_error_set(error, EINVAL,
831 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
832 NULL, "invalid feature name");
836 * Check extensive flow metadata register support.
839 * Pointer to rte_eth_dev structure.
842 * True if device supports extensive flow metadata register, otherwise false.
845 mlx5_flow_ext_mreg_supported(struct rte_eth_dev *dev)
847 struct mlx5_priv *priv = dev->data->dev_private;
848 struct mlx5_dev_config *config = &priv->config;
851 * Having available reg_c can be regarded inclusively as supporting
852 * extensive flow metadata register, which could mean,
853 * - metadata register copy action by modify header.
854 * - 16 modify header actions is supported.
855 * - reg_c's are preserved across different domain (FDB and NIC) on
856 * packet loopback by flow lookup miss.
858 return config->flow_mreg_c[2] != REG_NON;
862 * Verify the @p item specifications (spec, last, mask) are compatible with the
866 * Item specification.
868 * @p item->mask or flow default bit-masks.
869 * @param[in] nic_mask
870 * Bit-masks covering supported fields by the NIC to compare with user mask.
872 * Bit-masks size in bytes.
873 * @param[in] range_accepted
874 * True if range of values is accepted for specific fields, false otherwise.
876 * Pointer to error structure.
879 * 0 on success, a negative errno value otherwise and rte_errno is set.
882 mlx5_flow_item_acceptable(const struct rte_flow_item *item,
884 const uint8_t *nic_mask,
887 struct rte_flow_error *error)
891 MLX5_ASSERT(nic_mask);
892 for (i = 0; i < size; ++i)
893 if ((nic_mask[i] | mask[i]) != nic_mask[i])
894 return rte_flow_error_set(error, ENOTSUP,
895 RTE_FLOW_ERROR_TYPE_ITEM,
897 "mask enables non supported"
899 if (!item->spec && (item->mask || item->last))
900 return rte_flow_error_set(error, EINVAL,
901 RTE_FLOW_ERROR_TYPE_ITEM, item,
902 "mask/last without a spec is not"
904 if (item->spec && item->last && !range_accepted) {
910 for (i = 0; i < size; ++i) {
911 spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
912 last[i] = ((const uint8_t *)item->last)[i] & mask[i];
914 ret = memcmp(spec, last, size);
916 return rte_flow_error_set(error, EINVAL,
917 RTE_FLOW_ERROR_TYPE_ITEM,
919 "range is not valid");
925 * Adjust the hash fields according to the @p flow information.
927 * @param[in] dev_flow.
928 * Pointer to the mlx5_flow.
930 * 1 when the hash field is for a tunnel item.
931 * @param[in] layer_types
933 * @param[in] hash_fields
937 * The hash fields that should be used.
940 mlx5_flow_hashfields_adjust(struct mlx5_flow_rss_desc *rss_desc,
941 int tunnel __rte_unused, uint64_t layer_types,
942 uint64_t hash_fields)
944 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
945 int rss_request_inner = rss_desc->level >= 2;
947 /* Check RSS hash level for tunnel. */
948 if (tunnel && rss_request_inner)
949 hash_fields |= IBV_RX_HASH_INNER;
950 else if (tunnel || rss_request_inner)
953 /* Check if requested layer matches RSS hash fields. */
954 if (!(rss_desc->types & layer_types))
960 * Lookup and set the ptype in the data Rx part. A single Ptype can be used,
961 * if several tunnel rules are used on this queue, the tunnel ptype will be
965 * Rx queue to update.
968 flow_rxq_tunnel_ptype_update(struct mlx5_rxq_ctrl *rxq_ctrl)
971 uint32_t tunnel_ptype = 0;
973 /* Look up for the ptype to use. */
974 for (i = 0; i != MLX5_FLOW_TUNNEL; ++i) {
975 if (!rxq_ctrl->flow_tunnels_n[i])
978 tunnel_ptype = tunnels_info[i].ptype;
984 rxq_ctrl->rxq.tunnel = tunnel_ptype;
988 * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) according to the devive
992 * Pointer to the Ethernet device structure.
993 * @param[in] dev_handle
994 * Pointer to device flow handle structure.
997 flow_drv_rxq_flags_set(struct rte_eth_dev *dev,
998 struct mlx5_flow_handle *dev_handle)
1000 struct mlx5_priv *priv = dev->data->dev_private;
1001 const int mark = dev_handle->mark;
1002 const int tunnel = !!(dev_handle->layers & MLX5_FLOW_LAYER_TUNNEL);
1003 struct mlx5_hrxq *hrxq;
1006 if (dev_handle->fate_action != MLX5_FLOW_FATE_QUEUE)
1008 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1009 dev_handle->rix_hrxq);
1012 for (i = 0; i != hrxq->ind_table->queues_n; ++i) {
1013 int idx = hrxq->ind_table->queues[i];
1014 struct mlx5_rxq_ctrl *rxq_ctrl =
1015 container_of((*priv->rxqs)[idx],
1016 struct mlx5_rxq_ctrl, rxq);
1019 * To support metadata register copy on Tx loopback,
1020 * this must be always enabled (metadata may arive
1021 * from other port - not from local flows only.
1023 if (priv->config.dv_flow_en &&
1024 priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1025 mlx5_flow_ext_mreg_supported(dev)) {
1026 rxq_ctrl->rxq.mark = 1;
1027 rxq_ctrl->flow_mark_n = 1;
1029 rxq_ctrl->rxq.mark = 1;
1030 rxq_ctrl->flow_mark_n++;
1035 /* Increase the counter matching the flow. */
1036 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
1037 if ((tunnels_info[j].tunnel &
1038 dev_handle->layers) ==
1039 tunnels_info[j].tunnel) {
1040 rxq_ctrl->flow_tunnels_n[j]++;
1044 flow_rxq_tunnel_ptype_update(rxq_ctrl);
1050 * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) for a flow
1053 * Pointer to the Ethernet device structure.
1055 * Pointer to flow structure.
1058 flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
1060 struct mlx5_priv *priv = dev->data->dev_private;
1061 uint32_t handle_idx;
1062 struct mlx5_flow_handle *dev_handle;
1064 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1065 handle_idx, dev_handle, next)
1066 flow_drv_rxq_flags_set(dev, dev_handle);
1070 * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
1071 * device flow if no other flow uses it with the same kind of request.
1074 * Pointer to Ethernet device.
1075 * @param[in] dev_handle
1076 * Pointer to the device flow handle structure.
1079 flow_drv_rxq_flags_trim(struct rte_eth_dev *dev,
1080 struct mlx5_flow_handle *dev_handle)
1082 struct mlx5_priv *priv = dev->data->dev_private;
1083 const int mark = dev_handle->mark;
1084 const int tunnel = !!(dev_handle->layers & MLX5_FLOW_LAYER_TUNNEL);
1085 struct mlx5_hrxq *hrxq;
1088 if (dev_handle->fate_action != MLX5_FLOW_FATE_QUEUE)
1090 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1091 dev_handle->rix_hrxq);
1094 MLX5_ASSERT(dev->data->dev_started);
1095 for (i = 0; i != hrxq->ind_table->queues_n; ++i) {
1096 int idx = hrxq->ind_table->queues[i];
1097 struct mlx5_rxq_ctrl *rxq_ctrl =
1098 container_of((*priv->rxqs)[idx],
1099 struct mlx5_rxq_ctrl, rxq);
1101 if (priv->config.dv_flow_en &&
1102 priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1103 mlx5_flow_ext_mreg_supported(dev)) {
1104 rxq_ctrl->rxq.mark = 1;
1105 rxq_ctrl->flow_mark_n = 1;
1107 rxq_ctrl->flow_mark_n--;
1108 rxq_ctrl->rxq.mark = !!rxq_ctrl->flow_mark_n;
1113 /* Decrease the counter matching the flow. */
1114 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
1115 if ((tunnels_info[j].tunnel &
1116 dev_handle->layers) ==
1117 tunnels_info[j].tunnel) {
1118 rxq_ctrl->flow_tunnels_n[j]--;
1122 flow_rxq_tunnel_ptype_update(rxq_ctrl);
1128 * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
1129 * @p flow if no other flow uses it with the same kind of request.
1132 * Pointer to Ethernet device.
1134 * Pointer to the flow.
1137 flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
1139 struct mlx5_priv *priv = dev->data->dev_private;
1140 uint32_t handle_idx;
1141 struct mlx5_flow_handle *dev_handle;
1143 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1144 handle_idx, dev_handle, next)
1145 flow_drv_rxq_flags_trim(dev, dev_handle);
1149 * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
1152 * Pointer to Ethernet device.
1155 flow_rxq_flags_clear(struct rte_eth_dev *dev)
1157 struct mlx5_priv *priv = dev->data->dev_private;
1160 for (i = 0; i != priv->rxqs_n; ++i) {
1161 struct mlx5_rxq_ctrl *rxq_ctrl;
1164 if (!(*priv->rxqs)[i])
1166 rxq_ctrl = container_of((*priv->rxqs)[i],
1167 struct mlx5_rxq_ctrl, rxq);
1168 rxq_ctrl->flow_mark_n = 0;
1169 rxq_ctrl->rxq.mark = 0;
1170 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
1171 rxq_ctrl->flow_tunnels_n[j] = 0;
1172 rxq_ctrl->rxq.tunnel = 0;
1177 * Set the Rx queue dynamic metadata (mask and offset) for a flow
1180 * Pointer to the Ethernet device structure.
1183 mlx5_flow_rxq_dynf_metadata_set(struct rte_eth_dev *dev)
1185 struct mlx5_priv *priv = dev->data->dev_private;
1186 struct mlx5_rxq_data *data;
1189 for (i = 0; i != priv->rxqs_n; ++i) {
1190 if (!(*priv->rxqs)[i])
1192 data = (*priv->rxqs)[i];
1193 if (!rte_flow_dynf_metadata_avail()) {
1194 data->dynf_meta = 0;
1195 data->flow_meta_mask = 0;
1196 data->flow_meta_offset = -1;
1198 data->dynf_meta = 1;
1199 data->flow_meta_mask = rte_flow_dynf_metadata_mask;
1200 data->flow_meta_offset = rte_flow_dynf_metadata_offs;
1206 * return a pointer to the desired action in the list of actions.
1208 * @param[in] actions
1209 * The list of actions to search the action in.
1211 * The action to find.
1214 * Pointer to the action in the list, if found. NULL otherwise.
1216 const struct rte_flow_action *
1217 mlx5_flow_find_action(const struct rte_flow_action *actions,
1218 enum rte_flow_action_type action)
1220 if (actions == NULL)
1222 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++)
1223 if (actions->type == action)
1229 * Validate the flag action.
1231 * @param[in] action_flags
1232 * Bit-fields that holds the actions detected until now.
1234 * Attributes of flow that includes this action.
1236 * Pointer to error structure.
1239 * 0 on success, a negative errno value otherwise and rte_errno is set.
1242 mlx5_flow_validate_action_flag(uint64_t action_flags,
1243 const struct rte_flow_attr *attr,
1244 struct rte_flow_error *error)
1246 if (action_flags & MLX5_FLOW_ACTION_MARK)
1247 return rte_flow_error_set(error, EINVAL,
1248 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1249 "can't mark and flag in same flow");
1250 if (action_flags & MLX5_FLOW_ACTION_FLAG)
1251 return rte_flow_error_set(error, EINVAL,
1252 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1254 " actions in same flow");
1256 return rte_flow_error_set(error, ENOTSUP,
1257 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1258 "flag action not supported for "
1264 * Validate the mark action.
1267 * Pointer to the queue action.
1268 * @param[in] action_flags
1269 * Bit-fields that holds the actions detected until now.
1271 * Attributes of flow that includes this action.
1273 * Pointer to error structure.
1276 * 0 on success, a negative errno value otherwise and rte_errno is set.
1279 mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
1280 uint64_t action_flags,
1281 const struct rte_flow_attr *attr,
1282 struct rte_flow_error *error)
1284 const struct rte_flow_action_mark *mark = action->conf;
1287 return rte_flow_error_set(error, EINVAL,
1288 RTE_FLOW_ERROR_TYPE_ACTION,
1290 "configuration cannot be null");
1291 if (mark->id >= MLX5_FLOW_MARK_MAX)
1292 return rte_flow_error_set(error, EINVAL,
1293 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1295 "mark id must in 0 <= id < "
1296 RTE_STR(MLX5_FLOW_MARK_MAX));
1297 if (action_flags & MLX5_FLOW_ACTION_FLAG)
1298 return rte_flow_error_set(error, EINVAL,
1299 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1300 "can't flag and mark in same flow");
1301 if (action_flags & MLX5_FLOW_ACTION_MARK)
1302 return rte_flow_error_set(error, EINVAL,
1303 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1304 "can't have 2 mark actions in same"
1307 return rte_flow_error_set(error, ENOTSUP,
1308 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1309 "mark action not supported for "
1315 * Validate the drop action.
1317 * @param[in] action_flags
1318 * Bit-fields that holds the actions detected until now.
1320 * Attributes of flow that includes this action.
1322 * Pointer to error structure.
1325 * 0 on success, a negative errno value otherwise and rte_errno is set.
1328 mlx5_flow_validate_action_drop(uint64_t action_flags __rte_unused,
1329 const struct rte_flow_attr *attr,
1330 struct rte_flow_error *error)
1333 return rte_flow_error_set(error, ENOTSUP,
1334 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1335 "drop action not supported for "
1341 * Validate the queue action.
1344 * Pointer to the queue action.
1345 * @param[in] action_flags
1346 * Bit-fields that holds the actions detected until now.
1348 * Pointer to the Ethernet device structure.
1350 * Attributes of flow that includes this action.
1352 * Pointer to error structure.
1355 * 0 on success, a negative errno value otherwise and rte_errno is set.
1358 mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
1359 uint64_t action_flags,
1360 struct rte_eth_dev *dev,
1361 const struct rte_flow_attr *attr,
1362 struct rte_flow_error *error)
1364 struct mlx5_priv *priv = dev->data->dev_private;
1365 const struct rte_flow_action_queue *queue = action->conf;
1367 if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1368 return rte_flow_error_set(error, EINVAL,
1369 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1370 "can't have 2 fate actions in"
1373 return rte_flow_error_set(error, EINVAL,
1374 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1375 NULL, "No Rx queues configured");
1376 if (queue->index >= priv->rxqs_n)
1377 return rte_flow_error_set(error, EINVAL,
1378 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1380 "queue index out of range");
1381 if (!(*priv->rxqs)[queue->index])
1382 return rte_flow_error_set(error, EINVAL,
1383 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1385 "queue is not configured");
1387 return rte_flow_error_set(error, ENOTSUP,
1388 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1389 "queue action not supported for "
1395 * Validate the rss action.
1398 * Pointer to the Ethernet device structure.
1400 * Pointer to the queue action.
1402 * Pointer to error structure.
1405 * 0 on success, a negative errno value otherwise and rte_errno is set.
1408 mlx5_validate_action_rss(struct rte_eth_dev *dev,
1409 const struct rte_flow_action *action,
1410 struct rte_flow_error *error)
1412 struct mlx5_priv *priv = dev->data->dev_private;
1413 const struct rte_flow_action_rss *rss = action->conf;
1416 if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
1417 rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
1418 return rte_flow_error_set(error, ENOTSUP,
1419 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1421 "RSS hash function not supported");
1422 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1427 return rte_flow_error_set(error, ENOTSUP,
1428 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1430 "tunnel RSS is not supported");
1431 /* allow RSS key_len 0 in case of NULL (default) RSS key. */
1432 if (rss->key_len == 0 && rss->key != NULL)
1433 return rte_flow_error_set(error, ENOTSUP,
1434 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1436 "RSS hash key length 0");
1437 if (rss->key_len > 0 && rss->key_len < MLX5_RSS_HASH_KEY_LEN)
1438 return rte_flow_error_set(error, ENOTSUP,
1439 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1441 "RSS hash key too small");
1442 if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
1443 return rte_flow_error_set(error, ENOTSUP,
1444 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1446 "RSS hash key too large");
1447 if (rss->queue_num > priv->config.ind_table_max_size)
1448 return rte_flow_error_set(error, ENOTSUP,
1449 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1451 "number of queues too large");
1452 if (rss->types & MLX5_RSS_HF_MASK)
1453 return rte_flow_error_set(error, ENOTSUP,
1454 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1456 "some RSS protocols are not"
1458 if ((rss->types & (ETH_RSS_L3_SRC_ONLY | ETH_RSS_L3_DST_ONLY)) &&
1459 !(rss->types & ETH_RSS_IP))
1460 return rte_flow_error_set(error, EINVAL,
1461 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1462 "L3 partial RSS requested but L3 RSS"
1463 " type not specified");
1464 if ((rss->types & (ETH_RSS_L4_SRC_ONLY | ETH_RSS_L4_DST_ONLY)) &&
1465 !(rss->types & (ETH_RSS_UDP | ETH_RSS_TCP)))
1466 return rte_flow_error_set(error, EINVAL,
1467 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1468 "L4 partial RSS requested but L4 RSS"
1469 " type not specified");
1471 return rte_flow_error_set(error, EINVAL,
1472 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1473 NULL, "No Rx queues configured");
1474 if (!rss->queue_num)
1475 return rte_flow_error_set(error, EINVAL,
1476 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1477 NULL, "No queues configured");
1478 for (i = 0; i != rss->queue_num; ++i) {
1479 if (rss->queue[i] >= priv->rxqs_n)
1480 return rte_flow_error_set
1482 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1483 &rss->queue[i], "queue index out of range");
1484 if (!(*priv->rxqs)[rss->queue[i]])
1485 return rte_flow_error_set
1486 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1487 &rss->queue[i], "queue is not configured");
1493 * Validate the rss action.
1496 * Pointer to the queue action.
1497 * @param[in] action_flags
1498 * Bit-fields that holds the actions detected until now.
1500 * Pointer to the Ethernet device structure.
1502 * Attributes of flow that includes this action.
1503 * @param[in] item_flags
1504 * Items that were detected.
1506 * Pointer to error structure.
1509 * 0 on success, a negative errno value otherwise and rte_errno is set.
1512 mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
1513 uint64_t action_flags,
1514 struct rte_eth_dev *dev,
1515 const struct rte_flow_attr *attr,
1516 uint64_t item_flags,
1517 struct rte_flow_error *error)
1519 const struct rte_flow_action_rss *rss = action->conf;
1520 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1523 if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1524 return rte_flow_error_set(error, EINVAL,
1525 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1526 "can't have 2 fate actions"
1528 ret = mlx5_validate_action_rss(dev, action, error);
1532 return rte_flow_error_set(error, ENOTSUP,
1533 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1534 "rss action not supported for "
1536 if (rss->level > 1 && !tunnel)
1537 return rte_flow_error_set(error, EINVAL,
1538 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1539 "inner RSS is not supported for "
1540 "non-tunnel flows");
1541 if ((item_flags & MLX5_FLOW_LAYER_ECPRI) &&
1542 !(item_flags & MLX5_FLOW_LAYER_INNER_L4_UDP)) {
1543 return rte_flow_error_set(error, EINVAL,
1544 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1545 "RSS on eCPRI is not supported now");
1551 * Validate the default miss action.
1553 * @param[in] action_flags
1554 * Bit-fields that holds the actions detected until now.
1556 * Pointer to error structure.
1559 * 0 on success, a negative errno value otherwise and rte_errno is set.
1562 mlx5_flow_validate_action_default_miss(uint64_t action_flags,
1563 const struct rte_flow_attr *attr,
1564 struct rte_flow_error *error)
1566 if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1567 return rte_flow_error_set(error, EINVAL,
1568 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1569 "can't have 2 fate actions in"
1572 return rte_flow_error_set(error, ENOTSUP,
1573 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1574 "default miss action not supported "
1577 return rte_flow_error_set(error, ENOTSUP,
1578 RTE_FLOW_ERROR_TYPE_ATTR_GROUP, NULL,
1579 "only group 0 is supported");
1581 return rte_flow_error_set(error, ENOTSUP,
1582 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1583 NULL, "transfer is not supported");
1588 * Validate the count action.
1591 * Pointer to the Ethernet device structure.
1593 * Attributes of flow that includes this action.
1595 * Pointer to error structure.
1598 * 0 on success, a negative errno value otherwise and rte_errno is set.
1601 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
1602 const struct rte_flow_attr *attr,
1603 struct rte_flow_error *error)
1606 return rte_flow_error_set(error, ENOTSUP,
1607 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1608 "count action not supported for "
1614 * Verify the @p attributes will be correctly understood by the NIC and store
1615 * them in the @p flow if everything is correct.
1618 * Pointer to the Ethernet device structure.
1619 * @param[in] attributes
1620 * Pointer to flow attributes
1622 * Pointer to error structure.
1625 * 0 on success, a negative errno value otherwise and rte_errno is set.
1628 mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
1629 const struct rte_flow_attr *attributes,
1630 struct rte_flow_error *error)
1632 struct mlx5_priv *priv = dev->data->dev_private;
1633 uint32_t priority_max = priv->config.flow_prio - 1;
1635 if (attributes->group)
1636 return rte_flow_error_set(error, ENOTSUP,
1637 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1638 NULL, "groups is not supported");
1639 if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
1640 attributes->priority >= priority_max)
1641 return rte_flow_error_set(error, ENOTSUP,
1642 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1643 NULL, "priority out of range");
1644 if (attributes->egress)
1645 return rte_flow_error_set(error, ENOTSUP,
1646 RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1647 "egress is not supported");
1648 if (attributes->transfer && !priv->config.dv_esw_en)
1649 return rte_flow_error_set(error, ENOTSUP,
1650 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1651 NULL, "transfer is not supported");
1652 if (!attributes->ingress)
1653 return rte_flow_error_set(error, EINVAL,
1654 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1656 "ingress attribute is mandatory");
1661 * Validate ICMP6 item.
1664 * Item specification.
1665 * @param[in] item_flags
1666 * Bit-fields that holds the items detected until now.
1667 * @param[in] ext_vlan_sup
1668 * Whether extended VLAN features are supported or not.
1670 * Pointer to error structure.
1673 * 0 on success, a negative errno value otherwise and rte_errno is set.
1676 mlx5_flow_validate_item_icmp6(const struct rte_flow_item *item,
1677 uint64_t item_flags,
1678 uint8_t target_protocol,
1679 struct rte_flow_error *error)
1681 const struct rte_flow_item_icmp6 *mask = item->mask;
1682 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1683 const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1684 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1685 const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1686 MLX5_FLOW_LAYER_OUTER_L4;
1689 if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMPV6)
1690 return rte_flow_error_set(error, EINVAL,
1691 RTE_FLOW_ERROR_TYPE_ITEM, item,
1692 "protocol filtering not compatible"
1693 " with ICMP6 layer");
1694 if (!(item_flags & l3m))
1695 return rte_flow_error_set(error, EINVAL,
1696 RTE_FLOW_ERROR_TYPE_ITEM, item,
1697 "IPv6 is mandatory to filter on"
1699 if (item_flags & l4m)
1700 return rte_flow_error_set(error, EINVAL,
1701 RTE_FLOW_ERROR_TYPE_ITEM, item,
1702 "multiple L4 layers not supported");
1704 mask = &rte_flow_item_icmp6_mask;
1705 ret = mlx5_flow_item_acceptable
1706 (item, (const uint8_t *)mask,
1707 (const uint8_t *)&rte_flow_item_icmp6_mask,
1708 sizeof(struct rte_flow_item_icmp6),
1709 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1716 * Validate ICMP item.
1719 * Item specification.
1720 * @param[in] item_flags
1721 * Bit-fields that holds the items detected until now.
1723 * Pointer to error structure.
1726 * 0 on success, a negative errno value otherwise and rte_errno is set.
1729 mlx5_flow_validate_item_icmp(const struct rte_flow_item *item,
1730 uint64_t item_flags,
1731 uint8_t target_protocol,
1732 struct rte_flow_error *error)
1734 const struct rte_flow_item_icmp *mask = item->mask;
1735 const struct rte_flow_item_icmp nic_mask = {
1736 .hdr.icmp_type = 0xff,
1737 .hdr.icmp_code = 0xff,
1738 .hdr.icmp_ident = RTE_BE16(0xffff),
1739 .hdr.icmp_seq_nb = RTE_BE16(0xffff),
1741 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1742 const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1743 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1744 const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1745 MLX5_FLOW_LAYER_OUTER_L4;
1748 if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMP)
1749 return rte_flow_error_set(error, EINVAL,
1750 RTE_FLOW_ERROR_TYPE_ITEM, item,
1751 "protocol filtering not compatible"
1752 " with ICMP layer");
1753 if (!(item_flags & l3m))
1754 return rte_flow_error_set(error, EINVAL,
1755 RTE_FLOW_ERROR_TYPE_ITEM, item,
1756 "IPv4 is mandatory to filter"
1758 if (item_flags & l4m)
1759 return rte_flow_error_set(error, EINVAL,
1760 RTE_FLOW_ERROR_TYPE_ITEM, item,
1761 "multiple L4 layers not supported");
1764 ret = mlx5_flow_item_acceptable
1765 (item, (const uint8_t *)mask,
1766 (const uint8_t *)&nic_mask,
1767 sizeof(struct rte_flow_item_icmp),
1768 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1775 * Validate Ethernet item.
1778 * Item specification.
1779 * @param[in] item_flags
1780 * Bit-fields that holds the items detected until now.
1782 * Pointer to error structure.
1785 * 0 on success, a negative errno value otherwise and rte_errno is set.
1788 mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
1789 uint64_t item_flags, bool ext_vlan_sup,
1790 struct rte_flow_error *error)
1792 const struct rte_flow_item_eth *mask = item->mask;
1793 const struct rte_flow_item_eth nic_mask = {
1794 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1795 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1796 .type = RTE_BE16(0xffff),
1797 .has_vlan = ext_vlan_sup ? 1 : 0,
1800 int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1801 const uint64_t ethm = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1802 MLX5_FLOW_LAYER_OUTER_L2;
1804 if (item_flags & ethm)
1805 return rte_flow_error_set(error, ENOTSUP,
1806 RTE_FLOW_ERROR_TYPE_ITEM, item,
1807 "multiple L2 layers not supported");
1808 if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_L3)) ||
1809 (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_L3)))
1810 return rte_flow_error_set(error, EINVAL,
1811 RTE_FLOW_ERROR_TYPE_ITEM, item,
1812 "L2 layer should not follow "
1814 if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN)) ||
1815 (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_VLAN)))
1816 return rte_flow_error_set(error, EINVAL,
1817 RTE_FLOW_ERROR_TYPE_ITEM, item,
1818 "L2 layer should not follow VLAN");
1820 mask = &rte_flow_item_eth_mask;
1821 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1822 (const uint8_t *)&nic_mask,
1823 sizeof(struct rte_flow_item_eth),
1824 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1829 * Validate VLAN item.
1832 * Item specification.
1833 * @param[in] item_flags
1834 * Bit-fields that holds the items detected until now.
1836 * Ethernet device flow is being created on.
1838 * Pointer to error structure.
1841 * 0 on success, a negative errno value otherwise and rte_errno is set.
1844 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
1845 uint64_t item_flags,
1846 struct rte_eth_dev *dev,
1847 struct rte_flow_error *error)
1849 const struct rte_flow_item_vlan *spec = item->spec;
1850 const struct rte_flow_item_vlan *mask = item->mask;
1851 const struct rte_flow_item_vlan nic_mask = {
1852 .tci = RTE_BE16(UINT16_MAX),
1853 .inner_type = RTE_BE16(UINT16_MAX),
1855 uint16_t vlan_tag = 0;
1856 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1858 const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1859 MLX5_FLOW_LAYER_INNER_L4) :
1860 (MLX5_FLOW_LAYER_OUTER_L3 |
1861 MLX5_FLOW_LAYER_OUTER_L4);
1862 const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1863 MLX5_FLOW_LAYER_OUTER_VLAN;
1865 if (item_flags & vlanm)
1866 return rte_flow_error_set(error, EINVAL,
1867 RTE_FLOW_ERROR_TYPE_ITEM, item,
1868 "multiple VLAN layers not supported");
1869 else if ((item_flags & l34m) != 0)
1870 return rte_flow_error_set(error, EINVAL,
1871 RTE_FLOW_ERROR_TYPE_ITEM, item,
1872 "VLAN cannot follow L3/L4 layer");
1874 mask = &rte_flow_item_vlan_mask;
1875 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1876 (const uint8_t *)&nic_mask,
1877 sizeof(struct rte_flow_item_vlan),
1878 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1881 if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
1882 struct mlx5_priv *priv = dev->data->dev_private;
1884 if (priv->vmwa_context) {
1886 * Non-NULL context means we have a virtual machine
1887 * and SR-IOV enabled, we have to create VLAN interface
1888 * to make hypervisor to setup E-Switch vport
1889 * context correctly. We avoid creating the multiple
1890 * VLAN interfaces, so we cannot support VLAN tag mask.
1892 return rte_flow_error_set(error, EINVAL,
1893 RTE_FLOW_ERROR_TYPE_ITEM,
1895 "VLAN tag mask is not"
1896 " supported in virtual"
1901 vlan_tag = spec->tci;
1902 vlan_tag &= mask->tci;
1905 * From verbs perspective an empty VLAN is equivalent
1906 * to a packet without VLAN layer.
1909 return rte_flow_error_set(error, EINVAL,
1910 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1912 "VLAN cannot be empty");
1917 * Validate IPV4 item.
1920 * Item specification.
1921 * @param[in] item_flags
1922 * Bit-fields that holds the items detected until now.
1923 * @param[in] last_item
1924 * Previous validated item in the pattern items.
1925 * @param[in] ether_type
1926 * Type in the ethernet layer header (including dot1q).
1927 * @param[in] acc_mask
1928 * Acceptable mask, if NULL default internal default mask
1929 * will be used to check whether item fields are supported.
1930 * @param[in] range_accepted
1931 * True if range of values is accepted for specific fields, false otherwise.
1933 * Pointer to error structure.
1936 * 0 on success, a negative errno value otherwise and rte_errno is set.
1939 mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
1940 uint64_t item_flags,
1942 uint16_t ether_type,
1943 const struct rte_flow_item_ipv4 *acc_mask,
1944 bool range_accepted,
1945 struct rte_flow_error *error)
1947 const struct rte_flow_item_ipv4 *mask = item->mask;
1948 const struct rte_flow_item_ipv4 *spec = item->spec;
1949 const struct rte_flow_item_ipv4 nic_mask = {
1951 .src_addr = RTE_BE32(0xffffffff),
1952 .dst_addr = RTE_BE32(0xffffffff),
1953 .type_of_service = 0xff,
1954 .next_proto_id = 0xff,
1957 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1958 const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1959 MLX5_FLOW_LAYER_OUTER_L3;
1960 const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1961 MLX5_FLOW_LAYER_OUTER_L4;
1963 uint8_t next_proto = 0xFF;
1964 const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
1965 MLX5_FLOW_LAYER_OUTER_VLAN |
1966 MLX5_FLOW_LAYER_INNER_VLAN);
1968 if ((last_item & l2_vlan) && ether_type &&
1969 ether_type != RTE_ETHER_TYPE_IPV4)
1970 return rte_flow_error_set(error, EINVAL,
1971 RTE_FLOW_ERROR_TYPE_ITEM, item,
1972 "IPv4 cannot follow L2/VLAN layer "
1973 "which ether type is not IPv4");
1974 if (item_flags & MLX5_FLOW_LAYER_IPIP) {
1976 next_proto = mask->hdr.next_proto_id &
1977 spec->hdr.next_proto_id;
1978 if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
1979 return rte_flow_error_set(error, EINVAL,
1980 RTE_FLOW_ERROR_TYPE_ITEM,
1985 if (item_flags & MLX5_FLOW_LAYER_IPV6_ENCAP)
1986 return rte_flow_error_set(error, EINVAL,
1987 RTE_FLOW_ERROR_TYPE_ITEM, item,
1988 "wrong tunnel type - IPv6 specified "
1989 "but IPv4 item provided");
1990 if (item_flags & l3m)
1991 return rte_flow_error_set(error, ENOTSUP,
1992 RTE_FLOW_ERROR_TYPE_ITEM, item,
1993 "multiple L3 layers not supported");
1994 else if (item_flags & l4m)
1995 return rte_flow_error_set(error, EINVAL,
1996 RTE_FLOW_ERROR_TYPE_ITEM, item,
1997 "L3 cannot follow an L4 layer.");
1998 else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
1999 !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
2000 return rte_flow_error_set(error, EINVAL,
2001 RTE_FLOW_ERROR_TYPE_ITEM, item,
2002 "L3 cannot follow an NVGRE layer.");
2004 mask = &rte_flow_item_ipv4_mask;
2005 else if (mask->hdr.next_proto_id != 0 &&
2006 mask->hdr.next_proto_id != 0xff)
2007 return rte_flow_error_set(error, EINVAL,
2008 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
2009 "partial mask is not supported"
2011 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2012 acc_mask ? (const uint8_t *)acc_mask
2013 : (const uint8_t *)&nic_mask,
2014 sizeof(struct rte_flow_item_ipv4),
2015 range_accepted, error);
2022 * Validate IPV6 item.
2025 * Item specification.
2026 * @param[in] item_flags
2027 * Bit-fields that holds the items detected until now.
2028 * @param[in] last_item
2029 * Previous validated item in the pattern items.
2030 * @param[in] ether_type
2031 * Type in the ethernet layer header (including dot1q).
2032 * @param[in] acc_mask
2033 * Acceptable mask, if NULL default internal default mask
2034 * will be used to check whether item fields are supported.
2036 * Pointer to error structure.
2039 * 0 on success, a negative errno value otherwise and rte_errno is set.
2042 mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
2043 uint64_t item_flags,
2045 uint16_t ether_type,
2046 const struct rte_flow_item_ipv6 *acc_mask,
2047 struct rte_flow_error *error)
2049 const struct rte_flow_item_ipv6 *mask = item->mask;
2050 const struct rte_flow_item_ipv6 *spec = item->spec;
2051 const struct rte_flow_item_ipv6 nic_mask = {
2054 "\xff\xff\xff\xff\xff\xff\xff\xff"
2055 "\xff\xff\xff\xff\xff\xff\xff\xff",
2057 "\xff\xff\xff\xff\xff\xff\xff\xff"
2058 "\xff\xff\xff\xff\xff\xff\xff\xff",
2059 .vtc_flow = RTE_BE32(0xffffffff),
2063 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2064 const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2065 MLX5_FLOW_LAYER_OUTER_L3;
2066 const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2067 MLX5_FLOW_LAYER_OUTER_L4;
2069 uint8_t next_proto = 0xFF;
2070 const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
2071 MLX5_FLOW_LAYER_OUTER_VLAN |
2072 MLX5_FLOW_LAYER_INNER_VLAN);
2074 if ((last_item & l2_vlan) && ether_type &&
2075 ether_type != RTE_ETHER_TYPE_IPV6)
2076 return rte_flow_error_set(error, EINVAL,
2077 RTE_FLOW_ERROR_TYPE_ITEM, item,
2078 "IPv6 cannot follow L2/VLAN layer "
2079 "which ether type is not IPv6");
2080 if (mask && mask->hdr.proto == UINT8_MAX && spec)
2081 next_proto = spec->hdr.proto;
2082 if (item_flags & MLX5_FLOW_LAYER_IPV6_ENCAP) {
2083 if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
2084 return rte_flow_error_set(error, EINVAL,
2085 RTE_FLOW_ERROR_TYPE_ITEM,
2090 if (next_proto == IPPROTO_HOPOPTS ||
2091 next_proto == IPPROTO_ROUTING ||
2092 next_proto == IPPROTO_FRAGMENT ||
2093 next_proto == IPPROTO_ESP ||
2094 next_proto == IPPROTO_AH ||
2095 next_proto == IPPROTO_DSTOPTS)
2096 return rte_flow_error_set(error, EINVAL,
2097 RTE_FLOW_ERROR_TYPE_ITEM, item,
2098 "IPv6 proto (next header) should "
2099 "not be set as extension header");
2100 if (item_flags & MLX5_FLOW_LAYER_IPIP)
2101 return rte_flow_error_set(error, EINVAL,
2102 RTE_FLOW_ERROR_TYPE_ITEM, item,
2103 "wrong tunnel type - IPv4 specified "
2104 "but IPv6 item provided");
2105 if (item_flags & l3m)
2106 return rte_flow_error_set(error, ENOTSUP,
2107 RTE_FLOW_ERROR_TYPE_ITEM, item,
2108 "multiple L3 layers not supported");
2109 else if (item_flags & l4m)
2110 return rte_flow_error_set(error, EINVAL,
2111 RTE_FLOW_ERROR_TYPE_ITEM, item,
2112 "L3 cannot follow an L4 layer.");
2113 else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
2114 !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
2115 return rte_flow_error_set(error, EINVAL,
2116 RTE_FLOW_ERROR_TYPE_ITEM, item,
2117 "L3 cannot follow an NVGRE layer.");
2119 mask = &rte_flow_item_ipv6_mask;
2120 ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2121 acc_mask ? (const uint8_t *)acc_mask
2122 : (const uint8_t *)&nic_mask,
2123 sizeof(struct rte_flow_item_ipv6),
2124 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2131 * Validate UDP item.
2134 * Item specification.
2135 * @param[in] item_flags
2136 * Bit-fields that holds the items detected until now.
2137 * @param[in] target_protocol
2138 * The next protocol in the previous item.
2139 * @param[in] flow_mask
2140 * mlx5 flow-specific (DV, verbs, etc.) supported header fields mask.
2142 * Pointer to error structure.
2145 * 0 on success, a negative errno value otherwise and rte_errno is set.
2148 mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
2149 uint64_t item_flags,
2150 uint8_t target_protocol,
2151 struct rte_flow_error *error)
2153 const struct rte_flow_item_udp *mask = item->mask;
2154 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2155 const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2156 MLX5_FLOW_LAYER_OUTER_L3;
2157 const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2158 MLX5_FLOW_LAYER_OUTER_L4;
2161 if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
2162 return rte_flow_error_set(error, EINVAL,
2163 RTE_FLOW_ERROR_TYPE_ITEM, item,
2164 "protocol filtering not compatible"
2166 if (!(item_flags & l3m))
2167 return rte_flow_error_set(error, EINVAL,
2168 RTE_FLOW_ERROR_TYPE_ITEM, item,
2169 "L3 is mandatory to filter on L4");
2170 if (item_flags & l4m)
2171 return rte_flow_error_set(error, EINVAL,
2172 RTE_FLOW_ERROR_TYPE_ITEM, item,
2173 "multiple L4 layers not supported");
2175 mask = &rte_flow_item_udp_mask;
2176 ret = mlx5_flow_item_acceptable
2177 (item, (const uint8_t *)mask,
2178 (const uint8_t *)&rte_flow_item_udp_mask,
2179 sizeof(struct rte_flow_item_udp), MLX5_ITEM_RANGE_NOT_ACCEPTED,
2187 * Validate TCP item.
2190 * Item specification.
2191 * @param[in] item_flags
2192 * Bit-fields that holds the items detected until now.
2193 * @param[in] target_protocol
2194 * The next protocol in the previous item.
2196 * Pointer to error structure.
2199 * 0 on success, a negative errno value otherwise and rte_errno is set.
2202 mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
2203 uint64_t item_flags,
2204 uint8_t target_protocol,
2205 const struct rte_flow_item_tcp *flow_mask,
2206 struct rte_flow_error *error)
2208 const struct rte_flow_item_tcp *mask = item->mask;
2209 const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2210 const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2211 MLX5_FLOW_LAYER_OUTER_L3;
2212 const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2213 MLX5_FLOW_LAYER_OUTER_L4;
2216 MLX5_ASSERT(flow_mask);
2217 if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
2218 return rte_flow_error_set(error, EINVAL,
2219 RTE_FLOW_ERROR_TYPE_ITEM, item,
2220 "protocol filtering not compatible"
2222 if (!(item_flags & l3m))
2223 return rte_flow_error_set(error, EINVAL,
2224 RTE_FLOW_ERROR_TYPE_ITEM, item,
2225 "L3 is mandatory to filter on L4");
2226 if (item_flags & l4m)
2227 return rte_flow_error_set(error, EINVAL,
2228 RTE_FLOW_ERROR_TYPE_ITEM, item,
2229 "multiple L4 layers not supported");
2231 mask = &rte_flow_item_tcp_mask;
2232 ret = mlx5_flow_item_acceptable
2233 (item, (const uint8_t *)mask,
2234 (const uint8_t *)flow_mask,
2235 sizeof(struct rte_flow_item_tcp), MLX5_ITEM_RANGE_NOT_ACCEPTED,
2243 * Validate VXLAN item.
2246 * Item specification.
2247 * @param[in] item_flags
2248 * Bit-fields that holds the items detected until now.
2249 * @param[in] target_protocol
2250 * The next protocol in the previous item.
2252 * Pointer to error structure.
2255 * 0 on success, a negative errno value otherwise and rte_errno is set.
2258 mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
2259 uint64_t item_flags,
2260 struct rte_flow_error *error)
2262 const struct rte_flow_item_vxlan *spec = item->spec;
2263 const struct rte_flow_item_vxlan *mask = item->mask;
2268 } id = { .vlan_id = 0, };
2271 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2272 return rte_flow_error_set(error, ENOTSUP,
2273 RTE_FLOW_ERROR_TYPE_ITEM, item,
2274 "multiple tunnel layers not"
2277 * Verify only UDPv4 is present as defined in
2278 * https://tools.ietf.org/html/rfc7348
2280 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2281 return rte_flow_error_set(error, EINVAL,
2282 RTE_FLOW_ERROR_TYPE_ITEM, item,
2283 "no outer UDP layer found");
2285 mask = &rte_flow_item_vxlan_mask;
2286 ret = mlx5_flow_item_acceptable
2287 (item, (const uint8_t *)mask,
2288 (const uint8_t *)&rte_flow_item_vxlan_mask,
2289 sizeof(struct rte_flow_item_vxlan),
2290 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2294 memcpy(&id.vni[1], spec->vni, 3);
2295 memcpy(&id.vni[1], mask->vni, 3);
2297 if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2298 return rte_flow_error_set(error, ENOTSUP,
2299 RTE_FLOW_ERROR_TYPE_ITEM, item,
2300 "VXLAN tunnel must be fully defined");
2305 * Validate VXLAN_GPE item.
2308 * Item specification.
2309 * @param[in] item_flags
2310 * Bit-fields that holds the items detected until now.
2312 * Pointer to the private data structure.
2313 * @param[in] target_protocol
2314 * The next protocol in the previous item.
2316 * Pointer to error structure.
2319 * 0 on success, a negative errno value otherwise and rte_errno is set.
2322 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
2323 uint64_t item_flags,
2324 struct rte_eth_dev *dev,
2325 struct rte_flow_error *error)
2327 struct mlx5_priv *priv = dev->data->dev_private;
2328 const struct rte_flow_item_vxlan_gpe *spec = item->spec;
2329 const struct rte_flow_item_vxlan_gpe *mask = item->mask;
2334 } id = { .vlan_id = 0, };
2336 if (!priv->config.l3_vxlan_en)
2337 return rte_flow_error_set(error, ENOTSUP,
2338 RTE_FLOW_ERROR_TYPE_ITEM, item,
2339 "L3 VXLAN is not enabled by device"
2340 " parameter and/or not configured in"
2342 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2343 return rte_flow_error_set(error, ENOTSUP,
2344 RTE_FLOW_ERROR_TYPE_ITEM, item,
2345 "multiple tunnel layers not"
2348 * Verify only UDPv4 is present as defined in
2349 * https://tools.ietf.org/html/rfc7348
2351 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2352 return rte_flow_error_set(error, EINVAL,
2353 RTE_FLOW_ERROR_TYPE_ITEM, item,
2354 "no outer UDP layer found");
2356 mask = &rte_flow_item_vxlan_gpe_mask;
2357 ret = mlx5_flow_item_acceptable
2358 (item, (const uint8_t *)mask,
2359 (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
2360 sizeof(struct rte_flow_item_vxlan_gpe),
2361 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2366 return rte_flow_error_set(error, ENOTSUP,
2367 RTE_FLOW_ERROR_TYPE_ITEM,
2369 "VxLAN-GPE protocol"
2371 memcpy(&id.vni[1], spec->vni, 3);
2372 memcpy(&id.vni[1], mask->vni, 3);
2374 if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2375 return rte_flow_error_set(error, ENOTSUP,
2376 RTE_FLOW_ERROR_TYPE_ITEM, item,
2377 "VXLAN-GPE tunnel must be fully"
2382 * Validate GRE Key item.
2385 * Item specification.
2386 * @param[in] item_flags
2387 * Bit flags to mark detected items.
2388 * @param[in] gre_item
2389 * Pointer to gre_item
2391 * Pointer to error structure.
2394 * 0 on success, a negative errno value otherwise and rte_errno is set.
2397 mlx5_flow_validate_item_gre_key(const struct rte_flow_item *item,
2398 uint64_t item_flags,
2399 const struct rte_flow_item *gre_item,
2400 struct rte_flow_error *error)
2402 const rte_be32_t *mask = item->mask;
2404 rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
2405 const struct rte_flow_item_gre *gre_spec;
2406 const struct rte_flow_item_gre *gre_mask;
2408 if (item_flags & MLX5_FLOW_LAYER_GRE_KEY)
2409 return rte_flow_error_set(error, ENOTSUP,
2410 RTE_FLOW_ERROR_TYPE_ITEM, item,
2411 "Multiple GRE key not support");
2412 if (!(item_flags & MLX5_FLOW_LAYER_GRE))
2413 return rte_flow_error_set(error, ENOTSUP,
2414 RTE_FLOW_ERROR_TYPE_ITEM, item,
2415 "No preceding GRE header");
2416 if (item_flags & MLX5_FLOW_LAYER_INNER)
2417 return rte_flow_error_set(error, ENOTSUP,
2418 RTE_FLOW_ERROR_TYPE_ITEM, item,
2419 "GRE key following a wrong item");
2420 gre_mask = gre_item->mask;
2422 gre_mask = &rte_flow_item_gre_mask;
2423 gre_spec = gre_item->spec;
2424 if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x2000)) &&
2425 !(gre_spec->c_rsvd0_ver & RTE_BE16(0x2000)))
2426 return rte_flow_error_set(error, EINVAL,
2427 RTE_FLOW_ERROR_TYPE_ITEM, item,
2428 "Key bit must be on");
2431 mask = &gre_key_default_mask;
2432 ret = mlx5_flow_item_acceptable
2433 (item, (const uint8_t *)mask,
2434 (const uint8_t *)&gre_key_default_mask,
2435 sizeof(rte_be32_t), MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2440 * Validate GRE item.
2443 * Item specification.
2444 * @param[in] item_flags
2445 * Bit flags to mark detected items.
2446 * @param[in] target_protocol
2447 * The next protocol in the previous item.
2449 * Pointer to error structure.
2452 * 0 on success, a negative errno value otherwise and rte_errno is set.
2455 mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
2456 uint64_t item_flags,
2457 uint8_t target_protocol,
2458 struct rte_flow_error *error)
2460 const struct rte_flow_item_gre *spec __rte_unused = item->spec;
2461 const struct rte_flow_item_gre *mask = item->mask;
2463 const struct rte_flow_item_gre nic_mask = {
2464 .c_rsvd0_ver = RTE_BE16(0xB000),
2465 .protocol = RTE_BE16(UINT16_MAX),
2468 if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
2469 return rte_flow_error_set(error, EINVAL,
2470 RTE_FLOW_ERROR_TYPE_ITEM, item,
2471 "protocol filtering not compatible"
2472 " with this GRE layer");
2473 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2474 return rte_flow_error_set(error, ENOTSUP,
2475 RTE_FLOW_ERROR_TYPE_ITEM, item,
2476 "multiple tunnel layers not"
2478 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
2479 return rte_flow_error_set(error, ENOTSUP,
2480 RTE_FLOW_ERROR_TYPE_ITEM, item,
2481 "L3 Layer is missing");
2483 mask = &rte_flow_item_gre_mask;
2484 ret = mlx5_flow_item_acceptable
2485 (item, (const uint8_t *)mask,
2486 (const uint8_t *)&nic_mask,
2487 sizeof(struct rte_flow_item_gre), MLX5_ITEM_RANGE_NOT_ACCEPTED,
2491 #ifndef HAVE_MLX5DV_DR
2492 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
2493 if (spec && (spec->protocol & mask->protocol))
2494 return rte_flow_error_set(error, ENOTSUP,
2495 RTE_FLOW_ERROR_TYPE_ITEM, item,
2496 "without MPLS support the"
2497 " specification cannot be used for"
2505 * Validate Geneve item.
2508 * Item specification.
2509 * @param[in] itemFlags
2510 * Bit-fields that holds the items detected until now.
2512 * Pointer to the private data structure.
2514 * Pointer to error structure.
2517 * 0 on success, a negative errno value otherwise and rte_errno is set.
2521 mlx5_flow_validate_item_geneve(const struct rte_flow_item *item,
2522 uint64_t item_flags,
2523 struct rte_eth_dev *dev,
2524 struct rte_flow_error *error)
2526 struct mlx5_priv *priv = dev->data->dev_private;
2527 const struct rte_flow_item_geneve *spec = item->spec;
2528 const struct rte_flow_item_geneve *mask = item->mask;
2531 uint8_t opt_len = priv->config.hca_attr.geneve_max_opt_len ?
2532 MLX5_GENEVE_OPT_LEN_1 : MLX5_GENEVE_OPT_LEN_0;
2533 const struct rte_flow_item_geneve nic_mask = {
2534 .ver_opt_len_o_c_rsvd0 = RTE_BE16(0x3f80),
2535 .vni = "\xff\xff\xff",
2536 .protocol = RTE_BE16(UINT16_MAX),
2539 if (!priv->config.hca_attr.tunnel_stateless_geneve_rx)
2540 return rte_flow_error_set(error, ENOTSUP,
2541 RTE_FLOW_ERROR_TYPE_ITEM, item,
2542 "L3 Geneve is not enabled by device"
2543 " parameter and/or not configured in"
2545 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2546 return rte_flow_error_set(error, ENOTSUP,
2547 RTE_FLOW_ERROR_TYPE_ITEM, item,
2548 "multiple tunnel layers not"
2551 * Verify only UDPv4 is present as defined in
2552 * https://tools.ietf.org/html/rfc7348
2554 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2555 return rte_flow_error_set(error, EINVAL,
2556 RTE_FLOW_ERROR_TYPE_ITEM, item,
2557 "no outer UDP layer found");
2559 mask = &rte_flow_item_geneve_mask;
2560 ret = mlx5_flow_item_acceptable
2561 (item, (const uint8_t *)mask,
2562 (const uint8_t *)&nic_mask,
2563 sizeof(struct rte_flow_item_geneve),
2564 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2568 gbhdr = rte_be_to_cpu_16(spec->ver_opt_len_o_c_rsvd0);
2569 if (MLX5_GENEVE_VER_VAL(gbhdr) ||
2570 MLX5_GENEVE_CRITO_VAL(gbhdr) ||
2571 MLX5_GENEVE_RSVD_VAL(gbhdr) || spec->rsvd1)
2572 return rte_flow_error_set(error, ENOTSUP,
2573 RTE_FLOW_ERROR_TYPE_ITEM,
2575 "Geneve protocol unsupported"
2576 " fields are being used");
2577 if (MLX5_GENEVE_OPTLEN_VAL(gbhdr) > opt_len)
2578 return rte_flow_error_set
2580 RTE_FLOW_ERROR_TYPE_ITEM,
2582 "Unsupported Geneve options length");
2584 if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2585 return rte_flow_error_set
2587 RTE_FLOW_ERROR_TYPE_ITEM, item,
2588 "Geneve tunnel must be fully defined");
2593 * Validate MPLS item.
2596 * Pointer to the rte_eth_dev structure.
2598 * Item specification.
2599 * @param[in] item_flags
2600 * Bit-fields that holds the items detected until now.
2601 * @param[in] prev_layer
2602 * The protocol layer indicated in previous item.
2604 * Pointer to error structure.
2607 * 0 on success, a negative errno value otherwise and rte_errno is set.
2610 mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev __rte_unused,
2611 const struct rte_flow_item *item __rte_unused,
2612 uint64_t item_flags __rte_unused,
2613 uint64_t prev_layer __rte_unused,
2614 struct rte_flow_error *error)
2616 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
2617 const struct rte_flow_item_mpls *mask = item->mask;
2618 struct mlx5_priv *priv = dev->data->dev_private;
2621 if (!priv->config.mpls_en)
2622 return rte_flow_error_set(error, ENOTSUP,
2623 RTE_FLOW_ERROR_TYPE_ITEM, item,
2624 "MPLS not supported or"
2625 " disabled in firmware"
2627 /* MPLS over IP, UDP, GRE is allowed */
2628 if (!(prev_layer & (MLX5_FLOW_LAYER_OUTER_L3 |
2629 MLX5_FLOW_LAYER_OUTER_L4_UDP |
2630 MLX5_FLOW_LAYER_GRE |
2631 MLX5_FLOW_LAYER_GRE_KEY)))
2632 return rte_flow_error_set(error, EINVAL,
2633 RTE_FLOW_ERROR_TYPE_ITEM, item,
2634 "protocol filtering not compatible"
2635 " with MPLS layer");
2636 /* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
2637 if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
2638 !(item_flags & MLX5_FLOW_LAYER_GRE))
2639 return rte_flow_error_set(error, ENOTSUP,
2640 RTE_FLOW_ERROR_TYPE_ITEM, item,
2641 "multiple tunnel layers not"
2644 mask = &rte_flow_item_mpls_mask;
2645 ret = mlx5_flow_item_acceptable
2646 (item, (const uint8_t *)mask,
2647 (const uint8_t *)&rte_flow_item_mpls_mask,
2648 sizeof(struct rte_flow_item_mpls),
2649 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2654 return rte_flow_error_set(error, ENOTSUP,
2655 RTE_FLOW_ERROR_TYPE_ITEM, item,
2656 "MPLS is not supported by Verbs, please"
2662 * Validate NVGRE item.
2665 * Item specification.
2666 * @param[in] item_flags
2667 * Bit flags to mark detected items.
2668 * @param[in] target_protocol
2669 * The next protocol in the previous item.
2671 * Pointer to error structure.
2674 * 0 on success, a negative errno value otherwise and rte_errno is set.
2677 mlx5_flow_validate_item_nvgre(const struct rte_flow_item *item,
2678 uint64_t item_flags,
2679 uint8_t target_protocol,
2680 struct rte_flow_error *error)
2682 const struct rte_flow_item_nvgre *mask = item->mask;
2685 if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
2686 return rte_flow_error_set(error, EINVAL,
2687 RTE_FLOW_ERROR_TYPE_ITEM, item,
2688 "protocol filtering not compatible"
2689 " with this GRE layer");
2690 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2691 return rte_flow_error_set(error, ENOTSUP,
2692 RTE_FLOW_ERROR_TYPE_ITEM, item,
2693 "multiple tunnel layers not"
2695 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
2696 return rte_flow_error_set(error, ENOTSUP,
2697 RTE_FLOW_ERROR_TYPE_ITEM, item,
2698 "L3 Layer is missing");
2700 mask = &rte_flow_item_nvgre_mask;
2701 ret = mlx5_flow_item_acceptable
2702 (item, (const uint8_t *)mask,
2703 (const uint8_t *)&rte_flow_item_nvgre_mask,
2704 sizeof(struct rte_flow_item_nvgre),
2705 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2712 * Validate eCPRI item.
2715 * Item specification.
2716 * @param[in] item_flags
2717 * Bit-fields that holds the items detected until now.
2718 * @param[in] last_item
2719 * Previous validated item in the pattern items.
2720 * @param[in] ether_type
2721 * Type in the ethernet layer header (including dot1q).
2722 * @param[in] acc_mask
2723 * Acceptable mask, if NULL default internal default mask
2724 * will be used to check whether item fields are supported.
2726 * Pointer to error structure.
2729 * 0 on success, a negative errno value otherwise and rte_errno is set.
2732 mlx5_flow_validate_item_ecpri(const struct rte_flow_item *item,
2733 uint64_t item_flags,
2735 uint16_t ether_type,
2736 const struct rte_flow_item_ecpri *acc_mask,
2737 struct rte_flow_error *error)
2739 const struct rte_flow_item_ecpri *mask = item->mask;
2740 const struct rte_flow_item_ecpri nic_mask = {
2744 RTE_BE32(((const struct rte_ecpri_common_hdr) {
2748 .dummy[0] = 0xFFFFFFFF,
2751 const uint64_t outer_l2_vlan = (MLX5_FLOW_LAYER_OUTER_L2 |
2752 MLX5_FLOW_LAYER_OUTER_VLAN);
2753 struct rte_flow_item_ecpri mask_lo;
2755 if (!(last_item & outer_l2_vlan) &&
2756 last_item != MLX5_FLOW_LAYER_OUTER_L4_UDP)
2757 return rte_flow_error_set(error, EINVAL,
2758 RTE_FLOW_ERROR_TYPE_ITEM, item,
2759 "eCPRI can only follow L2/VLAN layer or UDP layer");
2760 if ((last_item & outer_l2_vlan) && ether_type &&
2761 ether_type != RTE_ETHER_TYPE_ECPRI)
2762 return rte_flow_error_set(error, EINVAL,
2763 RTE_FLOW_ERROR_TYPE_ITEM, item,
2764 "eCPRI cannot follow L2/VLAN layer which ether type is not 0xAEFE");
2765 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2766 return rte_flow_error_set(error, EINVAL,
2767 RTE_FLOW_ERROR_TYPE_ITEM, item,
2768 "eCPRI with tunnel is not supported right now");
2769 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3)
2770 return rte_flow_error_set(error, ENOTSUP,
2771 RTE_FLOW_ERROR_TYPE_ITEM, item,
2772 "multiple L3 layers not supported");
2773 else if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP)
2774 return rte_flow_error_set(error, EINVAL,
2775 RTE_FLOW_ERROR_TYPE_ITEM, item,
2776 "eCPRI cannot coexist with a TCP layer");
2777 /* In specification, eCPRI could be over UDP layer. */
2778 else if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP)
2779 return rte_flow_error_set(error, EINVAL,
2780 RTE_FLOW_ERROR_TYPE_ITEM, item,
2781 "eCPRI over UDP layer is not yet supported right now");
2782 /* Mask for type field in common header could be zero. */
2784 mask = &rte_flow_item_ecpri_mask;
2785 mask_lo.hdr.common.u32 = rte_be_to_cpu_32(mask->hdr.common.u32);
2786 /* Input mask is in big-endian format. */
2787 if (mask_lo.hdr.common.type != 0 && mask_lo.hdr.common.type != 0xff)
2788 return rte_flow_error_set(error, EINVAL,
2789 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
2790 "partial mask is not supported for protocol");
2791 else if (mask_lo.hdr.common.type == 0 && mask->hdr.dummy[0] != 0)
2792 return rte_flow_error_set(error, EINVAL,
2793 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
2794 "message header mask must be after a type mask");
2795 return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2796 acc_mask ? (const uint8_t *)acc_mask
2797 : (const uint8_t *)&nic_mask,
2798 sizeof(struct rte_flow_item_ecpri),
2799 MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2803 * Release resource related QUEUE/RSS action split.
2806 * Pointer to Ethernet device.
2808 * Flow to release id's from.
2811 flow_mreg_split_qrss_release(struct rte_eth_dev *dev,
2812 struct rte_flow *flow)
2814 struct mlx5_priv *priv = dev->data->dev_private;
2815 uint32_t handle_idx;
2816 struct mlx5_flow_handle *dev_handle;
2818 SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
2819 handle_idx, dev_handle, next)
2820 if (dev_handle->split_flow_id)
2821 mlx5_ipool_free(priv->sh->ipool
2822 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
2823 dev_handle->split_flow_id);
2827 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
2828 const struct rte_flow_attr *attr __rte_unused,
2829 const struct rte_flow_item items[] __rte_unused,
2830 const struct rte_flow_action actions[] __rte_unused,
2831 bool external __rte_unused,
2832 int hairpin __rte_unused,
2833 struct rte_flow_error *error)
2835 return rte_flow_error_set(error, ENOTSUP,
2836 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2839 static struct mlx5_flow *
2840 flow_null_prepare(struct rte_eth_dev *dev __rte_unused,
2841 const struct rte_flow_attr *attr __rte_unused,
2842 const struct rte_flow_item items[] __rte_unused,
2843 const struct rte_flow_action actions[] __rte_unused,
2844 struct rte_flow_error *error)
2846 rte_flow_error_set(error, ENOTSUP,
2847 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2852 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
2853 struct mlx5_flow *dev_flow __rte_unused,
2854 const struct rte_flow_attr *attr __rte_unused,
2855 const struct rte_flow_item items[] __rte_unused,
2856 const struct rte_flow_action actions[] __rte_unused,
2857 struct rte_flow_error *error)
2859 return rte_flow_error_set(error, ENOTSUP,
2860 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2864 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
2865 struct rte_flow *flow __rte_unused,
2866 struct rte_flow_error *error)
2868 return rte_flow_error_set(error, ENOTSUP,
2869 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2873 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
2874 struct rte_flow *flow __rte_unused)
2879 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
2880 struct rte_flow *flow __rte_unused)
2885 flow_null_query(struct rte_eth_dev *dev __rte_unused,
2886 struct rte_flow *flow __rte_unused,
2887 const struct rte_flow_action *actions __rte_unused,
2888 void *data __rte_unused,
2889 struct rte_flow_error *error)
2891 return rte_flow_error_set(error, ENOTSUP,
2892 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2896 flow_null_sync_domain(struct rte_eth_dev *dev __rte_unused,
2897 uint32_t domains __rte_unused,
2898 uint32_t flags __rte_unused)
2903 /* Void driver to protect from null pointer reference. */
2904 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
2905 .validate = flow_null_validate,
2906 .prepare = flow_null_prepare,
2907 .translate = flow_null_translate,
2908 .apply = flow_null_apply,
2909 .remove = flow_null_remove,
2910 .destroy = flow_null_destroy,
2911 .query = flow_null_query,
2912 .sync_domain = flow_null_sync_domain,
2916 * Select flow driver type according to flow attributes and device
2920 * Pointer to the dev structure.
2922 * Pointer to the flow attributes.
2925 * flow driver type, MLX5_FLOW_TYPE_MAX otherwise.
2927 static enum mlx5_flow_drv_type
2928 flow_get_drv_type(struct rte_eth_dev *dev, const struct rte_flow_attr *attr)
2930 struct mlx5_priv *priv = dev->data->dev_private;
2931 /* The OS can determine first a specific flow type (DV, VERBS) */
2932 enum mlx5_flow_drv_type type = mlx5_flow_os_get_type();
2934 if (type != MLX5_FLOW_TYPE_MAX)
2936 /* If no OS specific type - continue with DV/VERBS selection */
2937 if (attr->transfer && priv->config.dv_esw_en)
2938 type = MLX5_FLOW_TYPE_DV;
2939 if (!attr->transfer)
2940 type = priv->config.dv_flow_en ? MLX5_FLOW_TYPE_DV :
2941 MLX5_FLOW_TYPE_VERBS;
2945 #define flow_get_drv_ops(type) flow_drv_ops[type]
2948 * Flow driver validation API. This abstracts calling driver specific functions.
2949 * The type of flow driver is determined according to flow attributes.
2952 * Pointer to the dev structure.
2954 * Pointer to the flow attributes.
2956 * Pointer to the list of items.
2957 * @param[in] actions
2958 * Pointer to the list of actions.
2959 * @param[in] external
2960 * This flow rule is created by request external to PMD.
2961 * @param[in] hairpin
2962 * Number of hairpin TX actions, 0 means classic flow.
2964 * Pointer to the error structure.
2967 * 0 on success, a negative errno value otherwise and rte_errno is set.
2970 flow_drv_validate(struct rte_eth_dev *dev,
2971 const struct rte_flow_attr *attr,
2972 const struct rte_flow_item items[],
2973 const struct rte_flow_action actions[],
2974 bool external, int hairpin, struct rte_flow_error *error)
2976 const struct mlx5_flow_driver_ops *fops;
2977 enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
2979 fops = flow_get_drv_ops(type);
2980 return fops->validate(dev, attr, items, actions, external,
2985 * Flow driver preparation API. This abstracts calling driver specific
2986 * functions. Parent flow (rte_flow) should have driver type (drv_type). It
2987 * calculates the size of memory required for device flow, allocates the memory,
2988 * initializes the device flow and returns the pointer.
2991 * This function initializes device flow structure such as dv or verbs in
2992 * struct mlx5_flow. However, it is caller's responsibility to initialize the
2993 * rest. For example, adding returning device flow to flow->dev_flow list and
2994 * setting backward reference to the flow should be done out of this function.
2995 * layers field is not filled either.
2998 * Pointer to the dev structure.
3000 * Pointer to the flow attributes.
3002 * Pointer to the list of items.
3003 * @param[in] actions
3004 * Pointer to the list of actions.
3005 * @param[in] flow_idx
3006 * This memory pool index to the flow.
3008 * Pointer to the error structure.
3011 * Pointer to device flow on success, otherwise NULL and rte_errno is set.
3013 static inline struct mlx5_flow *
3014 flow_drv_prepare(struct rte_eth_dev *dev,
3015 const struct rte_flow *flow,
3016 const struct rte_flow_attr *attr,
3017 const struct rte_flow_item items[],
3018 const struct rte_flow_action actions[],
3020 struct rte_flow_error *error)
3022 const struct mlx5_flow_driver_ops *fops;
3023 enum mlx5_flow_drv_type type = flow->drv_type;
3024 struct mlx5_flow *mlx5_flow = NULL;
3026 MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3027 fops = flow_get_drv_ops(type);
3028 mlx5_flow = fops->prepare(dev, attr, items, actions, error);
3030 mlx5_flow->flow_idx = flow_idx;
3035 * Flow driver translation API. This abstracts calling driver specific
3036 * functions. Parent flow (rte_flow) should have driver type (drv_type). It
3037 * translates a generic flow into a driver flow. flow_drv_prepare() must
3041 * dev_flow->layers could be filled as a result of parsing during translation
3042 * if needed by flow_drv_apply(). dev_flow->flow->actions can also be filled
3043 * if necessary. As a flow can have multiple dev_flows by RSS flow expansion,
3044 * flow->actions could be overwritten even though all the expanded dev_flows
3045 * have the same actions.
3048 * Pointer to the rte dev structure.
3049 * @param[in, out] dev_flow
3050 * Pointer to the mlx5 flow.
3052 * Pointer to the flow attributes.
3054 * Pointer to the list of items.
3055 * @param[in] actions
3056 * Pointer to the list of actions.
3058 * Pointer to the error structure.
3061 * 0 on success, a negative errno value otherwise and rte_errno is set.
3064 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
3065 const struct rte_flow_attr *attr,
3066 const struct rte_flow_item items[],
3067 const struct rte_flow_action actions[],
3068 struct rte_flow_error *error)
3070 const struct mlx5_flow_driver_ops *fops;
3071 enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
3073 MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3074 fops = flow_get_drv_ops(type);
3075 return fops->translate(dev, dev_flow, attr, items, actions, error);
3079 * Flow driver apply API. This abstracts calling driver specific functions.
3080 * Parent flow (rte_flow) should have driver type (drv_type). It applies
3081 * translated driver flows on to device. flow_drv_translate() must precede.
3084 * Pointer to Ethernet device structure.
3085 * @param[in, out] flow
3086 * Pointer to flow structure.
3088 * Pointer to error structure.
3091 * 0 on success, a negative errno value otherwise and rte_errno is set.
3094 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
3095 struct rte_flow_error *error)
3097 const struct mlx5_flow_driver_ops *fops;
3098 enum mlx5_flow_drv_type type = flow->drv_type;
3100 MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3101 fops = flow_get_drv_ops(type);
3102 return fops->apply(dev, flow, error);
3106 * Flow driver destroy API. This abstracts calling driver specific functions.
3107 * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
3108 * on device and releases resources of the flow.
3111 * Pointer to Ethernet device.
3112 * @param[in, out] flow
3113 * Pointer to flow structure.
3116 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
3118 const struct mlx5_flow_driver_ops *fops;
3119 enum mlx5_flow_drv_type type = flow->drv_type;
3121 flow_mreg_split_qrss_release(dev, flow);
3122 MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3123 fops = flow_get_drv_ops(type);
3124 fops->destroy(dev, flow);
3128 * Get RSS action from the action list.
3130 * @param[in] actions
3131 * Pointer to the list of actions.
3134 * Pointer to the RSS action if exist, else return NULL.
3136 static const struct rte_flow_action_rss*
3137 flow_get_rss_action(const struct rte_flow_action actions[])
3139 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3140 switch (actions->type) {
3141 case RTE_FLOW_ACTION_TYPE_RSS:
3142 return (const struct rte_flow_action_rss *)
3152 * Get ASO age action by index.
3155 * Pointer to the Ethernet device structure.
3156 * @param[in] age_idx
3157 * Index to the ASO age action.
3160 * The specified ASO age action.
3162 struct mlx5_aso_age_action*
3163 flow_aso_age_get_by_idx(struct rte_eth_dev *dev, uint32_t age_idx)
3165 uint16_t pool_idx = age_idx & UINT16_MAX;
3166 uint16_t offset = (age_idx >> 16) & UINT16_MAX;
3167 struct mlx5_priv *priv = dev->data->dev_private;
3168 struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
3169 struct mlx5_aso_age_pool *pool = mng->pools[pool_idx];
3171 return &pool->actions[offset - 1];
3174 /* maps shared action to translated non shared in some actions array */
3175 struct mlx5_translated_shared_action {
3176 struct rte_flow_shared_action *action; /**< Shared action */
3177 int index; /**< Index in related array of rte_flow_action */
3181 * Translates actions of type RTE_FLOW_ACTION_TYPE_SHARED to related
3182 * non shared action if translation possible.
3183 * This functionality used to run same execution path for both shared & non
3184 * shared actions on flow create. All necessary preparations for shared
3185 * action handling should be preformed on *shared* actions list returned
3189 * Pointer to Ethernet device.
3190 * @param[in] actions
3191 * List of actions to translate.
3192 * @param[out] shared
3193 * List to store translated shared actions.
3194 * @param[in, out] shared_n
3195 * Size of *shared* array. On return should be updated with number of shared
3196 * actions retrieved from the *actions* list.
3197 * @param[out] translated_actions
3198 * List of actions where all shared actions were translated to non shared
3199 * if possible. NULL if no translation took place.
3201 * Pointer to the error structure.
3204 * 0 on success, a negative errno value otherwise and rte_errno is set.
3207 flow_shared_actions_translate(struct rte_eth_dev *dev,
3208 const struct rte_flow_action actions[],
3209 struct mlx5_translated_shared_action *shared,
3211 struct rte_flow_action **translated_actions,
3212 struct rte_flow_error *error)
3214 struct mlx5_priv *priv = dev->data->dev_private;
3215 struct rte_flow_action *translated = NULL;
3216 size_t actions_size;
3219 struct mlx5_translated_shared_action *shared_end = NULL;
3221 for (n = 0; actions[n].type != RTE_FLOW_ACTION_TYPE_END; n++) {
3222 if (actions[n].type != RTE_FLOW_ACTION_TYPE_SHARED)
3224 if (copied_n == *shared_n) {
3225 return rte_flow_error_set
3226 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
3227 NULL, "too many shared actions");
3229 rte_memcpy(&shared[copied_n].action, &actions[n].conf,
3230 sizeof(actions[n].conf));
3231 shared[copied_n].index = n;
3235 *shared_n = copied_n;
3238 actions_size = sizeof(struct rte_flow_action) * n;
3239 translated = mlx5_malloc(MLX5_MEM_ZERO, actions_size, 0, SOCKET_ID_ANY);
3244 memcpy(translated, actions, actions_size);
3245 for (shared_end = shared + copied_n; shared < shared_end; shared++) {
3246 struct mlx5_shared_action_rss *shared_rss;
3247 uint32_t act_idx = (uint32_t)(uintptr_t)shared->action;
3248 uint32_t type = act_idx >> MLX5_SHARED_ACTION_TYPE_OFFSET;
3249 uint32_t idx = act_idx & ((1u << MLX5_SHARED_ACTION_TYPE_OFFSET)
3253 case MLX5_SHARED_ACTION_TYPE_RSS:
3254 shared_rss = mlx5_ipool_get
3255 (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
3256 translated[shared->index].type =
3257 RTE_FLOW_ACTION_TYPE_RSS;
3258 translated[shared->index].conf =
3259 &shared_rss->origin;
3261 case MLX5_SHARED_ACTION_TYPE_AGE:
3262 if (priv->sh->flow_hit_aso_en) {
3263 translated[shared->index].type =
3264 (enum rte_flow_action_type)
3265 MLX5_RTE_FLOW_ACTION_TYPE_AGE;
3266 translated[shared->index].conf =
3267 (void *)(uintptr_t)idx;
3272 mlx5_free(translated);
3273 return rte_flow_error_set
3274 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
3275 NULL, "invalid shared action type");
3278 *translated_actions = translated;
3283 * Get Shared RSS action from the action list.
3286 * Pointer to Ethernet device.
3288 * Pointer to the list of actions.
3289 * @param[in] shared_n
3290 * Actions list length.
3293 * The MLX5 RSS action ID if exists, otherwise return 0.
3296 flow_get_shared_rss_action(struct rte_eth_dev *dev,
3297 struct mlx5_translated_shared_action *shared,
3300 struct mlx5_translated_shared_action *shared_end;
3301 struct mlx5_priv *priv = dev->data->dev_private;
3302 struct mlx5_shared_action_rss *shared_rss;
3305 for (shared_end = shared + shared_n; shared < shared_end; shared++) {
3306 uint32_t act_idx = (uint32_t)(uintptr_t)shared->action;
3307 uint32_t type = act_idx >> MLX5_SHARED_ACTION_TYPE_OFFSET;
3308 uint32_t idx = act_idx &
3309 ((1u << MLX5_SHARED_ACTION_TYPE_OFFSET) - 1);
3311 case MLX5_SHARED_ACTION_TYPE_RSS:
3312 shared_rss = mlx5_ipool_get
3313 (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
3315 __atomic_add_fetch(&shared_rss->refcnt, 1,
3326 find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
3328 const struct rte_flow_item *item;
3329 unsigned int has_vlan = 0;
3331 for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
3332 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
3338 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
3339 MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
3340 return rss_level < 2 ? MLX5_EXPANSION_ROOT :
3341 MLX5_EXPANSION_ROOT_OUTER;
3345 * Get layer flags from the prefix flow.
3347 * Some flows may be split to several subflows, the prefix subflow gets the
3348 * match items and the suffix sub flow gets the actions.
3349 * Some actions need the user defined match item flags to get the detail for
3351 * This function helps the suffix flow to get the item layer flags from prefix
3354 * @param[in] dev_flow
3355 * Pointer the created preifx subflow.
3358 * The layers get from prefix subflow.
3360 static inline uint64_t
3361 flow_get_prefix_layer_flags(struct mlx5_flow *dev_flow)
3363 uint64_t layers = 0;
3366 * Layers bits could be localization, but usually the compiler will
3367 * help to do the optimization work for source code.
3368 * If no decap actions, use the layers directly.
3370 if (!(dev_flow->act_flags & MLX5_FLOW_ACTION_DECAP))
3371 return dev_flow->handle->layers;
3372 /* Convert L3 layers with decap action. */
3373 if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L3_IPV4)
3374 layers |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3375 else if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L3_IPV6)
3376 layers |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3377 /* Convert L4 layers with decap action. */
3378 if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L4_TCP)
3379 layers |= MLX5_FLOW_LAYER_OUTER_L4_TCP;
3380 else if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L4_UDP)
3381 layers |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
3386 * Get metadata split action information.
3388 * @param[in] actions
3389 * Pointer to the list of actions.
3391 * Pointer to the return pointer.
3392 * @param[out] qrss_type
3393 * Pointer to the action type to return. RTE_FLOW_ACTION_TYPE_END is returned
3394 * if no QUEUE/RSS is found.
3395 * @param[out] encap_idx
3396 * Pointer to the index of the encap action if exists, otherwise the last
3400 * Total number of actions.
3403 flow_parse_metadata_split_actions_info(const struct rte_flow_action actions[],
3404 const struct rte_flow_action **qrss,
3407 const struct rte_flow_action_raw_encap *raw_encap;
3409 int raw_decap_idx = -1;
3412 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3413 switch (actions->type) {
3414 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3415 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3416 *encap_idx = actions_n;
3418 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3419 raw_decap_idx = actions_n;
3421 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3422 raw_encap = actions->conf;
3423 if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
3424 *encap_idx = raw_decap_idx != -1 ?
3425 raw_decap_idx : actions_n;
3427 case RTE_FLOW_ACTION_TYPE_QUEUE:
3428 case RTE_FLOW_ACTION_TYPE_RSS:
3436 if (*encap_idx == -1)
3437 *encap_idx = actions_n;
3438 /* Count RTE_FLOW_ACTION_TYPE_END. */
3439 return actions_n + 1;
3443 * Check meter action from the action list.
3445 * @param[in] actions
3446 * Pointer to the list of actions.
3448 * Pointer to the meter exist flag.
3451 * Total number of actions.
3454 flow_check_meter_action(const struct rte_flow_action actions[], uint32_t *mtr)
3460 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3461 switch (actions->type) {
3462 case RTE_FLOW_ACTION_TYPE_METER:
3470 /* Count RTE_FLOW_ACTION_TYPE_END. */
3471 return actions_n + 1;
3475 * Check if the flow should be split due to hairpin.
3476 * The reason for the split is that in current HW we can't
3477 * support encap and push-vlan on Rx, so if a flow contains
3478 * these actions we move it to Tx.
3481 * Pointer to Ethernet device.
3483 * Flow rule attributes.
3484 * @param[in] actions
3485 * Associated actions (list terminated by the END action).
3488 * > 0 the number of actions and the flow should be split,
3489 * 0 when no split required.
3492 flow_check_hairpin_split(struct rte_eth_dev *dev,
3493 const struct rte_flow_attr *attr,
3494 const struct rte_flow_action actions[])
3496 int queue_action = 0;
3499 const struct rte_flow_action_queue *queue;
3500 const struct rte_flow_action_rss *rss;
3501 const struct rte_flow_action_raw_encap *raw_encap;
3502 const struct rte_eth_hairpin_conf *conf;
3506 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3507 switch (actions->type) {
3508 case RTE_FLOW_ACTION_TYPE_QUEUE:
3509 queue = actions->conf;
3512 conf = mlx5_rxq_get_hairpin_conf(dev, queue->index);
3513 if (conf != NULL && !!conf->tx_explicit)
3518 case RTE_FLOW_ACTION_TYPE_RSS:
3519 rss = actions->conf;
3520 if (rss == NULL || rss->queue_num == 0)
3522 conf = mlx5_rxq_get_hairpin_conf(dev, rss->queue[0]);
3523 if (conf != NULL && !!conf->tx_explicit)
3528 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3529 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3530 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3531 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3532 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3536 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3537 raw_encap = actions->conf;
3538 if (raw_encap->size >
3539 (sizeof(struct rte_flow_item_eth) +
3540 sizeof(struct rte_flow_item_ipv4)))
3549 if (split && queue_action)
3554 /* Declare flow create/destroy prototype in advance. */
3556 flow_list_create(struct rte_eth_dev *dev, uint32_t *list,
3557 const struct rte_flow_attr *attr,
3558 const struct rte_flow_item items[],
3559 const struct rte_flow_action actions[],
3560 bool external, struct rte_flow_error *error);
3563 flow_list_destroy(struct rte_eth_dev *dev, uint32_t *list,
3566 struct mlx5_hlist_entry *
3567 flow_dv_mreg_create_cb(struct mlx5_hlist *list, uint64_t key,
3570 struct rte_eth_dev *dev = list->ctx;
3571 struct mlx5_priv *priv = dev->data->dev_private;
3572 struct mlx5_flow_cb_ctx *ctx = cb_ctx;
3573 struct mlx5_flow_mreg_copy_resource *mcp_res;
3574 struct rte_flow_error *error = ctx->error;
3577 uint32_t mark_id = key;
3578 struct rte_flow_attr attr = {
3579 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
3582 struct mlx5_rte_flow_item_tag tag_spec = {
3585 struct rte_flow_item items[] = {
3586 [1] = { .type = RTE_FLOW_ITEM_TYPE_END, },
3588 struct rte_flow_action_mark ftag = {
3591 struct mlx5_flow_action_copy_mreg cp_mreg = {
3595 struct rte_flow_action_jump jump = {
3596 .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
3598 struct rte_flow_action actions[] = {
3599 [3] = { .type = RTE_FLOW_ACTION_TYPE_END, },
3602 /* Fill the register fileds in the flow. */
3603 ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
3607 ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
3611 /* Provide the full width of FLAG specific value. */
3612 if (mark_id == (priv->sh->dv_regc0_mask & MLX5_FLOW_MARK_DEFAULT))
3613 tag_spec.data = MLX5_FLOW_MARK_DEFAULT;
3614 /* Build a new flow. */
3615 if (mark_id != MLX5_DEFAULT_COPY_ID) {
3616 items[0] = (struct rte_flow_item){
3617 .type = (enum rte_flow_item_type)
3618 MLX5_RTE_FLOW_ITEM_TYPE_TAG,
3621 items[1] = (struct rte_flow_item){
3622 .type = RTE_FLOW_ITEM_TYPE_END,
3624 actions[0] = (struct rte_flow_action){
3625 .type = (enum rte_flow_action_type)
3626 MLX5_RTE_FLOW_ACTION_TYPE_MARK,
3629 actions[1] = (struct rte_flow_action){
3630 .type = (enum rte_flow_action_type)
3631 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
3634 actions[2] = (struct rte_flow_action){
3635 .type = RTE_FLOW_ACTION_TYPE_JUMP,
3638 actions[3] = (struct rte_flow_action){
3639 .type = RTE_FLOW_ACTION_TYPE_END,
3642 /* Default rule, wildcard match. */
3643 attr.priority = MLX5_FLOW_PRIO_RSVD;
3644 items[0] = (struct rte_flow_item){
3645 .type = RTE_FLOW_ITEM_TYPE_END,
3647 actions[0] = (struct rte_flow_action){
3648 .type = (enum rte_flow_action_type)
3649 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
3652 actions[1] = (struct rte_flow_action){
3653 .type = RTE_FLOW_ACTION_TYPE_JUMP,
3656 actions[2] = (struct rte_flow_action){
3657 .type = RTE_FLOW_ACTION_TYPE_END,
3660 /* Build a new entry. */
3661 mcp_res = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MCP], &idx);
3668 * The copy Flows are not included in any list. There
3669 * ones are referenced from other Flows and can not
3670 * be applied, removed, deleted in ardbitrary order
3671 * by list traversing.
3673 mcp_res->rix_flow = flow_list_create(dev, NULL, &attr, items,
3674 actions, false, error);
3675 if (!mcp_res->rix_flow) {
3676 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], idx);
3679 return &mcp_res->hlist_ent;
3683 * Add a flow of copying flow metadata registers in RX_CP_TBL.
3685 * As mark_id is unique, if there's already a registered flow for the mark_id,
3686 * return by increasing the reference counter of the resource. Otherwise, create
3687 * the resource (mcp_res) and flow.
3690 * - If ingress port is ANY and reg_c[1] is mark_id,
3691 * flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
3693 * For default flow (zero mark_id), flow is like,
3694 * - If ingress port is ANY,
3695 * reg_b := reg_c[0] and jump to RX_ACT_TBL.
3698 * Pointer to Ethernet device.
3700 * ID of MARK action, zero means default flow for META.
3702 * Perform verbose error reporting if not NULL.
3705 * Associated resource on success, NULL otherwise and rte_errno is set.
3707 static struct mlx5_flow_mreg_copy_resource *
3708 flow_mreg_add_copy_action(struct rte_eth_dev *dev, uint32_t mark_id,
3709 struct rte_flow_error *error)
3711 struct mlx5_priv *priv = dev->data->dev_private;
3712 struct mlx5_hlist_entry *entry;
3713 struct mlx5_flow_cb_ctx ctx = {
3718 /* Check if already registered. */
3719 MLX5_ASSERT(priv->mreg_cp_tbl);
3720 entry = mlx5_hlist_register(priv->mreg_cp_tbl, mark_id, &ctx);
3723 return container_of(entry, struct mlx5_flow_mreg_copy_resource,
3728 flow_dv_mreg_remove_cb(struct mlx5_hlist *list, struct mlx5_hlist_entry *entry)
3730 struct mlx5_flow_mreg_copy_resource *mcp_res =
3731 container_of(entry, typeof(*mcp_res), hlist_ent);
3732 struct rte_eth_dev *dev = list->ctx;
3733 struct mlx5_priv *priv = dev->data->dev_private;
3735 MLX5_ASSERT(mcp_res->rix_flow);
3736 flow_list_destroy(dev, NULL, mcp_res->rix_flow);
3737 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], mcp_res->idx);
3741 * Release flow in RX_CP_TBL.
3744 * Pointer to Ethernet device.
3746 * Parent flow for wich copying is provided.
3749 flow_mreg_del_copy_action(struct rte_eth_dev *dev,
3750 struct rte_flow *flow)
3752 struct mlx5_flow_mreg_copy_resource *mcp_res;
3753 struct mlx5_priv *priv = dev->data->dev_private;
3755 if (!flow->rix_mreg_copy)
3757 mcp_res = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MCP],
3758 flow->rix_mreg_copy);
3759 if (!mcp_res || !priv->mreg_cp_tbl)
3761 MLX5_ASSERT(mcp_res->rix_flow);
3762 mlx5_hlist_unregister(priv->mreg_cp_tbl, &mcp_res->hlist_ent);
3763 flow->rix_mreg_copy = 0;
3767 * Remove the default copy action from RX_CP_TBL.
3769 * This functions is called in the mlx5_dev_start(). No thread safe
3773 * Pointer to Ethernet device.
3776 flow_mreg_del_default_copy_action(struct rte_eth_dev *dev)
3778 struct mlx5_hlist_entry *entry;
3779 struct mlx5_priv *priv = dev->data->dev_private;
3781 /* Check if default flow is registered. */
3782 if (!priv->mreg_cp_tbl)
3784 entry = mlx5_hlist_lookup(priv->mreg_cp_tbl,
3785 MLX5_DEFAULT_COPY_ID, NULL);
3788 mlx5_hlist_unregister(priv->mreg_cp_tbl, entry);
3792 * Add the default copy action in in RX_CP_TBL.
3794 * This functions is called in the mlx5_dev_start(). No thread safe
3798 * Pointer to Ethernet device.
3800 * Perform verbose error reporting if not NULL.
3803 * 0 for success, negative value otherwise and rte_errno is set.
3806 flow_mreg_add_default_copy_action(struct rte_eth_dev *dev,
3807 struct rte_flow_error *error)
3809 struct mlx5_priv *priv = dev->data->dev_private;
3810 struct mlx5_flow_mreg_copy_resource *mcp_res;
3812 /* Check whether extensive metadata feature is engaged. */
3813 if (!priv->config.dv_flow_en ||
3814 priv->config.dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
3815 !mlx5_flow_ext_mreg_supported(dev) ||
3816 !priv->sh->dv_regc0_mask)
3819 * Add default mreg copy flow may be called multiple time, but
3820 * only be called once in stop. Avoid register it twice.
3822 if (mlx5_hlist_lookup(priv->mreg_cp_tbl, MLX5_DEFAULT_COPY_ID, NULL))
3824 mcp_res = flow_mreg_add_copy_action(dev, MLX5_DEFAULT_COPY_ID, error);
3831 * Add a flow of copying flow metadata registers in RX_CP_TBL.
3833 * All the flow having Q/RSS action should be split by
3834 * flow_mreg_split_qrss_prep() to pass by RX_CP_TBL. A flow in the RX_CP_TBL
3835 * performs the following,
3836 * - CQE->flow_tag := reg_c[1] (MARK)
3837 * - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
3838 * As CQE's flow_tag is not a register, it can't be simply copied from reg_c[1]
3839 * but there should be a flow per each MARK ID set by MARK action.
3841 * For the aforementioned reason, if there's a MARK action in flow's action
3842 * list, a corresponding flow should be added to the RX_CP_TBL in order to copy
3843 * the MARK ID to CQE's flow_tag like,
3844 * - If reg_c[1] is mark_id,
3845 * flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
3847 * For SET_META action which stores value in reg_c[0], as the destination is
3848 * also a flow metadata register (reg_b), adding a default flow is enough. Zero
3849 * MARK ID means the default flow. The default flow looks like,
3850 * - For all flow, reg_b := reg_c[0] and jump to RX_ACT_TBL.
3853 * Pointer to Ethernet device.
3855 * Pointer to flow structure.
3856 * @param[in] actions
3857 * Pointer to the list of actions.
3859 * Perform verbose error reporting if not NULL.
3862 * 0 on success, negative value otherwise and rte_errno is set.
3865 flow_mreg_update_copy_table(struct rte_eth_dev *dev,
3866 struct rte_flow *flow,
3867 const struct rte_flow_action *actions,
3868 struct rte_flow_error *error)
3870 struct mlx5_priv *priv = dev->data->dev_private;
3871 struct mlx5_dev_config *config = &priv->config;
3872 struct mlx5_flow_mreg_copy_resource *mcp_res;
3873 const struct rte_flow_action_mark *mark;
3875 /* Check whether extensive metadata feature is engaged. */
3876 if (!config->dv_flow_en ||
3877 config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
3878 !mlx5_flow_ext_mreg_supported(dev) ||
3879 !priv->sh->dv_regc0_mask)
3881 /* Find MARK action. */
3882 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3883 switch (actions->type) {
3884 case RTE_FLOW_ACTION_TYPE_FLAG:
3885 mcp_res = flow_mreg_add_copy_action
3886 (dev, MLX5_FLOW_MARK_DEFAULT, error);
3889 flow->rix_mreg_copy = mcp_res->idx;
3891 case RTE_FLOW_ACTION_TYPE_MARK:
3892 mark = (const struct rte_flow_action_mark *)
3895 flow_mreg_add_copy_action(dev, mark->id, error);
3898 flow->rix_mreg_copy = mcp_res->idx;
3907 #define MLX5_MAX_SPLIT_ACTIONS 24
3908 #define MLX5_MAX_SPLIT_ITEMS 24
3911 * Split the hairpin flow.
3912 * Since HW can't support encap and push-vlan on Rx, we move these
3914 * If the count action is after the encap then we also
3915 * move the count action. in this case the count will also measure
3919 * Pointer to Ethernet device.
3920 * @param[in] actions
3921 * Associated actions (list terminated by the END action).
3922 * @param[out] actions_rx
3924 * @param[out] actions_tx
3926 * @param[out] pattern_tx
3927 * The pattern items for the Tx flow.
3928 * @param[out] flow_id
3929 * The flow ID connected to this flow.
3935 flow_hairpin_split(struct rte_eth_dev *dev,
3936 const struct rte_flow_action actions[],
3937 struct rte_flow_action actions_rx[],
3938 struct rte_flow_action actions_tx[],
3939 struct rte_flow_item pattern_tx[],
3942 const struct rte_flow_action_raw_encap *raw_encap;
3943 const struct rte_flow_action_raw_decap *raw_decap;
3944 struct mlx5_rte_flow_action_set_tag *set_tag;
3945 struct rte_flow_action *tag_action;
3946 struct mlx5_rte_flow_item_tag *tag_item;
3947 struct rte_flow_item *item;
3951 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3952 switch (actions->type) {
3953 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3954 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3955 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3956 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3957 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3958 rte_memcpy(actions_tx, actions,
3959 sizeof(struct rte_flow_action));
3962 case RTE_FLOW_ACTION_TYPE_COUNT:
3964 rte_memcpy(actions_tx, actions,
3965 sizeof(struct rte_flow_action));
3968 rte_memcpy(actions_rx, actions,
3969 sizeof(struct rte_flow_action));
3973 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3974 raw_encap = actions->conf;
3975 if (raw_encap->size >
3976 (sizeof(struct rte_flow_item_eth) +
3977 sizeof(struct rte_flow_item_ipv4))) {
3978 memcpy(actions_tx, actions,
3979 sizeof(struct rte_flow_action));
3983 rte_memcpy(actions_rx, actions,
3984 sizeof(struct rte_flow_action));
3988 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3989 raw_decap = actions->conf;
3990 if (raw_decap->size <
3991 (sizeof(struct rte_flow_item_eth) +
3992 sizeof(struct rte_flow_item_ipv4))) {
3993 memcpy(actions_tx, actions,
3994 sizeof(struct rte_flow_action));
3997 rte_memcpy(actions_rx, actions,
3998 sizeof(struct rte_flow_action));
4003 rte_memcpy(actions_rx, actions,
4004 sizeof(struct rte_flow_action));
4009 /* Add set meta action and end action for the Rx flow. */
4010 tag_action = actions_rx;
4011 tag_action->type = (enum rte_flow_action_type)
4012 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
4014 rte_memcpy(actions_rx, actions, sizeof(struct rte_flow_action));
4016 set_tag = (void *)actions_rx;
4017 set_tag->id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_RX, 0, NULL);
4018 MLX5_ASSERT(set_tag->id > REG_NON);
4019 set_tag->data = flow_id;
4020 tag_action->conf = set_tag;
4021 /* Create Tx item list. */
4022 rte_memcpy(actions_tx, actions, sizeof(struct rte_flow_action));
4023 addr = (void *)&pattern_tx[2];
4025 item->type = (enum rte_flow_item_type)
4026 MLX5_RTE_FLOW_ITEM_TYPE_TAG;
4027 tag_item = (void *)addr;
4028 tag_item->data = flow_id;
4029 tag_item->id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_TX, 0, NULL);
4030 MLX5_ASSERT(set_tag->id > REG_NON);
4031 item->spec = tag_item;
4032 addr += sizeof(struct mlx5_rte_flow_item_tag);
4033 tag_item = (void *)addr;
4034 tag_item->data = UINT32_MAX;
4035 tag_item->id = UINT16_MAX;
4036 item->mask = tag_item;
4039 item->type = RTE_FLOW_ITEM_TYPE_END;
4044 * The last stage of splitting chain, just creates the subflow
4045 * without any modification.
4048 * Pointer to Ethernet device.
4050 * Parent flow structure pointer.
4051 * @param[in, out] sub_flow
4052 * Pointer to return the created subflow, may be NULL.
4054 * Flow rule attributes.
4056 * Pattern specification (list terminated by the END pattern item).
4057 * @param[in] actions
4058 * Associated actions (list terminated by the END action).
4059 * @param[in] flow_split_info
4060 * Pointer to flow split info structure.
4062 * Perform verbose error reporting if not NULL.
4064 * 0 on success, negative value otherwise
4067 flow_create_split_inner(struct rte_eth_dev *dev,
4068 struct rte_flow *flow,
4069 struct mlx5_flow **sub_flow,
4070 const struct rte_flow_attr *attr,
4071 const struct rte_flow_item items[],
4072 const struct rte_flow_action actions[],
4073 struct mlx5_flow_split_info *flow_split_info,
4074 struct rte_flow_error *error)
4076 struct mlx5_flow *dev_flow;
4078 dev_flow = flow_drv_prepare(dev, flow, attr, items, actions,
4079 flow_split_info->flow_idx, error);
4082 dev_flow->flow = flow;
4083 dev_flow->external = flow_split_info->external;
4084 dev_flow->skip_scale = flow_split_info->skip_scale;
4085 /* Subflow object was created, we must include one in the list. */
4086 SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
4087 dev_flow->handle, next);
4089 * If dev_flow is as one of the suffix flow, some actions in suffix
4090 * flow may need some user defined item layer flags, and pass the
4091 * Metadate rxq mark flag to suffix flow as well.
4093 if (flow_split_info->prefix_layers)
4094 dev_flow->handle->layers = flow_split_info->prefix_layers;
4095 if (flow_split_info->prefix_mark)
4096 dev_flow->handle->mark = 1;
4098 *sub_flow = dev_flow;
4099 return flow_drv_translate(dev, dev_flow, attr, items, actions, error);
4103 * Split the meter flow.
4105 * As meter flow will split to three sub flow, other than meter
4106 * action, the other actions make sense to only meter accepts
4107 * the packet. If it need to be dropped, no other additional
4108 * actions should be take.
4110 * One kind of special action which decapsulates the L3 tunnel
4111 * header will be in the prefix sub flow, as not to take the
4112 * L3 tunnel header into account.
4115 * Pointer to Ethernet device.
4117 * Pattern specification (list terminated by the END pattern item).
4118 * @param[out] sfx_items
4119 * Suffix flow match items (list terminated by the END pattern item).
4120 * @param[in] actions
4121 * Associated actions (list terminated by the END action).
4122 * @param[out] actions_sfx
4123 * Suffix flow actions.
4124 * @param[out] actions_pre
4125 * Prefix flow actions.
4126 * @param[out] pattern_sfx
4127 * The pattern items for the suffix flow.
4128 * @param[out] tag_sfx
4129 * Pointer to suffix flow tag.
4135 flow_meter_split_prep(struct rte_eth_dev *dev,
4136 const struct rte_flow_item items[],
4137 struct rte_flow_item sfx_items[],
4138 const struct rte_flow_action actions[],
4139 struct rte_flow_action actions_sfx[],
4140 struct rte_flow_action actions_pre[])
4142 struct mlx5_priv *priv = dev->data->dev_private;
4143 struct rte_flow_action *tag_action = NULL;
4144 struct rte_flow_item *tag_item;
4145 struct mlx5_rte_flow_action_set_tag *set_tag;
4146 struct rte_flow_error error;
4147 const struct rte_flow_action_raw_encap *raw_encap;
4148 const struct rte_flow_action_raw_decap *raw_decap;
4149 struct mlx5_rte_flow_item_tag *tag_spec;
4150 struct mlx5_rte_flow_item_tag *tag_mask;
4151 uint32_t tag_id = 0;
4152 bool copy_vlan = false;
4154 /* Prepare the actions for prefix and suffix flow. */
4155 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4156 struct rte_flow_action **action_cur = NULL;
4158 switch (actions->type) {
4159 case RTE_FLOW_ACTION_TYPE_METER:
4160 /* Add the extra tag action first. */
4161 tag_action = actions_pre;
4162 tag_action->type = (enum rte_flow_action_type)
4163 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
4165 action_cur = &actions_pre;
4167 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4168 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4169 action_cur = &actions_pre;
4171 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4172 raw_encap = actions->conf;
4173 if (raw_encap->size < MLX5_ENCAPSULATION_DECISION_SIZE)
4174 action_cur = &actions_pre;
4176 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4177 raw_decap = actions->conf;
4178 if (raw_decap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
4179 action_cur = &actions_pre;
4181 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4182 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4189 action_cur = &actions_sfx;
4190 memcpy(*action_cur, actions, sizeof(struct rte_flow_action));
4193 /* Add end action to the actions. */
4194 actions_sfx->type = RTE_FLOW_ACTION_TYPE_END;
4195 actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
4198 set_tag = (void *)actions_pre;
4199 set_tag->id = mlx5_flow_get_reg_id(dev, MLX5_MTR_SFX, 0, &error);
4200 mlx5_ipool_malloc(priv->sh->ipool[MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
4202 if (tag_id >= (1 << (sizeof(tag_id) * 8 - MLX5_MTR_COLOR_BITS))) {
4203 DRV_LOG(ERR, "Port %u meter flow id exceed max limit.",
4204 dev->data->port_id);
4205 mlx5_ipool_free(priv->sh->ipool
4206 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], tag_id);
4208 } else if (!tag_id) {
4211 set_tag->data = tag_id << MLX5_MTR_COLOR_BITS;
4213 tag_action->conf = set_tag;
4214 /* Prepare the suffix subflow items. */
4215 tag_item = sfx_items++;
4216 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4217 int item_type = items->type;
4219 switch (item_type) {
4220 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4221 memcpy(sfx_items, items, sizeof(*sfx_items));
4224 case RTE_FLOW_ITEM_TYPE_VLAN:
4226 memcpy(sfx_items, items, sizeof(*sfx_items));
4228 * Convert to internal match item, it is used
4229 * for vlan push and set vid.
4231 sfx_items->type = (enum rte_flow_item_type)
4232 MLX5_RTE_FLOW_ITEM_TYPE_VLAN;
4240 sfx_items->type = RTE_FLOW_ITEM_TYPE_END;
4242 tag_spec = (struct mlx5_rte_flow_item_tag *)sfx_items;
4243 tag_spec->data = tag_id << MLX5_MTR_COLOR_BITS;
4244 tag_spec->id = mlx5_flow_get_reg_id(dev, MLX5_MTR_SFX, 0, &error);
4245 tag_mask = tag_spec + 1;
4246 tag_mask->data = 0xffffff00;
4247 tag_item->type = (enum rte_flow_item_type)
4248 MLX5_RTE_FLOW_ITEM_TYPE_TAG;
4249 tag_item->spec = tag_spec;
4250 tag_item->last = NULL;
4251 tag_item->mask = tag_mask;
4256 * Split action list having QUEUE/RSS for metadata register copy.
4258 * Once Q/RSS action is detected in user's action list, the flow action
4259 * should be split in order to copy metadata registers, which will happen in
4261 * - CQE->flow_tag := reg_c[1] (MARK)
4262 * - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
4263 * The Q/RSS action will be performed on RX_ACT_TBL after passing by RX_CP_TBL.
4264 * This is because the last action of each flow must be a terminal action
4265 * (QUEUE, RSS or DROP).
4267 * Flow ID must be allocated to identify actions in the RX_ACT_TBL and it is
4268 * stored and kept in the mlx5_flow structure per each sub_flow.
4270 * The Q/RSS action is replaced with,
4271 * - SET_TAG, setting the allocated flow ID to reg_c[2].
4272 * And the following JUMP action is added at the end,
4273 * - JUMP, to RX_CP_TBL.
4275 * A flow to perform remained Q/RSS action will be created in RX_ACT_TBL by
4276 * flow_create_split_metadata() routine. The flow will look like,
4277 * - If flow ID matches (reg_c[2]), perform Q/RSS.
4280 * Pointer to Ethernet device.
4281 * @param[out] split_actions
4282 * Pointer to store split actions to jump to CP_TBL.
4283 * @param[in] actions
4284 * Pointer to the list of original flow actions.
4286 * Pointer to the Q/RSS action.
4287 * @param[in] actions_n
4288 * Number of original actions.
4290 * Perform verbose error reporting if not NULL.
4293 * non-zero unique flow_id on success, otherwise 0 and
4294 * error/rte_error are set.
4297 flow_mreg_split_qrss_prep(struct rte_eth_dev *dev,
4298 struct rte_flow_action *split_actions,
4299 const struct rte_flow_action *actions,
4300 const struct rte_flow_action *qrss,
4301 int actions_n, struct rte_flow_error *error)
4303 struct mlx5_priv *priv = dev->data->dev_private;
4304 struct mlx5_rte_flow_action_set_tag *set_tag;
4305 struct rte_flow_action_jump *jump;
4306 const int qrss_idx = qrss - actions;
4307 uint32_t flow_id = 0;
4311 * Given actions will be split
4312 * - Replace QUEUE/RSS action with SET_TAG to set flow ID.
4313 * - Add jump to mreg CP_TBL.
4314 * As a result, there will be one more action.
4317 memcpy(split_actions, actions, sizeof(*split_actions) * actions_n);
4318 set_tag = (void *)(split_actions + actions_n);
4320 * If tag action is not set to void(it means we are not the meter
4321 * suffix flow), add the tag action. Since meter suffix flow already
4322 * has the tag added.
4324 if (split_actions[qrss_idx].type != RTE_FLOW_ACTION_TYPE_VOID) {
4326 * Allocate the new subflow ID. This one is unique within
4327 * device and not shared with representors. Otherwise,
4328 * we would have to resolve multi-thread access synch
4329 * issue. Each flow on the shared device is appended
4330 * with source vport identifier, so the resulting
4331 * flows will be unique in the shared (by master and
4332 * representors) domain even if they have coinciding
4335 mlx5_ipool_malloc(priv->sh->ipool
4336 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], &flow_id);
4338 return rte_flow_error_set(error, ENOMEM,
4339 RTE_FLOW_ERROR_TYPE_ACTION,
4340 NULL, "can't allocate id "
4341 "for split Q/RSS subflow");
4342 /* Internal SET_TAG action to set flow ID. */
4343 *set_tag = (struct mlx5_rte_flow_action_set_tag){
4346 ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0, error);
4350 /* Construct new actions array. */
4351 /* Replace QUEUE/RSS action. */
4352 split_actions[qrss_idx] = (struct rte_flow_action){
4353 .type = (enum rte_flow_action_type)
4354 MLX5_RTE_FLOW_ACTION_TYPE_TAG,
4358 /* JUMP action to jump to mreg copy table (CP_TBL). */
4359 jump = (void *)(set_tag + 1);
4360 *jump = (struct rte_flow_action_jump){
4361 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
4363 split_actions[actions_n - 2] = (struct rte_flow_action){
4364 .type = RTE_FLOW_ACTION_TYPE_JUMP,
4367 split_actions[actions_n - 1] = (struct rte_flow_action){
4368 .type = RTE_FLOW_ACTION_TYPE_END,
4374 * Extend the given action list for Tx metadata copy.
4376 * Copy the given action list to the ext_actions and add flow metadata register
4377 * copy action in order to copy reg_a set by WQE to reg_c[0].
4379 * @param[out] ext_actions
4380 * Pointer to the extended action list.
4381 * @param[in] actions
4382 * Pointer to the list of actions.
4383 * @param[in] actions_n
4384 * Number of actions in the list.
4386 * Perform verbose error reporting if not NULL.
4387 * @param[in] encap_idx
4388 * The encap action inndex.
4391 * 0 on success, negative value otherwise
4394 flow_mreg_tx_copy_prep(struct rte_eth_dev *dev,
4395 struct rte_flow_action *ext_actions,
4396 const struct rte_flow_action *actions,
4397 int actions_n, struct rte_flow_error *error,
4400 struct mlx5_flow_action_copy_mreg *cp_mreg =
4401 (struct mlx5_flow_action_copy_mreg *)
4402 (ext_actions + actions_n + 1);
4405 ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
4409 ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_TX, 0, error);
4414 memcpy(ext_actions, actions, sizeof(*ext_actions) * encap_idx);
4415 if (encap_idx == actions_n - 1) {
4416 ext_actions[actions_n - 1] = (struct rte_flow_action){
4417 .type = (enum rte_flow_action_type)
4418 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
4421 ext_actions[actions_n] = (struct rte_flow_action){
4422 .type = RTE_FLOW_ACTION_TYPE_END,
4425 ext_actions[encap_idx] = (struct rte_flow_action){
4426 .type = (enum rte_flow_action_type)
4427 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
4430 memcpy(ext_actions + encap_idx + 1, actions + encap_idx,
4431 sizeof(*ext_actions) * (actions_n - encap_idx));
4437 * Check the match action from the action list.
4439 * @param[in] actions
4440 * Pointer to the list of actions.
4442 * Flow rule attributes.
4444 * The action to be check if exist.
4445 * @param[out] match_action_pos
4446 * Pointer to the position of the matched action if exists, otherwise is -1.
4447 * @param[out] qrss_action_pos
4448 * Pointer to the position of the Queue/RSS action if exists, otherwise is -1.
4451 * > 0 the total number of actions.
4452 * 0 if not found match action in action list.
4455 flow_check_match_action(const struct rte_flow_action actions[],
4456 const struct rte_flow_attr *attr,
4457 enum rte_flow_action_type action,
4458 int *match_action_pos, int *qrss_action_pos)
4460 const struct rte_flow_action_sample *sample;
4467 *match_action_pos = -1;
4468 *qrss_action_pos = -1;
4469 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4470 if (actions->type == action) {
4472 *match_action_pos = actions_n;
4474 if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE ||
4475 actions->type == RTE_FLOW_ACTION_TYPE_RSS)
4476 *qrss_action_pos = actions_n;
4477 if (actions->type == RTE_FLOW_ACTION_TYPE_JUMP)
4479 if (actions->type == RTE_FLOW_ACTION_TYPE_SAMPLE) {
4480 sample = actions->conf;
4481 ratio = sample->ratio;
4482 sub_type = ((const struct rte_flow_action *)
4483 (sample->actions))->type;
4487 if (flag && action == RTE_FLOW_ACTION_TYPE_SAMPLE && attr->transfer) {
4489 /* JUMP Action not support for Mirroring;
4490 * Mirroring support multi-destination;
4492 if (!jump_flag && sub_type != RTE_FLOW_ACTION_TYPE_END)
4496 /* Count RTE_FLOW_ACTION_TYPE_END. */
4497 return flag ? actions_n + 1 : 0;
4500 #define SAMPLE_SUFFIX_ITEM 2
4503 * Split the sample flow.
4505 * As sample flow will split to two sub flow, sample flow with
4506 * sample action, the other actions will move to new suffix flow.
4508 * Also add unique tag id with tag action in the sample flow,
4509 * the same tag id will be as match in the suffix flow.
4512 * Pointer to Ethernet device.
4514 * FDB egress flow flag.
4515 * @param[out] sfx_items
4516 * Suffix flow match items (list terminated by the END pattern item).
4517 * @param[in] actions
4518 * Associated actions (list terminated by the END action).
4519 * @param[out] actions_sfx
4520 * Suffix flow actions.
4521 * @param[out] actions_pre
4522 * Prefix flow actions.
4523 * @param[in] actions_n
4524 * The total number of actions.
4525 * @param[in] sample_action_pos
4526 * The sample action position.
4527 * @param[in] qrss_action_pos
4528 * The Queue/RSS action position.
4530 * Perform verbose error reporting if not NULL.
4533 * 0 on success, or unique flow_id, a negative errno value
4534 * otherwise and rte_errno is set.
4537 flow_sample_split_prep(struct rte_eth_dev *dev,
4539 struct rte_flow_item sfx_items[],
4540 const struct rte_flow_action actions[],
4541 struct rte_flow_action actions_sfx[],
4542 struct rte_flow_action actions_pre[],
4544 int sample_action_pos,
4545 int qrss_action_pos,
4546 struct rte_flow_error *error)
4548 struct mlx5_priv *priv = dev->data->dev_private;
4549 struct mlx5_rte_flow_action_set_tag *set_tag;
4550 struct mlx5_rte_flow_item_tag *tag_spec;
4551 struct mlx5_rte_flow_item_tag *tag_mask;
4552 uint32_t tag_id = 0;
4556 if (sample_action_pos < 0)
4557 return rte_flow_error_set(error, EINVAL,
4558 RTE_FLOW_ERROR_TYPE_ACTION,
4559 NULL, "invalid position of sample "
4562 /* Prepare the prefix tag action. */
4563 set_tag = (void *)(actions_pre + actions_n + 1);
4564 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, 0, error);
4568 mlx5_ipool_malloc(priv->sh->ipool
4569 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], &tag_id);
4570 set_tag->data = tag_id;
4571 /* Prepare the suffix subflow items. */
4572 tag_spec = (void *)(sfx_items + SAMPLE_SUFFIX_ITEM);
4573 tag_spec->data = tag_id;
4574 tag_spec->id = set_tag->id;
4575 tag_mask = tag_spec + 1;
4576 tag_mask->data = UINT32_MAX;
4577 sfx_items[0] = (struct rte_flow_item){
4578 .type = (enum rte_flow_item_type)
4579 MLX5_RTE_FLOW_ITEM_TYPE_TAG,
4584 sfx_items[1] = (struct rte_flow_item){
4585 .type = (enum rte_flow_item_type)
4586 RTE_FLOW_ITEM_TYPE_END,
4589 /* Prepare the actions for prefix and suffix flow. */
4590 if (qrss_action_pos >= 0 && qrss_action_pos < sample_action_pos) {
4591 index = qrss_action_pos;
4592 /* Put the preceding the Queue/RSS action into prefix flow. */
4594 memcpy(actions_pre, actions,
4595 sizeof(struct rte_flow_action) * index);
4596 /* Put others preceding the sample action into prefix flow. */
4597 if (sample_action_pos > index + 1)
4598 memcpy(actions_pre + index, actions + index + 1,
4599 sizeof(struct rte_flow_action) *
4600 (sample_action_pos - index - 1));
4601 index = sample_action_pos - 1;
4602 /* Put Queue/RSS action into Suffix flow. */
4603 memcpy(actions_sfx, actions + qrss_action_pos,
4604 sizeof(struct rte_flow_action));
4607 index = sample_action_pos;
4609 memcpy(actions_pre, actions,
4610 sizeof(struct rte_flow_action) * index);
4612 /* Add the extra tag action for NIC-RX and E-Switch ingress. */
4614 actions_pre[index++] =
4615 (struct rte_flow_action){
4616 .type = (enum rte_flow_action_type)
4617 MLX5_RTE_FLOW_ACTION_TYPE_TAG,
4621 memcpy(actions_pre + index, actions + sample_action_pos,
4622 sizeof(struct rte_flow_action));
4624 actions_pre[index] = (struct rte_flow_action){
4625 .type = (enum rte_flow_action_type)
4626 RTE_FLOW_ACTION_TYPE_END,
4628 /* Put the actions after sample into Suffix flow. */
4629 memcpy(actions_sfx, actions + sample_action_pos + 1,
4630 sizeof(struct rte_flow_action) *
4631 (actions_n - sample_action_pos - 1));
4636 * The splitting for metadata feature.
4638 * - Q/RSS action on NIC Rx should be split in order to pass by
4639 * the mreg copy table (RX_CP_TBL) and then it jumps to the
4640 * action table (RX_ACT_TBL) which has the split Q/RSS action.
4642 * - All the actions on NIC Tx should have a mreg copy action to
4643 * copy reg_a from WQE to reg_c[0].
4646 * Pointer to Ethernet device.
4648 * Parent flow structure pointer.
4650 * Flow rule attributes.
4652 * Pattern specification (list terminated by the END pattern item).
4653 * @param[in] actions
4654 * Associated actions (list terminated by the END action).
4655 * @param[in] flow_split_info
4656 * Pointer to flow split info structure.
4658 * Perform verbose error reporting if not NULL.
4660 * 0 on success, negative value otherwise
4663 flow_create_split_metadata(struct rte_eth_dev *dev,
4664 struct rte_flow *flow,
4665 const struct rte_flow_attr *attr,
4666 const struct rte_flow_item items[],
4667 const struct rte_flow_action actions[],
4668 struct mlx5_flow_split_info *flow_split_info,
4669 struct rte_flow_error *error)
4671 struct mlx5_priv *priv = dev->data->dev_private;
4672 struct mlx5_dev_config *config = &priv->config;
4673 const struct rte_flow_action *qrss = NULL;
4674 struct rte_flow_action *ext_actions = NULL;
4675 struct mlx5_flow *dev_flow = NULL;
4676 uint32_t qrss_id = 0;
4683 /* Check whether extensive metadata feature is engaged. */
4684 if (!config->dv_flow_en ||
4685 config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
4686 !mlx5_flow_ext_mreg_supported(dev))
4687 return flow_create_split_inner(dev, flow, NULL, attr, items,
4688 actions, flow_split_info, error);
4689 actions_n = flow_parse_metadata_split_actions_info(actions, &qrss,
4692 /* Exclude hairpin flows from splitting. */
4693 if (qrss->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
4694 const struct rte_flow_action_queue *queue;
4697 if (mlx5_rxq_get_type(dev, queue->index) ==
4698 MLX5_RXQ_TYPE_HAIRPIN)
4700 } else if (qrss->type == RTE_FLOW_ACTION_TYPE_RSS) {
4701 const struct rte_flow_action_rss *rss;
4704 if (mlx5_rxq_get_type(dev, rss->queue[0]) ==
4705 MLX5_RXQ_TYPE_HAIRPIN)
4710 /* Check if it is in meter suffix table. */
4711 mtr_sfx = attr->group == (attr->transfer ?
4712 (MLX5_FLOW_TABLE_LEVEL_SUFFIX - 1) :
4713 MLX5_FLOW_TABLE_LEVEL_SUFFIX);
4715 * Q/RSS action on NIC Rx should be split in order to pass by
4716 * the mreg copy table (RX_CP_TBL) and then it jumps to the
4717 * action table (RX_ACT_TBL) which has the split Q/RSS action.
4719 act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
4720 sizeof(struct rte_flow_action_set_tag) +
4721 sizeof(struct rte_flow_action_jump);
4722 ext_actions = mlx5_malloc(MLX5_MEM_ZERO, act_size, 0,
4725 return rte_flow_error_set(error, ENOMEM,
4726 RTE_FLOW_ERROR_TYPE_ACTION,
4727 NULL, "no memory to split "
4730 * If we are the suffix flow of meter, tag already exist.
4731 * Set the tag action to void.
4734 ext_actions[qrss - actions].type =
4735 RTE_FLOW_ACTION_TYPE_VOID;
4737 ext_actions[qrss - actions].type =
4738 (enum rte_flow_action_type)
4739 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
4741 * Create the new actions list with removed Q/RSS action
4742 * and appended set tag and jump to register copy table
4743 * (RX_CP_TBL). We should preallocate unique tag ID here
4744 * in advance, because it is needed for set tag action.
4746 qrss_id = flow_mreg_split_qrss_prep(dev, ext_actions, actions,
4747 qrss, actions_n, error);
4748 if (!mtr_sfx && !qrss_id) {
4752 } else if (attr->egress && !attr->transfer) {
4754 * All the actions on NIC Tx should have a metadata register
4755 * copy action to copy reg_a from WQE to reg_c[meta]
4757 act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
4758 sizeof(struct mlx5_flow_action_copy_mreg);
4759 ext_actions = mlx5_malloc(MLX5_MEM_ZERO, act_size, 0,
4762 return rte_flow_error_set(error, ENOMEM,
4763 RTE_FLOW_ERROR_TYPE_ACTION,
4764 NULL, "no memory to split "
4766 /* Create the action list appended with copy register. */
4767 ret = flow_mreg_tx_copy_prep(dev, ext_actions, actions,
4768 actions_n, error, encap_idx);
4772 /* Add the unmodified original or prefix subflow. */
4773 ret = flow_create_split_inner(dev, flow, &dev_flow, attr,
4774 items, ext_actions ? ext_actions :
4775 actions, flow_split_info, error);
4778 MLX5_ASSERT(dev_flow);
4780 const struct rte_flow_attr q_attr = {
4781 .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
4784 /* Internal PMD action to set register. */
4785 struct mlx5_rte_flow_item_tag q_tag_spec = {
4789 struct rte_flow_item q_items[] = {
4791 .type = (enum rte_flow_item_type)
4792 MLX5_RTE_FLOW_ITEM_TYPE_TAG,
4793 .spec = &q_tag_spec,
4798 .type = RTE_FLOW_ITEM_TYPE_END,
4801 struct rte_flow_action q_actions[] = {
4807 .type = RTE_FLOW_ACTION_TYPE_END,
4810 uint64_t layers = flow_get_prefix_layer_flags(dev_flow);
4813 * Configure the tag item only if there is no meter subflow.
4814 * Since tag is already marked in the meter suffix subflow
4815 * we can just use the meter suffix items as is.
4818 /* Not meter subflow. */
4819 MLX5_ASSERT(!mtr_sfx);
4821 * Put unique id in prefix flow due to it is destroyed
4822 * after suffix flow and id will be freed after there
4823 * is no actual flows with this id and identifier
4824 * reallocation becomes possible (for example, for
4825 * other flows in other threads).
4827 dev_flow->handle->split_flow_id = qrss_id;
4828 ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0,
4832 q_tag_spec.id = ret;
4835 /* Add suffix subflow to execute Q/RSS. */
4836 flow_split_info->prefix_layers = layers;
4837 flow_split_info->prefix_mark = 0;
4838 ret = flow_create_split_inner(dev, flow, &dev_flow,
4839 &q_attr, mtr_sfx ? items :
4841 flow_split_info, error);
4844 /* qrss ID should be freed if failed. */
4846 MLX5_ASSERT(dev_flow);
4851 * We do not destroy the partially created sub_flows in case of error.
4852 * These ones are included into parent flow list and will be destroyed
4853 * by flow_drv_destroy.
4855 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
4857 mlx5_free(ext_actions);
4862 * The splitting for meter feature.
4864 * - The meter flow will be split to two flows as prefix and
4865 * suffix flow. The packets make sense only it pass the prefix
4868 * - Reg_C_5 is used for the packet to match betweend prefix and
4872 * Pointer to Ethernet device.
4874 * Parent flow structure pointer.
4876 * Flow rule attributes.
4878 * Pattern specification (list terminated by the END pattern item).
4879 * @param[in] actions
4880 * Associated actions (list terminated by the END action).
4881 * @param[in] flow_split_info
4882 * Pointer to flow split info structure.
4884 * Perform verbose error reporting if not NULL.
4886 * 0 on success, negative value otherwise
4889 flow_create_split_meter(struct rte_eth_dev *dev,
4890 struct rte_flow *flow,
4891 const struct rte_flow_attr *attr,
4892 const struct rte_flow_item items[],
4893 const struct rte_flow_action actions[],
4894 struct mlx5_flow_split_info *flow_split_info,
4895 struct rte_flow_error *error)
4897 struct mlx5_priv *priv = dev->data->dev_private;
4898 struct rte_flow_action *sfx_actions = NULL;
4899 struct rte_flow_action *pre_actions = NULL;
4900 struct rte_flow_item *sfx_items = NULL;
4901 struct mlx5_flow *dev_flow = NULL;
4902 struct rte_flow_attr sfx_attr = *attr;
4904 uint32_t mtr_tag_id = 0;
4911 actions_n = flow_check_meter_action(actions, &mtr);
4913 /* The five prefix actions: meter, decap, encap, tag, end. */
4914 act_size = sizeof(struct rte_flow_action) * (actions_n + 5) +
4915 sizeof(struct mlx5_rte_flow_action_set_tag);
4916 /* tag, vlan, port id, end. */
4917 #define METER_SUFFIX_ITEM 4
4918 item_size = sizeof(struct rte_flow_item) * METER_SUFFIX_ITEM +
4919 sizeof(struct mlx5_rte_flow_item_tag) * 2;
4920 sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size + item_size),
4923 return rte_flow_error_set(error, ENOMEM,
4924 RTE_FLOW_ERROR_TYPE_ACTION,
4925 NULL, "no memory to split "
4927 sfx_items = (struct rte_flow_item *)((char *)sfx_actions +
4929 pre_actions = sfx_actions + actions_n;
4930 mtr_tag_id = flow_meter_split_prep(dev, items, sfx_items,
4931 actions, sfx_actions,
4937 /* Add the prefix subflow. */
4938 flow_split_info->prefix_mark = 0;
4939 ret = flow_create_split_inner(dev, flow, &dev_flow,
4940 attr, items, pre_actions,
4941 flow_split_info, error);
4946 dev_flow->handle->split_flow_id = mtr_tag_id;
4947 /* Setting the sfx group atrr. */
4948 sfx_attr.group = sfx_attr.transfer ?
4949 (MLX5_FLOW_TABLE_LEVEL_SUFFIX - 1) :
4950 MLX5_FLOW_TABLE_LEVEL_SUFFIX;
4951 flow_split_info->prefix_layers =
4952 flow_get_prefix_layer_flags(dev_flow);
4953 flow_split_info->prefix_mark = dev_flow->handle->mark;
4955 /* Add the prefix subflow. */
4956 ret = flow_create_split_metadata(dev, flow,
4957 &sfx_attr, sfx_items ?
4959 sfx_actions ? sfx_actions : actions,
4960 flow_split_info, error);
4963 mlx5_free(sfx_actions);
4968 * The splitting for sample feature.
4970 * Once Sample action is detected in the action list, the flow actions should
4971 * be split into prefix sub flow and suffix sub flow.
4973 * The original items remain in the prefix sub flow, all actions preceding the
4974 * sample action and the sample action itself will be copied to the prefix
4975 * sub flow, the actions following the sample action will be copied to the
4976 * suffix sub flow, Queue action always be located in the suffix sub flow.
4978 * In order to make the packet from prefix sub flow matches with suffix sub
4979 * flow, an extra tag action be added into prefix sub flow, and the suffix sub
4980 * flow uses tag item with the unique flow id.
4983 * Pointer to Ethernet device.
4985 * Parent flow structure pointer.
4987 * Flow rule attributes.
4989 * Pattern specification (list terminated by the END pattern item).
4990 * @param[in] actions
4991 * Associated actions (list terminated by the END action).
4992 * @param[in] flow_split_info
4993 * Pointer to flow split info structure.
4995 * Perform verbose error reporting if not NULL.
4997 * 0 on success, negative value otherwise
5000 flow_create_split_sample(struct rte_eth_dev *dev,
5001 struct rte_flow *flow,
5002 const struct rte_flow_attr *attr,
5003 const struct rte_flow_item items[],
5004 const struct rte_flow_action actions[],
5005 struct mlx5_flow_split_info *flow_split_info,
5006 struct rte_flow_error *error)
5008 struct mlx5_priv *priv = dev->data->dev_private;
5009 struct rte_flow_action *sfx_actions = NULL;
5010 struct rte_flow_action *pre_actions = NULL;
5011 struct rte_flow_item *sfx_items = NULL;
5012 struct mlx5_flow *dev_flow = NULL;
5013 struct rte_flow_attr sfx_attr = *attr;
5014 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
5015 struct mlx5_flow_dv_sample_resource *sample_res;
5016 struct mlx5_flow_tbl_data_entry *sfx_tbl_data;
5017 struct mlx5_flow_tbl_resource *sfx_tbl;
5018 union mlx5_flow_tbl_key sfx_table_key;
5022 uint32_t fdb_tx = 0;
5025 int sample_action_pos;
5026 int qrss_action_pos;
5029 if (priv->sampler_en)
5030 actions_n = flow_check_match_action(actions, attr,
5031 RTE_FLOW_ACTION_TYPE_SAMPLE,
5032 &sample_action_pos, &qrss_action_pos);
5034 /* The prefix actions must includes sample, tag, end. */
5035 act_size = sizeof(struct rte_flow_action) * (actions_n * 2 + 1)
5036 + sizeof(struct mlx5_rte_flow_action_set_tag);
5037 item_size = sizeof(struct rte_flow_item) * SAMPLE_SUFFIX_ITEM +
5038 sizeof(struct mlx5_rte_flow_item_tag) * 2;
5039 sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size +
5040 item_size), 0, SOCKET_ID_ANY);
5042 return rte_flow_error_set(error, ENOMEM,
5043 RTE_FLOW_ERROR_TYPE_ACTION,
5044 NULL, "no memory to split "
5046 /* The representor_id is -1 for uplink. */
5047 fdb_tx = (attr->transfer && priv->representor_id != -1);
5049 sfx_items = (struct rte_flow_item *)((char *)sfx_actions
5051 pre_actions = sfx_actions + actions_n;
5052 tag_id = flow_sample_split_prep(dev, fdb_tx, sfx_items,
5053 actions, sfx_actions,
5054 pre_actions, actions_n,
5056 qrss_action_pos, error);
5057 if (tag_id < 0 || (!fdb_tx && !tag_id)) {
5061 /* Add the prefix subflow. */
5062 ret = flow_create_split_inner(dev, flow, &dev_flow, attr,
5064 flow_split_info, error);
5069 dev_flow->handle->split_flow_id = tag_id;
5070 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
5071 /* Set the sfx group attr. */
5072 sample_res = (struct mlx5_flow_dv_sample_resource *)
5073 dev_flow->dv.sample_res;
5074 sfx_tbl = (struct mlx5_flow_tbl_resource *)
5075 sample_res->normal_path_tbl;
5076 sfx_tbl_data = container_of(sfx_tbl,
5077 struct mlx5_flow_tbl_data_entry, tbl);
5078 sfx_table_key.v64 = sfx_tbl_data->entry.key;
5079 sfx_attr.group = sfx_attr.transfer ?
5080 (sfx_table_key.table_id - 1) :
5081 sfx_table_key.table_id;
5082 flow_split_info->prefix_layers =
5083 flow_get_prefix_layer_flags(dev_flow);
5084 flow_split_info->prefix_mark = dev_flow->handle->mark;
5085 /* Suffix group level already be scaled with factor, set
5086 * skip_scale to 1 to avoid scale again in translation.
5088 flow_split_info->skip_scale = 1;
5091 /* Add the suffix subflow. */
5092 ret = flow_create_split_meter(dev, flow, &sfx_attr,
5093 sfx_items ? sfx_items : items,
5094 sfx_actions ? sfx_actions : actions,
5095 flow_split_info, error);
5098 mlx5_free(sfx_actions);
5103 * Split the flow to subflow set. The splitters might be linked
5104 * in the chain, like this:
5105 * flow_create_split_outer() calls:
5106 * flow_create_split_meter() calls:
5107 * flow_create_split_metadata(meter_subflow_0) calls:
5108 * flow_create_split_inner(metadata_subflow_0)
5109 * flow_create_split_inner(metadata_subflow_1)
5110 * flow_create_split_inner(metadata_subflow_2)
5111 * flow_create_split_metadata(meter_subflow_1) calls:
5112 * flow_create_split_inner(metadata_subflow_0)
5113 * flow_create_split_inner(metadata_subflow_1)
5114 * flow_create_split_inner(metadata_subflow_2)
5116 * This provide flexible way to add new levels of flow splitting.
5117 * The all of successfully created subflows are included to the
5118 * parent flow dev_flow list.
5121 * Pointer to Ethernet device.
5123 * Parent flow structure pointer.
5125 * Flow rule attributes.
5127 * Pattern specification (list terminated by the END pattern item).
5128 * @param[in] actions
5129 * Associated actions (list terminated by the END action).
5130 * @param[in] flow_split_info
5131 * Pointer to flow split info structure.
5133 * Perform verbose error reporting if not NULL.
5135 * 0 on success, negative value otherwise
5138 flow_create_split_outer(struct rte_eth_dev *dev,
5139 struct rte_flow *flow,
5140 const struct rte_flow_attr *attr,
5141 const struct rte_flow_item items[],
5142 const struct rte_flow_action actions[],
5143 struct mlx5_flow_split_info *flow_split_info,
5144 struct rte_flow_error *error)
5148 ret = flow_create_split_sample(dev, flow, attr, items,
5149 actions, flow_split_info, error);
5150 MLX5_ASSERT(ret <= 0);
5154 static struct mlx5_flow_tunnel *
5155 flow_tunnel_from_rule(struct rte_eth_dev *dev,
5156 const struct rte_flow_attr *attr,
5157 const struct rte_flow_item items[],
5158 const struct rte_flow_action actions[])
5160 struct mlx5_flow_tunnel *tunnel;
5162 #pragma GCC diagnostic push
5163 #pragma GCC diagnostic ignored "-Wcast-qual"
5164 if (is_flow_tunnel_match_rule(dev, attr, items, actions))
5165 tunnel = (struct mlx5_flow_tunnel *)items[0].spec;
5166 else if (is_flow_tunnel_steer_rule(dev, attr, items, actions))
5167 tunnel = (struct mlx5_flow_tunnel *)actions[0].conf;
5170 #pragma GCC diagnostic pop
5176 * Adjust flow RSS workspace if needed.
5179 * Pointer to thread flow work space.
5181 * Pointer to RSS descriptor.
5182 * @param[in] nrssq_num
5183 * New RSS queue number.
5186 * 0 on success, -1 otherwise and rte_errno is set.
5189 flow_rss_workspace_adjust(struct mlx5_flow_workspace *wks,
5190 struct mlx5_flow_rss_desc *rss_desc,
5193 if (likely(nrssq_num <= wks->rssq_num))
5195 rss_desc->queue = realloc(rss_desc->queue,
5196 sizeof(*rss_desc->queue) * RTE_ALIGN(nrssq_num, 2));
5197 if (!rss_desc->queue) {
5201 wks->rssq_num = RTE_ALIGN(nrssq_num, 2);
5206 * Create a flow and add it to @p list.
5209 * Pointer to Ethernet device.
5211 * Pointer to a TAILQ flow list. If this parameter NULL,
5212 * no list insertion occurred, flow is just created,
5213 * this is caller's responsibility to track the
5216 * Flow rule attributes.
5218 * Pattern specification (list terminated by the END pattern item).
5219 * @param[in] actions
5220 * Associated actions (list terminated by the END action).
5221 * @param[in] external
5222 * This flow rule is created by request external to PMD.
5224 * Perform verbose error reporting if not NULL.
5227 * A flow index on success, 0 otherwise and rte_errno is set.
5230 flow_list_create(struct rte_eth_dev *dev, uint32_t *list,
5231 const struct rte_flow_attr *attr,
5232 const struct rte_flow_item items[],
5233 const struct rte_flow_action original_actions[],
5234 bool external, struct rte_flow_error *error)
5236 struct mlx5_priv *priv = dev->data->dev_private;
5237 struct rte_flow *flow = NULL;
5238 struct mlx5_flow *dev_flow;
5239 const struct rte_flow_action_rss *rss;
5240 struct mlx5_translated_shared_action
5241 shared_actions[MLX5_MAX_SHARED_ACTIONS];
5242 int shared_actions_n = MLX5_MAX_SHARED_ACTIONS;
5244 struct mlx5_flow_expand_rss buf;
5245 uint8_t buffer[2048];
5248 struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
5249 uint8_t buffer[2048];
5252 struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
5253 uint8_t buffer[2048];
5254 } actions_hairpin_tx;
5256 struct rte_flow_item items[MLX5_MAX_SPLIT_ITEMS];
5257 uint8_t buffer[2048];
5259 struct mlx5_flow_expand_rss *buf = &expand_buffer.buf;
5260 struct mlx5_flow_rss_desc *rss_desc;
5261 const struct rte_flow_action *p_actions_rx;
5265 struct rte_flow_attr attr_tx = { .priority = 0 };
5266 const struct rte_flow_action *actions;
5267 struct rte_flow_action *translated_actions = NULL;
5268 struct mlx5_flow_tunnel *tunnel;
5269 struct tunnel_default_miss_ctx default_miss_ctx = { 0, };
5270 struct mlx5_flow_workspace *wks = mlx5_flow_push_thread_workspace();
5271 struct mlx5_flow_split_info flow_split_info = {
5272 .external = !!external,
5281 rss_desc = &wks->rss_desc;
5282 ret = flow_shared_actions_translate(dev, original_actions,
5285 &translated_actions, error);
5287 MLX5_ASSERT(translated_actions == NULL);
5290 actions = translated_actions ? translated_actions : original_actions;
5291 p_actions_rx = actions;
5292 hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
5293 ret = flow_drv_validate(dev, attr, items, p_actions_rx,
5294 external, hairpin_flow, error);
5296 goto error_before_hairpin_split;
5297 flow = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW], &idx);
5300 goto error_before_hairpin_split;
5302 if (hairpin_flow > 0) {
5303 if (hairpin_flow > MLX5_MAX_SPLIT_ACTIONS) {
5305 goto error_before_hairpin_split;
5307 flow_hairpin_split(dev, actions, actions_rx.actions,
5308 actions_hairpin_tx.actions, items_tx.items,
5310 p_actions_rx = actions_rx.actions;
5312 flow_split_info.flow_idx = idx;
5313 flow->drv_type = flow_get_drv_type(dev, attr);
5314 MLX5_ASSERT(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
5315 flow->drv_type < MLX5_FLOW_TYPE_MAX);
5316 memset(rss_desc, 0, offsetof(struct mlx5_flow_rss_desc, queue));
5317 rss = flow_get_rss_action(p_actions_rx);
5319 if (flow_rss_workspace_adjust(wks, rss_desc, rss->queue_num))
5322 * The following information is required by
5323 * mlx5_flow_hashfields_adjust() in advance.
5325 rss_desc->level = rss->level;
5326 /* RSS type 0 indicates default RSS type (ETH_RSS_IP). */
5327 rss_desc->types = !rss->types ? ETH_RSS_IP : rss->types;
5329 flow->dev_handles = 0;
5330 if (rss && rss->types) {
5331 unsigned int graph_root;
5333 graph_root = find_graph_root(items, rss->level);
5334 ret = mlx5_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
5336 mlx5_support_expansion, graph_root);
5337 MLX5_ASSERT(ret > 0 &&
5338 (unsigned int)ret < sizeof(expand_buffer.buffer));
5341 buf->entry[0].pattern = (void *)(uintptr_t)items;
5343 rss_desc->shared_rss = flow_get_shared_rss_action(dev, shared_actions,
5345 for (i = 0; i < buf->entries; ++i) {
5346 /* Initialize flow split data. */
5347 flow_split_info.prefix_layers = 0;
5348 flow_split_info.prefix_mark = 0;
5349 flow_split_info.skip_scale = 0;
5351 * The splitter may create multiple dev_flows,
5352 * depending on configuration. In the simplest
5353 * case it just creates unmodified original flow.
5355 ret = flow_create_split_outer(dev, flow, attr,
5356 buf->entry[i].pattern,
5357 p_actions_rx, &flow_split_info,
5361 if (is_flow_tunnel_steer_rule(dev, attr,
5362 buf->entry[i].pattern,
5364 ret = flow_tunnel_add_default_miss(dev, flow, attr,
5370 mlx5_free(default_miss_ctx.queue);
5375 /* Create the tx flow. */
5377 attr_tx.group = MLX5_HAIRPIN_TX_TABLE;
5378 attr_tx.ingress = 0;
5380 dev_flow = flow_drv_prepare(dev, flow, &attr_tx, items_tx.items,
5381 actions_hairpin_tx.actions,
5385 dev_flow->flow = flow;
5386 dev_flow->external = 0;
5387 SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
5388 dev_flow->handle, next);
5389 ret = flow_drv_translate(dev, dev_flow, &attr_tx,
5391 actions_hairpin_tx.actions, error);
5396 * Update the metadata register copy table. If extensive
5397 * metadata feature is enabled and registers are supported
5398 * we might create the extra rte_flow for each unique
5399 * MARK/FLAG action ID.
5401 * The table is updated for ingress Flows only, because
5402 * the egress Flows belong to the different device and
5403 * copy table should be updated in peer NIC Rx domain.
5405 if (attr->ingress &&
5406 (external || attr->group != MLX5_FLOW_MREG_CP_TABLE_GROUP)) {
5407 ret = flow_mreg_update_copy_table(dev, flow, actions, error);
5412 * If the flow is external (from application) OR device is started,
5413 * OR mreg discover, then apply immediately.
5415 if (external || dev->data->dev_started ||
5416 (attr->group == MLX5_FLOW_MREG_CP_TABLE_GROUP &&
5417 attr->priority == MLX5_FLOW_PRIO_RSVD)) {
5418 ret = flow_drv_apply(dev, flow, error);
5423 rte_spinlock_lock(&priv->flow_list_lock);
5424 ILIST_INSERT(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW], list, idx,
5426 rte_spinlock_unlock(&priv->flow_list_lock);
5428 flow_rxq_flags_set(dev, flow);
5429 rte_free(translated_actions);
5430 tunnel = flow_tunnel_from_rule(dev, attr, items, actions);
5433 flow->tunnel_id = tunnel->tunnel_id;
5434 __atomic_add_fetch(&tunnel->refctn, 1, __ATOMIC_RELAXED);
5435 mlx5_free(default_miss_ctx.queue);
5437 mlx5_flow_pop_thread_workspace();
5441 ret = rte_errno; /* Save rte_errno before cleanup. */
5442 flow_mreg_del_copy_action(dev, flow);
5443 flow_drv_destroy(dev, flow);
5444 if (rss_desc->shared_rss)
5445 __atomic_sub_fetch(&((struct mlx5_shared_action_rss *)
5447 (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
5448 rss_desc->shared_rss))->refcnt, 1, __ATOMIC_RELAXED);
5449 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW], idx);
5450 rte_errno = ret; /* Restore rte_errno. */
5453 mlx5_flow_pop_thread_workspace();
5454 error_before_hairpin_split:
5455 rte_free(translated_actions);
5460 * Create a dedicated flow rule on e-switch table 0 (root table), to direct all
5461 * incoming packets to table 1.
5463 * Other flow rules, requested for group n, will be created in
5464 * e-switch table n+1.
5465 * Jump action to e-switch group n will be created to group n+1.
5467 * Used when working in switchdev mode, to utilise advantages of table 1
5471 * Pointer to Ethernet device.
5474 * Pointer to flow on success, NULL otherwise and rte_errno is set.
5477 mlx5_flow_create_esw_table_zero_flow(struct rte_eth_dev *dev)
5479 const struct rte_flow_attr attr = {
5486 const struct rte_flow_item pattern = {
5487 .type = RTE_FLOW_ITEM_TYPE_END,
5489 struct rte_flow_action_jump jump = {
5492 const struct rte_flow_action actions[] = {
5494 .type = RTE_FLOW_ACTION_TYPE_JUMP,
5498 .type = RTE_FLOW_ACTION_TYPE_END,
5501 struct mlx5_priv *priv = dev->data->dev_private;
5502 struct rte_flow_error error;
5504 return (void *)(uintptr_t)flow_list_create(dev, &priv->ctrl_flows,
5506 actions, false, &error);
5510 * Validate a flow supported by the NIC.
5512 * @see rte_flow_validate()
5516 mlx5_flow_validate(struct rte_eth_dev *dev,
5517 const struct rte_flow_attr *attr,
5518 const struct rte_flow_item items[],
5519 const struct rte_flow_action original_actions[],
5520 struct rte_flow_error *error)
5523 struct mlx5_translated_shared_action
5524 shared_actions[MLX5_MAX_SHARED_ACTIONS];
5525 int shared_actions_n = MLX5_MAX_SHARED_ACTIONS;
5526 const struct rte_flow_action *actions;
5527 struct rte_flow_action *translated_actions = NULL;
5528 int ret = flow_shared_actions_translate(dev, original_actions,
5531 &translated_actions, error);
5535 actions = translated_actions ? translated_actions : original_actions;
5536 hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
5537 ret = flow_drv_validate(dev, attr, items, actions,
5538 true, hairpin_flow, error);
5539 rte_free(translated_actions);
5546 * @see rte_flow_create()
5550 mlx5_flow_create(struct rte_eth_dev *dev,
5551 const struct rte_flow_attr *attr,
5552 const struct rte_flow_item items[],
5553 const struct rte_flow_action actions[],
5554 struct rte_flow_error *error)
5556 struct mlx5_priv *priv = dev->data->dev_private;
5559 * If the device is not started yet, it is not allowed to created a
5560 * flow from application. PMD default flows and traffic control flows
5563 if (unlikely(!dev->data->dev_started)) {
5564 DRV_LOG(DEBUG, "port %u is not started when "
5565 "inserting a flow", dev->data->port_id);
5566 rte_flow_error_set(error, ENODEV,
5567 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5569 "port not started");
5573 return (void *)(uintptr_t)flow_list_create(dev, &priv->flows,
5574 attr, items, actions, true, error);
5578 * Destroy a flow in a list.
5581 * Pointer to Ethernet device.
5583 * Pointer to the Indexed flow list. If this parameter NULL,
5584 * there is no flow removal from the list. Be noted that as
5585 * flow is add to the indexed list, memory of the indexed
5586 * list points to maybe changed as flow destroyed.
5587 * @param[in] flow_idx
5588 * Index of flow to destroy.
5591 flow_list_destroy(struct rte_eth_dev *dev, uint32_t *list,
5594 struct mlx5_priv *priv = dev->data->dev_private;
5595 struct rte_flow *flow = mlx5_ipool_get(priv->sh->ipool
5596 [MLX5_IPOOL_RTE_FLOW], flow_idx);
5601 * Update RX queue flags only if port is started, otherwise it is
5604 if (dev->data->dev_started)
5605 flow_rxq_flags_trim(dev, flow);
5606 flow_drv_destroy(dev, flow);
5608 rte_spinlock_lock(&priv->flow_list_lock);
5609 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW], list,
5610 flow_idx, flow, next);
5611 rte_spinlock_unlock(&priv->flow_list_lock);
5614 struct mlx5_flow_tunnel *tunnel;
5616 rte_spinlock_lock(&mlx5_tunnel_hub(dev)->sl);
5617 tunnel = mlx5_find_tunnel_id(dev, flow->tunnel_id);
5619 LIST_REMOVE(tunnel, chain);
5620 rte_spinlock_unlock(&mlx5_tunnel_hub(dev)->sl);
5621 if (!__atomic_sub_fetch(&tunnel->refctn, 1, __ATOMIC_RELAXED))
5622 mlx5_flow_tunnel_free(dev, tunnel);
5624 flow_mreg_del_copy_action(dev, flow);
5625 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW], flow_idx);
5629 * Destroy all flows.
5632 * Pointer to Ethernet device.
5634 * Pointer to the Indexed flow list.
5636 * If flushing is called avtively.
5639 mlx5_flow_list_flush(struct rte_eth_dev *dev, uint32_t *list, bool active)
5641 uint32_t num_flushed = 0;
5644 flow_list_destroy(dev, list, *list);
5648 DRV_LOG(INFO, "port %u: %u flows flushed before stopping",
5649 dev->data->port_id, num_flushed);
5654 * Stop all default actions for flows.
5657 * Pointer to Ethernet device.
5660 mlx5_flow_stop_default(struct rte_eth_dev *dev)
5662 flow_mreg_del_default_copy_action(dev);
5663 flow_rxq_flags_clear(dev);
5667 * Start all default actions for flows.
5670 * Pointer to Ethernet device.
5672 * 0 on success, a negative errno value otherwise and rte_errno is set.
5675 mlx5_flow_start_default(struct rte_eth_dev *dev)
5677 struct rte_flow_error error;
5679 /* Make sure default copy action (reg_c[0] -> reg_b) is created. */
5680 return flow_mreg_add_default_copy_action(dev, &error);
5684 * Release key of thread specific flow workspace data.
5687 flow_release_workspace(void *data)
5689 struct mlx5_flow_workspace *wks = data;
5690 struct mlx5_flow_workspace *next;
5694 free(wks->rss_desc.queue);
5701 * Initialize key of thread specific flow workspace data.
5704 flow_alloc_workspace(void)
5706 if (pthread_key_create(&key_workspace, flow_release_workspace))
5707 DRV_LOG(ERR, "Can't create flow workspace data thread key.");
5711 * Get thread specific current flow workspace.
5713 * @return pointer to thread specific flow workspace data, NULL on error.
5715 struct mlx5_flow_workspace*
5716 mlx5_flow_get_thread_workspace(void)
5718 struct mlx5_flow_workspace *data;
5720 data = pthread_getspecific(key_workspace);
5721 MLX5_ASSERT(data && data->inuse);
5722 if (!data || !data->inuse)
5723 DRV_LOG(ERR, "flow workspace not initialized.");
5728 * Allocate and init new flow workspace.
5730 * @return pointer to flow workspace data, NULL on error.
5732 static struct mlx5_flow_workspace*
5733 flow_alloc_thread_workspace(void)
5735 struct mlx5_flow_workspace *data = calloc(1, sizeof(*data));
5738 DRV_LOG(ERR, "Failed to allocate flow workspace "
5742 data->rss_desc.queue = calloc(1,
5743 sizeof(uint16_t) * MLX5_RSSQ_DEFAULT_NUM);
5744 if (!data->rss_desc.queue)
5746 data->rssq_num = MLX5_RSSQ_DEFAULT_NUM;
5749 if (data->rss_desc.queue)
5750 free(data->rss_desc.queue);
5756 * Get new thread specific flow workspace.
5758 * If current workspace inuse, create new one and set as current.
5760 * @return pointer to thread specific flow workspace data, NULL on error.
5762 static struct mlx5_flow_workspace*
5763 mlx5_flow_push_thread_workspace(void)
5765 struct mlx5_flow_workspace *curr;
5766 struct mlx5_flow_workspace *data;
5768 if (pthread_once(&key_workspace_init, flow_alloc_workspace)) {
5769 DRV_LOG(ERR, "Failed to init flow workspace data thread key.");
5772 curr = pthread_getspecific(key_workspace);
5774 data = flow_alloc_thread_workspace();
5777 } else if (!curr->inuse) {
5779 } else if (curr->next) {
5782 data = flow_alloc_thread_workspace();
5790 /* Set as current workspace */
5791 if (pthread_setspecific(key_workspace, data))
5792 DRV_LOG(ERR, "Failed to set flow workspace to thread.");
5797 * Close current thread specific flow workspace.
5799 * If previous workspace available, set it as current.
5801 * @return pointer to thread specific flow workspace data, NULL on error.
5804 mlx5_flow_pop_thread_workspace(void)
5806 struct mlx5_flow_workspace *data = mlx5_flow_get_thread_workspace();
5811 DRV_LOG(ERR, "Failed to close unused flow workspace.");
5817 if (pthread_setspecific(key_workspace, data->prev))
5818 DRV_LOG(ERR, "Failed to set flow workspace to thread.");
5822 * Verify the flow list is empty
5825 * Pointer to Ethernet device.
5827 * @return the number of flows not released.
5830 mlx5_flow_verify(struct rte_eth_dev *dev)
5832 struct mlx5_priv *priv = dev->data->dev_private;
5833 struct rte_flow *flow;
5837 ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW], priv->flows, idx,
5839 DRV_LOG(DEBUG, "port %u flow %p still referenced",
5840 dev->data->port_id, (void *)flow);
5847 * Enable default hairpin egress flow.
5850 * Pointer to Ethernet device.
5855 * 0 on success, a negative errno value otherwise and rte_errno is set.
5858 mlx5_ctrl_flow_source_queue(struct rte_eth_dev *dev,
5861 struct mlx5_priv *priv = dev->data->dev_private;
5862 const struct rte_flow_attr attr = {
5866 struct mlx5_rte_flow_item_tx_queue queue_spec = {
5869 struct mlx5_rte_flow_item_tx_queue queue_mask = {
5870 .queue = UINT32_MAX,
5872 struct rte_flow_item items[] = {
5874 .type = (enum rte_flow_item_type)
5875 MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE,
5876 .spec = &queue_spec,
5878 .mask = &queue_mask,
5881 .type = RTE_FLOW_ITEM_TYPE_END,
5884 struct rte_flow_action_jump jump = {
5885 .group = MLX5_HAIRPIN_TX_TABLE,
5887 struct rte_flow_action actions[2];
5889 struct rte_flow_error error;
5891 actions[0].type = RTE_FLOW_ACTION_TYPE_JUMP;
5892 actions[0].conf = &jump;
5893 actions[1].type = RTE_FLOW_ACTION_TYPE_END;
5894 flow_idx = flow_list_create(dev, &priv->ctrl_flows,
5895 &attr, items, actions, false, &error);
5898 "Failed to create ctrl flow: rte_errno(%d),"
5899 " type(%d), message(%s)",
5900 rte_errno, error.type,
5901 error.message ? error.message : " (no stated reason)");
5908 * Enable a control flow configured from the control plane.
5911 * Pointer to Ethernet device.
5913 * An Ethernet flow spec to apply.
5915 * An Ethernet flow mask to apply.
5917 * A VLAN flow spec to apply.
5919 * A VLAN flow mask to apply.
5922 * 0 on success, a negative errno value otherwise and rte_errno is set.
5925 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
5926 struct rte_flow_item_eth *eth_spec,
5927 struct rte_flow_item_eth *eth_mask,
5928 struct rte_flow_item_vlan *vlan_spec,
5929 struct rte_flow_item_vlan *vlan_mask)
5931 struct mlx5_priv *priv = dev->data->dev_private;
5932 const struct rte_flow_attr attr = {
5934 .priority = MLX5_FLOW_PRIO_RSVD,
5936 struct rte_flow_item items[] = {
5938 .type = RTE_FLOW_ITEM_TYPE_ETH,
5944 .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
5945 RTE_FLOW_ITEM_TYPE_END,
5951 .type = RTE_FLOW_ITEM_TYPE_END,
5954 uint16_t queue[priv->reta_idx_n];
5955 struct rte_flow_action_rss action_rss = {
5956 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
5958 .types = priv->rss_conf.rss_hf,
5959 .key_len = priv->rss_conf.rss_key_len,
5960 .queue_num = priv->reta_idx_n,
5961 .key = priv->rss_conf.rss_key,
5964 struct rte_flow_action actions[] = {
5966 .type = RTE_FLOW_ACTION_TYPE_RSS,
5967 .conf = &action_rss,
5970 .type = RTE_FLOW_ACTION_TYPE_END,
5974 struct rte_flow_error error;
5977 if (!priv->reta_idx_n || !priv->rxqs_n) {
5980 if (!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
5981 action_rss.types = 0;
5982 for (i = 0; i != priv->reta_idx_n; ++i)
5983 queue[i] = (*priv->reta_idx)[i];
5984 flow_idx = flow_list_create(dev, &priv->ctrl_flows,
5985 &attr, items, actions, false, &error);
5992 * Enable a flow control configured from the control plane.
5995 * Pointer to Ethernet device.
5997 * An Ethernet flow spec to apply.
5999 * An Ethernet flow mask to apply.
6002 * 0 on success, a negative errno value otherwise and rte_errno is set.
6005 mlx5_ctrl_flow(struct rte_eth_dev *dev,
6006 struct rte_flow_item_eth *eth_spec,
6007 struct rte_flow_item_eth *eth_mask)
6009 return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
6013 * Create default miss flow rule matching lacp traffic
6016 * Pointer to Ethernet device.
6018 * An Ethernet flow spec to apply.
6021 * 0 on success, a negative errno value otherwise and rte_errno is set.
6024 mlx5_flow_lacp_miss(struct rte_eth_dev *dev)
6026 struct mlx5_priv *priv = dev->data->dev_private;
6028 * The LACP matching is done by only using ether type since using
6029 * a multicast dst mac causes kernel to give low priority to this flow.
6031 static const struct rte_flow_item_eth lacp_spec = {
6032 .type = RTE_BE16(0x8809),
6034 static const struct rte_flow_item_eth lacp_mask = {
6037 const struct rte_flow_attr attr = {
6040 struct rte_flow_item items[] = {
6042 .type = RTE_FLOW_ITEM_TYPE_ETH,
6047 .type = RTE_FLOW_ITEM_TYPE_END,
6050 struct rte_flow_action actions[] = {
6052 .type = (enum rte_flow_action_type)
6053 MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS,
6056 .type = RTE_FLOW_ACTION_TYPE_END,
6059 struct rte_flow_error error;
6060 uint32_t flow_idx = flow_list_create(dev, &priv->ctrl_flows,
6061 &attr, items, actions, false, &error);
6071 * @see rte_flow_destroy()
6075 mlx5_flow_destroy(struct rte_eth_dev *dev,
6076 struct rte_flow *flow,
6077 struct rte_flow_error *error __rte_unused)
6079 struct mlx5_priv *priv = dev->data->dev_private;
6081 flow_list_destroy(dev, &priv->flows, (uintptr_t)(void *)flow);
6086 * Destroy all flows.
6088 * @see rte_flow_flush()
6092 mlx5_flow_flush(struct rte_eth_dev *dev,
6093 struct rte_flow_error *error __rte_unused)
6095 struct mlx5_priv *priv = dev->data->dev_private;
6097 mlx5_flow_list_flush(dev, &priv->flows, false);
6104 * @see rte_flow_isolate()
6108 mlx5_flow_isolate(struct rte_eth_dev *dev,
6110 struct rte_flow_error *error)
6112 struct mlx5_priv *priv = dev->data->dev_private;
6114 if (dev->data->dev_started) {
6115 rte_flow_error_set(error, EBUSY,
6116 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6118 "port must be stopped first");
6121 priv->isolated = !!enable;
6123 dev->dev_ops = &mlx5_os_dev_ops_isolate;
6125 dev->dev_ops = &mlx5_os_dev_ops;
6127 dev->rx_descriptor_status = mlx5_rx_descriptor_status;
6128 dev->tx_descriptor_status = mlx5_tx_descriptor_status;
6136 * @see rte_flow_query()
6140 flow_drv_query(struct rte_eth_dev *dev,
6142 const struct rte_flow_action *actions,
6144 struct rte_flow_error *error)
6146 struct mlx5_priv *priv = dev->data->dev_private;
6147 const struct mlx5_flow_driver_ops *fops;
6148 struct rte_flow *flow = mlx5_ipool_get(priv->sh->ipool
6149 [MLX5_IPOOL_RTE_FLOW],
6151 enum mlx5_flow_drv_type ftype;
6154 return rte_flow_error_set(error, ENOENT,
6155 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6157 "invalid flow handle");
6159 ftype = flow->drv_type;
6160 MLX5_ASSERT(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
6161 fops = flow_get_drv_ops(ftype);
6163 return fops->query(dev, flow, actions, data, error);
6169 * @see rte_flow_query()
6173 mlx5_flow_query(struct rte_eth_dev *dev,
6174 struct rte_flow *flow,
6175 const struct rte_flow_action *actions,
6177 struct rte_flow_error *error)
6181 ret = flow_drv_query(dev, (uintptr_t)(void *)flow, actions, data,
6189 * Manage filter operations.
6192 * Pointer to Ethernet device structure.
6193 * @param filter_type
6196 * Operation to perform.
6198 * Pointer to operation-specific structure.
6201 * 0 on success, a negative errno value otherwise and rte_errno is set.
6204 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
6205 enum rte_filter_type filter_type,
6206 enum rte_filter_op filter_op,
6209 switch (filter_type) {
6210 case RTE_ETH_FILTER_GENERIC:
6211 if (filter_op != RTE_ETH_FILTER_GET) {
6215 *(const void **)arg = &mlx5_flow_ops;
6218 DRV_LOG(ERR, "port %u filter type (%d) not supported",
6219 dev->data->port_id, filter_type);
6220 rte_errno = ENOTSUP;
6227 * Create the needed meter and suffix tables.
6230 * Pointer to Ethernet device.
6232 * Pointer to the flow meter.
6235 * Pointer to table set on success, NULL otherwise.
6237 struct mlx5_meter_domains_infos *
6238 mlx5_flow_create_mtr_tbls(struct rte_eth_dev *dev,
6239 const struct mlx5_flow_meter *fm)
6241 const struct mlx5_flow_driver_ops *fops;
6243 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6244 return fops->create_mtr_tbls(dev, fm);
6248 * Destroy the meter table set.
6251 * Pointer to Ethernet device.
6253 * Pointer to the meter table set.
6259 mlx5_flow_destroy_mtr_tbls(struct rte_eth_dev *dev,
6260 struct mlx5_meter_domains_infos *tbls)
6262 const struct mlx5_flow_driver_ops *fops;
6264 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6265 return fops->destroy_mtr_tbls(dev, tbls);
6269 * Create policer rules.
6272 * Pointer to Ethernet device.
6274 * Pointer to flow meter structure.
6276 * Pointer to flow attributes.
6279 * 0 on success, -1 otherwise.
6282 mlx5_flow_create_policer_rules(struct rte_eth_dev *dev,
6283 struct mlx5_flow_meter *fm,
6284 const struct rte_flow_attr *attr)
6286 const struct mlx5_flow_driver_ops *fops;
6288 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6289 return fops->create_policer_rules(dev, fm, attr);
6293 * Destroy policer rules.
6296 * Pointer to flow meter structure.
6298 * Pointer to flow attributes.
6301 * 0 on success, -1 otherwise.
6304 mlx5_flow_destroy_policer_rules(struct rte_eth_dev *dev,
6305 struct mlx5_flow_meter *fm,
6306 const struct rte_flow_attr *attr)
6308 const struct mlx5_flow_driver_ops *fops;
6310 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6311 return fops->destroy_policer_rules(dev, fm, attr);
6315 * Allocate a counter.
6318 * Pointer to Ethernet device structure.
6321 * Index to allocated counter on success, 0 otherwise.
6324 mlx5_counter_alloc(struct rte_eth_dev *dev)
6326 const struct mlx5_flow_driver_ops *fops;
6327 struct rte_flow_attr attr = { .transfer = 0 };
6329 if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
6330 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6331 return fops->counter_alloc(dev);
6334 "port %u counter allocate is not supported.",
6335 dev->data->port_id);
6343 * Pointer to Ethernet device structure.
6345 * Index to counter to be free.
6348 mlx5_counter_free(struct rte_eth_dev *dev, uint32_t cnt)
6350 const struct mlx5_flow_driver_ops *fops;
6351 struct rte_flow_attr attr = { .transfer = 0 };
6353 if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
6354 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6355 fops->counter_free(dev, cnt);
6359 "port %u counter free is not supported.",
6360 dev->data->port_id);
6364 * Query counter statistics.
6367 * Pointer to Ethernet device structure.
6369 * Index to counter to query.
6371 * Set to clear counter statistics.
6373 * The counter hits packets number to save.
6375 * The counter hits bytes number to save.
6378 * 0 on success, a negative errno value otherwise.
6381 mlx5_counter_query(struct rte_eth_dev *dev, uint32_t cnt,
6382 bool clear, uint64_t *pkts, uint64_t *bytes)
6384 const struct mlx5_flow_driver_ops *fops;
6385 struct rte_flow_attr attr = { .transfer = 0 };
6387 if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
6388 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6389 return fops->counter_query(dev, cnt, clear, pkts, bytes);
6392 "port %u counter query is not supported.",
6393 dev->data->port_id);
6398 * Allocate a new memory for the counter values wrapped by all the needed
6402 * Pointer to mlx5_dev_ctx_shared object.
6405 * 0 on success, a negative errno value otherwise.
6408 mlx5_flow_create_counter_stat_mem_mng(struct mlx5_dev_ctx_shared *sh)
6410 struct mlx5_devx_mkey_attr mkey_attr;
6411 struct mlx5_counter_stats_mem_mng *mem_mng;
6412 volatile struct flow_counter_stats *raw_data;
6413 int raws_n = MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES;
6414 int size = (sizeof(struct flow_counter_stats) *
6415 MLX5_COUNTERS_PER_POOL +
6416 sizeof(struct mlx5_counter_stats_raw)) * raws_n +
6417 sizeof(struct mlx5_counter_stats_mem_mng);
6418 size_t pgsize = rte_mem_page_size();
6422 if (pgsize == (size_t)-1) {
6423 DRV_LOG(ERR, "Failed to get mem page size");
6427 mem = mlx5_malloc(MLX5_MEM_ZERO, size, pgsize, SOCKET_ID_ANY);
6432 mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
6433 size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
6434 mem_mng->umem = mlx5_glue->devx_umem_reg(sh->ctx, mem, size,
6435 IBV_ACCESS_LOCAL_WRITE);
6436 if (!mem_mng->umem) {
6441 mkey_attr.addr = (uintptr_t)mem;
6442 mkey_attr.size = size;
6443 mkey_attr.umem_id = mlx5_os_get_umem_id(mem_mng->umem);
6444 mkey_attr.pd = sh->pdn;
6445 mkey_attr.log_entity_size = 0;
6446 mkey_attr.pg_access = 0;
6447 mkey_attr.klm_array = NULL;
6448 mkey_attr.klm_num = 0;
6449 mkey_attr.relaxed_ordering_write = sh->cmng.relaxed_ordering_write;
6450 mkey_attr.relaxed_ordering_read = sh->cmng.relaxed_ordering_read;
6451 mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
6453 mlx5_glue->devx_umem_dereg(mem_mng->umem);
6458 mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
6459 raw_data = (volatile struct flow_counter_stats *)mem;
6460 for (i = 0; i < raws_n; ++i) {
6461 mem_mng->raws[i].mem_mng = mem_mng;
6462 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
6464 for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
6465 LIST_INSERT_HEAD(&sh->cmng.free_stat_raws,
6466 mem_mng->raws + MLX5_CNT_CONTAINER_RESIZE + i,
6468 LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
6469 sh->cmng.mem_mng = mem_mng;
6474 * Set the statistic memory to the new counter pool.
6477 * Pointer to mlx5_dev_ctx_shared object.
6479 * Pointer to the pool to set the statistic memory.
6482 * 0 on success, a negative errno value otherwise.
6485 mlx5_flow_set_counter_stat_mem(struct mlx5_dev_ctx_shared *sh,
6486 struct mlx5_flow_counter_pool *pool)
6488 struct mlx5_flow_counter_mng *cmng = &sh->cmng;
6489 /* Resize statistic memory once used out. */
6490 if (!(pool->index % MLX5_CNT_CONTAINER_RESIZE) &&
6491 mlx5_flow_create_counter_stat_mem_mng(sh)) {
6492 DRV_LOG(ERR, "Cannot resize counter stat mem.");
6495 rte_spinlock_lock(&pool->sl);
6496 pool->raw = cmng->mem_mng->raws + pool->index %
6497 MLX5_CNT_CONTAINER_RESIZE;
6498 rte_spinlock_unlock(&pool->sl);
6499 pool->raw_hw = NULL;
6503 #define MLX5_POOL_QUERY_FREQ_US 1000000
6506 * Set the periodic procedure for triggering asynchronous batch queries for all
6507 * the counter pools.
6510 * Pointer to mlx5_dev_ctx_shared object.
6513 mlx5_set_query_alarm(struct mlx5_dev_ctx_shared *sh)
6515 uint32_t pools_n, us;
6517 pools_n = __atomic_load_n(&sh->cmng.n_valid, __ATOMIC_RELAXED);
6518 us = MLX5_POOL_QUERY_FREQ_US / pools_n;
6519 DRV_LOG(DEBUG, "Set alarm for %u pools each %u us", pools_n, us);
6520 if (rte_eal_alarm_set(us, mlx5_flow_query_alarm, sh)) {
6521 sh->cmng.query_thread_on = 0;
6522 DRV_LOG(ERR, "Cannot reinitialize query alarm");
6524 sh->cmng.query_thread_on = 1;
6529 * The periodic procedure for triggering asynchronous batch queries for all the
6530 * counter pools. This function is probably called by the host thread.
6533 * The parameter for the alarm process.
6536 mlx5_flow_query_alarm(void *arg)
6538 struct mlx5_dev_ctx_shared *sh = arg;
6540 uint16_t pool_index = sh->cmng.pool_index;
6541 struct mlx5_flow_counter_mng *cmng = &sh->cmng;
6542 struct mlx5_flow_counter_pool *pool;
6545 if (sh->cmng.pending_queries >= MLX5_MAX_PENDING_QUERIES)
6547 rte_spinlock_lock(&cmng->pool_update_sl);
6548 pool = cmng->pools[pool_index];
6549 n_valid = cmng->n_valid;
6550 rte_spinlock_unlock(&cmng->pool_update_sl);
6551 /* Set the statistic memory to the new created pool. */
6552 if ((!pool->raw && mlx5_flow_set_counter_stat_mem(sh, pool)))
6555 /* There is a pool query in progress. */
6558 LIST_FIRST(&sh->cmng.free_stat_raws);
6560 /* No free counter statistics raw memory. */
6563 * Identify the counters released between query trigger and query
6564 * handle more efficiently. The counter released in this gap period
6565 * should wait for a new round of query as the new arrived packets
6566 * will not be taken into account.
6569 ret = mlx5_devx_cmd_flow_counter_query(pool->min_dcs, 0,
6570 MLX5_COUNTERS_PER_POOL,
6572 pool->raw_hw->mem_mng->dm->id,
6576 (uint64_t)(uintptr_t)pool);
6578 DRV_LOG(ERR, "Failed to trigger asynchronous query for dcs ID"
6579 " %d", pool->min_dcs->id);
6580 pool->raw_hw = NULL;
6583 LIST_REMOVE(pool->raw_hw, next);
6584 sh->cmng.pending_queries++;
6586 if (pool_index >= n_valid)
6589 sh->cmng.pool_index = pool_index;
6590 mlx5_set_query_alarm(sh);
6594 * Check and callback event for new aged flow in the counter pool
6597 * Pointer to mlx5_dev_ctx_shared object.
6599 * Pointer to Current counter pool.
6602 mlx5_flow_aging_check(struct mlx5_dev_ctx_shared *sh,
6603 struct mlx5_flow_counter_pool *pool)
6605 struct mlx5_priv *priv;
6606 struct mlx5_flow_counter *cnt;
6607 struct mlx5_age_info *age_info;
6608 struct mlx5_age_param *age_param;
6609 struct mlx5_counter_stats_raw *cur = pool->raw_hw;
6610 struct mlx5_counter_stats_raw *prev = pool->raw;
6611 const uint64_t curr_time = MLX5_CURR_TIME_SEC;
6612 const uint32_t time_delta = curr_time - pool->time_of_last_age_check;
6613 uint16_t expected = AGE_CANDIDATE;
6616 pool->time_of_last_age_check = curr_time;
6617 for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
6618 cnt = MLX5_POOL_GET_CNT(pool, i);
6619 age_param = MLX5_CNT_TO_AGE(cnt);
6620 if (__atomic_load_n(&age_param->state,
6621 __ATOMIC_RELAXED) != AGE_CANDIDATE)
6623 if (cur->data[i].hits != prev->data[i].hits) {
6624 __atomic_store_n(&age_param->sec_since_last_hit, 0,
6628 if (__atomic_add_fetch(&age_param->sec_since_last_hit,
6630 __ATOMIC_RELAXED) <= age_param->timeout)
6633 * Hold the lock first, or if between the
6634 * state AGE_TMOUT and tailq operation the
6635 * release happened, the release procedure
6636 * may delete a non-existent tailq node.
6638 priv = rte_eth_devices[age_param->port_id].data->dev_private;
6639 age_info = GET_PORT_AGE_INFO(priv);
6640 rte_spinlock_lock(&age_info->aged_sl);
6641 if (__atomic_compare_exchange_n(&age_param->state, &expected,
6644 __ATOMIC_RELAXED)) {
6645 TAILQ_INSERT_TAIL(&age_info->aged_counters, cnt, next);
6646 MLX5_AGE_SET(age_info, MLX5_AGE_EVENT_NEW);
6648 rte_spinlock_unlock(&age_info->aged_sl);
6650 mlx5_age_event_prepare(sh);
6654 * Handler for the HW respond about ready values from an asynchronous batch
6655 * query. This function is probably called by the host thread.
6658 * The pointer to the shared device context.
6659 * @param[in] async_id
6660 * The Devx async ID.
6662 * The status of the completion.
6665 mlx5_flow_async_pool_query_handle(struct mlx5_dev_ctx_shared *sh,
6666 uint64_t async_id, int status)
6668 struct mlx5_flow_counter_pool *pool =
6669 (struct mlx5_flow_counter_pool *)(uintptr_t)async_id;
6670 struct mlx5_counter_stats_raw *raw_to_free;
6671 uint8_t query_gen = pool->query_gen ^ 1;
6672 struct mlx5_flow_counter_mng *cmng = &sh->cmng;
6673 enum mlx5_counter_type cnt_type =
6674 pool->is_aged ? MLX5_COUNTER_TYPE_AGE :
6675 MLX5_COUNTER_TYPE_ORIGIN;
6677 if (unlikely(status)) {
6678 raw_to_free = pool->raw_hw;
6680 raw_to_free = pool->raw;
6682 mlx5_flow_aging_check(sh, pool);
6683 rte_spinlock_lock(&pool->sl);
6684 pool->raw = pool->raw_hw;
6685 rte_spinlock_unlock(&pool->sl);
6686 /* Be sure the new raw counters data is updated in memory. */
6688 if (!TAILQ_EMPTY(&pool->counters[query_gen])) {
6689 rte_spinlock_lock(&cmng->csl[cnt_type]);
6690 TAILQ_CONCAT(&cmng->counters[cnt_type],
6691 &pool->counters[query_gen], next);
6692 rte_spinlock_unlock(&cmng->csl[cnt_type]);
6695 LIST_INSERT_HEAD(&sh->cmng.free_stat_raws, raw_to_free, next);
6696 pool->raw_hw = NULL;
6697 sh->cmng.pending_queries--;
6701 flow_group_to_table(uint32_t port_id, uint32_t group, uint32_t *table,
6702 struct flow_grp_info grp_info, struct rte_flow_error *error)
6704 if (grp_info.transfer && grp_info.external && grp_info.fdb_def_rule) {
6705 if (group == UINT32_MAX)
6706 return rte_flow_error_set
6708 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
6710 "group index not supported");
6715 DRV_LOG(DEBUG, "port %u group=%#x table=%#x", port_id, group, *table);
6720 * Translate the rte_flow group index to HW table value.
6722 * If tunnel offload is disabled, all group ids converted to flow table
6723 * id using the standard method.
6724 * If tunnel offload is enabled, group id can be converted using the
6725 * standard or tunnel conversion method. Group conversion method
6726 * selection depends on flags in `grp_info` parameter:
6727 * - Internal (grp_info.external == 0) groups conversion uses the
6729 * - Group ids in JUMP action converted with the tunnel conversion.
6730 * - Group id in rule attribute conversion depends on a rule type and
6732 * ** non zero group attributes converted with the tunnel method
6733 * ** zero group attribute in non-tunnel rule is converted using the
6734 * standard method - there's only one root table
6735 * ** zero group attribute in steer tunnel rule is converted with the
6736 * standard method - single root table
6737 * ** zero group attribute in match tunnel rule is a special OvS
6738 * case: that value is used for portability reasons. That group
6739 * id is converted with the tunnel conversion method.
6744 * PMD tunnel offload object
6746 * rte_flow group index value.
6749 * @param[in] grp_info
6750 * flags used for conversion
6752 * Pointer to error structure.
6755 * 0 on success, a negative errno value otherwise and rte_errno is set.
6758 mlx5_flow_group_to_table(struct rte_eth_dev *dev,
6759 const struct mlx5_flow_tunnel *tunnel,
6760 uint32_t group, uint32_t *table,
6761 struct flow_grp_info grp_info,
6762 struct rte_flow_error *error)
6765 bool standard_translation;
6767 if (!grp_info.skip_scale && grp_info.external &&
6768 group < MLX5_MAX_TABLES_EXTERNAL)
6769 group *= MLX5_FLOW_TABLE_FACTOR;
6770 if (is_tunnel_offload_active(dev)) {
6771 standard_translation = !grp_info.external ||
6772 grp_info.std_tbl_fix;
6774 standard_translation = true;
6777 "port %u group=%#x transfer=%d external=%d fdb_def_rule=%d translate=%s",
6778 dev->data->port_id, group, grp_info.transfer,
6779 grp_info.external, grp_info.fdb_def_rule,
6780 standard_translation ? "STANDARD" : "TUNNEL");
6781 if (standard_translation)
6782 ret = flow_group_to_table(dev->data->port_id, group, table,
6785 ret = tunnel_flow_group_to_flow_table(dev, tunnel, group,
6792 * Discover availability of metadata reg_c's.
6794 * Iteratively use test flows to check availability.
6797 * Pointer to the Ethernet device structure.
6800 * 0 on success, a negative errno value otherwise and rte_errno is set.
6803 mlx5_flow_discover_mreg_c(struct rte_eth_dev *dev)
6805 struct mlx5_priv *priv = dev->data->dev_private;
6806 struct mlx5_dev_config *config = &priv->config;
6807 enum modify_reg idx;
6810 /* reg_c[0] and reg_c[1] are reserved. */
6811 config->flow_mreg_c[n++] = REG_C_0;
6812 config->flow_mreg_c[n++] = REG_C_1;
6813 /* Discover availability of other reg_c's. */
6814 for (idx = REG_C_2; idx <= REG_C_7; ++idx) {
6815 struct rte_flow_attr attr = {
6816 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
6817 .priority = MLX5_FLOW_PRIO_RSVD,
6820 struct rte_flow_item items[] = {
6822 .type = RTE_FLOW_ITEM_TYPE_END,
6825 struct rte_flow_action actions[] = {
6827 .type = (enum rte_flow_action_type)
6828 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
6829 .conf = &(struct mlx5_flow_action_copy_mreg){
6835 .type = RTE_FLOW_ACTION_TYPE_JUMP,
6836 .conf = &(struct rte_flow_action_jump){
6837 .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
6841 .type = RTE_FLOW_ACTION_TYPE_END,
6845 struct rte_flow *flow;
6846 struct rte_flow_error error;
6848 if (!config->dv_flow_en)
6850 /* Create internal flow, validation skips copy action. */
6851 flow_idx = flow_list_create(dev, NULL, &attr, items,
6852 actions, false, &error);
6853 flow = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_RTE_FLOW],
6857 config->flow_mreg_c[n++] = idx;
6858 flow_list_destroy(dev, NULL, flow_idx);
6860 for (; n < MLX5_MREG_C_NUM; ++n)
6861 config->flow_mreg_c[n] = REG_NON;
6866 * Dump flow raw hw data to file
6869 * The pointer to Ethernet device.
6871 * A pointer to a file for output.
6873 * Perform verbose error reporting if not NULL. PMDs initialize this
6874 * structure in case of error only.
6876 * 0 on success, a nagative value otherwise.
6879 mlx5_flow_dev_dump(struct rte_eth_dev *dev,
6881 struct rte_flow_error *error __rte_unused)
6883 struct mlx5_priv *priv = dev->data->dev_private;
6884 struct mlx5_dev_ctx_shared *sh = priv->sh;
6886 if (!priv->config.dv_flow_en) {
6887 if (fputs("device dv flow disabled\n", file) <= 0)
6891 return mlx5_devx_cmd_flow_dump(sh->fdb_domain, sh->rx_domain,
6892 sh->tx_domain, file);
6896 * Get aged-out flows.
6899 * Pointer to the Ethernet device structure.
6900 * @param[in] context
6901 * The address of an array of pointers to the aged-out flows contexts.
6902 * @param[in] nb_countexts
6903 * The length of context array pointers.
6905 * Perform verbose error reporting if not NULL. Initialized in case of
6909 * how many contexts get in success, otherwise negative errno value.
6910 * if nb_contexts is 0, return the amount of all aged contexts.
6911 * if nb_contexts is not 0 , return the amount of aged flows reported
6912 * in the context array.
6915 mlx5_flow_get_aged_flows(struct rte_eth_dev *dev, void **contexts,
6916 uint32_t nb_contexts, struct rte_flow_error *error)
6918 const struct mlx5_flow_driver_ops *fops;
6919 struct rte_flow_attr attr = { .transfer = 0 };
6921 if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
6922 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
6923 return fops->get_aged_flows(dev, contexts, nb_contexts,
6927 "port %u get aged flows is not supported.",
6928 dev->data->port_id);
6932 /* Wrapper for driver action_validate op callback */
6934 flow_drv_action_validate(struct rte_eth_dev *dev,
6935 const struct rte_flow_shared_action_conf *conf,
6936 const struct rte_flow_action *action,
6937 const struct mlx5_flow_driver_ops *fops,
6938 struct rte_flow_error *error)
6940 static const char err_msg[] = "shared action validation unsupported";
6942 if (!fops->action_validate) {
6943 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
6944 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
6948 return fops->action_validate(dev, conf, action, error);
6952 * Destroys the shared action by handle.
6955 * Pointer to Ethernet device structure.
6957 * Handle for the shared action to be destroyed.
6959 * Perform verbose error reporting if not NULL. PMDs initialize this
6960 * structure in case of error only.
6963 * 0 on success, a negative errno value otherwise and rte_errno is set.
6965 * @note: wrapper for driver action_create op callback.
6968 mlx5_shared_action_destroy(struct rte_eth_dev *dev,
6969 struct rte_flow_shared_action *action,
6970 struct rte_flow_error *error)
6972 static const char err_msg[] = "shared action destruction unsupported";
6973 struct rte_flow_attr attr = { .transfer = 0 };
6974 const struct mlx5_flow_driver_ops *fops =
6975 flow_get_drv_ops(flow_get_drv_type(dev, &attr));
6977 if (!fops->action_destroy) {
6978 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
6979 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
6983 return fops->action_destroy(dev, action, error);
6986 /* Wrapper for driver action_destroy op callback */
6988 flow_drv_action_update(struct rte_eth_dev *dev,
6989 struct rte_flow_shared_action *action,
6990 const void *action_conf,
6991 const struct mlx5_flow_driver_ops *fops,
6992 struct rte_flow_error *error)
6994 static const char err_msg[] = "shared action update unsupported";
6996 if (!fops->action_update) {
6997 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
6998 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
7002 return fops->action_update(dev, action, action_conf, error);
7005 /* Wrapper for driver action_destroy op callback */
7007 flow_drv_action_query(struct rte_eth_dev *dev,
7008 const struct rte_flow_shared_action *action,
7010 const struct mlx5_flow_driver_ops *fops,
7011 struct rte_flow_error *error)
7013 static const char err_msg[] = "shared action query unsupported";
7015 if (!fops->action_query) {
7016 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
7017 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
7021 return fops->action_query(dev, action, data, error);
7025 * Create shared action for reuse in multiple flow rules.
7028 * Pointer to Ethernet device structure.
7030 * Action configuration for shared action creation.
7032 * Perform verbose error reporting if not NULL. PMDs initialize this
7033 * structure in case of error only.
7035 * A valid handle in case of success, NULL otherwise and rte_errno is set.
7037 static struct rte_flow_shared_action *
7038 mlx5_shared_action_create(struct rte_eth_dev *dev,
7039 const struct rte_flow_shared_action_conf *conf,
7040 const struct rte_flow_action *action,
7041 struct rte_flow_error *error)
7043 static const char err_msg[] = "shared action creation unsupported";
7044 struct rte_flow_attr attr = { .transfer = 0 };
7045 const struct mlx5_flow_driver_ops *fops =
7046 flow_get_drv_ops(flow_get_drv_type(dev, &attr));
7048 if (flow_drv_action_validate(dev, conf, action, fops, error))
7050 if (!fops->action_create) {
7051 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
7052 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
7056 return fops->action_create(dev, conf, action, error);
7060 * Updates inplace the shared action configuration pointed by *action* handle
7061 * with the configuration provided as *action* argument.
7062 * The update of the shared action configuration effects all flow rules reusing
7063 * the action via handle.
7066 * Pointer to Ethernet device structure.
7067 * @param[in] shared_action
7068 * Handle for the shared action to be updated.
7070 * Action specification used to modify the action pointed by handle.
7071 * *action* should be of same type with the action pointed by the *action*
7072 * handle argument, otherwise considered as invalid.
7074 * Perform verbose error reporting if not NULL. PMDs initialize this
7075 * structure in case of error only.
7078 * 0 on success, a negative errno value otherwise and rte_errno is set.
7081 mlx5_shared_action_update(struct rte_eth_dev *dev,
7082 struct rte_flow_shared_action *shared_action,
7083 const struct rte_flow_action *action,
7084 struct rte_flow_error *error)
7086 struct rte_flow_attr attr = { .transfer = 0 };
7087 const struct mlx5_flow_driver_ops *fops =
7088 flow_get_drv_ops(flow_get_drv_type(dev, &attr));
7091 ret = flow_drv_action_validate(dev, NULL, action, fops, error);
7094 return flow_drv_action_update(dev, shared_action, action->conf, fops,
7099 * Query the shared action by handle.
7101 * This function allows retrieving action-specific data such as counters.
7102 * Data is gathered by special action which may be present/referenced in
7103 * more than one flow rule definition.
7105 * \see RTE_FLOW_ACTION_TYPE_COUNT
7108 * Pointer to Ethernet device structure.
7110 * Handle for the shared action to query.
7111 * @param[in, out] data
7112 * Pointer to storage for the associated query data type.
7114 * Perform verbose error reporting if not NULL. PMDs initialize this
7115 * structure in case of error only.
7118 * 0 on success, a negative errno value otherwise and rte_errno is set.
7121 mlx5_shared_action_query(struct rte_eth_dev *dev,
7122 const struct rte_flow_shared_action *action,
7124 struct rte_flow_error *error)
7126 struct rte_flow_attr attr = { .transfer = 0 };
7127 const struct mlx5_flow_driver_ops *fops =
7128 flow_get_drv_ops(flow_get_drv_type(dev, &attr));
7130 return flow_drv_action_query(dev, action, data, fops, error);
7134 * Destroy all shared actions.
7137 * Pointer to Ethernet device.
7140 * 0 on success, a negative errno value otherwise and rte_errno is set.
7143 mlx5_shared_action_flush(struct rte_eth_dev *dev)
7145 struct rte_flow_error error;
7146 struct mlx5_priv *priv = dev->data->dev_private;
7147 struct mlx5_shared_action_rss *action;
7151 ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
7152 priv->rss_shared_actions, idx, action, next) {
7153 ret |= mlx5_shared_action_destroy(dev,
7154 (struct rte_flow_shared_action *)(uintptr_t)idx, &error);
7159 #ifndef HAVE_MLX5DV_DR
7160 #define MLX5_DOMAIN_SYNC_FLOW ((1 << 0) | (1 << 1))
7162 #define MLX5_DOMAIN_SYNC_FLOW \
7163 (MLX5DV_DR_DOMAIN_SYNC_FLAGS_SW | MLX5DV_DR_DOMAIN_SYNC_FLAGS_HW)
7166 int rte_pmd_mlx5_sync_flow(uint16_t port_id, uint32_t domains)
7168 struct rte_eth_dev *dev = &rte_eth_devices[port_id];
7169 const struct mlx5_flow_driver_ops *fops;
7171 struct rte_flow_attr attr = { .transfer = 0 };
7173 fops = flow_get_drv_ops(flow_get_drv_type(dev, &attr));
7174 ret = fops->sync_domain(dev, domains, MLX5_DOMAIN_SYNC_FLOW);
7181 * tunnel offload functionalilty is defined for DV environment only
7183 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
7185 union tunnel_offload_mark {
7188 uint32_t app_reserve:8;
7189 uint32_t table_id:15;
7190 uint32_t transfer:1;
7191 uint32_t _unused_:8;
7196 flow_tunnel_add_default_miss(struct rte_eth_dev *dev,
7197 struct rte_flow *flow,
7198 const struct rte_flow_attr *attr,
7199 const struct rte_flow_action *app_actions,
7201 struct tunnel_default_miss_ctx *ctx,
7202 struct rte_flow_error *error)
7204 struct mlx5_priv *priv = dev->data->dev_private;
7205 struct mlx5_flow *dev_flow;
7206 struct rte_flow_attr miss_attr = *attr;
7207 const struct mlx5_flow_tunnel *tunnel = app_actions[0].conf;
7208 const struct rte_flow_item miss_items[2] = {
7210 .type = RTE_FLOW_ITEM_TYPE_ETH,
7216 .type = RTE_FLOW_ITEM_TYPE_END,
7222 union tunnel_offload_mark mark_id;
7223 struct rte_flow_action_mark miss_mark;
7224 struct rte_flow_action miss_actions[3] = {
7225 [0] = { .type = RTE_FLOW_ACTION_TYPE_MARK, .conf = &miss_mark },
7226 [2] = { .type = RTE_FLOW_ACTION_TYPE_END, .conf = NULL }
7228 const struct rte_flow_action_jump *jump_data;
7229 uint32_t i, flow_table = 0; /* prevent compilation warning */
7230 struct flow_grp_info grp_info = {
7232 .transfer = attr->transfer,
7233 .fdb_def_rule = !!priv->fdb_def_rule,
7238 if (!attr->transfer) {
7241 miss_actions[1].type = RTE_FLOW_ACTION_TYPE_RSS;
7242 q_size = priv->reta_idx_n * sizeof(ctx->queue[0]);
7243 ctx->queue = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, q_size,
7246 return rte_flow_error_set
7248 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
7249 NULL, "invalid default miss RSS");
7250 ctx->action_rss.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
7251 ctx->action_rss.level = 0,
7252 ctx->action_rss.types = priv->rss_conf.rss_hf,
7253 ctx->action_rss.key_len = priv->rss_conf.rss_key_len,
7254 ctx->action_rss.queue_num = priv->reta_idx_n,
7255 ctx->action_rss.key = priv->rss_conf.rss_key,
7256 ctx->action_rss.queue = ctx->queue;
7257 if (!priv->reta_idx_n || !priv->rxqs_n)
7258 return rte_flow_error_set
7260 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
7261 NULL, "invalid port configuration");
7262 if (!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
7263 ctx->action_rss.types = 0;
7264 for (i = 0; i != priv->reta_idx_n; ++i)
7265 ctx->queue[i] = (*priv->reta_idx)[i];
7267 miss_actions[1].type = RTE_FLOW_ACTION_TYPE_JUMP;
7268 ctx->miss_jump.group = MLX5_TNL_MISS_FDB_JUMP_GRP;
7270 miss_actions[1].conf = (typeof(miss_actions[1].conf))ctx->raw;
7271 for (; app_actions->type != RTE_FLOW_ACTION_TYPE_JUMP; app_actions++);
7272 jump_data = app_actions->conf;
7273 miss_attr.priority = MLX5_TNL_MISS_RULE_PRIORITY;
7274 miss_attr.group = jump_data->group;
7275 ret = mlx5_flow_group_to_table(dev, tunnel, jump_data->group,
7276 &flow_table, grp_info, error);
7278 return rte_flow_error_set(error, EINVAL,
7279 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
7280 NULL, "invalid tunnel id");
7281 mark_id.app_reserve = 0;
7282 mark_id.table_id = tunnel_flow_tbl_to_id(flow_table);
7283 mark_id.transfer = !!attr->transfer;
7284 mark_id._unused_ = 0;
7285 miss_mark.id = mark_id.val;
7286 dev_flow = flow_drv_prepare(dev, flow, &miss_attr,
7287 miss_items, miss_actions, flow_idx, error);
7290 dev_flow->flow = flow;
7291 dev_flow->external = true;
7292 dev_flow->tunnel = tunnel;
7293 /* Subflow object was created, we must include one in the list. */
7294 SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
7295 dev_flow->handle, next);
7297 "port %u tunnel type=%d id=%u miss rule priority=%u group=%u",
7298 dev->data->port_id, tunnel->app_tunnel.type,
7299 tunnel->tunnel_id, miss_attr.priority, miss_attr.group);
7300 ret = flow_drv_translate(dev, dev_flow, &miss_attr, miss_items,
7301 miss_actions, error);
7303 ret = flow_mreg_update_copy_table(dev, flow, miss_actions,
7309 static const struct mlx5_flow_tbl_data_entry *
7310 tunnel_mark_decode(struct rte_eth_dev *dev, uint32_t mark)
7312 struct mlx5_priv *priv = dev->data->dev_private;
7313 struct mlx5_dev_ctx_shared *sh = priv->sh;
7314 struct mlx5_hlist_entry *he;
7315 union tunnel_offload_mark mbits = { .val = mark };
7316 union mlx5_flow_tbl_key table_key = {
7318 .table_id = tunnel_id_to_flow_tbl(mbits.table_id),
7320 .domain = !!mbits.transfer,
7324 he = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64, NULL);
7326 container_of(he, struct mlx5_flow_tbl_data_entry, entry) : NULL;
7330 mlx5_flow_tunnel_grp2tbl_remove_cb(struct mlx5_hlist *list,
7331 struct mlx5_hlist_entry *entry)
7333 struct mlx5_dev_ctx_shared *sh = list->ctx;
7334 struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
7336 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
7337 tunnel_flow_tbl_to_id(tte->flow_table));
7341 static struct mlx5_hlist_entry *
7342 mlx5_flow_tunnel_grp2tbl_create_cb(struct mlx5_hlist *list,
7343 uint64_t key __rte_unused,
7344 void *ctx __rte_unused)
7346 struct mlx5_dev_ctx_shared *sh = list->ctx;
7347 struct tunnel_tbl_entry *tte;
7349 tte = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO,
7354 mlx5_ipool_malloc(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
7356 if (tte->flow_table >= MLX5_MAX_TABLES) {
7357 DRV_LOG(ERR, "Tunnel TBL ID %d exceed max limit.",
7359 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
7362 } else if (!tte->flow_table) {
7365 tte->flow_table = tunnel_id_to_flow_tbl(tte->flow_table);
7374 tunnel_flow_group_to_flow_table(struct rte_eth_dev *dev,
7375 const struct mlx5_flow_tunnel *tunnel,
7376 uint32_t group, uint32_t *table,
7377 struct rte_flow_error *error)
7379 struct mlx5_hlist_entry *he;
7380 struct tunnel_tbl_entry *tte;
7381 union tunnel_tbl_key key = {
7382 .tunnel_id = tunnel ? tunnel->tunnel_id : 0,
7385 struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
7386 struct mlx5_hlist *group_hash;
7388 group_hash = tunnel ? tunnel->groups : thub->groups;
7389 he = mlx5_hlist_register(group_hash, key.val, NULL);
7391 return rte_flow_error_set(error, EINVAL,
7392 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
7394 "tunnel group index not supported");
7395 tte = container_of(he, typeof(*tte), hash);
7396 *table = tte->flow_table;
7397 DRV_LOG(DEBUG, "port %u tunnel %u group=%#x table=%#x",
7398 dev->data->port_id, key.tunnel_id, group, *table);
7403 mlx5_flow_tunnel_free(struct rte_eth_dev *dev,
7404 struct mlx5_flow_tunnel *tunnel)
7406 struct mlx5_priv *priv = dev->data->dev_private;
7408 DRV_LOG(DEBUG, "port %u release pmd tunnel id=0x%x",
7409 dev->data->port_id, tunnel->tunnel_id);
7410 RTE_VERIFY(!__atomic_load_n(&tunnel->refctn, __ATOMIC_RELAXED));
7411 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_TUNNEL_ID],
7413 mlx5_hlist_destroy(tunnel->groups);
7417 static struct mlx5_flow_tunnel *
7418 mlx5_find_tunnel_id(struct rte_eth_dev *dev, uint32_t id)
7420 struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
7421 struct mlx5_flow_tunnel *tun;
7423 LIST_FOREACH(tun, &thub->tunnels, chain) {
7424 if (tun->tunnel_id == id)
7431 static struct mlx5_flow_tunnel *
7432 mlx5_flow_tunnel_allocate(struct rte_eth_dev *dev,
7433 const struct rte_flow_tunnel *app_tunnel)
7435 struct mlx5_priv *priv = dev->data->dev_private;
7436 struct mlx5_flow_tunnel *tunnel;
7439 mlx5_ipool_malloc(priv->sh->ipool[MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
7441 if (id >= MLX5_MAX_TUNNELS) {
7442 mlx5_ipool_free(priv->sh->ipool
7443 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], id);
7444 DRV_LOG(ERR, "Tunnel ID %d exceed max limit.", id);
7450 * mlx5 flow tunnel is an auxlilary data structure
7451 * It's not part of IO. No need to allocate it from
7452 * huge pages pools dedicated for IO
7454 tunnel = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, sizeof(*tunnel),
7457 mlx5_ipool_free(priv->sh->ipool
7458 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], id);
7461 tunnel->groups = mlx5_hlist_create("tunnel groups", 1024, 0, 0,
7462 mlx5_flow_tunnel_grp2tbl_create_cb,
7464 mlx5_flow_tunnel_grp2tbl_remove_cb);
7465 if (!tunnel->groups) {
7466 mlx5_ipool_free(priv->sh->ipool
7467 [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], id);
7471 tunnel->groups->ctx = priv->sh;
7472 /* initiate new PMD tunnel */
7473 memcpy(&tunnel->app_tunnel, app_tunnel, sizeof(*app_tunnel));
7474 tunnel->tunnel_id = id;
7475 tunnel->action.type = (typeof(tunnel->action.type))
7476 MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET;
7477 tunnel->action.conf = tunnel;
7478 tunnel->item.type = (typeof(tunnel->item.type))
7479 MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL;
7480 tunnel->item.spec = tunnel;
7481 tunnel->item.last = NULL;
7482 tunnel->item.mask = NULL;
7484 DRV_LOG(DEBUG, "port %u new pmd tunnel id=0x%x",
7485 dev->data->port_id, tunnel->tunnel_id);
7491 mlx5_get_flow_tunnel(struct rte_eth_dev *dev,
7492 const struct rte_flow_tunnel *app_tunnel,
7493 struct mlx5_flow_tunnel **tunnel)
7496 struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
7497 struct mlx5_flow_tunnel *tun;
7499 rte_spinlock_lock(&thub->sl);
7500 LIST_FOREACH(tun, &thub->tunnels, chain) {
7501 if (!memcmp(app_tunnel, &tun->app_tunnel,
7502 sizeof(*app_tunnel))) {
7509 tun = mlx5_flow_tunnel_allocate(dev, app_tunnel);
7511 LIST_INSERT_HEAD(&thub->tunnels, tun, chain);
7517 rte_spinlock_unlock(&thub->sl);
7519 __atomic_add_fetch(&tun->refctn, 1, __ATOMIC_RELAXED);
7524 void mlx5_release_tunnel_hub(struct mlx5_dev_ctx_shared *sh, uint16_t port_id)
7526 struct mlx5_flow_tunnel_hub *thub = sh->tunnel_hub;
7530 if (!LIST_EMPTY(&thub->tunnels))
7531 DRV_LOG(WARNING, "port %u tunnels present\n", port_id);
7532 mlx5_hlist_destroy(thub->groups);
7536 int mlx5_alloc_tunnel_hub(struct mlx5_dev_ctx_shared *sh)
7539 struct mlx5_flow_tunnel_hub *thub;
7541 thub = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, sizeof(*thub),
7545 LIST_INIT(&thub->tunnels);
7546 rte_spinlock_init(&thub->sl);
7547 thub->groups = mlx5_hlist_create("flow groups", MLX5_MAX_TABLES, 0,
7548 0, mlx5_flow_tunnel_grp2tbl_create_cb,
7550 mlx5_flow_tunnel_grp2tbl_remove_cb);
7551 if (!thub->groups) {
7555 thub->groups->ctx = sh;
7556 sh->tunnel_hub = thub;
7562 mlx5_hlist_destroy(thub->groups);
7569 mlx5_flow_tunnel_validate(struct rte_eth_dev *dev,
7570 struct rte_flow_tunnel *tunnel,
7571 const char *err_msg)
7574 if (!is_tunnel_offload_active(dev)) {
7575 err_msg = "tunnel offload was not activated";
7577 } else if (!tunnel) {
7578 err_msg = "no application tunnel";
7582 switch (tunnel->type) {
7584 err_msg = "unsupported tunnel type";
7586 case RTE_FLOW_ITEM_TYPE_VXLAN:
7595 mlx5_flow_tunnel_decap_set(struct rte_eth_dev *dev,
7596 struct rte_flow_tunnel *app_tunnel,
7597 struct rte_flow_action **actions,
7598 uint32_t *num_of_actions,
7599 struct rte_flow_error *error)
7602 struct mlx5_flow_tunnel *tunnel;
7603 const char *err_msg = NULL;
7604 bool verdict = mlx5_flow_tunnel_validate(dev, app_tunnel, err_msg);
7607 return rte_flow_error_set(error, EINVAL,
7608 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
7610 ret = mlx5_get_flow_tunnel(dev, app_tunnel, &tunnel);
7612 return rte_flow_error_set(error, ret,
7613 RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
7614 "failed to initialize pmd tunnel");
7616 *actions = &tunnel->action;
7617 *num_of_actions = 1;
7622 mlx5_flow_tunnel_match(struct rte_eth_dev *dev,
7623 struct rte_flow_tunnel *app_tunnel,
7624 struct rte_flow_item **items,
7625 uint32_t *num_of_items,
7626 struct rte_flow_error *error)
7629 struct mlx5_flow_tunnel *tunnel;
7630 const char *err_msg = NULL;
7631 bool verdict = mlx5_flow_tunnel_validate(dev, app_tunnel, err_msg);
7634 return rte_flow_error_set(error, EINVAL,
7635 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
7637 ret = mlx5_get_flow_tunnel(dev, app_tunnel, &tunnel);
7639 return rte_flow_error_set(error, ret,
7640 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
7641 "failed to initialize pmd tunnel");
7643 *items = &tunnel->item;
7648 mlx5_flow_tunnel_item_release(struct rte_eth_dev *dev,
7649 struct rte_flow_item *pmd_items,
7650 uint32_t num_items, struct rte_flow_error *err)
7652 struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
7653 struct mlx5_flow_tunnel *tun;
7655 rte_spinlock_lock(&thub->sl);
7656 LIST_FOREACH(tun, &thub->tunnels, chain) {
7657 if (&tun->item == pmd_items) {
7658 LIST_REMOVE(tun, chain);
7662 rte_spinlock_unlock(&thub->sl);
7663 if (!tun || num_items != 1)
7664 return rte_flow_error_set(err, EINVAL,
7665 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
7666 "invalid argument");
7667 if (!__atomic_sub_fetch(&tun->refctn, 1, __ATOMIC_RELAXED))
7668 mlx5_flow_tunnel_free(dev, tun);
7673 mlx5_flow_tunnel_action_release(struct rte_eth_dev *dev,
7674 struct rte_flow_action *pmd_actions,
7675 uint32_t num_actions,
7676 struct rte_flow_error *err)
7678 struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
7679 struct mlx5_flow_tunnel *tun;
7681 rte_spinlock_lock(&thub->sl);
7682 LIST_FOREACH(tun, &thub->tunnels, chain) {
7683 if (&tun->action == pmd_actions) {
7684 LIST_REMOVE(tun, chain);
7688 rte_spinlock_unlock(&thub->sl);
7689 if (!tun || num_actions != 1)
7690 return rte_flow_error_set(err, EINVAL,
7691 RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
7692 "invalid argument");
7693 if (!__atomic_sub_fetch(&tun->refctn, 1, __ATOMIC_RELAXED))
7694 mlx5_flow_tunnel_free(dev, tun);
7700 mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
7702 struct rte_flow_restore_info *info,
7703 struct rte_flow_error *err)
7705 uint64_t ol_flags = m->ol_flags;
7706 const struct mlx5_flow_tbl_data_entry *tble;
7707 const uint64_t mask = PKT_RX_FDIR | PKT_RX_FDIR_ID;
7709 if ((ol_flags & mask) != mask)
7711 tble = tunnel_mark_decode(dev, m->hash.fdir.hi);
7713 DRV_LOG(DEBUG, "port %u invalid miss tunnel mark %#x",
7714 dev->data->port_id, m->hash.fdir.hi);
7717 MLX5_ASSERT(tble->tunnel);
7718 memcpy(&info->tunnel, &tble->tunnel->app_tunnel, sizeof(info->tunnel));
7719 info->group_id = tble->group_id;
7720 info->flags = RTE_FLOW_RESTORE_INFO_TUNNEL |
7721 RTE_FLOW_RESTORE_INFO_GROUP_ID |
7722 RTE_FLOW_RESTORE_INFO_ENCAPSULATED;
7727 return rte_flow_error_set(err, EINVAL,
7728 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7729 "failed to get restore info");
7732 #else /* HAVE_IBV_FLOW_DV_SUPPORT */
7734 mlx5_flow_tunnel_decap_set(__rte_unused struct rte_eth_dev *dev,
7735 __rte_unused struct rte_flow_tunnel *app_tunnel,
7736 __rte_unused struct rte_flow_action **actions,
7737 __rte_unused uint32_t *num_of_actions,
7738 __rte_unused struct rte_flow_error *error)
7744 mlx5_flow_tunnel_match(__rte_unused struct rte_eth_dev *dev,
7745 __rte_unused struct rte_flow_tunnel *app_tunnel,
7746 __rte_unused struct rte_flow_item **items,
7747 __rte_unused uint32_t *num_of_items,
7748 __rte_unused struct rte_flow_error *error)
7754 mlx5_flow_tunnel_item_release(__rte_unused struct rte_eth_dev *dev,
7755 __rte_unused struct rte_flow_item *pmd_items,
7756 __rte_unused uint32_t num_items,
7757 __rte_unused struct rte_flow_error *err)
7763 mlx5_flow_tunnel_action_release(__rte_unused struct rte_eth_dev *dev,
7764 __rte_unused struct rte_flow_action *pmd_action,
7765 __rte_unused uint32_t num_actions,
7766 __rte_unused struct rte_flow_error *err)
7772 mlx5_flow_tunnel_get_restore_info(__rte_unused struct rte_eth_dev *dev,
7773 __rte_unused struct rte_mbuf *m,
7774 __rte_unused struct rte_flow_restore_info *i,
7775 __rte_unused struct rte_flow_error *err)
7781 flow_tunnel_add_default_miss(__rte_unused struct rte_eth_dev *dev,
7782 __rte_unused struct rte_flow *flow,
7783 __rte_unused const struct rte_flow_attr *attr,
7784 __rte_unused const struct rte_flow_action *actions,
7785 __rte_unused uint32_t flow_idx,
7786 __rte_unused struct tunnel_default_miss_ctx *ctx,
7787 __rte_unused struct rte_flow_error *error)
7792 static struct mlx5_flow_tunnel *
7793 mlx5_find_tunnel_id(__rte_unused struct rte_eth_dev *dev,
7794 __rte_unused uint32_t id)
7800 mlx5_flow_tunnel_free(__rte_unused struct rte_eth_dev *dev,
7801 __rte_unused struct mlx5_flow_tunnel *tunnel)
7806 tunnel_flow_group_to_flow_table(__rte_unused struct rte_eth_dev *dev,
7807 __rte_unused const struct mlx5_flow_tunnel *t,
7808 __rte_unused uint32_t group,
7809 __rte_unused uint32_t *table,
7810 struct rte_flow_error *error)
7812 return rte_flow_error_set(error, ENOTSUP,
7813 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
7814 "tunnel offload requires DV support");
7818 mlx5_release_tunnel_hub(__rte_unused struct mlx5_dev_ctx_shared *sh,
7819 __rte_unused uint16_t port_id)
7822 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */