net/mlx5: split meter color policy handling
[dpdk.git] / drivers / net / mlx5 / mlx5_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5
6 #include <stdalign.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <stdbool.h>
10 #include <sys/queue.h>
11
12 #include <rte_common.h>
13 #include <rte_ether.h>
14 #include <ethdev_driver.h>
15 #include <rte_eal_paging.h>
16 #include <rte_flow.h>
17 #include <rte_cycles.h>
18 #include <rte_flow_driver.h>
19 #include <rte_malloc.h>
20 #include <rte_ip.h>
21
22 #include <mlx5_glue.h>
23 #include <mlx5_devx_cmds.h>
24 #include <mlx5_prm.h>
25 #include <mlx5_malloc.h>
26
27 #include "mlx5_defs.h"
28 #include "mlx5.h"
29 #include "mlx5_flow.h"
30 #include "mlx5_flow_os.h"
31 #include "mlx5_rx.h"
32 #include "mlx5_tx.h"
33 #include "mlx5_common_os.h"
34 #include "rte_pmd_mlx5.h"
35
36 struct tunnel_default_miss_ctx {
37         uint16_t *queue;
38         __extension__
39         union {
40                 struct rte_flow_action_rss action_rss;
41                 struct rte_flow_action_queue miss_queue;
42                 struct rte_flow_action_jump miss_jump;
43                 uint8_t raw[0];
44         };
45 };
46
47 static int
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,
52                              uint32_t flow_idx,
53                              const struct mlx5_flow_tunnel *tunnel,
54                              struct tunnel_default_miss_ctx *ctx,
55                              struct rte_flow_error *error);
56 static struct mlx5_flow_tunnel *
57 mlx5_find_tunnel_id(struct rte_eth_dev *dev, uint32_t id);
58 static void
59 mlx5_flow_tunnel_free(struct rte_eth_dev *dev, struct mlx5_flow_tunnel *tunnel);
60 static uint32_t
61 tunnel_flow_group_to_flow_table(struct rte_eth_dev *dev,
62                                 const struct mlx5_flow_tunnel *tunnel,
63                                 uint32_t group, uint32_t *table,
64                                 struct rte_flow_error *error);
65
66 static struct mlx5_flow_workspace *mlx5_flow_push_thread_workspace(void);
67 static void mlx5_flow_pop_thread_workspace(void);
68
69
70 /** Device flow drivers. */
71 extern const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops;
72
73 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops;
74
75 const struct mlx5_flow_driver_ops *flow_drv_ops[] = {
76         [MLX5_FLOW_TYPE_MIN] = &mlx5_flow_null_drv_ops,
77 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
78         [MLX5_FLOW_TYPE_DV] = &mlx5_flow_dv_drv_ops,
79 #endif
80         [MLX5_FLOW_TYPE_VERBS] = &mlx5_flow_verbs_drv_ops,
81         [MLX5_FLOW_TYPE_MAX] = &mlx5_flow_null_drv_ops
82 };
83
84 /** Helper macro to build input graph for mlx5_flow_expand_rss(). */
85 #define MLX5_FLOW_EXPAND_RSS_NEXT(...) \
86         (const int []){ \
87                 __VA_ARGS__, 0, \
88         }
89
90 /** Node object of input graph for mlx5_flow_expand_rss(). */
91 struct mlx5_flow_expand_node {
92         const int *const next;
93         /**<
94          * List of next node indexes. Index 0 is interpreted as a terminator.
95          */
96         const enum rte_flow_item_type type;
97         /**< Pattern item type of current node. */
98         uint64_t rss_types;
99         /**<
100          * RSS types bit-field associated with this node
101          * (see ETH_RSS_* definitions).
102          */
103         uint8_t optional;
104         /**< optional expand field. Default 0 to expand, 1 not go deeper. */
105 };
106
107 /** Object returned by mlx5_flow_expand_rss(). */
108 struct mlx5_flow_expand_rss {
109         uint32_t entries;
110         /**< Number of entries @p patterns and @p priorities. */
111         struct {
112                 struct rte_flow_item *pattern; /**< Expanded pattern array. */
113                 uint32_t priority; /**< Priority offset for each expansion. */
114         } entry[];
115 };
116
117 static void
118 mlx5_dbg__print_pattern(const struct rte_flow_item *item);
119
120 static bool
121 mlx5_flow_is_rss_expandable_item(const struct rte_flow_item *item)
122 {
123         switch (item->type) {
124         case RTE_FLOW_ITEM_TYPE_ETH:
125         case RTE_FLOW_ITEM_TYPE_VLAN:
126         case RTE_FLOW_ITEM_TYPE_IPV4:
127         case RTE_FLOW_ITEM_TYPE_IPV6:
128         case RTE_FLOW_ITEM_TYPE_UDP:
129         case RTE_FLOW_ITEM_TYPE_TCP:
130         case RTE_FLOW_ITEM_TYPE_VXLAN:
131         case RTE_FLOW_ITEM_TYPE_NVGRE:
132         case RTE_FLOW_ITEM_TYPE_GRE:
133         case RTE_FLOW_ITEM_TYPE_GENEVE:
134         case RTE_FLOW_ITEM_TYPE_MPLS:
135         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
136         case RTE_FLOW_ITEM_TYPE_GRE_KEY:
137         case RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT:
138                 return true;
139         default:
140                 break;
141         }
142         return false;
143 }
144
145 static enum rte_flow_item_type
146 mlx5_flow_expand_rss_item_complete(const struct rte_flow_item *item)
147 {
148         enum rte_flow_item_type ret = RTE_FLOW_ITEM_TYPE_VOID;
149         uint16_t ether_type = 0;
150         uint16_t ether_type_m;
151         uint8_t ip_next_proto = 0;
152         uint8_t ip_next_proto_m;
153
154         if (item == NULL || item->spec == NULL)
155                 return ret;
156         switch (item->type) {
157         case RTE_FLOW_ITEM_TYPE_ETH:
158                 if (item->mask)
159                         ether_type_m = ((const struct rte_flow_item_eth *)
160                                                 (item->mask))->type;
161                 else
162                         ether_type_m = rte_flow_item_eth_mask.type;
163                 if (ether_type_m != RTE_BE16(0xFFFF))
164                         break;
165                 ether_type = ((const struct rte_flow_item_eth *)
166                                 (item->spec))->type;
167                 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
168                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
169                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
170                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
171                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
172                         ret = RTE_FLOW_ITEM_TYPE_VLAN;
173                 else
174                         ret = RTE_FLOW_ITEM_TYPE_END;
175                 break;
176         case RTE_FLOW_ITEM_TYPE_VLAN:
177                 if (item->mask)
178                         ether_type_m = ((const struct rte_flow_item_vlan *)
179                                                 (item->mask))->inner_type;
180                 else
181                         ether_type_m = rte_flow_item_vlan_mask.inner_type;
182                 if (ether_type_m != RTE_BE16(0xFFFF))
183                         break;
184                 ether_type = ((const struct rte_flow_item_vlan *)
185                                 (item->spec))->inner_type;
186                 if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV4)
187                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
188                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_IPV6)
189                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
190                 else if (rte_be_to_cpu_16(ether_type) == RTE_ETHER_TYPE_VLAN)
191                         ret = RTE_FLOW_ITEM_TYPE_VLAN;
192                 else
193                         ret = RTE_FLOW_ITEM_TYPE_END;
194                 break;
195         case RTE_FLOW_ITEM_TYPE_IPV4:
196                 if (item->mask)
197                         ip_next_proto_m = ((const struct rte_flow_item_ipv4 *)
198                                         (item->mask))->hdr.next_proto_id;
199                 else
200                         ip_next_proto_m =
201                                 rte_flow_item_ipv4_mask.hdr.next_proto_id;
202                 if (ip_next_proto_m != 0xFF)
203                         break;
204                 ip_next_proto = ((const struct rte_flow_item_ipv4 *)
205                                 (item->spec))->hdr.next_proto_id;
206                 if (ip_next_proto == IPPROTO_UDP)
207                         ret = RTE_FLOW_ITEM_TYPE_UDP;
208                 else if (ip_next_proto == IPPROTO_TCP)
209                         ret = RTE_FLOW_ITEM_TYPE_TCP;
210                 else if (ip_next_proto == IPPROTO_IP)
211                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
212                 else if (ip_next_proto == IPPROTO_IPV6)
213                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
214                 else
215                         ret = RTE_FLOW_ITEM_TYPE_END;
216                 break;
217         case RTE_FLOW_ITEM_TYPE_IPV6:
218                 if (item->mask)
219                         ip_next_proto_m = ((const struct rte_flow_item_ipv6 *)
220                                                 (item->mask))->hdr.proto;
221                 else
222                         ip_next_proto_m =
223                                 rte_flow_item_ipv6_mask.hdr.proto;
224                 if (ip_next_proto_m != 0xFF)
225                         break;
226                 ip_next_proto = ((const struct rte_flow_item_ipv6 *)
227                                 (item->spec))->hdr.proto;
228                 if (ip_next_proto == IPPROTO_UDP)
229                         ret = RTE_FLOW_ITEM_TYPE_UDP;
230                 else if (ip_next_proto == IPPROTO_TCP)
231                         ret = RTE_FLOW_ITEM_TYPE_TCP;
232                 else if (ip_next_proto == IPPROTO_IP)
233                         ret = RTE_FLOW_ITEM_TYPE_IPV4;
234                 else if (ip_next_proto == IPPROTO_IPV6)
235                         ret = RTE_FLOW_ITEM_TYPE_IPV6;
236                 else
237                         ret = RTE_FLOW_ITEM_TYPE_END;
238                 break;
239         default:
240                 ret = RTE_FLOW_ITEM_TYPE_VOID;
241                 break;
242         }
243         return ret;
244 }
245
246 #define MLX5_RSS_EXP_ELT_N 16
247
248 /**
249  * Expand RSS flows into several possible flows according to the RSS hash
250  * fields requested and the driver capabilities.
251  *
252  * @param[out] buf
253  *   Buffer to store the result expansion.
254  * @param[in] size
255  *   Buffer size in bytes. If 0, @p buf can be NULL.
256  * @param[in] pattern
257  *   User flow pattern.
258  * @param[in] types
259  *   RSS types to expand (see ETH_RSS_* definitions).
260  * @param[in] graph
261  *   Input graph to expand @p pattern according to @p types.
262  * @param[in] graph_root_index
263  *   Index of root node in @p graph, typically 0.
264  *
265  * @return
266  *   A positive value representing the size of @p buf in bytes regardless of
267  *   @p size on success, a negative errno value otherwise and rte_errno is
268  *   set, the following errors are defined:
269  *
270  *   -E2BIG: graph-depth @p graph is too deep.
271  *   -EINVAL: @p size has not enough space for expanded pattern.
272  */
273 static int
274 mlx5_flow_expand_rss(struct mlx5_flow_expand_rss *buf, size_t size,
275                      const struct rte_flow_item *pattern, uint64_t types,
276                      const struct mlx5_flow_expand_node graph[],
277                      int graph_root_index)
278 {
279         const struct rte_flow_item *item;
280         const struct mlx5_flow_expand_node *node = &graph[graph_root_index];
281         const int *next_node;
282         const int *stack[MLX5_RSS_EXP_ELT_N];
283         int stack_pos = 0;
284         struct rte_flow_item flow_items[MLX5_RSS_EXP_ELT_N];
285         unsigned int i;
286         size_t lsize;
287         size_t user_pattern_size = 0;
288         void *addr = NULL;
289         const struct mlx5_flow_expand_node *next = NULL;
290         struct rte_flow_item missed_item;
291         int missed = 0;
292         int elt = 0;
293         const struct rte_flow_item *last_item = NULL;
294
295         memset(&missed_item, 0, sizeof(missed_item));
296         lsize = offsetof(struct mlx5_flow_expand_rss, entry) +
297                 MLX5_RSS_EXP_ELT_N * sizeof(buf->entry[0]);
298         if (lsize > size)
299                 return -EINVAL;
300         buf->entry[0].priority = 0;
301         buf->entry[0].pattern = (void *)&buf->entry[MLX5_RSS_EXP_ELT_N];
302         buf->entries = 0;
303         addr = buf->entry[0].pattern;
304         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
305                 if (!mlx5_flow_is_rss_expandable_item(item)) {
306                         user_pattern_size += sizeof(*item);
307                         continue;
308                 }
309                 last_item = item;
310                 for (i = 0; node->next && node->next[i]; ++i) {
311                         next = &graph[node->next[i]];
312                         if (next->type == item->type)
313                                 break;
314                 }
315                 if (next)
316                         node = next;
317                 user_pattern_size += sizeof(*item);
318         }
319         user_pattern_size += sizeof(*item); /* Handle END item. */
320         lsize += user_pattern_size;
321         if (lsize > size)
322                 return -EINVAL;
323         /* Copy the user pattern in the first entry of the buffer. */
324         rte_memcpy(addr, pattern, user_pattern_size);
325         addr = (void *)(((uintptr_t)addr) + user_pattern_size);
326         buf->entries = 1;
327         /* Start expanding. */
328         memset(flow_items, 0, sizeof(flow_items));
329         user_pattern_size -= sizeof(*item);
330         /*
331          * Check if the last valid item has spec set, need complete pattern,
332          * and the pattern can be used for expansion.
333          */
334         missed_item.type = mlx5_flow_expand_rss_item_complete(last_item);
335         if (missed_item.type == RTE_FLOW_ITEM_TYPE_END) {
336                 /* Item type END indicates expansion is not required. */
337                 return lsize;
338         }
339         if (missed_item.type != RTE_FLOW_ITEM_TYPE_VOID) {
340                 next = NULL;
341                 missed = 1;
342                 for (i = 0; node->next && node->next[i]; ++i) {
343                         next = &graph[node->next[i]];
344                         if (next->type == missed_item.type) {
345                                 flow_items[0].type = missed_item.type;
346                                 flow_items[1].type = RTE_FLOW_ITEM_TYPE_END;
347                                 break;
348                         }
349                         next = NULL;
350                 }
351         }
352         if (next && missed) {
353                 elt = 2; /* missed item + item end. */
354                 node = next;
355                 lsize += elt * sizeof(*item) + user_pattern_size;
356                 if (lsize > size)
357                         return -EINVAL;
358                 if (node->rss_types & types) {
359                         buf->entry[buf->entries].priority = 1;
360                         buf->entry[buf->entries].pattern = addr;
361                         buf->entries++;
362                         rte_memcpy(addr, buf->entry[0].pattern,
363                                    user_pattern_size);
364                         addr = (void *)(((uintptr_t)addr) + user_pattern_size);
365                         rte_memcpy(addr, flow_items, elt * sizeof(*item));
366                         addr = (void *)(((uintptr_t)addr) +
367                                         elt * sizeof(*item));
368                 }
369         }
370         memset(flow_items, 0, sizeof(flow_items));
371         next_node = node->next;
372         stack[stack_pos] = next_node;
373         node = next_node ? &graph[*next_node] : NULL;
374         while (node) {
375                 flow_items[stack_pos].type = node->type;
376                 if (node->rss_types & types) {
377                         size_t n;
378                         /*
379                          * compute the number of items to copy from the
380                          * expansion and copy it.
381                          * When the stack_pos is 0, there are 1 element in it,
382                          * plus the addition END item.
383                          */
384                         elt = stack_pos + 2;
385                         flow_items[stack_pos + 1].type = RTE_FLOW_ITEM_TYPE_END;
386                         lsize += elt * sizeof(*item) + user_pattern_size;
387                         if (lsize > size)
388                                 return -EINVAL;
389                         n = elt * sizeof(*item);
390                         buf->entry[buf->entries].priority =
391                                 stack_pos + 1 + missed;
392                         buf->entry[buf->entries].pattern = addr;
393                         buf->entries++;
394                         rte_memcpy(addr, buf->entry[0].pattern,
395                                    user_pattern_size);
396                         addr = (void *)(((uintptr_t)addr) +
397                                         user_pattern_size);
398                         rte_memcpy(addr, &missed_item,
399                                    missed * sizeof(*item));
400                         addr = (void *)(((uintptr_t)addr) +
401                                 missed * sizeof(*item));
402                         rte_memcpy(addr, flow_items, n);
403                         addr = (void *)(((uintptr_t)addr) + n);
404                 }
405                 /* Go deeper. */
406                 if (!node->optional && node->next) {
407                         next_node = node->next;
408                         if (stack_pos++ == MLX5_RSS_EXP_ELT_N) {
409                                 rte_errno = E2BIG;
410                                 return -rte_errno;
411                         }
412                         stack[stack_pos] = next_node;
413                 } else if (*(next_node + 1)) {
414                         /* Follow up with the next possibility. */
415                         ++next_node;
416                 } else {
417                         /* Move to the next path. */
418                         if (stack_pos)
419                                 next_node = stack[--stack_pos];
420                         next_node++;
421                         stack[stack_pos] = next_node;
422                 }
423                 node = *next_node ? &graph[*next_node] : NULL;
424         };
425         return lsize;
426 }
427
428 enum mlx5_expansion {
429         MLX5_EXPANSION_ROOT,
430         MLX5_EXPANSION_ROOT_OUTER,
431         MLX5_EXPANSION_ROOT_ETH_VLAN,
432         MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN,
433         MLX5_EXPANSION_OUTER_ETH,
434         MLX5_EXPANSION_OUTER_ETH_VLAN,
435         MLX5_EXPANSION_OUTER_VLAN,
436         MLX5_EXPANSION_OUTER_IPV4,
437         MLX5_EXPANSION_OUTER_IPV4_UDP,
438         MLX5_EXPANSION_OUTER_IPV4_TCP,
439         MLX5_EXPANSION_OUTER_IPV6,
440         MLX5_EXPANSION_OUTER_IPV6_UDP,
441         MLX5_EXPANSION_OUTER_IPV6_TCP,
442         MLX5_EXPANSION_VXLAN,
443         MLX5_EXPANSION_VXLAN_GPE,
444         MLX5_EXPANSION_GRE,
445         MLX5_EXPANSION_NVGRE,
446         MLX5_EXPANSION_GRE_KEY,
447         MLX5_EXPANSION_MPLS,
448         MLX5_EXPANSION_ETH,
449         MLX5_EXPANSION_ETH_VLAN,
450         MLX5_EXPANSION_VLAN,
451         MLX5_EXPANSION_IPV4,
452         MLX5_EXPANSION_IPV4_UDP,
453         MLX5_EXPANSION_IPV4_TCP,
454         MLX5_EXPANSION_IPV6,
455         MLX5_EXPANSION_IPV6_UDP,
456         MLX5_EXPANSION_IPV6_TCP,
457         MLX5_EXPANSION_IPV6_FRAG_EXT,
458 };
459
460 /** Supported expansion of items. */
461 static const struct mlx5_flow_expand_node mlx5_support_expansion[] = {
462         [MLX5_EXPANSION_ROOT] = {
463                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
464                                                   MLX5_EXPANSION_IPV4,
465                                                   MLX5_EXPANSION_IPV6),
466                 .type = RTE_FLOW_ITEM_TYPE_END,
467         },
468         [MLX5_EXPANSION_ROOT_OUTER] = {
469                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH,
470                                                   MLX5_EXPANSION_OUTER_IPV4,
471                                                   MLX5_EXPANSION_OUTER_IPV6),
472                 .type = RTE_FLOW_ITEM_TYPE_END,
473         },
474         [MLX5_EXPANSION_ROOT_ETH_VLAN] = {
475                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH_VLAN),
476                 .type = RTE_FLOW_ITEM_TYPE_END,
477         },
478         [MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN] = {
479                 .next = MLX5_FLOW_EXPAND_RSS_NEXT
480                                                 (MLX5_EXPANSION_OUTER_ETH_VLAN),
481                 .type = RTE_FLOW_ITEM_TYPE_END,
482         },
483         [MLX5_EXPANSION_OUTER_ETH] = {
484                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
485                                                   MLX5_EXPANSION_OUTER_IPV6),
486                 .type = RTE_FLOW_ITEM_TYPE_ETH,
487                 .rss_types = 0,
488         },
489         [MLX5_EXPANSION_OUTER_ETH_VLAN] = {
490                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_VLAN),
491                 .type = RTE_FLOW_ITEM_TYPE_ETH,
492                 .rss_types = 0,
493         },
494         [MLX5_EXPANSION_OUTER_VLAN] = {
495                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
496                                                   MLX5_EXPANSION_OUTER_IPV6),
497                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
498         },
499         [MLX5_EXPANSION_OUTER_IPV4] = {
500                 .next = MLX5_FLOW_EXPAND_RSS_NEXT
501                         (MLX5_EXPANSION_OUTER_IPV4_UDP,
502                          MLX5_EXPANSION_OUTER_IPV4_TCP,
503                          MLX5_EXPANSION_GRE,
504                          MLX5_EXPANSION_NVGRE,
505                          MLX5_EXPANSION_IPV4,
506                          MLX5_EXPANSION_IPV6),
507                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
508                 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
509                         ETH_RSS_NONFRAG_IPV4_OTHER,
510         },
511         [MLX5_EXPANSION_OUTER_IPV4_UDP] = {
512                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
513                                                   MLX5_EXPANSION_VXLAN_GPE,
514                                                   MLX5_EXPANSION_MPLS),
515                 .type = RTE_FLOW_ITEM_TYPE_UDP,
516                 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
517         },
518         [MLX5_EXPANSION_OUTER_IPV4_TCP] = {
519                 .type = RTE_FLOW_ITEM_TYPE_TCP,
520                 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
521         },
522         [MLX5_EXPANSION_OUTER_IPV6] = {
523                 .next = MLX5_FLOW_EXPAND_RSS_NEXT
524                         (MLX5_EXPANSION_OUTER_IPV6_UDP,
525                          MLX5_EXPANSION_OUTER_IPV6_TCP,
526                          MLX5_EXPANSION_IPV4,
527                          MLX5_EXPANSION_IPV6,
528                          MLX5_EXPANSION_GRE,
529                          MLX5_EXPANSION_NVGRE),
530                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
531                 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
532                         ETH_RSS_NONFRAG_IPV6_OTHER,
533         },
534         [MLX5_EXPANSION_OUTER_IPV6_UDP] = {
535                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
536                                                   MLX5_EXPANSION_VXLAN_GPE,
537                                                   MLX5_EXPANSION_MPLS),
538                 .type = RTE_FLOW_ITEM_TYPE_UDP,
539                 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
540         },
541         [MLX5_EXPANSION_OUTER_IPV6_TCP] = {
542                 .type = RTE_FLOW_ITEM_TYPE_TCP,
543                 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
544         },
545         [MLX5_EXPANSION_VXLAN] = {
546                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
547                                                   MLX5_EXPANSION_IPV4,
548                                                   MLX5_EXPANSION_IPV6),
549                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
550         },
551         [MLX5_EXPANSION_VXLAN_GPE] = {
552                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
553                                                   MLX5_EXPANSION_IPV4,
554                                                   MLX5_EXPANSION_IPV6),
555                 .type = RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
556         },
557         [MLX5_EXPANSION_GRE] = {
558                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
559                                                   MLX5_EXPANSION_IPV6,
560                                                   MLX5_EXPANSION_GRE_KEY,
561                                                   MLX5_EXPANSION_MPLS),
562                 .type = RTE_FLOW_ITEM_TYPE_GRE,
563         },
564         [MLX5_EXPANSION_GRE_KEY] = {
565                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
566                                                   MLX5_EXPANSION_IPV6,
567                                                   MLX5_EXPANSION_MPLS),
568                 .type = RTE_FLOW_ITEM_TYPE_GRE_KEY,
569                 .optional = 1,
570         },
571         [MLX5_EXPANSION_NVGRE] = {
572                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH),
573                 .type = RTE_FLOW_ITEM_TYPE_NVGRE,
574         },
575         [MLX5_EXPANSION_MPLS] = {
576                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
577                                                   MLX5_EXPANSION_IPV6,
578                                                   MLX5_EXPANSION_ETH),
579                 .type = RTE_FLOW_ITEM_TYPE_MPLS,
580         },
581         [MLX5_EXPANSION_ETH] = {
582                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
583                                                   MLX5_EXPANSION_IPV6),
584                 .type = RTE_FLOW_ITEM_TYPE_ETH,
585         },
586         [MLX5_EXPANSION_ETH_VLAN] = {
587                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VLAN),
588                 .type = RTE_FLOW_ITEM_TYPE_ETH,
589         },
590         [MLX5_EXPANSION_VLAN] = {
591                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
592                                                   MLX5_EXPANSION_IPV6),
593                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
594         },
595         [MLX5_EXPANSION_IPV4] = {
596                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4_UDP,
597                                                   MLX5_EXPANSION_IPV4_TCP),
598                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
599                 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
600                         ETH_RSS_NONFRAG_IPV4_OTHER,
601         },
602         [MLX5_EXPANSION_IPV4_UDP] = {
603                 .type = RTE_FLOW_ITEM_TYPE_UDP,
604                 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
605         },
606         [MLX5_EXPANSION_IPV4_TCP] = {
607                 .type = RTE_FLOW_ITEM_TYPE_TCP,
608                 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
609         },
610         [MLX5_EXPANSION_IPV6] = {
611                 .next = MLX5_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV6_UDP,
612                                                   MLX5_EXPANSION_IPV6_TCP,
613                                                   MLX5_EXPANSION_IPV6_FRAG_EXT),
614                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
615                 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
616                         ETH_RSS_NONFRAG_IPV6_OTHER,
617         },
618         [MLX5_EXPANSION_IPV6_UDP] = {
619                 .type = RTE_FLOW_ITEM_TYPE_UDP,
620                 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
621         },
622         [MLX5_EXPANSION_IPV6_TCP] = {
623                 .type = RTE_FLOW_ITEM_TYPE_TCP,
624                 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
625         },
626         [MLX5_EXPANSION_IPV6_FRAG_EXT] = {
627                 .type = RTE_FLOW_ITEM_TYPE_IPV6_FRAG_EXT,
628         },
629 };
630
631 static struct rte_flow_action_handle *
632 mlx5_action_handle_create(struct rte_eth_dev *dev,
633                           const struct rte_flow_indir_action_conf *conf,
634                           const struct rte_flow_action *action,
635                           struct rte_flow_error *error);
636 static int mlx5_action_handle_destroy
637                                 (struct rte_eth_dev *dev,
638                                  struct rte_flow_action_handle *handle,
639                                  struct rte_flow_error *error);
640 static int mlx5_action_handle_update
641                                 (struct rte_eth_dev *dev,
642                                  struct rte_flow_action_handle *handle,
643                                  const void *update,
644                                  struct rte_flow_error *error);
645 static int mlx5_action_handle_query
646                                 (struct rte_eth_dev *dev,
647                                  const struct rte_flow_action_handle *handle,
648                                  void *data,
649                                  struct rte_flow_error *error);
650 static int
651 mlx5_flow_tunnel_decap_set(struct rte_eth_dev *dev,
652                     struct rte_flow_tunnel *app_tunnel,
653                     struct rte_flow_action **actions,
654                     uint32_t *num_of_actions,
655                     struct rte_flow_error *error);
656 static int
657 mlx5_flow_tunnel_match(struct rte_eth_dev *dev,
658                        struct rte_flow_tunnel *app_tunnel,
659                        struct rte_flow_item **items,
660                        uint32_t *num_of_items,
661                        struct rte_flow_error *error);
662 static int
663 mlx5_flow_tunnel_item_release(struct rte_eth_dev *dev,
664                               struct rte_flow_item *pmd_items,
665                               uint32_t num_items, struct rte_flow_error *err);
666 static int
667 mlx5_flow_tunnel_action_release(struct rte_eth_dev *dev,
668                                 struct rte_flow_action *pmd_actions,
669                                 uint32_t num_actions,
670                                 struct rte_flow_error *err);
671 static int
672 mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
673                                   struct rte_mbuf *m,
674                                   struct rte_flow_restore_info *info,
675                                   struct rte_flow_error *err);
676
677 static const struct rte_flow_ops mlx5_flow_ops = {
678         .validate = mlx5_flow_validate,
679         .create = mlx5_flow_create,
680         .destroy = mlx5_flow_destroy,
681         .flush = mlx5_flow_flush,
682         .isolate = mlx5_flow_isolate,
683         .query = mlx5_flow_query,
684         .dev_dump = mlx5_flow_dev_dump,
685         .get_aged_flows = mlx5_flow_get_aged_flows,
686         .action_handle_create = mlx5_action_handle_create,
687         .action_handle_destroy = mlx5_action_handle_destroy,
688         .action_handle_update = mlx5_action_handle_update,
689         .action_handle_query = mlx5_action_handle_query,
690         .tunnel_decap_set = mlx5_flow_tunnel_decap_set,
691         .tunnel_match = mlx5_flow_tunnel_match,
692         .tunnel_action_decap_release = mlx5_flow_tunnel_action_release,
693         .tunnel_item_release = mlx5_flow_tunnel_item_release,
694         .get_restore_info = mlx5_flow_tunnel_get_restore_info,
695 };
696
697 /* Tunnel information. */
698 struct mlx5_flow_tunnel_info {
699         uint64_t tunnel; /**< Tunnel bit (see MLX5_FLOW_*). */
700         uint32_t ptype; /**< Tunnel Ptype (see RTE_PTYPE_*). */
701 };
702
703 static struct mlx5_flow_tunnel_info tunnels_info[] = {
704         {
705                 .tunnel = MLX5_FLOW_LAYER_VXLAN,
706                 .ptype = RTE_PTYPE_TUNNEL_VXLAN | RTE_PTYPE_L4_UDP,
707         },
708         {
709                 .tunnel = MLX5_FLOW_LAYER_GENEVE,
710                 .ptype = RTE_PTYPE_TUNNEL_GENEVE | RTE_PTYPE_L4_UDP,
711         },
712         {
713                 .tunnel = MLX5_FLOW_LAYER_VXLAN_GPE,
714                 .ptype = RTE_PTYPE_TUNNEL_VXLAN_GPE | RTE_PTYPE_L4_UDP,
715         },
716         {
717                 .tunnel = MLX5_FLOW_LAYER_GRE,
718                 .ptype = RTE_PTYPE_TUNNEL_GRE,
719         },
720         {
721                 .tunnel = MLX5_FLOW_LAYER_MPLS | MLX5_FLOW_LAYER_OUTER_L4_UDP,
722                 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_UDP | RTE_PTYPE_L4_UDP,
723         },
724         {
725                 .tunnel = MLX5_FLOW_LAYER_MPLS,
726                 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
727         },
728         {
729                 .tunnel = MLX5_FLOW_LAYER_NVGRE,
730                 .ptype = RTE_PTYPE_TUNNEL_NVGRE,
731         },
732         {
733                 .tunnel = MLX5_FLOW_LAYER_IPIP,
734                 .ptype = RTE_PTYPE_TUNNEL_IP,
735         },
736         {
737                 .tunnel = MLX5_FLOW_LAYER_IPV6_ENCAP,
738                 .ptype = RTE_PTYPE_TUNNEL_IP,
739         },
740         {
741                 .tunnel = MLX5_FLOW_LAYER_GTP,
742                 .ptype = RTE_PTYPE_TUNNEL_GTPU,
743         },
744 };
745
746
747
748 /**
749  * Translate tag ID to register.
750  *
751  * @param[in] dev
752  *   Pointer to the Ethernet device structure.
753  * @param[in] feature
754  *   The feature that request the register.
755  * @param[in] id
756  *   The request register ID.
757  * @param[out] error
758  *   Error description in case of any.
759  *
760  * @return
761  *   The request register on success, a negative errno
762  *   value otherwise and rte_errno is set.
763  */
764 int
765 mlx5_flow_get_reg_id(struct rte_eth_dev *dev,
766                      enum mlx5_feature_name feature,
767                      uint32_t id,
768                      struct rte_flow_error *error)
769 {
770         struct mlx5_priv *priv = dev->data->dev_private;
771         struct mlx5_dev_config *config = &priv->config;
772         enum modify_reg start_reg;
773         bool skip_mtr_reg = false;
774
775         switch (feature) {
776         case MLX5_HAIRPIN_RX:
777                 return REG_B;
778         case MLX5_HAIRPIN_TX:
779                 return REG_A;
780         case MLX5_METADATA_RX:
781                 switch (config->dv_xmeta_en) {
782                 case MLX5_XMETA_MODE_LEGACY:
783                         return REG_B;
784                 case MLX5_XMETA_MODE_META16:
785                         return REG_C_0;
786                 case MLX5_XMETA_MODE_META32:
787                         return REG_C_1;
788                 }
789                 break;
790         case MLX5_METADATA_TX:
791                 return REG_A;
792         case MLX5_METADATA_FDB:
793                 switch (config->dv_xmeta_en) {
794                 case MLX5_XMETA_MODE_LEGACY:
795                         return REG_NON;
796                 case MLX5_XMETA_MODE_META16:
797                         return REG_C_0;
798                 case MLX5_XMETA_MODE_META32:
799                         return REG_C_1;
800                 }
801                 break;
802         case MLX5_FLOW_MARK:
803                 switch (config->dv_xmeta_en) {
804                 case MLX5_XMETA_MODE_LEGACY:
805                         return REG_NON;
806                 case MLX5_XMETA_MODE_META16:
807                         return REG_C_1;
808                 case MLX5_XMETA_MODE_META32:
809                         return REG_C_0;
810                 }
811                 break;
812         case MLX5_MTR_ID:
813                 /*
814                  * If meter color and meter id share one register, flow match
815                  * should use the meter color register for match.
816                  */
817                 if (priv->mtr_reg_share)
818                         return priv->mtr_color_reg;
819                 else
820                         return priv->mtr_color_reg != REG_C_2 ? REG_C_2 :
821                                REG_C_3;
822         case MLX5_MTR_COLOR:
823         case MLX5_ASO_FLOW_HIT:
824         case MLX5_ASO_CONNTRACK:
825                 /* All features use the same REG_C. */
826                 MLX5_ASSERT(priv->mtr_color_reg != REG_NON);
827                 return priv->mtr_color_reg;
828         case MLX5_COPY_MARK:
829                 /*
830                  * Metadata COPY_MARK register using is in meter suffix sub
831                  * flow while with meter. It's safe to share the same register.
832                  */
833                 return priv->mtr_color_reg != REG_C_2 ? REG_C_2 : REG_C_3;
834         case MLX5_APP_TAG:
835                 /*
836                  * If meter is enable, it will engage the register for color
837                  * match and flow match. If meter color match is not using the
838                  * REG_C_2, need to skip the REG_C_x be used by meter color
839                  * match.
840                  * If meter is disable, free to use all available registers.
841                  */
842                 start_reg = priv->mtr_color_reg != REG_C_2 ? REG_C_2 :
843                             (priv->mtr_reg_share ? REG_C_3 : REG_C_4);
844                 skip_mtr_reg = !!(priv->mtr_en && start_reg == REG_C_2);
845                 if (id > (uint32_t)(REG_C_7 - start_reg))
846                         return rte_flow_error_set(error, EINVAL,
847                                                   RTE_FLOW_ERROR_TYPE_ITEM,
848                                                   NULL, "invalid tag id");
849                 if (config->flow_mreg_c[id + start_reg - REG_C_0] == REG_NON)
850                         return rte_flow_error_set(error, ENOTSUP,
851                                                   RTE_FLOW_ERROR_TYPE_ITEM,
852                                                   NULL, "unsupported tag id");
853                 /*
854                  * This case means meter is using the REG_C_x great than 2.
855                  * Take care not to conflict with meter color REG_C_x.
856                  * If the available index REG_C_y >= REG_C_x, skip the
857                  * color register.
858                  */
859                 if (skip_mtr_reg && config->flow_mreg_c
860                     [id + start_reg - REG_C_0] >= priv->mtr_color_reg) {
861                         if (id >= (uint32_t)(REG_C_7 - start_reg))
862                                 return rte_flow_error_set(error, EINVAL,
863                                                        RTE_FLOW_ERROR_TYPE_ITEM,
864                                                         NULL, "invalid tag id");
865                         if (config->flow_mreg_c
866                             [id + 1 + start_reg - REG_C_0] != REG_NON)
867                                 return config->flow_mreg_c
868                                                [id + 1 + start_reg - REG_C_0];
869                         return rte_flow_error_set(error, ENOTSUP,
870                                                   RTE_FLOW_ERROR_TYPE_ITEM,
871                                                   NULL, "unsupported tag id");
872                 }
873                 return config->flow_mreg_c[id + start_reg - REG_C_0];
874         }
875         MLX5_ASSERT(false);
876         return rte_flow_error_set(error, EINVAL,
877                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
878                                   NULL, "invalid feature name");
879 }
880
881 /**
882  * Check extensive flow metadata register support.
883  *
884  * @param dev
885  *   Pointer to rte_eth_dev structure.
886  *
887  * @return
888  *   True if device supports extensive flow metadata register, otherwise false.
889  */
890 bool
891 mlx5_flow_ext_mreg_supported(struct rte_eth_dev *dev)
892 {
893         struct mlx5_priv *priv = dev->data->dev_private;
894         struct mlx5_dev_config *config = &priv->config;
895
896         /*
897          * Having available reg_c can be regarded inclusively as supporting
898          * extensive flow metadata register, which could mean,
899          * - metadata register copy action by modify header.
900          * - 16 modify header actions is supported.
901          * - reg_c's are preserved across different domain (FDB and NIC) on
902          *   packet loopback by flow lookup miss.
903          */
904         return config->flow_mreg_c[2] != REG_NON;
905 }
906
907 /**
908  * Get the lowest priority.
909  *
910  * @param[in] dev
911  *   Pointer to the Ethernet device structure.
912  * @param[in] attributes
913  *   Pointer to device flow rule attributes.
914  *
915  * @return
916  *   The value of lowest priority of flow.
917  */
918 uint32_t
919 mlx5_get_lowest_priority(struct rte_eth_dev *dev,
920                           const struct rte_flow_attr *attr)
921 {
922         struct mlx5_priv *priv = dev->data->dev_private;
923
924         if (!attr->group && !attr->transfer)
925                 return priv->config.flow_prio - 2;
926         return MLX5_NON_ROOT_FLOW_MAX_PRIO - 1;
927 }
928
929 /**
930  * Calculate matcher priority of the flow.
931  *
932  * @param[in] dev
933  *   Pointer to the Ethernet device structure.
934  * @param[in] attr
935  *   Pointer to device flow rule attributes.
936  * @param[in] subpriority
937  *   The priority based on the items.
938  * @return
939  *   The matcher priority of the flow.
940  */
941 uint16_t
942 mlx5_get_matcher_priority(struct rte_eth_dev *dev,
943                           const struct rte_flow_attr *attr,
944                           uint32_t subpriority)
945 {
946         uint16_t priority = (uint16_t)attr->priority;
947         struct mlx5_priv *priv = dev->data->dev_private;
948
949         if (!attr->group && !attr->transfer) {
950                 if (attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)
951                         priority = priv->config.flow_prio - 1;
952                 return mlx5_os_flow_adjust_priority(dev, priority, subpriority);
953         }
954         if (attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)
955                 priority = MLX5_NON_ROOT_FLOW_MAX_PRIO;
956         return priority * 3 + subpriority;
957 }
958
959 /**
960  * Verify the @p item specifications (spec, last, mask) are compatible with the
961  * NIC capabilities.
962  *
963  * @param[in] item
964  *   Item specification.
965  * @param[in] mask
966  *   @p item->mask or flow default bit-masks.
967  * @param[in] nic_mask
968  *   Bit-masks covering supported fields by the NIC to compare with user mask.
969  * @param[in] size
970  *   Bit-masks size in bytes.
971  * @param[in] range_accepted
972  *   True if range of values is accepted for specific fields, false otherwise.
973  * @param[out] error
974  *   Pointer to error structure.
975  *
976  * @return
977  *   0 on success, a negative errno value otherwise and rte_errno is set.
978  */
979 int
980 mlx5_flow_item_acceptable(const struct rte_flow_item *item,
981                           const uint8_t *mask,
982                           const uint8_t *nic_mask,
983                           unsigned int size,
984                           bool range_accepted,
985                           struct rte_flow_error *error)
986 {
987         unsigned int i;
988
989         MLX5_ASSERT(nic_mask);
990         for (i = 0; i < size; ++i)
991                 if ((nic_mask[i] | mask[i]) != nic_mask[i])
992                         return rte_flow_error_set(error, ENOTSUP,
993                                                   RTE_FLOW_ERROR_TYPE_ITEM,
994                                                   item,
995                                                   "mask enables non supported"
996                                                   " bits");
997         if (!item->spec && (item->mask || item->last))
998                 return rte_flow_error_set(error, EINVAL,
999                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1000                                           "mask/last without a spec is not"
1001                                           " supported");
1002         if (item->spec && item->last && !range_accepted) {
1003                 uint8_t spec[size];
1004                 uint8_t last[size];
1005                 unsigned int i;
1006                 int ret;
1007
1008                 for (i = 0; i < size; ++i) {
1009                         spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
1010                         last[i] = ((const uint8_t *)item->last)[i] & mask[i];
1011                 }
1012                 ret = memcmp(spec, last, size);
1013                 if (ret != 0)
1014                         return rte_flow_error_set(error, EINVAL,
1015                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1016                                                   item,
1017                                                   "range is not valid");
1018         }
1019         return 0;
1020 }
1021
1022 /**
1023  * Adjust the hash fields according to the @p flow information.
1024  *
1025  * @param[in] dev_flow.
1026  *   Pointer to the mlx5_flow.
1027  * @param[in] tunnel
1028  *   1 when the hash field is for a tunnel item.
1029  * @param[in] layer_types
1030  *   ETH_RSS_* types.
1031  * @param[in] hash_fields
1032  *   Item hash fields.
1033  *
1034  * @return
1035  *   The hash fields that should be used.
1036  */
1037 uint64_t
1038 mlx5_flow_hashfields_adjust(struct mlx5_flow_rss_desc *rss_desc,
1039                             int tunnel __rte_unused, uint64_t layer_types,
1040                             uint64_t hash_fields)
1041 {
1042 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1043         int rss_request_inner = rss_desc->level >= 2;
1044
1045         /* Check RSS hash level for tunnel. */
1046         if (tunnel && rss_request_inner)
1047                 hash_fields |= IBV_RX_HASH_INNER;
1048         else if (tunnel || rss_request_inner)
1049                 return 0;
1050 #endif
1051         /* Check if requested layer matches RSS hash fields. */
1052         if (!(rss_desc->types & layer_types))
1053                 return 0;
1054         return hash_fields;
1055 }
1056
1057 /**
1058  * Lookup and set the ptype in the data Rx part.  A single Ptype can be used,
1059  * if several tunnel rules are used on this queue, the tunnel ptype will be
1060  * cleared.
1061  *
1062  * @param rxq_ctrl
1063  *   Rx queue to update.
1064  */
1065 static void
1066 flow_rxq_tunnel_ptype_update(struct mlx5_rxq_ctrl *rxq_ctrl)
1067 {
1068         unsigned int i;
1069         uint32_t tunnel_ptype = 0;
1070
1071         /* Look up for the ptype to use. */
1072         for (i = 0; i != MLX5_FLOW_TUNNEL; ++i) {
1073                 if (!rxq_ctrl->flow_tunnels_n[i])
1074                         continue;
1075                 if (!tunnel_ptype) {
1076                         tunnel_ptype = tunnels_info[i].ptype;
1077                 } else {
1078                         tunnel_ptype = 0;
1079                         break;
1080                 }
1081         }
1082         rxq_ctrl->rxq.tunnel = tunnel_ptype;
1083 }
1084
1085 /**
1086  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) according to the devive
1087  * flow.
1088  *
1089  * @param[in] dev
1090  *   Pointer to the Ethernet device structure.
1091  * @param[in] dev_handle
1092  *   Pointer to device flow handle structure.
1093  */
1094 void
1095 flow_drv_rxq_flags_set(struct rte_eth_dev *dev,
1096                        struct mlx5_flow_handle *dev_handle)
1097 {
1098         struct mlx5_priv *priv = dev->data->dev_private;
1099         const int mark = dev_handle->mark;
1100         const int tunnel = !!(dev_handle->layers & MLX5_FLOW_LAYER_TUNNEL);
1101         struct mlx5_ind_table_obj *ind_tbl = NULL;
1102         unsigned int i;
1103
1104         if (dev_handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1105                 struct mlx5_hrxq *hrxq;
1106
1107                 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1108                               dev_handle->rix_hrxq);
1109                 if (hrxq)
1110                         ind_tbl = hrxq->ind_table;
1111         } else if (dev_handle->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
1112                 struct mlx5_shared_action_rss *shared_rss;
1113
1114                 shared_rss = mlx5_ipool_get
1115                         (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
1116                          dev_handle->rix_srss);
1117                 if (shared_rss)
1118                         ind_tbl = shared_rss->ind_tbl;
1119         }
1120         if (!ind_tbl)
1121                 return;
1122         for (i = 0; i != ind_tbl->queues_n; ++i) {
1123                 int idx = ind_tbl->queues[i];
1124                 struct mlx5_rxq_ctrl *rxq_ctrl =
1125                         container_of((*priv->rxqs)[idx],
1126                                      struct mlx5_rxq_ctrl, rxq);
1127
1128                 /*
1129                  * To support metadata register copy on Tx loopback,
1130                  * this must be always enabled (metadata may arive
1131                  * from other port - not from local flows only.
1132                  */
1133                 if (priv->config.dv_flow_en &&
1134                     priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1135                     mlx5_flow_ext_mreg_supported(dev)) {
1136                         rxq_ctrl->rxq.mark = 1;
1137                         rxq_ctrl->flow_mark_n = 1;
1138                 } else if (mark) {
1139                         rxq_ctrl->rxq.mark = 1;
1140                         rxq_ctrl->flow_mark_n++;
1141                 }
1142                 if (tunnel) {
1143                         unsigned int j;
1144
1145                         /* Increase the counter matching the flow. */
1146                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
1147                                 if ((tunnels_info[j].tunnel &
1148                                      dev_handle->layers) ==
1149                                     tunnels_info[j].tunnel) {
1150                                         rxq_ctrl->flow_tunnels_n[j]++;
1151                                         break;
1152                                 }
1153                         }
1154                         flow_rxq_tunnel_ptype_update(rxq_ctrl);
1155                 }
1156         }
1157 }
1158
1159 /**
1160  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) for a flow
1161  *
1162  * @param[in] dev
1163  *   Pointer to the Ethernet device structure.
1164  * @param[in] flow
1165  *   Pointer to flow structure.
1166  */
1167 static void
1168 flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
1169 {
1170         struct mlx5_priv *priv = dev->data->dev_private;
1171         uint32_t handle_idx;
1172         struct mlx5_flow_handle *dev_handle;
1173
1174         SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1175                        handle_idx, dev_handle, next)
1176                 flow_drv_rxq_flags_set(dev, dev_handle);
1177 }
1178
1179 /**
1180  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
1181  * device flow if no other flow uses it with the same kind of request.
1182  *
1183  * @param dev
1184  *   Pointer to Ethernet device.
1185  * @param[in] dev_handle
1186  *   Pointer to the device flow handle structure.
1187  */
1188 static void
1189 flow_drv_rxq_flags_trim(struct rte_eth_dev *dev,
1190                         struct mlx5_flow_handle *dev_handle)
1191 {
1192         struct mlx5_priv *priv = dev->data->dev_private;
1193         const int mark = dev_handle->mark;
1194         const int tunnel = !!(dev_handle->layers & MLX5_FLOW_LAYER_TUNNEL);
1195         struct mlx5_ind_table_obj *ind_tbl = NULL;
1196         unsigned int i;
1197
1198         if (dev_handle->fate_action == MLX5_FLOW_FATE_QUEUE) {
1199                 struct mlx5_hrxq *hrxq;
1200
1201                 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ],
1202                               dev_handle->rix_hrxq);
1203                 if (hrxq)
1204                         ind_tbl = hrxq->ind_table;
1205         } else if (dev_handle->fate_action == MLX5_FLOW_FATE_SHARED_RSS) {
1206                 struct mlx5_shared_action_rss *shared_rss;
1207
1208                 shared_rss = mlx5_ipool_get
1209                         (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
1210                          dev_handle->rix_srss);
1211                 if (shared_rss)
1212                         ind_tbl = shared_rss->ind_tbl;
1213         }
1214         if (!ind_tbl)
1215                 return;
1216         MLX5_ASSERT(dev->data->dev_started);
1217         for (i = 0; i != ind_tbl->queues_n; ++i) {
1218                 int idx = ind_tbl->queues[i];
1219                 struct mlx5_rxq_ctrl *rxq_ctrl =
1220                         container_of((*priv->rxqs)[idx],
1221                                      struct mlx5_rxq_ctrl, rxq);
1222
1223                 if (priv->config.dv_flow_en &&
1224                     priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1225                     mlx5_flow_ext_mreg_supported(dev)) {
1226                         rxq_ctrl->rxq.mark = 1;
1227                         rxq_ctrl->flow_mark_n = 1;
1228                 } else if (mark) {
1229                         rxq_ctrl->flow_mark_n--;
1230                         rxq_ctrl->rxq.mark = !!rxq_ctrl->flow_mark_n;
1231                 }
1232                 if (tunnel) {
1233                         unsigned int j;
1234
1235                         /* Decrease the counter matching the flow. */
1236                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
1237                                 if ((tunnels_info[j].tunnel &
1238                                      dev_handle->layers) ==
1239                                     tunnels_info[j].tunnel) {
1240                                         rxq_ctrl->flow_tunnels_n[j]--;
1241                                         break;
1242                                 }
1243                         }
1244                         flow_rxq_tunnel_ptype_update(rxq_ctrl);
1245                 }
1246         }
1247 }
1248
1249 /**
1250  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
1251  * @p flow if no other flow uses it with the same kind of request.
1252  *
1253  * @param dev
1254  *   Pointer to Ethernet device.
1255  * @param[in] flow
1256  *   Pointer to the flow.
1257  */
1258 static void
1259 flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
1260 {
1261         struct mlx5_priv *priv = dev->data->dev_private;
1262         uint32_t handle_idx;
1263         struct mlx5_flow_handle *dev_handle;
1264
1265         SILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW], flow->dev_handles,
1266                        handle_idx, dev_handle, next)
1267                 flow_drv_rxq_flags_trim(dev, dev_handle);
1268 }
1269
1270 /**
1271  * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
1272  *
1273  * @param dev
1274  *   Pointer to Ethernet device.
1275  */
1276 static void
1277 flow_rxq_flags_clear(struct rte_eth_dev *dev)
1278 {
1279         struct mlx5_priv *priv = dev->data->dev_private;
1280         unsigned int i;
1281
1282         for (i = 0; i != priv->rxqs_n; ++i) {
1283                 struct mlx5_rxq_ctrl *rxq_ctrl;
1284                 unsigned int j;
1285
1286                 if (!(*priv->rxqs)[i])
1287                         continue;
1288                 rxq_ctrl = container_of((*priv->rxqs)[i],
1289                                         struct mlx5_rxq_ctrl, rxq);
1290                 rxq_ctrl->flow_mark_n = 0;
1291                 rxq_ctrl->rxq.mark = 0;
1292                 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
1293                         rxq_ctrl->flow_tunnels_n[j] = 0;
1294                 rxq_ctrl->rxq.tunnel = 0;
1295         }
1296 }
1297
1298 /**
1299  * Set the Rx queue dynamic metadata (mask and offset) for a flow
1300  *
1301  * @param[in] dev
1302  *   Pointer to the Ethernet device structure.
1303  */
1304 void
1305 mlx5_flow_rxq_dynf_metadata_set(struct rte_eth_dev *dev)
1306 {
1307         struct mlx5_priv *priv = dev->data->dev_private;
1308         struct mlx5_rxq_data *data;
1309         unsigned int i;
1310
1311         for (i = 0; i != priv->rxqs_n; ++i) {
1312                 if (!(*priv->rxqs)[i])
1313                         continue;
1314                 data = (*priv->rxqs)[i];
1315                 if (!rte_flow_dynf_metadata_avail()) {
1316                         data->dynf_meta = 0;
1317                         data->flow_meta_mask = 0;
1318                         data->flow_meta_offset = -1;
1319                         data->flow_meta_port_mask = 0;
1320                 } else {
1321                         data->dynf_meta = 1;
1322                         data->flow_meta_mask = rte_flow_dynf_metadata_mask;
1323                         data->flow_meta_offset = rte_flow_dynf_metadata_offs;
1324                         data->flow_meta_port_mask = (uint32_t)~0;
1325                         if (priv->config.dv_xmeta_en == MLX5_XMETA_MODE_META16)
1326                                 data->flow_meta_port_mask >>= 16;
1327                 }
1328         }
1329 }
1330
1331 /*
1332  * return a pointer to the desired action in the list of actions.
1333  *
1334  * @param[in] actions
1335  *   The list of actions to search the action in.
1336  * @param[in] action
1337  *   The action to find.
1338  *
1339  * @return
1340  *   Pointer to the action in the list, if found. NULL otherwise.
1341  */
1342 const struct rte_flow_action *
1343 mlx5_flow_find_action(const struct rte_flow_action *actions,
1344                       enum rte_flow_action_type action)
1345 {
1346         if (actions == NULL)
1347                 return NULL;
1348         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++)
1349                 if (actions->type == action)
1350                         return actions;
1351         return NULL;
1352 }
1353
1354 /*
1355  * Validate the flag action.
1356  *
1357  * @param[in] action_flags
1358  *   Bit-fields that holds the actions detected until now.
1359  * @param[in] attr
1360  *   Attributes of flow that includes this action.
1361  * @param[out] error
1362  *   Pointer to error structure.
1363  *
1364  * @return
1365  *   0 on success, a negative errno value otherwise and rte_errno is set.
1366  */
1367 int
1368 mlx5_flow_validate_action_flag(uint64_t action_flags,
1369                                const struct rte_flow_attr *attr,
1370                                struct rte_flow_error *error)
1371 {
1372         if (action_flags & MLX5_FLOW_ACTION_MARK)
1373                 return rte_flow_error_set(error, EINVAL,
1374                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1375                                           "can't mark and flag in same flow");
1376         if (action_flags & MLX5_FLOW_ACTION_FLAG)
1377                 return rte_flow_error_set(error, EINVAL,
1378                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1379                                           "can't have 2 flag"
1380                                           " actions in same flow");
1381         if (attr->egress)
1382                 return rte_flow_error_set(error, ENOTSUP,
1383                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1384                                           "flag action not supported for "
1385                                           "egress");
1386         return 0;
1387 }
1388
1389 /*
1390  * Validate the mark action.
1391  *
1392  * @param[in] action
1393  *   Pointer to the queue action.
1394  * @param[in] action_flags
1395  *   Bit-fields that holds the actions detected until now.
1396  * @param[in] attr
1397  *   Attributes of flow that includes this action.
1398  * @param[out] error
1399  *   Pointer to error structure.
1400  *
1401  * @return
1402  *   0 on success, a negative errno value otherwise and rte_errno is set.
1403  */
1404 int
1405 mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
1406                                uint64_t action_flags,
1407                                const struct rte_flow_attr *attr,
1408                                struct rte_flow_error *error)
1409 {
1410         const struct rte_flow_action_mark *mark = action->conf;
1411
1412         if (!mark)
1413                 return rte_flow_error_set(error, EINVAL,
1414                                           RTE_FLOW_ERROR_TYPE_ACTION,
1415                                           action,
1416                                           "configuration cannot be null");
1417         if (mark->id >= MLX5_FLOW_MARK_MAX)
1418                 return rte_flow_error_set(error, EINVAL,
1419                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1420                                           &mark->id,
1421                                           "mark id must in 0 <= id < "
1422                                           RTE_STR(MLX5_FLOW_MARK_MAX));
1423         if (action_flags & MLX5_FLOW_ACTION_FLAG)
1424                 return rte_flow_error_set(error, EINVAL,
1425                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1426                                           "can't flag and mark in same flow");
1427         if (action_flags & MLX5_FLOW_ACTION_MARK)
1428                 return rte_flow_error_set(error, EINVAL,
1429                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1430                                           "can't have 2 mark actions in same"
1431                                           " flow");
1432         if (attr->egress)
1433                 return rte_flow_error_set(error, ENOTSUP,
1434                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1435                                           "mark action not supported for "
1436                                           "egress");
1437         return 0;
1438 }
1439
1440 /*
1441  * Validate the drop action.
1442  *
1443  * @param[in] action_flags
1444  *   Bit-fields that holds the actions detected until now.
1445  * @param[in] attr
1446  *   Attributes of flow that includes this action.
1447  * @param[out] error
1448  *   Pointer to error structure.
1449  *
1450  * @return
1451  *   0 on success, a negative errno value otherwise and rte_errno is set.
1452  */
1453 int
1454 mlx5_flow_validate_action_drop(uint64_t action_flags __rte_unused,
1455                                const struct rte_flow_attr *attr,
1456                                struct rte_flow_error *error)
1457 {
1458         if (attr->egress)
1459                 return rte_flow_error_set(error, ENOTSUP,
1460                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1461                                           "drop action not supported for "
1462                                           "egress");
1463         return 0;
1464 }
1465
1466 /*
1467  * Validate the queue action.
1468  *
1469  * @param[in] action
1470  *   Pointer to the queue action.
1471  * @param[in] action_flags
1472  *   Bit-fields that holds the actions detected until now.
1473  * @param[in] dev
1474  *   Pointer to the Ethernet device structure.
1475  * @param[in] attr
1476  *   Attributes of flow that includes this action.
1477  * @param[out] error
1478  *   Pointer to error structure.
1479  *
1480  * @return
1481  *   0 on success, a negative errno value otherwise and rte_errno is set.
1482  */
1483 int
1484 mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
1485                                 uint64_t action_flags,
1486                                 struct rte_eth_dev *dev,
1487                                 const struct rte_flow_attr *attr,
1488                                 struct rte_flow_error *error)
1489 {
1490         struct mlx5_priv *priv = dev->data->dev_private;
1491         const struct rte_flow_action_queue *queue = action->conf;
1492
1493         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1494                 return rte_flow_error_set(error, EINVAL,
1495                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1496                                           "can't have 2 fate actions in"
1497                                           " same flow");
1498         if (!priv->rxqs_n)
1499                 return rte_flow_error_set(error, EINVAL,
1500                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1501                                           NULL, "No Rx queues configured");
1502         if (queue->index >= priv->rxqs_n)
1503                 return rte_flow_error_set(error, EINVAL,
1504                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1505                                           &queue->index,
1506                                           "queue index out of range");
1507         if (!(*priv->rxqs)[queue->index])
1508                 return rte_flow_error_set(error, EINVAL,
1509                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1510                                           &queue->index,
1511                                           "queue is not configured");
1512         if (attr->egress)
1513                 return rte_flow_error_set(error, ENOTSUP,
1514                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1515                                           "queue action not supported for "
1516                                           "egress");
1517         return 0;
1518 }
1519
1520 /*
1521  * Validate the rss action.
1522  *
1523  * @param[in] dev
1524  *   Pointer to the Ethernet device structure.
1525  * @param[in] action
1526  *   Pointer to the queue action.
1527  * @param[out] error
1528  *   Pointer to error structure.
1529  *
1530  * @return
1531  *   0 on success, a negative errno value otherwise and rte_errno is set.
1532  */
1533 int
1534 mlx5_validate_action_rss(struct rte_eth_dev *dev,
1535                          const struct rte_flow_action *action,
1536                          struct rte_flow_error *error)
1537 {
1538         struct mlx5_priv *priv = dev->data->dev_private;
1539         const struct rte_flow_action_rss *rss = action->conf;
1540         enum mlx5_rxq_type rxq_type = MLX5_RXQ_TYPE_UNDEFINED;
1541         unsigned int i;
1542
1543         if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
1544             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
1545                 return rte_flow_error_set(error, ENOTSUP,
1546                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1547                                           &rss->func,
1548                                           "RSS hash function not supported");
1549 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1550         if (rss->level > 2)
1551 #else
1552         if (rss->level > 1)
1553 #endif
1554                 return rte_flow_error_set(error, ENOTSUP,
1555                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1556                                           &rss->level,
1557                                           "tunnel RSS is not supported");
1558         /* allow RSS key_len 0 in case of NULL (default) RSS key. */
1559         if (rss->key_len == 0 && rss->key != NULL)
1560                 return rte_flow_error_set(error, ENOTSUP,
1561                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1562                                           &rss->key_len,
1563                                           "RSS hash key length 0");
1564         if (rss->key_len > 0 && rss->key_len < MLX5_RSS_HASH_KEY_LEN)
1565                 return rte_flow_error_set(error, ENOTSUP,
1566                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1567                                           &rss->key_len,
1568                                           "RSS hash key too small");
1569         if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
1570                 return rte_flow_error_set(error, ENOTSUP,
1571                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1572                                           &rss->key_len,
1573                                           "RSS hash key too large");
1574         if (rss->queue_num > priv->config.ind_table_max_size)
1575                 return rte_flow_error_set(error, ENOTSUP,
1576                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1577                                           &rss->queue_num,
1578                                           "number of queues too large");
1579         if (rss->types & MLX5_RSS_HF_MASK)
1580                 return rte_flow_error_set(error, ENOTSUP,
1581                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1582                                           &rss->types,
1583                                           "some RSS protocols are not"
1584                                           " supported");
1585         if ((rss->types & (ETH_RSS_L3_SRC_ONLY | ETH_RSS_L3_DST_ONLY)) &&
1586             !(rss->types & ETH_RSS_IP))
1587                 return rte_flow_error_set(error, EINVAL,
1588                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1589                                           "L3 partial RSS requested but L3 RSS"
1590                                           " type not specified");
1591         if ((rss->types & (ETH_RSS_L4_SRC_ONLY | ETH_RSS_L4_DST_ONLY)) &&
1592             !(rss->types & (ETH_RSS_UDP | ETH_RSS_TCP)))
1593                 return rte_flow_error_set(error, EINVAL,
1594                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1595                                           "L4 partial RSS requested but L4 RSS"
1596                                           " type not specified");
1597         if (!priv->rxqs_n)
1598                 return rte_flow_error_set(error, EINVAL,
1599                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1600                                           NULL, "No Rx queues configured");
1601         if (!rss->queue_num)
1602                 return rte_flow_error_set(error, EINVAL,
1603                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1604                                           NULL, "No queues configured");
1605         for (i = 0; i != rss->queue_num; ++i) {
1606                 struct mlx5_rxq_ctrl *rxq_ctrl;
1607
1608                 if (rss->queue[i] >= priv->rxqs_n)
1609                         return rte_flow_error_set
1610                                 (error, EINVAL,
1611                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1612                                  &rss->queue[i], "queue index out of range");
1613                 if (!(*priv->rxqs)[rss->queue[i]])
1614                         return rte_flow_error_set
1615                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1616                                  &rss->queue[i], "queue is not configured");
1617                 rxq_ctrl = container_of((*priv->rxqs)[rss->queue[i]],
1618                                         struct mlx5_rxq_ctrl, rxq);
1619                 if (i == 0)
1620                         rxq_type = rxq_ctrl->type;
1621                 if (rxq_type != rxq_ctrl->type)
1622                         return rte_flow_error_set
1623                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1624                                  &rss->queue[i],
1625                                  "combining hairpin and regular RSS queues is not supported");
1626         }
1627         return 0;
1628 }
1629
1630 /*
1631  * Validate the rss action.
1632  *
1633  * @param[in] action
1634  *   Pointer to the queue action.
1635  * @param[in] action_flags
1636  *   Bit-fields that holds the actions detected until now.
1637  * @param[in] dev
1638  *   Pointer to the Ethernet device structure.
1639  * @param[in] attr
1640  *   Attributes of flow that includes this action.
1641  * @param[in] item_flags
1642  *   Items that were detected.
1643  * @param[out] error
1644  *   Pointer to error structure.
1645  *
1646  * @return
1647  *   0 on success, a negative errno value otherwise and rte_errno is set.
1648  */
1649 int
1650 mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
1651                               uint64_t action_flags,
1652                               struct rte_eth_dev *dev,
1653                               const struct rte_flow_attr *attr,
1654                               uint64_t item_flags,
1655                               struct rte_flow_error *error)
1656 {
1657         const struct rte_flow_action_rss *rss = action->conf;
1658         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1659         int ret;
1660
1661         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1662                 return rte_flow_error_set(error, EINVAL,
1663                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1664                                           "can't have 2 fate actions"
1665                                           " in same flow");
1666         ret = mlx5_validate_action_rss(dev, action, error);
1667         if (ret)
1668                 return ret;
1669         if (attr->egress)
1670                 return rte_flow_error_set(error, ENOTSUP,
1671                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1672                                           "rss action not supported for "
1673                                           "egress");
1674         if (rss->level > 1 && !tunnel)
1675                 return rte_flow_error_set(error, EINVAL,
1676                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1677                                           "inner RSS is not supported for "
1678                                           "non-tunnel flows");
1679         if ((item_flags & MLX5_FLOW_LAYER_ECPRI) &&
1680             !(item_flags & MLX5_FLOW_LAYER_INNER_L4_UDP)) {
1681                 return rte_flow_error_set(error, EINVAL,
1682                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1683                                           "RSS on eCPRI is not supported now");
1684         }
1685         if ((item_flags & MLX5_FLOW_LAYER_MPLS) &&
1686             !(item_flags &
1687               (MLX5_FLOW_LAYER_INNER_L2 | MLX5_FLOW_LAYER_INNER_L3)) &&
1688             rss->level > 1)
1689                 return rte_flow_error_set(error, EINVAL,
1690                                           RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1691                                           "MPLS inner RSS needs to specify inner L2/L3 items after MPLS in pattern");
1692         return 0;
1693 }
1694
1695 /*
1696  * Validate the default miss action.
1697  *
1698  * @param[in] action_flags
1699  *   Bit-fields that holds the actions detected until now.
1700  * @param[out] error
1701  *   Pointer to error structure.
1702  *
1703  * @return
1704  *   0 on success, a negative errno value otherwise and rte_errno is set.
1705  */
1706 int
1707 mlx5_flow_validate_action_default_miss(uint64_t action_flags,
1708                                 const struct rte_flow_attr *attr,
1709                                 struct rte_flow_error *error)
1710 {
1711         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1712                 return rte_flow_error_set(error, EINVAL,
1713                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1714                                           "can't have 2 fate actions in"
1715                                           " same flow");
1716         if (attr->egress)
1717                 return rte_flow_error_set(error, ENOTSUP,
1718                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1719                                           "default miss action not supported "
1720                                           "for egress");
1721         if (attr->group)
1722                 return rte_flow_error_set(error, ENOTSUP,
1723                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP, NULL,
1724                                           "only group 0 is supported");
1725         if (attr->transfer)
1726                 return rte_flow_error_set(error, ENOTSUP,
1727                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1728                                           NULL, "transfer is not supported");
1729         return 0;
1730 }
1731
1732 /*
1733  * Validate the count action.
1734  *
1735  * @param[in] dev
1736  *   Pointer to the Ethernet device structure.
1737  * @param[in] attr
1738  *   Attributes of flow that includes this action.
1739  * @param[out] error
1740  *   Pointer to error structure.
1741  *
1742  * @return
1743  *   0 on success, a negative errno value otherwise and rte_errno is set.
1744  */
1745 int
1746 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
1747                                 const struct rte_flow_attr *attr,
1748                                 struct rte_flow_error *error)
1749 {
1750         if (attr->egress)
1751                 return rte_flow_error_set(error, ENOTSUP,
1752                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1753                                           "count action not supported for "
1754                                           "egress");
1755         return 0;
1756 }
1757
1758 /*
1759  * Validate the ASO CT action.
1760  *
1761  * @param[in] dev
1762  *   Pointer to the Ethernet device structure.
1763  * @param[in] conntrack
1764  *   Pointer to the CT action profile.
1765  * @param[out] error
1766  *   Pointer to error structure.
1767  *
1768  * @return
1769  *   0 on success, a negative errno value otherwise and rte_errno is set.
1770  */
1771 int
1772 mlx5_validate_action_ct(struct rte_eth_dev *dev,
1773                         const struct rte_flow_action_conntrack *conntrack,
1774                         struct rte_flow_error *error)
1775 {
1776         RTE_SET_USED(dev);
1777
1778         if (conntrack->state > RTE_FLOW_CONNTRACK_STATE_TIME_WAIT)
1779                 return rte_flow_error_set(error, EINVAL,
1780                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1781                                           "Invalid CT state");
1782         if (conntrack->last_index > RTE_FLOW_CONNTRACK_FLAG_RST)
1783                 return rte_flow_error_set(error, EINVAL,
1784                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1785                                           "Invalid last TCP packet flag");
1786         return 0;
1787 }
1788
1789 /**
1790  * Verify the @p attributes will be correctly understood by the NIC and store
1791  * them in the @p flow if everything is correct.
1792  *
1793  * @param[in] dev
1794  *   Pointer to the Ethernet device structure.
1795  * @param[in] attributes
1796  *   Pointer to flow attributes
1797  * @param[out] error
1798  *   Pointer to error structure.
1799  *
1800  * @return
1801  *   0 on success, a negative errno value otherwise and rte_errno is set.
1802  */
1803 int
1804 mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
1805                               const struct rte_flow_attr *attributes,
1806                               struct rte_flow_error *error)
1807 {
1808         struct mlx5_priv *priv = dev->data->dev_private;
1809         uint32_t priority_max = priv->config.flow_prio - 1;
1810
1811         if (attributes->group)
1812                 return rte_flow_error_set(error, ENOTSUP,
1813                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1814                                           NULL, "groups is not supported");
1815         if (attributes->priority != MLX5_FLOW_LOWEST_PRIO_INDICATOR &&
1816             attributes->priority >= priority_max)
1817                 return rte_flow_error_set(error, ENOTSUP,
1818                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1819                                           NULL, "priority out of range");
1820         if (attributes->egress)
1821                 return rte_flow_error_set(error, ENOTSUP,
1822                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1823                                           "egress is not supported");
1824         if (attributes->transfer && !priv->config.dv_esw_en)
1825                 return rte_flow_error_set(error, ENOTSUP,
1826                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1827                                           NULL, "transfer is not supported");
1828         if (!attributes->ingress)
1829                 return rte_flow_error_set(error, EINVAL,
1830                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1831                                           NULL,
1832                                           "ingress attribute is mandatory");
1833         return 0;
1834 }
1835
1836 /**
1837  * Validate ICMP6 item.
1838  *
1839  * @param[in] item
1840  *   Item specification.
1841  * @param[in] item_flags
1842  *   Bit-fields that holds the items detected until now.
1843  * @param[in] ext_vlan_sup
1844  *   Whether extended VLAN features are supported or not.
1845  * @param[out] error
1846  *   Pointer to error structure.
1847  *
1848  * @return
1849  *   0 on success, a negative errno value otherwise and rte_errno is set.
1850  */
1851 int
1852 mlx5_flow_validate_item_icmp6(const struct rte_flow_item *item,
1853                                uint64_t item_flags,
1854                                uint8_t target_protocol,
1855                                struct rte_flow_error *error)
1856 {
1857         const struct rte_flow_item_icmp6 *mask = item->mask;
1858         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1859         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1860                                       MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1861         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1862                                       MLX5_FLOW_LAYER_OUTER_L4;
1863         int ret;
1864
1865         if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMPV6)
1866                 return rte_flow_error_set(error, EINVAL,
1867                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1868                                           "protocol filtering not compatible"
1869                                           " with ICMP6 layer");
1870         if (!(item_flags & l3m))
1871                 return rte_flow_error_set(error, EINVAL,
1872                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1873                                           "IPv6 is mandatory to filter on"
1874                                           " ICMP6");
1875         if (item_flags & l4m)
1876                 return rte_flow_error_set(error, EINVAL,
1877                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1878                                           "multiple L4 layers not supported");
1879         if (!mask)
1880                 mask = &rte_flow_item_icmp6_mask;
1881         ret = mlx5_flow_item_acceptable
1882                 (item, (const uint8_t *)mask,
1883                  (const uint8_t *)&rte_flow_item_icmp6_mask,
1884                  sizeof(struct rte_flow_item_icmp6),
1885                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1886         if (ret < 0)
1887                 return ret;
1888         return 0;
1889 }
1890
1891 /**
1892  * Validate ICMP item.
1893  *
1894  * @param[in] item
1895  *   Item specification.
1896  * @param[in] item_flags
1897  *   Bit-fields that holds the items detected until now.
1898  * @param[out] error
1899  *   Pointer to error structure.
1900  *
1901  * @return
1902  *   0 on success, a negative errno value otherwise and rte_errno is set.
1903  */
1904 int
1905 mlx5_flow_validate_item_icmp(const struct rte_flow_item *item,
1906                              uint64_t item_flags,
1907                              uint8_t target_protocol,
1908                              struct rte_flow_error *error)
1909 {
1910         const struct rte_flow_item_icmp *mask = item->mask;
1911         const struct rte_flow_item_icmp nic_mask = {
1912                 .hdr.icmp_type = 0xff,
1913                 .hdr.icmp_code = 0xff,
1914                 .hdr.icmp_ident = RTE_BE16(0xffff),
1915                 .hdr.icmp_seq_nb = RTE_BE16(0xffff),
1916         };
1917         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1918         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1919                                       MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1920         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1921                                       MLX5_FLOW_LAYER_OUTER_L4;
1922         int ret;
1923
1924         if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMP)
1925                 return rte_flow_error_set(error, EINVAL,
1926                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1927                                           "protocol filtering not compatible"
1928                                           " with ICMP layer");
1929         if (!(item_flags & l3m))
1930                 return rte_flow_error_set(error, EINVAL,
1931                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1932                                           "IPv4 is mandatory to filter"
1933                                           " on ICMP");
1934         if (item_flags & l4m)
1935                 return rte_flow_error_set(error, EINVAL,
1936                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1937                                           "multiple L4 layers not supported");
1938         if (!mask)
1939                 mask = &nic_mask;
1940         ret = mlx5_flow_item_acceptable
1941                 (item, (const uint8_t *)mask,
1942                  (const uint8_t *)&nic_mask,
1943                  sizeof(struct rte_flow_item_icmp),
1944                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
1945         if (ret < 0)
1946                 return ret;
1947         return 0;
1948 }
1949
1950 /**
1951  * Validate Ethernet item.
1952  *
1953  * @param[in] item
1954  *   Item specification.
1955  * @param[in] item_flags
1956  *   Bit-fields that holds the items detected until now.
1957  * @param[out] error
1958  *   Pointer to error structure.
1959  *
1960  * @return
1961  *   0 on success, a negative errno value otherwise and rte_errno is set.
1962  */
1963 int
1964 mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
1965                             uint64_t item_flags, bool ext_vlan_sup,
1966                             struct rte_flow_error *error)
1967 {
1968         const struct rte_flow_item_eth *mask = item->mask;
1969         const struct rte_flow_item_eth nic_mask = {
1970                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1971                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1972                 .type = RTE_BE16(0xffff),
1973                 .has_vlan = ext_vlan_sup ? 1 : 0,
1974         };
1975         int ret;
1976         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1977         const uint64_t ethm = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1978                                        MLX5_FLOW_LAYER_OUTER_L2;
1979
1980         if (item_flags & ethm)
1981                 return rte_flow_error_set(error, ENOTSUP,
1982                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1983                                           "multiple L2 layers not supported");
1984         if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_L3)) ||
1985             (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_L3)))
1986                 return rte_flow_error_set(error, EINVAL,
1987                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1988                                           "L2 layer should not follow "
1989                                           "L3 layers");
1990         if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN)) ||
1991             (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_VLAN)))
1992                 return rte_flow_error_set(error, EINVAL,
1993                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1994                                           "L2 layer should not follow VLAN");
1995         if (!mask)
1996                 mask = &rte_flow_item_eth_mask;
1997         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1998                                         (const uint8_t *)&nic_mask,
1999                                         sizeof(struct rte_flow_item_eth),
2000                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2001         return ret;
2002 }
2003
2004 /**
2005  * Validate VLAN item.
2006  *
2007  * @param[in] item
2008  *   Item specification.
2009  * @param[in] item_flags
2010  *   Bit-fields that holds the items detected until now.
2011  * @param[in] dev
2012  *   Ethernet device flow is being created on.
2013  * @param[out] error
2014  *   Pointer to error structure.
2015  *
2016  * @return
2017  *   0 on success, a negative errno value otherwise and rte_errno is set.
2018  */
2019 int
2020 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
2021                              uint64_t item_flags,
2022                              struct rte_eth_dev *dev,
2023                              struct rte_flow_error *error)
2024 {
2025         const struct rte_flow_item_vlan *spec = item->spec;
2026         const struct rte_flow_item_vlan *mask = item->mask;
2027         const struct rte_flow_item_vlan nic_mask = {
2028                 .tci = RTE_BE16(UINT16_MAX),
2029                 .inner_type = RTE_BE16(UINT16_MAX),
2030         };
2031         uint16_t vlan_tag = 0;
2032         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2033         int ret;
2034         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
2035                                         MLX5_FLOW_LAYER_INNER_L4) :
2036                                        (MLX5_FLOW_LAYER_OUTER_L3 |
2037                                         MLX5_FLOW_LAYER_OUTER_L4);
2038         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
2039                                         MLX5_FLOW_LAYER_OUTER_VLAN;
2040
2041         if (item_flags & vlanm)
2042                 return rte_flow_error_set(error, EINVAL,
2043                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2044                                           "multiple VLAN layers not supported");
2045         else if ((item_flags & l34m) != 0)
2046                 return rte_flow_error_set(error, EINVAL,
2047                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2048                                           "VLAN cannot follow L3/L4 layer");
2049         if (!mask)
2050                 mask = &rte_flow_item_vlan_mask;
2051         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2052                                         (const uint8_t *)&nic_mask,
2053                                         sizeof(struct rte_flow_item_vlan),
2054                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2055         if (ret)
2056                 return ret;
2057         if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
2058                 struct mlx5_priv *priv = dev->data->dev_private;
2059
2060                 if (priv->vmwa_context) {
2061                         /*
2062                          * Non-NULL context means we have a virtual machine
2063                          * and SR-IOV enabled, we have to create VLAN interface
2064                          * to make hypervisor to setup E-Switch vport
2065                          * context correctly. We avoid creating the multiple
2066                          * VLAN interfaces, so we cannot support VLAN tag mask.
2067                          */
2068                         return rte_flow_error_set(error, EINVAL,
2069                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2070                                                   item,
2071                                                   "VLAN tag mask is not"
2072                                                   " supported in virtual"
2073                                                   " environment");
2074                 }
2075         }
2076         if (spec) {
2077                 vlan_tag = spec->tci;
2078                 vlan_tag &= mask->tci;
2079         }
2080         /*
2081          * From verbs perspective an empty VLAN is equivalent
2082          * to a packet without VLAN layer.
2083          */
2084         if (!vlan_tag)
2085                 return rte_flow_error_set(error, EINVAL,
2086                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2087                                           item->spec,
2088                                           "VLAN cannot be empty");
2089         return 0;
2090 }
2091
2092 /**
2093  * Validate IPV4 item.
2094  *
2095  * @param[in] item
2096  *   Item specification.
2097  * @param[in] item_flags
2098  *   Bit-fields that holds the items detected until now.
2099  * @param[in] last_item
2100  *   Previous validated item in the pattern items.
2101  * @param[in] ether_type
2102  *   Type in the ethernet layer header (including dot1q).
2103  * @param[in] acc_mask
2104  *   Acceptable mask, if NULL default internal default mask
2105  *   will be used to check whether item fields are supported.
2106  * @param[in] range_accepted
2107  *   True if range of values is accepted for specific fields, false otherwise.
2108  * @param[out] error
2109  *   Pointer to error structure.
2110  *
2111  * @return
2112  *   0 on success, a negative errno value otherwise and rte_errno is set.
2113  */
2114 int
2115 mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
2116                              uint64_t item_flags,
2117                              uint64_t last_item,
2118                              uint16_t ether_type,
2119                              const struct rte_flow_item_ipv4 *acc_mask,
2120                              bool range_accepted,
2121                              struct rte_flow_error *error)
2122 {
2123         const struct rte_flow_item_ipv4 *mask = item->mask;
2124         const struct rte_flow_item_ipv4 *spec = item->spec;
2125         const struct rte_flow_item_ipv4 nic_mask = {
2126                 .hdr = {
2127                         .src_addr = RTE_BE32(0xffffffff),
2128                         .dst_addr = RTE_BE32(0xffffffff),
2129                         .type_of_service = 0xff,
2130                         .next_proto_id = 0xff,
2131                 },
2132         };
2133         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2134         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2135                                       MLX5_FLOW_LAYER_OUTER_L3;
2136         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2137                                       MLX5_FLOW_LAYER_OUTER_L4;
2138         int ret;
2139         uint8_t next_proto = 0xFF;
2140         const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
2141                                   MLX5_FLOW_LAYER_OUTER_VLAN |
2142                                   MLX5_FLOW_LAYER_INNER_VLAN);
2143
2144         if ((last_item & l2_vlan) && ether_type &&
2145             ether_type != RTE_ETHER_TYPE_IPV4)
2146                 return rte_flow_error_set(error, EINVAL,
2147                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2148                                           "IPv4 cannot follow L2/VLAN layer "
2149                                           "which ether type is not IPv4");
2150         if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
2151                 if (mask && spec)
2152                         next_proto = mask->hdr.next_proto_id &
2153                                      spec->hdr.next_proto_id;
2154                 if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
2155                         return rte_flow_error_set(error, EINVAL,
2156                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2157                                                   item,
2158                                                   "multiple tunnel "
2159                                                   "not supported");
2160         }
2161         if (item_flags & MLX5_FLOW_LAYER_IPV6_ENCAP)
2162                 return rte_flow_error_set(error, EINVAL,
2163                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2164                                           "wrong tunnel type - IPv6 specified "
2165                                           "but IPv4 item provided");
2166         if (item_flags & l3m)
2167                 return rte_flow_error_set(error, ENOTSUP,
2168                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2169                                           "multiple L3 layers not supported");
2170         else if (item_flags & l4m)
2171                 return rte_flow_error_set(error, EINVAL,
2172                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2173                                           "L3 cannot follow an L4 layer.");
2174         else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
2175                   !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
2176                 return rte_flow_error_set(error, EINVAL,
2177                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2178                                           "L3 cannot follow an NVGRE layer.");
2179         if (!mask)
2180                 mask = &rte_flow_item_ipv4_mask;
2181         else if (mask->hdr.next_proto_id != 0 &&
2182                  mask->hdr.next_proto_id != 0xff)
2183                 return rte_flow_error_set(error, EINVAL,
2184                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
2185                                           "partial mask is not supported"
2186                                           " for protocol");
2187         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2188                                         acc_mask ? (const uint8_t *)acc_mask
2189                                                  : (const uint8_t *)&nic_mask,
2190                                         sizeof(struct rte_flow_item_ipv4),
2191                                         range_accepted, error);
2192         if (ret < 0)
2193                 return ret;
2194         return 0;
2195 }
2196
2197 /**
2198  * Validate IPV6 item.
2199  *
2200  * @param[in] item
2201  *   Item specification.
2202  * @param[in] item_flags
2203  *   Bit-fields that holds the items detected until now.
2204  * @param[in] last_item
2205  *   Previous validated item in the pattern items.
2206  * @param[in] ether_type
2207  *   Type in the ethernet layer header (including dot1q).
2208  * @param[in] acc_mask
2209  *   Acceptable mask, if NULL default internal default mask
2210  *   will be used to check whether item fields are supported.
2211  * @param[out] error
2212  *   Pointer to error structure.
2213  *
2214  * @return
2215  *   0 on success, a negative errno value otherwise and rte_errno is set.
2216  */
2217 int
2218 mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
2219                              uint64_t item_flags,
2220                              uint64_t last_item,
2221                              uint16_t ether_type,
2222                              const struct rte_flow_item_ipv6 *acc_mask,
2223                              struct rte_flow_error *error)
2224 {
2225         const struct rte_flow_item_ipv6 *mask = item->mask;
2226         const struct rte_flow_item_ipv6 *spec = item->spec;
2227         const struct rte_flow_item_ipv6 nic_mask = {
2228                 .hdr = {
2229                         .src_addr =
2230                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
2231                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
2232                         .dst_addr =
2233                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
2234                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
2235                         .vtc_flow = RTE_BE32(0xffffffff),
2236                         .proto = 0xff,
2237                 },
2238         };
2239         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2240         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2241                                       MLX5_FLOW_LAYER_OUTER_L3;
2242         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2243                                       MLX5_FLOW_LAYER_OUTER_L4;
2244         int ret;
2245         uint8_t next_proto = 0xFF;
2246         const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
2247                                   MLX5_FLOW_LAYER_OUTER_VLAN |
2248                                   MLX5_FLOW_LAYER_INNER_VLAN);
2249
2250         if ((last_item & l2_vlan) && ether_type &&
2251             ether_type != RTE_ETHER_TYPE_IPV6)
2252                 return rte_flow_error_set(error, EINVAL,
2253                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2254                                           "IPv6 cannot follow L2/VLAN layer "
2255                                           "which ether type is not IPv6");
2256         if (mask && mask->hdr.proto == UINT8_MAX && spec)
2257                 next_proto = spec->hdr.proto;
2258         if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
2259                 if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
2260                         return rte_flow_error_set(error, EINVAL,
2261                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2262                                                   item,
2263                                                   "multiple tunnel "
2264                                                   "not supported");
2265         }
2266         if (next_proto == IPPROTO_HOPOPTS  ||
2267             next_proto == IPPROTO_ROUTING  ||
2268             next_proto == IPPROTO_FRAGMENT ||
2269             next_proto == IPPROTO_ESP      ||
2270             next_proto == IPPROTO_AH       ||
2271             next_proto == IPPROTO_DSTOPTS)
2272                 return rte_flow_error_set(error, EINVAL,
2273                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2274                                           "IPv6 proto (next header) should "
2275                                           "not be set as extension header");
2276         if (item_flags & MLX5_FLOW_LAYER_IPIP)
2277                 return rte_flow_error_set(error, EINVAL,
2278                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2279                                           "wrong tunnel type - IPv4 specified "
2280                                           "but IPv6 item provided");
2281         if (item_flags & l3m)
2282                 return rte_flow_error_set(error, ENOTSUP,
2283                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2284                                           "multiple L3 layers not supported");
2285         else if (item_flags & l4m)
2286                 return rte_flow_error_set(error, EINVAL,
2287                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2288                                           "L3 cannot follow an L4 layer.");
2289         else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
2290                   !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
2291                 return rte_flow_error_set(error, EINVAL,
2292                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2293                                           "L3 cannot follow an NVGRE layer.");
2294         if (!mask)
2295                 mask = &rte_flow_item_ipv6_mask;
2296         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
2297                                         acc_mask ? (const uint8_t *)acc_mask
2298                                                  : (const uint8_t *)&nic_mask,
2299                                         sizeof(struct rte_flow_item_ipv6),
2300                                         MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2301         if (ret < 0)
2302                 return ret;
2303         return 0;
2304 }
2305
2306 /**
2307  * Validate UDP item.
2308  *
2309  * @param[in] item
2310  *   Item specification.
2311  * @param[in] item_flags
2312  *   Bit-fields that holds the items detected until now.
2313  * @param[in] target_protocol
2314  *   The next protocol in the previous item.
2315  * @param[in] flow_mask
2316  *   mlx5 flow-specific (DV, verbs, etc.) supported header fields mask.
2317  * @param[out] error
2318  *   Pointer to error structure.
2319  *
2320  * @return
2321  *   0 on success, a negative errno value otherwise and rte_errno is set.
2322  */
2323 int
2324 mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
2325                             uint64_t item_flags,
2326                             uint8_t target_protocol,
2327                             struct rte_flow_error *error)
2328 {
2329         const struct rte_flow_item_udp *mask = item->mask;
2330         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2331         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2332                                       MLX5_FLOW_LAYER_OUTER_L3;
2333         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2334                                       MLX5_FLOW_LAYER_OUTER_L4;
2335         int ret;
2336
2337         if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
2338                 return rte_flow_error_set(error, EINVAL,
2339                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2340                                           "protocol filtering not compatible"
2341                                           " with UDP layer");
2342         if (!(item_flags & l3m))
2343                 return rte_flow_error_set(error, EINVAL,
2344                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2345                                           "L3 is mandatory to filter on L4");
2346         if (item_flags & l4m)
2347                 return rte_flow_error_set(error, EINVAL,
2348                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2349                                           "multiple L4 layers not supported");
2350         if (!mask)
2351                 mask = &rte_flow_item_udp_mask;
2352         ret = mlx5_flow_item_acceptable
2353                 (item, (const uint8_t *)mask,
2354                  (const uint8_t *)&rte_flow_item_udp_mask,
2355                  sizeof(struct rte_flow_item_udp), MLX5_ITEM_RANGE_NOT_ACCEPTED,
2356                  error);
2357         if (ret < 0)
2358                 return ret;
2359         return 0;
2360 }
2361
2362 /**
2363  * Validate TCP item.
2364  *
2365  * @param[in] item
2366  *   Item specification.
2367  * @param[in] item_flags
2368  *   Bit-fields that holds the items detected until now.
2369  * @param[in] target_protocol
2370  *   The next protocol in the previous item.
2371  * @param[out] error
2372  *   Pointer to error structure.
2373  *
2374  * @return
2375  *   0 on success, a negative errno value otherwise and rte_errno is set.
2376  */
2377 int
2378 mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
2379                             uint64_t item_flags,
2380                             uint8_t target_protocol,
2381                             const struct rte_flow_item_tcp *flow_mask,
2382                             struct rte_flow_error *error)
2383 {
2384         const struct rte_flow_item_tcp *mask = item->mask;
2385         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
2386         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
2387                                       MLX5_FLOW_LAYER_OUTER_L3;
2388         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
2389                                       MLX5_FLOW_LAYER_OUTER_L4;
2390         int ret;
2391
2392         MLX5_ASSERT(flow_mask);
2393         if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
2394                 return rte_flow_error_set(error, EINVAL,
2395                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2396                                           "protocol filtering not compatible"
2397                                           " with TCP layer");
2398         if (!(item_flags & l3m))
2399                 return rte_flow_error_set(error, EINVAL,
2400                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2401                                           "L3 is mandatory to filter on L4");
2402         if (item_flags & l4m)
2403                 return rte_flow_error_set(error, EINVAL,
2404                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2405                                           "multiple L4 layers not supported");
2406         if (!mask)
2407                 mask = &rte_flow_item_tcp_mask;
2408         ret = mlx5_flow_item_acceptable
2409                 (item, (const uint8_t *)mask,
2410                  (const uint8_t *)flow_mask,
2411                  sizeof(struct rte_flow_item_tcp), MLX5_ITEM_RANGE_NOT_ACCEPTED,
2412                  error);
2413         if (ret < 0)
2414                 return ret;
2415         return 0;
2416 }
2417
2418 /**
2419  * Validate VXLAN item.
2420  *
2421  * @param[in] dev
2422  *   Pointer to the Ethernet device structure.
2423  * @param[in] item
2424  *   Item specification.
2425  * @param[in] item_flags
2426  *   Bit-fields that holds the items detected until now.
2427  * @param[in] attr
2428  *   Flow rule attributes.
2429  * @param[out] error
2430  *   Pointer to error structure.
2431  *
2432  * @return
2433  *   0 on success, a negative errno value otherwise and rte_errno is set.
2434  */
2435 int
2436 mlx5_flow_validate_item_vxlan(struct rte_eth_dev *dev,
2437                               const struct rte_flow_item *item,
2438                               uint64_t item_flags,
2439                               const struct rte_flow_attr *attr,
2440                               struct rte_flow_error *error)
2441 {
2442         const struct rte_flow_item_vxlan *spec = item->spec;
2443         const struct rte_flow_item_vxlan *mask = item->mask;
2444         int ret;
2445         struct mlx5_priv *priv = dev->data->dev_private;
2446         union vni {
2447                 uint32_t vlan_id;
2448                 uint8_t vni[4];
2449         } id = { .vlan_id = 0, };
2450         const struct rte_flow_item_vxlan nic_mask = {
2451                 .vni = "\xff\xff\xff",
2452                 .rsvd1 = 0xff,
2453         };
2454         const struct rte_flow_item_vxlan *valid_mask;
2455
2456         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2457                 return rte_flow_error_set(error, ENOTSUP,
2458                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2459                                           "multiple tunnel layers not"
2460                                           " supported");
2461         valid_mask = &rte_flow_item_vxlan_mask;
2462         /*
2463          * Verify only UDPv4 is present as defined in
2464          * https://tools.ietf.org/html/rfc7348
2465          */
2466         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2467                 return rte_flow_error_set(error, EINVAL,
2468                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2469                                           "no outer UDP layer found");
2470         if (!mask)
2471                 mask = &rte_flow_item_vxlan_mask;
2472         /* FDB domain & NIC domain non-zero group */
2473         if ((attr->transfer || attr->group) && priv->sh->misc5_cap)
2474                 valid_mask = &nic_mask;
2475         /* Group zero in NIC domain */
2476         if (!attr->group && !attr->transfer && priv->sh->tunnel_header_0_1)
2477                 valid_mask = &nic_mask;
2478         ret = mlx5_flow_item_acceptable
2479                 (item, (const uint8_t *)mask,
2480                  (const uint8_t *)valid_mask,
2481                  sizeof(struct rte_flow_item_vxlan),
2482                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2483         if (ret < 0)
2484                 return ret;
2485         if (spec) {
2486                 memcpy(&id.vni[1], spec->vni, 3);
2487                 memcpy(&id.vni[1], mask->vni, 3);
2488         }
2489         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2490                 return rte_flow_error_set(error, ENOTSUP,
2491                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2492                                           "VXLAN tunnel must be fully defined");
2493         return 0;
2494 }
2495
2496 /**
2497  * Validate VXLAN_GPE item.
2498  *
2499  * @param[in] item
2500  *   Item specification.
2501  * @param[in] item_flags
2502  *   Bit-fields that holds the items detected until now.
2503  * @param[in] priv
2504  *   Pointer to the private data structure.
2505  * @param[in] target_protocol
2506  *   The next protocol in the previous item.
2507  * @param[out] error
2508  *   Pointer to error structure.
2509  *
2510  * @return
2511  *   0 on success, a negative errno value otherwise and rte_errno is set.
2512  */
2513 int
2514 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
2515                                   uint64_t item_flags,
2516                                   struct rte_eth_dev *dev,
2517                                   struct rte_flow_error *error)
2518 {
2519         struct mlx5_priv *priv = dev->data->dev_private;
2520         const struct rte_flow_item_vxlan_gpe *spec = item->spec;
2521         const struct rte_flow_item_vxlan_gpe *mask = item->mask;
2522         int ret;
2523         union vni {
2524                 uint32_t vlan_id;
2525                 uint8_t vni[4];
2526         } id = { .vlan_id = 0, };
2527
2528         if (!priv->config.l3_vxlan_en)
2529                 return rte_flow_error_set(error, ENOTSUP,
2530                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2531                                           "L3 VXLAN is not enabled by device"
2532                                           " parameter and/or not configured in"
2533                                           " firmware");
2534         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2535                 return rte_flow_error_set(error, ENOTSUP,
2536                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2537                                           "multiple tunnel layers not"
2538                                           " supported");
2539         /*
2540          * Verify only UDPv4 is present as defined in
2541          * https://tools.ietf.org/html/rfc7348
2542          */
2543         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2544                 return rte_flow_error_set(error, EINVAL,
2545                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2546                                           "no outer UDP layer found");
2547         if (!mask)
2548                 mask = &rte_flow_item_vxlan_gpe_mask;
2549         ret = mlx5_flow_item_acceptable
2550                 (item, (const uint8_t *)mask,
2551                  (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
2552                  sizeof(struct rte_flow_item_vxlan_gpe),
2553                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2554         if (ret < 0)
2555                 return ret;
2556         if (spec) {
2557                 if (spec->protocol)
2558                         return rte_flow_error_set(error, ENOTSUP,
2559                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2560                                                   item,
2561                                                   "VxLAN-GPE protocol"
2562                                                   " not supported");
2563                 memcpy(&id.vni[1], spec->vni, 3);
2564                 memcpy(&id.vni[1], mask->vni, 3);
2565         }
2566         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2567                 return rte_flow_error_set(error, ENOTSUP,
2568                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2569                                           "VXLAN-GPE tunnel must be fully"
2570                                           " defined");
2571         return 0;
2572 }
2573 /**
2574  * Validate GRE Key item.
2575  *
2576  * @param[in] item
2577  *   Item specification.
2578  * @param[in] item_flags
2579  *   Bit flags to mark detected items.
2580  * @param[in] gre_item
2581  *   Pointer to gre_item
2582  * @param[out] error
2583  *   Pointer to error structure.
2584  *
2585  * @return
2586  *   0 on success, a negative errno value otherwise and rte_errno is set.
2587  */
2588 int
2589 mlx5_flow_validate_item_gre_key(const struct rte_flow_item *item,
2590                                 uint64_t item_flags,
2591                                 const struct rte_flow_item *gre_item,
2592                                 struct rte_flow_error *error)
2593 {
2594         const rte_be32_t *mask = item->mask;
2595         int ret = 0;
2596         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
2597         const struct rte_flow_item_gre *gre_spec;
2598         const struct rte_flow_item_gre *gre_mask;
2599
2600         if (item_flags & MLX5_FLOW_LAYER_GRE_KEY)
2601                 return rte_flow_error_set(error, ENOTSUP,
2602                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2603                                           "Multiple GRE key not support");
2604         if (!(item_flags & MLX5_FLOW_LAYER_GRE))
2605                 return rte_flow_error_set(error, ENOTSUP,
2606                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2607                                           "No preceding GRE header");
2608         if (item_flags & MLX5_FLOW_LAYER_INNER)
2609                 return rte_flow_error_set(error, ENOTSUP,
2610                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2611                                           "GRE key following a wrong item");
2612         gre_mask = gre_item->mask;
2613         if (!gre_mask)
2614                 gre_mask = &rte_flow_item_gre_mask;
2615         gre_spec = gre_item->spec;
2616         if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x2000)) &&
2617                          !(gre_spec->c_rsvd0_ver & RTE_BE16(0x2000)))
2618                 return rte_flow_error_set(error, EINVAL,
2619                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2620                                           "Key bit must be on");
2621
2622         if (!mask)
2623                 mask = &gre_key_default_mask;
2624         ret = mlx5_flow_item_acceptable
2625                 (item, (const uint8_t *)mask,
2626                  (const uint8_t *)&gre_key_default_mask,
2627                  sizeof(rte_be32_t), MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2628         return ret;
2629 }
2630
2631 /**
2632  * Validate GRE item.
2633  *
2634  * @param[in] item
2635  *   Item specification.
2636  * @param[in] item_flags
2637  *   Bit flags to mark detected items.
2638  * @param[in] target_protocol
2639  *   The next protocol in the previous item.
2640  * @param[out] error
2641  *   Pointer to error structure.
2642  *
2643  * @return
2644  *   0 on success, a negative errno value otherwise and rte_errno is set.
2645  */
2646 int
2647 mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
2648                             uint64_t item_flags,
2649                             uint8_t target_protocol,
2650                             struct rte_flow_error *error)
2651 {
2652         const struct rte_flow_item_gre *spec __rte_unused = item->spec;
2653         const struct rte_flow_item_gre *mask = item->mask;
2654         int ret;
2655         const struct rte_flow_item_gre nic_mask = {
2656                 .c_rsvd0_ver = RTE_BE16(0xB000),
2657                 .protocol = RTE_BE16(UINT16_MAX),
2658         };
2659
2660         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
2661                 return rte_flow_error_set(error, EINVAL,
2662                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2663                                           "protocol filtering not compatible"
2664                                           " with this GRE layer");
2665         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2666                 return rte_flow_error_set(error, ENOTSUP,
2667                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2668                                           "multiple tunnel layers not"
2669                                           " supported");
2670         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
2671                 return rte_flow_error_set(error, ENOTSUP,
2672                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2673                                           "L3 Layer is missing");
2674         if (!mask)
2675                 mask = &rte_flow_item_gre_mask;
2676         ret = mlx5_flow_item_acceptable
2677                 (item, (const uint8_t *)mask,
2678                  (const uint8_t *)&nic_mask,
2679                  sizeof(struct rte_flow_item_gre), MLX5_ITEM_RANGE_NOT_ACCEPTED,
2680                  error);
2681         if (ret < 0)
2682                 return ret;
2683 #ifndef HAVE_MLX5DV_DR
2684 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
2685         if (spec && (spec->protocol & mask->protocol))
2686                 return rte_flow_error_set(error, ENOTSUP,
2687                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2688                                           "without MPLS support the"
2689                                           " specification cannot be used for"
2690                                           " filtering");
2691 #endif
2692 #endif
2693         return 0;
2694 }
2695
2696 /**
2697  * Validate Geneve item.
2698  *
2699  * @param[in] item
2700  *   Item specification.
2701  * @param[in] itemFlags
2702  *   Bit-fields that holds the items detected until now.
2703  * @param[in] enPriv
2704  *   Pointer to the private data structure.
2705  * @param[out] error
2706  *   Pointer to error structure.
2707  *
2708  * @return
2709  *   0 on success, a negative errno value otherwise and rte_errno is set.
2710  */
2711
2712 int
2713 mlx5_flow_validate_item_geneve(const struct rte_flow_item *item,
2714                                uint64_t item_flags,
2715                                struct rte_eth_dev *dev,
2716                                struct rte_flow_error *error)
2717 {
2718         struct mlx5_priv *priv = dev->data->dev_private;
2719         const struct rte_flow_item_geneve *spec = item->spec;
2720         const struct rte_flow_item_geneve *mask = item->mask;
2721         int ret;
2722         uint16_t gbhdr;
2723         uint8_t opt_len = priv->config.hca_attr.geneve_max_opt_len ?
2724                           MLX5_GENEVE_OPT_LEN_1 : MLX5_GENEVE_OPT_LEN_0;
2725         const struct rte_flow_item_geneve nic_mask = {
2726                 .ver_opt_len_o_c_rsvd0 = RTE_BE16(0x3f80),
2727                 .vni = "\xff\xff\xff",
2728                 .protocol = RTE_BE16(UINT16_MAX),
2729         };
2730
2731         if (!priv->config.hca_attr.tunnel_stateless_geneve_rx)
2732                 return rte_flow_error_set(error, ENOTSUP,
2733                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2734                                           "L3 Geneve is not enabled by device"
2735                                           " parameter and/or not configured in"
2736                                           " firmware");
2737         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2738                 return rte_flow_error_set(error, ENOTSUP,
2739                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2740                                           "multiple tunnel layers not"
2741                                           " supported");
2742         /*
2743          * Verify only UDPv4 is present as defined in
2744          * https://tools.ietf.org/html/rfc7348
2745          */
2746         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2747                 return rte_flow_error_set(error, EINVAL,
2748                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2749                                           "no outer UDP layer found");
2750         if (!mask)
2751                 mask = &rte_flow_item_geneve_mask;
2752         ret = mlx5_flow_item_acceptable
2753                                   (item, (const uint8_t *)mask,
2754                                    (const uint8_t *)&nic_mask,
2755                                    sizeof(struct rte_flow_item_geneve),
2756                                    MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2757         if (ret)
2758                 return ret;
2759         if (spec) {
2760                 gbhdr = rte_be_to_cpu_16(spec->ver_opt_len_o_c_rsvd0);
2761                 if (MLX5_GENEVE_VER_VAL(gbhdr) ||
2762                      MLX5_GENEVE_CRITO_VAL(gbhdr) ||
2763                      MLX5_GENEVE_RSVD_VAL(gbhdr) || spec->rsvd1)
2764                         return rte_flow_error_set(error, ENOTSUP,
2765                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2766                                                   item,
2767                                                   "Geneve protocol unsupported"
2768                                                   " fields are being used");
2769                 if (MLX5_GENEVE_OPTLEN_VAL(gbhdr) > opt_len)
2770                         return rte_flow_error_set
2771                                         (error, ENOTSUP,
2772                                          RTE_FLOW_ERROR_TYPE_ITEM,
2773                                          item,
2774                                          "Unsupported Geneve options length");
2775         }
2776         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2777                 return rte_flow_error_set
2778                                     (error, ENOTSUP,
2779                                      RTE_FLOW_ERROR_TYPE_ITEM, item,
2780                                      "Geneve tunnel must be fully defined");
2781         return 0;
2782 }
2783
2784 /**
2785  * Validate Geneve TLV option item.
2786  *
2787  * @param[in] item
2788  *   Item specification.
2789  * @param[in] last_item
2790  *   Previous validated item in the pattern items.
2791  * @param[in] geneve_item
2792  *   Previous GENEVE item specification.
2793  * @param[in] dev
2794  *   Pointer to the rte_eth_dev structure.
2795  * @param[out] error
2796  *   Pointer to error structure.
2797  *
2798  * @return
2799  *   0 on success, a negative errno value otherwise and rte_errno is set.
2800  */
2801 int
2802 mlx5_flow_validate_item_geneve_opt(const struct rte_flow_item *item,
2803                                    uint64_t last_item,
2804                                    const struct rte_flow_item *geneve_item,
2805                                    struct rte_eth_dev *dev,
2806                                    struct rte_flow_error *error)
2807 {
2808         struct mlx5_priv *priv = dev->data->dev_private;
2809         struct mlx5_dev_ctx_shared *sh = priv->sh;
2810         struct mlx5_geneve_tlv_option_resource *geneve_opt_resource;
2811         struct mlx5_hca_attr *hca_attr = &priv->config.hca_attr;
2812         uint8_t data_max_supported =
2813                         hca_attr->max_geneve_tlv_option_data_len * 4;
2814         struct mlx5_dev_config *config = &priv->config;
2815         const struct rte_flow_item_geneve *geneve_spec;
2816         const struct rte_flow_item_geneve *geneve_mask;
2817         const struct rte_flow_item_geneve_opt *spec = item->spec;
2818         const struct rte_flow_item_geneve_opt *mask = item->mask;
2819         unsigned int i;
2820         unsigned int data_len;
2821         uint8_t tlv_option_len;
2822         uint16_t optlen_m, optlen_v;
2823         const struct rte_flow_item_geneve_opt full_mask = {
2824                 .option_class = RTE_BE16(0xffff),
2825                 .option_type = 0xff,
2826                 .option_len = 0x1f,
2827         };
2828
2829         if (!mask)
2830                 mask = &rte_flow_item_geneve_opt_mask;
2831         if (!spec)
2832                 return rte_flow_error_set
2833                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2834                         "Geneve TLV opt class/type/length must be specified");
2835         if ((uint32_t)spec->option_len > MLX5_GENEVE_OPTLEN_MASK)
2836                 return rte_flow_error_set
2837                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2838                         "Geneve TLV opt length exceeeds the limit (31)");
2839         /* Check if class type and length masks are full. */
2840         if (full_mask.option_class != mask->option_class ||
2841             full_mask.option_type != mask->option_type ||
2842             full_mask.option_len != (mask->option_len & full_mask.option_len))
2843                 return rte_flow_error_set
2844                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2845                         "Geneve TLV opt class/type/length masks must be full");
2846         /* Check if length is supported */
2847         if ((uint32_t)spec->option_len >
2848                         config->hca_attr.max_geneve_tlv_option_data_len)
2849                 return rte_flow_error_set
2850                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2851                         "Geneve TLV opt length not supported");
2852         if (config->hca_attr.max_geneve_tlv_options > 1)
2853                 DRV_LOG(DEBUG,
2854                         "max_geneve_tlv_options supports more than 1 option");
2855         /* Check GENEVE item preceding. */
2856         if (!geneve_item || !(last_item & MLX5_FLOW_LAYER_GENEVE))
2857                 return rte_flow_error_set
2858                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2859                         "Geneve opt item must be preceded with Geneve item");
2860         geneve_spec = geneve_item->spec;
2861         geneve_mask = geneve_item->mask ? geneve_item->mask :
2862                                           &rte_flow_item_geneve_mask;
2863         /* Check if GENEVE TLV option size doesn't exceed option length */
2864         if (geneve_spec && (geneve_mask->ver_opt_len_o_c_rsvd0 ||
2865                             geneve_spec->ver_opt_len_o_c_rsvd0)) {
2866                 tlv_option_len = spec->option_len & mask->option_len;
2867                 optlen_v = rte_be_to_cpu_16(geneve_spec->ver_opt_len_o_c_rsvd0);
2868                 optlen_v = MLX5_GENEVE_OPTLEN_VAL(optlen_v);
2869                 optlen_m = rte_be_to_cpu_16(geneve_mask->ver_opt_len_o_c_rsvd0);
2870                 optlen_m = MLX5_GENEVE_OPTLEN_VAL(optlen_m);
2871                 if ((optlen_v & optlen_m) <= tlv_option_len)
2872                         return rte_flow_error_set
2873                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2874                                  "GENEVE TLV option length exceeds optlen");
2875         }
2876         /* Check if length is 0 or data is 0. */
2877         if (spec->data == NULL || spec->option_len == 0)
2878                 return rte_flow_error_set
2879                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2880                         "Geneve TLV opt with zero data/length not supported");
2881         /* Check not all data & mask are 0. */
2882         data_len = spec->option_len * 4;
2883         if (mask->data == NULL) {
2884                 for (i = 0; i < data_len; i++)
2885                         if (spec->data[i])
2886                                 break;
2887                 if (i == data_len)
2888                         return rte_flow_error_set(error, ENOTSUP,
2889                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
2890                                 "Can't match on Geneve option data 0");
2891         } else {
2892                 for (i = 0; i < data_len; i++)
2893                         if (spec->data[i] & mask->data[i])
2894                                 break;
2895                 if (i == data_len)
2896                         return rte_flow_error_set(error, ENOTSUP,
2897                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
2898                                 "Can't match on Geneve option data and mask 0");
2899                 /* Check data mask supported. */
2900                 for (i = data_max_supported; i < data_len ; i++)
2901                         if (mask->data[i])
2902                                 return rte_flow_error_set(error, ENOTSUP,
2903                                         RTE_FLOW_ERROR_TYPE_ITEM, item,
2904                                         "Data mask is of unsupported size");
2905         }
2906         /* Check GENEVE option is supported in NIC. */
2907         if (!config->hca_attr.geneve_tlv_opt)
2908                 return rte_flow_error_set
2909                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM, item,
2910                         "Geneve TLV opt not supported");
2911         /* Check if we already have geneve option with different type/class. */
2912         rte_spinlock_lock(&sh->geneve_tlv_opt_sl);
2913         geneve_opt_resource = sh->geneve_tlv_option_resource;
2914         if (geneve_opt_resource != NULL)
2915                 if (geneve_opt_resource->option_class != spec->option_class ||
2916                     geneve_opt_resource->option_type != spec->option_type ||
2917                     geneve_opt_resource->length != spec->option_len) {
2918                         rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
2919                         return rte_flow_error_set(error, ENOTSUP,
2920                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
2921                                 "Only one Geneve TLV option supported");
2922                 }
2923         rte_spinlock_unlock(&sh->geneve_tlv_opt_sl);
2924         return 0;
2925 }
2926
2927 /**
2928  * Validate MPLS item.
2929  *
2930  * @param[in] dev
2931  *   Pointer to the rte_eth_dev structure.
2932  * @param[in] item
2933  *   Item specification.
2934  * @param[in] item_flags
2935  *   Bit-fields that holds the items detected until now.
2936  * @param[in] prev_layer
2937  *   The protocol layer indicated in previous item.
2938  * @param[out] error
2939  *   Pointer to error structure.
2940  *
2941  * @return
2942  *   0 on success, a negative errno value otherwise and rte_errno is set.
2943  */
2944 int
2945 mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev __rte_unused,
2946                              const struct rte_flow_item *item __rte_unused,
2947                              uint64_t item_flags __rte_unused,
2948                              uint64_t prev_layer __rte_unused,
2949                              struct rte_flow_error *error)
2950 {
2951 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
2952         const struct rte_flow_item_mpls *mask = item->mask;
2953         struct mlx5_priv *priv = dev->data->dev_private;
2954         int ret;
2955
2956         if (!priv->config.mpls_en)
2957                 return rte_flow_error_set(error, ENOTSUP,
2958                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2959                                           "MPLS not supported or"
2960                                           " disabled in firmware"
2961                                           " configuration.");
2962         /* MPLS over UDP, GRE is allowed */
2963         if (!(prev_layer & (MLX5_FLOW_LAYER_OUTER_L4_UDP |
2964                             MLX5_FLOW_LAYER_GRE |
2965                             MLX5_FLOW_LAYER_GRE_KEY)))
2966                 return rte_flow_error_set(error, EINVAL,
2967                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2968                                           "protocol filtering not compatible"
2969                                           " with MPLS layer");
2970         /* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
2971         if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
2972             !(item_flags & MLX5_FLOW_LAYER_GRE))
2973                 return rte_flow_error_set(error, ENOTSUP,
2974                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2975                                           "multiple tunnel layers not"
2976                                           " supported");
2977         if (!mask)
2978                 mask = &rte_flow_item_mpls_mask;
2979         ret = mlx5_flow_item_acceptable
2980                 (item, (const uint8_t *)mask,
2981                  (const uint8_t *)&rte_flow_item_mpls_mask,
2982                  sizeof(struct rte_flow_item_mpls),
2983                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
2984         if (ret < 0)
2985                 return ret;
2986         return 0;
2987 #else
2988         return rte_flow_error_set(error, ENOTSUP,
2989                                   RTE_FLOW_ERROR_TYPE_ITEM, item,
2990                                   "MPLS is not supported by Verbs, please"
2991                                   " update.");
2992 #endif
2993 }
2994
2995 /**
2996  * Validate NVGRE item.
2997  *
2998  * @param[in] item
2999  *   Item specification.
3000  * @param[in] item_flags
3001  *   Bit flags to mark detected items.
3002  * @param[in] target_protocol
3003  *   The next protocol in the previous item.
3004  * @param[out] error
3005  *   Pointer to error structure.
3006  *
3007  * @return
3008  *   0 on success, a negative errno value otherwise and rte_errno is set.
3009  */
3010 int
3011 mlx5_flow_validate_item_nvgre(const struct rte_flow_item *item,
3012                               uint64_t item_flags,
3013                               uint8_t target_protocol,
3014                               struct rte_flow_error *error)
3015 {
3016         const struct rte_flow_item_nvgre *mask = item->mask;
3017         int ret;
3018
3019         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
3020                 return rte_flow_error_set(error, EINVAL,
3021                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3022                                           "protocol filtering not compatible"
3023                                           " with this GRE layer");
3024         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3025                 return rte_flow_error_set(error, ENOTSUP,
3026                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3027                                           "multiple tunnel layers not"
3028                                           " supported");
3029         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
3030                 return rte_flow_error_set(error, ENOTSUP,
3031                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3032                                           "L3 Layer is missing");
3033         if (!mask)
3034                 mask = &rte_flow_item_nvgre_mask;
3035         ret = mlx5_flow_item_acceptable
3036                 (item, (const uint8_t *)mask,
3037                  (const uint8_t *)&rte_flow_item_nvgre_mask,
3038                  sizeof(struct rte_flow_item_nvgre),
3039                  MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3040         if (ret < 0)
3041                 return ret;
3042         return 0;
3043 }
3044
3045 /**
3046  * Validate eCPRI item.
3047  *
3048  * @param[in] item
3049  *   Item specification.
3050  * @param[in] item_flags
3051  *   Bit-fields that holds the items detected until now.
3052  * @param[in] last_item
3053  *   Previous validated item in the pattern items.
3054  * @param[in] ether_type
3055  *   Type in the ethernet layer header (including dot1q).
3056  * @param[in] acc_mask
3057  *   Acceptable mask, if NULL default internal default mask
3058  *   will be used to check whether item fields are supported.
3059  * @param[out] error
3060  *   Pointer to error structure.
3061  *
3062  * @return
3063  *   0 on success, a negative errno value otherwise and rte_errno is set.
3064  */
3065 int
3066 mlx5_flow_validate_item_ecpri(const struct rte_flow_item *item,
3067                               uint64_t item_flags,
3068                               uint64_t last_item,
3069                               uint16_t ether_type,
3070                               const struct rte_flow_item_ecpri *acc_mask,
3071                               struct rte_flow_error *error)
3072 {
3073         const struct rte_flow_item_ecpri *mask = item->mask;
3074         const struct rte_flow_item_ecpri nic_mask = {
3075                 .hdr = {
3076                         .common = {
3077                                 .u32 =
3078                                 RTE_BE32(((const struct rte_ecpri_common_hdr) {
3079                                         .type = 0xFF,
3080                                         }).u32),
3081                         },
3082                         .dummy[0] = 0xFFFFFFFF,
3083                 },
3084         };
3085         const uint64_t outer_l2_vlan = (MLX5_FLOW_LAYER_OUTER_L2 |
3086                                         MLX5_FLOW_LAYER_OUTER_VLAN);
3087         struct rte_flow_item_ecpri mask_lo;
3088
3089         if (!(last_item & outer_l2_vlan) &&
3090             last_item != MLX5_FLOW_LAYER_OUTER_L4_UDP)
3091                 return rte_flow_error_set(error, EINVAL,
3092                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3093                                           "eCPRI can only follow L2/VLAN layer or UDP layer");
3094         if ((last_item & outer_l2_vlan) && ether_type &&
3095             ether_type != RTE_ETHER_TYPE_ECPRI)
3096                 return rte_flow_error_set(error, EINVAL,
3097                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3098                                           "eCPRI cannot follow L2/VLAN layer which ether type is not 0xAEFE");
3099         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3100                 return rte_flow_error_set(error, EINVAL,
3101                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3102                                           "eCPRI with tunnel is not supported right now");
3103         if (item_flags & MLX5_FLOW_LAYER_OUTER_L3)
3104                 return rte_flow_error_set(error, ENOTSUP,
3105                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3106                                           "multiple L3 layers not supported");
3107         else if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP)
3108                 return rte_flow_error_set(error, EINVAL,
3109                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3110                                           "eCPRI cannot coexist with a TCP layer");
3111         /* In specification, eCPRI could be over UDP layer. */
3112         else if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP)
3113                 return rte_flow_error_set(error, EINVAL,
3114                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
3115                                           "eCPRI over UDP layer is not yet supported right now");
3116         /* Mask for type field in common header could be zero. */
3117         if (!mask)
3118                 mask = &rte_flow_item_ecpri_mask;
3119         mask_lo.hdr.common.u32 = rte_be_to_cpu_32(mask->hdr.common.u32);
3120         /* Input mask is in big-endian format. */
3121         if (mask_lo.hdr.common.type != 0 && mask_lo.hdr.common.type != 0xff)
3122                 return rte_flow_error_set(error, EINVAL,
3123                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
3124                                           "partial mask is not supported for protocol");
3125         else if (mask_lo.hdr.common.type == 0 && mask->hdr.dummy[0] != 0)
3126                 return rte_flow_error_set(error, EINVAL,
3127                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
3128                                           "message header mask must be after a type mask");
3129         return mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
3130                                          acc_mask ? (const uint8_t *)acc_mask
3131                                                   : (const uint8_t *)&nic_mask,
3132                                          sizeof(struct rte_flow_item_ecpri),
3133                                          MLX5_ITEM_RANGE_NOT_ACCEPTED, error);
3134 }
3135
3136 static int
3137 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
3138                    const struct rte_flow_attr *attr __rte_unused,
3139                    const struct rte_flow_item items[] __rte_unused,
3140                    const struct rte_flow_action actions[] __rte_unused,
3141                    bool external __rte_unused,
3142                    int hairpin __rte_unused,
3143                    struct rte_flow_error *error)
3144 {
3145         return rte_flow_error_set(error, ENOTSUP,
3146                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
3147 }
3148
3149 static struct mlx5_flow *
3150 flow_null_prepare(struct rte_eth_dev *dev __rte_unused,
3151                   const struct rte_flow_attr *attr __rte_unused,
3152                   const struct rte_flow_item items[] __rte_unused,
3153                   const struct rte_flow_action actions[] __rte_unused,
3154                   struct rte_flow_error *error)
3155 {
3156         rte_flow_error_set(error, ENOTSUP,
3157                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
3158         return NULL;
3159 }
3160
3161 static int
3162 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
3163                     struct mlx5_flow *dev_flow __rte_unused,
3164                     const struct rte_flow_attr *attr __rte_unused,
3165                     const struct rte_flow_item items[] __rte_unused,
3166                     const struct rte_flow_action actions[] __rte_unused,
3167                     struct rte_flow_error *error)
3168 {
3169         return rte_flow_error_set(error, ENOTSUP,
3170                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
3171 }
3172
3173 static int
3174 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
3175                 struct rte_flow *flow __rte_unused,
3176                 struct rte_flow_error *error)
3177 {
3178         return rte_flow_error_set(error, ENOTSUP,
3179                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
3180 }
3181
3182 static void
3183 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
3184                  struct rte_flow *flow __rte_unused)
3185 {
3186 }
3187
3188 static void
3189 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
3190                   struct rte_flow *flow __rte_unused)
3191 {
3192 }
3193
3194 static int
3195 flow_null_query(struct rte_eth_dev *dev __rte_unused,
3196                 struct rte_flow *flow __rte_unused,
3197                 const struct rte_flow_action *actions __rte_unused,
3198                 void *data __rte_unused,
3199                 struct rte_flow_error *error)
3200 {
3201         return rte_flow_error_set(error, ENOTSUP,
3202                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
3203 }
3204
3205 static int
3206 flow_null_sync_domain(struct rte_eth_dev *dev __rte_unused,
3207                       uint32_t domains __rte_unused,
3208                       uint32_t flags __rte_unused)
3209 {
3210         return 0;
3211 }
3212
3213 /* Void driver to protect from null pointer reference. */
3214 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
3215         .validate = flow_null_validate,
3216         .prepare = flow_null_prepare,
3217         .translate = flow_null_translate,
3218         .apply = flow_null_apply,
3219         .remove = flow_null_remove,
3220         .destroy = flow_null_destroy,
3221         .query = flow_null_query,
3222         .sync_domain = flow_null_sync_domain,
3223 };
3224
3225 /**
3226  * Select flow driver type according to flow attributes and device
3227  * configuration.
3228  *
3229  * @param[in] dev
3230  *   Pointer to the dev structure.
3231  * @param[in] attr
3232  *   Pointer to the flow attributes.
3233  *
3234  * @return
3235  *   flow driver type, MLX5_FLOW_TYPE_MAX otherwise.
3236  */
3237 static enum mlx5_flow_drv_type
3238 flow_get_drv_type(struct rte_eth_dev *dev, const struct rte_flow_attr *attr)
3239 {
3240         struct mlx5_priv *priv = dev->data->dev_private;
3241         /* The OS can determine first a specific flow type (DV, VERBS) */
3242         enum mlx5_flow_drv_type type = mlx5_flow_os_get_type();
3243
3244         if (type != MLX5_FLOW_TYPE_MAX)
3245                 return type;
3246         /* If no OS specific type - continue with DV/VERBS selection */
3247         if (attr->transfer && priv->config.dv_esw_en)
3248                 type = MLX5_FLOW_TYPE_DV;
3249         if (!attr->transfer)
3250                 type = priv->config.dv_flow_en ? MLX5_FLOW_TYPE_DV :
3251                                                  MLX5_FLOW_TYPE_VERBS;
3252         return type;
3253 }
3254
3255 #define flow_get_drv_ops(type) flow_drv_ops[type]
3256
3257 /**
3258  * Flow driver validation API. This abstracts calling driver specific functions.
3259  * The type of flow driver is determined according to flow attributes.
3260  *
3261  * @param[in] dev
3262  *   Pointer to the dev structure.
3263  * @param[in] attr
3264  *   Pointer to the flow attributes.
3265  * @param[in] items
3266  *   Pointer to the list of items.
3267  * @param[in] actions
3268  *   Pointer to the list of actions.
3269  * @param[in] external
3270  *   This flow rule is created by request external to PMD.
3271  * @param[in] hairpin
3272  *   Number of hairpin TX actions, 0 means classic flow.
3273  * @param[out] error
3274  *   Pointer to the error structure.
3275  *
3276  * @return
3277  *   0 on success, a negative errno value otherwise and rte_errno is set.
3278  */
3279 static inline int
3280 flow_drv_validate(struct rte_eth_dev *dev,
3281                   const struct rte_flow_attr *attr,
3282                   const struct rte_flow_item items[],
3283                   const struct rte_flow_action actions[],
3284                   bool external, int hairpin, struct rte_flow_error *error)
3285 {
3286         const struct mlx5_flow_driver_ops *fops;
3287         enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
3288
3289         fops = flow_get_drv_ops(type);
3290         return fops->validate(dev, attr, items, actions, external,
3291                               hairpin, error);
3292 }
3293
3294 /**
3295  * Flow driver preparation API. This abstracts calling driver specific
3296  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
3297  * calculates the size of memory required for device flow, allocates the memory,
3298  * initializes the device flow and returns the pointer.
3299  *
3300  * @note
3301  *   This function initializes device flow structure such as dv or verbs in
3302  *   struct mlx5_flow. However, it is caller's responsibility to initialize the
3303  *   rest. For example, adding returning device flow to flow->dev_flow list and
3304  *   setting backward reference to the flow should be done out of this function.
3305  *   layers field is not filled either.
3306  *
3307  * @param[in] dev
3308  *   Pointer to the dev structure.
3309  * @param[in] attr
3310  *   Pointer to the flow attributes.
3311  * @param[in] items
3312  *   Pointer to the list of items.
3313  * @param[in] actions
3314  *   Pointer to the list of actions.
3315  * @param[in] flow_idx
3316  *   This memory pool index to the flow.
3317  * @param[out] error
3318  *   Pointer to the error structure.
3319  *
3320  * @return
3321  *   Pointer to device flow on success, otherwise NULL and rte_errno is set.
3322  */
3323 static inline struct mlx5_flow *
3324 flow_drv_prepare(struct rte_eth_dev *dev,
3325                  const struct rte_flow *flow,
3326                  const struct rte_flow_attr *attr,
3327                  const struct rte_flow_item items[],
3328                  const struct rte_flow_action actions[],
3329                  uint32_t flow_idx,
3330                  struct rte_flow_error *error)
3331 {
3332         const struct mlx5_flow_driver_ops *fops;
3333         enum mlx5_flow_drv_type type = flow->drv_type;
3334         struct mlx5_flow *mlx5_flow = NULL;
3335
3336         MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3337         fops = flow_get_drv_ops(type);
3338         mlx5_flow = fops->prepare(dev, attr, items, actions, error);
3339         if (mlx5_flow)
3340                 mlx5_flow->flow_idx = flow_idx;
3341         return mlx5_flow;
3342 }
3343
3344 /**
3345  * Flow driver translation API. This abstracts calling driver specific
3346  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
3347  * translates a generic flow into a driver flow. flow_drv_prepare() must
3348  * precede.
3349  *
3350  * @note
3351  *   dev_flow->layers could be filled as a result of parsing during translation
3352  *   if needed by flow_drv_apply(). dev_flow->flow->actions can also be filled
3353  *   if necessary. As a flow can have multiple dev_flows by RSS flow expansion,
3354  *   flow->actions could be overwritten even though all the expanded dev_flows
3355  *   have the same actions.
3356  *
3357  * @param[in] dev
3358  *   Pointer to the rte dev structure.
3359  * @param[in, out] dev_flow
3360  *   Pointer to the mlx5 flow.
3361  * @param[in] attr
3362  *   Pointer to the flow attributes.
3363  * @param[in] items
3364  *   Pointer to the list of items.
3365  * @param[in] actions
3366  *   Pointer to the list of actions.
3367  * @param[out] error
3368  *   Pointer to the error structure.
3369  *
3370  * @return
3371  *   0 on success, a negative errno value otherwise and rte_errno is set.
3372  */
3373 static inline int
3374 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
3375                    const struct rte_flow_attr *attr,
3376                    const struct rte_flow_item items[],
3377                    const struct rte_flow_action actions[],
3378                    struct rte_flow_error *error)
3379 {
3380         const struct mlx5_flow_driver_ops *fops;
3381         enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
3382
3383         MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3384         fops = flow_get_drv_ops(type);
3385         return fops->translate(dev, dev_flow, attr, items, actions, error);
3386 }
3387
3388 /**
3389  * Flow driver apply API. This abstracts calling driver specific functions.
3390  * Parent flow (rte_flow) should have driver type (drv_type). It applies
3391  * translated driver flows on to device. flow_drv_translate() must precede.
3392  *
3393  * @param[in] dev
3394  *   Pointer to Ethernet device structure.
3395  * @param[in, out] flow
3396  *   Pointer to flow structure.
3397  * @param[out] error
3398  *   Pointer to error structure.
3399  *
3400  * @return
3401  *   0 on success, a negative errno value otherwise and rte_errno is set.
3402  */
3403 static inline int
3404 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
3405                struct rte_flow_error *error)
3406 {
3407         const struct mlx5_flow_driver_ops *fops;
3408         enum mlx5_flow_drv_type type = flow->drv_type;
3409
3410         MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3411         fops = flow_get_drv_ops(type);
3412         return fops->apply(dev, flow, error);
3413 }
3414
3415 /**
3416  * Flow driver destroy API. This abstracts calling driver specific functions.
3417  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
3418  * on device and releases resources of the flow.
3419  *
3420  * @param[in] dev
3421  *   Pointer to Ethernet device.
3422  * @param[in, out] flow
3423  *   Pointer to flow structure.
3424  */
3425 static inline void
3426 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
3427 {
3428         const struct mlx5_flow_driver_ops *fops;
3429         enum mlx5_flow_drv_type type = flow->drv_type;
3430
3431         MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3432         fops = flow_get_drv_ops(type);
3433         fops->destroy(dev, flow);
3434 }
3435
3436 /**
3437  * Flow driver find RSS policy tbl API. This abstracts calling driver
3438  * specific functions. Parent flow (rte_flow) should have driver
3439  * type (drv_type). It will find the RSS policy table that has the rss_desc.
3440  *
3441  * @param[in] dev
3442  *   Pointer to Ethernet device.
3443  * @param[in, out] flow
3444  *   Pointer to flow structure.
3445  * @param[in] policy
3446  *   Pointer to meter policy table.
3447  * @param[in] rss_desc
3448  *   Pointer to rss_desc
3449  */
3450 static struct mlx5_flow_meter_sub_policy *
3451 flow_drv_meter_sub_policy_rss_prepare(struct rte_eth_dev *dev,
3452                 struct rte_flow *flow,
3453                 struct mlx5_flow_meter_policy *policy,
3454                 struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS])
3455 {
3456         const struct mlx5_flow_driver_ops *fops;
3457         enum mlx5_flow_drv_type type = flow->drv_type;
3458
3459         MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3460         fops = flow_get_drv_ops(type);
3461         return fops->meter_sub_policy_rss_prepare(dev, policy, rss_desc);
3462 }
3463
3464 /**
3465  * Flow driver color tag rule API. This abstracts calling driver
3466  * specific functions. Parent flow (rte_flow) should have driver
3467  * type (drv_type). It will create the color tag rules in hierarchy meter.
3468  *
3469  * @param[in] dev
3470  *   Pointer to Ethernet device.
3471  * @param[in, out] flow
3472  *   Pointer to flow structure.
3473  * @param[in] fm
3474  *   Pointer to flow meter structure.
3475  * @param[in] src_port
3476  *   The src port this extra rule should use.
3477  * @param[in] item
3478  *   The src port id match item.
3479  * @param[out] error
3480  *   Pointer to error structure.
3481  */
3482 static int
3483 flow_drv_mtr_hierarchy_rule_create(struct rte_eth_dev *dev,
3484                 struct rte_flow *flow,
3485                 struct mlx5_flow_meter_info *fm,
3486                 int32_t src_port,
3487                 const struct rte_flow_item *item,
3488                 struct rte_flow_error *error)
3489 {
3490         const struct mlx5_flow_driver_ops *fops;
3491         enum mlx5_flow_drv_type type = flow->drv_type;
3492
3493         MLX5_ASSERT(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
3494         fops = flow_get_drv_ops(type);
3495         return fops->meter_hierarchy_rule_create(dev, fm,
3496                                                 src_port, item, error);
3497 }
3498
3499 /**
3500  * Get RSS action from the action list.
3501  *
3502  * @param[in] dev
3503  *   Pointer to Ethernet device.
3504  * @param[in] actions
3505  *   Pointer to the list of actions.
3506  * @param[in] flow
3507  *   Parent flow structure pointer.
3508  *
3509  * @return
3510  *   Pointer to the RSS action if exist, else return NULL.
3511  */
3512 static const struct rte_flow_action_rss*
3513 flow_get_rss_action(struct rte_eth_dev *dev,
3514                     const struct rte_flow_action actions[])
3515 {
3516         struct mlx5_priv *priv = dev->data->dev_private;
3517         const struct rte_flow_action_rss *rss = NULL;
3518
3519         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3520                 switch (actions->type) {
3521                 case RTE_FLOW_ACTION_TYPE_RSS:
3522                         rss = actions->conf;
3523                         break;
3524                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
3525                 {
3526                         const struct rte_flow_action_sample *sample =
3527                                                                 actions->conf;
3528                         const struct rte_flow_action *act = sample->actions;
3529                         for (; act->type != RTE_FLOW_ACTION_TYPE_END; act++)
3530                                 if (act->type == RTE_FLOW_ACTION_TYPE_RSS)
3531                                         rss = act->conf;
3532                         break;
3533                 }
3534                 case RTE_FLOW_ACTION_TYPE_METER:
3535                 {
3536                         uint32_t mtr_idx;
3537                         struct mlx5_flow_meter_info *fm;
3538                         struct mlx5_flow_meter_policy *policy;
3539                         const struct rte_flow_action_meter *mtr = actions->conf;
3540
3541                         fm = mlx5_flow_meter_find(priv, mtr->mtr_id, &mtr_idx);
3542                         if (fm && !fm->def_policy) {
3543                                 policy = mlx5_flow_meter_policy_find(dev,
3544                                                 fm->policy_id, NULL);
3545                                 MLX5_ASSERT(policy);
3546                                 if (policy->is_hierarchy) {
3547                                         policy =
3548                                 mlx5_flow_meter_hierarchy_get_final_policy(dev,
3549                                                                         policy);
3550                                         if (!policy)
3551                                                 return NULL;
3552                                 }
3553                                 if (policy->is_rss)
3554                                         rss =
3555                                 policy->act_cnt[RTE_COLOR_GREEN].rss->conf;
3556                         }
3557                         break;
3558                 }
3559                 default:
3560                         break;
3561                 }
3562         }
3563         return rss;
3564 }
3565
3566 /**
3567  * Get ASO age action by index.
3568  *
3569  * @param[in] dev
3570  *   Pointer to the Ethernet device structure.
3571  * @param[in] age_idx
3572  *   Index to the ASO age action.
3573  *
3574  * @return
3575  *   The specified ASO age action.
3576  */
3577 struct mlx5_aso_age_action*
3578 flow_aso_age_get_by_idx(struct rte_eth_dev *dev, uint32_t age_idx)
3579 {
3580         uint16_t pool_idx = age_idx & UINT16_MAX;
3581         uint16_t offset = (age_idx >> 16) & UINT16_MAX;
3582         struct mlx5_priv *priv = dev->data->dev_private;
3583         struct mlx5_aso_age_mng *mng = priv->sh->aso_age_mng;
3584         struct mlx5_aso_age_pool *pool = mng->pools[pool_idx];
3585
3586         return &pool->actions[offset - 1];
3587 }
3588
3589 /* maps indirect action to translated direct in some actions array */
3590 struct mlx5_translated_action_handle {
3591         struct rte_flow_action_handle *action; /**< Indirect action handle. */
3592         int index; /**< Index in related array of rte_flow_action. */
3593 };
3594
3595 /**
3596  * Translates actions of type RTE_FLOW_ACTION_TYPE_INDIRECT to related
3597  * direct action if translation possible.
3598  * This functionality used to run same execution path for both direct and
3599  * indirect actions on flow create. All necessary preparations for indirect
3600  * action handling should be performed on *handle* actions list returned
3601  * from this call.
3602  *
3603  * @param[in] dev
3604  *   Pointer to Ethernet device.
3605  * @param[in] actions
3606  *   List of actions to translate.
3607  * @param[out] handle
3608  *   List to store translated indirect action object handles.
3609  * @param[in, out] indir_n
3610  *   Size of *handle* array. On return should be updated with number of
3611  *   indirect actions retrieved from the *actions* list.
3612  * @param[out] translated_actions
3613  *   List of actions where all indirect actions were translated to direct
3614  *   if possible. NULL if no translation took place.
3615  * @param[out] error
3616  *   Pointer to the error structure.
3617  *
3618  * @return
3619  *   0 on success, a negative errno value otherwise and rte_errno is set.
3620  */
3621 static int
3622 flow_action_handles_translate(struct rte_eth_dev *dev,
3623                               const struct rte_flow_action actions[],
3624                               struct mlx5_translated_action_handle *handle,
3625                               int *indir_n,
3626                               struct rte_flow_action **translated_actions,
3627                               struct rte_flow_error *error)
3628 {
3629         struct mlx5_priv *priv = dev->data->dev_private;
3630         struct rte_flow_action *translated = NULL;
3631         size_t actions_size;
3632         int n;
3633         int copied_n = 0;
3634         struct mlx5_translated_action_handle *handle_end = NULL;
3635
3636         for (n = 0; actions[n].type != RTE_FLOW_ACTION_TYPE_END; n++) {
3637                 if (actions[n].type != RTE_FLOW_ACTION_TYPE_INDIRECT)
3638                         continue;
3639                 if (copied_n == *indir_n) {
3640                         return rte_flow_error_set
3641                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
3642                                  NULL, "too many shared actions");
3643                 }
3644                 rte_memcpy(&handle[copied_n].action, &actions[n].conf,
3645                            sizeof(actions[n].conf));
3646                 handle[copied_n].index = n;
3647                 copied_n++;
3648         }
3649         n++;
3650         *indir_n = copied_n;
3651         if (!copied_n)
3652                 return 0;
3653         actions_size = sizeof(struct rte_flow_action) * n;
3654         translated = mlx5_malloc(MLX5_MEM_ZERO, actions_size, 0, SOCKET_ID_ANY);
3655         if (!translated) {
3656                 rte_errno = ENOMEM;
3657                 return -ENOMEM;
3658         }
3659         memcpy(translated, actions, actions_size);
3660         for (handle_end = handle + copied_n; handle < handle_end; handle++) {
3661                 struct mlx5_shared_action_rss *shared_rss;
3662                 uint32_t act_idx = (uint32_t)(uintptr_t)handle->action;
3663                 uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
3664                 uint32_t idx = act_idx &
3665                                ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
3666
3667                 switch (type) {
3668                 case MLX5_INDIRECT_ACTION_TYPE_RSS:
3669                         shared_rss = mlx5_ipool_get
3670                           (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS], idx);
3671                         translated[handle->index].type =
3672                                 RTE_FLOW_ACTION_TYPE_RSS;
3673                         translated[handle->index].conf =
3674                                 &shared_rss->origin;
3675                         break;
3676                 case MLX5_INDIRECT_ACTION_TYPE_COUNT:
3677                         translated[handle->index].type =
3678                                                 (enum rte_flow_action_type)
3679                                                 MLX5_RTE_FLOW_ACTION_TYPE_COUNT;
3680                         translated[handle->index].conf = (void *)(uintptr_t)idx;
3681                         break;
3682                 case MLX5_INDIRECT_ACTION_TYPE_AGE:
3683                         if (priv->sh->flow_hit_aso_en) {
3684                                 translated[handle->index].type =
3685                                         (enum rte_flow_action_type)
3686                                         MLX5_RTE_FLOW_ACTION_TYPE_AGE;
3687                                 translated[handle->index].conf =
3688                                                          (void *)(uintptr_t)idx;
3689                                 break;
3690                         }
3691                         /* Fall-through */
3692                 case MLX5_INDIRECT_ACTION_TYPE_CT:
3693                         if (priv->sh->ct_aso_en) {
3694                                 translated[handle->index].type =
3695                                         RTE_FLOW_ACTION_TYPE_CONNTRACK;
3696                                 translated[handle->index].conf =
3697                                                          (void *)(uintptr_t)idx;
3698                                 break;
3699                         }
3700                         /* Fall-through */
3701                 default:
3702                         mlx5_free(translated);
3703                         return rte_flow_error_set
3704                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
3705                                  NULL, "invalid indirect action type");
3706                 }
3707         }
3708         *translated_actions = translated;
3709         return 0;
3710 }
3711
3712 /**
3713  * Get Shared RSS action from the action list.
3714  *
3715  * @param[in] dev
3716  *   Pointer to Ethernet device.
3717  * @param[in] shared
3718  *   Pointer to the list of actions.
3719  * @param[in] shared_n
3720  *   Actions list length.
3721  *
3722  * @return
3723  *   The MLX5 RSS action ID if exists, otherwise return 0.
3724  */
3725 static uint32_t
3726 flow_get_shared_rss_action(struct rte_eth_dev *dev,
3727                            struct mlx5_translated_action_handle *handle,
3728                            int shared_n)
3729 {
3730         struct mlx5_translated_action_handle *handle_end;
3731         struct mlx5_priv *priv = dev->data->dev_private;
3732         struct mlx5_shared_action_rss *shared_rss;
3733
3734
3735         for (handle_end = handle + shared_n; handle < handle_end; handle++) {
3736                 uint32_t act_idx = (uint32_t)(uintptr_t)handle->action;
3737                 uint32_t type = act_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET;
3738                 uint32_t idx = act_idx &
3739                                ((1u << MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1);
3740                 switch (type) {
3741                 case MLX5_INDIRECT_ACTION_TYPE_RSS:
3742                         shared_rss = mlx5_ipool_get
3743                                 (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
3744                                                                            idx);
3745                         __atomic_add_fetch(&shared_rss->refcnt, 1,
3746                                            __ATOMIC_RELAXED);
3747                         return idx;
3748                 default:
3749                         break;
3750                 }
3751         }
3752         return 0;
3753 }
3754
3755 static unsigned int
3756 find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
3757 {
3758         const struct rte_flow_item *item;
3759         unsigned int has_vlan = 0;
3760
3761         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
3762                 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
3763                         has_vlan = 1;
3764                         break;
3765                 }
3766         }
3767         if (has_vlan)
3768                 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
3769                                        MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
3770         return rss_level < 2 ? MLX5_EXPANSION_ROOT :
3771                                MLX5_EXPANSION_ROOT_OUTER;
3772 }
3773
3774 /**
3775  *  Get layer flags from the prefix flow.
3776  *
3777  *  Some flows may be split to several subflows, the prefix subflow gets the
3778  *  match items and the suffix sub flow gets the actions.
3779  *  Some actions need the user defined match item flags to get the detail for
3780  *  the action.
3781  *  This function helps the suffix flow to get the item layer flags from prefix
3782  *  subflow.
3783  *
3784  * @param[in] dev_flow
3785  *   Pointer the created preifx subflow.
3786  *
3787  * @return
3788  *   The layers get from prefix subflow.
3789  */
3790 static inline uint64_t
3791 flow_get_prefix_layer_flags(struct mlx5_flow *dev_flow)
3792 {
3793         uint64_t layers = 0;
3794
3795         /*
3796          * Layers bits could be localization, but usually the compiler will
3797          * help to do the optimization work for source code.
3798          * If no decap actions, use the layers directly.
3799          */
3800         if (!(dev_flow->act_flags & MLX5_FLOW_ACTION_DECAP))
3801                 return dev_flow->handle->layers;
3802         /* Convert L3 layers with decap action. */
3803         if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L3_IPV4)
3804                 layers |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3805         else if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L3_IPV6)
3806                 layers |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3807         /* Convert L4 layers with decap action.  */
3808         if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L4_TCP)
3809                 layers |= MLX5_FLOW_LAYER_OUTER_L4_TCP;
3810         else if (dev_flow->handle->layers & MLX5_FLOW_LAYER_INNER_L4_UDP)
3811                 layers |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
3812         return layers;
3813 }
3814
3815 /**
3816  * Get metadata split action information.
3817  *
3818  * @param[in] actions
3819  *   Pointer to the list of actions.
3820  * @param[out] qrss
3821  *   Pointer to the return pointer.
3822  * @param[out] qrss_type
3823  *   Pointer to the action type to return. RTE_FLOW_ACTION_TYPE_END is returned
3824  *   if no QUEUE/RSS is found.
3825  * @param[out] encap_idx
3826  *   Pointer to the index of the encap action if exists, otherwise the last
3827  *   action index.
3828  *
3829  * @return
3830  *   Total number of actions.
3831  */
3832 static int
3833 flow_parse_metadata_split_actions_info(const struct rte_flow_action actions[],
3834                                        const struct rte_flow_action **qrss,
3835                                        int *encap_idx)
3836 {
3837         const struct rte_flow_action_raw_encap *raw_encap;
3838         int actions_n = 0;
3839         int raw_decap_idx = -1;
3840
3841         *encap_idx = -1;
3842         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3843                 switch (actions->type) {
3844                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3845                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3846                         *encap_idx = actions_n;
3847                         break;
3848                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3849                         raw_decap_idx = actions_n;
3850                         break;
3851                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3852                         raw_encap = actions->conf;
3853                         if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
3854                                 *encap_idx = raw_decap_idx != -1 ?
3855                                                       raw_decap_idx : actions_n;
3856                         break;
3857                 case RTE_FLOW_ACTION_TYPE_QUEUE:
3858                 case RTE_FLOW_ACTION_TYPE_RSS:
3859                         *qrss = actions;
3860                         break;
3861                 default:
3862                         break;
3863                 }
3864                 actions_n++;
3865         }
3866         if (*encap_idx == -1)
3867                 *encap_idx = actions_n;
3868         /* Count RTE_FLOW_ACTION_TYPE_END. */
3869         return actions_n + 1;
3870 }
3871
3872 /**
3873  * Check if the action will change packet.
3874  *
3875  * @param dev
3876  *   Pointer to Ethernet device.
3877  * @param[in] type
3878  *   action type.
3879  *
3880  * @return
3881  *   true if action will change packet, false otherwise.
3882  */
3883 static bool flow_check_modify_action_type(struct rte_eth_dev *dev,
3884                                           enum rte_flow_action_type type)
3885 {
3886         struct mlx5_priv *priv = dev->data->dev_private;
3887
3888         switch (type) {
3889         case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
3890         case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
3891         case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
3892         case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
3893         case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
3894         case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
3895         case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
3896         case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
3897         case RTE_FLOW_ACTION_TYPE_DEC_TTL:
3898         case RTE_FLOW_ACTION_TYPE_SET_TTL:
3899         case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
3900         case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
3901         case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
3902         case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
3903         case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
3904         case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
3905         case RTE_FLOW_ACTION_TYPE_SET_META:
3906         case RTE_FLOW_ACTION_TYPE_SET_TAG:
3907         case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
3908         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3909         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3910         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3911         case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3912         case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
3913         case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3914         case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
3915         case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3916         case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3917         case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
3918                 return true;
3919         case RTE_FLOW_ACTION_TYPE_FLAG:
3920         case RTE_FLOW_ACTION_TYPE_MARK:
3921                 if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY)
3922                         return true;
3923                 else
3924                         return false;
3925         default:
3926                 return false;
3927         }
3928 }
3929
3930 /**
3931  * Check meter action from the action list.
3932  *
3933  * @param dev
3934  *   Pointer to Ethernet device.
3935  * @param[in] actions
3936  *   Pointer to the list of actions.
3937  * @param[out] has_mtr
3938  *   Pointer to the meter exist flag.
3939  * @param[out] has_modify
3940  *   Pointer to the flag showing there's packet change action.
3941  * @param[out] meter_id
3942  *   Pointer to the meter id.
3943  *
3944  * @return
3945  *   Total number of actions.
3946  */
3947 static int
3948 flow_check_meter_action(struct rte_eth_dev *dev,
3949                         const struct rte_flow_action actions[],
3950                         bool *has_mtr, bool *has_modify, uint32_t *meter_id)
3951 {
3952         const struct rte_flow_action_meter *mtr = NULL;
3953         int actions_n = 0;
3954
3955         MLX5_ASSERT(has_mtr);
3956         *has_mtr = false;
3957         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3958                 switch (actions->type) {
3959                 case RTE_FLOW_ACTION_TYPE_METER:
3960                         mtr = actions->conf;
3961                         *meter_id = mtr->mtr_id;
3962                         *has_mtr = true;
3963                         break;
3964                 default:
3965                         break;
3966                 }
3967                 if (!*has_mtr)
3968                         *has_modify |= flow_check_modify_action_type(dev,
3969                                                                 actions->type);
3970                 actions_n++;
3971         }
3972         /* Count RTE_FLOW_ACTION_TYPE_END. */
3973         return actions_n + 1;
3974 }
3975
3976 /**
3977  * Check if the flow should be split due to hairpin.
3978  * The reason for the split is that in current HW we can't
3979  * support encap and push-vlan on Rx, so if a flow contains
3980  * these actions we move it to Tx.
3981  *
3982  * @param dev
3983  *   Pointer to Ethernet device.
3984  * @param[in] attr
3985  *   Flow rule attributes.
3986  * @param[in] actions
3987  *   Associated actions (list terminated by the END action).
3988  *
3989  * @return
3990  *   > 0 the number of actions and the flow should be split,
3991  *   0 when no split required.
3992  */
3993 static int
3994 flow_check_hairpin_split(struct rte_eth_dev *dev,
3995                          const struct rte_flow_attr *attr,
3996                          const struct rte_flow_action actions[])
3997 {
3998         int queue_action = 0;
3999         int action_n = 0;
4000         int split = 0;
4001         const struct rte_flow_action_queue *queue;
4002         const struct rte_flow_action_rss *rss;
4003         const struct rte_flow_action_raw_encap *raw_encap;
4004         const struct rte_eth_hairpin_conf *conf;
4005
4006         if (!attr->ingress)
4007                 return 0;
4008         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4009                 switch (actions->type) {
4010                 case RTE_FLOW_ACTION_TYPE_QUEUE:
4011                         queue = actions->conf;
4012                         if (queue == NULL)
4013                                 return 0;
4014                         conf = mlx5_rxq_get_hairpin_conf(dev, queue->index);
4015                         if (conf == NULL || conf->tx_explicit != 0)
4016                                 return 0;
4017                         queue_action = 1;
4018                         action_n++;
4019                         break;
4020                 case RTE_FLOW_ACTION_TYPE_RSS:
4021                         rss = actions->conf;
4022                         if (rss == NULL || rss->queue_num == 0)
4023                                 return 0;
4024                         conf = mlx5_rxq_get_hairpin_conf(dev, rss->queue[0]);
4025                         if (conf == NULL || conf->tx_explicit != 0)
4026                                 return 0;
4027                         queue_action = 1;
4028                         action_n++;
4029                         break;
4030                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4031                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4032                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4033                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4034                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4035                         split++;
4036                         action_n++;
4037                         break;
4038                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4039                         raw_encap = actions->conf;
4040                         if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
4041                                 split++;
4042                         action_n++;
4043                         break;
4044                 default:
4045                         action_n++;
4046                         break;
4047                 }
4048         }
4049         if (split && queue_action)
4050                 return action_n;
4051         return 0;
4052 }
4053
4054 /* Declare flow create/destroy prototype in advance. */
4055 static uint32_t
4056 flow_list_create(struct rte_eth_dev *dev, enum mlx5_flow_type type,
4057                  const struct rte_flow_attr *attr,
4058                  const struct rte_flow_item items[],
4059                  const struct rte_flow_action actions[],
4060                  bool external, struct rte_flow_error *error);
4061
4062 static void
4063 flow_list_destroy(struct rte_eth_dev *dev, enum mlx5_flow_type type,
4064                   uint32_t flow_idx);
4065
4066 int
4067 flow_dv_mreg_match_cb(void *tool_ctx __rte_unused,
4068                       struct mlx5_list_entry *entry, void *cb_ctx)
4069 {
4070         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
4071         struct mlx5_flow_mreg_copy_resource *mcp_res =
4072                                container_of(entry, typeof(*mcp_res), hlist_ent);
4073
4074         return mcp_res->mark_id != *(uint32_t *)(ctx->data);
4075 }
4076
4077 struct mlx5_list_entry *
4078 flow_dv_mreg_create_cb(void *tool_ctx, void *cb_ctx)
4079 {
4080         struct rte_eth_dev *dev = tool_ctx;
4081         struct mlx5_priv *priv = dev->data->dev_private;
4082         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
4083         struct mlx5_flow_mreg_copy_resource *mcp_res;
4084         struct rte_flow_error *error = ctx->error;
4085         uint32_t idx = 0;
4086         int ret;
4087         uint32_t mark_id = *(uint32_t *)(ctx->data);
4088         struct rte_flow_attr attr = {
4089                 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
4090                 .ingress = 1,
4091         };
4092         struct mlx5_rte_flow_item_tag tag_spec = {
4093                 .data = mark_id,
4094         };
4095         struct rte_flow_item items[] = {
4096                 [1] = { .type = RTE_FLOW_ITEM_TYPE_END, },
4097         };
4098         struct rte_flow_action_mark ftag = {
4099                 .id = mark_id,
4100         };
4101         struct mlx5_flow_action_copy_mreg cp_mreg = {
4102                 .dst = REG_B,
4103                 .src = REG_NON,
4104         };
4105         struct rte_flow_action_jump jump = {
4106                 .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
4107         };
4108         struct rte_flow_action actions[] = {
4109                 [3] = { .type = RTE_FLOW_ACTION_TYPE_END, },
4110         };
4111
4112         /* Fill the register fileds in the flow. */
4113         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
4114         if (ret < 0)
4115                 return NULL;
4116         tag_spec.id = ret;
4117         ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
4118         if (ret < 0)
4119                 return NULL;
4120         cp_mreg.src = ret;
4121         /* Provide the full width of FLAG specific value. */
4122         if (mark_id == (priv->sh->dv_regc0_mask & MLX5_FLOW_MARK_DEFAULT))
4123                 tag_spec.data = MLX5_FLOW_MARK_DEFAULT;
4124         /* Build a new flow. */
4125         if (mark_id != MLX5_DEFAULT_COPY_ID) {
4126                 items[0] = (struct rte_flow_item){
4127                         .type = (enum rte_flow_item_type)
4128                                 MLX5_RTE_FLOW_ITEM_TYPE_TAG,
4129                         .spec = &tag_spec,
4130                 };
4131                 items[1] = (struct rte_flow_item){
4132                         .type = RTE_FLOW_ITEM_TYPE_END,
4133                 };
4134                 actions[0] = (struct rte_flow_action){
4135                         .type = (enum rte_flow_action_type)
4136                                 MLX5_RTE_FLOW_ACTION_TYPE_MARK,
4137                         .conf = &ftag,
4138                 };
4139                 actions[1] = (struct rte_flow_action){
4140                         .type = (enum rte_flow_action_type)
4141                                 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
4142                         .conf = &cp_mreg,
4143                 };
4144                 actions[2] = (struct rte_flow_action){
4145                         .type = RTE_FLOW_ACTION_TYPE_JUMP,
4146                         .conf = &jump,
4147                 };
4148                 actions[3] = (struct rte_flow_action){
4149                         .type = RTE_FLOW_ACTION_TYPE_END,
4150                 };
4151         } else {
4152                 /* Default rule, wildcard match. */
4153                 attr.priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR;
4154                 items[0] = (struct rte_flow_item){
4155                         .type = RTE_FLOW_ITEM_TYPE_END,
4156                 };
4157                 actions[0] = (struct rte_flow_action){
4158                         .type = (enum rte_flow_action_type)
4159                                 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
4160                         .conf = &cp_mreg,
4161                 };
4162                 actions[1] = (struct rte_flow_action){
4163                         .type = RTE_FLOW_ACTION_TYPE_JUMP,
4164                         .conf = &jump,
4165                 };
4166                 actions[2] = (struct rte_flow_action){
4167                         .type = RTE_FLOW_ACTION_TYPE_END,
4168                 };
4169         }
4170         /* Build a new entry. */
4171         mcp_res = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MCP], &idx);
4172         if (!mcp_res) {
4173                 rte_errno = ENOMEM;
4174                 return NULL;
4175         }
4176         mcp_res->idx = idx;
4177         mcp_res->mark_id = mark_id;
4178         /*
4179          * The copy Flows are not included in any list. There
4180          * ones are referenced from other Flows and can not
4181          * be applied, removed, deleted in ardbitrary order
4182          * by list traversing.
4183          */
4184         mcp_res->rix_flow = flow_list_create(dev, MLX5_FLOW_TYPE_MCP,
4185                                         &attr, items, actions, false, error);
4186         if (!mcp_res->rix_flow) {
4187                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], idx);
4188                 return NULL;
4189         }
4190         return &mcp_res->hlist_ent;
4191 }
4192
4193 struct mlx5_list_entry *
4194 flow_dv_mreg_clone_cb(void *tool_ctx, struct mlx5_list_entry *oentry,
4195                       void *cb_ctx __rte_unused)
4196 {
4197         struct rte_eth_dev *dev = tool_ctx;
4198         struct mlx5_priv *priv = dev->data->dev_private;
4199         struct mlx5_flow_mreg_copy_resource *mcp_res;
4200         uint32_t idx = 0;
4201
4202         mcp_res = mlx5_ipool_malloc(priv->sh->ipool[MLX5_IPOOL_MCP], &idx);
4203         if (!mcp_res) {
4204                 rte_errno = ENOMEM;
4205                 return NULL;
4206         }
4207         memcpy(mcp_res, oentry, sizeof(*mcp_res));
4208         mcp_res->idx = idx;
4209         return &mcp_res->hlist_ent;
4210 }
4211
4212 void
4213 flow_dv_mreg_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
4214 {
4215         struct mlx5_flow_mreg_copy_resource *mcp_res =
4216                                container_of(entry, typeof(*mcp_res), hlist_ent);
4217         struct rte_eth_dev *dev = tool_ctx;
4218         struct mlx5_priv *priv = dev->data->dev_private;
4219
4220         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], mcp_res->idx);
4221 }
4222
4223 /**
4224  * Add a flow of copying flow metadata registers in RX_CP_TBL.
4225  *
4226  * As mark_id is unique, if there's already a registered flow for the mark_id,
4227  * return by increasing the reference counter of the resource. Otherwise, create
4228  * the resource (mcp_res) and flow.
4229  *
4230  * Flow looks like,
4231  *   - If ingress port is ANY and reg_c[1] is mark_id,
4232  *     flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
4233  *
4234  * For default flow (zero mark_id), flow is like,
4235  *   - If ingress port is ANY,
4236  *     reg_b := reg_c[0] and jump to RX_ACT_TBL.
4237  *
4238  * @param dev
4239  *   Pointer to Ethernet device.
4240  * @param mark_id
4241  *   ID of MARK action, zero means default flow for META.
4242  * @param[out] error
4243  *   Perform verbose error reporting if not NULL.
4244  *
4245  * @return
4246  *   Associated resource on success, NULL otherwise and rte_errno is set.
4247  */
4248 static struct mlx5_flow_mreg_copy_resource *
4249 flow_mreg_add_copy_action(struct rte_eth_dev *dev, uint32_t mark_id,
4250                           struct rte_flow_error *error)
4251 {
4252         struct mlx5_priv *priv = dev->data->dev_private;
4253         struct mlx5_list_entry *entry;
4254         struct mlx5_flow_cb_ctx ctx = {
4255                 .dev = dev,
4256                 .error = error,
4257                 .data = &mark_id,
4258         };
4259
4260         /* Check if already registered. */
4261         MLX5_ASSERT(priv->mreg_cp_tbl);
4262         entry = mlx5_hlist_register(priv->mreg_cp_tbl, mark_id, &ctx);
4263         if (!entry)
4264                 return NULL;
4265         return container_of(entry, struct mlx5_flow_mreg_copy_resource,
4266                             hlist_ent);
4267 }
4268
4269 void
4270 flow_dv_mreg_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
4271 {
4272         struct mlx5_flow_mreg_copy_resource *mcp_res =
4273                                container_of(entry, typeof(*mcp_res), hlist_ent);
4274         struct rte_eth_dev *dev = tool_ctx;
4275         struct mlx5_priv *priv = dev->data->dev_private;
4276
4277         MLX5_ASSERT(mcp_res->rix_flow);
4278         flow_list_destroy(dev, MLX5_FLOW_TYPE_MCP, mcp_res->rix_flow);
4279         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MCP], mcp_res->idx);
4280 }
4281
4282 /**
4283  * Release flow in RX_CP_TBL.
4284  *
4285  * @param dev
4286  *   Pointer to Ethernet device.
4287  * @flow
4288  *   Parent flow for wich copying is provided.
4289  */
4290 static void
4291 flow_mreg_del_copy_action(struct rte_eth_dev *dev,
4292                           struct rte_flow *flow)
4293 {
4294         struct mlx5_flow_mreg_copy_resource *mcp_res;
4295         struct mlx5_priv *priv = dev->data->dev_private;
4296
4297         if (!flow->rix_mreg_copy)
4298                 return;
4299         mcp_res = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MCP],
4300                                  flow->rix_mreg_copy);
4301         if (!mcp_res || !priv->mreg_cp_tbl)
4302                 return;
4303         MLX5_ASSERT(mcp_res->rix_flow);
4304         mlx5_hlist_unregister(priv->mreg_cp_tbl, &mcp_res->hlist_ent);
4305         flow->rix_mreg_copy = 0;
4306 }
4307
4308 /**
4309  * Remove the default copy action from RX_CP_TBL.
4310  *
4311  * This functions is called in the mlx5_dev_start(). No thread safe
4312  * is guaranteed.
4313  *
4314  * @param dev
4315  *   Pointer to Ethernet device.
4316  */
4317 static void
4318 flow_mreg_del_default_copy_action(struct rte_eth_dev *dev)
4319 {
4320         struct mlx5_list_entry *entry;
4321         struct mlx5_priv *priv = dev->data->dev_private;
4322         struct mlx5_flow_cb_ctx ctx;
4323         uint32_t mark_id;
4324
4325         /* Check if default flow is registered. */
4326         if (!priv->mreg_cp_tbl)
4327                 return;
4328         mark_id = MLX5_DEFAULT_COPY_ID;
4329         ctx.data = &mark_id;
4330         entry = mlx5_hlist_lookup(priv->mreg_cp_tbl, mark_id, &ctx);
4331         if (!entry)
4332                 return;
4333         mlx5_hlist_unregister(priv->mreg_cp_tbl, entry);
4334 }
4335
4336 /**
4337  * Add the default copy action in in RX_CP_TBL.
4338  *
4339  * This functions is called in the mlx5_dev_start(). No thread safe
4340  * is guaranteed.
4341  *
4342  * @param dev
4343  *   Pointer to Ethernet device.
4344  * @param[out] error
4345  *   Perform verbose error reporting if not NULL.
4346  *
4347  * @return
4348  *   0 for success, negative value otherwise and rte_errno is set.
4349  */
4350 static int
4351 flow_mreg_add_default_copy_action(struct rte_eth_dev *dev,
4352                                   struct rte_flow_error *error)
4353 {
4354         struct mlx5_priv *priv = dev->data->dev_private;
4355         struct mlx5_flow_mreg_copy_resource *mcp_res;
4356         struct mlx5_flow_cb_ctx ctx;
4357         uint32_t mark_id;
4358
4359         /* Check whether extensive metadata feature is engaged. */
4360         if (!priv->config.dv_flow_en ||
4361             priv->config.dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
4362             !mlx5_flow_ext_mreg_supported(dev) ||
4363             !priv->sh->dv_regc0_mask)
4364                 return 0;
4365         /*
4366          * Add default mreg copy flow may be called multiple time, but
4367          * only be called once in stop. Avoid register it twice.
4368          */
4369         mark_id = MLX5_DEFAULT_COPY_ID;
4370         ctx.data = &mark_id;
4371         if (mlx5_hlist_lookup(priv->mreg_cp_tbl, mark_id, &ctx))
4372                 return 0;
4373         mcp_res = flow_mreg_add_copy_action(dev, mark_id, error);
4374         if (!mcp_res)
4375                 return -rte_errno;
4376         return 0;
4377 }
4378
4379 /**
4380  * Add a flow of copying flow metadata registers in RX_CP_TBL.
4381  *
4382  * All the flow having Q/RSS action should be split by
4383  * flow_mreg_split_qrss_prep() to pass by RX_CP_TBL. A flow in the RX_CP_TBL
4384  * performs the following,
4385  *   - CQE->flow_tag := reg_c[1] (MARK)
4386  *   - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
4387  * As CQE's flow_tag is not a register, it can't be simply copied from reg_c[1]
4388  * but there should be a flow per each MARK ID set by MARK action.
4389  *
4390  * For the aforementioned reason, if there's a MARK action in flow's action
4391  * list, a corresponding flow should be added to the RX_CP_TBL in order to copy
4392  * the MARK ID to CQE's flow_tag like,
4393  *   - If reg_c[1] is mark_id,
4394  *     flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
4395  *
4396  * For SET_META action which stores value in reg_c[0], as the destination is
4397  * also a flow metadata register (reg_b), adding a default flow is enough. Zero
4398  * MARK ID means the default flow. The default flow looks like,
4399  *   - For all flow, reg_b := reg_c[0] and jump to RX_ACT_TBL.
4400  *
4401  * @param dev
4402  *   Pointer to Ethernet device.
4403  * @param flow
4404  *   Pointer to flow structure.
4405  * @param[in] actions
4406  *   Pointer to the list of actions.
4407  * @param[out] error
4408  *   Perform verbose error reporting if not NULL.
4409  *
4410  * @return
4411  *   0 on success, negative value otherwise and rte_errno is set.
4412  */
4413 static int
4414 flow_mreg_update_copy_table(struct rte_eth_dev *dev,
4415                             struct rte_flow *flow,
4416                             const struct rte_flow_action *actions,
4417                             struct rte_flow_error *error)
4418 {
4419         struct mlx5_priv *priv = dev->data->dev_private;
4420         struct mlx5_dev_config *config = &priv->config;
4421         struct mlx5_flow_mreg_copy_resource *mcp_res;
4422         const struct rte_flow_action_mark *mark;
4423
4424         /* Check whether extensive metadata feature is engaged. */
4425         if (!config->dv_flow_en ||
4426             config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
4427             !mlx5_flow_ext_mreg_supported(dev) ||
4428             !priv->sh->dv_regc0_mask)
4429                 return 0;
4430         /* Find MARK action. */
4431         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4432                 switch (actions->type) {
4433                 case RTE_FLOW_ACTION_TYPE_FLAG:
4434                         mcp_res = flow_mreg_add_copy_action
4435                                 (dev, MLX5_FLOW_MARK_DEFAULT, error);
4436                         if (!mcp_res)
4437                                 return -rte_errno;
4438                         flow->rix_mreg_copy = mcp_res->idx;
4439                         return 0;
4440                 case RTE_FLOW_ACTION_TYPE_MARK:
4441                         mark = (const struct rte_flow_action_mark *)
4442                                 actions->conf;
4443                         mcp_res =
4444                                 flow_mreg_add_copy_action(dev, mark->id, error);
4445                         if (!mcp_res)
4446                                 return -rte_errno;
4447                         flow->rix_mreg_copy = mcp_res->idx;
4448                         return 0;
4449                 default:
4450                         break;
4451                 }
4452         }
4453         return 0;
4454 }
4455
4456 #define MLX5_MAX_SPLIT_ACTIONS 24
4457 #define MLX5_MAX_SPLIT_ITEMS 24
4458
4459 /**
4460  * Split the hairpin flow.
4461  * Since HW can't support encap and push-vlan on Rx, we move these
4462  * actions to Tx.
4463  * If the count action is after the encap then we also
4464  * move the count action. in this case the count will also measure
4465  * the outer bytes.
4466  *
4467  * @param dev
4468  *   Pointer to Ethernet device.
4469  * @param[in] actions
4470  *   Associated actions (list terminated by the END action).
4471  * @param[out] actions_rx
4472  *   Rx flow actions.
4473  * @param[out] actions_tx
4474  *   Tx flow actions..
4475  * @param[out] pattern_tx
4476  *   The pattern items for the Tx flow.
4477  * @param[out] flow_id
4478  *   The flow ID connected to this flow.
4479  *
4480  * @return
4481  *   0 on success.
4482  */
4483 static int
4484 flow_hairpin_split(struct rte_eth_dev *dev,
4485                    const struct rte_flow_action actions[],
4486                    struct rte_flow_action actions_rx[],
4487                    struct rte_flow_action actions_tx[],
4488                    struct rte_flow_item pattern_tx[],
4489                    uint32_t flow_id)
4490 {
4491         const struct rte_flow_action_raw_encap *raw_encap;
4492         const struct rte_flow_action_raw_decap *raw_decap;
4493         struct mlx5_rte_flow_action_set_tag *set_tag;
4494         struct rte_flow_action *tag_action;
4495         struct mlx5_rte_flow_item_tag *tag_item;
4496         struct rte_flow_item *item;
4497         char *addr;
4498         int encap = 0;
4499
4500         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4501                 switch (actions->type) {
4502                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
4503                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
4504                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4505                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4506                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
4507                         rte_memcpy(actions_tx, actions,
4508                                sizeof(struct rte_flow_action));
4509                         actions_tx++;
4510                         break;
4511                 case RTE_FLOW_ACTION_TYPE_COUNT:
4512                         if (encap) {
4513                                 rte_memcpy(actions_tx, actions,
4514                                            sizeof(struct rte_flow_action));
4515                                 actions_tx++;
4516                         } else {
4517                                 rte_memcpy(actions_rx, actions,
4518                                            sizeof(struct rte_flow_action));
4519                                 actions_rx++;
4520                         }
4521                         break;
4522                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4523                         raw_encap = actions->conf;
4524                         if (raw_encap->size > MLX5_ENCAPSULATION_DECISION_SIZE) {
4525                                 memcpy(actions_tx, actions,
4526                                        sizeof(struct rte_flow_action));
4527                                 actions_tx++;
4528                                 encap = 1;
4529                         } else {
4530                                 rte_memcpy(actions_rx, actions,
4531                                            sizeof(struct rte_flow_action));
4532                                 actions_rx++;
4533                         }
4534                         break;
4535                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4536                         raw_decap = actions->conf;
4537                         if (raw_decap->size < MLX5_ENCAPSULATION_DECISION_SIZE) {
4538                                 memcpy(actions_tx, actions,
4539                                        sizeof(struct rte_flow_action));
4540                                 actions_tx++;
4541                         } else {
4542                                 rte_memcpy(actions_rx, actions,
4543                                            sizeof(struct rte_flow_action));
4544                                 actions_rx++;
4545                         }
4546                         break;
4547                 default:
4548                         rte_memcpy(actions_rx, actions,
4549                                    sizeof(struct rte_flow_action));
4550                         actions_rx++;
4551                         break;
4552                 }
4553         }
4554         /* Add set meta action and end action for the Rx flow. */
4555         tag_action = actions_rx;
4556         tag_action->type = (enum rte_flow_action_type)
4557                            MLX5_RTE_FLOW_ACTION_TYPE_TAG;
4558         actions_rx++;
4559         rte_memcpy(actions_rx, actions, sizeof(struct rte_flow_action));
4560         actions_rx++;
4561         set_tag = (void *)actions_rx;
4562         *set_tag = (struct mlx5_rte_flow_action_set_tag) {
4563                 .id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_RX, 0, NULL),
4564                 .data = flow_id,
4565         };
4566         MLX5_ASSERT(set_tag->id > REG_NON);
4567         tag_action->conf = set_tag;
4568         /* Create Tx item list. */
4569         rte_memcpy(actions_tx, actions, sizeof(struct rte_flow_action));
4570         addr = (void *)&pattern_tx[2];
4571         item = pattern_tx;
4572         item->type = (enum rte_flow_item_type)
4573                      MLX5_RTE_FLOW_ITEM_TYPE_TAG;
4574         tag_item = (void *)addr;
4575         tag_item->data = flow_id;
4576         tag_item->id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_TX, 0, NULL);
4577         MLX5_ASSERT(set_tag->id > REG_NON);
4578         item->spec = tag_item;
4579         addr += sizeof(struct mlx5_rte_flow_item_tag);
4580         tag_item = (void *)addr;
4581         tag_item->data = UINT32_MAX;
4582         tag_item->id = UINT16_MAX;
4583         item->mask = tag_item;
4584         item->last = NULL;
4585         item++;
4586         item->type = RTE_FLOW_ITEM_TYPE_END;
4587         return 0;
4588 }
4589
4590 /**
4591  * The last stage of splitting chain, just creates the subflow
4592  * without any modification.
4593  *
4594  * @param[in] dev
4595  *   Pointer to Ethernet device.
4596  * @param[in] flow
4597  *   Parent flow structure pointer.
4598  * @param[in, out] sub_flow
4599  *   Pointer to return the created subflow, may be NULL.
4600  * @param[in] attr
4601  *   Flow rule attributes.
4602  * @param[in] items
4603  *   Pattern specification (list terminated by the END pattern item).
4604  * @param[in] actions
4605  *   Associated actions (list terminated by the END action).
4606  * @param[in] flow_split_info
4607  *   Pointer to flow split info structure.
4608  * @param[out] error
4609  *   Perform verbose error reporting if not NULL.
4610  * @return
4611  *   0 on success, negative value otherwise
4612  */
4613 static int
4614 flow_create_split_inner(struct rte_eth_dev *dev,
4615                         struct rte_flow *flow,
4616                         struct mlx5_flow **sub_flow,
4617                         const struct rte_flow_attr *attr,
4618                         const struct rte_flow_item items[],
4619                         const struct rte_flow_action actions[],
4620                         struct mlx5_flow_split_info *flow_split_info,
4621                         struct rte_flow_error *error)
4622 {
4623         struct mlx5_flow *dev_flow;
4624
4625         dev_flow = flow_drv_prepare(dev, flow, attr, items, actions,
4626                                     flow_split_info->flow_idx, error);
4627         if (!dev_flow)
4628                 return -rte_errno;
4629         dev_flow->flow = flow;
4630         dev_flow->external = flow_split_info->external;
4631         dev_flow->skip_scale = flow_split_info->skip_scale;
4632         /* Subflow object was created, we must include one in the list. */
4633         SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
4634                       dev_flow->handle, next);
4635         /*
4636          * If dev_flow is as one of the suffix flow, some actions in suffix
4637          * flow may need some user defined item layer flags, and pass the
4638          * Metadate rxq mark flag to suffix flow as well.
4639          */
4640         if (flow_split_info->prefix_layers)
4641                 dev_flow->handle->layers = flow_split_info->prefix_layers;
4642         if (flow_split_info->prefix_mark)
4643                 dev_flow->handle->mark = 1;
4644         if (sub_flow)
4645                 *sub_flow = dev_flow;
4646 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
4647         dev_flow->dv.table_id = flow_split_info->table_id;
4648 #endif
4649         return flow_drv_translate(dev, dev_flow, attr, items, actions, error);
4650 }
4651
4652 /**
4653  * Get the sub policy of a meter.
4654  *
4655  * @param[in] dev
4656  *   Pointer to Ethernet device.
4657  * @param[in] flow
4658  *   Parent flow structure pointer.
4659  * @param wks
4660  *   Pointer to thread flow work space.
4661  * @param[in] attr
4662  *   Flow rule attributes.
4663  * @param[in] items
4664  *   Pattern specification (list terminated by the END pattern item).
4665  * @param[out] error
4666  *   Perform verbose error reporting if not NULL.
4667  *
4668  * @return
4669  *   Pointer to the meter sub policy, NULL otherwise and rte_errno is set.
4670  */
4671 static struct mlx5_flow_meter_sub_policy *
4672 get_meter_sub_policy(struct rte_eth_dev *dev,
4673                      struct rte_flow *flow,
4674                      struct mlx5_flow_workspace *wks,
4675                      const struct rte_flow_attr *attr,
4676                      const struct rte_flow_item items[],
4677                      struct rte_flow_error *error)
4678 {
4679         struct mlx5_flow_meter_policy *policy;
4680         struct mlx5_flow_meter_policy *final_policy;
4681         struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
4682
4683         policy = wks->policy;
4684         final_policy = policy->is_hierarchy ? wks->final_policy : policy;
4685         if (final_policy->is_rss || final_policy->is_queue) {
4686                 struct mlx5_flow_rss_desc rss_desc_v[MLX5_MTR_RTE_COLORS];
4687                 struct mlx5_flow_rss_desc *rss_desc[MLX5_MTR_RTE_COLORS] = {0};
4688                 uint32_t i;
4689
4690                 /*
4691                  * This is a tmp dev_flow,
4692                  * no need to register any matcher for it in translate.
4693                  */
4694                 wks->skip_matcher_reg = 1;
4695                 for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
4696                         struct mlx5_flow dev_flow = {0};
4697                         struct mlx5_flow_handle dev_handle = { {0} };
4698                         uint8_t fate = final_policy->act_cnt[i].fate_action;
4699
4700                         if (fate == MLX5_FLOW_FATE_SHARED_RSS) {
4701                                 const void *rss_act =
4702                                         final_policy->act_cnt[i].rss->conf;
4703                                 struct rte_flow_action rss_actions[2] = {
4704                                         [0] = {
4705                                         .type = RTE_FLOW_ACTION_TYPE_RSS,
4706                                         .conf = rss_act,
4707                                         },
4708                                         [1] = {
4709                                         .type = RTE_FLOW_ACTION_TYPE_END,
4710                                         .conf = NULL,
4711                                         }
4712                                 };
4713
4714                                 dev_flow.handle = &dev_handle;
4715                                 dev_flow.ingress = attr->ingress;
4716                                 dev_flow.flow = flow;
4717                                 dev_flow.external = 0;
4718 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
4719                                 dev_flow.dv.transfer = attr->transfer;
4720 #endif
4721                                 /**
4722                                  * Translate RSS action to get rss hash fields.
4723                                  */
4724                                 if (flow_drv_translate(dev, &dev_flow, attr,
4725                                                 items, rss_actions, error))
4726                                         goto exit;
4727                                 rss_desc_v[i] = wks->rss_desc;
4728                                 rss_desc_v[i].key_len = MLX5_RSS_HASH_KEY_LEN;
4729                                 rss_desc_v[i].hash_fields =
4730                                                 dev_flow.hash_fields;
4731                                 rss_desc_v[i].queue_num =
4732                                                 rss_desc_v[i].hash_fields ?
4733                                                 rss_desc_v[i].queue_num : 1;
4734                                 rss_desc_v[i].tunnel =
4735                                                 !!(dev_flow.handle->layers &
4736                                                    MLX5_FLOW_LAYER_TUNNEL);
4737                                 rss_desc[i] = &rss_desc_v[i];
4738                         } else if (fate == MLX5_FLOW_FATE_QUEUE) {
4739                                 /* This is queue action. */
4740                                 rss_desc_v[i] = wks->rss_desc;
4741                                 rss_desc_v[i].key_len = 0;
4742                                 rss_desc_v[i].hash_fields = 0;
4743                                 rss_desc_v[i].queue =
4744                                         &final_policy->act_cnt[i].queue;
4745                                 rss_desc_v[i].queue_num = 1;
4746                                 rss_desc[i] = &rss_desc_v[i];
4747                         } else {
4748                                 rss_desc[i] = NULL;
4749                         }
4750                 }
4751                 sub_policy = flow_drv_meter_sub_policy_rss_prepare(dev,
4752                                                 flow, policy, rss_desc);
4753         } else {
4754                 enum mlx5_meter_domain mtr_domain =
4755                         attr->transfer ? MLX5_MTR_DOMAIN_TRANSFER :
4756                                 (attr->egress ? MLX5_MTR_DOMAIN_EGRESS :
4757                                                 MLX5_MTR_DOMAIN_INGRESS);
4758                 sub_policy = policy->sub_policys[mtr_domain][0];
4759         }
4760         if (!sub_policy)
4761                 rte_flow_error_set(error, EINVAL,
4762                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4763                                    "Failed to get meter sub-policy.");
4764 exit:
4765         return sub_policy;
4766 }
4767
4768 /**
4769  * Split the meter flow.
4770  *
4771  * As meter flow will split to three sub flow, other than meter
4772  * action, the other actions make sense to only meter accepts
4773  * the packet. If it need to be dropped, no other additional
4774  * actions should be take.
4775  *
4776  * One kind of special action which decapsulates the L3 tunnel
4777  * header will be in the prefix sub flow, as not to take the
4778  * L3 tunnel header into account.
4779  *
4780  * @param[in] dev
4781  *   Pointer to Ethernet device.
4782  * @param[in] flow
4783  *   Parent flow structure pointer.
4784  * @param wks
4785  *   Pointer to thread flow work space.
4786  * @param[in] attr
4787  *   Flow rule attributes.
4788  * @param[in] items
4789  *   Pattern specification (list terminated by the END pattern item).
4790  * @param[out] sfx_items
4791  *   Suffix flow match items (list terminated by the END pattern item).
4792  * @param[in] actions
4793  *   Associated actions (list terminated by the END action).
4794  * @param[out] actions_sfx
4795  *   Suffix flow actions.
4796  * @param[out] actions_pre
4797  *   Prefix flow actions.
4798  * @param[out] mtr_flow_id
4799  *   Pointer to meter flow id.
4800  * @param[out] error
4801  *   Perform verbose error reporting if not NULL.
4802  *
4803  * @return
4804  *   0 on success, a negative errno value otherwise and rte_errno is set.
4805  */
4806 static int
4807 flow_meter_split_prep(struct rte_eth_dev *dev,
4808                       struct rte_flow *flow,
4809                       struct mlx5_flow_workspace *wks,
4810                       const struct rte_flow_attr *attr,
4811                       const struct rte_flow_item items[],
4812                       struct rte_flow_item sfx_items[],
4813                       const struct rte_flow_action actions[],
4814                       struct rte_flow_action actions_sfx[],
4815                       struct rte_flow_action actions_pre[],
4816                       uint32_t *mtr_flow_id,
4817                       struct rte_flow_error *error)
4818 {
4819         struct mlx5_priv *priv = dev->data->dev_private;
4820         struct mlx5_flow_meter_info *fm = wks->fm;
4821         struct rte_flow_action *tag_action = NULL;
4822         struct rte_flow_item *tag_item;
4823         struct mlx5_rte_flow_action_set_tag *set_tag;
4824         const struct rte_flow_action_raw_encap *raw_encap;
4825         const struct rte_flow_action_raw_decap *raw_decap;
4826         struct mlx5_rte_flow_item_tag *tag_item_spec;
4827         struct mlx5_rte_flow_item_tag *tag_item_mask;
4828         uint32_t tag_id = 0;
4829         struct rte_flow_item *vlan_item_dst = NULL;
4830         const struct rte_flow_item *vlan_item_src = NULL;
4831         struct rte_flow_action *hw_mtr_action;
4832         struct rte_flow_action *action_pre_head = NULL;
4833         int32_t flow_src_port = priv->representor_id;
4834         bool mtr_first;
4835         uint8_t mtr_id_offset = priv->mtr_reg_share ? MLX5_MTR_COLOR_BITS : 0;
4836         uint8_t mtr_reg_bits = priv->mtr_reg_share ?
4837                                 MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
4838         uint32_t flow_id = 0;
4839         uint32_t flow_id_reversed = 0;
4840         uint8_t flow_id_bits = 0;
4841         int shift;
4842
4843         /* Prepare the suffix subflow items. */
4844         tag_item = sfx_items++;
4845         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
4846                 struct mlx5_priv *port_priv;
4847                 const struct rte_flow_item_port_id *pid_v;
4848                 int item_type = items->type;
4849
4850                 switch (item_type) {
4851                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
4852                         pid_v = items->spec;
4853                         MLX5_ASSERT(pid_v);
4854                         port_priv = mlx5_port_to_eswitch_info(pid_v->id, false);
4855                         if (!port_priv)
4856                                 return rte_flow_error_set(error,
4857                                                 rte_errno,
4858                                                 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
4859                                                 pid_v,
4860                                                 "Failed to get port info.");
4861                         flow_src_port = port_priv->representor_id;
4862                         if (!fm->def_policy && wks->policy->is_hierarchy &&
4863                             flow_src_port != priv->representor_id) {
4864                                 if (flow_drv_mtr_hierarchy_rule_create(dev,
4865                                                                 flow, fm,
4866                                                                 flow_src_port,
4867                                                                 items,
4868                                                                 error))
4869                                         return -rte_errno;
4870                         }
4871                         memcpy(sfx_items, items, sizeof(*sfx_items));
4872                         sfx_items++;
4873                         break;
4874                 case RTE_FLOW_ITEM_TYPE_VLAN:
4875                         /* Determine if copy vlan item below. */
4876                         vlan_item_src = items;
4877                         vlan_item_dst = sfx_items++;
4878                         vlan_item_dst->type = RTE_FLOW_ITEM_TYPE_VOID;
4879                         break;
4880                 default:
4881                         break;
4882                 }
4883         }
4884         sfx_items->type = RTE_FLOW_ITEM_TYPE_END;
4885         sfx_items++;
4886         mtr_first = priv->sh->meter_aso_en &&
4887                 (attr->egress || (attr->transfer && flow_src_port != UINT16_MAX));
4888         /* For ASO meter, meter must be before tag in TX direction. */
4889         if (mtr_first) {
4890                 action_pre_head = actions_pre++;
4891                 /* Leave space for tag action. */
4892                 tag_action = actions_pre++;
4893         }
4894         /* Prepare the actions for prefix and suffix flow. */
4895         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
4896                 struct rte_flow_action *action_cur = NULL;
4897
4898                 switch (actions->type) {
4899                 case RTE_FLOW_ACTION_TYPE_METER:
4900                         if (mtr_first) {
4901                                 action_cur = action_pre_head;
4902                         } else {
4903                                 /* Leave space for tag action. */
4904                                 tag_action = actions_pre++;
4905                                 action_cur = actions_pre++;
4906                         }
4907                         break;
4908                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
4909                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
4910                         action_cur = actions_pre++;
4911                         break;
4912                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
4913                         raw_encap = actions->conf;
4914                         if (raw_encap->size < MLX5_ENCAPSULATION_DECISION_SIZE)
4915                                 action_cur = actions_pre++;
4916                         break;
4917                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
4918                         raw_decap = actions->conf;
4919                         if (raw_decap->size > MLX5_ENCAPSULATION_DECISION_SIZE)
4920                                 action_cur = actions_pre++;
4921                         break;
4922                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
4923                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
4924                         if (vlan_item_dst && vlan_item_src) {
4925                                 memcpy(vlan_item_dst, vlan_item_src,
4926                                         sizeof(*vlan_item_dst));
4927                                 /*
4928                                  * Convert to internal match item, it is used
4929                                  * for vlan push and set vid.
4930                                  */
4931                                 vlan_item_dst->type = (enum rte_flow_item_type)
4932                                                 MLX5_RTE_FLOW_ITEM_TYPE_VLAN;
4933                         }
4934                         break;
4935                 default:
4936                         break;
4937                 }
4938                 if (!action_cur)
4939                         action_cur = (fm->def_policy) ?
4940                                         actions_sfx++ : actions_pre++;
4941                 memcpy(action_cur, actions, sizeof(struct rte_flow_action));
4942         }
4943         /* Add end action to the actions. */
4944         actions_sfx->type = RTE_FLOW_ACTION_TYPE_END;
4945         if (priv->sh->meter_aso_en) {
4946                 /**
4947                  * For ASO meter, need to add an extra jump action explicitly,
4948                  * to jump from meter to policer table.
4949                  */
4950                 struct mlx5_flow_meter_sub_policy *sub_policy;
4951                 struct mlx5_flow_tbl_data_entry *tbl_data;
4952
4953                 if (!fm->def_policy) {
4954                         sub_policy = get_meter_sub_policy(dev, flow, wks,
4955                                                           attr, items, error);
4956                         if (!sub_policy)
4957                                 return -rte_errno;
4958                 } else {
4959                         enum mlx5_meter_domain mtr_domain =
4960                         attr->transfer ? MLX5_MTR_DOMAIN_TRANSFER :
4961                                 (attr->egress ? MLX5_MTR_DOMAIN_EGRESS :
4962                                                 MLX5_MTR_DOMAIN_INGRESS);
4963
4964                         sub_policy =
4965                         &priv->sh->mtrmng->def_policy[mtr_domain]->sub_policy;
4966                 }
4967                 tbl_data = container_of(sub_policy->tbl_rsc,
4968                                         struct mlx5_flow_tbl_data_entry, tbl);
4969                 hw_mtr_action = actions_pre++;
4970                 hw_mtr_action->type = (enum rte_flow_action_type)
4971                                       MLX5_RTE_FLOW_ACTION_TYPE_JUMP;
4972                 hw_mtr_action->conf = tbl_data->jump.action;
4973         }
4974         actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
4975         actions_pre++;
4976         if (!tag_action)
4977                 return rte_flow_error_set(error, ENOMEM,
4978                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4979                                           NULL, "No tag action space.");
4980         if (!mtr_flow_id) {
4981                 tag_action->type = RTE_FLOW_ACTION_TYPE_VOID;
4982                 goto exit;
4983         }
4984         /* Only default-policy Meter creates mtr flow id. */
4985         if (fm->def_policy) {
4986                 mlx5_ipool_malloc(fm->flow_ipool, &tag_id);
4987                 if (!tag_id)
4988                         return rte_flow_error_set(error, ENOMEM,
4989                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4990                                         "Failed to allocate meter flow id.");
4991                 flow_id = tag_id - 1;
4992                 flow_id_bits = (!flow_id) ? 1 :
4993                                 (MLX5_REG_BITS - __builtin_clz(flow_id));
4994                 if ((flow_id_bits + priv->sh->mtrmng->max_mtr_bits) >
4995                     mtr_reg_bits) {
4996                         mlx5_ipool_free(fm->flow_ipool, tag_id);
4997                         return rte_flow_error_set(error, EINVAL,
4998                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4999                                         "Meter flow id exceeds max limit.");
5000                 }
5001                 if (flow_id_bits > priv->sh->mtrmng->max_mtr_flow_bits)
5002                         priv->sh->mtrmng->max_mtr_flow_bits = flow_id_bits;
5003         }
5004         /* Build tag actions and items for meter_id/meter flow_id. */
5005         set_tag = (struct mlx5_rte_flow_action_set_tag *)actions_pre;
5006         tag_item_spec = (struct mlx5_rte_flow_item_tag *)sfx_items;
5007         tag_item_mask = tag_item_spec + 1;
5008         /* Both flow_id and meter_id share the same register. */
5009         *set_tag = (struct mlx5_rte_flow_action_set_tag) {
5010                 .id = (enum modify_reg)mlx5_flow_get_reg_id(dev, MLX5_MTR_ID,
5011                                                             0, error),
5012                 .offset = mtr_id_offset,
5013                 .length = mtr_reg_bits,
5014                 .data = flow->meter,
5015         };
5016         /*
5017          * The color Reg bits used by flow_id are growing from
5018          * msb to lsb, so must do bit reverse for flow_id val in RegC.
5019          */
5020         for (shift = 0; shift < flow_id_bits; shift++)
5021                 flow_id_reversed = (flow_id_reversed << 1) |
5022                                 ((flow_id >> shift) & 0x1);
5023         set_tag->data |=
5024                 flow_id_reversed << (mtr_reg_bits - flow_id_bits);
5025         tag_item_spec->id = set_tag->id;
5026         tag_item_spec->data = set_tag->data << mtr_id_offset;
5027         tag_item_mask->data = UINT32_MAX << mtr_id_offset;
5028         tag_action->type = (enum rte_flow_action_type)
5029                                 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
5030         tag_action->conf = set_tag;
5031         tag_item->type = (enum rte_flow_item_type)
5032                                 MLX5_RTE_FLOW_ITEM_TYPE_TAG;
5033         tag_item->spec = tag_item_spec;
5034         tag_item->last = NULL;
5035         tag_item->mask = tag_item_mask;
5036 exit:
5037         if (mtr_flow_id)
5038                 *mtr_flow_id = tag_id;
5039         return 0;
5040 }
5041
5042 /**
5043  * Split action list having QUEUE/RSS for metadata register copy.
5044  *
5045  * Once Q/RSS action is detected in user's action list, the flow action
5046  * should be split in order to copy metadata registers, which will happen in
5047  * RX_CP_TBL like,
5048  *   - CQE->flow_tag := reg_c[1] (MARK)
5049  *   - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
5050  * The Q/RSS action will be performed on RX_ACT_TBL after passing by RX_CP_TBL.
5051  * This is because the last action of each flow must be a terminal action
5052  * (QUEUE, RSS or DROP).
5053  *
5054  * Flow ID must be allocated to identify actions in the RX_ACT_TBL and it is
5055  * stored and kept in the mlx5_flow structure per each sub_flow.
5056  *
5057  * The Q/RSS action is replaced with,
5058  *   - SET_TAG, setting the allocated flow ID to reg_c[2].
5059  * And the following JUMP action is added at the end,
5060  *   - JUMP, to RX_CP_TBL.
5061  *
5062  * A flow to perform remained Q/RSS action will be created in RX_ACT_TBL by
5063  * flow_create_split_metadata() routine. The flow will look like,
5064  *   - If flow ID matches (reg_c[2]), perform Q/RSS.
5065  *
5066  * @param dev
5067  *   Pointer to Ethernet device.
5068  * @param[out] split_actions
5069  *   Pointer to store split actions to jump to CP_TBL.
5070  * @param[in] actions
5071  *   Pointer to the list of original flow actions.
5072  * @param[in] qrss
5073  *   Pointer to the Q/RSS action.
5074  * @param[in] actions_n
5075  *   Number of original actions.
5076  * @param[out] error
5077  *   Perform verbose error reporting if not NULL.
5078  *
5079  * @return
5080  *   non-zero unique flow_id on success, otherwise 0 and
5081  *   error/rte_error are set.
5082  */
5083 static uint32_t
5084 flow_mreg_split_qrss_prep(struct rte_eth_dev *dev,
5085                           struct rte_flow_action *split_actions,
5086                           const struct rte_flow_action *actions,
5087                           const struct rte_flow_action *qrss,
5088                           int actions_n, struct rte_flow_error *error)
5089 {
5090         struct mlx5_priv *priv = dev->data->dev_private;
5091         struct mlx5_rte_flow_action_set_tag *set_tag;
5092         struct rte_flow_action_jump *jump;
5093         const int qrss_idx = qrss - actions;
5094         uint32_t flow_id = 0;
5095         int ret = 0;
5096
5097         /*
5098          * Given actions will be split
5099          * - Replace QUEUE/RSS action with SET_TAG to set flow ID.
5100          * - Add jump to mreg CP_TBL.
5101          * As a result, there will be one more action.
5102          */
5103         ++actions_n;
5104         memcpy(split_actions, actions, sizeof(*split_actions) * actions_n);
5105         set_tag = (void *)(split_actions + actions_n);
5106         /*
5107          * If tag action is not set to void(it means we are not the meter
5108          * suffix flow), add the tag action. Since meter suffix flow already
5109          * has the tag added.
5110          */
5111         if (split_actions[qrss_idx].type != RTE_FLOW_ACTION_TYPE_VOID) {
5112                 /*
5113                  * Allocate the new subflow ID. This one is unique within
5114                  * device and not shared with representors. Otherwise,
5115                  * we would have to resolve multi-thread access synch
5116                  * issue. Each flow on the shared device is appended
5117                  * with source vport identifier, so the resulting
5118                  * flows will be unique in the shared (by master and
5119                  * representors) domain even if they have coinciding
5120                  * IDs.
5121                  */
5122                 mlx5_ipool_malloc(priv->sh->ipool
5123                                   [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], &flow_id);
5124                 if (!flow_id)
5125                         return rte_flow_error_set(error, ENOMEM,
5126                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5127                                                   NULL, "can't allocate id "
5128                                                   "for split Q/RSS subflow");
5129                 /* Internal SET_TAG action to set flow ID. */
5130                 *set_tag = (struct mlx5_rte_flow_action_set_tag){
5131                         .data = flow_id,
5132                 };
5133                 ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0, error);
5134                 if (ret < 0)
5135                         return ret;
5136                 set_tag->id = ret;
5137                 /* Construct new actions array. */
5138                 /* Replace QUEUE/RSS action. */
5139                 split_actions[qrss_idx] = (struct rte_flow_action){
5140                         .type = (enum rte_flow_action_type)
5141                                 MLX5_RTE_FLOW_ACTION_TYPE_TAG,
5142                         .conf = set_tag,
5143                 };
5144         }
5145         /* JUMP action to jump to mreg copy table (CP_TBL). */
5146         jump = (void *)(set_tag + 1);
5147         *jump = (struct rte_flow_action_jump){
5148                 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
5149         };
5150         split_actions[actions_n - 2] = (struct rte_flow_action){
5151                 .type = RTE_FLOW_ACTION_TYPE_JUMP,
5152                 .conf = jump,
5153         };
5154         split_actions[actions_n - 1] = (struct rte_flow_action){
5155                 .type = RTE_FLOW_ACTION_TYPE_END,
5156         };
5157         return flow_id;
5158 }
5159
5160 /**
5161  * Extend the given action list for Tx metadata copy.
5162  *
5163  * Copy the given action list to the ext_actions and add flow metadata register
5164  * copy action in order to copy reg_a set by WQE to reg_c[0].
5165  *
5166  * @param[out] ext_actions
5167  *   Pointer to the extended action list.
5168  * @param[in] actions
5169  *   Pointer to the list of actions.
5170  * @param[in] actions_n
5171  *   Number of actions in the list.
5172  * @param[out] error
5173  *   Perform verbose error reporting if not NULL.
5174  * @param[in] encap_idx
5175  *   The encap action inndex.
5176  *
5177  * @return
5178  *   0 on success, negative value otherwise
5179  */
5180 static int
5181 flow_mreg_tx_copy_prep(struct rte_eth_dev *dev,
5182                        struct rte_flow_action *ext_actions,
5183                        const struct rte_flow_action *actions,
5184                        int actions_n, struct rte_flow_error *error,
5185                        int encap_idx)
5186 {
5187         struct mlx5_flow_action_copy_mreg *cp_mreg =
5188                 (struct mlx5_flow_action_copy_mreg *)
5189                         (ext_actions + actions_n + 1);
5190         int ret;
5191
5192         ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
5193         if (ret < 0)
5194                 return ret;
5195         cp_mreg->dst = ret;
5196         ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_TX, 0, error);
5197         if (ret < 0)
5198                 return ret;
5199         cp_mreg->src = ret;
5200         if (encap_idx != 0)
5201                 memcpy(ext_actions, actions, sizeof(*ext_actions) * encap_idx);
5202         if (encap_idx == actions_n - 1) {
5203                 ext_actions[actions_n - 1] = (struct rte_flow_action){
5204                         .type = (enum rte_flow_action_type)
5205                                 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
5206                         .conf = cp_mreg,
5207                 };
5208                 ext_actions[actions_n] = (struct rte_flow_action){
5209                         .type = RTE_FLOW_ACTION_TYPE_END,
5210                 };
5211         } else {
5212                 ext_actions[encap_idx] = (struct rte_flow_action){
5213                         .type = (enum rte_flow_action_type)
5214                                 MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
5215                         .conf = cp_mreg,
5216                 };
5217                 memcpy(ext_actions + encap_idx + 1, actions + encap_idx,
5218                                 sizeof(*ext_actions) * (actions_n - encap_idx));
5219         }
5220         return 0;
5221 }
5222
5223 /**
5224  * Check the match action from the action list.
5225  *
5226  * @param[in] actions
5227  *   Pointer to the list of actions.
5228  * @param[in] attr
5229  *   Flow rule attributes.
5230  * @param[in] action
5231  *   The action to be check if exist.
5232  * @param[out] match_action_pos
5233  *   Pointer to the position of the matched action if exists, otherwise is -1.
5234  * @param[out] qrss_action_pos
5235  *   Pointer to the position of the Queue/RSS action if exists, otherwise is -1.
5236  * @param[out] modify_after_mirror
5237  *   Pointer to the flag of modify action after FDB mirroring.
5238  *
5239  * @return
5240  *   > 0 the total number of actions.
5241  *   0 if not found match action in action list.
5242  */
5243 static int
5244 flow_check_match_action(const struct rte_flow_action actions[],
5245                         const struct rte_flow_attr *attr,
5246                         enum rte_flow_action_type action,
5247                         int *match_action_pos, int *qrss_action_pos,
5248                         int *modify_after_mirror)
5249 {
5250         const struct rte_flow_action_sample *sample;
5251         int actions_n = 0;
5252         uint32_t ratio = 0;
5253         int sub_type = 0;
5254         int flag = 0;
5255         int fdb_mirror = 0;
5256
5257         *match_action_pos = -1;
5258         *qrss_action_pos = -1;
5259         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5260                 if (actions->type == action) {
5261                         flag = 1;
5262                         *match_action_pos = actions_n;
5263                 }
5264                 switch (actions->type) {
5265                 case RTE_FLOW_ACTION_TYPE_QUEUE:
5266                 case RTE_FLOW_ACTION_TYPE_RSS:
5267                         *qrss_action_pos = actions_n;
5268                         break;
5269                 case RTE_FLOW_ACTION_TYPE_SAMPLE:
5270                         sample = actions->conf;
5271                         ratio = sample->ratio;
5272                         sub_type = ((const struct rte_flow_action *)
5273                                         (sample->actions))->type;
5274                         if (ratio == 1 && attr->transfer)
5275                                 fdb_mirror = 1;
5276                         break;
5277                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
5278                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
5279                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
5280                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
5281                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
5282                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
5283                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
5284                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
5285                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
5286                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
5287                 case RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ:
5288                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ:
5289                 case RTE_FLOW_ACTION_TYPE_INC_TCP_ACK:
5290                 case RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK:
5291                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP:
5292                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP:
5293                 case RTE_FLOW_ACTION_TYPE_FLAG:
5294                 case RTE_FLOW_ACTION_TYPE_MARK:
5295                 case RTE_FLOW_ACTION_TYPE_SET_META:
5296                 case RTE_FLOW_ACTION_TYPE_SET_TAG:
5297                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
5298                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
5299                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
5300                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
5301                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
5302                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
5303                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
5304                 case RTE_FLOW_ACTION_TYPE_MODIFY_FIELD:
5305                 case RTE_FLOW_ACTION_TYPE_METER:
5306                         if (fdb_mirror)
5307                                 *modify_after_mirror = 1;
5308                         break;
5309                 default:
5310                         break;
5311                 }
5312                 actions_n++;
5313         }
5314         if (flag && fdb_mirror && !*modify_after_mirror) {
5315                 /* FDB mirroring uses the destination array to implement
5316                  * instead of FLOW_SAMPLER object.
5317                  */
5318                 if (sub_type != RTE_FLOW_ACTION_TYPE_END)
5319                         flag = 0;
5320         }
5321         /* Count RTE_FLOW_ACTION_TYPE_END. */
5322         return flag ? actions_n + 1 : 0;
5323 }
5324
5325 #define SAMPLE_SUFFIX_ITEM 2
5326
5327 /**
5328  * Split the sample flow.
5329  *
5330  * As sample flow will split to two sub flow, sample flow with
5331  * sample action, the other actions will move to new suffix flow.
5332  *
5333  * Also add unique tag id with tag action in the sample flow,
5334  * the same tag id will be as match in the suffix flow.
5335  *
5336  * @param dev
5337  *   Pointer to Ethernet device.
5338  * @param[in] add_tag
5339  *   Add extra tag action flag.
5340  * @param[out] sfx_items
5341  *   Suffix flow match items (list terminated by the END pattern item).
5342  * @param[in] actions
5343  *   Associated actions (list terminated by the END action).
5344  * @param[out] actions_sfx
5345  *   Suffix flow actions.
5346  * @param[out] actions_pre
5347  *   Prefix flow actions.
5348  * @param[in] actions_n
5349  *  The total number of actions.
5350  * @param[in] sample_action_pos
5351  *   The sample action position.
5352  * @param[in] qrss_action_pos
5353  *   The Queue/RSS action position.
5354  * @param[in] jump_table
5355  *   Add extra jump action flag.
5356  * @param[out] error
5357  *   Perform verbose error reporting if not NULL.
5358  *
5359  * @return
5360  *   0 on success, or unique flow_id, a negative errno value
5361  *   otherwise and rte_errno is set.
5362  */
5363 static int
5364 flow_sample_split_prep(struct rte_eth_dev *dev,
5365                        int add_tag,
5366                        struct rte_flow_item sfx_items[],
5367                        const struct rte_flow_action actions[],
5368                        struct rte_flow_action actions_sfx[],
5369                        struct rte_flow_action actions_pre[],
5370                        int actions_n,
5371                        int sample_action_pos,
5372                        int qrss_action_pos,
5373                        int jump_table,
5374                        struct rte_flow_error *error)
5375 {
5376         struct mlx5_priv *priv = dev->data->dev_private;
5377         struct mlx5_rte_flow_action_set_tag *set_tag;
5378         struct mlx5_rte_flow_item_tag *tag_spec;
5379         struct mlx5_rte_flow_item_tag *tag_mask;
5380         struct rte_flow_action_jump *jump_action;
5381         uint32_t tag_id = 0;
5382         int index;
5383         int append_index = 0;
5384         int ret;
5385
5386         if (sample_action_pos < 0)
5387                 return rte_flow_error_set(error, EINVAL,
5388                                           RTE_FLOW_ERROR_TYPE_ACTION,
5389                                           NULL, "invalid position of sample "
5390                                           "action in list");
5391         /* Prepare the actions for prefix and suffix flow. */
5392         if (qrss_action_pos >= 0 && qrss_action_pos < sample_action_pos) {
5393                 index = qrss_action_pos;
5394                 /* Put the preceding the Queue/RSS action into prefix flow. */
5395                 if (index != 0)
5396                         memcpy(actions_pre, actions,
5397                                sizeof(struct rte_flow_action) * index);
5398                 /* Put others preceding the sample action into prefix flow. */
5399                 if (sample_action_pos > index + 1)
5400                         memcpy(actions_pre + index, actions + index + 1,
5401                                sizeof(struct rte_flow_action) *
5402                                (sample_action_pos - index - 1));
5403                 index = sample_action_pos - 1;
5404                 /* Put Queue/RSS action into Suffix flow. */
5405                 memcpy(actions_sfx, actions + qrss_action_pos,
5406                        sizeof(struct rte_flow_action));
5407                 actions_sfx++;
5408         } else {
5409                 index = sample_action_pos;
5410                 if (index != 0)
5411                         memcpy(actions_pre, actions,
5412                                sizeof(struct rte_flow_action) * index);
5413         }
5414         /* For CX5, add an extra tag action for NIC-RX and E-Switch ingress.
5415          * For CX6DX and above, metadata registers Cx preserve their value,
5416          * add an extra tag action for NIC-RX and E-Switch Domain.
5417          */
5418         if (add_tag) {
5419                 /* Prepare the prefix tag action. */
5420                 append_index++;
5421                 set_tag = (void *)(actions_pre + actions_n + append_index);
5422                 ret = mlx5_flow_get_reg_id(dev, MLX5_APP_TAG, 0, error);
5423                 if (ret < 0)
5424                         return ret;
5425                 mlx5_ipool_malloc(priv->sh->ipool
5426                                   [MLX5_IPOOL_RSS_EXPANTION_FLOW_ID], &tag_id);
5427                 *set_tag = (struct mlx5_rte_flow_action_set_tag) {
5428                         .id = ret,
5429                         .data = tag_id,
5430                 };
5431                 /* Prepare the suffix subflow items. */
5432                 tag_spec = (void *)(sfx_items + SAMPLE_SUFFIX_ITEM);
5433                 tag_spec->data = tag_id;
5434                 tag_spec->id = set_tag->id;
5435                 tag_mask = tag_spec + 1;
5436                 tag_mask->data = UINT32_MAX;
5437                 sfx_items[0] = (struct rte_flow_item){
5438                         .type = (enum rte_flow_item_type)
5439                                 MLX5_RTE_FLOW_ITEM_TYPE_TAG,
5440                         .spec = tag_spec,
5441                         .last = NULL,
5442                         .mask = tag_mask,
5443                 };
5444                 sfx_items[1] = (struct rte_flow_item){
5445                         .type = (enum rte_flow_item_type)
5446                                 RTE_FLOW_ITEM_TYPE_END,
5447                 };
5448                 /* Prepare the tag action in prefix subflow. */
5449                 actions_pre[index++] =
5450                         (struct rte_flow_action){
5451                         .type = (enum rte_flow_action_type)
5452                                 MLX5_RTE_FLOW_ACTION_TYPE_TAG,
5453                         .conf = set_tag,
5454                 };
5455         }
5456         memcpy(actions_pre + index, actions + sample_action_pos,
5457                sizeof(struct rte_flow_action));
5458         index += 1;
5459         /* For the modify action after the sample action in E-Switch mirroring,
5460          * Add the extra jump action in prefix subflow and jump into the next
5461          * table, then do the modify action in the new table.
5462          */
5463         if (jump_table) {
5464                 /* Prepare the prefix jump action. */
5465                 append_index++;
5466                 jump_action = (void *)(actions_pre + actions_n + append_index);
5467                 jump_action->group = jump_table;
5468                 actions_pre[index++] =
5469                         (struct rte_flow_action){
5470                         .type = (enum rte_flow_action_type)
5471                                 RTE_FLOW_ACTION_TYPE_JUMP,
5472                         .conf = jump_action,
5473                 };
5474         }
5475         actions_pre[index] = (struct rte_flow_action){
5476                 .type = (enum rte_flow_action_type)
5477                         RTE_FLOW_ACTION_TYPE_END,
5478         };
5479         /* Put the actions after sample into Suffix flow. */
5480         memcpy(actions_sfx, actions + sample_action_pos + 1,
5481                sizeof(struct rte_flow_action) *
5482                (actions_n - sample_action_pos - 1));
5483         return tag_id;
5484 }
5485
5486 /**
5487  * The splitting for metadata feature.
5488  *
5489  * - Q/RSS action on NIC Rx should be split in order to pass by
5490  *   the mreg copy table (RX_CP_TBL) and then it jumps to the
5491  *   action table (RX_ACT_TBL) which has the split Q/RSS action.
5492  *
5493  * - All the actions on NIC Tx should have a mreg copy action to
5494  *   copy reg_a from WQE to reg_c[0].
5495  *
5496  * @param dev
5497  *   Pointer to Ethernet device.
5498  * @param[in] flow
5499  *   Parent flow structure pointer.
5500  * @param[in] attr
5501  *   Flow rule attributes.
5502  * @param[in] items
5503  *   Pattern specification (list terminated by the END pattern item).
5504  * @param[in] actions
5505  *   Associated actions (list terminated by the END action).
5506  * @param[in] flow_split_info
5507  *   Pointer to flow split info structure.
5508  * @param[out] error
5509  *   Perform verbose error reporting if not NULL.
5510  * @return
5511  *   0 on success, negative value otherwise
5512  */
5513 static int
5514 flow_create_split_metadata(struct rte_eth_dev *dev,
5515                            struct rte_flow *flow,
5516                            const struct rte_flow_attr *attr,
5517                            const struct rte_flow_item items[],
5518                            const struct rte_flow_action actions[],
5519                            struct mlx5_flow_split_info *flow_split_info,
5520                            struct rte_flow_error *error)
5521 {
5522         struct mlx5_priv *priv = dev->data->dev_private;
5523         struct mlx5_dev_config *config = &priv->config;
5524         const struct rte_flow_action *qrss = NULL;
5525         struct rte_flow_action *ext_actions = NULL;
5526         struct mlx5_flow *dev_flow = NULL;
5527         uint32_t qrss_id = 0;
5528         int mtr_sfx = 0;
5529         size_t act_size;
5530         int actions_n;
5531         int encap_idx;
5532         int ret;
5533
5534         /* Check whether extensive metadata feature is engaged. */
5535         if (!config->dv_flow_en ||
5536             config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
5537             !mlx5_flow_ext_mreg_supported(dev))
5538                 return flow_create_split_inner(dev, flow, NULL, attr, items,
5539                                                actions, flow_split_info, error);
5540         actions_n = flow_parse_metadata_split_actions_info(actions, &qrss,
5541                                                            &encap_idx);
5542         if (qrss) {
5543                 /* Exclude hairpin flows from splitting. */
5544                 if (qrss->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
5545                         const struct rte_flow_action_queue *queue;
5546
5547                         queue = qrss->conf;
5548                         if (mlx5_rxq_get_type(dev, queue->index) ==
5549                             MLX5_RXQ_TYPE_HAIRPIN)
5550                                 qrss = NULL;
5551                 } else if (qrss->type == RTE_FLOW_ACTION_TYPE_RSS) {
5552                         const struct rte_flow_action_rss *rss;
5553
5554                         rss = qrss->conf;
5555                         if (mlx5_rxq_get_type(dev, rss->queue[0]) ==
5556                             MLX5_RXQ_TYPE_HAIRPIN)
5557                                 qrss = NULL;
5558                 }
5559         }
5560         if (qrss) {
5561                 /* Check if it is in meter suffix table. */
5562                 mtr_sfx = attr->group == (attr->transfer ?
5563                           (MLX5_FLOW_TABLE_LEVEL_METER - 1) :
5564                           MLX5_FLOW_TABLE_LEVEL_METER);
5565                 /*
5566                  * Q/RSS action on NIC Rx should be split in order to pass by
5567                  * the mreg copy table (RX_CP_TBL) and then it jumps to the
5568                  * action table (RX_ACT_TBL) which has the split Q/RSS action.
5569                  */
5570                 act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
5571                            sizeof(struct rte_flow_action_set_tag) +
5572                            sizeof(struct rte_flow_action_jump);
5573                 ext_actions = mlx5_malloc(MLX5_MEM_ZERO, act_size, 0,
5574                                           SOCKET_ID_ANY);
5575                 if (!ext_actions)
5576                         return rte_flow_error_set(error, ENOMEM,
5577                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5578                                                   NULL, "no memory to split "
5579                                                   "metadata flow");
5580                 /*
5581                  * If we are the suffix flow of meter, tag already exist.
5582                  * Set the tag action to void.
5583                  */
5584                 if (mtr_sfx)
5585                         ext_actions[qrss - actions].type =
5586                                                 RTE_FLOW_ACTION_TYPE_VOID;
5587                 else
5588                         ext_actions[qrss - actions].type =
5589                                                 (enum rte_flow_action_type)
5590                                                 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
5591                 /*
5592                  * Create the new actions list with removed Q/RSS action
5593                  * and appended set tag and jump to register copy table
5594                  * (RX_CP_TBL). We should preallocate unique tag ID here
5595                  * in advance, because it is needed for set tag action.
5596                  */
5597                 qrss_id = flow_mreg_split_qrss_prep(dev, ext_actions, actions,
5598                                                     qrss, actions_n, error);
5599                 if (!mtr_sfx && !qrss_id) {
5600                         ret = -rte_errno;
5601                         goto exit;
5602                 }
5603         } else if (attr->egress && !attr->transfer) {
5604                 /*
5605                  * All the actions on NIC Tx should have a metadata register
5606                  * copy action to copy reg_a from WQE to reg_c[meta]
5607                  */
5608                 act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
5609                            sizeof(struct mlx5_flow_action_copy_mreg);
5610                 ext_actions = mlx5_malloc(MLX5_MEM_ZERO, act_size, 0,
5611                                           SOCKET_ID_ANY);
5612                 if (!ext_actions)
5613                         return rte_flow_error_set(error, ENOMEM,
5614                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5615                                                   NULL, "no memory to split "
5616                                                   "metadata flow");
5617                 /* Create the action list appended with copy register. */
5618                 ret = flow_mreg_tx_copy_prep(dev, ext_actions, actions,
5619                                              actions_n, error, encap_idx);
5620                 if (ret < 0)
5621                         goto exit;
5622         }
5623         /* Add the unmodified original or prefix subflow. */
5624         ret = flow_create_split_inner(dev, flow, &dev_flow, attr,
5625                                       items, ext_actions ? ext_actions :
5626                                       actions, flow_split_info, error);
5627         if (ret < 0)
5628                 goto exit;
5629         MLX5_ASSERT(dev_flow);
5630         if (qrss) {
5631                 const struct rte_flow_attr q_attr = {
5632                         .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
5633                         .ingress = 1,
5634                 };
5635                 /* Internal PMD action to set register. */
5636                 struct mlx5_rte_flow_item_tag q_tag_spec = {
5637                         .data = qrss_id,
5638                         .id = REG_NON,
5639                 };
5640                 struct rte_flow_item q_items[] = {
5641                         {
5642                                 .type = (enum rte_flow_item_type)
5643                                         MLX5_RTE_FLOW_ITEM_TYPE_TAG,
5644                                 .spec = &q_tag_spec,
5645                                 .last = NULL,
5646                                 .mask = NULL,
5647                         },
5648                         {
5649                                 .type = RTE_FLOW_ITEM_TYPE_END,
5650                         },
5651                 };
5652                 struct rte_flow_action q_actions[] = {
5653                         {
5654                                 .type = qrss->type,
5655                                 .conf = qrss->conf,
5656                         },
5657                         {
5658                                 .type = RTE_FLOW_ACTION_TYPE_END,
5659                         },
5660                 };
5661                 uint64_t layers = flow_get_prefix_layer_flags(dev_flow);
5662
5663                 /*
5664                  * Configure the tag item only if there is no meter subflow.
5665                  * Since tag is already marked in the meter suffix subflow
5666                  * we can just use the meter suffix items as is.
5667                  */
5668                 if (qrss_id) {
5669                         /* Not meter subflow. */
5670                         MLX5_ASSERT(!mtr_sfx);
5671                         /*
5672                          * Put unique id in prefix flow due to it is destroyed
5673                          * after suffix flow and id will be freed after there
5674                          * is no actual flows with this id and identifier
5675                          * reallocation becomes possible (for example, for
5676                          * other flows in other threads).
5677                          */
5678                         dev_flow->handle->split_flow_id = qrss_id;
5679                         ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0,
5680                                                    error);
5681                         if (ret < 0)
5682                                 goto exit;
5683                         q_tag_spec.id = ret;
5684                 }
5685                 dev_flow = NULL;
5686                 /* Add suffix subflow to execute Q/RSS. */
5687                 flow_split_info->prefix_layers = layers;
5688                 flow_split_info->prefix_mark = 0;
5689                 ret = flow_create_split_inner(dev, flow, &dev_flow,
5690                                               &q_attr, mtr_sfx ? items :
5691                                               q_items, q_actions,
5692                                               flow_split_info, error);
5693                 if (ret < 0)
5694                         goto exit;
5695                 /* qrss ID should be freed if failed. */
5696                 qrss_id = 0;
5697                 MLX5_ASSERT(dev_flow);
5698         }
5699
5700 exit:
5701         /*
5702          * We do not destroy the partially created sub_flows in case of error.
5703          * These ones are included into parent flow list and will be destroyed
5704          * by flow_drv_destroy.
5705          */
5706         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_RSS_EXPANTION_FLOW_ID],
5707                         qrss_id);
5708         mlx5_free(ext_actions);
5709         return ret;
5710 }
5711
5712 /**
5713  * Create meter internal drop flow with the original pattern.
5714  *
5715  * @param dev
5716  *   Pointer to Ethernet device.
5717  * @param[in] flow
5718  *   Parent flow structure pointer.
5719  * @param[in] attr
5720  *   Flow rule attributes.
5721  * @param[in] items
5722  *   Pattern specification (list terminated by the END pattern item).
5723  * @param[in] flow_split_info
5724  *   Pointer to flow split info structure.
5725  * @param[in] fm
5726  *   Pointer to flow meter structure.
5727  * @param[out] error
5728  *   Perform verbose error reporting if not NULL.
5729  * @return
5730  *   0 on success, negative value otherwise
5731  */
5732 static uint32_t
5733 flow_meter_create_drop_flow_with_org_pattern(struct rte_eth_dev *dev,
5734                         struct rte_flow *flow,
5735                         const struct rte_flow_attr *attr,
5736                         const struct rte_flow_item items[],
5737                         struct mlx5_flow_split_info *flow_split_info,
5738                         struct mlx5_flow_meter_info *fm,
5739                         struct rte_flow_error *error)
5740 {
5741         struct mlx5_flow *dev_flow = NULL;
5742         struct rte_flow_attr drop_attr = *attr;
5743         struct rte_flow_action drop_actions[3];
5744         struct mlx5_flow_split_info drop_split_info = *flow_split_info;
5745
5746         MLX5_ASSERT(fm->drop_cnt);
5747         drop_actions[0].type =
5748                 (enum rte_flow_action_type)MLX5_RTE_FLOW_ACTION_TYPE_COUNT;
5749         drop_actions[0].conf = (void *)(uintptr_t)fm->drop_cnt;
5750         drop_actions[1].type = RTE_FLOW_ACTION_TYPE_DROP;
5751         drop_actions[1].conf = NULL;
5752         drop_actions[2].type = RTE_FLOW_ACTION_TYPE_END;
5753         drop_actions[2].conf = NULL;
5754         drop_split_info.external = false;
5755         drop_split_info.skip_scale |= 1 << MLX5_SCALE_FLOW_GROUP_BIT;
5756         drop_split_info.table_id = MLX5_MTR_TABLE_ID_DROP;
5757         drop_attr.group = MLX5_FLOW_TABLE_LEVEL_METER;
5758         return flow_create_split_inner(dev, flow, &dev_flow,
5759                                 &drop_attr, items, drop_actions,
5760                                 &drop_split_info, error);
5761 }
5762
5763 /**
5764  * The splitting for meter feature.
5765  *
5766  * - The meter flow will be split to two flows as prefix and
5767  *   suffix flow. The packets make sense only it pass the prefix
5768  *   meter action.
5769  *
5770  * - Reg_C_5 is used for the packet to match betweend prefix and
5771  *   suffix flow.
5772  *
5773  * @param dev
5774  *   Pointer to Ethernet device.
5775  * @param[in] flow
5776  *   Parent flow structure pointer.
5777  * @param[in] attr
5778  *   Flow rule attributes.
5779  * @param[in] items
5780  *   Pattern specification (list terminated by the END pattern item).
5781  * @param[in] actions
5782  *   Associated actions (list terminated by the END action).
5783  * @param[in] flow_split_info
5784  *   Pointer to flow split info structure.
5785  * @param[out] error
5786  *   Perform verbose error reporting if not NULL.
5787  * @return
5788  *   0 on success, negative value otherwise
5789  */
5790 static int
5791 flow_create_split_meter(struct rte_eth_dev *dev,
5792                         struct rte_flow *flow,
5793                         const struct rte_flow_attr *attr,
5794                         const struct rte_flow_item items[],
5795                         const struct rte_flow_action actions[],
5796                         struct mlx5_flow_split_info *flow_split_info,
5797                         struct rte_flow_error *error)
5798 {
5799         struct mlx5_priv *priv = dev->data->dev_private;
5800         struct mlx5_flow_workspace *wks = mlx5_flow_get_thread_workspace();
5801         struct rte_flow_action *sfx_actions = NULL;
5802         struct rte_flow_action *pre_actions = NULL;
5803         struct rte_flow_item *sfx_items = NULL;
5804         struct mlx5_flow *dev_flow = NULL;
5805         struct rte_flow_attr sfx_attr = *attr;
5806         struct mlx5_flow_meter_info *fm = NULL;
5807         uint8_t skip_scale_restore;
5808         bool has_mtr = false;
5809         bool has_modify = false;
5810         bool set_mtr_reg = true;
5811         bool is_mtr_hierarchy = false;
5812         uint32_t meter_id = 0;
5813         uint32_t mtr_idx = 0;
5814         uint32_t mtr_flow_id = 0;
5815         size_t act_size;
5816         size_t item_size;
5817         int actions_n = 0;
5818         int ret = 0;
5819
5820         if (priv->mtr_en)
5821                 actions_n = flow_check_meter_action(dev, actions, &has_mtr,
5822                                                     &has_modify, &meter_id);
5823         if (has_mtr) {
5824                 if (flow->meter) {
5825                         fm = flow_dv_meter_find_by_idx(priv, flow->meter);
5826                         if (!fm)
5827                                 return rte_flow_error_set(error, EINVAL,
5828                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5829                                                 NULL, "Meter not found.");
5830                 } else {
5831                         fm = mlx5_flow_meter_find(priv, meter_id, &mtr_idx);
5832                         if (!fm)
5833                                 return rte_flow_error_set(error, EINVAL,
5834                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5835                                                 NULL, "Meter not found.");
5836                         ret = mlx5_flow_meter_attach(priv, fm,
5837                                                      &sfx_attr, error);
5838                         if (ret)
5839                                 return -rte_errno;
5840                         flow->meter = mtr_idx;
5841                 }
5842                 MLX5_ASSERT(wks);
5843                 wks->fm = fm;
5844                 if (!fm->def_policy) {
5845                         wks->policy = mlx5_flow_meter_policy_find(dev,
5846                                                                   fm->policy_id,
5847                                                                   NULL);
5848                         MLX5_ASSERT(wks->policy);
5849                         if (wks->policy->is_hierarchy) {
5850                                 wks->final_policy =
5851                                 mlx5_flow_meter_hierarchy_get_final_policy(dev,
5852                                                                 wks->policy);
5853                                 if (!wks->final_policy)
5854                                         return rte_flow_error_set(error,
5855                                         EINVAL,
5856                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
5857                                 "Failed to find terminal policy of hierarchy.");
5858                                 is_mtr_hierarchy = true;
5859                         }
5860                 }
5861                 /*
5862                  * If it isn't default-policy Meter, and
5863                  * 1. There's no action in flow to change
5864                  *    packet (modify/encap/decap etc.), OR
5865                  * 2. No drop count needed for this meter.
5866                  * 3. It's not meter hierarchy.
5867                  * Then no need to use regC to save meter id anymore.
5868                  */
5869                 if (!fm->def_policy && !is_mtr_hierarchy &&
5870                     (!has_modify || !fm->drop_cnt))
5871                         set_mtr_reg = false;
5872                 /* Prefix actions: meter, decap, encap, tag, jump, end. */
5873                 act_size = sizeof(struct rte_flow_action) * (actions_n + 6) +
5874                            sizeof(struct mlx5_rte_flow_action_set_tag);
5875                 /* Suffix items: tag, vlan, port id, end. */
5876 #define METER_SUFFIX_ITEM 4
5877                 item_size = sizeof(struct rte_flow_item) * METER_SUFFIX_ITEM +
5878                             sizeof(struct mlx5_rte_flow_item_tag) * 2;
5879                 sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size + item_size),
5880                                           0, SOCKET_ID_ANY);
5881                 if (!sfx_actions)
5882                         return rte_flow_error_set(error, ENOMEM,
5883                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5884                                                   NULL, "no memory to split "
5885                                                   "meter flow");
5886                 sfx_items = (struct rte_flow_item *)((char *)sfx_actions +
5887                              act_size);
5888                 /* There's no suffix flow for meter of non-default policy. */
5889                 if (!fm->def_policy)
5890                         pre_actions = sfx_actions + 1;
5891                 else
5892                         pre_actions = sfx_actions + actions_n;
5893                 ret = flow_meter_split_prep(dev, flow, wks, &sfx_attr,
5894                                             items, sfx_items, actions,
5895                                             sfx_actions, pre_actions,
5896                                             (set_mtr_reg ? &mtr_flow_id : NULL),
5897                                             error);
5898                 if (ret) {
5899                         ret = -rte_errno;
5900                         goto exit;
5901                 }
5902                 /* Add the prefix subflow. */
5903                 flow_split_info->prefix_mark = 0;
5904                 skip_scale_restore = flow_split_info->skip_scale;
5905                 flow_split_info->skip_scale |=
5906                         1 << MLX5_SCALE_JUMP_FLOW_GROUP_BIT;
5907                 ret = flow_create_split_inner(dev, flow, &dev_flow,
5908                                               attr, items, pre_actions,
5909                                               flow_split_info, error);
5910                 flow_split_info->skip_scale = skip_scale_restore;
5911                 if (ret) {
5912                         if (mtr_flow_id)
5913                                 mlx5_ipool_free(fm->flow_ipool, mtr_flow_id);
5914                         ret = -rte_errno;
5915                         goto exit;
5916                 }
5917                 if (mtr_flow_id) {
5918                         dev_flow->handle->split_flow_id = mtr_flow_id;
5919                         dev_flow->handle->is_meter_flow_id = 1;
5920                 }
5921                 if (!fm->def_policy) {
5922                         if (!set_mtr_reg && fm->drop_cnt)
5923                                 ret =
5924                         flow_meter_create_drop_flow_with_org_pattern(dev, flow,
5925                                                         &sfx_attr, items,
5926                                                         flow_split_info,
5927                                                         fm, error);
5928                         goto exit;
5929                 }
5930                 /* Setting the sfx group atrr. */
5931                 sfx_attr.group = sfx_attr.transfer ?
5932                                 (MLX5_FLOW_TABLE_LEVEL_METER - 1) :
5933                                  MLX5_FLOW_TABLE_LEVEL_METER;
5934                 flow_split_info->prefix_layers =
5935                                 flow_get_prefix_layer_flags(dev_flow);
5936                 flow_split_info->prefix_mark = dev_flow->handle->mark;
5937                 flow_split_info->table_id = MLX5_MTR_TABLE_ID_SUFFIX;
5938         }
5939         /* Add the prefix subflow. */
5940         ret = flow_create_split_metadata(dev, flow,
5941                                          &sfx_attr, sfx_items ?
5942                                          sfx_items : items,
5943                                          sfx_actions ? sfx_actions : actions,
5944                                          flow_split_info, error);
5945 exit:
5946         if (sfx_actions)
5947                 mlx5_free(sfx_actions);
5948         return ret;
5949 }
5950
5951 /**
5952  * The splitting for sample feature.
5953  *
5954  * Once Sample action is detected in the action list, the flow actions should
5955  * be split into prefix sub flow and suffix sub flow.
5956  *
5957  * The original items remain in the prefix sub flow, all actions preceding the
5958  * sample action and the sample action itself will be copied to the prefix
5959  * sub flow, the actions following the sample action will be copied to the
5960  * suffix sub flow, Queue action always be located in the suffix sub flow.
5961  *
5962  * In order to make the packet from prefix sub flow matches with suffix sub
5963  * flow, an extra tag action be added into prefix sub flow, and the suffix sub
5964  * flow uses tag item with the unique flow id.
5965  *
5966  * @param dev
5967  *   Pointer to Ethernet device.
5968  * @param[in] flow
5969  *   Parent flow structure pointer.
5970  * @param[in] attr
5971  *   Flow rule attributes.
5972  * @param[in] items
5973  *   Pattern specification (list terminated by the END pattern item).
5974  * @param[in] actions
5975  *   Associated actions (list terminated by the END action).
5976  * @param[in] flow_split_info
5977  *   Pointer to flow split info structure.
5978  * @param[out] error
5979  *   Perform verbose error reporting if not NULL.
5980  * @return
5981  *   0 on success, negative value otherwise
5982  */
5983 static int
5984 flow_create_split_sample(struct rte_eth_dev *dev,
5985                          struct rte_flow *flow,
5986                          const struct rte_flow_attr *attr,
5987                          const struct rte_flow_item items[],
5988                          const struct rte_flow_action actions[],
5989                          struct mlx5_flow_split_info *flow_split_info,
5990                          struct rte_flow_error *error)
5991 {
5992         struct mlx5_priv *priv = dev->data->dev_private;
5993         struct rte_flow_action *sfx_actions = NULL;
5994         struct rte_flow_action *pre_actions = NULL;
5995         struct rte_flow_item *sfx_items = NULL;
5996         struct mlx5_flow *dev_flow = NULL;
5997         struct rte_flow_attr sfx_attr = *attr;
5998 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
5999         struct mlx5_flow_dv_sample_resource *sample_res;
6000         struct mlx5_flow_tbl_data_entry *sfx_tbl_data;
6001         struct mlx5_flow_tbl_resource *sfx_tbl;
6002 #endif
6003         size_t act_size;
6004         size_t item_size;
6005         uint32_t fdb_tx = 0;
6006         int32_t tag_id = 0;
6007         int actions_n = 0;
6008         int sample_action_pos;
6009         int qrss_action_pos;
6010         int add_tag = 0;
6011         int modify_after_mirror = 0;
6012         uint16_t jump_table = 0;
6013         const uint32_t next_ft_step = 1;
6014         int ret = 0;
6015
6016         if (priv->sampler_en)
6017                 actions_n = flow_check_match_action(actions, attr,
6018                                         RTE_FLOW_ACTION_TYPE_SAMPLE,
6019                                         &sample_action_pos, &qrss_action_pos,
6020                                         &modify_after_mirror);
6021         if (actions_n) {
6022                 /* The prefix actions must includes sample, tag, end. */
6023                 act_size = sizeof(struct rte_flow_action) * (actions_n * 2 + 1)
6024                            + sizeof(struct mlx5_rte_flow_action_set_tag);
6025                 item_size = sizeof(struct rte_flow_item) * SAMPLE_SUFFIX_ITEM +
6026                             sizeof(struct mlx5_rte_flow_item_tag) * 2;
6027                 sfx_actions = mlx5_malloc(MLX5_MEM_ZERO, (act_size +
6028                                           item_size), 0, SOCKET_ID_ANY);
6029                 if (!sfx_actions)
6030                         return rte_flow_error_set(error, ENOMEM,
6031                                                   RTE_FLOW_ERROR_TYPE_ACTION,
6032                                                   NULL, "no memory to split "
6033                                                   "sample flow");
6034                 /* The representor_id is UINT16_MAX for uplink. */
6035                 fdb_tx = (attr->transfer && priv->representor_id != UINT16_MAX);
6036                 /*
6037                  * When reg_c_preserve is set, metadata registers Cx preserve
6038                  * their value even through packet duplication.
6039                  */
6040                 add_tag = (!fdb_tx || priv->config.hca_attr.reg_c_preserve);
6041                 if (add_tag)
6042                         sfx_items = (struct rte_flow_item *)((char *)sfx_actions
6043                                         + act_size);
6044                 if (modify_after_mirror)
6045                         jump_table = attr->group * MLX5_FLOW_TABLE_FACTOR +
6046                                      next_ft_step;
6047                 pre_actions = sfx_actions + actions_n;
6048                 tag_id = flow_sample_split_prep(dev, add_tag, sfx_items,
6049                                                 actions, sfx_actions,
6050                                                 pre_actions, actions_n,
6051                                                 sample_action_pos,
6052                                                 qrss_action_pos, jump_table,
6053                                                 error);
6054                 if (tag_id < 0 || (add_tag && !tag_id)) {
6055                         ret = -rte_errno;
6056                         goto exit;
6057                 }
6058                 if (modify_after_mirror)
6059                         flow_split_info->skip_scale =
6060                                         1 << MLX5_SCALE_JUMP_FLOW_GROUP_BIT;
6061                 /* Add the prefix subflow. */
6062                 ret = flow_create_split_inner(dev, flow, &dev_flow, attr,
6063                                               items, pre_actions,
6064                                               flow_split_info, error);
6065                 if (ret) {
6066                         ret = -rte_errno;
6067                         goto exit;
6068                 }
6069                 dev_flow->handle->split_flow_id = tag_id;
6070 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
6071                 if (!modify_after_mirror) {
6072                         /* Set the sfx group attr. */
6073                         sample_res = (struct mlx5_flow_dv_sample_resource *)
6074                                                 dev_flow->dv.sample_res;
6075                         sfx_tbl = (struct mlx5_flow_tbl_resource *)
6076                                                 sample_res->normal_path_tbl;
6077                         sfx_tbl_data = container_of(sfx_tbl,
6078                                                 struct mlx5_flow_tbl_data_entry,
6079                                                 tbl);
6080                         sfx_attr.group = sfx_attr.transfer ?
6081                         (sfx_tbl_data->level - 1) : sfx_tbl_data->level;
6082                 } else {
6083                         MLX5_ASSERT(attr->transfer);
6084                         sfx_attr.group = jump_table;
6085                 }
6086                 flow_split_info->prefix_layers =
6087                                 flow_get_prefix_layer_flags(dev_flow);
6088                 flow_split_info->prefix_mark = dev_flow->handle->mark;
6089                 /* Suffix group level already be scaled with factor, set
6090                  * MLX5_SCALE_FLOW_GROUP_BIT of skip_scale to 1 to avoid scale
6091                  * again in translation.
6092                  */
6093                 flow_split_info->skip_scale = 1 << MLX5_SCALE_FLOW_GROUP_BIT;
6094 #endif
6095         }
6096         /* Add the suffix subflow. */
6097         ret = flow_create_split_meter(dev, flow, &sfx_attr,
6098                                       sfx_items ? sfx_items : items,
6099                                       sfx_actions ? sfx_actions : actions,
6100                                       flow_split_info, error);
6101 exit:
6102         if (sfx_actions)
6103                 mlx5_free(sfx_actions);
6104         return ret;
6105 }
6106
6107 /**
6108  * Split the flow to subflow set. The splitters might be linked
6109  * in the chain, like this:
6110  * flow_create_split_outer() calls:
6111  *   flow_create_split_meter() calls:
6112  *     flow_create_split_metadata(meter_subflow_0) calls:
6113  *       flow_create_split_inner(metadata_subflow_0)
6114  *       flow_create_split_inner(metadata_subflow_1)
6115  *       flow_create_split_inner(metadata_subflow_2)
6116  *     flow_create_split_metadata(meter_subflow_1) calls:
6117  *       flow_create_split_inner(metadata_subflow_0)
6118  *       flow_create_split_inner(metadata_subflow_1)
6119  *       flow_create_split_inner(metadata_subflow_2)
6120  *
6121  * This provide flexible way to add new levels of flow splitting.
6122  * The all of successfully created subflows are included to the
6123  * parent flow dev_flow list.
6124  *
6125  * @param dev
6126  *   Pointer to Ethernet device.
6127  * @param[in] flow
6128  *   Parent flow structure pointer.
6129  * @param[in] attr
6130  *   Flow rule attributes.
6131  * @param[in] items
6132  *   Pattern specification (list terminated by the END pattern item).
6133  * @param[in] actions
6134  *   Associated actions (list terminated by the END action).
6135  * @param[in] flow_split_info
6136  *   Pointer to flow split info structure.
6137  * @param[out] error
6138  *   Perform verbose error reporting if not NULL.
6139  * @return
6140  *   0 on success, negative value otherwise
6141  */
6142 static int
6143 flow_create_split_outer(struct rte_eth_dev *dev,
6144                         struct rte_flow *flow,
6145                         const struct rte_flow_attr *attr,
6146                         const struct rte_flow_item items[],
6147                         const struct rte_flow_action actions[],
6148                         struct mlx5_flow_split_info *flow_split_info,
6149                         struct rte_flow_error *error)
6150 {
6151         int ret;
6152
6153         ret = flow_create_split_sample(dev, flow, attr, items,
6154                                        actions, flow_split_info, error);
6155         MLX5_ASSERT(ret <= 0);
6156         return ret;
6157 }
6158
6159 static inline struct mlx5_flow_tunnel *
6160 flow_tunnel_from_rule(const struct mlx5_flow *flow)
6161 {
6162         struct mlx5_flow_tunnel *tunnel;
6163
6164 #pragma GCC diagnostic push
6165 #pragma GCC diagnostic ignored "-Wcast-qual"
6166         tunnel = (typeof(tunnel))flow->tunnel;
6167 #pragma GCC diagnostic pop
6168
6169         return tunnel;
6170 }
6171
6172 /**
6173  * Adjust flow RSS workspace if needed.
6174  *
6175  * @param wks
6176  *   Pointer to thread flow work space.
6177  * @param rss_desc
6178  *   Pointer to RSS descriptor.
6179  * @param[in] nrssq_num
6180  *   New RSS queue number.
6181  *
6182  * @return
6183  *   0 on success, -1 otherwise and rte_errno is set.
6184  */
6185 static int
6186 flow_rss_workspace_adjust(struct mlx5_flow_workspace *wks,
6187                           struct mlx5_flow_rss_desc *rss_desc,
6188                           uint32_t nrssq_num)
6189 {
6190         if (likely(nrssq_num <= wks->rssq_num))
6191                 return 0;
6192         rss_desc->queue = realloc(rss_desc->queue,
6193                           sizeof(*rss_desc->queue) * RTE_ALIGN(nrssq_num, 2));
6194         if (!rss_desc->queue) {
6195                 rte_errno = ENOMEM;
6196                 return -1;
6197         }
6198         wks->rssq_num = RTE_ALIGN(nrssq_num, 2);
6199         return 0;
6200 }
6201
6202 /**
6203  * Create a flow and add it to @p list.
6204  *
6205  * @param dev
6206  *   Pointer to Ethernet device.
6207  * @param list
6208  *   Pointer to a TAILQ flow list. If this parameter NULL,
6209  *   no list insertion occurred, flow is just created,
6210  *   this is caller's responsibility to track the
6211  *   created flow.
6212  * @param[in] attr
6213  *   Flow rule attributes.
6214  * @param[in] items
6215  *   Pattern specification (list terminated by the END pattern item).
6216  * @param[in] actions
6217  *   Associated actions (list terminated by the END action).
6218  * @param[in] external
6219  *   This flow rule is created by request external to PMD.
6220  * @param[out] error
6221  *   Perform verbose error reporting if not NULL.
6222  *
6223  * @return
6224  *   A flow index on success, 0 otherwise and rte_errno is set.
6225  */
6226 static uint32_t
6227 flow_list_create(struct rte_eth_dev *dev, enum mlx5_flow_type type,
6228                  const struct rte_flow_attr *attr,
6229                  const struct rte_flow_item items[],
6230                  const struct rte_flow_action original_actions[],
6231                  bool external, struct rte_flow_error *error)
6232 {
6233         struct mlx5_priv *priv = dev->data->dev_private;
6234         struct rte_flow *flow = NULL;
6235         struct mlx5_flow *dev_flow;
6236         const struct rte_flow_action_rss *rss = NULL;
6237         struct mlx5_translated_action_handle
6238                 indir_actions[MLX5_MAX_INDIRECT_ACTIONS];
6239         int indir_actions_n = MLX5_MAX_INDIRECT_ACTIONS;
6240         union {
6241                 struct mlx5_flow_expand_rss buf;
6242                 uint8_t buffer[2048];
6243         } expand_buffer;
6244         union {
6245                 struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
6246                 uint8_t buffer[2048];
6247         } actions_rx;
6248         union {
6249                 struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
6250                 uint8_t buffer[2048];
6251         } actions_hairpin_tx;
6252         union {
6253                 struct rte_flow_item items[MLX5_MAX_SPLIT_ITEMS];
6254                 uint8_t buffer[2048];
6255         } items_tx;
6256         struct mlx5_flow_expand_rss *buf = &expand_buffer.buf;
6257         struct mlx5_flow_rss_desc *rss_desc;
6258         const struct rte_flow_action *p_actions_rx;
6259         uint32_t i;
6260         uint32_t idx = 0;
6261         int hairpin_flow;
6262         struct rte_flow_attr attr_tx = { .priority = 0 };
6263         const struct rte_flow_action *actions;
6264         struct rte_flow_action *translated_actions = NULL;
6265         struct mlx5_flow_tunnel *tunnel;
6266         struct tunnel_default_miss_ctx default_miss_ctx = { 0, };
6267         struct mlx5_flow_workspace *wks = mlx5_flow_push_thread_workspace();
6268         struct mlx5_flow_split_info flow_split_info = {
6269                 .external = !!external,
6270                 .skip_scale = 0,
6271                 .flow_idx = 0,
6272                 .prefix_mark = 0,
6273                 .prefix_layers = 0,
6274                 .table_id = 0
6275         };
6276         int ret;
6277
6278         MLX5_ASSERT(wks);
6279         rss_desc = &wks->rss_desc;
6280         ret = flow_action_handles_translate(dev, original_actions,
6281                                             indir_actions,
6282                                             &indir_actions_n,
6283                                             &translated_actions, error);
6284         if (ret < 0) {
6285                 MLX5_ASSERT(translated_actions == NULL);
6286                 return 0;
6287         }
6288         actions = translated_actions ? translated_actions : original_actions;
6289         p_actions_rx = actions;
6290         hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
6291         ret = flow_drv_validate(dev, attr, items, p_actions_rx,
6292                                 external, hairpin_flow, error);
6293         if (ret < 0)
6294                 goto error_before_hairpin_split;
6295         flow = mlx5_ipool_zmalloc(priv->flows[type], &idx);
6296         if (!flow) {
6297                 rte_errno = ENOMEM;
6298                 goto error_before_hairpin_split;
6299         }
6300         if (hairpin_flow > 0) {
6301                 if (hairpin_flow > MLX5_MAX_SPLIT_ACTIONS) {
6302                         rte_errno = EINVAL;
6303                         goto error_before_hairpin_split;
6304                 }
6305                 flow_hairpin_split(dev, actions, actions_rx.actions,
6306                                    actions_hairpin_tx.actions, items_tx.items,
6307                                    idx);
6308                 p_actions_rx = actions_rx.actions;
6309         }
6310         flow_split_info.flow_idx = idx;
6311         flow->drv_type = flow_get_drv_type(dev, attr);
6312         MLX5_ASSERT(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
6313                     flow->drv_type < MLX5_FLOW_TYPE_MAX);
6314         memset(rss_desc, 0, offsetof(struct mlx5_flow_rss_desc, queue));
6315         /* RSS Action only works on NIC RX domain */
6316         if (attr->ingress && !attr->transfer)
6317                 rss = flow_get_rss_action(dev, p_actions_rx);
6318         if (rss) {
6319                 if (flow_rss_workspace_adjust(wks, rss_desc, rss->queue_num))
6320                         return 0;
6321                 /*
6322                  * The following information is required by
6323                  * mlx5_flow_hashfields_adjust() in advance.
6324                  */
6325                 rss_desc->level = rss->level;
6326                 /* RSS type 0 indicates default RSS type (ETH_RSS_IP). */
6327                 rss_desc->types = !rss->types ? ETH_RSS_IP : rss->types;
6328         }
6329         flow->dev_handles = 0;
6330         if (rss && rss->types) {
6331                 unsigned int graph_root;
6332
6333                 graph_root = find_graph_root(items, rss->level);
6334                 ret = mlx5_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
6335                                            items, rss->types,
6336                                            mlx5_support_expansion, graph_root);
6337                 MLX5_ASSERT(ret > 0 &&
6338                        (unsigned int)ret < sizeof(expand_buffer.buffer));
6339                 if (rte_log_can_log(mlx5_logtype, RTE_LOG_DEBUG)) {
6340                         for (i = 0; i < buf->entries; ++i)
6341                                 mlx5_dbg__print_pattern(buf->entry[i].pattern);
6342                 }
6343         } else {
6344                 buf->entries = 1;
6345                 buf->entry[0].pattern = (void *)(uintptr_t)items;
6346         }
6347         rss_desc->shared_rss = flow_get_shared_rss_action(dev, indir_actions,
6348                                                       indir_actions_n);
6349         for (i = 0; i < buf->entries; ++i) {
6350                 /* Initialize flow split data. */
6351                 flow_split_info.prefix_layers = 0;
6352                 flow_split_info.prefix_mark = 0;
6353                 flow_split_info.skip_scale = 0;
6354                 /*
6355                  * The splitter may create multiple dev_flows,
6356                  * depending on configuration. In the simplest
6357                  * case it just creates unmodified original flow.
6358                  */
6359                 ret = flow_create_split_outer(dev, flow, attr,
6360                                               buf->entry[i].pattern,
6361                                               p_actions_rx, &flow_split_info,
6362                                               error);
6363                 if (ret < 0)
6364                         goto error;
6365                 if (is_flow_tunnel_steer_rule(wks->flows[0].tof_type)) {
6366                         ret = flow_tunnel_add_default_miss(dev, flow, attr,
6367                                                            p_actions_rx,
6368                                                            idx,
6369                                                            wks->flows[0].tunnel,
6370                                                            &default_miss_ctx,
6371                                                            error);
6372                         if (ret < 0) {
6373                                 mlx5_free(default_miss_ctx.queue);
6374                                 goto error;
6375                         }
6376                 }
6377         }
6378         /* Create the tx flow. */
6379         if (hairpin_flow) {
6380                 attr_tx.group = MLX5_HAIRPIN_TX_TABLE;
6381                 attr_tx.ingress = 0;
6382                 attr_tx.egress = 1;
6383                 dev_flow = flow_drv_prepare(dev, flow, &attr_tx, items_tx.items,
6384                                          actions_hairpin_tx.actions,
6385                                          idx, error);
6386                 if (!dev_flow)
6387                         goto error;
6388                 dev_flow->flow = flow;
6389                 dev_flow->external = 0;
6390                 SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
6391                               dev_flow->handle, next);
6392                 ret = flow_drv_translate(dev, dev_flow, &attr_tx,
6393                                          items_tx.items,
6394                                          actions_hairpin_tx.actions, error);
6395                 if (ret < 0)
6396                         goto error;
6397         }
6398         /*
6399          * Update the metadata register copy table. If extensive
6400          * metadata feature is enabled and registers are supported
6401          * we might create the extra rte_flow for each unique
6402          * MARK/FLAG action ID.
6403          *
6404          * The table is updated for ingress Flows only, because
6405          * the egress Flows belong to the different device and
6406          * copy table should be updated in peer NIC Rx domain.
6407          */
6408         if (attr->ingress &&
6409             (external || attr->group != MLX5_FLOW_MREG_CP_TABLE_GROUP)) {
6410                 ret = flow_mreg_update_copy_table(dev, flow, actions, error);
6411                 if (ret)
6412                         goto error;
6413         }
6414         /*
6415          * If the flow is external (from application) OR device is started,
6416          * OR mreg discover, then apply immediately.
6417          */
6418         if (external || dev->data->dev_started ||
6419             (attr->group == MLX5_FLOW_MREG_CP_TABLE_GROUP &&
6420              attr->priority == MLX5_FLOW_LOWEST_PRIO_INDICATOR)) {
6421                 ret = flow_drv_apply(dev, flow, error);
6422                 if (ret < 0)
6423                         goto error;
6424         }
6425         flow->type = type;
6426         flow_rxq_flags_set(dev, flow);
6427         rte_free(translated_actions);
6428         tunnel = flow_tunnel_from_rule(wks->flows);
6429         if (tunnel) {
6430                 flow->tunnel = 1;
6431                 flow->tunnel_id = tunnel->tunnel_id;
6432                 __atomic_add_fetch(&tunnel->refctn, 1, __ATOMIC_RELAXED);
6433                 mlx5_free(default_miss_ctx.queue);
6434         }
6435         mlx5_flow_pop_thread_workspace();
6436         return idx;
6437 error:
6438         MLX5_ASSERT(flow);
6439         ret = rte_errno; /* Save rte_errno before cleanup. */
6440         flow_mreg_del_copy_action(dev, flow);
6441         flow_drv_destroy(dev, flow);
6442         if (rss_desc->shared_rss)
6443                 __atomic_sub_fetch(&((struct mlx5_shared_action_rss *)
6444                         mlx5_ipool_get
6445                         (priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
6446                         rss_desc->shared_rss))->refcnt, 1, __ATOMIC_RELAXED);
6447         mlx5_ipool_free(priv->flows[type], idx);
6448         rte_errno = ret; /* Restore rte_errno. */
6449         ret = rte_errno;
6450         rte_errno = ret;
6451         mlx5_flow_pop_thread_workspace();
6452 error_before_hairpin_split:
6453         rte_free(translated_actions);
6454         return 0;
6455 }
6456
6457 /**
6458  * Create a dedicated flow rule on e-switch table 0 (root table), to direct all
6459  * incoming packets to table 1.
6460  *
6461  * Other flow rules, requested for group n, will be created in
6462  * e-switch table n+1.
6463  * Jump action to e-switch group n will be created to group n+1.
6464  *
6465  * Used when working in switchdev mode, to utilise advantages of table 1
6466  * and above.
6467  *
6468  * @param dev
6469  *   Pointer to Ethernet device.
6470  *
6471  * @return
6472  *   Pointer to flow on success, NULL otherwise and rte_errno is set.
6473  */
6474 struct rte_flow *
6475 mlx5_flow_create_esw_table_zero_flow(struct rte_eth_dev *dev)
6476 {
6477         const struct rte_flow_attr attr = {
6478                 .group = 0,
6479                 .priority = 0,
6480                 .ingress = 1,
6481                 .egress = 0,
6482                 .transfer = 1,
6483         };
6484         const struct rte_flow_item pattern = {
6485                 .type = RTE_FLOW_ITEM_TYPE_END,
6486         };
6487         struct rte_flow_action_jump jump = {
6488                 .group = 1,
6489         };
6490         const struct rte_flow_action actions[] = {
6491                 {
6492                         .type = RTE_FLOW_ACTION_TYPE_JUMP,
6493                         .conf = &jump,
6494                 },
6495                 {
6496                         .type = RTE_FLOW_ACTION_TYPE_END,
6497                 },
6498         };
6499         struct rte_flow_error error;
6500
6501         return (void *)(uintptr_t)flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
6502                                                    &attr, &pattern,
6503                                                    actions, false, &error);
6504 }
6505
6506 /**
6507  * Validate a flow supported by the NIC.
6508  *
6509  * @see rte_flow_validate()
6510  * @see rte_flow_ops
6511  */
6512 int
6513 mlx5_flow_validate(struct rte_eth_dev *dev,
6514                    const struct rte_flow_attr *attr,
6515                    const struct rte_flow_item items[],
6516                    const struct rte_flow_action original_actions[],
6517                    struct rte_flow_error *error)
6518 {
6519         int hairpin_flow;
6520         struct mlx5_translated_action_handle
6521                 indir_actions[MLX5_MAX_INDIRECT_ACTIONS];
6522         int indir_actions_n = MLX5_MAX_INDIRECT_ACTIONS;
6523         const struct rte_flow_action *actions;
6524         struct rte_flow_action *translated_actions = NULL;
6525         int ret = flow_action_handles_translate(dev, original_actions,
6526                                                 indir_actions,
6527                                                 &indir_actions_n,
6528                                                 &translated_actions, error);
6529
6530         if (ret)
6531                 return ret;
6532         actions = translated_actions ? translated_actions : original_actions;
6533         hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
6534         ret = flow_drv_validate(dev, attr, items, actions,
6535                                 true, hairpin_flow, error);
6536         rte_free(translated_actions);
6537         return ret;
6538 }
6539
6540 /**
6541  * Create a flow.
6542  *
6543  * @see rte_flow_create()
6544  * @see rte_flow_ops
6545  */
6546 struct rte_flow *
6547 mlx5_flow_create(struct rte_eth_dev *dev,
6548                  const struct rte_flow_attr *attr,
6549                  const struct rte_flow_item items[],
6550                  const struct rte_flow_action actions[],
6551                  struct rte_flow_error *error)
6552 {
6553         /*
6554          * If the device is not started yet, it is not allowed to created a
6555          * flow from application. PMD default flows and traffic control flows
6556          * are not affected.
6557          */
6558         if (unlikely(!dev->data->dev_started)) {
6559                 DRV_LOG(DEBUG, "port %u is not started when "
6560                         "inserting a flow", dev->data->port_id);
6561                 rte_flow_error_set(error, ENODEV,
6562                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
6563                                    NULL,
6564                                    "port not started");
6565                 return NULL;
6566         }
6567
6568         return (void *)(uintptr_t)flow_list_create(dev, MLX5_FLOW_TYPE_GEN,
6569                                                    attr, items, actions,
6570                                                    true, error);
6571 }
6572
6573 /**
6574  * Destroy a flow in a list.
6575  *
6576  * @param dev
6577  *   Pointer to Ethernet device.
6578  * @param[in] flow_idx
6579  *   Index of flow to destroy.
6580  */
6581 static void
6582 flow_list_destroy(struct rte_eth_dev *dev, enum mlx5_flow_type type,
6583                   uint32_t flow_idx)
6584 {
6585         struct mlx5_priv *priv = dev->data->dev_private;
6586         struct rte_flow *flow = mlx5_ipool_get(priv->flows[type], flow_idx);
6587
6588         if (!flow)
6589                 return;
6590         MLX5_ASSERT(flow->type == type);
6591         /*
6592          * Update RX queue flags only if port is started, otherwise it is
6593          * already clean.
6594          */
6595         if (dev->data->dev_started)
6596                 flow_rxq_flags_trim(dev, flow);
6597         flow_drv_destroy(dev, flow);
6598         if (flow->tunnel) {
6599                 struct mlx5_flow_tunnel *tunnel;
6600
6601                 tunnel = mlx5_find_tunnel_id(dev, flow->tunnel_id);
6602                 RTE_VERIFY(tunnel);
6603                 if (!__atomic_sub_fetch(&tunnel->refctn, 1, __ATOMIC_RELAXED))
6604                         mlx5_flow_tunnel_free(dev, tunnel);
6605         }
6606         flow_mreg_del_copy_action(dev, flow);
6607         mlx5_ipool_free(priv->flows[type], flow_idx);
6608 }
6609
6610 /**
6611  * Destroy all flows.
6612  *
6613  * @param dev
6614  *   Pointer to Ethernet device.
6615  * @param type
6616  *   Flow type to be flushed.
6617  * @param active
6618  *   If flushing is called avtively.
6619  */
6620 void
6621 mlx5_flow_list_flush(struct rte_eth_dev *dev, enum mlx5_flow_type type,
6622                      bool active)
6623 {
6624         struct mlx5_priv *priv = dev->data->dev_private;
6625         uint32_t num_flushed = 0, fidx = 1;
6626         struct rte_flow *flow;
6627
6628         MLX5_IPOOL_FOREACH(priv->flows[type], fidx, flow) {
6629                 flow_list_destroy(dev, type, fidx);
6630                 num_flushed++;
6631         }
6632         if (active) {
6633                 DRV_LOG(INFO, "port %u: %u flows flushed before stopping",
6634                         dev->data->port_id, num_flushed);
6635         }
6636 }
6637
6638 /**
6639  * Stop all default actions for flows.
6640  *
6641  * @param dev
6642  *   Pointer to Ethernet device.
6643  */
6644 void
6645 mlx5_flow_stop_default(struct rte_eth_dev *dev)
6646 {
6647         flow_mreg_del_default_copy_action(dev);
6648         flow_rxq_flags_clear(dev);
6649 }
6650
6651 /**
6652  * Start all default actions for flows.
6653  *
6654  * @param dev
6655  *   Pointer to Ethernet device.
6656  * @return
6657  *   0 on success, a negative errno value otherwise and rte_errno is set.
6658  */
6659 int
6660 mlx5_flow_start_default(struct rte_eth_dev *dev)
6661 {
6662         struct rte_flow_error error;
6663
6664         /* Make sure default copy action (reg_c[0] -> reg_b) is created. */
6665         return flow_mreg_add_default_copy_action(dev, &error);
6666 }
6667
6668 /**
6669  * Release key of thread specific flow workspace data.
6670  */
6671 void
6672 flow_release_workspace(void *data)
6673 {
6674         struct mlx5_flow_workspace *wks = data;
6675         struct mlx5_flow_workspace *next;
6676
6677         while (wks) {
6678                 next = wks->next;
6679                 free(wks->rss_desc.queue);
6680                 free(wks);
6681                 wks = next;
6682         }
6683 }
6684
6685 /**
6686  * Get thread specific current flow workspace.
6687  *
6688  * @return pointer to thread specific flow workspace data, NULL on error.
6689  */
6690 struct mlx5_flow_workspace*
6691 mlx5_flow_get_thread_workspace(void)
6692 {
6693         struct mlx5_flow_workspace *data;
6694
6695         data = mlx5_flow_os_get_specific_workspace();
6696         MLX5_ASSERT(data && data->inuse);
6697         if (!data || !data->inuse)
6698                 DRV_LOG(ERR, "flow workspace not initialized.");
6699         return data;
6700 }
6701
6702 /**
6703  * Allocate and init new flow workspace.
6704  *
6705  * @return pointer to flow workspace data, NULL on error.
6706  */
6707 static struct mlx5_flow_workspace*
6708 flow_alloc_thread_workspace(void)
6709 {
6710         struct mlx5_flow_workspace *data = calloc(1, sizeof(*data));
6711
6712         if (!data) {
6713                 DRV_LOG(ERR, "Failed to allocate flow workspace "
6714                         "memory.");
6715                 return NULL;
6716         }
6717         data->rss_desc.queue = calloc(1,
6718                         sizeof(uint16_t) * MLX5_RSSQ_DEFAULT_NUM);
6719         if (!data->rss_desc.queue)
6720                 goto err;
6721         data->rssq_num = MLX5_RSSQ_DEFAULT_NUM;
6722         return data;
6723 err:
6724         if (data->rss_desc.queue)
6725                 free(data->rss_desc.queue);
6726         free(data);
6727         return NULL;
6728 }
6729
6730 /**
6731  * Get new thread specific flow workspace.
6732  *
6733  * If current workspace inuse, create new one and set as current.
6734  *
6735  * @return pointer to thread specific flow workspace data, NULL on error.
6736  */
6737 static struct mlx5_flow_workspace*
6738 mlx5_flow_push_thread_workspace(void)
6739 {
6740         struct mlx5_flow_workspace *curr;
6741         struct mlx5_flow_workspace *data;
6742
6743         curr = mlx5_flow_os_get_specific_workspace();
6744         if (!curr) {
6745                 data = flow_alloc_thread_workspace();
6746                 if (!data)
6747                         return NULL;
6748         } else if (!curr->inuse) {
6749                 data = curr;
6750         } else if (curr->next) {
6751                 data = curr->next;
6752         } else {
6753                 data = flow_alloc_thread_workspace();
6754                 if (!data)
6755                         return NULL;
6756                 curr->next = data;
6757                 data->prev = curr;
6758         }
6759         data->inuse = 1;
6760         data->flow_idx = 0;
6761         /* Set as current workspace */
6762         if (mlx5_flow_os_set_specific_workspace(data))
6763                 DRV_LOG(ERR, "Failed to set flow workspace to thread.");
6764         return data;
6765 }
6766
6767 /**
6768  * Close current thread specific flow workspace.
6769  *
6770  * If previous workspace available, set it as current.
6771  *
6772  * @return pointer to thread specific flow workspace data, NULL on error.
6773  */
6774 static void
6775 mlx5_flow_pop_thread_workspace(void)
6776 {
6777         struct mlx5_flow_workspace *data = mlx5_flow_get_thread_workspace();
6778
6779         if (!data)
6780                 return;
6781         if (!data->inuse) {
6782                 DRV_LOG(ERR, "Failed to close unused flow workspace.");
6783                 return;
6784         }
6785         data->inuse = 0;
6786         if (!data->prev)
6787                 return;
6788         if (mlx5_flow_os_set_specific_workspace(data->prev))
6789                 DRV_LOG(ERR, "Failed to set flow workspace to thread.");
6790 }
6791
6792 /**
6793  * Verify the flow list is empty
6794  *
6795  * @param dev
6796  *  Pointer to Ethernet device.
6797  *
6798  * @return the number of flows not released.
6799  */
6800 int
6801 mlx5_flow_verify(struct rte_eth_dev *dev __rte_unused)
6802 {
6803         struct mlx5_priv *priv = dev->data->dev_private;
6804         struct rte_flow *flow;
6805         uint32_t idx = 0;
6806         int ret = 0, i;
6807
6808         for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
6809                 MLX5_IPOOL_FOREACH(priv->flows[i], idx, flow) {
6810                         DRV_LOG(DEBUG, "port %u flow %p still referenced",
6811                                 dev->data->port_id, (void *)flow);
6812                         ret++;
6813                 }
6814         }
6815         return ret;
6816 }
6817
6818 /**
6819  * Enable default hairpin egress flow.
6820  *
6821  * @param dev
6822  *   Pointer to Ethernet device.
6823  * @param queue
6824  *   The queue index.
6825  *
6826  * @return
6827  *   0 on success, a negative errno value otherwise and rte_errno is set.
6828  */
6829 int
6830 mlx5_ctrl_flow_source_queue(struct rte_eth_dev *dev,
6831                             uint32_t queue)
6832 {
6833         const struct rte_flow_attr attr = {
6834                 .egress = 1,
6835                 .priority = 0,
6836         };
6837         struct mlx5_rte_flow_item_tx_queue queue_spec = {
6838                 .queue = queue,
6839         };
6840         struct mlx5_rte_flow_item_tx_queue queue_mask = {
6841                 .queue = UINT32_MAX,
6842         };
6843         struct rte_flow_item items[] = {
6844                 {
6845                         .type = (enum rte_flow_item_type)
6846                                 MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE,
6847                         .spec = &queue_spec,
6848                         .last = NULL,
6849                         .mask = &queue_mask,
6850                 },
6851                 {
6852                         .type = RTE_FLOW_ITEM_TYPE_END,
6853                 },
6854         };
6855         struct rte_flow_action_jump jump = {
6856                 .group = MLX5_HAIRPIN_TX_TABLE,
6857         };
6858         struct rte_flow_action actions[2];
6859         uint32_t flow_idx;
6860         struct rte_flow_error error;
6861
6862         actions[0].type = RTE_FLOW_ACTION_TYPE_JUMP;
6863         actions[0].conf = &jump;
6864         actions[1].type = RTE_FLOW_ACTION_TYPE_END;
6865         flow_idx = flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
6866                                     &attr, items, actions, false, &error);
6867         if (!flow_idx) {
6868                 DRV_LOG(DEBUG,
6869                         "Failed to create ctrl flow: rte_errno(%d),"
6870                         " type(%d), message(%s)",
6871                         rte_errno, error.type,
6872                         error.message ? error.message : " (no stated reason)");
6873                 return -rte_errno;
6874         }
6875         return 0;
6876 }
6877
6878 /**
6879  * Enable a control flow configured from the control plane.
6880  *
6881  * @param dev
6882  *   Pointer to Ethernet device.
6883  * @param eth_spec
6884  *   An Ethernet flow spec to apply.
6885  * @param eth_mask
6886  *   An Ethernet flow mask to apply.
6887  * @param vlan_spec
6888  *   A VLAN flow spec to apply.
6889  * @param vlan_mask
6890  *   A VLAN flow mask to apply.
6891  *
6892  * @return
6893  *   0 on success, a negative errno value otherwise and rte_errno is set.
6894  */
6895 int
6896 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
6897                     struct rte_flow_item_eth *eth_spec,
6898                     struct rte_flow_item_eth *eth_mask,
6899                     struct rte_flow_item_vlan *vlan_spec,
6900                     struct rte_flow_item_vlan *vlan_mask)
6901 {
6902         struct mlx5_priv *priv = dev->data->dev_private;
6903         const struct rte_flow_attr attr = {
6904                 .ingress = 1,
6905                 .priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR,
6906         };
6907         struct rte_flow_item items[] = {
6908                 {
6909                         .type = RTE_FLOW_ITEM_TYPE_ETH,
6910                         .spec = eth_spec,
6911                         .last = NULL,
6912                         .mask = eth_mask,
6913                 },
6914                 {
6915                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
6916                                               RTE_FLOW_ITEM_TYPE_END,
6917                         .spec = vlan_spec,
6918                         .last = NULL,
6919                         .mask = vlan_mask,
6920                 },
6921                 {
6922                         .type = RTE_FLOW_ITEM_TYPE_END,
6923                 },
6924         };
6925         uint16_t queue[priv->reta_idx_n];
6926         struct rte_flow_action_rss action_rss = {
6927                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
6928                 .level = 0,
6929                 .types = priv->rss_conf.rss_hf,
6930                 .key_len = priv->rss_conf.rss_key_len,
6931                 .queue_num = priv->reta_idx_n,
6932                 .key = priv->rss_conf.rss_key,
6933                 .queue = queue,
6934         };
6935         struct rte_flow_action actions[] = {
6936                 {
6937                         .type = RTE_FLOW_ACTION_TYPE_RSS,
6938                         .conf = &action_rss,
6939                 },
6940                 {
6941                         .type = RTE_FLOW_ACTION_TYPE_END,
6942                 },
6943         };
6944         uint32_t flow_idx;
6945         struct rte_flow_error error;
6946         unsigned int i;
6947
6948         if (!priv->reta_idx_n || !priv->rxqs_n) {
6949                 return 0;
6950         }
6951         if (!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
6952                 action_rss.types = 0;
6953         for (i = 0; i != priv->reta_idx_n; ++i)
6954                 queue[i] = (*priv->reta_idx)[i];
6955         flow_idx = flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
6956                                     &attr, items, actions, false, &error);
6957         if (!flow_idx)
6958                 return -rte_errno;
6959         return 0;
6960 }
6961
6962 /**
6963  * Enable a flow control configured from the control plane.
6964  *
6965  * @param dev
6966  *   Pointer to Ethernet device.
6967  * @param eth_spec
6968  *   An Ethernet flow spec to apply.
6969  * @param eth_mask
6970  *   An Ethernet flow mask to apply.
6971  *
6972  * @return
6973  *   0 on success, a negative errno value otherwise and rte_errno is set.
6974  */
6975 int
6976 mlx5_ctrl_flow(struct rte_eth_dev *dev,
6977                struct rte_flow_item_eth *eth_spec,
6978                struct rte_flow_item_eth *eth_mask)
6979 {
6980         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
6981 }
6982
6983 /**
6984  * Create default miss flow rule matching lacp traffic
6985  *
6986  * @param dev
6987  *   Pointer to Ethernet device.
6988  * @param eth_spec
6989  *   An Ethernet flow spec to apply.
6990  *
6991  * @return
6992  *   0 on success, a negative errno value otherwise and rte_errno is set.
6993  */
6994 int
6995 mlx5_flow_lacp_miss(struct rte_eth_dev *dev)
6996 {
6997         /*
6998          * The LACP matching is done by only using ether type since using
6999          * a multicast dst mac causes kernel to give low priority to this flow.
7000          */
7001         static const struct rte_flow_item_eth lacp_spec = {
7002                 .type = RTE_BE16(0x8809),
7003         };
7004         static const struct rte_flow_item_eth lacp_mask = {
7005                 .type = 0xffff,
7006         };
7007         const struct rte_flow_attr attr = {
7008                 .ingress = 1,
7009         };
7010         struct rte_flow_item items[] = {
7011                 {
7012                         .type = RTE_FLOW_ITEM_TYPE_ETH,
7013                         .spec = &lacp_spec,
7014                         .mask = &lacp_mask,
7015                 },
7016                 {
7017                         .type = RTE_FLOW_ITEM_TYPE_END,
7018                 },
7019         };
7020         struct rte_flow_action actions[] = {
7021                 {
7022                         .type = (enum rte_flow_action_type)
7023                                 MLX5_RTE_FLOW_ACTION_TYPE_DEFAULT_MISS,
7024                 },
7025                 {
7026                         .type = RTE_FLOW_ACTION_TYPE_END,
7027                 },
7028         };
7029         struct rte_flow_error error;
7030         uint32_t flow_idx = flow_list_create(dev, MLX5_FLOW_TYPE_CTL,
7031                                         &attr, items, actions,
7032                                         false, &error);
7033
7034         if (!flow_idx)
7035                 return -rte_errno;
7036         return 0;
7037 }
7038
7039 /**
7040  * Destroy a flow.
7041  *
7042  * @see rte_flow_destroy()
7043  * @see rte_flow_ops
7044  */
7045 int
7046 mlx5_flow_destroy(struct rte_eth_dev *dev,
7047                   struct rte_flow *flow,
7048                   struct rte_flow_error *error __rte_unused)
7049 {
7050         flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN,
7051                                 (uintptr_t)(void *)flow);
7052         return 0;
7053 }
7054
7055 /**
7056  * Destroy all flows.
7057  *
7058  * @see rte_flow_flush()
7059  * @see rte_flow_ops
7060  */
7061 int
7062 mlx5_flow_flush(struct rte_eth_dev *dev,
7063                 struct rte_flow_error *error __rte_unused)
7064 {
7065         mlx5_flow_list_flush(dev, MLX5_FLOW_TYPE_GEN, false);
7066         return 0;
7067 }
7068
7069 /**
7070  * Isolated mode.
7071  *
7072  * @see rte_flow_isolate()
7073  * @see rte_flow_ops
7074  */
7075 int
7076 mlx5_flow_isolate(struct rte_eth_dev *dev,
7077                   int enable,
7078                   struct rte_flow_error *error)
7079 {
7080         struct mlx5_priv *priv = dev->data->dev_private;
7081
7082         if (dev->data->dev_started) {
7083                 rte_flow_error_set(error, EBUSY,
7084                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7085                                    NULL,
7086                                    "port must be stopped first");
7087                 return -rte_errno;
7088         }
7089         priv->isolated = !!enable;
7090         if (enable)
7091                 dev->dev_ops = &mlx5_dev_ops_isolate;
7092         else
7093                 dev->dev_ops = &mlx5_dev_ops;
7094
7095         dev->rx_descriptor_status = mlx5_rx_descriptor_status;
7096         dev->tx_descriptor_status = mlx5_tx_descriptor_status;
7097
7098         return 0;
7099 }
7100
7101 /**
7102  * Query a flow.
7103  *
7104  * @see rte_flow_query()
7105  * @see rte_flow_ops
7106  */
7107 static int
7108 flow_drv_query(struct rte_eth_dev *dev,
7109                uint32_t flow_idx,
7110                const struct rte_flow_action *actions,
7111                void *data,
7112                struct rte_flow_error *error)
7113 {
7114         struct mlx5_priv *priv = dev->data->dev_private;
7115         const struct mlx5_flow_driver_ops *fops;
7116         struct rte_flow *flow = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
7117                                                flow_idx);
7118         enum mlx5_flow_drv_type ftype;
7119
7120         if (!flow) {
7121                 return rte_flow_error_set(error, ENOENT,
7122                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
7123                           NULL,
7124                           "invalid flow handle");
7125         }
7126         ftype = flow->drv_type;
7127         MLX5_ASSERT(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
7128         fops = flow_get_drv_ops(ftype);
7129
7130         return fops->query(dev, flow, actions, data, error);
7131 }
7132
7133 /**
7134  * Query a flow.
7135  *
7136  * @see rte_flow_query()
7137  * @see rte_flow_ops
7138  */
7139 int
7140 mlx5_flow_query(struct rte_eth_dev *dev,
7141                 struct rte_flow *flow,
7142                 const struct rte_flow_action *actions,
7143                 void *data,
7144                 struct rte_flow_error *error)
7145 {
7146         int ret;
7147
7148         ret = flow_drv_query(dev, (uintptr_t)(void *)flow, actions, data,
7149                              error);
7150         if (ret < 0)
7151                 return ret;
7152         return 0;
7153 }
7154
7155 /**
7156  * Get rte_flow callbacks.
7157  *
7158  * @param dev
7159  *   Pointer to Ethernet device structure.
7160  * @param ops
7161  *   Pointer to operation-specific structure.
7162  *
7163  * @return 0
7164  */
7165 int
7166 mlx5_flow_ops_get(struct rte_eth_dev *dev __rte_unused,
7167                   const struct rte_flow_ops **ops)
7168 {
7169         *ops = &mlx5_flow_ops;
7170         return 0;
7171 }
7172
7173 /**
7174  * Validate meter policy actions.
7175  * Dispatcher for action type specific validation.
7176  *
7177  * @param[in] dev
7178  *   Pointer to the Ethernet device structure.
7179  * @param[in] action
7180  *   The meter policy action object to validate.
7181  * @param[in] attr
7182  *   Attributes of flow to determine steering domain.
7183  * @param[out] is_rss
7184  *   Is RSS or not.
7185  * @param[out] domain_bitmap
7186  *   Domain bitmap.
7187  * @param[out] is_def_policy
7188  *   Is default policy or not.
7189  * @param[out] error
7190  *   Perform verbose error reporting if not NULL. Initialized in case of
7191  *   error only.
7192  *
7193  * @return
7194  *   0 on success, otherwise negative errno value.
7195  */
7196 int
7197 mlx5_flow_validate_mtr_acts(struct rte_eth_dev *dev,
7198                         const struct rte_flow_action *actions[RTE_COLORS],
7199                         struct rte_flow_attr *attr,
7200                         bool *is_rss,
7201                         uint8_t *domain_bitmap,
7202                         bool *is_def_policy,
7203                         struct rte_mtr_error *error)
7204 {
7205         const struct mlx5_flow_driver_ops *fops;
7206
7207         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7208         return fops->validate_mtr_acts(dev, actions, attr,
7209                         is_rss, domain_bitmap, is_def_policy, error);
7210 }
7211
7212 /**
7213  * Destroy the meter table set.
7214  *
7215  * @param[in] dev
7216  *   Pointer to Ethernet device.
7217  * @param[in] mtr_policy
7218  *   Meter policy struct.
7219  */
7220 void
7221 mlx5_flow_destroy_mtr_acts(struct rte_eth_dev *dev,
7222                       struct mlx5_flow_meter_policy *mtr_policy)
7223 {
7224         const struct mlx5_flow_driver_ops *fops;
7225
7226         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7227         fops->destroy_mtr_acts(dev, mtr_policy);
7228 }
7229
7230 /**
7231  * Create policy action, lock free,
7232  * (mutex should be acquired by caller).
7233  * Dispatcher for action type specific call.
7234  *
7235  * @param[in] dev
7236  *   Pointer to the Ethernet device structure.
7237  * @param[in] mtr_policy
7238  *   Meter policy struct.
7239  * @param[in] action
7240  *   Action specification used to create meter actions.
7241  * @param[out] error
7242  *   Perform verbose error reporting if not NULL. Initialized in case of
7243  *   error only.
7244  *
7245  * @return
7246  *   0 on success, otherwise negative errno value.
7247  */
7248 int
7249 mlx5_flow_create_mtr_acts(struct rte_eth_dev *dev,
7250                       struct mlx5_flow_meter_policy *mtr_policy,
7251                       const struct rte_flow_action *actions[RTE_COLORS],
7252                       struct rte_mtr_error *error)
7253 {
7254         const struct mlx5_flow_driver_ops *fops;
7255
7256         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7257         return fops->create_mtr_acts(dev, mtr_policy, actions, error);
7258 }
7259
7260 /**
7261  * Create policy rules, lock free,
7262  * (mutex should be acquired by caller).
7263  * Dispatcher for action type specific call.
7264  *
7265  * @param[in] dev
7266  *   Pointer to the Ethernet device structure.
7267  * @param[in] mtr_policy
7268  *   Meter policy struct.
7269  *
7270  * @return
7271  *   0 on success, -1 otherwise.
7272  */
7273 int
7274 mlx5_flow_create_policy_rules(struct rte_eth_dev *dev,
7275                              struct mlx5_flow_meter_policy *mtr_policy)
7276 {
7277         const struct mlx5_flow_driver_ops *fops;
7278
7279         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7280         return fops->create_policy_rules(dev, mtr_policy);
7281 }
7282
7283 /**
7284  * Destroy policy rules, lock free,
7285  * (mutex should be acquired by caller).
7286  * Dispatcher for action type specific call.
7287  *
7288  * @param[in] dev
7289  *   Pointer to the Ethernet device structure.
7290  * @param[in] mtr_policy
7291  *   Meter policy struct.
7292  */
7293 void
7294 mlx5_flow_destroy_policy_rules(struct rte_eth_dev *dev,
7295                              struct mlx5_flow_meter_policy *mtr_policy)
7296 {
7297         const struct mlx5_flow_driver_ops *fops;
7298
7299         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7300         fops->destroy_policy_rules(dev, mtr_policy);
7301 }
7302
7303 /**
7304  * Destroy the default policy table set.
7305  *
7306  * @param[in] dev
7307  *   Pointer to Ethernet device.
7308  */
7309 void
7310 mlx5_flow_destroy_def_policy(struct rte_eth_dev *dev)
7311 {
7312         const struct mlx5_flow_driver_ops *fops;
7313
7314         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7315         fops->destroy_def_policy(dev);
7316 }
7317
7318 /**
7319  * Destroy the default policy table set.
7320  *
7321  * @param[in] dev
7322  *   Pointer to Ethernet device.
7323  *
7324  * @return
7325  *   0 on success, -1 otherwise.
7326  */
7327 int
7328 mlx5_flow_create_def_policy(struct rte_eth_dev *dev)
7329 {
7330         const struct mlx5_flow_driver_ops *fops;
7331
7332         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7333         return fops->create_def_policy(dev);
7334 }
7335
7336 /**
7337  * Create the needed meter and suffix tables.
7338  *
7339  * @param[in] dev
7340  *   Pointer to Ethernet device.
7341  *
7342  * @return
7343  *   0 on success, -1 otherwise.
7344  */
7345 int
7346 mlx5_flow_create_mtr_tbls(struct rte_eth_dev *dev,
7347                         struct mlx5_flow_meter_info *fm,
7348                         uint32_t mtr_idx,
7349                         uint8_t domain_bitmap)
7350 {
7351         const struct mlx5_flow_driver_ops *fops;
7352
7353         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7354         return fops->create_mtr_tbls(dev, fm, mtr_idx, domain_bitmap);
7355 }
7356
7357 /**
7358  * Destroy the meter table set.
7359  *
7360  * @param[in] dev
7361  *   Pointer to Ethernet device.
7362  * @param[in] tbl
7363  *   Pointer to the meter table set.
7364  */
7365 void
7366 mlx5_flow_destroy_mtr_tbls(struct rte_eth_dev *dev,
7367                            struct mlx5_flow_meter_info *fm)
7368 {
7369         const struct mlx5_flow_driver_ops *fops;
7370
7371         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7372         fops->destroy_mtr_tbls(dev, fm);
7373 }
7374
7375 /**
7376  * Destroy the global meter drop table.
7377  *
7378  * @param[in] dev
7379  *   Pointer to Ethernet device.
7380  */
7381 void
7382 mlx5_flow_destroy_mtr_drop_tbls(struct rte_eth_dev *dev)
7383 {
7384         const struct mlx5_flow_driver_ops *fops;
7385
7386         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7387         fops->destroy_mtr_drop_tbls(dev);
7388 }
7389
7390 /**
7391  * Destroy the sub policy table with RX queue.
7392  *
7393  * @param[in] dev
7394  *   Pointer to Ethernet device.
7395  * @param[in] mtr_policy
7396  *   Pointer to meter policy table.
7397  */
7398 void
7399 mlx5_flow_destroy_sub_policy_with_rxq(struct rte_eth_dev *dev,
7400                 struct mlx5_flow_meter_policy *mtr_policy)
7401 {
7402         const struct mlx5_flow_driver_ops *fops;
7403
7404         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7405         fops->destroy_sub_policy_with_rxq(dev, mtr_policy);
7406 }
7407
7408 /**
7409  * Allocate the needed aso flow meter id.
7410  *
7411  * @param[in] dev
7412  *   Pointer to Ethernet device.
7413  *
7414  * @return
7415  *   Index to aso flow meter on success, NULL otherwise.
7416  */
7417 uint32_t
7418 mlx5_flow_mtr_alloc(struct rte_eth_dev *dev)
7419 {
7420         const struct mlx5_flow_driver_ops *fops;
7421
7422         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7423         return fops->create_meter(dev);
7424 }
7425
7426 /**
7427  * Free the aso flow meter id.
7428  *
7429  * @param[in] dev
7430  *   Pointer to Ethernet device.
7431  * @param[in] mtr_idx
7432  *  Index to aso flow meter to be free.
7433  *
7434  * @return
7435  *   0 on success.
7436  */
7437 void
7438 mlx5_flow_mtr_free(struct rte_eth_dev *dev, uint32_t mtr_idx)
7439 {
7440         const struct mlx5_flow_driver_ops *fops;
7441
7442         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7443         fops->free_meter(dev, mtr_idx);
7444 }
7445
7446 /**
7447  * Allocate a counter.
7448  *
7449  * @param[in] dev
7450  *   Pointer to Ethernet device structure.
7451  *
7452  * @return
7453  *   Index to allocated counter  on success, 0 otherwise.
7454  */
7455 uint32_t
7456 mlx5_counter_alloc(struct rte_eth_dev *dev)
7457 {
7458         const struct mlx5_flow_driver_ops *fops;
7459         struct rte_flow_attr attr = { .transfer = 0 };
7460
7461         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
7462                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7463                 return fops->counter_alloc(dev);
7464         }
7465         DRV_LOG(ERR,
7466                 "port %u counter allocate is not supported.",
7467                  dev->data->port_id);
7468         return 0;
7469 }
7470
7471 /**
7472  * Free a counter.
7473  *
7474  * @param[in] dev
7475  *   Pointer to Ethernet device structure.
7476  * @param[in] cnt
7477  *   Index to counter to be free.
7478  */
7479 void
7480 mlx5_counter_free(struct rte_eth_dev *dev, uint32_t cnt)
7481 {
7482         const struct mlx5_flow_driver_ops *fops;
7483         struct rte_flow_attr attr = { .transfer = 0 };
7484
7485         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
7486                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7487                 fops->counter_free(dev, cnt);
7488                 return;
7489         }
7490         DRV_LOG(ERR,
7491                 "port %u counter free is not supported.",
7492                  dev->data->port_id);
7493 }
7494
7495 /**
7496  * Query counter statistics.
7497  *
7498  * @param[in] dev
7499  *   Pointer to Ethernet device structure.
7500  * @param[in] cnt
7501  *   Index to counter to query.
7502  * @param[in] clear
7503  *   Set to clear counter statistics.
7504  * @param[out] pkts
7505  *   The counter hits packets number to save.
7506  * @param[out] bytes
7507  *   The counter hits bytes number to save.
7508  *
7509  * @return
7510  *   0 on success, a negative errno value otherwise.
7511  */
7512 int
7513 mlx5_counter_query(struct rte_eth_dev *dev, uint32_t cnt,
7514                    bool clear, uint64_t *pkts, uint64_t *bytes)
7515 {
7516         const struct mlx5_flow_driver_ops *fops;
7517         struct rte_flow_attr attr = { .transfer = 0 };
7518
7519         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
7520                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
7521                 return fops->counter_query(dev, cnt, clear, pkts, bytes);
7522         }
7523         DRV_LOG(ERR,
7524                 "port %u counter query is not supported.",
7525                  dev->data->port_id);
7526         return -ENOTSUP;
7527 }
7528
7529 /**
7530  * Allocate a new memory for the counter values wrapped by all the needed
7531  * management.
7532  *
7533  * @param[in] sh
7534  *   Pointer to mlx5_dev_ctx_shared object.
7535  *
7536  * @return
7537  *   0 on success, a negative errno value otherwise.
7538  */
7539 static int
7540 mlx5_flow_create_counter_stat_mem_mng(struct mlx5_dev_ctx_shared *sh)
7541 {
7542         struct mlx5_devx_mkey_attr mkey_attr;
7543         struct mlx5_counter_stats_mem_mng *mem_mng;
7544         volatile struct flow_counter_stats *raw_data;
7545         int raws_n = MLX5_CNT_CONTAINER_RESIZE + MLX5_MAX_PENDING_QUERIES;
7546         int size = (sizeof(struct flow_counter_stats) *
7547                         MLX5_COUNTERS_PER_POOL +
7548                         sizeof(struct mlx5_counter_stats_raw)) * raws_n +
7549                         sizeof(struct mlx5_counter_stats_mem_mng);
7550         size_t pgsize = rte_mem_page_size();
7551         uint8_t *mem;
7552         int i;
7553
7554         if (pgsize == (size_t)-1) {
7555                 DRV_LOG(ERR, "Failed to get mem page size");
7556                 rte_errno = ENOMEM;
7557                 return -ENOMEM;
7558         }
7559         mem = mlx5_malloc(MLX5_MEM_ZERO, size, pgsize, SOCKET_ID_ANY);
7560         if (!mem) {
7561                 rte_errno = ENOMEM;
7562                 return -ENOMEM;
7563         }
7564         mem_mng = (struct mlx5_counter_stats_mem_mng *)(mem + size) - 1;
7565         size = sizeof(*raw_data) * MLX5_COUNTERS_PER_POOL * raws_n;
7566         mem_mng->umem = mlx5_os_umem_reg(sh->ctx, mem, size,
7567                                                  IBV_ACCESS_LOCAL_WRITE);
7568         if (!mem_mng->umem) {
7569                 rte_errno = errno;
7570                 mlx5_free(mem);
7571                 return -rte_errno;
7572         }
7573         memset(&mkey_attr, 0, sizeof(mkey_attr));
7574         mkey_attr.addr = (uintptr_t)mem;
7575         mkey_attr.size = size;
7576         mkey_attr.umem_id = mlx5_os_get_umem_id(mem_mng->umem);
7577         mkey_attr.pd = sh->pdn;
7578         mkey_attr.relaxed_ordering_write = sh->cmng.relaxed_ordering_write;
7579         mkey_attr.relaxed_ordering_read = sh->cmng.relaxed_ordering_read;
7580         mem_mng->dm = mlx5_devx_cmd_mkey_create(sh->ctx, &mkey_attr);
7581         if (!mem_mng->dm) {
7582                 mlx5_os_umem_dereg(mem_mng->umem);
7583                 rte_errno = errno;
7584                 mlx5_free(mem);
7585                 return -rte_errno;
7586         }
7587         mem_mng->raws = (struct mlx5_counter_stats_raw *)(mem + size);
7588         raw_data = (volatile struct flow_counter_stats *)mem;
7589         for (i = 0; i < raws_n; ++i) {
7590                 mem_mng->raws[i].mem_mng = mem_mng;
7591                 mem_mng->raws[i].data = raw_data + i * MLX5_COUNTERS_PER_POOL;
7592         }
7593         for (i = 0; i < MLX5_MAX_PENDING_QUERIES; ++i)
7594                 LIST_INSERT_HEAD(&sh->cmng.free_stat_raws,
7595                                  mem_mng->raws + MLX5_CNT_CONTAINER_RESIZE + i,
7596                                  next);
7597         LIST_INSERT_HEAD(&sh->cmng.mem_mngs, mem_mng, next);
7598         sh->cmng.mem_mng = mem_mng;
7599         return 0;
7600 }
7601
7602 /**
7603  * Set the statistic memory to the new counter pool.
7604  *
7605  * @param[in] sh
7606  *   Pointer to mlx5_dev_ctx_shared object.
7607  * @param[in] pool
7608  *   Pointer to the pool to set the statistic memory.
7609  *
7610  * @return
7611  *   0 on success, a negative errno value otherwise.
7612  */
7613 static int
7614 mlx5_flow_set_counter_stat_mem(struct mlx5_dev_ctx_shared *sh,
7615                                struct mlx5_flow_counter_pool *pool)
7616 {
7617         struct mlx5_flow_counter_mng *cmng = &sh->cmng;
7618         /* Resize statistic memory once used out. */
7619         if (!(pool->index % MLX5_CNT_CONTAINER_RESIZE) &&
7620             mlx5_flow_create_counter_stat_mem_mng(sh)) {
7621                 DRV_LOG(ERR, "Cannot resize counter stat mem.");
7622                 return -1;
7623         }
7624         rte_spinlock_lock(&pool->sl);
7625         pool->raw = cmng->mem_mng->raws + pool->index %
7626                     MLX5_CNT_CONTAINER_RESIZE;
7627         rte_spinlock_unlock(&pool->sl);
7628         pool->raw_hw = NULL;
7629         return 0;
7630 }
7631
7632 #define MLX5_POOL_QUERY_FREQ_US 1000000
7633
7634 /**
7635  * Set the periodic procedure for triggering asynchronous batch queries for all
7636  * the counter pools.
7637  *
7638  * @param[in] sh
7639  *   Pointer to mlx5_dev_ctx_shared object.
7640  */
7641 void
7642 mlx5_set_query_alarm(struct mlx5_dev_ctx_shared *sh)
7643 {
7644         uint32_t pools_n, us;
7645
7646         pools_n = __atomic_load_n(&sh->cmng.n_valid, __ATOMIC_RELAXED);
7647         us = MLX5_POOL_QUERY_FREQ_US / pools_n;
7648         DRV_LOG(DEBUG, "Set alarm for %u pools each %u us", pools_n, us);
7649         if (rte_eal_alarm_set(us, mlx5_flow_query_alarm, sh)) {
7650                 sh->cmng.query_thread_on = 0;
7651                 DRV_LOG(ERR, "Cannot reinitialize query alarm");
7652         } else {
7653                 sh->cmng.query_thread_on = 1;
7654         }
7655 }
7656
7657 /**
7658  * The periodic procedure for triggering asynchronous batch queries for all the
7659  * counter pools. This function is probably called by the host thread.
7660  *
7661  * @param[in] arg
7662  *   The parameter for the alarm process.
7663  */
7664 void
7665 mlx5_flow_query_alarm(void *arg)
7666 {
7667         struct mlx5_dev_ctx_shared *sh = arg;
7668         int ret;
7669         uint16_t pool_index = sh->cmng.pool_index;
7670         struct mlx5_flow_counter_mng *cmng = &sh->cmng;
7671         struct mlx5_flow_counter_pool *pool;
7672         uint16_t n_valid;
7673
7674         if (sh->cmng.pending_queries >= MLX5_MAX_PENDING_QUERIES)
7675                 goto set_alarm;
7676         rte_spinlock_lock(&cmng->pool_update_sl);
7677         pool = cmng->pools[pool_index];
7678         n_valid = cmng->n_valid;
7679         rte_spinlock_unlock(&cmng->pool_update_sl);
7680         /* Set the statistic memory to the new created pool. */
7681         if ((!pool->raw && mlx5_flow_set_counter_stat_mem(sh, pool)))
7682                 goto set_alarm;
7683         if (pool->raw_hw)
7684                 /* There is a pool query in progress. */
7685                 goto set_alarm;
7686         pool->raw_hw =
7687                 LIST_FIRST(&sh->cmng.free_stat_raws);
7688         if (!pool->raw_hw)
7689                 /* No free counter statistics raw memory. */
7690                 goto set_alarm;
7691         /*
7692          * Identify the counters released between query trigger and query
7693          * handle more efficiently. The counter released in this gap period
7694          * should wait for a new round of query as the new arrived packets
7695          * will not be taken into account.
7696          */
7697         pool->query_gen++;
7698         ret = mlx5_devx_cmd_flow_counter_query(pool->min_dcs, 0,
7699                                                MLX5_COUNTERS_PER_POOL,
7700                                                NULL, NULL,
7701                                                pool->raw_hw->mem_mng->dm->id,
7702                                                (void *)(uintptr_t)
7703                                                pool->raw_hw->data,
7704                                                sh->devx_comp,
7705                                                (uint64_t)(uintptr_t)pool);
7706         if (ret) {
7707                 DRV_LOG(ERR, "Failed to trigger asynchronous query for dcs ID"
7708                         " %d", pool->min_dcs->id);
7709                 pool->raw_hw = NULL;
7710                 goto set_alarm;
7711         }
7712         LIST_REMOVE(pool->raw_hw, next);
7713         sh->cmng.pending_queries++;
7714         pool_index++;
7715         if (pool_index >= n_valid)
7716                 pool_index = 0;
7717 set_alarm:
7718         sh->cmng.pool_index = pool_index;
7719         mlx5_set_query_alarm(sh);
7720 }
7721
7722 /**
7723  * Check and callback event for new aged flow in the counter pool
7724  *
7725  * @param[in] sh
7726  *   Pointer to mlx5_dev_ctx_shared object.
7727  * @param[in] pool
7728  *   Pointer to Current counter pool.
7729  */
7730 static void
7731 mlx5_flow_aging_check(struct mlx5_dev_ctx_shared *sh,
7732                    struct mlx5_flow_counter_pool *pool)
7733 {
7734         struct mlx5_priv *priv;
7735         struct mlx5_flow_counter *cnt;
7736         struct mlx5_age_info *age_info;
7737         struct mlx5_age_param *age_param;
7738         struct mlx5_counter_stats_raw *cur = pool->raw_hw;
7739         struct mlx5_counter_stats_raw *prev = pool->raw;
7740         const uint64_t curr_time = MLX5_CURR_TIME_SEC;
7741         const uint32_t time_delta = curr_time - pool->time_of_last_age_check;
7742         uint16_t expected = AGE_CANDIDATE;
7743         uint32_t i;
7744
7745         pool->time_of_last_age_check = curr_time;
7746         for (i = 0; i < MLX5_COUNTERS_PER_POOL; ++i) {
7747                 cnt = MLX5_POOL_GET_CNT(pool, i);
7748                 age_param = MLX5_CNT_TO_AGE(cnt);
7749                 if (__atomic_load_n(&age_param->state,
7750                                     __ATOMIC_RELAXED) != AGE_CANDIDATE)
7751                         continue;
7752                 if (cur->data[i].hits != prev->data[i].hits) {
7753                         __atomic_store_n(&age_param->sec_since_last_hit, 0,
7754                                          __ATOMIC_RELAXED);
7755                         continue;
7756                 }
7757                 if (__atomic_add_fetch(&age_param->sec_since_last_hit,
7758                                        time_delta,
7759                                        __ATOMIC_RELAXED) <= age_param->timeout)
7760                         continue;
7761                 /**
7762                  * Hold the lock first, or if between the
7763                  * state AGE_TMOUT and tailq operation the
7764                  * release happened, the release procedure
7765                  * may delete a non-existent tailq node.
7766                  */
7767                 priv = rte_eth_devices[age_param->port_id].data->dev_private;
7768                 age_info = GET_PORT_AGE_INFO(priv);
7769                 rte_spinlock_lock(&age_info->aged_sl);
7770                 if (__atomic_compare_exchange_n(&age_param->state, &expected,
7771                                                 AGE_TMOUT, false,
7772                                                 __ATOMIC_RELAXED,
7773                                                 __ATOMIC_RELAXED)) {
7774                         TAILQ_INSERT_TAIL(&age_info->aged_counters, cnt, next);
7775                         MLX5_AGE_SET(age_info, MLX5_AGE_EVENT_NEW);
7776                 }
7777                 rte_spinlock_unlock(&age_info->aged_sl);
7778         }
7779         mlx5_age_event_prepare(sh);
7780 }
7781
7782 /**
7783  * Handler for the HW respond about ready values from an asynchronous batch
7784  * query. This function is probably called by the host thread.
7785  *
7786  * @param[in] sh
7787  *   The pointer to the shared device context.
7788  * @param[in] async_id
7789  *   The Devx async ID.
7790  * @param[in] status
7791  *   The status of the completion.
7792  */
7793 void
7794 mlx5_flow_async_pool_query_handle(struct mlx5_dev_ctx_shared *sh,
7795                                   uint64_t async_id, int status)
7796 {
7797         struct mlx5_flow_counter_pool *pool =
7798                 (struct mlx5_flow_counter_pool *)(uintptr_t)async_id;
7799         struct mlx5_counter_stats_raw *raw_to_free;
7800         uint8_t query_gen = pool->query_gen ^ 1;
7801         struct mlx5_flow_counter_mng *cmng = &sh->cmng;
7802         enum mlx5_counter_type cnt_type =
7803                 pool->is_aged ? MLX5_COUNTER_TYPE_AGE :
7804                                 MLX5_COUNTER_TYPE_ORIGIN;
7805
7806         if (unlikely(status)) {
7807                 raw_to_free = pool->raw_hw;
7808         } else {
7809                 raw_to_free = pool->raw;
7810                 if (pool->is_aged)
7811                         mlx5_flow_aging_check(sh, pool);
7812                 rte_spinlock_lock(&pool->sl);
7813                 pool->raw = pool->raw_hw;
7814                 rte_spinlock_unlock(&pool->sl);
7815                 /* Be sure the new raw counters data is updated in memory. */
7816                 rte_io_wmb();
7817                 if (!TAILQ_EMPTY(&pool->counters[query_gen])) {
7818                         rte_spinlock_lock(&cmng->csl[cnt_type]);
7819                         TAILQ_CONCAT(&cmng->counters[cnt_type],
7820                                      &pool->counters[query_gen], next);
7821                         rte_spinlock_unlock(&cmng->csl[cnt_type]);
7822                 }
7823         }
7824         LIST_INSERT_HEAD(&sh->cmng.free_stat_raws, raw_to_free, next);
7825         pool->raw_hw = NULL;
7826         sh->cmng.pending_queries--;
7827 }
7828
7829 static int
7830 flow_group_to_table(uint32_t port_id, uint32_t group, uint32_t *table,
7831                     const struct flow_grp_info *grp_info,
7832                     struct rte_flow_error *error)
7833 {
7834         if (grp_info->transfer && grp_info->external &&
7835             grp_info->fdb_def_rule) {
7836                 if (group == UINT32_MAX)
7837                         return rte_flow_error_set
7838                                                 (error, EINVAL,
7839                                                  RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
7840                                                  NULL,
7841                                                  "group index not supported");
7842                 *table = group + 1;
7843         } else {
7844                 *table = group;
7845         }
7846         DRV_LOG(DEBUG, "port %u group=%#x table=%#x", port_id, group, *table);
7847         return 0;
7848 }
7849
7850 /**
7851  * Translate the rte_flow group index to HW table value.
7852  *
7853  * If tunnel offload is disabled, all group ids converted to flow table
7854  * id using the standard method.
7855  * If tunnel offload is enabled, group id can be converted using the
7856  * standard or tunnel conversion method. Group conversion method
7857  * selection depends on flags in `grp_info` parameter:
7858  * - Internal (grp_info.external == 0) groups conversion uses the
7859  *   standard method.
7860  * - Group ids in JUMP action converted with the tunnel conversion.
7861  * - Group id in rule attribute conversion depends on a rule type and
7862  *   group id value:
7863  *   ** non zero group attributes converted with the tunnel method
7864  *   ** zero group attribute in non-tunnel rule is converted using the
7865  *      standard method - there's only one root table
7866  *   ** zero group attribute in steer tunnel rule is converted with the
7867  *      standard method - single root table
7868  *   ** zero group attribute in match tunnel rule is a special OvS
7869  *      case: that value is used for portability reasons. That group
7870  *      id is converted with the tunnel conversion method.
7871  *
7872  * @param[in] dev
7873  *   Port device
7874  * @param[in] tunnel
7875  *   PMD tunnel offload object
7876  * @param[in] group
7877  *   rte_flow group index value.
7878  * @param[out] table
7879  *   HW table value.
7880  * @param[in] grp_info
7881  *   flags used for conversion
7882  * @param[out] error
7883  *   Pointer to error structure.
7884  *
7885  * @return
7886  *   0 on success, a negative errno value otherwise and rte_errno is set.
7887  */
7888 int
7889 mlx5_flow_group_to_table(struct rte_eth_dev *dev,
7890                          const struct mlx5_flow_tunnel *tunnel,
7891                          uint32_t group, uint32_t *table,
7892                          const struct flow_grp_info *grp_info,
7893                          struct rte_flow_error *error)
7894 {
7895         int ret;
7896         bool standard_translation;
7897
7898         if (!grp_info->skip_scale && grp_info->external &&
7899             group < MLX5_MAX_TABLES_EXTERNAL)
7900                 group *= MLX5_FLOW_TABLE_FACTOR;
7901         if (is_tunnel_offload_active(dev)) {
7902                 standard_translation = !grp_info->external ||
7903                                         grp_info->std_tbl_fix;
7904         } else {
7905                 standard_translation = true;
7906         }
7907         DRV_LOG(DEBUG,
7908                 "port %u group=%u transfer=%d external=%d fdb_def_rule=%d translate=%s",
7909                 dev->data->port_id, group, grp_info->transfer,
7910                 grp_info->external, grp_info->fdb_def_rule,
7911                 standard_translation ? "STANDARD" : "TUNNEL");
7912         if (standard_translation)
7913                 ret = flow_group_to_table(dev->data->port_id, group, table,
7914                                           grp_info, error);
7915         else
7916                 ret = tunnel_flow_group_to_flow_table(dev, tunnel, group,
7917                                                       table, error);
7918
7919         return ret;
7920 }
7921
7922 /**
7923  * Discover availability of metadata reg_c's.
7924  *
7925  * Iteratively use test flows to check availability.
7926  *
7927  * @param[in] dev
7928  *   Pointer to the Ethernet device structure.
7929  *
7930  * @return
7931  *   0 on success, a negative errno value otherwise and rte_errno is set.
7932  */
7933 int
7934 mlx5_flow_discover_mreg_c(struct rte_eth_dev *dev)
7935 {
7936         struct mlx5_priv *priv = dev->data->dev_private;
7937         struct mlx5_dev_config *config = &priv->config;
7938         enum modify_reg idx;
7939         int n = 0;
7940
7941         /* reg_c[0] and reg_c[1] are reserved. */
7942         config->flow_mreg_c[n++] = REG_C_0;
7943         config->flow_mreg_c[n++] = REG_C_1;
7944         /* Discover availability of other reg_c's. */
7945         for (idx = REG_C_2; idx <= REG_C_7; ++idx) {
7946                 struct rte_flow_attr attr = {
7947                         .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
7948                         .priority = MLX5_FLOW_LOWEST_PRIO_INDICATOR,
7949                         .ingress = 1,
7950                 };
7951                 struct rte_flow_item items[] = {
7952                         [0] = {
7953                                 .type = RTE_FLOW_ITEM_TYPE_END,
7954                         },
7955                 };
7956                 struct rte_flow_action actions[] = {
7957                         [0] = {
7958                                 .type = (enum rte_flow_action_type)
7959                                         MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
7960                                 .conf = &(struct mlx5_flow_action_copy_mreg){
7961                                         .src = REG_C_1,
7962                                         .dst = idx,
7963                                 },
7964                         },
7965                         [1] = {
7966                                 .type = RTE_FLOW_ACTION_TYPE_JUMP,
7967                                 .conf = &(struct rte_flow_action_jump){
7968                                         .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
7969                                 },
7970                         },
7971                         [2] = {
7972                                 .type = RTE_FLOW_ACTION_TYPE_END,
7973                         },
7974                 };
7975                 uint32_t flow_idx;
7976                 struct rte_flow *flow;
7977                 struct rte_flow_error error;
7978
7979                 if (!config->dv_flow_en)
7980                         break;
7981                 /* Create internal flow, validation skips copy action. */
7982                 flow_idx = flow_list_create(dev, MLX5_FLOW_TYPE_GEN, &attr,
7983                                         items, actions, false, &error);
7984                 flow = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
7985                                       flow_idx);
7986                 if (!flow)
7987                         continue;
7988                 config->flow_mreg_c[n++] = idx;
7989                 flow_list_destroy(dev, MLX5_FLOW_TYPE_GEN, flow_idx);
7990         }
7991         for (; n < MLX5_MREG_C_NUM; ++n)
7992                 config->flow_mreg_c[n] = REG_NON;
7993         return 0;
7994 }
7995
7996 int
7997 save_dump_file(const uint8_t *data, uint32_t size,
7998         uint32_t type, uint32_t id, void *arg, FILE *file)
7999 {
8000         char line[BUF_SIZE];
8001         uint32_t out = 0;
8002         uint32_t k;
8003         uint32_t actions_num;
8004         struct rte_flow_query_count *count;
8005
8006         memset(line, 0, BUF_SIZE);
8007         switch (type) {
8008         case DR_DUMP_REC_TYPE_PMD_MODIFY_HDR:
8009                 actions_num = *(uint32_t *)(arg);
8010                 out += snprintf(line + out, BUF_SIZE - out, "%d,0x%x,%d,",
8011                                 type, id, actions_num);
8012                 break;
8013         case DR_DUMP_REC_TYPE_PMD_PKT_REFORMAT:
8014                 out += snprintf(line + out, BUF_SIZE - out, "%d,0x%x,",
8015                                 type, id);
8016                 break;
8017         case DR_DUMP_REC_TYPE_PMD_COUNTER:
8018                 count = (struct rte_flow_query_count *)arg;
8019                 fprintf(file, "%d,0x%x,%" PRIu64 ",%" PRIu64 "\n", type,
8020                                 id, count->hits, count->bytes);
8021                 return 0;
8022         default:
8023                 return -1;
8024         }
8025
8026         for (k = 0; k < size; k++) {
8027                 /* Make sure we do not overrun the line buffer length. */
8028                 if (out >= BUF_SIZE - 4) {
8029                         line[out] = '\0';
8030                         break;
8031                 }
8032                 out += snprintf(line + out, BUF_SIZE - out, "%02x",
8033                                 (data[k]) & 0xff);
8034         }
8035         fprintf(file, "%s\n", line);
8036         return 0;
8037 }
8038
8039 int
8040 mlx5_flow_query_counter(struct rte_eth_dev *dev, struct rte_flow *flow,
8041         struct rte_flow_query_count *count, struct rte_flow_error *error)
8042 {
8043         struct rte_flow_action action[2];
8044         enum mlx5_flow_drv_type ftype;
8045         const struct mlx5_flow_driver_ops *fops;
8046
8047         if (!flow) {
8048                 return rte_flow_error_set(error, ENOENT,
8049                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8050                                 NULL,
8051                                 "invalid flow handle");
8052         }
8053         action[0].type = RTE_FLOW_ACTION_TYPE_COUNT;
8054         action[1].type = RTE_FLOW_ACTION_TYPE_END;
8055         if (flow->counter) {
8056                 memset(count, 0, sizeof(struct rte_flow_query_count));
8057                 ftype = (enum mlx5_flow_drv_type)(flow->drv_type);
8058                 MLX5_ASSERT(ftype > MLX5_FLOW_TYPE_MIN &&
8059                                                 ftype < MLX5_FLOW_TYPE_MAX);
8060                 fops = flow_get_drv_ops(ftype);
8061                 return fops->query(dev, flow, action, count, error);
8062         }
8063         return -1;
8064 }
8065
8066 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
8067 /**
8068  * Dump flow ipool data to file
8069  *
8070  * @param[in] dev
8071  *   The pointer to Ethernet device.
8072  * @param[in] file
8073  *   A pointer to a file for output.
8074  * @param[out] error
8075  *   Perform verbose error reporting if not NULL. PMDs initialize this
8076  *   structure in case of error only.
8077  * @return
8078  *   0 on success, a negative value otherwise.
8079  */
8080 int
8081 mlx5_flow_dev_dump_ipool(struct rte_eth_dev *dev,
8082         struct rte_flow *flow, FILE *file,
8083         struct rte_flow_error *error)
8084 {
8085         struct mlx5_priv *priv = dev->data->dev_private;
8086         struct mlx5_flow_dv_modify_hdr_resource  *modify_hdr;
8087         struct mlx5_flow_dv_encap_decap_resource *encap_decap;
8088         uint32_t handle_idx;
8089         struct mlx5_flow_handle *dh;
8090         struct rte_flow_query_count count;
8091         uint32_t actions_num;
8092         const uint8_t *data;
8093         size_t size;
8094         uint32_t id;
8095         uint32_t type;
8096
8097         if (!flow) {
8098                 return rte_flow_error_set(error, ENOENT,
8099                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
8100                         NULL,
8101                         "invalid flow handle");
8102         }
8103         handle_idx = flow->dev_handles;
8104         while (handle_idx) {
8105                 dh = mlx5_ipool_get(priv->sh->ipool
8106                         [MLX5_IPOOL_MLX5_FLOW], handle_idx);
8107                 if (!dh)
8108                         continue;
8109                 handle_idx = dh->next.next;
8110                 id = (uint32_t)(uintptr_t)dh->drv_flow;
8111
8112                 /* query counter */
8113                 type = DR_DUMP_REC_TYPE_PMD_COUNTER;
8114                 if (!mlx5_flow_query_counter(dev, flow, &count, error))
8115                         save_dump_file(NULL, 0, type,
8116                                         id, (void *)&count, file);
8117
8118                 /* Get modify_hdr and encap_decap buf from ipools. */
8119                 encap_decap = NULL;
8120                 modify_hdr = dh->dvh.modify_hdr;
8121
8122                 if (dh->dvh.rix_encap_decap) {
8123                         encap_decap = mlx5_ipool_get(priv->sh->ipool
8124                                                 [MLX5_IPOOL_DECAP_ENCAP],
8125                                                 dh->dvh.rix_encap_decap);
8126                 }
8127                 if (modify_hdr) {
8128                         data = (const uint8_t *)modify_hdr->actions;
8129                         size = (size_t)(modify_hdr->actions_num) * 8;
8130                         actions_num = modify_hdr->actions_num;
8131                         type = DR_DUMP_REC_TYPE_PMD_MODIFY_HDR;
8132                         save_dump_file(data, size, type, id,
8133                                         (void *)(&actions_num), file);
8134                 }
8135                 if (encap_decap) {
8136                         data = encap_decap->buf;
8137                         size = encap_decap->size;
8138                         type = DR_DUMP_REC_TYPE_PMD_PKT_REFORMAT;
8139                         save_dump_file(data, size, type,
8140                                                 id, NULL, file);
8141                 }
8142         }
8143         return 0;
8144 }
8145 #endif
8146
8147 /**
8148  * Dump flow raw hw data to file
8149  *
8150  * @param[in] dev
8151  *    The pointer to Ethernet device.
8152  * @param[in] file
8153  *   A pointer to a file for output.
8154  * @param[out] error
8155  *   Perform verbose error reporting if not NULL. PMDs initialize this
8156  *   structure in case of error only.
8157  * @return
8158  *   0 on success, a nagative value otherwise.
8159  */
8160 int
8161 mlx5_flow_dev_dump(struct rte_eth_dev *dev, struct rte_flow *flow_idx,
8162                    FILE *file,
8163                    struct rte_flow_error *error __rte_unused)
8164 {
8165         struct mlx5_priv *priv = dev->data->dev_private;
8166         struct mlx5_dev_ctx_shared *sh = priv->sh;
8167         uint32_t handle_idx;
8168         int ret;
8169         struct mlx5_flow_handle *dh;
8170         struct rte_flow *flow;
8171 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
8172         uint32_t idx;
8173 #endif
8174
8175         if (!priv->config.dv_flow_en) {
8176                 if (fputs("device dv flow disabled\n", file) <= 0)
8177                         return -errno;
8178                 return -ENOTSUP;
8179         }
8180
8181         /* dump all */
8182         if (!flow_idx) {
8183 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
8184                 MLX5_IPOOL_FOREACH(priv->flows[MLX5_FLOW_TYPE_GEN], idx, flow)
8185                         mlx5_flow_dev_dump_ipool(dev, flow, file, error);
8186 #endif
8187                 return mlx5_devx_cmd_flow_dump(sh->fdb_domain,
8188                                         sh->rx_domain,
8189                                         sh->tx_domain, file);
8190         }
8191         /* dump one */
8192         flow = mlx5_ipool_get(priv->flows[MLX5_FLOW_TYPE_GEN],
8193                         (uintptr_t)(void *)flow_idx);
8194         if (!flow)
8195                 return -ENOENT;
8196
8197 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
8198         mlx5_flow_dev_dump_ipool(dev, flow, file, error);
8199 #endif
8200         handle_idx = flow->dev_handles;
8201         while (handle_idx) {
8202                 dh = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MLX5_FLOW],
8203                                 handle_idx);
8204                 if (!dh)
8205                         return -ENOENT;
8206                 if (dh->drv_flow) {
8207                         ret = mlx5_devx_cmd_flow_single_dump(dh->drv_flow,
8208                                         file);
8209                         if (ret)
8210                                 return -ENOENT;
8211                 }
8212                 handle_idx = dh->next.next;
8213         }
8214         return 0;
8215 }
8216
8217 /**
8218  * Get aged-out flows.
8219  *
8220  * @param[in] dev
8221  *   Pointer to the Ethernet device structure.
8222  * @param[in] context
8223  *   The address of an array of pointers to the aged-out flows contexts.
8224  * @param[in] nb_countexts
8225  *   The length of context array pointers.
8226  * @param[out] error
8227  *   Perform verbose error reporting if not NULL. Initialized in case of
8228  *   error only.
8229  *
8230  * @return
8231  *   how many contexts get in success, otherwise negative errno value.
8232  *   if nb_contexts is 0, return the amount of all aged contexts.
8233  *   if nb_contexts is not 0 , return the amount of aged flows reported
8234  *   in the context array.
8235  */
8236 int
8237 mlx5_flow_get_aged_flows(struct rte_eth_dev *dev, void **contexts,
8238                         uint32_t nb_contexts, struct rte_flow_error *error)
8239 {
8240         const struct mlx5_flow_driver_ops *fops;
8241         struct rte_flow_attr attr = { .transfer = 0 };
8242
8243         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
8244                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
8245                 return fops->get_aged_flows(dev, contexts, nb_contexts,
8246                                                     error);
8247         }
8248         DRV_LOG(ERR,
8249                 "port %u get aged flows is not supported.",
8250                  dev->data->port_id);
8251         return -ENOTSUP;
8252 }
8253
8254 /* Wrapper for driver action_validate op callback */
8255 static int
8256 flow_drv_action_validate(struct rte_eth_dev *dev,
8257                          const struct rte_flow_indir_action_conf *conf,
8258                          const struct rte_flow_action *action,
8259                          const struct mlx5_flow_driver_ops *fops,
8260                          struct rte_flow_error *error)
8261 {
8262         static const char err_msg[] = "indirect action validation unsupported";
8263
8264         if (!fops->action_validate) {
8265                 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
8266                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
8267                                    NULL, err_msg);
8268                 return -rte_errno;
8269         }
8270         return fops->action_validate(dev, conf, action, error);
8271 }
8272
8273 /**
8274  * Destroys the shared action by handle.
8275  *
8276  * @param dev
8277  *   Pointer to Ethernet device structure.
8278  * @param[in] handle
8279  *   Handle for the indirect action object to be destroyed.
8280  * @param[out] error
8281  *   Perform verbose error reporting if not NULL. PMDs initialize this
8282  *   structure in case of error only.
8283  *
8284  * @return
8285  *   0 on success, a negative errno value otherwise and rte_errno is set.
8286  *
8287  * @note: wrapper for driver action_create op callback.
8288  */
8289 static int
8290 mlx5_action_handle_destroy(struct rte_eth_dev *dev,
8291                            struct rte_flow_action_handle *handle,
8292                            struct rte_flow_error *error)
8293 {
8294         static const char err_msg[] = "indirect action destruction unsupported";
8295         struct rte_flow_attr attr = { .transfer = 0 };
8296         const struct mlx5_flow_driver_ops *fops =
8297                         flow_get_drv_ops(flow_get_drv_type(dev, &attr));
8298
8299         if (!fops->action_destroy) {
8300                 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
8301                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
8302                                    NULL, err_msg);
8303                 return -rte_errno;
8304         }
8305         return fops->action_destroy(dev, handle, error);
8306 }
8307
8308 /* Wrapper for driver action_destroy op callback */
8309 static int
8310 flow_drv_action_update(struct rte_eth_dev *dev,
8311                        struct rte_flow_action_handle *handle,
8312                        const void *update,
8313                        const struct mlx5_flow_driver_ops *fops,
8314                        struct rte_flow_error *error)
8315 {
8316         static const char err_msg[] = "indirect action update unsupported";
8317
8318         if (!fops->action_update) {
8319                 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
8320                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
8321                                    NULL, err_msg);
8322                 return -rte_errno;
8323         }
8324         return fops->action_update(dev, handle, update, error);
8325 }
8326
8327 /* Wrapper for driver action_destroy op callback */
8328 static int
8329 flow_drv_action_query(struct rte_eth_dev *dev,
8330                       const struct rte_flow_action_handle *handle,
8331                       void *data,
8332                       const struct mlx5_flow_driver_ops *fops,
8333                       struct rte_flow_error *error)
8334 {
8335         static const char err_msg[] = "indirect action query unsupported";
8336
8337         if (!fops->action_query) {
8338                 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
8339                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
8340                                    NULL, err_msg);
8341                 return -rte_errno;
8342         }
8343         return fops->action_query(dev, handle, data, error);
8344 }
8345
8346 /**
8347  * Create indirect action for reuse in multiple flow rules.
8348  *
8349  * @param dev
8350  *   Pointer to Ethernet device structure.
8351  * @param conf
8352  *   Pointer to indirect action object configuration.
8353  * @param[in] action
8354  *   Action configuration for indirect action object creation.
8355  * @param[out] error
8356  *   Perform verbose error reporting if not NULL. PMDs initialize this
8357  *   structure in case of error only.
8358  * @return
8359  *   A valid handle in case of success, NULL otherwise and rte_errno is set.
8360  */
8361 static struct rte_flow_action_handle *
8362 mlx5_action_handle_create(struct rte_eth_dev *dev,
8363                           const struct rte_flow_indir_action_conf *conf,
8364                           const struct rte_flow_action *action,
8365                           struct rte_flow_error *error)
8366 {
8367         static const char err_msg[] = "indirect action creation unsupported";
8368         struct rte_flow_attr attr = { .transfer = 0 };
8369         const struct mlx5_flow_driver_ops *fops =
8370                         flow_get_drv_ops(flow_get_drv_type(dev, &attr));
8371
8372         if (flow_drv_action_validate(dev, conf, action, fops, error))
8373                 return NULL;
8374         if (!fops->action_create) {
8375                 DRV_LOG(ERR, "port %u %s.", dev->data->port_id, err_msg);
8376                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
8377                                    NULL, err_msg);
8378                 return NULL;
8379         }
8380         return fops->action_create(dev, conf, action, error);
8381 }
8382
8383 /**
8384  * Updates inplace the indirect action configuration pointed by *handle*
8385  * with the configuration provided as *update* argument.
8386  * The update of the indirect action configuration effects all flow rules
8387  * reusing the action via handle.
8388  *
8389  * @param dev
8390  *   Pointer to Ethernet device structure.
8391  * @param[in] handle
8392  *   Handle for the indirect action to be updated.
8393  * @param[in] update
8394  *   Action specification used to modify the action pointed by handle.
8395  *   *update* could be of same type with the action pointed by the *handle*
8396  *   handle argument, or some other structures like a wrapper, depending on
8397  *   the indirect action type.
8398  * @param[out] error
8399  *   Perform verbose error reporting if not NULL. PMDs initialize this
8400  *   structure in case of error only.
8401  *
8402  * @return
8403  *   0 on success, a negative errno value otherwise and rte_errno is set.
8404  */
8405 static int
8406 mlx5_action_handle_update(struct rte_eth_dev *dev,
8407                 struct rte_flow_action_handle *handle,
8408                 const void *update,
8409                 struct rte_flow_error *error)
8410 {
8411         struct rte_flow_attr attr = { .transfer = 0 };
8412         const struct mlx5_flow_driver_ops *fops =
8413                         flow_get_drv_ops(flow_get_drv_type(dev, &attr));
8414         int ret;
8415
8416         ret = flow_drv_action_validate(dev, NULL,
8417                         (const struct rte_flow_action *)update, fops, error);
8418         if (ret)
8419                 return ret;
8420         return flow_drv_action_update(dev, handle, update, fops,
8421                                       error);
8422 }
8423
8424 /**
8425  * Query the indirect action by handle.
8426  *
8427  * This function allows retrieving action-specific data such as counters.
8428  * Data is gathered by special action which may be present/referenced in
8429  * more than one flow rule definition.
8430  *
8431  * see @RTE_FLOW_ACTION_TYPE_COUNT
8432  *
8433  * @param dev
8434  *   Pointer to Ethernet device structure.
8435  * @param[in] handle
8436  *   Handle for the indirect action to query.
8437  * @param[in, out] data
8438  *   Pointer to storage for the associated query data type.
8439  * @param[out] error
8440  *   Perform verbose error reporting if not NULL. PMDs initialize this
8441  *   structure in case of error only.
8442  *
8443  * @return
8444  *   0 on success, a negative errno value otherwise and rte_errno is set.
8445  */
8446 static int
8447 mlx5_action_handle_query(struct rte_eth_dev *dev,
8448                          const struct rte_flow_action_handle *handle,
8449                          void *data,
8450                          struct rte_flow_error *error)
8451 {
8452         struct rte_flow_attr attr = { .transfer = 0 };
8453         const struct mlx5_flow_driver_ops *fops =
8454                         flow_get_drv_ops(flow_get_drv_type(dev, &attr));
8455
8456         return flow_drv_action_query(dev, handle, data, fops, error);
8457 }
8458
8459 /**
8460  * Destroy all indirect actions (shared RSS).
8461  *
8462  * @param dev
8463  *   Pointer to Ethernet device.
8464  *
8465  * @return
8466  *   0 on success, a negative errno value otherwise and rte_errno is set.
8467  */
8468 int
8469 mlx5_action_handle_flush(struct rte_eth_dev *dev)
8470 {
8471         struct rte_flow_error error;
8472         struct mlx5_priv *priv = dev->data->dev_private;
8473         struct mlx5_shared_action_rss *shared_rss;
8474         int ret = 0;
8475         uint32_t idx;
8476
8477         ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_RSS_SHARED_ACTIONS],
8478                       priv->rss_shared_actions, idx, shared_rss, next) {
8479                 ret |= mlx5_action_handle_destroy(dev,
8480                        (struct rte_flow_action_handle *)(uintptr_t)idx, &error);
8481         }
8482         return ret;
8483 }
8484
8485 #ifndef HAVE_MLX5DV_DR
8486 #define MLX5_DOMAIN_SYNC_FLOW ((1 << 0) | (1 << 1))
8487 #else
8488 #define MLX5_DOMAIN_SYNC_FLOW \
8489         (MLX5DV_DR_DOMAIN_SYNC_FLAGS_SW | MLX5DV_DR_DOMAIN_SYNC_FLAGS_HW)
8490 #endif
8491
8492 int rte_pmd_mlx5_sync_flow(uint16_t port_id, uint32_t domains)
8493 {
8494         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
8495         const struct mlx5_flow_driver_ops *fops;
8496         int ret;
8497         struct rte_flow_attr attr = { .transfer = 0 };
8498
8499         fops = flow_get_drv_ops(flow_get_drv_type(dev, &attr));
8500         ret = fops->sync_domain(dev, domains, MLX5_DOMAIN_SYNC_FLOW);
8501         if (ret > 0)
8502                 ret = -ret;
8503         return ret;
8504 }
8505
8506 const struct mlx5_flow_tunnel *
8507 mlx5_get_tof(const struct rte_flow_item *item,
8508              const struct rte_flow_action *action,
8509              enum mlx5_tof_rule_type *rule_type)
8510 {
8511         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
8512                 if (item->type == (typeof(item->type))
8513                                   MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL) {
8514                         *rule_type = MLX5_TUNNEL_OFFLOAD_MATCH_RULE;
8515                         return flow_items_to_tunnel(item);
8516                 }
8517         }
8518         for (; action->conf != RTE_FLOW_ACTION_TYPE_END; action++) {
8519                 if (action->type == (typeof(action->type))
8520                                     MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET) {
8521                         *rule_type = MLX5_TUNNEL_OFFLOAD_SET_RULE;
8522                         return flow_actions_to_tunnel(action);
8523                 }
8524         }
8525         return NULL;
8526 }
8527
8528 /**
8529  * tunnel offload functionalilty is defined for DV environment only
8530  */
8531 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
8532 __extension__
8533 union tunnel_offload_mark {
8534         uint32_t val;
8535         struct {
8536                 uint32_t app_reserve:8;
8537                 uint32_t table_id:15;
8538                 uint32_t transfer:1;
8539                 uint32_t _unused_:8;
8540         };
8541 };
8542
8543 static bool
8544 mlx5_access_tunnel_offload_db
8545         (struct rte_eth_dev *dev,
8546          bool (*match)(struct rte_eth_dev *,
8547                        struct mlx5_flow_tunnel *, const void *),
8548          void (*hit)(struct rte_eth_dev *, struct mlx5_flow_tunnel *, void *),
8549          void (*miss)(struct rte_eth_dev *, void *),
8550          void *ctx, bool lock_op);
8551
8552 static int
8553 flow_tunnel_add_default_miss(struct rte_eth_dev *dev,
8554                              struct rte_flow *flow,
8555                              const struct rte_flow_attr *attr,
8556                              const struct rte_flow_action *app_actions,
8557                              uint32_t flow_idx,
8558                              const struct mlx5_flow_tunnel *tunnel,
8559                              struct tunnel_default_miss_ctx *ctx,
8560                              struct rte_flow_error *error)
8561 {
8562         struct mlx5_priv *priv = dev->data->dev_private;
8563         struct mlx5_flow *dev_flow;
8564         struct rte_flow_attr miss_attr = *attr;
8565         const struct rte_flow_item miss_items[2] = {
8566                 {
8567                         .type = RTE_FLOW_ITEM_TYPE_ETH,
8568                         .spec = NULL,
8569                         .last = NULL,
8570                         .mask = NULL
8571                 },
8572                 {
8573                         .type = RTE_FLOW_ITEM_TYPE_END,
8574                         .spec = NULL,
8575                         .last = NULL,
8576                         .mask = NULL
8577                 }
8578         };
8579         union tunnel_offload_mark mark_id;
8580         struct rte_flow_action_mark miss_mark;
8581         struct rte_flow_action miss_actions[3] = {
8582                 [0] = { .type = RTE_FLOW_ACTION_TYPE_MARK, .conf = &miss_mark },
8583                 [2] = { .type = RTE_FLOW_ACTION_TYPE_END,  .conf = NULL }
8584         };
8585         const struct rte_flow_action_jump *jump_data;
8586         uint32_t i, flow_table = 0; /* prevent compilation warning */
8587         struct flow_grp_info grp_info = {
8588                 .external = 1,
8589                 .transfer = attr->transfer,
8590                 .fdb_def_rule = !!priv->fdb_def_rule,
8591                 .std_tbl_fix = 0,
8592         };
8593         int ret;
8594
8595         if (!attr->transfer) {
8596                 uint32_t q_size;
8597
8598                 miss_actions[1].type = RTE_FLOW_ACTION_TYPE_RSS;
8599                 q_size = priv->reta_idx_n * sizeof(ctx->queue[0]);
8600                 ctx->queue = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, q_size,
8601                                          0, SOCKET_ID_ANY);
8602                 if (!ctx->queue)
8603                         return rte_flow_error_set
8604                                 (error, ENOMEM,
8605                                 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
8606                                 NULL, "invalid default miss RSS");
8607                 ctx->action_rss.func = RTE_ETH_HASH_FUNCTION_DEFAULT,
8608                 ctx->action_rss.level = 0,
8609                 ctx->action_rss.types = priv->rss_conf.rss_hf,
8610                 ctx->action_rss.key_len = priv->rss_conf.rss_key_len,
8611                 ctx->action_rss.queue_num = priv->reta_idx_n,
8612                 ctx->action_rss.key = priv->rss_conf.rss_key,
8613                 ctx->action_rss.queue = ctx->queue;
8614                 if (!priv->reta_idx_n || !priv->rxqs_n)
8615                         return rte_flow_error_set
8616                                 (error, EINVAL,
8617                                 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
8618                                 NULL, "invalid port configuration");
8619                 if (!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
8620                         ctx->action_rss.types = 0;
8621                 for (i = 0; i != priv->reta_idx_n; ++i)
8622                         ctx->queue[i] = (*priv->reta_idx)[i];
8623         } else {
8624                 miss_actions[1].type = RTE_FLOW_ACTION_TYPE_JUMP;
8625                 ctx->miss_jump.group = MLX5_TNL_MISS_FDB_JUMP_GRP;
8626         }
8627         miss_actions[1].conf = (typeof(miss_actions[1].conf))ctx->raw;
8628         for (; app_actions->type != RTE_FLOW_ACTION_TYPE_JUMP; app_actions++);
8629         jump_data = app_actions->conf;
8630         miss_attr.priority = MLX5_TNL_MISS_RULE_PRIORITY;
8631         miss_attr.group = jump_data->group;
8632         ret = mlx5_flow_group_to_table(dev, tunnel, jump_data->group,
8633                                        &flow_table, &grp_info, error);
8634         if (ret)
8635                 return rte_flow_error_set(error, EINVAL,
8636                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
8637                                           NULL, "invalid tunnel id");
8638         mark_id.app_reserve = 0;
8639         mark_id.table_id = tunnel_flow_tbl_to_id(flow_table);
8640         mark_id.transfer = !!attr->transfer;
8641         mark_id._unused_ = 0;
8642         miss_mark.id = mark_id.val;
8643         dev_flow = flow_drv_prepare(dev, flow, &miss_attr,
8644                                     miss_items, miss_actions, flow_idx, error);
8645         if (!dev_flow)
8646                 return -rte_errno;
8647         dev_flow->flow = flow;
8648         dev_flow->external = true;
8649         dev_flow->tunnel = tunnel;
8650         dev_flow->tof_type = MLX5_TUNNEL_OFFLOAD_MISS_RULE;
8651         /* Subflow object was created, we must include one in the list. */
8652         SILIST_INSERT(&flow->dev_handles, dev_flow->handle_idx,
8653                       dev_flow->handle, next);
8654         DRV_LOG(DEBUG,
8655                 "port %u tunnel type=%d id=%u miss rule priority=%u group=%u",
8656                 dev->data->port_id, tunnel->app_tunnel.type,
8657                 tunnel->tunnel_id, miss_attr.priority, miss_attr.group);
8658         ret = flow_drv_translate(dev, dev_flow, &miss_attr, miss_items,
8659                                   miss_actions, error);
8660         if (!ret)
8661                 ret = flow_mreg_update_copy_table(dev, flow, miss_actions,
8662                                                   error);
8663
8664         return ret;
8665 }
8666
8667 static const struct mlx5_flow_tbl_data_entry  *
8668 tunnel_mark_decode(struct rte_eth_dev *dev, uint32_t mark)
8669 {
8670         struct mlx5_priv *priv = dev->data->dev_private;
8671         struct mlx5_dev_ctx_shared *sh = priv->sh;
8672         struct mlx5_list_entry *he;
8673         union tunnel_offload_mark mbits = { .val = mark };
8674         union mlx5_flow_tbl_key table_key = {
8675                 {
8676                         .level = tunnel_id_to_flow_tbl(mbits.table_id),
8677                         .id = 0,
8678                         .reserved = 0,
8679                         .dummy = 0,
8680                         .is_fdb = !!mbits.transfer,
8681                         .is_egress = 0,
8682                 }
8683         };
8684         struct mlx5_flow_cb_ctx ctx = {
8685                 .data = &table_key.v64,
8686         };
8687
8688         he = mlx5_hlist_lookup(sh->flow_tbls, table_key.v64, &ctx);
8689         return he ?
8690                container_of(he, struct mlx5_flow_tbl_data_entry, entry) : NULL;
8691 }
8692
8693 static void
8694 mlx5_flow_tunnel_grp2tbl_remove_cb(void *tool_ctx,
8695                                    struct mlx5_list_entry *entry)
8696 {
8697         struct mlx5_dev_ctx_shared *sh = tool_ctx;
8698         struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
8699
8700         mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
8701                         tunnel_flow_tbl_to_id(tte->flow_table));
8702         mlx5_free(tte);
8703 }
8704
8705 static int
8706 mlx5_flow_tunnel_grp2tbl_match_cb(void *tool_ctx __rte_unused,
8707                                   struct mlx5_list_entry *entry, void *cb_ctx)
8708 {
8709         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
8710         union tunnel_tbl_key tbl = {
8711                 .val = *(uint64_t *)(ctx->data),
8712         };
8713         struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
8714
8715         return tbl.tunnel_id != tte->tunnel_id || tbl.group != tte->group;
8716 }
8717
8718 static struct mlx5_list_entry *
8719 mlx5_flow_tunnel_grp2tbl_create_cb(void *tool_ctx, void *cb_ctx)
8720 {
8721         struct mlx5_dev_ctx_shared *sh = tool_ctx;
8722         struct mlx5_flow_cb_ctx *ctx = cb_ctx;
8723         struct tunnel_tbl_entry *tte;
8724         union tunnel_tbl_key tbl = {
8725                 .val = *(uint64_t *)(ctx->data),
8726         };
8727
8728         tte = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO,
8729                           sizeof(*tte), 0,
8730                           SOCKET_ID_ANY);
8731         if (!tte)
8732                 goto err;
8733         mlx5_ipool_malloc(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
8734                           &tte->flow_table);
8735         if (tte->flow_table >= MLX5_MAX_TABLES) {
8736                 DRV_LOG(ERR, "Tunnel TBL ID %d exceed max limit.",
8737                         tte->flow_table);
8738                 mlx5_ipool_free(sh->ipool[MLX5_IPOOL_TNL_TBL_ID],
8739                                 tte->flow_table);
8740                 goto err;
8741         } else if (!tte->flow_table) {
8742                 goto err;
8743         }
8744         tte->flow_table = tunnel_id_to_flow_tbl(tte->flow_table);
8745         tte->tunnel_id = tbl.tunnel_id;
8746         tte->group = tbl.group;
8747         return &tte->hash;
8748 err:
8749         if (tte)
8750                 mlx5_free(tte);
8751         return NULL;
8752 }
8753
8754 static struct mlx5_list_entry *
8755 mlx5_flow_tunnel_grp2tbl_clone_cb(void *tool_ctx __rte_unused,
8756                                   struct mlx5_list_entry *oentry,
8757                                   void *cb_ctx __rte_unused)
8758 {
8759         struct tunnel_tbl_entry *tte = mlx5_malloc(MLX5_MEM_SYS, sizeof(*tte),
8760                                                    0, SOCKET_ID_ANY);
8761
8762         if (!tte)
8763                 return NULL;
8764         memcpy(tte, oentry, sizeof(*tte));
8765         return &tte->hash;
8766 }
8767
8768 static void
8769 mlx5_flow_tunnel_grp2tbl_clone_free_cb(void *tool_ctx __rte_unused,
8770                                        struct mlx5_list_entry *entry)
8771 {
8772         struct tunnel_tbl_entry *tte = container_of(entry, typeof(*tte), hash);
8773
8774         mlx5_free(tte);
8775 }
8776
8777 static uint32_t
8778 tunnel_flow_group_to_flow_table(struct rte_eth_dev *dev,
8779                                 const struct mlx5_flow_tunnel *tunnel,
8780                                 uint32_t group, uint32_t *table,
8781                                 struct rte_flow_error *error)
8782 {
8783         struct mlx5_list_entry *he;
8784         struct tunnel_tbl_entry *tte;
8785         union tunnel_tbl_key key = {
8786                 .tunnel_id = tunnel ? tunnel->tunnel_id : 0,
8787                 .group = group
8788         };
8789         struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
8790         struct mlx5_hlist *group_hash;
8791         struct mlx5_flow_cb_ctx ctx = {
8792                 .data = &key.val,
8793         };
8794
8795         group_hash = tunnel ? tunnel->groups : thub->groups;
8796         he = mlx5_hlist_register(group_hash, key.val, &ctx);
8797         if (!he)
8798                 return rte_flow_error_set(error, EINVAL,
8799                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
8800                                           NULL,
8801                                           "tunnel group index not supported");
8802         tte = container_of(he, typeof(*tte), hash);
8803         *table = tte->flow_table;
8804         DRV_LOG(DEBUG, "port %u tunnel %u group=%#x table=%#x",
8805                 dev->data->port_id, key.tunnel_id, group, *table);
8806         return 0;
8807 }
8808
8809 static void
8810 mlx5_flow_tunnel_free(struct rte_eth_dev *dev,
8811                       struct mlx5_flow_tunnel *tunnel)
8812 {
8813         struct mlx5_priv *priv = dev->data->dev_private;
8814         struct mlx5_indexed_pool *ipool;
8815
8816         DRV_LOG(DEBUG, "port %u release pmd tunnel id=0x%x",
8817                 dev->data->port_id, tunnel->tunnel_id);
8818         LIST_REMOVE(tunnel, chain);
8819         mlx5_hlist_destroy(tunnel->groups);
8820         ipool = priv->sh->ipool[MLX5_IPOOL_TUNNEL_ID];
8821         mlx5_ipool_free(ipool, tunnel->tunnel_id);
8822 }
8823
8824 static bool
8825 mlx5_access_tunnel_offload_db
8826         (struct rte_eth_dev *dev,
8827          bool (*match)(struct rte_eth_dev *,
8828                        struct mlx5_flow_tunnel *, const void *),
8829          void (*hit)(struct rte_eth_dev *, struct mlx5_flow_tunnel *, void *),
8830          void (*miss)(struct rte_eth_dev *, void *),
8831          void *ctx, bool lock_op)
8832 {
8833         bool verdict = false;
8834         struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
8835         struct mlx5_flow_tunnel *tunnel;
8836
8837         rte_spinlock_lock(&thub->sl);
8838         LIST_FOREACH(tunnel, &thub->tunnels, chain) {
8839                 verdict = match(dev, tunnel, (const void *)ctx);
8840                 if (verdict)
8841                         break;
8842         }
8843         if (!lock_op)
8844                 rte_spinlock_unlock(&thub->sl);
8845         if (verdict && hit)
8846                 hit(dev, tunnel, ctx);
8847         if (!verdict && miss)
8848                 miss(dev, ctx);
8849         if (lock_op)
8850                 rte_spinlock_unlock(&thub->sl);
8851
8852         return verdict;
8853 }
8854
8855 struct tunnel_db_find_tunnel_id_ctx {
8856         uint32_t tunnel_id;
8857         struct mlx5_flow_tunnel *tunnel;
8858 };
8859
8860 static bool
8861 find_tunnel_id_match(struct rte_eth_dev *dev,
8862                      struct mlx5_flow_tunnel *tunnel, const void *x)
8863 {
8864         const struct tunnel_db_find_tunnel_id_ctx *ctx = x;
8865
8866         RTE_SET_USED(dev);
8867         return tunnel->tunnel_id == ctx->tunnel_id;
8868 }
8869
8870 static void
8871 find_tunnel_id_hit(struct rte_eth_dev *dev,
8872                    struct mlx5_flow_tunnel *tunnel, void *x)
8873 {
8874         struct tunnel_db_find_tunnel_id_ctx *ctx = x;
8875         RTE_SET_USED(dev);
8876         ctx->tunnel = tunnel;
8877 }
8878
8879 static struct mlx5_flow_tunnel *
8880 mlx5_find_tunnel_id(struct rte_eth_dev *dev, uint32_t id)
8881 {
8882         struct tunnel_db_find_tunnel_id_ctx ctx = {
8883                 .tunnel_id = id,
8884         };
8885
8886         mlx5_access_tunnel_offload_db(dev, find_tunnel_id_match,
8887                                       find_tunnel_id_hit, NULL, &ctx, true);
8888
8889         return ctx.tunnel;
8890 }
8891
8892 static struct mlx5_flow_tunnel *
8893 mlx5_flow_tunnel_allocate(struct rte_eth_dev *dev,
8894                           const struct rte_flow_tunnel *app_tunnel)
8895 {
8896         struct mlx5_priv *priv = dev->data->dev_private;
8897         struct mlx5_indexed_pool *ipool;
8898         struct mlx5_flow_tunnel *tunnel;
8899         uint32_t id;
8900
8901         ipool = priv->sh->ipool[MLX5_IPOOL_TUNNEL_ID];
8902         tunnel = mlx5_ipool_zmalloc(ipool, &id);
8903         if (!tunnel)
8904                 return NULL;
8905         if (id >= MLX5_MAX_TUNNELS) {
8906                 mlx5_ipool_free(ipool, id);
8907                 DRV_LOG(ERR, "Tunnel ID %d exceed max limit.", id);
8908                 return NULL;
8909         }
8910         tunnel->groups = mlx5_hlist_create("tunnel groups", 64, false, true,
8911                                            priv->sh,
8912                                            mlx5_flow_tunnel_grp2tbl_create_cb,
8913                                            mlx5_flow_tunnel_grp2tbl_match_cb,
8914                                            mlx5_flow_tunnel_grp2tbl_remove_cb,
8915                                            mlx5_flow_tunnel_grp2tbl_clone_cb,
8916                                         mlx5_flow_tunnel_grp2tbl_clone_free_cb);
8917         if (!tunnel->groups) {
8918                 mlx5_ipool_free(ipool, id);
8919                 return NULL;
8920         }
8921         /* initiate new PMD tunnel */
8922         memcpy(&tunnel->app_tunnel, app_tunnel, sizeof(*app_tunnel));
8923         tunnel->tunnel_id = id;
8924         tunnel->action.type = (typeof(tunnel->action.type))
8925                               MLX5_RTE_FLOW_ACTION_TYPE_TUNNEL_SET;
8926         tunnel->action.conf = tunnel;
8927         tunnel->item.type = (typeof(tunnel->item.type))
8928                             MLX5_RTE_FLOW_ITEM_TYPE_TUNNEL;
8929         tunnel->item.spec = tunnel;
8930         tunnel->item.last = NULL;
8931         tunnel->item.mask = NULL;
8932
8933         DRV_LOG(DEBUG, "port %u new pmd tunnel id=0x%x",
8934                 dev->data->port_id, tunnel->tunnel_id);
8935
8936         return tunnel;
8937 }
8938
8939 struct tunnel_db_get_tunnel_ctx {
8940         const struct rte_flow_tunnel *app_tunnel;
8941         struct mlx5_flow_tunnel *tunnel;
8942 };
8943
8944 static bool get_tunnel_match(struct rte_eth_dev *dev,
8945                              struct mlx5_flow_tunnel *tunnel, const void *x)
8946 {
8947         const struct tunnel_db_get_tunnel_ctx *ctx = x;
8948
8949         RTE_SET_USED(dev);
8950         return !memcmp(ctx->app_tunnel, &tunnel->app_tunnel,
8951                        sizeof(*ctx->app_tunnel));
8952 }
8953
8954 static void get_tunnel_hit(struct rte_eth_dev *dev,
8955                            struct mlx5_flow_tunnel *tunnel, void *x)
8956 {
8957         /* called under tunnel spinlock protection */
8958         struct tunnel_db_get_tunnel_ctx *ctx = x;
8959
8960         RTE_SET_USED(dev);
8961         tunnel->refctn++;
8962         ctx->tunnel = tunnel;
8963 }
8964
8965 static void get_tunnel_miss(struct rte_eth_dev *dev, void *x)
8966 {
8967         /* called under tunnel spinlock protection */
8968         struct mlx5_flow_tunnel_hub *thub = mlx5_tunnel_hub(dev);
8969         struct tunnel_db_get_tunnel_ctx *ctx = x;
8970
8971         rte_spinlock_unlock(&thub->sl);
8972         ctx->tunnel = mlx5_flow_tunnel_allocate(dev, ctx->app_tunnel);
8973         rte_spinlock_lock(&thub->sl);
8974         if (ctx->tunnel) {
8975                 ctx->tunnel->refctn = 1;
8976                 LIST_INSERT_HEAD(&thub->tunnels, ctx->tunnel, chain);
8977         }
8978 }
8979
8980
8981 static int
8982 mlx5_get_flow_tunnel(struct rte_eth_dev *dev,
8983                      const struct rte_flow_tunnel *app_tunnel,
8984                      struct mlx5_flow_tunnel **tunnel)
8985 {
8986         struct tunnel_db_get_tunnel_ctx ctx = {
8987                 .app_tunnel = app_tunnel,
8988         };
8989
8990         mlx5_access_tunnel_offload_db(dev, get_tunnel_match, get_tunnel_hit,
8991                                       get_tunnel_miss, &ctx, true);
8992         *tunnel = ctx.tunnel;
8993         return ctx.tunnel ? 0 : -ENOMEM;
8994 }
8995
8996 void mlx5_release_tunnel_hub(struct mlx5_dev_ctx_shared *sh, uint16_t port_id)
8997 {
8998         struct mlx5_flow_tunnel_hub *thub = sh->tunnel_hub;
8999
9000         if (!thub)
9001                 return;
9002         if (!LIST_EMPTY(&thub->tunnels))
9003                 DRV_LOG(WARNING, "port %u tunnels present", port_id);
9004         mlx5_hlist_destroy(thub->groups);
9005         mlx5_free(thub);
9006 }
9007
9008 int mlx5_alloc_tunnel_hub(struct mlx5_dev_ctx_shared *sh)
9009 {
9010         int err;
9011         struct mlx5_flow_tunnel_hub *thub;
9012
9013         thub = mlx5_malloc(MLX5_MEM_SYS | MLX5_MEM_ZERO, sizeof(*thub),
9014                            0, SOCKET_ID_ANY);
9015         if (!thub)
9016                 return -ENOMEM;
9017         LIST_INIT(&thub->tunnels);
9018         rte_spinlock_init(&thub->sl);
9019         thub->groups = mlx5_hlist_create("flow groups", 64,
9020                                          false, true, sh,
9021                                          mlx5_flow_tunnel_grp2tbl_create_cb,
9022                                          mlx5_flow_tunnel_grp2tbl_match_cb,
9023                                          mlx5_flow_tunnel_grp2tbl_remove_cb,
9024                                          mlx5_flow_tunnel_grp2tbl_clone_cb,
9025                                         mlx5_flow_tunnel_grp2tbl_clone_free_cb);
9026         if (!thub->groups) {
9027                 err = -rte_errno;
9028                 goto err;
9029         }
9030         sh->tunnel_hub = thub;
9031
9032         return 0;
9033
9034 err:
9035         if (thub->groups)
9036                 mlx5_hlist_destroy(thub->groups);
9037         if (thub)
9038                 mlx5_free(thub);
9039         return err;
9040 }
9041
9042 static inline bool
9043 mlx5_flow_tunnel_validate(struct rte_eth_dev *dev,
9044                           struct rte_flow_tunnel *tunnel,
9045                           const char *err_msg)
9046 {
9047         err_msg = NULL;
9048         if (!is_tunnel_offload_active(dev)) {
9049                 err_msg = "tunnel offload was not activated";
9050                 goto out;
9051         } else if (!tunnel) {
9052                 err_msg = "no application tunnel";
9053                 goto out;
9054         }
9055
9056         switch (tunnel->type) {
9057         default:
9058                 err_msg = "unsupported tunnel type";
9059                 goto out;
9060         case RTE_FLOW_ITEM_TYPE_VXLAN:
9061                 break;
9062         }
9063
9064 out:
9065         return !err_msg;
9066 }
9067
9068 static int
9069 mlx5_flow_tunnel_decap_set(struct rte_eth_dev *dev,
9070                     struct rte_flow_tunnel *app_tunnel,
9071                     struct rte_flow_action **actions,
9072                     uint32_t *num_of_actions,
9073                     struct rte_flow_error *error)
9074 {
9075         int ret;
9076         struct mlx5_flow_tunnel *tunnel;
9077         const char *err_msg = NULL;
9078         bool verdict = mlx5_flow_tunnel_validate(dev, app_tunnel, err_msg);
9079
9080         if (!verdict)
9081                 return rte_flow_error_set(error, EINVAL,
9082                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
9083                                           err_msg);
9084         ret = mlx5_get_flow_tunnel(dev, app_tunnel, &tunnel);
9085         if (ret < 0) {
9086                 return rte_flow_error_set(error, ret,
9087                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
9088                                           "failed to initialize pmd tunnel");
9089         }
9090         *actions = &tunnel->action;
9091         *num_of_actions = 1;
9092         return 0;
9093 }
9094
9095 static int
9096 mlx5_flow_tunnel_match(struct rte_eth_dev *dev,
9097                        struct rte_flow_tunnel *app_tunnel,
9098                        struct rte_flow_item **items,
9099                        uint32_t *num_of_items,
9100                        struct rte_flow_error *error)
9101 {
9102         int ret;
9103         struct mlx5_flow_tunnel *tunnel;
9104         const char *err_msg = NULL;
9105         bool verdict = mlx5_flow_tunnel_validate(dev, app_tunnel, err_msg);
9106
9107         if (!verdict)
9108                 return rte_flow_error_set(error, EINVAL,
9109                                           RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
9110                                           err_msg);
9111         ret = mlx5_get_flow_tunnel(dev, app_tunnel, &tunnel);
9112         if (ret < 0) {
9113                 return rte_flow_error_set(error, ret,
9114                                           RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
9115                                           "failed to initialize pmd tunnel");
9116         }
9117         *items = &tunnel->item;
9118         *num_of_items = 1;
9119         return 0;
9120 }
9121
9122 struct tunnel_db_element_release_ctx {
9123         struct rte_flow_item *items;
9124         struct rte_flow_action *actions;
9125         uint32_t num_elements;
9126         struct rte_flow_error *error;
9127         int ret;
9128 };
9129
9130 static bool
9131 tunnel_element_release_match(struct rte_eth_dev *dev,
9132                              struct mlx5_flow_tunnel *tunnel, const void *x)
9133 {
9134         const struct tunnel_db_element_release_ctx *ctx = x;
9135
9136         RTE_SET_USED(dev);
9137         if (ctx->num_elements != 1)
9138                 return false;
9139         else if (ctx->items)
9140                 return ctx->items == &tunnel->item;
9141         else if (ctx->actions)
9142                 return ctx->actions == &tunnel->action;
9143
9144         return false;
9145 }
9146
9147 static void
9148 tunnel_element_release_hit(struct rte_eth_dev *dev,
9149                            struct mlx5_flow_tunnel *tunnel, void *x)
9150 {
9151         struct tunnel_db_element_release_ctx *ctx = x;
9152         ctx->ret = 0;
9153         if (!__atomic_sub_fetch(&tunnel->refctn, 1, __ATOMIC_RELAXED))
9154                 mlx5_flow_tunnel_free(dev, tunnel);
9155 }
9156
9157 static void
9158 tunnel_element_release_miss(struct rte_eth_dev *dev, void *x)
9159 {
9160         struct tunnel_db_element_release_ctx *ctx = x;
9161         RTE_SET_USED(dev);
9162         ctx->ret = rte_flow_error_set(ctx->error, EINVAL,
9163                                       RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
9164                                       "invalid argument");
9165 }
9166
9167 static int
9168 mlx5_flow_tunnel_item_release(struct rte_eth_dev *dev,
9169                        struct rte_flow_item *pmd_items,
9170                        uint32_t num_items, struct rte_flow_error *err)
9171 {
9172         struct tunnel_db_element_release_ctx ctx = {
9173                 .items = pmd_items,
9174                 .actions = NULL,
9175                 .num_elements = num_items,
9176                 .error = err,
9177         };
9178
9179         mlx5_access_tunnel_offload_db(dev, tunnel_element_release_match,
9180                                       tunnel_element_release_hit,
9181                                       tunnel_element_release_miss, &ctx, false);
9182
9183         return ctx.ret;
9184 }
9185
9186 static int
9187 mlx5_flow_tunnel_action_release(struct rte_eth_dev *dev,
9188                          struct rte_flow_action *pmd_actions,
9189                          uint32_t num_actions, struct rte_flow_error *err)
9190 {
9191         struct tunnel_db_element_release_ctx ctx = {
9192                 .items = NULL,
9193                 .actions = pmd_actions,
9194                 .num_elements = num_actions,
9195                 .error = err,
9196         };
9197
9198         mlx5_access_tunnel_offload_db(dev, tunnel_element_release_match,
9199                                       tunnel_element_release_hit,
9200                                       tunnel_element_release_miss, &ctx, false);
9201
9202         return ctx.ret;
9203 }
9204
9205 static int
9206 mlx5_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
9207                                   struct rte_mbuf *m,
9208                                   struct rte_flow_restore_info *info,
9209                                   struct rte_flow_error *err)
9210 {
9211         uint64_t ol_flags = m->ol_flags;
9212         const struct mlx5_flow_tbl_data_entry *tble;
9213         const uint64_t mask = PKT_RX_FDIR | PKT_RX_FDIR_ID;
9214
9215         if (!is_tunnel_offload_active(dev)) {
9216                 info->flags = 0;
9217                 return 0;
9218         }
9219
9220         if ((ol_flags & mask) != mask)
9221                 goto err;
9222         tble = tunnel_mark_decode(dev, m->hash.fdir.hi);
9223         if (!tble) {
9224                 DRV_LOG(DEBUG, "port %u invalid miss tunnel mark %#x",
9225                         dev->data->port_id, m->hash.fdir.hi);
9226                 goto err;
9227         }
9228         MLX5_ASSERT(tble->tunnel);
9229         memcpy(&info->tunnel, &tble->tunnel->app_tunnel, sizeof(info->tunnel));
9230         info->group_id = tble->group_id;
9231         info->flags = RTE_FLOW_RESTORE_INFO_TUNNEL |
9232                       RTE_FLOW_RESTORE_INFO_GROUP_ID |
9233                       RTE_FLOW_RESTORE_INFO_ENCAPSULATED;
9234
9235         return 0;
9236
9237 err:
9238         return rte_flow_error_set(err, EINVAL,
9239                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9240                                   "failed to get restore info");
9241 }
9242
9243 #else /* HAVE_IBV_FLOW_DV_SUPPORT */
9244 static int
9245 mlx5_flow_tunnel_decap_set(__rte_unused struct rte_eth_dev *dev,
9246                            __rte_unused struct rte_flow_tunnel *app_tunnel,
9247                            __rte_unused struct rte_flow_action **actions,
9248                            __rte_unused uint32_t *num_of_actions,
9249                            __rte_unused struct rte_flow_error *error)
9250 {
9251         return -ENOTSUP;
9252 }
9253
9254 static int
9255 mlx5_flow_tunnel_match(__rte_unused struct rte_eth_dev *dev,
9256                        __rte_unused struct rte_flow_tunnel *app_tunnel,
9257                        __rte_unused struct rte_flow_item **items,
9258                        __rte_unused uint32_t *num_of_items,
9259                        __rte_unused struct rte_flow_error *error)
9260 {
9261         return -ENOTSUP;
9262 }
9263
9264 static int
9265 mlx5_flow_tunnel_item_release(__rte_unused struct rte_eth_dev *dev,
9266                               __rte_unused struct rte_flow_item *pmd_items,
9267                               __rte_unused uint32_t num_items,
9268                               __rte_unused struct rte_flow_error *err)
9269 {
9270         return -ENOTSUP;
9271 }
9272
9273 static int
9274 mlx5_flow_tunnel_action_release(__rte_unused struct rte_eth_dev *dev,
9275                                 __rte_unused struct rte_flow_action *pmd_action,
9276                                 __rte_unused uint32_t num_actions,
9277                                 __rte_unused struct rte_flow_error *err)
9278 {
9279         return -ENOTSUP;
9280 }
9281
9282 static int
9283 mlx5_flow_tunnel_get_restore_info(__rte_unused struct rte_eth_dev *dev,
9284                                   __rte_unused struct rte_mbuf *m,
9285                                   __rte_unused struct rte_flow_restore_info *i,
9286                                   __rte_unused struct rte_flow_error *err)
9287 {
9288         return -ENOTSUP;
9289 }
9290
9291 static int
9292 flow_tunnel_add_default_miss(__rte_unused struct rte_eth_dev *dev,
9293                              __rte_unused struct rte_flow *flow,
9294                              __rte_unused const struct rte_flow_attr *attr,
9295                              __rte_unused const struct rte_flow_action *actions,
9296                              __rte_unused uint32_t flow_idx,
9297                              __rte_unused const struct mlx5_flow_tunnel *tunnel,
9298                              __rte_unused struct tunnel_default_miss_ctx *ctx,
9299                              __rte_unused struct rte_flow_error *error)
9300 {
9301         return -ENOTSUP;
9302 }
9303
9304 static struct mlx5_flow_tunnel *
9305 mlx5_find_tunnel_id(__rte_unused struct rte_eth_dev *dev,
9306                     __rte_unused uint32_t id)
9307 {
9308         return NULL;
9309 }
9310
9311 static void
9312 mlx5_flow_tunnel_free(__rte_unused struct rte_eth_dev *dev,
9313                       __rte_unused struct mlx5_flow_tunnel *tunnel)
9314 {
9315 }
9316
9317 static uint32_t
9318 tunnel_flow_group_to_flow_table(__rte_unused struct rte_eth_dev *dev,
9319                                 __rte_unused const struct mlx5_flow_tunnel *t,
9320                                 __rte_unused uint32_t group,
9321                                 __rte_unused uint32_t *table,
9322                                 struct rte_flow_error *error)
9323 {
9324         return rte_flow_error_set(error, ENOTSUP,
9325                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
9326                                   "tunnel offload requires DV support");
9327 }
9328
9329 void
9330 mlx5_release_tunnel_hub(__rte_unused struct mlx5_dev_ctx_shared *sh,
9331                         __rte_unused  uint16_t port_id)
9332 {
9333 }
9334 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
9335
9336 static void
9337 mlx5_dbg__print_pattern(const struct rte_flow_item *item)
9338 {
9339         int ret;
9340         struct rte_flow_error error;
9341
9342         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
9343                 char *item_name;
9344                 ret = rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR, &item_name,
9345                                     sizeof(item_name),
9346                                     (void *)(uintptr_t)item->type, &error);
9347                 if (ret > 0)
9348                         printf("%s ", item_name);
9349                 else
9350                         printf("%d\n", (int)item->type);
9351         }
9352         printf("END\n");
9353 }