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