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