net/mlx5: fix suffix flow creation in metadata split
[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 <netinet/in.h>
7 #include <sys/queue.h>
8 #include <stdalign.h>
9 #include <stdint.h>
10 #include <string.h>
11
12 /* Verbs header. */
13 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
14 #ifdef PEDANTIC
15 #pragma GCC diagnostic ignored "-Wpedantic"
16 #endif
17 #include <infiniband/verbs.h>
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic error "-Wpedantic"
20 #endif
21
22 #include <rte_common.h>
23 #include <rte_ether.h>
24 #include <rte_ethdev_driver.h>
25 #include <rte_flow.h>
26 #include <rte_flow_driver.h>
27 #include <rte_malloc.h>
28 #include <rte_ip.h>
29
30 #include "mlx5.h"
31 #include "mlx5_defs.h"
32 #include "mlx5_flow.h"
33 #include "mlx5_glue.h"
34 #include "mlx5_prm.h"
35 #include "mlx5_rxtx.h"
36
37 /* Dev ops structure defined in mlx5.c */
38 extern const struct eth_dev_ops mlx5_dev_ops;
39 extern const struct eth_dev_ops mlx5_dev_ops_isolate;
40
41 /** Device flow drivers. */
42 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
43 extern const struct mlx5_flow_driver_ops mlx5_flow_dv_drv_ops;
44 #endif
45 extern const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops;
46
47 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops;
48
49 const struct mlx5_flow_driver_ops *flow_drv_ops[] = {
50         [MLX5_FLOW_TYPE_MIN] = &mlx5_flow_null_drv_ops,
51 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
52         [MLX5_FLOW_TYPE_DV] = &mlx5_flow_dv_drv_ops,
53 #endif
54         [MLX5_FLOW_TYPE_VERBS] = &mlx5_flow_verbs_drv_ops,
55         [MLX5_FLOW_TYPE_MAX] = &mlx5_flow_null_drv_ops
56 };
57
58 enum mlx5_expansion {
59         MLX5_EXPANSION_ROOT,
60         MLX5_EXPANSION_ROOT_OUTER,
61         MLX5_EXPANSION_ROOT_ETH_VLAN,
62         MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN,
63         MLX5_EXPANSION_OUTER_ETH,
64         MLX5_EXPANSION_OUTER_ETH_VLAN,
65         MLX5_EXPANSION_OUTER_VLAN,
66         MLX5_EXPANSION_OUTER_IPV4,
67         MLX5_EXPANSION_OUTER_IPV4_UDP,
68         MLX5_EXPANSION_OUTER_IPV4_TCP,
69         MLX5_EXPANSION_OUTER_IPV6,
70         MLX5_EXPANSION_OUTER_IPV6_UDP,
71         MLX5_EXPANSION_OUTER_IPV6_TCP,
72         MLX5_EXPANSION_VXLAN,
73         MLX5_EXPANSION_VXLAN_GPE,
74         MLX5_EXPANSION_GRE,
75         MLX5_EXPANSION_MPLS,
76         MLX5_EXPANSION_ETH,
77         MLX5_EXPANSION_ETH_VLAN,
78         MLX5_EXPANSION_VLAN,
79         MLX5_EXPANSION_IPV4,
80         MLX5_EXPANSION_IPV4_UDP,
81         MLX5_EXPANSION_IPV4_TCP,
82         MLX5_EXPANSION_IPV6,
83         MLX5_EXPANSION_IPV6_UDP,
84         MLX5_EXPANSION_IPV6_TCP,
85 };
86
87 /** Supported expansion of items. */
88 static const struct rte_flow_expand_node mlx5_support_expansion[] = {
89         [MLX5_EXPANSION_ROOT] = {
90                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
91                                                  MLX5_EXPANSION_IPV4,
92                                                  MLX5_EXPANSION_IPV6),
93                 .type = RTE_FLOW_ITEM_TYPE_END,
94         },
95         [MLX5_EXPANSION_ROOT_OUTER] = {
96                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH,
97                                                  MLX5_EXPANSION_OUTER_IPV4,
98                                                  MLX5_EXPANSION_OUTER_IPV6),
99                 .type = RTE_FLOW_ITEM_TYPE_END,
100         },
101         [MLX5_EXPANSION_ROOT_ETH_VLAN] = {
102                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH_VLAN),
103                 .type = RTE_FLOW_ITEM_TYPE_END,
104         },
105         [MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN] = {
106                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH_VLAN),
107                 .type = RTE_FLOW_ITEM_TYPE_END,
108         },
109         [MLX5_EXPANSION_OUTER_ETH] = {
110                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
111                                                  MLX5_EXPANSION_OUTER_IPV6,
112                                                  MLX5_EXPANSION_MPLS),
113                 .type = RTE_FLOW_ITEM_TYPE_ETH,
114                 .rss_types = 0,
115         },
116         [MLX5_EXPANSION_OUTER_ETH_VLAN] = {
117                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_VLAN),
118                 .type = RTE_FLOW_ITEM_TYPE_ETH,
119                 .rss_types = 0,
120         },
121         [MLX5_EXPANSION_OUTER_VLAN] = {
122                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
123                                                  MLX5_EXPANSION_OUTER_IPV6),
124                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
125         },
126         [MLX5_EXPANSION_OUTER_IPV4] = {
127                 .next = RTE_FLOW_EXPAND_RSS_NEXT
128                         (MLX5_EXPANSION_OUTER_IPV4_UDP,
129                          MLX5_EXPANSION_OUTER_IPV4_TCP,
130                          MLX5_EXPANSION_GRE,
131                          MLX5_EXPANSION_IPV4,
132                          MLX5_EXPANSION_IPV6),
133                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
134                 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
135                         ETH_RSS_NONFRAG_IPV4_OTHER,
136         },
137         [MLX5_EXPANSION_OUTER_IPV4_UDP] = {
138                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
139                                                  MLX5_EXPANSION_VXLAN_GPE),
140                 .type = RTE_FLOW_ITEM_TYPE_UDP,
141                 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
142         },
143         [MLX5_EXPANSION_OUTER_IPV4_TCP] = {
144                 .type = RTE_FLOW_ITEM_TYPE_TCP,
145                 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
146         },
147         [MLX5_EXPANSION_OUTER_IPV6] = {
148                 .next = RTE_FLOW_EXPAND_RSS_NEXT
149                         (MLX5_EXPANSION_OUTER_IPV6_UDP,
150                          MLX5_EXPANSION_OUTER_IPV6_TCP,
151                          MLX5_EXPANSION_IPV4,
152                          MLX5_EXPANSION_IPV6),
153                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
154                 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
155                         ETH_RSS_NONFRAG_IPV6_OTHER,
156         },
157         [MLX5_EXPANSION_OUTER_IPV6_UDP] = {
158                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
159                                                  MLX5_EXPANSION_VXLAN_GPE),
160                 .type = RTE_FLOW_ITEM_TYPE_UDP,
161                 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
162         },
163         [MLX5_EXPANSION_OUTER_IPV6_TCP] = {
164                 .type = RTE_FLOW_ITEM_TYPE_TCP,
165                 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
166         },
167         [MLX5_EXPANSION_VXLAN] = {
168                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH),
169                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
170         },
171         [MLX5_EXPANSION_VXLAN_GPE] = {
172                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
173                                                  MLX5_EXPANSION_IPV4,
174                                                  MLX5_EXPANSION_IPV6),
175                 .type = RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
176         },
177         [MLX5_EXPANSION_GRE] = {
178                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4),
179                 .type = RTE_FLOW_ITEM_TYPE_GRE,
180         },
181         [MLX5_EXPANSION_MPLS] = {
182                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
183                                                  MLX5_EXPANSION_IPV6),
184                 .type = RTE_FLOW_ITEM_TYPE_MPLS,
185         },
186         [MLX5_EXPANSION_ETH] = {
187                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
188                                                  MLX5_EXPANSION_IPV6),
189                 .type = RTE_FLOW_ITEM_TYPE_ETH,
190         },
191         [MLX5_EXPANSION_ETH_VLAN] = {
192                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VLAN),
193                 .type = RTE_FLOW_ITEM_TYPE_ETH,
194         },
195         [MLX5_EXPANSION_VLAN] = {
196                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
197                                                  MLX5_EXPANSION_IPV6),
198                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
199         },
200         [MLX5_EXPANSION_IPV4] = {
201                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4_UDP,
202                                                  MLX5_EXPANSION_IPV4_TCP),
203                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
204                 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
205                         ETH_RSS_NONFRAG_IPV4_OTHER,
206         },
207         [MLX5_EXPANSION_IPV4_UDP] = {
208                 .type = RTE_FLOW_ITEM_TYPE_UDP,
209                 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
210         },
211         [MLX5_EXPANSION_IPV4_TCP] = {
212                 .type = RTE_FLOW_ITEM_TYPE_TCP,
213                 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
214         },
215         [MLX5_EXPANSION_IPV6] = {
216                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV6_UDP,
217                                                  MLX5_EXPANSION_IPV6_TCP),
218                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
219                 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
220                         ETH_RSS_NONFRAG_IPV6_OTHER,
221         },
222         [MLX5_EXPANSION_IPV6_UDP] = {
223                 .type = RTE_FLOW_ITEM_TYPE_UDP,
224                 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
225         },
226         [MLX5_EXPANSION_IPV6_TCP] = {
227                 .type = RTE_FLOW_ITEM_TYPE_TCP,
228                 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
229         },
230 };
231
232 static const struct rte_flow_ops mlx5_flow_ops = {
233         .validate = mlx5_flow_validate,
234         .create = mlx5_flow_create,
235         .destroy = mlx5_flow_destroy,
236         .flush = mlx5_flow_flush,
237         .isolate = mlx5_flow_isolate,
238         .query = mlx5_flow_query,
239 };
240
241 /* Convert FDIR request to Generic flow. */
242 struct mlx5_fdir {
243         struct rte_flow_attr attr;
244         struct rte_flow_item items[4];
245         struct rte_flow_item_eth l2;
246         struct rte_flow_item_eth l2_mask;
247         union {
248                 struct rte_flow_item_ipv4 ipv4;
249                 struct rte_flow_item_ipv6 ipv6;
250         } l3;
251         union {
252                 struct rte_flow_item_ipv4 ipv4;
253                 struct rte_flow_item_ipv6 ipv6;
254         } l3_mask;
255         union {
256                 struct rte_flow_item_udp udp;
257                 struct rte_flow_item_tcp tcp;
258         } l4;
259         union {
260                 struct rte_flow_item_udp udp;
261                 struct rte_flow_item_tcp tcp;
262         } l4_mask;
263         struct rte_flow_action actions[2];
264         struct rte_flow_action_queue queue;
265 };
266
267 /* Map of Verbs to Flow priority with 8 Verbs priorities. */
268 static const uint32_t priority_map_3[][MLX5_PRIORITY_MAP_MAX] = {
269         { 0, 1, 2 }, { 2, 3, 4 }, { 5, 6, 7 },
270 };
271
272 /* Map of Verbs to Flow priority with 16 Verbs priorities. */
273 static const uint32_t priority_map_5[][MLX5_PRIORITY_MAP_MAX] = {
274         { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
275         { 9, 10, 11 }, { 12, 13, 14 },
276 };
277
278 /* Tunnel information. */
279 struct mlx5_flow_tunnel_info {
280         uint64_t tunnel; /**< Tunnel bit (see MLX5_FLOW_*). */
281         uint32_t ptype; /**< Tunnel Ptype (see RTE_PTYPE_*). */
282 };
283
284 static struct mlx5_flow_tunnel_info tunnels_info[] = {
285         {
286                 .tunnel = MLX5_FLOW_LAYER_VXLAN,
287                 .ptype = RTE_PTYPE_TUNNEL_VXLAN | RTE_PTYPE_L4_UDP,
288         },
289         {
290                 .tunnel = MLX5_FLOW_LAYER_GENEVE,
291                 .ptype = RTE_PTYPE_TUNNEL_GENEVE | RTE_PTYPE_L4_UDP,
292         },
293         {
294                 .tunnel = MLX5_FLOW_LAYER_VXLAN_GPE,
295                 .ptype = RTE_PTYPE_TUNNEL_VXLAN_GPE | RTE_PTYPE_L4_UDP,
296         },
297         {
298                 .tunnel = MLX5_FLOW_LAYER_GRE,
299                 .ptype = RTE_PTYPE_TUNNEL_GRE,
300         },
301         {
302                 .tunnel = MLX5_FLOW_LAYER_MPLS | MLX5_FLOW_LAYER_OUTER_L4_UDP,
303                 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_UDP | RTE_PTYPE_L4_UDP,
304         },
305         {
306                 .tunnel = MLX5_FLOW_LAYER_MPLS,
307                 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
308         },
309         {
310                 .tunnel = MLX5_FLOW_LAYER_NVGRE,
311                 .ptype = RTE_PTYPE_TUNNEL_NVGRE,
312         },
313         {
314                 .tunnel = MLX5_FLOW_LAYER_IPIP,
315                 .ptype = RTE_PTYPE_TUNNEL_IP,
316         },
317         {
318                 .tunnel = MLX5_FLOW_LAYER_IPV6_ENCAP,
319                 .ptype = RTE_PTYPE_TUNNEL_IP,
320         },
321 };
322
323 /**
324  * Translate tag ID to register.
325  *
326  * @param[in] dev
327  *   Pointer to the Ethernet device structure.
328  * @param[in] feature
329  *   The feature that request the register.
330  * @param[in] id
331  *   The request register ID.
332  * @param[out] error
333  *   Error description in case of any.
334  *
335  * @return
336  *   The request register on success, a negative errno
337  *   value otherwise and rte_errno is set.
338  */
339 enum modify_reg
340 mlx5_flow_get_reg_id(struct rte_eth_dev *dev,
341                      enum mlx5_feature_name feature,
342                      uint32_t id,
343                      struct rte_flow_error *error)
344 {
345         struct mlx5_priv *priv = dev->data->dev_private;
346         struct mlx5_dev_config *config = &priv->config;
347         enum modify_reg start_reg;
348
349         switch (feature) {
350         case MLX5_HAIRPIN_RX:
351                 return REG_B;
352         case MLX5_HAIRPIN_TX:
353                 return REG_A;
354         case MLX5_METADATA_RX:
355                 switch (config->dv_xmeta_en) {
356                 case MLX5_XMETA_MODE_LEGACY:
357                         return REG_B;
358                 case MLX5_XMETA_MODE_META16:
359                         return REG_C_0;
360                 case MLX5_XMETA_MODE_META32:
361                         return REG_C_1;
362                 }
363                 break;
364         case MLX5_METADATA_TX:
365                 return REG_A;
366         case MLX5_METADATA_FDB:
367                 return REG_C_0;
368         case MLX5_FLOW_MARK:
369                 switch (config->dv_xmeta_en) {
370                 case MLX5_XMETA_MODE_LEGACY:
371                         return REG_NONE;
372                 case MLX5_XMETA_MODE_META16:
373                         return REG_C_1;
374                 case MLX5_XMETA_MODE_META32:
375                         return REG_C_0;
376                 }
377                 break;
378         case MLX5_COPY_MARK:
379         case MLX5_MTR_SFX:
380                 /*
381                  * Metadata COPY_MARK register using is in meter suffix sub
382                  * flow while with meter. It's safe to share the same register.
383                  */
384                 return priv->mtr_color_reg != REG_C_2 ? REG_C_2 : REG_C_3;
385         case MLX5_MTR_COLOR:
386                 RTE_ASSERT(priv->mtr_color_reg != REG_NONE);
387                 return priv->mtr_color_reg;
388         case MLX5_APP_TAG:
389                 /*
390                  * If meter is enable, it will engage two registers for color
391                  * match and flow match. If meter color match is not using the
392                  * REG_C_2, need to skip the REG_C_x be used by meter color
393                  * match.
394                  * If meter is disable, free to use all available registers.
395                  */
396                 if (priv->mtr_color_reg != REG_NONE)
397                         start_reg = priv->mtr_color_reg != REG_C_2 ? REG_C_3 :
398                                     REG_C_4;
399                 else
400                         start_reg = REG_C_2;
401                 if (id > (REG_C_7 - start_reg))
402                         return rte_flow_error_set(error, EINVAL,
403                                                   RTE_FLOW_ERROR_TYPE_ITEM,
404                                                   NULL, "invalid tag id");
405                 if (config->flow_mreg_c[id + start_reg - REG_C_0] == REG_NONE)
406                         return rte_flow_error_set(error, ENOTSUP,
407                                                   RTE_FLOW_ERROR_TYPE_ITEM,
408                                                   NULL, "unsupported tag id");
409                 /*
410                  * This case means meter is using the REG_C_x great than 2.
411                  * Take care not to conflict with meter color REG_C_x.
412                  * If the available index REG_C_y >= REG_C_x, skip the
413                  * color register.
414                  */
415                 if (start_reg == REG_C_3 && config->flow_mreg_c
416                     [id + REG_C_3 - REG_C_0] >= priv->mtr_color_reg) {
417                         if (config->flow_mreg_c[id + 1 + REG_C_3 - REG_C_0] !=
418                             REG_NONE)
419                                 return config->flow_mreg_c
420                                                 [id + 1 + REG_C_3 - REG_C_0];
421                         return rte_flow_error_set(error, ENOTSUP,
422                                                   RTE_FLOW_ERROR_TYPE_ITEM,
423                                                   NULL, "unsupported tag id");
424                 }
425                 return config->flow_mreg_c[id + start_reg - REG_C_0];
426         }
427         assert(false);
428         return rte_flow_error_set(error, EINVAL,
429                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
430                                   NULL, "invalid feature name");
431 }
432
433 /**
434  * Check extensive flow metadata register support.
435  *
436  * @param dev
437  *   Pointer to rte_eth_dev structure.
438  *
439  * @return
440  *   True if device supports extensive flow metadata register, otherwise false.
441  */
442 bool
443 mlx5_flow_ext_mreg_supported(struct rte_eth_dev *dev)
444 {
445         struct mlx5_priv *priv = dev->data->dev_private;
446         struct mlx5_dev_config *config = &priv->config;
447
448         /*
449          * Having available reg_c can be regarded inclusively as supporting
450          * extensive flow metadata register, which could mean,
451          * - metadata register copy action by modify header.
452          * - 16 modify header actions is supported.
453          * - reg_c's are preserved across different domain (FDB and NIC) on
454          *   packet loopback by flow lookup miss.
455          */
456         return config->flow_mreg_c[2] != REG_NONE;
457 }
458
459 /**
460  * Discover the maximum number of priority available.
461  *
462  * @param[in] dev
463  *   Pointer to the Ethernet device structure.
464  *
465  * @return
466  *   number of supported flow priority on success, a negative errno
467  *   value otherwise and rte_errno is set.
468  */
469 int
470 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
471 {
472         struct mlx5_priv *priv = dev->data->dev_private;
473         struct {
474                 struct ibv_flow_attr attr;
475                 struct ibv_flow_spec_eth eth;
476                 struct ibv_flow_spec_action_drop drop;
477         } flow_attr = {
478                 .attr = {
479                         .num_of_specs = 2,
480                         .port = (uint8_t)priv->ibv_port,
481                 },
482                 .eth = {
483                         .type = IBV_FLOW_SPEC_ETH,
484                         .size = sizeof(struct ibv_flow_spec_eth),
485                 },
486                 .drop = {
487                         .size = sizeof(struct ibv_flow_spec_action_drop),
488                         .type = IBV_FLOW_SPEC_ACTION_DROP,
489                 },
490         };
491         struct ibv_flow *flow;
492         struct mlx5_hrxq *drop = mlx5_hrxq_drop_new(dev);
493         uint16_t vprio[] = { 8, 16 };
494         int i;
495         int priority = 0;
496
497         if (!drop) {
498                 rte_errno = ENOTSUP;
499                 return -rte_errno;
500         }
501         for (i = 0; i != RTE_DIM(vprio); i++) {
502                 flow_attr.attr.priority = vprio[i] - 1;
503                 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
504                 if (!flow)
505                         break;
506                 claim_zero(mlx5_glue->destroy_flow(flow));
507                 priority = vprio[i];
508         }
509         mlx5_hrxq_drop_release(dev);
510         switch (priority) {
511         case 8:
512                 priority = RTE_DIM(priority_map_3);
513                 break;
514         case 16:
515                 priority = RTE_DIM(priority_map_5);
516                 break;
517         default:
518                 rte_errno = ENOTSUP;
519                 DRV_LOG(ERR,
520                         "port %u verbs maximum priority: %d expected 8/16",
521                         dev->data->port_id, priority);
522                 return -rte_errno;
523         }
524         DRV_LOG(INFO, "port %u flow maximum priority: %d",
525                 dev->data->port_id, priority);
526         return priority;
527 }
528
529 /**
530  * Adjust flow priority based on the highest layer and the request priority.
531  *
532  * @param[in] dev
533  *   Pointer to the Ethernet device structure.
534  * @param[in] priority
535  *   The rule base priority.
536  * @param[in] subpriority
537  *   The priority based on the items.
538  *
539  * @return
540  *   The new priority.
541  */
542 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
543                                    uint32_t subpriority)
544 {
545         uint32_t res = 0;
546         struct mlx5_priv *priv = dev->data->dev_private;
547
548         switch (priv->config.flow_prio) {
549         case RTE_DIM(priority_map_3):
550                 res = priority_map_3[priority][subpriority];
551                 break;
552         case RTE_DIM(priority_map_5):
553                 res = priority_map_5[priority][subpriority];
554                 break;
555         }
556         return  res;
557 }
558
559 /**
560  * Verify the @p item specifications (spec, last, mask) are compatible with the
561  * NIC capabilities.
562  *
563  * @param[in] item
564  *   Item specification.
565  * @param[in] mask
566  *   @p item->mask or flow default bit-masks.
567  * @param[in] nic_mask
568  *   Bit-masks covering supported fields by the NIC to compare with user mask.
569  * @param[in] size
570  *   Bit-masks size in bytes.
571  * @param[out] error
572  *   Pointer to error structure.
573  *
574  * @return
575  *   0 on success, a negative errno value otherwise and rte_errno is set.
576  */
577 int
578 mlx5_flow_item_acceptable(const struct rte_flow_item *item,
579                           const uint8_t *mask,
580                           const uint8_t *nic_mask,
581                           unsigned int size,
582                           struct rte_flow_error *error)
583 {
584         unsigned int i;
585
586         assert(nic_mask);
587         for (i = 0; i < size; ++i)
588                 if ((nic_mask[i] | mask[i]) != nic_mask[i])
589                         return rte_flow_error_set(error, ENOTSUP,
590                                                   RTE_FLOW_ERROR_TYPE_ITEM,
591                                                   item,
592                                                   "mask enables non supported"
593                                                   " bits");
594         if (!item->spec && (item->mask || item->last))
595                 return rte_flow_error_set(error, EINVAL,
596                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
597                                           "mask/last without a spec is not"
598                                           " supported");
599         if (item->spec && item->last) {
600                 uint8_t spec[size];
601                 uint8_t last[size];
602                 unsigned int i;
603                 int ret;
604
605                 for (i = 0; i < size; ++i) {
606                         spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
607                         last[i] = ((const uint8_t *)item->last)[i] & mask[i];
608                 }
609                 ret = memcmp(spec, last, size);
610                 if (ret != 0)
611                         return rte_flow_error_set(error, EINVAL,
612                                                   RTE_FLOW_ERROR_TYPE_ITEM,
613                                                   item,
614                                                   "range is not valid");
615         }
616         return 0;
617 }
618
619 /**
620  * Adjust the hash fields according to the @p flow information.
621  *
622  * @param[in] dev_flow.
623  *   Pointer to the mlx5_flow.
624  * @param[in] tunnel
625  *   1 when the hash field is for a tunnel item.
626  * @param[in] layer_types
627  *   ETH_RSS_* types.
628  * @param[in] hash_fields
629  *   Item hash fields.
630  *
631  * @return
632  *   The hash fields that should be used.
633  */
634 uint64_t
635 mlx5_flow_hashfields_adjust(struct mlx5_flow *dev_flow,
636                             int tunnel __rte_unused, uint64_t layer_types,
637                             uint64_t hash_fields)
638 {
639         struct rte_flow *flow = dev_flow->flow;
640 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
641         int rss_request_inner = flow->rss.level >= 2;
642
643         /* Check RSS hash level for tunnel. */
644         if (tunnel && rss_request_inner)
645                 hash_fields |= IBV_RX_HASH_INNER;
646         else if (tunnel || rss_request_inner)
647                 return 0;
648 #endif
649         /* Check if requested layer matches RSS hash fields. */
650         if (!(flow->rss.types & layer_types))
651                 return 0;
652         return hash_fields;
653 }
654
655 /**
656  * Lookup and set the ptype in the data Rx part.  A single Ptype can be used,
657  * if several tunnel rules are used on this queue, the tunnel ptype will be
658  * cleared.
659  *
660  * @param rxq_ctrl
661  *   Rx queue to update.
662  */
663 static void
664 flow_rxq_tunnel_ptype_update(struct mlx5_rxq_ctrl *rxq_ctrl)
665 {
666         unsigned int i;
667         uint32_t tunnel_ptype = 0;
668
669         /* Look up for the ptype to use. */
670         for (i = 0; i != MLX5_FLOW_TUNNEL; ++i) {
671                 if (!rxq_ctrl->flow_tunnels_n[i])
672                         continue;
673                 if (!tunnel_ptype) {
674                         tunnel_ptype = tunnels_info[i].ptype;
675                 } else {
676                         tunnel_ptype = 0;
677                         break;
678                 }
679         }
680         rxq_ctrl->rxq.tunnel = tunnel_ptype;
681 }
682
683 /**
684  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) according to the devive
685  * flow.
686  *
687  * @param[in] dev
688  *   Pointer to the Ethernet device structure.
689  * @param[in] dev_flow
690  *   Pointer to device flow structure.
691  */
692 static void
693 flow_drv_rxq_flags_set(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow)
694 {
695         struct mlx5_priv *priv = dev->data->dev_private;
696         struct rte_flow *flow = dev_flow->flow;
697         const int mark = !!(dev_flow->actions &
698                             (MLX5_FLOW_ACTION_FLAG | MLX5_FLOW_ACTION_MARK));
699         const int tunnel = !!(dev_flow->layers & MLX5_FLOW_LAYER_TUNNEL);
700         unsigned int i;
701
702         for (i = 0; i != flow->rss.queue_num; ++i) {
703                 int idx = (*flow->rss.queue)[i];
704                 struct mlx5_rxq_ctrl *rxq_ctrl =
705                         container_of((*priv->rxqs)[idx],
706                                      struct mlx5_rxq_ctrl, rxq);
707
708                 /*
709                  * To support metadata register copy on Tx loopback,
710                  * this must be always enabled (metadata may arive
711                  * from other port - not from local flows only.
712                  */
713                 if (priv->config.dv_flow_en &&
714                     priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
715                     mlx5_flow_ext_mreg_supported(dev)) {
716                         rxq_ctrl->rxq.mark = 1;
717                         rxq_ctrl->flow_mark_n = 1;
718                 } else if (mark) {
719                         rxq_ctrl->rxq.mark = 1;
720                         rxq_ctrl->flow_mark_n++;
721                 }
722                 if (tunnel) {
723                         unsigned int j;
724
725                         /* Increase the counter matching the flow. */
726                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
727                                 if ((tunnels_info[j].tunnel &
728                                      dev_flow->layers) ==
729                                     tunnels_info[j].tunnel) {
730                                         rxq_ctrl->flow_tunnels_n[j]++;
731                                         break;
732                                 }
733                         }
734                         flow_rxq_tunnel_ptype_update(rxq_ctrl);
735                 }
736         }
737 }
738
739 /**
740  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) for a flow
741  *
742  * @param[in] dev
743  *   Pointer to the Ethernet device structure.
744  * @param[in] flow
745  *   Pointer to flow structure.
746  */
747 static void
748 flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
749 {
750         struct mlx5_flow *dev_flow;
751
752         LIST_FOREACH(dev_flow, &flow->dev_flows, next)
753                 flow_drv_rxq_flags_set(dev, dev_flow);
754 }
755
756 /**
757  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
758  * device flow if no other flow uses it with the same kind of request.
759  *
760  * @param dev
761  *   Pointer to Ethernet device.
762  * @param[in] dev_flow
763  *   Pointer to the device flow.
764  */
765 static void
766 flow_drv_rxq_flags_trim(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow)
767 {
768         struct mlx5_priv *priv = dev->data->dev_private;
769         struct rte_flow *flow = dev_flow->flow;
770         const int mark = !!(dev_flow->actions &
771                             (MLX5_FLOW_ACTION_FLAG | MLX5_FLOW_ACTION_MARK));
772         const int tunnel = !!(dev_flow->layers & MLX5_FLOW_LAYER_TUNNEL);
773         unsigned int i;
774
775         assert(dev->data->dev_started);
776         for (i = 0; i != flow->rss.queue_num; ++i) {
777                 int idx = (*flow->rss.queue)[i];
778                 struct mlx5_rxq_ctrl *rxq_ctrl =
779                         container_of((*priv->rxqs)[idx],
780                                      struct mlx5_rxq_ctrl, rxq);
781
782                 if (priv->config.dv_flow_en &&
783                     priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
784                     mlx5_flow_ext_mreg_supported(dev)) {
785                         rxq_ctrl->rxq.mark = 1;
786                         rxq_ctrl->flow_mark_n = 1;
787                 } else if (mark) {
788                         rxq_ctrl->flow_mark_n--;
789                         rxq_ctrl->rxq.mark = !!rxq_ctrl->flow_mark_n;
790                 }
791                 if (tunnel) {
792                         unsigned int j;
793
794                         /* Decrease the counter matching the flow. */
795                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
796                                 if ((tunnels_info[j].tunnel &
797                                      dev_flow->layers) ==
798                                     tunnels_info[j].tunnel) {
799                                         rxq_ctrl->flow_tunnels_n[j]--;
800                                         break;
801                                 }
802                         }
803                         flow_rxq_tunnel_ptype_update(rxq_ctrl);
804                 }
805         }
806 }
807
808 /**
809  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
810  * @p flow if no other flow uses it with the same kind of request.
811  *
812  * @param dev
813  *   Pointer to Ethernet device.
814  * @param[in] flow
815  *   Pointer to the flow.
816  */
817 static void
818 flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
819 {
820         struct mlx5_flow *dev_flow;
821
822         LIST_FOREACH(dev_flow, &flow->dev_flows, next)
823                 flow_drv_rxq_flags_trim(dev, dev_flow);
824 }
825
826 /**
827  * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
828  *
829  * @param dev
830  *   Pointer to Ethernet device.
831  */
832 static void
833 flow_rxq_flags_clear(struct rte_eth_dev *dev)
834 {
835         struct mlx5_priv *priv = dev->data->dev_private;
836         unsigned int i;
837
838         for (i = 0; i != priv->rxqs_n; ++i) {
839                 struct mlx5_rxq_ctrl *rxq_ctrl;
840                 unsigned int j;
841
842                 if (!(*priv->rxqs)[i])
843                         continue;
844                 rxq_ctrl = container_of((*priv->rxqs)[i],
845                                         struct mlx5_rxq_ctrl, rxq);
846                 rxq_ctrl->flow_mark_n = 0;
847                 rxq_ctrl->rxq.mark = 0;
848                 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
849                         rxq_ctrl->flow_tunnels_n[j] = 0;
850                 rxq_ctrl->rxq.tunnel = 0;
851         }
852 }
853
854 /*
855  * return a pointer to the desired action in the list of actions.
856  *
857  * @param[in] actions
858  *   The list of actions to search the action in.
859  * @param[in] action
860  *   The action to find.
861  *
862  * @return
863  *   Pointer to the action in the list, if found. NULL otherwise.
864  */
865 const struct rte_flow_action *
866 mlx5_flow_find_action(const struct rte_flow_action *actions,
867                       enum rte_flow_action_type action)
868 {
869         if (actions == NULL)
870                 return NULL;
871         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++)
872                 if (actions->type == action)
873                         return actions;
874         return NULL;
875 }
876
877 /*
878  * Validate the flag action.
879  *
880  * @param[in] action_flags
881  *   Bit-fields that holds the actions detected until now.
882  * @param[in] attr
883  *   Attributes of flow that includes this action.
884  * @param[out] error
885  *   Pointer to error structure.
886  *
887  * @return
888  *   0 on success, a negative errno value otherwise and rte_errno is set.
889  */
890 int
891 mlx5_flow_validate_action_flag(uint64_t action_flags,
892                                const struct rte_flow_attr *attr,
893                                struct rte_flow_error *error)
894 {
895
896         if (action_flags & MLX5_FLOW_ACTION_DROP)
897                 return rte_flow_error_set(error, EINVAL,
898                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
899                                           "can't drop and flag in same flow");
900         if (action_flags & MLX5_FLOW_ACTION_MARK)
901                 return rte_flow_error_set(error, EINVAL,
902                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
903                                           "can't mark and flag in same flow");
904         if (action_flags & MLX5_FLOW_ACTION_FLAG)
905                 return rte_flow_error_set(error, EINVAL,
906                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
907                                           "can't have 2 flag"
908                                           " actions in same flow");
909         if (attr->egress)
910                 return rte_flow_error_set(error, ENOTSUP,
911                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
912                                           "flag action not supported for "
913                                           "egress");
914         return 0;
915 }
916
917 /*
918  * Validate the mark action.
919  *
920  * @param[in] action
921  *   Pointer to the queue action.
922  * @param[in] action_flags
923  *   Bit-fields that holds the actions detected until now.
924  * @param[in] attr
925  *   Attributes of flow that includes this action.
926  * @param[out] error
927  *   Pointer to error structure.
928  *
929  * @return
930  *   0 on success, a negative errno value otherwise and rte_errno is set.
931  */
932 int
933 mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
934                                uint64_t action_flags,
935                                const struct rte_flow_attr *attr,
936                                struct rte_flow_error *error)
937 {
938         const struct rte_flow_action_mark *mark = action->conf;
939
940         if (!mark)
941                 return rte_flow_error_set(error, EINVAL,
942                                           RTE_FLOW_ERROR_TYPE_ACTION,
943                                           action,
944                                           "configuration cannot be null");
945         if (mark->id >= MLX5_FLOW_MARK_MAX)
946                 return rte_flow_error_set(error, EINVAL,
947                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
948                                           &mark->id,
949                                           "mark id must in 0 <= id < "
950                                           RTE_STR(MLX5_FLOW_MARK_MAX));
951         if (action_flags & MLX5_FLOW_ACTION_DROP)
952                 return rte_flow_error_set(error, EINVAL,
953                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
954                                           "can't drop and mark in same flow");
955         if (action_flags & MLX5_FLOW_ACTION_FLAG)
956                 return rte_flow_error_set(error, EINVAL,
957                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
958                                           "can't flag and mark in same flow");
959         if (action_flags & MLX5_FLOW_ACTION_MARK)
960                 return rte_flow_error_set(error, EINVAL,
961                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
962                                           "can't have 2 mark actions in same"
963                                           " flow");
964         if (attr->egress)
965                 return rte_flow_error_set(error, ENOTSUP,
966                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
967                                           "mark action not supported for "
968                                           "egress");
969         return 0;
970 }
971
972 /*
973  * Validate the drop action.
974  *
975  * @param[in] action_flags
976  *   Bit-fields that holds the actions detected until now.
977  * @param[in] attr
978  *   Attributes of flow that includes this action.
979  * @param[out] error
980  *   Pointer to error structure.
981  *
982  * @return
983  *   0 on success, a negative errno value otherwise and rte_errno is set.
984  */
985 int
986 mlx5_flow_validate_action_drop(uint64_t action_flags,
987                                const struct rte_flow_attr *attr,
988                                struct rte_flow_error *error)
989 {
990         if (action_flags & MLX5_FLOW_ACTION_FLAG)
991                 return rte_flow_error_set(error, EINVAL,
992                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
993                                           "can't drop and flag in same flow");
994         if (action_flags & MLX5_FLOW_ACTION_MARK)
995                 return rte_flow_error_set(error, EINVAL,
996                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
997                                           "can't drop and mark in same flow");
998         if (action_flags & (MLX5_FLOW_FATE_ACTIONS |
999                             MLX5_FLOW_FATE_ESWITCH_ACTIONS))
1000                 return rte_flow_error_set(error, EINVAL,
1001                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1002                                           "can't have 2 fate actions in"
1003                                           " same flow");
1004         if (attr->egress)
1005                 return rte_flow_error_set(error, ENOTSUP,
1006                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1007                                           "drop action not supported for "
1008                                           "egress");
1009         return 0;
1010 }
1011
1012 /*
1013  * Validate the queue action.
1014  *
1015  * @param[in] action
1016  *   Pointer to the queue action.
1017  * @param[in] action_flags
1018  *   Bit-fields that holds the actions detected until now.
1019  * @param[in] dev
1020  *   Pointer to the Ethernet device structure.
1021  * @param[in] attr
1022  *   Attributes of flow that includes this action.
1023  * @param[out] error
1024  *   Pointer to error structure.
1025  *
1026  * @return
1027  *   0 on success, a negative errno value otherwise and rte_errno is set.
1028  */
1029 int
1030 mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
1031                                 uint64_t action_flags,
1032                                 struct rte_eth_dev *dev,
1033                                 const struct rte_flow_attr *attr,
1034                                 struct rte_flow_error *error)
1035 {
1036         struct mlx5_priv *priv = dev->data->dev_private;
1037         const struct rte_flow_action_queue *queue = action->conf;
1038
1039         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1040                 return rte_flow_error_set(error, EINVAL,
1041                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1042                                           "can't have 2 fate actions in"
1043                                           " same flow");
1044         if (!priv->rxqs_n)
1045                 return rte_flow_error_set(error, EINVAL,
1046                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1047                                           NULL, "No Rx queues configured");
1048         if (queue->index >= priv->rxqs_n)
1049                 return rte_flow_error_set(error, EINVAL,
1050                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1051                                           &queue->index,
1052                                           "queue index out of range");
1053         if (!(*priv->rxqs)[queue->index])
1054                 return rte_flow_error_set(error, EINVAL,
1055                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1056                                           &queue->index,
1057                                           "queue is not configured");
1058         if (attr->egress)
1059                 return rte_flow_error_set(error, ENOTSUP,
1060                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1061                                           "queue action not supported for "
1062                                           "egress");
1063         return 0;
1064 }
1065
1066 /*
1067  * Validate the rss action.
1068  *
1069  * @param[in] action
1070  *   Pointer to the queue action.
1071  * @param[in] action_flags
1072  *   Bit-fields that holds the actions detected until now.
1073  * @param[in] dev
1074  *   Pointer to the Ethernet device structure.
1075  * @param[in] attr
1076  *   Attributes of flow that includes this action.
1077  * @param[in] item_flags
1078  *   Items that were detected.
1079  * @param[out] error
1080  *   Pointer to error structure.
1081  *
1082  * @return
1083  *   0 on success, a negative errno value otherwise and rte_errno is set.
1084  */
1085 int
1086 mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
1087                               uint64_t action_flags,
1088                               struct rte_eth_dev *dev,
1089                               const struct rte_flow_attr *attr,
1090                               uint64_t item_flags,
1091                               struct rte_flow_error *error)
1092 {
1093         struct mlx5_priv *priv = dev->data->dev_private;
1094         const struct rte_flow_action_rss *rss = action->conf;
1095         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1096         unsigned int i;
1097
1098         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
1099                 return rte_flow_error_set(error, EINVAL,
1100                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1101                                           "can't have 2 fate actions"
1102                                           " in same flow");
1103         if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
1104             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
1105                 return rte_flow_error_set(error, ENOTSUP,
1106                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1107                                           &rss->func,
1108                                           "RSS hash function not supported");
1109 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1110         if (rss->level > 2)
1111 #else
1112         if (rss->level > 1)
1113 #endif
1114                 return rte_flow_error_set(error, ENOTSUP,
1115                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1116                                           &rss->level,
1117                                           "tunnel RSS is not supported");
1118         /* allow RSS key_len 0 in case of NULL (default) RSS key. */
1119         if (rss->key_len == 0 && rss->key != NULL)
1120                 return rte_flow_error_set(error, ENOTSUP,
1121                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1122                                           &rss->key_len,
1123                                           "RSS hash key length 0");
1124         if (rss->key_len > 0 && rss->key_len < MLX5_RSS_HASH_KEY_LEN)
1125                 return rte_flow_error_set(error, ENOTSUP,
1126                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1127                                           &rss->key_len,
1128                                           "RSS hash key too small");
1129         if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
1130                 return rte_flow_error_set(error, ENOTSUP,
1131                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1132                                           &rss->key_len,
1133                                           "RSS hash key too large");
1134         if (rss->queue_num > priv->config.ind_table_max_size)
1135                 return rte_flow_error_set(error, ENOTSUP,
1136                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1137                                           &rss->queue_num,
1138                                           "number of queues too large");
1139         if (rss->types & MLX5_RSS_HF_MASK)
1140                 return rte_flow_error_set(error, ENOTSUP,
1141                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1142                                           &rss->types,
1143                                           "some RSS protocols are not"
1144                                           " supported");
1145         if (!priv->rxqs_n)
1146                 return rte_flow_error_set(error, EINVAL,
1147                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1148                                           NULL, "No Rx queues configured");
1149         if (!rss->queue_num)
1150                 return rte_flow_error_set(error, EINVAL,
1151                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1152                                           NULL, "No queues configured");
1153         for (i = 0; i != rss->queue_num; ++i) {
1154                 if (rss->queue[i] >= priv->rxqs_n)
1155                         return rte_flow_error_set
1156                                 (error, EINVAL,
1157                                  RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1158                                  &rss->queue[i], "queue index out of range");
1159                 if (!(*priv->rxqs)[rss->queue[i]])
1160                         return rte_flow_error_set
1161                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1162                                  &rss->queue[i], "queue is not configured");
1163         }
1164         if (attr->egress)
1165                 return rte_flow_error_set(error, ENOTSUP,
1166                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1167                                           "rss action not supported for "
1168                                           "egress");
1169         if (rss->level > 1 &&  !tunnel)
1170                 return rte_flow_error_set(error, EINVAL,
1171                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF, NULL,
1172                                           "inner RSS is not supported for "
1173                                           "non-tunnel flows");
1174         return 0;
1175 }
1176
1177 /*
1178  * Validate the count action.
1179  *
1180  * @param[in] dev
1181  *   Pointer to the Ethernet device structure.
1182  * @param[in] attr
1183  *   Attributes of flow that includes this action.
1184  * @param[out] error
1185  *   Pointer to error structure.
1186  *
1187  * @return
1188  *   0 on success, a negative errno value otherwise and rte_errno is set.
1189  */
1190 int
1191 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
1192                                 const struct rte_flow_attr *attr,
1193                                 struct rte_flow_error *error)
1194 {
1195         if (attr->egress)
1196                 return rte_flow_error_set(error, ENOTSUP,
1197                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1198                                           "count action not supported for "
1199                                           "egress");
1200         return 0;
1201 }
1202
1203 /**
1204  * Verify the @p attributes will be correctly understood by the NIC and store
1205  * them in the @p flow if everything is correct.
1206  *
1207  * @param[in] dev
1208  *   Pointer to the Ethernet device structure.
1209  * @param[in] attributes
1210  *   Pointer to flow attributes
1211  * @param[out] error
1212  *   Pointer to error structure.
1213  *
1214  * @return
1215  *   0 on success, a negative errno value otherwise and rte_errno is set.
1216  */
1217 int
1218 mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
1219                               const struct rte_flow_attr *attributes,
1220                               struct rte_flow_error *error)
1221 {
1222         struct mlx5_priv *priv = dev->data->dev_private;
1223         uint32_t priority_max = priv->config.flow_prio - 1;
1224
1225         if (attributes->group)
1226                 return rte_flow_error_set(error, ENOTSUP,
1227                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1228                                           NULL, "groups is not supported");
1229         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
1230             attributes->priority >= priority_max)
1231                 return rte_flow_error_set(error, ENOTSUP,
1232                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1233                                           NULL, "priority out of range");
1234         if (attributes->egress)
1235                 return rte_flow_error_set(error, ENOTSUP,
1236                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1237                                           "egress is not supported");
1238         if (attributes->transfer && !priv->config.dv_esw_en)
1239                 return rte_flow_error_set(error, ENOTSUP,
1240                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1241                                           NULL, "transfer is not supported");
1242         if (!attributes->ingress)
1243                 return rte_flow_error_set(error, EINVAL,
1244                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1245                                           NULL,
1246                                           "ingress attribute is mandatory");
1247         return 0;
1248 }
1249
1250 /**
1251  * Validate ICMP6 item.
1252  *
1253  * @param[in] item
1254  *   Item specification.
1255  * @param[in] item_flags
1256  *   Bit-fields that holds the items detected until now.
1257  * @param[out] error
1258  *   Pointer to error structure.
1259  *
1260  * @return
1261  *   0 on success, a negative errno value otherwise and rte_errno is set.
1262  */
1263 int
1264 mlx5_flow_validate_item_icmp6(const struct rte_flow_item *item,
1265                                uint64_t item_flags,
1266                                uint8_t target_protocol,
1267                                struct rte_flow_error *error)
1268 {
1269         const struct rte_flow_item_icmp6 *mask = item->mask;
1270         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1271         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV6 :
1272                                       MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1273         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1274                                       MLX5_FLOW_LAYER_OUTER_L4;
1275         int ret;
1276
1277         if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMPV6)
1278                 return rte_flow_error_set(error, EINVAL,
1279                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1280                                           "protocol filtering not compatible"
1281                                           " with ICMP6 layer");
1282         if (!(item_flags & l3m))
1283                 return rte_flow_error_set(error, EINVAL,
1284                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1285                                           "IPv6 is mandatory to filter on"
1286                                           " ICMP6");
1287         if (item_flags & l4m)
1288                 return rte_flow_error_set(error, EINVAL,
1289                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1290                                           "multiple L4 layers not supported");
1291         if (!mask)
1292                 mask = &rte_flow_item_icmp6_mask;
1293         ret = mlx5_flow_item_acceptable
1294                 (item, (const uint8_t *)mask,
1295                  (const uint8_t *)&rte_flow_item_icmp6_mask,
1296                  sizeof(struct rte_flow_item_icmp6), error);
1297         if (ret < 0)
1298                 return ret;
1299         return 0;
1300 }
1301
1302 /**
1303  * Validate ICMP item.
1304  *
1305  * @param[in] item
1306  *   Item specification.
1307  * @param[in] item_flags
1308  *   Bit-fields that holds the items detected until now.
1309  * @param[out] error
1310  *   Pointer to error structure.
1311  *
1312  * @return
1313  *   0 on success, a negative errno value otherwise and rte_errno is set.
1314  */
1315 int
1316 mlx5_flow_validate_item_icmp(const struct rte_flow_item *item,
1317                              uint64_t item_flags,
1318                              uint8_t target_protocol,
1319                              struct rte_flow_error *error)
1320 {
1321         const struct rte_flow_item_icmp *mask = item->mask;
1322         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1323         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3_IPV4 :
1324                                       MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1325         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1326                                       MLX5_FLOW_LAYER_OUTER_L4;
1327         int ret;
1328
1329         if (target_protocol != 0xFF && target_protocol != IPPROTO_ICMP)
1330                 return rte_flow_error_set(error, EINVAL,
1331                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1332                                           "protocol filtering not compatible"
1333                                           " with ICMP layer");
1334         if (!(item_flags & l3m))
1335                 return rte_flow_error_set(error, EINVAL,
1336                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1337                                           "IPv4 is mandatory to filter"
1338                                           " on ICMP");
1339         if (item_flags & l4m)
1340                 return rte_flow_error_set(error, EINVAL,
1341                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1342                                           "multiple L4 layers not supported");
1343         if (!mask)
1344                 mask = &rte_flow_item_icmp_mask;
1345         ret = mlx5_flow_item_acceptable
1346                 (item, (const uint8_t *)mask,
1347                  (const uint8_t *)&rte_flow_item_icmp_mask,
1348                  sizeof(struct rte_flow_item_icmp), error);
1349         if (ret < 0)
1350                 return ret;
1351         return 0;
1352 }
1353
1354 /**
1355  * Validate Ethernet item.
1356  *
1357  * @param[in] item
1358  *   Item specification.
1359  * @param[in] item_flags
1360  *   Bit-fields that holds the items detected until now.
1361  * @param[out] error
1362  *   Pointer to error structure.
1363  *
1364  * @return
1365  *   0 on success, a negative errno value otherwise and rte_errno is set.
1366  */
1367 int
1368 mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
1369                             uint64_t item_flags,
1370                             struct rte_flow_error *error)
1371 {
1372         const struct rte_flow_item_eth *mask = item->mask;
1373         const struct rte_flow_item_eth nic_mask = {
1374                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1375                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1376                 .type = RTE_BE16(0xffff),
1377         };
1378         int ret;
1379         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1380         const uint64_t ethm = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1381                                        MLX5_FLOW_LAYER_OUTER_L2;
1382
1383         if (item_flags & ethm)
1384                 return rte_flow_error_set(error, ENOTSUP,
1385                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1386                                           "multiple L2 layers not supported");
1387         if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_L3)) ||
1388             (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_L3)))
1389                 return rte_flow_error_set(error, EINVAL,
1390                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1391                                           "L2 layer should not follow "
1392                                           "L3 layers");
1393         if ((!tunnel && (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN)) ||
1394             (tunnel && (item_flags & MLX5_FLOW_LAYER_INNER_VLAN)))
1395                 return rte_flow_error_set(error, EINVAL,
1396                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1397                                           "L2 layer should not follow VLAN");
1398         if (!mask)
1399                 mask = &rte_flow_item_eth_mask;
1400         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1401                                         (const uint8_t *)&nic_mask,
1402                                         sizeof(struct rte_flow_item_eth),
1403                                         error);
1404         return ret;
1405 }
1406
1407 /**
1408  * Validate VLAN item.
1409  *
1410  * @param[in] item
1411  *   Item specification.
1412  * @param[in] item_flags
1413  *   Bit-fields that holds the items detected until now.
1414  * @param[in] dev
1415  *   Ethernet device flow is being created on.
1416  * @param[out] error
1417  *   Pointer to error structure.
1418  *
1419  * @return
1420  *   0 on success, a negative errno value otherwise and rte_errno is set.
1421  */
1422 int
1423 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
1424                              uint64_t item_flags,
1425                              struct rte_eth_dev *dev,
1426                              struct rte_flow_error *error)
1427 {
1428         const struct rte_flow_item_vlan *spec = item->spec;
1429         const struct rte_flow_item_vlan *mask = item->mask;
1430         const struct rte_flow_item_vlan nic_mask = {
1431                 .tci = RTE_BE16(UINT16_MAX),
1432                 .inner_type = RTE_BE16(UINT16_MAX),
1433         };
1434         uint16_t vlan_tag = 0;
1435         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1436         int ret;
1437         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1438                                         MLX5_FLOW_LAYER_INNER_L4) :
1439                                        (MLX5_FLOW_LAYER_OUTER_L3 |
1440                                         MLX5_FLOW_LAYER_OUTER_L4);
1441         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1442                                         MLX5_FLOW_LAYER_OUTER_VLAN;
1443
1444         if (item_flags & vlanm)
1445                 return rte_flow_error_set(error, EINVAL,
1446                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1447                                           "multiple VLAN layers not supported");
1448         else if ((item_flags & l34m) != 0)
1449                 return rte_flow_error_set(error, EINVAL,
1450                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1451                                           "VLAN cannot follow L3/L4 layer");
1452         if (!mask)
1453                 mask = &rte_flow_item_vlan_mask;
1454         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1455                                         (const uint8_t *)&nic_mask,
1456                                         sizeof(struct rte_flow_item_vlan),
1457                                         error);
1458         if (ret)
1459                 return ret;
1460         if (!tunnel && mask->tci != RTE_BE16(0x0fff)) {
1461                 struct mlx5_priv *priv = dev->data->dev_private;
1462
1463                 if (priv->vmwa_context) {
1464                         /*
1465                          * Non-NULL context means we have a virtual machine
1466                          * and SR-IOV enabled, we have to create VLAN interface
1467                          * to make hypervisor to setup E-Switch vport
1468                          * context correctly. We avoid creating the multiple
1469                          * VLAN interfaces, so we cannot support VLAN tag mask.
1470                          */
1471                         return rte_flow_error_set(error, EINVAL,
1472                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1473                                                   item,
1474                                                   "VLAN tag mask is not"
1475                                                   " supported in virtual"
1476                                                   " environment");
1477                 }
1478         }
1479         if (spec) {
1480                 vlan_tag = spec->tci;
1481                 vlan_tag &= mask->tci;
1482         }
1483         /*
1484          * From verbs perspective an empty VLAN is equivalent
1485          * to a packet without VLAN layer.
1486          */
1487         if (!vlan_tag)
1488                 return rte_flow_error_set(error, EINVAL,
1489                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1490                                           item->spec,
1491                                           "VLAN cannot be empty");
1492         return 0;
1493 }
1494
1495 /**
1496  * Validate IPV4 item.
1497  *
1498  * @param[in] item
1499  *   Item specification.
1500  * @param[in] item_flags
1501  *   Bit-fields that holds the items detected until now.
1502  * @param[in] acc_mask
1503  *   Acceptable mask, if NULL default internal default mask
1504  *   will be used to check whether item fields are supported.
1505  * @param[out] error
1506  *   Pointer to error structure.
1507  *
1508  * @return
1509  *   0 on success, a negative errno value otherwise and rte_errno is set.
1510  */
1511 int
1512 mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
1513                              uint64_t item_flags,
1514                              uint64_t last_item,
1515                              uint16_t ether_type,
1516                              const struct rte_flow_item_ipv4 *acc_mask,
1517                              struct rte_flow_error *error)
1518 {
1519         const struct rte_flow_item_ipv4 *mask = item->mask;
1520         const struct rte_flow_item_ipv4 *spec = item->spec;
1521         const struct rte_flow_item_ipv4 nic_mask = {
1522                 .hdr = {
1523                         .src_addr = RTE_BE32(0xffffffff),
1524                         .dst_addr = RTE_BE32(0xffffffff),
1525                         .type_of_service = 0xff,
1526                         .next_proto_id = 0xff,
1527                 },
1528         };
1529         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1530         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1531                                       MLX5_FLOW_LAYER_OUTER_L3;
1532         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1533                                       MLX5_FLOW_LAYER_OUTER_L4;
1534         int ret;
1535         uint8_t next_proto = 0xFF;
1536         const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
1537                                   MLX5_FLOW_LAYER_OUTER_VLAN |
1538                                   MLX5_FLOW_LAYER_INNER_VLAN);
1539
1540         if ((last_item & l2_vlan) && ether_type &&
1541             ether_type != RTE_ETHER_TYPE_IPV4)
1542                 return rte_flow_error_set(error, EINVAL,
1543                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1544                                           "IPv4 cannot follow L2/VLAN layer "
1545                                           "which ether type is not IPv4");
1546         if (item_flags & MLX5_FLOW_LAYER_IPIP) {
1547                 if (mask && spec)
1548                         next_proto = mask->hdr.next_proto_id &
1549                                      spec->hdr.next_proto_id;
1550                 if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
1551                         return rte_flow_error_set(error, EINVAL,
1552                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1553                                                   item,
1554                                                   "multiple tunnel "
1555                                                   "not supported");
1556         }
1557         if (item_flags & MLX5_FLOW_LAYER_IPV6_ENCAP)
1558                 return rte_flow_error_set(error, EINVAL,
1559                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1560                                           "wrong tunnel type - IPv6 specified "
1561                                           "but IPv4 item provided");
1562         if (item_flags & l3m)
1563                 return rte_flow_error_set(error, ENOTSUP,
1564                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1565                                           "multiple L3 layers not supported");
1566         else if (item_flags & l4m)
1567                 return rte_flow_error_set(error, EINVAL,
1568                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1569                                           "L3 cannot follow an L4 layer.");
1570         else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
1571                   !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
1572                 return rte_flow_error_set(error, EINVAL,
1573                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1574                                           "L3 cannot follow an NVGRE layer.");
1575         if (!mask)
1576                 mask = &rte_flow_item_ipv4_mask;
1577         else if (mask->hdr.next_proto_id != 0 &&
1578                  mask->hdr.next_proto_id != 0xff)
1579                 return rte_flow_error_set(error, EINVAL,
1580                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1581                                           "partial mask is not supported"
1582                                           " for protocol");
1583         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1584                                         acc_mask ? (const uint8_t *)acc_mask
1585                                                  : (const uint8_t *)&nic_mask,
1586                                         sizeof(struct rte_flow_item_ipv4),
1587                                         error);
1588         if (ret < 0)
1589                 return ret;
1590         return 0;
1591 }
1592
1593 /**
1594  * Validate IPV6 item.
1595  *
1596  * @param[in] item
1597  *   Item specification.
1598  * @param[in] item_flags
1599  *   Bit-fields that holds the items detected until now.
1600  * @param[in] acc_mask
1601  *   Acceptable mask, if NULL default internal default mask
1602  *   will be used to check whether item fields are supported.
1603  * @param[out] error
1604  *   Pointer to error structure.
1605  *
1606  * @return
1607  *   0 on success, a negative errno value otherwise and rte_errno is set.
1608  */
1609 int
1610 mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
1611                              uint64_t item_flags,
1612                              uint64_t last_item,
1613                              uint16_t ether_type,
1614                              const struct rte_flow_item_ipv6 *acc_mask,
1615                              struct rte_flow_error *error)
1616 {
1617         const struct rte_flow_item_ipv6 *mask = item->mask;
1618         const struct rte_flow_item_ipv6 *spec = item->spec;
1619         const struct rte_flow_item_ipv6 nic_mask = {
1620                 .hdr = {
1621                         .src_addr =
1622                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1623                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1624                         .dst_addr =
1625                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1626                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1627                         .vtc_flow = RTE_BE32(0xffffffff),
1628                         .proto = 0xff,
1629                         .hop_limits = 0xff,
1630                 },
1631         };
1632         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1633         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1634                                       MLX5_FLOW_LAYER_OUTER_L3;
1635         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1636                                       MLX5_FLOW_LAYER_OUTER_L4;
1637         int ret;
1638         uint8_t next_proto = 0xFF;
1639         const uint64_t l2_vlan = (MLX5_FLOW_LAYER_L2 |
1640                                   MLX5_FLOW_LAYER_OUTER_VLAN |
1641                                   MLX5_FLOW_LAYER_INNER_VLAN);
1642
1643         if ((last_item & l2_vlan) && ether_type &&
1644             ether_type != RTE_ETHER_TYPE_IPV6)
1645                 return rte_flow_error_set(error, EINVAL,
1646                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1647                                           "IPv6 cannot follow L2/VLAN layer "
1648                                           "which ether type is not IPv6");
1649         if (item_flags & MLX5_FLOW_LAYER_IPV6_ENCAP) {
1650                 if (mask && spec)
1651                         next_proto = mask->hdr.proto & spec->hdr.proto;
1652                 if (next_proto == IPPROTO_IPIP || next_proto == IPPROTO_IPV6)
1653                         return rte_flow_error_set(error, EINVAL,
1654                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1655                                                   item,
1656                                                   "multiple tunnel "
1657                                                   "not supported");
1658         }
1659         if (item_flags & MLX5_FLOW_LAYER_IPIP)
1660                 return rte_flow_error_set(error, EINVAL,
1661                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1662                                           "wrong tunnel type - IPv4 specified "
1663                                           "but IPv6 item provided");
1664         if (item_flags & l3m)
1665                 return rte_flow_error_set(error, ENOTSUP,
1666                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1667                                           "multiple L3 layers not supported");
1668         else if (item_flags & l4m)
1669                 return rte_flow_error_set(error, EINVAL,
1670                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1671                                           "L3 cannot follow an L4 layer.");
1672         else if ((item_flags & MLX5_FLOW_LAYER_NVGRE) &&
1673                   !(item_flags & MLX5_FLOW_LAYER_INNER_L2))
1674                 return rte_flow_error_set(error, EINVAL,
1675                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1676                                           "L3 cannot follow an NVGRE layer.");
1677         if (!mask)
1678                 mask = &rte_flow_item_ipv6_mask;
1679         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1680                                         acc_mask ? (const uint8_t *)acc_mask
1681                                                  : (const uint8_t *)&nic_mask,
1682                                         sizeof(struct rte_flow_item_ipv6),
1683                                         error);
1684         if (ret < 0)
1685                 return ret;
1686         return 0;
1687 }
1688
1689 /**
1690  * Validate UDP item.
1691  *
1692  * @param[in] item
1693  *   Item specification.
1694  * @param[in] item_flags
1695  *   Bit-fields that holds the items detected until now.
1696  * @param[in] target_protocol
1697  *   The next protocol in the previous item.
1698  * @param[in] flow_mask
1699  *   mlx5 flow-specific (DV, verbs, etc.) supported header fields mask.
1700  * @param[out] error
1701  *   Pointer to error structure.
1702  *
1703  * @return
1704  *   0 on success, a negative errno value otherwise and rte_errno is set.
1705  */
1706 int
1707 mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
1708                             uint64_t item_flags,
1709                             uint8_t target_protocol,
1710                             struct rte_flow_error *error)
1711 {
1712         const struct rte_flow_item_udp *mask = item->mask;
1713         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1714         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1715                                       MLX5_FLOW_LAYER_OUTER_L3;
1716         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1717                                       MLX5_FLOW_LAYER_OUTER_L4;
1718         int ret;
1719
1720         if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
1721                 return rte_flow_error_set(error, EINVAL,
1722                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1723                                           "protocol filtering not compatible"
1724                                           " with UDP layer");
1725         if (!(item_flags & l3m))
1726                 return rte_flow_error_set(error, EINVAL,
1727                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1728                                           "L3 is mandatory to filter on L4");
1729         if (item_flags & l4m)
1730                 return rte_flow_error_set(error, EINVAL,
1731                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1732                                           "multiple L4 layers not supported");
1733         if (!mask)
1734                 mask = &rte_flow_item_udp_mask;
1735         ret = mlx5_flow_item_acceptable
1736                 (item, (const uint8_t *)mask,
1737                  (const uint8_t *)&rte_flow_item_udp_mask,
1738                  sizeof(struct rte_flow_item_udp), error);
1739         if (ret < 0)
1740                 return ret;
1741         return 0;
1742 }
1743
1744 /**
1745  * Validate TCP item.
1746  *
1747  * @param[in] item
1748  *   Item specification.
1749  * @param[in] item_flags
1750  *   Bit-fields that holds the items detected until now.
1751  * @param[in] target_protocol
1752  *   The next protocol in the previous item.
1753  * @param[out] error
1754  *   Pointer to error structure.
1755  *
1756  * @return
1757  *   0 on success, a negative errno value otherwise and rte_errno is set.
1758  */
1759 int
1760 mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
1761                             uint64_t item_flags,
1762                             uint8_t target_protocol,
1763                             const struct rte_flow_item_tcp *flow_mask,
1764                             struct rte_flow_error *error)
1765 {
1766         const struct rte_flow_item_tcp *mask = item->mask;
1767         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1768         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1769                                       MLX5_FLOW_LAYER_OUTER_L3;
1770         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1771                                       MLX5_FLOW_LAYER_OUTER_L4;
1772         int ret;
1773
1774         assert(flow_mask);
1775         if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
1776                 return rte_flow_error_set(error, EINVAL,
1777                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1778                                           "protocol filtering not compatible"
1779                                           " with TCP layer");
1780         if (!(item_flags & l3m))
1781                 return rte_flow_error_set(error, EINVAL,
1782                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1783                                           "L3 is mandatory to filter on L4");
1784         if (item_flags & l4m)
1785                 return rte_flow_error_set(error, EINVAL,
1786                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1787                                           "multiple L4 layers not supported");
1788         if (!mask)
1789                 mask = &rte_flow_item_tcp_mask;
1790         ret = mlx5_flow_item_acceptable
1791                 (item, (const uint8_t *)mask,
1792                  (const uint8_t *)flow_mask,
1793                  sizeof(struct rte_flow_item_tcp), error);
1794         if (ret < 0)
1795                 return ret;
1796         return 0;
1797 }
1798
1799 /**
1800  * Validate VXLAN item.
1801  *
1802  * @param[in] item
1803  *   Item specification.
1804  * @param[in] item_flags
1805  *   Bit-fields that holds the items detected until now.
1806  * @param[in] target_protocol
1807  *   The next protocol in the previous item.
1808  * @param[out] error
1809  *   Pointer to error structure.
1810  *
1811  * @return
1812  *   0 on success, a negative errno value otherwise and rte_errno is set.
1813  */
1814 int
1815 mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
1816                               uint64_t item_flags,
1817                               struct rte_flow_error *error)
1818 {
1819         const struct rte_flow_item_vxlan *spec = item->spec;
1820         const struct rte_flow_item_vxlan *mask = item->mask;
1821         int ret;
1822         union vni {
1823                 uint32_t vlan_id;
1824                 uint8_t vni[4];
1825         } id = { .vlan_id = 0, };
1826         uint32_t vlan_id = 0;
1827
1828
1829         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1830                 return rte_flow_error_set(error, ENOTSUP,
1831                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1832                                           "multiple tunnel layers not"
1833                                           " supported");
1834         /*
1835          * Verify only UDPv4 is present as defined in
1836          * https://tools.ietf.org/html/rfc7348
1837          */
1838         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1839                 return rte_flow_error_set(error, EINVAL,
1840                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1841                                           "no outer UDP layer found");
1842         if (!mask)
1843                 mask = &rte_flow_item_vxlan_mask;
1844         ret = mlx5_flow_item_acceptable
1845                 (item, (const uint8_t *)mask,
1846                  (const uint8_t *)&rte_flow_item_vxlan_mask,
1847                  sizeof(struct rte_flow_item_vxlan),
1848                  error);
1849         if (ret < 0)
1850                 return ret;
1851         if (spec) {
1852                 memcpy(&id.vni[1], spec->vni, 3);
1853                 vlan_id = id.vlan_id;
1854                 memcpy(&id.vni[1], mask->vni, 3);
1855                 vlan_id &= id.vlan_id;
1856         }
1857         /*
1858          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if
1859          * only this layer is defined in the Verbs specification it is
1860          * interpreted as wildcard and all packets will match this
1861          * rule, if it follows a full stack layer (ex: eth / ipv4 /
1862          * udp), all packets matching the layers before will also
1863          * match this rule.  To avoid such situation, VNI 0 is
1864          * currently refused.
1865          */
1866         if (!vlan_id)
1867                 return rte_flow_error_set(error, ENOTSUP,
1868                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1869                                           "VXLAN vni cannot be 0");
1870         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1871                 return rte_flow_error_set(error, ENOTSUP,
1872                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1873                                           "VXLAN tunnel must be fully defined");
1874         return 0;
1875 }
1876
1877 /**
1878  * Validate VXLAN_GPE item.
1879  *
1880  * @param[in] item
1881  *   Item specification.
1882  * @param[in] item_flags
1883  *   Bit-fields that holds the items detected until now.
1884  * @param[in] priv
1885  *   Pointer to the private data structure.
1886  * @param[in] target_protocol
1887  *   The next protocol in the previous item.
1888  * @param[out] error
1889  *   Pointer to error structure.
1890  *
1891  * @return
1892  *   0 on success, a negative errno value otherwise and rte_errno is set.
1893  */
1894 int
1895 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
1896                                   uint64_t item_flags,
1897                                   struct rte_eth_dev *dev,
1898                                   struct rte_flow_error *error)
1899 {
1900         struct mlx5_priv *priv = dev->data->dev_private;
1901         const struct rte_flow_item_vxlan_gpe *spec = item->spec;
1902         const struct rte_flow_item_vxlan_gpe *mask = item->mask;
1903         int ret;
1904         union vni {
1905                 uint32_t vlan_id;
1906                 uint8_t vni[4];
1907         } id = { .vlan_id = 0, };
1908         uint32_t vlan_id = 0;
1909
1910         if (!priv->config.l3_vxlan_en)
1911                 return rte_flow_error_set(error, ENOTSUP,
1912                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1913                                           "L3 VXLAN is not enabled by device"
1914                                           " parameter and/or not configured in"
1915                                           " firmware");
1916         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1917                 return rte_flow_error_set(error, ENOTSUP,
1918                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1919                                           "multiple tunnel layers not"
1920                                           " supported");
1921         /*
1922          * Verify only UDPv4 is present as defined in
1923          * https://tools.ietf.org/html/rfc7348
1924          */
1925         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1926                 return rte_flow_error_set(error, EINVAL,
1927                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1928                                           "no outer UDP layer found");
1929         if (!mask)
1930                 mask = &rte_flow_item_vxlan_gpe_mask;
1931         ret = mlx5_flow_item_acceptable
1932                 (item, (const uint8_t *)mask,
1933                  (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
1934                  sizeof(struct rte_flow_item_vxlan_gpe),
1935                  error);
1936         if (ret < 0)
1937                 return ret;
1938         if (spec) {
1939                 if (spec->protocol)
1940                         return rte_flow_error_set(error, ENOTSUP,
1941                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1942                                                   item,
1943                                                   "VxLAN-GPE protocol"
1944                                                   " not supported");
1945                 memcpy(&id.vni[1], spec->vni, 3);
1946                 vlan_id = id.vlan_id;
1947                 memcpy(&id.vni[1], mask->vni, 3);
1948                 vlan_id &= id.vlan_id;
1949         }
1950         /*
1951          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1952          * layer is defined in the Verbs specification it is interpreted as
1953          * wildcard and all packets will match this rule, if it follows a full
1954          * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1955          * before will also match this rule.  To avoid such situation, VNI 0
1956          * is currently refused.
1957          */
1958         if (!vlan_id)
1959                 return rte_flow_error_set(error, ENOTSUP,
1960                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1961                                           "VXLAN-GPE vni cannot be 0");
1962         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1963                 return rte_flow_error_set(error, ENOTSUP,
1964                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1965                                           "VXLAN-GPE tunnel must be fully"
1966                                           " defined");
1967         return 0;
1968 }
1969 /**
1970  * Validate GRE Key item.
1971  *
1972  * @param[in] item
1973  *   Item specification.
1974  * @param[in] item_flags
1975  *   Bit flags to mark detected items.
1976  * @param[in] gre_item
1977  *   Pointer to gre_item
1978  * @param[out] error
1979  *   Pointer to error structure.
1980  *
1981  * @return
1982  *   0 on success, a negative errno value otherwise and rte_errno is set.
1983  */
1984 int
1985 mlx5_flow_validate_item_gre_key(const struct rte_flow_item *item,
1986                                 uint64_t item_flags,
1987                                 const struct rte_flow_item *gre_item,
1988                                 struct rte_flow_error *error)
1989 {
1990         const rte_be32_t *mask = item->mask;
1991         int ret = 0;
1992         rte_be32_t gre_key_default_mask = RTE_BE32(UINT32_MAX);
1993         const struct rte_flow_item_gre *gre_spec = gre_item->spec;
1994         const struct rte_flow_item_gre *gre_mask = gre_item->mask;
1995
1996         if (item_flags & MLX5_FLOW_LAYER_GRE_KEY)
1997                 return rte_flow_error_set(error, ENOTSUP,
1998                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1999                                           "Multiple GRE key not support");
2000         if (!(item_flags & MLX5_FLOW_LAYER_GRE))
2001                 return rte_flow_error_set(error, ENOTSUP,
2002                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2003                                           "No preceding GRE header");
2004         if (item_flags & MLX5_FLOW_LAYER_INNER)
2005                 return rte_flow_error_set(error, ENOTSUP,
2006                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2007                                           "GRE key following a wrong item");
2008         if (!gre_mask)
2009                 gre_mask = &rte_flow_item_gre_mask;
2010         if (gre_spec && (gre_mask->c_rsvd0_ver & RTE_BE16(0x2000)) &&
2011                          !(gre_spec->c_rsvd0_ver & RTE_BE16(0x2000)))
2012                 return rte_flow_error_set(error, EINVAL,
2013                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2014                                           "Key bit must be on");
2015
2016         if (!mask)
2017                 mask = &gre_key_default_mask;
2018         ret = mlx5_flow_item_acceptable
2019                 (item, (const uint8_t *)mask,
2020                  (const uint8_t *)&gre_key_default_mask,
2021                  sizeof(rte_be32_t), error);
2022         return ret;
2023 }
2024
2025 /**
2026  * Validate GRE item.
2027  *
2028  * @param[in] item
2029  *   Item specification.
2030  * @param[in] item_flags
2031  *   Bit flags to mark detected items.
2032  * @param[in] target_protocol
2033  *   The next protocol in the previous item.
2034  * @param[out] error
2035  *   Pointer to error structure.
2036  *
2037  * @return
2038  *   0 on success, a negative errno value otherwise and rte_errno is set.
2039  */
2040 int
2041 mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
2042                             uint64_t item_flags,
2043                             uint8_t target_protocol,
2044                             struct rte_flow_error *error)
2045 {
2046         const struct rte_flow_item_gre *spec __rte_unused = item->spec;
2047         const struct rte_flow_item_gre *mask = item->mask;
2048         int ret;
2049         const struct rte_flow_item_gre nic_mask = {
2050                 .c_rsvd0_ver = RTE_BE16(0xB000),
2051                 .protocol = RTE_BE16(UINT16_MAX),
2052         };
2053
2054         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
2055                 return rte_flow_error_set(error, EINVAL,
2056                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2057                                           "protocol filtering not compatible"
2058                                           " with this GRE layer");
2059         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2060                 return rte_flow_error_set(error, ENOTSUP,
2061                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2062                                           "multiple tunnel layers not"
2063                                           " supported");
2064         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
2065                 return rte_flow_error_set(error, ENOTSUP,
2066                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2067                                           "L3 Layer is missing");
2068         if (!mask)
2069                 mask = &rte_flow_item_gre_mask;
2070         ret = mlx5_flow_item_acceptable
2071                 (item, (const uint8_t *)mask,
2072                  (const uint8_t *)&nic_mask,
2073                  sizeof(struct rte_flow_item_gre), error);
2074         if (ret < 0)
2075                 return ret;
2076 #ifndef HAVE_MLX5DV_DR
2077 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
2078         if (spec && (spec->protocol & mask->protocol))
2079                 return rte_flow_error_set(error, ENOTSUP,
2080                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2081                                           "without MPLS support the"
2082                                           " specification cannot be used for"
2083                                           " filtering");
2084 #endif
2085 #endif
2086         return 0;
2087 }
2088
2089 /**
2090  * Validate Geneve item.
2091  *
2092  * @param[in] item
2093  *   Item specification.
2094  * @param[in] itemFlags
2095  *   Bit-fields that holds the items detected until now.
2096  * @param[in] enPriv
2097  *   Pointer to the private data structure.
2098  * @param[out] error
2099  *   Pointer to error structure.
2100  *
2101  * @return
2102  *   0 on success, a negative errno value otherwise and rte_errno is set.
2103  */
2104
2105 int
2106 mlx5_flow_validate_item_geneve(const struct rte_flow_item *item,
2107                                uint64_t item_flags,
2108                                struct rte_eth_dev *dev,
2109                                struct rte_flow_error *error)
2110 {
2111         struct mlx5_priv *priv = dev->data->dev_private;
2112         const struct rte_flow_item_geneve *spec = item->spec;
2113         const struct rte_flow_item_geneve *mask = item->mask;
2114         int ret;
2115         uint16_t gbhdr;
2116         uint8_t opt_len = priv->config.hca_attr.geneve_max_opt_len ?
2117                           MLX5_GENEVE_OPT_LEN_1 : MLX5_GENEVE_OPT_LEN_0;
2118         const struct rte_flow_item_geneve nic_mask = {
2119                 .ver_opt_len_o_c_rsvd0 = RTE_BE16(0x3f80),
2120                 .vni = "\xff\xff\xff",
2121                 .protocol = RTE_BE16(UINT16_MAX),
2122         };
2123
2124         if (!(priv->config.hca_attr.flex_parser_protocols &
2125               MLX5_HCA_FLEX_GENEVE_ENABLED) ||
2126             !priv->config.hca_attr.tunnel_stateless_geneve_rx)
2127                 return rte_flow_error_set(error, ENOTSUP,
2128                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2129                                           "L3 Geneve is not enabled by device"
2130                                           " parameter and/or not configured in"
2131                                           " firmware");
2132         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2133                 return rte_flow_error_set(error, ENOTSUP,
2134                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2135                                           "multiple tunnel layers not"
2136                                           " supported");
2137         /*
2138          * Verify only UDPv4 is present as defined in
2139          * https://tools.ietf.org/html/rfc7348
2140          */
2141         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2142                 return rte_flow_error_set(error, EINVAL,
2143                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2144                                           "no outer UDP layer found");
2145         if (!mask)
2146                 mask = &rte_flow_item_geneve_mask;
2147         ret = mlx5_flow_item_acceptable
2148                                   (item, (const uint8_t *)mask,
2149                                    (const uint8_t *)&nic_mask,
2150                                    sizeof(struct rte_flow_item_geneve), error);
2151         if (ret)
2152                 return ret;
2153         if (spec) {
2154                 gbhdr = rte_be_to_cpu_16(spec->ver_opt_len_o_c_rsvd0);
2155                 if (MLX5_GENEVE_VER_VAL(gbhdr) ||
2156                      MLX5_GENEVE_CRITO_VAL(gbhdr) ||
2157                      MLX5_GENEVE_RSVD_VAL(gbhdr) || spec->rsvd1)
2158                         return rte_flow_error_set(error, ENOTSUP,
2159                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2160                                                   item,
2161                                                   "Geneve protocol unsupported"
2162                                                   " fields are being used");
2163                 if (MLX5_GENEVE_OPTLEN_VAL(gbhdr) > opt_len)
2164                         return rte_flow_error_set
2165                                         (error, ENOTSUP,
2166                                          RTE_FLOW_ERROR_TYPE_ITEM,
2167                                          item,
2168                                          "Unsupported Geneve options length");
2169         }
2170         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
2171                 return rte_flow_error_set
2172                                     (error, ENOTSUP,
2173                                      RTE_FLOW_ERROR_TYPE_ITEM, item,
2174                                      "Geneve tunnel must be fully defined");
2175         return 0;
2176 }
2177
2178 /**
2179  * Validate MPLS item.
2180  *
2181  * @param[in] dev
2182  *   Pointer to the rte_eth_dev structure.
2183  * @param[in] item
2184  *   Item specification.
2185  * @param[in] item_flags
2186  *   Bit-fields that holds the items detected until now.
2187  * @param[in] prev_layer
2188  *   The protocol layer indicated in previous item.
2189  * @param[out] error
2190  *   Pointer to error structure.
2191  *
2192  * @return
2193  *   0 on success, a negative errno value otherwise and rte_errno is set.
2194  */
2195 int
2196 mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev __rte_unused,
2197                              const struct rte_flow_item *item __rte_unused,
2198                              uint64_t item_flags __rte_unused,
2199                              uint64_t prev_layer __rte_unused,
2200                              struct rte_flow_error *error)
2201 {
2202 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
2203         const struct rte_flow_item_mpls *mask = item->mask;
2204         struct mlx5_priv *priv = dev->data->dev_private;
2205         int ret;
2206
2207         if (!priv->config.mpls_en)
2208                 return rte_flow_error_set(error, ENOTSUP,
2209                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2210                                           "MPLS not supported or"
2211                                           " disabled in firmware"
2212                                           " configuration.");
2213         /* MPLS over IP, UDP, GRE is allowed */
2214         if (!(prev_layer & (MLX5_FLOW_LAYER_OUTER_L3 |
2215                             MLX5_FLOW_LAYER_OUTER_L4_UDP |
2216                             MLX5_FLOW_LAYER_GRE)))
2217                 return rte_flow_error_set(error, EINVAL,
2218                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2219                                           "protocol filtering not compatible"
2220                                           " with MPLS layer");
2221         /* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
2222         if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
2223             !(item_flags & MLX5_FLOW_LAYER_GRE))
2224                 return rte_flow_error_set(error, ENOTSUP,
2225                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2226                                           "multiple tunnel layers not"
2227                                           " supported");
2228         if (!mask)
2229                 mask = &rte_flow_item_mpls_mask;
2230         ret = mlx5_flow_item_acceptable
2231                 (item, (const uint8_t *)mask,
2232                  (const uint8_t *)&rte_flow_item_mpls_mask,
2233                  sizeof(struct rte_flow_item_mpls), error);
2234         if (ret < 0)
2235                 return ret;
2236         return 0;
2237 #endif
2238         return rte_flow_error_set(error, ENOTSUP,
2239                                   RTE_FLOW_ERROR_TYPE_ITEM, item,
2240                                   "MPLS is not supported by Verbs, please"
2241                                   " update.");
2242 }
2243
2244 /**
2245  * Validate NVGRE item.
2246  *
2247  * @param[in] item
2248  *   Item specification.
2249  * @param[in] item_flags
2250  *   Bit flags to mark detected items.
2251  * @param[in] target_protocol
2252  *   The next protocol in the previous item.
2253  * @param[out] error
2254  *   Pointer to error structure.
2255  *
2256  * @return
2257  *   0 on success, a negative errno value otherwise and rte_errno is set.
2258  */
2259 int
2260 mlx5_flow_validate_item_nvgre(const struct rte_flow_item *item,
2261                               uint64_t item_flags,
2262                               uint8_t target_protocol,
2263                               struct rte_flow_error *error)
2264 {
2265         const struct rte_flow_item_nvgre *mask = item->mask;
2266         int ret;
2267
2268         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
2269                 return rte_flow_error_set(error, EINVAL,
2270                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2271                                           "protocol filtering not compatible"
2272                                           " with this GRE layer");
2273         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2274                 return rte_flow_error_set(error, ENOTSUP,
2275                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2276                                           "multiple tunnel layers not"
2277                                           " supported");
2278         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
2279                 return rte_flow_error_set(error, ENOTSUP,
2280                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
2281                                           "L3 Layer is missing");
2282         if (!mask)
2283                 mask = &rte_flow_item_nvgre_mask;
2284         ret = mlx5_flow_item_acceptable
2285                 (item, (const uint8_t *)mask,
2286                  (const uint8_t *)&rte_flow_item_nvgre_mask,
2287                  sizeof(struct rte_flow_item_nvgre), error);
2288         if (ret < 0)
2289                 return ret;
2290         return 0;
2291 }
2292
2293 /* Allocate unique ID for the split Q/RSS subflows. */
2294 static uint32_t
2295 flow_qrss_get_id(struct rte_eth_dev *dev)
2296 {
2297         struct mlx5_priv *priv = dev->data->dev_private;
2298         uint32_t qrss_id, ret;
2299
2300         ret = mlx5_flow_id_get(priv->qrss_id_pool, &qrss_id);
2301         if (ret)
2302                 return 0;
2303         assert(qrss_id);
2304         return qrss_id;
2305 }
2306
2307 /* Free unique ID for the split Q/RSS subflows. */
2308 static void
2309 flow_qrss_free_id(struct rte_eth_dev *dev,  uint32_t qrss_id)
2310 {
2311         struct mlx5_priv *priv = dev->data->dev_private;
2312
2313         if (qrss_id)
2314                 mlx5_flow_id_release(priv->qrss_id_pool, qrss_id);
2315 }
2316
2317 /**
2318  * Release resource related QUEUE/RSS action split.
2319  *
2320  * @param dev
2321  *   Pointer to Ethernet device.
2322  * @param flow
2323  *   Flow to release id's from.
2324  */
2325 static void
2326 flow_mreg_split_qrss_release(struct rte_eth_dev *dev,
2327                              struct rte_flow *flow)
2328 {
2329         struct mlx5_flow *dev_flow;
2330
2331         LIST_FOREACH(dev_flow, &flow->dev_flows, next)
2332                 if (dev_flow->qrss_id)
2333                         flow_qrss_free_id(dev, dev_flow->qrss_id);
2334 }
2335
2336 static int
2337 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
2338                    const struct rte_flow_attr *attr __rte_unused,
2339                    const struct rte_flow_item items[] __rte_unused,
2340                    const struct rte_flow_action actions[] __rte_unused,
2341                    bool external __rte_unused,
2342                    struct rte_flow_error *error)
2343 {
2344         return rte_flow_error_set(error, ENOTSUP,
2345                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2346 }
2347
2348 static struct mlx5_flow *
2349 flow_null_prepare(const struct rte_flow_attr *attr __rte_unused,
2350                   const struct rte_flow_item items[] __rte_unused,
2351                   const struct rte_flow_action actions[] __rte_unused,
2352                   struct rte_flow_error *error)
2353 {
2354         rte_flow_error_set(error, ENOTSUP,
2355                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2356         return NULL;
2357 }
2358
2359 static int
2360 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
2361                     struct mlx5_flow *dev_flow __rte_unused,
2362                     const struct rte_flow_attr *attr __rte_unused,
2363                     const struct rte_flow_item items[] __rte_unused,
2364                     const struct rte_flow_action actions[] __rte_unused,
2365                     struct rte_flow_error *error)
2366 {
2367         return rte_flow_error_set(error, ENOTSUP,
2368                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2369 }
2370
2371 static int
2372 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
2373                 struct rte_flow *flow __rte_unused,
2374                 struct rte_flow_error *error)
2375 {
2376         return rte_flow_error_set(error, ENOTSUP,
2377                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2378 }
2379
2380 static void
2381 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
2382                  struct rte_flow *flow __rte_unused)
2383 {
2384 }
2385
2386 static void
2387 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
2388                   struct rte_flow *flow __rte_unused)
2389 {
2390 }
2391
2392 static int
2393 flow_null_query(struct rte_eth_dev *dev __rte_unused,
2394                 struct rte_flow *flow __rte_unused,
2395                 const struct rte_flow_action *actions __rte_unused,
2396                 void *data __rte_unused,
2397                 struct rte_flow_error *error)
2398 {
2399         return rte_flow_error_set(error, ENOTSUP,
2400                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, NULL);
2401 }
2402
2403 /* Void driver to protect from null pointer reference. */
2404 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
2405         .validate = flow_null_validate,
2406         .prepare = flow_null_prepare,
2407         .translate = flow_null_translate,
2408         .apply = flow_null_apply,
2409         .remove = flow_null_remove,
2410         .destroy = flow_null_destroy,
2411         .query = flow_null_query,
2412 };
2413
2414 /**
2415  * Select flow driver type according to flow attributes and device
2416  * configuration.
2417  *
2418  * @param[in] dev
2419  *   Pointer to the dev structure.
2420  * @param[in] attr
2421  *   Pointer to the flow attributes.
2422  *
2423  * @return
2424  *   flow driver type, MLX5_FLOW_TYPE_MAX otherwise.
2425  */
2426 static enum mlx5_flow_drv_type
2427 flow_get_drv_type(struct rte_eth_dev *dev, const struct rte_flow_attr *attr)
2428 {
2429         struct mlx5_priv *priv = dev->data->dev_private;
2430         enum mlx5_flow_drv_type type = MLX5_FLOW_TYPE_MAX;
2431
2432         if (attr->transfer && priv->config.dv_esw_en)
2433                 type = MLX5_FLOW_TYPE_DV;
2434         if (!attr->transfer)
2435                 type = priv->config.dv_flow_en ? MLX5_FLOW_TYPE_DV :
2436                                                  MLX5_FLOW_TYPE_VERBS;
2437         return type;
2438 }
2439
2440 #define flow_get_drv_ops(type) flow_drv_ops[type]
2441
2442 /**
2443  * Flow driver validation API. This abstracts calling driver specific functions.
2444  * The type of flow driver is determined according to flow attributes.
2445  *
2446  * @param[in] dev
2447  *   Pointer to the dev structure.
2448  * @param[in] attr
2449  *   Pointer to the flow attributes.
2450  * @param[in] items
2451  *   Pointer to the list of items.
2452  * @param[in] actions
2453  *   Pointer to the list of actions.
2454  * @param[in] external
2455  *   This flow rule is created by request external to PMD.
2456  * @param[out] error
2457  *   Pointer to the error structure.
2458  *
2459  * @return
2460  *   0 on success, a negative errno value otherwise and rte_errno is set.
2461  */
2462 static inline int
2463 flow_drv_validate(struct rte_eth_dev *dev,
2464                   const struct rte_flow_attr *attr,
2465                   const struct rte_flow_item items[],
2466                   const struct rte_flow_action actions[],
2467                   bool external, struct rte_flow_error *error)
2468 {
2469         const struct mlx5_flow_driver_ops *fops;
2470         enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
2471
2472         fops = flow_get_drv_ops(type);
2473         return fops->validate(dev, attr, items, actions, external, error);
2474 }
2475
2476 /**
2477  * Flow driver preparation API. This abstracts calling driver specific
2478  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
2479  * calculates the size of memory required for device flow, allocates the memory,
2480  * initializes the device flow and returns the pointer.
2481  *
2482  * @note
2483  *   This function initializes device flow structure such as dv or verbs in
2484  *   struct mlx5_flow. However, it is caller's responsibility to initialize the
2485  *   rest. For example, adding returning device flow to flow->dev_flow list and
2486  *   setting backward reference to the flow should be done out of this function.
2487  *   layers field is not filled either.
2488  *
2489  * @param[in] attr
2490  *   Pointer to the flow attributes.
2491  * @param[in] items
2492  *   Pointer to the list of items.
2493  * @param[in] actions
2494  *   Pointer to the list of actions.
2495  * @param[out] error
2496  *   Pointer to the error structure.
2497  *
2498  * @return
2499  *   Pointer to device flow on success, otherwise NULL and rte_errno is set.
2500  */
2501 static inline struct mlx5_flow *
2502 flow_drv_prepare(const struct rte_flow *flow,
2503                  const struct rte_flow_attr *attr,
2504                  const struct rte_flow_item items[],
2505                  const struct rte_flow_action actions[],
2506                  struct rte_flow_error *error)
2507 {
2508         const struct mlx5_flow_driver_ops *fops;
2509         enum mlx5_flow_drv_type type = flow->drv_type;
2510
2511         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
2512         fops = flow_get_drv_ops(type);
2513         return fops->prepare(attr, items, actions, error);
2514 }
2515
2516 /**
2517  * Flow driver translation API. This abstracts calling driver specific
2518  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
2519  * translates a generic flow into a driver flow. flow_drv_prepare() must
2520  * precede.
2521  *
2522  * @note
2523  *   dev_flow->layers could be filled as a result of parsing during translation
2524  *   if needed by flow_drv_apply(). dev_flow->flow->actions can also be filled
2525  *   if necessary. As a flow can have multiple dev_flows by RSS flow expansion,
2526  *   flow->actions could be overwritten even though all the expanded dev_flows
2527  *   have the same actions.
2528  *
2529  * @param[in] dev
2530  *   Pointer to the rte dev structure.
2531  * @param[in, out] dev_flow
2532  *   Pointer to the mlx5 flow.
2533  * @param[in] attr
2534  *   Pointer to the flow attributes.
2535  * @param[in] items
2536  *   Pointer to the list of items.
2537  * @param[in] actions
2538  *   Pointer to the list of actions.
2539  * @param[out] error
2540  *   Pointer to the error structure.
2541  *
2542  * @return
2543  *   0 on success, a negative errno value otherwise and rte_errno is set.
2544  */
2545 static inline int
2546 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
2547                    const struct rte_flow_attr *attr,
2548                    const struct rte_flow_item items[],
2549                    const struct rte_flow_action actions[],
2550                    struct rte_flow_error *error)
2551 {
2552         const struct mlx5_flow_driver_ops *fops;
2553         enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
2554
2555         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
2556         fops = flow_get_drv_ops(type);
2557         return fops->translate(dev, dev_flow, attr, items, actions, error);
2558 }
2559
2560 /**
2561  * Flow driver apply API. This abstracts calling driver specific functions.
2562  * Parent flow (rte_flow) should have driver type (drv_type). It applies
2563  * translated driver flows on to device. flow_drv_translate() must precede.
2564  *
2565  * @param[in] dev
2566  *   Pointer to Ethernet device structure.
2567  * @param[in, out] flow
2568  *   Pointer to flow structure.
2569  * @param[out] error
2570  *   Pointer to error structure.
2571  *
2572  * @return
2573  *   0 on success, a negative errno value otherwise and rte_errno is set.
2574  */
2575 static inline int
2576 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
2577                struct rte_flow_error *error)
2578 {
2579         const struct mlx5_flow_driver_ops *fops;
2580         enum mlx5_flow_drv_type type = flow->drv_type;
2581
2582         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
2583         fops = flow_get_drv_ops(type);
2584         return fops->apply(dev, flow, error);
2585 }
2586
2587 /**
2588  * Flow driver remove API. This abstracts calling driver specific functions.
2589  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
2590  * on device. All the resources of the flow should be freed by calling
2591  * flow_drv_destroy().
2592  *
2593  * @param[in] dev
2594  *   Pointer to Ethernet device.
2595  * @param[in, out] flow
2596  *   Pointer to flow structure.
2597  */
2598 static inline void
2599 flow_drv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
2600 {
2601         const struct mlx5_flow_driver_ops *fops;
2602         enum mlx5_flow_drv_type type = flow->drv_type;
2603
2604         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
2605         fops = flow_get_drv_ops(type);
2606         fops->remove(dev, flow);
2607 }
2608
2609 /**
2610  * Flow driver destroy API. This abstracts calling driver specific functions.
2611  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
2612  * on device and releases resources of the flow.
2613  *
2614  * @param[in] dev
2615  *   Pointer to Ethernet device.
2616  * @param[in, out] flow
2617  *   Pointer to flow structure.
2618  */
2619 static inline void
2620 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
2621 {
2622         const struct mlx5_flow_driver_ops *fops;
2623         enum mlx5_flow_drv_type type = flow->drv_type;
2624
2625         flow_mreg_split_qrss_release(dev, flow);
2626         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
2627         fops = flow_get_drv_ops(type);
2628         fops->destroy(dev, flow);
2629 }
2630
2631 /**
2632  * Validate a flow supported by the NIC.
2633  *
2634  * @see rte_flow_validate()
2635  * @see rte_flow_ops
2636  */
2637 int
2638 mlx5_flow_validate(struct rte_eth_dev *dev,
2639                    const struct rte_flow_attr *attr,
2640                    const struct rte_flow_item items[],
2641                    const struct rte_flow_action actions[],
2642                    struct rte_flow_error *error)
2643 {
2644         int ret;
2645
2646         ret = flow_drv_validate(dev, attr, items, actions, true, error);
2647         if (ret < 0)
2648                 return ret;
2649         return 0;
2650 }
2651
2652 /**
2653  * Get port id item from the item list.
2654  *
2655  * @param[in] item
2656  *   Pointer to the list of items.
2657  *
2658  * @return
2659  *   Pointer to the port id item if exist, else return NULL.
2660  */
2661 static const struct rte_flow_item *
2662 find_port_id_item(const struct rte_flow_item *item)
2663 {
2664         assert(item);
2665         for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
2666                 if (item->type == RTE_FLOW_ITEM_TYPE_PORT_ID)
2667                         return item;
2668         }
2669         return NULL;
2670 }
2671
2672 /**
2673  * Get RSS action from the action list.
2674  *
2675  * @param[in] actions
2676  *   Pointer to the list of actions.
2677  *
2678  * @return
2679  *   Pointer to the RSS action if exist, else return NULL.
2680  */
2681 static const struct rte_flow_action_rss*
2682 flow_get_rss_action(const struct rte_flow_action actions[])
2683 {
2684         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2685                 switch (actions->type) {
2686                 case RTE_FLOW_ACTION_TYPE_RSS:
2687                         return (const struct rte_flow_action_rss *)
2688                                actions->conf;
2689                 default:
2690                         break;
2691                 }
2692         }
2693         return NULL;
2694 }
2695
2696 static unsigned int
2697 find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
2698 {
2699         const struct rte_flow_item *item;
2700         unsigned int has_vlan = 0;
2701
2702         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
2703                 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
2704                         has_vlan = 1;
2705                         break;
2706                 }
2707         }
2708         if (has_vlan)
2709                 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
2710                                        MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
2711         return rss_level < 2 ? MLX5_EXPANSION_ROOT :
2712                                MLX5_EXPANSION_ROOT_OUTER;
2713 }
2714
2715 /**
2716  * Get QUEUE/RSS action from the action list.
2717  *
2718  * @param[in] actions
2719  *   Pointer to the list of actions.
2720  * @param[out] qrss
2721  *   Pointer to the return pointer.
2722  * @param[out] qrss_type
2723  *   Pointer to the action type to return. RTE_FLOW_ACTION_TYPE_END is returned
2724  *   if no QUEUE/RSS is found.
2725  *
2726  * @return
2727  *   Total number of actions.
2728  */
2729 static int
2730 flow_parse_qrss_action(const struct rte_flow_action actions[],
2731                        const struct rte_flow_action **qrss)
2732 {
2733         int actions_n = 0;
2734
2735         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2736                 switch (actions->type) {
2737                 case RTE_FLOW_ACTION_TYPE_QUEUE:
2738                 case RTE_FLOW_ACTION_TYPE_RSS:
2739                         *qrss = actions;
2740                         break;
2741                 default:
2742                         break;
2743                 }
2744                 actions_n++;
2745         }
2746         /* Count RTE_FLOW_ACTION_TYPE_END. */
2747         return actions_n + 1;
2748 }
2749
2750 /**
2751  * Check meter action from the action list.
2752  *
2753  * @param[in] actions
2754  *   Pointer to the list of actions.
2755  * @param[out] mtr
2756  *   Pointer to the meter exist flag.
2757  *
2758  * @return
2759  *   Total number of actions.
2760  */
2761 static int
2762 flow_check_meter_action(const struct rte_flow_action actions[], uint32_t *mtr)
2763 {
2764         int actions_n = 0;
2765
2766         assert(mtr);
2767         *mtr = 0;
2768         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2769                 switch (actions->type) {
2770                 case RTE_FLOW_ACTION_TYPE_METER:
2771                         *mtr = 1;
2772                         break;
2773                 default:
2774                         break;
2775                 }
2776                 actions_n++;
2777         }
2778         /* Count RTE_FLOW_ACTION_TYPE_END. */
2779         return actions_n + 1;
2780 }
2781
2782 /**
2783  * Check if the flow should be splited due to hairpin.
2784  * The reason for the split is that in current HW we can't
2785  * support encap on Rx, so if a flow have encap we move it
2786  * to Tx.
2787  *
2788  * @param dev
2789  *   Pointer to Ethernet device.
2790  * @param[in] attr
2791  *   Flow rule attributes.
2792  * @param[in] actions
2793  *   Associated actions (list terminated by the END action).
2794  *
2795  * @return
2796  *   > 0 the number of actions and the flow should be split,
2797  *   0 when no split required.
2798  */
2799 static int
2800 flow_check_hairpin_split(struct rte_eth_dev *dev,
2801                          const struct rte_flow_attr *attr,
2802                          const struct rte_flow_action actions[])
2803 {
2804         int queue_action = 0;
2805         int action_n = 0;
2806         int encap = 0;
2807         const struct rte_flow_action_queue *queue;
2808         const struct rte_flow_action_rss *rss;
2809         const struct rte_flow_action_raw_encap *raw_encap;
2810
2811         if (!attr->ingress)
2812                 return 0;
2813         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2814                 switch (actions->type) {
2815                 case RTE_FLOW_ACTION_TYPE_QUEUE:
2816                         queue = actions->conf;
2817                         if (mlx5_rxq_get_type(dev, queue->index) !=
2818                             MLX5_RXQ_TYPE_HAIRPIN)
2819                                 return 0;
2820                         queue_action = 1;
2821                         action_n++;
2822                         break;
2823                 case RTE_FLOW_ACTION_TYPE_RSS:
2824                         rss = actions->conf;
2825                         if (mlx5_rxq_get_type(dev, rss->queue[0]) !=
2826                             MLX5_RXQ_TYPE_HAIRPIN)
2827                                 return 0;
2828                         queue_action = 1;
2829                         action_n++;
2830                         break;
2831                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
2832                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
2833                         encap = 1;
2834                         action_n++;
2835                         break;
2836                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
2837                         raw_encap = actions->conf;
2838                         if (raw_encap->size >
2839                             (sizeof(struct rte_flow_item_eth) +
2840                              sizeof(struct rte_flow_item_ipv4)))
2841                                 encap = 1;
2842                         action_n++;
2843                         break;
2844                 default:
2845                         action_n++;
2846                         break;
2847                 }
2848         }
2849         if (encap == 1 && queue_action)
2850                 return action_n;
2851         return 0;
2852 }
2853
2854 /* Declare flow create/destroy prototype in advance. */
2855 static struct rte_flow *
2856 flow_list_create(struct rte_eth_dev *dev, struct mlx5_flows *list,
2857                  const struct rte_flow_attr *attr,
2858                  const struct rte_flow_item items[],
2859                  const struct rte_flow_action actions[],
2860                  bool external, struct rte_flow_error *error);
2861
2862 static void
2863 flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2864                   struct rte_flow *flow);
2865
2866 /**
2867  * Add a flow of copying flow metadata registers in RX_CP_TBL.
2868  *
2869  * As mark_id is unique, if there's already a registered flow for the mark_id,
2870  * return by increasing the reference counter of the resource. Otherwise, create
2871  * the resource (mcp_res) and flow.
2872  *
2873  * Flow looks like,
2874  *   - If ingress port is ANY and reg_c[1] is mark_id,
2875  *     flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
2876  *
2877  * For default flow (zero mark_id), flow is like,
2878  *   - If ingress port is ANY,
2879  *     reg_b := reg_c[0] and jump to RX_ACT_TBL.
2880  *
2881  * @param dev
2882  *   Pointer to Ethernet device.
2883  * @param mark_id
2884  *   ID of MARK action, zero means default flow for META.
2885  * @param[out] error
2886  *   Perform verbose error reporting if not NULL.
2887  *
2888  * @return
2889  *   Associated resource on success, NULL otherwise and rte_errno is set.
2890  */
2891 static struct mlx5_flow_mreg_copy_resource *
2892 flow_mreg_add_copy_action(struct rte_eth_dev *dev, uint32_t mark_id,
2893                           struct rte_flow_error *error)
2894 {
2895         struct mlx5_priv *priv = dev->data->dev_private;
2896         struct rte_flow_attr attr = {
2897                 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
2898                 .ingress = 1,
2899         };
2900         struct mlx5_rte_flow_item_tag tag_spec = {
2901                 .data = mark_id,
2902         };
2903         struct rte_flow_item items[] = {
2904                 [1] = { .type = RTE_FLOW_ITEM_TYPE_END, },
2905         };
2906         struct rte_flow_action_mark ftag = {
2907                 .id = mark_id,
2908         };
2909         struct mlx5_flow_action_copy_mreg cp_mreg = {
2910                 .dst = REG_B,
2911                 .src = 0,
2912         };
2913         struct rte_flow_action_jump jump = {
2914                 .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
2915         };
2916         struct rte_flow_action actions[] = {
2917                 [3] = { .type = RTE_FLOW_ACTION_TYPE_END, },
2918         };
2919         struct mlx5_flow_mreg_copy_resource *mcp_res;
2920         int ret;
2921
2922         /* Fill the register fileds in the flow. */
2923         ret = mlx5_flow_get_reg_id(dev, MLX5_FLOW_MARK, 0, error);
2924         if (ret < 0)
2925                 return NULL;
2926         tag_spec.id = ret;
2927         ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
2928         if (ret < 0)
2929                 return NULL;
2930         cp_mreg.src = ret;
2931         /* Check if already registered. */
2932         assert(priv->mreg_cp_tbl);
2933         mcp_res = (void *)mlx5_hlist_lookup(priv->mreg_cp_tbl, mark_id);
2934         if (mcp_res) {
2935                 /* For non-default rule. */
2936                 if (mark_id)
2937                         mcp_res->refcnt++;
2938                 assert(mark_id || mcp_res->refcnt == 1);
2939                 return mcp_res;
2940         }
2941         /* Provide the full width of FLAG specific value. */
2942         if (mark_id == (priv->sh->dv_regc0_mask & MLX5_FLOW_MARK_DEFAULT))
2943                 tag_spec.data = MLX5_FLOW_MARK_DEFAULT;
2944         /* Build a new flow. */
2945         if (mark_id) {
2946                 items[0] = (struct rte_flow_item){
2947                         .type = MLX5_RTE_FLOW_ITEM_TYPE_TAG,
2948                         .spec = &tag_spec,
2949                 };
2950                 items[1] = (struct rte_flow_item){
2951                         .type = RTE_FLOW_ITEM_TYPE_END,
2952                 };
2953                 actions[0] = (struct rte_flow_action){
2954                         .type = MLX5_RTE_FLOW_ACTION_TYPE_MARK,
2955                         .conf = &ftag,
2956                 };
2957                 actions[1] = (struct rte_flow_action){
2958                         .type = MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
2959                         .conf = &cp_mreg,
2960                 };
2961                 actions[2] = (struct rte_flow_action){
2962                         .type = RTE_FLOW_ACTION_TYPE_JUMP,
2963                         .conf = &jump,
2964                 };
2965                 actions[3] = (struct rte_flow_action){
2966                         .type = RTE_FLOW_ACTION_TYPE_END,
2967                 };
2968         } else {
2969                 /* Default rule, wildcard match. */
2970                 attr.priority = MLX5_FLOW_PRIO_RSVD;
2971                 items[0] = (struct rte_flow_item){
2972                         .type = RTE_FLOW_ITEM_TYPE_END,
2973                 };
2974                 actions[0] = (struct rte_flow_action){
2975                         .type = MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
2976                         .conf = &cp_mreg,
2977                 };
2978                 actions[1] = (struct rte_flow_action){
2979                         .type = RTE_FLOW_ACTION_TYPE_JUMP,
2980                         .conf = &jump,
2981                 };
2982                 actions[2] = (struct rte_flow_action){
2983                         .type = RTE_FLOW_ACTION_TYPE_END,
2984                 };
2985         }
2986         /* Build a new entry. */
2987         mcp_res = rte_zmalloc(__func__, sizeof(*mcp_res), 0);
2988         if (!mcp_res) {
2989                 rte_errno = ENOMEM;
2990                 return NULL;
2991         }
2992         /*
2993          * The copy Flows are not included in any list. There
2994          * ones are referenced from other Flows and can not
2995          * be applied, removed, deleted in ardbitrary order
2996          * by list traversing.
2997          */
2998         mcp_res->flow = flow_list_create(dev, NULL, &attr, items,
2999                                          actions, false, error);
3000         if (!mcp_res->flow)
3001                 goto error;
3002         mcp_res->refcnt++;
3003         mcp_res->hlist_ent.key = mark_id;
3004         ret = mlx5_hlist_insert(priv->mreg_cp_tbl,
3005                                 &mcp_res->hlist_ent);
3006         assert(!ret);
3007         if (ret)
3008                 goto error;
3009         return mcp_res;
3010 error:
3011         if (mcp_res->flow)
3012                 flow_list_destroy(dev, NULL, mcp_res->flow);
3013         rte_free(mcp_res);
3014         return NULL;
3015 }
3016
3017 /**
3018  * Release flow in RX_CP_TBL.
3019  *
3020  * @param dev
3021  *   Pointer to Ethernet device.
3022  * @flow
3023  *   Parent flow for wich copying is provided.
3024  */
3025 static void
3026 flow_mreg_del_copy_action(struct rte_eth_dev *dev,
3027                           struct rte_flow *flow)
3028 {
3029         struct mlx5_flow_mreg_copy_resource *mcp_res = flow->mreg_copy;
3030         struct mlx5_priv *priv = dev->data->dev_private;
3031
3032         if (!mcp_res || !priv->mreg_cp_tbl)
3033                 return;
3034         if (flow->copy_applied) {
3035                 assert(mcp_res->appcnt);
3036                 flow->copy_applied = 0;
3037                 --mcp_res->appcnt;
3038                 if (!mcp_res->appcnt)
3039                         flow_drv_remove(dev, mcp_res->flow);
3040         }
3041         /*
3042          * We do not check availability of metadata registers here,
3043          * because copy resources are allocated in this case.
3044          */
3045         if (--mcp_res->refcnt)
3046                 return;
3047         assert(mcp_res->flow);
3048         flow_list_destroy(dev, NULL, mcp_res->flow);
3049         mlx5_hlist_remove(priv->mreg_cp_tbl, &mcp_res->hlist_ent);
3050         rte_free(mcp_res);
3051         flow->mreg_copy = NULL;
3052 }
3053
3054 /**
3055  * Start flow in RX_CP_TBL.
3056  *
3057  * @param dev
3058  *   Pointer to Ethernet device.
3059  * @flow
3060  *   Parent flow for wich copying is provided.
3061  *
3062  * @return
3063  *   0 on success, a negative errno value otherwise and rte_errno is set.
3064  */
3065 static int
3066 flow_mreg_start_copy_action(struct rte_eth_dev *dev,
3067                             struct rte_flow *flow)
3068 {
3069         struct mlx5_flow_mreg_copy_resource *mcp_res = flow->mreg_copy;
3070         int ret;
3071
3072         if (!mcp_res || flow->copy_applied)
3073                 return 0;
3074         if (!mcp_res->appcnt) {
3075                 ret = flow_drv_apply(dev, mcp_res->flow, NULL);
3076                 if (ret)
3077                         return ret;
3078         }
3079         ++mcp_res->appcnt;
3080         flow->copy_applied = 1;
3081         return 0;
3082 }
3083
3084 /**
3085  * Stop flow in RX_CP_TBL.
3086  *
3087  * @param dev
3088  *   Pointer to Ethernet device.
3089  * @flow
3090  *   Parent flow for wich copying is provided.
3091  */
3092 static void
3093 flow_mreg_stop_copy_action(struct rte_eth_dev *dev,
3094                            struct rte_flow *flow)
3095 {
3096         struct mlx5_flow_mreg_copy_resource *mcp_res = flow->mreg_copy;
3097
3098         if (!mcp_res || !flow->copy_applied)
3099                 return;
3100         assert(mcp_res->appcnt);
3101         --mcp_res->appcnt;
3102         flow->copy_applied = 0;
3103         if (!mcp_res->appcnt)
3104                 flow_drv_remove(dev, mcp_res->flow);
3105 }
3106
3107 /**
3108  * Remove the default copy action from RX_CP_TBL.
3109  *
3110  * @param dev
3111  *   Pointer to Ethernet device.
3112  */
3113 static void
3114 flow_mreg_del_default_copy_action(struct rte_eth_dev *dev)
3115 {
3116         struct mlx5_flow_mreg_copy_resource *mcp_res;
3117         struct mlx5_priv *priv = dev->data->dev_private;
3118
3119         /* Check if default flow is registered. */
3120         if (!priv->mreg_cp_tbl)
3121                 return;
3122         mcp_res = (void *)mlx5_hlist_lookup(priv->mreg_cp_tbl, 0ULL);
3123         if (!mcp_res)
3124                 return;
3125         assert(mcp_res->flow);
3126         flow_list_destroy(dev, NULL, mcp_res->flow);
3127         mlx5_hlist_remove(priv->mreg_cp_tbl, &mcp_res->hlist_ent);
3128         rte_free(mcp_res);
3129 }
3130
3131 /**
3132  * Add the default copy action in in RX_CP_TBL.
3133  *
3134  * @param dev
3135  *   Pointer to Ethernet device.
3136  * @param[out] error
3137  *   Perform verbose error reporting if not NULL.
3138  *
3139  * @return
3140  *   0 for success, negative value otherwise and rte_errno is set.
3141  */
3142 static int
3143 flow_mreg_add_default_copy_action(struct rte_eth_dev *dev,
3144                                   struct rte_flow_error *error)
3145 {
3146         struct mlx5_priv *priv = dev->data->dev_private;
3147         struct mlx5_flow_mreg_copy_resource *mcp_res;
3148
3149         /* Check whether extensive metadata feature is engaged. */
3150         if (!priv->config.dv_flow_en ||
3151             priv->config.dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
3152             !mlx5_flow_ext_mreg_supported(dev) ||
3153             !priv->sh->dv_regc0_mask)
3154                 return 0;
3155         mcp_res = flow_mreg_add_copy_action(dev, 0, error);
3156         if (!mcp_res)
3157                 return -rte_errno;
3158         return 0;
3159 }
3160
3161 /**
3162  * Add a flow of copying flow metadata registers in RX_CP_TBL.
3163  *
3164  * All the flow having Q/RSS action should be split by
3165  * flow_mreg_split_qrss_prep() to pass by RX_CP_TBL. A flow in the RX_CP_TBL
3166  * performs the following,
3167  *   - CQE->flow_tag := reg_c[1] (MARK)
3168  *   - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
3169  * As CQE's flow_tag is not a register, it can't be simply copied from reg_c[1]
3170  * but there should be a flow per each MARK ID set by MARK action.
3171  *
3172  * For the aforementioned reason, if there's a MARK action in flow's action
3173  * list, a corresponding flow should be added to the RX_CP_TBL in order to copy
3174  * the MARK ID to CQE's flow_tag like,
3175  *   - If reg_c[1] is mark_id,
3176  *     flow_tag := mark_id, reg_b := reg_c[0] and jump to RX_ACT_TBL.
3177  *
3178  * For SET_META action which stores value in reg_c[0], as the destination is
3179  * also a flow metadata register (reg_b), adding a default flow is enough. Zero
3180  * MARK ID means the default flow. The default flow looks like,
3181  *   - For all flow, reg_b := reg_c[0] and jump to RX_ACT_TBL.
3182  *
3183  * @param dev
3184  *   Pointer to Ethernet device.
3185  * @param flow
3186  *   Pointer to flow structure.
3187  * @param[in] actions
3188  *   Pointer to the list of actions.
3189  * @param[out] error
3190  *   Perform verbose error reporting if not NULL.
3191  *
3192  * @return
3193  *   0 on success, negative value otherwise and rte_errno is set.
3194  */
3195 static int
3196 flow_mreg_update_copy_table(struct rte_eth_dev *dev,
3197                             struct rte_flow *flow,
3198                             const struct rte_flow_action *actions,
3199                             struct rte_flow_error *error)
3200 {
3201         struct mlx5_priv *priv = dev->data->dev_private;
3202         struct mlx5_dev_config *config = &priv->config;
3203         struct mlx5_flow_mreg_copy_resource *mcp_res;
3204         const struct rte_flow_action_mark *mark;
3205
3206         /* Check whether extensive metadata feature is engaged. */
3207         if (!config->dv_flow_en ||
3208             config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
3209             !mlx5_flow_ext_mreg_supported(dev) ||
3210             !priv->sh->dv_regc0_mask)
3211                 return 0;
3212         /* Find MARK action. */
3213         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3214                 switch (actions->type) {
3215                 case RTE_FLOW_ACTION_TYPE_FLAG:
3216                         mcp_res = flow_mreg_add_copy_action
3217                                 (dev, MLX5_FLOW_MARK_DEFAULT, error);
3218                         if (!mcp_res)
3219                                 return -rte_errno;
3220                         flow->mreg_copy = mcp_res;
3221                         if (dev->data->dev_started) {
3222                                 mcp_res->appcnt++;
3223                                 flow->copy_applied = 1;
3224                         }
3225                         return 0;
3226                 case RTE_FLOW_ACTION_TYPE_MARK:
3227                         mark = (const struct rte_flow_action_mark *)
3228                                 actions->conf;
3229                         mcp_res =
3230                                 flow_mreg_add_copy_action(dev, mark->id, error);
3231                         if (!mcp_res)
3232                                 return -rte_errno;
3233                         flow->mreg_copy = mcp_res;
3234                         if (dev->data->dev_started) {
3235                                 mcp_res->appcnt++;
3236                                 flow->copy_applied = 1;
3237                         }
3238                         return 0;
3239                 default:
3240                         break;
3241                 }
3242         }
3243         return 0;
3244 }
3245
3246 #define MLX5_MAX_SPLIT_ACTIONS 24
3247 #define MLX5_MAX_SPLIT_ITEMS 24
3248
3249 /**
3250  * Split the hairpin flow.
3251  * Since HW can't support encap on Rx we move the encap to Tx.
3252  * If the count action is after the encap then we also
3253  * move the count action. in this case the count will also measure
3254  * the outer bytes.
3255  *
3256  * @param dev
3257  *   Pointer to Ethernet device.
3258  * @param[in] actions
3259  *   Associated actions (list terminated by the END action).
3260  * @param[out] actions_rx
3261  *   Rx flow actions.
3262  * @param[out] actions_tx
3263  *   Tx flow actions..
3264  * @param[out] pattern_tx
3265  *   The pattern items for the Tx flow.
3266  * @param[out] flow_id
3267  *   The flow ID connected to this flow.
3268  *
3269  * @return
3270  *   0 on success.
3271  */
3272 static int
3273 flow_hairpin_split(struct rte_eth_dev *dev,
3274                    const struct rte_flow_action actions[],
3275                    struct rte_flow_action actions_rx[],
3276                    struct rte_flow_action actions_tx[],
3277                    struct rte_flow_item pattern_tx[],
3278                    uint32_t *flow_id)
3279 {
3280         struct mlx5_priv *priv = dev->data->dev_private;
3281         const struct rte_flow_action_raw_encap *raw_encap;
3282         const struct rte_flow_action_raw_decap *raw_decap;
3283         struct mlx5_rte_flow_action_set_tag *set_tag;
3284         struct rte_flow_action *tag_action;
3285         struct mlx5_rte_flow_item_tag *tag_item;
3286         struct rte_flow_item *item;
3287         char *addr;
3288         int encap = 0;
3289
3290         mlx5_flow_id_get(priv->sh->flow_id_pool, flow_id);
3291         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3292                 switch (actions->type) {
3293                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3294                 case RTE_FLOW_ACTION_TYPE_NVGRE_ENCAP:
3295                         rte_memcpy(actions_tx, actions,
3296                                sizeof(struct rte_flow_action));
3297                         actions_tx++;
3298                         break;
3299                 case RTE_FLOW_ACTION_TYPE_COUNT:
3300                         if (encap) {
3301                                 rte_memcpy(actions_tx, actions,
3302                                            sizeof(struct rte_flow_action));
3303                                 actions_tx++;
3304                         } else {
3305                                 rte_memcpy(actions_rx, actions,
3306                                            sizeof(struct rte_flow_action));
3307                                 actions_rx++;
3308                         }
3309                         break;
3310                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3311                         raw_encap = actions->conf;
3312                         if (raw_encap->size >
3313                             (sizeof(struct rte_flow_item_eth) +
3314                              sizeof(struct rte_flow_item_ipv4))) {
3315                                 memcpy(actions_tx, actions,
3316                                        sizeof(struct rte_flow_action));
3317                                 actions_tx++;
3318                                 encap = 1;
3319                         } else {
3320                                 rte_memcpy(actions_rx, actions,
3321                                            sizeof(struct rte_flow_action));
3322                                 actions_rx++;
3323                         }
3324                         break;
3325                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3326                         raw_decap = actions->conf;
3327                         if (raw_decap->size <
3328                             (sizeof(struct rte_flow_item_eth) +
3329                              sizeof(struct rte_flow_item_ipv4))) {
3330                                 memcpy(actions_tx, actions,
3331                                        sizeof(struct rte_flow_action));
3332                                 actions_tx++;
3333                         } else {
3334                                 rte_memcpy(actions_rx, actions,
3335                                            sizeof(struct rte_flow_action));
3336                                 actions_rx++;
3337                         }
3338                         break;
3339                 default:
3340                         rte_memcpy(actions_rx, actions,
3341                                    sizeof(struct rte_flow_action));
3342                         actions_rx++;
3343                         break;
3344                 }
3345         }
3346         /* Add set meta action and end action for the Rx flow. */
3347         tag_action = actions_rx;
3348         tag_action->type = MLX5_RTE_FLOW_ACTION_TYPE_TAG;
3349         actions_rx++;
3350         rte_memcpy(actions_rx, actions, sizeof(struct rte_flow_action));
3351         actions_rx++;
3352         set_tag = (void *)actions_rx;
3353         set_tag->id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_RX, 0, NULL);
3354         assert(set_tag->id > REG_NONE);
3355         set_tag->data = *flow_id;
3356         tag_action->conf = set_tag;
3357         /* Create Tx item list. */
3358         rte_memcpy(actions_tx, actions, sizeof(struct rte_flow_action));
3359         addr = (void *)&pattern_tx[2];
3360         item = pattern_tx;
3361         item->type = MLX5_RTE_FLOW_ITEM_TYPE_TAG;
3362         tag_item = (void *)addr;
3363         tag_item->data = *flow_id;
3364         tag_item->id = mlx5_flow_get_reg_id(dev, MLX5_HAIRPIN_TX, 0, NULL);
3365         assert(set_tag->id > REG_NONE);
3366         item->spec = tag_item;
3367         addr += sizeof(struct mlx5_rte_flow_item_tag);
3368         tag_item = (void *)addr;
3369         tag_item->data = UINT32_MAX;
3370         tag_item->id = UINT16_MAX;
3371         item->mask = tag_item;
3372         addr += sizeof(struct mlx5_rte_flow_item_tag);
3373         item->last = NULL;
3374         item++;
3375         item->type = RTE_FLOW_ITEM_TYPE_END;
3376         return 0;
3377 }
3378
3379 /**
3380  * The last stage of splitting chain, just creates the subflow
3381  * without any modification.
3382  *
3383  * @param dev
3384  *   Pointer to Ethernet device.
3385  * @param[in] flow
3386  *   Parent flow structure pointer.
3387  * @param[in, out] sub_flow
3388  *   Pointer to return the created subflow, may be NULL.
3389  * @param[in] attr
3390  *   Flow rule attributes.
3391  * @param[in] items
3392  *   Pattern specification (list terminated by the END pattern item).
3393  * @param[in] actions
3394  *   Associated actions (list terminated by the END action).
3395  * @param[in] external
3396  *   This flow rule is created by request external to PMD.
3397  * @param[out] error
3398  *   Perform verbose error reporting if not NULL.
3399  * @return
3400  *   0 on success, negative value otherwise
3401  */
3402 static int
3403 flow_create_split_inner(struct rte_eth_dev *dev,
3404                         struct rte_flow *flow,
3405                         struct mlx5_flow **sub_flow,
3406                         const struct rte_flow_attr *attr,
3407                         const struct rte_flow_item items[],
3408                         const struct rte_flow_action actions[],
3409                         bool external, struct rte_flow_error *error)
3410 {
3411         struct mlx5_flow *dev_flow;
3412
3413         dev_flow = flow_drv_prepare(flow, attr, items, actions, error);
3414         if (!dev_flow)
3415                 return -rte_errno;
3416         dev_flow->flow = flow;
3417         dev_flow->external = external;
3418         /* Subflow object was created, we must include one in the list. */
3419         LIST_INSERT_HEAD(&flow->dev_flows, dev_flow, next);
3420         if (sub_flow)
3421                 *sub_flow = dev_flow;
3422         return flow_drv_translate(dev, dev_flow, attr, items, actions, error);
3423 }
3424
3425 /**
3426  * Split the meter flow.
3427  *
3428  * As meter flow will split to three sub flow, other than meter
3429  * action, the other actions make sense to only meter accepts
3430  * the packet. If it need to be dropped, no other additional
3431  * actions should be take.
3432  *
3433  * One kind of special action which decapsulates the L3 tunnel
3434  * header will be in the prefix sub flow, as not to take the
3435  * L3 tunnel header into account.
3436  *
3437  * @param dev
3438  *   Pointer to Ethernet device.
3439  * @param[in] actions
3440  *   Associated actions (list terminated by the END action).
3441  * @param[out] actions_sfx
3442  *   Suffix flow actions.
3443  * @param[out] actions_pre
3444  *   Prefix flow actions.
3445  * @param[out] pattern_sfx
3446  *   The pattern items for the suffix flow.
3447  * @param[out] tag_sfx
3448  *   Pointer to suffix flow tag.
3449  *
3450  * @return
3451  *   0 on success.
3452  */
3453 static int
3454 flow_meter_split_prep(struct rte_eth_dev *dev,
3455                  const struct rte_flow_action actions[],
3456                  struct rte_flow_action actions_sfx[],
3457                  struct rte_flow_action actions_pre[])
3458 {
3459         struct rte_flow_action *tag_action;
3460         struct mlx5_rte_flow_action_set_tag *set_tag;
3461         struct rte_flow_error error;
3462         const struct rte_flow_action_raw_encap *raw_encap;
3463         const struct rte_flow_action_raw_decap *raw_decap;
3464         uint32_t tag_id;
3465
3466         /* Add the extra tag action first. */
3467         tag_action = actions_pre;
3468         tag_action->type = MLX5_RTE_FLOW_ACTION_TYPE_TAG;
3469         actions_pre++;
3470         /* Prepare the actions for prefix and suffix flow. */
3471         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3472                 switch (actions->type) {
3473                 case RTE_FLOW_ACTION_TYPE_METER:
3474                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
3475                 case RTE_FLOW_ACTION_TYPE_NVGRE_DECAP:
3476                         memcpy(actions_pre, actions,
3477                                sizeof(struct rte_flow_action));
3478                         actions_pre++;
3479                         break;
3480                 case RTE_FLOW_ACTION_TYPE_RAW_ENCAP:
3481                         raw_encap = actions->conf;
3482                         if (raw_encap->size >
3483                             (sizeof(struct rte_flow_item_eth) +
3484                              sizeof(struct rte_flow_item_ipv4))) {
3485                                 memcpy(actions_sfx, actions,
3486                                        sizeof(struct rte_flow_action));
3487                                 actions_sfx++;
3488                         } else {
3489                                 rte_memcpy(actions_pre, actions,
3490                                            sizeof(struct rte_flow_action));
3491                                 actions_pre++;
3492                         }
3493                         break;
3494                 case RTE_FLOW_ACTION_TYPE_RAW_DECAP:
3495                         raw_decap = actions->conf;
3496                         /* Size 0 decap means 50 bytes as vxlan decap. */
3497                         if (raw_decap->size && (raw_decap->size <
3498                             (sizeof(struct rte_flow_item_eth) +
3499                              sizeof(struct rte_flow_item_ipv4)))) {
3500                                 memcpy(actions_sfx, actions,
3501                                        sizeof(struct rte_flow_action));
3502                                 actions_sfx++;
3503                         } else {
3504                                 rte_memcpy(actions_pre, actions,
3505                                            sizeof(struct rte_flow_action));
3506                                 actions_pre++;
3507                         }
3508                         break;
3509                 default:
3510                         memcpy(actions_sfx, actions,
3511                                 sizeof(struct rte_flow_action));
3512                         actions_sfx++;
3513                         break;
3514                 }
3515         }
3516         /* Add end action to the actions. */
3517         actions_sfx->type = RTE_FLOW_ACTION_TYPE_END;
3518         actions_pre->type = RTE_FLOW_ACTION_TYPE_END;
3519         actions_pre++;
3520         /* Set the tag. */
3521         set_tag = (void *)actions_pre;
3522         set_tag->id = mlx5_flow_get_reg_id(dev, MLX5_MTR_SFX, 0, &error);
3523         /*
3524          * Get the id from the qrss_pool to make qrss share the id with meter.
3525          */
3526         tag_id = flow_qrss_get_id(dev);
3527         set_tag->data = rte_cpu_to_be_32(tag_id);
3528         tag_action->conf = set_tag;
3529         return tag_id;
3530 }
3531
3532 /**
3533  * Split action list having QUEUE/RSS for metadata register copy.
3534  *
3535  * Once Q/RSS action is detected in user's action list, the flow action
3536  * should be split in order to copy metadata registers, which will happen in
3537  * RX_CP_TBL like,
3538  *   - CQE->flow_tag := reg_c[1] (MARK)
3539  *   - CQE->flow_table_metadata (reg_b) := reg_c[0] (META)
3540  * The Q/RSS action will be performed on RX_ACT_TBL after passing by RX_CP_TBL.
3541  * This is because the last action of each flow must be a terminal action
3542  * (QUEUE, RSS or DROP).
3543  *
3544  * Flow ID must be allocated to identify actions in the RX_ACT_TBL and it is
3545  * stored and kept in the mlx5_flow structure per each sub_flow.
3546  *
3547  * The Q/RSS action is replaced with,
3548  *   - SET_TAG, setting the allocated flow ID to reg_c[2].
3549  * And the following JUMP action is added at the end,
3550  *   - JUMP, to RX_CP_TBL.
3551  *
3552  * A flow to perform remained Q/RSS action will be created in RX_ACT_TBL by
3553  * flow_create_split_metadata() routine. The flow will look like,
3554  *   - If flow ID matches (reg_c[2]), perform Q/RSS.
3555  *
3556  * @param dev
3557  *   Pointer to Ethernet device.
3558  * @param[out] split_actions
3559  *   Pointer to store split actions to jump to CP_TBL.
3560  * @param[in] actions
3561  *   Pointer to the list of original flow actions.
3562  * @param[in] qrss
3563  *   Pointer to the Q/RSS action.
3564  * @param[in] actions_n
3565  *   Number of original actions.
3566  * @param[out] error
3567  *   Perform verbose error reporting if not NULL.
3568  *
3569  * @return
3570  *   non-zero unique flow_id on success, otherwise 0 and
3571  *   error/rte_error are set.
3572  */
3573 static uint32_t
3574 flow_mreg_split_qrss_prep(struct rte_eth_dev *dev,
3575                           struct rte_flow_action *split_actions,
3576                           const struct rte_flow_action *actions,
3577                           const struct rte_flow_action *qrss,
3578                           int actions_n, struct rte_flow_error *error)
3579 {
3580         struct mlx5_rte_flow_action_set_tag *set_tag;
3581         struct rte_flow_action_jump *jump;
3582         const int qrss_idx = qrss - actions;
3583         uint32_t flow_id = 0;
3584         int ret = 0;
3585
3586         /*
3587          * Given actions will be split
3588          * - Replace QUEUE/RSS action with SET_TAG to set flow ID.
3589          * - Add jump to mreg CP_TBL.
3590          * As a result, there will be one more action.
3591          */
3592         ++actions_n;
3593         memcpy(split_actions, actions, sizeof(*split_actions) * actions_n);
3594         set_tag = (void *)(split_actions + actions_n);
3595         /*
3596          * If tag action is not set to void(it means we are not the meter
3597          * suffix flow), add the tag action. Since meter suffix flow already
3598          * has the tag added.
3599          */
3600         if (split_actions[qrss_idx].type != RTE_FLOW_ACTION_TYPE_VOID) {
3601                 /*
3602                  * Allocate the new subflow ID. This one is unique within
3603                  * device and not shared with representors. Otherwise,
3604                  * we would have to resolve multi-thread access synch
3605                  * issue. Each flow on the shared device is appended
3606                  * with source vport identifier, so the resulting
3607                  * flows will be unique in the shared (by master and
3608                  * representors) domain even if they have coinciding
3609                  * IDs.
3610                  */
3611                 flow_id = flow_qrss_get_id(dev);
3612                 if (!flow_id)
3613                         return rte_flow_error_set(error, ENOMEM,
3614                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3615                                                   NULL, "can't allocate id "
3616                                                   "for split Q/RSS subflow");
3617                 /* Internal SET_TAG action to set flow ID. */
3618                 *set_tag = (struct mlx5_rte_flow_action_set_tag){
3619                         .data = flow_id,
3620                 };
3621                 ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0, error);
3622                 if (ret < 0)
3623                         return ret;
3624                 set_tag->id = ret;
3625                 /* Construct new actions array. */
3626                 /* Replace QUEUE/RSS action. */
3627                 split_actions[qrss_idx] = (struct rte_flow_action){
3628                         .type = MLX5_RTE_FLOW_ACTION_TYPE_TAG,
3629                         .conf = set_tag,
3630                 };
3631         }
3632         /* JUMP action to jump to mreg copy table (CP_TBL). */
3633         jump = (void *)(set_tag + 1);
3634         *jump = (struct rte_flow_action_jump){
3635                 .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
3636         };
3637         split_actions[actions_n - 2] = (struct rte_flow_action){
3638                 .type = RTE_FLOW_ACTION_TYPE_JUMP,
3639                 .conf = jump,
3640         };
3641         split_actions[actions_n - 1] = (struct rte_flow_action){
3642                 .type = RTE_FLOW_ACTION_TYPE_END,
3643         };
3644         return flow_id;
3645 }
3646
3647 /**
3648  * Extend the given action list for Tx metadata copy.
3649  *
3650  * Copy the given action list to the ext_actions and add flow metadata register
3651  * copy action in order to copy reg_a set by WQE to reg_c[0].
3652  *
3653  * @param[out] ext_actions
3654  *   Pointer to the extended action list.
3655  * @param[in] actions
3656  *   Pointer to the list of actions.
3657  * @param[in] actions_n
3658  *   Number of actions in the list.
3659  * @param[out] error
3660  *   Perform verbose error reporting if not NULL.
3661  *
3662  * @return
3663  *   0 on success, negative value otherwise
3664  */
3665 static int
3666 flow_mreg_tx_copy_prep(struct rte_eth_dev *dev,
3667                        struct rte_flow_action *ext_actions,
3668                        const struct rte_flow_action *actions,
3669                        int actions_n, struct rte_flow_error *error)
3670 {
3671         struct mlx5_flow_action_copy_mreg *cp_mreg =
3672                 (struct mlx5_flow_action_copy_mreg *)
3673                         (ext_actions + actions_n + 1);
3674         int ret;
3675
3676         ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_RX, 0, error);
3677         if (ret < 0)
3678                 return ret;
3679         cp_mreg->dst = ret;
3680         ret = mlx5_flow_get_reg_id(dev, MLX5_METADATA_TX, 0, error);
3681         if (ret < 0)
3682                 return ret;
3683         cp_mreg->src = ret;
3684         memcpy(ext_actions, actions,
3685                         sizeof(*ext_actions) * actions_n);
3686         ext_actions[actions_n - 1] = (struct rte_flow_action){
3687                 .type = MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
3688                 .conf = cp_mreg,
3689         };
3690         ext_actions[actions_n] = (struct rte_flow_action){
3691                 .type = RTE_FLOW_ACTION_TYPE_END,
3692         };
3693         return 0;
3694 }
3695
3696 /**
3697  * The splitting for metadata feature.
3698  *
3699  * - Q/RSS action on NIC Rx should be split in order to pass by
3700  *   the mreg copy table (RX_CP_TBL) and then it jumps to the
3701  *   action table (RX_ACT_TBL) which has the split Q/RSS action.
3702  *
3703  * - All the actions on NIC Tx should have a mreg copy action to
3704  *   copy reg_a from WQE to reg_c[0].
3705  *
3706  * @param dev
3707  *   Pointer to Ethernet device.
3708  * @param[in] flow
3709  *   Parent flow structure pointer.
3710  * @param[in] attr
3711  *   Flow rule attributes.
3712  * @param[in] items
3713  *   Pattern specification (list terminated by the END pattern item).
3714  * @param[in] actions
3715  *   Associated actions (list terminated by the END action).
3716  * @param[in] external
3717  *   This flow rule is created by request external to PMD.
3718  * @param[out] error
3719  *   Perform verbose error reporting if not NULL.
3720  * @return
3721  *   0 on success, negative value otherwise
3722  */
3723 static int
3724 flow_create_split_metadata(struct rte_eth_dev *dev,
3725                            struct rte_flow *flow,
3726                            const struct rte_flow_attr *attr,
3727                            const struct rte_flow_item items[],
3728                            const struct rte_flow_action actions[],
3729                            bool external, struct rte_flow_error *error)
3730 {
3731         struct mlx5_priv *priv = dev->data->dev_private;
3732         struct mlx5_dev_config *config = &priv->config;
3733         const struct rte_flow_action *qrss = NULL;
3734         struct rte_flow_action *ext_actions = NULL;
3735         struct mlx5_flow *dev_flow = NULL;
3736         uint32_t qrss_id = 0;
3737         int mtr_sfx = 0;
3738         size_t act_size;
3739         int actions_n;
3740         int ret;
3741
3742         /* Check whether extensive metadata feature is engaged. */
3743         if (!config->dv_flow_en ||
3744             config->dv_xmeta_en == MLX5_XMETA_MODE_LEGACY ||
3745             !mlx5_flow_ext_mreg_supported(dev))
3746                 return flow_create_split_inner(dev, flow, NULL, attr, items,
3747                                                actions, external, error);
3748         actions_n = flow_parse_qrss_action(actions, &qrss);
3749         if (qrss) {
3750                 /* Exclude hairpin flows from splitting. */
3751                 if (qrss->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
3752                         const struct rte_flow_action_queue *queue;
3753
3754                         queue = qrss->conf;
3755                         if (mlx5_rxq_get_type(dev, queue->index) ==
3756                             MLX5_RXQ_TYPE_HAIRPIN)
3757                                 qrss = NULL;
3758                 } else if (qrss->type == RTE_FLOW_ACTION_TYPE_RSS) {
3759                         const struct rte_flow_action_rss *rss;
3760
3761                         rss = qrss->conf;
3762                         if (mlx5_rxq_get_type(dev, rss->queue[0]) ==
3763                             MLX5_RXQ_TYPE_HAIRPIN)
3764                                 qrss = NULL;
3765                 }
3766         }
3767         if (qrss) {
3768                 /* Check if it is in meter suffix table. */
3769                 mtr_sfx = attr->group == (attr->transfer ?
3770                           (MLX5_FLOW_TABLE_LEVEL_SUFFIX - 1) :
3771                           MLX5_FLOW_TABLE_LEVEL_SUFFIX);
3772                 /*
3773                  * Q/RSS action on NIC Rx should be split in order to pass by
3774                  * the mreg copy table (RX_CP_TBL) and then it jumps to the
3775                  * action table (RX_ACT_TBL) which has the split Q/RSS action.
3776                  */
3777                 act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
3778                            sizeof(struct rte_flow_action_set_tag) +
3779                            sizeof(struct rte_flow_action_jump);
3780                 ext_actions = rte_zmalloc(__func__, act_size, 0);
3781                 if (!ext_actions)
3782                         return rte_flow_error_set(error, ENOMEM,
3783                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3784                                                   NULL, "no memory to split "
3785                                                   "metadata flow");
3786                 /*
3787                  * If we are the suffix flow of meter, tag already exist.
3788                  * Set the tag action to void.
3789                  */
3790                 if (mtr_sfx)
3791                         ext_actions[qrss - actions].type =
3792                                                 RTE_FLOW_ACTION_TYPE_VOID;
3793                 else
3794                         ext_actions[qrss - actions].type =
3795                                                 MLX5_RTE_FLOW_ACTION_TYPE_TAG;
3796                 /*
3797                  * Create the new actions list with removed Q/RSS action
3798                  * and appended set tag and jump to register copy table
3799                  * (RX_CP_TBL). We should preallocate unique tag ID here
3800                  * in advance, because it is needed for set tag action.
3801                  */
3802                 qrss_id = flow_mreg_split_qrss_prep(dev, ext_actions, actions,
3803                                                     qrss, actions_n, error);
3804                 if (!mtr_sfx && !qrss_id) {
3805                         ret = -rte_errno;
3806                         goto exit;
3807                 }
3808         } else if (attr->egress && !attr->transfer) {
3809                 /*
3810                  * All the actions on NIC Tx should have a metadata register
3811                  * copy action to copy reg_a from WQE to reg_c[meta]
3812                  */
3813                 act_size = sizeof(struct rte_flow_action) * (actions_n + 1) +
3814                            sizeof(struct mlx5_flow_action_copy_mreg);
3815                 ext_actions = rte_zmalloc(__func__, act_size, 0);
3816                 if (!ext_actions)
3817                         return rte_flow_error_set(error, ENOMEM,
3818                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3819                                                   NULL, "no memory to split "
3820                                                   "metadata flow");
3821                 /* Create the action list appended with copy register. */
3822                 ret = flow_mreg_tx_copy_prep(dev, ext_actions, actions,
3823                                              actions_n, error);
3824                 if (ret < 0)
3825                         goto exit;
3826         }
3827         /* Add the unmodified original or prefix subflow. */
3828         ret = flow_create_split_inner(dev, flow, &dev_flow, attr, items,
3829                                       ext_actions ? ext_actions : actions,
3830                                       external, error);
3831         if (ret < 0)
3832                 goto exit;
3833         assert(dev_flow);
3834         if (qrss) {
3835                 const struct rte_flow_attr q_attr = {
3836                         .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
3837                         .ingress = 1,
3838                 };
3839                 /* Internal PMD action to set register. */
3840                 struct mlx5_rte_flow_item_tag q_tag_spec = {
3841                         .data = qrss_id,
3842                         .id = 0,
3843                 };
3844                 struct rte_flow_item q_items[] = {
3845                         {
3846                                 .type = MLX5_RTE_FLOW_ITEM_TYPE_TAG,
3847                                 .spec = &q_tag_spec,
3848                                 .last = NULL,
3849                                 .mask = NULL,
3850                         },
3851                         {
3852                                 .type = RTE_FLOW_ITEM_TYPE_END,
3853                         },
3854                 };
3855                 struct rte_flow_action q_actions[] = {
3856                         {
3857                                 .type = qrss->type,
3858                                 .conf = qrss->conf,
3859                         },
3860                         {
3861                                 .type = RTE_FLOW_ACTION_TYPE_END,
3862                         },
3863                 };
3864                 uint64_t hash_fields = dev_flow->hash_fields;
3865
3866                 /*
3867                  * Configure the tag item only if there is no meter subflow.
3868                  * Since tag is already marked in the meter suffix subflow
3869                  * we can just use the meter suffix items as is.
3870                  */
3871                 if (qrss_id) {
3872                         /* Not meter subflow. */
3873                         assert(!mtr_sfx);
3874                         /*
3875                          * Put unique id in prefix flow due to it is destroyed
3876                          * after suffix flow and id will be freed after there
3877                          * is no actual flows with this id and identifier
3878                          * reallocation becomes possible (for example, for
3879                          * other flows in other threads).
3880                          */
3881                         dev_flow->qrss_id = qrss_id;
3882                         qrss_id = 0;
3883                         ret = mlx5_flow_get_reg_id(dev, MLX5_COPY_MARK, 0,
3884                                                    error);
3885                         if (ret < 0)
3886                                 goto exit;
3887                         q_tag_spec.id = ret;
3888                 }
3889                 dev_flow = NULL;
3890                 /* Add suffix subflow to execute Q/RSS. */
3891                 ret = flow_create_split_inner(dev, flow, &dev_flow,
3892                                               &q_attr, mtr_sfx ? items :
3893                                               q_items, q_actions,
3894                                               external, error);
3895                 if (ret < 0)
3896                         goto exit;
3897                 assert(dev_flow);
3898                 dev_flow->hash_fields = hash_fields;
3899         }
3900
3901 exit:
3902         /*
3903          * We do not destroy the partially created sub_flows in case of error.
3904          * These ones are included into parent flow list and will be destroyed
3905          * by flow_drv_destroy.
3906          */
3907         flow_qrss_free_id(dev, qrss_id);
3908         rte_free(ext_actions);
3909         return ret;
3910 }
3911
3912 /**
3913  * The splitting for meter feature.
3914  *
3915  * - The meter flow will be split to two flows as prefix and
3916  *   suffix flow. The packets make sense only it pass the prefix
3917  *   meter action.
3918  *
3919  * - Reg_C_5 is used for the packet to match betweend prefix and
3920  *   suffix flow.
3921  *
3922  * @param dev
3923  *   Pointer to Ethernet device.
3924  * @param[in] flow
3925  *   Parent flow structure pointer.
3926  * @param[in] attr
3927  *   Flow rule attributes.
3928  * @param[in] items
3929  *   Pattern specification (list terminated by the END pattern item).
3930  * @param[in] actions
3931  *   Associated actions (list terminated by the END action).
3932  * @param[in] external
3933  *   This flow rule is created by request external to PMD.
3934  * @param[out] error
3935  *   Perform verbose error reporting if not NULL.
3936  * @return
3937  *   0 on success, negative value otherwise
3938  */
3939 static int
3940 flow_create_split_meter(struct rte_eth_dev *dev,
3941                            struct rte_flow *flow,
3942                            const struct rte_flow_attr *attr,
3943                            const struct rte_flow_item items[],
3944                            const struct rte_flow_action actions[],
3945                            bool external, struct rte_flow_error *error)
3946 {
3947         struct mlx5_priv *priv = dev->data->dev_private;
3948         struct rte_flow_action *sfx_actions = NULL;
3949         struct rte_flow_action *pre_actions = NULL;
3950         struct rte_flow_item *sfx_items = NULL;
3951         const  struct rte_flow_item *sfx_port_id_item;
3952         struct mlx5_flow *dev_flow = NULL;
3953         struct rte_flow_attr sfx_attr = *attr;
3954         uint32_t mtr = 0;
3955         uint32_t mtr_tag_id = 0;
3956         size_t act_size;
3957         size_t item_size;
3958         int actions_n = 0;
3959         int ret;
3960
3961         if (priv->mtr_en)
3962                 actions_n = flow_check_meter_action(actions, &mtr);
3963         if (mtr) {
3964                 struct mlx5_rte_flow_item_tag *tag_spec;
3965                 /* The five prefix actions: meter, decap, encap, tag, end. */
3966                 act_size = sizeof(struct rte_flow_action) * (actions_n + 5) +
3967                            sizeof(struct rte_flow_action_set_tag);
3968                 /* tag, end. */
3969 #define METER_SUFFIX_ITEM 3
3970                 item_size = sizeof(struct rte_flow_item) * METER_SUFFIX_ITEM +
3971                             sizeof(struct mlx5_rte_flow_item_tag);
3972                 sfx_actions = rte_zmalloc(__func__, (act_size + item_size), 0);
3973                 if (!sfx_actions)
3974                         return rte_flow_error_set(error, ENOMEM,
3975                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3976                                                   NULL, "no memory to split "
3977                                                   "meter flow");
3978                 pre_actions = sfx_actions + actions_n;
3979                 mtr_tag_id = flow_meter_split_prep(dev, actions, sfx_actions,
3980                                                      pre_actions);
3981                 if (!mtr_tag_id) {
3982                         ret = -rte_errno;
3983                         goto exit;
3984                 }
3985                 /* Add the prefix subflow. */
3986                 ret = flow_create_split_inner(dev, flow, &dev_flow, attr, items,
3987                                                   pre_actions, external, error);
3988                 if (ret) {
3989                         ret = -rte_errno;
3990                         goto exit;
3991                 }
3992                 dev_flow->mtr_flow_id = mtr_tag_id;
3993                 /* Prepare the suffix flow match pattern. */
3994                 sfx_items = (struct rte_flow_item *)((char *)sfx_actions +
3995                              act_size);
3996                 tag_spec = (struct mlx5_rte_flow_item_tag *)(sfx_items +
3997                             METER_SUFFIX_ITEM);
3998                 tag_spec->data = rte_cpu_to_be_32(dev_flow->mtr_flow_id);
3999                 tag_spec->id = mlx5_flow_get_reg_id(dev, MLX5_MTR_SFX, 0,
4000                                                     error);
4001                 sfx_items->type = MLX5_RTE_FLOW_ITEM_TYPE_TAG;
4002                 sfx_items->spec = tag_spec;
4003                 sfx_items->last = NULL;
4004                 sfx_items->mask = NULL;
4005                 sfx_items++;
4006                 sfx_port_id_item = find_port_id_item(items);
4007                 if (sfx_port_id_item) {
4008                         memcpy(sfx_items, sfx_port_id_item,
4009                                sizeof(*sfx_items));
4010                         sfx_items++;
4011                 }
4012                 sfx_items->type = RTE_FLOW_ITEM_TYPE_END;
4013                 sfx_items -= METER_SUFFIX_ITEM;
4014                 /* Setting the sfx group atrr. */
4015                 sfx_attr.group = sfx_attr.transfer ?
4016                                 (MLX5_FLOW_TABLE_LEVEL_SUFFIX - 1) :
4017                                  MLX5_FLOW_TABLE_LEVEL_SUFFIX;
4018         }
4019         /* Add the prefix subflow. */
4020         ret = flow_create_split_metadata(dev, flow, &sfx_attr,
4021                                          sfx_items ? sfx_items : items,
4022                                          sfx_actions ? sfx_actions : actions,
4023                                          external, error);
4024 exit:
4025         if (sfx_actions)
4026                 rte_free(sfx_actions);
4027         return ret;
4028 }
4029
4030 /**
4031  * Split the flow to subflow set. The splitters might be linked
4032  * in the chain, like this:
4033  * flow_create_split_outer() calls:
4034  *   flow_create_split_meter() calls:
4035  *     flow_create_split_metadata(meter_subflow_0) calls:
4036  *       flow_create_split_inner(metadata_subflow_0)
4037  *       flow_create_split_inner(metadata_subflow_1)
4038  *       flow_create_split_inner(metadata_subflow_2)
4039  *     flow_create_split_metadata(meter_subflow_1) calls:
4040  *       flow_create_split_inner(metadata_subflow_0)
4041  *       flow_create_split_inner(metadata_subflow_1)
4042  *       flow_create_split_inner(metadata_subflow_2)
4043  *
4044  * This provide flexible way to add new levels of flow splitting.
4045  * The all of successfully created subflows are included to the
4046  * parent flow dev_flow list.
4047  *
4048  * @param dev
4049  *   Pointer to Ethernet device.
4050  * @param[in] flow
4051  *   Parent flow structure pointer.
4052  * @param[in] attr
4053  *   Flow rule attributes.
4054  * @param[in] items
4055  *   Pattern specification (list terminated by the END pattern item).
4056  * @param[in] actions
4057  *   Associated actions (list terminated by the END action).
4058  * @param[in] external
4059  *   This flow rule is created by request external to PMD.
4060  * @param[out] error
4061  *   Perform verbose error reporting if not NULL.
4062  * @return
4063  *   0 on success, negative value otherwise
4064  */
4065 static int
4066 flow_create_split_outer(struct rte_eth_dev *dev,
4067                         struct rte_flow *flow,
4068                         const struct rte_flow_attr *attr,
4069                         const struct rte_flow_item items[],
4070                         const struct rte_flow_action actions[],
4071                         bool external, struct rte_flow_error *error)
4072 {
4073         int ret;
4074
4075         ret = flow_create_split_meter(dev, flow, attr, items,
4076                                          actions, external, error);
4077         assert(ret <= 0);
4078         return ret;
4079 }
4080
4081 /**
4082  * Create a flow and add it to @p list.
4083  *
4084  * @param dev
4085  *   Pointer to Ethernet device.
4086  * @param list
4087  *   Pointer to a TAILQ flow list. If this parameter NULL,
4088  *   no list insertion occurred, flow is just created,
4089  *   this is caller's responsibility to track the
4090  *   created flow.
4091  * @param[in] attr
4092  *   Flow rule attributes.
4093  * @param[in] items
4094  *   Pattern specification (list terminated by the END pattern item).
4095  * @param[in] actions
4096  *   Associated actions (list terminated by the END action).
4097  * @param[in] external
4098  *   This flow rule is created by request external to PMD.
4099  * @param[out] error
4100  *   Perform verbose error reporting if not NULL.
4101  *
4102  * @return
4103  *   A flow on success, NULL otherwise and rte_errno is set.
4104  */
4105 static struct rte_flow *
4106 flow_list_create(struct rte_eth_dev *dev, struct mlx5_flows *list,
4107                  const struct rte_flow_attr *attr,
4108                  const struct rte_flow_item items[],
4109                  const struct rte_flow_action actions[],
4110                  bool external, struct rte_flow_error *error)
4111 {
4112         struct mlx5_priv *priv = dev->data->dev_private;
4113         struct rte_flow *flow = NULL;
4114         struct mlx5_flow *dev_flow;
4115         const struct rte_flow_action_rss *rss;
4116         union {
4117                 struct rte_flow_expand_rss buf;
4118                 uint8_t buffer[2048];
4119         } expand_buffer;
4120         union {
4121                 struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
4122                 uint8_t buffer[2048];
4123         } actions_rx;
4124         union {
4125                 struct rte_flow_action actions[MLX5_MAX_SPLIT_ACTIONS];
4126                 uint8_t buffer[2048];
4127         } actions_hairpin_tx;
4128         union {
4129                 struct rte_flow_item items[MLX5_MAX_SPLIT_ITEMS];
4130                 uint8_t buffer[2048];
4131         } items_tx;
4132         struct rte_flow_expand_rss *buf = &expand_buffer.buf;
4133         const struct rte_flow_action *p_actions_rx = actions;
4134         int ret;
4135         uint32_t i;
4136         uint32_t flow_size;
4137         int hairpin_flow = 0;
4138         uint32_t hairpin_id = 0;
4139         struct rte_flow_attr attr_tx = { .priority = 0 };
4140
4141         hairpin_flow = flow_check_hairpin_split(dev, attr, actions);
4142         if (hairpin_flow > 0) {
4143                 if (hairpin_flow > MLX5_MAX_SPLIT_ACTIONS) {
4144                         rte_errno = EINVAL;
4145                         return NULL;
4146                 }
4147                 flow_hairpin_split(dev, actions, actions_rx.actions,
4148                                    actions_hairpin_tx.actions, items_tx.items,
4149                                    &hairpin_id);
4150                 p_actions_rx = actions_rx.actions;
4151         }
4152         ret = flow_drv_validate(dev, attr, items, p_actions_rx, external,
4153                                 error);
4154         if (ret < 0)
4155                 goto error_before_flow;
4156         flow_size = sizeof(struct rte_flow);
4157         rss = flow_get_rss_action(p_actions_rx);
4158         if (rss)
4159                 flow_size += RTE_ALIGN_CEIL(rss->queue_num * sizeof(uint16_t),
4160                                             sizeof(void *));
4161         else
4162                 flow_size += RTE_ALIGN_CEIL(sizeof(uint16_t), sizeof(void *));
4163         flow = rte_calloc(__func__, 1, flow_size, 0);
4164         if (!flow) {
4165                 rte_errno = ENOMEM;
4166                 goto error_before_flow;
4167         }
4168         flow->drv_type = flow_get_drv_type(dev, attr);
4169         if (hairpin_id != 0)
4170                 flow->hairpin_flow_id = hairpin_id;
4171         assert(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
4172                flow->drv_type < MLX5_FLOW_TYPE_MAX);
4173         flow->rss.queue = (void *)(flow + 1);
4174         if (rss) {
4175                 /*
4176                  * The following information is required by
4177                  * mlx5_flow_hashfields_adjust() in advance.
4178                  */
4179                 flow->rss.level = rss->level;
4180                 /* RSS type 0 indicates default RSS type (ETH_RSS_IP). */
4181                 flow->rss.types = !rss->types ? ETH_RSS_IP : rss->types;
4182         }
4183         LIST_INIT(&flow->dev_flows);
4184         if (rss && rss->types) {
4185                 unsigned int graph_root;
4186
4187                 graph_root = find_graph_root(items, rss->level);
4188                 ret = rte_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
4189                                           items, rss->types,
4190                                           mlx5_support_expansion,
4191                                           graph_root);
4192                 assert(ret > 0 &&
4193                        (unsigned int)ret < sizeof(expand_buffer.buffer));
4194         } else {
4195                 buf->entries = 1;
4196                 buf->entry[0].pattern = (void *)(uintptr_t)items;
4197         }
4198         for (i = 0; i < buf->entries; ++i) {
4199                 /*
4200                  * The splitter may create multiple dev_flows,
4201                  * depending on configuration. In the simplest
4202                  * case it just creates unmodified original flow.
4203                  */
4204                 ret = flow_create_split_outer(dev, flow, attr,
4205                                               buf->entry[i].pattern,
4206                                               p_actions_rx, external,
4207                                               error);
4208                 if (ret < 0)
4209                         goto error;
4210         }
4211         /* Create the tx flow. */
4212         if (hairpin_flow) {
4213                 attr_tx.group = MLX5_HAIRPIN_TX_TABLE;
4214                 attr_tx.ingress = 0;
4215                 attr_tx.egress = 1;
4216                 dev_flow = flow_drv_prepare(flow, &attr_tx, items_tx.items,
4217                                             actions_hairpin_tx.actions, error);
4218                 if (!dev_flow)
4219                         goto error;
4220                 dev_flow->flow = flow;
4221                 dev_flow->external = 0;
4222                 LIST_INSERT_HEAD(&flow->dev_flows, dev_flow, next);
4223                 ret = flow_drv_translate(dev, dev_flow, &attr_tx,
4224                                          items_tx.items,
4225                                          actions_hairpin_tx.actions, error);
4226                 if (ret < 0)
4227                         goto error;
4228         }
4229         /*
4230          * Update the metadata register copy table. If extensive
4231          * metadata feature is enabled and registers are supported
4232          * we might create the extra rte_flow for each unique
4233          * MARK/FLAG action ID.
4234          *
4235          * The table is updated for ingress Flows only, because
4236          * the egress Flows belong to the different device and
4237          * copy table should be updated in peer NIC Rx domain.
4238          */
4239         if (attr->ingress &&
4240             (external || attr->group != MLX5_FLOW_MREG_CP_TABLE_GROUP)) {
4241                 ret = flow_mreg_update_copy_table(dev, flow, actions, error);
4242                 if (ret)
4243                         goto error;
4244         }
4245         if (dev->data->dev_started) {
4246                 ret = flow_drv_apply(dev, flow, error);
4247                 if (ret < 0)
4248                         goto error;
4249         }
4250         if (list)
4251                 TAILQ_INSERT_TAIL(list, flow, next);
4252         flow_rxq_flags_set(dev, flow);
4253         return flow;
4254 error_before_flow:
4255         if (hairpin_id)
4256                 mlx5_flow_id_release(priv->sh->flow_id_pool,
4257                                      hairpin_id);
4258         return NULL;
4259 error:
4260         assert(flow);
4261         flow_mreg_del_copy_action(dev, flow);
4262         ret = rte_errno; /* Save rte_errno before cleanup. */
4263         if (flow->hairpin_flow_id)
4264                 mlx5_flow_id_release(priv->sh->flow_id_pool,
4265                                      flow->hairpin_flow_id);
4266         assert(flow);
4267         flow_drv_destroy(dev, flow);
4268         rte_free(flow);
4269         rte_errno = ret; /* Restore rte_errno. */
4270         return NULL;
4271 }
4272
4273 /**
4274  * Create a dedicated flow rule on e-switch table 0 (root table), to direct all
4275  * incoming packets to table 1.
4276  *
4277  * Other flow rules, requested for group n, will be created in
4278  * e-switch table n+1.
4279  * Jump action to e-switch group n will be created to group n+1.
4280  *
4281  * Used when working in switchdev mode, to utilise advantages of table 1
4282  * and above.
4283  *
4284  * @param dev
4285  *   Pointer to Ethernet device.
4286  *
4287  * @return
4288  *   Pointer to flow on success, NULL otherwise and rte_errno is set.
4289  */
4290 struct rte_flow *
4291 mlx5_flow_create_esw_table_zero_flow(struct rte_eth_dev *dev)
4292 {
4293         const struct rte_flow_attr attr = {
4294                 .group = 0,
4295                 .priority = 0,
4296                 .ingress = 1,
4297                 .egress = 0,
4298                 .transfer = 1,
4299         };
4300         const struct rte_flow_item pattern = {
4301                 .type = RTE_FLOW_ITEM_TYPE_END,
4302         };
4303         struct rte_flow_action_jump jump = {
4304                 .group = 1,
4305         };
4306         const struct rte_flow_action actions[] = {
4307                 {
4308                         .type = RTE_FLOW_ACTION_TYPE_JUMP,
4309                         .conf = &jump,
4310                 },
4311                 {
4312                         .type = RTE_FLOW_ACTION_TYPE_END,
4313                 },
4314         };
4315         struct mlx5_priv *priv = dev->data->dev_private;
4316         struct rte_flow_error error;
4317
4318         return flow_list_create(dev, &priv->ctrl_flows, &attr, &pattern,
4319                                 actions, false, &error);
4320 }
4321
4322 /**
4323  * Create a flow.
4324  *
4325  * @see rte_flow_create()
4326  * @see rte_flow_ops
4327  */
4328 struct rte_flow *
4329 mlx5_flow_create(struct rte_eth_dev *dev,
4330                  const struct rte_flow_attr *attr,
4331                  const struct rte_flow_item items[],
4332                  const struct rte_flow_action actions[],
4333                  struct rte_flow_error *error)
4334 {
4335         struct mlx5_priv *priv = dev->data->dev_private;
4336
4337         return flow_list_create(dev, &priv->flows,
4338                                 attr, items, actions, true, error);
4339 }
4340
4341 /**
4342  * Destroy a flow in a list.
4343  *
4344  * @param dev
4345  *   Pointer to Ethernet device.
4346  * @param list
4347  *   Pointer to a TAILQ flow list. If this parameter NULL,
4348  *   there is no flow removal from the list.
4349  * @param[in] flow
4350  *   Flow to destroy.
4351  */
4352 static void
4353 flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
4354                   struct rte_flow *flow)
4355 {
4356         struct mlx5_priv *priv = dev->data->dev_private;
4357
4358         /*
4359          * Update RX queue flags only if port is started, otherwise it is
4360          * already clean.
4361          */
4362         if (dev->data->dev_started)
4363                 flow_rxq_flags_trim(dev, flow);
4364         if (flow->hairpin_flow_id)
4365                 mlx5_flow_id_release(priv->sh->flow_id_pool,
4366                                      flow->hairpin_flow_id);
4367         flow_drv_destroy(dev, flow);
4368         if (list)
4369                 TAILQ_REMOVE(list, flow, next);
4370         flow_mreg_del_copy_action(dev, flow);
4371         rte_free(flow->fdir);
4372         rte_free(flow);
4373 }
4374
4375 /**
4376  * Destroy all flows.
4377  *
4378  * @param dev
4379  *   Pointer to Ethernet device.
4380  * @param list
4381  *   Pointer to a TAILQ flow list.
4382  */
4383 void
4384 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
4385 {
4386         while (!TAILQ_EMPTY(list)) {
4387                 struct rte_flow *flow;
4388
4389                 flow = TAILQ_FIRST(list);
4390                 flow_list_destroy(dev, list, flow);
4391         }
4392 }
4393
4394 /**
4395  * Remove all flows.
4396  *
4397  * @param dev
4398  *   Pointer to Ethernet device.
4399  * @param list
4400  *   Pointer to a TAILQ flow list.
4401  */
4402 void
4403 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
4404 {
4405         struct rte_flow *flow;
4406
4407         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next) {
4408                 flow_drv_remove(dev, flow);
4409                 flow_mreg_stop_copy_action(dev, flow);
4410         }
4411         flow_mreg_del_default_copy_action(dev);
4412         flow_rxq_flags_clear(dev);
4413 }
4414
4415 /**
4416  * Add all flows.
4417  *
4418  * @param dev
4419  *   Pointer to Ethernet device.
4420  * @param list
4421  *   Pointer to a TAILQ flow list.
4422  *
4423  * @return
4424  *   0 on success, a negative errno value otherwise and rte_errno is set.
4425  */
4426 int
4427 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
4428 {
4429         struct rte_flow *flow;
4430         struct rte_flow_error error;
4431         int ret = 0;
4432
4433         /* Make sure default copy action (reg_c[0] -> reg_b) is created. */
4434         ret = flow_mreg_add_default_copy_action(dev, &error);
4435         if (ret < 0)
4436                 return -rte_errno;
4437         /* Apply Flows created by application. */
4438         TAILQ_FOREACH(flow, list, next) {
4439                 ret = flow_mreg_start_copy_action(dev, flow);
4440                 if (ret < 0)
4441                         goto error;
4442                 ret = flow_drv_apply(dev, flow, &error);
4443                 if (ret < 0)
4444                         goto error;
4445                 flow_rxq_flags_set(dev, flow);
4446         }
4447         return 0;
4448 error:
4449         ret = rte_errno; /* Save rte_errno before cleanup. */
4450         mlx5_flow_stop(dev, list);
4451         rte_errno = ret; /* Restore rte_errno. */
4452         return -rte_errno;
4453 }
4454
4455 /**
4456  * Verify the flow list is empty
4457  *
4458  * @param dev
4459  *  Pointer to Ethernet device.
4460  *
4461  * @return the number of flows not released.
4462  */
4463 int
4464 mlx5_flow_verify(struct rte_eth_dev *dev)
4465 {
4466         struct mlx5_priv *priv = dev->data->dev_private;
4467         struct rte_flow *flow;
4468         int ret = 0;
4469
4470         TAILQ_FOREACH(flow, &priv->flows, next) {
4471                 DRV_LOG(DEBUG, "port %u flow %p still referenced",
4472                         dev->data->port_id, (void *)flow);
4473                 ++ret;
4474         }
4475         return ret;
4476 }
4477
4478 /**
4479  * Enable default hairpin egress flow.
4480  *
4481  * @param dev
4482  *   Pointer to Ethernet device.
4483  * @param queue
4484  *   The queue index.
4485  *
4486  * @return
4487  *   0 on success, a negative errno value otherwise and rte_errno is set.
4488  */
4489 int
4490 mlx5_ctrl_flow_source_queue(struct rte_eth_dev *dev,
4491                             uint32_t queue)
4492 {
4493         struct mlx5_priv *priv = dev->data->dev_private;
4494         const struct rte_flow_attr attr = {
4495                 .egress = 1,
4496                 .priority = 0,
4497         };
4498         struct mlx5_rte_flow_item_tx_queue queue_spec = {
4499                 .queue = queue,
4500         };
4501         struct mlx5_rte_flow_item_tx_queue queue_mask = {
4502                 .queue = UINT32_MAX,
4503         };
4504         struct rte_flow_item items[] = {
4505                 {
4506                         .type = MLX5_RTE_FLOW_ITEM_TYPE_TX_QUEUE,
4507                         .spec = &queue_spec,
4508                         .last = NULL,
4509                         .mask = &queue_mask,
4510                 },
4511                 {
4512                         .type = RTE_FLOW_ITEM_TYPE_END,
4513                 },
4514         };
4515         struct rte_flow_action_jump jump = {
4516                 .group = MLX5_HAIRPIN_TX_TABLE,
4517         };
4518         struct rte_flow_action actions[2];
4519         struct rte_flow *flow;
4520         struct rte_flow_error error;
4521
4522         actions[0].type = RTE_FLOW_ACTION_TYPE_JUMP;
4523         actions[0].conf = &jump;
4524         actions[1].type = RTE_FLOW_ACTION_TYPE_END;
4525         flow = flow_list_create(dev, &priv->ctrl_flows,
4526                                 &attr, items, actions, false, &error);
4527         if (!flow) {
4528                 DRV_LOG(DEBUG,
4529                         "Failed to create ctrl flow: rte_errno(%d),"
4530                         " type(%d), message(%s)",
4531                         rte_errno, error.type,
4532                         error.message ? error.message : " (no stated reason)");
4533                 return -rte_errno;
4534         }
4535         return 0;
4536 }
4537
4538 /**
4539  * Enable a control flow configured from the control plane.
4540  *
4541  * @param dev
4542  *   Pointer to Ethernet device.
4543  * @param eth_spec
4544  *   An Ethernet flow spec to apply.
4545  * @param eth_mask
4546  *   An Ethernet flow mask to apply.
4547  * @param vlan_spec
4548  *   A VLAN flow spec to apply.
4549  * @param vlan_mask
4550  *   A VLAN flow mask to apply.
4551  *
4552  * @return
4553  *   0 on success, a negative errno value otherwise and rte_errno is set.
4554  */
4555 int
4556 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
4557                     struct rte_flow_item_eth *eth_spec,
4558                     struct rte_flow_item_eth *eth_mask,
4559                     struct rte_flow_item_vlan *vlan_spec,
4560                     struct rte_flow_item_vlan *vlan_mask)
4561 {
4562         struct mlx5_priv *priv = dev->data->dev_private;
4563         const struct rte_flow_attr attr = {
4564                 .ingress = 1,
4565                 .priority = MLX5_FLOW_PRIO_RSVD,
4566         };
4567         struct rte_flow_item items[] = {
4568                 {
4569                         .type = RTE_FLOW_ITEM_TYPE_ETH,
4570                         .spec = eth_spec,
4571                         .last = NULL,
4572                         .mask = eth_mask,
4573                 },
4574                 {
4575                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
4576                                               RTE_FLOW_ITEM_TYPE_END,
4577                         .spec = vlan_spec,
4578                         .last = NULL,
4579                         .mask = vlan_mask,
4580                 },
4581                 {
4582                         .type = RTE_FLOW_ITEM_TYPE_END,
4583                 },
4584         };
4585         uint16_t queue[priv->reta_idx_n];
4586         struct rte_flow_action_rss action_rss = {
4587                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
4588                 .level = 0,
4589                 .types = priv->rss_conf.rss_hf,
4590                 .key_len = priv->rss_conf.rss_key_len,
4591                 .queue_num = priv->reta_idx_n,
4592                 .key = priv->rss_conf.rss_key,
4593                 .queue = queue,
4594         };
4595         struct rte_flow_action actions[] = {
4596                 {
4597                         .type = RTE_FLOW_ACTION_TYPE_RSS,
4598                         .conf = &action_rss,
4599                 },
4600                 {
4601                         .type = RTE_FLOW_ACTION_TYPE_END,
4602                 },
4603         };
4604         struct rte_flow *flow;
4605         struct rte_flow_error error;
4606         unsigned int i;
4607
4608         if (!priv->reta_idx_n || !priv->rxqs_n) {
4609                 return 0;
4610         }
4611         for (i = 0; i != priv->reta_idx_n; ++i)
4612                 queue[i] = (*priv->reta_idx)[i];
4613         flow = flow_list_create(dev, &priv->ctrl_flows,
4614                                 &attr, items, actions, false, &error);
4615         if (!flow)
4616                 return -rte_errno;
4617         return 0;
4618 }
4619
4620 /**
4621  * Enable a flow control configured from the control plane.
4622  *
4623  * @param dev
4624  *   Pointer to Ethernet device.
4625  * @param eth_spec
4626  *   An Ethernet flow spec to apply.
4627  * @param eth_mask
4628  *   An Ethernet flow mask to apply.
4629  *
4630  * @return
4631  *   0 on success, a negative errno value otherwise and rte_errno is set.
4632  */
4633 int
4634 mlx5_ctrl_flow(struct rte_eth_dev *dev,
4635                struct rte_flow_item_eth *eth_spec,
4636                struct rte_flow_item_eth *eth_mask)
4637 {
4638         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
4639 }
4640
4641 /**
4642  * Destroy a flow.
4643  *
4644  * @see rte_flow_destroy()
4645  * @see rte_flow_ops
4646  */
4647 int
4648 mlx5_flow_destroy(struct rte_eth_dev *dev,
4649                   struct rte_flow *flow,
4650                   struct rte_flow_error *error __rte_unused)
4651 {
4652         struct mlx5_priv *priv = dev->data->dev_private;
4653
4654         flow_list_destroy(dev, &priv->flows, flow);
4655         return 0;
4656 }
4657
4658 /**
4659  * Destroy all flows.
4660  *
4661  * @see rte_flow_flush()
4662  * @see rte_flow_ops
4663  */
4664 int
4665 mlx5_flow_flush(struct rte_eth_dev *dev,
4666                 struct rte_flow_error *error __rte_unused)
4667 {
4668         struct mlx5_priv *priv = dev->data->dev_private;
4669
4670         mlx5_flow_list_flush(dev, &priv->flows);
4671         return 0;
4672 }
4673
4674 /**
4675  * Isolated mode.
4676  *
4677  * @see rte_flow_isolate()
4678  * @see rte_flow_ops
4679  */
4680 int
4681 mlx5_flow_isolate(struct rte_eth_dev *dev,
4682                   int enable,
4683                   struct rte_flow_error *error)
4684 {
4685         struct mlx5_priv *priv = dev->data->dev_private;
4686
4687         if (dev->data->dev_started) {
4688                 rte_flow_error_set(error, EBUSY,
4689                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4690                                    NULL,
4691                                    "port must be stopped first");
4692                 return -rte_errno;
4693         }
4694         priv->isolated = !!enable;
4695         if (enable)
4696                 dev->dev_ops = &mlx5_dev_ops_isolate;
4697         else
4698                 dev->dev_ops = &mlx5_dev_ops;
4699         return 0;
4700 }
4701
4702 /**
4703  * Query a flow.
4704  *
4705  * @see rte_flow_query()
4706  * @see rte_flow_ops
4707  */
4708 static int
4709 flow_drv_query(struct rte_eth_dev *dev,
4710                struct rte_flow *flow,
4711                const struct rte_flow_action *actions,
4712                void *data,
4713                struct rte_flow_error *error)
4714 {
4715         const struct mlx5_flow_driver_ops *fops;
4716         enum mlx5_flow_drv_type ftype = flow->drv_type;
4717
4718         assert(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
4719         fops = flow_get_drv_ops(ftype);
4720
4721         return fops->query(dev, flow, actions, data, error);
4722 }
4723
4724 /**
4725  * Query a flow.
4726  *
4727  * @see rte_flow_query()
4728  * @see rte_flow_ops
4729  */
4730 int
4731 mlx5_flow_query(struct rte_eth_dev *dev,
4732                 struct rte_flow *flow,
4733                 const struct rte_flow_action *actions,
4734                 void *data,
4735                 struct rte_flow_error *error)
4736 {
4737         int ret;
4738
4739         ret = flow_drv_query(dev, flow, actions, data, error);
4740         if (ret < 0)
4741                 return ret;
4742         return 0;
4743 }
4744
4745 /**
4746  * Convert a flow director filter to a generic flow.
4747  *
4748  * @param dev
4749  *   Pointer to Ethernet device.
4750  * @param fdir_filter
4751  *   Flow director filter to add.
4752  * @param attributes
4753  *   Generic flow parameters structure.
4754  *
4755  * @return
4756  *   0 on success, a negative errno value otherwise and rte_errno is set.
4757  */
4758 static int
4759 flow_fdir_filter_convert(struct rte_eth_dev *dev,
4760                          const struct rte_eth_fdir_filter *fdir_filter,
4761                          struct mlx5_fdir *attributes)
4762 {
4763         struct mlx5_priv *priv = dev->data->dev_private;
4764         const struct rte_eth_fdir_input *input = &fdir_filter->input;
4765         const struct rte_eth_fdir_masks *mask =
4766                 &dev->data->dev_conf.fdir_conf.mask;
4767
4768         /* Validate queue number. */
4769         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
4770                 DRV_LOG(ERR, "port %u invalid queue number %d",
4771                         dev->data->port_id, fdir_filter->action.rx_queue);
4772                 rte_errno = EINVAL;
4773                 return -rte_errno;
4774         }
4775         attributes->attr.ingress = 1;
4776         attributes->items[0] = (struct rte_flow_item) {
4777                 .type = RTE_FLOW_ITEM_TYPE_ETH,
4778                 .spec = &attributes->l2,
4779                 .mask = &attributes->l2_mask,
4780         };
4781         switch (fdir_filter->action.behavior) {
4782         case RTE_ETH_FDIR_ACCEPT:
4783                 attributes->actions[0] = (struct rte_flow_action){
4784                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
4785                         .conf = &attributes->queue,
4786                 };
4787                 break;
4788         case RTE_ETH_FDIR_REJECT:
4789                 attributes->actions[0] = (struct rte_flow_action){
4790                         .type = RTE_FLOW_ACTION_TYPE_DROP,
4791                 };
4792                 break;
4793         default:
4794                 DRV_LOG(ERR, "port %u invalid behavior %d",
4795                         dev->data->port_id,
4796                         fdir_filter->action.behavior);
4797                 rte_errno = ENOTSUP;
4798                 return -rte_errno;
4799         }
4800         attributes->queue.index = fdir_filter->action.rx_queue;
4801         /* Handle L3. */
4802         switch (fdir_filter->input.flow_type) {
4803         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
4804         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
4805         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
4806                 attributes->l3.ipv4.hdr = (struct rte_ipv4_hdr){
4807                         .src_addr = input->flow.ip4_flow.src_ip,
4808                         .dst_addr = input->flow.ip4_flow.dst_ip,
4809                         .time_to_live = input->flow.ip4_flow.ttl,
4810                         .type_of_service = input->flow.ip4_flow.tos,
4811                 };
4812                 attributes->l3_mask.ipv4.hdr = (struct rte_ipv4_hdr){
4813                         .src_addr = mask->ipv4_mask.src_ip,
4814                         .dst_addr = mask->ipv4_mask.dst_ip,
4815                         .time_to_live = mask->ipv4_mask.ttl,
4816                         .type_of_service = mask->ipv4_mask.tos,
4817                         .next_proto_id = mask->ipv4_mask.proto,
4818                 };
4819                 attributes->items[1] = (struct rte_flow_item){
4820                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
4821                         .spec = &attributes->l3,
4822                         .mask = &attributes->l3_mask,
4823                 };
4824                 break;
4825         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
4826         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
4827         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
4828                 attributes->l3.ipv6.hdr = (struct rte_ipv6_hdr){
4829                         .hop_limits = input->flow.ipv6_flow.hop_limits,
4830                         .proto = input->flow.ipv6_flow.proto,
4831                 };
4832
4833                 memcpy(attributes->l3.ipv6.hdr.src_addr,
4834                        input->flow.ipv6_flow.src_ip,
4835                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
4836                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
4837                        input->flow.ipv6_flow.dst_ip,
4838                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
4839                 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
4840                        mask->ipv6_mask.src_ip,
4841                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
4842                 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
4843                        mask->ipv6_mask.dst_ip,
4844                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
4845                 attributes->items[1] = (struct rte_flow_item){
4846                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
4847                         .spec = &attributes->l3,
4848                         .mask = &attributes->l3_mask,
4849                 };
4850                 break;
4851         default:
4852                 DRV_LOG(ERR, "port %u invalid flow type%d",
4853                         dev->data->port_id, fdir_filter->input.flow_type);
4854                 rte_errno = ENOTSUP;
4855                 return -rte_errno;
4856         }
4857         /* Handle L4. */
4858         switch (fdir_filter->input.flow_type) {
4859         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
4860                 attributes->l4.udp.hdr = (struct rte_udp_hdr){
4861                         .src_port = input->flow.udp4_flow.src_port,
4862                         .dst_port = input->flow.udp4_flow.dst_port,
4863                 };
4864                 attributes->l4_mask.udp.hdr = (struct rte_udp_hdr){
4865                         .src_port = mask->src_port_mask,
4866                         .dst_port = mask->dst_port_mask,
4867                 };
4868                 attributes->items[2] = (struct rte_flow_item){
4869                         .type = RTE_FLOW_ITEM_TYPE_UDP,
4870                         .spec = &attributes->l4,
4871                         .mask = &attributes->l4_mask,
4872                 };
4873                 break;
4874         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
4875                 attributes->l4.tcp.hdr = (struct rte_tcp_hdr){
4876                         .src_port = input->flow.tcp4_flow.src_port,
4877                         .dst_port = input->flow.tcp4_flow.dst_port,
4878                 };
4879                 attributes->l4_mask.tcp.hdr = (struct rte_tcp_hdr){
4880                         .src_port = mask->src_port_mask,
4881                         .dst_port = mask->dst_port_mask,
4882                 };
4883                 attributes->items[2] = (struct rte_flow_item){
4884                         .type = RTE_FLOW_ITEM_TYPE_TCP,
4885                         .spec = &attributes->l4,
4886                         .mask = &attributes->l4_mask,
4887                 };
4888                 break;
4889         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
4890                 attributes->l4.udp.hdr = (struct rte_udp_hdr){
4891                         .src_port = input->flow.udp6_flow.src_port,
4892                         .dst_port = input->flow.udp6_flow.dst_port,
4893                 };
4894                 attributes->l4_mask.udp.hdr = (struct rte_udp_hdr){
4895                         .src_port = mask->src_port_mask,
4896                         .dst_port = mask->dst_port_mask,
4897                 };
4898                 attributes->items[2] = (struct rte_flow_item){
4899                         .type = RTE_FLOW_ITEM_TYPE_UDP,
4900                         .spec = &attributes->l4,
4901                         .mask = &attributes->l4_mask,
4902                 };
4903                 break;
4904         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
4905                 attributes->l4.tcp.hdr = (struct rte_tcp_hdr){
4906                         .src_port = input->flow.tcp6_flow.src_port,
4907                         .dst_port = input->flow.tcp6_flow.dst_port,
4908                 };
4909                 attributes->l4_mask.tcp.hdr = (struct rte_tcp_hdr){
4910                         .src_port = mask->src_port_mask,
4911                         .dst_port = mask->dst_port_mask,
4912                 };
4913                 attributes->items[2] = (struct rte_flow_item){
4914                         .type = RTE_FLOW_ITEM_TYPE_TCP,
4915                         .spec = &attributes->l4,
4916                         .mask = &attributes->l4_mask,
4917                 };
4918                 break;
4919         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
4920         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
4921                 break;
4922         default:
4923                 DRV_LOG(ERR, "port %u invalid flow type%d",
4924                         dev->data->port_id, fdir_filter->input.flow_type);
4925                 rte_errno = ENOTSUP;
4926                 return -rte_errno;
4927         }
4928         return 0;
4929 }
4930
4931 #define FLOW_FDIR_CMP(f1, f2, fld) \
4932         memcmp(&(f1)->fld, &(f2)->fld, sizeof(f1->fld))
4933
4934 /**
4935  * Compare two FDIR flows. If items and actions are identical, the two flows are
4936  * regarded as same.
4937  *
4938  * @param dev
4939  *   Pointer to Ethernet device.
4940  * @param f1
4941  *   FDIR flow to compare.
4942  * @param f2
4943  *   FDIR flow to compare.
4944  *
4945  * @return
4946  *   Zero on match, 1 otherwise.
4947  */
4948 static int
4949 flow_fdir_cmp(const struct mlx5_fdir *f1, const struct mlx5_fdir *f2)
4950 {
4951         if (FLOW_FDIR_CMP(f1, f2, attr) ||
4952             FLOW_FDIR_CMP(f1, f2, l2) ||
4953             FLOW_FDIR_CMP(f1, f2, l2_mask) ||
4954             FLOW_FDIR_CMP(f1, f2, l3) ||
4955             FLOW_FDIR_CMP(f1, f2, l3_mask) ||
4956             FLOW_FDIR_CMP(f1, f2, l4) ||
4957             FLOW_FDIR_CMP(f1, f2, l4_mask) ||
4958             FLOW_FDIR_CMP(f1, f2, actions[0].type))
4959                 return 1;
4960         if (f1->actions[0].type == RTE_FLOW_ACTION_TYPE_QUEUE &&
4961             FLOW_FDIR_CMP(f1, f2, queue))
4962                 return 1;
4963         return 0;
4964 }
4965
4966 /**
4967  * Search device flow list to find out a matched FDIR flow.
4968  *
4969  * @param dev
4970  *   Pointer to Ethernet device.
4971  * @param fdir_flow
4972  *   FDIR flow to lookup.
4973  *
4974  * @return
4975  *   Pointer of flow if found, NULL otherwise.
4976  */
4977 static struct rte_flow *
4978 flow_fdir_filter_lookup(struct rte_eth_dev *dev, struct mlx5_fdir *fdir_flow)
4979 {
4980         struct mlx5_priv *priv = dev->data->dev_private;
4981         struct rte_flow *flow = NULL;
4982
4983         assert(fdir_flow);
4984         TAILQ_FOREACH(flow, &priv->flows, next) {
4985                 if (flow->fdir && !flow_fdir_cmp(flow->fdir, fdir_flow)) {
4986                         DRV_LOG(DEBUG, "port %u found FDIR flow %p",
4987                                 dev->data->port_id, (void *)flow);
4988                         break;
4989                 }
4990         }
4991         return flow;
4992 }
4993
4994 /**
4995  * Add new flow director filter and store it in list.
4996  *
4997  * @param dev
4998  *   Pointer to Ethernet device.
4999  * @param fdir_filter
5000  *   Flow director filter to add.
5001  *
5002  * @return
5003  *   0 on success, a negative errno value otherwise and rte_errno is set.
5004  */
5005 static int
5006 flow_fdir_filter_add(struct rte_eth_dev *dev,
5007                      const struct rte_eth_fdir_filter *fdir_filter)
5008 {
5009         struct mlx5_priv *priv = dev->data->dev_private;
5010         struct mlx5_fdir *fdir_flow;
5011         struct rte_flow *flow;
5012         int ret;
5013
5014         fdir_flow = rte_zmalloc(__func__, sizeof(*fdir_flow), 0);
5015         if (!fdir_flow) {
5016                 rte_errno = ENOMEM;
5017                 return -rte_errno;
5018         }
5019         ret = flow_fdir_filter_convert(dev, fdir_filter, fdir_flow);
5020         if (ret)
5021                 goto error;
5022         flow = flow_fdir_filter_lookup(dev, fdir_flow);
5023         if (flow) {
5024                 rte_errno = EEXIST;
5025                 goto error;
5026         }
5027         flow = flow_list_create(dev, &priv->flows, &fdir_flow->attr,
5028                                 fdir_flow->items, fdir_flow->actions, true,
5029                                 NULL);
5030         if (!flow)
5031                 goto error;
5032         assert(!flow->fdir);
5033         flow->fdir = fdir_flow;
5034         DRV_LOG(DEBUG, "port %u created FDIR flow %p",
5035                 dev->data->port_id, (void *)flow);
5036         return 0;
5037 error:
5038         rte_free(fdir_flow);
5039         return -rte_errno;
5040 }
5041
5042 /**
5043  * Delete specific filter.
5044  *
5045  * @param dev
5046  *   Pointer to Ethernet device.
5047  * @param fdir_filter
5048  *   Filter to be deleted.
5049  *
5050  * @return
5051  *   0 on success, a negative errno value otherwise and rte_errno is set.
5052  */
5053 static int
5054 flow_fdir_filter_delete(struct rte_eth_dev *dev,
5055                         const struct rte_eth_fdir_filter *fdir_filter)
5056 {
5057         struct mlx5_priv *priv = dev->data->dev_private;
5058         struct rte_flow *flow;
5059         struct mlx5_fdir fdir_flow = {
5060                 .attr.group = 0,
5061         };
5062         int ret;
5063
5064         ret = flow_fdir_filter_convert(dev, fdir_filter, &fdir_flow);
5065         if (ret)
5066                 return -rte_errno;
5067         flow = flow_fdir_filter_lookup(dev, &fdir_flow);
5068         if (!flow) {
5069                 rte_errno = ENOENT;
5070                 return -rte_errno;
5071         }
5072         flow_list_destroy(dev, &priv->flows, flow);
5073         DRV_LOG(DEBUG, "port %u deleted FDIR flow %p",
5074                 dev->data->port_id, (void *)flow);
5075         return 0;
5076 }
5077
5078 /**
5079  * Update queue for specific filter.
5080  *
5081  * @param dev
5082  *   Pointer to Ethernet device.
5083  * @param fdir_filter
5084  *   Filter to be updated.
5085  *
5086  * @return
5087  *   0 on success, a negative errno value otherwise and rte_errno is set.
5088  */
5089 static int
5090 flow_fdir_filter_update(struct rte_eth_dev *dev,
5091                         const struct rte_eth_fdir_filter *fdir_filter)
5092 {
5093         int ret;
5094
5095         ret = flow_fdir_filter_delete(dev, fdir_filter);
5096         if (ret)
5097                 return ret;
5098         return flow_fdir_filter_add(dev, fdir_filter);
5099 }
5100
5101 /**
5102  * Flush all filters.
5103  *
5104  * @param dev
5105  *   Pointer to Ethernet device.
5106  */
5107 static void
5108 flow_fdir_filter_flush(struct rte_eth_dev *dev)
5109 {
5110         struct mlx5_priv *priv = dev->data->dev_private;
5111
5112         mlx5_flow_list_flush(dev, &priv->flows);
5113 }
5114
5115 /**
5116  * Get flow director information.
5117  *
5118  * @param dev
5119  *   Pointer to Ethernet device.
5120  * @param[out] fdir_info
5121  *   Resulting flow director information.
5122  */
5123 static void
5124 flow_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
5125 {
5126         struct rte_eth_fdir_masks *mask =
5127                 &dev->data->dev_conf.fdir_conf.mask;
5128
5129         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
5130         fdir_info->guarant_spc = 0;
5131         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
5132         fdir_info->max_flexpayload = 0;
5133         fdir_info->flow_types_mask[0] = 0;
5134         fdir_info->flex_payload_unit = 0;
5135         fdir_info->max_flex_payload_segment_num = 0;
5136         fdir_info->flex_payload_limit = 0;
5137         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
5138 }
5139
5140 /**
5141  * Deal with flow director operations.
5142  *
5143  * @param dev
5144  *   Pointer to Ethernet device.
5145  * @param filter_op
5146  *   Operation to perform.
5147  * @param arg
5148  *   Pointer to operation-specific structure.
5149  *
5150  * @return
5151  *   0 on success, a negative errno value otherwise and rte_errno is set.
5152  */
5153 static int
5154 flow_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
5155                     void *arg)
5156 {
5157         enum rte_fdir_mode fdir_mode =
5158                 dev->data->dev_conf.fdir_conf.mode;
5159
5160         if (filter_op == RTE_ETH_FILTER_NOP)
5161                 return 0;
5162         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
5163             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
5164                 DRV_LOG(ERR, "port %u flow director mode %d not supported",
5165                         dev->data->port_id, fdir_mode);
5166                 rte_errno = EINVAL;
5167                 return -rte_errno;
5168         }
5169         switch (filter_op) {
5170         case RTE_ETH_FILTER_ADD:
5171                 return flow_fdir_filter_add(dev, arg);
5172         case RTE_ETH_FILTER_UPDATE:
5173                 return flow_fdir_filter_update(dev, arg);
5174         case RTE_ETH_FILTER_DELETE:
5175                 return flow_fdir_filter_delete(dev, arg);
5176         case RTE_ETH_FILTER_FLUSH:
5177                 flow_fdir_filter_flush(dev);
5178                 break;
5179         case RTE_ETH_FILTER_INFO:
5180                 flow_fdir_info_get(dev, arg);
5181                 break;
5182         default:
5183                 DRV_LOG(DEBUG, "port %u unknown operation %u",
5184                         dev->data->port_id, filter_op);
5185                 rte_errno = EINVAL;
5186                 return -rte_errno;
5187         }
5188         return 0;
5189 }
5190
5191 /**
5192  * Manage filter operations.
5193  *
5194  * @param dev
5195  *   Pointer to Ethernet device structure.
5196  * @param filter_type
5197  *   Filter type.
5198  * @param filter_op
5199  *   Operation to perform.
5200  * @param arg
5201  *   Pointer to operation-specific structure.
5202  *
5203  * @return
5204  *   0 on success, a negative errno value otherwise and rte_errno is set.
5205  */
5206 int
5207 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
5208                      enum rte_filter_type filter_type,
5209                      enum rte_filter_op filter_op,
5210                      void *arg)
5211 {
5212         switch (filter_type) {
5213         case RTE_ETH_FILTER_GENERIC:
5214                 if (filter_op != RTE_ETH_FILTER_GET) {
5215                         rte_errno = EINVAL;
5216                         return -rte_errno;
5217                 }
5218                 *(const void **)arg = &mlx5_flow_ops;
5219                 return 0;
5220         case RTE_ETH_FILTER_FDIR:
5221                 return flow_fdir_ctrl_func(dev, filter_op, arg);
5222         default:
5223                 DRV_LOG(ERR, "port %u filter type (%d) not supported",
5224                         dev->data->port_id, filter_type);
5225                 rte_errno = ENOTSUP;
5226                 return -rte_errno;
5227         }
5228         return 0;
5229 }
5230
5231 /**
5232  * Create the needed meter and suffix tables.
5233  *
5234  * @param[in] dev
5235  *   Pointer to Ethernet device.
5236  * @param[in] fm
5237  *   Pointer to the flow meter.
5238  *
5239  * @return
5240  *   Pointer to table set on success, NULL otherwise.
5241  */
5242 struct mlx5_meter_domains_infos *
5243 mlx5_flow_create_mtr_tbls(struct rte_eth_dev *dev,
5244                           const struct mlx5_flow_meter *fm)
5245 {
5246         const struct mlx5_flow_driver_ops *fops;
5247
5248         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5249         return fops->create_mtr_tbls(dev, fm);
5250 }
5251
5252 /**
5253  * Destroy the meter table set.
5254  *
5255  * @param[in] dev
5256  *   Pointer to Ethernet device.
5257  * @param[in] tbl
5258  *   Pointer to the meter table set.
5259  *
5260  * @return
5261  *   0 on success.
5262  */
5263 int
5264 mlx5_flow_destroy_mtr_tbls(struct rte_eth_dev *dev,
5265                            struct mlx5_meter_domains_infos *tbls)
5266 {
5267         const struct mlx5_flow_driver_ops *fops;
5268
5269         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5270         return fops->destroy_mtr_tbls(dev, tbls);
5271 }
5272
5273 /**
5274  * Create policer rules.
5275  *
5276  * @param[in] dev
5277  *   Pointer to Ethernet device.
5278  * @param[in] fm
5279  *   Pointer to flow meter structure.
5280  * @param[in] attr
5281  *   Pointer to flow attributes.
5282  *
5283  * @return
5284  *   0 on success, -1 otherwise.
5285  */
5286 int
5287 mlx5_flow_create_policer_rules(struct rte_eth_dev *dev,
5288                                struct mlx5_flow_meter *fm,
5289                                const struct rte_flow_attr *attr)
5290 {
5291         const struct mlx5_flow_driver_ops *fops;
5292
5293         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5294         return fops->create_policer_rules(dev, fm, attr);
5295 }
5296
5297 /**
5298  * Destroy policer rules.
5299  *
5300  * @param[in] fm
5301  *   Pointer to flow meter structure.
5302  * @param[in] attr
5303  *   Pointer to flow attributes.
5304  *
5305  * @return
5306  *   0 on success, -1 otherwise.
5307  */
5308 int
5309 mlx5_flow_destroy_policer_rules(struct rte_eth_dev *dev,
5310                                 struct mlx5_flow_meter *fm,
5311                                 const struct rte_flow_attr *attr)
5312 {
5313         const struct mlx5_flow_driver_ops *fops;
5314
5315         fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5316         return fops->destroy_policer_rules(dev, fm, attr);
5317 }
5318
5319 /**
5320  * Allocate a counter.
5321  *
5322  * @param[in] dev
5323  *   Pointer to Ethernet device structure.
5324  *
5325  * @return
5326  *   Pointer to allocated counter  on success, NULL otherwise.
5327  */
5328 struct mlx5_flow_counter *
5329 mlx5_counter_alloc(struct rte_eth_dev *dev)
5330 {
5331         const struct mlx5_flow_driver_ops *fops;
5332         struct rte_flow_attr attr = { .transfer = 0 };
5333
5334         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
5335                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5336                 return fops->counter_alloc(dev);
5337         }
5338         DRV_LOG(ERR,
5339                 "port %u counter allocate is not supported.",
5340                  dev->data->port_id);
5341         return NULL;
5342 }
5343
5344 /**
5345  * Free a counter.
5346  *
5347  * @param[in] dev
5348  *   Pointer to Ethernet device structure.
5349  * @param[in] cnt
5350  *   Pointer to counter to be free.
5351  */
5352 void
5353 mlx5_counter_free(struct rte_eth_dev *dev, struct mlx5_flow_counter *cnt)
5354 {
5355         const struct mlx5_flow_driver_ops *fops;
5356         struct rte_flow_attr attr = { .transfer = 0 };
5357
5358         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
5359                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5360                 fops->counter_free(dev, cnt);
5361                 return;
5362         }
5363         DRV_LOG(ERR,
5364                 "port %u counter free is not supported.",
5365                  dev->data->port_id);
5366 }
5367
5368 /**
5369  * Query counter statistics.
5370  *
5371  * @param[in] dev
5372  *   Pointer to Ethernet device structure.
5373  * @param[in] cnt
5374  *   Pointer to counter to query.
5375  * @param[in] clear
5376  *   Set to clear counter statistics.
5377  * @param[out] pkts
5378  *   The counter hits packets number to save.
5379  * @param[out] bytes
5380  *   The counter hits bytes number to save.
5381  *
5382  * @return
5383  *   0 on success, a negative errno value otherwise.
5384  */
5385 int
5386 mlx5_counter_query(struct rte_eth_dev *dev, struct mlx5_flow_counter *cnt,
5387                    bool clear, uint64_t *pkts, uint64_t *bytes)
5388 {
5389         const struct mlx5_flow_driver_ops *fops;
5390         struct rte_flow_attr attr = { .transfer = 0 };
5391
5392         if (flow_get_drv_type(dev, &attr) == MLX5_FLOW_TYPE_DV) {
5393                 fops = flow_get_drv_ops(MLX5_FLOW_TYPE_DV);
5394                 return fops->counter_query(dev, cnt, clear, pkts, bytes);
5395         }
5396         DRV_LOG(ERR,
5397                 "port %u counter query is not supported.",
5398                  dev->data->port_id);
5399         return -ENOTSUP;
5400 }
5401
5402 #define MLX5_POOL_QUERY_FREQ_US 1000000
5403
5404 /**
5405  * Set the periodic procedure for triggering asynchronous batch queries for all
5406  * the counter pools.
5407  *
5408  * @param[in] sh
5409  *   Pointer to mlx5_ibv_shared object.
5410  */
5411 void
5412 mlx5_set_query_alarm(struct mlx5_ibv_shared *sh)
5413 {
5414         struct mlx5_pools_container *cont = MLX5_CNT_CONTAINER(sh, 0, 0);
5415         uint32_t pools_n = rte_atomic16_read(&cont->n_valid);
5416         uint32_t us;
5417
5418         cont = MLX5_CNT_CONTAINER(sh, 1, 0);
5419         pools_n += rte_atomic16_read(&cont->n_valid);
5420         us = MLX5_POOL_QUERY_FREQ_US / pools_n;
5421         DRV_LOG(DEBUG, "Set alarm for %u pools each %u us", pools_n, us);
5422         if (rte_eal_alarm_set(us, mlx5_flow_query_alarm, sh)) {
5423                 sh->cmng.query_thread_on = 0;
5424                 DRV_LOG(ERR, "Cannot reinitialize query alarm");
5425         } else {
5426                 sh->cmng.query_thread_on = 1;
5427         }
5428 }
5429
5430 /**
5431  * The periodic procedure for triggering asynchronous batch queries for all the
5432  * counter pools. This function is probably called by the host thread.
5433  *
5434  * @param[in] arg
5435  *   The parameter for the alarm process.
5436  */
5437 void
5438 mlx5_flow_query_alarm(void *arg)
5439 {
5440         struct mlx5_ibv_shared *sh = arg;
5441         struct mlx5_devx_obj *dcs;
5442         uint16_t offset;
5443         int ret;
5444         uint8_t batch = sh->cmng.batch;
5445         uint16_t pool_index = sh->cmng.pool_index;
5446         struct mlx5_pools_container *cont;
5447         struct mlx5_pools_container *mcont;
5448         struct mlx5_flow_counter_pool *pool;
5449
5450         if (sh->cmng.pending_queries >= MLX5_MAX_PENDING_QUERIES)
5451                 goto set_alarm;
5452 next_container:
5453         cont = MLX5_CNT_CONTAINER(sh, batch, 1);
5454         mcont = MLX5_CNT_CONTAINER(sh, batch, 0);
5455         /* Check if resize was done and need to flip a container. */
5456         if (cont != mcont) {
5457                 if (cont->pools) {
5458                         /* Clean the old container. */
5459                         rte_free(cont->pools);
5460                         memset(cont, 0, sizeof(*cont));
5461                 }
5462                 rte_cio_wmb();
5463                  /* Flip the host container. */
5464                 sh->cmng.mhi[batch] ^= (uint8_t)2;
5465                 cont = mcont;
5466         }
5467         if (!cont->pools) {
5468                 /* 2 empty containers case is unexpected. */
5469                 if (unlikely(batch != sh->cmng.batch))
5470                         goto set_alarm;
5471                 batch ^= 0x1;
5472                 pool_index = 0;
5473                 goto next_container;
5474         }
5475         pool = cont->pools[pool_index];
5476         if (pool->raw_hw)
5477                 /* There is a pool query in progress. */
5478                 goto set_alarm;
5479         pool->raw_hw =
5480                 LIST_FIRST(&sh->cmng.free_stat_raws);
5481         if (!pool->raw_hw)
5482                 /* No free counter statistics raw memory. */
5483                 goto set_alarm;
5484         dcs = (struct mlx5_devx_obj *)(uintptr_t)rte_atomic64_read
5485                                                               (&pool->a64_dcs);
5486         offset = batch ? 0 : dcs->id % MLX5_COUNTERS_PER_POOL;
5487         ret = mlx5_devx_cmd_flow_counter_query(dcs, 0, MLX5_COUNTERS_PER_POOL -
5488                                                offset, NULL, NULL,
5489                                                pool->raw_hw->mem_mng->dm->id,
5490                                                (void *)(uintptr_t)
5491                                                (pool->raw_hw->data + offset),
5492                                                sh->devx_comp,
5493                                                (uint64_t)(uintptr_t)pool);
5494         if (ret) {
5495                 DRV_LOG(ERR, "Failed to trigger asynchronous query for dcs ID"
5496                         " %d", pool->min_dcs->id);
5497                 pool->raw_hw = NULL;
5498                 goto set_alarm;
5499         }
5500         pool->raw_hw->min_dcs_id = dcs->id;
5501         LIST_REMOVE(pool->raw_hw, next);
5502         sh->cmng.pending_queries++;
5503         pool_index++;
5504         if (pool_index >= rte_atomic16_read(&cont->n_valid)) {
5505                 batch ^= 0x1;
5506                 pool_index = 0;
5507         }
5508 set_alarm:
5509         sh->cmng.batch = batch;
5510         sh->cmng.pool_index = pool_index;
5511         mlx5_set_query_alarm(sh);
5512 }
5513
5514 /**
5515  * Handler for the HW respond about ready values from an asynchronous batch
5516  * query. This function is probably called by the host thread.
5517  *
5518  * @param[in] sh
5519  *   The pointer to the shared IB device context.
5520  * @param[in] async_id
5521  *   The Devx async ID.
5522  * @param[in] status
5523  *   The status of the completion.
5524  */
5525 void
5526 mlx5_flow_async_pool_query_handle(struct mlx5_ibv_shared *sh,
5527                                   uint64_t async_id, int status)
5528 {
5529         struct mlx5_flow_counter_pool *pool =
5530                 (struct mlx5_flow_counter_pool *)(uintptr_t)async_id;
5531         struct mlx5_counter_stats_raw *raw_to_free;
5532
5533         if (unlikely(status)) {
5534                 raw_to_free = pool->raw_hw;
5535         } else {
5536                 raw_to_free = pool->raw;
5537                 rte_spinlock_lock(&pool->sl);
5538                 pool->raw = pool->raw_hw;
5539                 rte_spinlock_unlock(&pool->sl);
5540                 rte_atomic64_add(&pool->query_gen, 1);
5541                 /* Be sure the new raw counters data is updated in memory. */
5542                 rte_cio_wmb();
5543         }
5544         LIST_INSERT_HEAD(&sh->cmng.free_stat_raws, raw_to_free, next);
5545         pool->raw_hw = NULL;
5546         sh->cmng.pending_queries--;
5547 }
5548
5549 /**
5550  * Translate the rte_flow group index to HW table value.
5551  *
5552  * @param[in] attributes
5553  *   Pointer to flow attributes
5554  * @param[in] external
5555  *   Value is part of flow rule created by request external to PMD.
5556  * @param[in] group
5557  *   rte_flow group index value.
5558  * @param[out] table
5559  *   HW table value.
5560  * @param[out] error
5561  *   Pointer to error structure.
5562  *
5563  * @return
5564  *   0 on success, a negative errno value otherwise and rte_errno is set.
5565  */
5566 int
5567 mlx5_flow_group_to_table(const struct rte_flow_attr *attributes, bool external,
5568                          uint32_t group, uint32_t *table,
5569                          struct rte_flow_error *error)
5570 {
5571         if (attributes->transfer && external) {
5572                 if (group == UINT32_MAX)
5573                         return rte_flow_error_set
5574                                                 (error, EINVAL,
5575                                                  RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
5576                                                  NULL,
5577                                                  "group index not supported");
5578                 *table = group + 1;
5579         } else {
5580                 *table = group;
5581         }
5582         return 0;
5583 }
5584
5585 /**
5586  * Discover availability of metadata reg_c's.
5587  *
5588  * Iteratively use test flows to check availability.
5589  *
5590  * @param[in] dev
5591  *   Pointer to the Ethernet device structure.
5592  *
5593  * @return
5594  *   0 on success, a negative errno value otherwise and rte_errno is set.
5595  */
5596 int
5597 mlx5_flow_discover_mreg_c(struct rte_eth_dev *dev)
5598 {
5599         struct mlx5_priv *priv = dev->data->dev_private;
5600         struct mlx5_dev_config *config = &priv->config;
5601         enum modify_reg idx;
5602         int n = 0;
5603
5604         /* reg_c[0] and reg_c[1] are reserved. */
5605         config->flow_mreg_c[n++] = REG_C_0;
5606         config->flow_mreg_c[n++] = REG_C_1;
5607         /* Discover availability of other reg_c's. */
5608         for (idx = REG_C_2; idx <= REG_C_7; ++idx) {
5609                 struct rte_flow_attr attr = {
5610                         .group = MLX5_FLOW_MREG_CP_TABLE_GROUP,
5611                         .priority = MLX5_FLOW_PRIO_RSVD,
5612                         .ingress = 1,
5613                 };
5614                 struct rte_flow_item items[] = {
5615                         [0] = {
5616                                 .type = RTE_FLOW_ITEM_TYPE_END,
5617                         },
5618                 };
5619                 struct rte_flow_action actions[] = {
5620                         [0] = {
5621                                 .type = MLX5_RTE_FLOW_ACTION_TYPE_COPY_MREG,
5622                                 .conf = &(struct mlx5_flow_action_copy_mreg){
5623                                         .src = REG_C_1,
5624                                         .dst = idx,
5625                                 },
5626                         },
5627                         [1] = {
5628                                 .type = RTE_FLOW_ACTION_TYPE_JUMP,
5629                                 .conf = &(struct rte_flow_action_jump){
5630                                         .group = MLX5_FLOW_MREG_ACT_TABLE_GROUP,
5631                                 },
5632                         },
5633                         [2] = {
5634                                 .type = RTE_FLOW_ACTION_TYPE_END,
5635                         },
5636                 };
5637                 struct rte_flow *flow;
5638                 struct rte_flow_error error;
5639
5640                 if (!config->dv_flow_en)
5641                         break;
5642                 /* Create internal flow, validation skips copy action. */
5643                 flow = flow_list_create(dev, NULL, &attr, items,
5644                                         actions, false, &error);
5645                 if (!flow)
5646                         continue;
5647                 if (dev->data->dev_started || !flow_drv_apply(dev, flow, NULL))
5648                         config->flow_mreg_c[n++] = idx;
5649                 flow_list_destroy(dev, NULL, flow);
5650         }
5651         for (; n < MLX5_MREG_C_NUM; ++n)
5652                 config->flow_mreg_c[n] = REG_NONE;
5653         return 0;
5654 }