net/mlx: prefix private structure
[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_eth_ctrl.h>
25 #include <rte_ethdev_driver.h>
26 #include <rte_flow.h>
27 #include <rte_flow_driver.h>
28 #include <rte_malloc.h>
29 #include <rte_ip.h>
30
31 #include "mlx5.h"
32 #include "mlx5_defs.h"
33 #include "mlx5_prm.h"
34 #include "mlx5_glue.h"
35 #include "mlx5_flow.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_tcf_drv_ops;
46 extern const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops;
47
48 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops;
49
50 const struct mlx5_flow_driver_ops *flow_drv_ops[] = {
51         [MLX5_FLOW_TYPE_MIN] = &mlx5_flow_null_drv_ops,
52 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
53         [MLX5_FLOW_TYPE_DV] = &mlx5_flow_dv_drv_ops,
54 #endif
55         [MLX5_FLOW_TYPE_TCF] = &mlx5_flow_tcf_drv_ops,
56         [MLX5_FLOW_TYPE_VERBS] = &mlx5_flow_verbs_drv_ops,
57         [MLX5_FLOW_TYPE_MAX] = &mlx5_flow_null_drv_ops
58 };
59
60 enum mlx5_expansion {
61         MLX5_EXPANSION_ROOT,
62         MLX5_EXPANSION_ROOT_OUTER,
63         MLX5_EXPANSION_ROOT_ETH_VLAN,
64         MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN,
65         MLX5_EXPANSION_OUTER_ETH,
66         MLX5_EXPANSION_OUTER_ETH_VLAN,
67         MLX5_EXPANSION_OUTER_VLAN,
68         MLX5_EXPANSION_OUTER_IPV4,
69         MLX5_EXPANSION_OUTER_IPV4_UDP,
70         MLX5_EXPANSION_OUTER_IPV4_TCP,
71         MLX5_EXPANSION_OUTER_IPV6,
72         MLX5_EXPANSION_OUTER_IPV6_UDP,
73         MLX5_EXPANSION_OUTER_IPV6_TCP,
74         MLX5_EXPANSION_VXLAN,
75         MLX5_EXPANSION_VXLAN_GPE,
76         MLX5_EXPANSION_GRE,
77         MLX5_EXPANSION_MPLS,
78         MLX5_EXPANSION_ETH,
79         MLX5_EXPANSION_ETH_VLAN,
80         MLX5_EXPANSION_VLAN,
81         MLX5_EXPANSION_IPV4,
82         MLX5_EXPANSION_IPV4_UDP,
83         MLX5_EXPANSION_IPV4_TCP,
84         MLX5_EXPANSION_IPV6,
85         MLX5_EXPANSION_IPV6_UDP,
86         MLX5_EXPANSION_IPV6_TCP,
87 };
88
89 /** Supported expansion of items. */
90 static const struct rte_flow_expand_node mlx5_support_expansion[] = {
91         [MLX5_EXPANSION_ROOT] = {
92                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
93                                                  MLX5_EXPANSION_IPV4,
94                                                  MLX5_EXPANSION_IPV6),
95                 .type = RTE_FLOW_ITEM_TYPE_END,
96         },
97         [MLX5_EXPANSION_ROOT_OUTER] = {
98                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH,
99                                                  MLX5_EXPANSION_OUTER_IPV4,
100                                                  MLX5_EXPANSION_OUTER_IPV6),
101                 .type = RTE_FLOW_ITEM_TYPE_END,
102         },
103         [MLX5_EXPANSION_ROOT_ETH_VLAN] = {
104                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH_VLAN),
105                 .type = RTE_FLOW_ITEM_TYPE_END,
106         },
107         [MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN] = {
108                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_ETH_VLAN),
109                 .type = RTE_FLOW_ITEM_TYPE_END,
110         },
111         [MLX5_EXPANSION_OUTER_ETH] = {
112                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
113                                                  MLX5_EXPANSION_OUTER_IPV6,
114                                                  MLX5_EXPANSION_MPLS),
115                 .type = RTE_FLOW_ITEM_TYPE_ETH,
116                 .rss_types = 0,
117         },
118         [MLX5_EXPANSION_OUTER_ETH_VLAN] = {
119                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_VLAN),
120                 .type = RTE_FLOW_ITEM_TYPE_ETH,
121                 .rss_types = 0,
122         },
123         [MLX5_EXPANSION_OUTER_VLAN] = {
124                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_OUTER_IPV4,
125                                                  MLX5_EXPANSION_OUTER_IPV6),
126                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
127         },
128         [MLX5_EXPANSION_OUTER_IPV4] = {
129                 .next = RTE_FLOW_EXPAND_RSS_NEXT
130                         (MLX5_EXPANSION_OUTER_IPV4_UDP,
131                          MLX5_EXPANSION_OUTER_IPV4_TCP,
132                          MLX5_EXPANSION_GRE),
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                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
152                 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
153                         ETH_RSS_NONFRAG_IPV6_OTHER,
154         },
155         [MLX5_EXPANSION_OUTER_IPV6_UDP] = {
156                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VXLAN,
157                                                  MLX5_EXPANSION_VXLAN_GPE),
158                 .type = RTE_FLOW_ITEM_TYPE_UDP,
159                 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
160         },
161         [MLX5_EXPANSION_OUTER_IPV6_TCP] = {
162                 .type = RTE_FLOW_ITEM_TYPE_TCP,
163                 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
164         },
165         [MLX5_EXPANSION_VXLAN] = {
166                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH),
167                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
168         },
169         [MLX5_EXPANSION_VXLAN_GPE] = {
170                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_ETH,
171                                                  MLX5_EXPANSION_IPV4,
172                                                  MLX5_EXPANSION_IPV6),
173                 .type = RTE_FLOW_ITEM_TYPE_VXLAN_GPE,
174         },
175         [MLX5_EXPANSION_GRE] = {
176                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4),
177                 .type = RTE_FLOW_ITEM_TYPE_GRE,
178         },
179         [MLX5_EXPANSION_MPLS] = {
180                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
181                                                  MLX5_EXPANSION_IPV6),
182                 .type = RTE_FLOW_ITEM_TYPE_MPLS,
183         },
184         [MLX5_EXPANSION_ETH] = {
185                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
186                                                  MLX5_EXPANSION_IPV6),
187                 .type = RTE_FLOW_ITEM_TYPE_ETH,
188         },
189         [MLX5_EXPANSION_ETH_VLAN] = {
190                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_VLAN),
191                 .type = RTE_FLOW_ITEM_TYPE_ETH,
192         },
193         [MLX5_EXPANSION_VLAN] = {
194                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4,
195                                                  MLX5_EXPANSION_IPV6),
196                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
197         },
198         [MLX5_EXPANSION_IPV4] = {
199                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV4_UDP,
200                                                  MLX5_EXPANSION_IPV4_TCP),
201                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
202                 .rss_types = ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
203                         ETH_RSS_NONFRAG_IPV4_OTHER,
204         },
205         [MLX5_EXPANSION_IPV4_UDP] = {
206                 .type = RTE_FLOW_ITEM_TYPE_UDP,
207                 .rss_types = ETH_RSS_NONFRAG_IPV4_UDP,
208         },
209         [MLX5_EXPANSION_IPV4_TCP] = {
210                 .type = RTE_FLOW_ITEM_TYPE_TCP,
211                 .rss_types = ETH_RSS_NONFRAG_IPV4_TCP,
212         },
213         [MLX5_EXPANSION_IPV6] = {
214                 .next = RTE_FLOW_EXPAND_RSS_NEXT(MLX5_EXPANSION_IPV6_UDP,
215                                                  MLX5_EXPANSION_IPV6_TCP),
216                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
217                 .rss_types = ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
218                         ETH_RSS_NONFRAG_IPV6_OTHER,
219         },
220         [MLX5_EXPANSION_IPV6_UDP] = {
221                 .type = RTE_FLOW_ITEM_TYPE_UDP,
222                 .rss_types = ETH_RSS_NONFRAG_IPV6_UDP,
223         },
224         [MLX5_EXPANSION_IPV6_TCP] = {
225                 .type = RTE_FLOW_ITEM_TYPE_TCP,
226                 .rss_types = ETH_RSS_NONFRAG_IPV6_TCP,
227         },
228 };
229
230 static const struct rte_flow_ops mlx5_flow_ops = {
231         .validate = mlx5_flow_validate,
232         .create = mlx5_flow_create,
233         .destroy = mlx5_flow_destroy,
234         .flush = mlx5_flow_flush,
235         .isolate = mlx5_flow_isolate,
236         .query = mlx5_flow_query,
237 };
238
239 /* Convert FDIR request to Generic flow. */
240 struct mlx5_fdir {
241         struct rte_flow_attr attr;
242         struct rte_flow_item items[4];
243         struct rte_flow_item_eth l2;
244         struct rte_flow_item_eth l2_mask;
245         union {
246                 struct rte_flow_item_ipv4 ipv4;
247                 struct rte_flow_item_ipv6 ipv6;
248         } l3;
249         union {
250                 struct rte_flow_item_ipv4 ipv4;
251                 struct rte_flow_item_ipv6 ipv6;
252         } l3_mask;
253         union {
254                 struct rte_flow_item_udp udp;
255                 struct rte_flow_item_tcp tcp;
256         } l4;
257         union {
258                 struct rte_flow_item_udp udp;
259                 struct rte_flow_item_tcp tcp;
260         } l4_mask;
261         struct rte_flow_action actions[2];
262         struct rte_flow_action_queue queue;
263 };
264
265 /* Map of Verbs to Flow priority with 8 Verbs priorities. */
266 static const uint32_t priority_map_3[][MLX5_PRIORITY_MAP_MAX] = {
267         { 0, 1, 2 }, { 2, 3, 4 }, { 5, 6, 7 },
268 };
269
270 /* Map of Verbs to Flow priority with 16 Verbs priorities. */
271 static const uint32_t priority_map_5[][MLX5_PRIORITY_MAP_MAX] = {
272         { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
273         { 9, 10, 11 }, { 12, 13, 14 },
274 };
275
276 /* Tunnel information. */
277 struct mlx5_flow_tunnel_info {
278         uint64_t tunnel; /**< Tunnel bit (see MLX5_FLOW_*). */
279         uint32_t ptype; /**< Tunnel Ptype (see RTE_PTYPE_*). */
280 };
281
282 static struct mlx5_flow_tunnel_info tunnels_info[] = {
283         {
284                 .tunnel = MLX5_FLOW_LAYER_VXLAN,
285                 .ptype = RTE_PTYPE_TUNNEL_VXLAN | RTE_PTYPE_L4_UDP,
286         },
287         {
288                 .tunnel = MLX5_FLOW_LAYER_VXLAN_GPE,
289                 .ptype = RTE_PTYPE_TUNNEL_VXLAN_GPE | RTE_PTYPE_L4_UDP,
290         },
291         {
292                 .tunnel = MLX5_FLOW_LAYER_GRE,
293                 .ptype = RTE_PTYPE_TUNNEL_GRE,
294         },
295         {
296                 .tunnel = MLX5_FLOW_LAYER_MPLS | MLX5_FLOW_LAYER_OUTER_L4_UDP,
297                 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_UDP | RTE_PTYPE_L4_UDP,
298         },
299         {
300                 .tunnel = MLX5_FLOW_LAYER_MPLS,
301                 .ptype = RTE_PTYPE_TUNNEL_MPLS_IN_GRE,
302         },
303 };
304
305 /**
306  * Discover the maximum number of priority available.
307  *
308  * @param[in] dev
309  *   Pointer to the Ethernet device structure.
310  *
311  * @return
312  *   number of supported flow priority on success, a negative errno
313  *   value otherwise and rte_errno is set.
314  */
315 int
316 mlx5_flow_discover_priorities(struct rte_eth_dev *dev)
317 {
318         struct {
319                 struct ibv_flow_attr attr;
320                 struct ibv_flow_spec_eth eth;
321                 struct ibv_flow_spec_action_drop drop;
322         } flow_attr = {
323                 .attr = {
324                         .num_of_specs = 2,
325                 },
326                 .eth = {
327                         .type = IBV_FLOW_SPEC_ETH,
328                         .size = sizeof(struct ibv_flow_spec_eth),
329                 },
330                 .drop = {
331                         .size = sizeof(struct ibv_flow_spec_action_drop),
332                         .type = IBV_FLOW_SPEC_ACTION_DROP,
333                 },
334         };
335         struct ibv_flow *flow;
336         struct mlx5_hrxq *drop = mlx5_hrxq_drop_new(dev);
337         uint16_t vprio[] = { 8, 16 };
338         int i;
339         int priority = 0;
340
341         if (!drop) {
342                 rte_errno = ENOTSUP;
343                 return -rte_errno;
344         }
345         for (i = 0; i != RTE_DIM(vprio); i++) {
346                 flow_attr.attr.priority = vprio[i] - 1;
347                 flow = mlx5_glue->create_flow(drop->qp, &flow_attr.attr);
348                 if (!flow)
349                         break;
350                 claim_zero(mlx5_glue->destroy_flow(flow));
351                 priority = vprio[i];
352         }
353         switch (priority) {
354         case 8:
355                 priority = RTE_DIM(priority_map_3);
356                 break;
357         case 16:
358                 priority = RTE_DIM(priority_map_5);
359                 break;
360         default:
361                 rte_errno = ENOTSUP;
362                 DRV_LOG(ERR,
363                         "port %u verbs maximum priority: %d expected 8/16",
364                         dev->data->port_id, vprio[i]);
365                 return -rte_errno;
366         }
367         mlx5_hrxq_drop_release(dev);
368         DRV_LOG(INFO, "port %u flow maximum priority: %d",
369                 dev->data->port_id, priority);
370         return priority;
371 }
372
373 /**
374  * Adjust flow priority based on the highest layer and the request priority.
375  *
376  * @param[in] dev
377  *   Pointer to the Ethernet device structure.
378  * @param[in] priority
379  *   The rule base priority.
380  * @param[in] subpriority
381  *   The priority based on the items.
382  *
383  * @return
384  *   The new priority.
385  */
386 uint32_t mlx5_flow_adjust_priority(struct rte_eth_dev *dev, int32_t priority,
387                                    uint32_t subpriority)
388 {
389         uint32_t res = 0;
390         struct mlx5_priv *priv = dev->data->dev_private;
391
392         switch (priv->config.flow_prio) {
393         case RTE_DIM(priority_map_3):
394                 res = priority_map_3[priority][subpriority];
395                 break;
396         case RTE_DIM(priority_map_5):
397                 res = priority_map_5[priority][subpriority];
398                 break;
399         }
400         return  res;
401 }
402
403 /**
404  * Verify the @p item specifications (spec, last, mask) are compatible with the
405  * NIC capabilities.
406  *
407  * @param[in] item
408  *   Item specification.
409  * @param[in] mask
410  *   @p item->mask or flow default bit-masks.
411  * @param[in] nic_mask
412  *   Bit-masks covering supported fields by the NIC to compare with user mask.
413  * @param[in] size
414  *   Bit-masks size in bytes.
415  * @param[out] error
416  *   Pointer to error structure.
417  *
418  * @return
419  *   0 on success, a negative errno value otherwise and rte_errno is set.
420  */
421 int
422 mlx5_flow_item_acceptable(const struct rte_flow_item *item,
423                           const uint8_t *mask,
424                           const uint8_t *nic_mask,
425                           unsigned int size,
426                           struct rte_flow_error *error)
427 {
428         unsigned int i;
429
430         assert(nic_mask);
431         for (i = 0; i < size; ++i)
432                 if ((nic_mask[i] | mask[i]) != nic_mask[i])
433                         return rte_flow_error_set(error, ENOTSUP,
434                                                   RTE_FLOW_ERROR_TYPE_ITEM,
435                                                   item,
436                                                   "mask enables non supported"
437                                                   " bits");
438         if (!item->spec && (item->mask || item->last))
439                 return rte_flow_error_set(error, EINVAL,
440                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
441                                           "mask/last without a spec is not"
442                                           " supported");
443         if (item->spec && item->last) {
444                 uint8_t spec[size];
445                 uint8_t last[size];
446                 unsigned int i;
447                 int ret;
448
449                 for (i = 0; i < size; ++i) {
450                         spec[i] = ((const uint8_t *)item->spec)[i] & mask[i];
451                         last[i] = ((const uint8_t *)item->last)[i] & mask[i];
452                 }
453                 ret = memcmp(spec, last, size);
454                 if (ret != 0)
455                         return rte_flow_error_set(error, EINVAL,
456                                                   RTE_FLOW_ERROR_TYPE_ITEM,
457                                                   item,
458                                                   "range is not valid");
459         }
460         return 0;
461 }
462
463 /**
464  * Adjust the hash fields according to the @p flow information.
465  *
466  * @param[in] dev_flow.
467  *   Pointer to the mlx5_flow.
468  * @param[in] tunnel
469  *   1 when the hash field is for a tunnel item.
470  * @param[in] layer_types
471  *   ETH_RSS_* types.
472  * @param[in] hash_fields
473  *   Item hash fields.
474  *
475  * @return
476  *   The hash fileds that should be used.
477  */
478 uint64_t
479 mlx5_flow_hashfields_adjust(struct mlx5_flow *dev_flow,
480                             int tunnel __rte_unused, uint64_t layer_types,
481                             uint64_t hash_fields)
482 {
483         struct rte_flow *flow = dev_flow->flow;
484 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
485         int rss_request_inner = flow->rss.level >= 2;
486
487         /* Check RSS hash level for tunnel. */
488         if (tunnel && rss_request_inner)
489                 hash_fields |= IBV_RX_HASH_INNER;
490         else if (tunnel || rss_request_inner)
491                 return 0;
492 #endif
493         /* Check if requested layer matches RSS hash fields. */
494         if (!(flow->rss.types & layer_types))
495                 return 0;
496         return hash_fields;
497 }
498
499 /**
500  * Lookup and set the ptype in the data Rx part.  A single Ptype can be used,
501  * if several tunnel rules are used on this queue, the tunnel ptype will be
502  * cleared.
503  *
504  * @param rxq_ctrl
505  *   Rx queue to update.
506  */
507 static void
508 flow_rxq_tunnel_ptype_update(struct mlx5_rxq_ctrl *rxq_ctrl)
509 {
510         unsigned int i;
511         uint32_t tunnel_ptype = 0;
512
513         /* Look up for the ptype to use. */
514         for (i = 0; i != MLX5_FLOW_TUNNEL; ++i) {
515                 if (!rxq_ctrl->flow_tunnels_n[i])
516                         continue;
517                 if (!tunnel_ptype) {
518                         tunnel_ptype = tunnels_info[i].ptype;
519                 } else {
520                         tunnel_ptype = 0;
521                         break;
522                 }
523         }
524         rxq_ctrl->rxq.tunnel = tunnel_ptype;
525 }
526
527 /**
528  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) according to the devive
529  * flow.
530  *
531  * @param[in] dev
532  *   Pointer to the Ethernet device structure.
533  * @param[in] dev_flow
534  *   Pointer to device flow structure.
535  */
536 static void
537 flow_drv_rxq_flags_set(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow)
538 {
539         struct mlx5_priv *priv = dev->data->dev_private;
540         struct rte_flow *flow = dev_flow->flow;
541         const int mark = !!(flow->actions &
542                             (MLX5_FLOW_ACTION_FLAG | MLX5_FLOW_ACTION_MARK));
543         const int tunnel = !!(dev_flow->layers & MLX5_FLOW_LAYER_TUNNEL);
544         unsigned int i;
545
546         for (i = 0; i != flow->rss.queue_num; ++i) {
547                 int idx = (*flow->queue)[i];
548                 struct mlx5_rxq_ctrl *rxq_ctrl =
549                         container_of((*priv->rxqs)[idx],
550                                      struct mlx5_rxq_ctrl, rxq);
551
552                 if (mark) {
553                         rxq_ctrl->rxq.mark = 1;
554                         rxq_ctrl->flow_mark_n++;
555                 }
556                 if (tunnel) {
557                         unsigned int j;
558
559                         /* Increase the counter matching the flow. */
560                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
561                                 if ((tunnels_info[j].tunnel &
562                                      dev_flow->layers) ==
563                                     tunnels_info[j].tunnel) {
564                                         rxq_ctrl->flow_tunnels_n[j]++;
565                                         break;
566                                 }
567                         }
568                         flow_rxq_tunnel_ptype_update(rxq_ctrl);
569                 }
570         }
571 }
572
573 /**
574  * Set the Rx queue flags (Mark/Flag and Tunnel Ptypes) for a flow
575  *
576  * @param[in] dev
577  *   Pointer to the Ethernet device structure.
578  * @param[in] flow
579  *   Pointer to flow structure.
580  */
581 static void
582 flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
583 {
584         struct mlx5_flow *dev_flow;
585
586         LIST_FOREACH(dev_flow, &flow->dev_flows, next)
587                 flow_drv_rxq_flags_set(dev, dev_flow);
588 }
589
590 /**
591  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
592  * device flow if no other flow uses it with the same kind of request.
593  *
594  * @param dev
595  *   Pointer to Ethernet device.
596  * @param[in] dev_flow
597  *   Pointer to the device flow.
598  */
599 static void
600 flow_drv_rxq_flags_trim(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow)
601 {
602         struct mlx5_priv *priv = dev->data->dev_private;
603         struct rte_flow *flow = dev_flow->flow;
604         const int mark = !!(flow->actions &
605                             (MLX5_FLOW_ACTION_FLAG | MLX5_FLOW_ACTION_MARK));
606         const int tunnel = !!(dev_flow->layers & MLX5_FLOW_LAYER_TUNNEL);
607         unsigned int i;
608
609         assert(dev->data->dev_started);
610         for (i = 0; i != flow->rss.queue_num; ++i) {
611                 int idx = (*flow->queue)[i];
612                 struct mlx5_rxq_ctrl *rxq_ctrl =
613                         container_of((*priv->rxqs)[idx],
614                                      struct mlx5_rxq_ctrl, rxq);
615
616                 if (mark) {
617                         rxq_ctrl->flow_mark_n--;
618                         rxq_ctrl->rxq.mark = !!rxq_ctrl->flow_mark_n;
619                 }
620                 if (tunnel) {
621                         unsigned int j;
622
623                         /* Decrease the counter matching the flow. */
624                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
625                                 if ((tunnels_info[j].tunnel &
626                                      dev_flow->layers) ==
627                                     tunnels_info[j].tunnel) {
628                                         rxq_ctrl->flow_tunnels_n[j]--;
629                                         break;
630                                 }
631                         }
632                         flow_rxq_tunnel_ptype_update(rxq_ctrl);
633                 }
634         }
635 }
636
637 /**
638  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
639  * @p flow if no other flow uses it with the same kind of request.
640  *
641  * @param dev
642  *   Pointer to Ethernet device.
643  * @param[in] flow
644  *   Pointer to the flow.
645  */
646 static void
647 flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
648 {
649         struct mlx5_flow *dev_flow;
650
651         LIST_FOREACH(dev_flow, &flow->dev_flows, next)
652                 flow_drv_rxq_flags_trim(dev, dev_flow);
653 }
654
655 /**
656  * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
657  *
658  * @param dev
659  *   Pointer to Ethernet device.
660  */
661 static void
662 flow_rxq_flags_clear(struct rte_eth_dev *dev)
663 {
664         struct mlx5_priv *priv = dev->data->dev_private;
665         unsigned int i;
666
667         for (i = 0; i != priv->rxqs_n; ++i) {
668                 struct mlx5_rxq_ctrl *rxq_ctrl;
669                 unsigned int j;
670
671                 if (!(*priv->rxqs)[i])
672                         continue;
673                 rxq_ctrl = container_of((*priv->rxqs)[i],
674                                         struct mlx5_rxq_ctrl, rxq);
675                 rxq_ctrl->flow_mark_n = 0;
676                 rxq_ctrl->rxq.mark = 0;
677                 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
678                         rxq_ctrl->flow_tunnels_n[j] = 0;
679                 rxq_ctrl->rxq.tunnel = 0;
680         }
681 }
682
683 /*
684  * Validate the flag action.
685  *
686  * @param[in] action_flags
687  *   Bit-fields that holds the actions detected until now.
688  * @param[in] attr
689  *   Attributes of flow that includes this action.
690  * @param[out] error
691  *   Pointer to error structure.
692  *
693  * @return
694  *   0 on success, a negative errno value otherwise and rte_errno is set.
695  */
696 int
697 mlx5_flow_validate_action_flag(uint64_t action_flags,
698                                const struct rte_flow_attr *attr,
699                                struct rte_flow_error *error)
700 {
701
702         if (action_flags & MLX5_FLOW_ACTION_DROP)
703                 return rte_flow_error_set(error, EINVAL,
704                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
705                                           "can't drop and flag in same flow");
706         if (action_flags & MLX5_FLOW_ACTION_MARK)
707                 return rte_flow_error_set(error, EINVAL,
708                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
709                                           "can't mark and flag in same flow");
710         if (action_flags & MLX5_FLOW_ACTION_FLAG)
711                 return rte_flow_error_set(error, EINVAL,
712                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
713                                           "can't have 2 flag"
714                                           " actions in same flow");
715         if (attr->egress)
716                 return rte_flow_error_set(error, ENOTSUP,
717                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
718                                           "flag action not supported for "
719                                           "egress");
720         return 0;
721 }
722
723 /*
724  * Validate the mark action.
725  *
726  * @param[in] action
727  *   Pointer to the queue action.
728  * @param[in] action_flags
729  *   Bit-fields that holds the actions detected until now.
730  * @param[in] attr
731  *   Attributes of flow that includes this action.
732  * @param[out] error
733  *   Pointer to error structure.
734  *
735  * @return
736  *   0 on success, a negative errno value otherwise and rte_errno is set.
737  */
738 int
739 mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
740                                uint64_t action_flags,
741                                const struct rte_flow_attr *attr,
742                                struct rte_flow_error *error)
743 {
744         const struct rte_flow_action_mark *mark = action->conf;
745
746         if (!mark)
747                 return rte_flow_error_set(error, EINVAL,
748                                           RTE_FLOW_ERROR_TYPE_ACTION,
749                                           action,
750                                           "configuration cannot be null");
751         if (mark->id >= MLX5_FLOW_MARK_MAX)
752                 return rte_flow_error_set(error, EINVAL,
753                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
754                                           &mark->id,
755                                           "mark id must in 0 <= id < "
756                                           RTE_STR(MLX5_FLOW_MARK_MAX));
757         if (action_flags & MLX5_FLOW_ACTION_DROP)
758                 return rte_flow_error_set(error, EINVAL,
759                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
760                                           "can't drop and mark in same flow");
761         if (action_flags & MLX5_FLOW_ACTION_FLAG)
762                 return rte_flow_error_set(error, EINVAL,
763                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
764                                           "can't flag and mark in same flow");
765         if (action_flags & MLX5_FLOW_ACTION_MARK)
766                 return rte_flow_error_set(error, EINVAL,
767                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
768                                           "can't have 2 mark actions in same"
769                                           " flow");
770         if (attr->egress)
771                 return rte_flow_error_set(error, ENOTSUP,
772                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
773                                           "mark action not supported for "
774                                           "egress");
775         return 0;
776 }
777
778 /*
779  * Validate the drop action.
780  *
781  * @param[in] action_flags
782  *   Bit-fields that holds the actions detected until now.
783  * @param[in] attr
784  *   Attributes of flow that includes this action.
785  * @param[out] error
786  *   Pointer to error structure.
787  *
788  * @return
789  *   0 on success, a negative errno value otherwise and rte_ernno is set.
790  */
791 int
792 mlx5_flow_validate_action_drop(uint64_t action_flags,
793                                const struct rte_flow_attr *attr,
794                                struct rte_flow_error *error)
795 {
796         if (action_flags & MLX5_FLOW_ACTION_FLAG)
797                 return rte_flow_error_set(error, EINVAL,
798                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
799                                           "can't drop and flag in same flow");
800         if (action_flags & MLX5_FLOW_ACTION_MARK)
801                 return rte_flow_error_set(error, EINVAL,
802                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
803                                           "can't drop and mark in same flow");
804         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
805                 return rte_flow_error_set(error, EINVAL,
806                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
807                                           "can't have 2 fate actions in"
808                                           " same flow");
809         if (attr->egress)
810                 return rte_flow_error_set(error, ENOTSUP,
811                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
812                                           "drop action not supported for "
813                                           "egress");
814         return 0;
815 }
816
817 /*
818  * Validate the queue action.
819  *
820  * @param[in] action
821  *   Pointer to the queue action.
822  * @param[in] action_flags
823  *   Bit-fields that holds the actions detected until now.
824  * @param[in] dev
825  *   Pointer to the Ethernet device structure.
826  * @param[in] attr
827  *   Attributes of flow that includes this action.
828  * @param[out] error
829  *   Pointer to error structure.
830  *
831  * @return
832  *   0 on success, a negative errno value otherwise and rte_ernno is set.
833  */
834 int
835 mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
836                                 uint64_t action_flags,
837                                 struct rte_eth_dev *dev,
838                                 const struct rte_flow_attr *attr,
839                                 struct rte_flow_error *error)
840 {
841         struct mlx5_priv *priv = dev->data->dev_private;
842         const struct rte_flow_action_queue *queue = action->conf;
843
844         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
845                 return rte_flow_error_set(error, EINVAL,
846                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
847                                           "can't have 2 fate actions in"
848                                           " same flow");
849         if (!priv->rxqs_n)
850                 return rte_flow_error_set(error, EINVAL,
851                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
852                                           NULL, "No Rx queues configured");
853         if (queue->index >= priv->rxqs_n)
854                 return rte_flow_error_set(error, EINVAL,
855                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
856                                           &queue->index,
857                                           "queue index out of range");
858         if (!(*priv->rxqs)[queue->index])
859                 return rte_flow_error_set(error, EINVAL,
860                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
861                                           &queue->index,
862                                           "queue is not configured");
863         if (attr->egress)
864                 return rte_flow_error_set(error, ENOTSUP,
865                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
866                                           "queue action not supported for "
867                                           "egress");
868         return 0;
869 }
870
871 /*
872  * Validate the rss action.
873  *
874  * @param[in] action
875  *   Pointer to the queue action.
876  * @param[in] action_flags
877  *   Bit-fields that holds the actions detected until now.
878  * @param[in] dev
879  *   Pointer to the Ethernet device structure.
880  * @param[in] attr
881  *   Attributes of flow that includes this action.
882  * @param[out] error
883  *   Pointer to error structure.
884  *
885  * @return
886  *   0 on success, a negative errno value otherwise and rte_ernno is set.
887  */
888 int
889 mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
890                               uint64_t action_flags,
891                               struct rte_eth_dev *dev,
892                               const struct rte_flow_attr *attr,
893                               struct rte_flow_error *error)
894 {
895         struct mlx5_priv *priv = dev->data->dev_private;
896         const struct rte_flow_action_rss *rss = action->conf;
897         unsigned int i;
898
899         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
900                 return rte_flow_error_set(error, EINVAL,
901                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
902                                           "can't have 2 fate actions"
903                                           " in same flow");
904         if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
905             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
906                 return rte_flow_error_set(error, ENOTSUP,
907                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
908                                           &rss->func,
909                                           "RSS hash function not supported");
910 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
911         if (rss->level > 2)
912 #else
913         if (rss->level > 1)
914 #endif
915                 return rte_flow_error_set(error, ENOTSUP,
916                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
917                                           &rss->level,
918                                           "tunnel RSS is not supported");
919         /* allow RSS key_len 0 in case of NULL (default) RSS key. */
920         if (rss->key_len == 0 && rss->key != NULL)
921                 return rte_flow_error_set(error, ENOTSUP,
922                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
923                                           &rss->key_len,
924                                           "RSS hash key length 0");
925         if (rss->key_len > 0 && rss->key_len < MLX5_RSS_HASH_KEY_LEN)
926                 return rte_flow_error_set(error, ENOTSUP,
927                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
928                                           &rss->key_len,
929                                           "RSS hash key too small");
930         if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
931                 return rte_flow_error_set(error, ENOTSUP,
932                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
933                                           &rss->key_len,
934                                           "RSS hash key too large");
935         if (rss->queue_num > priv->config.ind_table_max_size)
936                 return rte_flow_error_set(error, ENOTSUP,
937                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
938                                           &rss->queue_num,
939                                           "number of queues too large");
940         if (rss->types & MLX5_RSS_HF_MASK)
941                 return rte_flow_error_set(error, ENOTSUP,
942                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
943                                           &rss->types,
944                                           "some RSS protocols are not"
945                                           " supported");
946         if (!priv->rxqs_n)
947                 return rte_flow_error_set(error, EINVAL,
948                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
949                                           NULL, "No Rx queues configured");
950         if (!rss->queue_num)
951                 return rte_flow_error_set(error, EINVAL,
952                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
953                                           NULL, "No queues configured");
954         for (i = 0; i != rss->queue_num; ++i) {
955                 if (!(*priv->rxqs)[rss->queue[i]])
956                         return rte_flow_error_set
957                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
958                                  &rss->queue[i], "queue is not configured");
959         }
960         if (attr->egress)
961                 return rte_flow_error_set(error, ENOTSUP,
962                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
963                                           "rss action not supported for "
964                                           "egress");
965         return 0;
966 }
967
968 /*
969  * Validate the count action.
970  *
971  * @param[in] dev
972  *   Pointer to the Ethernet device structure.
973  * @param[in] attr
974  *   Attributes of flow that includes this action.
975  * @param[out] error
976  *   Pointer to error structure.
977  *
978  * @return
979  *   0 on success, a negative errno value otherwise and rte_ernno is set.
980  */
981 int
982 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
983                                 const struct rte_flow_attr *attr,
984                                 struct rte_flow_error *error)
985 {
986         if (attr->egress)
987                 return rte_flow_error_set(error, ENOTSUP,
988                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
989                                           "count action not supported for "
990                                           "egress");
991         return 0;
992 }
993
994 /**
995  * Verify the @p attributes will be correctly understood by the NIC and store
996  * them in the @p flow if everything is correct.
997  *
998  * @param[in] dev
999  *   Pointer to the Ethernet device structure.
1000  * @param[in] attributes
1001  *   Pointer to flow attributes
1002  * @param[out] error
1003  *   Pointer to error structure.
1004  *
1005  * @return
1006  *   0 on success, a negative errno value otherwise and rte_errno is set.
1007  */
1008 int
1009 mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
1010                               const struct rte_flow_attr *attributes,
1011                               struct rte_flow_error *error)
1012 {
1013         struct mlx5_priv *priv = dev->data->dev_private;
1014         uint32_t priority_max = priv->config.flow_prio - 1;
1015
1016         if (attributes->group)
1017                 return rte_flow_error_set(error, ENOTSUP,
1018                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1019                                           NULL, "groups is not supported");
1020         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
1021             attributes->priority >= priority_max)
1022                 return rte_flow_error_set(error, ENOTSUP,
1023                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1024                                           NULL, "priority out of range");
1025         if (attributes->egress)
1026                 return rte_flow_error_set(error, ENOTSUP,
1027                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1028                                           "egress is not supported");
1029         if (attributes->transfer)
1030                 return rte_flow_error_set(error, ENOTSUP,
1031                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1032                                           NULL, "transfer is not supported");
1033         if (!attributes->ingress)
1034                 return rte_flow_error_set(error, EINVAL,
1035                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1036                                           NULL,
1037                                           "ingress attribute is mandatory");
1038         return 0;
1039 }
1040
1041 /**
1042  * Validate Ethernet item.
1043  *
1044  * @param[in] item
1045  *   Item specification.
1046  * @param[in] item_flags
1047  *   Bit-fields that holds the items detected until now.
1048  * @param[out] error
1049  *   Pointer to error structure.
1050  *
1051  * @return
1052  *   0 on success, a negative errno value otherwise and rte_errno is set.
1053  */
1054 int
1055 mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
1056                             uint64_t item_flags,
1057                             struct rte_flow_error *error)
1058 {
1059         const struct rte_flow_item_eth *mask = item->mask;
1060         const struct rte_flow_item_eth nic_mask = {
1061                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1062                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1063                 .type = RTE_BE16(0xffff),
1064         };
1065         int ret;
1066         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1067         const uint64_t ethm = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1068                                        MLX5_FLOW_LAYER_OUTER_L2;
1069
1070         if (item_flags & ethm)
1071                 return rte_flow_error_set(error, ENOTSUP,
1072                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1073                                           "multiple L2 layers not supported");
1074         if (!mask)
1075                 mask = &rte_flow_item_eth_mask;
1076         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1077                                         (const uint8_t *)&nic_mask,
1078                                         sizeof(struct rte_flow_item_eth),
1079                                         error);
1080         return ret;
1081 }
1082
1083 /**
1084  * Validate VLAN item.
1085  *
1086  * @param[in] item
1087  *   Item specification.
1088  * @param[in] item_flags
1089  *   Bit-fields that holds the items detected until now.
1090  * @param[out] error
1091  *   Pointer to error structure.
1092  *
1093  * @return
1094  *   0 on success, a negative errno value otherwise and rte_errno is set.
1095  */
1096 int
1097 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
1098                              uint64_t item_flags,
1099                              struct rte_flow_error *error)
1100 {
1101         const struct rte_flow_item_vlan *spec = item->spec;
1102         const struct rte_flow_item_vlan *mask = item->mask;
1103         const struct rte_flow_item_vlan nic_mask = {
1104                 .tci = RTE_BE16(0x0fff),
1105                 .inner_type = RTE_BE16(0xffff),
1106         };
1107         uint16_t vlan_tag = 0;
1108         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1109         int ret;
1110         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1111                                         MLX5_FLOW_LAYER_INNER_L4) :
1112                                        (MLX5_FLOW_LAYER_OUTER_L3 |
1113                                         MLX5_FLOW_LAYER_OUTER_L4);
1114         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1115                                         MLX5_FLOW_LAYER_OUTER_VLAN;
1116
1117         if (item_flags & vlanm)
1118                 return rte_flow_error_set(error, EINVAL,
1119                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1120                                           "multiple VLAN layers not supported");
1121         else if ((item_flags & l34m) != 0)
1122                 return rte_flow_error_set(error, EINVAL,
1123                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1124                                           "L2 layer cannot follow L3/L4 layer");
1125         if (!mask)
1126                 mask = &rte_flow_item_vlan_mask;
1127         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1128                                         (const uint8_t *)&nic_mask,
1129                                         sizeof(struct rte_flow_item_vlan),
1130                                         error);
1131         if (ret)
1132                 return ret;
1133         if (spec) {
1134                 vlan_tag = spec->tci;
1135                 vlan_tag &= mask->tci;
1136         }
1137         /*
1138          * From verbs perspective an empty VLAN is equivalent
1139          * to a packet without VLAN layer.
1140          */
1141         if (!vlan_tag)
1142                 return rte_flow_error_set(error, EINVAL,
1143                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1144                                           item->spec,
1145                                           "VLAN cannot be empty");
1146         return 0;
1147 }
1148
1149 /**
1150  * Validate IPV4 item.
1151  *
1152  * @param[in] item
1153  *   Item specification.
1154  * @param[in] item_flags
1155  *   Bit-fields that holds the items detected until now.
1156  * @param[in] acc_mask
1157  *   Acceptable mask, if NULL default internal default mask
1158  *   will be used to check whether item fields are supported.
1159  * @param[out] error
1160  *   Pointer to error structure.
1161  *
1162  * @return
1163  *   0 on success, a negative errno value otherwise and rte_errno is set.
1164  */
1165 int
1166 mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
1167                              uint64_t item_flags,
1168                              const struct rte_flow_item_ipv4 *acc_mask,
1169                              struct rte_flow_error *error)
1170 {
1171         const struct rte_flow_item_ipv4 *mask = item->mask;
1172         const struct rte_flow_item_ipv4 nic_mask = {
1173                 .hdr = {
1174                         .src_addr = RTE_BE32(0xffffffff),
1175                         .dst_addr = RTE_BE32(0xffffffff),
1176                         .type_of_service = 0xff,
1177                         .next_proto_id = 0xff,
1178                 },
1179         };
1180         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1181         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1182                                       MLX5_FLOW_LAYER_OUTER_L3;
1183         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1184                                       MLX5_FLOW_LAYER_OUTER_L4;
1185         int ret;
1186
1187         if (item_flags & l3m)
1188                 return rte_flow_error_set(error, ENOTSUP,
1189                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1190                                           "multiple L3 layers not supported");
1191         else if (item_flags & l4m)
1192                 return rte_flow_error_set(error, EINVAL,
1193                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1194                                           "L3 cannot follow an L4 layer.");
1195         if (!mask)
1196                 mask = &rte_flow_item_ipv4_mask;
1197         else if (mask->hdr.next_proto_id != 0 &&
1198                  mask->hdr.next_proto_id != 0xff)
1199                 return rte_flow_error_set(error, EINVAL,
1200                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1201                                           "partial mask is not supported"
1202                                           " for protocol");
1203         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1204                                         acc_mask ? (const uint8_t *)acc_mask
1205                                                  : (const uint8_t *)&nic_mask,
1206                                         sizeof(struct rte_flow_item_ipv4),
1207                                         error);
1208         if (ret < 0)
1209                 return ret;
1210         return 0;
1211 }
1212
1213 /**
1214  * Validate IPV6 item.
1215  *
1216  * @param[in] item
1217  *   Item specification.
1218  * @param[in] item_flags
1219  *   Bit-fields that holds the items detected until now.
1220  * @param[in] acc_mask
1221  *   Acceptable mask, if NULL default internal default mask
1222  *   will be used to check whether item fields are supported.
1223  * @param[out] error
1224  *   Pointer to error structure.
1225  *
1226  * @return
1227  *   0 on success, a negative errno value otherwise and rte_errno is set.
1228  */
1229 int
1230 mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
1231                              uint64_t item_flags,
1232                              const struct rte_flow_item_ipv6 *acc_mask,
1233                              struct rte_flow_error *error)
1234 {
1235         const struct rte_flow_item_ipv6 *mask = item->mask;
1236         const struct rte_flow_item_ipv6 nic_mask = {
1237                 .hdr = {
1238                         .src_addr =
1239                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1240                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1241                         .dst_addr =
1242                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1243                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1244                         .vtc_flow = RTE_BE32(0xffffffff),
1245                         .proto = 0xff,
1246                         .hop_limits = 0xff,
1247                 },
1248         };
1249         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1250         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1251                                       MLX5_FLOW_LAYER_OUTER_L3;
1252         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1253                                       MLX5_FLOW_LAYER_OUTER_L4;
1254         int ret;
1255
1256         if (item_flags & l3m)
1257                 return rte_flow_error_set(error, ENOTSUP,
1258                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1259                                           "multiple L3 layers not supported");
1260         else if (item_flags & l4m)
1261                 return rte_flow_error_set(error, EINVAL,
1262                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1263                                           "L3 cannot follow an L4 layer.");
1264         if (!mask)
1265                 mask = &rte_flow_item_ipv6_mask;
1266         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1267                                         acc_mask ? (const uint8_t *)acc_mask
1268                                                  : (const uint8_t *)&nic_mask,
1269                                         sizeof(struct rte_flow_item_ipv6),
1270                                         error);
1271         if (ret < 0)
1272                 return ret;
1273         return 0;
1274 }
1275
1276 /**
1277  * Validate UDP item.
1278  *
1279  * @param[in] item
1280  *   Item specification.
1281  * @param[in] item_flags
1282  *   Bit-fields that holds the items detected until now.
1283  * @param[in] target_protocol
1284  *   The next protocol in the previous item.
1285  * @param[in] flow_mask
1286  *   mlx5 flow-specific (TCF, DV, verbs, etc.) supported header fields mask.
1287  * @param[out] error
1288  *   Pointer to error structure.
1289  *
1290  * @return
1291  *   0 on success, a negative errno value otherwise and rte_errno is set.
1292  */
1293 int
1294 mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
1295                             uint64_t item_flags,
1296                             uint8_t target_protocol,
1297                             struct rte_flow_error *error)
1298 {
1299         const struct rte_flow_item_udp *mask = item->mask;
1300         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1301         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1302                                       MLX5_FLOW_LAYER_OUTER_L3;
1303         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1304                                       MLX5_FLOW_LAYER_OUTER_L4;
1305         int ret;
1306
1307         if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
1308                 return rte_flow_error_set(error, EINVAL,
1309                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1310                                           "protocol filtering not compatible"
1311                                           " with UDP layer");
1312         if (!(item_flags & l3m))
1313                 return rte_flow_error_set(error, EINVAL,
1314                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1315                                           "L3 is mandatory to filter on L4");
1316         if (item_flags & l4m)
1317                 return rte_flow_error_set(error, EINVAL,
1318                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1319                                           "multiple L4 layers not supported");
1320         if (!mask)
1321                 mask = &rte_flow_item_udp_mask;
1322         ret = mlx5_flow_item_acceptable
1323                 (item, (const uint8_t *)mask,
1324                  (const uint8_t *)&rte_flow_item_udp_mask,
1325                  sizeof(struct rte_flow_item_udp), error);
1326         if (ret < 0)
1327                 return ret;
1328         return 0;
1329 }
1330
1331 /**
1332  * Validate TCP item.
1333  *
1334  * @param[in] item
1335  *   Item specification.
1336  * @param[in] item_flags
1337  *   Bit-fields that holds the items detected until now.
1338  * @param[in] target_protocol
1339  *   The next protocol in the previous item.
1340  * @param[out] error
1341  *   Pointer to error structure.
1342  *
1343  * @return
1344  *   0 on success, a negative errno value otherwise and rte_errno is set.
1345  */
1346 int
1347 mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
1348                             uint64_t item_flags,
1349                             uint8_t target_protocol,
1350                             const struct rte_flow_item_tcp *flow_mask,
1351                             struct rte_flow_error *error)
1352 {
1353         const struct rte_flow_item_tcp *mask = item->mask;
1354         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1355         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1356                                       MLX5_FLOW_LAYER_OUTER_L3;
1357         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1358                                       MLX5_FLOW_LAYER_OUTER_L4;
1359         int ret;
1360
1361         assert(flow_mask);
1362         if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
1363                 return rte_flow_error_set(error, EINVAL,
1364                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1365                                           "protocol filtering not compatible"
1366                                           " with TCP layer");
1367         if (!(item_flags & l3m))
1368                 return rte_flow_error_set(error, EINVAL,
1369                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1370                                           "L3 is mandatory to filter on L4");
1371         if (item_flags & l4m)
1372                 return rte_flow_error_set(error, EINVAL,
1373                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1374                                           "multiple L4 layers not supported");
1375         if (!mask)
1376                 mask = &rte_flow_item_tcp_mask;
1377         ret = mlx5_flow_item_acceptable
1378                 (item, (const uint8_t *)mask,
1379                  (const uint8_t *)flow_mask,
1380                  sizeof(struct rte_flow_item_tcp), error);
1381         if (ret < 0)
1382                 return ret;
1383         return 0;
1384 }
1385
1386 /**
1387  * Validate VXLAN item.
1388  *
1389  * @param[in] item
1390  *   Item specification.
1391  * @param[in] item_flags
1392  *   Bit-fields that holds the items detected until now.
1393  * @param[in] target_protocol
1394  *   The next protocol in the previous item.
1395  * @param[out] error
1396  *   Pointer to error structure.
1397  *
1398  * @return
1399  *   0 on success, a negative errno value otherwise and rte_errno is set.
1400  */
1401 int
1402 mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
1403                               uint64_t item_flags,
1404                               struct rte_flow_error *error)
1405 {
1406         const struct rte_flow_item_vxlan *spec = item->spec;
1407         const struct rte_flow_item_vxlan *mask = item->mask;
1408         int ret;
1409         union vni {
1410                 uint32_t vlan_id;
1411                 uint8_t vni[4];
1412         } id = { .vlan_id = 0, };
1413         uint32_t vlan_id = 0;
1414
1415
1416         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1417                 return rte_flow_error_set(error, ENOTSUP,
1418                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1419                                           "multiple tunnel layers not"
1420                                           " supported");
1421         /*
1422          * Verify only UDPv4 is present as defined in
1423          * https://tools.ietf.org/html/rfc7348
1424          */
1425         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1426                 return rte_flow_error_set(error, EINVAL,
1427                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1428                                           "no outer UDP layer found");
1429         if (!mask)
1430                 mask = &rte_flow_item_vxlan_mask;
1431         ret = mlx5_flow_item_acceptable
1432                 (item, (const uint8_t *)mask,
1433                  (const uint8_t *)&rte_flow_item_vxlan_mask,
1434                  sizeof(struct rte_flow_item_vxlan),
1435                  error);
1436         if (ret < 0)
1437                 return ret;
1438         if (spec) {
1439                 memcpy(&id.vni[1], spec->vni, 3);
1440                 vlan_id = id.vlan_id;
1441                 memcpy(&id.vni[1], mask->vni, 3);
1442                 vlan_id &= id.vlan_id;
1443         }
1444         /*
1445          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if
1446          * only this layer is defined in the Verbs specification it is
1447          * interpreted as wildcard and all packets will match this
1448          * rule, if it follows a full stack layer (ex: eth / ipv4 /
1449          * udp), all packets matching the layers before will also
1450          * match this rule.  To avoid such situation, VNI 0 is
1451          * currently refused.
1452          */
1453         if (!vlan_id)
1454                 return rte_flow_error_set(error, ENOTSUP,
1455                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1456                                           "VXLAN vni cannot be 0");
1457         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1458                 return rte_flow_error_set(error, ENOTSUP,
1459                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1460                                           "VXLAN tunnel must be fully defined");
1461         return 0;
1462 }
1463
1464 /**
1465  * Validate VXLAN_GPE item.
1466  *
1467  * @param[in] item
1468  *   Item specification.
1469  * @param[in] item_flags
1470  *   Bit-fields that holds the items detected until now.
1471  * @param[in] priv
1472  *   Pointer to the private data structure.
1473  * @param[in] target_protocol
1474  *   The next protocol in the previous item.
1475  * @param[out] error
1476  *   Pointer to error structure.
1477  *
1478  * @return
1479  *   0 on success, a negative errno value otherwise and rte_errno is set.
1480  */
1481 int
1482 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
1483                                   uint64_t item_flags,
1484                                   struct rte_eth_dev *dev,
1485                                   struct rte_flow_error *error)
1486 {
1487         struct mlx5_priv *priv = dev->data->dev_private;
1488         const struct rte_flow_item_vxlan_gpe *spec = item->spec;
1489         const struct rte_flow_item_vxlan_gpe *mask = item->mask;
1490         int ret;
1491         union vni {
1492                 uint32_t vlan_id;
1493                 uint8_t vni[4];
1494         } id = { .vlan_id = 0, };
1495         uint32_t vlan_id = 0;
1496
1497         if (!priv->config.l3_vxlan_en)
1498                 return rte_flow_error_set(error, ENOTSUP,
1499                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1500                                           "L3 VXLAN is not enabled by device"
1501                                           " parameter and/or not configured in"
1502                                           " firmware");
1503         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1504                 return rte_flow_error_set(error, ENOTSUP,
1505                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1506                                           "multiple tunnel layers not"
1507                                           " supported");
1508         /*
1509          * Verify only UDPv4 is present as defined in
1510          * https://tools.ietf.org/html/rfc7348
1511          */
1512         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1513                 return rte_flow_error_set(error, EINVAL,
1514                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1515                                           "no outer UDP layer found");
1516         if (!mask)
1517                 mask = &rte_flow_item_vxlan_gpe_mask;
1518         ret = mlx5_flow_item_acceptable
1519                 (item, (const uint8_t *)mask,
1520                  (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
1521                  sizeof(struct rte_flow_item_vxlan_gpe),
1522                  error);
1523         if (ret < 0)
1524                 return ret;
1525         if (spec) {
1526                 if (spec->protocol)
1527                         return rte_flow_error_set(error, ENOTSUP,
1528                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1529                                                   item,
1530                                                   "VxLAN-GPE protocol"
1531                                                   " not supported");
1532                 memcpy(&id.vni[1], spec->vni, 3);
1533                 vlan_id = id.vlan_id;
1534                 memcpy(&id.vni[1], mask->vni, 3);
1535                 vlan_id &= id.vlan_id;
1536         }
1537         /*
1538          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1539          * layer is defined in the Verbs specification it is interpreted as
1540          * wildcard and all packets will match this rule, if it follows a full
1541          * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1542          * before will also match this rule.  To avoid such situation, VNI 0
1543          * is currently refused.
1544          */
1545         if (!vlan_id)
1546                 return rte_flow_error_set(error, ENOTSUP,
1547                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1548                                           "VXLAN-GPE vni cannot be 0");
1549         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1550                 return rte_flow_error_set(error, ENOTSUP,
1551                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1552                                           "VXLAN-GPE tunnel must be fully"
1553                                           " defined");
1554         return 0;
1555 }
1556
1557 /**
1558  * Validate GRE item.
1559  *
1560  * @param[in] item
1561  *   Item specification.
1562  * @param[in] item_flags
1563  *   Bit flags to mark detected items.
1564  * @param[in] target_protocol
1565  *   The next protocol in the previous item.
1566  * @param[out] error
1567  *   Pointer to error structure.
1568  *
1569  * @return
1570  *   0 on success, a negative errno value otherwise and rte_errno is set.
1571  */
1572 int
1573 mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
1574                             uint64_t item_flags,
1575                             uint8_t target_protocol,
1576                             struct rte_flow_error *error)
1577 {
1578         const struct rte_flow_item_gre *spec __rte_unused = item->spec;
1579         const struct rte_flow_item_gre *mask = item->mask;
1580         int ret;
1581
1582         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
1583                 return rte_flow_error_set(error, EINVAL,
1584                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1585                                           "protocol filtering not compatible"
1586                                           " with this GRE layer");
1587         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1588                 return rte_flow_error_set(error, ENOTSUP,
1589                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1590                                           "multiple tunnel layers not"
1591                                           " supported");
1592         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
1593                 return rte_flow_error_set(error, ENOTSUP,
1594                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1595                                           "L3 Layer is missing");
1596         if (!mask)
1597                 mask = &rte_flow_item_gre_mask;
1598         ret = mlx5_flow_item_acceptable
1599                 (item, (const uint8_t *)mask,
1600                  (const uint8_t *)&rte_flow_item_gre_mask,
1601                  sizeof(struct rte_flow_item_gre), error);
1602         if (ret < 0)
1603                 return ret;
1604 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
1605         if (spec && (spec->protocol & mask->protocol))
1606                 return rte_flow_error_set(error, ENOTSUP,
1607                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1608                                           "without MPLS support the"
1609                                           " specification cannot be used for"
1610                                           " filtering");
1611 #endif
1612         return 0;
1613 }
1614
1615 /**
1616  * Validate MPLS item.
1617  *
1618  * @param[in] dev
1619  *   Pointer to the rte_eth_dev structure.
1620  * @param[in] item
1621  *   Item specification.
1622  * @param[in] item_flags
1623  *   Bit-fields that holds the items detected until now.
1624  * @param[in] prev_layer
1625  *   The protocol layer indicated in previous item.
1626  * @param[out] error
1627  *   Pointer to error structure.
1628  *
1629  * @return
1630  *   0 on success, a negative errno value otherwise and rte_errno is set.
1631  */
1632 int
1633 mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev __rte_unused,
1634                              const struct rte_flow_item *item __rte_unused,
1635                              uint64_t item_flags __rte_unused,
1636                              uint64_t prev_layer __rte_unused,
1637                              struct rte_flow_error *error)
1638 {
1639 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1640         const struct rte_flow_item_mpls *mask = item->mask;
1641         struct mlx5_priv *priv = dev->data->dev_private;
1642         int ret;
1643
1644         if (!priv->config.mpls_en)
1645                 return rte_flow_error_set(error, ENOTSUP,
1646                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1647                                           "MPLS not supported or"
1648                                           " disabled in firmware"
1649                                           " configuration.");
1650         /* MPLS over IP, UDP, GRE is allowed */
1651         if (!(prev_layer & (MLX5_FLOW_LAYER_OUTER_L3 |
1652                             MLX5_FLOW_LAYER_OUTER_L4_UDP |
1653                             MLX5_FLOW_LAYER_GRE)))
1654                 return rte_flow_error_set(error, EINVAL,
1655                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1656                                           "protocol filtering not compatible"
1657                                           " with MPLS layer");
1658         /* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
1659         if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
1660             !(item_flags & MLX5_FLOW_LAYER_GRE))
1661                 return rte_flow_error_set(error, ENOTSUP,
1662                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1663                                           "multiple tunnel layers not"
1664                                           " supported");
1665         if (!mask)
1666                 mask = &rte_flow_item_mpls_mask;
1667         ret = mlx5_flow_item_acceptable
1668                 (item, (const uint8_t *)mask,
1669                  (const uint8_t *)&rte_flow_item_mpls_mask,
1670                  sizeof(struct rte_flow_item_mpls), error);
1671         if (ret < 0)
1672                 return ret;
1673         return 0;
1674 #endif
1675         return rte_flow_error_set(error, ENOTSUP,
1676                                   RTE_FLOW_ERROR_TYPE_ITEM, item,
1677                                   "MPLS is not supported by Verbs, please"
1678                                   " update.");
1679 }
1680
1681 static int
1682 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
1683                    const struct rte_flow_attr *attr __rte_unused,
1684                    const struct rte_flow_item items[] __rte_unused,
1685                    const struct rte_flow_action actions[] __rte_unused,
1686                    struct rte_flow_error *error __rte_unused)
1687 {
1688         rte_errno = ENOTSUP;
1689         return -rte_errno;
1690 }
1691
1692 static struct mlx5_flow *
1693 flow_null_prepare(const struct rte_flow_attr *attr __rte_unused,
1694                   const struct rte_flow_item items[] __rte_unused,
1695                   const struct rte_flow_action actions[] __rte_unused,
1696                   struct rte_flow_error *error __rte_unused)
1697 {
1698         rte_errno = ENOTSUP;
1699         return NULL;
1700 }
1701
1702 static int
1703 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
1704                     struct mlx5_flow *dev_flow __rte_unused,
1705                     const struct rte_flow_attr *attr __rte_unused,
1706                     const struct rte_flow_item items[] __rte_unused,
1707                     const struct rte_flow_action actions[] __rte_unused,
1708                     struct rte_flow_error *error __rte_unused)
1709 {
1710         rte_errno = ENOTSUP;
1711         return -rte_errno;
1712 }
1713
1714 static int
1715 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
1716                 struct rte_flow *flow __rte_unused,
1717                 struct rte_flow_error *error __rte_unused)
1718 {
1719         rte_errno = ENOTSUP;
1720         return -rte_errno;
1721 }
1722
1723 static void
1724 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
1725                  struct rte_flow *flow __rte_unused)
1726 {
1727 }
1728
1729 static void
1730 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
1731                   struct rte_flow *flow __rte_unused)
1732 {
1733 }
1734
1735 static int
1736 flow_null_query(struct rte_eth_dev *dev __rte_unused,
1737                 struct rte_flow *flow __rte_unused,
1738                 const struct rte_flow_action *actions __rte_unused,
1739                 void *data __rte_unused,
1740                 struct rte_flow_error *error __rte_unused)
1741 {
1742         rte_errno = ENOTSUP;
1743         return -rte_errno;
1744 }
1745
1746 /* Void driver to protect from null pointer reference. */
1747 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
1748         .validate = flow_null_validate,
1749         .prepare = flow_null_prepare,
1750         .translate = flow_null_translate,
1751         .apply = flow_null_apply,
1752         .remove = flow_null_remove,
1753         .destroy = flow_null_destroy,
1754         .query = flow_null_query,
1755 };
1756
1757 /**
1758  * Select flow driver type according to flow attributes and device
1759  * configuration.
1760  *
1761  * @param[in] dev
1762  *   Pointer to the dev structure.
1763  * @param[in] attr
1764  *   Pointer to the flow attributes.
1765  *
1766  * @return
1767  *   flow driver type, MLX5_FLOW_TYPE_MAX otherwise.
1768  */
1769 static enum mlx5_flow_drv_type
1770 flow_get_drv_type(struct rte_eth_dev *dev, const struct rte_flow_attr *attr)
1771 {
1772         struct mlx5_priv *priv = dev->data->dev_private;
1773         enum mlx5_flow_drv_type type = MLX5_FLOW_TYPE_MAX;
1774
1775         if (attr->transfer)
1776                 type = MLX5_FLOW_TYPE_TCF;
1777         else
1778                 type = priv->config.dv_flow_en ? MLX5_FLOW_TYPE_DV :
1779                                                  MLX5_FLOW_TYPE_VERBS;
1780         return type;
1781 }
1782
1783 #define flow_get_drv_ops(type) flow_drv_ops[type]
1784
1785 /**
1786  * Flow driver validation API. This abstracts calling driver specific functions.
1787  * The type of flow driver is determined according to flow attributes.
1788  *
1789  * @param[in] dev
1790  *   Pointer to the dev structure.
1791  * @param[in] attr
1792  *   Pointer to the flow attributes.
1793  * @param[in] items
1794  *   Pointer to the list of items.
1795  * @param[in] actions
1796  *   Pointer to the list of actions.
1797  * @param[out] error
1798  *   Pointer to the error structure.
1799  *
1800  * @return
1801  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1802  */
1803 static inline int
1804 flow_drv_validate(struct rte_eth_dev *dev,
1805                   const struct rte_flow_attr *attr,
1806                   const struct rte_flow_item items[],
1807                   const struct rte_flow_action actions[],
1808                   struct rte_flow_error *error)
1809 {
1810         const struct mlx5_flow_driver_ops *fops;
1811         enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
1812
1813         fops = flow_get_drv_ops(type);
1814         return fops->validate(dev, attr, items, actions, error);
1815 }
1816
1817 /**
1818  * Flow driver preparation API. This abstracts calling driver specific
1819  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
1820  * calculates the size of memory required for device flow, allocates the memory,
1821  * initializes the device flow and returns the pointer.
1822  *
1823  * @note
1824  *   This function initializes device flow structure such as dv, tcf or verbs in
1825  *   struct mlx5_flow. However, it is caller's responsibility to initialize the
1826  *   rest. For example, adding returning device flow to flow->dev_flow list and
1827  *   setting backward reference to the flow should be done out of this function.
1828  *   layers field is not filled either.
1829  *
1830  * @param[in] attr
1831  *   Pointer to the flow attributes.
1832  * @param[in] items
1833  *   Pointer to the list of items.
1834  * @param[in] actions
1835  *   Pointer to the list of actions.
1836  * @param[out] error
1837  *   Pointer to the error structure.
1838  *
1839  * @return
1840  *   Pointer to device flow on success, otherwise NULL and rte_ernno is set.
1841  */
1842 static inline struct mlx5_flow *
1843 flow_drv_prepare(const struct rte_flow *flow,
1844                  const struct rte_flow_attr *attr,
1845                  const struct rte_flow_item items[],
1846                  const struct rte_flow_action actions[],
1847                  struct rte_flow_error *error)
1848 {
1849         const struct mlx5_flow_driver_ops *fops;
1850         enum mlx5_flow_drv_type type = flow->drv_type;
1851
1852         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1853         fops = flow_get_drv_ops(type);
1854         return fops->prepare(attr, items, actions, error);
1855 }
1856
1857 /**
1858  * Flow driver translation API. This abstracts calling driver specific
1859  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
1860  * translates a generic flow into a driver flow. flow_drv_prepare() must
1861  * precede.
1862  *
1863  * @note
1864  *   dev_flow->layers could be filled as a result of parsing during translation
1865  *   if needed by flow_drv_apply(). dev_flow->flow->actions can also be filled
1866  *   if necessary. As a flow can have multiple dev_flows by RSS flow expansion,
1867  *   flow->actions could be overwritten even though all the expanded dev_flows
1868  *   have the same actions.
1869  *
1870  * @param[in] dev
1871  *   Pointer to the rte dev structure.
1872  * @param[in, out] dev_flow
1873  *   Pointer to the mlx5 flow.
1874  * @param[in] attr
1875  *   Pointer to the flow attributes.
1876  * @param[in] items
1877  *   Pointer to the list of items.
1878  * @param[in] actions
1879  *   Pointer to the list of actions.
1880  * @param[out] error
1881  *   Pointer to the error structure.
1882  *
1883  * @return
1884  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1885  */
1886 static inline int
1887 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
1888                    const struct rte_flow_attr *attr,
1889                    const struct rte_flow_item items[],
1890                    const struct rte_flow_action actions[],
1891                    struct rte_flow_error *error)
1892 {
1893         const struct mlx5_flow_driver_ops *fops;
1894         enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
1895
1896         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1897         fops = flow_get_drv_ops(type);
1898         return fops->translate(dev, dev_flow, attr, items, actions, error);
1899 }
1900
1901 /**
1902  * Flow driver apply API. This abstracts calling driver specific functions.
1903  * Parent flow (rte_flow) should have driver type (drv_type). It applies
1904  * translated driver flows on to device. flow_drv_translate() must precede.
1905  *
1906  * @param[in] dev
1907  *   Pointer to Ethernet device structure.
1908  * @param[in, out] flow
1909  *   Pointer to flow structure.
1910  * @param[out] error
1911  *   Pointer to error structure.
1912  *
1913  * @return
1914  *   0 on success, a negative errno value otherwise and rte_errno is set.
1915  */
1916 static inline int
1917 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1918                struct rte_flow_error *error)
1919 {
1920         const struct mlx5_flow_driver_ops *fops;
1921         enum mlx5_flow_drv_type type = flow->drv_type;
1922
1923         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1924         fops = flow_get_drv_ops(type);
1925         return fops->apply(dev, flow, error);
1926 }
1927
1928 /**
1929  * Flow driver remove API. This abstracts calling driver specific functions.
1930  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
1931  * on device. All the resources of the flow should be freed by calling
1932  * flow_drv_destroy().
1933  *
1934  * @param[in] dev
1935  *   Pointer to Ethernet device.
1936  * @param[in, out] flow
1937  *   Pointer to flow structure.
1938  */
1939 static inline void
1940 flow_drv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1941 {
1942         const struct mlx5_flow_driver_ops *fops;
1943         enum mlx5_flow_drv_type type = flow->drv_type;
1944
1945         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1946         fops = flow_get_drv_ops(type);
1947         fops->remove(dev, flow);
1948 }
1949
1950 /**
1951  * Flow driver destroy API. This abstracts calling driver specific functions.
1952  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
1953  * on device and releases resources of the flow.
1954  *
1955  * @param[in] dev
1956  *   Pointer to Ethernet device.
1957  * @param[in, out] flow
1958  *   Pointer to flow structure.
1959  */
1960 static inline void
1961 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1962 {
1963         const struct mlx5_flow_driver_ops *fops;
1964         enum mlx5_flow_drv_type type = flow->drv_type;
1965
1966         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1967         fops = flow_get_drv_ops(type);
1968         fops->destroy(dev, flow);
1969 }
1970
1971 /**
1972  * Validate a flow supported by the NIC.
1973  *
1974  * @see rte_flow_validate()
1975  * @see rte_flow_ops
1976  */
1977 int
1978 mlx5_flow_validate(struct rte_eth_dev *dev,
1979                    const struct rte_flow_attr *attr,
1980                    const struct rte_flow_item items[],
1981                    const struct rte_flow_action actions[],
1982                    struct rte_flow_error *error)
1983 {
1984         int ret;
1985
1986         ret = flow_drv_validate(dev, attr, items, actions, error);
1987         if (ret < 0)
1988                 return ret;
1989         return 0;
1990 }
1991
1992 /**
1993  * Get RSS action from the action list.
1994  *
1995  * @param[in] actions
1996  *   Pointer to the list of actions.
1997  *
1998  * @return
1999  *   Pointer to the RSS action if exist, else return NULL.
2000  */
2001 static const struct rte_flow_action_rss*
2002 flow_get_rss_action(const struct rte_flow_action actions[])
2003 {
2004         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2005                 switch (actions->type) {
2006                 case RTE_FLOW_ACTION_TYPE_RSS:
2007                         return (const struct rte_flow_action_rss *)
2008                                actions->conf;
2009                 default:
2010                         break;
2011                 }
2012         }
2013         return NULL;
2014 }
2015
2016 static unsigned int
2017 find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
2018 {
2019         const struct rte_flow_item *item;
2020         unsigned int has_vlan = 0;
2021
2022         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
2023                 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
2024                         has_vlan = 1;
2025                         break;
2026                 }
2027         }
2028         if (has_vlan)
2029                 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
2030                                        MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
2031         return rss_level < 2 ? MLX5_EXPANSION_ROOT :
2032                                MLX5_EXPANSION_ROOT_OUTER;
2033 }
2034
2035 /**
2036  * Create a flow and add it to @p list.
2037  *
2038  * @param dev
2039  *   Pointer to Ethernet device.
2040  * @param list
2041  *   Pointer to a TAILQ flow list.
2042  * @param[in] attr
2043  *   Flow rule attributes.
2044  * @param[in] items
2045  *   Pattern specification (list terminated by the END pattern item).
2046  * @param[in] actions
2047  *   Associated actions (list terminated by the END action).
2048  * @param[out] error
2049  *   Perform verbose error reporting if not NULL.
2050  *
2051  * @return
2052  *   A flow on success, NULL otherwise and rte_errno is set.
2053  */
2054 static struct rte_flow *
2055 flow_list_create(struct rte_eth_dev *dev, struct mlx5_flows *list,
2056                  const struct rte_flow_attr *attr,
2057                  const struct rte_flow_item items[],
2058                  const struct rte_flow_action actions[],
2059                  struct rte_flow_error *error)
2060 {
2061         struct rte_flow *flow = NULL;
2062         struct mlx5_flow *dev_flow;
2063         const struct rte_flow_action_rss *rss;
2064         union {
2065                 struct rte_flow_expand_rss buf;
2066                 uint8_t buffer[2048];
2067         } expand_buffer;
2068         struct rte_flow_expand_rss *buf = &expand_buffer.buf;
2069         int ret;
2070         uint32_t i;
2071         uint32_t flow_size;
2072
2073         ret = flow_drv_validate(dev, attr, items, actions, error);
2074         if (ret < 0)
2075                 return NULL;
2076         flow_size = sizeof(struct rte_flow);
2077         rss = flow_get_rss_action(actions);
2078         if (rss)
2079                 flow_size += RTE_ALIGN_CEIL(rss->queue_num * sizeof(uint16_t),
2080                                             sizeof(void *));
2081         else
2082                 flow_size += RTE_ALIGN_CEIL(sizeof(uint16_t), sizeof(void *));
2083         flow = rte_calloc(__func__, 1, flow_size, 0);
2084         flow->drv_type = flow_get_drv_type(dev, attr);
2085         assert(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
2086                flow->drv_type < MLX5_FLOW_TYPE_MAX);
2087         flow->queue = (void *)(flow + 1);
2088         LIST_INIT(&flow->dev_flows);
2089         if (rss && rss->types) {
2090                 unsigned int graph_root;
2091
2092                 graph_root = find_graph_root(items, rss->level);
2093                 ret = rte_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
2094                                           items, rss->types,
2095                                           mlx5_support_expansion,
2096                                           graph_root);
2097                 assert(ret > 0 &&
2098                        (unsigned int)ret < sizeof(expand_buffer.buffer));
2099         } else {
2100                 buf->entries = 1;
2101                 buf->entry[0].pattern = (void *)(uintptr_t)items;
2102         }
2103         for (i = 0; i < buf->entries; ++i) {
2104                 dev_flow = flow_drv_prepare(flow, attr, buf->entry[i].pattern,
2105                                             actions, error);
2106                 if (!dev_flow)
2107                         goto error;
2108                 dev_flow->flow = flow;
2109                 LIST_INSERT_HEAD(&flow->dev_flows, dev_flow, next);
2110                 ret = flow_drv_translate(dev, dev_flow, attr,
2111                                          buf->entry[i].pattern,
2112                                          actions, error);
2113                 if (ret < 0)
2114                         goto error;
2115         }
2116         if (dev->data->dev_started) {
2117                 ret = flow_drv_apply(dev, flow, error);
2118                 if (ret < 0)
2119                         goto error;
2120         }
2121         TAILQ_INSERT_TAIL(list, flow, next);
2122         flow_rxq_flags_set(dev, flow);
2123         return flow;
2124 error:
2125         ret = rte_errno; /* Save rte_errno before cleanup. */
2126         assert(flow);
2127         flow_drv_destroy(dev, flow);
2128         rte_free(flow);
2129         rte_errno = ret; /* Restore rte_errno. */
2130         return NULL;
2131 }
2132
2133 /**
2134  * Create a flow.
2135  *
2136  * @see rte_flow_create()
2137  * @see rte_flow_ops
2138  */
2139 struct rte_flow *
2140 mlx5_flow_create(struct rte_eth_dev *dev,
2141                  const struct rte_flow_attr *attr,
2142                  const struct rte_flow_item items[],
2143                  const struct rte_flow_action actions[],
2144                  struct rte_flow_error *error)
2145 {
2146         struct mlx5_priv *priv = (struct mlx5_priv *)dev->data->dev_private;
2147
2148         return flow_list_create(dev, &priv->flows,
2149                                 attr, items, actions, error);
2150 }
2151
2152 /**
2153  * Destroy a flow in a list.
2154  *
2155  * @param dev
2156  *   Pointer to Ethernet device.
2157  * @param list
2158  *   Pointer to a TAILQ flow list.
2159  * @param[in] flow
2160  *   Flow to destroy.
2161  */
2162 static void
2163 flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2164                   struct rte_flow *flow)
2165 {
2166         /*
2167          * Update RX queue flags only if port is started, otherwise it is
2168          * already clean.
2169          */
2170         if (dev->data->dev_started)
2171                 flow_rxq_flags_trim(dev, flow);
2172         flow_drv_destroy(dev, flow);
2173         TAILQ_REMOVE(list, flow, next);
2174         rte_free(flow->fdir);
2175         rte_free(flow);
2176 }
2177
2178 /**
2179  * Destroy all flows.
2180  *
2181  * @param dev
2182  *   Pointer to Ethernet device.
2183  * @param list
2184  *   Pointer to a TAILQ flow list.
2185  */
2186 void
2187 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
2188 {
2189         while (!TAILQ_EMPTY(list)) {
2190                 struct rte_flow *flow;
2191
2192                 flow = TAILQ_FIRST(list);
2193                 flow_list_destroy(dev, list, flow);
2194         }
2195 }
2196
2197 /**
2198  * Remove all flows.
2199  *
2200  * @param dev
2201  *   Pointer to Ethernet device.
2202  * @param list
2203  *   Pointer to a TAILQ flow list.
2204  */
2205 void
2206 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
2207 {
2208         struct rte_flow *flow;
2209
2210         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next)
2211                 flow_drv_remove(dev, flow);
2212         flow_rxq_flags_clear(dev);
2213 }
2214
2215 /**
2216  * Add all flows.
2217  *
2218  * @param dev
2219  *   Pointer to Ethernet device.
2220  * @param list
2221  *   Pointer to a TAILQ flow list.
2222  *
2223  * @return
2224  *   0 on success, a negative errno value otherwise and rte_errno is set.
2225  */
2226 int
2227 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
2228 {
2229         struct rte_flow *flow;
2230         struct rte_flow_error error;
2231         int ret = 0;
2232
2233         TAILQ_FOREACH(flow, list, next) {
2234                 ret = flow_drv_apply(dev, flow, &error);
2235                 if (ret < 0)
2236                         goto error;
2237                 flow_rxq_flags_set(dev, flow);
2238         }
2239         return 0;
2240 error:
2241         ret = rte_errno; /* Save rte_errno before cleanup. */
2242         mlx5_flow_stop(dev, list);
2243         rte_errno = ret; /* Restore rte_errno. */
2244         return -rte_errno;
2245 }
2246
2247 /**
2248  * Verify the flow list is empty
2249  *
2250  * @param dev
2251  *  Pointer to Ethernet device.
2252  *
2253  * @return the number of flows not released.
2254  */
2255 int
2256 mlx5_flow_verify(struct rte_eth_dev *dev)
2257 {
2258         struct mlx5_priv *priv = dev->data->dev_private;
2259         struct rte_flow *flow;
2260         int ret = 0;
2261
2262         TAILQ_FOREACH(flow, &priv->flows, next) {
2263                 DRV_LOG(DEBUG, "port %u flow %p still referenced",
2264                         dev->data->port_id, (void *)flow);
2265                 ++ret;
2266         }
2267         return ret;
2268 }
2269
2270 /**
2271  * Enable a control flow configured from the control plane.
2272  *
2273  * @param dev
2274  *   Pointer to Ethernet device.
2275  * @param eth_spec
2276  *   An Ethernet flow spec to apply.
2277  * @param eth_mask
2278  *   An Ethernet flow mask to apply.
2279  * @param vlan_spec
2280  *   A VLAN flow spec to apply.
2281  * @param vlan_mask
2282  *   A VLAN flow mask to apply.
2283  *
2284  * @return
2285  *   0 on success, a negative errno value otherwise and rte_errno is set.
2286  */
2287 int
2288 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
2289                     struct rte_flow_item_eth *eth_spec,
2290                     struct rte_flow_item_eth *eth_mask,
2291                     struct rte_flow_item_vlan *vlan_spec,
2292                     struct rte_flow_item_vlan *vlan_mask)
2293 {
2294         struct mlx5_priv *priv = dev->data->dev_private;
2295         const struct rte_flow_attr attr = {
2296                 .ingress = 1,
2297                 .priority = MLX5_FLOW_PRIO_RSVD,
2298         };
2299         struct rte_flow_item items[] = {
2300                 {
2301                         .type = RTE_FLOW_ITEM_TYPE_ETH,
2302                         .spec = eth_spec,
2303                         .last = NULL,
2304                         .mask = eth_mask,
2305                 },
2306                 {
2307                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
2308                                               RTE_FLOW_ITEM_TYPE_END,
2309                         .spec = vlan_spec,
2310                         .last = NULL,
2311                         .mask = vlan_mask,
2312                 },
2313                 {
2314                         .type = RTE_FLOW_ITEM_TYPE_END,
2315                 },
2316         };
2317         uint16_t queue[priv->reta_idx_n];
2318         struct rte_flow_action_rss action_rss = {
2319                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2320                 .level = 0,
2321                 .types = priv->rss_conf.rss_hf,
2322                 .key_len = priv->rss_conf.rss_key_len,
2323                 .queue_num = priv->reta_idx_n,
2324                 .key = priv->rss_conf.rss_key,
2325                 .queue = queue,
2326         };
2327         struct rte_flow_action actions[] = {
2328                 {
2329                         .type = RTE_FLOW_ACTION_TYPE_RSS,
2330                         .conf = &action_rss,
2331                 },
2332                 {
2333                         .type = RTE_FLOW_ACTION_TYPE_END,
2334                 },
2335         };
2336         struct rte_flow *flow;
2337         struct rte_flow_error error;
2338         unsigned int i;
2339
2340         if (!priv->reta_idx_n || !priv->rxqs_n) {
2341                 return 0;
2342         }
2343         for (i = 0; i != priv->reta_idx_n; ++i)
2344                 queue[i] = (*priv->reta_idx)[i];
2345         flow = flow_list_create(dev, &priv->ctrl_flows,
2346                                 &attr, items, actions, &error);
2347         if (!flow)
2348                 return -rte_errno;
2349         return 0;
2350 }
2351
2352 /**
2353  * Enable a flow control configured from the control plane.
2354  *
2355  * @param dev
2356  *   Pointer to Ethernet device.
2357  * @param eth_spec
2358  *   An Ethernet flow spec to apply.
2359  * @param eth_mask
2360  *   An Ethernet flow mask to apply.
2361  *
2362  * @return
2363  *   0 on success, a negative errno value otherwise and rte_errno is set.
2364  */
2365 int
2366 mlx5_ctrl_flow(struct rte_eth_dev *dev,
2367                struct rte_flow_item_eth *eth_spec,
2368                struct rte_flow_item_eth *eth_mask)
2369 {
2370         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
2371 }
2372
2373 /**
2374  * Destroy a flow.
2375  *
2376  * @see rte_flow_destroy()
2377  * @see rte_flow_ops
2378  */
2379 int
2380 mlx5_flow_destroy(struct rte_eth_dev *dev,
2381                   struct rte_flow *flow,
2382                   struct rte_flow_error *error __rte_unused)
2383 {
2384         struct mlx5_priv *priv = dev->data->dev_private;
2385
2386         flow_list_destroy(dev, &priv->flows, flow);
2387         return 0;
2388 }
2389
2390 /**
2391  * Destroy all flows.
2392  *
2393  * @see rte_flow_flush()
2394  * @see rte_flow_ops
2395  */
2396 int
2397 mlx5_flow_flush(struct rte_eth_dev *dev,
2398                 struct rte_flow_error *error __rte_unused)
2399 {
2400         struct mlx5_priv *priv = dev->data->dev_private;
2401
2402         mlx5_flow_list_flush(dev, &priv->flows);
2403         return 0;
2404 }
2405
2406 /**
2407  * Isolated mode.
2408  *
2409  * @see rte_flow_isolate()
2410  * @see rte_flow_ops
2411  */
2412 int
2413 mlx5_flow_isolate(struct rte_eth_dev *dev,
2414                   int enable,
2415                   struct rte_flow_error *error)
2416 {
2417         struct mlx5_priv *priv = dev->data->dev_private;
2418
2419         if (dev->data->dev_started) {
2420                 rte_flow_error_set(error, EBUSY,
2421                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2422                                    NULL,
2423                                    "port must be stopped first");
2424                 return -rte_errno;
2425         }
2426         priv->isolated = !!enable;
2427         if (enable)
2428                 dev->dev_ops = &mlx5_dev_ops_isolate;
2429         else
2430                 dev->dev_ops = &mlx5_dev_ops;
2431         return 0;
2432 }
2433
2434 /**
2435  * Query a flow.
2436  *
2437  * @see rte_flow_query()
2438  * @see rte_flow_ops
2439  */
2440 static int
2441 flow_drv_query(struct rte_eth_dev *dev,
2442                struct rte_flow *flow,
2443                const struct rte_flow_action *actions,
2444                void *data,
2445                struct rte_flow_error *error)
2446 {
2447         const struct mlx5_flow_driver_ops *fops;
2448         enum mlx5_flow_drv_type ftype = flow->drv_type;
2449
2450         assert(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
2451         fops = flow_get_drv_ops(ftype);
2452
2453         return fops->query(dev, flow, actions, data, error);
2454 }
2455
2456 /**
2457  * Query a flow.
2458  *
2459  * @see rte_flow_query()
2460  * @see rte_flow_ops
2461  */
2462 int
2463 mlx5_flow_query(struct rte_eth_dev *dev,
2464                 struct rte_flow *flow,
2465                 const struct rte_flow_action *actions,
2466                 void *data,
2467                 struct rte_flow_error *error)
2468 {
2469         int ret;
2470
2471         ret = flow_drv_query(dev, flow, actions, data, error);
2472         if (ret < 0)
2473                 return ret;
2474         return 0;
2475 }
2476
2477 /**
2478  * Convert a flow director filter to a generic flow.
2479  *
2480  * @param dev
2481  *   Pointer to Ethernet device.
2482  * @param fdir_filter
2483  *   Flow director filter to add.
2484  * @param attributes
2485  *   Generic flow parameters structure.
2486  *
2487  * @return
2488  *   0 on success, a negative errno value otherwise and rte_errno is set.
2489  */
2490 static int
2491 flow_fdir_filter_convert(struct rte_eth_dev *dev,
2492                          const struct rte_eth_fdir_filter *fdir_filter,
2493                          struct mlx5_fdir *attributes)
2494 {
2495         struct mlx5_priv *priv = dev->data->dev_private;
2496         const struct rte_eth_fdir_input *input = &fdir_filter->input;
2497         const struct rte_eth_fdir_masks *mask =
2498                 &dev->data->dev_conf.fdir_conf.mask;
2499
2500         /* Validate queue number. */
2501         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
2502                 DRV_LOG(ERR, "port %u invalid queue number %d",
2503                         dev->data->port_id, fdir_filter->action.rx_queue);
2504                 rte_errno = EINVAL;
2505                 return -rte_errno;
2506         }
2507         attributes->attr.ingress = 1;
2508         attributes->items[0] = (struct rte_flow_item) {
2509                 .type = RTE_FLOW_ITEM_TYPE_ETH,
2510                 .spec = &attributes->l2,
2511                 .mask = &attributes->l2_mask,
2512         };
2513         switch (fdir_filter->action.behavior) {
2514         case RTE_ETH_FDIR_ACCEPT:
2515                 attributes->actions[0] = (struct rte_flow_action){
2516                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
2517                         .conf = &attributes->queue,
2518                 };
2519                 break;
2520         case RTE_ETH_FDIR_REJECT:
2521                 attributes->actions[0] = (struct rte_flow_action){
2522                         .type = RTE_FLOW_ACTION_TYPE_DROP,
2523                 };
2524                 break;
2525         default:
2526                 DRV_LOG(ERR, "port %u invalid behavior %d",
2527                         dev->data->port_id,
2528                         fdir_filter->action.behavior);
2529                 rte_errno = ENOTSUP;
2530                 return -rte_errno;
2531         }
2532         attributes->queue.index = fdir_filter->action.rx_queue;
2533         /* Handle L3. */
2534         switch (fdir_filter->input.flow_type) {
2535         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2536         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2537         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2538                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2539                         .src_addr = input->flow.ip4_flow.src_ip,
2540                         .dst_addr = input->flow.ip4_flow.dst_ip,
2541                         .time_to_live = input->flow.ip4_flow.ttl,
2542                         .type_of_service = input->flow.ip4_flow.tos,
2543                 };
2544                 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
2545                         .src_addr = mask->ipv4_mask.src_ip,
2546                         .dst_addr = mask->ipv4_mask.dst_ip,
2547                         .time_to_live = mask->ipv4_mask.ttl,
2548                         .type_of_service = mask->ipv4_mask.tos,
2549                         .next_proto_id = mask->ipv4_mask.proto,
2550                 };
2551                 attributes->items[1] = (struct rte_flow_item){
2552                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2553                         .spec = &attributes->l3,
2554                         .mask = &attributes->l3_mask,
2555                 };
2556                 break;
2557         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2558         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2559         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2560                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2561                         .hop_limits = input->flow.ipv6_flow.hop_limits,
2562                         .proto = input->flow.ipv6_flow.proto,
2563                 };
2564
2565                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2566                        input->flow.ipv6_flow.src_ip,
2567                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2568                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2569                        input->flow.ipv6_flow.dst_ip,
2570                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2571                 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
2572                        mask->ipv6_mask.src_ip,
2573                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2574                 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
2575                        mask->ipv6_mask.dst_ip,
2576                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2577                 attributes->items[1] = (struct rte_flow_item){
2578                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2579                         .spec = &attributes->l3,
2580                         .mask = &attributes->l3_mask,
2581                 };
2582                 break;
2583         default:
2584                 DRV_LOG(ERR, "port %u invalid flow type%d",
2585                         dev->data->port_id, fdir_filter->input.flow_type);
2586                 rte_errno = ENOTSUP;
2587                 return -rte_errno;
2588         }
2589         /* Handle L4. */
2590         switch (fdir_filter->input.flow_type) {
2591         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2592                 attributes->l4.udp.hdr = (struct udp_hdr){
2593                         .src_port = input->flow.udp4_flow.src_port,
2594                         .dst_port = input->flow.udp4_flow.dst_port,
2595                 };
2596                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2597                         .src_port = mask->src_port_mask,
2598                         .dst_port = mask->dst_port_mask,
2599                 };
2600                 attributes->items[2] = (struct rte_flow_item){
2601                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2602                         .spec = &attributes->l4,
2603                         .mask = &attributes->l4_mask,
2604                 };
2605                 break;
2606         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2607                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2608                         .src_port = input->flow.tcp4_flow.src_port,
2609                         .dst_port = input->flow.tcp4_flow.dst_port,
2610                 };
2611                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2612                         .src_port = mask->src_port_mask,
2613                         .dst_port = mask->dst_port_mask,
2614                 };
2615                 attributes->items[2] = (struct rte_flow_item){
2616                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2617                         .spec = &attributes->l4,
2618                         .mask = &attributes->l4_mask,
2619                 };
2620                 break;
2621         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2622                 attributes->l4.udp.hdr = (struct udp_hdr){
2623                         .src_port = input->flow.udp6_flow.src_port,
2624                         .dst_port = input->flow.udp6_flow.dst_port,
2625                 };
2626                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2627                         .src_port = mask->src_port_mask,
2628                         .dst_port = mask->dst_port_mask,
2629                 };
2630                 attributes->items[2] = (struct rte_flow_item){
2631                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2632                         .spec = &attributes->l4,
2633                         .mask = &attributes->l4_mask,
2634                 };
2635                 break;
2636         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2637                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2638                         .src_port = input->flow.tcp6_flow.src_port,
2639                         .dst_port = input->flow.tcp6_flow.dst_port,
2640                 };
2641                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2642                         .src_port = mask->src_port_mask,
2643                         .dst_port = mask->dst_port_mask,
2644                 };
2645                 attributes->items[2] = (struct rte_flow_item){
2646                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2647                         .spec = &attributes->l4,
2648                         .mask = &attributes->l4_mask,
2649                 };
2650                 break;
2651         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2652         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2653                 break;
2654         default:
2655                 DRV_LOG(ERR, "port %u invalid flow type%d",
2656                         dev->data->port_id, fdir_filter->input.flow_type);
2657                 rte_errno = ENOTSUP;
2658                 return -rte_errno;
2659         }
2660         return 0;
2661 }
2662
2663 #define FLOW_FDIR_CMP(f1, f2, fld) \
2664         memcmp(&(f1)->fld, &(f2)->fld, sizeof(f1->fld))
2665
2666 /**
2667  * Compare two FDIR flows. If items and actions are identical, the two flows are
2668  * regarded as same.
2669  *
2670  * @param dev
2671  *   Pointer to Ethernet device.
2672  * @param f1
2673  *   FDIR flow to compare.
2674  * @param f2
2675  *   FDIR flow to compare.
2676  *
2677  * @return
2678  *   Zero on match, 1 otherwise.
2679  */
2680 static int
2681 flow_fdir_cmp(const struct mlx5_fdir *f1, const struct mlx5_fdir *f2)
2682 {
2683         if (FLOW_FDIR_CMP(f1, f2, attr) ||
2684             FLOW_FDIR_CMP(f1, f2, l2) ||
2685             FLOW_FDIR_CMP(f1, f2, l2_mask) ||
2686             FLOW_FDIR_CMP(f1, f2, l3) ||
2687             FLOW_FDIR_CMP(f1, f2, l3_mask) ||
2688             FLOW_FDIR_CMP(f1, f2, l4) ||
2689             FLOW_FDIR_CMP(f1, f2, l4_mask) ||
2690             FLOW_FDIR_CMP(f1, f2, actions[0].type))
2691                 return 1;
2692         if (f1->actions[0].type == RTE_FLOW_ACTION_TYPE_QUEUE &&
2693             FLOW_FDIR_CMP(f1, f2, queue))
2694                 return 1;
2695         return 0;
2696 }
2697
2698 /**
2699  * Search device flow list to find out a matched FDIR flow.
2700  *
2701  * @param dev
2702  *   Pointer to Ethernet device.
2703  * @param fdir_flow
2704  *   FDIR flow to lookup.
2705  *
2706  * @return
2707  *   Pointer of flow if found, NULL otherwise.
2708  */
2709 static struct rte_flow *
2710 flow_fdir_filter_lookup(struct rte_eth_dev *dev, struct mlx5_fdir *fdir_flow)
2711 {
2712         struct mlx5_priv *priv = dev->data->dev_private;
2713         struct rte_flow *flow = NULL;
2714
2715         assert(fdir_flow);
2716         TAILQ_FOREACH(flow, &priv->flows, next) {
2717                 if (flow->fdir && !flow_fdir_cmp(flow->fdir, fdir_flow)) {
2718                         DRV_LOG(DEBUG, "port %u found FDIR flow %p",
2719                                 dev->data->port_id, (void *)flow);
2720                         break;
2721                 }
2722         }
2723         return flow;
2724 }
2725
2726 /**
2727  * Add new flow director filter and store it in list.
2728  *
2729  * @param dev
2730  *   Pointer to Ethernet device.
2731  * @param fdir_filter
2732  *   Flow director filter to add.
2733  *
2734  * @return
2735  *   0 on success, a negative errno value otherwise and rte_errno is set.
2736  */
2737 static int
2738 flow_fdir_filter_add(struct rte_eth_dev *dev,
2739                      const struct rte_eth_fdir_filter *fdir_filter)
2740 {
2741         struct mlx5_priv *priv = dev->data->dev_private;
2742         struct mlx5_fdir *fdir_flow;
2743         struct rte_flow *flow;
2744         int ret;
2745
2746         fdir_flow = rte_zmalloc(__func__, sizeof(*fdir_flow), 0);
2747         if (!fdir_flow) {
2748                 rte_errno = ENOMEM;
2749                 return -rte_errno;
2750         }
2751         ret = flow_fdir_filter_convert(dev, fdir_filter, fdir_flow);
2752         if (ret)
2753                 goto error;
2754         flow = flow_fdir_filter_lookup(dev, fdir_flow);
2755         if (flow) {
2756                 rte_errno = EEXIST;
2757                 goto error;
2758         }
2759         flow = flow_list_create(dev, &priv->flows, &fdir_flow->attr,
2760                                 fdir_flow->items, fdir_flow->actions, NULL);
2761         if (!flow)
2762                 goto error;
2763         assert(!flow->fdir);
2764         flow->fdir = fdir_flow;
2765         DRV_LOG(DEBUG, "port %u created FDIR flow %p",
2766                 dev->data->port_id, (void *)flow);
2767         return 0;
2768 error:
2769         rte_free(fdir_flow);
2770         return -rte_errno;
2771 }
2772
2773 /**
2774  * Delete specific filter.
2775  *
2776  * @param dev
2777  *   Pointer to Ethernet device.
2778  * @param fdir_filter
2779  *   Filter to be deleted.
2780  *
2781  * @return
2782  *   0 on success, a negative errno value otherwise and rte_errno is set.
2783  */
2784 static int
2785 flow_fdir_filter_delete(struct rte_eth_dev *dev,
2786                         const struct rte_eth_fdir_filter *fdir_filter)
2787 {
2788         struct mlx5_priv *priv = dev->data->dev_private;
2789         struct rte_flow *flow;
2790         struct mlx5_fdir fdir_flow = {
2791                 .attr.group = 0,
2792         };
2793         int ret;
2794
2795         ret = flow_fdir_filter_convert(dev, fdir_filter, &fdir_flow);
2796         if (ret)
2797                 return -rte_errno;
2798         flow = flow_fdir_filter_lookup(dev, &fdir_flow);
2799         if (!flow) {
2800                 rte_errno = ENOENT;
2801                 return -rte_errno;
2802         }
2803         flow_list_destroy(dev, &priv->flows, flow);
2804         DRV_LOG(DEBUG, "port %u deleted FDIR flow %p",
2805                 dev->data->port_id, (void *)flow);
2806         return 0;
2807 }
2808
2809 /**
2810  * Update queue for specific filter.
2811  *
2812  * @param dev
2813  *   Pointer to Ethernet device.
2814  * @param fdir_filter
2815  *   Filter to be updated.
2816  *
2817  * @return
2818  *   0 on success, a negative errno value otherwise and rte_errno is set.
2819  */
2820 static int
2821 flow_fdir_filter_update(struct rte_eth_dev *dev,
2822                         const struct rte_eth_fdir_filter *fdir_filter)
2823 {
2824         int ret;
2825
2826         ret = flow_fdir_filter_delete(dev, fdir_filter);
2827         if (ret)
2828                 return ret;
2829         return flow_fdir_filter_add(dev, fdir_filter);
2830 }
2831
2832 /**
2833  * Flush all filters.
2834  *
2835  * @param dev
2836  *   Pointer to Ethernet device.
2837  */
2838 static void
2839 flow_fdir_filter_flush(struct rte_eth_dev *dev)
2840 {
2841         struct mlx5_priv *priv = dev->data->dev_private;
2842
2843         mlx5_flow_list_flush(dev, &priv->flows);
2844 }
2845
2846 /**
2847  * Get flow director information.
2848  *
2849  * @param dev
2850  *   Pointer to Ethernet device.
2851  * @param[out] fdir_info
2852  *   Resulting flow director information.
2853  */
2854 static void
2855 flow_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
2856 {
2857         struct rte_eth_fdir_masks *mask =
2858                 &dev->data->dev_conf.fdir_conf.mask;
2859
2860         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
2861         fdir_info->guarant_spc = 0;
2862         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
2863         fdir_info->max_flexpayload = 0;
2864         fdir_info->flow_types_mask[0] = 0;
2865         fdir_info->flex_payload_unit = 0;
2866         fdir_info->max_flex_payload_segment_num = 0;
2867         fdir_info->flex_payload_limit = 0;
2868         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
2869 }
2870
2871 /**
2872  * Deal with flow director operations.
2873  *
2874  * @param dev
2875  *   Pointer to Ethernet device.
2876  * @param filter_op
2877  *   Operation to perform.
2878  * @param arg
2879  *   Pointer to operation-specific structure.
2880  *
2881  * @return
2882  *   0 on success, a negative errno value otherwise and rte_errno is set.
2883  */
2884 static int
2885 flow_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
2886                     void *arg)
2887 {
2888         enum rte_fdir_mode fdir_mode =
2889                 dev->data->dev_conf.fdir_conf.mode;
2890
2891         if (filter_op == RTE_ETH_FILTER_NOP)
2892                 return 0;
2893         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
2894             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
2895                 DRV_LOG(ERR, "port %u flow director mode %d not supported",
2896                         dev->data->port_id, fdir_mode);
2897                 rte_errno = EINVAL;
2898                 return -rte_errno;
2899         }
2900         switch (filter_op) {
2901         case RTE_ETH_FILTER_ADD:
2902                 return flow_fdir_filter_add(dev, arg);
2903         case RTE_ETH_FILTER_UPDATE:
2904                 return flow_fdir_filter_update(dev, arg);
2905         case RTE_ETH_FILTER_DELETE:
2906                 return flow_fdir_filter_delete(dev, arg);
2907         case RTE_ETH_FILTER_FLUSH:
2908                 flow_fdir_filter_flush(dev);
2909                 break;
2910         case RTE_ETH_FILTER_INFO:
2911                 flow_fdir_info_get(dev, arg);
2912                 break;
2913         default:
2914                 DRV_LOG(DEBUG, "port %u unknown operation %u",
2915                         dev->data->port_id, filter_op);
2916                 rte_errno = EINVAL;
2917                 return -rte_errno;
2918         }
2919         return 0;
2920 }
2921
2922 /**
2923  * Manage filter operations.
2924  *
2925  * @param dev
2926  *   Pointer to Ethernet device structure.
2927  * @param filter_type
2928  *   Filter type.
2929  * @param filter_op
2930  *   Operation to perform.
2931  * @param arg
2932  *   Pointer to operation-specific structure.
2933  *
2934  * @return
2935  *   0 on success, a negative errno value otherwise and rte_errno is set.
2936  */
2937 int
2938 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
2939                      enum rte_filter_type filter_type,
2940                      enum rte_filter_op filter_op,
2941                      void *arg)
2942 {
2943         switch (filter_type) {
2944         case RTE_ETH_FILTER_GENERIC:
2945                 if (filter_op != RTE_ETH_FILTER_GET) {
2946                         rte_errno = EINVAL;
2947                         return -rte_errno;
2948                 }
2949                 *(const void **)arg = &mlx5_flow_ops;
2950                 return 0;
2951         case RTE_ETH_FILTER_FDIR:
2952                 return flow_fdir_ctrl_func(dev, filter_op, arg);
2953         default:
2954                 DRV_LOG(ERR, "port %u filter type (%d) not supported",
2955                         dev->data->port_id, filter_type);
2956                 rte_errno = ENOTSUP;
2957                 return -rte_errno;
2958         }
2959         return 0;
2960 }