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