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