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