net/mlx5: simplify flow counters support check
[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, uint32_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 mlx5_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 flow.
529  *
530  * @param[in] dev
531  *   Pointer to the Ethernet device structure.
532  * @param[in] flow
533  *   Pointer to flow structure.
534  */
535 static void
536 mlx5_flow_rxq_flags_set(struct rte_eth_dev *dev, struct rte_flow *flow)
537 {
538         struct priv *priv = dev->data->dev_private;
539         const int mark = !!(flow->actions &
540                             (MLX5_FLOW_ACTION_FLAG | MLX5_FLOW_ACTION_MARK));
541         const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
542         unsigned int i;
543
544         for (i = 0; i != flow->rss.queue_num; ++i) {
545                 int idx = (*flow->queue)[i];
546                 struct mlx5_rxq_ctrl *rxq_ctrl =
547                         container_of((*priv->rxqs)[idx],
548                                      struct mlx5_rxq_ctrl, rxq);
549
550                 if (mark) {
551                         rxq_ctrl->rxq.mark = 1;
552                         rxq_ctrl->flow_mark_n++;
553                 }
554                 if (tunnel) {
555                         unsigned int j;
556
557                         /* Increase the counter matching the flow. */
558                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
559                                 if ((tunnels_info[j].tunnel & flow->layers) ==
560                                     tunnels_info[j].tunnel) {
561                                         rxq_ctrl->flow_tunnels_n[j]++;
562                                         break;
563                                 }
564                         }
565                         mlx5_flow_rxq_tunnel_ptype_update(rxq_ctrl);
566                 }
567         }
568 }
569
570 /**
571  * Clear the Rx queue flags (Mark/Flag and Tunnel Ptype) associated with the
572  * @p flow if no other flow uses it with the same kind of request.
573  *
574  * @param dev
575  *   Pointer to Ethernet device.
576  * @param[in] flow
577  *   Pointer to the flow.
578  */
579 static void
580 mlx5_flow_rxq_flags_trim(struct rte_eth_dev *dev, struct rte_flow *flow)
581 {
582         struct priv *priv = dev->data->dev_private;
583         const int mark = !!(flow->actions &
584                             (MLX5_FLOW_ACTION_FLAG | MLX5_FLOW_ACTION_MARK));
585         const int tunnel = !!(flow->layers & MLX5_FLOW_LAYER_TUNNEL);
586         unsigned int i;
587
588         assert(dev->data->dev_started);
589         for (i = 0; i != flow->rss.queue_num; ++i) {
590                 int idx = (*flow->queue)[i];
591                 struct mlx5_rxq_ctrl *rxq_ctrl =
592                         container_of((*priv->rxqs)[idx],
593                                      struct mlx5_rxq_ctrl, rxq);
594
595                 if (mark) {
596                         rxq_ctrl->flow_mark_n--;
597                         rxq_ctrl->rxq.mark = !!rxq_ctrl->flow_mark_n;
598                 }
599                 if (tunnel) {
600                         unsigned int j;
601
602                         /* Decrease the counter matching the flow. */
603                         for (j = 0; j != MLX5_FLOW_TUNNEL; ++j) {
604                                 if ((tunnels_info[j].tunnel & flow->layers) ==
605                                     tunnels_info[j].tunnel) {
606                                         rxq_ctrl->flow_tunnels_n[j]--;
607                                         break;
608                                 }
609                         }
610                         mlx5_flow_rxq_tunnel_ptype_update(rxq_ctrl);
611                 }
612         }
613 }
614
615 /**
616  * Clear the Mark/Flag and Tunnel ptype information in all Rx queues.
617  *
618  * @param dev
619  *   Pointer to Ethernet device.
620  */
621 static void
622 mlx5_flow_rxq_flags_clear(struct rte_eth_dev *dev)
623 {
624         struct priv *priv = dev->data->dev_private;
625         unsigned int i;
626
627         for (i = 0; i != priv->rxqs_n; ++i) {
628                 struct mlx5_rxq_ctrl *rxq_ctrl;
629                 unsigned int j;
630
631                 if (!(*priv->rxqs)[i])
632                         continue;
633                 rxq_ctrl = container_of((*priv->rxqs)[i],
634                                         struct mlx5_rxq_ctrl, rxq);
635                 rxq_ctrl->flow_mark_n = 0;
636                 rxq_ctrl->rxq.mark = 0;
637                 for (j = 0; j != MLX5_FLOW_TUNNEL; ++j)
638                         rxq_ctrl->flow_tunnels_n[j] = 0;
639                 rxq_ctrl->rxq.tunnel = 0;
640         }
641 }
642
643 /*
644  * Validate the flag action.
645  *
646  * @param[in] action_flags
647  *   Bit-fields that holds the actions detected until now.
648  * @param[in] attr
649  *   Attributes of flow that includes this action.
650  * @param[out] error
651  *   Pointer to error structure.
652  *
653  * @return
654  *   0 on success, a negative errno value otherwise and rte_errno is set.
655  */
656 int
657 mlx5_flow_validate_action_flag(uint64_t action_flags,
658                                const struct rte_flow_attr *attr,
659                                struct rte_flow_error *error)
660 {
661
662         if (action_flags & MLX5_FLOW_ACTION_DROP)
663                 return rte_flow_error_set(error, EINVAL,
664                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
665                                           "can't drop and flag in same flow");
666         if (action_flags & MLX5_FLOW_ACTION_MARK)
667                 return rte_flow_error_set(error, EINVAL,
668                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
669                                           "can't mark and flag in same flow");
670         if (action_flags & MLX5_FLOW_ACTION_FLAG)
671                 return rte_flow_error_set(error, EINVAL,
672                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
673                                           "can't have 2 flag"
674                                           " actions in same flow");
675         if (attr->egress)
676                 return rte_flow_error_set(error, ENOTSUP,
677                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
678                                           "flag action not supported for "
679                                           "egress");
680         return 0;
681 }
682
683 /*
684  * Validate the mark action.
685  *
686  * @param[in] action
687  *   Pointer to the queue action.
688  * @param[in] action_flags
689  *   Bit-fields that holds the actions detected until now.
690  * @param[in] attr
691  *   Attributes of flow that includes this action.
692  * @param[out] error
693  *   Pointer to error structure.
694  *
695  * @return
696  *   0 on success, a negative errno value otherwise and rte_errno is set.
697  */
698 int
699 mlx5_flow_validate_action_mark(const struct rte_flow_action *action,
700                                uint64_t action_flags,
701                                const struct rte_flow_attr *attr,
702                                struct rte_flow_error *error)
703 {
704         const struct rte_flow_action_mark *mark = action->conf;
705
706         if (!mark)
707                 return rte_flow_error_set(error, EINVAL,
708                                           RTE_FLOW_ERROR_TYPE_ACTION,
709                                           action,
710                                           "configuration cannot be null");
711         if (mark->id >= MLX5_FLOW_MARK_MAX)
712                 return rte_flow_error_set(error, EINVAL,
713                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
714                                           &mark->id,
715                                           "mark id must in 0 <= id < "
716                                           RTE_STR(MLX5_FLOW_MARK_MAX));
717         if (action_flags & MLX5_FLOW_ACTION_DROP)
718                 return rte_flow_error_set(error, EINVAL,
719                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
720                                           "can't drop and mark in same flow");
721         if (action_flags & MLX5_FLOW_ACTION_FLAG)
722                 return rte_flow_error_set(error, EINVAL,
723                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
724                                           "can't flag and mark in same flow");
725         if (action_flags & MLX5_FLOW_ACTION_MARK)
726                 return rte_flow_error_set(error, EINVAL,
727                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
728                                           "can't have 2 mark actions in same"
729                                           " flow");
730         if (attr->egress)
731                 return rte_flow_error_set(error, ENOTSUP,
732                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
733                                           "mark action not supported for "
734                                           "egress");
735         return 0;
736 }
737
738 /*
739  * Validate the drop action.
740  *
741  * @param[in] action_flags
742  *   Bit-fields that holds the actions detected until now.
743  * @param[in] attr
744  *   Attributes of flow that includes this action.
745  * @param[out] error
746  *   Pointer to error structure.
747  *
748  * @return
749  *   0 on success, a negative errno value otherwise and rte_ernno is set.
750  */
751 int
752 mlx5_flow_validate_action_drop(uint64_t action_flags,
753                                const struct rte_flow_attr *attr,
754                                struct rte_flow_error *error)
755 {
756         if (action_flags & MLX5_FLOW_ACTION_FLAG)
757                 return rte_flow_error_set(error, EINVAL,
758                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
759                                           "can't drop and flag in same flow");
760         if (action_flags & MLX5_FLOW_ACTION_MARK)
761                 return rte_flow_error_set(error, EINVAL,
762                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
763                                           "can't drop and mark in same flow");
764         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
765                 return rte_flow_error_set(error, EINVAL,
766                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
767                                           "can't have 2 fate actions in"
768                                           " same flow");
769         if (attr->egress)
770                 return rte_flow_error_set(error, ENOTSUP,
771                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
772                                           "drop action not supported for "
773                                           "egress");
774         return 0;
775 }
776
777 /*
778  * Validate the queue action.
779  *
780  * @param[in] action
781  *   Pointer to the queue action.
782  * @param[in] action_flags
783  *   Bit-fields that holds the actions detected until now.
784  * @param[in] dev
785  *   Pointer to the Ethernet device structure.
786  * @param[in] attr
787  *   Attributes of flow that includes this action.
788  * @param[out] error
789  *   Pointer to error structure.
790  *
791  * @return
792  *   0 on success, a negative errno value otherwise and rte_ernno is set.
793  */
794 int
795 mlx5_flow_validate_action_queue(const struct rte_flow_action *action,
796                                 uint64_t action_flags,
797                                 struct rte_eth_dev *dev,
798                                 const struct rte_flow_attr *attr,
799                                 struct rte_flow_error *error)
800 {
801         struct priv *priv = dev->data->dev_private;
802         const struct rte_flow_action_queue *queue = action->conf;
803
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 (queue->index >= priv->rxqs_n)
810                 return rte_flow_error_set(error, EINVAL,
811                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
812                                           &queue->index,
813                                           "queue index out of range");
814         if (!(*priv->rxqs)[queue->index])
815                 return rte_flow_error_set(error, EINVAL,
816                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
817                                           &queue->index,
818                                           "queue is not configured");
819         if (attr->egress)
820                 return rte_flow_error_set(error, ENOTSUP,
821                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
822                                           "queue action not supported for "
823                                           "egress");
824         return 0;
825 }
826
827 /*
828  * Validate the rss action.
829  *
830  * @param[in] action
831  *   Pointer to the queue action.
832  * @param[in] action_flags
833  *   Bit-fields that holds the actions detected until now.
834  * @param[in] dev
835  *   Pointer to the Ethernet device structure.
836  * @param[in] attr
837  *   Attributes of flow that includes this action.
838  * @param[out] error
839  *   Pointer to error structure.
840  *
841  * @return
842  *   0 on success, a negative errno value otherwise and rte_ernno is set.
843  */
844 int
845 mlx5_flow_validate_action_rss(const struct rte_flow_action *action,
846                               uint64_t action_flags,
847                               struct rte_eth_dev *dev,
848                               const struct rte_flow_attr *attr,
849                               struct rte_flow_error *error)
850 {
851         struct priv *priv = dev->data->dev_private;
852         const struct rte_flow_action_rss *rss = action->conf;
853         unsigned int i;
854
855         if (action_flags & MLX5_FLOW_FATE_ACTIONS)
856                 return rte_flow_error_set(error, EINVAL,
857                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
858                                           "can't have 2 fate actions"
859                                           " in same flow");
860         if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT &&
861             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ)
862                 return rte_flow_error_set(error, ENOTSUP,
863                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
864                                           &rss->func,
865                                           "RSS hash function not supported");
866 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
867         if (rss->level > 2)
868 #else
869         if (rss->level > 1)
870 #endif
871                 return rte_flow_error_set(error, ENOTSUP,
872                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
873                                           &rss->level,
874                                           "tunnel RSS is not supported");
875         if (rss->key_len < MLX5_RSS_HASH_KEY_LEN)
876                 return rte_flow_error_set(error, ENOTSUP,
877                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
878                                           &rss->key_len,
879                                           "RSS hash key too small");
880         if (rss->key_len > MLX5_RSS_HASH_KEY_LEN)
881                 return rte_flow_error_set(error, ENOTSUP,
882                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
883                                           &rss->key_len,
884                                           "RSS hash key too large");
885         if (rss->queue_num > priv->config.ind_table_max_size)
886                 return rte_flow_error_set(error, ENOTSUP,
887                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
888                                           &rss->queue_num,
889                                           "number of queues too large");
890         if (rss->types & MLX5_RSS_HF_MASK)
891                 return rte_flow_error_set(error, ENOTSUP,
892                                           RTE_FLOW_ERROR_TYPE_ACTION_CONF,
893                                           &rss->types,
894                                           "some RSS protocols are not"
895                                           " supported");
896         for (i = 0; i != rss->queue_num; ++i) {
897                 if (!(*priv->rxqs)[rss->queue[i]])
898                         return rte_flow_error_set
899                                 (error, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION_CONF,
900                                  &rss->queue[i], "queue is not configured");
901         }
902         if (attr->egress)
903                 return rte_flow_error_set(error, ENOTSUP,
904                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
905                                           "rss action not supported for "
906                                           "egress");
907         return 0;
908 }
909
910 /*
911  * Validate the count action.
912  *
913  * @param[in] dev
914  *   Pointer to the Ethernet device structure.
915  * @param[in] attr
916  *   Attributes of flow that includes this action.
917  * @param[out] error
918  *   Pointer to error structure.
919  *
920  * @return
921  *   0 on success, a negative errno value otherwise and rte_ernno is set.
922  */
923 int
924 mlx5_flow_validate_action_count(struct rte_eth_dev *dev __rte_unused,
925                                 const struct rte_flow_attr *attr,
926                                 struct rte_flow_error *error)
927 {
928         if (attr->egress)
929                 return rte_flow_error_set(error, ENOTSUP,
930                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
931                                           "count action not supported for "
932                                           "egress");
933         return 0;
934 }
935
936 /**
937  * Verify the @p attributes will be correctly understood by the NIC and store
938  * them in the @p flow if everything is correct.
939  *
940  * @param[in] dev
941  *   Pointer to the Ethernet device structure.
942  * @param[in] attributes
943  *   Pointer to flow attributes
944  * @param[out] error
945  *   Pointer to error structure.
946  *
947  * @return
948  *   0 on success, a negative errno value otherwise and rte_errno is set.
949  */
950 int
951 mlx5_flow_validate_attributes(struct rte_eth_dev *dev,
952                               const struct rte_flow_attr *attributes,
953                               struct rte_flow_error *error)
954 {
955         struct priv *priv = dev->data->dev_private;
956         uint32_t priority_max = priv->config.flow_prio - 1;
957
958         if (attributes->group)
959                 return rte_flow_error_set(error, ENOTSUP,
960                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
961                                           NULL, "groups is not supported");
962         if (attributes->priority != MLX5_FLOW_PRIO_RSVD &&
963             attributes->priority >= priority_max)
964                 return rte_flow_error_set(error, ENOTSUP,
965                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
966                                           NULL, "priority out of range");
967         if (attributes->egress)
968                 return rte_flow_error_set(error, ENOTSUP,
969                                           RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
970                                           "egress is not supported");
971         if (attributes->transfer)
972                 return rte_flow_error_set(error, ENOTSUP,
973                                           RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
974                                           NULL, "transfer is not supported");
975         if (!attributes->ingress)
976                 return rte_flow_error_set(error, EINVAL,
977                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
978                                           NULL,
979                                           "ingress attribute is mandatory");
980         return 0;
981 }
982
983 /**
984  * Validate Ethernet item.
985  *
986  * @param[in] item
987  *   Item specification.
988  * @param[in] item_flags
989  *   Bit-fields that holds the items detected until now.
990  * @param[out] error
991  *   Pointer to error structure.
992  *
993  * @return
994  *   0 on success, a negative errno value otherwise and rte_errno is set.
995  */
996 int
997 mlx5_flow_validate_item_eth(const struct rte_flow_item *item,
998                             uint64_t item_flags,
999                             struct rte_flow_error *error)
1000 {
1001         const struct rte_flow_item_eth *mask = item->mask;
1002         const struct rte_flow_item_eth nic_mask = {
1003                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1004                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1005                 .type = RTE_BE16(0xffff),
1006         };
1007         int ret;
1008         int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1009
1010         if (item_flags & MLX5_FLOW_LAYER_OUTER_L2)
1011                 return rte_flow_error_set(error, ENOTSUP,
1012                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1013                                           "3 levels of l2 are not supported");
1014         if ((item_flags & MLX5_FLOW_LAYER_INNER_L2) && !tunnel)
1015                 return rte_flow_error_set(error, ENOTSUP,
1016                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1017                                           "2 L2 without tunnel are not supported");
1018         if (!mask)
1019                 mask = &rte_flow_item_eth_mask;
1020         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1021                                         (const uint8_t *)&nic_mask,
1022                                         sizeof(struct rte_flow_item_eth),
1023                                         error);
1024         return ret;
1025 }
1026
1027 /**
1028  * Validate VLAN item.
1029  *
1030  * @param[in] item
1031  *   Item specification.
1032  * @param[in] item_flags
1033  *   Bit-fields that holds the items detected until now.
1034  * @param[out] error
1035  *   Pointer to error structure.
1036  *
1037  * @return
1038  *   0 on success, a negative errno value otherwise and rte_errno is set.
1039  */
1040 int
1041 mlx5_flow_validate_item_vlan(const struct rte_flow_item *item,
1042                              int64_t item_flags,
1043                              struct rte_flow_error *error)
1044 {
1045         const struct rte_flow_item_vlan *spec = item->spec;
1046         const struct rte_flow_item_vlan *mask = item->mask;
1047         const struct rte_flow_item_vlan nic_mask = {
1048                 .tci = RTE_BE16(0x0fff),
1049                 .inner_type = RTE_BE16(0xffff),
1050         };
1051         uint16_t vlan_tag = 0;
1052         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1053         int ret;
1054         const uint32_t l34m = tunnel ? (MLX5_FLOW_LAYER_INNER_L3 |
1055                                         MLX5_FLOW_LAYER_INNER_L4) :
1056                                        (MLX5_FLOW_LAYER_OUTER_L3 |
1057                                         MLX5_FLOW_LAYER_OUTER_L4);
1058         const uint32_t vlanm = tunnel ? MLX5_FLOW_LAYER_INNER_VLAN :
1059                                         MLX5_FLOW_LAYER_OUTER_VLAN;
1060
1061         if (item_flags & vlanm)
1062                 return rte_flow_error_set(error, EINVAL,
1063                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1064                                           "VLAN layer already configured");
1065         else if ((item_flags & l34m) != 0)
1066                 return rte_flow_error_set(error, EINVAL,
1067                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1068                                           "L2 layer cannot follow L3/L4 layer");
1069         if (!mask)
1070                 mask = &rte_flow_item_vlan_mask;
1071         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1072                                         (const uint8_t *)&nic_mask,
1073                                         sizeof(struct rte_flow_item_vlan),
1074                                         error);
1075         if (ret)
1076                 return ret;
1077         if (spec) {
1078                 vlan_tag = spec->tci;
1079                 vlan_tag &= mask->tci;
1080         }
1081         /*
1082          * From verbs perspective an empty VLAN is equivalent
1083          * to a packet without VLAN layer.
1084          */
1085         if (!vlan_tag)
1086                 return rte_flow_error_set(error, EINVAL,
1087                                           RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1088                                           item->spec,
1089                                           "VLAN cannot be empty");
1090         return 0;
1091 }
1092
1093 /**
1094  * Validate IPV4 item.
1095  *
1096  * @param[in] item
1097  *   Item specification.
1098  * @param[in] item_flags
1099  *   Bit-fields that holds the items detected until now.
1100  * @param[out] error
1101  *   Pointer to error structure.
1102  *
1103  * @return
1104  *   0 on success, a negative errno value otherwise and rte_errno is set.
1105  */
1106 int
1107 mlx5_flow_validate_item_ipv4(const struct rte_flow_item *item,
1108                              int64_t item_flags,
1109                              struct rte_flow_error *error)
1110 {
1111         const struct rte_flow_item_ipv4 *mask = item->mask;
1112         const struct rte_flow_item_ipv4 nic_mask = {
1113                 .hdr = {
1114                         .src_addr = RTE_BE32(0xffffffff),
1115                         .dst_addr = RTE_BE32(0xffffffff),
1116                         .type_of_service = 0xff,
1117                         .next_proto_id = 0xff,
1118                 },
1119         };
1120         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1121         int ret;
1122
1123         if (item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1124                                    MLX5_FLOW_LAYER_OUTER_L3))
1125                 return rte_flow_error_set(error, ENOTSUP,
1126                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1127                                           "multiple L3 layers not supported");
1128         else if (item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1129                                         MLX5_FLOW_LAYER_OUTER_L4))
1130                 return rte_flow_error_set(error, EINVAL,
1131                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1132                                           "L3 cannot follow an L4 layer.");
1133         if (!mask)
1134                 mask = &rte_flow_item_ipv4_mask;
1135         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1136                                         (const uint8_t *)&nic_mask,
1137                                         sizeof(struct rte_flow_item_ipv4),
1138                                         error);
1139         if (ret < 0)
1140                 return ret;
1141         return 0;
1142 }
1143
1144 /**
1145  * Validate IPV6 item.
1146  *
1147  * @param[in] item
1148  *   Item specification.
1149  * @param[in] item_flags
1150  *   Bit-fields that holds the items detected until now.
1151  * @param[out] error
1152  *   Pointer to error structure.
1153  *
1154  * @return
1155  *   0 on success, a negative errno value otherwise and rte_errno is set.
1156  */
1157 int
1158 mlx5_flow_validate_item_ipv6(const struct rte_flow_item *item,
1159                              uint64_t item_flags,
1160                              struct rte_flow_error *error)
1161 {
1162         const struct rte_flow_item_ipv6 *mask = item->mask;
1163         const struct rte_flow_item_ipv6 nic_mask = {
1164                 .hdr = {
1165                         .src_addr =
1166                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1167                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1168                         .dst_addr =
1169                                 "\xff\xff\xff\xff\xff\xff\xff\xff"
1170                                 "\xff\xff\xff\xff\xff\xff\xff\xff",
1171                         .vtc_flow = RTE_BE32(0xffffffff),
1172                         .proto = 0xff,
1173                         .hop_limits = 0xff,
1174                 },
1175         };
1176         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1177         int ret;
1178
1179         if (item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1180                                    MLX5_FLOW_LAYER_OUTER_L3))
1181                 return rte_flow_error_set(error, ENOTSUP,
1182                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1183                                           "multiple L3 layers not supported");
1184         else if (item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1185                                         MLX5_FLOW_LAYER_OUTER_L4))
1186                 return rte_flow_error_set(error, EINVAL,
1187                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1188                                           "L3 cannot follow an L4 layer.");
1189         /*
1190          * IPv6 is not recognised by the NIC inside a GRE tunnel.
1191          * Such support has to be disabled as the rule will be
1192          * accepted.  Issue reproduced with Mellanox OFED 4.3-3.0.2.1 and
1193          * Mellanox OFED 4.4-1.0.0.0.
1194          */
1195         if (tunnel && item_flags & MLX5_FLOW_LAYER_GRE)
1196                 return rte_flow_error_set(error, ENOTSUP,
1197                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1198                                           "IPv6 inside a GRE tunnel is"
1199                                           " not recognised.");
1200         if (!mask)
1201                 mask = &rte_flow_item_ipv6_mask;
1202         ret = mlx5_flow_item_acceptable(item, (const uint8_t *)mask,
1203                                         (const uint8_t *)&nic_mask,
1204                                         sizeof(struct rte_flow_item_ipv6),
1205                                         error);
1206         if (ret < 0)
1207                 return ret;
1208         return 0;
1209 }
1210
1211 /**
1212  * Validate UDP item.
1213  *
1214  * @param[in] item
1215  *   Item specification.
1216  * @param[in] item_flags
1217  *   Bit-fields that holds the items detected until now.
1218  * @param[in] target_protocol
1219  *   The next protocol in the previous item.
1220  * @param[in] flow_mask
1221  *   mlx5 flow-specific (TCF, DV, verbs, etc.) supported header fields mask.
1222  * @param[out] error
1223  *   Pointer to error structure.
1224  *
1225  * @return
1226  *   0 on success, a negative errno value otherwise and rte_errno is set.
1227  */
1228 int
1229 mlx5_flow_validate_item_udp(const struct rte_flow_item *item,
1230                             uint64_t item_flags,
1231                             uint8_t target_protocol,
1232                             struct rte_flow_error *error)
1233 {
1234         const struct rte_flow_item_udp *mask = item->mask;
1235         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1236         int ret;
1237
1238         if (target_protocol != 0xff && target_protocol != IPPROTO_UDP)
1239                 return rte_flow_error_set(error, EINVAL,
1240                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1241                                           "protocol filtering not compatible"
1242                                           " with UDP layer");
1243         if (!(item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1244                                      MLX5_FLOW_LAYER_OUTER_L3)))
1245                 return rte_flow_error_set(error, EINVAL,
1246                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1247                                           "L3 is mandatory to filter on L4");
1248         if (item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1249                                    MLX5_FLOW_LAYER_OUTER_L4))
1250                 return rte_flow_error_set(error, EINVAL,
1251                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1252                                           "L4 layer is already present");
1253         if (!mask)
1254                 mask = &rte_flow_item_udp_mask;
1255         ret = mlx5_flow_item_acceptable
1256                 (item, (const uint8_t *)mask,
1257                  (const uint8_t *)&rte_flow_item_udp_mask,
1258                  sizeof(struct rte_flow_item_udp), error);
1259         if (ret < 0)
1260                 return ret;
1261         return 0;
1262 }
1263
1264 /**
1265  * Validate TCP item.
1266  *
1267  * @param[in] item
1268  *   Item specification.
1269  * @param[in] item_flags
1270  *   Bit-fields that holds the items detected until now.
1271  * @param[in] target_protocol
1272  *   The next protocol in the previous item.
1273  * @param[out] error
1274  *   Pointer to error structure.
1275  *
1276  * @return
1277  *   0 on success, a negative errno value otherwise and rte_errno is set.
1278  */
1279 int
1280 mlx5_flow_validate_item_tcp(const struct rte_flow_item *item,
1281                             uint64_t item_flags,
1282                             uint8_t target_protocol,
1283                             const struct rte_flow_item_tcp *flow_mask,
1284                             struct rte_flow_error *error)
1285 {
1286         const struct rte_flow_item_tcp *mask = item->mask;
1287         const int tunnel = !!(item_flags & MLX5_FLOW_LAYER_TUNNEL);
1288         int ret;
1289
1290         assert(flow_mask);
1291         if (target_protocol != 0xff && target_protocol != IPPROTO_TCP)
1292                 return rte_flow_error_set(error, EINVAL,
1293                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1294                                           "protocol filtering not compatible"
1295                                           " with TCP layer");
1296         if (!(item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L3 :
1297                                      MLX5_FLOW_LAYER_OUTER_L3)))
1298                 return rte_flow_error_set(error, EINVAL,
1299                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1300                                           "L3 is mandatory to filter on L4");
1301         if (item_flags & (tunnel ? MLX5_FLOW_LAYER_INNER_L4 :
1302                                    MLX5_FLOW_LAYER_OUTER_L4))
1303                 return rte_flow_error_set(error, EINVAL,
1304                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1305                                           "L4 layer is already present");
1306         if (!mask)
1307                 mask = &rte_flow_item_tcp_mask;
1308         ret = mlx5_flow_item_acceptable
1309                 (item, (const uint8_t *)mask,
1310                  (const uint8_t *)flow_mask,
1311                  sizeof(struct rte_flow_item_tcp), error);
1312         if (ret < 0)
1313                 return ret;
1314         return 0;
1315 }
1316
1317 /**
1318  * Validate VXLAN item.
1319  *
1320  * @param[in] item
1321  *   Item specification.
1322  * @param[in] item_flags
1323  *   Bit-fields that holds the items detected until now.
1324  * @param[in] target_protocol
1325  *   The next protocol in the previous item.
1326  * @param[out] error
1327  *   Pointer to error structure.
1328  *
1329  * @return
1330  *   0 on success, a negative errno value otherwise and rte_errno is set.
1331  */
1332 int
1333 mlx5_flow_validate_item_vxlan(const struct rte_flow_item *item,
1334                               uint64_t item_flags,
1335                               struct rte_flow_error *error)
1336 {
1337         const struct rte_flow_item_vxlan *spec = item->spec;
1338         const struct rte_flow_item_vxlan *mask = item->mask;
1339         int ret;
1340         union vni {
1341                 uint32_t vlan_id;
1342                 uint8_t vni[4];
1343         } id = { .vlan_id = 0, };
1344         uint32_t vlan_id = 0;
1345
1346
1347         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1348                 return rte_flow_error_set(error, ENOTSUP,
1349                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1350                                           "a tunnel is already present");
1351         /*
1352          * Verify only UDPv4 is present as defined in
1353          * https://tools.ietf.org/html/rfc7348
1354          */
1355         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1356                 return rte_flow_error_set(error, EINVAL,
1357                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1358                                           "no outer UDP layer found");
1359         if (!mask)
1360                 mask = &rte_flow_item_vxlan_mask;
1361         ret = mlx5_flow_item_acceptable
1362                 (item, (const uint8_t *)mask,
1363                  (const uint8_t *)&rte_flow_item_vxlan_mask,
1364                  sizeof(struct rte_flow_item_vxlan),
1365                  error);
1366         if (ret < 0)
1367                 return ret;
1368         if (spec) {
1369                 memcpy(&id.vni[1], spec->vni, 3);
1370                 vlan_id = id.vlan_id;
1371                 memcpy(&id.vni[1], mask->vni, 3);
1372                 vlan_id &= id.vlan_id;
1373         }
1374         /*
1375          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if
1376          * only this layer is defined in the Verbs specification it is
1377          * interpreted as wildcard and all packets will match this
1378          * rule, if it follows a full stack layer (ex: eth / ipv4 /
1379          * udp), all packets matching the layers before will also
1380          * match this rule.  To avoid such situation, VNI 0 is
1381          * currently refused.
1382          */
1383         if (!vlan_id)
1384                 return rte_flow_error_set(error, ENOTSUP,
1385                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1386                                           "VXLAN vni cannot be 0");
1387         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1388                 return rte_flow_error_set(error, ENOTSUP,
1389                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1390                                           "VXLAN tunnel must be fully defined");
1391         return 0;
1392 }
1393
1394 /**
1395  * Validate VXLAN_GPE item.
1396  *
1397  * @param[in] item
1398  *   Item specification.
1399  * @param[in] item_flags
1400  *   Bit-fields that holds the items detected until now.
1401  * @param[in] priv
1402  *   Pointer to the private data structure.
1403  * @param[in] target_protocol
1404  *   The next protocol in the previous item.
1405  * @param[out] error
1406  *   Pointer to error structure.
1407  *
1408  * @return
1409  *   0 on success, a negative errno value otherwise and rte_errno is set.
1410  */
1411 int
1412 mlx5_flow_validate_item_vxlan_gpe(const struct rte_flow_item *item,
1413                                   uint64_t item_flags,
1414                                   struct rte_eth_dev *dev,
1415                                   struct rte_flow_error *error)
1416 {
1417         struct priv *priv = dev->data->dev_private;
1418         const struct rte_flow_item_vxlan_gpe *spec = item->spec;
1419         const struct rte_flow_item_vxlan_gpe *mask = item->mask;
1420         int ret;
1421         union vni {
1422                 uint32_t vlan_id;
1423                 uint8_t vni[4];
1424         } id = { .vlan_id = 0, };
1425         uint32_t vlan_id = 0;
1426
1427         if (!priv->config.l3_vxlan_en)
1428                 return rte_flow_error_set(error, ENOTSUP,
1429                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1430                                           "L3 VXLAN is not enabled by device"
1431                                           " parameter and/or not configured in"
1432                                           " firmware");
1433         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1434                 return rte_flow_error_set(error, ENOTSUP,
1435                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1436                                           "a tunnel is already present");
1437         /*
1438          * Verify only UDPv4 is present as defined in
1439          * https://tools.ietf.org/html/rfc7348
1440          */
1441         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1442                 return rte_flow_error_set(error, EINVAL,
1443                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1444                                           "no outer UDP layer found");
1445         if (!mask)
1446                 mask = &rte_flow_item_vxlan_gpe_mask;
1447         ret = mlx5_flow_item_acceptable
1448                 (item, (const uint8_t *)mask,
1449                  (const uint8_t *)&rte_flow_item_vxlan_gpe_mask,
1450                  sizeof(struct rte_flow_item_vxlan_gpe),
1451                  error);
1452         if (ret < 0)
1453                 return ret;
1454         if (spec) {
1455                 if (spec->protocol)
1456                         return rte_flow_error_set(error, ENOTSUP,
1457                                                   RTE_FLOW_ERROR_TYPE_ITEM,
1458                                                   item,
1459                                                   "VxLAN-GPE protocol"
1460                                                   " not supported");
1461                 memcpy(&id.vni[1], spec->vni, 3);
1462                 vlan_id = id.vlan_id;
1463                 memcpy(&id.vni[1], mask->vni, 3);
1464                 vlan_id &= id.vlan_id;
1465         }
1466         /*
1467          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1468          * layer is defined in the Verbs specification it is interpreted as
1469          * wildcard and all packets will match this rule, if it follows a full
1470          * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1471          * before will also match this rule.  To avoid such situation, VNI 0
1472          * is currently refused.
1473          */
1474         if (!vlan_id)
1475                 return rte_flow_error_set(error, ENOTSUP,
1476                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1477                                           "VXLAN-GPE vni cannot be 0");
1478         if (!(item_flags & MLX5_FLOW_LAYER_OUTER))
1479                 return rte_flow_error_set(error, ENOTSUP,
1480                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1481                                           "VXLAN-GPE tunnel must be fully"
1482                                           " defined");
1483         return 0;
1484 }
1485
1486 /**
1487  * Validate GRE item.
1488  *
1489  * @param[in] item
1490  *   Item specification.
1491  * @param[in] item_flags
1492  *   Bit flags to mark detected items.
1493  * @param[in] target_protocol
1494  *   The next protocol in the previous item.
1495  * @param[out] error
1496  *   Pointer to error structure.
1497  *
1498  * @return
1499  *   0 on success, a negative errno value otherwise and rte_errno is set.
1500  */
1501 int
1502 mlx5_flow_validate_item_gre(const struct rte_flow_item *item,
1503                             uint64_t item_flags,
1504                             uint8_t target_protocol,
1505                             struct rte_flow_error *error)
1506 {
1507         const struct rte_flow_item_gre *spec __rte_unused = item->spec;
1508         const struct rte_flow_item_gre *mask = item->mask;
1509         int ret;
1510
1511         if (target_protocol != 0xff && target_protocol != IPPROTO_GRE)
1512                 return rte_flow_error_set(error, EINVAL,
1513                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1514                                           "protocol filtering not compatible"
1515                                           " with this GRE layer");
1516         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1517                 return rte_flow_error_set(error, ENOTSUP,
1518                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1519                                           "a tunnel is already present");
1520         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
1521                 return rte_flow_error_set(error, ENOTSUP,
1522                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1523                                           "L3 Layer is missing");
1524         if (!mask)
1525                 mask = &rte_flow_item_gre_mask;
1526         ret = mlx5_flow_item_acceptable
1527                 (item, (const uint8_t *)mask,
1528                  (const uint8_t *)&rte_flow_item_gre_mask,
1529                  sizeof(struct rte_flow_item_gre), error);
1530         if (ret < 0)
1531                 return ret;
1532 #ifndef HAVE_IBV_DEVICE_MPLS_SUPPORT
1533         if (spec && (spec->protocol & mask->protocol))
1534                 return rte_flow_error_set(error, ENOTSUP,
1535                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1536                                           "without MPLS support the"
1537                                           " specification cannot be used for"
1538                                           " filtering");
1539 #endif
1540         return 0;
1541 }
1542
1543 /**
1544  * Validate MPLS item.
1545  *
1546  * @param[in] item
1547  *   Item specification.
1548  * @param[in] item_flags
1549  *   Bit-fields that holds the items detected until now.
1550  * @param[in] target_protocol
1551  *   The next protocol in the previous item.
1552  * @param[out] error
1553  *   Pointer to error structure.
1554  *
1555  * @return
1556  *   0 on success, a negative errno value otherwise and rte_errno is set.
1557  */
1558 int
1559 mlx5_flow_validate_item_mpls(const struct rte_flow_item *item __rte_unused,
1560                              uint64_t item_flags __rte_unused,
1561                              uint8_t target_protocol __rte_unused,
1562                              struct rte_flow_error *error)
1563 {
1564 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
1565         const struct rte_flow_item_mpls *mask = item->mask;
1566         int ret;
1567
1568         if (target_protocol != 0xff && target_protocol != IPPROTO_MPLS)
1569                 return rte_flow_error_set(error, EINVAL,
1570                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1571                                           "protocol filtering not compatible"
1572                                           " with MPLS layer");
1573         if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1574                 return rte_flow_error_set(error, ENOTSUP,
1575                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1576                                           "a tunnel is already"
1577                                           " present");
1578         if (!mask)
1579                 mask = &rte_flow_item_mpls_mask;
1580         ret = mlx5_flow_item_acceptable
1581                 (item, (const uint8_t *)mask,
1582                  (const uint8_t *)&rte_flow_item_mpls_mask,
1583                  sizeof(struct rte_flow_item_mpls), error);
1584         if (ret < 0)
1585                 return ret;
1586         return 0;
1587 #endif
1588         return rte_flow_error_set(error, ENOTSUP,
1589                                   RTE_FLOW_ERROR_TYPE_ITEM, item,
1590                                   "MPLS is not supported by Verbs, please"
1591                                   " update.");
1592 }
1593
1594 static int
1595 flow_null_validate(struct rte_eth_dev *dev __rte_unused,
1596                    const struct rte_flow_attr *attr __rte_unused,
1597                    const struct rte_flow_item items[] __rte_unused,
1598                    const struct rte_flow_action actions[] __rte_unused,
1599                    struct rte_flow_error *error __rte_unused)
1600 {
1601         rte_errno = ENOTSUP;
1602         return -rte_errno;
1603 }
1604
1605 static struct mlx5_flow *
1606 flow_null_prepare(const struct rte_flow_attr *attr __rte_unused,
1607                   const struct rte_flow_item items[] __rte_unused,
1608                   const struct rte_flow_action actions[] __rte_unused,
1609                   uint64_t *item_flags __rte_unused,
1610                   uint64_t *action_flags __rte_unused,
1611                   struct rte_flow_error *error __rte_unused)
1612 {
1613         rte_errno = ENOTSUP;
1614         return NULL;
1615 }
1616
1617 static int
1618 flow_null_translate(struct rte_eth_dev *dev __rte_unused,
1619                     struct mlx5_flow *dev_flow __rte_unused,
1620                     const struct rte_flow_attr *attr __rte_unused,
1621                     const struct rte_flow_item items[] __rte_unused,
1622                     const struct rte_flow_action actions[] __rte_unused,
1623                     struct rte_flow_error *error __rte_unused)
1624 {
1625         rte_errno = ENOTSUP;
1626         return -rte_errno;
1627 }
1628
1629 static int
1630 flow_null_apply(struct rte_eth_dev *dev __rte_unused,
1631                 struct rte_flow *flow __rte_unused,
1632                 struct rte_flow_error *error __rte_unused)
1633 {
1634         rte_errno = ENOTSUP;
1635         return -rte_errno;
1636 }
1637
1638 static void
1639 flow_null_remove(struct rte_eth_dev *dev __rte_unused,
1640                  struct rte_flow *flow __rte_unused)
1641 {
1642 }
1643
1644 static void
1645 flow_null_destroy(struct rte_eth_dev *dev __rte_unused,
1646                   struct rte_flow *flow __rte_unused)
1647 {
1648 }
1649
1650 static int
1651 flow_null_query(struct rte_eth_dev *dev __rte_unused,
1652                 struct rte_flow *flow __rte_unused,
1653                 const struct rte_flow_action *actions __rte_unused,
1654                 void *data __rte_unused,
1655                 struct rte_flow_error *error __rte_unused)
1656 {
1657         rte_errno = ENOTSUP;
1658         return -rte_errno;
1659 }
1660
1661 /* Void driver to protect from null pointer reference. */
1662 const struct mlx5_flow_driver_ops mlx5_flow_null_drv_ops = {
1663         .validate = flow_null_validate,
1664         .prepare = flow_null_prepare,
1665         .translate = flow_null_translate,
1666         .apply = flow_null_apply,
1667         .remove = flow_null_remove,
1668         .destroy = flow_null_destroy,
1669         .query = flow_null_query,
1670 };
1671
1672 /**
1673  * Select flow driver type according to flow attributes and device
1674  * configuration.
1675  *
1676  * @param[in] dev
1677  *   Pointer to the dev structure.
1678  * @param[in] attr
1679  *   Pointer to the flow attributes.
1680  *
1681  * @return
1682  *   flow driver type if supported, MLX5_FLOW_TYPE_MAX otherwise.
1683  */
1684 static enum mlx5_flow_drv_type
1685 flow_get_drv_type(struct rte_eth_dev *dev __rte_unused,
1686                   const struct rte_flow_attr *attr)
1687 {
1688         struct priv *priv __rte_unused = dev->data->dev_private;
1689         enum mlx5_flow_drv_type type = MLX5_FLOW_TYPE_MAX;
1690
1691         if (attr->transfer) {
1692                 type = MLX5_FLOW_TYPE_TCF;
1693         } else {
1694 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1695                 type = priv->config.dv_flow_en ?  MLX5_FLOW_TYPE_DV :
1696                                                   MLX5_FLOW_TYPE_VERBS;
1697 #else
1698                 type = MLX5_FLOW_TYPE_VERBS;
1699 #endif
1700         }
1701         return type;
1702 }
1703
1704 #define flow_get_drv_ops(type) flow_drv_ops[type]
1705
1706 /**
1707  * Flow driver validation API. This abstracts calling driver specific functions.
1708  * The type of flow driver is determined according to flow attributes.
1709  *
1710  * @param[in] dev
1711  *   Pointer to the dev structure.
1712  * @param[in] attr
1713  *   Pointer to the flow attributes.
1714  * @param[in] items
1715  *   Pointer to the list of items.
1716  * @param[in] actions
1717  *   Pointer to the list of actions.
1718  * @param[out] error
1719  *   Pointer to the error structure.
1720  *
1721  * @return
1722  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1723  */
1724 static inline int
1725 flow_drv_validate(struct rte_eth_dev *dev,
1726                   const struct rte_flow_attr *attr,
1727                   const struct rte_flow_item items[],
1728                   const struct rte_flow_action actions[],
1729                   struct rte_flow_error *error)
1730 {
1731         const struct mlx5_flow_driver_ops *fops;
1732         enum mlx5_flow_drv_type type = flow_get_drv_type(dev, attr);
1733
1734         fops = flow_get_drv_ops(type);
1735         return fops->validate(dev, attr, items, actions, error);
1736 }
1737
1738 /**
1739  * Flow driver preparation API. This abstracts calling driver specific
1740  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
1741  * calculates the size of memory required for device flow, allocates the memory,
1742  * initializes the device flow and returns the pointer.
1743  *
1744  * @param[in] attr
1745  *   Pointer to the flow attributes.
1746  * @param[in] items
1747  *   Pointer to the list of items.
1748  * @param[in] actions
1749  *   Pointer to the list of actions.
1750  * @param[out] item_flags
1751  *   Pointer to bit mask of all items detected.
1752  * @param[out] action_flags
1753  *   Pointer to bit mask of all actions detected.
1754  * @param[out] error
1755  *   Pointer to the error structure.
1756  *
1757  * @return
1758  *   Pointer to device flow on success, otherwise NULL and rte_ernno is set.
1759  */
1760 static inline struct mlx5_flow *
1761 flow_drv_prepare(struct rte_flow *flow,
1762                  const struct rte_flow_attr *attr,
1763                  const struct rte_flow_item items[],
1764                  const struct rte_flow_action actions[],
1765                  uint64_t *item_flags,
1766                  uint64_t *action_flags,
1767                  struct rte_flow_error *error)
1768 {
1769         const struct mlx5_flow_driver_ops *fops;
1770         enum mlx5_flow_drv_type type = flow->drv_type;
1771
1772         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1773         fops = flow_get_drv_ops(type);
1774         return fops->prepare(attr, items, actions, item_flags, action_flags,
1775                              error);
1776 }
1777
1778 /**
1779  * Flow driver translation API. This abstracts calling driver specific
1780  * functions. Parent flow (rte_flow) should have driver type (drv_type). It
1781  * translates a generic flow into a driver flow. flow_drv_prepare() must
1782  * precede.
1783  *
1784  *
1785  * @param[in] dev
1786  *   Pointer to the rte dev structure.
1787  * @param[in, out] dev_flow
1788  *   Pointer to the mlx5 flow.
1789  * @param[in] attr
1790  *   Pointer to the flow attributes.
1791  * @param[in] items
1792  *   Pointer to the list of items.
1793  * @param[in] actions
1794  *   Pointer to the list of actions.
1795  * @param[out] error
1796  *   Pointer to the error structure.
1797  *
1798  * @return
1799  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1800  */
1801 static inline int
1802 flow_drv_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
1803                    const struct rte_flow_attr *attr,
1804                    const struct rte_flow_item items[],
1805                    const struct rte_flow_action actions[],
1806                    struct rte_flow_error *error)
1807 {
1808         const struct mlx5_flow_driver_ops *fops;
1809         enum mlx5_flow_drv_type type = dev_flow->flow->drv_type;
1810
1811         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1812         fops = flow_get_drv_ops(type);
1813         return fops->translate(dev, dev_flow, attr, items, actions, error);
1814 }
1815
1816 /**
1817  * Flow driver apply API. This abstracts calling driver specific functions.
1818  * Parent flow (rte_flow) should have driver type (drv_type). It applies
1819  * translated driver flows on to device. flow_drv_translate() must precede.
1820  *
1821  * @param[in] dev
1822  *   Pointer to Ethernet device structure.
1823  * @param[in, out] flow
1824  *   Pointer to flow structure.
1825  * @param[out] error
1826  *   Pointer to error structure.
1827  *
1828  * @return
1829  *   0 on success, a negative errno value otherwise and rte_errno is set.
1830  */
1831 static inline int
1832 flow_drv_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
1833                struct rte_flow_error *error)
1834 {
1835         const struct mlx5_flow_driver_ops *fops;
1836         enum mlx5_flow_drv_type type = flow->drv_type;
1837
1838         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1839         fops = flow_get_drv_ops(type);
1840         return fops->apply(dev, flow, error);
1841 }
1842
1843 /**
1844  * Flow driver remove API. This abstracts calling driver specific functions.
1845  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
1846  * on device. All the resources of the flow should be freed by calling
1847  * flow_dv_destroy().
1848  *
1849  * @param[in] dev
1850  *   Pointer to Ethernet device.
1851  * @param[in, out] flow
1852  *   Pointer to flow structure.
1853  */
1854 static inline void
1855 flow_drv_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
1856 {
1857         const struct mlx5_flow_driver_ops *fops;
1858         enum mlx5_flow_drv_type type = flow->drv_type;
1859
1860         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1861         fops = flow_get_drv_ops(type);
1862         fops->remove(dev, flow);
1863 }
1864
1865 /**
1866  * Flow driver destroy API. This abstracts calling driver specific functions.
1867  * Parent flow (rte_flow) should have driver type (drv_type). It removes a flow
1868  * on device and releases resources of the flow.
1869  *
1870  * @param[in] dev
1871  *   Pointer to Ethernet device.
1872  * @param[in, out] flow
1873  *   Pointer to flow structure.
1874  */
1875 static inline void
1876 flow_drv_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
1877 {
1878         const struct mlx5_flow_driver_ops *fops;
1879         enum mlx5_flow_drv_type type = flow->drv_type;
1880
1881         assert(type > MLX5_FLOW_TYPE_MIN && type < MLX5_FLOW_TYPE_MAX);
1882         fops = flow_get_drv_ops(type);
1883         fops->destroy(dev, flow);
1884 }
1885
1886 /**
1887  * Validate a flow supported by the NIC.
1888  *
1889  * @see rte_flow_validate()
1890  * @see rte_flow_ops
1891  */
1892 int
1893 mlx5_flow_validate(struct rte_eth_dev *dev,
1894                    const struct rte_flow_attr *attr,
1895                    const struct rte_flow_item items[],
1896                    const struct rte_flow_action actions[],
1897                    struct rte_flow_error *error)
1898 {
1899         int ret;
1900
1901         ret = flow_drv_validate(dev, attr, items, actions, error);
1902         if (ret < 0)
1903                 return ret;
1904         return 0;
1905 }
1906
1907 /**
1908  * Get RSS action from the action list.
1909  *
1910  * @param[in] actions
1911  *   Pointer to the list of actions.
1912  *
1913  * @return
1914  *   Pointer to the RSS action if exist, else return NULL.
1915  */
1916 static const struct rte_flow_action_rss*
1917 mlx5_flow_get_rss_action(const struct rte_flow_action actions[])
1918 {
1919         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1920                 switch (actions->type) {
1921                 case RTE_FLOW_ACTION_TYPE_RSS:
1922                         return (const struct rte_flow_action_rss *)
1923                                actions->conf;
1924                 default:
1925                         break;
1926                 }
1927         }
1928         return NULL;
1929 }
1930
1931 static unsigned int
1932 mlx5_find_graph_root(const struct rte_flow_item pattern[], uint32_t rss_level)
1933 {
1934         const struct rte_flow_item *item;
1935         unsigned int has_vlan = 0;
1936
1937         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1938                 if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1939                         has_vlan = 1;
1940                         break;
1941                 }
1942         }
1943         if (has_vlan)
1944                 return rss_level < 2 ? MLX5_EXPANSION_ROOT_ETH_VLAN :
1945                                        MLX5_EXPANSION_ROOT_OUTER_ETH_VLAN;
1946         return rss_level < 2 ? MLX5_EXPANSION_ROOT :
1947                                MLX5_EXPANSION_ROOT_OUTER;
1948 }
1949
1950 /**
1951  * Create a flow and add it to @p list.
1952  *
1953  * @param dev
1954  *   Pointer to Ethernet device.
1955  * @param list
1956  *   Pointer to a TAILQ flow list.
1957  * @param[in] attr
1958  *   Flow rule attributes.
1959  * @param[in] items
1960  *   Pattern specification (list terminated by the END pattern item).
1961  * @param[in] actions
1962  *   Associated actions (list terminated by the END action).
1963  * @param[out] error
1964  *   Perform verbose error reporting if not NULL.
1965  *
1966  * @return
1967  *   A flow on success, NULL otherwise and rte_errno is set.
1968  */
1969 static struct rte_flow *
1970 mlx5_flow_list_create(struct rte_eth_dev *dev,
1971                       struct mlx5_flows *list,
1972                       const struct rte_flow_attr *attr,
1973                       const struct rte_flow_item items[],
1974                       const struct rte_flow_action actions[],
1975                       struct rte_flow_error *error)
1976 {
1977         struct rte_flow *flow = NULL;
1978         struct mlx5_flow *dev_flow;
1979         uint64_t action_flags = 0;
1980         uint64_t item_flags = 0;
1981         const struct rte_flow_action_rss *rss;
1982         union {
1983                 struct rte_flow_expand_rss buf;
1984                 uint8_t buffer[2048];
1985         } expand_buffer;
1986         struct rte_flow_expand_rss *buf = &expand_buffer.buf;
1987         int ret;
1988         uint32_t i;
1989         uint32_t flow_size;
1990
1991         ret = flow_drv_validate(dev, attr, items, actions, error);
1992         if (ret < 0)
1993                 return NULL;
1994         flow_size = sizeof(struct rte_flow);
1995         rss = mlx5_flow_get_rss_action(actions);
1996         if (rss)
1997                 flow_size += RTE_ALIGN_CEIL(rss->queue_num * sizeof(uint16_t),
1998                                             sizeof(void *));
1999         else
2000                 flow_size += RTE_ALIGN_CEIL(sizeof(uint16_t), sizeof(void *));
2001         flow = rte_calloc(__func__, 1, flow_size, 0);
2002         flow->drv_type = flow_get_drv_type(dev, attr);
2003         assert(flow->drv_type > MLX5_FLOW_TYPE_MIN &&
2004                flow->drv_type < MLX5_FLOW_TYPE_MAX);
2005         flow->queue = (void *)(flow + 1);
2006         LIST_INIT(&flow->dev_flows);
2007         if (rss && rss->types) {
2008                 unsigned int graph_root;
2009
2010                 graph_root = mlx5_find_graph_root(items, rss->level);
2011                 ret = rte_flow_expand_rss(buf, sizeof(expand_buffer.buffer),
2012                                           items, rss->types,
2013                                           mlx5_support_expansion,
2014                                           graph_root);
2015                 assert(ret > 0 &&
2016                        (unsigned int)ret < sizeof(expand_buffer.buffer));
2017         } else {
2018                 buf->entries = 1;
2019                 buf->entry[0].pattern = (void *)(uintptr_t)items;
2020         }
2021         for (i = 0; i < buf->entries; ++i) {
2022                 dev_flow = flow_drv_prepare(flow, attr, buf->entry[i].pattern,
2023                                             actions, &item_flags, &action_flags,
2024                                             error);
2025                 if (!dev_flow)
2026                         goto error;
2027                 dev_flow->flow = flow;
2028                 LIST_INSERT_HEAD(&flow->dev_flows, dev_flow, next);
2029                 ret = flow_drv_translate(dev, dev_flow, attr,
2030                                          buf->entry[i].pattern,
2031                                          actions, error);
2032                 if (ret < 0)
2033                         goto error;
2034         }
2035         if (dev->data->dev_started) {
2036                 ret = flow_drv_apply(dev, flow, error);
2037                 if (ret < 0)
2038                         goto error;
2039         }
2040         TAILQ_INSERT_TAIL(list, flow, next);
2041         mlx5_flow_rxq_flags_set(dev, flow);
2042         return flow;
2043 error:
2044         ret = rte_errno; /* Save rte_errno before cleanup. */
2045         assert(flow);
2046         flow_drv_destroy(dev, flow);
2047         rte_free(flow);
2048         rte_errno = ret; /* Restore rte_errno. */
2049         return NULL;
2050 }
2051
2052 /**
2053  * Create a flow.
2054  *
2055  * @see rte_flow_create()
2056  * @see rte_flow_ops
2057  */
2058 struct rte_flow *
2059 mlx5_flow_create(struct rte_eth_dev *dev,
2060                  const struct rte_flow_attr *attr,
2061                  const struct rte_flow_item items[],
2062                  const struct rte_flow_action actions[],
2063                  struct rte_flow_error *error)
2064 {
2065         return mlx5_flow_list_create
2066                 (dev, &((struct priv *)dev->data->dev_private)->flows,
2067                  attr, items, actions, error);
2068 }
2069
2070 /**
2071  * Destroy a flow in a list.
2072  *
2073  * @param dev
2074  *   Pointer to Ethernet device.
2075  * @param list
2076  *   Pointer to a TAILQ flow list.
2077  * @param[in] flow
2078  *   Flow to destroy.
2079  */
2080 static void
2081 mlx5_flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2082                        struct rte_flow *flow)
2083 {
2084         flow_drv_destroy(dev, flow);
2085         TAILQ_REMOVE(list, flow, next);
2086         /*
2087          * Update RX queue flags only if port is started, otherwise it is
2088          * already clean.
2089          */
2090         if (dev->data->dev_started)
2091                 mlx5_flow_rxq_flags_trim(dev, flow);
2092         rte_free(flow);
2093 }
2094
2095 /**
2096  * Destroy all flows.
2097  *
2098  * @param dev
2099  *   Pointer to Ethernet device.
2100  * @param list
2101  *   Pointer to a TAILQ flow list.
2102  */
2103 void
2104 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
2105 {
2106         while (!TAILQ_EMPTY(list)) {
2107                 struct rte_flow *flow;
2108
2109                 flow = TAILQ_FIRST(list);
2110                 mlx5_flow_list_destroy(dev, list, flow);
2111         }
2112 }
2113
2114 /**
2115  * Remove all flows.
2116  *
2117  * @param dev
2118  *   Pointer to Ethernet device.
2119  * @param list
2120  *   Pointer to a TAILQ flow list.
2121  */
2122 void
2123 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
2124 {
2125         struct rte_flow *flow;
2126
2127         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next)
2128                 flow_drv_remove(dev, flow);
2129         mlx5_flow_rxq_flags_clear(dev);
2130 }
2131
2132 /**
2133  * Add all flows.
2134  *
2135  * @param dev
2136  *   Pointer to Ethernet device.
2137  * @param list
2138  *   Pointer to a TAILQ flow list.
2139  *
2140  * @return
2141  *   0 on success, a negative errno value otherwise and rte_errno is set.
2142  */
2143 int
2144 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
2145 {
2146         struct rte_flow *flow;
2147         struct rte_flow_error error;
2148         int ret = 0;
2149
2150         TAILQ_FOREACH(flow, list, next) {
2151                 ret = flow_drv_apply(dev, flow, &error);
2152                 if (ret < 0)
2153                         goto error;
2154                 mlx5_flow_rxq_flags_set(dev, flow);
2155         }
2156         return 0;
2157 error:
2158         ret = rte_errno; /* Save rte_errno before cleanup. */
2159         mlx5_flow_stop(dev, list);
2160         rte_errno = ret; /* Restore rte_errno. */
2161         return -rte_errno;
2162 }
2163
2164 /**
2165  * Verify the flow list is empty
2166  *
2167  * @param dev
2168  *  Pointer to Ethernet device.
2169  *
2170  * @return the number of flows not released.
2171  */
2172 int
2173 mlx5_flow_verify(struct rte_eth_dev *dev)
2174 {
2175         struct priv *priv = dev->data->dev_private;
2176         struct rte_flow *flow;
2177         int ret = 0;
2178
2179         TAILQ_FOREACH(flow, &priv->flows, next) {
2180                 DRV_LOG(DEBUG, "port %u flow %p still referenced",
2181                         dev->data->port_id, (void *)flow);
2182                 ++ret;
2183         }
2184         return ret;
2185 }
2186
2187 /**
2188  * Enable a control flow configured from the control plane.
2189  *
2190  * @param dev
2191  *   Pointer to Ethernet device.
2192  * @param eth_spec
2193  *   An Ethernet flow spec to apply.
2194  * @param eth_mask
2195  *   An Ethernet flow mask to apply.
2196  * @param vlan_spec
2197  *   A VLAN flow spec to apply.
2198  * @param vlan_mask
2199  *   A VLAN flow mask to apply.
2200  *
2201  * @return
2202  *   0 on success, a negative errno value otherwise and rte_errno is set.
2203  */
2204 int
2205 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
2206                     struct rte_flow_item_eth *eth_spec,
2207                     struct rte_flow_item_eth *eth_mask,
2208                     struct rte_flow_item_vlan *vlan_spec,
2209                     struct rte_flow_item_vlan *vlan_mask)
2210 {
2211         struct priv *priv = dev->data->dev_private;
2212         const struct rte_flow_attr attr = {
2213                 .ingress = 1,
2214                 .priority = MLX5_FLOW_PRIO_RSVD,
2215         };
2216         struct rte_flow_item items[] = {
2217                 {
2218                         .type = RTE_FLOW_ITEM_TYPE_ETH,
2219                         .spec = eth_spec,
2220                         .last = NULL,
2221                         .mask = eth_mask,
2222                 },
2223                 {
2224                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
2225                                               RTE_FLOW_ITEM_TYPE_END,
2226                         .spec = vlan_spec,
2227                         .last = NULL,
2228                         .mask = vlan_mask,
2229                 },
2230                 {
2231                         .type = RTE_FLOW_ITEM_TYPE_END,
2232                 },
2233         };
2234         uint16_t queue[priv->reta_idx_n];
2235         struct rte_flow_action_rss action_rss = {
2236                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2237                 .level = 0,
2238                 .types = priv->rss_conf.rss_hf,
2239                 .key_len = priv->rss_conf.rss_key_len,
2240                 .queue_num = priv->reta_idx_n,
2241                 .key = priv->rss_conf.rss_key,
2242                 .queue = queue,
2243         };
2244         struct rte_flow_action actions[] = {
2245                 {
2246                         .type = RTE_FLOW_ACTION_TYPE_RSS,
2247                         .conf = &action_rss,
2248                 },
2249                 {
2250                         .type = RTE_FLOW_ACTION_TYPE_END,
2251                 },
2252         };
2253         struct rte_flow *flow;
2254         struct rte_flow_error error;
2255         unsigned int i;
2256
2257         if (!priv->reta_idx_n) {
2258                 rte_errno = EINVAL;
2259                 return -rte_errno;
2260         }
2261         for (i = 0; i != priv->reta_idx_n; ++i)
2262                 queue[i] = (*priv->reta_idx)[i];
2263         flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
2264                                      actions, &error);
2265         if (!flow)
2266                 return -rte_errno;
2267         return 0;
2268 }
2269
2270 /**
2271  * Enable a flow control configured from the control plane.
2272  *
2273  * @param dev
2274  *   Pointer to Ethernet device.
2275  * @param eth_spec
2276  *   An Ethernet flow spec to apply.
2277  * @param eth_mask
2278  *   An Ethernet flow mask to apply.
2279  *
2280  * @return
2281  *   0 on success, a negative errno value otherwise and rte_errno is set.
2282  */
2283 int
2284 mlx5_ctrl_flow(struct rte_eth_dev *dev,
2285                struct rte_flow_item_eth *eth_spec,
2286                struct rte_flow_item_eth *eth_mask)
2287 {
2288         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
2289 }
2290
2291 /**
2292  * Destroy a flow.
2293  *
2294  * @see rte_flow_destroy()
2295  * @see rte_flow_ops
2296  */
2297 int
2298 mlx5_flow_destroy(struct rte_eth_dev *dev,
2299                   struct rte_flow *flow,
2300                   struct rte_flow_error *error __rte_unused)
2301 {
2302         struct priv *priv = dev->data->dev_private;
2303
2304         mlx5_flow_list_destroy(dev, &priv->flows, flow);
2305         return 0;
2306 }
2307
2308 /**
2309  * Destroy all flows.
2310  *
2311  * @see rte_flow_flush()
2312  * @see rte_flow_ops
2313  */
2314 int
2315 mlx5_flow_flush(struct rte_eth_dev *dev,
2316                 struct rte_flow_error *error __rte_unused)
2317 {
2318         struct priv *priv = dev->data->dev_private;
2319
2320         mlx5_flow_list_flush(dev, &priv->flows);
2321         return 0;
2322 }
2323
2324 /**
2325  * Isolated mode.
2326  *
2327  * @see rte_flow_isolate()
2328  * @see rte_flow_ops
2329  */
2330 int
2331 mlx5_flow_isolate(struct rte_eth_dev *dev,
2332                   int enable,
2333                   struct rte_flow_error *error)
2334 {
2335         struct priv *priv = dev->data->dev_private;
2336
2337         if (dev->data->dev_started) {
2338                 rte_flow_error_set(error, EBUSY,
2339                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2340                                    NULL,
2341                                    "port must be stopped first");
2342                 return -rte_errno;
2343         }
2344         priv->isolated = !!enable;
2345         if (enable)
2346                 dev->dev_ops = &mlx5_dev_ops_isolate;
2347         else
2348                 dev->dev_ops = &mlx5_dev_ops;
2349         return 0;
2350 }
2351
2352 /**
2353  * Query a flow.
2354  *
2355  * @see rte_flow_query()
2356  * @see rte_flow_ops
2357  */
2358 static int
2359 flow_drv_query(struct rte_eth_dev *dev,
2360                struct rte_flow *flow,
2361                const struct rte_flow_action *actions,
2362                void *data,
2363                struct rte_flow_error *error)
2364 {
2365         const struct mlx5_flow_driver_ops *fops;
2366         enum mlx5_flow_drv_type ftype = flow->drv_type;
2367
2368         assert(ftype > MLX5_FLOW_TYPE_MIN && ftype < MLX5_FLOW_TYPE_MAX);
2369         fops = flow_get_drv_ops(ftype);
2370
2371         return fops->query(dev, flow, actions, data, error);
2372 }
2373
2374 /**
2375  * Query a flow.
2376  *
2377  * @see rte_flow_query()
2378  * @see rte_flow_ops
2379  */
2380 int
2381 mlx5_flow_query(struct rte_eth_dev *dev,
2382                 struct rte_flow *flow,
2383                 const struct rte_flow_action *actions,
2384                 void *data,
2385                 struct rte_flow_error *error)
2386 {
2387         int ret;
2388
2389         ret = flow_drv_query(dev, flow, actions, data, error);
2390         if (ret < 0)
2391                 return ret;
2392         return 0;
2393 }
2394
2395 /**
2396  * Convert a flow director filter to a generic flow.
2397  *
2398  * @param dev
2399  *   Pointer to Ethernet device.
2400  * @param fdir_filter
2401  *   Flow director filter to add.
2402  * @param attributes
2403  *   Generic flow parameters structure.
2404  *
2405  * @return
2406  *   0 on success, a negative errno value otherwise and rte_errno is set.
2407  */
2408 static int
2409 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
2410                          const struct rte_eth_fdir_filter *fdir_filter,
2411                          struct mlx5_fdir *attributes)
2412 {
2413         struct priv *priv = dev->data->dev_private;
2414         const struct rte_eth_fdir_input *input = &fdir_filter->input;
2415         const struct rte_eth_fdir_masks *mask =
2416                 &dev->data->dev_conf.fdir_conf.mask;
2417
2418         /* Validate queue number. */
2419         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
2420                 DRV_LOG(ERR, "port %u invalid queue number %d",
2421                         dev->data->port_id, fdir_filter->action.rx_queue);
2422                 rte_errno = EINVAL;
2423                 return -rte_errno;
2424         }
2425         attributes->attr.ingress = 1;
2426         attributes->items[0] = (struct rte_flow_item) {
2427                 .type = RTE_FLOW_ITEM_TYPE_ETH,
2428                 .spec = &attributes->l2,
2429                 .mask = &attributes->l2_mask,
2430         };
2431         switch (fdir_filter->action.behavior) {
2432         case RTE_ETH_FDIR_ACCEPT:
2433                 attributes->actions[0] = (struct rte_flow_action){
2434                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
2435                         .conf = &attributes->queue,
2436                 };
2437                 break;
2438         case RTE_ETH_FDIR_REJECT:
2439                 attributes->actions[0] = (struct rte_flow_action){
2440                         .type = RTE_FLOW_ACTION_TYPE_DROP,
2441                 };
2442                 break;
2443         default:
2444                 DRV_LOG(ERR, "port %u invalid behavior %d",
2445                         dev->data->port_id,
2446                         fdir_filter->action.behavior);
2447                 rte_errno = ENOTSUP;
2448                 return -rte_errno;
2449         }
2450         attributes->queue.index = fdir_filter->action.rx_queue;
2451         /* Handle L3. */
2452         switch (fdir_filter->input.flow_type) {
2453         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2454         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2455         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2456                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
2457                         .src_addr = input->flow.ip4_flow.src_ip,
2458                         .dst_addr = input->flow.ip4_flow.dst_ip,
2459                         .time_to_live = input->flow.ip4_flow.ttl,
2460                         .type_of_service = input->flow.ip4_flow.tos,
2461                 };
2462                 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
2463                         .src_addr = mask->ipv4_mask.src_ip,
2464                         .dst_addr = mask->ipv4_mask.dst_ip,
2465                         .time_to_live = mask->ipv4_mask.ttl,
2466                         .type_of_service = mask->ipv4_mask.tos,
2467                         .next_proto_id = mask->ipv4_mask.proto,
2468                 };
2469                 attributes->items[1] = (struct rte_flow_item){
2470                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
2471                         .spec = &attributes->l3,
2472                         .mask = &attributes->l3_mask,
2473                 };
2474                 break;
2475         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2476         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2477         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2478                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
2479                         .hop_limits = input->flow.ipv6_flow.hop_limits,
2480                         .proto = input->flow.ipv6_flow.proto,
2481                 };
2482
2483                 memcpy(attributes->l3.ipv6.hdr.src_addr,
2484                        input->flow.ipv6_flow.src_ip,
2485                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2486                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
2487                        input->flow.ipv6_flow.dst_ip,
2488                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
2489                 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
2490                        mask->ipv6_mask.src_ip,
2491                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2492                 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
2493                        mask->ipv6_mask.dst_ip,
2494                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
2495                 attributes->items[1] = (struct rte_flow_item){
2496                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
2497                         .spec = &attributes->l3,
2498                         .mask = &attributes->l3_mask,
2499                 };
2500                 break;
2501         default:
2502                 DRV_LOG(ERR, "port %u invalid flow type%d",
2503                         dev->data->port_id, fdir_filter->input.flow_type);
2504                 rte_errno = ENOTSUP;
2505                 return -rte_errno;
2506         }
2507         /* Handle L4. */
2508         switch (fdir_filter->input.flow_type) {
2509         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
2510                 attributes->l4.udp.hdr = (struct udp_hdr){
2511                         .src_port = input->flow.udp4_flow.src_port,
2512                         .dst_port = input->flow.udp4_flow.dst_port,
2513                 };
2514                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2515                         .src_port = mask->src_port_mask,
2516                         .dst_port = mask->dst_port_mask,
2517                 };
2518                 attributes->items[2] = (struct rte_flow_item){
2519                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2520                         .spec = &attributes->l4,
2521                         .mask = &attributes->l4_mask,
2522                 };
2523                 break;
2524         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
2525                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2526                         .src_port = input->flow.tcp4_flow.src_port,
2527                         .dst_port = input->flow.tcp4_flow.dst_port,
2528                 };
2529                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2530                         .src_port = mask->src_port_mask,
2531                         .dst_port = mask->dst_port_mask,
2532                 };
2533                 attributes->items[2] = (struct rte_flow_item){
2534                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2535                         .spec = &attributes->l4,
2536                         .mask = &attributes->l4_mask,
2537                 };
2538                 break;
2539         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
2540                 attributes->l4.udp.hdr = (struct udp_hdr){
2541                         .src_port = input->flow.udp6_flow.src_port,
2542                         .dst_port = input->flow.udp6_flow.dst_port,
2543                 };
2544                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
2545                         .src_port = mask->src_port_mask,
2546                         .dst_port = mask->dst_port_mask,
2547                 };
2548                 attributes->items[2] = (struct rte_flow_item){
2549                         .type = RTE_FLOW_ITEM_TYPE_UDP,
2550                         .spec = &attributes->l4,
2551                         .mask = &attributes->l4_mask,
2552                 };
2553                 break;
2554         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
2555                 attributes->l4.tcp.hdr = (struct tcp_hdr){
2556                         .src_port = input->flow.tcp6_flow.src_port,
2557                         .dst_port = input->flow.tcp6_flow.dst_port,
2558                 };
2559                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
2560                         .src_port = mask->src_port_mask,
2561                         .dst_port = mask->dst_port_mask,
2562                 };
2563                 attributes->items[2] = (struct rte_flow_item){
2564                         .type = RTE_FLOW_ITEM_TYPE_TCP,
2565                         .spec = &attributes->l4,
2566                         .mask = &attributes->l4_mask,
2567                 };
2568                 break;
2569         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
2570         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
2571                 break;
2572         default:
2573                 DRV_LOG(ERR, "port %u invalid flow type%d",
2574                         dev->data->port_id, fdir_filter->input.flow_type);
2575                 rte_errno = ENOTSUP;
2576                 return -rte_errno;
2577         }
2578         return 0;
2579 }
2580
2581 /**
2582  * Add new flow director filter and store it in list.
2583  *
2584  * @param dev
2585  *   Pointer to Ethernet device.
2586  * @param fdir_filter
2587  *   Flow director filter to add.
2588  *
2589  * @return
2590  *   0 on success, a negative errno value otherwise and rte_errno is set.
2591  */
2592 static int
2593 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
2594                      const struct rte_eth_fdir_filter *fdir_filter)
2595 {
2596         struct priv *priv = dev->data->dev_private;
2597         struct mlx5_fdir attributes = {
2598                 .attr.group = 0,
2599                 .l2_mask = {
2600                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
2601                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
2602                         .type = 0,
2603                 },
2604         };
2605         struct rte_flow_error error;
2606         struct rte_flow *flow;
2607         int ret;
2608
2609         ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
2610         if (ret)
2611                 return ret;
2612         flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
2613                                      attributes.items, attributes.actions,
2614                                      &error);
2615         if (flow) {
2616                 DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
2617                         (void *)flow);
2618                 return 0;
2619         }
2620         return -rte_errno;
2621 }
2622
2623 /**
2624  * Delete specific filter.
2625  *
2626  * @param dev
2627  *   Pointer to Ethernet device.
2628  * @param fdir_filter
2629  *   Filter to be deleted.
2630  *
2631  * @return
2632  *   0 on success, a negative errno value otherwise and rte_errno is set.
2633  */
2634 static int
2635 mlx5_fdir_filter_delete(struct rte_eth_dev *dev __rte_unused,
2636                         const struct rte_eth_fdir_filter *fdir_filter
2637                         __rte_unused)
2638 {
2639         rte_errno = ENOTSUP;
2640         return -rte_errno;
2641 }
2642
2643 /**
2644  * Update queue for specific filter.
2645  *
2646  * @param dev
2647  *   Pointer to Ethernet device.
2648  * @param fdir_filter
2649  *   Filter to be updated.
2650  *
2651  * @return
2652  *   0 on success, a negative errno value otherwise and rte_errno is set.
2653  */
2654 static int
2655 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
2656                         const struct rte_eth_fdir_filter *fdir_filter)
2657 {
2658         int ret;
2659
2660         ret = mlx5_fdir_filter_delete(dev, fdir_filter);
2661         if (ret)
2662                 return ret;
2663         return mlx5_fdir_filter_add(dev, fdir_filter);
2664 }
2665
2666 /**
2667  * Flush all filters.
2668  *
2669  * @param dev
2670  *   Pointer to Ethernet device.
2671  */
2672 static void
2673 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
2674 {
2675         struct priv *priv = dev->data->dev_private;
2676
2677         mlx5_flow_list_flush(dev, &priv->flows);
2678 }
2679
2680 /**
2681  * Get flow director information.
2682  *
2683  * @param dev
2684  *   Pointer to Ethernet device.
2685  * @param[out] fdir_info
2686  *   Resulting flow director information.
2687  */
2688 static void
2689 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
2690 {
2691         struct rte_eth_fdir_masks *mask =
2692                 &dev->data->dev_conf.fdir_conf.mask;
2693
2694         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
2695         fdir_info->guarant_spc = 0;
2696         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
2697         fdir_info->max_flexpayload = 0;
2698         fdir_info->flow_types_mask[0] = 0;
2699         fdir_info->flex_payload_unit = 0;
2700         fdir_info->max_flex_payload_segment_num = 0;
2701         fdir_info->flex_payload_limit = 0;
2702         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
2703 }
2704
2705 /**
2706  * Deal with flow director operations.
2707  *
2708  * @param dev
2709  *   Pointer to Ethernet device.
2710  * @param filter_op
2711  *   Operation to perform.
2712  * @param arg
2713  *   Pointer to operation-specific structure.
2714  *
2715  * @return
2716  *   0 on success, a negative errno value otherwise and rte_errno is set.
2717  */
2718 static int
2719 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
2720                     void *arg)
2721 {
2722         enum rte_fdir_mode fdir_mode =
2723                 dev->data->dev_conf.fdir_conf.mode;
2724
2725         if (filter_op == RTE_ETH_FILTER_NOP)
2726                 return 0;
2727         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
2728             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
2729                 DRV_LOG(ERR, "port %u flow director mode %d not supported",
2730                         dev->data->port_id, fdir_mode);
2731                 rte_errno = EINVAL;
2732                 return -rte_errno;
2733         }
2734         switch (filter_op) {
2735         case RTE_ETH_FILTER_ADD:
2736                 return mlx5_fdir_filter_add(dev, arg);
2737         case RTE_ETH_FILTER_UPDATE:
2738                 return mlx5_fdir_filter_update(dev, arg);
2739         case RTE_ETH_FILTER_DELETE:
2740                 return mlx5_fdir_filter_delete(dev, arg);
2741         case RTE_ETH_FILTER_FLUSH:
2742                 mlx5_fdir_filter_flush(dev);
2743                 break;
2744         case RTE_ETH_FILTER_INFO:
2745                 mlx5_fdir_info_get(dev, arg);
2746                 break;
2747         default:
2748                 DRV_LOG(DEBUG, "port %u unknown operation %u",
2749                         dev->data->port_id, filter_op);
2750                 rte_errno = EINVAL;
2751                 return -rte_errno;
2752         }
2753         return 0;
2754 }
2755
2756 /**
2757  * Manage filter operations.
2758  *
2759  * @param dev
2760  *   Pointer to Ethernet device structure.
2761  * @param filter_type
2762  *   Filter type.
2763  * @param filter_op
2764  *   Operation to perform.
2765  * @param arg
2766  *   Pointer to operation-specific structure.
2767  *
2768  * @return
2769  *   0 on success, a negative errno value otherwise and rte_errno is set.
2770  */
2771 int
2772 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
2773                      enum rte_filter_type filter_type,
2774                      enum rte_filter_op filter_op,
2775                      void *arg)
2776 {
2777         switch (filter_type) {
2778         case RTE_ETH_FILTER_GENERIC:
2779                 if (filter_op != RTE_ETH_FILTER_GET) {
2780                         rte_errno = EINVAL;
2781                         return -rte_errno;
2782                 }
2783                 *(const void **)arg = &mlx5_flow_ops;
2784                 return 0;
2785         case RTE_ETH_FILTER_FDIR:
2786                 return mlx5_fdir_ctrl_func(dev, filter_op, arg);
2787         default:
2788                 DRV_LOG(ERR, "port %u filter type (%d) not supported",
2789                         dev->data->port_id, filter_type);
2790                 rte_errno = ENOTSUP;
2791                 return -rte_errno;
2792         }
2793         return 0;
2794 }