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