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