net/mlx5: add hardware flow debug dump
[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 <sys/queue.h>
7 #include <stdint.h>
8 #include <string.h>
9
10 /* Verbs header. */
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/verbs.h>
16 #ifdef PEDANTIC
17 #pragma GCC diagnostic error "-Wpedantic"
18 #endif
19
20 #include <rte_common.h>
21 #include <rte_ether.h>
22 #include <rte_eth_ctrl.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_flow.h>
25 #include <rte_flow_driver.h>
26 #include <rte_malloc.h>
27 #include <rte_ip.h>
28
29 #include "mlx5.h"
30 #include "mlx5_defs.h"
31 #include "mlx5_prm.h"
32 #include "mlx5_glue.h"
33
34 /* Flow priority for control plane flows. */
35 #define MLX5_CTRL_FLOW_PRIORITY 1
36
37 /* Internet Protocol versions. */
38 #define MLX5_IPV4 4
39 #define MLX5_IPV6 6
40 #define MLX5_GRE 47
41
42 #ifndef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
43 struct ibv_flow_spec_counter_action {
44         int dummy;
45 };
46 #endif
47
48 /* Dev ops structure defined in mlx5.c */
49 extern const struct eth_dev_ops mlx5_dev_ops;
50 extern const struct eth_dev_ops mlx5_dev_ops_isolate;
51
52 /** Structure give to the conversion functions. */
53 struct mlx5_flow_data {
54         struct rte_eth_dev *dev; /** Ethernet device. */
55         struct mlx5_flow_parse *parser; /** Parser context. */
56         struct rte_flow_error *error; /** Error context. */
57 };
58
59 static int
60 mlx5_flow_create_eth(const struct rte_flow_item *item,
61                      const void *default_mask,
62                      struct mlx5_flow_data *data);
63
64 static int
65 mlx5_flow_create_vlan(const struct rte_flow_item *item,
66                       const void *default_mask,
67                       struct mlx5_flow_data *data);
68
69 static int
70 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
71                       const void *default_mask,
72                       struct mlx5_flow_data *data);
73
74 static int
75 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
76                       const void *default_mask,
77                       struct mlx5_flow_data *data);
78
79 static int
80 mlx5_flow_create_udp(const struct rte_flow_item *item,
81                      const void *default_mask,
82                      struct mlx5_flow_data *data);
83
84 static int
85 mlx5_flow_create_tcp(const struct rte_flow_item *item,
86                      const void *default_mask,
87                      struct mlx5_flow_data *data);
88
89 static int
90 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
91                        const void *default_mask,
92                        struct mlx5_flow_data *data);
93
94 static int
95 mlx5_flow_create_gre(const struct rte_flow_item *item,
96                      const void *default_mask,
97                      struct mlx5_flow_data *data);
98
99 struct mlx5_flow_parse;
100
101 static void
102 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
103                       unsigned int size);
104
105 static int
106 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id);
107
108 static int
109 mlx5_flow_create_count(struct rte_eth_dev *dev, struct mlx5_flow_parse *parser);
110
111 /* Hash RX queue types. */
112 enum hash_rxq_type {
113         HASH_RXQ_TCPV4,
114         HASH_RXQ_UDPV4,
115         HASH_RXQ_IPV4,
116         HASH_RXQ_TCPV6,
117         HASH_RXQ_UDPV6,
118         HASH_RXQ_IPV6,
119         HASH_RXQ_ETH,
120         HASH_RXQ_TUNNEL,
121 };
122
123 /* Initialization data for hash RX queue. */
124 struct hash_rxq_init {
125         uint64_t hash_fields; /* Fields that participate in the hash. */
126         uint64_t dpdk_rss_hf; /* Matching DPDK RSS hash fields. */
127         unsigned int flow_priority; /* Flow priority to use. */
128         unsigned int ip_version; /* Internet protocol. */
129 };
130
131 /* Initialization data for hash RX queues. */
132 const struct hash_rxq_init hash_rxq_init[] = {
133         [HASH_RXQ_TCPV4] = {
134                 .hash_fields = (IBV_RX_HASH_SRC_IPV4 |
135                                 IBV_RX_HASH_DST_IPV4 |
136                                 IBV_RX_HASH_SRC_PORT_TCP |
137                                 IBV_RX_HASH_DST_PORT_TCP),
138                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_TCP,
139                 .flow_priority = 0,
140                 .ip_version = MLX5_IPV4,
141         },
142         [HASH_RXQ_UDPV4] = {
143                 .hash_fields = (IBV_RX_HASH_SRC_IPV4 |
144                                 IBV_RX_HASH_DST_IPV4 |
145                                 IBV_RX_HASH_SRC_PORT_UDP |
146                                 IBV_RX_HASH_DST_PORT_UDP),
147                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV4_UDP,
148                 .flow_priority = 0,
149                 .ip_version = MLX5_IPV4,
150         },
151         [HASH_RXQ_IPV4] = {
152                 .hash_fields = (IBV_RX_HASH_SRC_IPV4 |
153                                 IBV_RX_HASH_DST_IPV4),
154                 .dpdk_rss_hf = (ETH_RSS_IPV4 |
155                                 ETH_RSS_FRAG_IPV4),
156                 .flow_priority = 1,
157                 .ip_version = MLX5_IPV4,
158         },
159         [HASH_RXQ_TCPV6] = {
160                 .hash_fields = (IBV_RX_HASH_SRC_IPV6 |
161                                 IBV_RX_HASH_DST_IPV6 |
162                                 IBV_RX_HASH_SRC_PORT_TCP |
163                                 IBV_RX_HASH_DST_PORT_TCP),
164                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_TCP,
165                 .flow_priority = 0,
166                 .ip_version = MLX5_IPV6,
167         },
168         [HASH_RXQ_UDPV6] = {
169                 .hash_fields = (IBV_RX_HASH_SRC_IPV6 |
170                                 IBV_RX_HASH_DST_IPV6 |
171                                 IBV_RX_HASH_SRC_PORT_UDP |
172                                 IBV_RX_HASH_DST_PORT_UDP),
173                 .dpdk_rss_hf = ETH_RSS_NONFRAG_IPV6_UDP,
174                 .flow_priority = 0,
175                 .ip_version = MLX5_IPV6,
176         },
177         [HASH_RXQ_IPV6] = {
178                 .hash_fields = (IBV_RX_HASH_SRC_IPV6 |
179                                 IBV_RX_HASH_DST_IPV6),
180                 .dpdk_rss_hf = (ETH_RSS_IPV6 |
181                                 ETH_RSS_FRAG_IPV6),
182                 .flow_priority = 1,
183                 .ip_version = MLX5_IPV6,
184         },
185         [HASH_RXQ_ETH] = {
186                 .hash_fields = 0,
187                 .dpdk_rss_hf = 0,
188                 .flow_priority = 2,
189         },
190 };
191
192 /* Number of entries in hash_rxq_init[]. */
193 const unsigned int hash_rxq_init_n = RTE_DIM(hash_rxq_init);
194
195 /** Structure for holding counter stats. */
196 struct mlx5_flow_counter_stats {
197         uint64_t hits; /**< Number of packets matched by the rule. */
198         uint64_t bytes; /**< Number of bytes matched by the rule. */
199 };
200
201 /** Structure for Drop queue. */
202 struct mlx5_hrxq_drop {
203         struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
204         struct ibv_qp *qp; /**< Verbs queue pair. */
205         struct ibv_wq *wq; /**< Verbs work queue. */
206         struct ibv_cq *cq; /**< Verbs completion queue. */
207 };
208
209 /* Flows structures. */
210 struct mlx5_flow {
211         uint64_t hash_fields; /**< Fields that participate in the hash. */
212         struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
213         struct ibv_flow *ibv_flow; /**< Verbs flow. */
214         struct mlx5_hrxq *hrxq; /**< Hash Rx queues. */
215 };
216
217 /* Drop flows structures. */
218 struct mlx5_flow_drop {
219         struct ibv_flow_attr *ibv_attr; /**< Pointer to Verbs attributes. */
220         struct ibv_flow *ibv_flow; /**< Verbs flow. */
221 };
222
223 struct rte_flow {
224         TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
225         uint32_t mark:1; /**< Set if the flow is marked. */
226         uint32_t drop:1; /**< Drop queue. */
227         struct rte_flow_action_rss rss_conf; /**< RSS configuration */
228         uint16_t (*queues)[]; /**< Queues indexes to use. */
229         uint8_t rss_key[40]; /**< copy of the RSS key. */
230         uint32_t tunnel; /**< Tunnel type of RTE_PTYPE_TUNNEL_XXX. */
231         struct ibv_counter_set *cs; /**< Holds the counters for the rule. */
232         struct mlx5_flow_counter_stats counter_stats;/**<The counter stats. */
233         struct mlx5_flow frxq[RTE_DIM(hash_rxq_init)];
234         /**< Flow with Rx queue. */
235 };
236
237 /** Static initializer for items. */
238 #define ITEMS(...) \
239         (const enum rte_flow_item_type []){ \
240                 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
241         }
242
243 #define IS_TUNNEL(type) ( \
244         (type) == RTE_FLOW_ITEM_TYPE_VXLAN || \
245         (type) == RTE_FLOW_ITEM_TYPE_GRE)
246
247 const uint32_t flow_ptype[] = {
248         [RTE_FLOW_ITEM_TYPE_VXLAN] = RTE_PTYPE_TUNNEL_VXLAN,
249         [RTE_FLOW_ITEM_TYPE_GRE] = RTE_PTYPE_TUNNEL_GRE,
250 };
251
252 #define PTYPE_IDX(t) ((RTE_PTYPE_TUNNEL_MASK & (t)) >> 12)
253
254 const uint32_t ptype_ext[] = {
255         [PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)] = RTE_PTYPE_TUNNEL_VXLAN |
256                                               RTE_PTYPE_L4_UDP,
257         [PTYPE_IDX(RTE_PTYPE_TUNNEL_GRE)] = RTE_PTYPE_TUNNEL_GRE,
258 };
259
260 /** Structure to generate a simple graph of layers supported by the NIC. */
261 struct mlx5_flow_items {
262         /** List of possible actions for these items. */
263         const enum rte_flow_action_type *const actions;
264         /** Bit-masks corresponding to the possibilities for the item. */
265         const void *mask;
266         /**
267          * Default bit-masks to use when item->mask is not provided. When
268          * \default_mask is also NULL, the full supported bit-mask (\mask) is
269          * used instead.
270          */
271         const void *default_mask;
272         /** Bit-masks size in bytes. */
273         const unsigned int mask_sz;
274         /**
275          * Conversion function from rte_flow to NIC specific flow.
276          *
277          * @param item
278          *   rte_flow item to convert.
279          * @param default_mask
280          *   Default bit-masks to use when item->mask is not provided.
281          * @param data
282          *   Internal structure to store the conversion.
283          *
284          * @return
285          *   0 on success, a negative errno value otherwise and rte_errno is
286          *   set.
287          */
288         int (*convert)(const struct rte_flow_item *item,
289                        const void *default_mask,
290                        struct mlx5_flow_data *data);
291         /** Size in bytes of the destination structure. */
292         const unsigned int dst_sz;
293         /** List of possible following items.  */
294         const enum rte_flow_item_type *const items;
295 };
296
297 /** Valid action for this PMD. */
298 static const enum rte_flow_action_type valid_actions[] = {
299         RTE_FLOW_ACTION_TYPE_DROP,
300         RTE_FLOW_ACTION_TYPE_QUEUE,
301         RTE_FLOW_ACTION_TYPE_MARK,
302         RTE_FLOW_ACTION_TYPE_FLAG,
303 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
304         RTE_FLOW_ACTION_TYPE_COUNT,
305 #endif
306         RTE_FLOW_ACTION_TYPE_END,
307 };
308
309 /** Graph of supported items and associated actions. */
310 static const struct mlx5_flow_items mlx5_flow_items[] = {
311         [RTE_FLOW_ITEM_TYPE_END] = {
312                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
313                                RTE_FLOW_ITEM_TYPE_VXLAN,
314                                RTE_FLOW_ITEM_TYPE_GRE),
315         },
316         [RTE_FLOW_ITEM_TYPE_ETH] = {
317                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_VLAN,
318                                RTE_FLOW_ITEM_TYPE_IPV4,
319                                RTE_FLOW_ITEM_TYPE_IPV6),
320                 .actions = valid_actions,
321                 .mask = &(const struct rte_flow_item_eth){
322                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
323                         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
324                         .type = -1,
325                 },
326                 .default_mask = &rte_flow_item_eth_mask,
327                 .mask_sz = sizeof(struct rte_flow_item_eth),
328                 .convert = mlx5_flow_create_eth,
329                 .dst_sz = sizeof(struct ibv_flow_spec_eth),
330         },
331         [RTE_FLOW_ITEM_TYPE_VLAN] = {
332                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
333                                RTE_FLOW_ITEM_TYPE_IPV6),
334                 .actions = valid_actions,
335                 .mask = &(const struct rte_flow_item_vlan){
336                         .tci = -1,
337                         .inner_type = -1,
338                 },
339                 .default_mask = &rte_flow_item_vlan_mask,
340                 .mask_sz = sizeof(struct rte_flow_item_vlan),
341                 .convert = mlx5_flow_create_vlan,
342                 .dst_sz = 0,
343         },
344         [RTE_FLOW_ITEM_TYPE_IPV4] = {
345                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
346                                RTE_FLOW_ITEM_TYPE_TCP,
347                                RTE_FLOW_ITEM_TYPE_GRE),
348                 .actions = valid_actions,
349                 .mask = &(const struct rte_flow_item_ipv4){
350                         .hdr = {
351                                 .src_addr = -1,
352                                 .dst_addr = -1,
353                                 .type_of_service = -1,
354                                 .next_proto_id = -1,
355                         },
356                 },
357                 .default_mask = &rte_flow_item_ipv4_mask,
358                 .mask_sz = sizeof(struct rte_flow_item_ipv4),
359                 .convert = mlx5_flow_create_ipv4,
360                 .dst_sz = sizeof(struct ibv_flow_spec_ipv4_ext),
361         },
362         [RTE_FLOW_ITEM_TYPE_IPV6] = {
363                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
364                                RTE_FLOW_ITEM_TYPE_TCP,
365                                RTE_FLOW_ITEM_TYPE_GRE),
366                 .actions = valid_actions,
367                 .mask = &(const struct rte_flow_item_ipv6){
368                         .hdr = {
369                                 .src_addr = {
370                                         0xff, 0xff, 0xff, 0xff,
371                                         0xff, 0xff, 0xff, 0xff,
372                                         0xff, 0xff, 0xff, 0xff,
373                                         0xff, 0xff, 0xff, 0xff,
374                                 },
375                                 .dst_addr = {
376                                         0xff, 0xff, 0xff, 0xff,
377                                         0xff, 0xff, 0xff, 0xff,
378                                         0xff, 0xff, 0xff, 0xff,
379                                         0xff, 0xff, 0xff, 0xff,
380                                 },
381                                 .vtc_flow = -1,
382                                 .proto = -1,
383                                 .hop_limits = -1,
384                         },
385                 },
386                 .default_mask = &rte_flow_item_ipv6_mask,
387                 .mask_sz = sizeof(struct rte_flow_item_ipv6),
388                 .convert = mlx5_flow_create_ipv6,
389                 .dst_sz = sizeof(struct ibv_flow_spec_ipv6),
390         },
391         [RTE_FLOW_ITEM_TYPE_UDP] = {
392                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_VXLAN),
393                 .actions = valid_actions,
394                 .mask = &(const struct rte_flow_item_udp){
395                         .hdr = {
396                                 .src_port = -1,
397                                 .dst_port = -1,
398                         },
399                 },
400                 .default_mask = &rte_flow_item_udp_mask,
401                 .mask_sz = sizeof(struct rte_flow_item_udp),
402                 .convert = mlx5_flow_create_udp,
403                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
404         },
405         [RTE_FLOW_ITEM_TYPE_TCP] = {
406                 .actions = valid_actions,
407                 .mask = &(const struct rte_flow_item_tcp){
408                         .hdr = {
409                                 .src_port = -1,
410                                 .dst_port = -1,
411                         },
412                 },
413                 .default_mask = &rte_flow_item_tcp_mask,
414                 .mask_sz = sizeof(struct rte_flow_item_tcp),
415                 .convert = mlx5_flow_create_tcp,
416                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
417         },
418         [RTE_FLOW_ITEM_TYPE_GRE] = {
419                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
420                                RTE_FLOW_ITEM_TYPE_IPV4,
421                                RTE_FLOW_ITEM_TYPE_IPV6),
422                 .actions = valid_actions,
423                 .mask = &(const struct rte_flow_item_gre){
424                         .protocol = -1,
425                 },
426                 .default_mask = &rte_flow_item_gre_mask,
427                 .mask_sz = sizeof(struct rte_flow_item_gre),
428                 .convert = mlx5_flow_create_gre,
429                 .dst_sz = sizeof(struct ibv_flow_spec_tunnel),
430         },
431         [RTE_FLOW_ITEM_TYPE_VXLAN] = {
432                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH,
433                                RTE_FLOW_ITEM_TYPE_IPV4, /* For L3 VXLAN. */
434                                RTE_FLOW_ITEM_TYPE_IPV6), /* For L3 VXLAN. */
435                 .actions = valid_actions,
436                 .mask = &(const struct rte_flow_item_vxlan){
437                         .vni = "\xff\xff\xff",
438                 },
439                 .default_mask = &rte_flow_item_vxlan_mask,
440                 .mask_sz = sizeof(struct rte_flow_item_vxlan),
441                 .convert = mlx5_flow_create_vxlan,
442                 .dst_sz = sizeof(struct ibv_flow_spec_tunnel),
443         },
444 };
445
446 /** Structure to pass to the conversion function. */
447 struct mlx5_flow_parse {
448         uint32_t inner; /**< Verbs value, set once tunnel is encountered. */
449         uint32_t create:1;
450         /**< Whether resources should remain after a validate. */
451         uint32_t drop:1; /**< Target is a drop queue. */
452         uint32_t mark:1; /**< Mark is present in the flow. */
453         uint32_t count:1; /**< Count is present in the flow. */
454         uint32_t mark_id; /**< Mark identifier. */
455         struct rte_flow_action_rss rss_conf; /**< RSS configuration */
456         uint16_t queues[RTE_MAX_QUEUES_PER_PORT]; /**< Queues indexes to use. */
457         uint8_t rss_key[40]; /**< copy of the RSS key. */
458         enum hash_rxq_type layer; /**< Last pattern layer detected. */
459         enum hash_rxq_type out_layer; /**< Last outer pattern layer detected. */
460         uint32_t tunnel; /**< Tunnel type of RTE_PTYPE_TUNNEL_XXX. */
461         struct ibv_counter_set *cs; /**< Holds the counter set for the rule */
462         struct {
463                 struct ibv_flow_attr *ibv_attr;
464                 /**< Pointer to Verbs attributes. */
465                 unsigned int offset;
466                 /**< Current position or total size of the attribute. */
467                 uint64_t hash_fields; /**< Verbs hash fields. */
468         } queue[RTE_DIM(hash_rxq_init)];
469 };
470
471 static const struct rte_flow_ops mlx5_flow_ops = {
472         .validate = mlx5_flow_validate,
473         .create = mlx5_flow_create,
474         .destroy = mlx5_flow_destroy,
475         .flush = mlx5_flow_flush,
476 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
477         .query = mlx5_flow_query,
478 #else
479         .query = NULL,
480 #endif
481         .isolate = mlx5_flow_isolate,
482 };
483
484 /* Convert FDIR request to Generic flow. */
485 struct mlx5_fdir {
486         struct rte_flow_attr attr;
487         struct rte_flow_action actions[2];
488         struct rte_flow_item items[4];
489         struct rte_flow_item_eth l2;
490         struct rte_flow_item_eth l2_mask;
491         union {
492                 struct rte_flow_item_ipv4 ipv4;
493                 struct rte_flow_item_ipv6 ipv6;
494         } l3;
495         union {
496                 struct rte_flow_item_ipv4 ipv4;
497                 struct rte_flow_item_ipv6 ipv6;
498         } l3_mask;
499         union {
500                 struct rte_flow_item_udp udp;
501                 struct rte_flow_item_tcp tcp;
502         } l4;
503         union {
504                 struct rte_flow_item_udp udp;
505                 struct rte_flow_item_tcp tcp;
506         } l4_mask;
507         struct rte_flow_action_queue queue;
508 };
509
510 /* Verbs specification header. */
511 struct ibv_spec_header {
512         enum ibv_flow_spec_type type;
513         uint16_t size;
514 };
515
516 /**
517  * Check support for a given item.
518  *
519  * @param item[in]
520  *   Item specification.
521  * @param mask[in]
522  *   Bit-masks covering supported fields to compare with spec, last and mask in
523  *   \item.
524  * @param size
525  *   Bit-Mask size in bytes.
526  *
527  * @return
528  *   0 on success, a negative errno value otherwise and rte_errno is set.
529  */
530 static int
531 mlx5_flow_item_validate(const struct rte_flow_item *item,
532                         const uint8_t *mask, unsigned int size)
533 {
534         if (!item->spec && (item->mask || item->last)) {
535                 rte_errno = EINVAL;
536                 return -rte_errno;
537         }
538         if (item->spec && !item->mask) {
539                 unsigned int i;
540                 const uint8_t *spec = item->spec;
541
542                 for (i = 0; i < size; ++i)
543                         if ((spec[i] | mask[i]) != mask[i]) {
544                                 rte_errno = EINVAL;
545                                 return -rte_errno;
546                         }
547         }
548         if (item->last && !item->mask) {
549                 unsigned int i;
550                 const uint8_t *spec = item->last;
551
552                 for (i = 0; i < size; ++i)
553                         if ((spec[i] | mask[i]) != mask[i]) {
554                                 rte_errno = EINVAL;
555                                 return -rte_errno;
556                         }
557         }
558         if (item->mask) {
559                 unsigned int i;
560                 const uint8_t *spec = item->spec;
561
562                 for (i = 0; i < size; ++i)
563                         if ((spec[i] | mask[i]) != mask[i]) {
564                                 rte_errno = EINVAL;
565                                 return -rte_errno;
566                         }
567         }
568         if (item->spec && item->last) {
569                 uint8_t spec[size];
570                 uint8_t last[size];
571                 const uint8_t *apply = mask;
572                 unsigned int i;
573                 int ret;
574
575                 if (item->mask)
576                         apply = item->mask;
577                 for (i = 0; i < size; ++i) {
578                         spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
579                         last[i] = ((const uint8_t *)item->last)[i] & apply[i];
580                 }
581                 ret = memcmp(spec, last, size);
582                 if (ret != 0) {
583                         rte_errno = EINVAL;
584                         return -rte_errno;
585                 }
586         }
587         return 0;
588 }
589
590 /**
591  * Extract attribute to the parser.
592  *
593  * @param[in] attr
594  *   Flow rule attributes.
595  * @param[out] error
596  *   Perform verbose error reporting if not NULL.
597  *
598  * @return
599  *   0 on success, a negative errno value otherwise and rte_errno is set.
600  */
601 static int
602 mlx5_flow_convert_attributes(const struct rte_flow_attr *attr,
603                              struct rte_flow_error *error)
604 {
605         if (attr->group) {
606                 rte_flow_error_set(error, ENOTSUP,
607                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
608                                    NULL,
609                                    "groups are not supported");
610                 return -rte_errno;
611         }
612         if (attr->priority && attr->priority != MLX5_CTRL_FLOW_PRIORITY) {
613                 rte_flow_error_set(error, ENOTSUP,
614                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
615                                    NULL,
616                                    "priorities are not supported");
617                 return -rte_errno;
618         }
619         if (attr->egress) {
620                 rte_flow_error_set(error, ENOTSUP,
621                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
622                                    NULL,
623                                    "egress is not supported");
624                 return -rte_errno;
625         }
626         if (attr->transfer) {
627                 rte_flow_error_set(error, ENOTSUP,
628                                    RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
629                                    NULL,
630                                    "transfer is not supported");
631                 return -rte_errno;
632         }
633         if (!attr->ingress) {
634                 rte_flow_error_set(error, ENOTSUP,
635                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
636                                    NULL,
637                                    "only ingress is supported");
638                 return -rte_errno;
639         }
640         return 0;
641 }
642
643 /**
644  * Extract actions request to the parser.
645  *
646  * @param dev
647  *   Pointer to Ethernet device.
648  * @param[in] actions
649  *   Associated actions (list terminated by the END action).
650  * @param[out] error
651  *   Perform verbose error reporting if not NULL.
652  * @param[in, out] parser
653  *   Internal parser structure.
654  *
655  * @return
656  *   0 on success, a negative errno value otherwise and rte_errno is set.
657  */
658 static int
659 mlx5_flow_convert_actions(struct rte_eth_dev *dev,
660                           const struct rte_flow_action actions[],
661                           struct rte_flow_error *error,
662                           struct mlx5_flow_parse *parser)
663 {
664         enum { FATE = 1, MARK = 2, COUNT = 4, };
665         uint32_t overlap = 0;
666         struct priv *priv = dev->data->dev_private;
667
668         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
669                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
670                         continue;
671                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
672                         if (overlap & FATE)
673                                 goto exit_action_overlap;
674                         overlap |= FATE;
675                         parser->drop = 1;
676                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
677                         const struct rte_flow_action_queue *queue =
678                                 (const struct rte_flow_action_queue *)
679                                 actions->conf;
680
681                         if (overlap & FATE)
682                                 goto exit_action_overlap;
683                         overlap |= FATE;
684                         if (!queue || (queue->index > (priv->rxqs_n - 1)))
685                                 goto exit_action_not_supported;
686                         parser->queues[0] = queue->index;
687                         parser->rss_conf = (struct rte_flow_action_rss){
688                                 .queue_num = 1,
689                                 .queue = parser->queues,
690                         };
691                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
692                         const struct rte_flow_action_rss *rss =
693                                 (const struct rte_flow_action_rss *)
694                                 actions->conf;
695                         const uint8_t *rss_key;
696                         uint32_t rss_key_len;
697                         uint16_t n;
698
699                         if (overlap & FATE)
700                                 goto exit_action_overlap;
701                         overlap |= FATE;
702                         if (rss->func &&
703                             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
704                                 rte_flow_error_set(error, EINVAL,
705                                                    RTE_FLOW_ERROR_TYPE_ACTION,
706                                                    actions,
707                                                    "the only supported RSS hash"
708                                                    " function is Toeplitz");
709                                 return -rte_errno;
710                         }
711 #ifndef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
712                         if (parser->rss_conf.level > 1) {
713                                 rte_flow_error_set(error, EINVAL,
714                                                    RTE_FLOW_ERROR_TYPE_ACTION,
715                                                    actions,
716                                                    "a nonzero RSS encapsulation"
717                                                    " level is not supported");
718                                 return -rte_errno;
719                         }
720 #endif
721                         if (parser->rss_conf.level > 2) {
722                                 rte_flow_error_set(error, EINVAL,
723                                                    RTE_FLOW_ERROR_TYPE_ACTION,
724                                                    actions,
725                                                    "RSS encapsulation level"
726                                                    " > 1 is not supported");
727                                 return -rte_errno;
728                         }
729                         if (rss->types & MLX5_RSS_HF_MASK) {
730                                 rte_flow_error_set(error, EINVAL,
731                                                    RTE_FLOW_ERROR_TYPE_ACTION,
732                                                    actions,
733                                                    "unsupported RSS type"
734                                                    " requested");
735                                 return -rte_errno;
736                         }
737                         if (rss->key_len) {
738                                 rss_key_len = rss->key_len;
739                                 rss_key = rss->key;
740                         } else {
741                                 rss_key_len = rss_hash_default_key_len;
742                                 rss_key = rss_hash_default_key;
743                         }
744                         if (rss_key_len != RTE_DIM(parser->rss_key)) {
745                                 rte_flow_error_set(error, EINVAL,
746                                                    RTE_FLOW_ERROR_TYPE_ACTION,
747                                                    actions,
748                                                    "RSS hash key must be"
749                                                    " exactly 40 bytes long");
750                                 return -rte_errno;
751                         }
752                         if (!rss->queue_num) {
753                                 rte_flow_error_set(error, EINVAL,
754                                                    RTE_FLOW_ERROR_TYPE_ACTION,
755                                                    actions,
756                                                    "no valid queues");
757                                 return -rte_errno;
758                         }
759                         if (rss->queue_num > RTE_DIM(parser->queues)) {
760                                 rte_flow_error_set(error, EINVAL,
761                                                    RTE_FLOW_ERROR_TYPE_ACTION,
762                                                    actions,
763                                                    "too many queues for RSS"
764                                                    " context");
765                                 return -rte_errno;
766                         }
767                         for (n = 0; n < rss->queue_num; ++n) {
768                                 if (rss->queue[n] >= priv->rxqs_n) {
769                                         rte_flow_error_set(error, EINVAL,
770                                                    RTE_FLOW_ERROR_TYPE_ACTION,
771                                                    actions,
772                                                    "queue id > number of"
773                                                    " queues");
774                                         return -rte_errno;
775                                 }
776                         }
777                         parser->rss_conf = (struct rte_flow_action_rss){
778                                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
779                                 .level = rss->level,
780                                 .types = rss->types,
781                                 .key_len = rss_key_len,
782                                 .queue_num = rss->queue_num,
783                                 .key = memcpy(parser->rss_key, rss_key,
784                                               sizeof(*rss_key) * rss_key_len),
785                                 .queue = memcpy(parser->queues, rss->queue,
786                                                 sizeof(*rss->queue) *
787                                                 rss->queue_num),
788                         };
789                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_MARK) {
790                         const struct rte_flow_action_mark *mark =
791                                 (const struct rte_flow_action_mark *)
792                                 actions->conf;
793
794                         if (overlap & MARK)
795                                 goto exit_action_overlap;
796                         overlap |= MARK;
797                         if (!mark) {
798                                 rte_flow_error_set(error, EINVAL,
799                                                    RTE_FLOW_ERROR_TYPE_ACTION,
800                                                    actions,
801                                                    "mark must be defined");
802                                 return -rte_errno;
803                         } else if (mark->id >= MLX5_FLOW_MARK_MAX) {
804                                 rte_flow_error_set(error, ENOTSUP,
805                                                    RTE_FLOW_ERROR_TYPE_ACTION,
806                                                    actions,
807                                                    "mark must be between 0"
808                                                    " and 16777199");
809                                 return -rte_errno;
810                         }
811                         parser->mark = 1;
812                         parser->mark_id = mark->id;
813                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_FLAG) {
814                         if (overlap & MARK)
815                                 goto exit_action_overlap;
816                         overlap |= MARK;
817                         parser->mark = 1;
818                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_COUNT &&
819                            priv->config.flow_counter_en) {
820                         if (overlap & COUNT)
821                                 goto exit_action_overlap;
822                         overlap |= COUNT;
823                         parser->count = 1;
824                 } else {
825                         goto exit_action_not_supported;
826                 }
827         }
828         /* When fate is unknown, drop traffic. */
829         if (!(overlap & FATE))
830                 parser->drop = 1;
831         if (parser->drop && parser->mark)
832                 parser->mark = 0;
833         if (!parser->rss_conf.queue_num && !parser->drop) {
834                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
835                                    NULL, "no valid action");
836                 return -rte_errno;
837         }
838         return 0;
839 exit_action_not_supported:
840         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
841                            actions, "action not supported");
842         return -rte_errno;
843 exit_action_overlap:
844         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
845                            actions, "overlapping actions are not supported");
846         return -rte_errno;
847 }
848
849 /**
850  * Validate items.
851  *
852  * @param[in] items
853  *   Pattern specification (list terminated by the END pattern item).
854  * @param[out] error
855  *   Perform verbose error reporting if not NULL.
856  * @param[in, out] parser
857  *   Internal parser structure.
858  *
859  * @return
860  *   0 on success, a negative errno value otherwise and rte_errno is set.
861  */
862 static int
863 mlx5_flow_convert_items_validate(struct rte_eth_dev *dev,
864                                  const struct rte_flow_item items[],
865                                  struct rte_flow_error *error,
866                                  struct mlx5_flow_parse *parser)
867 {
868         struct priv *priv = dev->data->dev_private;
869         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
870         unsigned int i;
871         int ret = 0;
872
873         /* Initialise the offsets to start after verbs attribute. */
874         for (i = 0; i != hash_rxq_init_n; ++i)
875                 parser->queue[i].offset = sizeof(struct ibv_flow_attr);
876         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
877                 const struct mlx5_flow_items *token = NULL;
878                 unsigned int n;
879
880                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
881                         continue;
882                 for (i = 0;
883                      cur_item->items &&
884                      cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
885                      ++i) {
886                         if (cur_item->items[i] == items->type) {
887                                 token = &mlx5_flow_items[items->type];
888                                 break;
889                         }
890                 }
891                 if (!token) {
892                         ret = -ENOTSUP;
893                         goto exit_item_not_supported;
894                 }
895                 cur_item = token;
896                 ret = mlx5_flow_item_validate(items,
897                                               (const uint8_t *)cur_item->mask,
898                                               cur_item->mask_sz);
899                 if (ret)
900                         goto exit_item_not_supported;
901                 if (IS_TUNNEL(items->type)) {
902                         if (parser->tunnel) {
903                                 rte_flow_error_set(error, ENOTSUP,
904                                                    RTE_FLOW_ERROR_TYPE_ITEM,
905                                                    items,
906                                                    "Cannot recognize multiple"
907                                                    " tunnel encapsulations.");
908                                 return -rte_errno;
909                         }
910                         if (!priv->config.tunnel_en &&
911                             parser->rss_conf.level > 1) {
912                                 rte_flow_error_set(error, ENOTSUP,
913                                         RTE_FLOW_ERROR_TYPE_ITEM,
914                                         items,
915                                         "RSS on tunnel is not supported");
916                                 return -rte_errno;
917                         }
918                         parser->inner = IBV_FLOW_SPEC_INNER;
919                         parser->tunnel = flow_ptype[items->type];
920                 }
921                 if (parser->drop) {
922                         parser->queue[HASH_RXQ_ETH].offset += cur_item->dst_sz;
923                 } else {
924                         for (n = 0; n != hash_rxq_init_n; ++n)
925                                 parser->queue[n].offset += cur_item->dst_sz;
926                 }
927         }
928         if (parser->drop) {
929                 parser->queue[HASH_RXQ_ETH].offset +=
930                         sizeof(struct ibv_flow_spec_action_drop);
931         }
932         if (parser->mark) {
933                 for (i = 0; i != hash_rxq_init_n; ++i)
934                         parser->queue[i].offset +=
935                                 sizeof(struct ibv_flow_spec_action_tag);
936         }
937         if (parser->count) {
938                 unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
939
940                 for (i = 0; i != hash_rxq_init_n; ++i)
941                         parser->queue[i].offset += size;
942         }
943         return 0;
944 exit_item_not_supported:
945         return rte_flow_error_set(error, -ret, RTE_FLOW_ERROR_TYPE_ITEM,
946                                   items, "item not supported");
947 }
948
949 /**
950  * Allocate memory space to store verbs flow attributes.
951  *
952  * @param[in] size
953  *   Amount of byte to allocate.
954  * @param[out] error
955  *   Perform verbose error reporting if not NULL.
956  *
957  * @return
958  *   A verbs flow attribute on success, NULL otherwise and rte_errno is set.
959  */
960 static struct ibv_flow_attr *
961 mlx5_flow_convert_allocate(unsigned int size, struct rte_flow_error *error)
962 {
963         struct ibv_flow_attr *ibv_attr;
964
965         ibv_attr = rte_calloc(__func__, 1, size, 0);
966         if (!ibv_attr) {
967                 rte_flow_error_set(error, ENOMEM,
968                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
969                                    NULL,
970                                    "cannot allocate verbs spec attributes");
971                 return NULL;
972         }
973         return ibv_attr;
974 }
975
976 /**
977  * Make inner packet matching with an higher priority from the non Inner
978  * matching.
979  *
980  * @param dev
981  *   Pointer to Ethernet device.
982  * @param[in, out] parser
983  *   Internal parser structure.
984  * @param attr
985  *   User flow attribute.
986  */
987 static void
988 mlx5_flow_update_priority(struct rte_eth_dev *dev,
989                           struct mlx5_flow_parse *parser,
990                           const struct rte_flow_attr *attr)
991 {
992         struct priv *priv = dev->data->dev_private;
993         unsigned int i;
994         uint16_t priority;
995
996         /*                      8 priorities    >= 16 priorities
997          * Control flow:        4-7             8-15
998          * User normal flow:    1-3             4-7
999          * User tunnel flow:    0-2             0-3
1000          */
1001         priority = attr->priority * MLX5_VERBS_FLOW_PRIO_8;
1002         if (priv->config.max_verbs_prio == MLX5_VERBS_FLOW_PRIO_8)
1003                 priority /= 2;
1004         /*
1005          * Lower non-tunnel flow Verbs priority 1 if only support 8 Verbs
1006          * priorities, lower 4 otherwise.
1007          */
1008         if (!parser->inner) {
1009                 if (priv->config.max_verbs_prio == MLX5_VERBS_FLOW_PRIO_8)
1010                         priority += 1;
1011                 else
1012                         priority += MLX5_VERBS_FLOW_PRIO_8 / 2;
1013         }
1014         if (parser->drop) {
1015                 parser->queue[HASH_RXQ_ETH].ibv_attr->priority = priority +
1016                                 hash_rxq_init[HASH_RXQ_ETH].flow_priority;
1017                 return;
1018         }
1019         for (i = 0; i != hash_rxq_init_n; ++i) {
1020                 if (!parser->queue[i].ibv_attr)
1021                         continue;
1022                 parser->queue[i].ibv_attr->priority = priority +
1023                                 hash_rxq_init[i].flow_priority;
1024         }
1025 }
1026
1027 /**
1028  * Finalise verbs flow attributes.
1029  *
1030  * @param[in, out] parser
1031  *   Internal parser structure.
1032  */
1033 static void
1034 mlx5_flow_convert_finalise(struct mlx5_flow_parse *parser)
1035 {
1036         unsigned int i;
1037         uint32_t inner = parser->inner;
1038
1039         /* Don't create extra flows for outer RSS. */
1040         if (parser->tunnel && parser->rss_conf.level < 2)
1041                 return;
1042         /*
1043          * Fill missing layers in verbs specifications, or compute the correct
1044          * offset to allocate the memory space for the attributes and
1045          * specifications.
1046          */
1047         for (i = 0; i != hash_rxq_init_n - 1; ++i) {
1048                 union {
1049                         struct ibv_flow_spec_ipv4_ext ipv4;
1050                         struct ibv_flow_spec_ipv6 ipv6;
1051                         struct ibv_flow_spec_tcp_udp udp_tcp;
1052                         struct ibv_flow_spec_eth eth;
1053                 } specs;
1054                 void *dst;
1055                 uint16_t size;
1056
1057                 if (i == parser->layer)
1058                         continue;
1059                 if (parser->layer == HASH_RXQ_ETH ||
1060                     parser->layer == HASH_RXQ_TUNNEL) {
1061                         if (hash_rxq_init[i].ip_version == MLX5_IPV4) {
1062                                 size = sizeof(struct ibv_flow_spec_ipv4_ext);
1063                                 specs.ipv4 = (struct ibv_flow_spec_ipv4_ext){
1064                                         .type = inner | IBV_FLOW_SPEC_IPV4_EXT,
1065                                         .size = size,
1066                                 };
1067                         } else {
1068                                 size = sizeof(struct ibv_flow_spec_ipv6);
1069                                 specs.ipv6 = (struct ibv_flow_spec_ipv6){
1070                                         .type = inner | IBV_FLOW_SPEC_IPV6,
1071                                         .size = size,
1072                                 };
1073                         }
1074                         if (parser->queue[i].ibv_attr) {
1075                                 dst = (void *)((uintptr_t)
1076                                                parser->queue[i].ibv_attr +
1077                                                parser->queue[i].offset);
1078                                 memcpy(dst, &specs, size);
1079                                 ++parser->queue[i].ibv_attr->num_of_specs;
1080                         }
1081                         parser->queue[i].offset += size;
1082                 }
1083                 if ((i == HASH_RXQ_UDPV4) || (i == HASH_RXQ_TCPV4) ||
1084                     (i == HASH_RXQ_UDPV6) || (i == HASH_RXQ_TCPV6)) {
1085                         size = sizeof(struct ibv_flow_spec_tcp_udp);
1086                         specs.udp_tcp = (struct ibv_flow_spec_tcp_udp) {
1087                                 .type = inner | ((i == HASH_RXQ_UDPV4 ||
1088                                           i == HASH_RXQ_UDPV6) ?
1089                                          IBV_FLOW_SPEC_UDP :
1090                                          IBV_FLOW_SPEC_TCP),
1091                                 .size = size,
1092                         };
1093                         if (parser->queue[i].ibv_attr) {
1094                                 dst = (void *)((uintptr_t)
1095                                                parser->queue[i].ibv_attr +
1096                                                parser->queue[i].offset);
1097                                 memcpy(dst, &specs, size);
1098                                 ++parser->queue[i].ibv_attr->num_of_specs;
1099                         }
1100                         parser->queue[i].offset += size;
1101                 }
1102         }
1103 }
1104
1105 /**
1106  * Update flows according to pattern and RSS hash fields.
1107  *
1108  * @param[in, out] parser
1109  *   Internal parser structure.
1110  *
1111  * @return
1112  *   0 on success, a negative errno value otherwise and rte_errno is set.
1113  */
1114 static int
1115 mlx5_flow_convert_rss(struct mlx5_flow_parse *parser)
1116 {
1117         unsigned int i;
1118         enum hash_rxq_type start;
1119         enum hash_rxq_type layer;
1120         int outer = parser->tunnel && parser->rss_conf.level < 2;
1121         uint64_t rss = parser->rss_conf.types;
1122
1123         /* Default to outer RSS. */
1124         if (!parser->rss_conf.level)
1125                 parser->rss_conf.level = 1;
1126         layer = outer ? parser->out_layer : parser->layer;
1127         if (layer == HASH_RXQ_TUNNEL)
1128                 layer = HASH_RXQ_ETH;
1129         if (outer) {
1130                 /* Only one hash type for outer RSS. */
1131                 if (rss && layer == HASH_RXQ_ETH) {
1132                         start = HASH_RXQ_TCPV4;
1133                 } else if (rss && layer != HASH_RXQ_ETH &&
1134                            !(rss & hash_rxq_init[layer].dpdk_rss_hf)) {
1135                         /* If RSS not match L4 pattern, try L3 RSS. */
1136                         if (layer < HASH_RXQ_IPV4)
1137                                 layer = HASH_RXQ_IPV4;
1138                         else if (layer > HASH_RXQ_IPV4 && layer < HASH_RXQ_IPV6)
1139                                 layer = HASH_RXQ_IPV6;
1140                         start = layer;
1141                 } else {
1142                         start = layer;
1143                 }
1144                 /* Scan first valid hash type. */
1145                 for (i = start; rss && i <= layer; ++i) {
1146                         if (!parser->queue[i].ibv_attr)
1147                                 continue;
1148                         if (hash_rxq_init[i].dpdk_rss_hf & rss)
1149                                 break;
1150                 }
1151                 if (rss && i <= layer)
1152                         parser->queue[layer].hash_fields =
1153                                         hash_rxq_init[i].hash_fields;
1154                 /* Trim unused hash types. */
1155                 for (i = 0; i != hash_rxq_init_n; ++i) {
1156                         if (parser->queue[i].ibv_attr && i != layer) {
1157                                 rte_free(parser->queue[i].ibv_attr);
1158                                 parser->queue[i].ibv_attr = NULL;
1159                         }
1160                 }
1161         } else {
1162                 /* Expand for inner or normal RSS. */
1163                 if (rss && (layer == HASH_RXQ_ETH || layer == HASH_RXQ_IPV4))
1164                         start = HASH_RXQ_TCPV4;
1165                 else if (rss && layer == HASH_RXQ_IPV6)
1166                         start = HASH_RXQ_TCPV6;
1167                 else
1168                         start = layer;
1169                 /* For L4 pattern, try L3 RSS if no L4 RSS. */
1170                 /* Trim unused hash types. */
1171                 for (i = 0; i != hash_rxq_init_n; ++i) {
1172                         if (!parser->queue[i].ibv_attr)
1173                                 continue;
1174                         if (i < start || i > layer) {
1175                                 rte_free(parser->queue[i].ibv_attr);
1176                                 parser->queue[i].ibv_attr = NULL;
1177                                 continue;
1178                         }
1179                         if (!rss)
1180                                 continue;
1181                         if (hash_rxq_init[i].dpdk_rss_hf & rss) {
1182                                 parser->queue[i].hash_fields =
1183                                                 hash_rxq_init[i].hash_fields;
1184                         } else if (i != layer) {
1185                                 /* Remove unused RSS expansion. */
1186                                 rte_free(parser->queue[i].ibv_attr);
1187                                 parser->queue[i].ibv_attr = NULL;
1188                         } else if (layer < HASH_RXQ_IPV4 &&
1189                                    (hash_rxq_init[HASH_RXQ_IPV4].dpdk_rss_hf &
1190                                     rss)) {
1191                                 /* Allow IPv4 RSS on L4 pattern. */
1192                                 parser->queue[i].hash_fields =
1193                                         hash_rxq_init[HASH_RXQ_IPV4]
1194                                                 .hash_fields;
1195                         } else if (i > HASH_RXQ_IPV4 && i < HASH_RXQ_IPV6 &&
1196                                    (hash_rxq_init[HASH_RXQ_IPV6].dpdk_rss_hf &
1197                                     rss)) {
1198                                 /* Allow IPv4 RSS on L4 pattern. */
1199                                 parser->queue[i].hash_fields =
1200                                         hash_rxq_init[HASH_RXQ_IPV6]
1201                                                 .hash_fields;
1202                         }
1203                 }
1204         }
1205         return 0;
1206 }
1207
1208 /**
1209  * Validate and convert a flow supported by the NIC.
1210  *
1211  * @param dev
1212  *   Pointer to Ethernet device.
1213  * @param[in] attr
1214  *   Flow rule attributes.
1215  * @param[in] pattern
1216  *   Pattern specification (list terminated by the END pattern item).
1217  * @param[in] actions
1218  *   Associated actions (list terminated by the END action).
1219  * @param[out] error
1220  *   Perform verbose error reporting if not NULL.
1221  * @param[in, out] parser
1222  *   Internal parser structure.
1223  *
1224  * @return
1225  *   0 on success, a negative errno value otherwise and rte_errno is set.
1226  */
1227 static int
1228 mlx5_flow_convert(struct rte_eth_dev *dev,
1229                   const struct rte_flow_attr *attr,
1230                   const struct rte_flow_item items[],
1231                   const struct rte_flow_action actions[],
1232                   struct rte_flow_error *error,
1233                   struct mlx5_flow_parse *parser)
1234 {
1235         const struct mlx5_flow_items *cur_item = mlx5_flow_items;
1236         unsigned int i;
1237         int ret;
1238
1239         /* First step. Validate the attributes, items and actions. */
1240         *parser = (struct mlx5_flow_parse){
1241                 .create = parser->create,
1242                 .layer = HASH_RXQ_ETH,
1243                 .mark_id = MLX5_FLOW_MARK_DEFAULT,
1244         };
1245         ret = mlx5_flow_convert_attributes(attr, error);
1246         if (ret)
1247                 return ret;
1248         ret = mlx5_flow_convert_actions(dev, actions, error, parser);
1249         if (ret)
1250                 return ret;
1251         ret = mlx5_flow_convert_items_validate(dev, items, error, parser);
1252         if (ret)
1253                 return ret;
1254         mlx5_flow_convert_finalise(parser);
1255         /*
1256          * Second step.
1257          * Allocate the memory space to store verbs specifications.
1258          */
1259         if (parser->drop) {
1260                 unsigned int offset = parser->queue[HASH_RXQ_ETH].offset;
1261
1262                 parser->queue[HASH_RXQ_ETH].ibv_attr =
1263                         mlx5_flow_convert_allocate(offset, error);
1264                 if (!parser->queue[HASH_RXQ_ETH].ibv_attr)
1265                         goto exit_enomem;
1266                 parser->queue[HASH_RXQ_ETH].offset =
1267                         sizeof(struct ibv_flow_attr);
1268         } else {
1269                 for (i = 0; i != hash_rxq_init_n; ++i) {
1270                         unsigned int offset;
1271
1272                         offset = parser->queue[i].offset;
1273                         parser->queue[i].ibv_attr =
1274                                 mlx5_flow_convert_allocate(offset, error);
1275                         if (!parser->queue[i].ibv_attr)
1276                                 goto exit_enomem;
1277                         parser->queue[i].offset = sizeof(struct ibv_flow_attr);
1278                 }
1279         }
1280         /* Third step. Conversion parse, fill the specifications. */
1281         parser->inner = 0;
1282         parser->tunnel = 0;
1283         parser->layer = HASH_RXQ_ETH;
1284         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1285                 struct mlx5_flow_data data = {
1286                         .dev = dev,
1287                         .parser = parser,
1288                         .error = error,
1289                 };
1290
1291                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1292                         continue;
1293                 cur_item = &mlx5_flow_items[items->type];
1294                 ret = cur_item->convert(items,
1295                                         (cur_item->default_mask ?
1296                                          cur_item->default_mask :
1297                                          cur_item->mask),
1298                                          &data);
1299                 if (ret)
1300                         goto exit_free;
1301         }
1302         if (!parser->drop) {
1303                 /* RSS check, remove unused hash types. */
1304                 ret = mlx5_flow_convert_rss(parser);
1305                 if (ret)
1306                         goto exit_free;
1307                 /* Complete missing specification. */
1308                 mlx5_flow_convert_finalise(parser);
1309         }
1310         mlx5_flow_update_priority(dev, parser, attr);
1311         if (parser->mark)
1312                 mlx5_flow_create_flag_mark(parser, parser->mark_id);
1313         if (parser->count && parser->create) {
1314                 mlx5_flow_create_count(dev, parser);
1315                 if (!parser->cs)
1316                         goto exit_count_error;
1317         }
1318 exit_free:
1319         /* Only verification is expected, all resources should be released. */
1320         if (!parser->create) {
1321                 for (i = 0; i != hash_rxq_init_n; ++i) {
1322                         if (parser->queue[i].ibv_attr) {
1323                                 rte_free(parser->queue[i].ibv_attr);
1324                                 parser->queue[i].ibv_attr = NULL;
1325                         }
1326                 }
1327         }
1328         return ret;
1329 exit_enomem:
1330         for (i = 0; i != hash_rxq_init_n; ++i) {
1331                 if (parser->queue[i].ibv_attr) {
1332                         rte_free(parser->queue[i].ibv_attr);
1333                         parser->queue[i].ibv_attr = NULL;
1334                 }
1335         }
1336         rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1337                            NULL, "cannot allocate verbs spec attributes");
1338         return -rte_errno;
1339 exit_count_error:
1340         rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1341                            NULL, "cannot create counter");
1342         return -rte_errno;
1343 }
1344
1345 /**
1346  * Copy the specification created into the flow.
1347  *
1348  * @param parser
1349  *   Internal parser structure.
1350  * @param src
1351  *   Create specification.
1352  * @param size
1353  *   Size in bytes of the specification to copy.
1354  */
1355 static void
1356 mlx5_flow_create_copy(struct mlx5_flow_parse *parser, void *src,
1357                       unsigned int size)
1358 {
1359         unsigned int i;
1360         void *dst;
1361
1362         for (i = 0; i != hash_rxq_init_n; ++i) {
1363                 if (!parser->queue[i].ibv_attr)
1364                         continue;
1365                 dst = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1366                                 parser->queue[i].offset);
1367                 memcpy(dst, src, size);
1368                 ++parser->queue[i].ibv_attr->num_of_specs;
1369                 parser->queue[i].offset += size;
1370         }
1371 }
1372
1373 /**
1374  * Convert Ethernet item to Verbs specification.
1375  *
1376  * @param item[in]
1377  *   Item specification.
1378  * @param default_mask[in]
1379  *   Default bit-masks to use when item->mask is not provided.
1380  * @param data[in, out]
1381  *   User structure.
1382  *
1383  * @return
1384  *   0 on success, a negative errno value otherwise and rte_errno is set.
1385  */
1386 static int
1387 mlx5_flow_create_eth(const struct rte_flow_item *item,
1388                      const void *default_mask,
1389                      struct mlx5_flow_data *data)
1390 {
1391         const struct rte_flow_item_eth *spec = item->spec;
1392         const struct rte_flow_item_eth *mask = item->mask;
1393         struct mlx5_flow_parse *parser = data->parser;
1394         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1395         struct ibv_flow_spec_eth eth = {
1396                 .type = parser->inner | IBV_FLOW_SPEC_ETH,
1397                 .size = eth_size,
1398         };
1399
1400         parser->layer = HASH_RXQ_ETH;
1401         if (spec) {
1402                 unsigned int i;
1403
1404                 if (!mask)
1405                         mask = default_mask;
1406                 memcpy(&eth.val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
1407                 memcpy(&eth.val.src_mac, spec->src.addr_bytes, ETHER_ADDR_LEN);
1408                 eth.val.ether_type = spec->type;
1409                 memcpy(&eth.mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
1410                 memcpy(&eth.mask.src_mac, mask->src.addr_bytes, ETHER_ADDR_LEN);
1411                 eth.mask.ether_type = mask->type;
1412                 /* Remove unwanted bits from values. */
1413                 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
1414                         eth.val.dst_mac[i] &= eth.mask.dst_mac[i];
1415                         eth.val.src_mac[i] &= eth.mask.src_mac[i];
1416                 }
1417                 eth.val.ether_type &= eth.mask.ether_type;
1418         }
1419         mlx5_flow_create_copy(parser, &eth, eth_size);
1420         return 0;
1421 }
1422
1423 /**
1424  * Convert VLAN item to Verbs specification.
1425  *
1426  * @param item[in]
1427  *   Item specification.
1428  * @param default_mask[in]
1429  *   Default bit-masks to use when item->mask is not provided.
1430  * @param data[in, out]
1431  *   User structure.
1432  *
1433  * @return
1434  *   0 on success, a negative errno value otherwise and rte_errno is set.
1435  */
1436 static int
1437 mlx5_flow_create_vlan(const struct rte_flow_item *item,
1438                       const void *default_mask,
1439                       struct mlx5_flow_data *data)
1440 {
1441         const struct rte_flow_item_vlan *spec = item->spec;
1442         const struct rte_flow_item_vlan *mask = item->mask;
1443         struct mlx5_flow_parse *parser = data->parser;
1444         struct ibv_flow_spec_eth *eth;
1445         const unsigned int eth_size = sizeof(struct ibv_flow_spec_eth);
1446         const char *msg = "VLAN cannot be empty";
1447
1448         if (spec) {
1449                 unsigned int i;
1450                 if (!mask)
1451                         mask = default_mask;
1452
1453                 for (i = 0; i != hash_rxq_init_n; ++i) {
1454                         if (!parser->queue[i].ibv_attr)
1455                                 continue;
1456
1457                         eth = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1458                                        parser->queue[i].offset - eth_size);
1459                         eth->val.vlan_tag = spec->tci;
1460                         eth->mask.vlan_tag = mask->tci;
1461                         eth->val.vlan_tag &= eth->mask.vlan_tag;
1462                         /*
1463                          * From verbs perspective an empty VLAN is equivalent
1464                          * to a packet without VLAN layer.
1465                          */
1466                         if (!eth->mask.vlan_tag)
1467                                 goto error;
1468                         /* Outer TPID cannot be matched. */
1469                         if (eth->mask.ether_type) {
1470                                 msg = "VLAN TPID matching is not supported";
1471                                 goto error;
1472                         }
1473                         eth->val.ether_type = spec->inner_type;
1474                         eth->mask.ether_type = mask->inner_type;
1475                         eth->val.ether_type &= eth->mask.ether_type;
1476                 }
1477                 return 0;
1478         }
1479 error:
1480         return rte_flow_error_set(data->error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
1481                                   item, msg);
1482 }
1483
1484 /**
1485  * Convert IPv4 item to Verbs specification.
1486  *
1487  * @param item[in]
1488  *   Item specification.
1489  * @param default_mask[in]
1490  *   Default bit-masks to use when item->mask is not provided.
1491  * @param data[in, out]
1492  *   User structure.
1493  *
1494  * @return
1495  *   0 on success, a negative errno value otherwise and rte_errno is set.
1496  */
1497 static int
1498 mlx5_flow_create_ipv4(const struct rte_flow_item *item,
1499                       const void *default_mask,
1500                       struct mlx5_flow_data *data)
1501 {
1502         struct priv *priv = data->dev->data->dev_private;
1503         const struct rte_flow_item_ipv4 *spec = item->spec;
1504         const struct rte_flow_item_ipv4 *mask = item->mask;
1505         struct mlx5_flow_parse *parser = data->parser;
1506         unsigned int ipv4_size = sizeof(struct ibv_flow_spec_ipv4_ext);
1507         struct ibv_flow_spec_ipv4_ext ipv4 = {
1508                 .type = parser->inner | IBV_FLOW_SPEC_IPV4_EXT,
1509                 .size = ipv4_size,
1510         };
1511
1512         if (parser->layer == HASH_RXQ_TUNNEL &&
1513             parser->tunnel == ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)] &&
1514             !priv->config.l3_vxlan_en)
1515                 return rte_flow_error_set(data->error, EINVAL,
1516                                           RTE_FLOW_ERROR_TYPE_ITEM,
1517                                           item,
1518                                           "L3 VXLAN not enabled by device"
1519                                           " parameter and/or not configured"
1520                                           " in firmware");
1521         parser->layer = HASH_RXQ_IPV4;
1522         if (spec) {
1523                 if (!mask)
1524                         mask = default_mask;
1525                 ipv4.val = (struct ibv_flow_ipv4_ext_filter){
1526                         .src_ip = spec->hdr.src_addr,
1527                         .dst_ip = spec->hdr.dst_addr,
1528                         .proto = spec->hdr.next_proto_id,
1529                         .tos = spec->hdr.type_of_service,
1530                 };
1531                 ipv4.mask = (struct ibv_flow_ipv4_ext_filter){
1532                         .src_ip = mask->hdr.src_addr,
1533                         .dst_ip = mask->hdr.dst_addr,
1534                         .proto = mask->hdr.next_proto_id,
1535                         .tos = mask->hdr.type_of_service,
1536                 };
1537                 /* Remove unwanted bits from values. */
1538                 ipv4.val.src_ip &= ipv4.mask.src_ip;
1539                 ipv4.val.dst_ip &= ipv4.mask.dst_ip;
1540                 ipv4.val.proto &= ipv4.mask.proto;
1541                 ipv4.val.tos &= ipv4.mask.tos;
1542         }
1543         mlx5_flow_create_copy(parser, &ipv4, ipv4_size);
1544         return 0;
1545 }
1546
1547 /**
1548  * Convert IPv6 item to Verbs specification.
1549  *
1550  * @param item[in]
1551  *   Item specification.
1552  * @param default_mask[in]
1553  *   Default bit-masks to use when item->mask is not provided.
1554  * @param data[in, out]
1555  *   User structure.
1556  *
1557  * @return
1558  *   0 on success, a negative errno value otherwise and rte_errno is set.
1559  */
1560 static int
1561 mlx5_flow_create_ipv6(const struct rte_flow_item *item,
1562                       const void *default_mask,
1563                       struct mlx5_flow_data *data)
1564 {
1565         struct priv *priv = data->dev->data->dev_private;
1566         const struct rte_flow_item_ipv6 *spec = item->spec;
1567         const struct rte_flow_item_ipv6 *mask = item->mask;
1568         struct mlx5_flow_parse *parser = data->parser;
1569         unsigned int ipv6_size = sizeof(struct ibv_flow_spec_ipv6);
1570         struct ibv_flow_spec_ipv6 ipv6 = {
1571                 .type = parser->inner | IBV_FLOW_SPEC_IPV6,
1572                 .size = ipv6_size,
1573         };
1574
1575         if (parser->layer == HASH_RXQ_TUNNEL &&
1576             parser->tunnel == ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)] &&
1577             !priv->config.l3_vxlan_en)
1578                 return rte_flow_error_set(data->error, EINVAL,
1579                                           RTE_FLOW_ERROR_TYPE_ITEM,
1580                                           item,
1581                                           "L3 VXLAN not enabled by device"
1582                                           " parameter and/or not configured"
1583                                           " in firmware");
1584         parser->layer = HASH_RXQ_IPV6;
1585         if (spec) {
1586                 unsigned int i;
1587                 uint32_t vtc_flow_val;
1588                 uint32_t vtc_flow_mask;
1589
1590                 if (!mask)
1591                         mask = default_mask;
1592                 memcpy(&ipv6.val.src_ip, spec->hdr.src_addr,
1593                        RTE_DIM(ipv6.val.src_ip));
1594                 memcpy(&ipv6.val.dst_ip, spec->hdr.dst_addr,
1595                        RTE_DIM(ipv6.val.dst_ip));
1596                 memcpy(&ipv6.mask.src_ip, mask->hdr.src_addr,
1597                        RTE_DIM(ipv6.mask.src_ip));
1598                 memcpy(&ipv6.mask.dst_ip, mask->hdr.dst_addr,
1599                        RTE_DIM(ipv6.mask.dst_ip));
1600                 vtc_flow_val = rte_be_to_cpu_32(spec->hdr.vtc_flow);
1601                 vtc_flow_mask = rte_be_to_cpu_32(mask->hdr.vtc_flow);
1602                 ipv6.val.flow_label =
1603                         rte_cpu_to_be_32((vtc_flow_val & IPV6_HDR_FL_MASK) >>
1604                                          IPV6_HDR_FL_SHIFT);
1605                 ipv6.val.traffic_class = (vtc_flow_val & IPV6_HDR_TC_MASK) >>
1606                                          IPV6_HDR_TC_SHIFT;
1607                 ipv6.val.next_hdr = spec->hdr.proto;
1608                 ipv6.val.hop_limit = spec->hdr.hop_limits;
1609                 ipv6.mask.flow_label =
1610                         rte_cpu_to_be_32((vtc_flow_mask & IPV6_HDR_FL_MASK) >>
1611                                          IPV6_HDR_FL_SHIFT);
1612                 ipv6.mask.traffic_class = (vtc_flow_mask & IPV6_HDR_TC_MASK) >>
1613                                           IPV6_HDR_TC_SHIFT;
1614                 ipv6.mask.next_hdr = mask->hdr.proto;
1615                 ipv6.mask.hop_limit = mask->hdr.hop_limits;
1616                 /* Remove unwanted bits from values. */
1617                 for (i = 0; i < RTE_DIM(ipv6.val.src_ip); ++i) {
1618                         ipv6.val.src_ip[i] &= ipv6.mask.src_ip[i];
1619                         ipv6.val.dst_ip[i] &= ipv6.mask.dst_ip[i];
1620                 }
1621                 ipv6.val.flow_label &= ipv6.mask.flow_label;
1622                 ipv6.val.traffic_class &= ipv6.mask.traffic_class;
1623                 ipv6.val.next_hdr &= ipv6.mask.next_hdr;
1624                 ipv6.val.hop_limit &= ipv6.mask.hop_limit;
1625         }
1626         mlx5_flow_create_copy(parser, &ipv6, ipv6_size);
1627         return 0;
1628 }
1629
1630 /**
1631  * Convert UDP item to Verbs specification.
1632  *
1633  * @param item[in]
1634  *   Item specification.
1635  * @param default_mask[in]
1636  *   Default bit-masks to use when item->mask is not provided.
1637  * @param data[in, out]
1638  *   User structure.
1639  *
1640  * @return
1641  *   0 on success, a negative errno value otherwise and rte_errno is set.
1642  */
1643 static int
1644 mlx5_flow_create_udp(const struct rte_flow_item *item,
1645                      const void *default_mask,
1646                      struct mlx5_flow_data *data)
1647 {
1648         const struct rte_flow_item_udp *spec = item->spec;
1649         const struct rte_flow_item_udp *mask = item->mask;
1650         struct mlx5_flow_parse *parser = data->parser;
1651         unsigned int udp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1652         struct ibv_flow_spec_tcp_udp udp = {
1653                 .type = parser->inner | IBV_FLOW_SPEC_UDP,
1654                 .size = udp_size,
1655         };
1656
1657         if (parser->layer == HASH_RXQ_IPV4)
1658                 parser->layer = HASH_RXQ_UDPV4;
1659         else
1660                 parser->layer = HASH_RXQ_UDPV6;
1661         if (spec) {
1662                 if (!mask)
1663                         mask = default_mask;
1664                 udp.val.dst_port = spec->hdr.dst_port;
1665                 udp.val.src_port = spec->hdr.src_port;
1666                 udp.mask.dst_port = mask->hdr.dst_port;
1667                 udp.mask.src_port = mask->hdr.src_port;
1668                 /* Remove unwanted bits from values. */
1669                 udp.val.src_port &= udp.mask.src_port;
1670                 udp.val.dst_port &= udp.mask.dst_port;
1671         }
1672         mlx5_flow_create_copy(parser, &udp, udp_size);
1673         return 0;
1674 }
1675
1676 /**
1677  * Convert TCP item to Verbs specification.
1678  *
1679  * @param item[in]
1680  *   Item specification.
1681  * @param default_mask[in]
1682  *   Default bit-masks to use when item->mask is not provided.
1683  * @param data[in, out]
1684  *   User structure.
1685  *
1686  * @return
1687  *   0 on success, a negative errno value otherwise and rte_errno is set.
1688  */
1689 static int
1690 mlx5_flow_create_tcp(const struct rte_flow_item *item,
1691                      const void *default_mask,
1692                      struct mlx5_flow_data *data)
1693 {
1694         const struct rte_flow_item_tcp *spec = item->spec;
1695         const struct rte_flow_item_tcp *mask = item->mask;
1696         struct mlx5_flow_parse *parser = data->parser;
1697         unsigned int tcp_size = sizeof(struct ibv_flow_spec_tcp_udp);
1698         struct ibv_flow_spec_tcp_udp tcp = {
1699                 .type = parser->inner | IBV_FLOW_SPEC_TCP,
1700                 .size = tcp_size,
1701         };
1702
1703         if (parser->layer == HASH_RXQ_IPV4)
1704                 parser->layer = HASH_RXQ_TCPV4;
1705         else
1706                 parser->layer = HASH_RXQ_TCPV6;
1707         if (spec) {
1708                 if (!mask)
1709                         mask = default_mask;
1710                 tcp.val.dst_port = spec->hdr.dst_port;
1711                 tcp.val.src_port = spec->hdr.src_port;
1712                 tcp.mask.dst_port = mask->hdr.dst_port;
1713                 tcp.mask.src_port = mask->hdr.src_port;
1714                 /* Remove unwanted bits from values. */
1715                 tcp.val.src_port &= tcp.mask.src_port;
1716                 tcp.val.dst_port &= tcp.mask.dst_port;
1717         }
1718         mlx5_flow_create_copy(parser, &tcp, tcp_size);
1719         return 0;
1720 }
1721
1722 /**
1723  * Convert VXLAN item to Verbs specification.
1724  *
1725  * @param item[in]
1726  *   Item specification.
1727  * @param default_mask[in]
1728  *   Default bit-masks to use when item->mask is not provided.
1729  * @param data[in, out]
1730  *   User structure.
1731  *
1732  * @return
1733  *   0 on success, a negative errno value otherwise and rte_errno is set.
1734  */
1735 static int
1736 mlx5_flow_create_vxlan(const struct rte_flow_item *item,
1737                        const void *default_mask,
1738                        struct mlx5_flow_data *data)
1739 {
1740         const struct rte_flow_item_vxlan *spec = item->spec;
1741         const struct rte_flow_item_vxlan *mask = item->mask;
1742         struct mlx5_flow_parse *parser = data->parser;
1743         unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1744         struct ibv_flow_spec_tunnel vxlan = {
1745                 .type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1746                 .size = size,
1747         };
1748         union vni {
1749                 uint32_t vlan_id;
1750                 uint8_t vni[4];
1751         } id;
1752
1753         id.vni[0] = 0;
1754         parser->inner = IBV_FLOW_SPEC_INNER;
1755         parser->tunnel = ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_VXLAN)];
1756         parser->out_layer = parser->layer;
1757         parser->layer = HASH_RXQ_TUNNEL;
1758         /* Default VXLAN to outer RSS. */
1759         if (!parser->rss_conf.level)
1760                 parser->rss_conf.level = 1;
1761         if (spec) {
1762                 if (!mask)
1763                         mask = default_mask;
1764                 memcpy(&id.vni[1], spec->vni, 3);
1765                 vxlan.val.tunnel_id = id.vlan_id;
1766                 memcpy(&id.vni[1], mask->vni, 3);
1767                 vxlan.mask.tunnel_id = id.vlan_id;
1768                 /* Remove unwanted bits from values. */
1769                 vxlan.val.tunnel_id &= vxlan.mask.tunnel_id;
1770         }
1771         /*
1772          * Tunnel id 0 is equivalent as not adding a VXLAN layer, if only this
1773          * layer is defined in the Verbs specification it is interpreted as
1774          * wildcard and all packets will match this rule, if it follows a full
1775          * stack layer (ex: eth / ipv4 / udp), all packets matching the layers
1776          * before will also match this rule.
1777          * To avoid such situation, VNI 0 is currently refused.
1778          */
1779         if (!vxlan.val.tunnel_id)
1780                 return rte_flow_error_set(data->error, EINVAL,
1781                                           RTE_FLOW_ERROR_TYPE_ITEM,
1782                                           item,
1783                                           "VxLAN vni cannot be 0");
1784         mlx5_flow_create_copy(parser, &vxlan, size);
1785         return 0;
1786 }
1787
1788 /**
1789  * Convert GRE item to Verbs specification.
1790  *
1791  * @param item[in]
1792  *   Item specification.
1793  * @param default_mask[in]
1794  *   Default bit-masks to use when item->mask is not provided.
1795  * @param data[in, out]
1796  *   User structure.
1797  *
1798  * @return
1799  *   0 on success, a negative errno value otherwise and rte_errno is set.
1800  */
1801 static int
1802 mlx5_flow_create_gre(const struct rte_flow_item *item __rte_unused,
1803                      const void *default_mask __rte_unused,
1804                      struct mlx5_flow_data *data)
1805 {
1806         struct mlx5_flow_parse *parser = data->parser;
1807         unsigned int size = sizeof(struct ibv_flow_spec_tunnel);
1808         struct ibv_flow_spec_tunnel tunnel = {
1809                 .type = parser->inner | IBV_FLOW_SPEC_VXLAN_TUNNEL,
1810                 .size = size,
1811         };
1812         struct ibv_flow_spec_ipv4_ext *ipv4;
1813         struct ibv_flow_spec_ipv6 *ipv6;
1814         unsigned int i;
1815
1816         parser->inner = IBV_FLOW_SPEC_INNER;
1817         parser->tunnel = ptype_ext[PTYPE_IDX(RTE_PTYPE_TUNNEL_GRE)];
1818         parser->out_layer = parser->layer;
1819         parser->layer = HASH_RXQ_TUNNEL;
1820         /* Default GRE to inner RSS. */
1821         if (!parser->rss_conf.level)
1822                 parser->rss_conf.level = 2;
1823         /* Update encapsulation IP layer protocol. */
1824         for (i = 0; i != hash_rxq_init_n; ++i) {
1825                 if (!parser->queue[i].ibv_attr)
1826                         continue;
1827                 if (parser->out_layer == HASH_RXQ_IPV4) {
1828                         ipv4 = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1829                                 parser->queue[i].offset -
1830                                 sizeof(struct ibv_flow_spec_ipv4_ext));
1831                         if (ipv4->mask.proto && ipv4->val.proto != MLX5_GRE)
1832                                 break;
1833                         ipv4->val.proto = MLX5_GRE;
1834                         ipv4->mask.proto = 0xff;
1835                 } else if (parser->out_layer == HASH_RXQ_IPV6) {
1836                         ipv6 = (void *)((uintptr_t)parser->queue[i].ibv_attr +
1837                                 parser->queue[i].offset -
1838                                 sizeof(struct ibv_flow_spec_ipv6));
1839                         if (ipv6->mask.next_hdr &&
1840                             ipv6->val.next_hdr != MLX5_GRE)
1841                                 break;
1842                         ipv6->val.next_hdr = MLX5_GRE;
1843                         ipv6->mask.next_hdr = 0xff;
1844                 }
1845         }
1846         if (i != hash_rxq_init_n)
1847                 return rte_flow_error_set(data->error, EINVAL,
1848                                           RTE_FLOW_ERROR_TYPE_ITEM,
1849                                           item,
1850                                           "IP protocol of GRE must be 47");
1851         mlx5_flow_create_copy(parser, &tunnel, size);
1852         return 0;
1853 }
1854
1855 /**
1856  * Convert mark/flag action to Verbs specification.
1857  *
1858  * @param parser
1859  *   Internal parser structure.
1860  * @param mark_id
1861  *   Mark identifier.
1862  *
1863  * @return
1864  *   0 on success, a negative errno value otherwise and rte_errno is set.
1865  */
1866 static int
1867 mlx5_flow_create_flag_mark(struct mlx5_flow_parse *parser, uint32_t mark_id)
1868 {
1869         unsigned int size = sizeof(struct ibv_flow_spec_action_tag);
1870         struct ibv_flow_spec_action_tag tag = {
1871                 .type = IBV_FLOW_SPEC_ACTION_TAG,
1872                 .size = size,
1873                 .tag_id = mlx5_flow_mark_set(mark_id),
1874         };
1875
1876         assert(parser->mark);
1877         mlx5_flow_create_copy(parser, &tag, size);
1878         return 0;
1879 }
1880
1881 /**
1882  * Convert count action to Verbs specification.
1883  *
1884  * @param dev
1885  *   Pointer to Ethernet device.
1886  * @param parser
1887  *   Pointer to MLX5 flow parser structure.
1888  *
1889  * @return
1890  *   0 on success, a negative errno value otherwise and rte_errno is set.
1891  */
1892 static int
1893 mlx5_flow_create_count(struct rte_eth_dev *dev __rte_unused,
1894                        struct mlx5_flow_parse *parser __rte_unused)
1895 {
1896 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
1897         struct priv *priv = dev->data->dev_private;
1898         unsigned int size = sizeof(struct ibv_flow_spec_counter_action);
1899         struct ibv_counter_set_init_attr init_attr = {0};
1900         struct ibv_flow_spec_counter_action counter = {
1901                 .type = IBV_FLOW_SPEC_ACTION_COUNT,
1902                 .size = size,
1903                 .counter_set_handle = 0,
1904         };
1905
1906         init_attr.counter_set_id = 0;
1907         parser->cs = mlx5_glue->create_counter_set(priv->ctx, &init_attr);
1908         if (!parser->cs) {
1909                 rte_errno = EINVAL;
1910                 return -rte_errno;
1911         }
1912         counter.counter_set_handle = parser->cs->handle;
1913         mlx5_flow_create_copy(parser, &counter, size);
1914 #endif
1915         return 0;
1916 }
1917
1918 /**
1919  * Complete flow rule creation with a drop queue.
1920  *
1921  * @param dev
1922  *   Pointer to Ethernet device.
1923  * @param parser
1924  *   Internal parser structure.
1925  * @param flow
1926  *   Pointer to the rte_flow.
1927  * @param[out] error
1928  *   Perform verbose error reporting if not NULL.
1929  *
1930  * @return
1931  *   0 on success, a negative errno value otherwise and rte_errno is set.
1932  */
1933 static int
1934 mlx5_flow_create_action_queue_drop(struct rte_eth_dev *dev,
1935                                    struct mlx5_flow_parse *parser,
1936                                    struct rte_flow *flow,
1937                                    struct rte_flow_error *error)
1938 {
1939         struct priv *priv = dev->data->dev_private;
1940         struct ibv_flow_spec_action_drop *drop;
1941         unsigned int size = sizeof(struct ibv_flow_spec_action_drop);
1942
1943         assert(priv->pd);
1944         assert(priv->ctx);
1945         flow->drop = 1;
1946         drop = (void *)((uintptr_t)parser->queue[HASH_RXQ_ETH].ibv_attr +
1947                         parser->queue[HASH_RXQ_ETH].offset);
1948         *drop = (struct ibv_flow_spec_action_drop){
1949                         .type = IBV_FLOW_SPEC_ACTION_DROP,
1950                         .size = size,
1951         };
1952         ++parser->queue[HASH_RXQ_ETH].ibv_attr->num_of_specs;
1953         parser->queue[HASH_RXQ_ETH].offset += size;
1954         flow->frxq[HASH_RXQ_ETH].ibv_attr =
1955                 parser->queue[HASH_RXQ_ETH].ibv_attr;
1956         if (parser->count)
1957                 flow->cs = parser->cs;
1958         if (!priv->dev->data->dev_started)
1959                 return 0;
1960         parser->queue[HASH_RXQ_ETH].ibv_attr = NULL;
1961         flow->frxq[HASH_RXQ_ETH].ibv_flow =
1962                 mlx5_glue->create_flow(priv->flow_drop_queue->qp,
1963                                        flow->frxq[HASH_RXQ_ETH].ibv_attr);
1964         if (!flow->frxq[HASH_RXQ_ETH].ibv_flow) {
1965                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1966                                    NULL, "flow rule creation failure");
1967                 goto error;
1968         }
1969         return 0;
1970 error:
1971         assert(flow);
1972         if (flow->frxq[HASH_RXQ_ETH].ibv_flow) {
1973                 claim_zero(mlx5_glue->destroy_flow
1974                            (flow->frxq[HASH_RXQ_ETH].ibv_flow));
1975                 flow->frxq[HASH_RXQ_ETH].ibv_flow = NULL;
1976         }
1977         if (flow->frxq[HASH_RXQ_ETH].ibv_attr) {
1978                 rte_free(flow->frxq[HASH_RXQ_ETH].ibv_attr);
1979                 flow->frxq[HASH_RXQ_ETH].ibv_attr = NULL;
1980         }
1981         if (flow->cs) {
1982                 claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
1983                 flow->cs = NULL;
1984                 parser->cs = NULL;
1985         }
1986         return -rte_errno;
1987 }
1988
1989 /**
1990  * Create hash Rx queues when RSS is enabled.
1991  *
1992  * @param dev
1993  *   Pointer to Ethernet device.
1994  * @param parser
1995  *   Internal parser structure.
1996  * @param flow
1997  *   Pointer to the rte_flow.
1998  * @param[out] error
1999  *   Perform verbose error reporting if not NULL.
2000  *
2001  * @return
2002  *   0 on success, a negative errno value otherwise and rte_errno is set.
2003  */
2004 static int
2005 mlx5_flow_create_action_queue_rss(struct rte_eth_dev *dev,
2006                                   struct mlx5_flow_parse *parser,
2007                                   struct rte_flow *flow,
2008                                   struct rte_flow_error *error)
2009 {
2010         struct priv *priv = dev->data->dev_private;
2011         unsigned int i;
2012
2013         for (i = 0; i != hash_rxq_init_n; ++i) {
2014                 if (!parser->queue[i].ibv_attr)
2015                         continue;
2016                 flow->frxq[i].ibv_attr = parser->queue[i].ibv_attr;
2017                 parser->queue[i].ibv_attr = NULL;
2018                 flow->frxq[i].hash_fields = parser->queue[i].hash_fields;
2019                 if (!priv->dev->data->dev_started)
2020                         continue;
2021                 flow->frxq[i].hrxq =
2022                         mlx5_hrxq_get(dev,
2023                                       parser->rss_conf.key,
2024                                       parser->rss_conf.key_len,
2025                                       flow->frxq[i].hash_fields,
2026                                       parser->rss_conf.queue,
2027                                       parser->rss_conf.queue_num,
2028                                       parser->tunnel,
2029                                       parser->rss_conf.level);
2030                 if (flow->frxq[i].hrxq)
2031                         continue;
2032                 flow->frxq[i].hrxq =
2033                         mlx5_hrxq_new(dev,
2034                                       parser->rss_conf.key,
2035                                       parser->rss_conf.key_len,
2036                                       flow->frxq[i].hash_fields,
2037                                       parser->rss_conf.queue,
2038                                       parser->rss_conf.queue_num,
2039                                       parser->tunnel,
2040                                       parser->rss_conf.level);
2041                 if (!flow->frxq[i].hrxq) {
2042                         return rte_flow_error_set(error, ENOMEM,
2043                                                   RTE_FLOW_ERROR_TYPE_HANDLE,
2044                                                   NULL,
2045                                                   "cannot create hash rxq");
2046                 }
2047         }
2048         return 0;
2049 }
2050
2051 /**
2052  * RXQ update after flow rule creation.
2053  *
2054  * @param dev
2055  *   Pointer to Ethernet device.
2056  * @param flow
2057  *   Pointer to the flow rule.
2058  */
2059 static void
2060 mlx5_flow_create_update_rxqs(struct rte_eth_dev *dev, struct rte_flow *flow)
2061 {
2062         struct priv *priv = dev->data->dev_private;
2063         unsigned int i;
2064         unsigned int j;
2065
2066         if (!dev->data->dev_started)
2067                 return;
2068         for (i = 0; i != flow->rss_conf.queue_num; ++i) {
2069                 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)
2070                                                  [(*flow->queues)[i]];
2071                 struct mlx5_rxq_ctrl *rxq_ctrl =
2072                         container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2073                 uint8_t tunnel = PTYPE_IDX(flow->tunnel);
2074
2075                 rxq_data->mark |= flow->mark;
2076                 if (!tunnel)
2077                         continue;
2078                 rxq_ctrl->tunnel_types[tunnel] += 1;
2079                 /* Clear tunnel type if more than one tunnel types set. */
2080                 for (j = 0; j != RTE_DIM(rxq_ctrl->tunnel_types); ++j) {
2081                         if (j == tunnel)
2082                                 continue;
2083                         if (rxq_ctrl->tunnel_types[j] > 0) {
2084                                 rxq_data->tunnel = 0;
2085                                 break;
2086                         }
2087                 }
2088                 if (j == RTE_DIM(rxq_ctrl->tunnel_types))
2089                         rxq_data->tunnel = flow->tunnel;
2090         }
2091 }
2092
2093 /**
2094  * Dump flow hash RX queue detail.
2095  *
2096  * @param dev
2097  *   Pointer to Ethernet device.
2098  * @param flow
2099  *   Pointer to the rte_flow.
2100  * @param hrxq_idx
2101  *   Hash RX queue index.
2102  */
2103 static void
2104 mlx5_flow_dump(struct rte_eth_dev *dev __rte_unused,
2105                struct rte_flow *flow __rte_unused,
2106                unsigned int hrxq_idx __rte_unused)
2107 {
2108 #ifndef NDEBUG
2109         uintptr_t spec_ptr;
2110         uint16_t j;
2111         char buf[256];
2112         uint8_t off;
2113
2114         spec_ptr = (uintptr_t)(flow->frxq[hrxq_idx].ibv_attr + 1);
2115         for (j = 0, off = 0; j < flow->frxq[hrxq_idx].ibv_attr->num_of_specs;
2116              j++) {
2117                 struct ibv_flow_spec *spec = (void *)spec_ptr;
2118                 off += sprintf(buf + off, " %x(%hu)", spec->hdr.type,
2119                                spec->hdr.size);
2120                 spec_ptr += spec->hdr.size;
2121         }
2122         DRV_LOG(DEBUG,
2123                 "port %u Verbs flow %p type %u: hrxq:%p qp:%p ind:%p,"
2124                 " hash:%" PRIx64 "/%u specs:%hhu(%hu), priority:%hu, type:%d,"
2125                 " flags:%x, comp_mask:%x specs:%s",
2126                 dev->data->port_id, (void *)flow, hrxq_idx,
2127                 (void *)flow->frxq[hrxq_idx].hrxq,
2128                 (void *)flow->frxq[hrxq_idx].hrxq->qp,
2129                 (void *)flow->frxq[hrxq_idx].hrxq->ind_table,
2130                 flow->frxq[hrxq_idx].hash_fields |
2131                 (flow->tunnel &&
2132                  flow->rss_conf.level > 1 ? (uint32_t)IBV_RX_HASH_INNER : 0),
2133                 flow->rss_conf.queue_num,
2134                 flow->frxq[hrxq_idx].ibv_attr->num_of_specs,
2135                 flow->frxq[hrxq_idx].ibv_attr->size,
2136                 flow->frxq[hrxq_idx].ibv_attr->priority,
2137                 flow->frxq[hrxq_idx].ibv_attr->type,
2138                 flow->frxq[hrxq_idx].ibv_attr->flags,
2139                 flow->frxq[hrxq_idx].ibv_attr->comp_mask,
2140                 buf);
2141 #endif
2142 }
2143
2144 /**
2145  * Complete flow rule creation.
2146  *
2147  * @param dev
2148  *   Pointer to Ethernet device.
2149  * @param parser
2150  *   Internal parser structure.
2151  * @param flow
2152  *   Pointer to the rte_flow.
2153  * @param[out] error
2154  *   Perform verbose error reporting if not NULL.
2155  *
2156  * @return
2157  *   0 on success, a negative errno value otherwise and rte_errno is set.
2158  */
2159 static int
2160 mlx5_flow_create_action_queue(struct rte_eth_dev *dev,
2161                               struct mlx5_flow_parse *parser,
2162                               struct rte_flow *flow,
2163                               struct rte_flow_error *error)
2164 {
2165         struct priv *priv = dev->data->dev_private;
2166         int ret;
2167         unsigned int i;
2168         unsigned int flows_n = 0;
2169
2170         assert(priv->pd);
2171         assert(priv->ctx);
2172         assert(!parser->drop);
2173         ret = mlx5_flow_create_action_queue_rss(dev, parser, flow, error);
2174         if (ret)
2175                 goto error;
2176         if (parser->count)
2177                 flow->cs = parser->cs;
2178         if (!priv->dev->data->dev_started)
2179                 return 0;
2180         for (i = 0; i != hash_rxq_init_n; ++i) {
2181                 if (!flow->frxq[i].hrxq)
2182                         continue;
2183                 flow->frxq[i].ibv_flow =
2184                         mlx5_glue->create_flow(flow->frxq[i].hrxq->qp,
2185                                                flow->frxq[i].ibv_attr);
2186                 mlx5_flow_dump(dev, flow, i);
2187                 if (!flow->frxq[i].ibv_flow) {
2188                         rte_flow_error_set(error, ENOMEM,
2189                                            RTE_FLOW_ERROR_TYPE_HANDLE,
2190                                            NULL, "flow rule creation failure");
2191                         goto error;
2192                 }
2193                 ++flows_n;
2194         }
2195         if (!flows_n) {
2196                 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
2197                                    NULL, "internal error in flow creation");
2198                 goto error;
2199         }
2200         mlx5_flow_create_update_rxqs(dev, flow);
2201         return 0;
2202 error:
2203         ret = rte_errno; /* Save rte_errno before cleanup. */
2204         assert(flow);
2205         for (i = 0; i != hash_rxq_init_n; ++i) {
2206                 if (flow->frxq[i].ibv_flow) {
2207                         struct ibv_flow *ibv_flow = flow->frxq[i].ibv_flow;
2208
2209                         claim_zero(mlx5_glue->destroy_flow(ibv_flow));
2210                 }
2211                 if (flow->frxq[i].hrxq)
2212                         mlx5_hrxq_release(dev, flow->frxq[i].hrxq);
2213                 if (flow->frxq[i].ibv_attr)
2214                         rte_free(flow->frxq[i].ibv_attr);
2215         }
2216         if (flow->cs) {
2217                 claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2218                 flow->cs = NULL;
2219                 parser->cs = NULL;
2220         }
2221         rte_errno = ret; /* Restore rte_errno. */
2222         return -rte_errno;
2223 }
2224
2225 /**
2226  * Convert a flow.
2227  *
2228  * @param dev
2229  *   Pointer to Ethernet device.
2230  * @param list
2231  *   Pointer to a TAILQ flow list.
2232  * @param[in] attr
2233  *   Flow rule attributes.
2234  * @param[in] pattern
2235  *   Pattern specification (list terminated by the END pattern item).
2236  * @param[in] actions
2237  *   Associated actions (list terminated by the END action).
2238  * @param[out] error
2239  *   Perform verbose error reporting if not NULL.
2240  *
2241  * @return
2242  *   A flow on success, NULL otherwise and rte_errno is set.
2243  */
2244 static struct rte_flow *
2245 mlx5_flow_list_create(struct rte_eth_dev *dev,
2246                       struct mlx5_flows *list,
2247                       const struct rte_flow_attr *attr,
2248                       const struct rte_flow_item items[],
2249                       const struct rte_flow_action actions[],
2250                       struct rte_flow_error *error)
2251 {
2252         struct mlx5_flow_parse parser = { .create = 1, };
2253         struct rte_flow *flow = NULL;
2254         unsigned int i;
2255         int ret;
2256
2257         ret = mlx5_flow_convert(dev, attr, items, actions, error, &parser);
2258         if (ret)
2259                 goto exit;
2260         flow = rte_calloc(__func__, 1,
2261                           sizeof(*flow) +
2262                           parser.rss_conf.queue_num * sizeof(uint16_t),
2263                           0);
2264         if (!flow) {
2265                 rte_flow_error_set(error, ENOMEM,
2266                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2267                                    NULL,
2268                                    "cannot allocate flow memory");
2269                 return NULL;
2270         }
2271         /* Copy configuration. */
2272         flow->queues = (uint16_t (*)[])(flow + 1);
2273         flow->tunnel = parser.tunnel;
2274         flow->rss_conf = (struct rte_flow_action_rss){
2275                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2276                 .level = 0,
2277                 .types = parser.rss_conf.types,
2278                 .key_len = parser.rss_conf.key_len,
2279                 .queue_num = parser.rss_conf.queue_num,
2280                 .key = memcpy(flow->rss_key, parser.rss_conf.key,
2281                               sizeof(*parser.rss_conf.key) *
2282                               parser.rss_conf.key_len),
2283                 .queue = memcpy(flow->queues, parser.rss_conf.queue,
2284                                 sizeof(*parser.rss_conf.queue) *
2285                                 parser.rss_conf.queue_num),
2286         };
2287         flow->mark = parser.mark;
2288         /* finalise the flow. */
2289         if (parser.drop)
2290                 ret = mlx5_flow_create_action_queue_drop(dev, &parser, flow,
2291                                                          error);
2292         else
2293                 ret = mlx5_flow_create_action_queue(dev, &parser, flow, error);
2294         if (ret)
2295                 goto exit;
2296         TAILQ_INSERT_TAIL(list, flow, next);
2297         DRV_LOG(DEBUG, "port %u flow created %p", dev->data->port_id,
2298                 (void *)flow);
2299         return flow;
2300 exit:
2301         DRV_LOG(ERR, "port %u flow creation error: %s", dev->data->port_id,
2302                 error->message);
2303         for (i = 0; i != hash_rxq_init_n; ++i) {
2304                 if (parser.queue[i].ibv_attr)
2305                         rte_free(parser.queue[i].ibv_attr);
2306         }
2307         rte_free(flow);
2308         return NULL;
2309 }
2310
2311 /**
2312  * Validate a flow supported by the NIC.
2313  *
2314  * @see rte_flow_validate()
2315  * @see rte_flow_ops
2316  */
2317 int
2318 mlx5_flow_validate(struct rte_eth_dev *dev,
2319                    const struct rte_flow_attr *attr,
2320                    const struct rte_flow_item items[],
2321                    const struct rte_flow_action actions[],
2322                    struct rte_flow_error *error)
2323 {
2324         struct mlx5_flow_parse parser = { .create = 0, };
2325
2326         return mlx5_flow_convert(dev, attr, items, actions, error, &parser);
2327 }
2328
2329 /**
2330  * Create a flow.
2331  *
2332  * @see rte_flow_create()
2333  * @see rte_flow_ops
2334  */
2335 struct rte_flow *
2336 mlx5_flow_create(struct rte_eth_dev *dev,
2337                  const struct rte_flow_attr *attr,
2338                  const struct rte_flow_item items[],
2339                  const struct rte_flow_action actions[],
2340                  struct rte_flow_error *error)
2341 {
2342         struct priv *priv = dev->data->dev_private;
2343
2344         return mlx5_flow_list_create(dev, &priv->flows, attr, items, actions,
2345                                      error);
2346 }
2347
2348 /**
2349  * Destroy a flow in a list.
2350  *
2351  * @param dev
2352  *   Pointer to Ethernet device.
2353  * @param list
2354  *   Pointer to a TAILQ flow list.
2355  * @param[in] flow
2356  *   Flow to destroy.
2357  */
2358 static void
2359 mlx5_flow_list_destroy(struct rte_eth_dev *dev, struct mlx5_flows *list,
2360                        struct rte_flow *flow)
2361 {
2362         struct priv *priv = dev->data->dev_private;
2363         unsigned int i;
2364
2365         if (flow->drop || !dev->data->dev_started)
2366                 goto free;
2367         for (i = 0; flow->tunnel && i != flow->rss_conf.queue_num; ++i) {
2368                 /* Update queue tunnel type. */
2369                 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)
2370                                                  [(*flow->queues)[i]];
2371                 struct mlx5_rxq_ctrl *rxq_ctrl =
2372                         container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2373                 uint8_t tunnel = PTYPE_IDX(flow->tunnel);
2374
2375                 assert(rxq_ctrl->tunnel_types[tunnel] > 0);
2376                 rxq_ctrl->tunnel_types[tunnel] -= 1;
2377                 if (!rxq_ctrl->tunnel_types[tunnel]) {
2378                         /* Update tunnel type. */
2379                         uint8_t j;
2380                         uint8_t types = 0;
2381                         uint8_t last;
2382
2383                         for (j = 0; j < RTE_DIM(rxq_ctrl->tunnel_types); j++)
2384                                 if (rxq_ctrl->tunnel_types[j]) {
2385                                         types += 1;
2386                                         last = j;
2387                                 }
2388                         /* Keep same if more than one tunnel types left. */
2389                         if (types == 1)
2390                                 rxq_data->tunnel = ptype_ext[last];
2391                         else if (types == 0)
2392                                 /* No tunnel type left. */
2393                                 rxq_data->tunnel = 0;
2394                 }
2395         }
2396         for (i = 0; flow->mark && i != flow->rss_conf.queue_num; ++i) {
2397                 struct rte_flow *tmp;
2398                 int mark = 0;
2399
2400                 /*
2401                  * To remove the mark from the queue, the queue must not be
2402                  * present in any other marked flow (RSS or not).
2403                  */
2404                 TAILQ_FOREACH(tmp, list, next) {
2405                         unsigned int j;
2406                         uint16_t *tqs = NULL;
2407                         uint16_t tq_n = 0;
2408
2409                         if (!tmp->mark)
2410                                 continue;
2411                         for (j = 0; j != hash_rxq_init_n; ++j) {
2412                                 if (!tmp->frxq[j].hrxq)
2413                                         continue;
2414                                 tqs = tmp->frxq[j].hrxq->ind_table->queues;
2415                                 tq_n = tmp->frxq[j].hrxq->ind_table->queues_n;
2416                         }
2417                         if (!tq_n)
2418                                 continue;
2419                         for (j = 0; (j != tq_n) && !mark; j++)
2420                                 if (tqs[j] == (*flow->queues)[i])
2421                                         mark = 1;
2422                 }
2423                 (*priv->rxqs)[(*flow->queues)[i]]->mark = mark;
2424         }
2425 free:
2426         if (flow->drop) {
2427                 if (flow->frxq[HASH_RXQ_ETH].ibv_flow)
2428                         claim_zero(mlx5_glue->destroy_flow
2429                                    (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2430                 rte_free(flow->frxq[HASH_RXQ_ETH].ibv_attr);
2431         } else {
2432                 for (i = 0; i != hash_rxq_init_n; ++i) {
2433                         struct mlx5_flow *frxq = &flow->frxq[i];
2434
2435                         if (frxq->ibv_flow)
2436                                 claim_zero(mlx5_glue->destroy_flow
2437                                            (frxq->ibv_flow));
2438                         if (frxq->hrxq)
2439                                 mlx5_hrxq_release(dev, frxq->hrxq);
2440                         if (frxq->ibv_attr)
2441                                 rte_free(frxq->ibv_attr);
2442                 }
2443         }
2444         if (flow->cs) {
2445                 claim_zero(mlx5_glue->destroy_counter_set(flow->cs));
2446                 flow->cs = NULL;
2447         }
2448         TAILQ_REMOVE(list, flow, next);
2449         DRV_LOG(DEBUG, "port %u flow destroyed %p", dev->data->port_id,
2450                 (void *)flow);
2451         rte_free(flow);
2452 }
2453
2454 /**
2455  * Destroy all flows.
2456  *
2457  * @param dev
2458  *   Pointer to Ethernet device.
2459  * @param list
2460  *   Pointer to a TAILQ flow list.
2461  */
2462 void
2463 mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list)
2464 {
2465         while (!TAILQ_EMPTY(list)) {
2466                 struct rte_flow *flow;
2467
2468                 flow = TAILQ_FIRST(list);
2469                 mlx5_flow_list_destroy(dev, list, flow);
2470         }
2471 }
2472
2473 /**
2474  * Create drop queue.
2475  *
2476  * @param dev
2477  *   Pointer to Ethernet device.
2478  *
2479  * @return
2480  *   0 on success, a negative errno value otherwise and rte_errno is set.
2481  */
2482 int
2483 mlx5_flow_create_drop_queue(struct rte_eth_dev *dev)
2484 {
2485         struct priv *priv = dev->data->dev_private;
2486         struct mlx5_hrxq_drop *fdq = NULL;
2487
2488         assert(priv->pd);
2489         assert(priv->ctx);
2490         fdq = rte_calloc(__func__, 1, sizeof(*fdq), 0);
2491         if (!fdq) {
2492                 DRV_LOG(WARNING,
2493                         "port %u cannot allocate memory for drop queue",
2494                         dev->data->port_id);
2495                 rte_errno = ENOMEM;
2496                 return -rte_errno;
2497         }
2498         fdq->cq = mlx5_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
2499         if (!fdq->cq) {
2500                 DRV_LOG(WARNING, "port %u cannot allocate CQ for drop queue",
2501                         dev->data->port_id);
2502                 rte_errno = errno;
2503                 goto error;
2504         }
2505         fdq->wq = mlx5_glue->create_wq
2506                 (priv->ctx,
2507                  &(struct ibv_wq_init_attr){
2508                         .wq_type = IBV_WQT_RQ,
2509                         .max_wr = 1,
2510                         .max_sge = 1,
2511                         .pd = priv->pd,
2512                         .cq = fdq->cq,
2513                  });
2514         if (!fdq->wq) {
2515                 DRV_LOG(WARNING, "port %u cannot allocate WQ for drop queue",
2516                         dev->data->port_id);
2517                 rte_errno = errno;
2518                 goto error;
2519         }
2520         fdq->ind_table = mlx5_glue->create_rwq_ind_table
2521                 (priv->ctx,
2522                  &(struct ibv_rwq_ind_table_init_attr){
2523                         .log_ind_tbl_size = 0,
2524                         .ind_tbl = &fdq->wq,
2525                         .comp_mask = 0,
2526                  });
2527         if (!fdq->ind_table) {
2528                 DRV_LOG(WARNING,
2529                         "port %u cannot allocate indirection table for drop"
2530                         " queue",
2531                         dev->data->port_id);
2532                 rte_errno = errno;
2533                 goto error;
2534         }
2535         fdq->qp = mlx5_glue->create_qp_ex
2536                 (priv->ctx,
2537                  &(struct ibv_qp_init_attr_ex){
2538                         .qp_type = IBV_QPT_RAW_PACKET,
2539                         .comp_mask =
2540                                 IBV_QP_INIT_ATTR_PD |
2541                                 IBV_QP_INIT_ATTR_IND_TABLE |
2542                                 IBV_QP_INIT_ATTR_RX_HASH,
2543                         .rx_hash_conf = (struct ibv_rx_hash_conf){
2544                                 .rx_hash_function =
2545                                         IBV_RX_HASH_FUNC_TOEPLITZ,
2546                                 .rx_hash_key_len = rss_hash_default_key_len,
2547                                 .rx_hash_key = rss_hash_default_key,
2548                                 .rx_hash_fields_mask = 0,
2549                                 },
2550                         .rwq_ind_tbl = fdq->ind_table,
2551                         .pd = priv->pd
2552                  });
2553         if (!fdq->qp) {
2554                 DRV_LOG(WARNING, "port %u cannot allocate QP for drop queue",
2555                         dev->data->port_id);
2556                 rte_errno = errno;
2557                 goto error;
2558         }
2559         priv->flow_drop_queue = fdq;
2560         return 0;
2561 error:
2562         if (fdq->qp)
2563                 claim_zero(mlx5_glue->destroy_qp(fdq->qp));
2564         if (fdq->ind_table)
2565                 claim_zero(mlx5_glue->destroy_rwq_ind_table(fdq->ind_table));
2566         if (fdq->wq)
2567                 claim_zero(mlx5_glue->destroy_wq(fdq->wq));
2568         if (fdq->cq)
2569                 claim_zero(mlx5_glue->destroy_cq(fdq->cq));
2570         if (fdq)
2571                 rte_free(fdq);
2572         priv->flow_drop_queue = NULL;
2573         return -rte_errno;
2574 }
2575
2576 /**
2577  * Delete drop queue.
2578  *
2579  * @param dev
2580  *   Pointer to Ethernet device.
2581  */
2582 void
2583 mlx5_flow_delete_drop_queue(struct rte_eth_dev *dev)
2584 {
2585         struct priv *priv = dev->data->dev_private;
2586         struct mlx5_hrxq_drop *fdq = priv->flow_drop_queue;
2587
2588         if (!fdq)
2589                 return;
2590         if (fdq->qp)
2591                 claim_zero(mlx5_glue->destroy_qp(fdq->qp));
2592         if (fdq->ind_table)
2593                 claim_zero(mlx5_glue->destroy_rwq_ind_table(fdq->ind_table));
2594         if (fdq->wq)
2595                 claim_zero(mlx5_glue->destroy_wq(fdq->wq));
2596         if (fdq->cq)
2597                 claim_zero(mlx5_glue->destroy_cq(fdq->cq));
2598         rte_free(fdq);
2599         priv->flow_drop_queue = NULL;
2600 }
2601
2602 /**
2603  * Remove all flows.
2604  *
2605  * @param dev
2606  *   Pointer to Ethernet device.
2607  * @param list
2608  *   Pointer to a TAILQ flow list.
2609  */
2610 void
2611 mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list)
2612 {
2613         struct priv *priv = dev->data->dev_private;
2614         struct rte_flow *flow;
2615         unsigned int i;
2616
2617         TAILQ_FOREACH_REVERSE(flow, list, mlx5_flows, next) {
2618                 struct mlx5_ind_table_ibv *ind_tbl = NULL;
2619
2620                 if (flow->drop) {
2621                         if (!flow->frxq[HASH_RXQ_ETH].ibv_flow)
2622                                 continue;
2623                         claim_zero(mlx5_glue->destroy_flow
2624                                    (flow->frxq[HASH_RXQ_ETH].ibv_flow));
2625                         flow->frxq[HASH_RXQ_ETH].ibv_flow = NULL;
2626                         DRV_LOG(DEBUG, "port %u flow %p removed",
2627                                 dev->data->port_id, (void *)flow);
2628                         /* Next flow. */
2629                         continue;
2630                 }
2631                 /* Verify the flow has not already been cleaned. */
2632                 for (i = 0; i != hash_rxq_init_n; ++i) {
2633                         if (!flow->frxq[i].ibv_flow)
2634                                 continue;
2635                         /*
2636                          * Indirection table may be necessary to remove the
2637                          * flags in the Rx queues.
2638                          * This helps to speed-up the process by avoiding
2639                          * another loop.
2640                          */
2641                         ind_tbl = flow->frxq[i].hrxq->ind_table;
2642                         break;
2643                 }
2644                 if (i == hash_rxq_init_n)
2645                         return;
2646                 if (flow->mark) {
2647                         assert(ind_tbl);
2648                         for (i = 0; i != ind_tbl->queues_n; ++i)
2649                                 (*priv->rxqs)[ind_tbl->queues[i]]->mark = 0;
2650                 }
2651                 for (i = 0; i != hash_rxq_init_n; ++i) {
2652                         if (!flow->frxq[i].ibv_flow)
2653                                 continue;
2654                         claim_zero(mlx5_glue->destroy_flow
2655                                    (flow->frxq[i].ibv_flow));
2656                         flow->frxq[i].ibv_flow = NULL;
2657                         mlx5_hrxq_release(dev, flow->frxq[i].hrxq);
2658                         flow->frxq[i].hrxq = NULL;
2659                 }
2660                 DRV_LOG(DEBUG, "port %u flow %p removed", dev->data->port_id,
2661                         (void *)flow);
2662         }
2663         /* Cleanup Rx queue tunnel info. */
2664         for (i = 0; i != priv->rxqs_n; ++i) {
2665                 struct mlx5_rxq_data *q = (*priv->rxqs)[i];
2666                 struct mlx5_rxq_ctrl *rxq_ctrl =
2667                         container_of(q, struct mlx5_rxq_ctrl, rxq);
2668
2669                 if (!q)
2670                         continue;
2671                 memset((void *)rxq_ctrl->tunnel_types, 0,
2672                        sizeof(rxq_ctrl->tunnel_types));
2673                 q->tunnel = 0;
2674         }
2675 }
2676
2677 /**
2678  * Add all flows.
2679  *
2680  * @param dev
2681  *   Pointer to Ethernet device.
2682  * @param list
2683  *   Pointer to a TAILQ flow list.
2684  *
2685  * @return
2686  *   0 on success, a negative errno value otherwise and rte_errno is set.
2687  */
2688 int
2689 mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list)
2690 {
2691         struct priv *priv = dev->data->dev_private;
2692         struct rte_flow *flow;
2693
2694         TAILQ_FOREACH(flow, list, next) {
2695                 unsigned int i;
2696
2697                 if (flow->drop) {
2698                         flow->frxq[HASH_RXQ_ETH].ibv_flow =
2699                                 mlx5_glue->create_flow
2700                                 (priv->flow_drop_queue->qp,
2701                                  flow->frxq[HASH_RXQ_ETH].ibv_attr);
2702                         if (!flow->frxq[HASH_RXQ_ETH].ibv_flow) {
2703                                 DRV_LOG(DEBUG,
2704                                         "port %u flow %p cannot be applied",
2705                                         dev->data->port_id, (void *)flow);
2706                                 rte_errno = EINVAL;
2707                                 return -rte_errno;
2708                         }
2709                         DRV_LOG(DEBUG, "port %u flow %p applied",
2710                                 dev->data->port_id, (void *)flow);
2711                         /* Next flow. */
2712                         continue;
2713                 }
2714                 for (i = 0; i != hash_rxq_init_n; ++i) {
2715                         if (!flow->frxq[i].ibv_attr)
2716                                 continue;
2717                         flow->frxq[i].hrxq =
2718                                 mlx5_hrxq_get(dev, flow->rss_conf.key,
2719                                               flow->rss_conf.key_len,
2720                                               flow->frxq[i].hash_fields,
2721                                               flow->rss_conf.queue,
2722                                               flow->rss_conf.queue_num,
2723                                               flow->tunnel,
2724                                               flow->rss_conf.level);
2725                         if (flow->frxq[i].hrxq)
2726                                 goto flow_create;
2727                         flow->frxq[i].hrxq =
2728                                 mlx5_hrxq_new(dev, flow->rss_conf.key,
2729                                               flow->rss_conf.key_len,
2730                                               flow->frxq[i].hash_fields,
2731                                               flow->rss_conf.queue,
2732                                               flow->rss_conf.queue_num,
2733                                               flow->tunnel,
2734                                               flow->rss_conf.level);
2735                         if (!flow->frxq[i].hrxq) {
2736                                 DRV_LOG(DEBUG,
2737                                         "port %u flow %p cannot create hash"
2738                                         " rxq",
2739                                         dev->data->port_id, (void *)flow);
2740                                 rte_errno = EINVAL;
2741                                 return -rte_errno;
2742                         }
2743 flow_create:
2744                         mlx5_flow_dump(dev, flow, i);
2745                         flow->frxq[i].ibv_flow =
2746                                 mlx5_glue->create_flow(flow->frxq[i].hrxq->qp,
2747                                                        flow->frxq[i].ibv_attr);
2748                         if (!flow->frxq[i].ibv_flow) {
2749                                 DRV_LOG(DEBUG,
2750                                         "port %u flow %p type %u cannot be"
2751                                         " applied",
2752                                         dev->data->port_id, (void *)flow, i);
2753                                 rte_errno = EINVAL;
2754                                 return -rte_errno;
2755                         }
2756                 }
2757                 mlx5_flow_create_update_rxqs(dev, flow);
2758         }
2759         return 0;
2760 }
2761
2762 /**
2763  * Verify the flow list is empty
2764  *
2765  * @param dev
2766  *  Pointer to Ethernet device.
2767  *
2768  * @return the number of flows not released.
2769  */
2770 int
2771 mlx5_flow_verify(struct rte_eth_dev *dev)
2772 {
2773         struct priv *priv = dev->data->dev_private;
2774         struct rte_flow *flow;
2775         int ret = 0;
2776
2777         TAILQ_FOREACH(flow, &priv->flows, next) {
2778                 DRV_LOG(DEBUG, "port %u flow %p still referenced",
2779                         dev->data->port_id, (void *)flow);
2780                 ++ret;
2781         }
2782         return ret;
2783 }
2784
2785 /**
2786  * Enable a control flow configured from the control plane.
2787  *
2788  * @param dev
2789  *   Pointer to Ethernet device.
2790  * @param eth_spec
2791  *   An Ethernet flow spec to apply.
2792  * @param eth_mask
2793  *   An Ethernet flow mask to apply.
2794  * @param vlan_spec
2795  *   A VLAN flow spec to apply.
2796  * @param vlan_mask
2797  *   A VLAN flow mask to apply.
2798  *
2799  * @return
2800  *   0 on success, a negative errno value otherwise and rte_errno is set.
2801  */
2802 int
2803 mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
2804                     struct rte_flow_item_eth *eth_spec,
2805                     struct rte_flow_item_eth *eth_mask,
2806                     struct rte_flow_item_vlan *vlan_spec,
2807                     struct rte_flow_item_vlan *vlan_mask)
2808 {
2809         struct priv *priv = dev->data->dev_private;
2810         const struct rte_flow_attr attr = {
2811                 .ingress = 1,
2812                 .priority = MLX5_CTRL_FLOW_PRIORITY,
2813         };
2814         struct rte_flow_item items[] = {
2815                 {
2816                         .type = RTE_FLOW_ITEM_TYPE_ETH,
2817                         .spec = eth_spec,
2818                         .last = NULL,
2819                         .mask = eth_mask,
2820                 },
2821                 {
2822                         .type = (vlan_spec) ? RTE_FLOW_ITEM_TYPE_VLAN :
2823                                 RTE_FLOW_ITEM_TYPE_END,
2824                         .spec = vlan_spec,
2825                         .last = NULL,
2826                         .mask = vlan_mask,
2827                 },
2828                 {
2829                         .type = RTE_FLOW_ITEM_TYPE_END,
2830                 },
2831         };
2832         uint16_t queue[priv->reta_idx_n];
2833         struct rte_flow_action_rss action_rss = {
2834                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2835                 .level = 0,
2836                 .types = priv->rss_conf.rss_hf,
2837                 .key_len = priv->rss_conf.rss_key_len,
2838                 .queue_num = priv->reta_idx_n,
2839                 .key = priv->rss_conf.rss_key,
2840                 .queue = queue,
2841         };
2842         struct rte_flow_action actions[] = {
2843                 {
2844                         .type = RTE_FLOW_ACTION_TYPE_RSS,
2845                         .conf = &action_rss,
2846                 },
2847                 {
2848                         .type = RTE_FLOW_ACTION_TYPE_END,
2849                 },
2850         };
2851         struct rte_flow *flow;
2852         struct rte_flow_error error;
2853         unsigned int i;
2854
2855         if (!priv->reta_idx_n) {
2856                 rte_errno = EINVAL;
2857                 return -rte_errno;
2858         }
2859         for (i = 0; i != priv->reta_idx_n; ++i)
2860                 queue[i] = (*priv->reta_idx)[i];
2861         flow = mlx5_flow_list_create(dev, &priv->ctrl_flows, &attr, items,
2862                                      actions, &error);
2863         if (!flow)
2864                 return -rte_errno;
2865         return 0;
2866 }
2867
2868 /**
2869  * Enable a flow control configured from the control plane.
2870  *
2871  * @param dev
2872  *   Pointer to Ethernet device.
2873  * @param eth_spec
2874  *   An Ethernet flow spec to apply.
2875  * @param eth_mask
2876  *   An Ethernet flow mask to apply.
2877  *
2878  * @return
2879  *   0 on success, a negative errno value otherwise and rte_errno is set.
2880  */
2881 int
2882 mlx5_ctrl_flow(struct rte_eth_dev *dev,
2883                struct rte_flow_item_eth *eth_spec,
2884                struct rte_flow_item_eth *eth_mask)
2885 {
2886         return mlx5_ctrl_flow_vlan(dev, eth_spec, eth_mask, NULL, NULL);
2887 }
2888
2889 /**
2890  * Destroy a flow.
2891  *
2892  * @see rte_flow_destroy()
2893  * @see rte_flow_ops
2894  */
2895 int
2896 mlx5_flow_destroy(struct rte_eth_dev *dev,
2897                   struct rte_flow *flow,
2898                   struct rte_flow_error *error __rte_unused)
2899 {
2900         struct priv *priv = dev->data->dev_private;
2901
2902         mlx5_flow_list_destroy(dev, &priv->flows, flow);
2903         return 0;
2904 }
2905
2906 /**
2907  * Destroy all flows.
2908  *
2909  * @see rte_flow_flush()
2910  * @see rte_flow_ops
2911  */
2912 int
2913 mlx5_flow_flush(struct rte_eth_dev *dev,
2914                 struct rte_flow_error *error __rte_unused)
2915 {
2916         struct priv *priv = dev->data->dev_private;
2917
2918         mlx5_flow_list_flush(dev, &priv->flows);
2919         return 0;
2920 }
2921
2922 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
2923 /**
2924  * Query flow counter.
2925  *
2926  * @param cs
2927  *   the counter set.
2928  * @param counter_value
2929  *   returned data from the counter.
2930  *
2931  * @return
2932  *   0 on success, a negative errno value otherwise and rte_errno is set.
2933  */
2934 static int
2935 mlx5_flow_query_count(struct ibv_counter_set *cs,
2936                       struct mlx5_flow_counter_stats *counter_stats,
2937                       struct rte_flow_query_count *query_count,
2938                       struct rte_flow_error *error)
2939 {
2940         uint64_t counters[2];
2941         struct ibv_query_counter_set_attr query_cs_attr = {
2942                 .cs = cs,
2943                 .query_flags = IBV_COUNTER_SET_FORCE_UPDATE,
2944         };
2945         struct ibv_counter_set_data query_out = {
2946                 .out = counters,
2947                 .outlen = 2 * sizeof(uint64_t),
2948         };
2949         int err = mlx5_glue->query_counter_set(&query_cs_attr, &query_out);
2950
2951         if (err)
2952                 return rte_flow_error_set(error, err,
2953                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2954                                           NULL,
2955                                           "cannot read counter");
2956         query_count->hits_set = 1;
2957         query_count->bytes_set = 1;
2958         query_count->hits = counters[0] - counter_stats->hits;
2959         query_count->bytes = counters[1] - counter_stats->bytes;
2960         if (query_count->reset) {
2961                 counter_stats->hits = counters[0];
2962                 counter_stats->bytes = counters[1];
2963         }
2964         return 0;
2965 }
2966
2967 /**
2968  * Query a flows.
2969  *
2970  * @see rte_flow_query()
2971  * @see rte_flow_ops
2972  */
2973 int
2974 mlx5_flow_query(struct rte_eth_dev *dev __rte_unused,
2975                 struct rte_flow *flow,
2976                 enum rte_flow_action_type action __rte_unused,
2977                 void *data,
2978                 struct rte_flow_error *error)
2979 {
2980         if (flow->cs) {
2981                 int ret;
2982
2983                 ret = mlx5_flow_query_count(flow->cs,
2984                                             &flow->counter_stats,
2985                                             (struct rte_flow_query_count *)data,
2986                                             error);
2987                 if (ret)
2988                         return ret;
2989         } else {
2990                 return rte_flow_error_set(error, EINVAL,
2991                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2992                                           NULL,
2993                                           "no counter found for flow");
2994         }
2995         return 0;
2996 }
2997 #endif
2998
2999 /**
3000  * Isolated mode.
3001  *
3002  * @see rte_flow_isolate()
3003  * @see rte_flow_ops
3004  */
3005 int
3006 mlx5_flow_isolate(struct rte_eth_dev *dev,
3007                   int enable,
3008                   struct rte_flow_error *error)
3009 {
3010         struct priv *priv = dev->data->dev_private;
3011
3012         if (dev->data->dev_started) {
3013                 rte_flow_error_set(error, EBUSY,
3014                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
3015                                    NULL,
3016                                    "port must be stopped first");
3017                 return -rte_errno;
3018         }
3019         priv->isolated = !!enable;
3020         if (enable)
3021                 priv->dev->dev_ops = &mlx5_dev_ops_isolate;
3022         else
3023                 priv->dev->dev_ops = &mlx5_dev_ops;
3024         return 0;
3025 }
3026
3027 /**
3028  * Convert a flow director filter to a generic flow.
3029  *
3030  * @param dev
3031  *   Pointer to Ethernet device.
3032  * @param fdir_filter
3033  *   Flow director filter to add.
3034  * @param attributes
3035  *   Generic flow parameters structure.
3036  *
3037  * @return
3038  *   0 on success, a negative errno value otherwise and rte_errno is set.
3039  */
3040 static int
3041 mlx5_fdir_filter_convert(struct rte_eth_dev *dev,
3042                          const struct rte_eth_fdir_filter *fdir_filter,
3043                          struct mlx5_fdir *attributes)
3044 {
3045         struct priv *priv = dev->data->dev_private;
3046         const struct rte_eth_fdir_input *input = &fdir_filter->input;
3047         const struct rte_eth_fdir_masks *mask =
3048                 &dev->data->dev_conf.fdir_conf.mask;
3049
3050         /* Validate queue number. */
3051         if (fdir_filter->action.rx_queue >= priv->rxqs_n) {
3052                 DRV_LOG(ERR, "port %u invalid queue number %d",
3053                         dev->data->port_id, fdir_filter->action.rx_queue);
3054                 rte_errno = EINVAL;
3055                 return -rte_errno;
3056         }
3057         attributes->attr.ingress = 1;
3058         attributes->items[0] = (struct rte_flow_item) {
3059                 .type = RTE_FLOW_ITEM_TYPE_ETH,
3060                 .spec = &attributes->l2,
3061                 .mask = &attributes->l2_mask,
3062         };
3063         switch (fdir_filter->action.behavior) {
3064         case RTE_ETH_FDIR_ACCEPT:
3065                 attributes->actions[0] = (struct rte_flow_action){
3066                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
3067                         .conf = &attributes->queue,
3068                 };
3069                 break;
3070         case RTE_ETH_FDIR_REJECT:
3071                 attributes->actions[0] = (struct rte_flow_action){
3072                         .type = RTE_FLOW_ACTION_TYPE_DROP,
3073                 };
3074                 break;
3075         default:
3076                 DRV_LOG(ERR, "port %u invalid behavior %d",
3077                         dev->data->port_id,
3078                         fdir_filter->action.behavior);
3079                 rte_errno = ENOTSUP;
3080                 return -rte_errno;
3081         }
3082         attributes->queue.index = fdir_filter->action.rx_queue;
3083         /* Handle L3. */
3084         switch (fdir_filter->input.flow_type) {
3085         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
3086         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
3087         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
3088                 attributes->l3.ipv4.hdr = (struct ipv4_hdr){
3089                         .src_addr = input->flow.ip4_flow.src_ip,
3090                         .dst_addr = input->flow.ip4_flow.dst_ip,
3091                         .time_to_live = input->flow.ip4_flow.ttl,
3092                         .type_of_service = input->flow.ip4_flow.tos,
3093                         .next_proto_id = input->flow.ip4_flow.proto,
3094                 };
3095                 attributes->l3_mask.ipv4.hdr = (struct ipv4_hdr){
3096                         .src_addr = mask->ipv4_mask.src_ip,
3097                         .dst_addr = mask->ipv4_mask.dst_ip,
3098                         .time_to_live = mask->ipv4_mask.ttl,
3099                         .type_of_service = mask->ipv4_mask.tos,
3100                         .next_proto_id = mask->ipv4_mask.proto,
3101                 };
3102                 attributes->items[1] = (struct rte_flow_item){
3103                         .type = RTE_FLOW_ITEM_TYPE_IPV4,
3104                         .spec = &attributes->l3,
3105                         .mask = &attributes->l3_mask,
3106                 };
3107                 break;
3108         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
3109         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
3110         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
3111                 attributes->l3.ipv6.hdr = (struct ipv6_hdr){
3112                         .hop_limits = input->flow.ipv6_flow.hop_limits,
3113                         .proto = input->flow.ipv6_flow.proto,
3114                 };
3115
3116                 memcpy(attributes->l3.ipv6.hdr.src_addr,
3117                        input->flow.ipv6_flow.src_ip,
3118                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
3119                 memcpy(attributes->l3.ipv6.hdr.dst_addr,
3120                        input->flow.ipv6_flow.dst_ip,
3121                        RTE_DIM(attributes->l3.ipv6.hdr.src_addr));
3122                 memcpy(attributes->l3_mask.ipv6.hdr.src_addr,
3123                        mask->ipv6_mask.src_ip,
3124                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
3125                 memcpy(attributes->l3_mask.ipv6.hdr.dst_addr,
3126                        mask->ipv6_mask.dst_ip,
3127                        RTE_DIM(attributes->l3_mask.ipv6.hdr.src_addr));
3128                 attributes->items[1] = (struct rte_flow_item){
3129                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
3130                         .spec = &attributes->l3,
3131                         .mask = &attributes->l3_mask,
3132                 };
3133                 break;
3134         default:
3135                 DRV_LOG(ERR, "port %u invalid flow type%d",
3136                         dev->data->port_id, fdir_filter->input.flow_type);
3137                 rte_errno = ENOTSUP;
3138                 return -rte_errno;
3139         }
3140         /* Handle L4. */
3141         switch (fdir_filter->input.flow_type) {
3142         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
3143                 attributes->l4.udp.hdr = (struct udp_hdr){
3144                         .src_port = input->flow.udp4_flow.src_port,
3145                         .dst_port = input->flow.udp4_flow.dst_port,
3146                 };
3147                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
3148                         .src_port = mask->src_port_mask,
3149                         .dst_port = mask->dst_port_mask,
3150                 };
3151                 attributes->items[2] = (struct rte_flow_item){
3152                         .type = RTE_FLOW_ITEM_TYPE_UDP,
3153                         .spec = &attributes->l4,
3154                         .mask = &attributes->l4_mask,
3155                 };
3156                 break;
3157         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
3158                 attributes->l4.tcp.hdr = (struct tcp_hdr){
3159                         .src_port = input->flow.tcp4_flow.src_port,
3160                         .dst_port = input->flow.tcp4_flow.dst_port,
3161                 };
3162                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
3163                         .src_port = mask->src_port_mask,
3164                         .dst_port = mask->dst_port_mask,
3165                 };
3166                 attributes->items[2] = (struct rte_flow_item){
3167                         .type = RTE_FLOW_ITEM_TYPE_TCP,
3168                         .spec = &attributes->l4,
3169                         .mask = &attributes->l4_mask,
3170                 };
3171                 break;
3172         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
3173                 attributes->l4.udp.hdr = (struct udp_hdr){
3174                         .src_port = input->flow.udp6_flow.src_port,
3175                         .dst_port = input->flow.udp6_flow.dst_port,
3176                 };
3177                 attributes->l4_mask.udp.hdr = (struct udp_hdr){
3178                         .src_port = mask->src_port_mask,
3179                         .dst_port = mask->dst_port_mask,
3180                 };
3181                 attributes->items[2] = (struct rte_flow_item){
3182                         .type = RTE_FLOW_ITEM_TYPE_UDP,
3183                         .spec = &attributes->l4,
3184                         .mask = &attributes->l4_mask,
3185                 };
3186                 break;
3187         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
3188                 attributes->l4.tcp.hdr = (struct tcp_hdr){
3189                         .src_port = input->flow.tcp6_flow.src_port,
3190                         .dst_port = input->flow.tcp6_flow.dst_port,
3191                 };
3192                 attributes->l4_mask.tcp.hdr = (struct tcp_hdr){
3193                         .src_port = mask->src_port_mask,
3194                         .dst_port = mask->dst_port_mask,
3195                 };
3196                 attributes->items[2] = (struct rte_flow_item){
3197                         .type = RTE_FLOW_ITEM_TYPE_TCP,
3198                         .spec = &attributes->l4,
3199                         .mask = &attributes->l4_mask,
3200                 };
3201                 break;
3202         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
3203         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
3204                 break;
3205         default:
3206                 DRV_LOG(ERR, "port %u invalid flow type%d",
3207                         dev->data->port_id, fdir_filter->input.flow_type);
3208                 rte_errno = ENOTSUP;
3209                 return -rte_errno;
3210         }
3211         return 0;
3212 }
3213
3214 /**
3215  * Add new flow director filter and store it in list.
3216  *
3217  * @param dev
3218  *   Pointer to Ethernet device.
3219  * @param fdir_filter
3220  *   Flow director filter to add.
3221  *
3222  * @return
3223  *   0 on success, a negative errno value otherwise and rte_errno is set.
3224  */
3225 static int
3226 mlx5_fdir_filter_add(struct rte_eth_dev *dev,
3227                      const struct rte_eth_fdir_filter *fdir_filter)
3228 {
3229         struct priv *priv = dev->data->dev_private;
3230         struct mlx5_fdir attributes = {
3231                 .attr.group = 0,
3232                 .l2_mask = {
3233                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
3234                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
3235                         .type = 0,
3236                 },
3237         };
3238         struct mlx5_flow_parse parser = {
3239                 .layer = HASH_RXQ_ETH,
3240         };
3241         struct rte_flow_error error;
3242         struct rte_flow *flow;
3243         int ret;
3244
3245         ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3246         if (ret)
3247                 return ret;
3248         ret = mlx5_flow_convert(dev, &attributes.attr, attributes.items,
3249                                 attributes.actions, &error, &parser);
3250         if (ret)
3251                 return ret;
3252         flow = mlx5_flow_list_create(dev, &priv->flows, &attributes.attr,
3253                                      attributes.items, attributes.actions,
3254                                      &error);
3255         if (flow) {
3256                 DRV_LOG(DEBUG, "port %u FDIR created %p", dev->data->port_id,
3257                         (void *)flow);
3258                 return 0;
3259         }
3260         return -rte_errno;
3261 }
3262
3263 /**
3264  * Delete specific filter.
3265  *
3266  * @param dev
3267  *   Pointer to Ethernet device.
3268  * @param fdir_filter
3269  *   Filter to be deleted.
3270  *
3271  * @return
3272  *   0 on success, a negative errno value otherwise and rte_errno is set.
3273  */
3274 static int
3275 mlx5_fdir_filter_delete(struct rte_eth_dev *dev,
3276                         const struct rte_eth_fdir_filter *fdir_filter)
3277 {
3278         struct priv *priv = dev->data->dev_private;
3279         struct mlx5_fdir attributes = {
3280                 .attr.group = 0,
3281         };
3282         struct mlx5_flow_parse parser = {
3283                 .create = 1,
3284                 .layer = HASH_RXQ_ETH,
3285         };
3286         struct rte_flow_error error;
3287         struct rte_flow *flow;
3288         unsigned int i;
3289         int ret;
3290
3291         ret = mlx5_fdir_filter_convert(dev, fdir_filter, &attributes);
3292         if (ret)
3293                 return ret;
3294         ret = mlx5_flow_convert(dev, &attributes.attr, attributes.items,
3295                                 attributes.actions, &error, &parser);
3296         if (ret)
3297                 goto exit;
3298         /*
3299          * Special case for drop action which is only set in the
3300          * specifications when the flow is created.  In this situation the
3301          * drop specification is missing.
3302          */
3303         if (parser.drop) {
3304                 struct ibv_flow_spec_action_drop *drop;
3305
3306                 drop = (void *)((uintptr_t)parser.queue[HASH_RXQ_ETH].ibv_attr +
3307                                 parser.queue[HASH_RXQ_ETH].offset);
3308                 *drop = (struct ibv_flow_spec_action_drop){
3309                         .type = IBV_FLOW_SPEC_ACTION_DROP,
3310                         .size = sizeof(struct ibv_flow_spec_action_drop),
3311                 };
3312                 parser.queue[HASH_RXQ_ETH].ibv_attr->num_of_specs++;
3313         }
3314         TAILQ_FOREACH(flow, &priv->flows, next) {
3315                 struct ibv_flow_attr *attr;
3316                 struct ibv_spec_header *attr_h;
3317                 void *spec;
3318                 struct ibv_flow_attr *flow_attr;
3319                 struct ibv_spec_header *flow_h;
3320                 void *flow_spec;
3321                 unsigned int specs_n;
3322
3323                 attr = parser.queue[HASH_RXQ_ETH].ibv_attr;
3324                 flow_attr = flow->frxq[HASH_RXQ_ETH].ibv_attr;
3325                 /* Compare first the attributes. */
3326                 if (memcmp(attr, flow_attr, sizeof(struct ibv_flow_attr)))
3327                         continue;
3328                 if (attr->num_of_specs == 0)
3329                         continue;
3330                 spec = (void *)((uintptr_t)attr +
3331                                 sizeof(struct ibv_flow_attr));
3332                 flow_spec = (void *)((uintptr_t)flow_attr +
3333                                      sizeof(struct ibv_flow_attr));
3334                 specs_n = RTE_MIN(attr->num_of_specs, flow_attr->num_of_specs);
3335                 for (i = 0; i != specs_n; ++i) {
3336                         attr_h = spec;
3337                         flow_h = flow_spec;
3338                         if (memcmp(spec, flow_spec,
3339                                    RTE_MIN(attr_h->size, flow_h->size)))
3340                                 goto wrong_flow;
3341                         spec = (void *)((uintptr_t)spec + attr_h->size);
3342                         flow_spec = (void *)((uintptr_t)flow_spec +
3343                                              flow_h->size);
3344                 }
3345                 /* At this point, the flow match. */
3346                 break;
3347 wrong_flow:
3348                 /* The flow does not match. */
3349                 continue;
3350         }
3351         ret = rte_errno; /* Save rte_errno before cleanup. */
3352         if (flow)
3353                 mlx5_flow_list_destroy(dev, &priv->flows, flow);
3354 exit:
3355         for (i = 0; i != hash_rxq_init_n; ++i) {
3356                 if (parser.queue[i].ibv_attr)
3357                         rte_free(parser.queue[i].ibv_attr);
3358         }
3359         rte_errno = ret; /* Restore rte_errno. */
3360         return -rte_errno;
3361 }
3362
3363 /**
3364  * Update queue for specific filter.
3365  *
3366  * @param dev
3367  *   Pointer to Ethernet device.
3368  * @param fdir_filter
3369  *   Filter to be updated.
3370  *
3371  * @return
3372  *   0 on success, a negative errno value otherwise and rte_errno is set.
3373  */
3374 static int
3375 mlx5_fdir_filter_update(struct rte_eth_dev *dev,
3376                         const struct rte_eth_fdir_filter *fdir_filter)
3377 {
3378         int ret;
3379
3380         ret = mlx5_fdir_filter_delete(dev, fdir_filter);
3381         if (ret)
3382                 return ret;
3383         return mlx5_fdir_filter_add(dev, fdir_filter);
3384 }
3385
3386 /**
3387  * Flush all filters.
3388  *
3389  * @param dev
3390  *   Pointer to Ethernet device.
3391  */
3392 static void
3393 mlx5_fdir_filter_flush(struct rte_eth_dev *dev)
3394 {
3395         struct priv *priv = dev->data->dev_private;
3396
3397         mlx5_flow_list_flush(dev, &priv->flows);
3398 }
3399
3400 /**
3401  * Get flow director information.
3402  *
3403  * @param dev
3404  *   Pointer to Ethernet device.
3405  * @param[out] fdir_info
3406  *   Resulting flow director information.
3407  */
3408 static void
3409 mlx5_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
3410 {
3411         struct priv *priv = dev->data->dev_private;
3412         struct rte_eth_fdir_masks *mask =
3413                 &priv->dev->data->dev_conf.fdir_conf.mask;
3414
3415         fdir_info->mode = priv->dev->data->dev_conf.fdir_conf.mode;
3416         fdir_info->guarant_spc = 0;
3417         rte_memcpy(&fdir_info->mask, mask, sizeof(fdir_info->mask));
3418         fdir_info->max_flexpayload = 0;
3419         fdir_info->flow_types_mask[0] = 0;
3420         fdir_info->flex_payload_unit = 0;
3421         fdir_info->max_flex_payload_segment_num = 0;
3422         fdir_info->flex_payload_limit = 0;
3423         memset(&fdir_info->flex_conf, 0, sizeof(fdir_info->flex_conf));
3424 }
3425
3426 /**
3427  * Deal with flow director operations.
3428  *
3429  * @param dev
3430  *   Pointer to Ethernet device.
3431  * @param filter_op
3432  *   Operation to perform.
3433  * @param arg
3434  *   Pointer to operation-specific structure.
3435  *
3436  * @return
3437  *   0 on success, a negative errno value otherwise and rte_errno is set.
3438  */
3439 static int
3440 mlx5_fdir_ctrl_func(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
3441                     void *arg)
3442 {
3443         struct priv *priv = dev->data->dev_private;
3444         enum rte_fdir_mode fdir_mode =
3445                 priv->dev->data->dev_conf.fdir_conf.mode;
3446
3447         if (filter_op == RTE_ETH_FILTER_NOP)
3448                 return 0;
3449         if (fdir_mode != RTE_FDIR_MODE_PERFECT &&
3450             fdir_mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
3451                 DRV_LOG(ERR, "port %u flow director mode %d not supported",
3452                         dev->data->port_id, fdir_mode);
3453                 rte_errno = EINVAL;
3454                 return -rte_errno;
3455         }
3456         switch (filter_op) {
3457         case RTE_ETH_FILTER_ADD:
3458                 return mlx5_fdir_filter_add(dev, arg);
3459         case RTE_ETH_FILTER_UPDATE:
3460                 return mlx5_fdir_filter_update(dev, arg);
3461         case RTE_ETH_FILTER_DELETE:
3462                 return mlx5_fdir_filter_delete(dev, arg);
3463         case RTE_ETH_FILTER_FLUSH:
3464                 mlx5_fdir_filter_flush(dev);
3465                 break;
3466         case RTE_ETH_FILTER_INFO:
3467                 mlx5_fdir_info_get(dev, arg);
3468                 break;
3469         default:
3470                 DRV_LOG(DEBUG, "port %u unknown operation %u",
3471                         dev->data->port_id, filter_op);
3472                 rte_errno = EINVAL;
3473                 return -rte_errno;
3474         }
3475         return 0;
3476 }
3477
3478 /**
3479  * Manage filter operations.
3480  *
3481  * @param dev
3482  *   Pointer to Ethernet device structure.
3483  * @param filter_type
3484  *   Filter type.
3485  * @param filter_op
3486  *   Operation to perform.
3487  * @param arg
3488  *   Pointer to operation-specific structure.
3489  *
3490  * @return
3491  *   0 on success, a negative errno value otherwise and rte_errno is set.
3492  */
3493 int
3494 mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
3495                      enum rte_filter_type filter_type,
3496                      enum rte_filter_op filter_op,
3497                      void *arg)
3498 {
3499         switch (filter_type) {
3500         case RTE_ETH_FILTER_GENERIC:
3501                 if (filter_op != RTE_ETH_FILTER_GET) {
3502                         rte_errno = EINVAL;
3503                         return -rte_errno;
3504                 }
3505                 *(const void **)arg = &mlx5_flow_ops;
3506                 return 0;
3507         case RTE_ETH_FILTER_FDIR:
3508                 return mlx5_fdir_ctrl_func(dev, filter_op, arg);
3509         default:
3510                 DRV_LOG(ERR, "port %u filter type (%d) not supported",
3511                         dev->data->port_id, filter_type);
3512                 rte_errno = ENOTSUP;
3513                 return -rte_errno;
3514         }
3515         return 0;
3516 }
3517
3518 /**
3519  * Detect number of Verbs flow priorities supported.
3520  *
3521  * @param dev
3522  *   Pointer to Ethernet device.
3523  *
3524  * @return
3525  *   number of supported Verbs flow priority.
3526  */
3527 unsigned int
3528 mlx5_get_max_verbs_prio(struct rte_eth_dev *dev)
3529 {
3530         struct priv *priv = dev->data->dev_private;
3531         unsigned int verb_priorities = MLX5_VERBS_FLOW_PRIO_8;
3532         struct {
3533                 struct ibv_flow_attr attr;
3534                 struct ibv_flow_spec_eth eth;
3535                 struct ibv_flow_spec_action_drop drop;
3536         } flow_attr = {
3537                 .attr = {
3538                         .num_of_specs = 2,
3539                 },
3540                 .eth = {
3541                         .type = IBV_FLOW_SPEC_ETH,
3542                         .size = sizeof(struct ibv_flow_spec_eth),
3543                 },
3544                 .drop = {
3545                         .size = sizeof(struct ibv_flow_spec_action_drop),
3546                         .type = IBV_FLOW_SPEC_ACTION_DROP,
3547                 },
3548         };
3549         struct ibv_flow *flow;
3550
3551         do {
3552                 flow_attr.attr.priority = verb_priorities - 1;
3553                 flow = mlx5_glue->create_flow(priv->flow_drop_queue->qp,
3554                                               &flow_attr.attr);
3555                 if (flow) {
3556                         claim_zero(mlx5_glue->destroy_flow(flow));
3557                         /* Try more priorities. */
3558                         verb_priorities *= 2;
3559                 } else {
3560                         /* Failed, restore last right number. */
3561                         verb_priorities /= 2;
3562                         break;
3563                 }
3564         } while (1);
3565         DRV_LOG(DEBUG, "port %u Verbs flow priorities: %d,"
3566                 " user flow priorities: %d",
3567                 dev->data->port_id, verb_priorities, MLX5_CTRL_FLOW_PRIORITY);
3568         return verb_priorities;
3569 }