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