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