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