net/mlx5: allow port start with zero Rx queue
[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 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 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 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 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 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 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         for (i = 0; i != rss->queue_num; ++i) {
951                 if (!(*priv->rxqs)[rss->queue[i]])
952                         return rte_flow_error_set
953                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
954                                  &rss->queue[i], "queue is not configured");
955         }
956         if (attr->egress)
957                 return rte_flow_error_set(error, ENOTSUP,
958                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
959                                           "rss action not supported for "
960                                           "egress");
961         return 0;
962 }
963
964 /*
965  * Validate the count action.
966  *
967  * @param[in] dev
968  *   Pointer to the Ethernet device structure.
969  * @param[in] attr
970  *   Attributes of flow that includes this action.
971  * @param[out] error
972  *   Pointer to error structure.
973  *
974  * @return
975  *   0 on success, a negative errno value otherwise and rte_ernno is set.
976  */
977 int
978 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
979                                 const struct rte_flow_attr *attr,
980                                 struct rte_flow_error *error)
981 {
982         if (attr->egress)
983                 return rte_flow_error_set(error, ENOTSUP,
984                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
985                                           "count action not supported for "
986                                           "egress");
987         return 0;
988 }
989
990 /**
991  * Verify the @p attributes will be correctly understood by the NIC and store
992  * them in the @p flow if everything is correct.
993  *
994  * @param[in] dev
995  *   Pointer to the Ethernet device structure.
996  * @param[in] attributes
997  *   Pointer to flow attributes
998  * @param[out] error
999  *   Pointer to error structure.
1000  *
1001  * @return
1002  *   0 on success, a negative errno value otherwise and rte_errno is set.
1003  */
1004 int
1005 mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
1006                               const struct rte_flow_attr *attributes,
1007                               struct rte_flow_error *error)
1008 {
1009         struct priv *priv = dev->data->dev_private;
1010         uint32_t priority_max = priv->config.flow_prio - 1;
1011
1012         if (attributes->group)
1013                 return rte_flow_error_set(error, ENOTSUP,
1014                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1015                                           NULL, "groups is not supported");
1016         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
1017             attributes->priority >= priority_max)
1018                 return rte_flow_error_set(error, ENOTSUP,
1019                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1020                                           NULL, "priority out of range");
1021         if (attributes->egress)
1022                 return rte_flow_error_set(error, ENOTSUP,
1023                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
1024                                           "egress is not supported");
1025         if (attributes->transfer)
1026                 return rte_flow_error_set(error, ENOTSUP,
1027                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
1028                                           NULL, "transfer is not supported");
1029         if (!attributes->ingress)
1030                 return rte_flow_error_set(error, EINVAL,
1031                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1032                                           NULL,
1033                                           "ingress attribute is mandatory");
1034         return 0;
1035 }
1036
1037 /**
1038  * Validate Ethernet item.
1039  *
1040  * @param[in] item
1041  *   Item specification.
1042  * @param[in] item_flags
1043  *   Bit-fields that holds the items detected until now.
1044  * @param[out] error
1045  *   Pointer to error structure.
1046  *
1047  * @return
1048  *   0 on success, a negative errno value otherwise and rte_errno is set.
1049  */
1050 int
1051 mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
1052                             uint64_t item_flags,
1053                             struct rte_flow_error *error)
1054 {
1055         const struct rte_flow_item_eth *mask = item->mask;
1056         const struct rte_flow_item_eth nic_mask = {
1057                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1058                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1059                 .type = RTE_BE16(0xffff),
1060         };
1061         int ret;
1062         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1063         const uint64_t ethm = tunnel ? MLX5_FLOW_LAYER_INNER_L2 :
1064                                        MLX5_FLOW_LAYER_OUTER_L2;
1065
1066         if (item_flags & ethm)
1067                 return rte_flow_error_set(error, ENOTSUP,
1068                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1069                                           "multiple L2 layers not supported");
1070         if (!mask)
1071                 mask = &rte_flow_item_eth_mask;
1072         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1073                                         (const uint8_t *)&nic_mask,
1074                                         sizeof(struct rte_flow_item_eth),
1075                                         error);
1076         return ret;
1077 }
1078
1079 /**
1080  * Validate VLAN item.
1081  *
1082  * @param[in] item
1083  *   Item specification.
1084  * @param[in] item_flags
1085  *   Bit-fields that holds the items detected until now.
1086  * @param[out] error
1087  *   Pointer to error structure.
1088  *
1089  * @return
1090  *   0 on success, a negative errno value otherwise and rte_errno is set.
1091  */
1092 int
1093 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
1094                              uint64_t item_flags,
1095                              struct rte_flow_error *error)
1096 {
1097         const struct rte_flow_item_vlan *spec = item->spec;
1098         const struct rte_flow_item_vlan *mask = item->mask;
1099         const struct rte_flow_item_vlan nic_mask = {
1100                 .tci = RTE_BE16(0x0fff),
1101                 .inner_type = RTE_BE16(0xffff),
1102         };
1103         uint16_t vlan_tag = 0;
1104         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1105         int ret;
1106         const uint64_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1107                                         MLX5_FLOW_LAYER_INNER_L4) :
1108                                        (MLX5_FLOW_LAYER_OUTER_L3 |
1109                                         MLX5_FLOW_LAYER_OUTER_L4);
1110         const uint64_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1111                                         MLX5_FLOW_LAYER_OUTER_VLAN;
1112
1113         if (item_flags & vlanm)
1114                 return rte_flow_error_set(error, EINVAL,
1115                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1116                                           "multiple VLAN layers not supported");
1117         else if ((item_flags & l34m) != 0)
1118                 return rte_flow_error_set(error, EINVAL,
1119                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1120                                           "L2 layer cannot follow L3/L4 layer");
1121         if (!mask)
1122                 mask = &rte_flow_item_vlan_mask;
1123         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1124                                         (const uint8_t *)&nic_mask,
1125                                         sizeof(struct rte_flow_item_vlan),
1126                                         error);
1127         if (ret)
1128                 return ret;
1129         if (spec) {
1130                 vlan_tag = spec->tci;
1131                 vlan_tag &= mask->tci;
1132         }
1133         /*
1134          * From verbs perspective an empty VLAN is equivalent
1135          * to a packet without VLAN layer.
1136          */
1137         if (!vlan_tag)
1138                 return rte_flow_error_set(error, EINVAL,
1139                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1140                                           item->spec,
1141                                           "VLAN cannot be empty");
1142         return 0;
1143 }
1144
1145 /**
1146  * Validate IPV4 item.
1147  *
1148  * @param[in] item
1149  *   Item specification.
1150  * @param[in] item_flags
1151  *   Bit-fields that holds the items detected until now.
1152  * @param[in] acc_mask
1153  *   Acceptable mask, if NULL default internal default mask
1154  *   will be used to check whether item fields are supported.
1155  * @param[out] error
1156  *   Pointer to error structure.
1157  *
1158  * @return
1159  *   0 on success, a negative errno value otherwise and rte_errno is set.
1160  */
1161 int
1162 mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
1163                              uint64_t item_flags,
1164                              const struct rte_flow_item_ipv4 *acc_mask,
1165                              struct rte_flow_error *error)
1166 {
1167         const struct rte_flow_item_ipv4 *mask = item->mask;
1168         const struct rte_flow_item_ipv4 nic_mask = {
1169                 .hdr = {
1170                         .src_addr = RTE_BE32(0xffffffff),
1171                         .dst_addr = RTE_BE32(0xffffffff),
1172                         .type_of_service = 0xff,
1173                         .next_proto_id = 0xff,
1174                 },
1175         };
1176         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1177         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1178                                       MLX5_FLOW_LAYER_OUTER_L3;
1179         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1180                                       MLX5_FLOW_LAYER_OUTER_L4;
1181         int ret;
1182
1183         if (item_flags & l3m)
1184                 return rte_flow_error_set(error, ENOTSUP,
1185                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1186                                           "multiple L3 layers not supported");
1187         else if (item_flags & l4m)
1188                 return rte_flow_error_set(error, EINVAL,
1189                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1190                                           "L3 cannot follow an L4 layer.");
1191         if (!mask)
1192                 mask = &rte_flow_item_ipv4_mask;
1193         else if (mask->hdr.next_proto_id != 0 &&
1194                  mask->hdr.next_proto_id != 0xff)
1195                 return rte_flow_error_set(error, EINVAL,
1196                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1197                                           "partial mask is not supported"
1198                                           " for protocol");
1199         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1200                                         acc_mask ? (const uint8_t *)acc_mask
1201                                                  : (const uint8_t *)&nic_mask,
1202                                         sizeof(struct rte_flow_item_ipv4),
1203                                         error);
1204         if (ret < 0)
1205                 return ret;
1206         return 0;
1207 }
1208
1209 /**
1210  * Validate IPV6 item.
1211  *
1212  * @param[in] item
1213  *   Item specification.
1214  * @param[in] item_flags
1215  *   Bit-fields that holds the items detected until now.
1216  * @param[in] acc_mask
1217  *   Acceptable mask, if NULL default internal default mask
1218  *   will be used to check whether item fields are supported.
1219  * @param[out] error
1220  *   Pointer to error structure.
1221  *
1222  * @return
1223  *   0 on success, a negative errno value otherwise and rte_errno is set.
1224  */
1225 int
1226 mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
1227                              uint64_t item_flags,
1228                              const struct rte_flow_item_ipv6 *acc_mask,
1229                              struct rte_flow_error *error)
1230 {
1231         const struct rte_flow_item_ipv6 *mask = item->mask;
1232         const struct rte_flow_item_ipv6 nic_mask = {
1233                 .hdr = {
1234                         .src_addr =
1235                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1236                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1237                         .dst_addr =
1238                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1239                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1240                         .vtc_flow = RTE_BE32(0xffffffff),
1241                         .proto = 0xff,
1242                         .hop_limits = 0xff,
1243                 },
1244         };
1245         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1246         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1247                                       MLX5_FLOW_LAYER_OUTER_L3;
1248         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1249                                       MLX5_FLOW_LAYER_OUTER_L4;
1250         int ret;
1251
1252         if (item_flags & l3m)
1253                 return rte_flow_error_set(error, ENOTSUP,
1254                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1255                                           "multiple L3 layers not supported");
1256         else if (item_flags & l4m)
1257                 return rte_flow_error_set(error, EINVAL,
1258                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1259                                           "L3 cannot follow an L4 layer.");
1260         if (!mask)
1261                 mask = &rte_flow_item_ipv6_mask;
1262         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1263                                         acc_mask ? (const uint8_t *)acc_mask
1264                                                  : (const uint8_t *)&nic_mask,
1265                                         sizeof(struct rte_flow_item_ipv6),
1266                                         error);
1267         if (ret < 0)
1268                 return ret;
1269         return 0;
1270 }
1271
1272 /**
1273  * Validate UDP item.
1274  *
1275  * @param[in] item
1276  *   Item specification.
1277  * @param[in] item_flags
1278  *   Bit-fields that holds the items detected until now.
1279  * @param[in] target_protocol
1280  *   The next protocol in the previous item.
1281  * @param[in] flow_mask
1282  *   mlx5 flow-specific (TCF, DV, verbs, etc.) supported header fields mask.
1283  * @param[out] error
1284  *   Pointer to error structure.
1285  *
1286  * @return
1287  *   0 on success, a negative errno value otherwise and rte_errno is set.
1288  */
1289 int
1290 mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
1291                             uint64_t item_flags,
1292                             uint8_t target_protocol,
1293                             struct rte_flow_error *error)
1294 {
1295         const struct rte_flow_item_udp *mask = item->mask;
1296         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1297         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1298                                       MLX5_FLOW_LAYER_OUTER_L3;
1299         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1300                                       MLX5_FLOW_LAYER_OUTER_L4;
1301         int ret;
1302
1303         if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
1304                 return rte_flow_error_set(error, EINVAL,
1305                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1306                                           "protocol filtering not compatible"
1307                                           " with UDP layer");
1308         if (!(item_flags & l3m))
1309                 return rte_flow_error_set(error, EINVAL,
1310                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1311                                           "L3 is mandatory to filter on L4");
1312         if (item_flags & l4m)
1313                 return rte_flow_error_set(error, EINVAL,
1314                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1315                                           "multiple L4 layers not supported");
1316         if (!mask)
1317                 mask = &rte_flow_item_udp_mask;
1318         ret = mlx5_flow_item_acceptable
1319                 (item, (const uint8_t *)mask,
1320                  (const uint8_t *)&rte_flow_item_udp_mask,
1321                  sizeof(struct rte_flow_item_udp), error);
1322         if (ret < 0)
1323                 return ret;
1324         return 0;
1325 }
1326
1327 /**
1328  * Validate TCP item.
1329  *
1330  * @param[in] item
1331  *   Item specification.
1332  * @param[in] item_flags
1333  *   Bit-fields that holds the items detected until now.
1334  * @param[in] target_protocol
1335  *   The next protocol in the previous item.
1336  * @param[out] error
1337  *   Pointer to error structure.
1338  *
1339  * @return
1340  *   0 on success, a negative errno value otherwise and rte_errno is set.
1341  */
1342 int
1343 mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
1344                             uint64_t item_flags,
1345                             uint8_t target_protocol,
1346                             const struct rte_flow_item_tcp *flow_mask,
1347                             struct rte_flow_error *error)
1348 {
1349         const struct rte_flow_item_tcp *mask = item->mask;
1350         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1351         const uint64_t l3m = tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1352                                       MLX5_FLOW_LAYER_OUTER_L3;
1353         const uint64_t l4m = tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1354                                       MLX5_FLOW_LAYER_OUTER_L4;
1355         int ret;
1356
1357         assert(flow_mask);
1358         if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
1359                 return rte_flow_error_set(error, EINVAL,
1360                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1361                                           "protocol filtering not compatible"
1362                                           " with TCP layer");
1363         if (!(item_flags & l3m))
1364                 return rte_flow_error_set(error, EINVAL,
1365                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1366                                           "L3 is mandatory to filter on L4");
1367         if (item_flags & l4m)
1368                 return rte_flow_error_set(error, EINVAL,
1369                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1370                                           "multiple L4 layers not supported");
1371         if (!mask)
1372                 mask = &rte_flow_item_tcp_mask;
1373         ret = mlx5_flow_item_acceptable
1374                 (item, (const uint8_t *)mask,
1375                  (const uint8_t *)flow_mask,
1376                  sizeof(struct rte_flow_item_tcp), error);
1377         if (ret < 0)
1378                 return ret;
1379         return 0;
1380 }
1381
1382 /**
1383  * Validate VXLAN item.
1384  *
1385  * @param[in] item
1386  *   Item specification.
1387  * @param[in] item_flags
1388  *   Bit-fields that holds the items detected until now.
1389  * @param[in] target_protocol
1390  *   The next protocol in the previous item.
1391  * @param[out] error
1392  *   Pointer to error structure.
1393  *
1394  * @return
1395  *   0 on success, a negative errno value otherwise and rte_errno is set.
1396  */
1397 int
1398 mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
1399                               uint64_t item_flags,
1400                               struct rte_flow_error *error)
1401 {
1402         const struct rte_flow_item_vxlan *spec = item->spec;
1403         const struct rte_flow_item_vxlan *mask = item->mask;
1404         int ret;
1405         union vni {
1406                 uint32_t vlan_id;
1407                 uint8_t vni[4];
1408         } id = { .vlan_id = 0, };
1409         uint32_t vlan_id = 0;
1410
1411
1412         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1413                 return rte_flow_error_set(error, ENOTSUP,
1414                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1415                                           "multiple tunnel layers not"
1416                                           " supported");
1417         /*
1418          * Verify only UDPv4 is present as defined in
1419          * https://tools.ietf.org/html/rfc7348
1420          */
1421         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1422                 return rte_flow_error_set(error, EINVAL,
1423                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1424                                           "no outer UDP layer found");
1425         if (!mask)
1426                 mask = &rte_flow_item_vxlan_mask;
1427         ret = mlx5_flow_item_acceptable
1428                 (item, (const uint8_t *)mask,
1429                  (const uint8_t *)&rte_flow_item_vxlan_mask,
1430                  sizeof(struct rte_flow_item_vxlan),
1431                  error);
1432         if (ret < 0)
1433                 return ret;
1434         if (spec) {
1435                 memcpy(&id.vni[1], spec->vni, 3);
1436                 vlan_id = id.vlan_id;
1437                 memcpy(&id.vni[1], mask->vni, 3);
1438                 vlan_id &= id.vlan_id;
1439         }
1440         /*
1441          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if
1442          * only this layer is defined in the Verbs specification it is
1443          * interpreted as wildcard and all packets will match this
1444          * rule, if it follows a full stack layer (ex: eth / ipv4 /
1445          * udp), all packets matching the layers before will also
1446          * match this rule.  To avoid such situation, VNI 0 is
1447          * currently refused.
1448          */
1449         if (!vlan_id)
1450                 return rte_flow_error_set(error, ENOTSUP,
1451                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1452                                           "VXLAN vni cannot be 0");
1453         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1454                 return rte_flow_error_set(error, ENOTSUP,
1455                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1456                                           "VXLAN tunnel must be fully defined");
1457         return 0;
1458 }
1459
1460 /**
1461  * Validate VXLAN_GPE item.
1462  *
1463  * @param[in] item
1464  *   Item specification.
1465  * @param[in] item_flags
1466  *   Bit-fields that holds the items detected until now.
1467  * @param[in] priv
1468  *   Pointer to the private data structure.
1469  * @param[in] target_protocol
1470  *   The next protocol in the previous item.
1471  * @param[out] error
1472  *   Pointer to error structure.
1473  *
1474  * @return
1475  *   0 on success, a negative errno value otherwise and rte_errno is set.
1476  */
1477 int
1478 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
1479                                   uint64_t item_flags,
1480                                   struct rte_eth_dev *dev,
1481                                   struct rte_flow_error *error)
1482 {
1483         struct priv *priv = dev->data->dev_private;
1484         const struct rte_flow_item_vxlan_gpe *spec = item->spec;
1485         const struct rte_flow_item_vxlan_gpe *mask = item->mask;
1486         int ret;
1487         union vni {
1488                 uint32_t vlan_id;
1489                 uint8_t vni[4];
1490         } id = { .vlan_id = 0, };
1491         uint32_t vlan_id = 0;
1492
1493         if (!priv->config.l3_vxlan_en)
1494                 return rte_flow_error_set(error, ENOTSUP,
1495                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1496                                           "L3 VXLAN is not enabled by device"
1497                                           " parameter and/or not configured in"
1498                                           " firmware");
1499         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1500                 return rte_flow_error_set(error, ENOTSUP,
1501                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1502                                           "multiple tunnel layers not"
1503                                           " supported");
1504         /*
1505          * Verify only UDPv4 is present as defined in
1506          * https://tools.ietf.org/html/rfc7348
1507          */
1508         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1509                 return rte_flow_error_set(error, EINVAL,
1510                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1511                                           "no outer UDP layer found");
1512         if (!mask)
1513                 mask = &rte_flow_item_vxlan_gpe_mask;
1514         ret = mlx5_flow_item_acceptable
1515                 (item, (const uint8_t *)mask,
1516                  (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
1517                  sizeof(struct rte_flow_item_vxlan_gpe),
1518                  error);
1519         if (ret < 0)
1520                 return ret;
1521         if (spec) {
1522                 if (spec->protocol)
1523                         return rte_flow_error_set(error, ENOTSUP,
1524                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1525                                                   item,
1526                                                   "VxLAN-GPE protocol"
1527                                                   " not supported");
1528                 memcpy(&id.vni[1], spec->vni, 3);
1529                 vlan_id = id.vlan_id;
1530                 memcpy(&id.vni[1], mask->vni, 3);
1531                 vlan_id &= id.vlan_id;
1532         }
1533         /*
1534          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1535          * layer is defined in the Verbs specification it is interpreted as
1536          * wildcard and all packets will match this rule, if it follows a full
1537          * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1538          * before will also match this rule.  To avoid such situation, VNI 0
1539          * is currently refused.
1540          */
1541         if (!vlan_id)
1542                 return rte_flow_error_set(error, ENOTSUP,
1543                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1544                                           "VXLAN-GPE vni cannot be 0");
1545         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1546                 return rte_flow_error_set(error, ENOTSUP,
1547                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1548                                           "VXLAN-GPE tunnel must be fully"
1549                                           " defined");
1550         return 0;
1551 }
1552
1553 /**
1554  * Validate GRE item.
1555  *
1556  * @param[in] item
1557  *   Item specification.
1558  * @param[in] item_flags
1559  *   Bit flags to mark detected items.
1560  * @param[in] target_protocol
1561  *   The next protocol in the previous item.
1562  * @param[out] error
1563  *   Pointer to error structure.
1564  *
1565  * @return
1566  *   0 on success, a negative errno value otherwise and rte_errno is set.
1567  */
1568 int
1569 mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
1570                             uint64_t item_flags,
1571                             uint8_t target_protocol,
1572                             struct rte_flow_error *error)
1573 {
1574         const struct rte_flow_item_gre *spec __rte_unused = item->spec;
1575         const struct rte_flow_item_gre *mask = item->mask;
1576         int ret;
1577
1578         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
1579                 return rte_flow_error_set(error, EINVAL,
1580                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1581                                           "protocol filtering not compatible"
1582                                           " with this GRE layer");
1583         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1584                 return rte_flow_error_set(error, ENOTSUP,
1585                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1586                                           "multiple tunnel layers not"
1587                                           " supported");
1588         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
1589                 return rte_flow_error_set(error, ENOTSUP,
1590                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1591                                           "L3 Layer is missing");
1592         if (!mask)
1593                 mask = &rte_flow_item_gre_mask;
1594         ret = mlx5_flow_item_acceptable
1595                 (item, (const uint8_t *)mask,
1596                  (const uint8_t *)&rte_flow_item_gre_mask,
1597                  sizeof(struct rte_flow_item_gre), error);
1598         if (ret < 0)
1599                 return ret;
1600 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
1601         if (spec && (spec->protocol & mask->protocol))
1602                 return rte_flow_error_set(error, ENOTSUP,
1603                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1604                                           "without MPLS support the"
1605                                           " specification cannot be used for"
1606                                           " filtering");
1607 #endif
1608         return 0;
1609 }
1610
1611 /**
1612  * Validate MPLS item.
1613  *
1614  * @param[in] dev
1615  *   Pointer to the rte_eth_dev structure.
1616  * @param[in] item
1617  *   Item specification.
1618  * @param[in] item_flags
1619  *   Bit-fields that holds the items detected until now.
1620  * @param[in] prev_layer
1621  *   The protocol layer indicated in previous item.
1622  * @param[out] error
1623  *   Pointer to error structure.
1624  *
1625  * @return
1626  *   0 on success, a negative errno value otherwise and rte_errno is set.
1627  */
1628 int
1629 mlx5_flow_validate_item_mpls(struct rte_eth_dev *dev __rte_unused,
1630                              const struct rte_flow_item *item __rte_unused,
1631                              uint64_t item_flags __rte_unused,
1632                              uint64_t prev_layer __rte_unused,
1633                              struct rte_flow_error *error)
1634 {
1635 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1636         const struct rte_flow_item_mpls *mask = item->mask;
1637         struct priv *priv = dev->data->dev_private;
1638         int ret;
1639
1640         if (!priv->config.mpls_en)
1641                 return rte_flow_error_set(error, ENOTSUP,
1642                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1643                                           "MPLS not supported or"
1644                                           " disabled in firmware"
1645                                           " configuration.");
1646         /* MPLS over IP, UDP, GRE is allowed */
1647         if (!(prev_layer & (MLX5_FLOW_LAYER_OUTER_L3 |
1648                             MLX5_FLOW_LAYER_OUTER_L4_UDP |
1649                             MLX5_FLOW_LAYER_GRE)))
1650                 return rte_flow_error_set(error, EINVAL,
1651                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1652                                           "protocol filtering not compatible"
1653                                           " with MPLS layer");
1654         /* Multi-tunnel isn't allowed but MPLS over GRE is an exception. */
1655         if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
1656             !(item_flags & MLX5_FLOW_LAYER_GRE))
1657                 return rte_flow_error_set(error, ENOTSUP,
1658                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1659                                           "multiple tunnel layers not"
1660                                           " supported");
1661         if (!mask)
1662                 mask = &rte_flow_item_mpls_mask;
1663         ret = mlx5_flow_item_acceptable
1664                 (item, (const uint8_t *)mask,
1665                  (const uint8_t *)&rte_flow_item_mpls_mask,
1666                  sizeof(struct rte_flow_item_mpls), error);
1667         if (ret < 0)
1668                 return ret;
1669         return 0;
1670 #endif
1671         return rte_flow_error_set(error, ENOTSUP,
1672                                   RTE_FLOW_ERROR_TYPE_ITEM, item,
1673                                   "MPLS is not supported by Verbs, please"
1674                                   " update.");
1675 }
1676
1677 static int
1678 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
1679                    const struct rte_flow_attr *attr __rte_unused,
1680                    const struct rte_flow_item items[] __rte_unused,
1681                    const struct rte_flow_action actions[] __rte_unused,
1682                    struct rte_flow_error *error __rte_unused)
1683 {
1684         rte_errno = ENOTSUP;
1685         return -rte_errno;
1686 }
1687
1688 static struct mlx5_flow *
1689 flow_null_prepare(const struct rte_flow_attr *attr __rte_unused,
1690                   const struct rte_flow_item items[] __rte_unused,
1691                   const struct rte_flow_action actions[] __rte_unused,
1692                   struct rte_flow_error *error __rte_unused)
1693 {
1694         rte_errno = ENOTSUP;
1695         return NULL;
1696 }
1697
1698 static int
1699 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
1700                     struct mlx5_flow *dev_flow __rte_unused,
1701                     const struct rte_flow_attr *attr __rte_unused,
1702                     const struct rte_flow_item items[] __rte_unused,
1703                     const struct rte_flow_action actions[] __rte_unused,
1704                     struct rte_flow_error *error __rte_unused)
1705 {
1706         rte_errno = ENOTSUP;
1707         return -rte_errno;
1708 }
1709
1710 static int
1711 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
1712                 struct rte_flow *flow __rte_unused,
1713                 struct rte_flow_error *error __rte_unused)
1714 {
1715         rte_errno = ENOTSUP;
1716         return -rte_errno;
1717 }
1718
1719 static void
1720 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
1721                  struct rte_flow *flow __rte_unused)
1722 {
1723 }
1724
1725 static void
1726 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
1727                   struct rte_flow *flow __rte_unused)
1728 {
1729 }
1730
1731 static int
1732 flow_null_query(struct rte_eth_dev *dev __rte_unused,
1733                 struct rte_flow *flow __rte_unused,
1734                 const struct rte_flow_action *actions __rte_unused,
1735                 void *data __rte_unused,
1736                 struct rte_flow_error *error __rte_unused)
1737 {
1738         rte_errno = ENOTSUP;
1739         return -rte_errno;
1740 }
1741
1742 /* Void driver to protect from null pointer reference. */
1743 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
1744         .validate = flow_null_validate,
1745         .prepare = flow_null_prepare,
1746         .translate = flow_null_translate,
1747         .apply = flow_null_apply,
1748         .remove = flow_null_remove,
1749         .destroy = flow_null_destroy,
1750         .query = flow_null_query,
1751 };
1752
1753 /**
1754  * Select flow driver type according to flow attributes and device
1755  * configuration.
1756  *
1757  * @param[in] dev
1758  *   Pointer to the dev structure.
1759  * @param[in] attr
1760  *   Pointer to the flow attributes.
1761  *
1762  * @return
1763  *   flow driver type, MLX5_FLOW_TYPE_MAX otherwise.
1764  */
1765 static enum mlx5_flow_drv_type
1766 flow_get_drv_type(struct rte_eth_dev *dev, const struct rte_flow_attr *attr)
1767 {
1768         struct priv *priv = dev->data->dev_private;
1769         enum mlx5_flow_drv_type type = MLX5_FLOW_TYPE_MAX;
1770
1771         if (attr->transfer)
1772                 type = MLX5_FLOW_TYPE_TCF;
1773         else
1774                 type = priv->config.dv_flow_en ? MLX5_FLOW_TYPE_DV :
1775                                                  MLX5_FLOW_TYPE_VERBS;
1776         return type;
1777 }
1778
1779 #define flow_get_drv_ops(type) flow_drv_ops[type]
1780
1781 /**
1782  * Flow driver validation API. This abstracts calling driver specific functions.
1783  * The type of flow driver is determined according to flow attributes.
1784  *
1785  * @param[in] dev
1786  *   Pointer to the dev structure.
1787  * @param[in] attr
1788  *   Pointer to the flow attributes.
1789  * @param[in] items
1790  *   Pointer to the list of items.
1791  * @param[in] actions
1792  *   Pointer to the list of actions.
1793  * @param[out] error
1794  *   Pointer to the error structure.
1795  *
1796  * @return
1797  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1798  */
1799 static inline int
1800 flow_drv_validate(struct rte_eth_dev *dev,
1801                   const struct rte_flow_attr *attr,
1802                   const struct rte_flow_item items[],
1803                   const struct rte_flow_action actions[],
1804                   struct rte_flow_error *error)
1805 {
1806         const struct mlx5_flow_driver_ops *fops;
1807         enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
1808
1809         fops = flow_get_drv_ops(type);
1810         return fops->validate(dev, attr, items, actions, error);
1811 }
1812
1813 /**
1814  * Flow driver preparation API. This abstracts calling driver specific
1815  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
1816  * calculates the size of memory required for device flow, allocates the memory,
1817  * initializes the device flow and returns the pointer.
1818  *
1819  * @note
1820  *   This function initializes device flow structure such as dv, tcf or verbs in
1821  *   struct mlx5_flow. However, it is caller's responsibility to initialize the
1822  *   rest. For example, adding returning device flow to flow->dev_flow list and
1823  *   setting backward reference to the flow should be done out of this function.
1824  *   layers field is not filled either.
1825  *
1826  * @param[in] attr
1827  *   Pointer to the flow attributes.
1828  * @param[in] items
1829  *   Pointer to the list of items.
1830  * @param[in] actions
1831  *   Pointer to the list of actions.
1832  * @param[out] error
1833  *   Pointer to the error structure.
1834  *
1835  * @return
1836  *   Pointer to device flow on success, otherwise NULL and rte_ernno is set.
1837  */
1838 static inline struct mlx5_flow *
1839 flow_drv_prepare(const struct rte_flow *flow,
1840                  const struct rte_flow_attr *attr,
1841                  const struct rte_flow_item items[],
1842                  const struct rte_flow_action actions[],
1843                  struct rte_flow_error *error)
1844 {
1845         const struct mlx5_flow_driver_ops *fops;
1846         enum mlx5_flow_drv_type type = flow->drv_type;
1847
1848         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1849         fops = flow_get_drv_ops(type);
1850         return fops->prepare(attr, items, actions, error);
1851 }
1852
1853 /**
1854  * Flow driver translation API. This abstracts calling driver specific
1855  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
1856  * translates a generic flow into a driver flow. flow_drv_prepare() must
1857  * precede.
1858  *
1859  * @note
1860  *   dev_flow->layers could be filled as a result of parsing during translation
1861  *   if needed by flow_drv_apply(). dev_flow->flow->actions can also be filled
1862  *   if necessary. As a flow can have multiple dev_flows by RSS flow expansion,
1863  *   flow->actions could be overwritten even though all the expanded dev_flows
1864  *   have the same actions.
1865  *
1866  * @param[in] dev
1867  *   Pointer to the rte dev structure.
1868  * @param[in, out] dev_flow
1869  *   Pointer to the mlx5 flow.
1870  * @param[in] attr
1871  *   Pointer to the flow attributes.
1872  * @param[in] items
1873  *   Pointer to the list of items.
1874  * @param[in] actions
1875  *   Pointer to the list of actions.
1876  * @param[out] error
1877  *   Pointer to the error structure.
1878  *
1879  * @return
1880  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1881  */
1882 static inline int
1883 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
1884                    const struct rte_flow_attr *attr,
1885                    const struct rte_flow_item items[],
1886                    const struct rte_flow_action actions[],
1887                    struct rte_flow_error *error)
1888 {
1889         const struct mlx5_flow_driver_ops *fops;
1890         enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
1891
1892         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1893         fops = flow_get_drv_ops(type);
1894         return fops->translate(dev, dev_flow, attr, items, actions, error);
1895 }
1896
1897 /**
1898  * Flow driver apply API. This abstracts calling driver specific functions.
1899  * Parent flow (rte_flow) should have driver type (drv_type). It applies
1900  * translated driver flows on to device. flow_drv_translate() must precede.
1901  *
1902  * @param[in] dev
1903  *   Pointer to Ethernet device structure.
1904  * @param[in, out] flow
1905  *   Pointer to flow structure.
1906  * @param[out] error
1907  *   Pointer to error structure.
1908  *
1909  * @return
1910  *   0 on success, a negative errno value otherwise and rte_errno is set.
1911  */
1912 static inline int
1913 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1914                struct rte_flow_error *error)
1915 {
1916         const struct mlx5_flow_driver_ops *fops;
1917         enum mlx5_flow_drv_type type = flow->drv_type;
1918
1919         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1920         fops = flow_get_drv_ops(type);
1921         return fops->apply(dev, flow, error);
1922 }
1923
1924 /**
1925  * Flow driver remove API. This abstracts calling driver specific functions.
1926  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
1927  * on device. All the resources of the flow should be freed by calling
1928  * flow_drv_destroy().
1929  *
1930  * @param[in] dev
1931  *   Pointer to Ethernet device.
1932  * @param[in, out] flow
1933  *   Pointer to flow structure.
1934  */
1935 static inline void
1936 flow_drv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1937 {
1938         const struct mlx5_flow_driver_ops *fops;
1939         enum mlx5_flow_drv_type type = flow->drv_type;
1940
1941         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1942         fops = flow_get_drv_ops(type);
1943         fops->remove(dev, flow);
1944 }
1945
1946 /**
1947  * Flow driver destroy API. This abstracts calling driver specific functions.
1948  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
1949  * on device and releases resources of the flow.
1950  *
1951  * @param[in] dev
1952  *   Pointer to Ethernet device.
1953  * @param[in, out] flow
1954  *   Pointer to flow structure.
1955  */
1956 static inline void
1957 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1958 {
1959         const struct mlx5_flow_driver_ops *fops;
1960         enum mlx5_flow_drv_type type = flow->drv_type;
1961
1962         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1963         fops = flow_get_drv_ops(type);
1964         fops->destroy(dev, flow);
1965 }
1966
1967 /**
1968  * Validate a flow supported by the NIC.
1969  *
1970  * @see rte_flow_validate()
1971  * @see rte_flow_ops
1972  */
1973 int
1974 mlx5_flow_validate(struct rte_eth_dev *dev,
1975                    const struct rte_flow_attr *attr,
1976                    const struct rte_flow_item items[],
1977                    const struct rte_flow_action actions[],
1978                    struct rte_flow_error *error)
1979 {
1980         int ret;
1981
1982         ret = flow_drv_validate(dev, attr, items, actions, error);
1983         if (ret < 0)
1984                 return ret;
1985         return 0;
1986 }
1987
1988 /**
1989  * Get RSS action from the action list.
1990  *
1991  * @param[in] actions
1992  *   Pointer to the list of actions.
1993  *
1994  * @return
1995  *   Pointer to the RSS action if exist, else return NULL.
1996  */
1997 static const struct rte_flow_action_rss*
1998 flow_get_rss_action(const struct rte_flow_action actions[])
1999 {
2000         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2001                 switch (actions->type) {
2002                 case RTE_FLOW_ACTION_TYPE_RSS:
2003                         return (const struct rte_flow_action_rss *)
2004                                actions->conf;
2005                 default:
2006                         break;
2007                 }
2008         }
2009         return NULL;
2010 }
2011
2012 static unsigned int
2013 find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
2014 {
2015         const struct rte_flow_item *item;
2016         unsigned int has_vlan = 0;
2017
2018         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
2019                 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
2020                         has_vlan = 1;
2021                         break;
2022                 }
2023         }
2024         if (has_vlan)
2025                 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
2026                                        MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
2027         return rss_level < 2 ? MLX5_EXPANSION_ROOT :
2028                                MLX5_EXPANSION_ROOT_OUTER;
2029 }
2030
2031 /**
2032  * Create a flow and add it to @p list.
2033  *
2034  * @param dev
2035  *   Pointer to Ethernet device.
2036  * @param list
2037  *   Pointer to a TAILQ flow list.
2038  * @param[in] attr
2039  *   Flow rule attributes.
2040  * @param[in] items
2041  *   Pattern specification (list terminated by the END pattern item).
2042  * @param[in] actions
2043  *   Associated actions (list terminated by the END action).
2044  * @param[out] error
2045  *   Perform verbose error reporting if not NULL.
2046  *
2047  * @return
2048  *   A flow on success, NULL otherwise and rte_errno is set.
2049  */
2050 static struct rte_flow *
2051 flow_list_create(struct rte_eth_dev *dev, struct mlx5_flows *list,
2052                  const struct rte_flow_attr *attr,
2053                  const struct rte_flow_item items[],
2054                  const struct rte_flow_action actions[],
2055                  struct rte_flow_error *error)
2056 {
2057         struct rte_flow *flow = NULL;
2058         struct mlx5_flow *dev_flow;
2059         const struct rte_flow_action_rss *rss;
2060         union {
2061                 struct rte_flow_expand_rss buf;
2062                 uint8_t buffer[2048];
2063         } expand_buffer;
2064         struct rte_flow_expand_rss *buf = &expand_buffer.buf;
2065         int ret;
2066         uint32_t i;
2067         uint32_t flow_size;
2068
2069         ret = flow_drv_validate(dev, attr, items, actions, error);
2070         if (ret < 0)
2071                 return NULL;
2072         flow_size = sizeof(struct rte_flow);
2073         rss = flow_get_rss_action(actions);
2074         if (rss)
2075                 flow_size += RTE_ALIGN_CEIL(rss->queue_num * sizeof(uint16_t),
2076                                             sizeof(void *));
2077         else
2078                 flow_size += RTE_ALIGN_CEIL(sizeof(uint16_t), sizeof(void *));
2079         flow = rte_calloc(__func__, 1, flow_size, 0);
2080         flow->drv_type = flow_get_drv_type(dev, attr);
2081         assert(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
2082                flow->drv_type < MLX5_FLOW_TYPE_MAX);
2083         flow->queue = (void *)(flow + 1);
2084         LIST_INIT(&flow->dev_flows);
2085         if (rss && rss->types) {
2086                 unsigned int graph_root;
2087
2088                 graph_root = find_graph_root(items, rss->level);
2089                 ret = rte_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
2090                                           items, rss->types,
2091                                           mlx5_support_expansion,
2092                                           graph_root);
2093                 assert(ret > 0 &&
2094                        (unsigned int)ret < sizeof(expand_buffer.buffer));
2095         } else {
2096                 buf->entries = 1;
2097                 buf->entry[0].pattern = (void *)(uintptr_t)items;
2098         }
2099         for (i = 0; i < buf->entries; ++i) {
2100                 dev_flow = flow_drv_prepare(flow, attr, buf->entry[i].pattern,
2101                                             actions, error);
2102                 if (!dev_flow)
2103                         goto error;
2104                 dev_flow->flow = flow;
2105                 LIST_INSERT_HEAD(&flow->dev_flows, dev_flow, next);
2106                 ret = flow_drv_translate(dev, dev_flow, attr,
2107                                          buf->entry[i].pattern,
2108                                          actions, error);
2109                 if (ret < 0)
2110                         goto error;
2111         }
2112         if (dev->data->dev_started) {
2113                 ret = flow_drv_apply(dev, flow, error);
2114                 if (ret < 0)
2115                         goto error;
2116         }
2117         TAILQ_INSERT_TAIL(list, flow, next);
2118         flow_rxq_flags_set(dev, flow);
2119         return flow;
2120 error:
2121         ret = rte_errno; /* Save rte_errno before cleanup. */
2122         assert(flow);
2123         flow_drv_destroy(dev, flow);
2124         rte_free(flow);
2125         rte_errno = ret; /* Restore rte_errno. */
2126         return NULL;
2127 }
2128
2129 /**
2130  * Create a flow.
2131  *
2132  * @see rte_flow_create()
2133  * @see rte_flow_ops
2134  */
2135 struct rte_flow *
2136 mlx5_flow_create(struct rte_eth_dev *dev,
2137                  const struct rte_flow_attr *attr,
2138                  const struct rte_flow_item items[],
2139                  const struct rte_flow_action actions[],
2140                  struct rte_flow_error *error)
2141 {
2142         return flow_list_create(dev,
2143                                 &((struct priv *)dev->data->dev_private)->flows,
2144                                 attr, items, actions, error);
2145 }
2146
2147 /**
2148  * Destroy a flow in a list.
2149  *
2150  * @param dev
2151  *   Pointer to Ethernet device.
2152  * @param list
2153  *   Pointer to a TAILQ flow list.
2154  * @param[in] flow
2155  *   Flow to destroy.
2156  */
2157 static void
2158 flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2159                   struct rte_flow *flow)
2160 {
2161         /*
2162          * Update RX queue flags only if port is started, otherwise it is
2163          * already clean.
2164          */
2165         if (dev->data->dev_started)
2166                 flow_rxq_flags_trim(dev, flow);
2167         flow_drv_destroy(dev, flow);
2168         TAILQ_REMOVE(list, flow, next);
2169         rte_free(flow->fdir);
2170         rte_free(flow);
2171 }
2172
2173 /**
2174  * Destroy all flows.
2175  *
2176  * @param dev
2177  *   Pointer to Ethernet device.
2178  * @param list
2179  *   Pointer to a TAILQ flow list.
2180  */
2181 void
2182 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
2183 {
2184         while (!TAILQ_EMPTY(list)) {
2185                 struct rte_flow *flow;
2186
2187                 flow = TAILQ_FIRST(list);
2188                 flow_list_destroy(dev, list, flow);
2189         }
2190 }
2191
2192 /**
2193  * Remove all flows.
2194  *
2195  * @param dev
2196  *   Pointer to Ethernet device.
2197  * @param list
2198  *   Pointer to a TAILQ flow list.
2199  */
2200 void
2201 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
2202 {
2203         struct rte_flow *flow;
2204
2205         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next)
2206                 flow_drv_remove(dev, flow);
2207         flow_rxq_flags_clear(dev);
2208 }
2209
2210 /**
2211  * Add all flows.
2212  *
2213  * @param dev
2214  *   Pointer to Ethernet device.
2215  * @param list
2216  *   Pointer to a TAILQ flow list.
2217  *
2218  * @return
2219  *   0 on success, a negative errno value otherwise and rte_errno is set.
2220  */
2221 int
2222 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
2223 {
2224         struct rte_flow *flow;
2225         struct rte_flow_error error;
2226         int ret = 0;
2227
2228         TAILQ_FOREACH(flow, list, next) {
2229                 ret = flow_drv_apply(dev, flow, &error);
2230                 if (ret < 0)
2231                         goto error;
2232                 flow_rxq_flags_set(dev, flow);
2233         }
2234         return 0;
2235 error:
2236         ret = rte_errno; /* Save rte_errno before cleanup. */
2237         mlx5_flow_stop(dev, list);
2238         rte_errno = ret; /* Restore rte_errno. */
2239         return -rte_errno;
2240 }
2241
2242 /**
2243  * Verify the flow list is empty
2244  *
2245  * @param dev
2246  *  Pointer to Ethernet device.
2247  *
2248  * @return the number of flows not released.
2249  */
2250 int
2251 mlx5_flow_verify(struct rte_eth_dev *dev)
2252 {
2253         struct priv *priv = dev->data->dev_private;
2254         struct rte_flow *flow;
2255         int ret = 0;
2256
2257         TAILQ_FOREACH(flow, &priv->flows, next) {
2258                 DRV_LOG(DEBUG, "port %u flow %p still referenced",
2259                         dev->data->port_id, (void *)flow);
2260                 ++ret;
2261         }
2262         return ret;
2263 }
2264
2265 /**
2266  * Enable a control flow configured from the control plane.
2267  *
2268  * @param dev
2269  *   Pointer to Ethernet device.
2270  * @param eth_spec
2271  *   An Ethernet flow spec to apply.
2272  * @param eth_mask
2273  *   An Ethernet flow mask to apply.
2274  * @param vlan_spec
2275  *   A VLAN flow spec to apply.
2276  * @param vlan_mask
2277  *   A VLAN flow mask to apply.
2278  *
2279  * @return
2280  *   0 on success, a negative errno value otherwise and rte_errno is set.
2281  */
2282 int
2283 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
2284                     struct rte_flow_item_eth *eth_spec,
2285                     struct rte_flow_item_eth *eth_mask,
2286                     struct rte_flow_item_vlan *vlan_spec,
2287                     struct rte_flow_item_vlan *vlan_mask)
2288 {
2289         struct priv *priv = dev->data->dev_private;
2290         const struct rte_flow_attr attr = {
2291                 .ingress = 1,
2292                 .priority = MLX5_FLOW_PRIO_RSVD,
2293         };
2294         struct rte_flow_item items[] = {
2295                 {
2296                         .type = RTE_FLOW_ITEM_TYPE_ETH,
2297                         .spec = eth_spec,
2298                         .last = NULL,
2299                         .mask = eth_mask,
2300                 },
2301                 {
2302                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
2303                                               RTE_FLOW_ITEM_TYPE_END,
2304                         .spec = vlan_spec,
2305                         .last = NULL,
2306                         .mask = vlan_mask,
2307                 },
2308                 {
2309                         .type = RTE_FLOW_ITEM_TYPE_END,
2310                 },
2311         };
2312         uint16_t queue[priv->reta_idx_n];
2313         struct rte_flow_action_rss action_rss = {
2314                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2315                 .level = 0,
2316                 .types = priv->rss_conf.rss_hf,
2317                 .key_len = priv->rss_conf.rss_key_len,
2318                 .queue_num = priv->reta_idx_n,
2319                 .key = priv->rss_conf.rss_key,
2320                 .queue = queue,
2321         };
2322         struct rte_flow_action actions[] = {
2323                 {
2324                         .type = RTE_FLOW_ACTION_TYPE_RSS,
2325                         .conf = &action_rss,
2326                 },
2327                 {
2328                         .type = RTE_FLOW_ACTION_TYPE_END,
2329                 },
2330         };
2331         struct rte_flow *flow;
2332         struct rte_flow_error error;
2333         unsigned int i;
2334
2335         if (!priv->reta_idx_n || !priv->rxqs_n) {
2336                 return 0;
2337         }
2338         for (i = 0; i != priv->reta_idx_n; ++i)
2339                 queue[i] = (*priv->reta_idx)[i];
2340         flow = flow_list_create(dev, &priv->ctrl_flows,
2341                                 &attr, items, actions, &error);
2342         if (!flow)
2343                 return -rte_errno;
2344         return 0;
2345 }
2346
2347 /**
2348  * Enable a flow control configured from the control plane.
2349  *
2350  * @param dev
2351  *   Pointer to Ethernet device.
2352  * @param eth_spec
2353  *   An Ethernet flow spec to apply.
2354  * @param eth_mask
2355  *   An Ethernet flow mask to apply.
2356  *
2357  * @return
2358  *   0 on success, a negative errno value otherwise and rte_errno is set.
2359  */
2360 int
2361 mlx5_ctrl_flow(struct rte_eth_dev *dev,
2362                struct rte_flow_item_eth *eth_spec,
2363                struct rte_flow_item_eth *eth_mask)
2364 {
2365         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
2366 }
2367
2368 /**
2369  * Destroy a flow.
2370  *
2371  * @see rte_flow_destroy()
2372  * @see rte_flow_ops
2373  */
2374 int
2375 mlx5_flow_destroy(struct rte_eth_dev *dev,
2376                   struct rte_flow *flow,
2377                   struct rte_flow_error *error __rte_unused)
2378 {
2379         struct priv *priv = dev->data->dev_private;
2380
2381         flow_list_destroy(dev, &priv->flows, flow);
2382         return 0;
2383 }
2384
2385 /**
2386  * Destroy all flows.
2387  *
2388  * @see rte_flow_flush()
2389  * @see rte_flow_ops
2390  */
2391 int
2392 mlx5_flow_flush(struct rte_eth_dev *dev,
2393                 struct rte_flow_error *error __rte_unused)
2394 {
2395         struct priv *priv = dev->data->dev_private;
2396
2397         mlx5_flow_list_flush(dev, &priv->flows);
2398         return 0;
2399 }
2400
2401 /**
2402  * Isolated mode.
2403  *
2404  * @see rte_flow_isolate()
2405  * @see rte_flow_ops
2406  */
2407 int
2408 mlx5_flow_isolate(struct rte_eth_dev *dev,
2409                   int enable,
2410                   struct rte_flow_error *error)
2411 {
2412         struct priv *priv = dev->data->dev_private;
2413
2414         if (dev->data->dev_started) {
2415                 rte_flow_error_set(error, EBUSY,
2416                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2417                                    NULL,
2418                                    "port must be stopped first");
2419                 return -rte_errno;
2420         }
2421         priv->isolated = !!enable;
2422         if (enable)
2423                 dev->dev_ops = &mlx5_dev_ops_isolate;
2424         else
2425                 dev->dev_ops = &mlx5_dev_ops;
2426         return 0;
2427 }
2428
2429 /**
2430  * Query a flow.
2431  *
2432  * @see rte_flow_query()
2433  * @see rte_flow_ops
2434  */
2435 static int
2436 flow_drv_query(struct rte_eth_dev *dev,
2437                struct rte_flow *flow,
2438                const struct rte_flow_action *actions,
2439                void *data,
2440                struct rte_flow_error *error)
2441 {
2442         const struct mlx5_flow_driver_ops *fops;
2443         enum mlx5_flow_drv_type ftype = flow->drv_type;
2444
2445         assert(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
2446         fops = flow_get_drv_ops(ftype);
2447
2448         return fops->query(dev, flow, actions, data, error);
2449 }
2450
2451 /**
2452  * Query a flow.
2453  *
2454  * @see rte_flow_query()
2455  * @see rte_flow_ops
2456  */
2457 int
2458 mlx5_flow_query(struct rte_eth_dev *dev,
2459                 struct rte_flow *flow,
2460                 const struct rte_flow_action *actions,
2461                 void *data,
2462                 struct rte_flow_error *error)
2463 {
2464         int ret;
2465
2466         ret = flow_drv_query(dev, flow, actions, data, error);
2467         if (ret < 0)
2468                 return ret;
2469         return 0;
2470 }
2471
2472 /**
2473  * Convert a flow director filter to a generic flow.
2474  *
2475  * @param dev
2476  *   Pointer to Ethernet device.
2477  * @param fdir_filter
2478  *   Flow director filter to add.
2479  * @param attributes
2480  *   Generic flow parameters structure.
2481  *
2482  * @return
2483  *   0 on success, a negative errno value otherwise and rte_errno is set.
2484  */
2485 static int
2486 flow_fdir_filter_convert(struct rte_eth_dev *dev,
2487                          const struct rte_eth_fdir_filter *fdir_filter,
2488                          struct mlx5_fdir *attributes)
2489 {
2490         struct priv *priv = dev->data->dev_private;
2491         const struct rte_eth_fdir_input *input = &fdir_filter->input;
2492         const struct rte_eth_fdir_masks *mask =
2493                 &dev->data->dev_conf.fdir_conf.mask;
2494
2495         /* Validate queue number. */
2496         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
2497                 DRV_LOG(ERR, "port %u invalid queue number %d",
2498                         dev->data->port_id, fdir_filter->action.rx_queue);
2499                 rte_errno = EINVAL;
2500                 return -rte_errno;
2501         }
2502         attributes->attr.ingress = 1;
2503         attributes->items[0] = (struct rte_flow_item) {
2504                 .type = RTE_FLOW_ITEM_TYPE_ETH,
2505                 .spec = &attributes->l2,
2506                 .mask = &attributes->l2_mask,
2507         };
2508         switch (fdir_filter->action.behavior) {
2509         case RTE_ETH_FDIR_ACCEPT:
2510                 attributes->actions[0] = (struct rte_flow_action){
2511                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
2512                         .conf = &attributes->queue,
2513                 };
2514                 break;
2515         case RTE_ETH_FDIR_REJECT:
2516                 attributes->actions[0] = (struct rte_flow_action){
2517                         .type = RTE_FLOW_ACTION_TYPE_DROP,
2518                 };
2519                 break;
2520         default:
2521                 DRV_LOG(ERR, "port %u invalid behavior %d",
2522                         dev->data->port_id,
2523                         fdir_filter->action.behavior);
2524                 rte_errno = ENOTSUP;
2525                 return -rte_errno;
2526         }
2527         attributes->queue.index = fdir_filter->action.rx_queue;
2528         /* Handle L3. */
2529         switch (fdir_filter->input.flow_type) {
2530         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2531         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2532         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2533                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2534                         .src_addr = input->flow.ip4_flow.src_ip,
2535                         .dst_addr = input->flow.ip4_flow.dst_ip,
2536                         .time_to_live = input->flow.ip4_flow.ttl,
2537                         .type_of_service = input->flow.ip4_flow.tos,
2538                 };
2539                 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
2540                         .src_addr = mask->ipv4_mask.src_ip,
2541                         .dst_addr = mask->ipv4_mask.dst_ip,
2542                         .time_to_live = mask->ipv4_mask.ttl,
2543                         .type_of_service = mask->ipv4_mask.tos,
2544                         .next_proto_id = mask->ipv4_mask.proto,
2545                 };
2546                 attributes->items[1] = (struct rte_flow_item){
2547                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2548                         .spec = &attributes->l3,
2549                         .mask = &attributes->l3_mask,
2550                 };
2551                 break;
2552         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2553         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2554         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2555                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2556                         .hop_limits = input->flow.ipv6_flow.hop_limits,
2557                         .proto = input->flow.ipv6_flow.proto,
2558                 };
2559
2560                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2561                        input->flow.ipv6_flow.src_ip,
2562                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2563                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2564                        input->flow.ipv6_flow.dst_ip,
2565                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2566                 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
2567                        mask->ipv6_mask.src_ip,
2568                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2569                 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
2570                        mask->ipv6_mask.dst_ip,
2571                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2572                 attributes->items[1] = (struct rte_flow_item){
2573                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2574                         .spec = &attributes->l3,
2575                         .mask = &attributes->l3_mask,
2576                 };
2577                 break;
2578         default:
2579                 DRV_LOG(ERR, "port %u invalid flow type%d",
2580                         dev->data->port_id, fdir_filter->input.flow_type);
2581                 rte_errno = ENOTSUP;
2582                 return -rte_errno;
2583         }
2584         /* Handle L4. */
2585         switch (fdir_filter->input.flow_type) {
2586         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2587                 attributes->l4.udp.hdr = (struct udp_hdr){
2588                         .src_port = input->flow.udp4_flow.src_port,
2589                         .dst_port = input->flow.udp4_flow.dst_port,
2590                 };
2591                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2592                         .src_port = mask->src_port_mask,
2593                         .dst_port = mask->dst_port_mask,
2594                 };
2595                 attributes->items[2] = (struct rte_flow_item){
2596                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2597                         .spec = &attributes->l4,
2598                         .mask = &attributes->l4_mask,
2599                 };
2600                 break;
2601         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2602                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2603                         .src_port = input->flow.tcp4_flow.src_port,
2604                         .dst_port = input->flow.tcp4_flow.dst_port,
2605                 };
2606                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2607                         .src_port = mask->src_port_mask,
2608                         .dst_port = mask->dst_port_mask,
2609                 };
2610                 attributes->items[2] = (struct rte_flow_item){
2611                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2612                         .spec = &attributes->l4,
2613                         .mask = &attributes->l4_mask,
2614                 };
2615                 break;
2616         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2617                 attributes->l4.udp.hdr = (struct udp_hdr){
2618                         .src_port = input->flow.udp6_flow.src_port,
2619                         .dst_port = input->flow.udp6_flow.dst_port,
2620                 };
2621                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2622                         .src_port = mask->src_port_mask,
2623                         .dst_port = mask->dst_port_mask,
2624                 };
2625                 attributes->items[2] = (struct rte_flow_item){
2626                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2627                         .spec = &attributes->l4,
2628                         .mask = &attributes->l4_mask,
2629                 };
2630                 break;
2631         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2632                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2633                         .src_port = input->flow.tcp6_flow.src_port,
2634                         .dst_port = input->flow.tcp6_flow.dst_port,
2635                 };
2636                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2637                         .src_port = mask->src_port_mask,
2638                         .dst_port = mask->dst_port_mask,
2639                 };
2640                 attributes->items[2] = (struct rte_flow_item){
2641                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2642                         .spec = &attributes->l4,
2643                         .mask = &attributes->l4_mask,
2644                 };
2645                 break;
2646         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2647         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2648                 break;
2649         default:
2650                 DRV_LOG(ERR, "port %u invalid flow type%d",
2651                         dev->data->port_id, fdir_filter->input.flow_type);
2652                 rte_errno = ENOTSUP;
2653                 return -rte_errno;
2654         }
2655         return 0;
2656 }
2657
2658 #define FLOW_FDIR_CMP(f1, f2, fld) \
2659         memcmp(&(f1)->fld, &(f2)->fld, sizeof(f1->fld))
2660
2661 /**
2662  * Compare two FDIR flows. If items and actions are identical, the two flows are
2663  * regarded as same.
2664  *
2665  * @param dev
2666  *   Pointer to Ethernet device.
2667  * @param f1
2668  *   FDIR flow to compare.
2669  * @param f2
2670  *   FDIR flow to compare.
2671  *
2672  * @return
2673  *   Zero on match, 1 otherwise.
2674  */
2675 static int
2676 flow_fdir_cmp(const struct mlx5_fdir *f1, const struct mlx5_fdir *f2)
2677 {
2678         if (FLOW_FDIR_CMP(f1, f2, attr) ||
2679             FLOW_FDIR_CMP(f1, f2, l2) ||
2680             FLOW_FDIR_CMP(f1, f2, l2_mask) ||
2681             FLOW_FDIR_CMP(f1, f2, l3) ||
2682             FLOW_FDIR_CMP(f1, f2, l3_mask) ||
2683             FLOW_FDIR_CMP(f1, f2, l4) ||
2684             FLOW_FDIR_CMP(f1, f2, l4_mask) ||
2685             FLOW_FDIR_CMP(f1, f2, actions[0].type))
2686                 return 1;
2687         if (f1->actions[0].type == RTE_FLOW_ACTION_TYPE_QUEUE &&
2688             FLOW_FDIR_CMP(f1, f2, queue))
2689                 return 1;
2690         return 0;
2691 }
2692
2693 /**
2694  * Search device flow list to find out a matched FDIR flow.
2695  *
2696  * @param dev
2697  *   Pointer to Ethernet device.
2698  * @param fdir_flow
2699  *   FDIR flow to lookup.
2700  *
2701  * @return
2702  *   Pointer of flow if found, NULL otherwise.
2703  */
2704 static struct rte_flow *
2705 flow_fdir_filter_lookup(struct rte_eth_dev *dev, struct mlx5_fdir *fdir_flow)
2706 {
2707         struct priv *priv = dev->data->dev_private;
2708         struct rte_flow *flow = NULL;
2709
2710         assert(fdir_flow);
2711         TAILQ_FOREACH(flow, &priv->flows, next) {
2712                 if (flow->fdir && !flow_fdir_cmp(flow->fdir, fdir_flow)) {
2713                         DRV_LOG(DEBUG, "port %u found FDIR flow %p",
2714                                 dev->data->port_id, (void *)flow);
2715                         break;
2716                 }
2717         }
2718         return flow;
2719 }
2720
2721 /**
2722  * Add new flow director filter and store it in list.
2723  *
2724  * @param dev
2725  *   Pointer to Ethernet device.
2726  * @param fdir_filter
2727  *   Flow director filter to add.
2728  *
2729  * @return
2730  *   0 on success, a negative errno value otherwise and rte_errno is set.
2731  */
2732 static int
2733 flow_fdir_filter_add(struct rte_eth_dev *dev,
2734                      const struct rte_eth_fdir_filter *fdir_filter)
2735 {
2736         struct priv *priv = dev->data->dev_private;
2737         struct mlx5_fdir *fdir_flow;
2738         struct rte_flow *flow;
2739         int ret;
2740
2741         fdir_flow = rte_zmalloc(__func__, sizeof(*fdir_flow), 0);
2742         if (!fdir_flow) {
2743                 rte_errno = ENOMEM;
2744                 return -rte_errno;
2745         }
2746         ret = flow_fdir_filter_convert(dev, fdir_filter, fdir_flow);
2747         if (ret)
2748                 goto error;
2749         flow = flow_fdir_filter_lookup(dev, fdir_flow);
2750         if (flow) {
2751                 rte_errno = EEXIST;
2752                 goto error;
2753         }
2754         flow = flow_list_create(dev, &priv->flows, &fdir_flow->attr,
2755                                 fdir_flow->items, fdir_flow->actions, NULL);
2756         if (!flow)
2757                 goto error;
2758         assert(!flow->fdir);
2759         flow->fdir = fdir_flow;
2760         DRV_LOG(DEBUG, "port %u created FDIR flow %p",
2761                 dev->data->port_id, (void *)flow);
2762         return 0;
2763 error:
2764         rte_free(fdir_flow);
2765         return -rte_errno;
2766 }
2767
2768 /**
2769  * Delete specific filter.
2770  *
2771  * @param dev
2772  *   Pointer to Ethernet device.
2773  * @param fdir_filter
2774  *   Filter to be deleted.
2775  *
2776  * @return
2777  *   0 on success, a negative errno value otherwise and rte_errno is set.
2778  */
2779 static int
2780 flow_fdir_filter_delete(struct rte_eth_dev *dev,
2781                         const struct rte_eth_fdir_filter *fdir_filter)
2782 {
2783         struct priv *priv = dev->data->dev_private;
2784         struct rte_flow *flow;
2785         struct mlx5_fdir fdir_flow = {
2786                 .attr.group = 0,
2787         };
2788         int ret;
2789
2790         ret = flow_fdir_filter_convert(dev, fdir_filter, &fdir_flow);
2791         if (ret)
2792                 return -rte_errno;
2793         flow = flow_fdir_filter_lookup(dev, &fdir_flow);
2794         if (!flow) {
2795                 rte_errno = ENOENT;
2796                 return -rte_errno;
2797         }
2798         flow_list_destroy(dev, &priv->flows, flow);
2799         DRV_LOG(DEBUG, "port %u deleted FDIR flow %p",
2800                 dev->data->port_id, (void *)flow);
2801         return 0;
2802 }
2803
2804 /**
2805  * Update queue for specific filter.
2806  *
2807  * @param dev
2808  *   Pointer to Ethernet device.
2809  * @param fdir_filter
2810  *   Filter to be updated.
2811  *
2812  * @return
2813  *   0 on success, a negative errno value otherwise and rte_errno is set.
2814  */
2815 static int
2816 flow_fdir_filter_update(struct rte_eth_dev *dev,
2817                         const struct rte_eth_fdir_filter *fdir_filter)
2818 {
2819         int ret;
2820
2821         ret = flow_fdir_filter_delete(dev, fdir_filter);
2822         if (ret)
2823                 return ret;
2824         return flow_fdir_filter_add(dev, fdir_filter);
2825 }
2826
2827 /**
2828  * Flush all filters.
2829  *
2830  * @param dev
2831  *   Pointer to Ethernet device.
2832  */
2833 static void
2834 flow_fdir_filter_flush(struct rte_eth_dev *dev)
2835 {
2836         struct priv *priv = dev->data->dev_private;
2837
2838         mlx5_flow_list_flush(dev, &priv->flows);
2839 }
2840
2841 /**
2842  * Get flow director information.
2843  *
2844  * @param dev
2845  *   Pointer to Ethernet device.
2846  * @param[out] fdir_info
2847  *   Resulting flow director information.
2848  */
2849 static void
2850 flow_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
2851 {
2852         struct rte_eth_fdir_masks *mask =
2853                 &dev->data->dev_conf.fdir_conf.mask;
2854
2855         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
2856         fdir_info->guarant_spc = 0;
2857         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
2858         fdir_info->max_flexpayload = 0;
2859         fdir_info->flow_types_mask[0] = 0;
2860         fdir_info->flex_payload_unit = 0;
2861         fdir_info->max_flex_payload_segment_num = 0;
2862         fdir_info->flex_payload_limit = 0;
2863         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
2864 }
2865
2866 /**
2867  * Deal with flow director operations.
2868  *
2869  * @param dev
2870  *   Pointer to Ethernet device.
2871  * @param filter_op
2872  *   Operation to perform.
2873  * @param arg
2874  *   Pointer to operation-specific structure.
2875  *
2876  * @return
2877  *   0 on success, a negative errno value otherwise and rte_errno is set.
2878  */
2879 static int
2880 flow_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
2881                     void *arg)
2882 {
2883         enum rte_fdir_mode fdir_mode =
2884                 dev->data->dev_conf.fdir_conf.mode;
2885
2886         if (filter_op == RTE_ETH_FILTER_NOP)
2887                 return 0;
2888         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
2889             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
2890                 DRV_LOG(ERR, "port %u flow director mode %d not supported",
2891                         dev->data->port_id, fdir_mode);
2892                 rte_errno = EINVAL;
2893                 return -rte_errno;
2894         }
2895         switch (filter_op) {
2896         case RTE_ETH_FILTER_ADD:
2897                 return flow_fdir_filter_add(dev, arg);
2898         case RTE_ETH_FILTER_UPDATE:
2899                 return flow_fdir_filter_update(dev, arg);
2900         case RTE_ETH_FILTER_DELETE:
2901                 return flow_fdir_filter_delete(dev, arg);
2902         case RTE_ETH_FILTER_FLUSH:
2903                 flow_fdir_filter_flush(dev);
2904                 break;
2905         case RTE_ETH_FILTER_INFO:
2906                 flow_fdir_info_get(dev, arg);
2907                 break;
2908         default:
2909                 DRV_LOG(DEBUG, "port %u unknown operation %u",
2910                         dev->data->port_id, filter_op);
2911                 rte_errno = EINVAL;
2912                 return -rte_errno;
2913         }
2914         return 0;
2915 }
2916
2917 /**
2918  * Manage filter operations.
2919  *
2920  * @param dev
2921  *   Pointer to Ethernet device structure.
2922  * @param filter_type
2923  *   Filter type.
2924  * @param filter_op
2925  *   Operation to perform.
2926  * @param arg
2927  *   Pointer to operation-specific structure.
2928  *
2929  * @return
2930  *   0 on success, a negative errno value otherwise and rte_errno is set.
2931  */
2932 int
2933 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
2934                      enum rte_filter_type filter_type,
2935                      enum rte_filter_op filter_op,
2936                      void *arg)
2937 {
2938         switch (filter_type) {
2939         case RTE_ETH_FILTER_GENERIC:
2940                 if (filter_op != RTE_ETH_FILTER_GET) {
2941                         rte_errno = EINVAL;
2942                         return -rte_errno;
2943                 }
2944                 *(const void **)arg = &mlx5_flow_ops;
2945                 return 0;
2946         case RTE_ETH_FILTER_FDIR:
2947                 return flow_fdir_ctrl_func(dev, filter_op, arg);
2948         default:
2949                 DRV_LOG(ERR, "port %u filter type (%d) not supported",
2950                         dev->data->port_id, filter_type);
2951                 rte_errno = ENOTSUP;
2952                 return -rte_errno;
2953         }
2954         return 0;
2955 }